* elf-m10300.c (mn10300_elf_check_relocs): Delete dead code.
[binutils.git] / bfd / coff-m68k.c
blob185b0590277def36158f13ccc3e28606b72cda4e
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, 2005, 2007
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 3 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., 51 Franklin Street - Fifth Floor, Boston,
22 MA 02110-1301, USA. */
24 #include "sysdep.h"
25 #include "bfd.h"
26 #include "libbfd.h"
27 #include "coff/m68k.h"
28 #include "coff/internal.h"
29 #include "libcoff.h"
31 /* This source file is compiled multiple times for various m68k COFF
32 variants. The following macros control its behaviour:
34 TARGET_SYM
35 The C name of the BFD target vector. The default is m68kcoff_vec.
36 TARGET_NAME
37 The user visible target name. The default is "coff-m68k".
38 NAMES_HAVE_UNDERSCORE
39 Whether symbol names have an underscore.
40 ONLY_DECLARE_RELOCS
41 Only declare the relocation howto array. Don't actually compile
42 it. The actual array will be picked up in another version of the
43 file.
44 STATIC_RELOCS
45 Make the relocation howto array, and associated functions, static.
46 COFF_COMMON_ADDEND
47 If this is defined, then, for a relocation against a common
48 symbol, the object file holds the value (the size) of the common
49 symbol. If this is not defined, then, for a relocation against a
50 common symbol, the object file holds zero. */
52 #define COFF_DEFAULT_SECTION_ALIGNMENT_POWER (2)
54 #ifndef COFF_PAGE_SIZE
55 /* The page size is a guess based on ELF. */
56 #define COFF_PAGE_SIZE 0x2000
57 #endif
59 #ifndef COFF_COMMON_ADDEND
60 #define RELOC_SPECIAL_FN 0
61 #else
62 static bfd_reloc_status_type m68kcoff_common_addend_special_fn
63 PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
64 static reloc_howto_type *m68kcoff_common_addend_rtype_to_howto
65 PARAMS ((bfd *, asection *, struct internal_reloc *,
66 struct coff_link_hash_entry *, struct internal_syment *,
67 bfd_vma *));
68 #define RELOC_SPECIAL_FN m68kcoff_common_addend_special_fn
69 #endif
71 static bfd_boolean m68k_coff_is_local_label_name
72 PARAMS ((bfd *, const char *));
74 /* On the delta, a symbol starting with L% is local. We won't see
75 such a symbol on other platforms, so it should be safe to always
76 consider it local here. */
78 static bfd_boolean
79 m68k_coff_is_local_label_name (abfd, name)
80 bfd *abfd;
81 const char *name;
83 if (name[0] == 'L' && name[1] == '%')
84 return TRUE;
86 return _bfd_coff_is_local_label_name (abfd, name);
89 #ifndef STATIC_RELOCS
90 /* Clean up namespace. */
91 #define m68kcoff_howto_table _bfd_m68kcoff_howto_table
92 #define m68k_rtype2howto _bfd_m68kcoff_rtype2howto
93 #define m68k_howto2rtype _bfd_m68kcoff_howto2rtype
94 #define m68k_reloc_type_lookup _bfd_m68kcoff_reloc_type_lookup
95 #define m68k_reloc_name_lookup _bfd_m68kcoff_reloc_name_lookup
96 #endif
98 #ifdef ONLY_DECLARE_RELOCS
99 extern reloc_howto_type m68kcoff_howto_table[];
100 #else
101 #ifdef STATIC_RELOCS
102 static
103 #endif
104 reloc_howto_type m68kcoff_howto_table[] =
106 HOWTO (R_RELBYTE, 0, 0, 8, FALSE, 0, complain_overflow_bitfield, RELOC_SPECIAL_FN, "8", TRUE, 0x000000ff,0x000000ff, FALSE),
107 HOWTO (R_RELWORD, 0, 1, 16, FALSE, 0, complain_overflow_bitfield, RELOC_SPECIAL_FN, "16", TRUE, 0x0000ffff,0x0000ffff, FALSE),
108 HOWTO (R_RELLONG, 0, 2, 32, FALSE, 0, complain_overflow_bitfield, RELOC_SPECIAL_FN, "32", TRUE, 0xffffffff,0xffffffff, FALSE),
109 HOWTO (R_PCRBYTE, 0, 0, 8, TRUE, 0, complain_overflow_signed, RELOC_SPECIAL_FN, "DISP8", TRUE, 0x000000ff,0x000000ff, FALSE),
110 HOWTO (R_PCRWORD, 0, 1, 16, TRUE, 0, complain_overflow_signed, RELOC_SPECIAL_FN, "DISP16", TRUE, 0x0000ffff,0x0000ffff, FALSE),
111 HOWTO (R_PCRLONG, 0, 2, 32, TRUE, 0, complain_overflow_signed, RELOC_SPECIAL_FN, "DISP32", TRUE, 0xffffffff,0xffffffff, FALSE),
112 HOWTO (R_RELLONG_NEG, 0, -2, 32, FALSE, 0, complain_overflow_bitfield, RELOC_SPECIAL_FN, "-32", TRUE, 0xffffffff,0xffffffff, FALSE),
114 #endif /* not ONLY_DECLARE_RELOCS */
116 #ifndef BADMAG
117 #define BADMAG(x) M68KBADMAG(x)
118 #endif
119 #define M68 1 /* Customize coffcode.h */
121 /* Turn a howto into a reloc number */
123 #ifdef ONLY_DECLARE_RELOCS
124 extern void m68k_rtype2howto PARAMS ((arelent *internal, int relocentry));
125 extern int m68k_howto2rtype PARAMS ((reloc_howto_type *));
126 extern reloc_howto_type *m68k_reloc_type_lookup
127 PARAMS ((bfd *, bfd_reloc_code_real_type));
128 extern reloc_howto_type *m68k_reloc_name_lookup
129 PARAMS ((bfd *, const char *));
130 #else
132 #ifdef STATIC_RELOCS
133 #define STAT_REL static
134 #else
135 #define STAT_REL
136 #endif
138 STAT_REL reloc_howto_type * m68k_reloc_type_lookup PARAMS ((bfd *, bfd_reloc_code_real_type));
139 STAT_REL reloc_howto_type * m68k_reloc_name_lookup PARAMS ((bfd *, const char *));
140 STAT_REL int m68k_howto2rtype PARAMS ((reloc_howto_type *));
141 STAT_REL void m68k_rtype2howto PARAMS ((arelent *, int));
144 STAT_REL void
145 m68k_rtype2howto(internal, relocentry)
146 arelent *internal;
147 int relocentry;
149 switch (relocentry)
151 case R_RELBYTE: internal->howto = m68kcoff_howto_table + 0; break;
152 case R_RELWORD: internal->howto = m68kcoff_howto_table + 1; break;
153 case R_RELLONG: internal->howto = m68kcoff_howto_table + 2; break;
154 case R_PCRBYTE: internal->howto = m68kcoff_howto_table + 3; break;
155 case R_PCRWORD: internal->howto = m68kcoff_howto_table + 4; break;
156 case R_PCRLONG: internal->howto = m68kcoff_howto_table + 5; break;
157 case R_RELLONG_NEG: internal->howto = m68kcoff_howto_table + 6; break;
161 STAT_REL int
162 m68k_howto2rtype (internal)
163 reloc_howto_type *internal;
165 if (internal->pc_relative)
167 switch (internal->bitsize)
169 case 32: return R_PCRLONG;
170 case 16: return R_PCRWORD;
171 case 8: return R_PCRBYTE;
174 else
176 switch (internal->bitsize)
178 case 32: return R_RELLONG;
179 case 16: return R_RELWORD;
180 case 8: return R_RELBYTE;
183 return R_RELLONG;
186 STAT_REL reloc_howto_type *
187 m68k_reloc_type_lookup (abfd, code)
188 bfd *abfd ATTRIBUTE_UNUSED;
189 bfd_reloc_code_real_type code;
191 switch (code)
193 default: return NULL;
194 case BFD_RELOC_8: return m68kcoff_howto_table + 0;
195 case BFD_RELOC_16: return m68kcoff_howto_table + 1;
196 case BFD_RELOC_CTOR:
197 case BFD_RELOC_32: return m68kcoff_howto_table + 2;
198 case BFD_RELOC_8_PCREL: return m68kcoff_howto_table + 3;
199 case BFD_RELOC_16_PCREL: return m68kcoff_howto_table + 4;
200 case BFD_RELOC_32_PCREL: return m68kcoff_howto_table + 5;
201 /* FIXME: There doesn't seem to be a code for R_RELLONG_NEG. */
203 /*NOTREACHED*/
206 STAT_REL reloc_howto_type *
207 m68k_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
208 const char *r_name)
210 unsigned int i;
212 for (i = 0;
213 i < sizeof (m68kcoff_howto_table) / sizeof (m68kcoff_howto_table[0]);
214 i++)
215 if (m68kcoff_howto_table[i].name != NULL
216 && strcasecmp (m68kcoff_howto_table[i].name, r_name) == 0)
217 return &m68kcoff_howto_table[i];
219 return NULL;
222 #endif /* not ONLY_DECLARE_RELOCS */
224 #define RTYPE2HOWTO(internal, relocentry) \
225 m68k_rtype2howto(internal, (relocentry)->r_type)
227 #define SELECT_RELOC(external, internal) \
228 external.r_type = m68k_howto2rtype (internal)
230 #define coff_bfd_reloc_type_lookup m68k_reloc_type_lookup
231 #define coff_bfd_reloc_name_lookup m68k_reloc_name_lookup
233 #ifndef COFF_COMMON_ADDEND
234 #ifndef coff_rtype_to_howto
236 #define coff_rtype_to_howto m68kcoff_rtype_to_howto
238 static reloc_howto_type *m68kcoff_rtype_to_howto
239 PARAMS ((bfd *, asection *, struct internal_reloc *,
240 struct coff_link_hash_entry *, struct internal_syment *,
241 bfd_vma *));
243 static reloc_howto_type *
244 m68kcoff_rtype_to_howto (abfd, sec, rel, h, sym, addendp)
245 bfd *abfd ATTRIBUTE_UNUSED;
246 asection *sec;
247 struct internal_reloc *rel;
248 struct coff_link_hash_entry *h ATTRIBUTE_UNUSED;
249 struct internal_syment *sym ATTRIBUTE_UNUSED;
250 bfd_vma *addendp;
252 arelent relent;
253 reloc_howto_type *howto;
255 relent.howto = NULL;
256 RTYPE2HOWTO (&relent, rel);
258 howto = relent.howto;
260 if (howto != NULL && howto->pc_relative)
261 *addendp += sec->vma;
263 return howto;
266 #endif /* ! defined (coff_rtype_to_howto) */
267 #endif /* ! defined (COFF_COMMON_ADDEND) */
269 #ifdef COFF_COMMON_ADDEND
271 /* If COFF_COMMON_ADDEND is defined, then when using m68k COFF the
272 value stored in the .text section for a reference to a common
273 symbol is the value itself plus any desired offset. (taken from
274 work done by Ian Taylor, Cygnus Support, for I386 COFF). */
276 /* If we are producing relocatable output, we need to do some
277 adjustments to the object file that are not done by the
278 bfd_perform_relocation function. This function is called by every
279 reloc type to make any required adjustments. */
281 static bfd_reloc_status_type
282 m68kcoff_common_addend_special_fn (abfd, reloc_entry, symbol, data,
283 input_section, output_bfd, error_message)
284 bfd *abfd;
285 arelent *reloc_entry;
286 asymbol *symbol;
287 PTR data;
288 asection *input_section ATTRIBUTE_UNUSED;
289 bfd *output_bfd;
290 char **error_message ATTRIBUTE_UNUSED;
292 symvalue diff;
294 if (output_bfd == (bfd *) NULL)
295 return bfd_reloc_continue;
297 if (bfd_is_com_section (symbol->section))
299 /* We are relocating a common symbol. The current value in the
300 object file is ORIG + OFFSET, where ORIG is the value of the
301 common symbol as seen by the object file when it was compiled
302 (this may be zero if the symbol was undefined) and OFFSET is
303 the offset into the common symbol (normally zero, but may be
304 non-zero when referring to a field in a common structure).
305 ORIG is the negative of reloc_entry->addend, which is set by
306 the CALC_ADDEND macro below. We want to replace the value in
307 the object file with NEW + OFFSET, where NEW is the value of
308 the common symbol which we are going to put in the final
309 object file. NEW is symbol->value. */
310 diff = symbol->value + reloc_entry->addend;
312 else
314 /* For some reason bfd_perform_relocation always effectively
315 ignores the addend for a COFF target when producing
316 relocatable output. This seems to be always wrong for 386
317 COFF, so we handle the addend here instead. */
318 diff = reloc_entry->addend;
321 #define DOIT(x) \
322 x = ((x & ~howto->dst_mask) | (((x & howto->src_mask) + diff) & howto->dst_mask))
324 if (diff != 0)
326 reloc_howto_type *howto = reloc_entry->howto;
327 unsigned char *addr = (unsigned char *) data + reloc_entry->address;
329 switch (howto->size)
331 case 0:
333 char x = bfd_get_8 (abfd, addr);
334 DOIT (x);
335 bfd_put_8 (abfd, x, addr);
337 break;
339 case 1:
341 short x = bfd_get_16 (abfd, addr);
342 DOIT (x);
343 bfd_put_16 (abfd, (bfd_vma) x, addr);
345 break;
347 case 2:
349 long x = bfd_get_32 (abfd, addr);
350 DOIT (x);
351 bfd_put_32 (abfd, (bfd_vma) x, addr);
353 break;
355 default:
356 abort ();
360 /* Now let bfd_perform_relocation finish everything up. */
361 return bfd_reloc_continue;
364 /* Compute the addend of a reloc. If the reloc is to a common symbol,
365 the object file contains the value of the common symbol. By the
366 time this is called, the linker may be using a different symbol
367 from a different object file with a different value. Therefore, we
368 hack wildly to locate the original symbol from this file so that we
369 can make the correct adjustment. This macro sets coffsym to the
370 symbol from the original file, and uses it to set the addend value
371 correctly. If this is not a common symbol, the usual addend
372 calculation is done, except that an additional tweak is needed for
373 PC relative relocs.
374 FIXME: This macro refers to symbols and asect; these are from the
375 calling function, not the macro arguments. */
377 #define CALC_ADDEND(abfd, ptr, reloc, cache_ptr) \
379 coff_symbol_type *coffsym = (coff_symbol_type *) NULL; \
380 if (ptr && bfd_asymbol_bfd (ptr) != abfd) \
381 coffsym = (obj_symbols (abfd) \
382 + (cache_ptr->sym_ptr_ptr - symbols)); \
383 else if (ptr) \
384 coffsym = coff_symbol_from (abfd, ptr); \
385 if (coffsym != (coff_symbol_type *) NULL \
386 && coffsym->native->u.syment.n_scnum == 0) \
387 cache_ptr->addend = - coffsym->native->u.syment.n_value; \
388 else if (ptr && bfd_asymbol_bfd (ptr) == abfd \
389 && ptr->section != (asection *) NULL) \
390 cache_ptr->addend = - (ptr->section->vma + ptr->value); \
391 else \
392 cache_ptr->addend = 0; \
393 if (ptr && (reloc.r_type == R_PCRBYTE \
394 || reloc.r_type == R_PCRWORD \
395 || reloc.r_type == R_PCRLONG)) \
396 cache_ptr->addend += asect->vma; \
399 #ifndef coff_rtype_to_howto
401 /* coff-m68k.c uses the special COFF backend linker. We need to
402 adjust common symbols. */
404 static reloc_howto_type *
405 m68kcoff_common_addend_rtype_to_howto (abfd, sec, rel, h, sym, addendp)
406 bfd *abfd ATTRIBUTE_UNUSED;
407 asection *sec;
408 struct internal_reloc *rel;
409 struct coff_link_hash_entry *h;
410 struct internal_syment *sym;
411 bfd_vma *addendp;
413 arelent relent;
414 reloc_howto_type *howto;
416 relent.howto = NULL;
417 RTYPE2HOWTO (&relent, rel);
419 howto = relent.howto;
421 if (howto->pc_relative)
422 *addendp += sec->vma;
424 if (sym != NULL && sym->n_scnum == 0 && sym->n_value != 0)
426 /* This is a common symbol. The section contents include the
427 size (sym->n_value) as an addend. The relocate_section
428 function will be adding in the final value of the symbol. We
429 need to subtract out the current size in order to get the
430 correct result. */
431 BFD_ASSERT (h != NULL);
432 *addendp -= sym->n_value;
435 /* If the output symbol is common (in which case this must be a
436 relocatable link), we need to add in the final size of the
437 common symbol. */
438 if (h != NULL && h->root.type == bfd_link_hash_common)
439 *addendp += h->root.u.c.size;
441 return howto;
444 #define coff_rtype_to_howto m68kcoff_common_addend_rtype_to_howto
446 #endif /* ! defined (coff_rtype_to_howto) */
448 #endif /* COFF_COMMON_ADDEND */
450 #if !defined ONLY_DECLARE_RELOCS && ! defined STATIC_RELOCS
451 /* Given a .data section and a .emreloc in-memory section, store
452 relocation information into the .emreloc section which can be
453 used at runtime to relocate the section. This is called by the
454 linker when the --embedded-relocs switch is used. This is called
455 after the add_symbols entry point has been called for all the
456 objects, and before the final_link entry point is called. */
458 bfd_boolean
459 bfd_m68k_coff_create_embedded_relocs (abfd, info, datasec, relsec, errmsg)
460 bfd *abfd;
461 struct bfd_link_info *info;
462 asection *datasec;
463 asection *relsec;
464 char **errmsg;
466 char *extsyms;
467 bfd_size_type symesz;
468 struct internal_reloc *irel, *irelend;
469 bfd_byte *p;
470 bfd_size_type amt;
472 BFD_ASSERT (! info->relocatable);
474 *errmsg = NULL;
476 if (datasec->reloc_count == 0)
477 return TRUE;
479 extsyms = obj_coff_external_syms (abfd);
480 symesz = bfd_coff_symesz (abfd);
482 irel = _bfd_coff_read_internal_relocs (abfd, datasec, TRUE, NULL, FALSE,
483 NULL);
484 irelend = irel + datasec->reloc_count;
486 amt = (bfd_size_type) datasec->reloc_count * 12;
487 relsec->contents = (bfd_byte *) bfd_alloc (abfd, amt);
488 if (relsec->contents == NULL)
489 return FALSE;
491 p = relsec->contents;
493 for (; irel < irelend; irel++, p += 12)
495 asection *targetsec;
497 /* We are going to write a four byte longword into the runtime
498 reloc section. The longword will be the address in the data
499 section which must be relocated. It is followed by the name
500 of the target section NUL-padded or truncated to 8
501 characters. */
503 /* We can only relocate absolute longword relocs at run time. */
504 if (irel->r_type != R_RELLONG)
506 *errmsg = _("unsupported reloc type");
507 bfd_set_error (bfd_error_bad_value);
508 return FALSE;
511 if (irel->r_symndx == -1)
512 targetsec = bfd_abs_section_ptr;
513 else
515 struct coff_link_hash_entry *h;
517 h = obj_coff_sym_hashes (abfd)[irel->r_symndx];
518 if (h == NULL)
520 struct internal_syment isym;
522 bfd_coff_swap_sym_in (abfd, extsyms + symesz * irel->r_symndx,
523 &isym);
524 targetsec = coff_section_from_bfd_index (abfd, isym.n_scnum);
526 else if (h->root.type == bfd_link_hash_defined
527 || h->root.type == bfd_link_hash_defweak)
528 targetsec = h->root.u.def.section;
529 else
530 targetsec = NULL;
533 bfd_put_32 (abfd,
534 (irel->r_vaddr - datasec->vma + datasec->output_offset), p);
535 memset (p + 4, 0, 8);
536 if (targetsec != NULL)
537 strncpy ((char *) p + 4, targetsec->output_section->name, 8);
540 return TRUE;
542 #endif /* neither ONLY_DECLARE_RELOCS not STATIC_RELOCS */
544 #define coff_bfd_is_local_label_name m68k_coff_is_local_label_name
546 #define coff_relocate_section _bfd_coff_generic_relocate_section
548 #include "coffcode.h"
550 #ifndef TARGET_SYM
551 #define TARGET_SYM m68kcoff_vec
552 #endif
554 #ifndef TARGET_NAME
555 #define TARGET_NAME "coff-m68k"
556 #endif
558 #ifdef NAMES_HAVE_UNDERSCORE
559 CREATE_BIG_COFF_TARGET_VEC (TARGET_SYM, TARGET_NAME, D_PAGED, 0, '_', NULL, COFF_SWAP_TABLE)
560 #else
561 CREATE_BIG_COFF_TARGET_VEC (TARGET_SYM, TARGET_NAME, D_PAGED, 0, 0, NULL, COFF_SWAP_TABLE)
562 #endif