1 /* BFD back-end for Intel 386 COFF files (DJGPP variant with a stub).
2 Copyright 1997, 1998, 1999, 2000, 2001, 2002, 2005, 2006, 2007, 2009,
3 2011, 2012 Free Software Foundation, Inc.
4 Written by Robert Hoehne.
6 This file is part of BFD, the Binary File Descriptor library.
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.
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.
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. */
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.
27 All the functions below are called by the corresponding functions
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.
36 To be compatible with any existing executables I have fixed this
37 here and NOT in the DJGPP startup code. */
39 #define TARGET_SYM go32stubbedcoff_vec
40 #define TARGET_NAME "coff-go32-exe"
41 #define TARGET_UNDERSCORE '_'
43 #define COFF_LONG_SECTION_NAMES
44 #define COFF_SUPPORT_GNU_LINKONCE
45 #define COFF_LONG_FILENAMES
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 }
60 /* At first the prototypes. */
63 adjust_filehdr_in_post
PARAMS ((bfd
*, PTR
, PTR
));
65 adjust_filehdr_out_pre
PARAMS ((bfd
*, PTR
, PTR
));
67 adjust_filehdr_out_post
PARAMS ((bfd
*, PTR
, PTR
));
69 adjust_scnhdr_in_post
PARAMS ((bfd
*, PTR
, PTR
));
71 adjust_scnhdr_out_pre
PARAMS ((bfd
*, PTR
, PTR
));
73 adjust_scnhdr_out_post
PARAMS ((bfd
*, PTR
, PTR
));
75 adjust_aux_in_post
PARAMS ((bfd
*, PTR
, int, int, int, int, PTR
));
77 adjust_aux_out_pre
PARAMS ((bfd
*, PTR
, int, int, int, int, PTR
));
79 adjust_aux_out_post
PARAMS ((bfd
*, PTR
, int, int, int, int, PTR
));
81 create_go32_stub
PARAMS ((bfd
*));
83 /* All that ..._PRE and ...POST functions are called from the corresponding
84 coff_swap... functions. The ...PRE functions are called at the beginning
85 of the function and the ...POST functions at the end of the swap routines. */
87 #define COFF_ADJUST_FILEHDR_IN_POST adjust_filehdr_in_post
88 #define COFF_ADJUST_FILEHDR_OUT_PRE adjust_filehdr_out_pre
89 #define COFF_ADJUST_FILEHDR_OUT_POST adjust_filehdr_out_post
91 #define COFF_ADJUST_SCNHDR_IN_POST adjust_scnhdr_in_post
92 #define COFF_ADJUST_SCNHDR_OUT_PRE adjust_scnhdr_out_pre
93 #define COFF_ADJUST_SCNHDR_OUT_POST adjust_scnhdr_out_post
95 #define COFF_ADJUST_AUX_IN_POST adjust_aux_in_post
96 #define COFF_ADJUST_AUX_OUT_PRE adjust_aux_out_pre
97 #define COFF_ADJUST_AUX_OUT_POST adjust_aux_out_post
99 static const bfd_target
*go32_check_format (bfd
*abfd
);
101 #define COFF_CHECK_FORMAT go32_check_format
104 go32_stubbed_coff_bfd_copy_private_bfd_data
PARAMS ((bfd
*, bfd
*));
106 #define coff_bfd_copy_private_bfd_data go32_stubbed_coff_bfd_copy_private_bfd_data
108 #include "coff-i386.c"
110 /* This macro is used, because I cannot assume the endianness of the
112 #define _H(index) (H_GET_16 (abfd, (header + index * 2)))
114 /* These bytes are a 2048-byte DOS executable, which loads the COFF
115 image into memory and then runs it. It is called 'stub'. */
117 static const unsigned char stub_bytes
[GO32_STUBSIZE
] =
119 #include "go32stub.h"
123 I have not commented each swap function below, because the
124 technique is in any function the same. For the ...in function,
125 all the pointers are adjusted by adding GO32_STUBSIZE and for the
126 ...out function, it is subtracted first and after calling the
127 standard swap function it is reset to the old value. */
129 /* This macro is used for adjusting the filepointers, which
130 is done only, if the pointer is nonzero. */
132 #define ADJUST_VAL(val,diff) \
133 if (val != 0) val += diff
136 adjust_filehdr_in_post (abfd
, src
, dst
)
137 bfd
*abfd ATTRIBUTE_UNUSED
;
141 FILHDR
*filehdr_src
= (FILHDR
*) src
;
142 struct internal_filehdr
*filehdr_dst
= (struct internal_filehdr
*) dst
;
144 ADJUST_VAL (filehdr_dst
->f_symptr
, GO32_STUBSIZE
);
146 /* Save now the stub to be used later. Put the stub data to FILEHDR_DST
147 first as coff_data (abfd) still does not exist. It may not even be ever
148 created as we are just checking the file format of ABFD. */
149 memcpy (filehdr_dst
->go32stub
, filehdr_src
->stub
, GO32_STUBSIZE
);
150 filehdr_dst
->f_flags
|= F_GO32STUB
;
154 adjust_filehdr_out_pre (abfd
, in
, out
)
159 struct internal_filehdr
*filehdr_in
= (struct internal_filehdr
*) in
;
160 FILHDR
*filehdr_out
= (FILHDR
*) out
;
162 /* Generate the stub. */
163 create_go32_stub (abfd
);
165 /* Copy the stub to the file header. */
166 if (coff_data (abfd
)->go32stub
!= NULL
)
167 memcpy (filehdr_out
->stub
, coff_data (abfd
)->go32stub
, GO32_STUBSIZE
);
169 /* Use the default. */
170 memcpy (filehdr_out
->stub
, stub_bytes
, GO32_STUBSIZE
);
172 ADJUST_VAL (filehdr_in
->f_symptr
, -GO32_STUBSIZE
);
176 adjust_filehdr_out_post (abfd
, in
, out
)
177 bfd
*abfd ATTRIBUTE_UNUSED
;
179 PTR out ATTRIBUTE_UNUSED
;
181 struct internal_filehdr
*filehdr_in
= (struct internal_filehdr
*) in
;
182 /* Undo the above change. */
183 ADJUST_VAL (filehdr_in
->f_symptr
, GO32_STUBSIZE
);
187 adjust_scnhdr_in_post (abfd
, ext
, in
)
188 bfd
*abfd ATTRIBUTE_UNUSED
;
189 PTR ext ATTRIBUTE_UNUSED
;
192 struct internal_scnhdr
*scnhdr_int
= (struct internal_scnhdr
*) in
;
194 ADJUST_VAL (scnhdr_int
->s_scnptr
, GO32_STUBSIZE
);
195 ADJUST_VAL (scnhdr_int
->s_relptr
, GO32_STUBSIZE
);
196 ADJUST_VAL (scnhdr_int
->s_lnnoptr
, GO32_STUBSIZE
);
200 adjust_scnhdr_out_pre (abfd
, in
, out
)
201 bfd
*abfd ATTRIBUTE_UNUSED
;
203 PTR out ATTRIBUTE_UNUSED
;
205 struct internal_scnhdr
*scnhdr_int
= (struct internal_scnhdr
*) in
;
207 ADJUST_VAL (scnhdr_int
->s_scnptr
, -GO32_STUBSIZE
);
208 ADJUST_VAL (scnhdr_int
->s_relptr
, -GO32_STUBSIZE
);
209 ADJUST_VAL (scnhdr_int
->s_lnnoptr
, -GO32_STUBSIZE
);
213 adjust_scnhdr_out_post (abfd
, in
, out
)
214 bfd
*abfd ATTRIBUTE_UNUSED
;
216 PTR out ATTRIBUTE_UNUSED
;
218 struct internal_scnhdr
*scnhdr_int
= (struct internal_scnhdr
*) in
;
220 ADJUST_VAL (scnhdr_int
->s_scnptr
, GO32_STUBSIZE
);
221 ADJUST_VAL (scnhdr_int
->s_relptr
, GO32_STUBSIZE
);
222 ADJUST_VAL (scnhdr_int
->s_lnnoptr
, GO32_STUBSIZE
);
226 adjust_aux_in_post (abfd
, ext1
, type
, in_class
, indx
, numaux
, in1
)
227 bfd
*abfd ATTRIBUTE_UNUSED
;
228 PTR ext1 ATTRIBUTE_UNUSED
;
231 int indx ATTRIBUTE_UNUSED
;
232 int numaux ATTRIBUTE_UNUSED
;
235 union internal_auxent
*in
= (union internal_auxent
*) in1
;
237 if (in_class
== C_BLOCK
|| in_class
== C_FCN
|| ISFCN (type
)
240 ADJUST_VAL (in
->x_sym
.x_fcnary
.x_fcn
.x_lnnoptr
, GO32_STUBSIZE
);
245 adjust_aux_out_pre (abfd
, inp
, type
, in_class
, indx
, numaux
, extp
)
246 bfd
*abfd ATTRIBUTE_UNUSED
;
250 int indx ATTRIBUTE_UNUSED
;
251 int numaux ATTRIBUTE_UNUSED
;
252 PTR extp ATTRIBUTE_UNUSED
;
254 union internal_auxent
*in
= (union internal_auxent
*) inp
;
256 if (in_class
== C_BLOCK
|| in_class
== C_FCN
|| ISFCN (type
)
259 ADJUST_VAL (in
->x_sym
.x_fcnary
.x_fcn
.x_lnnoptr
, -GO32_STUBSIZE
);
264 adjust_aux_out_post (abfd
, inp
, type
, in_class
, indx
, numaux
, extp
)
265 bfd
*abfd ATTRIBUTE_UNUSED
;
269 int indx ATTRIBUTE_UNUSED
;
270 int numaux ATTRIBUTE_UNUSED
;
271 PTR extp ATTRIBUTE_UNUSED
;
273 union internal_auxent
*in
= (union internal_auxent
*) inp
;
275 if (in_class
== C_BLOCK
|| in_class
== C_FCN
|| ISFCN (type
)
278 ADJUST_VAL (in
->x_sym
.x_fcnary
.x_fcn
.x_lnnoptr
, GO32_STUBSIZE
);
282 /* That's the function, which creates the stub. There are
283 different cases from where the stub is taken.
284 At first the environment variable $(GO32STUB) is checked and then
285 $(STUB) if it was not set.
286 If it exists and points to a valid stub the stub is taken from
287 that file. This file can be also a whole executable file, because
288 the stub is computed from the exe information at the start of that
291 If there was any error, the standard stub (compiled in this file)
295 create_go32_stub (abfd
)
298 /* Do it only once. */
299 if (coff_data (abfd
)->go32stub
== NULL
)
304 unsigned char header
[10];
306 unsigned long coff_start
;
309 /* Check at first the environment variable $(GO32STUB). */
310 stub
= getenv ("GO32STUB");
311 /* Now check the environment variable $(STUB). */
313 stub
= getenv ("STUB");
316 if (stat (stub
, &st
) != 0)
319 f
= open (stub
, O_RDONLY
| O_BINARY
);
321 f
= open (stub
, O_RDONLY
);
325 if (read (f
, &header
, sizeof (header
)) < 0)
330 if (_H (0) != 0x5a4d) /* It is not an exe file. */
335 /* Compute the size of the stub (it is every thing up
336 to the beginning of the coff image). */
337 coff_start
= (long) _H (2) * 512L;
339 coff_start
+= (long) _H (1) - 512L;
341 /* Currently there is only a fixed stub size of 2048 bytes
343 if (coff_start
!= 2048)
348 exe_start
= _H (4) * 16;
349 if ((long) lseek (f
, exe_start
, SEEK_SET
) != exe_start
)
354 if (read (f
, &magic
, 8) != 8)
359 if (! CONST_STRNEQ (magic
, "go32stub"))
364 /* Now we found a correct stub (hopefully). */
365 coff_data (abfd
)->go32stub
= bfd_alloc (abfd
, (bfd_size_type
) coff_start
);
366 if (coff_data (abfd
)->go32stub
== NULL
)
371 lseek (f
, 0L, SEEK_SET
);
372 if ((unsigned long) read (f
, coff_data (abfd
)->go32stub
, coff_start
)
375 bfd_release (abfd
, coff_data (abfd
)->go32stub
);
376 coff_data (abfd
)->go32stub
= NULL
;
381 /* There was something wrong above, so use now the standard builtin
383 if (coff_data (abfd
)->go32stub
== NULL
)
385 coff_data (abfd
)->go32stub
386 = bfd_alloc (abfd
, (bfd_size_type
) GO32_STUBSIZE
);
387 if (coff_data (abfd
)->go32stub
== NULL
)
389 memcpy (coff_data (abfd
)->go32stub
, stub_bytes
, GO32_STUBSIZE
);
393 /* If ibfd was a stubbed coff image, copy the stub from that bfd
397 go32_stubbed_coff_bfd_copy_private_bfd_data (ibfd
, obfd
)
401 /* Check if both are the same targets. */
402 if (ibfd
->xvec
!= obfd
->xvec
)
405 /* Check if we have a source stub. */
406 if (coff_data (ibfd
)->go32stub
== NULL
)
409 /* As adjust_filehdr_out_pre may get called only after this function,
410 optionally allocate the output stub. */
411 if (coff_data (obfd
)->go32stub
== NULL
)
412 coff_data (obfd
)->go32stub
= bfd_alloc (obfd
,
413 (bfd_size_type
) GO32_STUBSIZE
);
415 /* Now copy the stub. */
416 if (coff_data (obfd
)->go32stub
!= NULL
)
417 memcpy (coff_data (obfd
)->go32stub
, coff_data (ibfd
)->go32stub
,
423 /* coff_object_p only checks 2 bytes F_MAGIC at GO32_STUBSIZE inside the file
424 which is too fragile. */
426 static const bfd_target
*
427 go32_check_format (bfd
*abfd
)
431 if (bfd_bread (mz
, 2, abfd
) != 2 || mz
[0] != 'M' || mz
[1] != 'Z')
433 bfd_set_error (bfd_error_wrong_format
);
437 if (bfd_seek (abfd
, 0, SEEK_SET
) != 0)
440 return coff_object_p (abfd
);