utils.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122
  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 <assert.h>
  22. #include <ctype.h>
  23. #include <stdarg.h>
  24. #include <stdio.h>
  25. #include <stdlib.h>
  26. #include <string.h>
  27. #include "build.h"
  28. static struct strarray tmp_files;
  29. static const char *output_file_source_name;
  30. /* atexit handler to clean tmp files */
  31. void cleanup_tmp_files(void)
  32. {
  33. unsigned int i;
  34. for (i = 0; i < tmp_files.count; i++) if (tmp_files.str[i]) unlink( tmp_files.str[i] );
  35. }
  36. char *strupper(char *s)
  37. {
  38. char *p;
  39. for (p = s; *p; p++) *p = toupper(*p);
  40. return s;
  41. }
  42. void fatal_error( const char *msg, ... )
  43. {
  44. va_list valist;
  45. va_start( valist, msg );
  46. if (input_file_name)
  47. {
  48. fprintf( stderr, "%s:", input_file_name );
  49. if (current_line)
  50. fprintf( stderr, "%d:", current_line );
  51. fputc( ' ', stderr );
  52. }
  53. else fprintf( stderr, "winebuild: " );
  54. vfprintf( stderr, msg, valist );
  55. va_end( valist );
  56. exit(1);
  57. }
  58. void fatal_perror( const char *msg, ... )
  59. {
  60. va_list valist;
  61. va_start( valist, msg );
  62. if (input_file_name)
  63. {
  64. fprintf( stderr, "%s:", input_file_name );
  65. if (current_line)
  66. fprintf( stderr, "%d:", current_line );
  67. fputc( ' ', stderr );
  68. }
  69. vfprintf( stderr, msg, valist );
  70. perror( " " );
  71. va_end( valist );
  72. exit(1);
  73. }
  74. void error( const char *msg, ... )
  75. {
  76. va_list valist;
  77. va_start( valist, msg );
  78. if (input_file_name)
  79. {
  80. fprintf( stderr, "%s:", input_file_name );
  81. if (current_line)
  82. fprintf( stderr, "%d:", current_line );
  83. fputc( ' ', stderr );
  84. }
  85. vfprintf( stderr, msg, valist );
  86. va_end( valist );
  87. nb_errors++;
  88. }
  89. void warning( const char *msg, ... )
  90. {
  91. va_list valist;
  92. if (!display_warnings) return;
  93. va_start( valist, msg );
  94. if (input_file_name)
  95. {
  96. fprintf( stderr, "%s:", input_file_name );
  97. if (current_line)
  98. fprintf( stderr, "%d:", current_line );
  99. fputc( ' ', stderr );
  100. }
  101. fprintf( stderr, "warning: " );
  102. vfprintf( stderr, msg, valist );
  103. va_end( valist );
  104. }
  105. int output( const char *format, ... )
  106. {
  107. int ret;
  108. va_list valist;
  109. va_start( valist, format );
  110. ret = vfprintf( output_file, format, valist );
  111. va_end( valist );
  112. if (ret < 0) fatal_perror( "Output error" );
  113. return ret;
  114. }
  115. static struct strarray get_tools_path(void)
  116. {
  117. static int done;
  118. static struct strarray dirs;
  119. if (!done)
  120. {
  121. strarray_addall( &dirs, tools_path );
  122. strarray_addall( &dirs, strarray_frompath( getenv( "PATH" )));
  123. done = 1;
  124. }
  125. return dirs;
  126. }
  127. /* find a binary in the path */
  128. static const char *find_binary( const char *prefix, const char *name )
  129. {
  130. struct strarray dirs = get_tools_path();
  131. unsigned int i, maxlen = 0;
  132. struct stat st;
  133. char *p, *file;
  134. if (strchr( name, '/' )) return name;
  135. if (!prefix) prefix = "";
  136. for (i = 0; i < dirs.count; i++) maxlen = max( maxlen, strlen(dirs.str[i]) + 2 );
  137. file = xmalloc( maxlen + strlen(prefix) + strlen(name) + sizeof(EXEEXT) + 1 );
  138. for (i = 0; i < dirs.count; i++)
  139. {
  140. strcpy( file, dirs.str[i] );
  141. p = file + strlen(file);
  142. if (p == file) *p++ = '.';
  143. if (p[-1] != '/') *p++ = '/';
  144. if (*prefix)
  145. {
  146. strcpy( p, prefix );
  147. p += strlen(p);
  148. *p++ = '-';
  149. }
  150. strcpy( p, name );
  151. strcat( p, EXEEXT );
  152. if (!stat( file, &st ) && S_ISREG(st.st_mode) && (st.st_mode & 0111)) return file;
  153. }
  154. free( file );
  155. return NULL;
  156. }
  157. void spawn( struct strarray args )
  158. {
  159. int status;
  160. const char *argv0 = find_binary( NULL, args.str[0] );
  161. if (argv0) args.str[0] = argv0;
  162. if (verbose) strarray_trace( args );
  163. if ((status = strarray_spawn( args )))
  164. {
  165. if (status > 0) fatal_error( "%s failed with status %u\n", args.str[0], status );
  166. else fatal_perror( "winebuild" );
  167. exit( 1 );
  168. }
  169. }
  170. static const char *find_clang_tool( struct strarray clang, const char *tool )
  171. {
  172. const char *out = get_temp_file_name( "print_tool", ".out" );
  173. struct strarray args = empty_strarray;
  174. int sout = -1;
  175. char *path, *p;
  176. struct stat st;
  177. size_t cnt;
  178. strarray_addall( &args, clang );
  179. strarray_add( &args, strmake( "-print-prog-name=%s", tool ));
  180. if (verbose) strarray_add( &args, "-v" );
  181. sout = dup( fileno(stdout) );
  182. freopen( out, "w", stdout );
  183. spawn( args );
  184. if (sout >= 0)
  185. {
  186. dup2( sout, fileno(stdout) );
  187. close( sout );
  188. }
  189. if (stat(out, &st) || !st.st_size) return NULL;
  190. path = xmalloc(st.st_size + 1);
  191. sout = open(out, O_RDONLY);
  192. if (sout == -1) return NULL;
  193. cnt = read(sout, path, st.st_size);
  194. close(sout);
  195. path[cnt] = 0;
  196. if ((p = strchr(path, '\n'))) *p = 0;
  197. /* clang returns passed command instead of full path if the tool could not be found */
  198. if (!strcmp(path, tool))
  199. {
  200. free( path );
  201. return NULL;
  202. }
  203. return path;
  204. }
  205. /* find a build tool in the path, trying the various names */
  206. struct strarray find_tool( const char *name, const char * const *names )
  207. {
  208. struct strarray ret = empty_strarray;
  209. const char *file;
  210. const char *alt_names[2];
  211. if (!names)
  212. {
  213. alt_names[0] = name;
  214. alt_names[1] = NULL;
  215. names = alt_names;
  216. }
  217. while (*names)
  218. {
  219. if ((file = find_binary( target_alias, *names ))) break;
  220. names++;
  221. }
  222. if (!file)
  223. {
  224. if (cc_command.count) file = find_clang_tool( cc_command, name );
  225. if (!file && !(file = find_binary( "llvm", name )))
  226. {
  227. struct strarray clang = empty_strarray;
  228. strarray_add( &clang, "clang" );
  229. file = find_clang_tool( clang, strmake( "llvm-%s", name ));
  230. }
  231. }
  232. if (!file) fatal_error( "cannot find the '%s' tool\n", name );
  233. strarray_add( &ret, file );
  234. return ret;
  235. }
  236. /* find a link tool in the path */
  237. struct strarray find_link_tool(void)
  238. {
  239. struct strarray ret = empty_strarray;
  240. const char *file = NULL;
  241. if (cc_command.count) file = find_clang_tool( cc_command, "lld-link" );
  242. if (!file) file = find_binary( NULL, "lld-link" );
  243. if (!file)
  244. {
  245. struct strarray clang = empty_strarray;
  246. strarray_add( &clang, "clang" );
  247. file = find_clang_tool( clang, "lld-link" );
  248. }
  249. if (!file) fatal_error( "cannot find the 'lld-link' tool\n" );
  250. strarray_add( &ret, file );
  251. return ret;
  252. }
  253. struct strarray get_as_command(void)
  254. {
  255. struct strarray args = empty_strarray;
  256. const char *file;
  257. unsigned int i;
  258. int using_cc = 0;
  259. if (cc_command.count)
  260. {
  261. strarray_addall( &args, cc_command );
  262. using_cc = 1;
  263. }
  264. else if (as_command.count)
  265. {
  266. strarray_addall( &args, as_command );
  267. }
  268. else if ((file = find_binary( target_alias, "as" )) || (file = find_binary( target_alias, "gas ")))
  269. {
  270. strarray_add( &args, file );
  271. }
  272. else if ((file = find_binary( NULL, "clang" )))
  273. {
  274. strarray_add( &args, file );
  275. if (target_alias)
  276. {
  277. strarray_add( &args, "-target" );
  278. strarray_add( &args, target_alias );
  279. }
  280. using_cc = 1;
  281. }
  282. if (using_cc)
  283. {
  284. strarray_add( &args, "-xassembler" );
  285. strarray_add( &args, "-c" );
  286. if (force_pointer_size)
  287. strarray_add( &args, (force_pointer_size == 8) ? "-m64" : "-m32" );
  288. if (cpu_option) strarray_add( &args, strmake("-mcpu=%s", cpu_option) );
  289. if (fpu_option) strarray_add( &args, strmake("-mfpu=%s", fpu_option) );
  290. if (arch_option) strarray_add( &args, strmake("-march=%s", arch_option) );
  291. for (i = 0; i < tools_path.count; i++)
  292. strarray_add( &args, strmake("-B%s", tools_path.str[i] ));
  293. return args;
  294. }
  295. if (force_pointer_size)
  296. {
  297. switch (target.platform)
  298. {
  299. case PLATFORM_APPLE:
  300. strarray_add( &args, "-arch" );
  301. strarray_add( &args, (force_pointer_size == 8) ? "x86_64" : "i386" );
  302. break;
  303. default:
  304. strarray_add( &args, (force_pointer_size == 8) ? "--64" : "--32" );
  305. break;
  306. }
  307. }
  308. if (cpu_option) strarray_add( &args, strmake("-mcpu=%s", cpu_option) );
  309. if (fpu_option) strarray_add( &args, strmake("-mfpu=%s", fpu_option) );
  310. return args;
  311. }
  312. struct strarray get_ld_command(void)
  313. {
  314. struct strarray args = empty_strarray;
  315. if (!ld_command.count)
  316. {
  317. static const char * const commands[] = { "ld", "gld", NULL };
  318. ld_command = find_tool( "ld", commands );
  319. }
  320. strarray_addall( &args, ld_command );
  321. if (force_pointer_size)
  322. {
  323. switch (target.platform)
  324. {
  325. case PLATFORM_APPLE:
  326. strarray_add( &args, "-arch" );
  327. strarray_add( &args, (force_pointer_size == 8) ? "x86_64" : "i386" );
  328. break;
  329. case PLATFORM_FREEBSD:
  330. strarray_add( &args, "-m" );
  331. strarray_add( &args, (force_pointer_size == 8) ? "elf_x86_64_fbsd" : "elf_i386_fbsd" );
  332. break;
  333. case PLATFORM_MINGW:
  334. case PLATFORM_WINDOWS:
  335. strarray_add( &args, "-m" );
  336. strarray_add( &args, (force_pointer_size == 8) ? "i386pep" : "i386pe" );
  337. break;
  338. default:
  339. strarray_add( &args, "-m" );
  340. strarray_add( &args, (force_pointer_size == 8) ? "elf_x86_64" : "elf_i386" );
  341. break;
  342. }
  343. }
  344. if (target.cpu == CPU_ARM && !is_pe())
  345. strarray_add( &args, "--no-wchar-size-warning" );
  346. return args;
  347. }
  348. const char *get_nm_command(void)
  349. {
  350. if (!nm_command.count)
  351. {
  352. static const char * const commands[] = { "nm", "gnm", NULL };
  353. nm_command = find_tool( "nm", commands );
  354. }
  355. if (nm_command.count > 1)
  356. fatal_error( "multiple arguments in nm command not supported yet\n" );
  357. return nm_command.str[0];
  358. }
  359. /* get a name for a temp file, automatically cleaned up on exit */
  360. char *get_temp_file_name( const char *prefix, const char *suffix )
  361. {
  362. char *name;
  363. int fd;
  364. if (prefix) prefix = get_basename_noext( prefix );
  365. fd = make_temp_file( prefix, suffix, &name );
  366. close( fd );
  367. strarray_add( &tmp_files, name );
  368. return name;
  369. }
  370. /*******************************************************************
  371. * buffer management
  372. *
  373. * Function for reading from/writing to a memory buffer.
  374. */
  375. int byte_swapped = 0;
  376. const char *input_buffer_filename;
  377. const unsigned char *input_buffer;
  378. size_t input_buffer_pos;
  379. size_t input_buffer_size;
  380. unsigned char *output_buffer;
  381. size_t output_buffer_pos;
  382. size_t output_buffer_size;
  383. void init_input_buffer( const char *file )
  384. {
  385. if (!(input_buffer = read_file( file, &input_buffer_size ))) fatal_perror( "Cannot read %s", file );
  386. if (!input_buffer_size) fatal_error( "%s is an empty file\n", file );
  387. input_buffer_filename = xstrdup( file );
  388. input_buffer_pos = 0;
  389. byte_swapped = 0;
  390. }
  391. unsigned char get_byte(void)
  392. {
  393. if (input_buffer_pos >= input_buffer_size)
  394. fatal_error( "%s is a truncated file\n", input_buffer_filename );
  395. return input_buffer[input_buffer_pos++];
  396. }
  397. unsigned short get_word(void)
  398. {
  399. unsigned short ret;
  400. if (input_buffer_pos + sizeof(ret) > input_buffer_size)
  401. fatal_error( "%s is a truncated file\n", input_buffer_filename );
  402. memcpy( &ret, input_buffer + input_buffer_pos, sizeof(ret) );
  403. if (byte_swapped) ret = (ret << 8) | (ret >> 8);
  404. input_buffer_pos += sizeof(ret);
  405. return ret;
  406. }
  407. unsigned int get_dword(void)
  408. {
  409. unsigned int ret;
  410. if (input_buffer_pos + sizeof(ret) > input_buffer_size)
  411. fatal_error( "%s is a truncated file\n", input_buffer_filename );
  412. memcpy( &ret, input_buffer + input_buffer_pos, sizeof(ret) );
  413. if (byte_swapped)
  414. ret = ((ret << 24) | ((ret << 8) & 0x00ff0000) | ((ret >> 8) & 0x0000ff00) | (ret >> 24));
  415. input_buffer_pos += sizeof(ret);
  416. return ret;
  417. }
  418. /* pointer-sized word */
  419. void put_pword( unsigned int val )
  420. {
  421. if (get_ptr_size() == 8) put_qword( val );
  422. else put_dword( val );
  423. }
  424. /* output a standard header for generated files */
  425. void output_standard_file_header(void)
  426. {
  427. if (spec_file_name)
  428. output( "/* File generated automatically from %s; do not edit! */\n", spec_file_name );
  429. else
  430. output( "/* File generated automatically; do not edit! */\n" );
  431. output( "/* This file can be copied, modified and distributed without restriction. */\n\n" );
  432. if (safe_seh)
  433. {
  434. output( "\t.def @feat.00\n\t.scl 3\n\t.type 0\n\t.endef\n" );
  435. output( "\t.globl @feat.00\n" );
  436. output( ".set @feat.00, 1\n" );
  437. }
  438. if (thumb_mode)
  439. {
  440. output( "\t.syntax unified\n" );
  441. output( "\t.thumb\n" );
  442. }
  443. }
  444. /* dump a byte stream into the assembly code */
  445. void dump_bytes( const void *buffer, unsigned int size )
  446. {
  447. unsigned int i;
  448. const unsigned char *ptr = buffer;
  449. if (!size) return;
  450. output( "\t.byte " );
  451. for (i = 0; i < size - 1; i++, ptr++)
  452. {
  453. if ((i % 16) == 15) output( "0x%02x\n\t.byte ", *ptr );
  454. else output( "0x%02x,", *ptr );
  455. }
  456. output( "0x%02x\n", *ptr );
  457. }
  458. /*******************************************************************
  459. * open_input_file
  460. *
  461. * Open a file in the given srcdir and set the input_file_name global variable.
  462. */
  463. FILE *open_input_file( const char *srcdir, const char *name )
  464. {
  465. char *fullname;
  466. FILE *file = fopen( name, "r" );
  467. if (!file && srcdir)
  468. {
  469. fullname = strmake( "%s/%s", srcdir, name );
  470. file = fopen( fullname, "r" );
  471. }
  472. else fullname = xstrdup( name );
  473. if (!file) fatal_error( "Cannot open file '%s'\n", fullname );
  474. input_file_name = fullname;
  475. current_line = 1;
  476. return file;
  477. }
  478. /*******************************************************************
  479. * close_input_file
  480. *
  481. * Close the current input file (must have been opened with open_input_file).
  482. */
  483. void close_input_file( FILE *file )
  484. {
  485. fclose( file );
  486. free( input_file_name );
  487. input_file_name = NULL;
  488. current_line = 0;
  489. }
  490. /*******************************************************************
  491. * open_output_file
  492. */
  493. void open_output_file(void)
  494. {
  495. if (output_file_name)
  496. {
  497. if (strendswith( output_file_name, ".o" ))
  498. output_file_source_name = open_temp_output_file( ".s" );
  499. else
  500. if (!(output_file = fopen( output_file_name, "w" )))
  501. fatal_error( "Unable to create output file '%s'\n", output_file_name );
  502. }
  503. else output_file = stdout;
  504. }
  505. /*******************************************************************
  506. * close_output_file
  507. */
  508. void close_output_file(void)
  509. {
  510. if (!output_file || !output_file_name) return;
  511. if (fclose( output_file ) < 0) fatal_perror( "fclose" );
  512. if (output_file_source_name) assemble_file( output_file_source_name, output_file_name );
  513. output_file = NULL;
  514. }
  515. /*******************************************************************
  516. * open_temp_output_file
  517. */
  518. char *open_temp_output_file( const char *suffix )
  519. {
  520. char *tmp_file = get_temp_file_name( output_file_name, suffix );
  521. if (!(output_file = fopen( tmp_file, "w" )))
  522. fatal_error( "Unable to create output file '%s'\n", tmp_file );
  523. return tmp_file;
  524. }
  525. /*******************************************************************
  526. * remove_stdcall_decoration
  527. *
  528. * Remove a possible @xx suffix from a function name.
  529. * Return the numerical value of the suffix, or -1 if none.
  530. */
  531. int remove_stdcall_decoration( char *name )
  532. {
  533. char *p, *end = strrchr( name, '@' );
  534. if (!end || !end[1] || end == name) return -1;
  535. if (target.cpu != CPU_i386) return -1;
  536. /* make sure all the rest is digits */
  537. for (p = end + 1; *p; p++) if (!isdigit(*p)) return -1;
  538. *end = 0;
  539. return atoi( end + 1 );
  540. }
  541. /*******************************************************************
  542. * assemble_file
  543. *
  544. * Run a file through the assembler.
  545. */
  546. void assemble_file( const char *src_file, const char *obj_file )
  547. {
  548. struct strarray args = get_as_command();
  549. strarray_add( &args, "-o" );
  550. strarray_add( &args, obj_file );
  551. strarray_add( &args, src_file );
  552. spawn( args );
  553. }
  554. /*******************************************************************
  555. * alloc_dll_spec
  556. *
  557. * Create a new dll spec file descriptor
  558. */
  559. DLLSPEC *alloc_dll_spec(void)
  560. {
  561. DLLSPEC *spec;
  562. spec = xmalloc( sizeof(*spec) );
  563. memset( spec, 0, sizeof(*spec) );
  564. spec->type = SPEC_WIN32;
  565. spec->base = MAX_ORDINALS;
  566. spec->characteristics = IMAGE_FILE_EXECUTABLE_IMAGE;
  567. spec->subsystem = IMAGE_SUBSYSTEM_WINDOWS_CUI;
  568. spec->subsystem_major = 4;
  569. spec->subsystem_minor = 0;
  570. spec->syscall_table = 0;
  571. if (get_ptr_size() > 4)
  572. spec->characteristics |= IMAGE_FILE_LARGE_ADDRESS_AWARE;
  573. else
  574. spec->characteristics |= IMAGE_FILE_32BIT_MACHINE;
  575. spec->dll_characteristics = IMAGE_DLLCHARACTERISTICS_NX_COMPAT;
  576. return spec;
  577. }
  578. /*******************************************************************
  579. * free_dll_spec
  580. *
  581. * Free dll spec file descriptor
  582. */
  583. void free_dll_spec( DLLSPEC *spec )
  584. {
  585. int i;
  586. for (i = 0; i < spec->nb_entry_points; i++)
  587. {
  588. ORDDEF *odp = &spec->entry_points[i];
  589. free( odp->name );
  590. free( odp->export_name );
  591. free( odp->link_name );
  592. }
  593. free( spec->file_name );
  594. free( spec->dll_name );
  595. free( spec->c_name );
  596. free( spec->init_func );
  597. free( spec->entry_points );
  598. free( spec->names );
  599. free( spec->ordinals );
  600. free( spec->resources );
  601. free( spec );
  602. }
  603. /*******************************************************************
  604. * make_c_identifier
  605. *
  606. * Map a string to a valid C identifier.
  607. */
  608. char *make_c_identifier( const char *str )
  609. {
  610. char *p, buffer[256];
  611. for (p = buffer; *str && p < buffer+sizeof(buffer)-1; p++, str++)
  612. {
  613. if (isalnum(*str)) *p = *str;
  614. else *p = '_';
  615. }
  616. *p = 0;
  617. return xstrdup( buffer );
  618. }
  619. /*******************************************************************
  620. * get_stub_name
  621. *
  622. * Generate an internal name for a stub entry point.
  623. */
  624. const char *get_stub_name( const ORDDEF *odp, const DLLSPEC *spec )
  625. {
  626. static char *buffer;
  627. free( buffer );
  628. if (odp->name || odp->export_name)
  629. {
  630. char *p;
  631. buffer = strmake( "__wine_stub_%s", odp->name ? odp->name : odp->export_name );
  632. /* make sure name is a legal C identifier */
  633. for (p = buffer; *p; p++) if (!isalnum(*p) && *p != '_') break;
  634. if (!*p) return buffer;
  635. free( buffer );
  636. }
  637. buffer = strmake( "__wine_stub_%s_%d", make_c_identifier(spec->file_name), odp->ordinal );
  638. return buffer;
  639. }
  640. /* return the stdcall-decorated name for an entry point */
  641. const char *get_link_name( const ORDDEF *odp )
  642. {
  643. static char *buffer;
  644. char *ret;
  645. if (target.cpu != CPU_i386) return odp->link_name;
  646. switch (odp->type)
  647. {
  648. case TYPE_STDCALL:
  649. if (is_pe())
  650. {
  651. if (odp->flags & FLAG_THISCALL) return odp->link_name;
  652. if (odp->flags & FLAG_FASTCALL) ret = strmake( "@%s@%u", odp->link_name, get_args_size( odp ));
  653. else if (!kill_at) ret = strmake( "%s@%u", odp->link_name, get_args_size( odp ));
  654. else return odp->link_name;
  655. }
  656. else
  657. {
  658. if (odp->flags & FLAG_THISCALL) ret = strmake( "__thiscall_%s", odp->link_name );
  659. else if (odp->flags & FLAG_FASTCALL) ret = strmake( "__fastcall_%s", odp->link_name );
  660. else return odp->link_name;
  661. }
  662. break;
  663. case TYPE_PASCAL:
  664. if (is_pe() && !kill_at)
  665. {
  666. int args = get_args_size( odp );
  667. if (odp->flags & FLAG_REGISTER) args += get_ptr_size(); /* context argument */
  668. ret = strmake( "%s@%u", odp->link_name, args );
  669. }
  670. else return odp->link_name;
  671. break;
  672. default:
  673. return odp->link_name;
  674. }
  675. free( buffer );
  676. buffer = ret;
  677. return ret;
  678. }
  679. /*******************************************************************
  680. * sort_func_list
  681. *
  682. * Sort a list of functions, removing duplicates.
  683. */
  684. int sort_func_list( ORDDEF **list, int count, int (*compare)(const void *, const void *) )
  685. {
  686. int i, j;
  687. if (!count) return 0;
  688. qsort( list, count, sizeof(*list), compare );
  689. for (i = j = 0; i < count; i++) if (compare( &list[j], &list[i] )) list[++j] = list[i];
  690. return j + 1;
  691. }
  692. /*****************************************************************
  693. * Function: get_alignment
  694. *
  695. * Description:
  696. * According to the info page for gas, the .align directive behaves
  697. * differently on different systems. On some architectures, the
  698. * argument of a .align directive is the number of bytes to pad to, so
  699. * to align on an 8-byte boundary you'd say
  700. * .align 8
  701. * On other systems, the argument is "the number of low-order zero bits
  702. * that the location counter must have after advancement." So to
  703. * align on an 8-byte boundary you'd say
  704. * .align 3
  705. *
  706. * The reason gas is written this way is that it's trying to mimic
  707. * native assemblers for the various architectures it runs on. gas
  708. * provides other directives that work consistently across
  709. * architectures, but of course we want to work on all arches with or
  710. * without gas. Hence this function.
  711. *
  712. *
  713. * Parameters:
  714. * align -- the number of bytes to align to. Must be a power of 2.
  715. */
  716. unsigned int get_alignment(unsigned int align)
  717. {
  718. unsigned int n;
  719. assert( !(align & (align - 1)) );
  720. switch (target.cpu)
  721. {
  722. case CPU_i386:
  723. case CPU_x86_64:
  724. if (target.platform != PLATFORM_APPLE) return align;
  725. /* fall through */
  726. case CPU_ARM:
  727. case CPU_ARM64:
  728. n = 0;
  729. while ((1u << n) != align) n++;
  730. return n;
  731. }
  732. /* unreached */
  733. assert(0);
  734. return 0;
  735. }
  736. /* return the page size for the target CPU */
  737. unsigned int get_page_size(void)
  738. {
  739. return 0x1000; /* same on all platforms */
  740. }
  741. /* return the total size in bytes of the arguments on the stack */
  742. unsigned int get_args_size( const ORDDEF *odp )
  743. {
  744. int i, size;
  745. for (i = size = 0; i < odp->u.func.nb_args; i++)
  746. {
  747. switch (odp->u.func.args[i])
  748. {
  749. case ARG_INT64:
  750. case ARG_DOUBLE:
  751. if (target.cpu == CPU_ARM) size = (size + 7) & ~7;
  752. size += 8;
  753. break;
  754. case ARG_INT128:
  755. /* int128 is passed as pointer on x86_64 */
  756. if (target.cpu != CPU_x86_64)
  757. {
  758. size += 16;
  759. break;
  760. }
  761. /* fall through */
  762. default:
  763. size += get_ptr_size();
  764. break;
  765. }
  766. }
  767. return size;
  768. }
  769. /* return the assembly name for a C symbol */
  770. const char *asm_name( const char *sym )
  771. {
  772. static char *buffer;
  773. switch (target.platform)
  774. {
  775. case PLATFORM_MINGW:
  776. case PLATFORM_WINDOWS:
  777. if (target.cpu != CPU_i386) return sym;
  778. if (sym[0] == '@') return sym; /* fastcall */
  779. /* fall through */
  780. case PLATFORM_APPLE:
  781. if (sym[0] == '.' && sym[1] == 'L') return sym;
  782. free( buffer );
  783. buffer = strmake( "_%s", sym );
  784. return buffer;
  785. default:
  786. return sym;
  787. }
  788. }
  789. /* return an assembly function declaration for a C function name */
  790. const char *func_declaration( const char *func )
  791. {
  792. static char *buffer;
  793. switch (target.platform)
  794. {
  795. case PLATFORM_APPLE:
  796. return "";
  797. case PLATFORM_MINGW:
  798. case PLATFORM_WINDOWS:
  799. free( buffer );
  800. buffer = strmake( ".def %s\n\t.scl 2\n\t.type 32\n\t.endef%s", asm_name(func),
  801. thumb_mode ? "\n\t.thumb_func" : "" );
  802. break;
  803. default:
  804. free( buffer );
  805. switch (target.cpu)
  806. {
  807. case CPU_ARM:
  808. buffer = strmake( ".type %s,%%function%s", func,
  809. thumb_mode ? "\n\t.thumb_func" : "" );
  810. break;
  811. case CPU_ARM64:
  812. buffer = strmake( ".type %s,%%function", func );
  813. break;
  814. default:
  815. buffer = strmake( ".type %s,@function", func );
  816. break;
  817. }
  818. break;
  819. }
  820. return buffer;
  821. }
  822. /* output a size declaration for an assembly function */
  823. void output_function_size( const char *name )
  824. {
  825. switch (target.platform)
  826. {
  827. case PLATFORM_APPLE:
  828. case PLATFORM_MINGW:
  829. case PLATFORM_WINDOWS:
  830. break;
  831. default:
  832. output( "\t.size %s, .-%s\n", name, name );
  833. break;
  834. }
  835. }
  836. /* output a .cfi directive */
  837. void output_cfi( const char *format, ... )
  838. {
  839. va_list valist;
  840. if (!unwind_tables) return;
  841. va_start( valist, format );
  842. fputc( '\t', output_file );
  843. vfprintf( output_file, format, valist );
  844. fputc( '\n', output_file );
  845. va_end( valist );
  846. }
  847. /* output an RVA pointer */
  848. void output_rva( const char *format, ... )
  849. {
  850. va_list valist;
  851. va_start( valist, format );
  852. switch (target.platform)
  853. {
  854. case PLATFORM_MINGW:
  855. case PLATFORM_WINDOWS:
  856. output( "\t.rva " );
  857. vfprintf( output_file, format, valist );
  858. fputc( '\n', output_file );
  859. break;
  860. default:
  861. output( "\t.long " );
  862. vfprintf( output_file, format, valist );
  863. output( " - .L__wine_spec_rva_base\n" );
  864. break;
  865. }
  866. va_end( valist );
  867. }
  868. /* output the GNU note for non-exec stack */
  869. void output_gnu_stack_note(void)
  870. {
  871. switch (target.platform)
  872. {
  873. case PLATFORM_MINGW:
  874. case PLATFORM_WINDOWS:
  875. case PLATFORM_APPLE:
  876. break;
  877. default:
  878. switch (target.cpu)
  879. {
  880. case CPU_ARM:
  881. case CPU_ARM64:
  882. output( "\t.section .note.GNU-stack,\"\",%%progbits\n" );
  883. break;
  884. default:
  885. output( "\t.section .note.GNU-stack,\"\",@progbits\n" );
  886. break;
  887. }
  888. break;
  889. }
  890. }
  891. /* return a global symbol declaration for an assembly symbol */
  892. const char *asm_globl( const char *func )
  893. {
  894. static char *buffer;
  895. free( buffer );
  896. switch (target.platform)
  897. {
  898. case PLATFORM_APPLE:
  899. buffer = strmake( "\t.globl _%s\n\t.private_extern _%s\n_%s:", func, func, func );
  900. break;
  901. case PLATFORM_MINGW:
  902. case PLATFORM_WINDOWS:
  903. buffer = strmake( "\t.globl %s%s\n%s%s:", target.cpu == CPU_i386 ? "_" : "", func,
  904. target.cpu == CPU_i386 ? "_" : "", func );
  905. break;
  906. default:
  907. buffer = strmake( "\t.globl %s\n\t.hidden %s\n%s:", func, func, func );
  908. break;
  909. }
  910. return buffer;
  911. }
  912. const char *get_asm_ptr_keyword(void)
  913. {
  914. switch(get_ptr_size())
  915. {
  916. case 4: return ".long";
  917. case 8: return ".quad";
  918. }
  919. assert(0);
  920. return NULL;
  921. }
  922. const char *get_asm_string_keyword(void)
  923. {
  924. switch (target.platform)
  925. {
  926. case PLATFORM_APPLE:
  927. return ".asciz";
  928. default:
  929. return ".string";
  930. }
  931. }
  932. const char *get_asm_export_section(void)
  933. {
  934. switch (target.platform)
  935. {
  936. case PLATFORM_APPLE: return ".data";
  937. case PLATFORM_MINGW:
  938. case PLATFORM_WINDOWS: return ".section .edata";
  939. default: return ".section .data";
  940. }
  941. }
  942. const char *get_asm_rodata_section(void)
  943. {
  944. switch (target.platform)
  945. {
  946. case PLATFORM_APPLE: return ".const";
  947. default: return ".section .rodata";
  948. }
  949. }
  950. const char *get_asm_rsrc_section(void)
  951. {
  952. switch (target.platform)
  953. {
  954. case PLATFORM_APPLE: return ".data";
  955. case PLATFORM_MINGW:
  956. case PLATFORM_WINDOWS: return ".section .rsrc";
  957. default: return ".section .data";
  958. }
  959. }
  960. const char *get_asm_string_section(void)
  961. {
  962. switch (target.platform)
  963. {
  964. case PLATFORM_APPLE: return ".cstring";
  965. default: return ".section .rodata";
  966. }
  967. }
  968. const char *arm64_page( const char *sym )
  969. {
  970. static char *buffer;
  971. switch (target.platform)
  972. {
  973. case PLATFORM_APPLE:
  974. free( buffer );
  975. buffer = strmake( "%s@PAGE", sym );
  976. return buffer;
  977. default:
  978. return sym;
  979. }
  980. }
  981. const char *arm64_pageoff( const char *sym )
  982. {
  983. static char *buffer;
  984. free( buffer );
  985. switch (target.platform)
  986. {
  987. case PLATFORM_APPLE:
  988. buffer = strmake( "%s@PAGEOFF", sym );
  989. break;
  990. default:
  991. buffer = strmake( ":lo12:%s", sym );
  992. break;
  993. }
  994. return buffer;
  995. }