aout-adobe.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  1. /* BFD back-end for a.out.adobe binaries.
  2. Copyright (C) 1990-2015 Free Software Foundation, Inc.
  3. Written by Cygnus Support. Based on bout.c.
  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, MA 02110-1301, USA. */
  16. #include "sysdep.h"
  17. #include "bfd.h"
  18. #include "libbfd.h"
  19. #include "aout/adobe.h"
  20. #include "aout/stab_gnu.h"
  21. #include "libaout.h" /* BFD a.out internal data structures. */
  22. /* Forward decl. */
  23. extern const bfd_target aout_adobe_vec;
  24. /* Swaps the information in an executable header taken from a raw byte
  25. stream memory image, into the internal exec_header structure. */
  26. static void
  27. aout_adobe_swap_exec_header_in (bfd *abfd,
  28. struct external_exec *bytes,
  29. struct internal_exec *execp)
  30. {
  31. /* Now fill in fields in the execp, from the bytes in the raw data. */
  32. execp->a_info = H_GET_32 (abfd, bytes->e_info);
  33. execp->a_text = GET_WORD (abfd, bytes->e_text);
  34. execp->a_data = GET_WORD (abfd, bytes->e_data);
  35. execp->a_bss = GET_WORD (abfd, bytes->e_bss);
  36. execp->a_syms = GET_WORD (abfd, bytes->e_syms);
  37. execp->a_entry = GET_WORD (abfd, bytes->e_entry);
  38. execp->a_trsize = GET_WORD (abfd, bytes->e_trsize);
  39. execp->a_drsize = GET_WORD (abfd, bytes->e_drsize);
  40. }
  41. /* Swaps the information in an internal exec header structure into the
  42. supplied buffer ready for writing to disk. */
  43. static void
  44. aout_adobe_swap_exec_header_out (bfd *abfd,
  45. struct internal_exec *execp,
  46. struct external_exec *bytes)
  47. {
  48. /* Now fill in fields in the raw data, from the fields in the exec
  49. struct. */
  50. H_PUT_32 (abfd, execp->a_info , bytes->e_info);
  51. PUT_WORD (abfd, execp->a_text , bytes->e_text);
  52. PUT_WORD (abfd, execp->a_data , bytes->e_data);
  53. PUT_WORD (abfd, execp->a_bss , bytes->e_bss);
  54. PUT_WORD (abfd, execp->a_syms , bytes->e_syms);
  55. PUT_WORD (abfd, execp->a_entry , bytes->e_entry);
  56. PUT_WORD (abfd, execp->a_trsize, bytes->e_trsize);
  57. PUT_WORD (abfd, execp->a_drsize, bytes->e_drsize);
  58. }
  59. /* Finish up the opening of a b.out file for reading. Fill in all the
  60. fields that are not handled by common code. */
  61. static const bfd_target *
  62. aout_adobe_callback (bfd *abfd)
  63. {
  64. struct internal_exec *execp = exec_hdr (abfd);
  65. asection *sect;
  66. struct external_segdesc ext[1];
  67. char *section_name;
  68. char try_again[30]; /* Name and number. */
  69. char *newname;
  70. int trynum;
  71. flagword flags;
  72. /* Architecture and machine type -- unknown in this format. */
  73. bfd_set_arch_mach (abfd, bfd_arch_unknown, 0L);
  74. /* The positions of the string table and symbol table. */
  75. obj_str_filepos (abfd) = N_STROFF (*execp);
  76. obj_sym_filepos (abfd) = N_SYMOFF (*execp);
  77. /* Suck up the section information from the file, one section at a time. */
  78. for (;;)
  79. {
  80. bfd_size_type amt = sizeof (*ext);
  81. if (bfd_bread ( ext, amt, abfd) != amt)
  82. {
  83. if (bfd_get_error () != bfd_error_system_call)
  84. bfd_set_error (bfd_error_wrong_format);
  85. return NULL;
  86. }
  87. switch (ext->e_type[0])
  88. {
  89. case N_TEXT:
  90. section_name = ".text";
  91. flags = SEC_CODE | SEC_LOAD | SEC_ALLOC | SEC_HAS_CONTENTS;
  92. break;
  93. case N_DATA:
  94. section_name = ".data";
  95. flags = SEC_DATA | SEC_LOAD | SEC_ALLOC | SEC_HAS_CONTENTS;
  96. break;
  97. case N_BSS:
  98. section_name = ".bss";
  99. flags = SEC_DATA | SEC_HAS_CONTENTS;
  100. break;
  101. case 0:
  102. goto no_more_sections;
  103. default:
  104. (*_bfd_error_handler)
  105. (_("%B: Unknown section type in a.out.adobe file: %x\n"),
  106. abfd, ext->e_type[0]);
  107. goto no_more_sections;
  108. }
  109. /* First one is called ".text" or whatever; subsequent ones are
  110. ".text1", ".text2", ... */
  111. bfd_set_error (bfd_error_no_error);
  112. sect = bfd_make_section_with_flags (abfd, section_name, flags);
  113. trynum = 0;
  114. while (!sect)
  115. {
  116. if (bfd_get_error () != bfd_error_no_error)
  117. /* Some other error -- slide into the sunset. */
  118. return NULL;
  119. sprintf (try_again, "%s%d", section_name, ++trynum);
  120. sect = bfd_make_section_with_flags (abfd, try_again, flags);
  121. }
  122. /* Fix the name, if it is a sprintf'd name. */
  123. if (sect->name == try_again)
  124. {
  125. amt = strlen (sect->name);
  126. newname = bfd_zalloc (abfd, amt);
  127. if (newname == NULL)
  128. return NULL;
  129. strcpy (newname, sect->name);
  130. sect->name = newname;
  131. }
  132. /* Assumed big-endian. */
  133. sect->size = ((ext->e_size[0] << 8)
  134. | ext->e_size[1] << 8
  135. | ext->e_size[2]);
  136. sect->vma = H_GET_32 (abfd, ext->e_virtbase);
  137. sect->filepos = H_GET_32 (abfd, ext->e_filebase);
  138. /* FIXME XXX alignment? */
  139. /* Set relocation information for first section of each type. */
  140. if (trynum == 0)
  141. switch (ext->e_type[0])
  142. {
  143. case N_TEXT:
  144. sect->rel_filepos = N_TRELOFF (*execp);
  145. sect->reloc_count = execp->a_trsize;
  146. break;
  147. case N_DATA:
  148. sect->rel_filepos = N_DRELOFF (*execp);
  149. sect->reloc_count = execp->a_drsize;
  150. break;
  151. default:
  152. break;
  153. }
  154. }
  155. no_more_sections:
  156. adata (abfd).reloc_entry_size = sizeof (struct reloc_std_external);
  157. adata (abfd).symbol_entry_size = sizeof (struct external_nlist);
  158. adata (abfd).page_size = 1; /* Not applicable. */
  159. adata (abfd).segment_size = 1; /* Not applicable. */
  160. adata (abfd).exec_bytes_size = EXEC_BYTES_SIZE;
  161. return abfd->xvec;
  162. }
  163. static const bfd_target *
  164. aout_adobe_object_p (bfd *abfd)
  165. {
  166. struct internal_exec anexec;
  167. struct external_exec exec_bytes;
  168. char *targ;
  169. bfd_size_type amt = EXEC_BYTES_SIZE;
  170. if (bfd_bread (& exec_bytes, amt, abfd) != amt)
  171. {
  172. if (bfd_get_error () != bfd_error_system_call)
  173. bfd_set_error (bfd_error_wrong_format);
  174. return NULL;
  175. }
  176. anexec.a_info = H_GET_32 (abfd, exec_bytes.e_info);
  177. /* Normally we just compare for the magic number.
  178. However, a bunch of Adobe tools aren't fixed up yet; they generate
  179. files using ZMAGIC(!).
  180. If the environment variable GNUTARGET is set to "a.out.adobe", we will
  181. take just about any a.out file as an Adobe a.out file. FIXME! */
  182. if (N_BADMAG (anexec))
  183. {
  184. targ = getenv ("GNUTARGET");
  185. if (targ && !strcmp (targ, aout_adobe_vec.name))
  186. /* Just continue anyway, if specifically set to this format. */
  187. ;
  188. else
  189. {
  190. bfd_set_error (bfd_error_wrong_format);
  191. return NULL;
  192. }
  193. }
  194. aout_adobe_swap_exec_header_in (abfd, &exec_bytes, &anexec);
  195. return aout_32_some_aout_object_p (abfd, &anexec, aout_adobe_callback);
  196. }
  197. struct bout_data_struct
  198. {
  199. struct aoutdata a;
  200. struct internal_exec e;
  201. };
  202. static bfd_boolean
  203. aout_adobe_mkobject (bfd *abfd)
  204. {
  205. struct bout_data_struct *rawptr;
  206. bfd_size_type amt = sizeof (struct bout_data_struct);
  207. rawptr = bfd_zalloc (abfd, amt);
  208. if (rawptr == NULL)
  209. return FALSE;
  210. abfd->tdata.bout_data = rawptr;
  211. exec_hdr (abfd) = &rawptr->e;
  212. adata (abfd).reloc_entry_size = sizeof (struct reloc_std_external);
  213. adata (abfd).symbol_entry_size = sizeof (struct external_nlist);
  214. adata (abfd).page_size = 1; /* Not applicable. */
  215. adata (abfd).segment_size = 1; /* Not applicable. */
  216. adata (abfd).exec_bytes_size = EXEC_BYTES_SIZE;
  217. return TRUE;
  218. }
  219. static void
  220. aout_adobe_write_section (bfd *abfd ATTRIBUTE_UNUSED,
  221. sec_ptr sect ATTRIBUTE_UNUSED)
  222. {
  223. /* FIXME XXX. */
  224. }
  225. static bfd_boolean
  226. aout_adobe_write_object_contents (bfd *abfd)
  227. {
  228. struct external_exec swapped_hdr;
  229. static struct external_segdesc sentinel[1]; /* Initialized to zero. */
  230. asection *sect;
  231. bfd_size_type amt;
  232. exec_hdr (abfd)->a_info = ZMAGIC;
  233. /* Calculate text size as total of text sections, etc. */
  234. exec_hdr (abfd)->a_text = 0;
  235. exec_hdr (abfd)->a_data = 0;
  236. exec_hdr (abfd)->a_bss = 0;
  237. exec_hdr (abfd)->a_trsize = 0;
  238. exec_hdr (abfd)->a_drsize = 0;
  239. for (sect = abfd->sections; sect; sect = sect->next)
  240. {
  241. if (sect->flags & SEC_CODE)
  242. {
  243. exec_hdr (abfd)->a_text += sect->size;
  244. exec_hdr (abfd)->a_trsize += sect->reloc_count *
  245. sizeof (struct reloc_std_external);
  246. }
  247. else if (sect->flags & SEC_DATA)
  248. {
  249. exec_hdr (abfd)->a_data += sect->size;
  250. exec_hdr (abfd)->a_drsize += sect->reloc_count *
  251. sizeof (struct reloc_std_external);
  252. }
  253. else if (sect->flags & SEC_ALLOC && !(sect->flags & SEC_LOAD))
  254. exec_hdr (abfd)->a_bss += sect->size;
  255. }
  256. exec_hdr (abfd)->a_syms = bfd_get_symcount (abfd)
  257. * sizeof (struct external_nlist);
  258. exec_hdr (abfd)->a_entry = bfd_get_start_address (abfd);
  259. aout_adobe_swap_exec_header_out (abfd, exec_hdr (abfd), &swapped_hdr);
  260. amt = EXEC_BYTES_SIZE;
  261. if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0
  262. || bfd_bwrite (& swapped_hdr, amt, abfd) != amt)
  263. return FALSE;
  264. /* Now write out the section information. Text first, data next, rest
  265. afterward. */
  266. for (sect = abfd->sections; sect; sect = sect->next)
  267. if (sect->flags & SEC_CODE)
  268. aout_adobe_write_section (abfd, sect);
  269. for (sect = abfd->sections; sect; sect = sect->next)
  270. if (sect->flags & SEC_DATA)
  271. aout_adobe_write_section (abfd, sect);
  272. for (sect = abfd->sections; sect; sect = sect->next)
  273. if (!(sect->flags & (SEC_CODE | SEC_DATA)))
  274. aout_adobe_write_section (abfd, sect);
  275. /* Write final `sentinel` section header (with type of 0). */
  276. amt = sizeof (*sentinel);
  277. if (bfd_bwrite (sentinel, amt, abfd) != amt)
  278. return FALSE;
  279. /* Now write out reloc info, followed by syms and strings. */
  280. if (bfd_get_symcount (abfd) != 0)
  281. {
  282. if (bfd_seek (abfd, (file_ptr) (N_SYMOFF (*exec_hdr (abfd))), SEEK_SET)
  283. != 0)
  284. return FALSE;
  285. if (! aout_32_write_syms (abfd))
  286. return FALSE;
  287. if (bfd_seek (abfd, (file_ptr) (N_TRELOFF (*exec_hdr (abfd))), SEEK_SET)
  288. != 0)
  289. return FALSE;
  290. for (sect = abfd->sections; sect; sect = sect->next)
  291. if (sect->flags & SEC_CODE)
  292. if (!aout_32_squirt_out_relocs (abfd, sect))
  293. return FALSE;
  294. if (bfd_seek (abfd, (file_ptr) (N_DRELOFF (*exec_hdr (abfd))), SEEK_SET)
  295. != 0)
  296. return FALSE;
  297. for (sect = abfd->sections; sect; sect = sect->next)
  298. if (sect->flags & SEC_DATA)
  299. if (!aout_32_squirt_out_relocs (abfd, sect))
  300. return FALSE;
  301. }
  302. return TRUE;
  303. }
  304. static bfd_boolean
  305. aout_adobe_set_section_contents (bfd *abfd,
  306. asection *section,
  307. const void * location,
  308. file_ptr offset,
  309. bfd_size_type count)
  310. {
  311. file_ptr section_start;
  312. sec_ptr sect;
  313. /* Set by bfd.c handler. */
  314. if (! abfd->output_has_begun)
  315. {
  316. /* Assign file offsets to sections. Text sections are first, and
  317. are contiguous. Then data sections. Everything else at the end. */
  318. section_start = N_TXTOFF (ignore<-->me);
  319. for (sect = abfd->sections; sect; sect = sect->next)
  320. {
  321. if (sect->flags & SEC_CODE)
  322. {
  323. sect->filepos = section_start;
  324. /* FIXME: Round to alignment. */
  325. section_start += sect->size;
  326. }
  327. }
  328. for (sect = abfd->sections; sect; sect = sect->next)
  329. {
  330. if (sect->flags & SEC_DATA)
  331. {
  332. sect->filepos = section_start;
  333. /* FIXME: Round to alignment. */
  334. section_start += sect->size;
  335. }
  336. }
  337. for (sect = abfd->sections; sect; sect = sect->next)
  338. {
  339. if (sect->flags & SEC_HAS_CONTENTS &&
  340. !(sect->flags & (SEC_CODE | SEC_DATA)))
  341. {
  342. sect->filepos = section_start;
  343. /* FIXME: Round to alignment. */
  344. section_start += sect->size;
  345. }
  346. }
  347. }
  348. /* Regardless, once we know what we're doing, we might as well get
  349. going. */
  350. if (bfd_seek (abfd, section->filepos + offset, SEEK_SET) != 0)
  351. return FALSE;
  352. if (count == 0)
  353. return TRUE;
  354. return bfd_bwrite (location, count, abfd) == count;
  355. }
  356. static bfd_boolean
  357. aout_adobe_set_arch_mach (bfd *abfd,
  358. enum bfd_architecture arch,
  359. unsigned long machine)
  360. {
  361. if (! bfd_default_set_arch_mach (abfd, arch, machine))
  362. return FALSE;
  363. if (arch == bfd_arch_unknown
  364. || arch == bfd_arch_m68k)
  365. return TRUE;
  366. return FALSE;
  367. }
  368. static int
  369. aout_adobe_sizeof_headers (bfd *ignore_abfd ATTRIBUTE_UNUSED,
  370. struct bfd_link_info *info ATTRIBUTE_UNUSED)
  371. {
  372. return sizeof (struct internal_exec);
  373. }
  374. /* Build the transfer vector for Adobe A.Out files. */
  375. #define aout_32_find_line _bfd_nosymbols_find_line
  376. #define aout_32_get_symbol_version_string _bfd_nosymbols_get_symbol_version_string
  377. #define aout_32_bfd_make_debug_symbol _bfd_nosymbols_bfd_make_debug_symbol
  378. #define aout_32_bfd_reloc_type_lookup _bfd_norelocs_bfd_reloc_type_lookup
  379. #define aout_32_bfd_reloc_name_lookup _bfd_norelocs_bfd_reloc_name_lookup
  380. #define aout_32_close_and_cleanup aout_32_bfd_free_cached_info
  381. #define aout_32_set_arch_mach aout_adobe_set_arch_mach
  382. #define aout_32_set_section_contents aout_adobe_set_section_contents
  383. #define aout_32_sizeof_headers aout_adobe_sizeof_headers
  384. #define aout_32_bfd_get_relocated_section_contents bfd_generic_get_relocated_section_contents
  385. #define aout_32_get_section_contents_in_window _bfd_generic_get_section_contents_in_window
  386. #define aout_32_bfd_relax_section bfd_generic_relax_section
  387. #define aout_32_bfd_gc_sections bfd_generic_gc_sections
  388. #define aout_32_bfd_lookup_section_flags bfd_generic_lookup_section_flags
  389. #define aout_32_bfd_merge_sections bfd_generic_merge_sections
  390. #define aout_32_bfd_is_group_section bfd_generic_is_group_section
  391. #define aout_32_bfd_discard_group bfd_generic_discard_group
  392. #define aout_32_section_already_linked _bfd_generic_section_already_linked
  393. #define aout_32_bfd_define_common_symbol bfd_generic_define_common_symbol
  394. #define aout_32_bfd_link_hash_table_create _bfd_generic_link_hash_table_create
  395. #define aout_32_bfd_link_add_symbols _bfd_generic_link_add_symbols
  396. #define aout_32_bfd_link_just_syms _bfd_generic_link_just_syms
  397. #define aout_32_bfd_copy_link_hash_symbol_type \
  398. _bfd_generic_copy_link_hash_symbol_type
  399. #define aout_32_bfd_final_link _bfd_generic_final_link
  400. #define aout_32_bfd_link_split_section _bfd_generic_link_split_section
  401. const bfd_target aout_adobe_vec =
  402. {
  403. "a.out.adobe", /* Name. */
  404. bfd_target_aout_flavour,
  405. BFD_ENDIAN_BIG, /* Data byte order is unknown (big assumed). */
  406. BFD_ENDIAN_BIG, /* Header byte order is big. */
  407. (HAS_RELOC | EXEC_P | /* Object flags. */
  408. HAS_LINENO | HAS_DEBUG |
  409. HAS_SYMS | HAS_LOCALS | WP_TEXT ),
  410. /* section flags */
  411. (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_DATA | SEC_RELOC),
  412. '_', /* Symbol leading char. */
  413. ' ', /* AR_pad_char. */
  414. 16, /* AR_max_namelen. */
  415. 0, /* match priority. */
  416. bfd_getb64, bfd_getb_signed_64, bfd_putb64,
  417. bfd_getb32, bfd_getb_signed_32, bfd_putb32,
  418. bfd_getb16, bfd_getb_signed_16, bfd_putb16, /* Data. */
  419. bfd_getb64, bfd_getb_signed_64, bfd_putb64,
  420. bfd_getb32, bfd_getb_signed_32, bfd_putb32,
  421. bfd_getb16, bfd_getb_signed_16, bfd_putb16, /* Headers. */
  422. {_bfd_dummy_target, aout_adobe_object_p, /* bfd_check_format. */
  423. bfd_generic_archive_p, _bfd_dummy_target},
  424. {bfd_false, aout_adobe_mkobject, /* bfd_set_format. */
  425. _bfd_generic_mkarchive, bfd_false},
  426. {bfd_false, aout_adobe_write_object_contents,/* bfd_write_contents. */
  427. _bfd_write_archive_contents, bfd_false},
  428. BFD_JUMP_TABLE_GENERIC (aout_32),
  429. BFD_JUMP_TABLE_COPY (_bfd_generic),
  430. BFD_JUMP_TABLE_CORE (_bfd_nocore),
  431. BFD_JUMP_TABLE_ARCHIVE (_bfd_archive_bsd),
  432. BFD_JUMP_TABLE_SYMBOLS (aout_32),
  433. BFD_JUMP_TABLE_RELOCS (aout_32),
  434. BFD_JUMP_TABLE_WRITE (aout_32),
  435. BFD_JUMP_TABLE_LINK (aout_32),
  436. BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),
  437. NULL,
  438. NULL
  439. };