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