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