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>
6 This file is part of BFD, the Binary File Descriptor library.
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.
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.
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. */
25 #include "libiberty.h"
28 #include "elf/msp430.h"
30 /* Use RELA instead of REL. */
33 static reloc_howto_type elf_msp430_howto_table[] =
35 HOWTO (R_MSP430_NONE, /* type */
37 2, /* size (0 = byte, 1 = short, 2 = long) */
39 FALSE, /* pc_relative */
41 complain_overflow_bitfield,/* complain_on_overflow */
42 bfd_elf_generic_reloc, /* special_function */
43 "R_MSP430_NONE", /* name */
44 FALSE, /* partial_inplace */
47 FALSE), /* pcrel_offset */
49 HOWTO (R_MSP430_32, /* type */
51 2, /* size (0 = byte, 1 = short, 2 = long) */
53 FALSE, /* pc_relative */
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 */
63 /* A 13 bit PC relative relocation. */
64 HOWTO (R_MSP430_10_PCREL, /* type */
66 1, /* size (0 = byte, 1 = short, 2 = long) */
68 TRUE, /* pc_relative */
70 complain_overflow_bitfield,/* complain_on_overflow */
71 bfd_elf_generic_reloc, /* special_function */
72 "R_MSP430_13_PCREL", /* name */
73 FALSE, /* partial_inplace */
76 TRUE), /* pcrel_offset */
78 /* A 16 bit absolute relocation. */
79 HOWTO (R_MSP430_16, /* type */
81 1, /* size (0 = byte, 1 = short, 2 = long) */
83 FALSE, /* pc_relative */
85 complain_overflow_dont,/* complain_on_overflow */
86 bfd_elf_generic_reloc, /* special_function */
87 "R_MSP430_16", /* name */
88 FALSE, /* partial_inplace */
90 0xffff, /* dst_mask */
91 FALSE), /* pcrel_offset */
93 /* A 16 bit absolute relocation for command address. */
94 HOWTO (R_MSP430_16_PCREL, /* type */
96 1, /* size (0 = byte, 1 = short, 2 = long) */
98 TRUE, /* pc_relative */
100 complain_overflow_dont,/* complain_on_overflow */
101 bfd_elf_generic_reloc, /* special_function */
102 "R_MSP430_16_PCREL", /* name */
103 FALSE, /* partial_inplace */
105 0xffff, /* dst_mask */
106 TRUE), /* pcrel_offset */
108 /* A 16 bit absolute relocation, byte operations. */
109 HOWTO (R_MSP430_16_BYTE, /* type */
111 1, /* size (0 = byte, 1 = short, 2 = long) */
113 FALSE, /* pc_relative */
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 */
123 /* A 16 bit absolute relocation for command address. */
124 HOWTO (R_MSP430_16_PCREL_BYTE,/* type */
126 1, /* size (0 = byte, 1 = short, 2 = long) */
128 TRUE, /* pc_relative */
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 */
138 /* A 13 bit PC relative relocation for complicated polymorphs. */
139 HOWTO (R_MSP430_2X_PCREL, /* type */
141 2, /* size (0 = byte, 1 = short, 2 = long) */
143 TRUE, /* pc_relative */
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 */
153 /* A 16 bit relaxable relocation for command address. */
154 HOWTO (R_MSP430_RL_PCREL, /* type */
156 1, /* size (0 = byte, 1 = short, 2 = long) */
158 TRUE, /* pc_relative */
160 complain_overflow_dont,/* complain_on_overflow */
161 bfd_elf_generic_reloc, /* special_function */
162 "R_MSP430_RL_PCREL", /* name */
163 FALSE, /* partial_inplace */
165 0xffff, /* dst_mask */
166 TRUE) /* pcrel_offset */
169 /* Map BFD reloc types to MSP430 ELF reloc types. */
171 struct msp430_reloc_map
173 bfd_reloc_code_real_type bfd_reloc_val;
174 unsigned int elf_reloc_val;
177 static const struct msp430_reloc_map msp430_reloc_map[] =
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}
191 static reloc_howto_type *
192 bfd_elf32_bfd_reloc_type_lookup (bfd * abfd ATTRIBUTE_UNUSED,
193 bfd_reloc_code_real_type code)
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];
204 static reloc_howto_type *
205 bfd_elf32_bfd_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
211 i < (sizeof (elf_msp430_howto_table)
212 / sizeof (elf_msp430_howto_table[0]));
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];
221 /* Set the howto pointer for an MSP430 ELF reloc. */
224 msp430_info_to_howto_rela (bfd * abfd ATTRIBUTE_UNUSED,
226 Elf_Internal_Rela * dst)
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];
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. */
240 elf32_msp430_check_relocs (bfd * abfd, struct bfd_link_info * info,
241 asection * sec, const Elf_Internal_Rela * relocs)
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;
248 if (info->relocatable)
251 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
252 sym_hashes = elf_sym_hashes (abfd);
254 rel_end = relocs + sec->reloc_count;
255 for (rel = relocs; rel < rel_end; rel++)
257 struct elf_link_hash_entry *h;
258 unsigned long r_symndx;
260 r_symndx = ELF32_R_SYM (rel->r_info);
261 if (r_symndx < symtab_hdr->sh_info)
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;
270 /* PR15323, ref flags aren't set for references in the same
272 h->root.non_ir_ref = 1;
279 /* Perform a single relocation. By default we use the standard BFD
280 routines, but a few relocs, we have to do them ourselves. */
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)
287 bfd_reloc_status_type r = bfd_reloc_ok;
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);
303 return bfd_reloc_outofrange;
305 /* MSP430 addresses commands as words. */
308 /* Check for an overflow. */
309 if (srel < -512 || srel > 511)
310 return bfd_reloc_overflow;
312 x = bfd_get_16 (input_bfd, contents);
313 x = (x & 0xfc00) | (srel & 0x3ff);
314 bfd_put_16 (input_bfd, x, contents);
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);
327 return bfd_reloc_outofrange;
329 /* MSP430 addresses commands as words. */
332 /* Check for an overflow. */
333 if (srel < -512 || srel > 511)
334 return bfd_reloc_overflow;
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);
342 x = (x & 0xfc00) | (srel & 0x3ff);
343 bfd_put_16 (input_bfd, x, contents - 2);
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);
357 return bfd_reloc_outofrange;
359 bfd_put_16 (input_bfd, srel & 0xffff, contents);
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);
371 bfd_put_16 (input_bfd, srel & 0xffff, contents);
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);
382 contents += rel->r_offset;
383 srel = (bfd_signed_vma) relocation;
384 srel += rel->r_addend;
387 return bfd_reloc_notsupported;
389 bfd_put_16 (input_bfd, srel & 0xffff, contents);
393 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
394 contents, rel->r_offset,
395 relocation, rel->r_addend);
401 /* Relocate an MSP430 ELF section. */
404 elf32_msp430_relocate_section (bfd * output_bfd ATTRIBUTE_UNUSED,
405 struct bfd_link_info * info,
407 asection * input_section,
409 Elf_Internal_Rela * relocs,
410 Elf_Internal_Sym * local_syms,
411 asection ** local_sections)
413 Elf_Internal_Shdr *symtab_hdr;
414 struct elf_link_hash_entry **sym_hashes;
415 Elf_Internal_Rela *rel;
416 Elf_Internal_Rela *relend;
418 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
419 sym_hashes = elf_sym_hashes (input_bfd);
420 relend = relocs + input_section->reloc_count;
422 for (rel = relocs; rel < relend; rel++)
424 reloc_howto_type *howto;
425 unsigned long r_symndx;
426 Elf_Internal_Sym *sym;
428 struct elf_link_hash_entry *h;
430 bfd_reloc_status_type r;
431 const char *name = NULL;
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;
441 if (r_symndx < symtab_hdr->sh_info)
443 sym = local_syms + r_symndx;
444 sec = local_sections[r_symndx];
445 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
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;
453 bfd_boolean unresolved_reloc, warned;
455 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
456 r_symndx, symtab_hdr, sym_hashes,
458 unresolved_reloc, warned);
461 if (sec != NULL && discarded_section (sec))
462 RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
463 rel, 1, relend, howto, 0, contents);
465 if (info->relocatable)
468 r = msp430_final_link_relocate (howto, input_bfd, input_section,
469 contents, rel, relocation);
471 if (r != bfd_reloc_ok)
473 const char *msg = (const char *) NULL;
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,
484 case bfd_reloc_undefined:
485 r = info->callbacks->undefined_symbol
486 (info, name, input_bfd, input_section, rel->r_offset, TRUE);
489 case bfd_reloc_outofrange:
490 msg = _("internal error: out of range error");
493 case bfd_reloc_notsupported:
494 msg = _("internal error: unsupported relocation error");
497 case bfd_reloc_dangerous:
498 msg = _("internal error: dangerous relocation");
502 msg = _("internal error: unknown error");
507 r = info->callbacks->warning
508 (info, msg, name, input_bfd, input_section, rel->r_offset);
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
524 bfd_elf_msp430_final_write_processing (bfd * abfd,
525 bfd_boolean linker ATTRIBUTE_UNUSED)
529 switch (bfd_get_mach (abfd))
532 case bfd_mach_msp110:
533 val = E_MSP430_MACH_MSP430x11x1;
537 val = E_MSP430_MACH_MSP430x11;
541 val = E_MSP430_MACH_MSP430x12;
545 val = E_MSP430_MACH_MSP430x13;
549 val = E_MSP430_MACH_MSP430x14;
553 val = E_MSP430_MACH_MSP430x15;
557 val = E_MSP430_MACH_MSP430x16;
561 val = E_MSP430_MACH_MSP430x31;
565 val = E_MSP430_MACH_MSP430x32;
569 val = E_MSP430_MACH_MSP430x33;
573 val = E_MSP430_MACH_MSP430x41;
577 val = E_MSP430_MACH_MSP430x42;
581 val = E_MSP430_MACH_MSP430x43;
585 val = E_MSP430_MACH_MSP430x44;
589 elf_elfheader (abfd)->e_machine = EM_MSP430;
590 elf_elfheader (abfd)->e_flags &= ~EF_MSP430_MACH;
591 elf_elfheader (abfd)->e_flags |= val;
594 /* Set the right machine number. */
597 elf32_msp430_object_p (bfd * abfd)
599 int e_set = bfd_mach_msp14;
601 if (elf_elfheader (abfd)->e_machine == EM_MSP430
602 || elf_elfheader (abfd)->e_machine == EM_MSP430_OLD)
604 int e_mach = elf_elfheader (abfd)->e_flags & EF_MSP430_MACH;
609 case E_MSP430_MACH_MSP430x11:
610 e_set = bfd_mach_msp11;
613 case E_MSP430_MACH_MSP430x11x1:
614 e_set = bfd_mach_msp110;
617 case E_MSP430_MACH_MSP430x12:
618 e_set = bfd_mach_msp12;
621 case E_MSP430_MACH_MSP430x13:
622 e_set = bfd_mach_msp13;
625 case E_MSP430_MACH_MSP430x14:
626 e_set = bfd_mach_msp14;
629 case E_MSP430_MACH_MSP430x15:
630 e_set = bfd_mach_msp15;
633 case E_MSP430_MACH_MSP430x16:
634 e_set = bfd_mach_msp16;
637 case E_MSP430_MACH_MSP430x31:
638 e_set = bfd_mach_msp31;
641 case E_MSP430_MACH_MSP430x32:
642 e_set = bfd_mach_msp32;
645 case E_MSP430_MACH_MSP430x33:
646 e_set = bfd_mach_msp33;
649 case E_MSP430_MACH_MSP430x41:
650 e_set = bfd_mach_msp41;
653 case E_MSP430_MACH_MSP430x42:
654 e_set = bfd_mach_msp42;
657 case E_MSP430_MACH_MSP430x43:
658 e_set = bfd_mach_msp43;
661 case E_MSP430_MACH_MSP430x44:
662 e_set = bfd_mach_msp44;
667 return bfd_default_set_arch_mach (abfd, bfd_arch_msp430, e_set);
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
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
680 There are quite a few relaxing opportunities available on the msp430:
682 ================================================================
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
695 ltn < jn jn +2; jmp +4; br lab
697 3. 4 words -> 2 words
699 gt > jeq +2; jge label jeq +6; jl +4; br label
700 gtu > jeq +2; jhs label jeq +6; jlo +4; br label
702 4. 4 words -> 2 words and 2 labels
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 =================================================================
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
717 ltn: 0x3001,0x3c02,0x4010,0x0000 -> 0x3000
720 gt: 0x2403,0x3802,0x4010,0x0000 -> 0x2401,0x3400
721 gtu: 0x2403,0x2802,0x4010,0x0000 -> 0x2401,0x2c00
724 leu: 0x2401,0x2c02,0x4010,0x0000 -> 0x2400,0x2800
725 le: 0x2401,0x3402,0x4010,0x0000 -> 0x2400,0x3800
728 jump: 0x4010,0x0000 -> 0x3c00. */
730 #define NUMB_RELAX_CODES 12
731 static struct rcodes_s
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
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. */
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}
759 /* Return TRUE if a symbol exists at the given address. */
762 msp430_elf_symbol_address_p (bfd * abfd,
764 Elf_Internal_Sym * isym,
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;
774 sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
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)
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++)
788 struct elf_link_hash_entry *sym_hash = *sym_hashes;
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)
800 /* Adjust all local symbols defined as '.section + 0xXXXX' (.section has sec_shndx)
801 referenced from current and other sections. */
803 msp430_elf_relax_adjust_locals(bfd * abfd, asection * sec, bfd_vma addr,
804 int count, unsigned int sec_shndx, bfd_vma toaddr)
806 Elf_Internal_Shdr *symtab_hdr;
807 Elf_Internal_Rela *irel;
808 Elf_Internal_Rela *irelend;
809 Elf_Internal_Sym *isym;
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;
816 for (irel = elf_section_data (sec)->relocs; irel < irelend; irel++)
818 int sidx = ELF32_R_SYM(irel->r_info);
819 Elf_Internal_Sym *lsym = isym + sidx;
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;
830 /* Delete some bytes from a section while relaxing. */
833 msp430_elf_relax_delete_bytes (bfd * abfd, asection * sec, bfd_vma addr,
836 Elf_Internal_Shdr *symtab_hdr;
837 unsigned int sec_shndx;
839 Elf_Internal_Rela *irel;
840 Elf_Internal_Rela *irelend;
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;
849 sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
851 contents = elf_section_data (sec)->this_hdr.contents;
855 irel = elf_section_data (sec)->relocs;
856 irelend = irel + sec->reloc_count;
858 /* Actually delete the bytes. */
859 memmove (contents + addr, contents + addr + count,
860 (size_t) (toaddr - addr - count));
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++)
868 /* Get the new reloc address. */
869 if ((irel->r_offset > addr && irel->r_offset < toaddr))
870 irel->r_offset -= count;
873 for (p = abfd->sections; p != NULL; p = p->next)
874 msp430_elf_relax_adjust_locals(abfd,p,addr,count,sec_shndx,toaddr);
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;
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++)
891 struct elf_link_hash_entry *sym_hash = *sym_hashes;
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;
905 msp430_elf_relax_section (bfd * abfd, asection * sec,
906 struct bfd_link_info * link_info,
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;
916 /* Assume nothing changes. */
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
922 if (link_info->relocatable
923 || (sec->flags & SEC_RELOC) == 0
924 || sec->reloc_count == 0 || (sec->flags & SEC_CODE) == 0)
927 symtab_hdr = & elf_tdata (abfd)->symtab_hdr;
929 /* Get a copy of the native relocations. */
931 _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL, link_info->keep_memory);
932 if (internal_relocs == NULL)
935 /* Walk through them looking for relaxing opportunities. */
936 irelend = internal_relocs + sec->reloc_count;
937 for (irel = internal_relocs; irel < irelend; irel++)
941 /* If this isn't something that can be relaxed, then ignore
943 if (ELF32_R_TYPE (irel->r_info) != (int) R_MSP430_RL_PCREL)
946 /* Get the section contents if we haven't done so already. */
947 if (contents == NULL)
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))
956 /* Read this BFD's local symbols if we haven't done so already. */
957 if (isymbuf == NULL && symtab_hdr->sh_info != 0)
959 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
961 isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
962 symtab_hdr->sh_info, 0,
968 /* Get the value of the symbol referred to by the reloc. */
969 if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
971 /* A local symbol. */
972 Elf_Internal_Sym *isym;
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;
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);
990 struct elf_link_hash_entry *h;
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);
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. */
1004 symval = (h->root.u.def.value
1005 + h->root.u.def.section->output_section->vma
1006 + h->root.u.def.section->output_offset);
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. */
1017 /* Try to turn a 16bit pc-relative branch into a 10bit pc-relative
1019 /* Paranoia? paranoia... */
1020 if (ELF32_R_TYPE (irel->r_info) == (int) R_MSP430_RL_PCREL)
1022 bfd_vma value = symval;
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;
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
1032 if ((long) value < 1016 && (long) value > -1016)
1034 int code0 = 0, code1 = 0, code2 = 0;
1036 struct rcodes_s *rx;
1038 /* Get the opcode. */
1039 if (irel->r_offset >= 6)
1040 code0 = bfd_get_16 (abfd, contents + irel->r_offset - 6);
1042 if (irel->r_offset >= 4)
1043 code1 = bfd_get_16 (abfd, contents + irel->r_offset - 4);
1045 code2 = bfd_get_16 (abfd, contents + irel->r_offset - 2);
1047 if (code2 != 0x4010)
1050 /* Check r4 and r3. */
1051 for (i = NUMB_RELAX_CODES - 1; i >= 0; i--)
1054 if (rx->cdx == 2 && rx->f0 == code0 && rx->f1 == code1)
1056 else if (rx->cdx == 1 && rx->f1 == code1)
1058 else if (rx->cdx == 0) /* This is an unconditional jump. */
1063 .Label0: ; we do not care about this label
1065 .Label1: ; make sure there is no label here
1067 .Label2: ; make sure there is no label here
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. */
1076 && msp430_elf_symbol_address_p (abfd, sec, isymbuf,
1077 irel->r_offset - 2))
1080 && msp430_elf_symbol_address_p (abfd, sec, isymbuf,
1081 irel->r_offset - 4))
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;
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),
1094 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
1097 /* Fix the opcode right way. */
1098 bfd_put_16 (abfd, rx->t0, contents + irel->r_offset - rx->off);
1100 bfd_put_16 (abfd, rx->t1,
1101 contents + irel->r_offset - rx->off + 2);
1104 if (!msp430_elf_relax_delete_bytes (abfd, sec,
1105 irel->r_offset - rx->off +
1109 /* Handle unconditional jumps. */
1111 irel->r_offset -= 2;
1113 /* That will change things, so, we should relax again.
1114 Note that this is not required, and it may be slow. */
1120 if (isymbuf != NULL && symtab_hdr->contents != (unsigned char *) isymbuf)
1122 if (!link_info->keep_memory)
1126 /* Cache the symbols for elf_link_input_bfd. */
1127 symtab_hdr->contents = (unsigned char *) isymbuf;
1131 if (contents != NULL
1132 && elf_section_data (sec)->this_hdr.contents != contents)
1134 if (!link_info->keep_memory)
1138 /* Cache the section contents for elf_link_input_bfd. */
1139 elf_section_data (sec)->this_hdr.contents = contents;
1143 if (internal_relocs != NULL
1144 && elf_section_data (sec)->relocs != internal_relocs)
1145 free (internal_relocs);
1150 if (isymbuf != NULL && symtab_hdr->contents != (unsigned char *) isymbuf)
1152 if (contents != NULL
1153 && elf_section_data (sec)->this_hdr.contents != contents)
1155 if (internal_relocs != NULL
1156 && elf_section_data (sec)->relocs != internal_relocs)
1157 free (internal_relocs);
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
1169 #define TARGET_LITTLE_SYM bfd_elf32_msp430_vec
1170 #define TARGET_LITTLE_NAME "elf32-msp430"
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
1182 #include "elf32-target.h"