1 /* BFD back-end for Intel 386 COFF files (DJGPP variant with a stub).
2 Copyright (C) 1997-2024 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 }
49 /* Section contains extended relocations. */
50 #define IMAGE_SCN_LNK_NRELOC_OVFL (0x01000000)
54 #include "coff/msdos.h"
56 static bfd_cleanup
go32exe_check_format (bfd
*);
57 static bool go32exe_write_object_contents (bfd
*);
58 static bool go32exe_mkobject (bfd
*);
59 static bool go32exe_copy_private_bfd_data (bfd
*, bfd
*);
61 /* Defined in coff-go32.c. */
62 bool _bfd_go32_mkobject (bfd
*);
63 void _bfd_go32_swap_scnhdr_in (bfd
*, void *, void *);
64 unsigned int _bfd_go32_swap_scnhdr_out (bfd
*, void *, void *);
66 #define COFF_CHECK_FORMAT go32exe_check_format
67 #define COFF_WRITE_CONTENTS go32exe_write_object_contents
68 #define coff_mkobject go32exe_mkobject
69 #define coff_bfd_copy_private_bfd_data go32exe_copy_private_bfd_data
70 #define coff_SWAP_scnhdr_in _bfd_go32_swap_scnhdr_in
71 #define coff_SWAP_scnhdr_out _bfd_go32_swap_scnhdr_out
73 #include "coff-i386.c"
75 /* This macro is used, because I cannot assume the endianness of the
77 #define _H(index) (H_GET_16 (abfd, (header + index * 2)))
79 /* These bytes are a 2048-byte DOS executable, which loads the COFF
80 image into memory and then runs it. It is called 'stub'. */
81 #define GO32EXE_DEFAULT_STUB_SIZE 2048
82 static const unsigned char go32exe_default_stub
[GO32EXE_DEFAULT_STUB_SIZE
] =
87 /* Temporary location for stub read from input file. */
88 static char * go32exe_temp_stub
= NULL
;
89 static bfd_size_type go32exe_temp_stub_size
= 0;
91 /* That's the function, which creates the stub. There are
92 different cases from where the stub is taken.
93 At first the environment variable $(GO32STUB) is checked and then
94 $(STUB) if it was not set.
95 If it exists and points to a valid stub the stub is taken from
96 that file. This file can be also a whole executable file, because
97 the stub is computed from the exe information at the start of that
100 If there was any error, the standard stub (compiled in this file)
103 Ideally this function should exec '$(TARGET)-stubify' to generate
104 a stub, like gcc does. */
107 go32exe_create_stub (bfd
*abfd
)
109 /* Do it only once. */
110 if (coff_data (abfd
)->stub
== NULL
)
115 unsigned char header
[10];
117 unsigned long coff_start
;
120 /* If we read a stub from an input file, use that one. */
121 if (go32exe_temp_stub
!= NULL
)
123 coff_data (abfd
)->stub
= bfd_alloc (abfd
,
124 go32exe_temp_stub_size
);
125 if (coff_data (abfd
)->stub
== NULL
)
127 memcpy (coff_data (abfd
)->stub
, go32exe_temp_stub
,
128 go32exe_temp_stub_size
);
129 coff_data (abfd
)->stub_size
= go32exe_temp_stub_size
;
130 free (go32exe_temp_stub
);
131 go32exe_temp_stub
= NULL
;
132 go32exe_temp_stub_size
= 0;
136 /* Check at first the environment variable $(GO32STUB). */
137 stub
= getenv ("GO32STUB");
138 /* Now check the environment variable $(STUB). */
140 stub
= getenv ("STUB");
143 if (stat (stub
, &st
) != 0)
146 f
= open (stub
, O_RDONLY
| O_BINARY
);
148 f
= open (stub
, O_RDONLY
);
152 if (read (f
, &header
, sizeof (header
)) < 0)
157 if (_H (0) != 0x5a4d) /* It is not an exe file. */
162 /* Compute the size of the stub (it is every thing up
163 to the beginning of the coff image). */
164 coff_start
= (long) _H (2) * 512L;
166 coff_start
+= (long) _H (1) - 512L;
168 exe_start
= _H (4) * 16;
169 if ((long) lseek (f
, exe_start
, SEEK_SET
) != exe_start
)
174 if (read (f
, &magic
, 8) != 8)
179 if (! startswith (magic
, "go32stub"))
184 /* Now we found a correct stub (hopefully). */
185 coff_data (abfd
)->stub
= bfd_alloc (abfd
, (bfd_size_type
) coff_start
);
186 if (coff_data (abfd
)->stub
== NULL
)
191 lseek (f
, 0L, SEEK_SET
);
192 if ((unsigned long) read (f
, coff_data (abfd
)->stub
, coff_start
)
195 bfd_release (abfd
, coff_data (abfd
)->stub
);
196 coff_data (abfd
)->stub
= NULL
;
199 coff_data (abfd
)->stub_size
= coff_start
;
203 /* There was something wrong above, so use now the standard builtin
205 if (coff_data (abfd
)->stub
== NULL
)
207 coff_data (abfd
)->stub
208 = bfd_alloc (abfd
, (bfd_size_type
) GO32EXE_DEFAULT_STUB_SIZE
);
209 if (coff_data (abfd
)->stub
== NULL
)
211 memcpy (coff_data (abfd
)->stub
, go32exe_default_stub
,
212 GO32EXE_DEFAULT_STUB_SIZE
);
213 coff_data (abfd
)->stub_size
= GO32EXE_DEFAULT_STUB_SIZE
;
217 /* If ibfd was a stubbed coff image, copy the stub from that bfd
221 go32exe_copy_private_bfd_data (bfd
*ibfd
, bfd
*obfd
)
223 /* Check if both are the same targets. */
224 if (ibfd
->xvec
!= obfd
->xvec
)
227 /* Make sure we have a source stub. */
228 BFD_ASSERT (coff_data (ibfd
)->stub
!= NULL
);
230 /* Reallocate the output stub if necessary. */
231 if (coff_data (ibfd
)->stub_size
> coff_data (obfd
)->stub_size
)
232 coff_data (obfd
)->stub
= bfd_alloc (obfd
, coff_data (ibfd
)->stub_size
);
233 if (coff_data (obfd
)->stub
== NULL
)
236 /* Now copy the stub. */
237 memcpy (coff_data (obfd
)->stub
, coff_data (ibfd
)->stub
,
238 coff_data (ibfd
)->stub_size
);
239 coff_data (obfd
)->stub_size
= coff_data (ibfd
)->stub_size
;
240 obfd
->origin
= coff_data (obfd
)->stub_size
;
245 /* Cleanup function, returned from check_format hook. */
248 go32exe_cleanup (bfd
*abfd
)
251 coff_object_cleanup (abfd
);
253 free (go32exe_temp_stub
);
254 go32exe_temp_stub
= NULL
;
255 go32exe_temp_stub_size
= 0;
258 /* Check that there is a GO32 stub and read it to go32exe_temp_stub.
259 Then set abfd->origin so that the COFF image is read at the correct
263 go32exe_check_format (bfd
*abfd
)
265 struct external_DOS_hdr filehdr_dos
;
267 uint16_t last_page_size
;
269 bfd_size_type stubsize
;
271 /* This format can not appear in an archive. */
272 if (abfd
->origin
!= 0)
274 bfd_set_error (bfd_error_wrong_format
);
278 bfd_set_error (bfd_error_system_call
);
280 /* Read in the stub file header, which is a DOS MZ executable. */
281 if (bfd_read (&filehdr_dos
, DOS_HDR_SIZE
, abfd
) != DOS_HDR_SIZE
)
284 /* Make sure that this is an MZ executable. */
285 if (H_GET_16 (abfd
, filehdr_dos
.e_magic
) != IMAGE_DOS_SIGNATURE
)
288 /* Determine the size of the stub */
289 num_pages
= H_GET_16 (abfd
, filehdr_dos
.e_cp
);
290 last_page_size
= H_GET_16 (abfd
, filehdr_dos
.e_cblp
);
291 stubsize
= num_pages
* 512;
292 if (last_page_size
!= 0)
293 stubsize
+= last_page_size
- 512;
295 ufile_ptr filesize
= bfd_get_file_size (abfd
);
296 if (filesize
!= 0 && stubsize
> filesize
)
299 /* Save now the stub to be used later. Put the stub data to a temporary
300 location first as tdata still does not exist. It may not even
301 be ever created if we are just checking the file format of ABFD. */
302 if (bfd_seek (abfd
, 0, SEEK_SET
) != 0)
304 go32exe_temp_stub
= bfd_malloc (stubsize
);
305 if (go32exe_temp_stub
== NULL
)
307 if (bfd_read (go32exe_temp_stub
, stubsize
, abfd
) != stubsize
)
309 go32exe_temp_stub_size
= stubsize
;
311 /* Confirm that this is a go32stub. */
312 header_end
= H_GET_16 (abfd
, filehdr_dos
.e_cparhdr
) * 16UL;
313 if (go32exe_temp_stub_size
< header_end
314 || go32exe_temp_stub_size
- header_end
< sizeof "go32stub" - 1
315 || !startswith (go32exe_temp_stub
+ header_end
, "go32stub"))
318 /* Set origin to where the COFF header starts and seek there. */
319 abfd
->origin
= stubsize
;
320 if (bfd_seek (abfd
, 0, SEEK_SET
) != 0)
323 /* Call coff_object_p to read the COFF image. If this fails then the file
324 must be just a stub with no COFF data attached. */
325 bfd_cleanup cleanup
= coff_object_p (abfd
);
328 BFD_ASSERT (cleanup
== coff_object_cleanup
);
330 return go32exe_cleanup
;
333 bfd_set_error (bfd_error_wrong_format
);
335 go32exe_cleanup (abfd
);
339 /* Write the stub to the output file, then call coff_write_object_contents. */
342 go32exe_write_object_contents (bfd
*abfd
)
344 const bfd_size_type pos
= bfd_tell (abfd
);
345 const bfd_size_type stubsize
= coff_data (abfd
)->stub_size
;
347 BFD_ASSERT (stubsize
!= 0);
349 bfd_set_error (bfd_error_system_call
);
351 /* Write the stub. */
353 if (bfd_seek (abfd
, 0, SEEK_SET
) != 0)
355 if (bfd_write (coff_data (abfd
)->stub
, stubsize
, abfd
) != stubsize
)
358 /* Seek back to where we were. */
359 abfd
->origin
= stubsize
;
360 if (bfd_seek (abfd
, pos
, SEEK_SET
) != 0)
363 return coff_write_object_contents (abfd
);
366 /* mkobject hook. Called directly through bfd_set_format or via
367 coff_mkobject_hook etc from bfd_check_format. */
370 go32exe_mkobject (bfd
*abfd
)
372 /* Don't output to an archive. */
373 if (abfd
->my_archive
!= NULL
)
376 if (!_bfd_go32_mkobject (abfd
))
379 go32exe_create_stub (abfd
);
380 if (coff_data (abfd
)->stub
== NULL
)
382 bfd_release (abfd
, coff_data (abfd
));
385 abfd
->origin
= coff_data (abfd
)->stub_size
;