1 /* BFD back-end for Motorola 68000 COFF binaries.
2 Copyright (C) 1990-2014 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 3 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., 51 Franklin Street - Fifth Floor, Boston,
20 MA 02110-1301, USA. */
25 #include "coff/m68k.h"
26 #include "coff/internal.h"
29 /* This source file is compiled multiple times for various m68k COFF
30 variants. The following macros control its behaviour:
33 The C name of the BFD target vector. The default is m68k_coff_vec.
35 The user visible target name. The default is "coff-m68k".
37 Whether symbol names have an underscore.
39 Only declare the relocation howto array. Don't actually compile
40 it. The actual array will be picked up in another version of the
43 Make the relocation howto array, and associated functions, static.
45 If this is defined, then, for a relocation against a common
46 symbol, the object file holds the value (the size) of the common
47 symbol. If this is not defined, then, for a relocation against a
48 common symbol, the object file holds zero. */
50 #define COFF_DEFAULT_SECTION_ALIGNMENT_POWER (2)
52 #ifndef COFF_PAGE_SIZE
53 /* The page size is a guess based on ELF. */
54 #define COFF_PAGE_SIZE 0x2000
57 #ifndef COFF_COMMON_ADDEND
58 #define RELOC_SPECIAL_FN 0
60 static bfd_reloc_status_type m68kcoff_common_addend_special_fn
61 (bfd
*, arelent
*, asymbol
*, void *, asection
*, bfd
*, char **);
63 #define RELOC_SPECIAL_FN m68kcoff_common_addend_special_fn
66 static bfd_boolean
m68k_coff_is_local_label_name (bfd
*, const char *);
68 /* On the delta, a symbol starting with L% is local. We won't see
69 such a symbol on other platforms, so it should be safe to always
70 consider it local here. */
73 m68k_coff_is_local_label_name (bfd
*abfd
, const char *name
)
75 if (name
[0] == 'L' && name
[1] == '%')
78 return _bfd_coff_is_local_label_name (abfd
, name
);
82 /* Clean up namespace. */
83 #define m68kcoff_howto_table _bfd_m68kcoff_howto_table
84 #define m68k_rtype2howto _bfd_m68kcoff_rtype2howto
85 #define m68k_howto2rtype _bfd_m68kcoff_howto2rtype
86 #define m68k_reloc_type_lookup _bfd_m68kcoff_reloc_type_lookup
87 #define m68k_reloc_name_lookup _bfd_m68kcoff_reloc_name_lookup
90 #ifdef ONLY_DECLARE_RELOCS
91 extern reloc_howto_type m68kcoff_howto_table
[];
96 reloc_howto_type m68kcoff_howto_table
[] =
98 HOWTO (R_RELBYTE
, 0, 0, 8, FALSE
, 0, complain_overflow_bitfield
, RELOC_SPECIAL_FN
, "8", TRUE
, 0x000000ff,0x000000ff, FALSE
),
99 HOWTO (R_RELWORD
, 0, 1, 16, FALSE
, 0, complain_overflow_bitfield
, RELOC_SPECIAL_FN
, "16", TRUE
, 0x0000ffff,0x0000ffff, FALSE
),
100 HOWTO (R_RELLONG
, 0, 2, 32, FALSE
, 0, complain_overflow_bitfield
, RELOC_SPECIAL_FN
, "32", TRUE
, 0xffffffff,0xffffffff, FALSE
),
101 HOWTO (R_PCRBYTE
, 0, 0, 8, TRUE
, 0, complain_overflow_signed
, RELOC_SPECIAL_FN
, "DISP8", TRUE
, 0x000000ff,0x000000ff, FALSE
),
102 HOWTO (R_PCRWORD
, 0, 1, 16, TRUE
, 0, complain_overflow_signed
, RELOC_SPECIAL_FN
, "DISP16", TRUE
, 0x0000ffff,0x0000ffff, FALSE
),
103 HOWTO (R_PCRLONG
, 0, 2, 32, TRUE
, 0, complain_overflow_signed
, RELOC_SPECIAL_FN
, "DISP32", TRUE
, 0xffffffff,0xffffffff, FALSE
),
104 HOWTO (R_RELLONG_NEG
, 0, -2, 32, FALSE
, 0, complain_overflow_bitfield
, RELOC_SPECIAL_FN
, "-32", TRUE
, 0xffffffff,0xffffffff, FALSE
),
106 #endif /* not ONLY_DECLARE_RELOCS */
109 #define BADMAG(x) M68KBADMAG(x)
111 #define M68 1 /* Customize coffcode.h */
113 /* Turn a howto into a reloc number */
115 #ifdef ONLY_DECLARE_RELOCS
116 extern void m68k_rtype2howto (arelent
*internal
, int relocentry
);
117 extern int m68k_howto2rtype (reloc_howto_type
*);
118 extern reloc_howto_type
* m68k_reloc_type_lookup
119 (bfd
*, bfd_reloc_code_real_type
);
120 extern reloc_howto_type
* m68k_reloc_name_lookup (bfd
*, const char *);
124 #define STAT_REL static
129 STAT_REL
void m68k_rtype2howto (arelent
*, int);
130 STAT_REL
int m68k_howto2rtype (reloc_howto_type
*);
131 STAT_REL reloc_howto_type
* m68k_reloc_type_lookup (bfd
*, bfd_reloc_code_real_type
);
132 STAT_REL reloc_howto_type
* m68k_reloc_name_lookup (bfd
*, const char *);
135 m68k_rtype2howto (arelent
*internal
, int relocentry
)
139 case R_RELBYTE
: internal
->howto
= m68kcoff_howto_table
+ 0; break;
140 case R_RELWORD
: internal
->howto
= m68kcoff_howto_table
+ 1; break;
141 case R_RELLONG
: internal
->howto
= m68kcoff_howto_table
+ 2; break;
142 case R_PCRBYTE
: internal
->howto
= m68kcoff_howto_table
+ 3; break;
143 case R_PCRWORD
: internal
->howto
= m68kcoff_howto_table
+ 4; break;
144 case R_PCRLONG
: internal
->howto
= m68kcoff_howto_table
+ 5; break;
145 case R_RELLONG_NEG
: internal
->howto
= m68kcoff_howto_table
+ 6; break;
150 m68k_howto2rtype (reloc_howto_type
* internal
)
152 if (internal
->pc_relative
)
154 switch (internal
->bitsize
)
156 case 32: return R_PCRLONG
;
157 case 16: return R_PCRWORD
;
158 case 8: return R_PCRBYTE
;
163 switch (internal
->bitsize
)
165 case 32: return R_RELLONG
;
166 case 16: return R_RELWORD
;
167 case 8: return R_RELBYTE
;
173 STAT_REL reloc_howto_type
*
174 m68k_reloc_type_lookup (bfd
*abfd ATTRIBUTE_UNUSED
,
175 bfd_reloc_code_real_type code
)
179 default: return NULL
;
180 case BFD_RELOC_8
: return m68kcoff_howto_table
+ 0;
181 case BFD_RELOC_16
: return m68kcoff_howto_table
+ 1;
183 case BFD_RELOC_32
: return m68kcoff_howto_table
+ 2;
184 case BFD_RELOC_8_PCREL
: return m68kcoff_howto_table
+ 3;
185 case BFD_RELOC_16_PCREL
: return m68kcoff_howto_table
+ 4;
186 case BFD_RELOC_32_PCREL
: return m68kcoff_howto_table
+ 5;
187 /* FIXME: There doesn't seem to be a code for R_RELLONG_NEG. */
192 STAT_REL reloc_howto_type
*
193 m68k_reloc_name_lookup (bfd
*abfd ATTRIBUTE_UNUSED
,
199 i
< sizeof (m68kcoff_howto_table
) / sizeof (m68kcoff_howto_table
[0]);
201 if (m68kcoff_howto_table
[i
].name
!= NULL
202 && strcasecmp (m68kcoff_howto_table
[i
].name
, r_name
) == 0)
203 return &m68kcoff_howto_table
[i
];
208 #endif /* not ONLY_DECLARE_RELOCS */
210 #define RTYPE2HOWTO(internal, relocentry) \
211 m68k_rtype2howto(internal, (relocentry)->r_type)
213 #define SELECT_RELOC(external, internal) \
214 external.r_type = m68k_howto2rtype (internal)
216 #define coff_bfd_reloc_type_lookup m68k_reloc_type_lookup
217 #define coff_bfd_reloc_name_lookup m68k_reloc_name_lookup
219 #ifndef COFF_COMMON_ADDEND
220 #ifndef coff_rtype_to_howto
222 #define coff_rtype_to_howto m68kcoff_rtype_to_howto
224 static reloc_howto_type
*
225 m68kcoff_rtype_to_howto (bfd
*abfd ATTRIBUTE_UNUSED
,
227 struct internal_reloc
*rel
,
228 struct coff_link_hash_entry
*h ATTRIBUTE_UNUSED
,
229 struct internal_syment
*sym ATTRIBUTE_UNUSED
,
233 reloc_howto_type
*howto
;
236 RTYPE2HOWTO (&relent
, rel
);
238 howto
= relent
.howto
;
240 if (howto
!= NULL
&& howto
->pc_relative
)
241 *addendp
+= sec
->vma
;
246 #endif /* ! defined (coff_rtype_to_howto) */
247 #endif /* ! defined (COFF_COMMON_ADDEND) */
249 #ifdef COFF_COMMON_ADDEND
251 /* If COFF_COMMON_ADDEND is defined, then when using m68k COFF the
252 value stored in the .text section for a reference to a common
253 symbol is the value itself plus any desired offset. (taken from
254 work done by Ian Taylor, Cygnus Support, for I386 COFF). */
256 /* If we are producing relocatable output, we need to do some
257 adjustments to the object file that are not done by the
258 bfd_perform_relocation function. This function is called by every
259 reloc type to make any required adjustments. */
261 static bfd_reloc_status_type
262 m68kcoff_common_addend_special_fn (bfd
*abfd
,
263 arelent
*reloc_entry
,
266 asection
*input_section ATTRIBUTE_UNUSED
,
268 char **error_message ATTRIBUTE_UNUSED
)
272 if (output_bfd
== (bfd
*) NULL
)
273 return bfd_reloc_continue
;
275 if (bfd_is_com_section (symbol
->section
))
277 /* We are relocating a common symbol. The current value in the
278 object file is ORIG + OFFSET, where ORIG is the value of the
279 common symbol as seen by the object file when it was compiled
280 (this may be zero if the symbol was undefined) and OFFSET is
281 the offset into the common symbol (normally zero, but may be
282 non-zero when referring to a field in a common structure).
283 ORIG is the negative of reloc_entry->addend, which is set by
284 the CALC_ADDEND macro below. We want to replace the value in
285 the object file with NEW + OFFSET, where NEW is the value of
286 the common symbol which we are going to put in the final
287 object file. NEW is symbol->value. */
288 diff
= symbol
->value
+ reloc_entry
->addend
;
292 /* For some reason bfd_perform_relocation always effectively
293 ignores the addend for a COFF target when producing
294 relocatable output. This seems to be always wrong for 386
295 COFF, so we handle the addend here instead. */
296 diff
= reloc_entry
->addend
;
300 x = ((x & ~howto->dst_mask) | (((x & howto->src_mask) + diff) & howto->dst_mask))
304 reloc_howto_type
*howto
= reloc_entry
->howto
;
305 unsigned char *addr
= (unsigned char *) data
+ reloc_entry
->address
;
311 char x
= bfd_get_8 (abfd
, addr
);
313 bfd_put_8 (abfd
, x
, addr
);
319 short x
= bfd_get_16 (abfd
, addr
);
321 bfd_put_16 (abfd
, (bfd_vma
) x
, addr
);
327 long x
= bfd_get_32 (abfd
, addr
);
329 bfd_put_32 (abfd
, (bfd_vma
) x
, addr
);
338 /* Now let bfd_perform_relocation finish everything up. */
339 return bfd_reloc_continue
;
342 /* Compute the addend of a reloc. If the reloc is to a common symbol,
343 the object file contains the value of the common symbol. By the
344 time this is called, the linker may be using a different symbol
345 from a different object file with a different value. Therefore, we
346 hack wildly to locate the original symbol from this file so that we
347 can make the correct adjustment. This macro sets coffsym to the
348 symbol from the original file, and uses it to set the addend value
349 correctly. If this is not a common symbol, the usual addend
350 calculation is done, except that an additional tweak is needed for
352 FIXME: This macro refers to symbols and asect; these are from the
353 calling function, not the macro arguments. */
355 #define CALC_ADDEND(abfd, ptr, reloc, cache_ptr) \
357 coff_symbol_type *coffsym = (coff_symbol_type *) NULL; \
358 if (ptr && bfd_asymbol_bfd (ptr) != abfd) \
359 coffsym = (obj_symbols (abfd) \
360 + (cache_ptr->sym_ptr_ptr - symbols)); \
362 coffsym = coff_symbol_from (abfd, ptr); \
363 if (coffsym != (coff_symbol_type *) NULL \
364 && coffsym->native->u.syment.n_scnum == 0) \
365 cache_ptr->addend = - coffsym->native->u.syment.n_value; \
366 else if (ptr && bfd_asymbol_bfd (ptr) == abfd \
367 && ptr->section != (asection *) NULL) \
368 cache_ptr->addend = - (ptr->section->vma + ptr->value); \
370 cache_ptr->addend = 0; \
371 if (ptr && (reloc.r_type == R_PCRBYTE \
372 || reloc.r_type == R_PCRWORD \
373 || reloc.r_type == R_PCRLONG)) \
374 cache_ptr->addend += asect->vma; \
377 #ifndef coff_rtype_to_howto
379 /* coff-m68k.c uses the special COFF backend linker. We need to
380 adjust common symbols. */
382 static reloc_howto_type
*
383 m68kcoff_common_addend_rtype_to_howto (bfd
*abfd ATTRIBUTE_UNUSED
,
385 struct internal_reloc
*rel
,
386 struct coff_link_hash_entry
*h
,
387 struct internal_syment
*sym
,
391 reloc_howto_type
*howto
;
394 RTYPE2HOWTO (&relent
, rel
);
396 howto
= relent
.howto
;
398 if (howto
->pc_relative
)
399 *addendp
+= sec
->vma
;
401 if (sym
!= NULL
&& sym
->n_scnum
== 0 && sym
->n_value
!= 0)
403 /* This is a common symbol. The section contents include the
404 size (sym->n_value) as an addend. The relocate_section
405 function will be adding in the final value of the symbol. We
406 need to subtract out the current size in order to get the
408 BFD_ASSERT (h
!= NULL
);
409 *addendp
-= sym
->n_value
;
412 /* If the output symbol is common (in which case this must be a
413 relocatable link), we need to add in the final size of the
415 if (h
!= NULL
&& h
->root
.type
== bfd_link_hash_common
)
416 *addendp
+= h
->root
.u
.c
.size
;
421 #define coff_rtype_to_howto m68kcoff_common_addend_rtype_to_howto
423 #endif /* ! defined (coff_rtype_to_howto) */
425 #endif /* COFF_COMMON_ADDEND */
427 #if !defined ONLY_DECLARE_RELOCS && ! defined STATIC_RELOCS
428 /* Given a .data section and a .emreloc in-memory section, store
429 relocation information into the .emreloc section which can be
430 used at runtime to relocate the section. This is called by the
431 linker when the --embedded-relocs switch is used. This is called
432 after the add_symbols entry point has been called for all the
433 objects, and before the final_link entry point is called. */
436 bfd_m68k_coff_create_embedded_relocs (bfd
*abfd
,
437 struct bfd_link_info
*info
,
443 bfd_size_type symesz
;
444 struct internal_reloc
*irel
, *irelend
;
448 BFD_ASSERT (! info
->relocatable
);
452 if (datasec
->reloc_count
== 0)
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
,
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
)
467 p
= relsec
->contents
;
469 for (; irel
< irelend
; irel
++, p
+= 12)
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
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
);
487 if (irel
->r_symndx
== -1)
488 targetsec
= bfd_abs_section_ptr
;
491 struct coff_link_hash_entry
*h
;
493 h
= obj_coff_sym_hashes (abfd
)[irel
->r_symndx
];
496 struct internal_syment isym
;
498 bfd_coff_swap_sym_in (abfd
, extsyms
+ symesz
* irel
->r_symndx
,
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
;
510 (irel
->r_vaddr
- datasec
->vma
+ datasec
->output_offset
), p
);
511 memset (p
+ 4, 0, 8);
512 if (targetsec
!= NULL
)
513 strncpy ((char *) p
+ 4, targetsec
->output_section
->name
, 8);
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 #ifndef bfd_pe_print_pdata
525 #define bfd_pe_print_pdata NULL
528 #include "coffcode.h"
531 #define TARGET_SYM m68k_coff_vec
535 #define TARGET_NAME "coff-m68k"
538 #ifdef NAMES_HAVE_UNDERSCORE
539 CREATE_BIG_COFF_TARGET_VEC (TARGET_SYM
, TARGET_NAME
, D_PAGED
, 0, '_', NULL
, COFF_SWAP_TABLE
)
541 CREATE_BIG_COFF_TARGET_VEC (TARGET_SYM
, TARGET_NAME
, D_PAGED
, 0, 0, NULL
, COFF_SWAP_TABLE
)