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