simple-object-coff.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805
  1. /* simple-object-coff.c -- routines to manipulate COFF object files.
  2. Copyright 2010 Free Software Foundation, Inc.
  3. Written by Ian Lance Taylor, Google.
  4. This program is free software; you can redistribute it and/or modify it
  5. under the terms of the GNU General Public License as published by the
  6. Free Software Foundation; either version 2, or (at your option) any
  7. later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, 51 Franklin Street - Fifth Floor,
  15. Boston, MA 02110-1301, USA. */
  16. #include "config.h"
  17. #include "libiberty.h"
  18. #include "simple-object.h"
  19. #include <errno.h>
  20. #include <stddef.h>
  21. #ifdef HAVE_STDLIB_H
  22. #include <stdlib.h>
  23. #endif
  24. #ifdef HAVE_STDINT_H
  25. #include <stdint.h>
  26. #endif
  27. #ifdef HAVE_STRING_H
  28. #include <string.h>
  29. #endif
  30. #ifdef HAVE_INTTYPES_H
  31. #include <inttypes.h>
  32. #endif
  33. #include "simple-object-common.h"
  34. /* COFF structures and constants. */
  35. /* COFF file header. */
  36. struct external_filehdr
  37. {
  38. unsigned char f_magic[2]; /* magic number */
  39. unsigned char f_nscns[2]; /* number of sections */
  40. unsigned char f_timdat[4]; /* time & date stamp */
  41. unsigned char f_symptr[4]; /* file pointer to symtab */
  42. unsigned char f_nsyms[4]; /* number of symtab entries */
  43. unsigned char f_opthdr[2]; /* sizeof(optional hdr) */
  44. unsigned char f_flags[2]; /* flags */
  45. };
  46. /* Bits for filehdr f_flags field. */
  47. #define F_EXEC (0x0002)
  48. #define IMAGE_FILE_SYSTEM (0x1000)
  49. #define IMAGE_FILE_DLL (0x2000)
  50. /* COFF section header. */
  51. struct external_scnhdr
  52. {
  53. unsigned char s_name[8]; /* section name */
  54. unsigned char s_paddr[4]; /* physical address, aliased s_nlib */
  55. unsigned char s_vaddr[4]; /* virtual address */
  56. unsigned char s_size[4]; /* section size */
  57. unsigned char s_scnptr[4]; /* file ptr to raw data for section */
  58. unsigned char s_relptr[4]; /* file ptr to relocation */
  59. unsigned char s_lnnoptr[4]; /* file ptr to line numbers */
  60. unsigned char s_nreloc[2]; /* number of relocation entries */
  61. unsigned char s_nlnno[2]; /* number of line number entries */
  62. unsigned char s_flags[4]; /* flags */
  63. };
  64. /* The length of the s_name field in struct external_scnhdr. */
  65. #define SCNNMLEN (8)
  66. /* Bits for scnhdr s_flags field. This includes some bits defined
  67. only for PE. This may need to be moved into coff_magic. */
  68. #define STYP_DATA (1 << 6)
  69. #define IMAGE_SCN_MEM_DISCARDABLE (1 << 25)
  70. #define IMAGE_SCN_MEM_SHARED (1 << 28)
  71. #define IMAGE_SCN_MEM_READ (1 << 30)
  72. #define IMAGE_SCN_ALIGN_POWER_BIT_POS 20
  73. #define IMAGE_SCN_ALIGN_POWER_CONST(val) \
  74. (((val) + 1) << IMAGE_SCN_ALIGN_POWER_BIT_POS)
  75. /* COFF symbol table entry. */
  76. #define E_SYMNMLEN 8 /* # characters in a symbol name */
  77. struct external_syment
  78. {
  79. union
  80. {
  81. unsigned char e_name[E_SYMNMLEN];
  82. struct
  83. {
  84. unsigned char e_zeroes[4];
  85. unsigned char e_offset[4];
  86. } e;
  87. } e;
  88. unsigned char e_value[4];
  89. unsigned char e_scnum[2];
  90. unsigned char e_type[2];
  91. unsigned char e_sclass[1];
  92. unsigned char e_numaux[1];
  93. };
  94. /* Length allowed for filename in aux sym format 4. */
  95. #define E_FILNMLEN 18
  96. /* Omits x_sym and other unused variants. */
  97. union external_auxent
  98. {
  99. /* Aux sym format 4: file. */
  100. union
  101. {
  102. char x_fname[E_FILNMLEN];
  103. struct
  104. {
  105. unsigned char x_zeroes[4];
  106. unsigned char x_offset[4];
  107. } x_n;
  108. } x_file;
  109. /* Aux sym format 5: section. */
  110. struct
  111. {
  112. unsigned char x_scnlen[4]; /* section length */
  113. unsigned char x_nreloc[2]; /* # relocation entries */
  114. unsigned char x_nlinno[2]; /* # line numbers */
  115. unsigned char x_checksum[4]; /* section COMDAT checksum */
  116. unsigned char x_associated[2]; /* COMDAT assoc section index */
  117. unsigned char x_comdat[1]; /* COMDAT selection number */
  118. } x_scn;
  119. };
  120. /* Symbol-related constants. */
  121. #define IMAGE_SYM_DEBUG (-2)
  122. #define IMAGE_SYM_TYPE_NULL (0)
  123. #define IMAGE_SYM_DTYPE_NULL (0)
  124. #define IMAGE_SYM_CLASS_STATIC (3)
  125. #define IMAGE_SYM_CLASS_FILE (103)
  126. #define IMAGE_SYM_TYPE \
  127. ((IMAGE_SYM_DTYPE_NULL << 4) | IMAGE_SYM_TYPE_NULL)
  128. /* Private data for an simple_object_read. */
  129. struct simple_object_coff_read
  130. {
  131. /* Magic number. */
  132. unsigned short magic;
  133. /* Whether the file is big-endian. */
  134. unsigned char is_big_endian;
  135. /* Number of sections. */
  136. unsigned short nscns;
  137. /* File offset of symbol table. */
  138. off_t symptr;
  139. /* Number of symbol table entries. */
  140. unsigned int nsyms;
  141. /* Flags. */
  142. unsigned short flags;
  143. /* Offset of section headers in file. */
  144. off_t scnhdr_offset;
  145. };
  146. /* Private data for an simple_object_attributes. */
  147. struct simple_object_coff_attributes
  148. {
  149. /* Magic number. */
  150. unsigned short magic;
  151. /* Whether the file is big-endian. */
  152. unsigned char is_big_endian;
  153. /* Flags. */
  154. unsigned short flags;
  155. };
  156. /* There is no magic number which indicates a COFF file as opposed to
  157. any other sort of file. Instead, each COFF file starts with a
  158. two-byte magic number which also indicates the type of the target.
  159. This struct holds a magic number as well as characteristics of that
  160. COFF format. */
  161. struct coff_magic_struct
  162. {
  163. /* Magic number. */
  164. unsigned short magic;
  165. /* Whether this magic number is for a big-endian file. */
  166. unsigned char is_big_endian;
  167. /* Flag bits, in the f_flags fields, which indicates that this file
  168. is not a relocatable object file. There is no flag which
  169. specifically indicates a relocatable object file, it is only
  170. implied by the absence of these flags. */
  171. unsigned short non_object_flags;
  172. };
  173. /* This is a list of the COFF magic numbers which we recognize, namely
  174. the ones used on Windows. More can be added as needed. */
  175. static const struct coff_magic_struct coff_magic[] =
  176. {
  177. /* i386. */
  178. { 0x14c, 0, F_EXEC | IMAGE_FILE_SYSTEM | IMAGE_FILE_DLL },
  179. /* x86_64. */
  180. { 0x8664, 0, F_EXEC | IMAGE_FILE_SYSTEM | IMAGE_FILE_DLL }
  181. };
  182. /* See if we have a COFF file. */
  183. static void *
  184. simple_object_coff_match (unsigned char header[SIMPLE_OBJECT_MATCH_HEADER_LEN],
  185. int descriptor, off_t offset,
  186. const char *segment_name ATTRIBUTE_UNUSED,
  187. const char **errmsg, int *err)
  188. {
  189. size_t c;
  190. unsigned short magic_big;
  191. unsigned short magic_little;
  192. unsigned short magic;
  193. size_t i;
  194. int is_big_endian;
  195. unsigned short (*fetch_16) (const unsigned char *);
  196. unsigned int (*fetch_32) (const unsigned char *);
  197. unsigned char hdrbuf[sizeof (struct external_filehdr)];
  198. unsigned short flags;
  199. struct simple_object_coff_read *ocr;
  200. c = sizeof (coff_magic) / sizeof (coff_magic[0]);
  201. magic_big = simple_object_fetch_big_16 (header);
  202. magic_little = simple_object_fetch_little_16 (header);
  203. for (i = 0; i < c; ++i)
  204. {
  205. if (coff_magic[i].is_big_endian
  206. ? coff_magic[i].magic == magic_big
  207. : coff_magic[i].magic == magic_little)
  208. break;
  209. }
  210. if (i >= c)
  211. {
  212. *errmsg = NULL;
  213. *err = 0;
  214. return NULL;
  215. }
  216. is_big_endian = coff_magic[i].is_big_endian;
  217. magic = is_big_endian ? magic_big : magic_little;
  218. fetch_16 = (is_big_endian
  219. ? simple_object_fetch_big_16
  220. : simple_object_fetch_little_16);
  221. fetch_32 = (is_big_endian
  222. ? simple_object_fetch_big_32
  223. : simple_object_fetch_little_32);
  224. if (!simple_object_internal_read (descriptor, offset, hdrbuf, sizeof hdrbuf,
  225. errmsg, err))
  226. return NULL;
  227. flags = fetch_16 (hdrbuf + offsetof (struct external_filehdr, f_flags));
  228. if ((flags & coff_magic[i].non_object_flags) != 0)
  229. {
  230. *errmsg = "not relocatable object file";
  231. *err = 0;
  232. return NULL;
  233. }
  234. ocr = XNEW (struct simple_object_coff_read);
  235. ocr->magic = magic;
  236. ocr->is_big_endian = is_big_endian;
  237. ocr->nscns = fetch_16 (hdrbuf + offsetof (struct external_filehdr, f_nscns));
  238. ocr->symptr = fetch_32 (hdrbuf
  239. + offsetof (struct external_filehdr, f_symptr));
  240. ocr->nsyms = fetch_32 (hdrbuf + offsetof (struct external_filehdr, f_nsyms));
  241. ocr->flags = flags;
  242. ocr->scnhdr_offset = (sizeof (struct external_filehdr)
  243. + fetch_16 (hdrbuf + offsetof (struct external_filehdr,
  244. f_opthdr)));
  245. return (void *) ocr;
  246. }
  247. /* Read the string table in a COFF file. */
  248. static char *
  249. simple_object_coff_read_strtab (simple_object_read *sobj, size_t *strtab_size,
  250. const char **errmsg, int *err)
  251. {
  252. struct simple_object_coff_read *ocr =
  253. (struct simple_object_coff_read *) sobj->data;
  254. off_t strtab_offset;
  255. unsigned char strsizebuf[4];
  256. size_t strsize;
  257. char *strtab;
  258. strtab_offset = sobj->offset + ocr->symptr
  259. + ocr->nsyms * sizeof (struct external_syment);
  260. if (!simple_object_internal_read (sobj->descriptor, strtab_offset,
  261. strsizebuf, 4, errmsg, err))
  262. return NULL;
  263. strsize = (ocr->is_big_endian
  264. ? simple_object_fetch_big_32 (strsizebuf)
  265. : simple_object_fetch_little_32 (strsizebuf));
  266. strtab = XNEWVEC (char, strsize);
  267. if (!simple_object_internal_read (sobj->descriptor, strtab_offset,
  268. (unsigned char *) strtab, strsize, errmsg,
  269. err))
  270. {
  271. XDELETEVEC (strtab);
  272. return NULL;
  273. }
  274. *strtab_size = strsize;
  275. return strtab;
  276. }
  277. /* Find all sections in a COFF file. */
  278. static const char *
  279. simple_object_coff_find_sections (simple_object_read *sobj,
  280. int (*pfn) (void *, const char *,
  281. off_t offset, off_t length),
  282. void *data,
  283. int *err)
  284. {
  285. struct simple_object_coff_read *ocr =
  286. (struct simple_object_coff_read *) sobj->data;
  287. size_t scnhdr_size;
  288. unsigned char *scnbuf;
  289. const char *errmsg;
  290. unsigned int (*fetch_32) (const unsigned char *);
  291. unsigned int nscns;
  292. char *strtab;
  293. size_t strtab_size;
  294. unsigned int i;
  295. scnhdr_size = sizeof (struct external_scnhdr);
  296. scnbuf = XNEWVEC (unsigned char, scnhdr_size * ocr->nscns);
  297. if (!simple_object_internal_read (sobj->descriptor,
  298. sobj->offset + ocr->scnhdr_offset,
  299. scnbuf, scnhdr_size * ocr->nscns, &errmsg,
  300. err))
  301. {
  302. XDELETEVEC (scnbuf);
  303. return errmsg;
  304. }
  305. fetch_32 = (ocr->is_big_endian
  306. ? simple_object_fetch_big_32
  307. : simple_object_fetch_little_32);
  308. nscns = ocr->nscns;
  309. strtab = NULL;
  310. strtab_size = 0;
  311. for (i = 0; i < nscns; ++i)
  312. {
  313. unsigned char *scnhdr;
  314. unsigned char *scnname;
  315. char namebuf[SCNNMLEN + 1];
  316. char *name;
  317. off_t scnptr;
  318. unsigned int size;
  319. scnhdr = scnbuf + i * scnhdr_size;
  320. scnname = scnhdr + offsetof (struct external_scnhdr, s_name);
  321. memcpy (namebuf, scnname, SCNNMLEN);
  322. namebuf[SCNNMLEN] = '\0';
  323. name = &namebuf[0];
  324. if (namebuf[0] == '/')
  325. {
  326. size_t strindex;
  327. char *end;
  328. strindex = strtol (namebuf + 1, &end, 10);
  329. if (*end == '\0')
  330. {
  331. /* The real section name is found in the string
  332. table. */
  333. if (strtab == NULL)
  334. {
  335. strtab = simple_object_coff_read_strtab (sobj,
  336. &strtab_size,
  337. &errmsg, err);
  338. if (strtab == NULL)
  339. {
  340. XDELETEVEC (scnbuf);
  341. return errmsg;
  342. }
  343. }
  344. if (strindex < 4 || strindex >= strtab_size)
  345. {
  346. XDELETEVEC (strtab);
  347. XDELETEVEC (scnbuf);
  348. *err = 0;
  349. return "section string index out of range";
  350. }
  351. name = strtab + strindex;
  352. }
  353. }
  354. scnptr = fetch_32 (scnhdr + offsetof (struct external_scnhdr, s_scnptr));
  355. size = fetch_32 (scnhdr + offsetof (struct external_scnhdr, s_size));
  356. if (!(*pfn) (data, name, scnptr, size))
  357. break;
  358. }
  359. if (strtab != NULL)
  360. XDELETEVEC (strtab);
  361. XDELETEVEC (scnbuf);
  362. return NULL;
  363. }
  364. /* Fetch the attributes for an simple_object_read. */
  365. static void *
  366. simple_object_coff_fetch_attributes (simple_object_read *sobj,
  367. const char **errmsg ATTRIBUTE_UNUSED,
  368. int *err ATTRIBUTE_UNUSED)
  369. {
  370. struct simple_object_coff_read *ocr =
  371. (struct simple_object_coff_read *) sobj->data;
  372. struct simple_object_coff_attributes *ret;
  373. ret = XNEW (struct simple_object_coff_attributes);
  374. ret->magic = ocr->magic;
  375. ret->is_big_endian = ocr->is_big_endian;
  376. ret->flags = ocr->flags;
  377. return ret;
  378. }
  379. /* Release the private data for an simple_object_read. */
  380. static void
  381. simple_object_coff_release_read (void *data)
  382. {
  383. XDELETE (data);
  384. }
  385. /* Compare two attributes structures. */
  386. static const char *
  387. simple_object_coff_attributes_merge (void *todata, void *fromdata, int *err)
  388. {
  389. struct simple_object_coff_attributes *to =
  390. (struct simple_object_coff_attributes *) todata;
  391. struct simple_object_coff_attributes *from =
  392. (struct simple_object_coff_attributes *) fromdata;
  393. if (to->magic != from->magic || to->is_big_endian != from->is_big_endian)
  394. {
  395. *err = 0;
  396. return "COFF object format mismatch";
  397. }
  398. return NULL;
  399. }
  400. /* Release the private data for an attributes structure. */
  401. static void
  402. simple_object_coff_release_attributes (void *data)
  403. {
  404. XDELETE (data);
  405. }
  406. /* Prepare to write out a file. */
  407. static void *
  408. simple_object_coff_start_write (void *attributes_data,
  409. const char **errmsg ATTRIBUTE_UNUSED,
  410. int *err ATTRIBUTE_UNUSED)
  411. {
  412. struct simple_object_coff_attributes *attrs =
  413. (struct simple_object_coff_attributes *) attributes_data;
  414. struct simple_object_coff_attributes *ret;
  415. /* We're just going to record the attributes, but we need to make a
  416. copy because the user may delete them. */
  417. ret = XNEW (struct simple_object_coff_attributes);
  418. *ret = *attrs;
  419. return ret;
  420. }
  421. /* Write out a COFF filehdr. */
  422. static int
  423. simple_object_coff_write_filehdr (simple_object_write *sobj, int descriptor,
  424. unsigned int nscns, size_t symtab_offset,
  425. unsigned int nsyms, const char **errmsg,
  426. int *err)
  427. {
  428. struct simple_object_coff_attributes *attrs =
  429. (struct simple_object_coff_attributes *) sobj->data;
  430. unsigned char hdrbuf[sizeof (struct external_filehdr)];
  431. unsigned char *hdr;
  432. void (*set_16) (unsigned char *, unsigned short);
  433. void (*set_32) (unsigned char *, unsigned int);
  434. hdr = &hdrbuf[0];
  435. set_16 = (attrs->is_big_endian
  436. ? simple_object_set_big_16
  437. : simple_object_set_little_16);
  438. set_32 = (attrs->is_big_endian
  439. ? simple_object_set_big_32
  440. : simple_object_set_little_32);
  441. memset (hdr, 0, sizeof (struct external_filehdr));
  442. set_16 (hdr + offsetof (struct external_filehdr, f_magic), attrs->magic);
  443. set_16 (hdr + offsetof (struct external_filehdr, f_nscns), nscns);
  444. /* f_timdat left as zero. */
  445. set_32 (hdr + offsetof (struct external_filehdr, f_symptr), symtab_offset);
  446. set_32 (hdr + offsetof (struct external_filehdr, f_nsyms), nsyms);
  447. /* f_opthdr left as zero. */
  448. set_16 (hdr + offsetof (struct external_filehdr, f_flags), attrs->flags);
  449. return simple_object_internal_write (descriptor, 0, hdrbuf,
  450. sizeof (struct external_filehdr),
  451. errmsg, err);
  452. }
  453. /* Write out a COFF section header. */
  454. static int
  455. simple_object_coff_write_scnhdr (simple_object_write *sobj, int descriptor,
  456. const char *name, size_t *name_offset,
  457. off_t scnhdr_offset, size_t scnsize,
  458. off_t offset, unsigned int align,
  459. const char **errmsg, int *err)
  460. {
  461. struct simple_object_coff_attributes *attrs =
  462. (struct simple_object_coff_attributes *) sobj->data;
  463. void (*set_32) (unsigned char *, unsigned int);
  464. unsigned char hdrbuf[sizeof (struct external_scnhdr)];
  465. unsigned char *hdr;
  466. size_t namelen;
  467. unsigned int flags;
  468. set_32 = (attrs->is_big_endian
  469. ? simple_object_set_big_32
  470. : simple_object_set_little_32);
  471. memset (hdrbuf, 0, sizeof hdrbuf);
  472. hdr = &hdrbuf[0];
  473. namelen = strlen (name);
  474. if (namelen <= SCNNMLEN)
  475. strncpy ((char *) hdr + offsetof (struct external_scnhdr, s_name), name,
  476. SCNNMLEN);
  477. else
  478. {
  479. snprintf ((char *) hdr + offsetof (struct external_scnhdr, s_name),
  480. SCNNMLEN, "/%lu", (unsigned long) *name_offset);
  481. *name_offset += namelen + 1;
  482. }
  483. /* s_paddr left as zero. */
  484. /* s_vaddr left as zero. */
  485. set_32 (hdr + offsetof (struct external_scnhdr, s_size), scnsize);
  486. set_32 (hdr + offsetof (struct external_scnhdr, s_scnptr), offset);
  487. /* s_relptr left as zero. */
  488. /* s_lnnoptr left as zero. */
  489. /* s_nreloc left as zero. */
  490. /* s_nlnno left as zero. */
  491. flags = (STYP_DATA | IMAGE_SCN_MEM_DISCARDABLE | IMAGE_SCN_MEM_SHARED
  492. | IMAGE_SCN_MEM_READ);
  493. /* PE can represent alignment up to 13. */
  494. if (align > 13)
  495. align = 13;
  496. flags |= IMAGE_SCN_ALIGN_POWER_CONST(align);
  497. set_32 (hdr + offsetof (struct external_scnhdr, s_flags), flags);
  498. return simple_object_internal_write (descriptor, scnhdr_offset, hdrbuf,
  499. sizeof (struct external_scnhdr),
  500. errmsg, err);
  501. }
  502. /* Write out a complete COFF file. */
  503. static const char *
  504. simple_object_coff_write_to_file (simple_object_write *sobj, int descriptor,
  505. int *err)
  506. {
  507. struct simple_object_coff_attributes *attrs =
  508. (struct simple_object_coff_attributes *) sobj->data;
  509. unsigned int nscns, secnum;
  510. simple_object_write_section *section;
  511. off_t scnhdr_offset;
  512. size_t symtab_offset;
  513. off_t secsym_offset;
  514. unsigned int nsyms;
  515. size_t offset;
  516. size_t name_offset;
  517. const char *errmsg;
  518. unsigned char strsizebuf[4];
  519. /* The interface doesn't give us access to the name of the input file
  520. yet. We want to use its basename for the FILE symbol. This is
  521. what 'gas' uses when told to assemble from stdin. */
  522. const char *source_filename = "fake";
  523. size_t sflen;
  524. union
  525. {
  526. struct external_syment sym;
  527. union external_auxent aux;
  528. } syms[2];
  529. void (*set_16) (unsigned char *, unsigned short);
  530. void (*set_32) (unsigned char *, unsigned int);
  531. set_16 = (attrs->is_big_endian
  532. ? simple_object_set_big_16
  533. : simple_object_set_little_16);
  534. set_32 = (attrs->is_big_endian
  535. ? simple_object_set_big_32
  536. : simple_object_set_little_32);
  537. nscns = 0;
  538. for (section = sobj->sections; section != NULL; section = section->next)
  539. ++nscns;
  540. scnhdr_offset = sizeof (struct external_filehdr);
  541. offset = scnhdr_offset + nscns * sizeof (struct external_scnhdr);
  542. name_offset = 4;
  543. for (section = sobj->sections; section != NULL; section = section->next)
  544. {
  545. size_t mask;
  546. size_t new_offset;
  547. size_t scnsize;
  548. struct simple_object_write_section_buffer *buffer;
  549. mask = (1U << section->align) - 1;
  550. new_offset = offset & mask;
  551. new_offset &= ~ mask;
  552. while (new_offset > offset)
  553. {
  554. unsigned char zeroes[16];
  555. size_t write;
  556. memset (zeroes, 0, sizeof zeroes);
  557. write = new_offset - offset;
  558. if (write > sizeof zeroes)
  559. write = sizeof zeroes;
  560. if (!simple_object_internal_write (descriptor, offset, zeroes, write,
  561. &errmsg, err))
  562. return errmsg;
  563. }
  564. scnsize = 0;
  565. for (buffer = section->buffers; buffer != NULL; buffer = buffer->next)
  566. {
  567. if (!simple_object_internal_write (descriptor, offset + scnsize,
  568. ((const unsigned char *)
  569. buffer->buffer),
  570. buffer->size, &errmsg, err))
  571. return errmsg;
  572. scnsize += buffer->size;
  573. }
  574. if (!simple_object_coff_write_scnhdr (sobj, descriptor, section->name,
  575. &name_offset, scnhdr_offset,
  576. scnsize, offset, section->align,
  577. &errmsg, err))
  578. return errmsg;
  579. scnhdr_offset += sizeof (struct external_scnhdr);
  580. offset += scnsize;
  581. }
  582. /* Symbol table is always half-word aligned. */
  583. offset += (offset & 1);
  584. /* There is a file symbol and a section symbol per section,
  585. and each of these has a single auxiliary symbol following. */
  586. nsyms = 2 * (nscns + 1);
  587. symtab_offset = offset;
  588. /* Advance across space reserved for symbol table to locate
  589. start of string table. */
  590. offset += nsyms * sizeof (struct external_syment);
  591. /* Write out file symbol. */
  592. memset (&syms[0], 0, sizeof (syms));
  593. strcpy ((char *)&syms[0].sym.e.e_name[0], ".file");
  594. set_16 (&syms[0].sym.e_scnum[0], IMAGE_SYM_DEBUG);
  595. set_16 (&syms[0].sym.e_type[0], IMAGE_SYM_TYPE);
  596. syms[0].sym.e_sclass[0] = IMAGE_SYM_CLASS_FILE;
  597. syms[0].sym.e_numaux[0] = 1;
  598. /* The name need not be nul-terminated if it fits into the x_fname field
  599. directly, but must be if it has to be placed into the string table. */
  600. sflen = strlen (source_filename);
  601. if (sflen <= E_FILNMLEN)
  602. memcpy (&syms[1].aux.x_file.x_fname[0], source_filename, sflen);
  603. else
  604. {
  605. set_32 (&syms[1].aux.x_file.x_n.x_offset[0], name_offset);
  606. if (!simple_object_internal_write (descriptor, offset + name_offset,
  607. ((const unsigned char *)
  608. source_filename),
  609. sflen + 1, &errmsg, err))
  610. return errmsg;
  611. name_offset += strlen (source_filename) + 1;
  612. }
  613. if (!simple_object_internal_write (descriptor, symtab_offset,
  614. (const unsigned char *) &syms[0],
  615. sizeof (syms), &errmsg, err))
  616. return errmsg;
  617. /* Write the string table length, followed by the strings and section
  618. symbols in step with each other. */
  619. set_32 (strsizebuf, name_offset);
  620. if (!simple_object_internal_write (descriptor, offset, strsizebuf, 4,
  621. &errmsg, err))
  622. return errmsg;
  623. name_offset = 4;
  624. secsym_offset = symtab_offset + sizeof (syms);
  625. memset (&syms[0], 0, sizeof (syms));
  626. set_16 (&syms[0].sym.e_type[0], IMAGE_SYM_TYPE);
  627. syms[0].sym.e_sclass[0] = IMAGE_SYM_CLASS_STATIC;
  628. syms[0].sym.e_numaux[0] = 1;
  629. secnum = 1;
  630. for (section = sobj->sections; section != NULL; section = section->next)
  631. {
  632. size_t namelen;
  633. size_t scnsize;
  634. struct simple_object_write_section_buffer *buffer;
  635. namelen = strlen (section->name);
  636. set_16 (&syms[0].sym.e_scnum[0], secnum++);
  637. scnsize = 0;
  638. for (buffer = section->buffers; buffer != NULL; buffer = buffer->next)
  639. scnsize += buffer->size;
  640. set_32 (&syms[1].aux.x_scn.x_scnlen[0], scnsize);
  641. if (namelen > SCNNMLEN)
  642. {
  643. set_32 (&syms[0].sym.e.e.e_zeroes[0], 0);
  644. set_32 (&syms[0].sym.e.e.e_offset[0], name_offset);
  645. if (!simple_object_internal_write (descriptor, offset + name_offset,
  646. ((const unsigned char *)
  647. section->name),
  648. namelen + 1, &errmsg, err))
  649. return errmsg;
  650. name_offset += namelen + 1;
  651. }
  652. else
  653. {
  654. memcpy (&syms[0].sym.e.e_name[0], section->name,
  655. strlen (section->name));
  656. memset (&syms[0].sym.e.e_name[strlen (section->name)], 0,
  657. E_SYMNMLEN - strlen (section->name));
  658. }
  659. if (!simple_object_internal_write (descriptor, secsym_offset,
  660. (const unsigned char *) &syms[0],
  661. sizeof (syms), &errmsg, err))
  662. return errmsg;
  663. secsym_offset += sizeof (syms);
  664. }
  665. if (!simple_object_coff_write_filehdr (sobj, descriptor, nscns,
  666. symtab_offset, nsyms, &errmsg, err))
  667. return errmsg;
  668. return NULL;
  669. }
  670. /* Release the private data for an simple_object_write structure. */
  671. static void
  672. simple_object_coff_release_write (void *data)
  673. {
  674. XDELETE (data);
  675. }
  676. /* The COFF functions. */
  677. const struct simple_object_functions simple_object_coff_functions =
  678. {
  679. simple_object_coff_match,
  680. simple_object_coff_find_sections,
  681. simple_object_coff_fetch_attributes,
  682. simple_object_coff_release_read,
  683. simple_object_coff_attributes_merge,
  684. simple_object_coff_release_attributes,
  685. simple_object_coff_start_write,
  686. simple_object_coff_write_to_file,
  687. simple_object_coff_release_write
  688. };