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