1 /* BFD back-end for Motorola M68K COFF LynxOS files.
2 Copyright 1993, 1994 Free Software Foundation, Inc.
3 Written by Cygnus Support.
5 This file is part of BFD, the Binary File Descriptor library.
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.
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.
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. */
21 #define TARGET_SYM m68klynx_coff_vec
22 #define TARGET_NAME "coff-m68k-lynx"
26 #define COFF_LONG_FILENAMES
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
33 #define LYNX_SPECIAL_FN _bfd_m68klynx_special_fn
38 #ifdef ANSI_PROTOTYPES
39 struct internal_reloc;
40 struct coff_link_hash_entry;
41 struct internal_syment;
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 *,
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,
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. */
62 static bfd_reloc_status_type
63 _bfd_m68klynx_special_fn (abfd, reloc_entry, symbol, data, input_section,
64 output_bfd, error_message)
69 asection *input_section;
75 if (output_bfd == (bfd *) NULL)
76 return bfd_reloc_continue;
78 if (bfd_is_com_section (symbol->section))
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;
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;
103 x = ((x & ~howto->dst_mask) | (((x & howto->src_mask) + diff) & howto->dst_mask))
107 reloc_howto_type *howto = reloc_entry->howto;
108 unsigned char *addr = (unsigned char *) data + reloc_entry->address;
114 char x = bfd_get_8 (abfd, addr);
116 bfd_put_8 (abfd, x, addr);
122 short x = bfd_get_16 (abfd, addr);
124 bfd_put_16 (abfd, x, addr);
130 long x = bfd_get_32 (abfd, addr);
132 bfd_put_32 (abfd, x, addr);
141 /* Now let bfd_perform_relocation finish everything up. */
142 return bfd_reloc_continue;
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
154 FIXME: This macro refers to symbols and asect; these are from the
155 calling function, not the macro arguments. */
157 #define CALC_ADDEND(abfd, ptr, reloc, cache_ptr) \
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)); \
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); \
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; \
179 #define coff_bfd_link_add_symbols lynx_link_add_symbols
180 #define coff_rtype_to_howto coff_m68k_lynx_rtype_to_howto
182 #include "coff-m68k.c"
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. */
190 lynx_link_add_symbols (abfd, info)
192 struct bfd_link_info *info;
194 if (bfd_get_format (abfd) == bfd_archive)
198 first = bfd_openr_next_archived_file (abfd, (bfd *) NULL);
201 if (! bfd_check_format (first, bfd_object))
203 if (bfd_get_flavour (first) != bfd_target_coff_flavour)
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);
212 return _bfd_coff_link_add_symbols (abfd, info);
215 /* coff-m68k.c uses the special COFF backend linker. We need to
216 adjust common symbols.
218 We can't define this function until after we have included
219 coff-m68k.c, because it uses RTYPE2HOWTO. */
222 static const struct reloc_howto_struct *
223 coff_m68k_lynx_rtype_to_howto (abfd, sec, rel, h, sym, addendp)
226 struct internal_reloc *rel;
227 struct coff_link_hash_entry *h;
228 struct internal_syment *sym;
232 const struct reloc_howto_struct *howto;
234 RTYPE2HOWTO (&relent, rel);
236 howto = relent.howto;
238 if (sym != NULL && sym->n_scnum == 0 && sym->n_value != 0)
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
245 BFD_ASSERT (h != NULL);
246 *addendp -= sym->n_value;
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
252 if (h != NULL && h->root.type == bfd_link_hash_common)
253 *addendp += h->root.u.c.size;