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