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