Put common variables into the appropriate section, based on relocation types
[external/binutils.git] / bfd / elf32-v850.c
1 /* V850-specific support for 32-bit ELF
2    Copyright (C) 1996, 1997 Free Software Foundation, Inc.
3
4 This file is part of BFD, the Binary File Descriptor library.
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
19
20
21
22 /* XXX FIXME: This code is littered with 32bit int, 16bit short, 8bit char
23    dependencies.  As is the gas & simulator code or the v850.  */
24
25
26 #include "bfd.h"
27 #include "sysdep.h"
28 #include "bfdlink.h"
29 #include "libbfd.h"
30 #include "elf-bfd.h"
31 #include "elf/v850.h"
32
33 static reloc_howto_type *v850_elf_reloc_type_lookup
34   PARAMS ((bfd *abfd, bfd_reloc_code_real_type code));
35 static void v850_elf_info_to_howto_rel
36   PARAMS ((bfd *, arelent *, Elf32_Internal_Rel *));
37 static bfd_reloc_status_type v850_elf_reloc
38   PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
39 static boolean v850_elf_is_local_label PARAMS ((bfd *, asymbol *));
40 static boolean v850_elf_relocate_section PARAMS((bfd *,
41                                                  struct bfd_link_info *,
42                                                  bfd *,
43                                                  asection *,
44                                                  bfd_byte *,
45                                                  Elf_Internal_Rela *,
46                                                  Elf_Internal_Sym *,
47                                                  asection **));
48
49 /* Try to minimize the amount of space occupied by relocation tables
50    on the ROM (not that the ROM won't be swamped by other ELF overhead).  */
51 #define USE_REL
52
53 static reloc_howto_type v850_elf_howto_table[] =
54 {
55   /* This reloc does nothing.  */
56   HOWTO (R_V850_NONE,                   /* type */
57          0,                             /* rightshift */
58          2,                             /* size (0 = byte, 1 = short, 2 = long) */
59          32,                            /* bitsize */
60          false,                         /* pc_relative */
61          0,                             /* bitpos */
62          complain_overflow_bitfield,    /* complain_on_overflow */
63          bfd_elf_generic_reloc,         /* special_function */
64          "R_V850_NONE",                 /* name */
65          false,                         /* partial_inplace */
66          0,                             /* src_mask */
67          0,                             /* dst_mask */
68          false),                        /* pcrel_offset */
69
70   /* A PC relative 9 bit branch. */
71   HOWTO (R_V850_9_PCREL,                /* type */
72          2,                             /* rightshift */
73          2,                             /* size (0 = byte, 1 = short, 2 = long) */
74          26,                            /* bitsize */
75          true,                          /* pc_relative */
76          0,                             /* bitpos */
77          complain_overflow_bitfield,    /* complain_on_overflow */
78          v850_elf_reloc,                /* special_function */
79          "R_V850_9_PCREL",              /* name */
80          false,                         /* partial_inplace */
81          0x00ffffff,                    /* src_mask */
82          0x00ffffff,                    /* dst_mask */
83          true),                         /* pcrel_offset */
84
85   /* A PC relative 22 bit branch. */
86   HOWTO (R_V850_22_PCREL,               /* type */
87          2,                             /* rightshift */
88          2,                             /* size (0 = byte, 1 = short, 2 = long) */
89          22,                            /* bitsize */
90          true,                          /* pc_relative */
91          7,                             /* bitpos */
92          complain_overflow_signed,      /* complain_on_overflow */
93          v850_elf_reloc,                /* special_function */
94          "R_V850_22_PCREL",             /* name */
95          false,                         /* partial_inplace */
96          0x07ffff80,                    /* src_mask */
97          0x07ffff80,                    /* dst_mask */
98          true),                         /* pcrel_offset */
99
100   /* High 16 bits of symbol value.  */
101   HOWTO (R_V850_HI16_S,                 /* type */
102          0,                             /* rightshift */
103          1,                             /* size (0 = byte, 1 = short, 2 = long) */
104          16,                            /* bitsize */
105          false,                         /* pc_relative */
106          0,                             /* bitpos */
107          complain_overflow_dont,        /* complain_on_overflow */
108          v850_elf_reloc,                /* special_function */
109          "R_V850_HI16_S",               /* name */
110          true,                          /* partial_inplace */
111          0xffff,                        /* src_mask */
112          0xffff,                        /* dst_mask */
113          false),                        /* pcrel_offset */
114
115   /* High 16 bits of symbol value.  */
116   HOWTO (R_V850_HI16,                   /* type */
117          0,                             /* rightshift */
118          1,                             /* size (0 = byte, 1 = short, 2 = long) */
119          16,                            /* bitsize */
120          false,                         /* pc_relative */
121          0,                             /* bitpos */
122          complain_overflow_dont,        /* complain_on_overflow */
123          v850_elf_reloc,                /* special_function */
124          "R_V850_HI16",                 /* name */
125          true,                          /* partial_inplace */
126          0xffff,                        /* src_mask */
127          0xffff,                        /* dst_mask */
128          false),                        /* pcrel_offset */
129
130   /* Low 16 bits of symbol value.  */
131   HOWTO (R_V850_LO16,                   /* type */
132          0,                             /* rightshift */
133          1,                             /* size (0 = byte, 1 = short, 2 = long) */
134          16,                            /* bitsize */
135          false,                         /* pc_relative */
136          0,                             /* bitpos */
137          complain_overflow_dont,        /* complain_on_overflow */
138          bfd_elf_generic_reloc,         /* special_function */
139          "R_V850_LO16",                 /* name */
140          true,                          /* partial_inplace */
141          0xffff,                        /* src_mask */
142          0xffff,                        /* dst_mask */
143          false),                        /* pcrel_offset */
144
145   /* Simple 32bit reloc.  */
146   HOWTO (R_V850_32,                     /* type */
147          0,                             /* rightshift */
148          2,                             /* size (0 = byte, 1 = short, 2 = long) */
149          32,                            /* bitsize */
150          false,                         /* pc_relative */
151          0,                             /* bitpos */
152          complain_overflow_dont,        /* complain_on_overflow */
153          bfd_elf_generic_reloc,         /* special_function */
154          "R_V850_32",                   /* name */
155          true,                          /* partial_inplace */
156          0xffffffff,                    /* src_mask */
157          0xffffffff,                    /* dst_mask */
158          false),                        /* pcrel_offset */
159
160   /* Simple 16bit reloc.  */
161   HOWTO (R_V850_16,                     /* type */
162          0,                             /* rightshift */
163          1,                             /* size (0 = byte, 1 = short, 2 = long) */
164          16,                            /* bitsize */
165          false,                         /* pc_relative */
166          0,                             /* bitpos */
167          complain_overflow_dont,        /* complain_on_overflow */
168          bfd_elf_generic_reloc,         /* special_function */
169          "R_V850_16",                   /* name */
170          true,                          /* partial_inplace */
171          0xffff,                        /* src_mask */
172          0xffff,                        /* dst_mask */
173          false),                        /* pcrel_offset */
174
175   /* Simple 8bit reloc.  */
176   HOWTO (R_V850_8,                      /* type */
177          0,                             /* rightshift */
178          0,                             /* size (0 = byte, 1 = short, 2 = long) */
179          8,                             /* bitsize */
180          false,                         /* pc_relative */
181          0,                             /* bitpos */
182          complain_overflow_dont,        /* complain_on_overflow */
183          bfd_elf_generic_reloc,         /* special_function */
184          "R_V850_8",                    /* name */
185          true,                          /* partial_inplace */
186          0xff,                          /* src_mask */
187          0xff,                          /* dst_mask */
188          false),                        /* pcrel_offset */
189
190   /* Offset from the short data area pointer.  */
191   HOWTO (R_V850_SDA_OFFSET,             /* type */
192          0,                             /* rightshift */
193          1,                             /* size (0 = byte, 1 = short, 2 = long) */
194          16,                            /* bitsize */
195          false,                         /* pc_relative */
196          0,                             /* bitpos */
197          complain_overflow_dont,        /* complain_on_overflow */
198          bfd_elf_generic_reloc,         /* special_function */
199          "R_V850_SDA_OFFSET",           /* name */
200          true,                          /* partial_inplace */
201          0xffff,                        /* src_mask */
202          0xffff,                        /* dst_mask */
203          false),                        /* pcrel_offset */
204
205   /* Offset from the zero data area pointer.  */
206   HOWTO (R_V850_ZDA_OFFSET,             /* type */
207          0,                             /* rightshift */
208          1,                             /* size (0 = byte, 1 = short, 2 = long) */
209          16,                            /* bitsize */
210          false,                         /* pc_relative */
211          0,                             /* bitpos */
212          complain_overflow_dont,        /* complain_on_overflow */
213          bfd_elf_generic_reloc,         /* special_function */
214          "R_V850_ZDA_OFFSET",           /* name */
215          true,                          /* partial_inplace */
216          0xffff,                        /* src_mask */
217          0xffff,                        /* dst_mask */
218          false),                        /* pcrel_offset */
219
220   /* Offset from the tiny data area pointer.  */
221   HOWTO (R_V850_TDA_OFFSET,             /* type */
222          0,                             /* rightshift */
223          2,                             /* size (0 = byte, 1 = short, 2 = long) */
224          8,                             /* bitsize */
225          false,                         /* pc_relative */
226          0,                             /* bitpos */
227          complain_overflow_dont,        /* complain_on_overflow */
228          bfd_elf_generic_reloc,         /* special_function */
229          "R_V850_TDA_OFFSET",           /* name */
230          true,                          /* partial_inplace */
231          0xff,                          /* src_mask */
232          0xff,                          /* dst_mask */
233          false),                        /* pcrel_offset */
234
235 };
236
237 /* Map BFD reloc types to V850 ELF reloc types.  */
238
239 struct v850_elf_reloc_map
240 {
241   unsigned char bfd_reloc_val;
242   unsigned char elf_reloc_val;
243 };
244
245 static const struct v850_elf_reloc_map v850_elf_reloc_map[] =
246 {
247   { BFD_RELOC_NONE,             R_V850_NONE, },
248   { BFD_RELOC_V850_9_PCREL,     R_V850_9_PCREL, },
249   { BFD_RELOC_V850_22_PCREL,    R_V850_22_PCREL, },
250   { BFD_RELOC_HI16_S,           R_V850_HI16_S, },
251   { BFD_RELOC_HI16,             R_V850_HI16, },
252   { BFD_RELOC_LO16,             R_V850_LO16, },
253   { BFD_RELOC_32,               R_V850_32, },
254   { BFD_RELOC_16,               R_V850_16, },
255   { BFD_RELOC_8,                R_V850_8, },
256   { BFD_RELOC_V850_TDA_OFFSET,  R_V850_TDA_OFFSET, },
257   { BFD_RELOC_V850_SDA_OFFSET,  R_V850_SDA_OFFSET, },
258   { BFD_RELOC_V850_ZDA_OFFSET,  R_V850_ZDA_OFFSET, },
259 };
260
261 \f
262 /* Map a bfd relocation into the appropriate howto structure */
263 static reloc_howto_type *
264 v850_elf_reloc_type_lookup (abfd, code)
265      bfd *abfd;
266      bfd_reloc_code_real_type code;
267 {
268   unsigned int i;
269
270   for (i = 0;
271        i < sizeof (v850_elf_reloc_map) / sizeof (struct v850_elf_reloc_map);
272        i++)
273     {
274       if (v850_elf_reloc_map[i].bfd_reloc_val == code)
275         return &v850_elf_howto_table[v850_elf_reloc_map[i].elf_reloc_val];
276     }
277
278   return NULL;
279 }
280
281 \f
282 /* Set the howto pointer for an V850 ELF reloc.  */
283 static void
284 v850_elf_info_to_howto_rel (abfd, cache_ptr, dst)
285      bfd *abfd;
286      arelent *cache_ptr;
287      Elf32_Internal_Rel *dst;
288 {
289   unsigned int r_type;
290
291   r_type = ELF32_R_TYPE (dst->r_info);
292   BFD_ASSERT (r_type < (unsigned int) R_V850_max);
293   cache_ptr->howto = &v850_elf_howto_table[r_type];
294 }
295
296 \f
297 /* Look through the relocs for a section during the first phase, and
298    allocate space in the global offset table or procedure linkage
299    table.  */
300
301 static boolean
302 v850_elf_check_relocs (abfd, info, sec, relocs)
303      bfd *abfd;
304      struct bfd_link_info *info;
305      asection *sec;
306      const Elf_Internal_Rela *relocs;
307 {
308   boolean ret = true;
309   bfd *dynobj;
310   Elf_Internal_Shdr *symtab_hdr;
311   struct elf_link_hash_entry **sym_hashes;
312   const Elf_Internal_Rela *rel;
313   const Elf_Internal_Rela *rel_end;
314   asection *sreloc;
315   enum reloc_type r_type;
316   int other = 0;
317   const char *common = (const char *)0;
318
319   if (info->relocateable)
320     return true;
321
322 #ifdef DEBUG
323   fprintf (stderr, "v850_elf_check_relocs called for section %s in %s\n",
324            bfd_get_section_name (abfd, sec),
325            bfd_get_filename (abfd));
326 #endif
327
328   dynobj = elf_hash_table (info)->dynobj;
329   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
330   sym_hashes = elf_sym_hashes (abfd);
331   sreloc = NULL;
332
333   rel_end = relocs + sec->reloc_count;
334   for (rel = relocs; rel < rel_end; rel++)
335     {
336       unsigned long r_symndx;
337       struct elf_link_hash_entry *h;
338
339       r_symndx = ELF32_R_SYM (rel->r_info);
340       if (r_symndx < symtab_hdr->sh_info)
341         h = NULL;
342       else
343         h = sym_hashes[r_symndx - symtab_hdr->sh_info];
344
345       r_type = (enum reloc_type) ELF32_R_TYPE (rel->r_info);
346       switch (r_type)
347         {
348         default:
349         case R_V850_NONE:
350         case R_V850_9_PCREL:
351         case R_V850_22_PCREL:
352         case R_V850_HI16_S:
353         case R_V850_HI16:
354         case R_V850_LO16:
355         case R_V850_32:
356         case R_V850_16:
357         case R_V850_8:
358           break;
359
360         case R_V850_SDA_OFFSET:
361           other = V850_OTHER_SDA;
362           common = ".scommon";
363           goto small_data_common;
364
365         case R_V850_ZDA_OFFSET:
366           other = V850_OTHER_ZDA;
367           common = ".zcommon";
368           goto small_data_common;
369
370         case R_V850_TDA_OFFSET:
371           other = V850_OTHER_TDA;
372           common = ".tcommon";
373           /* fall through */
374
375 #define V850_OTHER_MASK (V850_OTHER_TDA | V850_OTHER_SDA | V850_OTHER_ZDA)
376
377         small_data_common:
378           if (h)
379             {
380               h->other |= other;        /* flag which type of relocation was used */
381               if ((h->other & V850_OTHER_MASK) != (other & V850_OTHER_MASK)
382                   && (h->other & V850_OTHER_ERROR) == 0)
383                 {
384                   const char *msg;
385
386                   switch (h->other & V850_OTHER_MASK)
387                     {
388                     default:
389                       msg = "Variable cannot occupy in multiple small data regions";
390                       break;
391                     case V850_OTHER_SDA | V850_OTHER_ZDA | V850_OTHER_TDA:
392                       msg = "Variable can only be in one of the small, zero, and tiny data regions";
393                       break;
394                     case V850_OTHER_SDA | V850_OTHER_ZDA:
395                       msg = "Variable cannot be in both small and zero data regions simultaneously";
396                       break;
397                     case V850_OTHER_SDA | V850_OTHER_TDA:
398                       msg = "Variable cannot be in both small and tiny data regions simultaneously";
399                       break;
400                     case V850_OTHER_ZDA | V850_OTHER_TDA:
401                       msg = "Variable cannot be in both zero and tiny data regions simultaneously";
402                       break;
403                     }
404
405                   (*info->callbacks->warning) (info, msg, h->root.root.string,
406                                                abfd, h->root.u.def.section, 0);
407
408                   bfd_set_error (bfd_error_bad_value);
409                   h->other |= V850_OTHER_ERROR;
410                   ret = false;
411                 }
412             }
413
414           if (h->root.type == bfd_link_hash_common
415               && h->root.u.c.p
416               && !strcmp (bfd_get_section_name (abfd, h->root.u.c.p->section), "COMMON"))
417             {
418               asection *section = h->root.u.c.p->section = bfd_make_section_old_way (abfd, common);
419               section->flags |= SEC_IS_COMMON;
420             }
421
422 #ifdef DEBUG
423           fprintf (stderr, "v850_elf_check_relocs, found %s relocation for %s%s\n",
424                    v850_elf_howto_table[ (int)r_type ].name,
425                    (h && h->root.root.string) ? h->root.root.string : "<unknown>",
426                    (h->root.type == bfd_link_hash_common) ? ", symbol is common" : "");
427 #endif
428           break;
429         }
430     }
431
432   return ret;
433 }
434
435 \f
436 static bfd_reloc_status_type
437 v850_elf_reloc (abfd, reloc, symbol, data, isection, obfd, err)
438      bfd *abfd;
439      arelent *reloc;
440      asymbol *symbol;
441      PTR data;
442      asection *isection;
443      bfd *obfd;
444      char **err;
445 {
446   if (obfd != (bfd *) NULL
447       && (symbol->flags & BSF_SECTION_SYM) == 0
448       && (! reloc->howto->partial_inplace
449           || reloc->addend == 0))
450     {
451       reloc->address += isection->output_offset;
452       return bfd_reloc_ok;
453     }
454   else if (obfd != NULL)
455     {
456       return bfd_reloc_continue;
457     }
458
459   /* Catch relocs involving undefined symbols.  */
460   if (bfd_is_und_section (symbol->section)
461       && (symbol->flags & BSF_WEAK) == 0
462       && obfd == NULL)
463     return bfd_reloc_undefined;
464
465   /* We handle final linking of some relocs ourselves.  */
466     {
467       long relocation, insn;
468
469       /* Is the address of the relocation really within the section?  */
470       if (reloc->address > isection->_cooked_size)
471         return bfd_reloc_outofrange;
472
473       /* Work out which section the relocation is targetted at and the
474          initial relocation command value.  */
475
476       /* Get symbol value.  (Common symbols are special.)  */
477       if (bfd_is_com_section (symbol->section))
478         relocation = 0;
479       else
480         relocation = symbol->value;
481
482       /* Convert input-section-relative symbol value to absolute + addend.  */
483       relocation += symbol->section->output_section->vma;
484       relocation += symbol->section->output_offset;
485       relocation += reloc->addend;
486
487       if (reloc->howto->pc_relative == true)
488         {
489           /* Here the variable relocation holds the final address of the
490              symbol we are relocating against, plus any addend.  */
491           relocation -= isection->output_section->vma + isection->output_offset;
492
493           /* Deal with pcrel_offset */
494           relocation -= reloc->address;
495         }
496
497       /* I've got no clue... */
498       reloc->addend = 0;        
499
500       if (reloc->howto->type == R_V850_22_PCREL)
501         {
502           if (relocation > 0x1ffff || relocation < -0x200000)
503             return bfd_reloc_overflow;
504
505           if ((relocation % 2) != 0)
506             return bfd_reloc_dangerous;
507
508           insn = bfd_get_32 (abfd, (bfd_byte *) data + reloc->address);
509           insn &= ~0xfffe003f;
510           insn |= (((relocation & 0xfffe) << 16)
511                    | ((relocation & 0x3f0000) >> 16));
512           bfd_put_32 (abfd, insn, (bfd_byte *)data + reloc->address);
513           return bfd_reloc_ok;
514         }
515       else if (reloc->howto->type == R_V850_9_PCREL)
516         {
517           if (relocation > 0xff || relocation < -0x100)
518             return bfd_reloc_overflow;
519
520           if ((relocation % 2) != 0)
521             return bfd_reloc_dangerous;
522
523           insn = bfd_get_16 (abfd, (bfd_byte *) data + reloc->address);
524           insn &= 0xf870;
525           insn |= ((relocation & 0x1f0) << 7) | ((relocation & 0x0e) << 3);
526           bfd_put_16 (abfd, insn, (bfd_byte *)data + reloc->address);
527           return bfd_reloc_ok;
528         }
529       else if (reloc->howto->type == R_V850_HI16_S)
530         {
531           relocation += bfd_get_16 (abfd, (bfd_byte *) data + reloc->address);
532           relocation = (relocation >> 16) + ((relocation & 0x8000) != 0);
533           bfd_put_16 (abfd, relocation, (bfd_byte *)data + reloc->address);
534           return bfd_reloc_ok;
535         }
536       else if (reloc->howto->type == R_V850_HI16)
537         {
538           relocation += bfd_get_16 (abfd, (bfd_byte *) data + reloc->address);
539           relocation = (relocation >> 16);
540           bfd_put_16 (abfd, relocation, (bfd_byte *)data + reloc->address);
541           return bfd_reloc_ok;
542         }
543       else
544         return bfd_reloc_notsupported;
545     }
546
547   return bfd_reloc_continue;
548 }
549
550 \f
551 /*ARGSUSED*/
552 static boolean
553 v850_elf_is_local_label (abfd, symbol)
554      bfd *abfd;
555      asymbol *symbol;
556 {
557   return ((symbol->name[0] == '.' && (symbol->name[1] == 'L' || symbol->name[1] == '.'))
558           || (symbol->name[0] == '_' && symbol->name[1] == '.' && symbol->name[2] == 'L'
559               && symbol->name[3] == '_'));
560 }
561
562 \f
563 /* Perform a relocation as part of a final link.  */
564 static bfd_reloc_status_type
565 v850_elf_final_link_relocate (howto, input_bfd, output_bfd,
566                                     input_section, contents, offset, value,
567                                     addend, info, sym_sec, is_local)
568      reloc_howto_type *howto;
569      bfd *input_bfd;
570      bfd *output_bfd;
571      asection *input_section;
572      bfd_byte *contents;
573      bfd_vma offset;
574      bfd_vma value;
575      bfd_vma addend;
576      struct bfd_link_info *info;
577      asection *sym_sec;
578      int is_local;
579 {
580   unsigned long insn;
581   unsigned long r_type = howto->type;
582   unsigned long r_format = howto->bitsize;
583   bfd_byte *hit_data = contents + offset;
584   boolean r_pcrel = howto->pc_relative;
585
586   switch (r_type)
587     {
588     case R_V850_9_PCREL:
589       value -= (input_section->output_section->vma
590                 + input_section->output_offset);
591       value -= offset;
592
593       if ((long)value > 0xff || (long)value < -0x100)
594         return bfd_reloc_overflow;
595
596       if ((value % 2) != 0)
597         return bfd_reloc_dangerous;
598
599       insn = bfd_get_16 (input_bfd, hit_data);
600       insn &= 0x078f;
601       insn |= ((value & 0x1f0) << 7) | ((value & 0x0e) << 3);
602       bfd_put_16 (input_bfd, insn, hit_data);
603       return bfd_reloc_ok;
604     
605     case R_V850_22_PCREL:
606       value -= (input_section->output_section->vma
607                 + input_section->output_offset);
608       value -= offset;
609     
610       if ((long)value > 0x1ffff || (long)value < -0x200000)
611         return bfd_reloc_overflow;
612
613       if ((value % 2) != 0)
614         return bfd_reloc_dangerous;
615
616       insn = bfd_get_32 (input_bfd, hit_data);
617       insn &= 0x1ffc0;
618       insn |= (((value & 0xfffe) << 16) | ((value & 0x3f0000) >> 16));
619       bfd_put_32 (input_bfd, insn, hit_data);
620       return bfd_reloc_ok;
621       
622     case R_V850_HI16_S:
623       value += (short)bfd_get_16 (input_bfd, hit_data);
624       value = (value >> 16) + ((value & 0x8000) != 0);
625
626       if ((long)value > 0x7fff || (long)value < -0x8000)
627         return bfd_reloc_overflow;
628
629       bfd_put_16 (input_bfd, value, hit_data);
630       return bfd_reloc_ok;
631
632     case R_V850_HI16:
633       value += (short)bfd_get_16 (input_bfd, hit_data);
634       value >>= 16;
635
636       if ((long)value > 0x7fff || (long)value < -0x8000)
637         return bfd_reloc_overflow;
638
639       bfd_put_16 (input_bfd, value, hit_data);
640       return bfd_reloc_ok;
641
642     case R_V850_LO16:
643       value += (short)bfd_get_16 (input_bfd, hit_data);
644       value &= 0xffff;
645
646       bfd_put_16 (input_bfd, value, hit_data);
647       return bfd_reloc_ok;
648
649     case R_V850_16:
650     case R_V850_ZDA_OFFSET:
651       value += (short)bfd_get_16 (input_bfd, hit_data);
652
653       if ((long)value > 0x7fff || (long)value < -0x8000)
654         return bfd_reloc_overflow;
655
656       bfd_put_16 (input_bfd, value, hit_data);
657       return bfd_reloc_ok;
658
659     case R_V850_32:
660       value += bfd_get_32 (input_bfd, hit_data);
661       bfd_put_32 (input_bfd, value, hit_data);
662       return bfd_reloc_ok;
663
664     case R_V850_8:
665       value += (char)bfd_get_8 (input_bfd, hit_data);
666
667       if ((long)value > 0x7f || (long)value < -0x80)
668         return bfd_reloc_overflow;
669
670       bfd_put_8 (input_bfd, value, hit_data);
671       return bfd_reloc_ok;
672
673     case R_V850_SDA_OFFSET:
674       {
675         unsigned long gp;
676         struct bfd_link_hash_entry *h;
677
678         value += (short)bfd_get_16 (input_bfd, hit_data);
679
680         /* Get the value of __gp.  */
681         h = bfd_link_hash_lookup (info->hash, "__gp", false,
682                                   false, true);
683         if (h == (struct bfd_link_hash_entry *) NULL
684             || h->type != bfd_link_hash_defined)
685           return bfd_reloc_undefined;
686
687         gp = (h->u.def.value
688               + h->u.def.section->output_section->vma
689               + h->u.def.section->output_offset);
690         value -= gp;
691
692         if ((long)value > 0x7fff || (long)value < -0x8000)
693           return bfd_reloc_overflow;
694
695         bfd_put_16 (input_bfd, value, hit_data);
696         return bfd_reloc_ok;
697       }
698
699     case R_V850_TDA_OFFSET:
700       {
701         unsigned long ep;
702         struct bfd_link_hash_entry *h;
703
704         insn = bfd_get_16 (input_bfd, hit_data);
705
706         /* Get the value of __ep.  */
707         h = bfd_link_hash_lookup (info->hash, "__ep", false,
708                                   false, true);
709         if (h == (struct bfd_link_hash_entry *) NULL
710             || h->type != bfd_link_hash_defined)
711           return bfd_reloc_undefined;
712
713         ep = (h->u.def.value
714               + h->u.def.section->output_section->vma
715               + h->u.def.section->output_offset);
716         value -= ep;
717
718
719         /* Overflow computation and operand insertion is complicated
720            by valid offsets and insertions changing depending on the
721            instruction being used!  */
722         if ((insn & 0x0780) == 0x0500)
723           {
724             value += ((insn & 0x7f) << 1);
725
726             /* Handle sld.w and sst.w -- 8 bit unsigned offset */
727             if ((long) value > 0xff || (long) value < 0)
728               return bfd_reloc_overflow;
729
730             if ((value % 2) != 0)
731               return bfd_reloc_dangerous;
732
733             insn &= 0xff81;
734             insn |= (value >> 1);
735             bfd_put_16 (input_bfd, insn, hit_data);
736             return bfd_reloc_ok;
737           }
738
739         if ((insn & 0x0780) == 0x0400 || (insn & 0x0780) == 0x0480)
740           {
741             value += ((insn & 0x7f) << 1);
742
743             /* Handle sld.h and sst.h -- 8 bit unsigned offset */
744             if ((long) value > 0xff || (long) value < 0)
745               return bfd_reloc_overflow;
746
747             if ((value % 2) != 0)
748               return bfd_reloc_dangerous;
749
750             insn &= 0xff80;
751             insn |= (value >> 1);
752             bfd_put_16 (input_bfd, insn, hit_data);
753             return bfd_reloc_ok;
754           }
755
756         if ((insn & 0x0780) == 0x0300 || (insn & 0x0780) == 0x0380)
757           {
758             value += (insn & 0x7f);
759
760             /* Handle sld.b and sst.b -- 7 bit unsigned offset */
761             if ((long) value > 0x7f || (long) value < 0)
762               return bfd_reloc_overflow;
763             insn &= 0xff80;
764             insn |= value;
765             bfd_put_16 (input_bfd, insn, hit_data);
766             return bfd_reloc_ok;
767           }
768
769         /* Guess (XXX) that it's a movea instruction or something
770            similar.  */
771         value += (short)insn;
772         if ((long)value > 0x7fff || (long)value < -0x8000)
773           return bfd_reloc_overflow;
774
775         bfd_put_16 (input_bfd, value, hit_data);
776         return bfd_reloc_ok;
777       }
778     
779     case R_V850_NONE:
780     default:
781       break;
782     }
783
784 }
785
786 \f
787 /* Relocate an V850 ELF section.  */
788 static boolean
789 v850_elf_relocate_section (output_bfd, info, input_bfd, input_section,
790                            contents, relocs, local_syms, local_sections)
791      bfd *output_bfd;
792      struct bfd_link_info *info;
793      bfd *input_bfd;
794      asection *input_section;
795      bfd_byte *contents;
796      Elf_Internal_Rela *relocs;
797      Elf_Internal_Sym *local_syms;
798      asection **local_sections;
799 {
800   Elf_Internal_Shdr *symtab_hdr;
801   struct elf_link_hash_entry **sym_hashes;
802   Elf_Internal_Rela *rel, *relend;
803
804   symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
805   sym_hashes = elf_sym_hashes (input_bfd);
806
807   rel = relocs;
808   relend = relocs + input_section->reloc_count;
809   for (; rel < relend; rel++)
810     {
811       int r_type;
812       reloc_howto_type *howto;
813       unsigned long r_symndx;
814       Elf_Internal_Sym *sym;
815       asection *sec;
816       struct elf_link_hash_entry *h;
817       bfd_vma relocation;
818       bfd_reloc_status_type r;
819
820       if (info->relocateable)
821         {
822           /* This is a relocateable link.  We don't have to change
823              anything, unless the reloc is against a section symbol,
824              in which case we have to adjust according to where the
825              section symbol winds up in the output section.  */
826           if (r_symndx < symtab_hdr->sh_info)
827             {
828               sym = local_syms + r_symndx;
829               if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
830                 {
831                   sec = local_sections[r_symndx];
832                   rel->r_addend += sec->output_offset + sym->st_value;
833                 }
834             }
835
836           continue;
837         }
838
839       r_type = ELF32_R_TYPE (rel->r_info);
840
841       howto = v850_elf_howto_table + r_type;
842
843       r_symndx = ELF32_R_SYM (rel->r_info);
844
845       /* This is a final link.  */
846       h = NULL;
847       sym = NULL;
848       sec = NULL;
849       if (r_symndx < symtab_hdr->sh_info)
850         {
851           sym = local_syms + r_symndx;
852           sec = local_sections[r_symndx];
853           relocation = (sec->output_section->vma
854                         + sec->output_offset
855                         + sym->st_value);
856         }
857       else
858         {
859           h = sym_hashes[r_symndx - symtab_hdr->sh_info];
860           while (h->root.type == bfd_link_hash_indirect
861                  || h->root.type == bfd_link_hash_warning)
862             h = (struct elf_link_hash_entry *) h->root.u.i.link;
863           if (h->root.type == bfd_link_hash_defined
864               || h->root.type == bfd_link_hash_defweak)
865             {
866               sec = h->root.u.def.section;
867               relocation = (h->root.u.def.value
868                             + sec->output_section->vma
869                             + sec->output_offset);
870             }
871           else if (h->root.type == bfd_link_hash_undefweak)
872             relocation = 0;
873           else
874             {
875               if (! ((*info->callbacks->undefined_symbol)
876                      (info, h->root.root.string, input_bfd,
877                       input_section, rel->r_offset)))
878                 return false;
879               relocation = 0;
880             }
881         }
882
883       /* FIXME: We should use the addend, but the COFF relocations
884          don't.  */
885       r = v850_elf_final_link_relocate (howto, input_bfd, output_bfd,
886                                         input_section,
887                                         contents, rel->r_offset,
888                                         relocation, rel->r_addend,
889                                         info, sec, h == NULL);
890
891       if (r != bfd_reloc_ok)
892         {
893           switch (r)
894             {
895             default:
896             case bfd_reloc_outofrange:
897               abort ();
898             case bfd_reloc_overflow:
899               {
900                 const char *name;
901
902                 if (h != NULL)
903                   name = h->root.root.string;
904                 else
905                   {
906                     name = (bfd_elf_string_from_elf_section
907                             (input_bfd, symtab_hdr->sh_link, sym->st_name));
908                     if (name == NULL)
909                       return false;
910                     if (*name == '\0')
911                       name = bfd_section_name (input_bfd, sec);
912                   }
913                 if (! ((*info->callbacks->reloc_overflow)
914                        (info, name, howto->name, (bfd_vma) 0,
915                         input_bfd, input_section, rel->r_offset)))
916                   return false;
917               }
918               break;
919             }
920         }
921     }
922
923   return true;
924 }
925
926 \f
927 #define TARGET_LITTLE_SYM                       bfd_elf32_v850_vec
928 #define TARGET_LITTLE_NAME                      "elf32-v850"
929 #define ELF_ARCH                                bfd_arch_v850
930 #define ELF_MACHINE_CODE                        EM_CYGNUS_V850
931 #define ELF_MAXPAGESIZE                         0x1000
932         
933 #define elf_info_to_howto                       0
934 #define elf_info_to_howto_rel                   v850_elf_info_to_howto_rel
935 #define elf_backend_check_relocs                v850_elf_check_relocs
936 #define elf_backend_relocate_section            v850_elf_relocate_section
937 #define bfd_elf32_bfd_is_local_label            v850_elf_is_local_label
938 #define bfd_elf32_bfd_reloc_type_lookup         v850_elf_reloc_type_lookup
939
940 #define elf_symbol_leading_char                 '_'
941
942 #include "elf32-target.h"