binutils-gdb/binutils/resres.c

731 lines
20 KiB
C
Raw Normal View History

1999-05-03 15:29:11 +08:00
/* resres.c: read_res_file and write_res_file implementation for windres.
Copyright (C) 1998-2021 Free Software Foundation, Inc.
1999-05-03 15:29:11 +08:00
Written by Anders Norlander <anorland@hem2.passagen.se>.
2007-05-23 16:48:29 +08:00
Rewritten by Kai Tietz, Onevision.
1999-05-03 15:29:11 +08:00
This file is part of GNU Binutils.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
2007-07-06 00:54:46 +08:00
the Free Software Foundation; either version 3 of the License, or
1999-05-03 15:29:11 +08:00
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
2005-05-08 22:17:41 +08:00
Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
02110-1301, USA. */
1999-05-03 15:29:11 +08:00
/* FIXME: This file does not work correctly in a cross configuration.
It assumes that it can use fread and fwrite to read and write
integers. It does no swapping. */
#include "sysdep.h"
1999-05-03 15:29:11 +08:00
#include "bfd.h"
#include "bucomm.h"
2007-05-23 16:48:29 +08:00
#include "libiberty.h"
1999-05-03 15:29:11 +08:00
#include "windres.h"
#include <assert.h>
2007-05-23 16:48:29 +08:00
static rc_uint_type write_res_directory (windres_bfd *, rc_uint_type,
const rc_res_directory *, const rc_res_id *,
const rc_res_id *, rc_uint_type *, int);
static rc_uint_type write_res_resource (windres_bfd *, rc_uint_type,const rc_res_id *,
const rc_res_id *, const rc_res_resource *,
rc_uint_type *);
static rc_uint_type write_res_bin (windres_bfd *, rc_uint_type, const rc_res_resource *,
const rc_res_id *, const rc_res_id *,
const rc_res_res_info *);
1999-05-03 15:29:11 +08:00
2007-05-23 16:48:29 +08:00
static rc_uint_type write_res_id (windres_bfd *, rc_uint_type, const rc_res_id *);
static rc_uint_type write_res_info (windres_bfd *, rc_uint_type, const rc_res_res_info *);
static rc_uint_type write_res_data_hdr (windres_bfd *, rc_uint_type, res_hdr *);
1999-05-03 15:29:11 +08:00
2007-05-23 16:48:29 +08:00
static rc_uint_type write_res_header (windres_bfd *, rc_uint_type, rc_uint_type,
const rc_res_id *, const rc_res_id *,
const rc_res_res_info *);
static int read_resource_entry (windres_bfd *, rc_uint_type *, rc_uint_type);
static void read_res_data (windres_bfd *, rc_uint_type *, rc_uint_type, void *,
rc_uint_type);
static void read_res_data_hdr (windres_bfd *, rc_uint_type *, rc_uint_type, res_hdr *);
static void read_res_id (windres_bfd *, rc_uint_type *, rc_uint_type, rc_res_id *);
static unichar *read_unistring (windres_bfd *, rc_uint_type *, rc_uint_type, rc_uint_type *);
static void skip_null_resource (windres_bfd *, rc_uint_type *, rc_uint_type);
static int probe_binary (windres_bfd *wrbfd, rc_uint_type);
static unsigned long get_id_size (const rc_res_id *);
static void res_add_resource (rc_res_resource *, const rc_res_id *,
const rc_res_id *, rc_uint_type, int);
1999-05-03 15:29:11 +08:00
2007-05-23 16:48:29 +08:00
static void res_append_resource (rc_res_directory **, rc_res_resource *,
int, const rc_res_id *, int);
static rc_res_directory *resources = NULL;
1999-05-03 15:29:11 +08:00
static const char *filename;
extern char *program_name;
/* Read resource file */
2007-05-23 16:48:29 +08:00
rc_res_directory *
read_res_file (const char *fn)
1999-05-03 15:29:11 +08:00
{
2007-05-23 16:48:29 +08:00
rc_uint_type off, flen;
windres_bfd wrbfd;
bfd *abfd;
asection *sec;
1999-05-03 15:29:11 +08:00
filename = fn;
2007-05-23 16:48:29 +08:00
flen = (rc_uint_type) get_file_size (filename);
if (! flen)
fatal ("can't open '%s' for input.", filename);
abfd = windres_open_as_binary (filename, 1);
sec = bfd_get_section_by_name (abfd, ".data");
if (sec == NULL)
bfd_fatal ("bfd_get_section_by_name");
set_windres_bfd (&wrbfd, abfd, sec,
(target_is_bigendian ? WR_KIND_BFD_BIN_B
: WR_KIND_BFD_BIN_L));
off = 0;
if (! probe_binary (&wrbfd, flen))
2011-06-02 21:43:24 +08:00
set_windres_bfd_endianness (&wrbfd, ! target_is_bigendian);
2007-05-23 16:48:29 +08:00
skip_null_resource (&wrbfd, &off, flen);
1999-05-03 15:29:11 +08:00
2007-05-23 16:48:29 +08:00
while (read_resource_entry (&wrbfd, &off, flen))
1999-05-03 15:29:11 +08:00
;
2007-05-23 16:48:29 +08:00
bfd_close (abfd);
1999-05-03 15:29:11 +08:00
return resources;
}
/* Write resource file */
void
2007-05-23 16:48:29 +08:00
write_res_file (const char *fn,const rc_res_directory *resdir)
1999-05-03 15:29:11 +08:00
{
2007-05-23 16:48:29 +08:00
asection *sec;
rc_uint_type language;
bfd *abfd;
windres_bfd wrbfd;
unsigned long sec_length = 0,sec_length_wrote;
static const bfd_byte sign[] =
1999-05-03 15:29:11 +08:00
{0x00, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00,
0xFF, 0xFF, 0x00, 0x00, 0xFF, 0xFF, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
filename = fn;
2007-05-23 16:48:29 +08:00
abfd = windres_open_as_binary (filename, 0);
sec = bfd_make_section_with_flags (abfd, ".data",
(SEC_HAS_CONTENTS | SEC_ALLOC
| SEC_LOAD | SEC_DATA));
2007-05-23 16:48:29 +08:00
if (sec == NULL)
bfd_fatal ("bfd_make_section");
/* Requiring this is probably a bug in BFD. */
sec->output_section = sec;
set_windres_bfd (&wrbfd, abfd, sec,
(target_is_bigendian ? WR_KIND_BFD_BIN_B
: WR_KIND_BFD_BIN_L));
1999-05-03 15:29:11 +08:00
language = -1;
2007-05-23 16:48:29 +08:00
sec_length = write_res_directory ((windres_bfd *) NULL, 0x20UL, resdir,
(const rc_res_id *) NULL,
(const rc_res_id *) NULL, &language, 1);
bfd_section_* macros This large patch removes the unnecessary bfd parameter from various bfd section macros and functions. The bfd is hardly ever used and if needed for the bfd_set_section_* or bfd_rename_section functions can be found via section->owner except for the com, und, abs, and ind std_section special sections. Those sections shouldn't be modified anyway. The patch also removes various bfd_get_section_<field> macros, replacing their use with bfd_section_<field>, and adds bfd_set_section_lma. I've also fixed a minor bug in gas where compressed section renaming was done directly rather than calling bfd_rename_section. This would have broken bfd_get_section_by_name and similar functions, but that hardly mattered at such a late stage in gas processing. bfd/ * bfd-in.h (bfd_get_section_name, bfd_get_section_vma), (bfd_get_section_lma, bfd_get_section_alignment), (bfd_get_section_size, bfd_get_section_flags), (bfd_get_section_userdata): Delete. (bfd_section_name, bfd_section_size, bfd_section_vma), (bfd_section_lma, bfd_section_alignment): Lose bfd parameter. (bfd_section_flags, bfd_section_userdata): New. (bfd_is_com_section): Rename parameter. * section.c (bfd_set_section_userdata, bfd_set_section_vma), (bfd_set_section_alignment, bfd_set_section_flags, bfd_rename_section), (bfd_set_section_size): Delete bfd parameter, rename section parameter. (bfd_set_section_lma): New. * bfd-in2.h: Regenerate. * mach-o.c (bfd_mach_o_init_section_from_mach_o): Delete bfd param, update callers. * aoutx.h, * bfd.c, * coff-alpha.c, * coff-arm.c, * coff-mips.c, * coff64-rs6000.c, * coffcode.h, * coffgen.c, * cofflink.c, * compress.c, * ecoff.c, * elf-eh-frame.c, * elf-hppa.h, * elf-ifunc.c, * elf-m10200.c, * elf-m10300.c, * elf-properties.c, * elf-s390-common.c, * elf-vxworks.c, * elf.c, * elf32-arc.c, * elf32-arm.c, * elf32-avr.c, * elf32-bfin.c, * elf32-cr16.c, * elf32-cr16c.c, * elf32-cris.c, * elf32-crx.c, * elf32-csky.c, * elf32-d10v.c, * elf32-epiphany.c, * elf32-fr30.c, * elf32-frv.c, * elf32-ft32.c, * elf32-h8300.c, * elf32-hppa.c, * elf32-i386.c, * elf32-ip2k.c, * elf32-iq2000.c, * elf32-lm32.c, * elf32-m32c.c, * elf32-m32r.c, * elf32-m68hc1x.c, * elf32-m68k.c, * elf32-mcore.c, * elf32-mep.c, * elf32-metag.c, * elf32-microblaze.c, * elf32-moxie.c, * elf32-msp430.c, * elf32-mt.c, * elf32-nds32.c, * elf32-nios2.c, * elf32-or1k.c, * elf32-ppc.c, * elf32-pru.c, * elf32-rl78.c, * elf32-rx.c, * elf32-s390.c, * elf32-score.c, * elf32-score7.c, * elf32-sh.c, * elf32-spu.c, * elf32-tic6x.c, * elf32-tilepro.c, * elf32-v850.c, * elf32-vax.c, * elf32-visium.c, * elf32-xstormy16.c, * elf32-xtensa.c, * elf64-alpha.c, * elf64-bpf.c, * elf64-hppa.c, * elf64-ia64-vms.c, * elf64-mmix.c, * elf64-ppc.c, * elf64-s390.c, * elf64-sparc.c, * elf64-x86-64.c, * elflink.c, * elfnn-aarch64.c, * elfnn-ia64.c, * elfnn-riscv.c, * elfxx-aarch64.c, * elfxx-mips.c, * elfxx-sparc.c, * elfxx-tilegx.c, * elfxx-x86.c, * i386msdos.c, * linker.c, * mach-o.c, * mmo.c, * opncls.c, * pdp11.c, * pei-x86_64.c, * peicode.h, * reloc.c, * section.c, * syms.c, * vms-alpha.c, * xcofflink.c: Update throughout for bfd section macro and function changes. binutils/ * addr2line.c, * bucomm.c, * coffgrok.c, * dlltool.c, * nm.c, * objcopy.c, * objdump.c, * od-elf32_avr.c, * od-macho.c, * od-xcoff.c, * prdbg.c, * rdcoff.c, * rddbg.c, * rescoff.c, * resres.c, * size.c, * srconv.c, * strings.c, * windmc.c: Update throughout for bfd section macro and function changes. gas/ * as.c, * as.h, * dw2gencfi.c, * dwarf2dbg.c, * ecoff.c, * read.c, * stabs.c, * subsegs.c, * subsegs.h, * write.c, * config/obj-coff-seh.c, * config/obj-coff.c, * config/obj-ecoff.c, * config/obj-elf.c, * config/obj-macho.c, * config/obj-som.c, * config/tc-aarch64.c, * config/tc-alpha.c, * config/tc-arc.c, * config/tc-arm.c, * config/tc-avr.c, * config/tc-bfin.c, * config/tc-bpf.c, * config/tc-d10v.c, * config/tc-d30v.c, * config/tc-epiphany.c, * config/tc-fr30.c, * config/tc-frv.c, * config/tc-h8300.c, * config/tc-hppa.c, * config/tc-i386.c, * config/tc-ia64.c, * config/tc-ip2k.c, * config/tc-iq2000.c, * config/tc-lm32.c, * config/tc-m32c.c, * config/tc-m32r.c, * config/tc-m68hc11.c, * config/tc-mep.c, * config/tc-microblaze.c, * config/tc-mips.c, * config/tc-mmix.c, * config/tc-mn10200.c, * config/tc-mn10300.c, * config/tc-msp430.c, * config/tc-mt.c, * config/tc-nds32.c, * config/tc-or1k.c, * config/tc-ppc.c, * config/tc-pru.c, * config/tc-rl78.c, * config/tc-rx.c, * config/tc-s12z.c, * config/tc-s390.c, * config/tc-score.c, * config/tc-score7.c, * config/tc-sh.c, * config/tc-sparc.c, * config/tc-spu.c, * config/tc-tic4x.c, * config/tc-tic54x.c, * config/tc-tic6x.c, * config/tc-tilegx.c, * config/tc-tilepro.c, * config/tc-v850.c, * config/tc-visium.c, * config/tc-wasm32.c, * config/tc-xc16x.c, * config/tc-xgate.c, * config/tc-xstormy16.c, * config/tc-xtensa.c, * config/tc-z8k.c: Update throughout for bfd section macro and function changes. * write.c (compress_debug): Use bfd_rename_section. gdb/ * aarch64-linux-tdep.c, * arm-tdep.c, * auto-load.c, * coff-pe-read.c, * coffread.c, * corelow.c, * dbxread.c, * dicos-tdep.c, * dwarf2-frame.c, * dwarf2read.c, * elfread.c, * exec.c, * fbsd-tdep.c, * gcore.c, * gdb_bfd.c, * gdb_bfd.h, * hppa-tdep.c, * i386-cygwin-tdep.c, * i386-fbsd-tdep.c, * i386-linux-tdep.c, * jit.c, * linux-tdep.c, * machoread.c, * maint.c, * mdebugread.c, * minidebug.c, * mips-linux-tdep.c, * mips-sde-tdep.c, * mips-tdep.c, * mipsread.c, * nto-tdep.c, * objfiles.c, * objfiles.h, * osabi.c, * ppc-linux-tdep.c, * ppc64-tdep.c, * record-btrace.c, * record-full.c, * remote.c, * rs6000-aix-tdep.c, * rs6000-tdep.c, * s390-linux-tdep.c, * s390-tdep.c, * solib-aix.c, * solib-dsbt.c, * solib-frv.c, * solib-spu.c, * solib-svr4.c, * solib-target.c, * spu-linux-nat.c, * spu-tdep.c, * symfile-mem.c, * symfile.c, * symmisc.c, * symtab.c, * target.c, * windows-nat.c, * xcoffread.c, * cli/cli-dump.c, * compile/compile-object-load.c, * mi/mi-interp.c: Update throughout for bfd section macro and function changes. * gcore (gcore_create_callback): Use bfd_set_section_lma. * spu-tdep.c (spu_overlay_new_objfile): Likewise. gprof/ * corefile.c, * symtab.c: Update throughout for bfd section macro and function changes. ld/ * ldcref.c, * ldctor.c, * ldelf.c, * ldlang.c, * pe-dll.c, * emultempl/aarch64elf.em, * emultempl/aix.em, * emultempl/armcoff.em, * emultempl/armelf.em, * emultempl/cr16elf.em, * emultempl/cskyelf.em, * emultempl/m68hc1xelf.em, * emultempl/m68kelf.em, * emultempl/mipself.em, * emultempl/mmix-elfnmmo.em, * emultempl/mmo.em, * emultempl/msp430.em, * emultempl/nios2elf.em, * emultempl/pe.em, * emultempl/pep.em, * emultempl/ppc64elf.em, * emultempl/xtensaelf.em: Update throughout for bfd section macro and function changes. libctf/ * ctf-open-bfd.c: Update throughout for bfd section macro changes. opcodes/ * arc-ext.c: Update throughout for bfd section macro changes. sim/ * common/sim-load.c, * common/sim-utils.c, * cris/sim-if.c, * erc32/func.c, * lm32/sim-if.c, * m32c/load.c, * m32c/trace.c, * m68hc11/interp.c, * ppc/hw_htab.c, * ppc/hw_init.c, * rl78/load.c, * rl78/trace.c, * rx/gdb-if.c, * rx/load.c, * rx/trace.c: Update throughout for bfd section macro changes.
2019-09-16 18:55:17 +08:00
if (!bfd_set_section_size (sec, (sec_length + 3) & ~3))
2007-05-23 16:48:29 +08:00
bfd_fatal ("bfd_set_section_size");
if ((sec_length & 3) != 0)
set_windres_bfd_content (&wrbfd, sign, sec_length, 4-(sec_length & 3));
set_windres_bfd_content (&wrbfd, sign, 0, sizeof (sign));
language = -1;
sec_length_wrote = write_res_directory (&wrbfd, 0x20UL, resdir,
(const rc_res_id *) NULL,
(const rc_res_id *) NULL,
&language, 1);
if (sec_length != sec_length_wrote)
2008-07-30 12:34:58 +08:00
fatal ("res write failed with different sizes (%lu/%lu).",
(unsigned long) sec_length, (unsigned long) sec_length_wrote);
2007-05-23 16:48:29 +08:00
bfd_close (abfd);
return;
1999-05-03 15:29:11 +08:00
}
/* Read a resource entry, returns 0 when all resources are read */
static int
2007-05-23 16:48:29 +08:00
read_resource_entry (windres_bfd *wrbfd, rc_uint_type *off, rc_uint_type omax)
1999-05-03 15:29:11 +08:00
{
2007-05-23 16:48:29 +08:00
rc_res_id type;
rc_res_id name;
rc_res_res_info resinfo;
res_hdr reshdr;
1999-05-03 15:29:11 +08:00
void *buff;
2007-05-23 16:48:29 +08:00
rc_res_resource *r;
struct bin_res_info l;
1999-05-03 15:29:11 +08:00
2007-05-23 16:48:29 +08:00
off[0] = (off[0] + 3) & ~3;
1999-05-03 15:29:11 +08:00
/* Read header */
2007-05-23 16:48:29 +08:00
if ((off[0] + 8) > omax)
1999-05-03 15:29:11 +08:00
return 0;
2007-05-23 16:48:29 +08:00
read_res_data_hdr (wrbfd, off, omax, &reshdr);
1999-05-03 15:29:11 +08:00
/* read resource type */
2007-05-23 16:48:29 +08:00
read_res_id (wrbfd, off, omax, &type);
1999-05-03 15:29:11 +08:00
/* read resource id */
2007-05-23 16:48:29 +08:00
read_res_id (wrbfd, off, omax, &name);
1999-05-03 15:29:11 +08:00
2007-05-23 16:48:29 +08:00
off[0] = (off[0] + 3) & ~3;
1999-05-03 15:29:11 +08:00
/* Read additional resource header */
2007-05-23 16:48:29 +08:00
read_res_data (wrbfd, off, omax, &l, BIN_RES_INFO_SIZE);
resinfo.version = windres_get_32 (wrbfd, l.version, 4);
resinfo.memflags = windres_get_16 (wrbfd, l.memflags, 2);
resinfo.language = windres_get_16 (wrbfd, l.language, 2);
/* resinfo.version2 = windres_get_32 (wrbfd, l.version2, 4); */
resinfo.characteristics = windres_get_32 (wrbfd, l.characteristics, 4);
1999-05-03 15:29:11 +08:00
2007-05-23 16:48:29 +08:00
off[0] = (off[0] + 3) & ~3;
1999-05-03 15:29:11 +08:00
/* Allocate buffer for data */
buff = res_alloc (reshdr.data_size);
/* Read data */
2007-05-23 16:48:29 +08:00
read_res_data (wrbfd, off, omax, buff, reshdr.data_size);
1999-05-03 15:29:11 +08:00
/* Convert binary data to resource */
2007-05-23 16:48:29 +08:00
r = bin_to_res (wrbfd, type, buff, reshdr.data_size);
1999-05-03 15:29:11 +08:00
r->res_info = resinfo;
/* Add resource to resource directory */
res_add_resource (r, &type, &name, resinfo.language, 0);
return 1;
}
/* write resource directory to binary resource file */
2007-05-23 16:48:29 +08:00
static rc_uint_type
write_res_directory (windres_bfd *wrbfd, rc_uint_type off, const rc_res_directory *rd,
const rc_res_id *type, const rc_res_id *name, rc_uint_type *language,
int level)
1999-05-03 15:29:11 +08:00
{
2007-05-23 16:48:29 +08:00
const rc_res_entry *re;
1999-05-03 15:29:11 +08:00
for (re = rd->entries; re != NULL; re = re->next)
{
switch (level)
{
case 1:
/* If we're at level 1, the key of this resource is the
type. This normally duplicates the information we have
stored with the resource itself, but we need to remember
the type if this is a user define resource type. */
type = &re->id;
break;
case 2:
/* If we're at level 2, the key of this resource is the name
we are going to use in the rc printout. */
1999-05-03 15:29:11 +08:00
name = &re->id;
break;
case 3:
/* If we're at level 3, then this key represents a language.
Use it to update the current language. */
2007-05-23 16:48:29 +08:00
if (! re->id.named
&& re->id.u.id != (unsigned long) *language
1999-05-03 15:29:11 +08:00
&& (re->id.u.id & 0xffff) == re->id.u.id)
{
*language = re->id.u.id;
}
break;
default:
break;
}
if (re->subdir)
2007-05-23 16:48:29 +08:00
off = write_res_directory (wrbfd, off, re->u.dir, type, name, language,
level + 1);
1999-05-03 15:29:11 +08:00
else
{
if (level == 3)
{
/* This is the normal case: the three levels are
TYPE/NAME/LANGUAGE. NAME will have been set at level
2, and represents the name to use. We probably just
set LANGUAGE, and it will probably match what the
resource itself records if anything. */
2007-05-23 16:48:29 +08:00
off = write_res_resource (wrbfd, off, type, name, re->u.res,
language);
1999-05-03 15:29:11 +08:00
}
else
{
fprintf (stderr, "// Resource at unexpected level %d\n", level);
2007-05-23 16:48:29 +08:00
off = write_res_resource (wrbfd, off, type, (rc_res_id *) NULL,
re->u.res, language);
1999-05-03 15:29:11 +08:00
}
}
}
2007-05-23 16:48:29 +08:00
return off;
1999-05-03 15:29:11 +08:00
}
2007-05-23 16:48:29 +08:00
static rc_uint_type
write_res_resource (windres_bfd *wrbfd, rc_uint_type off, const rc_res_id *type,
const rc_res_id *name, const rc_res_resource *res,
rc_uint_type *language ATTRIBUTE_UNUSED)
1999-05-03 15:29:11 +08:00
{
int rt;
switch (res->type)
{
default:
abort ();
case RES_TYPE_ACCELERATOR:
rt = RT_ACCELERATOR;
break;
case RES_TYPE_BITMAP:
rt = RT_BITMAP;
break;
case RES_TYPE_CURSOR:
rt = RT_CURSOR;
break;
case RES_TYPE_GROUP_CURSOR:
rt = RT_GROUP_CURSOR;
break;
case RES_TYPE_DIALOG:
rt = RT_DIALOG;
break;
case RES_TYPE_FONT:
rt = RT_FONT;
break;
case RES_TYPE_FONTDIR:
rt = RT_FONTDIR;
break;
case RES_TYPE_ICON:
rt = RT_ICON;
break;
case RES_TYPE_GROUP_ICON:
rt = RT_GROUP_ICON;
break;
case RES_TYPE_MENU:
rt = RT_MENU;
break;
case RES_TYPE_MESSAGETABLE:
rt = RT_MESSAGETABLE;
break;
case RES_TYPE_RCDATA:
rt = RT_RCDATA;
break;
case RES_TYPE_STRINGTABLE:
rt = RT_STRING;
break;
case RES_TYPE_USERDATA:
rt = 0;
break;
case RES_TYPE_VERSIONINFO:
rt = RT_VERSION;
break;
2007-05-23 16:48:29 +08:00
case RES_TYPE_TOOLBAR:
rt = RT_TOOLBAR;
break;
1999-05-03 15:29:11 +08:00
}
if (rt != 0
&& type != NULL
&& (type->named || type->u.id != (unsigned long) rt))
1999-05-03 15:29:11 +08:00
{
fprintf (stderr, "// Unexpected resource type mismatch: ");
res_id_print (stderr, *type, 1);
fprintf (stderr, " != %d", rt);
abort ();
}
2007-05-23 16:48:29 +08:00
return write_res_bin (wrbfd, off, res, type, name, &res->res_info);
1999-05-03 15:29:11 +08:00
}
/* Write a resource in binary resource format */
2007-05-23 16:48:29 +08:00
static rc_uint_type
write_res_bin (windres_bfd *wrbfd, rc_uint_type off, const rc_res_resource *res,
const rc_res_id *type, const rc_res_id *name,
const rc_res_res_info *resinfo)
1999-05-03 15:29:11 +08:00
{
2007-05-23 16:48:29 +08:00
rc_uint_type noff;
rc_uint_type datasize = 0;
1999-05-03 15:29:11 +08:00
2007-05-23 16:48:29 +08:00
noff = res_to_bin ((windres_bfd *) NULL, off, res);
datasize = noff - off;
1999-05-03 15:29:11 +08:00
2007-05-23 16:48:29 +08:00
off = write_res_header (wrbfd, off, datasize, type, name, resinfo);
return res_to_bin (wrbfd, off, res);
1999-05-03 15:29:11 +08:00
}
/* Get number of bytes needed to store an id in binary format */
static unsigned long
update many old style function definitions This includes regenerating a bunch of files in opcodes/ with trunk cgen. gprof/ChangeLog: 2016-04-20 Trevor Saunders <tbsaunde+binutils@tbsaunde.org> * basic_blocks.c: Update old style function definitions. * cg_arcs.c: Likewise. * cg_print.c: Likewise. * gen-c-prog.awk: Likewise. * gmon_io.c: Likewise. * hertz.c: Likewise. * hist.c: Likewise. * sym_ids.c: Likewise. bfd/ChangeLog: 2016-04-20 Trevor Saunders <tbsaunde+binutils@tbsaunde.org> * cache.c: Update old style function definitions. * elf32-m68k.c: Likewise. * elf64-mmix.c: Likewise. * stab-syms.c: Likewise. opcodes/ChangeLog: 2016-04-20 Trevor Saunders <tbsaunde+binutils@tbsaunde.org> * alpha-dis.c: Regenerate. * crx-dis.c: Likewise. * disassemble.c: Likewise. * epiphany-opc.c: Likewise. * fr30-opc.c: Likewise. * frv-opc.c: Likewise. * ip2k-opc.c: Likewise. * iq2000-opc.c: Likewise. * lm32-opc.c: Likewise. * lm32-opinst.c: Likewise. * m32c-opc.c: Likewise. * m32r-opc.c: Likewise. * m32r-opinst.c: Likewise. * mep-opc.c: Likewise. * mt-opc.c: Likewise. * or1k-opc.c: Likewise. * or1k-opinst.c: Likewise. * tic80-opc.c: Likewise. * xc16x-opc.c: Likewise. * xstormy16-opc.c: Likewise. ld/ChangeLog: 2016-04-20 Trevor Saunders <tbsaunde+binutils@tbsaunde.org> * emultempl/scoreelf.em: Likewise. binutils/ChangeLog: 2016-04-20 Trevor Saunders <tbsaunde+binutils@tbsaunde.org> * resres.c: Likewise. gas/ChangeLog: 2016-04-20 Trevor Saunders <tbsaunde+binutils@tbsaunde.org> * cgen.c: Likewise. * config/tc-bfin.c: Likewise. * config/tc-ia64.c: Likewise. * config/tc-mep.c: Likewise. * config/tc-metag.c: Likewise. * config/tc-nios2.c: Likewise. * config/tc-rl78.c: Likewise.
2016-04-14 06:30:46 +08:00
get_id_size (const rc_res_id *id)
1999-05-03 15:29:11 +08:00
{
if (id->named)
return sizeof (unichar) * (id->u.n.length + 1);
else
return sizeof (unichar) * 2;
}
/* Write a resource header */
2007-05-23 16:48:29 +08:00
static rc_uint_type
write_res_header (windres_bfd *wrbfd, rc_uint_type off, rc_uint_type datasize,
const rc_res_id *type, const rc_res_id *name,
const rc_res_res_info *resinfo)
1999-05-03 15:29:11 +08:00
{
2007-05-23 16:48:29 +08:00
res_hdr reshdr;
1999-05-03 15:29:11 +08:00
reshdr.data_size = datasize;
reshdr.header_size = 24 + get_id_size (type) + get_id_size (name);
reshdr.header_size = (reshdr.header_size + 3) & ~3;
2007-05-23 16:48:29 +08:00
off = (off + 3) & ~3;
off = write_res_data_hdr (wrbfd, off, &reshdr);
off = write_res_id (wrbfd, off, type);
off = write_res_id (wrbfd, off, name);
1999-05-03 15:29:11 +08:00
2007-05-23 16:48:29 +08:00
off = (off + 3) & ~3;
1999-05-03 15:29:11 +08:00
2007-05-23 16:48:29 +08:00
off = write_res_info (wrbfd, off, resinfo);
off = (off + 3) & ~3;
return off;
1999-05-03 15:29:11 +08:00
}
2007-05-23 16:48:29 +08:00
static rc_uint_type
write_res_data_hdr (windres_bfd *wrbfd, rc_uint_type off, res_hdr *hdr)
{
if (wrbfd)
{
struct bin_res_hdr brh;
windres_put_32 (wrbfd, brh.data_size, hdr->data_size);
windres_put_32 (wrbfd, brh.header_size, hdr->header_size);
set_windres_bfd_content (wrbfd, &brh, off, BIN_RES_HDR_SIZE);
}
return off + BIN_RES_HDR_SIZE;
}
1999-05-03 15:29:11 +08:00
static void
2007-05-23 16:48:29 +08:00
read_res_data_hdr (windres_bfd *wrbfd, rc_uint_type *off, rc_uint_type omax,
res_hdr *reshdr)
1999-05-03 15:29:11 +08:00
{
2007-05-23 16:48:29 +08:00
struct bin_res_hdr brh;
if ((off[0] + BIN_RES_HDR_SIZE) > omax)
fatal ("%s: unexpected end of file %ld/%ld", filename,(long) off[0], (long) omax);
get_windres_bfd_content (wrbfd, &brh, off[0], BIN_RES_HDR_SIZE);
reshdr->data_size = windres_get_32 (wrbfd, brh.data_size, 4);
reshdr->header_size = windres_get_32 (wrbfd, brh.header_size, 4);
off[0] += BIN_RES_HDR_SIZE;
1999-05-03 15:29:11 +08:00
}
/* Read data from file, abort on failure */
static void
2007-05-23 16:48:29 +08:00
read_res_data (windres_bfd *wrbfd, rc_uint_type *off, rc_uint_type omax, void *data,
rc_uint_type size)
1999-05-03 15:29:11 +08:00
{
2007-05-23 16:48:29 +08:00
if ((off[0] + size) > omax)
fatal ("%s: unexpected end of file %ld/%ld %ld", filename,(long) off[0],
(long) omax, (long) size);
get_windres_bfd_content (wrbfd, data, off[0], size);
off[0] += size;
1999-05-03 15:29:11 +08:00
}
/* Write a resource id */
2007-05-23 16:48:29 +08:00
static rc_uint_type
write_res_id (windres_bfd *wrbfd, rc_uint_type off, const rc_res_id *id)
1999-05-03 15:29:11 +08:00
{
if (id->named)
{
2007-05-23 16:48:29 +08:00
rc_uint_type len = (((bfd_signed_vma) id->u.n.length < 0 ? 0 : id->u.n.length) + 1);
if (wrbfd)
{
rc_uint_type i;
bfd_byte *d = (bfd_byte *) xmalloc (len * sizeof (unichar));
for (i = 0; i < (len - 1); i++)
windres_put_16 (wrbfd, d + (i * sizeof (unichar)), id->u.n.name[i]);
windres_put_16 (wrbfd, d + (i * sizeof (unichar)), 0);
set_windres_bfd_content (wrbfd, d, off, (len * sizeof (unichar)));
}
off += (len * sizeof (unichar));
1999-05-03 15:29:11 +08:00
}
else
{
2007-05-23 16:48:29 +08:00
if (wrbfd)
{
struct bin_res_id bid;
windres_put_16 (wrbfd, bid.sig, 0xffff);
windres_put_16 (wrbfd, bid.id, id->u.id);
set_windres_bfd_content (wrbfd, &bid, off, BIN_RES_ID);
}
off += BIN_RES_ID;
1999-05-03 15:29:11 +08:00
}
2007-05-23 16:48:29 +08:00
return off;
1999-05-03 15:29:11 +08:00
}
/* Write resource info */
2007-05-23 16:48:29 +08:00
static rc_uint_type
write_res_info (windres_bfd *wrbfd, rc_uint_type off, const rc_res_res_info *info)
1999-05-03 15:29:11 +08:00
{
2007-05-23 16:48:29 +08:00
if (wrbfd)
{
struct bin_res_info l;
2015-08-12 19:42:37 +08:00
2007-05-23 16:48:29 +08:00
windres_put_32 (wrbfd, l.version, info->version);
windres_put_16 (wrbfd, l.memflags, info->memflags);
windres_put_16 (wrbfd, l.language, info->language);
windres_put_32 (wrbfd, l.version2, info->version);
windres_put_32 (wrbfd, l.characteristics, info->characteristics);
set_windres_bfd_content (wrbfd, &l, off, BIN_RES_INFO_SIZE);
}
return off + BIN_RES_INFO_SIZE;
1999-05-03 15:29:11 +08:00
}
/* read a resource identifier */
2007-05-23 16:48:29 +08:00
static void
read_res_id (windres_bfd *wrbfd, rc_uint_type *off, rc_uint_type omax, rc_res_id *id)
1999-05-03 15:29:11 +08:00
{
2007-05-23 16:48:29 +08:00
struct bin_res_id bid;
1999-05-03 15:29:11 +08:00
unsigned short ord;
unichar *id_s = NULL;
2007-05-23 16:48:29 +08:00
rc_uint_type len;
1999-05-03 15:29:11 +08:00
2007-05-23 16:48:29 +08:00
read_res_data (wrbfd, off, omax, &bid, BIN_RES_ID - 2);
ord = (unsigned short) windres_get_16 (wrbfd, bid.sig, 2);
1999-05-03 15:29:11 +08:00
if (ord == 0xFFFF) /* an ordinal id */
{
2007-05-23 16:48:29 +08:00
read_res_data (wrbfd, off, omax, bid.id, BIN_RES_ID - 2);
1999-05-03 15:29:11 +08:00
id->named = 0;
2007-05-23 16:48:29 +08:00
id->u.id = windres_get_16 (wrbfd, bid.id, 2);
1999-05-03 15:29:11 +08:00
}
else
/* named id */
{
2007-05-23 16:48:29 +08:00
off[0] -= 2;
id_s = read_unistring (wrbfd, off, omax, &len);
1999-05-03 15:29:11 +08:00
id->named = 1;
id->u.n.length = len;
id->u.n.name = id_s;
}
}
/* Read a null terminated UNICODE string */
static unichar *
2007-05-23 16:48:29 +08:00
read_unistring (windres_bfd *wrbfd, rc_uint_type *off, rc_uint_type omax,
rc_uint_type *len)
1999-05-03 15:29:11 +08:00
{
unichar *s;
2007-05-23 16:48:29 +08:00
bfd_byte d[2];
1999-05-03 15:29:11 +08:00
unichar c;
unichar *p;
2007-05-23 16:48:29 +08:00
rc_uint_type l;
rc_uint_type soff = off[0];
1999-05-03 15:29:11 +08:00
do
{
read_res_data (wrbfd, &soff, omax, d, sizeof (unichar));
c = windres_get_16 (wrbfd, d, 2);
}
while (c != 0);
2007-05-23 16:48:29 +08:00
l = ((soff - off[0]) / sizeof (unichar));
1999-05-03 15:29:11 +08:00
2007-05-23 16:48:29 +08:00
/* there are hardly any names longer than 256 characters, but anyway. */
p = s = (unichar *) xmalloc (sizeof (unichar) * l);
1999-05-03 15:29:11 +08:00
do
{
2007-05-23 16:48:29 +08:00
read_res_data (wrbfd, off, omax, d, sizeof (unichar));
c = windres_get_16 (wrbfd, d, 2);
1999-05-03 15:29:11 +08:00
*p++ = c;
}
while (c != 0);
2007-05-23 16:48:29 +08:00
*len = l - 1;
1999-05-03 15:29:11 +08:00
return s;
}
2007-05-23 16:48:29 +08:00
static int
probe_binary (windres_bfd *wrbfd, rc_uint_type omax)
1999-05-03 15:29:11 +08:00
{
2007-05-23 16:48:29 +08:00
rc_uint_type off;
res_hdr reshdr;
off = 0;
read_res_data_hdr (wrbfd, &off, omax, &reshdr);
if (reshdr.data_size != 0)
return 1;
if ((reshdr.header_size != 0x20 && ! target_is_bigendian)
|| (reshdr.header_size != 0x20000000 && target_is_bigendian))
return 1;
/* Subtract size of HeaderSize. DataSize has to be zero. */
off += 0x20 - BIN_RES_HDR_SIZE;
if ((off + BIN_RES_HDR_SIZE) >= omax)
return 1;
read_res_data_hdr (wrbfd, &off, omax, &reshdr);
/* off is advanced by BIN_RES_HDR_SIZE in read_res_data_hdr()
which is part of reshdr.header_size. We shouldn't take it
into account twice. */
if ((off - BIN_RES_HDR_SIZE + reshdr.data_size + reshdr.header_size) > omax)
return 0;
2007-05-23 16:48:29 +08:00
return 1;
1999-05-03 15:29:11 +08:00
}
/* Check if file is a win32 binary resource file, if so
skip past the null resource. Returns 0 if successful, -1 on
error.
*/
static void
2007-05-23 16:48:29 +08:00
skip_null_resource (windres_bfd *wrbfd, rc_uint_type *off, rc_uint_type omax)
1999-05-03 15:29:11 +08:00
{
2007-05-23 16:48:29 +08:00
res_hdr reshdr;
read_res_data_hdr (wrbfd, off, omax, &reshdr);
if (reshdr.data_size != 0)
goto skip_err;
if ((reshdr.header_size != 0x20 && ! target_is_bigendian)
|| (reshdr.header_size != 0x20000000 && target_is_bigendian))
1999-05-03 15:29:11 +08:00
goto skip_err;
2007-05-23 16:48:29 +08:00
/* Subtract size of HeaderSize. DataSize has to be zero. */
off[0] += 0x20 - BIN_RES_HDR_SIZE;
if (off[0] >= omax)
1999-05-03 15:29:11 +08:00
goto skip_err;
return;
Indent labels Labels don't go in the first column according to standard emacs C indent rules, and I got annoyed enough at seeing diff -p show a label rather than the function name to fix this. bfd/ * aoutx.h: Indent labels correctly. Format error strings. * archive.c: Likewise. * archive64.c: Likewise. * coff-arm.c: Likewise. * coff-rs6000.c: Likewise. * coff-stgo32.c: Likewise. * cpu-arm.c: Likewise. * dwarf2.c: Likewise. * elf-ifunc.c: Likewise. * elf-properties.c: Likewise. * elf-s390-common.c: Likewise. * elf-strtab.c: Likewise. * elf.c: Likewise. * elf32-arm.c: Likewise. * elf32-bfin.c: Likewise. * elf32-cr16.c: Likewise. * elf32-csky.c: Likewise. * elf32-i386.c: Likewise. * elf32-m68k.c: Likewise. * elf32-msp430.c: Likewise. * elf32-nds32.c: Likewise. * elf32-nios2.c: Likewise. * elf32-pru.c: Likewise. * elf32-xtensa.c: Likewise. * elf64-ia64-vms.c: Likewise. * elf64-x86-64.c: Likewise. * elfcode.h: Likewise. * elfcore.h: Likewise. * elflink.c: Likewise. * elfnn-aarch64.c: Likewise. * elfnn-ia64.c: Likewise. * elfnn-riscv.c: Likewise. * elfxx-mips.c: Likewise. * elfxx-sparc.c: Likewise. * elfxx-x86.c: Likewise. * i386lynx.c: Likewise. * merge.c: Likewise. * pdp11.c: Likewise. * plugin.c: Likewise. * reloc.c: Likewise. binutils/ * elfedit.c: Indent labels correctly. * readelf.c: Likewise. * resres.c: Likewise. gas/ * config/obj-elf.c: Indent labels correctly. * config/obj-macho.c: Likewise. * config/tc-aarch64.c: Likewise. * config/tc-alpha.c: Likewise. * config/tc-arm.c: Likewise. * config/tc-cr16.c: Likewise. * config/tc-crx.c: Likewise. * config/tc-frv.c: Likewise. * config/tc-i386-intel.c: Likewise. * config/tc-i386.c: Likewise. * config/tc-ia64.c: Likewise. * config/tc-mn10200.c: Likewise. * config/tc-mn10300.c: Likewise. * config/tc-nds32.c: Likewise. * config/tc-riscv.c: Likewise. * config/tc-s12z.c: Likewise. * config/tc-xtensa.c: Likewise. * config/tc-z80.c: Likewise. * read.c: Likewise. * symbols.c: Likewise. * write.c: Likewise. ld/ * emultempl/cskyelf.em: Indent labels correctly. * ldfile.c: Likewise. * ldlang.c: Likewise. * plugin.c: Likewise. opcodes/ * aarch64-asm.c: Indent labels correctly. * aarch64-dis.c: Likewise. * aarch64-gen.c: Likewise. * aarch64-opc.c: Likewise. * alpha-dis.c: Likewise. * i386-dis.c: Likewise. * nds32-asm.c: Likewise. * nfp-dis.c: Likewise. * visium-dis.c: Likewise.
2020-02-25 13:04:46 +08:00
skip_err:
1999-05-03 15:29:11 +08:00
fprintf (stderr, "%s: %s: Not a valid WIN32 resource file\n", program_name,
filename);
xexit (1);
}
/* Add a resource to resource directory */
2007-05-23 16:48:29 +08:00
static void
res_add_resource (rc_res_resource *r, const rc_res_id *type, const rc_res_id *id,
rc_uint_type language, int dupok)
1999-05-03 15:29:11 +08:00
{
2007-05-23 16:48:29 +08:00
rc_res_id a[3];
1999-05-03 15:29:11 +08:00
a[0] = *type;
a[1] = *id;
a[2].named = 0;
a[2].u.id = language;
res_append_resource (&resources, r, 3, a, dupok);
}
/* Append a resource to resource directory.
This is just copied from define_resource
and modified to add an existing resource.
*/
2007-05-23 16:48:29 +08:00
static void
res_append_resource (rc_res_directory **res_dirs, rc_res_resource *resource,
2007-05-23 16:48:29 +08:00
int cids, const rc_res_id *ids, int dupok)
1999-05-03 15:29:11 +08:00
{
2007-05-23 16:48:29 +08:00
rc_res_entry *re = NULL;
1999-05-03 15:29:11 +08:00
int i;
assert (cids > 0);
for (i = 0; i < cids; i++)
{
2007-05-23 16:48:29 +08:00
rc_res_entry **pp;
1999-05-03 15:29:11 +08:00
if (*res_dirs == NULL)
1999-05-03 15:29:11 +08:00
{
*res_dirs = ((rc_res_directory *)
2007-05-23 16:48:29 +08:00
res_alloc (sizeof (rc_res_directory)));
(*res_dirs)->characteristics = 0;
/* Using a real timestamp only serves to create non-deterministic
results. Use zero instead. */
(*res_dirs)->time = 0;
(*res_dirs)->major = 0;
(*res_dirs)->minor = 0;
(*res_dirs)->entries = NULL;
1999-05-03 15:29:11 +08:00
}
for (pp = &(*res_dirs)->entries; *pp != NULL; pp = &(*pp)->next)
1999-05-03 15:29:11 +08:00
if (res_id_cmp ((*pp)->id, ids[i]) == 0)
break;
if (*pp != NULL)
re = *pp;
else
{
2007-05-23 16:48:29 +08:00
re = (rc_res_entry *) res_alloc (sizeof (rc_res_entry));
1999-05-03 15:29:11 +08:00
re->next = NULL;
re->id = ids[i];
if ((i + 1) < cids)
{
re->subdir = 1;
re->u.dir = NULL;
}
else
{
re->subdir = 0;
re->u.res = NULL;
}
*pp = re;
}
if ((i + 1) < cids)
{
2007-05-23 16:48:29 +08:00
if (! re->subdir)
1999-05-03 15:29:11 +08:00
{
fprintf (stderr, "%s: ", program_name);
res_ids_print (stderr, i, ids);
fprintf (stderr, ": expected to be a directory\n");
xexit (1);
}
res_dirs = &re->u.dir;
1999-05-03 15:29:11 +08:00
}
}
if (re->subdir)
{
fprintf (stderr, "%s: ", program_name);
res_ids_print (stderr, cids, ids);
fprintf (stderr, ": expected to be a leaf\n");
xexit (1);
}
if (re->u.res != NULL)
{
if (dupok)
return;
fprintf (stderr, "%s: warning: ", program_name);
res_ids_print (stderr, cids, ids);
fprintf (stderr, ": duplicate value\n");
}
re->u.res = resource;
}