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