daily update
[binutils.git] / bfd / coff-m68k.c
blob0947811687a9afb5d15c5f2b8e424925639d35be
1 /* BFD back-end for Motorola 68000 COFF binaries.
2 Copyright 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1999,
3 2000, 2001, 2002, 2003
4 Free Software Foundation, Inc.
5 Written by Cygnus Support.
7 This file is part of BFD, the Binary File Descriptor library.
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.
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.
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. */
23 #include "bfd.h"
24 #include "sysdep.h"
25 #include "libbfd.h"
26 #include "coff/m68k.h"
27 #include "coff/internal.h"
28 #include "libcoff.h"
30 /* This source file is compiled multiple times for various m68k COFF
31 variants. The following macros control its behaviour:
33 TARGET_SYM
34 The C name of the BFD target vector. The default is m68kcoff_vec.
35 TARGET_NAME
36 The user visible target name. The default is "coff-m68k".
37 NAMES_HAVE_UNDERSCORE
38 Whether symbol names have an underscore.
39 ONLY_DECLARE_RELOCS
40 Only declare the relocation howto array. Don't actually compile
41 it. The actual array will be picked up in another version of the
42 file.
43 STATIC_RELOCS
44 Make the relocation howto array, and associated functions, static.
45 COFF_COMMON_ADDEND
46 If this is defined, then, for a relocation against a common
47 symbol, the object file holds the value (the size) of the common
48 symbol. If this is not defined, then, for a relocation against a
49 common symbol, the object file holds zero. */
51 #define COFF_DEFAULT_SECTION_ALIGNMENT_POWER (2)
53 #ifndef COFF_PAGE_SIZE
54 /* The page size is a guess based on ELF. */
55 #define COFF_PAGE_SIZE 0x2000
56 #endif
58 #ifndef COFF_COMMON_ADDEND
59 #define RELOC_SPECIAL_FN 0
60 #else
61 static bfd_reloc_status_type m68kcoff_common_addend_special_fn
62 PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
63 static reloc_howto_type *m68kcoff_common_addend_rtype_to_howto
64 PARAMS ((bfd *, asection *, struct internal_reloc *,
65 struct coff_link_hash_entry *, struct internal_syment *,
66 bfd_vma *));
67 #define RELOC_SPECIAL_FN m68kcoff_common_addend_special_fn
68 #endif
70 static bfd_boolean m68k_coff_is_local_label_name
71 PARAMS ((bfd *, const char *));
73 /* On the delta, a symbol starting with L% is local. We won't see
74 such a symbol on other platforms, so it should be safe to always
75 consider it local here. */
77 static bfd_boolean
78 m68k_coff_is_local_label_name (abfd, name)
79 bfd *abfd;
80 const char *name;
82 if (name[0] == 'L' && name[1] == '%')
83 return TRUE;
85 return _bfd_coff_is_local_label_name (abfd, name);
88 #ifndef STATIC_RELOCS
89 /* Clean up namespace. */
90 #define m68kcoff_howto_table _bfd_m68kcoff_howto_table
91 #define m68k_rtype2howto _bfd_m68kcoff_rtype2howto
92 #define m68k_howto2rtype _bfd_m68kcoff_howto2rtype
93 #define m68k_reloc_type_lookup _bfd_m68kcoff_reloc_type_lookup
94 #endif
96 #ifdef ONLY_DECLARE_RELOCS
97 extern reloc_howto_type m68kcoff_howto_table[];
98 #else
99 #ifdef STATIC_RELOCS
100 static
101 #endif
102 reloc_howto_type m68kcoff_howto_table[] =
104 HOWTO (R_RELBYTE, 0, 0, 8, FALSE, 0, complain_overflow_bitfield, RELOC_SPECIAL_FN, "8", TRUE, 0x000000ff,0x000000ff, FALSE),
105 HOWTO (R_RELWORD, 0, 1, 16, FALSE, 0, complain_overflow_bitfield, RELOC_SPECIAL_FN, "16", TRUE, 0x0000ffff,0x0000ffff, FALSE),
106 HOWTO (R_RELLONG, 0, 2, 32, FALSE, 0, complain_overflow_bitfield, RELOC_SPECIAL_FN, "32", TRUE, 0xffffffff,0xffffffff, FALSE),
107 HOWTO (R_PCRBYTE, 0, 0, 8, TRUE, 0, complain_overflow_signed, RELOC_SPECIAL_FN, "DISP8", TRUE, 0x000000ff,0x000000ff, FALSE),
108 HOWTO (R_PCRWORD, 0, 1, 16, TRUE, 0, complain_overflow_signed, RELOC_SPECIAL_FN, "DISP16", TRUE, 0x0000ffff,0x0000ffff, FALSE),
109 HOWTO (R_PCRLONG, 0, 2, 32, TRUE, 0, complain_overflow_signed, RELOC_SPECIAL_FN, "DISP32", TRUE, 0xffffffff,0xffffffff, FALSE),
110 HOWTO (R_RELLONG_NEG, 0, -2, 32, FALSE, 0, complain_overflow_bitfield, RELOC_SPECIAL_FN, "-32", TRUE, 0xffffffff,0xffffffff, FALSE),
112 #endif /* not ONLY_DECLARE_RELOCS */
114 #ifndef BADMAG
115 #define BADMAG(x) M68KBADMAG(x)
116 #endif
117 #define M68 1 /* Customize coffcode.h */
119 /* Turn a howto into a reloc number */
121 #ifdef ONLY_DECLARE_RELOCS
122 extern void m68k_rtype2howto PARAMS ((arelent *internal, int relocentry));
123 extern int m68k_howto2rtype PARAMS ((reloc_howto_type *));
124 extern reloc_howto_type *m68k_reloc_type_lookup
125 PARAMS ((bfd *, bfd_reloc_code_real_type));
126 #else
128 #ifdef STATIC_RELOCS
129 #define STAT_REL static
130 #else
131 #define STAT_REL
132 #endif
134 STAT_REL reloc_howto_type * m68k_reloc_type_lookup PARAMS ((bfd *, bfd_reloc_code_real_type));
135 STAT_REL int m68k_howto2rtype PARAMS ((reloc_howto_type *));
136 STAT_REL void m68k_rtype2howto PARAMS ((arelent *, int));
139 STAT_REL void
140 m68k_rtype2howto(internal, relocentry)
141 arelent *internal;
142 int relocentry;
144 switch (relocentry)
146 case R_RELBYTE: internal->howto = m68kcoff_howto_table + 0; break;
147 case R_RELWORD: internal->howto = m68kcoff_howto_table + 1; break;
148 case R_RELLONG: internal->howto = m68kcoff_howto_table + 2; break;
149 case R_PCRBYTE: internal->howto = m68kcoff_howto_table + 3; break;
150 case R_PCRWORD: internal->howto = m68kcoff_howto_table + 4; break;
151 case R_PCRLONG: internal->howto = m68kcoff_howto_table + 5; break;
152 case R_RELLONG_NEG: internal->howto = m68kcoff_howto_table + 6; break;
156 STAT_REL int
157 m68k_howto2rtype (internal)
158 reloc_howto_type *internal;
160 if (internal->pc_relative)
162 switch (internal->bitsize)
164 case 32: return R_PCRLONG;
165 case 16: return R_PCRWORD;
166 case 8: return R_PCRBYTE;
169 else
171 switch (internal->bitsize)
173 case 32: return R_RELLONG;
174 case 16: return R_RELWORD;
175 case 8: return R_RELBYTE;
178 return R_RELLONG;
181 STAT_REL reloc_howto_type *
182 m68k_reloc_type_lookup (abfd, code)
183 bfd *abfd ATTRIBUTE_UNUSED;
184 bfd_reloc_code_real_type code;
186 switch (code)
188 default: return NULL;
189 case BFD_RELOC_8: return m68kcoff_howto_table + 0;
190 case BFD_RELOC_16: return m68kcoff_howto_table + 1;
191 case BFD_RELOC_CTOR:
192 case BFD_RELOC_32: return m68kcoff_howto_table + 2;
193 case BFD_RELOC_8_PCREL: return m68kcoff_howto_table + 3;
194 case BFD_RELOC_16_PCREL: return m68kcoff_howto_table + 4;
195 case BFD_RELOC_32_PCREL: return m68kcoff_howto_table + 5;
196 /* FIXME: There doesn't seem to be a code for R_RELLONG_NEG. */
198 /*NOTREACHED*/
201 #endif /* not ONLY_DECLARE_RELOCS */
203 #define RTYPE2HOWTO(internal, relocentry) \
204 m68k_rtype2howto(internal, (relocentry)->r_type)
206 #define SELECT_RELOC(external, internal) \
207 external.r_type = m68k_howto2rtype (internal)
209 #define coff_bfd_reloc_type_lookup m68k_reloc_type_lookup
211 #ifndef COFF_COMMON_ADDEND
212 #ifndef coff_rtype_to_howto
214 #define coff_rtype_to_howto m68kcoff_rtype_to_howto
216 static reloc_howto_type *m68kcoff_rtype_to_howto
217 PARAMS ((bfd *, asection *, struct internal_reloc *,
218 struct coff_link_hash_entry *, struct internal_syment *,
219 bfd_vma *));
221 static reloc_howto_type *
222 m68kcoff_rtype_to_howto (abfd, sec, rel, h, sym, addendp)
223 bfd *abfd ATTRIBUTE_UNUSED;
224 asection *sec;
225 struct internal_reloc *rel;
226 struct coff_link_hash_entry *h ATTRIBUTE_UNUSED;
227 struct internal_syment *sym ATTRIBUTE_UNUSED;
228 bfd_vma *addendp;
230 arelent relent;
231 reloc_howto_type *howto;
233 RTYPE2HOWTO (&relent, rel);
235 howto = relent.howto;
237 if (howto->pc_relative)
238 *addendp += sec->vma;
240 return howto;
243 #endif /* ! defined (coff_rtype_to_howto) */
244 #endif /* ! defined (COFF_COMMON_ADDEND) */
246 #ifdef COFF_COMMON_ADDEND
248 /* If COFF_COMMON_ADDEND is defined, then when using m68k COFF the
249 value stored in the .text section for a reference to a common
250 symbol is the value itself plus any desired offset. (taken from
251 work done by Ian Taylor, Cygnus Support, for I386 COFF). */
253 /* If we are producing relocatable output, we need to do some
254 adjustments to the object file that are not done by the
255 bfd_perform_relocation function. This function is called by every
256 reloc type to make any required adjustments. */
258 static bfd_reloc_status_type
259 m68kcoff_common_addend_special_fn (abfd, reloc_entry, symbol, data,
260 input_section, output_bfd, error_message)
261 bfd *abfd;
262 arelent *reloc_entry;
263 asymbol *symbol;
264 PTR data;
265 asection *input_section ATTRIBUTE_UNUSED;
266 bfd *output_bfd;
267 char **error_message ATTRIBUTE_UNUSED;
269 symvalue diff;
271 if (output_bfd == (bfd *) NULL)
272 return bfd_reloc_continue;
274 if (bfd_is_com_section (symbol->section))
276 /* We are relocating a common symbol. The current value in the
277 object file is ORIG + OFFSET, where ORIG is the value of the
278 common symbol as seen by the object file when it was compiled
279 (this may be zero if the symbol was undefined) and OFFSET is
280 the offset into the common symbol (normally zero, but may be
281 non-zero when referring to a field in a common structure).
282 ORIG is the negative of reloc_entry->addend, which is set by
283 the CALC_ADDEND macro below. We want to replace the value in
284 the object file with NEW + OFFSET, where NEW is the value of
285 the common symbol which we are going to put in the final
286 object file. NEW is symbol->value. */
287 diff = symbol->value + reloc_entry->addend;
289 else
291 /* For some reason bfd_perform_relocation always effectively
292 ignores the addend for a COFF target when producing
293 relocatable output. This seems to be always wrong for 386
294 COFF, so we handle the addend here instead. */
295 diff = reloc_entry->addend;
298 #define DOIT(x) \
299 x = ((x & ~howto->dst_mask) | (((x & howto->src_mask) + diff) & howto->dst_mask))
301 if (diff != 0)
303 reloc_howto_type *howto = reloc_entry->howto;
304 unsigned char *addr = (unsigned char *) data + reloc_entry->address;
306 switch (howto->size)
308 case 0:
310 char x = bfd_get_8 (abfd, addr);
311 DOIT (x);
312 bfd_put_8 (abfd, x, addr);
314 break;
316 case 1:
318 short x = bfd_get_16 (abfd, addr);
319 DOIT (x);
320 bfd_put_16 (abfd, (bfd_vma) x, addr);
322 break;
324 case 2:
326 long x = bfd_get_32 (abfd, addr);
327 DOIT (x);
328 bfd_put_32 (abfd, (bfd_vma) x, addr);
330 break;
332 default:
333 abort ();
337 /* Now let bfd_perform_relocation finish everything up. */
338 return bfd_reloc_continue;
341 /* Compute the addend of a reloc. If the reloc is to a common symbol,
342 the object file contains the value of the common symbol. By the
343 time this is called, the linker may be using a different symbol
344 from a different object file with a different value. Therefore, we
345 hack wildly to locate the original symbol from this file so that we
346 can make the correct adjustment. This macro sets coffsym to the
347 symbol from the original file, and uses it to set the addend value
348 correctly. If this is not a common symbol, the usual addend
349 calculation is done, except that an additional tweak is needed for
350 PC relative relocs.
351 FIXME: This macro refers to symbols and asect; these are from the
352 calling function, not the macro arguments. */
354 #define CALC_ADDEND(abfd, ptr, reloc, cache_ptr) \
356 coff_symbol_type *coffsym = (coff_symbol_type *) NULL; \
357 if (ptr && bfd_asymbol_bfd (ptr) != abfd) \
358 coffsym = (obj_symbols (abfd) \
359 + (cache_ptr->sym_ptr_ptr - symbols)); \
360 else if (ptr) \
361 coffsym = coff_symbol_from (abfd, ptr); \
362 if (coffsym != (coff_symbol_type *) NULL \
363 && coffsym->native->u.syment.n_scnum == 0) \
364 cache_ptr->addend = - coffsym->native->u.syment.n_value; \
365 else if (ptr && bfd_asymbol_bfd (ptr) == abfd \
366 && ptr->section != (asection *) NULL) \
367 cache_ptr->addend = - (ptr->section->vma + ptr->value); \
368 else \
369 cache_ptr->addend = 0; \
370 if (ptr && (reloc.r_type == R_PCRBYTE \
371 || reloc.r_type == R_PCRWORD \
372 || reloc.r_type == R_PCRLONG)) \
373 cache_ptr->addend += asect->vma; \
376 #ifndef coff_rtype_to_howto
378 /* coff-m68k.c uses the special COFF backend linker. We need to
379 adjust common symbols. */
381 static reloc_howto_type *
382 m68kcoff_common_addend_rtype_to_howto (abfd, sec, rel, h, sym, addendp)
383 bfd *abfd ATTRIBUTE_UNUSED;
384 asection *sec;
385 struct internal_reloc *rel;
386 struct coff_link_hash_entry *h;
387 struct internal_syment *sym;
388 bfd_vma *addendp;
390 arelent relent;
391 reloc_howto_type *howto;
393 RTYPE2HOWTO (&relent, rel);
395 howto = relent.howto;
397 if (howto->pc_relative)
398 *addendp += sec->vma;
400 if (sym != NULL && sym->n_scnum == 0 && sym->n_value != 0)
402 /* This is a common symbol. The section contents include the
403 size (sym->n_value) as an addend. The relocate_section
404 function will be adding in the final value of the symbol. We
405 need to subtract out the current size in order to get the
406 correct result. */
407 BFD_ASSERT (h != NULL);
408 *addendp -= sym->n_value;
411 /* If the output symbol is common (in which case this must be a
412 relocatable link), we need to add in the final size of the
413 common symbol. */
414 if (h != NULL && h->root.type == bfd_link_hash_common)
415 *addendp += h->root.u.c.size;
417 return howto;
420 #define coff_rtype_to_howto m68kcoff_common_addend_rtype_to_howto
422 #endif /* ! defined (coff_rtype_to_howto) */
424 #endif /* COFF_COMMON_ADDEND */
426 #if !defined ONLY_DECLARE_RELOCS && ! defined STATIC_RELOCS
427 /* Given a .data section and a .emreloc in-memory section, store
428 relocation information into the .emreloc section which can be
429 used at runtime to relocate the section. This is called by the
430 linker when the --embedded-relocs switch is used. This is called
431 after the add_symbols entry point has been called for all the
432 objects, and before the final_link entry point is called. */
434 bfd_boolean
435 bfd_m68k_coff_create_embedded_relocs (abfd, info, datasec, relsec, errmsg)
436 bfd *abfd;
437 struct bfd_link_info *info;
438 asection *datasec;
439 asection *relsec;
440 char **errmsg;
442 char *extsyms;
443 bfd_size_type symesz;
444 struct internal_reloc *irel, *irelend;
445 bfd_byte *p;
446 bfd_size_type amt;
448 BFD_ASSERT (! info->relocatable);
450 *errmsg = NULL;
452 if (datasec->reloc_count == 0)
453 return TRUE;
455 extsyms = obj_coff_external_syms (abfd);
456 symesz = bfd_coff_symesz (abfd);
458 irel = _bfd_coff_read_internal_relocs (abfd, datasec, TRUE, NULL, FALSE,
459 NULL);
460 irelend = irel + datasec->reloc_count;
462 amt = (bfd_size_type) datasec->reloc_count * 12;
463 relsec->contents = (bfd_byte *) bfd_alloc (abfd, amt);
464 if (relsec->contents == NULL)
465 return FALSE;
467 p = relsec->contents;
469 for (; irel < irelend; irel++, p += 12)
471 asection *targetsec;
473 /* We are going to write a four byte longword into the runtime
474 reloc section. The longword will be the address in the data
475 section which must be relocated. It is followed by the name
476 of the target section NUL-padded or truncated to 8
477 characters. */
479 /* We can only relocate absolute longword relocs at run time. */
480 if (irel->r_type != R_RELLONG)
482 *errmsg = _("unsupported reloc type");
483 bfd_set_error (bfd_error_bad_value);
484 return FALSE;
487 if (irel->r_symndx == -1)
488 targetsec = bfd_abs_section_ptr;
489 else
491 struct coff_link_hash_entry *h;
493 h = obj_coff_sym_hashes (abfd)[irel->r_symndx];
494 if (h == NULL)
496 struct internal_syment isym;
498 bfd_coff_swap_sym_in (abfd, extsyms + symesz * irel->r_symndx,
499 &isym);
500 targetsec = coff_section_from_bfd_index (abfd, isym.n_scnum);
502 else if (h->root.type == bfd_link_hash_defined
503 || h->root.type == bfd_link_hash_defweak)
504 targetsec = h->root.u.def.section;
505 else
506 targetsec = NULL;
509 bfd_put_32 (abfd,
510 (irel->r_vaddr - datasec->vma + datasec->output_offset), p);
511 memset (p + 4, 0, 8);
512 if (targetsec != NULL)
513 strncpy (p + 4, targetsec->output_section->name, 8);
516 return TRUE;
518 #endif /* neither ONLY_DECLARE_RELOCS not STATIC_RELOCS */
520 #define coff_bfd_is_local_label_name m68k_coff_is_local_label_name
522 #define coff_relocate_section _bfd_coff_generic_relocate_section
524 #include "coffcode.h"
526 #ifndef TARGET_SYM
527 #define TARGET_SYM m68kcoff_vec
528 #endif
530 #ifndef TARGET_NAME
531 #define TARGET_NAME "coff-m68k"
532 #endif
534 #ifdef NAMES_HAVE_UNDERSCORE
535 CREATE_BIG_COFF_TARGET_VEC (TARGET_SYM, TARGET_NAME, D_PAGED, 0, '_', NULL, COFF_SWAP_TABLE)
536 #else
537 CREATE_BIG_COFF_TARGET_VEC (TARGET_SYM, TARGET_NAME, D_PAGED, 0, 0, NULL, COFF_SWAP_TABLE)
538 #endif