125b0d915a1974080d617a1deedc851c454e85c7
[deliverable/binutils-gdb.git] / bfd / coff-stgo32.c
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.
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
10 the Free Software Foundation; either version 3 of the License, or
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
20 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21 MA 02110-1301, USA. */
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
37 here and NOT in the DJGPP startup code. */
38
39 #define TARGET_SYM go32stubbedcoff_vec
40 #define TARGET_NAME "coff-go32-exe"
41 #define TARGET_UNDERSCORE '_'
42 #define COFF_GO32_EXE
43 #define COFF_LONG_SECTION_NAMES
44 #define COFF_SUPPORT_GNU_LINKONCE
45 #define COFF_LONG_FILENAMES
46
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 }
56
57 #include "sysdep.h"
58 #include "bfd.h"
59
60 /* At first the prototypes. */
61
62 static void
63 adjust_filehdr_in_post PARAMS ((bfd *, PTR, PTR));
64 static void
65 adjust_filehdr_out_pre PARAMS ((bfd *, PTR, PTR));
66 static void
67 adjust_filehdr_out_post PARAMS ((bfd *, PTR, PTR));
68 static void
69 adjust_scnhdr_in_post PARAMS ((bfd *, PTR, PTR));
70 static void
71 adjust_scnhdr_out_pre PARAMS ((bfd *, PTR, PTR));
72 static void
73 adjust_scnhdr_out_post PARAMS ((bfd *, PTR, PTR));
74 static void
75 adjust_aux_in_post PARAMS ((bfd *, PTR, int, int, int, int, PTR));
76 static void
77 adjust_aux_out_pre PARAMS ((bfd *, PTR, int, int, int, int, PTR));
78 static void
79 adjust_aux_out_post PARAMS ((bfd *, PTR, int, int, int, int, PTR));
80 static void
81 create_go32_stub PARAMS ((bfd *));
82
83 /* All that ..._PRE and ...POST functions are called from the corresponding
84 coff_swap... functions. The ...PRE functions are called at the beginning
85 of the function and the ...POST functions at the end of the swap routines. */
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
99 static const bfd_target *go32_check_format (bfd *abfd);
100
101 #define COFF_CHECK_FORMAT go32_check_format
102
103 static bfd_boolean
104 go32_stubbed_coff_bfd_copy_private_bfd_data PARAMS ((bfd *, bfd *));
105
106 #define coff_bfd_copy_private_bfd_data go32_stubbed_coff_bfd_copy_private_bfd_data
107
108 #include "coff-i386.c"
109
110 /* This macro is used, because I cannot assume the endianness of the
111 host system. */
112 #define _H(index) (H_GET_16 (abfd, (header + index * 2)))
113
114 /* These bytes are a 2048-byte DOS executable, which loads the COFF
115 image into memory and then runs it. It is called 'stub'. */
116
117 static const unsigned char stub_bytes[GO32_STUBSIZE] =
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,
125 all the pointers are adjusted by adding GO32_STUBSIZE and for the
126 ...out function, it is subtracted first and after calling the
127 standard swap function it is reset to the old value. */
128
129 /* This macro is used for adjusting the filepointers, which
130 is done only, if the pointer is nonzero. */
131
132 #define ADJUST_VAL(val,diff) \
133 if (val != 0) val += diff
134
135 static void
136 adjust_filehdr_in_post (abfd, src, dst)
137 bfd *abfd ATTRIBUTE_UNUSED;
138 PTR src;
139 PTR dst;
140 {
141 FILHDR *filehdr_src = (FILHDR *) src;
142 struct internal_filehdr *filehdr_dst = (struct internal_filehdr *) dst;
143
144 ADJUST_VAL (filehdr_dst->f_symptr, GO32_STUBSIZE);
145
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;
151 }
152
153 static void
154 adjust_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
162 /* Generate the stub. */
163 create_go32_stub (abfd);
164
165 /* Copy the stub to the file header. */
166 if (coff_data (abfd)->go32stub != NULL)
167 memcpy (filehdr_out->stub, coff_data (abfd)->go32stub, GO32_STUBSIZE);
168 else
169 /* Use the default. */
170 memcpy (filehdr_out->stub, stub_bytes, GO32_STUBSIZE);
171
172 ADJUST_VAL (filehdr_in->f_symptr, -GO32_STUBSIZE);
173 }
174
175 static void
176 adjust_filehdr_out_post (abfd, in, out)
177 bfd *abfd ATTRIBUTE_UNUSED;
178 PTR in;
179 PTR out ATTRIBUTE_UNUSED;
180 {
181 struct internal_filehdr *filehdr_in = (struct internal_filehdr *) in;
182 /* Undo the above change. */
183 ADJUST_VAL (filehdr_in->f_symptr, GO32_STUBSIZE);
184 }
185
186 static void
187 adjust_scnhdr_in_post (abfd, ext, in)
188 bfd *abfd ATTRIBUTE_UNUSED;
189 PTR ext ATTRIBUTE_UNUSED;
190 PTR in;
191 {
192 struct internal_scnhdr *scnhdr_int = (struct internal_scnhdr *) in;
193
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);
197 }
198
199 static void
200 adjust_scnhdr_out_pre (abfd, in, out)
201 bfd *abfd ATTRIBUTE_UNUSED;
202 PTR in;
203 PTR out ATTRIBUTE_UNUSED;
204 {
205 struct internal_scnhdr *scnhdr_int = (struct internal_scnhdr *) in;
206
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);
210 }
211
212 static void
213 adjust_scnhdr_out_post (abfd, in, out)
214 bfd *abfd ATTRIBUTE_UNUSED;
215 PTR in;
216 PTR out ATTRIBUTE_UNUSED;
217 {
218 struct internal_scnhdr *scnhdr_int = (struct internal_scnhdr *) in;
219
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);
223 }
224
225 static void
226 adjust_aux_in_post (abfd, ext1, type, in_class, indx, numaux, in1)
227 bfd *abfd ATTRIBUTE_UNUSED;
228 PTR ext1 ATTRIBUTE_UNUSED;
229 int type;
230 int in_class;
231 int indx ATTRIBUTE_UNUSED;
232 int numaux ATTRIBUTE_UNUSED;
233 PTR in1;
234 {
235 union internal_auxent *in = (union internal_auxent *) in1;
236
237 if (in_class == C_BLOCK || in_class == C_FCN || ISFCN (type)
238 || ISTAG (in_class))
239 {
240 ADJUST_VAL (in->x_sym.x_fcnary.x_fcn.x_lnnoptr, GO32_STUBSIZE);
241 }
242 }
243
244 static void
245 adjust_aux_out_pre (abfd, inp, type, in_class, indx, numaux, extp)
246 bfd *abfd ATTRIBUTE_UNUSED;
247 PTR inp;
248 int type;
249 int in_class;
250 int indx ATTRIBUTE_UNUSED;
251 int numaux ATTRIBUTE_UNUSED;
252 PTR extp ATTRIBUTE_UNUSED;
253 {
254 union internal_auxent *in = (union internal_auxent *) inp;
255
256 if (in_class == C_BLOCK || in_class == C_FCN || ISFCN (type)
257 || ISTAG (in_class))
258 {
259 ADJUST_VAL (in->x_sym.x_fcnary.x_fcn.x_lnnoptr, -GO32_STUBSIZE);
260 }
261 }
262
263 static void
264 adjust_aux_out_post (abfd, inp, type, in_class, indx, numaux, extp)
265 bfd *abfd ATTRIBUTE_UNUSED;
266 PTR inp;
267 int type;
268 int in_class;
269 int indx ATTRIBUTE_UNUSED;
270 int numaux ATTRIBUTE_UNUSED;
271 PTR extp ATTRIBUTE_UNUSED;
272 {
273 union internal_auxent *in = (union internal_auxent *) inp;
274
275 if (in_class == C_BLOCK || in_class == C_FCN || ISFCN (type)
276 || ISTAG (in_class))
277 {
278 ADJUST_VAL (in->x_sym.x_fcnary.x_fcn.x_lnnoptr, GO32_STUBSIZE);
279 }
280 }
281
282 /* That's the function, which creates the stub. There are
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)
292 is taken. */
293
294 static void
295 create_go32_stub (abfd)
296 bfd *abfd;
297 {
298 /* Do it only once. */
299 if (coff_data (abfd)->go32stub == NULL)
300 {
301 char *stub;
302 struct stat st;
303 int f;
304 unsigned char header[10];
305 char magic[8];
306 unsigned long coff_start;
307 long exe_start;
308
309 /* Check at first the environment variable $(GO32STUB). */
310 stub = getenv ("GO32STUB");
311 /* Now check the environment variable $(STUB). */
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 }
330 if (_H (0) != 0x5a4d) /* It is not an exe file. */
331 {
332 close (f);
333 goto stub_end;
334 }
335 /* Compute the size of the stub (it is every thing up
336 to the beginning of the coff image). */
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
342 supported. */
343 if (coff_start != 2048)
344 {
345 close (f);
346 goto stub_end;
347 }
348 exe_start = _H (4) * 16;
349 if ((long) lseek (f, exe_start, SEEK_SET) != exe_start)
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 }
359 if (! CONST_STRNEQ (magic, "go32stub"))
360 {
361 close (f);
362 goto stub_end;
363 }
364 /* Now we found a correct stub (hopefully). */
365 coff_data (abfd)->go32stub = bfd_alloc (abfd, (bfd_size_type) coff_start);
366 if (coff_data (abfd)->go32stub == NULL)
367 {
368 close (f);
369 return;
370 }
371 lseek (f, 0L, SEEK_SET);
372 if ((unsigned long) read (f, coff_data (abfd)->go32stub, coff_start)
373 != coff_start)
374 {
375 bfd_release (abfd, coff_data (abfd)->go32stub);
376 coff_data (abfd)->go32stub = NULL;
377 }
378 close (f);
379 }
380 stub_end:
381 /* There was something wrong above, so use now the standard builtin
382 stub. */
383 if (coff_data (abfd)->go32stub == NULL)
384 {
385 coff_data (abfd)->go32stub
386 = bfd_alloc (abfd, (bfd_size_type) GO32_STUBSIZE);
387 if (coff_data (abfd)->go32stub == NULL)
388 return;
389 memcpy (coff_data (abfd)->go32stub, stub_bytes, GO32_STUBSIZE);
390 }
391 }
392
393 /* If ibfd was a stubbed coff image, copy the stub from that bfd
394 to the new obfd. */
395
396 static bfd_boolean
397 go32_stubbed_coff_bfd_copy_private_bfd_data (ibfd, obfd)
398 bfd *ibfd;
399 bfd *obfd;
400 {
401 /* Check if both are the same targets. */
402 if (ibfd->xvec != obfd->xvec)
403 return TRUE;
404
405 /* Check if we have a source stub. */
406 if (coff_data (ibfd)->go32stub == NULL)
407 return TRUE;
408
409 /* As adjust_filehdr_out_pre may get called only after this function,
410 optionally allocate the output stub. */
411 if (coff_data (obfd)->go32stub == NULL)
412 coff_data (obfd)->go32stub = bfd_alloc (obfd,
413 (bfd_size_type) GO32_STUBSIZE);
414
415 /* Now copy the stub. */
416 if (coff_data (obfd)->go32stub != NULL)
417 memcpy (coff_data (obfd)->go32stub, coff_data (ibfd)->go32stub,
418 GO32_STUBSIZE);
419
420 return TRUE;
421 }
422
423 /* coff_object_p only checks 2 bytes F_MAGIC at GO32_STUBSIZE inside the file
424 which is too fragile. */
425
426 static const bfd_target *
427 go32_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.037749 seconds and 3 git commands to generate.