coff-stgo32.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. /* BFD back-end for Intel 386 COFF files (DJGPP variant with a stub).
  2. Copyright (C) 1997-2015 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. All the functions below are called by the corresponding functions
  21. from coffswap.h.
  22. The only thing what they do is to adjust the information stored in
  23. the COFF file which are offset into the file.
  24. This is needed, because DJGPP uses a very special way to load and run
  25. the coff image. It loads the image in memory and assumes then, that the
  26. image had no stub by using the filepointers as pointers in the coff
  27. image and NOT in the file.
  28. To be compatible with any existing executables I have fixed this
  29. here and NOT in the DJGPP startup code. */
  30. #define TARGET_SYM i386_coff_go32stubbed_vec
  31. #define TARGET_NAME "coff-go32-exe"
  32. #define TARGET_UNDERSCORE '_'
  33. #define COFF_GO32_EXE
  34. #define COFF_LONG_SECTION_NAMES
  35. #define COFF_SUPPORT_GNU_LINKONCE
  36. #define COFF_LONG_FILENAMES
  37. #define COFF_SECTION_ALIGNMENT_ENTRIES \
  38. { COFF_SECTION_NAME_EXACT_MATCH (".data"), \
  39. COFF_ALIGNMENT_FIELD_EMPTY, COFF_ALIGNMENT_FIELD_EMPTY, 4 }, \
  40. { COFF_SECTION_NAME_EXACT_MATCH (".text"), \
  41. COFF_ALIGNMENT_FIELD_EMPTY, COFF_ALIGNMENT_FIELD_EMPTY, 4 }, \
  42. { COFF_SECTION_NAME_PARTIAL_MATCH (".debug"), \
  43. COFF_ALIGNMENT_FIELD_EMPTY, COFF_ALIGNMENT_FIELD_EMPTY, 0 }, \
  44. { COFF_SECTION_NAME_PARTIAL_MATCH (".gnu.linkonce.wi"), \
  45. COFF_ALIGNMENT_FIELD_EMPTY, COFF_ALIGNMENT_FIELD_EMPTY, 0 }
  46. #include "sysdep.h"
  47. #include "bfd.h"
  48. /* All that ..._PRE and ...POST functions are called from the corresponding
  49. coff_swap... functions. The ...PRE functions are called at the beginning
  50. of the function and the ...POST functions at the end of the swap routines. */
  51. static void
  52. adjust_filehdr_in_post (bfd *, void *, void *);
  53. static void
  54. adjust_filehdr_out_pre (bfd *, void *, void *);
  55. static void
  56. adjust_filehdr_out_post (bfd *, void *, void *);
  57. static void
  58. adjust_scnhdr_in_post (bfd *, void *, void *);
  59. static void
  60. adjust_scnhdr_out_pre (bfd *, void *, void *);
  61. static void
  62. adjust_scnhdr_out_post (bfd *, void *, void *);
  63. static void
  64. adjust_aux_in_post (bfd *, void *, int, int, int, int, void *);
  65. static void
  66. adjust_aux_out_pre (bfd *, void *, int, int, int, int, void *);
  67. static void
  68. adjust_aux_out_post (bfd *, void *, int, int, int, int, void *);
  69. static void
  70. create_go32_stub (bfd *);
  71. #define COFF_ADJUST_FILEHDR_IN_POST adjust_filehdr_in_post
  72. #define COFF_ADJUST_FILEHDR_OUT_PRE adjust_filehdr_out_pre
  73. #define COFF_ADJUST_FILEHDR_OUT_POST adjust_filehdr_out_post
  74. #define COFF_ADJUST_SCNHDR_IN_POST adjust_scnhdr_in_post
  75. #define COFF_ADJUST_SCNHDR_OUT_PRE adjust_scnhdr_out_pre
  76. #define COFF_ADJUST_SCNHDR_OUT_POST adjust_scnhdr_out_post
  77. #define COFF_ADJUST_AUX_IN_POST adjust_aux_in_post
  78. #define COFF_ADJUST_AUX_OUT_PRE adjust_aux_out_pre
  79. #define COFF_ADJUST_AUX_OUT_POST adjust_aux_out_post
  80. static const bfd_target *go32_check_format (bfd *);
  81. #define COFF_CHECK_FORMAT go32_check_format
  82. static bfd_boolean
  83. go32_stubbed_coff_bfd_copy_private_bfd_data (bfd *, bfd *);
  84. #define coff_bfd_copy_private_bfd_data go32_stubbed_coff_bfd_copy_private_bfd_data
  85. #include "coff-i386.c"
  86. /* This macro is used, because I cannot assume the endianness of the
  87. host system. */
  88. #define _H(index) (H_GET_16 (abfd, (header + index * 2)))
  89. /* These bytes are a 2048-byte DOS executable, which loads the COFF
  90. image into memory and then runs it. It is called 'stub'. */
  91. static const unsigned char stub_bytes[GO32_STUBSIZE] =
  92. {
  93. #include "go32stub.h"
  94. };
  95. /*
  96. I have not commented each swap function below, because the
  97. technique is in any function the same. For the ...in function,
  98. all the pointers are adjusted by adding GO32_STUBSIZE and for the
  99. ...out function, it is subtracted first and after calling the
  100. standard swap function it is reset to the old value. */
  101. /* This macro is used for adjusting the filepointers, which
  102. is done only, if the pointer is nonzero. */
  103. #define ADJUST_VAL(val,diff) \
  104. if (val != 0) val += diff
  105. static void
  106. adjust_filehdr_in_post (bfd * abfd ATTRIBUTE_UNUSED,
  107. void * src,
  108. void * dst)
  109. {
  110. FILHDR *filehdr_src = (FILHDR *) src;
  111. struct internal_filehdr *filehdr_dst = (struct internal_filehdr *) dst;
  112. ADJUST_VAL (filehdr_dst->f_symptr, GO32_STUBSIZE);
  113. /* Save now the stub to be used later. Put the stub data to FILEHDR_DST
  114. first as coff_data (abfd) still does not exist. It may not even be ever
  115. created as we are just checking the file format of ABFD. */
  116. memcpy (filehdr_dst->go32stub, filehdr_src->stub, GO32_STUBSIZE);
  117. filehdr_dst->f_flags |= F_GO32STUB;
  118. }
  119. static void
  120. adjust_filehdr_out_pre (bfd * abfd, void * in, void * out)
  121. {
  122. struct internal_filehdr *filehdr_in = (struct internal_filehdr *) in;
  123. FILHDR *filehdr_out = (FILHDR *) out;
  124. /* Generate the stub. */
  125. create_go32_stub (abfd);
  126. /* Copy the stub to the file header. */
  127. if (coff_data (abfd)->go32stub != NULL)
  128. memcpy (filehdr_out->stub, coff_data (abfd)->go32stub, GO32_STUBSIZE);
  129. else
  130. /* Use the default. */
  131. memcpy (filehdr_out->stub, stub_bytes, GO32_STUBSIZE);
  132. ADJUST_VAL (filehdr_in->f_symptr, -GO32_STUBSIZE);
  133. }
  134. static void
  135. adjust_filehdr_out_post (bfd * abfd ATTRIBUTE_UNUSED,
  136. void * in,
  137. void * out ATTRIBUTE_UNUSED)
  138. {
  139. struct internal_filehdr *filehdr_in = (struct internal_filehdr *) in;
  140. /* Undo the above change. */
  141. ADJUST_VAL (filehdr_in->f_symptr, GO32_STUBSIZE);
  142. }
  143. static void
  144. adjust_scnhdr_in_post (bfd * abfd ATTRIBUTE_UNUSED,
  145. void * ext ATTRIBUTE_UNUSED,
  146. void * in)
  147. {
  148. struct internal_scnhdr *scnhdr_int = (struct internal_scnhdr *) in;
  149. ADJUST_VAL (scnhdr_int->s_scnptr, GO32_STUBSIZE);
  150. ADJUST_VAL (scnhdr_int->s_relptr, GO32_STUBSIZE);
  151. ADJUST_VAL (scnhdr_int->s_lnnoptr, GO32_STUBSIZE);
  152. }
  153. static void
  154. adjust_scnhdr_out_pre (bfd * abfd ATTRIBUTE_UNUSED,
  155. void * in,
  156. void * out ATTRIBUTE_UNUSED)
  157. {
  158. struct internal_scnhdr *scnhdr_int = (struct internal_scnhdr *) in;
  159. ADJUST_VAL (scnhdr_int->s_scnptr, -GO32_STUBSIZE);
  160. ADJUST_VAL (scnhdr_int->s_relptr, -GO32_STUBSIZE);
  161. ADJUST_VAL (scnhdr_int->s_lnnoptr, -GO32_STUBSIZE);
  162. }
  163. static void
  164. adjust_scnhdr_out_post (bfd * abfd ATTRIBUTE_UNUSED,
  165. void * in,
  166. void * out ATTRIBUTE_UNUSED)
  167. {
  168. struct internal_scnhdr *scnhdr_int = (struct internal_scnhdr *) in;
  169. ADJUST_VAL (scnhdr_int->s_scnptr, GO32_STUBSIZE);
  170. ADJUST_VAL (scnhdr_int->s_relptr, GO32_STUBSIZE);
  171. ADJUST_VAL (scnhdr_int->s_lnnoptr, GO32_STUBSIZE);
  172. }
  173. static void
  174. adjust_aux_in_post (bfd * abfd ATTRIBUTE_UNUSED,
  175. void * ext1 ATTRIBUTE_UNUSED,
  176. int type,
  177. int in_class,
  178. int indx ATTRIBUTE_UNUSED,
  179. int numaux ATTRIBUTE_UNUSED,
  180. void * in1)
  181. {
  182. union internal_auxent *in = (union internal_auxent *) in1;
  183. if (in_class == C_BLOCK || in_class == C_FCN || ISFCN (type)
  184. || ISTAG (in_class))
  185. {
  186. ADJUST_VAL (in->x_sym.x_fcnary.x_fcn.x_lnnoptr, GO32_STUBSIZE);
  187. }
  188. }
  189. static void
  190. adjust_aux_out_pre (bfd *abfd ATTRIBUTE_UNUSED,
  191. void * inp,
  192. int type,
  193. int in_class,
  194. int indx ATTRIBUTE_UNUSED,
  195. int numaux ATTRIBUTE_UNUSED,
  196. void * extp ATTRIBUTE_UNUSED)
  197. {
  198. union internal_auxent *in = (union internal_auxent *) inp;
  199. if (in_class == C_BLOCK || in_class == C_FCN || ISFCN (type)
  200. || ISTAG (in_class))
  201. {
  202. ADJUST_VAL (in->x_sym.x_fcnary.x_fcn.x_lnnoptr, -GO32_STUBSIZE);
  203. }
  204. }
  205. static void
  206. adjust_aux_out_post (bfd *abfd ATTRIBUTE_UNUSED,
  207. void * inp,
  208. int type,
  209. int in_class,
  210. int indx ATTRIBUTE_UNUSED,
  211. int numaux ATTRIBUTE_UNUSED,
  212. void * extp ATTRIBUTE_UNUSED)
  213. {
  214. union internal_auxent *in = (union internal_auxent *) inp;
  215. if (in_class == C_BLOCK || in_class == C_FCN || ISFCN (type)
  216. || ISTAG (in_class))
  217. {
  218. ADJUST_VAL (in->x_sym.x_fcnary.x_fcn.x_lnnoptr, GO32_STUBSIZE);
  219. }
  220. }
  221. /* That's the function, which creates the stub. There are
  222. different cases from where the stub is taken.
  223. At first the environment variable $(GO32STUB) is checked and then
  224. $(STUB) if it was not set.
  225. If it exists and points to a valid stub the stub is taken from
  226. that file. This file can be also a whole executable file, because
  227. the stub is computed from the exe information at the start of that
  228. file.
  229. If there was any error, the standard stub (compiled in this file)
  230. is taken. */
  231. static void
  232. create_go32_stub (bfd *abfd)
  233. {
  234. /* Do it only once. */
  235. if (coff_data (abfd)->go32stub == NULL)
  236. {
  237. char *stub;
  238. struct stat st;
  239. int f;
  240. unsigned char header[10];
  241. char magic[8];
  242. unsigned long coff_start;
  243. long exe_start;
  244. /* Check at first the environment variable $(GO32STUB). */
  245. stub = getenv ("GO32STUB");
  246. /* Now check the environment variable $(STUB). */
  247. if (stub == NULL)
  248. stub = getenv ("STUB");
  249. if (stub == NULL)
  250. goto stub_end;
  251. if (stat (stub, &st) != 0)
  252. goto stub_end;
  253. #ifdef O_BINARY
  254. f = open (stub, O_RDONLY | O_BINARY);
  255. #else
  256. f = open (stub, O_RDONLY);
  257. #endif
  258. if (f < 0)
  259. goto stub_end;
  260. if (read (f, &header, sizeof (header)) < 0)
  261. {
  262. close (f);
  263. goto stub_end;
  264. }
  265. if (_H (0) != 0x5a4d) /* It is not an exe file. */
  266. {
  267. close (f);
  268. goto stub_end;
  269. }
  270. /* Compute the size of the stub (it is every thing up
  271. to the beginning of the coff image). */
  272. coff_start = (long) _H (2) * 512L;
  273. if (_H (1))
  274. coff_start += (long) _H (1) - 512L;
  275. /* Currently there is only a fixed stub size of 2048 bytes
  276. supported. */
  277. if (coff_start != 2048)
  278. {
  279. close (f);
  280. goto stub_end;
  281. }
  282. exe_start = _H (4) * 16;
  283. if ((long) lseek (f, exe_start, SEEK_SET) != exe_start)
  284. {
  285. close (f);
  286. goto stub_end;
  287. }
  288. if (read (f, &magic, 8) != 8)
  289. {
  290. close (f);
  291. goto stub_end;
  292. }
  293. if (! CONST_STRNEQ (magic, "go32stub"))
  294. {
  295. close (f);
  296. goto stub_end;
  297. }
  298. /* Now we found a correct stub (hopefully). */
  299. coff_data (abfd)->go32stub = bfd_alloc (abfd, (bfd_size_type) coff_start);
  300. if (coff_data (abfd)->go32stub == NULL)
  301. {
  302. close (f);
  303. return;
  304. }
  305. lseek (f, 0L, SEEK_SET);
  306. if ((unsigned long) read (f, coff_data (abfd)->go32stub, coff_start)
  307. != coff_start)
  308. {
  309. bfd_release (abfd, coff_data (abfd)->go32stub);
  310. coff_data (abfd)->go32stub = NULL;
  311. }
  312. close (f);
  313. }
  314. stub_end:
  315. /* There was something wrong above, so use now the standard builtin
  316. stub. */
  317. if (coff_data (abfd)->go32stub == NULL)
  318. {
  319. coff_data (abfd)->go32stub
  320. = bfd_alloc (abfd, (bfd_size_type) GO32_STUBSIZE);
  321. if (coff_data (abfd)->go32stub == NULL)
  322. return;
  323. memcpy (coff_data (abfd)->go32stub, stub_bytes, GO32_STUBSIZE);
  324. }
  325. }
  326. /* If ibfd was a stubbed coff image, copy the stub from that bfd
  327. to the new obfd. */
  328. static bfd_boolean
  329. go32_stubbed_coff_bfd_copy_private_bfd_data (bfd *ibfd, bfd *obfd)
  330. {
  331. /* Check if both are the same targets. */
  332. if (ibfd->xvec != obfd->xvec)
  333. return TRUE;
  334. /* Check if we have a source stub. */
  335. if (coff_data (ibfd)->go32stub == NULL)
  336. return TRUE;
  337. /* As adjust_filehdr_out_pre may get called only after this function,
  338. optionally allocate the output stub. */
  339. if (coff_data (obfd)->go32stub == NULL)
  340. coff_data (obfd)->go32stub = bfd_alloc (obfd,
  341. (bfd_size_type) GO32_STUBSIZE);
  342. /* Now copy the stub. */
  343. if (coff_data (obfd)->go32stub != NULL)
  344. memcpy (coff_data (obfd)->go32stub, coff_data (ibfd)->go32stub,
  345. GO32_STUBSIZE);
  346. return TRUE;
  347. }
  348. /* coff_object_p only checks 2 bytes F_MAGIC at GO32_STUBSIZE inside the file
  349. which is too fragile. */
  350. static const bfd_target *
  351. go32_check_format (bfd *abfd)
  352. {
  353. char mz[2];
  354. if (bfd_bread (mz, 2, abfd) != 2 || mz[0] != 'M' || mz[1] != 'Z')
  355. {
  356. bfd_set_error (bfd_error_wrong_format);
  357. return NULL;
  358. }
  359. if (bfd_seek (abfd, 0, SEEK_SET) != 0)
  360. return NULL;
  361. return coff_object_p (abfd);
  362. }