PR binutils/13558
[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
246178f2 60/* At first the prototypes. */
252b5132
RH
61
62static void
246178f2 63adjust_filehdr_in_post PARAMS ((bfd *, PTR, PTR));
252b5132 64static void
246178f2 65adjust_filehdr_out_pre PARAMS ((bfd *, PTR, PTR));
252b5132 66static void
246178f2 67adjust_filehdr_out_post PARAMS ((bfd *, PTR, PTR));
252b5132 68static void
246178f2 69adjust_scnhdr_in_post PARAMS ((bfd *, PTR, PTR));
252b5132 70static void
246178f2 71adjust_scnhdr_out_pre PARAMS ((bfd *, PTR, PTR));
252b5132 72static void
246178f2 73adjust_scnhdr_out_post PARAMS ((bfd *, PTR, PTR));
252b5132 74static void
246178f2 75adjust_aux_in_post PARAMS ((bfd *, PTR, int, int, int, int, PTR));
252b5132 76static void
246178f2 77adjust_aux_out_pre PARAMS ((bfd *, PTR, int, int, int, int, PTR));
252b5132 78static void
246178f2 79adjust_aux_out_post PARAMS ((bfd *, PTR, int, int, int, int, PTR));
252b5132 80static void
246178f2 81create_go32_stub PARAMS ((bfd *));
252b5132 82
246178f2 83/* All that ..._PRE and ...POST functions are called from the corresponding
252b5132 84 coff_swap... functions. The ...PRE functions are called at the beginning
246178f2 85 of the function and the ...POST functions at the end of the swap routines. */
252b5132
RH
86
87#define COFF_ADJUST_FILEHDR_IN_POST adjust_filehdr_in_post
88#define COFF_ADJUST_FILEHDR_OUT_PRE adjust_filehdr_out_pre
89#define COFF_ADJUST_FILEHDR_OUT_POST adjust_filehdr_out_post
90
91#define COFF_ADJUST_SCNHDR_IN_POST adjust_scnhdr_in_post
92#define COFF_ADJUST_SCNHDR_OUT_PRE adjust_scnhdr_out_pre
93#define COFF_ADJUST_SCNHDR_OUT_POST adjust_scnhdr_out_post
94
95#define COFF_ADJUST_AUX_IN_POST adjust_aux_in_post
96#define COFF_ADJUST_AUX_OUT_PRE adjust_aux_out_pre
97#define COFF_ADJUST_AUX_OUT_POST adjust_aux_out_post
98
156f6ad8
JK
99static const bfd_target *go32_check_format (bfd *abfd);
100
101#define COFF_CHECK_FORMAT go32_check_format
102
b34976b6 103static bfd_boolean
246178f2 104 go32_stubbed_coff_bfd_copy_private_bfd_data PARAMS ((bfd *, bfd *));
252b5132
RH
105
106#define coff_bfd_copy_private_bfd_data go32_stubbed_coff_bfd_copy_private_bfd_data
107
108#include "coff-i386.c"
109
cc643b88 110/* This macro is used, because I cannot assume the endianness of the
246178f2 111 host system. */
cc643b88 112#define _H(index) (H_GET_16 (abfd, (header + index * 2)))
252b5132 113
252b5132 114/* These bytes are a 2048-byte DOS executable, which loads the COFF
246178f2 115 image into memory and then runs it. It is called 'stub'. */
252b5132 116
9bf609ec 117static const unsigned char stub_bytes[GO32_STUBSIZE] =
252b5132
RH
118{
119#include "go32stub.h"
120};
121
122/*
123 I have not commented each swap function below, because the
124 technique is in any function the same. For the ...in function,
9bf609ec 125 all the pointers are adjusted by adding GO32_STUBSIZE and for the
252b5132 126 ...out function, it is subtracted first and after calling the
246178f2 127 standard swap function it is reset to the old value. */
252b5132
RH
128
129/* This macro is used for adjusting the filepointers, which
246178f2 130 is done only, if the pointer is nonzero. */
252b5132
RH
131
132#define ADJUST_VAL(val,diff) \
133 if (val != 0) val += diff
134
135static void
136adjust_filehdr_in_post (abfd, src, dst)
9bf609ec 137 bfd *abfd ATTRIBUTE_UNUSED;
252b5132
RH
138 PTR src;
139 PTR dst;
140{
141 FILHDR *filehdr_src = (FILHDR *) src;
142 struct internal_filehdr *filehdr_dst = (struct internal_filehdr *) dst;
143
9bf609ec 144 ADJUST_VAL (filehdr_dst->f_symptr, GO32_STUBSIZE);
252b5132 145
9bf609ec
JK
146 /* Save now the stub to be used later. Put the stub data to FILEHDR_DST
147 first as coff_data (abfd) still does not exist. It may not even be ever
148 created as we are just checking the file format of ABFD. */
149 memcpy (filehdr_dst->go32stub, filehdr_src->stub, GO32_STUBSIZE);
150 filehdr_dst->f_flags |= F_GO32STUB;
252b5132
RH
151}
152
153static void
154adjust_filehdr_out_pre (abfd, in, out)
155 bfd *abfd;
156 PTR in;
157 PTR out;
158{
159 struct internal_filehdr *filehdr_in = (struct internal_filehdr *) in;
160 FILHDR *filehdr_out = (FILHDR *) out;
161
246178f2 162 /* Generate the stub. */
252b5132
RH
163 create_go32_stub (abfd);
164
246178f2 165 /* Copy the stub to the file header. */
9bf609ec
JK
166 if (coff_data (abfd)->go32stub != NULL)
167 memcpy (filehdr_out->stub, coff_data (abfd)->go32stub, GO32_STUBSIZE);
252b5132 168 else
246178f2 169 /* Use the default. */
9bf609ec 170 memcpy (filehdr_out->stub, stub_bytes, GO32_STUBSIZE);
252b5132 171
9bf609ec 172 ADJUST_VAL (filehdr_in->f_symptr, -GO32_STUBSIZE);
252b5132
RH
173}
174
175static void
176adjust_filehdr_out_post (abfd, in, out)
5f771d47 177 bfd *abfd ATTRIBUTE_UNUSED;
252b5132 178 PTR in;
5f771d47 179 PTR out ATTRIBUTE_UNUSED;
252b5132
RH
180{
181 struct internal_filehdr *filehdr_in = (struct internal_filehdr *) in;
246178f2 182 /* Undo the above change. */
9bf609ec 183 ADJUST_VAL (filehdr_in->f_symptr, GO32_STUBSIZE);
252b5132
RH
184}
185
186static void
187adjust_scnhdr_in_post (abfd, ext, in)
5f771d47
ILT
188 bfd *abfd ATTRIBUTE_UNUSED;
189 PTR ext ATTRIBUTE_UNUSED;
252b5132
RH
190 PTR in;
191{
192 struct internal_scnhdr *scnhdr_int = (struct internal_scnhdr *) in;
193
9bf609ec
JK
194 ADJUST_VAL (scnhdr_int->s_scnptr, GO32_STUBSIZE);
195 ADJUST_VAL (scnhdr_int->s_relptr, GO32_STUBSIZE);
196 ADJUST_VAL (scnhdr_int->s_lnnoptr, GO32_STUBSIZE);
252b5132
RH
197}
198
199static void
200adjust_scnhdr_out_pre (abfd, in, out)
5f771d47 201 bfd *abfd ATTRIBUTE_UNUSED;
252b5132 202 PTR in;
5f771d47 203 PTR out ATTRIBUTE_UNUSED;
252b5132
RH
204{
205 struct internal_scnhdr *scnhdr_int = (struct internal_scnhdr *) in;
206
9bf609ec
JK
207 ADJUST_VAL (scnhdr_int->s_scnptr, -GO32_STUBSIZE);
208 ADJUST_VAL (scnhdr_int->s_relptr, -GO32_STUBSIZE);
209 ADJUST_VAL (scnhdr_int->s_lnnoptr, -GO32_STUBSIZE);
252b5132
RH
210}
211
212static void
213adjust_scnhdr_out_post (abfd, in, out)
5f771d47 214 bfd *abfd ATTRIBUTE_UNUSED;
252b5132 215 PTR in;
5f771d47 216 PTR out ATTRIBUTE_UNUSED;
252b5132
RH
217{
218 struct internal_scnhdr *scnhdr_int = (struct internal_scnhdr *) in;
219
9bf609ec
JK
220 ADJUST_VAL (scnhdr_int->s_scnptr, GO32_STUBSIZE);
221 ADJUST_VAL (scnhdr_int->s_relptr, GO32_STUBSIZE);
222 ADJUST_VAL (scnhdr_int->s_lnnoptr, GO32_STUBSIZE);
252b5132
RH
223}
224
225static void
96d56e9f 226adjust_aux_in_post (abfd, ext1, type, in_class, indx, numaux, in1)
5f771d47
ILT
227 bfd *abfd ATTRIBUTE_UNUSED;
228 PTR ext1 ATTRIBUTE_UNUSED;
252b5132 229 int type;
96d56e9f 230 int in_class;
5f771d47
ILT
231 int indx ATTRIBUTE_UNUSED;
232 int numaux ATTRIBUTE_UNUSED;
252b5132
RH
233 PTR in1;
234{
235 union internal_auxent *in = (union internal_auxent *) in1;
236
96d56e9f
NC
237 if (in_class == C_BLOCK || in_class == C_FCN || ISFCN (type)
238 || ISTAG (in_class))
252b5132 239 {
9bf609ec 240 ADJUST_VAL (in->x_sym.x_fcnary.x_fcn.x_lnnoptr, GO32_STUBSIZE);
252b5132
RH
241 }
242}
243
244static void
96d56e9f 245adjust_aux_out_pre (abfd, inp, type, in_class, indx, numaux, extp)
5f771d47 246 bfd *abfd ATTRIBUTE_UNUSED;
252b5132
RH
247 PTR inp;
248 int type;
96d56e9f 249 int in_class;
5f771d47
ILT
250 int indx ATTRIBUTE_UNUSED;
251 int numaux ATTRIBUTE_UNUSED;
252 PTR extp ATTRIBUTE_UNUSED;
252b5132
RH
253{
254 union internal_auxent *in = (union internal_auxent *) inp;
255
96d56e9f
NC
256 if (in_class == C_BLOCK || in_class == C_FCN || ISFCN (type)
257 || ISTAG (in_class))
252b5132 258 {
9bf609ec 259 ADJUST_VAL (in->x_sym.x_fcnary.x_fcn.x_lnnoptr, -GO32_STUBSIZE);
252b5132
RH
260 }
261}
262
263static void
96d56e9f 264adjust_aux_out_post (abfd, inp, type, in_class, indx, numaux, extp)
5f771d47 265 bfd *abfd ATTRIBUTE_UNUSED;
252b5132
RH
266 PTR inp;
267 int type;
96d56e9f 268 int in_class;
5f771d47
ILT
269 int indx ATTRIBUTE_UNUSED;
270 int numaux ATTRIBUTE_UNUSED;
271 PTR extp ATTRIBUTE_UNUSED;
252b5132
RH
272{
273 union internal_auxent *in = (union internal_auxent *) inp;
274
96d56e9f
NC
275 if (in_class == C_BLOCK || in_class == C_FCN || ISFCN (type)
276 || ISTAG (in_class))
252b5132 277 {
9bf609ec 278 ADJUST_VAL (in->x_sym.x_fcnary.x_fcn.x_lnnoptr, GO32_STUBSIZE);
252b5132
RH
279 }
280}
281
246178f2 282/* That's the function, which creates the stub. There are
252b5132
RH
283 different cases from where the stub is taken.
284 At first the environment variable $(GO32STUB) is checked and then
285 $(STUB) if it was not set.
286 If it exists and points to a valid stub the stub is taken from
287 that file. This file can be also a whole executable file, because
288 the stub is computed from the exe information at the start of that
289 file.
290
291 If there was any error, the standard stub (compiled in this file)
246178f2 292 is taken. */
252b5132
RH
293
294static void
295create_go32_stub (abfd)
296 bfd *abfd;
297{
246178f2 298 /* Do it only once. */
9bf609ec 299 if (coff_data (abfd)->go32stub == NULL)
252b5132
RH
300 {
301 char *stub;
302 struct stat st;
303 int f;
304 unsigned char header[10];
305 char magic[8];
dc810e39
AM
306 unsigned long coff_start;
307 long exe_start;
252b5132 308
246178f2 309 /* Check at first the environment variable $(GO32STUB). */
252b5132 310 stub = getenv ("GO32STUB");
246178f2 311 /* Now check the environment variable $(STUB). */
252b5132
RH
312 if (stub == NULL)
313 stub = getenv ("STUB");
314 if (stub == NULL)
315 goto stub_end;
316 if (stat (stub, &st) != 0)
317 goto stub_end;
318#ifdef O_BINARY
319 f = open (stub, O_RDONLY | O_BINARY);
320#else
321 f = open (stub, O_RDONLY);
322#endif
323 if (f < 0)
324 goto stub_end;
325 if (read (f, &header, sizeof (header)) < 0)
326 {
327 close (f);
328 goto stub_end;
329 }
246178f2 330 if (_H (0) != 0x5a4d) /* It is not an exe file. */
252b5132
RH
331 {
332 close (f);
333 goto stub_end;
334 }
335 /* Compute the size of the stub (it is every thing up
246178f2 336 to the beginning of the coff image). */
252b5132
RH
337 coff_start = (long) _H (2) * 512L;
338 if (_H (1))
339 coff_start += (long) _H (1) - 512L;
340
341 /* Currently there is only a fixed stub size of 2048 bytes
246178f2 342 supported. */
252b5132
RH
343 if (coff_start != 2048)
344 {
345 close (f);
346 goto stub_end;
347 }
348 exe_start = _H (4) * 16;
dc810e39 349 if ((long) lseek (f, exe_start, SEEK_SET) != exe_start)
252b5132
RH
350 {
351 close (f);
352 goto stub_end;
353 }
354 if (read (f, &magic, 8) != 8)
355 {
356 close (f);
357 goto stub_end;
358 }
0112cd26 359 if (! CONST_STRNEQ (magic, "go32stub"))
252b5132
RH
360 {
361 close (f);
362 goto stub_end;
363 }
246178f2 364 /* Now we found a correct stub (hopefully). */
9bf609ec
JK
365 coff_data (abfd)->go32stub = bfd_alloc (abfd, (bfd_size_type) coff_start);
366 if (coff_data (abfd)->go32stub == NULL)
252b5132
RH
367 {
368 close (f);
369 return;
370 }
371 lseek (f, 0L, SEEK_SET);
9bf609ec 372 if ((unsigned long) read (f, coff_data (abfd)->go32stub, coff_start)
252b5132
RH
373 != coff_start)
374 {
9bf609ec
JK
375 bfd_release (abfd, coff_data (abfd)->go32stub);
376 coff_data (abfd)->go32stub = NULL;
252b5132
RH
377 }
378 close (f);
379 }
380stub_end:
381 /* There was something wrong above, so use now the standard builtin
246178f2 382 stub. */
9bf609ec 383 if (coff_data (abfd)->go32stub == NULL)
252b5132 384 {
9bf609ec
JK
385 coff_data (abfd)->go32stub
386 = bfd_alloc (abfd, (bfd_size_type) GO32_STUBSIZE);
387 if (coff_data (abfd)->go32stub == NULL)
246178f2 388 return;
9bf609ec 389 memcpy (coff_data (abfd)->go32stub, stub_bytes, GO32_STUBSIZE);
252b5132
RH
390 }
391}
392
393/* If ibfd was a stubbed coff image, copy the stub from that bfd
246178f2 394 to the new obfd. */
252b5132 395
b34976b6 396static bfd_boolean
252b5132
RH
397go32_stubbed_coff_bfd_copy_private_bfd_data (ibfd, obfd)
398 bfd *ibfd;
399 bfd *obfd;
400{
246178f2 401 /* Check if both are the same targets. */
252b5132 402 if (ibfd->xvec != obfd->xvec)
b34976b6 403 return TRUE;
252b5132 404
e39e47bd 405 /* Check if we have a source stub. */
9bf609ec 406 if (coff_data (ibfd)->go32stub == NULL)
b34976b6 407 return TRUE;
252b5132 408
e39e47bd
JK
409 /* As adjust_filehdr_out_pre may get called only after this function,
410 optionally allocate the output stub. */
9bf609ec
JK
411 if (coff_data (obfd)->go32stub == NULL)
412 coff_data (obfd)->go32stub = bfd_alloc (obfd,
413 (bfd_size_type) GO32_STUBSIZE);
e39e47bd 414
246178f2 415 /* Now copy the stub. */
9bf609ec
JK
416 if (coff_data (obfd)->go32stub != NULL)
417 memcpy (coff_data (obfd)->go32stub, coff_data (ibfd)->go32stub,
418 GO32_STUBSIZE);
252b5132 419
b34976b6 420 return TRUE;
252b5132 421}
156f6ad8
JK
422
423/* coff_object_p only checks 2 bytes F_MAGIC at GO32_STUBSIZE inside the file
424 which is too fragile. */
425
426static const bfd_target *
427go32_check_format (bfd *abfd)
428{
429 char mz[2];
430
431 if (bfd_bread (mz, 2, abfd) != 2 || mz[0] != 'M' || mz[1] != 'Z')
432 {
433 bfd_set_error (bfd_error_wrong_format);
434 return NULL;
435 }
436
437 if (bfd_seek (abfd, 0, SEEK_SET) != 0)
438 return NULL;
439
440 return coff_object_p (abfd);
441}
This page took 0.547557 seconds and 4 git commands to generate.