1 /* windres.c -- a program to manipulate Windows resources
2 Copyright 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2007, 2008,
3 2009, 2011, 2012 Free Software Foundation, Inc.
4 Written by Ian Lance Taylor, Cygnus Support.
5 Rewritten by Kai Tietz, Onevision.
7 This file is part of GNU Binutils.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
24 /* This program can read and write Windows resources in various
25 formats. In particular, it can act like the rc resource compiler
26 program, and it can act like the cvtres res to COFF conversion
29 It is based on information taken from the following sources:
31 * Microsoft documentation.
33 * The rcl program, written by Gunther Ebert
34 <gunther.ebert@ixos-leipzig.de>.
36 * The res2coff program, written by Pedro A. Aranda <paag@tid.es>. */
44 #include "libiberty.h"
45 #include "safe-ctype.h"
49 /* Used by resrc.c at least. */
53 int target_is_bigendian
= 0;
54 const char *def_target_arch
;
56 static void set_endianness (bfd
*, const char *);
58 /* An enumeration of format types. */
64 /* Textual RC file. */
66 /* Binary RES file. */
72 /* A structure used to map between format types and strings. */
77 enum res_format format
;
80 /* A mapping between names and format types. */
82 static const struct format_map format_names
[] =
84 { "rc", RES_FORMAT_RC
},
85 { "res", RES_FORMAT_RES
},
86 { "coff", RES_FORMAT_COFF
},
87 { NULL
, RES_FORMAT_UNKNOWN
}
90 /* A mapping from file extensions to format types. */
92 static const struct format_map format_fileexts
[] =
94 { "rc", RES_FORMAT_RC
},
95 { "res", RES_FORMAT_RES
},
96 { "exe", RES_FORMAT_COFF
},
97 { "obj", RES_FORMAT_COFF
},
98 { "o", RES_FORMAT_COFF
},
99 { NULL
, RES_FORMAT_UNKNOWN
}
102 /* A list of include directories. */
106 struct include_dir
*next
;
110 static struct include_dir
*include_dirs
;
112 /* Static functions. */
114 static void res_init (void);
115 static int extended_menuitems (const rc_menuitem
*);
116 static enum res_format
format_from_name (const char *, int);
117 static enum res_format
format_from_filename (const char *, int);
118 static void usage (FILE *, int);
119 static int cmp_res_entry (const void *, const void *);
120 static rc_res_directory
*sort_resources (rc_res_directory
*);
121 static void reswr_init (void);
122 static const char * quot (const char *);
124 static rc_uint_type
target_get_8 (const void *, rc_uint_type
);
125 static void target_put_8 (void *, rc_uint_type
);
126 static rc_uint_type
target_get_16 (const void *, rc_uint_type
);
127 static void target_put_16 (void *, rc_uint_type
);
128 static rc_uint_type
target_get_32 (const void *, rc_uint_type
);
129 static void target_put_32 (void *, rc_uint_type
);
132 /* When we are building a resource tree, we allocate everything onto
133 an obstack, so that we can free it all at once if we want. */
135 #define obstack_chunk_alloc xmalloc
136 #define obstack_chunk_free free
138 /* The resource building obstack. */
140 static struct obstack res_obstack
;
142 /* Initialize the resource building obstack. */
147 obstack_init (&res_obstack
);
150 /* Allocate space on the resource building obstack. */
153 res_alloc (rc_uint_type bytes
)
155 return obstack_alloc (&res_obstack
, (size_t) bytes
);
158 /* We also use an obstack to save memory used while writing out a set
161 static struct obstack reswr_obstack
;
163 /* Initialize the resource writing obstack. */
168 obstack_init (&reswr_obstack
);
171 /* Allocate space on the resource writing obstack. */
174 reswr_alloc (rc_uint_type bytes
)
176 return obstack_alloc (&reswr_obstack
, (size_t) bytes
);
179 /* Open a file using the include directory search list. */
182 open_file_search (const char *filename
, const char *mode
, const char *errmsg
,
183 char **real_filename
)
186 struct include_dir
*d
;
188 e
= fopen (filename
, mode
);
191 *real_filename
= xstrdup (filename
);
197 for (d
= include_dirs
; d
!= NULL
; d
= d
->next
)
201 n
= (char *) xmalloc (strlen (d
->dir
) + strlen (filename
) + 2);
202 sprintf (n
, "%s/%s", d
->dir
, filename
);
215 fatal (_("can't open %s `%s': %s"), errmsg
, filename
, strerror (errno
));
217 /* Return a value to avoid a compiler warning. */
221 /* Compare two resource ID's. We consider name entries to come before
222 numeric entries, because that is how they appear in the COFF .rsrc
226 res_id_cmp (rc_res_id a
, rc_res_id b
)
234 else if (a
.u
.id
< b
.u
.id
)
241 unichar
*as
, *ase
, *bs
, *bse
;
247 ase
= as
+ a
.u
.n
.length
;
249 bse
= bs
+ b
.u
.n
.length
;
257 i
= (int) *as
- (int) *bs
;
271 /* Print a resource ID. */
274 res_id_print (FILE *stream
, rc_res_id id
, int quote
)
277 fprintf (stream
, "%u", (int) id
.u
.id
);
281 unicode_print_quoted (stream
, id
.u
.n
.name
, id
.u
.n
.length
);
283 unicode_print (stream
, id
.u
.n
.name
, id
.u
.n
.length
);
287 /* Print a list of resource ID's. */
290 res_ids_print (FILE *stream
, int cids
, const rc_res_id
*ids
)
294 for (i
= 0; i
< cids
; i
++)
296 res_id_print (stream
, ids
[i
], 1);
298 fprintf (stream
, ": ");
302 /* Convert an ASCII string to a resource ID. */
305 res_string_to_id (rc_res_id
*res_id
, const char *string
)
308 unicode_from_ascii (&res_id
->u
.n
.length
, &res_id
->u
.n
.name
, string
);
311 /* Convert an unicode string to a resource ID. */
313 res_unistring_to_id (rc_res_id
*res_id
, const unichar
*u
)
316 res_id
->u
.n
.length
= unichar_len (u
);
317 res_id
->u
.n
.name
= unichar_dup_uppercase (u
);
320 /* Define a resource. The arguments are the resource tree, RESOURCES,
321 and the location at which to put it in the tree, CIDS and IDS.
322 This returns a newly allocated rc_res_resource structure, which the
323 caller is expected to initialize. If DUPOK is non-zero, then if a
324 resource with this ID exists, it is returned. Otherwise, a warning
325 is issued, and a new resource is created replacing the existing
329 define_resource (rc_res_directory
**resources
, int cids
,
330 const rc_res_id
*ids
, int dupok
)
332 rc_res_entry
*re
= NULL
;
336 for (i
= 0; i
< cids
; i
++)
340 if (*resources
== NULL
)
342 static unsigned int timeval
;
344 /* Use the same timestamp for every resource created in a
347 timeval
= time (NULL
);
349 *resources
= ((rc_res_directory
*)
350 res_alloc (sizeof (rc_res_directory
)));
351 (*resources
)->characteristics
= 0;
352 (*resources
)->time
= timeval
;
353 (*resources
)->major
= 0;
354 (*resources
)->minor
= 0;
355 (*resources
)->entries
= NULL
;
358 for (pp
= &(*resources
)->entries
; *pp
!= NULL
; pp
= &(*pp
)->next
)
359 if (res_id_cmp ((*pp
)->id
, ids
[i
]) == 0)
366 re
= (rc_res_entry
*) res_alloc (sizeof (rc_res_entry
));
387 fprintf (stderr
, "%s: ", program_name
);
388 res_ids_print (stderr
, i
, ids
);
389 fprintf (stderr
, _(": expected to be a directory\n"));
393 resources
= &re
->u
.dir
;
399 fprintf (stderr
, "%s: ", program_name
);
400 res_ids_print (stderr
, cids
, ids
);
401 fprintf (stderr
, _(": expected to be a leaf\n"));
405 if (re
->u
.res
!= NULL
)
410 fprintf (stderr
, _("%s: warning: "), program_name
);
411 res_ids_print (stderr
, cids
, ids
);
412 fprintf (stderr
, _(": duplicate value\n"));
415 re
->u
.res
= ((rc_res_resource
*)
416 res_alloc (sizeof (rc_res_resource
)));
417 memset (re
->u
.res
, 0, sizeof (rc_res_resource
));
419 re
->u
.res
->type
= RES_TYPE_UNINITIALIZED
;
423 /* Define a standard resource. This is a version of define_resource
424 that just takes type, name, and language arguments. */
427 define_standard_resource (rc_res_directory
**resources
, int type
,
428 rc_res_id name
, rc_uint_type language
, int dupok
)
436 a
[2].u
.id
= language
;
437 return define_resource (resources
, 3, a
, dupok
);
440 /* Comparison routine for resource sorting. */
443 cmp_res_entry (const void *p1
, const void *p2
)
445 const rc_res_entry
**re1
, **re2
;
447 re1
= (const rc_res_entry
**) p1
;
448 re2
= (const rc_res_entry
**) p2
;
449 return res_id_cmp ((*re1
)->id
, (*re2
)->id
);
452 /* Sort the resources. */
454 static rc_res_directory
*
455 sort_resources (rc_res_directory
*resdir
)
461 if (resdir
->entries
== NULL
)
465 for (re
= resdir
->entries
; re
!= NULL
; re
= re
->next
)
468 /* This is a recursive routine, so using xmalloc is probably better
470 a
= (rc_res_entry
**) xmalloc (c
* sizeof (rc_res_entry
*));
472 for (i
= 0, re
= resdir
->entries
; re
!= NULL
; re
= re
->next
, i
++)
475 qsort (a
, c
, sizeof (rc_res_entry
*), cmp_res_entry
);
477 resdir
->entries
= a
[0];
478 for (i
= 0; i
< c
- 1; i
++)
479 a
[i
]->next
= a
[i
+ 1];
484 /* Now sort the subdirectories. */
486 for (re
= resdir
->entries
; re
!= NULL
; re
= re
->next
)
488 re
->u
.dir
= sort_resources (re
->u
.dir
);
493 /* Return whether the dialog resource DIALOG is a DIALOG or a
497 extended_dialog (const rc_dialog
*dialog
)
499 const rc_dialog_control
*c
;
501 if (dialog
->ex
!= NULL
)
504 for (c
= dialog
->controls
; c
!= NULL
; c
= c
->next
)
505 if (c
->data
!= NULL
|| c
->help
!= 0)
511 /* Return whether MENUITEMS are a MENU or a MENUEX. */
514 extended_menu (const rc_menu
*menu
)
516 return extended_menuitems (menu
->items
);
520 extended_menuitems (const rc_menuitem
*menuitems
)
522 const rc_menuitem
*mi
;
524 for (mi
= menuitems
; mi
!= NULL
; mi
= mi
->next
)
526 if (mi
->help
!= 0 || mi
->state
!= 0)
528 if (mi
->popup
!= NULL
&& mi
->id
!= 0)
531 & ~ (MENUITEM_CHECKED
535 | MENUITEM_MENUBARBREAK
536 | MENUITEM_MENUBREAK
))
539 if (mi
->popup
!= NULL
)
541 if (extended_menuitems (mi
->popup
))
549 /* Convert a string to a format type, or exit if it can't be done. */
551 static enum res_format
552 format_from_name (const char *name
, int exit_on_error
)
554 const struct format_map
*m
;
556 for (m
= format_names
; m
->name
!= NULL
; m
++)
557 if (strcasecmp (m
->name
, name
) == 0)
560 if (m
->name
== NULL
&& exit_on_error
)
562 non_fatal (_("unknown format type `%s'"), name
);
563 fprintf (stderr
, _("%s: supported formats:"), program_name
);
564 for (m
= format_names
; m
->name
!= NULL
; m
++)
565 fprintf (stderr
, " %s", m
->name
);
566 fprintf (stderr
, "\n");
573 /* Work out a format type given a file name. If INPUT is non-zero,
574 it's OK to look at the file itself. */
576 static enum res_format
577 format_from_filename (const char *filename
, int input
)
581 bfd_byte b1
, b2
, b3
, b4
, b5
;
584 /* If we have an extension, see if we recognize it as implying a
585 particular format. */
586 ext
= strrchr (filename
, '.');
589 const struct format_map
*m
;
592 for (m
= format_fileexts
; m
->name
!= NULL
; m
++)
593 if (strcasecmp (m
->name
, ext
) == 0)
597 /* If we don't recognize the name of an output file, assume it's a
600 return RES_FORMAT_COFF
;
602 /* Read the first few bytes of the file to see if we can guess what
604 e
= fopen (filename
, FOPEN_RB
);
606 fatal ("%s: %s", filename
, strerror (errno
));
616 /* A PE executable starts with 0x4d 0x5a. */
617 if (b1
== 0x4d && b2
== 0x5a)
618 return RES_FORMAT_COFF
;
620 /* A COFF .o file starts with a COFF magic number. */
621 magic
= (b2
<< 8) | b1
;
624 case 0x14c: /* i386 */
625 case 0x166: /* MIPS */
626 case 0x184: /* Alpha */
627 case 0x268: /* 68k */
628 case 0x1f0: /* PowerPC */
630 return RES_FORMAT_COFF
;
633 /* A RES file starts with 0x0 0x0 0x0 0x0 0x20 0x0 0x0 0x0. */
634 if (b1
== 0 && b2
== 0 && b3
== 0 && b4
== 0 && b5
== 0x20)
635 return RES_FORMAT_RES
;
637 /* If every character is printable or space, assume it's an RC file. */
638 if ((ISPRINT (b1
) || ISSPACE (b1
))
639 && (ISPRINT (b2
) || ISSPACE (b2
))
640 && (ISPRINT (b3
) || ISSPACE (b3
))
641 && (ISPRINT (b4
) || ISSPACE (b4
))
642 && (ISPRINT (b5
) || ISSPACE (b5
)))
643 return RES_FORMAT_RC
;
645 /* Otherwise, we give up. */
646 fatal (_("can not determine type of file `%s'; use the -J option"),
649 /* Return something to silence the compiler warning. */
650 return RES_FORMAT_UNKNOWN
;
653 /* Print a usage message and exit. */
656 usage (FILE *stream
, int status
)
658 fprintf (stream
, _("Usage: %s [option(s)] [input-file] [output-file]\n"),
660 fprintf (stream
, _(" The options are:\n\
661 -i --input=<file> Name input file\n\
662 -o --output=<file> Name output file\n\
663 -J --input-format=<format> Specify input format\n\
664 -O --output-format=<format> Specify output format\n\
665 -F --target=<target> Specify COFF target\n\
666 --preprocessor=<program> Program to use to preprocess rc file\n\
667 --preprocessor-arg=<arg> Additional preprocessor argument\n\
668 -I --include-dir=<dir> Include directory when preprocessing rc file\n\
669 -D --define <sym>[=<val>] Define SYM when preprocessing rc file\n\
670 -U --undefine <sym> Undefine SYM when preprocessing rc file\n\
671 -v --verbose Verbose - tells you what it's doing\n\
672 -c --codepage=<codepage> Specify default codepage\n\
673 -l --language=<val> Set language when reading rc file\n\
674 --use-temp-file Use a temporary file instead of popen to read\n\
675 the preprocessor output\n\
676 --no-use-temp-file Use popen (default)\n"));
678 fprintf (stream
, _("\
679 --yydebug Turn on parser debugging\n"));
681 fprintf (stream
, _("\
682 -r Ignored for compatibility with rc\n\
683 @<file> Read options from <file>\n\
684 -h --help Print this help message\n\
685 -V --version Print version information\n"));
686 fprintf (stream
, _("\
687 FORMAT is one of rc, res, or coff, and is deduced from the file name\n\
688 extension if not specified. A single file name is an input file.\n\
689 No input-file is stdin, default rc. No output-file is stdout, default rc.\n"));
691 list_supported_targets (program_name
, stream
);
693 if (REPORT_BUGS_TO
[0] && status
== 0)
694 fprintf (stream
, _("Report bugs to %s\n"), REPORT_BUGS_TO
);
699 /* Quote characters that will confuse the shell when we run the preprocessor. */
702 quot (const char *string
)
704 static char *buf
= 0;
705 static int buflen
= 0;
706 int slen
= strlen (string
);
710 if ((buflen
< slen
* 2 + 2) || ! buf
)
712 buflen
= slen
* 2 + 2;
715 buf
= (char *) xmalloc (buflen
);
718 for (src
=string
, dest
=buf
; *src
; src
++, dest
++)
720 if (*src
== '(' || *src
== ')' || *src
== ' ')
732 /* 150 isn't special; it's just an arbitrary non-ASCII char value. */
733 OPTION_PREPROCESSOR
= 150,
734 OPTION_USE_TEMP_FILE
,
735 OPTION_NO_USE_TEMP_FILE
,
738 OPTION_PREPROCESSOR_ARG
741 static const struct option long_options
[] =
743 {"input", required_argument
, 0, 'i'},
744 {"output", required_argument
, 0, 'o'},
745 {"input-format", required_argument
, 0, 'J'},
746 {"output-format", required_argument
, 0, 'O'},
747 {"target", required_argument
, 0, 'F'},
748 {"preprocessor", required_argument
, 0, OPTION_PREPROCESSOR
},
749 {"preprocessor-arg", required_argument
, 0, OPTION_PREPROCESSOR_ARG
},
750 {"include-dir", required_argument
, 0, OPTION_INCLUDE_DIR
},
751 {"define", required_argument
, 0, 'D'},
752 {"undefine", required_argument
, 0, 'U'},
753 {"verbose", no_argument
, 0, 'v'},
754 {"codepage", required_argument
, 0, 'c'},
755 {"language", required_argument
, 0, 'l'},
756 {"use-temp-file", no_argument
, 0, OPTION_USE_TEMP_FILE
},
757 {"no-use-temp-file", no_argument
, 0, OPTION_NO_USE_TEMP_FILE
},
758 {"yydebug", no_argument
, 0, OPTION_YYDEBUG
},
759 {"version", no_argument
, 0, 'V'},
760 {"help", no_argument
, 0, 'h'},
761 {0, no_argument
, 0, 0}
765 windres_add_include_dir (const char *p
)
767 struct include_dir
*n
, **pp
;
769 /* Computing paths is often complicated and error prone.
770 The easiest way to check for mistakes is at the time
771 we add them to include_dirs. */
775 n
= xmalloc (sizeof *n
);
777 n
->dir
= (char * ) p
;
779 for (pp
= &include_dirs
; *pp
!= NULL
; pp
= &(*pp
)->next
)
784 /* This keeps gcc happy when using -Wmissing-prototypes -Wstrict-prototypes. */
785 int main (int, char **);
787 /* The main function. */
790 main (int argc
, char **argv
)
793 char *input_filename
;
794 char *output_filename
;
795 enum res_format input_format
;
796 enum res_format input_format_tmp
;
797 enum res_format output_format
;
801 const char *quotedarg
;
803 rc_res_directory
*resources
;
806 #if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
807 setlocale (LC_MESSAGES
, "");
809 #if defined (HAVE_SETLOCALE)
810 setlocale (LC_CTYPE
, "");
812 bindtextdomain (PACKAGE
, LOCALEDIR
);
813 textdomain (PACKAGE
);
815 program_name
= argv
[0];
816 xmalloc_set_program_name (program_name
);
818 expandargv (&argc
, &argv
);
821 set_default_bfd_target ();
825 input_filename
= NULL
;
826 output_filename
= NULL
;
827 input_format
= RES_FORMAT_UNKNOWN
;
828 output_format
= RES_FORMAT_UNKNOWN
;
832 language
= 0x409; /* LANG_ENGLISH, SUBLANG_ENGLISH_US. */
835 while ((c
= getopt_long (argc
, argv
, "c:f:i:l:o:I:J:O:F:D:U:rhHvV", long_options
,
844 if (optarg
[0] == '0' && (optarg
[1] == 'x' || optarg
[1] == 'X'))
845 ncp
= (rc_uint_type
) strtol (optarg
+ 2, NULL
, 16);
847 ncp
= (rc_uint_type
) strtol (optarg
, NULL
, 10);
848 if (ncp
== CP_UTF16
|| ! unicode_is_valid_codepage (ncp
))
849 fatal (_("invalid codepage specified.\n"));
850 wind_default_codepage
= wind_current_codepage
= ncp
;
855 input_filename
= optarg
;
859 /* For compatibility with rc we accept "-fo <name>" as being the
860 equivalent of "-o <name>". We do not advertise this fact
861 though, as we do not want users to use non-GNU like command
864 fatal (_("invalid option -f\n"));
869 fatal (_("No filename following the -fo option.\n"));
870 optarg
= argv
[optind
++];
875 output_filename
= optarg
;
879 input_format
= format_from_name (optarg
, 1);
883 output_format
= format_from_name (optarg
, 1);
890 case OPTION_PREPROCESSOR
:
891 preprocessor
= optarg
;
894 case OPTION_PREPROCESSOR_ARG
:
895 if (preprocargs
== NULL
)
897 quotedarg
= quot (optarg
);
898 preprocargs
= xstrdup (quotedarg
);
904 quotedarg
= quot (optarg
);
905 n
= xmalloc (strlen (preprocargs
) + strlen (quotedarg
) + 2);
906 sprintf (n
, "%s %s", preprocargs
, quotedarg
);
914 if (preprocargs
== NULL
)
916 quotedarg
= quot (optarg
);
917 preprocargs
= xmalloc (strlen (quotedarg
) + 3);
918 sprintf (preprocargs
, "-%c%s", c
, quotedarg
);
924 quotedarg
= quot (optarg
);
925 n
= xmalloc (strlen (preprocargs
) + strlen (quotedarg
) + 4);
926 sprintf (n
, "%s -%c%s", preprocargs
, c
, quotedarg
);
933 /* Ignored for compatibility with rc. */
941 /* For backward compatibility, should be removed in the future. */
942 input_format_tmp
= format_from_name (optarg
, 0);
943 if (input_format_tmp
!= RES_FORMAT_UNKNOWN
)
948 if (stat (optarg
, & statbuf
) == 0
949 /* Coded this way to avoid importing knowledge of S_ISDIR into this file. */
950 && (mode_string (statbuf
.st_mode
, modebuf
), modebuf
[0] == 'd'))
951 /* We have a -I option with a directory name that just happens
952 to match a format name as well. eg: -I res Assume that the
953 user knows what they are doing and do not complain. */
958 _("Option -I is deprecated for setting the input format, please use -J instead.\n"));
959 input_format
= input_format_tmp
;
965 case OPTION_INCLUDE_DIR
:
966 if (preprocargs
== NULL
)
968 quotedarg
= quot (optarg
);
969 preprocargs
= xmalloc (strlen (quotedarg
) + 3);
970 sprintf (preprocargs
, "-I%s", quotedarg
);
976 quotedarg
= quot (optarg
);
977 n
= xmalloc (strlen (preprocargs
) + strlen (quotedarg
) + 4);
978 sprintf (n
, "%s -I%s", preprocargs
, quotedarg
);
983 windres_add_include_dir (optarg
);
988 language
= strtol (optarg
, (char **) NULL
, 16);
991 case OPTION_USE_TEMP_FILE
:
995 case OPTION_NO_USE_TEMP_FILE
:
1000 case OPTION_YYDEBUG
:
1011 print_version ("windres");
1020 if (input_filename
== NULL
&& optind
< argc
)
1022 input_filename
= argv
[optind
];
1026 if (output_filename
== NULL
&& optind
< argc
)
1028 output_filename
= argv
[optind
];
1035 if (input_format
== RES_FORMAT_UNKNOWN
)
1037 if (input_filename
== NULL
)
1038 input_format
= RES_FORMAT_RC
;
1040 input_format
= format_from_filename (input_filename
, 1);
1043 if (output_format
== RES_FORMAT_UNKNOWN
)
1045 if (output_filename
== NULL
)
1046 output_format
= RES_FORMAT_RC
;
1048 output_format
= format_from_filename (output_filename
, 0);
1051 set_endianness (NULL
, target
);
1053 /* Read the input file. */
1054 switch (input_format
)
1059 resources
= read_rc_file (input_filename
, preprocessor
, preprocargs
,
1060 language
, use_temp_file
);
1062 case RES_FORMAT_RES
:
1063 resources
= read_res_file (input_filename
);
1065 case RES_FORMAT_COFF
:
1066 resources
= read_coff_rsrc (input_filename
, target
);
1070 if (resources
== NULL
)
1071 fatal (_("no resources"));
1073 /* Sort the resources. This is required for COFF, convenient for
1074 rc, and unimportant for res. */
1075 resources
= sort_resources (resources
);
1077 /* Write the output file. */
1080 switch (output_format
)
1085 write_rc_file (output_filename
, resources
);
1087 case RES_FORMAT_RES
:
1088 write_res_file (output_filename
, resources
);
1090 case RES_FORMAT_COFF
:
1091 write_coff_file (output_filename
, target
, resources
);
1100 set_endianness (bfd
*abfd
, const char *target
)
1102 const bfd_target
*target_vec
;
1104 def_target_arch
= NULL
;
1105 target_vec
= bfd_get_target_info (target
, abfd
, &target_is_bigendian
, NULL
,
1108 fatal ("Can't detect target endianness and architecture.");
1109 if (! def_target_arch
)
1110 fatal ("Can't detect architecture.");
1114 windres_open_as_binary (const char *filename
, int rdmode
)
1118 abfd
= (rdmode
? bfd_openr (filename
, "binary") : bfd_openw (filename
, "binary"));
1120 fatal ("can't open `%s' for %s", filename
, (rdmode
? "input" : "output"));
1122 if (rdmode
&& ! bfd_check_format (abfd
, bfd_object
))
1123 fatal ("can't open `%s' for input.", filename
);
1129 set_windres_bfd_endianness (windres_bfd
*wrbfd
, int is_bigendian
)
1132 switch (WR_KIND(wrbfd
))
1134 case WR_KIND_BFD_BIN_L
:
1136 WR_KIND(wrbfd
) = WR_KIND_BFD_BIN_B
;
1138 case WR_KIND_BFD_BIN_B
:
1140 WR_KIND(wrbfd
) = WR_KIND_BFD_BIN_L
;
1143 /* only binary bfd can be overriden. */
1149 set_windres_bfd (windres_bfd
*wrbfd
, bfd
*abfd
, asection
*sec
, rc_uint_type kind
)
1154 case WR_KIND_TARGET
:
1159 case WR_KIND_BFD_BIN_L
:
1160 case WR_KIND_BFD_BIN_B
:
1167 WR_KIND(wrbfd
) = kind
;
1168 WR_BFD(wrbfd
) = abfd
;
1169 WR_SECTION(wrbfd
) = sec
;
1173 set_windres_bfd_content (windres_bfd
*wrbfd
, const void *data
, rc_uint_type off
,
1174 rc_uint_type length
)
1176 if (WR_KIND(wrbfd
) != WR_KIND_TARGET
)
1178 if (! bfd_set_section_contents (WR_BFD(wrbfd
), WR_SECTION(wrbfd
), data
, off
, length
))
1179 bfd_fatal ("bfd_set_section_contents");
1186 get_windres_bfd_content (windres_bfd
*wrbfd
, void *data
, rc_uint_type off
,
1187 rc_uint_type length
)
1189 if (WR_KIND(wrbfd
) != WR_KIND_TARGET
)
1191 if (! bfd_get_section_contents (WR_BFD(wrbfd
), WR_SECTION(wrbfd
), data
, off
, length
))
1192 bfd_fatal ("bfd_get_section_contents");
1199 windres_put_8 (windres_bfd
*wrbfd
, void *p
, rc_uint_type value
)
1201 switch (WR_KIND(wrbfd
))
1203 case WR_KIND_TARGET
:
1204 target_put_8 (p
, value
);
1207 case WR_KIND_BFD_BIN_L
:
1208 case WR_KIND_BFD_BIN_B
:
1209 bfd_put_8 (WR_BFD(wrbfd
), value
, p
);
1217 windres_put_16 (windres_bfd
*wrbfd
, void *data
, rc_uint_type value
)
1219 switch (WR_KIND(wrbfd
))
1221 case WR_KIND_TARGET
:
1222 target_put_16 (data
, value
);
1225 case WR_KIND_BFD_BIN_B
:
1226 bfd_put_16 (WR_BFD(wrbfd
), value
, data
);
1228 case WR_KIND_BFD_BIN_L
:
1229 bfd_putl16 (value
, data
);
1237 windres_put_32 (windres_bfd
*wrbfd
, void *data
, rc_uint_type value
)
1239 switch (WR_KIND(wrbfd
))
1241 case WR_KIND_TARGET
:
1242 target_put_32 (data
, value
);
1245 case WR_KIND_BFD_BIN_B
:
1246 bfd_put_32 (WR_BFD(wrbfd
), value
, data
);
1248 case WR_KIND_BFD_BIN_L
:
1249 bfd_putl32 (value
, data
);
1257 windres_get_8 (windres_bfd
*wrbfd
, const void *data
, rc_uint_type length
)
1260 fatal ("windres_get_8: unexpected eob.");
1261 switch (WR_KIND(wrbfd
))
1263 case WR_KIND_TARGET
:
1264 return target_get_8 (data
, length
);
1266 case WR_KIND_BFD_BIN_B
:
1267 case WR_KIND_BFD_BIN_L
:
1268 return bfd_get_8 (WR_BFD(wrbfd
), data
);
1276 windres_get_16 (windres_bfd
*wrbfd
, const void *data
, rc_uint_type length
)
1279 fatal ("windres_get_16: unexpected eob.");
1280 switch (WR_KIND(wrbfd
))
1282 case WR_KIND_TARGET
:
1283 return target_get_16 (data
, length
);
1285 case WR_KIND_BFD_BIN_B
:
1286 return bfd_get_16 (WR_BFD(wrbfd
), data
);
1287 case WR_KIND_BFD_BIN_L
:
1288 return bfd_getl16 (data
);
1296 windres_get_32 (windres_bfd
*wrbfd
, const void *data
, rc_uint_type length
)
1299 fatal ("windres_get_32: unexpected eob.");
1300 switch (WR_KIND(wrbfd
))
1302 case WR_KIND_TARGET
:
1303 return target_get_32 (data
, length
);
1305 case WR_KIND_BFD_BIN_B
:
1306 return bfd_get_32 (WR_BFD(wrbfd
), data
);
1307 case WR_KIND_BFD_BIN_L
:
1308 return bfd_getl32 (data
);
1316 target_get_8 (const void *p
, rc_uint_type length
)
1321 fatal ("Resource too small for getting 8-bit value.");
1323 ret
= (rc_uint_type
) *((const bfd_byte
*) p
);
1328 target_get_16 (const void *p
, rc_uint_type length
)
1331 fatal ("Resource too small for getting 16-bit value.");
1333 if (target_is_bigendian
)
1334 return bfd_getb16 (p
);
1336 return bfd_getl16 (p
);
1340 target_get_32 (const void *p
, rc_uint_type length
)
1343 fatal ("Resource too small for getting 32-bit value.");
1345 if (target_is_bigendian
)
1346 return bfd_getb32 (p
);
1348 return bfd_getl32 (p
);
1352 target_put_8 (void *p
, rc_uint_type value
)
1355 *((bfd_byte
*) p
)=(bfd_byte
) value
;
1359 target_put_16 (void *p
, rc_uint_type value
)
1363 if (target_is_bigendian
)
1364 bfd_putb16 (value
, p
);
1366 bfd_putl16 (value
, p
);
1370 target_put_32 (void *p
, rc_uint_type value
)
1374 if (target_is_bigendian
)
1375 bfd_putb32 (value
, p
);
1377 bfd_putl32 (value
, p
);
1380 static int isInComment
= 0;
1382 int wr_printcomment (FILE *e
, const char *fmt
, ...)
1388 r
+= fprintf (e
, "\n ");
1394 va_start (arg
, fmt
);
1395 r
+= vfprintf (e
, fmt
, arg
);
1400 int wr_print (FILE *e
, const char *fmt
, ...)
1405 r
+= fprintf (e
, ". */\n");
1409 va_start (arg
, fmt
);
1410 r
+= vfprintf (e
, fmt
, arg
);