source: branches/libc-0.6/src/binutils/bfd/coff-m68k.c

Last change on this file was 610, checked in by bird, 22 years ago

This commit was generated by cvs2svn to compensate for changes in r609,
which included commits to RCS files with non-trunk default branches.

  • Property cvs2svn:cvs-rev set to 1.1.1.2
  • Property svn:eol-style set to native
  • Property svn:executable set to *
File size: 16.7 KB
Line 
1/* BFD back-end for Motorola 68000 COFF binaries.
2 Copyright 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1999,
3 2000, 2001, 2002
4 Free Software Foundation, Inc.
5 Written by Cygnus Support.
6
7This file is part of BFD, the Binary File Descriptor library.
8
9This program is free software; you can redistribute it and/or modify
10it under the terms of the GNU General Public License as published by
11the Free Software Foundation; either version 2 of the License, or
12(at your option) any later version.
13
14This program is distributed in the hope that it will be useful,
15but WITHOUT ANY WARRANTY; without even the implied warranty of
16MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17GNU General Public License for more details.
18
19You should have received a copy of the GNU General Public License
20along with this program; if not, write to the Free Software
21Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
22
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"
29
30/* This source file is compiled multiple times for various m68k COFF
31 variants. The following macros control its behaviour:
32
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. */
50
51#define COFF_DEFAULT_SECTION_ALIGNMENT_POWER (2)
52
53#ifndef COFF_PAGE_SIZE
54/* The page size is a guess based on ELF. */
55#define COFF_PAGE_SIZE 0x2000
56#endif
57
58#ifndef COFF_COMMON_ADDEND
59#define RELOC_SPECIAL_FN 0
60#else
61static bfd_reloc_status_type m68kcoff_common_addend_special_fn
62 PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
63static 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
69
70static bfd_boolean m68k_coff_is_local_label_name
71 PARAMS ((bfd *, const char *));
72
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. */
76
77static bfd_boolean
78m68k_coff_is_local_label_name (abfd, name)
79 bfd *abfd;
80 const char *name;
81{
82 if (name[0] == 'L' && name[1] == '%')
83 return TRUE;
84
85 return _bfd_coff_is_local_label_name (abfd, name);
86}
87
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
95
96#ifdef ONLY_DECLARE_RELOCS
97extern reloc_howto_type m68kcoff_howto_table[];
98#else
99#ifdef STATIC_RELOCS
100static
101#endif
102reloc_howto_type m68kcoff_howto_table[] =
103 {
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),
111 };
112#endif /* not ONLY_DECLARE_RELOCS */
113
114#ifndef BADMAG
115#define BADMAG(x) M68KBADMAG(x)
116#endif
117#define M68 1 /* Customize coffcode.h */
118
119/* Turn a howto into a reloc number */
120
121#ifdef ONLY_DECLARE_RELOCS
122extern void m68k_rtype2howto PARAMS ((arelent *internal, int relocentry));
123extern int m68k_howto2rtype PARAMS ((reloc_howto_type *));
124extern reloc_howto_type *m68k_reloc_type_lookup
125 PARAMS ((bfd *, bfd_reloc_code_real_type));
126#else
127
128#ifdef STATIC_RELOCS
129#define STAT_REL static
130#else
131#define STAT_REL
132#endif
133
134STAT_REL reloc_howto_type * m68k_reloc_type_lookup PARAMS ((bfd *, bfd_reloc_code_real_type));
135STAT_REL int m68k_howto2rtype PARAMS ((reloc_howto_type *));
136STAT_REL void m68k_rtype2howto PARAMS ((arelent *, int));
137
138
139STAT_REL void
140m68k_rtype2howto(internal, relocentry)
141 arelent *internal;
142 int relocentry;
143{
144 switch (relocentry)
145 {
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;
153 }
154}
155
156STAT_REL int
157m68k_howto2rtype (internal)
158 reloc_howto_type *internal;
159{
160 if (internal->pc_relative)
161 {
162 switch (internal->bitsize)
163 {
164 case 32: return R_PCRLONG;
165 case 16: return R_PCRWORD;
166 case 8: return R_PCRBYTE;
167 }
168 }
169 else
170 {
171 switch (internal->bitsize)
172 {
173 case 32: return R_RELLONG;
174 case 16: return R_RELWORD;
175 case 8: return R_RELBYTE;
176 }
177 }
178 return R_RELLONG;
179}
180
181STAT_REL reloc_howto_type *
182m68k_reloc_type_lookup (abfd, code)
183 bfd *abfd ATTRIBUTE_UNUSED;
184 bfd_reloc_code_real_type code;
185{
186 switch (code)
187 {
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. */
197 }
198 /*NOTREACHED*/
199}
200
201#endif /* not ONLY_DECLARE_RELOCS */
202
203#define RTYPE2HOWTO(internal, relocentry) \
204 m68k_rtype2howto(internal, (relocentry)->r_type)
205
206#define SELECT_RELOC(external, internal) \
207 external.r_type = m68k_howto2rtype (internal)
208
209#define coff_bfd_reloc_type_lookup m68k_reloc_type_lookup
210
211#ifndef COFF_COMMON_ADDEND
212#ifndef coff_rtype_to_howto
213
214#define coff_rtype_to_howto m68kcoff_rtype_to_howto
215
216static 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 *));
220
221static reloc_howto_type *
222m68kcoff_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;
229{
230 arelent relent;
231 reloc_howto_type *howto;
232
233 RTYPE2HOWTO (&relent, rel);
234
235 howto = relent.howto;
236
237 if (howto->pc_relative)
238 *addendp += sec->vma;
239
240 return howto;
241}
242
243#endif /* ! defined (coff_rtype_to_howto) */
244#endif /* ! defined (COFF_COMMON_ADDEND) */
245
246
247#ifdef COFF_COMMON_ADDEND
248
249/* If COFF_COMMON_ADDEND is defined, then when using m68k COFF the
250 value stored in the .text section for a reference to a common
251 symbol is the value itself plus any desired offset. (taken from
252 work done by Ian Taylor, Cygnus Support, for I386 COFF). */
253
254/* If we are producing relocateable output, we need to do some
255 adjustments to the object file that are not done by the
256 bfd_perform_relocation function. This function is called by every
257 reloc type to make any required adjustments. */
258
259static bfd_reloc_status_type
260m68kcoff_common_addend_special_fn (abfd, reloc_entry, symbol, data,
261 input_section, output_bfd, error_message)
262 bfd *abfd;
263 arelent *reloc_entry;
264 asymbol *symbol;
265 PTR data;
266 asection *input_section ATTRIBUTE_UNUSED;
267 bfd *output_bfd;
268 char **error_message ATTRIBUTE_UNUSED;
269{
270 symvalue diff;
271
272 if (output_bfd == (bfd *) NULL)
273 return bfd_reloc_continue;
274
275 if (bfd_is_com_section (symbol->section))
276 {
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;
289 }
290 else
291 {
292 /* For some reason bfd_perform_relocation always effectively
293 ignores the addend for a COFF target when producing
294 relocateable output. This seems to be always wrong for 386
295 COFF, so we handle the addend here instead. */
296 diff = reloc_entry->addend;
297 }
298
299#define DOIT(x) \
300 x = ((x & ~howto->dst_mask) | (((x & howto->src_mask) + diff) & howto->dst_mask))
301
302 if (diff != 0)
303 {
304 reloc_howto_type *howto = reloc_entry->howto;
305 unsigned char *addr = (unsigned char *) data + reloc_entry->address;
306
307 switch (howto->size)
308 {
309 case 0:
310 {
311 char x = bfd_get_8 (abfd, addr);
312 DOIT (x);
313 bfd_put_8 (abfd, x, addr);
314 }
315 break;
316
317 case 1:
318 {
319 short x = bfd_get_16 (abfd, addr);
320 DOIT (x);
321 bfd_put_16 (abfd, (bfd_vma) x, addr);
322 }
323 break;
324
325 case 2:
326 {
327 long x = bfd_get_32 (abfd, addr);
328 DOIT (x);
329 bfd_put_32 (abfd, (bfd_vma) x, addr);
330 }
331 break;
332
333 default:
334 abort ();
335 }
336 }
337
338 /* Now let bfd_perform_relocation finish everything up. */
339 return bfd_reloc_continue;
340}
341
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
351 PC relative relocs.
352 FIXME: This macro refers to symbols and asect; these are from the
353 calling function, not the macro arguments. */
354
355#define CALC_ADDEND(abfd, ptr, reloc, cache_ptr) \
356 { \
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)); \
361 else if (ptr) \
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); \
369 else \
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; \
375 }
376
377#ifndef coff_rtype_to_howto
378
379/* coff-m68k.c uses the special COFF backend linker. We need to
380 adjust common symbols. */
381
382static reloc_howto_type *
383m68kcoff_common_addend_rtype_to_howto (abfd, sec, rel, h, sym, addendp)
384 bfd *abfd ATTRIBUTE_UNUSED;
385 asection *sec;
386 struct internal_reloc *rel;
387 struct coff_link_hash_entry *h;
388 struct internal_syment *sym;
389 bfd_vma *addendp;
390{
391 arelent relent;
392 reloc_howto_type *howto;
393
394 RTYPE2HOWTO (&relent, rel);
395
396 howto = relent.howto;
397
398 if (howto->pc_relative)
399 *addendp += sec->vma;
400
401 if (sym != NULL && sym->n_scnum == 0 && sym->n_value != 0)
402 {
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
407 correct result. */
408 BFD_ASSERT (h != NULL);
409 *addendp -= sym->n_value;
410 }
411
412 /* If the output symbol is common (in which case this must be a
413 relocateable link), we need to add in the final size of the
414 common symbol. */
415 if (h != NULL && h->root.type == bfd_link_hash_common)
416 *addendp += h->root.u.c.size;
417
418 return howto;
419}
420
421#define coff_rtype_to_howto m68kcoff_common_addend_rtype_to_howto
422
423#endif /* ! defined (coff_rtype_to_howto) */
424
425#endif /* COFF_COMMON_ADDEND */
426
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. */
434
435bfd_boolean
436bfd_m68k_coff_create_embedded_relocs (abfd, info, datasec, relsec, errmsg)
437 bfd *abfd;
438 struct bfd_link_info *info;
439 asection *datasec;
440 asection *relsec;
441 char **errmsg;
442{
443 char *extsyms;
444 bfd_size_type symesz;
445 struct internal_reloc *irel, *irelend;
446 bfd_byte *p;
447 bfd_size_type amt;
448
449 BFD_ASSERT (! info->relocateable);
450
451 *errmsg = NULL;
452
453 if (datasec->reloc_count == 0)
454 return TRUE;
455
456 extsyms = obj_coff_external_syms (abfd);
457 symesz = bfd_coff_symesz (abfd);
458
459 irel = _bfd_coff_read_internal_relocs (abfd, datasec, TRUE, NULL, FALSE,
460 NULL);
461 irelend = irel + datasec->reloc_count;
462
463 amt = (bfd_size_type) datasec->reloc_count * 12;
464 relsec->contents = (bfd_byte *) bfd_alloc (abfd, amt);
465 if (relsec->contents == NULL)
466 return FALSE;
467
468 p = relsec->contents;
469
470 for (; irel < irelend; irel++, p += 12)
471 {
472 asection *targetsec;
473
474 /* We are going to write a four byte longword into the runtime
475 reloc section. The longword will be the address in the data
476 section which must be relocated. It is followed by the name
477 of the target section NUL-padded or truncated to 8
478 characters. */
479
480 /* We can only relocate absolute longword relocs at run time. */
481 if (irel->r_type != R_RELLONG)
482 {
483 *errmsg = _("unsupported reloc type");
484 bfd_set_error (bfd_error_bad_value);
485 return FALSE;
486 }
487
488 if (irel->r_symndx == -1)
489 targetsec = bfd_abs_section_ptr;
490 else
491 {
492 struct coff_link_hash_entry *h;
493
494 h = obj_coff_sym_hashes (abfd)[irel->r_symndx];
495 if (h == NULL)
496 {
497 struct internal_syment isym;
498
499 bfd_coff_swap_sym_in (abfd, extsyms + symesz * irel->r_symndx,
500 &isym);
501 targetsec = coff_section_from_bfd_index (abfd, isym.n_scnum);
502 }
503 else if (h->root.type == bfd_link_hash_defined
504 || h->root.type == bfd_link_hash_defweak)
505 targetsec = h->root.u.def.section;
506 else
507 targetsec = NULL;
508 }
509
510 bfd_put_32 (abfd,
511 (irel->r_vaddr - datasec->vma + datasec->output_offset), p);
512 memset (p + 4, 0, 8);
513 if (targetsec != NULL)
514 strncpy (p + 4, targetsec->output_section->name, 8);
515 }
516
517 return TRUE;
518}
519#endif /* neither ONLY_DECLARE_RELOCS not STATIC_RELOCS */
520
521
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
526#include "coffcode.h"
527
528#ifndef TARGET_SYM
529#define TARGET_SYM m68kcoff_vec
530#endif
531
532#ifndef TARGET_NAME
533#define TARGET_NAME "coff-m68k"
534#endif
535
536#ifdef NAMES_HAVE_UNDERSCORE
537CREATE_BIG_COFF_TARGET_VEC (TARGET_SYM, TARGET_NAME, D_PAGED, 0, '_', NULL)
538#else
539CREATE_BIG_COFF_TARGET_VEC (TARGET_SYM, TARGET_NAME, D_PAGED, 0, 0, NULL)
540#endif
Note: See TracBrowser for help on using the repository browser.