windres.c 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415
  1. /* windres.c -- a program to manipulate Windows resources
  2. Copyright (C) 1997-2022 Free Software Foundation, Inc.
  3. Written by Ian Lance Taylor, Cygnus Support.
  4. Rewritten by Kai Tietz, Onevision.
  5. This file is part of GNU Binutils.
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 3 of the License, or
  9. (at your option) any later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program; if not, write to the Free Software
  16. Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
  17. 02110-1301, USA. */
  18. /* This program can read and write Windows resources in various
  19. formats. In particular, it can act like the rc resource compiler
  20. program, and it can act like the cvtres res to COFF conversion
  21. program.
  22. It is based on information taken from the following sources:
  23. * Microsoft documentation.
  24. * The rcl program, written by Gunther Ebert
  25. <gunther.ebert@ixos-leipzig.de>.
  26. * The res2coff program, written by Pedro A. Aranda <paag@tid.es>. */
  27. #include "sysdep.h"
  28. #include <assert.h>
  29. #include "bfd.h"
  30. #include "getopt.h"
  31. #include "bucomm.h"
  32. #include "libiberty.h"
  33. #include "safe-ctype.h"
  34. #include "obstack.h"
  35. #include "windres.h"
  36. /* Used by resrc.c at least. */
  37. int verbose = 0;
  38. bool target_is_bigendian = 0;
  39. const char *def_target_arch;
  40. static void set_endianness (bfd *, const char *);
  41. /* An enumeration of format types. */
  42. enum res_format
  43. {
  44. /* Unknown format. */
  45. RES_FORMAT_UNKNOWN,
  46. /* Textual RC file. */
  47. RES_FORMAT_RC,
  48. /* Binary RES file. */
  49. RES_FORMAT_RES,
  50. /* COFF file. */
  51. RES_FORMAT_COFF
  52. };
  53. /* A structure used to map between format types and strings. */
  54. struct format_map
  55. {
  56. const char *name;
  57. enum res_format format;
  58. };
  59. /* A mapping between names and format types. */
  60. static const struct format_map format_names[] =
  61. {
  62. { "rc", RES_FORMAT_RC },
  63. { "res", RES_FORMAT_RES },
  64. { "coff", RES_FORMAT_COFF },
  65. { NULL, RES_FORMAT_UNKNOWN }
  66. };
  67. /* A mapping from file extensions to format types. */
  68. static const struct format_map format_fileexts[] =
  69. {
  70. { "rc", RES_FORMAT_RC },
  71. { "res", RES_FORMAT_RES },
  72. { "exe", RES_FORMAT_COFF },
  73. { "obj", RES_FORMAT_COFF },
  74. { "o", RES_FORMAT_COFF },
  75. { NULL, RES_FORMAT_UNKNOWN }
  76. };
  77. /* A list of include directories. */
  78. struct include_dir
  79. {
  80. struct include_dir *next;
  81. char *dir;
  82. };
  83. static struct include_dir *include_dirs;
  84. /* Static functions. */
  85. static void res_init (void);
  86. static int extended_menuitems (const rc_menuitem *);
  87. static enum res_format format_from_name (const char *, int);
  88. static enum res_format format_from_filename (const char *, int);
  89. static void usage (FILE *, int);
  90. static int cmp_res_entry (const void *, const void *);
  91. static rc_res_directory *sort_resources (rc_res_directory *);
  92. static void reswr_init (void);
  93. static const char * quot (const char *);
  94. static rc_uint_type target_get_8 (const void *, rc_uint_type);
  95. static void target_put_8 (void *, rc_uint_type);
  96. static rc_uint_type target_get_16 (const void *, rc_uint_type);
  97. static void target_put_16 (void *, rc_uint_type);
  98. static rc_uint_type target_get_32 (const void *, rc_uint_type);
  99. static void target_put_32 (void *, rc_uint_type);
  100. /* When we are building a resource tree, we allocate everything onto
  101. an obstack, so that we can free it all at once if we want. */
  102. #define obstack_chunk_alloc xmalloc
  103. #define obstack_chunk_free free
  104. /* The resource building obstack. */
  105. static struct obstack res_obstack;
  106. /* Initialize the resource building obstack. */
  107. static void
  108. res_init (void)
  109. {
  110. obstack_init (&res_obstack);
  111. }
  112. /* Allocate space on the resource building obstack. */
  113. void *
  114. res_alloc (rc_uint_type bytes)
  115. {
  116. return obstack_alloc (&res_obstack, (size_t) bytes);
  117. }
  118. /* We also use an obstack to save memory used while writing out a set
  119. of resources. */
  120. static struct obstack reswr_obstack;
  121. /* Initialize the resource writing obstack. */
  122. static void
  123. reswr_init (void)
  124. {
  125. obstack_init (&reswr_obstack);
  126. }
  127. /* Allocate space on the resource writing obstack. */
  128. void *
  129. reswr_alloc (rc_uint_type bytes)
  130. {
  131. return obstack_alloc (&reswr_obstack, (size_t) bytes);
  132. }
  133. /* Open a file using the include directory search list. */
  134. FILE *
  135. open_file_search (const char *filename, const char *mode, const char *errmsg,
  136. char **real_filename)
  137. {
  138. FILE *e;
  139. struct include_dir *d;
  140. e = fopen (filename, mode);
  141. if (e != NULL)
  142. {
  143. *real_filename = xstrdup (filename);
  144. return e;
  145. }
  146. if (errno == ENOENT)
  147. {
  148. for (d = include_dirs; d != NULL; d = d->next)
  149. {
  150. char *n;
  151. n = (char *) xmalloc (strlen (d->dir) + strlen (filename) + 2);
  152. sprintf (n, "%s/%s", d->dir, filename);
  153. e = fopen (n, mode);
  154. if (e != NULL)
  155. {
  156. *real_filename = n;
  157. return e;
  158. }
  159. free (n);
  160. if (errno != ENOENT)
  161. break;
  162. }
  163. }
  164. fatal (_("can't open %s `%s': %s"), errmsg, filename, strerror (errno));
  165. /* Return a value to avoid a compiler warning. */
  166. return NULL;
  167. }
  168. /* Compare two resource ID's. We consider name entries to come before
  169. numeric entries, because that is how they appear in the COFF .rsrc
  170. section. */
  171. int
  172. res_id_cmp (rc_res_id a, rc_res_id b)
  173. {
  174. if (! a.named)
  175. {
  176. if (b.named)
  177. return 1;
  178. if (a.u.id > b.u.id)
  179. return 1;
  180. else if (a.u.id < b.u.id)
  181. return -1;
  182. else
  183. return 0;
  184. }
  185. else
  186. {
  187. unichar *as, *ase, *bs, *bse;
  188. if (! b.named)
  189. return -1;
  190. as = a.u.n.name;
  191. ase = as + a.u.n.length;
  192. bs = b.u.n.name;
  193. bse = bs + b.u.n.length;
  194. while (as < ase)
  195. {
  196. int i;
  197. if (bs >= bse)
  198. return 1;
  199. i = (int) *as - (int) *bs;
  200. if (i != 0)
  201. return i;
  202. ++as;
  203. ++bs;
  204. }
  205. if (bs < bse)
  206. return -1;
  207. return 0;
  208. }
  209. }
  210. /* Print a resource ID. */
  211. void
  212. res_id_print (FILE *stream, rc_res_id id, int quote)
  213. {
  214. if (! id.named)
  215. fprintf (stream, "%u", (int) id.u.id);
  216. else
  217. {
  218. if (quote)
  219. unicode_print_quoted (stream, id.u.n.name, id.u.n.length);
  220. else
  221. unicode_print (stream, id.u.n.name, id.u.n.length);
  222. }
  223. }
  224. /* Print a list of resource ID's. */
  225. void
  226. res_ids_print (FILE *stream, int cids, const rc_res_id *ids)
  227. {
  228. int i;
  229. for (i = 0; i < cids; i++)
  230. {
  231. res_id_print (stream, ids[i], 1);
  232. if (i + 1 < cids)
  233. fprintf (stream, ": ");
  234. }
  235. }
  236. /* Convert an ASCII string to a resource ID. */
  237. void
  238. res_string_to_id (rc_res_id *res_id, const char *string)
  239. {
  240. res_id->named = 1;
  241. unicode_from_ascii (&res_id->u.n.length, &res_id->u.n.name, string);
  242. }
  243. /* Convert an unicode string to a resource ID. */
  244. void
  245. res_unistring_to_id (rc_res_id *res_id, const unichar *u)
  246. {
  247. res_id->named = 1;
  248. res_id->u.n.length = unichar_len (u);
  249. res_id->u.n.name = unichar_dup_uppercase (u);
  250. }
  251. /* Define a resource. The arguments are the resource tree, RESOURCES,
  252. and the location at which to put it in the tree, CIDS and IDS.
  253. This returns a newly allocated rc_res_resource structure, which the
  254. caller is expected to initialize. If DUPOK is non-zero, then if a
  255. resource with this ID exists, it is returned. Otherwise, a warning
  256. is issued, and a new resource is created replacing the existing
  257. one. */
  258. rc_res_resource *
  259. define_resource (rc_res_directory **resources, int cids,
  260. const rc_res_id *ids, int dupok)
  261. {
  262. rc_res_entry *re = NULL;
  263. int i;
  264. assert (cids > 0);
  265. for (i = 0; i < cids; i++)
  266. {
  267. rc_res_entry **pp;
  268. if (*resources == NULL)
  269. {
  270. *resources = ((rc_res_directory *)
  271. res_alloc (sizeof (rc_res_directory)));
  272. (*resources)->characteristics = 0;
  273. /* Using a real timestamp only serves to create non-deterministic
  274. results. Use zero instead. */
  275. (*resources)->time = 0;
  276. (*resources)->major = 0;
  277. (*resources)->minor = 0;
  278. (*resources)->entries = NULL;
  279. }
  280. for (pp = &(*resources)->entries; *pp != NULL; pp = &(*pp)->next)
  281. if (res_id_cmp ((*pp)->id, ids[i]) == 0)
  282. break;
  283. if (*pp != NULL)
  284. re = *pp;
  285. else
  286. {
  287. re = (rc_res_entry *) res_alloc (sizeof (rc_res_entry));
  288. re->next = NULL;
  289. re->id = ids[i];
  290. if ((i + 1) < cids)
  291. {
  292. re->subdir = 1;
  293. re->u.dir = NULL;
  294. }
  295. else
  296. {
  297. re->subdir = 0;
  298. re->u.res = NULL;
  299. }
  300. *pp = re;
  301. }
  302. if ((i + 1) < cids)
  303. {
  304. if (! re->subdir)
  305. {
  306. fprintf (stderr, "%s: ", program_name);
  307. res_ids_print (stderr, i, ids);
  308. fprintf (stderr, _(": expected to be a directory\n"));
  309. xexit (1);
  310. }
  311. resources = &re->u.dir;
  312. }
  313. }
  314. if (re->subdir)
  315. {
  316. fprintf (stderr, "%s: ", program_name);
  317. res_ids_print (stderr, cids, ids);
  318. fprintf (stderr, _(": expected to be a leaf\n"));
  319. xexit (1);
  320. }
  321. if (re->u.res != NULL)
  322. {
  323. if (dupok)
  324. return re->u.res;
  325. fprintf (stderr, _("%s: warning: "), program_name);
  326. res_ids_print (stderr, cids, ids);
  327. fprintf (stderr, _(": duplicate value\n"));
  328. }
  329. re->u.res = ((rc_res_resource *)
  330. res_alloc (sizeof (rc_res_resource)));
  331. memset (re->u.res, 0, sizeof (rc_res_resource));
  332. re->u.res->type = RES_TYPE_UNINITIALIZED;
  333. return re->u.res;
  334. }
  335. /* Define a standard resource. This is a version of define_resource
  336. that just takes type, name, and language arguments. */
  337. rc_res_resource *
  338. define_standard_resource (rc_res_directory **resources, int type,
  339. rc_res_id name, rc_uint_type language, int dupok)
  340. {
  341. rc_res_id a[3];
  342. a[0].named = 0;
  343. a[0].u.id = type;
  344. a[1] = name;
  345. a[2].named = 0;
  346. a[2].u.id = language;
  347. return define_resource (resources, 3, a, dupok);
  348. }
  349. /* Comparison routine for resource sorting. */
  350. static int
  351. cmp_res_entry (const void *p1, const void *p2)
  352. {
  353. const rc_res_entry **re1, **re2;
  354. re1 = (const rc_res_entry **) p1;
  355. re2 = (const rc_res_entry **) p2;
  356. return res_id_cmp ((*re1)->id, (*re2)->id);
  357. }
  358. /* Sort the resources. */
  359. static rc_res_directory *
  360. sort_resources (rc_res_directory *resdir)
  361. {
  362. int c, i;
  363. rc_res_entry *re;
  364. rc_res_entry **a;
  365. if (resdir->entries == NULL)
  366. return resdir;
  367. c = 0;
  368. for (re = resdir->entries; re != NULL; re = re->next)
  369. ++c;
  370. /* This is a recursive routine, so using xmalloc is probably better
  371. than alloca. */
  372. a = (rc_res_entry **) xmalloc (c * sizeof (rc_res_entry *));
  373. for (i = 0, re = resdir->entries; re != NULL; re = re->next, i++)
  374. a[i] = re;
  375. qsort (a, c, sizeof (rc_res_entry *), cmp_res_entry);
  376. resdir->entries = a[0];
  377. for (i = 0; i < c - 1; i++)
  378. a[i]->next = a[i + 1];
  379. a[i]->next = NULL;
  380. free (a);
  381. /* Now sort the subdirectories. */
  382. for (re = resdir->entries; re != NULL; re = re->next)
  383. if (re->subdir)
  384. re->u.dir = sort_resources (re->u.dir);
  385. return resdir;
  386. }
  387. /* Return whether the dialog resource DIALOG is a DIALOG or a
  388. DIALOGEX. */
  389. int
  390. extended_dialog (const rc_dialog *dialog)
  391. {
  392. const rc_dialog_control *c;
  393. if (dialog->ex != NULL)
  394. return 1;
  395. for (c = dialog->controls; c != NULL; c = c->next)
  396. if (c->data != NULL || c->help != 0)
  397. return 1;
  398. return 0;
  399. }
  400. /* Return whether MENUITEMS are a MENU or a MENUEX. */
  401. int
  402. extended_menu (const rc_menu *menu)
  403. {
  404. return extended_menuitems (menu->items);
  405. }
  406. static int
  407. extended_menuitems (const rc_menuitem *menuitems)
  408. {
  409. const rc_menuitem *mi;
  410. for (mi = menuitems; mi != NULL; mi = mi->next)
  411. {
  412. if (mi->help != 0 || mi->state != 0)
  413. return 1;
  414. if (mi->popup != NULL && mi->id != 0)
  415. return 1;
  416. if ((mi->type
  417. & ~ (MENUITEM_CHECKED
  418. | MENUITEM_GRAYED
  419. | MENUITEM_HELP
  420. | MENUITEM_INACTIVE
  421. | MENUITEM_MENUBARBREAK
  422. | MENUITEM_BITMAP
  423. | MENUITEM_OWNERDRAW
  424. | MENUITEM_MENUBREAK))
  425. != 0)
  426. return 1;
  427. if (mi->popup != NULL)
  428. {
  429. if (extended_menuitems (mi->popup))
  430. return 1;
  431. }
  432. }
  433. return 0;
  434. }
  435. /* Convert a string to a format type, or exit if it can't be done. */
  436. static enum res_format
  437. format_from_name (const char *name, int exit_on_error)
  438. {
  439. const struct format_map *m;
  440. for (m = format_names; m->name != NULL; m++)
  441. if (strcasecmp (m->name, name) == 0)
  442. break;
  443. if (m->name == NULL && exit_on_error)
  444. {
  445. non_fatal (_("unknown format type `%s'"), name);
  446. fprintf (stderr, _("%s: supported formats:"), program_name);
  447. for (m = format_names; m->name != NULL; m++)
  448. fprintf (stderr, " %s", m->name);
  449. fprintf (stderr, "\n");
  450. xexit (1);
  451. }
  452. return m->format;
  453. }
  454. /* Work out a format type given a file name. If INPUT is non-zero,
  455. it's OK to look at the file itself. */
  456. static enum res_format
  457. format_from_filename (const char *filename, int input)
  458. {
  459. const char *ext;
  460. FILE *e;
  461. bfd_byte b1, b2, b3, b4, b5;
  462. int magic;
  463. /* If we have an extension, see if we recognize it as implying a
  464. particular format. */
  465. ext = strrchr (filename, '.');
  466. if (ext != NULL)
  467. {
  468. const struct format_map *m;
  469. ++ext;
  470. for (m = format_fileexts; m->name != NULL; m++)
  471. if (strcasecmp (m->name, ext) == 0)
  472. return m->format;
  473. }
  474. /* If we don't recognize the name of an output file, assume it's a
  475. COFF file. */
  476. if (! input)
  477. return RES_FORMAT_COFF;
  478. /* Read the first few bytes of the file to see if we can guess what
  479. it is. */
  480. e = fopen (filename, FOPEN_RB);
  481. if (e == NULL)
  482. fatal ("%s: %s", filename, strerror (errno));
  483. b1 = getc (e);
  484. b2 = getc (e);
  485. b3 = getc (e);
  486. b4 = getc (e);
  487. b5 = getc (e);
  488. fclose (e);
  489. /* A PE executable starts with 0x4d 0x5a. */
  490. if (b1 == 0x4d && b2 == 0x5a)
  491. return RES_FORMAT_COFF;
  492. /* A COFF .o file starts with a COFF magic number. */
  493. magic = (b2 << 8) | b1;
  494. switch (magic)
  495. {
  496. case 0x14c: /* i386 */
  497. case 0x166: /* MIPS */
  498. case 0x184: /* Alpha */
  499. case 0x268: /* 68k */
  500. case 0x1f0: /* PowerPC */
  501. case 0x290: /* PA */
  502. return RES_FORMAT_COFF;
  503. }
  504. /* A RES file starts with 0x0 0x0 0x0 0x0 0x20 0x0 0x0 0x0. */
  505. if (b1 == 0 && b2 == 0 && b3 == 0 && b4 == 0 && b5 == 0x20)
  506. return RES_FORMAT_RES;
  507. /* If every character is printable or space, assume it's an RC file. */
  508. if ((ISPRINT (b1) || ISSPACE (b1))
  509. && (ISPRINT (b2) || ISSPACE (b2))
  510. && (ISPRINT (b3) || ISSPACE (b3))
  511. && (ISPRINT (b4) || ISSPACE (b4))
  512. && (ISPRINT (b5) || ISSPACE (b5)))
  513. return RES_FORMAT_RC;
  514. /* Otherwise, we give up. */
  515. fatal (_("can not determine type of file `%s'; use the -J option"),
  516. filename);
  517. /* Return something to silence the compiler warning. */
  518. return RES_FORMAT_UNKNOWN;
  519. }
  520. /* Print a usage message and exit. */
  521. static void
  522. usage (FILE *stream, int status)
  523. {
  524. fprintf (stream, _("Usage: %s [option(s)] [input-file] [output-file]\n"),
  525. program_name);
  526. fprintf (stream, _(" The options are:\n\
  527. -i --input=<file> Name input file\n\
  528. -o --output=<file> Name output file\n\
  529. -J --input-format=<format> Specify input format\n\
  530. -O --output-format=<format> Specify output format\n\
  531. -F --target=<target> Specify COFF target\n\
  532. --preprocessor=<program> Program to use to preprocess rc file\n\
  533. --preprocessor-arg=<arg> Additional preprocessor argument\n\
  534. -I --include-dir=<dir> Include directory when preprocessing rc file\n\
  535. -D --define <sym>[=<val>] Define SYM when preprocessing rc file\n\
  536. -U --undefine <sym> Undefine SYM when preprocessing rc file\n\
  537. -v --verbose Verbose - tells you what it's doing\n\
  538. -c --codepage=<codepage> Specify default codepage\n\
  539. -l --language=<val> Set language when reading rc file\n\
  540. --use-temp-file Use a temporary file instead of popen to read\n\
  541. the preprocessor output\n\
  542. --no-use-temp-file Use popen (default)\n"));
  543. #ifdef YYDEBUG
  544. fprintf (stream, _("\
  545. --yydebug Turn on parser debugging\n"));
  546. #endif
  547. fprintf (stream, _("\
  548. -r Ignored for compatibility with rc\n\
  549. @<file> Read options from <file>\n\
  550. -h --help Print this help message\n\
  551. -V --version Print version information\n"));
  552. fprintf (stream, _("\
  553. FORMAT is one of rc, res, or coff, and is deduced from the file name\n\
  554. extension if not specified. A single file name is an input file.\n\
  555. No input-file is stdin, default rc. No output-file is stdout, default rc.\n"));
  556. list_supported_targets (program_name, stream);
  557. if (REPORT_BUGS_TO[0] && status == 0)
  558. fprintf (stream, _("Report bugs to %s\n"), REPORT_BUGS_TO);
  559. exit (status);
  560. }
  561. /* Quote characters that will confuse the shell when we run the preprocessor. */
  562. static const char *
  563. quot (const char *string)
  564. {
  565. static char *buf = 0;
  566. static int buflen = 0;
  567. int slen = strlen (string);
  568. const char *src;
  569. char *dest;
  570. if ((buflen < slen * 2 + 3) || ! buf)
  571. {
  572. buflen = slen * 2 + 3;
  573. free (buf);
  574. buf = (char *) xmalloc (buflen);
  575. }
  576. for (src = string, dest = buf; *src; src++, dest++)
  577. {
  578. if (*src == '(' || *src == ')' || *src == ' ')
  579. *dest++ = '\\';
  580. *dest = *src;
  581. }
  582. *dest = 0;
  583. return buf;
  584. }
  585. /* Long options. */
  586. enum option_values
  587. {
  588. /* 150 isn't special; it's just an arbitrary non-ASCII char value. */
  589. OPTION_PREPROCESSOR = 150,
  590. OPTION_USE_TEMP_FILE,
  591. OPTION_NO_USE_TEMP_FILE,
  592. OPTION_YYDEBUG,
  593. OPTION_INCLUDE_DIR,
  594. OPTION_PREPROCESSOR_ARG
  595. };
  596. static const struct option long_options[] =
  597. {
  598. {"input", required_argument, 0, 'i'},
  599. {"output", required_argument, 0, 'o'},
  600. {"input-format", required_argument, 0, 'J'},
  601. {"output-format", required_argument, 0, 'O'},
  602. {"target", required_argument, 0, 'F'},
  603. {"preprocessor", required_argument, 0, OPTION_PREPROCESSOR},
  604. {"preprocessor-arg", required_argument, 0, OPTION_PREPROCESSOR_ARG},
  605. {"include-dir", required_argument, 0, OPTION_INCLUDE_DIR},
  606. {"define", required_argument, 0, 'D'},
  607. {"undefine", required_argument, 0, 'U'},
  608. {"verbose", no_argument, 0, 'v'},
  609. {"codepage", required_argument, 0, 'c'},
  610. {"language", required_argument, 0, 'l'},
  611. {"use-temp-file", no_argument, 0, OPTION_USE_TEMP_FILE},
  612. {"no-use-temp-file", no_argument, 0, OPTION_NO_USE_TEMP_FILE},
  613. {"yydebug", no_argument, 0, OPTION_YYDEBUG},
  614. {"version", no_argument, 0, 'V'},
  615. {"help", no_argument, 0, 'h'},
  616. {0, no_argument, 0, 0}
  617. };
  618. void
  619. windres_add_include_dir (const char *p)
  620. {
  621. struct include_dir *n, **pp;
  622. /* Computing paths is often complicated and error prone.
  623. The easiest way to check for mistakes is at the time
  624. we add them to include_dirs. */
  625. assert (p != NULL);
  626. assert (*p != '\0');
  627. n = xmalloc (sizeof *n);
  628. n->next = NULL;
  629. n->dir = (char * ) p;
  630. for (pp = &include_dirs; *pp != NULL; pp = &(*pp)->next)
  631. ;
  632. *pp = n;
  633. }
  634. /* This keeps gcc happy when using -Wmissing-prototypes -Wstrict-prototypes. */
  635. int main (int, char **);
  636. /* The main function. */
  637. int
  638. main (int argc, char **argv)
  639. {
  640. int c;
  641. char *input_filename;
  642. char *output_filename;
  643. enum res_format input_format;
  644. enum res_format input_format_tmp;
  645. enum res_format output_format;
  646. char *target;
  647. char *preprocessor;
  648. char *preprocargs;
  649. const char *quotedarg;
  650. int language;
  651. rc_res_directory *resources;
  652. int use_temp_file;
  653. #ifdef HAVE_LC_MESSAGES
  654. setlocale (LC_MESSAGES, "");
  655. #endif
  656. setlocale (LC_CTYPE, "");
  657. bindtextdomain (PACKAGE, LOCALEDIR);
  658. textdomain (PACKAGE);
  659. program_name = argv[0];
  660. xmalloc_set_program_name (program_name);
  661. bfd_set_error_program_name (program_name);
  662. expandargv (&argc, &argv);
  663. if (bfd_init () != BFD_INIT_MAGIC)
  664. fatal (_("fatal error: libbfd ABI mismatch"));
  665. set_default_bfd_target ();
  666. res_init ();
  667. input_filename = NULL;
  668. output_filename = NULL;
  669. input_format = RES_FORMAT_UNKNOWN;
  670. output_format = RES_FORMAT_UNKNOWN;
  671. target = NULL;
  672. preprocessor = NULL;
  673. preprocargs = NULL;
  674. language = 0x409; /* LANG_ENGLISH, SUBLANG_ENGLISH_US. */
  675. use_temp_file = 0;
  676. while ((c = getopt_long (argc, argv, "c:f:i:l:o:I:J:O:F:D:U:rhHvV", long_options,
  677. (int *) 0)) != EOF)
  678. {
  679. switch (c)
  680. {
  681. case 'c':
  682. {
  683. rc_uint_type ncp;
  684. if (optarg[0] == '0' && (optarg[1] == 'x' || optarg[1] == 'X'))
  685. ncp = (rc_uint_type) strtol (optarg + 2, NULL, 16);
  686. else
  687. ncp = (rc_uint_type) strtol (optarg, NULL, 10);
  688. if (ncp == CP_UTF16 || ! unicode_is_valid_codepage (ncp))
  689. fatal (_("invalid codepage specified.\n"));
  690. wind_default_codepage = wind_current_codepage = ncp;
  691. }
  692. break;
  693. case 'i':
  694. input_filename = optarg;
  695. break;
  696. case 'f':
  697. /* For compatibility with rc we accept "-fo <name>" as being the
  698. equivalent of "-o <name>". We do not advertise this fact
  699. though, as we do not want users to use non-GNU like command
  700. line switches. */
  701. if (*optarg != 'o')
  702. fatal (_("invalid option -f\n"));
  703. optarg++;
  704. if (* optarg == 0)
  705. {
  706. if (optind == argc)
  707. fatal (_("No filename following the -fo option.\n"));
  708. optarg = argv [optind++];
  709. }
  710. /* Fall through. */
  711. case 'o':
  712. output_filename = optarg;
  713. break;
  714. case 'J':
  715. input_format = format_from_name (optarg, 1);
  716. break;
  717. case 'O':
  718. output_format = format_from_name (optarg, 1);
  719. break;
  720. case 'F':
  721. target = optarg;
  722. break;
  723. case OPTION_PREPROCESSOR:
  724. if (strchr (optarg, ' '))
  725. {
  726. if (asprintf (& preprocessor, "\"%s\"", optarg) == -1)
  727. preprocessor = optarg;
  728. }
  729. else
  730. preprocessor = optarg;
  731. break;
  732. case OPTION_PREPROCESSOR_ARG:
  733. if (preprocargs == NULL)
  734. {
  735. quotedarg = quot (optarg);
  736. preprocargs = xstrdup (quotedarg);
  737. }
  738. else
  739. {
  740. char *n;
  741. quotedarg = quot (optarg);
  742. n = xmalloc (strlen (preprocargs) + strlen (quotedarg) + 2);
  743. sprintf (n, "%s %s", preprocargs, quotedarg);
  744. free (preprocargs);
  745. preprocargs = n;
  746. }
  747. break;
  748. case 'D':
  749. case 'U':
  750. if (preprocargs == NULL)
  751. {
  752. quotedarg = quot (optarg);
  753. preprocargs = xmalloc (strlen (quotedarg) + 3);
  754. sprintf (preprocargs, "-%c%s", c, quotedarg);
  755. }
  756. else
  757. {
  758. char *n;
  759. quotedarg = quot (optarg);
  760. n = xmalloc (strlen (preprocargs) + strlen (quotedarg) + 4);
  761. sprintf (n, "%s -%c%s", preprocargs, c, quotedarg);
  762. free (preprocargs);
  763. preprocargs = n;
  764. }
  765. break;
  766. case 'r':
  767. /* Ignored for compatibility with rc. */
  768. break;
  769. case 'v':
  770. verbose ++;
  771. break;
  772. case 'I':
  773. /* For backward compatibility, should be removed in the future. */
  774. input_format_tmp = format_from_name (optarg, 0);
  775. if (input_format_tmp != RES_FORMAT_UNKNOWN)
  776. {
  777. struct stat statbuf;
  778. char modebuf[11];
  779. if (stat (optarg, & statbuf) == 0
  780. /* Coded this way to avoid importing knowledge of S_ISDIR into this file. */
  781. && (mode_string (statbuf.st_mode, modebuf), modebuf[0] == 'd'))
  782. /* We have a -I option with a directory name that just happens
  783. to match a format name as well. eg: -I res Assume that the
  784. user knows what they are doing and do not complain. */
  785. ;
  786. else
  787. {
  788. fprintf (stderr,
  789. _("Option -I is deprecated for setting the input format, please use -J instead.\n"));
  790. input_format = input_format_tmp;
  791. break;
  792. }
  793. }
  794. /* Fall through. */
  795. case OPTION_INCLUDE_DIR:
  796. if (preprocargs == NULL)
  797. {
  798. quotedarg = quot (optarg);
  799. preprocargs = xmalloc (strlen (quotedarg) + 3);
  800. sprintf (preprocargs, "-I%s", quotedarg);
  801. }
  802. else
  803. {
  804. char *n;
  805. quotedarg = quot (optarg);
  806. n = xmalloc (strlen (preprocargs) + strlen (quotedarg) + 4);
  807. sprintf (n, "%s -I%s", preprocargs, quotedarg);
  808. free (preprocargs);
  809. preprocargs = n;
  810. }
  811. windres_add_include_dir (optarg);
  812. break;
  813. case 'l':
  814. language = strtol (optarg, (char **) NULL, 16);
  815. break;
  816. case OPTION_USE_TEMP_FILE:
  817. use_temp_file = 1;
  818. break;
  819. case OPTION_NO_USE_TEMP_FILE:
  820. use_temp_file = 0;
  821. break;
  822. #ifdef YYDEBUG
  823. case OPTION_YYDEBUG:
  824. yydebug = 1;
  825. break;
  826. #endif
  827. case 'h':
  828. case 'H':
  829. usage (stdout, 0);
  830. break;
  831. case 'V':
  832. print_version ("windres");
  833. break;
  834. default:
  835. usage (stderr, 1);
  836. break;
  837. }
  838. }
  839. if (input_filename == NULL && optind < argc)
  840. {
  841. input_filename = argv[optind];
  842. ++optind;
  843. }
  844. if (output_filename == NULL && optind < argc)
  845. {
  846. output_filename = argv[optind];
  847. ++optind;
  848. }
  849. if (argc != optind)
  850. usage (stderr, 1);
  851. if (input_format == RES_FORMAT_UNKNOWN)
  852. {
  853. if (input_filename == NULL)
  854. input_format = RES_FORMAT_RC;
  855. else
  856. input_format = format_from_filename (input_filename, 1);
  857. }
  858. if (output_format == RES_FORMAT_UNKNOWN)
  859. {
  860. if (output_filename == NULL)
  861. output_format = RES_FORMAT_RC;
  862. else
  863. output_format = format_from_filename (output_filename, 0);
  864. }
  865. set_endianness (NULL, target);
  866. /* Read the input file. */
  867. switch (input_format)
  868. {
  869. default:
  870. abort ();
  871. case RES_FORMAT_RC:
  872. resources = read_rc_file (input_filename, preprocessor, preprocargs,
  873. language, use_temp_file);
  874. break;
  875. case RES_FORMAT_RES:
  876. resources = read_res_file (input_filename);
  877. break;
  878. case RES_FORMAT_COFF:
  879. resources = read_coff_rsrc (input_filename, target);
  880. break;
  881. }
  882. if (resources == NULL)
  883. fatal (_("no resources"));
  884. /* Sort the resources. This is required for COFF, convenient for
  885. rc, and unimportant for res. */
  886. resources = sort_resources (resources);
  887. /* Write the output file. */
  888. reswr_init ();
  889. switch (output_format)
  890. {
  891. default:
  892. abort ();
  893. case RES_FORMAT_RC:
  894. write_rc_file (output_filename, resources);
  895. break;
  896. case RES_FORMAT_RES:
  897. write_res_file (output_filename, resources);
  898. break;
  899. case RES_FORMAT_COFF:
  900. write_coff_file (output_filename, target, resources);
  901. break;
  902. }
  903. xexit (0);
  904. return 0;
  905. }
  906. static void
  907. set_endianness (bfd *abfd, const char *target)
  908. {
  909. const bfd_target *target_vec;
  910. def_target_arch = NULL;
  911. target_vec = bfd_get_target_info (target, abfd, &target_is_bigendian, NULL,
  912. &def_target_arch);
  913. if (! target_vec)
  914. fatal ("Can't detect target endianness and architecture.");
  915. if (! def_target_arch)
  916. fatal ("Can't detect architecture.");
  917. }
  918. bfd *
  919. windres_open_as_binary (const char *filename, int rdmode)
  920. {
  921. bfd *abfd;
  922. abfd = (rdmode ? bfd_openr (filename, "binary") : bfd_openw (filename, "binary"));
  923. if (! abfd)
  924. fatal ("can't open `%s' for %s", filename, (rdmode ? "input" : "output"));
  925. if (rdmode && ! bfd_check_format (abfd, bfd_object))
  926. fatal ("can't open `%s' for input.", filename);
  927. return abfd;
  928. }
  929. void
  930. set_windres_bfd_endianness (windres_bfd *wrbfd, int is_bigendian)
  931. {
  932. assert (!! wrbfd);
  933. switch (WR_KIND(wrbfd))
  934. {
  935. case WR_KIND_BFD_BIN_L:
  936. if (is_bigendian)
  937. WR_KIND(wrbfd) = WR_KIND_BFD_BIN_B;
  938. break;
  939. case WR_KIND_BFD_BIN_B:
  940. if (! is_bigendian)
  941. WR_KIND(wrbfd) = WR_KIND_BFD_BIN_L;
  942. break;
  943. default:
  944. /* only binary bfd can be overriden. */
  945. abort ();
  946. }
  947. }
  948. void
  949. set_windres_bfd (windres_bfd *wrbfd, bfd *abfd, asection *sec, rc_uint_type kind)
  950. {
  951. assert (!! wrbfd);
  952. switch (kind)
  953. {
  954. case WR_KIND_TARGET:
  955. abfd = NULL;
  956. sec = NULL;
  957. break;
  958. case WR_KIND_BFD:
  959. case WR_KIND_BFD_BIN_L:
  960. case WR_KIND_BFD_BIN_B:
  961. assert (!! abfd);
  962. assert (!!sec);
  963. break;
  964. default:
  965. abort ();
  966. }
  967. WR_KIND(wrbfd) = kind;
  968. WR_BFD(wrbfd) = abfd;
  969. WR_SECTION(wrbfd) = sec;
  970. }
  971. void
  972. set_windres_bfd_content (windres_bfd *wrbfd, const void *data, rc_uint_type off,
  973. rc_uint_type length)
  974. {
  975. if (WR_KIND(wrbfd) != WR_KIND_TARGET)
  976. {
  977. if (! bfd_set_section_contents (WR_BFD(wrbfd), WR_SECTION(wrbfd), data, off, length))
  978. bfd_fatal ("bfd_set_section_contents");
  979. }
  980. else
  981. abort ();
  982. }
  983. void
  984. get_windres_bfd_content (windres_bfd *wrbfd, void *data, rc_uint_type off,
  985. rc_uint_type length)
  986. {
  987. if (WR_KIND(wrbfd) != WR_KIND_TARGET)
  988. {
  989. if (! bfd_get_section_contents (WR_BFD(wrbfd), WR_SECTION(wrbfd), data, off, length))
  990. bfd_fatal ("bfd_get_section_contents");
  991. }
  992. else
  993. abort ();
  994. }
  995. void
  996. windres_put_8 (windres_bfd *wrbfd, void *p, rc_uint_type value)
  997. {
  998. switch (WR_KIND(wrbfd))
  999. {
  1000. case WR_KIND_TARGET:
  1001. target_put_8 (p, value);
  1002. break;
  1003. case WR_KIND_BFD:
  1004. case WR_KIND_BFD_BIN_L:
  1005. case WR_KIND_BFD_BIN_B:
  1006. bfd_put_8 (WR_BFD(wrbfd), value, p);
  1007. break;
  1008. default:
  1009. abort ();
  1010. }
  1011. }
  1012. void
  1013. windres_put_16 (windres_bfd *wrbfd, void *data, rc_uint_type value)
  1014. {
  1015. switch (WR_KIND(wrbfd))
  1016. {
  1017. case WR_KIND_TARGET:
  1018. target_put_16 (data, value);
  1019. break;
  1020. case WR_KIND_BFD:
  1021. case WR_KIND_BFD_BIN_B:
  1022. bfd_put_16 (WR_BFD(wrbfd), value, data);
  1023. break;
  1024. case WR_KIND_BFD_BIN_L:
  1025. bfd_putl16 (value, data);
  1026. break;
  1027. default:
  1028. abort ();
  1029. }
  1030. }
  1031. void
  1032. windres_put_32 (windres_bfd *wrbfd, void *data, rc_uint_type value)
  1033. {
  1034. switch (WR_KIND(wrbfd))
  1035. {
  1036. case WR_KIND_TARGET:
  1037. target_put_32 (data, value);
  1038. break;
  1039. case WR_KIND_BFD:
  1040. case WR_KIND_BFD_BIN_B:
  1041. bfd_put_32 (WR_BFD(wrbfd), value, data);
  1042. break;
  1043. case WR_KIND_BFD_BIN_L:
  1044. bfd_putl32 (value, data);
  1045. break;
  1046. default:
  1047. abort ();
  1048. }
  1049. }
  1050. rc_uint_type
  1051. windres_get_8 (windres_bfd *wrbfd, const void *data, rc_uint_type length)
  1052. {
  1053. if (length < 1)
  1054. fatal ("windres_get_8: unexpected eob.");
  1055. switch (WR_KIND(wrbfd))
  1056. {
  1057. case WR_KIND_TARGET:
  1058. return target_get_8 (data, length);
  1059. case WR_KIND_BFD:
  1060. case WR_KIND_BFD_BIN_B:
  1061. case WR_KIND_BFD_BIN_L:
  1062. return bfd_get_8 (WR_BFD(wrbfd), data);
  1063. default:
  1064. abort ();
  1065. }
  1066. return 0;
  1067. }
  1068. rc_uint_type
  1069. windres_get_16 (windres_bfd *wrbfd, const void *data, rc_uint_type length)
  1070. {
  1071. if (length < 2)
  1072. fatal ("windres_get_16: unexpected eob.");
  1073. switch (WR_KIND(wrbfd))
  1074. {
  1075. case WR_KIND_TARGET:
  1076. return target_get_16 (data, length);
  1077. case WR_KIND_BFD:
  1078. case WR_KIND_BFD_BIN_B:
  1079. return bfd_get_16 (WR_BFD(wrbfd), data);
  1080. case WR_KIND_BFD_BIN_L:
  1081. return bfd_getl16 (data);
  1082. default:
  1083. abort ();
  1084. }
  1085. return 0;
  1086. }
  1087. rc_uint_type
  1088. windres_get_32 (windres_bfd *wrbfd, const void *data, rc_uint_type length)
  1089. {
  1090. if (length < 4)
  1091. fatal ("windres_get_32: unexpected eob.");
  1092. switch (WR_KIND(wrbfd))
  1093. {
  1094. case WR_KIND_TARGET:
  1095. return target_get_32 (data, length);
  1096. case WR_KIND_BFD:
  1097. case WR_KIND_BFD_BIN_B:
  1098. return bfd_get_32 (WR_BFD(wrbfd), data);
  1099. case WR_KIND_BFD_BIN_L:
  1100. return bfd_getl32 (data);
  1101. default:
  1102. abort ();
  1103. }
  1104. return 0;
  1105. }
  1106. static rc_uint_type
  1107. target_get_8 (const void *p, rc_uint_type length)
  1108. {
  1109. rc_uint_type ret;
  1110. if (length < 1)
  1111. fatal ("Resource too small for getting 8-bit value.");
  1112. ret = (rc_uint_type) *((const bfd_byte *) p);
  1113. return ret & 0xff;
  1114. }
  1115. static rc_uint_type
  1116. target_get_16 (const void *p, rc_uint_type length)
  1117. {
  1118. if (length < 2)
  1119. fatal ("Resource too small for getting 16-bit value.");
  1120. if (target_is_bigendian)
  1121. return bfd_getb16 (p);
  1122. else
  1123. return bfd_getl16 (p);
  1124. }
  1125. static rc_uint_type
  1126. target_get_32 (const void *p, rc_uint_type length)
  1127. {
  1128. if (length < 4)
  1129. fatal ("Resource too small for getting 32-bit value.");
  1130. if (target_is_bigendian)
  1131. return bfd_getb32 (p);
  1132. else
  1133. return bfd_getl32 (p);
  1134. }
  1135. static void
  1136. target_put_8 (void *p, rc_uint_type value)
  1137. {
  1138. assert (!! p);
  1139. *((bfd_byte *) p)=(bfd_byte) value;
  1140. }
  1141. static void
  1142. target_put_16 (void *p, rc_uint_type value)
  1143. {
  1144. assert (!! p);
  1145. if (target_is_bigendian)
  1146. bfd_putb16 (value, p);
  1147. else
  1148. bfd_putl16 (value, p);
  1149. }
  1150. static void
  1151. target_put_32 (void *p, rc_uint_type value)
  1152. {
  1153. assert (!! p);
  1154. if (target_is_bigendian)
  1155. bfd_putb32 (value, p);
  1156. else
  1157. bfd_putl32 (value, p);
  1158. }
  1159. static int isInComment = 0;
  1160. int wr_printcomment (FILE *e, const char *fmt, ...)
  1161. {
  1162. va_list arg;
  1163. int r = 0;
  1164. if (isInComment)
  1165. r += fprintf (e, "\n ");
  1166. else
  1167. fprintf (e, "/* ");
  1168. isInComment = 1;
  1169. if (fmt == NULL)
  1170. return r;
  1171. va_start (arg, fmt);
  1172. r += vfprintf (e, fmt, arg);
  1173. va_end (arg);
  1174. return r;
  1175. }
  1176. int wr_print (FILE *e, const char *fmt, ...)
  1177. {
  1178. va_list arg;
  1179. int r = 0;
  1180. if (isInComment)
  1181. r += fprintf (e, ". */\n");
  1182. isInComment = 0;
  1183. if (! fmt)
  1184. return r;
  1185. va_start (arg, fmt);
  1186. r += vfprintf (e, fmt, arg);
  1187. va_end (arg);
  1188. return r;
  1189. }