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