Automatic date update in version.in
[deliverable/binutils-gdb.git] / bfd / coff-stgo32.c
CommitLineData
a4f68544 1/* BFD back-end for Intel 386 COFF files (DJGPP variant with a stub).
b90efa5b 2 Copyright (C) 1997-2015 Free Software Foundation, Inc.
252b5132
RH
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
cd123cb7 9 the Free Software Foundation; either version 3 of the License, or
252b5132
RH
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
cd123cb7
NC
19 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
20 MA 02110-1301, USA. */
252b5132
RH
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 All the functions below are called by the corresponding functions
27 from coffswap.h.
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.
34
35 To be compatible with any existing executables I have fixed this
246178f2 36 here and NOT in the DJGPP startup code. */
252b5132 37
6d00b590 38#define TARGET_SYM i386_coff_go32stubbed_vec
252b5132
RH
39#define TARGET_NAME "coff-go32-exe"
40#define TARGET_UNDERSCORE '_'
41#define COFF_GO32_EXE
242eabea
ILT
42#define COFF_LONG_SECTION_NAMES
43#define COFF_SUPPORT_GNU_LINKONCE
a4f68544 44#define COFF_LONG_FILENAMES
252b5132 45
5dccc1dd
ILT
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"), \
a7bda527
DD
50 COFF_ALIGNMENT_FIELD_EMPTY, COFF_ALIGNMENT_FIELD_EMPTY, 4 }, \
51{ COFF_SECTION_NAME_PARTIAL_MATCH (".debug"), \
26cd54bf 52 COFF_ALIGNMENT_FIELD_EMPTY, COFF_ALIGNMENT_FIELD_EMPTY, 0 }, \
4f6fbb1a 53{ COFF_SECTION_NAME_PARTIAL_MATCH (".gnu.linkonce.wi"), \
a7bda527 54 COFF_ALIGNMENT_FIELD_EMPTY, COFF_ALIGNMENT_FIELD_EMPTY, 0 }
5dccc1dd 55
691bf19c 56#include "sysdep.h"
252b5132
RH
57#include "bfd.h"
58
2c3fc389
NC
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. */
252b5132
RH
62
63static void
2c3fc389 64adjust_filehdr_in_post (bfd *, void *, void *);
252b5132 65static void
2c3fc389 66adjust_filehdr_out_pre (bfd *, void *, void *);
252b5132 67static void
2c3fc389 68adjust_filehdr_out_post (bfd *, void *, void *);
252b5132 69static void
2c3fc389 70adjust_scnhdr_in_post (bfd *, void *, void *);
252b5132 71static void
2c3fc389 72adjust_scnhdr_out_pre (bfd *, void *, void *);
252b5132 73static void
2c3fc389 74adjust_scnhdr_out_post (bfd *, void *, void *);
252b5132 75static void
2c3fc389 76adjust_aux_in_post (bfd *, void *, int, int, int, int, void *);
252b5132 77static void
2c3fc389 78adjust_aux_out_pre (bfd *, void *, int, int, int, int, void *);
252b5132 79static void
2c3fc389 80adjust_aux_out_post (bfd *, void *, int, int, int, int, void *);
252b5132 81static void
2c3fc389 82create_go32_stub (bfd *);
252b5132
RH
83
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
87
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
91
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
95
2c3fc389 96static const bfd_target *go32_check_format (bfd *);
156f6ad8
JK
97
98#define COFF_CHECK_FORMAT go32_check_format
99
b34976b6 100static bfd_boolean
2c3fc389 101 go32_stubbed_coff_bfd_copy_private_bfd_data (bfd *, bfd *);
252b5132
RH
102
103#define coff_bfd_copy_private_bfd_data go32_stubbed_coff_bfd_copy_private_bfd_data
104
105#include "coff-i386.c"
106
cc643b88 107/* This macro is used, because I cannot assume the endianness of the
246178f2 108 host system. */
cc643b88 109#define _H(index) (H_GET_16 (abfd, (header + index * 2)))
252b5132 110
252b5132 111/* These bytes are a 2048-byte DOS executable, which loads the COFF
246178f2 112 image into memory and then runs it. It is called 'stub'. */
252b5132 113
9bf609ec 114static const unsigned char stub_bytes[GO32_STUBSIZE] =
252b5132
RH
115{
116#include "go32stub.h"
117};
118
119/*
120 I have not commented each swap function below, because the
121 technique is in any function the same. For the ...in function,
9bf609ec 122 all the pointers are adjusted by adding GO32_STUBSIZE and for the
252b5132 123 ...out function, it is subtracted first and after calling the
246178f2 124 standard swap function it is reset to the old value. */
252b5132
RH
125
126/* This macro is used for adjusting the filepointers, which
246178f2 127 is done only, if the pointer is nonzero. */
252b5132
RH
128
129#define ADJUST_VAL(val,diff) \
130 if (val != 0) val += diff
131
132static void
2c3fc389
NC
133adjust_filehdr_in_post (bfd * abfd ATTRIBUTE_UNUSED,
134 void * src,
135 void * dst)
252b5132
RH
136{
137 FILHDR *filehdr_src = (FILHDR *) src;
138 struct internal_filehdr *filehdr_dst = (struct internal_filehdr *) dst;
139
9bf609ec 140 ADJUST_VAL (filehdr_dst->f_symptr, GO32_STUBSIZE);
252b5132 141
9bf609ec
JK
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;
252b5132
RH
147}
148
149static void
2c3fc389 150adjust_filehdr_out_pre (bfd * abfd, void * in, void * out)
252b5132
RH
151{
152 struct internal_filehdr *filehdr_in = (struct internal_filehdr *) in;
153 FILHDR *filehdr_out = (FILHDR *) out;
154
246178f2 155 /* Generate the stub. */
252b5132
RH
156 create_go32_stub (abfd);
157
246178f2 158 /* Copy the stub to the file header. */
9bf609ec
JK
159 if (coff_data (abfd)->go32stub != NULL)
160 memcpy (filehdr_out->stub, coff_data (abfd)->go32stub, GO32_STUBSIZE);
252b5132 161 else
246178f2 162 /* Use the default. */
9bf609ec 163 memcpy (filehdr_out->stub, stub_bytes, GO32_STUBSIZE);
252b5132 164
9bf609ec 165 ADJUST_VAL (filehdr_in->f_symptr, -GO32_STUBSIZE);
252b5132
RH
166}
167
168static void
2c3fc389
NC
169adjust_filehdr_out_post (bfd * abfd ATTRIBUTE_UNUSED,
170 void * in,
171 void * out ATTRIBUTE_UNUSED)
252b5132
RH
172{
173 struct internal_filehdr *filehdr_in = (struct internal_filehdr *) in;
246178f2 174 /* Undo the above change. */
9bf609ec 175 ADJUST_VAL (filehdr_in->f_symptr, GO32_STUBSIZE);
252b5132
RH
176}
177
178static void
2c3fc389
NC
179adjust_scnhdr_in_post (bfd * abfd ATTRIBUTE_UNUSED,
180 void * ext ATTRIBUTE_UNUSED,
181 void * in)
252b5132
RH
182{
183 struct internal_scnhdr *scnhdr_int = (struct internal_scnhdr *) in;
184
9bf609ec
JK
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);
252b5132
RH
188}
189
190static void
2c3fc389
NC
191adjust_scnhdr_out_pre (bfd * abfd ATTRIBUTE_UNUSED,
192 void * in,
193 void * out ATTRIBUTE_UNUSED)
252b5132
RH
194{
195 struct internal_scnhdr *scnhdr_int = (struct internal_scnhdr *) in;
196
9bf609ec
JK
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);
252b5132
RH
200}
201
202static void
2c3fc389
NC
203adjust_scnhdr_out_post (bfd * abfd ATTRIBUTE_UNUSED,
204 void * in,
205 void * out ATTRIBUTE_UNUSED)
252b5132
RH
206{
207 struct internal_scnhdr *scnhdr_int = (struct internal_scnhdr *) in;
208
9bf609ec
JK
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);
252b5132
RH
212}
213
214static void
2c3fc389
NC
215adjust_aux_in_post (bfd * abfd ATTRIBUTE_UNUSED,
216 void * ext1 ATTRIBUTE_UNUSED,
217 int type,
218 int in_class,
219 int indx ATTRIBUTE_UNUSED,
220 int numaux ATTRIBUTE_UNUSED,
221 void * in1)
252b5132
RH
222{
223 union internal_auxent *in = (union internal_auxent *) in1;
224
96d56e9f
NC
225 if (in_class == C_BLOCK || in_class == C_FCN || ISFCN (type)
226 || ISTAG (in_class))
252b5132 227 {
9bf609ec 228 ADJUST_VAL (in->x_sym.x_fcnary.x_fcn.x_lnnoptr, GO32_STUBSIZE);
252b5132
RH
229 }
230}
231
232static void
2c3fc389
NC
233adjust_aux_out_pre (bfd *abfd ATTRIBUTE_UNUSED,
234 void * inp,
235 int type,
236 int in_class,
237 int indx ATTRIBUTE_UNUSED,
238 int numaux ATTRIBUTE_UNUSED,
239 void * extp ATTRIBUTE_UNUSED)
252b5132
RH
240{
241 union internal_auxent *in = (union internal_auxent *) inp;
242
96d56e9f
NC
243 if (in_class == C_BLOCK || in_class == C_FCN || ISFCN (type)
244 || ISTAG (in_class))
252b5132 245 {
9bf609ec 246 ADJUST_VAL (in->x_sym.x_fcnary.x_fcn.x_lnnoptr, -GO32_STUBSIZE);
252b5132
RH
247 }
248}
249
250static void
2c3fc389
NC
251adjust_aux_out_post (bfd *abfd ATTRIBUTE_UNUSED,
252 void * inp,
253 int type,
254 int in_class,
255 int indx ATTRIBUTE_UNUSED,
256 int numaux ATTRIBUTE_UNUSED,
257 void * extp ATTRIBUTE_UNUSED)
252b5132
RH
258{
259 union internal_auxent *in = (union internal_auxent *) inp;
260
96d56e9f
NC
261 if (in_class == C_BLOCK || in_class == C_FCN || ISFCN (type)
262 || ISTAG (in_class))
252b5132 263 {
9bf609ec 264 ADJUST_VAL (in->x_sym.x_fcnary.x_fcn.x_lnnoptr, GO32_STUBSIZE);
252b5132
RH
265 }
266}
267
246178f2 268/* That's the function, which creates the stub. There are
252b5132
RH
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
275 file.
276
277 If there was any error, the standard stub (compiled in this file)
246178f2 278 is taken. */
252b5132
RH
279
280static void
2c3fc389 281create_go32_stub (bfd *abfd)
252b5132 282{
246178f2 283 /* Do it only once. */
9bf609ec 284 if (coff_data (abfd)->go32stub == NULL)
252b5132
RH
285 {
286 char *stub;
287 struct stat st;
288 int f;
289 unsigned char header[10];
290 char magic[8];
dc810e39
AM
291 unsigned long coff_start;
292 long exe_start;
252b5132 293
246178f2 294 /* Check at first the environment variable $(GO32STUB). */
252b5132 295 stub = getenv ("GO32STUB");
246178f2 296 /* Now check the environment variable $(STUB). */
252b5132
RH
297 if (stub == NULL)
298 stub = getenv ("STUB");
299 if (stub == NULL)
300 goto stub_end;
301 if (stat (stub, &st) != 0)
302 goto stub_end;
303#ifdef O_BINARY
304 f = open (stub, O_RDONLY | O_BINARY);
305#else
306 f = open (stub, O_RDONLY);
307#endif
308 if (f < 0)
309 goto stub_end;
310 if (read (f, &header, sizeof (header)) < 0)
311 {
312 close (f);
313 goto stub_end;
314 }
246178f2 315 if (_H (0) != 0x5a4d) /* It is not an exe file. */
252b5132
RH
316 {
317 close (f);
318 goto stub_end;
319 }
320 /* Compute the size of the stub (it is every thing up
246178f2 321 to the beginning of the coff image). */
252b5132
RH
322 coff_start = (long) _H (2) * 512L;
323 if (_H (1))
324 coff_start += (long) _H (1) - 512L;
325
326 /* Currently there is only a fixed stub size of 2048 bytes
246178f2 327 supported. */
252b5132
RH
328 if (coff_start != 2048)
329 {
330 close (f);
331 goto stub_end;
332 }
333 exe_start = _H (4) * 16;
dc810e39 334 if ((long) lseek (f, exe_start, SEEK_SET) != exe_start)
252b5132
RH
335 {
336 close (f);
337 goto stub_end;
338 }
339 if (read (f, &magic, 8) != 8)
340 {
341 close (f);
342 goto stub_end;
343 }
0112cd26 344 if (! CONST_STRNEQ (magic, "go32stub"))
252b5132
RH
345 {
346 close (f);
347 goto stub_end;
348 }
246178f2 349 /* Now we found a correct stub (hopefully). */
9bf609ec
JK
350 coff_data (abfd)->go32stub = bfd_alloc (abfd, (bfd_size_type) coff_start);
351 if (coff_data (abfd)->go32stub == NULL)
252b5132
RH
352 {
353 close (f);
354 return;
355 }
356 lseek (f, 0L, SEEK_SET);
9bf609ec 357 if ((unsigned long) read (f, coff_data (abfd)->go32stub, coff_start)
252b5132
RH
358 != coff_start)
359 {
9bf609ec
JK
360 bfd_release (abfd, coff_data (abfd)->go32stub);
361 coff_data (abfd)->go32stub = NULL;
252b5132
RH
362 }
363 close (f);
364 }
365stub_end:
366 /* There was something wrong above, so use now the standard builtin
246178f2 367 stub. */
9bf609ec 368 if (coff_data (abfd)->go32stub == NULL)
252b5132 369 {
9bf609ec
JK
370 coff_data (abfd)->go32stub
371 = bfd_alloc (abfd, (bfd_size_type) GO32_STUBSIZE);
372 if (coff_data (abfd)->go32stub == NULL)
246178f2 373 return;
9bf609ec 374 memcpy (coff_data (abfd)->go32stub, stub_bytes, GO32_STUBSIZE);
252b5132
RH
375 }
376}
377
378/* If ibfd was a stubbed coff image, copy the stub from that bfd
246178f2 379 to the new obfd. */
252b5132 380
b34976b6 381static bfd_boolean
2c3fc389 382go32_stubbed_coff_bfd_copy_private_bfd_data (bfd *ibfd, bfd *obfd)
252b5132 383{
246178f2 384 /* Check if both are the same targets. */
252b5132 385 if (ibfd->xvec != obfd->xvec)
b34976b6 386 return TRUE;
252b5132 387
e39e47bd 388 /* Check if we have a source stub. */
9bf609ec 389 if (coff_data (ibfd)->go32stub == NULL)
b34976b6 390 return TRUE;
252b5132 391
e39e47bd
JK
392 /* As adjust_filehdr_out_pre may get called only after this function,
393 optionally allocate the output stub. */
9bf609ec
JK
394 if (coff_data (obfd)->go32stub == NULL)
395 coff_data (obfd)->go32stub = bfd_alloc (obfd,
396 (bfd_size_type) GO32_STUBSIZE);
e39e47bd 397
246178f2 398 /* Now copy the stub. */
9bf609ec
JK
399 if (coff_data (obfd)->go32stub != NULL)
400 memcpy (coff_data (obfd)->go32stub, coff_data (ibfd)->go32stub,
401 GO32_STUBSIZE);
252b5132 402
b34976b6 403 return TRUE;
252b5132 404}
156f6ad8
JK
405
406/* coff_object_p only checks 2 bytes F_MAGIC at GO32_STUBSIZE inside the file
407 which is too fragile. */
408
409static const bfd_target *
410go32_check_format (bfd *abfd)
411{
412 char mz[2];
413
414 if (bfd_bread (mz, 2, abfd) != 2 || mz[0] != 'M' || mz[1] != 'Z')
415 {
416 bfd_set_error (bfd_error_wrong_format);
417 return NULL;
418 }
419
420 if (bfd_seek (abfd, 0, SEEK_SET) != 0)
421 return NULL;
422
423 return coff_object_p (abfd);
424}
This page took 0.661048 seconds and 4 git commands to generate.