rescoff.c 22 KB

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