utils.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413
  1. /*
  2. * Small utility functions for winebuild
  3. *
  4. * Copyright 2000 Alexandre Julliard
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this library; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
  19. */
  20. #include "config.h"
  21. #include "wine/port.h"
  22. #include <assert.h>
  23. #include <ctype.h>
  24. #include <stdarg.h>
  25. #include <stdio.h>
  26. #include <stdlib.h>
  27. #include <string.h>
  28. #ifdef HAVE_UNISTD_H
  29. # include <unistd.h>
  30. #endif
  31. #ifdef HAVE_SYS_STAT_H
  32. # include <sys/stat.h>
  33. #endif
  34. #include "build.h"
  35. #if defined(_WIN32) && !defined(__CYGWIN__)
  36. # define PATH_SEPARATOR ';'
  37. #else
  38. # define PATH_SEPARATOR ':'
  39. #endif
  40. static struct strarray tmp_files;
  41. static struct strarray empty_strarray;
  42. static const char *output_file_source_name;
  43. static const struct
  44. {
  45. const char *name;
  46. enum target_cpu cpu;
  47. } cpu_names[] =
  48. {
  49. { "i386", CPU_x86 },
  50. { "i486", CPU_x86 },
  51. { "i586", CPU_x86 },
  52. { "i686", CPU_x86 },
  53. { "i786", CPU_x86 },
  54. { "amd64", CPU_x86_64 },
  55. { "x86_64", CPU_x86_64 },
  56. { "powerpc", CPU_POWERPC },
  57. { "arm", CPU_ARM },
  58. { "armv5", CPU_ARM },
  59. { "armv6", CPU_ARM },
  60. { "armv7", CPU_ARM },
  61. { "armv7a", CPU_ARM },
  62. { "arm64", CPU_ARM64 },
  63. { "aarch64", CPU_ARM64 },
  64. };
  65. /* atexit handler to clean tmp files */
  66. void cleanup_tmp_files(void)
  67. {
  68. unsigned int i;
  69. for (i = 0; i < tmp_files.count; i++) if (tmp_files.str[i]) unlink( tmp_files.str[i] );
  70. }
  71. void *xmalloc (size_t size)
  72. {
  73. void *res;
  74. res = malloc (size ? size : 1);
  75. if (res == NULL)
  76. {
  77. fprintf (stderr, "Virtual memory exhausted.\n");
  78. exit (1);
  79. }
  80. return res;
  81. }
  82. void *xrealloc (void *ptr, size_t size)
  83. {
  84. void *res = realloc (ptr, size);
  85. if (size && res == NULL)
  86. {
  87. fprintf (stderr, "Virtual memory exhausted.\n");
  88. exit (1);
  89. }
  90. return res;
  91. }
  92. char *xstrdup( const char *str )
  93. {
  94. char *res = strdup( str );
  95. if (!res)
  96. {
  97. fprintf (stderr, "Virtual memory exhausted.\n");
  98. exit (1);
  99. }
  100. return res;
  101. }
  102. char *strupper(char *s)
  103. {
  104. char *p;
  105. for (p = s; *p; p++) *p = toupper(*p);
  106. return s;
  107. }
  108. int strendswith(const char* str, const char* end)
  109. {
  110. int l = strlen(str);
  111. int m = strlen(end);
  112. return l >= m && strcmp(str + l - m, end) == 0;
  113. }
  114. char *strmake( const char* fmt, ... )
  115. {
  116. int n;
  117. size_t size = 100;
  118. va_list ap;
  119. for (;;)
  120. {
  121. char *p = xmalloc( size );
  122. va_start( ap, fmt );
  123. n = vsnprintf( p, size, fmt, ap );
  124. va_end( ap );
  125. if (n == -1) size *= 2;
  126. else if ((size_t)n >= size) size = n + 1;
  127. else return p;
  128. free( p );
  129. }
  130. }
  131. static struct strarray strarray_copy( struct strarray src )
  132. {
  133. struct strarray array;
  134. array.count = src.count;
  135. array.max = src.max;
  136. array.str = xmalloc( array.max * sizeof(*array.str) );
  137. memcpy( array.str, src.str, array.count * sizeof(*array.str) );
  138. return array;
  139. }
  140. static void strarray_add_one( struct strarray *array, const char *str )
  141. {
  142. if (array->count == array->max)
  143. {
  144. array->max *= 2;
  145. if (array->max < 16) array->max = 16;
  146. array->str = xrealloc( array->str, array->max * sizeof(*array->str) );
  147. }
  148. array->str[array->count++] = str;
  149. }
  150. void strarray_add( struct strarray *array, ... )
  151. {
  152. va_list valist;
  153. const char *str;
  154. va_start( valist, array );
  155. while ((str = va_arg( valist, const char *))) strarray_add_one( array, str );
  156. va_end( valist );
  157. }
  158. void strarray_addv( struct strarray *array, char * const *argv )
  159. {
  160. while (*argv) strarray_add_one( array, *argv++ );
  161. }
  162. void strarray_addall( struct strarray *array, struct strarray args )
  163. {
  164. unsigned int i;
  165. for (i = 0; i < args.count; i++) strarray_add_one( array, args.str[i] );
  166. }
  167. struct strarray strarray_fromstring( const char *str, const char *delim )
  168. {
  169. const char *tok;
  170. struct strarray array = empty_strarray;
  171. char *buf = xstrdup( str );
  172. for (tok = strtok( buf, delim ); tok; tok = strtok( NULL, delim ))
  173. strarray_add_one( &array, strdup( tok ));
  174. free( buf );
  175. return array;
  176. }
  177. void fatal_error( const char *msg, ... )
  178. {
  179. va_list valist;
  180. va_start( valist, msg );
  181. if (input_file_name)
  182. {
  183. fprintf( stderr, "%s:", input_file_name );
  184. if (current_line)
  185. fprintf( stderr, "%d:", current_line );
  186. fputc( ' ', stderr );
  187. }
  188. else fprintf( stderr, "winebuild: " );
  189. vfprintf( stderr, msg, valist );
  190. va_end( valist );
  191. exit(1);
  192. }
  193. void fatal_perror( const char *msg, ... )
  194. {
  195. va_list valist;
  196. va_start( valist, msg );
  197. if (input_file_name)
  198. {
  199. fprintf( stderr, "%s:", input_file_name );
  200. if (current_line)
  201. fprintf( stderr, "%d:", current_line );
  202. fputc( ' ', stderr );
  203. }
  204. vfprintf( stderr, msg, valist );
  205. perror( " " );
  206. va_end( valist );
  207. exit(1);
  208. }
  209. void error( const char *msg, ... )
  210. {
  211. va_list valist;
  212. va_start( valist, msg );
  213. if (input_file_name)
  214. {
  215. fprintf( stderr, "%s:", input_file_name );
  216. if (current_line)
  217. fprintf( stderr, "%d:", current_line );
  218. fputc( ' ', stderr );
  219. }
  220. vfprintf( stderr, msg, valist );
  221. va_end( valist );
  222. nb_errors++;
  223. }
  224. void warning( const char *msg, ... )
  225. {
  226. va_list valist;
  227. if (!display_warnings) return;
  228. va_start( valist, msg );
  229. if (input_file_name)
  230. {
  231. fprintf( stderr, "%s:", input_file_name );
  232. if (current_line)
  233. fprintf( stderr, "%d:", current_line );
  234. fputc( ' ', stderr );
  235. }
  236. fprintf( stderr, "warning: " );
  237. vfprintf( stderr, msg, valist );
  238. va_end( valist );
  239. }
  240. int output( const char *format, ... )
  241. {
  242. int ret;
  243. va_list valist;
  244. va_start( valist, format );
  245. ret = vfprintf( output_file, format, valist );
  246. va_end( valist );
  247. if (ret < 0) fatal_perror( "Output error" );
  248. return ret;
  249. }
  250. static struct strarray get_tools_path(void)
  251. {
  252. static int done;
  253. static struct strarray dirs;
  254. if (!done)
  255. {
  256. dirs = strarray_copy( tools_path );
  257. /* then append the PATH directories */
  258. if (getenv( "PATH" ))
  259. {
  260. char *p = xstrdup( getenv( "PATH" ));
  261. while (*p)
  262. {
  263. strarray_add_one( &dirs, p );
  264. while (*p && *p != PATH_SEPARATOR) p++;
  265. if (!*p) break;
  266. *p++ = 0;
  267. }
  268. }
  269. done = 1;
  270. }
  271. return dirs;
  272. }
  273. /* find a binary in the path */
  274. static const char *find_binary( const char *prefix, const char *name )
  275. {
  276. struct strarray dirs = get_tools_path();
  277. unsigned int i, maxlen = 0;
  278. struct stat st;
  279. char *p, *file;
  280. if (strchr( name, '/' )) return name;
  281. if (!prefix) prefix = "";
  282. for (i = 0; i < dirs.count; i++) maxlen = max( maxlen, strlen(dirs.str[i]) + 2 );
  283. file = xmalloc( maxlen + strlen(prefix) + strlen(name) + sizeof(EXEEXT) + 1 );
  284. for (i = 0; i < dirs.count; i++)
  285. {
  286. strcpy( file, dirs.str[i] );
  287. p = file + strlen(file);
  288. if (p == file) *p++ = '.';
  289. if (p[-1] != '/') *p++ = '/';
  290. if (*prefix)
  291. {
  292. strcpy( p, prefix );
  293. p += strlen(p);
  294. *p++ = '-';
  295. }
  296. strcpy( p, name );
  297. strcat( p, EXEEXT );
  298. if (!stat( file, &st ) && S_ISREG(st.st_mode) && (st.st_mode & 0111)) return file;
  299. }
  300. free( file );
  301. return NULL;
  302. }
  303. void spawn( struct strarray args )
  304. {
  305. unsigned int i;
  306. int status;
  307. const char *argv0 = find_binary( NULL, args.str[0] );
  308. if (argv0) args.str[0] = argv0;
  309. strarray_add_one( &args, NULL );
  310. if (verbose)
  311. for (i = 0; args.str[i]; i++)
  312. fprintf( stderr, "%s%c", args.str[i], args.str[i+1] ? ' ' : '\n' );
  313. if ((status = _spawnvp( _P_WAIT, args.str[0], args.str )))
  314. {
  315. if (status > 0) fatal_error( "%s failed with status %u\n", args.str[0], status );
  316. else fatal_perror( "winebuild" );
  317. exit( 1 );
  318. }
  319. }
  320. static const char *find_clang_tool( const struct strarray clang, const char *tool )
  321. {
  322. const char *out = get_temp_file_name( "print_tool", ".out" );
  323. struct strarray args;
  324. int sout = -1;
  325. char *path, *p;
  326. struct stat st;
  327. size_t cnt;
  328. args = strarray_copy( clang );
  329. strarray_add_one( &args, strmake( "-print-prog-name=%s", tool ) );
  330. if (verbose) strarray_add_one( &args, "-v" );
  331. sout = dup( fileno(stdout) );
  332. freopen( out, "w", stdout );
  333. spawn( args );
  334. if (sout >= 0)
  335. {
  336. dup2( sout, fileno(stdout) );
  337. close( sout );
  338. }
  339. if (stat(out, &st) || !st.st_size) return NULL;
  340. path = xmalloc(st.st_size + 1);
  341. sout = open(out, O_RDONLY);
  342. if (sout == -1) return NULL;
  343. cnt = read(sout, path, st.st_size);
  344. close(sout);
  345. path[cnt] = 0;
  346. if ((p = strchr(path, '\n'))) *p = 0;
  347. /* clang returns passed command instead of full path if the tool could not be found */
  348. if (!strcmp(path, tool))
  349. {
  350. free( path );
  351. return NULL;
  352. }
  353. return path;
  354. }
  355. /* find a build tool in the path, trying the various names */
  356. struct strarray find_tool( const char *name, const char * const *names )
  357. {
  358. struct strarray ret = empty_strarray;
  359. const char *file;
  360. const char *alt_names[2];
  361. if (!names)
  362. {
  363. alt_names[0] = name;
  364. alt_names[1] = NULL;
  365. names = alt_names;
  366. }
  367. while (*names)
  368. {
  369. if ((file = find_binary( target_alias, *names ))) break;
  370. names++;
  371. }
  372. if (!file && names == alt_names + 1)
  373. {
  374. if (cc_command.count) file = find_clang_tool( cc_command, "lld-link" );
  375. if (!file && !(file = find_binary( "llvm", name )))
  376. {
  377. struct strarray clang = empty_strarray;
  378. strarray_add_one( &clang, "clang" );
  379. file = find_clang_tool( clang, strmake( "llvm-%s", name ));
  380. }
  381. }
  382. if (!file) fatal_error( "cannot find the '%s' tool\n", name );
  383. strarray_add_one( &ret, file );
  384. return ret;
  385. }
  386. /* find a link tool in the path */
  387. struct strarray find_link_tool(void)
  388. {
  389. struct strarray ret = empty_strarray;
  390. const char *file = NULL;
  391. if (cc_command.count) file = find_clang_tool( cc_command, "lld-link" );
  392. if (!file) file = find_binary( NULL, "lld-link" );
  393. if (!file)
  394. {
  395. struct strarray clang = empty_strarray;
  396. strarray_add_one( &clang, "clang" );
  397. file = find_clang_tool( clang, "lld-link" );
  398. }
  399. if (!file) fatal_error( "cannot find the 'lld-link' tool\n" );
  400. strarray_add_one( &ret, file );
  401. return ret;
  402. }
  403. struct strarray get_as_command(void)
  404. {
  405. struct strarray args;
  406. unsigned int i;
  407. if (cc_command.count)
  408. {
  409. args = strarray_copy( cc_command );
  410. strarray_add( &args, "-xassembler", "-c", NULL );
  411. if (force_pointer_size)
  412. strarray_add_one( &args, (force_pointer_size == 8) ? "-m64" : "-m32" );
  413. if (cpu_option) strarray_add_one( &args, strmake("-mcpu=%s", cpu_option) );
  414. if (fpu_option) strarray_add_one( &args, strmake("-mfpu=%s", fpu_option) );
  415. if (arch_option) strarray_add_one( &args, strmake("-march=%s", arch_option) );
  416. for (i = 0; i < tools_path.count; i++)
  417. strarray_add_one( &args, strmake("-B%s", tools_path.str[i] ));
  418. return args;
  419. }
  420. if (!as_command.count)
  421. {
  422. static const char * const commands[] = { "gas", "as", NULL };
  423. as_command = find_tool( "as", commands );
  424. }
  425. args = strarray_copy( as_command );
  426. if (force_pointer_size)
  427. {
  428. switch (target_platform)
  429. {
  430. case PLATFORM_APPLE:
  431. strarray_add( &args, "-arch", (force_pointer_size == 8) ? "x86_64" : "i386", NULL );
  432. break;
  433. default:
  434. switch(target_cpu)
  435. {
  436. case CPU_POWERPC:
  437. strarray_add_one( &args, (force_pointer_size == 8) ? "-a64" : "-a32" );
  438. break;
  439. default:
  440. strarray_add_one( &args, (force_pointer_size == 8) ? "--64" : "--32" );
  441. break;
  442. }
  443. break;
  444. }
  445. }
  446. if (cpu_option) strarray_add_one( &args, strmake("-mcpu=%s", cpu_option) );
  447. if (fpu_option) strarray_add_one( &args, strmake("-mfpu=%s", fpu_option) );
  448. return args;
  449. }
  450. struct strarray get_ld_command(void)
  451. {
  452. struct strarray args;
  453. if (!ld_command.count)
  454. {
  455. static const char * const commands[] = { "ld", "gld", NULL };
  456. ld_command = find_tool( "ld", commands );
  457. }
  458. args = strarray_copy( ld_command );
  459. if (force_pointer_size)
  460. {
  461. switch (target_platform)
  462. {
  463. case PLATFORM_APPLE:
  464. strarray_add( &args, "-arch", (force_pointer_size == 8) ? "x86_64" : "i386", NULL );
  465. break;
  466. case PLATFORM_FREEBSD:
  467. strarray_add( &args, "-m", (force_pointer_size == 8) ? "elf_x86_64_fbsd" : "elf_i386_fbsd", NULL );
  468. break;
  469. case PLATFORM_MINGW:
  470. case PLATFORM_WINDOWS:
  471. strarray_add( &args, "-m", (force_pointer_size == 8) ? "i386pep" : "i386pe", NULL );
  472. break;
  473. default:
  474. switch(target_cpu)
  475. {
  476. case CPU_POWERPC:
  477. strarray_add( &args, "-m", (force_pointer_size == 8) ? "elf64ppc" : "elf32ppc", NULL );
  478. break;
  479. default:
  480. strarray_add( &args, "-m", (force_pointer_size == 8) ? "elf_x86_64" : "elf_i386", NULL );
  481. break;
  482. }
  483. break;
  484. }
  485. }
  486. if (target_cpu == CPU_ARM && !is_pe())
  487. strarray_add( &args, "--no-wchar-size-warning", NULL );
  488. return args;
  489. }
  490. const char *get_nm_command(void)
  491. {
  492. if (!nm_command.count)
  493. {
  494. static const char * const commands[] = { "nm", "gnm", NULL };
  495. nm_command = find_tool( "nm", commands );
  496. }
  497. if (nm_command.count > 1)
  498. fatal_error( "multiple arguments in nm command not supported yet\n" );
  499. return nm_command.str[0];
  500. }
  501. /* get a name for a temp file, automatically cleaned up on exit */
  502. char *get_temp_file_name( const char *prefix, const char *suffix )
  503. {
  504. char *name;
  505. const char *ext, *basename;
  506. int fd;
  507. if (!prefix || !prefix[0]) prefix = "winebuild";
  508. if (!suffix) suffix = "";
  509. if ((basename = strrchr( prefix, '/' ))) basename++;
  510. else basename = prefix;
  511. if (!(ext = strchr( basename, '.' ))) ext = prefix + strlen(prefix);
  512. name = xmalloc( sizeof("/tmp/") + (ext - prefix) + sizeof(".XXXXXX") + strlen(suffix) );
  513. memcpy( name, prefix, ext - prefix );
  514. strcpy( name + (ext - prefix), ".XXXXXX" );
  515. strcat( name, suffix );
  516. if ((fd = mkstemps( name, strlen(suffix) )) == -1)
  517. {
  518. strcpy( name, "/tmp/" );
  519. memcpy( name + 5, basename, ext - basename );
  520. strcpy( name + 5 + (ext - basename), ".XXXXXX" );
  521. strcat( name, suffix );
  522. if ((fd = mkstemps( name, strlen(suffix) )) == -1)
  523. fatal_error( "could not generate a temp file\n" );
  524. }
  525. close( fd );
  526. strarray_add_one( &tmp_files, name );
  527. return name;
  528. }
  529. /*******************************************************************
  530. * buffer management
  531. *
  532. * Function for reading from/writing to a memory buffer.
  533. */
  534. int byte_swapped = 0;
  535. const char *input_buffer_filename;
  536. const unsigned char *input_buffer;
  537. size_t input_buffer_pos;
  538. size_t input_buffer_size;
  539. unsigned char *output_buffer;
  540. size_t output_buffer_pos;
  541. size_t output_buffer_size;
  542. static void check_output_buffer_space( size_t size )
  543. {
  544. if (output_buffer_pos + size >= output_buffer_size)
  545. {
  546. output_buffer_size = max( output_buffer_size * 2, output_buffer_pos + size );
  547. output_buffer = xrealloc( output_buffer, output_buffer_size );
  548. }
  549. }
  550. void init_input_buffer( const char *file )
  551. {
  552. int fd;
  553. struct stat st;
  554. unsigned char *buffer;
  555. if ((fd = open( file, O_RDONLY | O_BINARY )) == -1) fatal_perror( "Cannot open %s", file );
  556. if ((fstat( fd, &st ) == -1)) fatal_perror( "Cannot stat %s", file );
  557. if (!st.st_size) fatal_error( "%s is an empty file\n", file );
  558. input_buffer = buffer = xmalloc( st.st_size );
  559. if (read( fd, buffer, st.st_size ) != st.st_size) fatal_error( "Cannot read %s\n", file );
  560. close( fd );
  561. input_buffer_filename = xstrdup( file );
  562. input_buffer_size = st.st_size;
  563. input_buffer_pos = 0;
  564. byte_swapped = 0;
  565. }
  566. void init_output_buffer(void)
  567. {
  568. output_buffer_size = 1024;
  569. output_buffer_pos = 0;
  570. output_buffer = xmalloc( output_buffer_size );
  571. }
  572. void flush_output_buffer(void)
  573. {
  574. open_output_file();
  575. if (fwrite( output_buffer, 1, output_buffer_pos, output_file ) != output_buffer_pos)
  576. fatal_error( "Error writing to %s\n", output_file_name );
  577. close_output_file();
  578. free( output_buffer );
  579. }
  580. unsigned char get_byte(void)
  581. {
  582. if (input_buffer_pos >= input_buffer_size)
  583. fatal_error( "%s is a truncated file\n", input_buffer_filename );
  584. return input_buffer[input_buffer_pos++];
  585. }
  586. unsigned short get_word(void)
  587. {
  588. unsigned short ret;
  589. if (input_buffer_pos + sizeof(ret) > input_buffer_size)
  590. fatal_error( "%s is a truncated file\n", input_buffer_filename );
  591. memcpy( &ret, input_buffer + input_buffer_pos, sizeof(ret) );
  592. if (byte_swapped) ret = (ret << 8) | (ret >> 8);
  593. input_buffer_pos += sizeof(ret);
  594. return ret;
  595. }
  596. unsigned int get_dword(void)
  597. {
  598. unsigned int ret;
  599. if (input_buffer_pos + sizeof(ret) > input_buffer_size)
  600. fatal_error( "%s is a truncated file\n", input_buffer_filename );
  601. memcpy( &ret, input_buffer + input_buffer_pos, sizeof(ret) );
  602. if (byte_swapped)
  603. ret = ((ret << 24) | ((ret << 8) & 0x00ff0000) | ((ret >> 8) & 0x0000ff00) | (ret >> 24));
  604. input_buffer_pos += sizeof(ret);
  605. return ret;
  606. }
  607. void put_data( const void *data, size_t size )
  608. {
  609. check_output_buffer_space( size );
  610. memcpy( output_buffer + output_buffer_pos, data, size );
  611. output_buffer_pos += size;
  612. }
  613. void put_byte( unsigned char val )
  614. {
  615. check_output_buffer_space( 1 );
  616. output_buffer[output_buffer_pos++] = val;
  617. }
  618. void put_word( unsigned short val )
  619. {
  620. if (byte_swapped) val = (val << 8) | (val >> 8);
  621. put_data( &val, sizeof(val) );
  622. }
  623. void put_dword( unsigned int val )
  624. {
  625. if (byte_swapped)
  626. val = ((val << 24) | ((val << 8) & 0x00ff0000) | ((val >> 8) & 0x0000ff00) | (val >> 24));
  627. put_data( &val, sizeof(val) );
  628. }
  629. void put_qword( unsigned int val )
  630. {
  631. if (byte_swapped)
  632. {
  633. put_dword( 0 );
  634. put_dword( val );
  635. }
  636. else
  637. {
  638. put_dword( val );
  639. put_dword( 0 );
  640. }
  641. }
  642. /* pointer-sized word */
  643. void put_pword( unsigned int val )
  644. {
  645. if (get_ptr_size() == 8) put_qword( val );
  646. else put_dword( val );
  647. }
  648. void align_output( unsigned int align )
  649. {
  650. size_t size = align - (output_buffer_pos % align);
  651. if (size == align) return;
  652. check_output_buffer_space( size );
  653. memset( output_buffer + output_buffer_pos, 0, size );
  654. output_buffer_pos += size;
  655. }
  656. /* output a standard header for generated files */
  657. void output_standard_file_header(void)
  658. {
  659. if (spec_file_name)
  660. output( "/* File generated automatically from %s; do not edit! */\n", spec_file_name );
  661. else
  662. output( "/* File generated automatically; do not edit! */\n" );
  663. output( "/* This file can be copied, modified and distributed without restriction. */\n\n" );
  664. if (safe_seh)
  665. {
  666. output( "\t.def @feat.00\n\t.scl 3\n\t.type 0\n\t.endef\n" );
  667. output( "\t.globl @feat.00\n" );
  668. output( ".set @feat.00, 1\n" );
  669. }
  670. if (thumb_mode)
  671. {
  672. output( "\t.syntax unified\n" );
  673. output( "\t.thumb\n" );
  674. }
  675. }
  676. /* dump a byte stream into the assembly code */
  677. void dump_bytes( const void *buffer, unsigned int size )
  678. {
  679. unsigned int i;
  680. const unsigned char *ptr = buffer;
  681. if (!size) return;
  682. output( "\t.byte " );
  683. for (i = 0; i < size - 1; i++, ptr++)
  684. {
  685. if ((i % 16) == 15) output( "0x%02x\n\t.byte ", *ptr );
  686. else output( "0x%02x,", *ptr );
  687. }
  688. output( "0x%02x\n", *ptr );
  689. }
  690. /*******************************************************************
  691. * open_input_file
  692. *
  693. * Open a file in the given srcdir and set the input_file_name global variable.
  694. */
  695. FILE *open_input_file( const char *srcdir, const char *name )
  696. {
  697. char *fullname;
  698. FILE *file = fopen( name, "r" );
  699. if (!file && srcdir)
  700. {
  701. fullname = strmake( "%s/%s", srcdir, name );
  702. file = fopen( fullname, "r" );
  703. }
  704. else fullname = xstrdup( name );
  705. if (!file) fatal_error( "Cannot open file '%s'\n", fullname );
  706. input_file_name = fullname;
  707. current_line = 1;
  708. return file;
  709. }
  710. /*******************************************************************
  711. * close_input_file
  712. *
  713. * Close the current input file (must have been opened with open_input_file).
  714. */
  715. void close_input_file( FILE *file )
  716. {
  717. fclose( file );
  718. free( input_file_name );
  719. input_file_name = NULL;
  720. current_line = 0;
  721. }
  722. /*******************************************************************
  723. * open_output_file
  724. */
  725. void open_output_file(void)
  726. {
  727. if (output_file_name)
  728. {
  729. if (strendswith( output_file_name, ".o" ))
  730. output_file_source_name = open_temp_output_file( ".s" );
  731. else
  732. if (!(output_file = fopen( output_file_name, "w" )))
  733. fatal_error( "Unable to create output file '%s'\n", output_file_name );
  734. }
  735. else output_file = stdout;
  736. }
  737. /*******************************************************************
  738. * close_output_file
  739. */
  740. void close_output_file(void)
  741. {
  742. if (!output_file || !output_file_name) return;
  743. if (fclose( output_file ) < 0) fatal_perror( "fclose" );
  744. if (output_file_source_name) assemble_file( output_file_source_name, output_file_name );
  745. output_file = NULL;
  746. }
  747. /*******************************************************************
  748. * open_temp_output_file
  749. */
  750. char *open_temp_output_file( const char *suffix )
  751. {
  752. char *tmp_file = get_temp_file_name( output_file_name, suffix );
  753. if (!(output_file = fopen( tmp_file, "w" )))
  754. fatal_error( "Unable to create output file '%s'\n", tmp_file );
  755. return tmp_file;
  756. }
  757. /*******************************************************************
  758. * remove_stdcall_decoration
  759. *
  760. * Remove a possible @xx suffix from a function name.
  761. * Return the numerical value of the suffix, or -1 if none.
  762. */
  763. int remove_stdcall_decoration( char *name )
  764. {
  765. char *p, *end = strrchr( name, '@' );
  766. if (!end || !end[1] || end == name) return -1;
  767. if (target_cpu != CPU_x86) return -1;
  768. /* make sure all the rest is digits */
  769. for (p = end + 1; *p; p++) if (!isdigit(*p)) return -1;
  770. *end = 0;
  771. return atoi( end + 1 );
  772. }
  773. /*******************************************************************
  774. * assemble_file
  775. *
  776. * Run a file through the assembler.
  777. */
  778. void assemble_file( const char *src_file, const char *obj_file )
  779. {
  780. struct strarray args = get_as_command();
  781. strarray_add( &args, "-o", obj_file, src_file, NULL );
  782. spawn( args );
  783. }
  784. /*******************************************************************
  785. * alloc_dll_spec
  786. *
  787. * Create a new dll spec file descriptor
  788. */
  789. DLLSPEC *alloc_dll_spec(void)
  790. {
  791. DLLSPEC *spec;
  792. spec = xmalloc( sizeof(*spec) );
  793. memset( spec, 0, sizeof(*spec) );
  794. spec->type = SPEC_WIN32;
  795. spec->base = MAX_ORDINALS;
  796. spec->characteristics = IMAGE_FILE_EXECUTABLE_IMAGE;
  797. spec->subsystem = 0;
  798. spec->subsystem_major = 4;
  799. spec->subsystem_minor = 0;
  800. if (get_ptr_size() > 4)
  801. spec->characteristics |= IMAGE_FILE_LARGE_ADDRESS_AWARE;
  802. else
  803. spec->characteristics |= IMAGE_FILE_32BIT_MACHINE;
  804. spec->dll_characteristics = IMAGE_DLLCHARACTERISTICS_NX_COMPAT;
  805. return spec;
  806. }
  807. /*******************************************************************
  808. * free_dll_spec
  809. *
  810. * Free dll spec file descriptor
  811. */
  812. void free_dll_spec( DLLSPEC *spec )
  813. {
  814. int i;
  815. for (i = 0; i < spec->nb_entry_points; i++)
  816. {
  817. ORDDEF *odp = &spec->entry_points[i];
  818. free( odp->name );
  819. free( odp->export_name );
  820. free( odp->link_name );
  821. }
  822. free( spec->file_name );
  823. free( spec->dll_name );
  824. free( spec->c_name );
  825. free( spec->init_func );
  826. free( spec->entry_points );
  827. free( spec->names );
  828. free( spec->ordinals );
  829. free( spec->resources );
  830. free( spec );
  831. }
  832. /*******************************************************************
  833. * make_c_identifier
  834. *
  835. * Map a string to a valid C identifier.
  836. */
  837. char *make_c_identifier( const char *str )
  838. {
  839. char *p, buffer[256];
  840. for (p = buffer; *str && p < buffer+sizeof(buffer)-1; p++, str++)
  841. {
  842. if (isalnum(*str)) *p = *str;
  843. else *p = '_';
  844. }
  845. *p = 0;
  846. return xstrdup( buffer );
  847. }
  848. /*******************************************************************
  849. * get_stub_name
  850. *
  851. * Generate an internal name for a stub entry point.
  852. */
  853. const char *get_stub_name( const ORDDEF *odp, const DLLSPEC *spec )
  854. {
  855. static char *buffer;
  856. free( buffer );
  857. if (odp->name || odp->export_name)
  858. {
  859. char *p;
  860. buffer = strmake( "__wine_stub_%s", odp->name ? odp->name : odp->export_name );
  861. /* make sure name is a legal C identifier */
  862. for (p = buffer; *p; p++) if (!isalnum(*p) && *p != '_') break;
  863. if (!*p) return buffer;
  864. free( buffer );
  865. }
  866. buffer = strmake( "__wine_stub_%s_%d", make_c_identifier(spec->file_name), odp->ordinal );
  867. return buffer;
  868. }
  869. /* return the stdcall-decorated name for an entry point */
  870. const char *get_link_name( const ORDDEF *odp )
  871. {
  872. static char *buffer;
  873. char *ret;
  874. if (target_cpu != CPU_x86) return odp->link_name;
  875. switch (odp->type)
  876. {
  877. case TYPE_STDCALL:
  878. if (is_pe())
  879. {
  880. if (odp->flags & FLAG_THISCALL) return odp->link_name;
  881. if (odp->flags & FLAG_FASTCALL) ret = strmake( "@%s@%u", odp->link_name, get_args_size( odp ));
  882. else if (!kill_at) ret = strmake( "%s@%u", odp->link_name, get_args_size( odp ));
  883. else return odp->link_name;
  884. }
  885. else
  886. {
  887. if (odp->flags & FLAG_THISCALL) ret = strmake( "__thiscall_%s", odp->link_name );
  888. else if (odp->flags & FLAG_FASTCALL) ret = strmake( "__fastcall_%s", odp->link_name );
  889. else return odp->link_name;
  890. }
  891. break;
  892. case TYPE_PASCAL:
  893. if (is_pe() && !kill_at)
  894. {
  895. int args = get_args_size( odp );
  896. if (odp->flags & FLAG_REGISTER) args += get_ptr_size(); /* context argument */
  897. ret = strmake( "%s@%u", odp->link_name, args );
  898. }
  899. else return odp->link_name;
  900. break;
  901. default:
  902. return odp->link_name;
  903. }
  904. free( buffer );
  905. buffer = ret;
  906. return ret;
  907. }
  908. /*******************************************************************
  909. * sort_func_list
  910. *
  911. * Sort a list of functions, removing duplicates.
  912. */
  913. int sort_func_list( ORDDEF **list, int count, int (*compare)(const void *, const void *) )
  914. {
  915. int i, j;
  916. if (!count) return 0;
  917. qsort( list, count, sizeof(*list), compare );
  918. for (i = j = 0; i < count; i++) if (compare( &list[j], &list[i] )) list[++j] = list[i];
  919. return j + 1;
  920. }
  921. /* parse a cpu name and return the corresponding value */
  922. int get_cpu_from_name( const char *name )
  923. {
  924. unsigned int i;
  925. for (i = 0; i < ARRAY_SIZE(cpu_names); i++)
  926. if (!strcmp( cpu_names[i].name, name )) return cpu_names[i].cpu;
  927. return -1;
  928. }
  929. /*****************************************************************
  930. * Function: get_alignment
  931. *
  932. * Description:
  933. * According to the info page for gas, the .align directive behaves
  934. * differently on different systems. On some architectures, the
  935. * argument of a .align directive is the number of bytes to pad to, so
  936. * to align on an 8-byte boundary you'd say
  937. * .align 8
  938. * On other systems, the argument is "the number of low-order zero bits
  939. * that the location counter must have after advancement." So to
  940. * align on an 8-byte boundary you'd say
  941. * .align 3
  942. *
  943. * The reason gas is written this way is that it's trying to mimic
  944. * native assemblers for the various architectures it runs on. gas
  945. * provides other directives that work consistently across
  946. * architectures, but of course we want to work on all arches with or
  947. * without gas. Hence this function.
  948. *
  949. *
  950. * Parameters:
  951. * align -- the number of bytes to align to. Must be a power of 2.
  952. */
  953. unsigned int get_alignment(unsigned int align)
  954. {
  955. unsigned int n;
  956. assert( !(align & (align - 1)) );
  957. switch(target_cpu)
  958. {
  959. case CPU_x86:
  960. case CPU_x86_64:
  961. if (target_platform != PLATFORM_APPLE) return align;
  962. /* fall through */
  963. case CPU_POWERPC:
  964. case CPU_ARM:
  965. case CPU_ARM64:
  966. n = 0;
  967. while ((1u << n) != align) n++;
  968. return n;
  969. }
  970. /* unreached */
  971. assert(0);
  972. return 0;
  973. }
  974. /* return the page size for the target CPU */
  975. unsigned int get_page_size(void)
  976. {
  977. return 0x1000; /* same on all platforms */
  978. }
  979. /* return the size of a pointer on the target CPU */
  980. unsigned int get_ptr_size(void)
  981. {
  982. switch(target_cpu)
  983. {
  984. case CPU_x86:
  985. case CPU_POWERPC:
  986. case CPU_ARM:
  987. return 4;
  988. case CPU_x86_64:
  989. case CPU_ARM64:
  990. return 8;
  991. }
  992. /* unreached */
  993. assert(0);
  994. return 0;
  995. }
  996. /* return the total size in bytes of the arguments on the stack */
  997. unsigned int get_args_size( const ORDDEF *odp )
  998. {
  999. int i, size;
  1000. for (i = size = 0; i < odp->u.func.nb_args; i++)
  1001. {
  1002. switch (odp->u.func.args[i])
  1003. {
  1004. case ARG_INT64:
  1005. case ARG_DOUBLE:
  1006. size += 8;
  1007. break;
  1008. case ARG_INT128:
  1009. /* int128 is passed as pointer on x86_64 */
  1010. if (target_cpu != CPU_x86_64)
  1011. {
  1012. size += 16;
  1013. break;
  1014. }
  1015. /* fall through */
  1016. default:
  1017. size += get_ptr_size();
  1018. break;
  1019. }
  1020. }
  1021. return size;
  1022. }
  1023. /* return the assembly name for a C symbol */
  1024. const char *asm_name( const char *sym )
  1025. {
  1026. static char *buffer;
  1027. switch (target_platform)
  1028. {
  1029. case PLATFORM_MINGW:
  1030. case PLATFORM_WINDOWS:
  1031. if (target_cpu != CPU_x86) return sym;
  1032. if (sym[0] == '@') return sym; /* fastcall */
  1033. /* fall through */
  1034. case PLATFORM_APPLE:
  1035. if (sym[0] == '.' && sym[1] == 'L') return sym;
  1036. free( buffer );
  1037. buffer = strmake( "_%s", sym );
  1038. return buffer;
  1039. default:
  1040. return sym;
  1041. }
  1042. }
  1043. /* return an assembly function declaration for a C function name */
  1044. const char *func_declaration( const char *func )
  1045. {
  1046. static char *buffer;
  1047. switch (target_platform)
  1048. {
  1049. case PLATFORM_APPLE:
  1050. return "";
  1051. case PLATFORM_MINGW:
  1052. case PLATFORM_WINDOWS:
  1053. free( buffer );
  1054. buffer = strmake( ".def %s\n\t.scl 2\n\t.type 32\n\t.endef%s", asm_name(func),
  1055. thumb_mode ? "\n\t.thumb_func" : "" );
  1056. break;
  1057. default:
  1058. free( buffer );
  1059. switch(target_cpu)
  1060. {
  1061. case CPU_ARM:
  1062. buffer = strmake( ".type %s,%%function%s", func,
  1063. thumb_mode ? "\n\t.thumb_func" : "" );
  1064. break;
  1065. case CPU_ARM64:
  1066. buffer = strmake( ".type %s,%%function", func );
  1067. break;
  1068. default:
  1069. buffer = strmake( ".type %s,@function", func );
  1070. break;
  1071. }
  1072. break;
  1073. }
  1074. return buffer;
  1075. }
  1076. /* output a size declaration for an assembly function */
  1077. void output_function_size( const char *name )
  1078. {
  1079. switch (target_platform)
  1080. {
  1081. case PLATFORM_APPLE:
  1082. case PLATFORM_MINGW:
  1083. case PLATFORM_WINDOWS:
  1084. break;
  1085. default:
  1086. output( "\t.size %s, .-%s\n", name, name );
  1087. break;
  1088. }
  1089. }
  1090. /* output a .cfi directive */
  1091. void output_cfi( const char *format, ... )
  1092. {
  1093. va_list valist;
  1094. if (!unwind_tables) return;
  1095. va_start( valist, format );
  1096. fputc( '\t', output_file );
  1097. vfprintf( output_file, format, valist );
  1098. fputc( '\n', output_file );
  1099. va_end( valist );
  1100. }
  1101. /* output an RVA pointer */
  1102. void output_rva( const char *format, ... )
  1103. {
  1104. va_list valist;
  1105. va_start( valist, format );
  1106. switch (target_platform)
  1107. {
  1108. case PLATFORM_MINGW:
  1109. case PLATFORM_WINDOWS:
  1110. output( "\t.rva " );
  1111. vfprintf( output_file, format, valist );
  1112. fputc( '\n', output_file );
  1113. break;
  1114. default:
  1115. output( "\t.long " );
  1116. vfprintf( output_file, format, valist );
  1117. output( " - .L__wine_spec_rva_base\n" );
  1118. break;
  1119. }
  1120. va_end( valist );
  1121. }
  1122. /* output the GNU note for non-exec stack */
  1123. void output_gnu_stack_note(void)
  1124. {
  1125. switch (target_platform)
  1126. {
  1127. case PLATFORM_MINGW:
  1128. case PLATFORM_WINDOWS:
  1129. case PLATFORM_APPLE:
  1130. break;
  1131. default:
  1132. switch(target_cpu)
  1133. {
  1134. case CPU_ARM:
  1135. case CPU_ARM64:
  1136. output( "\t.section .note.GNU-stack,\"\",%%progbits\n" );
  1137. break;
  1138. default:
  1139. output( "\t.section .note.GNU-stack,\"\",@progbits\n" );
  1140. break;
  1141. }
  1142. break;
  1143. }
  1144. }
  1145. /* return a global symbol declaration for an assembly symbol */
  1146. const char *asm_globl( const char *func )
  1147. {
  1148. static char *buffer;
  1149. free( buffer );
  1150. switch (target_platform)
  1151. {
  1152. case PLATFORM_APPLE:
  1153. buffer = strmake( "\t.globl _%s\n\t.private_extern _%s\n_%s:", func, func, func );
  1154. break;
  1155. case PLATFORM_MINGW:
  1156. case PLATFORM_WINDOWS:
  1157. buffer = strmake( "\t.globl %s%s\n%s%s:", target_cpu == CPU_x86 ? "_" : "", func,
  1158. target_cpu == CPU_x86 ? "_" : "", func );
  1159. break;
  1160. default:
  1161. buffer = strmake( "\t.globl %s\n\t.hidden %s\n%s:", func, func, func );
  1162. break;
  1163. }
  1164. return buffer;
  1165. }
  1166. const char *get_asm_ptr_keyword(void)
  1167. {
  1168. switch(get_ptr_size())
  1169. {
  1170. case 4: return ".long";
  1171. case 8: return ".quad";
  1172. }
  1173. assert(0);
  1174. return NULL;
  1175. }
  1176. const char *get_asm_string_keyword(void)
  1177. {
  1178. switch (target_platform)
  1179. {
  1180. case PLATFORM_APPLE:
  1181. return ".asciz";
  1182. default:
  1183. return ".string";
  1184. }
  1185. }
  1186. const char *get_asm_export_section(void)
  1187. {
  1188. switch (target_platform)
  1189. {
  1190. case PLATFORM_APPLE: return ".data";
  1191. case PLATFORM_MINGW:
  1192. case PLATFORM_WINDOWS: return ".section .edata";
  1193. default: return ".section .data";
  1194. }
  1195. }
  1196. const char *get_asm_rodata_section(void)
  1197. {
  1198. switch (target_platform)
  1199. {
  1200. case PLATFORM_APPLE: return ".const";
  1201. default: return ".section .rodata";
  1202. }
  1203. }
  1204. const char *get_asm_rsrc_section(void)
  1205. {
  1206. switch (target_platform)
  1207. {
  1208. case PLATFORM_APPLE: return ".data";
  1209. case PLATFORM_MINGW:
  1210. case PLATFORM_WINDOWS: return ".section .rsrc";
  1211. default: return ".section .data";
  1212. }
  1213. }
  1214. const char *get_asm_string_section(void)
  1215. {
  1216. switch (target_platform)
  1217. {
  1218. case PLATFORM_APPLE: return ".cstring";
  1219. default: return ".section .rodata";
  1220. }
  1221. }
  1222. const char *arm64_page( const char *sym )
  1223. {
  1224. static char *buffer;
  1225. switch (target_platform)
  1226. {
  1227. case PLATFORM_APPLE:
  1228. free( buffer );
  1229. buffer = strmake( "%s@PAGE", sym );
  1230. return buffer;
  1231. default:
  1232. return sym;
  1233. }
  1234. }
  1235. const char *arm64_pageoff( const char *sym )
  1236. {
  1237. static char *buffer;
  1238. free( buffer );
  1239. switch (target_platform)
  1240. {
  1241. case PLATFORM_APPLE:
  1242. buffer = strmake( "%s@PAGEOFF", sym );
  1243. break;
  1244. default:
  1245. buffer = strmake( ":lo12:%s", sym );
  1246. break;
  1247. }
  1248. return buffer;
  1249. }