9115e3c8183758b91a67b45d3a2881accef4e4f5
[external/binutils.git] / bfd / coff-stgo32.c
1 /* BFD back-end for Intel 386 COFF files (go32 variant with a stub).
2    Copyright 1997, 1998, 1999 Free Software Foundation, Inc.
3    Written by Robert Hoehne.
4
5    This file is part of BFD, the Binary File Descriptor library.
6
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 2 of the License, or
10    (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
20
21 /* This file handles now also stubbed coff images. The stub is a small
22    DOS executable program before the coff image to load it in memory
23    and execute it. This is needed, because DOS cannot run coff files.
24
25    All the functions below are called by the corresponding functions
26    from coffswap.h.
27    The only thing what they do is to adjust the information stored in
28    the COFF file which are offset into the file.
29    This is needed, because DJGPP uses a very special way to load and run
30    the coff image. It loads the image in memory and assumes then, that the
31    image had no stub by using the filepointers as pointers in the coff
32    image and NOT in the file.
33
34    To be compatible with any existing executables I have fixed this
35    here and NOT in the DJGPP startup code.
36  */
37
38 #define TARGET_SYM              go32stubbedcoff_vec
39 #define TARGET_NAME             "coff-go32-exe"
40 #define TARGET_UNDERSCORE       '_'
41 #define COFF_GO32_EXE
42 #define COFF_LONG_SECTION_NAMES
43 #define COFF_SUPPORT_GNU_LINKONCE
44
45 #include "bfd.h"
46
47 /* At first the prototypes */
48
49 static void
50 adjust_filehdr_in_post PARAMS ((bfd * abfd, PTR src, PTR dst));
51 static void
52 adjust_filehdr_out_pre PARAMS ((bfd * abfd, PTR in, PTR out));
53 static void
54 adjust_filehdr_out_post PARAMS ((bfd * abfd, PTR in, PTR out));
55
56 static void
57 adjust_scnhdr_in_post PARAMS ((bfd * abfd, PTR ext, PTR in));
58 static void
59 adjust_scnhdr_out_pre PARAMS ((bfd * abfd, PTR in, PTR out));
60 static void
61 adjust_scnhdr_out_post PARAMS ((bfd * abfd, PTR in, PTR out));
62
63 static void
64 adjust_aux_in_post PARAMS ((bfd * abfd, PTR ext1, int type, int class, int indx,
65                             int numaux, PTR in1));
66 static void
67 adjust_aux_out_pre PARAMS ((bfd * abfd, PTR inp, int type, int class, int indx,
68                             int numaux, PTR extp));
69 static void
70 adjust_aux_out_post PARAMS ((bfd * abfd, PTR inp, int type, int class, int indx,
71                              int numaux, PTR extp));
72
73 static void
74 create_go32_stub PARAMS ((bfd * abfd));
75
76 /*
77    All that ..._PRE and ...POST functions are called from the corresponding
78    coff_swap... functions. The ...PRE functions are called at the beginning
79    of the function and the ...POST functions at the end of the swap routines.
80  */
81
82 #define COFF_ADJUST_FILEHDR_IN_POST adjust_filehdr_in_post
83 #define COFF_ADJUST_FILEHDR_OUT_PRE adjust_filehdr_out_pre
84 #define COFF_ADJUST_FILEHDR_OUT_POST adjust_filehdr_out_post
85
86 #define COFF_ADJUST_SCNHDR_IN_POST adjust_scnhdr_in_post
87 #define COFF_ADJUST_SCNHDR_OUT_PRE adjust_scnhdr_out_pre
88 #define COFF_ADJUST_SCNHDR_OUT_POST adjust_scnhdr_out_post
89
90 #define COFF_ADJUST_AUX_IN_POST adjust_aux_in_post
91 #define COFF_ADJUST_AUX_OUT_PRE adjust_aux_out_pre
92 #define COFF_ADJUST_AUX_OUT_POST adjust_aux_out_post
93
94 static boolean
95   go32_stubbed_coff_bfd_copy_private_bfd_data PARAMS ((bfd * ibfd, bfd * obfd));
96
97 #define coff_bfd_copy_private_bfd_data go32_stubbed_coff_bfd_copy_private_bfd_data
98
99 #include "coff-i386.c"
100
101 /* I hold in the usrdata the stub */
102 #define bfd_coff_go32stub bfd_usrdata
103
104 /* This macro is used, because I cannot assume the endianess of the
105    host system */
106 #define _H(index) (bfd_h_get_16(abfd, (bfd_byte *)(header+index*2)))
107
108 /* These bytes are a 2048-byte DOS executable, which loads the COFF
109    image into memory and then runs it. It is called 'stub' */
110
111 static unsigned char stub_bytes[STUBSIZE] =
112 {
113 #include "go32stub.h"
114 };
115
116 /*
117    I have not commented each swap function below, because the
118    technique is in any function the same. For the ...in function,
119    all the pointers are adjusted by adding STUBSIZE and for the
120    ...out function, it is subtracted first and after calling the
121    standard swap function it is reset to the old value */
122
123 /* This macro is used for adjusting the filepointers, which
124    is done only, if the pointer is nonzero */
125
126 #define ADJUST_VAL(val,diff) \
127   if (val != 0) val += diff
128
129 static void
130 adjust_filehdr_in_post  (abfd, src, dst)
131      bfd *abfd;
132      PTR src;
133      PTR dst;
134 {
135   FILHDR *filehdr_src = (FILHDR *) src;
136   struct internal_filehdr *filehdr_dst = (struct internal_filehdr *) dst;
137
138   ADJUST_VAL (filehdr_dst->f_symptr, STUBSIZE);
139
140   /* Save now the stub to be used later */
141   bfd_coff_go32stub (abfd) = (PTR) bfd_alloc (abfd, STUBSIZE);
142
143   /* Since this function returns no status, I do not set here
144      any bfd_error_...
145      That means, before the use of bfd_coff_go32stub (), this value
146      should be checked if it is != NULL */
147   if (bfd_coff_go32stub (abfd) == NULL)
148     return;
149   memcpy (bfd_coff_go32stub (abfd), filehdr_src->stub, STUBSIZE);
150 }
151
152 static void
153 adjust_filehdr_out_pre  (abfd, in, out)
154      bfd *abfd;
155      PTR in;
156      PTR out;
157 {
158   struct internal_filehdr *filehdr_in = (struct internal_filehdr *) in;
159   FILHDR *filehdr_out = (FILHDR *) out;
160
161   /* Generate the stub */
162   create_go32_stub (abfd);
163
164   /* Copy the stub to the file header */
165   if (bfd_coff_go32stub (abfd) != NULL)
166     memcpy (filehdr_out->stub, bfd_coff_go32stub (abfd), STUBSIZE);
167   else
168     /* use the default */
169     memcpy (filehdr_out->stub, stub_bytes, STUBSIZE);
170
171   ADJUST_VAL (filehdr_in->f_symptr, -STUBSIZE);
172 }
173
174 static void
175 adjust_filehdr_out_post  (abfd, in, out)
176      bfd *abfd ATTRIBUTE_UNUSED;
177      PTR in;
178      PTR out ATTRIBUTE_UNUSED;
179 {
180   struct internal_filehdr *filehdr_in = (struct internal_filehdr *) in;
181   /* undo the above change */
182   ADJUST_VAL (filehdr_in->f_symptr, STUBSIZE);
183 }
184
185 static void
186 adjust_scnhdr_in_post  (abfd, ext, in)
187      bfd *abfd ATTRIBUTE_UNUSED;
188      PTR ext ATTRIBUTE_UNUSED;
189      PTR in;
190 {
191   struct internal_scnhdr *scnhdr_int = (struct internal_scnhdr *) in;
192
193   ADJUST_VAL (scnhdr_int->s_scnptr, STUBSIZE);
194   ADJUST_VAL (scnhdr_int->s_relptr, STUBSIZE);
195   ADJUST_VAL (scnhdr_int->s_lnnoptr, STUBSIZE);
196 }
197
198 static void
199 adjust_scnhdr_out_pre  (abfd, in, out)
200      bfd *abfd ATTRIBUTE_UNUSED;
201      PTR in;
202      PTR out ATTRIBUTE_UNUSED;
203 {
204   struct internal_scnhdr *scnhdr_int = (struct internal_scnhdr *) in;
205
206   ADJUST_VAL (scnhdr_int->s_scnptr, -STUBSIZE);
207   ADJUST_VAL (scnhdr_int->s_relptr, -STUBSIZE);
208   ADJUST_VAL (scnhdr_int->s_lnnoptr, -STUBSIZE);
209 }
210
211 static void
212 adjust_scnhdr_out_post (abfd, in, out)
213      bfd *abfd ATTRIBUTE_UNUSED;
214      PTR in;
215      PTR out ATTRIBUTE_UNUSED;
216 {
217   struct internal_scnhdr *scnhdr_int = (struct internal_scnhdr *) in;
218
219   ADJUST_VAL (scnhdr_int->s_scnptr, STUBSIZE);
220   ADJUST_VAL (scnhdr_int->s_relptr, STUBSIZE);
221   ADJUST_VAL (scnhdr_int->s_lnnoptr, STUBSIZE);
222 }
223
224 static void
225 adjust_aux_in_post  (abfd, ext1, type, class, indx, numaux, in1)
226      bfd *abfd ATTRIBUTE_UNUSED;
227      PTR ext1 ATTRIBUTE_UNUSED;
228      int type;
229      int class;
230      int indx ATTRIBUTE_UNUSED;
231      int numaux ATTRIBUTE_UNUSED;
232      PTR in1;
233 {
234   union internal_auxent *in = (union internal_auxent *) in1;
235
236   if (class == C_BLOCK || class == C_FCN || ISFCN (type) || ISTAG (class))
237     {
238       ADJUST_VAL (in->x_sym.x_fcnary.x_fcn.x_lnnoptr, STUBSIZE);
239     }
240 }
241
242 static void
243 adjust_aux_out_pre  (abfd, inp, type, class, indx, numaux, extp)
244      bfd *abfd ATTRIBUTE_UNUSED;
245      PTR inp;
246      int type;
247      int class;
248      int indx ATTRIBUTE_UNUSED;
249      int numaux ATTRIBUTE_UNUSED;
250      PTR extp ATTRIBUTE_UNUSED;
251 {
252   union internal_auxent *in = (union internal_auxent *) inp;
253
254   if (class == C_BLOCK || class == C_FCN || ISFCN (type) || ISTAG (class))
255     {
256       ADJUST_VAL (in->x_sym.x_fcnary.x_fcn.x_lnnoptr, -STUBSIZE);
257     }
258 }
259
260 static void
261 adjust_aux_out_post (abfd, inp, type, class, indx, numaux, extp)
262      bfd *abfd ATTRIBUTE_UNUSED;
263      PTR inp;
264      int type;
265      int class;
266      int indx ATTRIBUTE_UNUSED;
267      int numaux ATTRIBUTE_UNUSED;
268      PTR extp ATTRIBUTE_UNUSED;
269 {
270   union internal_auxent *in = (union internal_auxent *) inp;
271
272   if (class == C_BLOCK || class == C_FCN || ISFCN (type) || ISTAG (class))
273     {
274       ADJUST_VAL (in->x_sym.x_fcnary.x_fcn.x_lnnoptr, STUBSIZE);
275     }
276 }
277
278 /*
279    That's the function, which creates the stub. There are
280    different cases from where the stub is taken.
281    At first the environment variable $(GO32STUB) is checked and then
282    $(STUB) if it was not set.
283    If it exists and points to a valid stub the stub is taken from
284    that file. This file can be also a whole executable file, because
285    the stub is computed from the exe information at the start of that
286    file.
287
288    If there was any error, the standard stub (compiled in this file)
289    is taken.
290  */
291
292 static void
293 create_go32_stub (abfd)
294      bfd *abfd;
295 {
296   /* Do it only once */
297   if (bfd_coff_go32stub (abfd) == NULL)
298     {
299       char *stub;
300       struct stat st;
301       int f;
302       unsigned char header[10];
303       char magic[8];
304       unsigned long coff_start, exe_start;
305
306       /* Check at first the environment variable $(GO32STUB) */
307       stub = getenv ("GO32STUB");
308       /* Now check the environment variable $(STUB) */
309       if (stub == NULL)
310         stub = getenv ("STUB");
311       if (stub == NULL)
312         goto stub_end;
313       if (stat (stub, &st) != 0)
314         goto stub_end;
315 #ifdef O_BINARY
316       f = open (stub, O_RDONLY | O_BINARY);
317 #else
318       f = open (stub, O_RDONLY);
319 #endif
320       if (f < 0)
321         goto stub_end;
322       if (read (f, &header, sizeof (header)) < 0)
323         {
324           close (f);
325           goto stub_end;
326         }
327       if (_H (0) != 0x5a4d)     /* it is not an exe file */
328         {
329           close (f);
330           goto stub_end;
331         }
332       /* Compute the size of the stub (it is every thing up
333          to the beginning of the coff image) */
334       coff_start = (long) _H (2) * 512L;
335       if (_H (1))
336         coff_start += (long) _H (1) - 512L;
337
338       /* Currently there is only a fixed stub size of 2048 bytes
339          supported */
340       if (coff_start != 2048)
341         {
342           close (f);
343           goto stub_end;
344         }
345       exe_start = _H (4) * 16;
346       if ((unsigned long) lseek (f, exe_start, SEEK_SET) != exe_start)
347         {
348           close (f);
349           goto stub_end;
350         }
351       if (read (f, &magic, 8) != 8)
352         {
353           close (f);
354           goto stub_end;
355         }
356       if (memcmp (magic, "go32stub", 8) != 0)
357         {
358           close (f);
359           goto stub_end;
360         }
361       /* Now we found a correct stub (hopefully) */
362       bfd_coff_go32stub (abfd) = (PTR) bfd_alloc (abfd, coff_start);
363       if (bfd_coff_go32stub (abfd) == NULL)
364         {
365           close (f);
366           return;
367         }
368       lseek (f, 0L, SEEK_SET);
369       if ((unsigned long) read (f, bfd_coff_go32stub (abfd), coff_start)
370           != coff_start)
371         {
372           bfd_release (abfd, bfd_coff_go32stub (abfd));
373           bfd_coff_go32stub (abfd) = NULL;
374         }
375       close (f);
376     }
377 stub_end:
378   /* There was something wrong above, so use now the standard builtin
379      stub */
380   if (bfd_coff_go32stub (abfd) == NULL)
381     {
382       bfd_coff_go32stub (abfd) = (PTR) bfd_alloc (abfd, STUBSIZE);
383       if (bfd_coff_go32stub (abfd) == NULL)
384         {
385           return;
386         }
387
388       memcpy (bfd_coff_go32stub (abfd), stub_bytes, STUBSIZE);
389     }
390 }
391
392 /* If ibfd was a stubbed coff image, copy the stub from that bfd
393    to the new obfd.
394  */
395
396 static boolean
397 go32_stubbed_coff_bfd_copy_private_bfd_data  (ibfd, obfd)
398      bfd *ibfd;
399      bfd *obfd;
400 {
401   /* check if both are the same targets */
402   if (ibfd->xvec != obfd->xvec)
403     return true;
404
405   /* check if both have a valid stub */
406   if (bfd_coff_go32stub (ibfd) == NULL
407       || bfd_coff_go32stub (obfd) == NULL)
408     return true;
409
410   /* Now copy the stub */
411   memcpy (bfd_coff_go32stub (obfd), bfd_coff_go32stub (ibfd), STUBSIZE);
412
413   return true;
414 }