i386msdos.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. /* BFD back-end for MS-DOS executables.
  2. Copyright (C) 1990-2022 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. #include "coff/msdos.h"
  24. #define EXE_LOAD_HIGH 0x0000
  25. #define EXE_LOAD_LOW 0xffff
  26. #define EXE_PAGE_SIZE 512
  27. static bool
  28. msdos_mkobject (bfd *abfd)
  29. {
  30. bfd_default_set_arch_mach (abfd, bfd_arch_i386, bfd_mach_i386_i8086);
  31. return aout_32_mkobject (abfd);
  32. }
  33. static bfd_cleanup
  34. msdos_object_p (bfd *abfd)
  35. {
  36. struct external_DOS_hdr hdr;
  37. bfd_byte buffer[2];
  38. asection *section;
  39. bfd_size_type size;
  40. if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0
  41. || (size = bfd_bread (&hdr, sizeof (hdr), abfd)) + 1 < DOS_HDR_SIZE + 1)
  42. {
  43. if (bfd_get_error () != bfd_error_system_call)
  44. bfd_set_error (bfd_error_wrong_format);
  45. return NULL;
  46. }
  47. if (H_GET_16 (abfd, hdr.e_magic) != IMAGE_DOS_SIGNATURE)
  48. {
  49. bfd_set_error (bfd_error_wrong_format);
  50. return NULL;
  51. }
  52. /* Check that this isn't actually a PE, NE, or LE file. If it is, the
  53. e_lfanew field will be valid and point to a header beginning with one of
  54. the relevant signatures. If not, e_lfanew might point to anything, so
  55. don't bail if we can't read there. */
  56. if (size < offsetof (struct external_DOS_hdr, e_lfanew) + 4
  57. || H_GET_16 (abfd, hdr.e_cparhdr) < 4)
  58. ;
  59. else if (bfd_seek (abfd, H_GET_32 (abfd, hdr.e_lfanew), SEEK_SET) != 0
  60. || bfd_bread (buffer, (bfd_size_type) 2, abfd) != 2)
  61. {
  62. if (bfd_get_error () == bfd_error_system_call)
  63. return NULL;
  64. }
  65. else
  66. {
  67. if (H_GET_16 (abfd, buffer) == IMAGE_NT_SIGNATURE
  68. || H_GET_16 (abfd, buffer) == IMAGE_OS2_SIGNATURE
  69. || H_GET_16 (abfd, buffer) == IMAGE_OS2_SIGNATURE_LE
  70. || H_GET_16 (abfd, buffer) == IMAGE_OS2_SIGNATURE_LX)
  71. {
  72. bfd_set_error (bfd_error_wrong_format);
  73. return NULL;
  74. }
  75. }
  76. if (!msdos_mkobject (abfd))
  77. return NULL;
  78. abfd->flags = EXEC_P;
  79. abfd->start_address = H_GET_16 (abfd, hdr.e_ip);
  80. section = bfd_make_section (abfd, ".text");
  81. if (section == NULL)
  82. return NULL;
  83. section->flags = (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_HAS_CONTENTS);
  84. section->filepos = H_GET_16 (abfd, hdr.e_cparhdr) * 16;
  85. size = (H_GET_16 (abfd, hdr.e_cp) - 1) * EXE_PAGE_SIZE - section->filepos;
  86. size += H_GET_16 (abfd, hdr.e_cblp);
  87. /* Check that the size is valid. */
  88. if (bfd_seek (abfd, section->filepos + size, SEEK_SET) != 0)
  89. {
  90. if (bfd_get_error () != bfd_error_system_call)
  91. bfd_set_error (bfd_error_wrong_format);
  92. return NULL;
  93. }
  94. bfd_set_section_size (section, size);
  95. section->alignment_power = 4;
  96. return _bfd_no_cleanup;
  97. }
  98. static int
  99. msdos_sizeof_headers (bfd *abfd ATTRIBUTE_UNUSED,
  100. struct bfd_link_info *info ATTRIBUTE_UNUSED)
  101. {
  102. return 0;
  103. }
  104. static bool
  105. msdos_write_object_contents (bfd *abfd)
  106. {
  107. static char hdr[EXE_PAGE_SIZE];
  108. file_ptr outfile_size = sizeof(hdr);
  109. bfd_vma high_vma = 0;
  110. asection *sec;
  111. /* Find the total size of the program on disk and in memory. */
  112. for (sec = abfd->sections; sec != (asection *) NULL; sec = sec->next)
  113. {
  114. if (sec->size == 0)
  115. continue;
  116. if (bfd_section_flags (sec) & SEC_ALLOC)
  117. {
  118. bfd_vma sec_vma = bfd_section_vma (sec) + sec->size;
  119. if (sec_vma > high_vma)
  120. high_vma = sec_vma;
  121. }
  122. if (bfd_section_flags (sec) & SEC_LOAD)
  123. {
  124. file_ptr sec_end = (sizeof (hdr)
  125. + bfd_section_vma (sec)
  126. + sec->size);
  127. if (sec_end > outfile_size)
  128. outfile_size = sec_end;
  129. }
  130. }
  131. /* Make sure the program isn't too big. */
  132. if (high_vma > (bfd_vma)0xffff)
  133. {
  134. bfd_set_error(bfd_error_file_too_big);
  135. return false;
  136. }
  137. /* Constants. */
  138. H_PUT_16 (abfd, IMAGE_DOS_SIGNATURE, &hdr[0]);
  139. H_PUT_16 (abfd, EXE_PAGE_SIZE / 16, &hdr[8]);
  140. H_PUT_16 (abfd, EXE_LOAD_LOW, &hdr[12]);
  141. H_PUT_16 (abfd, 0x3e, &hdr[24]);
  142. H_PUT_16 (abfd, 0x0001, &hdr[28]); /* XXX??? */
  143. H_PUT_16 (abfd, 0x30fb, &hdr[30]); /* XXX??? */
  144. H_PUT_16 (abfd, 0x726a, &hdr[32]); /* XXX??? */
  145. /* Bytes in last page (0 = full page). */
  146. H_PUT_16 (abfd, outfile_size & (EXE_PAGE_SIZE - 1), &hdr[2]);
  147. /* Number of pages. */
  148. H_PUT_16 (abfd, (outfile_size + EXE_PAGE_SIZE - 1) / EXE_PAGE_SIZE, &hdr[4]);
  149. /* Set the initial stack pointer to the end of the bss.
  150. The program's crt0 code must relocate it to a real stack. */
  151. H_PUT_16 (abfd, high_vma, &hdr[16]);
  152. if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0
  153. || bfd_bwrite (hdr, (bfd_size_type) sizeof(hdr), abfd) != sizeof(hdr))
  154. return false;
  155. return true;
  156. }
  157. static bool
  158. msdos_set_section_contents (bfd *abfd,
  159. sec_ptr section,
  160. const void *location,
  161. file_ptr offset,
  162. bfd_size_type count)
  163. {
  164. if (count == 0)
  165. return true;
  166. section->filepos = EXE_PAGE_SIZE + bfd_section_vma (section);
  167. if (bfd_section_flags (section) & SEC_LOAD)
  168. {
  169. if (bfd_seek (abfd, section->filepos + offset, SEEK_SET) != 0
  170. || bfd_bwrite (location, count, abfd) != count)
  171. return false;
  172. }
  173. return true;
  174. }
  175. #define msdos_make_empty_symbol aout_32_make_empty_symbol
  176. #define msdos_bfd_reloc_type_lookup aout_32_reloc_type_lookup
  177. #define msdos_bfd_reloc_name_lookup aout_32_reloc_name_lookup
  178. #define msdos_close_and_cleanup _bfd_generic_close_and_cleanup
  179. #define msdos_bfd_free_cached_info _bfd_generic_bfd_free_cached_info
  180. #define msdos_new_section_hook _bfd_generic_new_section_hook
  181. #define msdos_get_section_contents _bfd_generic_get_section_contents
  182. #define msdos_get_section_contents_in_window \
  183. _bfd_generic_get_section_contents_in_window
  184. #define msdos_bfd_get_relocated_section_contents \
  185. bfd_generic_get_relocated_section_contents
  186. #define msdos_bfd_relax_section bfd_generic_relax_section
  187. #define msdos_bfd_gc_sections bfd_generic_gc_sections
  188. #define msdos_bfd_lookup_section_flags bfd_generic_lookup_section_flags
  189. #define msdos_bfd_merge_sections bfd_generic_merge_sections
  190. #define msdos_bfd_is_group_section bfd_generic_is_group_section
  191. #define msdos_bfd_group_name bfd_generic_group_name
  192. #define msdos_bfd_discard_group bfd_generic_discard_group
  193. #define msdos_section_already_linked \
  194. _bfd_generic_section_already_linked
  195. #define msdos_bfd_define_common_symbol bfd_generic_define_common_symbol
  196. #define msdos_bfd_link_hide_symbol _bfd_generic_link_hide_symbol
  197. #define msdos_bfd_define_start_stop bfd_generic_define_start_stop
  198. #define msdos_bfd_link_hash_table_create _bfd_generic_link_hash_table_create
  199. #define msdos_bfd_link_add_symbols _bfd_generic_link_add_symbols
  200. #define msdos_bfd_link_just_syms _bfd_generic_link_just_syms
  201. #define msdos_bfd_copy_link_hash_symbol_type \
  202. _bfd_generic_copy_link_hash_symbol_type
  203. #define msdos_bfd_final_link _bfd_generic_final_link
  204. #define msdos_bfd_link_split_section _bfd_generic_link_split_section
  205. #define msdos_set_arch_mach _bfd_generic_set_arch_mach
  206. #define msdos_bfd_link_check_relocs _bfd_generic_link_check_relocs
  207. #define msdos_get_symtab_upper_bound _bfd_nosymbols_get_symtab_upper_bound
  208. #define msdos_canonicalize_symtab _bfd_nosymbols_canonicalize_symtab
  209. #define msdos_print_symbol _bfd_nosymbols_print_symbol
  210. #define msdos_get_symbol_info _bfd_nosymbols_get_symbol_info
  211. #define msdos_get_symbol_version_string \
  212. _bfd_nosymbols_get_symbol_version_string
  213. #define msdos_find_nearest_line _bfd_nosymbols_find_nearest_line
  214. #define msdos_find_line _bfd_nosymbols_find_line
  215. #define msdos_find_inliner_info _bfd_nosymbols_find_inliner_info
  216. #define msdos_get_lineno _bfd_nosymbols_get_lineno
  217. #define msdos_bfd_is_target_special_symbol _bfd_bool_bfd_asymbol_false
  218. #define msdos_bfd_is_local_label_name _bfd_nosymbols_bfd_is_local_label_name
  219. #define msdos_bfd_make_debug_symbol _bfd_nosymbols_bfd_make_debug_symbol
  220. #define msdos_read_minisymbols _bfd_nosymbols_read_minisymbols
  221. #define msdos_minisymbol_to_symbol _bfd_nosymbols_minisymbol_to_symbol
  222. #define msdos_canonicalize_reloc _bfd_norelocs_canonicalize_reloc
  223. #define msdos_set_reloc _bfd_norelocs_set_reloc
  224. #define msdos_get_reloc_upper_bound _bfd_norelocs_get_reloc_upper_bound
  225. #define msdos_32_bfd_link_split_section _bfd_generic_link_split_section
  226. const bfd_target i386_msdos_vec =
  227. {
  228. "msdos", /* name */
  229. bfd_target_msdos_flavour,
  230. BFD_ENDIAN_LITTLE, /* target byte order */
  231. BFD_ENDIAN_LITTLE, /* target headers byte order */
  232. (EXEC_P), /* object flags */
  233. (SEC_CODE | SEC_DATA | SEC_HAS_CONTENTS
  234. | SEC_ALLOC | SEC_LOAD), /* section flags */
  235. 0, /* leading underscore */
  236. ' ', /* ar_pad_char */
  237. 16, /* ar_max_namelen */
  238. 0, /* match priority. */
  239. TARGET_KEEP_UNUSED_SECTION_SYMBOLS, /* keep unused section symbols. */
  240. bfd_getl64, bfd_getl_signed_64, bfd_putl64,
  241. bfd_getl32, bfd_getl_signed_32, bfd_putl32,
  242. bfd_getl16, bfd_getl_signed_16, bfd_putl16, /* data */
  243. bfd_getl64, bfd_getl_signed_64, bfd_putl64,
  244. bfd_getl32, bfd_getl_signed_32, bfd_putl32,
  245. bfd_getl16, bfd_getl_signed_16, bfd_putl16, /* hdrs */
  246. {
  247. _bfd_dummy_target,
  248. msdos_object_p, /* bfd_check_format */
  249. _bfd_dummy_target,
  250. _bfd_dummy_target,
  251. },
  252. {
  253. _bfd_bool_bfd_false_error,
  254. msdos_mkobject,
  255. _bfd_generic_mkarchive,
  256. _bfd_bool_bfd_false_error,
  257. },
  258. { /* bfd_write_contents */
  259. _bfd_bool_bfd_false_error,
  260. msdos_write_object_contents,
  261. _bfd_write_archive_contents,
  262. _bfd_bool_bfd_false_error,
  263. },
  264. BFD_JUMP_TABLE_GENERIC (msdos),
  265. BFD_JUMP_TABLE_COPY (_bfd_generic),
  266. BFD_JUMP_TABLE_CORE (_bfd_nocore),
  267. BFD_JUMP_TABLE_ARCHIVE (_bfd_noarchive),
  268. BFD_JUMP_TABLE_SYMBOLS (msdos),
  269. BFD_JUMP_TABLE_RELOCS (msdos),
  270. BFD_JUMP_TABLE_WRITE (msdos),
  271. BFD_JUMP_TABLE_LINK (msdos),
  272. BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),
  273. NULL,
  274. NULL
  275. };