2001-01-23 Kazu Hirata <kazu@hxi.com>
[external/binutils.git] / bfd / coff-mcore.c
1 /* BFD back-end for Motorola MCore COFF/PE
2    Copyright (C) 1999 Free Software Foundation, Inc.
3
4 This file is part of BFD, the Binary File Descriptor library.
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA.  */
20
21 #include "bfd.h"
22 #include "sysdep.h"
23 #include "libbfd.h"
24 #include "coff/mcore.h"
25 #include "coff/internal.h"
26 #include "coff/pe.h"
27 #include "libcoff.h"
28
29 #ifdef BADMAG
30 #undef BADMAG
31 #endif
32 #define BADMAG(x) MCOREBADMAG(x)
33
34 #ifndef NUM_ELEM
35 #define NUM_ELEM(A) (sizeof (A) / sizeof (A)[0])
36 #endif
37
38 /* This file is compiled more than once, but we only compile the
39    final_link routine once.  */
40 extern boolean mcore_bfd_coff_final_link
41   PARAMS ((bfd *, struct bfd_link_info *));
42
43 static struct bfd_link_hash_table * coff_mcore_link_hash_table_create
44   PARAMS ((bfd *));
45 static bfd_reloc_status_type        mcore_coff_unsupported_reloc
46   PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
47 static boolean                      coff_mcore_relocate_section
48   PARAMS ((bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
49            struct internal_reloc *, struct internal_syment *, asection **));
50 static reloc_howto_type *           mcore_coff_reloc_type_lookup
51   PARAMS ((bfd *, bfd_reloc_code_real_type));
52 static reloc_howto_type *           coff_mcore_rtype_to_howto
53   PARAMS ((bfd *, asection *, struct internal_reloc *,
54            struct coff_link_hash_entry *, struct internal_syment *, bfd_vma *));
55 \f
56 /* The NT loader points the toc register to &toc + 32768, in order to
57    use the complete range of a 16-bit displacement. We have to adjust
58    for this when we fix up loads displaced off the toc reg.  */
59 #define TOC_LOAD_ADJUSTMENT (-32768)
60 #define TOC_SECTION_NAME ".private.toc"
61
62 /* The main body of code is in coffcode.h.  */
63 #define COFF_DEFAULT_SECTION_ALIGNMENT_POWER 2
64
65 /* In case we're on a 32-bit machine, construct a 64-bit "-1" value
66    from smaller values.  Start with zero, widen, *then* decrement.  */
67 #define MINUS_ONE       (((bfd_vma)0) - 1)
68 \f
69 static reloc_howto_type mcore_coff_howto_table[] =
70 {
71   /* Unused: */
72   HOWTO (IMAGE_REL_MCORE_ABSOLUTE,/* type */
73          0,                      /* rightshift */
74          0,                      /* size (0 = byte, 1 = short, 2 = long) */
75          0,                      /* bitsize */
76          false,                  /* pc_relative */
77          0,                      /* bitpos */
78          complain_overflow_dont, /* dont complain_on_overflow */
79          NULL,                   /* special_function */
80          "ABSOLUTE",             /* name */
81          false,                  /* partial_inplace */
82          0x00,                   /* src_mask */
83          0x00,                   /* dst_mask */
84          false),                 /* pcrel_offset */
85
86   HOWTO (IMAGE_REL_MCORE_ADDR32,/* type */
87          0,                     /* rightshift */
88          2,                     /* size (0 = byte, 1 = short, 2 = long) */
89          32,                    /* bitsize */
90          false,                 /* pc_relative */
91          0,                     /* bitpos */
92          complain_overflow_bitfield, /* complain_on_overflow */
93          NULL,                  /* special_function */
94          "ADDR32",              /* name */
95          true,                  /* partial_inplace */
96          0xffffffff,            /* src_mask */
97          0xffffffff,            /* dst_mask */
98          false),                /* pcrel_offset */
99
100   /* 8 bits + 2 zero bits; jmpi/jsri/lrw instructions.
101      Should not appear in object files.  */
102   HOWTO (IMAGE_REL_MCORE_PCREL_IMM8BY4, /* type */
103          2,                     /* rightshift */
104          1,                     /* size (0 = byte, 1 = short, 2 = long) */
105          8,                     /* bitsize */
106          true,                  /* pc_relative */
107          0,                     /* bitpos */
108          complain_overflow_bitfield, /* complain_on_overflow */
109          mcore_coff_unsupported_reloc, /* special_function */
110          "IMM8BY4",             /* name */
111          false,                 /* partial_inplace */
112          0,                     /* src_mask */
113          0,                     /* dst_mask */
114          true),                 /* pcrel_offset */
115
116   /* bsr/bt/bf/br instructions; 11 bits + 1 zero bit
117      Span 2k instructions == 4k bytes.
118      Only useful pieces at the relocated address are the opcode (5 bits) */
119   HOWTO (IMAGE_REL_MCORE_PCREL_IMM11BY2,/* type */
120          1,                     /* rightshift */
121          1,                     /* size (0 = byte, 1 = short, 2 = long) */
122          11,                    /* bitsize */
123          true,                  /* pc_relative */
124          0,                     /* bitpos */
125          complain_overflow_signed, /* complain_on_overflow */
126          NULL,                  /* special_function */
127          "IMM11BY2",            /* name */
128          false,                 /* partial_inplace */
129          0x0,                   /* src_mask */
130          0x7ff,                 /* dst_mask */
131          true),                 /* pcrel_offset */
132
133   /* 4 bits + 1 zero bit; 'loopt' instruction only; unsupported.  */
134   HOWTO (IMAGE_REL_MCORE_PCREL_IMM4BY2, /* type */
135          1,                     /* rightshift */
136          1,                     /* size (0 = byte, 1 = short, 2 = long) */
137          4,                     /* bitsize */
138          true,                  /* pc_relative */
139          0,                     /* bitpos */
140          complain_overflow_bitfield, /* complain_on_overflow */
141          mcore_coff_unsupported_reloc, /* special_function */
142          "IMM4BY2",              /* name */
143          false,                 /* partial_inplace */
144          0,                     /* src_mask */
145          0,                     /* dst_mask */
146          true),                 /* pcrel_offset */
147
148   /* 32-bit pc-relative. Eventually this will help support PIC code.  */
149   HOWTO (IMAGE_REL_MCORE_PCREL_32,/* type */
150          0,                     /* rightshift */
151          2,                     /* size (0 = byte, 1 = short, 2 = long) */
152          32,                    /* bitsize */
153          true,                  /* pc_relative */
154          0,                     /* bitpos */
155          complain_overflow_bitfield, /* complain_on_overflow */
156          NULL,                  /* special_function */
157          "PCREL_32",            /* name */
158          false,                 /* partial_inplace */
159          0x0,                   /* src_mask */
160          0xffffffff,            /* dst_mask */
161          true),                 /* pcrel_offset */
162
163   /* Like PCREL_IMM11BY2, this relocation indicates that there is a
164      'jsri' at the specified address. There is a separate relocation
165      entry for the literal pool entry that it references, but we
166      might be able to change the jsri to a bsr if the target turns out
167      to be close enough [even though we won't reclaim the literal pool
168      entry, we'll get some runtime efficiency back]. Note that this
169      is a relocation that we are allowed to safely ignore.  */
170   HOWTO (IMAGE_REL_MCORE_PCREL_JSR_IMM11BY2,/* type */
171          1,                     /* rightshift */
172          1,                     /* size (0 = byte, 1 = short, 2 = long) */
173          11,                    /* bitsize */
174          true,                  /* pc_relative */
175          0,                     /* bitpos */
176          complain_overflow_signed, /* complain_on_overflow */
177          NULL,                  /* special_function */
178          "JSR_IMM11BY2",        /* name */
179          false,                 /* partial_inplace */
180          0x0,                   /* src_mask */
181          0x7ff,                 /* dst_mask */
182          true),                 /* pcrel_offset */
183
184   HOWTO (IMAGE_REL_MCORE_RVA,   /* type */
185          0,                     /* rightshift */
186          2,                     /* size (0 = byte, 1 = short, 2 = long) */
187          32,                    /* bitsize */
188          false,                 /* pc_relative */
189          0,                     /* bitpos */
190          complain_overflow_signed, /* complain_on_overflow */
191          NULL,                  /* special_function */
192          "MCORE_RVA",           /* name */
193          true,                  /* partial_inplace */
194          0xffffffff,            /* src_mask */
195          0xffffffff,            /* dst_mask */
196          true)                  /* pcrel_offset */
197 };
198 \f
199 /* Extend the coff_link_hash_table structure with a few M*Core specific fields.
200    This allows us to store global data here without actually creating any
201    global variables, which is a no-no in the BFD world.  */
202 typedef struct coff_mcore_link_hash_table
203 {
204   /* The original coff_link_hash_table structure.  MUST be first field.  */
205   struct coff_link_hash_table   root;
206
207   bfd *                         bfd_of_toc_owner;
208   long int                      global_toc_size;
209   long int                      import_table_size;
210   long int                      first_thunk_address;
211   long int                      thunk_size;
212 }
213 mcore_hash_table;
214
215 /* Get the MCore coff linker hash table from a link_info structure.  */
216 #define coff_mcore_hash_table(info) \
217   ((mcore_hash_table *) ((info)->hash))
218
219 /* Create an MCore coff linker hash table.  */
220 static struct bfd_link_hash_table *
221 coff_mcore_link_hash_table_create (abfd)
222      bfd * abfd;
223 {
224   mcore_hash_table * ret;
225
226   ret = ((mcore_hash_table *) bfd_alloc (abfd, sizeof (* ret)));
227   if (ret == (mcore_hash_table *) NULL)
228     return NULL;
229
230   if (! _bfd_coff_link_hash_table_init
231       (& ret->root, abfd, _bfd_coff_link_hash_newfunc))
232     {
233       bfd_release (abfd, ret);
234       return (struct bfd_link_hash_table *) NULL;
235     }
236
237   ret->bfd_of_toc_owner = NULL;
238   ret->global_toc_size  = 0;
239   ret->import_table_size = 0;
240   ret->first_thunk_address = 0;
241   ret->thunk_size = 0;
242
243   return & ret->root.root;
244 }
245 \f
246 /* Add an entry to the base file.  */
247 static void
248 mcore_emit_base_file_entry (info, output_bfd, input_section, reloc_offset)
249       struct bfd_link_info * info;
250       bfd *                  output_bfd;
251       asection *             input_section;
252       bfd_vma                reloc_offset;
253 {
254   bfd_vma addr = reloc_offset
255                  - input_section->vma
256                  + input_section->output_offset
257                  + input_section->output_section->vma;
258
259   if (coff_data (output_bfd)->pe)
260      addr -= pe_data (output_bfd)->pe_opthdr.ImageBase;
261
262   fwrite (&addr, 1, sizeof (addr), (FILE *) info->base_file);
263 }
264 \f
265 static bfd_reloc_status_type
266 mcore_coff_unsupported_reloc (abfd, reloc_entry, symbol, data, input_section,
267                            output_bfd, error_message)
268      bfd * abfd;
269      arelent * reloc_entry;
270      asymbol * symbol ATTRIBUTE_UNUSED;
271      PTR data ATTRIBUTE_UNUSED;
272      asection * input_section ATTRIBUTE_UNUSED;
273      bfd * output_bfd ATTRIBUTE_UNUSED;
274      char ** error_message ATTRIBUTE_UNUSED;
275 {
276   BFD_ASSERT (reloc_entry->howto != (reloc_howto_type *)0);
277
278   _bfd_error_handler (_("%s: Relocation %s (%d) is not currently supported.\n"),
279                       bfd_get_filename (abfd),
280                       reloc_entry->howto->name,
281                       reloc_entry->howto->type);
282
283   return bfd_reloc_notsupported;
284 }
285 \f
286 /* A cheesy little macro to make the code a little more readable.  */
287 #define HOW2MAP(bfd_rtype, mcore_rtype)  \
288  case bfd_rtype: return & mcore_coff_howto_table [mcore_rtype]
289
290 static reloc_howto_type *
291 mcore_coff_reloc_type_lookup (abfd, code)
292      bfd * abfd ATTRIBUTE_UNUSED;
293      bfd_reloc_code_real_type code;
294 {
295   switch (code)
296     {
297       HOW2MAP (BFD_RELOC_32,                       IMAGE_REL_MCORE_ADDR32);
298       HOW2MAP (BFD_RELOC_MCORE_PCREL_IMM8BY4,      IMAGE_REL_MCORE_PCREL_IMM8BY4);
299       HOW2MAP (BFD_RELOC_MCORE_PCREL_IMM11BY2,     IMAGE_REL_MCORE_PCREL_IMM11BY2);
300       HOW2MAP (BFD_RELOC_MCORE_PCREL_IMM4BY2,      IMAGE_REL_MCORE_PCREL_IMM4BY2);
301       HOW2MAP (BFD_RELOC_32_PCREL,                 IMAGE_REL_MCORE_PCREL_32);
302       HOW2MAP (BFD_RELOC_MCORE_PCREL_JSR_IMM11BY2, IMAGE_REL_MCORE_PCREL_JSR_IMM11BY2);
303       HOW2MAP (BFD_RELOC_RVA,                      IMAGE_REL_MCORE_RVA);
304    default:
305       return NULL;
306     }
307   /*NOTREACHED*/
308 }
309
310 #undef HOW2MAP
311
312 #define RTYPE2HOWTO(cache_ptr, dst) \
313   (cache_ptr)->howto = mcore_coff_howto_table + (dst)->r_type;
314
315 static reloc_howto_type *
316 coff_mcore_rtype_to_howto (abfd, sec, rel, h, sym, addendp)
317      bfd * abfd ATTRIBUTE_UNUSED;
318      asection * sec;
319      struct internal_reloc * rel;
320      struct coff_link_hash_entry * h ATTRIBUTE_UNUSED;
321      struct internal_syment * sym;
322      bfd_vma * addendp;
323 {
324   reloc_howto_type * howto;
325
326   if (rel->r_type >= NUM_ELEM (mcore_coff_howto_table))
327     return NULL;
328
329   howto = mcore_coff_howto_table + rel->r_type;
330
331   if (rel->r_type == IMAGE_REL_MCORE_RVA)
332     * addendp -= pe_data (sec->output_section->owner)->pe_opthdr.ImageBase;
333
334   else if (howto->pc_relative)
335     {
336       * addendp = sec->vma - 2; /* XXX guess - is this right ? */
337
338       /* If the symbol is defined, then the generic code is going to
339          add back the symbol value in order to cancel out an
340          adjustment it made to the addend.  However, we set the addend
341          to 0 at the start of this function.  We need to adjust here,
342          to avoid the adjustment the generic code will make.  FIXME:
343          This is getting a bit hackish.  */
344       if (sym != NULL && sym->n_scnum != 0)
345         * addendp -= sym->n_value;
346     }
347   else
348     * addendp = 0;
349
350   return howto;
351 }
352
353 /* Return true if this relocation should appear in the output .reloc section.
354    This function is referenced in pe_mkobject in peicode.h.  */
355 static boolean
356 in_reloc_p (abfd, howto)
357      bfd * abfd ATTRIBUTE_UNUSED;
358      reloc_howto_type * howto;
359 {
360   return ! howto->pc_relative && howto->type != IMAGE_REL_MCORE_RVA;
361 }
362 \f
363 /* The reloc processing routine for the optimized COFF linker.  */
364 static boolean
365 coff_mcore_relocate_section (output_bfd, info, input_bfd, input_section,
366                            contents, relocs, syms, sections)
367      bfd * output_bfd;
368      struct bfd_link_info * info;
369      bfd * input_bfd;
370      asection * input_section;
371      bfd_byte * contents;
372      struct internal_reloc * relocs;
373      struct internal_syment * syms;
374      asection ** sections;
375 {
376   struct internal_reloc * rel;
377   struct internal_reloc * relend;
378   boolean hihalf;
379   bfd_vma hihalf_val;
380
381   /* If we are performing a relocateable link, we don't need to do a
382      thing.  The caller will take care of adjusting the reloc
383      addresses and symbol indices.  */
384   if (info->relocateable)
385     return true;
386
387   /* Check if we have the same endianess */
388   if (   input_bfd->xvec->byteorder != output_bfd->xvec->byteorder
389       && output_bfd->xvec->byteorder != BFD_ENDIAN_UNKNOWN)
390     {
391       (*_bfd_error_handler)
392         (_("%s: compiled for a %s endian system and target is %s endian.\n"),
393          bfd_get_filename (input_bfd),
394          bfd_big_endian (input_bfd) ? "big" : "little",
395          bfd_big_endian (output_bfd) ? "big" : "little");
396
397       bfd_set_error (bfd_error_wrong_format);
398       return false;
399     }
400
401   hihalf = false;
402   hihalf_val = 0;
403
404   rel = relocs;
405   relend = rel + input_section->reloc_count;
406
407   for (; rel < relend; rel++)
408     {
409       long                           symndx;
410       struct internal_syment *       sym;
411       bfd_vma                        val;
412       bfd_vma                        addend;
413       bfd_reloc_status_type          rstat;
414       bfd_byte *                     loc;
415       unsigned short                 r_type = rel->r_type;
416       reloc_howto_type *             howto = NULL;
417       struct coff_link_hash_entry *  h;
418       const char *                   my_name;
419
420       symndx = rel->r_symndx;
421       loc = contents + rel->r_vaddr - input_section->vma;
422
423       if (symndx == -1)
424         {
425           h = NULL;
426           sym = NULL;
427         }
428       else
429         {
430           h = obj_coff_sym_hashes (input_bfd)[symndx];
431           sym = syms + symndx;
432         }
433
434       addend = 0;
435
436       /* Get the howto and initialise the addend.  */
437       howto = bfd_coff_rtype_to_howto (input_bfd, input_section, rel, h,
438                                        sym, & addend);
439       if (howto == NULL)
440         return false;
441
442       val = 0;
443
444       if (h == NULL)
445         {
446           if (symndx == -1)
447             my_name = "*ABS*";
448           else
449             {
450               asection * sec = sections[symndx];
451
452               val = (sym->n_value
453                      + sec->output_section->vma
454                      + sec->output_offset);
455
456               if (sym == NULL)
457                 my_name = "*unknown*";
458               else if (   sym->_n._n_n._n_zeroes == 0
459                        && sym->_n._n_n._n_offset != 0)
460                 my_name = obj_coff_strings (input_bfd) + sym->_n._n_n._n_offset;
461               else
462                 {
463                   static char buf [SYMNMLEN + 1];
464
465                   strncpy (buf, sym->_n._n_name, SYMNMLEN);
466                   buf[SYMNMLEN] = '\0';
467                   my_name = buf;
468                 }
469             }
470         }
471       else
472         {
473           if (   h->root.type == bfd_link_hash_defined
474               || h->root.type == bfd_link_hash_defweak)
475             {
476               asection * sec = h->root.u.def.section;
477
478               val = (h->root.u.def.value
479                      + sec->output_section->vma
480                      + sec->output_offset);
481             }
482           else
483             {
484               if (! ((*info->callbacks->undefined_symbol)
485                      (info, h->root.root.string, input_bfd, input_section,
486                       rel->r_vaddr - input_section->vma, true)))
487                 return false;
488             }
489
490           my_name = h->root.root.string;
491         }
492
493       rstat = bfd_reloc_ok;
494
495       /* Each case must do its own relocation, setting rstat appropriately.  */
496       switch (r_type)
497         {
498         default:
499           _bfd_error_handler (_("%s: unsupported relocation type 0x%02x"),
500                               bfd_get_filename (input_bfd), r_type);
501           bfd_set_error (bfd_error_bad_value);
502           return false;
503
504         case IMAGE_REL_MCORE_ABSOLUTE:
505           fprintf (stderr,
506                    _("Warning: unsupported reloc %s <file %s, section %s>\n"),
507                    howto->name,
508                    bfd_get_filename (input_bfd),
509                    input_section->name);
510
511           fprintf (stderr,"sym %ld (%s), r_vaddr %ld (%lx)\n",
512                    rel->r_symndx, my_name, (long) rel->r_vaddr,
513                    (unsigned long) rel->r_vaddr);
514           break;
515
516         case IMAGE_REL_MCORE_PCREL_IMM8BY4:
517         case IMAGE_REL_MCORE_PCREL_IMM11BY2:
518         case IMAGE_REL_MCORE_PCREL_IMM4BY2:
519         case IMAGE_REL_MCORE_PCREL_32:
520         case IMAGE_REL_MCORE_PCREL_JSR_IMM11BY2:
521         case IMAGE_REL_MCORE_ADDR32:
522           /* XXX fixme - shouldn't this be like the code for the RVA reloc ? */
523           rstat = _bfd_relocate_contents (howto, input_bfd, val, loc);
524           break;
525
526         case IMAGE_REL_MCORE_RVA:
527           rstat = _bfd_final_link_relocate
528             (howto, input_bfd,
529              input_section, contents, rel->r_vaddr - input_section->vma,
530              val, addend);
531           break;
532         }
533
534       if (info->base_file)
535         {
536           /* Emit a reloc if the backend thinks it needs it.  */
537           if (sym && pe_data (output_bfd)->in_reloc_p (output_bfd, howto))
538             mcore_emit_base_file_entry (info, output_bfd, input_section, rel->r_vaddr);
539         }
540
541       switch (rstat)
542         {
543         default:
544           abort ();
545
546         case bfd_reloc_ok:
547           break;
548
549         case bfd_reloc_overflow:
550           if (! ((*info->callbacks->reloc_overflow)
551                  (info, my_name, howto->name,
552                   (bfd_vma) 0, input_bfd,
553                   input_section, rel->r_vaddr - input_section->vma)))
554             return false;
555         }
556     }
557
558   return true;
559 }
560 \f
561 /* Tailor coffcode.h -- macro heaven.  */
562
563 /* We use the special COFF backend linker, with our own special touch.  */
564
565 #define coff_bfd_reloc_type_lookup   mcore_coff_reloc_type_lookup
566 #define coff_relocate_section        coff_mcore_relocate_section
567 #define coff_rtype_to_howto          coff_mcore_rtype_to_howto
568
569 #define SELECT_RELOC(internal, howto) {internal.r_type = howto->type;}
570
571 #define COFF_PAGE_SIZE               0x1000
572
573 #include "coffcode.h"
574 \f
575 /* Forward declaration to initialise alterbative_target field.  */
576 extern const bfd_target TARGET_LITTLE_SYM;
577
578 /* The transfer vectors that lead the outside world to all of the above.  */
579 CREATE_BIG_COFF_TARGET_VEC (TARGET_BIG_SYM, TARGET_BIG_NAME, D_PAGED,
580                             (SEC_CODE | SEC_DATA | SEC_DEBUGGING | SEC_READONLY | SEC_LINK_ONCE | SEC_LINK_DUPLICATES),
581                             0, & TARGET_LITTLE_SYM)
582 CREATE_LITTLE_COFF_TARGET_VEC (TARGET_LITTLE_SYM, TARGET_LITTLE_NAME, D_PAGED,
583                                (SEC_CODE | SEC_DATA | SEC_DEBUGGING | SEC_READONLY | SEC_LINK_ONCE | SEC_LINK_DUPLICATES),
584                                0, & TARGET_BIG_SYM)