OSDN Git Service

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