1 /* windres.c -- a program to manipulate Windows resources
2 Copyright 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2007
3 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 /* Defined in bfd/binary.c. Used to set architecture and machine of input
51 extern enum bfd_architecture bfd_external_binary_architecture
;
52 extern unsigned long bfd_external_machine
;
54 /* Used by resrc.c at least. */
58 int target_is_bigendian
= 0;
59 const char *def_target_arch
;
61 static void set_endianess (bfd
*, const char *);
63 /* An enumeration of format types. */
69 /* Textual RC file. */
71 /* Binary RES file. */
77 /* A structure used to map between format types and strings. */
82 enum res_format format
;
85 /* A mapping between names and format types. */
87 static const struct format_map format_names
[] =
89 { "rc", RES_FORMAT_RC
},
90 { "res", RES_FORMAT_RES
},
91 { "coff", RES_FORMAT_COFF
},
92 { NULL
, RES_FORMAT_UNKNOWN
}
95 /* A mapping from file extensions to format types. */
97 static const struct format_map format_fileexts
[] =
99 { "rc", RES_FORMAT_RC
},
100 { "res", RES_FORMAT_RES
},
101 { "exe", RES_FORMAT_COFF
},
102 { "obj", RES_FORMAT_COFF
},
103 { "o", RES_FORMAT_COFF
},
104 { NULL
, RES_FORMAT_UNKNOWN
}
107 /* A list of include directories. */
111 struct include_dir
*next
;
115 static struct include_dir
*include_dirs
;
117 /* Static functions. */
119 static void res_init (void);
120 static int extended_menuitems (const rc_menuitem
*);
121 static enum res_format
format_from_name (const char *, int);
122 static enum res_format
format_from_filename (const char *, int);
123 static void usage (FILE *, int);
124 static int cmp_res_entry (const void *, const void *);
125 static rc_res_directory
*sort_resources (rc_res_directory
*);
126 static void reswr_init (void);
127 static const char * quot (const char *);
129 static rc_uint_type
target_get_8 (const void *, rc_uint_type
);
130 static void target_put_8 (void *, rc_uint_type
);
131 static rc_uint_type
target_get_16 (const void *, rc_uint_type
);
132 static void target_put_16 (void *, rc_uint_type
);
133 static rc_uint_type
target_get_32 (const void *, rc_uint_type
);
134 static void target_put_32 (void *, rc_uint_type
);
137 /* When we are building a resource tree, we allocate everything onto
138 an obstack, so that we can free it all at once if we want. */
140 #define obstack_chunk_alloc xmalloc
141 #define obstack_chunk_free free
143 /* The resource building obstack. */
145 static struct obstack res_obstack
;
147 /* Initialize the resource building obstack. */
152 obstack_init (&res_obstack
);
155 /* Allocate space on the resource building obstack. */
158 res_alloc (rc_uint_type bytes
)
160 return obstack_alloc (&res_obstack
, (size_t) bytes
);
163 /* We also use an obstack to save memory used while writing out a set
166 static struct obstack reswr_obstack
;
168 /* Initialize the resource writing obstack. */
173 obstack_init (&reswr_obstack
);
176 /* Allocate space on the resource writing obstack. */
179 reswr_alloc (rc_uint_type bytes
)
181 return obstack_alloc (&reswr_obstack
, (size_t) bytes
);
184 /* Open a file using the include directory search list. */
187 open_file_search (const char *filename
, const char *mode
, const char *errmsg
,
188 char **real_filename
)
191 struct include_dir
*d
;
193 e
= fopen (filename
, mode
);
196 *real_filename
= xstrdup (filename
);
202 for (d
= include_dirs
; d
!= NULL
; d
= d
->next
)
206 n
= (char *) xmalloc (strlen (d
->dir
) + strlen (filename
) + 2);
207 sprintf (n
, "%s/%s", d
->dir
, filename
);
220 fatal (_("can't open %s `%s': %s"), errmsg
, filename
, strerror (errno
));
222 /* Return a value to avoid a compiler warning. */
226 /* Compare two resource ID's. We consider name entries to come before
227 numeric entries, because that is how they appear in the COFF .rsrc
231 res_id_cmp (rc_res_id a
, rc_res_id b
)
239 else if (a
.u
.id
< b
.u
.id
)
246 unichar
*as
, *ase
, *bs
, *bse
;
252 ase
= as
+ a
.u
.n
.length
;
254 bse
= bs
+ b
.u
.n
.length
;
262 i
= (int) *as
- (int) *bs
;
276 /* Print a resource ID. */
279 res_id_print (FILE *stream
, rc_res_id id
, int quote
)
282 fprintf (stream
, "%u", (int) id
.u
.id
);
286 unicode_print_quoted (stream
, id
.u
.n
.name
, id
.u
.n
.length
);
288 unicode_print (stream
, id
.u
.n
.name
, id
.u
.n
.length
);
292 /* Print a list of resource ID's. */
295 res_ids_print (FILE *stream
, int cids
, const rc_res_id
*ids
)
299 for (i
= 0; i
< cids
; i
++)
301 res_id_print (stream
, ids
[i
], 1);
303 fprintf (stream
, ": ");
307 /* Convert an ASCII string to a resource ID. */
310 res_string_to_id (rc_res_id
*res_id
, const char *string
)
313 unicode_from_ascii (&res_id
->u
.n
.length
, &res_id
->u
.n
.name
, string
);
316 /* Convert an unicode string to a resource ID. */
318 res_unistring_to_id (rc_res_id
*res_id
, const unichar
*u
)
321 res_id
->u
.n
.length
= unichar_len (u
);
322 res_id
->u
.n
.name
= unichar_dup_uppercase (u
);
325 /* Define a resource. The arguments are the resource tree, RESOURCES,
326 and the location at which to put it in the tree, CIDS and IDS.
327 This returns a newly allocated rc_res_resource structure, which the
328 caller is expected to initialize. If DUPOK is non-zero, then if a
329 resource with this ID exists, it is returned. Otherwise, a warning
330 is issued, and a new resource is created replacing the existing
334 define_resource (rc_res_directory
**resources
, int cids
,
335 const rc_res_id
*ids
, int dupok
)
337 rc_res_entry
*re
= NULL
;
341 for (i
= 0; i
< cids
; i
++)
345 if (*resources
== NULL
)
347 static unsigned int timeval
;
349 /* Use the same timestamp for every resource created in a
352 timeval
= time (NULL
);
354 *resources
= ((rc_res_directory
*)
355 res_alloc (sizeof (rc_res_directory
)));
356 (*resources
)->characteristics
= 0;
357 (*resources
)->time
= timeval
;
358 (*resources
)->major
= 0;
359 (*resources
)->minor
= 0;
360 (*resources
)->entries
= NULL
;
363 for (pp
= &(*resources
)->entries
; *pp
!= NULL
; pp
= &(*pp
)->next
)
364 if (res_id_cmp ((*pp
)->id
, ids
[i
]) == 0)
371 re
= (rc_res_entry
*) res_alloc (sizeof (rc_res_entry
));
392 fprintf (stderr
, "%s: ", program_name
);
393 res_ids_print (stderr
, i
, ids
);
394 fprintf (stderr
, _(": expected to be a directory\n"));
398 resources
= &re
->u
.dir
;
404 fprintf (stderr
, "%s: ", program_name
);
405 res_ids_print (stderr
, cids
, ids
);
406 fprintf (stderr
, _(": expected to be a leaf\n"));
410 if (re
->u
.res
!= NULL
)
415 fprintf (stderr
, _("%s: warning: "), program_name
);
416 res_ids_print (stderr
, cids
, ids
);
417 fprintf (stderr
, _(": duplicate value\n"));
420 re
->u
.res
= ((rc_res_resource
*)
421 res_alloc (sizeof (rc_res_resource
)));
422 memset (re
->u
.res
, 0, sizeof (rc_res_resource
));
424 re
->u
.res
->type
= RES_TYPE_UNINITIALIZED
;
428 /* Define a standard resource. This is a version of define_resource
429 that just takes type, name, and language arguments. */
432 define_standard_resource (rc_res_directory
**resources
, int type
,
433 rc_res_id name
, rc_uint_type language
, int dupok
)
441 a
[2].u
.id
= language
;
442 return define_resource (resources
, 3, a
, dupok
);
445 /* Comparison routine for resource sorting. */
448 cmp_res_entry (const void *p1
, const void *p2
)
450 const rc_res_entry
**re1
, **re2
;
452 re1
= (const rc_res_entry
**) p1
;
453 re2
= (const rc_res_entry
**) p2
;
454 return res_id_cmp ((*re1
)->id
, (*re2
)->id
);
457 /* Sort the resources. */
459 static rc_res_directory
*
460 sort_resources (rc_res_directory
*resdir
)
466 if (resdir
->entries
== NULL
)
470 for (re
= resdir
->entries
; re
!= NULL
; re
= re
->next
)
473 /* This is a recursive routine, so using xmalloc is probably better
475 a
= (rc_res_entry
**) xmalloc (c
* sizeof (rc_res_entry
*));
477 for (i
= 0, re
= resdir
->entries
; re
!= NULL
; re
= re
->next
, i
++)
480 qsort (a
, c
, sizeof (rc_res_entry
*), cmp_res_entry
);
482 resdir
->entries
= a
[0];
483 for (i
= 0; i
< c
- 1; i
++)
484 a
[i
]->next
= a
[i
+ 1];
489 /* Now sort the subdirectories. */
491 for (re
= resdir
->entries
; re
!= NULL
; re
= re
->next
)
493 re
->u
.dir
= sort_resources (re
->u
.dir
);
498 /* Return whether the dialog resource DIALOG is a DIALOG or a
502 extended_dialog (const rc_dialog
*dialog
)
504 const rc_dialog_control
*c
;
506 if (dialog
->ex
!= NULL
)
509 for (c
= dialog
->controls
; c
!= NULL
; c
= c
->next
)
510 if (c
->data
!= NULL
|| c
->help
!= 0)
516 /* Return whether MENUITEMS are a MENU or a MENUEX. */
519 extended_menu (const rc_menu
*menu
)
521 return extended_menuitems (menu
->items
);
525 extended_menuitems (const rc_menuitem
*menuitems
)
527 const rc_menuitem
*mi
;
529 for (mi
= menuitems
; mi
!= NULL
; mi
= mi
->next
)
531 if (mi
->help
!= 0 || mi
->state
!= 0)
533 if (mi
->popup
!= NULL
&& mi
->id
!= 0)
536 & ~ (MENUITEM_CHECKED
540 | MENUITEM_MENUBARBREAK
541 | MENUITEM_MENUBREAK
))
544 if (mi
->popup
!= NULL
)
546 if (extended_menuitems (mi
->popup
))
554 /* Convert a string to a format type, or exit if it can't be done. */
556 static enum res_format
557 format_from_name (const char *name
, int exit_on_error
)
559 const struct format_map
*m
;
561 for (m
= format_names
; m
->name
!= NULL
; m
++)
562 if (strcasecmp (m
->name
, name
) == 0)
565 if (m
->name
== NULL
&& exit_on_error
)
567 non_fatal (_("unknown format type `%s'"), name
);
568 fprintf (stderr
, _("%s: supported formats:"), program_name
);
569 for (m
= format_names
; m
->name
!= NULL
; m
++)
570 fprintf (stderr
, " %s", m
->name
);
571 fprintf (stderr
, "\n");
578 /* Work out a format type given a file name. If INPUT is non-zero,
579 it's OK to look at the file itself. */
581 static enum res_format
582 format_from_filename (const char *filename
, int input
)
586 bfd_byte b1
, b2
, b3
, b4
, b5
;
589 /* If we have an extension, see if we recognize it as implying a
590 particular format. */
591 ext
= strrchr (filename
, '.');
594 const struct format_map
*m
;
597 for (m
= format_fileexts
; m
->name
!= NULL
; m
++)
598 if (strcasecmp (m
->name
, ext
) == 0)
602 /* If we don't recognize the name of an output file, assume it's a
605 return RES_FORMAT_COFF
;
607 /* Read the first few bytes of the file to see if we can guess what
609 e
= fopen (filename
, FOPEN_RB
);
611 fatal ("%s: %s", filename
, strerror (errno
));
621 /* A PE executable starts with 0x4d 0x5a. */
622 if (b1
== 0x4d && b2
== 0x5a)
623 return RES_FORMAT_COFF
;
625 /* A COFF .o file starts with a COFF magic number. */
626 magic
= (b2
<< 8) | b1
;
629 case 0x14c: /* i386 */
630 case 0x166: /* MIPS */
631 case 0x184: /* Alpha */
632 case 0x268: /* 68k */
633 case 0x1f0: /* PowerPC */
635 return RES_FORMAT_COFF
;
638 /* A RES file starts with 0x0 0x0 0x0 0x0 0x20 0x0 0x0 0x0. */
639 if (b1
== 0 && b2
== 0 && b3
== 0 && b4
== 0 && b5
== 0x20)
640 return RES_FORMAT_RES
;
642 /* If every character is printable or space, assume it's an RC file. */
643 if ((ISPRINT (b1
) || ISSPACE (b1
))
644 && (ISPRINT (b2
) || ISSPACE (b2
))
645 && (ISPRINT (b3
) || ISSPACE (b3
))
646 && (ISPRINT (b4
) || ISSPACE (b4
))
647 && (ISPRINT (b5
) || ISSPACE (b5
)))
648 return RES_FORMAT_RC
;
650 /* Otherwise, we give up. */
651 fatal (_("can not determine type of file `%s'; use the -J option"),
654 /* Return something to silence the compiler warning. */
655 return RES_FORMAT_UNKNOWN
;
658 /* Print a usage message and exit. */
661 usage (FILE *stream
, int status
)
663 fprintf (stream
, _("Usage: %s [option(s)] [input-file] [output-file]\n"),
665 fprintf (stream
, _(" The options are:\n\
666 -i --input=<file> Name input file\n\
667 -o --output=<file> Name output file\n\
668 -J --input-format=<format> Specify input format\n\
669 -O --output-format=<format> Specify output format\n\
670 -F --target=<target> Specify COFF target\n\
671 --preprocessor=<program> Program to use to preprocess rc file\n\
672 -I --include-dir=<dir> Include directory when preprocessing rc file\n\
673 -D --define <sym>[=<val>] Define SYM when preprocessing rc file\n\
674 -U --undefine <sym> Undefine SYM when preprocessing rc file\n\
675 -v --verbose Verbose - tells you what it's doing\n\
676 -c --codepage=<codepage> Specify default codepage\n\
677 -l --language=<val> Set language when reading rc file\n\
678 --use-temp-file Use a temporary file instead of popen to read\n\
679 the preprocessor output\n\
680 --no-use-temp-file Use popen (default)\n"));
682 fprintf (stream
, _("\
683 --yydebug Turn on parser debugging\n"));
685 fprintf (stream
, _("\
686 -r Ignored for compatibility with rc\n\
687 @<file> Read options from <file>\n\
688 -h --help Print this help message\n\
689 -V --version Print version information\n"));
690 fprintf (stream
, _("\
691 FORMAT is one of rc, res, or coff, and is deduced from the file name\n\
692 extension if not specified. A single file name is an input file.\n\
693 No input-file is stdin, default rc. No output-file is stdout, default rc.\n"));
695 list_supported_targets (program_name
, stream
);
697 if (REPORT_BUGS_TO
[0] && status
== 0)
698 fprintf (stream
, _("Report bugs to %s\n"), REPORT_BUGS_TO
);
703 /* Quote characters that will confuse the shell when we run the preprocessor. */
706 quot (const char *string
)
708 static char *buf
= 0;
709 static int buflen
= 0;
710 int slen
= strlen (string
);
714 if ((buflen
< slen
* 2 + 2) || ! buf
)
716 buflen
= slen
* 2 + 2;
719 buf
= (char *) xmalloc (buflen
);
722 for (src
=string
, dest
=buf
; *src
; src
++, dest
++)
724 if (*src
== '(' || *src
== ')' || *src
== ' ')
734 /* 150 isn't special; it's just an arbitrary non-ASCII char value. */
736 #define OPTION_PREPROCESSOR 150
737 #define OPTION_USE_TEMP_FILE (OPTION_PREPROCESSOR + 1)
738 #define OPTION_NO_USE_TEMP_FILE (OPTION_USE_TEMP_FILE + 1)
739 #define OPTION_YYDEBUG (OPTION_NO_USE_TEMP_FILE + 1)
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 {"include-dir", required_argument
, 0, 'I'},
750 {"define", required_argument
, 0, 'D'},
751 {"undefine", required_argument
, 0, 'U'},
752 {"verbose", no_argument
, 0, 'v'},
753 {"codepage", required_argument
, 0, 'c'},
754 {"language", required_argument
, 0, 'l'},
755 {"use-temp-file", no_argument
, 0, OPTION_USE_TEMP_FILE
},
756 {"no-use-temp-file", no_argument
, 0, OPTION_NO_USE_TEMP_FILE
},
757 {"yydebug", no_argument
, 0, OPTION_YYDEBUG
},
758 {"version", no_argument
, 0, 'V'},
759 {"help", no_argument
, 0, 'h'},
760 {0, no_argument
, 0, 0}
764 windres_add_include_dir (const char *p
)
766 struct include_dir
*n
, **pp
;
768 /* Computing paths is often complicated and error prone.
769 The easiest way to check for mistakes is at the time
770 we add them to include_dirs. */
774 n
= xmalloc (sizeof *n
);
776 n
->dir
= (char * ) p
;
778 for (pp
= &include_dirs
; *pp
!= NULL
; pp
= &(*pp
)->next
)
783 /* This keeps gcc happy when using -Wmissing-prototypes -Wstrict-prototypes. */
784 int main (int, char **);
786 /* The main function. */
789 main (int argc
, char **argv
)
792 char *input_filename
;
793 char *output_filename
;
794 enum res_format input_format
;
795 enum res_format input_format_tmp
;
796 enum res_format output_format
;
800 const char *quotedarg
;
802 rc_res_directory
*resources
;
805 #if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
806 setlocale (LC_MESSAGES
, "");
808 #if defined (HAVE_SETLOCALE)
809 setlocale (LC_CTYPE
, "");
811 bindtextdomain (PACKAGE
, LOCALEDIR
);
812 textdomain (PACKAGE
);
814 program_name
= argv
[0];
815 xmalloc_set_program_name (program_name
);
817 expandargv (&argc
, &argv
);
820 set_default_bfd_target ();
824 input_filename
= NULL
;
825 output_filename
= NULL
;
826 input_format
= RES_FORMAT_UNKNOWN
;
827 output_format
= RES_FORMAT_UNKNOWN
;
831 language
= 0x409; /* LANG_ENGLISH, SUBLANG_ENGLISH_US. */
834 while ((c
= getopt_long (argc
, argv
, "c:f:i:l:o:I:J:O:F:D:U:rhHvV", long_options
,
843 if (optarg
[0] == '0' && (optarg
[1] == 'x' || optarg
[1] == 'X'))
844 ncp
= (rc_uint_type
) strtol (optarg
+ 2, NULL
, 16);
846 ncp
= (rc_uint_type
) strtol (optarg
, NULL
, 10);
847 if (ncp
== CP_UTF16
|| ! unicode_is_valid_codepage (ncp
))
848 fatal (_("invalid codepage specified.\n"));
849 wind_default_codepage
= wind_current_codepage
= ncp
;
854 input_filename
= optarg
;
858 /* For compatibility with rc we accept "-fo <name>" as being the
859 equivalent of "-o <name>". We do not advertise this fact
860 though, as we do not want users to use non-GNU like command
863 fatal (_("invalid option -f\n"));
868 fatal (_("No filename following the -fo option.\n"));
869 optarg
= argv
[optind
++];
874 output_filename
= optarg
;
878 input_format
= format_from_name (optarg
, 1);
882 output_format
= format_from_name (optarg
, 1);
889 case OPTION_PREPROCESSOR
:
890 preprocessor
= optarg
;
895 if (preprocargs
== NULL
)
897 quotedarg
= quot (optarg
);
898 preprocargs
= xmalloc (strlen (quotedarg
) + 3);
899 sprintf (preprocargs
, "-%c%s", c
, quotedarg
);
905 quotedarg
= quot (optarg
);
906 n
= xmalloc (strlen (preprocargs
) + strlen (quotedarg
) + 4);
907 sprintf (n
, "%s -%c%s", preprocargs
, c
, quotedarg
);
914 /* Ignored for compatibility with rc. */
922 /* For backward compatibility, should be removed in the future. */
923 input_format_tmp
= format_from_name (optarg
, 0);
924 if (input_format_tmp
!= RES_FORMAT_UNKNOWN
)
927 _("Option -I is deprecated for setting the input format, please use -J instead.\n"));
928 input_format
= input_format_tmp
;
932 if (preprocargs
== NULL
)
934 quotedarg
= quot (optarg
);
935 preprocargs
= xmalloc (strlen (quotedarg
) + 3);
936 sprintf (preprocargs
, "-I%s", quotedarg
);
942 quotedarg
= quot (optarg
);
943 n
= xmalloc (strlen (preprocargs
) + strlen (quotedarg
) + 4);
944 sprintf (n
, "%s -I%s", preprocargs
, quotedarg
);
949 windres_add_include_dir (optarg
);
954 language
= strtol (optarg
, (char **) NULL
, 16);
957 case OPTION_USE_TEMP_FILE
:
961 case OPTION_NO_USE_TEMP_FILE
:
977 print_version ("windres");
986 if (input_filename
== NULL
&& optind
< argc
)
988 input_filename
= argv
[optind
];
992 if (output_filename
== NULL
&& optind
< argc
)
994 output_filename
= argv
[optind
];
1001 if (input_format
== RES_FORMAT_UNKNOWN
)
1003 if (input_filename
== NULL
)
1004 input_format
= RES_FORMAT_RC
;
1006 input_format
= format_from_filename (input_filename
, 1);
1009 if (output_format
== RES_FORMAT_UNKNOWN
)
1011 if (output_filename
== NULL
)
1012 output_format
= RES_FORMAT_RC
;
1014 output_format
= format_from_filename (output_filename
, 0);
1017 set_endianess (NULL
, target
);
1019 /* Read the input file. */
1020 switch (input_format
)
1025 resources
= read_rc_file (input_filename
, preprocessor
, preprocargs
,
1026 language
, use_temp_file
);
1028 case RES_FORMAT_RES
:
1029 resources
= read_res_file (input_filename
);
1031 case RES_FORMAT_COFF
:
1032 resources
= read_coff_rsrc (input_filename
, target
);
1036 if (resources
== NULL
)
1037 fatal (_("no resources"));
1039 /* Sort the resources. This is required for COFF, convenient for
1040 rc, and unimportant for res. */
1041 resources
= sort_resources (resources
);
1043 /* Write the output file. */
1046 switch (output_format
)
1051 write_rc_file (output_filename
, resources
);
1053 case RES_FORMAT_RES
:
1054 write_res_file (output_filename
, resources
);
1056 case RES_FORMAT_COFF
:
1057 write_coff_file (output_filename
, target
, resources
);
1066 set_endianess (bfd
*abfd
, const char *target
)
1068 const bfd_target
*target_vec
;
1070 def_target_arch
= NULL
;
1071 target_vec
= bfd_find_target (target
, abfd
);
1073 fatal ("Can't detect target endianess and architecture.");
1074 target_is_bigendian
= ((target_vec
->byteorder
== BFD_ENDIAN_BIG
) ? 1 : 0);
1077 const char * tname
= target_vec
->name
;
1078 const char ** arches
= bfd_arch_list();
1080 if (arches
&& tname
)
1082 const char ** arch
= arches
;
1084 if (strchr (tname
, '-') != NULL
)
1085 tname
= strchr (tname
, '-') + 1;
1086 while (*arch
!= NULL
)
1088 const char *in_a
= strstr (*arch
, tname
);
1089 char end_ch
= (in_a
? in_a
[strlen(tname
)] : 0);
1091 if (in_a
&& (in_a
== *arch
|| in_a
[-1] == ':')
1094 def_target_arch
= *arch
;
1103 if (! def_target_arch
)
1104 fatal ("Can't detect architecture.");
1109 windres_open_as_binary (const char *filename
, int rdmode
)
1113 abfd
= (rdmode
? bfd_openr (filename
, "binary") : bfd_openw (filename
, "binary"));
1115 fatal ("can't open `%s' for %s", filename
, (rdmode
? "input" : "output"));
1117 if (rdmode
&& ! bfd_check_format (abfd
, bfd_object
))
1118 fatal ("can't open `%s' for input.", filename
);
1124 set_windres_bfd_endianess (windres_bfd
*wrbfd
, int is_bigendian
)
1127 switch (WR_KIND(wrbfd
))
1129 case WR_KIND_BFD_BIN_L
:
1131 WR_KIND(wrbfd
) = WR_KIND_BFD_BIN_B
;
1133 case WR_KIND_BFD_BIN_B
:
1135 WR_KIND(wrbfd
) = WR_KIND_BFD_BIN_L
;
1138 /* only binary bfd can be overriden. */
1144 set_windres_bfd (windres_bfd
*wrbfd
, bfd
*abfd
, asection
*sec
, rc_uint_type kind
)
1149 case WR_KIND_TARGET
:
1154 case WR_KIND_BFD_BIN_L
:
1155 case WR_KIND_BFD_BIN_B
:
1162 WR_KIND(wrbfd
) = kind
;
1163 WR_BFD(wrbfd
) = abfd
;
1164 WR_SECTION(wrbfd
) = sec
;
1168 set_windres_bfd_content (windres_bfd
*wrbfd
, const void *data
, rc_uint_type off
,
1169 rc_uint_type length
)
1171 if (WR_KIND(wrbfd
) != WR_KIND_TARGET
)
1173 if (! bfd_set_section_contents (WR_BFD(wrbfd
), WR_SECTION(wrbfd
), data
, off
, length
))
1174 bfd_fatal ("bfd_set_section_contents");
1181 get_windres_bfd_content (windres_bfd
*wrbfd
, void *data
, rc_uint_type off
,
1182 rc_uint_type length
)
1184 if (WR_KIND(wrbfd
) != WR_KIND_TARGET
)
1186 if (! bfd_get_section_contents (WR_BFD(wrbfd
), WR_SECTION(wrbfd
), data
, off
, length
))
1187 bfd_fatal ("bfd_get_section_contents");
1194 windres_put_8 (windres_bfd
*wrbfd
, void *p
, rc_uint_type value
)
1196 switch (WR_KIND(wrbfd
))
1198 case WR_KIND_TARGET
:
1199 target_put_8 (p
, value
);
1202 case WR_KIND_BFD_BIN_L
:
1203 case WR_KIND_BFD_BIN_B
:
1204 bfd_put_8 (WR_BFD(wrbfd
), value
, p
);
1212 windres_put_16 (windres_bfd
*wrbfd
, void *data
, rc_uint_type value
)
1214 switch (WR_KIND(wrbfd
))
1216 case WR_KIND_TARGET
:
1217 target_put_16 (data
, value
);
1220 case WR_KIND_BFD_BIN_B
:
1221 bfd_put_16 (WR_BFD(wrbfd
), value
, data
);
1223 case WR_KIND_BFD_BIN_L
:
1224 bfd_putl16 (value
, data
);
1232 windres_put_32 (windres_bfd
*wrbfd
, void *data
, rc_uint_type value
)
1234 switch (WR_KIND(wrbfd
))
1236 case WR_KIND_TARGET
:
1237 target_put_32 (data
, value
);
1240 case WR_KIND_BFD_BIN_B
:
1241 bfd_put_32 (WR_BFD(wrbfd
), value
, data
);
1243 case WR_KIND_BFD_BIN_L
:
1244 bfd_putl32 (value
, data
);
1252 windres_get_8 (windres_bfd
*wrbfd
, const void *data
, rc_uint_type length
)
1255 fatal ("windres_get_8: unexpected eob.");
1256 switch (WR_KIND(wrbfd
))
1258 case WR_KIND_TARGET
:
1259 return target_get_8 (data
, length
);
1261 case WR_KIND_BFD_BIN_B
:
1262 case WR_KIND_BFD_BIN_L
:
1263 return bfd_get_8 (WR_BFD(wrbfd
), data
);
1271 windres_get_16 (windres_bfd
*wrbfd
, const void *data
, rc_uint_type length
)
1274 fatal ("windres_get_16: unexpected eob.");
1275 switch (WR_KIND(wrbfd
))
1277 case WR_KIND_TARGET
:
1278 return target_get_16 (data
, length
);
1280 case WR_KIND_BFD_BIN_B
:
1281 return bfd_get_16 (WR_BFD(wrbfd
), data
);
1282 case WR_KIND_BFD_BIN_L
:
1283 return bfd_getl16 (data
);
1291 windres_get_32 (windres_bfd
*wrbfd
, const void *data
, rc_uint_type length
)
1294 fatal ("windres_get_32: unexpected eob.");
1295 switch (WR_KIND(wrbfd
))
1297 case WR_KIND_TARGET
:
1298 return target_get_32 (data
, length
);
1300 case WR_KIND_BFD_BIN_B
:
1301 return bfd_get_32 (WR_BFD(wrbfd
), data
);
1302 case WR_KIND_BFD_BIN_L
:
1303 return bfd_getl32 (data
);
1311 target_get_8 (const void *p
, rc_uint_type length
)
1316 fatal ("Resource too small for getting 8-bit value.");
1318 ret
= (rc_uint_type
) *((const bfd_byte
*) p
);
1323 target_get_16 (const void *p
, rc_uint_type length
)
1326 fatal ("Resource too small for getting 16-bit value.");
1328 if (target_is_bigendian
)
1329 return bfd_getb16 (p
);
1331 return bfd_getl16 (p
);
1335 target_get_32 (const void *p
, rc_uint_type length
)
1338 fatal ("Resource too small for getting 32-bit value.");
1340 if (target_is_bigendian
)
1341 return bfd_getb32 (p
);
1343 return bfd_getl32 (p
);
1347 target_put_8 (void *p
, rc_uint_type value
)
1350 *((bfd_byte
*) p
)=(bfd_byte
) value
;
1354 target_put_16 (void *p
, rc_uint_type value
)
1358 if (target_is_bigendian
)
1359 bfd_putb16 (value
, p
);
1361 bfd_putl16 (value
, p
);
1365 target_put_32 (void *p
, rc_uint_type value
)
1369 if (target_is_bigendian
)
1370 bfd_putb32 (value
, p
);
1372 bfd_putl32 (value
, p
);
1375 static int isInComment
= 0;
1377 int wr_printcomment (FILE *e
, const char *fmt
, ...)
1383 r
+= fprintf (e
, "\n ");
1389 va_start (arg
, fmt
);
1390 r
+= vfprintf (e
, fmt
, arg
);
1395 int wr_print (FILE *e
, const char *fmt
, ...)
1400 r
+= fprintf (e
, ". */\n");
1404 va_start (arg
, fmt
);
1405 r
+= vfprintf (e
, fmt
, arg
);