* cofflink.c (coff_link_input_bfd): If r_symndx is -1, don't
[external/binutils.git] / bfd / cf-m68klynx.c
1 /* BFD back-end for Motorola M68K COFF LynxOS files.
2    Copyright 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 #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 #define _bfd_m68kcoff_reloc_type_lookup _bfd_m68klynx_reloc_type_lookup
32
33 #define LYNX_SPECIAL_FN _bfd_m68klynx_special_fn
34
35 #include "bfd.h"
36 #include "sysdep.h"
37
38 #ifdef ANSI_PROTOTYPES
39 struct internal_reloc;
40 struct coff_link_hash_entry;
41 struct internal_syment;
42 #endif
43
44 static bfd_reloc_status_type _bfd_m68klynx_special_fn
45   PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
46 static boolean lynx_link_add_symbols PARAMS ((bfd *, struct bfd_link_info *));
47 static const struct reloc_howto_struct *coff_m68k_lynx_rtype_to_howto
48   PARAMS ((bfd *, asection *, struct internal_reloc *,
49            struct coff_link_hash_entry *, struct internal_syment *,
50            bfd_vma *));
51
52 /* For some reason when using m68k COFF the value stored in the .text
53    section for a reference to a common symbol is the value itself plus
54    any desired offset.  (taken from work done by Ian Taylor, Cygnus Support,
55    for I386 COFF).  */
56
57 /* If we are producing relocateable output, we need to do some
58    adjustments to the object file that are not done by the
59    bfd_perform_relocation function.  This function is called by every
60    reloc type to make any required adjustments.  */
61
62 static bfd_reloc_status_type
63 _bfd_m68klynx_special_fn (abfd, reloc_entry, symbol, data, input_section,
64                           output_bfd, error_message)
65      bfd *abfd;
66      arelent *reloc_entry;
67      asymbol *symbol;
68      PTR data;
69      asection *input_section;
70      bfd *output_bfd;
71      char **error_message;
72 {
73   symvalue diff;
74
75   if (output_bfd == (bfd *) NULL)
76     return bfd_reloc_continue;
77
78   if (bfd_is_com_section (symbol->section))
79     {
80       /* We are relocating a common symbol.  The current value in the
81          object file is ORIG + OFFSET, where ORIG is the value of the
82          common symbol as seen by the object file when it was compiled
83          (this may be zero if the symbol was undefined) and OFFSET is
84          the offset into the common symbol (normally zero, but may be
85          non-zero when referring to a field in a common structure).
86          ORIG is the negative of reloc_entry->addend, which is set by
87          the CALC_ADDEND macro below.  We want to replace the value in
88          the object file with NEW + OFFSET, where NEW is the value of
89          the common symbol which we are going to put in the final
90          object file.  NEW is symbol->value.  */
91       diff = symbol->value + reloc_entry->addend;
92     }
93   else
94     {
95       /* For some reason bfd_perform_relocation always effectively
96          ignores the addend for a COFF target when producing
97          relocateable output.  This seems to be always wrong for 386
98          COFF, so we handle the addend here instead.  */
99       diff = reloc_entry->addend;
100     }
101
102 #define DOIT(x) \
103   x = ((x & ~howto->dst_mask) | (((x & howto->src_mask) + diff) & howto->dst_mask))
104
105   if (diff != 0)
106     {
107       const reloc_howto_type *howto = reloc_entry->howto;
108       unsigned char *addr = (unsigned char *) data + reloc_entry->address;
109
110       switch (howto->size)
111         {
112         case 0:
113           {
114             char x = bfd_get_8 (abfd, addr);
115             DOIT (x);
116             bfd_put_8 (abfd, x, addr);
117           }
118           break;
119
120         case 1:
121           {
122             short x = bfd_get_16 (abfd, addr);
123             DOIT (x);
124             bfd_put_16 (abfd, x, addr);
125           }
126           break;
127
128         case 2:
129           {
130             long x = bfd_get_32 (abfd, addr);
131             DOIT (x);
132             bfd_put_32 (abfd, x, addr);
133           }
134           break;
135
136         default:
137           abort ();
138         }
139     }
140
141   /* Now let bfd_perform_relocation finish everything up.  */
142   return bfd_reloc_continue;
143 }
144 /* Compute the addend of a reloc.  If the reloc is to a common symbol,
145    the object file contains the value of the common symbol.  By the
146    time this is called, the linker may be using a different symbol
147    from a different object file with a different value.  Therefore, we
148    hack wildly to locate the original symbol from this file so that we
149    can make the correct adjustment.  This macro sets coffsym to the
150    symbol from the original file, and uses it to set the addend value
151    correctly.  If this is not a common symbol, the usual addend
152    calculation is done, except that an additional tweak is needed for
153    PC relative relocs.
154    FIXME: This macro refers to symbols and asect; these are from the
155    calling function, not the macro arguments.  */
156
157 #define CALC_ADDEND(abfd, ptr, reloc, cache_ptr)                \
158   {                                                             \
159     coff_symbol_type *coffsym = (coff_symbol_type *) NULL;      \
160     if (ptr && bfd_asymbol_bfd (ptr) != abfd)                   \
161       coffsym = (obj_symbols (abfd)                             \
162                  + (cache_ptr->sym_ptr_ptr - symbols));         \
163     else if (ptr)                                               \
164       coffsym = coff_symbol_from (abfd, ptr);                   \
165     if (coffsym != (coff_symbol_type *) NULL                    \
166         && coffsym->native->u.syment.n_scnum == 0)              \
167       cache_ptr->addend = - coffsym->native->u.syment.n_value;  \
168     else if (ptr && bfd_asymbol_bfd (ptr) == abfd               \
169              && ptr->section != (asection *) NULL)              \
170       cache_ptr->addend = - (ptr->section->vma + ptr->value);   \
171     else                                                        \
172       cache_ptr->addend = 0;                                    \
173     if (ptr && (reloc.r_type == R_PCRBYTE                       \
174                 || reloc.r_type == R_PCRWORD                    \
175                 || reloc.r_type == R_PCRLONG))                  \
176       cache_ptr->addend += asect->vma;                          \
177   }
178
179 #define coff_bfd_link_add_symbols lynx_link_add_symbols
180 #define coff_rtype_to_howto coff_m68k_lynx_rtype_to_howto
181
182 #include "coff-m68k.c"
183
184 /* On Lynx, we may have a COFF archive which contains a.out elements.
185    This screws up the COFF linker, which expects that any archive it
186    gets contains COFF elements.  We override the add_symbols function
187    to check for this case.  */
188
189 static boolean
190 lynx_link_add_symbols (abfd, info)
191      bfd *abfd;
192      struct bfd_link_info *info;
193 {
194   if (bfd_get_format (abfd) == bfd_archive)
195     {
196       bfd *first;
197
198       first = bfd_openr_next_archived_file (abfd, (bfd *) NULL);
199       if (first == NULL)
200         return false;
201       if (! bfd_check_format (first, bfd_object))
202         return false;
203       if (bfd_get_flavour (first) != bfd_target_coff_flavour)
204         {
205           /* Treat the archive as though it were actually of the
206              flavour of its first element.  This ought to work,
207              since the archive support is fairly generic.  */
208           return (*first->xvec->_bfd_link_add_symbols) (abfd, info);
209         }
210     }
211
212   return _bfd_coff_link_add_symbols (abfd, info);
213 }
214
215 /* coff-m68k.c uses the special COFF backend linker.  We need to
216    adjust common symbols.
217
218    We can't define this function until after we have included
219    coff-m68k.c, because it uses RTYPE2HOWTO.  */
220
221 /*ARGSUSED*/
222 static const struct reloc_howto_struct *
223 coff_m68k_lynx_rtype_to_howto (abfd, sec, rel, h, sym, addendp)
224      bfd *abfd;
225      asection *sec;
226      struct internal_reloc *rel;
227      struct coff_link_hash_entry *h;
228      struct internal_syment *sym;
229      bfd_vma *addendp;
230 {
231   arelent relent;
232   const struct reloc_howto_struct *howto;
233
234   RTYPE2HOWTO (&relent, rel);
235
236   howto = relent.howto;
237
238   if (sym != NULL && sym->n_scnum == 0 && sym->n_value != 0)
239     {
240       /* This is a common symbol.  The section contents include the
241          size (sym->n_value) as an addend.  The relocate_section
242          function will be adding in the final value of the symbol.  We
243          need to subtract out the current size in order to get the
244          correct result.  */
245       BFD_ASSERT (h != NULL);
246       *addendp -= sym->n_value;
247     }
248
249   /* If the output symbol is common (in which case this must be a
250      relocateable link), we need to add in the final size of the
251      common symbol.  */
252   if (h != NULL && h->root.type == bfd_link_hash_common)
253     *addendp += h->root.u.c.size;
254
255   return howto;
256 }