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