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