Deleted Added
sdiff udiff text old ( 78828 ) new ( 130561 )
full compact
1/* rescoff.c -- read and write resources in Windows COFF files.
2 Copyright 1997, 1998, 1999, 2000, 2003
3 Free Software Foundation, Inc.
4 Written by Ian Lance Taylor, Cygnus Support.
5
6 This file is part of GNU Binutils.
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.

--- 86 unchanged lines hidden (view full) ---

98
99/* Macros to swap in values. */
100
101#define getfi_16(fi, s) ((fi)->big_endian ? bfd_getb16 (s) : bfd_getl16 (s))
102#define getfi_32(fi, s) ((fi)->big_endian ? bfd_getb32 (s) : bfd_getl32 (s))
103
104/* Local functions. */
105
106static void overrun (const struct coff_file_info *, const char *);
107static struct res_directory *read_coff_res_dir
108 (const bfd_byte *, const struct coff_file_info *,
109 const struct res_id *, int);
110static struct res_resource *read_coff_data_entry
111 (const bfd_byte *, const struct coff_file_info *, const struct res_id *);
112
113/* Read the resources in a COFF file. */
114
115struct res_directory *
116read_coff_rsrc (const char *filename, const char *target)
117{
118 bfd *abfd;
119 char **matching;
120 asection *sec;
121 bfd_size_type size;
122 bfd_byte *data;
123 struct coff_file_info finfo;
124

--- 39 unchanged lines hidden (view full) ---

164 this will have to be cleaned up. */
165
166 return read_coff_res_dir (data, &finfo, (const struct res_id *) NULL, 0);
167}
168
169/* Give an error if we are out of bounds. */
170
171static void
172overrun (const struct coff_file_info *finfo, const char *msg)
173{
174 fatal (_("%s: %s: address out of bounds"), finfo->filename, msg);
175}
176
177/* Read a resource directory. */
178
179static struct res_directory *
180read_coff_res_dir (const bfd_byte *data, const struct coff_file_info *finfo,
181 const struct res_id *type, int level)
182{
183 const struct extern_res_directory *erd;
184 struct res_directory *rd;
185 int name_count, id_count, i;
186 struct res_entry **pp;
187 const struct extern_res_entry *ere;
188
189 if ((size_t) (finfo->data_end - data) < sizeof (struct extern_res_directory))

--- 112 unchanged lines hidden (view full) ---

302 }
303
304 return rd;
305}
306
307/* Read a resource data entry. */
308
309static struct res_resource *
310read_coff_data_entry (const bfd_byte *data, const struct coff_file_info *finfo, const struct res_id *type)
311{
312 const struct extern_res_data *erd;
313 struct res_resource *r;
314 unsigned long size, rva;
315 const bfd_byte *resdata;
316
317 if (type == NULL)
318 fatal (_("resource type unknown"));

--- 74 unchanged lines hidden (view full) ---

393/* Macros to swap out values. */
394
395#define putcwi_16(cwi, v, s) \
396 ((cwi->big_endian) ? bfd_putb16 ((v), (s)) : bfd_putl16 ((v), (s)))
397#define putcwi_32(cwi, v, s) \
398 ((cwi->big_endian) ? bfd_putb32 ((v), (s)) : bfd_putl32 ((v), (s)))
399
400static void coff_bin_sizes
401 (const struct res_directory *, struct coff_write_info *);
402static unsigned char *coff_alloc (struct bindata_build *, size_t);
403static void coff_to_bin
404 (const struct res_directory *, struct coff_write_info *);
405static void coff_res_to_bin
406 (const struct res_resource *, struct coff_write_info *);
407
408/* Write resources to a COFF file. RESOURCES should already be
409 sorted.
410
411 Right now we always create a new file. Someday we should also
412 offer the ability to merge resources into an existing file. This
413 would require doing the basic work of objcopy, just modifying or
414 adding the .rsrc section. */
415
416void
417write_coff_file (const char *filename, const char *target,
418 const struct res_directory *resources)
419{
420 bfd *abfd;
421 asection *sec;
422 struct coff_write_info cwi;
423 struct bindata *d;
424 unsigned long length, offset;
425
426 if (filename == NULL)

--- 140 unchanged lines hidden (view full) ---

567 /* We allocated the relocs array using malloc. */
568 free (cwi.relocs);
569}
570
571/* Work out the sizes of the various fixed size resource directory
572 entries. This updates fields in CWI. */
573
574static void
575coff_bin_sizes (const struct res_directory *resdir,
576 struct coff_write_info *cwi)
577{
578 const struct res_entry *re;
579
580 cwi->dirsize += sizeof (struct extern_res_directory);
581
582 for (re = resdir->entries; re != NULL; re = re->next)
583 {
584 cwi->dirsize += sizeof (struct extern_res_entry);

--- 6 unchanged lines hidden (view full) ---

591 else
592 cwi->dataentsize += sizeof (struct extern_res_data);
593 }
594}
595
596/* Allocate data for a particular list. */
597
598static unsigned char *
599coff_alloc (struct bindata_build *bb, size_t size)
600{
601 struct bindata *d;
602
603 d = (struct bindata *) reswr_alloc (sizeof *d);
604
605 d->next = NULL;
606 d->data = (unsigned char *) reswr_alloc (size);
607 d->length = size;

--- 6 unchanged lines hidden (view full) ---

614 bb->length += size;
615
616 return d->data;
617}
618
619/* Convert the resource directory RESDIR to binary. */
620
621static void
622coff_to_bin (const struct res_directory *resdir, struct coff_write_info *cwi)
623{
624 struct extern_res_directory *erd;
625 int ci, cn;
626 const struct res_entry *e;
627 struct extern_res_entry *ere;
628
629 /* Write out the directory table. */
630

--- 60 unchanged lines hidden (view full) ---

691 coff_res_to_bin (e->u.res, cwi);
692 }
693 }
694}
695
696/* Convert the resource RES to binary. */
697
698static void
699coff_res_to_bin (const struct res_resource *res, struct coff_write_info *cwi)
700{
701 arelent *r;
702 struct extern_res_data *erd;
703 struct bindata *d;
704 unsigned long length;
705
706 /* For some reason, although every other address is a section
707 offset, the address of the resource data itself is an RVA. That

--- 59 unchanged lines hidden ---