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"
49 /* Used by resrc.c at least. */
53 int target_is_bigendian
= 0;
54 const char *def_target_arch
;
56 static void set_endianess (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 -I --include-dir=<dir> Include directory when preprocessing rc file\n\
668 -D --define <sym>[=<val>] Define SYM when preprocessing rc file\n\
669 -U --undefine <sym> Undefine SYM when preprocessing rc file\n\
670 -v --verbose Verbose - tells you what it's doing\n\
671 -c --codepage=<codepage> Specify default codepage\n\
672 -l --language=<val> Set language when reading rc file\n\
673 --use-temp-file Use a temporary file instead of popen to read\n\
674 the preprocessor output\n\
675 --no-use-temp-file Use popen (default)\n"));
677 fprintf (stream
, _("\
678 --yydebug Turn on parser debugging\n"));
680 fprintf (stream
, _("\
681 -r Ignored for compatibility with rc\n\
682 @<file> Read options from <file>\n\
683 -h --help Print this help message\n\
684 -V --version Print version information\n"));
685 fprintf (stream
, _("\
686 FORMAT is one of rc, res, or coff, and is deduced from the file name\n\
687 extension if not specified. A single file name is an input file.\n\
688 No input-file is stdin, default rc. No output-file is stdout, default rc.\n"));
690 list_supported_targets (program_name
, stream
);
692 if (REPORT_BUGS_TO
[0] && status
== 0)
693 fprintf (stream
, _("Report bugs to %s\n"), REPORT_BUGS_TO
);
698 /* Quote characters that will confuse the shell when we run the preprocessor. */
701 quot (const char *string
)
703 static char *buf
= 0;
704 static int buflen
= 0;
705 int slen
= strlen (string
);
709 if ((buflen
< slen
* 2 + 2) || ! buf
)
711 buflen
= slen
* 2 + 2;
714 buf
= (char *) xmalloc (buflen
);
717 for (src
=string
, dest
=buf
; *src
; src
++, dest
++)
719 if (*src
== '(' || *src
== ')' || *src
== ' ')
729 /* 150 isn't special; it's just an arbitrary non-ASCII char value. */
731 #define OPTION_PREPROCESSOR 150
732 #define OPTION_USE_TEMP_FILE (OPTION_PREPROCESSOR + 1)
733 #define OPTION_NO_USE_TEMP_FILE (OPTION_USE_TEMP_FILE + 1)
734 #define OPTION_YYDEBUG (OPTION_NO_USE_TEMP_FILE + 1)
736 static const struct option long_options
[] =
738 {"input", required_argument
, 0, 'i'},
739 {"output", required_argument
, 0, 'o'},
740 {"input-format", required_argument
, 0, 'J'},
741 {"output-format", required_argument
, 0, 'O'},
742 {"target", required_argument
, 0, 'F'},
743 {"preprocessor", required_argument
, 0, OPTION_PREPROCESSOR
},
744 {"include-dir", required_argument
, 0, 'I'},
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
);
812 expandargv (&argc
, &argv
);
815 set_default_bfd_target ();
819 input_filename
= NULL
;
820 output_filename
= NULL
;
821 input_format
= RES_FORMAT_UNKNOWN
;
822 output_format
= RES_FORMAT_UNKNOWN
;
826 language
= 0x409; /* LANG_ENGLISH, SUBLANG_ENGLISH_US. */
829 while ((c
= getopt_long (argc
, argv
, "c:f:i:l:o:I:J:O:F:D:U:rhHvV", long_options
,
838 if (optarg
[0] == '0' && (optarg
[1] == 'x' || optarg
[1] == 'X'))
839 ncp
= (rc_uint_type
) strtol (optarg
+ 2, NULL
, 16);
841 ncp
= (rc_uint_type
) strtol (optarg
, NULL
, 10);
842 if (ncp
== CP_UTF16
|| ! unicode_is_valid_codepage (ncp
))
843 fatal (_("invalid codepage specified.\n"));
844 wind_default_codepage
= wind_current_codepage
= ncp
;
849 input_filename
= optarg
;
853 /* For compatibility with rc we accept "-fo <name>" as being the
854 equivalent of "-o <name>". We do not advertise this fact
855 though, as we do not want users to use non-GNU like command
858 fatal (_("invalid option -f\n"));
863 fatal (_("No filename following the -fo option.\n"));
864 optarg
= argv
[optind
++];
869 output_filename
= optarg
;
873 input_format
= format_from_name (optarg
, 1);
877 output_format
= format_from_name (optarg
, 1);
884 case OPTION_PREPROCESSOR
:
885 preprocessor
= optarg
;
890 if (preprocargs
== NULL
)
892 quotedarg
= quot (optarg
);
893 preprocargs
= xmalloc (strlen (quotedarg
) + 3);
894 sprintf (preprocargs
, "-%c%s", c
, quotedarg
);
900 quotedarg
= quot (optarg
);
901 n
= xmalloc (strlen (preprocargs
) + strlen (quotedarg
) + 4);
902 sprintf (n
, "%s -%c%s", preprocargs
, c
, quotedarg
);
909 /* Ignored for compatibility with rc. */
917 /* For backward compatibility, should be removed in the future. */
918 input_format_tmp
= format_from_name (optarg
, 0);
919 if (input_format_tmp
!= RES_FORMAT_UNKNOWN
)
922 _("Option -I is deprecated for setting the input format, please use -J instead.\n"));
923 input_format
= input_format_tmp
;
927 if (preprocargs
== NULL
)
929 quotedarg
= quot (optarg
);
930 preprocargs
= xmalloc (strlen (quotedarg
) + 3);
931 sprintf (preprocargs
, "-I%s", quotedarg
);
937 quotedarg
= quot (optarg
);
938 n
= xmalloc (strlen (preprocargs
) + strlen (quotedarg
) + 4);
939 sprintf (n
, "%s -I%s", preprocargs
, quotedarg
);
944 windres_add_include_dir (optarg
);
949 language
= strtol (optarg
, (char **) NULL
, 16);
952 case OPTION_USE_TEMP_FILE
:
956 case OPTION_NO_USE_TEMP_FILE
:
972 print_version ("windres");
981 if (input_filename
== NULL
&& optind
< argc
)
983 input_filename
= argv
[optind
];
987 if (output_filename
== NULL
&& optind
< argc
)
989 output_filename
= argv
[optind
];
996 if (input_format
== RES_FORMAT_UNKNOWN
)
998 if (input_filename
== NULL
)
999 input_format
= RES_FORMAT_RC
;
1001 input_format
= format_from_filename (input_filename
, 1);
1004 if (output_format
== RES_FORMAT_UNKNOWN
)
1006 if (output_filename
== NULL
)
1007 output_format
= RES_FORMAT_RC
;
1009 output_format
= format_from_filename (output_filename
, 0);
1012 set_endianess (NULL
, target
);
1014 /* Read the input file. */
1015 switch (input_format
)
1020 resources
= read_rc_file (input_filename
, preprocessor
, preprocargs
,
1021 language
, use_temp_file
);
1023 case RES_FORMAT_RES
:
1024 resources
= read_res_file (input_filename
);
1026 case RES_FORMAT_COFF
:
1027 resources
= read_coff_rsrc (input_filename
, target
);
1031 if (resources
== NULL
)
1032 fatal (_("no resources"));
1034 /* Sort the resources. This is required for COFF, convenient for
1035 rc, and unimportant for res. */
1036 resources
= sort_resources (resources
);
1038 /* Write the output file. */
1041 switch (output_format
)
1046 write_rc_file (output_filename
, resources
);
1048 case RES_FORMAT_RES
:
1049 write_res_file (output_filename
, resources
);
1051 case RES_FORMAT_COFF
:
1052 write_coff_file (output_filename
, target
, resources
);
1061 set_endianess (bfd
*abfd
, const char *target
)
1063 const bfd_target
*target_vec
;
1065 def_target_arch
= NULL
;
1066 target_vec
= bfd_get_target_info (target
, abfd
, &target_is_bigendian
, NULL
,
1069 fatal ("Can't detect target endianess and architecture.");
1070 if (! def_target_arch
)
1071 fatal ("Can't detect architecture.");
1075 windres_open_as_binary (const char *filename
, int rdmode
)
1079 abfd
= (rdmode
? bfd_openr (filename
, "binary") : bfd_openw (filename
, "binary"));
1081 fatal ("can't open `%s' for %s", filename
, (rdmode
? "input" : "output"));
1083 if (rdmode
&& ! bfd_check_format (abfd
, bfd_object
))
1084 fatal ("can't open `%s' for input.", filename
);
1090 set_windres_bfd_endianess (windres_bfd
*wrbfd
, int is_bigendian
)
1093 switch (WR_KIND(wrbfd
))
1095 case WR_KIND_BFD_BIN_L
:
1097 WR_KIND(wrbfd
) = WR_KIND_BFD_BIN_B
;
1099 case WR_KIND_BFD_BIN_B
:
1101 WR_KIND(wrbfd
) = WR_KIND_BFD_BIN_L
;
1104 /* only binary bfd can be overriden. */
1110 set_windres_bfd (windres_bfd
*wrbfd
, bfd
*abfd
, asection
*sec
, rc_uint_type kind
)
1115 case WR_KIND_TARGET
:
1120 case WR_KIND_BFD_BIN_L
:
1121 case WR_KIND_BFD_BIN_B
:
1128 WR_KIND(wrbfd
) = kind
;
1129 WR_BFD(wrbfd
) = abfd
;
1130 WR_SECTION(wrbfd
) = sec
;
1134 set_windres_bfd_content (windres_bfd
*wrbfd
, const void *data
, rc_uint_type off
,
1135 rc_uint_type length
)
1137 if (WR_KIND(wrbfd
) != WR_KIND_TARGET
)
1139 if (! bfd_set_section_contents (WR_BFD(wrbfd
), WR_SECTION(wrbfd
), data
, off
, length
))
1140 bfd_fatal ("bfd_set_section_contents");
1147 get_windres_bfd_content (windres_bfd
*wrbfd
, void *data
, rc_uint_type off
,
1148 rc_uint_type length
)
1150 if (WR_KIND(wrbfd
) != WR_KIND_TARGET
)
1152 if (! bfd_get_section_contents (WR_BFD(wrbfd
), WR_SECTION(wrbfd
), data
, off
, length
))
1153 bfd_fatal ("bfd_get_section_contents");
1160 windres_put_8 (windres_bfd
*wrbfd
, void *p
, rc_uint_type value
)
1162 switch (WR_KIND(wrbfd
))
1164 case WR_KIND_TARGET
:
1165 target_put_8 (p
, value
);
1168 case WR_KIND_BFD_BIN_L
:
1169 case WR_KIND_BFD_BIN_B
:
1170 bfd_put_8 (WR_BFD(wrbfd
), value
, p
);
1178 windres_put_16 (windres_bfd
*wrbfd
, void *data
, rc_uint_type value
)
1180 switch (WR_KIND(wrbfd
))
1182 case WR_KIND_TARGET
:
1183 target_put_16 (data
, value
);
1186 case WR_KIND_BFD_BIN_B
:
1187 bfd_put_16 (WR_BFD(wrbfd
), value
, data
);
1189 case WR_KIND_BFD_BIN_L
:
1190 bfd_putl16 (value
, data
);
1198 windres_put_32 (windres_bfd
*wrbfd
, void *data
, rc_uint_type value
)
1200 switch (WR_KIND(wrbfd
))
1202 case WR_KIND_TARGET
:
1203 target_put_32 (data
, value
);
1206 case WR_KIND_BFD_BIN_B
:
1207 bfd_put_32 (WR_BFD(wrbfd
), value
, data
);
1209 case WR_KIND_BFD_BIN_L
:
1210 bfd_putl32 (value
, data
);
1218 windres_get_8 (windres_bfd
*wrbfd
, const void *data
, rc_uint_type length
)
1221 fatal ("windres_get_8: unexpected eob.");
1222 switch (WR_KIND(wrbfd
))
1224 case WR_KIND_TARGET
:
1225 return target_get_8 (data
, length
);
1227 case WR_KIND_BFD_BIN_B
:
1228 case WR_KIND_BFD_BIN_L
:
1229 return bfd_get_8 (WR_BFD(wrbfd
), data
);
1237 windres_get_16 (windres_bfd
*wrbfd
, const void *data
, rc_uint_type length
)
1240 fatal ("windres_get_16: unexpected eob.");
1241 switch (WR_KIND(wrbfd
))
1243 case WR_KIND_TARGET
:
1244 return target_get_16 (data
, length
);
1246 case WR_KIND_BFD_BIN_B
:
1247 return bfd_get_16 (WR_BFD(wrbfd
), data
);
1248 case WR_KIND_BFD_BIN_L
:
1249 return bfd_getl16 (data
);
1257 windres_get_32 (windres_bfd
*wrbfd
, const void *data
, rc_uint_type length
)
1260 fatal ("windres_get_32: unexpected eob.");
1261 switch (WR_KIND(wrbfd
))
1263 case WR_KIND_TARGET
:
1264 return target_get_32 (data
, length
);
1266 case WR_KIND_BFD_BIN_B
:
1267 return bfd_get_32 (WR_BFD(wrbfd
), data
);
1268 case WR_KIND_BFD_BIN_L
:
1269 return bfd_getl32 (data
);
1277 target_get_8 (const void *p
, rc_uint_type length
)
1282 fatal ("Resource too small for getting 8-bit value.");
1284 ret
= (rc_uint_type
) *((const bfd_byte
*) p
);
1289 target_get_16 (const void *p
, rc_uint_type length
)
1292 fatal ("Resource too small for getting 16-bit value.");
1294 if (target_is_bigendian
)
1295 return bfd_getb16 (p
);
1297 return bfd_getl16 (p
);
1301 target_get_32 (const void *p
, rc_uint_type length
)
1304 fatal ("Resource too small for getting 32-bit value.");
1306 if (target_is_bigendian
)
1307 return bfd_getb32 (p
);
1309 return bfd_getl32 (p
);
1313 target_put_8 (void *p
, rc_uint_type value
)
1316 *((bfd_byte
*) p
)=(bfd_byte
) value
;
1320 target_put_16 (void *p
, rc_uint_type value
)
1324 if (target_is_bigendian
)
1325 bfd_putb16 (value
, p
);
1327 bfd_putl16 (value
, p
);
1331 target_put_32 (void *p
, rc_uint_type value
)
1335 if (target_is_bigendian
)
1336 bfd_putb32 (value
, p
);
1338 bfd_putl32 (value
, p
);
1341 static int isInComment
= 0;
1343 int wr_printcomment (FILE *e
, const char *fmt
, ...)
1349 r
+= fprintf (e
, "\n ");
1355 va_start (arg
, fmt
);
1356 r
+= vfprintf (e
, fmt
, arg
);
1361 int wr_print (FILE *e
, const char *fmt
, ...)
1366 r
+= fprintf (e
, ". */\n");
1370 va_start (arg
, fmt
);
1371 r
+= vfprintf (e
, fmt
, arg
);