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