]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - bfd/elf32-v850.c
21df3c8b9ac21b46a41692b6f685fe45da3334cd
[thirdparty/binutils-gdb.git] / bfd / elf32-v850.c
1 /* V850-specific support for 32-bit ELF
2 Copyright (C) 1996-2019 Free Software Foundation, Inc.
3
4 This file is part of BFD, the Binary File Descriptor library.
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
19 MA 02110-1301, USA. */
20
21
22 /* XXX FIXME: This code is littered with 32bit int, 16bit short, 8bit char
23 dependencies. As is the gas & simulator code for the v850. */
24
25 #include "sysdep.h"
26 #include "bfd.h"
27 #include "bfdlink.h"
28 #include "libbfd.h"
29 #include "elf-bfd.h"
30 #include "elf/v850.h"
31 #include "libiberty.h"
32
33 /* Sign-extend a 17-bit number. */
34 #define SEXT17(x) ((((x) & 0x1ffff) ^ 0x10000) - 0x10000)
35
36 /* Sign-extend a 22-bit number. */
37 #define SEXT22(x) ((((x) & 0x3fffff) ^ 0x200000) - 0x200000)
38
39 static reloc_howto_type v850_elf_howto_table[];
40
41 /* Look through the relocs for a section during the first phase, and
42 allocate space in the global offset table or procedure linkage
43 table. */
44
45 static bfd_boolean
46 v850_elf_check_relocs (bfd *abfd,
47 struct bfd_link_info *info,
48 asection *sec,
49 const Elf_Internal_Rela *relocs)
50 {
51 bfd_boolean ret = TRUE;
52 Elf_Internal_Shdr *symtab_hdr;
53 struct elf_link_hash_entry **sym_hashes;
54 const Elf_Internal_Rela *rel;
55 const Elf_Internal_Rela *rel_end;
56 unsigned int r_type;
57 int other = 0;
58 const char *common = NULL;
59
60 if (bfd_link_relocatable (info))
61 return TRUE;
62
63 #ifdef DEBUG
64 _bfd_error_handler ("v850_elf_check_relocs called for section %pA in %pB",
65 sec, abfd);
66 #endif
67
68 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
69 sym_hashes = elf_sym_hashes (abfd);
70
71 rel_end = relocs + sec->reloc_count;
72 for (rel = relocs; rel < rel_end; rel++)
73 {
74 unsigned long r_symndx;
75 struct elf_link_hash_entry *h;
76
77 r_symndx = ELF32_R_SYM (rel->r_info);
78 if (r_symndx < symtab_hdr->sh_info)
79 h = NULL;
80 else
81 {
82 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
83 while (h->root.type == bfd_link_hash_indirect
84 || h->root.type == bfd_link_hash_warning)
85 h = (struct elf_link_hash_entry *) h->root.u.i.link;
86 }
87
88 r_type = ELF32_R_TYPE (rel->r_info);
89 switch (r_type)
90 {
91 default:
92 break;
93
94 /* This relocation describes the C++ object vtable hierarchy.
95 Reconstruct it for later use during GC. */
96 case R_V850_GNU_VTINHERIT:
97 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
98 return FALSE;
99 break;
100
101 /* This relocation describes which C++ vtable entries
102 are actually used. Record for later use during GC. */
103 case R_V850_GNU_VTENTRY:
104 if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
105 return FALSE;
106 break;
107
108 case R_V850_SDA_16_16_SPLIT_OFFSET:
109 case R_V850_SDA_16_16_OFFSET:
110 case R_V850_SDA_15_16_OFFSET:
111 case R_V810_GPWLO_1:
112 case R_V850_HWLO:
113 case R_V850_HWLO_1:
114 other = V850_OTHER_SDA;
115 common = ".scommon";
116 goto small_data_common;
117
118 case R_V850_ZDA_16_16_SPLIT_OFFSET:
119 case R_V850_ZDA_16_16_OFFSET:
120 case R_V850_ZDA_15_16_OFFSET:
121 other = V850_OTHER_ZDA;
122 common = ".zcommon";
123 goto small_data_common;
124
125 case R_V850_TDA_4_4_OFFSET:
126 case R_V850_TDA_4_5_OFFSET:
127 case R_V850_TDA_7_7_OFFSET:
128 case R_V850_TDA_6_8_OFFSET:
129 case R_V850_TDA_7_8_OFFSET:
130 case R_V850_TDA_16_16_OFFSET:
131 other = V850_OTHER_TDA;
132 common = ".tcommon";
133 /* fall through */
134
135 #define V850_OTHER_MASK (V850_OTHER_TDA | V850_OTHER_SDA | V850_OTHER_ZDA)
136
137 small_data_common:
138 if (h)
139 {
140 /* Flag which type of relocation was used. */
141 h->other |= other;
142 if ((h->other & V850_OTHER_MASK) != (other & V850_OTHER_MASK)
143 && (h->other & V850_OTHER_ERROR) == 0)
144 {
145 const char * msg;
146 static char buff[200]; /* XXX */
147
148 switch (h->other & V850_OTHER_MASK)
149 {
150 default:
151 msg = _("variable `%s' cannot occupy in multiple small data regions");
152 break;
153 case V850_OTHER_SDA | V850_OTHER_ZDA | V850_OTHER_TDA:
154 msg = _("variable `%s' can only be in one of the small, zero, and tiny data regions");
155 break;
156 case V850_OTHER_SDA | V850_OTHER_ZDA:
157 msg = _("variable `%s' cannot be in both small and zero data regions simultaneously");
158 break;
159 case V850_OTHER_SDA | V850_OTHER_TDA:
160 msg = _("variable `%s' cannot be in both small and tiny data regions simultaneously");
161 break;
162 case V850_OTHER_ZDA | V850_OTHER_TDA:
163 msg = _("variable `%s' cannot be in both zero and tiny data regions simultaneously");
164 break;
165 }
166
167 sprintf (buff, msg, h->root.root.string);
168 info->callbacks->warning (info, buff, h->root.root.string,
169 abfd, h->root.u.def.section,
170 (bfd_vma) 0);
171
172 bfd_set_error (bfd_error_bad_value);
173 h->other |= V850_OTHER_ERROR;
174 ret = FALSE;
175 }
176 }
177
178 if (h && h->root.type == bfd_link_hash_common
179 && h->root.u.c.p
180 && !strcmp (bfd_section_name (h->root.u.c.p->section), "COMMON"))
181 {
182 asection * section;
183
184 section = h->root.u.c.p->section = bfd_make_section_old_way (abfd, common);
185 section->flags |= SEC_IS_COMMON;
186 }
187
188 #ifdef DEBUG
189 fprintf (stderr, "v850_elf_check_relocs, found %s relocation for %s%s\n",
190 v850_elf_howto_table[ (int)r_type ].name,
191 (h && h->root.root.string) ? h->root.root.string : "<unknown>",
192 (h->root.type == bfd_link_hash_common) ? ", symbol is common" : "");
193 #endif
194 break;
195 }
196 }
197
198 return ret;
199 }
200
201 /* In the old version, when an entry was checked out from the table,
202 it was deleted. This produced an error if the entry was needed
203 more than once, as the second attempted retry failed.
204
205 In the current version, the entry is not deleted, instead we set
206 the field 'found' to TRUE. If a second lookup matches the same
207 entry, then we know that the hi16s reloc has already been updated
208 and does not need to be updated a second time.
209
210 TODO - TOFIX: If it is possible that we need to restore 2 different
211 addresses from the same table entry, where the first generates an
212 overflow, whilst the second do not, then this code will fail. */
213
214 typedef struct hi16s_location
215 {
216 bfd_vma addend;
217 bfd_byte * address;
218 unsigned long counter;
219 bfd_boolean found;
220 struct hi16s_location * next;
221 }
222 hi16s_location;
223
224 static hi16s_location * previous_hi16s;
225 static hi16s_location * free_hi16s;
226 static unsigned long hi16s_counter;
227
228 static void
229 remember_hi16s_reloc (bfd *abfd, bfd_vma addend, bfd_byte *address)
230 {
231 hi16s_location * entry = NULL;
232 bfd_size_type amt = sizeof (* free_hi16s);
233
234 /* Find a free structure. */
235 if (free_hi16s == NULL)
236 free_hi16s = bfd_zalloc (abfd, amt);
237
238 entry = free_hi16s;
239 free_hi16s = free_hi16s->next;
240
241 entry->addend = addend;
242 entry->address = address;
243 entry->counter = hi16s_counter ++;
244 entry->found = FALSE;
245 entry->next = previous_hi16s;
246 previous_hi16s = entry;
247
248 /* Cope with wrap around of our counter. */
249 if (hi16s_counter == 0)
250 {
251 /* XXX: Assume that all counter entries differ only in their low 16 bits. */
252 for (entry = previous_hi16s; entry != NULL; entry = entry->next)
253 entry->counter &= 0xffff;
254
255 hi16s_counter = 0x10000;
256 }
257 }
258
259 static bfd_byte *
260 find_remembered_hi16s_reloc (bfd_vma addend, bfd_boolean *already_found)
261 {
262 hi16s_location *match = NULL;
263 hi16s_location *entry;
264 bfd_byte *addr;
265
266 /* Search the table. Record the most recent entry that matches. */
267 for (entry = previous_hi16s; entry; entry = entry->next)
268 {
269 if (entry->addend == addend
270 && (match == NULL || match->counter < entry->counter))
271 {
272 match = entry;
273 }
274 }
275
276 if (match == NULL)
277 return NULL;
278
279 /* Extract the address. */
280 addr = match->address;
281
282 /* Remember if this entry has already been used before. */
283 if (already_found)
284 * already_found = match->found;
285
286 /* Note that this entry has now been used. */
287 match->found = TRUE;
288
289 return addr;
290 }
291
292 /* Calculate the final operand value for a R_V850_LO16 or
293 R_V850_LO16_SPLIT_OFFSET. *INSN is the current operand value and
294 ADDEND is the sum of the relocation symbol and offset. Store the
295 operand value in *INSN and return true on success.
296
297 The assembler has already done some of this: If the value stored in
298 the instruction has its 15th bit set, (counting from zero) then the
299 assembler will have added 1 to the value stored in the associated
300 HI16S reloc. So for example, these relocations:
301
302 movhi hi( fred ), r0, r1
303 movea lo( fred ), r1, r1
304
305 will store 0 in the value fields for the MOVHI and MOVEA instructions
306 and addend will be the address of fred, but for these instructions:
307
308 movhi hi( fred + 0x123456 ), r0, r1
309 movea lo( fred + 0x123456 ), r1, r1
310
311 the value stored in the MOVHI instruction will be 0x12 and the value
312 stored in the MOVEA instruction will be 0x3456. If however the
313 instructions were:
314
315 movhi hi( fred + 0x10ffff ), r0, r1
316 movea lo( fred + 0x10ffff ), r1, r1
317
318 then the value stored in the MOVHI instruction would be 0x11 (not
319 0x10) and the value stored in the MOVEA instruction would be 0xffff.
320 Thus (assuming for the moment that the addend is 0), at run time the
321 MOVHI instruction loads 0x110000 into r1, then the MOVEA instruction
322 adds 0xffffffff (sign extension!) producing 0x10ffff. Similarly if
323 the instructions were:
324
325 movhi hi( fred - 1 ), r0, r1
326 movea lo( fred - 1 ), r1, r1
327
328 then 0 is stored in the MOVHI instruction and -1 is stored in the
329 MOVEA instruction.
330
331 Overflow can occur if the addition of the value stored in the
332 instruction plus the addend sets the 15th bit when before it was clear.
333 This is because the 15th bit will be sign extended into the high part,
334 thus reducing its value by one, but since the 15th bit was originally
335 clear, the assembler will not have added 1 to the previous HI16S reloc
336 to compensate for this effect. For example:
337
338 movhi hi( fred + 0x123456 ), r0, r1
339 movea lo( fred + 0x123456 ), r1, r1
340
341 The value stored in HI16S reloc is 0x12, the value stored in the LO16
342 reloc is 0x3456. If we assume that the address of fred is 0x00007000
343 then the relocations become:
344
345 HI16S: 0x0012 + (0x00007000 >> 16) = 0x12
346 LO16: 0x3456 + (0x00007000 & 0xffff) = 0xa456
347
348 but when the instructions are executed, the MOVEA instruction's value
349 is signed extended, so the sum becomes:
350
351 0x00120000
352 + 0xffffa456
353 ------------
354 0x0011a456 but 'fred + 0x123456' = 0x0012a456
355
356 Note that if the 15th bit was set in the value stored in the LO16
357 reloc, then we do not have to do anything:
358
359 movhi hi( fred + 0x10ffff ), r0, r1
360 movea lo( fred + 0x10ffff ), r1, r1
361
362 HI16S: 0x0011 + (0x00007000 >> 16) = 0x11
363 LO16: 0xffff + (0x00007000 & 0xffff) = 0x6fff
364
365 0x00110000
366 + 0x00006fff
367 ------------
368 0x00116fff = fred + 0x10ffff = 0x7000 + 0x10ffff
369
370 Overflow can also occur if the computation carries into the 16th bit
371 and it also results in the 15th bit having the same value as the 15th
372 bit of the original value. What happens is that the HI16S reloc
373 will have already examined the 15th bit of the original value and
374 added 1 to the high part if the bit is set. This compensates for the
375 sign extension of 15th bit of the result of the computation. But now
376 there is a carry into the 16th bit, and this has not been allowed for.
377
378 So, for example if fred is at address 0xf000:
379
380 movhi hi( fred + 0xffff ), r0, r1 [bit 15 of the offset is set]
381 movea lo( fred + 0xffff ), r1, r1
382
383 HI16S: 0x0001 + (0x0000f000 >> 16) = 0x0001
384 LO16: 0xffff + (0x0000f000 & 0xffff) = 0xefff (carry into bit 16 is lost)
385
386 0x00010000
387 + 0xffffefff
388 ------------
389 0x0000efff but 'fred + 0xffff' = 0x0001efff
390
391 Similarly, if the 15th bit remains clear, but overflow occurs into
392 the 16th bit then (assuming the address of fred is 0xf000):
393
394 movhi hi( fred + 0x7000 ), r0, r1 [bit 15 of the offset is clear]
395 movea lo( fred + 0x7000 ), r1, r1
396
397 HI16S: 0x0000 + (0x0000f000 >> 16) = 0x0000
398 LO16: 0x7000 + (0x0000f000 & 0xffff) = 0x6fff (carry into bit 16 is lost)
399
400 0x00000000
401 + 0x00006fff
402 ------------
403 0x00006fff but 'fred + 0x7000' = 0x00016fff
404
405 Note - there is no need to change anything if a carry occurs, and the
406 15th bit changes its value from being set to being clear, as the HI16S
407 reloc will have already added in 1 to the high part for us:
408
409 movhi hi( fred + 0xffff ), r0, r1 [bit 15 of the offset is set]
410 movea lo( fred + 0xffff ), r1, r1
411
412 HI16S: 0x0001 + (0x00007000 >> 16)
413 LO16: 0xffff + (0x00007000 & 0xffff) = 0x6fff (carry into bit 16 is lost)
414
415 0x00010000
416 + 0x00006fff (bit 15 not set, so the top half is zero)
417 ------------
418 0x00016fff which is right (assuming that fred is at 0x7000)
419
420 but if the 15th bit goes from being clear to being set, then we must
421 once again handle overflow:
422
423 movhi hi( fred + 0x7000 ), r0, r1 [bit 15 of the offset is clear]
424 movea lo( fred + 0x7000 ), r1, r1
425
426 HI16S: 0x0000 + (0x0000ffff >> 16)
427 LO16: 0x7000 + (0x0000ffff & 0xffff) = 0x6fff (carry into bit 16)
428
429 0x00000000
430 + 0x00006fff (bit 15 not set, so the top half is zero)
431 ------------
432 0x00006fff which is wrong (assuming that fred is at 0xffff). */
433
434 static bfd_boolean
435 v850_elf_perform_lo16_relocation (bfd *abfd, unsigned long *insn,
436 unsigned long addend)
437 {
438 #define BIT15_SET(x) ((x) & 0x8000)
439 #define OVERFLOWS(a,i) ((((a) & 0xffff) + (i)) > 0xffff)
440
441 if ((BIT15_SET (*insn + addend) && ! BIT15_SET (addend))
442 || (OVERFLOWS (addend, *insn)
443 && ((! BIT15_SET (*insn)) || (BIT15_SET (addend)))))
444 {
445 bfd_boolean already_updated;
446 bfd_byte *hi16s_address = find_remembered_hi16s_reloc
447 (addend, & already_updated);
448
449 /* Amend the matching HI16_S relocation. */
450 if (hi16s_address != NULL)
451 {
452 if (! already_updated)
453 {
454 unsigned long hi_insn = bfd_get_16 (abfd, hi16s_address);
455 hi_insn += 1;
456 bfd_put_16 (abfd, hi_insn, hi16s_address);
457 }
458 }
459 else
460 {
461 _bfd_error_handler (_("failed to find previous HI16 reloc"));
462 return FALSE;
463 }
464 }
465 #undef OVERFLOWS
466 #undef BIT15_SET
467
468 /* Do not complain if value has top bit set, as this has been
469 anticipated. */
470 *insn = (*insn + addend) & 0xffff;
471 return TRUE;
472 }
473
474 /* FIXME: The code here probably ought to be removed and the code in reloc.c
475 allowed to do its stuff instead. At least for most of the relocs, anyway. */
476
477 static bfd_reloc_status_type
478 v850_elf_perform_relocation (bfd *abfd,
479 unsigned int r_type,
480 bfd_vma addend,
481 bfd_byte *address)
482 {
483 unsigned long insn;
484 unsigned long result;
485 bfd_signed_vma saddend = (bfd_signed_vma) addend;
486
487 switch (r_type)
488 {
489 default:
490 #ifdef DEBUG
491 _bfd_error_handler ("%pB: unsupported relocation type %#x",
492 abfd, r_type);
493 #endif
494 return bfd_reloc_notsupported;
495
496 case R_V850_REL32:
497 case R_V850_ABS32:
498 case R_V810_WORD:
499 case R_V850_PC32:
500 bfd_put_32 (abfd, addend, address);
501 return bfd_reloc_ok;
502
503 case R_V850_WLO23:
504 case R_V850_23:
505 insn = bfd_get_32 (abfd, address);
506 insn &= ~((0x7f << 4) | (0x7fff80 << (16-7)));
507 insn |= ((addend & 0x7f) << 4) | ((addend & 0x7fff80) << (16-7));
508 bfd_put_32 (abfd, (bfd_vma) insn, address);
509 return bfd_reloc_ok;
510
511 case R_V850_PCR22:
512 case R_V850_22_PCREL:
513 if (saddend > 0x1fffff || saddend < -0x200000)
514 return bfd_reloc_overflow;
515
516 if ((addend % 2) != 0)
517 return bfd_reloc_dangerous;
518
519 insn = bfd_get_32 (abfd, address);
520 insn &= ~0xfffe003f;
521 insn |= (((addend & 0xfffe) << 16) | ((addend & 0x3f0000) >> 16));
522 bfd_put_32 (abfd, (bfd_vma) insn, address);
523 return bfd_reloc_ok;
524
525 case R_V850_PC17:
526 case R_V850_17_PCREL:
527 if (saddend > 0xffff || saddend < -0x10000)
528 return bfd_reloc_overflow;
529
530 if ((addend % 2) != 0)
531 return bfd_reloc_dangerous;
532
533 insn = bfd_get_32 (abfd, address);
534 insn &= ~ 0xfffe0010;
535 insn |= ((addend & 0xfffe) << 16) | ((addend & 0x10000) >> (16-4));
536 break;
537
538 case R_V850_PC16U:
539 case R_V850_16_PCREL:
540 if ((saddend < -0xffff) || (saddend > 0))
541 return bfd_reloc_overflow;
542
543 if ((addend % 2) != 0)
544 return bfd_reloc_dangerous;
545
546 insn = bfd_get_16 (abfd, address);
547 insn &= ~0xfffe;
548 insn |= (-addend & 0xfffe);
549 break;
550
551 case R_V850_PC9:
552 case R_V850_9_PCREL:
553 if (saddend > 0xff || saddend < -0x100)
554 return bfd_reloc_overflow;
555
556 if ((addend % 2) != 0)
557 return bfd_reloc_dangerous;
558
559 insn = bfd_get_16 (abfd, address);
560 insn &= ~ 0xf870;
561 insn |= ((addend & 0x1f0) << 7) | ((addend & 0x0e) << 3);
562 break;
563
564 case R_V810_WHI:
565 case R_V850_HI16:
566 addend += (bfd_get_16 (abfd, address) << 16);
567 addend = (addend >> 16);
568 insn = addend;
569 break;
570
571 case R_V810_WHI1:
572 case R_V850_HI16_S:
573 /* Remember where this relocation took place. */
574 remember_hi16s_reloc (abfd, addend, address);
575
576 addend += (bfd_get_16 (abfd, address) << 16);
577 addend = (addend >> 16) + ((addend & 0x8000) != 0);
578
579 /* This relocation cannot overflow. */
580 if (addend > 0xffff)
581 addend = 0;
582
583 insn = addend;
584 break;
585
586 case R_V810_WLO:
587 case R_V850_LO16:
588 insn = bfd_get_16 (abfd, address);
589 if (! v850_elf_perform_lo16_relocation (abfd, &insn, addend))
590 return bfd_reloc_overflow;
591 break;
592
593 case R_V810_BYTE:
594 case R_V850_8:
595 addend += (char) bfd_get_8 (abfd, address);
596
597 saddend = (bfd_signed_vma) addend;
598
599 if (saddend > 0x7f || saddend < -0x80)
600 return bfd_reloc_overflow;
601
602 bfd_put_8 (abfd, addend, address);
603 return bfd_reloc_ok;
604
605 case R_V850_CALLT_16_16_OFFSET:
606 addend += bfd_get_16 (abfd, address);
607
608 saddend = (bfd_signed_vma) addend;
609
610 if (saddend > 0xffff || saddend < 0)
611 return bfd_reloc_overflow;
612
613 insn = addend;
614 break;
615
616 case R_V850_CALLT_15_16_OFFSET:
617 insn = bfd_get_16 (abfd, address);
618
619 addend += insn & 0xfffe;
620
621 saddend = (bfd_signed_vma) addend;
622
623 if (saddend > 0xffff || saddend < 0)
624 return bfd_reloc_overflow;
625
626 insn = (0xfffe & addend)
627 | (insn & ~0xfffe);
628 break;
629
630 case R_V850_CALLT_6_7_OFFSET:
631 insn = bfd_get_16 (abfd, address);
632 addend += ((insn & 0x3f) << 1);
633
634 saddend = (bfd_signed_vma) addend;
635
636 if (saddend > 0x7e || saddend < 0)
637 return bfd_reloc_overflow;
638
639 if (addend & 1)
640 return bfd_reloc_dangerous;
641
642 insn &= 0xff80;
643 insn |= (addend >> 1);
644 break;
645
646 case R_V850_16:
647 case R_V810_HWORD:
648 case R_V850_SDA_16_16_OFFSET:
649 case R_V850_ZDA_16_16_OFFSET:
650 case R_V850_TDA_16_16_OFFSET:
651 addend += bfd_get_16 (abfd, address);
652
653 saddend = (bfd_signed_vma) addend;
654
655 if (saddend > 0x7fff || saddend < -0x8000)
656 return bfd_reloc_overflow;
657
658 insn = addend;
659 break;
660
661 case R_V850_16_S1:
662 case R_V850_SDA_15_16_OFFSET:
663 case R_V850_ZDA_15_16_OFFSET:
664 case R_V810_GPWLO_1:
665 insn = bfd_get_16 (abfd, address);
666 addend += (insn & 0xfffe);
667
668 saddend = (bfd_signed_vma) addend;
669
670 if (saddend > 0x7ffe || saddend < -0x8000)
671 return bfd_reloc_overflow;
672
673 if (addend & 1)
674 return bfd_reloc_dangerous;
675
676 insn = (addend &~ (bfd_vma) 1) | (insn & 1);
677 break;
678
679 case R_V850_TDA_6_8_OFFSET:
680 insn = bfd_get_16 (abfd, address);
681 addend += ((insn & 0x7e) << 1);
682
683 saddend = (bfd_signed_vma) addend;
684
685 if (saddend > 0xfc || saddend < 0)
686 return bfd_reloc_overflow;
687
688 if (addend & 3)
689 return bfd_reloc_dangerous;
690
691 insn &= 0xff81;
692 insn |= (addend >> 1);
693 break;
694
695 case R_V850_TDA_7_8_OFFSET:
696 insn = bfd_get_16 (abfd, address);
697 addend += ((insn & 0x7f) << 1);
698
699 saddend = (bfd_signed_vma) addend;
700
701 if (saddend > 0xfe || saddend < 0)
702 return bfd_reloc_overflow;
703
704 if (addend & 1)
705 return bfd_reloc_dangerous;
706
707 insn &= 0xff80;
708 insn |= (addend >> 1);
709 break;
710
711 case R_V850_TDA_7_7_OFFSET:
712 insn = bfd_get_16 (abfd, address);
713 addend += insn & 0x7f;
714
715 saddend = (bfd_signed_vma) addend;
716
717 if (saddend > 0x7f || saddend < 0)
718 return bfd_reloc_overflow;
719
720 insn &= 0xff80;
721 insn |= addend;
722 break;
723
724 case R_V850_TDA_4_5_OFFSET:
725 insn = bfd_get_16 (abfd, address);
726 addend += ((insn & 0xf) << 1);
727
728 saddend = (bfd_signed_vma) addend;
729
730 if (saddend > 0x1e || saddend < 0)
731 return bfd_reloc_overflow;
732
733 if (addend & 1)
734 return bfd_reloc_dangerous;
735
736 insn &= 0xfff0;
737 insn |= (addend >> 1);
738 break;
739
740 case R_V850_TDA_4_4_OFFSET:
741 insn = bfd_get_16 (abfd, address);
742 addend += insn & 0xf;
743
744 saddend = (bfd_signed_vma) addend;
745
746 if (saddend > 0xf || saddend < 0)
747 return bfd_reloc_overflow;
748
749 insn &= 0xfff0;
750 insn |= addend;
751 break;
752
753 case R_V810_WLO_1:
754 case R_V850_HWLO:
755 case R_V850_HWLO_1:
756 case R_V850_LO16_S1:
757 insn = bfd_get_16 (abfd, address);
758 result = insn & 0xfffe;
759 if (! v850_elf_perform_lo16_relocation (abfd, &result, addend))
760 return bfd_reloc_overflow;
761 if (result & 1)
762 return bfd_reloc_overflow;
763 insn = (result & 0xfffe)
764 | (insn & ~0xfffe);
765 bfd_put_16 (abfd, insn, address);
766 return bfd_reloc_ok;
767
768 case R_V850_BLO:
769 case R_V850_LO16_SPLIT_OFFSET:
770 insn = bfd_get_32 (abfd, address);
771 result = ((insn & 0xfffe0000) >> 16) | ((insn & 0x20) >> 5);
772 if (! v850_elf_perform_lo16_relocation (abfd, &result, addend))
773 return bfd_reloc_overflow;
774 insn = (((result << 16) & 0xfffe0000)
775 | ((result << 5) & 0x20)
776 | (insn & ~0xfffe0020));
777 bfd_put_32 (abfd, insn, address);
778 return bfd_reloc_ok;
779
780 case R_V850_16_SPLIT_OFFSET:
781 case R_V850_SDA_16_16_SPLIT_OFFSET:
782 case R_V850_ZDA_16_16_SPLIT_OFFSET:
783 insn = bfd_get_32 (abfd, address);
784 addend += ((insn & 0xfffe0000) >> 16) + ((insn & 0x20) >> 5);
785
786 saddend = (bfd_signed_vma) addend;
787
788 if (saddend > 0x7fff || saddend < -0x8000)
789 return bfd_reloc_overflow;
790
791 insn &= 0x0001ffdf;
792 insn |= (addend & 1) << 5;
793 insn |= (addend &~ (bfd_vma) 1) << 16;
794
795 bfd_put_32 (abfd, (bfd_vma) insn, address);
796 return bfd_reloc_ok;
797
798 case R_V850_GNU_VTINHERIT:
799 case R_V850_GNU_VTENTRY:
800 return bfd_reloc_ok;
801
802 }
803
804 bfd_put_16 (abfd, (bfd_vma) insn, address);
805 return bfd_reloc_ok;
806 }
807 \f
808 /* Insert the addend into the instruction. */
809
810 static bfd_reloc_status_type
811 v850_elf_reloc (bfd *abfd ATTRIBUTE_UNUSED,
812 arelent *reloc,
813 asymbol *symbol,
814 void * data ATTRIBUTE_UNUSED,
815 asection *isection,
816 bfd *obfd,
817 char **err ATTRIBUTE_UNUSED)
818 {
819 long relocation;
820
821 /* If there is an output BFD,
822 and the symbol is not a section name (which is only defined at final link time),
823 and either we are not putting the addend into the instruction
824 or the addend is zero, so there is nothing to add into the instruction
825 then just fixup the address and return. */
826 if (obfd != NULL
827 && (symbol->flags & BSF_SECTION_SYM) == 0
828 && (! reloc->howto->partial_inplace
829 || reloc->addend == 0))
830 {
831 reloc->address += isection->output_offset;
832 return bfd_reloc_ok;
833 }
834
835 /* Catch relocs involving undefined symbols. */
836 if (bfd_is_und_section (symbol->section)
837 && (symbol->flags & BSF_WEAK) == 0
838 && obfd == NULL)
839 return bfd_reloc_undefined;
840
841 /* We handle final linking of some relocs ourselves. */
842
843 /* Is the address of the relocation really within the section? */
844 if (reloc->address > bfd_get_section_limit (abfd, isection))
845 return bfd_reloc_outofrange;
846
847 /* Work out which section the relocation is targeted at and the
848 initial relocation command value. */
849
850 if (reloc->howto->pc_relative)
851 return bfd_reloc_ok;
852
853 /* Get symbol value. (Common symbols are special.) */
854 if (bfd_is_com_section (symbol->section))
855 relocation = 0;
856 else
857 relocation = symbol->value;
858
859 /* Convert input-section-relative symbol value to absolute + addend. */
860 relocation += symbol->section->output_section->vma;
861 relocation += symbol->section->output_offset;
862 relocation += reloc->addend;
863
864 reloc->addend = relocation;
865 return bfd_reloc_ok;
866 }
867
868 /* This function is used for relocs which are only used
869 for relaxing, which the linker should otherwise ignore. */
870
871 static bfd_reloc_status_type
872 v850_elf_ignore_reloc (bfd *abfd ATTRIBUTE_UNUSED,
873 arelent *reloc_entry,
874 asymbol *symbol ATTRIBUTE_UNUSED,
875 void * data ATTRIBUTE_UNUSED,
876 asection *input_section,
877 bfd *output_bfd,
878 char **error_message ATTRIBUTE_UNUSED)
879 {
880 if (output_bfd != NULL)
881 reloc_entry->address += input_section->output_offset;
882
883 return bfd_reloc_ok;
884 }
885 /* Note: It is REQUIRED that the 'type' value of each entry
886 in this array match the index of the entry in the array.
887 SeeAlso: RELOC_NUBMER in include/elf/v850.h. */
888 static reloc_howto_type v850_elf_howto_table[] =
889 {
890 /* This reloc does nothing. */
891 HOWTO (R_V850_NONE, /* Type. */
892 0, /* Rightshift. */
893 3, /* Size (0 = byte, 1 = short, 2 = long). */
894 0, /* Bitsize. */
895 FALSE, /* PC_relative. */
896 0, /* Bitpos. */
897 complain_overflow_dont, /* Complain_on_overflow. */
898 bfd_elf_generic_reloc, /* Special_function. */
899 "R_V850_NONE", /* Name. */
900 FALSE, /* Partial_inplace. */
901 0, /* Src_mask. */
902 0, /* Dst_mask. */
903 FALSE), /* PCrel_offset. */
904
905 /* A PC relative 9 bit branch. */
906 HOWTO (R_V850_9_PCREL, /* Type. */
907 0, /* Rightshift. */
908 1, /* Size (0 = byte, 1 = short, 2 = long). */
909 9, /* Bitsize. */
910 TRUE, /* PC_relative. */
911 0, /* Bitpos. */
912 complain_overflow_bitfield, /* Complain_on_overflow. */
913 v850_elf_reloc, /* Special_function. */
914 "R_V850_9_PCREL", /* Name. */
915 FALSE, /* Partial_inplace. */
916 0x00ffffff, /* Src_mask. */
917 0x00ffffff, /* Dst_mask. */
918 TRUE), /* PCrel_offset. */
919
920 /* A PC relative 22 bit branch. */
921 HOWTO (R_V850_22_PCREL, /* Type. */
922 0, /* Rightshift. */
923 2, /* Size (0 = byte, 1 = short, 2 = long). */
924 22, /* Bitsize. */
925 TRUE, /* PC_relative. */
926 0, /* Bitpos. */
927 complain_overflow_signed, /* Complain_on_overflow. */
928 v850_elf_reloc, /* Special_function. */
929 "R_V850_22_PCREL", /* Name. */
930 FALSE, /* Partial_inplace. */
931 0x07ffff80, /* Src_mask. */
932 0x07ffff80, /* Dst_mask. */
933 TRUE), /* PCrel_offset. */
934
935 /* High 16 bits of symbol value. */
936 HOWTO (R_V850_HI16_S, /* Type. */
937 0, /* Rightshift. */
938 1, /* Size (0 = byte, 1 = short, 2 = long). */
939 16, /* Bitsize. */
940 FALSE, /* PC_relative. */
941 0, /* Bitpos. */
942 complain_overflow_dont, /* Complain_on_overflow. */
943 v850_elf_reloc, /* Special_function. */
944 "R_V850_HI16_S", /* Name. */
945 FALSE, /* Partial_inplace. */
946 0xffff, /* Src_mask. */
947 0xffff, /* Dst_mask. */
948 FALSE), /* PCrel_offset. */
949
950 /* High 16 bits of symbol value. */
951 HOWTO (R_V850_HI16, /* Type. */
952 0, /* Rightshift. */
953 1, /* Size (0 = byte, 1 = short, 2 = long). */
954 16, /* Bitsize. */
955 FALSE, /* PC_relative. */
956 0, /* Bitpos. */
957 complain_overflow_dont, /* Complain_on_overflow. */
958 v850_elf_reloc, /* Special_function. */
959 "R_V850_HI16", /* Name. */
960 FALSE, /* Partial_inplace. */
961 0xffff, /* Src_mask. */
962 0xffff, /* Dst_mask. */
963 FALSE), /* PCrel_offset. */
964
965 /* Low 16 bits of symbol value. */
966 HOWTO (R_V850_LO16, /* Type. */
967 0, /* Rightshift. */
968 1, /* Size (0 = byte, 1 = short, 2 = long). */
969 16, /* Bitsize. */
970 FALSE, /* PC_relative. */
971 0, /* Bitpos. */
972 complain_overflow_dont, /* Complain_on_overflow. */
973 v850_elf_reloc, /* Special_function. */
974 "R_V850_LO16", /* Name. */
975 FALSE, /* Partial_inplace. */
976 0xffff, /* Src_mask. */
977 0xffff, /* Dst_mask. */
978 FALSE), /* PCrel_offset. */
979
980 /* Simple 32bit reloc. */
981 HOWTO (R_V850_ABS32, /* Type. */
982 0, /* Rightshift. */
983 2, /* Size (0 = byte, 1 = short, 2 = long). */
984 32, /* Bitsize. */
985 FALSE, /* PC_relative. */
986 0, /* Bitpos. */
987 complain_overflow_dont, /* Complain_on_overflow. */
988 v850_elf_reloc, /* Special_function. */
989 "R_V850_ABS32", /* Name. */
990 FALSE, /* Partial_inplace. */
991 0xffffffff, /* Src_mask. */
992 0xffffffff, /* Dst_mask. */
993 FALSE), /* PCrel_offset. */
994
995 /* Simple 16bit reloc. */
996 HOWTO (R_V850_16, /* Type. */
997 0, /* Rightshift. */
998 1, /* Size (0 = byte, 1 = short, 2 = long). */
999 16, /* Bitsize. */
1000 FALSE, /* PC_relative. */
1001 0, /* Bitpos. */
1002 complain_overflow_dont, /* Complain_on_overflow. */
1003 bfd_elf_generic_reloc, /* Special_function. */
1004 "R_V850_16", /* Name. */
1005 FALSE, /* Partial_inplace. */
1006 0xffff, /* Src_mask. */
1007 0xffff, /* Dst_mask. */
1008 FALSE), /* PCrel_offset. */
1009
1010 /* Simple 8bit reloc. */
1011 HOWTO (R_V850_8, /* Type. */
1012 0, /* Rightshift. */
1013 0, /* Size (0 = byte, 1 = short, 2 = long). */
1014 8, /* Bitsize. */
1015 FALSE, /* PC_relative. */
1016 0, /* Bitpos. */
1017 complain_overflow_dont, /* Complain_on_overflow. */
1018 bfd_elf_generic_reloc, /* Special_function. */
1019 "R_V850_8", /* Name. */
1020 FALSE, /* Partial_inplace. */
1021 0xff, /* Src_mask. */
1022 0xff, /* Dst_mask. */
1023 FALSE), /* PCrel_offset. */
1024
1025 /* 16 bit offset from the short data area pointer. */
1026 HOWTO (R_V850_SDA_16_16_OFFSET, /* Type. */
1027 0, /* Rightshift. */
1028 1, /* Size (0 = byte, 1 = short, 2 = long). */
1029 16, /* Bitsize. */
1030 FALSE, /* PC_relative. */
1031 0, /* Bitpos. */
1032 complain_overflow_dont, /* Complain_on_overflow. */
1033 v850_elf_reloc, /* Special_function. */
1034 "R_V850_SDA_16_16_OFFSET", /* Name. */
1035 FALSE, /* Partial_inplace. */
1036 0xffff, /* Src_mask. */
1037 0xffff, /* Dst_mask. */
1038 FALSE), /* PCrel_offset. */
1039
1040 /* 15 bit offset from the short data area pointer. */
1041 HOWTO (R_V850_SDA_15_16_OFFSET, /* Type. */
1042 1, /* Rightshift. */
1043 1, /* Size (0 = byte, 1 = short, 2 = long). */
1044 16, /* Bitsize. */
1045 FALSE, /* PC_relative. */
1046 1, /* Bitpos. */
1047 complain_overflow_dont, /* Complain_on_overflow. */
1048 v850_elf_reloc, /* Special_function. */
1049 "R_V850_SDA_15_16_OFFSET", /* Name. */
1050 FALSE, /* Partial_inplace. */
1051 0xfffe, /* Src_mask. */
1052 0xfffe, /* Dst_mask. */
1053 FALSE), /* PCrel_offset. */
1054
1055 /* 16 bit offset from the zero data area pointer. */
1056 HOWTO (R_V850_ZDA_16_16_OFFSET, /* Type. */
1057 0, /* Rightshift. */
1058 1, /* Size (0 = byte, 1 = short, 2 = long). */
1059 16, /* Bitsize. */
1060 FALSE, /* PC_relative. */
1061 0, /* Bitpos. */
1062 complain_overflow_dont, /* Complain_on_overflow. */
1063 v850_elf_reloc, /* Special_function. */
1064 "R_V850_ZDA_16_16_OFFSET", /* Name. */
1065 FALSE, /* Partial_inplace. */
1066 0xffff, /* Src_mask. */
1067 0xffff, /* Dst_mask. */
1068 FALSE), /* PCrel_offset. */
1069
1070 /* 15 bit offset from the zero data area pointer. */
1071 HOWTO (R_V850_ZDA_15_16_OFFSET, /* Type. */
1072 1, /* Rightshift. */
1073 1, /* Size (0 = byte, 1 = short, 2 = long). */
1074 16, /* Bitsize. */
1075 FALSE, /* PC_relative. */
1076 1, /* Bitpos. */
1077 complain_overflow_dont, /* Complain_on_overflow. */
1078 v850_elf_reloc, /* Special_function. */
1079 "R_V850_ZDA_15_16_OFFSET", /* Name. */
1080 FALSE, /* Partial_inplace. */
1081 0xfffe, /* Src_mask. */
1082 0xfffe, /* Dst_mask. */
1083 FALSE), /* PCrel_offset. */
1084
1085 /* 6 bit offset from the tiny data area pointer. */
1086 HOWTO (R_V850_TDA_6_8_OFFSET, /* Type. */
1087 2, /* Rightshift. */
1088 1, /* Size (0 = byte, 1 = short, 2 = long). */
1089 8, /* Bitsize. */
1090 FALSE, /* PC_relative. */
1091 1, /* Bitpos. */
1092 complain_overflow_dont, /* Complain_on_overflow. */
1093 v850_elf_reloc, /* Special_function. */
1094 "R_V850_TDA_6_8_OFFSET", /* Name. */
1095 FALSE, /* Partial_inplace. */
1096 0x7e, /* Src_mask. */
1097 0x7e, /* Dst_mask. */
1098 FALSE), /* PCrel_offset. */
1099
1100 /* 8 bit offset from the tiny data area pointer. */
1101 HOWTO (R_V850_TDA_7_8_OFFSET, /* Type. */
1102 1, /* Rightshift. */
1103 1, /* Size (0 = byte, 1 = short, 2 = long). */
1104 8, /* Bitsize. */
1105 FALSE, /* PC_relative. */
1106 0, /* Bitpos. */
1107 complain_overflow_dont, /* Complain_on_overflow. */
1108 v850_elf_reloc, /* Special_function. */
1109 "R_V850_TDA_7_8_OFFSET", /* Name. */
1110 FALSE, /* Partial_inplace. */
1111 0x7f, /* Src_mask. */
1112 0x7f, /* Dst_mask. */
1113 FALSE), /* PCrel_offset. */
1114
1115 /* 7 bit offset from the tiny data area pointer. */
1116 HOWTO (R_V850_TDA_7_7_OFFSET, /* Type. */
1117 0, /* Rightshift. */
1118 1, /* Size (0 = byte, 1 = short, 2 = long). */
1119 7, /* Bitsize. */
1120 FALSE, /* PC_relative. */
1121 0, /* Bitpos. */
1122 complain_overflow_dont, /* Complain_on_overflow. */
1123 v850_elf_reloc, /* Special_function. */
1124 "R_V850_TDA_7_7_OFFSET", /* Name. */
1125 FALSE, /* Partial_inplace. */
1126 0x7f, /* Src_mask. */
1127 0x7f, /* Dst_mask. */
1128 FALSE), /* PCrel_offset. */
1129
1130 /* 16 bit offset from the tiny data area pointer! */
1131 HOWTO (R_V850_TDA_16_16_OFFSET, /* Type. */
1132 0, /* Rightshift. */
1133 1, /* Size (0 = byte, 1 = short, 2 = long). */
1134 16, /* Bitsize. */
1135 FALSE, /* PC_relative. */
1136 0, /* Bitpos. */
1137 complain_overflow_dont, /* Complain_on_overflow. */
1138 v850_elf_reloc, /* Special_function. */
1139 "R_V850_TDA_16_16_OFFSET", /* Name. */
1140 FALSE, /* Partial_inplace. */
1141 0xffff, /* Src_mask. */
1142 0xfff, /* Dst_mask. */
1143 FALSE), /* PCrel_offset. */
1144
1145 /* 5 bit offset from the tiny data area pointer. */
1146 HOWTO (R_V850_TDA_4_5_OFFSET, /* Type. */
1147 1, /* Rightshift. */
1148 1, /* Size (0 = byte, 1 = short, 2 = long). */
1149 5, /* Bitsize. */
1150 FALSE, /* PC_relative. */
1151 0, /* Bitpos. */
1152 complain_overflow_dont, /* Complain_on_overflow. */
1153 v850_elf_reloc, /* Special_function. */
1154 "R_V850_TDA_4_5_OFFSET", /* Name. */
1155 FALSE, /* Partial_inplace. */
1156 0x0f, /* Src_mask. */
1157 0x0f, /* Dst_mask. */
1158 FALSE), /* PCrel_offset. */
1159
1160 /* 4 bit offset from the tiny data area pointer. */
1161 HOWTO (R_V850_TDA_4_4_OFFSET, /* Type. */
1162 0, /* Rightshift. */
1163 1, /* Size (0 = byte, 1 = short, 2 = long). */
1164 4, /* Bitsize. */
1165 FALSE, /* PC_relative. */
1166 0, /* Bitpos. */
1167 complain_overflow_dont, /* Complain_on_overflow. */
1168 v850_elf_reloc, /* Special_function. */
1169 "R_V850_TDA_4_4_OFFSET", /* Name. */
1170 FALSE, /* Partial_inplace. */
1171 0x0f, /* Src_mask. */
1172 0x0f, /* Dst_mask. */
1173 FALSE), /* PCrel_offset. */
1174
1175 /* 16 bit offset from the short data area pointer. */
1176 HOWTO (R_V850_SDA_16_16_SPLIT_OFFSET, /* Type. */
1177 0, /* Rightshift. */
1178 2, /* Size (0 = byte, 1 = short, 2 = long). */
1179 16, /* Bitsize. */
1180 FALSE, /* PC_relative. */
1181 0, /* Bitpos. */
1182 complain_overflow_dont, /* Complain_on_overflow. */
1183 v850_elf_reloc, /* Special_function. */
1184 "R_V850_SDA_16_16_SPLIT_OFFSET",/* Name. */
1185 FALSE, /* Partial_inplace. */
1186 0xfffe0020, /* Src_mask. */
1187 0xfffe0020, /* Dst_mask. */
1188 FALSE), /* PCrel_offset. */
1189
1190 /* 16 bit offset from the zero data area pointer. */
1191 HOWTO (R_V850_ZDA_16_16_SPLIT_OFFSET, /* Type. */
1192 0, /* Rightshift. */
1193 2, /* Size (0 = byte, 1 = short, 2 = long). */
1194 16, /* Bitsize. */
1195 FALSE, /* PC_relative. */
1196 0, /* Bitpos. */
1197 complain_overflow_dont, /* Complain_on_overflow. */
1198 v850_elf_reloc, /* Special_function. */
1199 "R_V850_ZDA_16_16_SPLIT_OFFSET",/* Name. */
1200 FALSE, /* Partial_inplace. */
1201 0xfffe0020, /* Src_mask. */
1202 0xfffe0020, /* Dst_mask. */
1203 FALSE), /* PCrel_offset. */
1204
1205 /* 6 bit offset from the call table base pointer. */
1206 HOWTO (R_V850_CALLT_6_7_OFFSET, /* Type. */
1207 0, /* Rightshift. */
1208 1, /* Size (0 = byte, 1 = short, 2 = long). */
1209 7, /* Bitsize. */
1210 FALSE, /* PC_relative. */
1211 0, /* Bitpos. */
1212 complain_overflow_dont, /* Complain_on_overflow. */
1213 v850_elf_reloc, /* Special_function. */
1214 "R_V850_CALLT_6_7_OFFSET", /* Name. */
1215 FALSE, /* Partial_inplace. */
1216 0x3f, /* Src_mask. */
1217 0x3f, /* Dst_mask. */
1218 FALSE), /* PCrel_offset. */
1219
1220 /* 16 bit offset from the call table base pointer. */
1221 HOWTO (R_V850_CALLT_16_16_OFFSET, /* Type. */
1222 0, /* Rightshift. */
1223 1, /* Size (0 = byte, 1 = short, 2 = long). */
1224 16, /* Bitsize. */
1225 FALSE, /* PC_relative. */
1226 0, /* Bitpos. */
1227 complain_overflow_dont, /* Complain_on_overflow. */
1228 v850_elf_reloc, /* Special_function. */
1229 "R_V850_CALLT_16_16_OFFSET", /* Name. */
1230 FALSE, /* Partial_inplace. */
1231 0xffff, /* Src_mask. */
1232 0xffff, /* Dst_mask. */
1233 FALSE), /* PCrel_offset. */
1234
1235
1236 /* GNU extension to record C++ vtable hierarchy */
1237 HOWTO (R_V850_GNU_VTINHERIT, /* Type. */
1238 0, /* Rightshift. */
1239 2, /* Size (0 = byte, 1 = short, 2 = long). */
1240 0, /* Bitsize. */
1241 FALSE, /* PC_relative. */
1242 0, /* Bitpos. */
1243 complain_overflow_dont, /* Complain_on_overflow. */
1244 NULL, /* Special_function. */
1245 "R_V850_GNU_VTINHERIT", /* Name. */
1246 FALSE, /* Partial_inplace. */
1247 0, /* Src_mask. */
1248 0, /* Dst_mask. */
1249 FALSE), /* PCrel_offset. */
1250
1251 /* GNU extension to record C++ vtable member usage. */
1252 HOWTO (R_V850_GNU_VTENTRY, /* Type. */
1253 0, /* Rightshift. */
1254 2, /* Size (0 = byte, 1 = short, 2 = long). */
1255 0, /* Bitsize. */
1256 FALSE, /* PC_relative. */
1257 0, /* Bitpos. */
1258 complain_overflow_dont, /* Complain_on_overflow. */
1259 _bfd_elf_rel_vtable_reloc_fn, /* Special_function. */
1260 "R_V850_GNU_VTENTRY", /* Name. */
1261 FALSE, /* Partial_inplace. */
1262 0, /* Src_mask. */
1263 0, /* Dst_mask. */
1264 FALSE), /* PCrel_offset. */
1265
1266 /* Indicates a .longcall pseudo-op. The compiler will generate a .longcall
1267 pseudo-op when it finds a function call which can be relaxed. */
1268 HOWTO (R_V850_LONGCALL, /* Type. */
1269 0, /* Rightshift. */
1270 2, /* Size (0 = byte, 1 = short, 2 = long). */
1271 32, /* Bitsize. */
1272 TRUE, /* PC_relative. */
1273 0, /* Bitpos. */
1274 complain_overflow_signed, /* Complain_on_overflow. */
1275 v850_elf_ignore_reloc, /* Special_function. */
1276 "R_V850_LONGCALL", /* Name. */
1277 FALSE, /* Partial_inplace. */
1278 0, /* Src_mask. */
1279 0, /* Dst_mask. */
1280 TRUE), /* PCrel_offset. */
1281
1282 /* Indicates a .longjump pseudo-op. The compiler will generate a
1283 .longjump pseudo-op when it finds a branch which can be relaxed. */
1284 HOWTO (R_V850_LONGJUMP, /* Type. */
1285 0, /* Rightshift. */
1286 2, /* Size (0 = byte, 1 = short, 2 = long). */
1287 32, /* Bitsize. */
1288 TRUE, /* PC_relative. */
1289 0, /* Bitpos. */
1290 complain_overflow_signed, /* Complain_on_overflow. */
1291 v850_elf_ignore_reloc, /* Special_function. */
1292 "R_V850_LONGJUMP", /* Name. */
1293 FALSE, /* Partial_inplace. */
1294 0, /* Src_mask. */
1295 0, /* Dst_mask. */
1296 TRUE), /* PCrel_offset. */
1297
1298 HOWTO (R_V850_ALIGN, /* Type. */
1299 0, /* Rightshift. */
1300 1, /* Size (0 = byte, 1 = short, 2 = long). */
1301 0, /* Bitsize. */
1302 FALSE, /* PC_relative. */
1303 0, /* Bitpos. */
1304 complain_overflow_unsigned, /* Complain_on_overflow. */
1305 v850_elf_ignore_reloc, /* Special_function. */
1306 "R_V850_ALIGN", /* Name. */
1307 FALSE, /* Partial_inplace. */
1308 0, /* Src_mask. */
1309 0, /* Dst_mask. */
1310 TRUE), /* PCrel_offset. */
1311
1312 /* Simple pc-relative 32bit reloc. */
1313 HOWTO (R_V850_REL32, /* Type. */
1314 0, /* Rightshift. */
1315 2, /* Size (0 = byte, 1 = short, 2 = long). */
1316 32, /* Bitsize. */
1317 TRUE, /* PC_relative. */
1318 0, /* Bitpos. */
1319 complain_overflow_dont, /* Complain_on_overflow. */
1320 v850_elf_reloc, /* Special_function. */
1321 "R_V850_REL32", /* Name. */
1322 FALSE, /* Partial_inplace. */
1323 0xffffffff, /* Src_mask. */
1324 0xffffffff, /* Dst_mask. */
1325 FALSE), /* PCrel_offset. */
1326
1327 /* An ld.bu version of R_V850_LO16. */
1328 HOWTO (R_V850_LO16_SPLIT_OFFSET, /* Type. */
1329 0, /* Rightshift. */
1330 2, /* Size (0 = byte, 1 = short, 2 = long). */
1331 16, /* Bitsize. */
1332 FALSE, /* PC_relative. */
1333 0, /* Bitpos. */
1334 complain_overflow_dont, /* Complain_on_overflow. */
1335 v850_elf_reloc, /* Special_function. */
1336 "R_V850_LO16_SPLIT_OFFSET", /* Name. */
1337 FALSE, /* Partial_inplace. */
1338 0xfffe0020, /* Src_mask. */
1339 0xfffe0020, /* Dst_mask. */
1340 FALSE), /* PCrel_offset. */
1341
1342 /* A unsigned PC relative 16 bit loop. */
1343 HOWTO (R_V850_16_PCREL, /* Type. */
1344 0, /* Rightshift. */
1345 1, /* Size (0 = byte, 1 = short, 2 = long). */
1346 16, /* Bitsize. */
1347 TRUE, /* PC_relative. */
1348 0, /* Bitpos. */
1349 complain_overflow_bitfield, /* Complain_on_overflow. */
1350 v850_elf_reloc, /* Special_function. */
1351 "R_V850_16_PCREL", /* Name. */
1352 FALSE, /* Partial_inplace. */
1353 0xfffe, /* Src_mask. */
1354 0xfffe, /* Dst_mask. */
1355 TRUE), /* PCrel_offset. */
1356
1357 /* A PC relative 17 bit branch. */
1358 HOWTO (R_V850_17_PCREL, /* Type. */
1359 0, /* Rightshift. */
1360 2, /* Size (0 = byte, 1 = short, 2 = long). */
1361 17, /* Bitsize. */
1362 TRUE, /* PC_relative. */
1363 0, /* Bitpos. */
1364 complain_overflow_bitfield, /* Complain_on_overflow. */
1365 v850_elf_reloc, /* Special_function. */
1366 "R_V850_17_PCREL", /* Name. */
1367 FALSE, /* Partial_inplace. */
1368 0x0010fffe, /* Src_mask. */
1369 0x0010fffe, /* Dst_mask. */
1370 TRUE), /* PCrel_offset. */
1371
1372 /* A 23bit offset ld/st. */
1373 HOWTO (R_V850_23, /* type. */
1374 0, /* rightshift. */
1375 2, /* size (0 = byte, 1 = short, 2 = long). */
1376 23, /* bitsize. */
1377 FALSE, /* pc_relative. */
1378 0, /* bitpos. */
1379 complain_overflow_dont, /* complain_on_overflow. */
1380 v850_elf_reloc, /* special_function. */
1381 "R_V850_23", /* name. */
1382 FALSE, /* partial_inplace. */
1383 0xffff07f0, /* src_mask. */
1384 0xffff07f0, /* dst_mask. */
1385 FALSE), /* pcrel_offset. */
1386
1387 /* A PC relative 32 bit branch. */
1388 HOWTO (R_V850_32_PCREL, /* type. */
1389 1, /* rightshift. */
1390 2, /* size (0 = byte, 1 = short, 2 = long). */
1391 32, /* bitsize. */
1392 TRUE, /* pc_relative. */
1393 1, /* bitpos. */
1394 complain_overflow_signed, /* complain_on_overflow. */
1395 v850_elf_reloc, /* special_function. */
1396 "R_V850_32_PCREL", /* name. */
1397 FALSE, /* partial_inplace. */
1398 0xfffffffe, /* src_mask. */
1399 0xfffffffe, /* dst_mask. */
1400 TRUE), /* pcrel_offset. */
1401
1402 /* A absolute 32 bit branch. */
1403 HOWTO (R_V850_32_ABS, /* type. */
1404 1, /* rightshift. */
1405 2, /* size (0 = byte, 1 = short, 2 = long). */
1406 32, /* bitsize. */
1407 TRUE, /* pc_relative. */
1408 1, /* bitpos. */
1409 complain_overflow_signed, /* complain_on_overflow. */
1410 v850_elf_reloc, /* special_function. */
1411 "R_V850_32_ABS", /* name. */
1412 FALSE, /* partial_inplace. */
1413 0xfffffffe, /* src_mask. */
1414 0xfffffffe, /* dst_mask. */
1415 FALSE), /* pcrel_offset. */
1416
1417 /* High 16 bits of symbol value. */
1418 HOWTO (R_V850_HI16, /* Type. */
1419 0, /* Rightshift. */
1420 1, /* Size (0 = byte, 1 = short, 2 = long). */
1421 16, /* Bitsize. */
1422 FALSE, /* PC_relative. */
1423 0, /* Bitpos. */
1424 complain_overflow_dont, /* Complain_on_overflow. */
1425 v850_elf_reloc, /* Special_function. */
1426 "R_V850_HI16", /* Name. */
1427 FALSE, /* Partial_inplace. */
1428 0xffff, /* Src_mask. */
1429 0xffff, /* Dst_mask. */
1430 FALSE), /* PCrel_offset. */
1431
1432 /* Low 16 bits of symbol value. */
1433 HOWTO (R_V850_16_S1, /* type. */
1434 1, /* rightshift. */
1435 1, /* size (0 = byte, 1 = short, 2 = long). */
1436 16, /* bitsize. */
1437 FALSE, /* pc_relative. */
1438 1, /* bitpos. */
1439 complain_overflow_dont, /* complain_on_overflow. */
1440 v850_elf_reloc, /* special_function. */
1441 "R_V850_16_S1", /* name. */
1442 FALSE, /* partial_inplace. */
1443 0xfffe, /* src_mask. */
1444 0xfffe, /* dst_mask. */
1445 FALSE), /* pcrel_offset. */
1446
1447 /* Low 16 bits of symbol value. */
1448 HOWTO (R_V850_LO16_S1, /* type. */
1449 1, /* rightshift. */
1450 1, /* size (0 = byte, 1 = short, 2 = long). */
1451 16, /* bitsize. */
1452 FALSE, /* pc_relative. */
1453 1, /* bitpos. */
1454 complain_overflow_dont, /* complain_on_overflow. */
1455 v850_elf_reloc, /* special_function. */
1456 "R_V850_LO16_S1", /* name. */
1457 FALSE, /* partial_inplace. */
1458 0xfffe, /* src_mask. */
1459 0xfffe, /* dst_mask. */
1460 FALSE), /* pcrel_offset. */
1461
1462 /* 16 bit offset from the call table base pointer. */
1463 HOWTO (R_V850_CALLT_15_16_OFFSET, /* type. */
1464 1, /* rightshift. */
1465 1, /* size (0 = byte, 1 = short, 2 = long). */
1466 16, /* bitsize. */
1467 FALSE, /* pc_relative. */
1468 1, /* bitpos. */
1469 complain_overflow_dont, /* complain_on_overflow. */
1470 v850_elf_reloc, /* special_function. */
1471 "R_V850_CALLT_15_16_OFFSET", /* name. */
1472 FALSE, /* partial_inplace. */
1473 0xfffe, /* src_mask. */
1474 0xfffe, /* dst_mask. */
1475 FALSE), /* pcrel_offset. */
1476
1477 /* Like R_V850_32 PCREL, but referring to the GOT table entry for
1478 the symbol. */
1479 HOWTO (R_V850_32_GOTPCREL, /* type. */
1480 0, /* rightshift. */
1481 2, /* size (0 = byte, 1 = short, 2 = long). */
1482 32, /* bitsize. */
1483 TRUE, /* pc_relative. */
1484 0, /* bitpos. */
1485 complain_overflow_unsigned, /* complain_on_overflow. */
1486 v850_elf_reloc, /* special_function. */
1487 "R_V850_32_GOTPCREL", /* name. */
1488 FALSE, /* partial_inplace. */
1489 0xffffffff, /* src_mask. */
1490 0xffffffff, /* dst_mask. */
1491 TRUE), /* pcrel_offset. */
1492
1493 /* Like R_V850_SDA_, but referring to the GOT table entry for
1494 the symbol. */
1495 HOWTO (R_V850_16_GOT, /* type. */
1496 0, /* rightshift. */
1497 2, /* size (0 = byte, 1 = short, 2 = long). */
1498 16, /* bitsize. */
1499 FALSE, /* pc_relative. */
1500 0, /* bitpos. */
1501 complain_overflow_unsigned, /* complain_on_overflow. */
1502 bfd_elf_generic_reloc, /* special_function. */
1503 "R_V850_16_GOT", /* name. */
1504 FALSE, /* partial_inplace. */
1505 0xffff, /* src_mask. */
1506 0xffff, /* dst_mask. */
1507 FALSE), /* pcrel_offset. */
1508
1509 HOWTO (R_V850_32_GOT, /* type. */
1510 0, /* rightshift. */
1511 2, /* size (0 = byte, 1 = short, 2 = long). */
1512 32, /* bitsize. */
1513 FALSE, /* pc_relative. */
1514 0, /* bitpos. */
1515 complain_overflow_unsigned, /* complain_on_overflow. */
1516 bfd_elf_generic_reloc, /* special_function. */
1517 "R_V850_32_GOT", /* name. */
1518 FALSE, /* partial_inplace. */
1519 0xffffffff, /* src_mask. */
1520 0xffffffff, /* dst_mask. */
1521 FALSE), /* pcrel_offset. */
1522
1523 /* Like R_V850_22_PCREL, but referring to the procedure linkage table
1524 entry for the symbol. */
1525 HOWTO (R_V850_22_PLT, /* type. */
1526 1, /* rightshift. */
1527 2, /* size (0 = byte, 1 = short, 2 = long). */
1528 22, /* bitsize. */
1529 TRUE, /* pc_relative. */
1530 7, /* bitpos. */
1531 complain_overflow_signed, /* complain_on_overflow. */
1532 bfd_elf_generic_reloc, /* special_function. */
1533 "R_V850_22_PLT", /* name. */
1534 FALSE, /* partial_inplace. */
1535 0x07ffff80, /* src_mask. */
1536 0x07ffff80, /* dst_mask. */
1537 TRUE), /* pcrel_offset. */
1538
1539 HOWTO (R_V850_32_PLT, /* type. */
1540 1, /* rightshift. */
1541 2, /* size (0 = byte, 1 = short, 2 = long). */
1542 32, /* bitsize. */
1543 TRUE, /* pc_relative. */
1544 1, /* bitpos. */
1545 complain_overflow_signed, /* complain_on_overflow. */
1546 bfd_elf_generic_reloc, /* special_function. */
1547 "R_V850_32_PLT", /* name. */
1548 FALSE, /* partial_inplace. */
1549 0xffffffff, /* src_mask. */
1550 0xffffffff, /* dst_mask. */
1551 TRUE), /* pcrel_offset. */
1552
1553 /* This is used only by the dynamic linker. The symbol should exist
1554 both in the object being run and in some shared library. The
1555 dynamic linker copies the data addressed by the symbol from the
1556 shared library into the object, because the object being
1557 run has to have the data at some particular address. */
1558 HOWTO (R_V850_COPY, /* type. */
1559 0, /* rightshift. */
1560 2, /* size (0 = byte, 1 = short, 2 = long). */
1561 32, /* bitsize. */
1562 FALSE, /* pc_relative. */
1563 0, /* bitpos. */
1564 complain_overflow_bitfield, /* complain_on_overflow. */
1565 bfd_elf_generic_reloc, /* special_function. */
1566 "R_V850_COPY", /* name. */
1567 FALSE, /* partial_inplace. */
1568 0xffffffff, /* src_mask. */
1569 0xffffffff, /* dst_mask. */
1570 FALSE), /* pcrel_offset. */
1571
1572 /* Like R_M32R_24, but used when setting global offset table
1573 entries. */
1574 HOWTO (R_V850_GLOB_DAT, /* type. */
1575 0, /* rightshift. */
1576 2, /* size (0 = byte, 1 = short, 2 = long) */
1577 32, /* bitsize. */
1578 FALSE, /* pc_relative. */
1579 0, /* bitpos. */
1580 complain_overflow_bitfield, /* complain_on_overflow. */
1581 bfd_elf_generic_reloc, /* special_function. */
1582 "R_V850_GLOB_DAT", /* name. */
1583 FALSE, /* partial_inplace. */
1584 0xffffffff, /* src_mask. */
1585 0xffffffff, /* dst_mask. */
1586 FALSE), /* pcrel_offset. */
1587
1588 /* Marks a procedure linkage table entry for a symbol. */
1589 HOWTO (R_V850_JMP_SLOT, /* type. */
1590 0, /* rightshift. */
1591 2, /* size (0 = byte, 1 = short, 2 = long) */
1592 32, /* bitsize. */
1593 FALSE, /* pc_relative. */
1594 0, /* bitpos. */
1595 complain_overflow_bitfield, /* complain_on_overflow. */
1596 bfd_elf_generic_reloc, /* special_function. */
1597 "R_V850_JMP_SLOT", /* name. */
1598 FALSE, /* partial_inplace. */
1599 0xffffffff, /* src_mask. */
1600 0xffffffff, /* dst_mask. */
1601 FALSE), /* pcrel_offset. */
1602
1603 /* Used only by the dynamic linker. When the object is run, this
1604 longword is set to the load address of the object, plus the
1605 addend. */
1606 HOWTO (R_V850_RELATIVE, /* type. */
1607 0, /* rightshift. */
1608 2, /* size (0 = byte, 1 = short, 2 = long) */
1609 32, /* bitsize. */
1610 FALSE, /* pc_relative. */
1611 0, /* bitpos. */
1612 complain_overflow_bitfield, /* complain_on_overflow. */
1613 bfd_elf_generic_reloc, /* special_function. */
1614 "R_V850_RELATIVE", /* name. */
1615 FALSE, /* partial_inplace. */
1616 0xffffffff, /* src_mask. */
1617 0xffffffff, /* dst_mask. */
1618 FALSE), /* pcrel_offset. */
1619
1620 HOWTO (R_V850_16_GOTOFF, /* type. */
1621 0, /* rightshift. */
1622 2, /* size (0 = byte, 1 = short, 2 = long) */
1623 16, /* bitsize. */
1624 FALSE, /* pc_relative. */
1625 0, /* bitpos. */
1626 complain_overflow_bitfield, /* complain_on_overflow. */
1627 bfd_elf_generic_reloc, /* special_function. */
1628 "R_V850_16_GOTOFF", /* name. */
1629 FALSE, /* partial_inplace. */
1630 0xffff, /* src_mask. */
1631 0xffff, /* dst_mask. */
1632 FALSE), /* pcrel_offset. */
1633
1634 HOWTO (R_V850_32_GOTOFF, /* type. */
1635 0, /* rightshift. */
1636 2, /* size (0 = byte, 1 = short, 2 = long) */
1637 32, /* bitsize. */
1638 FALSE, /* pc_relative. */
1639 0, /* bitpos. */
1640 complain_overflow_bitfield, /* complain_on_overflow. */
1641 bfd_elf_generic_reloc, /* special_function. */
1642 "R_V850_32_GOTOFF", /* name. */
1643 FALSE, /* partial_inplace. */
1644 0xffffffff, /* src_mask. */
1645 0xffffffff, /* dst_mask. */
1646 FALSE), /* pcrel_offset. */
1647
1648 HOWTO (R_V850_CODE, /* type. */
1649 0, /* rightshift. */
1650 1, /* size (0 = byte, 1 = short, 2 = long) */
1651 0, /* bitsize. */
1652 FALSE, /* pc_relative. */
1653 0, /* bitpos. */
1654 complain_overflow_unsigned, /* complain_on_overflow. */
1655 v850_elf_ignore_reloc, /* special_function. */
1656 "R_V850_CODE", /* name. */
1657 FALSE, /* partial_inplace. */
1658 0, /* src_mask. */
1659 0, /* dst_mask. */
1660 TRUE), /* pcrel_offset. */
1661
1662 HOWTO (R_V850_DATA, /* type. */
1663 0, /* rightshift. */
1664 1, /* size (0 = byte, 1 = short, 2 = long) */
1665 0, /* bitsize. */
1666 FALSE, /* pc_relative. */
1667 0, /* bitpos. */
1668 complain_overflow_unsigned, /* complain_on_overflow. */
1669 v850_elf_ignore_reloc, /* special_function. */
1670 "R_V850_DATA", /* name. */
1671 FALSE, /* partial_inplace. */
1672 0, /* src_mask. */
1673 0, /* dst_mask. */
1674 TRUE), /* pcrel_offset. */
1675
1676 };
1677
1678 /* Map BFD reloc types to V850 ELF reloc types. */
1679
1680 struct v850_elf_reloc_map
1681 {
1682 /* BFD_RELOC_V850_CALLT_16_16_OFFSET is 258, which will not fix in an
1683 unsigned char. */
1684 bfd_reloc_code_real_type bfd_reloc_val;
1685 unsigned int elf_reloc_val;
1686 };
1687
1688 static const struct v850_elf_reloc_map v850_elf_reloc_map[] =
1689 {
1690 { BFD_RELOC_NONE, R_V850_NONE },
1691 { BFD_RELOC_V850_9_PCREL, R_V850_9_PCREL },
1692 { BFD_RELOC_V850_22_PCREL, R_V850_22_PCREL },
1693 { BFD_RELOC_HI16_S, R_V850_HI16_S },
1694 { BFD_RELOC_HI16, R_V850_HI16 },
1695 { BFD_RELOC_LO16, R_V850_LO16 },
1696 { BFD_RELOC_32, R_V850_ABS32 },
1697 { BFD_RELOC_32_PCREL, R_V850_REL32 },
1698 { BFD_RELOC_16, R_V850_16 },
1699 { BFD_RELOC_8, R_V850_8 },
1700 { BFD_RELOC_V850_SDA_16_16_OFFSET, R_V850_SDA_16_16_OFFSET },
1701 { BFD_RELOC_V850_SDA_15_16_OFFSET, R_V850_SDA_15_16_OFFSET },
1702 { BFD_RELOC_V850_ZDA_16_16_OFFSET, R_V850_ZDA_16_16_OFFSET },
1703 { BFD_RELOC_V850_ZDA_15_16_OFFSET, R_V850_ZDA_15_16_OFFSET },
1704 { BFD_RELOC_V850_TDA_6_8_OFFSET, R_V850_TDA_6_8_OFFSET },
1705 { BFD_RELOC_V850_TDA_7_8_OFFSET, R_V850_TDA_7_8_OFFSET },
1706 { BFD_RELOC_V850_TDA_7_7_OFFSET, R_V850_TDA_7_7_OFFSET },
1707 { BFD_RELOC_V850_TDA_16_16_OFFSET, R_V850_TDA_16_16_OFFSET },
1708 { BFD_RELOC_V850_TDA_4_5_OFFSET, R_V850_TDA_4_5_OFFSET },
1709 { BFD_RELOC_V850_TDA_4_4_OFFSET, R_V850_TDA_4_4_OFFSET },
1710 { BFD_RELOC_V850_LO16_SPLIT_OFFSET, R_V850_LO16_SPLIT_OFFSET },
1711 { BFD_RELOC_V850_SDA_16_16_SPLIT_OFFSET, R_V850_SDA_16_16_SPLIT_OFFSET },
1712 { BFD_RELOC_V850_ZDA_16_16_SPLIT_OFFSET, R_V850_ZDA_16_16_SPLIT_OFFSET },
1713 { BFD_RELOC_V850_CALLT_6_7_OFFSET, R_V850_CALLT_6_7_OFFSET },
1714 { BFD_RELOC_V850_CALLT_16_16_OFFSET, R_V850_CALLT_16_16_OFFSET },
1715 { BFD_RELOC_VTABLE_INHERIT, R_V850_GNU_VTINHERIT },
1716 { BFD_RELOC_VTABLE_ENTRY, R_V850_GNU_VTENTRY },
1717 { BFD_RELOC_V850_LONGCALL, R_V850_LONGCALL },
1718 { BFD_RELOC_V850_LONGJUMP, R_V850_LONGJUMP },
1719 { BFD_RELOC_V850_ALIGN, R_V850_ALIGN },
1720 { BFD_RELOC_V850_16_PCREL, R_V850_16_PCREL },
1721 { BFD_RELOC_V850_17_PCREL, R_V850_17_PCREL },
1722 { BFD_RELOC_V850_23, R_V850_23 },
1723 { BFD_RELOC_V850_32_PCREL, R_V850_32_PCREL },
1724 { BFD_RELOC_V850_32_ABS, R_V850_32_ABS },
1725 { BFD_RELOC_V850_16_SPLIT_OFFSET, R_V850_HI16 },
1726 { BFD_RELOC_V850_16_S1, R_V850_16_S1 },
1727 { BFD_RELOC_V850_LO16_S1, R_V850_LO16_S1 },
1728 { BFD_RELOC_V850_CALLT_15_16_OFFSET, R_V850_CALLT_15_16_OFFSET },
1729 { BFD_RELOC_V850_32_GOTPCREL, R_V850_32_GOTPCREL },
1730 { BFD_RELOC_V850_16_GOT, R_V850_16_GOT },
1731 { BFD_RELOC_V850_32_GOT, R_V850_32_GOT },
1732 { BFD_RELOC_V850_22_PLT_PCREL, R_V850_22_PLT },
1733 { BFD_RELOC_V850_32_PLT_PCREL, R_V850_32_PLT },
1734 { BFD_RELOC_V850_COPY, R_V850_COPY },
1735 { BFD_RELOC_V850_GLOB_DAT, R_V850_GLOB_DAT },
1736 { BFD_RELOC_V850_JMP_SLOT, R_V850_JMP_SLOT },
1737 { BFD_RELOC_V850_RELATIVE, R_V850_RELATIVE },
1738 { BFD_RELOC_V850_16_GOTOFF, R_V850_16_GOTOFF },
1739 { BFD_RELOC_V850_32_GOTOFF, R_V850_32_GOTOFF },
1740 { BFD_RELOC_V850_CODE, R_V850_CODE },
1741 { BFD_RELOC_V850_DATA, R_V850_DATA },
1742 };
1743
1744 #define V800_RELOC(name,sz,bit,shift,complain,pcrel,resolver) \
1745 HOWTO (name, shift, sz, bit, pcrel, 0, complain_overflow_ ## complain, \
1746 bfd_elf_ ## resolver ## _reloc, #name, FALSE, 0, ~0, FALSE)
1747
1748 #define V800_EMPTY(name) EMPTY_HOWTO (name - R_V810_NONE)
1749
1750 #define bfd_elf_v850_reloc v850_elf_reloc
1751
1752 /* Note: It is REQUIRED that the 'type' value (R_V810_...) of each entry
1753 in this array match the index of the entry in the array minus 0x30.
1754 See: bfd_elf_v850_relocate_section(), v800_elf_reloc_type_lookup()
1755 and v800_elf_info_to_howto(). */
1756
1757 static reloc_howto_type v800_elf_howto_table[] =
1758 {
1759 V800_RELOC (R_V810_NONE, 0, 0, 0, dont, FALSE, generic), /* Type = 0x30 */
1760 V800_RELOC (R_V810_BYTE, 0, 8, 0, dont, FALSE, generic),
1761 V800_RELOC (R_V810_HWORD, 1, 16, 0, dont, FALSE, generic),
1762 V800_RELOC (R_V810_WORD, 2, 32, 0, dont, FALSE, generic),
1763 V800_RELOC (R_V810_WLO, 1, 16, 0, dont, FALSE, generic),
1764 V800_RELOC (R_V810_WHI, 1, 16, 0, dont, FALSE, generic),
1765 V800_RELOC (R_V810_WHI1, 1, 16, 0, dont, FALSE, generic),
1766 V800_RELOC (R_V810_GPBYTE, 0, 8, 0, dont, FALSE, v850),
1767 V800_RELOC (R_V810_GPHWORD, 1, 16, 0, dont, FALSE, v850),
1768 V800_RELOC (R_V810_GPWORD, 2, 32, 0, dont, FALSE, v850),
1769 V800_RELOC (R_V810_GPWLO, 1, 16, 0, dont, FALSE, v850),
1770 V800_RELOC (R_V810_GPWHI, 1, 16, 0, dont, FALSE, v850),
1771 V800_RELOC (R_V810_GPWHI1, 1, 16, 0, dont, FALSE, v850),
1772 V800_RELOC (R_V850_HWLO, 1, 16, 0, dont, FALSE, generic),
1773 V800_EMPTY (R_V810_reserved1),
1774 V800_RELOC (R_V850_EP7BIT, 0, 7, 0, unsigned, FALSE, v850),
1775 V800_RELOC (R_V850_EPHBYTE, 0, 8, 1, unsigned, FALSE, v850),
1776 V800_RELOC (R_V850_EPWBYTE, 0, 8, 2, unsigned, FALSE, v850),
1777 V800_RELOC (R_V850_REGHWLO, 1, 16, 0, dont, FALSE, v850),
1778 V800_EMPTY (R_V810_reserved2),
1779 V800_RELOC (R_V850_GPHWLO, 1, 16, 0, dont, FALSE, v850),
1780 V800_EMPTY (R_V810_reserved3),
1781 V800_RELOC (R_V850_PCR22, 2, 22, 0, signed, TRUE, generic),
1782 V800_RELOC (R_V850_BLO, 2, 24, 0, dont, FALSE, v850),
1783 V800_RELOC (R_V850_EP4BIT, 0, 4, 0, unsigned, FALSE, v850),
1784 V800_RELOC (R_V850_EP5BIT, 0, 5, 0, unsigned, FALSE, v850),
1785 V800_RELOC (R_V850_REGBLO, 2, 24, 0, dont, FALSE, v850),
1786 V800_RELOC (R_V850_GPBLO, 2, 24, 0, dont, FALSE, v850),
1787 V800_RELOC (R_V810_WLO_1, 1, 16, 0, dont, FALSE, v850),
1788 V800_RELOC (R_V810_GPWLO_1, 1, 16, 0, signed, FALSE, v850),
1789 V800_RELOC (R_V850_BLO_1, 2, 16, 0, signed, FALSE, v850),
1790 V800_RELOC (R_V850_HWLO_1, 1, 16, 0, signed, FALSE, v850),
1791 V800_EMPTY (R_V810_reserved4),
1792 V800_RELOC (R_V850_GPBLO_1, 2, 16, 1, signed, FALSE, v850),
1793 V800_RELOC (R_V850_GPHWLO_1, 1, 16, 1, signed, FALSE, v850),
1794 V800_EMPTY (R_V810_reserved5),
1795 V800_RELOC (R_V850_EPBLO, 2, 16, 1, signed, FALSE, v850),
1796 V800_RELOC (R_V850_EPHWLO, 1, 16, 1, signed, FALSE, v850),
1797 V800_EMPTY (R_V810_reserved6),
1798 V800_RELOC (R_V850_EPWLO_N, 1, 16, 1, signed, FALSE, v850),
1799 V800_RELOC (R_V850_PC32, 2, 32, 1, signed, TRUE, v850),
1800 V800_RELOC (R_V850_W23BIT, 2, 23, 1, signed, FALSE, v850),
1801 V800_RELOC (R_V850_GPW23BIT, 2, 23, 1, signed, FALSE, v850),
1802 V800_RELOC (R_V850_EPW23BIT, 2, 23, 1, signed, FALSE, v850),
1803 V800_RELOC (R_V850_B23BIT, 2, 23, 1, signed, FALSE, v850),
1804 V800_RELOC (R_V850_GPB23BIT, 2, 23, 1, signed, FALSE, v850),
1805 V800_RELOC (R_V850_EPB23BIT, 2, 23, 1, signed, FALSE, v850),
1806 V800_RELOC (R_V850_PC16U, 1, 16, 1, unsigned, TRUE, generic),
1807 V800_RELOC (R_V850_PC17, 2, 17, 1, signed, TRUE, generic),
1808 V800_RELOC (R_V850_DW8, 2, 8, 2, signed, FALSE, v850),
1809 V800_RELOC (R_V850_GPDW8, 2, 8, 2, signed, FALSE, v850),
1810 V800_RELOC (R_V850_EPDW8, 2, 8, 2, signed, FALSE, v850),
1811 V800_RELOC (R_V850_PC9, 1, 9, 3, signed, TRUE, v850),
1812 V800_RELOC (R_V810_REGBYTE, 0, 8, 0, dont, FALSE, v850),
1813 V800_RELOC (R_V810_REGHWORD, 1, 16, 0, dont, FALSE, v850),
1814 V800_RELOC (R_V810_REGWORD, 2, 32, 0, dont, FALSE, v850),
1815 V800_RELOC (R_V810_REGWLO, 1, 16, 0, dont, FALSE, v850),
1816 V800_RELOC (R_V810_REGWHI, 1, 16, 0, dont, FALSE, v850),
1817 V800_RELOC (R_V810_REGWHI1, 1, 16, 0, dont, FALSE, v850),
1818 V800_RELOC (R_V850_REGW23BIT, 2, 23, 1, signed, FALSE, v850),
1819 V800_RELOC (R_V850_REGB23BIT, 2, 23, 1, signed, FALSE, v850),
1820 V800_RELOC (R_V850_REGDW8, 2, 8, 2, signed, FALSE, v850),
1821 V800_RELOC (R_V810_EPBYTE, 0, 8, 0, dont, FALSE, v850),
1822 V800_RELOC (R_V810_EPHWORD, 1, 16, 0, dont, FALSE, v850),
1823 V800_RELOC (R_V810_EPWORD, 2, 32, 0, dont, FALSE, v850),
1824 V800_RELOC (R_V850_WLO23, 2, 32, 1, dont, FALSE, v850),
1825 V800_RELOC (R_V850_WORD_E, 2, 32, 1, dont, FALSE, v850),
1826 V800_RELOC (R_V850_REGWORD_E, 2, 32, 1, dont, FALSE, v850),
1827 V800_RELOC (R_V850_WORD, 2, 32, 0, dont, FALSE, v850),
1828 V800_RELOC (R_V850_GPWORD, 2, 32, 0, dont, FALSE, v850),
1829 V800_RELOC (R_V850_REGWORD, 2, 32, 0, dont, FALSE, v850),
1830 V800_RELOC (R_V850_EPWORD, 2, 32, 0, dont, FALSE, v850),
1831 V800_RELOC (R_V810_TPBYTE, 0, 8, 0, dont, FALSE, v850),
1832 V800_RELOC (R_V810_TPHWORD, 1, 16, 0, dont, FALSE, v850),
1833 V800_RELOC (R_V810_TPWORD, 2, 32, 0, dont, FALSE, v850),
1834 V800_RELOC (R_V810_TPWLO, 1, 16, 0, dont, FALSE, v850),
1835 V800_RELOC (R_V810_TPWHI, 1, 16, 0, dont, FALSE, v850),
1836 V800_RELOC (R_V810_TPWHI1, 1, 16, 0, dont, FALSE, v850),
1837 V800_RELOC (R_V850_TPHWLO, 1, 16, 1, dont, FALSE, v850),
1838 V800_RELOC (R_V850_TPBLO, 2, 24, 0, dont, FALSE, v850),
1839 V800_RELOC (R_V810_TPWLO_1, 1, 16, 0, signed, FALSE, v850),
1840 V800_RELOC (R_V850_TPBLO_1, 2, 16, 0, signed, FALSE, v850),
1841 V800_RELOC (R_V850_TPHWLO_1, 1, 16, 0, signed, FALSE, v850),
1842 V800_RELOC (R_V850_TP23BIT, 2, 23, 0, signed, FALSE, v850),
1843 V800_RELOC (R_V850_TPW23BIT, 2, 23, 0, signed, FALSE, v850),
1844 V800_RELOC (R_V850_TPDW8, 2, 8, 0, signed, FALSE, v850)
1845 };
1846 \f
1847 /* Map a bfd relocation into the appropriate howto structure. */
1848
1849 static reloc_howto_type *
1850 v850_elf_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
1851 bfd_reloc_code_real_type code)
1852 {
1853 unsigned int i;
1854
1855 for (i = ARRAY_SIZE (v850_elf_reloc_map); i --;)
1856 if (v850_elf_reloc_map[i].bfd_reloc_val == code)
1857 {
1858 unsigned int elf_reloc_val = v850_elf_reloc_map[i].elf_reloc_val;
1859
1860 BFD_ASSERT (v850_elf_howto_table[elf_reloc_val].type == elf_reloc_val);
1861
1862 return v850_elf_howto_table + elf_reloc_val;
1863 }
1864
1865 return NULL;
1866 }
1867
1868 static reloc_howto_type *
1869 v850_elf_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
1870 const char *r_name)
1871 {
1872 unsigned int i;
1873
1874 for (i = 0;
1875 i < sizeof (v850_elf_howto_table) / sizeof (v850_elf_howto_table[0]);
1876 i++)
1877 if (v850_elf_howto_table[i].name != NULL
1878 && strcasecmp (v850_elf_howto_table[i].name, r_name) == 0)
1879 return &v850_elf_howto_table[i];
1880
1881 return NULL;
1882 }
1883 \f
1884 /* Set the howto pointer for an V850 ELF reloc. */
1885
1886 static bfd_boolean
1887 v850_elf_info_to_howto_rel (bfd *abfd,
1888 arelent *cache_ptr,
1889 Elf_Internal_Rela *dst)
1890 {
1891 unsigned int r_type;
1892
1893 r_type = ELF32_R_TYPE (dst->r_info);
1894 if (r_type >= (unsigned int) R_V850_max)
1895 {
1896 /* xgettext:c-format */
1897 _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
1898 abfd, r_type);
1899 bfd_set_error (bfd_error_bad_value);
1900 return FALSE;
1901 }
1902 cache_ptr->howto = &v850_elf_howto_table[r_type];
1903 return TRUE;
1904 }
1905
1906 /* Set the howto pointer for a V850 ELF reloc (type RELA). */
1907
1908 static bfd_boolean
1909 v850_elf_info_to_howto_rela (bfd *abfd,
1910 arelent * cache_ptr,
1911 Elf_Internal_Rela *dst)
1912 {
1913 unsigned int r_type;
1914
1915 r_type = ELF32_R_TYPE (dst->r_info);
1916 if (r_type >= (unsigned int) R_V850_max)
1917 {
1918 /* xgettext:c-format */
1919 _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
1920 abfd, r_type);
1921 bfd_set_error (bfd_error_bad_value);
1922 return FALSE;
1923 }
1924 cache_ptr->howto = &v850_elf_howto_table[r_type];
1925 return TRUE;
1926 }
1927 \f
1928 static bfd_boolean
1929 v850_elf_is_local_label_name (bfd *abfd ATTRIBUTE_UNUSED, const char *name)
1930 {
1931 return ( (name[0] == '.' && (name[1] == 'L' || name[1] == '.'))
1932 || (name[0] == '_' && name[1] == '.' && name[2] == 'L' && name[3] == '_'));
1933 }
1934
1935 static bfd_boolean
1936 v850_elf_is_target_special_symbol (bfd *abfd, asymbol *sym)
1937 {
1938 return v850_elf_is_local_label_name (abfd, sym->name);
1939 }
1940 \f
1941 /* We overload some of the bfd_reloc error codes for own purposes. */
1942 #define bfd_reloc_gp_not_found bfd_reloc_other
1943 #define bfd_reloc_ep_not_found bfd_reloc_continue
1944 #define bfd_reloc_ctbp_not_found (bfd_reloc_dangerous + 1)
1945
1946 /* Perform a relocation as part of a final link. */
1947
1948 static bfd_reloc_status_type
1949 v850_elf_final_link_relocate (reloc_howto_type *howto,
1950 bfd *input_bfd,
1951 bfd *output_bfd ATTRIBUTE_UNUSED,
1952 asection *input_section,
1953 bfd_byte *contents,
1954 bfd_vma offset,
1955 bfd_vma value,
1956 bfd_vma addend,
1957 struct bfd_link_info *info,
1958 asection *sym_sec,
1959 int is_local ATTRIBUTE_UNUSED)
1960 {
1961 unsigned int r_type = howto->type;
1962 bfd_byte *hit_data = contents + offset;
1963
1964 /* Adjust the value according to the relocation. */
1965 switch (r_type)
1966 {
1967 case R_V850_PC9:
1968 case R_V850_9_PCREL:
1969 value -= (input_section->output_section->vma
1970 + input_section->output_offset);
1971 value -= offset;
1972 break;
1973
1974 case R_V850_PC16U:
1975 case R_V850_16_PCREL:
1976 value -= (input_section->output_section->vma
1977 + input_section->output_offset
1978 + offset);
1979
1980 /* If the sign extension will corrupt the value then we have overflowed. */
1981 if ((value & 0xffff0000) != 0xffff0000)
1982 return bfd_reloc_overflow;
1983
1984 break;
1985
1986 case R_V850_PC17:
1987 case R_V850_17_PCREL:
1988 value -= (input_section->output_section->vma
1989 + input_section->output_offset
1990 + offset);
1991
1992 /* If the sign extension will corrupt the value then we have overflowed. */
1993 if (((value & 0xffff0000) != 0x0) && ((value & 0xffff0000) != 0xffff0000))
1994 return bfd_reloc_overflow;
1995
1996 value = SEXT17 (value);
1997 break;
1998
1999 case R_V850_PCR22:
2000 case R_V850_22_PCREL:
2001 value -= (input_section->output_section->vma
2002 + input_section->output_offset
2003 + offset);
2004
2005 /* If the sign extension will corrupt the value then we have overflowed. */
2006 if (((value & 0xffe00000) != 0x0) && ((value & 0xffe00000) != 0xffe00000))
2007 return bfd_reloc_overflow;
2008
2009 /* Only the bottom 22 bits of the PC are valid. */
2010 value = SEXT22 (value);
2011 break;
2012
2013 case R_V850_PC32:
2014 case R_V850_32_PCREL:
2015 value -= (input_section->output_section->vma
2016 + input_section->output_offset
2017 + offset);
2018 break;
2019
2020 case R_V850_32_ABS:
2021 case R_V850_23:
2022 case R_V850_HI16_S:
2023 case R_V850_HI16:
2024 case R_V850_LO16:
2025 case R_V850_LO16_S1:
2026 case R_V850_LO16_SPLIT_OFFSET:
2027 case R_V850_16:
2028 case R_V850_ABS32:
2029 case R_V850_8:
2030 case R_V810_BYTE:
2031 case R_V810_HWORD:
2032 case R_V810_WORD:
2033 case R_V810_WLO:
2034 case R_V810_WHI:
2035 case R_V810_WHI1:
2036 case R_V810_WLO_1:
2037 case R_V850_WLO23:
2038 case R_V850_BLO:
2039 break;
2040
2041 case R_V850_ZDA_15_16_OFFSET:
2042 case R_V850_ZDA_16_16_OFFSET:
2043 case R_V850_ZDA_16_16_SPLIT_OFFSET:
2044 if (sym_sec == NULL)
2045 return bfd_reloc_undefined;
2046
2047 value -= sym_sec->output_section->vma;
2048 break;
2049
2050 case R_V850_SDA_15_16_OFFSET:
2051 case R_V850_SDA_16_16_OFFSET:
2052 case R_V850_SDA_16_16_SPLIT_OFFSET:
2053 case R_V810_GPWLO_1:
2054 {
2055 unsigned long gp;
2056 struct bfd_link_hash_entry * h;
2057
2058 if (sym_sec == NULL)
2059 return bfd_reloc_undefined;
2060
2061 /* Get the value of __gp. */
2062 h = bfd_link_hash_lookup (info->hash, "__gp", FALSE, FALSE, TRUE);
2063 if (h == NULL
2064 || h->type != bfd_link_hash_defined)
2065 return bfd_reloc_gp_not_found;
2066
2067 gp = (h->u.def.value
2068 + h->u.def.section->output_section->vma
2069 + h->u.def.section->output_offset);
2070
2071 value -= sym_sec->output_section->vma;
2072 value -= (gp - sym_sec->output_section->vma);
2073 }
2074 break;
2075
2076 case R_V850_TDA_4_4_OFFSET:
2077 case R_V850_TDA_4_5_OFFSET:
2078 case R_V850_TDA_7_7_OFFSET:
2079 case R_V850_TDA_7_8_OFFSET:
2080 case R_V850_TDA_6_8_OFFSET:
2081 case R_V850_TDA_16_16_OFFSET:
2082 {
2083 unsigned long ep;
2084 struct bfd_link_hash_entry * h;
2085
2086 /* Get the value of __ep. */
2087 h = bfd_link_hash_lookup (info->hash, "__ep", FALSE, FALSE, TRUE);
2088 if (h == NULL
2089 || h->type != bfd_link_hash_defined)
2090 return bfd_reloc_ep_not_found;
2091
2092 ep = (h->u.def.value
2093 + h->u.def.section->output_section->vma
2094 + h->u.def.section->output_offset);
2095
2096 value -= ep;
2097 }
2098 break;
2099
2100 case R_V850_CALLT_6_7_OFFSET:
2101 {
2102 unsigned long ctbp;
2103 struct bfd_link_hash_entry * h;
2104
2105 /* Get the value of __ctbp. */
2106 h = bfd_link_hash_lookup (info->hash, "__ctbp", FALSE, FALSE, TRUE);
2107 if (h == NULL
2108 || h->type != bfd_link_hash_defined)
2109 return bfd_reloc_ctbp_not_found;
2110
2111 ctbp = (h->u.def.value
2112 + h->u.def.section->output_section->vma
2113 + h->u.def.section->output_offset);
2114 value -= ctbp;
2115 }
2116 break;
2117
2118 case R_V850_CALLT_15_16_OFFSET:
2119 case R_V850_CALLT_16_16_OFFSET:
2120 {
2121 unsigned long ctbp;
2122 struct bfd_link_hash_entry * h;
2123
2124 if (sym_sec == NULL)
2125 return bfd_reloc_undefined;
2126
2127 /* Get the value of __ctbp. */
2128 h = bfd_link_hash_lookup (info->hash, "__ctbp", FALSE, FALSE, TRUE);
2129 if (h == NULL
2130 || h->type != bfd_link_hash_defined)
2131 return bfd_reloc_ctbp_not_found;
2132
2133 ctbp = (h->u.def.value
2134 + h->u.def.section->output_section->vma
2135 + h->u.def.section->output_offset);
2136
2137 value -= sym_sec->output_section->vma;
2138 value -= (ctbp - sym_sec->output_section->vma);
2139 }
2140 break;
2141
2142 case R_V850_NONE:
2143 case R_V810_NONE:
2144 case R_V850_GNU_VTINHERIT:
2145 case R_V850_GNU_VTENTRY:
2146 case R_V850_LONGCALL:
2147 case R_V850_LONGJUMP:
2148 case R_V850_ALIGN:
2149 return bfd_reloc_ok;
2150
2151 default:
2152 #ifdef DEBUG
2153 _bfd_error_handler ("%pB: unsupported relocation type %#x",
2154 input_bfd, r_type);
2155 #endif
2156 return bfd_reloc_notsupported;
2157 }
2158
2159 /* Perform the relocation. */
2160 return v850_elf_perform_relocation (input_bfd, r_type, value + addend, hit_data);
2161 }
2162 \f
2163 /* Relocate an V850 ELF section. */
2164
2165 static bfd_boolean
2166 v850_elf_relocate_section (bfd *output_bfd,
2167 struct bfd_link_info *info,
2168 bfd *input_bfd,
2169 asection *input_section,
2170 bfd_byte *contents,
2171 Elf_Internal_Rela *relocs,
2172 Elf_Internal_Sym *local_syms,
2173 asection **local_sections)
2174 {
2175 Elf_Internal_Shdr *symtab_hdr;
2176 struct elf_link_hash_entry **sym_hashes;
2177 Elf_Internal_Rela *rel;
2178 Elf_Internal_Rela *relend;
2179
2180 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
2181 sym_hashes = elf_sym_hashes (input_bfd);
2182
2183 /* Reset the list of remembered HI16S relocs to empty. */
2184 free_hi16s = previous_hi16s;
2185 previous_hi16s = NULL;
2186 hi16s_counter = 0;
2187
2188 rel = relocs;
2189 relend = relocs + input_section->reloc_count;
2190 for (; rel < relend; rel++)
2191 {
2192 unsigned int r_type;
2193 reloc_howto_type *howto;
2194 unsigned long r_symndx;
2195 Elf_Internal_Sym *sym;
2196 asection *sec;
2197 struct elf_link_hash_entry *h;
2198 bfd_vma relocation;
2199 bfd_reloc_status_type r;
2200
2201 r_symndx = ELF32_R_SYM (rel->r_info);
2202 r_type = ELF32_R_TYPE (rel->r_info);
2203
2204 if (r_type == R_V850_GNU_VTENTRY
2205 || r_type == R_V850_GNU_VTINHERIT)
2206 continue;
2207
2208 if (bfd_get_arch (input_bfd) == bfd_arch_v850_rh850)
2209 howto = v800_elf_howto_table + (r_type - R_V810_NONE);
2210 else
2211 howto = v850_elf_howto_table + r_type;
2212
2213 BFD_ASSERT (r_type == howto->type);
2214
2215 h = NULL;
2216 sym = NULL;
2217 sec = NULL;
2218 if (r_symndx < symtab_hdr->sh_info)
2219 {
2220 sym = local_syms + r_symndx;
2221 sec = local_sections[r_symndx];
2222 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
2223 }
2224 else
2225 {
2226 bfd_boolean unresolved_reloc, warned, ignored;
2227
2228 /* Note - this check is delayed until now as it is possible and
2229 valid to have a file without any symbols but with relocs that
2230 can be processed. */
2231 if (sym_hashes == NULL)
2232 {
2233 info->callbacks->warning
2234 (info, "no hash table available",
2235 NULL, input_bfd, input_section, (bfd_vma) 0);
2236
2237 return FALSE;
2238 }
2239
2240 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
2241 r_symndx, symtab_hdr, sym_hashes,
2242 h, sec, relocation,
2243 unresolved_reloc, warned, ignored);
2244 }
2245
2246 if (sec != NULL && discarded_section (sec))
2247 RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
2248 rel, 1, relend, howto, 0, contents);
2249
2250 if (bfd_link_relocatable (info))
2251 continue;
2252
2253 /* FIXME: We should use the addend, but the COFF relocations don't. */
2254 r = v850_elf_final_link_relocate (howto, input_bfd, output_bfd,
2255 input_section,
2256 contents, rel->r_offset,
2257 relocation, rel->r_addend,
2258 info, sec, h == NULL);
2259
2260 if (r != bfd_reloc_ok)
2261 {
2262 const char * name;
2263 const char * msg = NULL;
2264
2265 if (h != NULL)
2266 name = h->root.root.string;
2267 else
2268 {
2269 name = (bfd_elf_string_from_elf_section
2270 (input_bfd, symtab_hdr->sh_link, sym->st_name));
2271 if (name == NULL || *name == '\0')
2272 name = bfd_section_name (sec);
2273 }
2274
2275 switch ((int) r)
2276 {
2277 case bfd_reloc_overflow:
2278 (*info->callbacks->reloc_overflow)
2279 (info, (h ? &h->root : NULL), name, howto->name,
2280 (bfd_vma) 0, input_bfd, input_section, rel->r_offset);
2281 break;
2282
2283 case bfd_reloc_undefined:
2284 (*info->callbacks->undefined_symbol)
2285 (info, name, input_bfd, input_section, rel->r_offset, TRUE);
2286 break;
2287
2288 case bfd_reloc_outofrange:
2289 msg = _("internal error: out of range error");
2290 goto common_error;
2291
2292 case bfd_reloc_notsupported:
2293 msg = _("internal error: unsupported relocation error");
2294 goto common_error;
2295
2296 case bfd_reloc_dangerous:
2297 msg = _("internal error: dangerous relocation");
2298 goto common_error;
2299
2300 case bfd_reloc_gp_not_found:
2301 msg = _("could not locate special linker symbol __gp");
2302 goto common_error;
2303
2304 case bfd_reloc_ep_not_found:
2305 msg = _("could not locate special linker symbol __ep");
2306 goto common_error;
2307
2308 case bfd_reloc_ctbp_not_found:
2309 msg = _("could not locate special linker symbol __ctbp");
2310 goto common_error;
2311
2312 default:
2313 msg = _("internal error: unknown error");
2314 /* fall through */
2315
2316 common_error:
2317 (*info->callbacks->warning) (info, msg, name, input_bfd,
2318 input_section, rel->r_offset);
2319 break;
2320 }
2321 }
2322 }
2323
2324 return TRUE;
2325 }
2326
2327 static asection *
2328 v850_elf_gc_mark_hook (asection *sec,
2329 struct bfd_link_info *info,
2330 Elf_Internal_Rela *rel,
2331 struct elf_link_hash_entry *h,
2332 Elf_Internal_Sym *sym)
2333 {
2334 if (h != NULL)
2335 switch (ELF32_R_TYPE (rel->r_info))
2336 {
2337 case R_V850_GNU_VTINHERIT:
2338 case R_V850_GNU_VTENTRY:
2339 return NULL;
2340 }
2341
2342 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
2343 }
2344
2345 static void
2346 v850_set_note (bfd * abfd, asection * s, enum v850_notes note, unsigned int val)
2347 {
2348 bfd_byte * data = s->contents + ((note - 1) * SIZEOF_V850_NOTE);
2349
2350 bfd_put_32 (abfd, 4, data + 0);
2351 bfd_put_32 (abfd, 4, data + 4);
2352 bfd_put_32 (abfd, note, data + 8);
2353 memcpy (data + 12, V850_NOTE_NAME, 4);
2354 bfd_put_32 (abfd, val, data + 16);
2355 }
2356
2357 /* Create the note section if not already present. This is done early so
2358 that the linker maps the sections to the right place in the output. */
2359
2360 static asection *
2361 v850_elf_make_note_section (bfd * abfd)
2362 {
2363 asection *s;
2364 bfd_byte *data;
2365 flagword flags;
2366 enum v850_notes id;
2367
2368 /* Make the note section. */
2369 flags = SEC_READONLY | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_MERGE;
2370
2371 s = bfd_make_section_anyway_with_flags (abfd, V850_NOTE_SECNAME, flags);
2372 if (s == NULL)
2373 return NULL;
2374
2375 if (!bfd_set_section_alignment (s, 2))
2376 return NULL;
2377
2378 /* Allocate space for all known notes. */
2379 if (!bfd_set_section_size (s, NUM_V850_NOTES * SIZEOF_V850_NOTE))
2380 return NULL;
2381
2382 data = bfd_zalloc (abfd, NUM_V850_NOTES * SIZEOF_V850_NOTE);
2383 if (data == NULL)
2384 return NULL;
2385
2386 s->contents = data;
2387
2388 /* Provide default (= uninitilaised) values for all of the notes. */
2389 for (id = V850_NOTE_ALIGNMENT; id <= NUM_V850_NOTES; id++)
2390 v850_set_note (abfd, s, id, 0);
2391
2392 return s;
2393 }
2394
2395 /* Create the note section if not already present. This is done early so
2396 that the linker maps the sections to the right place in the output. */
2397
2398 bfd_boolean
2399 v850_elf_create_sections (struct bfd_link_info * info)
2400 {
2401 bfd * ibfd;
2402
2403 /* If we already have a note section, do not make another. */
2404 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
2405 if (bfd_get_section_by_name (ibfd, V850_NOTE_SECNAME) != NULL)
2406 return TRUE;
2407
2408 return v850_elf_make_note_section (info->input_bfds) != NULL;
2409 }
2410
2411 bfd_boolean
2412 v850_elf_set_note (bfd * abfd, enum v850_notes note, unsigned int val)
2413 {
2414 asection * notes = bfd_get_section_by_name (abfd, V850_NOTE_SECNAME);
2415
2416 if (val > 2)
2417 /* At the moment, no known note has a value over 2. */
2418 return FALSE;
2419
2420 if (notes == NULL)
2421 notes = v850_elf_make_note_section (abfd);
2422 if (notes == NULL)
2423 return FALSE;
2424
2425 v850_set_note (abfd, notes, note, val);
2426 return TRUE;
2427 }
2428
2429 /* Copy a v850 note section from one object module to another. */
2430
2431 static void
2432 v850_elf_copy_notes (bfd *ibfd, bfd *obfd)
2433 {
2434 asection * onotes;
2435 asection * inotes;
2436
2437 /* If the output bfd does not have a note section, then
2438 skip the merge. The normal input to output section
2439 copying will take care of everythng for us. */
2440 if ((onotes = bfd_get_section_by_name (obfd, V850_NOTE_SECNAME)) == NULL)
2441 return;
2442
2443 if ((inotes = bfd_get_section_by_name (ibfd, V850_NOTE_SECNAME)) == NULL)
2444 return;
2445
2446 if (bfd_section_size (inotes) == bfd_section_size (onotes))
2447 {
2448 bfd_byte * icont;
2449 bfd_byte * ocont;
2450
2451 if ((icont = elf_section_data (inotes)->this_hdr.contents) == NULL)
2452 BFD_ASSERT (bfd_malloc_and_get_section (ibfd, inotes, & icont));
2453
2454 if ((ocont = elf_section_data (onotes)->this_hdr.contents) == NULL)
2455 /* If the output is being stripped then it is possible for
2456 the notes section to disappear. In this case do nothing. */
2457 return;
2458
2459 /* Copy/overwrite notes from the input to the output. */
2460 memcpy (ocont, icont, bfd_section_size (onotes));
2461 }
2462 }
2463
2464 /* Copy backend specific data from one object module to another. */
2465
2466 static bfd_boolean
2467 v850_elf_copy_private_bfd_data (bfd *ibfd, bfd *obfd)
2468 {
2469 v850_elf_copy_notes (ibfd, obfd);
2470 return _bfd_elf_copy_private_bfd_data (ibfd, obfd);
2471 }
2472 #define bfd_elf32_bfd_copy_private_bfd_data v850_elf_copy_private_bfd_data
2473
2474 static bfd_boolean
2475 v850_elf_merge_notes (bfd * ibfd, bfd *obfd)
2476 {
2477 asection * onotes;
2478 asection * inotes;
2479 bfd_boolean result = TRUE;
2480
2481 /* If the output bfd does not have a note section, then
2482 skip the merge. The normal input to output section
2483 copying will take care of everythng for us. */
2484 if ((onotes = bfd_get_section_by_name (obfd, V850_NOTE_SECNAME)) == NULL)
2485 return TRUE;
2486
2487 if ((inotes = bfd_get_section_by_name (ibfd, V850_NOTE_SECNAME)) != NULL)
2488 {
2489 enum v850_notes id;
2490 bfd_byte * icont;
2491 bfd_byte * ocont;
2492
2493 BFD_ASSERT (bfd_section_size (inotes) == bfd_section_size (onotes));
2494
2495 if ((icont = elf_section_data (inotes)->this_hdr.contents) == NULL)
2496 BFD_ASSERT (bfd_malloc_and_get_section (ibfd, inotes, & icont));
2497
2498 if ((ocont = elf_section_data (onotes)->this_hdr.contents) == NULL)
2499 BFD_ASSERT (bfd_malloc_and_get_section (obfd, onotes, & ocont));
2500
2501 for (id = V850_NOTE_ALIGNMENT; id <= NUM_V850_NOTES; id++)
2502 {
2503 unsigned int ival;
2504 unsigned int oval;
2505 bfd_byte * idata = icont + ((id - 1) * SIZEOF_V850_NOTE) + 16;
2506 bfd_byte * odata = ocont + ((id - 1) * SIZEOF_V850_NOTE) + 16;
2507
2508 ival = bfd_get_32 (ibfd, idata);
2509 oval = bfd_get_32 (obfd, odata);
2510
2511 if (ival == 0 || ival == oval)
2512 continue;
2513
2514 if (oval == 0)
2515 {
2516 bfd_put_32 (obfd, ival, odata);
2517 v850_set_note (obfd, onotes, id, ival);
2518 continue;
2519 }
2520
2521 /* We have a mismatch. The ABI defines how to handle
2522 this siutation on a per note type basis. */
2523 switch (id)
2524 {
2525 case V850_NOTE_ALIGNMENT:
2526 if (oval == EF_RH850_DATA_ALIGN4)
2527 {
2528 _bfd_error_handler
2529 /* xgettext:c-format */
2530 (_("error: %pB needs 8-byte alignment but %pB is set for 4-byte alignment"),
2531 ibfd, obfd);
2532 result = FALSE;
2533 }
2534 else
2535 /* ibfd uses 4-byte alignment, obfd uses 8-byte alignment.
2536 Leave the obfd alignment as it is. */
2537 BFD_ASSERT (oval == EF_RH850_DATA_ALIGN8);
2538
2539 break;
2540
2541 case V850_NOTE_DATA_SIZE:
2542 if (oval == EF_RH850_DOUBLE32)
2543 {
2544 _bfd_error_handler
2545 /* xgettext:c-format */
2546 (_("error: %pB uses 64-bit doubles but "
2547 "%pB uses 32-bit doubles"), ibfd, obfd);
2548 result = FALSE;
2549 }
2550 else
2551 /* ibfd uses 32-bit doubles, obfd uses 64-bit doubles.
2552 This is acceptable. Honest, that is what the ABI says. */
2553 BFD_ASSERT (oval == EF_RH850_DOUBLE64);
2554 break;
2555
2556 case V850_NOTE_FPU_INFO:
2557 if (oval == EF_RH850_FPU20)
2558 {
2559 _bfd_error_handler
2560 /* xgettext:c-format */
2561 (_("error: %pB uses FPU-3.0 but %pB only supports FPU-2.0"),
2562 ibfd, obfd);
2563 result = FALSE;
2564 }
2565 else
2566 /* ibfd uses FPU-2.0, obfd uses FPU-3.0. Leave obfd as it is. */
2567 BFD_ASSERT (oval == EF_RH850_FPU30);
2568
2569 break;
2570
2571 default:
2572 /* None of the other conflicts matter.
2573 Stick with the current output values. */
2574 break;
2575 }
2576 }
2577
2578 /* FIXME: We should also check for conflicts between the notes
2579 and the EF flags in the ELF header. */
2580 }
2581
2582 return result;
2583 }
2584
2585 static void
2586 print_v850_note (bfd * abfd, FILE * file, bfd_byte * data, enum v850_notes id)
2587 {
2588 unsigned int value = bfd_get_32 (abfd, data + ((id - 1) * SIZEOF_V850_NOTE) + 16);
2589
2590 switch (id)
2591 {
2592 case V850_NOTE_ALIGNMENT:
2593 fprintf (file, _(" alignment of 8-byte entities: "));
2594 switch (value)
2595 {
2596 case EF_RH850_DATA_ALIGN4: fprintf (file, _("4-byte")); break;
2597 case EF_RH850_DATA_ALIGN8: fprintf (file, _("8-byte")); break;
2598 case 0: fprintf (file, _("not set")); break;
2599 default: fprintf (file, _("unknown: %x"), value); break;
2600 }
2601 fputc ('\n', file);
2602 break;
2603
2604 case V850_NOTE_DATA_SIZE:
2605 fprintf (file, _(" size of doubles: "));
2606 switch (value)
2607 {
2608 case EF_RH850_DOUBLE32: fprintf (file, _("4-bytes")); break;
2609 case EF_RH850_DOUBLE64: fprintf (file, _("8-bytes")); break;
2610 case 0: fprintf (file, _("not set")); break;
2611 default: fprintf (file, _("unknown: %x"), value); break;
2612 }
2613 fputc ('\n', file);
2614 break;
2615
2616 case V850_NOTE_FPU_INFO:
2617 fprintf (file, _(" FPU support required: "));
2618 switch (value)
2619 {
2620 case EF_RH850_FPU20: fprintf (file, _("FPU-2.0")); break;
2621 case EF_RH850_FPU30: fprintf (file, _("FPU-3.0")); break;
2622 case 0: fprintf (file, _("none")); break;
2623 default: fprintf (file, _("unknown: %x"), value); break;
2624 }
2625 fputc ('\n', file);
2626 break;
2627
2628 case V850_NOTE_SIMD_INFO:
2629 fprintf (file, _("SIMD use: "));
2630 switch (value)
2631 {
2632 case EF_RH850_SIMD: fprintf (file, _("yes")); break;
2633 case 0: fprintf (file, _("no")); break;
2634 default: fprintf (file, _("unknown: %x"), value); break;
2635 }
2636 fputc ('\n', file);
2637 break;
2638
2639 case V850_NOTE_CACHE_INFO:
2640 fprintf (file, _("CACHE use: "));
2641 switch (value)
2642 {
2643 case EF_RH850_CACHE: fprintf (file, _("yes")); break;
2644 case 0: fprintf (file, _("no")); break;
2645 default: fprintf (file, _("unknown: %x"), value); break;
2646 }
2647 fputc ('\n', file);
2648 break;
2649
2650 case V850_NOTE_MMU_INFO:
2651 fprintf (file, _("MMU use: "));
2652 switch (value)
2653 {
2654 case EF_RH850_MMU: fprintf (file, _("yes")); break;
2655 case 0: fprintf (file, _("no")); break;
2656 default: fprintf (file, _("unknown: %x"), value); break;
2657 }
2658 fputc ('\n', file);
2659 break;
2660
2661 default:
2662 BFD_ASSERT (0);
2663 }
2664 }
2665
2666 static void
2667 v850_elf_print_notes (bfd * abfd, FILE * file)
2668 {
2669 asection * notes = bfd_get_section_by_name (abfd, V850_NOTE_SECNAME);
2670 enum v850_notes id;
2671
2672 if (notes == NULL || notes->contents == NULL)
2673 return;
2674
2675 BFD_ASSERT (bfd_section_size (notes) == NUM_V850_NOTES * SIZEOF_V850_NOTE);
2676
2677 for (id = V850_NOTE_ALIGNMENT; id <= NUM_V850_NOTES; id++)
2678 print_v850_note (abfd, file, notes->contents, id);
2679 }
2680
2681 /* Set the right machine number and architecture. */
2682
2683 static bfd_boolean
2684 v850_elf_object_p (bfd *abfd)
2685 {
2686 enum bfd_architecture arch;
2687 unsigned long mach;
2688
2689 switch (elf_elfheader (abfd)->e_machine)
2690 {
2691 case EM_V800:
2692 arch = bfd_arch_v850_rh850;
2693 mach = (elf_elfheader (abfd)->e_flags & EF_V800_850E3)
2694 ? bfd_mach_v850e3v5 : bfd_mach_v850e2v3;
2695 break;
2696
2697 case EM_CYGNUS_V850:
2698 case EM_V850:
2699 arch = bfd_arch_v850;
2700 switch (elf_elfheader (abfd)->e_flags & EF_V850_ARCH)
2701 {
2702 default:
2703 case E_V850_ARCH: mach = bfd_mach_v850; break;
2704 case E_V850E_ARCH: mach = bfd_mach_v850e; break;
2705 case E_V850E1_ARCH: mach = bfd_mach_v850e1; break;
2706 case E_V850E2_ARCH: mach = bfd_mach_v850e2; break;
2707 case E_V850E2V3_ARCH: mach = bfd_mach_v850e2v3; break;
2708 case E_V850E3V5_ARCH: mach = bfd_mach_v850e3v5; break;
2709 }
2710 break;
2711
2712 default:
2713 return FALSE;
2714 }
2715
2716 return bfd_default_set_arch_mach (abfd, arch, mach);
2717 }
2718
2719 /* Store the machine number in the flags field. */
2720
2721 static bfd_boolean
2722 v850_elf_final_write_processing (bfd *abfd)
2723 {
2724 unsigned long val;
2725
2726 switch (bfd_get_arch (abfd))
2727 {
2728 case bfd_arch_v850_rh850:
2729 val = EF_RH850_ABI;
2730 if (bfd_get_mach (abfd) == bfd_mach_v850e3v5)
2731 val |= EF_V800_850E3;
2732 elf_elfheader (abfd)->e_flags |= val;
2733 break;
2734
2735 case bfd_arch_v850:
2736 switch (bfd_get_mach (abfd))
2737 {
2738 default:
2739 case bfd_mach_v850: val = E_V850_ARCH; break;
2740 case bfd_mach_v850e: val = E_V850E_ARCH; break;
2741 case bfd_mach_v850e1: val = E_V850E1_ARCH; break;
2742 case bfd_mach_v850e2: val = E_V850E2_ARCH; break;
2743 case bfd_mach_v850e2v3: val = E_V850E2V3_ARCH; break;
2744 case bfd_mach_v850e3v5: val = E_V850E3V5_ARCH; break;
2745 }
2746 elf_elfheader (abfd)->e_flags &=~ EF_V850_ARCH;
2747 elf_elfheader (abfd)->e_flags |= val;
2748 break;
2749 default:
2750 break;
2751 }
2752 return _bfd_elf_final_write_processing (abfd);
2753 }
2754
2755 /* Function to keep V850 specific file flags. */
2756
2757 static bfd_boolean
2758 v850_elf_set_private_flags (bfd *abfd, flagword flags)
2759 {
2760 BFD_ASSERT (!elf_flags_init (abfd)
2761 || elf_elfheader (abfd)->e_flags == flags);
2762
2763 elf_elfheader (abfd)->e_flags = flags;
2764 elf_flags_init (abfd) = TRUE;
2765 return TRUE;
2766 }
2767
2768 /* Merge backend specific data from an object file
2769 to the output object file when linking. */
2770
2771 static bfd_boolean
2772 v850_elf_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
2773 {
2774 bfd *obfd = info->output_bfd;
2775 flagword out_flags;
2776 flagword in_flags;
2777 bfd_boolean result = TRUE;
2778
2779 if ( bfd_get_flavour (ibfd) != bfd_target_elf_flavour
2780 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
2781 return TRUE;
2782
2783 result &= v850_elf_merge_notes (ibfd, obfd);
2784
2785 in_flags = elf_elfheader (ibfd)->e_flags;
2786 out_flags = elf_elfheader (obfd)->e_flags;
2787
2788 if (! elf_flags_init (obfd))
2789 {
2790 /* If the input is the default architecture then do not
2791 bother setting the flags for the output architecture,
2792 instead allow future merges to do this. If no future
2793 merges ever set these flags then they will retain their
2794 unitialised values, which surprise surprise, correspond
2795 to the default values. */
2796 if (bfd_get_arch_info (ibfd)->the_default)
2797 return TRUE;
2798
2799 elf_flags_init (obfd) = TRUE;
2800 elf_elfheader (obfd)->e_flags = in_flags;
2801
2802 if (bfd_get_arch (obfd) == bfd_get_arch (ibfd)
2803 && bfd_get_arch_info (obfd)->the_default)
2804 result &= bfd_set_arch_mach (obfd, bfd_get_arch (ibfd), bfd_get_mach (ibfd));
2805
2806 return result;
2807 }
2808
2809 /* Check flag compatibility. */
2810 if (in_flags == out_flags)
2811 return result;
2812
2813 if (bfd_get_arch (obfd) == bfd_arch_v850_rh850)
2814 {
2815 if ((in_flags & EF_V800_850E3) != (out_flags & EF_V800_850E3))
2816 {
2817 _bfd_error_handler
2818 (_("%pB: architecture mismatch with previous modules"), ibfd);
2819 elf_elfheader (obfd)->e_flags |= EF_V800_850E3;
2820 }
2821
2822 return result;
2823 }
2824
2825 if ((in_flags & EF_V850_ARCH) != (out_flags & EF_V850_ARCH)
2826 && (in_flags & EF_V850_ARCH) != E_V850_ARCH)
2827 {
2828 /* Allow earlier architecture binaries to be linked with later binaries.
2829 Set the output binary to the later architecture, except for v850e1,
2830 which we set to v850e. */
2831 if ( (in_flags & EF_V850_ARCH) == E_V850E1_ARCH
2832 && (out_flags & EF_V850_ARCH) == E_V850E_ARCH)
2833 return result;
2834
2835 if ( (in_flags & EF_V850_ARCH) == E_V850_ARCH
2836 && (out_flags & EF_V850_ARCH) == E_V850E_ARCH)
2837 {
2838 elf_elfheader (obfd)->e_flags =
2839 ((out_flags & ~ EF_V850_ARCH) | E_V850E_ARCH);
2840 return result;
2841 }
2842
2843 if (( (in_flags & EF_V850_ARCH) == E_V850_ARCH
2844 || (in_flags & EF_V850_ARCH) == E_V850E_ARCH)
2845 && (out_flags & EF_V850_ARCH) == E_V850E2_ARCH)
2846 {
2847 elf_elfheader (obfd)->e_flags =
2848 ((out_flags & ~ EF_V850_ARCH) | E_V850E2_ARCH);
2849 return result;
2850 }
2851
2852 if (( (in_flags & EF_V850_ARCH) == E_V850_ARCH
2853 || (in_flags & EF_V850_ARCH) == E_V850E_ARCH
2854 || (in_flags & EF_V850_ARCH) == E_V850E2_ARCH)
2855 && (out_flags & EF_V850_ARCH) == E_V850E2V3_ARCH)
2856 {
2857 elf_elfheader (obfd)->e_flags =
2858 ((out_flags & ~ EF_V850_ARCH) | E_V850E2V3_ARCH);
2859 return result;
2860 }
2861
2862 if (( (in_flags & EF_V850_ARCH) == E_V850_ARCH
2863 || (in_flags & EF_V850_ARCH) == E_V850E_ARCH
2864 || (in_flags & EF_V850_ARCH) == E_V850E2_ARCH
2865 || (in_flags & EF_V850_ARCH) == E_V850E2V3_ARCH)
2866 && (out_flags & EF_V850_ARCH) == E_V850E3V5_ARCH)
2867 {
2868 elf_elfheader (obfd)->e_flags =
2869 ((out_flags & ~ EF_V850_ARCH) | E_V850E3V5_ARCH);
2870 return result;
2871 }
2872
2873 _bfd_error_handler
2874 (_("%pB: architecture mismatch with previous modules"), ibfd);
2875 }
2876
2877 return result;
2878 }
2879
2880 /* Display the flags field. */
2881
2882 static bfd_boolean
2883 v850_elf_print_private_bfd_data (bfd *abfd, void * ptr)
2884 {
2885 FILE * file = (FILE *) ptr;
2886
2887 BFD_ASSERT (abfd != NULL && ptr != NULL);
2888
2889 _bfd_elf_print_private_bfd_data (abfd, ptr);
2890
2891 /* xgettext:c-format. */
2892 fprintf (file, _("private flags = %lx: "), elf_elfheader (abfd)->e_flags);
2893
2894 if (bfd_get_arch (abfd) == bfd_arch_v850_rh850)
2895 {
2896 if ((elf_elfheader (abfd)->e_flags & EF_RH850_ABI) != EF_RH850_ABI)
2897 fprintf (file, _("unknown v850 architecture"));
2898 else if (elf_elfheader (abfd)->e_flags & EF_V800_850E3)
2899 fprintf (file, _("v850 E3 architecture"));
2900 else
2901 fprintf (file, _("v850 architecture"));
2902 }
2903 else
2904 {
2905 switch (elf_elfheader (abfd)->e_flags & EF_V850_ARCH)
2906 {
2907 default:
2908 case E_V850_ARCH: fprintf (file, _("v850 architecture")); break;
2909 case E_V850E_ARCH: fprintf (file, _("v850e architecture")); break;
2910 case E_V850E1_ARCH: fprintf (file, _("v850e1 architecture")); break;
2911 case E_V850E2_ARCH: fprintf (file, _("v850e2 architecture")); break;
2912 case E_V850E2V3_ARCH: fprintf (file, _("v850e2v3 architecture")); break;
2913 case E_V850E3V5_ARCH: fprintf (file, _("v850e3v5 architecture")); break;
2914 }
2915 }
2916
2917 fputc ('\n', file);
2918
2919 v850_elf_print_notes (abfd, file);
2920
2921 return TRUE;
2922 }
2923
2924 /* V850 ELF uses four common sections. One is the usual one, and the
2925 others are for (small) objects in one of the special data areas:
2926 small, tiny and zero. All the objects are kept together, and then
2927 referenced via the gp register, the ep register or the r0 register
2928 respectively, which yields smaller, faster assembler code. This
2929 approach is copied from elf32-mips.c. */
2930
2931 static asection v850_elf_scom_section;
2932 static asymbol v850_elf_scom_symbol;
2933 static asymbol * v850_elf_scom_symbol_ptr;
2934 static asection v850_elf_tcom_section;
2935 static asymbol v850_elf_tcom_symbol;
2936 static asymbol * v850_elf_tcom_symbol_ptr;
2937 static asection v850_elf_zcom_section;
2938 static asymbol v850_elf_zcom_symbol;
2939 static asymbol * v850_elf_zcom_symbol_ptr;
2940
2941 /* Given a BFD section, try to locate the
2942 corresponding ELF section index. */
2943
2944 static bfd_boolean
2945 v850_elf_section_from_bfd_section (bfd *abfd ATTRIBUTE_UNUSED,
2946 asection *sec,
2947 int *retval)
2948 {
2949 if (strcmp (bfd_section_name (sec), ".scommon") == 0)
2950 *retval = SHN_V850_SCOMMON;
2951 else if (strcmp (bfd_section_name (sec), ".tcommon") == 0)
2952 *retval = SHN_V850_TCOMMON;
2953 else if (strcmp (bfd_section_name (sec), ".zcommon") == 0)
2954 *retval = SHN_V850_ZCOMMON;
2955 else
2956 return FALSE;
2957
2958 return TRUE;
2959 }
2960
2961 /* Handle the special V850 section numbers that a symbol may use. */
2962
2963 static void
2964 v850_elf_symbol_processing (bfd *abfd, asymbol *asym)
2965 {
2966 elf_symbol_type * elfsym = (elf_symbol_type *) asym;
2967 unsigned int indx;
2968
2969 indx = elfsym->internal_elf_sym.st_shndx;
2970
2971 /* If the section index is an "ordinary" index, then it may
2972 refer to a v850 specific section created by the assembler.
2973 Check the section's type and change the index it matches.
2974
2975 FIXME: Should we alter the st_shndx field as well ? */
2976
2977 if (indx < elf_numsections (abfd))
2978 switch (elf_elfsections (abfd)[indx]->sh_type)
2979 {
2980 case SHT_V850_SCOMMON:
2981 indx = SHN_V850_SCOMMON;
2982 break;
2983
2984 case SHT_V850_TCOMMON:
2985 indx = SHN_V850_TCOMMON;
2986 break;
2987
2988 case SHT_V850_ZCOMMON:
2989 indx = SHN_V850_ZCOMMON;
2990 break;
2991
2992 default:
2993 break;
2994 }
2995
2996 switch (indx)
2997 {
2998 case SHN_V850_SCOMMON:
2999 if (v850_elf_scom_section.name == NULL)
3000 {
3001 /* Initialize the small common section. */
3002 v850_elf_scom_section.name = ".scommon";
3003 v850_elf_scom_section.flags = SEC_IS_COMMON | SEC_ALLOC | SEC_DATA;
3004 v850_elf_scom_section.output_section = & v850_elf_scom_section;
3005 v850_elf_scom_section.symbol = & v850_elf_scom_symbol;
3006 v850_elf_scom_section.symbol_ptr_ptr = & v850_elf_scom_symbol_ptr;
3007 v850_elf_scom_symbol.name = ".scommon";
3008 v850_elf_scom_symbol.flags = BSF_SECTION_SYM;
3009 v850_elf_scom_symbol.section = & v850_elf_scom_section;
3010 v850_elf_scom_symbol_ptr = & v850_elf_scom_symbol;
3011 }
3012 asym->section = & v850_elf_scom_section;
3013 asym->value = elfsym->internal_elf_sym.st_size;
3014 break;
3015
3016 case SHN_V850_TCOMMON:
3017 if (v850_elf_tcom_section.name == NULL)
3018 {
3019 /* Initialize the tcommon section. */
3020 v850_elf_tcom_section.name = ".tcommon";
3021 v850_elf_tcom_section.flags = SEC_IS_COMMON;
3022 v850_elf_tcom_section.output_section = & v850_elf_tcom_section;
3023 v850_elf_tcom_section.symbol = & v850_elf_tcom_symbol;
3024 v850_elf_tcom_section.symbol_ptr_ptr = & v850_elf_tcom_symbol_ptr;
3025 v850_elf_tcom_symbol.name = ".tcommon";
3026 v850_elf_tcom_symbol.flags = BSF_SECTION_SYM;
3027 v850_elf_tcom_symbol.section = & v850_elf_tcom_section;
3028 v850_elf_tcom_symbol_ptr = & v850_elf_tcom_symbol;
3029 }
3030 asym->section = & v850_elf_tcom_section;
3031 asym->value = elfsym->internal_elf_sym.st_size;
3032 break;
3033
3034 case SHN_V850_ZCOMMON:
3035 if (v850_elf_zcom_section.name == NULL)
3036 {
3037 /* Initialize the zcommon section. */
3038 v850_elf_zcom_section.name = ".zcommon";
3039 v850_elf_zcom_section.flags = SEC_IS_COMMON;
3040 v850_elf_zcom_section.output_section = & v850_elf_zcom_section;
3041 v850_elf_zcom_section.symbol = & v850_elf_zcom_symbol;
3042 v850_elf_zcom_section.symbol_ptr_ptr = & v850_elf_zcom_symbol_ptr;
3043 v850_elf_zcom_symbol.name = ".zcommon";
3044 v850_elf_zcom_symbol.flags = BSF_SECTION_SYM;
3045 v850_elf_zcom_symbol.section = & v850_elf_zcom_section;
3046 v850_elf_zcom_symbol_ptr = & v850_elf_zcom_symbol;
3047 }
3048 asym->section = & v850_elf_zcom_section;
3049 asym->value = elfsym->internal_elf_sym.st_size;
3050 break;
3051 }
3052 }
3053
3054 /* Hook called by the linker routine which adds symbols from an object
3055 file. We must handle the special v850 section numbers here. */
3056
3057 static bfd_boolean
3058 v850_elf_add_symbol_hook (bfd *abfd,
3059 struct bfd_link_info *info ATTRIBUTE_UNUSED,
3060 Elf_Internal_Sym *sym,
3061 const char **namep ATTRIBUTE_UNUSED,
3062 flagword *flagsp ATTRIBUTE_UNUSED,
3063 asection **secp,
3064 bfd_vma *valp)
3065 {
3066 unsigned int indx = sym->st_shndx;
3067
3068 /* If the section index is an "ordinary" index, then it may
3069 refer to a v850 specific section created by the assembler.
3070 Check the section's type and change the index it matches.
3071
3072 FIXME: Should we alter the st_shndx field as well ? */
3073
3074 if (indx < elf_numsections (abfd))
3075 switch (elf_elfsections (abfd)[indx]->sh_type)
3076 {
3077 case SHT_V850_SCOMMON:
3078 indx = SHN_V850_SCOMMON;
3079 break;
3080
3081 case SHT_V850_TCOMMON:
3082 indx = SHN_V850_TCOMMON;
3083 break;
3084
3085 case SHT_V850_ZCOMMON:
3086 indx = SHN_V850_ZCOMMON;
3087 break;
3088
3089 default:
3090 break;
3091 }
3092
3093 switch (indx)
3094 {
3095 case SHN_V850_SCOMMON:
3096 *secp = bfd_make_section_old_way (abfd, ".scommon");
3097 (*secp)->flags |= SEC_IS_COMMON;
3098 *valp = sym->st_size;
3099 break;
3100
3101 case SHN_V850_TCOMMON:
3102 *secp = bfd_make_section_old_way (abfd, ".tcommon");
3103 (*secp)->flags |= SEC_IS_COMMON;
3104 *valp = sym->st_size;
3105 break;
3106
3107 case SHN_V850_ZCOMMON:
3108 *secp = bfd_make_section_old_way (abfd, ".zcommon");
3109 (*secp)->flags |= SEC_IS_COMMON;
3110 *valp = sym->st_size;
3111 break;
3112 }
3113
3114 return TRUE;
3115 }
3116
3117 static int
3118 v850_elf_link_output_symbol_hook (struct bfd_link_info *info ATTRIBUTE_UNUSED,
3119 const char *name ATTRIBUTE_UNUSED,
3120 Elf_Internal_Sym *sym,
3121 asection *input_sec,
3122 struct elf_link_hash_entry *h ATTRIBUTE_UNUSED)
3123 {
3124 /* If we see a common symbol, which implies a relocatable link, then
3125 if a symbol was in a special common section in an input file, mark
3126 it as a special common in the output file. */
3127
3128 if (sym->st_shndx == SHN_COMMON)
3129 {
3130 if (strcmp (input_sec->name, ".scommon") == 0)
3131 sym->st_shndx = SHN_V850_SCOMMON;
3132 else if (strcmp (input_sec->name, ".tcommon") == 0)
3133 sym->st_shndx = SHN_V850_TCOMMON;
3134 else if (strcmp (input_sec->name, ".zcommon") == 0)
3135 sym->st_shndx = SHN_V850_ZCOMMON;
3136 }
3137
3138 /* The price we pay for using h->other unused bits as flags in the
3139 linker is cleaning up after ourselves. */
3140
3141 sym->st_other &= ~(V850_OTHER_SDA | V850_OTHER_ZDA | V850_OTHER_TDA
3142 | V850_OTHER_ERROR);
3143
3144 return 1;
3145 }
3146
3147 static bfd_boolean
3148 v850_elf_section_from_shdr (bfd *abfd,
3149 Elf_Internal_Shdr *hdr,
3150 const char *name,
3151 int shindex)
3152 {
3153 /* There ought to be a place to keep ELF backend specific flags, but
3154 at the moment there isn't one. We just keep track of the
3155 sections by their name, instead. */
3156
3157 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
3158 return FALSE;
3159
3160 switch (hdr->sh_type)
3161 {
3162 case SHT_V850_SCOMMON:
3163 case SHT_V850_TCOMMON:
3164 case SHT_V850_ZCOMMON:
3165 if (!bfd_set_section_flags (hdr->bfd_section,
3166 (bfd_section_flags (hdr->bfd_section)
3167 | SEC_IS_COMMON)))
3168 return FALSE;
3169 }
3170
3171 return TRUE;
3172 }
3173
3174 /* Set the correct type for a V850 ELF section. We do this
3175 by the section name, which is a hack, but ought to work. */
3176
3177 static bfd_boolean
3178 v850_elf_fake_sections (bfd *abfd ATTRIBUTE_UNUSED,
3179 Elf_Internal_Shdr *hdr,
3180 asection *sec)
3181 {
3182 const char * name;
3183
3184 name = bfd_section_name (sec);
3185
3186 if (strcmp (name, ".scommon") == 0)
3187 hdr->sh_type = SHT_V850_SCOMMON;
3188 else if (strcmp (name, ".tcommon") == 0)
3189 hdr->sh_type = SHT_V850_TCOMMON;
3190 else if (strcmp (name, ".zcommon") == 0)
3191 hdr->sh_type = SHT_V850_ZCOMMON;
3192 /* Tweak the section type of .note.renesas. */
3193 else if (strcmp (name, V850_NOTE_SECNAME) == 0)
3194 {
3195 hdr->sh_type = SHT_RENESAS_INFO;
3196 hdr->sh_entsize = SIZEOF_V850_NOTE;
3197 }
3198
3199 return TRUE;
3200 }
3201
3202 /* Delete some bytes from a section while relaxing. */
3203
3204 static bfd_boolean
3205 v850_elf_relax_delete_bytes (bfd *abfd,
3206 asection *sec,
3207 bfd_vma addr,
3208 bfd_vma toaddr,
3209 int count)
3210 {
3211 Elf_Internal_Shdr *symtab_hdr;
3212 Elf32_External_Sym *extsyms;
3213 Elf32_External_Sym *esym;
3214 Elf32_External_Sym *esymend;
3215 int sym_index;
3216 unsigned int sec_shndx;
3217 bfd_byte *contents;
3218 Elf_Internal_Rela *irel;
3219 Elf_Internal_Rela *irelend;
3220 struct elf_link_hash_entry *sym_hash;
3221 Elf_External_Sym_Shndx *shndx;
3222
3223 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
3224 extsyms = (Elf32_External_Sym *) symtab_hdr->contents;
3225
3226 sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
3227
3228 contents = elf_section_data (sec)->this_hdr.contents;
3229
3230 /* The deletion must stop at the next ALIGN reloc for an alignment
3231 power larger than the number of bytes we are deleting. */
3232
3233 /* Actually delete the bytes. */
3234 #if (DEBUG_RELAX & 2)
3235 fprintf (stderr, "relax_delete: contents: sec: %s %p .. %p %x\n",
3236 sec->name, addr, toaddr, count );
3237 #endif
3238 memmove (contents + addr, contents + addr + count,
3239 toaddr - addr - count);
3240 memset (contents + toaddr-count, 0, count);
3241
3242 /* Adjust all the relocs. */
3243 irel = elf_section_data (sec)->relocs;
3244 irelend = irel + sec->reloc_count;
3245 if (elf_symtab_shndx_list (abfd))
3246 {
3247 Elf_Internal_Shdr *shndx_hdr;
3248
3249 shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr;
3250 shndx = (Elf_External_Sym_Shndx *) shndx_hdr->contents;
3251 }
3252 else
3253 {
3254 shndx = NULL;
3255 }
3256
3257 for (; irel < irelend; irel++)
3258 {
3259 bfd_vma raddr, paddr, symval;
3260 Elf_Internal_Sym isym;
3261
3262 /* Get the new reloc address. */
3263 raddr = irel->r_offset;
3264 if ((raddr >= (addr + count) && raddr < toaddr))
3265 irel->r_offset -= count;
3266
3267 if (raddr >= addr && raddr < addr + count)
3268 {
3269 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
3270 (int) R_V850_NONE);
3271 continue;
3272 }
3273
3274 if (ELF32_R_TYPE (irel->r_info) == (int) R_V850_ALIGN)
3275 continue;
3276
3277 bfd_elf32_swap_symbol_in (abfd,
3278 extsyms + ELF32_R_SYM (irel->r_info),
3279 shndx ? shndx + ELF32_R_SYM (irel->r_info) : NULL,
3280 & isym);
3281
3282 if (isym.st_shndx != sec_shndx)
3283 continue;
3284
3285 /* Get the value of the symbol referred to by the reloc. */
3286 if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
3287 {
3288 symval = isym.st_value;
3289 #if (DEBUG_RELAX & 2)
3290 {
3291 char * name = bfd_elf_string_from_elf_section
3292 (abfd, symtab_hdr->sh_link, isym.st_name);
3293 fprintf (stderr,
3294 "relax_delete: local: sec: %s, sym: %s (%d), value: %x + %x + %x addend %x\n",
3295 sec->name, name, isym.st_name,
3296 sec->output_section->vma, sec->output_offset,
3297 isym.st_value, irel->r_addend);
3298 }
3299 #endif
3300 }
3301 else
3302 {
3303 unsigned long indx;
3304 struct elf_link_hash_entry * h;
3305
3306 /* An external symbol. */
3307 indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
3308
3309 h = elf_sym_hashes (abfd) [indx];
3310 BFD_ASSERT (h != NULL);
3311
3312 symval = h->root.u.def.value;
3313 #if (DEBUG_RELAX & 2)
3314 fprintf (stderr,
3315 "relax_delete: defined: sec: %s, name: %s, value: %x + %x + %x addend %x\n",
3316 sec->name, h->root.root.string, h->root.u.def.value,
3317 sec->output_section->vma, sec->output_offset, irel->r_addend);
3318 #endif
3319 }
3320
3321 paddr = symval + irel->r_addend;
3322
3323 if ( (symval >= addr + count && symval < toaddr)
3324 && (paddr < addr + count || paddr >= toaddr))
3325 irel->r_addend += count;
3326 else if ( (symval < addr + count || symval >= toaddr)
3327 && (paddr >= addr + count && paddr < toaddr))
3328 irel->r_addend -= count;
3329 }
3330
3331 /* Adjust the local symbols defined in this section. */
3332 esym = extsyms;
3333 esymend = esym + symtab_hdr->sh_info;
3334
3335 for (; esym < esymend; esym++, shndx = (shndx ? shndx + 1 : NULL))
3336 {
3337 Elf_Internal_Sym isym;
3338
3339 bfd_elf32_swap_symbol_in (abfd, esym, shndx, & isym);
3340
3341 if (isym.st_shndx == sec_shndx
3342 && isym.st_value >= addr + count
3343 && isym.st_value < toaddr)
3344 {
3345 isym.st_value -= count;
3346
3347 if (isym.st_value + isym.st_size >= toaddr)
3348 isym.st_size += count;
3349
3350 bfd_elf32_swap_symbol_out (abfd, & isym, esym, shndx);
3351 }
3352 else if (isym.st_shndx == sec_shndx
3353 && isym.st_value < addr + count)
3354 {
3355 if (isym.st_value+isym.st_size >= addr + count
3356 && isym.st_value+isym.st_size < toaddr)
3357 isym.st_size -= count;
3358
3359 if (isym.st_value >= addr
3360 && isym.st_value < addr + count)
3361 isym.st_value = addr;
3362
3363 bfd_elf32_swap_symbol_out (abfd, & isym, esym, shndx);
3364 }
3365 }
3366
3367 /* Now adjust the global symbols defined in this section. */
3368 esym = extsyms + symtab_hdr->sh_info;
3369 esymend = extsyms + (symtab_hdr->sh_size / sizeof (Elf32_External_Sym));
3370
3371 for (sym_index = 0; esym < esymend; esym ++, sym_index ++)
3372 {
3373 Elf_Internal_Sym isym;
3374
3375 bfd_elf32_swap_symbol_in (abfd, esym, shndx, & isym);
3376 sym_hash = elf_sym_hashes (abfd) [sym_index];
3377
3378 if (isym.st_shndx == sec_shndx
3379 && ((sym_hash)->root.type == bfd_link_hash_defined
3380 || (sym_hash)->root.type == bfd_link_hash_defweak)
3381 && (sym_hash)->root.u.def.section == sec
3382 && (sym_hash)->root.u.def.value >= addr + count
3383 && (sym_hash)->root.u.def.value < toaddr)
3384 {
3385 if ((sym_hash)->root.u.def.value + isym.st_size >= toaddr)
3386 {
3387 isym.st_size += count;
3388 bfd_elf32_swap_symbol_out (abfd, & isym, esym, shndx);
3389 }
3390
3391 (sym_hash)->root.u.def.value -= count;
3392 }
3393 else if (isym.st_shndx == sec_shndx
3394 && ((sym_hash)->root.type == bfd_link_hash_defined
3395 || (sym_hash)->root.type == bfd_link_hash_defweak)
3396 && (sym_hash)->root.u.def.section == sec
3397 && (sym_hash)->root.u.def.value < addr + count)
3398 {
3399 if ((sym_hash)->root.u.def.value+isym.st_size >= addr + count
3400 && (sym_hash)->root.u.def.value+isym.st_size < toaddr)
3401 isym.st_size -= count;
3402
3403 if ((sym_hash)->root.u.def.value >= addr
3404 && (sym_hash)->root.u.def.value < addr + count)
3405 (sym_hash)->root.u.def.value = addr;
3406
3407 bfd_elf32_swap_symbol_out (abfd, & isym, esym, shndx);
3408 }
3409
3410 if (shndx)
3411 ++ shndx;
3412 }
3413
3414 return TRUE;
3415 }
3416
3417 #define NOP_OPCODE (0x0000)
3418 #define MOVHI 0x0640 /* 4byte. */
3419 #define MOVHI_MASK 0x07e0
3420 #define MOVHI_R1(insn) ((insn) & 0x1f) /* 4byte. */
3421 #define MOVHI_R2(insn) ((insn) >> 11)
3422 #define MOVEA 0x0620 /* 2byte. */
3423 #define MOVEA_MASK 0x07e0
3424 #define MOVEA_R1(insn) ((insn) & 0x1f)
3425 #define MOVEA_R2(insn) ((insn) >> 11)
3426 #define JARL_4 0x00040780 /* 4byte. */
3427 #define JARL_4_MASK 0xFFFF07FF
3428 #define JARL_R2(insn) (int)(((insn) & (~JARL_4_MASK)) >> 11)
3429 #define ADD_I 0x0240 /* 2byte. */
3430 #define ADD_I_MASK 0x07e0
3431 #define ADD_I5(insn) ((((insn) & 0x001f) << 11) >> 11) /* 2byte. */
3432 #define ADD_R2(insn) ((insn) >> 11)
3433 #define JMP_R 0x0060 /* 2byte. */
3434 #define JMP_R_MASK 0xFFE0
3435 #define JMP_R1(insn) ((insn) & 0x1f)
3436
3437 static bfd_boolean
3438 v850_elf_relax_section (bfd *abfd,
3439 asection *sec,
3440 struct bfd_link_info *link_info,
3441 bfd_boolean *again)
3442 {
3443 Elf_Internal_Shdr *symtab_hdr;
3444 Elf_Internal_Rela *internal_relocs;
3445 Elf_Internal_Rela *irel;
3446 Elf_Internal_Rela *irelend;
3447 Elf_Internal_Rela *irelalign = NULL;
3448 Elf_Internal_Sym *isymbuf = NULL;
3449 bfd_byte *contents = NULL;
3450 bfd_vma addr = 0;
3451 bfd_vma toaddr;
3452 int align_pad_size = 0;
3453 bfd_boolean result = TRUE;
3454
3455 *again = FALSE;
3456
3457 if (bfd_link_relocatable (link_info)
3458 || (sec->flags & SEC_RELOC) == 0
3459 || sec->reloc_count == 0)
3460 return TRUE;
3461
3462 symtab_hdr = & elf_tdata (abfd)->symtab_hdr;
3463
3464 internal_relocs = (_bfd_elf_link_read_relocs
3465 (abfd, sec, NULL, NULL, link_info->keep_memory));
3466 if (internal_relocs == NULL)
3467 goto error_return;
3468
3469 irelend = internal_relocs + sec->reloc_count;
3470
3471 while (addr < sec->size)
3472 {
3473 toaddr = sec->size;
3474
3475 for (irel = internal_relocs; irel < irelend; irel ++)
3476 if (ELF32_R_TYPE (irel->r_info) == (int) R_V850_ALIGN
3477 && irel->r_offset > addr
3478 && irel->r_offset < toaddr)
3479 toaddr = irel->r_offset;
3480
3481 #ifdef DEBUG_RELAX
3482 fprintf (stderr, "relax region 0x%x to 0x%x align pad %d\n",
3483 addr, toaddr, align_pad_size);
3484 #endif
3485 if (irelalign)
3486 {
3487 bfd_vma alignto;
3488 bfd_vma alignmoveto;
3489
3490 alignmoveto = BFD_ALIGN (addr - align_pad_size, 1 << irelalign->r_addend);
3491 alignto = BFD_ALIGN (addr, 1 << irelalign->r_addend);
3492
3493 if (alignmoveto < alignto)
3494 {
3495 bfd_vma i;
3496
3497 align_pad_size = alignto - alignmoveto;
3498 #ifdef DEBUG_RELAX
3499 fprintf (stderr, "relax move region 0x%x to 0x%x delete size 0x%x\n",
3500 alignmoveto, toaddr, align_pad_size);
3501 #endif
3502 if (!v850_elf_relax_delete_bytes (abfd, sec, alignmoveto,
3503 toaddr, align_pad_size))
3504 goto error_return;
3505
3506 for (i = BFD_ALIGN (toaddr - align_pad_size, 1);
3507 (i + 1) < toaddr; i += 2)
3508 bfd_put_16 (abfd, NOP_OPCODE, contents + i);
3509
3510 addr = alignmoveto;
3511 }
3512 else
3513 align_pad_size = 0;
3514 }
3515
3516 for (irel = internal_relocs; irel < irelend; irel++)
3517 {
3518 bfd_vma laddr;
3519 bfd_vma addend;
3520 bfd_vma symval;
3521 int insn[5];
3522 int no_match = -1;
3523 Elf_Internal_Rela *hi_irelfn;
3524 Elf_Internal_Rela *lo_irelfn;
3525 Elf_Internal_Rela *irelcall;
3526 bfd_signed_vma foff;
3527 unsigned int r_type;
3528
3529 if (! (irel->r_offset >= addr && irel->r_offset < toaddr
3530 && (ELF32_R_TYPE (irel->r_info) == (int) R_V850_LONGCALL
3531 || ELF32_R_TYPE (irel->r_info) == (int) R_V850_LONGJUMP)))
3532 continue;
3533
3534 #ifdef DEBUG_RELAX
3535 fprintf (stderr, "relax check r_info 0x%x r_offset 0x%x r_addend 0x%x\n",
3536 irel->r_info,
3537 irel->r_offset,
3538 irel->r_addend );
3539 #endif
3540
3541 /* Get the section contents. */
3542 if (contents == NULL)
3543 {
3544 if (elf_section_data (sec)->this_hdr.contents != NULL)
3545 contents = elf_section_data (sec)->this_hdr.contents;
3546 else
3547 {
3548 if (! bfd_malloc_and_get_section (abfd, sec, &contents))
3549 goto error_return;
3550 }
3551 }
3552
3553 /* Read this BFD's local symbols if we haven't done so already. */
3554 if (isymbuf == NULL && symtab_hdr->sh_info != 0)
3555 {
3556 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
3557 if (isymbuf == NULL)
3558 isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
3559 symtab_hdr->sh_info, 0,
3560 NULL, NULL, NULL);
3561 if (isymbuf == NULL)
3562 goto error_return;
3563 }
3564
3565 laddr = irel->r_offset;
3566
3567 if (ELF32_R_TYPE (irel->r_info) == (int) R_V850_LONGCALL)
3568 {
3569 /* Check code for -mlong-calls output. */
3570 if (laddr + 16 <= (bfd_vma) sec->size)
3571 {
3572 insn[0] = bfd_get_16 (abfd, contents + laddr);
3573 insn[1] = bfd_get_16 (abfd, contents + laddr + 4);
3574 insn[2] = bfd_get_32 (abfd, contents + laddr + 8);
3575 insn[3] = bfd_get_16 (abfd, contents + laddr + 12);
3576 insn[4] = bfd_get_16 (abfd, contents + laddr + 14);
3577
3578 if ((insn[0] & MOVHI_MASK) != MOVHI
3579 || MOVHI_R1 (insn[0]) != 0)
3580 no_match = 0;
3581
3582 if (no_match < 0
3583 && ((insn[1] & MOVEA_MASK) != MOVEA
3584 || MOVHI_R2 (insn[0]) != MOVEA_R1 (insn[1])))
3585 no_match = 1;
3586
3587 if (no_match < 0
3588 && (insn[2] & JARL_4_MASK) != JARL_4)
3589 no_match = 2;
3590
3591 if (no_match < 0
3592 && ((insn[3] & ADD_I_MASK) != ADD_I
3593 || ADD_I5 (insn[3]) != 4
3594 || JARL_R2 (insn[2]) != ADD_R2 (insn[3])))
3595 no_match = 3;
3596
3597 if (no_match < 0
3598 && ((insn[4] & JMP_R_MASK) != JMP_R
3599 || MOVEA_R2 (insn[1]) != JMP_R1 (insn[4])))
3600 no_match = 4;
3601 }
3602 else
3603 {
3604 _bfd_error_handler
3605 /* xgettext:c-format */
3606 (_("%pB: %#" PRIx64 ": warning: %s points to "
3607 "unrecognized insns"),
3608 abfd, (uint64_t) irel->r_offset, "R_V850_LONGCALL");
3609 continue;
3610 }
3611
3612 if (no_match >= 0)
3613 {
3614 _bfd_error_handler
3615 /* xgettext:c-format */
3616 (_("%pB: %#" PRIx64 ": warning: %s points to "
3617 "unrecognized insn %#x"),
3618 abfd,
3619 (uint64_t) (irel->r_offset + no_match),
3620 "R_V850_LONGCALL",
3621 insn[no_match]);
3622 continue;
3623 }
3624
3625 /* Get the reloc for the address from which the register is
3626 being loaded. This reloc will tell us which function is
3627 actually being called. */
3628
3629 for (hi_irelfn = internal_relocs; hi_irelfn < irelend; hi_irelfn ++)
3630 {
3631 r_type = ELF32_R_TYPE (hi_irelfn->r_info);
3632
3633 if (hi_irelfn->r_offset == laddr + 2
3634 && (r_type == (int) R_V850_HI16_S || r_type == (int) R_V810_WHI1))
3635 break;
3636 }
3637
3638 for (lo_irelfn = internal_relocs; lo_irelfn < irelend; lo_irelfn ++)
3639 {
3640 r_type = ELF32_R_TYPE (lo_irelfn->r_info);
3641
3642 if (lo_irelfn->r_offset == laddr + 6
3643 && (r_type == (int) R_V850_LO16 || r_type == (int) R_V810_WLO))
3644 break;
3645 }
3646
3647 for (irelcall = internal_relocs; irelcall < irelend; irelcall ++)
3648 {
3649 r_type = ELF32_R_TYPE (irelcall->r_info);
3650
3651 if (irelcall->r_offset == laddr + 8
3652 && (r_type == (int) R_V850_22_PCREL || r_type == (int) R_V850_PCR22))
3653 break;
3654 }
3655
3656 if ( hi_irelfn == irelend
3657 || lo_irelfn == irelend
3658 || irelcall == irelend)
3659 {
3660 _bfd_error_handler
3661 /* xgettext:c-format */
3662 (_("%pB: %#" PRIx64 ": warning: %s points to "
3663 "unrecognized reloc"),
3664 abfd, (uint64_t) irel->r_offset, "R_V850_LONGCALL");
3665
3666 continue;
3667 }
3668
3669 if (ELF32_R_SYM (irelcall->r_info) < symtab_hdr->sh_info)
3670 {
3671 Elf_Internal_Sym * isym;
3672
3673 /* A local symbol. */
3674 isym = isymbuf + ELF32_R_SYM (irelcall->r_info);
3675
3676 symval = isym->st_value;
3677 }
3678 else
3679 {
3680 unsigned long indx;
3681 struct elf_link_hash_entry * h;
3682
3683 /* An external symbol. */
3684 indx = ELF32_R_SYM (irelcall->r_info) - symtab_hdr->sh_info;
3685 h = elf_sym_hashes (abfd)[indx];
3686 BFD_ASSERT (h != NULL);
3687
3688 if ( h->root.type != bfd_link_hash_defined
3689 && h->root.type != bfd_link_hash_defweak)
3690 /* This appears to be a reference to an undefined
3691 symbol. Just ignore it--it will be caught by the
3692 regular reloc processing. */
3693 continue;
3694
3695 symval = h->root.u.def.value;
3696 }
3697
3698 if (symval + irelcall->r_addend != irelcall->r_offset + 4)
3699 {
3700 _bfd_error_handler
3701 /* xgettext:c-format */
3702 (_("%pB: %#" PRIx64 ": warning: %s points to "
3703 "unrecognized reloc %#" PRIx64),
3704 abfd, (uint64_t) irel->r_offset, "R_V850_LONGCALL",
3705 (uint64_t) irelcall->r_offset);
3706 continue;
3707 }
3708
3709 /* Get the value of the symbol referred to by the reloc. */
3710 if (ELF32_R_SYM (hi_irelfn->r_info) < symtab_hdr->sh_info)
3711 {
3712 Elf_Internal_Sym *isym;
3713 asection *sym_sec;
3714
3715 /* A local symbol. */
3716 isym = isymbuf + ELF32_R_SYM (hi_irelfn->r_info);
3717
3718 if (isym->st_shndx == SHN_UNDEF)
3719 sym_sec = bfd_und_section_ptr;
3720 else if (isym->st_shndx == SHN_ABS)
3721 sym_sec = bfd_abs_section_ptr;
3722 else if (isym->st_shndx == SHN_COMMON)
3723 sym_sec = bfd_com_section_ptr;
3724 else
3725 sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
3726 symval = (isym->st_value
3727 + sym_sec->output_section->vma
3728 + sym_sec->output_offset);
3729 }
3730 else
3731 {
3732 unsigned long indx;
3733 struct elf_link_hash_entry *h;
3734
3735 /* An external symbol. */
3736 indx = ELF32_R_SYM (hi_irelfn->r_info) - symtab_hdr->sh_info;
3737 h = elf_sym_hashes (abfd)[indx];
3738 BFD_ASSERT (h != NULL);
3739
3740 if ( h->root.type != bfd_link_hash_defined
3741 && h->root.type != bfd_link_hash_defweak)
3742 /* This appears to be a reference to an undefined
3743 symbol. Just ignore it--it will be caught by the
3744 regular reloc processing. */
3745 continue;
3746
3747 symval = (h->root.u.def.value
3748 + h->root.u.def.section->output_section->vma
3749 + h->root.u.def.section->output_offset);
3750 }
3751
3752 addend = irel->r_addend;
3753
3754 foff = (symval + addend
3755 - (irel->r_offset
3756 + sec->output_section->vma
3757 + sec->output_offset
3758 + 4));
3759 #ifdef DEBUG_RELAX
3760 fprintf (stderr, "relax longcall r_offset 0x%x ptr 0x%x symbol 0x%x addend 0x%x distance 0x%x\n",
3761 irel->r_offset,
3762 (irel->r_offset
3763 + sec->output_section->vma
3764 + sec->output_offset),
3765 symval, addend, foff);
3766 #endif
3767
3768 if (foff < -0x100000 || foff >= 0x100000)
3769 /* After all that work, we can't shorten this function call. */
3770 continue;
3771
3772 /* For simplicity of coding, we are going to modify the section
3773 contents, the section relocs, and the BFD symbol table. We
3774 must tell the rest of the code not to free up this
3775 information. It would be possible to instead create a table
3776 of changes which have to be made, as is done in coff-mips.c;
3777 that would be more work, but would require less memory when
3778 the linker is run. */
3779 elf_section_data (sec)->relocs = internal_relocs;
3780 elf_section_data (sec)->this_hdr.contents = contents;
3781 symtab_hdr->contents = (bfd_byte *) isymbuf;
3782
3783 /* Replace the long call with a jarl. */
3784 if (bfd_get_arch (abfd) == bfd_arch_v850_rh850)
3785 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info), R_V850_PCR22);
3786 else
3787 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info), R_V850_22_PCREL);
3788
3789 addend = 0;
3790
3791 if (ELF32_R_SYM (hi_irelfn->r_info) < symtab_hdr->sh_info)
3792 /* If this needs to be changed because of future relaxing,
3793 it will be handled here like other internal IND12W
3794 relocs. */
3795 bfd_put_32 (abfd,
3796 0x00000780 | (JARL_R2 (insn[2])<<11) | ((addend << 16) & 0xffff) | ((addend >> 16) & 0xf),
3797 contents + irel->r_offset);
3798 else
3799 /* We can't fully resolve this yet, because the external
3800 symbol value may be changed by future relaxing.
3801 We let the final link phase handle it. */
3802 bfd_put_32 (abfd, 0x00000780 | (JARL_R2 (insn[2])<<11),
3803 contents + irel->r_offset);
3804
3805 hi_irelfn->r_info =
3806 ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info), R_V850_NONE);
3807 lo_irelfn->r_info =
3808 ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info), R_V850_NONE);
3809 irelcall->r_info =
3810 ELF32_R_INFO (ELF32_R_SYM (irelcall->r_info), R_V850_NONE);
3811
3812 if (! v850_elf_relax_delete_bytes (abfd, sec,
3813 irel->r_offset + 4, toaddr, 12))
3814 goto error_return;
3815
3816 align_pad_size += 12;
3817 }
3818 else if (ELF32_R_TYPE (irel->r_info) == (int) R_V850_LONGJUMP)
3819 {
3820 /* Check code for -mlong-jumps output. */
3821 if (laddr + 10 <= (bfd_vma) sec->size)
3822 {
3823 insn[0] = bfd_get_16 (abfd, contents + laddr);
3824 insn[1] = bfd_get_16 (abfd, contents + laddr + 4);
3825 insn[2] = bfd_get_16 (abfd, contents + laddr + 8);
3826
3827 if ((insn[0] & MOVHI_MASK) != MOVHI
3828 || MOVHI_R1 (insn[0]) != 0)
3829 no_match = 0;
3830
3831 if (no_match < 0
3832 && ((insn[1] & MOVEA_MASK) != MOVEA
3833 || MOVHI_R2 (insn[0]) != MOVEA_R1 (insn[1])))
3834 no_match = 1;
3835
3836 if (no_match < 0
3837 && ((insn[2] & JMP_R_MASK) != JMP_R
3838 || MOVEA_R2 (insn[1]) != JMP_R1 (insn[2])))
3839 no_match = 4;
3840 }
3841 else
3842 {
3843 _bfd_error_handler
3844 /* xgettext:c-format */
3845 (_("%pB: %#" PRIx64 ": warning: %s points to "
3846 "unrecognized insns"),
3847 abfd, (uint64_t) irel->r_offset, "R_V850_LONGJUMP");
3848 continue;
3849 }
3850
3851 if (no_match >= 0)
3852 {
3853 _bfd_error_handler
3854 /* xgettext:c-format */
3855 (_("%pB: %#" PRIx64 ": warning: %s points to "
3856 "unrecognized insn %#x"),
3857 abfd,
3858 (uint64_t) (irel->r_offset + no_match),
3859 "R_V850_LONGJUMP",
3860 insn[no_match]);
3861 continue;
3862 }
3863
3864 /* Get the reloc for the address from which the register is
3865 being loaded. This reloc will tell us which function is
3866 actually being called. */
3867 for (hi_irelfn = internal_relocs; hi_irelfn < irelend; hi_irelfn ++)
3868 {
3869 r_type = ELF32_R_TYPE (hi_irelfn->r_info);
3870
3871 if (hi_irelfn->r_offset == laddr + 2
3872 && ((r_type == (int) R_V850_HI16_S) || r_type == (int) R_V810_WHI1))
3873 break;
3874 }
3875
3876 for (lo_irelfn = internal_relocs; lo_irelfn < irelend; lo_irelfn ++)
3877 {
3878 r_type = ELF32_R_TYPE (lo_irelfn->r_info);
3879
3880 if (lo_irelfn->r_offset == laddr + 6
3881 && (r_type == (int) R_V850_LO16 || r_type == (int) R_V810_WLO))
3882 break;
3883 }
3884
3885 if ( hi_irelfn == irelend
3886 || lo_irelfn == irelend)
3887 {
3888 _bfd_error_handler
3889 /* xgettext:c-format */
3890 (_("%pB: %#" PRIx64 ": warning: %s points to "
3891 "unrecognized reloc"),
3892 abfd, (uint64_t) irel->r_offset, "R_V850_LONGJUMP");
3893 continue;
3894 }
3895
3896 /* Get the value of the symbol referred to by the reloc. */
3897 if (ELF32_R_SYM (hi_irelfn->r_info) < symtab_hdr->sh_info)
3898 {
3899 Elf_Internal_Sym * isym;
3900 asection * sym_sec;
3901
3902 /* A local symbol. */
3903 isym = isymbuf + ELF32_R_SYM (hi_irelfn->r_info);
3904
3905 if (isym->st_shndx == SHN_UNDEF)
3906 sym_sec = bfd_und_section_ptr;
3907 else if (isym->st_shndx == SHN_ABS)
3908 sym_sec = bfd_abs_section_ptr;
3909 else if (isym->st_shndx == SHN_COMMON)
3910 sym_sec = bfd_com_section_ptr;
3911 else
3912 sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
3913 symval = (isym->st_value
3914 + sym_sec->output_section->vma
3915 + sym_sec->output_offset);
3916 #ifdef DEBUG_RELAX
3917 {
3918 char * name = bfd_elf_string_from_elf_section
3919 (abfd, symtab_hdr->sh_link, isym->st_name);
3920
3921 fprintf (stderr, "relax long jump local: sec: %s, sym: %s (%d), value: %x + %x + %x addend %x\n",
3922 sym_sec->name, name, isym->st_name,
3923 sym_sec->output_section->vma,
3924 sym_sec->output_offset,
3925 isym->st_value, irel->r_addend);
3926 }
3927 #endif
3928 }
3929 else
3930 {
3931 unsigned long indx;
3932 struct elf_link_hash_entry * h;
3933
3934 /* An external symbol. */
3935 indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
3936 h = elf_sym_hashes (abfd)[indx];
3937 BFD_ASSERT (h != NULL);
3938
3939 if ( h->root.type != bfd_link_hash_defined
3940 && h->root.type != bfd_link_hash_defweak)
3941 /* This appears to be a reference to an undefined
3942 symbol. Just ignore it--it will be caught by the
3943 regular reloc processing. */
3944 continue;
3945
3946 symval = (h->root.u.def.value
3947 + h->root.u.def.section->output_section->vma
3948 + h->root.u.def.section->output_offset);
3949 #ifdef DEBUG_RELAX
3950 fprintf (stderr,
3951 "relax longjump defined: sec: %s, name: %s, value: %x + %x + %x addend %x\n",
3952 sec->name, h->root.root.string, h->root.u.def.value,
3953 sec->output_section->vma, sec->output_offset, irel->r_addend);
3954 #endif
3955 }
3956
3957 addend = irel->r_addend;
3958
3959 foff = (symval + addend
3960 - (irel->r_offset
3961 + sec->output_section->vma
3962 + sec->output_offset
3963 + 4));
3964 #ifdef DEBUG_RELAX
3965 fprintf (stderr, "relax longjump r_offset 0x%x ptr 0x%x symbol 0x%x addend 0x%x distance 0x%x\n",
3966 irel->r_offset,
3967 (irel->r_offset
3968 + sec->output_section->vma
3969 + sec->output_offset),
3970 symval, addend, foff);
3971 #endif
3972 if (foff < -0x100000 || foff >= 0x100000)
3973 /* After all that work, we can't shorten this function call. */
3974 continue;
3975
3976 /* For simplicity of coding, we are going to modify the section
3977 contents, the section relocs, and the BFD symbol table. We
3978 must tell the rest of the code not to free up this
3979 information. It would be possible to instead create a table
3980 of changes which have to be made, as is done in coff-mips.c;
3981 that would be more work, but would require less memory when
3982 the linker is run. */
3983 elf_section_data (sec)->relocs = internal_relocs;
3984 elf_section_data (sec)->this_hdr.contents = contents;
3985 symtab_hdr->contents = (bfd_byte *) isymbuf;
3986
3987 if (foff < -0x100 || foff >= 0x100)
3988 {
3989 /* Replace the long jump with a jr. */
3990
3991 if (bfd_get_arch (abfd) == bfd_arch_v850_rh850)
3992 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_V850_PCR22);
3993 else
3994 irel->r_info =
3995 ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_V850_22_PCREL);
3996
3997 irel->r_addend = addend;
3998 addend = 0;
3999
4000 if (ELF32_R_SYM (hi_irelfn->r_info) < symtab_hdr->sh_info)
4001 /* If this needs to be changed because of future relaxing,
4002 it will be handled here like other internal IND12W
4003 relocs. */
4004 bfd_put_32 (abfd,
4005 0x00000780 | ((addend << 15) & 0xffff0000) | ((addend >> 17) & 0xf),
4006 contents + irel->r_offset);
4007 else
4008 /* We can't fully resolve this yet, because the external
4009 symbol value may be changed by future relaxing.
4010 We let the final link phase handle it. */
4011 bfd_put_32 (abfd, 0x00000780, contents + irel->r_offset);
4012
4013 hi_irelfn->r_info =
4014 ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info), R_V850_NONE);
4015 lo_irelfn->r_info =
4016 ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info), R_V850_NONE);
4017 if (!v850_elf_relax_delete_bytes (abfd, sec,
4018 irel->r_offset + 4, toaddr, 6))
4019 goto error_return;
4020
4021 align_pad_size += 6;
4022 }
4023 else
4024 {
4025 /* Replace the long jump with a br. */
4026
4027 if (bfd_get_arch (abfd) == bfd_arch_v850_rh850)
4028 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_V850_PC9);
4029 else
4030 irel->r_info =
4031 ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_V850_9_PCREL);
4032
4033 irel->r_addend = addend;
4034 addend = 0;
4035
4036 if (ELF32_R_SYM (hi_irelfn->r_info) < symtab_hdr->sh_info)
4037 /* If this needs to be changed because of future relaxing,
4038 it will be handled here like other internal IND12W
4039 relocs. */
4040 bfd_put_16 (abfd,
4041 0x0585 | ((addend << 10) & 0xf800) | ((addend << 3) & 0x0070),
4042 contents + irel->r_offset);
4043 else
4044 /* We can't fully resolve this yet, because the external
4045 symbol value may be changed by future relaxing.
4046 We let the final link phase handle it. */
4047 bfd_put_16 (abfd, 0x0585, contents + irel->r_offset);
4048
4049 hi_irelfn->r_info =
4050 ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info), R_V850_NONE);
4051 lo_irelfn->r_info =
4052 ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info), R_V850_NONE);
4053 if (!v850_elf_relax_delete_bytes (abfd, sec,
4054 irel->r_offset + 2, toaddr, 8))
4055 goto error_return;
4056
4057 align_pad_size += 8;
4058 }
4059 }
4060 }
4061
4062 irelalign = NULL;
4063 for (irel = internal_relocs; irel < irelend; irel++)
4064 {
4065 if (ELF32_R_TYPE (irel->r_info) == (int) R_V850_ALIGN
4066 && irel->r_offset == toaddr)
4067 {
4068 irel->r_offset -= align_pad_size;
4069
4070 if (irelalign == NULL || irelalign->r_addend > irel->r_addend)
4071 irelalign = irel;
4072 }
4073 }
4074
4075 addr = toaddr;
4076 }
4077
4078 if (!irelalign)
4079 {
4080 #ifdef DEBUG_RELAX
4081 fprintf (stderr, "relax pad %d shorten %d -> %d\n",
4082 align_pad_size,
4083 sec->size,
4084 sec->size - align_pad_size);
4085 #endif
4086 sec->size -= align_pad_size;
4087 }
4088
4089 finish:
4090 if (internal_relocs != NULL
4091 && elf_section_data (sec)->relocs != internal_relocs)
4092 free (internal_relocs);
4093
4094 if (contents != NULL
4095 && elf_section_data (sec)->this_hdr.contents != (unsigned char *) contents)
4096 free (contents);
4097
4098 if (isymbuf != NULL
4099 && symtab_hdr->contents != (bfd_byte *) isymbuf)
4100 free (isymbuf);
4101
4102 return result;
4103
4104 error_return:
4105 result = FALSE;
4106 goto finish;
4107 }
4108
4109 static const struct bfd_elf_special_section v850_elf_special_sections[] =
4110 {
4111 { STRING_COMMA_LEN (".call_table_data"), 0, SHT_PROGBITS, (SHF_ALLOC + SHF_WRITE) },
4112 { STRING_COMMA_LEN (".call_table_text"), 0, SHT_PROGBITS, (SHF_ALLOC + SHF_WRITE
4113 + SHF_EXECINSTR) },
4114 { STRING_COMMA_LEN (".rosdata"), -2, SHT_PROGBITS, (SHF_ALLOC
4115 + SHF_V850_GPREL) },
4116 { STRING_COMMA_LEN (".rozdata"), -2, SHT_PROGBITS, (SHF_ALLOC
4117 + SHF_V850_R0REL) },
4118 { STRING_COMMA_LEN (".sbss"), -2, SHT_NOBITS, (SHF_ALLOC + SHF_WRITE
4119 + SHF_V850_GPREL) },
4120 { STRING_COMMA_LEN (".scommon"), -2, SHT_V850_SCOMMON, (SHF_ALLOC + SHF_WRITE
4121 + SHF_V850_GPREL) },
4122 { STRING_COMMA_LEN (".sdata"), -2, SHT_PROGBITS, (SHF_ALLOC + SHF_WRITE
4123 + SHF_V850_GPREL) },
4124 { STRING_COMMA_LEN (".tbss"), -2, SHT_NOBITS, (SHF_ALLOC + SHF_WRITE
4125 + SHF_V850_EPREL) },
4126 { STRING_COMMA_LEN (".tcommon"), -2, SHT_V850_TCOMMON, (SHF_ALLOC + SHF_WRITE
4127 + SHF_V850_R0REL) },
4128 { STRING_COMMA_LEN (".tdata"), -2, SHT_PROGBITS, (SHF_ALLOC + SHF_WRITE
4129 + SHF_V850_EPREL) },
4130 { STRING_COMMA_LEN (".zbss"), -2, SHT_NOBITS, (SHF_ALLOC + SHF_WRITE
4131 + SHF_V850_R0REL) },
4132 { STRING_COMMA_LEN (".zcommon"), -2, SHT_V850_ZCOMMON, (SHF_ALLOC + SHF_WRITE
4133 + SHF_V850_R0REL) },
4134 { STRING_COMMA_LEN (".zdata"), -2, SHT_PROGBITS, (SHF_ALLOC + SHF_WRITE
4135 + SHF_V850_R0REL) },
4136 { NULL, 0, 0, 0, 0 }
4137 };
4138 \f
4139 #define TARGET_LITTLE_SYM v850_elf32_vec
4140 #define TARGET_LITTLE_NAME "elf32-v850"
4141 #define ELF_ARCH bfd_arch_v850
4142 #define ELF_MACHINE_CODE EM_V850
4143 #define ELF_MACHINE_ALT1 EM_CYGNUS_V850
4144 #define ELF_MAXPAGESIZE 0x1000
4145
4146 #define elf_info_to_howto v850_elf_info_to_howto_rela
4147 #define elf_info_to_howto_rel v850_elf_info_to_howto_rel
4148
4149 #define elf_backend_check_relocs v850_elf_check_relocs
4150 #define elf_backend_relocate_section v850_elf_relocate_section
4151 #define elf_backend_object_p v850_elf_object_p
4152 #define elf_backend_final_write_processing v850_elf_final_write_processing
4153 #define elf_backend_section_from_bfd_section v850_elf_section_from_bfd_section
4154 #define elf_backend_symbol_processing v850_elf_symbol_processing
4155 #define elf_backend_add_symbol_hook v850_elf_add_symbol_hook
4156 #define elf_backend_link_output_symbol_hook v850_elf_link_output_symbol_hook
4157 #define elf_backend_section_from_shdr v850_elf_section_from_shdr
4158 #define elf_backend_fake_sections v850_elf_fake_sections
4159 #define elf_backend_gc_mark_hook v850_elf_gc_mark_hook
4160 #define elf_backend_special_sections v850_elf_special_sections
4161
4162 #define elf_backend_can_gc_sections 1
4163 #define elf_backend_rela_normal 1
4164
4165 #define bfd_elf32_bfd_is_local_label_name v850_elf_is_local_label_name
4166 #define bfd_elf32_bfd_is_target_special_symbol v850_elf_is_target_special_symbol
4167
4168 #define bfd_elf32_bfd_reloc_type_lookup v850_elf_reloc_type_lookup
4169 #define bfd_elf32_bfd_reloc_name_lookup v850_elf_reloc_name_lookup
4170 #define bfd_elf32_bfd_merge_private_bfd_data v850_elf_merge_private_bfd_data
4171 #define bfd_elf32_bfd_set_private_flags v850_elf_set_private_flags
4172 #define bfd_elf32_bfd_print_private_bfd_data v850_elf_print_private_bfd_data
4173 #define bfd_elf32_bfd_relax_section v850_elf_relax_section
4174
4175 #define elf_symbol_leading_char '_'
4176
4177 #undef elf32_bed
4178 #define elf32_bed elf32_v850_bed
4179
4180 #include "elf32-target.h"
4181
4182 /* Map BFD reloc types to V800 ELF reloc types. */
4183
4184 static const struct v850_elf_reloc_map v800_elf_reloc_map[] =
4185 {
4186 { BFD_RELOC_NONE, R_V810_NONE },
4187 { BFD_RELOC_8, R_V810_BYTE },
4188 { BFD_RELOC_16, R_V810_HWORD },
4189 { BFD_RELOC_32, R_V810_WORD },
4190 { BFD_RELOC_LO16, R_V810_WLO },
4191 { BFD_RELOC_HI16, R_V810_WHI },
4192 { BFD_RELOC_HI16_S, R_V810_WHI1 },
4193 { BFD_RELOC_V850_32_PCREL, R_V850_PC32 },
4194 { BFD_RELOC_V850_22_PCREL, R_V850_PCR22 },
4195 { BFD_RELOC_V850_17_PCREL, R_V850_PC17 },
4196 { BFD_RELOC_V850_16_PCREL, R_V850_PC16U },
4197 { BFD_RELOC_V850_9_PCREL, R_V850_PC9 },
4198 { BFD_RELOC_V850_LO16_S1, R_V810_WLO_1 }, /* Or R_V850_HWLO or R_V850_HWLO_1. */
4199 { BFD_RELOC_V850_23, R_V850_WLO23 },
4200 { BFD_RELOC_V850_LO16_SPLIT_OFFSET, R_V850_BLO },
4201 { BFD_RELOC_V850_ZDA_16_16_OFFSET, R_V810_HWORD },
4202 { BFD_RELOC_V850_TDA_16_16_OFFSET, R_V810_HWORD },
4203 { BFD_RELOC_V850_SDA_16_16_OFFSET, R_V810_HWORD },
4204 { BFD_RELOC_V850_SDA_15_16_OFFSET, R_V810_GPWLO_1 }
4205 };
4206
4207 /* Map a bfd relocation into the appropriate howto structure. */
4208
4209 static reloc_howto_type *
4210 v800_elf_reloc_type_lookup (bfd * abfd, bfd_reloc_code_real_type code)
4211 {
4212 unsigned int i;
4213
4214 BFD_ASSERT (bfd_get_arch (abfd) == bfd_arch_v850_rh850);
4215
4216 for (i = ARRAY_SIZE (v800_elf_reloc_map); i --;)
4217 if (v800_elf_reloc_map[i].bfd_reloc_val == code)
4218 {
4219 unsigned int elf_reloc_val = v800_elf_reloc_map[i].elf_reloc_val;
4220 unsigned int idx = elf_reloc_val - R_V810_NONE;
4221
4222 BFD_ASSERT (v800_elf_howto_table[idx].type == elf_reloc_val);
4223
4224 return v800_elf_howto_table + idx;
4225 }
4226
4227 #ifdef DEBUG
4228 fprintf (stderr, "failed to find v800 equiv of bfd reloc code %d\n", code);
4229 #endif
4230 return NULL;
4231 }
4232
4233 static reloc_howto_type *
4234 v800_elf_reloc_name_lookup (bfd * abfd, const char * r_name)
4235 {
4236 unsigned int i;
4237
4238 BFD_ASSERT (bfd_get_arch (abfd) == bfd_arch_v850_rh850);
4239
4240 for (i = ARRAY_SIZE (v800_elf_howto_table); i--;)
4241 if (v800_elf_howto_table[i].name != NULL
4242 && strcasecmp (v800_elf_howto_table[i].name, r_name) == 0)
4243 return v800_elf_howto_table + i;
4244
4245 return NULL;
4246 }
4247
4248
4249 /* Set the howto pointer in CACHE_PTR for a V800 ELF reloc. */
4250
4251 static bfd_boolean
4252 v800_elf_info_to_howto (bfd * abfd,
4253 arelent * cache_ptr,
4254 Elf_Internal_Rela * dst)
4255 {
4256 unsigned int r_type = ELF32_R_TYPE (dst->r_info);
4257
4258 if (r_type == R_V800_NONE)
4259 r_type = R_V810_NONE;
4260
4261 if (bfd_get_arch (abfd) != bfd_arch_v850_rh850
4262 || r_type >= (unsigned int) R_V800_max
4263 || r_type < (unsigned int) R_V810_NONE
4264 || (r_type - R_V810_NONE) >= ARRAY_SIZE (v800_elf_howto_table))
4265 {
4266 /* xgettext:c-format */
4267 _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
4268 abfd, r_type);
4269 bfd_set_error (bfd_error_bad_value);
4270 return FALSE;
4271 }
4272
4273 cache_ptr->howto = v800_elf_howto_table + (r_type - R_V810_NONE);
4274 return TRUE;
4275 }
4276 \f
4277 #undef TARGET_LITTLE_SYM
4278 #define TARGET_LITTLE_SYM v800_elf32_vec
4279 #undef TARGET_LITTLE_NAME
4280 #define TARGET_LITTLE_NAME "elf32-v850-rh850"
4281 #undef ELF_ARCH
4282 #define ELF_ARCH bfd_arch_v850_rh850
4283 #undef ELF_MACHINE_CODE
4284 #define ELF_MACHINE_CODE EM_V800
4285 #undef ELF_MACHINE_ALT1
4286
4287 #undef elf32_bed
4288 #define elf32_bed elf32_v850_rh850_bed
4289
4290 #undef elf_info_to_howto
4291 #define elf_info_to_howto v800_elf_info_to_howto
4292 #undef elf_info_to_howto_rel
4293 #define elf_info_to_howto_rel NULL
4294 #undef bfd_elf32_bfd_reloc_type_lookup
4295 #define bfd_elf32_bfd_reloc_type_lookup v800_elf_reloc_type_lookup
4296 #undef bfd_elf32_bfd_reloc_name_lookup
4297 #define bfd_elf32_bfd_reloc_name_lookup v800_elf_reloc_name_lookup
4298
4299 #include "elf32-target.h"