PR ld/15323
[platform/upstream/binutils.git] / bfd / elf32-msp430.c
1 /*  MSP430-specific support for 32-bit ELF
2     Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2010, 2012
3     Free Software Foundation, Inc.
4     Contributed by Dmitry Diky <diwil@mail.ru>
5
6     This file is part of BFD, the Binary File Descriptor library.
7
8     This program is free software; you can redistribute it and/or modify
9     it under the terms of the GNU General Public License as published by
10     the Free Software Foundation; either version 3 of the License, or
11     (at your option) any later version.
12
13     This program is distributed in the hope that it will be useful,
14     but WITHOUT ANY WARRANTY; without even the implied warranty of
15     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16     GNU General Public License for more details.
17
18     You should have received a copy of the GNU General Public License
19     along with this program; if not, write to the Free Software
20     Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21     MA 02110-1301, USA.  */
22
23 #include "sysdep.h"
24 #include "bfd.h"
25 #include "libiberty.h"
26 #include "libbfd.h"
27 #include "elf-bfd.h"
28 #include "elf/msp430.h"
29
30 /* Use RELA instead of REL.  */
31 #undef USE_REL
32
33 static reloc_howto_type elf_msp430_howto_table[] =
34 {
35   HOWTO (R_MSP430_NONE,         /* type */
36          0,                     /* rightshift */
37          2,                     /* size (0 = byte, 1 = short, 2 = long) */
38          32,                    /* bitsize */
39          FALSE,                 /* pc_relative */
40          0,                     /* bitpos */
41          complain_overflow_bitfield,/* complain_on_overflow */
42          bfd_elf_generic_reloc, /* special_function */
43          "R_MSP430_NONE",       /* name */
44          FALSE,                 /* partial_inplace */
45          0,                     /* src_mask */
46          0,                     /* dst_mask */
47          FALSE),                /* pcrel_offset */
48
49   HOWTO (R_MSP430_32,           /* type */
50          0,                     /* rightshift */
51          2,                     /* size (0 = byte, 1 = short, 2 = long) */
52          32,                    /* bitsize */
53          FALSE,                 /* pc_relative */
54          0,                     /* bitpos */
55          complain_overflow_bitfield,/* complain_on_overflow */
56          bfd_elf_generic_reloc, /* special_function */
57          "R_MSP430_32",         /* name */
58          FALSE,                 /* partial_inplace */
59          0xffffffff,            /* src_mask */
60          0xffffffff,            /* dst_mask */
61          FALSE),                /* pcrel_offset */
62
63   /* A 13 bit PC relative relocation.  */
64   HOWTO (R_MSP430_10_PCREL,     /* type */
65          1,                     /* rightshift */
66          1,                     /* size (0 = byte, 1 = short, 2 = long) */
67          10,                    /* bitsize */
68          TRUE,                  /* pc_relative */
69          0,                     /* bitpos */
70          complain_overflow_bitfield,/* complain_on_overflow */
71          bfd_elf_generic_reloc, /* special_function */
72          "R_MSP430_13_PCREL",   /* name */
73          FALSE,                 /* partial_inplace */
74          0xfff,                 /* src_mask */
75          0xfff,                 /* dst_mask */
76          TRUE),                 /* pcrel_offset */
77
78   /* A 16 bit absolute relocation.  */
79   HOWTO (R_MSP430_16,           /* type */
80          0,                     /* rightshift */
81          1,                     /* size (0 = byte, 1 = short, 2 = long) */
82          16,                    /* bitsize */
83          FALSE,                 /* pc_relative */
84          0,                     /* bitpos */
85          complain_overflow_dont,/* complain_on_overflow */
86          bfd_elf_generic_reloc, /* special_function */
87          "R_MSP430_16",         /* name */
88          FALSE,                 /* partial_inplace */
89          0,                     /* src_mask */
90          0xffff,                /* dst_mask */
91          FALSE),                /* pcrel_offset */
92
93   /* A 16 bit absolute relocation for command address.  */
94   HOWTO (R_MSP430_16_PCREL,     /* type */
95          1,                     /* rightshift */
96          1,                     /* size (0 = byte, 1 = short, 2 = long) */
97          16,                    /* bitsize */
98          TRUE,                  /* pc_relative */
99          0,                     /* bitpos */
100          complain_overflow_dont,/* complain_on_overflow */
101          bfd_elf_generic_reloc, /* special_function */
102          "R_MSP430_16_PCREL",   /* name */
103          FALSE,                 /* partial_inplace */
104          0,                     /* src_mask */
105          0xffff,                /* dst_mask */
106          TRUE),                 /* pcrel_offset */
107
108   /* A 16 bit absolute relocation, byte operations.  */
109   HOWTO (R_MSP430_16_BYTE,      /* type */
110          0,                     /* rightshift */
111          1,                     /* size (0 = byte, 1 = short, 2 = long) */
112          16,                    /* bitsize */
113          FALSE,                 /* pc_relative */
114          0,                     /* bitpos */
115          complain_overflow_dont,/* complain_on_overflow */
116          bfd_elf_generic_reloc, /* special_function */
117          "R_MSP430_16_BYTE",    /* name */
118          FALSE,                 /* partial_inplace */
119          0xffff,                /* src_mask */
120          0xffff,                /* dst_mask */
121          FALSE),                /* pcrel_offset */
122
123   /* A 16 bit absolute relocation for command address.  */
124   HOWTO (R_MSP430_16_PCREL_BYTE,/* type */
125          1,                     /* rightshift */
126          1,                     /* size (0 = byte, 1 = short, 2 = long) */
127          16,                    /* bitsize */
128          TRUE,                  /* pc_relative */
129          0,                     /* bitpos */
130          complain_overflow_dont,/* complain_on_overflow */
131          bfd_elf_generic_reloc, /* special_function */
132          "R_MSP430_16_PCREL_BYTE",/* name */
133          FALSE,                 /* partial_inplace */
134          0xffff,                /* src_mask */
135          0xffff,                /* dst_mask */
136          TRUE),                 /* pcrel_offset */
137
138   /* A 13 bit PC relative relocation for complicated polymorphs.  */
139   HOWTO (R_MSP430_2X_PCREL,     /* type */
140          1,                     /* rightshift */
141          2,                     /* size (0 = byte, 1 = short, 2 = long) */
142          10,                    /* bitsize */
143          TRUE,                  /* pc_relative */
144          0,                     /* bitpos */
145          complain_overflow_bitfield,/* complain_on_overflow */
146          bfd_elf_generic_reloc, /* special_function */
147          "R_MSP430_2X_PCREL",   /* name */
148          FALSE,                 /* partial_inplace */
149          0xfff,                 /* src_mask */
150          0xfff,                 /* dst_mask */
151          TRUE),                 /* pcrel_offset */
152
153   /* A 16 bit relaxable relocation for command address.  */
154   HOWTO (R_MSP430_RL_PCREL,     /* type */
155          1,                     /* rightshift */
156          1,                     /* size (0 = byte, 1 = short, 2 = long) */
157          16,                    /* bitsize */
158          TRUE,                  /* pc_relative */
159          0,                     /* bitpos */
160          complain_overflow_dont,/* complain_on_overflow */
161          bfd_elf_generic_reloc, /* special_function */
162          "R_MSP430_RL_PCREL",   /* name */
163          FALSE,                 /* partial_inplace */
164          0,                     /* src_mask */
165          0xffff,                /* dst_mask */
166          TRUE)                  /* pcrel_offset */
167 };
168
169 /* Map BFD reloc types to MSP430 ELF reloc types.  */
170
171 struct msp430_reloc_map
172 {
173   bfd_reloc_code_real_type bfd_reloc_val;
174   unsigned int elf_reloc_val;
175 };
176
177 static const struct msp430_reloc_map msp430_reloc_map[] =
178   {
179     {BFD_RELOC_NONE,                 R_MSP430_NONE},
180     {BFD_RELOC_32,                   R_MSP430_32},
181     {BFD_RELOC_MSP430_10_PCREL,      R_MSP430_10_PCREL},
182     {BFD_RELOC_16,                   R_MSP430_16_BYTE},
183     {BFD_RELOC_MSP430_16_PCREL,      R_MSP430_16_PCREL},
184     {BFD_RELOC_MSP430_16,            R_MSP430_16},
185     {BFD_RELOC_MSP430_16_PCREL_BYTE, R_MSP430_16_PCREL_BYTE},
186     {BFD_RELOC_MSP430_16_BYTE,       R_MSP430_16_BYTE},
187     {BFD_RELOC_MSP430_2X_PCREL,      R_MSP430_2X_PCREL},
188     {BFD_RELOC_MSP430_RL_PCREL,      R_MSP430_RL_PCREL}
189   };
190
191 static reloc_howto_type *
192 bfd_elf32_bfd_reloc_type_lookup (bfd * abfd ATTRIBUTE_UNUSED,
193                                  bfd_reloc_code_real_type code)
194 {
195   unsigned int i;
196
197   for (i = 0; i < ARRAY_SIZE (msp430_reloc_map); i++)
198     if (msp430_reloc_map[i].bfd_reloc_val == code)
199       return &elf_msp430_howto_table[msp430_reloc_map[i].elf_reloc_val];
200
201   return NULL;
202 }
203
204 static reloc_howto_type *
205 bfd_elf32_bfd_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
206                                  const char *r_name)
207 {
208   unsigned int i;
209
210   for (i = 0;
211        i < (sizeof (elf_msp430_howto_table)
212             / sizeof (elf_msp430_howto_table[0]));
213        i++)
214     if (elf_msp430_howto_table[i].name != NULL
215         && strcasecmp (elf_msp430_howto_table[i].name, r_name) == 0)
216       return &elf_msp430_howto_table[i];
217
218   return NULL;
219 }
220
221 /* Set the howto pointer for an MSP430 ELF reloc.  */
222
223 static void
224 msp430_info_to_howto_rela (bfd * abfd ATTRIBUTE_UNUSED,
225                            arelent * cache_ptr,
226                            Elf_Internal_Rela * dst)
227 {
228   unsigned int r_type;
229
230   r_type = ELF32_R_TYPE (dst->r_info);
231   BFD_ASSERT (r_type < (unsigned int) R_MSP430_max);
232   cache_ptr->howto = &elf_msp430_howto_table[r_type];
233 }
234
235 /* Look through the relocs for a section during the first phase.
236    Since we don't do .gots or .plts, we just need to consider the
237    virtual table relocs for gc.  */
238
239 static bfd_boolean
240 elf32_msp430_check_relocs (bfd * abfd, struct bfd_link_info * info,
241                            asection * sec, const Elf_Internal_Rela * relocs)
242 {
243   Elf_Internal_Shdr *symtab_hdr;
244   struct elf_link_hash_entry **sym_hashes;
245   const Elf_Internal_Rela *rel;
246   const Elf_Internal_Rela *rel_end;
247
248   if (info->relocatable)
249     return TRUE;
250
251   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
252   sym_hashes = elf_sym_hashes (abfd);
253
254   rel_end = relocs + sec->reloc_count;
255   for (rel = relocs; rel < rel_end; rel++)
256     {
257       struct elf_link_hash_entry *h;
258       unsigned long r_symndx;
259
260       r_symndx = ELF32_R_SYM (rel->r_info);
261       if (r_symndx < symtab_hdr->sh_info)
262         h = NULL;
263       else
264         {
265           h = sym_hashes[r_symndx - symtab_hdr->sh_info];
266           while (h->root.type == bfd_link_hash_indirect
267                  || h->root.type == bfd_link_hash_warning)
268             h = (struct elf_link_hash_entry *) h->root.u.i.link;
269
270           /* PR15323, ref flags aren't set for references in the same
271              object.  */
272           h->root.non_ir_ref = 1;
273         }
274     }
275
276   return TRUE;
277 }
278
279 /* Perform a single relocation.  By default we use the standard BFD
280    routines, but a few relocs, we have to do them ourselves.  */
281
282 static bfd_reloc_status_type
283 msp430_final_link_relocate (reloc_howto_type * howto, bfd * input_bfd,
284                             asection * input_section, bfd_byte * contents,
285                             Elf_Internal_Rela * rel, bfd_vma relocation)
286 {
287   bfd_reloc_status_type r = bfd_reloc_ok;
288   bfd_vma x;
289   bfd_signed_vma srel;
290
291   switch (howto->type)
292     {
293     case R_MSP430_10_PCREL:
294       contents += rel->r_offset;
295       srel = (bfd_signed_vma) relocation;
296       srel += rel->r_addend;
297       srel -= rel->r_offset;
298       srel -= 2;                /* Branch instructions add 2 to the PC...  */
299       srel -= (input_section->output_section->vma +
300                input_section->output_offset);
301
302       if (srel & 1)
303         return bfd_reloc_outofrange;
304
305       /* MSP430 addresses commands as words.  */
306       srel >>= 1;
307
308       /* Check for an overflow.  */
309       if (srel < -512 || srel > 511)
310         return bfd_reloc_overflow;
311
312       x = bfd_get_16 (input_bfd, contents);
313       x = (x & 0xfc00) | (srel & 0x3ff);
314       bfd_put_16 (input_bfd, x, contents);
315       break;
316
317     case R_MSP430_2X_PCREL:
318       contents += rel->r_offset;
319       srel = (bfd_signed_vma) relocation;
320       srel += rel->r_addend;
321       srel -= rel->r_offset;
322       srel -= 2;                /* Branch instructions add 2 to the PC...  */
323       srel -= (input_section->output_section->vma +
324                input_section->output_offset);
325
326       if (srel & 1)
327         return bfd_reloc_outofrange;
328
329       /* MSP430 addresses commands as words.  */
330       srel >>= 1;
331
332       /* Check for an overflow.  */
333       if (srel < -512 || srel > 511)
334         return bfd_reloc_overflow;
335
336       x = bfd_get_16 (input_bfd, contents);
337       x = (x & 0xfc00) | (srel & 0x3ff);
338       bfd_put_16 (input_bfd, x, contents);
339       /* Handle second jump instruction.  */
340       x = bfd_get_16 (input_bfd, contents - 2);
341       srel += 1;
342       x = (x & 0xfc00) | (srel & 0x3ff);
343       bfd_put_16 (input_bfd, x, contents - 2);
344       break;
345
346     case R_MSP430_RL_PCREL:
347     case R_MSP430_16_PCREL:
348       contents += rel->r_offset;
349       srel = (bfd_signed_vma) relocation;
350       srel += rel->r_addend;
351       srel -= rel->r_offset;
352       /* Only branch instructions add 2 to the PC...  */
353       srel -= (input_section->output_section->vma +
354                input_section->output_offset);
355
356       if (srel & 1)
357         return bfd_reloc_outofrange;
358
359       bfd_put_16 (input_bfd, srel & 0xffff, contents);
360       break;
361
362     case R_MSP430_16_PCREL_BYTE:
363       contents += rel->r_offset;
364       srel = (bfd_signed_vma) relocation;
365       srel += rel->r_addend;
366       srel -= rel->r_offset;
367       /* Only branch instructions add 2 to the PC...  */
368       srel -= (input_section->output_section->vma +
369                input_section->output_offset);
370
371       bfd_put_16 (input_bfd, srel & 0xffff, contents);
372       break;
373
374     case R_MSP430_16_BYTE:
375       contents += rel->r_offset;
376       srel = (bfd_signed_vma) relocation;
377       srel += rel->r_addend;
378       bfd_put_16 (input_bfd, srel & 0xffff, contents);
379       break;
380
381     case R_MSP430_16:
382       contents += rel->r_offset;
383       srel = (bfd_signed_vma) relocation;
384       srel += rel->r_addend;
385
386       if (srel & 1)
387         return bfd_reloc_notsupported;
388
389       bfd_put_16 (input_bfd, srel & 0xffff, contents);
390       break;
391
392     default:
393       r = _bfd_final_link_relocate (howto, input_bfd, input_section,
394                                     contents, rel->r_offset,
395                                     relocation, rel->r_addend);
396     }
397
398   return r;
399 }
400
401 /* Relocate an MSP430 ELF section.  */
402
403 static bfd_boolean
404 elf32_msp430_relocate_section (bfd * output_bfd ATTRIBUTE_UNUSED,
405                                struct bfd_link_info * info,
406                                bfd * input_bfd,
407                                asection * input_section,
408                                bfd_byte * contents,
409                                Elf_Internal_Rela * relocs,
410                                Elf_Internal_Sym * local_syms,
411                                asection ** local_sections)
412 {
413   Elf_Internal_Shdr *symtab_hdr;
414   struct elf_link_hash_entry **sym_hashes;
415   Elf_Internal_Rela *rel;
416   Elf_Internal_Rela *relend;
417
418   symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
419   sym_hashes = elf_sym_hashes (input_bfd);
420   relend = relocs + input_section->reloc_count;
421
422   for (rel = relocs; rel < relend; rel++)
423     {
424       reloc_howto_type *howto;
425       unsigned long r_symndx;
426       Elf_Internal_Sym *sym;
427       asection *sec;
428       struct elf_link_hash_entry *h;
429       bfd_vma relocation;
430       bfd_reloc_status_type r;
431       const char *name = NULL;
432       int r_type;
433
434       r_type = ELF32_R_TYPE (rel->r_info);
435       r_symndx = ELF32_R_SYM (rel->r_info);
436       howto = elf_msp430_howto_table + r_type;
437       h = NULL;
438       sym = NULL;
439       sec = NULL;
440
441       if (r_symndx < symtab_hdr->sh_info)
442         {
443           sym = local_syms + r_symndx;
444           sec = local_sections[r_symndx];
445           relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
446
447           name = bfd_elf_string_from_elf_section
448               (input_bfd, symtab_hdr->sh_link, sym->st_name);
449           name = (name == NULL) ? bfd_section_name (input_bfd, sec) : name;
450         }
451       else
452         {
453           bfd_boolean unresolved_reloc, warned;
454
455           RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
456                                    r_symndx, symtab_hdr, sym_hashes,
457                                    h, sec, relocation,
458                                    unresolved_reloc, warned);
459         }
460
461       if (sec != NULL && discarded_section (sec))
462         RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
463                                          rel, 1, relend, howto, 0, contents);
464
465       if (info->relocatable)
466         continue;
467
468       r = msp430_final_link_relocate (howto, input_bfd, input_section,
469                                       contents, rel, relocation);
470
471       if (r != bfd_reloc_ok)
472         {
473           const char *msg = (const char *) NULL;
474
475           switch (r)
476             {
477             case bfd_reloc_overflow:
478               r = info->callbacks->reloc_overflow
479                   (info, (h ? &h->root : NULL), name, howto->name,
480                    (bfd_vma) 0, input_bfd, input_section,
481                    rel->r_offset);
482               break;
483
484             case bfd_reloc_undefined:
485               r = info->callbacks->undefined_symbol
486                   (info, name, input_bfd, input_section, rel->r_offset, TRUE);
487               break;
488
489             case bfd_reloc_outofrange:
490               msg = _("internal error: out of range error");
491               break;
492
493             case bfd_reloc_notsupported:
494               msg = _("internal error: unsupported relocation error");
495               break;
496
497             case bfd_reloc_dangerous:
498               msg = _("internal error: dangerous relocation");
499               break;
500
501             default:
502               msg = _("internal error: unknown error");
503               break;
504             }
505
506           if (msg)
507             r = info->callbacks->warning
508                 (info, msg, name, input_bfd, input_section, rel->r_offset);
509
510           if (!r)
511             return FALSE;
512         }
513
514     }
515
516   return TRUE;
517 }
518
519 /* The final processing done just before writing out a MSP430 ELF object
520    file.  This gets the MSP430 architecture right based on the machine
521    number.  */
522
523 static void
524 bfd_elf_msp430_final_write_processing (bfd * abfd,
525                                        bfd_boolean linker ATTRIBUTE_UNUSED)
526 {
527   unsigned long val;
528
529   switch (bfd_get_mach (abfd))
530     {
531     default:
532     case bfd_mach_msp110:
533       val = E_MSP430_MACH_MSP430x11x1;
534       break;
535
536     case bfd_mach_msp11:
537       val = E_MSP430_MACH_MSP430x11;
538       break;
539
540     case bfd_mach_msp12:
541       val = E_MSP430_MACH_MSP430x12;
542       break;
543
544     case bfd_mach_msp13:
545       val = E_MSP430_MACH_MSP430x13;
546       break;
547
548     case bfd_mach_msp14:
549       val = E_MSP430_MACH_MSP430x14;
550       break;
551
552     case bfd_mach_msp15:
553       val = E_MSP430_MACH_MSP430x15;
554       break;
555
556     case bfd_mach_msp16:
557       val = E_MSP430_MACH_MSP430x16;
558       break;
559
560     case bfd_mach_msp31:
561       val = E_MSP430_MACH_MSP430x31;
562       break;
563
564     case bfd_mach_msp32:
565       val = E_MSP430_MACH_MSP430x32;
566       break;
567
568     case bfd_mach_msp33:
569       val = E_MSP430_MACH_MSP430x33;
570       break;
571
572     case bfd_mach_msp41:
573       val = E_MSP430_MACH_MSP430x41;
574       break;
575
576     case bfd_mach_msp42:
577       val = E_MSP430_MACH_MSP430x42;
578       break;
579
580     case bfd_mach_msp43:
581       val = E_MSP430_MACH_MSP430x43;
582       break;
583
584     case bfd_mach_msp44:
585       val = E_MSP430_MACH_MSP430x44;
586       break;
587     }
588
589   elf_elfheader (abfd)->e_machine = EM_MSP430;
590   elf_elfheader (abfd)->e_flags &= ~EF_MSP430_MACH;
591   elf_elfheader (abfd)->e_flags |= val;
592 }
593
594 /* Set the right machine number.  */
595
596 static bfd_boolean
597 elf32_msp430_object_p (bfd * abfd)
598 {
599   int e_set = bfd_mach_msp14;
600
601   if (elf_elfheader (abfd)->e_machine == EM_MSP430
602       || elf_elfheader (abfd)->e_machine == EM_MSP430_OLD)
603     {
604       int e_mach = elf_elfheader (abfd)->e_flags & EF_MSP430_MACH;
605
606       switch (e_mach)
607         {
608         default:
609         case E_MSP430_MACH_MSP430x11:
610           e_set = bfd_mach_msp11;
611           break;
612
613         case E_MSP430_MACH_MSP430x11x1:
614           e_set = bfd_mach_msp110;
615           break;
616
617         case E_MSP430_MACH_MSP430x12:
618           e_set = bfd_mach_msp12;
619           break;
620
621         case E_MSP430_MACH_MSP430x13:
622           e_set = bfd_mach_msp13;
623           break;
624
625         case E_MSP430_MACH_MSP430x14:
626           e_set = bfd_mach_msp14;
627           break;
628
629         case E_MSP430_MACH_MSP430x15:
630           e_set = bfd_mach_msp15;
631           break;
632
633         case E_MSP430_MACH_MSP430x16:
634           e_set = bfd_mach_msp16;
635           break;
636
637         case E_MSP430_MACH_MSP430x31:
638           e_set = bfd_mach_msp31;
639           break;
640
641         case E_MSP430_MACH_MSP430x32:
642           e_set = bfd_mach_msp32;
643           break;
644
645         case E_MSP430_MACH_MSP430x33:
646           e_set = bfd_mach_msp33;
647           break;
648
649         case E_MSP430_MACH_MSP430x41:
650           e_set = bfd_mach_msp41;
651           break;
652
653         case E_MSP430_MACH_MSP430x42:
654           e_set = bfd_mach_msp42;
655           break;
656
657         case E_MSP430_MACH_MSP430x43:
658           e_set = bfd_mach_msp43;
659           break;
660
661         case E_MSP430_MACH_MSP430x44:
662           e_set = bfd_mach_msp44;
663           break;
664         }
665     }
666
667   return bfd_default_set_arch_mach (abfd, bfd_arch_msp430, e_set);
668 }
669
670 /* These functions handle relaxing for the msp430.
671    Relaxation required only in two cases:
672     - Bad hand coding like jumps from one section to another or
673       from file to file.
674     - Sibling calls. This will affect only 'jump label' polymorph. Without
675       relaxing this enlarges code by 2 bytes. Sibcalls implemented but
676       do not work in gcc's port by the reason I do not know.
677    Anyway, if a relaxation required, user should pass -relax option to the
678    linker.
679
680    There are quite a few relaxing opportunities available on the msp430:
681
682    ================================================================
683
684    1. 3 words -> 1 word
685
686    eq      ==      jeq label                    jne +4; br lab
687    ne      !=      jne label                    jeq +4; br lab
688    lt      <       jl  label                    jge +4; br lab
689    ltu     <       jlo label                    lhs +4; br lab
690    ge      >=      jge label                    jl  +4; br lab
691    geu     >=      jhs label                    jlo +4; br lab
692
693    2. 4 words -> 1 word
694
695    ltn     <       jn                      jn  +2; jmp +4; br lab
696
697    3. 4 words -> 2 words
698
699    gt      >       jeq +2; jge label       jeq +6; jl  +4; br label
700    gtu     >       jeq +2; jhs label       jeq +6; jlo +4; br label
701
702    4. 4 words -> 2 words and 2 labels
703
704    leu     <=      jeq label; jlo label    jeq +2; jhs +4; br label
705    le      <=      jeq label; jl  label    jeq +2; jge +4; br label
706    =================================================================
707
708    codemap for first cases is (labels masked ):
709               eq:       0x2002,0x4010,0x0000 -> 0x2400
710               ne:       0x2402,0x4010,0x0000 -> 0x2000
711               lt:       0x3402,0x4010,0x0000 -> 0x3800
712               ltu:      0x2c02,0x4010,0x0000 -> 0x2800
713               ge:       0x3802,0x4010,0x0000 -> 0x3400
714               geu:      0x2802,0x4010,0x0000 -> 0x2c00
715
716   second case:
717               ltn:      0x3001,0x3c02,0x4010,0x0000 -> 0x3000
718
719   third case:
720               gt:       0x2403,0x3802,0x4010,0x0000 -> 0x2401,0x3400
721               gtu:      0x2403,0x2802,0x4010,0x0000 -> 0x2401,0x2c00
722
723   fourth case:
724               leu:      0x2401,0x2c02,0x4010,0x0000 -> 0x2400,0x2800
725               le:       0x2401,0x3402,0x4010,0x0000 -> 0x2400,0x3800
726
727   Unspecified case :)
728               jump:     0x4010,0x0000 -> 0x3c00.  */
729
730 #define NUMB_RELAX_CODES        12
731 static struct rcodes_s
732 {
733   int f0, f1;                   /* From code.  */
734   int t0, t1;                   /* To code.  */
735   int labels;                   /* Position of labels: 1 - one label at first
736                                    word, 2 - one at second word, 3 - two
737                                    labels at both.  */
738   int cdx;                      /* Words to match.  */
739   int bs;                       /* Shrink bytes.  */
740   int off;                      /* Offset from old label for new code.  */
741   int ncl;                      /* New code length.  */
742 } rcode[] =
743 {/*                               lab,cdx,bs,off,ncl */
744   { 0x0000, 0x0000, 0x3c00, 0x0000, 1, 0, 2, 2,  2},    /* jump */
745   { 0x0000, 0x2002, 0x2400, 0x0000, 1, 1, 4, 4,  2},    /* eq */
746   { 0x0000, 0x2402, 0x2000, 0x0000, 1, 1, 4, 4,  2},    /* ne */
747   { 0x0000, 0x3402, 0x3800, 0x0000, 1, 1, 4, 4,  2},    /* lt */
748   { 0x0000, 0x2c02, 0x2800, 0x0000, 1, 1, 4, 4,  2},    /* ltu */
749   { 0x0000, 0x3802, 0x3400, 0x0000, 1, 1, 4, 4,  2},    /* ge */
750   { 0x0000, 0x2802, 0x2c00, 0x0000, 1, 1, 4, 4,  2},    /* geu */
751   { 0x3001, 0x3c02, 0x3000, 0x0000, 1, 2, 6, 6,  2},    /* ltn */
752   { 0x2403, 0x3802, 0x2401, 0x3400, 2, 2, 4, 6,  4},    /* gt */
753   { 0x2403, 0x2802, 0x2401, 0x2c00, 2, 2, 4, 6,  4},    /* gtu */
754   { 0x2401, 0x2c02, 0x2400, 0x2800, 3, 2, 4, 6,  4},    /* leu , 2 labels */
755   { 0x2401, 0x2c02, 0x2400, 0x2800, 3, 2, 4, 6,  4},    /* le  , 2 labels */
756   { 0,      0,      0,      0,      0, 0, 0, 0,  0}
757 };
758
759 /* Return TRUE if a symbol exists at the given address.  */
760
761 static bfd_boolean
762 msp430_elf_symbol_address_p (bfd * abfd,
763                              asection * sec,
764                              Elf_Internal_Sym * isym,
765                              bfd_vma addr)
766 {
767   Elf_Internal_Shdr *symtab_hdr;
768   unsigned int sec_shndx;
769   Elf_Internal_Sym *isymend;
770   struct elf_link_hash_entry **sym_hashes;
771   struct elf_link_hash_entry **end_hashes;
772   unsigned int symcount;
773
774   sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
775
776   /* Examine all the local symbols.  */
777   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
778   for (isymend = isym + symtab_hdr->sh_info; isym < isymend; isym++)
779     if (isym->st_shndx == sec_shndx && isym->st_value == addr)
780       return TRUE;
781
782   symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
783               - symtab_hdr->sh_info);
784   sym_hashes = elf_sym_hashes (abfd);
785   end_hashes = sym_hashes + symcount;
786   for (; sym_hashes < end_hashes; sym_hashes++)
787     {
788       struct elf_link_hash_entry *sym_hash = *sym_hashes;
789
790       if ((sym_hash->root.type == bfd_link_hash_defined
791            || sym_hash->root.type == bfd_link_hash_defweak)
792           && sym_hash->root.u.def.section == sec
793           && sym_hash->root.u.def.value == addr)
794         return TRUE;
795     }
796
797   return FALSE;
798 }
799
800 /* Adjust all local symbols defined as '.section + 0xXXXX' (.section has sec_shndx)
801     referenced from current and other sections.  */
802 static bfd_boolean
803 msp430_elf_relax_adjust_locals(bfd * abfd, asection * sec, bfd_vma addr,
804     int count, unsigned int sec_shndx, bfd_vma toaddr)
805 {
806   Elf_Internal_Shdr *symtab_hdr;
807   Elf_Internal_Rela *irel;
808   Elf_Internal_Rela *irelend;
809   Elf_Internal_Sym *isym;
810
811   irel = elf_section_data (sec)->relocs;
812   irelend = irel + sec->reloc_count;
813   symtab_hdr = & elf_tdata (abfd)->symtab_hdr;
814   isym = (Elf_Internal_Sym *) symtab_hdr->contents;
815
816   for (irel = elf_section_data (sec)->relocs; irel < irelend; irel++)
817     {
818       int sidx = ELF32_R_SYM(irel->r_info);
819       Elf_Internal_Sym *lsym = isym + sidx;
820
821       /* Adjust symbols referenced by .sec+0xXX */
822       if (irel->r_addend > addr && irel->r_addend < toaddr
823           && lsym->st_shndx == sec_shndx)
824         irel->r_addend -= count;
825     }
826
827   return TRUE;
828 }
829
830 /* Delete some bytes from a section while relaxing.  */
831
832 static bfd_boolean
833 msp430_elf_relax_delete_bytes (bfd * abfd, asection * sec, bfd_vma addr,
834                                int count)
835 {
836   Elf_Internal_Shdr *symtab_hdr;
837   unsigned int sec_shndx;
838   bfd_byte *contents;
839   Elf_Internal_Rela *irel;
840   Elf_Internal_Rela *irelend;
841   bfd_vma toaddr;
842   Elf_Internal_Sym *isym;
843   Elf_Internal_Sym *isymend;
844   struct elf_link_hash_entry **sym_hashes;
845   struct elf_link_hash_entry **end_hashes;
846   unsigned int symcount;
847   asection *p;
848
849   sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
850
851   contents = elf_section_data (sec)->this_hdr.contents;
852
853   toaddr = sec->size;
854
855   irel = elf_section_data (sec)->relocs;
856   irelend = irel + sec->reloc_count;
857
858   /* Actually delete the bytes.  */
859   memmove (contents + addr, contents + addr + count,
860            (size_t) (toaddr - addr - count));
861   sec->size -= count;
862
863   /* Adjust all the relocs.  */
864   symtab_hdr = & elf_tdata (abfd)->symtab_hdr;
865   isym = (Elf_Internal_Sym *) symtab_hdr->contents;
866   for (irel = elf_section_data (sec)->relocs; irel < irelend; irel++)
867     {
868       /* Get the new reloc address.  */
869       if ((irel->r_offset > addr && irel->r_offset < toaddr))
870         irel->r_offset -= count;
871     }
872
873   for (p = abfd->sections; p != NULL; p = p->next)
874     msp430_elf_relax_adjust_locals(abfd,p,addr,count,sec_shndx,toaddr);
875
876   /* Adjust the local symbols defined in this section.  */
877   symtab_hdr = & elf_tdata (abfd)->symtab_hdr;
878   isym = (Elf_Internal_Sym *) symtab_hdr->contents;
879   for (isymend = isym + symtab_hdr->sh_info; isym < isymend; isym++)
880     if (isym->st_shndx == sec_shndx
881         && isym->st_value > addr && isym->st_value < toaddr)
882       isym->st_value -= count;
883
884   /* Now adjust the global symbols defined in this section.  */
885   symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
886               - symtab_hdr->sh_info);
887   sym_hashes = elf_sym_hashes (abfd);
888   end_hashes = sym_hashes + symcount;
889   for (; sym_hashes < end_hashes; sym_hashes++)
890     {
891       struct elf_link_hash_entry *sym_hash = *sym_hashes;
892
893       if ((sym_hash->root.type == bfd_link_hash_defined
894            || sym_hash->root.type == bfd_link_hash_defweak)
895           && sym_hash->root.u.def.section == sec
896           && sym_hash->root.u.def.value > addr
897           && sym_hash->root.u.def.value < toaddr)
898         sym_hash->root.u.def.value -= count;
899     }
900
901   return TRUE;
902 }
903
904 static bfd_boolean
905 msp430_elf_relax_section (bfd * abfd, asection * sec,
906                           struct bfd_link_info * link_info,
907                           bfd_boolean * again)
908 {
909   Elf_Internal_Shdr * symtab_hdr;
910   Elf_Internal_Rela * internal_relocs;
911   Elf_Internal_Rela * irel;
912   Elf_Internal_Rela * irelend;
913   bfd_byte *          contents = NULL;
914   Elf_Internal_Sym *  isymbuf = NULL;
915
916   /* Assume nothing changes.  */
917   *again = FALSE;
918
919   /* We don't have to do anything for a relocatable link, if
920      this section does not have relocs, or if this is not a
921      code section.  */
922   if (link_info->relocatable
923       || (sec->flags & SEC_RELOC) == 0
924       || sec->reloc_count == 0 || (sec->flags & SEC_CODE) == 0)
925     return TRUE;
926
927   symtab_hdr = & elf_tdata (abfd)->symtab_hdr;
928
929   /* Get a copy of the native relocations.  */
930   internal_relocs =
931     _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL, link_info->keep_memory);
932   if (internal_relocs == NULL)
933     goto error_return;
934
935   /* Walk through them looking for relaxing opportunities.  */
936   irelend = internal_relocs + sec->reloc_count;
937   for (irel = internal_relocs; irel < irelend; irel++)
938     {
939       bfd_vma symval;
940
941       /* If this isn't something that can be relaxed, then ignore
942          this reloc.  */
943       if (ELF32_R_TYPE (irel->r_info) != (int) R_MSP430_RL_PCREL)
944         continue;
945
946       /* Get the section contents if we haven't done so already.  */
947       if (contents == NULL)
948         {
949           /* Get cached copy if it exists.  */
950           if (elf_section_data (sec)->this_hdr.contents != NULL)
951             contents = elf_section_data (sec)->this_hdr.contents;
952           else if (! bfd_malloc_and_get_section (abfd, sec, &contents))
953             goto error_return;
954         }
955
956       /* Read this BFD's local symbols if we haven't done so already.  */
957       if (isymbuf == NULL && symtab_hdr->sh_info != 0)
958         {
959           isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
960           if (isymbuf == NULL)
961             isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
962                                             symtab_hdr->sh_info, 0,
963                                             NULL, NULL, NULL);
964           if (isymbuf == NULL)
965             goto error_return;
966         }
967
968       /* Get the value of the symbol referred to by the reloc.  */
969       if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
970         {
971           /* A local symbol.  */
972           Elf_Internal_Sym *isym;
973           asection *sym_sec;
974
975           isym = isymbuf + ELF32_R_SYM (irel->r_info);
976           if (isym->st_shndx == SHN_UNDEF)
977             sym_sec = bfd_und_section_ptr;
978           else if (isym->st_shndx == SHN_ABS)
979             sym_sec = bfd_abs_section_ptr;
980           else if (isym->st_shndx == SHN_COMMON)
981             sym_sec = bfd_com_section_ptr;
982           else
983             sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
984           symval = (isym->st_value
985                     + sym_sec->output_section->vma + sym_sec->output_offset);
986         }
987       else
988         {
989           unsigned long indx;
990           struct elf_link_hash_entry *h;
991
992           /* An external symbol.  */
993           indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
994           h = elf_sym_hashes (abfd)[indx];
995           BFD_ASSERT (h != NULL);
996
997           if (h->root.type != bfd_link_hash_defined
998               && h->root.type != bfd_link_hash_defweak)
999             /* This appears to be a reference to an undefined
1000                symbol.  Just ignore it--it will be caught by the
1001                regular reloc processing.  */
1002             continue;
1003
1004           symval = (h->root.u.def.value
1005                     + h->root.u.def.section->output_section->vma
1006                     + h->root.u.def.section->output_offset);
1007         }
1008
1009       /* For simplicity of coding, we are going to modify the section
1010          contents, the section relocs, and the BFD symbol table.  We
1011          must tell the rest of the code not to free up this
1012          information.  It would be possible to instead create a table
1013          of changes which have to be made, as is done in coff-mips.c;
1014          that would be more work, but would require less memory when
1015          the linker is run.  */
1016
1017       /* Try to turn a 16bit pc-relative branch into a 10bit pc-relative
1018          branch.  */
1019       /* Paranoia? paranoia...  */
1020       if (ELF32_R_TYPE (irel->r_info) == (int) R_MSP430_RL_PCREL)
1021         {
1022           bfd_vma value = symval;
1023
1024           /* Deal with pc-relative gunk.  */
1025           value -= (sec->output_section->vma + sec->output_offset);
1026           value -= irel->r_offset;
1027           value += irel->r_addend;
1028
1029           /* See if the value will fit in 10 bits, note the high value is
1030              1016 as the target will be two bytes closer if we are
1031              able to relax. */
1032           if ((long) value < 1016 && (long) value > -1016)
1033             {
1034               int code0 = 0, code1 = 0, code2 = 0;
1035               int i;
1036               struct rcodes_s *rx;
1037
1038               /* Get the opcode.  */
1039               if (irel->r_offset >= 6)
1040                 code0 = bfd_get_16 (abfd, contents + irel->r_offset - 6);
1041
1042               if (irel->r_offset >= 4)
1043                 code1 = bfd_get_16 (abfd, contents + irel->r_offset - 4);
1044
1045               code2 = bfd_get_16 (abfd, contents + irel->r_offset - 2);
1046
1047               if (code2 != 0x4010)
1048                 continue;
1049
1050               /* Check r4 and r3.  */
1051               for (i = NUMB_RELAX_CODES - 1; i >= 0; i--)
1052                 {
1053                   rx = &rcode[i];
1054                   if (rx->cdx == 2 && rx->f0 == code0 && rx->f1 == code1)
1055                     break;
1056                   else if (rx->cdx == 1 && rx->f1 == code1)
1057                     break;
1058                   else if (rx->cdx == 0)        /* This is an unconditional jump.  */
1059                     break;
1060                 }
1061
1062               /* Check labels:
1063                    .Label0:       ; we do not care about this label
1064                       jeq    +6
1065                    .Label1:       ; make sure there is no label here
1066                       jl     +4
1067                    .Label2:       ; make sure there is no label here
1068                       br .Label_dst
1069
1070                  So, if there is .Label1 or .Label2 we cannot relax this code.
1071                  This actually should not happen, cause for relaxable
1072                  instructions we use RL_PCREL reloc instead of 16_PCREL.
1073                  Will change this in the future. */
1074
1075               if (rx->cdx > 0
1076                   && msp430_elf_symbol_address_p (abfd, sec, isymbuf,
1077                                                   irel->r_offset - 2))
1078                 continue;
1079               if (rx->cdx > 1
1080                   && msp430_elf_symbol_address_p (abfd, sec, isymbuf,
1081                                                   irel->r_offset - 4))
1082                 continue;
1083
1084               /* Note that we've changed the relocs, section contents, etc.  */
1085               elf_section_data (sec)->relocs = internal_relocs;
1086               elf_section_data (sec)->this_hdr.contents = contents;
1087               symtab_hdr->contents = (unsigned char *) isymbuf;
1088
1089               /* Fix the relocation's type.  */
1090               if (rx->labels == 3)      /* Handle special cases.  */
1091                 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
1092                                            R_MSP430_2X_PCREL);
1093               else
1094                 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
1095                                            R_MSP430_10_PCREL);
1096
1097               /* Fix the opcode right way.  */
1098               bfd_put_16 (abfd, rx->t0, contents + irel->r_offset - rx->off);
1099               if (rx->t1)
1100                 bfd_put_16 (abfd, rx->t1,
1101                             contents + irel->r_offset - rx->off + 2);
1102
1103               /* Delete bytes. */
1104               if (!msp430_elf_relax_delete_bytes (abfd, sec,
1105                                                   irel->r_offset - rx->off +
1106                                                   rx->ncl, rx->bs))
1107                 goto error_return;
1108
1109               /* Handle unconditional jumps.  */
1110               if (rx->cdx == 0)
1111                 irel->r_offset -= 2;
1112
1113               /* That will change things, so, we should relax again.
1114                  Note that this is not required, and it may be slow.  */
1115               *again = TRUE;
1116             }
1117         }
1118     }
1119
1120   if (isymbuf != NULL && symtab_hdr->contents != (unsigned char *) isymbuf)
1121     {
1122       if (!link_info->keep_memory)
1123         free (isymbuf);
1124       else
1125         {
1126           /* Cache the symbols for elf_link_input_bfd.  */
1127           symtab_hdr->contents = (unsigned char *) isymbuf;
1128         }
1129     }
1130
1131   if (contents != NULL
1132       && elf_section_data (sec)->this_hdr.contents != contents)
1133     {
1134       if (!link_info->keep_memory)
1135         free (contents);
1136       else
1137         {
1138           /* Cache the section contents for elf_link_input_bfd.  */
1139           elf_section_data (sec)->this_hdr.contents = contents;
1140         }
1141     }
1142
1143   if (internal_relocs != NULL
1144       && elf_section_data (sec)->relocs != internal_relocs)
1145     free (internal_relocs);
1146
1147   return TRUE;
1148
1149 error_return:
1150   if (isymbuf != NULL && symtab_hdr->contents != (unsigned char *) isymbuf)
1151     free (isymbuf);
1152   if (contents != NULL
1153       && elf_section_data (sec)->this_hdr.contents != contents)
1154     free (contents);
1155   if (internal_relocs != NULL
1156       && elf_section_data (sec)->relocs != internal_relocs)
1157     free (internal_relocs);
1158
1159   return FALSE;
1160 }
1161
1162
1163 #define ELF_ARCH                bfd_arch_msp430
1164 #define ELF_MACHINE_CODE        EM_MSP430
1165 #define ELF_MACHINE_ALT1        EM_MSP430_OLD
1166 #define ELF_MAXPAGESIZE         1
1167 #define ELF_OSABI               ELFOSABI_STANDALONE
1168
1169 #define TARGET_LITTLE_SYM       bfd_elf32_msp430_vec
1170 #define TARGET_LITTLE_NAME      "elf32-msp430"
1171
1172 #define elf_info_to_howto                    msp430_info_to_howto_rela
1173 #define elf_info_to_howto_rel                NULL
1174 #define elf_backend_relocate_section         elf32_msp430_relocate_section
1175 #define elf_backend_check_relocs             elf32_msp430_check_relocs
1176 #define elf_backend_can_gc_sections          1
1177 #define elf_backend_final_write_processing   bfd_elf_msp430_final_write_processing
1178 #define elf_backend_object_p                 elf32_msp430_object_p
1179 #define elf_backend_post_process_headers     _bfd_elf_set_osabi
1180 #define bfd_elf32_bfd_relax_section          msp430_elf_relax_section
1181
1182 #include "elf32-target.h"