1999-07-21 Mark Elbrecht <snowball3@bigfoot.com>
[deliverable/binutils-gdb.git] / bfd / coff-stgo32.c
CommitLineData
252b5132 1/* BFD back-end for Intel 386 COFF files (go32 variant with a stub).
5f771d47 2 Copyright 1997, 1998, 1999 Free Software Foundation, Inc.
252b5132
RH
3 Written by Robert Hoehne.
4
5 This file is part of BFD, the Binary File Descriptor library.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
20
21/* This file handles now also stubbed coff images. The stub is a small
22 DOS executable program before the coff image to load it in memory
23 and execute it. This is needed, because DOS cannot run coff files.
24
25 All the functions below are called by the corresponding functions
26 from coffswap.h.
27 The only thing what they do is to adjust the information stored in
28 the COFF file which are offset into the file.
29 This is needed, because DJGPP uses a very special way to load and run
30 the coff image. It loads the image in memory and assumes then, that the
31 image had no stub by using the filepointers as pointers in the coff
32 image and NOT in the file.
33
34 To be compatible with any existing executables I have fixed this
35 here and NOT in the DJGPP startup code.
36 */
37
38#define TARGET_SYM go32stubbedcoff_vec
39#define TARGET_NAME "coff-go32-exe"
40#define TARGET_UNDERSCORE '_'
41#define COFF_GO32_EXE
242eabea
ILT
42#define COFF_LONG_SECTION_NAMES
43#define COFF_SUPPORT_GNU_LINKONCE
252b5132
RH
44
45#include "bfd.h"
46
47/* At first the prototypes */
48
49static void
50adjust_filehdr_in_post PARAMS ((bfd * abfd, PTR src, PTR dst));
51static void
52adjust_filehdr_out_pre PARAMS ((bfd * abfd, PTR in, PTR out));
53static void
54adjust_filehdr_out_post PARAMS ((bfd * abfd, PTR in, PTR out));
55
56static void
57adjust_scnhdr_in_post PARAMS ((bfd * abfd, PTR ext, PTR in));
58static void
59adjust_scnhdr_out_pre PARAMS ((bfd * abfd, PTR in, PTR out));
60static void
61adjust_scnhdr_out_post PARAMS ((bfd * abfd, PTR in, PTR out));
62
63static void
64adjust_aux_in_post PARAMS ((bfd * abfd, PTR ext1, int type, int class, int indx,
65 int numaux, PTR in1));
66static void
67adjust_aux_out_pre PARAMS ((bfd * abfd, PTR inp, int type, int class, int indx,
68 int numaux, PTR extp));
69static void
70adjust_aux_out_post PARAMS ((bfd * abfd, PTR inp, int type, int class, int indx,
71 int numaux, PTR extp));
72
73static void
74create_go32_stub PARAMS ((bfd * abfd));
75
76/*
77 All that ..._PRE and ...POST functions are called from the corresponding
78 coff_swap... functions. The ...PRE functions are called at the beginning
79 of the function and the ...POST functions at the end of the swap routines.
80 */
81
82#define COFF_ADJUST_FILEHDR_IN_POST adjust_filehdr_in_post
83#define COFF_ADJUST_FILEHDR_OUT_PRE adjust_filehdr_out_pre
84#define COFF_ADJUST_FILEHDR_OUT_POST adjust_filehdr_out_post
85
86#define COFF_ADJUST_SCNHDR_IN_POST adjust_scnhdr_in_post
87#define COFF_ADJUST_SCNHDR_OUT_PRE adjust_scnhdr_out_pre
88#define COFF_ADJUST_SCNHDR_OUT_POST adjust_scnhdr_out_post
89
90#define COFF_ADJUST_AUX_IN_POST adjust_aux_in_post
91#define COFF_ADJUST_AUX_OUT_PRE adjust_aux_out_pre
92#define COFF_ADJUST_AUX_OUT_POST adjust_aux_out_post
93
94static boolean
95 go32_stubbed_coff_bfd_copy_private_bfd_data PARAMS ((bfd * ibfd, bfd * obfd));
96
97#define coff_bfd_copy_private_bfd_data go32_stubbed_coff_bfd_copy_private_bfd_data
98
99#include "coff-i386.c"
100
101/* I hold in the usrdata the stub */
102#define bfd_coff_go32stub bfd_usrdata
103
104/* This macro is used, because I cannot assume the endianess of the
105 host system */
106#define _H(index) (bfd_h_get_16(abfd, (bfd_byte *)(header+index*2)))
107
108/* This function checks if the bfd is a stubbed coff image */
109static const bfd_target *
110go32_stubbed_coff_object_p (abfd)
111 bfd *abfd;
112{
113 unsigned char header[10];
114 char magic[8];
115 unsigned long coff_start, exe_start;
116
117 if (bfd_read (&header, 1, sizeof (header), abfd) != sizeof (header))
118 {
119 if (bfd_get_error () != bfd_error_system_call)
120 bfd_set_error (bfd_error_wrong_format);
121 return 0;
122 }
123 if (_H (0) != 0x5a4d) /* it is not an exe file. maybe a coff-image */
124 {
125 if (bfd_get_error () != bfd_error_system_call)
126 bfd_set_error (bfd_error_wrong_format);
127 return 0;
128 }
129 coff_start = (long) _H (2) * 512L;
130 if (_H (1))
131 coff_start += (long) _H (1) - 512L;
132
133 /* We can handle only a stub with a length of STUBSIZE */
134 if (coff_start != STUBSIZE)
135 {
136 bfd_set_error (bfd_error_wrong_format);
137 return 0;
138 }
139 exe_start = _H (4) * 16;
140 if (bfd_seek (abfd, exe_start, SEEK_SET) != 0)
141 return 0;
142 if (bfd_read (&magic, 1, 8, abfd) != 8)
143 {
144 if (bfd_get_error () != bfd_error_system_call)
145 bfd_set_error (bfd_error_wrong_format);
146 return 0;
147 }
148 if (memcmp (magic, "go32stub", 8) != 0)
149 {
150 bfd_set_error (bfd_error_wrong_format);
151 return 0;
152 }
153 if (bfd_seek (abfd, 0, SEEK_SET) != 0)
154 return 0;
155
156 /* Call the normal COFF detection routine */
157 return coff_object_p (abfd);
158}
159
160/* These bytes are a 2048-byte DOS executable, which loads the COFF
161 image into memory and then runs it. It is called 'stub' */
162
163static unsigned char stub_bytes[STUBSIZE] =
164{
165#include "go32stub.h"
166};
167
168/*
169 I have not commented each swap function below, because the
170 technique is in any function the same. For the ...in function,
171 all the pointers are adjusted by adding STUBSIZE and for the
172 ...out function, it is subtracted first and after calling the
173 standard swap function it is reset to the old value */
174
175/* This macro is used for adjusting the filepointers, which
176 is done only, if the pointer is nonzero */
177
178#define ADJUST_VAL(val,diff) \
179 if (val != 0) val += diff
180
181static void
182adjust_filehdr_in_post (abfd, src, dst)
183 bfd *abfd;
184 PTR src;
185 PTR dst;
186{
187 FILHDR *filehdr_src = (FILHDR *) src;
188 struct internal_filehdr *filehdr_dst = (struct internal_filehdr *) dst;
189
190 ADJUST_VAL (filehdr_dst->f_symptr, STUBSIZE);
191
192 /* Save now the stub to be used later */
193 bfd_coff_go32stub (abfd) = (PTR) bfd_alloc (abfd, STUBSIZE);
194
195 /* Since this function returns no status, I do not set here
196 any bfd_error_...
197 That means, before the use of bfd_coff_go32stub (), this value
198 should be checked if it is != NULL */
199 if (bfd_coff_go32stub (abfd) == NULL)
200 return;
201 memcpy (bfd_coff_go32stub (abfd), filehdr_src->stub, STUBSIZE);
202}
203
204static void
205adjust_filehdr_out_pre (abfd, in, out)
206 bfd *abfd;
207 PTR in;
208 PTR out;
209{
210 struct internal_filehdr *filehdr_in = (struct internal_filehdr *) in;
211 FILHDR *filehdr_out = (FILHDR *) out;
212
213 /* Generate the stub */
214 create_go32_stub (abfd);
215
216 /* Copy the stub to the file header */
217 if (bfd_coff_go32stub (abfd) != NULL)
218 memcpy (filehdr_out->stub, bfd_coff_go32stub (abfd), STUBSIZE);
219 else
220 /* use the default */
221 memcpy (filehdr_out->stub, stub_bytes, STUBSIZE);
222
223 ADJUST_VAL (filehdr_in->f_symptr, -STUBSIZE);
224}
225
226static void
227adjust_filehdr_out_post (abfd, in, out)
5f771d47 228 bfd *abfd ATTRIBUTE_UNUSED;
252b5132 229 PTR in;
5f771d47 230 PTR out ATTRIBUTE_UNUSED;
252b5132
RH
231{
232 struct internal_filehdr *filehdr_in = (struct internal_filehdr *) in;
233 /* undo the above change */
234 ADJUST_VAL (filehdr_in->f_symptr, STUBSIZE);
235}
236
237static void
238adjust_scnhdr_in_post (abfd, ext, in)
5f771d47
ILT
239 bfd *abfd ATTRIBUTE_UNUSED;
240 PTR ext ATTRIBUTE_UNUSED;
252b5132
RH
241 PTR in;
242{
243 struct internal_scnhdr *scnhdr_int = (struct internal_scnhdr *) in;
244
245 ADJUST_VAL (scnhdr_int->s_scnptr, STUBSIZE);
246 ADJUST_VAL (scnhdr_int->s_relptr, STUBSIZE);
247 ADJUST_VAL (scnhdr_int->s_lnnoptr, STUBSIZE);
248}
249
250static void
251adjust_scnhdr_out_pre (abfd, in, out)
5f771d47 252 bfd *abfd ATTRIBUTE_UNUSED;
252b5132 253 PTR in;
5f771d47 254 PTR out ATTRIBUTE_UNUSED;
252b5132
RH
255{
256 struct internal_scnhdr *scnhdr_int = (struct internal_scnhdr *) in;
257
258 ADJUST_VAL (scnhdr_int->s_scnptr, -STUBSIZE);
259 ADJUST_VAL (scnhdr_int->s_relptr, -STUBSIZE);
260 ADJUST_VAL (scnhdr_int->s_lnnoptr, -STUBSIZE);
261}
262
263static void
264adjust_scnhdr_out_post (abfd, in, out)
5f771d47 265 bfd *abfd ATTRIBUTE_UNUSED;
252b5132 266 PTR in;
5f771d47 267 PTR out ATTRIBUTE_UNUSED;
252b5132
RH
268{
269 struct internal_scnhdr *scnhdr_int = (struct internal_scnhdr *) in;
270
271 ADJUST_VAL (scnhdr_int->s_scnptr, STUBSIZE);
272 ADJUST_VAL (scnhdr_int->s_relptr, STUBSIZE);
273 ADJUST_VAL (scnhdr_int->s_lnnoptr, STUBSIZE);
274}
275
276static void
277adjust_aux_in_post (abfd, ext1, type, class, indx, numaux, in1)
5f771d47
ILT
278 bfd *abfd ATTRIBUTE_UNUSED;
279 PTR ext1 ATTRIBUTE_UNUSED;
252b5132
RH
280 int type;
281 int class;
5f771d47
ILT
282 int indx ATTRIBUTE_UNUSED;
283 int numaux ATTRIBUTE_UNUSED;
252b5132
RH
284 PTR in1;
285{
286 union internal_auxent *in = (union internal_auxent *) in1;
287
288 if (class == C_BLOCK || class == C_FCN || ISFCN (type) || ISTAG (class))
289 {
290 ADJUST_VAL (in->x_sym.x_fcnary.x_fcn.x_lnnoptr, STUBSIZE);
291 }
292}
293
294static void
295adjust_aux_out_pre (abfd, inp, type, class, indx, numaux, extp)
5f771d47 296 bfd *abfd ATTRIBUTE_UNUSED;
252b5132
RH
297 PTR inp;
298 int type;
299 int class;
5f771d47
ILT
300 int indx ATTRIBUTE_UNUSED;
301 int numaux ATTRIBUTE_UNUSED;
302 PTR extp ATTRIBUTE_UNUSED;
252b5132
RH
303{
304 union internal_auxent *in = (union internal_auxent *) inp;
305
306 if (class == C_BLOCK || class == C_FCN || ISFCN (type) || ISTAG (class))
307 {
308 ADJUST_VAL (in->x_sym.x_fcnary.x_fcn.x_lnnoptr, -STUBSIZE);
309 }
310}
311
312static void
313adjust_aux_out_post (abfd, inp, type, class, indx, numaux, extp)
5f771d47 314 bfd *abfd ATTRIBUTE_UNUSED;
252b5132
RH
315 PTR inp;
316 int type;
317 int class;
5f771d47
ILT
318 int indx ATTRIBUTE_UNUSED;
319 int numaux ATTRIBUTE_UNUSED;
320 PTR extp ATTRIBUTE_UNUSED;
252b5132
RH
321{
322 union internal_auxent *in = (union internal_auxent *) inp;
323
324 if (class == C_BLOCK || class == C_FCN || ISFCN (type) || ISTAG (class))
325 {
326 ADJUST_VAL (in->x_sym.x_fcnary.x_fcn.x_lnnoptr, STUBSIZE);
327 }
328}
329
330/*
331 That's the function, which creates the stub. There are
332 different cases from where the stub is taken.
333 At first the environment variable $(GO32STUB) is checked and then
334 $(STUB) if it was not set.
335 If it exists and points to a valid stub the stub is taken from
336 that file. This file can be also a whole executable file, because
337 the stub is computed from the exe information at the start of that
338 file.
339
340 If there was any error, the standard stub (compiled in this file)
341 is taken.
342 */
343
344static void
345create_go32_stub (abfd)
346 bfd *abfd;
347{
348 /* Do it only once */
349 if (bfd_coff_go32stub (abfd) == NULL)
350 {
351 char *stub;
352 struct stat st;
353 int f;
354 unsigned char header[10];
355 char magic[8];
356 unsigned long coff_start, exe_start;
357
358 /* Check at first the environment variable $(GO32STUB) */
359 stub = getenv ("GO32STUB");
360 /* Now check the environment variable $(STUB) */
361 if (stub == NULL)
362 stub = getenv ("STUB");
363 if (stub == NULL)
364 goto stub_end;
365 if (stat (stub, &st) != 0)
366 goto stub_end;
367#ifdef O_BINARY
368 f = open (stub, O_RDONLY | O_BINARY);
369#else
370 f = open (stub, O_RDONLY);
371#endif
372 if (f < 0)
373 goto stub_end;
374 if (read (f, &header, sizeof (header)) < 0)
375 {
376 close (f);
377 goto stub_end;
378 }
379 if (_H (0) != 0x5a4d) /* it is not an exe file */
380 {
381 close (f);
382 goto stub_end;
383 }
384 /* Compute the size of the stub (it is every thing up
385 to the beginning of the coff image) */
386 coff_start = (long) _H (2) * 512L;
387 if (_H (1))
388 coff_start += (long) _H (1) - 512L;
389
390 /* Currently there is only a fixed stub size of 2048 bytes
391 supported */
392 if (coff_start != 2048)
393 {
394 close (f);
395 goto stub_end;
396 }
397 exe_start = _H (4) * 16;
398 if ((unsigned long) lseek (f, exe_start, SEEK_SET) != exe_start)
399 {
400 close (f);
401 goto stub_end;
402 }
403 if (read (f, &magic, 8) != 8)
404 {
405 close (f);
406 goto stub_end;
407 }
408 if (memcmp (magic, "go32stub", 8) != 0)
409 {
410 close (f);
411 goto stub_end;
412 }
413 /* Now we found a correct stub (hopefully) */
414 bfd_coff_go32stub (abfd) = (PTR) bfd_alloc (abfd, coff_start);
415 if (bfd_coff_go32stub (abfd) == NULL)
416 {
417 close (f);
418 return;
419 }
420 lseek (f, 0L, SEEK_SET);
421 if ((unsigned long) read (f, bfd_coff_go32stub (abfd), coff_start)
422 != coff_start)
423 {
424 bfd_release (abfd, bfd_coff_go32stub (abfd));
425 bfd_coff_go32stub (abfd) = NULL;
426 }
427 close (f);
428 }
429stub_end:
430 /* There was something wrong above, so use now the standard builtin
431 stub */
432 if (bfd_coff_go32stub (abfd) == NULL)
433 {
434 bfd_coff_go32stub (abfd) = (PTR) bfd_alloc (abfd, STUBSIZE);
435 if (bfd_coff_go32stub (abfd) == NULL)
436 {
437 return;
438 }
439
440 memcpy (bfd_coff_go32stub (abfd), stub_bytes, STUBSIZE);
441 }
442}
443
444/* If ibfd was a stubbed coff image, copy the stub from that bfd
445 to the new obfd.
446 */
447
448static boolean
449go32_stubbed_coff_bfd_copy_private_bfd_data (ibfd, obfd)
450 bfd *ibfd;
451 bfd *obfd;
452{
453 /* check if both are the same targets */
454 if (ibfd->xvec != obfd->xvec)
455 return true;
456
457 /* check if both have a valid stub */
458 if (bfd_coff_go32stub (ibfd) == NULL
459 || bfd_coff_go32stub (obfd) == NULL)
460 return true;
461
462 /* Now copy the stub */
463 memcpy (bfd_coff_go32stub (obfd), bfd_coff_go32stub (ibfd), STUBSIZE);
464
465 return true;
466}
This page took 0.046295 seconds and 4 git commands to generate.