Commit | Line | Data |
---|---|---|
252b5132 | 1 | /* rescoff.c -- read and write resources in Windows COFF files. |
4b95cf5c | 2 | Copyright (C) 1997-2014 Free Software Foundation, Inc. |
252b5132 | 3 | Written by Ian Lance Taylor, Cygnus Support. |
4a594fce | 4 | Rewritten by Kai Tietz, Onevision. |
252b5132 RH |
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 | |
32866df7 | 10 | the Free Software Foundation; either version 3 of the License, or |
252b5132 RH |
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" |
3db64b00 | 28 | #include "bucomm.h" |
4a594fce | 29 | #include "libiberty.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. */ | |
4a594fce | 52 | rc_uint_type secaddr; |
252b5132 RH |
53 | }; |
54 | ||
55 | /* A resource directory table in a COFF file. */ | |
56 | ||
4a594fce | 57 | struct __attribute__ ((__packed__)) extern_res_directory |
252b5132 RH |
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 | ||
252b5132 RH |
98 | /* Local functions. */ |
99 | ||
2da42df6 | 100 | static void overrun (const struct coff_file_info *, const char *); |
4a594fce NC |
101 | static rc_res_directory *read_coff_res_dir (windres_bfd *, const bfd_byte *, |
102 | const struct coff_file_info *, | |
103 | const rc_res_id *, int); | |
104 | static rc_res_resource *read_coff_data_entry (windres_bfd *, const bfd_byte *, | |
105 | const struct coff_file_info *, | |
106 | const rc_res_id *); | |
252b5132 RH |
107 | \f |
108 | /* Read the resources in a COFF file. */ | |
109 | ||
4a594fce | 110 | rc_res_directory * |
2da42df6 | 111 | read_coff_rsrc (const char *filename, const char *target) |
252b5132 | 112 | { |
4a594fce | 113 | rc_res_directory *ret; |
252b5132 | 114 | bfd *abfd; |
4a594fce | 115 | windres_bfd wrbfd; |
252b5132 RH |
116 | char **matching; |
117 | asection *sec; | |
118 | bfd_size_type size; | |
119 | bfd_byte *data; | |
57402f1e | 120 | struct coff_file_info flaginfo; |
252b5132 RH |
121 | |
122 | if (filename == NULL) | |
123 | fatal (_("filename required for COFF input")); | |
124 | ||
125 | abfd = bfd_openr (filename, target); | |
126 | if (abfd == NULL) | |
127 | bfd_fatal (filename); | |
128 | ||
129 | if (! bfd_check_format_matches (abfd, bfd_object, &matching)) | |
130 | { | |
131 | bfd_nonfatal (bfd_get_filename (abfd)); | |
132 | if (bfd_get_error () == bfd_error_file_ambiguously_recognized) | |
133 | list_matching_formats (matching); | |
134 | xexit (1); | |
135 | } | |
136 | ||
137 | sec = bfd_get_section_by_name (abfd, ".rsrc"); | |
138 | if (sec == NULL) | |
139 | { | |
37cc8ec1 | 140 | fatal (_("%s: no resource section"), filename); |
252b5132 RH |
141 | } |
142 | ||
4a594fce | 143 | set_windres_bfd (&wrbfd, abfd, sec, WR_KIND_BFD); |
252b5132 RH |
144 | size = bfd_section_size (abfd, sec); |
145 | data = (bfd_byte *) res_alloc (size); | |
146 | ||
4a594fce | 147 | get_windres_bfd_content (&wrbfd, data, 0, size); |
252b5132 | 148 | |
57402f1e NC |
149 | flaginfo.filename = filename; |
150 | flaginfo.data = data; | |
151 | flaginfo.data_end = data + size; | |
152 | flaginfo.secaddr = (bfd_get_section_vma (abfd, sec) | |
252b5132 | 153 | - pe_data (abfd)->pe_opthdr.ImageBase); |
252b5132 RH |
154 | |
155 | /* Now just read in the top level resource directory. Note that we | |
156 | don't free data, since we create resource entries that point into | |
157 | it. If we ever want to free up the resource information we read, | |
158 | this will have to be cleaned up. */ | |
159 | ||
57402f1e | 160 | ret = read_coff_res_dir (&wrbfd, data, &flaginfo, (const rc_res_id *) NULL, 0); |
4a594fce NC |
161 | |
162 | bfd_close (abfd); | |
163 | ||
164 | return ret; | |
252b5132 RH |
165 | } |
166 | ||
167 | /* Give an error if we are out of bounds. */ | |
168 | ||
169 | static void | |
57402f1e | 170 | overrun (const struct coff_file_info *flaginfo, const char *msg) |
252b5132 | 171 | { |
57402f1e | 172 | fatal (_("%s: %s: address out of bounds"), flaginfo->filename, msg); |
252b5132 RH |
173 | } |
174 | ||
175 | /* Read a resource directory. */ | |
176 | ||
4a594fce NC |
177 | static rc_res_directory * |
178 | read_coff_res_dir (windres_bfd *wrbfd, const bfd_byte *data, | |
57402f1e | 179 | const struct coff_file_info *flaginfo, |
4a594fce | 180 | const rc_res_id *type, int level) |
252b5132 RH |
181 | { |
182 | const struct extern_res_directory *erd; | |
4a594fce | 183 | rc_res_directory *rd; |
252b5132 | 184 | int name_count, id_count, i; |
4a594fce | 185 | rc_res_entry **pp; |
252b5132 RH |
186 | const struct extern_res_entry *ere; |
187 | ||
57402f1e NC |
188 | if ((size_t) (flaginfo->data_end - data) < sizeof (struct extern_res_directory)) |
189 | overrun (flaginfo, _("directory")); | |
252b5132 RH |
190 | |
191 | erd = (const struct extern_res_directory *) data; | |
192 | ||
4a594fce NC |
193 | rd = (rc_res_directory *) res_alloc (sizeof (rc_res_directory)); |
194 | rd->characteristics = windres_get_32 (wrbfd, erd->characteristics, 4); | |
195 | rd->time = windres_get_32 (wrbfd, erd->time, 4); | |
196 | rd->major = windres_get_16 (wrbfd, erd->major, 2); | |
197 | rd->minor = windres_get_16 (wrbfd, erd->minor, 2); | |
252b5132 RH |
198 | rd->entries = NULL; |
199 | ||
4a594fce NC |
200 | name_count = windres_get_16 (wrbfd, erd->name_count, 2); |
201 | id_count = windres_get_16 (wrbfd, erd->id_count, 2); | |
252b5132 RH |
202 | |
203 | pp = &rd->entries; | |
204 | ||
205 | /* The resource directory entries immediately follow the directory | |
206 | table. */ | |
207 | ere = (const struct extern_res_entry *) (erd + 1); | |
208 | ||
209 | for (i = 0; i < name_count; i++, ere++) | |
210 | { | |
4a594fce NC |
211 | rc_uint_type name, rva; |
212 | rc_res_entry *re; | |
252b5132 RH |
213 | const bfd_byte *ers; |
214 | int length, j; | |
215 | ||
57402f1e NC |
216 | if ((const bfd_byte *) ere >= flaginfo->data_end) |
217 | overrun (flaginfo, _("named directory entry")); | |
252b5132 | 218 | |
4a594fce NC |
219 | name = windres_get_32 (wrbfd, ere->name, 4); |
220 | rva = windres_get_32 (wrbfd, ere->rva, 4); | |
252b5132 RH |
221 | |
222 | /* For some reason the high bit in NAME is set. */ | |
223 | name &=~ 0x80000000; | |
224 | ||
57402f1e NC |
225 | if (name > (rc_uint_type) (flaginfo->data_end - flaginfo->data)) |
226 | overrun (flaginfo, _("directory entry name")); | |
252b5132 | 227 | |
57402f1e | 228 | ers = flaginfo->data + name; |
252b5132 | 229 | |
4a594fce | 230 | re = (rc_res_entry *) res_alloc (sizeof *re); |
252b5132 RH |
231 | re->next = NULL; |
232 | re->id.named = 1; | |
4a594fce | 233 | length = windres_get_16 (wrbfd, ers, 2); |
252b5132 RH |
234 | re->id.u.n.length = length; |
235 | re->id.u.n.name = (unichar *) res_alloc (length * sizeof (unichar)); | |
236 | for (j = 0; j < length; j++) | |
4a594fce | 237 | re->id.u.n.name[j] = windres_get_16 (wrbfd, ers + j * 2 + 2, 2); |
252b5132 RH |
238 | |
239 | if (level == 0) | |
240 | type = &re->id; | |
241 | ||
242 | if ((rva & 0x80000000) != 0) | |
243 | { | |
244 | rva &=~ 0x80000000; | |
57402f1e NC |
245 | if (rva >= (rc_uint_type) (flaginfo->data_end - flaginfo->data)) |
246 | overrun (flaginfo, _("named subdirectory")); | |
252b5132 | 247 | re->subdir = 1; |
57402f1e | 248 | re->u.dir = read_coff_res_dir (wrbfd, flaginfo->data + rva, flaginfo, type, |
252b5132 RH |
249 | level + 1); |
250 | } | |
251 | else | |
252 | { | |
57402f1e NC |
253 | if (rva >= (rc_uint_type) (flaginfo->data_end - flaginfo->data)) |
254 | overrun (flaginfo, _("named resource")); | |
252b5132 | 255 | re->subdir = 0; |
57402f1e | 256 | re->u.res = read_coff_data_entry (wrbfd, flaginfo->data + rva, flaginfo, type); |
252b5132 RH |
257 | } |
258 | ||
259 | *pp = re; | |
260 | pp = &re->next; | |
261 | } | |
262 | ||
263 | for (i = 0; i < id_count; i++, ere++) | |
264 | { | |
265 | unsigned long name, rva; | |
4a594fce | 266 | rc_res_entry *re; |
252b5132 | 267 | |
57402f1e NC |
268 | if ((const bfd_byte *) ere >= flaginfo->data_end) |
269 | overrun (flaginfo, _("ID directory entry")); | |
252b5132 | 270 | |
4a594fce NC |
271 | name = windres_get_32 (wrbfd, ere->name, 4); |
272 | rva = windres_get_32 (wrbfd, ere->rva, 4); | |
252b5132 | 273 | |
4a594fce | 274 | re = (rc_res_entry *) res_alloc (sizeof *re); |
252b5132 RH |
275 | re->next = NULL; |
276 | re->id.named = 0; | |
277 | re->id.u.id = name; | |
278 | ||
279 | if (level == 0) | |
280 | type = &re->id; | |
281 | ||
282 | if ((rva & 0x80000000) != 0) | |
283 | { | |
284 | rva &=~ 0x80000000; | |
57402f1e NC |
285 | if (rva >= (rc_uint_type) (flaginfo->data_end - flaginfo->data)) |
286 | overrun (flaginfo, _("ID subdirectory")); | |
252b5132 | 287 | re->subdir = 1; |
57402f1e | 288 | re->u.dir = read_coff_res_dir (wrbfd, flaginfo->data + rva, flaginfo, type, |
252b5132 RH |
289 | level + 1); |
290 | } | |
291 | else | |
292 | { | |
57402f1e NC |
293 | if (rva >= (rc_uint_type) (flaginfo->data_end - flaginfo->data)) |
294 | overrun (flaginfo, _("ID resource")); | |
252b5132 | 295 | re->subdir = 0; |
57402f1e | 296 | re->u.res = read_coff_data_entry (wrbfd, flaginfo->data + rva, flaginfo, type); |
252b5132 RH |
297 | } |
298 | ||
299 | *pp = re; | |
300 | pp = &re->next; | |
301 | } | |
302 | ||
303 | return rd; | |
304 | } | |
305 | ||
306 | /* Read a resource data entry. */ | |
307 | ||
4a594fce NC |
308 | static rc_res_resource * |
309 | read_coff_data_entry (windres_bfd *wrbfd, const bfd_byte *data, | |
57402f1e | 310 | const struct coff_file_info *flaginfo, |
4a594fce | 311 | const rc_res_id *type) |
252b5132 RH |
312 | { |
313 | const struct extern_res_data *erd; | |
4a594fce NC |
314 | rc_res_resource *r; |
315 | rc_uint_type size, rva; | |
252b5132 RH |
316 | const bfd_byte *resdata; |
317 | ||
318 | if (type == NULL) | |
319 | fatal (_("resource type unknown")); | |
320 | ||
57402f1e NC |
321 | if ((size_t) (flaginfo->data_end - data) < sizeof (struct extern_res_data)) |
322 | overrun (flaginfo, _("data entry")); | |
252b5132 RH |
323 | |
324 | erd = (const struct extern_res_data *) data; | |
325 | ||
4a594fce NC |
326 | size = windres_get_32 (wrbfd, erd->size, 4); |
327 | rva = windres_get_32 (wrbfd, erd->rva, 4); | |
57402f1e NC |
328 | if (rva < flaginfo->secaddr |
329 | || rva - flaginfo->secaddr >= (rc_uint_type) (flaginfo->data_end - flaginfo->data)) | |
330 | overrun (flaginfo, _("resource data")); | |
252b5132 | 331 | |
57402f1e | 332 | resdata = flaginfo->data + (rva - flaginfo->secaddr); |
252b5132 | 333 | |
57402f1e NC |
334 | if (size > (rc_uint_type) (flaginfo->data_end - resdata)) |
335 | overrun (flaginfo, _("resource data size")); | |
252b5132 | 336 | |
4a594fce | 337 | r = bin_to_res (wrbfd, *type, resdata, size); |
252b5132 | 338 | |
4a594fce NC |
339 | memset (&r->res_info, 0, sizeof (rc_res_res_info)); |
340 | r->coff_info.codepage = windres_get_32 (wrbfd, erd->codepage, 4); | |
341 | r->coff_info.reserved = windres_get_32 (wrbfd, erd->reserved, 4); | |
252b5132 RH |
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. */ | |
4a594fce | 351 | bindata *d; |
252b5132 | 352 | /* The last structure we have added to the list. */ |
4a594fce NC |
353 | bindata *last; |
354 | /* The size of the list as a whole. */ | |
355 | unsigned long length; | |
356 | }; | |
357 | ||
358 | struct coff_res_data_build | |
359 | { | |
360 | /* The data. */ | |
361 | coff_res_data *d; | |
362 | /* The last structure we have added to the list. */ | |
363 | coff_res_data *last; | |
252b5132 RH |
364 | /* The size of the list as a whole. */ |
365 | unsigned long length; | |
366 | }; | |
367 | ||
368 | /* This structure keeps track of information as we build the directory | |
369 | tree. */ | |
370 | ||
371 | struct coff_write_info | |
372 | { | |
373 | /* These fields are based on the BFD. */ | |
374 | /* The BFD itself. */ | |
4a594fce | 375 | windres_bfd *wrbfd; |
252b5132 RH |
376 | /* Pointer to section symbol used to build RVA relocs. */ |
377 | asymbol **sympp; | |
378 | ||
379 | /* These fields are computed initially, and then not changed. */ | |
380 | /* Length of directory tables and entries. */ | |
381 | unsigned long dirsize; | |
382 | /* Length of directory entry strings. */ | |
383 | unsigned long dirstrsize; | |
384 | /* Length of resource data entries. */ | |
385 | unsigned long dataentsize; | |
386 | ||
387 | /* These fields are updated as we add data. */ | |
388 | /* Directory tables and entries. */ | |
389 | struct bindata_build dirs; | |
390 | /* Directory entry strings. */ | |
391 | struct bindata_build dirstrs; | |
392 | /* Resource data entries. */ | |
393 | struct bindata_build dataents; | |
394 | /* Actual resource data. */ | |
4a594fce | 395 | struct coff_res_data_build resources; |
252b5132 RH |
396 | /* Relocations. */ |
397 | arelent **relocs; | |
398 | /* Number of relocations. */ | |
399 | unsigned int reloc_count; | |
400 | }; | |
401 | ||
4a594fce NC |
402 | static void coff_bin_sizes (const rc_res_directory *, struct coff_write_info *); |
403 | static bfd_byte *coff_alloc (struct bindata_build *, rc_uint_type); | |
252b5132 | 404 | static void coff_to_bin |
4a594fce | 405 | (const rc_res_directory *, struct coff_write_info *); |
252b5132 | 406 | static void coff_res_to_bin |
4a594fce | 407 | (const rc_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 | 418 | write_coff_file (const char *filename, const char *target, |
4a594fce | 419 | const rc_res_directory *resources) |
252b5132 RH |
420 | { |
421 | bfd *abfd; | |
422 | asection *sec; | |
423 | struct coff_write_info cwi; | |
4a594fce NC |
424 | windres_bfd wrbfd; |
425 | bindata *d; | |
426 | coff_res_data *rd; | |
252b5132 RH |
427 | unsigned long length, offset; |
428 | ||
429 | if (filename == NULL) | |
430 | fatal (_("filename required for COFF output")); | |
431 | ||
432 | abfd = bfd_openw (filename, target); | |
433 | if (abfd == NULL) | |
434 | bfd_fatal (filename); | |
435 | ||
436 | if (! bfd_set_format (abfd, bfd_object)) | |
437 | bfd_fatal ("bfd_set_format"); | |
438 | ||
8a0e0f38 NC |
439 | #if defined DLLTOOL_SH |
440 | if (! bfd_set_arch_mach (abfd, bfd_arch_sh, 0)) | |
441 | bfd_fatal ("bfd_set_arch_mach(sh)"); | |
442 | #elif defined DLLTOOL_MIPS | |
443 | if (! bfd_set_arch_mach (abfd, bfd_arch_mips, 0)) | |
444 | bfd_fatal ("bfd_set_arch_mach(mips)"); | |
445 | #elif defined DLLTOOL_ARM | |
09cda596 DD |
446 | if (! bfd_set_arch_mach (abfd, bfd_arch_arm, 0)) |
447 | bfd_fatal ("bfd_set_arch_mach(arm)"); | |
448 | #else | |
252b5132 RH |
449 | /* FIXME: This is obviously i386 specific. */ |
450 | if (! bfd_set_arch_mach (abfd, bfd_arch_i386, 0)) | |
09cda596 | 451 | bfd_fatal ("bfd_set_arch_mach(i386)"); |
8a0e0f38 | 452 | #endif |
252b5132 RH |
453 | |
454 | if (! bfd_set_file_flags (abfd, HAS_SYMS | HAS_RELOC)) | |
455 | bfd_fatal ("bfd_set_file_flags"); | |
456 | ||
0eb80fd3 AM |
457 | sec = bfd_make_section_with_flags (abfd, ".rsrc", |
458 | (SEC_HAS_CONTENTS | SEC_ALLOC | |
459 | | SEC_LOAD | SEC_DATA)); | |
252b5132 RH |
460 | if (sec == NULL) |
461 | bfd_fatal ("bfd_make_section"); | |
462 | ||
252b5132 RH |
463 | if (! bfd_set_symtab (abfd, sec->symbol_ptr_ptr, 1)) |
464 | bfd_fatal ("bfd_set_symtab"); | |
465 | ||
466 | /* Requiring this is probably a bug in BFD. */ | |
467 | sec->output_section = sec; | |
468 | ||
469 | /* The order of data in the .rsrc section is | |
470 | resource directory tables and entries | |
471 | resource directory strings | |
472 | resource data entries | |
473 | actual resource data | |
474 | ||
475 | We build these different types of data in different lists. */ | |
476 | ||
4a594fce NC |
477 | set_windres_bfd (&wrbfd, abfd, sec, WR_KIND_BFD); |
478 | ||
479 | cwi.wrbfd = &wrbfd; | |
252b5132 RH |
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 | ||
e2729ebe NC |
503 | /* Force the directory strings to be 64 bit aligned. Every other |
504 | structure is 64 bit aligned anyhow. */ | |
505 | cwi.dirstrsize = (cwi.dirstrsize + 7) & ~7; | |
252b5132 RH |
506 | |
507 | /* Actually convert the resources to binary. */ | |
508 | coff_to_bin (resources, &cwi); | |
509 | ||
e2729ebe | 510 | /* Add another few bytes to the directory strings if needed for |
252b5132 | 511 | alignment. */ |
e2729ebe | 512 | if ((cwi.dirstrs.length & 7) != 0) |
252b5132 | 513 | { |
e2729ebe | 514 | rc_uint_type pad = 8 - (cwi.dirstrs.length & 7); |
4a594fce | 515 | bfd_byte *ex; |
252b5132 | 516 | |
e2729ebe NC |
517 | ex = coff_alloc (& cwi.dirstrs, pad); |
518 | memset (ex, 0, pad); | |
252b5132 RH |
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 | { | |
4a594fce | 546 | set_windres_bfd_content (&wrbfd, d->data, offset, d->length); |
252b5132 RH |
547 | offset += d->length; |
548 | } | |
549 | for (d = cwi.dataents.d; d != NULL; d = d->next) | |
550 | { | |
4a594fce | 551 | set_windres_bfd_content (&wrbfd, d->data, offset, d->length); |
252b5132 RH |
552 | offset += d->length; |
553 | } | |
4a594fce | 554 | for (rd = cwi.resources.d; rd != NULL; rd = rd->next) |
252b5132 | 555 | { |
4a594fce NC |
556 | res_to_bin (cwi.wrbfd, (rc_uint_type) offset, rd->res); |
557 | offset += rd->length; | |
252b5132 RH |
558 | } |
559 | ||
560 | assert (offset == length); | |
561 | ||
562 | if (! bfd_close (abfd)) | |
563 | bfd_fatal ("bfd_close"); | |
564 | ||
565 | /* We allocated the relocs array using malloc. */ | |
566 | free (cwi.relocs); | |
567 | } | |
568 | ||
569 | /* Work out the sizes of the various fixed size resource directory | |
570 | entries. This updates fields in CWI. */ | |
571 | ||
572 | static void | |
4a594fce | 573 | coff_bin_sizes (const rc_res_directory *resdir, |
2da42df6 | 574 | struct coff_write_info *cwi) |
252b5132 | 575 | { |
4a594fce | 576 | const rc_res_entry *re; |
252b5132 RH |
577 | |
578 | cwi->dirsize += sizeof (struct extern_res_directory); | |
579 | ||
580 | for (re = resdir->entries; re != NULL; re = re->next) | |
581 | { | |
582 | cwi->dirsize += sizeof (struct extern_res_entry); | |
583 | ||
584 | if (re->id.named) | |
585 | cwi->dirstrsize += re->id.u.n.length * 2 + 2; | |
586 | ||
587 | if (re->subdir) | |
588 | coff_bin_sizes (re->u.dir, cwi); | |
589 | else | |
590 | cwi->dataentsize += sizeof (struct extern_res_data); | |
591 | } | |
592 | } | |
593 | ||
594 | /* Allocate data for a particular list. */ | |
595 | ||
4a594fce NC |
596 | static bfd_byte * |
597 | coff_alloc (struct bindata_build *bb, rc_uint_type size) | |
252b5132 | 598 | { |
4a594fce | 599 | bindata *d; |
252b5132 | 600 | |
4a594fce | 601 | d = (bindata *) reswr_alloc (sizeof (bindata)); |
252b5132 RH |
602 | |
603 | d->next = NULL; | |
4a594fce | 604 | d->data = (bfd_byte *) reswr_alloc (size); |
252b5132 RH |
605 | d->length = size; |
606 | ||
607 | if (bb->d == NULL) | |
608 | bb->d = d; | |
609 | else | |
610 | bb->last->next = d; | |
611 | bb->last = d; | |
612 | bb->length += size; | |
613 | ||
614 | return d->data; | |
615 | } | |
616 | ||
617 | /* Convert the resource directory RESDIR to binary. */ | |
618 | ||
619 | static void | |
4a594fce | 620 | coff_to_bin (const rc_res_directory *resdir, struct coff_write_info *cwi) |
252b5132 RH |
621 | { |
622 | struct extern_res_directory *erd; | |
623 | int ci, cn; | |
4a594fce | 624 | const rc_res_entry *e; |
252b5132 RH |
625 | struct extern_res_entry *ere; |
626 | ||
627 | /* Write out the directory table. */ | |
628 | ||
629 | erd = ((struct extern_res_directory *) | |
630 | coff_alloc (&cwi->dirs, sizeof (*erd))); | |
631 | ||
4a594fce NC |
632 | windres_put_32 (cwi->wrbfd, erd->characteristics, resdir->characteristics); |
633 | windres_put_32 (cwi->wrbfd, erd->time, resdir->time); | |
634 | windres_put_16 (cwi->wrbfd, erd->major, resdir->major); | |
635 | windres_put_16 (cwi->wrbfd, erd->minor, resdir->minor); | |
252b5132 RH |
636 | |
637 | ci = 0; | |
638 | cn = 0; | |
639 | for (e = resdir->entries; e != NULL; e = e->next) | |
640 | { | |
641 | if (e->id.named) | |
642 | ++cn; | |
643 | else | |
644 | ++ci; | |
645 | } | |
646 | ||
4a594fce NC |
647 | windres_put_16 (cwi->wrbfd, erd->name_count, cn); |
648 | windres_put_16 (cwi->wrbfd, erd->id_count, ci); | |
252b5132 RH |
649 | |
650 | /* Write out the data entries. Note that we allocate space for all | |
651 | the entries before writing them out. That permits a recursive | |
652 | call to work correctly when writing out subdirectories. */ | |
653 | ||
654 | ere = ((struct extern_res_entry *) | |
655 | coff_alloc (&cwi->dirs, (ci + cn) * sizeof (*ere))); | |
656 | for (e = resdir->entries; e != NULL; e = e->next, ere++) | |
657 | { | |
658 | if (! e->id.named) | |
4a594fce | 659 | windres_put_32 (cwi->wrbfd, ere->name, e->id.u.id); |
252b5132 RH |
660 | else |
661 | { | |
4a594fce NC |
662 | bfd_byte *str; |
663 | rc_uint_type i; | |
252b5132 RH |
664 | |
665 | /* For some reason existing files seem to have the high bit | |
666 | set on the address of the name, although that is not | |
667 | documented. */ | |
4a594fce NC |
668 | windres_put_32 (cwi->wrbfd, ere->name, |
669 | 0x80000000 | (cwi->dirsize + cwi->dirstrs.length)); | |
252b5132 RH |
670 | |
671 | str = coff_alloc (&cwi->dirstrs, e->id.u.n.length * 2 + 2); | |
4a594fce | 672 | windres_put_16 (cwi->wrbfd, str, e->id.u.n.length); |
252b5132 | 673 | for (i = 0; i < e->id.u.n.length; i++) |
4a594fce | 674 | windres_put_16 (cwi->wrbfd, str + (i + 1) * sizeof (unichar), e->id.u.n.name[i]); |
252b5132 RH |
675 | } |
676 | ||
677 | if (e->subdir) | |
678 | { | |
4a594fce | 679 | windres_put_32 (cwi->wrbfd, ere->rva, 0x80000000 | cwi->dirs.length); |
252b5132 RH |
680 | coff_to_bin (e->u.dir, cwi); |
681 | } | |
682 | else | |
683 | { | |
4a594fce NC |
684 | windres_put_32 (cwi->wrbfd, ere->rva, |
685 | cwi->dirsize + cwi->dirstrsize + cwi->dataents.length); | |
252b5132 RH |
686 | |
687 | coff_res_to_bin (e->u.res, cwi); | |
688 | } | |
689 | } | |
690 | } | |
691 | ||
692 | /* Convert the resource RES to binary. */ | |
693 | ||
694 | static void | |
4a594fce | 695 | coff_res_to_bin (const rc_res_resource *res, struct coff_write_info *cwi) |
252b5132 RH |
696 | { |
697 | arelent *r; | |
698 | struct extern_res_data *erd; | |
4a594fce | 699 | coff_res_data *d; |
252b5132 RH |
700 | |
701 | /* For some reason, although every other address is a section | |
702 | offset, the address of the resource data itself is an RVA. That | |
703 | means that we need to generate a relocation for it. We allocate | |
704 | the relocs array using malloc so that we can use realloc. FIXME: | |
705 | This relocation handling is correct for the i386, but probably | |
706 | not for any other target. */ | |
707 | ||
708 | r = (arelent *) reswr_alloc (sizeof (arelent)); | |
709 | r->sym_ptr_ptr = cwi->sympp; | |
710 | r->address = cwi->dirsize + cwi->dirstrsize + cwi->dataents.length; | |
711 | r->addend = 0; | |
4a594fce | 712 | r->howto = bfd_reloc_type_lookup (WR_BFD (cwi->wrbfd), BFD_RELOC_RVA); |
252b5132 RH |
713 | if (r->howto == NULL) |
714 | bfd_fatal (_("can't get BFD_RELOC_RVA relocation type")); | |
715 | ||
716 | cwi->relocs = xrealloc (cwi->relocs, | |
717 | (cwi->reloc_count + 2) * sizeof (arelent *)); | |
718 | cwi->relocs[cwi->reloc_count] = r; | |
719 | cwi->relocs[cwi->reloc_count + 1] = NULL; | |
720 | ++cwi->reloc_count; | |
721 | ||
722 | erd = (struct extern_res_data *) coff_alloc (&cwi->dataents, sizeof (*erd)); | |
723 | ||
4a594fce | 724 | windres_put_32 (cwi->wrbfd, erd->rva, |
252b5132 RH |
725 | (cwi->dirsize |
726 | + cwi->dirstrsize | |
727 | + cwi->dataentsize | |
4a594fce NC |
728 | + cwi->resources.length)); |
729 | windres_put_32 (cwi->wrbfd, erd->codepage, res->coff_info.codepage); | |
730 | windres_put_32 (cwi->wrbfd, erd->reserved, res->coff_info.reserved); | |
252b5132 | 731 | |
4a594fce NC |
732 | d = (coff_res_data *) reswr_alloc (sizeof (coff_res_data)); |
733 | d->length = res_to_bin (NULL, (rc_uint_type) 0, res); | |
734 | d->res = res; | |
735 | d->next = NULL; | |
252b5132 RH |
736 | |
737 | if (cwi->resources.d == NULL) | |
738 | cwi->resources.d = d; | |
739 | else | |
740 | cwi->resources.last->next = d; | |
741 | ||
252b5132 | 742 | cwi->resources.last = d; |
e2729ebe | 743 | cwi->resources.length += (d->length + 7) & ~7; |
252b5132 | 744 | |
4a594fce | 745 | windres_put_32 (cwi->wrbfd, erd->size, d->length); |
252b5132 | 746 | |
e2729ebe NC |
747 | /* Force the next resource to have 64 bit alignment. */ |
748 | d->length = (d->length + 7) & ~7; | |
252b5132 | 749 | } |