1f7cc78ecSespie /* rescoff.c -- read and write resources in Windows COFF files.
2*cf2f2c56Smiod Copyright 1997, 1998, 1999, 2000, 2003
3*cf2f2c56Smiod Free Software Foundation, Inc.
4f7cc78ecSespie Written by Ian Lance Taylor, Cygnus Support.
5f7cc78ecSespie
6f7cc78ecSespie This file is part of GNU Binutils.
7f7cc78ecSespie
8f7cc78ecSespie This program is free software; you can redistribute it and/or modify
9f7cc78ecSespie it under the terms of the GNU General Public License as published by
10f7cc78ecSespie the Free Software Foundation; either version 2 of the License, or
11f7cc78ecSespie (at your option) any later version.
12f7cc78ecSespie
13f7cc78ecSespie This program is distributed in the hope that it will be useful,
14f7cc78ecSespie but WITHOUT ANY WARRANTY; without even the implied warranty of
15f7cc78ecSespie MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16f7cc78ecSespie GNU General Public License for more details.
17f7cc78ecSespie
18f7cc78ecSespie You should have received a copy of the GNU General Public License
19f7cc78ecSespie along with this program; if not, write to the Free Software
20f7cc78ecSespie Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
21f7cc78ecSespie 02111-1307, USA. */
22f7cc78ecSespie
23f7cc78ecSespie /* This file contains function that read and write Windows resources
24f7cc78ecSespie in COFF files. */
25f7cc78ecSespie
26f7cc78ecSespie #include "bfd.h"
27f7cc78ecSespie #include "bucomm.h"
28f7cc78ecSespie #include "libiberty.h"
29f7cc78ecSespie #include "windres.h"
30f7cc78ecSespie
31f7cc78ecSespie #include <assert.h>
32f7cc78ecSespie
33f7cc78ecSespie /* In order to use the address of a resource data entry, we need to
34f7cc78ecSespie get the image base of the file. Right now we extract it from
35f7cc78ecSespie internal BFD information. FIXME. */
36f7cc78ecSespie
37f7cc78ecSespie #include "coff/internal.h"
38f7cc78ecSespie #include "libcoff.h"
39f7cc78ecSespie
40f7cc78ecSespie /* Information we extract from the file. */
41f7cc78ecSespie
42f7cc78ecSespie struct coff_file_info
43f7cc78ecSespie {
44f7cc78ecSespie /* File name. */
45f7cc78ecSespie const char *filename;
46f7cc78ecSespie /* Data read from the file. */
47f7cc78ecSespie const bfd_byte *data;
48f7cc78ecSespie /* End of data read from file. */
49f7cc78ecSespie const bfd_byte *data_end;
50f7cc78ecSespie /* Address of the resource section minus the image base of the file. */
51f7cc78ecSespie bfd_vma secaddr;
52f7cc78ecSespie /* Non-zero if the file is big endian. */
53f7cc78ecSespie int big_endian;
54f7cc78ecSespie };
55f7cc78ecSespie
56f7cc78ecSespie /* A resource directory table in a COFF file. */
57f7cc78ecSespie
58f7cc78ecSespie struct extern_res_directory
59f7cc78ecSespie {
60f7cc78ecSespie /* Characteristics. */
61f7cc78ecSespie bfd_byte characteristics[4];
62f7cc78ecSespie /* Time stamp. */
63f7cc78ecSespie bfd_byte time[4];
64f7cc78ecSespie /* Major version number. */
65f7cc78ecSespie bfd_byte major[2];
66f7cc78ecSespie /* Minor version number. */
67f7cc78ecSespie bfd_byte minor[2];
68f7cc78ecSespie /* Number of named directory entries. */
69f7cc78ecSespie bfd_byte name_count[2];
70f7cc78ecSespie /* Number of directory entries with IDs. */
71f7cc78ecSespie bfd_byte id_count[2];
72f7cc78ecSespie };
73f7cc78ecSespie
74f7cc78ecSespie /* A resource directory entry in a COFF file. */
75f7cc78ecSespie
76f7cc78ecSespie struct extern_res_entry
77f7cc78ecSespie {
78f7cc78ecSespie /* Name or ID. */
79f7cc78ecSespie bfd_byte name[4];
80f7cc78ecSespie /* Address of resource entry or subdirectory. */
81f7cc78ecSespie bfd_byte rva[4];
82f7cc78ecSespie };
83f7cc78ecSespie
84f7cc78ecSespie /* A resource data entry in a COFF file. */
85f7cc78ecSespie
86f7cc78ecSespie struct extern_res_data
87f7cc78ecSespie {
88f7cc78ecSespie /* Address of resource data. This is apparently a file relative
89f7cc78ecSespie address, rather than a section offset. */
90f7cc78ecSespie bfd_byte rva[4];
91f7cc78ecSespie /* Size of resource data. */
92f7cc78ecSespie bfd_byte size[4];
93f7cc78ecSespie /* Code page. */
94f7cc78ecSespie bfd_byte codepage[4];
95f7cc78ecSespie /* Reserved. */
96f7cc78ecSespie bfd_byte reserved[4];
97f7cc78ecSespie };
98f7cc78ecSespie
99f7cc78ecSespie /* Macros to swap in values. */
100f7cc78ecSespie
101f7cc78ecSespie #define getfi_16(fi, s) ((fi)->big_endian ? bfd_getb16 (s) : bfd_getl16 (s))
102f7cc78ecSespie #define getfi_32(fi, s) ((fi)->big_endian ? bfd_getb32 (s) : bfd_getl32 (s))
103f7cc78ecSespie
104f7cc78ecSespie /* Local functions. */
105f7cc78ecSespie
106*cf2f2c56Smiod static void overrun (const struct coff_file_info *, const char *);
107f7cc78ecSespie static struct res_directory *read_coff_res_dir
108*cf2f2c56Smiod (const bfd_byte *, const struct coff_file_info *,
109*cf2f2c56Smiod const struct res_id *, int);
110f7cc78ecSespie static struct res_resource *read_coff_data_entry
111*cf2f2c56Smiod (const bfd_byte *, const struct coff_file_info *, const struct res_id *);
112f7cc78ecSespie
113f7cc78ecSespie /* Read the resources in a COFF file. */
114f7cc78ecSespie
115f7cc78ecSespie struct res_directory *
read_coff_rsrc(const char * filename,const char * target)116*cf2f2c56Smiod read_coff_rsrc (const char *filename, const char *target)
117f7cc78ecSespie {
118f7cc78ecSespie bfd *abfd;
119f7cc78ecSespie char **matching;
120f7cc78ecSespie asection *sec;
121f7cc78ecSespie bfd_size_type size;
122f7cc78ecSespie bfd_byte *data;
123f7cc78ecSespie struct coff_file_info finfo;
124f7cc78ecSespie
125f7cc78ecSespie if (filename == NULL)
126f7cc78ecSespie fatal (_("filename required for COFF input"));
127f7cc78ecSespie
128f7cc78ecSespie abfd = bfd_openr (filename, target);
129f7cc78ecSespie if (abfd == NULL)
130f7cc78ecSespie bfd_fatal (filename);
131f7cc78ecSespie
132f7cc78ecSespie if (! bfd_check_format_matches (abfd, bfd_object, &matching))
133f7cc78ecSespie {
134f7cc78ecSespie bfd_nonfatal (bfd_get_filename (abfd));
135f7cc78ecSespie if (bfd_get_error () == bfd_error_file_ambiguously_recognized)
136f7cc78ecSespie list_matching_formats (matching);
137f7cc78ecSespie xexit (1);
138f7cc78ecSespie }
139f7cc78ecSespie
140f7cc78ecSespie sec = bfd_get_section_by_name (abfd, ".rsrc");
141f7cc78ecSespie if (sec == NULL)
142f7cc78ecSespie {
143f7cc78ecSespie fatal (_("%s: no resource section"), filename);
144f7cc78ecSespie }
145f7cc78ecSespie
146f7cc78ecSespie size = bfd_section_size (abfd, sec);
147f7cc78ecSespie data = (bfd_byte *) res_alloc (size);
148f7cc78ecSespie
149f7cc78ecSespie if (! bfd_get_section_contents (abfd, sec, data, 0, size))
150f7cc78ecSespie bfd_fatal (_("can't read resource section"));
151f7cc78ecSespie
152f7cc78ecSespie finfo.filename = filename;
153f7cc78ecSespie finfo.data = data;
154f7cc78ecSespie finfo.data_end = data + size;
155f7cc78ecSespie finfo.secaddr = (bfd_get_section_vma (abfd, sec)
156f7cc78ecSespie - pe_data (abfd)->pe_opthdr.ImageBase);
157f7cc78ecSespie finfo.big_endian = bfd_big_endian (abfd);
158f7cc78ecSespie
159f7cc78ecSespie bfd_close (abfd);
160f7cc78ecSespie
161f7cc78ecSespie /* Now just read in the top level resource directory. Note that we
162f7cc78ecSespie don't free data, since we create resource entries that point into
163f7cc78ecSespie it. If we ever want to free up the resource information we read,
164f7cc78ecSespie this will have to be cleaned up. */
165f7cc78ecSespie
166f7cc78ecSespie return read_coff_res_dir (data, &finfo, (const struct res_id *) NULL, 0);
167f7cc78ecSespie }
168f7cc78ecSespie
169f7cc78ecSespie /* Give an error if we are out of bounds. */
170f7cc78ecSespie
171f7cc78ecSespie static void
overrun(const struct coff_file_info * finfo,const char * msg)172*cf2f2c56Smiod overrun (const struct coff_file_info *finfo, const char *msg)
173f7cc78ecSespie {
174f7cc78ecSespie fatal (_("%s: %s: address out of bounds"), finfo->filename, msg);
175f7cc78ecSespie }
176f7cc78ecSespie
177f7cc78ecSespie /* Read a resource directory. */
178f7cc78ecSespie
179f7cc78ecSespie static struct res_directory *
read_coff_res_dir(const bfd_byte * data,const struct coff_file_info * finfo,const struct res_id * type,int level)180*cf2f2c56Smiod read_coff_res_dir (const bfd_byte *data, const struct coff_file_info *finfo,
181*cf2f2c56Smiod const struct res_id *type, int level)
182f7cc78ecSespie {
183f7cc78ecSespie const struct extern_res_directory *erd;
184f7cc78ecSespie struct res_directory *rd;
185f7cc78ecSespie int name_count, id_count, i;
186f7cc78ecSespie struct res_entry **pp;
187f7cc78ecSespie const struct extern_res_entry *ere;
188f7cc78ecSespie
189f7cc78ecSespie if ((size_t) (finfo->data_end - data) < sizeof (struct extern_res_directory))
190f7cc78ecSespie overrun (finfo, _("directory"));
191f7cc78ecSespie
192f7cc78ecSespie erd = (const struct extern_res_directory *) data;
193f7cc78ecSespie
194f7cc78ecSespie rd = (struct res_directory *) res_alloc (sizeof *rd);
195f7cc78ecSespie rd->characteristics = getfi_32 (finfo, erd->characteristics);
196f7cc78ecSespie rd->time = getfi_32 (finfo, erd->time);
197f7cc78ecSespie rd->major = getfi_16 (finfo, erd->major);
198f7cc78ecSespie rd->minor = getfi_16 (finfo, erd->minor);
199f7cc78ecSespie rd->entries = NULL;
200f7cc78ecSespie
201f7cc78ecSespie name_count = getfi_16 (finfo, erd->name_count);
202f7cc78ecSespie id_count = getfi_16 (finfo, erd->id_count);
203f7cc78ecSespie
204f7cc78ecSespie pp = &rd->entries;
205f7cc78ecSespie
206f7cc78ecSespie /* The resource directory entries immediately follow the directory
207f7cc78ecSespie table. */
208f7cc78ecSespie ere = (const struct extern_res_entry *) (erd + 1);
209f7cc78ecSespie
210f7cc78ecSespie for (i = 0; i < name_count; i++, ere++)
211f7cc78ecSespie {
212f7cc78ecSespie unsigned long name, rva;
213f7cc78ecSespie struct res_entry *re;
214f7cc78ecSespie const bfd_byte *ers;
215f7cc78ecSespie int length, j;
216f7cc78ecSespie
217f7cc78ecSespie if ((const bfd_byte *) ere >= finfo->data_end)
218f7cc78ecSespie overrun (finfo, _("named directory entry"));
219f7cc78ecSespie
220f7cc78ecSespie name = getfi_32 (finfo, ere->name);
221f7cc78ecSespie rva = getfi_32 (finfo, ere->rva);
222f7cc78ecSespie
223f7cc78ecSespie /* For some reason the high bit in NAME is set. */
224f7cc78ecSespie name &=~ 0x80000000;
225f7cc78ecSespie
226f7cc78ecSespie if (name > (size_t) (finfo->data_end - finfo->data))
227f7cc78ecSespie overrun (finfo, _("directory entry name"));
228f7cc78ecSespie
229f7cc78ecSespie ers = finfo->data + name;
230f7cc78ecSespie
231f7cc78ecSespie re = (struct res_entry *) res_alloc (sizeof *re);
232f7cc78ecSespie re->next = NULL;
233f7cc78ecSespie re->id.named = 1;
234f7cc78ecSespie length = getfi_16 (finfo, ers);
235f7cc78ecSespie re->id.u.n.length = length;
236f7cc78ecSespie re->id.u.n.name = (unichar *) res_alloc (length * sizeof (unichar));
237f7cc78ecSespie for (j = 0; j < length; j++)
238f7cc78ecSespie re->id.u.n.name[j] = getfi_16 (finfo, ers + j * 2 + 2);
239f7cc78ecSespie
240f7cc78ecSespie if (level == 0)
241f7cc78ecSespie type = &re->id;
242f7cc78ecSespie
243f7cc78ecSespie if ((rva & 0x80000000) != 0)
244f7cc78ecSespie {
245f7cc78ecSespie rva &=~ 0x80000000;
246f7cc78ecSespie if (rva >= (size_t) (finfo->data_end - finfo->data))
247f7cc78ecSespie overrun (finfo, _("named subdirectory"));
248f7cc78ecSespie re->subdir = 1;
249f7cc78ecSespie re->u.dir = read_coff_res_dir (finfo->data + rva, finfo, type,
250f7cc78ecSespie level + 1);
251f7cc78ecSespie }
252f7cc78ecSespie else
253f7cc78ecSespie {
254f7cc78ecSespie if (rva >= (size_t) (finfo->data_end - finfo->data))
255f7cc78ecSespie overrun (finfo, _("named resource"));
256f7cc78ecSespie re->subdir = 0;
257f7cc78ecSespie re->u.res = read_coff_data_entry (finfo->data + rva, finfo, type);
258f7cc78ecSespie }
259f7cc78ecSespie
260f7cc78ecSespie *pp = re;
261f7cc78ecSespie pp = &re->next;
262f7cc78ecSespie }
263f7cc78ecSespie
264f7cc78ecSespie for (i = 0; i < id_count; i++, ere++)
265f7cc78ecSespie {
266f7cc78ecSespie unsigned long name, rva;
267f7cc78ecSespie struct res_entry *re;
268f7cc78ecSespie
269f7cc78ecSespie if ((const bfd_byte *) ere >= finfo->data_end)
270f7cc78ecSespie overrun (finfo, _("ID directory entry"));
271f7cc78ecSespie
272f7cc78ecSespie name = getfi_32 (finfo, ere->name);
273f7cc78ecSespie rva = getfi_32 (finfo, ere->rva);
274f7cc78ecSespie
275f7cc78ecSespie re = (struct res_entry *) res_alloc (sizeof *re);
276f7cc78ecSespie re->next = NULL;
277f7cc78ecSespie re->id.named = 0;
278f7cc78ecSespie re->id.u.id = name;
279f7cc78ecSespie
280f7cc78ecSespie if (level == 0)
281f7cc78ecSespie type = &re->id;
282f7cc78ecSespie
283f7cc78ecSespie if ((rva & 0x80000000) != 0)
284f7cc78ecSespie {
285f7cc78ecSespie rva &=~ 0x80000000;
286f7cc78ecSespie if (rva >= (size_t) (finfo->data_end - finfo->data))
287f7cc78ecSespie overrun (finfo, _("ID subdirectory"));
288f7cc78ecSespie re->subdir = 1;
289f7cc78ecSespie re->u.dir = read_coff_res_dir (finfo->data + rva, finfo, type,
290f7cc78ecSespie level + 1);
291f7cc78ecSespie }
292f7cc78ecSespie else
293f7cc78ecSespie {
294f7cc78ecSespie if (rva >= (size_t) (finfo->data_end - finfo->data))
295f7cc78ecSespie overrun (finfo, _("ID resource"));
296f7cc78ecSespie re->subdir = 0;
297f7cc78ecSespie re->u.res = read_coff_data_entry (finfo->data + rva, finfo, type);
298f7cc78ecSespie }
299f7cc78ecSespie
300f7cc78ecSespie *pp = re;
301f7cc78ecSespie pp = &re->next;
302f7cc78ecSespie }
303f7cc78ecSespie
304f7cc78ecSespie return rd;
305f7cc78ecSespie }
306f7cc78ecSespie
307f7cc78ecSespie /* Read a resource data entry. */
308f7cc78ecSespie
309f7cc78ecSespie static struct res_resource *
read_coff_data_entry(const bfd_byte * data,const struct coff_file_info * finfo,const struct res_id * type)310*cf2f2c56Smiod read_coff_data_entry (const bfd_byte *data, const struct coff_file_info *finfo, const struct res_id *type)
311f7cc78ecSespie {
312f7cc78ecSespie const struct extern_res_data *erd;
313f7cc78ecSespie struct res_resource *r;
314f7cc78ecSespie unsigned long size, rva;
315f7cc78ecSespie const bfd_byte *resdata;
316f7cc78ecSespie
317f7cc78ecSespie if (type == NULL)
318f7cc78ecSespie fatal (_("resource type unknown"));
319f7cc78ecSespie
320f7cc78ecSespie if ((size_t) (finfo->data_end - data) < sizeof (struct extern_res_data))
321f7cc78ecSespie overrun (finfo, _("data entry"));
322f7cc78ecSespie
323f7cc78ecSespie erd = (const struct extern_res_data *) data;
324f7cc78ecSespie
325f7cc78ecSespie size = getfi_32 (finfo, erd->size);
326f7cc78ecSespie rva = getfi_32 (finfo, erd->rva);
327f7cc78ecSespie if (rva < finfo->secaddr
328f7cc78ecSespie || rva - finfo->secaddr >= (size_t) (finfo->data_end - finfo->data))
329f7cc78ecSespie overrun (finfo, _("resource data"));
330f7cc78ecSespie
331f7cc78ecSespie resdata = finfo->data + (rva - finfo->secaddr);
332f7cc78ecSespie
333f7cc78ecSespie if (size > (size_t) (finfo->data_end - resdata))
334f7cc78ecSespie overrun (finfo, _("resource data size"));
335f7cc78ecSespie
336f7cc78ecSespie r = bin_to_res (*type, resdata, size, finfo->big_endian);
337f7cc78ecSespie
338f7cc78ecSespie memset (&r->res_info, 0, sizeof (struct res_res_info));
339f7cc78ecSespie r->coff_info.codepage = getfi_32 (finfo, erd->codepage);
340f7cc78ecSespie r->coff_info.reserved = getfi_32 (finfo, erd->reserved);
341f7cc78ecSespie
342f7cc78ecSespie return r;
343f7cc78ecSespie }
344f7cc78ecSespie
345f7cc78ecSespie /* This structure is used to build a list of bindata structures. */
346f7cc78ecSespie
347f7cc78ecSespie struct bindata_build
348f7cc78ecSespie {
349f7cc78ecSespie /* The data. */
350f7cc78ecSespie struct bindata *d;
351f7cc78ecSespie /* The last structure we have added to the list. */
352f7cc78ecSespie struct bindata *last;
353f7cc78ecSespie /* The size of the list as a whole. */
354f7cc78ecSespie unsigned long length;
355f7cc78ecSespie };
356f7cc78ecSespie
357f7cc78ecSespie /* This structure keeps track of information as we build the directory
358f7cc78ecSespie tree. */
359f7cc78ecSespie
360f7cc78ecSespie struct coff_write_info
361f7cc78ecSespie {
362f7cc78ecSespie /* These fields are based on the BFD. */
363f7cc78ecSespie /* The BFD itself. */
364f7cc78ecSespie bfd *abfd;
365f7cc78ecSespie /* Non-zero if the file is big endian. */
366f7cc78ecSespie int big_endian;
367f7cc78ecSespie /* Pointer to section symbol used to build RVA relocs. */
368f7cc78ecSespie asymbol **sympp;
369f7cc78ecSespie
370f7cc78ecSespie /* These fields are computed initially, and then not changed. */
371f7cc78ecSespie /* Length of directory tables and entries. */
372f7cc78ecSespie unsigned long dirsize;
373f7cc78ecSespie /* Length of directory entry strings. */
374f7cc78ecSespie unsigned long dirstrsize;
375f7cc78ecSespie /* Length of resource data entries. */
376f7cc78ecSespie unsigned long dataentsize;
377f7cc78ecSespie
378f7cc78ecSespie /* These fields are updated as we add data. */
379f7cc78ecSespie /* Directory tables and entries. */
380f7cc78ecSespie struct bindata_build dirs;
381f7cc78ecSespie /* Directory entry strings. */
382f7cc78ecSespie struct bindata_build dirstrs;
383f7cc78ecSespie /* Resource data entries. */
384f7cc78ecSespie struct bindata_build dataents;
385f7cc78ecSespie /* Actual resource data. */
386f7cc78ecSespie struct bindata_build resources;
387f7cc78ecSespie /* Relocations. */
388f7cc78ecSespie arelent **relocs;
389f7cc78ecSespie /* Number of relocations. */
390f7cc78ecSespie unsigned int reloc_count;
391f7cc78ecSespie };
392f7cc78ecSespie
393f7cc78ecSespie /* Macros to swap out values. */
394f7cc78ecSespie
395f7cc78ecSespie #define putcwi_16(cwi, v, s) \
396f7cc78ecSespie ((cwi->big_endian) ? bfd_putb16 ((v), (s)) : bfd_putl16 ((v), (s)))
397f7cc78ecSespie #define putcwi_32(cwi, v, s) \
398f7cc78ecSespie ((cwi->big_endian) ? bfd_putb32 ((v), (s)) : bfd_putl32 ((v), (s)))
399f7cc78ecSespie
400f7cc78ecSespie static void coff_bin_sizes
401*cf2f2c56Smiod (const struct res_directory *, struct coff_write_info *);
402*cf2f2c56Smiod static unsigned char *coff_alloc (struct bindata_build *, size_t);
403f7cc78ecSespie static void coff_to_bin
404*cf2f2c56Smiod (const struct res_directory *, struct coff_write_info *);
405f7cc78ecSespie static void coff_res_to_bin
406*cf2f2c56Smiod (const struct res_resource *, struct coff_write_info *);
407f7cc78ecSespie
408f7cc78ecSespie /* Write resources to a COFF file. RESOURCES should already be
409f7cc78ecSespie sorted.
410f7cc78ecSespie
411f7cc78ecSespie Right now we always create a new file. Someday we should also
412f7cc78ecSespie offer the ability to merge resources into an existing file. This
413f7cc78ecSespie would require doing the basic work of objcopy, just modifying or
414f7cc78ecSespie adding the .rsrc section. */
415f7cc78ecSespie
416f7cc78ecSespie void
write_coff_file(const char * filename,const char * target,const struct res_directory * resources)417*cf2f2c56Smiod write_coff_file (const char *filename, const char *target,
418*cf2f2c56Smiod const struct res_directory *resources)
419f7cc78ecSespie {
420f7cc78ecSespie bfd *abfd;
421f7cc78ecSespie asection *sec;
422f7cc78ecSespie struct coff_write_info cwi;
423f7cc78ecSespie struct bindata *d;
424f7cc78ecSespie unsigned long length, offset;
425f7cc78ecSespie
426f7cc78ecSespie if (filename == NULL)
427f7cc78ecSespie fatal (_("filename required for COFF output"));
428f7cc78ecSespie
429f7cc78ecSespie abfd = bfd_openw (filename, target);
430f7cc78ecSespie if (abfd == NULL)
431f7cc78ecSespie bfd_fatal (filename);
432f7cc78ecSespie
433f7cc78ecSespie if (! bfd_set_format (abfd, bfd_object))
434f7cc78ecSespie bfd_fatal ("bfd_set_format");
435f7cc78ecSespie
436f7cc78ecSespie #if defined DLLTOOL_SH
437f7cc78ecSespie if (! bfd_set_arch_mach (abfd, bfd_arch_sh, 0))
438f7cc78ecSespie bfd_fatal ("bfd_set_arch_mach(sh)");
439f7cc78ecSespie #elif defined DLLTOOL_MIPS
440f7cc78ecSespie if (! bfd_set_arch_mach (abfd, bfd_arch_mips, 0))
441f7cc78ecSespie bfd_fatal ("bfd_set_arch_mach(mips)");
442f7cc78ecSespie #elif defined DLLTOOL_ARM
443f7cc78ecSespie if (! bfd_set_arch_mach (abfd, bfd_arch_arm, 0))
444f7cc78ecSespie bfd_fatal ("bfd_set_arch_mach(arm)");
445f7cc78ecSespie #else
446f7cc78ecSespie /* FIXME: This is obviously i386 specific. */
447f7cc78ecSespie if (! bfd_set_arch_mach (abfd, bfd_arch_i386, 0))
448f7cc78ecSespie bfd_fatal ("bfd_set_arch_mach(i386)");
449f7cc78ecSespie #endif
450f7cc78ecSespie
451f7cc78ecSespie if (! bfd_set_file_flags (abfd, HAS_SYMS | HAS_RELOC))
452f7cc78ecSespie bfd_fatal ("bfd_set_file_flags");
453f7cc78ecSespie
454f7cc78ecSespie sec = bfd_make_section (abfd, ".rsrc");
455f7cc78ecSespie if (sec == NULL)
456f7cc78ecSespie bfd_fatal ("bfd_make_section");
457f7cc78ecSespie
458f7cc78ecSespie if (! bfd_set_section_flags (abfd, sec,
459f7cc78ecSespie (SEC_HAS_CONTENTS | SEC_ALLOC
460f7cc78ecSespie | SEC_LOAD | SEC_DATA)))
461f7cc78ecSespie bfd_fatal ("bfd_set_section_flags");
462f7cc78ecSespie
463f7cc78ecSespie if (! bfd_set_symtab (abfd, sec->symbol_ptr_ptr, 1))
464f7cc78ecSespie bfd_fatal ("bfd_set_symtab");
465f7cc78ecSespie
466f7cc78ecSespie /* Requiring this is probably a bug in BFD. */
467f7cc78ecSespie sec->output_section = sec;
468f7cc78ecSespie
469f7cc78ecSespie /* The order of data in the .rsrc section is
470f7cc78ecSespie resource directory tables and entries
471f7cc78ecSespie resource directory strings
472f7cc78ecSespie resource data entries
473f7cc78ecSespie actual resource data
474f7cc78ecSespie
475f7cc78ecSespie We build these different types of data in different lists. */
476f7cc78ecSespie
477f7cc78ecSespie cwi.abfd = abfd;
478f7cc78ecSespie cwi.big_endian = bfd_big_endian (abfd);
479f7cc78ecSespie cwi.sympp = sec->symbol_ptr_ptr;
480f7cc78ecSespie cwi.dirsize = 0;
481f7cc78ecSespie cwi.dirstrsize = 0;
482f7cc78ecSespie cwi.dataentsize = 0;
483f7cc78ecSespie cwi.dirs.d = NULL;
484f7cc78ecSespie cwi.dirs.last = NULL;
485f7cc78ecSespie cwi.dirs.length = 0;
486f7cc78ecSespie cwi.dirstrs.d = NULL;
487f7cc78ecSespie cwi.dirstrs.last = NULL;
488f7cc78ecSespie cwi.dirstrs.length = 0;
489f7cc78ecSespie cwi.dataents.d = NULL;
490f7cc78ecSespie cwi.dataents.last = NULL;
491f7cc78ecSespie cwi.dataents.length = 0;
492f7cc78ecSespie cwi.resources.d = NULL;
493f7cc78ecSespie cwi.resources.last = NULL;
494f7cc78ecSespie cwi.resources.length = 0;
495f7cc78ecSespie cwi.relocs = NULL;
496f7cc78ecSespie cwi.reloc_count = 0;
497f7cc78ecSespie
498f7cc78ecSespie /* Work out the sizes of the resource directory entries, so that we
499f7cc78ecSespie know the various offsets we will need. */
500f7cc78ecSespie coff_bin_sizes (resources, &cwi);
501f7cc78ecSespie
502f7cc78ecSespie /* Force the directory strings to be 32 bit aligned. Every other
503f7cc78ecSespie structure is 32 bit aligned anyhow. */
504f7cc78ecSespie cwi.dirstrsize = (cwi.dirstrsize + 3) &~ 3;
505f7cc78ecSespie
506f7cc78ecSespie /* Actually convert the resources to binary. */
507f7cc78ecSespie coff_to_bin (resources, &cwi);
508f7cc78ecSespie
509f7cc78ecSespie /* Add another 2 bytes to the directory strings if needed for
510f7cc78ecSespie alignment. */
511f7cc78ecSespie if ((cwi.dirstrs.length & 3) != 0)
512f7cc78ecSespie {
513f7cc78ecSespie unsigned char *ex;
514f7cc78ecSespie
515f7cc78ecSespie ex = coff_alloc (&cwi.dirstrs, 2);
516f7cc78ecSespie ex[0] = 0;
517f7cc78ecSespie ex[1] = 0;
518f7cc78ecSespie }
519f7cc78ecSespie
520f7cc78ecSespie /* Make sure that the data we built came out to the same size as we
521f7cc78ecSespie calculated initially. */
522f7cc78ecSespie assert (cwi.dirs.length == cwi.dirsize);
523f7cc78ecSespie assert (cwi.dirstrs.length == cwi.dirstrsize);
524f7cc78ecSespie assert (cwi.dataents.length == cwi.dataentsize);
525f7cc78ecSespie
526f7cc78ecSespie length = (cwi.dirsize
527f7cc78ecSespie + cwi.dirstrsize
528f7cc78ecSespie + cwi.dataentsize
529f7cc78ecSespie + cwi.resources.length);
530f7cc78ecSespie
531f7cc78ecSespie if (! bfd_set_section_size (abfd, sec, length))
532f7cc78ecSespie bfd_fatal ("bfd_set_section_size");
533f7cc78ecSespie
534f7cc78ecSespie bfd_set_reloc (abfd, sec, cwi.relocs, cwi.reloc_count);
535f7cc78ecSespie
536f7cc78ecSespie offset = 0;
537f7cc78ecSespie for (d = cwi.dirs.d; d != NULL; d = d->next)
538f7cc78ecSespie {
539f7cc78ecSespie if (! bfd_set_section_contents (abfd, sec, d->data, offset, d->length))
540f7cc78ecSespie bfd_fatal ("bfd_set_section_contents");
541f7cc78ecSespie offset += d->length;
542f7cc78ecSespie }
543f7cc78ecSespie for (d = cwi.dirstrs.d; d != NULL; d = d->next)
544f7cc78ecSespie {
545f7cc78ecSespie if (! bfd_set_section_contents (abfd, sec, d->data, offset, d->length))
546f7cc78ecSespie bfd_fatal ("bfd_set_section_contents");
547f7cc78ecSespie offset += d->length;
548f7cc78ecSespie }
549f7cc78ecSespie for (d = cwi.dataents.d; d != NULL; d = d->next)
550f7cc78ecSespie {
551f7cc78ecSespie if (! bfd_set_section_contents (abfd, sec, d->data, offset, d->length))
552f7cc78ecSespie bfd_fatal ("bfd_set_section_contents");
553f7cc78ecSespie offset += d->length;
554f7cc78ecSespie }
555f7cc78ecSespie for (d = cwi.resources.d; d != NULL; d = d->next)
556f7cc78ecSespie {
557f7cc78ecSespie if (! bfd_set_section_contents (abfd, sec, d->data, offset, d->length))
558f7cc78ecSespie bfd_fatal ("bfd_set_section_contents");
559f7cc78ecSespie offset += d->length;
560f7cc78ecSespie }
561f7cc78ecSespie
562f7cc78ecSespie assert (offset == length);
563f7cc78ecSespie
564f7cc78ecSespie if (! bfd_close (abfd))
565f7cc78ecSespie bfd_fatal ("bfd_close");
566f7cc78ecSespie
567f7cc78ecSespie /* We allocated the relocs array using malloc. */
568f7cc78ecSespie free (cwi.relocs);
569f7cc78ecSespie }
570f7cc78ecSespie
571f7cc78ecSespie /* Work out the sizes of the various fixed size resource directory
572f7cc78ecSespie entries. This updates fields in CWI. */
573f7cc78ecSespie
574f7cc78ecSespie static void
coff_bin_sizes(const struct res_directory * resdir,struct coff_write_info * cwi)575*cf2f2c56Smiod coff_bin_sizes (const struct res_directory *resdir,
576*cf2f2c56Smiod struct coff_write_info *cwi)
577f7cc78ecSespie {
578f7cc78ecSespie const struct res_entry *re;
579f7cc78ecSespie
580f7cc78ecSespie cwi->dirsize += sizeof (struct extern_res_directory);
581f7cc78ecSespie
582f7cc78ecSespie for (re = resdir->entries; re != NULL; re = re->next)
583f7cc78ecSespie {
584f7cc78ecSespie cwi->dirsize += sizeof (struct extern_res_entry);
585f7cc78ecSespie
586f7cc78ecSespie if (re->id.named)
587f7cc78ecSespie cwi->dirstrsize += re->id.u.n.length * 2 + 2;
588f7cc78ecSespie
589f7cc78ecSespie if (re->subdir)
590f7cc78ecSespie coff_bin_sizes (re->u.dir, cwi);
591f7cc78ecSespie else
592f7cc78ecSespie cwi->dataentsize += sizeof (struct extern_res_data);
593f7cc78ecSespie }
594f7cc78ecSespie }
595f7cc78ecSespie
596f7cc78ecSespie /* Allocate data for a particular list. */
597f7cc78ecSespie
598f7cc78ecSespie static unsigned char *
coff_alloc(struct bindata_build * bb,size_t size)599*cf2f2c56Smiod coff_alloc (struct bindata_build *bb, size_t size)
600f7cc78ecSespie {
601f7cc78ecSespie struct bindata *d;
602f7cc78ecSespie
603f7cc78ecSespie d = (struct bindata *) reswr_alloc (sizeof *d);
604f7cc78ecSespie
605f7cc78ecSespie d->next = NULL;
606f7cc78ecSespie d->data = (unsigned char *) reswr_alloc (size);
607f7cc78ecSespie d->length = size;
608f7cc78ecSespie
609f7cc78ecSespie if (bb->d == NULL)
610f7cc78ecSespie bb->d = d;
611f7cc78ecSespie else
612f7cc78ecSespie bb->last->next = d;
613f7cc78ecSespie bb->last = d;
614f7cc78ecSespie bb->length += size;
615f7cc78ecSespie
616f7cc78ecSespie return d->data;
617f7cc78ecSespie }
618f7cc78ecSespie
619f7cc78ecSespie /* Convert the resource directory RESDIR to binary. */
620f7cc78ecSespie
621f7cc78ecSespie static void
coff_to_bin(const struct res_directory * resdir,struct coff_write_info * cwi)622*cf2f2c56Smiod coff_to_bin (const struct res_directory *resdir, struct coff_write_info *cwi)
623f7cc78ecSespie {
624f7cc78ecSespie struct extern_res_directory *erd;
625f7cc78ecSespie int ci, cn;
626f7cc78ecSespie const struct res_entry *e;
627f7cc78ecSespie struct extern_res_entry *ere;
628f7cc78ecSespie
629f7cc78ecSespie /* Write out the directory table. */
630f7cc78ecSespie
631f7cc78ecSespie erd = ((struct extern_res_directory *)
632f7cc78ecSespie coff_alloc (&cwi->dirs, sizeof (*erd)));
633f7cc78ecSespie
634f7cc78ecSespie putcwi_32 (cwi, resdir->characteristics, erd->characteristics);
635f7cc78ecSespie putcwi_32 (cwi, resdir->time, erd->time);
636f7cc78ecSespie putcwi_16 (cwi, resdir->major, erd->major);
637f7cc78ecSespie putcwi_16 (cwi, resdir->minor, erd->minor);
638f7cc78ecSespie
639f7cc78ecSespie ci = 0;
640f7cc78ecSespie cn = 0;
641f7cc78ecSespie for (e = resdir->entries; e != NULL; e = e->next)
642f7cc78ecSespie {
643f7cc78ecSespie if (e->id.named)
644f7cc78ecSespie ++cn;
645f7cc78ecSespie else
646f7cc78ecSespie ++ci;
647f7cc78ecSespie }
648f7cc78ecSespie
649f7cc78ecSespie putcwi_16 (cwi, cn, erd->name_count);
650f7cc78ecSespie putcwi_16 (cwi, ci, erd->id_count);
651f7cc78ecSespie
652f7cc78ecSespie /* Write out the data entries. Note that we allocate space for all
653f7cc78ecSespie the entries before writing them out. That permits a recursive
654f7cc78ecSespie call to work correctly when writing out subdirectories. */
655f7cc78ecSespie
656f7cc78ecSespie ere = ((struct extern_res_entry *)
657f7cc78ecSespie coff_alloc (&cwi->dirs, (ci + cn) * sizeof (*ere)));
658f7cc78ecSespie for (e = resdir->entries; e != NULL; e = e->next, ere++)
659f7cc78ecSespie {
660f7cc78ecSespie if (! e->id.named)
661f7cc78ecSespie putcwi_32 (cwi, e->id.u.id, ere->name);
662f7cc78ecSespie else
663f7cc78ecSespie {
664f7cc78ecSespie unsigned char *str;
665f7cc78ecSespie int i;
666f7cc78ecSespie
667f7cc78ecSespie /* For some reason existing files seem to have the high bit
668f7cc78ecSespie set on the address of the name, although that is not
669f7cc78ecSespie documented. */
670f7cc78ecSespie putcwi_32 (cwi,
671f7cc78ecSespie 0x80000000 | (cwi->dirsize + cwi->dirstrs.length),
672f7cc78ecSespie ere->name);
673f7cc78ecSespie
674f7cc78ecSespie str = coff_alloc (&cwi->dirstrs, e->id.u.n.length * 2 + 2);
675f7cc78ecSespie putcwi_16 (cwi, e->id.u.n.length, str);
676f7cc78ecSespie for (i = 0; i < e->id.u.n.length; i++)
677f7cc78ecSespie putcwi_16 (cwi, e->id.u.n.name[i], str + i * 2 + 2);
678f7cc78ecSespie }
679f7cc78ecSespie
680f7cc78ecSespie if (e->subdir)
681f7cc78ecSespie {
682f7cc78ecSespie putcwi_32 (cwi, 0x80000000 | cwi->dirs.length, ere->rva);
683f7cc78ecSespie coff_to_bin (e->u.dir, cwi);
684f7cc78ecSespie }
685f7cc78ecSespie else
686f7cc78ecSespie {
687f7cc78ecSespie putcwi_32 (cwi,
688f7cc78ecSespie cwi->dirsize + cwi->dirstrsize + cwi->dataents.length,
689f7cc78ecSespie ere->rva);
690f7cc78ecSespie
691f7cc78ecSespie coff_res_to_bin (e->u.res, cwi);
692f7cc78ecSespie }
693f7cc78ecSespie }
694f7cc78ecSespie }
695f7cc78ecSespie
696f7cc78ecSespie /* Convert the resource RES to binary. */
697f7cc78ecSespie
698f7cc78ecSespie static void
coff_res_to_bin(const struct res_resource * res,struct coff_write_info * cwi)699*cf2f2c56Smiod coff_res_to_bin (const struct res_resource *res, struct coff_write_info *cwi)
700f7cc78ecSespie {
701f7cc78ecSespie arelent *r;
702f7cc78ecSespie struct extern_res_data *erd;
703f7cc78ecSespie struct bindata *d;
704f7cc78ecSespie unsigned long length;
705f7cc78ecSespie
706f7cc78ecSespie /* For some reason, although every other address is a section
707f7cc78ecSespie offset, the address of the resource data itself is an RVA. That
708f7cc78ecSespie means that we need to generate a relocation for it. We allocate
709f7cc78ecSespie the relocs array using malloc so that we can use realloc. FIXME:
710f7cc78ecSespie This relocation handling is correct for the i386, but probably
711f7cc78ecSespie not for any other target. */
712f7cc78ecSespie
713f7cc78ecSespie r = (arelent *) reswr_alloc (sizeof (arelent));
714f7cc78ecSespie r->sym_ptr_ptr = cwi->sympp;
715f7cc78ecSespie r->address = cwi->dirsize + cwi->dirstrsize + cwi->dataents.length;
716f7cc78ecSespie r->addend = 0;
717f7cc78ecSespie r->howto = bfd_reloc_type_lookup (cwi->abfd, BFD_RELOC_RVA);
718f7cc78ecSespie if (r->howto == NULL)
719f7cc78ecSespie bfd_fatal (_("can't get BFD_RELOC_RVA relocation type"));
720f7cc78ecSespie
721f7cc78ecSespie cwi->relocs = xrealloc (cwi->relocs,
722f7cc78ecSespie (cwi->reloc_count + 2) * sizeof (arelent *));
723f7cc78ecSespie cwi->relocs[cwi->reloc_count] = r;
724f7cc78ecSespie cwi->relocs[cwi->reloc_count + 1] = NULL;
725f7cc78ecSespie ++cwi->reloc_count;
726f7cc78ecSespie
727f7cc78ecSespie erd = (struct extern_res_data *) coff_alloc (&cwi->dataents, sizeof (*erd));
728f7cc78ecSespie
729f7cc78ecSespie putcwi_32 (cwi,
730f7cc78ecSespie (cwi->dirsize
731f7cc78ecSespie + cwi->dirstrsize
732f7cc78ecSespie + cwi->dataentsize
733f7cc78ecSespie + cwi->resources.length),
734f7cc78ecSespie erd->rva);
735f7cc78ecSespie putcwi_32 (cwi, res->coff_info.codepage, erd->codepage);
736f7cc78ecSespie putcwi_32 (cwi, res->coff_info.reserved, erd->reserved);
737f7cc78ecSespie
738f7cc78ecSespie d = res_to_bin (res, cwi->big_endian);
739f7cc78ecSespie
740f7cc78ecSespie if (cwi->resources.d == NULL)
741f7cc78ecSespie cwi->resources.d = d;
742f7cc78ecSespie else
743f7cc78ecSespie cwi->resources.last->next = d;
744f7cc78ecSespie
745f7cc78ecSespie length = 0;
746f7cc78ecSespie for (; d->next != NULL; d = d->next)
747f7cc78ecSespie length += d->length;
748f7cc78ecSespie length += d->length;
749f7cc78ecSespie cwi->resources.last = d;
750f7cc78ecSespie cwi->resources.length += length;
751f7cc78ecSespie
752f7cc78ecSespie putcwi_32 (cwi, length, erd->size);
753f7cc78ecSespie
754f7cc78ecSespie /* Force the next resource to have 32 bit alignment. */
755f7cc78ecSespie
756f7cc78ecSespie if ((length & 3) != 0)
757f7cc78ecSespie {
758f7cc78ecSespie int add;
759f7cc78ecSespie unsigned char *ex;
760f7cc78ecSespie
761f7cc78ecSespie add = 4 - (length & 3);
762f7cc78ecSespie
763f7cc78ecSespie ex = coff_alloc (&cwi->resources, add);
764f7cc78ecSespie memset (ex, 0, add);
765f7cc78ecSespie }
766f7cc78ecSespie }
767