1 /* V850-specific support for 32-bit ELF
2 Copyright 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003
3 Free Software Foundation, Inc.
5 This file is part of BFD, the Binary File Descriptor library.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
21 /* XXX FIXME: This code is littered with 32bit int, 16bit short, 8bit char
22 dependencies. As is the gas & simulator code for the v850. */
30 #include "libiberty.h"
32 /* Sign-extend a 24-bit number. */
33 #define SEXT24(x) ((((x) & 0xffffff) ^ 0x800000) - 0x800000)
35 static reloc_howto_type *v850_elf_reloc_type_lookup
36 PARAMS ((bfd *abfd, bfd_reloc_code_real_type code));
37 static void v850_elf_info_to_howto_rel
38 PARAMS ((bfd *, arelent *, Elf_Internal_Rela *));
39 static void v850_elf_info_to_howto_rela
40 PARAMS ((bfd *, arelent *, Elf_Internal_Rela *));
41 static bfd_reloc_status_type v850_elf_reloc
42 PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
43 static bfd_boolean v850_elf_is_local_label_name
44 PARAMS ((bfd *, const char *));
45 static bfd_boolean v850_elf_relocate_section
46 PARAMS((bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
47 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **));
48 static bfd_reloc_status_type v850_elf_perform_relocation
49 PARAMS ((bfd *, unsigned int, bfd_vma, bfd_byte *));
50 static bfd_boolean v850_elf_check_relocs
51 PARAMS ((bfd *, struct bfd_link_info *, asection *, const Elf_Internal_Rela *));
52 static void remember_hi16s_reloc
53 PARAMS ((bfd *, bfd_vma, bfd_byte *));
54 static bfd_byte * find_remembered_hi16s_reloc
55 PARAMS ((bfd_vma, bfd_boolean *));
56 static bfd_reloc_status_type v850_elf_final_link_relocate
57 PARAMS ((reloc_howto_type *, bfd *, bfd *, asection *, bfd_byte *, bfd_vma,
58 bfd_vma, bfd_vma, struct bfd_link_info *, asection *, int));
59 static bfd_boolean v850_elf_object_p
61 static bfd_boolean v850_elf_fake_sections
62 PARAMS ((bfd *, Elf_Internal_Shdr *, asection *));
63 static void v850_elf_final_write_processing
64 PARAMS ((bfd *, bfd_boolean));
65 static bfd_boolean v850_elf_set_private_flags
66 PARAMS ((bfd *, flagword));
67 static bfd_boolean v850_elf_merge_private_bfd_data
68 PARAMS ((bfd *, bfd *));
69 static bfd_boolean v850_elf_print_private_bfd_data
70 PARAMS ((bfd *, PTR));
71 static bfd_boolean v850_elf_section_from_bfd_section
72 PARAMS ((bfd *, asection *, int *));
73 static void v850_elf_symbol_processing
74 PARAMS ((bfd *, asymbol *));
75 static bfd_boolean v850_elf_add_symbol_hook
76 PARAMS ((bfd *, struct bfd_link_info *, const Elf_Internal_Sym *,
77 const char **, flagword *, asection **, bfd_vma *));
78 static bfd_boolean v850_elf_link_output_symbol_hook
79 PARAMS ((bfd *, struct bfd_link_info *, const char *,
80 Elf_Internal_Sym *, asection *));
81 static bfd_boolean v850_elf_section_from_shdr
82 PARAMS ((bfd *, Elf_Internal_Shdr *, const char *));
83 static bfd_boolean v850_elf_gc_sweep_hook
84 PARAMS ((bfd *, struct bfd_link_info *, asection *,
85 const Elf_Internal_Rela *));
86 static asection * v850_elf_gc_mark_hook
87 PARAMS ((asection *, struct bfd_link_info *,
88 Elf_Internal_Rela *, struct elf_link_hash_entry *,
90 static bfd_reloc_status_type v850_elf_ignore_reloc
91 PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
92 static bfd_boolean v850_elf_relax_delete_bytes
93 PARAMS ((bfd *, asection *, bfd_vma, bfd_vma, int));
94 static bfd_boolean v850_elf_relax_section
95 PARAMS ((bfd *, asection *, struct bfd_link_info *, bfd_boolean *));
97 /* Note: It is REQUIRED that the 'type' value of each entry
98 in this array match the index of the entry in the array. */
99 static reloc_howto_type v850_elf_howto_table[] =
101 /* This reloc does nothing. */
102 HOWTO (R_V850_NONE, /* type */
104 2, /* size (0 = byte, 1 = short, 2 = long) */
106 FALSE, /* pc_relative */
108 complain_overflow_bitfield, /* complain_on_overflow */
109 bfd_elf_generic_reloc, /* special_function */
110 "R_V850_NONE", /* name */
111 FALSE, /* partial_inplace */
114 FALSE), /* pcrel_offset */
116 /* A PC relative 9 bit branch. */
117 HOWTO (R_V850_9_PCREL, /* type */
119 2, /* size (0 = byte, 1 = short, 2 = long) */
121 TRUE, /* pc_relative */
123 complain_overflow_bitfield, /* complain_on_overflow */
124 v850_elf_reloc, /* special_function */
125 "R_V850_9_PCREL", /* name */
126 FALSE, /* partial_inplace */
127 0x00ffffff, /* src_mask */
128 0x00ffffff, /* dst_mask */
129 TRUE), /* pcrel_offset */
131 /* A PC relative 22 bit branch. */
132 HOWTO (R_V850_22_PCREL, /* type */
134 2, /* size (0 = byte, 1 = short, 2 = long) */
136 TRUE, /* pc_relative */
138 complain_overflow_signed, /* complain_on_overflow */
139 v850_elf_reloc, /* special_function */
140 "R_V850_22_PCREL", /* name */
141 FALSE, /* partial_inplace */
142 0x07ffff80, /* src_mask */
143 0x07ffff80, /* dst_mask */
144 TRUE), /* pcrel_offset */
146 /* High 16 bits of symbol value. */
147 HOWTO (R_V850_HI16_S, /* type */
149 1, /* size (0 = byte, 1 = short, 2 = long) */
151 FALSE, /* pc_relative */
153 complain_overflow_dont, /* complain_on_overflow */
154 v850_elf_reloc, /* special_function */
155 "R_V850_HI16_S", /* name */
156 FALSE, /* partial_inplace */
157 0xffff, /* src_mask */
158 0xffff, /* dst_mask */
159 FALSE), /* pcrel_offset */
161 /* High 16 bits of symbol value. */
162 HOWTO (R_V850_HI16, /* type */
164 1, /* size (0 = byte, 1 = short, 2 = long) */
166 FALSE, /* pc_relative */
168 complain_overflow_dont, /* complain_on_overflow */
169 v850_elf_reloc, /* special_function */
170 "R_V850_HI16", /* name */
171 FALSE, /* partial_inplace */
172 0xffff, /* src_mask */
173 0xffff, /* dst_mask */
174 FALSE), /* pcrel_offset */
176 /* Low 16 bits of symbol value. */
177 HOWTO (R_V850_LO16, /* type */
179 1, /* size (0 = byte, 1 = short, 2 = long) */
181 FALSE, /* pc_relative */
183 complain_overflow_dont, /* complain_on_overflow */
184 v850_elf_reloc, /* special_function */
185 "R_V850_LO16", /* name */
186 FALSE, /* partial_inplace */
187 0xffff, /* src_mask */
188 0xffff, /* dst_mask */
189 FALSE), /* pcrel_offset */
191 /* Simple 32bit reloc. */
192 HOWTO (R_V850_ABS32, /* type */
194 2, /* size (0 = byte, 1 = short, 2 = long) */
196 FALSE, /* pc_relative */
198 complain_overflow_dont, /* complain_on_overflow */
199 v850_elf_reloc, /* special_function */
200 "R_V850_ABS32", /* name */
201 FALSE, /* partial_inplace */
202 0xffffffff, /* src_mask */
203 0xffffffff, /* dst_mask */
204 FALSE), /* pcrel_offset */
206 /* Simple 16bit reloc. */
207 HOWTO (R_V850_16, /* type */
209 1, /* size (0 = byte, 1 = short, 2 = long) */
211 FALSE, /* pc_relative */
213 complain_overflow_dont, /* complain_on_overflow */
214 bfd_elf_generic_reloc, /* special_function */
215 "R_V850_16", /* name */
216 FALSE, /* partial_inplace */
217 0xffff, /* src_mask */
218 0xffff, /* dst_mask */
219 FALSE), /* pcrel_offset */
221 /* Simple 8bit reloc. */
222 HOWTO (R_V850_8, /* type */
224 0, /* size (0 = byte, 1 = short, 2 = long) */
226 FALSE, /* pc_relative */
228 complain_overflow_dont, /* complain_on_overflow */
229 bfd_elf_generic_reloc, /* special_function */
230 "R_V850_8", /* name */
231 FALSE, /* partial_inplace */
234 FALSE), /* pcrel_offset */
236 /* 16 bit offset from the short data area pointer. */
237 HOWTO (R_V850_SDA_16_16_OFFSET, /* type */
239 1, /* size (0 = byte, 1 = short, 2 = long) */
241 FALSE, /* pc_relative */
243 complain_overflow_dont, /* complain_on_overflow */
244 v850_elf_reloc, /* special_function */
245 "R_V850_SDA_16_16_OFFSET", /* name */
246 FALSE, /* partial_inplace */
247 0xffff, /* src_mask */
248 0xffff, /* dst_mask */
249 FALSE), /* pcrel_offset */
251 /* 15 bit offset from the short data area pointer. */
252 HOWTO (R_V850_SDA_15_16_OFFSET, /* type */
254 1, /* size (0 = byte, 1 = short, 2 = long) */
256 FALSE, /* pc_relative */
258 complain_overflow_dont, /* complain_on_overflow */
259 v850_elf_reloc, /* special_function */
260 "R_V850_SDA_15_16_OFFSET", /* name */
261 FALSE, /* partial_inplace */
262 0xfffe, /* src_mask */
263 0xfffe, /* dst_mask */
264 FALSE), /* pcrel_offset */
266 /* 16 bit offset from the zero data area pointer. */
267 HOWTO (R_V850_ZDA_16_16_OFFSET, /* type */
269 1, /* size (0 = byte, 1 = short, 2 = long) */
271 FALSE, /* pc_relative */
273 complain_overflow_dont, /* complain_on_overflow */
274 v850_elf_reloc, /* special_function */
275 "R_V850_ZDA_16_16_OFFSET", /* name */
276 FALSE, /* partial_inplace */
277 0xffff, /* src_mask */
278 0xffff, /* dst_mask */
279 FALSE), /* pcrel_offset */
281 /* 15 bit offset from the zero data area pointer. */
282 HOWTO (R_V850_ZDA_15_16_OFFSET, /* type */
284 1, /* size (0 = byte, 1 = short, 2 = long) */
286 FALSE, /* pc_relative */
288 complain_overflow_dont, /* complain_on_overflow */
289 v850_elf_reloc, /* special_function */
290 "R_V850_ZDA_15_16_OFFSET", /* name */
291 FALSE, /* partial_inplace */
292 0xfffe, /* src_mask */
293 0xfffe, /* dst_mask */
294 FALSE), /* pcrel_offset */
296 /* 6 bit offset from the tiny data area pointer. */
297 HOWTO (R_V850_TDA_6_8_OFFSET, /* type */
299 1, /* size (0 = byte, 1 = short, 2 = long) */
301 FALSE, /* pc_relative */
303 complain_overflow_dont, /* complain_on_overflow */
304 v850_elf_reloc, /* special_function */
305 "R_V850_TDA_6_8_OFFSET", /* name */
306 FALSE, /* partial_inplace */
309 FALSE), /* pcrel_offset */
311 /* 8 bit offset from the tiny data area pointer. */
312 HOWTO (R_V850_TDA_7_8_OFFSET, /* type */
314 1, /* size (0 = byte, 1 = short, 2 = long) */
316 FALSE, /* pc_relative */
318 complain_overflow_dont, /* complain_on_overflow */
319 v850_elf_reloc, /* special_function */
320 "R_V850_TDA_7_8_OFFSET", /* name */
321 FALSE, /* partial_inplace */
324 FALSE), /* pcrel_offset */
326 /* 7 bit offset from the tiny data area pointer. */
327 HOWTO (R_V850_TDA_7_7_OFFSET, /* type */
329 1, /* size (0 = byte, 1 = short, 2 = long) */
331 FALSE, /* pc_relative */
333 complain_overflow_dont, /* complain_on_overflow */
334 v850_elf_reloc, /* special_function */
335 "R_V850_TDA_7_7_OFFSET", /* name */
336 FALSE, /* partial_inplace */
339 FALSE), /* pcrel_offset */
341 /* 16 bit offset from the tiny data area pointer! */
342 HOWTO (R_V850_TDA_16_16_OFFSET, /* type */
344 1, /* size (0 = byte, 1 = short, 2 = long) */
346 FALSE, /* pc_relative */
348 complain_overflow_dont, /* complain_on_overflow */
349 v850_elf_reloc, /* special_function */
350 "R_V850_TDA_16_16_OFFSET", /* name */
351 FALSE, /* partial_inplace */
352 0xffff, /* src_mask */
353 0xfff, /* dst_mask */
354 FALSE), /* pcrel_offset */
356 /* 5 bit offset from the tiny data area pointer. */
357 HOWTO (R_V850_TDA_4_5_OFFSET, /* type */
359 1, /* size (0 = byte, 1 = short, 2 = long) */
361 FALSE, /* pc_relative */
363 complain_overflow_dont, /* complain_on_overflow */
364 v850_elf_reloc, /* special_function */
365 "R_V850_TDA_4_5_OFFSET", /* name */
366 FALSE, /* partial_inplace */
369 FALSE), /* pcrel_offset */
371 /* 4 bit offset from the tiny data area pointer. */
372 HOWTO (R_V850_TDA_4_4_OFFSET, /* type */
374 1, /* size (0 = byte, 1 = short, 2 = long) */
376 FALSE, /* pc_relative */
378 complain_overflow_dont, /* complain_on_overflow */
379 v850_elf_reloc, /* special_function */
380 "R_V850_TDA_4_4_OFFSET", /* name */
381 FALSE, /* partial_inplace */
384 FALSE), /* pcrel_offset */
386 /* 16 bit offset from the short data area pointer. */
387 HOWTO (R_V850_SDA_16_16_SPLIT_OFFSET, /* type */
389 2, /* size (0 = byte, 1 = short, 2 = long) */
391 FALSE, /* pc_relative */
393 complain_overflow_dont, /* complain_on_overflow */
394 v850_elf_reloc, /* special_function */
395 "R_V850_SDA_16_16_SPLIT_OFFSET",/* name */
396 FALSE, /* partial_inplace */
397 0xfffe0020, /* src_mask */
398 0xfffe0020, /* dst_mask */
399 FALSE), /* pcrel_offset */
401 /* 16 bit offset from the zero data area pointer. */
402 HOWTO (R_V850_ZDA_16_16_SPLIT_OFFSET, /* type */
404 2, /* size (0 = byte, 1 = short, 2 = long) */
406 FALSE, /* pc_relative */
408 complain_overflow_dont, /* complain_on_overflow */
409 v850_elf_reloc, /* special_function */
410 "R_V850_ZDA_16_16_SPLIT_OFFSET",/* name */
411 FALSE, /* partial_inplace */
412 0xfffe0020, /* src_mask */
413 0xfffe0020, /* dst_mask */
414 FALSE), /* pcrel_offset */
416 /* 6 bit offset from the call table base pointer. */
417 HOWTO (R_V850_CALLT_6_7_OFFSET, /* type */
419 1, /* size (0 = byte, 1 = short, 2 = long) */
421 FALSE, /* pc_relative */
423 complain_overflow_dont, /* complain_on_overflow */
424 v850_elf_reloc, /* special_function */
425 "R_V850_CALLT_6_7_OFFSET", /* name */
426 FALSE, /* partial_inplace */
429 FALSE), /* pcrel_offset */
431 /* 16 bit offset from the call table base pointer. */
432 HOWTO (R_V850_CALLT_16_16_OFFSET, /* type */
434 1, /* size (0 = byte, 1 = short, 2 = long) */
436 FALSE, /* pc_relative */
438 complain_overflow_dont, /* complain_on_overflow */
439 v850_elf_reloc, /* special_function */
440 "R_V850_CALLT_16_16_OFFSET", /* name */
441 FALSE, /* partial_inplace */
442 0xffff, /* src_mask */
443 0xffff, /* dst_mask */
444 FALSE), /* pcrel_offset */
446 /* GNU extension to record C++ vtable hierarchy */
447 HOWTO (R_V850_GNU_VTINHERIT, /* type */
449 2, /* size (0 = byte, 1 = short, 2 = long) */
451 FALSE, /* pc_relative */
453 complain_overflow_dont, /* complain_on_overflow */
454 NULL, /* special_function */
455 "R_V850_GNU_VTINHERIT", /* name */
456 FALSE, /* partial_inplace */
459 FALSE), /* pcrel_offset */
461 /* GNU extension to record C++ vtable member usage */
462 HOWTO (R_V850_GNU_VTENTRY, /* type */
464 2, /* size (0 = byte, 1 = short, 2 = long) */
466 FALSE, /* pc_relative */
468 complain_overflow_dont, /* complain_on_overflow */
469 _bfd_elf_rel_vtable_reloc_fn, /* special_function */
470 "R_V850_GNU_VTENTRY", /* name */
471 FALSE, /* partial_inplace */
474 FALSE), /* pcrel_offset */
476 /* Indicates a .longcall pseudo-op. The compiler will generate a .longcall
477 pseudo-op when it finds a function call which can be relaxed. */
478 HOWTO (R_V850_LONGCALL, /* type */
480 2, /* size (0 = byte, 1 = short, 2 = long) */
482 TRUE, /* pc_relative */
484 complain_overflow_signed, /* complain_on_overflow */
485 v850_elf_ignore_reloc, /* special_function */
486 "R_V850_LONGCALL", /* name */
487 FALSE, /* partial_inplace */
490 TRUE), /* pcrel_offset */
492 /* Indicates a .longjump pseudo-op. The compiler will generate a
493 .longjump pseudo-op when it finds a branch which can be relaxed. */
494 HOWTO (R_V850_LONGJUMP, /* type */
496 2, /* size (0 = byte, 1 = short, 2 = long) */
498 TRUE, /* pc_relative */
500 complain_overflow_signed, /* complain_on_overflow */
501 v850_elf_ignore_reloc, /* special_function */
502 "R_V850_LONGJUMP", /* name */
503 FALSE, /* partial_inplace */
506 TRUE), /* pcrel_offset */
508 HOWTO (R_V850_ALIGN, /* type */
510 1, /* size (0 = byte, 1 = short, 2 = long) */
512 FALSE, /* pc_relative */
514 complain_overflow_unsigned, /* complain_on_overflow */
515 v850_elf_ignore_reloc, /* special_function */
516 "R_V850_ALIGN", /* name */
517 FALSE, /* partial_inplace */
520 TRUE), /* pcrel_offset */
522 /* Simple pc-relative 32bit reloc. */
523 HOWTO (R_V850_REL32, /* type */
525 2, /* size (0 = byte, 1 = short, 2 = long) */
527 TRUE, /* pc_relative */
529 complain_overflow_dont, /* complain_on_overflow */
530 v850_elf_reloc, /* special_function */
531 "R_V850_REL32", /* name */
532 FALSE, /* partial_inplace */
533 0xffffffff, /* src_mask */
534 0xffffffff, /* dst_mask */
535 FALSE), /* pcrel_offset */
538 /* Map BFD reloc types to V850 ELF reloc types. */
540 struct v850_elf_reloc_map
542 /* BFD_RELOC_V850_CALLT_16_16_OFFSET is 258, which will not fix in an
544 bfd_reloc_code_real_type bfd_reloc_val;
545 unsigned int elf_reloc_val;
548 static const struct v850_elf_reloc_map v850_elf_reloc_map[] =
550 { BFD_RELOC_NONE, R_V850_NONE },
551 { BFD_RELOC_V850_9_PCREL, R_V850_9_PCREL },
552 { BFD_RELOC_V850_22_PCREL, R_V850_22_PCREL },
553 { BFD_RELOC_HI16_S, R_V850_HI16_S },
554 { BFD_RELOC_HI16, R_V850_HI16 },
555 { BFD_RELOC_LO16, R_V850_LO16 },
556 { BFD_RELOC_32, R_V850_ABS32 },
557 { BFD_RELOC_32_PCREL, R_V850_REL32 },
558 { BFD_RELOC_16, R_V850_16 },
559 { BFD_RELOC_8, R_V850_8 },
560 { BFD_RELOC_V850_SDA_16_16_OFFSET, R_V850_SDA_16_16_OFFSET },
561 { BFD_RELOC_V850_SDA_15_16_OFFSET, R_V850_SDA_15_16_OFFSET },
562 { BFD_RELOC_V850_ZDA_16_16_OFFSET, R_V850_ZDA_16_16_OFFSET },
563 { BFD_RELOC_V850_ZDA_15_16_OFFSET, R_V850_ZDA_15_16_OFFSET },
564 { BFD_RELOC_V850_TDA_6_8_OFFSET, R_V850_TDA_6_8_OFFSET },
565 { BFD_RELOC_V850_TDA_7_8_OFFSET, R_V850_TDA_7_8_OFFSET },
566 { BFD_RELOC_V850_TDA_7_7_OFFSET, R_V850_TDA_7_7_OFFSET },
567 { BFD_RELOC_V850_TDA_16_16_OFFSET, R_V850_TDA_16_16_OFFSET },
568 { BFD_RELOC_V850_TDA_4_5_OFFSET, R_V850_TDA_4_5_OFFSET },
569 { BFD_RELOC_V850_TDA_4_4_OFFSET, R_V850_TDA_4_4_OFFSET },
570 { BFD_RELOC_V850_SDA_16_16_SPLIT_OFFSET, R_V850_SDA_16_16_SPLIT_OFFSET },
571 { BFD_RELOC_V850_ZDA_16_16_SPLIT_OFFSET, R_V850_ZDA_16_16_SPLIT_OFFSET },
572 { BFD_RELOC_V850_CALLT_6_7_OFFSET, R_V850_CALLT_6_7_OFFSET },
573 { BFD_RELOC_V850_CALLT_16_16_OFFSET, R_V850_CALLT_16_16_OFFSET },
574 { BFD_RELOC_VTABLE_INHERIT, R_V850_GNU_VTINHERIT },
575 { BFD_RELOC_VTABLE_ENTRY, R_V850_GNU_VTENTRY },
576 { BFD_RELOC_V850_LONGCALL, R_V850_LONGCALL },
577 { BFD_RELOC_V850_LONGJUMP, R_V850_LONGJUMP },
578 { BFD_RELOC_V850_ALIGN, R_V850_ALIGN },
582 /* Map a bfd relocation into the appropriate howto structure. */
584 static reloc_howto_type *
585 v850_elf_reloc_type_lookup (abfd, code)
586 bfd *abfd ATTRIBUTE_UNUSED;
587 bfd_reloc_code_real_type code;
591 for (i = ARRAY_SIZE (v850_elf_reloc_map); i --;)
592 if (v850_elf_reloc_map[i].bfd_reloc_val == code)
594 unsigned int elf_reloc_val = v850_elf_reloc_map[i].elf_reloc_val;
596 BFD_ASSERT (v850_elf_howto_table[elf_reloc_val].type == elf_reloc_val);
598 return v850_elf_howto_table + elf_reloc_val;
604 /* Set the howto pointer for an V850 ELF reloc. */
607 v850_elf_info_to_howto_rel (abfd, cache_ptr, dst)
608 bfd *abfd ATTRIBUTE_UNUSED;
610 Elf_Internal_Rela *dst;
614 r_type = ELF32_R_TYPE (dst->r_info);
615 BFD_ASSERT (r_type < (unsigned int) R_V850_max);
616 cache_ptr->howto = &v850_elf_howto_table[r_type];
619 /* Set the howto pointer for a V850 ELF reloc (type RELA). */
621 v850_elf_info_to_howto_rela (abfd, cache_ptr, dst)
622 bfd *abfd ATTRIBUTE_UNUSED;
624 Elf_Internal_Rela *dst;
628 r_type = ELF32_R_TYPE (dst->r_info);
629 BFD_ASSERT (r_type < (unsigned int) R_V850_max);
630 cache_ptr->howto = &v850_elf_howto_table[r_type];
633 /* Look through the relocs for a section during the first phase, and
634 allocate space in the global offset table or procedure linkage
638 v850_elf_check_relocs (abfd, info, sec, relocs)
640 struct bfd_link_info *info;
642 const Elf_Internal_Rela *relocs;
644 bfd_boolean ret = TRUE;
646 Elf_Internal_Shdr *symtab_hdr;
647 struct elf_link_hash_entry **sym_hashes;
648 const Elf_Internal_Rela *rel;
649 const Elf_Internal_Rela *rel_end;
651 enum v850_reloc_type r_type;
653 const char *common = (const char *)0;
655 if (info->relocatable)
659 fprintf (stderr, "v850_elf_check_relocs called for section %s in %s\n",
660 bfd_get_section_name (abfd, sec),
661 bfd_archive_filename (abfd));
664 dynobj = elf_hash_table (info)->dynobj;
665 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
666 sym_hashes = elf_sym_hashes (abfd);
669 rel_end = relocs + sec->reloc_count;
670 for (rel = relocs; rel < rel_end; rel++)
672 unsigned long r_symndx;
673 struct elf_link_hash_entry *h;
675 r_symndx = ELF32_R_SYM (rel->r_info);
676 if (r_symndx < symtab_hdr->sh_info)
679 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
681 r_type = (enum v850_reloc_type) ELF32_R_TYPE (rel->r_info);
687 case R_V850_22_PCREL:
695 case R_V850_CALLT_6_7_OFFSET:
696 case R_V850_CALLT_16_16_OFFSET:
699 /* This relocation describes the C++ object vtable hierarchy.
700 Reconstruct it for later use during GC. */
701 case R_V850_GNU_VTINHERIT:
702 if (!_bfd_elf32_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
706 /* This relocation describes which C++ vtable entries
707 are actually used. Record for later use during GC. */
708 case R_V850_GNU_VTENTRY:
709 if (!_bfd_elf32_gc_record_vtentry (abfd, sec, h, rel->r_addend))
713 case R_V850_SDA_16_16_SPLIT_OFFSET:
714 case R_V850_SDA_16_16_OFFSET:
715 case R_V850_SDA_15_16_OFFSET:
716 other = V850_OTHER_SDA;
718 goto small_data_common;
720 case R_V850_ZDA_16_16_SPLIT_OFFSET:
721 case R_V850_ZDA_16_16_OFFSET:
722 case R_V850_ZDA_15_16_OFFSET:
723 other = V850_OTHER_ZDA;
725 goto small_data_common;
727 case R_V850_TDA_4_5_OFFSET:
728 case R_V850_TDA_4_4_OFFSET:
729 case R_V850_TDA_6_8_OFFSET:
730 case R_V850_TDA_7_8_OFFSET:
731 case R_V850_TDA_7_7_OFFSET:
732 case R_V850_TDA_16_16_OFFSET:
733 other = V850_OTHER_TDA;
737 #define V850_OTHER_MASK (V850_OTHER_TDA | V850_OTHER_SDA | V850_OTHER_ZDA)
742 /* Flag which type of relocation was used. */
744 if ((h->other & V850_OTHER_MASK) != (other & V850_OTHER_MASK)
745 && (h->other & V850_OTHER_ERROR) == 0)
748 static char buff[200]; /* XXX */
750 switch (h->other & V850_OTHER_MASK)
753 msg = _("Variable `%s' cannot occupy in multiple small data regions");
755 case V850_OTHER_SDA | V850_OTHER_ZDA | V850_OTHER_TDA:
756 msg = _("Variable `%s' can only be in one of the small, zero, and tiny data regions");
758 case V850_OTHER_SDA | V850_OTHER_ZDA:
759 msg = _("Variable `%s' cannot be in both small and zero data regions simultaneously");
761 case V850_OTHER_SDA | V850_OTHER_TDA:
762 msg = _("Variable `%s' cannot be in both small and tiny data regions simultaneously");
764 case V850_OTHER_ZDA | V850_OTHER_TDA:
765 msg = _("Variable `%s' cannot be in both zero and tiny data regions simultaneously");
769 sprintf (buff, msg, h->root.root.string);
770 info->callbacks->warning (info, buff, h->root.root.string,
771 abfd, h->root.u.def.section,
774 bfd_set_error (bfd_error_bad_value);
775 h->other |= V850_OTHER_ERROR;
780 if (h && h->root.type == bfd_link_hash_common
782 && !strcmp (bfd_get_section_name (abfd, h->root.u.c.p->section), "COMMON"))
786 section = h->root.u.c.p->section = bfd_make_section_old_way (abfd, common);
787 section->flags |= SEC_IS_COMMON;
791 fprintf (stderr, "v850_elf_check_relocs, found %s relocation for %s%s\n",
792 v850_elf_howto_table[ (int)r_type ].name,
793 (h && h->root.root.string) ? h->root.root.string : "<unknown>",
794 (h->root.type == bfd_link_hash_common) ? ", symbol is common" : "");
803 /* In the old version, when an entry was checked out from the table,
804 it was deleted. This produced an error if the entry was needed
805 more than once, as the second attempted retry failed.
807 In the current version, the entry is not deleted, instead we set
808 the field 'found' to TRUE. If a second lookup matches the same
809 entry, then we know that the hi16s reloc has already been updated
810 and does not need to be updated a second time.
812 TODO - TOFIX: If it is possible that we need to restore 2 different
813 addresses from the same table entry, where the first generates an
814 overflow, whilst the second do not, then this code will fail. */
816 typedef struct hi16s_location
820 unsigned long counter;
822 struct hi16s_location *next;
826 static hi16s_location *previous_hi16s;
827 static hi16s_location *free_hi16s;
828 static unsigned long hi16s_counter;
831 remember_hi16s_reloc (abfd, addend, address)
836 hi16s_location * entry = NULL;
837 bfd_size_type amt = sizeof (* free_hi16s);
839 /* Find a free structure. */
840 if (free_hi16s == NULL)
841 free_hi16s = (hi16s_location *) bfd_zalloc (abfd, amt);
844 free_hi16s = free_hi16s->next;
846 entry->addend = addend;
847 entry->address = address;
848 entry->counter = hi16s_counter ++;
849 entry->found = FALSE;
850 entry->next = previous_hi16s;
851 previous_hi16s = entry;
853 /* Cope with wrap around of our counter. */
854 if (hi16s_counter == 0)
856 /* XXX - Assume that all counter entries differ only in their low 16 bits. */
857 for (entry = previous_hi16s; entry != NULL; entry = entry->next)
858 entry->counter &= 0xffff;
860 hi16s_counter = 0x10000;
867 find_remembered_hi16s_reloc (addend, already_found)
869 bfd_boolean *already_found;
871 hi16s_location *match = NULL;
872 hi16s_location *entry;
873 hi16s_location *previous = NULL;
874 hi16s_location *prev;
877 /* Search the table. Record the most recent entry that matches. */
878 for (entry = previous_hi16s; entry; entry = entry->next)
880 if (entry->addend == addend
881 && (match == NULL || match->counter < entry->counter))
893 /* Extract the address. */
894 addr = match->address;
896 /* Remeber if this entry has already been used before. */
898 * already_found = match->found;
900 /* Note that this entry has now been used. */
906 /* FIXME: The code here probably ought to be removed and the code in reloc.c
907 allowed to do its stuff instead. At least for most of the relocs, anwyay. */
909 static bfd_reloc_status_type
910 v850_elf_perform_relocation (abfd, r_type, addend, address)
917 bfd_signed_vma saddend = (bfd_signed_vma) addend;
922 /* fprintf (stderr, "reloc type %d not SUPPORTED\n", r_type ); */
923 return bfd_reloc_notsupported;
927 bfd_put_32 (abfd, addend, address);
930 case R_V850_22_PCREL:
931 if (saddend > 0x1fffff || saddend < -0x200000)
932 return bfd_reloc_overflow;
934 if ((addend % 2) != 0)
935 return bfd_reloc_dangerous;
937 insn = bfd_get_32 (abfd, address);
939 insn |= (((addend & 0xfffe) << 16) | ((addend & 0x3f0000) >> 16));
940 bfd_put_32 (abfd, (bfd_vma) insn, address);
944 if (saddend > 0xff || saddend < -0x100)
945 return bfd_reloc_overflow;
947 if ((addend % 2) != 0)
948 return bfd_reloc_dangerous;
950 insn = bfd_get_16 (abfd, address);
952 insn |= ((addend & 0x1f0) << 7) | ((addend & 0x0e) << 3);
956 addend += (bfd_get_16 (abfd, address) << 16);
957 addend = (addend >> 16);
962 /* Remember where this relocation took place. */
963 remember_hi16s_reloc (abfd, addend, address);
965 addend += (bfd_get_16 (abfd, address) << 16);
966 addend = (addend >> 16) + ((addend & 0x8000) != 0);
968 /* This relocation cannot overflow. */
976 /* Calculate the sum of the value stored in the instruction and the
977 addend and check for overflow from the low 16 bits into the high
978 16 bits. The assembler has already done some of this: If the
979 value stored in the instruction has its 15th bit set, (counting
980 from zero) then the assembler will have added 1 to the value
981 stored in the associated HI16S reloc. So for example, these
984 movhi hi( fred ), r0, r1
985 movea lo( fred ), r1, r1
987 will store 0 in the value fields for the MOVHI and MOVEA instructions
988 and addend will be the address of fred, but for these instructions:
990 movhi hi( fred + 0x123456), r0, r1
991 movea lo( fred + 0x123456), r1, r1
993 the value stored in the MOVHI instruction will be 0x12 and the value
994 stored in the MOVEA instruction will be 0x3456. If however the
997 movhi hi( fred + 0x10ffff), r0, r1
998 movea lo( fred + 0x10ffff), r1, r1
1000 then the value stored in the MOVHI instruction would be 0x11 (not
1001 0x10) and the value stored in the MOVEA instruction would be 0xffff.
1002 Thus (assuming for the moment that the addend is 0), at run time the
1003 MOVHI instruction loads 0x110000 into r1, then the MOVEA instruction
1004 adds 0xffffffff (sign extension!) producing 0x10ffff. Similarly if
1005 the instructions were:
1007 movhi hi( fred - 1), r0, r1
1008 movea lo( fred - 1), r1, r1
1010 then 0 is stored in the MOVHI instruction and -1 is stored in the
1013 Overflow can occur if the addition of the value stored in the
1014 instruction plus the addend sets the 15th bit when before it was clear.
1015 This is because the 15th bit will be sign extended into the high part,
1016 thus reducing its value by one, but since the 15th bit was originally
1017 clear, the assembler will not have added 1 to the previous HI16S reloc
1018 to compensate for this effect. For example:
1020 movhi hi( fred + 0x123456), r0, r1
1021 movea lo( fred + 0x123456), r1, r1
1023 The value stored in HI16S reloc is 0x12, the value stored in the LO16
1024 reloc is 0x3456. If we assume that the address of fred is 0x00007000
1025 then the relocations become:
1027 HI16S: 0x0012 + (0x00007000 >> 16) = 0x12
1028 LO16: 0x3456 + (0x00007000 & 0xffff) = 0xa456
1030 but when the instructions are executed, the MOVEA instruction's value
1031 is signed extended, so the sum becomes:
1036 0x0011a456 but 'fred + 0x123456' = 0x0012a456
1038 Note that if the 15th bit was set in the value stored in the LO16
1039 reloc, then we do not have to do anything:
1041 movhi hi( fred + 0x10ffff), r0, r1
1042 movea lo( fred + 0x10ffff), r1, r1
1044 HI16S: 0x0011 + (0x00007000 >> 16) = 0x11
1045 LO16: 0xffff + (0x00007000 & 0xffff) = 0x6fff
1050 0x00116fff = fred + 0x10ffff = 0x7000 + 0x10ffff
1052 Overflow can also occur if the computation carries into the 16th bit
1053 and it also results in the 15th bit having the same value as the 15th
1054 bit of the original value. What happens is that the HI16S reloc
1055 will have already examined the 15th bit of the original value and
1056 added 1 to the high part if the bit is set. This compensates for the
1057 sign extension of 15th bit of the result of the computation. But now
1058 there is a carry into the 16th bit, and this has not been allowed for.
1060 So, for example if fred is at address 0xf000:
1062 movhi hi( fred + 0xffff), r0, r1 [bit 15 of the offset is set]
1063 movea lo( fred + 0xffff), r1, r1
1065 HI16S: 0x0001 + (0x0000f000 >> 16) = 0x0001
1066 LO16: 0xffff + (0x0000f000 & 0xffff) = 0xefff (carry into bit 16 is lost)
1071 0x0000efff but 'fred + 0xffff' = 0x0001efff
1073 Similarly, if the 15th bit remains clear, but overflow occurs into
1074 the 16th bit then (assuming the address of fred is 0xf000):
1076 movhi hi( fred + 0x7000), r0, r1 [bit 15 of the offset is clear]
1077 movea lo( fred + 0x7000), r1, r1
1079 HI16S: 0x0000 + (0x0000f000 >> 16) = 0x0000
1080 LO16: 0x7000 + (0x0000f000 & 0xffff) = 0x6fff (carry into bit 16 is lost)
1085 0x00006fff but 'fred + 0x7000' = 0x00016fff
1087 Note - there is no need to change anything if a carry occurs, and the
1088 15th bit changes its value from being set to being clear, as the HI16S
1089 reloc will have already added in 1 to the high part for us:
1091 movhi hi( fred + 0xffff), r0, r1 [bit 15 of the offset is set]
1092 movea lo( fred + 0xffff), r1, r1
1094 HI16S: 0x0001 + (0x00007000 >> 16)
1095 LO16: 0xffff + (0x00007000 & 0xffff) = 0x6fff (carry into bit 16 is lost)
1098 + 0x00006fff (bit 15 not set, so the top half is zero)
1100 0x00016fff which is right (assuming that fred is at 0x7000)
1102 but if the 15th bit goes from being clear to being set, then we must
1103 once again handle overflow:
1105 movhi hi( fred + 0x7000), r0, r1 [bit 15 of the offset is clear]
1106 movea lo( fred + 0x7000), r1, r1
1108 HI16S: 0x0000 + (0x0000ffff >> 16)
1109 LO16: 0x7000 + (0x0000ffff & 0xffff) = 0x6fff (carry into bit 16)
1112 + 0x00006fff (bit 15 not set, so the top half is zero)
1114 0x00006fff which is wrong (assuming that fred is at 0xffff). */
1118 insn = bfd_get_16 (abfd, address);
1119 result = insn + addend;
1121 #define BIT15_SET(x) ((x) & 0x8000)
1122 #define OVERFLOWS(a,i) ((((a) & 0xffff) + (i)) > 0xffff)
1124 if ((BIT15_SET (result) && ! BIT15_SET (addend))
1125 || (OVERFLOWS (addend, insn)
1126 && ((! BIT15_SET (insn)) || (BIT15_SET (addend)))))
1128 bfd_boolean already_updated;
1129 bfd_byte *hi16s_address = find_remembered_hi16s_reloc
1130 (addend, & already_updated);
1132 /* Amend the matching HI16_S relocation. */
1133 if (hi16s_address != NULL)
1135 if (! already_updated)
1137 insn = bfd_get_16 (abfd, hi16s_address);
1139 bfd_put_16 (abfd, (bfd_vma) insn, hi16s_address);
1144 fprintf (stderr, _("FAILED to find previous HI16 reloc\n"));
1145 return bfd_reloc_overflow;
1149 /* Do not complain if value has top bit set, as this has been anticipated. */
1150 insn = result & 0xffff;
1155 addend += (char) bfd_get_8 (abfd, address);
1157 saddend = (bfd_signed_vma) addend;
1159 if (saddend > 0x7f || saddend < -0x80)
1160 return bfd_reloc_overflow;
1162 bfd_put_8 (abfd, addend, address);
1163 return bfd_reloc_ok;
1165 case R_V850_CALLT_16_16_OFFSET:
1166 addend += bfd_get_16 (abfd, address);
1168 saddend = (bfd_signed_vma) addend;
1170 if (saddend > 0xffff || saddend < 0)
1171 return bfd_reloc_overflow;
1179 case R_V850_SDA_16_16_OFFSET:
1180 case R_V850_ZDA_16_16_OFFSET:
1181 case R_V850_TDA_16_16_OFFSET:
1182 addend += bfd_get_16 (abfd, address);
1184 saddend = (bfd_signed_vma) addend;
1186 if (saddend > 0x7fff || saddend < -0x8000)
1187 return bfd_reloc_overflow;
1192 case R_V850_SDA_15_16_OFFSET:
1193 case R_V850_ZDA_15_16_OFFSET:
1194 insn = bfd_get_16 (abfd, address);
1195 addend += (insn & 0xfffe);
1197 saddend = (bfd_signed_vma) addend;
1199 if (saddend > 0x7ffe || saddend < -0x8000)
1200 return bfd_reloc_overflow;
1203 return bfd_reloc_dangerous;
1205 insn = (addend &~ (bfd_vma) 1) | (insn & 1);
1208 case R_V850_TDA_6_8_OFFSET:
1209 insn = bfd_get_16 (abfd, address);
1210 addend += ((insn & 0x7e) << 1);
1212 saddend = (bfd_signed_vma) addend;
1214 if (saddend > 0xfc || saddend < 0)
1215 return bfd_reloc_overflow;
1218 return bfd_reloc_dangerous;
1221 insn |= (addend >> 1);
1224 case R_V850_TDA_7_8_OFFSET:
1225 insn = bfd_get_16 (abfd, address);
1226 addend += ((insn & 0x7f) << 1);
1228 saddend = (bfd_signed_vma) addend;
1230 if (saddend > 0xfe || saddend < 0)
1231 return bfd_reloc_overflow;
1234 return bfd_reloc_dangerous;
1237 insn |= (addend >> 1);
1240 case R_V850_TDA_7_7_OFFSET:
1241 insn = bfd_get_16 (abfd, address);
1242 addend += insn & 0x7f;
1244 saddend = (bfd_signed_vma) addend;
1246 if (saddend > 0x7f || saddend < 0)
1247 return bfd_reloc_overflow;
1253 case R_V850_TDA_4_5_OFFSET:
1254 insn = bfd_get_16 (abfd, address);
1255 addend += ((insn & 0xf) << 1);
1257 saddend = (bfd_signed_vma) addend;
1259 if (saddend > 0x1e || saddend < 0)
1260 return bfd_reloc_overflow;
1263 return bfd_reloc_dangerous;
1266 insn |= (addend >> 1);
1269 case R_V850_TDA_4_4_OFFSET:
1270 insn = bfd_get_16 (abfd, address);
1271 addend += insn & 0xf;
1273 saddend = (bfd_signed_vma) addend;
1275 if (saddend > 0xf || saddend < 0)
1276 return bfd_reloc_overflow;
1282 case R_V850_ZDA_16_16_SPLIT_OFFSET:
1283 case R_V850_SDA_16_16_SPLIT_OFFSET:
1284 insn = bfd_get_32 (abfd, address);
1285 addend += ((insn & 0xfffe0000) >> 16) + ((insn & 0x20) >> 5);
1287 saddend = (bfd_signed_vma) addend;
1289 if (saddend > 0x7fff || saddend < -0x8000)
1290 return bfd_reloc_overflow;
1293 insn |= (addend & 1) << 5;
1294 insn |= (addend &~ (bfd_vma) 1) << 16;
1296 bfd_put_32 (abfd, (bfd_vma) insn, address);
1297 return bfd_reloc_ok;
1299 case R_V850_CALLT_6_7_OFFSET:
1300 insn = bfd_get_16 (abfd, address);
1301 addend += ((insn & 0x3f) << 1);
1303 saddend = (bfd_signed_vma) addend;
1305 if (saddend > 0x7e || saddend < 0)
1306 return bfd_reloc_overflow;
1309 return bfd_reloc_dangerous;
1312 insn |= (addend >> 1);
1315 case R_V850_GNU_VTINHERIT:
1316 case R_V850_GNU_VTENTRY:
1317 return bfd_reloc_ok;
1321 bfd_put_16 (abfd, (bfd_vma) insn, address);
1322 return bfd_reloc_ok;
1325 /* Insert the addend into the instruction. */
1327 static bfd_reloc_status_type
1328 v850_elf_reloc (abfd, reloc, symbol, data, isection, obfd, err)
1329 bfd *abfd ATTRIBUTE_UNUSED;
1332 PTR data ATTRIBUTE_UNUSED;
1335 char **err ATTRIBUTE_UNUSED;
1339 /* If there is an output BFD,
1340 and the symbol is not a section name (which is only defined at final link time),
1341 and either we are not putting the addend into the instruction
1342 or the addend is zero, so there is nothing to add into the instruction
1343 then just fixup the address and return. */
1344 if (obfd != (bfd *) NULL
1345 && (symbol->flags & BSF_SECTION_SYM) == 0
1346 && (! reloc->howto->partial_inplace
1347 || reloc->addend == 0))
1349 reloc->address += isection->output_offset;
1350 return bfd_reloc_ok;
1353 /* Catch relocs involving undefined symbols. */
1354 if (bfd_is_und_section (symbol->section)
1355 && (symbol->flags & BSF_WEAK) == 0
1357 return bfd_reloc_undefined;
1359 /* We handle final linking of some relocs ourselves. */
1361 /* Is the address of the relocation really within the section? */
1362 if (reloc->address > isection->_cooked_size)
1363 return bfd_reloc_outofrange;
1365 /* Work out which section the relocation is targetted at and the
1366 initial relocation command value. */
1368 if (reloc->howto->pc_relative)
1369 return bfd_reloc_ok;
1371 /* Get symbol value. (Common symbols are special.) */
1372 if (bfd_is_com_section (symbol->section))
1375 relocation = symbol->value;
1377 /* Convert input-section-relative symbol value to absolute + addend. */
1378 relocation += symbol->section->output_section->vma;
1379 relocation += symbol->section->output_offset;
1380 relocation += reloc->addend;
1382 #if 0 /* Since this reloc is going to be processed later on, we should
1383 not make it pc-relative here. To test this, try assembling and
1384 linking this program:
1392 .section ".foo","ax"
1396 if (reloc->howto->pc_relative)
1398 /* Here the variable relocation holds the final address of the
1399 symbol we are relocating against, plus any addend. */
1400 relocation -= isection->output_section->vma + isection->output_offset;
1402 /* Deal with pcrel_offset. */
1403 relocation -= reloc->address;
1406 reloc->addend = relocation;
1407 return bfd_reloc_ok;
1410 /* This function is used for relocs which are only used
1411 for relaxing, which the linker should otherwise ignore. */
1413 static bfd_reloc_status_type
1414 v850_elf_ignore_reloc (abfd, reloc_entry, symbol, data, input_section,
1415 output_bfd, error_message)
1416 bfd *abfd ATTRIBUTE_UNUSED;
1417 arelent *reloc_entry;
1418 asymbol *symbol ATTRIBUTE_UNUSED;
1419 PTR data ATTRIBUTE_UNUSED;
1420 asection *input_section;
1422 char **error_message ATTRIBUTE_UNUSED;
1424 if (output_bfd != NULL)
1425 reloc_entry->address += input_section->output_offset;
1427 return bfd_reloc_ok;
1431 v850_elf_is_local_label_name (abfd, name)
1432 bfd *abfd ATTRIBUTE_UNUSED;
1435 return ( (name[0] == '.' && (name[1] == 'L' || name[1] == '.'))
1436 || (name[0] == '_' && name[1] == '.' && name[2] == 'L' && name[3] == '_'));
1439 /* We overload some of the bfd_reloc error codes for own purposes. */
1440 #define bfd_reloc_gp_not_found bfd_reloc_other
1441 #define bfd_reloc_ep_not_found bfd_reloc_continue
1442 #define bfd_reloc_ctbp_not_found (bfd_reloc_dangerous + 1)
1444 /* Perform a relocation as part of a final link. */
1446 static bfd_reloc_status_type
1447 v850_elf_final_link_relocate (howto, input_bfd, output_bfd,
1448 input_section, contents, offset, value,
1449 addend, info, sym_sec, is_local)
1450 reloc_howto_type *howto;
1452 bfd *output_bfd ATTRIBUTE_UNUSED;
1453 asection *input_section;
1458 struct bfd_link_info *info;
1460 int is_local ATTRIBUTE_UNUSED;
1462 unsigned int r_type = howto->type;
1463 bfd_byte *hit_data = contents + offset;
1465 /* Adjust the value according to the relocation. */
1468 case R_V850_9_PCREL:
1469 value -= (input_section->output_section->vma
1470 + input_section->output_offset);
1474 case R_V850_22_PCREL:
1475 value -= (input_section->output_section->vma
1476 + input_section->output_offset
1479 /* If the sign extension will corrupt the value then we have overflowed. */
1480 if (((value & 0xff000000) != 0x0) && ((value & 0xff000000) != 0xff000000))
1481 return bfd_reloc_overflow;
1483 /* Only the bottom 24 bits of the PC are valid */
1484 value = SEXT24 (value);
1488 value -= (input_section->output_section->vma
1489 + input_section->output_offset
1501 case R_V850_ZDA_15_16_OFFSET:
1502 case R_V850_ZDA_16_16_OFFSET:
1503 case R_V850_ZDA_16_16_SPLIT_OFFSET:
1504 if (sym_sec == NULL)
1505 return bfd_reloc_undefined;
1507 value -= sym_sec->output_section->vma;
1510 case R_V850_SDA_15_16_OFFSET:
1511 case R_V850_SDA_16_16_OFFSET:
1512 case R_V850_SDA_16_16_SPLIT_OFFSET:
1515 struct bfd_link_hash_entry * h;
1517 if (sym_sec == NULL)
1518 return bfd_reloc_undefined;
1520 /* Get the value of __gp. */
1521 h = bfd_link_hash_lookup (info->hash, "__gp", FALSE, FALSE, TRUE);
1522 if (h == (struct bfd_link_hash_entry *) NULL
1523 || h->type != bfd_link_hash_defined)
1524 return bfd_reloc_gp_not_found;
1526 gp = (h->u.def.value
1527 + h->u.def.section->output_section->vma
1528 + h->u.def.section->output_offset);
1530 value -= sym_sec->output_section->vma;
1531 value -= (gp - sym_sec->output_section->vma);
1535 case R_V850_TDA_4_4_OFFSET:
1536 case R_V850_TDA_4_5_OFFSET:
1537 case R_V850_TDA_16_16_OFFSET:
1538 case R_V850_TDA_7_7_OFFSET:
1539 case R_V850_TDA_7_8_OFFSET:
1540 case R_V850_TDA_6_8_OFFSET:
1543 struct bfd_link_hash_entry * h;
1545 /* Get the value of __ep. */
1546 h = bfd_link_hash_lookup (info->hash, "__ep", FALSE, FALSE, TRUE);
1547 if (h == (struct bfd_link_hash_entry *) NULL
1548 || h->type != bfd_link_hash_defined)
1549 return bfd_reloc_ep_not_found;
1551 ep = (h->u.def.value
1552 + h->u.def.section->output_section->vma
1553 + h->u.def.section->output_offset);
1559 case R_V850_CALLT_6_7_OFFSET:
1562 struct bfd_link_hash_entry * h;
1564 /* Get the value of __ctbp. */
1565 h = bfd_link_hash_lookup (info->hash, "__ctbp", FALSE, FALSE, TRUE);
1566 if (h == (struct bfd_link_hash_entry *) NULL
1567 || h->type != bfd_link_hash_defined)
1568 return bfd_reloc_ctbp_not_found;
1570 ctbp = (h->u.def.value
1571 + h->u.def.section->output_section->vma
1572 + h->u.def.section->output_offset);
1577 case R_V850_CALLT_16_16_OFFSET:
1580 struct bfd_link_hash_entry * h;
1582 if (sym_sec == NULL)
1583 return bfd_reloc_undefined;
1585 /* Get the value of __ctbp. */
1586 h = bfd_link_hash_lookup (info->hash, "__ctbp", FALSE, FALSE, TRUE);
1587 if (h == (struct bfd_link_hash_entry *) NULL
1588 || h->type != bfd_link_hash_defined)
1589 return bfd_reloc_ctbp_not_found;
1591 ctbp = (h->u.def.value
1592 + h->u.def.section->output_section->vma
1593 + h->u.def.section->output_offset);
1595 value -= sym_sec->output_section->vma;
1596 value -= (ctbp - sym_sec->output_section->vma);
1601 case R_V850_GNU_VTINHERIT:
1602 case R_V850_GNU_VTENTRY:
1603 case R_V850_LONGCALL:
1604 case R_V850_LONGJUMP:
1606 return bfd_reloc_ok;
1609 return bfd_reloc_notsupported;
1612 /* Perform the relocation. */
1613 return v850_elf_perform_relocation (input_bfd, r_type, value + addend, hit_data);
1616 /* Relocate an V850 ELF section. */
1619 v850_elf_relocate_section (output_bfd, info, input_bfd, input_section,
1620 contents, relocs, local_syms, local_sections)
1622 struct bfd_link_info *info;
1624 asection *input_section;
1626 Elf_Internal_Rela *relocs;
1627 Elf_Internal_Sym *local_syms;
1628 asection **local_sections;
1630 Elf_Internal_Shdr *symtab_hdr;
1631 struct elf_link_hash_entry **sym_hashes;
1632 Elf_Internal_Rela *rel;
1633 Elf_Internal_Rela *relend;
1635 if (info->relocatable)
1638 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
1639 sym_hashes = elf_sym_hashes (input_bfd);
1641 if (sym_hashes == NULL)
1643 info->callbacks->warning
1644 (info, "no hash table available",
1645 NULL, input_bfd, input_section, (bfd_vma) 0);
1650 /* Reset the list of remembered HI16S relocs to empty. */
1651 free_hi16s = previous_hi16s;
1652 previous_hi16s = NULL;
1656 relend = relocs + input_section->reloc_count;
1657 for (; rel < relend; rel++)
1660 reloc_howto_type *howto;
1661 unsigned long r_symndx;
1662 Elf_Internal_Sym *sym;
1664 struct elf_link_hash_entry *h;
1666 bfd_reloc_status_type r;
1668 r_symndx = ELF32_R_SYM (rel->r_info);
1669 r_type = ELF32_R_TYPE (rel->r_info);
1671 if (r_type == R_V850_GNU_VTENTRY
1672 || r_type == R_V850_GNU_VTINHERIT)
1675 /* This is a final link. */
1676 howto = v850_elf_howto_table + r_type;
1680 if (r_symndx < symtab_hdr->sh_info)
1682 sym = local_syms + r_symndx;
1683 sec = local_sections[r_symndx];
1684 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, sec, rel);
1689 name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link, sym->st_name);
1690 name = (name == NULL) ? "<none>" : name;
1691 fprintf (stderr, "local: sec: %s, sym: %s (%d), value: %x + %x + %x addend %x\n",
1692 sec->name, name, sym->st_name,
1693 sec->output_section->vma, sec->output_offset, sym->st_value, rel->r_addend);
1699 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1701 while (h->root.type == bfd_link_hash_indirect
1702 || h->root.type == bfd_link_hash_warning)
1703 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1705 if (h->root.type == bfd_link_hash_defined
1706 || h->root.type == bfd_link_hash_defweak)
1708 sec = h->root.u.def.section;
1709 relocation = (h->root.u.def.value
1710 + sec->output_section->vma
1711 + sec->output_offset);
1713 fprintf (stderr, "defined: sec: %s, name: %s, value: %x + %x + %x gives: %x\n",
1714 sec->name, h->root.root.string, h->root.u.def.value, sec->output_section->vma, sec->output_offset, relocation);
1717 else if (h->root.type == bfd_link_hash_undefweak)
1720 fprintf (stderr, "undefined: sec: %s, name: %s\n",
1721 sec->name, h->root.root.string);
1727 if (! ((*info->callbacks->undefined_symbol)
1728 (info, h->root.root.string, input_bfd,
1729 input_section, rel->r_offset, TRUE)))
1732 fprintf (stderr, "unknown: name: %s\n", h->root.root.string);
1738 /* FIXME: We should use the addend, but the COFF relocations don't. */
1739 r = v850_elf_final_link_relocate (howto, input_bfd, output_bfd,
1741 contents, rel->r_offset,
1742 relocation, rel->r_addend,
1743 info, sec, h == NULL);
1745 if (r != bfd_reloc_ok)
1748 const char * msg = (const char *)0;
1751 name = h->root.root.string;
1754 name = (bfd_elf_string_from_elf_section
1755 (input_bfd, symtab_hdr->sh_link, sym->st_name));
1756 if (name == NULL || *name == '\0')
1757 name = bfd_section_name (input_bfd, sec);
1762 case bfd_reloc_overflow:
1763 if (! ((*info->callbacks->reloc_overflow)
1764 (info, name, howto->name, (bfd_vma) 0,
1765 input_bfd, input_section, rel->r_offset)))
1769 case bfd_reloc_undefined:
1770 if (! ((*info->callbacks->undefined_symbol)
1771 (info, name, input_bfd, input_section,
1772 rel->r_offset, TRUE)))
1776 case bfd_reloc_outofrange:
1777 msg = _("internal error: out of range error");
1780 case bfd_reloc_notsupported:
1781 msg = _("internal error: unsupported relocation error");
1784 case bfd_reloc_dangerous:
1785 msg = _("internal error: dangerous relocation");
1788 case bfd_reloc_gp_not_found:
1789 msg = _("could not locate special linker symbol __gp");
1792 case bfd_reloc_ep_not_found:
1793 msg = _("could not locate special linker symbol __ep");
1796 case bfd_reloc_ctbp_not_found:
1797 msg = _("could not locate special linker symbol __ctbp");
1801 msg = _("internal error: unknown error");
1805 if (!((*info->callbacks->warning)
1806 (info, msg, name, input_bfd, input_section,
1818 v850_elf_gc_sweep_hook (abfd, info, sec, relocs)
1819 bfd *abfd ATTRIBUTE_UNUSED;
1820 struct bfd_link_info *info ATTRIBUTE_UNUSED;
1821 asection *sec ATTRIBUTE_UNUSED;
1822 const Elf_Internal_Rela *relocs ATTRIBUTE_UNUSED;
1824 /* No got and plt entries for v850-elf. */
1829 v850_elf_gc_mark_hook (sec, info, rel, h, sym)
1831 struct bfd_link_info *info ATTRIBUTE_UNUSED;
1832 Elf_Internal_Rela *rel;
1833 struct elf_link_hash_entry *h;
1834 Elf_Internal_Sym *sym;
1838 switch (ELF32_R_TYPE (rel->r_info))
1840 case R_V850_GNU_VTINHERIT:
1841 case R_V850_GNU_VTENTRY:
1845 switch (h->root.type)
1847 case bfd_link_hash_defined:
1848 case bfd_link_hash_defweak:
1849 return h->root.u.def.section;
1851 case bfd_link_hash_common:
1852 return h->root.u.c.p->section;
1860 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
1865 /* Set the right machine number. */
1868 v850_elf_object_p (abfd)
1871 switch (elf_elfheader (abfd)->e_flags & EF_V850_ARCH)
1875 bfd_default_set_arch_mach (abfd, bfd_arch_v850, bfd_mach_v850);
1878 bfd_default_set_arch_mach (abfd, bfd_arch_v850, bfd_mach_v850e);
1884 /* Store the machine number in the flags field. */
1887 v850_elf_final_write_processing (abfd, linker)
1889 bfd_boolean linker ATTRIBUTE_UNUSED;
1893 switch (bfd_get_mach (abfd))
1896 case bfd_mach_v850: val = E_V850_ARCH; break;
1897 case bfd_mach_v850e: val = E_V850E_ARCH; break;
1900 elf_elfheader (abfd)->e_flags &=~ EF_V850_ARCH;
1901 elf_elfheader (abfd)->e_flags |= val;
1904 /* Function to keep V850 specific file flags. */
1907 v850_elf_set_private_flags (abfd, flags)
1911 BFD_ASSERT (!elf_flags_init (abfd)
1912 || elf_elfheader (abfd)->e_flags == flags);
1914 elf_elfheader (abfd)->e_flags = flags;
1915 elf_flags_init (abfd) = TRUE;
1919 /* Merge backend specific data from an object file
1920 to the output object file when linking. */
1922 v850_elf_merge_private_bfd_data (ibfd, obfd)
1929 if ( bfd_get_flavour (ibfd) != bfd_target_elf_flavour
1930 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
1933 in_flags = elf_elfheader (ibfd)->e_flags;
1934 out_flags = elf_elfheader (obfd)->e_flags;
1936 if (! elf_flags_init (obfd))
1938 /* If the input is the default architecture then do not
1939 bother setting the flags for the output architecture,
1940 instead allow future merges to do this. If no future
1941 merges ever set these flags then they will retain their
1942 unitialised values, which surprise surprise, correspond
1943 to the default values. */
1944 if (bfd_get_arch_info (ibfd)->the_default)
1947 elf_flags_init (obfd) = TRUE;
1948 elf_elfheader (obfd)->e_flags = in_flags;
1950 if (bfd_get_arch (obfd) == bfd_get_arch (ibfd)
1951 && bfd_get_arch_info (obfd)->the_default)
1952 return bfd_set_arch_mach (obfd, bfd_get_arch (ibfd), bfd_get_mach (ibfd));
1957 /* Check flag compatibility. */
1958 if (in_flags == out_flags)
1961 if ((in_flags & EF_V850_ARCH) != (out_flags & EF_V850_ARCH)
1962 && (in_flags & EF_V850_ARCH) != E_V850_ARCH)
1963 _bfd_error_handler (_("%s: Architecture mismatch with previous modules"),
1964 bfd_archive_filename (ibfd));
1969 /* Display the flags field. */
1972 v850_elf_print_private_bfd_data (abfd, ptr)
1976 FILE * file = (FILE *) ptr;
1978 BFD_ASSERT (abfd != NULL && ptr != NULL);
1980 _bfd_elf_print_private_bfd_data (abfd, ptr);
1982 /* xgettext:c-format */
1983 fprintf (file, _("private flags = %lx: "), elf_elfheader (abfd)->e_flags);
1985 switch (elf_elfheader (abfd)->e_flags & EF_V850_ARCH)
1988 case E_V850_ARCH: fprintf (file, _("v850 architecture")); break;
1989 case E_V850E_ARCH: fprintf (file, _("v850e architecture")); break;
1997 /* V850 ELF uses four common sections. One is the usual one, and the
1998 others are for (small) objects in one of the special data areas:
1999 small, tiny and zero. All the objects are kept together, and then
2000 referenced via the gp register, the ep register or the r0 register
2001 respectively, which yields smaller, faster assembler code. This
2002 approach is copied from elf32-mips.c. */
2004 static asection v850_elf_scom_section;
2005 static asymbol v850_elf_scom_symbol;
2006 static asymbol * v850_elf_scom_symbol_ptr;
2007 static asection v850_elf_tcom_section;
2008 static asymbol v850_elf_tcom_symbol;
2009 static asymbol * v850_elf_tcom_symbol_ptr;
2010 static asection v850_elf_zcom_section;
2011 static asymbol v850_elf_zcom_symbol;
2012 static asymbol * v850_elf_zcom_symbol_ptr;
2014 /* Given a BFD section, try to locate the
2015 corresponding ELF section index. */
2018 v850_elf_section_from_bfd_section (abfd, sec, retval)
2019 bfd *abfd ATTRIBUTE_UNUSED;
2023 if (strcmp (bfd_get_section_name (abfd, sec), ".scommon") == 0)
2024 *retval = SHN_V850_SCOMMON;
2025 else if (strcmp (bfd_get_section_name (abfd, sec), ".tcommon") == 0)
2026 *retval = SHN_V850_TCOMMON;
2027 else if (strcmp (bfd_get_section_name (abfd, sec), ".zcommon") == 0)
2028 *retval = SHN_V850_ZCOMMON;
2035 /* Handle the special V850 section numbers that a symbol may use. */
2038 v850_elf_symbol_processing (abfd, asym)
2042 elf_symbol_type * elfsym = (elf_symbol_type *) asym;
2045 indx = elfsym->internal_elf_sym.st_shndx;
2047 /* If the section index is an "ordinary" index, then it may
2048 refer to a v850 specific section created by the assembler.
2049 Check the section's type and change the index it matches.
2051 FIXME: Should we alter the st_shndx field as well ? */
2053 if (indx < elf_numsections (abfd))
2054 switch (elf_elfsections(abfd)[indx]->sh_type)
2056 case SHT_V850_SCOMMON:
2057 indx = SHN_V850_SCOMMON;
2060 case SHT_V850_TCOMMON:
2061 indx = SHN_V850_TCOMMON;
2064 case SHT_V850_ZCOMMON:
2065 indx = SHN_V850_ZCOMMON;
2074 case SHN_V850_SCOMMON:
2075 if (v850_elf_scom_section.name == NULL)
2077 /* Initialize the small common section. */
2078 v850_elf_scom_section.name = ".scommon";
2079 v850_elf_scom_section.flags = SEC_IS_COMMON | SEC_ALLOC | SEC_DATA;
2080 v850_elf_scom_section.output_section = & v850_elf_scom_section;
2081 v850_elf_scom_section.symbol = & v850_elf_scom_symbol;
2082 v850_elf_scom_section.symbol_ptr_ptr = & v850_elf_scom_symbol_ptr;
2083 v850_elf_scom_symbol.name = ".scommon";
2084 v850_elf_scom_symbol.flags = BSF_SECTION_SYM;
2085 v850_elf_scom_symbol.section = & v850_elf_scom_section;
2086 v850_elf_scom_symbol_ptr = & v850_elf_scom_symbol;
2088 asym->section = & v850_elf_scom_section;
2089 asym->value = elfsym->internal_elf_sym.st_size;
2092 case SHN_V850_TCOMMON:
2093 if (v850_elf_tcom_section.name == NULL)
2095 /* Initialize the tcommon section. */
2096 v850_elf_tcom_section.name = ".tcommon";
2097 v850_elf_tcom_section.flags = SEC_IS_COMMON;
2098 v850_elf_tcom_section.output_section = & v850_elf_tcom_section;
2099 v850_elf_tcom_section.symbol = & v850_elf_tcom_symbol;
2100 v850_elf_tcom_section.symbol_ptr_ptr = & v850_elf_tcom_symbol_ptr;
2101 v850_elf_tcom_symbol.name = ".tcommon";
2102 v850_elf_tcom_symbol.flags = BSF_SECTION_SYM;
2103 v850_elf_tcom_symbol.section = & v850_elf_tcom_section;
2104 v850_elf_tcom_symbol_ptr = & v850_elf_tcom_symbol;
2106 asym->section = & v850_elf_tcom_section;
2107 asym->value = elfsym->internal_elf_sym.st_size;
2110 case SHN_V850_ZCOMMON:
2111 if (v850_elf_zcom_section.name == NULL)
2113 /* Initialize the zcommon section. */
2114 v850_elf_zcom_section.name = ".zcommon";
2115 v850_elf_zcom_section.flags = SEC_IS_COMMON;
2116 v850_elf_zcom_section.output_section = & v850_elf_zcom_section;
2117 v850_elf_zcom_section.symbol = & v850_elf_zcom_symbol;
2118 v850_elf_zcom_section.symbol_ptr_ptr = & v850_elf_zcom_symbol_ptr;
2119 v850_elf_zcom_symbol.name = ".zcommon";
2120 v850_elf_zcom_symbol.flags = BSF_SECTION_SYM;
2121 v850_elf_zcom_symbol.section = & v850_elf_zcom_section;
2122 v850_elf_zcom_symbol_ptr = & v850_elf_zcom_symbol;
2124 asym->section = & v850_elf_zcom_section;
2125 asym->value = elfsym->internal_elf_sym.st_size;
2130 /* Hook called by the linker routine which adds symbols from an object
2131 file. We must handle the special v850 section numbers here. */
2134 v850_elf_add_symbol_hook (abfd, info, sym, namep, flagsp, secp, valp)
2136 struct bfd_link_info *info ATTRIBUTE_UNUSED;
2137 const Elf_Internal_Sym *sym;
2138 const char **namep ATTRIBUTE_UNUSED;
2139 flagword *flagsp ATTRIBUTE_UNUSED;
2143 unsigned int indx = sym->st_shndx;
2145 /* If the section index is an "ordinary" index, then it may
2146 refer to a v850 specific section created by the assembler.
2147 Check the section's type and change the index it matches.
2149 FIXME: Should we alter the st_shndx field as well ? */
2151 if (indx < elf_numsections (abfd))
2152 switch (elf_elfsections(abfd)[indx]->sh_type)
2154 case SHT_V850_SCOMMON:
2155 indx = SHN_V850_SCOMMON;
2158 case SHT_V850_TCOMMON:
2159 indx = SHN_V850_TCOMMON;
2162 case SHT_V850_ZCOMMON:
2163 indx = SHN_V850_ZCOMMON;
2172 case SHN_V850_SCOMMON:
2173 *secp = bfd_make_section_old_way (abfd, ".scommon");
2174 (*secp)->flags |= SEC_IS_COMMON;
2175 *valp = sym->st_size;
2178 case SHN_V850_TCOMMON:
2179 *secp = bfd_make_section_old_way (abfd, ".tcommon");
2180 (*secp)->flags |= SEC_IS_COMMON;
2181 *valp = sym->st_size;
2184 case SHN_V850_ZCOMMON:
2185 *secp = bfd_make_section_old_way (abfd, ".zcommon");
2186 (*secp)->flags |= SEC_IS_COMMON;
2187 *valp = sym->st_size;
2195 v850_elf_link_output_symbol_hook (abfd, info, name, sym, input_sec)
2196 bfd *abfd ATTRIBUTE_UNUSED;
2197 struct bfd_link_info *info ATTRIBUTE_UNUSED;
2198 const char *name ATTRIBUTE_UNUSED;
2199 Elf_Internal_Sym *sym;
2200 asection *input_sec;
2202 /* If we see a common symbol, which implies a relocatable link, then
2203 if a symbol was in a special common section in an input file, mark
2204 it as a special common in the output file. */
2206 if (sym->st_shndx == SHN_COMMON)
2208 if (strcmp (input_sec->name, ".scommon") == 0)
2209 sym->st_shndx = SHN_V850_SCOMMON;
2210 else if (strcmp (input_sec->name, ".tcommon") == 0)
2211 sym->st_shndx = SHN_V850_TCOMMON;
2212 else if (strcmp (input_sec->name, ".zcommon") == 0)
2213 sym->st_shndx = SHN_V850_ZCOMMON;
2220 v850_elf_section_from_shdr (abfd, hdr, name)
2222 Elf_Internal_Shdr *hdr;
2225 /* There ought to be a place to keep ELF backend specific flags, but
2226 at the moment there isn't one. We just keep track of the
2227 sections by their name, instead. */
2229 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name))
2232 switch (hdr->sh_type)
2234 case SHT_V850_SCOMMON:
2235 case SHT_V850_TCOMMON:
2236 case SHT_V850_ZCOMMON:
2237 if (! bfd_set_section_flags (abfd, hdr->bfd_section,
2238 (bfd_get_section_flags (abfd,
2247 /* Set the correct type for a V850 ELF section. We do this
2248 by the section name, which is a hack, but ought to work. */
2251 v850_elf_fake_sections (abfd, hdr, sec)
2252 bfd *abfd ATTRIBUTE_UNUSED;
2253 Elf_Internal_Shdr *hdr;
2256 register const char * name;
2258 name = bfd_get_section_name (abfd, sec);
2260 if (strcmp (name, ".scommon") == 0)
2262 hdr->sh_type = SHT_V850_SCOMMON;
2264 else if (strcmp (name, ".tcommon") == 0)
2266 hdr->sh_type = SHT_V850_TCOMMON;
2268 else if (strcmp (name, ".zcommon") == 0)
2269 hdr->sh_type = SHT_V850_ZCOMMON;
2274 /* Delete some bytes from a section while relaxing. */
2277 v850_elf_relax_delete_bytes (abfd, sec, addr, toaddr, count)
2284 Elf_Internal_Shdr *symtab_hdr;
2285 Elf32_External_Sym *extsyms;
2286 Elf32_External_Sym *esym;
2287 Elf32_External_Sym *esymend;
2289 unsigned int sec_shndx;
2291 Elf_Internal_Rela *irel;
2292 Elf_Internal_Rela *irelend;
2293 struct elf_link_hash_entry *sym_hash;
2294 Elf_Internal_Shdr *shndx_hdr;
2295 Elf_External_Sym_Shndx *shndx;
2297 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2298 extsyms = (Elf32_External_Sym *) symtab_hdr->contents;
2300 sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
2302 contents = elf_section_data (sec)->this_hdr.contents;
2304 /* The deletion must stop at the next ALIGN reloc for an alignment
2305 power larger than the number of bytes we are deleting. */
2307 /* Actually delete the bytes. */
2308 #if (DEBUG_RELAX & 2)
2309 fprintf (stderr, "relax_delete: contents: sec: %s %p .. %p %x\n",
2310 sec->name, addr, toaddr, count );
2312 memmove (contents + addr, contents + addr + count,
2313 toaddr - addr - count);
2314 memset (contents + toaddr-count, 0, count);
2316 /* Adjust all the relocs. */
2317 irel = elf_section_data (sec)->relocs;
2318 irelend = irel + sec->reloc_count;
2319 shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
2320 shndx = (Elf_External_Sym_Shndx *) shndx_hdr->contents;
2322 for (; irel < irelend; irel++)
2324 bfd_vma raddr, paddr, symval;
2325 Elf_Internal_Sym isym;
2327 /* Get the new reloc address. */
2328 raddr = irel->r_offset;
2329 if ((raddr >= (addr + count) && raddr < toaddr))
2330 irel->r_offset -= count;
2332 if (raddr >= addr && raddr < addr + count)
2334 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
2339 if (ELF32_R_TYPE (irel->r_info) == (int) R_V850_ALIGN)
2342 bfd_elf32_swap_symbol_in (abfd,
2343 extsyms + ELF32_R_SYM (irel->r_info),
2344 shndx ? shndx + ELF32_R_SYM (irel->r_info) : NULL,
2347 if (isym.st_shndx != sec_shndx)
2350 /* Get the value of the symbol referred to by the reloc. */
2351 if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
2353 symval = isym.st_value;
2354 #if (DEBUG_RELAX & 2)
2356 char * name = bfd_elf_string_from_elf_section
2357 (abfd, symtab_hdr->sh_link, isym.st_name);
2359 "relax_delete: local: sec: %s, sym: %s (%d), value: %x + %x + %x addend %x\n",
2360 sec->name, name, isym.st_name,
2361 sec->output_section->vma, sec->output_offset,
2362 isym.st_value, irel->r_addend);
2369 struct elf_link_hash_entry * h;
2371 /* An external symbol. */
2372 indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
2374 h = elf_sym_hashes (abfd) [indx];
2375 BFD_ASSERT (h != NULL);
2377 symval = h->root.u.def.value;
2378 #if (DEBUG_RELAX & 2)
2380 "relax_delete: defined: sec: %s, name: %s, value: %x + %x + %x addend %x\n",
2381 sec->name, h->root.root.string, h->root.u.def.value,
2382 sec->output_section->vma, sec->output_offset, irel->r_addend);
2386 paddr = symval + irel->r_addend;
2388 if ( (symval >= addr + count && symval < toaddr)
2389 && (paddr < addr + count || paddr >= toaddr))
2390 irel->r_addend += count;
2391 else if ( (symval < addr + count || symval >= toaddr)
2392 && (paddr >= addr + count && paddr < toaddr))
2393 irel->r_addend -= count;
2396 /* Adjust the local symbols defined in this section. */
2398 esymend = esym + symtab_hdr->sh_info;
2400 for (; esym < esymend; esym++, shndx = (shndx ? shndx + 1 : NULL))
2402 Elf_Internal_Sym isym;
2404 bfd_elf32_swap_symbol_in (abfd, esym, shndx, & isym);
2406 if (isym.st_shndx == sec_shndx
2407 && isym.st_value >= addr + count
2408 && isym.st_value < toaddr)
2410 isym.st_value -= count;
2412 if (isym.st_value + isym.st_size >= toaddr)
2413 isym.st_size += count;
2415 bfd_elf32_swap_symbol_out (abfd, & isym, esym, shndx);
2417 else if (isym.st_shndx == sec_shndx
2418 && isym.st_value < addr + count)
2420 if (isym.st_value+isym.st_size >= addr + count
2421 && isym.st_value+isym.st_size < toaddr)
2422 isym.st_size -= count;
2424 if (isym.st_value >= addr
2425 && isym.st_value < addr + count)
2426 isym.st_value = addr;
2428 bfd_elf32_swap_symbol_out (abfd, & isym, esym, shndx);
2432 /* Now adjust the global symbols defined in this section. */
2433 esym = extsyms + symtab_hdr->sh_info;
2434 esymend = extsyms + (symtab_hdr->sh_size / sizeof (Elf32_External_Sym));
2436 for (index = 0; esym < esymend; esym ++, index ++)
2438 Elf_Internal_Sym isym;
2440 bfd_elf32_swap_symbol_in (abfd, esym, shndx, & isym);
2441 sym_hash = elf_sym_hashes (abfd) [index];
2443 if (isym.st_shndx == sec_shndx
2444 && ((sym_hash)->root.type == bfd_link_hash_defined
2445 || (sym_hash)->root.type == bfd_link_hash_defweak)
2446 && (sym_hash)->root.u.def.section == sec
2447 && (sym_hash)->root.u.def.value >= addr + count
2448 && (sym_hash)->root.u.def.value < toaddr)
2450 if ((sym_hash)->root.u.def.value + isym.st_size >= toaddr)
2452 isym.st_size += count;
2453 bfd_elf32_swap_symbol_out (abfd, & isym, esym, shndx);
2456 (sym_hash)->root.u.def.value -= count;
2458 else if (isym.st_shndx == sec_shndx
2459 && ((sym_hash)->root.type == bfd_link_hash_defined
2460 || (sym_hash)->root.type == bfd_link_hash_defweak)
2461 && (sym_hash)->root.u.def.section == sec
2462 && (sym_hash)->root.u.def.value < addr + count)
2464 if ((sym_hash)->root.u.def.value+isym.st_size >= addr + count
2465 && (sym_hash)->root.u.def.value+isym.st_size < toaddr)
2466 isym.st_size -= count;
2468 if ((sym_hash)->root.u.def.value >= addr
2469 && (sym_hash)->root.u.def.value < addr + count)
2470 (sym_hash)->root.u.def.value = addr;
2472 bfd_elf32_swap_symbol_out (abfd, & isym, esym, shndx);
2482 #define NOP_OPCODE (0x0000)
2483 #define MOVHI 0x0640 /* 4byte */
2484 #define MOVHI_MASK 0x07e0
2485 #define MOVHI_R1(insn) ((insn) & 0x1f) /* 4byte */
2486 #define MOVHI_R2(insn) ((insn) >> 11)
2487 #define MOVEA 0x0620 /* 2byte */
2488 #define MOVEA_MASK 0x07e0
2489 #define MOVEA_R1(insn) ((insn) & 0x1f)
2490 #define MOVEA_R2(insn) ((insn) >> 11)
2491 #define JARL_4 0x00040780 /* 4byte */
2492 #define JARL_4_MASK 0xFFFF07FF
2493 #define JARL_R2(insn) (int)(((insn) & (~JARL_4_MASK)) >> 11)
2494 #define ADD_I 0x0240 /* 2byte */
2495 #define ADD_I_MASK 0x07e0
2496 #define ADD_I5(insn) ((((insn) & 0x001f) << 11) >> 11) /* 2byte */
2497 #define ADD_R2(insn) ((insn) >> 11)
2498 #define JMP_R 0x0060 /* 2byte */
2499 #define JMP_R_MASK 0xFFE0
2500 #define JMP_R1(insn) ((insn) & 0x1f)
2503 v850_elf_relax_section (abfd, sec, link_info, again)
2506 struct bfd_link_info *link_info;
2509 Elf_Internal_Shdr *symtab_hdr;
2510 Elf_Internal_Rela *internal_relocs;
2511 Elf_Internal_Rela *irel;
2512 Elf_Internal_Rela *irelend;
2513 Elf_Internal_Rela *irelalign = NULL;
2514 Elf_Internal_Sym *isymbuf = NULL;
2515 bfd_byte *contents = NULL;
2518 int align_pad_size = 0;
2519 bfd_boolean result = TRUE;
2523 if (link_info->relocatable
2524 || (sec->flags & SEC_RELOC) == 0
2525 || sec->reloc_count == 0)
2528 /* If this is the first time we have been called
2529 for this section, initialize the cooked size. */
2530 if (sec->_cooked_size == 0)
2531 sec->_cooked_size = sec->_raw_size;
2533 symtab_hdr = & elf_tdata (abfd)->symtab_hdr;
2535 internal_relocs = (_bfd_elf_link_read_relocs
2536 (abfd, sec, (PTR) NULL, (Elf_Internal_Rela *) NULL,
2537 link_info->keep_memory));
2538 if (internal_relocs == NULL)
2541 irelend = internal_relocs + sec->reloc_count;
2543 while (addr < sec->_cooked_size)
2545 toaddr = sec->_cooked_size;
2547 for (irel = internal_relocs; irel < irelend; irel ++)
2548 if (ELF32_R_TYPE (irel->r_info) == (int) R_V850_ALIGN
2549 && irel->r_offset > addr
2550 && irel->r_offset < toaddr)
2551 toaddr = irel->r_offset;
2554 fprintf (stderr, "relax region 0x%x to 0x%x align pad %d\n",
2555 addr, toaddr, align_pad_size);
2560 bfd_vma alignmoveto;
2562 alignmoveto = BFD_ALIGN (addr - align_pad_size, 1 << irelalign->r_addend);
2563 alignto = BFD_ALIGN (addr, 1 << irelalign->r_addend);
2565 if (alignmoveto < alignto)
2569 align_pad_size = alignto - alignmoveto;
2571 fprintf (stderr, "relax move region 0x%x to 0x%x delete size 0x%x\n",
2572 alignmoveto, toaddr, align_pad_size);
2574 if (!v850_elf_relax_delete_bytes (abfd, sec, alignmoveto,
2575 toaddr, align_pad_size))
2578 for (i = BFD_ALIGN (toaddr - align_pad_size, 1);
2579 (i + 1) < toaddr; i += 2)
2580 bfd_put_16 (abfd, NOP_OPCODE, contents + i);
2588 for (irel = internal_relocs; irel < irelend; irel++)
2595 Elf_Internal_Rela *hi_irelfn;
2596 Elf_Internal_Rela *lo_irelfn;
2597 Elf_Internal_Rela *irelcall;
2598 bfd_signed_vma foff;
2600 if (! (irel->r_offset >= addr && irel->r_offset < toaddr
2601 && (ELF32_R_TYPE (irel->r_info) == (int) R_V850_LONGCALL
2602 || ELF32_R_TYPE (irel->r_info) == (int) R_V850_LONGJUMP)))
2606 fprintf (stderr, "relax check r_info 0x%x r_offset 0x%x r_addend 0x%x\n",
2612 /* Get the section contents. */
2613 if (contents == NULL)
2615 if (elf_section_data (sec)->this_hdr.contents != NULL)
2616 contents = elf_section_data (sec)->this_hdr.contents;
2619 contents = (bfd_byte *) bfd_malloc (sec->_raw_size);
2620 if (contents == NULL)
2623 if (! bfd_get_section_contents (abfd, sec, contents,
2624 (file_ptr) 0, sec->_raw_size))
2629 /* Read this BFD's local symbols if we haven't done so already. */
2630 if (isymbuf == NULL && symtab_hdr->sh_info != 0)
2632 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
2633 if (isymbuf == NULL)
2634 isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
2635 symtab_hdr->sh_info, 0,
2637 if (isymbuf == NULL)
2641 laddr = irel->r_offset;
2643 if (ELF32_R_TYPE (irel->r_info) == (int) R_V850_LONGCALL)
2645 /* Check code for -mlong-calls output. */
2646 if (laddr + 16 <= (bfd_vma) sec->_raw_size)
2648 insn[0] = bfd_get_16 (abfd, contents + laddr);
2649 insn[1] = bfd_get_16 (abfd, contents + laddr + 4);
2650 insn[2] = bfd_get_32 (abfd, contents + laddr + 8);
2651 insn[3] = bfd_get_16 (abfd, contents + laddr + 12);
2652 insn[4] = bfd_get_16 (abfd, contents + laddr + 14);
2654 if ((insn[0] & MOVHI_MASK) != MOVHI
2655 || MOVHI_R1 (insn[0]) != 0)
2659 && ((insn[1] & MOVEA_MASK) != MOVEA
2660 || MOVHI_R2 (insn[0]) != MOVEA_R1 (insn[1])))
2664 && (insn[2] & JARL_4_MASK) != JARL_4)
2668 && ((insn[3] & ADD_I_MASK) != ADD_I
2669 || ADD_I5 (insn[3]) != 4
2670 || JARL_R2 (insn[2]) != ADD_R2 (insn[3])))
2674 && ((insn[4] & JMP_R_MASK) != JMP_R
2675 || MOVEA_R2 (insn[1]) != JMP_R1 (insn[4])))
2680 ((*_bfd_error_handler)
2681 ("%s: 0x%lx: warning: R_V850_LONGCALL points to unrecognized insns",
2682 bfd_get_filename (abfd), (unsigned long) irel->r_offset));
2689 ((*_bfd_error_handler)
2690 ("%s: 0x%lx: warning: R_V850_LONGCALL points to unrecognized insn 0x%x",
2691 bfd_get_filename (abfd), (unsigned long) irel->r_offset+no_match, insn[no_match]));
2696 /* Get the reloc for the address from which the register is
2697 being loaded. This reloc will tell us which function is
2698 actually being called. */
2699 for (hi_irelfn = internal_relocs; hi_irelfn < irelend; hi_irelfn ++)
2700 if (hi_irelfn->r_offset == laddr + 2
2701 && ELF32_R_TYPE (hi_irelfn->r_info)
2702 == (int) R_V850_HI16_S)
2705 for (lo_irelfn = internal_relocs; lo_irelfn < irelend; lo_irelfn ++)
2706 if (lo_irelfn->r_offset == laddr + 6
2707 && ELF32_R_TYPE (lo_irelfn->r_info)
2708 == (int) R_V850_LO16)
2711 for (irelcall = internal_relocs; irelcall < irelend; irelcall ++)
2712 if (irelcall->r_offset == laddr + 8
2713 && ELF32_R_TYPE (irelcall->r_info)
2714 == (int) R_V850_22_PCREL)
2717 if ( hi_irelfn == irelend
2718 || lo_irelfn == irelend
2719 || irelcall == irelend)
2721 ((*_bfd_error_handler)
2722 ("%s: 0x%lx: warning: R_V850_LONGCALL points to unrecognized reloc",
2723 bfd_get_filename (abfd), (unsigned long) irel->r_offset ));
2728 if (ELF32_R_SYM (irelcall->r_info) < symtab_hdr->sh_info)
2730 Elf_Internal_Sym * isym;
2732 /* A local symbol. */
2733 isym = isymbuf + ELF32_R_SYM (irelcall->r_info);
2735 symval = isym->st_value;
2740 struct elf_link_hash_entry * h;
2742 /* An external symbol. */
2743 indx = ELF32_R_SYM (irelcall->r_info) - symtab_hdr->sh_info;
2744 h = elf_sym_hashes (abfd)[indx];
2745 BFD_ASSERT (h != NULL);
2747 if ( h->root.type != bfd_link_hash_defined
2748 && h->root.type != bfd_link_hash_defweak)
2749 /* This appears to be a reference to an undefined
2750 symbol. Just ignore it--it will be caught by the
2751 regular reloc processing. */
2754 symval = h->root.u.def.value;
2757 if (symval + irelcall->r_addend != irelcall->r_offset + 4)
2759 ((*_bfd_error_handler)
2760 ("%s: 0x%lx: warning: R_V850_LONGCALL points to unrecognized reloc 0x%lx",
2761 bfd_get_filename (abfd), (unsigned long) irel->r_offset, irelcall->r_offset ));
2766 /* Get the value of the symbol referred to by the reloc. */
2767 if (ELF32_R_SYM (hi_irelfn->r_info) < symtab_hdr->sh_info)
2769 Elf_Internal_Sym *isym;
2772 /* A local symbol. */
2773 isym = isymbuf + ELF32_R_SYM (hi_irelfn->r_info);
2775 if (isym->st_shndx == SHN_UNDEF)
2776 sym_sec = bfd_und_section_ptr;
2777 else if (isym->st_shndx == SHN_ABS)
2778 sym_sec = bfd_abs_section_ptr;
2779 else if (isym->st_shndx == SHN_COMMON)
2780 sym_sec = bfd_com_section_ptr;
2782 sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
2783 symval = (isym->st_value
2784 + sym_sec->output_section->vma
2785 + sym_sec->output_offset);
2790 struct elf_link_hash_entry *h;
2792 /* An external symbol. */
2793 indx = ELF32_R_SYM (hi_irelfn->r_info) - symtab_hdr->sh_info;
2794 h = elf_sym_hashes (abfd)[indx];
2795 BFD_ASSERT (h != NULL);
2797 if ( h->root.type != bfd_link_hash_defined
2798 && h->root.type != bfd_link_hash_defweak)
2799 /* This appears to be a reference to an undefined
2800 symbol. Just ignore it--it will be caught by the
2801 regular reloc processing. */
2804 symval = (h->root.u.def.value
2805 + h->root.u.def.section->output_section->vma
2806 + h->root.u.def.section->output_offset);
2809 addend = irel->r_addend;
2811 foff = (symval + addend
2813 + sec->output_section->vma
2814 + sec->output_offset
2817 fprintf (stderr, "relax longcall r_offset 0x%x ptr 0x%x symbol 0x%x addend 0x%x distance 0x%x\n",
2820 + sec->output_section->vma
2821 + sec->output_offset),
2822 symval, addend, foff);
2825 if (foff < -0x100000 || foff >= 0x100000)
2826 /* After all that work, we can't shorten this function call. */
2829 /* For simplicity of coding, we are going to modify the section
2830 contents, the section relocs, and the BFD symbol table. We
2831 must tell the rest of the code not to free up this
2832 information. It would be possible to instead create a table
2833 of changes which have to be made, as is done in coff-mips.c;
2834 that would be more work, but would require less memory when
2835 the linker is run. */
2836 elf_section_data (sec)->relocs = internal_relocs;
2837 elf_section_data (sec)->this_hdr.contents = contents;
2838 symtab_hdr->contents = (bfd_byte *) isymbuf;
2840 /* Replace the long call with a jarl. */
2841 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info), R_V850_22_PCREL);
2845 if (ELF32_R_SYM (hi_irelfn->r_info) < symtab_hdr->sh_info)
2846 /* If this needs to be changed because of future relaxing,
2847 it will be handled here like other internal IND12W
2850 0x00000780 | (JARL_R2 (insn[2])<<11) | ((addend << 16) & 0xffff) | ((addend >> 16) & 0xf),
2851 contents + irel->r_offset);
2853 /* We can't fully resolve this yet, because the external
2854 symbol value may be changed by future relaxing.
2855 We let the final link phase handle it. */
2856 bfd_put_32 (abfd, 0x00000780 | (JARL_R2 (insn[2])<<11),
2857 contents + irel->r_offset);
2860 ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info), R_V850_NONE);
2862 ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info), R_V850_NONE);
2864 ELF32_R_INFO (ELF32_R_SYM (irelcall->r_info), R_V850_NONE);
2866 if (! v850_elf_relax_delete_bytes (abfd, sec,
2867 irel->r_offset + 4, toaddr, 12))
2870 align_pad_size += 12;
2872 else if (ELF32_R_TYPE (irel->r_info) == (int) R_V850_LONGJUMP)
2874 /* Check code for -mlong-jumps output. */
2875 if (laddr + 10 <= (bfd_vma) sec->_raw_size)
2877 insn[0] = bfd_get_16 (abfd, contents + laddr);
2878 insn[1] = bfd_get_16 (abfd, contents + laddr + 4);
2879 insn[2] = bfd_get_16 (abfd, contents + laddr + 8);
2881 if ((insn[0] & MOVHI_MASK) != MOVHI
2882 || MOVHI_R1 (insn[0]) != 0)
2886 && ((insn[1] & MOVEA_MASK) != MOVEA
2887 || MOVHI_R2 (insn[0]) != MOVEA_R1 (insn[1])))
2891 && ((insn[2] & JMP_R_MASK) != JMP_R
2892 || MOVEA_R2 (insn[1]) != JMP_R1 (insn[2])))
2897 ((*_bfd_error_handler)
2898 ("%s: 0x%lx: warning: R_V850_LONGJUMP points to unrecognized insns",
2899 bfd_get_filename (abfd), (unsigned long) irel->r_offset));
2906 ((*_bfd_error_handler)
2907 ("%s: 0x%lx: warning: R_V850_LONGJUMP points to unrecognized insn 0x%x",
2908 bfd_get_filename (abfd), (unsigned long) irel->r_offset+no_match, insn[no_match]));
2913 /* Get the reloc for the address from which the register is
2914 being loaded. This reloc will tell us which function is
2915 actually being called. */
2916 for (hi_irelfn = internal_relocs; hi_irelfn < irelend; hi_irelfn ++)
2917 if (hi_irelfn->r_offset == laddr + 2
2918 && ELF32_R_TYPE (hi_irelfn->r_info) == (int) R_V850_HI16_S)
2921 for (lo_irelfn = internal_relocs; lo_irelfn < irelend; lo_irelfn ++)
2922 if (lo_irelfn->r_offset == laddr + 6
2923 && ELF32_R_TYPE (lo_irelfn->r_info) == (int) R_V850_LO16)
2926 if ( hi_irelfn == irelend
2927 || lo_irelfn == irelend)
2929 ((*_bfd_error_handler)
2930 ("%s: 0x%lx: warning: R_V850_LONGJUMP points to unrecognized reloc",
2931 bfd_get_filename (abfd), (unsigned long) irel->r_offset ));
2936 /* Get the value of the symbol referred to by the reloc. */
2937 if (ELF32_R_SYM (hi_irelfn->r_info) < symtab_hdr->sh_info)
2939 Elf_Internal_Sym * isym;
2942 /* A local symbol. */
2943 isym = isymbuf + ELF32_R_SYM (hi_irelfn->r_info);
2945 if (isym->st_shndx == SHN_UNDEF)
2946 sym_sec = bfd_und_section_ptr;
2947 else if (isym->st_shndx == SHN_ABS)
2948 sym_sec = bfd_abs_section_ptr;
2949 else if (isym->st_shndx == SHN_COMMON)
2950 sym_sec = bfd_com_section_ptr;
2952 sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
2953 symval = (isym->st_value
2954 + sym_sec->output_section->vma
2955 + sym_sec->output_offset);
2958 char * name = bfd_elf_string_from_elf_section
2959 (abfd, symtab_hdr->sh_link, isym->st_name);
2961 fprintf (stderr, "relax long jump local: sec: %s, sym: %s (%d), value: %x + %x + %x addend %x\n",
2962 sym_sec->name, name, isym->st_name,
2963 sym_sec->output_section->vma,
2964 sym_sec->output_offset,
2965 isym->st_value, irel->r_addend);
2972 struct elf_link_hash_entry * h;
2974 /* An external symbol. */
2975 indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
2976 h = elf_sym_hashes (abfd)[indx];
2977 BFD_ASSERT (h != NULL);
2979 if ( h->root.type != bfd_link_hash_defined
2980 && h->root.type != bfd_link_hash_defweak)
2981 /* This appears to be a reference to an undefined
2982 symbol. Just ignore it--it will be caught by the
2983 regular reloc processing. */
2986 symval = (h->root.u.def.value
2987 + h->root.u.def.section->output_section->vma
2988 + h->root.u.def.section->output_offset);
2991 "relax longjump defined: sec: %s, name: %s, value: %x + %x + %x addend %x\n",
2992 sec->name, h->root.root.string, h->root.u.def.value,
2993 sec->output_section->vma, sec->output_offset, irel->r_addend);
2997 addend = irel->r_addend;
2999 foff = (symval + addend
3001 + sec->output_section->vma
3002 + sec->output_offset
3005 fprintf (stderr, "relax longjump r_offset 0x%x ptr 0x%x symbol 0x%x addend 0x%x distance 0x%x\n",
3008 + sec->output_section->vma
3009 + sec->output_offset),
3010 symval, addend, foff);
3012 if (foff < -0x100000 || foff >= 0x100000)
3013 /* After all that work, we can't shorten this function call. */
3016 /* For simplicity of coding, we are going to modify the section
3017 contents, the section relocs, and the BFD symbol table. We
3018 must tell the rest of the code not to free up this
3019 information. It would be possible to instead create a table
3020 of changes which have to be made, as is done in coff-mips.c;
3021 that would be more work, but would require less memory when
3022 the linker is run. */
3023 elf_section_data (sec)->relocs = internal_relocs;
3024 elf_section_data (sec)->this_hdr.contents = contents;
3025 symtab_hdr->contents = (bfd_byte *) isymbuf;
3027 if (foff < -0x100 || foff >= 0x100)
3029 /* Replace the long jump with a jr. */
3032 ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_V850_22_PCREL);
3034 irel->r_addend = addend;
3037 if (ELF32_R_SYM (hi_irelfn->r_info) < symtab_hdr->sh_info)
3038 /* If this needs to be changed because of future relaxing,
3039 it will be handled here like other internal IND12W
3042 0x00000780 | ((addend << 15) & 0xffff0000) | ((addend >> 17) & 0xf),
3043 contents + irel->r_offset);
3045 /* We can't fully resolve this yet, because the external
3046 symbol value may be changed by future relaxing.
3047 We let the final link phase handle it. */
3048 bfd_put_32 (abfd, 0x00000780, contents + irel->r_offset);
3051 ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info), R_V850_NONE);
3053 ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info), R_V850_NONE);
3054 if (!v850_elf_relax_delete_bytes (abfd, sec,
3055 irel->r_offset + 4, toaddr, 6))
3058 align_pad_size += 6;
3062 /* Replace the long jump with a br. */
3065 ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_V850_9_PCREL);
3067 irel->r_addend = addend;
3070 if (ELF32_R_SYM (hi_irelfn->r_info) < symtab_hdr->sh_info)
3071 /* If this needs to be changed because of future relaxing,
3072 it will be handled here like other internal IND12W
3075 0x0585 | ((addend << 10) & 0xf800) | ((addend << 3) & 0x0070),
3076 contents + irel->r_offset);
3078 /* We can't fully resolve this yet, because the external
3079 symbol value may be changed by future relaxing.
3080 We let the final link phase handle it. */
3081 bfd_put_16 (abfd, 0x0585, contents + irel->r_offset);
3084 ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info), R_V850_NONE);
3086 ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info), R_V850_NONE);
3087 if (!v850_elf_relax_delete_bytes (abfd, sec,
3088 irel->r_offset + 2, toaddr, 8))
3091 align_pad_size += 8;
3097 for (irel = internal_relocs; irel < irelend; irel++)
3099 if (ELF32_R_TYPE (irel->r_info) == (int) R_V850_ALIGN
3100 && irel->r_offset == toaddr)
3102 irel->r_offset -= align_pad_size;
3104 if (irelalign == NULL || irelalign->r_addend > irel->r_addend)
3115 fprintf (stderr, "relax pad %d shorten %d -> %d\n",
3118 sec->_cooked_size - align_pad_size);
3120 sec->_cooked_size -= align_pad_size;
3124 if (internal_relocs != NULL
3125 && elf_section_data (sec)->relocs != internal_relocs)
3126 free (internal_relocs);
3128 if (contents != NULL
3129 && elf_section_data (sec)->this_hdr.contents != (unsigned char *) contents)
3133 && symtab_hdr->contents != (bfd_byte *) isymbuf)
3143 static struct bfd_elf_special_section const v850_elf_special_sections[]=
3145 { ".sdata", 0, NULL, 0,
3146 SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_V850_GPREL },
3147 { ".rosdata", 0, NULL, 0,
3148 SHT_PROGBITS, SHF_ALLOC + SHF_V850_GPREL },
3149 { ".sbss", 0, NULL, 0,
3150 SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_V850_GPREL },
3151 { ".scommon", 0, NULL, 0,
3152 SHT_V850_SCOMMON, SHF_ALLOC + SHF_WRITE + SHF_V850_GPREL },
3153 { ".tdata", 0, NULL, 0,
3154 SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_V850_EPREL },
3155 { ".tbss", 0, NULL, 0,
3156 SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_V850_EPREL },
3157 { ".tcommon", 0, NULL, 0,
3158 SHT_V850_TCOMMON, SHF_ALLOC + SHF_WRITE + SHF_V850_R0REL },
3159 { ".zdata", 0, NULL, 0,
3160 SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_V850_R0REL },
3161 { ".rozdata", 0, NULL, 0,
3162 SHT_PROGBITS, SHF_ALLOC + SHF_V850_R0REL },
3163 { ".zbss", 0, NULL, 0,
3164 SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_V850_R0REL },
3165 { ".zcommon", 0, NULL, 0,
3166 SHT_V850_ZCOMMON, SHF_ALLOC + SHF_WRITE + SHF_V850_R0REL },
3167 { ".call_table_data", 0, NULL, 0,
3168 SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
3169 { ".call_table_text", 0, NULL, 0,
3170 SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_EXECINSTR },
3175 #define TARGET_LITTLE_SYM bfd_elf32_v850_vec
3176 #define TARGET_LITTLE_NAME "elf32-v850"
3177 #define ELF_ARCH bfd_arch_v850
3178 #define ELF_MACHINE_CODE EM_V850
3179 #define ELF_MACHINE_ALT1 EM_CYGNUS_V850
3180 #define ELF_MAXPAGESIZE 0x1000
3182 #define elf_info_to_howto v850_elf_info_to_howto_rela
3183 #define elf_info_to_howto_rel v850_elf_info_to_howto_rel
3185 #define elf_backend_check_relocs v850_elf_check_relocs
3186 #define elf_backend_relocate_section v850_elf_relocate_section
3187 #define elf_backend_object_p v850_elf_object_p
3188 #define elf_backend_final_write_processing v850_elf_final_write_processing
3189 #define elf_backend_section_from_bfd_section v850_elf_section_from_bfd_section
3190 #define elf_backend_symbol_processing v850_elf_symbol_processing
3191 #define elf_backend_add_symbol_hook v850_elf_add_symbol_hook
3192 #define elf_backend_link_output_symbol_hook v850_elf_link_output_symbol_hook
3193 #define elf_backend_section_from_shdr v850_elf_section_from_shdr
3194 #define elf_backend_fake_sections v850_elf_fake_sections
3195 #define elf_backend_gc_mark_hook v850_elf_gc_mark_hook
3196 #define elf_backend_gc_sweep_hook v850_elf_gc_sweep_hook
3197 #define elf_backend_special_sections v850_elf_special_sections
3199 #define elf_backend_can_gc_sections 1
3200 #define elf_backend_rela_normal 1
3202 #define bfd_elf32_bfd_is_local_label_name v850_elf_is_local_label_name
3203 #define bfd_elf32_bfd_reloc_type_lookup v850_elf_reloc_type_lookup
3204 #define bfd_elf32_bfd_merge_private_bfd_data v850_elf_merge_private_bfd_data
3205 #define bfd_elf32_bfd_set_private_flags v850_elf_set_private_flags
3206 #define bfd_elf32_bfd_print_private_bfd_data v850_elf_print_private_bfd_data
3207 #define bfd_elf32_bfd_relax_section v850_elf_relax_section
3209 #define elf_symbol_leading_char '_'
3211 #include "elf32-target.h"