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