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