home *** CD-ROM | disk | FTP | other *** search
/ Geek Gadgets 1 / ADE-1.bin / ade-dist / binutils-2.7-src.tgz / tar.out / fsf / binutils / bfd / coff-aux.c < prev    next >
C/C++ Source or Header  |  1996-09-28  |  10KB  |  333 lines

  1. /* BFD back-end for Apple M68K COFF A/UX 3.x files.
  2.    Copyright 1996 Free Software Foundation, Inc.
  3.    Portions written by Richard Henderson <rth@tamu.edu>,
  4.    COMMON symbol munging cribbed from cf-m68klynx.c which was
  5.    written by Cygnus Support.
  6.  
  7. This file is part of BFD, the Binary File Descriptor library.
  8.  
  9. This program is free software; you can redistribute it and/or modify
  10. it under the terms of the GNU General Public License as published by
  11. the Free Software Foundation; either version 2 of the License, or
  12. (at your option) any later version.
  13.  
  14. This program is distributed in the hope that it will be useful,
  15. but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17. GNU General Public License for more details.
  18.  
  19. You should have received a copy of the GNU General Public License
  20. along with this program; if not, write to the Free Software
  21. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
  22.  
  23. #define TARGET_SYM    m68kaux_coff_vec
  24. #define TARGET_NAME    "coff-m68k-aux"
  25.  
  26. #ifndef TARG_AUX
  27. #define TARG_AUX
  28. #endif
  29.  
  30. #define COFF_LONG_FILENAMES
  31.  
  32. /* 4k pages */
  33. #define COFF_PAGE_SIZE 0x1000
  34.  
  35. /* On AUX, a STYP_NOLOAD|STYP_BSS section is part of a shared library. */
  36. #define BSS_NOLOAD_IS_SHARED_LIBRARY
  37.  
  38. #define _bfd_m68kcoff_howto_table _bfd_m68kaux_howto_table    
  39. #define _bfd_m68kcoff_rtype2howto _bfd_m68kaux_rtype2howto    
  40. #define _bfd_m68kcoff_howto2rtype _bfd_m68kaux_howto2rtype    
  41. #define _bfd_m68kcoff_reloc_type_lookup _bfd_m68kaux_reloc_type_lookup
  42.  
  43. /* Rather than change names lots of places, reuse the same hack */
  44. #define LYNX_SPECIAL_FN _bfd_m68kaux_special_fn
  45.  
  46. #include "bfd.h"
  47. #include "sysdep.h"
  48.  
  49. #ifdef ANSI_PROTOTYPES
  50. struct internal_reloc;
  51. struct coff_link_hash_entry;
  52. struct internal_syment;
  53. #endif
  54.  
  55.  
  56. static bfd_reloc_status_type _bfd_m68kaux_special_fn
  57.   PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
  58. static reloc_howto_type *coff_m68k_aux_rtype_to_howto
  59.   PARAMS ((bfd *, asection *, struct internal_reloc *,
  60.        struct coff_link_hash_entry *, struct internal_syment *,
  61.        bfd_vma *));
  62. static boolean coff_m68k_aux_link_add_one_symbol
  63.   PARAMS ((struct bfd_link_info *, bfd *, const char *, flagword, 
  64.            asection *, bfd_vma, const char *, boolean, boolean, 
  65.            struct bfd_link_hash_entry **));
  66.  
  67.  
  68. #define coff_rtype_to_howto coff_m68k_aux_rtype_to_howto
  69. #define coff_link_add_one_symbol coff_m68k_aux_link_add_one_symbol
  70.  
  71.  
  72. /* Compute the addend of a reloc.  If the reloc is to a common symbol,
  73.    the object file contains the value of the common symbol.  By the
  74.    time this is called, the linker may be using a different symbol
  75.    from a different object file with a different value.  Therefore, we
  76.    hack wildly to locate the original symbol from this file so that we
  77.    can make the correct adjustment.  This macro sets coffsym to the
  78.    symbol from the original file, and uses it to set the addend value
  79.    correctly.  If this is not a common symbol, the usual addend
  80.    calculation is done, except that an additional tweak is needed for
  81.    PC relative relocs.
  82.    FIXME: This macro refers to symbols and asect; these are from the
  83.    calling function, not the macro arguments.  */
  84.  
  85. #define CALC_ADDEND(abfd, ptr, reloc, cache_ptr)        \
  86.   {                                \
  87.     coff_symbol_type *coffsym = (coff_symbol_type *) NULL;    \
  88.     if (ptr && bfd_asymbol_bfd (ptr) != abfd)            \
  89.       coffsym = (obj_symbols (abfd)                \
  90.              + (cache_ptr->sym_ptr_ptr - symbols));        \
  91.     else if (ptr)                        \
  92.       coffsym = coff_symbol_from (abfd, ptr);            \
  93.     if (coffsym != (coff_symbol_type *) NULL            \
  94.     && coffsym->native->u.syment.n_scnum == 0)        \
  95.       cache_ptr->addend = - coffsym->native->u.syment.n_value;    \
  96.     else if (ptr && bfd_asymbol_bfd (ptr) == abfd        \
  97.          && ptr->section != (asection *) NULL)        \
  98.       cache_ptr->addend = - (ptr->section->vma + ptr->value);    \
  99.     else                            \
  100.       cache_ptr->addend = 0;                    \
  101.     if (ptr && (reloc.r_type == R_PCRBYTE            \
  102.         || reloc.r_type == R_PCRWORD            \
  103.         || reloc.r_type == R_PCRLONG))            \
  104.       cache_ptr->addend += asect->vma;                \
  105.   }
  106.  
  107.  
  108.  
  109. #include "coff/aux-coff.h"  /* override coff/internal.h and coff/m68k.h */
  110. #include "coff-m68k.c"
  111.  
  112.  
  113.  
  114. /* For some reason when using m68k COFF the value stored in the .text
  115.    section for a reference to a common symbol is the value itself plus
  116.    any desired offset.  (taken from work done by Ian Taylor, Cygnus Support,
  117.    for I386 COFF).  */
  118.  
  119. /* If we are producing relocateable output, we need to do some
  120.    adjustments to the object file that are not done by the
  121.    bfd_perform_relocation function.  This function is called by every
  122.    reloc type to make any required adjustments.  */
  123.  
  124. static bfd_reloc_status_type
  125. _bfd_m68kaux_special_fn (abfd, reloc_entry, symbol, data, input_section,
  126.               output_bfd, error_message)
  127.      bfd *abfd;
  128.      arelent *reloc_entry;
  129.      asymbol *symbol;
  130.      PTR data;
  131.      asection *input_section;
  132.      bfd *output_bfd;
  133.      char **error_message;
  134. {
  135.   symvalue diff;
  136.  
  137.   if (output_bfd == (bfd *) NULL)
  138.     return bfd_reloc_continue;
  139.  
  140.   if (bfd_is_com_section (symbol->section))
  141.     {
  142.       /* We are relocating a common symbol.  The current value in the
  143.      object file is ORIG + OFFSET, where ORIG is the value of the
  144.      common symbol as seen by the object file when it was compiled
  145.      (this may be zero if the symbol was undefined) and OFFSET is
  146.      the offset into the common symbol (normally zero, but may be
  147.      non-zero when referring to a field in a common structure).
  148.      ORIG is the negative of reloc_entry->addend, which is set by
  149.      the CALC_ADDEND macro below.  We want to replace the value in
  150.      the object file with NEW + OFFSET, where NEW is the value of
  151.      the common symbol which we are going to put in the final
  152.      object file.  NEW is symbol->value.  */
  153.       diff = symbol->value + reloc_entry->addend;
  154.     }
  155.   else
  156.     {
  157.       /* For some reason bfd_perform_relocation always effectively
  158.      ignores the addend for a COFF target when producing
  159.      relocateable output.  This seems to be always wrong for 386
  160.      COFF, so we handle the addend here instead.  */
  161.       diff = reloc_entry->addend;
  162.     }
  163.  
  164. #define DOIT(x) \
  165.   x = ((x & ~howto->dst_mask) | \
  166.        (((x & howto->src_mask) + diff) & howto->dst_mask))
  167.  
  168.   if (diff != 0)
  169.     {
  170.       reloc_howto_type *howto = reloc_entry->howto;
  171.       unsigned char *addr = (unsigned char *) data + reloc_entry->address;
  172.  
  173.       switch (howto->size)
  174.     {
  175.     case 0:
  176.       {
  177.         char x = bfd_get_8 (abfd, addr);
  178.         DOIT (x);
  179.         bfd_put_8 (abfd, x, addr);
  180.       }
  181.       break;
  182.  
  183.     case 1:
  184.       {
  185.         short x = bfd_get_16 (abfd, addr);
  186.         DOIT (x);
  187.         bfd_put_16 (abfd, x, addr);
  188.       }
  189.       break;
  190.  
  191.     case 2:
  192.       {
  193.         long x = bfd_get_32 (abfd, addr);
  194.         DOIT (x);
  195.         bfd_put_32 (abfd, x, addr);
  196.       }
  197.       break;
  198.  
  199.     default:
  200.       abort ();
  201.     }
  202.     }
  203.  
  204.   /* Now let bfd_perform_relocation finish everything up.  */
  205.   return bfd_reloc_continue;
  206. }
  207.  
  208.  
  209. /* coff-m68k.c uses the special COFF backend linker.  We need to
  210.    adjust common symbols.  */
  211.  
  212. /*ARGSUSED*/
  213. static reloc_howto_type *
  214. coff_m68k_aux_rtype_to_howto (abfd, sec, rel, h, sym, addendp)
  215.      bfd *abfd;
  216.      asection *sec;
  217.      struct internal_reloc *rel;
  218.      struct coff_link_hash_entry *h;
  219.      struct internal_syment *sym;
  220.      bfd_vma *addendp;
  221. {
  222.   arelent relent;
  223.   reloc_howto_type *howto;
  224.  
  225.   RTYPE2HOWTO (&relent, rel);
  226.  
  227.   howto = relent.howto;
  228.  
  229.   if (sym != NULL && sym->n_scnum == 0 && sym->n_value != 0)
  230.     {
  231.       /* This is a common symbol.  The section contents include the
  232.      size (sym->n_value) as an addend.  The relocate_section
  233.      function will be adding in the final value of the symbol.  We
  234.      need to subtract out the current size in order to get the
  235.      correct result.  */
  236.       BFD_ASSERT (h != NULL);
  237.       *addendp -= sym->n_value;
  238.     }
  239.  
  240.   /* If the output symbol is common (in which case this must be a
  241.      relocateable link), we need to add in the final size of the
  242.      common symbol.  */
  243.   if (h != NULL && h->root.type == bfd_link_hash_common)
  244.     *addendp += h->root.u.c.size;
  245.  
  246.   return howto;
  247. }
  248.  
  249.  
  250. /* We need non-absolute symbols to override absolute symbols.  This
  251.    mirrors Apple's "solution" to let a static library symbol override
  252.    a shared library symbol.  On the whole not a good thing, given how
  253.    shared libraries work here, but can work if you are careful with
  254.    what you include in the shared object. */
  255.  
  256. boolean
  257. coff_m68k_aux_link_add_one_symbol (info, abfd, name, flags, section, value,
  258.                    string, copy, collect, hashp)
  259.      struct bfd_link_info *info;
  260.      bfd *abfd;
  261.      const char *name;
  262.      flagword flags;
  263.      asection *section;
  264.      bfd_vma value;
  265.      const char *string;
  266.      boolean copy;
  267.      boolean collect;
  268.      struct bfd_link_hash_entry **hashp;
  269. {
  270.   struct bfd_link_hash_entry *h;
  271.  
  272.   if ((flags & (BSF_WARNING | BSF_CONSTRUCTOR | BSF_WEAK)) == 0 &&
  273.       !bfd_is_und_section (section) &&
  274.       !bfd_is_com_section (section))
  275.     {
  276.       /* The new symbol is a definition or an indirect definition */
  277.  
  278.       /* This bit copied from linker.c */
  279.       if (hashp != NULL && *hashp != NULL)
  280.     {
  281.       h = *hashp;
  282.       BFD_ASSERT (strcmp (h->root.string, name) == 0);
  283.     }
  284.       else
  285.     {
  286.       h = bfd_link_hash_lookup (info->hash, name, true, copy, false);
  287.       if (h == NULL)
  288.         {
  289.           if (hashp != NULL)
  290.         *hashp = NULL;
  291.           return false;
  292.         }
  293.     }
  294.  
  295.       if (info->notice_hash != (struct bfd_hash_table *) NULL
  296.       && (bfd_hash_lookup (info->notice_hash, name, false, false)
  297.           != (struct bfd_hash_entry *) NULL))
  298.     {
  299.       if (! (*info->callbacks->notice) (info, name, abfd, section, value))
  300.         return false;
  301.     }
  302.  
  303.       if (hashp != (struct bfd_link_hash_entry **) NULL)
  304.     *hashp = h;
  305.       /* end duplication from linker.c */
  306.  
  307.       if (h->type == bfd_link_hash_defined
  308.       || h->type == bfd_link_hash_indirect)
  309.     {
  310.       asection *msec;
  311.  
  312.       if (h->type == bfd_link_hash_defined)
  313.         msec = h->u.def.section;
  314.       else
  315.         msec = bfd_ind_section_ptr;
  316.  
  317.       if (bfd_is_abs_section (msec) && !bfd_is_abs_section (section))
  318.         {
  319.           h->u.def.section = section;
  320.           h->u.def.value = value;
  321.           return true;
  322.         }
  323.       else if (bfd_is_abs_section (section) && !bfd_is_abs_section (msec))
  324.         return true;
  325.     }
  326.     }
  327.  
  328.   /* If we didn't exit early, finish processing in the generic routine */
  329.   return _bfd_generic_link_add_one_symbol (info, abfd, name, flags, section,
  330.                        value, string, copy, collect,
  331.                        hashp);
  332. }
  333.