* aix386-core.c: Remove use of PTR and PARAMS macros.
[deliverable/binutils-gdb.git] / bfd / coff-m68k.c
CommitLineData
252b5132 1/* BFD back-end for Motorola 68000 COFF binaries.
67a374a5 2 Copyright 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1999,
2c3fc389 3 2000, 2001, 2002, 2003, 2005, 2007, 2008, 2012
5f771d47 4 Free Software Foundation, Inc.
252b5132
RH
5 Written by Cygnus Support.
6
cd123cb7 7 This file is part of BFD, the Binary File Descriptor library.
252b5132 8
cd123cb7
NC
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.
252b5132 13
cd123cb7
NC
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.
252b5132 18
cd123cb7
NC
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. */
252b5132 23
252b5132 24#include "sysdep.h"
3db64b00 25#include "bfd.h"
252b5132
RH
26#include "libbfd.h"
27#include "coff/m68k.h"
28#include "coff/internal.h"
29#include "libcoff.h"
30
31/* This source file is compiled multiple times for various m68k COFF
32 variants. The following macros control its behaviour:
33
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. */
51
52#define COFF_DEFAULT_SECTION_ALIGNMENT_POWER (2)
53
54#ifndef COFF_PAGE_SIZE
55/* The page size is a guess based on ELF. */
56#define COFF_PAGE_SIZE 0x2000
57#endif
58
59#ifndef COFF_COMMON_ADDEND
60#define RELOC_SPECIAL_FN 0
61#else
62static bfd_reloc_status_type m68kcoff_common_addend_special_fn
2c3fc389
NC
63 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
64
252b5132
RH
65#define RELOC_SPECIAL_FN m68kcoff_common_addend_special_fn
66#endif
67
2c3fc389 68static bfd_boolean m68k_coff_is_local_label_name (bfd *, const char *);
252b5132
RH
69
70/* On the delta, a symbol starting with L% is local. We won't see
71 such a symbol on other platforms, so it should be safe to always
72 consider it local here. */
73
b34976b6 74static bfd_boolean
2c3fc389 75m68k_coff_is_local_label_name (bfd *abfd, const char *name)
252b5132
RH
76{
77 if (name[0] == 'L' && name[1] == '%')
b34976b6 78 return TRUE;
252b5132
RH
79
80 return _bfd_coff_is_local_label_name (abfd, name);
81}
82
83#ifndef STATIC_RELOCS
84/* Clean up namespace. */
85#define m68kcoff_howto_table _bfd_m68kcoff_howto_table
86#define m68k_rtype2howto _bfd_m68kcoff_rtype2howto
87#define m68k_howto2rtype _bfd_m68kcoff_howto2rtype
88#define m68k_reloc_type_lookup _bfd_m68kcoff_reloc_type_lookup
157090f7 89#define m68k_reloc_name_lookup _bfd_m68kcoff_reloc_name_lookup
252b5132
RH
90#endif
91
92#ifdef ONLY_DECLARE_RELOCS
93extern reloc_howto_type m68kcoff_howto_table[];
94#else
95#ifdef STATIC_RELOCS
96static
97#endif
b48499ec 98reloc_howto_type m68kcoff_howto_table[] =
67a374a5 99 {
b34976b6
AM
100 HOWTO (R_RELBYTE, 0, 0, 8, FALSE, 0, complain_overflow_bitfield, RELOC_SPECIAL_FN, "8", TRUE, 0x000000ff,0x000000ff, FALSE),
101 HOWTO (R_RELWORD, 0, 1, 16, FALSE, 0, complain_overflow_bitfield, RELOC_SPECIAL_FN, "16", TRUE, 0x0000ffff,0x0000ffff, FALSE),
102 HOWTO (R_RELLONG, 0, 2, 32, FALSE, 0, complain_overflow_bitfield, RELOC_SPECIAL_FN, "32", TRUE, 0xffffffff,0xffffffff, FALSE),
103 HOWTO (R_PCRBYTE, 0, 0, 8, TRUE, 0, complain_overflow_signed, RELOC_SPECIAL_FN, "DISP8", TRUE, 0x000000ff,0x000000ff, FALSE),
104 HOWTO (R_PCRWORD, 0, 1, 16, TRUE, 0, complain_overflow_signed, RELOC_SPECIAL_FN, "DISP16", TRUE, 0x0000ffff,0x0000ffff, FALSE),
105 HOWTO (R_PCRLONG, 0, 2, 32, TRUE, 0, complain_overflow_signed, RELOC_SPECIAL_FN, "DISP32", TRUE, 0xffffffff,0xffffffff, FALSE),
106 HOWTO (R_RELLONG_NEG, 0, -2, 32, FALSE, 0, complain_overflow_bitfield, RELOC_SPECIAL_FN, "-32", TRUE, 0xffffffff,0xffffffff, FALSE),
67a374a5 107 };
252b5132
RH
108#endif /* not ONLY_DECLARE_RELOCS */
109
110#ifndef BADMAG
111#define BADMAG(x) M68KBADMAG(x)
112#endif
113#define M68 1 /* Customize coffcode.h */
114
115/* Turn a howto into a reloc number */
116
117#ifdef ONLY_DECLARE_RELOCS
2c3fc389
NC
118extern void m68k_rtype2howto (arelent *internal, int relocentry);
119extern int m68k_howto2rtype (reloc_howto_type *);
120extern reloc_howto_type * m68k_reloc_type_lookup
121 (bfd *, bfd_reloc_code_real_type);
122extern reloc_howto_type * m68k_reloc_name_lookup (bfd *, const char *);
252b5132 123#else
f4ffd778 124
252b5132 125#ifdef STATIC_RELOCS
f4ffd778
NC
126#define STAT_REL static
127#else
128#define STAT_REL
252b5132 129#endif
f4ffd778 130
2c3fc389
NC
131STAT_REL void m68k_rtype2howto (arelent *, int);
132STAT_REL int m68k_howto2rtype (reloc_howto_type *);
133STAT_REL reloc_howto_type * m68k_reloc_type_lookup (bfd *, bfd_reloc_code_real_type);
134STAT_REL reloc_howto_type * m68k_reloc_name_lookup (bfd *, const char *);
f4ffd778
NC
135
136STAT_REL void
2c3fc389 137m68k_rtype2howto (arelent *internal, int relocentry)
252b5132 138{
b48499ec 139 switch (relocentry)
67a374a5
NC
140 {
141 case R_RELBYTE: internal->howto = m68kcoff_howto_table + 0; break;
142 case R_RELWORD: internal->howto = m68kcoff_howto_table + 1; break;
143 case R_RELLONG: internal->howto = m68kcoff_howto_table + 2; break;
144 case R_PCRBYTE: internal->howto = m68kcoff_howto_table + 3; break;
145 case R_PCRWORD: internal->howto = m68kcoff_howto_table + 4; break;
146 case R_PCRLONG: internal->howto = m68kcoff_howto_table + 5; break;
147 case R_RELLONG_NEG: internal->howto = m68kcoff_howto_table + 6; break;
148 }
252b5132
RH
149}
150
f4ffd778 151STAT_REL int
2c3fc389 152m68k_howto2rtype (reloc_howto_type * internal)
252b5132 153{
b48499ec 154 if (internal->pc_relative)
252b5132 155 {
67a374a5
NC
156 switch (internal->bitsize)
157 {
158 case 32: return R_PCRLONG;
159 case 16: return R_PCRWORD;
160 case 8: return R_PCRBYTE;
161 }
252b5132 162 }
b48499ec 163 else
67a374a5
NC
164 {
165 switch (internal->bitsize)
166 {
167 case 32: return R_RELLONG;
168 case 16: return R_RELWORD;
169 case 8: return R_RELBYTE;
170 }
171 }
b48499ec 172 return R_RELLONG;
252b5132
RH
173}
174
f4ffd778 175STAT_REL reloc_howto_type *
2c3fc389
NC
176m68k_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
177 bfd_reloc_code_real_type code)
252b5132
RH
178{
179 switch (code)
180 {
181 default: return NULL;
182 case BFD_RELOC_8: return m68kcoff_howto_table + 0;
183 case BFD_RELOC_16: return m68kcoff_howto_table + 1;
184 case BFD_RELOC_CTOR:
185 case BFD_RELOC_32: return m68kcoff_howto_table + 2;
186 case BFD_RELOC_8_PCREL: return m68kcoff_howto_table + 3;
187 case BFD_RELOC_16_PCREL: return m68kcoff_howto_table + 4;
188 case BFD_RELOC_32_PCREL: return m68kcoff_howto_table + 5;
189 /* FIXME: There doesn't seem to be a code for R_RELLONG_NEG. */
190 }
191 /*NOTREACHED*/
192}
193
157090f7
AM
194STAT_REL reloc_howto_type *
195m68k_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
196 const char *r_name)
197{
198 unsigned int i;
199
200 for (i = 0;
201 i < sizeof (m68kcoff_howto_table) / sizeof (m68kcoff_howto_table[0]);
202 i++)
203 if (m68kcoff_howto_table[i].name != NULL
204 && strcasecmp (m68kcoff_howto_table[i].name, r_name) == 0)
205 return &m68kcoff_howto_table[i];
206
207 return NULL;
208}
209
252b5132
RH
210#endif /* not ONLY_DECLARE_RELOCS */
211
212#define RTYPE2HOWTO(internal, relocentry) \
213 m68k_rtype2howto(internal, (relocentry)->r_type)
214
215#define SELECT_RELOC(external, internal) \
216 external.r_type = m68k_howto2rtype (internal)
217
218#define coff_bfd_reloc_type_lookup m68k_reloc_type_lookup
157090f7 219#define coff_bfd_reloc_name_lookup m68k_reloc_name_lookup
252b5132
RH
220
221#ifndef COFF_COMMON_ADDEND
222#ifndef coff_rtype_to_howto
223
224#define coff_rtype_to_howto m68kcoff_rtype_to_howto
225
252b5132 226static reloc_howto_type *
2c3fc389
NC
227m68kcoff_rtype_to_howto (bfd *abfd ATTRIBUTE_UNUSED,
228 asection *sec,
229 struct internal_reloc *rel,
230 struct coff_link_hash_entry *h ATTRIBUTE_UNUSED,
231 struct internal_syment *sym ATTRIBUTE_UNUSED,
232 bfd_vma *addendp)
252b5132
RH
233{
234 arelent relent;
235 reloc_howto_type *howto;
236
40b1c6c5 237 relent.howto = NULL;
252b5132
RH
238 RTYPE2HOWTO (&relent, rel);
239
240 howto = relent.howto;
241
40b1c6c5 242 if (howto != NULL && howto->pc_relative)
252b5132
RH
243 *addendp += sec->vma;
244
245 return howto;
246}
247
248#endif /* ! defined (coff_rtype_to_howto) */
249#endif /* ! defined (COFF_COMMON_ADDEND) */
250\f
251#ifdef COFF_COMMON_ADDEND
252
253/* If COFF_COMMON_ADDEND is defined, then when using m68k COFF the
254 value stored in the .text section for a reference to a common
255 symbol is the value itself plus any desired offset. (taken from
256 work done by Ian Taylor, Cygnus Support, for I386 COFF). */
257
1049f94e 258/* If we are producing relocatable output, we need to do some
252b5132
RH
259 adjustments to the object file that are not done by the
260 bfd_perform_relocation function. This function is called by every
261 reloc type to make any required adjustments. */
262
263static bfd_reloc_status_type
2c3fc389
NC
264m68kcoff_common_addend_special_fn (bfd *abfd,
265 arelent *reloc_entry,
266 asymbol *symbol,
267 void * data,
268 asection *input_section ATTRIBUTE_UNUSED,
269 bfd *output_bfd,
270 char **error_message ATTRIBUTE_UNUSED)
252b5132
RH
271{
272 symvalue diff;
273
274 if (output_bfd == (bfd *) NULL)
275 return bfd_reloc_continue;
276
277 if (bfd_is_com_section (symbol->section))
278 {
279 /* We are relocating a common symbol. The current value in the
280 object file is ORIG + OFFSET, where ORIG is the value of the
281 common symbol as seen by the object file when it was compiled
282 (this may be zero if the symbol was undefined) and OFFSET is
283 the offset into the common symbol (normally zero, but may be
284 non-zero when referring to a field in a common structure).
285 ORIG is the negative of reloc_entry->addend, which is set by
286 the CALC_ADDEND macro below. We want to replace the value in
287 the object file with NEW + OFFSET, where NEW is the value of
288 the common symbol which we are going to put in the final
289 object file. NEW is symbol->value. */
290 diff = symbol->value + reloc_entry->addend;
291 }
292 else
293 {
294 /* For some reason bfd_perform_relocation always effectively
295 ignores the addend for a COFF target when producing
1049f94e 296 relocatable output. This seems to be always wrong for 386
252b5132
RH
297 COFF, so we handle the addend here instead. */
298 diff = reloc_entry->addend;
299 }
300
301#define DOIT(x) \
302 x = ((x & ~howto->dst_mask) | (((x & howto->src_mask) + diff) & howto->dst_mask))
303
304 if (diff != 0)
305 {
306 reloc_howto_type *howto = reloc_entry->howto;
307 unsigned char *addr = (unsigned char *) data + reloc_entry->address;
308
309 switch (howto->size)
310 {
311 case 0:
312 {
313 char x = bfd_get_8 (abfd, addr);
314 DOIT (x);
315 bfd_put_8 (abfd, x, addr);
316 }
317 break;
318
319 case 1:
320 {
321 short x = bfd_get_16 (abfd, addr);
322 DOIT (x);
dc810e39 323 bfd_put_16 (abfd, (bfd_vma) x, addr);
252b5132
RH
324 }
325 break;
326
327 case 2:
328 {
329 long x = bfd_get_32 (abfd, addr);
330 DOIT (x);
dc810e39 331 bfd_put_32 (abfd, (bfd_vma) x, addr);
252b5132
RH
332 }
333 break;
334
335 default:
336 abort ();
337 }
338 }
339
340 /* Now let bfd_perform_relocation finish everything up. */
341 return bfd_reloc_continue;
342}
343
344/* Compute the addend of a reloc. If the reloc is to a common symbol,
345 the object file contains the value of the common symbol. By the
346 time this is called, the linker may be using a different symbol
347 from a different object file with a different value. Therefore, we
348 hack wildly to locate the original symbol from this file so that we
349 can make the correct adjustment. This macro sets coffsym to the
350 symbol from the original file, and uses it to set the addend value
351 correctly. If this is not a common symbol, the usual addend
352 calculation is done, except that an additional tweak is needed for
353 PC relative relocs.
354 FIXME: This macro refers to symbols and asect; these are from the
355 calling function, not the macro arguments. */
356
357#define CALC_ADDEND(abfd, ptr, reloc, cache_ptr) \
358 { \
359 coff_symbol_type *coffsym = (coff_symbol_type *) NULL; \
360 if (ptr && bfd_asymbol_bfd (ptr) != abfd) \
361 coffsym = (obj_symbols (abfd) \
362 + (cache_ptr->sym_ptr_ptr - symbols)); \
363 else if (ptr) \
364 coffsym = coff_symbol_from (abfd, ptr); \
365 if (coffsym != (coff_symbol_type *) NULL \
366 && coffsym->native->u.syment.n_scnum == 0) \
367 cache_ptr->addend = - coffsym->native->u.syment.n_value; \
368 else if (ptr && bfd_asymbol_bfd (ptr) == abfd \
369 && ptr->section != (asection *) NULL) \
370 cache_ptr->addend = - (ptr->section->vma + ptr->value); \
371 else \
372 cache_ptr->addend = 0; \
373 if (ptr && (reloc.r_type == R_PCRBYTE \
374 || reloc.r_type == R_PCRWORD \
375 || reloc.r_type == R_PCRLONG)) \
376 cache_ptr->addend += asect->vma; \
377 }
378
379#ifndef coff_rtype_to_howto
380
381/* coff-m68k.c uses the special COFF backend linker. We need to
382 adjust common symbols. */
383
252b5132 384static reloc_howto_type *
2c3fc389
NC
385m68kcoff_common_addend_rtype_to_howto (bfd *abfd ATTRIBUTE_UNUSED,
386 asection *sec,
387 struct internal_reloc *rel,
388 struct coff_link_hash_entry *h,
389 struct internal_syment *sym,
390 bfd_vma *addendp)
252b5132
RH
391{
392 arelent relent;
393 reloc_howto_type *howto;
394
1b0b5b1b 395 relent.howto = NULL;
252b5132
RH
396 RTYPE2HOWTO (&relent, rel);
397
398 howto = relent.howto;
399
400 if (howto->pc_relative)
401 *addendp += sec->vma;
402
403 if (sym != NULL && sym->n_scnum == 0 && sym->n_value != 0)
404 {
405 /* This is a common symbol. The section contents include the
406 size (sym->n_value) as an addend. The relocate_section
407 function will be adding in the final value of the symbol. We
408 need to subtract out the current size in order to get the
409 correct result. */
410 BFD_ASSERT (h != NULL);
411 *addendp -= sym->n_value;
412 }
413
414 /* If the output symbol is common (in which case this must be a
1049f94e 415 relocatable link), we need to add in the final size of the
252b5132
RH
416 common symbol. */
417 if (h != NULL && h->root.type == bfd_link_hash_common)
418 *addendp += h->root.u.c.size;
419
420 return howto;
421}
422
423#define coff_rtype_to_howto m68kcoff_common_addend_rtype_to_howto
424
425#endif /* ! defined (coff_rtype_to_howto) */
426
427#endif /* COFF_COMMON_ADDEND */
20bccb34
NC
428
429#if !defined ONLY_DECLARE_RELOCS && ! defined STATIC_RELOCS
430/* Given a .data section and a .emreloc in-memory section, store
431 relocation information into the .emreloc section which can be
432 used at runtime to relocate the section. This is called by the
433 linker when the --embedded-relocs switch is used. This is called
434 after the add_symbols entry point has been called for all the
435 objects, and before the final_link entry point is called. */
436
b34976b6 437bfd_boolean
2c3fc389
NC
438bfd_m68k_coff_create_embedded_relocs (bfd *abfd,
439 struct bfd_link_info *info,
440 asection *datasec,
441 asection *relsec,
442 char **errmsg)
20bccb34
NC
443{
444 char *extsyms;
445 bfd_size_type symesz;
446 struct internal_reloc *irel, *irelend;
447 bfd_byte *p;
dc810e39 448 bfd_size_type amt;
20bccb34 449
1049f94e 450 BFD_ASSERT (! info->relocatable);
20bccb34
NC
451
452 *errmsg = NULL;
453
454 if (datasec->reloc_count == 0)
b34976b6 455 return TRUE;
20bccb34
NC
456
457 extsyms = obj_coff_external_syms (abfd);
458 symesz = bfd_coff_symesz (abfd);
459
b34976b6 460 irel = _bfd_coff_read_internal_relocs (abfd, datasec, TRUE, NULL, FALSE,
20bccb34
NC
461 NULL);
462 irelend = irel + datasec->reloc_count;
463
dc810e39
AM
464 amt = (bfd_size_type) datasec->reloc_count * 12;
465 relsec->contents = (bfd_byte *) bfd_alloc (abfd, amt);
20bccb34 466 if (relsec->contents == NULL)
b34976b6 467 return FALSE;
20bccb34
NC
468
469 p = relsec->contents;
470
471 for (; irel < irelend; irel++, p += 12)
472 {
473 asection *targetsec;
474
475 /* We are going to write a four byte longword into the runtime
476 reloc section. The longword will be the address in the data
477 section which must be relocated. It is followed by the name
478 of the target section NUL-padded or truncated to 8
479 characters. */
480
481 /* We can only relocate absolute longword relocs at run time. */
482 if (irel->r_type != R_RELLONG)
483 {
484 *errmsg = _("unsupported reloc type");
485 bfd_set_error (bfd_error_bad_value);
b34976b6 486 return FALSE;
20bccb34
NC
487 }
488
489 if (irel->r_symndx == -1)
490 targetsec = bfd_abs_section_ptr;
491 else
492 {
493 struct coff_link_hash_entry *h;
b48499ec 494
20bccb34
NC
495 h = obj_coff_sym_hashes (abfd)[irel->r_symndx];
496 if (h == NULL)
497 {
498 struct internal_syment isym;
b48499ec 499
20bccb34
NC
500 bfd_coff_swap_sym_in (abfd, extsyms + symesz * irel->r_symndx,
501 &isym);
502 targetsec = coff_section_from_bfd_index (abfd, isym.n_scnum);
503 }
504 else if (h->root.type == bfd_link_hash_defined
505 || h->root.type == bfd_link_hash_defweak)
506 targetsec = h->root.u.def.section;
507 else
508 targetsec = NULL;
509 }
b48499ec 510
20bccb34
NC
511 bfd_put_32 (abfd,
512 (irel->r_vaddr - datasec->vma + datasec->output_offset), p);
513 memset (p + 4, 0, 8);
ff0829fd 514 if (targetsec != NULL)
f075ee0c 515 strncpy ((char *) p + 4, targetsec->output_section->name, 8);
20bccb34 516 }
b48499ec 517
b34976b6 518 return TRUE;
20bccb34
NC
519}
520#endif /* neither ONLY_DECLARE_RELOCS not STATIC_RELOCS */
252b5132
RH
521\f
522#define coff_bfd_is_local_label_name m68k_coff_is_local_label_name
523
524#define coff_relocate_section _bfd_coff_generic_relocate_section
525
2b5c217d
NC
526#ifndef bfd_pe_print_pdata
527#define bfd_pe_print_pdata NULL
528#endif
529
252b5132
RH
530#include "coffcode.h"
531
532#ifndef TARGET_SYM
533#define TARGET_SYM m68kcoff_vec
534#endif
535
536#ifndef TARGET_NAME
537#define TARGET_NAME "coff-m68k"
538#endif
539
252b5132 540#ifdef NAMES_HAVE_UNDERSCORE
3fa78519 541CREATE_BIG_COFF_TARGET_VEC (TARGET_SYM, TARGET_NAME, D_PAGED, 0, '_', NULL, COFF_SWAP_TABLE)
252b5132 542#else
3fa78519 543CREATE_BIG_COFF_TARGET_VEC (TARGET_SYM, TARGET_NAME, D_PAGED, 0, 0, NULL, COFF_SWAP_TABLE)
252b5132 544#endif
This page took 0.570807 seconds and 4 git commands to generate.