Commit | Line | Data |
---|---|---|
662cc41e | 1 | /* rescoff.c -- read and write resources in Windows COFF files. |
e4486bdf ILT |
2 | Copyright 1997 Free Software Foundation, Inc. |
3 | Written by Ian Lance Taylor, Cygnus Support. | |
4 | ||
5 | This file is part of GNU Binutils. | |
6 | ||
7 | This program is free software; you can redistribute it and/or modify | |
8 | it under the terms of the GNU General Public License as published by | |
9 | the Free Software Foundation; either version 2 of the License, or | |
10 | (at your option) any later version. | |
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 | ||
662cc41e ILT |
30 | #include <assert.h> |
31 | ||
e4486bdf ILT |
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 | ||
662cc41e ILT |
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)) | |
e4486bdf ILT |
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 | |
662cc41e ILT |
107 | PARAMS ((const bfd_byte *, const struct coff_file_info *, |
108 | const struct res_id *, int)); | |
e4486bdf | 109 | static struct res_resource *read_coff_data_entry |
662cc41e ILT |
110 | PARAMS ((const bfd_byte *, const struct coff_file_info *, |
111 | const struct res_id *)); | |
e4486bdf ILT |
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 | ||
72574626 ILT |
127 | if (filename == NULL) |
128 | fatal (_("filename required for COFF input")); | |
129 | ||
e4486bdf ILT |
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 | { | |
72574626 | 145 | fprintf (stderr, _("%s: %s: no resource section\n"), program_name, |
e4486bdf ILT |
146 | filename); |
147 | xexit (1); | |
148 | } | |
149 | ||
150 | size = bfd_section_size (abfd, sec); | |
662cc41e | 151 | data = (bfd_byte *) res_alloc (size); |
e4486bdf ILT |
152 | |
153 | if (! bfd_get_section_contents (abfd, sec, data, 0, size)) | |
72574626 | 154 | bfd_fatal (_("can't read resource section")); |
e4486bdf ILT |
155 | |
156 | finfo.filename = filename; | |
157 | finfo.data = data; | |
158 | finfo.data_end = data + size; | |
159 | finfo.secaddr = (bfd_get_section_vma (abfd, sec) | |
160 | - pe_data (abfd)->pe_opthdr.ImageBase); | |
161 | finfo.big_endian = bfd_big_endian (abfd); | |
162 | ||
163 | bfd_close (abfd); | |
164 | ||
165 | /* Now just read in the top level resource directory. Note that we | |
166 | don't free data, since we create resource entries that point into | |
167 | it. If we ever want to free up the resource information we read, | |
168 | this will have to be cleaned up. */ | |
169 | ||
662cc41e | 170 | return read_coff_res_dir (data, &finfo, (const struct res_id *) NULL, 0); |
e4486bdf ILT |
171 | } |
172 | ||
173 | /* Give an error if we are out of bounds. */ | |
174 | ||
175 | static void | |
176 | overrun (finfo, msg) | |
177 | const struct coff_file_info *finfo; | |
178 | const char *msg; | |
179 | { | |
72574626 | 180 | fatal (_("%s: %s: address out of bounds"), finfo->filename, msg); |
e4486bdf ILT |
181 | } |
182 | ||
183 | /* Read a resource directory. */ | |
184 | ||
185 | static struct res_directory * | |
662cc41e | 186 | read_coff_res_dir (data, finfo, type, level) |
e4486bdf ILT |
187 | const bfd_byte *data; |
188 | const struct coff_file_info *finfo; | |
662cc41e ILT |
189 | const struct res_id *type; |
190 | int level; | |
e4486bdf ILT |
191 | { |
192 | const struct extern_res_directory *erd; | |
193 | struct res_directory *rd; | |
194 | int name_count, id_count, i; | |
195 | struct res_entry **pp; | |
196 | const struct extern_res_entry *ere; | |
197 | ||
72574626 ILT |
198 | if ((size_t) (finfo->data_end - data) < sizeof (struct extern_res_directory)) |
199 | overrun (finfo, _("directory")); | |
e4486bdf ILT |
200 | |
201 | erd = (const struct extern_res_directory *) data; | |
202 | ||
662cc41e ILT |
203 | rd = (struct res_directory *) res_alloc (sizeof *rd); |
204 | rd->characteristics = getfi_32 (finfo, erd->characteristics); | |
205 | rd->time = getfi_32 (finfo, erd->time); | |
206 | rd->major = getfi_16 (finfo, erd->major); | |
207 | rd->minor = getfi_16 (finfo, erd->minor); | |
e4486bdf ILT |
208 | rd->entries = NULL; |
209 | ||
662cc41e ILT |
210 | name_count = getfi_16 (finfo, erd->name_count); |
211 | id_count = getfi_16 (finfo, erd->id_count); | |
e4486bdf ILT |
212 | |
213 | pp = &rd->entries; | |
214 | ||
215 | /* The resource directory entries immediately follow the directory | |
216 | table. */ | |
217 | ere = (const struct extern_res_entry *) (erd + 1); | |
218 | ||
219 | for (i = 0; i < name_count; i++, ere++) | |
220 | { | |
221 | unsigned long name, rva; | |
222 | struct res_entry *re; | |
223 | const bfd_byte *ers; | |
224 | int length, j; | |
225 | ||
226 | if ((const bfd_byte *) ere >= finfo->data_end) | |
72574626 | 227 | overrun (finfo, _("named directory entry")); |
e4486bdf | 228 | |
662cc41e ILT |
229 | name = getfi_32 (finfo, ere->name); |
230 | rva = getfi_32 (finfo, ere->rva); | |
e4486bdf ILT |
231 | |
232 | /* For some reason the high bit in NAME is set. */ | |
233 | name &=~ 0x80000000; | |
234 | ||
72574626 ILT |
235 | if (name > (size_t) (finfo->data_end - finfo->data)) |
236 | overrun (finfo, _("directory entry name")); | |
e4486bdf ILT |
237 | |
238 | ers = finfo->data + name; | |
239 | ||
662cc41e | 240 | re = (struct res_entry *) res_alloc (sizeof *re); |
e4486bdf ILT |
241 | re->next = NULL; |
242 | re->id.named = 1; | |
662cc41e | 243 | length = getfi_16 (finfo, ers); |
e4486bdf | 244 | re->id.u.n.length = length; |
662cc41e | 245 | re->id.u.n.name = (unichar *) res_alloc (length * sizeof (unichar)); |
e4486bdf | 246 | for (j = 0; j < length; j++) |
662cc41e ILT |
247 | re->id.u.n.name[j] = getfi_16 (finfo, ers + j * 2 + 2); |
248 | ||
249 | if (level == 0) | |
250 | type = &re->id; | |
e4486bdf ILT |
251 | |
252 | if ((rva & 0x80000000) != 0) | |
253 | { | |
254 | rva &=~ 0x80000000; | |
72574626 ILT |
255 | if (rva >= (size_t) (finfo->data_end - finfo->data)) |
256 | overrun (finfo, _("named subdirectory")); | |
e4486bdf | 257 | re->subdir = 1; |
662cc41e ILT |
258 | re->u.dir = read_coff_res_dir (finfo->data + rva, finfo, type, |
259 | level + 1); | |
e4486bdf ILT |
260 | } |
261 | else | |
262 | { | |
72574626 ILT |
263 | if (rva >= (size_t) (finfo->data_end - finfo->data)) |
264 | overrun (finfo, _("named resource")); | |
e4486bdf | 265 | re->subdir = 0; |
662cc41e | 266 | re->u.res = read_coff_data_entry (finfo->data + rva, finfo, type); |
e4486bdf ILT |
267 | } |
268 | ||
269 | *pp = re; | |
270 | pp = &re->next; | |
271 | } | |
272 | ||
273 | for (i = 0; i < id_count; i++, ere++) | |
274 | { | |
275 | unsigned long name, rva; | |
276 | struct res_entry *re; | |
277 | ||
278 | if ((const bfd_byte *) ere >= finfo->data_end) | |
72574626 | 279 | overrun (finfo, _("ID directory entry")); |
e4486bdf | 280 | |
662cc41e ILT |
281 | name = getfi_32 (finfo, ere->name); |
282 | rva = getfi_32 (finfo, ere->rva); | |
e4486bdf | 283 | |
662cc41e | 284 | re = (struct res_entry *) res_alloc (sizeof *re); |
e4486bdf ILT |
285 | re->next = NULL; |
286 | re->id.named = 0; | |
287 | re->id.u.id = name; | |
288 | ||
662cc41e ILT |
289 | if (level == 0) |
290 | type = &re->id; | |
291 | ||
e4486bdf ILT |
292 | if ((rva & 0x80000000) != 0) |
293 | { | |
294 | rva &=~ 0x80000000; | |
72574626 ILT |
295 | if (rva >= (size_t) (finfo->data_end - finfo->data)) |
296 | overrun (finfo, _("ID subdirectory")); | |
e4486bdf | 297 | re->subdir = 1; |
662cc41e ILT |
298 | re->u.dir = read_coff_res_dir (finfo->data + rva, finfo, type, |
299 | level + 1); | |
e4486bdf ILT |
300 | } |
301 | else | |
302 | { | |
72574626 ILT |
303 | if (rva >= (size_t) (finfo->data_end - finfo->data)) |
304 | overrun (finfo, _("ID resource")); | |
e4486bdf | 305 | re->subdir = 0; |
662cc41e | 306 | re->u.res = read_coff_data_entry (finfo->data + rva, finfo, type); |
e4486bdf ILT |
307 | } |
308 | ||
309 | *pp = re; | |
310 | pp = &re->next; | |
311 | } | |
312 | ||
313 | return rd; | |
314 | } | |
315 | ||
316 | /* Read a resource data entry. */ | |
317 | ||
318 | static struct res_resource * | |
662cc41e | 319 | read_coff_data_entry (data, finfo, type) |
e4486bdf ILT |
320 | const bfd_byte *data; |
321 | const struct coff_file_info *finfo; | |
662cc41e | 322 | const struct res_id *type; |
e4486bdf ILT |
323 | { |
324 | const struct extern_res_data *erd; | |
325 | struct res_resource *r; | |
326 | unsigned long size, rva; | |
327 | const bfd_byte *resdata; | |
328 | ||
662cc41e | 329 | if (type == NULL) |
72574626 | 330 | fatal (_("resource type unknown")); |
662cc41e | 331 | |
72574626 ILT |
332 | if ((size_t) (finfo->data_end - data) < sizeof (struct extern_res_data)) |
333 | overrun (finfo, _("data entry")); | |
e4486bdf ILT |
334 | |
335 | erd = (const struct extern_res_data *) data; | |
336 | ||
662cc41e ILT |
337 | size = getfi_32 (finfo, erd->size); |
338 | rva = getfi_32 (finfo, erd->rva); | |
e4486bdf | 339 | if (rva < finfo->secaddr |
72574626 ILT |
340 | || rva - finfo->secaddr >= (size_t) (finfo->data_end - finfo->data)) |
341 | overrun (finfo, _("resource data")); | |
e4486bdf ILT |
342 | |
343 | resdata = finfo->data + (rva - finfo->secaddr); | |
344 | ||
72574626 ILT |
345 | if (size > (size_t) (finfo->data_end - resdata)) |
346 | overrun (finfo, _("resource data size")); | |
662cc41e ILT |
347 | |
348 | r = bin_to_res (*type, resdata, size, finfo->big_endian); | |
349 | ||
350 | memset (&r->res_info, 0, sizeof (struct res_res_info)); | |
351 | r->coff_info.codepage = getfi_32 (finfo, erd->codepage); | |
352 | r->coff_info.reserved = getfi_32 (finfo, erd->reserved); | |
e4486bdf ILT |
353 | |
354 | return r; | |
355 | } | |
662cc41e ILT |
356 | \f |
357 | /* This structure is used to build a list of bindata structures. */ | |
358 | ||
359 | struct bindata_build | |
360 | { | |
361 | /* The data. */ | |
362 | struct bindata *d; | |
363 | /* The last structure we have added to the list. */ | |
364 | struct bindata *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 | bfd *abfd; | |
377 | /* Non-zero if the file is big endian. */ | |
378 | int big_endian; | |
379 | /* Pointer to section symbol used to build RVA relocs. */ | |
380 | asymbol **sympp; | |
381 | ||
382 | /* These fields are computed initially, and then not changed. */ | |
383 | /* Length of directory tables and entries. */ | |
384 | unsigned long dirsize; | |
385 | /* Length of directory entry strings. */ | |
386 | unsigned long dirstrsize; | |
387 | /* Length of resource data entries. */ | |
388 | unsigned long dataentsize; | |
389 | ||
390 | /* These fields are updated as we add data. */ | |
391 | /* Directory tables and entries. */ | |
392 | struct bindata_build dirs; | |
393 | /* Directory entry strings. */ | |
394 | struct bindata_build dirstrs; | |
395 | /* Resource data entries. */ | |
396 | struct bindata_build dataents; | |
397 | /* Actual resource data. */ | |
398 | struct bindata_build resources; | |
399 | /* Relocations. */ | |
400 | arelent **relocs; | |
401 | /* Number of relocations. */ | |
402 | unsigned int reloc_count; | |
403 | }; | |
404 | ||
405 | /* Macros to swap out values. */ | |
406 | ||
407 | #define putcwi_16(cwi, v, s) \ | |
408 | ((cwi->big_endian) ? bfd_putb16 ((v), (s)) : bfd_putl16 ((v), (s))) | |
409 | #define putcwi_32(cwi, v, s) \ | |
410 | ((cwi->big_endian) ? bfd_putb32 ((v), (s)) : bfd_putl32 ((v), (s))) | |
411 | ||
412 | static void coff_bin_sizes | |
413 | PARAMS ((const struct res_directory *, struct coff_write_info *)); | |
414 | static unsigned char *coff_alloc PARAMS ((struct bindata_build *, size_t)); | |
415 | static void coff_to_bin | |
416 | PARAMS ((const struct res_directory *, struct coff_write_info *)); | |
417 | static void coff_res_to_bin | |
418 | PARAMS ((const struct res_resource *, struct coff_write_info *)); | |
419 | ||
420 | /* Write resources to a COFF file. RESOURCES should already be | |
421 | sorted. | |
422 | ||
423 | Right now we always create a new file. Someday we should also | |
424 | offer the ability to merge resources into an existing file. This | |
425 | would require doing the basic work of objcopy, just modifying or | |
426 | adding the .rsrc section. */ | |
427 | ||
662cc41e ILT |
428 | void |
429 | write_coff_file (filename, target, resources) | |
430 | const char *filename; | |
431 | const char *target; | |
432 | const struct res_directory *resources; | |
433 | { | |
434 | bfd *abfd; | |
435 | asection *sec; | |
436 | struct coff_write_info cwi; | |
437 | struct bindata *d; | |
438 | unsigned long length, offset; | |
439 | ||
72574626 ILT |
440 | if (filename == NULL) |
441 | fatal (_("filename required for COFF output")); | |
442 | ||
662cc41e ILT |
443 | abfd = bfd_openw (filename, target); |
444 | if (abfd == NULL) | |
445 | bfd_fatal (filename); | |
446 | ||
447 | if (! bfd_set_format (abfd, bfd_object)) | |
448 | bfd_fatal ("bfd_set_format"); | |
449 | ||
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"); | |
453 | ||
454 | if (! bfd_set_file_flags (abfd, HAS_SYMS | HAS_RELOC)) | |
455 | bfd_fatal ("bfd_set_file_flags"); | |
456 | ||
457 | sec = bfd_make_section (abfd, ".rsrc"); | |
458 | if (sec == NULL) | |
459 | bfd_fatal ("bfd_make_section"); | |
460 | ||
461 | if (! bfd_set_section_flags (abfd, sec, | |
462 | (SEC_HAS_CONTENTS | SEC_ALLOC | |
463 | | SEC_LOAD | SEC_DATA))) | |
464 | bfd_fatal ("bfd_set_section_flags"); | |
465 | ||
466 | if (! bfd_set_symtab (abfd, sec->symbol_ptr_ptr, 1)) | |
467 | bfd_fatal ("bfd_set_symtab"); | |
468 | ||
469 | /* Requiring this is probably a bug in BFD. */ | |
470 | sec->output_section = sec; | |
471 | ||
472 | /* The order of data in the .rsrc section is | |
473 | resource directory tables and entries | |
474 | resource directory strings | |
475 | resource data entries | |
476 | actual resource data | |
477 | ||
478 | We build these different types of data in different lists. */ | |
479 | ||
480 | cwi.abfd = abfd; | |
481 | cwi.big_endian = bfd_big_endian (abfd); | |
482 | cwi.sympp = sec->symbol_ptr_ptr; | |
483 | cwi.dirsize = 0; | |
484 | cwi.dirstrsize = 0; | |
485 | cwi.dataentsize = 0; | |
486 | cwi.dirs.d = NULL; | |
487 | cwi.dirs.last = NULL; | |
488 | cwi.dirs.length = 0; | |
489 | cwi.dirstrs.d = NULL; | |
490 | cwi.dirstrs.last = NULL; | |
491 | cwi.dirstrs.length = 0; | |
492 | cwi.dataents.d = NULL; | |
493 | cwi.dataents.last = NULL; | |
494 | cwi.dataents.length = 0; | |
495 | cwi.resources.d = NULL; | |
496 | cwi.resources.last = NULL; | |
497 | cwi.resources.length = 0; | |
498 | cwi.relocs = NULL; | |
499 | cwi.reloc_count = 0; | |
500 | ||
501 | /* Work out the sizes of the resource directory entries, so that we | |
502 | know the various offsets we will need. */ | |
503 | coff_bin_sizes (resources, &cwi); | |
504 | ||
505 | /* Force the directory strings to be 32 bit aligned. Every other | |
506 | structure is 32 bit aligned anyhow. */ | |
507 | cwi.dirstrsize = (cwi.dirstrsize + 3) &~ 3; | |
508 | ||
509 | /* Actually convert the resources to binary. */ | |
510 | coff_to_bin (resources, &cwi); | |
511 | ||
512 | /* Add another 2 bytes to the directory strings if needed for | |
513 | alignment. */ | |
514 | if ((cwi.dirstrs.length & 3) != 0) | |
515 | { | |
516 | unsigned char *ex; | |
517 | ||
518 | ex = coff_alloc (&cwi.dirstrs, 2); | |
519 | ex[0] = 0; | |
520 | ex[1] = 0; | |
521 | } | |
522 | ||
523 | /* Make sure that the data we built came out to the same size as we | |
524 | calculated initially. */ | |
525 | assert (cwi.dirs.length == cwi.dirsize); | |
526 | assert (cwi.dirstrs.length == cwi.dirstrsize); | |
527 | assert (cwi.dataents.length == cwi.dataentsize); | |
528 | ||
529 | length = (cwi.dirsize | |
530 | + cwi.dirstrsize | |
531 | + cwi.dataentsize | |
532 | + cwi.resources.length); | |
533 | ||
534 | if (! bfd_set_section_size (abfd, sec, length)) | |
535 | bfd_fatal ("bfd_set_section_size"); | |
536 | ||
537 | bfd_set_reloc (abfd, sec, cwi.relocs, cwi.reloc_count); | |
538 | ||
662cc41e ILT |
539 | offset = 0; |
540 | for (d = cwi.dirs.d; d != NULL; d = d->next) | |
541 | { | |
542 | if (! bfd_set_section_contents (abfd, sec, d->data, offset, d->length)) | |
543 | bfd_fatal ("bfd_set_section_contents"); | |
544 | offset += d->length; | |
545 | } | |
546 | for (d = cwi.dirstrs.d; d != NULL; d = d->next) | |
547 | { | |
548 | if (! bfd_set_section_contents (abfd, sec, d->data, offset, d->length)) | |
549 | bfd_fatal ("bfd_set_section_contents"); | |
550 | offset += d->length; | |
551 | } | |
552 | for (d = cwi.dataents.d; d != NULL; d = d->next) | |
553 | { | |
554 | if (! bfd_set_section_contents (abfd, sec, d->data, offset, d->length)) | |
555 | bfd_fatal ("bfd_set_section_contents"); | |
556 | offset += d->length; | |
557 | } | |
558 | for (d = cwi.resources.d; d != NULL; d = d->next) | |
559 | { | |
560 | if (! bfd_set_section_contents (abfd, sec, d->data, offset, d->length)) | |
561 | bfd_fatal ("bfd_set_section_contents"); | |
562 | offset += d->length; | |
563 | } | |
564 | ||
565 | assert (offset == length); | |
566 | ||
567 | if (! bfd_close (abfd)) | |
568 | bfd_fatal ("bfd_close"); | |
d5a7bb53 ILT |
569 | |
570 | /* We allocated the relocs array using malloc. */ | |
571 | free (cwi.relocs); | |
662cc41e ILT |
572 | } |
573 | ||
574 | /* Work out the sizes of the various fixed size resource directory | |
575 | entries. This updates fields in CWI. */ | |
576 | ||
577 | static void | |
578 | coff_bin_sizes (resdir, cwi) | |
579 | const struct res_directory *resdir; | |
580 | struct coff_write_info *cwi; | |
581 | { | |
582 | const struct res_entry *re; | |
583 | ||
584 | cwi->dirsize += sizeof (struct extern_res_directory); | |
585 | ||
586 | for (re = resdir->entries; re != NULL; re = re->next) | |
587 | { | |
588 | cwi->dirsize += sizeof (struct extern_res_entry); | |
589 | ||
590 | if (re->id.named) | |
591 | cwi->dirstrsize += re->id.u.n.length * 2 + 2; | |
592 | ||
593 | if (re->subdir) | |
594 | coff_bin_sizes (re->u.dir, cwi); | |
595 | else | |
596 | cwi->dataentsize += sizeof (struct extern_res_data); | |
597 | } | |
598 | } | |
599 | ||
600 | /* Allocate data for a particular list. */ | |
601 | ||
602 | static unsigned char * | |
603 | coff_alloc (bb, size) | |
604 | struct bindata_build *bb; | |
605 | size_t size; | |
606 | { | |
607 | struct bindata *d; | |
608 | ||
609 | d = (struct bindata *) reswr_alloc (sizeof *d); | |
610 | ||
611 | d->next = NULL; | |
612 | d->data = (unsigned char *) reswr_alloc (size); | |
613 | d->length = size; | |
614 | ||
615 | if (bb->d == NULL) | |
616 | bb->d = d; | |
617 | else | |
618 | bb->last->next = d; | |
619 | bb->last = d; | |
620 | bb->length += size; | |
621 | ||
622 | return d->data; | |
623 | } | |
624 | ||
625 | /* Convert the resource directory RESDIR to binary. */ | |
626 | ||
627 | static void | |
628 | coff_to_bin (resdir, cwi) | |
629 | const struct res_directory *resdir; | |
630 | struct coff_write_info *cwi; | |
631 | { | |
632 | struct extern_res_directory *erd; | |
633 | int ci, cn; | |
634 | const struct res_entry *e; | |
635 | struct extern_res_entry *ere; | |
636 | ||
637 | /* Write out the directory table. */ | |
638 | ||
639 | erd = ((struct extern_res_directory *) | |
640 | coff_alloc (&cwi->dirs, sizeof (*erd))); | |
641 | ||
642 | putcwi_32 (cwi, resdir->characteristics, erd->characteristics); | |
643 | putcwi_32 (cwi, resdir->time, erd->time); | |
644 | putcwi_16 (cwi, resdir->major, erd->major); | |
645 | putcwi_16 (cwi, resdir->minor, erd->minor); | |
646 | ||
647 | ci = 0; | |
648 | cn = 0; | |
649 | for (e = resdir->entries; e != NULL; e = e->next) | |
650 | { | |
651 | if (e->id.named) | |
652 | ++cn; | |
653 | else | |
654 | ++ci; | |
655 | } | |
656 | ||
657 | putcwi_16 (cwi, cn, erd->name_count); | |
658 | putcwi_16 (cwi, ci, erd->id_count); | |
659 | ||
660 | /* Write out the data entries. Note that we allocate space for all | |
661 | the entries before writing them out. That permits a recursive | |
662 | call to work correctly when writing out subdirectories. */ | |
663 | ||
664 | ere = ((struct extern_res_entry *) | |
665 | coff_alloc (&cwi->dirs, (ci + cn) * sizeof (*ere))); | |
666 | for (e = resdir->entries; e != NULL; e = e->next, ere++) | |
667 | { | |
668 | if (! e->id.named) | |
669 | putcwi_32 (cwi, e->id.u.id, ere->name); | |
670 | else | |
671 | { | |
672 | unsigned char *str; | |
673 | int i; | |
674 | ||
675 | /* For some reason existing files seem to have the high bit | |
676 | set on the address of the name, although that is not | |
677 | documented. */ | |
678 | putcwi_32 (cwi, | |
679 | 0x80000000 | (cwi->dirsize + cwi->dirstrs.length), | |
680 | ere->name); | |
681 | ||
682 | str = coff_alloc (&cwi->dirstrs, e->id.u.n.length * 2 + 2); | |
683 | putcwi_16 (cwi, e->id.u.n.length, str); | |
684 | for (i = 0; i < e->id.u.n.length; i++) | |
685 | putcwi_16 (cwi, e->id.u.n.name[i], str + i * 2 + 2); | |
686 | } | |
687 | ||
688 | if (e->subdir) | |
689 | { | |
690 | putcwi_32 (cwi, 0x80000000 | cwi->dirs.length, ere->rva); | |
691 | coff_to_bin (e->u.dir, cwi); | |
692 | } | |
693 | else | |
694 | { | |
695 | putcwi_32 (cwi, | |
696 | cwi->dirsize + cwi->dirstrsize + cwi->dataents.length, | |
697 | ere->rva); | |
698 | ||
699 | coff_res_to_bin (e->u.res, cwi); | |
700 | } | |
701 | } | |
702 | } | |
703 | ||
704 | /* Convert the resource RES to binary. */ | |
705 | ||
706 | static void | |
707 | coff_res_to_bin (res, cwi) | |
708 | const struct res_resource *res; | |
709 | struct coff_write_info *cwi; | |
710 | { | |
711 | arelent *r; | |
712 | struct extern_res_data *erd; | |
713 | struct bindata *d; | |
714 | unsigned long length; | |
715 | ||
716 | /* For some reason, although every other address is a section | |
717 | offset, the address of the resource data itself is an RVA. That | |
718 | means that we need to generate a relocation for it. We allocate | |
719 | the relocs array using malloc so that we can use realloc. FIXME: | |
720 | This relocation handling is correct for the i386, but probably | |
721 | not for any other target. */ | |
722 | ||
723 | r = (arelent *) reswr_alloc (sizeof (arelent)); | |
724 | r->sym_ptr_ptr = cwi->sympp; | |
725 | r->address = cwi->dirsize + cwi->dirstrsize + cwi->dataents.length; | |
726 | r->addend = 0; | |
727 | r->howto = bfd_reloc_type_lookup (cwi->abfd, BFD_RELOC_RVA); | |
728 | if (r->howto == NULL) | |
72574626 | 729 | bfd_fatal (_("can't get BFD_RELOC_RVA relocation type")); |
662cc41e ILT |
730 | |
731 | cwi->relocs = xrealloc (cwi->relocs, | |
732 | (cwi->reloc_count + 2) * sizeof (arelent *)); | |
733 | cwi->relocs[cwi->reloc_count] = r; | |
734 | cwi->relocs[cwi->reloc_count + 1] = NULL; | |
735 | ++cwi->reloc_count; | |
736 | ||
737 | erd = (struct extern_res_data *) coff_alloc (&cwi->dataents, sizeof (*erd)); | |
738 | ||
739 | putcwi_32 (cwi, | |
740 | (cwi->dirsize | |
741 | + cwi->dirstrsize | |
742 | + cwi->dataentsize | |
743 | + cwi->resources.length), | |
744 | erd->rva); | |
745 | putcwi_32 (cwi, res->coff_info.codepage, erd->codepage); | |
746 | putcwi_32 (cwi, res->coff_info.reserved, erd->reserved); | |
747 | ||
748 | d = res_to_bin (res, cwi->big_endian); | |
749 | ||
750 | if (cwi->resources.d == NULL) | |
751 | cwi->resources.d = d; | |
752 | else | |
753 | cwi->resources.last->next = d; | |
754 | ||
755 | length = 0; | |
756 | for (; d->next != NULL; d = d->next) | |
757 | length += d->length; | |
758 | length += d->length; | |
759 | cwi->resources.last = d; | |
760 | cwi->resources.length += length; | |
761 | ||
762 | putcwi_32 (cwi, length, erd->size); | |
763 | ||
764 | /* Force the next resource to have 32 bit alignment. */ | |
765 | ||
766 | if ((length & 3) != 0) | |
767 | { | |
768 | int add; | |
769 | unsigned char *ex; | |
770 | ||
771 | add = 4 - (length & 3); | |
772 | ||
773 | ex = coff_alloc (&cwi->resources, add); | |
774 | memset (ex, 0, add); | |
775 | } | |
776 | } |