Add missing prototypes. Fix formatting.
[external/binutils.git] / bfd / elf32-v850.c
1 /* V850-specific support for 32-bit ELF
2    Copyright 1996, 1997, 1998, 1999, 2000, 2001
3    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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
20
21 /* XXX FIXME: This code is littered with 32bit int, 16bit short, 8bit char
22    dependencies.  As is the gas & simulator code or the v850.  */
23
24 #include "bfd.h"
25 #include "sysdep.h"
26 #include "bfdlink.h"
27 #include "libbfd.h"
28 #include "elf-bfd.h"
29 #include "elf/v850.h"
30 #include "libiberty.h"
31
32 /* Sign-extend a 24-bit number.  */
33 #define SEXT24(x)       ((((x) & 0xffffff) ^ (~ 0x7fffff)) + 0x800000)
34
35 static reloc_howto_type *v850_elf_reloc_type_lookup
36   PARAMS ((bfd *abfd, bfd_reloc_code_real_type code));
37 static void v850_elf_info_to_howto_rel
38   PARAMS ((bfd *, arelent *, Elf32_Internal_Rel *));
39 static void v850_elf_info_to_howto_rela
40   PARAMS ((bfd *, arelent *, Elf32_Internal_Rela *));
41 static bfd_reloc_status_type v850_elf_reloc
42   PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
43 static boolean v850_elf_is_local_label_name
44   PARAMS ((bfd *, const char *));
45 static boolean v850_elf_relocate_section
46   PARAMS((bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
47           Elf_Internal_Rela *, Elf_Internal_Sym *, asection **));
48 static bfd_reloc_status_type v850_elf_perform_relocation
49   PARAMS ((bfd *, int, bfd_vma, bfd_byte *));
50 static boolean v850_elf_check_relocs
51   PARAMS ((bfd *, struct bfd_link_info *, asection *, const Elf_Internal_Rela *));
52 static void remember_hi16s_reloc
53   PARAMS ((bfd *, bfd_vma, bfd_byte *));
54 static bfd_byte * find_remembered_hi16s_reloc
55   PARAMS ((bfd_vma, boolean *));
56 static bfd_reloc_status_type v850_elf_final_link_relocate
57   PARAMS ((reloc_howto_type *, bfd *, bfd *, asection *, bfd_byte *, bfd_vma,
58            bfd_vma, bfd_vma, struct bfd_link_info *, asection *, int));
59 static boolean v850_elf_object_p
60   PARAMS ((bfd *));
61 static boolean v850_elf_fake_sections
62   PARAMS ((bfd *, Elf32_Internal_Shdr *, asection *));
63 static void v850_elf_final_write_processing
64   PARAMS ((bfd *, boolean));
65 static boolean v850_elf_set_private_flags
66   PARAMS ((bfd *, flagword));
67 static boolean v850_elf_copy_private_bfd_data
68   PARAMS ((bfd *, bfd *));
69 static boolean v850_elf_merge_private_bfd_data
70   PARAMS ((bfd *, bfd *));
71 static boolean v850_elf_print_private_bfd_data
72   PARAMS ((bfd *, PTR));
73 static boolean v850_elf_section_from_bfd_section
74   PARAMS ((bfd *, Elf32_Internal_Shdr *, asection *, int *));
75 static void v850_elf_symbol_processing
76   PARAMS ((bfd *, asymbol *));
77 static boolean v850_elf_add_symbol_hook
78   PARAMS ((bfd *, struct bfd_link_info *, const Elf_Internal_Sym *,
79            const char **, flagword *, asection **, bfd_vma *));
80 static boolean v850_elf_link_output_symbol_hook
81   PARAMS ((bfd *, struct bfd_link_info *, const char *,
82            Elf_Internal_Sym *, asection *));
83 static boolean v850_elf_section_from_shdr
84   PARAMS ((bfd *, Elf_Internal_Shdr *, char *));
85 static boolean v850_elf_gc_sweep_hook
86   PARAMS ((bfd *, struct bfd_link_info *, asection *,
87            const Elf_Internal_Rela *));
88 static asection * v850_elf_gc_mark_hook
89   PARAMS ((bfd *, struct bfd_link_info *,
90            Elf_Internal_Rela *, struct elf_link_hash_entry *,
91            Elf_Internal_Sym *));
92
93 /* Note: It is REQUIRED that the 'type' value of each entry
94    in this array match the index of the entry in the array.  */
95 static reloc_howto_type v850_elf_howto_table[] =
96 {
97   /* This reloc does nothing.  */
98   HOWTO (R_V850_NONE,                   /* type */
99          0,                             /* rightshift */
100          2,                             /* size (0 = byte, 1 = short, 2 = long) */
101          32,                            /* bitsize */
102          false,                         /* pc_relative */
103          0,                             /* bitpos */
104          complain_overflow_bitfield,    /* complain_on_overflow */
105          bfd_elf_generic_reloc,         /* special_function */
106          "R_V850_NONE",                 /* name */
107          false,                         /* partial_inplace */
108          0,                             /* src_mask */
109          0,                             /* dst_mask */
110          false),                        /* pcrel_offset */
111
112   /* A PC relative 9 bit branch.  */
113   HOWTO (R_V850_9_PCREL,                /* type */
114          2,                             /* rightshift */
115          2,                             /* size (0 = byte, 1 = short, 2 = long) */
116          26,                            /* bitsize */
117          true,                          /* pc_relative */
118          0,                             /* bitpos */
119          complain_overflow_bitfield,    /* complain_on_overflow */
120          v850_elf_reloc,                /* special_function */
121          "R_V850_9_PCREL",              /* name */
122          false,                         /* partial_inplace */
123          0x00ffffff,                    /* src_mask */
124          0x00ffffff,                    /* dst_mask */
125          true),                         /* pcrel_offset */
126
127   /* A PC relative 22 bit branch.  */
128   HOWTO (R_V850_22_PCREL,               /* type */
129          2,                             /* rightshift */
130          2,                             /* size (0 = byte, 1 = short, 2 = long) */
131          22,                            /* bitsize */
132          true,                          /* pc_relative */
133          7,                             /* bitpos */
134          complain_overflow_signed,      /* complain_on_overflow */
135          v850_elf_reloc,                /* special_function */
136          "R_V850_22_PCREL",             /* name */
137          false,                         /* partial_inplace */
138          0x07ffff80,                    /* src_mask */
139          0x07ffff80,                    /* dst_mask */
140          true),                         /* pcrel_offset */
141
142   /* High 16 bits of symbol value.  */
143   HOWTO (R_V850_HI16_S,                 /* type */
144          0,                             /* rightshift */
145          1,                             /* size (0 = byte, 1 = short, 2 = long) */
146          16,                            /* bitsize */
147          false,                         /* pc_relative */
148          0,                             /* bitpos */
149          complain_overflow_dont,        /* complain_on_overflow */
150          v850_elf_reloc,                /* special_function */
151          "R_V850_HI16_S",               /* name */
152          false,                         /* partial_inplace */
153          0xffff,                        /* src_mask */
154          0xffff,                        /* dst_mask */
155          false),                        /* pcrel_offset */
156
157   /* High 16 bits of symbol value.  */
158   HOWTO (R_V850_HI16,                   /* type */
159          0,                             /* rightshift */
160          1,                             /* size (0 = byte, 1 = short, 2 = long) */
161          16,                            /* bitsize */
162          false,                         /* pc_relative */
163          0,                             /* bitpos */
164          complain_overflow_dont,        /* complain_on_overflow */
165          v850_elf_reloc,                /* special_function */
166          "R_V850_HI16",                 /* name */
167          false,                         /* partial_inplace */
168          0xffff,                        /* src_mask */
169          0xffff,                        /* dst_mask */
170          false),                        /* pcrel_offset */
171
172   /* Low 16 bits of symbol value.  */
173   HOWTO (R_V850_LO16,                   /* type */
174          0,                             /* rightshift */
175          1,                             /* size (0 = byte, 1 = short, 2 = long) */
176          16,                            /* bitsize */
177          false,                         /* pc_relative */
178          0,                             /* bitpos */
179          complain_overflow_dont,        /* complain_on_overflow */
180          v850_elf_reloc,                /* special_function */
181          "R_V850_LO16",                 /* name */
182          false,                         /* partial_inplace */
183          0xffff,                        /* src_mask */
184          0xffff,                        /* dst_mask */
185          false),                        /* pcrel_offset */
186
187   /* Simple 32bit reloc.  */
188   HOWTO (R_V850_32,                     /* type */
189          0,                             /* rightshift */
190          2,                             /* size (0 = byte, 1 = short, 2 = long) */
191          32,                            /* bitsize */
192          false,                         /* pc_relative */
193          0,                             /* bitpos */
194          complain_overflow_dont,        /* complain_on_overflow */
195          v850_elf_reloc,                /* special_function */
196          "R_V850_32",                   /* name */
197          false,                         /* partial_inplace */
198          0xffffffff,                    /* src_mask */
199          0xffffffff,                    /* dst_mask */
200          false),                        /* pcrel_offset */
201
202   /* Simple 16bit reloc.  */
203   HOWTO (R_V850_16,                     /* type */
204          0,                             /* rightshift */
205          1,                             /* size (0 = byte, 1 = short, 2 = long) */
206          16,                            /* bitsize */
207          false,                         /* pc_relative */
208          0,                             /* bitpos */
209          complain_overflow_dont,        /* complain_on_overflow */
210          bfd_elf_generic_reloc,         /* special_function */
211          "R_V850_16",                   /* name */
212          false,                         /* partial_inplace */
213          0xffff,                        /* src_mask */
214          0xffff,                        /* dst_mask */
215          false),                        /* pcrel_offset */
216
217   /* Simple 8bit reloc.  */
218   HOWTO (R_V850_8,                      /* type */
219          0,                             /* rightshift */
220          0,                             /* size (0 = byte, 1 = short, 2 = long) */
221          8,                             /* bitsize */
222          false,                         /* pc_relative */
223          0,                             /* bitpos */
224          complain_overflow_dont,        /* complain_on_overflow */
225          bfd_elf_generic_reloc,         /* special_function */
226          "R_V850_8",                    /* name */
227          false,                         /* partial_inplace */
228          0xff,                          /* src_mask */
229          0xff,                          /* dst_mask */
230          false),                        /* pcrel_offset */
231
232   /* 16 bit offset from the short data area pointer.  */
233   HOWTO (R_V850_SDA_16_16_OFFSET,       /* type */
234          0,                             /* rightshift */
235          1,                             /* size (0 = byte, 1 = short, 2 = long) */
236          16,                            /* bitsize */
237          false,                         /* pc_relative */
238          0,                             /* bitpos */
239          complain_overflow_dont,        /* complain_on_overflow */
240          v850_elf_reloc,                /* special_function */
241          "R_V850_SDA_16_16_OFFSET",     /* name */
242          false,                         /* partial_inplace */
243          0xffff,                        /* src_mask */
244          0xffff,                        /* dst_mask */
245          false),                        /* pcrel_offset */
246
247   /* 15 bit offset from the short data area pointer.  */
248   HOWTO (R_V850_SDA_15_16_OFFSET,       /* type */
249          1,                             /* rightshift */
250          1,                             /* size (0 = byte, 1 = short, 2 = long) */
251          16,                            /* bitsize */
252          false,                         /* pc_relative */
253          1,                             /* bitpos */
254          complain_overflow_dont,        /* complain_on_overflow */
255          v850_elf_reloc,                /* special_function */
256          "R_V850_SDA_15_16_OFFSET",     /* name */
257          false,                         /* partial_inplace */
258          0xfffe,                        /* src_mask */
259          0xfffe,                        /* dst_mask */
260          false),                        /* pcrel_offset */
261
262   /* 16 bit offset from the zero data area pointer.  */
263   HOWTO (R_V850_ZDA_16_16_OFFSET,       /* type */
264          0,                             /* rightshift */
265          1,                             /* size (0 = byte, 1 = short, 2 = long) */
266          16,                            /* bitsize */
267          false,                         /* pc_relative */
268          0,                             /* bitpos */
269          complain_overflow_dont,        /* complain_on_overflow */
270          v850_elf_reloc,                /* special_function */
271          "R_V850_ZDA_16_16_OFFSET",     /* name */
272          false,                         /* partial_inplace */
273          0xffff,                        /* src_mask */
274          0xffff,                        /* dst_mask */
275          false),                        /* pcrel_offset */
276
277   /* 15 bit offset from the zero data area pointer.  */
278   HOWTO (R_V850_ZDA_15_16_OFFSET,       /* type */
279          1,                             /* rightshift */
280          1,                             /* size (0 = byte, 1 = short, 2 = long) */
281          16,                            /* bitsize */
282          false,                         /* pc_relative */
283          1,                             /* bitpos */
284          complain_overflow_dont,        /* complain_on_overflow */
285          v850_elf_reloc,                /* special_function */
286          "R_V850_ZDA_15_16_OFFSET",     /* name */
287          false,                         /* partial_inplace */
288          0xfffe,                        /* src_mask */
289          0xfffe,                        /* dst_mask */
290          false),                        /* pcrel_offset */
291
292   /* 6 bit offset from the tiny data area pointer.  */
293   HOWTO (R_V850_TDA_6_8_OFFSET,         /* type */
294          2,                             /* rightshift */
295          1,                             /* size (0 = byte, 1 = short, 2 = long) */
296          8,                             /* bitsize */
297          false,                         /* pc_relative */
298          1,                             /* bitpos */
299          complain_overflow_dont,        /* complain_on_overflow */
300          v850_elf_reloc,                /* special_function */
301          "R_V850_TDA_6_8_OFFSET",       /* name */
302          false,                         /* partial_inplace */
303          0x7e,                          /* src_mask */
304          0x7e,                          /* dst_mask */
305          false),                        /* pcrel_offset */
306
307   /* 8 bit offset from the tiny data area pointer.  */
308   HOWTO (R_V850_TDA_7_8_OFFSET,         /* type */
309          1,                             /* rightshift */
310          1,                             /* size (0 = byte, 1 = short, 2 = long) */
311          8,                             /* bitsize */
312          false,                         /* pc_relative */
313          0,                             /* bitpos */
314          complain_overflow_dont,        /* complain_on_overflow */
315          v850_elf_reloc,                /* special_function */
316          "R_V850_TDA_7_8_OFFSET",       /* name */
317          false,                         /* partial_inplace */
318          0x7f,                          /* src_mask */
319          0x7f,                          /* dst_mask */
320          false),                        /* pcrel_offset */
321
322   /* 7 bit offset from the tiny data area pointer.  */
323   HOWTO (R_V850_TDA_7_7_OFFSET,         /* type */
324          0,                             /* rightshift */
325          1,                             /* size (0 = byte, 1 = short, 2 = long) */
326          7,                             /* bitsize */
327          false,                         /* pc_relative */
328          0,                             /* bitpos */
329          complain_overflow_dont,        /* complain_on_overflow */
330          v850_elf_reloc,                /* special_function */
331          "R_V850_TDA_7_7_OFFSET",       /* name */
332          false,                         /* partial_inplace */
333          0x7f,                          /* src_mask */
334          0x7f,                          /* dst_mask */
335          false),                        /* pcrel_offset */
336
337   /* 16 bit offset from the tiny data area pointer!  */
338   HOWTO (R_V850_TDA_16_16_OFFSET,       /* type */
339          0,                             /* rightshift */
340          1,                             /* size (0 = byte, 1 = short, 2 = long) */
341          16,                            /* bitsize */
342          false,                         /* pc_relative */
343          0,                             /* bitpos */
344          complain_overflow_dont,        /* complain_on_overflow */
345          v850_elf_reloc,                /* special_function */
346          "R_V850_TDA_16_16_OFFSET",     /* name */
347          false,                         /* partial_inplace */
348          0xffff,                        /* src_mask */
349          0xfff,                         /* dst_mask */
350          false),                        /* pcrel_offset */
351
352   /* 5 bit offset from the tiny data area pointer.  */
353   HOWTO (R_V850_TDA_4_5_OFFSET,         /* type */
354          1,                             /* rightshift */
355          1,                             /* size (0 = byte, 1 = short, 2 = long) */
356          5,                             /* bitsize */
357          false,                         /* pc_relative */
358          0,                             /* bitpos */
359          complain_overflow_dont,        /* complain_on_overflow */
360          v850_elf_reloc,                /* special_function */
361          "R_V850_TDA_4_5_OFFSET",       /* name */
362          false,                         /* partial_inplace */
363          0x0f,                          /* src_mask */
364          0x0f,                          /* dst_mask */
365          false),                        /* pcrel_offset */
366
367   /* 4 bit offset from the tiny data area pointer.  */
368   HOWTO (R_V850_TDA_4_4_OFFSET,         /* type */
369          0,                             /* rightshift */
370          1,                             /* size (0 = byte, 1 = short, 2 = long) */
371          4,                             /* bitsize */
372          false,                         /* pc_relative */
373          0,                             /* bitpos */
374          complain_overflow_dont,        /* complain_on_overflow */
375          v850_elf_reloc,                /* special_function */
376          "R_V850_TDA_4_4_OFFSET",       /* name */
377          false,                         /* partial_inplace */
378          0x0f,                          /* src_mask */
379          0x0f,                          /* dst_mask */
380          false),                        /* pcrel_offset */
381
382   /* 16 bit offset from the short data area pointer.  */
383   HOWTO (R_V850_SDA_16_16_SPLIT_OFFSET, /* type */
384          0,                             /* rightshift */
385          2,                             /* size (0 = byte, 1 = short, 2 = long) */
386          16,                            /* bitsize */
387          false,                         /* pc_relative */
388          0,                             /* bitpos */
389          complain_overflow_dont,        /* complain_on_overflow */
390          v850_elf_reloc,                /* special_function */
391          "R_V850_SDA_16_16_SPLIT_OFFSET",/* name */
392          false,                         /* partial_inplace */
393          0xfffe0020,                    /* src_mask */
394          0xfffe0020,                    /* dst_mask */
395          false),                        /* pcrel_offset */
396
397   /* 16 bit offset from the zero data area pointer.  */
398   HOWTO (R_V850_ZDA_16_16_SPLIT_OFFSET, /* type */
399          0,                             /* rightshift */
400          2,                             /* size (0 = byte, 1 = short, 2 = long) */
401          16,                            /* bitsize */
402          false,                         /* pc_relative */
403          0,                             /* bitpos */
404          complain_overflow_dont,        /* complain_on_overflow */
405          v850_elf_reloc,                /* special_function */
406          "R_V850_ZDA_16_16_SPLIT_OFFSET",/* name */
407          false,                         /* partial_inplace */
408          0xfffe0020,                    /* src_mask */
409          0xfffe0020,                    /* dst_mask */
410          false),                        /* pcrel_offset */
411
412   /* 6 bit offset from the call table base pointer.  */
413   HOWTO (R_V850_CALLT_6_7_OFFSET,       /* type */
414          0,                             /* rightshift */
415          1,                             /* size (0 = byte, 1 = short, 2 = long) */
416          7,                             /* bitsize */
417          false,                         /* pc_relative */
418          0,                             /* bitpos */
419          complain_overflow_dont,        /* complain_on_overflow */
420          v850_elf_reloc,                /* special_function */
421          "R_V850_CALLT_6_7_OFFSET",     /* name */
422          false,                         /* partial_inplace */
423          0x3f,                          /* src_mask */
424          0x3f,                          /* dst_mask */
425          false),                        /* pcrel_offset */
426
427   /* 16 bit offset from the call table base pointer.  */
428   HOWTO (R_V850_CALLT_16_16_OFFSET,     /* type */
429          0,                             /* rightshift */
430          1,                             /* size (0 = byte, 1 = short, 2 = long) */
431          16,                            /* bitsize */
432          false,                         /* pc_relative */
433          0,                             /* bitpos */
434          complain_overflow_dont,        /* complain_on_overflow */
435          v850_elf_reloc,                /* special_function */
436          "R_V850_CALLT_16_16_OFFSET",   /* name */
437          false,                         /* partial_inplace */
438          0xffff,                        /* src_mask */
439          0xffff,                        /* dst_mask */
440          false),                        /* pcrel_offset */
441
442   /* GNU extension to record C++ vtable hierarchy */
443   HOWTO (R_V850_GNU_VTINHERIT, /* type */
444          0,                     /* rightshift */
445          2,                     /* size (0 = byte, 1 = short, 2 = long) */
446          0,                     /* bitsize */
447          false,                 /* pc_relative */
448          0,                     /* bitpos */
449          complain_overflow_dont, /* complain_on_overflow */
450          NULL,                  /* special_function */
451          "R_V850_GNU_VTINHERIT", /* name */
452          false,                 /* partial_inplace */
453          0,                     /* src_mask */
454          0,                     /* dst_mask */
455          false),                /* pcrel_offset */
456
457   /* GNU extension to record C++ vtable member usage */
458   HOWTO (R_V850_GNU_VTENTRY,     /* type */
459          0,                     /* rightshift */
460          2,                     /* size (0 = byte, 1 = short, 2 = long) */
461          0,                     /* bitsize */
462          false,                 /* pc_relative */
463          0,                     /* bitpos */
464          complain_overflow_dont, /* complain_on_overflow */
465          _bfd_elf_rel_vtable_reloc_fn,  /* special_function */
466          "R_V850_GNU_VTENTRY",   /* name */
467          false,                 /* partial_inplace */
468          0,                     /* src_mask */
469          0,                     /* dst_mask */
470          false),                /* pcrel_offset */
471
472 };
473
474 /* Map BFD reloc types to V850 ELF reloc types.  */
475
476 struct v850_elf_reloc_map
477 {
478   /* BFD_RELOC_V850_CALLT_16_16_OFFSET is 258, which will not fix in an
479      unsigned char.  */
480   bfd_reloc_code_real_type bfd_reloc_val;
481   unsigned char elf_reloc_val;
482 };
483
484 static const struct v850_elf_reloc_map v850_elf_reloc_map[] =
485 {
486   { BFD_RELOC_NONE,             R_V850_NONE },
487   { BFD_RELOC_V850_9_PCREL,     R_V850_9_PCREL },
488   { BFD_RELOC_V850_22_PCREL,    R_V850_22_PCREL },
489   { BFD_RELOC_HI16_S,           R_V850_HI16_S },
490   { BFD_RELOC_HI16,             R_V850_HI16 },
491   { BFD_RELOC_LO16,             R_V850_LO16 },
492   { BFD_RELOC_32,               R_V850_32 },
493   { BFD_RELOC_16,               R_V850_16 },
494   { BFD_RELOC_8,                R_V850_8 },
495   { BFD_RELOC_V850_SDA_16_16_OFFSET, R_V850_SDA_16_16_OFFSET },
496   { BFD_RELOC_V850_SDA_15_16_OFFSET, R_V850_SDA_15_16_OFFSET },
497   { BFD_RELOC_V850_ZDA_16_16_OFFSET, R_V850_ZDA_16_16_OFFSET },
498   { BFD_RELOC_V850_ZDA_15_16_OFFSET, R_V850_ZDA_15_16_OFFSET },
499   { BFD_RELOC_V850_TDA_6_8_OFFSET,   R_V850_TDA_6_8_OFFSET   },
500   { BFD_RELOC_V850_TDA_7_8_OFFSET,   R_V850_TDA_7_8_OFFSET   },
501   { BFD_RELOC_V850_TDA_7_7_OFFSET,   R_V850_TDA_7_7_OFFSET   },
502   { BFD_RELOC_V850_TDA_16_16_OFFSET, R_V850_TDA_16_16_OFFSET },
503   { BFD_RELOC_V850_TDA_4_5_OFFSET,         R_V850_TDA_4_5_OFFSET         },
504   { BFD_RELOC_V850_TDA_4_4_OFFSET,         R_V850_TDA_4_4_OFFSET         },
505   { BFD_RELOC_V850_SDA_16_16_SPLIT_OFFSET, R_V850_SDA_16_16_SPLIT_OFFSET },
506   { BFD_RELOC_V850_ZDA_16_16_SPLIT_OFFSET, R_V850_ZDA_16_16_SPLIT_OFFSET },
507   { BFD_RELOC_V850_CALLT_6_7_OFFSET,       R_V850_CALLT_6_7_OFFSET       },
508   { BFD_RELOC_V850_CALLT_16_16_OFFSET,     R_V850_CALLT_16_16_OFFSET     },
509   { BFD_RELOC_VTABLE_INHERIT,               R_V850_GNU_VTINHERIT },
510   { BFD_RELOC_VTABLE_ENTRY,                 R_V850_GNU_VTENTRY },
511
512 };
513 \f
514 /* Map a bfd relocation into the appropriate howto structure.  */
515
516 static reloc_howto_type *
517 v850_elf_reloc_type_lookup (abfd, code)
518      bfd *                     abfd ATTRIBUTE_UNUSED;
519      bfd_reloc_code_real_type  code;
520 {
521   unsigned int i;
522
523   for (i = ARRAY_SIZE (v850_elf_reloc_map); i --;)
524     if (v850_elf_reloc_map[i].bfd_reloc_val == code)
525       {
526         BFD_ASSERT (v850_elf_howto_table[code].type == v850_elf_reloc_map[i].elf_reloc_val);
527
528         return v850_elf_howto_table + code;
529       }
530
531   return NULL;
532 }
533 \f
534 /* Set the howto pointer for an V850 ELF reloc.  */
535
536 static void
537 v850_elf_info_to_howto_rel (abfd, cache_ptr, dst)
538      bfd *                 abfd ATTRIBUTE_UNUSED;
539      arelent *             cache_ptr;
540      Elf32_Internal_Rel *  dst;
541 {
542   unsigned int r_type;
543
544   r_type = ELF32_R_TYPE (dst->r_info);
545   BFD_ASSERT (r_type < (unsigned int) R_V850_max);
546   cache_ptr->howto = &v850_elf_howto_table[r_type];
547 }
548
549 /* Set the howto pointer for a V850 ELF reloc (type RELA).  */
550 static void
551 v850_elf_info_to_howto_rela (abfd, cache_ptr, dst)
552      bfd *                 abfd ATTRIBUTE_UNUSED;
553      arelent *             cache_ptr;
554      Elf32_Internal_Rela   *dst;
555 {
556   unsigned int r_type;
557
558   r_type = ELF32_R_TYPE (dst->r_info);
559   BFD_ASSERT (r_type < (unsigned int) R_V850_max);
560   cache_ptr->howto = &v850_elf_howto_table[r_type];
561 }
562 \f
563 /* Look through the relocs for a section during the first phase, and
564    allocate space in the global offset table or procedure linkage
565    table.  */
566
567 static boolean
568 v850_elf_check_relocs (abfd, info, sec, relocs)
569      bfd *                      abfd;
570      struct bfd_link_info *     info;
571      asection *                 sec;
572      const Elf_Internal_Rela *  relocs;
573 {
574   boolean ret = true;
575   bfd *dynobj;
576   Elf_Internal_Shdr *symtab_hdr;
577   struct elf_link_hash_entry **sym_hashes;
578   const Elf_Internal_Rela *rel;
579   const Elf_Internal_Rela *rel_end;
580   asection *sreloc;
581   enum v850_reloc_type r_type;
582   int other = 0;
583   const char *common = (const char *)0;
584
585   if (info->relocateable)
586     return true;
587
588 #ifdef DEBUG
589   fprintf (stderr, "v850_elf_check_relocs called for section %s in %s\n",
590            bfd_get_section_name (abfd, sec),
591            bfd_get_filename (abfd));
592 #endif
593
594   dynobj = elf_hash_table (info)->dynobj;
595   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
596   sym_hashes = elf_sym_hashes (abfd);
597   sreloc = NULL;
598
599   rel_end = relocs + sec->reloc_count;
600   for (rel = relocs; rel < rel_end; rel++)
601     {
602       unsigned long r_symndx;
603       struct elf_link_hash_entry *h;
604
605       r_symndx = ELF32_R_SYM (rel->r_info);
606       if (r_symndx < symtab_hdr->sh_info)
607         h = NULL;
608       else
609         h = sym_hashes[r_symndx - symtab_hdr->sh_info];
610
611       r_type = (enum v850_reloc_type) ELF32_R_TYPE (rel->r_info);
612       switch (r_type)
613         {
614         default:
615         case R_V850_NONE:
616         case R_V850_9_PCREL:
617         case R_V850_22_PCREL:
618         case R_V850_HI16_S:
619         case R_V850_HI16:
620         case R_V850_LO16:
621         case R_V850_32:
622         case R_V850_16:
623         case R_V850_8:
624         case R_V850_CALLT_6_7_OFFSET:
625         case R_V850_CALLT_16_16_OFFSET:
626           break;
627
628         /* This relocation describes the C++ object vtable hierarchy.
629            Reconstruct it for later use during GC.  */
630         case R_V850_GNU_VTINHERIT:
631           if (!_bfd_elf32_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
632             return false;
633           break;
634
635         /* This relocation describes which C++ vtable entries
636            are actually used.  Record for later use during GC.  */
637         case R_V850_GNU_VTENTRY:
638           if (!_bfd_elf32_gc_record_vtentry (abfd, sec, h, rel->r_addend))
639             return false;
640           break;
641
642         case R_V850_SDA_16_16_SPLIT_OFFSET:
643         case R_V850_SDA_16_16_OFFSET:
644         case R_V850_SDA_15_16_OFFSET:
645           other = V850_OTHER_SDA;
646           common = ".scommon";
647           goto small_data_common;
648
649         case R_V850_ZDA_16_16_SPLIT_OFFSET:
650         case R_V850_ZDA_16_16_OFFSET:
651         case R_V850_ZDA_15_16_OFFSET:
652           other = V850_OTHER_ZDA;
653           common = ".zcommon";
654           goto small_data_common;
655
656         case R_V850_TDA_4_5_OFFSET:
657         case R_V850_TDA_4_4_OFFSET:
658         case R_V850_TDA_6_8_OFFSET:
659         case R_V850_TDA_7_8_OFFSET:
660         case R_V850_TDA_7_7_OFFSET:
661         case R_V850_TDA_16_16_OFFSET:
662           other = V850_OTHER_TDA;
663           common = ".tcommon";
664           /* fall through */
665
666 #define V850_OTHER_MASK (V850_OTHER_TDA | V850_OTHER_SDA | V850_OTHER_ZDA)
667
668         small_data_common:
669           if (h)
670             {
671               /* Flag which type of relocation was used.  */
672               h->other |= other;
673               if ((h->other & V850_OTHER_MASK) != (other & V850_OTHER_MASK)
674                   && (h->other & V850_OTHER_ERROR) == 0)
675                 {
676                   const char * msg;
677                   static char  buff[200]; /* XXX */
678
679                   switch (h->other & V850_OTHER_MASK)
680                     {
681                     default:
682                       msg = _("Variable `%s' cannot occupy in multiple small data regions");
683                       break;
684                     case V850_OTHER_SDA | V850_OTHER_ZDA | V850_OTHER_TDA:
685                       msg = _("Variable `%s' can only be in one of the small, zero, and tiny data regions");
686                       break;
687                     case V850_OTHER_SDA | V850_OTHER_ZDA:
688                       msg = _("Variable `%s' cannot be in both small and zero data regions simultaneously");
689                       break;
690                     case V850_OTHER_SDA | V850_OTHER_TDA:
691                       msg = _("Variable `%s' cannot be in both small and tiny data regions simultaneously");
692                       break;
693                     case V850_OTHER_ZDA | V850_OTHER_TDA:
694                       msg = _("Variable `%s' cannot be in both zero and tiny data regions simultaneously");
695                       break;
696                     }
697
698                   sprintf (buff, msg, h->root.root.string);
699                   info->callbacks->warning (info, buff, h->root.root.string,
700                                             abfd, h->root.u.def.section, 0);
701
702                   bfd_set_error (bfd_error_bad_value);
703                   h->other |= V850_OTHER_ERROR;
704                   ret = false;
705                 }
706             }
707
708           if (h && h->root.type == bfd_link_hash_common
709               && h->root.u.c.p
710               && !strcmp (bfd_get_section_name (abfd, h->root.u.c.p->section), "COMMON"))
711             {
712               asection * section;
713
714               section = h->root.u.c.p->section = bfd_make_section_old_way (abfd, common);
715               section->flags |= SEC_IS_COMMON;
716             }
717
718 #ifdef DEBUG
719           fprintf (stderr, "v850_elf_check_relocs, found %s relocation for %s%s\n",
720                    v850_elf_howto_table[ (int)r_type ].name,
721                    (h && h->root.root.string) ? h->root.root.string : "<unknown>",
722                    (h->root.type == bfd_link_hash_common) ? ", symbol is common" : "");
723 #endif
724           break;
725         }
726     }
727
728   return ret;
729 }
730
731 /* In the old version, when an entry was checked out from the table,
732    it was deleted.  This produced an error if the entry was needed
733    more than once, as the second attempted retry failed.
734
735    In the current version, the entry is not deleted, instead we set
736    the field 'found' to true.  If a second lookup matches the same
737    entry, then we know that the hi16s reloc has already been updated
738    and does not need to be updated a second time.
739
740    TODO - TOFIX: If it is possible that we need to restore 2 different
741    addresses from the same table entry, where the first generates an
742    overflow, whilst the second do not, then this code will fail.  */
743
744 typedef struct hi16s_location
745 {
746   bfd_vma       addend;
747   bfd_byte *    address;
748   unsigned long counter;
749   boolean       found;
750   struct hi16s_location * next;
751 }
752 hi16s_location;
753
754 static hi16s_location *  previous_hi16s;
755 static hi16s_location *  free_hi16s;
756 static unsigned long     hi16s_counter;
757
758 static void
759 remember_hi16s_reloc (abfd, addend, address)
760      bfd *      abfd;
761      bfd_vma    addend;
762      bfd_byte * address;
763 {
764   hi16s_location * entry = NULL;
765
766   /* Find a free structure.  */
767   if (free_hi16s == NULL)
768     free_hi16s = (hi16s_location *) bfd_zalloc (abfd, sizeof (* free_hi16s));
769
770   entry      = free_hi16s;
771   free_hi16s = free_hi16s->next;
772
773   entry->addend  = addend;
774   entry->address = address;
775   entry->counter = hi16s_counter ++;
776   entry->found   = false;
777   entry->next    = previous_hi16s;
778   previous_hi16s = entry;
779
780   /* Cope with wrap around of our counter.  */
781   if (hi16s_counter == 0)
782     {
783       /* XXX - Assume that all counter entries differ only in their low 16 bits.  */
784       for (entry = previous_hi16s; entry != NULL; entry = entry->next)
785         entry->counter &= 0xffff;
786
787       hi16s_counter = 0x10000;
788     }
789
790   return;
791 }
792
793 static bfd_byte *
794 find_remembered_hi16s_reloc (addend, already_found)
795      bfd_vma   addend;
796      boolean * already_found;
797 {
798   hi16s_location * match = NULL;
799   hi16s_location * entry;
800   hi16s_location * previous = NULL;
801   hi16s_location * prev;
802   bfd_byte *       addr;
803
804   /* Search the table.  Record the most recent entry that matches.  */
805   for (entry = previous_hi16s; entry; entry = entry->next)
806     {
807       if (entry->addend == addend
808           && (match == NULL || match->counter < entry->counter))
809         {
810           previous = prev;
811           match    = entry;
812         }
813
814       prev = entry;
815     }
816
817   if (match == NULL)
818     return NULL;
819
820   /* Extract the address.  */
821   addr = match->address;
822
823   /* Remeber if this entry has already been used before.  */
824   if (already_found)
825     * already_found = match->found;
826
827   /* Note that this entry has now been used.  */
828   match->found = true;
829
830   return addr;
831 }
832
833 /* FIXME:  The code here probably ought to be removed and the code in reloc.c
834    allowed to do its  stuff instead.  At least for most of the relocs, anwyay.  */
835
836 static bfd_reloc_status_type
837 v850_elf_perform_relocation (abfd, r_type, addend, address)
838      bfd *      abfd;
839      int        r_type;
840      bfd_vma    addend;
841      bfd_byte * address;
842 {
843   unsigned long insn;
844   bfd_signed_vma saddend = (bfd_signed_vma) addend;
845
846   switch (r_type)
847     {
848     default:
849       /* fprintf (stderr, "reloc type %d not SUPPORTED\n", r_type ); */
850       return bfd_reloc_notsupported;
851
852     case R_V850_32:
853       bfd_put_32 (abfd, addend, address);
854       return bfd_reloc_ok;
855
856     case R_V850_22_PCREL:
857       if (saddend > 0x1fffff || saddend < -0x200000)
858         return bfd_reloc_overflow;
859
860       if ((addend % 2) != 0)
861         return bfd_reloc_dangerous;
862
863       insn  = bfd_get_32 (abfd, address);
864       insn &= ~0xfffe003f;
865       insn |= (((addend & 0xfffe) << 16) | ((addend & 0x3f0000) >> 16));
866       bfd_put_32 (abfd, insn, address);
867       return bfd_reloc_ok;
868
869     case R_V850_9_PCREL:
870       if (saddend > 0xff || saddend < -0x100)
871         return bfd_reloc_overflow;
872
873       if ((addend % 2) != 0)
874         return bfd_reloc_dangerous;
875
876       insn  = bfd_get_16 (abfd, address);
877       insn &= ~ 0xf870;
878       insn |= ((addend & 0x1f0) << 7) | ((addend & 0x0e) << 3);
879       break;
880
881     case R_V850_HI16:
882       addend += (bfd_get_16 (abfd, address) << 16);
883       addend = (addend >> 16);
884       insn = addend;
885       break;
886
887     case R_V850_HI16_S:
888       /* Remember where this relocation took place.  */
889       remember_hi16s_reloc (abfd, addend, address);
890
891       addend += (bfd_get_16 (abfd, address) << 16);
892       addend = (addend >> 16) + ((addend & 0x8000) != 0);
893
894       /* This relocation cannot overflow.  */
895       if (addend > 0x7fff)
896         addend = 0;
897
898       insn = addend;
899       break;
900
901     case R_V850_LO16:
902       /* Calculate the sum of the value stored in the instruction and the
903          addend and check for overflow from the low 16 bits into the high
904          16 bits.  The assembler has already done some of this:  If the
905          value stored in the instruction has its 15th bit set, (counting
906          from zero) then the assembler will have added 1 to the value
907          stored in the associated HI16S reloc.  So for example, these
908          relocations:
909
910              movhi hi( fred ), r0, r1
911              movea lo( fred ), r1, r1
912
913          will store 0 in the value fields for the MOVHI and MOVEA instructions
914          and addend will be the address of fred, but for these instructions:
915
916              movhi hi( fred + 0x123456), r0, r1
917              movea lo( fred + 0x123456), r1, r1
918
919          the value stored in the MOVHI instruction will be 0x12 and the value
920          stored in the MOVEA instruction will be 0x3456.  If however the
921          instructions were:
922
923              movhi hi( fred + 0x10ffff), r0, r1
924              movea lo( fred + 0x10ffff), r1, r1
925
926          then the value stored in the MOVHI instruction would be 0x11 (not
927          0x10) and the value stored in the MOVEA instruction would be 0xffff.
928          Thus (assuming for the moment that the addend is 0), at run time the
929          MOVHI instruction loads 0x110000 into r1, then the MOVEA instruction
930          adds 0xffffffff (sign extension!) producing 0x10ffff.  Similarly if
931          the instructions were:
932
933              movhi hi( fred - 1), r0, r1
934              movea lo( fred - 1), r1, r1
935
936          then 0 is stored in the MOVHI instruction and -1 is stored in the
937          MOVEA instruction.
938
939          Overflow can occur if the addition of the value stored in the
940          instruction plus the addend sets the 15th bit when before it was clear.
941          This is because the 15th bit will be sign extended into the high part,
942          thus reducing its value by one, but since the 15th bit was originally
943          clear, the assembler will not have added 1 to the previous HI16S reloc
944          to compensate for this effect.  For example:
945
946             movhi hi( fred + 0x123456), r0, r1
947             movea lo( fred + 0x123456), r1, r1
948
949          The value stored in HI16S reloc is 0x12, the value stored in the LO16
950          reloc is 0x3456.  If we assume that the address of fred is 0x00007000
951          then the relocations become:
952
953            HI16S: 0x0012 + (0x00007000 >> 16)    = 0x12
954            LO16:  0x3456 + (0x00007000 & 0xffff) = 0xa456
955
956          but when the instructions are executed, the MOVEA instruction's value
957          is signed extended, so the sum becomes:
958
959               0x00120000
960             + 0xffffa456
961             ------------
962               0x0011a456    but 'fred + 0x123456' = 0x0012a456
963
964          Note that if the 15th bit was set in the value stored in the LO16
965          reloc, then we do not have to do anything:
966
967             movhi hi( fred + 0x10ffff), r0, r1
968             movea lo( fred + 0x10ffff), r1, r1
969
970             HI16S:  0x0011 + (0x00007000 >> 16)    = 0x11
971             LO16:   0xffff + (0x00007000 & 0xffff) = 0x6fff
972
973               0x00110000
974             + 0x00006fff
975             ------------
976               0x00116fff  = fred + 0x10ffff = 0x7000 + 0x10ffff
977
978          Overflow can also occur if the computation carries into the 16th bit
979          and it also results in the 15th bit having the same value as the 15th
980          bit of the original value.   What happens is that the HI16S reloc
981          will have already examined the 15th bit of the original value and
982          added 1 to the high part if the bit is set.  This compensates for the
983          sign extension of 15th bit of the result of the computation.  But now
984          there is a carry into the 16th bit, and this has not been allowed for.
985
986          So, for example if fred is at address 0xf000:
987
988            movhi hi( fred + 0xffff), r0, r1    [bit 15 of the offset is set]
989            movea lo( fred + 0xffff), r1, r1
990
991            HI16S: 0x0001 + (0x0000f000 >> 16)    = 0x0001
992            LO16:  0xffff + (0x0000f000 & 0xffff) = 0xefff   (carry into bit 16 is lost)
993
994              0x00010000
995            + 0xffffefff
996            ------------
997              0x0000efff   but 'fred + 0xffff' = 0x0001efff
998
999          Similarly, if the 15th bit remains clear, but overflow occurs into
1000          the 16th bit then (assuming the address of fred is 0xf000):
1001
1002            movhi hi( fred + 0x7000), r0, r1    [bit 15 of the offset is clear]
1003            movea lo( fred + 0x7000), r1, r1
1004
1005            HI16S: 0x0000 + (0x0000f000 >> 16)    = 0x0000
1006            LO16:  0x7000 + (0x0000f000 & 0xffff) = 0x6fff  (carry into bit 16 is lost)
1007
1008              0x00000000
1009            + 0x00006fff
1010            ------------
1011              0x00006fff   but 'fred + 0x7000' = 0x00016fff
1012
1013          Note - there is no need to change anything if a carry occurs, and the
1014          15th bit changes its value from being set to being clear, as the HI16S
1015          reloc will have already added in 1 to the high part for us:
1016
1017            movhi hi( fred + 0xffff), r0, r1     [bit 15 of the offset is set]
1018            movea lo( fred + 0xffff), r1, r1
1019
1020            HI16S: 0x0001 + (0x00007000 >> 16)
1021            LO16:  0xffff + (0x00007000 & 0xffff) = 0x6fff  (carry into bit 16 is lost)
1022
1023              0x00010000
1024            + 0x00006fff   (bit 15 not set, so the top half is zero)
1025            ------------
1026              0x00016fff   which is right (assuming that fred is at 0x7000)
1027
1028          but if the 15th bit goes from being clear to being set, then we must
1029          once again handle overflow:
1030
1031            movhi hi( fred + 0x7000), r0, r1     [bit 15 of the offset is clear]
1032            movea lo( fred + 0x7000), r1, r1
1033
1034            HI16S: 0x0000 + (0x0000ffff >> 16)
1035            LO16:  0x7000 + (0x0000ffff & 0xffff) = 0x6fff  (carry into bit 16)
1036
1037              0x00000000
1038            + 0x00006fff   (bit 15 not set, so the top half is zero)
1039            ------------
1040              0x00006fff   which is wrong (assuming that fred is at 0xffff).  */
1041       {
1042         long result;
1043
1044         insn   = bfd_get_16 (abfd, address);
1045         result = insn + addend;
1046
1047 #define BIT15_SET(x) ((x) & 0x8000)
1048 #define OVERFLOWS(a,i) ((((a) & 0xffff) + (i)) > 0xffff)
1049
1050         if ((BIT15_SET (result) && ! BIT15_SET (addend))
1051             || (OVERFLOWS (addend, insn)
1052                 && ((! BIT15_SET (insn)) || (BIT15_SET (addend)))))
1053           {
1054             boolean already_updated;
1055             bfd_byte * hi16s_address = find_remembered_hi16s_reloc
1056               (addend, & already_updated);
1057
1058             /* Amend the matching HI16_S relocation.  */
1059             if (hi16s_address != NULL)
1060               {
1061                 if (! already_updated)
1062                   {
1063                     insn = bfd_get_16 (abfd, hi16s_address);
1064                     insn += 1;
1065                     bfd_put_16 (abfd, insn, hi16s_address);
1066                   }
1067               }
1068             else
1069               {
1070                 fprintf (stderr, _("FAILED to find previous HI16 reloc\n"));
1071                 return bfd_reloc_overflow;
1072               }
1073           }
1074
1075         /* Do not complain if value has top bit set, as this has been anticipated.  */
1076         insn = result & 0xffff;
1077         break;
1078       }
1079
1080     case R_V850_8:
1081       addend += (char) bfd_get_8 (abfd, address);
1082
1083       saddend = (bfd_signed_vma) addend;
1084
1085       if (saddend > 0x7f || saddend < -0x80)
1086         return bfd_reloc_overflow;
1087
1088       bfd_put_8 (abfd, addend, address);
1089       return bfd_reloc_ok;
1090
1091     case R_V850_CALLT_16_16_OFFSET:
1092       addend += bfd_get_16 (abfd, address);
1093
1094       saddend = (bfd_signed_vma) addend;
1095
1096       if (saddend > 0xffff || saddend < 0)
1097         return bfd_reloc_overflow;
1098
1099       insn = addend;
1100       break;
1101
1102     case R_V850_16:
1103
1104       /* drop through */
1105     case R_V850_SDA_16_16_OFFSET:
1106     case R_V850_ZDA_16_16_OFFSET:
1107     case R_V850_TDA_16_16_OFFSET:
1108       addend += bfd_get_16 (abfd, address);
1109
1110       saddend = (bfd_signed_vma) addend;
1111
1112       if (saddend > 0x7fff || saddend < -0x8000)
1113         return bfd_reloc_overflow;
1114
1115       insn = addend;
1116       break;
1117
1118     case R_V850_SDA_15_16_OFFSET:
1119     case R_V850_ZDA_15_16_OFFSET:
1120       insn = bfd_get_16 (abfd, address);
1121       addend += (insn & 0xfffe);
1122
1123       saddend = (bfd_signed_vma) addend;
1124
1125       if (saddend > 0x7ffe || saddend < -0x8000)
1126         return bfd_reloc_overflow;
1127
1128       if (addend & 1)
1129         return bfd_reloc_dangerous;
1130
1131       insn = (addend & ~1) | (insn & 1);
1132       break;
1133
1134     case R_V850_TDA_6_8_OFFSET:
1135       insn = bfd_get_16 (abfd, address);
1136       addend += ((insn & 0x7e) << 1);
1137
1138       saddend = (bfd_signed_vma) addend;
1139
1140       if (saddend > 0xfc || saddend < 0)
1141         return bfd_reloc_overflow;
1142
1143       if (addend & 3)
1144         return bfd_reloc_dangerous;
1145
1146       insn &= 0xff81;
1147       insn |= (addend >> 1);
1148       break;
1149
1150     case R_V850_TDA_7_8_OFFSET:
1151       insn = bfd_get_16 (abfd, address);
1152       addend += ((insn & 0x7f) << 1);
1153
1154       saddend = (bfd_signed_vma) addend;
1155
1156       if (saddend > 0xfe || saddend < 0)
1157         return bfd_reloc_overflow;
1158
1159       if (addend & 1)
1160         return bfd_reloc_dangerous;
1161
1162       insn &= 0xff80;
1163       insn |= (addend >> 1);
1164       break;
1165
1166     case R_V850_TDA_7_7_OFFSET:
1167       insn = bfd_get_16 (abfd, address);
1168       addend += insn & 0x7f;
1169
1170       saddend = (bfd_signed_vma) addend;
1171
1172       if (saddend > 0x7f || saddend < 0)
1173         return bfd_reloc_overflow;
1174
1175       insn &= 0xff80;
1176       insn |= addend;
1177       break;
1178
1179     case R_V850_TDA_4_5_OFFSET:
1180       insn = bfd_get_16 (abfd, address);
1181       addend += ((insn & 0xf) << 1);
1182
1183       saddend = (bfd_signed_vma) addend;
1184
1185       if (saddend > 0x1e || saddend < 0)
1186         return bfd_reloc_overflow;
1187
1188       if (addend & 1)
1189         return bfd_reloc_dangerous;
1190
1191       insn &= 0xfff0;
1192       insn |= (addend >> 1);
1193       break;
1194
1195     case R_V850_TDA_4_4_OFFSET:
1196       insn = bfd_get_16 (abfd, address);
1197       addend += insn & 0xf;
1198
1199       saddend = (bfd_signed_vma) addend;
1200
1201       if (saddend > 0xf || saddend < 0)
1202         return bfd_reloc_overflow;
1203
1204       insn &= 0xfff0;
1205       insn |= addend;
1206       break;
1207
1208     case R_V850_ZDA_16_16_SPLIT_OFFSET:
1209     case R_V850_SDA_16_16_SPLIT_OFFSET:
1210       insn = bfd_get_32 (abfd, address);
1211       addend += ((insn & 0xfffe0000) >> 16) + ((insn & 0x20) >> 5);
1212
1213       saddend = (bfd_signed_vma) addend;
1214
1215       if (saddend > 0x7fff || saddend < -0x8000)
1216         return bfd_reloc_overflow;
1217
1218       insn &= 0x0001ffdf;
1219       insn |= (addend & 1) << 5;
1220       insn |= (addend & ~1) << 16;
1221
1222       bfd_put_32 (abfd, insn, address);
1223       return bfd_reloc_ok;
1224
1225     case R_V850_CALLT_6_7_OFFSET:
1226       insn = bfd_get_16 (abfd, address);
1227       addend += ((insn & 0x3f) << 1);
1228
1229       saddend = (bfd_signed_vma) addend;
1230
1231       if (saddend > 0x7e || saddend < 0)
1232         return bfd_reloc_overflow;
1233
1234       if (addend & 1)
1235         return bfd_reloc_dangerous;
1236
1237       insn &= 0xff80;
1238       insn |= (addend >> 1);
1239       break;
1240
1241     case R_V850_GNU_VTINHERIT:
1242     case R_V850_GNU_VTENTRY:
1243       return bfd_reloc_ok;
1244
1245     }
1246
1247   bfd_put_16 (abfd, insn, address);
1248   return bfd_reloc_ok;
1249 }
1250 \f
1251 /* Insert the addend into the instruction.  */
1252
1253 static bfd_reloc_status_type
1254 v850_elf_reloc (abfd, reloc, symbol, data, isection, obfd, err)
1255      bfd *       abfd ATTRIBUTE_UNUSED;
1256      arelent *   reloc;
1257      asymbol *   symbol;
1258      PTR         data ATTRIBUTE_UNUSED;
1259      asection *  isection;
1260      bfd *       obfd;
1261      char **     err ATTRIBUTE_UNUSED;
1262 {
1263   long relocation;
1264
1265   /* If there is an output BFD,
1266      and the symbol is not a section name (which is only defined at final link time),
1267      and either we are not putting the addend into the instruction
1268       or the addend is zero, so there is nothing to add into the instruction
1269      then just fixup the address and return.  */
1270   if (obfd != (bfd *) NULL
1271       && (symbol->flags & BSF_SECTION_SYM) == 0
1272       && (! reloc->howto->partial_inplace
1273           || reloc->addend == 0))
1274     {
1275       reloc->address += isection->output_offset;
1276       return bfd_reloc_ok;
1277     }
1278 #if 0
1279   else if (obfd != NULL)
1280     return bfd_reloc_continue;
1281 #endif
1282
1283   /* Catch relocs involving undefined symbols.  */
1284   if (bfd_is_und_section (symbol->section)
1285       && (symbol->flags & BSF_WEAK) == 0
1286       && obfd == NULL)
1287     return bfd_reloc_undefined;
1288
1289   /* We handle final linking of some relocs ourselves.  */
1290
1291   /* Is the address of the relocation really within the section?  */
1292   if (reloc->address > isection->_cooked_size)
1293     return bfd_reloc_outofrange;
1294
1295   /* Work out which section the relocation is targetted at and the
1296      initial relocation command value.  */
1297
1298   /* Get symbol value.  (Common symbols are special.)  */
1299   if (bfd_is_com_section (symbol->section))
1300     relocation = 0;
1301   else
1302     relocation = symbol->value;
1303
1304   /* Convert input-section-relative symbol value to absolute + addend.  */
1305   relocation += symbol->section->output_section->vma;
1306   relocation += symbol->section->output_offset;
1307   relocation += reloc->addend;
1308
1309 #if 0 /* Since this reloc is going to be processed later on, we should
1310          not make it pc-relative here.  To test this, try assembling and
1311          linking this program:
1312
1313                 .text
1314                 .globl _start
1315                 nop
1316         _start:         
1317                 jr foo
1318
1319                 .section ".foo","ax"
1320                 nop
1321         foo:
1322                 nop      */
1323   if (reloc->howto->pc_relative == true)
1324     {
1325       /* Here the variable relocation holds the final address of the
1326          symbol we are relocating against, plus any addend.  */
1327       relocation -= isection->output_section->vma + isection->output_offset;
1328
1329       /* Deal with pcrel_offset.  */
1330       relocation -= reloc->address;
1331     }
1332 #endif
1333   reloc->addend = relocation;
1334   return bfd_reloc_ok;
1335 }
1336 \f
1337 static boolean
1338 v850_elf_is_local_label_name (abfd, name)
1339      bfd *         abfd ATTRIBUTE_UNUSED;
1340      const char *  name;
1341 {
1342   return (   (name[0] == '.' && (name[1] == 'L' || name[1] == '.'))
1343           || (name[0] == '_' &&  name[1] == '.' && name[2] == 'L' && name[3] == '_'));
1344 }
1345 \f
1346 /* Perform a relocation as part of a final link.  */
1347
1348 static bfd_reloc_status_type
1349 v850_elf_final_link_relocate (howto, input_bfd, output_bfd,
1350                                     input_section, contents, offset, value,
1351                                     addend, info, sym_sec, is_local)
1352      reloc_howto_type *      howto;
1353      bfd *                   input_bfd;
1354      bfd *                   output_bfd ATTRIBUTE_UNUSED;
1355      asection *              input_section;
1356      bfd_byte *              contents;
1357      bfd_vma                 offset;
1358      bfd_vma                 value;
1359      bfd_vma                 addend;
1360      struct bfd_link_info *  info;
1361      asection *              sym_sec;
1362      int                     is_local ATTRIBUTE_UNUSED;
1363 {
1364   unsigned long  r_type   = howto->type;
1365   bfd_byte *     hit_data = contents + offset;
1366
1367   /* Adjust the value according to the relocation.  */
1368   switch (r_type)
1369     {
1370     case R_V850_9_PCREL:
1371       value -= (input_section->output_section->vma
1372                 + input_section->output_offset);
1373       value -= offset;
1374       break;
1375
1376     case R_V850_22_PCREL:
1377       value -= (input_section->output_section->vma
1378                 + input_section->output_offset
1379                 + offset);
1380
1381       /* If the sign extension will corrupt the value then we have overflowed.  */
1382       if (((value & 0xff000000) != 0x0) && ((value & 0xff000000) != 0xff000000))
1383         return bfd_reloc_overflow;
1384
1385       /* Only the bottom 24 bits of the PC are valid */
1386       value = SEXT24 (value);
1387       break;
1388
1389     case R_V850_HI16_S:
1390     case R_V850_HI16:
1391     case R_V850_LO16:
1392     case R_V850_16:
1393     case R_V850_32:
1394     case R_V850_8:
1395       break;
1396
1397     case R_V850_ZDA_15_16_OFFSET:
1398     case R_V850_ZDA_16_16_OFFSET:
1399     case R_V850_ZDA_16_16_SPLIT_OFFSET:
1400       if (sym_sec == NULL)
1401         return bfd_reloc_undefined;
1402
1403       value -= sym_sec->output_section->vma;
1404       break;
1405
1406     case R_V850_SDA_15_16_OFFSET:
1407     case R_V850_SDA_16_16_OFFSET:
1408     case R_V850_SDA_16_16_SPLIT_OFFSET:
1409       {
1410         unsigned long                gp;
1411         struct bfd_link_hash_entry * h;
1412
1413         if (sym_sec == NULL)
1414           return bfd_reloc_undefined;
1415
1416         /* Get the value of __gp.  */
1417         h = bfd_link_hash_lookup (info->hash, "__gp", false, false, true);
1418         if (h == (struct bfd_link_hash_entry *) NULL
1419             || h->type != bfd_link_hash_defined)
1420           return bfd_reloc_other;
1421
1422         gp = (h->u.def.value
1423               + h->u.def.section->output_section->vma
1424               + h->u.def.section->output_offset);
1425
1426         value -= sym_sec->output_section->vma;
1427         value -= (gp - sym_sec->output_section->vma);
1428       }
1429     break;
1430
1431     case R_V850_TDA_4_4_OFFSET:
1432     case R_V850_TDA_4_5_OFFSET:
1433     case R_V850_TDA_16_16_OFFSET:
1434     case R_V850_TDA_7_7_OFFSET:
1435     case R_V850_TDA_7_8_OFFSET:
1436     case R_V850_TDA_6_8_OFFSET:
1437       {
1438         unsigned long                ep;
1439         struct bfd_link_hash_entry * h;
1440
1441         /* Get the value of __ep.  */
1442         h = bfd_link_hash_lookup (info->hash, "__ep", false, false, true);
1443         if (h == (struct bfd_link_hash_entry *) NULL
1444             || h->type != bfd_link_hash_defined)
1445           /* Actually this indicates that __ep could not be found.  */
1446           return bfd_reloc_continue;
1447
1448         ep = (h->u.def.value
1449               + h->u.def.section->output_section->vma
1450               + h->u.def.section->output_offset);
1451
1452         value -= ep;
1453       }
1454     break;
1455
1456     case R_V850_CALLT_6_7_OFFSET:
1457       {
1458         unsigned long                ctbp;
1459         struct bfd_link_hash_entry * h;
1460
1461         /* Get the value of __ctbp.  */
1462         h = bfd_link_hash_lookup (info->hash, "__ctbp", false, false, true);
1463         if (h == (struct bfd_link_hash_entry *) NULL
1464             || h->type != bfd_link_hash_defined)
1465           /* Actually this indicates that __ctbp could not be found.  */
1466           return bfd_reloc_dangerous + 1;
1467
1468         ctbp = (h->u.def.value
1469               + h->u.def.section->output_section->vma
1470               + h->u.def.section->output_offset);
1471         value -= ctbp;
1472       }
1473     break;
1474
1475     case R_V850_CALLT_16_16_OFFSET:
1476       {
1477         unsigned long                ctbp;
1478         struct bfd_link_hash_entry * h;
1479
1480         if (sym_sec == NULL)
1481           return bfd_reloc_undefined;
1482
1483         /* Get the value of __ctbp.  */
1484         h = bfd_link_hash_lookup (info->hash, "__ctbp", false, false, true);
1485         if (h == (struct bfd_link_hash_entry *) NULL
1486             || h->type != bfd_link_hash_defined)
1487           return (bfd_reloc_dangerous + 1);
1488
1489         ctbp = (h->u.def.value
1490               + h->u.def.section->output_section->vma
1491               + h->u.def.section->output_offset);
1492
1493         value -= sym_sec->output_section->vma;
1494         value -= (ctbp - sym_sec->output_section->vma);
1495       }
1496     break;
1497
1498     case R_V850_NONE:
1499     case R_V850_GNU_VTINHERIT:
1500     case R_V850_GNU_VTENTRY:
1501       return bfd_reloc_ok;
1502
1503     default:
1504       return bfd_reloc_notsupported;
1505     }
1506
1507   /* Perform the relocation.  */
1508   return v850_elf_perform_relocation (input_bfd, r_type, value + addend, hit_data);
1509 }
1510 \f
1511 /* Relocate an V850 ELF section.  */
1512
1513 static boolean
1514 v850_elf_relocate_section (output_bfd, info, input_bfd, input_section,
1515                            contents, relocs, local_syms, local_sections)
1516      bfd *                  output_bfd;
1517      struct bfd_link_info * info;
1518      bfd *                  input_bfd;
1519      asection *             input_section;
1520      bfd_byte *             contents;
1521      Elf_Internal_Rela *    relocs;
1522      Elf_Internal_Sym *     local_syms;
1523      asection **            local_sections;
1524 {
1525   Elf_Internal_Shdr *           symtab_hdr;
1526   struct elf_link_hash_entry ** sym_hashes;
1527   Elf_Internal_Rela *           rel;
1528   Elf_Internal_Rela *           relend;
1529
1530   symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
1531   sym_hashes = elf_sym_hashes (input_bfd);
1532
1533   if (sym_hashes == NULL)
1534     {
1535       info->callbacks->warning
1536         (info, "no hash table available", NULL, input_bfd, input_section, 0);
1537
1538       return false;
1539     }
1540
1541   /* Reset the list of remembered HI16S relocs to empty.  */
1542   free_hi16s     = previous_hi16s;
1543   previous_hi16s = NULL;
1544   hi16s_counter  = 0;
1545
1546   rel    = relocs;
1547   relend = relocs + input_section->reloc_count;
1548   for (; rel < relend; rel++)
1549     {
1550       int                          r_type;
1551       reloc_howto_type *           howto;
1552       unsigned long                r_symndx;
1553       Elf_Internal_Sym *           sym;
1554       asection *                   sec;
1555       struct elf_link_hash_entry * h;
1556       bfd_vma                      relocation;
1557       bfd_reloc_status_type        r;
1558
1559       r_symndx = ELF32_R_SYM (rel->r_info);
1560       r_type   = ELF32_R_TYPE (rel->r_info);
1561
1562       if (r_type == R_V850_GNU_VTENTRY
1563           || r_type == R_V850_GNU_VTINHERIT)
1564         continue;
1565
1566       howto = v850_elf_howto_table + r_type;
1567
1568       if (info->relocateable)
1569         {
1570           /* This is a relocateable link.  We don't have to change
1571              anything, unless the reloc is against a section symbol,
1572              in which case we have to adjust according to where the
1573              section symbol winds up in the output section.  */
1574           if (r_symndx < symtab_hdr->sh_info)
1575             {
1576               sym = local_syms + r_symndx;
1577               if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
1578                 {
1579                   sec = local_sections[r_symndx];
1580                   rel->r_addend += sec->output_offset + sym->st_value;
1581                 }
1582             }
1583
1584           continue;
1585         }
1586
1587       /* This is a final link.  */
1588       h = NULL;
1589       sym = NULL;
1590       sec = NULL;
1591       if (r_symndx < symtab_hdr->sh_info)
1592         {
1593           sym = local_syms + r_symndx;
1594           sec = local_sections[r_symndx];
1595           relocation = (sec->output_section->vma
1596                         + sec->output_offset
1597                         + sym->st_value);
1598 #if 0
1599           {
1600             char * name;
1601
1602             name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link, sym->st_name);
1603             name = (name == NULL) ? "<none>" : name;
1604             fprintf (stderr, "local: sec: %s, sym: %s (%d), value: %x + %x + %x addend %x\n",
1605                      sec->name, name, sym->st_name,
1606                      sec->output_section->vma, sec->output_offset, sym->st_value, rel->r_addend);
1607           }
1608 #endif
1609         }
1610       else
1611         {
1612           h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1613
1614           while (h->root.type == bfd_link_hash_indirect
1615                  || h->root.type == bfd_link_hash_warning)
1616             h = (struct elf_link_hash_entry *) h->root.u.i.link;
1617
1618           if (h->root.type == bfd_link_hash_defined
1619               || h->root.type == bfd_link_hash_defweak)
1620             {
1621               sec = h->root.u.def.section;
1622               relocation = (h->root.u.def.value
1623                             + sec->output_section->vma
1624                             + sec->output_offset);
1625 #if 0
1626               fprintf (stderr, "defined: sec: %s, name: %s, value: %x + %x + %x gives: %x\n",
1627                        sec->name, h->root.root.string, h->root.u.def.value, sec->output_section->vma, sec->output_offset, relocation);
1628 #endif
1629             }
1630           else if (h->root.type == bfd_link_hash_undefweak)
1631             {
1632 #if 0
1633               fprintf (stderr, "undefined: sec: %s, name: %s\n",
1634                        sec->name, h->root.root.string);
1635 #endif
1636               relocation = 0;
1637             }
1638           else
1639             {
1640               if (! ((*info->callbacks->undefined_symbol)
1641                      (info, h->root.root.string, input_bfd,
1642                       input_section, rel->r_offset, true)))
1643                 return false;
1644 #if 0
1645               fprintf (stderr, "unknown: name: %s\n", h->root.root.string);
1646 #endif
1647               relocation = 0;
1648             }
1649         }
1650
1651       /* FIXME: We should use the addend, but the COFF relocations don't.  */
1652       r = v850_elf_final_link_relocate (howto, input_bfd, output_bfd,
1653                                         input_section,
1654                                         contents, rel->r_offset,
1655                                         relocation, rel->r_addend,
1656                                         info, sec, h == NULL);
1657
1658       if (r != bfd_reloc_ok)
1659         {
1660           const char * name;
1661           const char * msg = (const char *)0;
1662
1663           if (h != NULL)
1664             name = h->root.root.string;
1665           else
1666             {
1667               name = (bfd_elf_string_from_elf_section
1668                       (input_bfd, symtab_hdr->sh_link, sym->st_name));
1669               if (name == NULL || *name == '\0')
1670                 name = bfd_section_name (input_bfd, sec);
1671             }
1672
1673           switch (r)
1674             {
1675             case bfd_reloc_overflow:
1676               if (! ((*info->callbacks->reloc_overflow)
1677                      (info, name, howto->name, (bfd_vma) 0,
1678                       input_bfd, input_section, rel->r_offset)))
1679                 return false;
1680               break;
1681
1682             case bfd_reloc_undefined:
1683               if (! ((*info->callbacks->undefined_symbol)
1684                      (info, name, input_bfd, input_section,
1685                       rel->r_offset, true)))
1686                 return false;
1687               break;
1688
1689             case bfd_reloc_outofrange:
1690               msg = _("internal error: out of range error");
1691               goto common_error;
1692
1693             case bfd_reloc_notsupported:
1694               msg = _("internal error: unsupported relocation error");
1695               goto common_error;
1696
1697             case bfd_reloc_dangerous:
1698               msg = _("internal error: dangerous relocation");
1699               goto common_error;
1700
1701             case bfd_reloc_other:
1702               msg = _("could not locate special linker symbol __gp");
1703               goto common_error;
1704
1705             case bfd_reloc_continue:
1706               msg = _("could not locate special linker symbol __ep");
1707               goto common_error;
1708
1709             case (bfd_reloc_dangerous + 1):
1710               msg = _("could not locate special linker symbol __ctbp");
1711               goto common_error;
1712
1713             default:
1714               msg = _("internal error: unknown error");
1715               /* fall through */
1716
1717             common_error:
1718               if (!((*info->callbacks->warning)
1719                     (info, msg, name, input_bfd, input_section,
1720                      rel->r_offset)))
1721                 return false;
1722               break;
1723             }
1724         }
1725     }
1726
1727   return true;
1728 }
1729
1730 static boolean
1731 v850_elf_gc_sweep_hook (abfd, info, sec, relocs)
1732      bfd *abfd ATTRIBUTE_UNUSED;
1733      struct bfd_link_info *info ATTRIBUTE_UNUSED;
1734      asection *sec ATTRIBUTE_UNUSED;
1735      const Elf_Internal_Rela *relocs ATTRIBUTE_UNUSED;
1736 {
1737   /* No got and plt entries for v850-elf.  */
1738   return true;
1739 }
1740
1741 static asection *
1742 v850_elf_gc_mark_hook (abfd, info, rel, h, sym)
1743        bfd *abfd;
1744        struct bfd_link_info *info ATTRIBUTE_UNUSED;
1745        Elf_Internal_Rela *rel;
1746        struct elf_link_hash_entry *h;
1747        Elf_Internal_Sym *sym;
1748 {
1749   if (h != NULL)
1750     {
1751       switch (ELF32_R_TYPE (rel->r_info))
1752       {
1753       case R_V850_GNU_VTINHERIT:
1754       case R_V850_GNU_VTENTRY:
1755         break;
1756
1757       default:
1758         switch (h->root.type)
1759           {
1760           case bfd_link_hash_defined:
1761           case bfd_link_hash_defweak:
1762             return h->root.u.def.section;
1763
1764           case bfd_link_hash_common:
1765             return h->root.u.c.p->section;
1766
1767           default:
1768             break;
1769           }
1770        }
1771      }
1772    else
1773      {
1774        if (!(elf_bad_symtab (abfd)
1775            && ELF_ST_BIND (sym->st_info) != STB_LOCAL)
1776          && ! ((sym->st_shndx <= 0 || sym->st_shndx >= SHN_LORESERVE)
1777                 && sym->st_shndx != SHN_COMMON))
1778           {
1779             return bfd_section_from_elf_index (abfd, sym->st_shndx);
1780           }
1781       }
1782   return NULL;
1783 }
1784
1785 /* Set the right machine number.  */
1786
1787 static boolean
1788 v850_elf_object_p (abfd)
1789      bfd *abfd;
1790 {
1791   switch (elf_elfheader (abfd)->e_flags & EF_V850_ARCH)
1792     {
1793     default:
1794     case E_V850_ARCH:   (void) bfd_default_set_arch_mach (abfd, bfd_arch_v850, 0); break;
1795     case E_V850E_ARCH:  (void) bfd_default_set_arch_mach (abfd, bfd_arch_v850, bfd_mach_v850e); break;
1796     case E_V850EA_ARCH: (void) bfd_default_set_arch_mach (abfd, bfd_arch_v850, bfd_mach_v850ea); break;
1797     }
1798   return true;
1799 }
1800
1801 /* Store the machine number in the flags field.  */
1802
1803 static void
1804 v850_elf_final_write_processing (abfd, linker)
1805      bfd *   abfd;
1806      boolean linker ATTRIBUTE_UNUSED;
1807 {
1808   unsigned long val;
1809
1810   switch (bfd_get_mach (abfd))
1811     {
1812     default:
1813     case 0: val = E_V850_ARCH; break;
1814     case bfd_mach_v850e:  val = E_V850E_ARCH; break;
1815     case bfd_mach_v850ea: val = E_V850EA_ARCH;  break;
1816     }
1817
1818   elf_elfheader (abfd)->e_flags &=~ EF_V850_ARCH;
1819   elf_elfheader (abfd)->e_flags |= val;
1820 }
1821
1822 /* Function to keep V850 specific file flags.  */
1823
1824 static boolean
1825 v850_elf_set_private_flags (abfd, flags)
1826      bfd *    abfd;
1827      flagword flags;
1828 {
1829   BFD_ASSERT (!elf_flags_init (abfd)
1830               || elf_elfheader (abfd)->e_flags == flags);
1831
1832   elf_elfheader (abfd)->e_flags = flags;
1833   elf_flags_init (abfd) = true;
1834   return true;
1835 }
1836
1837 /* Copy backend specific data from one object module to another.  */
1838
1839 static boolean
1840 v850_elf_copy_private_bfd_data (ibfd, obfd)
1841      bfd * ibfd;
1842      bfd * obfd;
1843 {
1844   if (   bfd_get_flavour (ibfd) != bfd_target_elf_flavour
1845       || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
1846     return true;
1847
1848   BFD_ASSERT (!elf_flags_init (obfd)
1849               || (elf_elfheader (obfd)->e_flags
1850                   == elf_elfheader (ibfd)->e_flags));
1851
1852   elf_gp (obfd) = elf_gp (ibfd);
1853   elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
1854   elf_flags_init (obfd) = true;
1855
1856   return true;
1857 }
1858
1859 /* Merge backend specific data from an object file
1860    to the output object file when linking.  */
1861 static boolean
1862 v850_elf_merge_private_bfd_data (ibfd, obfd)
1863      bfd * ibfd;
1864      bfd * obfd;
1865 {
1866   flagword out_flags;
1867   flagword in_flags;
1868
1869   if (   bfd_get_flavour (ibfd) != bfd_target_elf_flavour
1870       || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
1871     return true;
1872
1873   in_flags = elf_elfheader (ibfd)->e_flags;
1874   out_flags = elf_elfheader (obfd)->e_flags;
1875
1876   if (! elf_flags_init (obfd))
1877     {
1878       /* If the input is the default architecture then do not
1879          bother setting the flags for the output architecture,
1880          instead allow future merges to do this.  If no future
1881          merges ever set these flags then they will retain their
1882          unitialised values, which surprise surprise, correspond
1883          to the default values.  */
1884       if (bfd_get_arch_info (ibfd)->the_default)
1885         return true;
1886
1887       elf_flags_init (obfd) = true;
1888       elf_elfheader (obfd)->e_flags = in_flags;
1889
1890       if (bfd_get_arch (obfd) == bfd_get_arch (ibfd)
1891           && bfd_get_arch_info (obfd)->the_default)
1892         return bfd_set_arch_mach (obfd, bfd_get_arch (ibfd), bfd_get_mach (ibfd));
1893
1894       return true;
1895     }
1896
1897   /* Check flag compatibility.  */
1898   if (in_flags == out_flags)
1899     return true;
1900
1901   if ((in_flags & EF_V850_ARCH) != (out_flags & EF_V850_ARCH)
1902       && (in_flags & EF_V850_ARCH) != E_V850_ARCH)
1903     _bfd_error_handler (_("%s: Architecture mismatch with previous modules"),
1904                         bfd_get_filename (ibfd));
1905
1906   return true;
1907 }
1908
1909 /* Display the flags field.  */
1910
1911 static boolean
1912 v850_elf_print_private_bfd_data (abfd, ptr)
1913      bfd *   abfd;
1914      PTR     ptr;
1915 {
1916   FILE * file = (FILE *) ptr;
1917
1918   BFD_ASSERT (abfd != NULL && ptr != NULL);
1919
1920   _bfd_elf_print_private_bfd_data (abfd, ptr);
1921
1922   /* xgettext:c-format */
1923   fprintf (file, _("private flags = %lx: "), elf_elfheader (abfd)->e_flags);
1924
1925   switch (elf_elfheader (abfd)->e_flags & EF_V850_ARCH)
1926     {
1927     default:
1928     case E_V850_ARCH: fprintf (file, _("v850 architecture")); break;
1929     case E_V850E_ARCH:  fprintf (file, _("v850e architecture")); break;
1930     case E_V850EA_ARCH: fprintf (file, _("v850ea architecture")); break;
1931     }
1932
1933   fputc ('\n', file);
1934
1935   return true;
1936 }
1937
1938 /* V850 ELF uses four common sections.  One is the usual one, and the
1939    others are for (small) objects in one of the special data areas:
1940    small, tiny and zero.  All the objects are kept together, and then
1941    referenced via the gp register, the ep register or the r0 register
1942    respectively, which yields smaller, faster assembler code.  This
1943    approach is copied from elf32-mips.c.  */
1944
1945 static asection  v850_elf_scom_section;
1946 static asymbol   v850_elf_scom_symbol;
1947 static asymbol * v850_elf_scom_symbol_ptr;
1948 static asection  v850_elf_tcom_section;
1949 static asymbol   v850_elf_tcom_symbol;
1950 static asymbol * v850_elf_tcom_symbol_ptr;
1951 static asection  v850_elf_zcom_section;
1952 static asymbol   v850_elf_zcom_symbol;
1953 static asymbol * v850_elf_zcom_symbol_ptr;
1954
1955 /* Given a BFD section, try to locate the
1956    corresponding ELF section index.  */
1957
1958 static boolean
1959 v850_elf_section_from_bfd_section (abfd, hdr, sec, retval)
1960      bfd *                 abfd ATTRIBUTE_UNUSED;
1961      Elf32_Internal_Shdr * hdr ATTRIBUTE_UNUSED;
1962      asection *            sec;
1963      int *                 retval;
1964 {
1965   if (strcmp (bfd_get_section_name (abfd, sec), ".scommon") == 0)
1966     *retval = SHN_V850_SCOMMON;
1967   else if (strcmp (bfd_get_section_name (abfd, sec), ".tcommon") == 0)
1968     *retval = SHN_V850_TCOMMON;
1969   else if (strcmp (bfd_get_section_name (abfd, sec), ".zcommon") == 0)
1970     *retval = SHN_V850_ZCOMMON;
1971   else
1972     return false;
1973
1974   return true;
1975 }
1976
1977 /* Handle the special V850 section numbers that a symbol may use.  */
1978
1979 static void
1980 v850_elf_symbol_processing (abfd, asym)
1981      bfd *     abfd;
1982      asymbol * asym;
1983 {
1984   elf_symbol_type * elfsym = (elf_symbol_type *) asym;
1985   unsigned short index;
1986
1987   index = elfsym->internal_elf_sym.st_shndx;
1988
1989   /* If the section index is an "ordinary" index, then it may
1990      refer to a v850 specific section created by the assembler.
1991      Check the section's type and change the index it matches.
1992
1993      FIXME: Should we alter the st_shndx field as well ?  */
1994
1995   if (index < elf_elfheader(abfd)[0].e_shnum)
1996     switch (elf_elfsections(abfd)[index]->sh_type)
1997       {
1998       case SHT_V850_SCOMMON:
1999         index = SHN_V850_SCOMMON;
2000         break;
2001
2002       case SHT_V850_TCOMMON:
2003         index = SHN_V850_TCOMMON;
2004         break;
2005
2006       case SHT_V850_ZCOMMON:
2007         index = SHN_V850_ZCOMMON;
2008         break;
2009
2010       default:
2011         break;
2012       }
2013
2014   switch (index)
2015     {
2016     case SHN_V850_SCOMMON:
2017       if (v850_elf_scom_section.name == NULL)
2018         {
2019           /* Initialize the small common section.  */
2020           v850_elf_scom_section.name           = ".scommon";
2021           v850_elf_scom_section.flags          = SEC_IS_COMMON | SEC_ALLOC | SEC_DATA;
2022           v850_elf_scom_section.output_section = & v850_elf_scom_section;
2023           v850_elf_scom_section.symbol         = & v850_elf_scom_symbol;
2024           v850_elf_scom_section.symbol_ptr_ptr = & v850_elf_scom_symbol_ptr;
2025           v850_elf_scom_symbol.name            = ".scommon";
2026           v850_elf_scom_symbol.flags           = BSF_SECTION_SYM;
2027           v850_elf_scom_symbol.section         = & v850_elf_scom_section;
2028           v850_elf_scom_symbol_ptr             = & v850_elf_scom_symbol;
2029         }
2030       asym->section = & v850_elf_scom_section;
2031       asym->value = elfsym->internal_elf_sym.st_size;
2032       break;
2033
2034     case SHN_V850_TCOMMON:
2035       if (v850_elf_tcom_section.name == NULL)
2036         {
2037           /* Initialize the tcommon section.  */
2038           v850_elf_tcom_section.name           = ".tcommon";
2039           v850_elf_tcom_section.flags          = SEC_IS_COMMON;
2040           v850_elf_tcom_section.output_section = & v850_elf_tcom_section;
2041           v850_elf_tcom_section.symbol         = & v850_elf_tcom_symbol;
2042           v850_elf_tcom_section.symbol_ptr_ptr = & v850_elf_tcom_symbol_ptr;
2043           v850_elf_tcom_symbol.name            = ".tcommon";
2044           v850_elf_tcom_symbol.flags           = BSF_SECTION_SYM;
2045           v850_elf_tcom_symbol.section         = & v850_elf_tcom_section;
2046           v850_elf_tcom_symbol_ptr             = & v850_elf_tcom_symbol;
2047         }
2048       asym->section = & v850_elf_tcom_section;
2049       asym->value = elfsym->internal_elf_sym.st_size;
2050       break;
2051
2052     case SHN_V850_ZCOMMON:
2053       if (v850_elf_zcom_section.name == NULL)
2054         {
2055           /* Initialize the zcommon section.  */
2056           v850_elf_zcom_section.name           = ".zcommon";
2057           v850_elf_zcom_section.flags          = SEC_IS_COMMON;
2058           v850_elf_zcom_section.output_section = & v850_elf_zcom_section;
2059           v850_elf_zcom_section.symbol         = & v850_elf_zcom_symbol;
2060           v850_elf_zcom_section.symbol_ptr_ptr = & v850_elf_zcom_symbol_ptr;
2061           v850_elf_zcom_symbol.name            = ".zcommon";
2062           v850_elf_zcom_symbol.flags           = BSF_SECTION_SYM;
2063           v850_elf_zcom_symbol.section         = & v850_elf_zcom_section;
2064           v850_elf_zcom_symbol_ptr             = & v850_elf_zcom_symbol;
2065         }
2066       asym->section = & v850_elf_zcom_section;
2067       asym->value = elfsym->internal_elf_sym.st_size;
2068       break;
2069     }
2070 }
2071
2072 /* Hook called by the linker routine which adds symbols from an object
2073    file.  We must handle the special v850 section numbers here.  */
2074
2075 static boolean
2076 v850_elf_add_symbol_hook (abfd, info, sym, namep, flagsp, secp, valp)
2077      bfd *                    abfd;
2078      struct bfd_link_info *   info ATTRIBUTE_UNUSED;
2079      const Elf_Internal_Sym * sym;
2080      const char **            namep ATTRIBUTE_UNUSED;
2081      flagword *               flagsp ATTRIBUTE_UNUSED;
2082      asection **              secp;
2083      bfd_vma *                valp;
2084 {
2085   int index = sym->st_shndx;
2086
2087   /* If the section index is an "ordinary" index, then it may
2088      refer to a v850 specific section created by the assembler.
2089      Check the section's type and change the index it matches.
2090
2091      FIXME: Should we alter the st_shndx field as well ?  */
2092
2093   if (index < elf_elfheader(abfd)[0].e_shnum)
2094     switch (elf_elfsections(abfd)[index]->sh_type)
2095       {
2096       case SHT_V850_SCOMMON:
2097         index = SHN_V850_SCOMMON;
2098         break;
2099
2100       case SHT_V850_TCOMMON:
2101         index = SHN_V850_TCOMMON;
2102         break;
2103
2104       case SHT_V850_ZCOMMON:
2105         index = SHN_V850_ZCOMMON;
2106         break;
2107
2108       default:
2109         break;
2110       }
2111
2112   switch (index)
2113     {
2114     case SHN_V850_SCOMMON:
2115       *secp = bfd_make_section_old_way (abfd, ".scommon");
2116       (*secp)->flags |= SEC_IS_COMMON;
2117       *valp = sym->st_size;
2118       break;
2119
2120     case SHN_V850_TCOMMON:
2121       *secp = bfd_make_section_old_way (abfd, ".tcommon");
2122       (*secp)->flags |= SEC_IS_COMMON;
2123       *valp = sym->st_size;
2124       break;
2125
2126     case SHN_V850_ZCOMMON:
2127       *secp = bfd_make_section_old_way (abfd, ".zcommon");
2128       (*secp)->flags |= SEC_IS_COMMON;
2129       *valp = sym->st_size;
2130       break;
2131     }
2132
2133   return true;
2134 }
2135
2136 static boolean
2137 v850_elf_link_output_symbol_hook (abfd, info, name, sym, input_sec)
2138      bfd *                  abfd ATTRIBUTE_UNUSED;
2139      struct bfd_link_info * info ATTRIBUTE_UNUSED;
2140      const char *           name ATTRIBUTE_UNUSED;
2141      Elf_Internal_Sym *     sym;
2142      asection *             input_sec;
2143 {
2144   /* If we see a common symbol, which implies a relocatable link, then
2145      if a symbol was in a special common section in an input file, mark
2146      it as a special common in the output file.  */
2147
2148   if (sym->st_shndx == SHN_COMMON)
2149     {
2150       if (strcmp (input_sec->name, ".scommon") == 0)
2151         sym->st_shndx = SHN_V850_SCOMMON;
2152       else if (strcmp (input_sec->name, ".tcommon") == 0)
2153         sym->st_shndx = SHN_V850_TCOMMON;
2154       else if (strcmp (input_sec->name, ".zcommon") == 0)
2155         sym->st_shndx = SHN_V850_ZCOMMON;
2156     }
2157
2158   return true;
2159 }
2160
2161 static boolean
2162 v850_elf_section_from_shdr (abfd, hdr, name)
2163      bfd *               abfd;
2164      Elf_Internal_Shdr * hdr;
2165      char *              name;
2166 {
2167   /* There ought to be a place to keep ELF backend specific flags, but
2168      at the moment there isn't one.  We just keep track of the
2169      sections by their name, instead.  */
2170
2171   if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name))
2172     return false;
2173
2174   switch (hdr->sh_type)
2175     {
2176     case SHT_V850_SCOMMON:
2177     case SHT_V850_TCOMMON:
2178     case SHT_V850_ZCOMMON:
2179       if (! bfd_set_section_flags (abfd, hdr->bfd_section,
2180                                    (bfd_get_section_flags (abfd,
2181                                                            hdr->bfd_section)
2182                                     | SEC_IS_COMMON)))
2183         return false;
2184     }
2185
2186   return true;
2187 }
2188
2189 /* Set the correct type for a V850 ELF section.  We do this
2190    by the section name, which is a hack, but ought to work.  */
2191
2192 static boolean
2193 v850_elf_fake_sections (abfd, hdr, sec)
2194      bfd *                 abfd ATTRIBUTE_UNUSED;
2195      Elf32_Internal_Shdr * hdr;
2196      asection *            sec;
2197 {
2198   register const char * name;
2199
2200   name = bfd_get_section_name (abfd, sec);
2201
2202   if (strcmp (name, ".scommon") == 0)
2203     {
2204       hdr->sh_type = SHT_V850_SCOMMON;
2205     }
2206   else if (strcmp (name, ".tcommon") == 0)
2207     {
2208       hdr->sh_type = SHT_V850_TCOMMON;
2209     }
2210   else if (strcmp (name, ".zcommon") == 0)
2211     hdr->sh_type = SHT_V850_ZCOMMON;
2212
2213   return true;
2214 }
2215 \f
2216 #define TARGET_LITTLE_SYM                       bfd_elf32_v850_vec
2217 #define TARGET_LITTLE_NAME                      "elf32-v850"
2218 #define ELF_ARCH                                bfd_arch_v850
2219 #define ELF_MACHINE_CODE                        EM_CYGNUS_V850
2220 #define ELF_MAXPAGESIZE                         0x1000
2221
2222 #define elf_info_to_howto                       v850_elf_info_to_howto_rela
2223 #define elf_info_to_howto_rel                   v850_elf_info_to_howto_rel
2224
2225 #define elf_backend_check_relocs                v850_elf_check_relocs
2226 #define elf_backend_relocate_section            v850_elf_relocate_section
2227 #define elf_backend_object_p                    v850_elf_object_p
2228 #define elf_backend_final_write_processing      v850_elf_final_write_processing
2229 #define elf_backend_section_from_bfd_section    v850_elf_section_from_bfd_section
2230 #define elf_backend_symbol_processing           v850_elf_symbol_processing
2231 #define elf_backend_add_symbol_hook             v850_elf_add_symbol_hook
2232 #define elf_backend_link_output_symbol_hook     v850_elf_link_output_symbol_hook
2233 #define elf_backend_section_from_shdr           v850_elf_section_from_shdr
2234 #define elf_backend_fake_sections               v850_elf_fake_sections
2235 #define elf_backend_gc_mark_hook                v850_elf_gc_mark_hook
2236 #define elf_backend_gc_sweep_hook               v850_elf_gc_sweep_hook
2237
2238 #define elf_backend_can_gc_sections 1
2239
2240 #define bfd_elf32_bfd_is_local_label_name       v850_elf_is_local_label_name
2241 #define bfd_elf32_bfd_reloc_type_lookup         v850_elf_reloc_type_lookup
2242 #define bfd_elf32_bfd_copy_private_bfd_data     v850_elf_copy_private_bfd_data
2243 #define bfd_elf32_bfd_merge_private_bfd_data    v850_elf_merge_private_bfd_data
2244 #define bfd_elf32_bfd_set_private_flags         v850_elf_set_private_flags
2245 #define bfd_elf32_bfd_print_private_bfd_data    v850_elf_print_private_bfd_data
2246
2247 #define elf_symbol_leading_char                 '_'
2248
2249 #include "elf32-target.h"