* cf-m68klynx.c (CALC_ADDEND): Use _bfd_m68klynx_howto_table.
[external/binutils.git] / bfd / cf-m68klynx.c
1 /* BFD back-end for Motorola M68K COFF LynxOS files.
2    Copyright 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 #define TARGET_SYM      m68klynx_coff_vec
22 #define TARGET_NAME     "coff-m68k-lynx"
23
24 #define LYNXOS
25
26 #define COFF_LONG_FILENAMES
27
28 #define _bfd_m68kcoff_howto_table _bfd_m68klynx_howto_table     
29 #define _bfd_m68kcoff_rtype2howto _bfd_m68klynx_rtype2howto     
30 #define _bfd_m68kcoff_howto2rtype _bfd_m68klynx_howto2rtype     
31
32 #define LYNX_SPECIAL_FN _bfd_m68klynx_special_fn
33
34 #include "bfd.h"
35
36 static bfd_reloc_status_type _bfd_m68klynx_special_fn PARAMS ((bfd *abfd,
37                                                       arelent *reloc_entry,
38                                                       asymbol *symbol,
39                                                       PTR data,
40                                                       asection *input_section,
41                                                       bfd *output_bfd,
42                                                       char **error_message));
43
44 /* For some reason when using m68k COFF the value stored in the .text
45    section for a reference to a common symbol is the value itself plus
46    any desired offset.  (taken from work done by Ian Taylor, Cygnus Support,
47    for I386 COFF).  */
48
49 /* If we are producing relocateable output, we need to do some
50    adjustments to the object file that are not done by the
51    bfd_perform_relocation function.  This function is called by every
52    reloc type to make any required adjustments.  */
53
54 static bfd_reloc_status_type
55 _bfd_m68klynx_special_fn (abfd, reloc_entry, symbol, data, input_section, output_bfd,
56                           error_message)
57      bfd *abfd;
58      arelent *reloc_entry;
59      asymbol *symbol;
60      PTR data;
61      asection *input_section;
62      bfd *output_bfd;
63      char **error_message;
64 {
65   symvalue diff;
66
67   if (output_bfd == (bfd *) NULL)
68     return bfd_reloc_continue;
69
70   if (bfd_is_com_section (symbol->section))
71     {
72       /* We are relocating a common symbol.  The current value in the
73          object file is ORIG + OFFSET, where ORIG is the value of the
74          common symbol as seen by the object file when it was compiled
75          (this may be zero if the symbol was undefined) and OFFSET is
76          the offset into the common symbol (normally zero, but may be
77          non-zero when referring to a field in a common structure).
78          ORIG is the negative of reloc_entry->addend, which is set by
79          the CALC_ADDEND macro below.  We want to replace the value in
80          the object file with NEW + OFFSET, where NEW is the value of
81          the common symbol which we are going to put in the final
82          object file.  NEW is symbol->value.  */
83       diff = symbol->value + reloc_entry->addend;
84     }
85   else
86     {
87       /* For some reason bfd_perform_relocation always effectively
88          ignores the addend for a COFF target when producing
89          relocateable output.  This seems to be always wrong for 386
90          COFF, so we handle the addend here instead.  */
91       diff = reloc_entry->addend;
92     }
93
94 #define DOIT(x) \
95   x = ((x & ~howto->dst_mask) | (((x & howto->src_mask) + diff) & howto->dst_mask))
96
97   if (diff != 0)
98     {
99       const reloc_howto_type *howto = reloc_entry->howto;
100       unsigned char *addr = (unsigned char *) data + reloc_entry->address;
101
102       switch (howto->size)
103         {
104         case 0:
105           {
106             char x = bfd_get_8 (abfd, addr);
107             DOIT (x);
108             bfd_put_8 (abfd, x, addr);
109           }
110           break;
111
112         case 1:
113           {
114             short x = bfd_get_16 (abfd, addr);
115             DOIT (x);
116             bfd_put_16 (abfd, x, addr);
117           }
118           break;
119
120         case 2:
121           {
122             long x = bfd_get_32 (abfd, addr);
123             DOIT (x);
124             bfd_put_32 (abfd, x, addr);
125           }
126           break;
127
128         default:
129           abort ();
130         }
131     }
132
133   /* Now let bfd_perform_relocation finish everything up.  */
134   return bfd_reloc_continue;
135 }
136 /* Compute the addend of a reloc.  If the reloc is to a common symbol,
137    the object file contains the value of the common symbol.  By the
138    time this is called, the linker may be using a different symbol
139    from a different object file with a different value.  Therefore, we
140    hack wildly to locate the original symbol from this file so that we
141    can make the correct adjustment.  This macro sets coffsym to the
142    symbol from the original file, and uses it to set the addend value
143    correctly.  If this is not a common symbol, the usual addend
144    calculation is done, except that an additional tweak is needed for
145    PC relative relocs.
146    FIXME: This macro refers to symbols and asect; these are from the
147    calling function, not the macro arguments.  */
148
149 #define CALC_ADDEND(abfd, ptr, reloc, cache_ptr)                \
150   {                                                             \
151     coff_symbol_type *coffsym = (coff_symbol_type *) NULL;      \
152     if (ptr && bfd_asymbol_bfd (ptr) != abfd)                   \
153       coffsym = (obj_symbols (abfd)                             \
154                  + (cache_ptr->sym_ptr_ptr - symbols));         \
155     else if (ptr)                                               \
156       coffsym = coff_symbol_from (abfd, ptr);                   \
157     if (coffsym != (coff_symbol_type *) NULL                    \
158         && coffsym->native->u.syment.n_scnum == 0)              \
159       cache_ptr->addend = - coffsym->native->u.syment.n_value;  \
160     else if (ptr && bfd_asymbol_bfd (ptr) == abfd               \
161              && ptr->section != (asection *) NULL)              \
162       cache_ptr->addend = - (ptr->section->vma + ptr->value);   \
163     else                                                        \
164       cache_ptr->addend = 0;                                    \
165     if (ptr && _bfd_m68klynx_howto_table[reloc.r_type].pc_relative) \
166       cache_ptr->addend += asect->vma;                          \
167   }
168
169
170 #include "coff-m68k.c"