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