coffswap.h 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841
  1. /* Generic COFF swapping routines, for BFD.
  2. Copyright (C) 1990-2015 Free Software Foundation, Inc.
  3. Written by Cygnus Support.
  4. This file is part of BFD, the Binary File Descriptor library.
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 3 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
  16. MA 02110-1301, USA. */
  17. /* This file contains routines used to swap COFF data. It is a header
  18. file because the details of swapping depend on the details of the
  19. structures used by each COFF implementation. This is included by
  20. coffcode.h, as well as by the ECOFF backend.
  21. Any file which uses this must first include "coff/internal.h" and
  22. "coff/CPU.h". The functions will then be correct for that CPU. */
  23. #ifndef GET_FCN_LNNOPTR
  24. #define GET_FCN_LNNOPTR(abfd, ext) \
  25. H_GET_32 (abfd, ext->x_sym.x_fcnary.x_fcn.x_lnnoptr)
  26. #endif
  27. #ifndef GET_FCN_ENDNDX
  28. #define GET_FCN_ENDNDX(abfd, ext) \
  29. H_GET_32 (abfd, ext->x_sym.x_fcnary.x_fcn.x_endndx)
  30. #endif
  31. #ifndef PUT_FCN_LNNOPTR
  32. #define PUT_FCN_LNNOPTR(abfd, in, ext) \
  33. H_PUT_32 (abfd, in, ext->x_sym.x_fcnary.x_fcn.x_lnnoptr)
  34. #endif
  35. #ifndef PUT_FCN_ENDNDX
  36. #define PUT_FCN_ENDNDX(abfd, in, ext) \
  37. H_PUT_32 (abfd, in, ext->x_sym.x_fcnary.x_fcn.x_endndx)
  38. #endif
  39. #ifndef GET_LNSZ_LNNO
  40. #define GET_LNSZ_LNNO(abfd, ext) \
  41. H_GET_16 (abfd, ext->x_sym.x_misc.x_lnsz.x_lnno)
  42. #endif
  43. #ifndef GET_LNSZ_SIZE
  44. #define GET_LNSZ_SIZE(abfd, ext) \
  45. H_GET_16 (abfd, ext->x_sym.x_misc.x_lnsz.x_size)
  46. #endif
  47. #ifndef PUT_LNSZ_LNNO
  48. #define PUT_LNSZ_LNNO(abfd, in, ext) \
  49. H_PUT_16 (abfd, in, ext->x_sym.x_misc.x_lnsz.x_lnno)
  50. #endif
  51. #ifndef PUT_LNSZ_SIZE
  52. #define PUT_LNSZ_SIZE(abfd, in, ext) \
  53. H_PUT_16 (abfd, in, ext->x_sym.x_misc.x_lnsz.x_size)
  54. #endif
  55. #ifndef GET_SCN_SCNLEN
  56. #define GET_SCN_SCNLEN(abfd, ext) \
  57. H_GET_32 (abfd, ext->x_scn.x_scnlen)
  58. #endif
  59. #ifndef GET_SCN_NRELOC
  60. #define GET_SCN_NRELOC(abfd, ext) \
  61. H_GET_16 (abfd, ext->x_scn.x_nreloc)
  62. #endif
  63. #ifndef GET_SCN_NLINNO
  64. #define GET_SCN_NLINNO(abfd, ext) \
  65. H_GET_16 (abfd, ext->x_scn.x_nlinno)
  66. #endif
  67. #ifndef PUT_SCN_SCNLEN
  68. #define PUT_SCN_SCNLEN(abfd, in, ext) \
  69. H_PUT_32 (abfd, in, ext->x_scn.x_scnlen)
  70. #endif
  71. #ifndef PUT_SCN_NRELOC
  72. #define PUT_SCN_NRELOC(abfd, in, ext) \
  73. H_PUT_16 (abfd, in, ext->x_scn.x_nreloc)
  74. #endif
  75. #ifndef PUT_SCN_NLINNO
  76. #define PUT_SCN_NLINNO(abfd, in, ext) \
  77. H_PUT_16 (abfd, in, ext->x_scn.x_nlinno)
  78. #endif
  79. #ifndef GET_LINENO_LNNO
  80. #define GET_LINENO_LNNO(abfd, ext) \
  81. H_GET_16 (abfd, ext->l_lnno);
  82. #endif
  83. #ifndef PUT_LINENO_LNNO
  84. #define PUT_LINENO_LNNO(abfd, val, ext) \
  85. H_PUT_16 (abfd, val, ext->l_lnno);
  86. #endif
  87. /* The f_symptr field in the filehdr is sometimes 64 bits. */
  88. #ifndef GET_FILEHDR_SYMPTR
  89. #define GET_FILEHDR_SYMPTR H_GET_32
  90. #endif
  91. #ifndef PUT_FILEHDR_SYMPTR
  92. #define PUT_FILEHDR_SYMPTR H_PUT_32
  93. #endif
  94. /* Some fields in the aouthdr are sometimes 64 bits. */
  95. #ifndef GET_AOUTHDR_TSIZE
  96. #define GET_AOUTHDR_TSIZE H_GET_32
  97. #endif
  98. #ifndef PUT_AOUTHDR_TSIZE
  99. #define PUT_AOUTHDR_TSIZE H_PUT_32
  100. #endif
  101. #ifndef GET_AOUTHDR_DSIZE
  102. #define GET_AOUTHDR_DSIZE H_GET_32
  103. #endif
  104. #ifndef PUT_AOUTHDR_DSIZE
  105. #define PUT_AOUTHDR_DSIZE H_PUT_32
  106. #endif
  107. #ifndef GET_AOUTHDR_BSIZE
  108. #define GET_AOUTHDR_BSIZE H_GET_32
  109. #endif
  110. #ifndef PUT_AOUTHDR_BSIZE
  111. #define PUT_AOUTHDR_BSIZE H_PUT_32
  112. #endif
  113. #ifndef GET_AOUTHDR_ENTRY
  114. #define GET_AOUTHDR_ENTRY H_GET_32
  115. #endif
  116. #ifndef PUT_AOUTHDR_ENTRY
  117. #define PUT_AOUTHDR_ENTRY H_PUT_32
  118. #endif
  119. #ifndef GET_AOUTHDR_TEXT_START
  120. #define GET_AOUTHDR_TEXT_START H_GET_32
  121. #endif
  122. #ifndef PUT_AOUTHDR_TEXT_START
  123. #define PUT_AOUTHDR_TEXT_START H_PUT_32
  124. #endif
  125. #ifndef GET_AOUTHDR_DATA_START
  126. #define GET_AOUTHDR_DATA_START H_GET_32
  127. #endif
  128. #ifndef PUT_AOUTHDR_DATA_START
  129. #define PUT_AOUTHDR_DATA_START H_PUT_32
  130. #endif
  131. /* Some fields in the scnhdr are sometimes 64 bits. */
  132. #ifndef GET_SCNHDR_PADDR
  133. #define GET_SCNHDR_PADDR H_GET_32
  134. #endif
  135. #ifndef PUT_SCNHDR_PADDR
  136. #define PUT_SCNHDR_PADDR H_PUT_32
  137. #endif
  138. #ifndef GET_SCNHDR_VADDR
  139. #define GET_SCNHDR_VADDR H_GET_32
  140. #endif
  141. #ifndef PUT_SCNHDR_VADDR
  142. #define PUT_SCNHDR_VADDR H_PUT_32
  143. #endif
  144. #ifndef GET_SCNHDR_SIZE
  145. #define GET_SCNHDR_SIZE H_GET_32
  146. #endif
  147. #ifndef PUT_SCNHDR_SIZE
  148. #define PUT_SCNHDR_SIZE H_PUT_32
  149. #endif
  150. #ifndef GET_SCNHDR_SCNPTR
  151. #define GET_SCNHDR_SCNPTR H_GET_32
  152. #endif
  153. #ifndef PUT_SCNHDR_SCNPTR
  154. #define PUT_SCNHDR_SCNPTR H_PUT_32
  155. #endif
  156. #ifndef GET_SCNHDR_RELPTR
  157. #define GET_SCNHDR_RELPTR H_GET_32
  158. #endif
  159. #ifndef PUT_SCNHDR_RELPTR
  160. #define PUT_SCNHDR_RELPTR H_PUT_32
  161. #endif
  162. #ifndef GET_SCNHDR_LNNOPTR
  163. #define GET_SCNHDR_LNNOPTR H_GET_32
  164. #endif
  165. #ifndef PUT_SCNHDR_LNNOPTR
  166. #define PUT_SCNHDR_LNNOPTR H_PUT_32
  167. #endif
  168. #ifndef GET_SCNHDR_NRELOC
  169. #define GET_SCNHDR_NRELOC H_GET_16
  170. #endif
  171. #ifndef MAX_SCNHDR_NRELOC
  172. #define MAX_SCNHDR_NRELOC 0xffff
  173. #endif
  174. #ifndef PUT_SCNHDR_NRELOC
  175. #define PUT_SCNHDR_NRELOC H_PUT_16
  176. #endif
  177. #ifndef GET_SCNHDR_NLNNO
  178. #define GET_SCNHDR_NLNNO H_GET_16
  179. #endif
  180. #ifndef MAX_SCNHDR_NLNNO
  181. #define MAX_SCNHDR_NLNNO 0xffff
  182. #endif
  183. #ifndef PUT_SCNHDR_NLNNO
  184. #define PUT_SCNHDR_NLNNO H_PUT_16
  185. #endif
  186. #ifndef GET_SCNHDR_FLAGS
  187. #define GET_SCNHDR_FLAGS H_GET_32
  188. #endif
  189. #ifndef PUT_SCNHDR_FLAGS
  190. #define PUT_SCNHDR_FLAGS H_PUT_32
  191. #endif
  192. #ifndef GET_RELOC_VADDR
  193. #define GET_RELOC_VADDR H_GET_32
  194. #endif
  195. #ifndef PUT_RELOC_VADDR
  196. #define PUT_RELOC_VADDR H_PUT_32
  197. #endif
  198. #ifndef NO_COFF_RELOCS
  199. static void
  200. coff_swap_reloc_in (bfd * abfd, void * src, void * dst)
  201. {
  202. RELOC *reloc_src = (RELOC *) src;
  203. struct internal_reloc *reloc_dst = (struct internal_reloc *) dst;
  204. reloc_dst->r_vaddr = GET_RELOC_VADDR (abfd, reloc_src->r_vaddr);
  205. reloc_dst->r_symndx = H_GET_S32 (abfd, reloc_src->r_symndx);
  206. reloc_dst->r_type = H_GET_16 (abfd, reloc_src->r_type);
  207. #ifdef SWAP_IN_RELOC_OFFSET
  208. reloc_dst->r_offset = SWAP_IN_RELOC_OFFSET (abfd, reloc_src->r_offset);
  209. #endif
  210. }
  211. static unsigned int
  212. coff_swap_reloc_out (bfd * abfd, void * src, void * dst)
  213. {
  214. struct internal_reloc *reloc_src = (struct internal_reloc *) src;
  215. struct external_reloc *reloc_dst = (struct external_reloc *) dst;
  216. PUT_RELOC_VADDR (abfd, reloc_src->r_vaddr, reloc_dst->r_vaddr);
  217. H_PUT_32 (abfd, reloc_src->r_symndx, reloc_dst->r_symndx);
  218. H_PUT_16 (abfd, reloc_src->r_type, reloc_dst->r_type);
  219. #ifdef SWAP_OUT_RELOC_OFFSET
  220. SWAP_OUT_RELOC_OFFSET (abfd, reloc_src->r_offset, reloc_dst->r_offset);
  221. #endif
  222. #ifdef SWAP_OUT_RELOC_EXTRA
  223. SWAP_OUT_RELOC_EXTRA (abfd, reloc_src, reloc_dst);
  224. #endif
  225. return bfd_coff_relsz (abfd);
  226. }
  227. #endif /* NO_COFF_RELOCS */
  228. static void
  229. coff_swap_filehdr_in (bfd * abfd, void * src, void * dst)
  230. {
  231. FILHDR *filehdr_src = (FILHDR *) src;
  232. struct internal_filehdr *filehdr_dst = (struct internal_filehdr *) dst;
  233. #ifdef COFF_ADJUST_FILEHDR_IN_PRE
  234. COFF_ADJUST_FILEHDR_IN_PRE (abfd, src, dst);
  235. #endif
  236. filehdr_dst->f_magic = H_GET_16 (abfd, filehdr_src->f_magic);
  237. filehdr_dst->f_nscns = H_GET_16 (abfd, filehdr_src->f_nscns);
  238. filehdr_dst->f_timdat = H_GET_32 (abfd, filehdr_src->f_timdat);
  239. filehdr_dst->f_symptr = GET_FILEHDR_SYMPTR (abfd, filehdr_src->f_symptr);
  240. filehdr_dst->f_nsyms = H_GET_32 (abfd, filehdr_src->f_nsyms);
  241. filehdr_dst->f_opthdr = H_GET_16 (abfd, filehdr_src->f_opthdr);
  242. filehdr_dst->f_flags = H_GET_16 (abfd, filehdr_src->f_flags);
  243. #ifdef TIC80_TARGET_ID
  244. filehdr_dst->f_target_id = H_GET_16 (abfd, filehdr_src->f_target_id);
  245. #endif
  246. #ifdef COFF_ADJUST_FILEHDR_IN_POST
  247. COFF_ADJUST_FILEHDR_IN_POST (abfd, src, dst);
  248. #endif
  249. }
  250. static unsigned int
  251. coff_swap_filehdr_out (bfd *abfd, void * in, void * out)
  252. {
  253. struct internal_filehdr *filehdr_in = (struct internal_filehdr *) in;
  254. FILHDR *filehdr_out = (FILHDR *) out;
  255. #ifdef COFF_ADJUST_FILEHDR_OUT_PRE
  256. COFF_ADJUST_FILEHDR_OUT_PRE (abfd, in, out);
  257. #endif
  258. H_PUT_16 (abfd, filehdr_in->f_magic, filehdr_out->f_magic);
  259. H_PUT_16 (abfd, filehdr_in->f_nscns, filehdr_out->f_nscns);
  260. H_PUT_32 (abfd, filehdr_in->f_timdat, filehdr_out->f_timdat);
  261. PUT_FILEHDR_SYMPTR (abfd, filehdr_in->f_symptr, filehdr_out->f_symptr);
  262. H_PUT_32 (abfd, filehdr_in->f_nsyms, filehdr_out->f_nsyms);
  263. H_PUT_16 (abfd, filehdr_in->f_opthdr, filehdr_out->f_opthdr);
  264. H_PUT_16 (abfd, filehdr_in->f_flags, filehdr_out->f_flags);
  265. #ifdef TIC80_TARGET_ID
  266. H_PUT_16 (abfd, filehdr_in->f_target_id, filehdr_out->f_target_id);
  267. #endif
  268. #ifdef COFF_ADJUST_FILEHDR_OUT_POST
  269. COFF_ADJUST_FILEHDR_OUT_POST (abfd, in, out);
  270. #endif
  271. return bfd_coff_filhsz (abfd);
  272. }
  273. #ifndef NO_COFF_SYMBOLS
  274. static void
  275. coff_swap_sym_in (bfd * abfd, void * ext1, void * in1)
  276. {
  277. SYMENT *ext = (SYMENT *) ext1;
  278. struct internal_syment *in = (struct internal_syment *) in1;
  279. if (ext->e.e_name[0] == 0)
  280. {
  281. in->_n._n_n._n_zeroes = 0;
  282. in->_n._n_n._n_offset = H_GET_32 (abfd, ext->e.e.e_offset);
  283. }
  284. else
  285. {
  286. #if SYMNMLEN != E_SYMNMLEN
  287. #error we need to cope with truncating or extending SYMNMLEN
  288. #else
  289. memcpy (in->_n._n_name, ext->e.e_name, SYMNMLEN);
  290. #endif
  291. }
  292. in->n_value = H_GET_32 (abfd, ext->e_value);
  293. in->n_scnum = H_GET_16 (abfd, ext->e_scnum);
  294. if (sizeof (ext->e_type) == 2)
  295. in->n_type = H_GET_16 (abfd, ext->e_type);
  296. else
  297. in->n_type = H_GET_32 (abfd, ext->e_type);
  298. in->n_sclass = H_GET_8 (abfd, ext->e_sclass);
  299. in->n_numaux = H_GET_8 (abfd, ext->e_numaux);
  300. #ifdef COFF_ADJUST_SYM_IN_POST
  301. COFF_ADJUST_SYM_IN_POST (abfd, ext1, in1);
  302. #endif
  303. }
  304. static unsigned int
  305. coff_swap_sym_out (bfd * abfd, void * inp, void * extp)
  306. {
  307. struct internal_syment *in = (struct internal_syment *) inp;
  308. SYMENT *ext =(SYMENT *) extp;
  309. #ifdef COFF_ADJUST_SYM_OUT_PRE
  310. COFF_ADJUST_SYM_OUT_PRE (abfd, inp, extp);
  311. #endif
  312. if (in->_n._n_name[0] == 0)
  313. {
  314. H_PUT_32 (abfd, 0, ext->e.e.e_zeroes);
  315. H_PUT_32 (abfd, in->_n._n_n._n_offset, ext->e.e.e_offset);
  316. }
  317. else
  318. {
  319. #if SYMNMLEN != E_SYMNMLEN
  320. #error we need to cope with truncating or extending SYMNMLEN
  321. #else
  322. memcpy (ext->e.e_name, in->_n._n_name, SYMNMLEN);
  323. #endif
  324. }
  325. H_PUT_32 (abfd, in->n_value, ext->e_value);
  326. H_PUT_16 (abfd, in->n_scnum, ext->e_scnum);
  327. if (sizeof (ext->e_type) == 2)
  328. H_PUT_16 (abfd, in->n_type, ext->e_type);
  329. else
  330. H_PUT_32 (abfd, in->n_type, ext->e_type);
  331. H_PUT_8 (abfd, in->n_sclass, ext->e_sclass);
  332. H_PUT_8 (abfd, in->n_numaux, ext->e_numaux);
  333. #ifdef COFF_ADJUST_SYM_OUT_POST
  334. COFF_ADJUST_SYM_OUT_POST (abfd, inp, extp);
  335. #endif
  336. return SYMESZ;
  337. }
  338. static void
  339. coff_swap_aux_in (bfd *abfd,
  340. void * ext1,
  341. int type,
  342. int in_class,
  343. int indx,
  344. int numaux,
  345. void * in1)
  346. {
  347. AUXENT *ext = (AUXENT *) ext1;
  348. union internal_auxent *in = (union internal_auxent *) in1;
  349. #ifdef COFF_ADJUST_AUX_IN_PRE
  350. COFF_ADJUST_AUX_IN_PRE (abfd, ext1, type, in_class, indx, numaux, in1);
  351. #endif
  352. switch (in_class)
  353. {
  354. case C_FILE:
  355. if (ext->x_file.x_fname[0] == 0)
  356. {
  357. in->x_file.x_n.x_zeroes = 0;
  358. in->x_file.x_n.x_offset = H_GET_32 (abfd, ext->x_file.x_n.x_offset);
  359. }
  360. else
  361. {
  362. #if FILNMLEN != E_FILNMLEN
  363. #error we need to cope with truncating or extending FILNMLEN
  364. #else
  365. if (numaux > 1)
  366. {
  367. if (indx == 0)
  368. memcpy (in->x_file.x_fname, ext->x_file.x_fname,
  369. numaux * sizeof (AUXENT));
  370. }
  371. else
  372. memcpy (in->x_file.x_fname, ext->x_file.x_fname, FILNMLEN);
  373. #endif
  374. }
  375. goto end;
  376. case C_STAT:
  377. #ifdef C_LEAFSTAT
  378. case C_LEAFSTAT:
  379. #endif
  380. case C_HIDDEN:
  381. if (type == T_NULL)
  382. {
  383. in->x_scn.x_scnlen = GET_SCN_SCNLEN (abfd, ext);
  384. in->x_scn.x_nreloc = GET_SCN_NRELOC (abfd, ext);
  385. in->x_scn.x_nlinno = GET_SCN_NLINNO (abfd, ext);
  386. /* PE defines some extra fields; we zero them out for
  387. safety. */
  388. in->x_scn.x_checksum = 0;
  389. in->x_scn.x_associated = 0;
  390. in->x_scn.x_comdat = 0;
  391. goto end;
  392. }
  393. break;
  394. }
  395. in->x_sym.x_tagndx.l = H_GET_32 (abfd, ext->x_sym.x_tagndx);
  396. #ifndef NO_TVNDX
  397. in->x_sym.x_tvndx = H_GET_16 (abfd, ext->x_sym.x_tvndx);
  398. #endif
  399. if (in_class == C_BLOCK || in_class == C_FCN || ISFCN (type)
  400. || ISTAG (in_class))
  401. {
  402. in->x_sym.x_fcnary.x_fcn.x_lnnoptr = GET_FCN_LNNOPTR (abfd, ext);
  403. in->x_sym.x_fcnary.x_fcn.x_endndx.l = GET_FCN_ENDNDX (abfd, ext);
  404. }
  405. else
  406. {
  407. #if DIMNUM != E_DIMNUM
  408. #error we need to cope with truncating or extending DIMNUM
  409. #endif
  410. in->x_sym.x_fcnary.x_ary.x_dimen[0] =
  411. H_GET_16 (abfd, ext->x_sym.x_fcnary.x_ary.x_dimen[0]);
  412. in->x_sym.x_fcnary.x_ary.x_dimen[1] =
  413. H_GET_16 (abfd, ext->x_sym.x_fcnary.x_ary.x_dimen[1]);
  414. in->x_sym.x_fcnary.x_ary.x_dimen[2] =
  415. H_GET_16 (abfd, ext->x_sym.x_fcnary.x_ary.x_dimen[2]);
  416. in->x_sym.x_fcnary.x_ary.x_dimen[3] =
  417. H_GET_16 (abfd, ext->x_sym.x_fcnary.x_ary.x_dimen[3]);
  418. }
  419. if (ISFCN (type))
  420. in->x_sym.x_misc.x_fsize = H_GET_32 (abfd, ext->x_sym.x_misc.x_fsize);
  421. else
  422. {
  423. in->x_sym.x_misc.x_lnsz.x_lnno = GET_LNSZ_LNNO (abfd, ext);
  424. in->x_sym.x_misc.x_lnsz.x_size = GET_LNSZ_SIZE (abfd, ext);
  425. }
  426. end: ;
  427. #ifdef COFF_ADJUST_AUX_IN_POST
  428. COFF_ADJUST_AUX_IN_POST (abfd, ext1, type, in_class, indx, numaux, in1);
  429. #endif
  430. }
  431. static unsigned int
  432. coff_swap_aux_out (bfd * abfd,
  433. void * inp,
  434. int type,
  435. int in_class,
  436. int indx ATTRIBUTE_UNUSED,
  437. int numaux ATTRIBUTE_UNUSED,
  438. void * extp)
  439. {
  440. union internal_auxent * in = (union internal_auxent *) inp;
  441. AUXENT *ext = (AUXENT *) extp;
  442. #ifdef COFF_ADJUST_AUX_OUT_PRE
  443. COFF_ADJUST_AUX_OUT_PRE (abfd, inp, type, in_class, indx, numaux, extp);
  444. #endif
  445. memset (ext, 0, AUXESZ);
  446. switch (in_class)
  447. {
  448. case C_FILE:
  449. if (in->x_file.x_fname[0] == 0)
  450. {
  451. H_PUT_32 (abfd, 0, ext->x_file.x_n.x_zeroes);
  452. H_PUT_32 (abfd, in->x_file.x_n.x_offset, ext->x_file.x_n.x_offset);
  453. }
  454. else
  455. {
  456. #if FILNMLEN != E_FILNMLEN
  457. #error we need to cope with truncating or extending FILNMLEN
  458. #else
  459. memcpy (ext->x_file.x_fname, in->x_file.x_fname, FILNMLEN);
  460. #endif
  461. }
  462. goto end;
  463. case C_STAT:
  464. #ifdef C_LEAFSTAT
  465. case C_LEAFSTAT:
  466. #endif
  467. case C_HIDDEN:
  468. if (type == T_NULL)
  469. {
  470. PUT_SCN_SCNLEN (abfd, in->x_scn.x_scnlen, ext);
  471. PUT_SCN_NRELOC (abfd, in->x_scn.x_nreloc, ext);
  472. PUT_SCN_NLINNO (abfd, in->x_scn.x_nlinno, ext);
  473. goto end;
  474. }
  475. break;
  476. }
  477. H_PUT_32 (abfd, in->x_sym.x_tagndx.l, ext->x_sym.x_tagndx);
  478. #ifndef NO_TVNDX
  479. H_PUT_16 (abfd, in->x_sym.x_tvndx, ext->x_sym.x_tvndx);
  480. #endif
  481. if (in_class == C_BLOCK || in_class == C_FCN || ISFCN (type)
  482. || ISTAG (in_class))
  483. {
  484. PUT_FCN_LNNOPTR (abfd, in->x_sym.x_fcnary.x_fcn.x_lnnoptr, ext);
  485. PUT_FCN_ENDNDX (abfd, in->x_sym.x_fcnary.x_fcn.x_endndx.l, ext);
  486. }
  487. else
  488. {
  489. #if DIMNUM != E_DIMNUM
  490. #error we need to cope with truncating or extending DIMNUM
  491. #endif
  492. H_PUT_16 (abfd, in->x_sym.x_fcnary.x_ary.x_dimen[0],
  493. ext->x_sym.x_fcnary.x_ary.x_dimen[0]);
  494. H_PUT_16 (abfd, in->x_sym.x_fcnary.x_ary.x_dimen[1],
  495. ext->x_sym.x_fcnary.x_ary.x_dimen[1]);
  496. H_PUT_16 (abfd, in->x_sym.x_fcnary.x_ary.x_dimen[2],
  497. ext->x_sym.x_fcnary.x_ary.x_dimen[2]);
  498. H_PUT_16 (abfd, in->x_sym.x_fcnary.x_ary.x_dimen[3],
  499. ext->x_sym.x_fcnary.x_ary.x_dimen[3]);
  500. }
  501. if (ISFCN (type))
  502. H_PUT_32 (abfd, in->x_sym.x_misc.x_fsize, ext->x_sym.x_misc.x_fsize);
  503. else
  504. {
  505. PUT_LNSZ_LNNO (abfd, in->x_sym.x_misc.x_lnsz.x_lnno, ext);
  506. PUT_LNSZ_SIZE (abfd, in->x_sym.x_misc.x_lnsz.x_size, ext);
  507. }
  508. end:
  509. #ifdef COFF_ADJUST_AUX_OUT_POST
  510. COFF_ADJUST_AUX_OUT_POST (abfd, inp, type, in_class, indx, numaux, extp);
  511. #endif
  512. return AUXESZ;
  513. }
  514. #endif /* NO_COFF_SYMBOLS */
  515. #ifndef NO_COFF_LINENOS
  516. static void
  517. coff_swap_lineno_in (bfd * abfd, void * ext1, void * in1)
  518. {
  519. LINENO *ext = (LINENO *) ext1;
  520. struct internal_lineno *in = (struct internal_lineno *) in1;
  521. in->l_addr.l_symndx = H_GET_32 (abfd, ext->l_addr.l_symndx);
  522. in->l_lnno = GET_LINENO_LNNO (abfd, ext);
  523. }
  524. static unsigned int
  525. coff_swap_lineno_out (bfd * abfd, void * inp, void * outp)
  526. {
  527. struct internal_lineno *in = (struct internal_lineno *) inp;
  528. struct external_lineno *ext = (struct external_lineno *) outp;
  529. H_PUT_32 (abfd, in->l_addr.l_symndx, ext->l_addr.l_symndx);
  530. PUT_LINENO_LNNO (abfd, in->l_lnno, ext);
  531. return LINESZ;
  532. }
  533. #endif /* NO_COFF_LINENOS */
  534. static void
  535. coff_swap_aouthdr_in (bfd * abfd, void * aouthdr_ext1, void * aouthdr_int1)
  536. {
  537. AOUTHDR *aouthdr_ext;
  538. struct internal_aouthdr *aouthdr_int;
  539. aouthdr_ext = (AOUTHDR *) aouthdr_ext1;
  540. aouthdr_int = (struct internal_aouthdr *) aouthdr_int1;
  541. aouthdr_int->magic = H_GET_16 (abfd, aouthdr_ext->magic);
  542. aouthdr_int->vstamp = H_GET_16 (abfd, aouthdr_ext->vstamp);
  543. aouthdr_int->tsize = GET_AOUTHDR_TSIZE (abfd, aouthdr_ext->tsize);
  544. aouthdr_int->dsize = GET_AOUTHDR_DSIZE (abfd, aouthdr_ext->dsize);
  545. aouthdr_int->bsize = GET_AOUTHDR_BSIZE (abfd, aouthdr_ext->bsize);
  546. aouthdr_int->entry = GET_AOUTHDR_ENTRY (abfd, aouthdr_ext->entry);
  547. aouthdr_int->text_start =
  548. GET_AOUTHDR_TEXT_START (abfd, aouthdr_ext->text_start);
  549. aouthdr_int->data_start =
  550. GET_AOUTHDR_DATA_START (abfd, aouthdr_ext->data_start);
  551. #ifdef I960
  552. aouthdr_int->tagentries = H_GET_32 (abfd, aouthdr_ext->tagentries);
  553. #endif
  554. #ifdef APOLLO_M68
  555. H_PUT_32 (abfd, aouthdr_int->o_inlib, aouthdr_ext->o_inlib);
  556. H_PUT_32 (abfd, aouthdr_int->o_sri, aouthdr_ext->o_sri);
  557. H_PUT_32 (abfd, aouthdr_int->vid[0], aouthdr_ext->vid);
  558. H_PUT_32 (abfd, aouthdr_int->vid[1], aouthdr_ext->vid + 4);
  559. #endif
  560. #ifdef RS6000COFF_C
  561. #ifdef XCOFF64
  562. aouthdr_int->o_toc = H_GET_64 (abfd, aouthdr_ext->o_toc);
  563. #else
  564. aouthdr_int->o_toc = H_GET_32 (abfd, aouthdr_ext->o_toc);
  565. #endif
  566. aouthdr_int->o_snentry = H_GET_16 (abfd, aouthdr_ext->o_snentry);
  567. aouthdr_int->o_sntext = H_GET_16 (abfd, aouthdr_ext->o_sntext);
  568. aouthdr_int->o_sndata = H_GET_16 (abfd, aouthdr_ext->o_sndata);
  569. aouthdr_int->o_sntoc = H_GET_16 (abfd, aouthdr_ext->o_sntoc);
  570. aouthdr_int->o_snloader = H_GET_16 (abfd, aouthdr_ext->o_snloader);
  571. aouthdr_int->o_snbss = H_GET_16 (abfd, aouthdr_ext->o_snbss);
  572. aouthdr_int->o_algntext = H_GET_16 (abfd, aouthdr_ext->o_algntext);
  573. aouthdr_int->o_algndata = H_GET_16 (abfd, aouthdr_ext->o_algndata);
  574. aouthdr_int->o_modtype = H_GET_16 (abfd, aouthdr_ext->o_modtype);
  575. aouthdr_int->o_cputype = H_GET_16 (abfd, aouthdr_ext->o_cputype);
  576. #ifdef XCOFF64
  577. aouthdr_int->o_maxstack = H_GET_64 (abfd, aouthdr_ext->o_maxstack);
  578. aouthdr_int->o_maxdata = H_GET_64 (abfd, aouthdr_ext->o_maxdata);
  579. #else
  580. aouthdr_int->o_maxstack = H_GET_32 (abfd, aouthdr_ext->o_maxstack);
  581. aouthdr_int->o_maxdata = H_GET_32 (abfd, aouthdr_ext->o_maxdata);
  582. #endif
  583. #endif
  584. #ifdef MIPSECOFF
  585. aouthdr_int->bss_start = H_GET_32 (abfd, aouthdr_ext->bss_start);
  586. aouthdr_int->gp_value = H_GET_32 (abfd, aouthdr_ext->gp_value);
  587. aouthdr_int->gprmask = H_GET_32 (abfd, aouthdr_ext->gprmask);
  588. aouthdr_int->cprmask[0] = H_GET_32 (abfd, aouthdr_ext->cprmask[0]);
  589. aouthdr_int->cprmask[1] = H_GET_32 (abfd, aouthdr_ext->cprmask[1]);
  590. aouthdr_int->cprmask[2] = H_GET_32 (abfd, aouthdr_ext->cprmask[2]);
  591. aouthdr_int->cprmask[3] = H_GET_32 (abfd, aouthdr_ext->cprmask[3]);
  592. #endif
  593. #ifdef ALPHAECOFF
  594. aouthdr_int->bss_start = H_GET_64 (abfd, aouthdr_ext->bss_start);
  595. aouthdr_int->gp_value = H_GET_64 (abfd, aouthdr_ext->gp_value);
  596. aouthdr_int->gprmask = H_GET_32 (abfd, aouthdr_ext->gprmask);
  597. aouthdr_int->fprmask = H_GET_32 (abfd, aouthdr_ext->fprmask);
  598. #endif
  599. }
  600. static unsigned int
  601. coff_swap_aouthdr_out (bfd * abfd, void * in, void * out)
  602. {
  603. struct internal_aouthdr *aouthdr_in = (struct internal_aouthdr *) in;
  604. AOUTHDR *aouthdr_out = (AOUTHDR *) out;
  605. H_PUT_16 (abfd, aouthdr_in->magic, aouthdr_out->magic);
  606. H_PUT_16 (abfd, aouthdr_in->vstamp, aouthdr_out->vstamp);
  607. PUT_AOUTHDR_TSIZE (abfd, aouthdr_in->tsize, aouthdr_out->tsize);
  608. PUT_AOUTHDR_DSIZE (abfd, aouthdr_in->dsize, aouthdr_out->dsize);
  609. PUT_AOUTHDR_BSIZE (abfd, aouthdr_in->bsize, aouthdr_out->bsize);
  610. PUT_AOUTHDR_ENTRY (abfd, aouthdr_in->entry, aouthdr_out->entry);
  611. PUT_AOUTHDR_TEXT_START (abfd, aouthdr_in->text_start,
  612. aouthdr_out->text_start);
  613. PUT_AOUTHDR_DATA_START (abfd, aouthdr_in->data_start,
  614. aouthdr_out->data_start);
  615. #ifdef I960
  616. H_PUT_32 (abfd, aouthdr_in->tagentries, aouthdr_out->tagentries);
  617. #endif
  618. #ifdef RS6000COFF_C
  619. #ifdef XCOFF64
  620. H_PUT_64 (abfd, aouthdr_in->o_toc, aouthdr_out->o_toc);
  621. #else
  622. H_PUT_32 (abfd, aouthdr_in->o_toc, aouthdr_out->o_toc);
  623. #endif
  624. H_PUT_16 (abfd, aouthdr_in->o_snentry, aouthdr_out->o_snentry);
  625. H_PUT_16 (abfd, aouthdr_in->o_sntext, aouthdr_out->o_sntext);
  626. H_PUT_16 (abfd, aouthdr_in->o_sndata, aouthdr_out->o_sndata);
  627. H_PUT_16 (abfd, aouthdr_in->o_sntoc, aouthdr_out->o_sntoc);
  628. H_PUT_16 (abfd, aouthdr_in->o_snloader, aouthdr_out->o_snloader);
  629. H_PUT_16 (abfd, aouthdr_in->o_snbss, aouthdr_out->o_snbss);
  630. H_PUT_16 (abfd, aouthdr_in->o_algntext, aouthdr_out->o_algntext);
  631. H_PUT_16 (abfd, aouthdr_in->o_algndata, aouthdr_out->o_algndata);
  632. H_PUT_16 (abfd, aouthdr_in->o_modtype, aouthdr_out->o_modtype);
  633. H_PUT_16 (abfd, aouthdr_in->o_cputype, aouthdr_out->o_cputype);
  634. #ifdef XCOFF64
  635. H_PUT_64 (abfd, aouthdr_in->o_maxstack, aouthdr_out->o_maxstack);
  636. H_PUT_64 (abfd, aouthdr_in->o_maxdata, aouthdr_out->o_maxdata);
  637. #else
  638. H_PUT_32 (abfd, aouthdr_in->o_maxstack, aouthdr_out->o_maxstack);
  639. H_PUT_32 (abfd, aouthdr_in->o_maxdata, aouthdr_out->o_maxdata);
  640. #endif
  641. memset (aouthdr_out->o_resv2, 0, sizeof aouthdr_out->o_resv2);
  642. #ifdef XCOFF64
  643. memset (aouthdr_out->o_debugger, 0, sizeof aouthdr_out->o_debugger);
  644. memset (aouthdr_out->o_resv3, 0, sizeof aouthdr_out->o_resv3);
  645. #endif
  646. #endif
  647. #ifdef MIPSECOFF
  648. H_PUT_32 (abfd, aouthdr_in->bss_start, aouthdr_out->bss_start);
  649. H_PUT_32 (abfd, aouthdr_in->gp_value, aouthdr_out->gp_value);
  650. H_PUT_32 (abfd, aouthdr_in->gprmask, aouthdr_out->gprmask);
  651. H_PUT_32 (abfd, aouthdr_in->cprmask[0], aouthdr_out->cprmask[0]);
  652. H_PUT_32 (abfd, aouthdr_in->cprmask[1], aouthdr_out->cprmask[1]);
  653. H_PUT_32 (abfd, aouthdr_in->cprmask[2], aouthdr_out->cprmask[2]);
  654. H_PUT_32 (abfd, aouthdr_in->cprmask[3], aouthdr_out->cprmask[3]);
  655. #endif
  656. #ifdef ALPHAECOFF
  657. /* FIXME: What does bldrev mean? */
  658. H_PUT_16 (abfd, 2, aouthdr_out->bldrev);
  659. H_PUT_16 (abfd, 0, aouthdr_out->padding);
  660. H_PUT_64 (abfd, aouthdr_in->bss_start, aouthdr_out->bss_start);
  661. H_PUT_64 (abfd, aouthdr_in->gp_value, aouthdr_out->gp_value);
  662. H_PUT_32 (abfd, aouthdr_in->gprmask, aouthdr_out->gprmask);
  663. H_PUT_32 (abfd, aouthdr_in->fprmask, aouthdr_out->fprmask);
  664. #endif
  665. return AOUTSZ;
  666. }
  667. static void
  668. coff_swap_scnhdr_in (bfd * abfd, void * ext, void * in)
  669. {
  670. SCNHDR *scnhdr_ext = (SCNHDR *) ext;
  671. struct internal_scnhdr *scnhdr_int = (struct internal_scnhdr *) in;
  672. #ifdef COFF_ADJUST_SCNHDR_IN_PRE
  673. COFF_ADJUST_SCNHDR_IN_PRE (abfd, ext, in);
  674. #endif
  675. memcpy (scnhdr_int->s_name, scnhdr_ext->s_name, sizeof (scnhdr_int->s_name));
  676. scnhdr_int->s_vaddr = GET_SCNHDR_VADDR (abfd, scnhdr_ext->s_vaddr);
  677. scnhdr_int->s_paddr = GET_SCNHDR_PADDR (abfd, scnhdr_ext->s_paddr);
  678. scnhdr_int->s_size = GET_SCNHDR_SIZE (abfd, scnhdr_ext->s_size);
  679. scnhdr_int->s_scnptr = GET_SCNHDR_SCNPTR (abfd, scnhdr_ext->s_scnptr);
  680. scnhdr_int->s_relptr = GET_SCNHDR_RELPTR (abfd, scnhdr_ext->s_relptr);
  681. scnhdr_int->s_lnnoptr = GET_SCNHDR_LNNOPTR (abfd, scnhdr_ext->s_lnnoptr);
  682. scnhdr_int->s_flags = GET_SCNHDR_FLAGS (abfd, scnhdr_ext->s_flags);
  683. scnhdr_int->s_nreloc = GET_SCNHDR_NRELOC (abfd, scnhdr_ext->s_nreloc);
  684. scnhdr_int->s_nlnno = GET_SCNHDR_NLNNO (abfd, scnhdr_ext->s_nlnno);
  685. #ifdef I960
  686. scnhdr_int->s_align = GET_SCNHDR_ALIGN (abfd, scnhdr_ext->s_align);
  687. #endif
  688. #ifdef COFF_ADJUST_SCNHDR_IN_POST
  689. COFF_ADJUST_SCNHDR_IN_POST (abfd, ext, in);
  690. #endif
  691. }
  692. static unsigned int
  693. coff_swap_scnhdr_out (bfd * abfd, void * in, void * out)
  694. {
  695. struct internal_scnhdr *scnhdr_int = (struct internal_scnhdr *) in;
  696. SCNHDR *scnhdr_ext = (SCNHDR *) out;
  697. unsigned int ret = bfd_coff_scnhsz (abfd);
  698. #ifdef COFF_ADJUST_SCNHDR_OUT_PRE
  699. COFF_ADJUST_SCNHDR_OUT_PRE (abfd, in, out);
  700. #endif
  701. memcpy (scnhdr_ext->s_name, scnhdr_int->s_name, sizeof (scnhdr_int->s_name));
  702. PUT_SCNHDR_VADDR (abfd, scnhdr_int->s_vaddr, scnhdr_ext->s_vaddr);
  703. PUT_SCNHDR_PADDR (abfd, scnhdr_int->s_paddr, scnhdr_ext->s_paddr);
  704. PUT_SCNHDR_SIZE (abfd, scnhdr_int->s_size, scnhdr_ext->s_size);
  705. PUT_SCNHDR_SCNPTR (abfd, scnhdr_int->s_scnptr, scnhdr_ext->s_scnptr);
  706. PUT_SCNHDR_RELPTR (abfd, scnhdr_int->s_relptr, scnhdr_ext->s_relptr);
  707. PUT_SCNHDR_LNNOPTR (abfd, scnhdr_int->s_lnnoptr, scnhdr_ext->s_lnnoptr);
  708. PUT_SCNHDR_FLAGS (abfd, scnhdr_int->s_flags, scnhdr_ext->s_flags);
  709. #if defined(M88)
  710. H_PUT_32 (abfd, scnhdr_int->s_nlnno, scnhdr_ext->s_nlnno);
  711. H_PUT_32 (abfd, scnhdr_int->s_nreloc, scnhdr_ext->s_nreloc);
  712. #else
  713. if (scnhdr_int->s_nlnno <= MAX_SCNHDR_NLNNO)
  714. PUT_SCNHDR_NLNNO (abfd, scnhdr_int->s_nlnno, scnhdr_ext->s_nlnno);
  715. else
  716. {
  717. char buf[sizeof (scnhdr_int->s_name) + 1];
  718. memcpy (buf, scnhdr_int->s_name, sizeof (scnhdr_int->s_name));
  719. buf[sizeof (scnhdr_int->s_name)] = '\0';
  720. (*_bfd_error_handler)
  721. (_("%s: warning: %s: line number overflow: 0x%lx > 0xffff"),
  722. bfd_get_filename (abfd),
  723. buf, scnhdr_int->s_nlnno);
  724. PUT_SCNHDR_NLNNO (abfd, 0xffff, scnhdr_ext->s_nlnno);
  725. }
  726. if (scnhdr_int->s_nreloc <= MAX_SCNHDR_NRELOC)
  727. PUT_SCNHDR_NRELOC (abfd, scnhdr_int->s_nreloc, scnhdr_ext->s_nreloc);
  728. else
  729. {
  730. char buf[sizeof (scnhdr_int->s_name) + 1];
  731. memcpy (buf, scnhdr_int->s_name, sizeof (scnhdr_int->s_name));
  732. buf[sizeof (scnhdr_int->s_name)] = '\0';
  733. (*_bfd_error_handler) (_("%s: %s: reloc overflow: 0x%lx > 0xffff"),
  734. bfd_get_filename (abfd),
  735. buf, scnhdr_int->s_nreloc);
  736. bfd_set_error (bfd_error_file_truncated);
  737. PUT_SCNHDR_NRELOC (abfd, 0xffff, scnhdr_ext->s_nreloc);
  738. ret = 0;
  739. }
  740. #endif
  741. #ifdef I960
  742. PUT_SCNHDR_ALIGN (abfd, scnhdr_int->s_align, scnhdr_ext->s_align);
  743. #endif
  744. #ifdef COFF_ADJUST_SCNHDR_OUT_POST
  745. COFF_ADJUST_SCNHDR_OUT_POST (abfd, in, out);
  746. #endif
  747. return ret;
  748. }