1 /* windres.c -- a program to manipulate Windows resources
2 Copyright (C) 1997-2014 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
);
213 fatal (_("can't open %s `%s': %s"), errmsg
, filename
, strerror (errno
));
215 /* Return a value to avoid a compiler warning. */
219 /* Compare two resource ID's. We consider name entries to come before
220 numeric entries, because that is how they appear in the COFF .rsrc
224 res_id_cmp (rc_res_id a
, rc_res_id b
)
232 else if (a
.u
.id
< b
.u
.id
)
239 unichar
*as
, *ase
, *bs
, *bse
;
245 ase
= as
+ a
.u
.n
.length
;
247 bse
= bs
+ b
.u
.n
.length
;
255 i
= (int) *as
- (int) *bs
;
269 /* Print a resource ID. */
272 res_id_print (FILE *stream
, rc_res_id id
, int quote
)
275 fprintf (stream
, "%u", (int) id
.u
.id
);
279 unicode_print_quoted (stream
, id
.u
.n
.name
, id
.u
.n
.length
);
281 unicode_print (stream
, id
.u
.n
.name
, id
.u
.n
.length
);
285 /* Print a list of resource ID's. */
288 res_ids_print (FILE *stream
, int cids
, const rc_res_id
*ids
)
292 for (i
= 0; i
< cids
; i
++)
294 res_id_print (stream
, ids
[i
], 1);
296 fprintf (stream
, ": ");
300 /* Convert an ASCII string to a resource ID. */
303 res_string_to_id (rc_res_id
*res_id
, const char *string
)
306 unicode_from_ascii (&res_id
->u
.n
.length
, &res_id
->u
.n
.name
, string
);
309 /* Convert an unicode string to a resource ID. */
311 res_unistring_to_id (rc_res_id
*res_id
, const unichar
*u
)
314 res_id
->u
.n
.length
= unichar_len (u
);
315 res_id
->u
.n
.name
= unichar_dup_uppercase (u
);
318 /* Define a resource. The arguments are the resource tree, RESOURCES,
319 and the location at which to put it in the tree, CIDS and IDS.
320 This returns a newly allocated rc_res_resource structure, which the
321 caller is expected to initialize. If DUPOK is non-zero, then if a
322 resource with this ID exists, it is returned. Otherwise, a warning
323 is issued, and a new resource is created replacing the existing
327 define_resource (rc_res_directory
**resources
, int cids
,
328 const rc_res_id
*ids
, int dupok
)
330 rc_res_entry
*re
= NULL
;
334 for (i
= 0; i
< cids
; i
++)
338 if (*resources
== NULL
)
340 *resources
= ((rc_res_directory
*)
341 res_alloc (sizeof (rc_res_directory
)));
342 (*resources
)->characteristics
= 0;
343 /* Using a real timestamp only serves to create non-deterministic
344 results. Use zero instead. */
345 (*resources
)->time
= 0;
346 (*resources
)->major
= 0;
347 (*resources
)->minor
= 0;
348 (*resources
)->entries
= NULL
;
351 for (pp
= &(*resources
)->entries
; *pp
!= NULL
; pp
= &(*pp
)->next
)
352 if (res_id_cmp ((*pp
)->id
, ids
[i
]) == 0)
359 re
= (rc_res_entry
*) res_alloc (sizeof (rc_res_entry
));
380 fprintf (stderr
, "%s: ", program_name
);
381 res_ids_print (stderr
, i
, ids
);
382 fprintf (stderr
, _(": expected to be a directory\n"));
386 resources
= &re
->u
.dir
;
392 fprintf (stderr
, "%s: ", program_name
);
393 res_ids_print (stderr
, cids
, ids
);
394 fprintf (stderr
, _(": expected to be a leaf\n"));
398 if (re
->u
.res
!= NULL
)
403 fprintf (stderr
, _("%s: warning: "), program_name
);
404 res_ids_print (stderr
, cids
, ids
);
405 fprintf (stderr
, _(": duplicate value\n"));
408 re
->u
.res
= ((rc_res_resource
*)
409 res_alloc (sizeof (rc_res_resource
)));
410 memset (re
->u
.res
, 0, sizeof (rc_res_resource
));
412 re
->u
.res
->type
= RES_TYPE_UNINITIALIZED
;
416 /* Define a standard resource. This is a version of define_resource
417 that just takes type, name, and language arguments. */
420 define_standard_resource (rc_res_directory
**resources
, int type
,
421 rc_res_id name
, rc_uint_type language
, int dupok
)
429 a
[2].u
.id
= language
;
430 return define_resource (resources
, 3, a
, dupok
);
433 /* Comparison routine for resource sorting. */
436 cmp_res_entry (const void *p1
, const void *p2
)
438 const rc_res_entry
**re1
, **re2
;
440 re1
= (const rc_res_entry
**) p1
;
441 re2
= (const rc_res_entry
**) p2
;
442 return res_id_cmp ((*re1
)->id
, (*re2
)->id
);
445 /* Sort the resources. */
447 static rc_res_directory
*
448 sort_resources (rc_res_directory
*resdir
)
454 if (resdir
->entries
== NULL
)
458 for (re
= resdir
->entries
; re
!= NULL
; re
= re
->next
)
461 /* This is a recursive routine, so using xmalloc is probably better
463 a
= (rc_res_entry
**) xmalloc (c
* sizeof (rc_res_entry
*));
465 for (i
= 0, re
= resdir
->entries
; re
!= NULL
; re
= re
->next
, i
++)
468 qsort (a
, c
, sizeof (rc_res_entry
*), cmp_res_entry
);
470 resdir
->entries
= a
[0];
471 for (i
= 0; i
< c
- 1; i
++)
472 a
[i
]->next
= a
[i
+ 1];
477 /* Now sort the subdirectories. */
479 for (re
= resdir
->entries
; re
!= NULL
; re
= re
->next
)
481 re
->u
.dir
= sort_resources (re
->u
.dir
);
486 /* Return whether the dialog resource DIALOG is a DIALOG or a
490 extended_dialog (const rc_dialog
*dialog
)
492 const rc_dialog_control
*c
;
494 if (dialog
->ex
!= NULL
)
497 for (c
= dialog
->controls
; c
!= NULL
; c
= c
->next
)
498 if (c
->data
!= NULL
|| c
->help
!= 0)
504 /* Return whether MENUITEMS are a MENU or a MENUEX. */
507 extended_menu (const rc_menu
*menu
)
509 return extended_menuitems (menu
->items
);
513 extended_menuitems (const rc_menuitem
*menuitems
)
515 const rc_menuitem
*mi
;
517 for (mi
= menuitems
; mi
!= NULL
; mi
= mi
->next
)
519 if (mi
->help
!= 0 || mi
->state
!= 0)
521 if (mi
->popup
!= NULL
&& mi
->id
!= 0)
524 & ~ (MENUITEM_CHECKED
528 | MENUITEM_MENUBARBREAK
529 | MENUITEM_MENUBREAK
))
532 if (mi
->popup
!= NULL
)
534 if (extended_menuitems (mi
->popup
))
542 /* Convert a string to a format type, or exit if it can't be done. */
544 static enum res_format
545 format_from_name (const char *name
, int exit_on_error
)
547 const struct format_map
*m
;
549 for (m
= format_names
; m
->name
!= NULL
; m
++)
550 if (strcasecmp (m
->name
, name
) == 0)
553 if (m
->name
== NULL
&& exit_on_error
)
555 non_fatal (_("unknown format type `%s'"), name
);
556 fprintf (stderr
, _("%s: supported formats:"), program_name
);
557 for (m
= format_names
; m
->name
!= NULL
; m
++)
558 fprintf (stderr
, " %s", m
->name
);
559 fprintf (stderr
, "\n");
566 /* Work out a format type given a file name. If INPUT is non-zero,
567 it's OK to look at the file itself. */
569 static enum res_format
570 format_from_filename (const char *filename
, int input
)
574 bfd_byte b1
, b2
, b3
, b4
, b5
;
577 /* If we have an extension, see if we recognize it as implying a
578 particular format. */
579 ext
= strrchr (filename
, '.');
582 const struct format_map
*m
;
585 for (m
= format_fileexts
; m
->name
!= NULL
; m
++)
586 if (strcasecmp (m
->name
, ext
) == 0)
590 /* If we don't recognize the name of an output file, assume it's a
593 return RES_FORMAT_COFF
;
595 /* Read the first few bytes of the file to see if we can guess what
597 e
= fopen (filename
, FOPEN_RB
);
599 fatal ("%s: %s", filename
, strerror (errno
));
609 /* A PE executable starts with 0x4d 0x5a. */
610 if (b1
== 0x4d && b2
== 0x5a)
611 return RES_FORMAT_COFF
;
613 /* A COFF .o file starts with a COFF magic number. */
614 magic
= (b2
<< 8) | b1
;
617 case 0x14c: /* i386 */
618 case 0x166: /* MIPS */
619 case 0x184: /* Alpha */
620 case 0x268: /* 68k */
621 case 0x1f0: /* PowerPC */
623 return RES_FORMAT_COFF
;
626 /* A RES file starts with 0x0 0x0 0x0 0x0 0x20 0x0 0x0 0x0. */
627 if (b1
== 0 && b2
== 0 && b3
== 0 && b4
== 0 && b5
== 0x20)
628 return RES_FORMAT_RES
;
630 /* If every character is printable or space, assume it's an RC file. */
631 if ((ISPRINT (b1
) || ISSPACE (b1
))
632 && (ISPRINT (b2
) || ISSPACE (b2
))
633 && (ISPRINT (b3
) || ISSPACE (b3
))
634 && (ISPRINT (b4
) || ISSPACE (b4
))
635 && (ISPRINT (b5
) || ISSPACE (b5
)))
636 return RES_FORMAT_RC
;
638 /* Otherwise, we give up. */
639 fatal (_("can not determine type of file `%s'; use the -J option"),
642 /* Return something to silence the compiler warning. */
643 return RES_FORMAT_UNKNOWN
;
646 /* Print a usage message and exit. */
649 usage (FILE *stream
, int status
)
651 fprintf (stream
, _("Usage: %s [option(s)] [input-file] [output-file]\n"),
653 fprintf (stream
, _(" The options are:\n\
654 -i --input=<file> Name input file\n\
655 -o --output=<file> Name output file\n\
656 -J --input-format=<format> Specify input format\n\
657 -O --output-format=<format> Specify output format\n\
658 -F --target=<target> Specify COFF target\n\
659 --preprocessor=<program> Program to use to preprocess rc file\n\
660 --preprocessor-arg=<arg> Additional preprocessor argument\n\
661 -I --include-dir=<dir> Include directory when preprocessing rc file\n\
662 -D --define <sym>[=<val>] Define SYM when preprocessing rc file\n\
663 -U --undefine <sym> Undefine SYM when preprocessing rc file\n\
664 -v --verbose Verbose - tells you what it's doing\n\
665 -c --codepage=<codepage> Specify default codepage\n\
666 -l --language=<val> Set language when reading rc file\n\
667 --use-temp-file Use a temporary file instead of popen to read\n\
668 the preprocessor output\n\
669 --no-use-temp-file Use popen (default)\n"));
671 fprintf (stream
, _("\
672 --yydebug Turn on parser debugging\n"));
674 fprintf (stream
, _("\
675 -r Ignored for compatibility with rc\n\
676 @<file> Read options from <file>\n\
677 -h --help Print this help message\n\
678 -V --version Print version information\n"));
679 fprintf (stream
, _("\
680 FORMAT is one of rc, res, or coff, and is deduced from the file name\n\
681 extension if not specified. A single file name is an input file.\n\
682 No input-file is stdin, default rc. No output-file is stdout, default rc.\n"));
684 list_supported_targets (program_name
, stream
);
686 if (REPORT_BUGS_TO
[0] && status
== 0)
687 fprintf (stream
, _("Report bugs to %s\n"), REPORT_BUGS_TO
);
692 /* Quote characters that will confuse the shell when we run the preprocessor. */
695 quot (const char *string
)
697 static char *buf
= 0;
698 static int buflen
= 0;
699 int slen
= strlen (string
);
703 if ((buflen
< slen
* 2 + 2) || ! buf
)
705 buflen
= slen
* 2 + 2;
708 buf
= (char *) xmalloc (buflen
);
711 for (src
=string
, dest
=buf
; *src
; src
++, dest
++)
713 if (*src
== '(' || *src
== ')' || *src
== ' ')
725 /* 150 isn't special; it's just an arbitrary non-ASCII char value. */
726 OPTION_PREPROCESSOR
= 150,
727 OPTION_USE_TEMP_FILE
,
728 OPTION_NO_USE_TEMP_FILE
,
731 OPTION_PREPROCESSOR_ARG
734 static const struct option long_options
[] =
736 {"input", required_argument
, 0, 'i'},
737 {"output", required_argument
, 0, 'o'},
738 {"input-format", required_argument
, 0, 'J'},
739 {"output-format", required_argument
, 0, 'O'},
740 {"target", required_argument
, 0, 'F'},
741 {"preprocessor", required_argument
, 0, OPTION_PREPROCESSOR
},
742 {"preprocessor-arg", required_argument
, 0, OPTION_PREPROCESSOR_ARG
},
743 {"include-dir", required_argument
, 0, OPTION_INCLUDE_DIR
},
744 {"define", required_argument
, 0, 'D'},
745 {"undefine", required_argument
, 0, 'U'},
746 {"verbose", no_argument
, 0, 'v'},
747 {"codepage", required_argument
, 0, 'c'},
748 {"language", required_argument
, 0, 'l'},
749 {"use-temp-file", no_argument
, 0, OPTION_USE_TEMP_FILE
},
750 {"no-use-temp-file", no_argument
, 0, OPTION_NO_USE_TEMP_FILE
},
751 {"yydebug", no_argument
, 0, OPTION_YYDEBUG
},
752 {"version", no_argument
, 0, 'V'},
753 {"help", no_argument
, 0, 'h'},
754 {0, no_argument
, 0, 0}
758 windres_add_include_dir (const char *p
)
760 struct include_dir
*n
, **pp
;
762 /* Computing paths is often complicated and error prone.
763 The easiest way to check for mistakes is at the time
764 we add them to include_dirs. */
768 n
= xmalloc (sizeof *n
);
770 n
->dir
= (char * ) p
;
772 for (pp
= &include_dirs
; *pp
!= NULL
; pp
= &(*pp
)->next
)
777 /* This keeps gcc happy when using -Wmissing-prototypes -Wstrict-prototypes. */
778 int main (int, char **);
780 /* The main function. */
783 main (int argc
, char **argv
)
786 char *input_filename
;
787 char *output_filename
;
788 enum res_format input_format
;
789 enum res_format input_format_tmp
;
790 enum res_format output_format
;
794 const char *quotedarg
;
796 rc_res_directory
*resources
;
799 #if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
800 setlocale (LC_MESSAGES
, "");
802 #if defined (HAVE_SETLOCALE)
803 setlocale (LC_CTYPE
, "");
805 bindtextdomain (PACKAGE
, LOCALEDIR
);
806 textdomain (PACKAGE
);
808 program_name
= argv
[0];
809 xmalloc_set_program_name (program_name
);
811 expandargv (&argc
, &argv
);
814 set_default_bfd_target ();
818 input_filename
= NULL
;
819 output_filename
= NULL
;
820 input_format
= RES_FORMAT_UNKNOWN
;
821 output_format
= RES_FORMAT_UNKNOWN
;
825 language
= 0x409; /* LANG_ENGLISH, SUBLANG_ENGLISH_US. */
828 while ((c
= getopt_long (argc
, argv
, "c:f:i:l:o:I:J:O:F:D:U:rhHvV", long_options
,
837 if (optarg
[0] == '0' && (optarg
[1] == 'x' || optarg
[1] == 'X'))
838 ncp
= (rc_uint_type
) strtol (optarg
+ 2, NULL
, 16);
840 ncp
= (rc_uint_type
) strtol (optarg
, NULL
, 10);
841 if (ncp
== CP_UTF16
|| ! unicode_is_valid_codepage (ncp
))
842 fatal (_("invalid codepage specified.\n"));
843 wind_default_codepage
= wind_current_codepage
= ncp
;
848 input_filename
= optarg
;
852 /* For compatibility with rc we accept "-fo <name>" as being the
853 equivalent of "-o <name>". We do not advertise this fact
854 though, as we do not want users to use non-GNU like command
857 fatal (_("invalid option -f\n"));
862 fatal (_("No filename following the -fo option.\n"));
863 optarg
= argv
[optind
++];
868 output_filename
= optarg
;
872 input_format
= format_from_name (optarg
, 1);
876 output_format
= format_from_name (optarg
, 1);
883 case OPTION_PREPROCESSOR
:
884 preprocessor
= optarg
;
887 case OPTION_PREPROCESSOR_ARG
:
888 if (preprocargs
== NULL
)
890 quotedarg
= quot (optarg
);
891 preprocargs
= xstrdup (quotedarg
);
897 quotedarg
= quot (optarg
);
898 n
= xmalloc (strlen (preprocargs
) + strlen (quotedarg
) + 2);
899 sprintf (n
, "%s %s", preprocargs
, quotedarg
);
907 if (preprocargs
== NULL
)
909 quotedarg
= quot (optarg
);
910 preprocargs
= xmalloc (strlen (quotedarg
) + 3);
911 sprintf (preprocargs
, "-%c%s", c
, quotedarg
);
917 quotedarg
= quot (optarg
);
918 n
= xmalloc (strlen (preprocargs
) + strlen (quotedarg
) + 4);
919 sprintf (n
, "%s -%c%s", preprocargs
, c
, quotedarg
);
926 /* Ignored for compatibility with rc. */
934 /* For backward compatibility, should be removed in the future. */
935 input_format_tmp
= format_from_name (optarg
, 0);
936 if (input_format_tmp
!= RES_FORMAT_UNKNOWN
)
941 if (stat (optarg
, & statbuf
) == 0
942 /* Coded this way to avoid importing knowledge of S_ISDIR into this file. */
943 && (mode_string (statbuf
.st_mode
, modebuf
), modebuf
[0] == 'd'))
944 /* We have a -I option with a directory name that just happens
945 to match a format name as well. eg: -I res Assume that the
946 user knows what they are doing and do not complain. */
951 _("Option -I is deprecated for setting the input format, please use -J instead.\n"));
952 input_format
= input_format_tmp
;
958 case OPTION_INCLUDE_DIR
:
959 if (preprocargs
== NULL
)
961 quotedarg
= quot (optarg
);
962 preprocargs
= xmalloc (strlen (quotedarg
) + 3);
963 sprintf (preprocargs
, "-I%s", quotedarg
);
969 quotedarg
= quot (optarg
);
970 n
= xmalloc (strlen (preprocargs
) + strlen (quotedarg
) + 4);
971 sprintf (n
, "%s -I%s", preprocargs
, quotedarg
);
976 windres_add_include_dir (optarg
);
981 language
= strtol (optarg
, (char **) NULL
, 16);
984 case OPTION_USE_TEMP_FILE
:
988 case OPTION_NO_USE_TEMP_FILE
:
1004 print_version ("windres");
1013 if (input_filename
== NULL
&& optind
< argc
)
1015 input_filename
= argv
[optind
];
1019 if (output_filename
== NULL
&& optind
< argc
)
1021 output_filename
= argv
[optind
];
1028 if (input_format
== RES_FORMAT_UNKNOWN
)
1030 if (input_filename
== NULL
)
1031 input_format
= RES_FORMAT_RC
;
1033 input_format
= format_from_filename (input_filename
, 1);
1036 if (output_format
== RES_FORMAT_UNKNOWN
)
1038 if (output_filename
== NULL
)
1039 output_format
= RES_FORMAT_RC
;
1041 output_format
= format_from_filename (output_filename
, 0);
1044 set_endianness (NULL
, target
);
1046 /* Read the input file. */
1047 switch (input_format
)
1052 resources
= read_rc_file (input_filename
, preprocessor
, preprocargs
,
1053 language
, use_temp_file
);
1055 case RES_FORMAT_RES
:
1056 resources
= read_res_file (input_filename
);
1058 case RES_FORMAT_COFF
:
1059 resources
= read_coff_rsrc (input_filename
, target
);
1063 if (resources
== NULL
)
1064 fatal (_("no resources"));
1066 /* Sort the resources. This is required for COFF, convenient for
1067 rc, and unimportant for res. */
1068 resources
= sort_resources (resources
);
1070 /* Write the output file. */
1073 switch (output_format
)
1078 write_rc_file (output_filename
, resources
);
1080 case RES_FORMAT_RES
:
1081 write_res_file (output_filename
, resources
);
1083 case RES_FORMAT_COFF
:
1084 write_coff_file (output_filename
, target
, resources
);
1093 set_endianness (bfd
*abfd
, const char *target
)
1095 const bfd_target
*target_vec
;
1097 def_target_arch
= NULL
;
1098 target_vec
= bfd_get_target_info (target
, abfd
, &target_is_bigendian
, NULL
,
1101 fatal ("Can't detect target endianness and architecture.");
1102 if (! def_target_arch
)
1103 fatal ("Can't detect architecture.");
1107 windres_open_as_binary (const char *filename
, int rdmode
)
1111 abfd
= (rdmode
? bfd_openr (filename
, "binary") : bfd_openw (filename
, "binary"));
1113 fatal ("can't open `%s' for %s", filename
, (rdmode
? "input" : "output"));
1115 if (rdmode
&& ! bfd_check_format (abfd
, bfd_object
))
1116 fatal ("can't open `%s' for input.", filename
);
1122 set_windres_bfd_endianness (windres_bfd
*wrbfd
, int is_bigendian
)
1125 switch (WR_KIND(wrbfd
))
1127 case WR_KIND_BFD_BIN_L
:
1129 WR_KIND(wrbfd
) = WR_KIND_BFD_BIN_B
;
1131 case WR_KIND_BFD_BIN_B
:
1133 WR_KIND(wrbfd
) = WR_KIND_BFD_BIN_L
;
1136 /* only binary bfd can be overriden. */
1142 set_windres_bfd (windres_bfd
*wrbfd
, bfd
*abfd
, asection
*sec
, rc_uint_type kind
)
1147 case WR_KIND_TARGET
:
1152 case WR_KIND_BFD_BIN_L
:
1153 case WR_KIND_BFD_BIN_B
:
1160 WR_KIND(wrbfd
) = kind
;
1161 WR_BFD(wrbfd
) = abfd
;
1162 WR_SECTION(wrbfd
) = sec
;
1166 set_windres_bfd_content (windres_bfd
*wrbfd
, const void *data
, rc_uint_type off
,
1167 rc_uint_type length
)
1169 if (WR_KIND(wrbfd
) != WR_KIND_TARGET
)
1171 if (! bfd_set_section_contents (WR_BFD(wrbfd
), WR_SECTION(wrbfd
), data
, off
, length
))
1172 bfd_fatal ("bfd_set_section_contents");
1179 get_windres_bfd_content (windres_bfd
*wrbfd
, void *data
, rc_uint_type off
,
1180 rc_uint_type length
)
1182 if (WR_KIND(wrbfd
) != WR_KIND_TARGET
)
1184 if (! bfd_get_section_contents (WR_BFD(wrbfd
), WR_SECTION(wrbfd
), data
, off
, length
))
1185 bfd_fatal ("bfd_get_section_contents");
1192 windres_put_8 (windres_bfd
*wrbfd
, void *p
, rc_uint_type value
)
1194 switch (WR_KIND(wrbfd
))
1196 case WR_KIND_TARGET
:
1197 target_put_8 (p
, value
);
1200 case WR_KIND_BFD_BIN_L
:
1201 case WR_KIND_BFD_BIN_B
:
1202 bfd_put_8 (WR_BFD(wrbfd
), value
, p
);
1210 windres_put_16 (windres_bfd
*wrbfd
, void *data
, rc_uint_type value
)
1212 switch (WR_KIND(wrbfd
))
1214 case WR_KIND_TARGET
:
1215 target_put_16 (data
, value
);
1218 case WR_KIND_BFD_BIN_B
:
1219 bfd_put_16 (WR_BFD(wrbfd
), value
, data
);
1221 case WR_KIND_BFD_BIN_L
:
1222 bfd_putl16 (value
, data
);
1230 windres_put_32 (windres_bfd
*wrbfd
, void *data
, rc_uint_type value
)
1232 switch (WR_KIND(wrbfd
))
1234 case WR_KIND_TARGET
:
1235 target_put_32 (data
, value
);
1238 case WR_KIND_BFD_BIN_B
:
1239 bfd_put_32 (WR_BFD(wrbfd
), value
, data
);
1241 case WR_KIND_BFD_BIN_L
:
1242 bfd_putl32 (value
, data
);
1250 windres_get_8 (windres_bfd
*wrbfd
, const void *data
, rc_uint_type length
)
1253 fatal ("windres_get_8: unexpected eob.");
1254 switch (WR_KIND(wrbfd
))
1256 case WR_KIND_TARGET
:
1257 return target_get_8 (data
, length
);
1259 case WR_KIND_BFD_BIN_B
:
1260 case WR_KIND_BFD_BIN_L
:
1261 return bfd_get_8 (WR_BFD(wrbfd
), data
);
1269 windres_get_16 (windres_bfd
*wrbfd
, const void *data
, rc_uint_type length
)
1272 fatal ("windres_get_16: unexpected eob.");
1273 switch (WR_KIND(wrbfd
))
1275 case WR_KIND_TARGET
:
1276 return target_get_16 (data
, length
);
1278 case WR_KIND_BFD_BIN_B
:
1279 return bfd_get_16 (WR_BFD(wrbfd
), data
);
1280 case WR_KIND_BFD_BIN_L
:
1281 return bfd_getl16 (data
);
1289 windres_get_32 (windres_bfd
*wrbfd
, const void *data
, rc_uint_type length
)
1292 fatal ("windres_get_32: unexpected eob.");
1293 switch (WR_KIND(wrbfd
))
1295 case WR_KIND_TARGET
:
1296 return target_get_32 (data
, length
);
1298 case WR_KIND_BFD_BIN_B
:
1299 return bfd_get_32 (WR_BFD(wrbfd
), data
);
1300 case WR_KIND_BFD_BIN_L
:
1301 return bfd_getl32 (data
);
1309 target_get_8 (const void *p
, rc_uint_type length
)
1314 fatal ("Resource too small for getting 8-bit value.");
1316 ret
= (rc_uint_type
) *((const bfd_byte
*) p
);
1321 target_get_16 (const void *p
, rc_uint_type length
)
1324 fatal ("Resource too small for getting 16-bit value.");
1326 if (target_is_bigendian
)
1327 return bfd_getb16 (p
);
1329 return bfd_getl16 (p
);
1333 target_get_32 (const void *p
, rc_uint_type length
)
1336 fatal ("Resource too small for getting 32-bit value.");
1338 if (target_is_bigendian
)
1339 return bfd_getb32 (p
);
1341 return bfd_getl32 (p
);
1345 target_put_8 (void *p
, rc_uint_type value
)
1348 *((bfd_byte
*) p
)=(bfd_byte
) value
;
1352 target_put_16 (void *p
, rc_uint_type value
)
1356 if (target_is_bigendian
)
1357 bfd_putb16 (value
, p
);
1359 bfd_putl16 (value
, p
);
1363 target_put_32 (void *p
, rc_uint_type value
)
1367 if (target_is_bigendian
)
1368 bfd_putb32 (value
, p
);
1370 bfd_putl32 (value
, p
);
1373 static int isInComment
= 0;
1375 int wr_printcomment (FILE *e
, const char *fmt
, ...)
1381 r
+= fprintf (e
, "\n ");
1387 va_start (arg
, fmt
);
1388 r
+= vfprintf (e
, fmt
, arg
);
1393 int wr_print (FILE *e
, const char *fmt
, ...)
1398 r
+= fprintf (e
, ". */\n");
1402 va_start (arg
, fmt
);
1403 r
+= vfprintf (e
, fmt
, arg
);