1
/* BFD back-end for Intel 386 COFF files (DJGPP variant with a stub).
2
Copyright 1997, 1998, 1999, 2000, 2001, 2002, 2005, 2006, 2007, 2009,
3
2011, 2012 Free Software Foundation, Inc.
4
Written by Robert Hoehne.
6
This file is part of BFD, the Binary File Descriptor library.
8
This program is free software; you can redistribute it and/or modify
9
it under the terms of the GNU General Public License as published by
10
the Free Software Foundation; either version 3 of the License, or
11
(at your option) any later version.
13
This program is distributed in the hope that it will be useful,
14
but WITHOUT ANY WARRANTY; without even the implied warranty of
15
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
GNU General Public License for more details.
18
You should have received a copy of the GNU General Public License
19
along with this program; if not, write to the Free Software
20
Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21
MA 02110-1301, USA. */
23
/* This file handles now also stubbed coff images. The stub is a small
24
DOS executable program before the coff image to load it in memory
25
and execute it. This is needed, because DOS cannot run coff files.
27
All the functions below are called by the corresponding functions
29
The only thing what they do is to adjust the information stored in
30
the COFF file which are offset into the file.
31
This is needed, because DJGPP uses a very special way to load and run
32
the coff image. It loads the image in memory and assumes then, that the
33
image had no stub by using the filepointers as pointers in the coff
34
image and NOT in the file.
36
To be compatible with any existing executables I have fixed this
37
here and NOT in the DJGPP startup code. */
39
#define TARGET_SYM go32stubbedcoff_vec
40
#define TARGET_NAME "coff-go32-exe"
41
#define TARGET_UNDERSCORE '_'
43
#define COFF_LONG_SECTION_NAMES
44
#define COFF_SUPPORT_GNU_LINKONCE
45
#define COFF_LONG_FILENAMES
47
#define COFF_SECTION_ALIGNMENT_ENTRIES \
48
{ COFF_SECTION_NAME_EXACT_MATCH (".data"), \
49
COFF_ALIGNMENT_FIELD_EMPTY, COFF_ALIGNMENT_FIELD_EMPTY, 4 }, \
50
{ COFF_SECTION_NAME_EXACT_MATCH (".text"), \
51
COFF_ALIGNMENT_FIELD_EMPTY, COFF_ALIGNMENT_FIELD_EMPTY, 4 }, \
52
{ COFF_SECTION_NAME_PARTIAL_MATCH (".debug"), \
53
COFF_ALIGNMENT_FIELD_EMPTY, COFF_ALIGNMENT_FIELD_EMPTY, 0 }, \
54
{ COFF_SECTION_NAME_PARTIAL_MATCH (".gnu.linkonce.wi"), \
55
COFF_ALIGNMENT_FIELD_EMPTY, COFF_ALIGNMENT_FIELD_EMPTY, 0 }
60
/* All that ..._PRE and ...POST functions are called from the corresponding
61
coff_swap... functions. The ...PRE functions are called at the beginning
62
of the function and the ...POST functions at the end of the swap routines. */
65
adjust_filehdr_in_post (bfd *, void *, void *);
67
adjust_filehdr_out_pre (bfd *, void *, void *);
69
adjust_filehdr_out_post (bfd *, void *, void *);
71
adjust_scnhdr_in_post (bfd *, void *, void *);
73
adjust_scnhdr_out_pre (bfd *, void *, void *);
75
adjust_scnhdr_out_post (bfd *, void *, void *);
77
adjust_aux_in_post (bfd *, void *, int, int, int, int, void *);
79
adjust_aux_out_pre (bfd *, void *, int, int, int, int, void *);
81
adjust_aux_out_post (bfd *, void *, int, int, int, int, void *);
83
create_go32_stub (bfd *);
85
#define COFF_ADJUST_FILEHDR_IN_POST adjust_filehdr_in_post
86
#define COFF_ADJUST_FILEHDR_OUT_PRE adjust_filehdr_out_pre
87
#define COFF_ADJUST_FILEHDR_OUT_POST adjust_filehdr_out_post
89
#define COFF_ADJUST_SCNHDR_IN_POST adjust_scnhdr_in_post
90
#define COFF_ADJUST_SCNHDR_OUT_PRE adjust_scnhdr_out_pre
91
#define COFF_ADJUST_SCNHDR_OUT_POST adjust_scnhdr_out_post
93
#define COFF_ADJUST_AUX_IN_POST adjust_aux_in_post
94
#define COFF_ADJUST_AUX_OUT_PRE adjust_aux_out_pre
95
#define COFF_ADJUST_AUX_OUT_POST adjust_aux_out_post
97
static const bfd_target *go32_check_format (bfd *);
99
#define COFF_CHECK_FORMAT go32_check_format
102
go32_stubbed_coff_bfd_copy_private_bfd_data (bfd *, bfd *);
104
#define coff_bfd_copy_private_bfd_data go32_stubbed_coff_bfd_copy_private_bfd_data
106
#include "coff-i386.c"
108
/* This macro is used, because I cannot assume the endianness of the
110
#define _H(index) (H_GET_16 (abfd, (header + index * 2)))
112
/* These bytes are a 2048-byte DOS executable, which loads the COFF
113
image into memory and then runs it. It is called 'stub'. */
115
static const unsigned char stub_bytes[GO32_STUBSIZE] =
117
#include "go32stub.h"
121
I have not commented each swap function below, because the
122
technique is in any function the same. For the ...in function,
123
all the pointers are adjusted by adding GO32_STUBSIZE and for the
124
...out function, it is subtracted first and after calling the
125
standard swap function it is reset to the old value. */
127
/* This macro is used for adjusting the filepointers, which
128
is done only, if the pointer is nonzero. */
130
#define ADJUST_VAL(val,diff) \
131
if (val != 0) val += diff
134
adjust_filehdr_in_post (bfd * abfd ATTRIBUTE_UNUSED,
138
FILHDR *filehdr_src = (FILHDR *) src;
139
struct internal_filehdr *filehdr_dst = (struct internal_filehdr *) dst;
141
ADJUST_VAL (filehdr_dst->f_symptr, GO32_STUBSIZE);
143
/* Save now the stub to be used later. Put the stub data to FILEHDR_DST
144
first as coff_data (abfd) still does not exist. It may not even be ever
145
created as we are just checking the file format of ABFD. */
146
memcpy (filehdr_dst->go32stub, filehdr_src->stub, GO32_STUBSIZE);
147
filehdr_dst->f_flags |= F_GO32STUB;
151
adjust_filehdr_out_pre (bfd * abfd, void * in, void * out)
153
struct internal_filehdr *filehdr_in = (struct internal_filehdr *) in;
154
FILHDR *filehdr_out = (FILHDR *) out;
156
/* Generate the stub. */
157
create_go32_stub (abfd);
159
/* Copy the stub to the file header. */
160
if (coff_data (abfd)->go32stub != NULL)
161
memcpy (filehdr_out->stub, coff_data (abfd)->go32stub, GO32_STUBSIZE);
163
/* Use the default. */
164
memcpy (filehdr_out->stub, stub_bytes, GO32_STUBSIZE);
166
ADJUST_VAL (filehdr_in->f_symptr, -GO32_STUBSIZE);
170
adjust_filehdr_out_post (bfd * abfd ATTRIBUTE_UNUSED,
172
void * out ATTRIBUTE_UNUSED)
174
struct internal_filehdr *filehdr_in = (struct internal_filehdr *) in;
175
/* Undo the above change. */
176
ADJUST_VAL (filehdr_in->f_symptr, GO32_STUBSIZE);
180
adjust_scnhdr_in_post (bfd * abfd ATTRIBUTE_UNUSED,
181
void * ext ATTRIBUTE_UNUSED,
184
struct internal_scnhdr *scnhdr_int = (struct internal_scnhdr *) in;
186
ADJUST_VAL (scnhdr_int->s_scnptr, GO32_STUBSIZE);
187
ADJUST_VAL (scnhdr_int->s_relptr, GO32_STUBSIZE);
188
ADJUST_VAL (scnhdr_int->s_lnnoptr, GO32_STUBSIZE);
192
adjust_scnhdr_out_pre (bfd * abfd ATTRIBUTE_UNUSED,
194
void * out ATTRIBUTE_UNUSED)
196
struct internal_scnhdr *scnhdr_int = (struct internal_scnhdr *) in;
198
ADJUST_VAL (scnhdr_int->s_scnptr, -GO32_STUBSIZE);
199
ADJUST_VAL (scnhdr_int->s_relptr, -GO32_STUBSIZE);
200
ADJUST_VAL (scnhdr_int->s_lnnoptr, -GO32_STUBSIZE);
204
adjust_scnhdr_out_post (bfd * abfd ATTRIBUTE_UNUSED,
206
void * out ATTRIBUTE_UNUSED)
208
struct internal_scnhdr *scnhdr_int = (struct internal_scnhdr *) in;
210
ADJUST_VAL (scnhdr_int->s_scnptr, GO32_STUBSIZE);
211
ADJUST_VAL (scnhdr_int->s_relptr, GO32_STUBSIZE);
212
ADJUST_VAL (scnhdr_int->s_lnnoptr, GO32_STUBSIZE);
216
adjust_aux_in_post (bfd * abfd ATTRIBUTE_UNUSED,
217
void * ext1 ATTRIBUTE_UNUSED,
220
int indx ATTRIBUTE_UNUSED,
221
int numaux ATTRIBUTE_UNUSED,
224
union internal_auxent *in = (union internal_auxent *) in1;
226
if (in_class == C_BLOCK || in_class == C_FCN || ISFCN (type)
229
ADJUST_VAL (in->x_sym.x_fcnary.x_fcn.x_lnnoptr, GO32_STUBSIZE);
234
adjust_aux_out_pre (bfd *abfd ATTRIBUTE_UNUSED,
238
int indx ATTRIBUTE_UNUSED,
239
int numaux ATTRIBUTE_UNUSED,
240
void * extp ATTRIBUTE_UNUSED)
242
union internal_auxent *in = (union internal_auxent *) inp;
244
if (in_class == C_BLOCK || in_class == C_FCN || ISFCN (type)
247
ADJUST_VAL (in->x_sym.x_fcnary.x_fcn.x_lnnoptr, -GO32_STUBSIZE);
252
adjust_aux_out_post (bfd *abfd ATTRIBUTE_UNUSED,
256
int indx ATTRIBUTE_UNUSED,
257
int numaux ATTRIBUTE_UNUSED,
258
void * extp ATTRIBUTE_UNUSED)
260
union internal_auxent *in = (union internal_auxent *) inp;
262
if (in_class == C_BLOCK || in_class == C_FCN || ISFCN (type)
265
ADJUST_VAL (in->x_sym.x_fcnary.x_fcn.x_lnnoptr, GO32_STUBSIZE);
269
/* That's the function, which creates the stub. There are
270
different cases from where the stub is taken.
271
At first the environment variable $(GO32STUB) is checked and then
272
$(STUB) if it was not set.
273
If it exists and points to a valid stub the stub is taken from
274
that file. This file can be also a whole executable file, because
275
the stub is computed from the exe information at the start of that
278
If there was any error, the standard stub (compiled in this file)
282
create_go32_stub (bfd *abfd)
284
/* Do it only once. */
285
if (coff_data (abfd)->go32stub == NULL)
290
unsigned char header[10];
292
unsigned long coff_start;
295
/* Check at first the environment variable $(GO32STUB). */
296
stub = getenv ("GO32STUB");
297
/* Now check the environment variable $(STUB). */
299
stub = getenv ("STUB");
302
if (stat (stub, &st) != 0)
305
f = open (stub, O_RDONLY | O_BINARY);
307
f = open (stub, O_RDONLY);
311
if (read (f, &header, sizeof (header)) < 0)
316
if (_H (0) != 0x5a4d) /* It is not an exe file. */
321
/* Compute the size of the stub (it is every thing up
322
to the beginning of the coff image). */
323
coff_start = (long) _H (2) * 512L;
325
coff_start += (long) _H (1) - 512L;
327
/* Currently there is only a fixed stub size of 2048 bytes
329
if (coff_start != 2048)
334
exe_start = _H (4) * 16;
335
if ((long) lseek (f, exe_start, SEEK_SET) != exe_start)
340
if (read (f, &magic, 8) != 8)
345
if (! CONST_STRNEQ (magic, "go32stub"))
350
/* Now we found a correct stub (hopefully). */
351
coff_data (abfd)->go32stub = bfd_alloc (abfd, (bfd_size_type) coff_start);
352
if (coff_data (abfd)->go32stub == NULL)
357
lseek (f, 0L, SEEK_SET);
358
if ((unsigned long) read (f, coff_data (abfd)->go32stub, coff_start)
361
bfd_release (abfd, coff_data (abfd)->go32stub);
362
coff_data (abfd)->go32stub = NULL;
367
/* There was something wrong above, so use now the standard builtin
369
if (coff_data (abfd)->go32stub == NULL)
371
coff_data (abfd)->go32stub
372
= bfd_alloc (abfd, (bfd_size_type) GO32_STUBSIZE);
373
if (coff_data (abfd)->go32stub == NULL)
375
memcpy (coff_data (abfd)->go32stub, stub_bytes, GO32_STUBSIZE);
379
/* If ibfd was a stubbed coff image, copy the stub from that bfd
383
go32_stubbed_coff_bfd_copy_private_bfd_data (bfd *ibfd, bfd *obfd)
385
/* Check if both are the same targets. */
386
if (ibfd->xvec != obfd->xvec)
389
/* Check if we have a source stub. */
390
if (coff_data (ibfd)->go32stub == NULL)
393
/* As adjust_filehdr_out_pre may get called only after this function,
394
optionally allocate the output stub. */
395
if (coff_data (obfd)->go32stub == NULL)
396
coff_data (obfd)->go32stub = bfd_alloc (obfd,
397
(bfd_size_type) GO32_STUBSIZE);
399
/* Now copy the stub. */
400
if (coff_data (obfd)->go32stub != NULL)
401
memcpy (coff_data (obfd)->go32stub, coff_data (ibfd)->go32stub,
407
/* coff_object_p only checks 2 bytes F_MAGIC at GO32_STUBSIZE inside the file
408
which is too fragile. */
410
static const bfd_target *
411
go32_check_format (bfd *abfd)
415
if (bfd_bread (mz, 2, abfd) != 2 || mz[0] != 'M' || mz[1] != 'Z')
417
bfd_set_error (bfd_error_wrong_format);
421
if (bfd_seek (abfd, 0, SEEK_SET) != 0)
424
return coff_object_p (abfd);