home *** CD-ROM | disk | FTP | other *** search
/ Il CD di internet / CD.iso / SOURCE / D / GDB / GDB-4.13 / GDB-4 / gdb-4.13 / bfd / coff-i386.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-06-20  |  12.9 KB  |  355 lines

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