2005-03-20 H.J. Lu <hongjiu.lu@intel.com>
[external/binutils.git] / bfd / elf32-v850.c
1 /* V850-specific support for 32-bit ELF
2    Copyright 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005
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 for 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 *, Elf_Internal_Rela *));
39 static void v850_elf_info_to_howto_rela
40   PARAMS ((bfd *, arelent *, Elf_Internal_Rela *));
41 static bfd_reloc_status_type v850_elf_reloc
42   PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
43 static bfd_boolean v850_elf_is_local_label_name
44   PARAMS ((bfd *, const char *));
45 static bfd_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 bfd_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, bfd_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 bfd_boolean v850_elf_object_p
60   PARAMS ((bfd *));
61 static bfd_boolean v850_elf_fake_sections
62   PARAMS ((bfd *, Elf_Internal_Shdr *, asection *));
63 static void v850_elf_final_write_processing
64   PARAMS ((bfd *, bfd_boolean));
65 static bfd_boolean v850_elf_set_private_flags
66   PARAMS ((bfd *, flagword));
67 static bfd_boolean v850_elf_merge_private_bfd_data
68   PARAMS ((bfd *, bfd *));
69 static bfd_boolean v850_elf_print_private_bfd_data
70   PARAMS ((bfd *, PTR));
71 static bfd_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 bfd_boolean v850_elf_add_symbol_hook
76   PARAMS ((bfd *, struct bfd_link_info *, Elf_Internal_Sym *,
77            const char **, flagword *, asection **, bfd_vma *));
78 static bfd_boolean v850_elf_link_output_symbol_hook
79   PARAMS ((struct bfd_link_info *, const char *, Elf_Internal_Sym *,
80            asection *, struct elf_link_hash_entry *));
81 static bfd_boolean v850_elf_gc_sweep_hook
82   PARAMS ((bfd *, struct bfd_link_info *, asection *,
83            const Elf_Internal_Rela *));
84 static asection * v850_elf_gc_mark_hook
85   PARAMS ((asection *, struct bfd_link_info *,
86            Elf_Internal_Rela *, struct elf_link_hash_entry *,
87            Elf_Internal_Sym *));
88 static bfd_reloc_status_type v850_elf_ignore_reloc
89   PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
90 static bfd_boolean v850_elf_relax_delete_bytes
91   PARAMS ((bfd *, asection *, bfd_vma, bfd_vma, int));
92 static bfd_boolean v850_elf_relax_section
93   PARAMS ((bfd *, asection *, struct bfd_link_info *, bfd_boolean *));
94
95 /* Note: It is REQUIRED that the 'type' value of each entry
96    in this array match the index of the entry in the array.  */
97 static reloc_howto_type v850_elf_howto_table[] =
98 {
99   /* This reloc does nothing.  */
100   HOWTO (R_V850_NONE,                   /* type */
101          0,                             /* rightshift */
102          2,                             /* size (0 = byte, 1 = short, 2 = long) */
103          32,                            /* bitsize */
104          FALSE,                         /* pc_relative */
105          0,                             /* bitpos */
106          complain_overflow_bitfield,    /* complain_on_overflow */
107          bfd_elf_generic_reloc,         /* special_function */
108          "R_V850_NONE",                 /* name */
109          FALSE,                         /* partial_inplace */
110          0,                             /* src_mask */
111          0,                             /* dst_mask */
112          FALSE),                        /* pcrel_offset */
113
114   /* A PC relative 9 bit branch.  */
115   HOWTO (R_V850_9_PCREL,                /* type */
116          2,                             /* rightshift */
117          2,                             /* size (0 = byte, 1 = short, 2 = long) */
118          26,                            /* bitsize */
119          TRUE,                          /* pc_relative */
120          0,                             /* bitpos */
121          complain_overflow_bitfield,    /* complain_on_overflow */
122          v850_elf_reloc,                /* special_function */
123          "R_V850_9_PCREL",              /* name */
124          FALSE,                         /* partial_inplace */
125          0x00ffffff,                    /* src_mask */
126          0x00ffffff,                    /* dst_mask */
127          TRUE),                         /* pcrel_offset */
128
129   /* A PC relative 22 bit branch.  */
130   HOWTO (R_V850_22_PCREL,               /* type */
131          2,                             /* rightshift */
132          2,                             /* size (0 = byte, 1 = short, 2 = long) */
133          22,                            /* bitsize */
134          TRUE,                          /* pc_relative */
135          7,                             /* bitpos */
136          complain_overflow_signed,      /* complain_on_overflow */
137          v850_elf_reloc,                /* special_function */
138          "R_V850_22_PCREL",             /* name */
139          FALSE,                         /* partial_inplace */
140          0x07ffff80,                    /* src_mask */
141          0x07ffff80,                    /* dst_mask */
142          TRUE),                         /* pcrel_offset */
143
144   /* High 16 bits of symbol value.  */
145   HOWTO (R_V850_HI16_S,                 /* type */
146          0,                             /* rightshift */
147          1,                             /* size (0 = byte, 1 = short, 2 = long) */
148          16,                            /* bitsize */
149          FALSE,                         /* pc_relative */
150          0,                             /* bitpos */
151          complain_overflow_dont,        /* complain_on_overflow */
152          v850_elf_reloc,                /* special_function */
153          "R_V850_HI16_S",               /* name */
154          FALSE,                         /* partial_inplace */
155          0xffff,                        /* src_mask */
156          0xffff,                        /* dst_mask */
157          FALSE),                        /* pcrel_offset */
158
159   /* High 16 bits of symbol value.  */
160   HOWTO (R_V850_HI16,                   /* type */
161          0,                             /* rightshift */
162          1,                             /* size (0 = byte, 1 = short, 2 = long) */
163          16,                            /* bitsize */
164          FALSE,                         /* pc_relative */
165          0,                             /* bitpos */
166          complain_overflow_dont,        /* complain_on_overflow */
167          v850_elf_reloc,                /* special_function */
168          "R_V850_HI16",                 /* name */
169          FALSE,                         /* partial_inplace */
170          0xffff,                        /* src_mask */
171          0xffff,                        /* dst_mask */
172          FALSE),                        /* pcrel_offset */
173
174   /* Low 16 bits of symbol value.  */
175   HOWTO (R_V850_LO16,                   /* type */
176          0,                             /* rightshift */
177          1,                             /* size (0 = byte, 1 = short, 2 = long) */
178          16,                            /* bitsize */
179          FALSE,                         /* pc_relative */
180          0,                             /* bitpos */
181          complain_overflow_dont,        /* complain_on_overflow */
182          v850_elf_reloc,                /* special_function */
183          "R_V850_LO16",                 /* name */
184          FALSE,                         /* partial_inplace */
185          0xffff,                        /* src_mask */
186          0xffff,                        /* dst_mask */
187          FALSE),                        /* pcrel_offset */
188
189   /* Simple 32bit reloc.  */
190   HOWTO (R_V850_ABS32,                  /* type */
191          0,                             /* rightshift */
192          2,                             /* size (0 = byte, 1 = short, 2 = long) */
193          32,                            /* bitsize */
194          FALSE,                         /* pc_relative */
195          0,                             /* bitpos */
196          complain_overflow_dont,        /* complain_on_overflow */
197          v850_elf_reloc,                /* special_function */
198          "R_V850_ABS32",                /* name */
199          FALSE,                         /* partial_inplace */
200          0xffffffff,                    /* src_mask */
201          0xffffffff,                    /* dst_mask */
202          FALSE),                        /* pcrel_offset */
203
204   /* Simple 16bit reloc.  */
205   HOWTO (R_V850_16,                     /* type */
206          0,                             /* rightshift */
207          1,                             /* size (0 = byte, 1 = short, 2 = long) */
208          16,                            /* bitsize */
209          FALSE,                         /* pc_relative */
210          0,                             /* bitpos */
211          complain_overflow_dont,        /* complain_on_overflow */
212          bfd_elf_generic_reloc,         /* special_function */
213          "R_V850_16",                   /* name */
214          FALSE,                         /* partial_inplace */
215          0xffff,                        /* src_mask */
216          0xffff,                        /* dst_mask */
217          FALSE),                        /* pcrel_offset */
218
219   /* Simple 8bit reloc.  */
220   HOWTO (R_V850_8,                      /* type */
221          0,                             /* rightshift */
222          0,                             /* size (0 = byte, 1 = short, 2 = long) */
223          8,                             /* bitsize */
224          FALSE,                         /* pc_relative */
225          0,                             /* bitpos */
226          complain_overflow_dont,        /* complain_on_overflow */
227          bfd_elf_generic_reloc,         /* special_function */
228          "R_V850_8",                    /* name */
229          FALSE,                         /* partial_inplace */
230          0xff,                          /* src_mask */
231          0xff,                          /* dst_mask */
232          FALSE),                        /* pcrel_offset */
233
234   /* 16 bit offset from the short data area pointer.  */
235   HOWTO (R_V850_SDA_16_16_OFFSET,       /* type */
236          0,                             /* rightshift */
237          1,                             /* size (0 = byte, 1 = short, 2 = long) */
238          16,                            /* bitsize */
239          FALSE,                         /* pc_relative */
240          0,                             /* bitpos */
241          complain_overflow_dont,        /* complain_on_overflow */
242          v850_elf_reloc,                /* special_function */
243          "R_V850_SDA_16_16_OFFSET",     /* name */
244          FALSE,                         /* partial_inplace */
245          0xffff,                        /* src_mask */
246          0xffff,                        /* dst_mask */
247          FALSE),                        /* pcrel_offset */
248
249   /* 15 bit offset from the short data area pointer.  */
250   HOWTO (R_V850_SDA_15_16_OFFSET,       /* type */
251          1,                             /* rightshift */
252          1,                             /* size (0 = byte, 1 = short, 2 = long) */
253          16,                            /* bitsize */
254          FALSE,                         /* pc_relative */
255          1,                             /* bitpos */
256          complain_overflow_dont,        /* complain_on_overflow */
257          v850_elf_reloc,                /* special_function */
258          "R_V850_SDA_15_16_OFFSET",     /* name */
259          FALSE,                         /* partial_inplace */
260          0xfffe,                        /* src_mask */
261          0xfffe,                        /* dst_mask */
262          FALSE),                        /* pcrel_offset */
263
264   /* 16 bit offset from the zero data area pointer.  */
265   HOWTO (R_V850_ZDA_16_16_OFFSET,       /* type */
266          0,                             /* rightshift */
267          1,                             /* size (0 = byte, 1 = short, 2 = long) */
268          16,                            /* bitsize */
269          FALSE,                         /* pc_relative */
270          0,                             /* bitpos */
271          complain_overflow_dont,        /* complain_on_overflow */
272          v850_elf_reloc,                /* special_function */
273          "R_V850_ZDA_16_16_OFFSET",     /* name */
274          FALSE,                         /* partial_inplace */
275          0xffff,                        /* src_mask */
276          0xffff,                        /* dst_mask */
277          FALSE),                        /* pcrel_offset */
278
279   /* 15 bit offset from the zero data area pointer.  */
280   HOWTO (R_V850_ZDA_15_16_OFFSET,       /* type */
281          1,                             /* rightshift */
282          1,                             /* size (0 = byte, 1 = short, 2 = long) */
283          16,                            /* bitsize */
284          FALSE,                         /* pc_relative */
285          1,                             /* bitpos */
286          complain_overflow_dont,        /* complain_on_overflow */
287          v850_elf_reloc,                /* special_function */
288          "R_V850_ZDA_15_16_OFFSET",     /* name */
289          FALSE,                         /* partial_inplace */
290          0xfffe,                        /* src_mask */
291          0xfffe,                        /* dst_mask */
292          FALSE),                        /* pcrel_offset */
293
294   /* 6 bit offset from the tiny data area pointer.  */
295   HOWTO (R_V850_TDA_6_8_OFFSET,         /* type */
296          2,                             /* rightshift */
297          1,                             /* size (0 = byte, 1 = short, 2 = long) */
298          8,                             /* bitsize */
299          FALSE,                         /* pc_relative */
300          1,                             /* bitpos */
301          complain_overflow_dont,        /* complain_on_overflow */
302          v850_elf_reloc,                /* special_function */
303          "R_V850_TDA_6_8_OFFSET",       /* name */
304          FALSE,                         /* partial_inplace */
305          0x7e,                          /* src_mask */
306          0x7e,                          /* dst_mask */
307          FALSE),                        /* pcrel_offset */
308
309   /* 8 bit offset from the tiny data area pointer.  */
310   HOWTO (R_V850_TDA_7_8_OFFSET,         /* type */
311          1,                             /* rightshift */
312          1,                             /* size (0 = byte, 1 = short, 2 = long) */
313          8,                             /* bitsize */
314          FALSE,                         /* pc_relative */
315          0,                             /* bitpos */
316          complain_overflow_dont,        /* complain_on_overflow */
317          v850_elf_reloc,                /* special_function */
318          "R_V850_TDA_7_8_OFFSET",       /* name */
319          FALSE,                         /* partial_inplace */
320          0x7f,                          /* src_mask */
321          0x7f,                          /* dst_mask */
322          FALSE),                        /* pcrel_offset */
323
324   /* 7 bit offset from the tiny data area pointer.  */
325   HOWTO (R_V850_TDA_7_7_OFFSET,         /* type */
326          0,                             /* rightshift */
327          1,                             /* size (0 = byte, 1 = short, 2 = long) */
328          7,                             /* bitsize */
329          FALSE,                         /* pc_relative */
330          0,                             /* bitpos */
331          complain_overflow_dont,        /* complain_on_overflow */
332          v850_elf_reloc,                /* special_function */
333          "R_V850_TDA_7_7_OFFSET",       /* name */
334          FALSE,                         /* partial_inplace */
335          0x7f,                          /* src_mask */
336          0x7f,                          /* dst_mask */
337          FALSE),                        /* pcrel_offset */
338
339   /* 16 bit offset from the tiny data area pointer!  */
340   HOWTO (R_V850_TDA_16_16_OFFSET,       /* type */
341          0,                             /* rightshift */
342          1,                             /* size (0 = byte, 1 = short, 2 = long) */
343          16,                            /* bitsize */
344          FALSE,                         /* pc_relative */
345          0,                             /* bitpos */
346          complain_overflow_dont,        /* complain_on_overflow */
347          v850_elf_reloc,                /* special_function */
348          "R_V850_TDA_16_16_OFFSET",     /* name */
349          FALSE,                         /* partial_inplace */
350          0xffff,                        /* src_mask */
351          0xfff,                         /* dst_mask */
352          FALSE),                        /* pcrel_offset */
353
354   /* 5 bit offset from the tiny data area pointer.  */
355   HOWTO (R_V850_TDA_4_5_OFFSET,         /* type */
356          1,                             /* rightshift */
357          1,                             /* size (0 = byte, 1 = short, 2 = long) */
358          5,                             /* bitsize */
359          FALSE,                         /* pc_relative */
360          0,                             /* bitpos */
361          complain_overflow_dont,        /* complain_on_overflow */
362          v850_elf_reloc,                /* special_function */
363          "R_V850_TDA_4_5_OFFSET",       /* name */
364          FALSE,                         /* partial_inplace */
365          0x0f,                          /* src_mask */
366          0x0f,                          /* dst_mask */
367          FALSE),                        /* pcrel_offset */
368
369   /* 4 bit offset from the tiny data area pointer.  */
370   HOWTO (R_V850_TDA_4_4_OFFSET,         /* type */
371          0,                             /* rightshift */
372          1,                             /* size (0 = byte, 1 = short, 2 = long) */
373          4,                             /* bitsize */
374          FALSE,                         /* pc_relative */
375          0,                             /* bitpos */
376          complain_overflow_dont,        /* complain_on_overflow */
377          v850_elf_reloc,                /* special_function */
378          "R_V850_TDA_4_4_OFFSET",       /* name */
379          FALSE,                         /* partial_inplace */
380          0x0f,                          /* src_mask */
381          0x0f,                          /* dst_mask */
382          FALSE),                        /* pcrel_offset */
383
384   /* 16 bit offset from the short data area pointer.  */
385   HOWTO (R_V850_SDA_16_16_SPLIT_OFFSET, /* type */
386          0,                             /* rightshift */
387          2,                             /* size (0 = byte, 1 = short, 2 = long) */
388          16,                            /* bitsize */
389          FALSE,                         /* pc_relative */
390          0,                             /* bitpos */
391          complain_overflow_dont,        /* complain_on_overflow */
392          v850_elf_reloc,                /* special_function */
393          "R_V850_SDA_16_16_SPLIT_OFFSET",/* name */
394          FALSE,                         /* partial_inplace */
395          0xfffe0020,                    /* src_mask */
396          0xfffe0020,                    /* dst_mask */
397          FALSE),                        /* pcrel_offset */
398
399   /* 16 bit offset from the zero data area pointer.  */
400   HOWTO (R_V850_ZDA_16_16_SPLIT_OFFSET, /* type */
401          0,                             /* rightshift */
402          2,                             /* size (0 = byte, 1 = short, 2 = long) */
403          16,                            /* bitsize */
404          FALSE,                         /* pc_relative */
405          0,                             /* bitpos */
406          complain_overflow_dont,        /* complain_on_overflow */
407          v850_elf_reloc,                /* special_function */
408          "R_V850_ZDA_16_16_SPLIT_OFFSET",/* name */
409          FALSE,                         /* partial_inplace */
410          0xfffe0020,                    /* src_mask */
411          0xfffe0020,                    /* dst_mask */
412          FALSE),                        /* pcrel_offset */
413
414   /* 6 bit offset from the call table base pointer.  */
415   HOWTO (R_V850_CALLT_6_7_OFFSET,       /* type */
416          0,                             /* rightshift */
417          1,                             /* size (0 = byte, 1 = short, 2 = long) */
418          7,                             /* bitsize */
419          FALSE,                         /* pc_relative */
420          0,                             /* bitpos */
421          complain_overflow_dont,        /* complain_on_overflow */
422          v850_elf_reloc,                /* special_function */
423          "R_V850_CALLT_6_7_OFFSET",     /* name */
424          FALSE,                         /* partial_inplace */
425          0x3f,                          /* src_mask */
426          0x3f,                          /* dst_mask */
427          FALSE),                        /* pcrel_offset */
428
429   /* 16 bit offset from the call table base pointer.  */
430   HOWTO (R_V850_CALLT_16_16_OFFSET,     /* type */
431          0,                             /* rightshift */
432          1,                             /* size (0 = byte, 1 = short, 2 = long) */
433          16,                            /* bitsize */
434          FALSE,                         /* pc_relative */
435          0,                             /* bitpos */
436          complain_overflow_dont,        /* complain_on_overflow */
437          v850_elf_reloc,                /* special_function */
438          "R_V850_CALLT_16_16_OFFSET",   /* name */
439          FALSE,                         /* partial_inplace */
440          0xffff,                        /* src_mask */
441          0xffff,                        /* dst_mask */
442          FALSE),                        /* pcrel_offset */
443
444   /* GNU extension to record C++ vtable hierarchy */
445   HOWTO (R_V850_GNU_VTINHERIT, /* type */
446          0,                     /* rightshift */
447          2,                     /* size (0 = byte, 1 = short, 2 = long) */
448          0,                     /* bitsize */
449          FALSE,                 /* pc_relative */
450          0,                     /* bitpos */
451          complain_overflow_dont, /* complain_on_overflow */
452          NULL,                  /* special_function */
453          "R_V850_GNU_VTINHERIT", /* name */
454          FALSE,                 /* partial_inplace */
455          0,                     /* src_mask */
456          0,                     /* dst_mask */
457          FALSE),                /* pcrel_offset */
458
459   /* GNU extension to record C++ vtable member usage */
460   HOWTO (R_V850_GNU_VTENTRY,     /* type */
461          0,                     /* rightshift */
462          2,                     /* size (0 = byte, 1 = short, 2 = long) */
463          0,                     /* bitsize */
464          FALSE,                 /* pc_relative */
465          0,                     /* bitpos */
466          complain_overflow_dont, /* complain_on_overflow */
467          _bfd_elf_rel_vtable_reloc_fn,  /* special_function */
468          "R_V850_GNU_VTENTRY",   /* name */
469          FALSE,                 /* partial_inplace */
470          0,                     /* src_mask */
471          0,                     /* dst_mask */
472          FALSE),                /* pcrel_offset */
473
474   /* Indicates a .longcall pseudo-op.  The compiler will generate a .longcall
475      pseudo-op when it finds a function call which can be relaxed.  */
476   HOWTO (R_V850_LONGCALL,     /* type */
477        0,                     /* rightshift */
478        2,                     /* size (0 = byte, 1 = short, 2 = long) */
479        32,                    /* bitsize */
480        TRUE,                  /* pc_relative */
481        0,                     /* bitpos */
482        complain_overflow_signed, /* complain_on_overflow */
483        v850_elf_ignore_reloc, /* special_function */
484        "R_V850_LONGCALL",     /* name */
485        FALSE,                 /* partial_inplace */
486        0,                     /* src_mask */
487        0,                     /* dst_mask */
488        TRUE),                 /* pcrel_offset */
489
490   /* Indicates a .longjump pseudo-op.  The compiler will generate a
491      .longjump pseudo-op when it finds a branch which can be relaxed.  */
492   HOWTO (R_V850_LONGJUMP,     /* type */
493        0,                     /* rightshift */
494        2,                     /* size (0 = byte, 1 = short, 2 = long) */
495        32,                    /* bitsize */
496        TRUE,                  /* pc_relative */
497        0,                     /* bitpos */
498        complain_overflow_signed, /* complain_on_overflow */
499        v850_elf_ignore_reloc, /* special_function */
500        "R_V850_LONGJUMP",     /* name */
501        FALSE,                 /* partial_inplace */
502        0,                     /* src_mask */
503        0,                     /* dst_mask */
504        TRUE),                 /* pcrel_offset */
505
506   HOWTO (R_V850_ALIGN,        /* type */
507        0,                     /* rightshift */
508        1,                     /* size (0 = byte, 1 = short, 2 = long) */
509        0,                     /* bitsize */
510        FALSE,                 /* pc_relative */
511        0,                     /* bitpos */
512        complain_overflow_unsigned, /* complain_on_overflow */
513        v850_elf_ignore_reloc, /* special_function */
514        "R_V850_ALIGN",        /* name */
515        FALSE,                 /* partial_inplace */
516        0,                     /* src_mask */
517        0,                     /* dst_mask */
518        TRUE),                 /* pcrel_offset */
519   
520   /* Simple pc-relative 32bit reloc.  */
521   HOWTO (R_V850_REL32,                  /* type */
522          0,                             /* rightshift */
523          2,                             /* size (0 = byte, 1 = short, 2 = long) */
524          32,                            /* bitsize */
525          TRUE,                          /* pc_relative */
526          0,                             /* bitpos */
527          complain_overflow_dont,        /* complain_on_overflow */
528          v850_elf_reloc,                /* special_function */
529          "R_V850_REL32",                /* name */
530          FALSE,                         /* partial_inplace */
531          0xffffffff,                    /* src_mask */
532          0xffffffff,                    /* dst_mask */
533          FALSE),                        /* pcrel_offset */
534
535   /* An ld.bu version of R_V850_LO16.  */
536   HOWTO (R_V850_LO16_SPLIT_OFFSET,      /* type */
537          0,                             /* rightshift */
538          2,                             /* size (0 = byte, 1 = short, 2 = long) */
539          16,                            /* bitsize */
540          FALSE,                         /* pc_relative */
541          0,                             /* bitpos */
542          complain_overflow_dont,        /* complain_on_overflow */
543          v850_elf_reloc,                /* special_function */
544          "R_V850_LO16_SPLIT_OFFSET",    /* name */
545          FALSE,                         /* partial_inplace */
546          0xfffe0020,                    /* src_mask */
547          0xfffe0020,                    /* dst_mask */
548          FALSE),                        /* pcrel_offset */
549 };
550
551 /* Map BFD reloc types to V850 ELF reloc types.  */
552
553 struct v850_elf_reloc_map
554 {
555   /* BFD_RELOC_V850_CALLT_16_16_OFFSET is 258, which will not fix in an
556      unsigned char.  */
557   bfd_reloc_code_real_type bfd_reloc_val;
558   unsigned int elf_reloc_val;
559 };
560
561 static const struct v850_elf_reloc_map v850_elf_reloc_map[] =
562 {
563   { BFD_RELOC_NONE,                        R_V850_NONE                   },
564   { BFD_RELOC_V850_9_PCREL,                R_V850_9_PCREL                },
565   { BFD_RELOC_V850_22_PCREL,               R_V850_22_PCREL               },
566   { BFD_RELOC_HI16_S,                      R_V850_HI16_S                 },
567   { BFD_RELOC_HI16,                        R_V850_HI16                   },
568   { BFD_RELOC_LO16,                        R_V850_LO16                   },
569   { BFD_RELOC_32,                          R_V850_ABS32                  },
570   { BFD_RELOC_32_PCREL,                    R_V850_REL32                  },
571   { BFD_RELOC_16,                          R_V850_16                     },
572   { BFD_RELOC_8,                           R_V850_8                      },
573   { BFD_RELOC_V850_SDA_16_16_OFFSET,       R_V850_SDA_16_16_OFFSET       },
574   { BFD_RELOC_V850_SDA_15_16_OFFSET,       R_V850_SDA_15_16_OFFSET       },
575   { BFD_RELOC_V850_ZDA_16_16_OFFSET,       R_V850_ZDA_16_16_OFFSET       },
576   { BFD_RELOC_V850_ZDA_15_16_OFFSET,       R_V850_ZDA_15_16_OFFSET       },
577   { BFD_RELOC_V850_TDA_6_8_OFFSET,         R_V850_TDA_6_8_OFFSET         },
578   { BFD_RELOC_V850_TDA_7_8_OFFSET,         R_V850_TDA_7_8_OFFSET         },
579   { BFD_RELOC_V850_TDA_7_7_OFFSET,         R_V850_TDA_7_7_OFFSET         },
580   { BFD_RELOC_V850_TDA_16_16_OFFSET,       R_V850_TDA_16_16_OFFSET       },
581   { BFD_RELOC_V850_TDA_4_5_OFFSET,         R_V850_TDA_4_5_OFFSET         },
582   { BFD_RELOC_V850_TDA_4_4_OFFSET,         R_V850_TDA_4_4_OFFSET         },
583   { BFD_RELOC_V850_LO16_SPLIT_OFFSET,      R_V850_LO16_SPLIT_OFFSET      },
584   { BFD_RELOC_V850_SDA_16_16_SPLIT_OFFSET, R_V850_SDA_16_16_SPLIT_OFFSET },
585   { BFD_RELOC_V850_ZDA_16_16_SPLIT_OFFSET, R_V850_ZDA_16_16_SPLIT_OFFSET },
586   { BFD_RELOC_V850_CALLT_6_7_OFFSET,       R_V850_CALLT_6_7_OFFSET       },
587   { BFD_RELOC_V850_CALLT_16_16_OFFSET,     R_V850_CALLT_16_16_OFFSET     },
588   { BFD_RELOC_VTABLE_INHERIT,              R_V850_GNU_VTINHERIT          },
589   { BFD_RELOC_VTABLE_ENTRY,                R_V850_GNU_VTENTRY            },
590   { BFD_RELOC_V850_LONGCALL,               R_V850_LONGCALL               },
591   { BFD_RELOC_V850_LONGJUMP,               R_V850_LONGJUMP               },
592   { BFD_RELOC_V850_ALIGN,                  R_V850_ALIGN                  },
593
594 };
595 \f
596 /* Map a bfd relocation into the appropriate howto structure.  */
597
598 static reloc_howto_type *
599 v850_elf_reloc_type_lookup (abfd, code)
600      bfd *abfd ATTRIBUTE_UNUSED;
601      bfd_reloc_code_real_type code;
602 {
603   unsigned int i;
604
605   for (i = ARRAY_SIZE (v850_elf_reloc_map); i --;)
606     if (v850_elf_reloc_map[i].bfd_reloc_val == code)
607       {
608         unsigned int elf_reloc_val = v850_elf_reloc_map[i].elf_reloc_val;
609
610         BFD_ASSERT (v850_elf_howto_table[elf_reloc_val].type == elf_reloc_val);
611
612         return v850_elf_howto_table + elf_reloc_val;
613       }
614
615   return NULL;
616 }
617 \f
618 /* Set the howto pointer for an V850 ELF reloc.  */
619
620 static void
621 v850_elf_info_to_howto_rel (abfd, cache_ptr, dst)
622      bfd *abfd ATTRIBUTE_UNUSED;
623      arelent *cache_ptr;
624      Elf_Internal_Rela *dst;
625 {
626   unsigned int r_type;
627
628   r_type = ELF32_R_TYPE (dst->r_info);
629   BFD_ASSERT (r_type < (unsigned int) R_V850_max);
630   cache_ptr->howto = &v850_elf_howto_table[r_type];
631 }
632
633 /* Set the howto pointer for a V850 ELF reloc (type RELA).  */
634 static void
635 v850_elf_info_to_howto_rela (abfd, cache_ptr, dst)
636      bfd *abfd ATTRIBUTE_UNUSED;
637      arelent * cache_ptr;
638      Elf_Internal_Rela *dst;
639 {
640   unsigned int r_type;
641
642   r_type = ELF32_R_TYPE (dst->r_info);
643   BFD_ASSERT (r_type < (unsigned int) R_V850_max);
644   cache_ptr->howto = &v850_elf_howto_table[r_type];
645 }
646 \f
647 /* Look through the relocs for a section during the first phase, and
648    allocate space in the global offset table or procedure linkage
649    table.  */
650
651 static bfd_boolean
652 v850_elf_check_relocs (abfd, info, sec, relocs)
653      bfd *abfd;
654      struct bfd_link_info *info;
655      asection *sec;
656      const Elf_Internal_Rela *relocs;
657 {
658   bfd_boolean ret = TRUE;
659   bfd *dynobj;
660   Elf_Internal_Shdr *symtab_hdr;
661   struct elf_link_hash_entry **sym_hashes;
662   const Elf_Internal_Rela *rel;
663   const Elf_Internal_Rela *rel_end;
664   asection *sreloc;
665   enum v850_reloc_type r_type;
666   int other = 0;
667   const char *common = (const char *)0;
668
669   if (info->relocatable)
670     return TRUE;
671
672 #ifdef DEBUG
673   _bfd_error_handler ("v850_elf_check_relocs called for section %A in %B",
674                       sec, abfd);
675 #endif
676
677   dynobj = elf_hash_table (info)->dynobj;
678   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
679   sym_hashes = elf_sym_hashes (abfd);
680   sreloc = NULL;
681
682   rel_end = relocs + sec->reloc_count;
683   for (rel = relocs; rel < rel_end; rel++)
684     {
685       unsigned long r_symndx;
686       struct elf_link_hash_entry *h;
687
688       r_symndx = ELF32_R_SYM (rel->r_info);
689       if (r_symndx < symtab_hdr->sh_info)
690         h = NULL;
691       else
692         h = sym_hashes[r_symndx - symtab_hdr->sh_info];
693
694       r_type = (enum v850_reloc_type) ELF32_R_TYPE (rel->r_info);
695       switch (r_type)
696         {
697         default:
698         case R_V850_NONE:
699         case R_V850_9_PCREL:
700         case R_V850_22_PCREL:
701         case R_V850_HI16_S:
702         case R_V850_HI16:
703         case R_V850_LO16:
704         case R_V850_LO16_SPLIT_OFFSET:
705         case R_V850_ABS32:
706         case R_V850_REL32:
707         case R_V850_16:
708         case R_V850_8:
709         case R_V850_CALLT_6_7_OFFSET:
710         case R_V850_CALLT_16_16_OFFSET:
711           break;
712
713         /* This relocation describes the C++ object vtable hierarchy.
714            Reconstruct it for later use during GC.  */
715         case R_V850_GNU_VTINHERIT:
716           if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
717             return FALSE;
718           break;
719
720         /* This relocation describes which C++ vtable entries
721            are actually used.  Record for later use during GC.  */
722         case R_V850_GNU_VTENTRY:
723           if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
724             return FALSE;
725           break;
726
727         case R_V850_SDA_16_16_SPLIT_OFFSET:
728         case R_V850_SDA_16_16_OFFSET:
729         case R_V850_SDA_15_16_OFFSET:
730           other = V850_OTHER_SDA;
731           common = ".scommon";
732           goto small_data_common;
733
734         case R_V850_ZDA_16_16_SPLIT_OFFSET:
735         case R_V850_ZDA_16_16_OFFSET:
736         case R_V850_ZDA_15_16_OFFSET:
737           other = V850_OTHER_ZDA;
738           common = ".zcommon";
739           goto small_data_common;
740
741         case R_V850_TDA_4_5_OFFSET:
742         case R_V850_TDA_4_4_OFFSET:
743         case R_V850_TDA_6_8_OFFSET:
744         case R_V850_TDA_7_8_OFFSET:
745         case R_V850_TDA_7_7_OFFSET:
746         case R_V850_TDA_16_16_OFFSET:
747           other = V850_OTHER_TDA;
748           common = ".tcommon";
749           /* fall through */
750
751 #define V850_OTHER_MASK (V850_OTHER_TDA | V850_OTHER_SDA | V850_OTHER_ZDA)
752
753         small_data_common:
754           if (h)
755             {
756               /* Flag which type of relocation was used.  */
757               h->other |= other;
758               if ((h->other & V850_OTHER_MASK) != (other & V850_OTHER_MASK)
759                   && (h->other & V850_OTHER_ERROR) == 0)
760                 {
761                   const char * msg;
762                   static char  buff[200]; /* XXX */
763
764                   switch (h->other & V850_OTHER_MASK)
765                     {
766                     default:
767                       msg = _("Variable `%s' cannot occupy in multiple small data regions");
768                       break;
769                     case V850_OTHER_SDA | V850_OTHER_ZDA | V850_OTHER_TDA:
770                       msg = _("Variable `%s' can only be in one of the small, zero, and tiny data regions");
771                       break;
772                     case V850_OTHER_SDA | V850_OTHER_ZDA:
773                       msg = _("Variable `%s' cannot be in both small and zero data regions simultaneously");
774                       break;
775                     case V850_OTHER_SDA | V850_OTHER_TDA:
776                       msg = _("Variable `%s' cannot be in both small and tiny data regions simultaneously");
777                       break;
778                     case V850_OTHER_ZDA | V850_OTHER_TDA:
779                       msg = _("Variable `%s' cannot be in both zero and tiny data regions simultaneously");
780                       break;
781                     }
782
783                   sprintf (buff, msg, h->root.root.string);
784                   info->callbacks->warning (info, buff, h->root.root.string,
785                                             abfd, h->root.u.def.section,
786                                             (bfd_vma) 0);
787
788                   bfd_set_error (bfd_error_bad_value);
789                   h->other |= V850_OTHER_ERROR;
790                   ret = FALSE;
791                 }
792             }
793
794           if (h && h->root.type == bfd_link_hash_common
795               && h->root.u.c.p
796               && !strcmp (bfd_get_section_name (abfd, h->root.u.c.p->section), "COMMON"))
797             {
798               asection * section;
799
800               section = h->root.u.c.p->section = bfd_make_section_old_way (abfd, common);
801               section->flags |= SEC_IS_COMMON;
802             }
803
804 #ifdef DEBUG
805           fprintf (stderr, "v850_elf_check_relocs, found %s relocation for %s%s\n",
806                    v850_elf_howto_table[ (int)r_type ].name,
807                    (h && h->root.root.string) ? h->root.root.string : "<unknown>",
808                    (h->root.type == bfd_link_hash_common) ? ", symbol is common" : "");
809 #endif
810           break;
811         }
812     }
813
814   return ret;
815 }
816
817 /* In the old version, when an entry was checked out from the table,
818    it was deleted.  This produced an error if the entry was needed
819    more than once, as the second attempted retry failed.
820
821    In the current version, the entry is not deleted, instead we set
822    the field 'found' to TRUE.  If a second lookup matches the same
823    entry, then we know that the hi16s reloc has already been updated
824    and does not need to be updated a second time.
825
826    TODO - TOFIX: If it is possible that we need to restore 2 different
827    addresses from the same table entry, where the first generates an
828    overflow, whilst the second do not, then this code will fail.  */
829
830 typedef struct hi16s_location
831 {
832   bfd_vma addend;
833   bfd_byte *address;
834   unsigned long counter;
835   bfd_boolean found;
836   struct hi16s_location *next;
837 }
838 hi16s_location;
839
840 static hi16s_location *previous_hi16s;
841 static hi16s_location *free_hi16s;
842 static unsigned long hi16s_counter;
843
844 static void
845 remember_hi16s_reloc (abfd, addend, address)
846      bfd *abfd;
847      bfd_vma addend;
848      bfd_byte *address;
849 {
850   hi16s_location * entry = NULL;
851   bfd_size_type amt = sizeof (* free_hi16s);
852
853   /* Find a free structure.  */
854   if (free_hi16s == NULL)
855     free_hi16s = (hi16s_location *) bfd_zalloc (abfd, amt);
856
857   entry      = free_hi16s;
858   free_hi16s = free_hi16s->next;
859
860   entry->addend  = addend;
861   entry->address = address;
862   entry->counter = hi16s_counter ++;
863   entry->found   = FALSE;
864   entry->next    = previous_hi16s;
865   previous_hi16s = entry;
866
867   /* Cope with wrap around of our counter.  */
868   if (hi16s_counter == 0)
869     {
870       /* XXX - Assume that all counter entries differ only in their low 16 bits.  */
871       for (entry = previous_hi16s; entry != NULL; entry = entry->next)
872         entry->counter &= 0xffff;
873
874       hi16s_counter = 0x10000;
875     }
876
877   return;
878 }
879
880 static bfd_byte *
881 find_remembered_hi16s_reloc (addend, already_found)
882      bfd_vma addend;
883      bfd_boolean *already_found;
884 {
885   hi16s_location *match = NULL;
886   hi16s_location *entry;
887   hi16s_location *previous = NULL;
888   hi16s_location *prev;
889   bfd_byte *addr;
890
891   /* Search the table.  Record the most recent entry that matches.  */
892   for (entry = previous_hi16s; entry; entry = entry->next)
893     {
894       if (entry->addend == addend
895           && (match == NULL || match->counter < entry->counter))
896         {
897           previous = prev;
898           match    = entry;
899         }
900
901       prev = entry;
902     }
903
904   if (match == NULL)
905     return NULL;
906
907   /* Extract the address.  */
908   addr = match->address;
909
910   /* Remember if this entry has already been used before.  */
911   if (already_found)
912     * already_found = match->found;
913
914   /* Note that this entry has now been used.  */
915   match->found = TRUE;
916
917   return addr;
918 }
919
920 /* Calculate the final operand value for a R_V850_LO16 or
921    R_V850_LO16_SPLIT_OFFSET.  *INSN is the current operand value and
922    ADDEND is the sum of the relocation symbol and offset.  Store the
923    operand value in *INSN and return true on success.
924
925    The assembler has already done some of this: If the value stored in
926    the instruction has its 15th bit set, (counting from zero) then the
927    assembler will have added 1 to the value stored in the associated
928    HI16S reloc.  So for example, these relocations:
929
930        movhi hi( fred ), r0, r1
931        movea lo( fred ), r1, r1
932
933    will store 0 in the value fields for the MOVHI and MOVEA instructions
934    and addend will be the address of fred, but for these instructions:
935
936        movhi hi( fred + 0x123456), r0, r1
937        movea lo( fred + 0x123456), r1, r1
938
939    the value stored in the MOVHI instruction will be 0x12 and the value
940    stored in the MOVEA instruction will be 0x3456.  If however the
941    instructions were:
942
943        movhi hi( fred + 0x10ffff), r0, r1
944        movea lo( fred + 0x10ffff), r1, r1
945
946    then the value stored in the MOVHI instruction would be 0x11 (not
947    0x10) and the value stored in the MOVEA instruction would be 0xffff.
948    Thus (assuming for the moment that the addend is 0), at run time the
949    MOVHI instruction loads 0x110000 into r1, then the MOVEA instruction
950    adds 0xffffffff (sign extension!) producing 0x10ffff.  Similarly if
951    the instructions were:
952
953        movhi hi( fred - 1), r0, r1
954        movea lo( fred - 1), r1, r1
955
956    then 0 is stored in the MOVHI instruction and -1 is stored in the
957    MOVEA instruction.
958
959    Overflow can occur if the addition of the value stored in the
960    instruction plus the addend sets the 15th bit when before it was clear.
961    This is because the 15th bit will be sign extended into the high part,
962    thus reducing its value by one, but since the 15th bit was originally
963    clear, the assembler will not have added 1 to the previous HI16S reloc
964    to compensate for this effect.  For example:
965
966       movhi hi( fred + 0x123456), r0, r1
967       movea lo( fred + 0x123456), r1, r1
968
969    The value stored in HI16S reloc is 0x12, the value stored in the LO16
970    reloc is 0x3456.  If we assume that the address of fred is 0x00007000
971    then the relocations become:
972
973      HI16S: 0x0012 + (0x00007000 >> 16)    = 0x12
974      LO16:  0x3456 + (0x00007000 & 0xffff) = 0xa456
975
976    but when the instructions are executed, the MOVEA instruction's value
977    is signed extended, so the sum becomes:
978
979         0x00120000
980       + 0xffffa456
981       ------------
982         0x0011a456    but 'fred + 0x123456' = 0x0012a456
983
984    Note that if the 15th bit was set in the value stored in the LO16
985    reloc, then we do not have to do anything:
986
987       movhi hi( fred + 0x10ffff), r0, r1
988       movea lo( fred + 0x10ffff), r1, r1
989
990       HI16S:  0x0011 + (0x00007000 >> 16)    = 0x11
991       LO16:   0xffff + (0x00007000 & 0xffff) = 0x6fff
992
993         0x00110000
994       + 0x00006fff
995       ------------
996         0x00116fff  = fred + 0x10ffff = 0x7000 + 0x10ffff
997
998    Overflow can also occur if the computation carries into the 16th bit
999    and it also results in the 15th bit having the same value as the 15th
1000    bit of the original value.   What happens is that the HI16S reloc
1001    will have already examined the 15th bit of the original value and
1002    added 1 to the high part if the bit is set.  This compensates for the
1003    sign extension of 15th bit of the result of the computation.  But now
1004    there is a carry into the 16th bit, and this has not been allowed for.
1005
1006    So, for example if fred is at address 0xf000:
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 + (0x0000f000 >> 16)    = 0x0001
1012      LO16:  0xffff + (0x0000f000 & 0xffff) = 0xefff   (carry into bit 16 is lost)
1013
1014        0x00010000
1015      + 0xffffefff
1016      ------------
1017        0x0000efff   but 'fred + 0xffff' = 0x0001efff
1018
1019    Similarly, if the 15th bit remains clear, but overflow occurs into
1020    the 16th bit then (assuming the address of fred is 0xf000):
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 + (0x0000f000 >> 16)    = 0x0000
1026      LO16:  0x7000 + (0x0000f000 & 0xffff) = 0x6fff  (carry into bit 16 is lost)
1027
1028        0x00000000
1029      + 0x00006fff
1030      ------------
1031        0x00006fff   but 'fred + 0x7000' = 0x00016fff
1032
1033    Note - there is no need to change anything if a carry occurs, and the
1034    15th bit changes its value from being set to being clear, as the HI16S
1035    reloc will have already added in 1 to the high part for us:
1036
1037      movhi hi( fred + 0xffff), r0, r1     [bit 15 of the offset is set]
1038      movea lo( fred + 0xffff), r1, r1
1039
1040      HI16S: 0x0001 + (0x00007000 >> 16)
1041      LO16:  0xffff + (0x00007000 & 0xffff) = 0x6fff  (carry into bit 16 is lost)
1042
1043        0x00010000
1044      + 0x00006fff   (bit 15 not set, so the top half is zero)
1045      ------------
1046        0x00016fff   which is right (assuming that fred is at 0x7000)
1047
1048    but if the 15th bit goes from being clear to being set, then we must
1049    once again handle overflow:
1050
1051      movhi hi( fred + 0x7000), r0, r1     [bit 15 of the offset is clear]
1052      movea lo( fred + 0x7000), r1, r1
1053
1054      HI16S: 0x0000 + (0x0000ffff >> 16)
1055      LO16:  0x7000 + (0x0000ffff & 0xffff) = 0x6fff  (carry into bit 16)
1056
1057        0x00000000
1058      + 0x00006fff   (bit 15 not set, so the top half is zero)
1059      ------------
1060        0x00006fff   which is wrong (assuming that fred is at 0xffff).  */
1061
1062 static bfd_boolean
1063 v850_elf_perform_lo16_relocation (bfd *abfd, unsigned long *insn,
1064                                   unsigned long addend)
1065 {
1066 #define BIT15_SET(x) ((x) & 0x8000)
1067 #define OVERFLOWS(a,i) ((((a) & 0xffff) + (i)) > 0xffff)
1068   if ((BIT15_SET (*insn + addend) && ! BIT15_SET (addend))
1069       || (OVERFLOWS (addend, *insn)
1070           && ((! BIT15_SET (*insn)) || (BIT15_SET (addend)))))
1071     {
1072       bfd_boolean already_updated;
1073       bfd_byte *hi16s_address = find_remembered_hi16s_reloc
1074         (addend, & already_updated);
1075
1076       /* Amend the matching HI16_S relocation.  */
1077       if (hi16s_address != NULL)
1078         {
1079           if (! already_updated)
1080             {
1081               unsigned long hi_insn = bfd_get_16 (abfd, hi16s_address);
1082               hi_insn += 1;
1083               bfd_put_16 (abfd, hi_insn, hi16s_address);
1084             }
1085         }
1086       else
1087         {
1088           fprintf (stderr, _("FAILED to find previous HI16 reloc\n"));
1089           return FALSE;
1090         }
1091     }
1092 #undef OVERFLOWS
1093 #undef BIT15_SET
1094
1095   /* Do not complain if value has top bit set, as this has been
1096      anticipated.  */
1097   *insn = (*insn + addend) & 0xffff;
1098   return TRUE;
1099 }
1100
1101 /* FIXME:  The code here probably ought to be removed and the code in reloc.c
1102    allowed to do its stuff instead.  At least for most of the relocs, anyway.  */
1103
1104 static bfd_reloc_status_type
1105 v850_elf_perform_relocation (abfd, r_type, addend, address)
1106      bfd *abfd;
1107      unsigned int r_type;
1108      bfd_vma addend;
1109      bfd_byte *address;
1110 {
1111   unsigned long insn;
1112   unsigned long result;
1113   bfd_signed_vma saddend = (bfd_signed_vma) addend;
1114
1115   switch (r_type)
1116     {
1117     default:
1118       /* fprintf (stderr, "reloc type %d not SUPPORTED\n", r_type ); */
1119       return bfd_reloc_notsupported;
1120
1121     case R_V850_REL32:
1122     case R_V850_ABS32:
1123       bfd_put_32 (abfd, addend, address);
1124       return bfd_reloc_ok;
1125
1126     case R_V850_22_PCREL:
1127       if (saddend > 0x1fffff || saddend < -0x200000)
1128         return bfd_reloc_overflow;
1129
1130       if ((addend % 2) != 0)
1131         return bfd_reloc_dangerous;
1132
1133       insn  = bfd_get_32 (abfd, address);
1134       insn &= ~0xfffe003f;
1135       insn |= (((addend & 0xfffe) << 16) | ((addend & 0x3f0000) >> 16));
1136       bfd_put_32 (abfd, (bfd_vma) insn, address);
1137       return bfd_reloc_ok;
1138
1139     case R_V850_9_PCREL:
1140       if (saddend > 0xff || saddend < -0x100)
1141         return bfd_reloc_overflow;
1142
1143       if ((addend % 2) != 0)
1144         return bfd_reloc_dangerous;
1145
1146       insn  = bfd_get_16 (abfd, address);
1147       insn &= ~ 0xf870;
1148       insn |= ((addend & 0x1f0) << 7) | ((addend & 0x0e) << 3);
1149       break;
1150
1151     case R_V850_HI16:
1152       addend += (bfd_get_16 (abfd, address) << 16);
1153       addend = (addend >> 16);
1154       insn = addend;
1155       break;
1156
1157     case R_V850_HI16_S:
1158       /* Remember where this relocation took place.  */
1159       remember_hi16s_reloc (abfd, addend, address);
1160
1161       addend += (bfd_get_16 (abfd, address) << 16);
1162       addend = (addend >> 16) + ((addend & 0x8000) != 0);
1163
1164       /* This relocation cannot overflow.  */
1165       if (addend > 0x7fff)
1166         addend = 0;
1167
1168       insn = addend;
1169       break;
1170
1171     case R_V850_LO16:
1172       insn = bfd_get_16 (abfd, address);
1173       if (! v850_elf_perform_lo16_relocation (abfd, &insn, addend))
1174         return bfd_reloc_overflow;
1175       break;
1176
1177     case R_V850_8:
1178       addend += (char) bfd_get_8 (abfd, address);
1179
1180       saddend = (bfd_signed_vma) addend;
1181
1182       if (saddend > 0x7f || saddend < -0x80)
1183         return bfd_reloc_overflow;
1184
1185       bfd_put_8 (abfd, addend, address);
1186       return bfd_reloc_ok;
1187
1188     case R_V850_CALLT_16_16_OFFSET:
1189       addend += bfd_get_16 (abfd, address);
1190
1191       saddend = (bfd_signed_vma) addend;
1192
1193       if (saddend > 0xffff || saddend < 0)
1194         return bfd_reloc_overflow;
1195
1196       insn = addend;
1197       break;
1198
1199     case R_V850_16:
1200
1201       /* drop through */
1202     case R_V850_SDA_16_16_OFFSET:
1203     case R_V850_ZDA_16_16_OFFSET:
1204     case R_V850_TDA_16_16_OFFSET:
1205       addend += bfd_get_16 (abfd, address);
1206
1207       saddend = (bfd_signed_vma) addend;
1208
1209       if (saddend > 0x7fff || saddend < -0x8000)
1210         return bfd_reloc_overflow;
1211
1212       insn = addend;
1213       break;
1214
1215     case R_V850_SDA_15_16_OFFSET:
1216     case R_V850_ZDA_15_16_OFFSET:
1217       insn = bfd_get_16 (abfd, address);
1218       addend += (insn & 0xfffe);
1219
1220       saddend = (bfd_signed_vma) addend;
1221
1222       if (saddend > 0x7ffe || saddend < -0x8000)
1223         return bfd_reloc_overflow;
1224
1225       if (addend & 1)
1226         return bfd_reloc_dangerous;
1227
1228       insn = (addend &~ (bfd_vma) 1) | (insn & 1);
1229       break;
1230
1231     case R_V850_TDA_6_8_OFFSET:
1232       insn = bfd_get_16 (abfd, address);
1233       addend += ((insn & 0x7e) << 1);
1234
1235       saddend = (bfd_signed_vma) addend;
1236
1237       if (saddend > 0xfc || saddend < 0)
1238         return bfd_reloc_overflow;
1239
1240       if (addend & 3)
1241         return bfd_reloc_dangerous;
1242
1243       insn &= 0xff81;
1244       insn |= (addend >> 1);
1245       break;
1246
1247     case R_V850_TDA_7_8_OFFSET:
1248       insn = bfd_get_16 (abfd, address);
1249       addend += ((insn & 0x7f) << 1);
1250
1251       saddend = (bfd_signed_vma) addend;
1252
1253       if (saddend > 0xfe || saddend < 0)
1254         return bfd_reloc_overflow;
1255
1256       if (addend & 1)
1257         return bfd_reloc_dangerous;
1258
1259       insn &= 0xff80;
1260       insn |= (addend >> 1);
1261       break;
1262
1263     case R_V850_TDA_7_7_OFFSET:
1264       insn = bfd_get_16 (abfd, address);
1265       addend += insn & 0x7f;
1266
1267       saddend = (bfd_signed_vma) addend;
1268
1269       if (saddend > 0x7f || saddend < 0)
1270         return bfd_reloc_overflow;
1271
1272       insn &= 0xff80;
1273       insn |= addend;
1274       break;
1275
1276     case R_V850_TDA_4_5_OFFSET:
1277       insn = bfd_get_16 (abfd, address);
1278       addend += ((insn & 0xf) << 1);
1279
1280       saddend = (bfd_signed_vma) addend;
1281
1282       if (saddend > 0x1e || saddend < 0)
1283         return bfd_reloc_overflow;
1284
1285       if (addend & 1)
1286         return bfd_reloc_dangerous;
1287
1288       insn &= 0xfff0;
1289       insn |= (addend >> 1);
1290       break;
1291
1292     case R_V850_TDA_4_4_OFFSET:
1293       insn = bfd_get_16 (abfd, address);
1294       addend += insn & 0xf;
1295
1296       saddend = (bfd_signed_vma) addend;
1297
1298       if (saddend > 0xf || saddend < 0)
1299         return bfd_reloc_overflow;
1300
1301       insn &= 0xfff0;
1302       insn |= addend;
1303       break;
1304
1305     case R_V850_LO16_SPLIT_OFFSET:
1306       insn = bfd_get_32 (abfd, address);
1307       result = ((insn & 0xfffe0000) >> 16) | ((insn & 0x20) >> 5);
1308       if (! v850_elf_perform_lo16_relocation (abfd, &result, addend))
1309         return bfd_reloc_overflow;
1310       insn = (((result << 16) & 0xfffe0000)
1311               | ((result << 5) & 0x20)
1312               | (insn & ~0xfffe0020));
1313       bfd_put_32 (abfd, insn, address);
1314       return bfd_reloc_ok;
1315
1316     case R_V850_ZDA_16_16_SPLIT_OFFSET:
1317     case R_V850_SDA_16_16_SPLIT_OFFSET:
1318       insn = bfd_get_32 (abfd, address);
1319       addend += ((insn & 0xfffe0000) >> 16) + ((insn & 0x20) >> 5);
1320
1321       saddend = (bfd_signed_vma) addend;
1322
1323       if (saddend > 0x7fff || saddend < -0x8000)
1324         return bfd_reloc_overflow;
1325
1326       insn &= 0x0001ffdf;
1327       insn |= (addend & 1) << 5;
1328       insn |= (addend &~ (bfd_vma) 1) << 16;
1329
1330       bfd_put_32 (abfd, (bfd_vma) insn, address);
1331       return bfd_reloc_ok;
1332
1333     case R_V850_CALLT_6_7_OFFSET:
1334       insn = bfd_get_16 (abfd, address);
1335       addend += ((insn & 0x3f) << 1);
1336
1337       saddend = (bfd_signed_vma) addend;
1338
1339       if (saddend > 0x7e || saddend < 0)
1340         return bfd_reloc_overflow;
1341
1342       if (addend & 1)
1343         return bfd_reloc_dangerous;
1344
1345       insn &= 0xff80;
1346       insn |= (addend >> 1);
1347       break;
1348
1349     case R_V850_GNU_VTINHERIT:
1350     case R_V850_GNU_VTENTRY:
1351       return bfd_reloc_ok;
1352
1353     }
1354
1355   bfd_put_16 (abfd, (bfd_vma) insn, address);
1356   return bfd_reloc_ok;
1357 }
1358 \f
1359 /* Insert the addend into the instruction.  */
1360
1361 static bfd_reloc_status_type
1362 v850_elf_reloc (abfd, reloc, symbol, data, isection, obfd, err)
1363      bfd *abfd ATTRIBUTE_UNUSED;
1364      arelent *reloc;
1365      asymbol *symbol;
1366      PTR data ATTRIBUTE_UNUSED;
1367      asection *isection;
1368      bfd *obfd;
1369      char **err ATTRIBUTE_UNUSED;
1370 {
1371   long relocation;
1372
1373   /* If there is an output BFD,
1374      and the symbol is not a section name (which is only defined at final link time),
1375      and either we are not putting the addend into the instruction
1376       or the addend is zero, so there is nothing to add into the instruction
1377      then just fixup the address and return.  */
1378   if (obfd != (bfd *) NULL
1379       && (symbol->flags & BSF_SECTION_SYM) == 0
1380       && (! reloc->howto->partial_inplace
1381           || reloc->addend == 0))
1382     {
1383       reloc->address += isection->output_offset;
1384       return bfd_reloc_ok;
1385     }
1386
1387   /* Catch relocs involving undefined symbols.  */
1388   if (bfd_is_und_section (symbol->section)
1389       && (symbol->flags & BSF_WEAK) == 0
1390       && obfd == NULL)
1391     return bfd_reloc_undefined;
1392
1393   /* We handle final linking of some relocs ourselves.  */
1394
1395   /* Is the address of the relocation really within the section?  */
1396   if (reloc->address > bfd_get_section_limit (abfd, isection))
1397     return bfd_reloc_outofrange;
1398
1399   /* Work out which section the relocation is targeted at and the
1400      initial relocation command value.  */
1401
1402   if (reloc->howto->pc_relative)
1403     return bfd_reloc_ok;
1404
1405   /* Get symbol value.  (Common symbols are special.)  */
1406   if (bfd_is_com_section (symbol->section))
1407     relocation = 0;
1408   else
1409     relocation = symbol->value;
1410
1411   /* Convert input-section-relative symbol value to absolute + addend.  */
1412   relocation += symbol->section->output_section->vma;
1413   relocation += symbol->section->output_offset;
1414   relocation += reloc->addend;
1415
1416   reloc->addend = relocation;
1417   return bfd_reloc_ok;
1418 }
1419
1420 /* This function is used for relocs which are only used
1421    for relaxing, which the linker should otherwise ignore.  */
1422
1423 static bfd_reloc_status_type
1424 v850_elf_ignore_reloc (abfd, reloc_entry, symbol, data, input_section,
1425                        output_bfd, error_message)
1426      bfd *abfd ATTRIBUTE_UNUSED;
1427      arelent *reloc_entry;
1428      asymbol *symbol ATTRIBUTE_UNUSED;
1429      PTR data ATTRIBUTE_UNUSED;
1430      asection *input_section;
1431      bfd *output_bfd;
1432      char **error_message ATTRIBUTE_UNUSED;
1433 {
1434   if (output_bfd != NULL)
1435     reloc_entry->address += input_section->output_offset;
1436
1437   return bfd_reloc_ok;
1438 }
1439 \f
1440 static bfd_boolean
1441 v850_elf_is_local_label_name (abfd, name)
1442      bfd *abfd ATTRIBUTE_UNUSED;
1443      const char *name;
1444 {
1445   return (   (name[0] == '.' && (name[1] == 'L' || name[1] == '.'))
1446           || (name[0] == '_' &&  name[1] == '.' && name[2] == 'L' && name[3] == '_'));
1447 }
1448 \f
1449 /* We overload some of the bfd_reloc error codes for own purposes.  */
1450 #define bfd_reloc_gp_not_found          bfd_reloc_other
1451 #define bfd_reloc_ep_not_found          bfd_reloc_continue
1452 #define bfd_reloc_ctbp_not_found        (bfd_reloc_dangerous + 1)
1453
1454 /* Perform a relocation as part of a final link.  */
1455
1456 static bfd_reloc_status_type
1457 v850_elf_final_link_relocate (howto, input_bfd, output_bfd,
1458                               input_section, contents, offset, value,
1459                               addend, info, sym_sec, is_local)
1460      reloc_howto_type *howto;
1461      bfd *input_bfd;
1462      bfd *output_bfd ATTRIBUTE_UNUSED;
1463      asection *input_section;
1464      bfd_byte *contents;
1465      bfd_vma offset;
1466      bfd_vma value;
1467      bfd_vma addend;
1468      struct bfd_link_info *info;
1469      asection *sym_sec;
1470      int is_local ATTRIBUTE_UNUSED;
1471 {
1472   unsigned int r_type = howto->type;
1473   bfd_byte *hit_data = contents + offset;
1474
1475   /* Adjust the value according to the relocation.  */
1476   switch (r_type)
1477     {
1478     case R_V850_9_PCREL:
1479       value -= (input_section->output_section->vma
1480                 + input_section->output_offset);
1481       value -= offset;
1482       break;
1483
1484     case R_V850_22_PCREL:
1485       value -= (input_section->output_section->vma
1486                 + input_section->output_offset
1487                 + offset);
1488
1489       /* If the sign extension will corrupt the value then we have overflowed.  */
1490       if (((value & 0xff000000) != 0x0) && ((value & 0xff000000) != 0xff000000))
1491         return bfd_reloc_overflow;
1492
1493       /* Only the bottom 24 bits of the PC are valid */
1494       value = SEXT24 (value);
1495       break;
1496
1497     case R_V850_REL32:
1498       value -= (input_section->output_section->vma
1499                 + input_section->output_offset
1500                 + offset);
1501       break;
1502
1503     case R_V850_HI16_S:
1504     case R_V850_HI16:
1505     case R_V850_LO16:
1506     case R_V850_LO16_SPLIT_OFFSET:
1507     case R_V850_16:
1508     case R_V850_ABS32:
1509     case R_V850_8:
1510       break;
1511
1512     case R_V850_ZDA_15_16_OFFSET:
1513     case R_V850_ZDA_16_16_OFFSET:
1514     case R_V850_ZDA_16_16_SPLIT_OFFSET:
1515       if (sym_sec == NULL)
1516         return bfd_reloc_undefined;
1517
1518       value -= sym_sec->output_section->vma;
1519       break;
1520
1521     case R_V850_SDA_15_16_OFFSET:
1522     case R_V850_SDA_16_16_OFFSET:
1523     case R_V850_SDA_16_16_SPLIT_OFFSET:
1524       {
1525         unsigned long                gp;
1526         struct bfd_link_hash_entry * h;
1527
1528         if (sym_sec == NULL)
1529           return bfd_reloc_undefined;
1530
1531         /* Get the value of __gp.  */
1532         h = bfd_link_hash_lookup (info->hash, "__gp", FALSE, FALSE, TRUE);
1533         if (h == (struct bfd_link_hash_entry *) NULL
1534             || h->type != bfd_link_hash_defined)
1535           return bfd_reloc_gp_not_found;
1536
1537         gp = (h->u.def.value
1538               + h->u.def.section->output_section->vma
1539               + h->u.def.section->output_offset);
1540
1541         value -= sym_sec->output_section->vma;
1542         value -= (gp - sym_sec->output_section->vma);
1543       }
1544     break;
1545
1546     case R_V850_TDA_4_4_OFFSET:
1547     case R_V850_TDA_4_5_OFFSET:
1548     case R_V850_TDA_16_16_OFFSET:
1549     case R_V850_TDA_7_7_OFFSET:
1550     case R_V850_TDA_7_8_OFFSET:
1551     case R_V850_TDA_6_8_OFFSET:
1552       {
1553         unsigned long                ep;
1554         struct bfd_link_hash_entry * h;
1555
1556         /* Get the value of __ep.  */
1557         h = bfd_link_hash_lookup (info->hash, "__ep", FALSE, FALSE, TRUE);
1558         if (h == (struct bfd_link_hash_entry *) NULL
1559             || h->type != bfd_link_hash_defined)
1560           return bfd_reloc_ep_not_found;
1561
1562         ep = (h->u.def.value
1563               + h->u.def.section->output_section->vma
1564               + h->u.def.section->output_offset);
1565
1566         value -= ep;
1567       }
1568     break;
1569
1570     case R_V850_CALLT_6_7_OFFSET:
1571       {
1572         unsigned long                ctbp;
1573         struct bfd_link_hash_entry * h;
1574
1575         /* Get the value of __ctbp.  */
1576         h = bfd_link_hash_lookup (info->hash, "__ctbp", FALSE, FALSE, TRUE);
1577         if (h == (struct bfd_link_hash_entry *) NULL
1578             || h->type != bfd_link_hash_defined)
1579           return bfd_reloc_ctbp_not_found;
1580
1581         ctbp = (h->u.def.value
1582               + h->u.def.section->output_section->vma
1583               + h->u.def.section->output_offset);
1584         value -= ctbp;
1585       }
1586     break;
1587
1588     case R_V850_CALLT_16_16_OFFSET:
1589       {
1590         unsigned long                ctbp;
1591         struct bfd_link_hash_entry * h;
1592
1593         if (sym_sec == NULL)
1594           return bfd_reloc_undefined;
1595
1596         /* Get the value of __ctbp.  */
1597         h = bfd_link_hash_lookup (info->hash, "__ctbp", FALSE, FALSE, TRUE);
1598         if (h == (struct bfd_link_hash_entry *) NULL
1599             || h->type != bfd_link_hash_defined)
1600           return bfd_reloc_ctbp_not_found;
1601
1602         ctbp = (h->u.def.value
1603               + h->u.def.section->output_section->vma
1604               + h->u.def.section->output_offset);
1605
1606         value -= sym_sec->output_section->vma;
1607         value -= (ctbp - sym_sec->output_section->vma);
1608       }
1609     break;
1610
1611     case R_V850_NONE:
1612     case R_V850_GNU_VTINHERIT:
1613     case R_V850_GNU_VTENTRY:
1614     case R_V850_LONGCALL:
1615     case R_V850_LONGJUMP:
1616     case R_V850_ALIGN:
1617       return bfd_reloc_ok;
1618
1619     default:
1620       return bfd_reloc_notsupported;
1621     }
1622
1623   /* Perform the relocation.  */
1624   return v850_elf_perform_relocation (input_bfd, r_type, value + addend, hit_data);
1625 }
1626 \f
1627 /* Relocate an V850 ELF section.  */
1628
1629 static bfd_boolean
1630 v850_elf_relocate_section (output_bfd, info, input_bfd, input_section,
1631                            contents, relocs, local_syms, local_sections)
1632      bfd *output_bfd;
1633      struct bfd_link_info *info;
1634      bfd *input_bfd;
1635      asection *input_section;
1636      bfd_byte *contents;
1637      Elf_Internal_Rela *relocs;
1638      Elf_Internal_Sym *local_syms;
1639      asection **local_sections;
1640 {
1641   Elf_Internal_Shdr *symtab_hdr;
1642   struct elf_link_hash_entry **sym_hashes;
1643   Elf_Internal_Rela *rel;
1644   Elf_Internal_Rela *relend;
1645
1646   if (info->relocatable)
1647     return TRUE;
1648
1649   symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
1650   sym_hashes = elf_sym_hashes (input_bfd);
1651
1652   if (sym_hashes == NULL)
1653     {
1654       info->callbacks->warning
1655         (info, "no hash table available",
1656          NULL, input_bfd, input_section, (bfd_vma) 0);
1657
1658       return FALSE;
1659     }
1660
1661   /* Reset the list of remembered HI16S relocs to empty.  */
1662   free_hi16s     = previous_hi16s;
1663   previous_hi16s = NULL;
1664   hi16s_counter  = 0;
1665
1666   rel    = relocs;
1667   relend = relocs + input_section->reloc_count;
1668   for (; rel < relend; rel++)
1669     {
1670       int r_type;
1671       reloc_howto_type *howto;
1672       unsigned long r_symndx;
1673       Elf_Internal_Sym *sym;
1674       asection *sec;
1675       struct elf_link_hash_entry *h;
1676       bfd_vma relocation;
1677       bfd_reloc_status_type r;
1678
1679       r_symndx = ELF32_R_SYM (rel->r_info);
1680       r_type   = ELF32_R_TYPE (rel->r_info);
1681
1682       if (r_type == R_V850_GNU_VTENTRY
1683           || r_type == R_V850_GNU_VTINHERIT)
1684         continue;
1685
1686       /* This is a final link.  */
1687       howto = v850_elf_howto_table + r_type;
1688       h = NULL;
1689       sym = NULL;
1690       sec = NULL;
1691       if (r_symndx < symtab_hdr->sh_info)
1692         {
1693           sym = local_syms + r_symndx;
1694           sec = local_sections[r_symndx];
1695           relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
1696         }
1697       else
1698         {
1699           bfd_boolean unresolved_reloc, warned;
1700
1701           RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
1702                                    r_symndx, symtab_hdr, sym_hashes,
1703                                    h, sec, relocation,
1704                                    unresolved_reloc, warned);
1705         }
1706
1707       /* FIXME: We should use the addend, but the COFF relocations don't.  */
1708       r = v850_elf_final_link_relocate (howto, input_bfd, output_bfd,
1709                                         input_section,
1710                                         contents, rel->r_offset,
1711                                         relocation, rel->r_addend,
1712                                         info, sec, h == NULL);
1713
1714       if (r != bfd_reloc_ok)
1715         {
1716           const char * name;
1717           const char * msg = (const char *)0;
1718
1719           if (h != NULL)
1720             name = h->root.root.string;
1721           else
1722             {
1723               name = (bfd_elf_string_from_elf_section
1724                       (input_bfd, symtab_hdr->sh_link, sym->st_name));
1725               if (name == NULL || *name == '\0')
1726                 name = bfd_section_name (input_bfd, sec);
1727             }
1728
1729           switch (r)
1730             {
1731             case bfd_reloc_overflow:
1732               if (! ((*info->callbacks->reloc_overflow)
1733                      (info, (h ? &h->root : NULL), name, howto->name,
1734                       (bfd_vma) 0, input_bfd, input_section,
1735                       rel->r_offset)))
1736                 return FALSE;
1737               break;
1738
1739             case bfd_reloc_undefined:
1740               if (! ((*info->callbacks->undefined_symbol)
1741                      (info, name, input_bfd, input_section,
1742                       rel->r_offset, TRUE)))
1743                 return FALSE;
1744               break;
1745
1746             case bfd_reloc_outofrange:
1747               msg = _("internal error: out of range error");
1748               goto common_error;
1749
1750             case bfd_reloc_notsupported:
1751               msg = _("internal error: unsupported relocation error");
1752               goto common_error;
1753
1754             case bfd_reloc_dangerous:
1755               msg = _("internal error: dangerous relocation");
1756               goto common_error;
1757
1758             case bfd_reloc_gp_not_found:
1759               msg = _("could not locate special linker symbol __gp");
1760               goto common_error;
1761
1762             case bfd_reloc_ep_not_found:
1763               msg = _("could not locate special linker symbol __ep");
1764               goto common_error;
1765
1766             case bfd_reloc_ctbp_not_found:
1767               msg = _("could not locate special linker symbol __ctbp");
1768               goto common_error;
1769
1770             default:
1771               msg = _("internal error: unknown error");
1772               /* fall through */
1773
1774             common_error:
1775               if (!((*info->callbacks->warning)
1776                     (info, msg, name, input_bfd, input_section,
1777                      rel->r_offset)))
1778                 return FALSE;
1779               break;
1780             }
1781         }
1782     }
1783
1784   return TRUE;
1785 }
1786
1787 static bfd_boolean
1788 v850_elf_gc_sweep_hook (abfd, info, sec, relocs)
1789      bfd *abfd ATTRIBUTE_UNUSED;
1790      struct bfd_link_info *info ATTRIBUTE_UNUSED;
1791      asection *sec ATTRIBUTE_UNUSED;
1792      const Elf_Internal_Rela *relocs ATTRIBUTE_UNUSED;
1793 {
1794   /* No got and plt entries for v850-elf.  */
1795   return TRUE;
1796 }
1797
1798 static asection *
1799 v850_elf_gc_mark_hook (sec, info, rel, h, sym)
1800        asection *sec;
1801        struct bfd_link_info *info ATTRIBUTE_UNUSED;
1802        Elf_Internal_Rela *rel;
1803        struct elf_link_hash_entry *h;
1804        Elf_Internal_Sym *sym;
1805 {
1806   if (h != NULL)
1807     {
1808       switch (ELF32_R_TYPE (rel->r_info))
1809       {
1810       case R_V850_GNU_VTINHERIT:
1811       case R_V850_GNU_VTENTRY:
1812         break;
1813
1814       default:
1815         switch (h->root.type)
1816           {
1817           case bfd_link_hash_defined:
1818           case bfd_link_hash_defweak:
1819             return h->root.u.def.section;
1820
1821           case bfd_link_hash_common:
1822             return h->root.u.c.p->section;
1823
1824           default:
1825             break;
1826           }
1827        }
1828      }
1829    else
1830      return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
1831
1832   return NULL;
1833 }
1834
1835 /* Set the right machine number.  */
1836
1837 static bfd_boolean
1838 v850_elf_object_p (abfd)
1839      bfd *abfd;
1840 {
1841   switch (elf_elfheader (abfd)->e_flags & EF_V850_ARCH)
1842     {
1843     default:
1844     case E_V850_ARCH:
1845       bfd_default_set_arch_mach (abfd, bfd_arch_v850, bfd_mach_v850);
1846       break;
1847     case E_V850E_ARCH:
1848       bfd_default_set_arch_mach (abfd, bfd_arch_v850, bfd_mach_v850e);
1849       break;
1850     case E_V850E1_ARCH:
1851       bfd_default_set_arch_mach (abfd, bfd_arch_v850, bfd_mach_v850e1);
1852       break;
1853     }
1854   return TRUE;
1855 }
1856
1857 /* Store the machine number in the flags field.  */
1858
1859 static void
1860 v850_elf_final_write_processing (abfd, linker)
1861      bfd *abfd;
1862      bfd_boolean linker ATTRIBUTE_UNUSED;
1863 {
1864   unsigned long val;
1865
1866   switch (bfd_get_mach (abfd))
1867     {
1868     default:
1869     case bfd_mach_v850:  val = E_V850_ARCH; break;
1870     case bfd_mach_v850e: val = E_V850E_ARCH; break;
1871     case bfd_mach_v850e1: val = E_V850E1_ARCH;  break;
1872     }
1873
1874   elf_elfheader (abfd)->e_flags &=~ EF_V850_ARCH;
1875   elf_elfheader (abfd)->e_flags |= val;
1876 }
1877
1878 /* Function to keep V850 specific file flags.  */
1879
1880 static bfd_boolean
1881 v850_elf_set_private_flags (abfd, flags)
1882      bfd *abfd;
1883      flagword flags;
1884 {
1885   BFD_ASSERT (!elf_flags_init (abfd)
1886               || elf_elfheader (abfd)->e_flags == flags);
1887
1888   elf_elfheader (abfd)->e_flags = flags;
1889   elf_flags_init (abfd) = TRUE;
1890   return TRUE;
1891 }
1892
1893 /* Merge backend specific data from an object file
1894    to the output object file when linking.  */
1895 static bfd_boolean
1896 v850_elf_merge_private_bfd_data (ibfd, obfd)
1897      bfd *ibfd;
1898      bfd *obfd;
1899 {
1900   flagword out_flags;
1901   flagword in_flags;
1902
1903   if (   bfd_get_flavour (ibfd) != bfd_target_elf_flavour
1904       || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
1905     return TRUE;
1906
1907   in_flags = elf_elfheader (ibfd)->e_flags;
1908   out_flags = elf_elfheader (obfd)->e_flags;
1909
1910   if (! elf_flags_init (obfd))
1911     {
1912       /* If the input is the default architecture then do not
1913          bother setting the flags for the output architecture,
1914          instead allow future merges to do this.  If no future
1915          merges ever set these flags then they will retain their
1916          unitialised values, which surprise surprise, correspond
1917          to the default values.  */
1918       if (bfd_get_arch_info (ibfd)->the_default)
1919         return TRUE;
1920
1921       elf_flags_init (obfd) = TRUE;
1922       elf_elfheader (obfd)->e_flags = in_flags;
1923
1924       if (bfd_get_arch (obfd) == bfd_get_arch (ibfd)
1925           && bfd_get_arch_info (obfd)->the_default)
1926         return bfd_set_arch_mach (obfd, bfd_get_arch (ibfd), bfd_get_mach (ibfd));
1927
1928       return TRUE;
1929     }
1930
1931   /* Check flag compatibility.  */
1932   if (in_flags == out_flags)
1933     return TRUE;
1934
1935   if ((in_flags & EF_V850_ARCH) != (out_flags & EF_V850_ARCH)
1936       && (in_flags & EF_V850_ARCH) != E_V850_ARCH)
1937     {
1938       /* Allow v850e1 binaries to be linked with v850e binaries.
1939          Set the output binary to v850e.  */
1940       if ((in_flags & EF_V850_ARCH) == E_V850E1_ARCH
1941           && (out_flags & EF_V850_ARCH) == E_V850E_ARCH)
1942         return TRUE;
1943
1944       if ((in_flags & EF_V850_ARCH) == E_V850E_ARCH
1945           && (out_flags & EF_V850_ARCH) == E_V850E1_ARCH)
1946         {
1947           elf_elfheader (obfd)->e_flags =
1948             ((out_flags & ~ EF_V850_ARCH) | E_V850E_ARCH);
1949           return TRUE;
1950         }
1951
1952       _bfd_error_handler (_("%B: Architecture mismatch with previous modules"),
1953                           ibfd);
1954     }
1955
1956   return TRUE;
1957 }
1958
1959 /* Display the flags field.  */
1960
1961 static bfd_boolean
1962 v850_elf_print_private_bfd_data (abfd, ptr)
1963      bfd *abfd;
1964      PTR ptr;
1965 {
1966   FILE * file = (FILE *) ptr;
1967
1968   BFD_ASSERT (abfd != NULL && ptr != NULL);
1969
1970   _bfd_elf_print_private_bfd_data (abfd, ptr);
1971
1972   /* xgettext:c-format */
1973   fprintf (file, _("private flags = %lx: "), elf_elfheader (abfd)->e_flags);
1974
1975   switch (elf_elfheader (abfd)->e_flags & EF_V850_ARCH)
1976     {
1977     default:
1978     case E_V850_ARCH: fprintf (file, _("v850 architecture")); break;
1979     case E_V850E_ARCH:  fprintf (file, _("v850e architecture")); break;
1980     case E_V850E1_ARCH: fprintf (file, _("v850e1 architecture")); break;
1981     }
1982
1983   fputc ('\n', file);
1984
1985   return TRUE;
1986 }
1987
1988 /* V850 ELF uses four common sections.  One is the usual one, and the
1989    others are for (small) objects in one of the special data areas:
1990    small, tiny and zero.  All the objects are kept together, and then
1991    referenced via the gp register, the ep register or the r0 register
1992    respectively, which yields smaller, faster assembler code.  This
1993    approach is copied from elf32-mips.c.  */
1994
1995 static asection  v850_elf_scom_section;
1996 static asymbol   v850_elf_scom_symbol;
1997 static asymbol * v850_elf_scom_symbol_ptr;
1998 static asection  v850_elf_tcom_section;
1999 static asymbol   v850_elf_tcom_symbol;
2000 static asymbol * v850_elf_tcom_symbol_ptr;
2001 static asection  v850_elf_zcom_section;
2002 static asymbol   v850_elf_zcom_symbol;
2003 static asymbol * v850_elf_zcom_symbol_ptr;
2004
2005 /* Given a BFD section, try to locate the
2006    corresponding ELF section index.  */
2007
2008 static bfd_boolean
2009 v850_elf_section_from_bfd_section (abfd, sec, retval)
2010      bfd *abfd ATTRIBUTE_UNUSED;
2011      asection *sec;
2012      int *retval;
2013 {
2014   if (strcmp (bfd_get_section_name (abfd, sec), ".scommon") == 0)
2015     *retval = SHN_V850_SCOMMON;
2016   else if (strcmp (bfd_get_section_name (abfd, sec), ".tcommon") == 0)
2017     *retval = SHN_V850_TCOMMON;
2018   else if (strcmp (bfd_get_section_name (abfd, sec), ".zcommon") == 0)
2019     *retval = SHN_V850_ZCOMMON;
2020   else
2021     return FALSE;
2022
2023   return TRUE;
2024 }
2025
2026 /* Handle the special V850 section numbers that a symbol may use.  */
2027
2028 static void
2029 v850_elf_symbol_processing (abfd, asym)
2030      bfd *abfd;
2031      asymbol *asym;
2032 {
2033   elf_symbol_type * elfsym = (elf_symbol_type *) asym;
2034   unsigned int indx;
2035
2036   indx = elfsym->internal_elf_sym.st_shndx;
2037
2038   /* If the section index is an "ordinary" index, then it may
2039      refer to a v850 specific section created by the assembler.
2040      Check the section's type and change the index it matches.
2041
2042      FIXME: Should we alter the st_shndx field as well ?  */
2043
2044   if (indx < elf_numsections (abfd))
2045     switch (elf_elfsections(abfd)[indx]->sh_type)
2046       {
2047       case SHT_V850_SCOMMON:
2048         indx = SHN_V850_SCOMMON;
2049         break;
2050
2051       case SHT_V850_TCOMMON:
2052         indx = SHN_V850_TCOMMON;
2053         break;
2054
2055       case SHT_V850_ZCOMMON:
2056         indx = SHN_V850_ZCOMMON;
2057         break;
2058
2059       default:
2060         break;
2061       }
2062
2063   switch (indx)
2064     {
2065     case SHN_V850_SCOMMON:
2066       if (v850_elf_scom_section.name == NULL)
2067         {
2068           /* Initialize the small common section.  */
2069           v850_elf_scom_section.name           = ".scommon";
2070           v850_elf_scom_section.flags          = SEC_IS_COMMON | SEC_ALLOC | SEC_DATA;
2071           v850_elf_scom_section.output_section = & v850_elf_scom_section;
2072           v850_elf_scom_section.symbol         = & v850_elf_scom_symbol;
2073           v850_elf_scom_section.symbol_ptr_ptr = & v850_elf_scom_symbol_ptr;
2074           v850_elf_scom_symbol.name            = ".scommon";
2075           v850_elf_scom_symbol.flags           = BSF_SECTION_SYM;
2076           v850_elf_scom_symbol.section         = & v850_elf_scom_section;
2077           v850_elf_scom_symbol_ptr             = & v850_elf_scom_symbol;
2078         }
2079       asym->section = & v850_elf_scom_section;
2080       asym->value = elfsym->internal_elf_sym.st_size;
2081       break;
2082
2083     case SHN_V850_TCOMMON:
2084       if (v850_elf_tcom_section.name == NULL)
2085         {
2086           /* Initialize the tcommon section.  */
2087           v850_elf_tcom_section.name           = ".tcommon";
2088           v850_elf_tcom_section.flags          = SEC_IS_COMMON;
2089           v850_elf_tcom_section.output_section = & v850_elf_tcom_section;
2090           v850_elf_tcom_section.symbol         = & v850_elf_tcom_symbol;
2091           v850_elf_tcom_section.symbol_ptr_ptr = & v850_elf_tcom_symbol_ptr;
2092           v850_elf_tcom_symbol.name            = ".tcommon";
2093           v850_elf_tcom_symbol.flags           = BSF_SECTION_SYM;
2094           v850_elf_tcom_symbol.section         = & v850_elf_tcom_section;
2095           v850_elf_tcom_symbol_ptr             = & v850_elf_tcom_symbol;
2096         }
2097       asym->section = & v850_elf_tcom_section;
2098       asym->value = elfsym->internal_elf_sym.st_size;
2099       break;
2100
2101     case SHN_V850_ZCOMMON:
2102       if (v850_elf_zcom_section.name == NULL)
2103         {
2104           /* Initialize the zcommon section.  */
2105           v850_elf_zcom_section.name           = ".zcommon";
2106           v850_elf_zcom_section.flags          = SEC_IS_COMMON;
2107           v850_elf_zcom_section.output_section = & v850_elf_zcom_section;
2108           v850_elf_zcom_section.symbol         = & v850_elf_zcom_symbol;
2109           v850_elf_zcom_section.symbol_ptr_ptr = & v850_elf_zcom_symbol_ptr;
2110           v850_elf_zcom_symbol.name            = ".zcommon";
2111           v850_elf_zcom_symbol.flags           = BSF_SECTION_SYM;
2112           v850_elf_zcom_symbol.section         = & v850_elf_zcom_section;
2113           v850_elf_zcom_symbol_ptr             = & v850_elf_zcom_symbol;
2114         }
2115       asym->section = & v850_elf_zcom_section;
2116       asym->value = elfsym->internal_elf_sym.st_size;
2117       break;
2118     }
2119 }
2120
2121 /* Hook called by the linker routine which adds symbols from an object
2122    file.  We must handle the special v850 section numbers here.  */
2123
2124 static bfd_boolean
2125 v850_elf_add_symbol_hook (abfd, info, sym, namep, flagsp, secp, valp)
2126      bfd *abfd;
2127      struct bfd_link_info *info ATTRIBUTE_UNUSED;
2128      Elf_Internal_Sym *sym;
2129      const char **namep ATTRIBUTE_UNUSED;
2130      flagword *flagsp ATTRIBUTE_UNUSED;
2131      asection **secp;
2132      bfd_vma *valp;
2133 {
2134   unsigned int indx = sym->st_shndx;
2135
2136   /* If the section index is an "ordinary" index, then it may
2137      refer to a v850 specific section created by the assembler.
2138      Check the section's type and change the index it matches.
2139
2140      FIXME: Should we alter the st_shndx field as well ?  */
2141
2142   if (indx < elf_numsections (abfd))
2143     switch (elf_elfsections(abfd)[indx]->sh_type)
2144       {
2145       case SHT_V850_SCOMMON:
2146         indx = SHN_V850_SCOMMON;
2147         break;
2148
2149       case SHT_V850_TCOMMON:
2150         indx = SHN_V850_TCOMMON;
2151         break;
2152
2153       case SHT_V850_ZCOMMON:
2154         indx = SHN_V850_ZCOMMON;
2155         break;
2156
2157       default:
2158         break;
2159       }
2160
2161   switch (indx)
2162     {
2163     case SHN_V850_SCOMMON:
2164       *secp = bfd_make_section_old_way (abfd, ".scommon");
2165       (*secp)->flags |= SEC_IS_COMMON;
2166       *valp = sym->st_size;
2167       break;
2168
2169     case SHN_V850_TCOMMON:
2170       *secp = bfd_make_section_old_way (abfd, ".tcommon");
2171       (*secp)->flags |= SEC_IS_COMMON;
2172       *valp = sym->st_size;
2173       break;
2174
2175     case SHN_V850_ZCOMMON:
2176       *secp = bfd_make_section_old_way (abfd, ".zcommon");
2177       (*secp)->flags |= SEC_IS_COMMON;
2178       *valp = sym->st_size;
2179       break;
2180     }
2181
2182   return TRUE;
2183 }
2184
2185 static bfd_boolean
2186 v850_elf_link_output_symbol_hook (info, name, sym, input_sec, h)
2187      struct bfd_link_info *info ATTRIBUTE_UNUSED;
2188      const char *name ATTRIBUTE_UNUSED;
2189      Elf_Internal_Sym *sym;
2190      asection *input_sec;
2191      struct elf_link_hash_entry *h ATTRIBUTE_UNUSED;
2192 {
2193   /* If we see a common symbol, which implies a relocatable link, then
2194      if a symbol was in a special common section in an input file, mark
2195      it as a special common in the output file.  */
2196
2197   if (sym->st_shndx == SHN_COMMON)
2198     {
2199       if (strcmp (input_sec->name, ".scommon") == 0)
2200         sym->st_shndx = SHN_V850_SCOMMON;
2201       else if (strcmp (input_sec->name, ".tcommon") == 0)
2202         sym->st_shndx = SHN_V850_TCOMMON;
2203       else if (strcmp (input_sec->name, ".zcommon") == 0)
2204         sym->st_shndx = SHN_V850_ZCOMMON;
2205     }
2206
2207   return TRUE;
2208 }
2209
2210 static bfd_boolean
2211 v850_elf_section_from_shdr (bfd *abfd,
2212                             Elf_Internal_Shdr *hdr,
2213                             const char *name,
2214                             int shindex)
2215 {
2216   /* There ought to be a place to keep ELF backend specific flags, but
2217      at the moment there isn't one.  We just keep track of the
2218      sections by their name, instead.  */
2219
2220   if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
2221     return FALSE;
2222
2223   switch (hdr->sh_type)
2224     {
2225     case SHT_V850_SCOMMON:
2226     case SHT_V850_TCOMMON:
2227     case SHT_V850_ZCOMMON:
2228       if (! bfd_set_section_flags (abfd, hdr->bfd_section,
2229                                    (bfd_get_section_flags (abfd,
2230                                                            hdr->bfd_section)
2231                                     | SEC_IS_COMMON)))
2232         return FALSE;
2233     }
2234
2235   return TRUE;
2236 }
2237
2238 /* Set the correct type for a V850 ELF section.  We do this
2239    by the section name, which is a hack, but ought to work.  */
2240
2241 static bfd_boolean
2242 v850_elf_fake_sections (abfd, hdr, sec)
2243      bfd *abfd ATTRIBUTE_UNUSED;
2244      Elf_Internal_Shdr *hdr;
2245      asection *sec;
2246 {
2247   register const char * name;
2248
2249   name = bfd_get_section_name (abfd, sec);
2250
2251   if (strcmp (name, ".scommon") == 0)
2252     {
2253       hdr->sh_type = SHT_V850_SCOMMON;
2254     }
2255   else if (strcmp (name, ".tcommon") == 0)
2256     {
2257       hdr->sh_type = SHT_V850_TCOMMON;
2258     }
2259   else if (strcmp (name, ".zcommon") == 0)
2260     hdr->sh_type = SHT_V850_ZCOMMON;
2261
2262   return TRUE;
2263 }
2264
2265 /* Delete some bytes from a section while relaxing.  */
2266
2267 static bfd_boolean
2268 v850_elf_relax_delete_bytes (abfd, sec, addr, toaddr, count)
2269      bfd *abfd;
2270      asection *sec;
2271      bfd_vma addr;
2272      bfd_vma toaddr;
2273      int count;
2274 {
2275   Elf_Internal_Shdr *symtab_hdr;
2276   Elf32_External_Sym *extsyms;
2277   Elf32_External_Sym *esym;
2278   Elf32_External_Sym *esymend;
2279   int index;
2280   unsigned int sec_shndx;
2281   bfd_byte *contents;
2282   Elf_Internal_Rela *irel;
2283   Elf_Internal_Rela *irelend;
2284   struct elf_link_hash_entry *sym_hash;
2285   Elf_Internal_Shdr *shndx_hdr;
2286   Elf_External_Sym_Shndx *shndx;
2287
2288   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2289   extsyms = (Elf32_External_Sym *) symtab_hdr->contents;
2290
2291   sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
2292
2293   contents = elf_section_data (sec)->this_hdr.contents;
2294
2295   /* The deletion must stop at the next ALIGN reloc for an alignment
2296      power larger than the number of bytes we are deleting.  */
2297
2298   /* Actually delete the bytes.  */
2299 #if (DEBUG_RELAX & 2)
2300   fprintf (stderr, "relax_delete: contents: sec: %s  %p .. %p %x\n",
2301            sec->name, addr, toaddr, count );
2302 #endif
2303   memmove (contents + addr, contents + addr + count,
2304            toaddr - addr - count);
2305   memset (contents + toaddr-count, 0, count);
2306
2307   /* Adjust all the relocs.  */
2308   irel = elf_section_data (sec)->relocs;
2309   irelend = irel + sec->reloc_count;
2310   shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
2311   shndx = (Elf_External_Sym_Shndx *) shndx_hdr->contents;
2312
2313   for (; irel < irelend; irel++)
2314     {
2315       bfd_vma raddr, paddr, symval;
2316       Elf_Internal_Sym isym;
2317
2318       /* Get the new reloc address.  */
2319       raddr = irel->r_offset;
2320       if ((raddr >= (addr + count) && raddr < toaddr))
2321         irel->r_offset -= count;
2322
2323       if (raddr >= addr && raddr < addr + count)
2324         {
2325           irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
2326                                        (int) R_V850_NONE);
2327           continue;
2328         }
2329
2330       if (ELF32_R_TYPE (irel->r_info) == (int) R_V850_ALIGN)
2331         continue;
2332
2333       bfd_elf32_swap_symbol_in (abfd,
2334                                 extsyms + ELF32_R_SYM (irel->r_info),
2335                                 shndx ? shndx + ELF32_R_SYM (irel->r_info) : NULL,
2336                                 & isym);
2337
2338       if (isym.st_shndx != sec_shndx)
2339         continue;
2340
2341       /* Get the value of the symbol referred to by the reloc.  */
2342       if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
2343         {
2344           symval = isym.st_value;
2345 #if (DEBUG_RELAX & 2)
2346           {
2347             char * name = bfd_elf_string_from_elf_section
2348                            (abfd, symtab_hdr->sh_link, isym.st_name);
2349             fprintf (stderr,
2350                "relax_delete: local: sec: %s, sym: %s (%d), value: %x + %x + %x addend %x\n",
2351                sec->name, name, isym.st_name,
2352                sec->output_section->vma, sec->output_offset,
2353                isym.st_value, irel->r_addend);
2354           }
2355 #endif
2356         }
2357       else
2358         {
2359           unsigned long indx;
2360           struct elf_link_hash_entry * h;
2361
2362           /* An external symbol.  */
2363           indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
2364
2365           h = elf_sym_hashes (abfd) [indx];
2366           BFD_ASSERT (h != NULL);
2367
2368           symval = h->root.u.def.value;
2369 #if (DEBUG_RELAX & 2)
2370           fprintf (stderr,
2371                    "relax_delete: defined: sec: %s, name: %s, value: %x + %x + %x addend %x\n",
2372                    sec->name, h->root.root.string, h->root.u.def.value,
2373                    sec->output_section->vma, sec->output_offset, irel->r_addend);
2374 #endif
2375         }
2376
2377       paddr = symval + irel->r_addend;
2378
2379       if ( (symval >= addr + count && symval < toaddr)
2380           && (paddr < addr + count || paddr >= toaddr))
2381         irel->r_addend += count;
2382       else if (    (symval < addr + count || symval >= toaddr)
2383                 && (paddr >= addr + count && paddr < toaddr))
2384         irel->r_addend -= count;
2385     }
2386
2387   /* Adjust the local symbols defined in this section.  */
2388   esym = extsyms;
2389   esymend = esym + symtab_hdr->sh_info;
2390
2391   for (; esym < esymend; esym++, shndx = (shndx ? shndx + 1 : NULL))
2392     {
2393       Elf_Internal_Sym isym;
2394
2395       bfd_elf32_swap_symbol_in (abfd, esym, shndx, & isym);
2396
2397       if (isym.st_shndx == sec_shndx
2398           && isym.st_value >= addr + count
2399           && isym.st_value < toaddr)
2400         {
2401           isym.st_value -= count;
2402
2403           if (isym.st_value + isym.st_size >= toaddr)
2404             isym.st_size += count;
2405
2406           bfd_elf32_swap_symbol_out (abfd, & isym, esym, shndx);
2407         }
2408       else if (isym.st_shndx == sec_shndx
2409                && isym.st_value < addr + count)
2410         {
2411           if (isym.st_value+isym.st_size >= addr + count
2412               && isym.st_value+isym.st_size < toaddr)
2413             isym.st_size -= count;
2414
2415           if (isym.st_value >= addr
2416               && isym.st_value <  addr + count)
2417             isym.st_value = addr;
2418
2419           bfd_elf32_swap_symbol_out (abfd, & isym, esym, shndx);
2420         }
2421     }
2422
2423   /* Now adjust the global symbols defined in this section.  */
2424   esym = extsyms + symtab_hdr->sh_info;
2425   esymend = extsyms + (symtab_hdr->sh_size / sizeof (Elf32_External_Sym));
2426
2427   for (index = 0; esym < esymend; esym ++, index ++)
2428     {
2429       Elf_Internal_Sym isym;
2430
2431       bfd_elf32_swap_symbol_in (abfd, esym, shndx, & isym);
2432       sym_hash = elf_sym_hashes (abfd) [index];
2433
2434       if (isym.st_shndx == sec_shndx
2435           && ((sym_hash)->root.type == bfd_link_hash_defined
2436               || (sym_hash)->root.type == bfd_link_hash_defweak)
2437           && (sym_hash)->root.u.def.section == sec
2438           && (sym_hash)->root.u.def.value >= addr + count
2439           && (sym_hash)->root.u.def.value < toaddr)
2440         {
2441           if ((sym_hash)->root.u.def.value + isym.st_size >= toaddr)
2442             {
2443               isym.st_size += count;
2444               bfd_elf32_swap_symbol_out (abfd, & isym, esym, shndx);
2445             }
2446
2447           (sym_hash)->root.u.def.value -= count;
2448         }
2449       else if (isym.st_shndx == sec_shndx
2450                && ((sym_hash)->root.type == bfd_link_hash_defined
2451                    || (sym_hash)->root.type == bfd_link_hash_defweak)
2452                && (sym_hash)->root.u.def.section == sec
2453                && (sym_hash)->root.u.def.value < addr + count)
2454         {
2455           if ((sym_hash)->root.u.def.value+isym.st_size >= addr + count
2456               && (sym_hash)->root.u.def.value+isym.st_size < toaddr)
2457             isym.st_size -= count;
2458
2459           if ((sym_hash)->root.u.def.value >= addr
2460               && (sym_hash)->root.u.def.value < addr + count)
2461             (sym_hash)->root.u.def.value = addr;
2462
2463           bfd_elf32_swap_symbol_out (abfd, & isym, esym, shndx);
2464         }
2465
2466       if (shndx)
2467         ++ shndx;
2468     }
2469
2470   return TRUE;
2471 }
2472
2473 #define NOP_OPCODE      (0x0000)
2474 #define MOVHI           0x0640                          /* 4byte */
2475 #define MOVHI_MASK      0x07e0
2476 #define MOVHI_R1(insn)  ((insn) & 0x1f)                 /* 4byte */
2477 #define MOVHI_R2(insn)  ((insn) >> 11)
2478 #define MOVEA           0x0620                          /* 2byte */
2479 #define MOVEA_MASK      0x07e0
2480 #define MOVEA_R1(insn)  ((insn) & 0x1f)
2481 #define MOVEA_R2(insn)  ((insn) >> 11)
2482 #define JARL_4          0x00040780                              /* 4byte */
2483 #define JARL_4_MASK     0xFFFF07FF
2484 #define JARL_R2(insn)   (int)(((insn) & (~JARL_4_MASK)) >> 11)
2485 #define ADD_I           0x0240                                  /* 2byte */
2486 #define ADD_I_MASK      0x07e0
2487 #define ADD_I5(insn)    ((((insn) & 0x001f) << 11) >> 11)       /* 2byte */
2488 #define ADD_R2(insn)    ((insn) >> 11)
2489 #define JMP_R           0x0060                                  /* 2byte */
2490 #define JMP_R_MASK      0xFFE0
2491 #define JMP_R1(insn)    ((insn) & 0x1f)
2492
2493 static bfd_boolean
2494 v850_elf_relax_section (abfd, sec, link_info, again)
2495      bfd *abfd;
2496      asection *sec;
2497      struct bfd_link_info *link_info;
2498      bfd_boolean *again;
2499 {
2500   Elf_Internal_Shdr *symtab_hdr;
2501   Elf_Internal_Rela *internal_relocs;
2502   Elf_Internal_Rela *irel;
2503   Elf_Internal_Rela *irelend;
2504   Elf_Internal_Rela *irelalign = NULL;
2505   Elf_Internal_Sym *isymbuf = NULL;
2506   bfd_byte *contents = NULL;
2507   bfd_vma addr = 0;
2508   bfd_vma toaddr;
2509   int align_pad_size = 0;
2510   bfd_boolean result = TRUE;
2511
2512   *again = FALSE;
2513
2514   if (link_info->relocatable
2515       || (sec->flags & SEC_RELOC) == 0
2516       || sec->reloc_count == 0)
2517     return TRUE;
2518
2519   symtab_hdr = & elf_tdata (abfd)->symtab_hdr;
2520
2521   internal_relocs = (_bfd_elf_link_read_relocs
2522                      (abfd, sec, (PTR) NULL, (Elf_Internal_Rela *) NULL,
2523                       link_info->keep_memory));
2524   if (internal_relocs == NULL)
2525     goto error_return;
2526
2527   irelend = internal_relocs + sec->reloc_count;
2528
2529   while (addr < sec->size)
2530     {
2531       toaddr = sec->size;
2532
2533       for (irel = internal_relocs; irel < irelend; irel ++)
2534         if (ELF32_R_TYPE (irel->r_info) == (int) R_V850_ALIGN
2535             && irel->r_offset > addr
2536             && irel->r_offset < toaddr)
2537           toaddr = irel->r_offset;
2538
2539 #ifdef DEBUG_RELAX
2540       fprintf (stderr, "relax region 0x%x to 0x%x align pad %d\n",
2541                addr, toaddr, align_pad_size);
2542 #endif
2543       if (irelalign)
2544         {
2545           bfd_vma alignto;
2546           bfd_vma alignmoveto;
2547
2548           alignmoveto = BFD_ALIGN (addr - align_pad_size, 1 << irelalign->r_addend);
2549           alignto = BFD_ALIGN (addr, 1 << irelalign->r_addend);
2550
2551           if (alignmoveto < alignto)
2552             {
2553               unsigned int i;
2554
2555               align_pad_size = alignto - alignmoveto;
2556 #ifdef DEBUG_RELAX
2557               fprintf (stderr, "relax move region 0x%x to 0x%x delete size 0x%x\n",
2558                        alignmoveto, toaddr, align_pad_size);
2559 #endif
2560               if (!v850_elf_relax_delete_bytes (abfd, sec, alignmoveto,
2561                                                 toaddr, align_pad_size))
2562                 goto error_return;
2563
2564               for (i  = BFD_ALIGN (toaddr - align_pad_size, 1);
2565                    (i + 1) < toaddr; i += 2)
2566                 bfd_put_16 (abfd, NOP_OPCODE, contents + i);
2567
2568               addr = alignmoveto;
2569             }
2570           else
2571             align_pad_size = 0;
2572         }
2573
2574       for (irel = internal_relocs; irel < irelend; irel++)
2575         {
2576           bfd_vma laddr;
2577           bfd_vma addend;
2578           bfd_vma symval;
2579           int insn[5];
2580           int no_match = -1;
2581           Elf_Internal_Rela *hi_irelfn;
2582           Elf_Internal_Rela *lo_irelfn;
2583           Elf_Internal_Rela *irelcall;
2584           bfd_signed_vma foff;
2585
2586           if (! (irel->r_offset >= addr && irel->r_offset < toaddr
2587                  && (ELF32_R_TYPE (irel->r_info) == (int) R_V850_LONGCALL
2588                      || ELF32_R_TYPE (irel->r_info) == (int) R_V850_LONGJUMP)))
2589             continue;
2590
2591 #ifdef DEBUG_RELAX
2592           fprintf (stderr, "relax check r_info 0x%x r_offset 0x%x r_addend 0x%x\n",
2593                    irel->r_info,
2594                    irel->r_offset,
2595                    irel->r_addend );
2596 #endif
2597
2598           /* Get the section contents.  */
2599           if (contents == NULL)
2600             {
2601               if (elf_section_data (sec)->this_hdr.contents != NULL)
2602                 contents = elf_section_data (sec)->this_hdr.contents;
2603               else
2604                 {
2605                   if (!bfd_malloc_and_get_section (abfd, sec, &contents))
2606                     goto error_return;
2607                 }
2608             }
2609
2610           /* Read this BFD's local symbols if we haven't done so already.  */
2611           if (isymbuf == NULL && symtab_hdr->sh_info != 0)
2612             {
2613               isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
2614               if (isymbuf == NULL)
2615                 isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
2616                                                 symtab_hdr->sh_info, 0,
2617                                                 NULL, NULL, NULL);
2618               if (isymbuf == NULL)
2619                 goto error_return;
2620             }
2621
2622           laddr = irel->r_offset;
2623
2624           if (ELF32_R_TYPE (irel->r_info) == (int) R_V850_LONGCALL)
2625             {
2626               /* Check code for -mlong-calls output. */
2627               if (laddr + 16 <= (bfd_vma) sec->size)
2628                 {
2629                   insn[0] = bfd_get_16 (abfd, contents + laddr);
2630                   insn[1] = bfd_get_16 (abfd, contents + laddr + 4);
2631                   insn[2] = bfd_get_32 (abfd, contents + laddr + 8);
2632                   insn[3] = bfd_get_16 (abfd, contents + laddr + 12);
2633                   insn[4] = bfd_get_16 (abfd, contents + laddr + 14);
2634
2635                   if ((insn[0] & MOVHI_MASK) != MOVHI
2636                        || MOVHI_R1 (insn[0]) != 0)
2637                     no_match = 0;
2638
2639                   if (no_match < 0
2640                       && ((insn[1] & MOVEA_MASK) != MOVEA
2641                            || MOVHI_R2 (insn[0]) != MOVEA_R1 (insn[1])))
2642                     no_match = 1;
2643
2644                   if (no_match < 0
2645                       && (insn[2] & JARL_4_MASK) != JARL_4)
2646                     no_match = 2;
2647
2648                   if (no_match < 0
2649                       && ((insn[3] & ADD_I_MASK) != ADD_I
2650                            || ADD_I5 (insn[3]) != 4
2651                            || JARL_R2 (insn[2]) != ADD_R2 (insn[3])))
2652                     no_match = 3;
2653
2654                   if (no_match < 0
2655                       && ((insn[4] & JMP_R_MASK) != JMP_R
2656                            || MOVEA_R2 (insn[1]) != JMP_R1 (insn[4])))
2657                     no_match = 4;
2658                 }
2659               else
2660                 {
2661                   ((*_bfd_error_handler)
2662                    ("%s: 0x%lx: warning: R_V850_LONGCALL points to unrecognized insns",
2663                     bfd_get_filename (abfd), (unsigned long) irel->r_offset));
2664
2665                   continue;
2666                 }
2667
2668               if (no_match >= 0)
2669                 {
2670                   ((*_bfd_error_handler)
2671                    ("%s: 0x%lx: warning: R_V850_LONGCALL points to unrecognized insn 0x%x",
2672                     bfd_get_filename (abfd), (unsigned long) irel->r_offset+no_match, insn[no_match]));
2673
2674                   continue;
2675                 }
2676
2677               /* Get the reloc for the address from which the register is
2678                  being loaded.  This reloc will tell us which function is
2679                  actually being called.  */
2680               for (hi_irelfn = internal_relocs; hi_irelfn < irelend; hi_irelfn ++)
2681                 if (hi_irelfn->r_offset == laddr + 2
2682                     && ELF32_R_TYPE (hi_irelfn->r_info)
2683                         == (int) R_V850_HI16_S)
2684                   break;
2685
2686               for (lo_irelfn = internal_relocs; lo_irelfn < irelend; lo_irelfn ++)
2687                 if (lo_irelfn->r_offset == laddr + 6
2688                     && ELF32_R_TYPE (lo_irelfn->r_info)
2689                         == (int) R_V850_LO16)
2690                   break;
2691
2692               for (irelcall = internal_relocs; irelcall < irelend; irelcall ++)
2693                 if (irelcall->r_offset == laddr + 8
2694                     && ELF32_R_TYPE (irelcall->r_info)
2695                         == (int) R_V850_22_PCREL)
2696                   break;
2697
2698               if (   hi_irelfn == irelend
2699                   || lo_irelfn == irelend
2700                   || irelcall  == irelend)
2701                 {
2702                   ((*_bfd_error_handler)
2703                    ("%s: 0x%lx: warning: R_V850_LONGCALL points to unrecognized reloc",
2704                     bfd_get_filename (abfd), (unsigned long) irel->r_offset ));
2705
2706                   continue;
2707                 }
2708
2709               if (ELF32_R_SYM (irelcall->r_info) < symtab_hdr->sh_info)
2710                 {
2711                   Elf_Internal_Sym *  isym;
2712
2713                   /* A local symbol.  */
2714                   isym = isymbuf + ELF32_R_SYM (irelcall->r_info);
2715
2716                   symval = isym->st_value;
2717                 }
2718               else
2719                 {
2720                   unsigned long indx;
2721                   struct elf_link_hash_entry * h;
2722
2723                   /* An external symbol.  */
2724                   indx = ELF32_R_SYM (irelcall->r_info) - symtab_hdr->sh_info;
2725                   h = elf_sym_hashes (abfd)[indx];
2726                   BFD_ASSERT (h != NULL);
2727
2728                   if (   h->root.type != bfd_link_hash_defined
2729                       && h->root.type != bfd_link_hash_defweak)
2730                     /* This appears to be a reference to an undefined
2731                        symbol.  Just ignore it--it will be caught by the
2732                        regular reloc processing.  */
2733                     continue;
2734
2735                   symval = h->root.u.def.value;
2736                 }
2737
2738               if (symval + irelcall->r_addend != irelcall->r_offset + 4)
2739                 {
2740                   ((*_bfd_error_handler)
2741                    ("%s: 0x%lx: warning: R_V850_LONGCALL points to unrecognized reloc 0x%lx",
2742                     bfd_get_filename (abfd), (unsigned long) irel->r_offset, irelcall->r_offset ));
2743
2744                   continue;
2745                 }
2746
2747               /* Get the value of the symbol referred to by the reloc.  */
2748               if (ELF32_R_SYM (hi_irelfn->r_info) < symtab_hdr->sh_info)
2749                 {
2750                   Elf_Internal_Sym *isym;
2751                   asection *sym_sec;
2752
2753                   /* A local symbol.  */
2754                   isym = isymbuf + ELF32_R_SYM (hi_irelfn->r_info);
2755
2756                   if (isym->st_shndx == SHN_UNDEF)
2757                     sym_sec = bfd_und_section_ptr;
2758                   else if (isym->st_shndx == SHN_ABS)
2759                     sym_sec = bfd_abs_section_ptr;
2760                   else if (isym->st_shndx == SHN_COMMON)
2761                     sym_sec = bfd_com_section_ptr;
2762                   else
2763                     sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
2764                   symval = (isym->st_value
2765                             + sym_sec->output_section->vma
2766                             + sym_sec->output_offset);
2767                 }
2768               else
2769                 {
2770                   unsigned long indx;
2771                   struct elf_link_hash_entry *h;
2772
2773                   /* An external symbol.  */
2774                   indx = ELF32_R_SYM (hi_irelfn->r_info) - symtab_hdr->sh_info;
2775                   h = elf_sym_hashes (abfd)[indx];
2776                   BFD_ASSERT (h != NULL);
2777
2778                   if (   h->root.type != bfd_link_hash_defined
2779                       && h->root.type != bfd_link_hash_defweak)
2780                     /* This appears to be a reference to an undefined
2781                        symbol.  Just ignore it--it will be caught by the
2782                        regular reloc processing.  */
2783                     continue;
2784
2785                   symval = (h->root.u.def.value
2786                             + h->root.u.def.section->output_section->vma
2787                             + h->root.u.def.section->output_offset);
2788                 }
2789
2790               addend = irel->r_addend;
2791
2792               foff = (symval + addend
2793                       - (irel->r_offset
2794                          + sec->output_section->vma
2795                          + sec->output_offset
2796                          + 4));
2797 #ifdef DEBUG_RELAX
2798               fprintf (stderr, "relax longcall r_offset 0x%x ptr 0x%x symbol 0x%x addend 0x%x distance 0x%x\n",
2799                        irel->r_offset,
2800                        (irel->r_offset
2801                         + sec->output_section->vma
2802                         + sec->output_offset),
2803                        symval, addend, foff);
2804 #endif
2805
2806               if (foff < -0x100000 || foff >= 0x100000)
2807                 /* After all that work, we can't shorten this function call.  */
2808                 continue;
2809
2810               /* For simplicity of coding, we are going to modify the section
2811                  contents, the section relocs, and the BFD symbol table.  We
2812                  must tell the rest of the code not to free up this
2813                  information.  It would be possible to instead create a table
2814                  of changes which have to be made, as is done in coff-mips.c;
2815                  that would be more work, but would require less memory when
2816                  the linker is run.  */
2817               elf_section_data (sec)->relocs = internal_relocs;
2818               elf_section_data (sec)->this_hdr.contents = contents;
2819               symtab_hdr->contents = (bfd_byte *) isymbuf;
2820
2821               /* Replace the long call with a jarl.  */
2822               irel->r_info = ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info), R_V850_22_PCREL);
2823
2824               addend = 0;
2825
2826               if (ELF32_R_SYM (hi_irelfn->r_info) < symtab_hdr->sh_info)
2827                 /* If this needs to be changed because of future relaxing,
2828                    it will be handled here like other internal IND12W
2829                    relocs.  */
2830                 bfd_put_32 (abfd,
2831                             0x00000780 | (JARL_R2 (insn[2])<<11) | ((addend << 16) & 0xffff) | ((addend >> 16) & 0xf),
2832                             contents + irel->r_offset);
2833               else
2834                 /* We can't fully resolve this yet, because the external
2835                    symbol value may be changed by future relaxing.
2836                    We let the final link phase handle it.  */
2837                 bfd_put_32 (abfd, 0x00000780 | (JARL_R2 (insn[2])<<11),
2838                             contents + irel->r_offset);
2839
2840               hi_irelfn->r_info =
2841                 ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info), R_V850_NONE);
2842               lo_irelfn->r_info =
2843                 ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info), R_V850_NONE);
2844               irelcall->r_info =
2845                 ELF32_R_INFO (ELF32_R_SYM (irelcall->r_info), R_V850_NONE);
2846
2847               if (! v850_elf_relax_delete_bytes (abfd, sec,
2848                                                  irel->r_offset + 4, toaddr, 12))
2849                 goto error_return;
2850
2851               align_pad_size += 12;
2852             }
2853           else if (ELF32_R_TYPE (irel->r_info) == (int) R_V850_LONGJUMP)
2854             {
2855               /* Check code for -mlong-jumps output.  */
2856               if (laddr + 10 <= (bfd_vma) sec->size)
2857                 {
2858                   insn[0] = bfd_get_16 (abfd, contents + laddr);
2859                   insn[1] = bfd_get_16 (abfd, contents + laddr + 4);
2860                   insn[2] = bfd_get_16 (abfd, contents + laddr + 8);
2861
2862                   if ((insn[0] & MOVHI_MASK) != MOVHI
2863                        || MOVHI_R1 (insn[0]) != 0)
2864                     no_match = 0;
2865
2866                   if (no_match < 0
2867                       && ((insn[1] & MOVEA_MASK) != MOVEA
2868                            || MOVHI_R2 (insn[0]) != MOVEA_R1 (insn[1])))
2869                     no_match = 1;
2870
2871                   if (no_match < 0
2872                       && ((insn[2] & JMP_R_MASK) != JMP_R
2873                            || MOVEA_R2 (insn[1]) != JMP_R1 (insn[2])))
2874                     no_match = 4;
2875                 }
2876               else
2877                 {
2878                   ((*_bfd_error_handler)
2879                    ("%s: 0x%lx: warning: R_V850_LONGJUMP points to unrecognized insns",
2880                     bfd_get_filename (abfd), (unsigned long) irel->r_offset));
2881
2882                   continue;
2883                 }
2884
2885               if (no_match >= 0)
2886                 {
2887                   ((*_bfd_error_handler)
2888                    ("%s: 0x%lx: warning: R_V850_LONGJUMP points to unrecognized insn 0x%x",
2889                     bfd_get_filename (abfd), (unsigned long) irel->r_offset+no_match, insn[no_match]));
2890
2891                   continue;
2892                 }
2893
2894               /* Get the reloc for the address from which the register is
2895                  being loaded.  This reloc will tell us which function is
2896                  actually being called.  */
2897               for (hi_irelfn = internal_relocs; hi_irelfn < irelend; hi_irelfn ++)
2898                 if (hi_irelfn->r_offset == laddr + 2
2899                     && ELF32_R_TYPE (hi_irelfn->r_info) == (int) R_V850_HI16_S)
2900                   break;
2901
2902               for (lo_irelfn = internal_relocs; lo_irelfn < irelend; lo_irelfn ++)
2903                 if (lo_irelfn->r_offset == laddr + 6
2904                     && ELF32_R_TYPE (lo_irelfn->r_info) == (int) R_V850_LO16)
2905                   break;
2906
2907               if (   hi_irelfn == irelend
2908                   || lo_irelfn == irelend)
2909                 {
2910                   ((*_bfd_error_handler)
2911                    ("%s: 0x%lx: warning: R_V850_LONGJUMP points to unrecognized reloc",
2912                     bfd_get_filename (abfd), (unsigned long) irel->r_offset ));
2913
2914                   continue;
2915                 }
2916
2917               /* Get the value of the symbol referred to by the reloc.  */
2918               if (ELF32_R_SYM (hi_irelfn->r_info) < symtab_hdr->sh_info)
2919                 {
2920                   Elf_Internal_Sym *  isym;
2921                   asection *          sym_sec;
2922
2923                   /* A local symbol.  */
2924                   isym = isymbuf + ELF32_R_SYM (hi_irelfn->r_info);
2925
2926                   if (isym->st_shndx == SHN_UNDEF)
2927                     sym_sec = bfd_und_section_ptr;
2928                   else if (isym->st_shndx == SHN_ABS)
2929                     sym_sec = bfd_abs_section_ptr;
2930                   else if (isym->st_shndx == SHN_COMMON)
2931                     sym_sec = bfd_com_section_ptr;
2932                   else
2933                     sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
2934                   symval = (isym->st_value
2935                             + sym_sec->output_section->vma
2936                             + sym_sec->output_offset);
2937 #ifdef DEBUG_RELAX
2938                   {
2939                     char * name = bfd_elf_string_from_elf_section
2940                       (abfd, symtab_hdr->sh_link, isym->st_name);
2941
2942                     fprintf (stderr, "relax long jump local: sec: %s, sym: %s (%d), value: %x + %x + %x addend %x\n",
2943                              sym_sec->name, name, isym->st_name,
2944                              sym_sec->output_section->vma,
2945                              sym_sec->output_offset,
2946                              isym->st_value, irel->r_addend);
2947                   }
2948 #endif
2949                 }
2950               else
2951                 {
2952                   unsigned long indx;
2953                   struct elf_link_hash_entry * h;
2954
2955                   /* An external symbol.  */
2956                   indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
2957                   h = elf_sym_hashes (abfd)[indx];
2958                   BFD_ASSERT (h != NULL);
2959
2960                   if (   h->root.type != bfd_link_hash_defined
2961                       && h->root.type != bfd_link_hash_defweak)
2962                     /* This appears to be a reference to an undefined
2963                        symbol.  Just ignore it--it will be caught by the
2964                        regular reloc processing.  */
2965                     continue;
2966
2967                   symval = (h->root.u.def.value
2968                             + h->root.u.def.section->output_section->vma
2969                             + h->root.u.def.section->output_offset);
2970 #ifdef DEBUG_RELAX
2971                   fprintf (stderr,
2972                            "relax longjump defined: sec: %s, name: %s, value: %x + %x + %x addend %x\n",
2973                            sec->name, h->root.root.string, h->root.u.def.value,
2974                            sec->output_section->vma, sec->output_offset, irel->r_addend);
2975 #endif
2976                 }
2977
2978               addend = irel->r_addend;
2979
2980               foff = (symval + addend
2981                       - (irel->r_offset
2982                          + sec->output_section->vma
2983                          + sec->output_offset
2984                          + 4));
2985 #ifdef DEBUG_RELAX
2986               fprintf (stderr, "relax longjump r_offset 0x%x ptr 0x%x symbol 0x%x addend 0x%x distance 0x%x\n",
2987                        irel->r_offset,
2988                        (irel->r_offset
2989                         + sec->output_section->vma
2990                         + sec->output_offset),
2991                        symval, addend, foff);
2992 #endif
2993               if (foff < -0x100000 || foff >= 0x100000)
2994                 /* After all that work, we can't shorten this function call.  */
2995                 continue;
2996
2997               /* For simplicity of coding, we are going to modify the section
2998                  contents, the section relocs, and the BFD symbol table.  We
2999                  must tell the rest of the code not to free up this
3000                  information.  It would be possible to instead create a table
3001                  of changes which have to be made, as is done in coff-mips.c;
3002                  that would be more work, but would require less memory when
3003                  the linker is run.  */
3004               elf_section_data (sec)->relocs = internal_relocs;
3005               elf_section_data (sec)->this_hdr.contents = contents;
3006               symtab_hdr->contents = (bfd_byte *) isymbuf;
3007
3008               if (foff < -0x100 || foff >= 0x100)
3009                 {
3010                   /* Replace the long jump with a jr.  */
3011
3012                   irel->r_info =
3013                     ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_V850_22_PCREL);
3014
3015                   irel->r_addend = addend;
3016                   addend = 0;
3017
3018                   if (ELF32_R_SYM (hi_irelfn->r_info) < symtab_hdr->sh_info)
3019                     /* If this needs to be changed because of future relaxing,
3020                        it will be handled here like other internal IND12W
3021                        relocs.  */
3022                     bfd_put_32 (abfd,
3023                                 0x00000780 | ((addend << 15) & 0xffff0000) | ((addend >> 17) & 0xf),
3024                                 contents + irel->r_offset);
3025                   else
3026                     /* We can't fully resolve this yet, because the external
3027                        symbol value may be changed by future relaxing.
3028                        We let the final link phase handle it.  */
3029                     bfd_put_32 (abfd, 0x00000780, contents + irel->r_offset);
3030
3031                   hi_irelfn->r_info =
3032                         ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info), R_V850_NONE);
3033                   lo_irelfn->r_info =
3034                         ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info), R_V850_NONE);
3035                   if (!v850_elf_relax_delete_bytes (abfd, sec,
3036                                                     irel->r_offset + 4, toaddr, 6))
3037                     goto error_return;
3038
3039                   align_pad_size += 6;
3040                 }
3041               else
3042                 {
3043                   /* Replace the long jump with a br.  */
3044
3045                   irel->r_info =
3046                         ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_V850_9_PCREL);
3047
3048                   irel->r_addend = addend;
3049                   addend = 0;
3050
3051                   if (ELF32_R_SYM (hi_irelfn->r_info) < symtab_hdr->sh_info)
3052                     /* If this needs to be changed because of future relaxing,
3053                        it will be handled here like other internal IND12W
3054                        relocs.  */
3055                     bfd_put_16 (abfd,
3056                                 0x0585 | ((addend << 10) & 0xf800) | ((addend << 3) & 0x0070),
3057                                 contents + irel->r_offset);
3058                   else
3059                     /* We can't fully resolve this yet, because the external
3060                        symbol value may be changed by future relaxing.
3061                        We let the final link phase handle it.  */
3062                     bfd_put_16 (abfd, 0x0585, contents + irel->r_offset);
3063
3064                   hi_irelfn->r_info =
3065                         ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info), R_V850_NONE);
3066                   lo_irelfn->r_info =
3067                         ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info), R_V850_NONE);
3068                   if (!v850_elf_relax_delete_bytes (abfd, sec,
3069                                                     irel->r_offset + 2, toaddr, 8))
3070                     goto error_return;
3071
3072                   align_pad_size += 8;
3073                 }
3074             }
3075         }
3076
3077       irelalign = NULL;
3078       for (irel = internal_relocs; irel < irelend; irel++)
3079         {
3080           if (ELF32_R_TYPE (irel->r_info) == (int) R_V850_ALIGN
3081               && irel->r_offset == toaddr)
3082             {
3083               irel->r_offset -= align_pad_size;
3084
3085               if (irelalign == NULL || irelalign->r_addend > irel->r_addend)
3086                 irelalign = irel;
3087             }
3088         }
3089
3090       addr = toaddr;
3091     }
3092
3093   if (!irelalign)
3094     {
3095 #ifdef DEBUG_RELAX
3096       fprintf (stderr, "relax pad %d shorten %d -> %d\n",
3097                align_pad_size,
3098                sec->size,
3099                sec->size - align_pad_size);
3100 #endif
3101       sec->size -= align_pad_size;
3102     }
3103
3104  finish:
3105   if (internal_relocs != NULL
3106       && elf_section_data (sec)->relocs != internal_relocs)
3107     free (internal_relocs);
3108
3109   if (contents != NULL
3110       && elf_section_data (sec)->this_hdr.contents != (unsigned char *) contents)
3111     free (contents);
3112
3113   if (isymbuf != NULL
3114       && symtab_hdr->contents != (bfd_byte *) isymbuf)
3115     free (isymbuf);
3116
3117   return result;
3118
3119  error_return:
3120   result = FALSE;
3121   goto finish;
3122 }
3123
3124 static struct bfd_elf_special_section const v850_elf_special_sections[]=
3125 {
3126   { ".sdata",            6, -2, SHT_PROGBITS,     (SHF_ALLOC + SHF_WRITE
3127                                                    + SHF_V850_GPREL) },
3128   { ".rosdata",          8, -2, SHT_PROGBITS,     (SHF_ALLOC
3129                                                    + SHF_V850_GPREL) },
3130   { ".sbss",             5, -2, SHT_NOBITS,       (SHF_ALLOC + SHF_WRITE
3131                                                    + SHF_V850_GPREL) },
3132   { ".scommon",          8, -2, SHT_V850_SCOMMON, (SHF_ALLOC + SHF_WRITE
3133                                                    + SHF_V850_GPREL) },
3134   { ".tdata",            6, -2, SHT_PROGBITS,     (SHF_ALLOC + SHF_WRITE
3135                                                    + SHF_V850_EPREL) },
3136   { ".tbss",             5, -2, SHT_NOBITS,       (SHF_ALLOC + SHF_WRITE
3137                                                    + SHF_V850_EPREL) },
3138   { ".tcommon",          8, -2, SHT_V850_TCOMMON, (SHF_ALLOC + SHF_WRITE
3139                                                    + SHF_V850_R0REL) },
3140   { ".zdata",            6, -2, SHT_PROGBITS,     (SHF_ALLOC + SHF_WRITE
3141                                                    + SHF_V850_R0REL) },
3142   { ".rozdata",          8, -2, SHT_PROGBITS,     (SHF_ALLOC
3143                                                    + SHF_V850_R0REL) },
3144   { ".zbss",             5, -2, SHT_NOBITS,       (SHF_ALLOC + SHF_WRITE
3145                                                    + SHF_V850_R0REL) },
3146   { ".zcommon",          8, -2, SHT_V850_ZCOMMON, (SHF_ALLOC + SHF_WRITE
3147                                                    + SHF_V850_R0REL) },
3148   { ".call_table_data", 16,  0, SHT_PROGBITS,     (SHF_ALLOC
3149                                                    + SHF_WRITE) },
3150   { ".call_table_text", 16,  0, SHT_PROGBITS,     (SHF_ALLOC + SHF_WRITE
3151                                                    + SHF_EXECINSTR) },
3152   { NULL,               0,   0, 0,                0 }
3153 };
3154 \f
3155 #define TARGET_LITTLE_SYM                       bfd_elf32_v850_vec
3156 #define TARGET_LITTLE_NAME                      "elf32-v850"
3157 #define ELF_ARCH                                bfd_arch_v850
3158 #define ELF_MACHINE_CODE                        EM_V850
3159 #define ELF_MACHINE_ALT1                        EM_CYGNUS_V850
3160 #define ELF_MAXPAGESIZE                         0x1000
3161
3162 #define elf_info_to_howto                       v850_elf_info_to_howto_rela
3163 #define elf_info_to_howto_rel                   v850_elf_info_to_howto_rel
3164
3165 #define elf_backend_check_relocs                v850_elf_check_relocs
3166 #define elf_backend_relocate_section            v850_elf_relocate_section
3167 #define elf_backend_object_p                    v850_elf_object_p
3168 #define elf_backend_final_write_processing      v850_elf_final_write_processing
3169 #define elf_backend_section_from_bfd_section    v850_elf_section_from_bfd_section
3170 #define elf_backend_symbol_processing           v850_elf_symbol_processing
3171 #define elf_backend_add_symbol_hook             v850_elf_add_symbol_hook
3172 #define elf_backend_link_output_symbol_hook     v850_elf_link_output_symbol_hook
3173 #define elf_backend_section_from_shdr           v850_elf_section_from_shdr
3174 #define elf_backend_fake_sections               v850_elf_fake_sections
3175 #define elf_backend_gc_mark_hook                v850_elf_gc_mark_hook
3176 #define elf_backend_gc_sweep_hook               v850_elf_gc_sweep_hook
3177 #define elf_backend_special_sections            v850_elf_special_sections
3178
3179 #define elf_backend_can_gc_sections 1
3180 #define elf_backend_rela_normal 1
3181
3182 #define bfd_elf32_bfd_is_local_label_name       v850_elf_is_local_label_name
3183 #define bfd_elf32_bfd_reloc_type_lookup         v850_elf_reloc_type_lookup
3184 #define bfd_elf32_bfd_merge_private_bfd_data    v850_elf_merge_private_bfd_data
3185 #define bfd_elf32_bfd_set_private_flags         v850_elf_set_private_flags
3186 #define bfd_elf32_bfd_print_private_bfd_data    v850_elf_print_private_bfd_data
3187 #define bfd_elf32_bfd_relax_section             v850_elf_relax_section
3188
3189 #define elf_symbol_leading_char                 '_'
3190
3191 #include "elf32-target.h"