bfd/
[platform/upstream/binutils.git] / bfd / elf32-frv.c
1 /* FRV-specific support for 32-bit ELF.
2    Copyright 2002, 2003, 2004 Free Software Foundation, Inc.
3
4 This file is part of BFD, the Binary File Descriptor library.
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
19
20 #include "bfd.h"
21 #include "sysdep.h"
22 #include "libbfd.h"
23 #include "elf-bfd.h"
24 #include "elf/frv.h"
25 #include "elf/dwarf2.h"
26 #include "hashtab.h"
27
28 /* Forward declarations.  */
29 static bfd_reloc_status_type elf32_frv_relocate_lo16
30   PARAMS ((bfd *,  Elf_Internal_Rela *, bfd_byte *, bfd_vma));
31 static bfd_reloc_status_type elf32_frv_relocate_hi16
32   PARAMS ((bfd *,  Elf_Internal_Rela *, bfd_byte *, bfd_vma));
33 static bfd_reloc_status_type elf32_frv_relocate_label24
34   PARAMS ((bfd *, asection *, Elf_Internal_Rela *, bfd_byte *, bfd_vma));
35 static bfd_reloc_status_type elf32_frv_relocate_gprel12
36   PARAMS ((struct bfd_link_info *, bfd *, asection *, Elf_Internal_Rela *,
37            bfd_byte *, bfd_vma));
38 static bfd_reloc_status_type elf32_frv_relocate_gprelu12
39   PARAMS ((struct bfd_link_info *, bfd *, asection *, Elf_Internal_Rela *,
40            bfd_byte *, bfd_vma));
41 static bfd_reloc_status_type elf32_frv_relocate_gprello
42   PARAMS ((struct bfd_link_info *, bfd *, asection *, Elf_Internal_Rela *,
43            bfd_byte *, bfd_vma));
44 static bfd_reloc_status_type elf32_frv_relocate_gprelhi
45   PARAMS ((struct bfd_link_info *, bfd *, asection *, Elf_Internal_Rela *,
46            bfd_byte *, bfd_vma));
47 static reloc_howto_type *frv_reloc_type_lookup
48   PARAMS ((bfd *, bfd_reloc_code_real_type));
49 static void frv_info_to_howto_rela
50   PARAMS ((bfd *, arelent *, Elf_Internal_Rela *));
51 static bfd_boolean elf32_frv_relocate_section
52   PARAMS ((bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
53            Elf_Internal_Rela *, Elf_Internal_Sym *, asection **));
54 static bfd_boolean elf32_frv_add_symbol_hook
55   PARAMS (( bfd *, struct bfd_link_info *, const Elf_Internal_Sym *,
56             const char **, flagword *, asection **, bfd_vma *));
57 static bfd_reloc_status_type frv_final_link_relocate
58   PARAMS ((reloc_howto_type *, bfd *, asection *, bfd_byte *,
59            Elf_Internal_Rela *, bfd_vma));
60 static bfd_boolean elf32_frv_gc_sweep_hook
61   PARAMS ((bfd *, struct bfd_link_info *, asection *, const
62            Elf_Internal_Rela *));
63 static asection * elf32_frv_gc_mark_hook
64   PARAMS ((asection *, struct bfd_link_info *, Elf_Internal_Rela *,
65            struct elf_link_hash_entry *, Elf_Internal_Sym *));
66 static bfd_boolean elf32_frv_check_relocs
67   PARAMS ((bfd *, struct bfd_link_info *, asection *,
68            const Elf_Internal_Rela *));
69 static int elf32_frv_machine
70   PARAMS ((bfd *));
71 static bfd_boolean elf32_frv_object_p
72   PARAMS ((bfd *));
73 static bfd_boolean frv_elf_set_private_flags
74   PARAMS ((bfd *, flagword));
75 static bfd_boolean frv_elf_copy_private_bfd_data
76   PARAMS ((bfd *, bfd *));
77 static bfd_boolean frv_elf_merge_private_bfd_data
78   PARAMS ((bfd *, bfd *));
79 static bfd_boolean frv_elf_print_private_bfd_data
80   PARAMS ((bfd *, PTR));
81
82 static reloc_howto_type elf32_frv_howto_table [] =
83 {
84   /* This reloc does nothing.  */
85   HOWTO (R_FRV_NONE,            /* type */
86          0,                     /* rightshift */
87          2,                     /* size (0 = byte, 1 = short, 2 = long) */
88          32,                    /* bitsize */
89          FALSE,                 /* pc_relative */
90          0,                     /* bitpos */
91          complain_overflow_bitfield, /* complain_on_overflow */
92          bfd_elf_generic_reloc, /* special_function */
93          "R_FRV_NONE",          /* name */
94          FALSE,                 /* partial_inplace */
95          0,                     /* src_mask */
96          0,                     /* dst_mask */
97          FALSE),                /* pcrel_offset */
98
99   /* A 32 bit absolute relocation.  */
100   HOWTO (R_FRV_32,              /* 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_FRV_32",            /* name */
109          FALSE,                 /* partial_inplace */
110          0xffffffff,            /* src_mask */
111          0xffffffff,            /* dst_mask */
112          FALSE),                /* pcrel_offset */
113
114   /* A 16 bit pc-relative relocation.  */
115   HOWTO (R_FRV_LABEL16,         /* type */
116          2,                     /* rightshift */
117          2,                     /* size (0 = byte, 1 = short, 2 = long) */
118          16,                    /* bitsize */
119          TRUE,                  /* pc_relative */
120          0,                     /* bitpos */
121          complain_overflow_signed, /* complain_on_overflow */
122          bfd_elf_generic_reloc, /* special_function */
123          "R_FRV_LABEL16",       /* name */
124          FALSE,                 /* partial_inplace */
125          0xffff,                /* src_mask */
126          0xffff,                /* dst_mask */
127          TRUE),                 /* pcrel_offset */
128
129   /* A 24-bit pc-relative relocation.  */
130   HOWTO (R_FRV_LABEL24, /* type */
131          2,                     /* rightshift */
132          2,                     /* size (0 = byte, 1 = short, 2 = long) */
133          26,                    /* bitsize */
134          TRUE,                  /* pc_relative */
135          0,                     /* bitpos */
136          complain_overflow_bitfield, /* complain_on_overflow */
137          bfd_elf_generic_reloc, /* special_function */
138          "R_FRV_LABEL24",       /* name */
139          FALSE,                 /* partial_inplace */
140          0x7e03ffff,            /* src_mask */
141          0x7e03ffff,            /* dst_mask */
142          TRUE),                 /* pcrel_offset */
143
144   HOWTO (R_FRV_LO16,            /* type */
145          0,                     /* rightshift */
146          2,                     /* size (0 = byte, 1 = short, 2 = long) */
147          16,                    /* bitsize */
148          FALSE,                 /* pc_relative */
149          0,                     /* bitpos */
150          complain_overflow_dont, /* complain_on_overflow */
151          bfd_elf_generic_reloc, /* special_function */
152          "R_FRV_LO16",          /* name */
153          FALSE,                 /* partial_inplace */
154          0xffff,                /* src_mask */
155          0xffff,                /* dst_mask */
156          FALSE),                /* pcrel_offset */
157
158   HOWTO (R_FRV_HI16,            /* type */
159          0,                     /* rightshift */
160          2,                     /* size (0 = byte, 1 = short, 2 = long) */
161          16,                    /* bitsize */
162          FALSE,                 /* pc_relative */
163          0,                     /* bitpos */
164          complain_overflow_dont, /* complain_on_overflow */
165          bfd_elf_generic_reloc, /* special_function */
166          "R_FRV_HI16",          /* name */
167          FALSE,                 /* partial_inplace */
168          0xffff,                /* src_mask */
169          0xffff,                /* dst_mask */
170          FALSE),                /* pcrel_offset */
171
172   HOWTO (R_FRV_GPREL12,         /* type */
173          0,                     /* rightshift */
174          2,                     /* size (0 = byte, 1 = short, 2 = long) */
175          12,                    /* bitsize */
176          FALSE,                 /* pc_relative */
177          0,                     /* bitpos */
178          complain_overflow_dont, /* complain_on_overflow */
179          bfd_elf_generic_reloc, /* special_function */
180          "R_FRV_GPREL12",       /* name */
181          FALSE,                 /* partial_inplace */
182          0xfff,                 /* src_mask */
183          0xfff,                 /* dst_mask */
184          FALSE),                /* pcrel_offset */
185
186   HOWTO (R_FRV_GPRELU12,        /* type */
187          0,                     /* rightshift */
188          2,                     /* size (0 = byte, 1 = short, 2 = long) */
189          12,                    /* bitsize */
190          FALSE,                 /* pc_relative */
191          0,                     /* bitpos */
192          complain_overflow_dont, /* complain_on_overflow */
193          bfd_elf_generic_reloc, /* special_function */
194          "R_FRV_GPRELU12",      /* name */
195          FALSE,                 /* partial_inplace */
196          0xfff,                 /* src_mask */
197          0x3f03f,               /* dst_mask */
198          FALSE),                /* pcrel_offset */
199
200   HOWTO (R_FRV_GPREL32,         /* type */
201          0,                     /* rightshift */
202          2,                     /* size (0 = byte, 1 = short, 2 = long) */
203          32,                    /* bitsize */
204          FALSE,                 /* pc_relative */
205          0,                     /* bitpos */
206          complain_overflow_dont, /* complain_on_overflow */
207          bfd_elf_generic_reloc, /* special_function */
208          "R_FRV_GPREL32",       /* name */
209          FALSE,                 /* partial_inplace */
210          0xffffffff,            /* src_mask */
211          0xffffffff,            /* dst_mask */
212          FALSE),                /* pcrel_offset */
213
214   HOWTO (R_FRV_GPRELHI,         /* type */
215          0,                     /* rightshift */
216          2,                     /* size (0 = byte, 1 = short, 2 = long) */
217          16,                    /* bitsize */
218          FALSE,                 /* pc_relative */
219          0,                     /* bitpos */
220          complain_overflow_dont, /* complain_on_overflow */
221          bfd_elf_generic_reloc, /* special_function */
222          "R_FRV_GPRELHI",       /* name */
223          FALSE,                 /* partial_inplace */
224          0xffff,                        /* src_mask */
225          0xffff,                /* dst_mask */
226          FALSE),                /* pcrel_offset */
227
228   HOWTO (R_FRV_GPRELLO,         /* type */
229          0,                     /* rightshift */
230          2,                     /* size (0 = byte, 1 = short, 2 = long) */
231          16,                    /* bitsize */
232          FALSE,                 /* pc_relative */
233          0,                     /* bitpos */
234          complain_overflow_dont, /* complain_on_overflow */
235          bfd_elf_generic_reloc, /* special_function */
236          "R_FRV_GPRELLO",       /* name */
237          FALSE,                 /* partial_inplace */
238          0xffff,                        /* src_mask */
239          0xffff,                /* dst_mask */
240          FALSE),                /* pcrel_offset */
241
242   /* A 12-bit signed operand with the GOT offset for the address of
243      the symbol.  */
244   HOWTO (R_FRV_GOT12,           /* type */
245          0,                     /* rightshift */
246          2,                     /* size (0 = byte, 1 = short, 2 = long) */
247          12,                    /* bitsize */
248          FALSE,                 /* pc_relative */
249          0,                     /* bitpos */
250          complain_overflow_signed, /* complain_on_overflow */
251          bfd_elf_generic_reloc, /* special_function */
252          "R_FRV_GOT12",         /* name */
253          FALSE,                 /* partial_inplace */
254          0xfff,                 /* src_mask */
255          0xfff,                 /* dst_mask */
256          FALSE),                /* pcrel_offset */
257
258   /* The upper 16 bits of the GOT offset for the address of the
259      symbol.  */
260   HOWTO (R_FRV_GOTHI,           /* type */
261          0,                     /* rightshift */
262          2,                     /* size (0 = byte, 1 = short, 2 = long) */
263          16,                    /* bitsize */
264          FALSE,                 /* pc_relative */
265          0,                     /* bitpos */
266          complain_overflow_dont, /* complain_on_overflow */
267          bfd_elf_generic_reloc, /* special_function */
268          "R_FRV_GOTHI",         /* name */
269          FALSE,                 /* partial_inplace */
270          0xffff,                        /* src_mask */
271          0xffff,                /* dst_mask */
272          FALSE),                /* pcrel_offset */
273
274   /* The lower 16 bits of the GOT offset for the address of the
275      symbol.  */
276   HOWTO (R_FRV_GOTLO,           /* type */
277          0,                     /* rightshift */
278          2,                     /* size (0 = byte, 1 = short, 2 = long) */
279          16,                    /* bitsize */
280          FALSE,                 /* pc_relative */
281          0,                     /* bitpos */
282          complain_overflow_dont, /* complain_on_overflow */
283          bfd_elf_generic_reloc, /* special_function */
284          "R_FRV_GOTLO",         /* name */
285          FALSE,                 /* partial_inplace */
286          0xffff,                /* src_mask */
287          0xffff,                /* dst_mask */
288          FALSE),                /* pcrel_offset */
289
290   /* The 32-bit address of the canonical descriptor of a function.  */
291   HOWTO (R_FRV_FUNCDESC,        /* type */
292          0,                     /* rightshift */
293          2,                     /* size (0 = byte, 1 = short, 2 = long) */
294          32,                    /* bitsize */
295          FALSE,                 /* pc_relative */
296          0,                     /* bitpos */
297          complain_overflow_bitfield, /* complain_on_overflow */
298          bfd_elf_generic_reloc, /* special_function */
299          "R_FRV_FUNCDESC",      /* name */
300          FALSE,                 /* partial_inplace */
301          0xffffffff,            /* src_mask */
302          0xffffffff,            /* dst_mask */
303          FALSE),                /* pcrel_offset */
304
305   /* A 12-bit signed operand with the GOT offset for the address of
306      canonical descriptor of a function.  */
307   HOWTO (R_FRV_FUNCDESC_GOT12,  /* type */
308          0,                     /* rightshift */
309          2,                     /* size (0 = byte, 1 = short, 2 = long) */
310          12,                    /* bitsize */
311          FALSE,                 /* pc_relative */
312          0,                     /* bitpos */
313          complain_overflow_signed, /* complain_on_overflow */
314          bfd_elf_generic_reloc, /* special_function */
315          "R_FRV_FUNCDESC_GOT12", /* name */
316          FALSE,                 /* partial_inplace */
317          0xfff,                 /* src_mask */
318          0xfff,                 /* dst_mask */
319          FALSE),                /* pcrel_offset */
320
321   /* The upper 16 bits of the GOT offset for the address of the
322      canonical descriptor of a function.  */
323   HOWTO (R_FRV_FUNCDESC_GOTHI,  /* type */
324          0,                     /* rightshift */
325          2,                     /* size (0 = byte, 1 = short, 2 = long) */
326          16,                    /* bitsize */
327          FALSE,                 /* pc_relative */
328          0,                     /* bitpos */
329          complain_overflow_dont, /* complain_on_overflow */
330          bfd_elf_generic_reloc, /* special_function */
331          "R_FRV_FUNCDESC_GOTHI", /* name */
332          FALSE,                 /* partial_inplace */
333          0xffff,                /* src_mask */
334          0xffff,                /* dst_mask */
335          FALSE),                /* pcrel_offset */
336
337   /* The lower 16 bits of the GOT offset for the address of the
338      canonical descriptor of a function.  */
339   HOWTO (R_FRV_FUNCDESC_GOTLO,  /* type */
340          0,                     /* rightshift */
341          2,                     /* size (0 = byte, 1 = short, 2 = long) */
342          16,                    /* bitsize */
343          FALSE,                 /* pc_relative */
344          0,                     /* bitpos */
345          complain_overflow_dont, /* complain_on_overflow */
346          bfd_elf_generic_reloc, /* special_function */
347          "R_FRV_FUNCDESC_GOTLO", /* name */
348          FALSE,                 /* partial_inplace */
349          0xffff,                /* src_mask */
350          0xffff,                /* dst_mask */
351          FALSE),                /* pcrel_offset */
352
353   /* The 32-bit address of the canonical descriptor of a function.  */
354   HOWTO (R_FRV_FUNCDESC_VALUE,  /* type */
355          0,                     /* rightshift */
356          2,                     /* size (0 = byte, 1 = short, 2 = long) */
357          64,                    /* bitsize */
358          FALSE,                 /* pc_relative */
359          0,                     /* bitpos */
360          complain_overflow_bitfield, /* complain_on_overflow */
361          bfd_elf_generic_reloc, /* special_function */
362          "R_FRV_FUNCDESC_VALUE", /* name */
363          FALSE,                 /* partial_inplace */
364          0xffffffff,            /* src_mask */
365          0xffffffff,            /* dst_mask */
366          FALSE),                /* pcrel_offset */
367
368   /* A 12-bit signed operand with the GOT offset for the address of
369      canonical descriptor of a function.  */
370   HOWTO (R_FRV_FUNCDESC_GOTOFF12, /* type */
371          0,                     /* rightshift */
372          2,                     /* size (0 = byte, 1 = short, 2 = long) */
373          12,                    /* bitsize */
374          FALSE,                 /* pc_relative */
375          0,                     /* bitpos */
376          complain_overflow_signed, /* complain_on_overflow */
377          bfd_elf_generic_reloc, /* special_function */
378          "R_FRV_FUNCDESC_GOTOFF12", /* name */
379          FALSE,                 /* partial_inplace */
380          0xfff,                 /* src_mask */
381          0xfff,                 /* dst_mask */
382          FALSE),                /* pcrel_offset */
383
384   /* The upper 16 bits of the GOT offset for the address of the
385      canonical descriptor of a function.  */
386   HOWTO (R_FRV_FUNCDESC_GOTOFFHI, /* type */
387          0,                     /* rightshift */
388          2,                     /* size (0 = byte, 1 = short, 2 = long) */
389          16,                    /* bitsize */
390          FALSE,                 /* pc_relative */
391          0,                     /* bitpos */
392          complain_overflow_dont, /* complain_on_overflow */
393          bfd_elf_generic_reloc, /* special_function */
394          "R_FRV_FUNCDESC_GOTOFFHI", /* name */
395          FALSE,                 /* partial_inplace */
396          0xffff,                /* src_mask */
397          0xffff,                /* dst_mask */
398          FALSE),                /* pcrel_offset */
399
400   /* The lower 16 bits of the GOT offset for the address of the
401      canonical descriptor of a function.  */
402   HOWTO (R_FRV_FUNCDESC_GOTOFFLO, /* type */
403          0,                     /* rightshift */
404          2,                     /* size (0 = byte, 1 = short, 2 = long) */
405          16,                    /* bitsize */
406          FALSE,                 /* pc_relative */
407          0,                     /* bitpos */
408          complain_overflow_dont, /* complain_on_overflow */
409          bfd_elf_generic_reloc, /* special_function */
410          "R_FRV_FUNCDESC_GOTOFFLO", /* name */
411          FALSE,                 /* partial_inplace */
412          0xffff,                /* src_mask */
413          0xffff,                /* dst_mask */
414          FALSE),                /* pcrel_offset */
415
416   /* A 12-bit signed operand with the GOT offset for the address of
417      the symbol.  */
418   HOWTO (R_FRV_GOTOFF12,        /* type */
419          0,                     /* rightshift */
420          2,                     /* size (0 = byte, 1 = short, 2 = long) */
421          12,                    /* bitsize */
422          FALSE,                 /* pc_relative */
423          0,                     /* bitpos */
424          complain_overflow_signed, /* complain_on_overflow */
425          bfd_elf_generic_reloc, /* special_function */
426          "R_FRV_GOTOFF12",      /* name */
427          FALSE,                 /* partial_inplace */
428          0xfff,                 /* src_mask */
429          0xfff,                 /* dst_mask */
430          FALSE),                /* pcrel_offset */
431
432   /* The upper 16 bits of the GOT offset for the address of the
433      symbol.  */
434   HOWTO (R_FRV_GOTOFFHI,        /* type */
435          0,                     /* rightshift */
436          2,                     /* size (0 = byte, 1 = short, 2 = long) */
437          16,                    /* bitsize */
438          FALSE,                 /* pc_relative */
439          0,                     /* bitpos */
440          complain_overflow_dont, /* complain_on_overflow */
441          bfd_elf_generic_reloc, /* special_function */
442          "R_FRV_GOTOFFHI",      /* name */
443          FALSE,                 /* partial_inplace */
444          0xffff,                /* src_mask */
445          0xffff,                /* dst_mask */
446          FALSE),                /* pcrel_offset */
447
448   /* The lower 16 bits of the GOT offset for the address of the
449      symbol.  */
450   HOWTO (R_FRV_GOTOFFLO,        /* type */
451          0,                     /* rightshift */
452          2,                     /* size (0 = byte, 1 = short, 2 = long) */
453          16,                    /* bitsize */
454          FALSE,                 /* pc_relative */
455          0,                     /* bitpos */
456          complain_overflow_dont, /* complain_on_overflow */
457          bfd_elf_generic_reloc, /* special_function */
458          "R_FRV_GOTOFFLO",      /* name */
459          FALSE,                 /* partial_inplace */
460          0xffff,                /* src_mask */
461          0xffff,                /* dst_mask */
462          FALSE),                /* pcrel_offset */
463
464 };
465
466 /* GNU extension to record C++ vtable hierarchy.  */
467 static reloc_howto_type elf32_frv_vtinherit_howto =
468   HOWTO (R_FRV_GNU_VTINHERIT,   /* type */
469          0,                     /* rightshift */
470          2,                     /* size (0 = byte, 1 = short, 2 = long) */
471          0,                     /* bitsize */
472          FALSE,                 /* pc_relative */
473          0,                     /* bitpos */
474          complain_overflow_dont, /* complain_on_overflow */
475          NULL,                  /* special_function */
476          "R_FRV_GNU_VTINHERIT", /* name */
477          FALSE,                 /* partial_inplace */
478          0,                     /* src_mask */
479          0,                     /* dst_mask */
480          FALSE);                /* pcrel_offset */
481
482   /* GNU extension to record C++ vtable member usage.  */
483 static reloc_howto_type elf32_frv_vtentry_howto =
484   HOWTO (R_FRV_GNU_VTENTRY,     /* type */
485          0,                     /* rightshift */
486          2,                     /* size (0 = byte, 1 = short, 2 = long) */
487          0,                     /* bitsize */
488          FALSE,                 /* pc_relative */
489          0,                     /* bitpos */
490          complain_overflow_dont, /* complain_on_overflow */
491          _bfd_elf_rel_vtable_reloc_fn,  /* special_function */
492          "R_FRV_GNU_VTENTRY",   /* name */
493          FALSE,                 /* partial_inplace */
494          0,                     /* src_mask */
495          0,                     /* dst_mask */
496          FALSE);                /* pcrel_offset */
497
498 /* The following 3 relocations are REL.  The only difference to the
499    entries in the table above are that partial_inplace is TRUE.  */
500 static reloc_howto_type elf32_frv_rel_32_howto =
501   HOWTO (R_FRV_32,              /* type */
502          0,                     /* rightshift */
503          2,                     /* size (0 = byte, 1 = short, 2 = long) */
504          32,                    /* bitsize */
505          FALSE,                 /* pc_relative */
506          0,                     /* bitpos */
507          complain_overflow_bitfield, /* complain_on_overflow */
508          bfd_elf_generic_reloc, /* special_function */
509          "R_FRV_32",            /* name */
510          TRUE,                  /* partial_inplace */
511          0xffffffff,            /* src_mask */
512          0xffffffff,            /* dst_mask */
513          FALSE);                /* pcrel_offset */
514
515 static reloc_howto_type elf32_frv_rel_funcdesc_howto =
516   HOWTO (R_FRV_FUNCDESC,        /* type */
517          0,                     /* rightshift */
518          2,                     /* size (0 = byte, 1 = short, 2 = long) */
519          32,                    /* bitsize */
520          FALSE,                 /* pc_relative */
521          0,                     /* bitpos */
522          complain_overflow_bitfield, /* complain_on_overflow */
523          bfd_elf_generic_reloc, /* special_function */
524          "R_FRV_FUNCDESC",      /* name */
525          TRUE,                  /* partial_inplace */
526          0xffffffff,            /* src_mask */
527          0xffffffff,            /* dst_mask */
528          FALSE);                /* pcrel_offset */
529
530 static reloc_howto_type elf32_frv_rel_funcdesc_value_howto =
531   HOWTO (R_FRV_FUNCDESC_VALUE,  /* type */
532          0,                     /* rightshift */
533          2,                     /* size (0 = byte, 1 = short, 2 = long) */
534          64,                    /* bitsize */
535          FALSE,                 /* pc_relative */
536          0,                     /* bitpos */
537          complain_overflow_bitfield, /* complain_on_overflow */
538          bfd_elf_generic_reloc, /* special_function */
539          "R_FRV_FUNCDESC_VALUE", /* name */
540          TRUE,                  /* partial_inplace */
541          0xffffffff,            /* src_mask */
542          0xffffffff,            /* dst_mask */
543          FALSE);                /* pcrel_offset */
544
545 \f
546 /* Map BFD reloc types to FRV ELF reloc types.  */
547 #if 0
548 struct frv_reloc_map
549 {
550   unsigned int bfd_reloc_val;
551   unsigned int frv_reloc_val;
552 };
553
554 static const struct frv_reloc_map frv_reloc_map [] =
555 {
556   { BFD_RELOC_NONE,           R_FRV_NONE },
557   { BFD_RELOC_32,             R_FRV_32 },
558   { BFD_RELOC_FRV_LABEL16,    R_FRV_LABEL16 },
559   { BFD_RELOC_FRV_LABEL24,    R_FRV_LABEL24 },
560   { BFD_RELOC_FRV_LO16,       R_FRV_LO16 },
561   { BFD_RELOC_FRV_HI16,       R_FRV_HI16 },
562   { BFD_RELOC_FRV_GPREL12,    R_FRV_GPREL12 },
563   { BFD_RELOC_FRV_GPRELU12,   R_FRV_GPRELU12 },
564   { BFD_RELOC_FRV_GPREL32,    R_FRV_GPREL32 },
565   { BFD_RELOC_FRV_GPRELHI,    R_FRV_GPRELHI },
566   { BFD_RELOC_FRV_GPRELLO,    R_FRV_GPRELLO },
567   { BFD_RELOC_FRV_GOT12,      R_FRV_GOT12 },
568   { BFD_RELOC_FRV_GOTHI,      R_FRV_GOTHI },
569   { BFD_RELOC_FRV_GOTLO,      R_FRV_GOTLO },
570   { BFD_RELOC_FRV_FUNCDESC,   R_FRV_FUNCDESC },
571   { BFD_RELOC_FRV_FUNCDESC_GOT12, R_FRV_FUNCDESC_GOT12 },
572   { BFD_RELOC_FRV_FUNCDESC_GOTHI, R_FRV_FUNCDESC_GOTHI },
573   { BFD_RELOC_FRV_FUNCDESC_GOTLO, R_FRV_FUNCDESC_GOTLO },
574   { BFD_RELOC_FRV_FUNCDESC_VALUE, R_FRV_FUNCDESC_VALUE },
575   { BFD_RELOC_FRV_FUNCDESC_GOTOFF12, R_FRV_FUNCDESC_GOTOFF12 },
576   { BFD_RELOC_FRV_FUNCDESC_GOTOFFHI, R_FRV_FUNCDESC_GOTOFFHI },
577   { BFD_RELOC_FRV_FUNCDESC_GOTOFFLO, R_FRV_FUNCDESC_GOTOFFLO },
578   { BFD_RELOC_FRV_GOTOFF12,   R_FRV_GOTOFF12 },
579   { BFD_RELOC_FRV_GOTOFFHI,   R_FRV_GOTOFFHI },
580   { BFD_RELOC_FRV_GOTOFFLO,   R_FRV_GOTOFFLO },
581   { BFD_RELOC_VTABLE_INHERIT, R_FRV_GNU_VTINHERIT },
582   { BFD_RELOC_VTABLE_ENTRY,   R_FRV_GNU_VTENTRY },
583 };
584 #endif
585
586
587 /* An extension of the elf hash table data structure, containing some
588    additional FRV-specific data.  */
589 struct frv_elf_link_hash_table
590 {
591   struct elf_link_hash_table elf;
592
593   /* A pointer to the .got section.  */
594   asection *sgot;
595   /* A pointer to the .rel.got section.  */
596   asection *sgotrel;
597   /* A pointer to the .rofixup section.  */
598   asection *sgotfixup;
599   /* A pointer to the .plt section.  */
600   asection *splt;
601   /* A pointer to the .rel.plt section.  */
602   asection *spltrel;
603   /* GOT base offset.  */
604   bfd_vma got0;
605   /* Location of the first non-lazy PLT entry, i.e., the number of
606      bytes taken by lazy PLT entries.  */
607   bfd_vma plt0;
608   /* A hash table holding information about which symbols were
609      referenced with which PIC-related relocations.  */
610   struct htab *relocs_info;
611 };
612
613 /* Get the FRV ELF linker hash table from a link_info structure.  */
614
615 #define frv_hash_table(info) \
616   ((struct frv_elf_link_hash_table *) ((info)->hash))
617
618 #define frv_got_section(info) \
619   (frv_hash_table (info)->sgot)
620 #define frv_gotrel_section(info) \
621   (frv_hash_table (info)->sgotrel)
622 #define frv_gotfixup_section(info) \
623   (frv_hash_table (info)->sgotfixup)
624 #define frv_plt_section(info) \
625   (frv_hash_table (info)->splt)
626 #define frv_pltrel_section(info) \
627   (frv_hash_table (info)->spltrel)
628 #define frv_relocs_info(info) \
629   (frv_hash_table (info)->relocs_info)
630 #define frv_got_initial_offset(info) \
631   (frv_hash_table (info)->got0)
632 #define frv_plt_initial_offset(info) \
633   (frv_hash_table (info)->plt0)
634
635 /* Create an FRV ELF linker hash table.  */
636
637 static struct bfd_link_hash_table *
638 frv_elf_link_hash_table_create (bfd *abfd)
639 {
640   struct frv_elf_link_hash_table *ret;
641   bfd_size_type amt = sizeof (struct frv_elf_link_hash_table);
642
643   ret = bfd_zalloc (abfd, amt);
644   if (ret == NULL)
645     return NULL;
646
647   if (! _bfd_elf_link_hash_table_init (&ret->elf, abfd,
648                                        _bfd_elf_link_hash_newfunc))
649     {
650       free (ret);
651       return NULL;
652     }
653
654   return &ret->elf.root;
655 }
656
657 /* Decide whether a reference to a symbol can be resolved locally or
658    not.  If the symbol is protected, we want the local address, but
659    its function descriptor must be assigned by the dynamic linker.  */
660 #define FRV_SYM_LOCAL(INFO, H) \
661   (_bfd_elf_symbol_refs_local_p ((H), (INFO), 1) \
662    || ! elf_hash_table (INFO)->dynamic_sections_created \
663    || (/* The condition below is an ugly hack to get .scommon data to
664           be regarded as local.  For some reason the
665           ELF_LINK_HASH_DEF_REGULAR bit is not set on such common
666           symbols, and the SEC_IS_COMMON bit is not set any longer
667           when we need to perform this test.  Hopefully this
668           approximation is good enough.  */ \
669        ((H)->root.type == bfd_link_hash_defined \
670         || (H)->root.type == bfd_link_hash_defweak) \
671        && (H)->root.u.def.section->output_section \
672        && ((H)->root.u.def.section->flags & SEC_LINKER_CREATED)))
673 #define FRV_FUNCDESC_LOCAL(INFO, H) \
674   ((H)->dynindx == -1 || ! elf_hash_table (INFO)->dynamic_sections_created)
675
676 /* This structure collects information on what kind of GOT, PLT or
677    function descriptors are required by relocations that reference a
678    certain symbol.  */
679 struct frv_pic_relocs_info
680 {
681   /* The index of the symbol, as stored in the relocation r_info, if
682      we have a local symbol; -1 otherwise.  */
683   long symndx;
684   union
685   {
686     /* The input bfd in which the symbol is defined, if it's a local
687        symbol.  */
688     bfd *abfd;
689     /* If symndx == -1, the hash table entry corresponding to a global
690        symbol (even if it turns out to bind locally, in which case it
691        should ideally be replaced with section's symndx + addend).  */
692     struct elf_link_hash_entry *h;
693   } d;
694   /* The addend of the relocation that references the symbol.  */
695   bfd_vma addend;
696
697   /* The fields above are used to identify an entry.  The fields below
698      contain information on how an entry is used and, later on, which
699      locations it was assigned.  */
700   /* The following 3 fields record whether the symbol+addend above was
701      ever referenced with a GOT relocation.  The 12 suffix indicates a
702      GOT12 relocation; los is used for GOTLO relocations that are not
703      matched by a GOTHI relocation; hilo is used for GOTLO/GOTHI
704      pairs.  */
705   unsigned got12:1;
706   unsigned gotlos:1;
707   unsigned gothilo:1;
708   /* Whether a FUNCDESC relocation references symbol+addend.  */
709   unsigned fd:1;
710   /* Whether a FUNCDESC_GOT relocation references symbol+addend.  */
711   unsigned fdgot12:1;
712   unsigned fdgotlos:1;
713   unsigned fdgothilo:1;
714   /* Whether a FUNCDESC_GOTOFF relocation references symbol+addend.  */
715   unsigned fdgoff12:1;
716   unsigned fdgofflos:1;
717   unsigned fdgoffhilo:1;
718   /* Whether symbol+addend is referenced with GOTOFF12, GOTOFFLO or
719      GOTOFFHI relocations.  The addend doesn't really matter, since we
720      envision that this will only be used to check whether the symbol
721      is mapped to the same segment as the got.  */
722   unsigned gotoff:1;
723   /* Whether symbol+addend is referenced by a LABEL24 relocation.  */
724   unsigned call:1;
725   /* Whether symbol+addend is referenced by a 32 or FUNCDESC_VALUE
726      relocation.  */
727   unsigned sym:1;
728   /* Whether we need a PLT entry for a symbol.  Should be implied by
729      something like:
730      (call && symndx == -1 && ! FRV_SYM_LOCAL (info, d.h))  */
731   unsigned plt:1;
732   /* Whether a function descriptor should be created in this link unit
733      for symbol+addend.  Should be implied by something like:
734      (plt || fdgotoff12 || fdgotofflos || fdgotofflohi
735       || ((fd || fdgot12 || fdgotlos || fdgothilo)
736           && (symndx != -1 || FRV_FUNCDESC_LOCAL (info, d.h))))  */
737   unsigned privfd:1;
738   /* Whether a lazy PLT entry is needed for this symbol+addend.
739      Should be implied by something like:
740      (privfd && symndx == -1 && ! FRV_SYM_LOCAL (info, d.h)
741       && ! (info->flags & DF_BIND_NOW))  */
742   unsigned lazyplt:1;
743   /* Whether we've already emitted GOT relocations and PLT entries as
744      needed for this symbol.  */
745   unsigned done:1;
746
747   /* The number of R_FRV_32, R_FRV_FUNCDESC and R_FRV_FUNCDESC_VALUE
748      relocations referencing the symbol.  */
749   unsigned relocs32, relocsfd, relocsfdv;
750
751   /* The number of .rofixups entries and dynamic relocations allocated
752      for this symbol, minus any that might have already been used.  */
753   unsigned fixups, dynrelocs;
754
755   /* The offsets of the GOT entries assigned to symbol+addend, to the
756      function descriptor's address, and to a function descriptor,
757      respectively.  Should be zero if unassigned.  The offsets are
758      counted from the value that will be assigned to the PIC register,
759      not from the beginning of the .got section.  */
760   bfd_signed_vma got_entry, fdgot_entry, fd_entry;
761   /* The offsets of the PLT entries assigned to symbol+addend,
762      non-lazy and lazy, respectively.  If unassigned, should be
763      (bfd_vma)-1.  */
764   bfd_vma plt_entry, lzplt_entry;
765 };
766
767 /* Compute a hash with the key fields of an frv_pic_relocs_info entry.  */
768 static hashval_t
769 frv_pic_relocs_info_hash (const void *entry_)
770 {
771   const struct frv_pic_relocs_info *entry = entry_;
772
773   return (entry->symndx == -1
774           ? entry->d.h->root.root.hash
775           : entry->symndx + entry->d.abfd->id * 257) + entry->addend;
776 }
777
778 /* Test whether the key fields of two frv_pic_relocs_info entries are
779    identical.  */
780 static int
781 frv_pic_relocs_info_eq (const void *entry1, const void *entry2)
782 {
783   const struct frv_pic_relocs_info *e1 = entry1;
784   const struct frv_pic_relocs_info *e2 = entry2;
785
786   return e1->symndx == e2->symndx && e1->addend == e2->addend
787     && (e1->symndx == -1 ? e1->d.h == e2->d.h : e1->d.abfd == e2->d.abfd);
788 }
789
790 /* Find or create an entry in a hash table HT that matches the key
791    fields of the given ENTRY.  If it's not found, memory for a new
792    entry is allocated in ABFD's obstack.  */
793 static struct frv_pic_relocs_info *
794 frv_pic_relocs_info_find (struct htab *ht,
795                           bfd *abfd,
796                           const struct frv_pic_relocs_info *entry,
797                           enum insert_option insert)
798 {
799   struct frv_pic_relocs_info **loc =
800     (struct frv_pic_relocs_info **) htab_find_slot (ht, entry, insert);
801
802   if (! loc)
803     return NULL;
804
805   if (*loc)
806     return *loc;
807
808   *loc = bfd_zalloc (abfd, sizeof (**loc));
809
810   if (! *loc)
811     return *loc;
812
813   (*loc)->symndx = entry->symndx;
814   (*loc)->d = entry->d;
815   (*loc)->addend = entry->addend;
816   (*loc)->plt_entry = (bfd_vma)-1;
817   (*loc)->lzplt_entry = (bfd_vma)-1;
818
819   return *loc;
820 }
821
822 /* Obtain the address of the entry in HT associated with H's symbol +
823    addend, creating a new entry if none existed.  ABFD is only used
824    for memory allocation purposes.  */
825 inline static struct frv_pic_relocs_info *
826 frv_pic_relocs_info_for_global (struct htab *ht,
827                                 bfd *abfd,
828                                 struct elf_link_hash_entry *h,
829                                 bfd_vma addend,
830                                 enum insert_option insert)
831 {
832   struct frv_pic_relocs_info entry;
833
834   entry.symndx = -1;
835   entry.d.h = h;
836   entry.addend = addend;
837
838   return frv_pic_relocs_info_find (ht, abfd, &entry, insert);
839 }
840
841 /* Obtain the address of the entry in HT associated with the SYMNDXth
842    local symbol of the input bfd ABFD, plus the addend, creating a new
843    entry if none existed.  */  
844 inline static struct frv_pic_relocs_info *
845 frv_pic_relocs_info_for_local (struct htab *ht,
846                                bfd *abfd,
847                                long symndx,
848                                bfd_vma addend,
849                                enum insert_option insert)
850 {
851   struct frv_pic_relocs_info entry;
852
853   entry.symndx = symndx;
854   entry.d.abfd = abfd;
855   entry.addend = addend;
856
857   return frv_pic_relocs_info_find (ht, abfd, &entry, insert);
858 }
859
860 /* Merge fields set by check_relocs() of two entries that end up being
861    mapped to the same (presumably global) symbol.  */
862
863 inline static void
864 frv_pic_merge_early_relocs_info (struct frv_pic_relocs_info *e2,
865                                  struct frv_pic_relocs_info const *e1)
866 {
867   e2->got12 |= e1->got12;
868   e2->gotlos |= e1->gotlos;
869   e2->gothilo |= e1->gothilo;
870   e2->fd |= e1->fd;
871   e2->fdgot12 |= e1->fdgot12;
872   e2->fdgotlos |= e1->fdgotlos;
873   e2->fdgothilo |= e1->fdgothilo;
874   e2->fdgoff12 |= e1->fdgoff12;
875   e2->fdgofflos |= e1->fdgofflos;
876   e2->fdgoffhilo |= e1->fdgoffhilo;
877   e2->gotoff |= e1->gotoff;
878   e2->call |= e1->call;
879   e2->sym |= e1->sym;
880
881 #if 0
882   /* These are set in _frv_count_got_plt_entries() or later, and this
883      function is only called in _frv_resolve_final_relocs_info(), that
884      runs just before it, so we don't have to worry about the fields
885      below.  */
886
887   e2->plt |= e1->plt;
888   e2->privfd |= e1->privfd;
889   e2->lazyplt |= e1->lazyplt;
890   e2->done |= e1->done;
891
892   e2->relocs32 += e1->relocs32;
893   e2->relocsfd += e1->relocsfd;
894   e2->relocsfdv += e1->relocsfdv;
895   e2->fixups += e1->fixups;
896   e2->dynrelocs += e1->dynrelocs;
897
898   if (abs (e1->got_entry) < abs (e2->got_entry))
899     e2->got_entry = e1->got_entry;
900   if (abs (e1->fdgot_entry) < abs (e2->fdgot_entry))
901     e2->fdgot_entry = e1->fdgot_entry;
902   if (abs (e1->fd_entry) < abs (e2->fd_entry))
903     e2->fd_entry = e1->fd_entry;
904
905   if (e1->plt_entry < e2->plt_entry)
906     e2->plt_entry = e1->plt_entry;
907   if (e1->lzplt_entry < e2->lzplt_entry)
908     e2->lzplt_entry = e1->lzplt_entry;
909 #endif
910 }
911
912 /* Every block of 65535 lazy PLT entries shares a single call to the
913    resolver, inserted in the 32768th lazy PLT entry (i.e., entry #
914    32767, counting from 0).  All other lazy PLT entries branch to it
915    in a single instruction.  */
916
917 #define FRV_LZPLT_BLOCK_SIZE ((bfd_vma) 8 * 65535 + 4)
918 #define FRV_LZPLT_RESOLV_LOC (8 * 32767)
919
920 /* Add a dynamic relocation to the SRELOC section.  */
921
922 inline static bfd_vma
923 _frv_add_dyn_reloc (bfd *output_bfd, asection *sreloc, bfd_vma offset,
924                     int reloc_type, long dynindx, bfd_vma addend,
925                     struct frv_pic_relocs_info *entry)
926 {
927   Elf_Internal_Rela outrel;
928   bfd_vma reloc_offset;
929
930   outrel.r_offset = offset;
931   outrel.r_info = ELF32_R_INFO (dynindx, reloc_type);
932   outrel.r_addend = addend;
933
934   reloc_offset = sreloc->reloc_count * sizeof (Elf32_External_Rel);
935   BFD_ASSERT (reloc_offset < sreloc->_raw_size);
936   bfd_elf32_swap_reloc_out (output_bfd, &outrel,
937                             sreloc->contents + reloc_offset);
938   sreloc->reloc_count++;
939
940   BFD_ASSERT (entry->dynrelocs > 0);
941   entry->dynrelocs--;
942
943   return reloc_offset;
944 }
945
946 /* Add a fixup to the ROFIXUP section.  */
947
948 static bfd_vma
949 _frv_add_rofixup (bfd *output_bfd, asection *rofixup, bfd_vma offset,
950                   struct frv_pic_relocs_info *entry)
951 {
952   bfd_vma fixup_offset;
953
954   if (rofixup->flags & SEC_EXCLUDE)
955     return -1;
956
957   fixup_offset = rofixup->reloc_count * 4;
958   if (rofixup->contents)
959     {
960       BFD_ASSERT (fixup_offset < rofixup->_raw_size);
961       bfd_put_32 (output_bfd, offset, rofixup->contents + fixup_offset);
962     }
963   rofixup->reloc_count++;
964
965   if (entry)
966     {
967       BFD_ASSERT (entry->fixups > 0);
968       entry->fixups--;
969     }
970
971   return fixup_offset;
972 }
973
974 /* Find the segment number in which OSEC, and output section, is
975    located.  */
976
977 static unsigned
978 _frv_osec_to_segment (bfd *output_bfd, asection *osec)
979 {
980   struct elf_segment_map *m;
981   Elf_Internal_Phdr *p;
982
983   /* Find the segment that contains the output_section.  */
984   for (m = elf_tdata (output_bfd)->segment_map,
985          p = elf_tdata (output_bfd)->phdr;
986        m != NULL;
987        m = m->next, p++)
988     {
989       int i;
990
991       for (i = m->count - 1; i >= 0; i--)
992         if (m->sections[i] == osec)
993           break;
994
995       if (i >= 0)
996         break;
997     }
998
999   return p - elf_tdata (output_bfd)->phdr;
1000 }
1001
1002 inline static bfd_boolean
1003 _frv_osec_readonly_p (bfd *output_bfd, asection *osec)
1004 {
1005   unsigned seg = _frv_osec_to_segment (output_bfd, osec);
1006
1007   return ! (elf_tdata (output_bfd)->phdr[seg].p_flags & PF_W);
1008 }
1009
1010 /* Generate relocations for GOT entries, function descriptors, and
1011    code for PLT and lazy PLT entries.  */
1012
1013 inline static bfd_boolean
1014 _frv_emit_got_relocs_plt_entries (struct frv_pic_relocs_info *entry,
1015                                   bfd *output_bfd,
1016                                   struct bfd_link_info *info,
1017                                   asection *sec,
1018                                   Elf_Internal_Sym *sym,
1019                                   bfd_vma addend)
1020                                   
1021 {
1022   bfd_vma fd_lazy_rel_offset = (bfd_vma)-1;
1023   int dynindx = -1;
1024
1025   if (entry->done)
1026     return TRUE;
1027   entry->done = 1;
1028
1029   if (entry->got_entry || entry->fdgot_entry || entry->fd_entry)
1030     {
1031       /* If the symbol is dynamic, consider it for dynamic
1032          relocations, otherwise decay to section + offset.  */
1033       if (entry->symndx == -1 && entry->d.h->dynindx != -1)
1034         dynindx = entry->d.h->dynindx;
1035       else
1036         {
1037           if (sec->output_section
1038               && ! bfd_is_abs_section (sec->output_section)
1039               && ! bfd_is_und_section (sec->output_section))
1040             dynindx = elf_section_data (sec->output_section)->dynindx;
1041           else
1042             dynindx = 0;
1043         }
1044     }
1045
1046   /* Generate relocation for GOT entry pointing to the symbol.  */
1047   if (entry->got_entry)
1048     {
1049       int idx = dynindx;
1050       bfd_vma ad = addend;
1051
1052       /* If the symbol is dynamic but binds locally, use
1053          section+offset.  */
1054       if (sec && (entry->symndx != -1 || FRV_SYM_LOCAL (info, entry->d.h)))
1055         {
1056           if (entry->symndx == -1)
1057             ad += entry->d.h->root.u.def.value;
1058           else
1059             ad += sym->st_value;
1060           ad += sec->output_offset;
1061           if (sec->output_section && elf_section_data (sec->output_section))
1062             idx = elf_section_data (sec->output_section)->dynindx;
1063           else
1064             idx = 0;
1065         }
1066
1067       /* If we're linking an executable at a fixed address, we can
1068          omit the dynamic relocation as long as the symbol is local to
1069          this module.  */
1070       if (info->executable && !info->pie
1071           && (entry->symndx != -1 || FRV_SYM_LOCAL (info, entry->d.h)))
1072         {
1073           if (sec)
1074             ad += sec->output_section->vma;
1075           if (entry->symndx != -1
1076               || entry->d.h->root.type != bfd_link_hash_undefweak)
1077             _frv_add_rofixup (output_bfd, frv_gotfixup_section (info),
1078                               frv_got_section (info)->output_section->vma
1079                               + frv_got_section (info)->output_offset
1080                               + frv_got_initial_offset (info)
1081                               + entry->got_entry, entry);
1082         }
1083       else
1084         _frv_add_dyn_reloc (output_bfd, frv_gotrel_section (info),
1085                             _bfd_elf_section_offset
1086                             (output_bfd, info,
1087                              frv_got_section (info),
1088                              frv_got_initial_offset (info)
1089                              + entry->got_entry)
1090                             + frv_got_section (info)->output_section->vma
1091                             + frv_got_section (info)->output_offset,
1092                             R_FRV_32, idx, ad, entry);
1093         
1094       bfd_put_32 (output_bfd, ad,
1095                   frv_got_section (info)->contents
1096                   + frv_got_initial_offset (info)
1097                   + entry->got_entry);
1098     }
1099
1100   /* Generate relocation for GOT entry pointing to a canonical
1101      function descriptor.  */
1102   if (entry->fdgot_entry)
1103     {
1104       int reloc, idx;
1105       bfd_vma ad = 0;
1106       
1107       if (! (entry->symndx == -1
1108              && entry->d.h->root.type == bfd_link_hash_undefweak
1109              && FRV_SYM_LOCAL (info, entry->d.h)))
1110         {
1111           /* If the symbol is dynamic and there may be dynamic symbol
1112              resolution because we are, or are linked with, a shared
1113              library, emit a FUNCDESC relocation such that the dynamic
1114              linker will allocate the function descriptor.  If the
1115              symbol needs a non-local function descriptor but binds
1116              locally (e.g., its visibility is protected, emit a
1117              dynamic relocation decayed to section+offset.  */
1118           if (entry->symndx == -1 && ! FRV_FUNCDESC_LOCAL (info, entry->d.h)
1119               && FRV_SYM_LOCAL (info, entry->d.h)
1120               && !(info->executable && !info->pie))
1121             {
1122               reloc = R_FRV_FUNCDESC;
1123               idx = elf_section_data (entry->d.h->root.u.def.section
1124                                       ->output_section)->dynindx;
1125               ad = entry->d.h->root.u.def.section->output_offset
1126                 + entry->d.h->root.u.def.value;
1127             }
1128           else if (entry->symndx == -1
1129                    && ! FRV_FUNCDESC_LOCAL (info, entry->d.h))
1130             {
1131               reloc = R_FRV_FUNCDESC;
1132               idx = dynindx;
1133               ad = addend;
1134               if (ad)
1135                 return FALSE;
1136             }
1137           else
1138             {
1139               /* Otherwise, we know we have a private function descriptor,
1140                  so reference it directly.  */
1141               if (elf_hash_table (info)->dynamic_sections_created)
1142                 BFD_ASSERT (entry->privfd);
1143               reloc = R_FRV_32;
1144               idx = elf_section_data (frv_got_section (info)
1145                                       ->output_section)->dynindx;
1146               ad = frv_got_section (info)->output_offset
1147                 + frv_got_initial_offset (info) + entry->fd_entry;
1148             }
1149
1150           /* If there is room for dynamic symbol resolution, emit the
1151              dynamic relocation.  However, if we're linking an
1152              executable at a fixed location, we won't have emitted a
1153              dynamic symbol entry for the got section, so idx will be
1154              zero, which means we can and should compute the address
1155              of the private descriptor ourselves.  */
1156           if (info->executable && !info->pie
1157               && (entry->symndx != -1
1158                   || FRV_FUNCDESC_LOCAL (info, entry->d.h)))
1159             {
1160               ad += frv_got_section (info)->output_section->vma;
1161               _frv_add_rofixup (output_bfd, frv_gotfixup_section (info),
1162                                 frv_got_section (info)->output_section->vma
1163                                 + frv_got_section (info)->output_offset
1164                                 + frv_got_initial_offset (info)
1165                                 + entry->fdgot_entry, entry);
1166             }
1167           else
1168             _frv_add_dyn_reloc (output_bfd, frv_gotrel_section (info),
1169                                 _bfd_elf_section_offset
1170                                 (output_bfd, info,
1171                                  frv_got_section (info),
1172                                  frv_got_initial_offset (info)
1173                                  + entry->fdgot_entry)
1174                                 + frv_got_section (info)->output_section->vma
1175                                 + frv_got_section (info)->output_offset,
1176                                 reloc, idx, ad, entry);
1177         }
1178
1179       bfd_put_32 (output_bfd, ad,
1180                   frv_got_section (info)->contents
1181                   + frv_got_initial_offset (info)
1182                   + entry->fdgot_entry);
1183     }
1184
1185   /* Generate relocation to fill in a private function descriptor in
1186      the GOT.  */
1187   if (entry->fd_entry)
1188     {
1189       int idx = dynindx;
1190       bfd_vma ad = addend;
1191       bfd_vma ofst;
1192       long lowword, highword;
1193
1194       /* If the symbol is dynamic but binds locally, use
1195          section+offset.  */
1196       if (sec && (entry->symndx != -1 || FRV_SYM_LOCAL (info, entry->d.h)))
1197         {
1198           if (entry->symndx == -1)
1199             ad += entry->d.h->root.u.def.value;
1200           else
1201             ad += sym->st_value;
1202           ad += sec->output_offset;
1203           if (sec->output_section && elf_section_data (sec->output_section))
1204             idx = elf_section_data (sec->output_section)->dynindx;
1205           else
1206             idx = 0;
1207         }
1208
1209       /* If we're linking an executable at a fixed address, we can
1210          omit the dynamic relocation as long as the symbol is local to
1211          this module.  */
1212       if (info->executable && !info->pie
1213           && (entry->symndx != -1 || FRV_SYM_LOCAL (info, entry->d.h)))
1214         {
1215           if (sec)
1216             ad += sec->output_section->vma;
1217           ofst = 0;
1218           if (entry->symndx != -1
1219               || entry->d.h->root.type != bfd_link_hash_undefweak)
1220             {
1221               _frv_add_rofixup (output_bfd, frv_gotfixup_section (info),
1222                                 frv_got_section (info)->output_section->vma
1223                                 + frv_got_section (info)->output_offset
1224                                 + frv_got_initial_offset (info)
1225                                 + entry->fd_entry, entry);
1226               _frv_add_rofixup (output_bfd, frv_gotfixup_section (info),
1227                                 frv_got_section (info)->output_section->vma
1228                                 + frv_got_section (info)->output_offset
1229                                 + frv_got_initial_offset (info)
1230                                 + entry->fd_entry + 4, entry);
1231             }
1232         }
1233       else
1234         {
1235           ofst =
1236             _frv_add_dyn_reloc (output_bfd,
1237                                 entry->lazyplt ? frv_pltrel_section (info)
1238                                 : frv_gotrel_section (info),
1239                                 _bfd_elf_section_offset
1240                                 (output_bfd, info,
1241                                  frv_got_section (info),
1242                                  frv_got_initial_offset (info)
1243                                  + entry->fd_entry)
1244                                 + frv_got_section (info)->output_section->vma
1245                                 + frv_got_section (info)->output_offset,
1246                                 R_FRV_FUNCDESC_VALUE, idx, ad, entry);
1247         }
1248
1249       /* If we've omitted the dynamic relocation, just emit the fixed
1250          addresses of the symbol and of the local GOT base offset.  */
1251       if (info->executable && !info->pie && sec && sec->output_section)
1252         {
1253           lowword = ad;
1254           highword = frv_got_section (info)->output_section->vma
1255             + frv_got_section (info)->output_offset
1256             + frv_got_initial_offset (info);
1257         }
1258       else if (entry->lazyplt)
1259         {
1260           if (ad)
1261             return FALSE;
1262           
1263           fd_lazy_rel_offset = ofst;
1264
1265           /* A function descriptor used for lazy or local resolving is
1266              initialized such that its high word contains the output
1267              section index in which the PLT entries are located, and
1268              the low word contains the address of the lazy PLT entry
1269              entry point, that must be within the memory region
1270              assigned to that section.  */
1271           lowword = entry->lzplt_entry + 4
1272             + frv_plt_section (info)->output_offset
1273             + frv_plt_section (info)->output_section->vma;
1274           highword = _frv_osec_to_segment 
1275             (output_bfd, frv_plt_section (info)->output_section);
1276         }
1277       else
1278         {
1279           /* A function descriptor for a local function gets the index
1280              of the section.  For a non-local function, it's
1281              disregarded.  */
1282           lowword = ad;
1283           if (entry->symndx == -1 && entry->d.h->dynindx != -1
1284               && entry->d.h->dynindx == idx)
1285             highword = 0;
1286           else
1287             highword = _frv_osec_to_segment (output_bfd, sec->output_section);
1288         }
1289
1290       bfd_put_32 (output_bfd, lowword,
1291                   frv_got_section (info)->contents
1292                   + frv_got_initial_offset (info)
1293                   + entry->fd_entry);
1294       bfd_put_32 (output_bfd, highword,
1295                   frv_got_section (info)->contents
1296                   + frv_got_initial_offset (info)
1297                   + entry->fd_entry + 4);
1298     }
1299
1300   /* Generate code for the PLT entry.  */
1301   if (entry->plt_entry != (bfd_vma) -1)
1302     {
1303       bfd_byte *plt_code = frv_plt_section (info)->contents + entry->plt_entry;
1304
1305       BFD_ASSERT (entry->fd_entry);
1306
1307       /* Figure out what kind of PLT entry we need, depending on the
1308          location of the function descriptor within the GOT.  */
1309       if (entry->fd_entry >= -(1 << (12 - 1))
1310           && entry->fd_entry < (1 << (12 - 1)))
1311         {
1312           /* lddi @(gr15, fd_entry), gr14 */
1313           bfd_put_32 (output_bfd,
1314                       0x9cccf000 | (entry->fd_entry & ((1 << 12) - 1)),
1315                       plt_code);
1316           plt_code += 4;
1317         }
1318       else
1319         {
1320           if (entry->fd_entry >= -(1 << (16 - 1))
1321               && entry->fd_entry < (1 << (16 - 1)))
1322             {
1323               /* setlos lo(fd_entry), gr14 */
1324               bfd_put_32 (output_bfd,
1325                           0x9cfc0000
1326                           | (entry->fd_entry & (((bfd_vma)1 << 16) - 1)),
1327                           plt_code);
1328               plt_code += 4;
1329             }
1330           else
1331             {
1332               /* sethi.p hi(fd_entry), gr14
1333                  setlo lo(fd_entry), gr14 */
1334               bfd_put_32 (output_bfd,
1335                           0x1cf80000
1336                           | ((entry->fd_entry >> 16)
1337                              & (((bfd_vma)1 << 16) - 1)),
1338                           plt_code);
1339               bfd_put_32 (output_bfd,
1340                           0x9cf40000
1341                           | (entry->fd_entry & (((bfd_vma)1 << 16) - 1)),
1342                           plt_code);
1343               plt_code += 8;
1344             }
1345           /* ldd @(gr14,gr15),gr14 */
1346           bfd_put_32 (output_bfd, 0x9c08e14f, plt_code);
1347           plt_code += 4;
1348         }
1349       /* jmpl @(gr14,gr0) */
1350       bfd_put_32 (output_bfd, 0x8030e000, plt_code);
1351     }
1352
1353   /* Generate code for the lazy PLT entry.  */
1354   if (entry->lzplt_entry != (bfd_vma) -1)
1355     {
1356       bfd_byte *lzplt_code = frv_plt_section (info)->contents
1357         + entry->lzplt_entry;
1358       bfd_vma resolverStub_addr;
1359
1360       bfd_put_32 (output_bfd, fd_lazy_rel_offset, lzplt_code);
1361       lzplt_code += 4;
1362
1363       resolverStub_addr = entry->lzplt_entry / FRV_LZPLT_BLOCK_SIZE
1364         * FRV_LZPLT_BLOCK_SIZE + FRV_LZPLT_RESOLV_LOC;
1365       if (resolverStub_addr >= frv_plt_initial_offset (info))
1366         resolverStub_addr = frv_plt_initial_offset (info) - 12;
1367
1368       if (entry->lzplt_entry == resolverStub_addr)
1369         {
1370           /* This is a lazy PLT entry that includes a resolver call.  */
1371           /* ldd @(gr15,gr0), gr4
1372              jmpl @(gr4,gr0)  */
1373           bfd_put_32 (output_bfd, 0x8808f140, lzplt_code);
1374           bfd_put_32 (output_bfd, 0x80304000, lzplt_code + 4);
1375         }
1376       else
1377         {
1378           /* bra  resolverStub */
1379           bfd_put_32 (output_bfd,
1380                       0xc01a0000
1381                       | (((resolverStub_addr - entry->lzplt_entry)
1382                           / 4) & (((bfd_vma)1 << 16) - 1)),
1383                       lzplt_code);
1384         }
1385     }
1386
1387   return TRUE;
1388 }
1389
1390 /* Handle an FRV small data reloc.  */
1391
1392 static bfd_reloc_status_type
1393 elf32_frv_relocate_gprel12 (info, input_bfd, input_section, relocation,
1394                             contents, value)
1395      struct bfd_link_info *info;
1396      bfd *input_bfd;
1397      asection *input_section;
1398      Elf_Internal_Rela *relocation;
1399      bfd_byte *contents;
1400      bfd_vma value;
1401 {
1402   bfd_vma insn;
1403   bfd_vma gp;
1404   struct bfd_link_hash_entry *h;
1405
1406   h = bfd_link_hash_lookup (info->hash, "_gp", FALSE, FALSE, TRUE);
1407
1408   gp = (h->u.def.value
1409         + h->u.def.section->output_section->vma
1410         + h->u.def.section->output_offset);
1411
1412   value -= input_section->output_section->vma;
1413   value -= (gp - input_section->output_section->vma);
1414
1415   insn = bfd_get_32 (input_bfd, contents + relocation->r_offset);
1416
1417   value += relocation->r_addend;
1418
1419   if ((long) value > 0x7ff || (long) value < -0x800)
1420     return bfd_reloc_overflow;
1421
1422   bfd_put_32 (input_bfd,
1423               (insn & 0xfffff000) | (value & 0xfff),
1424               contents + relocation->r_offset);
1425
1426   return bfd_reloc_ok;
1427 }
1428
1429 /* Handle an FRV small data reloc. for the u12 field.  */
1430
1431 static bfd_reloc_status_type
1432 elf32_frv_relocate_gprelu12 (info, input_bfd, input_section, relocation,
1433                              contents, value)
1434      struct bfd_link_info *info;
1435      bfd *input_bfd;
1436      asection *input_section;
1437      Elf_Internal_Rela *relocation;
1438      bfd_byte *contents;
1439      bfd_vma value;
1440 {
1441   bfd_vma insn;
1442   bfd_vma gp;
1443   struct bfd_link_hash_entry *h;
1444   bfd_vma mask;
1445
1446   h = bfd_link_hash_lookup (info->hash, "_gp", FALSE, FALSE, TRUE);
1447
1448   gp = (h->u.def.value
1449         + h->u.def.section->output_section->vma
1450         + h->u.def.section->output_offset);
1451
1452   value -= input_section->output_section->vma;
1453   value -= (gp - input_section->output_section->vma);
1454
1455   insn = bfd_get_32 (input_bfd, contents + relocation->r_offset);
1456
1457   value += relocation->r_addend;
1458
1459   if ((long) value > 0x7ff || (long) value < -0x800)
1460     return bfd_reloc_overflow;
1461
1462   /* The high 6 bits go into bits 17-12. The low 6 bits go into bits 5-0.  */
1463   mask = 0x3f03f;
1464   insn = (insn & ~mask) | ((value & 0xfc0) << 12) | (value & 0x3f);
1465
1466   bfd_put_32 (input_bfd, insn, contents + relocation->r_offset);
1467
1468   return bfd_reloc_ok;
1469 }
1470
1471 /* Handle an FRV ELF HI16 reloc.  */
1472
1473 static bfd_reloc_status_type
1474 elf32_frv_relocate_hi16 (input_bfd, relhi, contents, value)
1475      bfd *input_bfd;
1476      Elf_Internal_Rela *relhi;
1477      bfd_byte *contents;
1478      bfd_vma value;
1479 {
1480   bfd_vma insn;
1481
1482   insn = bfd_get_32 (input_bfd, contents + relhi->r_offset);
1483
1484   value += relhi->r_addend;
1485   value = ((value >> 16) & 0xffff);
1486
1487   insn = (insn & 0xffff0000) | value;
1488
1489   if ((long) value > 0xffff || (long) value < -0x10000)
1490     return bfd_reloc_overflow;
1491
1492   bfd_put_32 (input_bfd, insn, contents + relhi->r_offset);
1493   return bfd_reloc_ok;
1494
1495 }
1496 static bfd_reloc_status_type
1497 elf32_frv_relocate_lo16 (input_bfd, rello, contents, value)
1498      bfd *input_bfd;
1499      Elf_Internal_Rela *rello;
1500      bfd_byte *contents;
1501      bfd_vma value;
1502 {
1503   bfd_vma insn;
1504
1505   insn = bfd_get_32 (input_bfd, contents + rello->r_offset);
1506
1507   value += rello->r_addend;
1508   value = value & 0xffff;
1509
1510   insn = (insn & 0xffff0000) | value;
1511
1512   if ((long) value > 0xffff || (long) value < -0x10000)
1513     return bfd_reloc_overflow;
1514
1515   bfd_put_32 (input_bfd, insn, contents + rello->r_offset);
1516   return bfd_reloc_ok;
1517 }
1518
1519 /* Perform the relocation for the CALL label24 instruction.  */
1520
1521 static bfd_reloc_status_type
1522 elf32_frv_relocate_label24 (input_bfd, input_section, rello, contents, value)
1523      bfd *input_bfd;
1524      asection *input_section;
1525      Elf_Internal_Rela *rello;
1526      bfd_byte *contents;
1527      bfd_vma value;
1528 {
1529   bfd_vma insn;
1530   bfd_vma label6;
1531   bfd_vma label18;
1532
1533   /* The format for the call instruction is:
1534
1535     0 000000 0001111 000000000000000000
1536       label6 opcode  label18
1537
1538     The branch calculation is: pc + (4*label24)
1539     where label24 is the concatenation of label6 and label18.  */
1540
1541   /* Grab the instruction.  */
1542   insn = bfd_get_32 (input_bfd, contents + rello->r_offset);
1543
1544   value -= input_section->output_section->vma + input_section->output_offset;
1545   value -= rello->r_offset;
1546   value += rello->r_addend;
1547
1548   value = value >> 2;
1549
1550   label6  = value & 0xfc0000;
1551   label6  = label6 << 7;
1552
1553   label18 = value & 0x3ffff;
1554
1555   insn = insn & 0x803c0000;
1556   insn = insn | label6;
1557   insn = insn | label18;
1558
1559   bfd_put_32 (input_bfd, insn, contents + rello->r_offset);
1560
1561   return bfd_reloc_ok;
1562 }
1563
1564 static bfd_reloc_status_type
1565 elf32_frv_relocate_gprelhi (info, input_bfd, input_section, relocation,
1566                             contents, value)
1567      struct bfd_link_info *info;
1568      bfd *input_bfd;
1569      asection *input_section;
1570      Elf_Internal_Rela *relocation;
1571      bfd_byte *contents;
1572      bfd_vma value;
1573 {
1574   bfd_vma insn;
1575   bfd_vma gp;
1576   struct bfd_link_hash_entry *h;
1577
1578   h = bfd_link_hash_lookup (info->hash, "_gp", FALSE, FALSE, TRUE);
1579
1580   gp = (h->u.def.value
1581         + h->u.def.section->output_section->vma
1582         + h->u.def.section->output_offset);
1583
1584   value -= input_section->output_section->vma;
1585   value -= (gp - input_section->output_section->vma);
1586   value += relocation->r_addend;
1587   value = ((value >> 16) & 0xffff);
1588
1589   if ((long) value > 0xffff || (long) value < -0x10000)
1590     return bfd_reloc_overflow;
1591
1592   insn = bfd_get_32 (input_bfd, contents + relocation->r_offset);
1593   insn = (insn & 0xffff0000) | value;
1594
1595   bfd_put_32 (input_bfd, insn, contents + relocation->r_offset);
1596   return bfd_reloc_ok;
1597 }
1598
1599 static bfd_reloc_status_type
1600 elf32_frv_relocate_gprello (info, input_bfd, input_section, relocation,
1601                             contents, value)
1602      struct bfd_link_info *info;
1603      bfd *input_bfd;
1604      asection *input_section;
1605      Elf_Internal_Rela *relocation;
1606      bfd_byte *contents;
1607      bfd_vma value;
1608 {
1609   bfd_vma insn;
1610   bfd_vma gp;
1611   struct bfd_link_hash_entry *h;
1612
1613   h = bfd_link_hash_lookup (info->hash, "_gp", FALSE, FALSE, TRUE);
1614
1615   gp = (h->u.def.value
1616         + h->u.def.section->output_section->vma
1617         + h->u.def.section->output_offset);
1618
1619   value -= input_section->output_section->vma;
1620   value -= (gp - input_section->output_section->vma);
1621   value += relocation->r_addend;
1622   value = value & 0xffff;
1623
1624   if ((long) value > 0xffff || (long) value < -0x10000)
1625     return bfd_reloc_overflow;
1626
1627   insn = bfd_get_32 (input_bfd, contents + relocation->r_offset);
1628   insn = (insn & 0xffff0000) | value;
1629
1630   bfd_put_32 (input_bfd, insn, contents + relocation->r_offset);
1631
1632  return bfd_reloc_ok;
1633 }
1634
1635 static reloc_howto_type *
1636 frv_reloc_type_lookup (abfd, code)
1637      bfd *abfd ATTRIBUTE_UNUSED;
1638      bfd_reloc_code_real_type code;
1639 {
1640   switch (code)
1641     {
1642     default:
1643       break;
1644
1645     case BFD_RELOC_NONE:
1646       return &elf32_frv_howto_table[ (int) R_FRV_NONE];
1647
1648     case BFD_RELOC_32:
1649       if (elf_elfheader (abfd)->e_type == ET_EXEC
1650           || elf_elfheader (abfd)->e_type == ET_DYN)
1651         return &elf32_frv_rel_32_howto;
1652       /* Fall through.  */
1653     case BFD_RELOC_CTOR:
1654       return &elf32_frv_howto_table[ (int) R_FRV_32];
1655
1656     case BFD_RELOC_FRV_LABEL16:
1657       return &elf32_frv_howto_table[ (int) R_FRV_LABEL16];
1658
1659     case BFD_RELOC_FRV_LABEL24:
1660       return &elf32_frv_howto_table[ (int) R_FRV_LABEL24];
1661
1662     case BFD_RELOC_FRV_LO16:
1663       return &elf32_frv_howto_table[ (int) R_FRV_LO16];
1664
1665     case BFD_RELOC_FRV_HI16:
1666       return &elf32_frv_howto_table[ (int) R_FRV_HI16];
1667
1668     case BFD_RELOC_FRV_GPREL12:
1669       return &elf32_frv_howto_table[ (int) R_FRV_GPREL12];
1670
1671     case BFD_RELOC_FRV_GPRELU12:
1672       return &elf32_frv_howto_table[ (int) R_FRV_GPRELU12];
1673
1674     case BFD_RELOC_FRV_GPREL32:
1675       return &elf32_frv_howto_table[ (int) R_FRV_GPREL32];
1676
1677     case BFD_RELOC_FRV_GPRELHI:
1678       return &elf32_frv_howto_table[ (int) R_FRV_GPRELHI];
1679
1680     case BFD_RELOC_FRV_GPRELLO:
1681       return &elf32_frv_howto_table[ (int) R_FRV_GPRELLO];
1682
1683     case BFD_RELOC_FRV_GOT12:
1684       return &elf32_frv_howto_table[ (int) R_FRV_GOT12];
1685
1686     case BFD_RELOC_FRV_GOTHI:
1687       return &elf32_frv_howto_table[ (int) R_FRV_GOTHI];
1688
1689     case BFD_RELOC_FRV_GOTLO:
1690       return &elf32_frv_howto_table[ (int) R_FRV_GOTLO];
1691
1692     case BFD_RELOC_FRV_FUNCDESC:
1693       if (elf_elfheader (abfd)->e_type == ET_EXEC
1694           || elf_elfheader (abfd)->e_type == ET_DYN)
1695         return &elf32_frv_rel_funcdesc_howto;
1696       return &elf32_frv_howto_table[ (int) R_FRV_FUNCDESC];
1697
1698     case BFD_RELOC_FRV_FUNCDESC_GOT12:
1699       return &elf32_frv_howto_table[ (int) R_FRV_FUNCDESC_GOT12];
1700
1701     case BFD_RELOC_FRV_FUNCDESC_GOTHI:
1702       return &elf32_frv_howto_table[ (int) R_FRV_FUNCDESC_GOTHI];
1703
1704     case BFD_RELOC_FRV_FUNCDESC_GOTLO:
1705       return &elf32_frv_howto_table[ (int) R_FRV_FUNCDESC_GOTLO];
1706
1707     case BFD_RELOC_FRV_FUNCDESC_VALUE:
1708       if (elf_elfheader (abfd)->e_type == ET_EXEC
1709           || elf_elfheader (abfd)->e_type == ET_DYN)
1710         return &elf32_frv_rel_funcdesc_value_howto;
1711       return &elf32_frv_howto_table[ (int) R_FRV_FUNCDESC_VALUE];
1712
1713     case BFD_RELOC_FRV_FUNCDESC_GOTOFF12:
1714       return &elf32_frv_howto_table[ (int) R_FRV_FUNCDESC_GOTOFF12];
1715
1716     case BFD_RELOC_FRV_FUNCDESC_GOTOFFHI:
1717       return &elf32_frv_howto_table[ (int) R_FRV_FUNCDESC_GOTOFFHI];
1718
1719     case BFD_RELOC_FRV_FUNCDESC_GOTOFFLO:
1720       return &elf32_frv_howto_table[ (int) R_FRV_FUNCDESC_GOTOFFLO];
1721
1722     case BFD_RELOC_FRV_GOTOFF12:
1723       return &elf32_frv_howto_table[ (int) R_FRV_GOTOFF12];
1724
1725     case BFD_RELOC_FRV_GOTOFFHI:
1726       return &elf32_frv_howto_table[ (int) R_FRV_GOTOFFHI];
1727
1728     case BFD_RELOC_FRV_GOTOFFLO:
1729       return &elf32_frv_howto_table[ (int) R_FRV_GOTOFFLO];
1730
1731     case BFD_RELOC_VTABLE_INHERIT:
1732       return &elf32_frv_vtinherit_howto;
1733
1734     case BFD_RELOC_VTABLE_ENTRY:
1735       return &elf32_frv_vtentry_howto;
1736     }
1737
1738   return NULL;
1739 }
1740
1741 /* Set the howto pointer for an FRV ELF reloc.  */
1742
1743 static void
1744 frv_info_to_howto_rela (abfd, cache_ptr, dst)
1745      bfd *abfd ATTRIBUTE_UNUSED;
1746      arelent *cache_ptr;
1747      Elf_Internal_Rela *dst;
1748 {
1749   unsigned int r_type;
1750
1751   r_type = ELF32_R_TYPE (dst->r_info);
1752   switch (r_type)
1753     {
1754     case R_FRV_GNU_VTINHERIT:
1755       cache_ptr->howto = &elf32_frv_vtinherit_howto;
1756       break;
1757
1758     case R_FRV_GNU_VTENTRY:
1759       cache_ptr->howto = &elf32_frv_vtentry_howto;
1760       break;
1761
1762     default:
1763       cache_ptr->howto = & elf32_frv_howto_table [r_type];
1764       break;
1765     }
1766 }
1767
1768 /* Set the howto pointer for an FRV ELF REL reloc.  */
1769 static void
1770 frv_info_to_howto_rel (bfd *abfd ATTRIBUTE_UNUSED,
1771                        arelent *cache_ptr, Elf_Internal_Rela *dst)
1772 {
1773   unsigned int r_type;
1774
1775   r_type = ELF32_R_TYPE (dst->r_info);
1776   switch (r_type)
1777     {
1778     case R_FRV_32:
1779       cache_ptr->howto = &elf32_frv_rel_32_howto;
1780       break;
1781
1782     case R_FRV_FUNCDESC:
1783       cache_ptr->howto = &elf32_frv_rel_funcdesc_howto;
1784       break;
1785
1786     case R_FRV_FUNCDESC_VALUE:
1787       cache_ptr->howto = &elf32_frv_rel_funcdesc_value_howto;
1788       break;
1789
1790     default:
1791       cache_ptr->howto = NULL;
1792       break;
1793     }
1794 }
1795 \f
1796 /* Perform a single relocation.  By default we use the standard BFD
1797    routines, but a few relocs, we have to do them ourselves.  */
1798
1799 static bfd_reloc_status_type
1800 frv_final_link_relocate (howto, input_bfd, input_section, contents, rel,
1801                          relocation)
1802      reloc_howto_type *howto;
1803      bfd *input_bfd;
1804      asection *input_section;
1805      bfd_byte *contents;
1806      Elf_Internal_Rela *rel;
1807      bfd_vma relocation;
1808 {
1809   return _bfd_final_link_relocate (howto, input_bfd, input_section,
1810                                    contents, rel->r_offset, relocation,
1811                                    rel->r_addend);
1812 }
1813
1814 \f
1815 /* Relocate an FRV ELF section.
1816
1817    The RELOCATE_SECTION function is called by the new ELF backend linker
1818    to handle the relocations for a section.
1819
1820    The relocs are always passed as Rela structures; if the section
1821    actually uses Rel structures, the r_addend field will always be
1822    zero.
1823
1824    This function is responsible for adjusting the section contents as
1825    necessary, and (if using Rela relocs and generating a relocatable
1826    output file) adjusting the reloc addend as necessary.
1827
1828    This function does not have to worry about setting the reloc
1829    address or the reloc symbol index.
1830
1831    LOCAL_SYMS is a pointer to the swapped in local symbols.
1832
1833    LOCAL_SECTIONS is an array giving the section in the input file
1834    corresponding to the st_shndx field of each local symbol.
1835
1836    The global hash table entry for the global symbols can be found
1837    via elf_sym_hashes (input_bfd).
1838
1839    When generating relocatable output, this function must handle
1840    STB_LOCAL/STT_SECTION symbols specially.  The output symbol is
1841    going to be the section symbol corresponding to the output
1842    section, which means that the addend must be adjusted
1843    accordingly.  */
1844
1845 static bfd_boolean
1846 elf32_frv_relocate_section (output_bfd, info, input_bfd, input_section,
1847                             contents, relocs, local_syms, local_sections)
1848      bfd *output_bfd ATTRIBUTE_UNUSED;
1849      struct bfd_link_info *info;
1850      bfd *input_bfd;
1851      asection *input_section;
1852      bfd_byte *contents;
1853      Elf_Internal_Rela *relocs;
1854      Elf_Internal_Sym *local_syms;
1855      asection **local_sections;
1856 {
1857   Elf_Internal_Shdr *symtab_hdr;
1858   struct elf_link_hash_entry **sym_hashes;
1859   Elf_Internal_Rela *rel;
1860   Elf_Internal_Rela *relend;
1861   unsigned isec_segment, got_segment, plt_segment, gprel_segment,
1862     check_segment[2];
1863   int silence_segment_error = !(info->shared || info->pie);
1864
1865   if (info->relocatable)
1866     return TRUE;
1867
1868   symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
1869   sym_hashes = elf_sym_hashes (input_bfd);
1870   relend     = relocs + input_section->reloc_count;
1871
1872   isec_segment = _frv_osec_to_segment (output_bfd,
1873                                        input_section->output_section);
1874   if (frv_got_section (info))
1875     got_segment = _frv_osec_to_segment (output_bfd,
1876                                         frv_got_section (info)
1877                                         ->output_section);
1878   else
1879     got_segment = -1;
1880   if (frv_gotfixup_section (info))
1881     gprel_segment = _frv_osec_to_segment (output_bfd,
1882                                           frv_gotfixup_section (info)
1883                                           ->output_section);
1884   else
1885     gprel_segment = -1;
1886   if (elf_hash_table (info)->dynamic_sections_created)
1887     plt_segment = _frv_osec_to_segment (output_bfd,
1888                                         frv_plt_section (info)
1889                                         ->output_section);
1890   else
1891     plt_segment = -1;
1892
1893   for (rel = relocs; rel < relend; rel ++)
1894     {
1895       reloc_howto_type *howto;
1896       unsigned long r_symndx;
1897       Elf_Internal_Sym *sym;
1898       asection *sec;
1899       struct elf_link_hash_entry *h;
1900       bfd_vma relocation;
1901       bfd_reloc_status_type r;
1902       const char * name = NULL;
1903       int r_type;
1904       asection *osec;
1905       struct frv_pic_relocs_info *picrel;
1906       bfd_vma orig_addend = rel->r_addend;
1907
1908       r_type = ELF32_R_TYPE (rel->r_info);
1909
1910       if (   r_type == R_FRV_GNU_VTINHERIT
1911           || r_type == R_FRV_GNU_VTENTRY)
1912         continue;
1913
1914       /* This is a final link.  */
1915       r_symndx = ELF32_R_SYM (rel->r_info);
1916       howto  = elf32_frv_howto_table + ELF32_R_TYPE (rel->r_info);
1917       h      = NULL;
1918       sym    = NULL;
1919       sec    = NULL;
1920
1921       if (r_symndx < symtab_hdr->sh_info)
1922         {
1923           sym = local_syms + r_symndx;
1924           osec = sec = local_sections [r_symndx];
1925           relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
1926
1927           name = bfd_elf_string_from_elf_section
1928             (input_bfd, symtab_hdr->sh_link, sym->st_name);
1929           name = (name == NULL) ? bfd_section_name (input_bfd, sec) : name;
1930         }
1931       else
1932         {
1933           h = sym_hashes [r_symndx - symtab_hdr->sh_info];
1934
1935           while (h->root.type == bfd_link_hash_indirect
1936                  || h->root.type == bfd_link_hash_warning)
1937             h = (struct elf_link_hash_entry *) h->root.u.i.link;
1938
1939           name = h->root.root.string;
1940
1941           if ((h->root.type == bfd_link_hash_defined
1942                || h->root.type == bfd_link_hash_defweak)
1943               && ! FRV_SYM_LOCAL (info, h))
1944             {
1945               sec = NULL;
1946               relocation = 0;
1947             }
1948           else
1949           if (h->root.type == bfd_link_hash_defined
1950               || h->root.type == bfd_link_hash_defweak)
1951             {
1952               sec = h->root.u.def.section;
1953               relocation = (h->root.u.def.value
1954                             + sec->output_section->vma
1955                             + sec->output_offset);
1956             }
1957           else if (h->root.type == bfd_link_hash_undefweak)
1958             {
1959               relocation = 0;
1960             }
1961           else if (info->unresolved_syms_in_objects == RM_IGNORE
1962                    && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
1963             relocation = 0;
1964           else
1965             {
1966               if (! ((*info->callbacks->undefined_symbol)
1967                      (info, h->root.root.string, input_bfd,
1968                       input_section, rel->r_offset,
1969                       (info->unresolved_syms_in_objects == RM_GENERATE_ERROR
1970                        || ELF_ST_VISIBILITY (h->other)))))
1971                 return FALSE;
1972               relocation = 0;
1973             }
1974           osec = sec;
1975         }
1976
1977       switch (r_type)
1978         {
1979         case R_FRV_LABEL24:
1980         case R_FRV_32:
1981         case R_FRV_GOT12:
1982         case R_FRV_GOTHI:
1983         case R_FRV_GOTLO:
1984         case R_FRV_FUNCDESC_GOT12:
1985         case R_FRV_FUNCDESC_GOTHI:
1986         case R_FRV_FUNCDESC_GOTLO:
1987         case R_FRV_GOTOFF12:
1988         case R_FRV_GOTOFFHI:
1989         case R_FRV_GOTOFFLO:
1990         case R_FRV_FUNCDESC_GOTOFF12:
1991         case R_FRV_FUNCDESC_GOTOFFHI:
1992         case R_FRV_FUNCDESC_GOTOFFLO:
1993         case R_FRV_FUNCDESC:
1994         case R_FRV_FUNCDESC_VALUE:
1995           if (h != NULL)
1996             picrel = frv_pic_relocs_info_for_global (frv_relocs_info (info),
1997                                                      input_bfd, h,
1998                                                      orig_addend, INSERT);
1999           else
2000             /* In order to find the entry we created before, we must
2001                use the original addend, not the one that may have been
2002                modified by _bfd_elf_rela_local_sym().  */
2003             picrel = frv_pic_relocs_info_for_local (frv_relocs_info (info),
2004                                                     input_bfd, r_symndx,
2005                                                     orig_addend, INSERT);
2006           if (! picrel)
2007             return FALSE;
2008
2009           if (!_frv_emit_got_relocs_plt_entries (picrel, output_bfd, info,
2010                                                  osec, sym, rel->r_addend))
2011             {
2012               info->callbacks->warning
2013                 (info, _("Dynamic relocation references symbol with nonzero addend"),
2014                  name, input_bfd, input_section, rel->r_offset);
2015               return FALSE;
2016
2017             }
2018
2019           break;
2020
2021         default:
2022           picrel = NULL;
2023           if (h && ! FRV_SYM_LOCAL (info, h))
2024             {
2025               info->callbacks->warning
2026                 (info, _("relocation references symbol not defined in the module"),
2027                  name, input_bfd, input_section, rel->r_offset);
2028               return FALSE;
2029             }
2030           break;
2031         }
2032
2033       switch (r_type)
2034         {
2035         case R_FRV_LABEL24:
2036           check_segment[0] = isec_segment;
2037           if (picrel->plt)
2038             {
2039               relocation = frv_plt_section (info)->output_section->vma
2040                 + frv_plt_section (info)->output_offset
2041                 + picrel->plt_entry;
2042               check_segment[1] = plt_segment;
2043             }
2044           /* We don't want to warn on calls to undefined weak symbols,
2045              as calls to them must be protected by non-NULL tests
2046              anyway, and unprotected calls would invoke undefined
2047              behavior.  */
2048           else if (picrel->symndx == -1
2049                    && picrel->d.h->root.type == bfd_link_hash_undefweak)
2050             check_segment[1] = check_segment[0];
2051           else
2052             check_segment[1] = sec
2053               ? _frv_osec_to_segment (output_bfd, sec->output_section)
2054               : (unsigned)-1;
2055           break;
2056
2057         case R_FRV_GOT12:
2058         case R_FRV_GOTHI:
2059         case R_FRV_GOTLO:
2060           relocation = picrel->got_entry;
2061           check_segment[0] = check_segment[1] = got_segment;
2062           break;
2063           
2064         case R_FRV_FUNCDESC_GOT12:
2065         case R_FRV_FUNCDESC_GOTHI:
2066         case R_FRV_FUNCDESC_GOTLO:
2067           relocation = picrel->fdgot_entry;
2068           check_segment[0] = check_segment[1] = got_segment;
2069           break;
2070           
2071         case R_FRV_GOTOFFHI:
2072         case R_FRV_GOTOFF12:
2073         case R_FRV_GOTOFFLO:
2074           relocation -= frv_got_section (info)->output_section->vma
2075             + frv_got_section (info)->output_offset
2076             + frv_got_initial_offset (info);
2077           check_segment[0] = got_segment;
2078           check_segment[1] = sec
2079             ? _frv_osec_to_segment (output_bfd, sec->output_section)
2080             : (unsigned)-1;
2081           break;
2082
2083         case R_FRV_FUNCDESC_GOTOFF12:
2084         case R_FRV_FUNCDESC_GOTOFFHI:
2085         case R_FRV_FUNCDESC_GOTOFFLO:
2086           relocation = picrel->fd_entry;
2087           check_segment[0] = check_segment[1] = got_segment;
2088           break;
2089
2090         case R_FRV_FUNCDESC:
2091           {
2092             int dynindx;
2093             bfd_vma addend = rel->r_addend;
2094
2095             if (! (h && h->root.type == bfd_link_hash_undefweak
2096                    && FRV_SYM_LOCAL (info, h)))
2097               {
2098                 /* If the symbol is dynamic and there may be dynamic
2099                    symbol resolution because we are or are linked with a
2100                    shared library, emit a FUNCDESC relocation such that
2101                    the dynamic linker will allocate the function
2102                    descriptor.  If the symbol needs a non-local function
2103                    descriptor but binds locally (e.g., its visibility is
2104                    protected, emit a dynamic relocation decayed to
2105                    section+offset.  */
2106                 if (h && ! FRV_FUNCDESC_LOCAL (info, h)
2107                     && FRV_SYM_LOCAL (info, h)
2108                     && !(info->executable && !info->pie))
2109                   {
2110                     dynindx = elf_section_data (h->root.u.def.section
2111                                                 ->output_section)->dynindx;
2112                     addend += h->root.u.def.section->output_offset
2113                       + h->root.u.def.value;
2114                   }
2115                 else if (h && ! FRV_FUNCDESC_LOCAL (info, h))
2116                   {
2117                     if (addend)
2118                       {
2119                         info->callbacks->warning
2120                           (info, _("R_FRV_FUNCDESC references dynamic symbol with nonzero addend"),
2121                            name, input_bfd, input_section, rel->r_offset);
2122                         return FALSE;
2123                       }
2124                     dynindx = h->dynindx;
2125                   }
2126                 else
2127                   {
2128                     /* Otherwise, we know we have a private function
2129                        descriptor, so reference it directly.  */
2130                     BFD_ASSERT (picrel->privfd);
2131                     r_type = R_FRV_32;
2132                     dynindx = elf_section_data (frv_got_section (info)
2133                                                 ->output_section)->dynindx;
2134                     addend = frv_got_section (info)->output_offset
2135                       + frv_got_initial_offset (info)
2136                       + picrel->fd_entry;
2137                   }
2138
2139                 /* If there is room for dynamic symbol resolution, emit
2140                    the dynamic relocation.  However, if we're linking an
2141                    executable at a fixed location, we won't have emitted a
2142                    dynamic symbol entry for the got section, so idx will
2143                    be zero, which means we can and should compute the
2144                    address of the private descriptor ourselves.  */
2145                 if (info->executable && !info->pie
2146                     && (!h || FRV_FUNCDESC_LOCAL (info, h)))
2147                   {
2148                     addend += frv_got_section (info)->output_section->vma;
2149                     if ((bfd_get_section_flags (output_bfd,
2150                                                 input_section->output_section)
2151                          & (SEC_ALLOC | SEC_LOAD)) == (SEC_ALLOC | SEC_LOAD))
2152                       {
2153                         if (_frv_osec_readonly_p (output_bfd,
2154                                                   input_section->output_section))
2155                           {
2156                             info->callbacks->warning
2157                               (info,
2158                                _("cannot emit fixups in read-only section"),
2159                                name, input_bfd, input_section, rel->r_offset);
2160                             return FALSE;
2161                           }
2162                         _frv_add_rofixup (output_bfd,
2163                                           frv_gotfixup_section (info),
2164                                           _bfd_elf_section_offset
2165                                           (output_bfd, info,
2166                                            input_section, rel->r_offset)
2167                                           + input_section->output_section->vma
2168                                           + input_section->output_offset,
2169                                           picrel);
2170                       }
2171                   }
2172                 else if ((bfd_get_section_flags (output_bfd,
2173                                                  input_section->output_section)
2174                           & (SEC_ALLOC | SEC_LOAD)) == (SEC_ALLOC | SEC_LOAD))
2175                   {
2176                     if (_frv_osec_readonly_p (output_bfd,
2177                                               input_section->output_section))
2178                       {
2179                         info->callbacks->warning
2180                           (info,
2181                            _("cannot emit dynamic relocations in read-only section"),
2182                            name, input_bfd, input_section, rel->r_offset);
2183                         return FALSE;
2184                       }
2185                     _frv_add_dyn_reloc (output_bfd, frv_gotrel_section (info),
2186                                         _bfd_elf_section_offset
2187                                         (output_bfd, info,
2188                                          input_section, rel->r_offset)
2189                                         + input_section->output_section->vma
2190                                         + input_section->output_offset,
2191                                         r_type, dynindx, addend, picrel);
2192                   }
2193               }
2194
2195             /* We want the addend in-place because dynamic
2196                relocations are REL.  Setting relocation to it should
2197                arrange for it to be installed.  */
2198             relocation = addend - rel->r_addend;
2199           }
2200           check_segment[0] = check_segment[1] = got_segment;
2201           break;
2202
2203         case R_FRV_32:
2204         case R_FRV_FUNCDESC_VALUE:
2205           {
2206             int dynindx;
2207             bfd_vma addend = rel->r_addend;
2208
2209             /* If the symbol is dynamic but binds locally, use
2210                section+offset.  */
2211             if (h && ! FRV_SYM_LOCAL (info, h))
2212               {
2213                 if (addend && r_type == R_FRV_FUNCDESC_VALUE)
2214                   {
2215                     info->callbacks->warning
2216                       (info, _("R_FRV_FUNCDESC_VALUE references dynamic symbol with nonzero addend"),
2217                        name, input_bfd, input_section, rel->r_offset);
2218                     return FALSE;
2219                   }
2220                 dynindx = h->dynindx;
2221               }
2222             else
2223               {
2224                 if (h)
2225                   addend += h->root.u.def.value;
2226                 else
2227                   addend += sym->st_value;
2228                 if (osec)
2229                   addend += osec->output_offset;
2230                 if (osec && osec->output_section
2231                     && ! bfd_is_abs_section (osec->output_section)
2232                     && ! bfd_is_und_section (osec->output_section))
2233                   dynindx = elf_section_data (osec->output_section)->dynindx;
2234                 else
2235                   dynindx = 0;
2236               }
2237
2238             /* If we're linking an executable at a fixed address, we
2239                can omit the dynamic relocation as long as the symbol
2240                is defined in the current link unit (which is implied
2241                by its output section not being NULL).  */
2242             if (info->executable && !info->pie
2243                 && (!h || FRV_SYM_LOCAL (info, h)))
2244               {
2245                 if (osec)
2246                   addend += osec->output_section->vma;
2247                 if ((elf_elfheader (input_bfd)->e_flags & EF_FRV_FDPIC)
2248                     && (bfd_get_section_flags (output_bfd,
2249                                                input_section->output_section)
2250                         & (SEC_ALLOC | SEC_LOAD)) == (SEC_ALLOC | SEC_LOAD))
2251                   {
2252                     if (_frv_osec_readonly_p (output_bfd,
2253                                               input_section->output_section))
2254                       {
2255                         info->callbacks->warning
2256                           (info,
2257                            _("cannot emit fixups in read-only section"),
2258                            name, input_bfd, input_section, rel->r_offset);
2259                         return FALSE;
2260                       }
2261                     if (!h || h->root.type != bfd_link_hash_undefweak)
2262                       {
2263                         _frv_add_rofixup (output_bfd,
2264                                           frv_gotfixup_section (info),
2265                                           _bfd_elf_section_offset
2266                                           (output_bfd, info,
2267                                            input_section, rel->r_offset)
2268                                           + input_section->output_section->vma
2269                                           + input_section->output_offset,
2270                                           picrel);
2271                         if (r_type == R_FRV_FUNCDESC_VALUE)
2272                           _frv_add_rofixup
2273                             (output_bfd,
2274                              frv_gotfixup_section (info),
2275                              _bfd_elf_section_offset
2276                              (output_bfd, info,
2277                               input_section, rel->r_offset)
2278                              + input_section->output_section->vma
2279                              + input_section->output_offset + 4, picrel);
2280                       }
2281                   }
2282               }
2283             else
2284               {
2285                 if ((bfd_get_section_flags (output_bfd,
2286                                             input_section->output_section)
2287                      & (SEC_ALLOC | SEC_LOAD)) == (SEC_ALLOC | SEC_LOAD))
2288                   {
2289                     if (_frv_osec_readonly_p (output_bfd,
2290                                               input_section->output_section))
2291                       {
2292                         info->callbacks->warning
2293                           (info,
2294                            _("cannot emit dynamic relocations in read-only section"),
2295                            name, input_bfd, input_section, rel->r_offset);
2296                         return FALSE;
2297                       }
2298                     _frv_add_dyn_reloc (output_bfd, frv_gotrel_section (info),
2299                                         _bfd_elf_section_offset
2300                                         (output_bfd, info,
2301                                          input_section, rel->r_offset)
2302                                         + input_section->output_section->vma
2303                                         + input_section->output_offset,
2304                                         r_type, dynindx, addend, picrel);
2305                   }
2306                 /* We want the addend in-place because dynamic
2307                    relocations are REL.  Setting relocation to it
2308                    should arrange for it to be installed.  */
2309                 relocation = addend - rel->r_addend;
2310               }
2311
2312             if (r_type == R_FRV_FUNCDESC_VALUE)
2313               {
2314                 /* If we've omitted the dynamic relocation, just emit
2315                    the fixed addresses of the symbol and of the local
2316                    GOT base offset.  */
2317                 if (info->executable && !info->pie
2318                     && (!h || FRV_SYM_LOCAL (info, h)))
2319                   bfd_put_32 (output_bfd,
2320                               frv_got_section (info)->output_section->vma
2321                               + frv_got_section (info)->output_offset
2322                               + frv_got_initial_offset (info),
2323                               contents + rel->r_offset + 4);
2324                 else
2325                   /* A function descriptor used for lazy or local
2326                      resolving is initialized such that its high word
2327                      contains the output section index in which the
2328                      PLT entries are located, and the low word
2329                      contains the offset of the lazy PLT entry entry
2330                      point into that section.  */
2331                   bfd_put_32 (output_bfd,
2332                               h && ! FRV_SYM_LOCAL (info, h)
2333                               ? 0
2334                               : _frv_osec_to_segment (output_bfd,
2335                                                       sec->output_section),
2336                               contents + rel->r_offset + 4);
2337               }
2338           }
2339           check_segment[0] = check_segment[1] = got_segment;
2340           break;
2341
2342         case R_FRV_GPREL12:
2343         case R_FRV_GPRELU12:
2344         case R_FRV_GPREL32:
2345         case R_FRV_GPRELHI:
2346         case R_FRV_GPRELLO:
2347           check_segment[0] = gprel_segment;
2348           check_segment[1] = sec
2349             ? _frv_osec_to_segment (output_bfd, sec->output_section)
2350             : (unsigned)-1;
2351           break;
2352
2353         default:
2354           check_segment[0] = isec_segment;
2355           check_segment[1] = sec
2356             ? _frv_osec_to_segment (output_bfd, sec->output_section)
2357             : (unsigned)-1;
2358           break;
2359         }
2360
2361       if (check_segment[0] != check_segment[1]
2362           && (elf_elfheader (output_bfd)->e_flags & EF_FRV_FDPIC))
2363         {
2364 #if 1
2365           /* This helps catch problems in GCC while we can't do more
2366              than static linking.  The idea is to test whether the
2367              input file basename is crt0.o only once.  */
2368           if (silence_segment_error == 1)
2369             silence_segment_error =
2370               (strlen (input_bfd->filename) == 6
2371                && strcmp (input_bfd->filename, "crt0.o") == 0)
2372               || (strlen (input_bfd->filename) > 6
2373                   && strcmp (input_bfd->filename
2374                              + strlen (input_bfd->filename) - 7,
2375                              "/crt0.o") == 0)
2376               ? -1 : 0;
2377 #endif
2378           if (!silence_segment_error
2379               /* We don't want duplicate errors for undefined
2380                  symbols.  */
2381               && !(picrel && picrel->symndx == -1
2382                    && picrel->d.h->root.type == bfd_link_hash_undefined))
2383             info->callbacks->warning
2384               (info,
2385                (info->shared || info->pie)
2386                ? _("relocations between different segments are not supported")
2387                : _("warning: relocation references a different segment"),
2388                name, input_bfd, input_section, rel->r_offset);
2389           if (!silence_segment_error && (info->shared || info->pie))
2390             return FALSE;
2391           elf_elfheader (output_bfd)->e_flags |= EF_FRV_PIC;
2392         }
2393
2394       switch (r_type)
2395         {
2396         case R_FRV_GOTOFFHI:
2397           /* We need the addend to be applied before we shift the
2398              value right.  */
2399           relocation += rel->r_addend;
2400           /* Fall through.  */
2401         case R_FRV_GOTHI:
2402         case R_FRV_FUNCDESC_GOTHI:
2403         case R_FRV_FUNCDESC_GOTOFFHI:
2404           relocation >>= 16;
2405           /* Fall through.  */
2406
2407         case R_FRV_GOTLO:
2408         case R_FRV_FUNCDESC_GOTLO:
2409         case R_FRV_GOTOFFLO:
2410         case R_FRV_FUNCDESC_GOTOFFLO:
2411           relocation &= 0xffff;
2412           break;
2413
2414         default:
2415           break;
2416         }
2417
2418       switch (r_type)
2419         {
2420         case R_FRV_LABEL24:
2421           if (! picrel->plt)
2422             break;
2423           /* Fall through.  */
2424           
2425           /* When referencing a GOT entry, a function descriptor or a
2426              PLT, we don't want the addend to apply to the reference,
2427              but rather to the referenced symbol.  The actual entry
2428              will have already been created taking the addend into
2429              account, so cancel it out here.  */
2430         case R_FRV_GOT12:
2431         case R_FRV_GOTHI:
2432         case R_FRV_GOTLO:
2433         case R_FRV_FUNCDESC_GOT12:
2434         case R_FRV_FUNCDESC_GOTHI:
2435         case R_FRV_FUNCDESC_GOTLO:
2436         case R_FRV_FUNCDESC_GOTOFF12:
2437         case R_FRV_FUNCDESC_GOTOFFHI:
2438         case R_FRV_FUNCDESC_GOTOFFLO:
2439           /* Note that we only want GOTOFFHI, not GOTOFFLO or GOTOFF12
2440              here, since we do want to apply the addend to the others.
2441              Note that we've applied the addend to GOTOFFHI before we
2442              shifted it right.  */ 
2443         case R_FRV_GOTOFFHI:
2444           relocation -= rel->r_addend;
2445           break;
2446
2447         default:
2448           break;
2449         }
2450
2451      if (r_type == R_FRV_HI16)
2452        r = elf32_frv_relocate_hi16 (input_bfd, rel, contents, relocation);
2453
2454      else if (r_type == R_FRV_LO16)
2455        r = elf32_frv_relocate_lo16 (input_bfd, rel, contents, relocation);
2456
2457      else if (r_type == R_FRV_LABEL24)
2458        r = elf32_frv_relocate_label24 (input_bfd, input_section, rel,
2459                                        contents, relocation);
2460
2461      else if (r_type == R_FRV_GPREL12)
2462        r = elf32_frv_relocate_gprel12 (info, input_bfd, input_section, rel,
2463                                        contents, relocation);
2464
2465      else if (r_type == R_FRV_GPRELU12)
2466        r = elf32_frv_relocate_gprelu12 (info, input_bfd, input_section, rel,
2467                                         contents, relocation);
2468
2469      else if (r_type == R_FRV_GPRELLO)
2470        r = elf32_frv_relocate_gprello (info, input_bfd, input_section, rel,
2471                                        contents, relocation);
2472
2473      else if (r_type == R_FRV_GPRELHI)
2474        r = elf32_frv_relocate_gprelhi (info, input_bfd, input_section, rel,
2475                                        contents, relocation);
2476
2477      else
2478        r = frv_final_link_relocate (howto, input_bfd, input_section, contents,
2479                                     rel, relocation);
2480
2481       if (r != bfd_reloc_ok)
2482         {
2483           const char * msg = (const char *) NULL;
2484
2485           switch (r)
2486             {
2487             case bfd_reloc_overflow:
2488               r = info->callbacks->reloc_overflow
2489                 (info, name, howto->name, (bfd_vma) 0,
2490                  input_bfd, input_section, rel->r_offset);
2491               break;
2492
2493             case bfd_reloc_undefined:
2494               r = info->callbacks->undefined_symbol
2495                 (info, name, input_bfd, input_section, rel->r_offset, TRUE);
2496               break;
2497
2498             case bfd_reloc_outofrange:
2499               msg = _("internal error: out of range error");
2500               break;
2501
2502             case bfd_reloc_notsupported:
2503               msg = _("internal error: unsupported relocation error");
2504               break;
2505
2506             case bfd_reloc_dangerous:
2507               msg = _("internal error: dangerous relocation");
2508               break;
2509
2510             default:
2511               msg = _("internal error: unknown error");
2512               break;
2513             }
2514
2515           if (msg)
2516             r = info->callbacks->warning
2517               (info, msg, name, input_bfd, input_section, rel->r_offset);
2518
2519           if (! r)
2520             return FALSE;
2521         }
2522     }
2523
2524   return TRUE;
2525 }
2526 \f
2527 /* Return the section that should be marked against GC for a given
2528    relocation.  */
2529
2530 static asection *
2531 elf32_frv_gc_mark_hook (sec, info, rel, h, sym)
2532      asection *sec;
2533      struct bfd_link_info *info ATTRIBUTE_UNUSED;
2534      Elf_Internal_Rela *rel;
2535      struct elf_link_hash_entry *h;
2536      Elf_Internal_Sym *sym;
2537 {
2538   if (h != NULL)
2539     {
2540       switch (ELF32_R_TYPE (rel->r_info))
2541         {
2542         case R_FRV_GNU_VTINHERIT:
2543         case R_FRV_GNU_VTENTRY:
2544           break;
2545
2546         default:
2547           switch (h->root.type)
2548             {
2549             default:
2550               break;
2551
2552             case bfd_link_hash_defined:
2553             case bfd_link_hash_defweak:
2554               return h->root.u.def.section;
2555
2556             case bfd_link_hash_common:
2557               return h->root.u.c.p->section;
2558             }
2559         }
2560     }
2561   else
2562     return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
2563
2564   return NULL;
2565 }
2566
2567 /* Update the got entry reference counts for the section being removed.  */
2568
2569 static bfd_boolean
2570 elf32_frv_gc_sweep_hook (abfd, info, sec, relocs)
2571      bfd *abfd ATTRIBUTE_UNUSED;
2572      struct bfd_link_info *info ATTRIBUTE_UNUSED;
2573      asection *sec ATTRIBUTE_UNUSED;
2574      const Elf_Internal_Rela *relocs ATTRIBUTE_UNUSED;
2575 {
2576   return TRUE;
2577 }
2578
2579 \f
2580 /* Hook called by the linker routine which adds symbols from an object
2581    file.  We use it to put .comm items in .scomm, and not .comm.  */
2582
2583 static bfd_boolean
2584 elf32_frv_add_symbol_hook (abfd, info, sym, namep, flagsp, secp, valp)
2585      bfd *abfd;
2586      struct bfd_link_info *info;
2587      const Elf_Internal_Sym *sym;
2588      const char **namep ATTRIBUTE_UNUSED;
2589      flagword *flagsp ATTRIBUTE_UNUSED;
2590      asection **secp;
2591      bfd_vma *valp;
2592 {
2593   if (sym->st_shndx == SHN_COMMON
2594       && !info->relocatable
2595       && (int)sym->st_size <= (int)bfd_get_gp_size (abfd))
2596     {
2597       /* Common symbols less than or equal to -G nn bytes are
2598          automatically put into .sbss.  */
2599
2600       asection *scomm = bfd_get_section_by_name (abfd, ".scommon");
2601
2602       if (scomm == NULL)
2603         {
2604           scomm = bfd_make_section (abfd, ".scommon");
2605           if (scomm == NULL
2606               || !bfd_set_section_flags (abfd, scomm, (SEC_ALLOC
2607                                                        | SEC_IS_COMMON
2608                                                        | SEC_LINKER_CREATED)))
2609             return FALSE;
2610         }
2611
2612       *secp = scomm;
2613       *valp = sym->st_size;
2614     }
2615
2616   return TRUE;
2617 }
2618 /* Create a .got section, as well as its additional info field.  This
2619    is almost entirely copied from
2620    elflink.c:_bfd_elf_create_got_section().  */
2621
2622 static bfd_boolean
2623 _frv_create_got_section (bfd *abfd, struct bfd_link_info *info)
2624 {
2625   flagword flags;
2626   asection *s;
2627   struct elf_link_hash_entry *h;
2628   struct bfd_link_hash_entry *bh;
2629   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2630   int ptralign;
2631
2632   /* This function may be called more than once.  */
2633   s = bfd_get_section_by_name (abfd, ".got");
2634   if (s != NULL && (s->flags & SEC_LINKER_CREATED) != 0)
2635     return TRUE;
2636
2637   /* Machine specific: although pointers are 32-bits wide, we want the
2638      GOT to be aligned to a 64-bit boundary, such that function
2639      descriptors in it can be accessed with 64-bit loads and
2640      stores.  */
2641   ptralign = 3;
2642
2643   flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
2644            | SEC_LINKER_CREATED);
2645
2646   s = bfd_make_section (abfd, ".got");
2647   if (s == NULL
2648       || !bfd_set_section_flags (abfd, s, flags)
2649       || !bfd_set_section_alignment (abfd, s, ptralign))
2650     return FALSE;
2651
2652   if (bed->want_got_plt)
2653     {
2654       s = bfd_make_section (abfd, ".got.plt");
2655       if (s == NULL
2656           || !bfd_set_section_flags (abfd, s, flags)
2657           || !bfd_set_section_alignment (abfd, s, ptralign))
2658         return FALSE;
2659     }
2660
2661   if (bed->want_got_sym)
2662     {
2663       /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
2664          (or .got.plt) section.  We don't do this in the linker script
2665          because we don't want to define the symbol if we are not creating
2666          a global offset table.  */
2667       bh = NULL;
2668       if (!(_bfd_generic_link_add_one_symbol
2669             (info, abfd, "_GLOBAL_OFFSET_TABLE_", BSF_GLOBAL, s,
2670              bed->got_symbol_offset, (const char *) NULL, FALSE,
2671              bed->collect, &bh)))
2672         return FALSE;
2673       h = (struct elf_link_hash_entry *) bh;
2674       h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
2675       h->type = STT_OBJECT;
2676
2677       /* Machine-specific: we want the symbol for executables as
2678          well.  */
2679       if (! _bfd_elf_link_record_dynamic_symbol (info, h))
2680         return FALSE;
2681
2682       elf_hash_table (info)->hgot = h;
2683     }
2684
2685   /* The first bit of the global offset table is the header.  */
2686   s->_raw_size += bed->got_header_size + bed->got_symbol_offset;
2687
2688   /* This is the machine-specific part.  Create and initialize section
2689      data for the got.  */
2690   frv_got_section (info) = s;
2691   frv_relocs_info (info) = htab_try_create (1, frv_pic_relocs_info_hash,
2692                                             frv_pic_relocs_info_eq,
2693                                             (htab_del) NULL);
2694   if (! frv_relocs_info (info))
2695     return FALSE;
2696
2697   s = bfd_make_section (abfd, ".rel.got");
2698   if (s == NULL
2699       || ! bfd_set_section_flags (abfd, s, (flags | SEC_READONLY))
2700       || ! bfd_set_section_alignment (abfd, s, 2))
2701     return FALSE;
2702
2703   frv_gotrel_section (info) = s;
2704
2705   /* Machine-specific.  */
2706   s = bfd_make_section (abfd, ".rofixup");
2707   if (s == NULL
2708       || ! bfd_set_section_flags (abfd, s, (flags | SEC_READONLY))
2709       || ! bfd_set_section_alignment (abfd, s, 2))
2710     return FALSE;
2711
2712   frv_gotfixup_section (info) = s;
2713
2714   /* Define _gp in .rofixup, for FDPIC.  If it turns out that
2715      we're linking with a different linker script, the linker script
2716      will override it.  */
2717   bh = NULL;
2718   if (!(_bfd_generic_link_add_one_symbol
2719         (info, abfd, "_gp", BSF_GLOBAL, s, -2048, (const char *) NULL, FALSE,
2720          bed->collect, &bh)))
2721     return FALSE;
2722   h = (struct elf_link_hash_entry *) bh;
2723   h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
2724   h->type = STT_OBJECT;
2725
2726   /* Machine-specific: we want the symbol for executables as well.  */
2727   if (! _bfd_elf_link_record_dynamic_symbol (info, h))
2728     return FALSE;
2729   
2730   return TRUE;
2731 }
2732
2733 /* Make sure the got and plt sections exist, and that our pointers in
2734    the link hash table point to them.  */
2735
2736 static bfd_boolean
2737 elf32_frv_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
2738 {
2739   /* This is mostly copied from
2740      elflink.c:_bfd_elf_create_dynamic_sections().  */
2741   flagword flags, pltflags;
2742   asection *s;
2743   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2744
2745   /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
2746      .rel[a].bss sections.  */
2747
2748   flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
2749            | SEC_LINKER_CREATED);
2750
2751   pltflags = flags;
2752   pltflags |= SEC_CODE;
2753   if (bed->plt_not_loaded)
2754     pltflags &= ~ (SEC_CODE | SEC_LOAD | SEC_HAS_CONTENTS);
2755   if (bed->plt_readonly)
2756     pltflags |= SEC_READONLY;
2757
2758   s = bfd_make_section (abfd, ".plt");
2759   if (s == NULL
2760       || ! bfd_set_section_flags (abfd, s, pltflags)
2761       || ! bfd_set_section_alignment (abfd, s, bed->plt_alignment))
2762     return FALSE;
2763   /* FRV-specific: remember it.  */
2764   frv_plt_section (info) = s;
2765
2766   if (bed->want_plt_sym)
2767     {
2768       /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
2769          .plt section.  */
2770       struct elf_link_hash_entry *h;
2771       struct bfd_link_hash_entry *bh = NULL;
2772
2773       if (! (_bfd_generic_link_add_one_symbol
2774              (info, abfd, "_PROCEDURE_LINKAGE_TABLE_", BSF_GLOBAL, s, 0, NULL,
2775               FALSE, get_elf_backend_data (abfd)->collect, &bh)))
2776         return FALSE;
2777       h = (struct elf_link_hash_entry *) bh;
2778       h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
2779       h->type = STT_OBJECT;
2780
2781       if (! info->executable
2782           && ! _bfd_elf_link_record_dynamic_symbol (info, h))
2783         return FALSE;
2784     }
2785
2786   /* FRV-specific: we want rel relocations for the plt.  */
2787   s = bfd_make_section (abfd, ".rel.plt");
2788   if (s == NULL
2789       || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
2790       || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
2791     return FALSE;
2792   /* FRV-specific: remember it.  */
2793   frv_pltrel_section (info) = s;
2794
2795   /* FRV-specific: we want to create the GOT in the FRV way.  */
2796   if (! _frv_create_got_section (abfd, info))
2797     return FALSE;
2798
2799   /* FRV-specific: make sure we created everything we wanted.  */
2800   BFD_ASSERT (frv_got_section (info) && frv_gotrel_section (info)
2801               && frv_gotfixup_section (info)
2802               && frv_plt_section (info) && frv_pltrel_section (info));
2803
2804   if (bed->want_dynbss)
2805     {
2806       /* The .dynbss section is a place to put symbols which are defined
2807          by dynamic objects, are referenced by regular objects, and are
2808          not functions.  We must allocate space for them in the process
2809          image and use a R_*_COPY reloc to tell the dynamic linker to
2810          initialize them at run time.  The linker script puts the .dynbss
2811          section into the .bss section of the final image.  */
2812       s = bfd_make_section (abfd, ".dynbss");
2813       if (s == NULL
2814           || ! bfd_set_section_flags (abfd, s, SEC_ALLOC | SEC_LINKER_CREATED))
2815         return FALSE;
2816
2817       /* The .rel[a].bss section holds copy relocs.  This section is not
2818      normally needed.  We need to create it here, though, so that the
2819      linker will map it to an output section.  We can't just create it
2820      only if we need it, because we will not know whether we need it
2821      until we have seen all the input files, and the first time the
2822      main linker code calls BFD after examining all the input files
2823      (size_dynamic_sections) the input sections have already been
2824      mapped to the output sections.  If the section turns out not to
2825      be needed, we can discard it later.  We will never need this
2826      section when generating a shared object, since they do not use
2827      copy relocs.  */
2828       if (! info->shared)
2829         {
2830           s = bfd_make_section (abfd,
2831                                 (bed->default_use_rela_p
2832                                  ? ".rela.bss" : ".rel.bss"));
2833           if (s == NULL
2834               || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
2835               || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
2836             return FALSE;
2837         }
2838     }
2839
2840   return TRUE;
2841 }
2842
2843 /* The name of the dynamic interpreter.  This is put in the .interp
2844    section.  */
2845
2846 #define ELF_DYNAMIC_INTERPRETER "/lib/ld.so.1"
2847
2848 #define DEFAULT_STACK_SIZE 0x20000
2849
2850 /* This structure is used to collect the number of entries present in
2851    each addressable range of the got.  */
2852 struct _frv_dynamic_got_info
2853 {
2854   /* Several bits of information about the current link.  */
2855   struct bfd_link_info *info;
2856   /* Total size needed for GOT entries within the 12-, 16- or 32-bit
2857      ranges.  */
2858   bfd_vma got12, gotlos, gothilo;
2859   /* Total size needed for function descriptor entries within the 12-,
2860      16- or 32-bit ranges.  */
2861   bfd_vma fd12, fdlos, fdhilo;
2862   /* Total size needed function descriptor entries referenced in PLT
2863      entries, that would be profitable to place in offsets close to
2864      the PIC register.  */
2865   bfd_vma fdplt;
2866   /* Total size needed by lazy PLT entries.  */
2867   bfd_vma lzplt;
2868   /* Number of relocations carried over from input object files.  */
2869   unsigned long relocs;
2870   /* Number of fixups introduced by relocations in input object files.  */
2871   unsigned long fixups;
2872 };
2873
2874 /* Compute the total GOT size required by each symbol in each range.
2875    Symbols may require up to 4 words in the GOT: an entry pointing to
2876    the symbol, an entry pointing to its function descriptor, and a
2877    private function descriptors taking two words.  */
2878
2879 static int
2880 _frv_count_got_plt_entries (void **entryp, void *dinfo_)
2881 {
2882   struct frv_pic_relocs_info *entry = *entryp;
2883   struct _frv_dynamic_got_info *dinfo = dinfo_;
2884   unsigned relocs = 0, fixups = 0;
2885
2886   /* Allocate space for a GOT entry pointing to the symbol.  */
2887   if (entry->got12)
2888     dinfo->got12 += 4;
2889   else if (entry->gotlos)
2890     dinfo->gotlos += 4;
2891   else if (entry->gothilo)
2892     dinfo->gothilo += 4;
2893   else
2894     entry->relocs32--;
2895   entry->relocs32++;
2896
2897   /* Allocate space for a GOT entry pointing to the function
2898      descriptor.  */
2899   if (entry->fdgot12)
2900     dinfo->got12 += 4;
2901   else if (entry->fdgotlos)
2902     dinfo->gotlos += 4;
2903   else if (entry->fdgothilo)
2904     dinfo->gothilo += 4;
2905   else
2906     entry->relocsfd--;
2907   entry->relocsfd++;
2908
2909   /* Decide whether we need a PLT entry, a function descriptor in the
2910      GOT, and a lazy PLT entry for this symbol.  */
2911   entry->plt = entry->call
2912     && entry->symndx == -1 && ! FRV_SYM_LOCAL (dinfo->info, entry->d.h)
2913     && elf_hash_table (dinfo->info)->dynamic_sections_created;
2914   entry->privfd = entry->plt
2915     || entry->fdgoff12 || entry->fdgofflos || entry->fdgoffhilo
2916     || ((entry->fd || entry->fdgot12 || entry->fdgotlos || entry->fdgothilo)
2917         && (entry->symndx != -1
2918             || FRV_FUNCDESC_LOCAL (dinfo->info, entry->d.h)));
2919   entry->lazyplt = entry->privfd
2920     && entry->symndx == -1 && ! FRV_SYM_LOCAL (dinfo->info, entry->d.h)
2921     && ! (dinfo->info->flags & DF_BIND_NOW)
2922     && elf_hash_table (dinfo->info)->dynamic_sections_created;
2923
2924   /* Allocate space for a function descriptor.  */
2925   if (entry->fdgoff12)
2926     dinfo->fd12 += 8;
2927   else if (entry->fdgofflos)
2928     dinfo->fdlos += 8;
2929   else if (entry->privfd && entry->plt)
2930     dinfo->fdplt += 8;
2931   else if (entry->privfd)
2932     dinfo->fdhilo += 8;
2933   else
2934     entry->relocsfdv--;
2935   entry->relocsfdv++;
2936
2937   if (entry->lazyplt)
2938     dinfo->lzplt += 8;
2939
2940   if (!dinfo->info->executable || dinfo->info->pie)
2941     relocs = entry->relocs32 + entry->relocsfd + entry->relocsfdv;
2942   else
2943     {
2944       if (entry->symndx != -1 || FRV_SYM_LOCAL (dinfo->info, entry->d.h))
2945         {
2946           if (entry->symndx != -1
2947               || entry->d.h->root.type != bfd_link_hash_undefweak)
2948             fixups += entry->relocs32 + 2 * entry->relocsfdv;
2949         }
2950       else
2951         relocs += entry->relocs32 + entry->relocsfdv;
2952
2953       if (entry->symndx != -1 || FRV_FUNCDESC_LOCAL (dinfo->info, entry->d.h))
2954         {
2955           if (entry->symndx != -1
2956               || entry->d.h->root.type != bfd_link_hash_undefweak)
2957             fixups += entry->relocsfd;
2958         }
2959       else
2960         relocs += entry->relocsfd;
2961     }
2962
2963   entry->dynrelocs += relocs;
2964   entry->fixups += fixups;
2965   dinfo->relocs += relocs;
2966   dinfo->fixups += fixups;
2967
2968   return 1;
2969 }
2970
2971 /* This structure is used to assign offsets to got entries, function
2972    descriptors, plt entries and lazy plt entries.  */
2973
2974 struct _frv_dynamic_got_plt_info
2975 {
2976   /* Summary information collected with _frv_count_got_plt_entries.  */
2977   struct _frv_dynamic_got_info g;
2978
2979   /* For each addressable range, we record a MAX (positive) and MIN
2980      (negative) value.  CUR is used to assign got entries, and it's
2981      incremented from an initial positive value to MAX, then from MIN
2982      to FDCUR (unless FDCUR wraps around first).  FDCUR is used to
2983      assign function descriptors, and it's decreased from an initial
2984      non-positive value to MIN, then from MAX down to CUR (unless CUR
2985      wraps around first).  All of MIN, MAX, CUR and FDCUR always point
2986      to even words.  ODD, if non-zero, indicates an odd word to be
2987      used for the next got entry, otherwise CUR is used and
2988      incremented by a pair of words, wrapping around when it reaches
2989      MAX.  FDCUR is decremented (and wrapped) before the next function
2990      descriptor is chosen.  FDPLT indicates the number of remaining
2991      slots that can be used for function descriptors used only by PLT
2992      entries.  */
2993   struct _frv_dynamic_got_alloc_data
2994   {
2995     bfd_signed_vma max, cur, odd, fdcur, min;
2996     bfd_vma fdplt;
2997   } got12, gotlos, gothilo;
2998 };
2999
3000 /* Determine the positive and negative ranges to be used by each
3001    offset range in the GOT.  FDCUR and CUR, that must be aligned to a
3002    double-word boundary, are the minimum (negative) and maximum
3003    (positive) GOT offsets already used by previous ranges, except for
3004    an ODD entry that may have been left behind.  GOT and FD indicate
3005    the size of GOT entries and function descriptors that must be
3006    placed within the range from -WRAP to WRAP.  If there's room left,
3007    up to FDPLT bytes should be reserved for additional function
3008    descriptors.  */
3009
3010 inline static bfd_signed_vma
3011 _frv_compute_got_alloc_data (struct _frv_dynamic_got_alloc_data *gad,
3012                              bfd_signed_vma fdcur,
3013                              bfd_signed_vma odd,
3014                              bfd_signed_vma cur,
3015                              bfd_vma got,
3016                              bfd_vma fd,
3017                              bfd_vma fdplt,
3018                              bfd_vma wrap)
3019 {
3020   bfd_signed_vma wrapmin = -wrap;
3021
3022   /* Start at the given initial points.  */
3023   gad->fdcur = fdcur;
3024   gad->cur = cur;
3025
3026   /* If we had an incoming odd word and we have any got entries that
3027      are going to use it, consume it, otherwise leave gad->odd at
3028      zero.  We might force gad->odd to zero and return the incoming
3029      odd such that it is used by the next range, but then GOT entries
3030      might appear to be out of order and we wouldn't be able to
3031      shorten the GOT by one word if it turns out to end with an
3032      unpaired GOT entry.  */
3033   if (odd && got)
3034     {
3035       gad->odd = odd;
3036       got -= 4;
3037       odd = 0;
3038     }
3039   else
3040     gad->odd = 0;
3041
3042   /* If we're left with an unpaired GOT entry, compute its location
3043      such that we can return it.  Otherwise, if got doesn't require an
3044      odd number of words here, either odd was already zero in the
3045      block above, or it was set to zero because got was non-zero, or
3046      got was already zero.  In the latter case, we want the value of
3047      odd to carry over to the return statement, so we don't want to
3048      reset odd unless the condition below is true.  */
3049   if (got & 4)
3050     {
3051       odd = cur + got;
3052       got += 4;
3053     }
3054   
3055   /* Compute the tentative boundaries of this range.  */
3056   gad->max = cur + got;
3057   gad->min = fdcur - fd;
3058   gad->fdplt = 0;
3059
3060   /* If function descriptors took too much space, wrap some of them
3061      around.  */
3062   if (gad->min < wrapmin)
3063     {
3064       gad->max += wrapmin - gad->min;
3065       gad->min = wrapmin;
3066     }
3067   /* If there is space left and we have function descriptors
3068      referenced in PLT entries that could take advantage of shorter
3069      offsets, place them here.  */
3070   else if (fdplt && gad->min > wrapmin)
3071     {
3072       bfd_vma fds;
3073       if ((bfd_vma) (gad->min - wrapmin) < fdplt)
3074         fds = gad->min - wrapmin;
3075       else
3076         fds = fdplt;
3077
3078       fdplt -= fds;
3079       gad->min -= fds;
3080       gad->fdplt += fds;
3081     }
3082
3083   /* If GOT entries took too much space, wrap some of them around.
3084      This may well cause gad->min to become lower than wrapmin.  This
3085      will cause a relocation overflow later on, so we don't have to
3086      report it here . */
3087   if ((bfd_vma) gad->max > wrap)
3088     {
3089       gad->min -= gad->max - wrap;
3090       gad->max = wrap;
3091     }
3092   /* If there is more space left, try to place some more function
3093      descriptors for PLT entries.  */
3094   else if (fdplt && (bfd_vma) gad->max < wrap)
3095     {
3096       bfd_vma fds;
3097       if ((bfd_vma) (wrap - gad->max) < fdplt)
3098         fds = wrap - gad->max;
3099       else
3100         fds = fdplt;
3101
3102       fdplt -= fds;
3103       gad->max += fds;
3104       gad->fdplt += fds;
3105     }
3106
3107   /* If odd was initially computed as an offset past the wrap point,
3108      wrap it around.  */
3109   if (odd > gad->max)
3110     odd = gad->min + odd - gad->max;
3111
3112   /* _frv_get_got_entry() below will always wrap gad->cur if needed
3113      before returning, so do it here too.  This guarantees that,
3114      should cur and fdcur meet at the wrap point, they'll both be
3115      equal to min.  */
3116   if (gad->cur == gad->max)
3117     gad->cur = gad->min;
3118
3119   return odd;
3120 }
3121
3122 /* Compute the location of the next GOT entry, given the allocation
3123    data for a range.  */
3124
3125 inline static bfd_signed_vma
3126 _frv_get_got_entry (struct _frv_dynamic_got_alloc_data *gad)
3127 {
3128   bfd_signed_vma ret;
3129   
3130   if (gad->odd)
3131     {
3132       /* If there was an odd word left behind, use it.  */
3133       ret = gad->odd;
3134       gad->odd = 0;
3135     }
3136   else
3137     {
3138       /* Otherwise, use the word pointed to by cur, reserve the next
3139          as an odd word, and skip to the next pair of words, possibly
3140          wrapping around.  */
3141       ret = gad->cur;
3142       gad->odd = gad->cur + 4;
3143       gad->cur += 8;
3144       if (gad->cur == gad->max)
3145         gad->cur = gad->min;
3146     }
3147
3148   return ret;
3149 }
3150
3151 /* Compute the location of the next function descriptor entry in the
3152    GOT, given the allocation data for a range.  */
3153
3154 inline static bfd_signed_vma
3155 _frv_get_fd_entry (struct _frv_dynamic_got_alloc_data *gad)
3156 {
3157   /* If we're at the bottom, wrap around, and only then allocate the
3158      next pair of words.  */
3159   if (gad->fdcur == gad->min)
3160     gad->fdcur = gad->max;
3161   return gad->fdcur -= 8;
3162 }
3163
3164 /* Assign GOT offsets for every GOT entry and function descriptor.
3165    Doing everything in a single pass is tricky.  */
3166
3167 static int
3168 _frv_assign_got_entries (void **entryp, void *info_)
3169 {
3170   struct frv_pic_relocs_info *entry = *entryp;
3171   struct _frv_dynamic_got_plt_info *dinfo = info_;
3172
3173   if (entry->got12)
3174     entry->got_entry = _frv_get_got_entry (&dinfo->got12);
3175   else if (entry->gotlos)
3176     entry->got_entry = _frv_get_got_entry (&dinfo->gotlos);
3177   else if (entry->gothilo)
3178     entry->got_entry = _frv_get_got_entry (&dinfo->gothilo);
3179
3180   if (entry->fdgot12)
3181     entry->fdgot_entry = _frv_get_got_entry (&dinfo->got12);
3182   else if (entry->fdgotlos)
3183     entry->fdgot_entry = _frv_get_got_entry (&dinfo->gotlos);
3184   else if (entry->fdgothilo)
3185     entry->fdgot_entry = _frv_get_got_entry (&dinfo->gothilo);
3186
3187   if (entry->fdgoff12)
3188     entry->fd_entry = _frv_get_fd_entry (&dinfo->got12);
3189   else if (entry->plt && dinfo->got12.fdplt)
3190     {
3191       dinfo->got12.fdplt -= 8;
3192       entry->fd_entry = _frv_get_fd_entry (&dinfo->got12);
3193     }
3194   else if (entry->fdgofflos)
3195     entry->fd_entry = _frv_get_fd_entry (&dinfo->gotlos);
3196   else if (entry->plt && dinfo->gotlos.fdplt)
3197     {
3198       dinfo->gotlos.fdplt -= 8;
3199       entry->fd_entry = _frv_get_fd_entry (&dinfo->gotlos);
3200     }
3201   else if (entry->plt)
3202     {
3203       dinfo->gothilo.fdplt -= 8;
3204       entry->fd_entry = _frv_get_fd_entry (&dinfo->gothilo);
3205     }
3206   else if (entry->privfd)
3207     entry->fd_entry = _frv_get_fd_entry (&dinfo->gothilo);
3208   
3209   return 1;
3210 }
3211
3212 /* Assign GOT offsets to private function descriptors used by PLT
3213    entries (or referenced by 32-bit offsets), as well as PLT entries
3214    and lazy PLT entries.  */
3215
3216 static int
3217 _frv_assign_plt_entries (void **entryp, void *info_)
3218 {
3219   struct frv_pic_relocs_info *entry = *entryp;
3220   struct _frv_dynamic_got_plt_info *dinfo = info_;
3221
3222   /* If this symbol requires a local function descriptor, allocate
3223      one.  */
3224   if (entry->privfd && entry->fd_entry == 0)
3225     {
3226       if (dinfo->got12.fdplt)
3227         {
3228           entry->fd_entry = _frv_get_fd_entry (&dinfo->got12);
3229           dinfo->got12.fdplt -= 8;
3230         }
3231       else if (dinfo->gotlos.fdplt)
3232         {
3233           entry->fd_entry = _frv_get_fd_entry (&dinfo->gotlos);
3234           dinfo->gotlos.fdplt -= 8;
3235         }
3236       else
3237         {
3238           BFD_ASSERT (dinfo->gothilo.fdplt)
3239           entry->fd_entry = _frv_get_fd_entry (&dinfo->gothilo);
3240           dinfo->gothilo.fdplt -= 8;
3241         }
3242     }
3243
3244   if (entry->plt)
3245     {
3246       int size;
3247
3248       /* We use the section's raw size to mark the location of the
3249          next PLT entry.  */
3250       entry->plt_entry = frv_plt_section (dinfo->g.info)->_raw_size;
3251
3252       /* Figure out the length of this PLT entry based on the
3253          addressing mode we need to reach the function descriptor.  */
3254       BFD_ASSERT (entry->fd_entry);
3255       if (entry->fd_entry >= -(1 << (12 - 1))
3256           && entry->fd_entry < (1 << (12 - 1)))
3257         size = 8;
3258       else if (entry->fd_entry >= -(1 << (16 - 1))
3259                && entry->fd_entry < (1 << (16 - 1)))
3260         size = 12;
3261       else
3262         size = 16;
3263
3264       frv_plt_section (dinfo->g.info)->_raw_size += size;
3265     }
3266
3267   if (entry->lazyplt)
3268     {
3269       entry->lzplt_entry = dinfo->g.lzplt;
3270       dinfo->g.lzplt += 8;
3271       /* If this entry is the one that gets the resolver stub, account
3272          for the additional instruction.  */
3273       if (entry->lzplt_entry % FRV_LZPLT_BLOCK_SIZE == FRV_LZPLT_RESOLV_LOC)
3274         dinfo->g.lzplt += 4;
3275     }
3276       
3277   return 1;
3278 }  
3279
3280 /* Follow indirect and warning hash entries so that each got entry
3281    points to the final symbol definition.  P must point to a pointer
3282    to the hash table we're traversing.  Since this traversal may
3283    modify the hash table, we set this pointer to NULL to indicate
3284    we've made a potentially-destructive change to the hash table, so
3285    the traversal must be restarted.  */
3286 static int
3287 _frv_resolve_final_relocs_info (void **entryp, void *p)
3288 {
3289   struct frv_pic_relocs_info *entry = *entryp;
3290   htab_t *htab = p;
3291
3292   if (entry->symndx == -1)
3293     {
3294       struct elf_link_hash_entry *h = entry->d.h;
3295       struct frv_pic_relocs_info *oentry;
3296
3297       while (h->root.type == bfd_link_hash_indirect
3298              || h->root.type == bfd_link_hash_warning)
3299         h = (struct elf_link_hash_entry *)h->root.u.i.link;
3300
3301       if (entry->d.h == h)
3302         return 1;
3303
3304       oentry = frv_pic_relocs_info_for_global (*htab, 0, h, entry->addend,
3305                                                NO_INSERT);
3306
3307       if (oentry)
3308         {
3309           /* Merge the two entries.  */
3310           frv_pic_merge_early_relocs_info (oentry, entry);
3311           htab_clear_slot (*htab, entryp);
3312           return 1;
3313         }
3314
3315       entry->d.h = h;
3316
3317       /* If we can't find this entry with the new bfd hash, re-insert
3318          it, and get the traversal restarted.  */
3319       if (! htab_find (*htab, entry))
3320         {
3321           htab_clear_slot (*htab, entryp);
3322           entryp = htab_find_slot (*htab, entry, INSERT);
3323           if (! *entryp)
3324             *entryp = entry;
3325           /* Abort the traversal, since the whole table may have
3326              moved, and leave it up to the parent to restart the
3327              process.  */
3328           *(htab_t *)p = NULL;
3329           return 0;
3330         }
3331     }
3332
3333   return 1;
3334 }
3335
3336 /* Set the sizes of the dynamic sections.  */
3337
3338 static bfd_boolean
3339 elf32_frv_size_dynamic_sections (bfd *output_bfd,
3340                                  struct bfd_link_info *info)
3341 {
3342   bfd *dynobj;
3343   asection *s;
3344   struct _frv_dynamic_got_plt_info gpinfo;
3345   bfd_signed_vma odd;
3346   bfd_vma limit;
3347
3348   dynobj = elf_hash_table (info)->dynobj;
3349   BFD_ASSERT (dynobj != NULL);
3350
3351   if (elf_hash_table (info)->dynamic_sections_created)
3352     {
3353       /* Set the contents of the .interp section to the interpreter.  */
3354       if (info->executable)
3355         {
3356           s = bfd_get_section_by_name (dynobj, ".interp");
3357           BFD_ASSERT (s != NULL);
3358           s->_raw_size = sizeof ELF_DYNAMIC_INTERPRETER;
3359           s->contents = (bfd_byte *) ELF_DYNAMIC_INTERPRETER;
3360         }
3361     }
3362
3363   memset (&gpinfo, 0, sizeof (gpinfo));
3364   gpinfo.g.info = info;
3365
3366   for (;;)
3367     {
3368       htab_t relocs = frv_relocs_info (info);
3369
3370       htab_traverse (relocs, _frv_resolve_final_relocs_info, &relocs);
3371
3372       if (relocs == frv_relocs_info (info))
3373         break;
3374     }
3375
3376   htab_traverse (frv_relocs_info (info), _frv_count_got_plt_entries,
3377                  &gpinfo.g);
3378
3379   odd = 12;
3380   /* Compute the total size taken by entries in the 12-bit and 16-bit
3381      ranges, to tell how many PLT function descriptors we can bring
3382      into the 12-bit range without causing the 16-bit range to
3383      overflow.  */
3384   limit = odd + gpinfo.g.got12 + gpinfo.g.gotlos
3385     + gpinfo.g.fd12 + gpinfo.g.fdlos;
3386   if (limit < (bfd_vma)1 << 16)
3387     limit = ((bfd_vma)1 << 16) - limit;
3388   else
3389     limit = 0;
3390   if (gpinfo.g.fdplt < limit)
3391     limit = gpinfo.g.fdplt;
3392
3393   /* Determine the ranges of GOT offsets that we can use for each
3394      range of addressing modes.  */
3395   odd = _frv_compute_got_alloc_data (&gpinfo.got12,
3396                                      0,
3397                                      odd,
3398                                      16,
3399                                      gpinfo.g.got12,
3400                                      gpinfo.g.fd12,
3401                                      limit,
3402                                      (bfd_vma)1 << (12-1));
3403   odd = _frv_compute_got_alloc_data (&gpinfo.gotlos,
3404                                      gpinfo.got12.min,
3405                                      odd,
3406                                      gpinfo.got12.max,
3407                                      gpinfo.g.gotlos,
3408                                      gpinfo.g.fdlos,
3409                                      gpinfo.g.fdplt - gpinfo.got12.fdplt,
3410                                      (bfd_vma)1 << (16-1));
3411   odd = _frv_compute_got_alloc_data (&gpinfo.gothilo,
3412                                      gpinfo.gotlos.min,
3413                                      odd,
3414                                      gpinfo.gotlos.max,
3415                                      gpinfo.g.gothilo,
3416                                      gpinfo.g.fdhilo,
3417                                      gpinfo.g.fdplt - gpinfo.got12.fdplt
3418                                      - gpinfo.gotlos.fdplt,
3419                                      (bfd_vma)1 << (32-1));
3420
3421   /* Now assign (most) GOT offsets.  */
3422   htab_traverse (frv_relocs_info (info), _frv_assign_got_entries, &gpinfo);
3423
3424   frv_got_section (info)->_raw_size = gpinfo.gothilo.max - gpinfo.gothilo.min
3425     /* If an odd word is the last word of the GOT, we don't need this
3426        word to be part of the GOT.  */
3427     - (odd + 4 == gpinfo.gothilo.max ? 4 : 0);
3428   if (frv_got_section (info)->_raw_size == 0)
3429     frv_got_section (info)->flags |= SEC_EXCLUDE;
3430   else if (frv_got_section (info)->_raw_size == 12
3431            && ! elf_hash_table (info)->dynamic_sections_created)
3432     {
3433       frv_got_section (info)->flags |= SEC_EXCLUDE;
3434       frv_got_section (info)->_raw_size = 0;
3435     }
3436   else
3437     {
3438       frv_got_section (info)->contents =
3439         (bfd_byte *) bfd_zalloc (dynobj, frv_got_section (info)->_raw_size);
3440       if (frv_got_section (info)->contents == NULL)
3441         return FALSE;
3442     }
3443   
3444   if (elf_hash_table (info)->dynamic_sections_created)
3445     /* Subtract the number of lzplt entries, since those will generate
3446        relocations in the pltrel section.  */
3447     frv_gotrel_section (info)->_raw_size =
3448       (gpinfo.g.relocs - gpinfo.g.lzplt / 8)
3449       * get_elf_backend_data (output_bfd)->s->sizeof_rel;
3450   else
3451     BFD_ASSERT (gpinfo.g.relocs == 0);
3452   if (frv_gotrel_section (info)->_raw_size == 0)
3453     frv_gotrel_section (info)->flags |= SEC_EXCLUDE;
3454   else
3455     {
3456       frv_gotrel_section (info)->contents =
3457         (bfd_byte *) bfd_zalloc (dynobj, frv_gotrel_section (info)->_raw_size);
3458       if (frv_gotrel_section (info)->contents == NULL)
3459         return FALSE;
3460     }
3461
3462   if (elf_elfheader (output_bfd)->e_flags & EF_FRV_FDPIC)
3463     frv_gotfixup_section (info)->_raw_size = (gpinfo.g.fixups + 1) * 4;
3464   if (frv_gotfixup_section (info)->_raw_size == 0)
3465     frv_gotfixup_section (info)->flags |= SEC_EXCLUDE;
3466   else
3467     {
3468       frv_gotfixup_section (info)->contents =
3469         (bfd_byte *) bfd_zalloc (dynobj,
3470                                  frv_gotfixup_section (info)->_raw_size);
3471       if (frv_gotfixup_section (info)->contents == NULL)
3472         return FALSE;
3473     }
3474   
3475   if (elf_hash_table (info)->dynamic_sections_created)
3476     {
3477       frv_pltrel_section (info)->_raw_size =
3478         gpinfo.g.lzplt / 8 * get_elf_backend_data (output_bfd)->s->sizeof_rel;
3479       if (frv_pltrel_section (info)->_raw_size == 0)
3480         frv_pltrel_section (info)->flags |= SEC_EXCLUDE;
3481       else
3482         {
3483           frv_pltrel_section (info)->contents =
3484             (bfd_byte *) bfd_zalloc (dynobj,
3485                                      frv_pltrel_section (info)->_raw_size);
3486           if (frv_pltrel_section (info)->contents == NULL)
3487             return FALSE;
3488         }
3489     }
3490   
3491   /* Add 4 bytes for every block of at most 65535 lazy PLT entries,
3492      such that there's room for the additional instruction needed to
3493      call the resolver.  Since _frv_assign_got_entries didn't account
3494      for them, our block size is 4 bytes smaller than the real block
3495      size.  */
3496   if (elf_hash_table (info)->dynamic_sections_created)
3497     {
3498       frv_plt_section (info)->_raw_size = gpinfo.g.lzplt
3499         + ((gpinfo.g.lzplt + (FRV_LZPLT_BLOCK_SIZE - 4) - 8)
3500            / (FRV_LZPLT_BLOCK_SIZE - 4) * 4);
3501     }
3502
3503   /* Reset it, such that _frv_assign_plt_entries() can use it to
3504      actually assign lazy PLT entries addresses.  */
3505   gpinfo.g.lzplt = 0;
3506
3507   /* Save information that we're going to need to generate GOT and PLT
3508      entries.  */
3509   frv_got_initial_offset (info) = -gpinfo.gothilo.min;
3510
3511   if (get_elf_backend_data (output_bfd)->want_got_sym)
3512     elf_hash_table (info)->hgot->root.u.def.value
3513       += frv_got_initial_offset (info);
3514
3515   if (elf_hash_table (info)->dynamic_sections_created)
3516     frv_plt_initial_offset (info) = frv_plt_section (info)->_raw_size;
3517
3518   htab_traverse (frv_relocs_info (info), _frv_assign_plt_entries, &gpinfo);
3519
3520   /* Allocate the PLT section contents only after
3521      _frv_assign_plt_entries has a chance to add the size of the
3522      non-lazy PLT entries.  */
3523   if (elf_hash_table (info)->dynamic_sections_created)
3524     {
3525       if (frv_plt_section (info)->_raw_size == 0)
3526         frv_plt_section (info)->flags |= SEC_EXCLUDE;
3527       else
3528         {
3529           frv_plt_section (info)->contents =
3530             (bfd_byte *) bfd_zalloc (dynobj, frv_plt_section (info)->_raw_size);
3531           if (frv_plt_section (info)->contents == NULL)
3532             return FALSE;
3533         }
3534     }
3535
3536   if (elf_hash_table (info)->dynamic_sections_created)
3537     {
3538       if (frv_got_section (info)->_raw_size)
3539         if (!_bfd_elf_add_dynamic_entry (info, DT_PLTGOT, 0))
3540           return FALSE;
3541
3542       if (frv_pltrel_section (info)->_raw_size)
3543         if (!_bfd_elf_add_dynamic_entry (info, DT_PLTRELSZ, 0)
3544             || !_bfd_elf_add_dynamic_entry (info, DT_PLTREL, DT_REL)
3545             || !_bfd_elf_add_dynamic_entry (info, DT_JMPREL, 0))
3546           return FALSE;
3547
3548       if (frv_gotrel_section (info)->_raw_size)
3549         if (!_bfd_elf_add_dynamic_entry (info, DT_REL, 0)
3550             || !_bfd_elf_add_dynamic_entry (info, DT_RELSZ, 0)
3551             || !_bfd_elf_add_dynamic_entry (info, DT_RELENT,
3552                                             sizeof (Elf32_External_Rel)))
3553           return FALSE;
3554     }
3555
3556   return TRUE;
3557 }
3558
3559 static bfd_boolean
3560 elf32_frv_always_size_sections (bfd *output_bfd,
3561                                 struct bfd_link_info *info)
3562 {
3563   if (!info->relocatable
3564       && elf_elfheader (output_bfd)->e_flags & EF_FRV_FDPIC)
3565     {
3566       struct elf_link_hash_entry *h;
3567       asection *sec;
3568
3569       /* Force a PT_GNU_STACK segment to be created.  */
3570       if (! elf_tdata (output_bfd)->stack_flags)
3571         elf_tdata (output_bfd)->stack_flags = PF_R | PF_W | PF_X;
3572
3573       /* Define __stacksize if it's not defined yet.  */
3574       h = elf_link_hash_lookup (elf_hash_table (info), "__stacksize",
3575                                 FALSE, FALSE, FALSE);
3576       if (! h || h->root.type != bfd_link_hash_defined
3577           || h->type != STT_OBJECT
3578           || !(h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR))
3579         {
3580           struct bfd_link_hash_entry *bh = NULL;
3581
3582           if (!(_bfd_generic_link_add_one_symbol
3583                 (info, output_bfd, "__stacksize",
3584                  BSF_GLOBAL, bfd_abs_section_ptr, DEFAULT_STACK_SIZE,
3585                  (const char *) NULL, FALSE,
3586                  get_elf_backend_data (output_bfd)->collect, &bh)))
3587             return FALSE;
3588
3589           h = (struct elf_link_hash_entry *) bh;
3590           h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
3591           h->type = STT_OBJECT;
3592         }
3593
3594       /* Create a stack section, and set its alignment.  */
3595       sec = bfd_make_section (output_bfd, ".stack");
3596
3597       if (sec == NULL
3598           || ! bfd_set_section_alignment (output_bfd, sec, 3))
3599         return FALSE;
3600     }
3601
3602   return TRUE;
3603 }
3604
3605 static bfd_boolean
3606 elf32_frv_modify_segment_map (bfd *output_bfd,
3607                               struct bfd_link_info *info)
3608 {
3609   if (elf_elfheader (output_bfd)->e_flags & EF_FRV_FDPIC)
3610     {
3611       struct elf_segment_map *m;
3612
3613       for (m = elf_tdata (output_bfd)->segment_map; m != NULL; m = m->next)
3614         if (m->p_type == PT_GNU_STACK)
3615           break;
3616
3617       if (m)
3618         {
3619           asection *sec = bfd_get_section_by_name (output_bfd, ".stack");
3620           struct elf_link_hash_entry *h;
3621
3622           if (sec)
3623             {
3624               /* Obtain the pointer to the __stacksize symbol.  */
3625               h = elf_link_hash_lookup (elf_hash_table (info), "__stacksize",
3626                                         FALSE, FALSE, FALSE);
3627               while (h->root.type == bfd_link_hash_indirect
3628                      || h->root.type == bfd_link_hash_warning)
3629                 h = (struct elf_link_hash_entry *)h->root.u.i.link;
3630               BFD_ASSERT (h->root.type == bfd_link_hash_defined);
3631
3632               /* Set the section size from the symbol value.  We
3633                  intentionally ignore the symbol section.  */
3634               if (h->root.type == bfd_link_hash_defined)
3635                 sec->_raw_size = h->root.u.def.value;
3636               else
3637                 sec->_raw_size = DEFAULT_STACK_SIZE;
3638
3639               /* Add the stack section to the PT_GNU_STACK segment,
3640                  such that its size and alignment requirements make it
3641                  to the segment.  */
3642               m->sections[m->count] = sec;
3643               m->count++;
3644             }
3645         }
3646     }
3647
3648   return TRUE;
3649 }
3650
3651 /* Fill in code and data in dynamic sections.  */
3652
3653 static bfd_boolean
3654 elf32_frv_finish_dynamic_sections (bfd *output_bfd,
3655                                    struct bfd_link_info *info)
3656 {
3657   bfd *dynobj;
3658   asection *sdyn;
3659
3660   dynobj = elf_hash_table (info)->dynobj;
3661
3662   if (frv_got_section (info))
3663     {
3664       BFD_ASSERT (frv_gotrel_section (info)->_raw_size
3665                   == (frv_gotrel_section (info)->reloc_count
3666                       * sizeof (Elf32_External_Rel)));
3667
3668       if (frv_gotfixup_section (info))
3669         {
3670           if (elf_elfheader (output_bfd)->e_flags & EF_FRV_FDPIC)
3671             {
3672               struct elf_link_hash_entry *hgot = elf_hash_table (info)->hgot;
3673               bfd_vma got_value = hgot->root.u.def.value
3674                 + hgot->root.u.def.section->output_section->vma
3675                 + hgot->root.u.def.section->output_offset;
3676
3677               _frv_add_rofixup (output_bfd, frv_gotfixup_section (info),
3678                                 got_value, 0);
3679             }
3680
3681           if (frv_gotfixup_section (info)->_raw_size
3682               != (frv_gotfixup_section (info)->reloc_count * 4))
3683             {
3684               if (frv_gotfixup_section (info)->_raw_size
3685                   < frv_gotfixup_section (info)->reloc_count * 4)
3686                 {
3687                   info->callbacks->warning
3688                     (info, "LINKER BUG: .rofixup section size mismatch",
3689                      ".rofixup", NULL, NULL, 0);
3690                   abort ();
3691                   return FALSE;
3692                 }
3693               else if (!elf_hash_table (info)->dynamic_sections_created)
3694                 {
3695                   info->callbacks->warning
3696                     (info, "no dynamic sections, missing -melf32frvfd?",
3697                      ".rofixup", NULL, NULL, 0);
3698                   return FALSE;
3699                 }
3700               BFD_ASSERT (0);
3701             }
3702         }
3703     }
3704   if (elf_hash_table (info)->dynamic_sections_created)
3705     {
3706       BFD_ASSERT (frv_pltrel_section (info)->_raw_size
3707                   == (frv_pltrel_section (info)->reloc_count
3708                       * sizeof (Elf32_External_Rel)));
3709     }
3710
3711   sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
3712
3713   if (elf_hash_table (info)->dynamic_sections_created)
3714     {
3715       Elf32_External_Dyn * dyncon;
3716       Elf32_External_Dyn * dynconend;
3717
3718       BFD_ASSERT (sdyn != NULL);
3719
3720       dyncon = (Elf32_External_Dyn *) sdyn->contents;
3721       dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->_raw_size);
3722
3723       for (; dyncon < dynconend; dyncon++)
3724         {
3725           Elf_Internal_Dyn dyn;
3726
3727           bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
3728
3729           switch (dyn.d_tag)
3730             {
3731             default:
3732               break;
3733
3734             case DT_PLTGOT:
3735               dyn.d_un.d_ptr = frv_got_section (info)->output_section->vma
3736                 + frv_got_section (info)->output_offset
3737                 + frv_got_initial_offset (info);
3738               bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
3739               break;
3740
3741             case DT_JMPREL:
3742               dyn.d_un.d_ptr = frv_pltrel_section (info)->output_section->vma
3743                 + frv_pltrel_section (info)->output_offset;
3744               bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
3745               break;
3746
3747             case DT_PLTRELSZ:
3748               if (frv_pltrel_section (info)->_cooked_size != 0)
3749                 dyn.d_un.d_val = frv_pltrel_section (info)->_cooked_size;
3750               else
3751                 dyn.d_un.d_val = frv_pltrel_section (info)->_raw_size;
3752               bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
3753               break;
3754             }
3755         }
3756     }
3757
3758   return TRUE;
3759 }
3760
3761 /* Adjust a symbol defined by a dynamic object and referenced by a
3762    regular object.  */
3763
3764 static bfd_boolean
3765 elf32_frv_adjust_dynamic_symbol (struct bfd_link_info *info ATTRIBUTE_UNUSED,
3766                                  struct elf_link_hash_entry *h ATTRIBUTE_UNUSED)
3767 {
3768   bfd * dynobj;
3769
3770   dynobj = elf_hash_table (info)->dynobj;
3771
3772   /* Make sure we know what is going on here.  */
3773   BFD_ASSERT (dynobj != NULL
3774               && (h->weakdef != NULL
3775                   || ((h->elf_link_hash_flags
3776                        & ELF_LINK_HASH_DEF_DYNAMIC) != 0
3777                       && (h->elf_link_hash_flags
3778                           & ELF_LINK_HASH_REF_REGULAR) != 0
3779                       && (h->elf_link_hash_flags
3780                           & ELF_LINK_HASH_DEF_REGULAR) == 0)));
3781
3782   /* If this is a weak symbol, and there is a real definition, the
3783      processor independent code will have arranged for us to see the
3784      real definition first, and we can just use the same value.  */
3785   if (h->weakdef != NULL)
3786     {
3787       BFD_ASSERT (h->weakdef->root.type == bfd_link_hash_defined
3788                   || h->weakdef->root.type == bfd_link_hash_defweak);
3789       h->root.u.def.section = h->weakdef->root.u.def.section;
3790       h->root.u.def.value = h->weakdef->root.u.def.value;
3791     }
3792
3793   return TRUE;
3794 }
3795
3796 /* Perform any actions needed for dynamic symbols.  */
3797
3798 static bfd_boolean
3799 elf32_frv_finish_dynamic_symbol (bfd *output_bfd ATTRIBUTE_UNUSED,
3800                                  struct bfd_link_info *info ATTRIBUTE_UNUSED,
3801                                  struct elf_link_hash_entry *h ATTRIBUTE_UNUSED,
3802                                  Elf_Internal_Sym *sym ATTRIBUTE_UNUSED)
3803 {
3804   return TRUE;
3805 }
3806
3807 /* Decide whether to attempt to turn absptr or lsda encodings in
3808    shared libraries into pcrel within the given input section.  */
3809
3810 static bfd_boolean
3811 frv_elf_use_relative_eh_frame (bfd *input_bfd,
3812                                struct bfd_link_info *info ATTRIBUTE_UNUSED,
3813                                asection *eh_frame_section ATTRIBUTE_UNUSED)
3814 {
3815   /* We can't use PC-relative encodings in FDPIC binaries, in general.  */
3816   if (elf_elfheader (input_bfd)->e_flags & EF_FRV_FDPIC)
3817     return FALSE;
3818
3819   return TRUE;
3820 }
3821
3822 /* Adjust the contents of an eh_frame_hdr section before they're output.  */
3823
3824 static bfd_byte
3825 frv_elf_encode_eh_address (bfd *abfd,
3826                            struct bfd_link_info *info,
3827                            asection *osec, bfd_vma offset,
3828                            asection *loc_sec, bfd_vma loc_offset,
3829                            bfd_vma *encoded)
3830 {
3831   struct elf_link_hash_entry *h;
3832
3833   /* Non-FDPIC binaries can use PC-relative encodings.  */
3834   if (! (elf_elfheader (abfd)->e_flags & EF_FRV_FDPIC))
3835     return _bfd_elf_encode_eh_address (abfd, info, osec, offset,
3836                                        loc_sec, loc_offset, encoded);
3837
3838   h = elf_hash_table (info)->hgot;
3839   BFD_ASSERT (h && h->root.type == bfd_link_hash_defined);
3840
3841   if (! h || (_frv_osec_to_segment (abfd, osec)
3842               == _frv_osec_to_segment (abfd, loc_sec->output_section)))
3843     return _bfd_elf_encode_eh_address (abfd, info, osec, offset,
3844                                        loc_sec, loc_offset, encoded);
3845
3846   BFD_ASSERT (_frv_osec_to_segment (abfd, osec)
3847               == _frv_osec_to_segment (abfd,
3848                                        h->root.u.def.section->output_section));
3849
3850   *encoded = osec->vma + offset
3851     - (h->root.u.def.value
3852        + h->root.u.def.section->output_section->vma
3853        + h->root.u.def.section->output_offset);
3854
3855   return DW_EH_PE_datarel | DW_EH_PE_sdata4;
3856 }
3857
3858 /* Look through the relocs for a section during the first phase.
3859
3860    Besides handling virtual table relocs for gc, we have to deal with
3861    all sorts of PIC-related relocations.  We describe below the
3862    general plan on how to handle such relocations, even though we only
3863    collect information at this point, storing them in hash tables for
3864    perusal of later passes.
3865
3866    32 relocations are propagated to the linker output when creating
3867    position-independent output.  LO16 and HI16 relocations are not
3868    supposed to be encountered in this case.
3869
3870    LABEL16 should always be resolvable by the linker, since it's only
3871    used by branches.
3872
3873    LABEL24, on the other hand, is used by calls.  If it turns out that
3874    the target of a call is a dynamic symbol, a PLT entry must be
3875    created for it, which triggers the creation of a private function
3876    descriptor and, unless lazy binding is disabled, a lazy PLT entry.
3877
3878    GPREL relocations require the referenced symbol to be in the same
3879    segment as _gp, but this can only be checked later.
3880
3881    All GOT, GOTOFF and FUNCDESC relocations require a .got section to
3882    exist.  LABEL24 might as well, since it may require a PLT entry,
3883    that will require a got.
3884
3885    Non-FUNCDESC GOT relocations require a GOT entry to be created
3886    regardless of whether the symbol is dynamic.  However, since a
3887    global symbol that turns out to not be exported may have the same
3888    address of a non-dynamic symbol, we don't assign GOT entries at
3889    this point, such that we can share them in this case.  A relocation
3890    for the GOT entry always has to be created, be it to offset a
3891    private symbol by the section load address, be it to get the symbol
3892    resolved dynamically.
3893
3894    FUNCDESC GOT relocations require a GOT entry to be created, and
3895    handled as if a FUNCDESC relocation was applied to the GOT entry in
3896    an object file.
3897
3898    FUNCDESC relocations referencing a symbol that turns out to NOT be
3899    dynamic cause a private function descriptor to be created.  The
3900    FUNCDESC relocation then decays to a 32 relocation that points at
3901    the private descriptor.  If the symbol is dynamic, the FUNCDESC
3902    relocation is propagated to the linker output, such that the
3903    dynamic linker creates the canonical descriptor, pointing to the
3904    dynamically-resolved definition of the function.
3905
3906    Non-FUNCDESC GOTOFF relocations must always refer to non-dynamic
3907    symbols that are assigned to the same segment as the GOT, but we
3908    can only check this later, after we know the complete set of
3909    symbols defined and/or exported.
3910
3911    FUNCDESC GOTOFF relocations require a function descriptor to be
3912    created and, unless lazy binding is disabled or the symbol is not
3913    dynamic, a lazy PLT entry.  Since we can't tell at this point
3914    whether a symbol is going to be dynamic, we have to decide later
3915    whether to create a lazy PLT entry or bind the descriptor directly
3916    to the private function.
3917
3918    FUNCDESC_VALUE relocations are not supposed to be present in object
3919    files, but they may very well be simply propagated to the linker
3920    output, since they have no side effect.
3921
3922
3923    A function descriptor always requires a FUNCDESC_VALUE relocation.
3924    Whether it's in .plt.rel or not depends on whether lazy binding is
3925    enabled and on whether the referenced symbol is dynamic.
3926
3927    The existence of a lazy PLT requires the resolverStub lazy PLT
3928    entry to be present.
3929
3930
3931    As for assignment of GOT, PLT and lazy PLT entries, and private
3932    descriptors, we might do them all sequentially, but we can do
3933    better than that.  For example, we can place GOT entries and
3934    private function descriptors referenced using 12-bit operands
3935    closer to the PIC register value, such that these relocations don't
3936    overflow.  Those that are only referenced with LO16 relocations
3937    could come next, but we may as well place PLT-required function
3938    descriptors in the 12-bit range to make them shorter.  Symbols
3939    referenced with LO16/HI16 may come next, but we may place
3940    additional function descriptors in the 16-bit range if we can
3941    reliably tell that we've already placed entries that are ever
3942    referenced with only LO16.  PLT entries are therefore generated as
3943    small as possible, while not introducing relocation overflows in
3944    GOT or FUNCDESC_GOTOFF relocations.  Lazy PLT entries could be
3945    generated before or after PLT entries, but not intermingled with
3946    them, such that we can have more lazy PLT entries in range for a
3947    branch to the resolverStub.  The resolverStub should be emitted at
3948    the most distant location from the first lazy PLT entry such that
3949    it's still in range for a branch, or closer, if there isn't a need
3950    for so many lazy PLT entries.  Additional lazy PLT entries may be
3951    emitted after the resolverStub, as long as branches are still in
3952    range.  If the branch goes out of range, longer lazy PLT entries
3953    are emitted.
3954
3955    We could further optimize PLT and lazy PLT entries by giving them
3956    priority in assignment to closer-to-gr17 locations depending on the
3957    number of occurrences of references to them (assuming a function
3958    that's called more often is more important for performance, so its
3959    PLT entry should be faster), or taking hints from the compiler.
3960    Given infinite time and money... :-)  */
3961
3962 static bfd_boolean
3963 elf32_frv_check_relocs (abfd, info, sec, relocs)
3964      bfd *abfd;
3965      struct bfd_link_info *info;
3966      asection *sec;
3967      const Elf_Internal_Rela *relocs;
3968 {
3969   Elf_Internal_Shdr *symtab_hdr;
3970   struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
3971   const Elf_Internal_Rela *rel;
3972   const Elf_Internal_Rela *rel_end;
3973   bfd *dynobj;
3974   struct frv_pic_relocs_info *picrel;
3975
3976   if (info->relocatable)
3977     return TRUE;
3978
3979   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
3980   sym_hashes = elf_sym_hashes (abfd);
3981   sym_hashes_end = sym_hashes + symtab_hdr->sh_size/sizeof(Elf32_External_Sym);
3982   if (!elf_bad_symtab (abfd))
3983     sym_hashes_end -= symtab_hdr->sh_info;
3984
3985   dynobj = elf_hash_table (info)->dynobj;
3986   rel_end = relocs + sec->reloc_count;
3987   for (rel = relocs; rel < rel_end; rel++)
3988     {
3989       struct elf_link_hash_entry *h;
3990       unsigned long r_symndx;
3991
3992       r_symndx = ELF32_R_SYM (rel->r_info);
3993       if (r_symndx < symtab_hdr->sh_info)
3994         h = NULL;
3995       else
3996         h = sym_hashes[r_symndx - symtab_hdr->sh_info];
3997
3998       switch (ELF32_R_TYPE (rel->r_info))
3999         {
4000         case R_FRV_LABEL24:
4001         case R_FRV_32:
4002         case R_FRV_GOT12:
4003         case R_FRV_GOTHI:
4004         case R_FRV_GOTLO:
4005         case R_FRV_FUNCDESC_GOT12:
4006         case R_FRV_FUNCDESC_GOTHI:
4007         case R_FRV_FUNCDESC_GOTLO:
4008         case R_FRV_GOTOFF12:
4009         case R_FRV_GOTOFFHI:
4010         case R_FRV_GOTOFFLO:
4011         case R_FRV_FUNCDESC_GOTOFF12:
4012         case R_FRV_FUNCDESC_GOTOFFHI:
4013         case R_FRV_FUNCDESC_GOTOFFLO:
4014         case R_FRV_FUNCDESC:
4015         case R_FRV_FUNCDESC_VALUE:
4016           if (! dynobj)
4017             {
4018               elf_hash_table (info)->dynobj = dynobj = abfd;
4019               if (! _frv_create_got_section (abfd, info))
4020                 return FALSE;
4021             }
4022           if (h != NULL)
4023             {
4024               if (h->dynindx == -1)
4025                 switch (ELF_ST_VISIBILITY (h->other))
4026                   {
4027                   case STV_INTERNAL:
4028                   case STV_HIDDEN:
4029                     break;
4030                   default:
4031                     bfd_elf32_link_record_dynamic_symbol (info, h);
4032                     break;
4033                   }
4034               picrel
4035                 = frv_pic_relocs_info_for_global (frv_relocs_info (info),
4036                                                   abfd, h,
4037                                                   rel->r_addend, INSERT);
4038             }
4039           else
4040             picrel = frv_pic_relocs_info_for_local (frv_relocs_info (info),
4041                                                     abfd, r_symndx,
4042                                                     rel->r_addend, INSERT);
4043           if (! picrel)
4044             return FALSE;
4045           break;
4046
4047         default:
4048           picrel = NULL;
4049           break;
4050         }
4051       
4052       switch (ELF32_R_TYPE (rel->r_info))
4053         {
4054         case R_FRV_LABEL24:
4055           picrel->call = 1;
4056           break;
4057                 
4058         case R_FRV_FUNCDESC_VALUE:
4059           picrel->relocsfdv++;
4060           if (bfd_get_section_flags (abfd, sec) & SEC_ALLOC)
4061             picrel->relocs32--;
4062           /* Fall through.  */
4063         case R_FRV_32:
4064           picrel->sym = 1;
4065           if (bfd_get_section_flags (abfd, sec) & SEC_ALLOC)
4066             picrel->relocs32++;
4067           break;
4068             
4069         case R_FRV_GOT12:
4070           picrel->got12 = 1;
4071           break;
4072             
4073         case R_FRV_GOTHI:
4074         case R_FRV_GOTLO:
4075           picrel->gothilo = 1;
4076           break;
4077
4078         case R_FRV_FUNCDESC_GOT12:
4079           picrel->fdgot12 = 1;
4080           break;
4081             
4082         case R_FRV_FUNCDESC_GOTHI:
4083         case R_FRV_FUNCDESC_GOTLO:
4084           picrel->fdgothilo = 1;
4085           break;
4086             
4087         case R_FRV_GOTOFF12:
4088         case R_FRV_GOTOFFHI:
4089         case R_FRV_GOTOFFLO:
4090           picrel->gotoff = 1;
4091           break;
4092             
4093         case R_FRV_FUNCDESC_GOTOFF12:
4094           picrel->fdgoff12 = 1;
4095           break;
4096             
4097         case R_FRV_FUNCDESC_GOTOFFHI:
4098         case R_FRV_FUNCDESC_GOTOFFLO:
4099           picrel->fdgoffhilo = 1;
4100           break;
4101             
4102         case R_FRV_FUNCDESC:
4103           picrel->fd = 1;
4104           picrel->relocsfd++;
4105           break;
4106           
4107         /* This relocation describes the C++ object vtable hierarchy.
4108            Reconstruct it for later use during GC.  */
4109         case R_FRV_GNU_VTINHERIT:
4110           if (!_bfd_elf32_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
4111             return FALSE;
4112           break;
4113
4114         /* This relocation describes which C++ vtable entries are actually
4115            used.  Record for later use during GC.  */
4116         case R_FRV_GNU_VTENTRY:
4117           if (!_bfd_elf32_gc_record_vtentry (abfd, sec, h, rel->r_addend))
4118             return FALSE;
4119           break;
4120         }
4121     }
4122
4123   return TRUE;
4124 }
4125
4126 \f
4127 /* Return the machine subcode from the ELF e_flags header.  */
4128
4129 static int
4130 elf32_frv_machine (abfd)
4131      bfd *abfd;
4132 {
4133   switch (elf_elfheader (abfd)->e_flags & EF_FRV_CPU_MASK)
4134     {
4135     default:                break;
4136     case EF_FRV_CPU_FR550:  return bfd_mach_fr550;
4137     case EF_FRV_CPU_FR500:  return bfd_mach_fr500;
4138     case EF_FRV_CPU_FR450:  return bfd_mach_fr450;
4139     case EF_FRV_CPU_FR405:  return bfd_mach_fr400;
4140     case EF_FRV_CPU_FR400:  return bfd_mach_fr400;
4141     case EF_FRV_CPU_FR300:  return bfd_mach_fr300;
4142     case EF_FRV_CPU_SIMPLE: return bfd_mach_frvsimple;
4143     case EF_FRV_CPU_TOMCAT: return bfd_mach_frvtomcat;
4144     }
4145
4146   return bfd_mach_frv;
4147 }
4148
4149 /* Set the right machine number for a FRV ELF file.  */
4150
4151 static bfd_boolean
4152 elf32_frv_object_p (abfd)
4153      bfd *abfd;
4154 {
4155   bfd_default_set_arch_mach (abfd, bfd_arch_frv, elf32_frv_machine (abfd));
4156   return TRUE;
4157 }
4158 \f
4159 /* Function to set the ELF flag bits.  */
4160
4161 static bfd_boolean
4162 frv_elf_set_private_flags (abfd, flags)
4163      bfd *abfd;
4164      flagword flags;
4165 {
4166   elf_elfheader (abfd)->e_flags = flags;
4167   elf_flags_init (abfd) = TRUE;
4168   return TRUE;
4169 }
4170
4171 /* Copy backend specific data from one object module to another.  */
4172
4173 static bfd_boolean
4174 frv_elf_copy_private_bfd_data (ibfd, obfd)
4175      bfd *ibfd;
4176      bfd *obfd;
4177 {
4178   if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
4179       || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
4180     return TRUE;
4181
4182   BFD_ASSERT (!elf_flags_init (obfd)
4183               || elf_elfheader (obfd)->e_flags == elf_elfheader (ibfd)->e_flags);
4184
4185   elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
4186   elf_flags_init (obfd) = TRUE;
4187   return TRUE;
4188 }
4189
4190 /* Return true if the architecture described by elf header flag
4191    EXTENSION is an extension of the architecture described by BASE.  */
4192
4193 static bfd_boolean
4194 frv_elf_arch_extension_p (flagword base, flagword extension)
4195 {
4196   if (base == extension)
4197     return TRUE;
4198
4199   /* CPU_GENERIC code can be merged with code for a specific
4200      architecture, in which case the result is marked as being
4201      for the specific architecture.  Everything is therefore
4202      an extension of CPU_GENERIC.  */
4203   if (base == EF_FRV_CPU_GENERIC)
4204     return TRUE;
4205
4206   if (extension == EF_FRV_CPU_FR450)
4207     if (base == EF_FRV_CPU_FR400 || base == EF_FRV_CPU_FR405)
4208       return TRUE;
4209
4210   if (extension == EF_FRV_CPU_FR405)
4211     if (base == EF_FRV_CPU_FR400)
4212       return TRUE;
4213
4214   return FALSE;
4215 }
4216
4217 /* Merge backend specific data from an object file to the output
4218    object file when linking.  */
4219
4220 static bfd_boolean
4221 frv_elf_merge_private_bfd_data (ibfd, obfd)
4222      bfd *ibfd;
4223      bfd *obfd;
4224 {
4225   flagword old_flags, old_partial;
4226   flagword new_flags, new_partial;
4227   bfd_boolean error = FALSE;
4228   char new_opt[80];
4229   char old_opt[80];
4230
4231   new_opt[0] = old_opt[0] = '\0';
4232   new_flags = elf_elfheader (ibfd)->e_flags;
4233   old_flags = elf_elfheader (obfd)->e_flags;
4234
4235   if (new_flags & EF_FRV_FDPIC)
4236     new_flags &= ~EF_FRV_PIC;
4237
4238 #ifdef DEBUG
4239   (*_bfd_error_handler) ("old_flags = 0x%.8lx, new_flags = 0x%.8lx, init = %s, filename = %s",
4240                          old_flags, new_flags, elf_flags_init (obfd) ? "yes" : "no",
4241                          bfd_get_filename (ibfd));
4242 #endif
4243
4244   if (!elf_flags_init (obfd))                   /* First call, no flags set.  */
4245     {
4246       elf_flags_init (obfd) = TRUE;
4247       old_flags = new_flags;
4248     }
4249
4250   else if (new_flags == old_flags)              /* Compatible flags are ok.  */
4251     ;
4252
4253   else                                          /* Possibly incompatible flags.  */
4254     {
4255       /* Warn if different # of gprs are used.  Note, 0 means nothing is
4256          said about the size of gprs.  */
4257       new_partial = (new_flags & EF_FRV_GPR_MASK);
4258       old_partial = (old_flags & EF_FRV_GPR_MASK);
4259       if (new_partial == old_partial)
4260         ;
4261
4262       else if (new_partial == 0)
4263         ;
4264
4265       else if (old_partial == 0)
4266         old_flags |= new_partial;
4267
4268       else
4269         {
4270           switch (new_partial)
4271             {
4272             default:            strcat (new_opt, " -mgpr-??"); break;
4273             case EF_FRV_GPR_32: strcat (new_opt, " -mgpr-32"); break;
4274             case EF_FRV_GPR_64: strcat (new_opt, " -mgpr-64"); break;
4275             }
4276
4277           switch (old_partial)
4278             {
4279             default:            strcat (old_opt, " -mgpr-??"); break;
4280             case EF_FRV_GPR_32: strcat (old_opt, " -mgpr-32"); break;
4281             case EF_FRV_GPR_64: strcat (old_opt, " -mgpr-64"); break;
4282             }
4283         }
4284
4285       /* Warn if different # of fprs are used.  Note, 0 means nothing is
4286          said about the size of fprs.  */
4287       new_partial = (new_flags & EF_FRV_FPR_MASK);
4288       old_partial = (old_flags & EF_FRV_FPR_MASK);
4289       if (new_partial == old_partial)
4290         ;
4291
4292       else if (new_partial == 0)
4293         ;
4294
4295       else if (old_partial == 0)
4296         old_flags |= new_partial;
4297
4298       else
4299         {
4300           switch (new_partial)
4301             {
4302             default:              strcat (new_opt, " -mfpr-?");      break;
4303             case EF_FRV_FPR_32:   strcat (new_opt, " -mfpr-32");     break;
4304             case EF_FRV_FPR_64:   strcat (new_opt, " -mfpr-64");     break;
4305             case EF_FRV_FPR_NONE: strcat (new_opt, " -msoft-float"); break;
4306             }
4307
4308           switch (old_partial)
4309             {
4310             default:              strcat (old_opt, " -mfpr-?");      break;
4311             case EF_FRV_FPR_32:   strcat (old_opt, " -mfpr-32");     break;
4312             case EF_FRV_FPR_64:   strcat (old_opt, " -mfpr-64");     break;
4313             case EF_FRV_FPR_NONE: strcat (old_opt, " -msoft-float"); break;
4314             }
4315         }
4316
4317       /* Warn if different dword support was used.  Note, 0 means nothing is
4318          said about the dword support.  */
4319       new_partial = (new_flags & EF_FRV_DWORD_MASK);
4320       old_partial = (old_flags & EF_FRV_DWORD_MASK);
4321       if (new_partial == old_partial)
4322         ;
4323
4324       else if (new_partial == 0)
4325         ;
4326
4327       else if (old_partial == 0)
4328         old_flags |= new_partial;
4329
4330       else
4331         {
4332           switch (new_partial)
4333             {
4334             default:               strcat (new_opt, " -mdword-?");  break;
4335             case EF_FRV_DWORD_YES: strcat (new_opt, " -mdword");    break;
4336             case EF_FRV_DWORD_NO:  strcat (new_opt, " -mno-dword"); break;
4337             }
4338
4339           switch (old_partial)
4340             {
4341             default:               strcat (old_opt, " -mdword-?");  break;
4342             case EF_FRV_DWORD_YES: strcat (old_opt, " -mdword");    break;
4343             case EF_FRV_DWORD_NO:  strcat (old_opt, " -mno-dword"); break;
4344             }
4345         }
4346
4347       /* Or in flags that accumulate (ie, if one module uses it, mark that the
4348          feature is used.  */
4349       old_flags |= new_flags & (EF_FRV_DOUBLE
4350                                 | EF_FRV_MEDIA
4351                                 | EF_FRV_MULADD
4352                                 | EF_FRV_NON_PIC_RELOCS);
4353
4354       /* If any module was compiled without -G0, clear the G0 bit.  */
4355       old_flags = ((old_flags & ~ EF_FRV_G0)
4356                    | (old_flags & new_flags & EF_FRV_G0));
4357
4358       /* If any module was compiled without -mnopack, clear the mnopack bit.  */
4359       old_flags = ((old_flags & ~ EF_FRV_NOPACK)
4360                    | (old_flags & new_flags & EF_FRV_NOPACK));
4361
4362       /* We don't have to do anything if the pic flags are the same, or the new
4363          module(s) were compiled with -mlibrary-pic.  */
4364       new_partial = (new_flags & EF_FRV_PIC_FLAGS);
4365       old_partial = (old_flags & EF_FRV_PIC_FLAGS);
4366       if ((new_partial == old_partial) || ((new_partial & EF_FRV_LIBPIC) != 0))
4367         ;
4368
4369       /* If the old module(s) were compiled with -mlibrary-pic, copy in the pic
4370          flags if any from the new module.  */
4371       else if ((old_partial & EF_FRV_LIBPIC) != 0)
4372         old_flags = (old_flags & ~ EF_FRV_PIC_FLAGS) | new_partial;
4373
4374       /* If we have mixtures of -fpic and -fPIC, or in both bits.  */
4375       else if (new_partial != 0 && old_partial != 0)
4376         old_flags |= new_partial;
4377
4378       /* One module was compiled for pic and the other was not, see if we have
4379          had any relocations that are not pic-safe.  */
4380       else
4381         {
4382           if ((old_flags & EF_FRV_NON_PIC_RELOCS) == 0)
4383             old_flags |= new_partial;
4384           else
4385             {
4386               old_flags &= ~ EF_FRV_PIC_FLAGS;
4387 #ifndef FRV_NO_PIC_ERROR
4388               error = TRUE;
4389               (*_bfd_error_handler)
4390                 (_("%s: compiled with %s and linked with modules that use non-pic relocations"),
4391                  bfd_get_filename (ibfd),
4392                  (new_flags & EF_FRV_BIGPIC) ? "-fPIC" : "-fpic");
4393 #endif
4394             }
4395         }
4396
4397       /* Warn if different cpu is used (allow a specific cpu to override
4398          the generic cpu).  */
4399       new_partial = (new_flags & EF_FRV_CPU_MASK);
4400       old_partial = (old_flags & EF_FRV_CPU_MASK);
4401       if (frv_elf_arch_extension_p (new_partial, old_partial))
4402         ;
4403
4404       else if (frv_elf_arch_extension_p (old_partial, new_partial))
4405         old_flags = (old_flags & ~EF_FRV_CPU_MASK) | new_partial;
4406
4407       else
4408         {
4409           switch (new_partial)
4410             {
4411             default:                 strcat (new_opt, " -mcpu=?");      break;
4412             case EF_FRV_CPU_GENERIC: strcat (new_opt, " -mcpu=frv");    break;
4413             case EF_FRV_CPU_SIMPLE:  strcat (new_opt, " -mcpu=simple"); break;
4414             case EF_FRV_CPU_FR550:   strcat (new_opt, " -mcpu=fr550");  break;
4415             case EF_FRV_CPU_FR500:   strcat (new_opt, " -mcpu=fr500");  break;
4416             case EF_FRV_CPU_FR450:   strcat (new_opt, " -mcpu=fr450");  break;
4417             case EF_FRV_CPU_FR405:   strcat (new_opt, " -mcpu=fr405");  break;
4418             case EF_FRV_CPU_FR400:   strcat (new_opt, " -mcpu=fr400");  break;
4419             case EF_FRV_CPU_FR300:   strcat (new_opt, " -mcpu=fr300");  break;
4420             case EF_FRV_CPU_TOMCAT:  strcat (new_opt, " -mcpu=tomcat"); break;
4421             }
4422
4423           switch (old_partial)
4424             {
4425             default:                 strcat (old_opt, " -mcpu=?");      break;
4426             case EF_FRV_CPU_GENERIC: strcat (old_opt, " -mcpu=frv");    break;
4427             case EF_FRV_CPU_SIMPLE:  strcat (old_opt, " -mcpu=simple"); break;
4428             case EF_FRV_CPU_FR550:   strcat (old_opt, " -mcpu=fr550");  break;
4429             case EF_FRV_CPU_FR500:   strcat (old_opt, " -mcpu=fr500");  break;
4430             case EF_FRV_CPU_FR450:   strcat (old_opt, " -mcpu=fr450");  break;
4431             case EF_FRV_CPU_FR405:   strcat (old_opt, " -mcpu=fr405");  break;
4432             case EF_FRV_CPU_FR400:   strcat (old_opt, " -mcpu=fr400");  break;
4433             case EF_FRV_CPU_FR300:   strcat (old_opt, " -mcpu=fr300");  break;
4434             case EF_FRV_CPU_TOMCAT:  strcat (old_opt, " -mcpu=tomcat"); break;
4435             }
4436         }
4437
4438       /* Print out any mismatches from above.  */
4439       if (new_opt[0])
4440         {
4441           error = TRUE;
4442           (*_bfd_error_handler)
4443             (_("%s: compiled with %s and linked with modules compiled with %s"),
4444              bfd_get_filename (ibfd), new_opt, old_opt);
4445         }
4446
4447       /* Warn about any other mismatches */
4448       new_partial = (new_flags & ~ EF_FRV_ALL_FLAGS);
4449       old_partial = (old_flags & ~ EF_FRV_ALL_FLAGS);
4450       if (new_partial != old_partial)
4451         {
4452           old_flags |= new_partial;
4453           error = TRUE;
4454           (*_bfd_error_handler)
4455             (_("%s: uses different unknown e_flags (0x%lx) fields than previous modules (0x%lx)"),
4456              bfd_get_filename (ibfd), (long)new_partial, (long)old_partial);
4457         }
4458     }
4459
4460   /* If the cpu is -mcpu=simple, then set the -mnopack bit.  */
4461   if ((old_flags & EF_FRV_CPU_MASK) == EF_FRV_CPU_SIMPLE)
4462     old_flags |= EF_FRV_NOPACK;
4463
4464   /* Update the old flags now with changes made above.  */
4465   old_partial = elf_elfheader (obfd)->e_flags & EF_FRV_CPU_MASK;
4466   elf_elfheader (obfd)->e_flags = old_flags;
4467   if (old_partial != (old_flags & EF_FRV_CPU_MASK))
4468     bfd_default_set_arch_mach (obfd, bfd_arch_frv, elf32_frv_machine (obfd));
4469
4470   if (error)
4471     bfd_set_error (bfd_error_bad_value);
4472
4473   return !error;
4474 }
4475
4476 \f
4477 bfd_boolean
4478 frv_elf_print_private_bfd_data (abfd, ptr)
4479      bfd *abfd;
4480      PTR ptr;
4481 {
4482   FILE *file = (FILE *) ptr;
4483   flagword flags;
4484
4485   BFD_ASSERT (abfd != NULL && ptr != NULL);
4486
4487   /* Print normal ELF private data.  */
4488   _bfd_elf_print_private_bfd_data (abfd, ptr);
4489
4490   flags = elf_elfheader (abfd)->e_flags;
4491   fprintf (file, _("private flags = 0x%lx:"), (long)flags);
4492
4493   switch (flags & EF_FRV_CPU_MASK)
4494     {
4495     default:                                                    break;
4496     case EF_FRV_CPU_SIMPLE: fprintf (file, " -mcpu=simple");    break;
4497     case EF_FRV_CPU_FR550:  fprintf (file, " -mcpu=fr550");     break;
4498     case EF_FRV_CPU_FR500:  fprintf (file, " -mcpu=fr500");     break;
4499     case EF_FRV_CPU_FR450:  fprintf (file, " -mcpu=fr450");     break;
4500     case EF_FRV_CPU_FR405:  fprintf (file, " -mcpu=fr405");     break;
4501     case EF_FRV_CPU_FR400:  fprintf (file, " -mcpu=fr400");     break;
4502     case EF_FRV_CPU_FR300:  fprintf (file, " -mcpu=fr300");     break;
4503     case EF_FRV_CPU_TOMCAT: fprintf (file, " -mcpu=tomcat");    break;
4504     }
4505
4506   switch (flags & EF_FRV_GPR_MASK)
4507     {
4508     default:                                                    break;
4509     case EF_FRV_GPR_32: fprintf (file, " -mgpr-32");            break;
4510     case EF_FRV_GPR_64: fprintf (file, " -mgpr-64");            break;
4511     }
4512
4513   switch (flags & EF_FRV_FPR_MASK)
4514     {
4515     default:                                                    break;
4516     case EF_FRV_FPR_32:   fprintf (file, " -mfpr-32");          break;
4517     case EF_FRV_FPR_64:   fprintf (file, " -mfpr-64");          break;
4518     case EF_FRV_FPR_NONE: fprintf (file, " -msoft-float");      break;
4519     }
4520
4521   switch (flags & EF_FRV_DWORD_MASK)
4522     {
4523     default:                                                    break;
4524     case EF_FRV_DWORD_YES: fprintf (file, " -mdword");          break;
4525     case EF_FRV_DWORD_NO:  fprintf (file, " -mno-dword");       break;
4526     }
4527
4528   if (flags & EF_FRV_DOUBLE)
4529     fprintf (file, " -mdouble");
4530
4531   if (flags & EF_FRV_MEDIA)
4532     fprintf (file, " -mmedia");
4533
4534   if (flags & EF_FRV_MULADD)
4535     fprintf (file, " -mmuladd");
4536
4537   if (flags & EF_FRV_PIC)
4538     fprintf (file, " -fpic");
4539
4540   if (flags & EF_FRV_BIGPIC)
4541     fprintf (file, " -fPIC");
4542
4543   if (flags & EF_FRV_LIBPIC)
4544     fprintf (file, " -mlibrary-pic");
4545
4546   if (flags & EF_FRV_FDPIC)
4547     fprintf (file, " -mfdpic");
4548   
4549   if (flags & EF_FRV_NON_PIC_RELOCS)
4550     fprintf (file, " non-pic relocations");
4551
4552   if (flags & EF_FRV_G0)
4553     fprintf (file, " -G0");
4554
4555   fputc ('\n', file);
4556   return TRUE;
4557 }
4558
4559 \f
4560 #define ELF_ARCH                bfd_arch_frv
4561 #define ELF_MACHINE_CODE        EM_CYGNUS_FRV
4562 #define ELF_MAXPAGESIZE         0x1000
4563
4564 #define TARGET_BIG_SYM          bfd_elf32_frv_vec
4565 #define TARGET_BIG_NAME         "elf32-frv"
4566
4567 #define elf_info_to_howto_rel                   frv_info_to_howto_rel
4568 #define elf_info_to_howto                       frv_info_to_howto_rela
4569 #define elf_backend_relocate_section            elf32_frv_relocate_section
4570 #define elf_backend_gc_mark_hook                elf32_frv_gc_mark_hook
4571 #define elf_backend_gc_sweep_hook               elf32_frv_gc_sweep_hook
4572 #define elf_backend_check_relocs                elf32_frv_check_relocs
4573 #define elf_backend_object_p                    elf32_frv_object_p
4574 #define elf_backend_add_symbol_hook             elf32_frv_add_symbol_hook
4575
4576 #define elf_backend_can_gc_sections             1
4577 #define elf_backend_rela_normal                 1
4578
4579 #define bfd_elf32_bfd_reloc_type_lookup         frv_reloc_type_lookup
4580 #define bfd_elf32_bfd_set_private_flags         frv_elf_set_private_flags
4581 #define bfd_elf32_bfd_copy_private_bfd_data     frv_elf_copy_private_bfd_data
4582 #define bfd_elf32_bfd_merge_private_bfd_data    frv_elf_merge_private_bfd_data
4583 #define bfd_elf32_bfd_print_private_bfd_data    frv_elf_print_private_bfd_data
4584
4585 #define bfd_elf32_bfd_link_hash_table_create  frv_elf_link_hash_table_create
4586 #define elf_backend_always_size_sections \
4587                 elf32_frv_always_size_sections
4588 #define elf_backend_modify_segment_map \
4589                 elf32_frv_modify_segment_map
4590
4591 #define elf_backend_create_dynamic_sections \
4592                 elf32_frv_create_dynamic_sections
4593 #define elf_backend_adjust_dynamic_symbol \
4594                 elf32_frv_adjust_dynamic_symbol
4595 #define elf_backend_size_dynamic_sections \
4596                 elf32_frv_size_dynamic_sections
4597 #define elf_backend_finish_dynamic_symbol \
4598                 elf32_frv_finish_dynamic_symbol
4599 #define elf_backend_finish_dynamic_sections \
4600                 elf32_frv_finish_dynamic_sections
4601
4602 #define elf_backend_want_got_sym        1
4603 #define elf_backend_got_header_size     0
4604 #define elf_backend_want_got_plt        0
4605 #define elf_backend_plt_readonly        1
4606 #define elf_backend_want_plt_sym        0
4607 #define elf_backend_plt_header_size     0
4608
4609 #define elf_backend_can_make_relative_eh_frame \
4610                 frv_elf_use_relative_eh_frame
4611 #define elf_backend_can_make_lsda_relative_eh_frame \
4612                 frv_elf_use_relative_eh_frame
4613 #define elf_backend_encode_eh_address   frv_elf_encode_eh_address
4614
4615 #define elf_backend_may_use_rel_p       1
4616 #define elf_backend_may_use_rela_p      1
4617 /* We use REL for dynamic relocations only.  */
4618 #define elf_backend_default_use_rela_p  1
4619
4620 #include "elf32-target.h"