coff-stgo32.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. /* BFD back-end for Intel 386 COFF files (DJGPP variant with a stub).
  2. Copyright (C) 1997-2022 Free Software Foundation, Inc.
  3. Written by Robert Hoehne.
  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 handles now also stubbed coff images. The stub is a small
  18. DOS executable program before the coff image to load it in memory
  19. and execute it. This is needed, because DOS cannot run coff files.
  20. The COFF image is loaded in memory without the stub attached, so
  21. all offsets are relative to the beginning of the image, not the
  22. actual file. We handle this in bfd by setting bfd->origin to where
  23. the COFF image starts. */
  24. #define TARGET_SYM i386_coff_go32stubbed_vec
  25. #define TARGET_NAME "coff-go32-exe"
  26. #define TARGET_UNDERSCORE '_'
  27. #define COFF_GO32_EXE
  28. #define COFF_LONG_SECTION_NAMES
  29. #define COFF_SUPPORT_GNU_LINKONCE
  30. #define COFF_LONG_FILENAMES
  31. #define COFF_SECTION_ALIGNMENT_ENTRIES \
  32. { COFF_SECTION_NAME_EXACT_MATCH (".data"), \
  33. COFF_ALIGNMENT_FIELD_EMPTY, COFF_ALIGNMENT_FIELD_EMPTY, 4 }, \
  34. { COFF_SECTION_NAME_EXACT_MATCH (".text"), \
  35. COFF_ALIGNMENT_FIELD_EMPTY, COFF_ALIGNMENT_FIELD_EMPTY, 4 }, \
  36. { COFF_SECTION_NAME_PARTIAL_MATCH (".debug"), \
  37. COFF_ALIGNMENT_FIELD_EMPTY, COFF_ALIGNMENT_FIELD_EMPTY, 0 }, \
  38. { COFF_SECTION_NAME_PARTIAL_MATCH (".gnu.linkonce.wi"), \
  39. COFF_ALIGNMENT_FIELD_EMPTY, COFF_ALIGNMENT_FIELD_EMPTY, 0 }
  40. /* Section contains extended relocations. */
  41. #define IMAGE_SCN_LNK_NRELOC_OVFL (0x01000000)
  42. #include "sysdep.h"
  43. #include "bfd.h"
  44. #include "coff/msdos.h"
  45. static bfd_cleanup go32exe_check_format (bfd *);
  46. static bool go32exe_write_object_contents (bfd *);
  47. static bool go32exe_mkobject (bfd *);
  48. static bool go32exe_copy_private_bfd_data (bfd *, bfd *);
  49. /* Defined in coff-go32.c. */
  50. bool _bfd_go32_mkobject (bfd *);
  51. void _bfd_go32_swap_scnhdr_in (bfd *, void *, void *);
  52. unsigned int _bfd_go32_swap_scnhdr_out (bfd *, void *, void *);
  53. #define COFF_CHECK_FORMAT go32exe_check_format
  54. #define COFF_WRITE_CONTENTS go32exe_write_object_contents
  55. #define coff_mkobject go32exe_mkobject
  56. #define coff_bfd_copy_private_bfd_data go32exe_copy_private_bfd_data
  57. #define coff_SWAP_scnhdr_in _bfd_go32_swap_scnhdr_in
  58. #define coff_SWAP_scnhdr_out _bfd_go32_swap_scnhdr_out
  59. #include "coff-i386.c"
  60. /* This macro is used, because I cannot assume the endianness of the
  61. host system. */
  62. #define _H(index) (H_GET_16 (abfd, (header + index * 2)))
  63. /* These bytes are a 2048-byte DOS executable, which loads the COFF
  64. image into memory and then runs it. It is called 'stub'. */
  65. #define GO32EXE_DEFAULT_STUB_SIZE 2048
  66. static const unsigned char go32exe_default_stub[GO32EXE_DEFAULT_STUB_SIZE] =
  67. {
  68. #include "go32stub.h"
  69. };
  70. /* Temporary location for stub read from input file. */
  71. static char * go32exe_temp_stub = NULL;
  72. static bfd_size_type go32exe_temp_stub_size = 0;
  73. /* That's the function, which creates the stub. There are
  74. different cases from where the stub is taken.
  75. At first the environment variable $(GO32STUB) is checked and then
  76. $(STUB) if it was not set.
  77. If it exists and points to a valid stub the stub is taken from
  78. that file. This file can be also a whole executable file, because
  79. the stub is computed from the exe information at the start of that
  80. file.
  81. If there was any error, the standard stub (compiled in this file)
  82. is taken.
  83. Ideally this function should exec '$(TARGET)-stubify' to generate
  84. a stub, like gcc does. */
  85. static void
  86. go32exe_create_stub (bfd *abfd)
  87. {
  88. /* Do it only once. */
  89. if (coff_data (abfd)->stub == NULL)
  90. {
  91. char *stub;
  92. struct stat st;
  93. int f;
  94. unsigned char header[10];
  95. char magic[8];
  96. unsigned long coff_start;
  97. long exe_start;
  98. /* If we read a stub from an input file, use that one. */
  99. if (go32exe_temp_stub != NULL)
  100. {
  101. coff_data (abfd)->stub = bfd_alloc (abfd,
  102. go32exe_temp_stub_size);
  103. if (coff_data (abfd)->stub == NULL)
  104. return;
  105. memcpy (coff_data (abfd)->stub, go32exe_temp_stub,
  106. go32exe_temp_stub_size);
  107. coff_data (abfd)->stub_size = go32exe_temp_stub_size;
  108. free (go32exe_temp_stub);
  109. go32exe_temp_stub = NULL;
  110. go32exe_temp_stub_size = 0;
  111. return;
  112. }
  113. /* Check at first the environment variable $(GO32STUB). */
  114. stub = getenv ("GO32STUB");
  115. /* Now check the environment variable $(STUB). */
  116. if (stub == NULL)
  117. stub = getenv ("STUB");
  118. if (stub == NULL)
  119. goto stub_end;
  120. if (stat (stub, &st) != 0)
  121. goto stub_end;
  122. #ifdef O_BINARY
  123. f = open (stub, O_RDONLY | O_BINARY);
  124. #else
  125. f = open (stub, O_RDONLY);
  126. #endif
  127. if (f < 0)
  128. goto stub_end;
  129. if (read (f, &header, sizeof (header)) < 0)
  130. {
  131. close (f);
  132. goto stub_end;
  133. }
  134. if (_H (0) != 0x5a4d) /* It is not an exe file. */
  135. {
  136. close (f);
  137. goto stub_end;
  138. }
  139. /* Compute the size of the stub (it is every thing up
  140. to the beginning of the coff image). */
  141. coff_start = (long) _H (2) * 512L;
  142. if (_H (1))
  143. coff_start += (long) _H (1) - 512L;
  144. exe_start = _H (4) * 16;
  145. if ((long) lseek (f, exe_start, SEEK_SET) != exe_start)
  146. {
  147. close (f);
  148. goto stub_end;
  149. }
  150. if (read (f, &magic, 8) != 8)
  151. {
  152. close (f);
  153. goto stub_end;
  154. }
  155. if (! startswith (magic, "go32stub"))
  156. {
  157. close (f);
  158. goto stub_end;
  159. }
  160. /* Now we found a correct stub (hopefully). */
  161. coff_data (abfd)->stub = bfd_alloc (abfd, (bfd_size_type) coff_start);
  162. if (coff_data (abfd)->stub == NULL)
  163. {
  164. close (f);
  165. return;
  166. }
  167. lseek (f, 0L, SEEK_SET);
  168. if ((unsigned long) read (f, coff_data (abfd)->stub, coff_start)
  169. != coff_start)
  170. {
  171. bfd_release (abfd, coff_data (abfd)->stub);
  172. coff_data (abfd)->stub = NULL;
  173. }
  174. else
  175. coff_data (abfd)->stub_size = coff_start;
  176. close (f);
  177. }
  178. stub_end:
  179. /* There was something wrong above, so use now the standard builtin
  180. stub. */
  181. if (coff_data (abfd)->stub == NULL)
  182. {
  183. coff_data (abfd)->stub
  184. = bfd_alloc (abfd, (bfd_size_type) GO32EXE_DEFAULT_STUB_SIZE);
  185. if (coff_data (abfd)->stub == NULL)
  186. return;
  187. memcpy (coff_data (abfd)->stub, go32exe_default_stub,
  188. GO32EXE_DEFAULT_STUB_SIZE);
  189. coff_data (abfd)->stub_size = GO32EXE_DEFAULT_STUB_SIZE;
  190. }
  191. }
  192. /* If ibfd was a stubbed coff image, copy the stub from that bfd
  193. to the new obfd. */
  194. static bool
  195. go32exe_copy_private_bfd_data (bfd *ibfd, bfd *obfd)
  196. {
  197. /* Check if both are the same targets. */
  198. if (ibfd->xvec != obfd->xvec)
  199. return true;
  200. /* Make sure we have a source stub. */
  201. BFD_ASSERT (coff_data (ibfd)->stub != NULL);
  202. /* Reallocate the output stub if necessary. */
  203. if (coff_data (ibfd)->stub_size > coff_data (obfd)->stub_size)
  204. coff_data (obfd)->stub = bfd_alloc (obfd, coff_data (ibfd)->stub_size);
  205. if (coff_data (obfd)->stub == NULL)
  206. return false;
  207. /* Now copy the stub. */
  208. memcpy (coff_data (obfd)->stub, coff_data (ibfd)->stub,
  209. coff_data (ibfd)->stub_size);
  210. coff_data (obfd)->stub_size = coff_data (ibfd)->stub_size;
  211. obfd->origin = coff_data (obfd)->stub_size;
  212. return true;
  213. }
  214. /* Cleanup function, returned from check_format hook. */
  215. static void
  216. go32exe_cleanup (bfd *abfd)
  217. {
  218. abfd->origin = 0;
  219. free (go32exe_temp_stub);
  220. go32exe_temp_stub = NULL;
  221. go32exe_temp_stub_size = 0;
  222. }
  223. /* Check that there is a GO32 stub and read it to go32exe_temp_stub.
  224. Then set abfd->origin so that the COFF image is read at the correct
  225. file offset. */
  226. static bfd_cleanup
  227. go32exe_check_format (bfd *abfd)
  228. {
  229. struct external_DOS_hdr filehdr_dos;
  230. uint16_t num_pages;
  231. uint16_t last_page_size;
  232. uint32_t header_end;
  233. bfd_size_type stubsize;
  234. /* This format can not appear in an archive. */
  235. if (abfd->origin != 0)
  236. {
  237. bfd_set_error (bfd_error_wrong_format);
  238. return NULL;
  239. }
  240. bfd_set_error (bfd_error_system_call);
  241. /* Read in the stub file header, which is a DOS MZ executable. */
  242. if (bfd_bread (&filehdr_dos, DOS_HDR_SIZE, abfd) != DOS_HDR_SIZE)
  243. goto fail;
  244. /* Make sure that this is an MZ executable. */
  245. if (H_GET_16 (abfd, filehdr_dos.e_magic) != IMAGE_DOS_SIGNATURE)
  246. goto fail_format;
  247. /* Determine the size of the stub */
  248. num_pages = H_GET_16 (abfd, filehdr_dos.e_cp);
  249. last_page_size = H_GET_16 (abfd, filehdr_dos.e_cblp);
  250. stubsize = num_pages * 512;
  251. if (last_page_size != 0)
  252. stubsize += last_page_size - 512;
  253. /* Save now the stub to be used later. Put the stub data to a temporary
  254. location first as tdata still does not exist. It may not even
  255. be ever created if we are just checking the file format of ABFD. */
  256. bfd_seek (abfd, 0, SEEK_SET);
  257. go32exe_temp_stub = bfd_malloc (stubsize);
  258. if (go32exe_temp_stub == NULL)
  259. goto fail;
  260. if (bfd_bread (go32exe_temp_stub, stubsize, abfd) != stubsize)
  261. goto fail;
  262. go32exe_temp_stub_size = stubsize;
  263. /* Confirm that this is a go32stub. */
  264. header_end = H_GET_16 (abfd, filehdr_dos.e_cparhdr) * 16UL;
  265. if (go32exe_temp_stub_size < header_end
  266. || go32exe_temp_stub_size - header_end < sizeof "go32stub" - 1
  267. || !startswith (go32exe_temp_stub + header_end, "go32stub"))
  268. goto fail_format;
  269. /* Set origin to where the COFF header starts and seek there. */
  270. abfd->origin = stubsize;
  271. if (bfd_seek (abfd, 0, SEEK_SET) != 0)
  272. goto fail;
  273. /* Call coff_object_p to read the COFF image. If this fails then the file
  274. must be just a stub with no COFF data attached. */
  275. bfd_cleanup cleanup = coff_object_p (abfd);
  276. if (cleanup == NULL)
  277. goto fail;
  278. BFD_ASSERT (cleanup == _bfd_no_cleanup);
  279. return go32exe_cleanup;
  280. fail_format:
  281. bfd_set_error (bfd_error_wrong_format);
  282. fail:
  283. go32exe_cleanup (abfd);
  284. return NULL;
  285. }
  286. /* Write the stub to the output file, then call coff_write_object_contents. */
  287. static bool
  288. go32exe_write_object_contents (bfd *abfd)
  289. {
  290. const bfd_size_type pos = bfd_tell (abfd);
  291. const bfd_size_type stubsize = coff_data (abfd)->stub_size;
  292. BFD_ASSERT (stubsize != 0);
  293. bfd_set_error (bfd_error_system_call);
  294. /* Write the stub. */
  295. abfd->origin = 0;
  296. if (bfd_seek (abfd, 0, SEEK_SET) != 0)
  297. return false;
  298. if (bfd_bwrite (coff_data (abfd)->stub, stubsize, abfd) != stubsize)
  299. return false;
  300. /* Seek back to where we were. */
  301. abfd->origin = stubsize;
  302. if (bfd_seek (abfd, pos, SEEK_SET) != 0)
  303. return false;
  304. return coff_write_object_contents (abfd);
  305. }
  306. /* mkobject hook. Called directly through bfd_set_format or via
  307. coff_mkobject_hook etc from bfd_check_format. */
  308. static bool
  309. go32exe_mkobject (bfd *abfd)
  310. {
  311. /* Don't output to an archive. */
  312. if (abfd->my_archive != NULL)
  313. return false;
  314. if (!_bfd_go32_mkobject (abfd))
  315. return false;
  316. go32exe_create_stub (abfd);
  317. if (coff_data (abfd)->stub == NULL)
  318. {
  319. bfd_release (abfd, coff_data (abfd));
  320. return false;
  321. }
  322. abfd->origin = coff_data (abfd)->stub_size;
  323. return true;
  324. }