1 /* windres.c -- a program to manipulate Windows resources
2 Copyright 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2007, 2008,
3 2009 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"
50 /* Used by resrc.c at least. */
54 int target_is_bigendian
= 0;
55 const char *def_target_arch
;
57 static void set_endianess (bfd
*, const char *);
59 /* An enumeration of format types. */
65 /* Textual RC file. */
67 /* Binary RES file. */
73 /* A structure used to map between format types and strings. */
78 enum res_format format
;
81 /* A mapping between names and format types. */
83 static const struct format_map format_names
[] =
85 { "rc", RES_FORMAT_RC
},
86 { "res", RES_FORMAT_RES
},
87 { "coff", RES_FORMAT_COFF
},
88 { NULL
, RES_FORMAT_UNKNOWN
}
91 /* A mapping from file extensions to format types. */
93 static const struct format_map format_fileexts
[] =
95 { "rc", RES_FORMAT_RC
},
96 { "res", RES_FORMAT_RES
},
97 { "exe", RES_FORMAT_COFF
},
98 { "obj", RES_FORMAT_COFF
},
99 { "o", RES_FORMAT_COFF
},
100 { NULL
, RES_FORMAT_UNKNOWN
}
103 /* A list of include directories. */
107 struct include_dir
*next
;
111 static struct include_dir
*include_dirs
;
113 /* Static functions. */
115 static void res_init (void);
116 static int extended_menuitems (const rc_menuitem
*);
117 static enum res_format
format_from_name (const char *, int);
118 static enum res_format
format_from_filename (const char *, int);
119 static void usage (FILE *, int);
120 static int cmp_res_entry (const void *, const void *);
121 static rc_res_directory
*sort_resources (rc_res_directory
*);
122 static void reswr_init (void);
123 static const char * quot (const char *);
125 static rc_uint_type
target_get_8 (const void *, rc_uint_type
);
126 static void target_put_8 (void *, rc_uint_type
);
127 static rc_uint_type
target_get_16 (const void *, rc_uint_type
);
128 static void target_put_16 (void *, rc_uint_type
);
129 static rc_uint_type
target_get_32 (const void *, rc_uint_type
);
130 static void target_put_32 (void *, rc_uint_type
);
133 /* When we are building a resource tree, we allocate everything onto
134 an obstack, so that we can free it all at once if we want. */
136 #define obstack_chunk_alloc xmalloc
137 #define obstack_chunk_free free
139 /* The resource building obstack. */
141 static struct obstack res_obstack
;
143 /* Initialize the resource building obstack. */
148 obstack_init (&res_obstack
);
151 /* Allocate space on the resource building obstack. */
154 res_alloc (rc_uint_type bytes
)
156 return obstack_alloc (&res_obstack
, (size_t) bytes
);
159 /* We also use an obstack to save memory used while writing out a set
162 static struct obstack reswr_obstack
;
164 /* Initialize the resource writing obstack. */
169 obstack_init (&reswr_obstack
);
172 /* Allocate space on the resource writing obstack. */
175 reswr_alloc (rc_uint_type bytes
)
177 return obstack_alloc (&reswr_obstack
, (size_t) bytes
);
180 /* Open a file using the include directory search list. */
183 open_file_search (const char *filename
, const char *mode
, const char *errmsg
,
184 char **real_filename
)
187 struct include_dir
*d
;
189 e
= fopen (filename
, mode
);
192 *real_filename
= xstrdup (filename
);
198 for (d
= include_dirs
; d
!= NULL
; d
= d
->next
)
202 n
= (char *) xmalloc (strlen (d
->dir
) + strlen (filename
) + 2);
203 sprintf (n
, "%s/%s", d
->dir
, filename
);
216 fatal (_("can't open %s `%s': %s"), errmsg
, filename
, strerror (errno
));
218 /* Return a value to avoid a compiler warning. */
222 /* Compare two resource ID's. We consider name entries to come before
223 numeric entries, because that is how they appear in the COFF .rsrc
227 res_id_cmp (rc_res_id a
, rc_res_id b
)
235 else if (a
.u
.id
< b
.u
.id
)
242 unichar
*as
, *ase
, *bs
, *bse
;
248 ase
= as
+ a
.u
.n
.length
;
250 bse
= bs
+ b
.u
.n
.length
;
258 i
= (int) *as
- (int) *bs
;
272 /* Print a resource ID. */
275 res_id_print (FILE *stream
, rc_res_id id
, int quote
)
278 fprintf (stream
, "%u", (int) id
.u
.id
);
282 unicode_print_quoted (stream
, id
.u
.n
.name
, id
.u
.n
.length
);
284 unicode_print (stream
, id
.u
.n
.name
, id
.u
.n
.length
);
288 /* Print a list of resource ID's. */
291 res_ids_print (FILE *stream
, int cids
, const rc_res_id
*ids
)
295 for (i
= 0; i
< cids
; i
++)
297 res_id_print (stream
, ids
[i
], 1);
299 fprintf (stream
, ": ");
303 /* Convert an ASCII string to a resource ID. */
306 res_string_to_id (rc_res_id
*res_id
, const char *string
)
309 unicode_from_ascii (&res_id
->u
.n
.length
, &res_id
->u
.n
.name
, string
);
312 /* Convert an unicode string to a resource ID. */
314 res_unistring_to_id (rc_res_id
*res_id
, const unichar
*u
)
317 res_id
->u
.n
.length
= unichar_len (u
);
318 res_id
->u
.n
.name
= unichar_dup_uppercase (u
);
321 /* Define a resource. The arguments are the resource tree, RESOURCES,
322 and the location at which to put it in the tree, CIDS and IDS.
323 This returns a newly allocated rc_res_resource structure, which the
324 caller is expected to initialize. If DUPOK is non-zero, then if a
325 resource with this ID exists, it is returned. Otherwise, a warning
326 is issued, and a new resource is created replacing the existing
330 define_resource (rc_res_directory
**resources
, int cids
,
331 const rc_res_id
*ids
, int dupok
)
333 rc_res_entry
*re
= NULL
;
337 for (i
= 0; i
< cids
; i
++)
341 if (*resources
== NULL
)
343 static unsigned int timeval
;
345 /* Use the same timestamp for every resource created in a
348 timeval
= time (NULL
);
350 *resources
= ((rc_res_directory
*)
351 res_alloc (sizeof (rc_res_directory
)));
352 (*resources
)->characteristics
= 0;
353 (*resources
)->time
= timeval
;
354 (*resources
)->major
= 0;
355 (*resources
)->minor
= 0;
356 (*resources
)->entries
= NULL
;
359 for (pp
= &(*resources
)->entries
; *pp
!= NULL
; pp
= &(*pp
)->next
)
360 if (res_id_cmp ((*pp
)->id
, ids
[i
]) == 0)
367 re
= (rc_res_entry
*) res_alloc (sizeof (rc_res_entry
));
388 fprintf (stderr
, "%s: ", program_name
);
389 res_ids_print (stderr
, i
, ids
);
390 fprintf (stderr
, _(": expected to be a directory\n"));
394 resources
= &re
->u
.dir
;
400 fprintf (stderr
, "%s: ", program_name
);
401 res_ids_print (stderr
, cids
, ids
);
402 fprintf (stderr
, _(": expected to be a leaf\n"));
406 if (re
->u
.res
!= NULL
)
411 fprintf (stderr
, _("%s: warning: "), program_name
);
412 res_ids_print (stderr
, cids
, ids
);
413 fprintf (stderr
, _(": duplicate value\n"));
416 re
->u
.res
= ((rc_res_resource
*)
417 res_alloc (sizeof (rc_res_resource
)));
418 memset (re
->u
.res
, 0, sizeof (rc_res_resource
));
420 re
->u
.res
->type
= RES_TYPE_UNINITIALIZED
;
424 /* Define a standard resource. This is a version of define_resource
425 that just takes type, name, and language arguments. */
428 define_standard_resource (rc_res_directory
**resources
, int type
,
429 rc_res_id name
, rc_uint_type language
, int dupok
)
437 a
[2].u
.id
= language
;
438 return define_resource (resources
, 3, a
, dupok
);
441 /* Comparison routine for resource sorting. */
444 cmp_res_entry (const void *p1
, const void *p2
)
446 const rc_res_entry
**re1
, **re2
;
448 re1
= (const rc_res_entry
**) p1
;
449 re2
= (const rc_res_entry
**) p2
;
450 return res_id_cmp ((*re1
)->id
, (*re2
)->id
);
453 /* Sort the resources. */
455 static rc_res_directory
*
456 sort_resources (rc_res_directory
*resdir
)
462 if (resdir
->entries
== NULL
)
466 for (re
= resdir
->entries
; re
!= NULL
; re
= re
->next
)
469 /* This is a recursive routine, so using xmalloc is probably better
471 a
= (rc_res_entry
**) xmalloc (c
* sizeof (rc_res_entry
*));
473 for (i
= 0, re
= resdir
->entries
; re
!= NULL
; re
= re
->next
, i
++)
476 qsort (a
, c
, sizeof (rc_res_entry
*), cmp_res_entry
);
478 resdir
->entries
= a
[0];
479 for (i
= 0; i
< c
- 1; i
++)
480 a
[i
]->next
= a
[i
+ 1];
485 /* Now sort the subdirectories. */
487 for (re
= resdir
->entries
; re
!= NULL
; re
= re
->next
)
489 re
->u
.dir
= sort_resources (re
->u
.dir
);
494 /* Return whether the dialog resource DIALOG is a DIALOG or a
498 extended_dialog (const rc_dialog
*dialog
)
500 const rc_dialog_control
*c
;
502 if (dialog
->ex
!= NULL
)
505 for (c
= dialog
->controls
; c
!= NULL
; c
= c
->next
)
506 if (c
->data
!= NULL
|| c
->help
!= 0)
512 /* Return whether MENUITEMS are a MENU or a MENUEX. */
515 extended_menu (const rc_menu
*menu
)
517 return extended_menuitems (menu
->items
);
521 extended_menuitems (const rc_menuitem
*menuitems
)
523 const rc_menuitem
*mi
;
525 for (mi
= menuitems
; mi
!= NULL
; mi
= mi
->next
)
527 if (mi
->help
!= 0 || mi
->state
!= 0)
529 if (mi
->popup
!= NULL
&& mi
->id
!= 0)
532 & ~ (MENUITEM_CHECKED
536 | MENUITEM_MENUBARBREAK
537 | MENUITEM_MENUBREAK
))
540 if (mi
->popup
!= NULL
)
542 if (extended_menuitems (mi
->popup
))
550 /* Convert a string to a format type, or exit if it can't be done. */
552 static enum res_format
553 format_from_name (const char *name
, int exit_on_error
)
555 const struct format_map
*m
;
557 for (m
= format_names
; m
->name
!= NULL
; m
++)
558 if (strcasecmp (m
->name
, name
) == 0)
561 if (m
->name
== NULL
&& exit_on_error
)
563 non_fatal (_("unknown format type `%s'"), name
);
564 fprintf (stderr
, _("%s: supported formats:"), program_name
);
565 for (m
= format_names
; m
->name
!= NULL
; m
++)
566 fprintf (stderr
, " %s", m
->name
);
567 fprintf (stderr
, "\n");
574 /* Work out a format type given a file name. If INPUT is non-zero,
575 it's OK to look at the file itself. */
577 static enum res_format
578 format_from_filename (const char *filename
, int input
)
582 bfd_byte b1
, b2
, b3
, b4
, b5
;
585 /* If we have an extension, see if we recognize it as implying a
586 particular format. */
587 ext
= strrchr (filename
, '.');
590 const struct format_map
*m
;
593 for (m
= format_fileexts
; m
->name
!= NULL
; m
++)
594 if (strcasecmp (m
->name
, ext
) == 0)
598 /* If we don't recognize the name of an output file, assume it's a
601 return RES_FORMAT_COFF
;
603 /* Read the first few bytes of the file to see if we can guess what
605 e
= fopen (filename
, FOPEN_RB
);
607 fatal ("%s: %s", filename
, strerror (errno
));
617 /* A PE executable starts with 0x4d 0x5a. */
618 if (b1
== 0x4d && b2
== 0x5a)
619 return RES_FORMAT_COFF
;
621 /* A COFF .o file starts with a COFF magic number. */
622 magic
= (b2
<< 8) | b1
;
625 case 0x14c: /* i386 */
626 case 0x166: /* MIPS */
627 case 0x184: /* Alpha */
628 case 0x268: /* 68k */
629 case 0x1f0: /* PowerPC */
631 return RES_FORMAT_COFF
;
634 /* A RES file starts with 0x0 0x0 0x0 0x0 0x20 0x0 0x0 0x0. */
635 if (b1
== 0 && b2
== 0 && b3
== 0 && b4
== 0 && b5
== 0x20)
636 return RES_FORMAT_RES
;
638 /* If every character is printable or space, assume it's an RC file. */
639 if ((ISPRINT (b1
) || ISSPACE (b1
))
640 && (ISPRINT (b2
) || ISSPACE (b2
))
641 && (ISPRINT (b3
) || ISSPACE (b3
))
642 && (ISPRINT (b4
) || ISSPACE (b4
))
643 && (ISPRINT (b5
) || ISSPACE (b5
)))
644 return RES_FORMAT_RC
;
646 /* Otherwise, we give up. */
647 fatal (_("can not determine type of file `%s'; use the -J option"),
650 /* Return something to silence the compiler warning. */
651 return RES_FORMAT_UNKNOWN
;
654 /* Print a usage message and exit. */
657 usage (FILE *stream
, int status
)
659 fprintf (stream
, _("Usage: %s [option(s)] [input-file] [output-file]\n"),
661 fprintf (stream
, _(" The options are:\n\
662 -i --input=<file> Name input file\n\
663 -o --output=<file> Name output file\n\
664 -J --input-format=<format> Specify input format\n\
665 -O --output-format=<format> Specify output format\n\
666 -F --target=<target> Specify COFF target\n\
667 --preprocessor=<program> Program to use to preprocess rc file\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
,
740 static const struct option long_options
[] =
742 {"input", required_argument
, 0, 'i'},
743 {"output", required_argument
, 0, 'o'},
744 {"input-format", required_argument
, 0, 'J'},
745 {"output-format", required_argument
, 0, 'O'},
746 {"target", required_argument
, 0, 'F'},
747 {"preprocessor", required_argument
, 0, OPTION_PREPROCESSOR
},
748 {"include-dir", required_argument
, 0, OPTION_INCLUDE_DIR
},
749 {"define", required_argument
, 0, 'D'},
750 {"undefine", required_argument
, 0, 'U'},
751 {"verbose", no_argument
, 0, 'v'},
752 {"codepage", required_argument
, 0, 'c'},
753 {"language", required_argument
, 0, 'l'},
754 {"use-temp-file", no_argument
, 0, OPTION_USE_TEMP_FILE
},
755 {"no-use-temp-file", no_argument
, 0, OPTION_NO_USE_TEMP_FILE
},
756 {"yydebug", no_argument
, 0, OPTION_YYDEBUG
},
757 {"version", no_argument
, 0, 'V'},
758 {"help", no_argument
, 0, 'h'},
759 {0, no_argument
, 0, 0}
763 windres_add_include_dir (const char *p
)
765 struct include_dir
*n
, **pp
;
767 /* Computing paths is often complicated and error prone.
768 The easiest way to check for mistakes is at the time
769 we add them to include_dirs. */
773 n
= xmalloc (sizeof *n
);
775 n
->dir
= (char * ) p
;
777 for (pp
= &include_dirs
; *pp
!= NULL
; pp
= &(*pp
)->next
)
782 /* This keeps gcc happy when using -Wmissing-prototypes -Wstrict-prototypes. */
783 int main (int, char **);
785 /* The main function. */
788 main (int argc
, char **argv
)
791 char *input_filename
;
792 char *output_filename
;
793 enum res_format input_format
;
794 enum res_format input_format_tmp
;
795 enum res_format output_format
;
799 const char *quotedarg
;
801 rc_res_directory
*resources
;
804 #if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
805 setlocale (LC_MESSAGES
, "");
807 #if defined (HAVE_SETLOCALE)
808 setlocale (LC_CTYPE
, "");
810 bindtextdomain (PACKAGE
, LOCALEDIR
);
811 textdomain (PACKAGE
);
813 program_name
= argv
[0];
814 xmalloc_set_program_name (program_name
);
816 expandargv (&argc
, &argv
);
819 set_default_bfd_target ();
823 input_filename
= NULL
;
824 output_filename
= NULL
;
825 input_format
= RES_FORMAT_UNKNOWN
;
826 output_format
= RES_FORMAT_UNKNOWN
;
830 language
= 0x409; /* LANG_ENGLISH, SUBLANG_ENGLISH_US. */
833 while ((c
= getopt_long (argc
, argv
, "c:f:i:l:o:I:J:O:F:D:U:rhHvV", long_options
,
842 if (optarg
[0] == '0' && (optarg
[1] == 'x' || optarg
[1] == 'X'))
843 ncp
= (rc_uint_type
) strtol (optarg
+ 2, NULL
, 16);
845 ncp
= (rc_uint_type
) strtol (optarg
, NULL
, 10);
846 if (ncp
== CP_UTF16
|| ! unicode_is_valid_codepage (ncp
))
847 fatal (_("invalid codepage specified.\n"));
848 wind_default_codepage
= wind_current_codepage
= ncp
;
853 input_filename
= optarg
;
857 /* For compatibility with rc we accept "-fo <name>" as being the
858 equivalent of "-o <name>". We do not advertise this fact
859 though, as we do not want users to use non-GNU like command
862 fatal (_("invalid option -f\n"));
867 fatal (_("No filename following the -fo option.\n"));
868 optarg
= argv
[optind
++];
873 output_filename
= optarg
;
877 input_format
= format_from_name (optarg
, 1);
881 output_format
= format_from_name (optarg
, 1);
888 case OPTION_PREPROCESSOR
:
889 preprocessor
= optarg
;
894 if (preprocargs
== NULL
)
896 quotedarg
= quot (optarg
);
897 preprocargs
= xmalloc (strlen (quotedarg
) + 3);
898 sprintf (preprocargs
, "-%c%s", c
, quotedarg
);
904 quotedarg
= quot (optarg
);
905 n
= xmalloc (strlen (preprocargs
) + strlen (quotedarg
) + 4);
906 sprintf (n
, "%s -%c%s", preprocargs
, c
, quotedarg
);
913 /* Ignored for compatibility with rc. */
921 /* For backward compatibility, should be removed in the future. */
922 input_format_tmp
= format_from_name (optarg
, 0);
923 if (input_format_tmp
!= RES_FORMAT_UNKNOWN
)
928 if (stat (optarg
, & statbuf
) == 0
929 /* Coded this way to avoid importing knowledge of S_ISDIR into this file. */
930 && (mode_string (statbuf
.st_mode
, modebuf
), modebuf
[0] == 'd'))
931 /* We have a -I option with a directory name that just happens
932 to match a format name as well. eg: -I res Assume that the
933 user knows what they are doing and do not complain. */
938 _("Option -I is deprecated for setting the input format, please use -J instead.\n"));
939 input_format
= input_format_tmp
;
945 case OPTION_INCLUDE_DIR
:
946 if (preprocargs
== NULL
)
948 quotedarg
= quot (optarg
);
949 preprocargs
= xmalloc (strlen (quotedarg
) + 3);
950 sprintf (preprocargs
, "-I%s", quotedarg
);
956 quotedarg
= quot (optarg
);
957 n
= xmalloc (strlen (preprocargs
) + strlen (quotedarg
) + 4);
958 sprintf (n
, "%s -I%s", preprocargs
, quotedarg
);
963 windres_add_include_dir (optarg
);
968 language
= strtol (optarg
, (char **) NULL
, 16);
971 case OPTION_USE_TEMP_FILE
:
975 case OPTION_NO_USE_TEMP_FILE
:
991 print_version ("windres");
1000 if (input_filename
== NULL
&& optind
< argc
)
1002 input_filename
= argv
[optind
];
1006 if (output_filename
== NULL
&& optind
< argc
)
1008 output_filename
= argv
[optind
];
1015 if (input_format
== RES_FORMAT_UNKNOWN
)
1017 if (input_filename
== NULL
)
1018 input_format
= RES_FORMAT_RC
;
1020 input_format
= format_from_filename (input_filename
, 1);
1023 if (output_format
== RES_FORMAT_UNKNOWN
)
1025 if (output_filename
== NULL
)
1026 output_format
= RES_FORMAT_RC
;
1028 output_format
= format_from_filename (output_filename
, 0);
1031 set_endianess (NULL
, target
);
1033 /* Read the input file. */
1034 switch (input_format
)
1039 resources
= read_rc_file (input_filename
, preprocessor
, preprocargs
,
1040 language
, use_temp_file
);
1042 case RES_FORMAT_RES
:
1043 resources
= read_res_file (input_filename
);
1045 case RES_FORMAT_COFF
:
1046 resources
= read_coff_rsrc (input_filename
, target
);
1050 if (resources
== NULL
)
1051 fatal (_("no resources"));
1053 /* Sort the resources. This is required for COFF, convenient for
1054 rc, and unimportant for res. */
1055 resources
= sort_resources (resources
);
1057 /* Write the output file. */
1060 switch (output_format
)
1065 write_rc_file (output_filename
, resources
);
1067 case RES_FORMAT_RES
:
1068 write_res_file (output_filename
, resources
);
1070 case RES_FORMAT_COFF
:
1071 write_coff_file (output_filename
, target
, resources
);
1080 set_endianess (bfd
*abfd
, const char *target
)
1082 const bfd_target
*target_vec
;
1084 def_target_arch
= NULL
;
1085 target_vec
= bfd_get_target_info (target
, abfd
, &target_is_bigendian
, NULL
,
1088 fatal ("Can't detect target endianess and architecture.");
1089 if (! def_target_arch
)
1090 fatal ("Can't detect architecture.");
1094 windres_open_as_binary (const char *filename
, int rdmode
)
1098 abfd
= (rdmode
? bfd_openr (filename
, "binary") : bfd_openw (filename
, "binary"));
1100 fatal ("can't open `%s' for %s", filename
, (rdmode
? "input" : "output"));
1102 if (rdmode
&& ! bfd_check_format (abfd
, bfd_object
))
1103 fatal ("can't open `%s' for input.", filename
);
1109 set_windres_bfd_endianess (windres_bfd
*wrbfd
, int is_bigendian
)
1112 switch (WR_KIND(wrbfd
))
1114 case WR_KIND_BFD_BIN_L
:
1116 WR_KIND(wrbfd
) = WR_KIND_BFD_BIN_B
;
1118 case WR_KIND_BFD_BIN_B
:
1120 WR_KIND(wrbfd
) = WR_KIND_BFD_BIN_L
;
1123 /* only binary bfd can be overriden. */
1129 set_windres_bfd (windres_bfd
*wrbfd
, bfd
*abfd
, asection
*sec
, rc_uint_type kind
)
1134 case WR_KIND_TARGET
:
1139 case WR_KIND_BFD_BIN_L
:
1140 case WR_KIND_BFD_BIN_B
:
1147 WR_KIND(wrbfd
) = kind
;
1148 WR_BFD(wrbfd
) = abfd
;
1149 WR_SECTION(wrbfd
) = sec
;
1153 set_windres_bfd_content (windres_bfd
*wrbfd
, const void *data
, rc_uint_type off
,
1154 rc_uint_type length
)
1156 if (WR_KIND(wrbfd
) != WR_KIND_TARGET
)
1158 if (! bfd_set_section_contents (WR_BFD(wrbfd
), WR_SECTION(wrbfd
), data
, off
, length
))
1159 bfd_fatal ("bfd_set_section_contents");
1166 get_windres_bfd_content (windres_bfd
*wrbfd
, void *data
, rc_uint_type off
,
1167 rc_uint_type length
)
1169 if (WR_KIND(wrbfd
) != WR_KIND_TARGET
)
1171 if (! bfd_get_section_contents (WR_BFD(wrbfd
), WR_SECTION(wrbfd
), data
, off
, length
))
1172 bfd_fatal ("bfd_get_section_contents");
1179 windres_put_8 (windres_bfd
*wrbfd
, void *p
, rc_uint_type value
)
1181 switch (WR_KIND(wrbfd
))
1183 case WR_KIND_TARGET
:
1184 target_put_8 (p
, value
);
1187 case WR_KIND_BFD_BIN_L
:
1188 case WR_KIND_BFD_BIN_B
:
1189 bfd_put_8 (WR_BFD(wrbfd
), value
, p
);
1197 windres_put_16 (windres_bfd
*wrbfd
, void *data
, rc_uint_type value
)
1199 switch (WR_KIND(wrbfd
))
1201 case WR_KIND_TARGET
:
1202 target_put_16 (data
, value
);
1205 case WR_KIND_BFD_BIN_B
:
1206 bfd_put_16 (WR_BFD(wrbfd
), value
, data
);
1208 case WR_KIND_BFD_BIN_L
:
1209 bfd_putl16 (value
, data
);
1217 windres_put_32 (windres_bfd
*wrbfd
, void *data
, rc_uint_type value
)
1219 switch (WR_KIND(wrbfd
))
1221 case WR_KIND_TARGET
:
1222 target_put_32 (data
, value
);
1225 case WR_KIND_BFD_BIN_B
:
1226 bfd_put_32 (WR_BFD(wrbfd
), value
, data
);
1228 case WR_KIND_BFD_BIN_L
:
1229 bfd_putl32 (value
, data
);
1237 windres_get_8 (windres_bfd
*wrbfd
, const void *data
, rc_uint_type length
)
1240 fatal ("windres_get_8: unexpected eob.");
1241 switch (WR_KIND(wrbfd
))
1243 case WR_KIND_TARGET
:
1244 return target_get_8 (data
, length
);
1246 case WR_KIND_BFD_BIN_B
:
1247 case WR_KIND_BFD_BIN_L
:
1248 return bfd_get_8 (WR_BFD(wrbfd
), data
);
1256 windres_get_16 (windres_bfd
*wrbfd
, const void *data
, rc_uint_type length
)
1259 fatal ("windres_get_16: unexpected eob.");
1260 switch (WR_KIND(wrbfd
))
1262 case WR_KIND_TARGET
:
1263 return target_get_16 (data
, length
);
1265 case WR_KIND_BFD_BIN_B
:
1266 return bfd_get_16 (WR_BFD(wrbfd
), data
);
1267 case WR_KIND_BFD_BIN_L
:
1268 return bfd_getl16 (data
);
1276 windres_get_32 (windres_bfd
*wrbfd
, const void *data
, rc_uint_type length
)
1279 fatal ("windres_get_32: unexpected eob.");
1280 switch (WR_KIND(wrbfd
))
1282 case WR_KIND_TARGET
:
1283 return target_get_32 (data
, length
);
1285 case WR_KIND_BFD_BIN_B
:
1286 return bfd_get_32 (WR_BFD(wrbfd
), data
);
1287 case WR_KIND_BFD_BIN_L
:
1288 return bfd_getl32 (data
);
1296 target_get_8 (const void *p
, rc_uint_type length
)
1301 fatal ("Resource too small for getting 8-bit value.");
1303 ret
= (rc_uint_type
) *((const bfd_byte
*) p
);
1308 target_get_16 (const void *p
, rc_uint_type length
)
1311 fatal ("Resource too small for getting 16-bit value.");
1313 if (target_is_bigendian
)
1314 return bfd_getb16 (p
);
1316 return bfd_getl16 (p
);
1320 target_get_32 (const void *p
, rc_uint_type length
)
1323 fatal ("Resource too small for getting 32-bit value.");
1325 if (target_is_bigendian
)
1326 return bfd_getb32 (p
);
1328 return bfd_getl32 (p
);
1332 target_put_8 (void *p
, rc_uint_type value
)
1335 *((bfd_byte
*) p
)=(bfd_byte
) value
;
1339 target_put_16 (void *p
, rc_uint_type value
)
1343 if (target_is_bigendian
)
1344 bfd_putb16 (value
, p
);
1346 bfd_putl16 (value
, p
);
1350 target_put_32 (void *p
, rc_uint_type value
)
1354 if (target_is_bigendian
)
1355 bfd_putb32 (value
, p
);
1357 bfd_putl32 (value
, p
);
1360 static int isInComment
= 0;
1362 int wr_printcomment (FILE *e
, const char *fmt
, ...)
1368 r
+= fprintf (e
, "\n ");
1374 va_start (arg
, fmt
);
1375 r
+= vfprintf (e
, fmt
, arg
);
1380 int wr_print (FILE *e
, const char *fmt
, ...)
1385 r
+= fprintf (e
, ". */\n");
1389 va_start (arg
, fmt
);
1390 r
+= vfprintf (e
, fmt
, arg
);