import.c 55 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696
  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 <assert.h>
  23. #include <ctype.h>
  24. #include <fcntl.h>
  25. #include <stdio.h>
  26. #include <string.h>
  27. #include <stdarg.h>
  28. #include "wine/list.h"
  29. #include "build.h"
  30. /* standard C functions that are also exported from ntdll */
  31. static const char *stdc_names[] =
  32. {
  33. "abs",
  34. "atan",
  35. "atoi",
  36. "atol",
  37. "bsearch",
  38. "ceil",
  39. "cos",
  40. "fabs",
  41. "floor",
  42. "isalnum",
  43. "isalpha",
  44. "iscntrl",
  45. "isdigit",
  46. "isgraph",
  47. "islower",
  48. "isprint",
  49. "ispunct",
  50. "isspace",
  51. "isupper",
  52. "iswalpha",
  53. "iswctype",
  54. "iswdigit",
  55. "iswlower",
  56. "iswspace",
  57. "iswxdigit",
  58. "isxdigit",
  59. "labs",
  60. "log",
  61. "mbstowcs",
  62. "memchr",
  63. "memcmp",
  64. "memcpy",
  65. "memmove",
  66. "memset",
  67. "pow",
  68. "qsort",
  69. "sin",
  70. "sprintf",
  71. "sqrt",
  72. "sscanf",
  73. "strcat",
  74. "strchr",
  75. "strcmp",
  76. "strcpy",
  77. "strcspn",
  78. "strlen",
  79. "strncat",
  80. "strncmp",
  81. "strncpy",
  82. "strnlen",
  83. "strpbrk",
  84. "strrchr",
  85. "strspn",
  86. "strstr",
  87. "strtol",
  88. "strtoul",
  89. "swprintf",
  90. "tan",
  91. "tolower",
  92. "toupper",
  93. "towlower",
  94. "towupper",
  95. "vsprintf",
  96. "wcscat",
  97. "wcschr",
  98. "wcscmp",
  99. "wcscpy",
  100. "wcscspn",
  101. "wcslen",
  102. "wcsncat",
  103. "wcsncmp",
  104. "wcsncpy",
  105. "wcspbrk",
  106. "wcsrchr",
  107. "wcsspn",
  108. "wcsstr",
  109. "wcstok",
  110. "wcstol",
  111. "wcstombs",
  112. "wcstoul"
  113. };
  114. static const struct strarray stdc_functions = { ARRAY_SIZE(stdc_names), ARRAY_SIZE(stdc_names), stdc_names };
  115. struct import_func
  116. {
  117. const char *name;
  118. const char *export_name;
  119. int ordinal;
  120. int hint;
  121. };
  122. struct import
  123. {
  124. struct list entry; /* entry in global dll list */
  125. char *dll_name; /* exported file name of the dll */
  126. char *c_name; /* dll name as a C-compatible identifier */
  127. char *full_name; /* full name of the input file */
  128. dev_t dev; /* device/inode of the input file */
  129. ino_t ino;
  130. ORDDEF **exports; /* functions exported from this dll */
  131. int nb_exports; /* number of exported functions */
  132. struct import_func *imports; /* functions we want to import from this dll */
  133. int nb_imports; /* number of imported functions */
  134. int max_imports; /* size of imports array */
  135. };
  136. static struct strarray undef_symbols; /* list of undefined symbols */
  137. static struct strarray extra_ld_symbols; /* list of extra symbols that ld should resolve */
  138. static struct strarray delayed_imports; /* list of delayed import dlls */
  139. static struct strarray ext_link_imports; /* list of external symbols to link to */
  140. static struct list dll_imports = LIST_INIT( dll_imports );
  141. static struct list dll_delayed = LIST_INIT( dll_delayed );
  142. static struct strarray as_files;
  143. static const char import_func_prefix[] = "__wine$func$";
  144. static const char import_ord_prefix[] = "__wine$ord$";
  145. /* compare function names; helper for resolve_imports */
  146. static int name_cmp( const char **name, const char **entry )
  147. {
  148. return strcmp( *name, *entry );
  149. }
  150. /* compare function names; helper for resolve_imports */
  151. static int func_cmp( const void *func1, const void *func2 )
  152. {
  153. const ORDDEF *odp1 = *(const ORDDEF * const *)func1;
  154. const ORDDEF *odp2 = *(const ORDDEF * const *)func2;
  155. return strcmp( odp1->name ? odp1->name : odp1->export_name,
  156. odp2->name ? odp2->name : odp2->export_name );
  157. }
  158. /* remove a name from a name table */
  159. static inline void remove_name( struct strarray *table, unsigned int idx )
  160. {
  161. assert( idx < table->count );
  162. memmove( table->str + idx, table->str + idx + 1,
  163. (table->count - idx - 1) * sizeof(*table->str) );
  164. table->count--;
  165. }
  166. /* locate a name in a (sorted) list */
  167. static inline const char *find_name( const char *name, struct strarray table )
  168. {
  169. return strarray_bsearch( &table, name, name_cmp );
  170. }
  171. /* sort a name table */
  172. static inline void sort_names( struct strarray *table )
  173. {
  174. strarray_qsort( table, name_cmp );
  175. }
  176. /* locate an export in a (sorted) export list */
  177. static inline ORDDEF *find_export( const char *name, ORDDEF **table, int size )
  178. {
  179. ORDDEF func, *odp, **res = NULL;
  180. func.name = func.export_name = xstrdup(name);
  181. odp = &func;
  182. if (table) res = bsearch( &odp, table, size, sizeof(*table), func_cmp );
  183. free( func.name );
  184. return res ? *res : NULL;
  185. }
  186. static const char valid_chars[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_.";
  187. /* encode a dll name into a linker-compatible name */
  188. static char *encode_dll_name( const char *name )
  189. {
  190. char *p, *ret;
  191. int len = strlen(name);
  192. if (strendswith( name, ".dll" )) len -= 4;
  193. if (strspn( name, valid_chars ) >= len) return strmake( "%.*s", len, name );
  194. ret = p = xmalloc( len * 4 + 1 );
  195. for ( ; len > 0; len--, name++)
  196. {
  197. if (!strchr( valid_chars, *name )) p += sprintf( p, "$x%02x", *name );
  198. else *p++ = *name;
  199. }
  200. *p = 0;
  201. return ret;
  202. }
  203. /* decode a linker-compatible dll name */
  204. static char *decode_dll_name( const char **name )
  205. {
  206. const char *src = *name;
  207. char *p, *ret;
  208. ret = p = xmalloc( strlen( src ) + 5 );
  209. for ( ; *src; src++, p++)
  210. {
  211. if (*src != '$')
  212. {
  213. *p = *src;
  214. }
  215. else if (src[1] == 'x') /* hex escape */
  216. {
  217. int val = 0;
  218. src += 2;
  219. if (*src >= '0' && *src <= '9') val += *src - '0';
  220. else if (*src >= 'A' && *src <= 'F') val += *src - 'A' + 10;
  221. else if (*src >= 'a' && *src <= 'f') val += *src - 'a' + 10;
  222. else return NULL;
  223. val *= 16;
  224. src++;
  225. if (*src >= '0' && *src <= '9') val += *src - '0';
  226. else if (*src >= 'A' && *src <= 'F') val += *src - 'A' + 10;
  227. else if (*src >= 'a' && *src <= 'f') val += *src - 'a' + 10;
  228. else return NULL;
  229. *p = val;
  230. }
  231. else break; /* end of dll name */
  232. }
  233. *p = 0;
  234. if (!strchr( ret, '.' )) strcpy( p, ".dll" );
  235. *name = src;
  236. return ret;
  237. }
  238. /* free an import structure */
  239. static void free_imports( struct import *imp )
  240. {
  241. free( imp->exports );
  242. free( imp->imports );
  243. free( imp->dll_name );
  244. free( imp->c_name );
  245. free( imp->full_name );
  246. free( imp );
  247. }
  248. /* check whether a given dll is imported in delayed mode */
  249. static int is_delayed_import( const char *name )
  250. {
  251. unsigned int i;
  252. for (i = 0; i < delayed_imports.count; i++)
  253. {
  254. if (!strcmp( delayed_imports.str[i], name )) return 1;
  255. }
  256. return 0;
  257. }
  258. /* find an imported dll from its name */
  259. static struct import *find_import_dll( const char *name )
  260. {
  261. struct import *import;
  262. LIST_FOR_EACH_ENTRY( import, &dll_imports, struct import, entry )
  263. if (!strcasecmp( import->dll_name, name )) return import;
  264. LIST_FOR_EACH_ENTRY( import, &dll_delayed, struct import, entry )
  265. if (!strcasecmp( import->dll_name, name )) return import;
  266. return NULL;
  267. }
  268. /* open the .so library for a given dll in a specified path */
  269. static char *try_library_path( const char *path, const char *name )
  270. {
  271. char *buffer;
  272. int fd;
  273. buffer = strmake( "%s/lib%s.def", path, name );
  274. /* check if the file exists */
  275. if ((fd = open( buffer, O_RDONLY )) != -1)
  276. {
  277. close( fd );
  278. return buffer;
  279. }
  280. free( buffer );
  281. return NULL;
  282. }
  283. /* find the .def import library for a given dll */
  284. static char *find_library( const char *name )
  285. {
  286. char *fullname;
  287. unsigned int i;
  288. for (i = 0; i < lib_path.count; i++)
  289. {
  290. if ((fullname = try_library_path( lib_path.str[i], name ))) return fullname;
  291. }
  292. fatal_error( "could not open .def file for %s\n", name );
  293. return NULL;
  294. }
  295. /* read in the list of exported symbols of an import library */
  296. static DLLSPEC *read_import_lib( struct import *imp )
  297. {
  298. FILE *f;
  299. int i;
  300. struct stat stat;
  301. struct import *prev_imp;
  302. DLLSPEC *spec = alloc_dll_spec();
  303. f = open_input_file( NULL, imp->full_name );
  304. fstat( fileno(f), &stat );
  305. imp->dev = stat.st_dev;
  306. imp->ino = stat.st_ino;
  307. if (!parse_def_file( f, spec )) exit( 1 );
  308. close_input_file( f );
  309. /* check if we already imported that library from a different file */
  310. if ((prev_imp = find_import_dll( spec->file_name )))
  311. {
  312. if (prev_imp->dev != imp->dev || prev_imp->ino != imp->ino)
  313. fatal_error( "%s and %s have the same export name '%s'\n",
  314. prev_imp->full_name, imp->full_name, spec->file_name );
  315. free_dll_spec( spec );
  316. return NULL; /* the same file was already loaded, ignore this one */
  317. }
  318. if (spec->nb_entry_points)
  319. {
  320. imp->exports = xmalloc( spec->nb_entry_points * sizeof(*imp->exports) );
  321. for (i = 0; i < spec->nb_entry_points; i++)
  322. imp->exports[imp->nb_exports++] = &spec->entry_points[i];
  323. qsort( imp->exports, imp->nb_exports, sizeof(*imp->exports), func_cmp );
  324. }
  325. return spec;
  326. }
  327. /* build the dll exported name from the import lib name or path */
  328. static char *get_dll_name( const char *name, const char *filename )
  329. {
  330. char *ret;
  331. if (filename)
  332. {
  333. const char *basename = get_basename( filename );
  334. if (!strncmp( basename, "lib", 3 )) basename += 3;
  335. ret = xmalloc( strlen(basename) + 5 );
  336. strcpy( ret, basename );
  337. if (strendswith( ret, ".def" )) ret[strlen(ret)-4] = 0;
  338. }
  339. else
  340. {
  341. ret = xmalloc( strlen(name) + 5 );
  342. strcpy( ret, name );
  343. }
  344. if (!strchr( ret, '.' )) strcat( ret, ".dll" );
  345. return ret;
  346. }
  347. /* add a dll to the list of imports */
  348. void add_import_dll( const char *name, const char *filename )
  349. {
  350. DLLSPEC *spec;
  351. char *dll_name = get_dll_name( name, filename );
  352. struct import *imp = xmalloc( sizeof(*imp) );
  353. memset( imp, 0, sizeof(*imp) );
  354. if (filename) imp->full_name = xstrdup( filename );
  355. else imp->full_name = find_library( name );
  356. if (!(spec = read_import_lib( imp )))
  357. {
  358. free_imports( imp );
  359. return;
  360. }
  361. imp->dll_name = spec->file_name ? spec->file_name : dll_name;
  362. imp->c_name = make_c_identifier( imp->dll_name );
  363. if (is_delayed_import( imp->dll_name ))
  364. list_add_tail( &dll_delayed, &imp->entry );
  365. else
  366. list_add_tail( &dll_imports, &imp->entry );
  367. }
  368. /* add a library to the list of delayed imports */
  369. void add_delayed_import( const char *name )
  370. {
  371. struct import *imp;
  372. char *fullname = get_dll_name( name, NULL );
  373. strarray_add( &delayed_imports, fullname );
  374. if ((imp = find_import_dll( fullname )))
  375. {
  376. list_remove( &imp->entry );
  377. list_add_tail( &dll_delayed, &imp->entry );
  378. }
  379. }
  380. /* add a symbol to the list of extra symbols that ld must resolve */
  381. void add_extra_ld_symbol( const char *name )
  382. {
  383. strarray_add( &extra_ld_symbols, name );
  384. }
  385. /* retrieve an imported dll, adding one if necessary */
  386. static struct import *add_static_import_dll( const char *name )
  387. {
  388. struct import *import;
  389. if ((import = find_import_dll( name ))) return import;
  390. import = xmalloc( sizeof(*import) );
  391. memset( import, 0, sizeof(*import) );
  392. import->dll_name = xstrdup( name );
  393. import->full_name = xstrdup( name );
  394. import->c_name = make_c_identifier( name );
  395. if (is_delayed_import( name ))
  396. list_add_tail( &dll_delayed, &import->entry );
  397. else
  398. list_add_tail( &dll_imports, &import->entry );
  399. return import;
  400. }
  401. /* add a function to the list of imports from a given dll */
  402. static void add_import_func( struct import *imp, const char *name, const char *export_name,
  403. int ordinal, int hint )
  404. {
  405. if (imp->nb_imports == imp->max_imports)
  406. {
  407. imp->max_imports *= 2;
  408. if (imp->max_imports < 32) imp->max_imports = 32;
  409. imp->imports = xrealloc( imp->imports, imp->max_imports * sizeof(*imp->imports) );
  410. }
  411. imp->imports[imp->nb_imports].name = name;
  412. imp->imports[imp->nb_imports].export_name = export_name;
  413. imp->imports[imp->nb_imports].ordinal = ordinal;
  414. imp->imports[imp->nb_imports].hint = hint;
  415. imp->nb_imports++;
  416. }
  417. /* add an import for an undefined function of the form __wine$func$ */
  418. static void add_undef_import( const char *name, int is_ordinal )
  419. {
  420. char *dll_name = decode_dll_name( &name );
  421. int ordinal = 0;
  422. struct import *import;
  423. if (!dll_name) return;
  424. if (*name++ != '$') return;
  425. while (*name >= '0' && *name <= '9') ordinal = 10 * ordinal + *name++ - '0';
  426. if (*name++ != '$') return;
  427. if (!use_msvcrt && find_name( name, stdc_functions )) return;
  428. import = add_static_import_dll( dll_name );
  429. if (is_ordinal)
  430. add_import_func( import, NULL, xstrdup( name ), ordinal, 0 );
  431. else
  432. add_import_func( import, xstrdup( name ), NULL, ordinal, 0 );
  433. free( dll_name );
  434. }
  435. /* check if the spec file exports any stubs */
  436. static int has_stubs( const DLLSPEC *spec )
  437. {
  438. int i;
  439. if (unix_lib) return 0;
  440. for (i = 0; i < spec->nb_entry_points; i++)
  441. {
  442. ORDDEF *odp = &spec->entry_points[i];
  443. if (odp->type == TYPE_STUB) return 1;
  444. }
  445. return 0;
  446. }
  447. /* add the extra undefined symbols that will be contained in the generated spec file itself */
  448. static void add_extra_undef_symbols( DLLSPEC *spec )
  449. {
  450. add_extra_ld_symbol( spec->init_func );
  451. if (spec->type == SPEC_WIN16) add_extra_ld_symbol( "DllMain" );
  452. if (has_stubs( spec )) add_extra_ld_symbol( "__wine_spec_unimplemented_stub" );
  453. if (delayed_imports.count) add_extra_ld_symbol( "__wine_spec_delay_load" );
  454. }
  455. /* check if a given imported dll is not needed, taking forwards into account */
  456. static int check_unused( const struct import* imp, const DLLSPEC *spec )
  457. {
  458. int i;
  459. const char *file_name = imp->dll_name;
  460. size_t len = strlen( file_name );
  461. const char *p = strchr( file_name, '.' );
  462. if (p && !strcasecmp( p, ".dll" )) len = p - file_name;
  463. for (i = spec->base; i <= spec->limit; i++)
  464. {
  465. ORDDEF *odp = spec->ordinals[i];
  466. if (!odp || !(odp->flags & FLAG_FORWARD)) continue;
  467. if (!strncasecmp( odp->link_name, file_name, len ) &&
  468. odp->link_name[len] == '.')
  469. return 0; /* found a forward, it is used */
  470. }
  471. return 1;
  472. }
  473. /* check if a given forward does exist in one of the imported dlls */
  474. static void check_undefined_forwards( DLLSPEC *spec )
  475. {
  476. struct import *imp;
  477. char *link_name, *api_name, *dll_name, *p;
  478. int i;
  479. if (unix_lib) return;
  480. for (i = 0; i < spec->nb_entry_points; i++)
  481. {
  482. ORDDEF *odp = &spec->entry_points[i];
  483. if (!(odp->flags & FLAG_FORWARD)) continue;
  484. link_name = xstrdup( odp->link_name );
  485. p = strrchr( link_name, '.' );
  486. *p = 0;
  487. api_name = p + 1;
  488. dll_name = get_dll_name( link_name, NULL );
  489. if ((imp = find_import_dll( dll_name )))
  490. {
  491. if (!find_export( api_name, imp->exports, imp->nb_exports ))
  492. warning( "%s:%d: forward '%s' not found in %s\n",
  493. spec->src_name, odp->lineno, odp->link_name, imp->dll_name );
  494. }
  495. else warning( "%s:%d: forward '%s' not found in the imported dll list\n",
  496. spec->src_name, odp->lineno, odp->link_name );
  497. free( link_name );
  498. free( dll_name );
  499. }
  500. }
  501. /* flag the dll exports that link to an undefined symbol */
  502. static void check_undefined_exports( DLLSPEC *spec )
  503. {
  504. int i;
  505. if (unix_lib) return;
  506. for (i = 0; i < spec->nb_entry_points; i++)
  507. {
  508. ORDDEF *odp = &spec->entry_points[i];
  509. if (odp->type == TYPE_STUB || odp->type == TYPE_ABS || odp->type == TYPE_VARIABLE) continue;
  510. if (odp->flags & FLAG_FORWARD) continue;
  511. if (odp->flags & FLAG_SYSCALL) continue;
  512. if (find_name( odp->link_name, undef_symbols ))
  513. {
  514. switch(odp->type)
  515. {
  516. case TYPE_PASCAL:
  517. case TYPE_STDCALL:
  518. case TYPE_CDECL:
  519. case TYPE_VARARGS:
  520. if (link_ext_symbols)
  521. {
  522. odp->flags |= FLAG_EXT_LINK;
  523. strarray_add( &ext_link_imports, odp->link_name );
  524. }
  525. else error( "%s:%d: function '%s' not defined\n",
  526. spec->src_name, odp->lineno, odp->link_name );
  527. break;
  528. default:
  529. if (!strcmp( odp->link_name, "__wine_syscall_dispatcher" )) break;
  530. error( "%s:%d: external symbol '%s' is not a function\n",
  531. spec->src_name, odp->lineno, odp->link_name );
  532. break;
  533. }
  534. }
  535. }
  536. }
  537. /* create a .o file that references all the undefined symbols we want to resolve */
  538. static char *create_undef_symbols_file( DLLSPEC *spec )
  539. {
  540. char *as_file, *obj_file;
  541. int i;
  542. unsigned int j;
  543. if (unix_lib) return NULL;
  544. as_file = open_temp_output_file( ".s" );
  545. output( "\t.data\n" );
  546. for (i = 0; i < spec->nb_entry_points; i++)
  547. {
  548. ORDDEF *odp = &spec->entry_points[i];
  549. if (odp->type == TYPE_STUB || odp->type == TYPE_ABS || odp->type == TYPE_VARIABLE) continue;
  550. if (odp->flags & FLAG_FORWARD) continue;
  551. if (odp->flags & FLAG_SYSCALL) continue;
  552. output( "\t%s %s\n", get_asm_ptr_keyword(), asm_name( get_link_name( odp )));
  553. }
  554. for (j = 0; j < extra_ld_symbols.count; j++)
  555. output( "\t%s %s\n", get_asm_ptr_keyword(), asm_name(extra_ld_symbols.str[j]) );
  556. fclose( output_file );
  557. obj_file = get_temp_file_name( output_file_name, ".o" );
  558. assemble_file( as_file, obj_file );
  559. return obj_file;
  560. }
  561. /* combine a list of object files with ld into a single object file */
  562. /* returns the name of the combined file */
  563. static const char *ldcombine_files( DLLSPEC *spec, struct strarray files )
  564. {
  565. char *ld_tmp_file, *undef_file;
  566. struct strarray args = get_ld_command();
  567. undef_file = create_undef_symbols_file( spec );
  568. ld_tmp_file = get_temp_file_name( output_file_name, ".o" );
  569. strarray_add( &args, "-r" );
  570. strarray_add( &args, "-o" );
  571. strarray_add( &args, ld_tmp_file );
  572. if (undef_file) strarray_add( &args, undef_file );
  573. strarray_addall( &args, files );
  574. spawn( args );
  575. return ld_tmp_file;
  576. }
  577. /* read in the list of undefined symbols */
  578. void read_undef_symbols( DLLSPEC *spec, struct strarray files )
  579. {
  580. size_t prefix_len;
  581. FILE *f;
  582. const char *prog = get_nm_command();
  583. char *cmd, buffer[1024], name_prefix[16];
  584. int err;
  585. const char *name;
  586. if (!files.count) return;
  587. add_extra_undef_symbols( spec );
  588. strcpy( name_prefix, asm_name("") );
  589. prefix_len = strlen( name_prefix );
  590. name = ldcombine_files( spec, files );
  591. cmd = strmake( "%s -u %s", prog, name );
  592. if (verbose)
  593. fprintf( stderr, "%s\n", cmd );
  594. if (!(f = popen( cmd, "r" )))
  595. fatal_error( "Cannot execute '%s'\n", cmd );
  596. while (fgets( buffer, sizeof(buffer), f ))
  597. {
  598. char *p = buffer + strlen(buffer) - 1;
  599. if (p < buffer) continue;
  600. if (*p == '\n') *p-- = 0;
  601. p = buffer;
  602. while (*p == ' ') p++;
  603. if (p[0] == 'U' && p[1] == ' ' && p[2]) p += 2;
  604. if (prefix_len && !strncmp( p, name_prefix, prefix_len )) p += prefix_len;
  605. if (!strncmp( p, import_func_prefix, strlen(import_func_prefix) ))
  606. add_undef_import( p + strlen( import_func_prefix ), 0 );
  607. else if (!strncmp( p, import_ord_prefix, strlen(import_ord_prefix) ))
  608. add_undef_import( p + strlen( import_ord_prefix ), 1 );
  609. else if (use_msvcrt || !find_name( p, stdc_functions ))
  610. strarray_add( &undef_symbols, xstrdup( p ));
  611. }
  612. if ((err = pclose( f ))) warning( "%s failed with status %d\n", cmd, err );
  613. free( cmd );
  614. }
  615. void resolve_dll_imports( DLLSPEC *spec, struct list *list )
  616. {
  617. unsigned int j;
  618. struct import *imp, *next;
  619. ORDDEF *odp;
  620. LIST_FOR_EACH_ENTRY_SAFE( imp, next, list, struct import, entry )
  621. {
  622. for (j = 0; j < undef_symbols.count; j++)
  623. {
  624. odp = find_export( undef_symbols.str[j], imp->exports, imp->nb_exports );
  625. if (odp)
  626. {
  627. if (odp->flags & FLAG_PRIVATE) continue;
  628. if (odp->type != TYPE_STDCALL && odp->type != TYPE_CDECL)
  629. warning( "winebuild: Data export '%s' cannot be imported from %s\n",
  630. odp->link_name, imp->dll_name );
  631. else
  632. {
  633. add_import_func( imp, (odp->flags & FLAG_NONAME) ? NULL : odp->name,
  634. odp->export_name, odp->ordinal, odp->hint );
  635. remove_name( &undef_symbols, j-- );
  636. }
  637. }
  638. }
  639. if (!imp->nb_imports)
  640. {
  641. /* the dll is not used, get rid of it */
  642. if (check_unused( imp, spec ))
  643. warning( "winebuild: %s imported but no symbols used\n", imp->dll_name );
  644. list_remove( &imp->entry );
  645. free_imports( imp );
  646. }
  647. }
  648. }
  649. /* resolve the imports for a Win32 module */
  650. void resolve_imports( DLLSPEC *spec )
  651. {
  652. check_undefined_forwards( spec );
  653. resolve_dll_imports( spec, &dll_imports );
  654. resolve_dll_imports( spec, &dll_delayed );
  655. sort_names( &undef_symbols );
  656. check_undefined_exports( spec );
  657. }
  658. /* check if symbol is still undefined */
  659. int is_undefined( const char *name )
  660. {
  661. return find_name( name, undef_symbols ) != NULL;
  662. }
  663. /* output the get_pc thunk if needed */
  664. void output_get_pc_thunk(void)
  665. {
  666. assert( target.cpu == CPU_i386 );
  667. output( "\n\t.text\n" );
  668. output( "\t.align %d\n", get_alignment(4) );
  669. output( "\t%s\n", func_declaration("__wine_spec_get_pc_thunk_eax") );
  670. output( "%s:\n", asm_name("__wine_spec_get_pc_thunk_eax") );
  671. output_cfi( ".cfi_startproc" );
  672. output( "\tmovl (%%esp),%%eax\n" );
  673. output( "\tret\n" );
  674. output_cfi( ".cfi_endproc" );
  675. output_function_size( "__wine_spec_get_pc_thunk_eax" );
  676. }
  677. /* output a single import thunk */
  678. static void output_import_thunk( const char *name, const char *table, int pos )
  679. {
  680. output( "\n\t.align %d\n", get_alignment(4) );
  681. output( "\t%s\n", func_declaration(name) );
  682. output( "%s\n", asm_globl(name) );
  683. output_cfi( ".cfi_startproc" );
  684. switch (target.cpu)
  685. {
  686. case CPU_i386:
  687. if (!UsePIC)
  688. {
  689. output( "\tjmp *(%s+%d)\n", table, pos );
  690. }
  691. else
  692. {
  693. output( "\tcall %s\n", asm_name("__wine_spec_get_pc_thunk_eax") );
  694. output( "1:\tjmp *%s+%d-1b(%%eax)\n", table, pos );
  695. needs_get_pc_thunk = 1;
  696. }
  697. break;
  698. case CPU_x86_64:
  699. output( "\tjmpq *%s+%d(%%rip)\n", table, pos );
  700. break;
  701. case CPU_ARM:
  702. if (UsePIC)
  703. {
  704. output( "\tldr ip, 2f\n");
  705. output( "1:\tadd ip, pc\n" );
  706. output( "\tldr pc, [ip]\n");
  707. output( "2:\t.long %s+%u-1b-%u\n", table, pos, thumb_mode ? 4 : 8 );
  708. }
  709. else
  710. {
  711. output( "\tldr ip, 1f\n");
  712. output( "\tldr pc, [ip]\n");
  713. output( "1:\t.long %s+%u\n", table, pos );
  714. }
  715. break;
  716. case CPU_ARM64:
  717. output( "\tadrp x16, %s\n", arm64_page( table ) );
  718. output( "\tadd x16, x16, #%s\n", arm64_pageoff( table ) );
  719. if (pos & ~0x7fff) output( "\tadd x16, x16, #%u\n", pos & ~0x7fff );
  720. output( "\tldr x16, [x16, #%u]\n", pos & 0x7fff );
  721. output( "\tbr x16\n" );
  722. break;
  723. }
  724. output_cfi( ".cfi_endproc" );
  725. output_function_size( name );
  726. }
  727. /* check if we need an import directory */
  728. int has_imports(void)
  729. {
  730. return !list_empty( &dll_imports );
  731. }
  732. /* output the import table of a Win32 module */
  733. static void output_immediate_imports(void)
  734. {
  735. int i, j;
  736. struct import *import;
  737. if (list_empty( &dll_imports )) return; /* no immediate imports */
  738. /* main import header */
  739. output( "\n/* import table */\n" );
  740. output( "\n\t.data\n" );
  741. output( "\t.align %d\n", get_alignment(4) );
  742. output( ".L__wine_spec_imports:\n" );
  743. /* list of dlls */
  744. j = 0;
  745. LIST_FOR_EACH_ENTRY( import, &dll_imports, struct import, entry )
  746. {
  747. output_rva( ".L__wine_spec_import_data_names + %d", j * get_ptr_size() ); /* OriginalFirstThunk */
  748. output( "\t.long 0\n" ); /* TimeDateStamp */
  749. output( "\t.long 0\n" ); /* ForwarderChain */
  750. output_rva( ".L__wine_spec_import_name_%s", import->c_name ); /* Name */
  751. output_rva( ".L__wine_spec_import_data_ptrs + %d", j * get_ptr_size() ); /* FirstThunk */
  752. j += import->nb_imports + 1;
  753. }
  754. output( "\t.long 0\n" ); /* OriginalFirstThunk */
  755. output( "\t.long 0\n" ); /* TimeDateStamp */
  756. output( "\t.long 0\n" ); /* ForwarderChain */
  757. output( "\t.long 0\n" ); /* Name */
  758. output( "\t.long 0\n" ); /* FirstThunk */
  759. output( "\n\t.align %d\n", get_alignment(get_ptr_size()) );
  760. /* output the names twice, once for OriginalFirstThunk and once for FirstThunk */
  761. for (i = 0; i < 2; i++)
  762. {
  763. output( ".L__wine_spec_import_data_%s:\n", i ? "ptrs" : "names" );
  764. LIST_FOR_EACH_ENTRY( import, &dll_imports, struct import, entry )
  765. {
  766. for (j = 0; j < import->nb_imports; j++)
  767. {
  768. struct import_func *func = &import->imports[j];
  769. if (i)
  770. {
  771. if (func->name) output( "__imp_%s:\n", asm_name( func->name ));
  772. else if (func->export_name) output( "__imp_%s:\n", asm_name( func->export_name ));
  773. }
  774. if (func->name)
  775. output( "\t%s .L__wine_spec_import_data_%s_%s-.L__wine_spec_rva_base\n",
  776. get_asm_ptr_keyword(), import->c_name, func->name );
  777. else
  778. {
  779. if (get_ptr_size() == 8)
  780. output( "\t.quad 0x800000000000%04x\n", func->ordinal );
  781. else
  782. output( "\t.long 0x8000%04x\n", func->ordinal );
  783. }
  784. }
  785. output( "\t%s 0\n", get_asm_ptr_keyword() );
  786. }
  787. }
  788. output( ".L__wine_spec_imports_end:\n" );
  789. LIST_FOR_EACH_ENTRY( import, &dll_imports, struct import, entry )
  790. {
  791. for (j = 0; j < import->nb_imports; j++)
  792. {
  793. struct import_func *func = &import->imports[j];
  794. if (!func->name) continue;
  795. output( "\t.align %d\n", get_alignment(2) );
  796. output( ".L__wine_spec_import_data_%s_%s:\n", import->c_name, func->name );
  797. output( "\t.short %d\n", func->hint );
  798. output( "\t%s \"%s\"\n", get_asm_string_keyword(), func->name );
  799. }
  800. }
  801. LIST_FOR_EACH_ENTRY( import, &dll_imports, struct import, entry )
  802. {
  803. output( ".L__wine_spec_import_name_%s:\n\t%s \"%s\"\n",
  804. import->c_name, get_asm_string_keyword(), import->dll_name );
  805. }
  806. }
  807. /* output the import thunks of a Win32 module */
  808. static void output_immediate_import_thunks(void)
  809. {
  810. int j, pos;
  811. struct import *import;
  812. static const char import_thunks[] = "__wine_spec_import_thunks";
  813. if (list_empty( &dll_imports )) return;
  814. output( "\n/* immediate import thunks */\n\n" );
  815. output( "\t.text\n" );
  816. output( "\t.align %d\n", get_alignment(8) );
  817. output( "%s:\n", asm_name(import_thunks));
  818. pos = 0;
  819. LIST_FOR_EACH_ENTRY( import, &dll_imports, struct import, entry )
  820. {
  821. for (j = 0; j < import->nb_imports; j++, pos += get_ptr_size())
  822. {
  823. struct import_func *func = &import->imports[j];
  824. output_import_thunk( func->name ? func->name : func->export_name,
  825. ".L__wine_spec_import_data_ptrs", pos );
  826. }
  827. pos += get_ptr_size();
  828. }
  829. output_function_size( import_thunks );
  830. }
  831. /* output the delayed import table of a Win32 module */
  832. static void output_delayed_imports( const DLLSPEC *spec )
  833. {
  834. int j, mod;
  835. struct import *import;
  836. if (list_empty( &dll_delayed )) return;
  837. output( "\n/* delayed imports */\n\n" );
  838. output( "\t.data\n" );
  839. output( "\t.align %d\n", get_alignment(get_ptr_size()) );
  840. output( "%s\n", asm_globl("__wine_spec_delay_imports") );
  841. /* list of dlls */
  842. j = mod = 0;
  843. LIST_FOR_EACH_ENTRY( import, &dll_delayed, struct import, entry )
  844. {
  845. output( "\t%s 0\n", get_asm_ptr_keyword() ); /* grAttrs */
  846. output( "\t%s .L__wine_delay_name_%s\n", /* szName */
  847. get_asm_ptr_keyword(), import->c_name );
  848. output( "\t%s .L__wine_delay_modules+%d\n", /* phmod */
  849. get_asm_ptr_keyword(), mod * get_ptr_size() );
  850. output( "\t%s .L__wine_delay_IAT+%d\n", /* pIAT */
  851. get_asm_ptr_keyword(), j * get_ptr_size() );
  852. output( "\t%s .L__wine_delay_INT+%d\n", /* pINT */
  853. get_asm_ptr_keyword(), j * get_ptr_size() );
  854. output( "\t%s 0\n", get_asm_ptr_keyword() ); /* pBoundIAT */
  855. output( "\t%s 0\n", get_asm_ptr_keyword() ); /* pUnloadIAT */
  856. output( "\t%s 0\n", get_asm_ptr_keyword() ); /* dwTimeStamp */
  857. j += import->nb_imports;
  858. mod++;
  859. }
  860. output( "\t%s 0\n", get_asm_ptr_keyword() ); /* grAttrs */
  861. output( "\t%s 0\n", get_asm_ptr_keyword() ); /* szName */
  862. output( "\t%s 0\n", get_asm_ptr_keyword() ); /* phmod */
  863. output( "\t%s 0\n", get_asm_ptr_keyword() ); /* pIAT */
  864. output( "\t%s 0\n", get_asm_ptr_keyword() ); /* pINT */
  865. output( "\t%s 0\n", get_asm_ptr_keyword() ); /* pBoundIAT */
  866. output( "\t%s 0\n", get_asm_ptr_keyword() ); /* pUnloadIAT */
  867. output( "\t%s 0\n", get_asm_ptr_keyword() ); /* dwTimeStamp */
  868. output( "\n.L__wine_delay_IAT:\n" );
  869. LIST_FOR_EACH_ENTRY( import, &dll_delayed, struct import, entry )
  870. {
  871. for (j = 0; j < import->nb_imports; j++)
  872. {
  873. struct import_func *func = &import->imports[j];
  874. const char *name = func->name ? func->name : func->export_name;
  875. output( "__imp_%s:\n", asm_name( name ));
  876. output( "\t%s __wine_delay_imp_%s_%s\n",
  877. get_asm_ptr_keyword(), import->c_name, name );
  878. }
  879. }
  880. output( "\n.L__wine_delay_INT:\n" );
  881. LIST_FOR_EACH_ENTRY( import, &dll_delayed, struct import, entry )
  882. {
  883. for (j = 0; j < import->nb_imports; j++)
  884. {
  885. struct import_func *func = &import->imports[j];
  886. if (!func->name)
  887. output( "\t%s %d\n", get_asm_ptr_keyword(), func->ordinal );
  888. else
  889. output( "\t%s .L__wine_delay_data_%s_%s\n",
  890. get_asm_ptr_keyword(), import->c_name, func->name );
  891. }
  892. }
  893. output( "\n.L__wine_delay_modules:\n" );
  894. LIST_FOR_EACH_ENTRY( import, &dll_delayed, struct import, entry )
  895. {
  896. output( "\t%s 0\n", get_asm_ptr_keyword() );
  897. }
  898. LIST_FOR_EACH_ENTRY( import, &dll_delayed, struct import, entry )
  899. {
  900. output( ".L__wine_delay_name_%s:\n", import->c_name );
  901. output( "\t%s \"%s\"\n", get_asm_string_keyword(), import->dll_name );
  902. }
  903. LIST_FOR_EACH_ENTRY( import, &dll_delayed, struct import, entry )
  904. {
  905. for (j = 0; j < import->nb_imports; j++)
  906. {
  907. struct import_func *func = &import->imports[j];
  908. if (!func->name) continue;
  909. output( ".L__wine_delay_data_%s_%s:\n", import->c_name, func->name );
  910. output( "\t%s \"%s\"\n", get_asm_string_keyword(), func->name );
  911. }
  912. }
  913. output_function_size( "__wine_spec_delay_imports" );
  914. }
  915. /* output the delayed import thunks of a Win32 module */
  916. static void output_delayed_import_thunks( const DLLSPEC *spec )
  917. {
  918. int idx, j, pos;
  919. struct import *import;
  920. static const char delayed_import_loaders[] = "__wine_spec_delayed_import_loaders";
  921. static const char delayed_import_thunks[] = "__wine_spec_delayed_import_thunks";
  922. if (list_empty( &dll_delayed )) return;
  923. output( "\n/* delayed import thunks */\n\n" );
  924. output( "\t.text\n" );
  925. output( "\t.align %d\n", get_alignment(8) );
  926. output( "%s:\n", asm_name(delayed_import_loaders));
  927. output( "\t%s\n", func_declaration("__wine_delay_load_asm") );
  928. output( "%s:\n", asm_name("__wine_delay_load_asm") );
  929. output_cfi( ".cfi_startproc" );
  930. switch (target.cpu)
  931. {
  932. case CPU_i386:
  933. output( "\tpushl %%ecx\n" );
  934. output_cfi( ".cfi_adjust_cfa_offset 4" );
  935. output( "\tpushl %%edx\n" );
  936. output_cfi( ".cfi_adjust_cfa_offset 4" );
  937. output( "\tpushl %%eax\n" );
  938. output_cfi( ".cfi_adjust_cfa_offset 4" );
  939. output( "\tcall %s\n", asm_name("__wine_spec_delay_load") );
  940. output_cfi( ".cfi_adjust_cfa_offset -4" );
  941. output( "\tpopl %%edx\n" );
  942. output_cfi( ".cfi_adjust_cfa_offset -4" );
  943. output( "\tpopl %%ecx\n" );
  944. output_cfi( ".cfi_adjust_cfa_offset -4" );
  945. output( "\tjmp *%%eax\n" );
  946. break;
  947. case CPU_x86_64:
  948. output( "\tsubq $0x98,%%rsp\n" );
  949. output_cfi( ".cfi_adjust_cfa_offset 0x98" );
  950. output( "\tmovq %%rdx,0x88(%%rsp)\n" );
  951. output( "\tmovq %%rcx,0x80(%%rsp)\n" );
  952. output( "\tmovq %%r8,0x78(%%rsp)\n" );
  953. output( "\tmovq %%r9,0x70(%%rsp)\n" );
  954. output( "\tmovq %%r10,0x68(%%rsp)\n" );
  955. output( "\tmovq %%r11,0x60(%%rsp)\n" );
  956. output( "\tmovups %%xmm0,0x50(%%rsp)\n" );
  957. output( "\tmovups %%xmm1,0x40(%%rsp)\n" );
  958. output( "\tmovups %%xmm2,0x30(%%rsp)\n" );
  959. output( "\tmovups %%xmm3,0x20(%%rsp)\n" );
  960. output( "\tmovq %%rax,%%rcx\n" );
  961. output( "\tcall %s\n", asm_name("__wine_spec_delay_load") );
  962. output( "\tmovups 0x20(%%rsp),%%xmm3\n" );
  963. output( "\tmovups 0x30(%%rsp),%%xmm2\n" );
  964. output( "\tmovups 0x40(%%rsp),%%xmm1\n" );
  965. output( "\tmovups 0x50(%%rsp),%%xmm0\n" );
  966. output( "\tmovq 0x60(%%rsp),%%r11\n" );
  967. output( "\tmovq 0x68(%%rsp),%%r10\n" );
  968. output( "\tmovq 0x70(%%rsp),%%r9\n" );
  969. output( "\tmovq 0x78(%%rsp),%%r8\n" );
  970. output( "\tmovq 0x80(%%rsp),%%rcx\n" );
  971. output( "\tmovq 0x88(%%rsp),%%rdx\n" );
  972. output( "\taddq $0x98,%%rsp\n" );
  973. output_cfi( ".cfi_adjust_cfa_offset -0x98" );
  974. output( "\tjmp *%%rax\n" );
  975. break;
  976. case CPU_ARM:
  977. output( "\tpush {r0-r3,FP,LR}\n" );
  978. output( "\tmov r0,IP\n" );
  979. output( "\tbl %s\n", asm_name("__wine_spec_delay_load") );
  980. output( "\tmov IP,r0\n");
  981. output( "\tpop {r0-r3,FP,LR}\n" );
  982. output( "\tbx IP\n");
  983. break;
  984. case CPU_ARM64:
  985. output( "\tstp x29, x30, [sp,#-80]!\n" );
  986. output( "\tmov x29, sp\n" );
  987. output( "\tstp x0, x1, [sp,#16]\n" );
  988. output( "\tstp x2, x3, [sp,#32]\n" );
  989. output( "\tstp x4, x5, [sp,#48]\n" );
  990. output( "\tstp x6, x7, [sp,#64]\n" );
  991. output( "\tmov x0, x16\n" );
  992. output( "\tbl %s\n", asm_name("__wine_spec_delay_load") );
  993. output( "\tmov x16, x0\n" );
  994. output( "\tldp x0, x1, [sp,#16]\n" );
  995. output( "\tldp x2, x3, [sp,#32]\n" );
  996. output( "\tldp x4, x5, [sp,#48]\n" );
  997. output( "\tldp x6, x7, [sp,#64]\n" );
  998. output( "\tldp x29, x30, [sp],#80\n" );
  999. output( "\tbr x16\n" );
  1000. break;
  1001. }
  1002. output_cfi( ".cfi_endproc" );
  1003. output_function_size( "__wine_delay_load_asm" );
  1004. output( "\n" );
  1005. idx = 0;
  1006. LIST_FOR_EACH_ENTRY( import, &dll_delayed, struct import, entry )
  1007. {
  1008. for (j = 0; j < import->nb_imports; j++)
  1009. {
  1010. struct import_func *func = &import->imports[j];
  1011. const char *name = func->name ? func->name : func->export_name;
  1012. if (thumb_mode) output( "\t.thumb_func\n" );
  1013. output( "__wine_delay_imp_%s_%s:\n", import->c_name, name );
  1014. output_cfi( ".cfi_startproc" );
  1015. switch (target.cpu)
  1016. {
  1017. case CPU_i386:
  1018. case CPU_x86_64:
  1019. output( "\tmovl $%d,%%eax\n", (idx << 16) | j );
  1020. output( "\tjmp %s\n", asm_name("__wine_delay_load_asm") );
  1021. break;
  1022. case CPU_ARM:
  1023. output( "\tmov ip, #%u\n", j );
  1024. if (idx) output( "\tmovt ip, #%u\n", idx );
  1025. output( "\tb %s\n", asm_name("__wine_delay_load_asm") );
  1026. break;
  1027. case CPU_ARM64:
  1028. if (idx)
  1029. {
  1030. output( "\tmov x16, #0x%x\n", idx << 16 );
  1031. if (j) output( "\tmovk x16, #0x%x\n", j );
  1032. }
  1033. else output( "\tmov x16, #0x%x\n", j );
  1034. output( "\tb %s\n", asm_name("__wine_delay_load_asm") );
  1035. break;
  1036. }
  1037. output_cfi( ".cfi_endproc" );
  1038. }
  1039. idx++;
  1040. }
  1041. output_function_size( delayed_import_loaders );
  1042. output( "\n\t.align %d\n", get_alignment(get_ptr_size()) );
  1043. output( "%s:\n", asm_name(delayed_import_thunks));
  1044. pos = 0;
  1045. LIST_FOR_EACH_ENTRY( import, &dll_delayed, struct import, entry )
  1046. {
  1047. for (j = 0; j < import->nb_imports; j++, pos += get_ptr_size())
  1048. {
  1049. struct import_func *func = &import->imports[j];
  1050. output_import_thunk( func->name ? func->name : func->export_name,
  1051. ".L__wine_delay_IAT", pos );
  1052. }
  1053. }
  1054. output_function_size( delayed_import_thunks );
  1055. }
  1056. /* output import stubs for exported entry points that link to external symbols */
  1057. static void output_external_link_imports( DLLSPEC *spec )
  1058. {
  1059. unsigned int i, pos;
  1060. if (!ext_link_imports.count) return; /* nothing to do */
  1061. sort_names( &ext_link_imports );
  1062. /* get rid of duplicate names */
  1063. for (i = 1; i < ext_link_imports.count; i++)
  1064. {
  1065. if (!strcmp( ext_link_imports.str[i-1], ext_link_imports.str[i] ))
  1066. remove_name( &ext_link_imports, i-- );
  1067. }
  1068. output( "\n/* external link thunks */\n\n" );
  1069. output( "\t.data\n" );
  1070. output( "\t.align %d\n", get_alignment(get_ptr_size()) );
  1071. output( ".L__wine_spec_external_links:\n" );
  1072. for (i = 0; i < ext_link_imports.count; i++)
  1073. output( "\t%s %s\n", get_asm_ptr_keyword(), asm_name(ext_link_imports.str[i]) );
  1074. output( "\n\t.text\n" );
  1075. output( "\t.align %d\n", get_alignment(get_ptr_size()) );
  1076. output( "%s:\n", asm_name("__wine_spec_external_link_thunks") );
  1077. for (i = pos = 0; i < ext_link_imports.count; i++)
  1078. {
  1079. char *buffer = strmake( "__wine_spec_ext_link_%s", ext_link_imports.str[i] );
  1080. output_import_thunk( buffer, ".L__wine_spec_external_links", pos );
  1081. free( buffer );
  1082. pos += get_ptr_size();
  1083. }
  1084. output_function_size( "__wine_spec_external_link_thunks" );
  1085. }
  1086. /*******************************************************************
  1087. * output_stubs
  1088. *
  1089. * Output the functions for stub entry points
  1090. */
  1091. void output_stubs( DLLSPEC *spec )
  1092. {
  1093. const char *name, *exp_name;
  1094. int i;
  1095. if (!has_stubs( spec )) return;
  1096. output( "\n/* stub functions */\n\n" );
  1097. output( "\t.text\n" );
  1098. for (i = 0; i < spec->nb_entry_points; i++)
  1099. {
  1100. ORDDEF *odp = &spec->entry_points[i];
  1101. if (odp->type != TYPE_STUB) continue;
  1102. name = get_stub_name( odp, spec );
  1103. exp_name = odp->name ? odp->name : odp->export_name;
  1104. output( "\t.align %d\n", get_alignment(4) );
  1105. output( "\t%s\n", func_declaration(name) );
  1106. output( "%s:\n", asm_name(name) );
  1107. output_cfi( ".cfi_startproc" );
  1108. switch (target.cpu)
  1109. {
  1110. case CPU_i386:
  1111. /* flesh out the stub a bit to make safedisc happy */
  1112. output(" \tnop\n" );
  1113. output(" \tnop\n" );
  1114. output(" \tnop\n" );
  1115. output(" \tnop\n" );
  1116. output(" \tnop\n" );
  1117. output(" \tnop\n" );
  1118. output(" \tnop\n" );
  1119. output(" \tnop\n" );
  1120. output(" \tnop\n" );
  1121. output( "\tsubl $12,%%esp\n" );
  1122. output_cfi( ".cfi_adjust_cfa_offset 12" );
  1123. if (UsePIC)
  1124. {
  1125. output( "\tcall %s\n", asm_name("__wine_spec_get_pc_thunk_eax") );
  1126. output( "1:" );
  1127. needs_get_pc_thunk = 1;
  1128. if (exp_name)
  1129. {
  1130. output( "\tleal .L%s_string-1b(%%eax),%%ecx\n", name );
  1131. output( "\tmovl %%ecx,4(%%esp)\n" );
  1132. }
  1133. else
  1134. output( "\tmovl $%d,4(%%esp)\n", odp->ordinal );
  1135. output( "\tleal .L__wine_spec_file_name-1b(%%eax),%%ecx\n" );
  1136. output( "\tmovl %%ecx,(%%esp)\n" );
  1137. }
  1138. else
  1139. {
  1140. if (exp_name)
  1141. output( "\tmovl $.L%s_string,4(%%esp)\n", name );
  1142. else
  1143. output( "\tmovl $%d,4(%%esp)\n", odp->ordinal );
  1144. output( "\tmovl $.L__wine_spec_file_name,(%%esp)\n" );
  1145. }
  1146. output( "\tcall %s\n", asm_name("__wine_spec_unimplemented_stub") );
  1147. break;
  1148. case CPU_x86_64:
  1149. output( "\tsubq $0x28,%%rsp\n" );
  1150. output_cfi( ".cfi_adjust_cfa_offset 8" );
  1151. output( "\tleaq .L__wine_spec_file_name(%%rip),%%rcx\n" );
  1152. if (exp_name)
  1153. output( "leaq .L%s_string(%%rip),%%rdx\n", name );
  1154. else
  1155. output( "\tmovq $%d,%%rdx\n", odp->ordinal );
  1156. output( "\tcall %s\n", asm_name("__wine_spec_unimplemented_stub") );
  1157. break;
  1158. case CPU_ARM:
  1159. if (UsePIC)
  1160. {
  1161. output( "\tldr r0,3f\n");
  1162. output( "1:\tadd r0,PC\n");
  1163. output( "\tldr r1,3f+4\n");
  1164. if (exp_name) output( "2:\tadd r1,PC\n");
  1165. output( "\tbl %s\n", asm_name("__wine_spec_unimplemented_stub") );
  1166. output( "3:\t.long .L__wine_spec_file_name-1b-%u\n", thumb_mode ? 4 : 8 );
  1167. if (exp_name) output( "\t.long .L%s_string-2b-%u\n", name, thumb_mode ? 4 : 8 );
  1168. else output( "\t.long %u\n", odp->ordinal );
  1169. }
  1170. else
  1171. {
  1172. output( "\tmovw r0,:lower16:.L__wine_spec_file_name\n");
  1173. output( "\tmovt r0,:upper16:.L__wine_spec_file_name\n");
  1174. if (exp_name)
  1175. {
  1176. output( "\tmovw r1,:lower16:.L%s_string\n", name );
  1177. output( "\tmovt r1,:upper16:.L%s_string\n", name );
  1178. }
  1179. else output( "\tmov r1,#%u\n", odp->ordinal );
  1180. output( "\tbl %s\n", asm_name("__wine_spec_unimplemented_stub") );
  1181. }
  1182. break;
  1183. case CPU_ARM64:
  1184. output( "\tadrp x0, %s\n", arm64_page(".L__wine_spec_file_name") );
  1185. output( "\tadd x0, x0, #%s\n", arm64_pageoff(".L__wine_spec_file_name") );
  1186. if (exp_name)
  1187. {
  1188. char *sym = strmake( ".L%s_string", name );
  1189. output( "\tadrp x1, %s\n", arm64_page( sym ) );
  1190. output( "\tadd x1, x1, #%s\n", arm64_pageoff( sym ) );
  1191. free( sym );
  1192. }
  1193. else
  1194. output( "\tmov x1, %u\n", odp->ordinal );
  1195. output( "\tbl %s\n", asm_name("__wine_spec_unimplemented_stub") );
  1196. break;
  1197. default:
  1198. assert(0);
  1199. }
  1200. output_cfi( ".cfi_endproc" );
  1201. output_function_size( name );
  1202. }
  1203. output( "\t%s\n", get_asm_string_section() );
  1204. output( ".L__wine_spec_file_name:\n" );
  1205. output( "\t%s \"%s\"\n", get_asm_string_keyword(), spec->file_name );
  1206. for (i = 0; i < spec->nb_entry_points; i++)
  1207. {
  1208. ORDDEF *odp = &spec->entry_points[i];
  1209. if (odp->type != TYPE_STUB) continue;
  1210. exp_name = odp->name ? odp->name : odp->export_name;
  1211. if (exp_name)
  1212. {
  1213. name = get_stub_name( odp, spec );
  1214. output( ".L%s_string:\n", name );
  1215. output( "\t%s \"%s\"\n", get_asm_string_keyword(), exp_name );
  1216. }
  1217. }
  1218. }
  1219. static int cmp_link_name( const void *e1, const void *e2 )
  1220. {
  1221. const ORDDEF *odp1 = *(const ORDDEF * const *)e1;
  1222. const ORDDEF *odp2 = *(const ORDDEF * const *)e2;
  1223. return strcmp( odp1->link_name, odp2->link_name );
  1224. }
  1225. /* output the functions for system calls */
  1226. void output_syscalls( DLLSPEC *spec )
  1227. {
  1228. int i, count;
  1229. ORDDEF **syscalls = NULL;
  1230. if (unix_lib) return;
  1231. for (i = count = 0; i < spec->nb_entry_points; i++)
  1232. {
  1233. ORDDEF *odp = &spec->entry_points[i];
  1234. if (!(odp->flags & FLAG_SYSCALL)) continue;
  1235. if (!syscalls) syscalls = xmalloc( (spec->nb_entry_points - i) * sizeof(*syscalls) );
  1236. syscalls[count++] = odp;
  1237. }
  1238. if (!count) return;
  1239. count = sort_func_list( syscalls, count, cmp_link_name );
  1240. output( "\n/* system calls */\n\n" );
  1241. output( "\t.text\n" );
  1242. for (i = 0; i < count; i++)
  1243. {
  1244. ORDDEF *odp = syscalls[i];
  1245. const char *name = get_link_name(odp);
  1246. unsigned int id = (spec->syscall_table << 12) + i;
  1247. output( "\t.align %d\n", get_alignment(16) );
  1248. output( "\t%s\n", func_declaration(name) );
  1249. output( "%s\n", asm_globl(name) );
  1250. output_cfi( ".cfi_startproc" );
  1251. switch (target.cpu)
  1252. {
  1253. case CPU_i386:
  1254. if (UsePIC)
  1255. {
  1256. output( "\tcall %s\n", asm_name("__wine_spec_get_pc_thunk_eax") );
  1257. output( "1:\tmovl %s-1b(%%eax),%%edx\n", asm_name("__wine_syscall_dispatcher") );
  1258. output( "\tmovl $%u,%%eax\n", id );
  1259. needs_get_pc_thunk = 1;
  1260. }
  1261. else
  1262. {
  1263. output( "\tmovl $%u,%%eax\n", id );
  1264. output( "\tmovl $%s,%%edx\n", asm_name("__wine_syscall") );
  1265. }
  1266. output( "\tcall *%%edx\n" );
  1267. output( "\tret $%u\n", odp->type == TYPE_STDCALL ? get_args_size( odp ) : 0 );
  1268. break;
  1269. case CPU_x86_64:
  1270. /* Chromium depends on syscall thunks having the same form as on
  1271. * Windows. For 64-bit systems the only viable form we can emulate is
  1272. * having an int $0x2e fallback. Since actually using an interrupt is
  1273. * expensive, and since for some reason Chromium doesn't actually
  1274. * validate that instruction, we can just put a jmp there instead. */
  1275. output( "\t.byte 0x4c,0x8b,0xd1\n" ); /* movq %rcx,%r10 */
  1276. output( "\t.byte 0xb8\n" ); /* movl $i,%eax */
  1277. output( "\t.long %u\n", id );
  1278. output( "\t.byte 0xf6,0x04,0x25,0x08,0x03,0xfe,0x7f,0x01\n" ); /* testb $1,0x7ffe0308 */
  1279. output( "\t.byte 0x75,0x03\n" ); /* jne 1f */
  1280. output( "\t.byte 0x0f,0x05\n" ); /* syscall */
  1281. output( "\t.byte 0xc3\n" ); /* ret */
  1282. output( "\tjmp 1f\n" );
  1283. output( "\t.byte 0xc3\n" ); /* ret */
  1284. if (is_pe())
  1285. {
  1286. output( "1:\t.byte 0xff,0x14,0x25\n" ); /* 1: callq *(0x7ffe1000) */
  1287. output( "\t.long 0x7ffe1000\n" );
  1288. }
  1289. else
  1290. {
  1291. output( "\tnop\n" );
  1292. output( "1:\tcallq *%s(%%rip)\n", asm_name("__wine_syscall_dispatcher") );
  1293. }
  1294. output( "\tret\n" );
  1295. break;
  1296. case CPU_ARM:
  1297. output( "\tpush {r0-r3}\n" );
  1298. output( "\tmovw ip, #%u\n", id );
  1299. output( "\tmov r3, lr\n" );
  1300. output( "\tbl %s\n", asm_name("__wine_syscall") );
  1301. output( "\tbx lr\n" );
  1302. break;
  1303. case CPU_ARM64:
  1304. output( "\tmov x8, #%u\n", id );
  1305. output( "\tmov x9, x30\n" );
  1306. output( "\tbl %s\n", asm_name("__wine_syscall" ));
  1307. output( "\tret\n" );
  1308. break;
  1309. default:
  1310. assert(0);
  1311. }
  1312. output_cfi( ".cfi_endproc" );
  1313. output_function_size( name );
  1314. }
  1315. switch (target.cpu)
  1316. {
  1317. case CPU_i386:
  1318. if (UsePIC) break;
  1319. output( "\t.align %d\n", get_alignment(16) );
  1320. output( "\t%s\n", func_declaration("__wine_syscall") );
  1321. output( "%s:\n", asm_name("__wine_syscall") );
  1322. output( "\tjmp *(%s)\n", asm_name("__wine_syscall_dispatcher") );
  1323. output_function_size( "__wine_syscall" );
  1324. break;
  1325. case CPU_ARM:
  1326. output( "\t.align %d\n", get_alignment(16) );
  1327. output( "\t%s\n", func_declaration("__wine_syscall") );
  1328. output( "%s:\n", asm_name("__wine_syscall") );
  1329. if (UsePIC)
  1330. {
  1331. output( "\tldr r0, 2f\n");
  1332. output( "1:\tadd r0, pc\n" );
  1333. }
  1334. else
  1335. {
  1336. output( "\tmovw r0, :lower16:%s\n", asm_name("__wine_syscall_dispatcher") );
  1337. output( "\tmovt r0, :upper16:%s\n", asm_name("__wine_syscall_dispatcher") );
  1338. }
  1339. output( "\tldr r0, [r0]\n");
  1340. output( "\tbx r0\n");
  1341. if (UsePIC) output( "2:\t.long %s-1b-%u\n", asm_name("__wine_syscall_dispatcher"), thumb_mode ? 4 : 8 );
  1342. output_function_size( "__wine_syscall" );
  1343. break;
  1344. case CPU_ARM64:
  1345. output( "\t.align %d\n", get_alignment(16) );
  1346. output( "\t%s\n", func_declaration("__wine_syscall") );
  1347. output( "%s:\n", asm_name("__wine_syscall") );
  1348. output( "\tadrp x16, %s\n", arm64_page( asm_name("__wine_syscall_dispatcher") ) );
  1349. output( "\tldr x16, [x16, #%s]\n", arm64_pageoff( asm_name("__wine_syscall_dispatcher") ) );
  1350. output( "\tbr x16\n");
  1351. output_function_size( "__wine_syscall" );
  1352. default:
  1353. break;
  1354. }
  1355. output( "\t.data\n" );
  1356. output( "\t.align %d\n", get_alignment( get_ptr_size() ) );
  1357. output( "%s\n", asm_globl("__wine_syscall_dispatcher") );
  1358. output( "\t%s 0\n", get_asm_ptr_keyword() );
  1359. output( "\t.short %u\n", count );
  1360. for (i = 0; i < count; i++) output( "\t.byte %u\n", get_args_size( syscalls[i] ));
  1361. }
  1362. /* output the import and delayed import tables of a Win32 module */
  1363. void output_imports( DLLSPEC *spec )
  1364. {
  1365. if (is_pe()) return;
  1366. output_immediate_imports();
  1367. output_delayed_imports( spec );
  1368. output_immediate_import_thunks();
  1369. output_delayed_import_thunks( spec );
  1370. output_external_link_imports( spec );
  1371. }
  1372. /* create a new asm temp file */
  1373. static void new_output_as_file(void)
  1374. {
  1375. char *name;
  1376. if (output_file) fclose( output_file );
  1377. name = open_temp_output_file( ".s" );
  1378. strarray_add( &as_files, name );
  1379. }
  1380. /* assemble all the asm files */
  1381. static void assemble_files( const char *prefix )
  1382. {
  1383. unsigned int i;
  1384. if (output_file) fclose( output_file );
  1385. output_file = NULL;
  1386. for (i = 0; i < as_files.count; i++)
  1387. {
  1388. char *obj = get_temp_file_name( prefix, ".o" );
  1389. assemble_file( as_files.str[i], obj );
  1390. as_files.str[i] = obj;
  1391. }
  1392. }
  1393. /* build a library from the current asm files and any additional object files in argv */
  1394. static void build_library( const char *output_name, struct strarray files, int create )
  1395. {
  1396. struct strarray args;
  1397. if (!create || target.platform != PLATFORM_WINDOWS)
  1398. {
  1399. args = find_tool( "ar", NULL );
  1400. strarray_add( &args, create ? "rc" : "r" );
  1401. strarray_add( &args, output_name );
  1402. }
  1403. else
  1404. {
  1405. args = find_link_tool();
  1406. strarray_add( &args, "/lib" );
  1407. strarray_add( &args, strmake( "-out:%s", output_name ));
  1408. }
  1409. strarray_addall( &args, as_files );
  1410. strarray_addall( &args, files );
  1411. if (create) unlink( output_name );
  1412. spawn( args );
  1413. if (target.platform != PLATFORM_WINDOWS)
  1414. {
  1415. struct strarray ranlib = find_tool( "ranlib", NULL );
  1416. strarray_add( &ranlib, output_name );
  1417. spawn( ranlib );
  1418. }
  1419. }
  1420. /* create a Windows-style import library */
  1421. static void build_windows_import_lib( const char *lib_name, DLLSPEC *spec )
  1422. {
  1423. struct strarray args;
  1424. char *def_file;
  1425. def_file = open_temp_output_file( ".def" );
  1426. output_def_file( spec, 1 );
  1427. fclose( output_file );
  1428. args = find_tool( "dlltool", NULL );
  1429. strarray_add( &args, "-k" );
  1430. strarray_add( &args, strendswith( lib_name, ".delay.a" ) ? "-y" : "-l" );
  1431. strarray_add( &args, lib_name );
  1432. strarray_add( &args, "-d" );
  1433. strarray_add( &args, def_file );
  1434. switch (target.cpu)
  1435. {
  1436. case CPU_i386:
  1437. strarray_add( &args, "-m" );
  1438. strarray_add( &args, "i386" );
  1439. strarray_add( &args, "--as-flags=--32" );
  1440. break;
  1441. case CPU_x86_64:
  1442. strarray_add( &args, "-m" );
  1443. strarray_add( &args, "i386:x86-64" );
  1444. strarray_add( &args, "--as-flags=--64" );
  1445. break;
  1446. case CPU_ARM:
  1447. strarray_add( &args, "-m" );
  1448. strarray_add( &args, "arm" );
  1449. break;
  1450. case CPU_ARM64:
  1451. strarray_add( &args, "-m" );
  1452. strarray_add( &args, "arm64" );
  1453. break;
  1454. default:
  1455. break;
  1456. }
  1457. spawn( args );
  1458. }
  1459. /* create a Unix-style import library */
  1460. static void build_unix_import_lib( DLLSPEC *spec )
  1461. {
  1462. int i, total;
  1463. const char *name, *prefix;
  1464. char *dll_name = encode_dll_name( spec->file_name );
  1465. /* entry points */
  1466. for (i = total = 0; i < spec->nb_entry_points; i++)
  1467. {
  1468. const ORDDEF *odp = &spec->entry_points[i];
  1469. if (odp->name) name = odp->name;
  1470. else if (odp->export_name) name = odp->export_name;
  1471. else continue;
  1472. if (odp->flags & FLAG_PRIVATE) continue;
  1473. total++;
  1474. /* C++ mangled names cannot be imported */
  1475. if (strpbrk( name, "?@" )) continue;
  1476. switch(odp->type)
  1477. {
  1478. case TYPE_VARARGS:
  1479. case TYPE_CDECL:
  1480. case TYPE_STDCALL:
  1481. prefix = (!odp->name || (odp->flags & FLAG_ORDINAL)) ? import_ord_prefix : import_func_prefix;
  1482. new_output_as_file();
  1483. output( "\t.text\n" );
  1484. output( "\n\t.align %d\n", get_alignment( get_ptr_size() ));
  1485. output( "\t%s\n", func_declaration( name ) );
  1486. output( "%s\n", asm_globl( name ) );
  1487. output( "\t%s %s%s$%u$%s\n", get_asm_ptr_keyword(),
  1488. asm_name( prefix ), dll_name, odp->ordinal, name );
  1489. output_function_size( name );
  1490. output_gnu_stack_note();
  1491. break;
  1492. default:
  1493. break;
  1494. }
  1495. }
  1496. if (!total) warning( "%s: Import library doesn't export anything\n", spec->file_name );
  1497. if (!as_files.count) /* create a dummy file to avoid empty import libraries */
  1498. {
  1499. new_output_as_file();
  1500. output( "\t.text\n" );
  1501. }
  1502. assemble_files( spec->file_name );
  1503. free( dll_name );
  1504. }
  1505. /* output an import library for a Win32 module and additional object files */
  1506. void output_static_lib( DLLSPEC *spec, struct strarray files )
  1507. {
  1508. if (is_pe())
  1509. {
  1510. if (spec) build_windows_import_lib( output_file_name, spec );
  1511. if (files.count || !spec) build_library( output_file_name, files, !spec );
  1512. }
  1513. else
  1514. {
  1515. if (spec) build_unix_import_lib( spec );
  1516. build_library( output_file_name, files, 1 );
  1517. }
  1518. }