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