1 /* MSP430-specific support for 32-bit ELF
2 Copyright (C) 2002, 2003, 2004, 2005, 2006
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 2 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, MA 02110-1301, USA. */
24 #include "libiberty.h"
27 #include "elf/msp430.h"
29 /* Use RELA instead of REL. */
32 static reloc_howto_type elf_msp430_howto_table
[] =
34 HOWTO (R_MSP430_NONE
, /* type */
36 2, /* size (0 = byte, 1 = short, 2 = long) */
38 FALSE
, /* pc_relative */
40 complain_overflow_bitfield
,/* complain_on_overflow */
41 bfd_elf_generic_reloc
, /* special_function */
42 "R_MSP430_NONE", /* name */
43 FALSE
, /* partial_inplace */
46 FALSE
), /* pcrel_offset */
48 HOWTO (R_MSP430_32
, /* type */
50 2, /* size (0 = byte, 1 = short, 2 = long) */
52 FALSE
, /* pc_relative */
54 complain_overflow_bitfield
,/* complain_on_overflow */
55 bfd_elf_generic_reloc
, /* special_function */
56 "R_MSP430_32", /* name */
57 FALSE
, /* partial_inplace */
58 0xffffffff, /* src_mask */
59 0xffffffff, /* dst_mask */
60 FALSE
), /* pcrel_offset */
62 /* A 13 bit PC relative relocation. */
63 HOWTO (R_MSP430_10_PCREL
, /* type */
65 1, /* size (0 = byte, 1 = short, 2 = long) */
67 TRUE
, /* pc_relative */
69 complain_overflow_bitfield
,/* complain_on_overflow */
70 bfd_elf_generic_reloc
, /* special_function */
71 "R_MSP430_13_PCREL", /* name */
72 FALSE
, /* partial_inplace */
75 TRUE
), /* pcrel_offset */
77 /* A 16 bit absolute relocation. */
78 HOWTO (R_MSP430_16
, /* type */
80 1, /* size (0 = byte, 1 = short, 2 = long) */
82 FALSE
, /* pc_relative */
84 complain_overflow_dont
,/* complain_on_overflow */
85 bfd_elf_generic_reloc
, /* special_function */
86 "R_MSP430_16", /* name */
87 FALSE
, /* partial_inplace */
89 0xffff, /* dst_mask */
90 FALSE
), /* pcrel_offset */
92 /* A 16 bit absolute relocation for command address. */
93 HOWTO (R_MSP430_16_PCREL
, /* type */
95 1, /* size (0 = byte, 1 = short, 2 = long) */
97 TRUE
, /* pc_relative */
99 complain_overflow_dont
,/* complain_on_overflow */
100 bfd_elf_generic_reloc
, /* special_function */
101 "R_MSP430_16_PCREL", /* name */
102 FALSE
, /* partial_inplace */
104 0xffff, /* dst_mask */
105 TRUE
), /* pcrel_offset */
107 /* A 16 bit absolute relocation, byte operations. */
108 HOWTO (R_MSP430_16_BYTE
, /* type */
110 1, /* size (0 = byte, 1 = short, 2 = long) */
112 FALSE
, /* pc_relative */
114 complain_overflow_dont
,/* complain_on_overflow */
115 bfd_elf_generic_reloc
, /* special_function */
116 "R_MSP430_16_BYTE", /* name */
117 FALSE
, /* partial_inplace */
118 0xffff, /* src_mask */
119 0xffff, /* dst_mask */
120 FALSE
), /* pcrel_offset */
122 /* A 16 bit absolute relocation for command address. */
123 HOWTO (R_MSP430_16_PCREL_BYTE
,/* type */
125 1, /* size (0 = byte, 1 = short, 2 = long) */
127 TRUE
, /* pc_relative */
129 complain_overflow_dont
,/* complain_on_overflow */
130 bfd_elf_generic_reloc
, /* special_function */
131 "R_MSP430_16_PCREL_BYTE",/* name */
132 FALSE
, /* partial_inplace */
133 0xffff, /* src_mask */
134 0xffff, /* dst_mask */
135 TRUE
), /* pcrel_offset */
137 /* A 13 bit PC relative relocation for complicated polymorphs. */
138 HOWTO (R_MSP430_2X_PCREL
, /* type */
140 2, /* size (0 = byte, 1 = short, 2 = long) */
142 TRUE
, /* pc_relative */
144 complain_overflow_bitfield
,/* complain_on_overflow */
145 bfd_elf_generic_reloc
, /* special_function */
146 "R_MSP430_2X_PCREL", /* name */
147 FALSE
, /* partial_inplace */
148 0xfff, /* src_mask */
149 0xfff, /* dst_mask */
150 TRUE
), /* pcrel_offset */
152 /* A 16 bit relaxable relocation for command address. */
153 HOWTO (R_MSP430_RL_PCREL
, /* type */
155 1, /* size (0 = byte, 1 = short, 2 = long) */
157 TRUE
, /* pc_relative */
159 complain_overflow_dont
,/* complain_on_overflow */
160 bfd_elf_generic_reloc
, /* special_function */
161 "R_MSP430_RL_PCREL", /* name */
162 FALSE
, /* partial_inplace */
164 0xffff, /* dst_mask */
165 TRUE
) /* pcrel_offset */
168 /* Map BFD reloc types to MSP430 ELF reloc types. */
170 struct msp430_reloc_map
172 bfd_reloc_code_real_type bfd_reloc_val
;
173 unsigned int elf_reloc_val
;
176 static const struct msp430_reloc_map msp430_reloc_map
[] =
178 {BFD_RELOC_NONE
, R_MSP430_NONE
},
179 {BFD_RELOC_32
, R_MSP430_32
},
180 {BFD_RELOC_MSP430_10_PCREL
, R_MSP430_10_PCREL
},
181 {BFD_RELOC_16
, R_MSP430_16_BYTE
},
182 {BFD_RELOC_MSP430_16_PCREL
, R_MSP430_16_PCREL
},
183 {BFD_RELOC_MSP430_16
, R_MSP430_16
},
184 {BFD_RELOC_MSP430_16_PCREL_BYTE
, R_MSP430_16_PCREL_BYTE
},
185 {BFD_RELOC_MSP430_16_BYTE
, R_MSP430_16_BYTE
},
186 {BFD_RELOC_MSP430_2X_PCREL
, R_MSP430_2X_PCREL
},
187 {BFD_RELOC_MSP430_RL_PCREL
, R_MSP430_RL_PCREL
}
190 static reloc_howto_type
*
191 bfd_elf32_bfd_reloc_type_lookup (bfd
* abfd ATTRIBUTE_UNUSED
,
192 bfd_reloc_code_real_type code
)
196 for (i
= 0; i
< ARRAY_SIZE (msp430_reloc_map
); i
++)
197 if (msp430_reloc_map
[i
].bfd_reloc_val
== code
)
198 return &elf_msp430_howto_table
[msp430_reloc_map
[i
].elf_reloc_val
];
203 /* Set the howto pointer for an MSP430 ELF reloc. */
206 msp430_info_to_howto_rela (bfd
* abfd ATTRIBUTE_UNUSED
,
208 Elf_Internal_Rela
* dst
)
212 r_type
= ELF32_R_TYPE (dst
->r_info
);
213 BFD_ASSERT (r_type
< (unsigned int) R_MSP430_max
);
214 cache_ptr
->howto
= &elf_msp430_howto_table
[r_type
];
217 /* Look through the relocs for a section during the first phase.
218 Since we don't do .gots or .plts, we just need to consider the
219 virtual table relocs for gc. */
222 elf32_msp430_check_relocs (bfd
* abfd
, struct bfd_link_info
* info
,
223 asection
* sec
, const Elf_Internal_Rela
* relocs
)
225 Elf_Internal_Shdr
*symtab_hdr
;
226 struct elf_link_hash_entry
**sym_hashes
, **sym_hashes_end
;
227 const Elf_Internal_Rela
*rel
;
228 const Elf_Internal_Rela
*rel_end
;
230 if (info
->relocatable
)
233 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
234 sym_hashes
= elf_sym_hashes (abfd
);
236 sym_hashes
+ symtab_hdr
->sh_size
/ sizeof (Elf32_External_Sym
);
237 if (!elf_bad_symtab (abfd
))
238 sym_hashes_end
-= symtab_hdr
->sh_info
;
240 rel_end
= relocs
+ sec
->reloc_count
;
241 for (rel
= relocs
; rel
< rel_end
; rel
++)
243 struct elf_link_hash_entry
*h
;
244 unsigned long r_symndx
;
246 r_symndx
= ELF32_R_SYM (rel
->r_info
);
247 if (r_symndx
< symtab_hdr
->sh_info
)
251 h
= sym_hashes
[r_symndx
- symtab_hdr
->sh_info
];
252 while (h
->root
.type
== bfd_link_hash_indirect
253 || h
->root
.type
== bfd_link_hash_warning
)
254 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
261 /* Perform a single relocation. By default we use the standard BFD
262 routines, but a few relocs, we have to do them ourselves. */
264 static bfd_reloc_status_type
265 msp430_final_link_relocate (reloc_howto_type
* howto
, bfd
* input_bfd
,
266 asection
* input_section
, bfd_byte
* contents
,
267 Elf_Internal_Rela
* rel
, bfd_vma relocation
)
269 bfd_reloc_status_type r
= bfd_reloc_ok
;
275 case R_MSP430_10_PCREL
:
276 contents
+= rel
->r_offset
;
277 srel
= (bfd_signed_vma
) relocation
;
278 srel
+= rel
->r_addend
;
279 srel
-= rel
->r_offset
;
280 srel
-= 2; /* Branch instructions add 2 to the PC... */
281 srel
-= (input_section
->output_section
->vma
+
282 input_section
->output_offset
);
285 return bfd_reloc_outofrange
;
287 /* MSP430 addresses commands as words. */
290 /* Check for an overflow. */
291 if (srel
< -512 || srel
> 511)
292 return bfd_reloc_overflow
;
294 x
= bfd_get_16 (input_bfd
, contents
);
295 x
= (x
& 0xfc00) | (srel
& 0x3ff);
296 bfd_put_16 (input_bfd
, x
, contents
);
299 case R_MSP430_2X_PCREL
:
300 contents
+= rel
->r_offset
;
301 srel
= (bfd_signed_vma
) relocation
;
302 srel
+= rel
->r_addend
;
303 srel
-= rel
->r_offset
;
304 srel
-= 2; /* Branch instructions add 2 to the PC... */
305 srel
-= (input_section
->output_section
->vma
+
306 input_section
->output_offset
);
309 return bfd_reloc_outofrange
;
311 /* MSP430 addresses commands as words. */
314 /* Check for an overflow. */
315 if (srel
< -512 || srel
> 511)
316 return bfd_reloc_overflow
;
318 x
= bfd_get_16 (input_bfd
, contents
);
319 x
= (x
& 0xfc00) | (srel
& 0x3ff);
320 bfd_put_16 (input_bfd
, x
, contents
);
321 /* Handle second jump instruction. */
322 x
= bfd_get_16 (input_bfd
, contents
- 2);
324 x
= (x
& 0xfc00) | (srel
& 0x3ff);
325 bfd_put_16 (input_bfd
, x
, contents
- 2);
328 case R_MSP430_16_PCREL
:
329 case R_MSP430_RL_PCREL
:
330 contents
+= rel
->r_offset
;
331 srel
= (bfd_signed_vma
) relocation
;
332 srel
+= rel
->r_addend
;
333 srel
-= rel
->r_offset
;
334 /* Only branch instructions add 2 to the PC... */
335 srel
-= (input_section
->output_section
->vma
+
336 input_section
->output_offset
);
339 return bfd_reloc_outofrange
;
341 bfd_put_16 (input_bfd
, srel
& 0xffff, contents
);
344 case R_MSP430_16_PCREL_BYTE
:
345 contents
+= rel
->r_offset
;
346 srel
= (bfd_signed_vma
) relocation
;
347 srel
+= rel
->r_addend
;
348 srel
-= rel
->r_offset
;
349 /* Only branch instructions add 2 to the PC... */
350 srel
-= (input_section
->output_section
->vma
+
351 input_section
->output_offset
);
353 bfd_put_16 (input_bfd
, srel
& 0xffff, contents
);
356 case R_MSP430_16_BYTE
:
357 contents
+= rel
->r_offset
;
358 srel
= (bfd_signed_vma
) relocation
;
359 srel
+= rel
->r_addend
;
360 bfd_put_16 (input_bfd
, srel
& 0xffff, contents
);
364 contents
+= rel
->r_offset
;
365 srel
= (bfd_signed_vma
) relocation
;
366 srel
+= rel
->r_addend
;
369 return bfd_reloc_notsupported
;
371 bfd_put_16 (input_bfd
, srel
& 0xffff, contents
);
375 r
= _bfd_final_link_relocate (howto
, input_bfd
, input_section
,
376 contents
, rel
->r_offset
,
377 relocation
, rel
->r_addend
);
383 /* Relocate an MSP430 ELF section. */
386 elf32_msp430_relocate_section (bfd
* output_bfd ATTRIBUTE_UNUSED
,
387 struct bfd_link_info
* info
,
389 asection
* input_section
,
391 Elf_Internal_Rela
* relocs
,
392 Elf_Internal_Sym
* local_syms
,
393 asection
** local_sections
)
395 Elf_Internal_Shdr
*symtab_hdr
;
396 struct elf_link_hash_entry
**sym_hashes
;
397 Elf_Internal_Rela
*rel
;
398 Elf_Internal_Rela
*relend
;
400 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
401 sym_hashes
= elf_sym_hashes (input_bfd
);
402 relend
= relocs
+ input_section
->reloc_count
;
404 for (rel
= relocs
; rel
< relend
; rel
++)
406 reloc_howto_type
*howto
;
407 unsigned long r_symndx
;
408 Elf_Internal_Sym
*sym
;
410 struct elf_link_hash_entry
*h
;
412 bfd_reloc_status_type r
;
413 const char *name
= NULL
;
416 /* This is a final link. */
418 r_type
= ELF32_R_TYPE (rel
->r_info
);
419 r_symndx
= ELF32_R_SYM (rel
->r_info
);
420 howto
= elf_msp430_howto_table
+ ELF32_R_TYPE (rel
->r_info
);
425 if (r_symndx
< symtab_hdr
->sh_info
)
427 sym
= local_syms
+ r_symndx
;
428 sec
= local_sections
[r_symndx
];
429 relocation
= _bfd_elf_rela_local_sym (output_bfd
, sym
, &sec
, rel
);
431 name
= bfd_elf_string_from_elf_section
432 (input_bfd
, symtab_hdr
->sh_link
, sym
->st_name
);
433 name
= (name
== NULL
) ? bfd_section_name (input_bfd
, sec
) : name
;
437 bfd_boolean unresolved_reloc
, warned
;
439 RELOC_FOR_GLOBAL_SYMBOL (info
, input_bfd
, input_section
, rel
,
440 r_symndx
, symtab_hdr
, sym_hashes
,
442 unresolved_reloc
, warned
);
445 r
= msp430_final_link_relocate (howto
, input_bfd
, input_section
,
446 contents
, rel
, relocation
);
448 if (r
!= bfd_reloc_ok
)
450 const char *msg
= (const char *) NULL
;
454 case bfd_reloc_overflow
:
455 r
= info
->callbacks
->reloc_overflow
456 (info
, (h
? &h
->root
: NULL
), name
, howto
->name
,
457 (bfd_vma
) 0, input_bfd
, input_section
,
461 case bfd_reloc_undefined
:
462 r
= info
->callbacks
->undefined_symbol
463 (info
, name
, input_bfd
, input_section
, rel
->r_offset
, TRUE
);
466 case bfd_reloc_outofrange
:
467 msg
= _("internal error: out of range error");
470 case bfd_reloc_notsupported
:
471 msg
= _("internal error: unsupported relocation error");
474 case bfd_reloc_dangerous
:
475 msg
= _("internal error: dangerous relocation");
479 msg
= _("internal error: unknown error");
484 r
= info
->callbacks
->warning
485 (info
, msg
, name
, input_bfd
, input_section
, rel
->r_offset
);
496 /* The final processing done just before writing out a MSP430 ELF object
497 file. This gets the MSP430 architecture right based on the machine
501 bfd_elf_msp430_final_write_processing (bfd
* abfd
,
502 bfd_boolean linker ATTRIBUTE_UNUSED
)
506 switch (bfd_get_mach (abfd
))
509 case bfd_mach_msp110
:
510 val
= E_MSP430_MACH_MSP430x11x1
;
514 val
= E_MSP430_MACH_MSP430x11
;
518 val
= E_MSP430_MACH_MSP430x12
;
522 val
= E_MSP430_MACH_MSP430x13
;
526 val
= E_MSP430_MACH_MSP430x14
;
530 val
= E_MSP430_MACH_MSP430x15
;
534 val
= E_MSP430_MACH_MSP430x16
;
538 val
= E_MSP430_MACH_MSP430x31
;
542 val
= E_MSP430_MACH_MSP430x32
;
546 val
= E_MSP430_MACH_MSP430x33
;
550 val
= E_MSP430_MACH_MSP430x41
;
554 val
= E_MSP430_MACH_MSP430x42
;
558 val
= E_MSP430_MACH_MSP430x43
;
562 val
= E_MSP430_MACH_MSP430x44
;
566 elf_elfheader (abfd
)->e_machine
= EM_MSP430
;
567 elf_elfheader (abfd
)->e_flags
&= ~EF_MSP430_MACH
;
568 elf_elfheader (abfd
)->e_flags
|= val
;
571 /* Set the right machine number. */
574 elf32_msp430_object_p (bfd
* abfd
)
576 int e_set
= bfd_mach_msp14
;
578 if (elf_elfheader (abfd
)->e_machine
== EM_MSP430
579 || elf_elfheader (abfd
)->e_machine
== EM_MSP430_OLD
)
581 int e_mach
= elf_elfheader (abfd
)->e_flags
& EF_MSP430_MACH
;
586 case E_MSP430_MACH_MSP430x11
:
587 e_set
= bfd_mach_msp11
;
590 case E_MSP430_MACH_MSP430x11x1
:
591 e_set
= bfd_mach_msp110
;
594 case E_MSP430_MACH_MSP430x12
:
595 e_set
= bfd_mach_msp12
;
598 case E_MSP430_MACH_MSP430x13
:
599 e_set
= bfd_mach_msp13
;
602 case E_MSP430_MACH_MSP430x14
:
603 e_set
= bfd_mach_msp14
;
606 case E_MSP430_MACH_MSP430x15
:
607 e_set
= bfd_mach_msp15
;
610 case E_MSP430_MACH_MSP430x16
:
611 e_set
= bfd_mach_msp16
;
614 case E_MSP430_MACH_MSP430x31
:
615 e_set
= bfd_mach_msp31
;
618 case E_MSP430_MACH_MSP430x32
:
619 e_set
= bfd_mach_msp32
;
622 case E_MSP430_MACH_MSP430x33
:
623 e_set
= bfd_mach_msp33
;
626 case E_MSP430_MACH_MSP430x41
:
627 e_set
= bfd_mach_msp41
;
630 case E_MSP430_MACH_MSP430x42
:
631 e_set
= bfd_mach_msp42
;
634 case E_MSP430_MACH_MSP430x43
:
635 e_set
= bfd_mach_msp43
;
638 case E_MSP430_MACH_MSP430x44
:
639 e_set
= bfd_mach_msp44
;
644 return bfd_default_set_arch_mach (abfd
, bfd_arch_msp430
, e_set
);
648 elf32_msp430_post_process_headers (bfd
* abfd
,
649 struct bfd_link_info
* link_info ATTRIBUTE_UNUSED
)
651 Elf_Internal_Ehdr
* i_ehdrp
; /* ELF file header, internal form. */
653 i_ehdrp
= elf_elfheader (abfd
);
655 #ifndef ELFOSABI_STANDALONE
656 #define ELFOSABI_STANDALONE 255
659 i_ehdrp
->e_ident
[EI_OSABI
] = ELFOSABI_STANDALONE
;
662 /* These functions handle relaxing for the msp430.
663 Relaxation required only in two cases:
664 - Bad hand coding like jumps from one section to another or
666 - Sibling calls. This will affect onlu 'jump label' polymorph. Without
667 relaxing this enlarges code by 2 bytes. Sibcalls implemented but
668 do not work in gcc's port by the reason I do not know.
669 Anyway, if a relaxation required, user should pass -relax option to the
672 There are quite a few relaxing opportunities available on the msp430:
674 ================================================================
678 eq == jeq label jne +4; br lab
679 ne != jne label jeq +4; br lab
680 lt < jl label jge +4; br lab
681 ltu < jlo label lhs +4; br lab
682 ge >= jge label jl +4; br lab
683 geu >= jhs label jlo +4; br lab
687 ltn < jn jn +2; jmp +4; br lab
689 3. 4 words -> 2 words
691 gt > jeq +2; jge label jeq +6; jl +4; br label
692 gtu > jeq +2; jhs label jeq +6; jlo +4; br label
694 4. 4 words -> 2 words and 2 labels
696 leu <= jeq label; jlo label jeq +2; jhs +4; br label
697 le <= jeq label; jl label jeq +2; jge +4; br label
698 =================================================================
700 codemap for first cases is (labels masked ):
701 eq: 0x2002,0x4010,0x0000 -> 0x2400
702 ne: 0x2402,0x4010,0x0000 -> 0x2000
703 lt: 0x3402,0x4010,0x0000 -> 0x3800
704 ltu: 0x2c02,0x4010,0x0000 -> 0x2800
705 ge: 0x3802,0x4010,0x0000 -> 0x3400
706 geu: 0x2802,0x4010,0x0000 -> 0x2c00
709 ltn: 0x3001,0x3c02,0x4010,0x0000 -> 0x3000
712 gt: 0x2403,0x3802,0x4010,0x0000 -> 0x2401,0x3400
713 gtu: 0x2403,0x2802,0x4010,0x0000 -> 0x2401,0x2c00
716 leu: 0x2401,0x2c02,0x4010,0x0000 -> 0x2400,0x2800
717 le: 0x2401,0x3402,0x4010,0x0000 -> 0x2400,0x3800
720 jump: 0x4010,0x0000 -> 0x3c00. */
722 #define NUMB_RELAX_CODES 12
723 static struct rcodes_s
725 int f0
, f1
; /* From code. */
726 int t0
, t1
; /* To code. */
727 int labels
; /* Position of labels: 1 - one label at first
728 word, 2 - one at second word, 3 - two
730 int cdx
; /* Words to match. */
731 int bs
; /* Shrink bytes. */
732 int off
; /* Offset from old label for new code. */
733 int ncl
; /* New code length. */
735 {/* lab,cdx,bs,off,ncl */
736 { 0x0000, 0x0000, 0x3c00, 0x0000, 1, 0, 2, 2, 2}, /* jump */
737 { 0x0000, 0x2002, 0x2400, 0x0000, 1, 1, 4, 4, 2}, /* eq */
738 { 0x0000, 0x2402, 0x2000, 0x0000, 1, 1, 4, 4, 2}, /* ne */
739 { 0x0000, 0x3402, 0x3800, 0x0000, 1, 1, 4, 4, 2}, /* lt */
740 { 0x0000, 0x2c02, 0x2800, 0x0000, 1, 1, 4, 4, 2}, /* ltu */
741 { 0x0000, 0x3802, 0x3400, 0x0000, 1, 1, 4, 4, 2}, /* ge */
742 { 0x0000, 0x2802, 0x2c00, 0x0000, 1, 1, 4, 4, 2}, /* geu */
743 { 0x3001, 0x3c02, 0x3000, 0x0000, 1, 2, 6, 6, 2}, /* ltn */
744 { 0x2403, 0x3802, 0x2401, 0x3400, 2, 2, 4, 6, 4}, /* gt */
745 { 0x2403, 0x2802, 0x2401, 0x2c00, 2, 2, 4, 6, 4}, /* gtu */
746 { 0x2401, 0x2c02, 0x2400, 0x2800, 3, 2, 4, 6, 4}, /* leu , 2 labels */
747 { 0x2401, 0x2c02, 0x2400, 0x2800, 3, 2, 4, 6, 4}, /* le , 2 labels */
748 { 0, 0, 0, 0, 0, 0, 0, 0, 0}
751 /* Return TRUE if a symbol exists at the given address. */
754 msp430_elf_symbol_address_p (bfd
* abfd
,
756 Elf_Internal_Sym
* isym
,
759 Elf_Internal_Shdr
*symtab_hdr
;
760 unsigned int sec_shndx
;
761 Elf_Internal_Sym
*isymend
;
762 struct elf_link_hash_entry
**sym_hashes
;
763 struct elf_link_hash_entry
**end_hashes
;
764 unsigned int symcount
;
766 sec_shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec
);
768 /* Examine all the local symbols. */
769 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
770 for (isymend
= isym
+ symtab_hdr
->sh_info
; isym
< isymend
; isym
++)
771 if (isym
->st_shndx
== sec_shndx
&& isym
->st_value
== addr
)
774 symcount
= (symtab_hdr
->sh_size
/ sizeof (Elf32_External_Sym
)
775 - symtab_hdr
->sh_info
);
776 sym_hashes
= elf_sym_hashes (abfd
);
777 end_hashes
= sym_hashes
+ symcount
;
778 for (; sym_hashes
< end_hashes
; sym_hashes
++)
780 struct elf_link_hash_entry
*sym_hash
= *sym_hashes
;
782 if ((sym_hash
->root
.type
== bfd_link_hash_defined
783 || sym_hash
->root
.type
== bfd_link_hash_defweak
)
784 && sym_hash
->root
.u
.def
.section
== sec
785 && sym_hash
->root
.u
.def
.value
== addr
)
792 /* Adjust all local symbols defined as '.section + 0xXXXX' (.section has sec_shndx)
793 referenced from current and other sections */
795 msp430_elf_relax_adjust_locals(bfd
* abfd
, asection
* sec
, bfd_vma addr
,
796 int count
, unsigned int sec_shndx
, bfd_vma toaddr
)
798 Elf_Internal_Shdr
*symtab_hdr
;
799 Elf_Internal_Rela
*irel
;
800 Elf_Internal_Rela
*irelend
;
801 Elf_Internal_Sym
*isym
;
803 irel
= elf_section_data (sec
)->relocs
;
804 irelend
= irel
+ sec
->reloc_count
;
805 symtab_hdr
= & elf_tdata (abfd
)->symtab_hdr
;
806 isym
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
808 for (irel
= elf_section_data (sec
)->relocs
; irel
< irelend
; irel
++)
810 int sidx
= ELF32_R_SYM(irel
->r_info
);
811 Elf_Internal_Sym
*lsym
= isym
+ sidx
;
813 /* Adjust symbols referenced by .sec+0xXX */
814 if (irel
->r_addend
> addr
&& irel
->r_addend
< toaddr
815 && lsym
->st_shndx
== sec_shndx
)
816 irel
->r_addend
-= count
;
822 /* Delete some bytes from a section while relaxing. */
825 msp430_elf_relax_delete_bytes (bfd
* abfd
, asection
* sec
, bfd_vma addr
,
828 Elf_Internal_Shdr
*symtab_hdr
;
829 unsigned int sec_shndx
;
831 Elf_Internal_Rela
*irel
;
832 Elf_Internal_Rela
*irelend
;
833 Elf_Internal_Rela
*irelalign
;
835 Elf_Internal_Sym
*isym
;
836 Elf_Internal_Sym
*isymend
;
837 struct elf_link_hash_entry
**sym_hashes
;
838 struct elf_link_hash_entry
**end_hashes
;
839 unsigned int symcount
;
842 sec_shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec
);
844 contents
= elf_section_data (sec
)->this_hdr
.contents
;
846 /* The deletion must stop at the next ALIGN reloc for an aligment
847 power larger than the number of bytes we are deleting. */
852 irel
= elf_section_data (sec
)->relocs
;
853 irelend
= irel
+ sec
->reloc_count
;
855 /* Actually delete the bytes. */
856 memmove (contents
+ addr
, contents
+ addr
+ count
,
857 (size_t) (toaddr
- addr
- count
));
860 /* Adjust all the relocs. */
861 symtab_hdr
= & elf_tdata (abfd
)->symtab_hdr
;
862 isym
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
863 for (irel
= elf_section_data (sec
)->relocs
; irel
< irelend
; irel
++)
865 /* Get the new reloc address. */
866 if ((irel
->r_offset
> addr
&& irel
->r_offset
< toaddr
))
867 irel
->r_offset
-= count
;
870 for (p
= abfd
->sections
; p
!= NULL
; p
= p
->next
)
871 msp430_elf_relax_adjust_locals(abfd
,p
,addr
,count
,sec_shndx
,toaddr
);
873 /* Adjust the local symbols defined in this section. */
874 symtab_hdr
= & elf_tdata (abfd
)->symtab_hdr
;
875 isym
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
876 for (isymend
= isym
+ symtab_hdr
->sh_info
; isym
< isymend
; isym
++)
877 if (isym
->st_shndx
== sec_shndx
878 && isym
->st_value
> addr
&& isym
->st_value
< toaddr
)
879 isym
->st_value
-= count
;
881 /* Now adjust the global symbols defined in this section. */
882 symcount
= (symtab_hdr
->sh_size
/ sizeof (Elf32_External_Sym
)
883 - symtab_hdr
->sh_info
);
884 sym_hashes
= elf_sym_hashes (abfd
);
885 end_hashes
= sym_hashes
+ symcount
;
886 for (; sym_hashes
< end_hashes
; sym_hashes
++)
888 struct elf_link_hash_entry
*sym_hash
= *sym_hashes
;
890 if ((sym_hash
->root
.type
== bfd_link_hash_defined
891 || sym_hash
->root
.type
== bfd_link_hash_defweak
)
892 && sym_hash
->root
.u
.def
.section
== sec
893 && sym_hash
->root
.u
.def
.value
> addr
894 && sym_hash
->root
.u
.def
.value
< toaddr
)
895 sym_hash
->root
.u
.def
.value
-= count
;
903 msp430_elf_relax_section (bfd
* abfd
, asection
* sec
,
904 struct bfd_link_info
* link_info
,
907 Elf_Internal_Shdr
* symtab_hdr
;
908 Elf_Internal_Rela
* internal_relocs
;
909 Elf_Internal_Rela
* irel
;
910 Elf_Internal_Rela
* irelend
;
911 bfd_byte
* contents
= NULL
;
912 Elf_Internal_Sym
* isymbuf
= NULL
;
914 /* Assume nothing changes. */
917 /* We don't have to do anything for a relocatable link, if
918 this section does not have relocs, or if this is not a
920 if (link_info
->relocatable
921 || (sec
->flags
& SEC_RELOC
) == 0
922 || sec
->reloc_count
== 0 || (sec
->flags
& SEC_CODE
) == 0)
925 symtab_hdr
= & elf_tdata (abfd
)->symtab_hdr
;
927 /* Get a copy of the native relocations. */
929 _bfd_elf_link_read_relocs (abfd
, sec
, NULL
, NULL
, link_info
->keep_memory
);
930 if (internal_relocs
== NULL
)
933 /* Walk through them looking for relaxing opportunities. */
934 irelend
= internal_relocs
+ sec
->reloc_count
;
935 for (irel
= internal_relocs
; irel
< irelend
; irel
++)
939 /* If this isn't something that can be relaxed, then ignore
941 if (ELF32_R_TYPE (irel
->r_info
) != (int) R_MSP430_RL_PCREL
)
944 /* Get the section contents if we haven't done so already. */
945 if (contents
== NULL
)
947 /* Get cached copy if it exists. */
948 if (elf_section_data (sec
)->this_hdr
.contents
!= NULL
)
949 contents
= elf_section_data (sec
)->this_hdr
.contents
;
950 else if (! bfd_malloc_and_get_section (abfd
, sec
, &contents
))
954 /* Read this BFD's local symbols if we haven't done so already. */
955 if (isymbuf
== NULL
&& symtab_hdr
->sh_info
!= 0)
957 isymbuf
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
959 isymbuf
= bfd_elf_get_elf_syms (abfd
, symtab_hdr
,
960 symtab_hdr
->sh_info
, 0,
966 /* Get the value of the symbol referred to by the reloc. */
967 if (ELF32_R_SYM (irel
->r_info
) < symtab_hdr
->sh_info
)
969 /* A local symbol. */
970 Elf_Internal_Sym
*isym
;
973 isym
= isymbuf
+ ELF32_R_SYM (irel
->r_info
);
974 if (isym
->st_shndx
== SHN_UNDEF
)
975 sym_sec
= bfd_und_section_ptr
;
976 else if (isym
->st_shndx
== SHN_ABS
)
977 sym_sec
= bfd_abs_section_ptr
;
978 else if (isym
->st_shndx
== SHN_COMMON
)
979 sym_sec
= bfd_com_section_ptr
;
981 sym_sec
= bfd_section_from_elf_index (abfd
, isym
->st_shndx
);
982 symval
= (isym
->st_value
983 + sym_sec
->output_section
->vma
+ sym_sec
->output_offset
);
988 struct elf_link_hash_entry
*h
;
990 /* An external symbol. */
991 indx
= ELF32_R_SYM (irel
->r_info
) - symtab_hdr
->sh_info
;
992 h
= elf_sym_hashes (abfd
)[indx
];
993 BFD_ASSERT (h
!= NULL
);
995 if (h
->root
.type
!= bfd_link_hash_defined
996 && h
->root
.type
!= bfd_link_hash_defweak
)
997 /* This appears to be a reference to an undefined
998 symbol. Just ignore it--it will be caught by the
999 regular reloc processing. */
1002 symval
= (h
->root
.u
.def
.value
1003 + h
->root
.u
.def
.section
->output_section
->vma
1004 + h
->root
.u
.def
.section
->output_offset
);
1007 /* For simplicity of coding, we are going to modify the section
1008 contents, the section relocs, and the BFD symbol table. We
1009 must tell the rest of the code not to free up this
1010 information. It would be possible to instead create a table
1011 of changes which have to be made, as is done in coff-mips.c;
1012 that would be more work, but would require less memory when
1013 the linker is run. */
1015 /* Try to turn a 16bit pc-relative branch into a 10bit pc-relative
1017 /* Paranoia? paranoia... */
1018 if (ELF32_R_TYPE (irel
->r_info
) == (int) R_MSP430_RL_PCREL
)
1020 bfd_vma value
= symval
;
1022 /* Deal with pc-relative gunk. */
1023 value
-= (sec
->output_section
->vma
+ sec
->output_offset
);
1024 value
-= irel
->r_offset
;
1025 value
+= irel
->r_addend
;
1027 /* See if the value will fit in 10 bits, note the high value is
1028 1016 as the target will be two bytes closer if we are
1030 if ((long) value
< 1016 && (long) value
> -1016)
1032 int code0
= 0, code1
= 0, code2
= 0;
1034 struct rcodes_s
*rx
;
1036 /* Get the opcode. */
1037 if (irel
->r_offset
>= 6)
1038 code0
= bfd_get_16 (abfd
, contents
+ irel
->r_offset
- 6);
1040 if (irel
->r_offset
>= 4)
1041 code1
= bfd_get_16 (abfd
, contents
+ irel
->r_offset
- 4);
1043 code2
= bfd_get_16 (abfd
, contents
+ irel
->r_offset
- 2);
1045 if (code2
!= 0x4010)
1048 /* Check r4 and r3. */
1049 for (i
= NUMB_RELAX_CODES
- 1; i
>= 0; i
--)
1052 if (rx
->cdx
== 2 && rx
->f0
== code0
&& rx
->f1
== code1
)
1054 else if (rx
->cdx
== 1 && rx
->f1
== code1
)
1056 else if (rx
->cdx
== 0) /* This is an unconditional jump. */
1061 .Label0: ; we do not care about this label
1063 .Label1: ; make sure there is no label here
1065 .Label2: ; make sure there is no label here
1068 So, if there is .Label1 or .Label2 we cannot relax this code.
1069 This actually should not happen, cause for relaxable
1070 instructions we use RL_PCREL reloc instead of 16_PCREL.
1071 Will change this in the future. */
1074 && msp430_elf_symbol_address_p (abfd
, sec
, isymbuf
,
1075 irel
->r_offset
- 2))
1078 && msp430_elf_symbol_address_p (abfd
, sec
, isymbuf
,
1079 irel
->r_offset
- 4))
1082 /* Note that we've changed the relocs, section contents, etc. */
1083 elf_section_data (sec
)->relocs
= internal_relocs
;
1084 elf_section_data (sec
)->this_hdr
.contents
= contents
;
1085 symtab_hdr
->contents
= (unsigned char *) isymbuf
;
1087 /* Fix the relocation's type. */
1088 if (rx
->labels
== 3) /* Handle special cases. */
1089 irel
->r_info
= ELF32_R_INFO (ELF32_R_SYM (irel
->r_info
),
1092 irel
->r_info
= ELF32_R_INFO (ELF32_R_SYM (irel
->r_info
),
1095 /* Fix the opcode right way. */
1096 bfd_put_16 (abfd
, rx
->t0
, contents
+ irel
->r_offset
- rx
->off
);
1098 bfd_put_16 (abfd
, rx
->t1
,
1099 contents
+ irel
->r_offset
- rx
->off
+ 2);
1102 if (!msp430_elf_relax_delete_bytes (abfd
, sec
,
1103 irel
->r_offset
- rx
->off
+
1107 /* Handle unconditional jumps. */
1109 irel
->r_offset
-= 2;
1111 /* That will change things, so, we should relax again.
1112 Note that this is not required, and it may be slow. */
1118 if (isymbuf
!= NULL
&& symtab_hdr
->contents
!= (unsigned char *) isymbuf
)
1120 if (!link_info
->keep_memory
)
1124 /* Cache the symbols for elf_link_input_bfd. */
1125 symtab_hdr
->contents
= (unsigned char *) isymbuf
;
1129 if (contents
!= NULL
1130 && elf_section_data (sec
)->this_hdr
.contents
!= contents
)
1132 if (!link_info
->keep_memory
)
1136 /* Cache the section contents for elf_link_input_bfd. */
1137 elf_section_data (sec
)->this_hdr
.contents
= contents
;
1141 if (internal_relocs
!= NULL
1142 && elf_section_data (sec
)->relocs
!= internal_relocs
)
1143 free (internal_relocs
);
1148 if (isymbuf
!= NULL
&& symtab_hdr
->contents
!= (unsigned char *) isymbuf
)
1150 if (contents
!= NULL
1151 && elf_section_data (sec
)->this_hdr
.contents
!= contents
)
1153 if (internal_relocs
!= NULL
1154 && elf_section_data (sec
)->relocs
!= internal_relocs
)
1155 free (internal_relocs
);
1161 #define ELF_ARCH bfd_arch_msp430
1162 #define ELF_MACHINE_CODE EM_MSP430
1163 #define ELF_MACHINE_ALT1 EM_MSP430_OLD
1164 #define ELF_MAXPAGESIZE 1
1166 #define TARGET_LITTLE_SYM bfd_elf32_msp430_vec
1167 #define TARGET_LITTLE_NAME "elf32-msp430"
1169 #define elf_info_to_howto msp430_info_to_howto_rela
1170 #define elf_info_to_howto_rel NULL
1171 #define elf_backend_relocate_section elf32_msp430_relocate_section
1172 #define elf_backend_check_relocs elf32_msp430_check_relocs
1173 #define elf_backend_can_gc_sections 1
1174 #define elf_backend_final_write_processing bfd_elf_msp430_final_write_processing
1175 #define elf_backend_object_p elf32_msp430_object_p
1176 #define elf_backend_post_process_headers elf32_msp430_post_process_headers
1177 #define bfd_elf32_bfd_relax_section msp430_elf_relax_section
1179 #include "elf32-target.h"