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