Home | History | Annotate | Download | only in bfd
      1 /* BFD back-end for Intel 386 COFF files (DJGPP variant with a stub).
      2    Copyright (C) 1997-2014 Free Software Foundation, Inc.
      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 3 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., 51 Franklin Street - Fifth Floor, Boston,
     20    MA 02110-1301, USA.  */
     21 
     22 /* This file handles now also stubbed coff images. The stub is a small
     23    DOS executable program before the coff image to load it in memory
     24    and execute it. This is needed, because DOS cannot run coff files.
     25 
     26    All the functions below are called by the corresponding functions
     27    from coffswap.h.
     28    The only thing what they do is to adjust the information stored in
     29    the COFF file which are offset into the file.
     30    This is needed, because DJGPP uses a very special way to load and run
     31    the coff image. It loads the image in memory and assumes then, that the
     32    image had no stub by using the filepointers as pointers in the coff
     33    image and NOT in the file.
     34 
     35    To be compatible with any existing executables I have fixed this
     36    here and NOT in the DJGPP startup code.  */
     37 
     38 #define TARGET_SYM		i386_coff_go32stubbed_vec
     39 #define TARGET_NAME		"coff-go32-exe"
     40 #define TARGET_UNDERSCORE	'_'
     41 #define COFF_GO32_EXE
     42 #define COFF_LONG_SECTION_NAMES
     43 #define COFF_SUPPORT_GNU_LINKONCE
     44 #define COFF_LONG_FILENAMES
     45 
     46 #define COFF_SECTION_ALIGNMENT_ENTRIES \
     47 { COFF_SECTION_NAME_EXACT_MATCH (".data"), \
     48   COFF_ALIGNMENT_FIELD_EMPTY, COFF_ALIGNMENT_FIELD_EMPTY, 4 }, \
     49 { COFF_SECTION_NAME_EXACT_MATCH (".text"), \
     50   COFF_ALIGNMENT_FIELD_EMPTY, COFF_ALIGNMENT_FIELD_EMPTY, 4 }, \
     51 { COFF_SECTION_NAME_PARTIAL_MATCH (".debug"), \
     52   COFF_ALIGNMENT_FIELD_EMPTY, COFF_ALIGNMENT_FIELD_EMPTY, 0 }, \
     53 { COFF_SECTION_NAME_PARTIAL_MATCH (".gnu.linkonce.wi"), \
     54   COFF_ALIGNMENT_FIELD_EMPTY, COFF_ALIGNMENT_FIELD_EMPTY, 0 }
     55 
     56 #include "sysdep.h"
     57 #include "bfd.h"
     58 
     59 /* All that ..._PRE and ...POST functions are called from the corresponding
     60    coff_swap... functions. The ...PRE functions are called at the beginning
     61    of the function and the ...POST functions at the end of the swap routines.  */
     62 
     63 static void
     64 adjust_filehdr_in_post  (bfd *, void *, void *);
     65 static void
     66 adjust_filehdr_out_pre  (bfd *, void *, void *);
     67 static void
     68 adjust_filehdr_out_post  (bfd *, void *, void *);
     69 static void
     70 adjust_scnhdr_in_post  (bfd *, void *, void *);
     71 static void
     72 adjust_scnhdr_out_pre  (bfd *, void *, void *);
     73 static void
     74 adjust_scnhdr_out_post (bfd *, void *, void *);
     75 static void
     76 adjust_aux_in_post (bfd *, void *, int, int, int, int, void *);
     77 static void
     78 adjust_aux_out_pre (bfd *, void *, int, int, int, int, void *);
     79 static void
     80 adjust_aux_out_post (bfd *, void *, int, int, int, int, void *);
     81 static void
     82 create_go32_stub (bfd *);
     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 
     96 static const bfd_target *go32_check_format (bfd *);
     97 
     98 #define COFF_CHECK_FORMAT go32_check_format
     99 
    100 static bfd_boolean
    101   go32_stubbed_coff_bfd_copy_private_bfd_data (bfd *, bfd *);
    102 
    103 #define coff_bfd_copy_private_bfd_data go32_stubbed_coff_bfd_copy_private_bfd_data
    104 
    105 #include "coff-i386.c"
    106 
    107 /* This macro is used, because I cannot assume the endianness of the
    108    host system.  */
    109 #define _H(index) (H_GET_16 (abfd, (header + index * 2)))
    110 
    111 /* These bytes are a 2048-byte DOS executable, which loads the COFF
    112    image into memory and then runs it. It is called 'stub'.  */
    113 
    114 static const unsigned char stub_bytes[GO32_STUBSIZE] =
    115 {
    116 #include "go32stub.h"
    117 };
    118 
    119 /*
    120    I have not commented each swap function below, because the
    121    technique is in any function the same. For the ...in function,
    122    all the pointers are adjusted by adding GO32_STUBSIZE and for the
    123    ...out function, it is subtracted first and after calling the
    124    standard swap function it is reset to the old value.  */
    125 
    126 /* This macro is used for adjusting the filepointers, which
    127    is done only, if the pointer is nonzero.  */
    128 
    129 #define ADJUST_VAL(val,diff) \
    130   if (val != 0) val += diff
    131 
    132 static void
    133 adjust_filehdr_in_post  (bfd *  abfd ATTRIBUTE_UNUSED,
    134 			 void * src,
    135 			 void * 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, GO32_STUBSIZE);
    141 
    142   /* Save now the stub to be used later.  Put the stub data to FILEHDR_DST
    143      first as coff_data (abfd) still does not exist.  It may not even be ever
    144      created as we are just checking the file format of ABFD.  */
    145   memcpy (filehdr_dst->go32stub, filehdr_src->stub, GO32_STUBSIZE);
    146   filehdr_dst->f_flags |= F_GO32STUB;
    147 }
    148 
    149 static void
    150 adjust_filehdr_out_pre  (bfd * abfd, void * in, void * out)
    151 {
    152   struct internal_filehdr *filehdr_in = (struct internal_filehdr *) in;
    153   FILHDR *filehdr_out = (FILHDR *) out;
    154 
    155   /* Generate the stub.  */
    156   create_go32_stub (abfd);
    157 
    158   /* Copy the stub to the file header.  */
    159   if (coff_data (abfd)->go32stub != NULL)
    160     memcpy (filehdr_out->stub, coff_data (abfd)->go32stub, GO32_STUBSIZE);
    161   else
    162     /* Use the default.  */
    163     memcpy (filehdr_out->stub, stub_bytes, GO32_STUBSIZE);
    164 
    165   ADJUST_VAL (filehdr_in->f_symptr, -GO32_STUBSIZE);
    166 }
    167 
    168 static void
    169 adjust_filehdr_out_post  (bfd *  abfd ATTRIBUTE_UNUSED,
    170 			  void * in,
    171 			  void * out ATTRIBUTE_UNUSED)
    172 {
    173   struct internal_filehdr *filehdr_in = (struct internal_filehdr *) in;
    174   /* Undo the above change.  */
    175   ADJUST_VAL (filehdr_in->f_symptr, GO32_STUBSIZE);
    176 }
    177 
    178 static void
    179 adjust_scnhdr_in_post  (bfd *  abfd ATTRIBUTE_UNUSED,
    180 			void * ext ATTRIBUTE_UNUSED,
    181 			void * in)
    182 {
    183   struct internal_scnhdr *scnhdr_int = (struct internal_scnhdr *) in;
    184 
    185   ADJUST_VAL (scnhdr_int->s_scnptr, GO32_STUBSIZE);
    186   ADJUST_VAL (scnhdr_int->s_relptr, GO32_STUBSIZE);
    187   ADJUST_VAL (scnhdr_int->s_lnnoptr, GO32_STUBSIZE);
    188 }
    189 
    190 static void
    191 adjust_scnhdr_out_pre  (bfd *  abfd ATTRIBUTE_UNUSED,
    192 			void * in,
    193 			void * out ATTRIBUTE_UNUSED)
    194 {
    195   struct internal_scnhdr *scnhdr_int = (struct internal_scnhdr *) in;
    196 
    197   ADJUST_VAL (scnhdr_int->s_scnptr, -GO32_STUBSIZE);
    198   ADJUST_VAL (scnhdr_int->s_relptr, -GO32_STUBSIZE);
    199   ADJUST_VAL (scnhdr_int->s_lnnoptr, -GO32_STUBSIZE);
    200 }
    201 
    202 static void
    203 adjust_scnhdr_out_post (bfd *  abfd ATTRIBUTE_UNUSED,
    204 			void * in,
    205 			void * out ATTRIBUTE_UNUSED)
    206 {
    207   struct internal_scnhdr *scnhdr_int = (struct internal_scnhdr *) in;
    208 
    209   ADJUST_VAL (scnhdr_int->s_scnptr, GO32_STUBSIZE);
    210   ADJUST_VAL (scnhdr_int->s_relptr, GO32_STUBSIZE);
    211   ADJUST_VAL (scnhdr_int->s_lnnoptr, GO32_STUBSIZE);
    212 }
    213 
    214 static void
    215 adjust_aux_in_post (bfd * abfd ATTRIBUTE_UNUSED,
    216 		    void * ext1 ATTRIBUTE_UNUSED,
    217 		    int type,
    218 		    int in_class,
    219 		    int indx ATTRIBUTE_UNUSED,
    220 		    int numaux ATTRIBUTE_UNUSED,
    221 		    void * in1)
    222 {
    223   union internal_auxent *in = (union internal_auxent *) in1;
    224 
    225   if (in_class == C_BLOCK || in_class == C_FCN || ISFCN (type)
    226       || ISTAG (in_class))
    227     {
    228       ADJUST_VAL (in->x_sym.x_fcnary.x_fcn.x_lnnoptr, GO32_STUBSIZE);
    229     }
    230 }
    231 
    232 static void
    233 adjust_aux_out_pre (bfd *abfd ATTRIBUTE_UNUSED,
    234 		    void * inp,
    235 		    int type,
    236 		    int in_class,
    237 		    int indx ATTRIBUTE_UNUSED,
    238 		    int numaux ATTRIBUTE_UNUSED,
    239 		    void * extp ATTRIBUTE_UNUSED)
    240 {
    241   union internal_auxent *in = (union internal_auxent *) inp;
    242 
    243   if (in_class == C_BLOCK || in_class == C_FCN || ISFCN (type)
    244       || ISTAG (in_class))
    245     {
    246       ADJUST_VAL (in->x_sym.x_fcnary.x_fcn.x_lnnoptr, -GO32_STUBSIZE);
    247     }
    248 }
    249 
    250 static void
    251 adjust_aux_out_post (bfd *abfd ATTRIBUTE_UNUSED,
    252 		     void * inp,
    253 		     int type,
    254 		     int in_class,
    255 		     int indx ATTRIBUTE_UNUSED,
    256 		     int numaux ATTRIBUTE_UNUSED,
    257 		     void * extp ATTRIBUTE_UNUSED)
    258 {
    259   union internal_auxent *in = (union internal_auxent *) inp;
    260 
    261   if (in_class == C_BLOCK || in_class == C_FCN || ISFCN (type)
    262       || ISTAG (in_class))
    263     {
    264       ADJUST_VAL (in->x_sym.x_fcnary.x_fcn.x_lnnoptr, GO32_STUBSIZE);
    265     }
    266 }
    267 
    268 /* That's the function, which creates the stub. There are
    269    different cases from where the stub is taken.
    270    At first the environment variable $(GO32STUB) is checked and then
    271    $(STUB) if it was not set.
    272    If it exists and points to a valid stub the stub is taken from
    273    that file. This file can be also a whole executable file, because
    274    the stub is computed from the exe information at the start of that
    275    file.
    276 
    277    If there was any error, the standard stub (compiled in this file)
    278    is taken.  */
    279 
    280 static void
    281 create_go32_stub (bfd *abfd)
    282 {
    283   /* Do it only once.  */
    284   if (coff_data (abfd)->go32stub == NULL)
    285     {
    286       char *stub;
    287       struct stat st;
    288       int f;
    289       unsigned char header[10];
    290       char magic[8];
    291       unsigned long coff_start;
    292       long exe_start;
    293 
    294       /* Check at first the environment variable $(GO32STUB).  */
    295       stub = getenv ("GO32STUB");
    296       /* Now check the environment variable $(STUB).  */
    297       if (stub == NULL)
    298 	stub = getenv ("STUB");
    299       if (stub == NULL)
    300 	goto stub_end;
    301       if (stat (stub, &st) != 0)
    302 	goto stub_end;
    303 #ifdef O_BINARY
    304       f = open (stub, O_RDONLY | O_BINARY);
    305 #else
    306       f = open (stub, O_RDONLY);
    307 #endif
    308       if (f < 0)
    309 	goto stub_end;
    310       if (read (f, &header, sizeof (header)) < 0)
    311 	{
    312 	  close (f);
    313 	  goto stub_end;
    314 	}
    315       if (_H (0) != 0x5a4d)	/* It is not an exe file.  */
    316 	{
    317 	  close (f);
    318 	  goto stub_end;
    319 	}
    320       /* Compute the size of the stub (it is every thing up
    321          to the beginning of the coff image).  */
    322       coff_start = (long) _H (2) * 512L;
    323       if (_H (1))
    324 	coff_start += (long) _H (1) - 512L;
    325 
    326       /* Currently there is only a fixed stub size of 2048 bytes
    327          supported.  */
    328       if (coff_start != 2048)
    329 	{
    330 	  close (f);
    331 	  goto stub_end;
    332 	}
    333       exe_start = _H (4) * 16;
    334       if ((long) lseek (f, exe_start, SEEK_SET) != exe_start)
    335 	{
    336 	  close (f);
    337 	  goto stub_end;
    338 	}
    339       if (read (f, &magic, 8) != 8)
    340 	{
    341 	  close (f);
    342 	  goto stub_end;
    343 	}
    344       if (! CONST_STRNEQ (magic, "go32stub"))
    345 	{
    346 	  close (f);
    347 	  goto stub_end;
    348 	}
    349       /* Now we found a correct stub (hopefully).  */
    350       coff_data (abfd)->go32stub = bfd_alloc (abfd, (bfd_size_type) coff_start);
    351       if (coff_data (abfd)->go32stub == NULL)
    352 	{
    353 	  close (f);
    354 	  return;
    355 	}
    356       lseek (f, 0L, SEEK_SET);
    357       if ((unsigned long) read (f, coff_data (abfd)->go32stub, coff_start)
    358 	  != coff_start)
    359 	{
    360 	  bfd_release (abfd, coff_data (abfd)->go32stub);
    361 	  coff_data (abfd)->go32stub = NULL;
    362 	}
    363       close (f);
    364     }
    365 stub_end:
    366   /* There was something wrong above, so use now the standard builtin
    367      stub.  */
    368   if (coff_data (abfd)->go32stub == NULL)
    369     {
    370       coff_data (abfd)->go32stub
    371 	= bfd_alloc (abfd, (bfd_size_type) GO32_STUBSIZE);
    372       if (coff_data (abfd)->go32stub == NULL)
    373 	return;
    374       memcpy (coff_data (abfd)->go32stub, stub_bytes, GO32_STUBSIZE);
    375     }
    376 }
    377 
    378 /* If ibfd was a stubbed coff image, copy the stub from that bfd
    379    to the new obfd.  */
    380 
    381 static bfd_boolean
    382 go32_stubbed_coff_bfd_copy_private_bfd_data (bfd *ibfd, bfd *obfd)
    383 {
    384   /* Check if both are the same targets.  */
    385   if (ibfd->xvec != obfd->xvec)
    386     return TRUE;
    387 
    388   /* Check if we have a source stub.  */
    389   if (coff_data (ibfd)->go32stub == NULL)
    390     return TRUE;
    391 
    392   /* As adjust_filehdr_out_pre may get called only after this function,
    393      optionally allocate the output stub.  */
    394   if (coff_data (obfd)->go32stub == NULL)
    395     coff_data (obfd)->go32stub = bfd_alloc (obfd,
    396 					  (bfd_size_type) GO32_STUBSIZE);
    397 
    398   /* Now copy the stub.  */
    399   if (coff_data (obfd)->go32stub != NULL)
    400     memcpy (coff_data (obfd)->go32stub, coff_data (ibfd)->go32stub,
    401 	    GO32_STUBSIZE);
    402 
    403   return TRUE;
    404 }
    405 
    406 /* coff_object_p only checks 2 bytes F_MAGIC at GO32_STUBSIZE inside the file
    407    which is too fragile.  */
    408 
    409 static const bfd_target *
    410 go32_check_format (bfd *abfd)
    411 {
    412   char mz[2];
    413 
    414   if (bfd_bread (mz, 2, abfd) != 2 || mz[0] != 'M' || mz[1] != 'Z')
    415     {
    416       bfd_set_error (bfd_error_wrong_format);
    417       return NULL;
    418     }
    419 
    420   if (bfd_seek (abfd, 0, SEEK_SET) != 0)
    421     return NULL;
    422 
    423   return coff_object_p (abfd);
    424 }
    425