utils.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355
  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. /* find a build tool in the path, trying the various names */
  321. struct strarray find_tool( const char *name, const char * const *names )
  322. {
  323. const char *file;
  324. const char *alt_names[2];
  325. if (!names)
  326. {
  327. alt_names[0] = name;
  328. alt_names[1] = NULL;
  329. names = alt_names;
  330. }
  331. while (*names)
  332. {
  333. if ((file = find_binary( target_alias, *names ))
  334. || (names == alt_names && (file = find_binary( "llvm", *names ))))
  335. {
  336. struct strarray ret = empty_strarray;
  337. strarray_add_one( &ret, file );
  338. return ret;
  339. }
  340. names++;
  341. }
  342. fatal_error( "cannot find the '%s' tool\n", name );
  343. }
  344. /* find a link tool in the path */
  345. struct strarray find_link_tool(void)
  346. {
  347. struct strarray ret = empty_strarray;
  348. const char *file;
  349. if (!(file = find_binary( NULL, "lld-link" )))
  350. fatal_error( "cannot find the 'lld-link tool\n" );
  351. strarray_add_one( &ret, file );
  352. return ret;
  353. }
  354. struct strarray get_as_command(void)
  355. {
  356. struct strarray args;
  357. unsigned int i;
  358. if (cc_command.count)
  359. {
  360. args = strarray_copy( cc_command );
  361. strarray_add( &args, "-xassembler", "-c", NULL );
  362. if (force_pointer_size)
  363. strarray_add_one( &args, (force_pointer_size == 8) ? "-m64" : "-m32" );
  364. if (cpu_option) strarray_add_one( &args, strmake("-mcpu=%s", cpu_option) );
  365. if (fpu_option) strarray_add_one( &args, strmake("-mfpu=%s", fpu_option) );
  366. if (arch_option) strarray_add_one( &args, strmake("-march=%s", arch_option) );
  367. for (i = 0; i < tools_path.count; i++)
  368. strarray_add_one( &args, strmake("-B%s", tools_path.str[i] ));
  369. return args;
  370. }
  371. if (!as_command.count)
  372. {
  373. static const char * const commands[] = { "gas", "as", NULL };
  374. as_command = find_tool( "as", commands );
  375. }
  376. args = strarray_copy( as_command );
  377. if (force_pointer_size)
  378. {
  379. switch (target_platform)
  380. {
  381. case PLATFORM_APPLE:
  382. strarray_add( &args, "-arch", (force_pointer_size == 8) ? "x86_64" : "i386", NULL );
  383. break;
  384. default:
  385. switch(target_cpu)
  386. {
  387. case CPU_POWERPC:
  388. strarray_add_one( &args, (force_pointer_size == 8) ? "-a64" : "-a32" );
  389. break;
  390. default:
  391. strarray_add_one( &args, (force_pointer_size == 8) ? "--64" : "--32" );
  392. break;
  393. }
  394. break;
  395. }
  396. }
  397. if (cpu_option) strarray_add_one( &args, strmake("-mcpu=%s", cpu_option) );
  398. if (fpu_option) strarray_add_one( &args, strmake("-mfpu=%s", fpu_option) );
  399. return args;
  400. }
  401. struct strarray get_ld_command(void)
  402. {
  403. struct strarray args;
  404. if (!ld_command.count)
  405. {
  406. static const char * const commands[] = { "ld", "gld", NULL };
  407. ld_command = find_tool( "ld", commands );
  408. }
  409. args = strarray_copy( ld_command );
  410. if (force_pointer_size)
  411. {
  412. switch (target_platform)
  413. {
  414. case PLATFORM_APPLE:
  415. strarray_add( &args, "-arch", (force_pointer_size == 8) ? "x86_64" : "i386", NULL );
  416. break;
  417. case PLATFORM_FREEBSD:
  418. strarray_add( &args, "-m", (force_pointer_size == 8) ? "elf_x86_64_fbsd" : "elf_i386_fbsd", NULL );
  419. break;
  420. case PLATFORM_MINGW:
  421. case PLATFORM_WINDOWS:
  422. strarray_add( &args, "-m", (force_pointer_size == 8) ? "i386pep" : "i386pe", NULL );
  423. break;
  424. default:
  425. switch(target_cpu)
  426. {
  427. case CPU_POWERPC:
  428. strarray_add( &args, "-m", (force_pointer_size == 8) ? "elf64ppc" : "elf32ppc", NULL );
  429. break;
  430. default:
  431. strarray_add( &args, "-m", (force_pointer_size == 8) ? "elf_x86_64" : "elf_i386", NULL );
  432. break;
  433. }
  434. break;
  435. }
  436. }
  437. if (target_cpu == CPU_ARM && !is_pe())
  438. strarray_add( &args, "--no-wchar-size-warning", NULL );
  439. return args;
  440. }
  441. const char *get_nm_command(void)
  442. {
  443. if (!nm_command.count)
  444. {
  445. static const char * const commands[] = { "nm", "gnm", NULL };
  446. nm_command = find_tool( "nm", commands );
  447. }
  448. if (nm_command.count > 1)
  449. fatal_error( "multiple arguments in nm command not supported yet\n" );
  450. return nm_command.str[0];
  451. }
  452. /* get a name for a temp file, automatically cleaned up on exit */
  453. char *get_temp_file_name( const char *prefix, const char *suffix )
  454. {
  455. char *name;
  456. const char *ext, *basename;
  457. int fd;
  458. if (!prefix || !prefix[0]) prefix = "winebuild";
  459. if (!suffix) suffix = "";
  460. if ((basename = strrchr( prefix, '/' ))) basename++;
  461. else basename = prefix;
  462. if (!(ext = strchr( basename, '.' ))) ext = prefix + strlen(prefix);
  463. name = xmalloc( sizeof("/tmp/") + (ext - prefix) + sizeof(".XXXXXX") + strlen(suffix) );
  464. memcpy( name, prefix, ext - prefix );
  465. strcpy( name + (ext - prefix), ".XXXXXX" );
  466. strcat( name, suffix );
  467. if ((fd = mkstemps( name, strlen(suffix) )) == -1)
  468. {
  469. strcpy( name, "/tmp/" );
  470. memcpy( name + 5, basename, ext - basename );
  471. strcpy( name + 5 + (ext - basename), ".XXXXXX" );
  472. strcat( name, suffix );
  473. if ((fd = mkstemps( name, strlen(suffix) )) == -1)
  474. fatal_error( "could not generate a temp file\n" );
  475. }
  476. close( fd );
  477. strarray_add_one( &tmp_files, name );
  478. return name;
  479. }
  480. /*******************************************************************
  481. * buffer management
  482. *
  483. * Function for reading from/writing to a memory buffer.
  484. */
  485. int byte_swapped = 0;
  486. const char *input_buffer_filename;
  487. const unsigned char *input_buffer;
  488. size_t input_buffer_pos;
  489. size_t input_buffer_size;
  490. unsigned char *output_buffer;
  491. size_t output_buffer_pos;
  492. size_t output_buffer_size;
  493. static void check_output_buffer_space( size_t size )
  494. {
  495. if (output_buffer_pos + size >= output_buffer_size)
  496. {
  497. output_buffer_size = max( output_buffer_size * 2, output_buffer_pos + size );
  498. output_buffer = xrealloc( output_buffer, output_buffer_size );
  499. }
  500. }
  501. void init_input_buffer( const char *file )
  502. {
  503. int fd;
  504. struct stat st;
  505. unsigned char *buffer;
  506. if ((fd = open( file, O_RDONLY | O_BINARY )) == -1) fatal_perror( "Cannot open %s", file );
  507. if ((fstat( fd, &st ) == -1)) fatal_perror( "Cannot stat %s", file );
  508. if (!st.st_size) fatal_error( "%s is an empty file\n", file );
  509. input_buffer = buffer = xmalloc( st.st_size );
  510. if (read( fd, buffer, st.st_size ) != st.st_size) fatal_error( "Cannot read %s\n", file );
  511. close( fd );
  512. input_buffer_filename = xstrdup( file );
  513. input_buffer_size = st.st_size;
  514. input_buffer_pos = 0;
  515. byte_swapped = 0;
  516. }
  517. void init_output_buffer(void)
  518. {
  519. output_buffer_size = 1024;
  520. output_buffer_pos = 0;
  521. output_buffer = xmalloc( output_buffer_size );
  522. }
  523. void flush_output_buffer(void)
  524. {
  525. open_output_file();
  526. if (fwrite( output_buffer, 1, output_buffer_pos, output_file ) != output_buffer_pos)
  527. fatal_error( "Error writing to %s\n", output_file_name );
  528. close_output_file();
  529. free( output_buffer );
  530. }
  531. unsigned char get_byte(void)
  532. {
  533. if (input_buffer_pos >= input_buffer_size)
  534. fatal_error( "%s is a truncated file\n", input_buffer_filename );
  535. return input_buffer[input_buffer_pos++];
  536. }
  537. unsigned short get_word(void)
  538. {
  539. unsigned short ret;
  540. if (input_buffer_pos + sizeof(ret) > input_buffer_size)
  541. fatal_error( "%s is a truncated file\n", input_buffer_filename );
  542. memcpy( &ret, input_buffer + input_buffer_pos, sizeof(ret) );
  543. if (byte_swapped) ret = (ret << 8) | (ret >> 8);
  544. input_buffer_pos += sizeof(ret);
  545. return ret;
  546. }
  547. unsigned int get_dword(void)
  548. {
  549. unsigned int ret;
  550. if (input_buffer_pos + sizeof(ret) > input_buffer_size)
  551. fatal_error( "%s is a truncated file\n", input_buffer_filename );
  552. memcpy( &ret, input_buffer + input_buffer_pos, sizeof(ret) );
  553. if (byte_swapped)
  554. ret = ((ret << 24) | ((ret << 8) & 0x00ff0000) | ((ret >> 8) & 0x0000ff00) | (ret >> 24));
  555. input_buffer_pos += sizeof(ret);
  556. return ret;
  557. }
  558. void put_data( const void *data, size_t size )
  559. {
  560. check_output_buffer_space( size );
  561. memcpy( output_buffer + output_buffer_pos, data, size );
  562. output_buffer_pos += size;
  563. }
  564. void put_byte( unsigned char val )
  565. {
  566. check_output_buffer_space( 1 );
  567. output_buffer[output_buffer_pos++] = val;
  568. }
  569. void put_word( unsigned short val )
  570. {
  571. if (byte_swapped) val = (val << 8) | (val >> 8);
  572. put_data( &val, sizeof(val) );
  573. }
  574. void put_dword( unsigned int val )
  575. {
  576. if (byte_swapped)
  577. val = ((val << 24) | ((val << 8) & 0x00ff0000) | ((val >> 8) & 0x0000ff00) | (val >> 24));
  578. put_data( &val, sizeof(val) );
  579. }
  580. void put_qword( unsigned int val )
  581. {
  582. if (byte_swapped)
  583. {
  584. put_dword( 0 );
  585. put_dword( val );
  586. }
  587. else
  588. {
  589. put_dword( val );
  590. put_dword( 0 );
  591. }
  592. }
  593. /* pointer-sized word */
  594. void put_pword( unsigned int val )
  595. {
  596. if (get_ptr_size() == 8) put_qword( val );
  597. else put_dword( val );
  598. }
  599. void align_output( unsigned int align )
  600. {
  601. size_t size = align - (output_buffer_pos % align);
  602. if (size == align) return;
  603. check_output_buffer_space( size );
  604. memset( output_buffer + output_buffer_pos, 0, size );
  605. output_buffer_pos += size;
  606. }
  607. /* output a standard header for generated files */
  608. void output_standard_file_header(void)
  609. {
  610. if (spec_file_name)
  611. output( "/* File generated automatically from %s; do not edit! */\n", spec_file_name );
  612. else
  613. output( "/* File generated automatically; do not edit! */\n" );
  614. output( "/* This file can be copied, modified and distributed without restriction. */\n\n" );
  615. if (safe_seh)
  616. {
  617. output( "\t.def @feat.00\n\t.scl 3\n\t.type 0\n\t.endef\n" );
  618. output( "\t.globl @feat.00\n" );
  619. output( ".set @feat.00, 1\n" );
  620. }
  621. if (thumb_mode)
  622. {
  623. output( "\t.syntax unified\n" );
  624. output( "\t.thumb\n" );
  625. }
  626. }
  627. /* dump a byte stream into the assembly code */
  628. void dump_bytes( const void *buffer, unsigned int size )
  629. {
  630. unsigned int i;
  631. const unsigned char *ptr = buffer;
  632. if (!size) return;
  633. output( "\t.byte " );
  634. for (i = 0; i < size - 1; i++, ptr++)
  635. {
  636. if ((i % 16) == 15) output( "0x%02x\n\t.byte ", *ptr );
  637. else output( "0x%02x,", *ptr );
  638. }
  639. output( "0x%02x\n", *ptr );
  640. }
  641. /*******************************************************************
  642. * open_input_file
  643. *
  644. * Open a file in the given srcdir and set the input_file_name global variable.
  645. */
  646. FILE *open_input_file( const char *srcdir, const char *name )
  647. {
  648. char *fullname;
  649. FILE *file = fopen( name, "r" );
  650. if (!file && srcdir)
  651. {
  652. fullname = strmake( "%s/%s", srcdir, name );
  653. file = fopen( fullname, "r" );
  654. }
  655. else fullname = xstrdup( name );
  656. if (!file) fatal_error( "Cannot open file '%s'\n", fullname );
  657. input_file_name = fullname;
  658. current_line = 1;
  659. return file;
  660. }
  661. /*******************************************************************
  662. * close_input_file
  663. *
  664. * Close the current input file (must have been opened with open_input_file).
  665. */
  666. void close_input_file( FILE *file )
  667. {
  668. fclose( file );
  669. free( input_file_name );
  670. input_file_name = NULL;
  671. current_line = 0;
  672. }
  673. /*******************************************************************
  674. * open_output_file
  675. */
  676. void open_output_file(void)
  677. {
  678. if (output_file_name)
  679. {
  680. if (strendswith( output_file_name, ".o" ))
  681. output_file_source_name = open_temp_output_file( ".s" );
  682. else
  683. if (!(output_file = fopen( output_file_name, "w" )))
  684. fatal_error( "Unable to create output file '%s'\n", output_file_name );
  685. }
  686. else output_file = stdout;
  687. }
  688. /*******************************************************************
  689. * close_output_file
  690. */
  691. void close_output_file(void)
  692. {
  693. if (!output_file || !output_file_name) return;
  694. if (fclose( output_file ) < 0) fatal_perror( "fclose" );
  695. if (output_file_source_name) assemble_file( output_file_source_name, output_file_name );
  696. output_file = NULL;
  697. }
  698. /*******************************************************************
  699. * open_temp_output_file
  700. */
  701. char *open_temp_output_file( const char *suffix )
  702. {
  703. char *tmp_file = get_temp_file_name( output_file_name, suffix );
  704. if (!(output_file = fopen( tmp_file, "w" )))
  705. fatal_error( "Unable to create output file '%s'\n", tmp_file );
  706. return tmp_file;
  707. }
  708. /*******************************************************************
  709. * remove_stdcall_decoration
  710. *
  711. * Remove a possible @xx suffix from a function name.
  712. * Return the numerical value of the suffix, or -1 if none.
  713. */
  714. int remove_stdcall_decoration( char *name )
  715. {
  716. char *p, *end = strrchr( name, '@' );
  717. if (!end || !end[1] || end == name) return -1;
  718. if (target_cpu != CPU_x86) return -1;
  719. /* make sure all the rest is digits */
  720. for (p = end + 1; *p; p++) if (!isdigit(*p)) return -1;
  721. *end = 0;
  722. return atoi( end + 1 );
  723. }
  724. /*******************************************************************
  725. * assemble_file
  726. *
  727. * Run a file through the assembler.
  728. */
  729. void assemble_file( const char *src_file, const char *obj_file )
  730. {
  731. struct strarray args = get_as_command();
  732. strarray_add( &args, "-o", obj_file, src_file, NULL );
  733. spawn( args );
  734. }
  735. /*******************************************************************
  736. * alloc_dll_spec
  737. *
  738. * Create a new dll spec file descriptor
  739. */
  740. DLLSPEC *alloc_dll_spec(void)
  741. {
  742. DLLSPEC *spec;
  743. spec = xmalloc( sizeof(*spec) );
  744. memset( spec, 0, sizeof(*spec) );
  745. spec->type = SPEC_WIN32;
  746. spec->base = MAX_ORDINALS;
  747. spec->characteristics = IMAGE_FILE_EXECUTABLE_IMAGE;
  748. spec->subsystem = 0;
  749. spec->subsystem_major = 4;
  750. spec->subsystem_minor = 0;
  751. if (get_ptr_size() > 4)
  752. spec->characteristics |= IMAGE_FILE_LARGE_ADDRESS_AWARE;
  753. else
  754. spec->characteristics |= IMAGE_FILE_32BIT_MACHINE;
  755. spec->dll_characteristics = IMAGE_DLLCHARACTERISTICS_NX_COMPAT;
  756. return spec;
  757. }
  758. /*******************************************************************
  759. * free_dll_spec
  760. *
  761. * Free dll spec file descriptor
  762. */
  763. void free_dll_spec( DLLSPEC *spec )
  764. {
  765. int i;
  766. for (i = 0; i < spec->nb_entry_points; i++)
  767. {
  768. ORDDEF *odp = &spec->entry_points[i];
  769. free( odp->name );
  770. free( odp->export_name );
  771. free( odp->link_name );
  772. }
  773. free( spec->file_name );
  774. free( spec->dll_name );
  775. free( spec->c_name );
  776. free( spec->init_func );
  777. free( spec->entry_points );
  778. free( spec->names );
  779. free( spec->ordinals );
  780. free( spec->resources );
  781. free( spec );
  782. }
  783. /*******************************************************************
  784. * make_c_identifier
  785. *
  786. * Map a string to a valid C identifier.
  787. */
  788. char *make_c_identifier( const char *str )
  789. {
  790. char *p, buffer[256];
  791. for (p = buffer; *str && p < buffer+sizeof(buffer)-1; p++, str++)
  792. {
  793. if (isalnum(*str)) *p = *str;
  794. else *p = '_';
  795. }
  796. *p = 0;
  797. return xstrdup( buffer );
  798. }
  799. /*******************************************************************
  800. * get_stub_name
  801. *
  802. * Generate an internal name for a stub entry point.
  803. */
  804. const char *get_stub_name( const ORDDEF *odp, const DLLSPEC *spec )
  805. {
  806. static char *buffer;
  807. free( buffer );
  808. if (odp->name || odp->export_name)
  809. {
  810. char *p;
  811. buffer = strmake( "__wine_stub_%s", odp->name ? odp->name : odp->export_name );
  812. /* make sure name is a legal C identifier */
  813. for (p = buffer; *p; p++) if (!isalnum(*p) && *p != '_') break;
  814. if (!*p) return buffer;
  815. free( buffer );
  816. }
  817. buffer = strmake( "__wine_stub_%s_%d", make_c_identifier(spec->file_name), odp->ordinal );
  818. return buffer;
  819. }
  820. /* return the stdcall-decorated name for an entry point */
  821. const char *get_link_name( const ORDDEF *odp )
  822. {
  823. static char *buffer;
  824. char *ret;
  825. if (target_cpu != CPU_x86) return odp->link_name;
  826. switch (odp->type)
  827. {
  828. case TYPE_STDCALL:
  829. if (is_pe())
  830. {
  831. if (odp->flags & FLAG_THISCALL) return odp->link_name;
  832. if (odp->flags & FLAG_FASTCALL) ret = strmake( "@%s@%u", odp->link_name, get_args_size( odp ));
  833. else if (!kill_at) ret = strmake( "%s@%u", odp->link_name, get_args_size( odp ));
  834. else return odp->link_name;
  835. }
  836. else
  837. {
  838. if (odp->flags & FLAG_THISCALL) ret = strmake( "__thiscall_%s", odp->link_name );
  839. else if (odp->flags & FLAG_FASTCALL) ret = strmake( "__fastcall_%s", odp->link_name );
  840. else return odp->link_name;
  841. }
  842. break;
  843. case TYPE_PASCAL:
  844. if (is_pe() && !kill_at)
  845. {
  846. int args = get_args_size( odp );
  847. if (odp->flags & FLAG_REGISTER) args += get_ptr_size(); /* context argument */
  848. ret = strmake( "%s@%u", odp->link_name, args );
  849. }
  850. else return odp->link_name;
  851. break;
  852. default:
  853. return odp->link_name;
  854. }
  855. free( buffer );
  856. buffer = ret;
  857. return ret;
  858. }
  859. /*******************************************************************
  860. * sort_func_list
  861. *
  862. * Sort a list of functions, removing duplicates.
  863. */
  864. int sort_func_list( ORDDEF **list, int count, int (*compare)(const void *, const void *) )
  865. {
  866. int i, j;
  867. if (!count) return 0;
  868. qsort( list, count, sizeof(*list), compare );
  869. for (i = j = 0; i < count; i++) if (compare( &list[j], &list[i] )) list[++j] = list[i];
  870. return j + 1;
  871. }
  872. /* parse a cpu name and return the corresponding value */
  873. int get_cpu_from_name( const char *name )
  874. {
  875. unsigned int i;
  876. for (i = 0; i < ARRAY_SIZE(cpu_names); i++)
  877. if (!strcmp( cpu_names[i].name, name )) return cpu_names[i].cpu;
  878. return -1;
  879. }
  880. /*****************************************************************
  881. * Function: get_alignment
  882. *
  883. * Description:
  884. * According to the info page for gas, the .align directive behaves
  885. * differently on different systems. On some architectures, the
  886. * argument of a .align directive is the number of bytes to pad to, so
  887. * to align on an 8-byte boundary you'd say
  888. * .align 8
  889. * On other systems, the argument is "the number of low-order zero bits
  890. * that the location counter must have after advancement." So to
  891. * align on an 8-byte boundary you'd say
  892. * .align 3
  893. *
  894. * The reason gas is written this way is that it's trying to mimic
  895. * native assemblers for the various architectures it runs on. gas
  896. * provides other directives that work consistently across
  897. * architectures, but of course we want to work on all arches with or
  898. * without gas. Hence this function.
  899. *
  900. *
  901. * Parameters:
  902. * align -- the number of bytes to align to. Must be a power of 2.
  903. */
  904. unsigned int get_alignment(unsigned int align)
  905. {
  906. unsigned int n;
  907. assert( !(align & (align - 1)) );
  908. switch(target_cpu)
  909. {
  910. case CPU_x86:
  911. case CPU_x86_64:
  912. if (target_platform != PLATFORM_APPLE) return align;
  913. /* fall through */
  914. case CPU_POWERPC:
  915. case CPU_ARM:
  916. case CPU_ARM64:
  917. n = 0;
  918. while ((1u << n) != align) n++;
  919. return n;
  920. }
  921. /* unreached */
  922. assert(0);
  923. return 0;
  924. }
  925. /* return the page size for the target CPU */
  926. unsigned int get_page_size(void)
  927. {
  928. return 0x1000; /* same on all platforms */
  929. }
  930. /* return the size of a pointer on the target CPU */
  931. unsigned int get_ptr_size(void)
  932. {
  933. switch(target_cpu)
  934. {
  935. case CPU_x86:
  936. case CPU_POWERPC:
  937. case CPU_ARM:
  938. return 4;
  939. case CPU_x86_64:
  940. case CPU_ARM64:
  941. return 8;
  942. }
  943. /* unreached */
  944. assert(0);
  945. return 0;
  946. }
  947. /* return the total size in bytes of the arguments on the stack */
  948. unsigned int get_args_size( const ORDDEF *odp )
  949. {
  950. int i, size;
  951. for (i = size = 0; i < odp->u.func.nb_args; i++)
  952. {
  953. switch (odp->u.func.args[i])
  954. {
  955. case ARG_INT64:
  956. case ARG_DOUBLE:
  957. size += 8;
  958. break;
  959. case ARG_INT128:
  960. /* int128 is passed as pointer on x86_64 */
  961. if (target_cpu != CPU_x86_64)
  962. {
  963. size += 16;
  964. break;
  965. }
  966. /* fall through */
  967. default:
  968. size += get_ptr_size();
  969. break;
  970. }
  971. }
  972. return size;
  973. }
  974. /* return the assembly name for a C symbol */
  975. const char *asm_name( const char *sym )
  976. {
  977. static char *buffer;
  978. switch (target_platform)
  979. {
  980. case PLATFORM_MINGW:
  981. case PLATFORM_WINDOWS:
  982. if (target_cpu != CPU_x86) return sym;
  983. if (sym[0] == '@') return sym; /* fastcall */
  984. /* fall through */
  985. case PLATFORM_APPLE:
  986. if (sym[0] == '.' && sym[1] == 'L') return sym;
  987. free( buffer );
  988. buffer = strmake( "_%s", sym );
  989. return buffer;
  990. default:
  991. return sym;
  992. }
  993. }
  994. /* return an assembly function declaration for a C function name */
  995. const char *func_declaration( const char *func )
  996. {
  997. static char *buffer;
  998. switch (target_platform)
  999. {
  1000. case PLATFORM_APPLE:
  1001. return "";
  1002. case PLATFORM_MINGW:
  1003. case PLATFORM_WINDOWS:
  1004. free( buffer );
  1005. buffer = strmake( ".def %s\n\t.scl 2\n\t.type 32\n\t.endef%s", asm_name(func),
  1006. thumb_mode ? "\n\t.thumb_func" : "" );
  1007. break;
  1008. default:
  1009. free( buffer );
  1010. switch(target_cpu)
  1011. {
  1012. case CPU_ARM:
  1013. buffer = strmake( ".type %s,%%function%s", func,
  1014. thumb_mode ? "\n\t.thumb_func" : "" );
  1015. break;
  1016. case CPU_ARM64:
  1017. buffer = strmake( ".type %s,%%function", func );
  1018. break;
  1019. default:
  1020. buffer = strmake( ".type %s,@function", func );
  1021. break;
  1022. }
  1023. break;
  1024. }
  1025. return buffer;
  1026. }
  1027. /* output a size declaration for an assembly function */
  1028. void output_function_size( const char *name )
  1029. {
  1030. switch (target_platform)
  1031. {
  1032. case PLATFORM_APPLE:
  1033. case PLATFORM_MINGW:
  1034. case PLATFORM_WINDOWS:
  1035. break;
  1036. default:
  1037. output( "\t.size %s, .-%s\n", name, name );
  1038. break;
  1039. }
  1040. }
  1041. /* output a .cfi directive */
  1042. void output_cfi( const char *format, ... )
  1043. {
  1044. va_list valist;
  1045. if (!unwind_tables) return;
  1046. va_start( valist, format );
  1047. fputc( '\t', output_file );
  1048. vfprintf( output_file, format, valist );
  1049. fputc( '\n', output_file );
  1050. va_end( valist );
  1051. }
  1052. /* output an RVA pointer */
  1053. void output_rva( const char *format, ... )
  1054. {
  1055. va_list valist;
  1056. va_start( valist, format );
  1057. switch (target_platform)
  1058. {
  1059. case PLATFORM_MINGW:
  1060. case PLATFORM_WINDOWS:
  1061. output( "\t.rva " );
  1062. vfprintf( output_file, format, valist );
  1063. fputc( '\n', output_file );
  1064. break;
  1065. default:
  1066. output( "\t.long " );
  1067. vfprintf( output_file, format, valist );
  1068. output( " - .L__wine_spec_rva_base\n" );
  1069. break;
  1070. }
  1071. va_end( valist );
  1072. }
  1073. /* output the GNU note for non-exec stack */
  1074. void output_gnu_stack_note(void)
  1075. {
  1076. switch (target_platform)
  1077. {
  1078. case PLATFORM_MINGW:
  1079. case PLATFORM_WINDOWS:
  1080. case PLATFORM_APPLE:
  1081. break;
  1082. default:
  1083. switch(target_cpu)
  1084. {
  1085. case CPU_ARM:
  1086. case CPU_ARM64:
  1087. output( "\t.section .note.GNU-stack,\"\",%%progbits\n" );
  1088. break;
  1089. default:
  1090. output( "\t.section .note.GNU-stack,\"\",@progbits\n" );
  1091. break;
  1092. }
  1093. break;
  1094. }
  1095. }
  1096. /* return a global symbol declaration for an assembly symbol */
  1097. const char *asm_globl( const char *func )
  1098. {
  1099. static char *buffer;
  1100. free( buffer );
  1101. switch (target_platform)
  1102. {
  1103. case PLATFORM_APPLE:
  1104. buffer = strmake( "\t.globl _%s\n\t.private_extern _%s\n_%s:", func, func, func );
  1105. break;
  1106. case PLATFORM_MINGW:
  1107. case PLATFORM_WINDOWS:
  1108. buffer = strmake( "\t.globl %s%s\n%s%s:", target_cpu == CPU_x86 ? "_" : "", func,
  1109. target_cpu == CPU_x86 ? "_" : "", func );
  1110. break;
  1111. default:
  1112. buffer = strmake( "\t.globl %s\n\t.hidden %s\n%s:", func, func, func );
  1113. break;
  1114. }
  1115. return buffer;
  1116. }
  1117. const char *get_asm_ptr_keyword(void)
  1118. {
  1119. switch(get_ptr_size())
  1120. {
  1121. case 4: return ".long";
  1122. case 8: return ".quad";
  1123. }
  1124. assert(0);
  1125. return NULL;
  1126. }
  1127. const char *get_asm_string_keyword(void)
  1128. {
  1129. switch (target_platform)
  1130. {
  1131. case PLATFORM_APPLE:
  1132. return ".asciz";
  1133. default:
  1134. return ".string";
  1135. }
  1136. }
  1137. const char *get_asm_export_section(void)
  1138. {
  1139. switch (target_platform)
  1140. {
  1141. case PLATFORM_APPLE: return ".data";
  1142. case PLATFORM_MINGW:
  1143. case PLATFORM_WINDOWS: return ".section .edata";
  1144. default: return ".section .data";
  1145. }
  1146. }
  1147. const char *get_asm_rodata_section(void)
  1148. {
  1149. switch (target_platform)
  1150. {
  1151. case PLATFORM_APPLE: return ".const";
  1152. default: return ".section .rodata";
  1153. }
  1154. }
  1155. const char *get_asm_rsrc_section(void)
  1156. {
  1157. switch (target_platform)
  1158. {
  1159. case PLATFORM_APPLE: return ".data";
  1160. case PLATFORM_MINGW:
  1161. case PLATFORM_WINDOWS: return ".section .rsrc";
  1162. default: return ".section .data";
  1163. }
  1164. }
  1165. const char *get_asm_string_section(void)
  1166. {
  1167. switch (target_platform)
  1168. {
  1169. case PLATFORM_APPLE: return ".cstring";
  1170. default: return ".section .rodata";
  1171. }
  1172. }
  1173. const char *arm64_page( const char *sym )
  1174. {
  1175. static char *buffer;
  1176. switch (target_platform)
  1177. {
  1178. case PLATFORM_APPLE:
  1179. free( buffer );
  1180. buffer = strmake( "%s@PAGE", sym );
  1181. return buffer;
  1182. default:
  1183. return sym;
  1184. }
  1185. }
  1186. const char *arm64_pageoff( const char *sym )
  1187. {
  1188. static char *buffer;
  1189. free( buffer );
  1190. switch (target_platform)
  1191. {
  1192. case PLATFORM_APPLE:
  1193. buffer = strmake( "%s@PAGEOFF", sym );
  1194. break;
  1195. default:
  1196. buffer = strmake( ":lo12:%s", sym );
  1197. break;
  1198. }
  1199. return buffer;
  1200. }