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