i386msdos.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. /* BFD back-end for MS-DOS executables.
  2. Copyright (C) 1990-2015 Free Software Foundation, Inc.
  3. Written by Bryan Ford of the University of Utah.
  4. Contributed by the Center for Software Science at the
  5. University of Utah (pa-gdb-bugs@cs.utah.edu).
  6. This file is part of BFD, the Binary File Descriptor library.
  7. This program is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 3 of the License, or
  10. (at your option) any later version.
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. GNU General Public License for more details.
  15. You should have received a copy of the GNU General Public License
  16. along with this program; if not, write to the Free Software
  17. Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
  18. MA 02110-1301, USA. */
  19. #include "sysdep.h"
  20. #include "bfd.h"
  21. #include "libbfd.h"
  22. #include "libaout.h"
  23. #define EXE_MAGIC 0x5a4d
  24. #define EXE_LOAD_HIGH 0x0000
  25. #define EXE_LOAD_LOW 0xffff
  26. #define EXE_PAGE_SIZE 512
  27. static int
  28. msdos_sizeof_headers (bfd *abfd ATTRIBUTE_UNUSED,
  29. struct bfd_link_info *info ATTRIBUTE_UNUSED)
  30. {
  31. return 0;
  32. }
  33. static bfd_boolean
  34. msdos_write_object_contents (bfd *abfd)
  35. {
  36. static char hdr[EXE_PAGE_SIZE];
  37. file_ptr outfile_size = sizeof(hdr);
  38. bfd_vma high_vma = 0;
  39. asection *sec;
  40. /* Find the total size of the program on disk and in memory. */
  41. for (sec = abfd->sections; sec != (asection *) NULL; sec = sec->next)
  42. {
  43. if (sec->size == 0)
  44. continue;
  45. if (bfd_get_section_flags (abfd, sec) & SEC_ALLOC)
  46. {
  47. bfd_vma sec_vma = bfd_get_section_vma (abfd, sec) + sec->size;
  48. if (sec_vma > high_vma)
  49. high_vma = sec_vma;
  50. }
  51. if (bfd_get_section_flags (abfd, sec) & SEC_LOAD)
  52. {
  53. file_ptr sec_end = (sizeof (hdr)
  54. + bfd_get_section_vma (abfd, sec)
  55. + sec->size);
  56. if (sec_end > outfile_size)
  57. outfile_size = sec_end;
  58. }
  59. }
  60. /* Make sure the program isn't too big. */
  61. if (high_vma > (bfd_vma)0xffff)
  62. {
  63. bfd_set_error(bfd_error_file_too_big);
  64. return FALSE;
  65. }
  66. /* Constants. */
  67. H_PUT_16 (abfd, EXE_MAGIC, &hdr[0]);
  68. H_PUT_16 (abfd, EXE_PAGE_SIZE / 16, &hdr[8]);
  69. H_PUT_16 (abfd, EXE_LOAD_LOW, &hdr[12]);
  70. H_PUT_16 (abfd, 0x3e, &hdr[24]);
  71. H_PUT_16 (abfd, 0x0001, &hdr[28]); /* XXX??? */
  72. H_PUT_16 (abfd, 0x30fb, &hdr[30]); /* XXX??? */
  73. H_PUT_16 (abfd, 0x726a, &hdr[32]); /* XXX??? */
  74. /* Bytes in last page (0 = full page). */
  75. H_PUT_16 (abfd, outfile_size & (EXE_PAGE_SIZE - 1), &hdr[2]);
  76. /* Number of pages. */
  77. H_PUT_16 (abfd, (outfile_size + EXE_PAGE_SIZE - 1) / EXE_PAGE_SIZE, &hdr[4]);
  78. /* Set the initial stack pointer to the end of the bss.
  79. The program's crt0 code must relocate it to a real stack. */
  80. H_PUT_16 (abfd, high_vma, &hdr[16]);
  81. if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0
  82. || bfd_bwrite (hdr, (bfd_size_type) sizeof(hdr), abfd) != sizeof(hdr))
  83. return FALSE;
  84. return TRUE;
  85. }
  86. static bfd_boolean
  87. msdos_set_section_contents (bfd *abfd,
  88. sec_ptr section,
  89. const void *location,
  90. file_ptr offset,
  91. bfd_size_type count)
  92. {
  93. if (count == 0)
  94. return TRUE;
  95. section->filepos = EXE_PAGE_SIZE + bfd_get_section_vma (abfd, section);
  96. if (bfd_get_section_flags (abfd, section) & SEC_LOAD)
  97. {
  98. if (bfd_seek (abfd, section->filepos + offset, SEEK_SET) != 0
  99. || bfd_bwrite (location, count, abfd) != count)
  100. return FALSE;
  101. }
  102. return TRUE;
  103. }
  104. #define msdos_mkobject aout_32_mkobject
  105. #define msdos_make_empty_symbol aout_32_make_empty_symbol
  106. #define msdos_bfd_reloc_type_lookup aout_32_reloc_type_lookup
  107. #define msdos_bfd_reloc_name_lookup aout_32_reloc_name_lookup
  108. #define msdos_close_and_cleanup _bfd_generic_close_and_cleanup
  109. #define msdos_bfd_free_cached_info _bfd_generic_bfd_free_cached_info
  110. #define msdos_new_section_hook _bfd_generic_new_section_hook
  111. #define msdos_get_section_contents _bfd_generic_get_section_contents
  112. #define msdos_get_section_contents_in_window \
  113. _bfd_generic_get_section_contents_in_window
  114. #define msdos_bfd_get_relocated_section_contents \
  115. bfd_generic_get_relocated_section_contents
  116. #define msdos_bfd_relax_section bfd_generic_relax_section
  117. #define msdos_bfd_gc_sections bfd_generic_gc_sections
  118. #define msdos_bfd_lookup_section_flags bfd_generic_lookup_section_flags
  119. #define msdos_bfd_merge_sections bfd_generic_merge_sections
  120. #define msdos_bfd_is_group_section bfd_generic_is_group_section
  121. #define msdos_bfd_discard_group bfd_generic_discard_group
  122. #define msdos_section_already_linked \
  123. _bfd_generic_section_already_linked
  124. #define msdos_bfd_define_common_symbol bfd_generic_define_common_symbol
  125. #define msdos_bfd_link_hash_table_create _bfd_generic_link_hash_table_create
  126. #define msdos_bfd_link_add_symbols _bfd_generic_link_add_symbols
  127. #define msdos_bfd_link_just_syms _bfd_generic_link_just_syms
  128. #define msdos_bfd_copy_link_hash_symbol_type \
  129. _bfd_generic_copy_link_hash_symbol_type
  130. #define msdos_bfd_final_link _bfd_generic_final_link
  131. #define msdos_bfd_link_split_section _bfd_generic_link_split_section
  132. #define msdos_set_arch_mach _bfd_generic_set_arch_mach
  133. #define msdos_get_symtab_upper_bound _bfd_nosymbols_get_symtab_upper_bound
  134. #define msdos_canonicalize_symtab _bfd_nosymbols_canonicalize_symtab
  135. #define msdos_print_symbol _bfd_nosymbols_print_symbol
  136. #define msdos_get_symbol_info _bfd_nosymbols_get_symbol_info
  137. #define msdos_get_symbol_version_string \
  138. _bfd_nosymbols_get_symbol_version_string
  139. #define msdos_find_nearest_line _bfd_nosymbols_find_nearest_line
  140. #define msdos_find_line _bfd_nosymbols_find_line
  141. #define msdos_find_inliner_info _bfd_nosymbols_find_inliner_info
  142. #define msdos_get_lineno _bfd_nosymbols_get_lineno
  143. #define msdos_bfd_is_target_special_symbol ((bfd_boolean (*) (bfd *, asymbol *)) bfd_false)
  144. #define msdos_bfd_is_local_label_name _bfd_nosymbols_bfd_is_local_label_name
  145. #define msdos_bfd_make_debug_symbol _bfd_nosymbols_bfd_make_debug_symbol
  146. #define msdos_read_minisymbols _bfd_nosymbols_read_minisymbols
  147. #define msdos_minisymbol_to_symbol _bfd_nosymbols_minisymbol_to_symbol
  148. #define msdos_canonicalize_reloc _bfd_norelocs_canonicalize_reloc
  149. #define msdos_get_reloc_upper_bound _bfd_norelocs_get_reloc_upper_bound
  150. #define msdos_32_bfd_link_split_section _bfd_generic_link_split_section
  151. const bfd_target i386_msdos_vec =
  152. {
  153. "msdos", /* name */
  154. bfd_target_msdos_flavour,
  155. BFD_ENDIAN_LITTLE, /* target byte order */
  156. BFD_ENDIAN_LITTLE, /* target headers byte order */
  157. (EXEC_P), /* object flags */
  158. (SEC_CODE | SEC_DATA | SEC_HAS_CONTENTS
  159. | SEC_ALLOC | SEC_LOAD), /* section flags */
  160. 0, /* leading underscore */
  161. ' ', /* ar_pad_char */
  162. 16, /* ar_max_namelen */
  163. 0, /* match priority. */
  164. bfd_getl64, bfd_getl_signed_64, bfd_putl64,
  165. bfd_getl32, bfd_getl_signed_32, bfd_putl32,
  166. bfd_getl16, bfd_getl_signed_16, bfd_putl16, /* data */
  167. bfd_getl64, bfd_getl_signed_64, bfd_putl64,
  168. bfd_getl32, bfd_getl_signed_32, bfd_putl32,
  169. bfd_getl16, bfd_getl_signed_16, bfd_putl16, /* hdrs */
  170. {
  171. _bfd_dummy_target,
  172. _bfd_dummy_target, /* bfd_check_format */
  173. _bfd_dummy_target,
  174. _bfd_dummy_target,
  175. },
  176. {
  177. bfd_false,
  178. msdos_mkobject,
  179. _bfd_generic_mkarchive,
  180. bfd_false,
  181. },
  182. { /* bfd_write_contents */
  183. bfd_false,
  184. msdos_write_object_contents,
  185. _bfd_write_archive_contents,
  186. bfd_false,
  187. },
  188. BFD_JUMP_TABLE_GENERIC (msdos),
  189. BFD_JUMP_TABLE_COPY (_bfd_generic),
  190. BFD_JUMP_TABLE_CORE (_bfd_nocore),
  191. BFD_JUMP_TABLE_ARCHIVE (_bfd_noarchive),
  192. BFD_JUMP_TABLE_SYMBOLS (msdos),
  193. BFD_JUMP_TABLE_RELOCS (msdos),
  194. BFD_JUMP_TABLE_WRITE (msdos),
  195. BFD_JUMP_TABLE_LINK (msdos),
  196. BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),
  197. NULL,
  198. NULL
  199. };