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