d1be578c6aa3d1edbb5f71e29748ec402cb396ae
[deliverable/binutils-gdb.git] / bfd / coff-stgo32.c
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.
4
5 This file is part of BFD, the Binary File Descriptor library.
6
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.
11
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.
16
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. */
21
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.
25
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. */
30
31 #define TARGET_SYM i386_coff_go32stubbed_vec
32 #define TARGET_NAME "coff-go32-exe"
33 #define TARGET_UNDERSCORE '_'
34 #define COFF_GO32_EXE
35 #define COFF_LONG_SECTION_NAMES
36 #define COFF_SUPPORT_GNU_LINKONCE
37 #define COFF_LONG_FILENAMES
38
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 }
48
49 #include "sysdep.h"
50 #include "bfd.h"
51 #include "coff/msdos.h"
52
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 *);
57
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
62
63 #include "coff-i386.c"
64
65 /* This macro is used, because I cannot assume the endianness of the
66 host system. */
67 #define _H(index) (H_GET_16 (abfd, (header + index * 2)))
68
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] =
73 {
74 #include "go32stub.h"
75 };
76
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;
80
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
88 file.
89
90 If there was any error, the standard stub (compiled in this file)
91 is taken.
92
93 Ideally this function should exec '$(TARGET)-stubify' to generate
94 a stub, like gcc does. */
95
96 static void
97 go32exe_create_stub (bfd *abfd)
98 {
99 /* Do it only once. */
100 if (coff_data (abfd)->stub == NULL)
101 {
102 char *stub;
103 struct stat st;
104 int f;
105 unsigned char header[10];
106 char magic[8];
107 unsigned long coff_start;
108 long exe_start;
109
110 /* If we read a stub from an input file, use that one. */
111 if (go32exe_temp_stub != NULL)
112 {
113 coff_data (abfd)->stub = bfd_alloc (abfd,
114 go32exe_temp_stub_size);
115 if (coff_data (abfd)->stub == NULL)
116 return;
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;
123 return;
124 }
125
126 /* Check at first the environment variable $(GO32STUB). */
127 stub = getenv ("GO32STUB");
128 /* Now check the environment variable $(STUB). */
129 if (stub == NULL)
130 stub = getenv ("STUB");
131 if (stub == NULL)
132 goto stub_end;
133 if (stat (stub, &st) != 0)
134 goto stub_end;
135 #ifdef O_BINARY
136 f = open (stub, O_RDONLY | O_BINARY);
137 #else
138 f = open (stub, O_RDONLY);
139 #endif
140 if (f < 0)
141 goto stub_end;
142 if (read (f, &header, sizeof (header)) < 0)
143 {
144 close (f);
145 goto stub_end;
146 }
147 if (_H (0) != 0x5a4d) /* It is not an exe file. */
148 {
149 close (f);
150 goto stub_end;
151 }
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;
155 if (_H (1))
156 coff_start += (long) _H (1) - 512L;
157
158 exe_start = _H (4) * 16;
159 if ((long) lseek (f, exe_start, SEEK_SET) != exe_start)
160 {
161 close (f);
162 goto stub_end;
163 }
164 if (read (f, &magic, 8) != 8)
165 {
166 close (f);
167 goto stub_end;
168 }
169 if (! CONST_STRNEQ (magic, "go32stub"))
170 {
171 close (f);
172 goto stub_end;
173 }
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)
177 {
178 close (f);
179 return;
180 }
181 lseek (f, 0L, SEEK_SET);
182 if ((unsigned long) read (f, coff_data (abfd)->stub, coff_start)
183 != coff_start)
184 {
185 bfd_release (abfd, coff_data (abfd)->stub);
186 coff_data (abfd)->stub = NULL;
187 }
188 else
189 coff_data (abfd)->stub_size = coff_start;
190 close (f);
191 }
192 stub_end:
193 /* There was something wrong above, so use now the standard builtin
194 stub. */
195 if (coff_data (abfd)->stub == NULL)
196 {
197 coff_data (abfd)->stub
198 = bfd_alloc (abfd, (bfd_size_type) GO32EXE_DEFAULT_STUB_SIZE);
199 if (coff_data (abfd)->stub == NULL)
200 return;
201 memcpy (coff_data (abfd)->stub, go32exe_default_stub,
202 GO32EXE_DEFAULT_STUB_SIZE);
203 coff_data (abfd)->stub_size = GO32EXE_DEFAULT_STUB_SIZE;
204 }
205 }
206
207 /* If ibfd was a stubbed coff image, copy the stub from that bfd
208 to the new obfd. */
209
210 static bfd_boolean
211 go32exe_copy_private_bfd_data (bfd *ibfd, bfd *obfd)
212 {
213 /* Check if both are the same targets. */
214 if (ibfd->xvec != obfd->xvec)
215 return TRUE;
216
217 /* Make sure we have a source stub. */
218 BFD_ASSERT (coff_data (ibfd)->stub != NULL);
219
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)
224 return FALSE;
225
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;
231
232 return TRUE;
233 }
234
235 /* Cleanup function, returned from check_format hook. */
236
237 static void
238 go32exe_cleanup (bfd *abfd)
239 {
240 abfd->origin = 0;
241
242 if (go32exe_temp_stub != NULL)
243 free (go32exe_temp_stub);
244 go32exe_temp_stub = NULL;
245 go32exe_temp_stub_size = 0;
246 }
247
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
250 file offset. */
251
252 static bfd_cleanup
253 go32exe_check_format (bfd *abfd)
254 {
255 struct external_DOS_hdr filehdr_dos;
256 uint16_t num_pages;
257 uint16_t last_page_size;
258 uint32_t header_end;
259 bfd_size_type stubsize;
260
261 /* This format can not appear in an archive. */
262 if (abfd->origin != 0)
263 {
264 bfd_set_error (bfd_error_wrong_format);
265 return NULL;
266 }
267
268 bfd_set_error (bfd_error_system_call);
269
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)
272 goto fail;
273
274 /* Make sure that this is an MZ executable. */
275 if (H_GET_16 (abfd, filehdr_dos.e_magic) != IMAGE_DOS_SIGNATURE)
276 goto fail_format;
277
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;
284
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)
291 goto fail;
292 if (bfd_bread (go32exe_temp_stub, stubsize, abfd) != stubsize)
293 goto fail;
294 go32exe_temp_stub_size = stubsize;
295
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"))
299 goto fail_format;
300
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)
304 goto fail;
305
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);
309 if (cleanup == NULL)
310 goto fail;
311 BFD_ASSERT (cleanup == _bfd_no_cleanup);
312
313 return go32exe_cleanup;
314
315 fail_format:
316 bfd_set_error (bfd_error_wrong_format);
317 fail:
318 go32exe_cleanup (abfd);
319 return NULL;
320 }
321
322 /* Write the stub to the output file, then call coff_write_object_contents. */
323
324 static bfd_boolean
325 go32exe_write_object_contents (bfd *abfd)
326 {
327 const bfd_size_type pos = bfd_tell (abfd);
328 const bfd_size_type stubsize = coff_data (abfd)->stub_size;
329
330 BFD_ASSERT (stubsize != 0);
331
332 bfd_set_error (bfd_error_system_call);
333
334 /* Write the stub. */
335 abfd->origin = 0;
336 if (bfd_seek (abfd, 0, SEEK_SET) != 0)
337 return FALSE;
338 if (bfd_bwrite (coff_data (abfd)->stub, stubsize, abfd) != stubsize)
339 return FALSE;
340
341 /* Seek back to where we were. */
342 abfd->origin = stubsize;
343 if (bfd_seek (abfd, pos, SEEK_SET) != 0)
344 return FALSE;
345
346 return coff_write_object_contents (abfd);
347 }
348
349 /* mkobject hook. Called directly through bfd_set_format or via
350 coff_mkobject_hook etc from bfd_check_format. */
351
352 static bfd_boolean
353 go32exe_mkobject (bfd *abfd)
354 {
355 coff_data_type *coff = NULL;
356 const bfd_size_type amt = sizeof (coff_data_type);
357
358 /* Don't output to an archive. */
359 if (abfd->my_archive != NULL)
360 return FALSE;
361
362 abfd->tdata.coff_obj_data = bfd_zalloc (abfd, amt);
363 if (abfd->tdata.coff_obj_data == NULL)
364 return FALSE;
365 coff = coff_data (abfd);
366 coff->symbols = NULL;
367 coff->conversion_table = NULL;
368 coff->raw_syments = NULL;
369 coff->relocbase = 0;
370 coff->local_toc_sym_map = 0;
371
372 go32exe_create_stub (abfd);
373 if (coff->stub == NULL)
374 {
375 bfd_release (abfd, coff);
376 return FALSE;
377 }
378 abfd->origin = coff->stub_size;
379
380 /* make_abs_section(abfd);*/ /* ??? */
381
382 return TRUE;
383 }
This page took 0.056917 seconds and 4 git commands to generate.