dllwrap.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255
  1. /* dllwrap.c -- wrapper for DLLTOOL and GCC to generate PE style DLLs
  2. Copyright (C) 1998-2022 Free Software Foundation, Inc.
  3. Contributed by Mumit Khan (khan@xraylith.wisc.edu).
  4. This file is part of GNU Binutils.
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 3 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
  16. 02110-1301, USA. */
  17. #include "sysdep.h"
  18. #include "bfd.h"
  19. #include "libiberty.h"
  20. #include "getopt.h"
  21. #include "dyn-string.h"
  22. #include "bucomm.h"
  23. #include <time.h>
  24. #ifdef HAVE_SYS_WAIT_H
  25. #include <sys/wait.h>
  26. #else /* ! HAVE_SYS_WAIT_H */
  27. #if ! defined (_WIN32) || defined (__CYGWIN32__)
  28. #ifndef WIFEXITED
  29. #define WIFEXITED(w) (((w)&0377) == 0)
  30. #endif
  31. #ifndef WIFSIGNALED
  32. #define WIFSIGNALED(w) (((w)&0377) != 0177 && ((w)&~0377) == 0)
  33. #endif
  34. #ifndef WTERMSIG
  35. #define WTERMSIG(w) ((w) & 0177)
  36. #endif
  37. #ifndef WEXITSTATUS
  38. #define WEXITSTATUS(w) (((w) >> 8) & 0377)
  39. #endif
  40. #else /* defined (_WIN32) && ! defined (__CYGWIN32__) */
  41. #ifndef WIFEXITED
  42. #define WIFEXITED(w) (((w) & 0xff) == 0)
  43. #endif
  44. #ifndef WIFSIGNALED
  45. #define WIFSIGNALED(w) (((w) & 0xff) != 0 && ((w) & 0xff) != 0x7f)
  46. #endif
  47. #ifndef WTERMSIG
  48. #define WTERMSIG(w) ((w) & 0x7f)
  49. #endif
  50. #ifndef WEXITSTATUS
  51. #define WEXITSTATUS(w) (((w) & 0xff00) >> 8)
  52. #endif
  53. #endif /* defined (_WIN32) && ! defined (__CYGWIN32__) */
  54. #endif /* ! HAVE_SYS_WAIT_H */
  55. static char *driver_name = NULL;
  56. static char *cygwin_driver_flags =
  57. "-Wl,--dll -nostartfiles";
  58. static char *mingw32_driver_flags = "-mdll";
  59. static char *generic_driver_flags = "-Wl,--dll";
  60. static char *entry_point;
  61. static char *dlltool_name = NULL;
  62. static char *target = TARGET;
  63. /* -1: use default, 0: no underscoring, 1: underscore. */
  64. static int is_leading_underscore = -1;
  65. typedef enum {
  66. UNKNOWN_TARGET,
  67. CYGWIN_TARGET,
  68. MINGW_TARGET
  69. }
  70. target_type;
  71. typedef enum {
  72. UNKNOWN_CPU,
  73. X86_CPU,
  74. X64_CPU,
  75. ARM_CPU
  76. }
  77. target_cpu;
  78. static target_type which_target = UNKNOWN_TARGET;
  79. static target_cpu which_cpu = UNKNOWN_CPU;
  80. static int dontdeltemps = 0;
  81. static int dry_run = 0;
  82. static char *prog_name;
  83. static int verbose = 0;
  84. static char *dll_file_name;
  85. static char *dll_name;
  86. static char *base_file_name;
  87. static char *exp_file_name;
  88. static char *def_file_name;
  89. static int delete_base_file = 1;
  90. static int delete_exp_file = 1;
  91. static int delete_def_file = 1;
  92. static int run (const char *, char *);
  93. static char *mybasename (const char *);
  94. static int strhash (const char *);
  95. static void usage (FILE *, int);
  96. static void display (const char *, va_list) ATTRIBUTE_PRINTF(1,0);
  97. static void inform (const char *, ...) ATTRIBUTE_PRINTF_1;
  98. static void warn (const char *, ...) ATTRIBUTE_PRINTF_1;
  99. static char *look_for_prog (const char *, const char *, int);
  100. static char *deduce_name (const char *);
  101. static void delete_temp_files (void);
  102. static void cleanup_and_exit (int);
  103. /**********************************************************************/
  104. /* Please keep the following 4 routines in sync with dlltool.c:
  105. display ()
  106. inform ()
  107. look_for_prog ()
  108. deduce_name ()
  109. It's not worth the hassle to break these out since dllwrap will
  110. (hopefully) soon be retired in favor of `ld --shared. */
  111. static void
  112. display (const char * message, va_list args)
  113. {
  114. if (prog_name != NULL)
  115. fprintf (stderr, "%s: ", prog_name);
  116. vfprintf (stderr, message, args);
  117. fputc ('\n', stderr);
  118. }
  119. static void
  120. inform (const char *message, ...)
  121. {
  122. va_list args;
  123. va_start (args, message);
  124. if (!verbose)
  125. return;
  126. display (message, args);
  127. va_end (args);
  128. }
  129. static void
  130. warn (const char *format, ...)
  131. {
  132. va_list args;
  133. va_start (args, format);
  134. display (format, args);
  135. va_end (args);
  136. }
  137. /* Look for the program formed by concatenating PROG_NAME and the
  138. string running from PREFIX to END_PREFIX. If the concatenated
  139. string contains a '/', try appending EXECUTABLE_SUFFIX if it is
  140. appropriate. */
  141. static char *
  142. look_for_prog (const char *progname, const char *prefix, int end_prefix)
  143. {
  144. struct stat s;
  145. char *cmd;
  146. cmd = xmalloc (strlen (prefix)
  147. + strlen (progname)
  148. #ifdef HAVE_EXECUTABLE_SUFFIX
  149. + strlen (EXECUTABLE_SUFFIX)
  150. #endif
  151. + 10);
  152. strcpy (cmd, prefix);
  153. sprintf (cmd + end_prefix, "%s", progname);
  154. if (strchr (cmd, '/') != NULL)
  155. {
  156. int found;
  157. found = (stat (cmd, &s) == 0
  158. #ifdef HAVE_EXECUTABLE_SUFFIX
  159. || stat (strcat (cmd, EXECUTABLE_SUFFIX), &s) == 0
  160. #endif
  161. );
  162. if (! found)
  163. {
  164. /* xgettext:c-format */
  165. inform (_("Tried file: %s"), cmd);
  166. free (cmd);
  167. return NULL;
  168. }
  169. }
  170. /* xgettext:c-format */
  171. inform (_("Using file: %s"), cmd);
  172. return cmd;
  173. }
  174. /* Deduce the name of the program we are want to invoke.
  175. PROG_NAME is the basic name of the program we want to run,
  176. eg "as" or "ld". The catch is that we might want actually
  177. run "i386-pe-as" or "ppc-pe-ld".
  178. If argv[0] contains the full path, then try to find the program
  179. in the same place, with and then without a target-like prefix.
  180. Given, argv[0] = /usr/local/bin/i586-cygwin32-dlltool,
  181. deduce_name("as") uses the following search order:
  182. /usr/local/bin/i586-cygwin32-as
  183. /usr/local/bin/as
  184. as
  185. If there's an EXECUTABLE_SUFFIX, it'll use that as well; for each
  186. name, it'll try without and then with EXECUTABLE_SUFFIX.
  187. Given, argv[0] = i586-cygwin32-dlltool, it will not even try "as"
  188. as the fallback, but rather return i586-cygwin32-as.
  189. Oh, and given, argv[0] = dlltool, it'll return "as".
  190. Returns a dynamically allocated string. */
  191. static char *
  192. deduce_name (const char * name)
  193. {
  194. char *cmd;
  195. const char *dash;
  196. const char *slash;
  197. const char *cp;
  198. dash = NULL;
  199. slash = NULL;
  200. for (cp = prog_name; *cp != '\0'; ++cp)
  201. {
  202. if (*cp == '-')
  203. dash = cp;
  204. if (
  205. #if defined(__DJGPP__) || defined (__CYGWIN__) || defined(__WIN32__)
  206. *cp == ':' || *cp == '\\' ||
  207. #endif
  208. *cp == '/')
  209. {
  210. slash = cp;
  211. dash = NULL;
  212. }
  213. }
  214. cmd = NULL;
  215. if (dash != NULL)
  216. /* First, try looking for a prefixed NAME in the
  217. PROG_NAME directory, with the same prefix as PROG_NAME. */
  218. cmd = look_for_prog (name, prog_name, dash - prog_name + 1);
  219. if (slash != NULL && cmd == NULL)
  220. /* Next, try looking for a NAME in the same directory as
  221. that of this program. */
  222. cmd = look_for_prog (name, prog_name, slash - prog_name + 1);
  223. if (cmd == NULL)
  224. /* Just return NAME as is. */
  225. cmd = xstrdup (name);
  226. return cmd;
  227. }
  228. static void
  229. delete_temp_files (void)
  230. {
  231. if (delete_base_file && base_file_name)
  232. {
  233. if (verbose)
  234. {
  235. if (dontdeltemps)
  236. warn (_("Keeping temporary base file %s"), base_file_name);
  237. else
  238. warn (_("Deleting temporary base file %s"), base_file_name);
  239. }
  240. if (! dontdeltemps)
  241. {
  242. unlink (base_file_name);
  243. free (base_file_name);
  244. }
  245. }
  246. if (delete_exp_file && exp_file_name)
  247. {
  248. if (verbose)
  249. {
  250. if (dontdeltemps)
  251. warn (_("Keeping temporary exp file %s"), exp_file_name);
  252. else
  253. warn (_("Deleting temporary exp file %s"), exp_file_name);
  254. }
  255. if (! dontdeltemps)
  256. {
  257. unlink (exp_file_name);
  258. free (exp_file_name);
  259. }
  260. }
  261. if (delete_def_file && def_file_name)
  262. {
  263. if (verbose)
  264. {
  265. if (dontdeltemps)
  266. warn (_("Keeping temporary def file %s"), def_file_name);
  267. else
  268. warn (_("Deleting temporary def file %s"), def_file_name);
  269. }
  270. if (! dontdeltemps)
  271. {
  272. unlink (def_file_name);
  273. free (def_file_name);
  274. }
  275. }
  276. }
  277. static void
  278. cleanup_and_exit (int status)
  279. {
  280. delete_temp_files ();
  281. exit (status);
  282. }
  283. static int
  284. run (const char *what, char *args)
  285. {
  286. char *s;
  287. int pid, wait_status, retcode;
  288. int i;
  289. const char **argv;
  290. char *errmsg_fmt, *errmsg_arg;
  291. char *temp_base = choose_temp_base ();
  292. int in_quote;
  293. char sep;
  294. if (verbose || dry_run)
  295. fprintf (stderr, "%s %s\n", what, args);
  296. /* Count the args */
  297. i = 0;
  298. for (s = args; *s; s++)
  299. if (*s == ' ')
  300. i++;
  301. i++;
  302. argv = xmalloc (sizeof (char *) * (i + 3));
  303. i = 0;
  304. argv[i++] = what;
  305. s = args;
  306. while (1)
  307. {
  308. while (*s == ' ' && *s != 0)
  309. s++;
  310. if (*s == 0)
  311. break;
  312. in_quote = (*s == '\'' || *s == '"');
  313. sep = (in_quote) ? *s++ : ' ';
  314. argv[i++] = s;
  315. while (*s != sep && *s != 0)
  316. s++;
  317. if (*s == 0)
  318. break;
  319. *s++ = 0;
  320. if (in_quote)
  321. s++;
  322. }
  323. argv[i++] = NULL;
  324. if (dry_run)
  325. return 0;
  326. pid = pexecute (argv[0], (char * const *) argv, prog_name, temp_base,
  327. &errmsg_fmt, &errmsg_arg, PEXECUTE_ONE | PEXECUTE_SEARCH);
  328. free (argv);
  329. if (pid == -1)
  330. {
  331. int errno_val = errno;
  332. fprintf (stderr, "%s: ", prog_name);
  333. fprintf (stderr, errmsg_fmt, errmsg_arg);
  334. fprintf (stderr, ": %s\n", strerror (errno_val));
  335. return 1;
  336. }
  337. retcode = 0;
  338. pid = pwait (pid, &wait_status, 0);
  339. if (pid == -1)
  340. {
  341. warn (_("pwait returns: %s"), strerror (errno));
  342. retcode = 1;
  343. }
  344. else if (WIFSIGNALED (wait_status))
  345. {
  346. warn (_("subprocess got fatal signal %d"), WTERMSIG (wait_status));
  347. retcode = 1;
  348. }
  349. else if (WIFEXITED (wait_status))
  350. {
  351. if (WEXITSTATUS (wait_status) != 0)
  352. {
  353. warn (_("%s exited with status %d"), what, WEXITSTATUS (wait_status));
  354. retcode = 1;
  355. }
  356. }
  357. else
  358. retcode = 1;
  359. return retcode;
  360. }
  361. static char *
  362. mybasename (const char *name)
  363. {
  364. const char *base = name;
  365. while (*name)
  366. {
  367. if (*name == '/' || *name == '\\')
  368. {
  369. base = name + 1;
  370. }
  371. ++name;
  372. }
  373. return (char *) base;
  374. }
  375. static int
  376. strhash (const char *str)
  377. {
  378. const unsigned char *s;
  379. unsigned long hash;
  380. unsigned int c;
  381. unsigned int len;
  382. hash = 0;
  383. len = 0;
  384. s = (const unsigned char *) str;
  385. while ((c = *s++) != '\0')
  386. {
  387. hash += c + (c << 17);
  388. hash ^= hash >> 2;
  389. ++len;
  390. }
  391. hash += len + (len << 17);
  392. hash ^= hash >> 2;
  393. return hash;
  394. }
  395. /**********************************************************************/
  396. static void
  397. usage (FILE *file, int status)
  398. {
  399. fprintf (file, _("Usage %s <option(s)> <object-file(s)>\n"), prog_name);
  400. fprintf (file, _(" Generic options:\n"));
  401. fprintf (file, _(" @<file> Read options from <file>\n"));
  402. fprintf (file, _(" --quiet, -q Work quietly\n"));
  403. fprintf (file, _(" --verbose, -v Verbose\n"));
  404. fprintf (file, _(" --version Print dllwrap version\n"));
  405. fprintf (file, _(" --implib <outname> Synonym for --output-lib\n"));
  406. fprintf (file, _(" Options for %s:\n"), prog_name);
  407. fprintf (file, _(" --driver-name <driver> Defaults to \"gcc\"\n"));
  408. fprintf (file, _(" --driver-flags <flags> Override default ld flags\n"));
  409. fprintf (file, _(" --dlltool-name <dlltool> Defaults to \"dlltool\"\n"));
  410. fprintf (file, _(" --entry <entry> Specify alternate DLL entry point\n"));
  411. fprintf (file, _(" --image-base <base> Specify image base address\n"));
  412. fprintf (file, _(" --target <machine> i386-cygwin32 or i386-mingw32\n"));
  413. fprintf (file, _(" --dry-run Show what needs to be run\n"));
  414. fprintf (file, _(" --mno-cygwin Create Mingw DLL\n"));
  415. fprintf (file, _(" Options passed to DLLTOOL:\n"));
  416. fprintf (file, _(" --machine <machine>\n"));
  417. fprintf (file, _(" --output-exp <outname> Generate export file.\n"));
  418. fprintf (file, _(" --output-lib <outname> Generate input library.\n"));
  419. fprintf (file, _(" --add-indirect Add dll indirects to export file.\n"));
  420. fprintf (file, _(" --dllname <name> Name of input dll to put into output lib.\n"));
  421. fprintf (file, _(" --def <deffile> Name input .def file\n"));
  422. fprintf (file, _(" --output-def <deffile> Name output .def file\n"));
  423. fprintf (file, _(" --export-all-symbols Export all symbols to .def\n"));
  424. fprintf (file, _(" --no-export-all-symbols Only export .drectve symbols\n"));
  425. fprintf (file, _(" --exclude-symbols <list> Exclude <list> from .def\n"));
  426. fprintf (file, _(" --no-default-excludes Zap default exclude symbols\n"));
  427. fprintf (file, _(" --base-file <basefile> Read linker generated base file\n"));
  428. fprintf (file, _(" --no-idata4 Don't generate idata$4 section\n"));
  429. fprintf (file, _(" --no-idata5 Don't generate idata$5 section\n"));
  430. fprintf (file, _(" -U Add underscores to .lib\n"));
  431. fprintf (file, _(" -k Kill @<n> from exported names\n"));
  432. fprintf (file, _(" --add-stdcall-alias Add aliases without @<n>\n"));
  433. fprintf (file, _(" --as <name> Use <name> for assembler\n"));
  434. fprintf (file, _(" --nodelete Keep temp files.\n"));
  435. fprintf (file, _(" --no-leading-underscore Entrypoint without underscore\n"));
  436. fprintf (file, _(" --leading-underscore Entrypoint with underscore.\n"));
  437. fprintf (file, _(" Rest are passed unmodified to the language driver\n"));
  438. fprintf (file, "\n\n");
  439. if (REPORT_BUGS_TO[0] && status == 0)
  440. fprintf (file, _("Report bugs to %s\n"), REPORT_BUGS_TO);
  441. exit (status);
  442. }
  443. #define OPTION_START 149
  444. /* GENERIC options. */
  445. #define OPTION_QUIET (OPTION_START + 1)
  446. #define OPTION_VERBOSE (OPTION_QUIET + 1)
  447. #define OPTION_VERSION (OPTION_VERBOSE + 1)
  448. /* DLLWRAP options. */
  449. #define OPTION_DRY_RUN (OPTION_VERSION + 1)
  450. #define OPTION_DRIVER_NAME (OPTION_DRY_RUN + 1)
  451. #define OPTION_DRIVER_FLAGS (OPTION_DRIVER_NAME + 1)
  452. #define OPTION_DLLTOOL_NAME (OPTION_DRIVER_FLAGS + 1)
  453. #define OPTION_ENTRY (OPTION_DLLTOOL_NAME + 1)
  454. #define OPTION_IMAGE_BASE (OPTION_ENTRY + 1)
  455. #define OPTION_TARGET (OPTION_IMAGE_BASE + 1)
  456. #define OPTION_MNO_CYGWIN (OPTION_TARGET + 1)
  457. #define OPTION_NO_LEADING_UNDERSCORE (OPTION_MNO_CYGWIN + 1)
  458. #define OPTION_LEADING_UNDERSCORE (OPTION_NO_LEADING_UNDERSCORE + 1)
  459. /* DLLTOOL options. */
  460. #define OPTION_NODELETE (OPTION_LEADING_UNDERSCORE + 1)
  461. #define OPTION_DLLNAME (OPTION_NODELETE + 1)
  462. #define OPTION_NO_IDATA4 (OPTION_DLLNAME + 1)
  463. #define OPTION_NO_IDATA5 (OPTION_NO_IDATA4 + 1)
  464. #define OPTION_OUTPUT_EXP (OPTION_NO_IDATA5 + 1)
  465. #define OPTION_OUTPUT_DEF (OPTION_OUTPUT_EXP + 1)
  466. #define OPTION_EXPORT_ALL_SYMS (OPTION_OUTPUT_DEF + 1)
  467. #define OPTION_NO_EXPORT_ALL_SYMS (OPTION_EXPORT_ALL_SYMS + 1)
  468. #define OPTION_EXCLUDE_SYMS (OPTION_NO_EXPORT_ALL_SYMS + 1)
  469. #define OPTION_NO_DEFAULT_EXCLUDES (OPTION_EXCLUDE_SYMS + 1)
  470. #define OPTION_OUTPUT_LIB (OPTION_NO_DEFAULT_EXCLUDES + 1)
  471. #define OPTION_DEF (OPTION_OUTPUT_LIB + 1)
  472. #define OPTION_ADD_UNDERSCORE (OPTION_DEF + 1)
  473. #define OPTION_KILLAT (OPTION_ADD_UNDERSCORE + 1)
  474. #define OPTION_HELP (OPTION_KILLAT + 1)
  475. #define OPTION_MACHINE (OPTION_HELP + 1)
  476. #define OPTION_ADD_INDIRECT (OPTION_MACHINE + 1)
  477. #define OPTION_BASE_FILE (OPTION_ADD_INDIRECT + 1)
  478. #define OPTION_AS (OPTION_BASE_FILE + 1)
  479. static const struct option long_options[] =
  480. {
  481. /* generic options. */
  482. {"quiet", no_argument, NULL, 'q'},
  483. {"verbose", no_argument, NULL, 'v'},
  484. {"version", no_argument, NULL, OPTION_VERSION},
  485. {"implib", required_argument, NULL, OPTION_OUTPUT_LIB},
  486. /* dllwrap options. */
  487. {"dry-run", no_argument, NULL, OPTION_DRY_RUN},
  488. {"driver-name", required_argument, NULL, OPTION_DRIVER_NAME},
  489. {"driver-flags", required_argument, NULL, OPTION_DRIVER_FLAGS},
  490. {"dlltool-name", required_argument, NULL, OPTION_DLLTOOL_NAME},
  491. {"entry", required_argument, NULL, 'e'},
  492. {"image-base", required_argument, NULL, OPTION_IMAGE_BASE},
  493. {"target", required_argument, NULL, OPTION_TARGET},
  494. {"no-leading-underscore", no_argument, NULL, OPTION_NO_LEADING_UNDERSCORE},
  495. {"leading-underscore", no_argument, NULL, OPTION_NO_LEADING_UNDERSCORE},
  496. /* dlltool options. */
  497. {"no-delete", no_argument, NULL, 'n'},
  498. {"dllname", required_argument, NULL, OPTION_DLLNAME},
  499. {"no-idata4", no_argument, NULL, OPTION_NO_IDATA4},
  500. {"no-idata5", no_argument, NULL, OPTION_NO_IDATA5},
  501. {"output-exp", required_argument, NULL, OPTION_OUTPUT_EXP},
  502. {"output-def", required_argument, NULL, OPTION_OUTPUT_DEF},
  503. {"export-all-symbols", no_argument, NULL, OPTION_EXPORT_ALL_SYMS},
  504. {"no-export-all-symbols", no_argument, NULL, OPTION_NO_EXPORT_ALL_SYMS},
  505. {"exclude-symbols", required_argument, NULL, OPTION_EXCLUDE_SYMS},
  506. {"no-default-excludes", no_argument, NULL, OPTION_NO_DEFAULT_EXCLUDES},
  507. {"output-lib", required_argument, NULL, OPTION_OUTPUT_LIB},
  508. {"def", required_argument, NULL, OPTION_DEF},
  509. {"add-underscore", no_argument, NULL, 'U'},
  510. {"killat", no_argument, NULL, 'k'},
  511. {"add-stdcall-alias", no_argument, NULL, 'A'},
  512. {"help", no_argument, NULL, 'h'},
  513. {"machine", required_argument, NULL, OPTION_MACHINE},
  514. {"add-indirect", no_argument, NULL, OPTION_ADD_INDIRECT},
  515. {"base-file", required_argument, NULL, OPTION_BASE_FILE},
  516. {"as", required_argument, NULL, OPTION_AS},
  517. {0, 0, 0, 0}
  518. };
  519. int main (int, char **);
  520. int
  521. main (int argc, char **argv)
  522. {
  523. int c;
  524. int i;
  525. char **saved_argv = 0;
  526. int cmdline_len = 0;
  527. int export_all = 0;
  528. int *dlltool_arg_indices;
  529. int *driver_arg_indices;
  530. char *driver_flags = 0;
  531. char *output_lib_file_name = 0;
  532. dyn_string_t dlltool_cmdline;
  533. dyn_string_t driver_cmdline;
  534. int def_file_seen = 0;
  535. char *image_base_str = 0;
  536. prog_name = argv[0];
  537. #ifdef HAVE_LC_MESSAGES
  538. setlocale (LC_MESSAGES, "");
  539. #endif
  540. setlocale (LC_CTYPE, "");
  541. bindtextdomain (PACKAGE, LOCALEDIR);
  542. textdomain (PACKAGE);
  543. warn (_("WARNING: %s is deprecated, use gcc -shared or ld -shared instead\n"),
  544. prog_name);
  545. expandargv (&argc, &argv);
  546. saved_argv = (char **) xmalloc (argc * sizeof (char*));
  547. dlltool_arg_indices = (int *) xmalloc (argc * sizeof (int));
  548. driver_arg_indices = (int *) xmalloc (argc * sizeof (int));
  549. for (i = 0; i < argc; ++i)
  550. {
  551. size_t len = strlen (argv[i]);
  552. char *arg = (char *) xmalloc (len + 1);
  553. strcpy (arg, argv[i]);
  554. cmdline_len += len;
  555. saved_argv[i] = arg;
  556. dlltool_arg_indices[i] = 0;
  557. driver_arg_indices[i] = 1;
  558. }
  559. cmdline_len++;
  560. /* We recognize dllwrap and dlltool options, and everything else is
  561. passed onto the language driver (eg., to GCC). We collect options
  562. to dlltool and driver in dlltool_args and driver_args. */
  563. opterr = 0;
  564. while ((c = getopt_long_only (argc, argv, "nkAqve:Uho:l:L:I:",
  565. long_options, (int *) 0)) != EOF)
  566. {
  567. int dlltool_arg;
  568. int driver_arg;
  569. int single_word_option_value_pair;
  570. dlltool_arg = 0;
  571. driver_arg = 1;
  572. single_word_option_value_pair = 0;
  573. if (c != '?')
  574. {
  575. /* We recognize this option, so it has to be either dllwrap or
  576. dlltool option. Do not pass to driver unless it's one of the
  577. generic options that are passed to all the tools (such as -v)
  578. which are dealt with later. */
  579. driver_arg = 0;
  580. }
  581. /* deal with generic and dllwrap options first. */
  582. switch (c)
  583. {
  584. case 'h':
  585. usage (stdout, 0);
  586. break;
  587. case 'q':
  588. verbose = 0;
  589. break;
  590. case 'v':
  591. verbose = 1;
  592. break;
  593. case OPTION_VERSION:
  594. print_version (prog_name);
  595. break;
  596. case 'e':
  597. entry_point = optarg;
  598. break;
  599. case OPTION_IMAGE_BASE:
  600. image_base_str = optarg;
  601. break;
  602. case OPTION_DEF:
  603. def_file_name = optarg;
  604. def_file_seen = 1;
  605. delete_def_file = 0;
  606. break;
  607. case 'n':
  608. dontdeltemps = 1;
  609. dlltool_arg = 1;
  610. break;
  611. case 'o':
  612. dll_file_name = optarg;
  613. break;
  614. case 'I':
  615. case 'l':
  616. case 'L':
  617. driver_arg = 1;
  618. break;
  619. case OPTION_DLLNAME:
  620. dll_name = optarg;
  621. break;
  622. case OPTION_DRY_RUN:
  623. dry_run = 1;
  624. break;
  625. case OPTION_DRIVER_NAME:
  626. driver_name = optarg;
  627. break;
  628. case OPTION_DRIVER_FLAGS:
  629. driver_flags = optarg;
  630. break;
  631. case OPTION_DLLTOOL_NAME:
  632. dlltool_name = optarg;
  633. break;
  634. case OPTION_TARGET:
  635. target = optarg;
  636. break;
  637. case OPTION_MNO_CYGWIN:
  638. target = "i386-mingw32";
  639. break;
  640. case OPTION_NO_LEADING_UNDERSCORE:
  641. is_leading_underscore = 0;
  642. break;
  643. case OPTION_LEADING_UNDERSCORE:
  644. is_leading_underscore = 1;
  645. break;
  646. case OPTION_BASE_FILE:
  647. base_file_name = optarg;
  648. delete_base_file = 0;
  649. break;
  650. case OPTION_OUTPUT_EXP:
  651. exp_file_name = optarg;
  652. delete_exp_file = 0;
  653. break;
  654. case OPTION_EXPORT_ALL_SYMS:
  655. export_all = 1;
  656. break;
  657. case OPTION_OUTPUT_LIB:
  658. output_lib_file_name = optarg;
  659. break;
  660. case '?':
  661. break;
  662. default:
  663. dlltool_arg = 1;
  664. break;
  665. }
  666. /* Handle passing through --option=value case. */
  667. if (optarg
  668. && saved_argv[optind-1][0] == '-'
  669. && saved_argv[optind-1][1] == '-'
  670. && strchr (saved_argv[optind-1], '='))
  671. single_word_option_value_pair = 1;
  672. if (dlltool_arg)
  673. {
  674. dlltool_arg_indices[optind-1] = 1;
  675. if (optarg && ! single_word_option_value_pair)
  676. {
  677. dlltool_arg_indices[optind-2] = 1;
  678. }
  679. }
  680. if (! driver_arg)
  681. {
  682. driver_arg_indices[optind-1] = 0;
  683. if (optarg && ! single_word_option_value_pair)
  684. {
  685. driver_arg_indices[optind-2] = 0;
  686. }
  687. }
  688. }
  689. /* Sanity checks. */
  690. if (! dll_name && ! dll_file_name)
  691. {
  692. warn (_("Must provide at least one of -o or --dllname options"));
  693. exit (1);
  694. }
  695. else if (! dll_name)
  696. {
  697. dll_name = xstrdup (mybasename (dll_file_name));
  698. }
  699. else if (! dll_file_name)
  700. {
  701. dll_file_name = xstrdup (dll_name);
  702. }
  703. /* Deduce driver-name and dlltool-name from our own. */
  704. if (driver_name == NULL)
  705. driver_name = deduce_name ("gcc");
  706. if (dlltool_name == NULL)
  707. dlltool_name = deduce_name ("dlltool");
  708. if (! def_file_seen)
  709. {
  710. char *fileprefix = choose_temp_base ();
  711. def_file_name = (char *) xmalloc (strlen (fileprefix) + 5);
  712. sprintf (def_file_name, "%s.def",
  713. (dontdeltemps) ? mybasename (fileprefix) : fileprefix);
  714. delete_def_file = 1;
  715. free (fileprefix);
  716. delete_def_file = 1;
  717. warn (_("no export definition file provided.\n\
  718. Creating one, but that may not be what you want"));
  719. }
  720. /* Set the target platform. */
  721. if (strstr (target, "cygwin"))
  722. which_target = CYGWIN_TARGET;
  723. else if (strstr (target, "mingw"))
  724. which_target = MINGW_TARGET;
  725. else
  726. which_target = UNKNOWN_TARGET;
  727. if (startswith (target, "arm"))
  728. which_cpu = ARM_CPU;
  729. else if (startswith (target, "x86_64")
  730. || startswith (target, "athlon64")
  731. || startswith (target, "amd64"))
  732. which_cpu = X64_CPU;
  733. else if (target[0] == 'i' && (target[1] >= '3' && target[1] <= '6')
  734. && target[2] == '8' && target[3] == '6')
  735. which_cpu = X86_CPU;
  736. else
  737. which_cpu = UNKNOWN_CPU;
  738. if (is_leading_underscore == -1)
  739. is_leading_underscore = (which_cpu != X64_CPU && which_cpu != ARM_CPU);
  740. /* Re-create the command lines as a string, taking care to quote stuff. */
  741. dlltool_cmdline = dyn_string_new (cmdline_len);
  742. if (verbose)
  743. dyn_string_append_cstr (dlltool_cmdline, " -v");
  744. dyn_string_append_cstr (dlltool_cmdline, " --dllname ");
  745. dyn_string_append_cstr (dlltool_cmdline, dll_name);
  746. for (i = 1; i < argc; ++i)
  747. {
  748. if (dlltool_arg_indices[i])
  749. {
  750. char *arg = saved_argv[i];
  751. int quote = (strchr (arg, ' ') || strchr (arg, '\t'));
  752. dyn_string_append_cstr (dlltool_cmdline,
  753. (quote) ? " \"" : " ");
  754. dyn_string_append_cstr (dlltool_cmdline, arg);
  755. dyn_string_append_cstr (dlltool_cmdline,
  756. (quote) ? "\"" : "");
  757. }
  758. }
  759. driver_cmdline = dyn_string_new (cmdline_len);
  760. if (! driver_flags || strlen (driver_flags) == 0)
  761. {
  762. switch (which_target)
  763. {
  764. case CYGWIN_TARGET:
  765. driver_flags = cygwin_driver_flags;
  766. break;
  767. case MINGW_TARGET:
  768. driver_flags = mingw32_driver_flags;
  769. break;
  770. default:
  771. driver_flags = generic_driver_flags;
  772. break;
  773. }
  774. }
  775. dyn_string_append_cstr (driver_cmdline, driver_flags);
  776. dyn_string_append_cstr (driver_cmdline, " -o ");
  777. dyn_string_append_cstr (driver_cmdline, dll_file_name);
  778. if (is_leading_underscore == 0)
  779. dyn_string_append_cstr (driver_cmdline, " --no-leading-underscore");
  780. else if (is_leading_underscore == 1)
  781. dyn_string_append_cstr (driver_cmdline, " --leading-underscore");
  782. if (! entry_point || strlen (entry_point) == 0)
  783. {
  784. const char *prefix = (is_leading_underscore != 0 ? "_" : "");
  785. const char *postfix = "";
  786. const char *name_entry;
  787. if (which_cpu == X86_CPU || which_cpu == UNKNOWN_CPU)
  788. postfix = "@12";
  789. switch (which_target)
  790. {
  791. case CYGWIN_TARGET:
  792. name_entry = "_cygwin_dll_entry";
  793. break;
  794. case MINGW_TARGET:
  795. name_entry = "DllMainCRTStartup";
  796. break;
  797. default:
  798. name_entry = "DllMain";
  799. break;
  800. }
  801. entry_point =
  802. (char *) malloc (strlen (name_entry) + strlen (prefix)
  803. + strlen (postfix) + 1);
  804. sprintf (entry_point, "%s%s%s", prefix, name_entry, postfix);
  805. }
  806. dyn_string_append_cstr (driver_cmdline, " -Wl,-e,");
  807. dyn_string_append_cstr (driver_cmdline, entry_point);
  808. dyn_string_append_cstr (dlltool_cmdline, " --exclude-symbol=");
  809. dyn_string_append_cstr (dlltool_cmdline,
  810. (entry_point[0] == '_') ? entry_point+1 : entry_point);
  811. if (! image_base_str || strlen (image_base_str) == 0)
  812. {
  813. char *tmpbuf = (char *) xmalloc (sizeof ("0x12345678") + 1);
  814. unsigned long hash = strhash (dll_file_name);
  815. sprintf (tmpbuf, "0x%.8lX", 0x60000000|((hash<<16)&0xFFC0000));
  816. image_base_str = tmpbuf;
  817. }
  818. dyn_string_append_cstr (driver_cmdline, " -Wl,--image-base,");
  819. dyn_string_append_cstr (driver_cmdline, image_base_str);
  820. if (verbose)
  821. {
  822. dyn_string_append_cstr (driver_cmdline, " -v");
  823. }
  824. for (i = 1; i < argc; ++i)
  825. {
  826. if (driver_arg_indices[i])
  827. {
  828. char *arg = saved_argv[i];
  829. int quote = (strchr (arg, ' ') || strchr (arg, '\t'));
  830. dyn_string_append_cstr (driver_cmdline,
  831. (quote) ? " \"" : " ");
  832. dyn_string_append_cstr (driver_cmdline, arg);
  833. dyn_string_append_cstr (driver_cmdline,
  834. (quote) ? "\"" : "");
  835. }
  836. }
  837. /* Step pre-1. If no --def <EXPORT_DEF> is specified,
  838. then create it and then pass it on. */
  839. if (! def_file_seen)
  840. {
  841. dyn_string_t step_pre1;
  842. step_pre1 = dyn_string_new (1024);
  843. dyn_string_append_cstr (step_pre1, dlltool_cmdline->s);
  844. if (export_all)
  845. {
  846. dyn_string_append_cstr (step_pre1, " --export-all --exclude-symbol=");
  847. dyn_string_append_cstr (step_pre1,
  848. "_cygwin_dll_entry@12,DllMainCRTStartup@12,DllMain@12,DllEntryPoint@12");
  849. }
  850. dyn_string_append_cstr (step_pre1, " --output-def ");
  851. dyn_string_append_cstr (step_pre1, def_file_name);
  852. for (i = 1; i < argc; ++i)
  853. {
  854. if (driver_arg_indices[i])
  855. {
  856. char *arg = saved_argv[i];
  857. size_t len = strlen (arg);
  858. if (len >= 2 && arg[len-2] == '.'
  859. && (arg[len-1] == 'o' || arg[len-1] == 'a'))
  860. {
  861. int quote = (strchr (arg, ' ') || strchr (arg, '\t'));
  862. dyn_string_append_cstr (step_pre1,
  863. (quote) ? " \"" : " ");
  864. dyn_string_append_cstr (step_pre1, arg);
  865. dyn_string_append_cstr (step_pre1,
  866. (quote) ? "\"" : "");
  867. }
  868. }
  869. }
  870. if (run (dlltool_name, step_pre1->s))
  871. cleanup_and_exit (1);
  872. dyn_string_delete (step_pre1);
  873. }
  874. dyn_string_append_cstr (dlltool_cmdline, " --def ");
  875. dyn_string_append_cstr (dlltool_cmdline, def_file_name);
  876. if (verbose)
  877. {
  878. fprintf (stderr, _("DLLTOOL name : %s\n"), dlltool_name);
  879. fprintf (stderr, _("DLLTOOL options : %s\n"), dlltool_cmdline->s);
  880. fprintf (stderr, _("DRIVER name : %s\n"), driver_name);
  881. fprintf (stderr, _("DRIVER options : %s\n"), driver_cmdline->s);
  882. }
  883. /* Step 1. Call GCC/LD to create base relocation file. If using GCC, the
  884. driver command line will look like the following:
  885. % gcc -Wl,--dll --Wl,--base-file,foo.base [rest of command line]
  886. If the user does not specify a base name, create temporary one that
  887. is deleted at exit. */
  888. if (! base_file_name)
  889. {
  890. char *fileprefix = choose_temp_base ();
  891. base_file_name = (char *) xmalloc (strlen (fileprefix) + 6);
  892. sprintf (base_file_name, "%s.base",
  893. (dontdeltemps) ? mybasename (fileprefix) : fileprefix);
  894. delete_base_file = 1;
  895. free (fileprefix);
  896. }
  897. {
  898. int quote;
  899. dyn_string_t step1 = dyn_string_new (driver_cmdline->length
  900. + strlen (base_file_name)
  901. + 20);
  902. dyn_string_append_cstr (step1, "-Wl,--base-file,");
  903. quote = (strchr (base_file_name, ' ')
  904. || strchr (base_file_name, '\t'));
  905. dyn_string_append_cstr (step1,
  906. (quote) ? "\"" : "");
  907. dyn_string_append_cstr (step1, base_file_name);
  908. dyn_string_append_cstr (step1,
  909. (quote) ? "\"" : "");
  910. if (driver_cmdline->length)
  911. {
  912. dyn_string_append_cstr (step1, " ");
  913. dyn_string_append_cstr (step1, driver_cmdline->s);
  914. }
  915. if (run (driver_name, step1->s))
  916. cleanup_and_exit (1);
  917. dyn_string_delete (step1);
  918. }
  919. /* Step 2. generate the exp file by running dlltool.
  920. dlltool command line will look like the following:
  921. % dlltool -Wl,--dll --Wl,--base-file,foo.base [rest of command line]
  922. If the user does not specify a base name, create temporary one that
  923. is deleted at exit. */
  924. if (! exp_file_name)
  925. {
  926. char *p = strrchr (dll_name, '.');
  927. size_t prefix_len = (p) ? (size_t) (p - dll_name) : strlen (dll_name);
  928. exp_file_name = (char *) xmalloc (prefix_len + 4 + 1);
  929. strncpy (exp_file_name, dll_name, prefix_len);
  930. exp_file_name[prefix_len] = '\0';
  931. strcat (exp_file_name, ".exp");
  932. delete_exp_file = 1;
  933. }
  934. {
  935. int quote;
  936. dyn_string_t step2 = dyn_string_new (dlltool_cmdline->length
  937. + strlen (base_file_name)
  938. + strlen (exp_file_name)
  939. + 20);
  940. dyn_string_append_cstr (step2, "--base-file ");
  941. quote = (strchr (base_file_name, ' ')
  942. || strchr (base_file_name, '\t'));
  943. dyn_string_append_cstr (step2,
  944. (quote) ? "\"" : "");
  945. dyn_string_append_cstr (step2, base_file_name);
  946. dyn_string_append_cstr (step2,
  947. (quote) ? "\" " : " ");
  948. dyn_string_append_cstr (step2, "--output-exp ");
  949. quote = (strchr (exp_file_name, ' ')
  950. || strchr (exp_file_name, '\t'));
  951. dyn_string_append_cstr (step2,
  952. (quote) ? "\"" : "");
  953. dyn_string_append_cstr (step2, exp_file_name);
  954. dyn_string_append_cstr (step2,
  955. (quote) ? "\"" : "");
  956. if (dlltool_cmdline->length)
  957. {
  958. dyn_string_append_cstr (step2, " ");
  959. dyn_string_append_cstr (step2, dlltool_cmdline->s);
  960. }
  961. if (run (dlltool_name, step2->s))
  962. cleanup_and_exit (1);
  963. dyn_string_delete (step2);
  964. }
  965. /*
  966. * Step 3. Call GCC/LD to again, adding the exp file this time.
  967. * driver command line will look like the following:
  968. *
  969. * % gcc -Wl,--dll --Wl,--base-file,foo.base foo.exp [rest ...]
  970. */
  971. {
  972. int quote;
  973. dyn_string_t step3 = dyn_string_new (driver_cmdline->length
  974. + strlen (exp_file_name)
  975. + strlen (base_file_name)
  976. + 20);
  977. dyn_string_append_cstr (step3, "-Wl,--base-file,");
  978. quote = (strchr (base_file_name, ' ')
  979. || strchr (base_file_name, '\t'));
  980. dyn_string_append_cstr (step3,
  981. (quote) ? "\"" : "");
  982. dyn_string_append_cstr (step3, base_file_name);
  983. dyn_string_append_cstr (step3,
  984. (quote) ? "\" " : " ");
  985. quote = (strchr (exp_file_name, ' ')
  986. || strchr (exp_file_name, '\t'));
  987. dyn_string_append_cstr (step3,
  988. (quote) ? "\"" : "");
  989. dyn_string_append_cstr (step3, exp_file_name);
  990. dyn_string_append_cstr (step3,
  991. (quote) ? "\"" : "");
  992. if (driver_cmdline->length)
  993. {
  994. dyn_string_append_cstr (step3, " ");
  995. dyn_string_append_cstr (step3, driver_cmdline->s);
  996. }
  997. if (run (driver_name, step3->s))
  998. cleanup_and_exit (1);
  999. dyn_string_delete (step3);
  1000. }
  1001. /*
  1002. * Step 4. Run DLLTOOL again using the same command line.
  1003. */
  1004. {
  1005. int quote;
  1006. dyn_string_t step4 = dyn_string_new (dlltool_cmdline->length
  1007. + strlen (base_file_name)
  1008. + strlen (exp_file_name)
  1009. + 20);
  1010. dyn_string_append_cstr (step4, "--base-file ");
  1011. quote = (strchr (base_file_name, ' ')
  1012. || strchr (base_file_name, '\t'));
  1013. dyn_string_append_cstr (step4,
  1014. (quote) ? "\"" : "");
  1015. dyn_string_append_cstr (step4, base_file_name);
  1016. dyn_string_append_cstr (step4,
  1017. (quote) ? "\" " : " ");
  1018. dyn_string_append_cstr (step4, "--output-exp ");
  1019. quote = (strchr (exp_file_name, ' ')
  1020. || strchr (exp_file_name, '\t'));
  1021. dyn_string_append_cstr (step4,
  1022. (quote) ? "\"" : "");
  1023. dyn_string_append_cstr (step4, exp_file_name);
  1024. dyn_string_append_cstr (step4,
  1025. (quote) ? "\"" : "");
  1026. if (dlltool_cmdline->length)
  1027. {
  1028. dyn_string_append_cstr (step4, " ");
  1029. dyn_string_append_cstr (step4, dlltool_cmdline->s);
  1030. }
  1031. if (output_lib_file_name)
  1032. {
  1033. dyn_string_append_cstr (step4, " --output-lib ");
  1034. dyn_string_append_cstr (step4, output_lib_file_name);
  1035. }
  1036. if (run (dlltool_name, step4->s))
  1037. cleanup_and_exit (1);
  1038. dyn_string_delete (step4);
  1039. }
  1040. /*
  1041. * Step 5. Link it all together and be done with it.
  1042. * driver command line will look like the following:
  1043. *
  1044. * % gcc -Wl,--dll foo.exp [rest ...]
  1045. *
  1046. */
  1047. {
  1048. int quote;
  1049. dyn_string_t step5 = dyn_string_new (driver_cmdline->length
  1050. + strlen (exp_file_name)
  1051. + 20);
  1052. quote = (strchr (exp_file_name, ' ')
  1053. || strchr (exp_file_name, '\t'));
  1054. dyn_string_append_cstr (step5,
  1055. (quote) ? "\"" : "");
  1056. dyn_string_append_cstr (step5, exp_file_name);
  1057. dyn_string_append_cstr (step5,
  1058. (quote) ? "\"" : "");
  1059. if (driver_cmdline->length)
  1060. {
  1061. dyn_string_append_cstr (step5, " ");
  1062. dyn_string_append_cstr (step5, driver_cmdline->s);
  1063. }
  1064. if (run (driver_name, step5->s))
  1065. cleanup_and_exit (1);
  1066. dyn_string_delete (step5);
  1067. }
  1068. cleanup_and_exit (0);
  1069. return 0;
  1070. }