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