]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - bfd/elf32-v850.c
* ecoff.c (_bfd_ecoff_set_arch_mach_hook): Don't use hard-coded
[thirdparty/binutils-gdb.git] / bfd / elf32-v850.c
1 /* V850-specific support for 32-bit ELF
2 Copyright 1996, 1997, 1998, 1999, 2000, 2001, 2002
3 Free Software Foundation, Inc.
4
5 This file is part of BFD, the Binary File Descriptor library.
6
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.
11
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.
16
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. */
20
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. */
23
24 #include "bfd.h"
25 #include "sysdep.h"
26 #include "bfdlink.h"
27 #include "libbfd.h"
28 #include "elf-bfd.h"
29 #include "elf/v850.h"
30 #include "libiberty.h"
31
32 /* Sign-extend a 24-bit number. */
33 #define SEXT24(x) ((((x) & 0xffffff) ^ 0x800000) - 0x800000)
34
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 *, Elf32_Internal_Rel *));
39 static void v850_elf_info_to_howto_rela
40 PARAMS ((bfd *, arelent *, Elf32_Internal_Rela *));
41 static bfd_reloc_status_type v850_elf_reloc
42 PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
43 static boolean v850_elf_is_local_label_name
44 PARAMS ((bfd *, const char *));
45 static 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 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, 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 boolean v850_elf_object_p
60 PARAMS ((bfd *));
61 static boolean v850_elf_fake_sections
62 PARAMS ((bfd *, Elf32_Internal_Shdr *, asection *));
63 static void v850_elf_final_write_processing
64 PARAMS ((bfd *, boolean));
65 static boolean v850_elf_set_private_flags
66 PARAMS ((bfd *, flagword));
67 static boolean v850_elf_merge_private_bfd_data
68 PARAMS ((bfd *, bfd *));
69 static boolean v850_elf_print_private_bfd_data
70 PARAMS ((bfd *, PTR));
71 static 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 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 boolean v850_elf_link_output_symbol_hook
79 PARAMS ((bfd *, struct bfd_link_info *, const char *,
80 Elf_Internal_Sym *, asection *));
81 static boolean v850_elf_section_from_shdr
82 PARAMS ((bfd *, Elf_Internal_Shdr *, const char *));
83 static 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 *,
89 Elf_Internal_Sym *));
90 static bfd_reloc_status_type v850_elf_ignore_reloc
91 PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
92 static boolean v850_elf_relax_delete_bytes
93 PARAMS ((bfd *, asection *, bfd_vma, bfd_vma, int));
94 static boolean v850_elf_relax_section
95 PARAMS ((bfd *, asection *, struct bfd_link_info *, boolean *));
96
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[] =
100 {
101 /* This reloc does nothing. */
102 HOWTO (R_V850_NONE, /* type */
103 0, /* rightshift */
104 2, /* size (0 = byte, 1 = short, 2 = long) */
105 32, /* bitsize */
106 false, /* pc_relative */
107 0, /* bitpos */
108 complain_overflow_bitfield, /* complain_on_overflow */
109 bfd_elf_generic_reloc, /* special_function */
110 "R_V850_NONE", /* name */
111 false, /* partial_inplace */
112 0, /* src_mask */
113 0, /* dst_mask */
114 false), /* pcrel_offset */
115
116 /* A PC relative 9 bit branch. */
117 HOWTO (R_V850_9_PCREL, /* type */
118 2, /* rightshift */
119 2, /* size (0 = byte, 1 = short, 2 = long) */
120 26, /* bitsize */
121 true, /* pc_relative */
122 0, /* bitpos */
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 */
130
131 /* A PC relative 22 bit branch. */
132 HOWTO (R_V850_22_PCREL, /* type */
133 2, /* rightshift */
134 2, /* size (0 = byte, 1 = short, 2 = long) */
135 22, /* bitsize */
136 true, /* pc_relative */
137 7, /* bitpos */
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 */
145
146 /* High 16 bits of symbol value. */
147 HOWTO (R_V850_HI16_S, /* type */
148 0, /* rightshift */
149 1, /* size (0 = byte, 1 = short, 2 = long) */
150 16, /* bitsize */
151 false, /* pc_relative */
152 0, /* bitpos */
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 */
160
161 /* High 16 bits of symbol value. */
162 HOWTO (R_V850_HI16, /* type */
163 0, /* rightshift */
164 1, /* size (0 = byte, 1 = short, 2 = long) */
165 16, /* bitsize */
166 false, /* pc_relative */
167 0, /* bitpos */
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 */
175
176 /* Low 16 bits of symbol value. */
177 HOWTO (R_V850_LO16, /* type */
178 0, /* rightshift */
179 1, /* size (0 = byte, 1 = short, 2 = long) */
180 16, /* bitsize */
181 false, /* pc_relative */
182 0, /* bitpos */
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 */
190
191 /* Simple 32bit reloc. */
192 HOWTO (R_V850_32, /* type */
193 0, /* rightshift */
194 2, /* size (0 = byte, 1 = short, 2 = long) */
195 32, /* bitsize */
196 false, /* pc_relative */
197 0, /* bitpos */
198 complain_overflow_dont, /* complain_on_overflow */
199 v850_elf_reloc, /* special_function */
200 "R_V850_32", /* name */
201 false, /* partial_inplace */
202 0xffffffff, /* src_mask */
203 0xffffffff, /* dst_mask */
204 false), /* pcrel_offset */
205
206 /* Simple 16bit reloc. */
207 HOWTO (R_V850_16, /* type */
208 0, /* rightshift */
209 1, /* size (0 = byte, 1 = short, 2 = long) */
210 16, /* bitsize */
211 false, /* pc_relative */
212 0, /* bitpos */
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 */
220
221 /* Simple 8bit reloc. */
222 HOWTO (R_V850_8, /* type */
223 0, /* rightshift */
224 0, /* size (0 = byte, 1 = short, 2 = long) */
225 8, /* bitsize */
226 false, /* pc_relative */
227 0, /* bitpos */
228 complain_overflow_dont, /* complain_on_overflow */
229 bfd_elf_generic_reloc, /* special_function */
230 "R_V850_8", /* name */
231 false, /* partial_inplace */
232 0xff, /* src_mask */
233 0xff, /* dst_mask */
234 false), /* pcrel_offset */
235
236 /* 16 bit offset from the short data area pointer. */
237 HOWTO (R_V850_SDA_16_16_OFFSET, /* type */
238 0, /* rightshift */
239 1, /* size (0 = byte, 1 = short, 2 = long) */
240 16, /* bitsize */
241 false, /* pc_relative */
242 0, /* bitpos */
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 */
250
251 /* 15 bit offset from the short data area pointer. */
252 HOWTO (R_V850_SDA_15_16_OFFSET, /* type */
253 1, /* rightshift */
254 1, /* size (0 = byte, 1 = short, 2 = long) */
255 16, /* bitsize */
256 false, /* pc_relative */
257 1, /* bitpos */
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 */
265
266 /* 16 bit offset from the zero data area pointer. */
267 HOWTO (R_V850_ZDA_16_16_OFFSET, /* type */
268 0, /* rightshift */
269 1, /* size (0 = byte, 1 = short, 2 = long) */
270 16, /* bitsize */
271 false, /* pc_relative */
272 0, /* bitpos */
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 */
280
281 /* 15 bit offset from the zero data area pointer. */
282 HOWTO (R_V850_ZDA_15_16_OFFSET, /* type */
283 1, /* rightshift */
284 1, /* size (0 = byte, 1 = short, 2 = long) */
285 16, /* bitsize */
286 false, /* pc_relative */
287 1, /* bitpos */
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 */
295
296 /* 6 bit offset from the tiny data area pointer. */
297 HOWTO (R_V850_TDA_6_8_OFFSET, /* type */
298 2, /* rightshift */
299 1, /* size (0 = byte, 1 = short, 2 = long) */
300 8, /* bitsize */
301 false, /* pc_relative */
302 1, /* bitpos */
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 */
307 0x7e, /* src_mask */
308 0x7e, /* dst_mask */
309 false), /* pcrel_offset */
310
311 /* 8 bit offset from the tiny data area pointer. */
312 HOWTO (R_V850_TDA_7_8_OFFSET, /* type */
313 1, /* rightshift */
314 1, /* size (0 = byte, 1 = short, 2 = long) */
315 8, /* bitsize */
316 false, /* pc_relative */
317 0, /* bitpos */
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 */
322 0x7f, /* src_mask */
323 0x7f, /* dst_mask */
324 false), /* pcrel_offset */
325
326 /* 7 bit offset from the tiny data area pointer. */
327 HOWTO (R_V850_TDA_7_7_OFFSET, /* type */
328 0, /* rightshift */
329 1, /* size (0 = byte, 1 = short, 2 = long) */
330 7, /* bitsize */
331 false, /* pc_relative */
332 0, /* bitpos */
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 */
337 0x7f, /* src_mask */
338 0x7f, /* dst_mask */
339 false), /* pcrel_offset */
340
341 /* 16 bit offset from the tiny data area pointer! */
342 HOWTO (R_V850_TDA_16_16_OFFSET, /* type */
343 0, /* rightshift */
344 1, /* size (0 = byte, 1 = short, 2 = long) */
345 16, /* bitsize */
346 false, /* pc_relative */
347 0, /* bitpos */
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 */
355
356 /* 5 bit offset from the tiny data area pointer. */
357 HOWTO (R_V850_TDA_4_5_OFFSET, /* type */
358 1, /* rightshift */
359 1, /* size (0 = byte, 1 = short, 2 = long) */
360 5, /* bitsize */
361 false, /* pc_relative */
362 0, /* bitpos */
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 */
367 0x0f, /* src_mask */
368 0x0f, /* dst_mask */
369 false), /* pcrel_offset */
370
371 /* 4 bit offset from the tiny data area pointer. */
372 HOWTO (R_V850_TDA_4_4_OFFSET, /* type */
373 0, /* rightshift */
374 1, /* size (0 = byte, 1 = short, 2 = long) */
375 4, /* bitsize */
376 false, /* pc_relative */
377 0, /* bitpos */
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 */
382 0x0f, /* src_mask */
383 0x0f, /* dst_mask */
384 false), /* pcrel_offset */
385
386 /* 16 bit offset from the short data area pointer. */
387 HOWTO (R_V850_SDA_16_16_SPLIT_OFFSET, /* type */
388 0, /* rightshift */
389 2, /* size (0 = byte, 1 = short, 2 = long) */
390 16, /* bitsize */
391 false, /* pc_relative */
392 0, /* bitpos */
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 */
400
401 /* 16 bit offset from the zero data area pointer. */
402 HOWTO (R_V850_ZDA_16_16_SPLIT_OFFSET, /* type */
403 0, /* rightshift */
404 2, /* size (0 = byte, 1 = short, 2 = long) */
405 16, /* bitsize */
406 false, /* pc_relative */
407 0, /* bitpos */
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 */
415
416 /* 6 bit offset from the call table base pointer. */
417 HOWTO (R_V850_CALLT_6_7_OFFSET, /* type */
418 0, /* rightshift */
419 1, /* size (0 = byte, 1 = short, 2 = long) */
420 7, /* bitsize */
421 false, /* pc_relative */
422 0, /* bitpos */
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 */
427 0x3f, /* src_mask */
428 0x3f, /* dst_mask */
429 false), /* pcrel_offset */
430
431 /* 16 bit offset from the call table base pointer. */
432 HOWTO (R_V850_CALLT_16_16_OFFSET, /* type */
433 0, /* rightshift */
434 1, /* size (0 = byte, 1 = short, 2 = long) */
435 16, /* bitsize */
436 false, /* pc_relative */
437 0, /* bitpos */
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 */
445
446 /* GNU extension to record C++ vtable hierarchy */
447 HOWTO (R_V850_GNU_VTINHERIT, /* type */
448 0, /* rightshift */
449 2, /* size (0 = byte, 1 = short, 2 = long) */
450 0, /* bitsize */
451 false, /* pc_relative */
452 0, /* bitpos */
453 complain_overflow_dont, /* complain_on_overflow */
454 NULL, /* special_function */
455 "R_V850_GNU_VTINHERIT", /* name */
456 false, /* partial_inplace */
457 0, /* src_mask */
458 0, /* dst_mask */
459 false), /* pcrel_offset */
460
461 /* GNU extension to record C++ vtable member usage */
462 HOWTO (R_V850_GNU_VTENTRY, /* type */
463 0, /* rightshift */
464 2, /* size (0 = byte, 1 = short, 2 = long) */
465 0, /* bitsize */
466 false, /* pc_relative */
467 0, /* bitpos */
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 */
472 0, /* src_mask */
473 0, /* dst_mask */
474 false), /* pcrel_offset */
475
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 */
479 0, /* rightshift */
480 2, /* size (0 = byte, 1 = short, 2 = long) */
481 32, /* bitsize */
482 true, /* pc_relative */
483 0, /* bitpos */
484 complain_overflow_signed, /* complain_on_overflow */
485 v850_elf_ignore_reloc, /* special_function */
486 "R_V850_LONGCALL", /* name */
487 false, /* partial_inplace */
488 0, /* src_mask */
489 0, /* dst_mask */
490 true), /* pcrel_offset */
491
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 */
495 0, /* rightshift */
496 2, /* size (0 = byte, 1 = short, 2 = long) */
497 32, /* bitsize */
498 true, /* pc_relative */
499 0, /* bitpos */
500 complain_overflow_signed, /* complain_on_overflow */
501 v850_elf_ignore_reloc, /* special_function */
502 "R_V850_LONGJUMP", /* name */
503 false, /* partial_inplace */
504 0, /* src_mask */
505 0, /* dst_mask */
506 true), /* pcrel_offset */
507
508 HOWTO (R_V850_ALIGN, /* type */
509 0, /* rightshift */
510 1, /* size (0 = byte, 1 = short, 2 = long) */
511 0, /* bitsize */
512 false, /* pc_relative */
513 0, /* bitpos */
514 complain_overflow_unsigned, /* complain_on_overflow */
515 v850_elf_ignore_reloc, /* special_function */
516 "R_V850_ALIGN", /* name */
517 false, /* partial_inplace */
518 0, /* src_mask */
519 0, /* dst_mask */
520 true), /* pcrel_offset */
521 };
522
523 /* Map BFD reloc types to V850 ELF reloc types. */
524
525 struct v850_elf_reloc_map
526 {
527 /* BFD_RELOC_V850_CALLT_16_16_OFFSET is 258, which will not fix in an
528 unsigned char. */
529 bfd_reloc_code_real_type bfd_reloc_val;
530 unsigned int elf_reloc_val;
531 };
532
533 static const struct v850_elf_reloc_map v850_elf_reloc_map[] =
534 {
535 { BFD_RELOC_NONE, R_V850_NONE },
536 { BFD_RELOC_V850_9_PCREL, R_V850_9_PCREL },
537 { BFD_RELOC_V850_22_PCREL, R_V850_22_PCREL },
538 { BFD_RELOC_HI16_S, R_V850_HI16_S },
539 { BFD_RELOC_HI16, R_V850_HI16 },
540 { BFD_RELOC_LO16, R_V850_LO16 },
541 { BFD_RELOC_32, R_V850_32 },
542 { BFD_RELOC_16, R_V850_16 },
543 { BFD_RELOC_8, R_V850_8 },
544 { BFD_RELOC_V850_SDA_16_16_OFFSET, R_V850_SDA_16_16_OFFSET },
545 { BFD_RELOC_V850_SDA_15_16_OFFSET, R_V850_SDA_15_16_OFFSET },
546 { BFD_RELOC_V850_ZDA_16_16_OFFSET, R_V850_ZDA_16_16_OFFSET },
547 { BFD_RELOC_V850_ZDA_15_16_OFFSET, R_V850_ZDA_15_16_OFFSET },
548 { BFD_RELOC_V850_TDA_6_8_OFFSET, R_V850_TDA_6_8_OFFSET },
549 { BFD_RELOC_V850_TDA_7_8_OFFSET, R_V850_TDA_7_8_OFFSET },
550 { BFD_RELOC_V850_TDA_7_7_OFFSET, R_V850_TDA_7_7_OFFSET },
551 { BFD_RELOC_V850_TDA_16_16_OFFSET, R_V850_TDA_16_16_OFFSET },
552 { BFD_RELOC_V850_TDA_4_5_OFFSET, R_V850_TDA_4_5_OFFSET },
553 { BFD_RELOC_V850_TDA_4_4_OFFSET, R_V850_TDA_4_4_OFFSET },
554 { BFD_RELOC_V850_SDA_16_16_SPLIT_OFFSET, R_V850_SDA_16_16_SPLIT_OFFSET },
555 { BFD_RELOC_V850_ZDA_16_16_SPLIT_OFFSET, R_V850_ZDA_16_16_SPLIT_OFFSET },
556 { BFD_RELOC_V850_CALLT_6_7_OFFSET, R_V850_CALLT_6_7_OFFSET },
557 { BFD_RELOC_V850_CALLT_16_16_OFFSET, R_V850_CALLT_16_16_OFFSET },
558 { BFD_RELOC_VTABLE_INHERIT, R_V850_GNU_VTINHERIT },
559 { BFD_RELOC_VTABLE_ENTRY, R_V850_GNU_VTENTRY },
560 { BFD_RELOC_V850_LONGCALL, R_V850_LONGCALL },
561 { BFD_RELOC_V850_LONGJUMP, R_V850_LONGJUMP },
562 { BFD_RELOC_V850_ALIGN, R_V850_ALIGN },
563
564 };
565 \f
566 /* Map a bfd relocation into the appropriate howto structure. */
567
568 static reloc_howto_type *
569 v850_elf_reloc_type_lookup (abfd, code)
570 bfd * abfd ATTRIBUTE_UNUSED;
571 bfd_reloc_code_real_type code;
572 {
573 unsigned int i;
574
575 for (i = ARRAY_SIZE (v850_elf_reloc_map); i --;)
576 if (v850_elf_reloc_map[i].bfd_reloc_val == code)
577 {
578 unsigned int elf_reloc_val = v850_elf_reloc_map[i].elf_reloc_val;
579
580 BFD_ASSERT (v850_elf_howto_table[elf_reloc_val].type == elf_reloc_val);
581
582 return v850_elf_howto_table + elf_reloc_val;
583 }
584
585 return NULL;
586 }
587 \f
588 /* Set the howto pointer for an V850 ELF reloc. */
589
590 static void
591 v850_elf_info_to_howto_rel (abfd, cache_ptr, dst)
592 bfd * abfd ATTRIBUTE_UNUSED;
593 arelent * cache_ptr;
594 Elf32_Internal_Rel * dst;
595 {
596 unsigned int r_type;
597
598 r_type = ELF32_R_TYPE (dst->r_info);
599 BFD_ASSERT (r_type < (unsigned int) R_V850_max);
600 cache_ptr->howto = &v850_elf_howto_table[r_type];
601 }
602
603 /* Set the howto pointer for a V850 ELF reloc (type RELA). */
604 static void
605 v850_elf_info_to_howto_rela (abfd, cache_ptr, dst)
606 bfd * abfd ATTRIBUTE_UNUSED;
607 arelent * cache_ptr;
608 Elf32_Internal_Rela *dst;
609 {
610 unsigned int r_type;
611
612 r_type = ELF32_R_TYPE (dst->r_info);
613 BFD_ASSERT (r_type < (unsigned int) R_V850_max);
614 cache_ptr->howto = &v850_elf_howto_table[r_type];
615 }
616 \f
617 /* Look through the relocs for a section during the first phase, and
618 allocate space in the global offset table or procedure linkage
619 table. */
620
621 static boolean
622 v850_elf_check_relocs (abfd, info, sec, relocs)
623 bfd * abfd;
624 struct bfd_link_info * info;
625 asection * sec;
626 const Elf_Internal_Rela * relocs;
627 {
628 boolean ret = true;
629 bfd *dynobj;
630 Elf_Internal_Shdr *symtab_hdr;
631 struct elf_link_hash_entry **sym_hashes;
632 const Elf_Internal_Rela *rel;
633 const Elf_Internal_Rela *rel_end;
634 asection *sreloc;
635 enum v850_reloc_type r_type;
636 int other = 0;
637 const char *common = (const char *)0;
638
639 if (info->relocateable)
640 return true;
641
642 #ifdef DEBUG
643 fprintf (stderr, "v850_elf_check_relocs called for section %s in %s\n",
644 bfd_get_section_name (abfd, sec),
645 bfd_archive_filename (abfd));
646 #endif
647
648 dynobj = elf_hash_table (info)->dynobj;
649 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
650 sym_hashes = elf_sym_hashes (abfd);
651 sreloc = NULL;
652
653 rel_end = relocs + sec->reloc_count;
654 for (rel = relocs; rel < rel_end; rel++)
655 {
656 unsigned long r_symndx;
657 struct elf_link_hash_entry *h;
658
659 r_symndx = ELF32_R_SYM (rel->r_info);
660 if (r_symndx < symtab_hdr->sh_info)
661 h = NULL;
662 else
663 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
664
665 r_type = (enum v850_reloc_type) ELF32_R_TYPE (rel->r_info);
666 switch (r_type)
667 {
668 default:
669 case R_V850_NONE:
670 case R_V850_9_PCREL:
671 case R_V850_22_PCREL:
672 case R_V850_HI16_S:
673 case R_V850_HI16:
674 case R_V850_LO16:
675 case R_V850_32:
676 case R_V850_16:
677 case R_V850_8:
678 case R_V850_CALLT_6_7_OFFSET:
679 case R_V850_CALLT_16_16_OFFSET:
680 break;
681
682 /* This relocation describes the C++ object vtable hierarchy.
683 Reconstruct it for later use during GC. */
684 case R_V850_GNU_VTINHERIT:
685 if (!_bfd_elf32_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
686 return false;
687 break;
688
689 /* This relocation describes which C++ vtable entries
690 are actually used. Record for later use during GC. */
691 case R_V850_GNU_VTENTRY:
692 if (!_bfd_elf32_gc_record_vtentry (abfd, sec, h, rel->r_addend))
693 return false;
694 break;
695
696 case R_V850_SDA_16_16_SPLIT_OFFSET:
697 case R_V850_SDA_16_16_OFFSET:
698 case R_V850_SDA_15_16_OFFSET:
699 other = V850_OTHER_SDA;
700 common = ".scommon";
701 goto small_data_common;
702
703 case R_V850_ZDA_16_16_SPLIT_OFFSET:
704 case R_V850_ZDA_16_16_OFFSET:
705 case R_V850_ZDA_15_16_OFFSET:
706 other = V850_OTHER_ZDA;
707 common = ".zcommon";
708 goto small_data_common;
709
710 case R_V850_TDA_4_5_OFFSET:
711 case R_V850_TDA_4_4_OFFSET:
712 case R_V850_TDA_6_8_OFFSET:
713 case R_V850_TDA_7_8_OFFSET:
714 case R_V850_TDA_7_7_OFFSET:
715 case R_V850_TDA_16_16_OFFSET:
716 other = V850_OTHER_TDA;
717 common = ".tcommon";
718 /* fall through */
719
720 #define V850_OTHER_MASK (V850_OTHER_TDA | V850_OTHER_SDA | V850_OTHER_ZDA)
721
722 small_data_common:
723 if (h)
724 {
725 /* Flag which type of relocation was used. */
726 h->other |= other;
727 if ((h->other & V850_OTHER_MASK) != (other & V850_OTHER_MASK)
728 && (h->other & V850_OTHER_ERROR) == 0)
729 {
730 const char * msg;
731 static char buff[200]; /* XXX */
732
733 switch (h->other & V850_OTHER_MASK)
734 {
735 default:
736 msg = _("Variable `%s' cannot occupy in multiple small data regions");
737 break;
738 case V850_OTHER_SDA | V850_OTHER_ZDA | V850_OTHER_TDA:
739 msg = _("Variable `%s' can only be in one of the small, zero, and tiny data regions");
740 break;
741 case V850_OTHER_SDA | V850_OTHER_ZDA:
742 msg = _("Variable `%s' cannot be in both small and zero data regions simultaneously");
743 break;
744 case V850_OTHER_SDA | V850_OTHER_TDA:
745 msg = _("Variable `%s' cannot be in both small and tiny data regions simultaneously");
746 break;
747 case V850_OTHER_ZDA | V850_OTHER_TDA:
748 msg = _("Variable `%s' cannot be in both zero and tiny data regions simultaneously");
749 break;
750 }
751
752 sprintf (buff, msg, h->root.root.string);
753 info->callbacks->warning (info, buff, h->root.root.string,
754 abfd, h->root.u.def.section,
755 (bfd_vma) 0);
756
757 bfd_set_error (bfd_error_bad_value);
758 h->other |= V850_OTHER_ERROR;
759 ret = false;
760 }
761 }
762
763 if (h && h->root.type == bfd_link_hash_common
764 && h->root.u.c.p
765 && !strcmp (bfd_get_section_name (abfd, h->root.u.c.p->section), "COMMON"))
766 {
767 asection * section;
768
769 section = h->root.u.c.p->section = bfd_make_section_old_way (abfd, common);
770 section->flags |= SEC_IS_COMMON;
771 }
772
773 #ifdef DEBUG
774 fprintf (stderr, "v850_elf_check_relocs, found %s relocation for %s%s\n",
775 v850_elf_howto_table[ (int)r_type ].name,
776 (h && h->root.root.string) ? h->root.root.string : "<unknown>",
777 (h->root.type == bfd_link_hash_common) ? ", symbol is common" : "");
778 #endif
779 break;
780 }
781 }
782
783 return ret;
784 }
785
786 /* In the old version, when an entry was checked out from the table,
787 it was deleted. This produced an error if the entry was needed
788 more than once, as the second attempted retry failed.
789
790 In the current version, the entry is not deleted, instead we set
791 the field 'found' to true. If a second lookup matches the same
792 entry, then we know that the hi16s reloc has already been updated
793 and does not need to be updated a second time.
794
795 TODO - TOFIX: If it is possible that we need to restore 2 different
796 addresses from the same table entry, where the first generates an
797 overflow, whilst the second do not, then this code will fail. */
798
799 typedef struct hi16s_location
800 {
801 bfd_vma addend;
802 bfd_byte * address;
803 unsigned long counter;
804 boolean found;
805 struct hi16s_location * next;
806 }
807 hi16s_location;
808
809 static hi16s_location * previous_hi16s;
810 static hi16s_location * free_hi16s;
811 static unsigned long hi16s_counter;
812
813 static void
814 remember_hi16s_reloc (abfd, addend, address)
815 bfd * abfd;
816 bfd_vma addend;
817 bfd_byte * address;
818 {
819 hi16s_location * entry = NULL;
820 bfd_size_type amt = sizeof (* free_hi16s);
821
822 /* Find a free structure. */
823 if (free_hi16s == NULL)
824 free_hi16s = (hi16s_location *) bfd_zalloc (abfd, amt);
825
826 entry = free_hi16s;
827 free_hi16s = free_hi16s->next;
828
829 entry->addend = addend;
830 entry->address = address;
831 entry->counter = hi16s_counter ++;
832 entry->found = false;
833 entry->next = previous_hi16s;
834 previous_hi16s = entry;
835
836 /* Cope with wrap around of our counter. */
837 if (hi16s_counter == 0)
838 {
839 /* XXX - Assume that all counter entries differ only in their low 16 bits. */
840 for (entry = previous_hi16s; entry != NULL; entry = entry->next)
841 entry->counter &= 0xffff;
842
843 hi16s_counter = 0x10000;
844 }
845
846 return;
847 }
848
849 static bfd_byte *
850 find_remembered_hi16s_reloc (addend, already_found)
851 bfd_vma addend;
852 boolean * already_found;
853 {
854 hi16s_location * match = NULL;
855 hi16s_location * entry;
856 hi16s_location * previous = NULL;
857 hi16s_location * prev;
858 bfd_byte * addr;
859
860 /* Search the table. Record the most recent entry that matches. */
861 for (entry = previous_hi16s; entry; entry = entry->next)
862 {
863 if (entry->addend == addend
864 && (match == NULL || match->counter < entry->counter))
865 {
866 previous = prev;
867 match = entry;
868 }
869
870 prev = entry;
871 }
872
873 if (match == NULL)
874 return NULL;
875
876 /* Extract the address. */
877 addr = match->address;
878
879 /* Remeber if this entry has already been used before. */
880 if (already_found)
881 * already_found = match->found;
882
883 /* Note that this entry has now been used. */
884 match->found = true;
885
886 return addr;
887 }
888
889 /* FIXME: The code here probably ought to be removed and the code in reloc.c
890 allowed to do its stuff instead. At least for most of the relocs, anwyay. */
891
892 static bfd_reloc_status_type
893 v850_elf_perform_relocation (abfd, r_type, addend, address)
894 bfd *abfd;
895 unsigned int r_type;
896 bfd_vma addend;
897 bfd_byte *address;
898 {
899 unsigned long insn;
900 bfd_signed_vma saddend = (bfd_signed_vma) addend;
901
902 switch (r_type)
903 {
904 default:
905 /* fprintf (stderr, "reloc type %d not SUPPORTED\n", r_type ); */
906 return bfd_reloc_notsupported;
907
908 case R_V850_32:
909 bfd_put_32 (abfd, addend, address);
910 return bfd_reloc_ok;
911
912 case R_V850_22_PCREL:
913 if (saddend > 0x1fffff || saddend < -0x200000)
914 return bfd_reloc_overflow;
915
916 if ((addend % 2) != 0)
917 return bfd_reloc_dangerous;
918
919 insn = bfd_get_32 (abfd, address);
920 insn &= ~0xfffe003f;
921 insn |= (((addend & 0xfffe) << 16) | ((addend & 0x3f0000) >> 16));
922 bfd_put_32 (abfd, (bfd_vma) insn, address);
923 return bfd_reloc_ok;
924
925 case R_V850_9_PCREL:
926 if (saddend > 0xff || saddend < -0x100)
927 return bfd_reloc_overflow;
928
929 if ((addend % 2) != 0)
930 return bfd_reloc_dangerous;
931
932 insn = bfd_get_16 (abfd, address);
933 insn &= ~ 0xf870;
934 insn |= ((addend & 0x1f0) << 7) | ((addend & 0x0e) << 3);
935 break;
936
937 case R_V850_HI16:
938 addend += (bfd_get_16 (abfd, address) << 16);
939 addend = (addend >> 16);
940 insn = addend;
941 break;
942
943 case R_V850_HI16_S:
944 /* Remember where this relocation took place. */
945 remember_hi16s_reloc (abfd, addend, address);
946
947 addend += (bfd_get_16 (abfd, address) << 16);
948 addend = (addend >> 16) + ((addend & 0x8000) != 0);
949
950 /* This relocation cannot overflow. */
951 if (addend > 0x7fff)
952 addend = 0;
953
954 insn = addend;
955 break;
956
957 case R_V850_LO16:
958 /* Calculate the sum of the value stored in the instruction and the
959 addend and check for overflow from the low 16 bits into the high
960 16 bits. The assembler has already done some of this: If the
961 value stored in the instruction has its 15th bit set, (counting
962 from zero) then the assembler will have added 1 to the value
963 stored in the associated HI16S reloc. So for example, these
964 relocations:
965
966 movhi hi( fred ), r0, r1
967 movea lo( fred ), r1, r1
968
969 will store 0 in the value fields for the MOVHI and MOVEA instructions
970 and addend will be the address of fred, but for these instructions:
971
972 movhi hi( fred + 0x123456), r0, r1
973 movea lo( fred + 0x123456), r1, r1
974
975 the value stored in the MOVHI instruction will be 0x12 and the value
976 stored in the MOVEA instruction will be 0x3456. If however the
977 instructions were:
978
979 movhi hi( fred + 0x10ffff), r0, r1
980 movea lo( fred + 0x10ffff), r1, r1
981
982 then the value stored in the MOVHI instruction would be 0x11 (not
983 0x10) and the value stored in the MOVEA instruction would be 0xffff.
984 Thus (assuming for the moment that the addend is 0), at run time the
985 MOVHI instruction loads 0x110000 into r1, then the MOVEA instruction
986 adds 0xffffffff (sign extension!) producing 0x10ffff. Similarly if
987 the instructions were:
988
989 movhi hi( fred - 1), r0, r1
990 movea lo( fred - 1), r1, r1
991
992 then 0 is stored in the MOVHI instruction and -1 is stored in the
993 MOVEA instruction.
994
995 Overflow can occur if the addition of the value stored in the
996 instruction plus the addend sets the 15th bit when before it was clear.
997 This is because the 15th bit will be sign extended into the high part,
998 thus reducing its value by one, but since the 15th bit was originally
999 clear, the assembler will not have added 1 to the previous HI16S reloc
1000 to compensate for this effect. For example:
1001
1002 movhi hi( fred + 0x123456), r0, r1
1003 movea lo( fred + 0x123456), r1, r1
1004
1005 The value stored in HI16S reloc is 0x12, the value stored in the LO16
1006 reloc is 0x3456. If we assume that the address of fred is 0x00007000
1007 then the relocations become:
1008
1009 HI16S: 0x0012 + (0x00007000 >> 16) = 0x12
1010 LO16: 0x3456 + (0x00007000 & 0xffff) = 0xa456
1011
1012 but when the instructions are executed, the MOVEA instruction's value
1013 is signed extended, so the sum becomes:
1014
1015 0x00120000
1016 + 0xffffa456
1017 ------------
1018 0x0011a456 but 'fred + 0x123456' = 0x0012a456
1019
1020 Note that if the 15th bit was set in the value stored in the LO16
1021 reloc, then we do not have to do anything:
1022
1023 movhi hi( fred + 0x10ffff), r0, r1
1024 movea lo( fred + 0x10ffff), r1, r1
1025
1026 HI16S: 0x0011 + (0x00007000 >> 16) = 0x11
1027 LO16: 0xffff + (0x00007000 & 0xffff) = 0x6fff
1028
1029 0x00110000
1030 + 0x00006fff
1031 ------------
1032 0x00116fff = fred + 0x10ffff = 0x7000 + 0x10ffff
1033
1034 Overflow can also occur if the computation carries into the 16th bit
1035 and it also results in the 15th bit having the same value as the 15th
1036 bit of the original value. What happens is that the HI16S reloc
1037 will have already examined the 15th bit of the original value and
1038 added 1 to the high part if the bit is set. This compensates for the
1039 sign extension of 15th bit of the result of the computation. But now
1040 there is a carry into the 16th bit, and this has not been allowed for.
1041
1042 So, for example if fred is at address 0xf000:
1043
1044 movhi hi( fred + 0xffff), r0, r1 [bit 15 of the offset is set]
1045 movea lo( fred + 0xffff), r1, r1
1046
1047 HI16S: 0x0001 + (0x0000f000 >> 16) = 0x0001
1048 LO16: 0xffff + (0x0000f000 & 0xffff) = 0xefff (carry into bit 16 is lost)
1049
1050 0x00010000
1051 + 0xffffefff
1052 ------------
1053 0x0000efff but 'fred + 0xffff' = 0x0001efff
1054
1055 Similarly, if the 15th bit remains clear, but overflow occurs into
1056 the 16th bit then (assuming the address of fred is 0xf000):
1057
1058 movhi hi( fred + 0x7000), r0, r1 [bit 15 of the offset is clear]
1059 movea lo( fred + 0x7000), r1, r1
1060
1061 HI16S: 0x0000 + (0x0000f000 >> 16) = 0x0000
1062 LO16: 0x7000 + (0x0000f000 & 0xffff) = 0x6fff (carry into bit 16 is lost)
1063
1064 0x00000000
1065 + 0x00006fff
1066 ------------
1067 0x00006fff but 'fred + 0x7000' = 0x00016fff
1068
1069 Note - there is no need to change anything if a carry occurs, and the
1070 15th bit changes its value from being set to being clear, as the HI16S
1071 reloc will have already added in 1 to the high part for us:
1072
1073 movhi hi( fred + 0xffff), r0, r1 [bit 15 of the offset is set]
1074 movea lo( fred + 0xffff), r1, r1
1075
1076 HI16S: 0x0001 + (0x00007000 >> 16)
1077 LO16: 0xffff + (0x00007000 & 0xffff) = 0x6fff (carry into bit 16 is lost)
1078
1079 0x00010000
1080 + 0x00006fff (bit 15 not set, so the top half is zero)
1081 ------------
1082 0x00016fff which is right (assuming that fred is at 0x7000)
1083
1084 but if the 15th bit goes from being clear to being set, then we must
1085 once again handle overflow:
1086
1087 movhi hi( fred + 0x7000), r0, r1 [bit 15 of the offset is clear]
1088 movea lo( fred + 0x7000), r1, r1
1089
1090 HI16S: 0x0000 + (0x0000ffff >> 16)
1091 LO16: 0x7000 + (0x0000ffff & 0xffff) = 0x6fff (carry into bit 16)
1092
1093 0x00000000
1094 + 0x00006fff (bit 15 not set, so the top half is zero)
1095 ------------
1096 0x00006fff which is wrong (assuming that fred is at 0xffff). */
1097 {
1098 long result;
1099
1100 insn = bfd_get_16 (abfd, address);
1101 result = insn + addend;
1102
1103 #define BIT15_SET(x) ((x) & 0x8000)
1104 #define OVERFLOWS(a,i) ((((a) & 0xffff) + (i)) > 0xffff)
1105
1106 if ((BIT15_SET (result) && ! BIT15_SET (addend))
1107 || (OVERFLOWS (addend, insn)
1108 && ((! BIT15_SET (insn)) || (BIT15_SET (addend)))))
1109 {
1110 boolean already_updated;
1111 bfd_byte * hi16s_address = find_remembered_hi16s_reloc
1112 (addend, & already_updated);
1113
1114 /* Amend the matching HI16_S relocation. */
1115 if (hi16s_address != NULL)
1116 {
1117 if (! already_updated)
1118 {
1119 insn = bfd_get_16 (abfd, hi16s_address);
1120 insn += 1;
1121 bfd_put_16 (abfd, (bfd_vma) insn, hi16s_address);
1122 }
1123 }
1124 else
1125 {
1126 fprintf (stderr, _("FAILED to find previous HI16 reloc\n"));
1127 return bfd_reloc_overflow;
1128 }
1129 }
1130
1131 /* Do not complain if value has top bit set, as this has been anticipated. */
1132 insn = result & 0xffff;
1133 break;
1134 }
1135
1136 case R_V850_8:
1137 addend += (char) bfd_get_8 (abfd, address);
1138
1139 saddend = (bfd_signed_vma) addend;
1140
1141 if (saddend > 0x7f || saddend < -0x80)
1142 return bfd_reloc_overflow;
1143
1144 bfd_put_8 (abfd, addend, address);
1145 return bfd_reloc_ok;
1146
1147 case R_V850_CALLT_16_16_OFFSET:
1148 addend += bfd_get_16 (abfd, address);
1149
1150 saddend = (bfd_signed_vma) addend;
1151
1152 if (saddend > 0xffff || saddend < 0)
1153 return bfd_reloc_overflow;
1154
1155 insn = addend;
1156 break;
1157
1158 case R_V850_16:
1159
1160 /* drop through */
1161 case R_V850_SDA_16_16_OFFSET:
1162 case R_V850_ZDA_16_16_OFFSET:
1163 case R_V850_TDA_16_16_OFFSET:
1164 addend += bfd_get_16 (abfd, address);
1165
1166 saddend = (bfd_signed_vma) addend;
1167
1168 if (saddend > 0x7fff || saddend < -0x8000)
1169 return bfd_reloc_overflow;
1170
1171 insn = addend;
1172 break;
1173
1174 case R_V850_SDA_15_16_OFFSET:
1175 case R_V850_ZDA_15_16_OFFSET:
1176 insn = bfd_get_16 (abfd, address);
1177 addend += (insn & 0xfffe);
1178
1179 saddend = (bfd_signed_vma) addend;
1180
1181 if (saddend > 0x7ffe || saddend < -0x8000)
1182 return bfd_reloc_overflow;
1183
1184 if (addend & 1)
1185 return bfd_reloc_dangerous;
1186
1187 insn = (addend &~ (bfd_vma) 1) | (insn & 1);
1188 break;
1189
1190 case R_V850_TDA_6_8_OFFSET:
1191 insn = bfd_get_16 (abfd, address);
1192 addend += ((insn & 0x7e) << 1);
1193
1194 saddend = (bfd_signed_vma) addend;
1195
1196 if (saddend > 0xfc || saddend < 0)
1197 return bfd_reloc_overflow;
1198
1199 if (addend & 3)
1200 return bfd_reloc_dangerous;
1201
1202 insn &= 0xff81;
1203 insn |= (addend >> 1);
1204 break;
1205
1206 case R_V850_TDA_7_8_OFFSET:
1207 insn = bfd_get_16 (abfd, address);
1208 addend += ((insn & 0x7f) << 1);
1209
1210 saddend = (bfd_signed_vma) addend;
1211
1212 if (saddend > 0xfe || saddend < 0)
1213 return bfd_reloc_overflow;
1214
1215 if (addend & 1)
1216 return bfd_reloc_dangerous;
1217
1218 insn &= 0xff80;
1219 insn |= (addend >> 1);
1220 break;
1221
1222 case R_V850_TDA_7_7_OFFSET:
1223 insn = bfd_get_16 (abfd, address);
1224 addend += insn & 0x7f;
1225
1226 saddend = (bfd_signed_vma) addend;
1227
1228 if (saddend > 0x7f || saddend < 0)
1229 return bfd_reloc_overflow;
1230
1231 insn &= 0xff80;
1232 insn |= addend;
1233 break;
1234
1235 case R_V850_TDA_4_5_OFFSET:
1236 insn = bfd_get_16 (abfd, address);
1237 addend += ((insn & 0xf) << 1);
1238
1239 saddend = (bfd_signed_vma) addend;
1240
1241 if (saddend > 0x1e || saddend < 0)
1242 return bfd_reloc_overflow;
1243
1244 if (addend & 1)
1245 return bfd_reloc_dangerous;
1246
1247 insn &= 0xfff0;
1248 insn |= (addend >> 1);
1249 break;
1250
1251 case R_V850_TDA_4_4_OFFSET:
1252 insn = bfd_get_16 (abfd, address);
1253 addend += insn & 0xf;
1254
1255 saddend = (bfd_signed_vma) addend;
1256
1257 if (saddend > 0xf || saddend < 0)
1258 return bfd_reloc_overflow;
1259
1260 insn &= 0xfff0;
1261 insn |= addend;
1262 break;
1263
1264 case R_V850_ZDA_16_16_SPLIT_OFFSET:
1265 case R_V850_SDA_16_16_SPLIT_OFFSET:
1266 insn = bfd_get_32 (abfd, address);
1267 addend += ((insn & 0xfffe0000) >> 16) + ((insn & 0x20) >> 5);
1268
1269 saddend = (bfd_signed_vma) addend;
1270
1271 if (saddend > 0x7fff || saddend < -0x8000)
1272 return bfd_reloc_overflow;
1273
1274 insn &= 0x0001ffdf;
1275 insn |= (addend & 1) << 5;
1276 insn |= (addend &~ (bfd_vma) 1) << 16;
1277
1278 bfd_put_32 (abfd, (bfd_vma) insn, address);
1279 return bfd_reloc_ok;
1280
1281 case R_V850_CALLT_6_7_OFFSET:
1282 insn = bfd_get_16 (abfd, address);
1283 addend += ((insn & 0x3f) << 1);
1284
1285 saddend = (bfd_signed_vma) addend;
1286
1287 if (saddend > 0x7e || saddend < 0)
1288 return bfd_reloc_overflow;
1289
1290 if (addend & 1)
1291 return bfd_reloc_dangerous;
1292
1293 insn &= 0xff80;
1294 insn |= (addend >> 1);
1295 break;
1296
1297 case R_V850_GNU_VTINHERIT:
1298 case R_V850_GNU_VTENTRY:
1299 return bfd_reloc_ok;
1300
1301 }
1302
1303 bfd_put_16 (abfd, (bfd_vma) insn, address);
1304 return bfd_reloc_ok;
1305 }
1306 \f
1307 /* Insert the addend into the instruction. */
1308
1309 static bfd_reloc_status_type
1310 v850_elf_reloc (abfd, reloc, symbol, data, isection, obfd, err)
1311 bfd * abfd ATTRIBUTE_UNUSED;
1312 arelent * reloc;
1313 asymbol * symbol;
1314 PTR data ATTRIBUTE_UNUSED;
1315 asection * isection;
1316 bfd * obfd;
1317 char ** err ATTRIBUTE_UNUSED;
1318 {
1319 long relocation;
1320
1321 /* If there is an output BFD,
1322 and the symbol is not a section name (which is only defined at final link time),
1323 and either we are not putting the addend into the instruction
1324 or the addend is zero, so there is nothing to add into the instruction
1325 then just fixup the address and return. */
1326 if (obfd != (bfd *) NULL
1327 && (symbol->flags & BSF_SECTION_SYM) == 0
1328 && (! reloc->howto->partial_inplace
1329 || reloc->addend == 0))
1330 {
1331 reloc->address += isection->output_offset;
1332 return bfd_reloc_ok;
1333 }
1334 #if 0
1335 else if (obfd != NULL)
1336 return bfd_reloc_continue;
1337 #endif
1338
1339 /* Catch relocs involving undefined symbols. */
1340 if (bfd_is_und_section (symbol->section)
1341 && (symbol->flags & BSF_WEAK) == 0
1342 && obfd == NULL)
1343 return bfd_reloc_undefined;
1344
1345 /* We handle final linking of some relocs ourselves. */
1346
1347 /* Is the address of the relocation really within the section? */
1348 if (reloc->address > isection->_cooked_size)
1349 return bfd_reloc_outofrange;
1350
1351 /* Work out which section the relocation is targetted at and the
1352 initial relocation command value. */
1353
1354 if (reloc->howto->pc_relative == true)
1355 return bfd_reloc_ok;
1356
1357 /* Get symbol value. (Common symbols are special.) */
1358 if (bfd_is_com_section (symbol->section))
1359 relocation = 0;
1360 else
1361 relocation = symbol->value;
1362
1363 /* Convert input-section-relative symbol value to absolute + addend. */
1364 relocation += symbol->section->output_section->vma;
1365 relocation += symbol->section->output_offset;
1366 relocation += reloc->addend;
1367
1368 #if 0 /* Since this reloc is going to be processed later on, we should
1369 not make it pc-relative here. To test this, try assembling and
1370 linking this program:
1371
1372 .text
1373 .globl _start
1374 nop
1375 _start:
1376 jr foo
1377
1378 .section ".foo","ax"
1379 nop
1380 foo:
1381 nop */
1382 if (reloc->howto->pc_relative)
1383 {
1384 /* Here the variable relocation holds the final address of the
1385 symbol we are relocating against, plus any addend. */
1386 relocation -= isection->output_section->vma + isection->output_offset;
1387
1388 /* Deal with pcrel_offset. */
1389 relocation -= reloc->address;
1390 }
1391 #endif
1392 reloc->addend = relocation;
1393 return bfd_reloc_ok;
1394 }
1395
1396 /* This function is used for relocs which are only used
1397 for relaxing, which the linker should otherwise ignore. */
1398
1399 static bfd_reloc_status_type
1400 v850_elf_ignore_reloc (abfd, reloc_entry, symbol, data, input_section,
1401 output_bfd, error_message)
1402 bfd * abfd ATTRIBUTE_UNUSED;
1403 arelent * reloc_entry;
1404 asymbol * symbol ATTRIBUTE_UNUSED;
1405 PTR data ATTRIBUTE_UNUSED;
1406 asection * input_section;
1407 bfd * output_bfd;
1408 char ** error_message ATTRIBUTE_UNUSED;
1409 {
1410 if (output_bfd != NULL)
1411 reloc_entry->address += input_section->output_offset;
1412
1413 return bfd_reloc_ok;
1414 }
1415 \f
1416 static boolean
1417 v850_elf_is_local_label_name (abfd, name)
1418 bfd * abfd ATTRIBUTE_UNUSED;
1419 const char * name;
1420 {
1421 return ( (name[0] == '.' && (name[1] == 'L' || name[1] == '.'))
1422 || (name[0] == '_' && name[1] == '.' && name[2] == 'L' && name[3] == '_'));
1423 }
1424 \f
1425 /* Perform a relocation as part of a final link. */
1426
1427 static bfd_reloc_status_type
1428 v850_elf_final_link_relocate (howto, input_bfd, output_bfd,
1429 input_section, contents, offset, value,
1430 addend, info, sym_sec, is_local)
1431 reloc_howto_type * howto;
1432 bfd * input_bfd;
1433 bfd * output_bfd ATTRIBUTE_UNUSED;
1434 asection * input_section;
1435 bfd_byte * contents;
1436 bfd_vma offset;
1437 bfd_vma value;
1438 bfd_vma addend;
1439 struct bfd_link_info * info;
1440 asection * sym_sec;
1441 int is_local ATTRIBUTE_UNUSED;
1442 {
1443 unsigned int r_type = howto->type;
1444 bfd_byte * hit_data = contents + offset;
1445
1446 /* Adjust the value according to the relocation. */
1447 switch (r_type)
1448 {
1449 case R_V850_9_PCREL:
1450 value -= (input_section->output_section->vma
1451 + input_section->output_offset);
1452 value -= offset;
1453 break;
1454
1455 case R_V850_22_PCREL:
1456 value -= (input_section->output_section->vma
1457 + input_section->output_offset
1458 + offset);
1459
1460 /* If the sign extension will corrupt the value then we have overflowed. */
1461 if (((value & 0xff000000) != 0x0) && ((value & 0xff000000) != 0xff000000))
1462 return bfd_reloc_overflow;
1463
1464 /* Only the bottom 24 bits of the PC are valid */
1465 value = SEXT24 (value);
1466 break;
1467
1468 case R_V850_HI16_S:
1469 case R_V850_HI16:
1470 case R_V850_LO16:
1471 case R_V850_16:
1472 case R_V850_32:
1473 case R_V850_8:
1474 break;
1475
1476 case R_V850_ZDA_15_16_OFFSET:
1477 case R_V850_ZDA_16_16_OFFSET:
1478 case R_V850_ZDA_16_16_SPLIT_OFFSET:
1479 if (sym_sec == NULL)
1480 return bfd_reloc_undefined;
1481
1482 value -= sym_sec->output_section->vma;
1483 break;
1484
1485 case R_V850_SDA_15_16_OFFSET:
1486 case R_V850_SDA_16_16_OFFSET:
1487 case R_V850_SDA_16_16_SPLIT_OFFSET:
1488 {
1489 unsigned long gp;
1490 struct bfd_link_hash_entry * h;
1491
1492 if (sym_sec == NULL)
1493 return bfd_reloc_undefined;
1494
1495 /* Get the value of __gp. */
1496 h = bfd_link_hash_lookup (info->hash, "__gp", false, false, true);
1497 if (h == (struct bfd_link_hash_entry *) NULL
1498 || h->type != bfd_link_hash_defined)
1499 return bfd_reloc_other;
1500
1501 gp = (h->u.def.value
1502 + h->u.def.section->output_section->vma
1503 + h->u.def.section->output_offset);
1504
1505 value -= sym_sec->output_section->vma;
1506 value -= (gp - sym_sec->output_section->vma);
1507 }
1508 break;
1509
1510 case R_V850_TDA_4_4_OFFSET:
1511 case R_V850_TDA_4_5_OFFSET:
1512 case R_V850_TDA_16_16_OFFSET:
1513 case R_V850_TDA_7_7_OFFSET:
1514 case R_V850_TDA_7_8_OFFSET:
1515 case R_V850_TDA_6_8_OFFSET:
1516 {
1517 unsigned long ep;
1518 struct bfd_link_hash_entry * h;
1519
1520 /* Get the value of __ep. */
1521 h = bfd_link_hash_lookup (info->hash, "__ep", false, false, true);
1522 if (h == (struct bfd_link_hash_entry *) NULL
1523 || h->type != bfd_link_hash_defined)
1524 /* Actually this indicates that __ep could not be found. */
1525 return bfd_reloc_continue;
1526
1527 ep = (h->u.def.value
1528 + h->u.def.section->output_section->vma
1529 + h->u.def.section->output_offset);
1530
1531 value -= ep;
1532 }
1533 break;
1534
1535 case R_V850_CALLT_6_7_OFFSET:
1536 {
1537 unsigned long ctbp;
1538 struct bfd_link_hash_entry * h;
1539
1540 /* Get the value of __ctbp. */
1541 h = bfd_link_hash_lookup (info->hash, "__ctbp", false, false, true);
1542 if (h == (struct bfd_link_hash_entry *) NULL
1543 || h->type != bfd_link_hash_defined)
1544 /* Actually this indicates that __ctbp could not be found. */
1545 return bfd_reloc_dangerous + 1;
1546
1547 ctbp = (h->u.def.value
1548 + h->u.def.section->output_section->vma
1549 + h->u.def.section->output_offset);
1550 value -= ctbp;
1551 }
1552 break;
1553
1554 case R_V850_CALLT_16_16_OFFSET:
1555 {
1556 unsigned long ctbp;
1557 struct bfd_link_hash_entry * h;
1558
1559 if (sym_sec == NULL)
1560 return bfd_reloc_undefined;
1561
1562 /* Get the value of __ctbp. */
1563 h = bfd_link_hash_lookup (info->hash, "__ctbp", false, false, true);
1564 if (h == (struct bfd_link_hash_entry *) NULL
1565 || h->type != bfd_link_hash_defined)
1566 return (bfd_reloc_dangerous + 1);
1567
1568 ctbp = (h->u.def.value
1569 + h->u.def.section->output_section->vma
1570 + h->u.def.section->output_offset);
1571
1572 value -= sym_sec->output_section->vma;
1573 value -= (ctbp - sym_sec->output_section->vma);
1574 }
1575 break;
1576
1577 case R_V850_NONE:
1578 case R_V850_GNU_VTINHERIT:
1579 case R_V850_GNU_VTENTRY:
1580 case R_V850_LONGCALL:
1581 case R_V850_LONGJUMP:
1582 case R_V850_ALIGN:
1583 return bfd_reloc_ok;
1584
1585 default:
1586 return bfd_reloc_notsupported;
1587 }
1588
1589 /* Perform the relocation. */
1590 return v850_elf_perform_relocation (input_bfd, r_type, value + addend, hit_data);
1591 }
1592 \f
1593 /* Relocate an V850 ELF section. */
1594
1595 static boolean
1596 v850_elf_relocate_section (output_bfd, info, input_bfd, input_section,
1597 contents, relocs, local_syms, local_sections)
1598 bfd * output_bfd;
1599 struct bfd_link_info * info;
1600 bfd * input_bfd;
1601 asection * input_section;
1602 bfd_byte * contents;
1603 Elf_Internal_Rela * relocs;
1604 Elf_Internal_Sym * local_syms;
1605 asection ** local_sections;
1606 {
1607 Elf_Internal_Shdr * symtab_hdr;
1608 struct elf_link_hash_entry ** sym_hashes;
1609 Elf_Internal_Rela * rel;
1610 Elf_Internal_Rela * relend;
1611
1612 if (info->relocateable)
1613 return true;
1614
1615 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
1616 sym_hashes = elf_sym_hashes (input_bfd);
1617
1618 if (sym_hashes == NULL)
1619 {
1620 info->callbacks->warning
1621 (info, "no hash table available",
1622 NULL, input_bfd, input_section, (bfd_vma) 0);
1623
1624 return false;
1625 }
1626
1627 /* Reset the list of remembered HI16S relocs to empty. */
1628 free_hi16s = previous_hi16s;
1629 previous_hi16s = NULL;
1630 hi16s_counter = 0;
1631
1632 rel = relocs;
1633 relend = relocs + input_section->reloc_count;
1634 for (; rel < relend; rel++)
1635 {
1636 int r_type;
1637 reloc_howto_type * howto;
1638 unsigned long r_symndx;
1639 Elf_Internal_Sym * sym;
1640 asection * sec;
1641 struct elf_link_hash_entry * h;
1642 bfd_vma relocation;
1643 bfd_reloc_status_type r;
1644
1645 r_symndx = ELF32_R_SYM (rel->r_info);
1646 r_type = ELF32_R_TYPE (rel->r_info);
1647
1648 if (r_type == R_V850_GNU_VTENTRY
1649 || r_type == R_V850_GNU_VTINHERIT)
1650 continue;
1651
1652 /* This is a final link. */
1653 howto = v850_elf_howto_table + r_type;
1654 h = NULL;
1655 sym = NULL;
1656 sec = NULL;
1657 if (r_symndx < symtab_hdr->sh_info)
1658 {
1659 sym = local_syms + r_symndx;
1660 sec = local_sections[r_symndx];
1661 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, sec, rel);
1662 #if 0
1663 {
1664 char * name;
1665
1666 name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link, sym->st_name);
1667 name = (name == NULL) ? "<none>" : name;
1668 fprintf (stderr, "local: sec: %s, sym: %s (%d), value: %x + %x + %x addend %x\n",
1669 sec->name, name, sym->st_name,
1670 sec->output_section->vma, sec->output_offset, sym->st_value, rel->r_addend);
1671 }
1672 #endif
1673 }
1674 else
1675 {
1676 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1677
1678 while (h->root.type == bfd_link_hash_indirect
1679 || h->root.type == bfd_link_hash_warning)
1680 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1681
1682 if (h->root.type == bfd_link_hash_defined
1683 || h->root.type == bfd_link_hash_defweak)
1684 {
1685 sec = h->root.u.def.section;
1686 relocation = (h->root.u.def.value
1687 + sec->output_section->vma
1688 + sec->output_offset);
1689 #if 0
1690 fprintf (stderr, "defined: sec: %s, name: %s, value: %x + %x + %x gives: %x\n",
1691 sec->name, h->root.root.string, h->root.u.def.value, sec->output_section->vma, sec->output_offset, relocation);
1692 #endif
1693 }
1694 else if (h->root.type == bfd_link_hash_undefweak)
1695 {
1696 #if 0
1697 fprintf (stderr, "undefined: sec: %s, name: %s\n",
1698 sec->name, h->root.root.string);
1699 #endif
1700 relocation = 0;
1701 }
1702 else
1703 {
1704 if (! ((*info->callbacks->undefined_symbol)
1705 (info, h->root.root.string, input_bfd,
1706 input_section, rel->r_offset, true)))
1707 return false;
1708 #if 0
1709 fprintf (stderr, "unknown: name: %s\n", h->root.root.string);
1710 #endif
1711 relocation = 0;
1712 }
1713 }
1714
1715 /* FIXME: We should use the addend, but the COFF relocations don't. */
1716 r = v850_elf_final_link_relocate (howto, input_bfd, output_bfd,
1717 input_section,
1718 contents, rel->r_offset,
1719 relocation, rel->r_addend,
1720 info, sec, h == NULL);
1721
1722 if (r != bfd_reloc_ok)
1723 {
1724 const char * name;
1725 const char * msg = (const char *)0;
1726
1727 if (h != NULL)
1728 name = h->root.root.string;
1729 else
1730 {
1731 name = (bfd_elf_string_from_elf_section
1732 (input_bfd, symtab_hdr->sh_link, sym->st_name));
1733 if (name == NULL || *name == '\0')
1734 name = bfd_section_name (input_bfd, sec);
1735 }
1736
1737 switch (r)
1738 {
1739 case bfd_reloc_overflow:
1740 if (! ((*info->callbacks->reloc_overflow)
1741 (info, name, howto->name, (bfd_vma) 0,
1742 input_bfd, input_section, rel->r_offset)))
1743 return false;
1744 break;
1745
1746 case bfd_reloc_undefined:
1747 if (! ((*info->callbacks->undefined_symbol)
1748 (info, name, input_bfd, input_section,
1749 rel->r_offset, true)))
1750 return false;
1751 break;
1752
1753 case bfd_reloc_outofrange:
1754 msg = _("internal error: out of range error");
1755 goto common_error;
1756
1757 case bfd_reloc_notsupported:
1758 msg = _("internal error: unsupported relocation error");
1759 goto common_error;
1760
1761 case bfd_reloc_dangerous:
1762 msg = _("internal error: dangerous relocation");
1763 goto common_error;
1764
1765 case bfd_reloc_other:
1766 msg = _("could not locate special linker symbol __gp");
1767 goto common_error;
1768
1769 case bfd_reloc_continue:
1770 msg = _("could not locate special linker symbol __ep");
1771 goto common_error;
1772
1773 case (bfd_reloc_dangerous + 1):
1774 msg = _("could not locate special linker symbol __ctbp");
1775 goto common_error;
1776
1777 default:
1778 msg = _("internal error: unknown error");
1779 /* fall through */
1780
1781 common_error:
1782 if (!((*info->callbacks->warning)
1783 (info, msg, name, input_bfd, input_section,
1784 rel->r_offset)))
1785 return false;
1786 break;
1787 }
1788 }
1789 }
1790
1791 return true;
1792 }
1793
1794 static boolean
1795 v850_elf_gc_sweep_hook (abfd, info, sec, relocs)
1796 bfd *abfd ATTRIBUTE_UNUSED;
1797 struct bfd_link_info *info ATTRIBUTE_UNUSED;
1798 asection *sec ATTRIBUTE_UNUSED;
1799 const Elf_Internal_Rela *relocs ATTRIBUTE_UNUSED;
1800 {
1801 /* No got and plt entries for v850-elf. */
1802 return true;
1803 }
1804
1805 static asection *
1806 v850_elf_gc_mark_hook (sec, info, rel, h, sym)
1807 asection *sec;
1808 struct bfd_link_info *info ATTRIBUTE_UNUSED;
1809 Elf_Internal_Rela *rel;
1810 struct elf_link_hash_entry *h;
1811 Elf_Internal_Sym *sym;
1812 {
1813 if (h != NULL)
1814 {
1815 switch (ELF32_R_TYPE (rel->r_info))
1816 {
1817 case R_V850_GNU_VTINHERIT:
1818 case R_V850_GNU_VTENTRY:
1819 break;
1820
1821 default:
1822 switch (h->root.type)
1823 {
1824 case bfd_link_hash_defined:
1825 case bfd_link_hash_defweak:
1826 return h->root.u.def.section;
1827
1828 case bfd_link_hash_common:
1829 return h->root.u.c.p->section;
1830
1831 default:
1832 break;
1833 }
1834 }
1835 }
1836 else
1837 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
1838
1839 return NULL;
1840 }
1841
1842 /* Set the right machine number. */
1843
1844 static boolean
1845 v850_elf_object_p (abfd)
1846 bfd *abfd;
1847 {
1848 switch (elf_elfheader (abfd)->e_flags & EF_V850_ARCH)
1849 {
1850 default:
1851 case E_V850_ARCH:
1852 bfd_default_set_arch_mach (abfd, bfd_arch_v850, bfd_mach_v850);
1853 break;
1854 case E_V850E_ARCH:
1855 bfd_default_set_arch_mach (abfd, bfd_arch_v850, bfd_mach_v850e);
1856 break;
1857 }
1858 return true;
1859 }
1860
1861 /* Store the machine number in the flags field. */
1862
1863 static void
1864 v850_elf_final_write_processing (abfd, linker)
1865 bfd * abfd;
1866 boolean linker ATTRIBUTE_UNUSED;
1867 {
1868 unsigned long val;
1869
1870 switch (bfd_get_mach (abfd))
1871 {
1872 default:
1873 case bfd_mach_v850: val = E_V850_ARCH; break;
1874 case bfd_mach_v850e: val = E_V850E_ARCH; break;
1875 }
1876
1877 elf_elfheader (abfd)->e_flags &=~ EF_V850_ARCH;
1878 elf_elfheader (abfd)->e_flags |= val;
1879 }
1880
1881 /* Function to keep V850 specific file flags. */
1882
1883 static boolean
1884 v850_elf_set_private_flags (abfd, flags)
1885 bfd * abfd;
1886 flagword flags;
1887 {
1888 BFD_ASSERT (!elf_flags_init (abfd)
1889 || elf_elfheader (abfd)->e_flags == flags);
1890
1891 elf_elfheader (abfd)->e_flags = flags;
1892 elf_flags_init (abfd) = true;
1893 return true;
1894 }
1895
1896 /* Merge backend specific data from an object file
1897 to the output object file when linking. */
1898 static boolean
1899 v850_elf_merge_private_bfd_data (ibfd, obfd)
1900 bfd * ibfd;
1901 bfd * obfd;
1902 {
1903 flagword out_flags;
1904 flagword in_flags;
1905
1906 if ( bfd_get_flavour (ibfd) != bfd_target_elf_flavour
1907 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
1908 return true;
1909
1910 in_flags = elf_elfheader (ibfd)->e_flags;
1911 out_flags = elf_elfheader (obfd)->e_flags;
1912
1913 if (! elf_flags_init (obfd))
1914 {
1915 /* If the input is the default architecture then do not
1916 bother setting the flags for the output architecture,
1917 instead allow future merges to do this. If no future
1918 merges ever set these flags then they will retain their
1919 unitialised values, which surprise surprise, correspond
1920 to the default values. */
1921 if (bfd_get_arch_info (ibfd)->the_default)
1922 return true;
1923
1924 elf_flags_init (obfd) = true;
1925 elf_elfheader (obfd)->e_flags = in_flags;
1926
1927 if (bfd_get_arch (obfd) == bfd_get_arch (ibfd)
1928 && bfd_get_arch_info (obfd)->the_default)
1929 return bfd_set_arch_mach (obfd, bfd_get_arch (ibfd), bfd_get_mach (ibfd));
1930
1931 return true;
1932 }
1933
1934 /* Check flag compatibility. */
1935 if (in_flags == out_flags)
1936 return true;
1937
1938 if ((in_flags & EF_V850_ARCH) != (out_flags & EF_V850_ARCH)
1939 && (in_flags & EF_V850_ARCH) != E_V850_ARCH)
1940 _bfd_error_handler (_("%s: Architecture mismatch with previous modules"),
1941 bfd_archive_filename (ibfd));
1942
1943 return true;
1944 }
1945
1946 /* Display the flags field. */
1947
1948 static boolean
1949 v850_elf_print_private_bfd_data (abfd, ptr)
1950 bfd * abfd;
1951 PTR ptr;
1952 {
1953 FILE * file = (FILE *) ptr;
1954
1955 BFD_ASSERT (abfd != NULL && ptr != NULL);
1956
1957 _bfd_elf_print_private_bfd_data (abfd, ptr);
1958
1959 /* xgettext:c-format */
1960 fprintf (file, _("private flags = %lx: "), elf_elfheader (abfd)->e_flags);
1961
1962 switch (elf_elfheader (abfd)->e_flags & EF_V850_ARCH)
1963 {
1964 default:
1965 case E_V850_ARCH: fprintf (file, _("v850 architecture")); break;
1966 case E_V850E_ARCH: fprintf (file, _("v850e architecture")); break;
1967 }
1968
1969 fputc ('\n', file);
1970
1971 return true;
1972 }
1973
1974 /* V850 ELF uses four common sections. One is the usual one, and the
1975 others are for (small) objects in one of the special data areas:
1976 small, tiny and zero. All the objects are kept together, and then
1977 referenced via the gp register, the ep register or the r0 register
1978 respectively, which yields smaller, faster assembler code. This
1979 approach is copied from elf32-mips.c. */
1980
1981 static asection v850_elf_scom_section;
1982 static asymbol v850_elf_scom_symbol;
1983 static asymbol * v850_elf_scom_symbol_ptr;
1984 static asection v850_elf_tcom_section;
1985 static asymbol v850_elf_tcom_symbol;
1986 static asymbol * v850_elf_tcom_symbol_ptr;
1987 static asection v850_elf_zcom_section;
1988 static asymbol v850_elf_zcom_symbol;
1989 static asymbol * v850_elf_zcom_symbol_ptr;
1990
1991 /* Given a BFD section, try to locate the
1992 corresponding ELF section index. */
1993
1994 static boolean
1995 v850_elf_section_from_bfd_section (abfd, sec, retval)
1996 bfd * abfd ATTRIBUTE_UNUSED;
1997 asection * sec;
1998 int * retval;
1999 {
2000 if (strcmp (bfd_get_section_name (abfd, sec), ".scommon") == 0)
2001 *retval = SHN_V850_SCOMMON;
2002 else if (strcmp (bfd_get_section_name (abfd, sec), ".tcommon") == 0)
2003 *retval = SHN_V850_TCOMMON;
2004 else if (strcmp (bfd_get_section_name (abfd, sec), ".zcommon") == 0)
2005 *retval = SHN_V850_ZCOMMON;
2006 else
2007 return false;
2008
2009 return true;
2010 }
2011
2012 /* Handle the special V850 section numbers that a symbol may use. */
2013
2014 static void
2015 v850_elf_symbol_processing (abfd, asym)
2016 bfd * abfd;
2017 asymbol * asym;
2018 {
2019 elf_symbol_type * elfsym = (elf_symbol_type *) asym;
2020 unsigned int indx;
2021
2022 indx = elfsym->internal_elf_sym.st_shndx;
2023
2024 /* If the section index is an "ordinary" index, then it may
2025 refer to a v850 specific section created by the assembler.
2026 Check the section's type and change the index it matches.
2027
2028 FIXME: Should we alter the st_shndx field as well ? */
2029
2030 if (indx < elf_numsections (abfd))
2031 switch (elf_elfsections(abfd)[indx]->sh_type)
2032 {
2033 case SHT_V850_SCOMMON:
2034 indx = SHN_V850_SCOMMON;
2035 break;
2036
2037 case SHT_V850_TCOMMON:
2038 indx = SHN_V850_TCOMMON;
2039 break;
2040
2041 case SHT_V850_ZCOMMON:
2042 indx = SHN_V850_ZCOMMON;
2043 break;
2044
2045 default:
2046 break;
2047 }
2048
2049 switch (indx)
2050 {
2051 case SHN_V850_SCOMMON:
2052 if (v850_elf_scom_section.name == NULL)
2053 {
2054 /* Initialize the small common section. */
2055 v850_elf_scom_section.name = ".scommon";
2056 v850_elf_scom_section.flags = SEC_IS_COMMON | SEC_ALLOC | SEC_DATA;
2057 v850_elf_scom_section.output_section = & v850_elf_scom_section;
2058 v850_elf_scom_section.symbol = & v850_elf_scom_symbol;
2059 v850_elf_scom_section.symbol_ptr_ptr = & v850_elf_scom_symbol_ptr;
2060 v850_elf_scom_symbol.name = ".scommon";
2061 v850_elf_scom_symbol.flags = BSF_SECTION_SYM;
2062 v850_elf_scom_symbol.section = & v850_elf_scom_section;
2063 v850_elf_scom_symbol_ptr = & v850_elf_scom_symbol;
2064 }
2065 asym->section = & v850_elf_scom_section;
2066 asym->value = elfsym->internal_elf_sym.st_size;
2067 break;
2068
2069 case SHN_V850_TCOMMON:
2070 if (v850_elf_tcom_section.name == NULL)
2071 {
2072 /* Initialize the tcommon section. */
2073 v850_elf_tcom_section.name = ".tcommon";
2074 v850_elf_tcom_section.flags = SEC_IS_COMMON;
2075 v850_elf_tcom_section.output_section = & v850_elf_tcom_section;
2076 v850_elf_tcom_section.symbol = & v850_elf_tcom_symbol;
2077 v850_elf_tcom_section.symbol_ptr_ptr = & v850_elf_tcom_symbol_ptr;
2078 v850_elf_tcom_symbol.name = ".tcommon";
2079 v850_elf_tcom_symbol.flags = BSF_SECTION_SYM;
2080 v850_elf_tcom_symbol.section = & v850_elf_tcom_section;
2081 v850_elf_tcom_symbol_ptr = & v850_elf_tcom_symbol;
2082 }
2083 asym->section = & v850_elf_tcom_section;
2084 asym->value = elfsym->internal_elf_sym.st_size;
2085 break;
2086
2087 case SHN_V850_ZCOMMON:
2088 if (v850_elf_zcom_section.name == NULL)
2089 {
2090 /* Initialize the zcommon section. */
2091 v850_elf_zcom_section.name = ".zcommon";
2092 v850_elf_zcom_section.flags = SEC_IS_COMMON;
2093 v850_elf_zcom_section.output_section = & v850_elf_zcom_section;
2094 v850_elf_zcom_section.symbol = & v850_elf_zcom_symbol;
2095 v850_elf_zcom_section.symbol_ptr_ptr = & v850_elf_zcom_symbol_ptr;
2096 v850_elf_zcom_symbol.name = ".zcommon";
2097 v850_elf_zcom_symbol.flags = BSF_SECTION_SYM;
2098 v850_elf_zcom_symbol.section = & v850_elf_zcom_section;
2099 v850_elf_zcom_symbol_ptr = & v850_elf_zcom_symbol;
2100 }
2101 asym->section = & v850_elf_zcom_section;
2102 asym->value = elfsym->internal_elf_sym.st_size;
2103 break;
2104 }
2105 }
2106
2107 /* Hook called by the linker routine which adds symbols from an object
2108 file. We must handle the special v850 section numbers here. */
2109
2110 static boolean
2111 v850_elf_add_symbol_hook (abfd, info, sym, namep, flagsp, secp, valp)
2112 bfd * abfd;
2113 struct bfd_link_info * info ATTRIBUTE_UNUSED;
2114 const Elf_Internal_Sym * sym;
2115 const char ** namep ATTRIBUTE_UNUSED;
2116 flagword * flagsp ATTRIBUTE_UNUSED;
2117 asection ** secp;
2118 bfd_vma * valp;
2119 {
2120 unsigned int indx = sym->st_shndx;
2121
2122 /* If the section index is an "ordinary" index, then it may
2123 refer to a v850 specific section created by the assembler.
2124 Check the section's type and change the index it matches.
2125
2126 FIXME: Should we alter the st_shndx field as well ? */
2127
2128 if (indx < elf_numsections (abfd))
2129 switch (elf_elfsections(abfd)[indx]->sh_type)
2130 {
2131 case SHT_V850_SCOMMON:
2132 indx = SHN_V850_SCOMMON;
2133 break;
2134
2135 case SHT_V850_TCOMMON:
2136 indx = SHN_V850_TCOMMON;
2137 break;
2138
2139 case SHT_V850_ZCOMMON:
2140 indx = SHN_V850_ZCOMMON;
2141 break;
2142
2143 default:
2144 break;
2145 }
2146
2147 switch (indx)
2148 {
2149 case SHN_V850_SCOMMON:
2150 *secp = bfd_make_section_old_way (abfd, ".scommon");
2151 (*secp)->flags |= SEC_IS_COMMON;
2152 *valp = sym->st_size;
2153 break;
2154
2155 case SHN_V850_TCOMMON:
2156 *secp = bfd_make_section_old_way (abfd, ".tcommon");
2157 (*secp)->flags |= SEC_IS_COMMON;
2158 *valp = sym->st_size;
2159 break;
2160
2161 case SHN_V850_ZCOMMON:
2162 *secp = bfd_make_section_old_way (abfd, ".zcommon");
2163 (*secp)->flags |= SEC_IS_COMMON;
2164 *valp = sym->st_size;
2165 break;
2166 }
2167
2168 return true;
2169 }
2170
2171 static boolean
2172 v850_elf_link_output_symbol_hook (abfd, info, name, sym, input_sec)
2173 bfd * abfd ATTRIBUTE_UNUSED;
2174 struct bfd_link_info * info ATTRIBUTE_UNUSED;
2175 const char * name ATTRIBUTE_UNUSED;
2176 Elf_Internal_Sym * sym;
2177 asection * input_sec;
2178 {
2179 /* If we see a common symbol, which implies a relocatable link, then
2180 if a symbol was in a special common section in an input file, mark
2181 it as a special common in the output file. */
2182
2183 if (sym->st_shndx == SHN_COMMON)
2184 {
2185 if (strcmp (input_sec->name, ".scommon") == 0)
2186 sym->st_shndx = SHN_V850_SCOMMON;
2187 else if (strcmp (input_sec->name, ".tcommon") == 0)
2188 sym->st_shndx = SHN_V850_TCOMMON;
2189 else if (strcmp (input_sec->name, ".zcommon") == 0)
2190 sym->st_shndx = SHN_V850_ZCOMMON;
2191 }
2192
2193 return true;
2194 }
2195
2196 static boolean
2197 v850_elf_section_from_shdr (abfd, hdr, name)
2198 bfd * abfd;
2199 Elf_Internal_Shdr * hdr;
2200 const char * name;
2201 {
2202 /* There ought to be a place to keep ELF backend specific flags, but
2203 at the moment there isn't one. We just keep track of the
2204 sections by their name, instead. */
2205
2206 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name))
2207 return false;
2208
2209 switch (hdr->sh_type)
2210 {
2211 case SHT_V850_SCOMMON:
2212 case SHT_V850_TCOMMON:
2213 case SHT_V850_ZCOMMON:
2214 if (! bfd_set_section_flags (abfd, hdr->bfd_section,
2215 (bfd_get_section_flags (abfd,
2216 hdr->bfd_section)
2217 | SEC_IS_COMMON)))
2218 return false;
2219 }
2220
2221 return true;
2222 }
2223
2224 /* Set the correct type for a V850 ELF section. We do this
2225 by the section name, which is a hack, but ought to work. */
2226
2227 static boolean
2228 v850_elf_fake_sections (abfd, hdr, sec)
2229 bfd * abfd ATTRIBUTE_UNUSED;
2230 Elf32_Internal_Shdr * hdr;
2231 asection * sec;
2232 {
2233 register const char * name;
2234
2235 name = bfd_get_section_name (abfd, sec);
2236
2237 if (strcmp (name, ".scommon") == 0)
2238 {
2239 hdr->sh_type = SHT_V850_SCOMMON;
2240 }
2241 else if (strcmp (name, ".tcommon") == 0)
2242 {
2243 hdr->sh_type = SHT_V850_TCOMMON;
2244 }
2245 else if (strcmp (name, ".zcommon") == 0)
2246 hdr->sh_type = SHT_V850_ZCOMMON;
2247
2248 return true;
2249 }
2250
2251 /* Delete some bytes from a section while relaxing. */
2252
2253 static boolean
2254 v850_elf_relax_delete_bytes (abfd, sec, addr, toaddr, count)
2255 bfd * abfd;
2256 asection * sec;
2257 bfd_vma addr;
2258 bfd_vma toaddr;
2259 int count;
2260 {
2261 Elf_Internal_Shdr * symtab_hdr;
2262 Elf32_External_Sym * extsyms;
2263 Elf32_External_Sym * esym;
2264 Elf32_External_Sym * esymend;
2265 int index;
2266 unsigned int sec_shndx;
2267 bfd_byte * contents;
2268 Elf_Internal_Rela * irel;
2269 Elf_Internal_Rela * irelend;
2270 struct elf_link_hash_entry * sym_hash;
2271 Elf_Internal_Shdr * shndx_hdr;
2272 Elf_External_Sym_Shndx * shndx;
2273
2274 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2275 extsyms = (Elf32_External_Sym *) symtab_hdr->contents;
2276
2277 sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
2278
2279 contents = elf_section_data (sec)->this_hdr.contents;
2280
2281 /* The deletion must stop at the next ALIGN reloc for an alignment
2282 power larger than the number of bytes we are deleting. */
2283
2284 /* Actually delete the bytes. */
2285 #if (DEBUG_RELAX & 2)
2286 fprintf (stderr, "relax_delete: contents: sec: %s %p .. %p %x\n",
2287 sec->name, addr, toaddr, count );
2288 #endif
2289 memmove (contents + addr, contents + addr + count,
2290 toaddr - addr - count);
2291 memset (contents + toaddr-count, 0, count);
2292
2293 /* Adjust all the relocs. */
2294 irel = elf_section_data (sec)->relocs;
2295 irelend = irel + sec->reloc_count;
2296 shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
2297 shndx = (Elf_External_Sym_Shndx *) shndx_hdr->contents;
2298
2299 for (; irel < irelend; irel++)
2300 {
2301 bfd_vma raddr, paddr, symval;
2302 Elf_Internal_Sym isym;
2303
2304 /* Get the new reloc address. */
2305 raddr = irel->r_offset;
2306 if ((raddr >= (addr + count) && raddr < toaddr))
2307 irel->r_offset -= count;
2308
2309 if (raddr >= addr && raddr < addr + count)
2310 {
2311 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
2312 (int) R_V850_NONE);
2313 continue;
2314 }
2315
2316 if (ELF32_R_TYPE (irel->r_info) == (int) R_V850_ALIGN)
2317 continue;
2318
2319 bfd_elf32_swap_symbol_in (abfd,
2320 extsyms + ELF32_R_SYM (irel->r_info),
2321 shndx ? shndx + ELF32_R_SYM (irel->r_info) : NULL,
2322 & isym);
2323
2324 if (isym.st_shndx != sec_shndx)
2325 continue;
2326
2327 /* Get the value of the symbol referred to by the reloc. */
2328 if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
2329 {
2330 symval = isym.st_value;
2331 #if (DEBUG_RELAX & 2)
2332 {
2333 char * name = bfd_elf_string_from_elf_section
2334 (abfd, symtab_hdr->sh_link, isym.st_name);
2335 fprintf (stderr,
2336 "relax_delete: local: sec: %s, sym: %s (%d), value: %x + %x + %x addend %x\n",
2337 sec->name, name, isym.st_name,
2338 sec->output_section->vma, sec->output_offset,
2339 isym.st_value, irel->r_addend);
2340 }
2341 #endif
2342 }
2343 else
2344 {
2345 unsigned long indx;
2346 struct elf_link_hash_entry * h;
2347
2348 /* An external symbol. */
2349 indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
2350
2351 h = elf_sym_hashes (abfd) [indx];
2352 BFD_ASSERT (h != NULL);
2353
2354 symval = h->root.u.def.value;
2355 #if (DEBUG_RELAX & 2)
2356 fprintf (stderr,
2357 "relax_delete: defined: sec: %s, name: %s, value: %x + %x + %x addend %x\n",
2358 sec->name, h->root.root.string, h->root.u.def.value,
2359 sec->output_section->vma, sec->output_offset, irel->r_addend);
2360 #endif
2361 }
2362
2363 paddr = symval + irel->r_addend;
2364
2365 if ( (symval >= addr + count && symval < toaddr)
2366 && (paddr < addr + count || paddr >= toaddr))
2367 irel->r_addend += count;
2368 else if ( (symval < addr + count || symval >= toaddr)
2369 && (paddr >= addr + count && paddr < toaddr))
2370 irel->r_addend -= count;
2371 }
2372
2373 /* Adjust the local symbols defined in this section. */
2374 esym = extsyms;
2375 esymend = esym + symtab_hdr->sh_info;
2376
2377 for (; esym < esymend; esym++, shndx = (shndx ? shndx + 1 : NULL))
2378 {
2379 Elf_Internal_Sym isym;
2380
2381 bfd_elf32_swap_symbol_in (abfd, esym, shndx, & isym);
2382
2383 if (isym.st_shndx == sec_shndx
2384 && isym.st_value >= addr + count
2385 && isym.st_value < toaddr)
2386 {
2387 isym.st_value -= count;
2388
2389 if (isym.st_value + isym.st_size >= toaddr)
2390 isym.st_size += count;
2391
2392 bfd_elf32_swap_symbol_out (abfd, & isym, shndx, esym);
2393 }
2394 else if (isym.st_shndx == sec_shndx
2395 && isym.st_value < addr + count)
2396 {
2397 if (isym.st_value+isym.st_size >= addr + count
2398 && isym.st_value+isym.st_size < toaddr)
2399 isym.st_size -= count;
2400
2401 if (isym.st_value >= addr
2402 && isym.st_value < addr + count)
2403 isym.st_value = addr;
2404
2405 bfd_elf32_swap_symbol_out (abfd, & isym, shndx, esym);
2406 }
2407 }
2408
2409 /* Now adjust the global symbols defined in this section. */
2410 esym = extsyms + symtab_hdr->sh_info;
2411 esymend = extsyms + (symtab_hdr->sh_size / sizeof (Elf32_External_Sym));
2412
2413 for (index = 0; esym < esymend; esym ++, index ++)
2414 {
2415 Elf_Internal_Sym isym;
2416
2417 bfd_elf32_swap_symbol_in (abfd, esym, shndx, & isym);
2418 sym_hash = elf_sym_hashes (abfd) [index];
2419
2420 if (isym.st_shndx == sec_shndx
2421 && ((sym_hash)->root.type == bfd_link_hash_defined
2422 || (sym_hash)->root.type == bfd_link_hash_defweak)
2423 && (sym_hash)->root.u.def.section == sec
2424 && (sym_hash)->root.u.def.value >= addr + count
2425 && (sym_hash)->root.u.def.value < toaddr)
2426 {
2427 if ((sym_hash)->root.u.def.value + isym.st_size >= toaddr)
2428 {
2429 isym.st_size += count;
2430 bfd_elf32_swap_symbol_out (abfd, & isym, shndx, esym);
2431 }
2432
2433 (sym_hash)->root.u.def.value -= count;
2434 }
2435 else if (isym.st_shndx == sec_shndx
2436 && ((sym_hash)->root.type == bfd_link_hash_defined
2437 || (sym_hash)->root.type == bfd_link_hash_defweak)
2438 && (sym_hash)->root.u.def.section == sec
2439 && (sym_hash)->root.u.def.value < addr + count)
2440 {
2441 if ((sym_hash)->root.u.def.value+isym.st_size >= addr + count
2442 && (sym_hash)->root.u.def.value+isym.st_size < toaddr)
2443 isym.st_size -= count;
2444
2445 if ((sym_hash)->root.u.def.value >= addr
2446 && (sym_hash)->root.u.def.value < addr + count)
2447 (sym_hash)->root.u.def.value = addr;
2448
2449 bfd_elf32_swap_symbol_out (abfd, & isym, shndx, esym);
2450 }
2451
2452 if (shndx)
2453 ++ shndx;
2454 }
2455
2456 return true;
2457 }
2458
2459 #define NOP_OPCODE (0x0000)
2460 #define MOVHI 0x0640 /* 4byte */
2461 #define MOVHI_MASK 0x07e0
2462 #define MOVHI_R1(insn) ((insn) & 0x1f) /* 4byte */
2463 #define MOVHI_R2(insn) ((insn) >> 11)
2464 #define MOVEA 0x0620 /* 2byte */
2465 #define MOVEA_MASK 0x07e0
2466 #define MOVEA_R1(insn) ((insn) & 0x1f)
2467 #define MOVEA_R2(insn) ((insn) >> 11)
2468 #define JARL_4 0x00040780 /* 4byte */
2469 #define JARL_4_MASK 0xFFFF07FF
2470 #define JARL_R2(insn) (int)(((insn) & (~JARL_4_MASK)) >> 11)
2471 #define ADD_I 0x0240 /* 2byte */
2472 #define ADD_I_MASK 0x07e0
2473 #define ADD_I5(insn) ((((insn) & 0x001f) << 11) >> 11) /* 2byte */
2474 #define ADD_R2(insn) ((insn) >> 11)
2475 #define JMP_R 0x0060 /* 2byte */
2476 #define JMP_R_MASK 0xFFE0
2477 #define JMP_R1(insn) ((insn) & 0x1f)
2478
2479 static boolean
2480 v850_elf_relax_section (abfd, sec, link_info, again)
2481 bfd * abfd;
2482 asection * sec;
2483 struct bfd_link_info * link_info;
2484 boolean * again;
2485 {
2486 Elf_Internal_Shdr * symtab_hdr;
2487 Elf_Internal_Rela * internal_relocs;
2488 Elf_Internal_Rela * free_relocs = NULL;
2489 Elf_Internal_Rela * irel;
2490 Elf_Internal_Rela * irelend;
2491 Elf_Internal_Rela * irelalign = NULL;
2492 bfd_byte * contents = NULL;
2493 bfd_byte * free_contents = NULL;
2494 Elf32_External_Sym * extsyms = NULL;
2495 Elf32_External_Sym * free_extsyms = NULL;
2496 bfd_vma addr = 0;
2497 bfd_vma toaddr;
2498 int align_pad_size = 0;
2499 Elf_Internal_Shdr * shndx_hdr = NULL;
2500 Elf_External_Sym_Shndx * shndx_buf = NULL;
2501
2502 * again = false;
2503
2504 if (link_info->relocateable
2505 || (sec->flags & SEC_RELOC) == 0
2506 || sec->reloc_count == 0)
2507 return true;
2508
2509 /* If this is the first time we have been called
2510 for this section, initialize the cooked size. */
2511 if (sec->_cooked_size == 0)
2512 sec->_cooked_size = sec->_raw_size;
2513
2514 symtab_hdr = & elf_tdata (abfd)->symtab_hdr;
2515
2516 internal_relocs = (_bfd_elf32_link_read_relocs
2517 (abfd, sec, (PTR) NULL, (Elf_Internal_Rela *) NULL,
2518 link_info->keep_memory));
2519 if (internal_relocs == NULL)
2520 goto error_return;
2521 if (! link_info->keep_memory)
2522 free_relocs = internal_relocs;
2523
2524 irelend = internal_relocs + sec->reloc_count;
2525
2526 while (addr < sec->_cooked_size)
2527 {
2528 toaddr = sec->_cooked_size;
2529
2530 for (irel = internal_relocs; irel < irelend; irel ++)
2531 if (ELF32_R_TYPE (irel->r_info) == (int) R_V850_ALIGN
2532 && irel->r_offset > addr
2533 && irel->r_offset < toaddr)
2534 toaddr = irel->r_offset;
2535
2536 #ifdef DEBUG_RELAX
2537 fprintf (stderr, "relax region 0x%x to 0x%x align pad %d\n",
2538 addr, toaddr, align_pad_size);
2539 #endif
2540 if (irelalign)
2541 {
2542 bfd_vma alignto;
2543 bfd_vma alignmoveto;
2544
2545 alignmoveto = BFD_ALIGN (addr - align_pad_size, 1 << irelalign->r_addend);
2546 alignto = BFD_ALIGN (addr, 1 << irelalign->r_addend);
2547
2548 if (alignmoveto < alignto)
2549 {
2550 unsigned int i;
2551
2552 align_pad_size = alignto - alignmoveto;
2553 #ifdef DEBUG_RELAX
2554 fprintf (stderr, "relax move region 0x%x to 0x%x delete size 0x%x\n",
2555 alignmoveto, toaddr, align_pad_size);
2556 #endif
2557 if (!v850_elf_relax_delete_bytes (abfd, sec, alignmoveto,
2558 toaddr, align_pad_size))
2559 goto error_return;
2560
2561 for (i = BFD_ALIGN (toaddr - align_pad_size, 1);
2562 (i + 1) < toaddr; i += 2)
2563 bfd_put_16 (abfd, NOP_OPCODE, contents + i);
2564
2565 addr = alignmoveto;
2566 }
2567 else
2568 align_pad_size = 0;
2569 }
2570
2571 for (irel = internal_relocs; irel < irelend; irel++)
2572 {
2573 bfd_vma laddr;
2574 bfd_vma addend;
2575 bfd_vma symval;
2576 int insn[5];
2577 int no_match = -1;
2578 Elf_Internal_Rela * hi_irelfn;
2579 Elf_Internal_Rela * lo_irelfn;
2580 Elf_Internal_Rela * irelcall;
2581 bfd_signed_vma foff;
2582
2583 if (! (irel->r_offset >= addr && irel->r_offset < toaddr
2584 && (ELF32_R_TYPE (irel->r_info) == (int) R_V850_LONGCALL
2585 || ELF32_R_TYPE (irel->r_info) == (int) R_V850_LONGJUMP)))
2586 continue;
2587
2588 #ifdef DEBUG_RELAX
2589 fprintf (stderr, "relax check r_info 0x%x r_offset 0x%x r_addend 0x%x\n",
2590 irel->r_info,
2591 irel->r_offset,
2592 irel->r_addend );
2593 #endif
2594
2595 /* Get the section contents. */
2596 if (contents == NULL)
2597 {
2598 if (elf_section_data (sec)->this_hdr.contents != NULL)
2599 contents = elf_section_data (sec)->this_hdr.contents;
2600 else
2601 {
2602 contents = (bfd_byte *) bfd_malloc (sec->_raw_size);
2603 if (contents == NULL)
2604 goto error_return;
2605
2606 free_contents = contents;
2607
2608 if (! bfd_get_section_contents (abfd, sec, contents,
2609 (file_ptr) 0, sec->_raw_size))
2610 goto error_return;
2611 }
2612 }
2613
2614 /* Read this BFD's symbols if we haven't done so already. */
2615 if (extsyms == NULL)
2616 {
2617 /* Get cached copy if it exists. */
2618 if (symtab_hdr->contents != NULL)
2619 extsyms = (Elf32_External_Sym *) symtab_hdr->contents;
2620 else
2621 {
2622 /* Go get them off disk. */
2623 bfd_size_type amt;
2624
2625 amt = symtab_hdr->sh_info;
2626 amt *= sizeof (Elf32_External_Sym);
2627 extsyms = (Elf32_External_Sym *) bfd_malloc (amt);
2628 if (extsyms == NULL)
2629 goto error_return;
2630 free_extsyms = extsyms;
2631 if (bfd_seek (abfd, symtab_hdr->sh_offset, SEEK_SET) != 0
2632 || bfd_bread ((PTR) extsyms, amt, abfd) != amt)
2633 goto error_return;
2634 }
2635
2636 if (shndx_hdr->sh_size != 0)
2637 {
2638 bfd_size_type amt;
2639
2640 amt = symtab_hdr->sh_info;
2641 amt *= sizeof (Elf_External_Sym_Shndx);
2642 shndx_buf = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
2643 if (shndx_buf == NULL)
2644 goto error_return;
2645 if (bfd_seek (abfd, shndx_hdr->sh_offset, SEEK_SET) != 0
2646 || bfd_bread ((PTR) shndx_buf, amt, abfd) != amt)
2647 goto error_return;
2648 }
2649 }
2650
2651 laddr = irel->r_offset;
2652
2653 if (ELF32_R_TYPE (irel->r_info) == (int) R_V850_LONGCALL)
2654 {
2655 /* Check code for -mlong-calls output. */
2656 if (laddr + 16 <= (bfd_vma) sec->_raw_size)
2657 {
2658 insn[0] = bfd_get_16 (abfd, contents + laddr);
2659 insn[1] = bfd_get_16 (abfd, contents + laddr + 4);
2660 insn[2] = bfd_get_32 (abfd, contents + laddr + 8);
2661 insn[3] = bfd_get_16 (abfd, contents + laddr + 12);
2662 insn[4] = bfd_get_16 (abfd, contents + laddr + 14);
2663
2664 if ((insn[0] & MOVHI_MASK) != MOVHI
2665 || MOVHI_R1 (insn[0]) != 0)
2666 no_match = 0;
2667
2668 if (no_match < 0
2669 && ((insn[1] & MOVEA_MASK) != MOVEA
2670 || MOVHI_R2 (insn[0]) != MOVEA_R1 (insn[1])))
2671 no_match = 1;
2672
2673 if (no_match < 0
2674 && (insn[2] & JARL_4_MASK) != JARL_4)
2675 no_match = 2;
2676
2677 if (no_match < 0
2678 && ((insn[3] & ADD_I_MASK) != ADD_I
2679 || ADD_I5 (insn[3]) != 4
2680 || JARL_R2 (insn[2]) != ADD_R2 (insn[3])))
2681 no_match = 3;
2682
2683 if (no_match < 0
2684 && ((insn[4] & JMP_R_MASK) != JMP_R
2685 || MOVEA_R2 (insn[1]) != JMP_R1 (insn[4])))
2686 no_match = 4;
2687 }
2688 else
2689 {
2690 ((*_bfd_error_handler)
2691 ("%s: 0x%lx: warning: R_V850_LONGCALL points to unrecognized insns",
2692 bfd_get_filename (abfd), (unsigned long) irel->r_offset));
2693
2694 continue;
2695 }
2696
2697 if (no_match >= 0)
2698 {
2699 ((*_bfd_error_handler)
2700 ("%s: 0x%lx: warning: R_V850_LONGCALL points to unrecognized insn 0x%x",
2701 bfd_get_filename (abfd), (unsigned long) irel->r_offset+no_match, insn[no_match]));
2702
2703 continue;
2704 }
2705
2706 /* Get the reloc for the address from which the register is
2707 being loaded. This reloc will tell us which function is
2708 actually being called. */
2709 for (hi_irelfn = internal_relocs; hi_irelfn < irelend; hi_irelfn ++)
2710 if (hi_irelfn->r_offset == laddr + 2
2711 && ELF32_R_TYPE (hi_irelfn->r_info)
2712 == (int) R_V850_HI16_S)
2713 break;
2714
2715 for (lo_irelfn = internal_relocs; lo_irelfn < irelend; lo_irelfn ++)
2716 if (lo_irelfn->r_offset == laddr + 6
2717 && ELF32_R_TYPE (lo_irelfn->r_info)
2718 == (int) R_V850_LO16)
2719 break;
2720
2721 for (irelcall = internal_relocs; irelcall < irelend; irelcall ++)
2722 if (irelcall->r_offset == laddr + 8
2723 && ELF32_R_TYPE (irelcall->r_info)
2724 == (int) R_V850_22_PCREL)
2725 break;
2726
2727 if ( hi_irelfn == irelend
2728 || lo_irelfn == irelend
2729 || irelcall == irelend)
2730 {
2731 ((*_bfd_error_handler)
2732 ("%s: 0x%lx: warning: R_V850_LONGCALL points to unrecognized reloc",
2733 bfd_get_filename (abfd), (unsigned long) irel->r_offset ));
2734
2735 continue;
2736 }
2737
2738 if (ELF32_R_SYM (irelcall->r_info) < symtab_hdr->sh_info)
2739 {
2740 unsigned int r_index;
2741 Elf_Internal_Sym isym;
2742 asection * sym_sec;
2743 Elf32_External_Sym * esym;
2744 Elf_External_Sym_Shndx * shndx;
2745
2746 /* A local symbol. */
2747 r_index = ELF32_R_SYM (irelcall->r_info);
2748 esym = extsyms + r_index;
2749 shndx = shndx_buf + (shndx_buf ? r_index : 0);
2750 bfd_elf32_swap_symbol_in (abfd, esym, shndx, & isym);
2751
2752 if (isym.st_shndx == SHN_UNDEF)
2753 sym_sec = bfd_und_section_ptr;
2754 else if (isym.st_shndx == SHN_ABS)
2755 sym_sec = bfd_abs_section_ptr;
2756 else if (isym.st_shndx == SHN_COMMON)
2757 sym_sec = bfd_com_section_ptr;
2758 else
2759 sym_sec = bfd_section_from_elf_index (abfd, isym.st_shndx);
2760 symval = isym.st_value;
2761 }
2762 else
2763 {
2764 unsigned long indx;
2765 struct elf_link_hash_entry * h;
2766
2767 /* An external symbol. */
2768 indx = ELF32_R_SYM (irelcall->r_info) - symtab_hdr->sh_info;
2769 h = elf_sym_hashes (abfd)[indx];
2770 BFD_ASSERT (h != NULL);
2771
2772 if ( h->root.type != bfd_link_hash_defined
2773 && h->root.type != bfd_link_hash_defweak)
2774 /* This appears to be a reference to an undefined
2775 symbol. Just ignore it--it will be caught by the
2776 regular reloc processing. */
2777 continue;
2778
2779 symval = h->root.u.def.value;
2780 }
2781
2782 if (symval + irelcall->r_addend != irelcall->r_offset + 4)
2783 {
2784 ((*_bfd_error_handler)
2785 ("%s: 0x%lx: warning: R_V850_LONGCALL points to unrecognized reloc 0x%lx",
2786 bfd_get_filename (abfd), (unsigned long) irel->r_offset, irelcall->r_offset ));
2787
2788 continue;
2789 }
2790
2791 /* Get the value of the symbol referred to by the reloc. */
2792 if (ELF32_R_SYM (hi_irelfn->r_info) < symtab_hdr->sh_info)
2793 {
2794 unsigned int r_index;
2795 Elf_Internal_Sym isym;
2796 asection * sym_sec;
2797 Elf32_External_Sym * esym;
2798 Elf_External_Sym_Shndx * shndx;
2799
2800 /* A local symbol. */
2801 r_index = ELF32_R_SYM (irel->r_info);
2802 esym = extsyms + r_index;
2803 shndx = shndx_buf + (shndx_buf ? r_index : 0);
2804 bfd_elf32_swap_symbol_in (abfd, esym, shndx, & isym);
2805
2806 if (isym.st_shndx == SHN_UNDEF)
2807 sym_sec = bfd_und_section_ptr;
2808 else if (isym.st_shndx == SHN_ABS)
2809 sym_sec = bfd_abs_section_ptr;
2810 else if (isym.st_shndx == SHN_COMMON)
2811 sym_sec = bfd_com_section_ptr;
2812 else
2813 sym_sec = bfd_section_from_elf_index (abfd, isym.st_shndx);
2814 symval = (isym.st_value
2815 + sym_sec->output_section->vma
2816 + sym_sec->output_offset);
2817 }
2818 else
2819 {
2820 unsigned long indx;
2821 struct elf_link_hash_entry * h;
2822
2823 /* An external symbol. */
2824 indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
2825 h = elf_sym_hashes (abfd)[indx];
2826 BFD_ASSERT (h != NULL);
2827
2828 if ( h->root.type != bfd_link_hash_defined
2829 && h->root.type != bfd_link_hash_defweak)
2830 /* This appears to be a reference to an undefined
2831 symbol. Just ignore it--it will be caught by the
2832 regular reloc processing. */
2833 continue;
2834
2835 symval = (h->root.u.def.value
2836 + h->root.u.def.section->output_section->vma
2837 + h->root.u.def.section->output_offset);
2838 }
2839
2840 addend = irel->r_addend;
2841
2842 foff = (symval + addend
2843 - (irel->r_offset
2844 + sec->output_section->vma
2845 + sec->output_offset
2846 + 4));
2847 #ifdef DEBUG_RELAX
2848 fprintf (stderr, "relax longcall r_offset 0x%x ptr 0x%x symbol 0x%x addend 0x%x distance 0x%x\n",
2849 irel->r_offset,
2850 (irel->r_offset
2851 + sec->output_section->vma
2852 + sec->output_offset),
2853 symval, addend, foff);
2854 #endif
2855
2856 if (foff < -0x100000 || foff >= 0x100000)
2857 /* After all that work, we can't shorten this function call. */
2858 continue;
2859
2860 /* For simplicity of coding, we are going to modify the section
2861 contents, the section relocs, and the BFD symbol table. We
2862 must tell the rest of the code not to free up this
2863 information. It would be possible to instead create a table
2864 of changes which have to be made, as is done in coff-mips.c;
2865 that would be more work, but would require less memory when
2866 the linker is run. */
2867 elf_section_data (sec)->relocs = internal_relocs;
2868 free_relocs = NULL;
2869
2870 elf_section_data (sec)->this_hdr.contents = contents;
2871 free_contents = NULL;
2872
2873 symtab_hdr->contents = (bfd_byte *) extsyms;
2874 free_extsyms = NULL;
2875
2876 /* Replace the long call with a jarl. */
2877 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info), R_V850_22_PCREL);
2878
2879 addend = 0;
2880
2881 if (ELF32_R_SYM (hi_irelfn->r_info) < symtab_hdr->sh_info)
2882 /* If this needs to be changed because of future relaxing,
2883 it will be handled here like other internal IND12W
2884 relocs. */
2885 bfd_put_32 (abfd,
2886 0x00000780 | (JARL_R2 (insn[2])<<11) | ((addend << 16) & 0xffff) | ((addend >> 16) & 0xf),
2887 contents + irel->r_offset);
2888 else
2889 /* We can't fully resolve this yet, because the external
2890 symbol value may be changed by future relaxing.
2891 We let the final link phase handle it. */
2892 bfd_put_32 (abfd, 0x00000780 | (JARL_R2 (insn[2])<<11),
2893 contents + irel->r_offset);
2894
2895 hi_irelfn->r_info =
2896 ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info), R_V850_NONE);
2897 lo_irelfn->r_info =
2898 ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info), R_V850_NONE);
2899 irelcall->r_info =
2900 ELF32_R_INFO (ELF32_R_SYM (irelcall->r_info), R_V850_NONE);
2901
2902 if (! v850_elf_relax_delete_bytes (abfd, sec,
2903 irel->r_offset + 4, toaddr, 12))
2904 goto error_return;
2905
2906 align_pad_size += 12;
2907 }
2908 else if (ELF32_R_TYPE (irel->r_info) == (int) R_V850_LONGJUMP)
2909 {
2910 /* Check code for -mlong-jumps output. */
2911 if (laddr + 10 <= (bfd_vma) sec->_raw_size)
2912 {
2913 insn[0] = bfd_get_16 (abfd, contents + laddr);
2914 insn[1] = bfd_get_16 (abfd, contents + laddr + 4);
2915 insn[2] = bfd_get_16 (abfd, contents + laddr + 8);
2916
2917 if ((insn[0] & MOVHI_MASK) != MOVHI
2918 || MOVHI_R1 (insn[0]) != 0)
2919 no_match = 0;
2920
2921 if (no_match < 0
2922 && ((insn[1] & MOVEA_MASK) != MOVEA
2923 || MOVHI_R2 (insn[0]) != MOVEA_R1 (insn[1])))
2924 no_match = 1;
2925
2926 if (no_match < 0
2927 && ((insn[2] & JMP_R_MASK) != JMP_R
2928 || MOVEA_R2 (insn[1]) != JMP_R1 (insn[2])))
2929 no_match = 4;
2930 }
2931 else
2932 {
2933 ((*_bfd_error_handler)
2934 ("%s: 0x%lx: warning: R_V850_LONGJUMP points to unrecognized insns",
2935 bfd_get_filename (abfd), (unsigned long) irel->r_offset));
2936
2937 continue;
2938 }
2939
2940 if (no_match >= 0)
2941 {
2942 ((*_bfd_error_handler)
2943 ("%s: 0x%lx: warning: R_V850_LONGJUMP points to unrecognized insn 0x%x",
2944 bfd_get_filename (abfd), (unsigned long) irel->r_offset+no_match, insn[no_match]));
2945
2946 continue;
2947 }
2948
2949 /* Get the reloc for the address from which the register is
2950 being loaded. This reloc will tell us which function is
2951 actually being called. */
2952 for (hi_irelfn = internal_relocs; hi_irelfn < irelend; hi_irelfn ++)
2953 if (hi_irelfn->r_offset == laddr + 2
2954 && ELF32_R_TYPE (hi_irelfn->r_info) == (int) R_V850_HI16_S)
2955 break;
2956
2957 for (lo_irelfn = internal_relocs; lo_irelfn < irelend; lo_irelfn ++)
2958 if (lo_irelfn->r_offset == laddr + 6
2959 && ELF32_R_TYPE (lo_irelfn->r_info) == (int) R_V850_LO16)
2960 break;
2961
2962 if ( hi_irelfn == irelend
2963 || lo_irelfn == irelend)
2964 {
2965 ((*_bfd_error_handler)
2966 ("%s: 0x%lx: warning: R_V850_LONGJUMP points to unrecognized reloc",
2967 bfd_get_filename (abfd), (unsigned long) irel->r_offset ));
2968
2969 continue;
2970 }
2971
2972 /* Get the value of the symbol referred to by the reloc. */
2973 if (ELF32_R_SYM (hi_irelfn->r_info) < symtab_hdr->sh_info)
2974 {
2975 unsigned int r_index;
2976 Elf_Internal_Sym isym;
2977 asection * sym_sec;
2978 Elf32_External_Sym * esym;
2979 Elf_External_Sym_Shndx * shndx;
2980
2981 /* A local symbol. */
2982 r_index = ELF32_R_SYM (irel->r_info);
2983 esym = extsyms + r_index;
2984 shndx = shndx_buf + (shndx_buf ? r_index : 0);
2985 bfd_elf32_swap_symbol_in (abfd, esym, shndx, & isym);
2986
2987 if (isym.st_shndx == SHN_UNDEF)
2988 sym_sec = bfd_und_section_ptr;
2989 else if (isym.st_shndx == SHN_ABS)
2990 sym_sec = bfd_abs_section_ptr;
2991 else if (isym.st_shndx == SHN_COMMON)
2992 sym_sec = bfd_com_section_ptr;
2993 else
2994 sym_sec = bfd_section_from_elf_index (abfd, isym.st_shndx);
2995 symval = (isym.st_value
2996 + sym_sec->output_section->vma
2997 + sym_sec->output_offset);
2998 #ifdef DEBUG_RELAX
2999 {
3000 char * name = bfd_elf_string_from_elf_section
3001 (abfd, symtab_hdr->sh_link, isym.st_name);
3002
3003 fprintf (stderr, "relax long jump local: sec: %s, sym: %s (%d), value: %x + %x + %x addend %x\n",
3004 sym_sec->name, name, isym.st_name,
3005 sym_sec->output_section->vma, sym_sec->output_offset,
3006 isym.st_value, irel->r_addend);
3007 }
3008 #endif
3009 }
3010 else
3011 {
3012 unsigned long indx;
3013 struct elf_link_hash_entry * h;
3014
3015 /* An external symbol. */
3016 indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
3017 h = elf_sym_hashes (abfd)[indx];
3018 BFD_ASSERT (h != NULL);
3019
3020 if ( h->root.type != bfd_link_hash_defined
3021 && h->root.type != bfd_link_hash_defweak)
3022 /* This appears to be a reference to an undefined
3023 symbol. Just ignore it--it will be caught by the
3024 regular reloc processing. */
3025 continue;
3026
3027 symval = (h->root.u.def.value
3028 + h->root.u.def.section->output_section->vma
3029 + h->root.u.def.section->output_offset);
3030 #ifdef DEBUG_RELAX
3031 fprintf (stderr,
3032 "relax longjump defined: sec: %s, name: %s, value: %x + %x + %x addend %x\n",
3033 sec->name, h->root.root.string, h->root.u.def.value,
3034 sec->output_section->vma, sec->output_offset, irel->r_addend);
3035 #endif
3036 }
3037
3038 addend = irel->r_addend;
3039
3040 foff = (symval + addend
3041 - (irel->r_offset
3042 + sec->output_section->vma
3043 + sec->output_offset
3044 + 4));
3045 #ifdef DEBUG_RELAX
3046 fprintf (stderr, "relax longjump r_offset 0x%x ptr 0x%x symbol 0x%x addend 0x%x distance 0x%x\n",
3047 irel->r_offset,
3048 (irel->r_offset
3049 + sec->output_section->vma
3050 + sec->output_offset),
3051 symval, addend, foff);
3052 #endif
3053 if (foff < -0x100000 || foff >= 0x100000)
3054 /* After all that work, we can't shorten this function call. */
3055 continue;
3056
3057 /* For simplicity of coding, we are going to modify the section
3058 contents, the section relocs, and the BFD symbol table. We
3059 must tell the rest of the code not to free up this
3060 information. It would be possible to instead create a table
3061 of changes which have to be made, as is done in coff-mips.c;
3062 that would be more work, but would require less memory when
3063 the linker is run. */
3064 elf_section_data (sec)->relocs = internal_relocs;
3065 free_relocs = NULL;
3066
3067 elf_section_data (sec)->this_hdr.contents = contents;
3068 free_contents = NULL;
3069
3070 symtab_hdr->contents = (bfd_byte *) extsyms;
3071 free_extsyms = NULL;
3072
3073 if (foff < -0x100 || foff >= 0x100)
3074 {
3075 /* Replace the long jump with a jr. */
3076
3077 irel->r_info =
3078 ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_V850_22_PCREL);
3079
3080 irel->r_addend = addend;
3081 addend = 0;
3082
3083 if (ELF32_R_SYM (hi_irelfn->r_info) < symtab_hdr->sh_info)
3084 /* If this needs to be changed because of future relaxing,
3085 it will be handled here like other internal IND12W
3086 relocs. */
3087 bfd_put_32 (abfd,
3088 0x00000780 | ((addend << 15) & 0xffff0000) | ((addend >> 17) & 0xf),
3089 contents + irel->r_offset);
3090 else
3091 /* We can't fully resolve this yet, because the external
3092 symbol value may be changed by future relaxing.
3093 We let the final link phase handle it. */
3094 bfd_put_32 (abfd, 0x00000780, contents + irel->r_offset);
3095
3096 hi_irelfn->r_info =
3097 ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info), R_V850_NONE);
3098 lo_irelfn->r_info =
3099 ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info), R_V850_NONE);
3100 if (!v850_elf_relax_delete_bytes (abfd, sec,
3101 irel->r_offset + 4, toaddr, 6))
3102 goto error_return;
3103
3104 align_pad_size += 6;
3105 }
3106 else
3107 {
3108 /* Replace the long jump with a br. */
3109
3110 irel->r_info =
3111 ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_V850_9_PCREL);
3112
3113 irel->r_addend = addend;
3114 addend = 0;
3115
3116 if (ELF32_R_SYM (hi_irelfn->r_info) < symtab_hdr->sh_info)
3117 /* If this needs to be changed because of future relaxing,
3118 it will be handled here like other internal IND12W
3119 relocs. */
3120 bfd_put_16 (abfd,
3121 0x0585 | ((addend << 10) & 0xf800) | ((addend << 3) & 0x0070),
3122 contents + irel->r_offset);
3123 else
3124 /* We can't fully resolve this yet, because the external
3125 symbol value may be changed by future relaxing.
3126 We let the final link phase handle it. */
3127 bfd_put_16 (abfd, 0x0585, contents + irel->r_offset);
3128
3129 hi_irelfn->r_info =
3130 ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info), R_V850_NONE);
3131 lo_irelfn->r_info =
3132 ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info), R_V850_NONE);
3133 if (!v850_elf_relax_delete_bytes (abfd, sec,
3134 irel->r_offset + 2, toaddr, 8))
3135 goto error_return;
3136
3137 align_pad_size += 8;
3138 }
3139 }
3140 }
3141
3142 irelalign = NULL;
3143 for (irel = internal_relocs; irel < irelend; irel++)
3144 {
3145 if (ELF32_R_TYPE (irel->r_info) == (int) R_V850_ALIGN
3146 && irel->r_offset == toaddr)
3147 {
3148 irel->r_offset -= align_pad_size;
3149
3150 if (irelalign == NULL || irelalign->r_addend > irel->r_addend)
3151 irelalign = irel;
3152 }
3153 }
3154
3155 addr = toaddr;
3156 }
3157
3158 if (!irelalign)
3159 {
3160 #ifdef DEBUG_RELAX
3161 fprintf (stderr, "relax pad %d shorten %d -> %d\n",
3162 align_pad_size,
3163 sec->_cooked_size,
3164 sec->_cooked_size - align_pad_size);
3165 #endif
3166 sec->_cooked_size -= align_pad_size;
3167 }
3168
3169 return true;
3170
3171 error_return:
3172 if (free_relocs != NULL)
3173 free (free_relocs);
3174
3175 if (free_contents != NULL)
3176 free (free_contents);
3177
3178 if (free_extsyms != NULL)
3179 free (free_extsyms);
3180
3181 return false;
3182 }
3183 \f
3184 #define TARGET_LITTLE_SYM bfd_elf32_v850_vec
3185 #define TARGET_LITTLE_NAME "elf32-v850"
3186 #define ELF_ARCH bfd_arch_v850
3187 #define ELF_MACHINE_CODE EM_V850
3188 #define ELF_MACHINE_ALT1 EM_CYGNUS_V850
3189 #define ELF_MAXPAGESIZE 0x1000
3190
3191 #define elf_info_to_howto v850_elf_info_to_howto_rela
3192 #define elf_info_to_howto_rel v850_elf_info_to_howto_rel
3193
3194 #define elf_backend_check_relocs v850_elf_check_relocs
3195 #define elf_backend_relocate_section v850_elf_relocate_section
3196 #define elf_backend_object_p v850_elf_object_p
3197 #define elf_backend_final_write_processing v850_elf_final_write_processing
3198 #define elf_backend_section_from_bfd_section v850_elf_section_from_bfd_section
3199 #define elf_backend_symbol_processing v850_elf_symbol_processing
3200 #define elf_backend_add_symbol_hook v850_elf_add_symbol_hook
3201 #define elf_backend_link_output_symbol_hook v850_elf_link_output_symbol_hook
3202 #define elf_backend_section_from_shdr v850_elf_section_from_shdr
3203 #define elf_backend_fake_sections v850_elf_fake_sections
3204 #define elf_backend_gc_mark_hook v850_elf_gc_mark_hook
3205 #define elf_backend_gc_sweep_hook v850_elf_gc_sweep_hook
3206
3207 #define elf_backend_can_gc_sections 1
3208 #define elf_backend_rela_normal 1
3209
3210 #define bfd_elf32_bfd_is_local_label_name v850_elf_is_local_label_name
3211 #define bfd_elf32_bfd_reloc_type_lookup v850_elf_reloc_type_lookup
3212 #define bfd_elf32_bfd_merge_private_bfd_data v850_elf_merge_private_bfd_data
3213 #define bfd_elf32_bfd_set_private_flags v850_elf_set_private_flags
3214 #define bfd_elf32_bfd_print_private_bfd_data v850_elf_print_private_bfd_data
3215 #define bfd_elf32_bfd_relax_section v850_elf_relax_section
3216
3217 #define elf_symbol_leading_char '_'
3218
3219 #include "elf32-target.h"