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 The COFF image is loaded in memory without the stub attached, so
27 all offsets are relative to the beginning of the image, not the
28 actual file. We handle this in bfd by setting bfd->origin to where
29 the COFF image starts. */
31 #define TARGET_SYM i386_coff_go32stubbed_vec
32 #define TARGET_NAME "coff-go32-exe"
33 #define TARGET_UNDERSCORE '_'
35 #define COFF_LONG_SECTION_NAMES
36 #define COFF_SUPPORT_GNU_LINKONCE
37 #define COFF_LONG_FILENAMES
39 #define COFF_SECTION_ALIGNMENT_ENTRIES \
40 { COFF_SECTION_NAME_EXACT_MATCH (".data"), \
41 COFF_ALIGNMENT_FIELD_EMPTY, COFF_ALIGNMENT_FIELD_EMPTY, 4 }, \
42 { COFF_SECTION_NAME_EXACT_MATCH (".text"), \
43 COFF_ALIGNMENT_FIELD_EMPTY, COFF_ALIGNMENT_FIELD_EMPTY, 4 }, \
44 { COFF_SECTION_NAME_PARTIAL_MATCH (".debug"), \
45 COFF_ALIGNMENT_FIELD_EMPTY, COFF_ALIGNMENT_FIELD_EMPTY, 0 }, \
46 { COFF_SECTION_NAME_PARTIAL_MATCH (".gnu.linkonce.wi"), \
47 COFF_ALIGNMENT_FIELD_EMPTY, COFF_ALIGNMENT_FIELD_EMPTY, 0 }
51 #include "coff/msdos.h"
53 static bfd_cleanup
go32exe_check_format (bfd
*);
54 static bfd_boolean
go32exe_write_object_contents (bfd
*);
55 static bfd_boolean
go32exe_mkobject (bfd
*);
56 static bfd_boolean
go32exe_copy_private_bfd_data (bfd
*, bfd
*);
58 #define COFF_CHECK_FORMAT go32exe_check_format
59 #define COFF_WRITE_CONTENTS go32exe_write_object_contents
60 #define coff_mkobject go32exe_mkobject
61 #define coff_bfd_copy_private_bfd_data go32exe_copy_private_bfd_data
63 #include "coff-i386.c"
65 /* This macro is used, because I cannot assume the endianness of the
67 #define _H(index) (H_GET_16 (abfd, (header + index * 2)))
69 /* These bytes are a 2048-byte DOS executable, which loads the COFF
70 image into memory and then runs it. It is called 'stub'. */
71 #define GO32EXE_DEFAULT_STUB_SIZE 2048
72 static const unsigned char go32exe_default_stub
[GO32EXE_DEFAULT_STUB_SIZE
] =
77 /* Temporary location for stub read from input file. */
78 static char * go32exe_temp_stub
= NULL
;
79 static bfd_size_type go32exe_temp_stub_size
= 0;
81 /* That's the function, which creates the stub. There are
82 different cases from where the stub is taken.
83 At first the environment variable $(GO32STUB) is checked and then
84 $(STUB) if it was not set.
85 If it exists and points to a valid stub the stub is taken from
86 that file. This file can be also a whole executable file, because
87 the stub is computed from the exe information at the start of that
90 If there was any error, the standard stub (compiled in this file)
93 Ideally this function should exec '$(TARGET)-stubify' to generate
94 a stub, like gcc does. */
97 go32exe_create_stub (bfd
*abfd
)
99 /* Do it only once. */
100 if (coff_data (abfd
)->stub
== NULL
)
105 unsigned char header
[10];
107 unsigned long coff_start
;
110 /* If we read a stub from an input file, use that one. */
111 if (go32exe_temp_stub
!= NULL
)
113 coff_data (abfd
)->stub
= bfd_alloc (abfd
,
114 go32exe_temp_stub_size
);
115 if (coff_data (abfd
)->stub
== NULL
)
117 memcpy (coff_data (abfd
)->stub
, go32exe_temp_stub
,
118 go32exe_temp_stub_size
);
119 coff_data (abfd
)->stub_size
= go32exe_temp_stub_size
;
120 free (go32exe_temp_stub
);
121 go32exe_temp_stub
= NULL
;
122 go32exe_temp_stub_size
= 0;
126 /* Check at first the environment variable $(GO32STUB). */
127 stub
= getenv ("GO32STUB");
128 /* Now check the environment variable $(STUB). */
130 stub
= getenv ("STUB");
133 if (stat (stub
, &st
) != 0)
136 f
= open (stub
, O_RDONLY
| O_BINARY
);
138 f
= open (stub
, O_RDONLY
);
142 if (read (f
, &header
, sizeof (header
)) < 0)
147 if (_H (0) != 0x5a4d) /* It is not an exe file. */
152 /* Compute the size of the stub (it is every thing up
153 to the beginning of the coff image). */
154 coff_start
= (long) _H (2) * 512L;
156 coff_start
+= (long) _H (1) - 512L;
158 exe_start
= _H (4) * 16;
159 if ((long) lseek (f
, exe_start
, SEEK_SET
) != exe_start
)
164 if (read (f
, &magic
, 8) != 8)
169 if (! CONST_STRNEQ (magic
, "go32stub"))
174 /* Now we found a correct stub (hopefully). */
175 coff_data (abfd
)->stub
= bfd_alloc (abfd
, (bfd_size_type
) coff_start
);
176 if (coff_data (abfd
)->stub
== NULL
)
181 lseek (f
, 0L, SEEK_SET
);
182 if ((unsigned long) read (f
, coff_data (abfd
)->stub
, coff_start
)
185 bfd_release (abfd
, coff_data (abfd
)->stub
);
186 coff_data (abfd
)->stub
= NULL
;
189 coff_data (abfd
)->stub_size
= coff_start
;
193 /* There was something wrong above, so use now the standard builtin
195 if (coff_data (abfd
)->stub
== NULL
)
197 coff_data (abfd
)->stub
198 = bfd_alloc (abfd
, (bfd_size_type
) GO32EXE_DEFAULT_STUB_SIZE
);
199 if (coff_data (abfd
)->stub
== NULL
)
201 memcpy (coff_data (abfd
)->stub
, go32exe_default_stub
,
202 GO32EXE_DEFAULT_STUB_SIZE
);
203 coff_data (abfd
)->stub_size
= GO32EXE_DEFAULT_STUB_SIZE
;
207 /* If ibfd was a stubbed coff image, copy the stub from that bfd
211 go32exe_copy_private_bfd_data (bfd
*ibfd
, bfd
*obfd
)
213 /* Check if both are the same targets. */
214 if (ibfd
->xvec
!= obfd
->xvec
)
217 /* Make sure we have a source stub. */
218 BFD_ASSERT (coff_data (ibfd
)->stub
!= NULL
);
220 /* Reallocate the output stub if necessary. */
221 if (coff_data (ibfd
)->stub_size
> coff_data (obfd
)->stub_size
)
222 coff_data (obfd
)->stub
= bfd_alloc (obfd
, coff_data (ibfd
)->stub_size
);
223 if (coff_data (obfd
)->stub
== NULL
)
226 /* Now copy the stub. */
227 memcpy (coff_data (obfd
)->stub
, coff_data (ibfd
)->stub
,
228 coff_data (ibfd
)->stub_size
);
229 coff_data (obfd
)->stub_size
= coff_data (ibfd
)->stub_size
;
230 obfd
->origin
= coff_data (obfd
)->stub_size
;
235 /* Cleanup function, returned from check_format hook. */
238 go32exe_cleanup (bfd
*abfd
)
242 if (go32exe_temp_stub
!= NULL
)
243 free (go32exe_temp_stub
);
244 go32exe_temp_stub
= NULL
;
245 go32exe_temp_stub_size
= 0;
248 /* Check that there is a GO32 stub and read it to go32exe_temp_stub.
249 Then set abfd->origin so that the COFF image is read at the correct
253 go32exe_check_format (bfd
*abfd
)
255 struct external_DOS_hdr filehdr_dos
;
257 uint16_t last_page_size
;
259 bfd_size_type stubsize
;
261 /* This format can not appear in an archive. */
262 if (abfd
->origin
!= 0)
264 bfd_set_error (bfd_error_wrong_format
);
268 bfd_set_error (bfd_error_system_call
);
270 /* Read in the stub file header, which is a DOS MZ executable. */
271 if (bfd_bread (&filehdr_dos
, DOS_HDR_SIZE
, abfd
) != DOS_HDR_SIZE
)
274 /* Make sure that this is an MZ executable. */
275 if (H_GET_16 (abfd
, filehdr_dos
.e_magic
) != IMAGE_DOS_SIGNATURE
)
278 /* Determine the size of the stub */
279 num_pages
= H_GET_16 (abfd
, filehdr_dos
.e_cp
);
280 last_page_size
= H_GET_16 (abfd
, filehdr_dos
.e_cblp
);
281 stubsize
= num_pages
* 512;
282 if (last_page_size
!= 0)
283 stubsize
+= last_page_size
- 512;
285 /* Save now the stub to be used later. Put the stub data to a temporary
286 location first as tdata still does not exist. It may not even
287 be ever created if we are just checking the file format of ABFD. */
288 bfd_seek (abfd
, 0, SEEK_SET
);
289 go32exe_temp_stub
= bfd_malloc (stubsize
);
290 if (go32exe_temp_stub
== NULL
)
292 if (bfd_bread (go32exe_temp_stub
, stubsize
, abfd
) != stubsize
)
294 go32exe_temp_stub_size
= stubsize
;
296 /* Confirm that this is a go32stub. */
297 header_end
= H_GET_16 (abfd
, filehdr_dos
.e_cparhdr
) * 16UL;
298 if (! CONST_STRNEQ (go32exe_temp_stub
+ header_end
, "go32stub"))
301 /* Set origin to where the COFF header starts and seek there. */
302 abfd
->origin
= stubsize
;
303 if (bfd_seek (abfd
, 0, SEEK_SET
) != 0)
306 /* Call coff_object_p to read the COFF image. If this fails then the file
307 must be just a stub with no COFF data attached. */
308 bfd_cleanup cleanup
= coff_object_p (abfd
);
311 BFD_ASSERT (cleanup
== _bfd_no_cleanup
);
313 return go32exe_cleanup
;
316 bfd_set_error (bfd_error_wrong_format
);
318 go32exe_cleanup (abfd
);
322 /* Write the stub to the output file, then call coff_write_object_contents. */
325 go32exe_write_object_contents (bfd
*abfd
)
327 const bfd_size_type pos
= bfd_tell (abfd
);
328 const bfd_size_type stubsize
= coff_data (abfd
)->stub_size
;
330 BFD_ASSERT (stubsize
!= 0);
332 bfd_set_error (bfd_error_system_call
);
334 /* Write the stub. */
336 if (bfd_seek (abfd
, 0, SEEK_SET
) != 0)
338 if (bfd_bwrite (coff_data (abfd
)->stub
, stubsize
, abfd
) != stubsize
)
341 /* Seek back to where we were. */
342 abfd
->origin
= stubsize
;
343 if (bfd_seek (abfd
, pos
, SEEK_SET
) != 0)
346 return coff_write_object_contents (abfd
);
349 /* mkobject hook. Called directly through bfd_set_format or via
350 coff_mkobject_hook etc from bfd_check_format. */
353 go32exe_mkobject (bfd
*abfd
)
355 coff_data_type
*coff
= NULL
;
356 const bfd_size_type amt
= sizeof (coff_data_type
);
358 /* Don't output to an archive. */
359 if (abfd
->my_archive
!= NULL
)
362 abfd
->tdata
.coff_obj_data
= bfd_zalloc (abfd
, amt
);
363 if (abfd
->tdata
.coff_obj_data
== NULL
)
365 coff
= coff_data (abfd
);
366 coff
->symbols
= NULL
;
367 coff
->conversion_table
= NULL
;
368 coff
->raw_syments
= NULL
;
370 coff
->local_toc_sym_map
= 0;
372 go32exe_create_stub (abfd
);
373 if (coff
->stub
== NULL
)
375 bfd_release (abfd
, coff
);
378 abfd
->origin
= coff
->stub_size
;
380 /* make_abs_section(abfd);*/ /* ??? */