1 /* BFD back-end for Intel 386 COFF files (DJGPP variant with a stub).
2 Copyright (C) 1997-2020 Free Software Foundation, Inc.
3 Written by Robert Hoehne.
5 This file is part of BFD, the Binary File Descriptor library.
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 3 of the License, or
10 (at your option) any later version.
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.
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., 51 Franklin Street - Fifth Floor, Boston,
20 MA 02110-1301, USA. */
22 /* This file handles now also stubbed coff images. The stub is a small
23 DOS executable program before the coff image to load it in memory
24 and execute it. This is needed, because DOS cannot run coff files.
26 All the functions below are called by the corresponding functions
28 The only thing what they do is to adjust the information stored in
29 the COFF file which are offset into the file.
30 This is needed, because DJGPP uses a very special way to load and run
31 the coff image. It loads the image in memory and assumes then, that the
32 image had no stub by using the filepointers as pointers in the coff
33 image and NOT in the file.
35 To be compatible with any existing executables I have fixed this
36 here and NOT in the DJGPP startup code. */
38 #define TARGET_SYM i386_coff_go32stubbed_vec
39 #define TARGET_NAME "coff-go32-exe"
40 #define TARGET_UNDERSCORE '_'
42 #define COFF_LONG_SECTION_NAMES
43 #define COFF_SUPPORT_GNU_LINKONCE
44 #define COFF_LONG_FILENAMES
46 #define COFF_SECTION_ALIGNMENT_ENTRIES \
47 { COFF_SECTION_NAME_EXACT_MATCH (".data"), \
48 COFF_ALIGNMENT_FIELD_EMPTY, COFF_ALIGNMENT_FIELD_EMPTY, 4 }, \
49 { COFF_SECTION_NAME_EXACT_MATCH (".text"), \
50 COFF_ALIGNMENT_FIELD_EMPTY, COFF_ALIGNMENT_FIELD_EMPTY, 4 }, \
51 { COFF_SECTION_NAME_PARTIAL_MATCH (".debug"), \
52 COFF_ALIGNMENT_FIELD_EMPTY, COFF_ALIGNMENT_FIELD_EMPTY, 0 }, \
53 { COFF_SECTION_NAME_PARTIAL_MATCH (".gnu.linkonce.wi"), \
54 COFF_ALIGNMENT_FIELD_EMPTY, COFF_ALIGNMENT_FIELD_EMPTY, 0 }
59 /* All that ..._PRE and ...POST functions are called from the corresponding
60 coff_swap... functions. The ...PRE functions are called at the beginning
61 of the function and the ...POST functions at the end of the swap routines. */
64 adjust_filehdr_in_post (bfd
*, void *, void *);
66 adjust_filehdr_out_pre (bfd
*, void *, void *);
68 adjust_filehdr_out_post (bfd
*, void *, void *);
70 adjust_scnhdr_in_post (bfd
*, void *, void *);
72 adjust_scnhdr_out_pre (bfd
*, void *, void *);
74 adjust_scnhdr_out_post (bfd
*, void *, void *);
76 adjust_aux_in_post (bfd
*, void *, int, int, int, int, void *);
78 adjust_aux_out_pre (bfd
*, void *, int, int, int, int, void *);
80 adjust_aux_out_post (bfd
*, void *, int, int, int, int, void *);
82 create_go32_stub (bfd
*);
84 #define COFF_ADJUST_FILEHDR_IN_POST adjust_filehdr_in_post
85 #define COFF_ADJUST_FILEHDR_OUT_PRE adjust_filehdr_out_pre
86 #define COFF_ADJUST_FILEHDR_OUT_POST adjust_filehdr_out_post
88 #define COFF_ADJUST_SCNHDR_IN_POST adjust_scnhdr_in_post
89 #define COFF_ADJUST_SCNHDR_OUT_PRE adjust_scnhdr_out_pre
90 #define COFF_ADJUST_SCNHDR_OUT_POST adjust_scnhdr_out_post
92 #define COFF_ADJUST_AUX_IN_POST adjust_aux_in_post
93 #define COFF_ADJUST_AUX_OUT_PRE adjust_aux_out_pre
94 #define COFF_ADJUST_AUX_OUT_POST adjust_aux_out_post
96 static const bfd_target
*go32_check_format (bfd
*);
98 #define COFF_CHECK_FORMAT go32_check_format
101 go32_stubbed_coff_bfd_copy_private_bfd_data (bfd
*, bfd
*);
103 #define coff_bfd_copy_private_bfd_data go32_stubbed_coff_bfd_copy_private_bfd_data
105 #include "coff-i386.c"
107 /* This macro is used, because I cannot assume the endianness of the
109 #define _H(index) (H_GET_16 (abfd, (header + index * 2)))
111 /* These bytes are a 2048-byte DOS executable, which loads the COFF
112 image into memory and then runs it. It is called 'stub'. */
114 static const unsigned char stub_bytes
[GO32_STUBSIZE
] =
116 #include "go32stub.h"
120 I have not commented each swap function below, because the
121 technique is in any function the same. For the ...in function,
122 all the pointers are adjusted by adding GO32_STUBSIZE and for the
123 ...out function, it is subtracted first and after calling the
124 standard swap function it is reset to the old value. */
126 /* This macro is used for adjusting the filepointers, which
127 is done only, if the pointer is nonzero. */
129 #define ADJUST_VAL(val,diff) \
130 if (val != 0) val += diff
133 adjust_filehdr_in_post (bfd
* abfd ATTRIBUTE_UNUSED
,
137 FILHDR
*filehdr_src
= (FILHDR
*) src
;
138 struct internal_filehdr
*filehdr_dst
= (struct internal_filehdr
*) dst
;
140 ADJUST_VAL (filehdr_dst
->f_symptr
, GO32_STUBSIZE
);
142 /* Save now the stub to be used later. Put the stub data to FILEHDR_DST
143 first as coff_data (abfd) still does not exist. It may not even be ever
144 created as we are just checking the file format of ABFD. */
145 memcpy (filehdr_dst
->go32stub
, filehdr_src
->stub
, GO32_STUBSIZE
);
146 filehdr_dst
->f_flags
|= F_GO32STUB
;
150 adjust_filehdr_out_pre (bfd
* abfd
, void * in
, void * out
)
152 struct internal_filehdr
*filehdr_in
= (struct internal_filehdr
*) in
;
153 FILHDR
*filehdr_out
= (FILHDR
*) out
;
155 /* Generate the stub. */
156 create_go32_stub (abfd
);
158 /* Copy the stub to the file header. */
159 if (coff_data (abfd
)->go32stub
!= NULL
)
160 memcpy (filehdr_out
->stub
, coff_data (abfd
)->go32stub
, GO32_STUBSIZE
);
162 /* Use the default. */
163 memcpy (filehdr_out
->stub
, stub_bytes
, GO32_STUBSIZE
);
165 ADJUST_VAL (filehdr_in
->f_symptr
, -GO32_STUBSIZE
);
169 adjust_filehdr_out_post (bfd
* abfd ATTRIBUTE_UNUSED
,
171 void * out ATTRIBUTE_UNUSED
)
173 struct internal_filehdr
*filehdr_in
= (struct internal_filehdr
*) in
;
174 /* Undo the above change. */
175 ADJUST_VAL (filehdr_in
->f_symptr
, GO32_STUBSIZE
);
179 adjust_scnhdr_in_post (bfd
* abfd ATTRIBUTE_UNUSED
,
180 void * ext ATTRIBUTE_UNUSED
,
183 struct internal_scnhdr
*scnhdr_int
= (struct internal_scnhdr
*) in
;
185 ADJUST_VAL (scnhdr_int
->s_scnptr
, GO32_STUBSIZE
);
186 ADJUST_VAL (scnhdr_int
->s_relptr
, GO32_STUBSIZE
);
187 ADJUST_VAL (scnhdr_int
->s_lnnoptr
, GO32_STUBSIZE
);
191 adjust_scnhdr_out_pre (bfd
* abfd ATTRIBUTE_UNUSED
,
193 void * out ATTRIBUTE_UNUSED
)
195 struct internal_scnhdr
*scnhdr_int
= (struct internal_scnhdr
*) in
;
197 ADJUST_VAL (scnhdr_int
->s_scnptr
, -GO32_STUBSIZE
);
198 ADJUST_VAL (scnhdr_int
->s_relptr
, -GO32_STUBSIZE
);
199 ADJUST_VAL (scnhdr_int
->s_lnnoptr
, -GO32_STUBSIZE
);
203 adjust_scnhdr_out_post (bfd
* abfd ATTRIBUTE_UNUSED
,
205 void * out ATTRIBUTE_UNUSED
)
207 struct internal_scnhdr
*scnhdr_int
= (struct internal_scnhdr
*) in
;
209 ADJUST_VAL (scnhdr_int
->s_scnptr
, GO32_STUBSIZE
);
210 ADJUST_VAL (scnhdr_int
->s_relptr
, GO32_STUBSIZE
);
211 ADJUST_VAL (scnhdr_int
->s_lnnoptr
, GO32_STUBSIZE
);
215 adjust_aux_in_post (bfd
* abfd ATTRIBUTE_UNUSED
,
216 void * ext1 ATTRIBUTE_UNUSED
,
219 int indx ATTRIBUTE_UNUSED
,
220 int numaux ATTRIBUTE_UNUSED
,
223 union internal_auxent
*in
= (union internal_auxent
*) in1
;
225 if (in_class
== C_BLOCK
|| in_class
== C_FCN
|| ISFCN (type
)
228 ADJUST_VAL (in
->x_sym
.x_fcnary
.x_fcn
.x_lnnoptr
, GO32_STUBSIZE
);
233 adjust_aux_out_pre (bfd
*abfd ATTRIBUTE_UNUSED
,
237 int indx ATTRIBUTE_UNUSED
,
238 int numaux ATTRIBUTE_UNUSED
,
239 void * extp ATTRIBUTE_UNUSED
)
241 union internal_auxent
*in
= (union internal_auxent
*) inp
;
243 if (in_class
== C_BLOCK
|| in_class
== C_FCN
|| ISFCN (type
)
246 ADJUST_VAL (in
->x_sym
.x_fcnary
.x_fcn
.x_lnnoptr
, -GO32_STUBSIZE
);
251 adjust_aux_out_post (bfd
*abfd ATTRIBUTE_UNUSED
,
255 int indx ATTRIBUTE_UNUSED
,
256 int numaux ATTRIBUTE_UNUSED
,
257 void * extp ATTRIBUTE_UNUSED
)
259 union internal_auxent
*in
= (union internal_auxent
*) inp
;
261 if (in_class
== C_BLOCK
|| in_class
== C_FCN
|| ISFCN (type
)
264 ADJUST_VAL (in
->x_sym
.x_fcnary
.x_fcn
.x_lnnoptr
, GO32_STUBSIZE
);
268 /* That's the function, which creates the stub. There are
269 different cases from where the stub is taken.
270 At first the environment variable $(GO32STUB) is checked and then
271 $(STUB) if it was not set.
272 If it exists and points to a valid stub the stub is taken from
273 that file. This file can be also a whole executable file, because
274 the stub is computed from the exe information at the start of that
277 If there was any error, the standard stub (compiled in this file)
281 create_go32_stub (bfd
*abfd
)
283 /* Do it only once. */
284 if (coff_data (abfd
)->go32stub
== NULL
)
289 unsigned char header
[10];
291 unsigned long coff_start
;
294 /* Check at first the environment variable $(GO32STUB). */
295 stub
= getenv ("GO32STUB");
296 /* Now check the environment variable $(STUB). */
298 stub
= getenv ("STUB");
301 if (stat (stub
, &st
) != 0)
304 f
= open (stub
, O_RDONLY
| O_BINARY
);
306 f
= open (stub
, O_RDONLY
);
310 if (read (f
, &header
, sizeof (header
)) < 0)
315 if (_H (0) != 0x5a4d) /* It is not an exe file. */
320 /* Compute the size of the stub (it is every thing up
321 to the beginning of the coff image). */
322 coff_start
= (long) _H (2) * 512L;
324 coff_start
+= (long) _H (1) - 512L;
326 /* Currently there is only a fixed stub size of 2048 bytes
328 if (coff_start
!= 2048)
333 exe_start
= _H (4) * 16;
334 if ((long) lseek (f
, exe_start
, SEEK_SET
) != exe_start
)
339 if (read (f
, &magic
, 8) != 8)
344 if (! CONST_STRNEQ (magic
, "go32stub"))
349 /* Now we found a correct stub (hopefully). */
350 coff_data (abfd
)->go32stub
= bfd_alloc (abfd
, (bfd_size_type
) coff_start
);
351 if (coff_data (abfd
)->go32stub
== NULL
)
356 lseek (f
, 0L, SEEK_SET
);
357 if ((unsigned long) read (f
, coff_data (abfd
)->go32stub
, coff_start
)
360 bfd_release (abfd
, coff_data (abfd
)->go32stub
);
361 coff_data (abfd
)->go32stub
= NULL
;
366 /* There was something wrong above, so use now the standard builtin
368 if (coff_data (abfd
)->go32stub
== NULL
)
370 coff_data (abfd
)->go32stub
371 = bfd_alloc (abfd
, (bfd_size_type
) GO32_STUBSIZE
);
372 if (coff_data (abfd
)->go32stub
== NULL
)
374 memcpy (coff_data (abfd
)->go32stub
, stub_bytes
, GO32_STUBSIZE
);
378 /* If ibfd was a stubbed coff image, copy the stub from that bfd
382 go32_stubbed_coff_bfd_copy_private_bfd_data (bfd
*ibfd
, bfd
*obfd
)
384 /* Check if both are the same targets. */
385 if (ibfd
->xvec
!= obfd
->xvec
)
388 /* Check if we have a source stub. */
389 if (coff_data (ibfd
)->go32stub
== NULL
)
392 /* As adjust_filehdr_out_pre may get called only after this function,
393 optionally allocate the output stub. */
394 if (coff_data (obfd
)->go32stub
== NULL
)
395 coff_data (obfd
)->go32stub
= bfd_alloc (obfd
,
396 (bfd_size_type
) GO32_STUBSIZE
);
398 /* Now copy the stub. */
399 if (coff_data (obfd
)->go32stub
!= NULL
)
400 memcpy (coff_data (obfd
)->go32stub
, coff_data (ibfd
)->go32stub
,
406 /* coff_object_p only checks 2 bytes F_MAGIC at GO32_STUBSIZE inside the file
407 which is too fragile. */
409 static const bfd_target
*
410 go32_check_format (bfd
*abfd
)
414 if (bfd_bread (mz
, 2, abfd
) != 2 || mz
[0] != 'M' || mz
[1] != 'Z')
416 bfd_set_error (bfd_error_wrong_format
);
420 if (bfd_seek (abfd
, 0, SEEK_SET
) != 0)
423 return coff_object_p (abfd
);