home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / gnu / djgpp / src / binutils.2 / bfd / coff-i38.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-05-30  |  12.7 KB  |  335 lines

  1. /* BFD back-end for Intel 386 COFF files.
  2.    Copyright 1990, 1991, 1992, 1993 Free Software Foundation, Inc.
  3.    Written by Cygnus Support.
  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., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  20.  
  21. #include "bfd.h"
  22. #include "sysdep.h"
  23. #include "libbfd.h"
  24. #include "obstack.h"
  25. #include "coff/i386.h"
  26. #include "coff/internal.h"
  27. #include "libcoff.h"
  28.  
  29. static bfd_reloc_status_type coff_i386_reloc PARAMS ((bfd *abfd,
  30.                               arelent *reloc_entry,
  31.                               asymbol *symbol,
  32.                               PTR data,
  33.                               asection *input_section,
  34.                               bfd *output_bfd));
  35.  
  36. /* For some reason when using i386 COFF the value stored in the .text
  37.    section for a reference to a common symbol is the value itself plus
  38.    any desired offset.  Ian Taylor, Cygnus Support.  */
  39.  
  40. /* If we are producing relocateable output, we need to do some
  41.    adjustments to the object file that are not done by the
  42.    bfd_perform_relocation function.  This function is called by every
  43.    reloc type to make any required adjustments.  */
  44.  
  45. static bfd_reloc_status_type
  46. coff_i386_reloc (abfd, reloc_entry, symbol, data, input_section, output_bfd)
  47.      bfd *abfd;
  48.      arelent *reloc_entry;
  49.      asymbol *symbol;
  50.      PTR data;
  51.      asection *input_section;
  52.      bfd *output_bfd;
  53. {
  54.   symvalue diff;
  55.  
  56.   if (output_bfd == (bfd *) NULL)
  57.     return bfd_reloc_continue;
  58.  
  59.   if (bfd_is_com_section (symbol->section))
  60.     {
  61.       /* We are relocating a common symbol.  The current value in the
  62.      object file is ORIG + OFFSET, where ORIG is the value of the
  63.      common symbol as seen by the object file when it was compiled
  64.      (this may be zero if the symbol was undefined) and OFFSET is
  65.      the offset into the common symbol (normally zero, but may be
  66.      non-zero when referring to a field in a common structure).
  67.      ORIG is the negative of reloc_entry->addend, which is set by
  68.      the CALC_ADDEND macro below.  We want to replace the value in
  69.      the object file with NEW + OFFSET, where NEW is the value of
  70.      the common symbol which we are going to put in the final
  71.      object file.  NEW is symbol->value.  */
  72.       diff = symbol->value + reloc_entry->addend;
  73.     }
  74.   else
  75.     {
  76.       /* For some reason bfd_perform_relocation always effectively
  77.      ignores the addend for a COFF target when producing
  78.      relocateable output.  This seems to be always wrong for 386
  79.      COFF, so we handle the addend here instead.  */
  80.       diff = reloc_entry->addend;
  81.     }
  82.  
  83. #define DOIT(x) \
  84.   x = ((x & ~howto->dst_mask) | (((x & howto->src_mask) + diff) & howto->dst_mask))
  85.  
  86.   if (diff != 0)
  87.     {
  88.       reloc_howto_type *howto = reloc_entry->howto;
  89.       char *addr = (char *) data + reloc_entry->address;
  90.  
  91.       switch (howto->size)
  92.     {
  93.     case 0:
  94.       {
  95.         char x = bfd_get_8 (abfd, addr);
  96.         DOIT (x);
  97.         bfd_put_8 (abfd, x, addr);
  98.       }
  99.       break;
  100.  
  101.     case 1:
  102.       {
  103.         short x = bfd_get_16 (abfd, addr);
  104.         DOIT (x);
  105.         bfd_put_16 (abfd, x, addr);
  106.       }
  107.       break;
  108.  
  109.     case 2:
  110.       {
  111.         long x = bfd_get_32 (abfd, addr);
  112.         DOIT (x);
  113.         bfd_put_32 (abfd, x, addr);
  114.       }
  115.       break;
  116.  
  117.     default:
  118.       abort ();
  119.     }
  120.     }
  121.  
  122.   /* Now let bfd_perform_relocation finish everything up.  */
  123.   return bfd_reloc_continue;
  124. }
  125.  
  126. static reloc_howto_type howto_table[] = 
  127. {
  128.   {0},
  129.   {1},
  130.   {2},
  131.   {3},
  132.   {4},
  133.   {5},
  134.   HOWTO (R_DIR32,               /* type */                                 
  135.      0,                    /* rightshift */                           
  136.      2,                    /* size (0 = byte, 1 = short, 2 = long) */ 
  137.      32,                    /* bitsize (obsolete) */                   
  138.      false,                    /* pc_relative */                          
  139.      0,                    /* bitpos */                               
  140.      true,                    /* absolute (obsolete) */                  
  141.      true,                    /* complain_on_overflow */                 
  142.      coff_i386_reloc,       /* special_function */                     
  143.      "dir32",               /* name */                                 
  144.      true,                    /* partial_inplace */                      
  145.      0xffffffff,            /* src_mask */                             
  146.      0xffffffff,            /* dst_mask */                             
  147.      false),                /* pcrel_offset */
  148.   {7},
  149.   {010},
  150.   {011},
  151.   {012},
  152.   {013},
  153.   {014},
  154.   {015},
  155.   {016},
  156.   HOWTO (R_RELBYTE,        /* type */                                 
  157.      0,            /* rightshift */                           
  158.      0,            /* size (0 = byte, 1 = short, 2 = long) */ 
  159.      8,            /* bitsize (obsolete) */                   
  160.      false,            /* pc_relative */                          
  161.      0,            /* bitpos */                               
  162.      true,            /* absolute (obsolete) */                  
  163.      true,            /* complain_on_overflow */                 
  164.      coff_i386_reloc,    /* special_function */                     
  165.      "8",            /* name */                                 
  166.      true,            /* partial_inplace */                      
  167.      0x000000ff,        /* src_mask */                             
  168.      0x000000ff,        /* dst_mask */                             
  169.      false),        /* pcrel_offset */
  170.   HOWTO (R_RELWORD,        /* type */                                 
  171.      0,            /* rightshift */                           
  172.      1,            /* size (0 = byte, 1 = short, 2 = long) */ 
  173.      16,            /* bitsize (obsolete) */                   
  174.      false,            /* pc_relative */                          
  175.      0,            /* bitpos */                               
  176.      true,            /* absolute (obsolete) */                  
  177.      true,            /* complain_on_overflow */                 
  178.      coff_i386_reloc,    /* special_function */                     
  179.      "16",            /* name */                                 
  180.      true,            /* partial_inplace */                      
  181.      0x0000ffff,        /* src_mask */                             
  182.      0x0000ffff,        /* dst_mask */                             
  183.      false),        /* pcrel_offset */
  184.   HOWTO (R_RELLONG,        /* type */                                 
  185.      0,            /* rightshift */                           
  186.      2,            /* size (0 = byte, 1 = short, 2 = long) */ 
  187.      32,            /* bitsize (obsolete) */                   
  188.      false,            /* pc_relative */                          
  189.      0,            /* bitpos */                               
  190.      true,            /* absolute (obsolete) */                  
  191.      true,            /* complain_on_overflow */                 
  192.      coff_i386_reloc,    /* special_function */                     
  193.      "32",            /* name */                                 
  194.      true,            /* partial_inplace */                      
  195.      0xffffffff,        /* src_mask */                             
  196.      0xffffffff,        /* dst_mask */                             
  197.      false),        /* pcrel_offset */
  198.   HOWTO (R_PCRBYTE,        /* type */                                 
  199.      0,            /* rightshift */                           
  200.      0,            /* size (0 = byte, 1 = short, 2 = long) */ 
  201.      8,            /* bitsize (obsolete) */                   
  202.      true,            /* pc_relative */                          
  203.      0,            /* bitpos */                               
  204.      false,            /* absolute (obsolete) */                  
  205.      true,            /* complain_on_overflow */                 
  206.      coff_i386_reloc,    /* special_function */                     
  207.      "DISP8",        /* name */                                 
  208.      true,            /* partial_inplace */                      
  209.      0x000000ff,        /* src_mask */                             
  210.      0x000000ff,        /* dst_mask */                             
  211.      false),        /* pcrel_offset */
  212.   HOWTO (R_PCRWORD,        /* type */                                 
  213.      0,            /* rightshift */                           
  214.      1,            /* size (0 = byte, 1 = short, 2 = long) */ 
  215.      16,            /* bitsize (obsolete) */                   
  216.      true,            /* pc_relative */                          
  217.      0,            /* bitpos */                               
  218.      false,            /* absolute (obsolete) */                  
  219.      true,            /* complain_on_overflow */                 
  220.      coff_i386_reloc,    /* special_function */                     
  221.      "DISP16",        /* name */                                 
  222.      true,            /* partial_inplace */                      
  223.      0x0000ffff,        /* src_mask */                             
  224.      0x0000ffff,        /* dst_mask */                             
  225.      false),        /* pcrel_offset */
  226.   HOWTO (R_PCRLONG,        /* type */                                 
  227.      0,            /* rightshift */                           
  228.      2,            /* size (0 = byte, 1 = short, 2 = long) */ 
  229.      32,            /* bitsize (obsolete) */                   
  230.      true,            /* pc_relative */                          
  231.      0,            /* bitpos */                               
  232.      false,            /* absolute (obsolete) */                  
  233.      true,            /* complain_on_overflow */                 
  234.      coff_i386_reloc,    /* special_function */                     
  235.      "DISP32",        /* name */                                 
  236.      true,            /* partial_inplace */                      
  237.      0xffffffff,        /* src_mask */                             
  238.      0xffffffff,        /* dst_mask */                             
  239.      false)            /* pcrel_offset */
  240. };
  241.  
  242. /* Turn a howto into a reloc  nunmber */
  243.  
  244. #define SELECT_RELOC(x,howto) { x = howto->type; }
  245. #define BADMAG(x) I386BADMAG(x)
  246. #define I386 1            /* Customize coffcode.h */
  247.  
  248. #define RTYPE2HOWTO(cache_ptr, dst) \
  249.         cache_ptr->howto = howto_table + (dst)->r_type;
  250.  
  251. /* On SCO Unix 3.2.2 the native assembler generates two .data
  252.    sections.  We handle that by renaming the second one to .data2.  It
  253.    does no harm to do this for any 386 COFF target.  */
  254. #define TWO_DATA_SECS
  255.  
  256. /* For 386 COFF a STYP_NOLOAD | STYP_BSS section is part of a shared
  257.    library.  On some other COFF targets STYP_BSS is normally
  258.    STYP_NOLOAD.  */
  259. #define BSS_NOLOAD_IS_SHARED_LIBRARY
  260.  
  261. /* Compute the addend of a reloc.  If the reloc is to a common symbol,
  262.    the object file contains the value of the common symbol.  By the
  263.    time this is called, the linker may be using a different symbol
  264.    from a different object file with a different value.  Therefore, we
  265.    hack wildly to locate the original symbol from this file so that we
  266.    can make the correct adjustment.  This macro sets coffsym to the
  267.    symbol from the original file, and uses it to set the addend value
  268.    correctly.  If this is not a common symbol, the usual addend
  269.    calculation is done, except that an additional tweak is needed for
  270.    PC relative relocs.
  271.    FIXME: This macro refers to symbols and asect; these are from the
  272.    calling function, not the macro arguments.  */
  273.  
  274. #define CALC_ADDEND(abfd, ptr, reloc, cache_ptr)        \
  275.   {                                \
  276.     coff_symbol_type *coffsym = (coff_symbol_type *) NULL;    \
  277.     if (ptr && bfd_asymbol_bfd (ptr) != abfd)            \
  278.       coffsym = (obj_symbols (abfd)                \
  279.              + (cache_ptr->sym_ptr_ptr - symbols));        \
  280.     else if (ptr)                        \
  281.       coffsym = coff_symbol_from (abfd, ptr);            \
  282.     if (coffsym != (coff_symbol_type *) NULL            \
  283.     && coffsym->native->u.syment.n_scnum == 0)        \
  284.       cache_ptr->addend = - coffsym->native->u.syment.n_value;    \
  285.     else if (ptr && bfd_asymbol_bfd (ptr) == abfd        \
  286.          && ptr->section != (asection *) NULL)        \
  287.       cache_ptr->addend = - (ptr->section->vma + ptr->value);    \
  288.     else                            \
  289.       cache_ptr->addend = 0;                    \
  290.     if (ptr && howto_table[reloc.r_type].pc_relative)        \
  291.       cache_ptr->addend += asect->vma;                \
  292.   }
  293.  
  294. #include "coffcode.h"
  295.  
  296. bfd_target *i3coff_object_p(a)
  297. bfd *a ;
  298. { return coff_object_p(a); }
  299.  
  300. bfd_target i386coff_vec =
  301. {
  302.   "coff-i386",            /* name */
  303.   bfd_target_coff_flavour,
  304.   false,            /* data byte order is little */
  305.   false,            /* header byte order is little */
  306.  
  307.   (HAS_RELOC | EXEC_P |        /* object flags */
  308.    HAS_LINENO | HAS_DEBUG |
  309.    HAS_SYMS | HAS_LOCALS | DYNAMIC | WP_TEXT),
  310.  
  311.   (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC), /* section flags */
  312.   0,                /* leading underscore */
  313.   '/',                /* ar_pad_char */
  314.   15,                /* ar_max_namelen */
  315.  
  316.   2,                /* minimum alignment power */
  317.   _do_getl64, _do_getl_signed_64, _do_putl64,
  318.      _do_getl32, _do_getl_signed_32, _do_putl32,
  319.      _do_getl16, _do_getl_signed_16, _do_putl16, /* data */
  320.   _do_getl64, _do_getl_signed_64, _do_putl64,
  321.      _do_getl32, _do_getl_signed_32, _do_putl32,
  322.      _do_getl16, _do_getl_signed_16, _do_putl16, /* hdrs */
  323.  
  324. /* Note that we allow an object file to be treated as a core file as well. */
  325.     {_bfd_dummy_target, i3coff_object_p, /* bfd_check_format */
  326.        bfd_generic_archive_p, i3coff_object_p},
  327.     {bfd_false, coff_mkobject, _bfd_generic_mkarchive, /* bfd_set_format */
  328.        bfd_false},
  329.     {bfd_false, coff_write_object_contents, /* bfd_write_contents */
  330.        _bfd_write_archive_contents, bfd_false},
  331.  
  332.   JUMP_TABLE(coff),
  333.   COFF_SWAP_TABLE,
  334. };
  335.