1 /* windres.c -- a program to manipulate Windows resources
2 Copyright (C) 1997-2017 Free Software Foundation, Inc.
3 Written by Ian Lance Taylor, Cygnus Support.
4 Rewritten by Kai Tietz, Onevision.
6 This file is part of GNU Binutils.
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.
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.
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
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
28 It is based on information taken from the following sources:
30 * Microsoft documentation.
32 * The rcl program, written by Gunther Ebert
33 <gunther.ebert@ixos-leipzig.de>.
35 * The res2coff program, written by Pedro A. Aranda <paag@tid.es>. */
42 #include "libiberty.h"
43 #include "safe-ctype.h"
47 /* Used by resrc.c at least. */
51 int target_is_bigendian
= 0;
52 const char *def_target_arch
;
54 static void set_endianness (bfd
*, const char *);
56 /* An enumeration of format types. */
62 /* Textual RC file. */
64 /* Binary RES file. */
70 /* A structure used to map between format types and strings. */
75 enum res_format format
;
78 /* A mapping between names and format types. */
80 static const struct format_map format_names
[] =
82 { "rc", RES_FORMAT_RC
},
83 { "res", RES_FORMAT_RES
},
84 { "coff", RES_FORMAT_COFF
},
85 { NULL
, RES_FORMAT_UNKNOWN
}
88 /* A mapping from file extensions to format types. */
90 static const struct format_map format_fileexts
[] =
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
}
100 /* A list of include directories. */
104 struct include_dir
*next
;
108 static struct include_dir
*include_dirs
;
110 /* Static functions. */
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 *);
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
);
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. */
133 #define obstack_chunk_alloc xmalloc
134 #define obstack_chunk_free free
136 /* The resource building obstack. */
138 static struct obstack res_obstack
;
140 /* Initialize the resource building obstack. */
145 obstack_init (&res_obstack
);
148 /* Allocate space on the resource building obstack. */
151 res_alloc (rc_uint_type bytes
)
153 return obstack_alloc (&res_obstack
, (size_t) bytes
);
156 /* We also use an obstack to save memory used while writing out a set
159 static struct obstack reswr_obstack
;
161 /* Initialize the resource writing obstack. */
166 obstack_init (&reswr_obstack
);
169 /* Allocate space on the resource writing obstack. */
172 reswr_alloc (rc_uint_type bytes
)
174 return obstack_alloc (&reswr_obstack
, (size_t) bytes
);
177 /* Open a file using the include directory search list. */
180 open_file_search (const char *filename
, const char *mode
, const char *errmsg
,
181 char **real_filename
)
184 struct include_dir
*d
;
186 e
= fopen (filename
, mode
);
189 *real_filename
= xstrdup (filename
);
195 for (d
= include_dirs
; d
!= NULL
; d
= d
->next
)
199 n
= (char *) xmalloc (strlen (d
->dir
) + strlen (filename
) + 2);
200 sprintf (n
, "%s/%s", d
->dir
, filename
);
214 fatal (_("can't open %s `%s': %s"), errmsg
, filename
, strerror (errno
));
216 /* Return a value to avoid a compiler warning. */
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
225 res_id_cmp (rc_res_id a
, rc_res_id b
)
233 else if (a
.u
.id
< b
.u
.id
)
240 unichar
*as
, *ase
, *bs
, *bse
;
246 ase
= as
+ a
.u
.n
.length
;
248 bse
= bs
+ b
.u
.n
.length
;
256 i
= (int) *as
- (int) *bs
;
270 /* Print a resource ID. */
273 res_id_print (FILE *stream
, rc_res_id id
, int quote
)
276 fprintf (stream
, "%u", (int) id
.u
.id
);
280 unicode_print_quoted (stream
, id
.u
.n
.name
, id
.u
.n
.length
);
282 unicode_print (stream
, id
.u
.n
.name
, id
.u
.n
.length
);
286 /* Print a list of resource ID's. */
289 res_ids_print (FILE *stream
, int cids
, const rc_res_id
*ids
)
293 for (i
= 0; i
< cids
; i
++)
295 res_id_print (stream
, ids
[i
], 1);
297 fprintf (stream
, ": ");
301 /* Convert an ASCII string to a resource ID. */
304 res_string_to_id (rc_res_id
*res_id
, const char *string
)
307 unicode_from_ascii (&res_id
->u
.n
.length
, &res_id
->u
.n
.name
, string
);
310 /* Convert an unicode string to a resource ID. */
312 res_unistring_to_id (rc_res_id
*res_id
, const unichar
*u
)
315 res_id
->u
.n
.length
= unichar_len (u
);
316 res_id
->u
.n
.name
= unichar_dup_uppercase (u
);
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
328 define_resource (rc_res_directory
**resources
, int cids
,
329 const rc_res_id
*ids
, int dupok
)
331 rc_res_entry
*re
= NULL
;
335 for (i
= 0; i
< cids
; i
++)
339 if (*resources
== NULL
)
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
;
352 for (pp
= &(*resources
)->entries
; *pp
!= NULL
; pp
= &(*pp
)->next
)
353 if (res_id_cmp ((*pp
)->id
, ids
[i
]) == 0)
360 re
= (rc_res_entry
*) res_alloc (sizeof (rc_res_entry
));
381 fprintf (stderr
, "%s: ", program_name
);
382 res_ids_print (stderr
, i
, ids
);
383 fprintf (stderr
, _(": expected to be a directory\n"));
387 resources
= &re
->u
.dir
;
393 fprintf (stderr
, "%s: ", program_name
);
394 res_ids_print (stderr
, cids
, ids
);
395 fprintf (stderr
, _(": expected to be a leaf\n"));
399 if (re
->u
.res
!= NULL
)
404 fprintf (stderr
, _("%s: warning: "), program_name
);
405 res_ids_print (stderr
, cids
, ids
);
406 fprintf (stderr
, _(": duplicate value\n"));
409 re
->u
.res
= ((rc_res_resource
*)
410 res_alloc (sizeof (rc_res_resource
)));
411 memset (re
->u
.res
, 0, sizeof (rc_res_resource
));
413 re
->u
.res
->type
= RES_TYPE_UNINITIALIZED
;
417 /* Define a standard resource. This is a version of define_resource
418 that just takes type, name, and language arguments. */
421 define_standard_resource (rc_res_directory
**resources
, int type
,
422 rc_res_id name
, rc_uint_type language
, int dupok
)
430 a
[2].u
.id
= language
;
431 return define_resource (resources
, 3, a
, dupok
);
434 /* Comparison routine for resource sorting. */
437 cmp_res_entry (const void *p1
, const void *p2
)
439 const rc_res_entry
**re1
, **re2
;
441 re1
= (const rc_res_entry
**) p1
;
442 re2
= (const rc_res_entry
**) p2
;
443 return res_id_cmp ((*re1
)->id
, (*re2
)->id
);
446 /* Sort the resources. */
448 static rc_res_directory
*
449 sort_resources (rc_res_directory
*resdir
)
455 if (resdir
->entries
== NULL
)
459 for (re
= resdir
->entries
; re
!= NULL
; re
= re
->next
)
462 /* This is a recursive routine, so using xmalloc is probably better
464 a
= (rc_res_entry
**) xmalloc (c
* sizeof (rc_res_entry
*));
466 for (i
= 0, re
= resdir
->entries
; re
!= NULL
; re
= re
->next
, i
++)
469 qsort (a
, c
, sizeof (rc_res_entry
*), cmp_res_entry
);
471 resdir
->entries
= a
[0];
472 for (i
= 0; i
< c
- 1; i
++)
473 a
[i
]->next
= a
[i
+ 1];
478 /* Now sort the subdirectories. */
480 for (re
= resdir
->entries
; re
!= NULL
; re
= re
->next
)
482 re
->u
.dir
= sort_resources (re
->u
.dir
);
487 /* Return whether the dialog resource DIALOG is a DIALOG or a
491 extended_dialog (const rc_dialog
*dialog
)
493 const rc_dialog_control
*c
;
495 if (dialog
->ex
!= NULL
)
498 for (c
= dialog
->controls
; c
!= NULL
; c
= c
->next
)
499 if (c
->data
!= NULL
|| c
->help
!= 0)
505 /* Return whether MENUITEMS are a MENU or a MENUEX. */
508 extended_menu (const rc_menu
*menu
)
510 return extended_menuitems (menu
->items
);
514 extended_menuitems (const rc_menuitem
*menuitems
)
516 const rc_menuitem
*mi
;
518 for (mi
= menuitems
; mi
!= NULL
; mi
= mi
->next
)
520 if (mi
->help
!= 0 || mi
->state
!= 0)
522 if (mi
->popup
!= NULL
&& mi
->id
!= 0)
525 & ~ (MENUITEM_CHECKED
529 | MENUITEM_MENUBARBREAK
530 | MENUITEM_MENUBREAK
))
533 if (mi
->popup
!= NULL
)
535 if (extended_menuitems (mi
->popup
))
543 /* Convert a string to a format type, or exit if it can't be done. */
545 static enum res_format
546 format_from_name (const char *name
, int exit_on_error
)
548 const struct format_map
*m
;
550 for (m
= format_names
; m
->name
!= NULL
; m
++)
551 if (strcasecmp (m
->name
, name
) == 0)
554 if (m
->name
== NULL
&& exit_on_error
)
556 non_fatal (_("unknown format type `%s'"), name
);
557 fprintf (stderr
, _("%s: supported formats:"), program_name
);
558 for (m
= format_names
; m
->name
!= NULL
; m
++)
559 fprintf (stderr
, " %s", m
->name
);
560 fprintf (stderr
, "\n");
567 /* Work out a format type given a file name. If INPUT is non-zero,
568 it's OK to look at the file itself. */
570 static enum res_format
571 format_from_filename (const char *filename
, int input
)
575 bfd_byte b1
, b2
, b3
, b4
, b5
;
578 /* If we have an extension, see if we recognize it as implying a
579 particular format. */
580 ext
= strrchr (filename
, '.');
583 const struct format_map
*m
;
586 for (m
= format_fileexts
; m
->name
!= NULL
; m
++)
587 if (strcasecmp (m
->name
, ext
) == 0)
591 /* If we don't recognize the name of an output file, assume it's a
594 return RES_FORMAT_COFF
;
596 /* Read the first few bytes of the file to see if we can guess what
598 e
= fopen (filename
, FOPEN_RB
);
600 fatal ("%s: %s", filename
, strerror (errno
));
610 /* A PE executable starts with 0x4d 0x5a. */
611 if (b1
== 0x4d && b2
== 0x5a)
612 return RES_FORMAT_COFF
;
614 /* A COFF .o file starts with a COFF magic number. */
615 magic
= (b2
<< 8) | b1
;
618 case 0x14c: /* i386 */
619 case 0x166: /* MIPS */
620 case 0x184: /* Alpha */
621 case 0x268: /* 68k */
622 case 0x1f0: /* PowerPC */
624 return RES_FORMAT_COFF
;
627 /* A RES file starts with 0x0 0x0 0x0 0x0 0x20 0x0 0x0 0x0. */
628 if (b1
== 0 && b2
== 0 && b3
== 0 && b4
== 0 && b5
== 0x20)
629 return RES_FORMAT_RES
;
631 /* If every character is printable or space, assume it's an RC file. */
632 if ((ISPRINT (b1
) || ISSPACE (b1
))
633 && (ISPRINT (b2
) || ISSPACE (b2
))
634 && (ISPRINT (b3
) || ISSPACE (b3
))
635 && (ISPRINT (b4
) || ISSPACE (b4
))
636 && (ISPRINT (b5
) || ISSPACE (b5
)))
637 return RES_FORMAT_RC
;
639 /* Otherwise, we give up. */
640 fatal (_("can not determine type of file `%s'; use the -J option"),
643 /* Return something to silence the compiler warning. */
644 return RES_FORMAT_UNKNOWN
;
647 /* Print a usage message and exit. */
650 usage (FILE *stream
, int status
)
652 fprintf (stream
, _("Usage: %s [option(s)] [input-file] [output-file]\n"),
654 fprintf (stream
, _(" The options are:\n\
655 -i --input=<file> Name input file\n\
656 -o --output=<file> Name output file\n\
657 -J --input-format=<format> Specify input format\n\
658 -O --output-format=<format> Specify output format\n\
659 -F --target=<target> Specify COFF target\n\
660 --preprocessor=<program> Program to use to preprocess rc file\n\
661 --preprocessor-arg=<arg> Additional preprocessor argument\n\
662 -I --include-dir=<dir> Include directory when preprocessing rc file\n\
663 -D --define <sym>[=<val>] Define SYM when preprocessing rc file\n\
664 -U --undefine <sym> Undefine SYM when preprocessing rc file\n\
665 -v --verbose Verbose - tells you what it's doing\n\
666 -c --codepage=<codepage> Specify default codepage\n\
667 -l --language=<val> Set language when reading rc file\n\
668 --use-temp-file Use a temporary file instead of popen to read\n\
669 the preprocessor output\n\
670 --no-use-temp-file Use popen (default)\n"));
672 fprintf (stream
, _("\
673 --yydebug Turn on parser debugging\n"));
675 fprintf (stream
, _("\
676 -r Ignored for compatibility with rc\n\
677 @<file> Read options from <file>\n\
678 -h --help Print this help message\n\
679 -V --version Print version information\n"));
680 fprintf (stream
, _("\
681 FORMAT is one of rc, res, or coff, and is deduced from the file name\n\
682 extension if not specified. A single file name is an input file.\n\
683 No input-file is stdin, default rc. No output-file is stdout, default rc.\n"));
685 list_supported_targets (program_name
, stream
);
687 if (REPORT_BUGS_TO
[0] && status
== 0)
688 fprintf (stream
, _("Report bugs to %s\n"), REPORT_BUGS_TO
);
693 /* Quote characters that will confuse the shell when we run the preprocessor. */
696 quot (const char *string
)
698 static char *buf
= 0;
699 static int buflen
= 0;
700 int slen
= strlen (string
);
704 if ((buflen
< slen
* 2 + 2) || ! buf
)
706 buflen
= slen
* 2 + 2;
709 buf
= (char *) xmalloc (buflen
);
712 for (src
=string
, dest
=buf
; *src
; src
++, dest
++)
714 if (*src
== '(' || *src
== ')' || *src
== ' ')
726 /* 150 isn't special; it's just an arbitrary non-ASCII char value. */
727 OPTION_PREPROCESSOR
= 150,
728 OPTION_USE_TEMP_FILE
,
729 OPTION_NO_USE_TEMP_FILE
,
732 OPTION_PREPROCESSOR_ARG
735 static const struct option long_options
[] =
737 {"input", required_argument
, 0, 'i'},
738 {"output", required_argument
, 0, 'o'},
739 {"input-format", required_argument
, 0, 'J'},
740 {"output-format", required_argument
, 0, 'O'},
741 {"target", required_argument
, 0, 'F'},
742 {"preprocessor", required_argument
, 0, OPTION_PREPROCESSOR
},
743 {"preprocessor-arg", required_argument
, 0, OPTION_PREPROCESSOR_ARG
},
744 {"include-dir", required_argument
, 0, OPTION_INCLUDE_DIR
},
745 {"define", required_argument
, 0, 'D'},
746 {"undefine", required_argument
, 0, 'U'},
747 {"verbose", no_argument
, 0, 'v'},
748 {"codepage", required_argument
, 0, 'c'},
749 {"language", required_argument
, 0, 'l'},
750 {"use-temp-file", no_argument
, 0, OPTION_USE_TEMP_FILE
},
751 {"no-use-temp-file", no_argument
, 0, OPTION_NO_USE_TEMP_FILE
},
752 {"yydebug", no_argument
, 0, OPTION_YYDEBUG
},
753 {"version", no_argument
, 0, 'V'},
754 {"help", no_argument
, 0, 'h'},
755 {0, no_argument
, 0, 0}
759 windres_add_include_dir (const char *p
)
761 struct include_dir
*n
, **pp
;
763 /* Computing paths is often complicated and error prone.
764 The easiest way to check for mistakes is at the time
765 we add them to include_dirs. */
769 n
= xmalloc (sizeof *n
);
771 n
->dir
= (char * ) p
;
773 for (pp
= &include_dirs
; *pp
!= NULL
; pp
= &(*pp
)->next
)
778 /* This keeps gcc happy when using -Wmissing-prototypes -Wstrict-prototypes. */
779 int main (int, char **);
781 /* The main function. */
784 main (int argc
, char **argv
)
787 char *input_filename
;
788 char *output_filename
;
789 enum res_format input_format
;
790 enum res_format input_format_tmp
;
791 enum res_format output_format
;
795 const char *quotedarg
;
797 rc_res_directory
*resources
;
800 #if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
801 setlocale (LC_MESSAGES
, "");
803 #if defined (HAVE_SETLOCALE)
804 setlocale (LC_CTYPE
, "");
806 bindtextdomain (PACKAGE
, LOCALEDIR
);
807 textdomain (PACKAGE
);
809 program_name
= argv
[0];
810 xmalloc_set_program_name (program_name
);
811 bfd_set_error_program_name (program_name
);
813 expandargv (&argc
, &argv
);
816 set_default_bfd_target ();
820 input_filename
= NULL
;
821 output_filename
= NULL
;
822 input_format
= RES_FORMAT_UNKNOWN
;
823 output_format
= RES_FORMAT_UNKNOWN
;
827 language
= 0x409; /* LANG_ENGLISH, SUBLANG_ENGLISH_US. */
830 while ((c
= getopt_long (argc
, argv
, "c:f:i:l:o:I:J:O:F:D:U:rhHvV", long_options
,
839 if (optarg
[0] == '0' && (optarg
[1] == 'x' || optarg
[1] == 'X'))
840 ncp
= (rc_uint_type
) strtol (optarg
+ 2, NULL
, 16);
842 ncp
= (rc_uint_type
) strtol (optarg
, NULL
, 10);
843 if (ncp
== CP_UTF16
|| ! unicode_is_valid_codepage (ncp
))
844 fatal (_("invalid codepage specified.\n"));
845 wind_default_codepage
= wind_current_codepage
= ncp
;
850 input_filename
= optarg
;
854 /* For compatibility with rc we accept "-fo <name>" as being the
855 equivalent of "-o <name>". We do not advertise this fact
856 though, as we do not want users to use non-GNU like command
859 fatal (_("invalid option -f\n"));
864 fatal (_("No filename following the -fo option.\n"));
865 optarg
= argv
[optind
++];
870 output_filename
= optarg
;
874 input_format
= format_from_name (optarg
, 1);
878 output_format
= format_from_name (optarg
, 1);
885 case OPTION_PREPROCESSOR
:
886 preprocessor
= optarg
;
889 case OPTION_PREPROCESSOR_ARG
:
890 if (preprocargs
== NULL
)
892 quotedarg
= quot (optarg
);
893 preprocargs
= xstrdup (quotedarg
);
899 quotedarg
= quot (optarg
);
900 n
= xmalloc (strlen (preprocargs
) + strlen (quotedarg
) + 2);
901 sprintf (n
, "%s %s", preprocargs
, quotedarg
);
909 if (preprocargs
== NULL
)
911 quotedarg
= quot (optarg
);
912 preprocargs
= xmalloc (strlen (quotedarg
) + 3);
913 sprintf (preprocargs
, "-%c%s", c
, quotedarg
);
919 quotedarg
= quot (optarg
);
920 n
= xmalloc (strlen (preprocargs
) + strlen (quotedarg
) + 4);
921 sprintf (n
, "%s -%c%s", preprocargs
, c
, quotedarg
);
928 /* Ignored for compatibility with rc. */
936 /* For backward compatibility, should be removed in the future. */
937 input_format_tmp
= format_from_name (optarg
, 0);
938 if (input_format_tmp
!= RES_FORMAT_UNKNOWN
)
943 if (stat (optarg
, & statbuf
) == 0
944 /* Coded this way to avoid importing knowledge of S_ISDIR into this file. */
945 && (mode_string (statbuf
.st_mode
, modebuf
), modebuf
[0] == 'd'))
946 /* We have a -I option with a directory name that just happens
947 to match a format name as well. eg: -I res Assume that the
948 user knows what they are doing and do not complain. */
953 _("Option -I is deprecated for setting the input format, please use -J instead.\n"));
954 input_format
= input_format_tmp
;
960 case OPTION_INCLUDE_DIR
:
961 if (preprocargs
== NULL
)
963 quotedarg
= quot (optarg
);
964 preprocargs
= xmalloc (strlen (quotedarg
) + 3);
965 sprintf (preprocargs
, "-I%s", quotedarg
);
971 quotedarg
= quot (optarg
);
972 n
= xmalloc (strlen (preprocargs
) + strlen (quotedarg
) + 4);
973 sprintf (n
, "%s -I%s", preprocargs
, quotedarg
);
978 windres_add_include_dir (optarg
);
983 language
= strtol (optarg
, (char **) NULL
, 16);
986 case OPTION_USE_TEMP_FILE
:
990 case OPTION_NO_USE_TEMP_FILE
:
1006 print_version ("windres");
1015 if (input_filename
== NULL
&& optind
< argc
)
1017 input_filename
= argv
[optind
];
1021 if (output_filename
== NULL
&& optind
< argc
)
1023 output_filename
= argv
[optind
];
1030 if (input_format
== RES_FORMAT_UNKNOWN
)
1032 if (input_filename
== NULL
)
1033 input_format
= RES_FORMAT_RC
;
1035 input_format
= format_from_filename (input_filename
, 1);
1038 if (output_format
== RES_FORMAT_UNKNOWN
)
1040 if (output_filename
== NULL
)
1041 output_format
= RES_FORMAT_RC
;
1043 output_format
= format_from_filename (output_filename
, 0);
1046 set_endianness (NULL
, target
);
1048 /* Read the input file. */
1049 switch (input_format
)
1054 resources
= read_rc_file (input_filename
, preprocessor
, preprocargs
,
1055 language
, use_temp_file
);
1057 case RES_FORMAT_RES
:
1058 resources
= read_res_file (input_filename
);
1060 case RES_FORMAT_COFF
:
1061 resources
= read_coff_rsrc (input_filename
, target
);
1065 if (resources
== NULL
)
1066 fatal (_("no resources"));
1068 /* Sort the resources. This is required for COFF, convenient for
1069 rc, and unimportant for res. */
1070 resources
= sort_resources (resources
);
1072 /* Write the output file. */
1075 switch (output_format
)
1080 write_rc_file (output_filename
, resources
);
1082 case RES_FORMAT_RES
:
1083 write_res_file (output_filename
, resources
);
1085 case RES_FORMAT_COFF
:
1086 write_coff_file (output_filename
, target
, resources
);
1095 set_endianness (bfd
*abfd
, const char *target
)
1097 const bfd_target
*target_vec
;
1099 def_target_arch
= NULL
;
1100 target_vec
= bfd_get_target_info (target
, abfd
, &target_is_bigendian
, NULL
,
1103 fatal ("Can't detect target endianness and architecture.");
1104 if (! def_target_arch
)
1105 fatal ("Can't detect architecture.");
1109 windres_open_as_binary (const char *filename
, int rdmode
)
1113 abfd
= (rdmode
? bfd_openr (filename
, "binary") : bfd_openw (filename
, "binary"));
1115 fatal ("can't open `%s' for %s", filename
, (rdmode
? "input" : "output"));
1117 if (rdmode
&& ! bfd_check_format (abfd
, bfd_object
))
1118 fatal ("can't open `%s' for input.", filename
);
1124 set_windres_bfd_endianness (windres_bfd
*wrbfd
, int is_bigendian
)
1127 switch (WR_KIND(wrbfd
))
1129 case WR_KIND_BFD_BIN_L
:
1131 WR_KIND(wrbfd
) = WR_KIND_BFD_BIN_B
;
1133 case WR_KIND_BFD_BIN_B
:
1135 WR_KIND(wrbfd
) = WR_KIND_BFD_BIN_L
;
1138 /* only binary bfd can be overriden. */
1144 set_windres_bfd (windres_bfd
*wrbfd
, bfd
*abfd
, asection
*sec
, rc_uint_type kind
)
1149 case WR_KIND_TARGET
:
1154 case WR_KIND_BFD_BIN_L
:
1155 case WR_KIND_BFD_BIN_B
:
1162 WR_KIND(wrbfd
) = kind
;
1163 WR_BFD(wrbfd
) = abfd
;
1164 WR_SECTION(wrbfd
) = sec
;
1168 set_windres_bfd_content (windres_bfd
*wrbfd
, const void *data
, rc_uint_type off
,
1169 rc_uint_type length
)
1171 if (WR_KIND(wrbfd
) != WR_KIND_TARGET
)
1173 if (! bfd_set_section_contents (WR_BFD(wrbfd
), WR_SECTION(wrbfd
), data
, off
, length
))
1174 bfd_fatal ("bfd_set_section_contents");
1181 get_windres_bfd_content (windres_bfd
*wrbfd
, void *data
, rc_uint_type off
,
1182 rc_uint_type length
)
1184 if (WR_KIND(wrbfd
) != WR_KIND_TARGET
)
1186 if (! bfd_get_section_contents (WR_BFD(wrbfd
), WR_SECTION(wrbfd
), data
, off
, length
))
1187 bfd_fatal ("bfd_get_section_contents");
1194 windres_put_8 (windres_bfd
*wrbfd
, void *p
, rc_uint_type value
)
1196 switch (WR_KIND(wrbfd
))
1198 case WR_KIND_TARGET
:
1199 target_put_8 (p
, value
);
1202 case WR_KIND_BFD_BIN_L
:
1203 case WR_KIND_BFD_BIN_B
:
1204 bfd_put_8 (WR_BFD(wrbfd
), value
, p
);
1212 windres_put_16 (windres_bfd
*wrbfd
, void *data
, rc_uint_type value
)
1214 switch (WR_KIND(wrbfd
))
1216 case WR_KIND_TARGET
:
1217 target_put_16 (data
, value
);
1220 case WR_KIND_BFD_BIN_B
:
1221 bfd_put_16 (WR_BFD(wrbfd
), value
, data
);
1223 case WR_KIND_BFD_BIN_L
:
1224 bfd_putl16 (value
, data
);
1232 windres_put_32 (windres_bfd
*wrbfd
, void *data
, rc_uint_type value
)
1234 switch (WR_KIND(wrbfd
))
1236 case WR_KIND_TARGET
:
1237 target_put_32 (data
, value
);
1240 case WR_KIND_BFD_BIN_B
:
1241 bfd_put_32 (WR_BFD(wrbfd
), value
, data
);
1243 case WR_KIND_BFD_BIN_L
:
1244 bfd_putl32 (value
, data
);
1252 windres_get_8 (windres_bfd
*wrbfd
, const void *data
, rc_uint_type length
)
1255 fatal ("windres_get_8: unexpected eob.");
1256 switch (WR_KIND(wrbfd
))
1258 case WR_KIND_TARGET
:
1259 return target_get_8 (data
, length
);
1261 case WR_KIND_BFD_BIN_B
:
1262 case WR_KIND_BFD_BIN_L
:
1263 return bfd_get_8 (WR_BFD(wrbfd
), data
);
1271 windres_get_16 (windres_bfd
*wrbfd
, const void *data
, rc_uint_type length
)
1274 fatal ("windres_get_16: unexpected eob.");
1275 switch (WR_KIND(wrbfd
))
1277 case WR_KIND_TARGET
:
1278 return target_get_16 (data
, length
);
1280 case WR_KIND_BFD_BIN_B
:
1281 return bfd_get_16 (WR_BFD(wrbfd
), data
);
1282 case WR_KIND_BFD_BIN_L
:
1283 return bfd_getl16 (data
);
1291 windres_get_32 (windres_bfd
*wrbfd
, const void *data
, rc_uint_type length
)
1294 fatal ("windres_get_32: unexpected eob.");
1295 switch (WR_KIND(wrbfd
))
1297 case WR_KIND_TARGET
:
1298 return target_get_32 (data
, length
);
1300 case WR_KIND_BFD_BIN_B
:
1301 return bfd_get_32 (WR_BFD(wrbfd
), data
);
1302 case WR_KIND_BFD_BIN_L
:
1303 return bfd_getl32 (data
);
1311 target_get_8 (const void *p
, rc_uint_type length
)
1316 fatal ("Resource too small for getting 8-bit value.");
1318 ret
= (rc_uint_type
) *((const bfd_byte
*) p
);
1323 target_get_16 (const void *p
, rc_uint_type length
)
1326 fatal ("Resource too small for getting 16-bit value.");
1328 if (target_is_bigendian
)
1329 return bfd_getb16 (p
);
1331 return bfd_getl16 (p
);
1335 target_get_32 (const void *p
, rc_uint_type length
)
1338 fatal ("Resource too small for getting 32-bit value.");
1340 if (target_is_bigendian
)
1341 return bfd_getb32 (p
);
1343 return bfd_getl32 (p
);
1347 target_put_8 (void *p
, rc_uint_type value
)
1350 *((bfd_byte
*) p
)=(bfd_byte
) value
;
1354 target_put_16 (void *p
, rc_uint_type value
)
1358 if (target_is_bigendian
)
1359 bfd_putb16 (value
, p
);
1361 bfd_putl16 (value
, p
);
1365 target_put_32 (void *p
, rc_uint_type value
)
1369 if (target_is_bigendian
)
1370 bfd_putb32 (value
, p
);
1372 bfd_putl32 (value
, p
);
1375 static int isInComment
= 0;
1377 int wr_printcomment (FILE *e
, const char *fmt
, ...)
1383 r
+= fprintf (e
, "\n ");
1389 va_start (arg
, fmt
);
1390 r
+= vfprintf (e
, fmt
, arg
);
1395 int wr_print (FILE *e
, const char *fmt
, ...)
1400 r
+= fprintf (e
, ". */\n");
1404 va_start (arg
, fmt
);
1405 r
+= vfprintf (e
, fmt
, arg
);