rescoff.c (78828) | rescoff.c (130561) |
---|---|
1/* rescoff.c -- read and write resources in Windows COFF files. | 1/* rescoff.c -- read and write resources in Windows COFF files. |
2 Copyright 1997, 1998, 1999, 2000 Free Software Foundation, Inc. | 2 Copyright 1997, 1998, 1999, 2000, 2003 3 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. --- 86 unchanged lines hidden (view full) --- 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 | 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 |
105static void overrun PARAMS ((const struct coff_file_info *, const char *)); | 106static void overrun (const struct coff_file_info *, const char *); |
106static struct res_directory *read_coff_res_dir | 107static struct res_directory *read_coff_res_dir |
107 PARAMS ((const bfd_byte *, const struct coff_file_info *, 108 const struct res_id *, int)); | 108 (const bfd_byte *, const struct coff_file_info *, 109 const struct res_id *, int); |
109static struct res_resource *read_coff_data_entry | 110static struct res_resource *read_coff_data_entry |
110 PARAMS ((const bfd_byte *, const struct coff_file_info *, 111 const struct res_id *)); | 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 * | 112 113/* Read the resources in a COFF file. */ 114 115struct res_directory * |
116read_coff_rsrc (filename, target) 117 const char *filename; 118 const char *target; | 116read_coff_rsrc (const char *filename, 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 --- 39 unchanged lines hidden (view full) --- 166 this will have to be cleaned up. */ 167 168 return read_coff_res_dir (data, &finfo, (const struct res_id *) NULL, 0); 169} 170 171/* Give an error if we are out of bounds. */ 172 173static void | 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 |
174overrun (finfo, msg) 175 const struct coff_file_info *finfo; 176 const char *msg; | 172overrun (const struct coff_file_info *finfo, const char *msg) |
177{ 178 fatal (_("%s: %s: address out of bounds"), finfo->filename, msg); 179} 180 181/* Read a resource directory. */ 182 183static struct res_directory * | 173{ 174 fatal (_("%s: %s: address out of bounds"), finfo->filename, msg); 175} 176 177/* Read a resource directory. */ 178 179static struct res_directory * |
184read_coff_res_dir (data, finfo, type, level) 185 const bfd_byte *data; 186 const struct coff_file_info *finfo; 187 const struct res_id *type; 188 int level; | 180read_coff_res_dir (const bfd_byte *data, const struct coff_file_info *finfo, 181 const struct res_id *type, int level) |
189{ 190 const struct extern_res_directory *erd; 191 struct res_directory *rd; 192 int name_count, id_count, i; 193 struct res_entry **pp; 194 const struct extern_res_entry *ere; 195 196 if ((size_t) (finfo->data_end - data) < sizeof (struct extern_res_directory)) --- 112 unchanged lines hidden (view full) --- 309 } 310 311 return rd; 312} 313 314/* Read a resource data entry. */ 315 316static struct res_resource * | 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 * |
317read_coff_data_entry (data, finfo, type) 318 const bfd_byte *data; 319 const struct coff_file_info *finfo; 320 const struct res_id *type; | 310read_coff_data_entry (const bfd_byte *data, const struct coff_file_info *finfo, const struct res_id *type) |
321{ 322 const struct extern_res_data *erd; 323 struct res_resource *r; 324 unsigned long size, rva; 325 const bfd_byte *resdata; 326 327 if (type == NULL) 328 fatal (_("resource type unknown")); --- 74 unchanged lines hidden (view full) --- 403/* Macros to swap out values. */ 404 405#define putcwi_16(cwi, v, s) \ 406 ((cwi->big_endian) ? bfd_putb16 ((v), (s)) : bfd_putl16 ((v), (s))) 407#define putcwi_32(cwi, v, s) \ 408 ((cwi->big_endian) ? bfd_putb32 ((v), (s)) : bfd_putl32 ((v), (s))) 409 410static void coff_bin_sizes | 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 |
411 PARAMS ((const struct res_directory *, struct coff_write_info *)); 412static unsigned char *coff_alloc PARAMS ((struct bindata_build *, size_t)); | 401 (const struct res_directory *, struct coff_write_info *); 402static unsigned char *coff_alloc (struct bindata_build *, size_t); |
413static void coff_to_bin | 403static void coff_to_bin |
414 PARAMS ((const struct res_directory *, struct coff_write_info *)); | 404 (const struct res_directory *, struct coff_write_info *); |
415static void coff_res_to_bin | 405static void coff_res_to_bin |
416 PARAMS ((const struct res_resource *, struct coff_write_info *)); | 406 (const struct res_resource *, struct coff_write_info *); |
417 418/* Write resources to a COFF file. RESOURCES should already be 419 sorted. 420 421 Right now we always create a new file. Someday we should also 422 offer the ability to merge resources into an existing file. This 423 would require doing the basic work of objcopy, just modifying or 424 adding the .rsrc section. */ 425 426void | 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 |
427write_coff_file (filename, target, resources) 428 const char *filename; 429 const char *target; 430 const struct res_directory *resources; | 417write_coff_file (const char *filename, const char *target, 418 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 if (filename == NULL) --- 140 unchanged lines hidden (view full) --- 579 /* We allocated the relocs array using malloc. */ 580 free (cwi.relocs); 581} 582 583/* Work out the sizes of the various fixed size resource directory 584 entries. This updates fields in CWI. */ 585 586static void | 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 |
587coff_bin_sizes (resdir, cwi) 588 const struct res_directory *resdir; 589 struct coff_write_info *cwi; | 575coff_bin_sizes (const struct res_directory *resdir, 576 struct coff_write_info *cwi) |
590{ 591 const struct res_entry *re; 592 593 cwi->dirsize += sizeof (struct extern_res_directory); 594 595 for (re = resdir->entries; re != NULL; re = re->next) 596 { 597 cwi->dirsize += sizeof (struct extern_res_entry); --- 6 unchanged lines hidden (view full) --- 604 else 605 cwi->dataentsize += sizeof (struct extern_res_data); 606 } 607} 608 609/* Allocate data for a particular list. */ 610 611static unsigned char * | 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 * |
612coff_alloc (bb, size) 613 struct bindata_build *bb; 614 size_t size; | 599coff_alloc (struct bindata_build *bb, size_t size) |
615{ 616 struct bindata *d; 617 618 d = (struct bindata *) reswr_alloc (sizeof *d); 619 620 d->next = NULL; 621 d->data = (unsigned char *) reswr_alloc (size); 622 d->length = size; --- 6 unchanged lines hidden (view full) --- 629 bb->length += size; 630 631 return d->data; 632} 633 634/* Convert the resource directory RESDIR to binary. */ 635 636static void | 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 |
637coff_to_bin (resdir, cwi) 638 const struct res_directory *resdir; 639 struct coff_write_info *cwi; | 622coff_to_bin (const struct res_directory *resdir, struct coff_write_info *cwi) |
640{ 641 struct extern_res_directory *erd; 642 int ci, cn; 643 const struct res_entry *e; 644 struct extern_res_entry *ere; 645 646 /* Write out the directory table. */ 647 --- 60 unchanged lines hidden (view full) --- 708 coff_res_to_bin (e->u.res, cwi); 709 } 710 } 711} 712 713/* Convert the resource RES to binary. */ 714 715static void | 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 |
716coff_res_to_bin (res, cwi) 717 const struct res_resource *res; 718 struct coff_write_info *cwi; | 699coff_res_to_bin (const struct res_resource *res, struct coff_write_info *cwi) |
719{ 720 arelent *r; 721 struct extern_res_data *erd; 722 struct bindata *d; 723 unsigned long length; 724 725 /* For some reason, although every other address is a section 726 offset, the address of the resource data itself is an RVA. That --- 59 unchanged lines hidden --- | 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 --- |