* aout-arm.c, aout-target.h, aoutx.h, archive.c, armnetbsd.c,
[external/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 *, 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 64-bit 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   /* A 24-bit pc-relative relocation referencing the TLS PLT entry for
465      a thread-local symbol.  If the symbol number is 0, it refers to
466      the module.  */
467   HOWTO (R_FRV_GETTLSOFF,       /* type */
468          2,                     /* rightshift */
469          2,                     /* size (0 = byte, 1 = short, 2 = long) */
470          26,                    /* bitsize */
471          TRUE,                  /* pc_relative */
472          0,                     /* bitpos */
473          complain_overflow_bitfield, /* complain_on_overflow */
474          bfd_elf_generic_reloc, /* special_function */
475          "R_FRV_GETTLSOFF",     /* name */
476          FALSE,                 /* partial_inplace */
477          0x7e03ffff,            /* src_mask */
478          0x7e03ffff,            /* dst_mask */
479          TRUE),                 /* pcrel_offset */
480
481   /* A 64-bit TLS descriptor for a symbol.  This relocation is only
482      valid as a REL, dynamic relocation.  */
483   HOWTO (R_FRV_TLSDESC_VALUE,   /* type */
484          0,                     /* rightshift */
485          2,                     /* size (0 = byte, 1 = short, 2 = long) */
486          64,                    /* bitsize */
487          FALSE,                 /* pc_relative */
488          0,                     /* bitpos */
489          complain_overflow_bitfield, /* complain_on_overflow */
490          bfd_elf_generic_reloc, /* special_function */
491          "R_FRV_TLSDESC_VALUE", /* name */
492          FALSE,                 /* partial_inplace */
493          0xffffffff,            /* src_mask */
494          0xffffffff,            /* dst_mask */
495          FALSE),                /* pcrel_offset */
496
497   /* A 12-bit signed operand with the GOT offset for the TLS
498      descriptor of the symbol.  */
499   HOWTO (R_FRV_GOTTLSDESC12,    /* type */
500          0,                     /* rightshift */
501          2,                     /* size (0 = byte, 1 = short, 2 = long) */
502          12,                    /* bitsize */
503          FALSE,                 /* pc_relative */
504          0,                     /* bitpos */
505          complain_overflow_signed, /* complain_on_overflow */
506          bfd_elf_generic_reloc, /* special_function */
507          "R_FRV_GOTTLSDESC12",  /* name */
508          FALSE,                 /* partial_inplace */
509          0xfff,                 /* src_mask */
510          0xfff,                 /* dst_mask */
511          FALSE),                /* pcrel_offset */
512
513   /* The upper 16 bits of the GOT offset for the TLS descriptor of the
514      symbol.  */
515   HOWTO (R_FRV_GOTTLSDESCHI,    /* type */
516          0,                     /* rightshift */
517          2,                     /* size (0 = byte, 1 = short, 2 = long) */
518          16,                    /* bitsize */
519          FALSE,                 /* pc_relative */
520          0,                     /* bitpos */
521          complain_overflow_dont, /* complain_on_overflow */
522          bfd_elf_generic_reloc, /* special_function */
523          "R_FRV_GOTTLSDESCHI",  /* name */
524          FALSE,                 /* partial_inplace */
525          0xffff,                /* src_mask */
526          0xffff,                /* dst_mask */
527          FALSE),                /* pcrel_offset */
528
529   /* The lower 16 bits of the GOT offset for the TLS descriptor of the
530      symbol.  */
531   HOWTO (R_FRV_GOTTLSDESCLO,    /* type */
532          0,                     /* rightshift */
533          2,                     /* size (0 = byte, 1 = short, 2 = long) */
534          16,                    /* bitsize */
535          FALSE,                 /* pc_relative */
536          0,                     /* bitpos */
537          complain_overflow_dont, /* complain_on_overflow */
538          bfd_elf_generic_reloc, /* special_function */
539          "R_FRV_GOTTLSDESCLO",  /* name */
540          FALSE,                 /* partial_inplace */
541          0xffff,                /* src_mask */
542          0xffff,                /* dst_mask */
543          FALSE),                /* pcrel_offset */
544
545   /* A 12-bit signed operand with the offset from the module base
546      address to the thread-local symbol address.  */
547   HOWTO (R_FRV_TLSMOFF12,        /* type */
548          0,                     /* rightshift */
549          2,                     /* size (0 = byte, 1 = short, 2 = long) */
550          12,                    /* bitsize */
551          FALSE,                 /* pc_relative */
552          0,                     /* bitpos */
553          complain_overflow_signed, /* complain_on_overflow */
554          bfd_elf_generic_reloc, /* special_function */
555          "R_FRV_TLSMOFF12",     /* name */
556          FALSE,                 /* partial_inplace */
557          0xfff,                 /* src_mask */
558          0xfff,                 /* dst_mask */
559          FALSE),                /* pcrel_offset */
560
561   /* The upper 16 bits of the offset from the module base address to
562      the thread-local symbol address.  */
563   HOWTO (R_FRV_TLSMOFFHI,        /* type */
564          0,                     /* rightshift */
565          2,                     /* size (0 = byte, 1 = short, 2 = long) */
566          16,                    /* bitsize */
567          FALSE,                 /* pc_relative */
568          0,                     /* bitpos */
569          complain_overflow_dont, /* complain_on_overflow */
570          bfd_elf_generic_reloc, /* special_function */
571          "R_FRV_TLSMOFFHI",     /* name */
572          FALSE,                 /* partial_inplace */
573          0xffff,                /* src_mask */
574          0xffff,                /* dst_mask */
575          FALSE),                /* pcrel_offset */
576
577   /* The lower 16 bits of the offset from the module base address to
578      the thread-local symbol address.  */
579   HOWTO (R_FRV_TLSMOFFLO,       /* type */
580          0,                     /* rightshift */
581          2,                     /* size (0 = byte, 1 = short, 2 = long) */
582          16,                    /* bitsize */
583          FALSE,                 /* pc_relative */
584          0,                     /* bitpos */
585          complain_overflow_dont, /* complain_on_overflow */
586          bfd_elf_generic_reloc, /* special_function */
587          "R_FRV_TLSMOFFLO",     /* name */
588          FALSE,                 /* partial_inplace */
589          0xffff,                /* src_mask */
590          0xffff,                /* dst_mask */
591          FALSE),                /* pcrel_offset */
592
593   /* A 12-bit signed operand with the GOT offset for the TLSOFF entry
594      for a symbol.  */
595   HOWTO (R_FRV_GOTTLSOFF12,        /* type */
596          0,                     /* rightshift */
597          2,                     /* size (0 = byte, 1 = short, 2 = long) */
598          12,                    /* bitsize */
599          FALSE,                 /* pc_relative */
600          0,                     /* bitpos */
601          complain_overflow_signed, /* complain_on_overflow */
602          bfd_elf_generic_reloc, /* special_function */
603          "R_FRV_GOTTLSOFF12",   /* name */
604          FALSE,                 /* partial_inplace */
605          0xfff,                 /* src_mask */
606          0xfff,                 /* dst_mask */
607          FALSE),                /* pcrel_offset */
608
609   /* The upper 16 bits of the GOT offset for the TLSOFF entry for a
610      symbol.  */
611   HOWTO (R_FRV_GOTTLSOFFHI,        /* type */
612          0,                     /* rightshift */
613          2,                     /* size (0 = byte, 1 = short, 2 = long) */
614          16,                    /* bitsize */
615          FALSE,                 /* pc_relative */
616          0,                     /* bitpos */
617          complain_overflow_dont, /* complain_on_overflow */
618          bfd_elf_generic_reloc, /* special_function */
619          "R_FRV_GOTTLSOFFHI",   /* name */
620          FALSE,                 /* partial_inplace */
621          0xffff,                /* src_mask */
622          0xffff,                /* dst_mask */
623          FALSE),                /* pcrel_offset */
624
625   /* The lower 16 bits of the GOT offset for the TLSOFF entry for a
626      symbol.  */
627   HOWTO (R_FRV_GOTTLSOFFLO,     /* type */
628          0,                     /* rightshift */
629          2,                     /* size (0 = byte, 1 = short, 2 = long) */
630          16,                    /* bitsize */
631          FALSE,                 /* pc_relative */
632          0,                     /* bitpos */
633          complain_overflow_dont, /* complain_on_overflow */
634          bfd_elf_generic_reloc, /* special_function */
635          "R_FRV_GOTTLSOFFLO",   /* name */
636          FALSE,                 /* partial_inplace */
637          0xffff,                /* src_mask */
638          0xffff,                /* dst_mask */
639          FALSE),                /* pcrel_offset */
640
641   /* The 32-bit offset from the thread pointer (not the module base
642      address) to a thread-local symbol.  */
643   HOWTO (R_FRV_TLSOFF,          /* type */
644          0,                     /* rightshift */
645          2,                     /* size (0 = byte, 1 = short, 2 = long) */
646          32,                    /* bitsize */
647          FALSE,                 /* pc_relative */
648          0,                     /* bitpos */
649          complain_overflow_dont, /* complain_on_overflow */
650          bfd_elf_generic_reloc, /* special_function */
651          "R_FRV_TLSOFF",        /* name */
652          FALSE,                 /* partial_inplace */
653          0xffffffff,            /* src_mask */
654          0xffffffff,            /* dst_mask */
655          FALSE),                /* pcrel_offset */
656
657   /* An annotation for linker relaxation, that denotes the
658      symbol+addend whose TLS descriptor is referenced by the sum of
659      the two input registers of an ldd instruction.  */
660   HOWTO (R_FRV_TLSDESC_RELAX,   /* type */
661          0,                     /* rightshift */
662          2,                     /* size (0 = byte, 1 = short, 2 = long) */
663          0,                     /* bitsize */
664          FALSE,                 /* pc_relative */
665          0,                     /* bitpos */
666          complain_overflow_dont, /* complain_on_overflow */
667          bfd_elf_generic_reloc, /* special_function */
668          "R_FRV_TLSDESC_RELAX", /* name */
669          FALSE,                 /* partial_inplace */
670          0,                     /* src_mask */
671          0,                     /* dst_mask */
672          FALSE),                /* pcrel_offset */
673
674   /* An annotation for linker relaxation, that denotes the
675      symbol+addend whose TLS resolver entry point is given by the sum
676      of the two register operands of an calll instruction.  */
677   HOWTO (R_FRV_GETTLSOFF_RELAX, /* type */
678          0,                     /* rightshift */
679          2,                     /* size (0 = byte, 1 = short, 2 = long) */
680          0,                     /* bitsize */
681          FALSE,                 /* pc_relative */
682          0,                     /* bitpos */
683          complain_overflow_dont, /* complain_on_overflow */
684          bfd_elf_generic_reloc, /* special_function */
685          "R_FRV_GETTLSOFF_RELAX", /* name */
686          FALSE,                 /* partial_inplace */
687          0,                     /* src_mask */
688          0,                     /* dst_mask */
689          FALSE),                /* pcrel_offset */
690
691   /* An annotation for linker relaxation, that denotes the
692      symbol+addend whose TLS offset GOT entry is given by the sum of
693      the two input registers of an ld instruction.  */
694   HOWTO (R_FRV_TLSOFF_RELAX,    /* type */
695          0,                     /* rightshift */
696          2,                     /* size (0 = byte, 1 = short, 2 = long) */
697          0,                     /* bitsize */
698          FALSE,                 /* pc_relative */
699          0,                     /* bitpos */
700          complain_overflow_bitfield, /* complain_on_overflow */
701          bfd_elf_generic_reloc, /* special_function */
702          "R_FRV_TLSOFF_RELAX",  /* name */
703          FALSE,                 /* partial_inplace */
704          0,                     /* src_mask */
705          0,                     /* dst_mask */
706          FALSE),                /* pcrel_offset */
707
708   /* A 32-bit offset from the module base address to
709      the thread-local symbol address.  */
710   HOWTO (R_FRV_TLSMOFF, /* type */
711          0,                     /* rightshift */
712          2,                     /* size (0 = byte, 1 = short, 2 = long) */
713          32,                    /* bitsize */
714          FALSE,                 /* pc_relative */
715          0,                     /* bitpos */
716          complain_overflow_dont, /* complain_on_overflow */
717          bfd_elf_generic_reloc, /* special_function */
718          "R_FRV_TLSMOFF",       /* name */
719          FALSE,                 /* partial_inplace */
720          0xffffffff,            /* src_mask */
721          0xffffffff,            /* dst_mask */
722          FALSE),                /* pcrel_offset */
723 };
724
725 /* GNU extension to record C++ vtable hierarchy.  */
726 static reloc_howto_type elf32_frv_vtinherit_howto =
727   HOWTO (R_FRV_GNU_VTINHERIT,   /* type */
728          0,                     /* rightshift */
729          2,                     /* size (0 = byte, 1 = short, 2 = long) */
730          0,                     /* bitsize */
731          FALSE,                 /* pc_relative */
732          0,                     /* bitpos */
733          complain_overflow_dont, /* complain_on_overflow */
734          NULL,                  /* special_function */
735          "R_FRV_GNU_VTINHERIT", /* name */
736          FALSE,                 /* partial_inplace */
737          0,                     /* src_mask */
738          0,                     /* dst_mask */
739          FALSE);                /* pcrel_offset */
740
741   /* GNU extension to record C++ vtable member usage.  */
742 static reloc_howto_type elf32_frv_vtentry_howto =
743   HOWTO (R_FRV_GNU_VTENTRY,     /* type */
744          0,                     /* rightshift */
745          2,                     /* size (0 = byte, 1 = short, 2 = long) */
746          0,                     /* bitsize */
747          FALSE,                 /* pc_relative */
748          0,                     /* bitpos */
749          complain_overflow_dont, /* complain_on_overflow */
750          _bfd_elf_rel_vtable_reloc_fn,  /* special_function */
751          "R_FRV_GNU_VTENTRY",   /* name */
752          FALSE,                 /* partial_inplace */
753          0,                     /* src_mask */
754          0,                     /* dst_mask */
755          FALSE);                /* pcrel_offset */
756
757 /* The following 3 relocations are REL.  The only difference to the
758    entries in the table above are that partial_inplace is TRUE.  */
759 static reloc_howto_type elf32_frv_rel_32_howto =
760   HOWTO (R_FRV_32,              /* type */
761          0,                     /* rightshift */
762          2,                     /* size (0 = byte, 1 = short, 2 = long) */
763          32,                    /* bitsize */
764          FALSE,                 /* pc_relative */
765          0,                     /* bitpos */
766          complain_overflow_bitfield, /* complain_on_overflow */
767          bfd_elf_generic_reloc, /* special_function */
768          "R_FRV_32",            /* name */
769          TRUE,                  /* partial_inplace */
770          0xffffffff,            /* src_mask */
771          0xffffffff,            /* dst_mask */
772          FALSE);                /* pcrel_offset */
773
774 static reloc_howto_type elf32_frv_rel_funcdesc_howto =
775   HOWTO (R_FRV_FUNCDESC,        /* type */
776          0,                     /* rightshift */
777          2,                     /* size (0 = byte, 1 = short, 2 = long) */
778          32,                    /* bitsize */
779          FALSE,                 /* pc_relative */
780          0,                     /* bitpos */
781          complain_overflow_bitfield, /* complain_on_overflow */
782          bfd_elf_generic_reloc, /* special_function */
783          "R_FRV_FUNCDESC",      /* name */
784          TRUE,                  /* partial_inplace */
785          0xffffffff,            /* src_mask */
786          0xffffffff,            /* dst_mask */
787          FALSE);                /* pcrel_offset */
788
789 static reloc_howto_type elf32_frv_rel_funcdesc_value_howto =
790   HOWTO (R_FRV_FUNCDESC_VALUE,  /* type */
791          0,                     /* rightshift */
792          2,                     /* size (0 = byte, 1 = short, 2 = long) */
793          64,                    /* bitsize */
794          FALSE,                 /* pc_relative */
795          0,                     /* bitpos */
796          complain_overflow_bitfield, /* complain_on_overflow */
797          bfd_elf_generic_reloc, /* special_function */
798          "R_FRV_FUNCDESC_VALUE", /* name */
799          TRUE,                  /* partial_inplace */
800          0xffffffff,            /* src_mask */
801          0xffffffff,            /* dst_mask */
802          FALSE);                /* pcrel_offset */
803
804 static reloc_howto_type elf32_frv_rel_tlsdesc_value_howto =
805   /* A 64-bit TLS descriptor for a symbol.  The first word resolves to
806      an entry point, and the second resolves to a special argument.
807      If the symbol turns out to be in static TLS, the entry point is a
808      return instruction, and the special argument is the TLS offset
809      for the symbol.  If it's in dynamic TLS, the entry point is a TLS
810      offset resolver, and the special argument is a pointer to a data
811      structure allocated by the dynamic loader, containing the GOT
812      address for the offset resolver, the module id, the offset within
813      the module, and anything else the TLS offset resolver might need
814      to determine the TLS offset for the symbol in the running
815      thread.  */
816   HOWTO (R_FRV_TLSDESC_VALUE,   /* type */
817          0,                     /* rightshift */
818          2,                     /* size (0 = byte, 1 = short, 2 = long) */
819          64,                    /* bitsize */
820          FALSE,                 /* pc_relative */
821          0,                     /* bitpos */
822          complain_overflow_bitfield, /* complain_on_overflow */
823          bfd_elf_generic_reloc, /* special_function */
824          "R_FRV_TLSDESC_VALUE", /* name */
825          TRUE,                  /* partial_inplace */
826          0xffffffff,            /* src_mask */
827          0xffffffff,            /* dst_mask */
828          FALSE);                /* pcrel_offset */
829
830 static reloc_howto_type elf32_frv_rel_tlsoff_howto =
831   /* The 32-bit offset from the thread pointer (not the module base
832      address) to a thread-local symbol.  */
833   HOWTO (R_FRV_TLSOFF,          /* type */
834          0,                     /* rightshift */
835          2,                     /* size (0 = byte, 1 = short, 2 = long) */
836          32,                    /* bitsize */
837          FALSE,                 /* pc_relative */
838          0,                     /* bitpos */
839          complain_overflow_bitfield, /* complain_on_overflow */
840          bfd_elf_generic_reloc, /* special_function */
841          "R_FRV_TLSOFF",        /* name */
842          TRUE,                  /* partial_inplace */
843          0xffffffff,            /* src_mask */
844          0xffffffff,            /* dst_mask */
845          FALSE);                /* pcrel_offset */
846
847
848 \f
849 extern const bfd_target bfd_elf32_frvfdpic_vec;
850 #define IS_FDPIC(bfd) ((bfd)->xvec == &bfd_elf32_frvfdpic_vec)
851
852 /* An extension of the elf hash table data structure, containing some
853    additional FRV-specific data.  */
854 struct frvfdpic_elf_link_hash_table
855 {
856   struct elf_link_hash_table elf;
857
858   /* A pointer to the .got section.  */
859   asection *sgot;
860   /* A pointer to the .rel.got section.  */
861   asection *sgotrel;
862   /* A pointer to the .rofixup section.  */
863   asection *sgotfixup;
864   /* A pointer to the .plt section.  */
865   asection *splt;
866   /* A pointer to the .rel.plt section.  */
867   asection *spltrel;
868   /* GOT base offset.  */
869   bfd_vma got0;
870   /* Location of the first non-lazy PLT entry, i.e., the number of
871      bytes taken by lazy PLT entries.  If locally-bound TLS
872      descriptors require a ret instruction, it will be placed at this
873      offset.  */
874   bfd_vma plt0;
875   /* A hash table holding information about which symbols were
876      referenced with which PIC-related relocations.  */
877   struct htab *relocs_info;
878   /* Summary reloc information collected by
879      _frvfdpic_count_got_plt_entries.  */
880   struct _frvfdpic_dynamic_got_info *g;
881 };
882
883 /* Get the FRV ELF linker hash table from a link_info structure.  */
884
885 #define frvfdpic_hash_table(info) \
886   ((struct frvfdpic_elf_link_hash_table *) ((info)->hash))
887
888 #define frvfdpic_got_section(info) \
889   (frvfdpic_hash_table (info)->sgot)
890 #define frvfdpic_gotrel_section(info) \
891   (frvfdpic_hash_table (info)->sgotrel)
892 #define frvfdpic_gotfixup_section(info) \
893   (frvfdpic_hash_table (info)->sgotfixup)
894 #define frvfdpic_plt_section(info) \
895   (frvfdpic_hash_table (info)->splt)
896 #define frvfdpic_pltrel_section(info) \
897   (frvfdpic_hash_table (info)->spltrel)
898 #define frvfdpic_relocs_info(info) \
899   (frvfdpic_hash_table (info)->relocs_info)
900 #define frvfdpic_got_initial_offset(info) \
901   (frvfdpic_hash_table (info)->got0)
902 #define frvfdpic_plt_initial_offset(info) \
903   (frvfdpic_hash_table (info)->plt0)
904 #define frvfdpic_dynamic_got_plt_info(info) \
905   (frvfdpic_hash_table (info)->g)
906
907 /* Currently it's the same, but if some day we have a reason to change
908    it, we'd better be using a different macro.
909
910    FIXME: if there's any TLS PLT entry that uses local-exec or
911    initial-exec models, we could use the ret at the end of any of them
912    instead of adding one more.  */
913 #define frvfdpic_plt_tls_ret_offset(info) \
914   (frvfdpic_plt_initial_offset (info))
915
916 /* The name of the dynamic interpreter.  This is put in the .interp
917    section.  */
918
919 #define ELF_DYNAMIC_INTERPRETER "/lib/ld.so.1"
920
921 #define DEFAULT_STACK_SIZE 0x20000
922
923 /* This structure is used to collect the number of entries present in
924    each addressable range of the got.  */
925 struct _frvfdpic_dynamic_got_info
926 {
927   /* Several bits of information about the current link.  */
928   struct bfd_link_info *info;
929   /* Total GOT size needed for GOT entries within the 12-, 16- or 32-bit
930      ranges.  */
931   bfd_vma got12, gotlos, gothilo;
932   /* Total GOT size needed for function descriptor entries within the 12-,
933      16- or 32-bit ranges.  */
934   bfd_vma fd12, fdlos, fdhilo;
935   /* Total GOT size needed by function descriptor entries referenced
936      in PLT entries, that would be profitable to place in offsets
937      close to the PIC register.  */
938   bfd_vma fdplt;
939   /* Total PLT size needed by lazy PLT entries.  */
940   bfd_vma lzplt;
941   /* Total GOT size needed for TLS descriptor entries within the 12-,
942      16- or 32-bit ranges.  */
943   bfd_vma tlsd12, tlsdlos, tlsdhilo;
944   /* Total GOT size needed by TLS descriptors referenced in PLT
945      entries, that would be profitable to place in offers close to the
946      PIC register.  */
947   bfd_vma tlsdplt;
948   /* Total PLT size needed by TLS lazy PLT entries.  */
949   bfd_vma tlslzplt;
950   /* Number of relocations carried over from input object files.  */
951   unsigned long relocs;
952   /* Number of fixups introduced by relocations in input object files.  */
953   unsigned long fixups;
954   /* The number of fixups that reference the ret instruction added to
955      the PLT for locally-resolved TLS descriptors.  */
956   unsigned long tls_ret_refs;
957 };
958
959 /* This structure is used to assign offsets to got entries, function
960    descriptors, plt entries and lazy plt entries.  */
961
962 struct _frvfdpic_dynamic_got_plt_info
963 {
964   /* Summary information collected with _frvfdpic_count_got_plt_entries.  */
965   struct _frvfdpic_dynamic_got_info g;
966
967   /* For each addressable range, we record a MAX (positive) and MIN
968      (negative) value.  CUR is used to assign got entries, and it's
969      incremented from an initial positive value to MAX, then from MIN
970      to FDCUR (unless FDCUR wraps around first).  FDCUR is used to
971      assign function descriptors, and it's decreased from an initial
972      non-positive value to MIN, then from MAX down to CUR (unless CUR
973      wraps around first).  All of MIN, MAX, CUR and FDCUR always point
974      to even words.  ODD, if non-zero, indicates an odd word to be
975      used for the next got entry, otherwise CUR is used and
976      incremented by a pair of words, wrapping around when it reaches
977      MAX.  FDCUR is decremented (and wrapped) before the next function
978      descriptor is chosen.  FDPLT indicates the number of remaining
979      slots that can be used for function descriptors used only by PLT
980      entries.
981
982      TMAX, TMIN and TCUR are used to assign TLS descriptors.  TCUR
983      starts as MAX, and grows up to TMAX, then wraps around to TMIN
984      and grows up to MIN.  TLSDPLT indicates the number of remaining
985      slots that can be used for TLS descriptors used only by TLS PLT
986      entries.  */
987   struct _frvfdpic_dynamic_got_alloc_data
988   {
989     bfd_signed_vma max, cur, odd, fdcur, min;
990     bfd_signed_vma tmax, tcur, tmin;
991     bfd_vma fdplt, tlsdplt;
992   } got12, gotlos, gothilo;
993 };
994
995 /* Create an FRV ELF linker hash table.  */
996
997 static struct bfd_link_hash_table *
998 frvfdpic_elf_link_hash_table_create (bfd *abfd)
999 {
1000   struct frvfdpic_elf_link_hash_table *ret;
1001   bfd_size_type amt = sizeof (struct frvfdpic_elf_link_hash_table);
1002
1003   ret = bfd_zalloc (abfd, amt);
1004   if (ret == NULL)
1005     return NULL;
1006
1007   if (! _bfd_elf_link_hash_table_init (&ret->elf, abfd,
1008                                        _bfd_elf_link_hash_newfunc))
1009     {
1010       free (ret);
1011       return NULL;
1012     }
1013
1014   return &ret->elf.root;
1015 }
1016
1017 /* Decide whether a reference to a symbol can be resolved locally or
1018    not.  If the symbol is protected, we want the local address, but
1019    its function descriptor must be assigned by the dynamic linker.  */
1020 #define FRVFDPIC_SYM_LOCAL(INFO, H) \
1021   (_bfd_elf_symbol_refs_local_p ((H), (INFO), 1) \
1022    || ! elf_hash_table (INFO)->dynamic_sections_created \
1023    /* These two additional alternatives are needed for TLS
1024       relocations, that use *ABS*+offset relocations to refer to the
1025       TLS section of the local module.  FIXME: At some point during
1026       linking, the definition section ceases to be regarded as the
1027       absolute section, and starts being regarded as the undefined
1028       section.  */ \
1029    || bfd_is_abs_section ((H)->root.u.def.section) \
1030    || bfd_is_und_section ((H)->root.u.def.section))
1031 #define FRVFDPIC_FUNCDESC_LOCAL(INFO, H) \
1032   ((H)->dynindx == -1 || ! elf_hash_table (INFO)->dynamic_sections_created)
1033
1034 /* This structure collects information on what kind of GOT, PLT or
1035    function descriptors are required by relocations that reference a
1036    certain symbol.  */
1037 struct frvfdpic_relocs_info
1038 {
1039   /* The index of the symbol, as stored in the relocation r_info, if
1040      we have a local symbol; -1 otherwise.  */
1041   long symndx;
1042   union
1043   {
1044     /* The input bfd in which the symbol is defined, if it's a local
1045        symbol.  */
1046     bfd *abfd;
1047     /* If symndx == -1, the hash table entry corresponding to a global
1048        symbol (even if it turns out to bind locally, in which case it
1049        should ideally be replaced with section's symndx + addend).  */
1050     struct elf_link_hash_entry *h;
1051   } d;
1052   /* The addend of the relocation that references the symbol.  */
1053   bfd_vma addend;
1054
1055   /* The fields above are used to identify an entry.  The fields below
1056      contain information on how an entry is used and, later on, which
1057      locations it was assigned.  */
1058   /* The following 3 fields record whether the symbol+addend above was
1059      ever referenced with a GOT relocation.  The 12 suffix indicates a
1060      GOT12 relocation; los is used for GOTLO relocations that are not
1061      matched by a GOTHI relocation; hilo is used for GOTLO/GOTHI
1062      pairs.  */
1063   unsigned got12:1;
1064   unsigned gotlos:1;
1065   unsigned gothilo:1;
1066   /* Whether a FUNCDESC relocation references symbol+addend.  */
1067   unsigned fd:1;
1068   /* Whether a FUNCDESC_GOT relocation references symbol+addend.  */
1069   unsigned fdgot12:1;
1070   unsigned fdgotlos:1;
1071   unsigned fdgothilo:1;
1072   /* Whether a FUNCDESC_GOTOFF relocation references symbol+addend.  */
1073   unsigned fdgoff12:1;
1074   unsigned fdgofflos:1;
1075   unsigned fdgoffhilo:1;
1076   /* Whether a GETTLSOFF relocation references symbol+addend.  */
1077   unsigned tlsplt:1;
1078   /* FIXME: we should probably add tlspltdesc, tlspltoff and
1079      tlspltimm, to tell what kind of TLS PLT entry we're generating.
1080      We might instead just pre-compute flags telling whether the
1081      object is suitable for local exec, initial exec or general
1082      dynamic addressing, and use that all over the place.  We could
1083      also try to do a better job of merging TLSOFF and TLSDESC entries
1084      in main executables, but perhaps we can get rid of TLSDESC
1085      entirely in them instead.  */
1086   /* Whether a GOTTLSDESC relocation references symbol+addend.  */
1087   unsigned tlsdesc12:1;
1088   unsigned tlsdesclos:1;
1089   unsigned tlsdeschilo:1;
1090   /* Whether a GOTTLSOFF relocation references symbol+addend.  */
1091   unsigned tlsoff12:1;
1092   unsigned tlsofflos:1;
1093   unsigned tlsoffhilo:1;
1094   /* Whether symbol+addend is referenced with GOTOFF12, GOTOFFLO or
1095      GOTOFFHI relocations.  The addend doesn't really matter, since we
1096      envision that this will only be used to check whether the symbol
1097      is mapped to the same segment as the got.  */
1098   unsigned gotoff:1;
1099   /* Whether symbol+addend is referenced by a LABEL24 relocation.  */
1100   unsigned call:1;
1101   /* Whether symbol+addend is referenced by a 32 or FUNCDESC_VALUE
1102      relocation.  */
1103   unsigned sym:1;
1104   /* Whether we need a PLT entry for a symbol.  Should be implied by
1105      something like:
1106      (call && symndx == -1 && ! FRVFDPIC_SYM_LOCAL (info, d.h))  */
1107   unsigned plt:1;
1108   /* Whether a function descriptor should be created in this link unit
1109      for symbol+addend.  Should be implied by something like:
1110      (plt || fdgotoff12 || fdgotofflos || fdgotofflohi
1111       || ((fd || fdgot12 || fdgotlos || fdgothilo)
1112           && (symndx != -1 || FRVFDPIC_FUNCDESC_LOCAL (info, d.h))))  */
1113   unsigned privfd:1;
1114   /* Whether a lazy PLT entry is needed for this symbol+addend.
1115      Should be implied by something like:
1116      (privfd && symndx == -1 && ! FRVFDPIC_SYM_LOCAL (info, d.h)
1117       && ! (info->flags & DF_BIND_NOW))  */
1118   unsigned lazyplt:1;
1119   /* Whether we've already emitted GOT relocations and PLT entries as
1120      needed for this symbol.  */
1121   unsigned done:1;
1122
1123   /* The number of R_FRV_32, R_FRV_FUNCDESC, R_FRV_FUNCDESC_VALUE and
1124      R_FRV_TLSDESC_VALUE, R_FRV_TLSOFF relocations referencing
1125      symbol+addend.  */
1126   unsigned relocs32, relocsfd, relocsfdv, relocstlsd, relocstlsoff;
1127
1128   /* The number of .rofixups entries and dynamic relocations allocated
1129      for this symbol, minus any that might have already been used.  */
1130   unsigned fixups, dynrelocs;
1131
1132   /* The offsets of the GOT entries assigned to symbol+addend, to the
1133      function descriptor's address, and to a function descriptor,
1134      respectively.  Should be zero if unassigned.  The offsets are
1135      counted from the value that will be assigned to the PIC register,
1136      not from the beginning of the .got section.  */
1137   bfd_signed_vma got_entry, fdgot_entry, fd_entry;
1138   /* The offsets of the PLT entries assigned to symbol+addend,
1139      non-lazy and lazy, respectively.  If unassigned, should be
1140      (bfd_vma)-1.  */
1141   bfd_vma plt_entry, lzplt_entry;
1142   /* The offsets of the GOT entries for TLS offset and TLS descriptor.  */
1143   bfd_signed_vma tlsoff_entry, tlsdesc_entry;
1144   /* The offset of the TLS offset PLT entry.  */
1145   bfd_vma tlsplt_entry;
1146 };
1147
1148 /* Compute a hash with the key fields of an frvfdpic_relocs_info entry.  */
1149 static hashval_t
1150 frvfdpic_relocs_info_hash (const void *entry_)
1151 {
1152   const struct frvfdpic_relocs_info *entry = entry_;
1153
1154   return (entry->symndx == -1
1155           ? (long)entry->d.h->root.root.hash
1156           : entry->symndx + entry->d.abfd->id * 257) + entry->addend;
1157 }
1158
1159 /* Test whether the key fields of two frvfdpic_relocs_info entries are
1160    identical.  */
1161 static int
1162 frvfdpic_relocs_info_eq (const void *entry1, const void *entry2)
1163 {
1164   const struct frvfdpic_relocs_info *e1 = entry1;
1165   const struct frvfdpic_relocs_info *e2 = entry2;
1166
1167   return e1->symndx == e2->symndx && e1->addend == e2->addend
1168     && (e1->symndx == -1 ? e1->d.h == e2->d.h : e1->d.abfd == e2->d.abfd);
1169 }
1170
1171 /* Find or create an entry in a hash table HT that matches the key
1172    fields of the given ENTRY.  If it's not found, memory for a new
1173    entry is allocated in ABFD's obstack.  */
1174 static struct frvfdpic_relocs_info *
1175 frvfdpic_relocs_info_find (struct htab *ht,
1176                            bfd *abfd,
1177                            const struct frvfdpic_relocs_info *entry,
1178                            enum insert_option insert)
1179 {
1180   struct frvfdpic_relocs_info **loc =
1181     (struct frvfdpic_relocs_info **) htab_find_slot (ht, entry, insert);
1182
1183   if (! loc)
1184     return NULL;
1185
1186   if (*loc)
1187     return *loc;
1188
1189   *loc = bfd_zalloc (abfd, sizeof (**loc));
1190
1191   if (! *loc)
1192     return *loc;
1193
1194   (*loc)->symndx = entry->symndx;
1195   (*loc)->d = entry->d;
1196   (*loc)->addend = entry->addend;
1197   (*loc)->plt_entry = (bfd_vma)-1;
1198   (*loc)->lzplt_entry = (bfd_vma)-1;
1199   (*loc)->tlsplt_entry = (bfd_vma)-1;
1200
1201   return *loc;
1202 }
1203
1204 /* Obtain the address of the entry in HT associated with H's symbol +
1205    addend, creating a new entry if none existed.  ABFD is only used
1206    for memory allocation purposes.  */
1207 inline static struct frvfdpic_relocs_info *
1208 frvfdpic_relocs_info_for_global (struct htab *ht,
1209                                  bfd *abfd,
1210                                  struct elf_link_hash_entry *h,
1211                                  bfd_vma addend,
1212                                  enum insert_option insert)
1213 {
1214   struct frvfdpic_relocs_info entry;
1215
1216   entry.symndx = -1;
1217   entry.d.h = h;
1218   entry.addend = addend;
1219
1220   return frvfdpic_relocs_info_find (ht, abfd, &entry, insert);
1221 }
1222
1223 /* Obtain the address of the entry in HT associated with the SYMNDXth
1224    local symbol of the input bfd ABFD, plus the addend, creating a new
1225    entry if none existed.  */
1226 inline static struct frvfdpic_relocs_info *
1227 frvfdpic_relocs_info_for_local (struct htab *ht,
1228                                 bfd *abfd,
1229                                 long symndx,
1230                                 bfd_vma addend,
1231                                 enum insert_option insert)
1232 {
1233   struct frvfdpic_relocs_info entry;
1234
1235   entry.symndx = symndx;
1236   entry.d.abfd = abfd;
1237   entry.addend = addend;
1238
1239   return frvfdpic_relocs_info_find (ht, abfd, &entry, insert);
1240 }
1241
1242 /* Merge fields set by check_relocs() of two entries that end up being
1243    mapped to the same (presumably global) symbol.  */
1244
1245 inline static void
1246 frvfdpic_pic_merge_early_relocs_info (struct frvfdpic_relocs_info *e2,
1247                                       struct frvfdpic_relocs_info const *e1)
1248 {
1249   e2->got12 |= e1->got12;
1250   e2->gotlos |= e1->gotlos;
1251   e2->gothilo |= e1->gothilo;
1252   e2->fd |= e1->fd;
1253   e2->fdgot12 |= e1->fdgot12;
1254   e2->fdgotlos |= e1->fdgotlos;
1255   e2->fdgothilo |= e1->fdgothilo;
1256   e2->fdgoff12 |= e1->fdgoff12;
1257   e2->fdgofflos |= e1->fdgofflos;
1258   e2->fdgoffhilo |= e1->fdgoffhilo;
1259   e2->tlsplt |= e1->tlsplt;
1260   e2->tlsdesc12 |= e1->tlsdesc12;
1261   e2->tlsdesclos |= e1->tlsdesclos;
1262   e2->tlsdeschilo |= e1->tlsdeschilo;
1263   e2->tlsoff12 |= e1->tlsoff12;
1264   e2->tlsofflos |= e1->tlsofflos;
1265   e2->tlsoffhilo |= e1->tlsoffhilo;
1266   e2->gotoff |= e1->gotoff;
1267   e2->call |= e1->call;
1268   e2->sym |= e1->sym;
1269 }
1270
1271 /* Every block of 65535 lazy PLT entries shares a single call to the
1272    resolver, inserted in the 32768th lazy PLT entry (i.e., entry #
1273    32767, counting from 0).  All other lazy PLT entries branch to it
1274    in a single instruction.  */
1275
1276 #define FRVFDPIC_LZPLT_BLOCK_SIZE ((bfd_vma) 8 * 65535 + 4)
1277 #define FRVFDPIC_LZPLT_RESOLV_LOC (8 * 32767)
1278
1279 /* Add a dynamic relocation to the SRELOC section.  */
1280
1281 inline static bfd_vma
1282 _frvfdpic_add_dyn_reloc (bfd *output_bfd, asection *sreloc, bfd_vma offset,
1283                          int reloc_type, long dynindx, bfd_vma addend,
1284                          struct frvfdpic_relocs_info *entry)
1285 {
1286   Elf_Internal_Rela outrel;
1287   bfd_vma reloc_offset;
1288
1289   outrel.r_offset = offset;
1290   outrel.r_info = ELF32_R_INFO (dynindx, reloc_type);
1291   outrel.r_addend = addend;
1292
1293   reloc_offset = sreloc->reloc_count * sizeof (Elf32_External_Rel);
1294   BFD_ASSERT (reloc_offset < sreloc->size);
1295   bfd_elf32_swap_reloc_out (output_bfd, &outrel,
1296                             sreloc->contents + reloc_offset);
1297   sreloc->reloc_count++;
1298
1299   /* If the entry's index is zero, this relocation was probably to a
1300      linkonce section that got discarded.  We reserved a dynamic
1301      relocation, but it was for another entry than the one we got at
1302      the time of emitting the relocation.  Unfortunately there's no
1303      simple way for us to catch this situation, since the relocation
1304      is cleared right before calling relocate_section, at which point
1305      we no longer know what the relocation used to point to.  */
1306   if (entry->symndx)
1307     {
1308       BFD_ASSERT (entry->dynrelocs > 0);
1309       entry->dynrelocs--;
1310     }
1311
1312   return reloc_offset;
1313 }
1314
1315 /* Add a fixup to the ROFIXUP section.  */
1316
1317 static bfd_vma
1318 _frvfdpic_add_rofixup (bfd *output_bfd, asection *rofixup, bfd_vma offset,
1319                        struct frvfdpic_relocs_info *entry)
1320 {
1321   bfd_vma fixup_offset;
1322
1323   if (rofixup->flags & SEC_EXCLUDE)
1324     return -1;
1325
1326   fixup_offset = rofixup->reloc_count * 4;
1327   if (rofixup->contents)
1328     {
1329       BFD_ASSERT (fixup_offset < rofixup->size);
1330       bfd_put_32 (output_bfd, offset, rofixup->contents + fixup_offset);
1331     }
1332   rofixup->reloc_count++;
1333
1334   if (entry && entry->symndx)
1335     {
1336       /* See discussion about symndx == 0 in _frvfdpic_add_dyn_reloc
1337          above.  */
1338       BFD_ASSERT (entry->fixups > 0);
1339       entry->fixups--;
1340     }
1341
1342   return fixup_offset;
1343 }
1344
1345 /* Find the segment number in which OSEC, and output section, is
1346    located.  */
1347
1348 static unsigned
1349 _frvfdpic_osec_to_segment (bfd *output_bfd, asection *osec)
1350 {
1351   struct elf_segment_map *m;
1352   Elf_Internal_Phdr *p;
1353
1354   /* Find the segment that contains the output_section.  */
1355   for (m = elf_tdata (output_bfd)->segment_map,
1356          p = elf_tdata (output_bfd)->phdr;
1357        m != NULL;
1358        m = m->next, p++)
1359     {
1360       int i;
1361
1362       for (i = m->count - 1; i >= 0; i--)
1363         if (m->sections[i] == osec)
1364           break;
1365
1366       if (i >= 0)
1367         break;
1368     }
1369
1370   return p - elf_tdata (output_bfd)->phdr;
1371 }
1372
1373 inline static bfd_boolean
1374 _frvfdpic_osec_readonly_p (bfd *output_bfd, asection *osec)
1375 {
1376   unsigned seg = _frvfdpic_osec_to_segment (output_bfd, osec);
1377
1378   return ! (elf_tdata (output_bfd)->phdr[seg].p_flags & PF_W);
1379 }
1380
1381 #define FRVFDPIC_TLS_BIAS (2048 - 16)
1382
1383 /* Return the base VMA address which should be subtracted from real addresses
1384    when resolving TLSMOFF relocation.
1385    This is PT_TLS segment p_vaddr, plus the 2048-16 bias.  */
1386
1387 static bfd_vma
1388 tls_biased_base (struct bfd_link_info *info)
1389 {
1390   /* If tls_sec is NULL, we should have signalled an error already.  */
1391   if (elf_hash_table (info)->tls_sec == NULL)
1392     return FRVFDPIC_TLS_BIAS;
1393   return elf_hash_table (info)->tls_sec->vma + FRVFDPIC_TLS_BIAS;
1394 }
1395
1396 /* Generate relocations for GOT entries, function descriptors, and
1397    code for PLT and lazy PLT entries.  */
1398
1399 inline static bfd_boolean
1400 _frvfdpic_emit_got_relocs_plt_entries (struct frvfdpic_relocs_info *entry,
1401                                        bfd *output_bfd,
1402                                        struct bfd_link_info *info,
1403                                        asection *sec,
1404                                        Elf_Internal_Sym *sym,
1405                                        bfd_vma addend)
1406
1407 {
1408   bfd_vma fd_lazy_rel_offset = (bfd_vma)-1;
1409   int dynindx = -1;
1410
1411   if (entry->done)
1412     return TRUE;
1413   entry->done = 1;
1414
1415   if (entry->got_entry || entry->fdgot_entry || entry->fd_entry
1416       || entry->tlsoff_entry || entry->tlsdesc_entry)
1417     {
1418       /* If the symbol is dynamic, consider it for dynamic
1419          relocations, otherwise decay to section + offset.  */
1420       if (entry->symndx == -1 && entry->d.h->dynindx != -1)
1421         dynindx = entry->d.h->dynindx;
1422       else
1423         {
1424           if (sec->output_section
1425               && ! bfd_is_abs_section (sec->output_section)
1426               && ! bfd_is_und_section (sec->output_section))
1427             dynindx = elf_section_data (sec->output_section)->dynindx;
1428           else
1429             dynindx = 0;
1430         }
1431     }
1432
1433   /* Generate relocation for GOT entry pointing to the symbol.  */
1434   if (entry->got_entry)
1435     {
1436       int idx = dynindx;
1437       bfd_vma ad = addend;
1438
1439       /* If the symbol is dynamic but binds locally, use
1440          section+offset.  */
1441       if (sec && (entry->symndx != -1
1442                   || FRVFDPIC_SYM_LOCAL (info, entry->d.h)))
1443         {
1444           if (entry->symndx == -1)
1445             ad += entry->d.h->root.u.def.value;
1446           else
1447             ad += sym->st_value;
1448           ad += sec->output_offset;
1449           if (sec->output_section && elf_section_data (sec->output_section))
1450             idx = elf_section_data (sec->output_section)->dynindx;
1451           else
1452             idx = 0;
1453         }
1454
1455       /* If we're linking an executable at a fixed address, we can
1456          omit the dynamic relocation as long as the symbol is local to
1457          this module.  */
1458       if (info->executable && !info->pie
1459           && (entry->symndx != -1
1460               || FRVFDPIC_SYM_LOCAL (info, entry->d.h)))
1461         {
1462           if (sec)
1463             ad += sec->output_section->vma;
1464           if (entry->symndx != -1
1465               || entry->d.h->root.type != bfd_link_hash_undefweak)
1466             _frvfdpic_add_rofixup (output_bfd,
1467                                    frvfdpic_gotfixup_section (info),
1468                                    frvfdpic_got_section (info)->output_section
1469                                    ->vma
1470                                    + frvfdpic_got_section (info)->output_offset
1471                                    + frvfdpic_got_initial_offset (info)
1472                                    + entry->got_entry, entry);
1473         }
1474       else
1475         _frvfdpic_add_dyn_reloc (output_bfd, frvfdpic_gotrel_section (info),
1476                                  _bfd_elf_section_offset
1477                                  (output_bfd, info,
1478                                   frvfdpic_got_section (info),
1479                                   frvfdpic_got_initial_offset (info)
1480                                   + entry->got_entry)
1481                                  + frvfdpic_got_section (info)
1482                                  ->output_section->vma
1483                                  + frvfdpic_got_section (info)->output_offset,
1484                                  R_FRV_32, idx, ad, entry);
1485
1486       bfd_put_32 (output_bfd, ad,
1487                   frvfdpic_got_section (info)->contents
1488                   + frvfdpic_got_initial_offset (info)
1489                   + entry->got_entry);
1490     }
1491
1492   /* Generate relocation for GOT entry pointing to a canonical
1493      function descriptor.  */
1494   if (entry->fdgot_entry)
1495     {
1496       int reloc, idx;
1497       bfd_vma ad = 0;
1498
1499       if (! (entry->symndx == -1
1500              && entry->d.h->root.type == bfd_link_hash_undefweak
1501              && FRVFDPIC_SYM_LOCAL (info, entry->d.h)))
1502         {
1503           /* If the symbol is dynamic and there may be dynamic symbol
1504              resolution because we are, or are linked with, a shared
1505              library, emit a FUNCDESC relocation such that the dynamic
1506              linker will allocate the function descriptor.  If the
1507              symbol needs a non-local function descriptor but binds
1508              locally (e.g., its visibility is protected, emit a
1509              dynamic relocation decayed to section+offset.  */
1510           if (entry->symndx == -1
1511               && ! FRVFDPIC_FUNCDESC_LOCAL (info, entry->d.h)
1512               && FRVFDPIC_SYM_LOCAL (info, entry->d.h)
1513               && !(info->executable && !info->pie))
1514             {
1515               reloc = R_FRV_FUNCDESC;
1516               idx = elf_section_data (entry->d.h->root.u.def.section
1517                                       ->output_section)->dynindx;
1518               ad = entry->d.h->root.u.def.section->output_offset
1519                 + entry->d.h->root.u.def.value;
1520             }
1521           else if (entry->symndx == -1
1522                    && ! FRVFDPIC_FUNCDESC_LOCAL (info, entry->d.h))
1523             {
1524               reloc = R_FRV_FUNCDESC;
1525               idx = dynindx;
1526               ad = addend;
1527               if (ad)
1528                 return FALSE;
1529             }
1530           else
1531             {
1532               /* Otherwise, we know we have a private function descriptor,
1533                  so reference it directly.  */
1534               if (elf_hash_table (info)->dynamic_sections_created)
1535                 BFD_ASSERT (entry->privfd);
1536               reloc = R_FRV_32;
1537               idx = elf_section_data (frvfdpic_got_section (info)
1538                                       ->output_section)->dynindx;
1539               ad = frvfdpic_got_section (info)->output_offset
1540                 + frvfdpic_got_initial_offset (info) + entry->fd_entry;
1541             }
1542
1543           /* If there is room for dynamic symbol resolution, emit the
1544              dynamic relocation.  However, if we're linking an
1545              executable at a fixed location, we won't have emitted a
1546              dynamic symbol entry for the got section, so idx will be
1547              zero, which means we can and should compute the address
1548              of the private descriptor ourselves.  */
1549           if (info->executable && !info->pie
1550               && (entry->symndx != -1
1551                   || FRVFDPIC_FUNCDESC_LOCAL (info, entry->d.h)))
1552             {
1553               ad += frvfdpic_got_section (info)->output_section->vma;
1554               _frvfdpic_add_rofixup (output_bfd,
1555                                      frvfdpic_gotfixup_section (info),
1556                                      frvfdpic_got_section (info)
1557                                      ->output_section->vma
1558                                      + frvfdpic_got_section (info)
1559                                      ->output_offset
1560                                      + frvfdpic_got_initial_offset (info)
1561                                      + entry->fdgot_entry, entry);
1562             }
1563           else
1564             _frvfdpic_add_dyn_reloc (output_bfd,
1565                                      frvfdpic_gotrel_section (info),
1566                                      _bfd_elf_section_offset
1567                                      (output_bfd, info,
1568                                       frvfdpic_got_section (info),
1569                                       frvfdpic_got_initial_offset (info)
1570                                       + entry->fdgot_entry)
1571                                      + frvfdpic_got_section (info)
1572                                      ->output_section->vma
1573                                      + frvfdpic_got_section (info)
1574                                      ->output_offset,
1575                                      reloc, idx, ad, entry);
1576         }
1577
1578       bfd_put_32 (output_bfd, ad,
1579                   frvfdpic_got_section (info)->contents
1580                   + frvfdpic_got_initial_offset (info)
1581                   + entry->fdgot_entry);
1582     }
1583
1584   /* Generate relocation to fill in a private function descriptor in
1585      the GOT.  */
1586   if (entry->fd_entry)
1587     {
1588       int idx = dynindx;
1589       bfd_vma ad = addend;
1590       bfd_vma ofst;
1591       long lowword, highword;
1592
1593       /* If the symbol is dynamic but binds locally, use
1594          section+offset.  */
1595       if (sec && (entry->symndx != -1
1596                   || FRVFDPIC_SYM_LOCAL (info, entry->d.h)))
1597         {
1598           if (entry->symndx == -1)
1599             ad += entry->d.h->root.u.def.value;
1600           else
1601             ad += sym->st_value;
1602           ad += sec->output_offset;
1603           if (sec->output_section && elf_section_data (sec->output_section))
1604             idx = elf_section_data (sec->output_section)->dynindx;
1605           else
1606             idx = 0;
1607         }
1608
1609       /* If we're linking an executable at a fixed address, we can
1610          omit the dynamic relocation as long as the symbol is local to
1611          this module.  */
1612       if (info->executable && !info->pie
1613           && (entry->symndx != -1 || FRVFDPIC_SYM_LOCAL (info, entry->d.h)))
1614         {
1615           if (sec)
1616             ad += sec->output_section->vma;
1617           ofst = 0;
1618           if (entry->symndx != -1
1619               || entry->d.h->root.type != bfd_link_hash_undefweak)
1620             {
1621               _frvfdpic_add_rofixup (output_bfd,
1622                                      frvfdpic_gotfixup_section (info),
1623                                      frvfdpic_got_section (info)
1624                                      ->output_section->vma
1625                                      + frvfdpic_got_section (info)
1626                                      ->output_offset
1627                                      + frvfdpic_got_initial_offset (info)
1628                                      + entry->fd_entry, entry);
1629               _frvfdpic_add_rofixup (output_bfd,
1630                                      frvfdpic_gotfixup_section (info),
1631                                      frvfdpic_got_section (info)
1632                                      ->output_section->vma
1633                                      + frvfdpic_got_section (info)
1634                                      ->output_offset
1635                                      + frvfdpic_got_initial_offset (info)
1636                                      + entry->fd_entry + 4, entry);
1637             }
1638         }
1639       else
1640         {
1641           ofst =
1642             _frvfdpic_add_dyn_reloc (output_bfd,
1643                                      entry->lazyplt
1644                                      ? frvfdpic_pltrel_section (info)
1645                                      : frvfdpic_gotrel_section (info),
1646                                      _bfd_elf_section_offset
1647                                      (output_bfd, info,
1648                                       frvfdpic_got_section (info),
1649                                       frvfdpic_got_initial_offset (info)
1650                                       + entry->fd_entry)
1651                                      + frvfdpic_got_section (info)
1652                                      ->output_section->vma
1653                                      + frvfdpic_got_section (info)
1654                                      ->output_offset,
1655                                      R_FRV_FUNCDESC_VALUE, idx, ad, entry);
1656         }
1657
1658       /* If we've omitted the dynamic relocation, just emit the fixed
1659          addresses of the symbol and of the local GOT base offset.  */
1660       if (info->executable && !info->pie && sec && sec->output_section)
1661         {
1662           lowword = ad;
1663           highword = frvfdpic_got_section (info)->output_section->vma
1664             + frvfdpic_got_section (info)->output_offset
1665             + frvfdpic_got_initial_offset (info);
1666         }
1667       else if (entry->lazyplt)
1668         {
1669           if (ad)
1670             return FALSE;
1671
1672           fd_lazy_rel_offset = ofst;
1673
1674           /* A function descriptor used for lazy or local resolving is
1675              initialized such that its high word contains the output
1676              section index in which the PLT entries are located, and
1677              the low word contains the address of the lazy PLT entry
1678              entry point, that must be within the memory region
1679              assigned to that section.  */
1680           lowword = entry->lzplt_entry + 4
1681             + frvfdpic_plt_section (info)->output_offset
1682             + frvfdpic_plt_section (info)->output_section->vma;
1683           highword = _frvfdpic_osec_to_segment
1684             (output_bfd, frvfdpic_plt_section (info)->output_section);
1685         }
1686       else
1687         {
1688           /* A function descriptor for a local function gets the index
1689              of the section.  For a non-local function, it's
1690              disregarded.  */
1691           lowword = ad;
1692           if (entry->symndx == -1 && entry->d.h->dynindx != -1
1693               && entry->d.h->dynindx == idx)
1694             highword = 0;
1695           else
1696             highword = _frvfdpic_osec_to_segment
1697               (output_bfd, sec->output_section);
1698         }
1699
1700       bfd_put_32 (output_bfd, lowword,
1701                   frvfdpic_got_section (info)->contents
1702                   + frvfdpic_got_initial_offset (info)
1703                   + entry->fd_entry);
1704       bfd_put_32 (output_bfd, highword,
1705                   frvfdpic_got_section (info)->contents
1706                   + frvfdpic_got_initial_offset (info)
1707                   + entry->fd_entry + 4);
1708     }
1709
1710   /* Generate code for the PLT entry.  */
1711   if (entry->plt_entry != (bfd_vma) -1)
1712     {
1713       bfd_byte *plt_code = frvfdpic_plt_section (info)->contents
1714         + entry->plt_entry;
1715
1716       BFD_ASSERT (entry->fd_entry);
1717
1718       /* Figure out what kind of PLT entry we need, depending on the
1719          location of the function descriptor within the GOT.  */
1720       if (entry->fd_entry >= -(1 << (12 - 1))
1721           && entry->fd_entry < (1 << (12 - 1)))
1722         {
1723           /* lddi @(gr15, fd_entry), gr14 */
1724           bfd_put_32 (output_bfd,
1725                       0x9cccf000 | (entry->fd_entry & ((1 << 12) - 1)),
1726                       plt_code);
1727           plt_code += 4;
1728         }
1729       else
1730         {
1731           if (entry->fd_entry >= -(1 << (16 - 1))
1732               && entry->fd_entry < (1 << (16 - 1)))
1733             {
1734               /* setlos lo(fd_entry), gr14 */
1735               bfd_put_32 (output_bfd,
1736                           0x9cfc0000
1737                           | (entry->fd_entry & (((bfd_vma)1 << 16) - 1)),
1738                           plt_code);
1739               plt_code += 4;
1740             }
1741           else
1742             {
1743               /* sethi.p hi(fd_entry), gr14
1744                  setlo lo(fd_entry), gr14 */
1745               bfd_put_32 (output_bfd,
1746                           0x1cf80000
1747                           | ((entry->fd_entry >> 16)
1748                              & (((bfd_vma)1 << 16) - 1)),
1749                           plt_code);
1750               plt_code += 4;
1751               bfd_put_32 (output_bfd,
1752                           0x9cf40000
1753                           | (entry->fd_entry & (((bfd_vma)1 << 16) - 1)),
1754                           plt_code);
1755               plt_code += 4;
1756             }
1757           /* ldd @(gr14,gr15),gr14 */
1758           bfd_put_32 (output_bfd, 0x9c08e14f, plt_code);
1759           plt_code += 4;
1760         }
1761       /* jmpl @(gr14,gr0) */
1762       bfd_put_32 (output_bfd, 0x8030e000, plt_code);
1763     }
1764
1765   /* Generate code for the lazy PLT entry.  */
1766   if (entry->lzplt_entry != (bfd_vma) -1)
1767     {
1768       bfd_byte *lzplt_code = frvfdpic_plt_section (info)->contents
1769         + entry->lzplt_entry;
1770       bfd_vma resolverStub_addr;
1771
1772       bfd_put_32 (output_bfd, fd_lazy_rel_offset, lzplt_code);
1773       lzplt_code += 4;
1774
1775       resolverStub_addr = entry->lzplt_entry / FRVFDPIC_LZPLT_BLOCK_SIZE
1776         * FRVFDPIC_LZPLT_BLOCK_SIZE + FRVFDPIC_LZPLT_RESOLV_LOC;
1777       if (resolverStub_addr >= frvfdpic_plt_initial_offset (info))
1778         resolverStub_addr = frvfdpic_plt_initial_offset (info) - 12;
1779
1780       if (entry->lzplt_entry == resolverStub_addr)
1781         {
1782           /* This is a lazy PLT entry that includes a resolver call.  */
1783           /* ldd @(gr15,gr0), gr4
1784              jmpl @(gr4,gr0)  */
1785           bfd_put_32 (output_bfd, 0x8808f140, lzplt_code);
1786           bfd_put_32 (output_bfd, 0x80304000, lzplt_code + 4);
1787         }
1788       else
1789         {
1790           /* bra  resolverStub */
1791           bfd_put_32 (output_bfd,
1792                       0xc01a0000
1793                       | (((resolverStub_addr - entry->lzplt_entry)
1794                           / 4) & (((bfd_vma)1 << 16) - 1)),
1795                       lzplt_code);
1796         }
1797     }
1798
1799   /* Generate relocation for GOT entry holding the TLS offset.  */
1800   if (entry->tlsoff_entry)
1801     {
1802       int idx = dynindx;
1803       bfd_vma ad = addend;
1804
1805       if (entry->symndx != -1
1806           || FRVFDPIC_SYM_LOCAL (info, entry->d.h))
1807         {
1808           /* If the symbol is dynamic but binds locally, use
1809              section+offset.  */
1810           if (sec)
1811             {
1812               if (entry->symndx == -1)
1813                 ad += entry->d.h->root.u.def.value;
1814               else
1815                 ad += sym->st_value;
1816               ad += sec->output_offset;
1817               if (sec->output_section
1818                   && elf_section_data (sec->output_section))
1819                 idx = elf_section_data (sec->output_section)->dynindx;
1820               else
1821                 idx = 0;
1822             }
1823         }
1824
1825       /* *ABS*+addend is special for TLS relocations, use only the
1826          addend.  */
1827       if (info->executable
1828           && idx == 0
1829           && (bfd_is_abs_section (sec)
1830               || bfd_is_und_section (sec)))
1831         ;
1832       /* If we're linking an executable, we can entirely omit the
1833          dynamic relocation if the symbol is local to this module.  */
1834       else if (info->executable
1835                && (entry->symndx != -1
1836                    || FRVFDPIC_SYM_LOCAL (info, entry->d.h)))
1837         {
1838           if (sec)
1839             ad += sec->output_section->vma - tls_biased_base (info);
1840         }
1841       else
1842         {
1843           if (idx == 0
1844               && (bfd_is_abs_section (sec)
1845                   || bfd_is_und_section (sec)))
1846             {
1847               idx = elf_section_data (elf_hash_table (info)->tls_sec)->dynindx;
1848               ad += FRVFDPIC_TLS_BIAS;
1849             }
1850           _frvfdpic_add_dyn_reloc (output_bfd, frvfdpic_gotrel_section (info),
1851                                    _bfd_elf_section_offset
1852                                    (output_bfd, info,
1853                                     frvfdpic_got_section (info),
1854                                     frvfdpic_got_initial_offset (info)
1855                                     + entry->tlsoff_entry)
1856                                    + frvfdpic_got_section (info)
1857                                    ->output_section->vma
1858                                    + frvfdpic_got_section (info)
1859                                    ->output_offset,
1860                                    R_FRV_TLSOFF, idx, ad, entry);
1861         }
1862         
1863       bfd_put_32 (output_bfd, ad,
1864                   frvfdpic_got_section (info)->contents
1865                   + frvfdpic_got_initial_offset (info)
1866                   + entry->tlsoff_entry);
1867     }
1868
1869   if (entry->tlsdesc_entry)
1870     {
1871       int idx = dynindx;
1872       bfd_vma ad = addend;
1873
1874       /* If the symbol is dynamic but binds locally, use
1875          section+offset.  */
1876       if (sec && (entry->symndx != -1
1877                   || FRVFDPIC_SYM_LOCAL (info, entry->d.h)))
1878         {
1879           if (entry->symndx == -1)
1880             ad += entry->d.h->root.u.def.value;
1881           else
1882             ad += sym->st_value;
1883           ad += sec->output_offset;
1884           if (sec->output_section && elf_section_data (sec->output_section))
1885             idx = elf_section_data (sec->output_section)->dynindx;
1886           else
1887             idx = 0;
1888         }
1889
1890       /* If we didn't set up a TLS offset entry, but we're linking an
1891          executable and the symbol binds locally, we can use the
1892          module offset in the TLS descriptor in relaxations.  */
1893       if (info->executable && ! entry->tlsoff_entry)
1894         entry->tlsoff_entry = entry->tlsdesc_entry + 4;
1895
1896       if (info->executable && !info->pie
1897           && ((idx == 0
1898                && (bfd_is_abs_section (sec)
1899                    || bfd_is_und_section (sec)))
1900               || entry->symndx != -1
1901               || FRVFDPIC_SYM_LOCAL (info, entry->d.h)))
1902         {
1903           /* *ABS*+addend is special for TLS relocations, use only the
1904              addend for the TLS offset, and take the module id as
1905              0.  */
1906           if (idx == 0
1907               && (bfd_is_abs_section (sec)
1908                   || bfd_is_und_section (sec)))
1909             ;
1910           /* For other TLS symbols that bind locally, add the section
1911              TLS offset to the addend.  */
1912           else if (sec)
1913             ad += sec->output_section->vma - tls_biased_base (info);
1914
1915           bfd_put_32 (output_bfd,
1916                       frvfdpic_plt_section (info)->output_section->vma
1917                       + frvfdpic_plt_section (info)->output_offset
1918                       + frvfdpic_plt_tls_ret_offset (info),
1919                       frvfdpic_got_section (info)->contents
1920                       + frvfdpic_got_initial_offset (info)
1921                       + entry->tlsdesc_entry);
1922
1923           _frvfdpic_add_rofixup (output_bfd,
1924                                  frvfdpic_gotfixup_section (info),
1925                                  frvfdpic_got_section (info)
1926                                  ->output_section->vma
1927                                  + frvfdpic_got_section (info)
1928                                  ->output_offset
1929                                  + frvfdpic_got_initial_offset (info)
1930                                  + entry->tlsdesc_entry, entry);
1931
1932           BFD_ASSERT (frvfdpic_dynamic_got_plt_info (info)->tls_ret_refs);
1933
1934           /* We've used one of the reserved fixups, so discount it so
1935              that we can check at the end that we've used them
1936              all.  */
1937           frvfdpic_dynamic_got_plt_info (info)->tls_ret_refs--;
1938
1939           /* While at that, make sure the ret instruction makes to the
1940              right location in the PLT.  We could do it only when we
1941              got to 0, but since the check at the end will only print
1942              a warning, make sure we have the ret in place in case the
1943              warning is missed.  */
1944           bfd_put_32 (output_bfd, 0xc03a4000,
1945                       frvfdpic_plt_section (info)->contents
1946                       + frvfdpic_plt_tls_ret_offset (info));
1947         }
1948       else
1949         {
1950           if (idx == 0
1951               && (bfd_is_abs_section (sec)
1952                   || bfd_is_und_section (sec)))
1953             {
1954               idx = elf_section_data (elf_hash_table (info)->tls_sec)->dynindx;
1955               ad += FRVFDPIC_TLS_BIAS;
1956             }
1957
1958           _frvfdpic_add_dyn_reloc (output_bfd, frvfdpic_gotrel_section (info),
1959                                    _bfd_elf_section_offset
1960                                    (output_bfd, info,
1961                                     frvfdpic_got_section (info),
1962                                     frvfdpic_got_initial_offset (info)
1963                                     + entry->tlsdesc_entry)
1964                                    + frvfdpic_got_section (info)
1965                                    ->output_section->vma
1966                                    + frvfdpic_got_section (info)
1967                                    ->output_offset,
1968                                    R_FRV_TLSDESC_VALUE, idx, ad, entry);
1969
1970           bfd_put_32 (output_bfd, 0,
1971                       frvfdpic_got_section (info)->contents
1972                       + frvfdpic_got_initial_offset (info)
1973                       + entry->tlsdesc_entry);
1974         }
1975
1976       bfd_put_32 (output_bfd, ad,
1977                   frvfdpic_got_section (info)->contents
1978                   + frvfdpic_got_initial_offset (info)
1979                   + entry->tlsdesc_entry + 4);
1980     }
1981
1982   /* Generate code for the get-TLS-offset PLT entry.  */
1983   if (entry->tlsplt_entry != (bfd_vma) -1)
1984     {
1985       bfd_byte *plt_code = frvfdpic_plt_section (info)->contents
1986         + entry->tlsplt_entry;
1987       
1988       if (info->executable
1989           && (entry->symndx != -1
1990               || FRVFDPIC_SYM_LOCAL (info, entry->d.h)))
1991         {
1992           int idx = dynindx;
1993           bfd_vma ad = addend;
1994
1995           /* sec may be NULL when referencing an undefweak symbol
1996              while linking a static executable.  */
1997           if (!sec)
1998             {
1999               BFD_ASSERT (entry->symndx == -1
2000                           && entry->d.h->root.type == bfd_link_hash_undefweak);
2001             }
2002           else
2003             {
2004               if (entry->symndx == -1)
2005                 ad += entry->d.h->root.u.def.value;
2006               else
2007                 ad += sym->st_value;
2008               ad += sec->output_offset;
2009               if (sec->output_section
2010                   && elf_section_data (sec->output_section))
2011                 idx = elf_section_data (sec->output_section)->dynindx;
2012               else
2013                 idx = 0;
2014             }
2015           
2016           /* *ABS*+addend is special for TLS relocations, use only the
2017              addend for the TLS offset, and take the module id as
2018              0.  */
2019           if (idx == 0
2020               && (bfd_is_abs_section (sec)
2021                   || bfd_is_und_section (sec)))
2022             ;
2023           /* For other TLS symbols that bind locally, add the section
2024              TLS offset to the addend.  */
2025           else if (sec)
2026             ad += sec->output_section->vma - tls_biased_base (info);
2027           
2028           if ((bfd_signed_vma)ad >= -(1 << (16 - 1))
2029               && (bfd_signed_vma)ad < (1 << (16 - 1)))
2030             {
2031               /* setlos lo(ad), gr9 */
2032               bfd_put_32 (output_bfd,
2033                           0x92fc0000
2034                           | (ad
2035                              & (((bfd_vma)1 << 16) - 1)),
2036                           plt_code);
2037               plt_code += 4;
2038             }
2039           else
2040             {
2041               /* sethi.p hi(ad), gr9
2042                  setlo lo(ad), gr9 */
2043               bfd_put_32 (output_bfd,
2044                           0x12f80000
2045                           | ((ad >> 16)
2046                              & (((bfd_vma)1 << 16) - 1)),
2047                           plt_code);
2048               plt_code += 4;
2049               bfd_put_32 (output_bfd,
2050                           0x92f40000
2051                           | (ad
2052                              & (((bfd_vma)1 << 16) - 1)),
2053                           plt_code);
2054               plt_code += 4;
2055             }
2056           /* ret */
2057           bfd_put_32 (output_bfd, 0xc03a4000, plt_code);
2058         }
2059       else if (entry->tlsoff_entry)
2060         {
2061           /* Figure out what kind of PLT entry we need, depending on the
2062              location of the TLS descriptor within the GOT.  */
2063           if (entry->tlsoff_entry >= -(1 << (12 - 1))
2064               && entry->tlsoff_entry < (1 << (12 - 1)))
2065             {
2066               /* ldi @(gr15, tlsoff_entry), gr9 */
2067               bfd_put_32 (output_bfd,
2068                           0x92c8f000 | (entry->tlsoff_entry
2069                                         & ((1 << 12) - 1)),
2070                           plt_code);
2071               plt_code += 4;
2072             }
2073           else
2074             {
2075               if (entry->tlsoff_entry >= -(1 << (16 - 1))
2076                   && entry->tlsoff_entry < (1 << (16 - 1)))
2077                 {
2078                   /* setlos lo(tlsoff_entry), gr8 */
2079                   bfd_put_32 (output_bfd,
2080                               0x90fc0000
2081                               | (entry->tlsoff_entry
2082                                  & (((bfd_vma)1 << 16) - 1)),
2083                               plt_code);
2084                   plt_code += 4;
2085                 }
2086               else
2087                 {
2088                   /* sethi.p hi(tlsoff_entry), gr8
2089                      setlo lo(tlsoff_entry), gr8 */
2090                   bfd_put_32 (output_bfd,
2091                               0x10f80000
2092                               | ((entry->tlsoff_entry >> 16)
2093                                  & (((bfd_vma)1 << 16) - 1)),
2094                               plt_code);
2095                   plt_code += 4;
2096                   bfd_put_32 (output_bfd,
2097                               0x90f40000
2098                               | (entry->tlsoff_entry
2099                                  & (((bfd_vma)1 << 16) - 1)),
2100                               plt_code);
2101                   plt_code += 4;
2102                 }
2103               /* ld @(gr15,gr8),gr9 */
2104               bfd_put_32 (output_bfd, 0x9008f108, plt_code);
2105               plt_code += 4;
2106             }
2107           /* ret */
2108           bfd_put_32 (output_bfd, 0xc03a4000, plt_code);
2109         }
2110       else
2111         {
2112           BFD_ASSERT (entry->tlsdesc_entry);
2113
2114           /* Figure out what kind of PLT entry we need, depending on the
2115              location of the TLS descriptor within the GOT.  */
2116           if (entry->tlsdesc_entry >= -(1 << (12 - 1))
2117               && entry->tlsdesc_entry < (1 << (12 - 1)))
2118             {
2119               /* lddi @(gr15, tlsdesc_entry), gr8 */
2120               bfd_put_32 (output_bfd,
2121                           0x90ccf000 | (entry->tlsdesc_entry
2122                                         & ((1 << 12) - 1)),
2123                           plt_code);
2124               plt_code += 4;
2125             }
2126           else
2127             {
2128               if (entry->tlsdesc_entry >= -(1 << (16 - 1))
2129                   && entry->tlsdesc_entry < (1 << (16 - 1)))
2130                 {
2131                   /* setlos lo(tlsdesc_entry), gr8 */
2132                   bfd_put_32 (output_bfd,
2133                               0x90fc0000
2134                               | (entry->tlsdesc_entry
2135                                  & (((bfd_vma)1 << 16) - 1)),
2136                               plt_code);
2137                   plt_code += 4;
2138                 }
2139               else
2140                 {
2141                   /* sethi.p hi(tlsdesc_entry), gr8
2142                      setlo lo(tlsdesc_entry), gr8 */
2143                   bfd_put_32 (output_bfd,
2144                               0x10f80000
2145                               | ((entry->tlsdesc_entry >> 16)
2146                                  & (((bfd_vma)1 << 16) - 1)),
2147                               plt_code);
2148                   plt_code += 4;
2149                   bfd_put_32 (output_bfd,
2150                               0x90f40000
2151                               | (entry->tlsdesc_entry
2152                                  & (((bfd_vma)1 << 16) - 1)),
2153                               plt_code);
2154                   plt_code += 4;
2155                 }
2156               /* ldd @(gr15,gr8),gr8 */
2157               bfd_put_32 (output_bfd, 0x9008f148, plt_code);
2158               plt_code += 4;
2159             }
2160           /* jmpl @(gr8,gr0) */
2161           bfd_put_32 (output_bfd, 0x80308000, plt_code);
2162         }
2163     }
2164
2165   return TRUE;
2166 }
2167
2168 /* Handle an FRV small data reloc.  */
2169
2170 static bfd_reloc_status_type
2171 elf32_frv_relocate_gprel12 (info, input_bfd, input_section, relocation,
2172                             contents, value)
2173      struct bfd_link_info *info;
2174      bfd *input_bfd;
2175      asection *input_section;
2176      Elf_Internal_Rela *relocation;
2177      bfd_byte *contents;
2178      bfd_vma value;
2179 {
2180   bfd_vma insn;
2181   bfd_vma gp;
2182   struct bfd_link_hash_entry *h;
2183
2184   h = bfd_link_hash_lookup (info->hash, "_gp", FALSE, FALSE, TRUE);
2185
2186   gp = (h->u.def.value
2187         + h->u.def.section->output_section->vma
2188         + h->u.def.section->output_offset);
2189
2190   value -= input_section->output_section->vma;
2191   value -= (gp - input_section->output_section->vma);
2192
2193   insn = bfd_get_32 (input_bfd, contents + relocation->r_offset);
2194
2195   value += relocation->r_addend;
2196
2197   if ((long) value > 0x7ff || (long) value < -0x800)
2198     return bfd_reloc_overflow;
2199
2200   bfd_put_32 (input_bfd,
2201               (insn & 0xfffff000) | (value & 0xfff),
2202               contents + relocation->r_offset);
2203
2204   return bfd_reloc_ok;
2205 }
2206
2207 /* Handle an FRV small data reloc. for the u12 field.  */
2208
2209 static bfd_reloc_status_type
2210 elf32_frv_relocate_gprelu12 (info, input_bfd, input_section, relocation,
2211                              contents, value)
2212      struct bfd_link_info *info;
2213      bfd *input_bfd;
2214      asection *input_section;
2215      Elf_Internal_Rela *relocation;
2216      bfd_byte *contents;
2217      bfd_vma value;
2218 {
2219   bfd_vma insn;
2220   bfd_vma gp;
2221   struct bfd_link_hash_entry *h;
2222   bfd_vma mask;
2223
2224   h = bfd_link_hash_lookup (info->hash, "_gp", FALSE, FALSE, TRUE);
2225
2226   gp = (h->u.def.value
2227         + h->u.def.section->output_section->vma
2228         + h->u.def.section->output_offset);
2229
2230   value -= input_section->output_section->vma;
2231   value -= (gp - input_section->output_section->vma);
2232
2233   insn = bfd_get_32 (input_bfd, contents + relocation->r_offset);
2234
2235   value += relocation->r_addend;
2236
2237   if ((long) value > 0x7ff || (long) value < -0x800)
2238     return bfd_reloc_overflow;
2239
2240   /* The high 6 bits go into bits 17-12. The low 6 bits go into bits 5-0.  */
2241   mask = 0x3f03f;
2242   insn = (insn & ~mask) | ((value & 0xfc0) << 12) | (value & 0x3f);
2243
2244   bfd_put_32 (input_bfd, insn, contents + relocation->r_offset);
2245
2246   return bfd_reloc_ok;
2247 }
2248
2249 /* Handle an FRV ELF HI16 reloc.  */
2250
2251 static bfd_reloc_status_type
2252 elf32_frv_relocate_hi16 (input_bfd, relhi, contents, value)
2253      bfd *input_bfd;
2254      Elf_Internal_Rela *relhi;
2255      bfd_byte *contents;
2256      bfd_vma value;
2257 {
2258   bfd_vma insn;
2259
2260   insn = bfd_get_32 (input_bfd, contents + relhi->r_offset);
2261
2262   value += relhi->r_addend;
2263   value = ((value >> 16) & 0xffff);
2264
2265   insn = (insn & 0xffff0000) | value;
2266
2267   if ((long) value > 0xffff || (long) value < -0x10000)
2268     return bfd_reloc_overflow;
2269
2270   bfd_put_32 (input_bfd, insn, contents + relhi->r_offset);
2271   return bfd_reloc_ok;
2272
2273 }
2274 static bfd_reloc_status_type
2275 elf32_frv_relocate_lo16 (input_bfd, rello, contents, value)
2276      bfd *input_bfd;
2277      Elf_Internal_Rela *rello;
2278      bfd_byte *contents;
2279      bfd_vma value;
2280 {
2281   bfd_vma insn;
2282
2283   insn = bfd_get_32 (input_bfd, contents + rello->r_offset);
2284
2285   value += rello->r_addend;
2286   value = value & 0xffff;
2287
2288   insn = (insn & 0xffff0000) | value;
2289
2290   if ((long) value > 0xffff || (long) value < -0x10000)
2291     return bfd_reloc_overflow;
2292
2293   bfd_put_32 (input_bfd, insn, contents + rello->r_offset);
2294   return bfd_reloc_ok;
2295 }
2296
2297 /* Perform the relocation for the CALL label24 instruction.  */
2298
2299 static bfd_reloc_status_type
2300 elf32_frv_relocate_label24 (input_bfd, input_section, rello, contents, value)
2301      bfd *input_bfd;
2302      asection *input_section;
2303      Elf_Internal_Rela *rello;
2304      bfd_byte *contents;
2305      bfd_vma value;
2306 {
2307   bfd_vma insn;
2308   bfd_vma label6;
2309   bfd_vma label18;
2310
2311   /* The format for the call instruction is:
2312
2313     0 000000 0001111 000000000000000000
2314       label6 opcode  label18
2315
2316     The branch calculation is: pc + (4*label24)
2317     where label24 is the concatenation of label6 and label18.  */
2318
2319   /* Grab the instruction.  */
2320   insn = bfd_get_32 (input_bfd, contents + rello->r_offset);
2321
2322   value -= input_section->output_section->vma + input_section->output_offset;
2323   value -= rello->r_offset;
2324   value += rello->r_addend;
2325
2326   value = value >> 2;
2327
2328   label6  = value & 0xfc0000;
2329   label6  = label6 << 7;
2330
2331   label18 = value & 0x3ffff;
2332
2333   insn = insn & 0x803c0000;
2334   insn = insn | label6;
2335   insn = insn | label18;
2336
2337   bfd_put_32 (input_bfd, insn, contents + rello->r_offset);
2338
2339   return bfd_reloc_ok;
2340 }
2341
2342 static bfd_reloc_status_type
2343 elf32_frv_relocate_gprelhi (info, input_bfd, input_section, relocation,
2344                             contents, value)
2345      struct bfd_link_info *info;
2346      bfd *input_bfd;
2347      asection *input_section;
2348      Elf_Internal_Rela *relocation;
2349      bfd_byte *contents;
2350      bfd_vma value;
2351 {
2352   bfd_vma insn;
2353   bfd_vma gp;
2354   struct bfd_link_hash_entry *h;
2355
2356   h = bfd_link_hash_lookup (info->hash, "_gp", FALSE, FALSE, TRUE);
2357
2358   gp = (h->u.def.value
2359         + h->u.def.section->output_section->vma
2360         + h->u.def.section->output_offset);
2361
2362   value -= input_section->output_section->vma;
2363   value -= (gp - input_section->output_section->vma);
2364   value += relocation->r_addend;
2365   value = ((value >> 16) & 0xffff);
2366
2367   if ((long) value > 0xffff || (long) value < -0x10000)
2368     return bfd_reloc_overflow;
2369
2370   insn = bfd_get_32 (input_bfd, contents + relocation->r_offset);
2371   insn = (insn & 0xffff0000) | value;
2372
2373   bfd_put_32 (input_bfd, insn, contents + relocation->r_offset);
2374   return bfd_reloc_ok;
2375 }
2376
2377 static bfd_reloc_status_type
2378 elf32_frv_relocate_gprello (info, input_bfd, input_section, relocation,
2379                             contents, value)
2380      struct bfd_link_info *info;
2381      bfd *input_bfd;
2382      asection *input_section;
2383      Elf_Internal_Rela *relocation;
2384      bfd_byte *contents;
2385      bfd_vma value;
2386 {
2387   bfd_vma insn;
2388   bfd_vma gp;
2389   struct bfd_link_hash_entry *h;
2390
2391   h = bfd_link_hash_lookup (info->hash, "_gp", FALSE, FALSE, TRUE);
2392
2393   gp = (h->u.def.value
2394         + h->u.def.section->output_section->vma
2395         + h->u.def.section->output_offset);
2396
2397   value -= input_section->output_section->vma;
2398   value -= (gp - input_section->output_section->vma);
2399   value += relocation->r_addend;
2400   value = value & 0xffff;
2401
2402   if ((long) value > 0xffff || (long) value < -0x10000)
2403     return bfd_reloc_overflow;
2404
2405   insn = bfd_get_32 (input_bfd, contents + relocation->r_offset);
2406   insn = (insn & 0xffff0000) | value;
2407
2408   bfd_put_32 (input_bfd, insn, contents + relocation->r_offset);
2409
2410  return bfd_reloc_ok;
2411 }
2412
2413 static reloc_howto_type *
2414 frv_reloc_type_lookup (abfd, code)
2415      bfd *abfd ATTRIBUTE_UNUSED;
2416      bfd_reloc_code_real_type code;
2417 {
2418   switch (code)
2419     {
2420     default:
2421       break;
2422
2423     case BFD_RELOC_NONE:
2424       return &elf32_frv_howto_table[ (int) R_FRV_NONE];
2425
2426     case BFD_RELOC_32:
2427       if (elf_elfheader (abfd)->e_type == ET_EXEC
2428           || elf_elfheader (abfd)->e_type == ET_DYN)
2429         return &elf32_frv_rel_32_howto;
2430       /* Fall through.  */
2431     case BFD_RELOC_CTOR:
2432       return &elf32_frv_howto_table[ (int) R_FRV_32];
2433
2434     case BFD_RELOC_FRV_LABEL16:
2435       return &elf32_frv_howto_table[ (int) R_FRV_LABEL16];
2436
2437     case BFD_RELOC_FRV_LABEL24:
2438       return &elf32_frv_howto_table[ (int) R_FRV_LABEL24];
2439
2440     case BFD_RELOC_FRV_LO16:
2441       return &elf32_frv_howto_table[ (int) R_FRV_LO16];
2442
2443     case BFD_RELOC_FRV_HI16:
2444       return &elf32_frv_howto_table[ (int) R_FRV_HI16];
2445
2446     case BFD_RELOC_FRV_GPREL12:
2447       return &elf32_frv_howto_table[ (int) R_FRV_GPREL12];
2448
2449     case BFD_RELOC_FRV_GPRELU12:
2450       return &elf32_frv_howto_table[ (int) R_FRV_GPRELU12];
2451
2452     case BFD_RELOC_FRV_GPREL32:
2453       return &elf32_frv_howto_table[ (int) R_FRV_GPREL32];
2454
2455     case BFD_RELOC_FRV_GPRELHI:
2456       return &elf32_frv_howto_table[ (int) R_FRV_GPRELHI];
2457
2458     case BFD_RELOC_FRV_GPRELLO:
2459       return &elf32_frv_howto_table[ (int) R_FRV_GPRELLO];
2460
2461     case BFD_RELOC_FRV_GOT12:
2462       return &elf32_frv_howto_table[ (int) R_FRV_GOT12];
2463
2464     case BFD_RELOC_FRV_GOTHI:
2465       return &elf32_frv_howto_table[ (int) R_FRV_GOTHI];
2466
2467     case BFD_RELOC_FRV_GOTLO:
2468       return &elf32_frv_howto_table[ (int) R_FRV_GOTLO];
2469
2470     case BFD_RELOC_FRV_FUNCDESC:
2471       if (elf_elfheader (abfd)->e_type == ET_EXEC
2472           || elf_elfheader (abfd)->e_type == ET_DYN)
2473         return &elf32_frv_rel_funcdesc_howto;
2474       return &elf32_frv_howto_table[ (int) R_FRV_FUNCDESC];
2475
2476     case BFD_RELOC_FRV_FUNCDESC_GOT12:
2477       return &elf32_frv_howto_table[ (int) R_FRV_FUNCDESC_GOT12];
2478
2479     case BFD_RELOC_FRV_FUNCDESC_GOTHI:
2480       return &elf32_frv_howto_table[ (int) R_FRV_FUNCDESC_GOTHI];
2481
2482     case BFD_RELOC_FRV_FUNCDESC_GOTLO:
2483       return &elf32_frv_howto_table[ (int) R_FRV_FUNCDESC_GOTLO];
2484
2485     case BFD_RELOC_FRV_FUNCDESC_VALUE:
2486       if (elf_elfheader (abfd)->e_type == ET_EXEC
2487           || elf_elfheader (abfd)->e_type == ET_DYN)
2488         return &elf32_frv_rel_funcdesc_value_howto;
2489       return &elf32_frv_howto_table[ (int) R_FRV_FUNCDESC_VALUE];
2490
2491     case BFD_RELOC_FRV_FUNCDESC_GOTOFF12:
2492       return &elf32_frv_howto_table[ (int) R_FRV_FUNCDESC_GOTOFF12];
2493
2494     case BFD_RELOC_FRV_FUNCDESC_GOTOFFHI:
2495       return &elf32_frv_howto_table[ (int) R_FRV_FUNCDESC_GOTOFFHI];
2496
2497     case BFD_RELOC_FRV_FUNCDESC_GOTOFFLO:
2498       return &elf32_frv_howto_table[ (int) R_FRV_FUNCDESC_GOTOFFLO];
2499
2500     case BFD_RELOC_FRV_GOTOFF12:
2501       return &elf32_frv_howto_table[ (int) R_FRV_GOTOFF12];
2502
2503     case BFD_RELOC_FRV_GOTOFFHI:
2504       return &elf32_frv_howto_table[ (int) R_FRV_GOTOFFHI];
2505
2506     case BFD_RELOC_FRV_GOTOFFLO:
2507       return &elf32_frv_howto_table[ (int) R_FRV_GOTOFFLO];
2508
2509     case BFD_RELOC_FRV_GETTLSOFF:
2510       return &elf32_frv_howto_table[ (int) R_FRV_GETTLSOFF];
2511
2512     case BFD_RELOC_FRV_TLSDESC_VALUE:
2513       if (elf_elfheader (abfd)->e_type == ET_EXEC
2514           || elf_elfheader (abfd)->e_type == ET_DYN)
2515         return &elf32_frv_rel_tlsdesc_value_howto;
2516       return &elf32_frv_howto_table[ (int) R_FRV_TLSDESC_VALUE];
2517
2518     case BFD_RELOC_FRV_GOTTLSDESC12:
2519       return &elf32_frv_howto_table[ (int) R_FRV_GOTTLSDESC12];
2520
2521     case BFD_RELOC_FRV_GOTTLSDESCHI:
2522       return &elf32_frv_howto_table[ (int) R_FRV_GOTTLSDESCHI];
2523
2524     case BFD_RELOC_FRV_GOTTLSDESCLO:
2525       return &elf32_frv_howto_table[ (int) R_FRV_GOTTLSDESCLO];
2526
2527     case BFD_RELOC_FRV_TLSMOFF12:
2528       return &elf32_frv_howto_table[ (int) R_FRV_TLSMOFF12];
2529
2530     case BFD_RELOC_FRV_TLSMOFFHI:
2531       return &elf32_frv_howto_table[ (int) R_FRV_TLSMOFFHI];
2532
2533     case BFD_RELOC_FRV_TLSMOFFLO:
2534       return &elf32_frv_howto_table[ (int) R_FRV_TLSMOFFLO];
2535
2536     case BFD_RELOC_FRV_GOTTLSOFF12:
2537       return &elf32_frv_howto_table[ (int) R_FRV_GOTTLSOFF12];
2538
2539     case BFD_RELOC_FRV_GOTTLSOFFHI:
2540       return &elf32_frv_howto_table[ (int) R_FRV_GOTTLSOFFHI];
2541
2542     case BFD_RELOC_FRV_GOTTLSOFFLO:
2543       return &elf32_frv_howto_table[ (int) R_FRV_GOTTLSOFFLO];
2544
2545     case BFD_RELOC_FRV_TLSOFF:
2546       if (elf_elfheader (abfd)->e_type == ET_EXEC
2547           || elf_elfheader (abfd)->e_type == ET_DYN)
2548         return &elf32_frv_rel_tlsoff_howto;
2549       return &elf32_frv_howto_table[ (int) R_FRV_TLSOFF];
2550
2551     case BFD_RELOC_FRV_TLSDESC_RELAX:
2552       return &elf32_frv_howto_table[ (int) R_FRV_TLSDESC_RELAX];
2553
2554     case BFD_RELOC_FRV_GETTLSOFF_RELAX:
2555       return &elf32_frv_howto_table[ (int) R_FRV_GETTLSOFF_RELAX];
2556
2557     case BFD_RELOC_FRV_TLSOFF_RELAX:
2558       return &elf32_frv_howto_table[ (int) R_FRV_TLSOFF_RELAX];
2559
2560     case BFD_RELOC_FRV_TLSMOFF:
2561       return &elf32_frv_howto_table[ (int) R_FRV_TLSMOFF];
2562
2563     case BFD_RELOC_VTABLE_INHERIT:
2564       return &elf32_frv_vtinherit_howto;
2565
2566     case BFD_RELOC_VTABLE_ENTRY:
2567       return &elf32_frv_vtentry_howto;
2568     }
2569
2570   return NULL;
2571 }
2572
2573 /* Set the howto pointer for an FRV ELF reloc.  */
2574
2575 static void
2576 frv_info_to_howto_rela (abfd, cache_ptr, dst)
2577      bfd *abfd ATTRIBUTE_UNUSED;
2578      arelent *cache_ptr;
2579      Elf_Internal_Rela *dst;
2580 {
2581   unsigned int r_type;
2582
2583   r_type = ELF32_R_TYPE (dst->r_info);
2584   switch (r_type)
2585     {
2586     case R_FRV_GNU_VTINHERIT:
2587       cache_ptr->howto = &elf32_frv_vtinherit_howto;
2588       break;
2589
2590     case R_FRV_GNU_VTENTRY:
2591       cache_ptr->howto = &elf32_frv_vtentry_howto;
2592       break;
2593
2594     default:
2595       cache_ptr->howto = & elf32_frv_howto_table [r_type];
2596       break;
2597     }
2598 }
2599
2600 /* Set the howto pointer for an FRV ELF REL reloc.  */
2601 static void
2602 frvfdpic_info_to_howto_rel (bfd *abfd ATTRIBUTE_UNUSED,
2603                             arelent *cache_ptr, Elf_Internal_Rela *dst)
2604 {
2605   unsigned int r_type;
2606
2607   r_type = ELF32_R_TYPE (dst->r_info);
2608   switch (r_type)
2609     {
2610     case R_FRV_32:
2611       cache_ptr->howto = &elf32_frv_rel_32_howto;
2612       break;
2613
2614     case R_FRV_FUNCDESC:
2615       cache_ptr->howto = &elf32_frv_rel_funcdesc_howto;
2616       break;
2617
2618     case R_FRV_FUNCDESC_VALUE:
2619       cache_ptr->howto = &elf32_frv_rel_funcdesc_value_howto;
2620       break;
2621
2622     case R_FRV_TLSDESC_VALUE:
2623       cache_ptr->howto = &elf32_frv_rel_tlsdesc_value_howto;
2624       break;
2625
2626     case R_FRV_TLSOFF:
2627       cache_ptr->howto = &elf32_frv_rel_tlsoff_howto;
2628       break;
2629
2630     default:
2631       cache_ptr->howto = NULL;
2632       break;
2633     }
2634 }
2635 \f
2636 /* Perform a single relocation.  By default we use the standard BFD
2637    routines, but a few relocs, we have to do them ourselves.  */
2638
2639 static bfd_reloc_status_type
2640 frv_final_link_relocate (howto, input_bfd, input_section, contents, rel,
2641                          relocation)
2642      reloc_howto_type *howto;
2643      bfd *input_bfd;
2644      asection *input_section;
2645      bfd_byte *contents;
2646      Elf_Internal_Rela *rel;
2647      bfd_vma relocation;
2648 {
2649   return _bfd_final_link_relocate (howto, input_bfd, input_section,
2650                                    contents, rel->r_offset, relocation,
2651                                    rel->r_addend);
2652 }
2653
2654 \f
2655 /* Relocate an FRV ELF section.
2656
2657    The RELOCATE_SECTION function is called by the new ELF backend linker
2658    to handle the relocations for a section.
2659
2660    The relocs are always passed as Rela structures; if the section
2661    actually uses Rel structures, the r_addend field will always be
2662    zero.
2663
2664    This function is responsible for adjusting the section contents as
2665    necessary, and (if using Rela relocs and generating a relocatable
2666    output file) adjusting the reloc addend as necessary.
2667
2668    This function does not have to worry about setting the reloc
2669    address or the reloc symbol index.
2670
2671    LOCAL_SYMS is a pointer to the swapped in local symbols.
2672
2673    LOCAL_SECTIONS is an array giving the section in the input file
2674    corresponding to the st_shndx field of each local symbol.
2675
2676    The global hash table entry for the global symbols can be found
2677    via elf_sym_hashes (input_bfd).
2678
2679    When generating relocatable output, this function must handle
2680    STB_LOCAL/STT_SECTION symbols specially.  The output symbol is
2681    going to be the section symbol corresponding to the output
2682    section, which means that the addend must be adjusted
2683    accordingly.  */
2684
2685 static bfd_boolean
2686 elf32_frv_relocate_section (output_bfd, info, input_bfd, input_section,
2687                             contents, relocs, local_syms, local_sections)
2688      bfd *output_bfd ATTRIBUTE_UNUSED;
2689      struct bfd_link_info *info;
2690      bfd *input_bfd;
2691      asection *input_section;
2692      bfd_byte *contents;
2693      Elf_Internal_Rela *relocs;
2694      Elf_Internal_Sym *local_syms;
2695      asection **local_sections;
2696 {
2697   Elf_Internal_Shdr *symtab_hdr;
2698   struct elf_link_hash_entry **sym_hashes;
2699   Elf_Internal_Rela *rel;
2700   Elf_Internal_Rela *relend;
2701   unsigned isec_segment, got_segment, plt_segment, gprel_segment, tls_segment,
2702     check_segment[2];
2703   int silence_segment_error = !(info->shared || info->pie);
2704   unsigned long insn;
2705
2706   if (info->relocatable)
2707     return TRUE;
2708
2709   symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
2710   sym_hashes = elf_sym_hashes (input_bfd);
2711   relend     = relocs + input_section->reloc_count;
2712
2713   isec_segment = _frvfdpic_osec_to_segment (output_bfd,
2714                                             input_section->output_section);
2715   if (IS_FDPIC (output_bfd) && frvfdpic_got_section (info))
2716     got_segment = _frvfdpic_osec_to_segment (output_bfd,
2717                                              frvfdpic_got_section (info)
2718                                              ->output_section);
2719   else
2720     got_segment = -1;
2721   if (IS_FDPIC (output_bfd) && frvfdpic_gotfixup_section (info))
2722     gprel_segment = _frvfdpic_osec_to_segment (output_bfd,
2723                                                frvfdpic_gotfixup_section (info)
2724                                                ->output_section);
2725   else
2726     gprel_segment = -1;
2727   if (IS_FDPIC (output_bfd) && frvfdpic_plt_section (info))
2728     plt_segment = _frvfdpic_osec_to_segment (output_bfd,
2729                                              frvfdpic_plt_section (info)
2730                                              ->output_section);
2731   else
2732     plt_segment = -1;
2733   if (elf_hash_table (info)->tls_sec)
2734     tls_segment = _frvfdpic_osec_to_segment (output_bfd,
2735                                              elf_hash_table (info)->tls_sec);
2736   else
2737     tls_segment = -1;
2738
2739   for (rel = relocs; rel < relend; rel ++)
2740     {
2741       reloc_howto_type *howto;
2742       unsigned long r_symndx;
2743       Elf_Internal_Sym *sym;
2744       asection *sec;
2745       struct elf_link_hash_entry *h;
2746       bfd_vma relocation;
2747       bfd_reloc_status_type r;
2748       const char * name = NULL;
2749       int r_type;
2750       asection *osec;
2751       struct frvfdpic_relocs_info *picrel;
2752       bfd_vma orig_addend = rel->r_addend;
2753
2754       r_type = ELF32_R_TYPE (rel->r_info);
2755
2756       if (   r_type == R_FRV_GNU_VTINHERIT
2757           || r_type == R_FRV_GNU_VTENTRY)
2758         continue;
2759
2760       /* This is a final link.  */
2761       r_symndx = ELF32_R_SYM (rel->r_info);
2762       howto  = elf32_frv_howto_table + ELF32_R_TYPE (rel->r_info);
2763       h      = NULL;
2764       sym    = NULL;
2765       sec    = NULL;
2766
2767       if (r_symndx < symtab_hdr->sh_info)
2768         {
2769           sym = local_syms + r_symndx;
2770           osec = sec = local_sections [r_symndx];
2771           relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
2772
2773           name = bfd_elf_string_from_elf_section
2774             (input_bfd, symtab_hdr->sh_link, sym->st_name);
2775           name = (name == NULL) ? bfd_section_name (input_bfd, sec) : name;
2776         }
2777       else
2778         {
2779           h = sym_hashes [r_symndx - symtab_hdr->sh_info];
2780
2781           while (h->root.type == bfd_link_hash_indirect
2782                  || h->root.type == bfd_link_hash_warning)
2783             h = (struct elf_link_hash_entry *) h->root.u.i.link;
2784
2785           name = h->root.root.string;
2786
2787           if ((h->root.type == bfd_link_hash_defined
2788                || h->root.type == bfd_link_hash_defweak))
2789             {
2790               if (/* TLSMOFF forces local binding.  */
2791                   r_type != R_FRV_TLSMOFF
2792                   && ! FRVFDPIC_SYM_LOCAL (info, h))
2793                 {
2794                   sec = NULL;
2795                   relocation = 0;
2796                 }
2797               else
2798                 {
2799                   sec = h->root.u.def.section;
2800                   relocation = (h->root.u.def.value
2801                                 + sec->output_section->vma
2802                                 + sec->output_offset);
2803                 }
2804             }
2805           else if (h->root.type == bfd_link_hash_undefweak)
2806             {
2807               relocation = 0;
2808             }
2809           else if (info->unresolved_syms_in_objects == RM_IGNORE
2810                    && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
2811             relocation = 0;
2812           else
2813             {
2814               if (! ((*info->callbacks->undefined_symbol)
2815                      (info, h->root.root.string, input_bfd,
2816                       input_section, rel->r_offset,
2817                       (info->unresolved_syms_in_objects == RM_GENERATE_ERROR
2818                        || ELF_ST_VISIBILITY (h->other)))))
2819                 return FALSE;
2820               relocation = 0;
2821             }
2822           osec = sec;
2823         }
2824
2825       switch (r_type)
2826         {
2827         case R_FRV_LABEL24:
2828         case R_FRV_32:
2829           if (! IS_FDPIC (output_bfd))
2830             goto non_fdpic;
2831
2832         case R_FRV_GOT12:
2833         case R_FRV_GOTHI:
2834         case R_FRV_GOTLO:
2835         case R_FRV_FUNCDESC_GOT12:
2836         case R_FRV_FUNCDESC_GOTHI:
2837         case R_FRV_FUNCDESC_GOTLO:
2838         case R_FRV_GOTOFF12:
2839         case R_FRV_GOTOFFHI:
2840         case R_FRV_GOTOFFLO:
2841         case R_FRV_FUNCDESC_GOTOFF12:
2842         case R_FRV_FUNCDESC_GOTOFFHI:
2843         case R_FRV_FUNCDESC_GOTOFFLO:
2844         case R_FRV_FUNCDESC:
2845         case R_FRV_FUNCDESC_VALUE:
2846         case R_FRV_GETTLSOFF:
2847         case R_FRV_TLSDESC_VALUE:
2848         case R_FRV_GOTTLSDESC12:
2849         case R_FRV_GOTTLSDESCHI:
2850         case R_FRV_GOTTLSDESCLO:
2851         case R_FRV_TLSMOFF12:
2852         case R_FRV_TLSMOFFHI:
2853         case R_FRV_TLSMOFFLO:
2854         case R_FRV_GOTTLSOFF12:
2855         case R_FRV_GOTTLSOFFHI:
2856         case R_FRV_GOTTLSOFFLO:
2857         case R_FRV_TLSOFF:
2858         case R_FRV_TLSDESC_RELAX:
2859         case R_FRV_GETTLSOFF_RELAX:
2860         case R_FRV_TLSOFF_RELAX:
2861         case R_FRV_TLSMOFF:
2862           if (h != NULL)
2863             picrel = frvfdpic_relocs_info_for_global (frvfdpic_relocs_info
2864                                                       (info), input_bfd, h,
2865                                                       orig_addend, INSERT);
2866           else
2867             /* In order to find the entry we created before, we must
2868                use the original addend, not the one that may have been
2869                modified by _bfd_elf_rela_local_sym().  */
2870             picrel = frvfdpic_relocs_info_for_local (frvfdpic_relocs_info
2871                                                      (info), input_bfd, r_symndx,
2872                                                      orig_addend, INSERT);
2873           if (! picrel)
2874             return FALSE;
2875
2876           if (!_frvfdpic_emit_got_relocs_plt_entries (picrel, output_bfd, info,
2877                                                       osec, sym,
2878                                                       rel->r_addend))
2879             {
2880               (*_bfd_error_handler)
2881                 (_("%B: relocation at `%A+0x%x' references symbol `%s' with nonzero addend"),
2882                  input_bfd, input_section, rel->r_offset, name);
2883               return FALSE;
2884
2885             }
2886
2887           break;
2888
2889         default:
2890         non_fdpic:
2891           picrel = NULL;
2892           if (h && ! FRVFDPIC_SYM_LOCAL (info, h))
2893             {
2894               info->callbacks->warning
2895                 (info, _("relocation references symbol not defined in the module"),
2896                  name, input_bfd, input_section, rel->r_offset);
2897               return FALSE;
2898             }
2899           break;
2900         }
2901
2902       switch (r_type)
2903         {
2904         case R_FRV_GETTLSOFF:
2905         case R_FRV_TLSDESC_VALUE:
2906         case R_FRV_GOTTLSDESC12:
2907         case R_FRV_GOTTLSDESCHI:
2908         case R_FRV_GOTTLSDESCLO:
2909         case R_FRV_TLSMOFF12:
2910         case R_FRV_TLSMOFFHI:
2911         case R_FRV_TLSMOFFLO:
2912         case R_FRV_GOTTLSOFF12:
2913         case R_FRV_GOTTLSOFFHI:
2914         case R_FRV_GOTTLSOFFLO:
2915         case R_FRV_TLSOFF:
2916         case R_FRV_TLSDESC_RELAX:
2917         case R_FRV_GETTLSOFF_RELAX:
2918         case R_FRV_TLSOFF_RELAX:
2919         case R_FRV_TLSMOFF:
2920           if (sec && (bfd_is_abs_section (sec) || bfd_is_und_section (sec)))
2921             relocation += tls_biased_base (info);
2922           break;
2923
2924         default:
2925           break;
2926         }
2927
2928       /* Try to apply TLS relaxations.  */
2929       if (1)
2930         switch (r_type)
2931           {
2932
2933 #define LOCAL_EXEC_P(info, picrel) \
2934   ((info)->executable \
2935    && (picrel->symndx != -1 || FRVFDPIC_SYM_LOCAL ((info), (picrel)->d.h)))
2936 #define INITIAL_EXEC_P(info, picrel) \
2937   (((info)->executable || (info)->flags & DF_STATIC_TLS) \
2938    && (picrel)->tlsoff_entry)
2939
2940 #define IN_RANGE_FOR_OFST12_P(value) \
2941   ((bfd_vma)((value) + 2048) < (bfd_vma)4096)
2942 #define IN_RANGE_FOR_SETLOS_P(value) \
2943   ((bfd_vma)((value) + 32768) < (bfd_vma)65536)
2944 #define TLSMOFF_IN_RANGE_FOR_SETLOS_P(value, info) \
2945   (IN_RANGE_FOR_SETLOS_P ((value) - tls_biased_base (info)))
2946
2947 #define RELAX_GETTLSOFF_LOCAL_EXEC_P(info, picrel, value) \
2948   (LOCAL_EXEC_P ((info), (picrel)) \
2949    && TLSMOFF_IN_RANGE_FOR_SETLOS_P((value), (info)))
2950 #define RELAX_GETTLSOFF_INITIAL_EXEC_P(info, picrel) \
2951   (INITIAL_EXEC_P ((info), (picrel)) \
2952    && IN_RANGE_FOR_OFST12_P ((picrel)->tlsoff_entry))
2953
2954 #define RELAX_TLSDESC_LOCAL_EXEC_P(info, picrel, value) \
2955   (LOCAL_EXEC_P ((info), (picrel)))
2956 #define RELAX_TLSDESC_INITIAL_EXEC_P(info, picrel) \
2957   (INITIAL_EXEC_P ((info), (picrel)))
2958  
2959 #define RELAX_GOTTLSOFF_LOCAL_EXEC_P(info, picrel, value) \
2960   (LOCAL_EXEC_P ((info), (picrel)) \
2961    && TLSMOFF_IN_RANGE_FOR_SETLOS_P((value), (info)))
2962
2963           case R_FRV_GETTLSOFF:
2964             insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
2965
2966             /* Is this a call instruction?  */
2967             if ((insn & (unsigned long)0x01fc0000) != 0x003c0000)
2968               {
2969                 r = info->callbacks->warning
2970                   (info,
2971                    _("R_FRV_GETTLSOFF not applied to a call instruction"),
2972                    name, input_bfd, input_section, rel->r_offset);
2973                 return FALSE;
2974               }
2975             
2976             if (RELAX_GETTLSOFF_LOCAL_EXEC_P (info, picrel,
2977                                               relocation + rel->r_addend))
2978               {
2979                 /* Replace the call instruction (except the packing bit)
2980                    with setlos #tlsmofflo(symbol+offset), gr9.  */
2981                 insn &= (unsigned long)0x80000000;
2982                 insn |= (unsigned long)0x12fc0000;
2983                 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
2984
2985                 r_type = R_FRV_TLSMOFFLO;
2986                 howto  = elf32_frv_howto_table + r_type;
2987                 rel->r_info = ELF32_R_INFO (r_symndx, r_type);
2988               }
2989
2990             else if (RELAX_GETTLSOFF_INITIAL_EXEC_P (info, picrel))
2991               {
2992                 /* Replace the call instruction (except the packing bit)
2993                    with ldi @(gr15, #gottlsoff12(symbol+addend)), gr9.  */
2994                 insn &= (unsigned long)0x80000000;
2995                 insn |= (unsigned long)0x12c8f000;
2996                 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
2997
2998                 r_type = R_FRV_GOTTLSOFF12;
2999                 howto  = elf32_frv_howto_table + r_type;
3000                 rel->r_info = ELF32_R_INFO (r_symndx, r_type);
3001               }
3002
3003             break;
3004
3005           case R_FRV_GOTTLSDESC12:
3006             insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
3007
3008             /* Is this an lddi instruction?  */
3009             if ((insn & (unsigned long)0x01fc0000) != 0x00cc0000)
3010               {
3011                 r = info->callbacks->warning
3012                   (info,
3013                    _("R_FRV_GOTTLSDESC12 not applied to an lddi instruction"),
3014                    name, input_bfd, input_section, rel->r_offset);
3015                 return FALSE;
3016               }
3017
3018             if (RELAX_TLSDESC_LOCAL_EXEC_P (info, picrel,
3019                                             relocation + rel->r_addend)
3020                 && TLSMOFF_IN_RANGE_FOR_SETLOS_P (relocation + rel->r_addend,
3021                                                   info))
3022               {
3023                 /* Replace lddi @(grB, #gottlsdesc12(symbol+offset), grC
3024                    with setlos #tlsmofflo(symbol+offset), gr<C+1>.
3025                    Preserve the packing bit.  */
3026                 insn = (insn & (unsigned long)0x80000000)
3027                   | ((insn + (unsigned long)0x02000000)
3028                      & (unsigned long)0x7e000000);
3029                 insn |= (unsigned long)0x00fc0000;
3030                 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
3031
3032                 r_type = R_FRV_TLSMOFFLO;
3033                 howto  = elf32_frv_howto_table + r_type;
3034                 rel->r_info = ELF32_R_INFO (r_symndx, r_type);
3035               }
3036
3037             else if (RELAX_TLSDESC_LOCAL_EXEC_P (info, picrel,
3038                                                  relocation + rel->r_addend))
3039               {
3040                 /* Replace lddi @(grB, #gottlsdesc12(symbol+offset), grC
3041                    with sethi #tlsmoffhi(symbol+offset), gr<C+1>.
3042                    Preserve the packing bit.  */
3043                 insn = (insn & (unsigned long)0x80000000)
3044                   | ((insn + (unsigned long)0x02000000)
3045                      & (unsigned long)0x7e000000);
3046                 insn |= (unsigned long)0x00f80000;
3047                 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
3048
3049                 r_type = R_FRV_TLSMOFFHI;
3050                 howto  = elf32_frv_howto_table + r_type;
3051                 rel->r_info = ELF32_R_INFO (r_symndx, r_type);
3052               }
3053
3054             else if (RELAX_TLSDESC_INITIAL_EXEC_P (info, picrel))
3055               {
3056                 /* Replace lddi @(grB, #gottlsdesc12(symbol+offset), grC
3057                    with ldi @(grB, #gottlsoff12(symbol+offset),
3058                    gr<C+1>.  Preserve the packing bit.  If gottlsoff12
3059                    overflows, we'll error out, but that's sort-of ok,
3060                    since we'd started with gottlsdesc12, that's actually
3061                    more demanding.  Compiling with -fPIE instead of
3062                    -fpie would fix it; linking with --relax should fix
3063                    it as well.  */
3064                 insn = (insn & (unsigned long)0x80cbf000)
3065                   | ((insn + (unsigned long)0x02000000)
3066                      & (unsigned long)0x7e000000);
3067                 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
3068
3069                 r_type = R_FRV_GOTTLSOFF12;
3070                 howto  = elf32_frv_howto_table + r_type;
3071                 rel->r_info = ELF32_R_INFO (r_symndx, r_type);
3072               }
3073
3074             break;
3075
3076           case R_FRV_GOTTLSDESCHI:
3077             insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
3078
3079             /* Is this a sethi instruction?  */
3080             if ((insn & (unsigned long)0x01ff0000) != 0x00f80000)
3081               {
3082                 r = info->callbacks->warning
3083                   (info,
3084                    _("R_FRV_GOTTLSDESCHI not applied to a sethi instruction"),
3085                    name, input_bfd, input_section, rel->r_offset);
3086                 return FALSE;
3087               }
3088
3089             if (RELAX_TLSDESC_LOCAL_EXEC_P (info, picrel,
3090                                             relocation + rel->r_addend)
3091                 || (RELAX_TLSDESC_INITIAL_EXEC_P (info, picrel)
3092                     && IN_RANGE_FOR_SETLOS_P (picrel->tlsoff_entry)))
3093               {
3094                 /* Replace sethi with a nop.  Preserve the packing bit.  */
3095                 insn &= (unsigned long)0x80000000;
3096                 insn |= (unsigned long)0x00880000;
3097                 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
3098
3099                 /* Nothing to relocate.  */
3100                 continue;
3101               }
3102
3103             else if (RELAX_TLSDESC_INITIAL_EXEC_P (info, picrel))
3104               {
3105                 /* Simply decay GOTTLSDESC to GOTTLSOFF.  */
3106                 r_type = R_FRV_GOTTLSOFFHI;
3107                 howto  = elf32_frv_howto_table + r_type;
3108                 rel->r_info = ELF32_R_INFO (r_symndx, r_type);
3109               }
3110
3111             break;
3112             
3113           case R_FRV_GOTTLSDESCLO:
3114             insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
3115
3116             /* Is this a setlo or setlos instruction?  */
3117             if ((insn & (unsigned long)0x01f70000) != 0x00f40000)
3118               {
3119                 r = info->callbacks->warning
3120                   (info,
3121                    _("R_FRV_GOTTLSDESCLO"
3122                      " not applied to a setlo or setlos instruction"),
3123                    name, input_bfd, input_section, rel->r_offset);
3124                 return FALSE;
3125               }
3126
3127             if (RELAX_TLSDESC_LOCAL_EXEC_P (info, picrel,
3128                                             relocation + rel->r_addend)
3129                 || (RELAX_TLSDESC_INITIAL_EXEC_P (info, picrel)
3130                     && IN_RANGE_FOR_OFST12_P (picrel->tlsoff_entry)))
3131               {
3132                 /* Replace setlo/setlos with a nop.  Preserve the
3133                    packing bit.  */
3134                 insn &= (unsigned long)0x80000000;
3135                 insn |= (unsigned long)0x00880000;
3136                 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
3137
3138                 /* Nothing to relocate.  */
3139                 continue;
3140               }
3141
3142             else if (RELAX_TLSDESC_INITIAL_EXEC_P (info, picrel))
3143               {
3144                 /* If the corresponding sethi (if it exists) decayed
3145                    to a nop, make sure this becomes (or already is) a
3146                    setlos, not setlo.  */
3147                 if (IN_RANGE_FOR_SETLOS_P (picrel->tlsoff_entry))
3148                   {
3149                     insn |= (unsigned long)0x00080000;
3150                     bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
3151                   }
3152
3153                 /* Simply decay GOTTLSDESC to GOTTLSOFF.  */
3154                 r_type = R_FRV_GOTTLSOFFLO;
3155                 howto  = elf32_frv_howto_table + r_type;
3156                 rel->r_info = ELF32_R_INFO (r_symndx, r_type);
3157               }
3158
3159             break;
3160             
3161           case R_FRV_TLSDESC_RELAX:
3162             insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
3163
3164             /* Is this an ldd instruction?  */
3165             if ((insn & (unsigned long)0x01fc0fc0) != 0x00080140)
3166               {
3167                 r = info->callbacks->warning
3168                   (info,
3169                    _("R_FRV_TLSDESC_RELAX not applied to an ldd instruction"),
3170                    name, input_bfd, input_section, rel->r_offset);
3171                 return FALSE;
3172               }
3173
3174             if (RELAX_TLSDESC_LOCAL_EXEC_P (info, picrel,
3175                                             relocation + rel->r_addend)
3176                 && TLSMOFF_IN_RANGE_FOR_SETLOS_P (relocation + rel->r_addend,
3177                                                   info))
3178               {
3179                 /* Replace ldd #tlsdesc(symbol+offset)@(grB, grA), grC
3180                    with setlos #tlsmofflo(symbol+offset), gr<C+1>.
3181                    Preserve the packing bit.  */
3182                 insn = (insn & (unsigned long)0x80000000)
3183                   | ((insn + (unsigned long)0x02000000)
3184                      & (unsigned long)0x7e000000);
3185                 insn |= (unsigned long)0x00fc0000;
3186                 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
3187
3188                 r_type = R_FRV_TLSMOFFLO;
3189                 howto  = elf32_frv_howto_table + r_type;
3190                 rel->r_info = ELF32_R_INFO (r_symndx, r_type);
3191               }
3192
3193             else if (RELAX_TLSDESC_LOCAL_EXEC_P (info, picrel,
3194                                                  relocation + rel->r_addend))
3195               {
3196                 /* Replace ldd #tlsdesc(symbol+offset)@(grB, grA), grC
3197                    with sethi #tlsmoffhi(symbol+offset), gr<C+1>.
3198                    Preserve the packing bit.  */
3199                 insn = (insn & (unsigned long)0x80000000)
3200                   | ((insn + (unsigned long)0x02000000)
3201                      & (unsigned long)0x7e000000);
3202                 insn |= (unsigned long)0x00f80000;
3203                 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
3204
3205                 r_type = R_FRV_TLSMOFFHI;
3206                 howto  = elf32_frv_howto_table + r_type;
3207                 rel->r_info = ELF32_R_INFO (r_symndx, r_type);
3208               }
3209
3210             else if (RELAX_TLSDESC_INITIAL_EXEC_P (info, picrel)
3211                      && IN_RANGE_FOR_OFST12_P (picrel->tlsoff_entry))
3212               {
3213                 /* Replace ldd #tlsdesc(symbol+offset)@(grB, grA), grC
3214                    with ldi @(grB, #gottlsoff12(symbol+offset), gr<C+1>.
3215                    Preserve the packing bit.  */
3216                 insn = (insn & (unsigned long)0x8003f000)
3217                   | (unsigned long)0x00c80000
3218                   | ((insn + (unsigned long)0x02000000)
3219                      & (unsigned long)0x7e000000);
3220                 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
3221
3222                 r_type = R_FRV_GOTTLSOFF12;
3223                 howto  = elf32_frv_howto_table + r_type;
3224                 rel->r_info = ELF32_R_INFO (r_symndx, r_type);
3225               }
3226
3227             else if (RELAX_TLSDESC_INITIAL_EXEC_P (info, picrel))
3228               {
3229                 /* Replace ldd #tlsdesc(symbol+offset)@(grB, grA), grC
3230                    with ld #tlsoff(symbol+offset)@(grB, grA), gr<C+1>.
3231                    Preserve the packing bit.  */
3232                 insn = (insn & (unsigned long)0x81ffffbf)
3233                   | ((insn + (unsigned long)0x02000000)
3234                      & (unsigned long)0x7e000000);
3235                 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
3236
3237                 /* #tlsoff(symbol+offset) is just a relaxation
3238                     annotation, so there's nothing left to
3239                     relocate.  */
3240                 continue;
3241               }
3242
3243             break;
3244
3245           case R_FRV_GETTLSOFF_RELAX:
3246             insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
3247
3248             /* Is this a calll or callil instruction?  */
3249             if ((insn & (unsigned long)0x7ff80fc0) != 0x02300000)
3250               {
3251                 r = info->callbacks->warning
3252                   (info,
3253                    _("R_FRV_GETTLSOFF_RELAX"
3254                      " not applied to a calll instruction"),
3255                    name, input_bfd, input_section, rel->r_offset);
3256                 return FALSE;
3257               }
3258
3259             if (RELAX_TLSDESC_LOCAL_EXEC_P (info, picrel,
3260                                             relocation + rel->r_addend)
3261                 && TLSMOFF_IN_RANGE_FOR_SETLOS_P (relocation + rel->r_addend,
3262                                                   info))
3263               {
3264                 /* Replace calll with a nop.  Preserve the packing bit.  */
3265                 insn &= (unsigned long)0x80000000;
3266                 insn |= (unsigned long)0x00880000;
3267                 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
3268
3269                 /* Nothing to relocate.  */
3270                 continue;
3271               }
3272
3273             else if (RELAX_TLSDESC_LOCAL_EXEC_P (info, picrel,
3274                                                  relocation + rel->r_addend))
3275               {
3276                 /* Replace calll with setlo #tlsmofflo(symbol+offset), gr9.
3277                    Preserve the packing bit.  */
3278                 insn &= (unsigned long)0x80000000;
3279                 insn |= (unsigned long)0x12f40000;
3280                 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
3281
3282                 r_type = R_FRV_TLSMOFFLO;
3283                 howto  = elf32_frv_howto_table + r_type;
3284                 rel->r_info = ELF32_R_INFO (r_symndx, r_type);
3285               }
3286
3287             else if (RELAX_TLSDESC_INITIAL_EXEC_P (info, picrel))
3288               {
3289                 /* Replace calll with a nop.  Preserve the packing bit.  */
3290                 insn &= (unsigned long)0x80000000;
3291                 insn |= (unsigned long)0x00880000;
3292                 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
3293
3294                 /* Nothing to relocate.  */
3295                 continue;
3296               }
3297
3298             break;
3299
3300           case R_FRV_GOTTLSOFF12:
3301             insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
3302
3303             /* Is this an ldi instruction?  */
3304             if ((insn & (unsigned long)0x01fc0000) != 0x00c80000)
3305               {
3306                 r = info->callbacks->warning
3307                   (info,
3308                    _("R_FRV_GOTTLSOFF12 not applied to an ldi instruction"),
3309                    name, input_bfd, input_section, rel->r_offset);
3310                 return FALSE;
3311               }
3312
3313             if (RELAX_GOTTLSOFF_LOCAL_EXEC_P (info, picrel,
3314                                               relocation + rel->r_addend))
3315               {
3316                 /* Replace ldi @(grB, #gottlsoff12(symbol+offset), grC
3317                    with setlos #tlsmofflo(symbol+offset), grC.
3318                    Preserve the packing bit.  */
3319                 insn &= (unsigned long)0xfe000000;
3320                 insn |= (unsigned long)0x00fc0000;
3321                 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
3322
3323                 r_type = R_FRV_TLSMOFFLO;
3324                 howto  = elf32_frv_howto_table + r_type;
3325                 rel->r_info = ELF32_R_INFO (r_symndx, r_type);
3326               }
3327
3328             break;
3329
3330           case R_FRV_GOTTLSOFFHI:
3331             insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
3332
3333             /* Is this a sethi instruction?  */
3334             if ((insn & (unsigned long)0x01ff0000) != 0x00f80000)
3335               {
3336                 r = info->callbacks->warning
3337                   (info,
3338                    _("R_FRV_GOTTLSOFFHI not applied to a sethi instruction"),
3339                    name, input_bfd, input_section, rel->r_offset);
3340                 return FALSE;
3341               }
3342
3343             if (RELAX_GOTTLSOFF_LOCAL_EXEC_P (info, picrel,
3344                                               relocation + rel->r_addend)
3345                 || (RELAX_TLSDESC_INITIAL_EXEC_P (info, picrel)
3346                     && IN_RANGE_FOR_OFST12_P (picrel->tlsoff_entry)))
3347               {
3348                 /* Replace sethi with a nop.  Preserve the packing bit.  */
3349                 insn &= (unsigned long)0x80000000;
3350                 insn |= (unsigned long)0x00880000;
3351                 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
3352
3353                 /* Nothing to relocate.  */
3354                 continue;
3355               }
3356
3357             break;
3358
3359           case R_FRV_GOTTLSOFFLO:
3360             insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
3361
3362             /* Is this a setlo or setlos instruction?  */
3363             if ((insn & (unsigned long)0x01f70000) != 0x00f40000)
3364               {
3365                 r = info->callbacks->warning
3366                   (info,
3367                    _("R_FRV_GOTTLSOFFLO"
3368                      " not applied to a setlo or setlos instruction"),
3369                    name, input_bfd, input_section, rel->r_offset);
3370                 return FALSE;
3371               }
3372
3373             if (RELAX_GOTTLSOFF_LOCAL_EXEC_P (info, picrel,
3374                                               relocation + rel->r_addend)
3375                 || (RELAX_TLSDESC_INITIAL_EXEC_P (info, picrel)
3376                     && IN_RANGE_FOR_OFST12_P (picrel->tlsoff_entry)))
3377               {
3378                 /* Replace setlo/setlos with a nop.  Preserve the
3379                    packing bit.  */
3380                 insn &= (unsigned long)0x80000000;
3381                 insn |= (unsigned long)0x00880000;
3382                 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
3383
3384                 /* Nothing to relocate.  */
3385                 continue;
3386               }
3387
3388             break;
3389
3390           case R_FRV_TLSOFF_RELAX:
3391             insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
3392
3393             /* Is this an ld instruction?  */
3394             if ((insn & (unsigned long)0x01fc0fc0) != 0x00080100)
3395               {
3396                 r = info->callbacks->warning
3397                   (info,
3398                    _("R_FRV_TLSOFF_RELAX not applied to an ld instruction"),
3399                    name, input_bfd, input_section, rel->r_offset);
3400                 return FALSE;
3401               }
3402
3403             if (RELAX_GOTTLSOFF_LOCAL_EXEC_P (info, picrel,
3404                                               relocation + rel->r_addend))
3405               {
3406                 /* Replace ld #gottlsoff(symbol+offset)@(grB, grA), grC
3407                    with setlos #tlsmofflo(symbol+offset), grC.
3408                    Preserve the packing bit.  */
3409                 insn &= (unsigned long)0xfe000000;
3410                 insn |= (unsigned long)0x00fc0000;
3411                 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
3412
3413                 r_type = R_FRV_TLSMOFFLO;
3414                 howto  = elf32_frv_howto_table + r_type;
3415                 rel->r_info = ELF32_R_INFO (r_symndx, r_type);
3416               }
3417
3418             else if (RELAX_TLSDESC_INITIAL_EXEC_P (info, picrel)
3419                      && IN_RANGE_FOR_OFST12_P (picrel->tlsoff_entry))
3420               {
3421                 /* Replace ld #tlsoff(symbol+offset)@(grB, grA), grC
3422                    with ldi @(grB, #gottlsoff12(symbol+offset), grC.
3423                    Preserve the packing bit.  */
3424                 insn = (insn & (unsigned long)0xfe03f000)
3425                   | (unsigned long)0x00c80000;;
3426                 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
3427
3428                 r_type = R_FRV_GOTTLSOFF12;
3429                 howto  = elf32_frv_howto_table + r_type;
3430                 rel->r_info = ELF32_R_INFO (r_symndx, r_type);
3431               }
3432
3433             break;
3434
3435           case R_FRV_TLSMOFFHI:
3436             insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
3437
3438             /* Is this a sethi instruction?  */
3439             if ((insn & (unsigned long)0x01ff0000) != 0x00f80000)
3440               {
3441                 r = info->callbacks->warning
3442                   (info,
3443                    _("R_FRV_TLSMOFFHI not applied to a sethi instruction"),
3444                    name, input_bfd, input_section, rel->r_offset);
3445                 return FALSE;
3446               }
3447
3448             if (TLSMOFF_IN_RANGE_FOR_SETLOS_P (relocation + rel->r_addend,
3449                                                info))
3450               {
3451                 /* Replace sethi with a nop.  Preserve the packing bit.  */
3452                 insn &= (unsigned long)0x80000000;
3453                 insn |= (unsigned long)0x00880000;
3454                 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
3455
3456                 /* Nothing to relocate.  */
3457                 continue;
3458               }
3459
3460             break;
3461
3462           case R_FRV_TLSMOFFLO:
3463             insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
3464
3465             /* Is this a setlo or setlos instruction?  */
3466             if ((insn & (unsigned long)0x01f70000) != 0x00f40000)
3467               {
3468                 r = info->callbacks->warning
3469                   (info,
3470                    _("R_FRV_TLSMOFFLO"
3471                      " not applied to a setlo or setlos instruction"),
3472                    name, input_bfd, input_section, rel->r_offset);
3473                 return FALSE;
3474               }
3475
3476             if (TLSMOFF_IN_RANGE_FOR_SETLOS_P (relocation + rel->r_addend,
3477                                                info))
3478               /* If the corresponding sethi (if it exists) decayed
3479                  to a nop, make sure this becomes (or already is) a
3480                  setlos, not setlo.  */
3481               {
3482                 insn |= (unsigned long)0x00080000;
3483                 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
3484               }
3485
3486             break;
3487
3488             /*
3489               There's nothing to relax in these:
3490                 R_FRV_TLSDESC_VALUE
3491                 R_FRV_TLSOFF
3492                 R_FRV_TLSMOFF12
3493                 R_FRV_TLSMOFFHI
3494                 R_FRV_TLSMOFFLO
3495                 R_FRV_TLSMOFF
3496             */
3497
3498           default:
3499             break;
3500           }
3501
3502       switch (r_type)
3503         {
3504         case R_FRV_LABEL24:
3505           check_segment[0] = isec_segment;
3506           if (! IS_FDPIC (output_bfd))
3507             check_segment[1] = isec_segment;
3508           else if (picrel->plt)
3509             {
3510               relocation = frvfdpic_plt_section (info)->output_section->vma
3511                 + frvfdpic_plt_section (info)->output_offset
3512                 + picrel->plt_entry;
3513               check_segment[1] = plt_segment;
3514             }
3515           /* We don't want to warn on calls to undefined weak symbols,
3516              as calls to them must be protected by non-NULL tests
3517              anyway, and unprotected calls would invoke undefined
3518              behavior.  */
3519           else if (picrel->symndx == -1
3520                    && picrel->d.h->root.type == bfd_link_hash_undefweak)
3521             check_segment[1] = check_segment[0];
3522           else
3523             check_segment[1] = sec
3524               ? _frvfdpic_osec_to_segment (output_bfd, sec->output_section)
3525               : (unsigned)-1;
3526           break;
3527
3528         case R_FRV_GOT12:
3529         case R_FRV_GOTHI:
3530         case R_FRV_GOTLO:
3531           relocation = picrel->got_entry;
3532           check_segment[0] = check_segment[1] = got_segment;
3533           break;
3534
3535         case R_FRV_FUNCDESC_GOT12:
3536         case R_FRV_FUNCDESC_GOTHI:
3537         case R_FRV_FUNCDESC_GOTLO:
3538           relocation = picrel->fdgot_entry;
3539           check_segment[0] = check_segment[1] = got_segment;
3540           break;
3541
3542         case R_FRV_GOTOFFHI:
3543         case R_FRV_GOTOFF12:
3544         case R_FRV_GOTOFFLO:
3545           relocation -= frvfdpic_got_section (info)->output_section->vma
3546             + frvfdpic_got_section (info)->output_offset
3547             + frvfdpic_got_initial_offset (info);
3548           check_segment[0] = got_segment;
3549           check_segment[1] = sec
3550             ? _frvfdpic_osec_to_segment (output_bfd, sec->output_section)
3551             : (unsigned)-1;
3552           break;
3553
3554         case R_FRV_FUNCDESC_GOTOFF12:
3555         case R_FRV_FUNCDESC_GOTOFFHI:
3556         case R_FRV_FUNCDESC_GOTOFFLO:
3557           relocation = picrel->fd_entry;
3558           check_segment[0] = check_segment[1] = got_segment;
3559           break;
3560
3561         case R_FRV_FUNCDESC:
3562           {
3563             int dynindx;
3564             bfd_vma addend = rel->r_addend;
3565
3566             if (! (h && h->root.type == bfd_link_hash_undefweak
3567                    && FRVFDPIC_SYM_LOCAL (info, h)))
3568               {
3569                 /* If the symbol is dynamic and there may be dynamic
3570                    symbol resolution because we are or are linked with a
3571                    shared library, emit a FUNCDESC relocation such that
3572                    the dynamic linker will allocate the function
3573                    descriptor.  If the symbol needs a non-local function
3574                    descriptor but binds locally (e.g., its visibility is
3575                    protected, emit a dynamic relocation decayed to
3576                    section+offset.  */
3577                 if (h && ! FRVFDPIC_FUNCDESC_LOCAL (info, h)
3578                     && FRVFDPIC_SYM_LOCAL (info, h)
3579                     && !(info->executable && !info->pie))
3580                   {
3581                     dynindx = elf_section_data (h->root.u.def.section
3582                                                 ->output_section)->dynindx;
3583                     addend += h->root.u.def.section->output_offset
3584                       + h->root.u.def.value;
3585                   }
3586                 else if (h && ! FRVFDPIC_FUNCDESC_LOCAL (info, h))
3587                   {
3588                     if (addend)
3589                       {
3590                         info->callbacks->warning
3591                           (info, _("R_FRV_FUNCDESC references dynamic symbol with nonzero addend"),
3592                            name, input_bfd, input_section, rel->r_offset);
3593                         return FALSE;
3594                       }
3595                     dynindx = h->dynindx;
3596                   }
3597                 else
3598                   {
3599                     /* Otherwise, we know we have a private function
3600                        descriptor, so reference it directly.  */
3601                     BFD_ASSERT (picrel->privfd);
3602                     r_type = R_FRV_32;
3603                     dynindx = elf_section_data (frvfdpic_got_section (info)
3604                                                 ->output_section)->dynindx;
3605                     addend = frvfdpic_got_section (info)->output_offset
3606                       + frvfdpic_got_initial_offset (info)
3607                       + picrel->fd_entry;
3608                   }
3609
3610                 /* If there is room for dynamic symbol resolution, emit
3611                    the dynamic relocation.  However, if we're linking an
3612                    executable at a fixed location, we won't have emitted a
3613                    dynamic symbol entry for the got section, so idx will
3614                    be zero, which means we can and should compute the
3615                    address of the private descriptor ourselves.  */
3616                 if (info->executable && !info->pie
3617                     && (!h || FRVFDPIC_FUNCDESC_LOCAL (info, h)))
3618                   {
3619                     addend += frvfdpic_got_section (info)->output_section->vma;
3620                     if ((bfd_get_section_flags (output_bfd,
3621                                                 input_section->output_section)
3622                          & (SEC_ALLOC | SEC_LOAD)) == (SEC_ALLOC | SEC_LOAD))
3623                       {
3624                         if (_frvfdpic_osec_readonly_p (output_bfd,
3625                                                        input_section
3626                                                        ->output_section))
3627                           {
3628                             info->callbacks->warning
3629                               (info,
3630                                _("cannot emit fixups in read-only section"),
3631                                name, input_bfd, input_section, rel->r_offset);
3632                             return FALSE;
3633                           }
3634                         _frvfdpic_add_rofixup (output_bfd,
3635                                                frvfdpic_gotfixup_section
3636                                                (info),
3637                                                _bfd_elf_section_offset
3638                                                (output_bfd, info,
3639                                                 input_section, rel->r_offset)
3640                                                + input_section
3641                                                ->output_section->vma
3642                                                + input_section->output_offset,
3643                                                picrel);
3644                       }
3645                   }
3646                 else if ((bfd_get_section_flags (output_bfd,
3647                                                  input_section->output_section)
3648                           & (SEC_ALLOC | SEC_LOAD)) == (SEC_ALLOC | SEC_LOAD))
3649                   {
3650                     if (_frvfdpic_osec_readonly_p (output_bfd,
3651                                                    input_section
3652                                                    ->output_section))
3653                       {
3654                         info->callbacks->warning
3655                           (info,
3656                            _("cannot emit dynamic relocations in read-only section"),
3657                            name, input_bfd, input_section, rel->r_offset);
3658                         return FALSE;
3659                       }
3660                     _frvfdpic_add_dyn_reloc (output_bfd,
3661                                              frvfdpic_gotrel_section (info),
3662                                              _bfd_elf_section_offset
3663                                              (output_bfd, info,
3664                                               input_section, rel->r_offset)
3665                                              + input_section
3666                                              ->output_section->vma
3667                                              + input_section->output_offset,
3668                                              r_type, dynindx, addend, picrel);
3669                   }
3670                 else
3671                   addend += frvfdpic_got_section (info)->output_section->vma;
3672               }
3673
3674             /* We want the addend in-place because dynamic
3675                relocations are REL.  Setting relocation to it should
3676                arrange for it to be installed.  */
3677             relocation = addend - rel->r_addend;
3678           }
3679           check_segment[0] = check_segment[1] = got_segment;
3680           break;
3681
3682         case R_FRV_32:
3683           if (! IS_FDPIC (output_bfd))
3684             {
3685               check_segment[0] = check_segment[1] = -1;
3686               break;
3687             }
3688           /* Fall through.  */
3689         case R_FRV_FUNCDESC_VALUE:
3690           {
3691             int dynindx;
3692             bfd_vma addend = rel->r_addend;
3693
3694             /* If the symbol is dynamic but binds locally, use
3695                section+offset.  */
3696             if (h && ! FRVFDPIC_SYM_LOCAL (info, h))
3697               {
3698                 if (addend && r_type == R_FRV_FUNCDESC_VALUE)
3699                   {
3700                     info->callbacks->warning
3701                       (info, _("R_FRV_FUNCDESC_VALUE references dynamic symbol with nonzero addend"),
3702                        name, input_bfd, input_section, rel->r_offset);
3703                     return FALSE;
3704                   }
3705                 dynindx = h->dynindx;
3706               }
3707             else
3708               {
3709                 if (h)
3710                   addend += h->root.u.def.value;
3711                 else
3712                   addend += sym->st_value;
3713                 if (osec)
3714                   addend += osec->output_offset;
3715                 if (osec && osec->output_section
3716                     && ! bfd_is_abs_section (osec->output_section)
3717                     && ! bfd_is_und_section (osec->output_section))
3718                   dynindx = elf_section_data (osec->output_section)->dynindx;
3719                 else
3720                   dynindx = 0;
3721               }
3722
3723             /* If we're linking an executable at a fixed address, we
3724                can omit the dynamic relocation as long as the symbol
3725                is defined in the current link unit (which is implied
3726                by its output section not being NULL).  */
3727             if (info->executable && !info->pie
3728                 && (!h || FRVFDPIC_SYM_LOCAL (info, h)))
3729               {
3730                 if (osec)
3731                   addend += osec->output_section->vma;
3732                 if (IS_FDPIC (input_bfd)
3733                     && (bfd_get_section_flags (output_bfd,
3734                                                input_section->output_section)
3735                         & (SEC_ALLOC | SEC_LOAD)) == (SEC_ALLOC | SEC_LOAD))
3736                   {
3737                     if (_frvfdpic_osec_readonly_p (output_bfd,
3738                                                    input_section
3739                                                    ->output_section))
3740                       {
3741                         info->callbacks->warning
3742                           (info,
3743                            _("cannot emit fixups in read-only section"),
3744                            name, input_bfd, input_section, rel->r_offset);
3745                         return FALSE;
3746                       }
3747                     if (!h || h->root.type != bfd_link_hash_undefweak)
3748                       {
3749                         _frvfdpic_add_rofixup (output_bfd,
3750                                                frvfdpic_gotfixup_section
3751                                                (info),
3752                                                _bfd_elf_section_offset
3753                                                (output_bfd, info,
3754                                                 input_section, rel->r_offset)
3755                                                + input_section
3756                                                ->output_section->vma
3757                                                + input_section->output_offset,
3758                                                picrel);
3759                         if (r_type == R_FRV_FUNCDESC_VALUE)
3760                           _frvfdpic_add_rofixup
3761                             (output_bfd,
3762                              frvfdpic_gotfixup_section (info),
3763                              _bfd_elf_section_offset
3764                              (output_bfd, info,
3765                               input_section, rel->r_offset)
3766                              + input_section->output_section->vma
3767                              + input_section->output_offset + 4, picrel);
3768                       }
3769                   }
3770               }
3771             else
3772               {
3773                 if ((bfd_get_section_flags (output_bfd,
3774                                             input_section->output_section)
3775                      & (SEC_ALLOC | SEC_LOAD)) == (SEC_ALLOC | SEC_LOAD))
3776                   {
3777                     if (_frvfdpic_osec_readonly_p (output_bfd,
3778                                                    input_section
3779                                                    ->output_section))
3780                       {
3781                         info->callbacks->warning
3782                           (info,
3783                            _("cannot emit dynamic relocations in read-only section"),
3784                            name, input_bfd, input_section, rel->r_offset);
3785                         return FALSE;
3786                       }
3787                     _frvfdpic_add_dyn_reloc (output_bfd,
3788                                              frvfdpic_gotrel_section (info),
3789                                              _bfd_elf_section_offset
3790                                              (output_bfd, info,
3791                                               input_section, rel->r_offset)
3792                                              + input_section
3793                                              ->output_section->vma
3794                                              + input_section->output_offset,
3795                                              r_type, dynindx, addend, picrel);
3796                   }
3797                 else if (osec)
3798                   addend += osec->output_section->vma;
3799                 /* We want the addend in-place because dynamic
3800                    relocations are REL.  Setting relocation to it
3801                    should arrange for it to be installed.  */
3802                 relocation = addend - rel->r_addend;
3803               }
3804
3805             if (r_type == R_FRV_FUNCDESC_VALUE)
3806               {
3807                 /* If we've omitted the dynamic relocation, just emit
3808                    the fixed addresses of the symbol and of the local
3809                    GOT base offset.  */
3810                 if (info->executable && !info->pie
3811                     && (!h || FRVFDPIC_SYM_LOCAL (info, h)))
3812                   bfd_put_32 (output_bfd,
3813                               frvfdpic_got_section (info)->output_section->vma
3814                               + frvfdpic_got_section (info)->output_offset
3815                               + frvfdpic_got_initial_offset (info),
3816                               contents + rel->r_offset + 4);
3817                 else
3818                   /* A function descriptor used for lazy or local
3819                      resolving is initialized such that its high word
3820                      contains the output section index in which the
3821                      PLT entries are located, and the low word
3822                      contains the offset of the lazy PLT entry entry
3823                      point into that section.  */
3824                   bfd_put_32 (output_bfd,
3825                               h && ! FRVFDPIC_SYM_LOCAL (info, h)
3826                               ? 0
3827                               : _frvfdpic_osec_to_segment (output_bfd,
3828                                                            sec
3829                                                            ->output_section),
3830                               contents + rel->r_offset + 4);
3831               }
3832           }
3833           check_segment[0] = check_segment[1] = got_segment;
3834           break;
3835
3836         case R_FRV_GPREL12:
3837         case R_FRV_GPRELU12:
3838         case R_FRV_GPREL32:
3839         case R_FRV_GPRELHI:
3840         case R_FRV_GPRELLO:
3841           check_segment[0] = gprel_segment;
3842           check_segment[1] = sec
3843             ? _frvfdpic_osec_to_segment (output_bfd, sec->output_section)
3844             : (unsigned)-1;
3845           break;
3846
3847         case R_FRV_GETTLSOFF:
3848           relocation = frvfdpic_plt_section (info)->output_section->vma
3849             + frvfdpic_plt_section (info)->output_offset
3850             + picrel->tlsplt_entry;
3851           BFD_ASSERT (picrel->tlsplt_entry != (bfd_vma)-1
3852                       && picrel->tlsdesc_entry);
3853           check_segment[0] = isec_segment;
3854           check_segment[1] = plt_segment;
3855           break;
3856
3857         case R_FRV_GOTTLSDESC12:
3858         case R_FRV_GOTTLSDESCHI:
3859         case R_FRV_GOTTLSDESCLO:
3860           BFD_ASSERT (picrel->tlsdesc_entry);
3861           relocation = picrel->tlsdesc_entry;
3862           check_segment[0] = tls_segment;
3863           check_segment[1] = sec
3864             && ! bfd_is_abs_section (sec)
3865             && ! bfd_is_und_section (sec)
3866             ? _frvfdpic_osec_to_segment (output_bfd, sec->output_section)
3867             : tls_segment;
3868           break;
3869
3870         case R_FRV_TLSMOFF12:
3871         case R_FRV_TLSMOFFHI:
3872         case R_FRV_TLSMOFFLO:
3873         case R_FRV_TLSMOFF:
3874           check_segment[0] = tls_segment;
3875           if (! sec)
3876             check_segment[1] = -1;
3877           else if (bfd_is_abs_section (sec)
3878                    || bfd_is_und_section (sec))
3879             {
3880               relocation = 0;
3881               check_segment[1] = tls_segment;
3882             }
3883           else if (sec->output_section)
3884             {
3885               relocation -= tls_biased_base (info);
3886               check_segment[1] =
3887                 _frvfdpic_osec_to_segment (output_bfd, sec->output_section);
3888             }
3889           break;
3890
3891         case R_FRV_GOTTLSOFF12:
3892         case R_FRV_GOTTLSOFFHI:
3893         case R_FRV_GOTTLSOFFLO:
3894           BFD_ASSERT (picrel->tlsoff_entry);
3895           relocation = picrel->tlsoff_entry;
3896           check_segment[0] = tls_segment;
3897           check_segment[1] = sec
3898             && ! bfd_is_abs_section (sec)
3899             && ! bfd_is_und_section (sec)
3900             ? _frvfdpic_osec_to_segment (output_bfd, sec->output_section)
3901             : tls_segment;
3902           break;
3903           
3904         case R_FRV_TLSDESC_VALUE:
3905         case R_FRV_TLSOFF:
3906           /* These shouldn't be present in input object files.  */
3907           check_segment[0] = check_segment[1] = isec_segment;
3908           break;
3909
3910         case R_FRV_TLSDESC_RELAX:
3911         case R_FRV_GETTLSOFF_RELAX:
3912         case R_FRV_TLSOFF_RELAX:
3913           /* These are just annotations for relaxation, nothing to do
3914              here.  */
3915           continue;
3916
3917         default:
3918           check_segment[0] = isec_segment;
3919           check_segment[1] = sec
3920             ? _frvfdpic_osec_to_segment (output_bfd, sec->output_section)
3921             : (unsigned)-1;
3922           break;
3923         }
3924
3925       if (check_segment[0] != check_segment[1] && IS_FDPIC (output_bfd))
3926         {
3927           /* If you take this out, remove the #error from fdpic-static-6.d
3928              in the ld testsuite.  */
3929           /* This helps catch problems in GCC while we can't do more
3930              than static linking.  The idea is to test whether the
3931              input file basename is crt0.o only once.  */
3932           if (silence_segment_error == 1)
3933             silence_segment_error =
3934               (strlen (input_bfd->filename) == 6
3935                && strcmp (input_bfd->filename, "crt0.o") == 0)
3936               || (strlen (input_bfd->filename) > 6
3937                   && strcmp (input_bfd->filename
3938                              + strlen (input_bfd->filename) - 7,
3939                              "/crt0.o") == 0)
3940               ? -1 : 0;
3941           if (!silence_segment_error
3942               /* We don't want duplicate errors for undefined
3943                  symbols.  */
3944               && !(picrel && picrel->symndx == -1
3945                    && picrel->d.h->root.type == bfd_link_hash_undefined))
3946             info->callbacks->warning
3947               (info,
3948                (info->shared || info->pie)
3949                ? _("relocations between different segments are not supported")
3950                : _("warning: relocation references a different segment"),
3951                name, input_bfd, input_section, rel->r_offset);
3952           if (!silence_segment_error && (info->shared || info->pie))
3953             return FALSE;
3954           elf_elfheader (output_bfd)->e_flags |= EF_FRV_PIC;
3955         }
3956
3957       switch (r_type)
3958         {
3959         case R_FRV_GOTOFFHI:
3960         case R_FRV_TLSMOFFHI:
3961           /* We need the addend to be applied before we shift the
3962              value right.  */
3963           relocation += rel->r_addend;
3964           /* Fall through.  */
3965         case R_FRV_GOTHI:
3966         case R_FRV_FUNCDESC_GOTHI:
3967         case R_FRV_FUNCDESC_GOTOFFHI:
3968         case R_FRV_GOTTLSOFFHI:
3969         case R_FRV_GOTTLSDESCHI:
3970           relocation >>= 16;
3971           /* Fall through.  */
3972
3973         case R_FRV_GOTLO:
3974         case R_FRV_FUNCDESC_GOTLO:
3975         case R_FRV_GOTOFFLO:
3976         case R_FRV_FUNCDESC_GOTOFFLO:
3977         case R_FRV_GOTTLSOFFLO:
3978         case R_FRV_GOTTLSDESCLO:
3979         case R_FRV_TLSMOFFLO:
3980           relocation &= 0xffff;
3981           break;
3982
3983         default:
3984           break;
3985         }
3986
3987       switch (r_type)
3988         {
3989         case R_FRV_LABEL24:
3990           if (! IS_FDPIC (output_bfd) || ! picrel->plt)
3991             break;
3992           /* Fall through.  */
3993
3994           /* When referencing a GOT entry, a function descriptor or a
3995              PLT, we don't want the addend to apply to the reference,
3996              but rather to the referenced symbol.  The actual entry
3997              will have already been created taking the addend into
3998              account, so cancel it out here.  */
3999         case R_FRV_GOT12:
4000         case R_FRV_GOTHI:
4001         case R_FRV_GOTLO:
4002         case R_FRV_FUNCDESC_GOT12:
4003         case R_FRV_FUNCDESC_GOTHI:
4004         case R_FRV_FUNCDESC_GOTLO:
4005         case R_FRV_FUNCDESC_GOTOFF12:
4006         case R_FRV_FUNCDESC_GOTOFFHI:
4007         case R_FRV_FUNCDESC_GOTOFFLO:
4008         case R_FRV_GETTLSOFF:
4009         case R_FRV_GOTTLSDESC12:
4010         case R_FRV_GOTTLSDESCHI:
4011         case R_FRV_GOTTLSDESCLO:
4012         case R_FRV_GOTTLSOFF12:
4013         case R_FRV_GOTTLSOFFHI:
4014         case R_FRV_GOTTLSOFFLO:
4015           /* Note that we only want GOTOFFHI, not GOTOFFLO or GOTOFF12
4016              here, since we do want to apply the addend to the others.
4017              Note that we've applied the addend to GOTOFFHI before we
4018              shifted it right.  */
4019         case R_FRV_GOTOFFHI:
4020         case R_FRV_TLSMOFFHI:
4021           relocation -= rel->r_addend;
4022           break;
4023
4024         default:
4025           break;
4026         }
4027
4028      if (r_type == R_FRV_HI16)
4029        r = elf32_frv_relocate_hi16 (input_bfd, rel, contents, relocation);
4030
4031      else if (r_type == R_FRV_LO16)
4032        r = elf32_frv_relocate_lo16 (input_bfd, rel, contents, relocation);
4033
4034      else if (r_type == R_FRV_LABEL24 || r_type == R_FRV_GETTLSOFF)
4035        r = elf32_frv_relocate_label24 (input_bfd, input_section, rel,
4036                                        contents, relocation);
4037
4038      else if (r_type == R_FRV_GPREL12)
4039        r = elf32_frv_relocate_gprel12 (info, input_bfd, input_section, rel,
4040                                        contents, relocation);
4041
4042      else if (r_type == R_FRV_GPRELU12)
4043        r = elf32_frv_relocate_gprelu12 (info, input_bfd, input_section, rel,
4044                                         contents, relocation);
4045
4046      else if (r_type == R_FRV_GPRELLO)
4047        r = elf32_frv_relocate_gprello (info, input_bfd, input_section, rel,
4048                                        contents, relocation);
4049
4050      else if (r_type == R_FRV_GPRELHI)
4051        r = elf32_frv_relocate_gprelhi (info, input_bfd, input_section, rel,
4052                                        contents, relocation);
4053
4054      else if (r_type == R_FRV_TLSOFF
4055               || r_type == R_FRV_TLSDESC_VALUE)
4056        r = bfd_reloc_notsupported;
4057
4058      else
4059        r = frv_final_link_relocate (howto, input_bfd, input_section, contents,
4060                                     rel, relocation);
4061
4062       if (r != bfd_reloc_ok)
4063         {
4064           const char * msg = (const char *) NULL;
4065
4066           switch (r)
4067             {
4068             case bfd_reloc_overflow:
4069               r = info->callbacks->reloc_overflow
4070                 (info, (h ? &h->root : NULL), name, howto->name,
4071                  (bfd_vma) 0, input_bfd, input_section, rel->r_offset);
4072               break;
4073
4074             case bfd_reloc_undefined:
4075               r = info->callbacks->undefined_symbol
4076                 (info, name, input_bfd, input_section, rel->r_offset, TRUE);
4077               break;
4078
4079             case bfd_reloc_outofrange:
4080               msg = _("internal error: out of range error");
4081               break;
4082
4083             case bfd_reloc_notsupported:
4084               msg = _("internal error: unsupported relocation error");
4085               break;
4086
4087             case bfd_reloc_dangerous:
4088               msg = _("internal error: dangerous relocation");
4089               break;
4090
4091             default:
4092               msg = _("internal error: unknown error");
4093               break;
4094             }
4095
4096           if (msg)
4097             r = info->callbacks->warning
4098               (info, msg, name, input_bfd, input_section, rel->r_offset);
4099
4100           if (! r)
4101             return FALSE;
4102         }
4103     }
4104
4105   return TRUE;
4106 }
4107 \f
4108 /* Return the section that should be marked against GC for a given
4109    relocation.  */
4110
4111 static asection *
4112 elf32_frv_gc_mark_hook (sec, info, rel, h, sym)
4113      asection *sec;
4114      struct bfd_link_info *info ATTRIBUTE_UNUSED;
4115      Elf_Internal_Rela *rel;
4116      struct elf_link_hash_entry *h;
4117      Elf_Internal_Sym *sym;
4118 {
4119   if (h != NULL)
4120     {
4121       switch (ELF32_R_TYPE (rel->r_info))
4122         {
4123         case R_FRV_GNU_VTINHERIT:
4124         case R_FRV_GNU_VTENTRY:
4125           break;
4126
4127         default:
4128           switch (h->root.type)
4129             {
4130             default:
4131               break;
4132
4133             case bfd_link_hash_defined:
4134             case bfd_link_hash_defweak:
4135               return h->root.u.def.section;
4136
4137             case bfd_link_hash_common:
4138               return h->root.u.c.p->section;
4139             }
4140         }
4141     }
4142   else
4143     return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
4144
4145   return NULL;
4146 }
4147
4148 /* Update the got entry reference counts for the section being removed.  */
4149
4150 static bfd_boolean
4151 elf32_frv_gc_sweep_hook (abfd, info, sec, relocs)
4152      bfd *abfd ATTRIBUTE_UNUSED;
4153      struct bfd_link_info *info ATTRIBUTE_UNUSED;
4154      asection *sec ATTRIBUTE_UNUSED;
4155      const Elf_Internal_Rela *relocs ATTRIBUTE_UNUSED;
4156 {
4157   return TRUE;
4158 }
4159
4160 \f
4161 /* Hook called by the linker routine which adds symbols from an object
4162    file.  We use it to put .comm items in .scomm, and not .comm.  */
4163
4164 static bfd_boolean
4165 elf32_frv_add_symbol_hook (abfd, info, sym, namep, flagsp, secp, valp)
4166      bfd *abfd;
4167      struct bfd_link_info *info;
4168      Elf_Internal_Sym *sym;
4169      const char **namep ATTRIBUTE_UNUSED;
4170      flagword *flagsp ATTRIBUTE_UNUSED;
4171      asection **secp;
4172      bfd_vma *valp;
4173 {
4174   if (sym->st_shndx == SHN_COMMON
4175       && !info->relocatable
4176       && (int)sym->st_size <= (int)bfd_get_gp_size (abfd))
4177     {
4178       /* Common symbols less than or equal to -G nn bytes are
4179          automatically put into .sbss.  */
4180
4181       asection *scomm = bfd_get_section_by_name (abfd, ".scommon");
4182
4183       if (scomm == NULL)
4184         {
4185           scomm = bfd_make_section (abfd, ".scommon");
4186           if (scomm == NULL
4187               || !bfd_set_section_flags (abfd, scomm, (SEC_ALLOC
4188                                                        | SEC_IS_COMMON
4189                                                        | SEC_LINKER_CREATED)))
4190             return FALSE;
4191         }
4192
4193       *secp = scomm;
4194       *valp = sym->st_size;
4195     }
4196
4197   return TRUE;
4198 }
4199
4200 /* We need dynamic symbols for every section, since segments can
4201    relocate independently.  */
4202 static bfd_boolean
4203 _frvfdpic_link_omit_section_dynsym (bfd *output_bfd ATTRIBUTE_UNUSED,
4204                                     struct bfd_link_info *info
4205                                     ATTRIBUTE_UNUSED,
4206                                     asection *p ATTRIBUTE_UNUSED)
4207 {
4208   switch (elf_section_data (p)->this_hdr.sh_type)
4209     {
4210     case SHT_PROGBITS:
4211     case SHT_NOBITS:
4212       /* If sh_type is yet undecided, assume it could be
4213          SHT_PROGBITS/SHT_NOBITS.  */
4214     case SHT_NULL:
4215       return FALSE;
4216
4217       /* There shouldn't be section relative relocations
4218          against any other section.  */
4219     default:
4220       return TRUE;
4221     }
4222 }
4223
4224 /* Create  a .got section, as well as its additional info field.  This
4225    is almost entirely copied from
4226    elflink.c:_bfd_elf_create_got_section().  */
4227
4228 static bfd_boolean
4229 _frv_create_got_section (bfd *abfd, struct bfd_link_info *info)
4230 {
4231   flagword flags, pltflags;
4232   asection *s;
4233   struct elf_link_hash_entry *h;
4234   struct bfd_link_hash_entry *bh;
4235   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4236   int ptralign;
4237   int offset;
4238
4239   /* This function may be called more than once.  */
4240   s = bfd_get_section_by_name (abfd, ".got");
4241   if (s != NULL && (s->flags & SEC_LINKER_CREATED) != 0)
4242     return TRUE;
4243
4244   /* Machine specific: although pointers are 32-bits wide, we want the
4245      GOT to be aligned to a 64-bit boundary, such that function
4246      descriptors in it can be accessed with 64-bit loads and
4247      stores.  */
4248   ptralign = 3;
4249
4250   flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
4251            | SEC_LINKER_CREATED);
4252   pltflags = flags;
4253
4254   s = bfd_make_section (abfd, ".got");
4255   if (s == NULL
4256       || !bfd_set_section_flags (abfd, s, flags)
4257       || !bfd_set_section_alignment (abfd, s, ptralign))
4258     return FALSE;
4259
4260   if (bed->want_got_plt)
4261     {
4262       s = bfd_make_section (abfd, ".got.plt");
4263       if (s == NULL
4264           || !bfd_set_section_flags (abfd, s, flags)
4265           || !bfd_set_section_alignment (abfd, s, ptralign))
4266         return FALSE;
4267     }
4268
4269   if (bed->want_got_sym)
4270     {
4271       /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
4272          (or .got.plt) section.  We don't do this in the linker script
4273          because we don't want to define the symbol if we are not creating
4274          a global offset table.  */
4275       bh = NULL;
4276       if (!(_bfd_generic_link_add_one_symbol
4277             (info, abfd, "_GLOBAL_OFFSET_TABLE_", BSF_GLOBAL, s,
4278              bed->got_symbol_offset, (const char *) NULL, FALSE,
4279              bed->collect, &bh)))
4280         return FALSE;
4281       h = (struct elf_link_hash_entry *) bh;
4282       h->def_regular = 1;
4283       h->type = STT_OBJECT;
4284       /* h->other = STV_HIDDEN; */ /* Should we?  */
4285
4286       /* Machine-specific: we want the symbol for executables as
4287          well.  */
4288       if (! bfd_elf_link_record_dynamic_symbol (info, h))
4289         return FALSE;
4290
4291       elf_hash_table (info)->hgot = h;
4292     }
4293
4294   /* The first bit of the global offset table is the header.  */
4295   s->size += bed->got_header_size + bed->got_symbol_offset;
4296
4297   /* This is the machine-specific part.  Create and initialize section
4298      data for the got.  */
4299   if (IS_FDPIC (abfd))
4300     {
4301       frvfdpic_got_section (info) = s;
4302       frvfdpic_relocs_info (info) = htab_try_create (1,
4303                                                      frvfdpic_relocs_info_hash,
4304                                                      frvfdpic_relocs_info_eq,
4305                                                      (htab_del) NULL);
4306       if (! frvfdpic_relocs_info (info))
4307         return FALSE;
4308
4309       s = bfd_make_section (abfd, ".rel.got");
4310       if (s == NULL
4311           || ! bfd_set_section_flags (abfd, s, (flags | SEC_READONLY))
4312           || ! bfd_set_section_alignment (abfd, s, 2))
4313         return FALSE;
4314
4315       frvfdpic_gotrel_section (info) = s;
4316
4317       /* Machine-specific.  */
4318       s = bfd_make_section (abfd, ".rofixup");
4319       if (s == NULL
4320           || ! bfd_set_section_flags (abfd, s, (flags | SEC_READONLY))
4321           || ! bfd_set_section_alignment (abfd, s, 2))
4322         return FALSE;
4323
4324       frvfdpic_gotfixup_section (info) = s;
4325       offset = -2048;
4326       flags = BSF_GLOBAL;
4327     }
4328   else
4329     {
4330       offset = 2048;
4331       flags = BSF_GLOBAL | BSF_WEAK;
4332     }
4333
4334   /* Define _gp in .rofixup, for FDPIC, or .got otherwise.  If it
4335      turns out that we're linking with a different linker script, the
4336      linker script will override it.  */
4337   bh = NULL;
4338   if (!(_bfd_generic_link_add_one_symbol
4339         (info, abfd, "_gp", flags, s, offset, (const char *) NULL, FALSE,
4340          bed->collect, &bh)))
4341     return FALSE;
4342   h = (struct elf_link_hash_entry *) bh;
4343   h->def_regular = 1;
4344   h->type = STT_OBJECT;
4345   /* h->other = STV_HIDDEN; */ /* Should we?  */
4346
4347   /* Machine-specific: we want the symbol for executables as well.  */
4348   if (IS_FDPIC (abfd) && ! bfd_elf_link_record_dynamic_symbol (info, h))
4349     return FALSE;
4350
4351   if (!IS_FDPIC (abfd))
4352     return TRUE;
4353
4354   /* FDPIC supports Thread Local Storage, and this may require a
4355      procedure linkage table for TLS PLT entries.  */
4356
4357   /* This is mostly copied from
4358      elflink.c:_bfd_elf_create_dynamic_sections().  */
4359
4360   flags = pltflags;
4361   pltflags |= SEC_CODE;
4362   if (bed->plt_not_loaded)
4363     pltflags &= ~ (SEC_CODE | SEC_LOAD | SEC_HAS_CONTENTS);
4364   if (bed->plt_readonly)
4365     pltflags |= SEC_READONLY;
4366
4367   s = bfd_make_section (abfd, ".plt");
4368   if (s == NULL
4369       || ! bfd_set_section_flags (abfd, s, pltflags)
4370       || ! bfd_set_section_alignment (abfd, s, bed->plt_alignment))
4371     return FALSE;
4372   /* FRV-specific: remember it.  */
4373   frvfdpic_plt_section (info) = s;
4374
4375   if (bed->want_plt_sym)
4376     {
4377       /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
4378          .plt section.  */
4379       struct elf_link_hash_entry *h;
4380       struct bfd_link_hash_entry *bh = NULL;
4381
4382       if (! (_bfd_generic_link_add_one_symbol
4383              (info, abfd, "_PROCEDURE_LINKAGE_TABLE_", BSF_GLOBAL, s, 0, NULL,
4384               FALSE, get_elf_backend_data (abfd)->collect, &bh)))
4385         return FALSE;
4386       h = (struct elf_link_hash_entry *) bh;
4387       h->def_regular = 1;
4388       h->type = STT_OBJECT;
4389       /* h->other = STV_HIDDEN; */ /* Should we?  */
4390
4391       if (! info->executable
4392           && ! bfd_elf_link_record_dynamic_symbol (info, h))
4393         return FALSE;
4394     }
4395
4396   /* FRV-specific: we want rel relocations for the plt.  */
4397   s = bfd_make_section (abfd, ".rel.plt");
4398   if (s == NULL
4399       || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
4400       || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
4401     return FALSE;
4402   /* FRV-specific: remember it.  */
4403   frvfdpic_pltrel_section (info) = s;
4404
4405   return TRUE;
4406 }
4407
4408 /* Make sure the got and plt sections exist, and that our pointers in
4409    the link hash table point to them.  */
4410
4411 static bfd_boolean
4412 elf32_frvfdpic_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
4413 {
4414   /* This is mostly copied from
4415      elflink.c:_bfd_elf_create_dynamic_sections().  */
4416   flagword flags;
4417   asection *s;
4418   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4419
4420   flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
4421            | SEC_LINKER_CREATED);
4422
4423   /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
4424      .rel[a].bss sections.  */
4425
4426   /* FRV-specific: we want to create the GOT and the PLT in the FRV
4427      way.  */
4428   if (! _frv_create_got_section (abfd, info))
4429     return FALSE;
4430
4431   /* FRV-specific: make sure we created everything we wanted.  */
4432   BFD_ASSERT (frvfdpic_got_section (info) && frvfdpic_gotrel_section (info)
4433               && frvfdpic_gotfixup_section (info)
4434               && frvfdpic_plt_section (info)
4435               && frvfdpic_pltrel_section (info));
4436
4437   if (bed->want_dynbss)
4438     {
4439       /* The .dynbss section is a place to put symbols which are defined
4440          by dynamic objects, are referenced by regular objects, and are
4441          not functions.  We must allocate space for them in the process
4442          image and use a R_*_COPY reloc to tell the dynamic linker to
4443          initialize them at run time.  The linker script puts the .dynbss
4444          section into the .bss section of the final image.  */
4445       s = bfd_make_section (abfd, ".dynbss");
4446       if (s == NULL
4447           || ! bfd_set_section_flags (abfd, s, SEC_ALLOC | SEC_LINKER_CREATED))
4448         return FALSE;
4449
4450       /* The .rel[a].bss section holds copy relocs.  This section is not
4451      normally needed.  We need to create it here, though, so that the
4452      linker will map it to an output section.  We can't just create it
4453      only if we need it, because we will not know whether we need it
4454      until we have seen all the input files, and the first time the
4455      main linker code calls BFD after examining all the input files
4456      (size_dynamic_sections) the input sections have already been
4457      mapped to the output sections.  If the section turns out not to
4458      be needed, we can discard it later.  We will never need this
4459      section when generating a shared object, since they do not use
4460      copy relocs.  */
4461       if (! info->shared)
4462         {
4463           s = bfd_make_section (abfd,
4464                                 (bed->default_use_rela_p
4465                                  ? ".rela.bss" : ".rel.bss"));
4466           if (s == NULL
4467               || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
4468               || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
4469             return FALSE;
4470         }
4471     }
4472
4473   return TRUE;
4474 }
4475
4476 /* Compute the total GOT and PLT size required by each symbol in each
4477    range.  Symbols may require up to 4 words in the GOT: an entry
4478    pointing to the symbol, an entry pointing to its function
4479    descriptor, and a private function descriptors taking two
4480    words.  */
4481
4482 static void
4483 _frvfdpic_count_nontls_entries (struct frvfdpic_relocs_info *entry,
4484                                 struct _frvfdpic_dynamic_got_info *dinfo)
4485 {
4486   /* Allocate space for a GOT entry pointing to the symbol.  */
4487   if (entry->got12)
4488     dinfo->got12 += 4;
4489   else if (entry->gotlos)
4490     dinfo->gotlos += 4;
4491   else if (entry->gothilo)
4492     dinfo->gothilo += 4;
4493   else
4494     entry->relocs32--;
4495   entry->relocs32++;
4496
4497   /* Allocate space for a GOT entry pointing to the function
4498      descriptor.  */
4499   if (entry->fdgot12)
4500     dinfo->got12 += 4;
4501   else if (entry->fdgotlos)
4502     dinfo->gotlos += 4;
4503   else if (entry->fdgothilo)
4504     dinfo->gothilo += 4;
4505   else
4506     entry->relocsfd--;
4507   entry->relocsfd++;
4508
4509   /* Decide whether we need a PLT entry, a function descriptor in the
4510      GOT, and a lazy PLT entry for this symbol.  */
4511   entry->plt = entry->call
4512     && entry->symndx == -1 && ! FRVFDPIC_SYM_LOCAL (dinfo->info, entry->d.h)
4513     && elf_hash_table (dinfo->info)->dynamic_sections_created;
4514   entry->privfd = entry->plt
4515     || entry->fdgoff12 || entry->fdgofflos || entry->fdgoffhilo
4516     || ((entry->fd || entry->fdgot12 || entry->fdgotlos || entry->fdgothilo)
4517         && (entry->symndx != -1
4518             || FRVFDPIC_FUNCDESC_LOCAL (dinfo->info, entry->d.h)));
4519   entry->lazyplt = entry->privfd
4520     && entry->symndx == -1 && ! FRVFDPIC_SYM_LOCAL (dinfo->info, entry->d.h)
4521     && ! (dinfo->info->flags & DF_BIND_NOW)
4522     && elf_hash_table (dinfo->info)->dynamic_sections_created;
4523
4524   /* Allocate space for a function descriptor.  */
4525   if (entry->fdgoff12)
4526     dinfo->fd12 += 8;
4527   else if (entry->fdgofflos)
4528     dinfo->fdlos += 8;
4529   else if (entry->privfd && entry->plt)
4530     dinfo->fdplt += 8;
4531   else if (entry->privfd)
4532     dinfo->fdhilo += 8;
4533   else
4534     entry->relocsfdv--;
4535   entry->relocsfdv++;
4536
4537   if (entry->lazyplt)
4538     dinfo->lzplt += 8;
4539 }
4540
4541 /* Compute the total GOT size required by each TLS symbol in each
4542    range.  Symbols may require up to 5 words in the GOT: an entry
4543    holding the TLS offset for the symbol, and an entry with a full TLS
4544    descriptor taking 4 words.  */
4545
4546 static void
4547 _frvfdpic_count_tls_entries (struct frvfdpic_relocs_info *entry,
4548                              struct _frvfdpic_dynamic_got_info *dinfo,
4549                              bfd_boolean subtract)
4550 {
4551   const int l = subtract ? -1 : 1;
4552   
4553   /* Allocate space for a GOT entry with the TLS offset of the
4554      symbol.  */
4555   if (entry->tlsoff12)
4556     dinfo->got12 += 4 * l;
4557   else if (entry->tlsofflos)
4558     dinfo->gotlos += 4 * l;
4559   else if (entry->tlsoffhilo)
4560     dinfo->gothilo += 4 * l;
4561   else
4562     entry->relocstlsoff -= l;
4563   entry->relocstlsoff += l;
4564
4565   /* If there's any TLSOFF relocation, mark the output file as not
4566      suitable for dlopening.  This mark will remain even if we relax
4567      all such relocations, but this is not a problem, since we'll only
4568      do so for executables, and we definitely don't want anyone
4569      dlopening executables.  */
4570   if (entry->relocstlsoff)
4571     dinfo->info->flags |= DF_STATIC_TLS;
4572
4573   /* Allocate space for a TLS descriptor.  */
4574   if (entry->tlsdesc12)
4575     dinfo->tlsd12 += 8 * l;
4576   else if (entry->tlsdesclos)
4577     dinfo->tlsdlos += 8 * l;
4578   else if (entry->tlsplt)
4579     dinfo->tlsdplt += 8 * l;
4580   else if (entry->tlsdeschilo)
4581     dinfo->tlsdhilo += 8 * l;
4582   else
4583     entry->relocstlsd -= l;
4584   entry->relocstlsd += l;
4585 }
4586
4587 /* Compute the number of dynamic relocations and fixups that a symbol
4588    requires, and add (or subtract) from the grand and per-symbol
4589    totals.  */
4590
4591 static void
4592 _frvfdpic_count_relocs_fixups (struct frvfdpic_relocs_info *entry,
4593                                struct _frvfdpic_dynamic_got_info *dinfo,
4594                                bfd_boolean subtract)
4595 {
4596   bfd_vma relocs = 0, fixups = 0, tlsrets = 0;
4597
4598   if (!dinfo->info->executable || dinfo->info->pie)
4599     {
4600       relocs = entry->relocs32 + entry->relocsfd + entry->relocsfdv
4601         + entry->relocstlsd;
4602
4603       /* In the executable, TLS relocations to symbols that bind
4604          locally (including those that resolve to global TLS offsets)
4605          are resolved immediately, without any need for fixups or
4606          dynamic relocations.  In shared libraries, however, we must
4607          emit dynamic relocations even for local symbols, because we
4608          don't know the module id the library is going to get at
4609          run-time, nor its TLS base offset.  */
4610       if (!dinfo->info->executable
4611           || (entry->symndx == -1
4612               && ! FRVFDPIC_SYM_LOCAL (dinfo->info, entry->d.h)))
4613         relocs += entry->relocstlsoff;
4614     }
4615   else
4616     {
4617       if (entry->symndx != -1 || FRVFDPIC_SYM_LOCAL (dinfo->info, entry->d.h))
4618         {
4619           if (entry->symndx != -1
4620               || entry->d.h->root.type != bfd_link_hash_undefweak)
4621             fixups += entry->relocs32 + 2 * entry->relocsfdv;
4622           fixups += entry->relocstlsd;
4623           tlsrets += entry->relocstlsd;
4624         }
4625       else
4626         {
4627           relocs += entry->relocs32 + entry->relocsfdv
4628             + entry->relocstlsoff + entry->relocstlsd;
4629         }
4630
4631       if (entry->symndx != -1
4632           || FRVFDPIC_FUNCDESC_LOCAL (dinfo->info, entry->d.h))
4633         {
4634           if (entry->symndx != -1
4635               || entry->d.h->root.type != bfd_link_hash_undefweak)
4636             fixups += entry->relocsfd;
4637         }
4638       else
4639         relocs += entry->relocsfd;
4640     }
4641
4642   if (subtract)
4643     {
4644       relocs = - relocs;
4645       fixups = - fixups;
4646       tlsrets = - tlsrets;
4647     }
4648
4649   entry->dynrelocs += relocs;
4650   entry->fixups += fixups;
4651   dinfo->relocs += relocs;
4652   dinfo->fixups += fixups;
4653   dinfo->tls_ret_refs += tlsrets;
4654 }
4655
4656 /* Look for opportunities to relax TLS relocations.  We can assume
4657    we're linking the main executable or a static-tls library, since
4658    otherwise we wouldn't have got here.  When relaxing, we have to
4659    first undo any previous accounting of TLS uses of fixups, dynamic
4660    relocations, GOT and PLT entries.  */
4661
4662 static void
4663 _frvfdpic_relax_tls_entries (struct frvfdpic_relocs_info *entry,
4664                              struct _frvfdpic_dynamic_got_info *dinfo,
4665                              bfd_boolean relaxing)
4666 {
4667   bfd_boolean changed = ! relaxing;
4668
4669   BFD_ASSERT (dinfo->info->executable
4670               || (dinfo->info->flags & DF_STATIC_TLS));
4671
4672   if (entry->tlsdesc12 || entry->tlsdesclos || entry->tlsdeschilo)
4673     {
4674       if (! changed)
4675         {
4676           _frvfdpic_count_relocs_fixups (entry, dinfo, TRUE);
4677           _frvfdpic_count_tls_entries (entry, dinfo, TRUE);
4678           changed = TRUE;
4679         }
4680
4681       /* When linking an executable, we can always decay GOTTLSDESC to
4682          TLSMOFF, if the symbol is local, or GOTTLSOFF, otherwise.
4683          When linking a static-tls shared library, using TLSMOFF is
4684          not an option, but we can still use GOTTLSOFF.  When decaying
4685          to GOTTLSOFF, we must keep the GOT entry in range.  We know
4686          it has to fit because we'll be trading the 4 words of hte TLS
4687          descriptor for a single word in the same range.  */
4688       if (! dinfo->info->executable
4689           || (entry->symndx == -1
4690               && ! FRVFDPIC_SYM_LOCAL (dinfo->info, entry->d.h)))
4691         {
4692           entry->tlsoff12 |= entry->tlsdesc12;
4693           entry->tlsofflos |= entry->tlsdesclos;
4694           entry->tlsoffhilo |= entry->tlsdeschilo;
4695         }
4696
4697       entry->tlsdesc12 = entry->tlsdesclos = entry->tlsdeschilo = 0;
4698     }
4699
4700   /* We can only decay TLSOFFs or call #gettlsoff to TLSMOFF in the
4701      main executable.  We have to check whether the symbol's TLSOFF is
4702      in range for a setlos.  For symbols with a hash entry, we can
4703      determine exactly what to do; for others locals, we don't have
4704      addresses handy, so we use the size of the TLS section as an
4705      approximation.  If we get it wrong, we'll retain a GOT entry
4706      holding the TLS offset (without dynamic relocations or fixups),
4707      but we'll still optimize away the loads from it.  Since TLS sizes
4708      are generally very small, it's probably not worth attempting to
4709      do better than this.  */
4710   if ((entry->tlsplt
4711        || entry->tlsoff12 || entry->tlsofflos || entry->tlsoffhilo)
4712       && dinfo->info->executable && relaxing
4713       && ((entry->symndx == -1
4714            && FRVFDPIC_SYM_LOCAL (dinfo->info, entry->d.h)
4715            /* The above may hold for an undefweak TLS symbol, so make
4716               sure we don't have this case before accessing def.value
4717               and def.section.  */
4718            && (entry->d.h->root.type == bfd_link_hash_undefweak
4719                || (bfd_vma)(entry->d.h->root.u.def.value
4720                             + (entry->d.h->root.u.def.section
4721                                ->output_section->vma)
4722                             + entry->d.h->root.u.def.section->output_offset
4723                             + entry->addend
4724                             - tls_biased_base (dinfo->info)
4725                             + 32768) < (bfd_vma)65536))
4726           || (entry->symndx != -1
4727               && (elf_hash_table (dinfo->info)->tls_sec->size
4728                   + abs (entry->addend) < 32768 + FRVFDPIC_TLS_BIAS))))
4729     {
4730       if (! changed)
4731         {
4732           _frvfdpic_count_relocs_fixups (entry, dinfo, TRUE);
4733           _frvfdpic_count_tls_entries (entry, dinfo, TRUE);
4734           changed = TRUE;
4735         }
4736       
4737       entry->tlsplt =
4738         entry->tlsoff12 = entry->tlsofflos = entry->tlsoffhilo = 0;
4739     }
4740
4741   /* We can decay `call #gettlsoff' to a ldi #tlsoff if we already
4742      have a #gottlsoff12 relocation for this entry, or if we can fit
4743      one more in the 12-bit (and 16-bit) ranges.  */
4744   if (entry->tlsplt
4745       && (entry->tlsoff12
4746           || (relaxing
4747               && dinfo->got12 + dinfo->fd12 + dinfo->tlsd12 <= 4096 - 12 - 4
4748               && (dinfo->got12 + dinfo->fd12 + dinfo->tlsd12
4749                   + dinfo->gotlos + dinfo->fdlos + dinfo->tlsdlos
4750                   <= 65536 - 12 - 4))))
4751     {
4752       if (! changed)
4753         {
4754           _frvfdpic_count_relocs_fixups (entry, dinfo, TRUE);
4755           _frvfdpic_count_tls_entries (entry, dinfo, TRUE);
4756           changed = TRUE;
4757         }
4758
4759       entry->tlsoff12 = 1;
4760       entry->tlsplt = 0;
4761     }
4762
4763   if (changed)
4764     {
4765       _frvfdpic_count_tls_entries (entry, dinfo, FALSE);
4766       _frvfdpic_count_relocs_fixups (entry, dinfo, FALSE);
4767     }
4768
4769   return;
4770 }
4771
4772 /* Compute the total GOT and PLT size required by each symbol in each range. *
4773    Symbols may require up to 4 words in the GOT: an entry pointing to
4774    the symbol, an entry pointing to its function descriptor, and a
4775    private function descriptors taking two words.  */
4776
4777 static int
4778 _frvfdpic_count_got_plt_entries (void **entryp, void *dinfo_)
4779 {
4780   struct frvfdpic_relocs_info *entry = *entryp;
4781   struct _frvfdpic_dynamic_got_info *dinfo = dinfo_;
4782
4783   _frvfdpic_count_nontls_entries (entry, dinfo);
4784
4785   if (dinfo->info->executable || (dinfo->info->flags & DF_STATIC_TLS))
4786     _frvfdpic_relax_tls_entries (entry, dinfo, FALSE);
4787   else
4788     {
4789       _frvfdpic_count_tls_entries (entry, dinfo, FALSE);
4790       _frvfdpic_count_relocs_fixups (entry, dinfo, FALSE);
4791     }
4792
4793   return 1;
4794 }
4795
4796 /* Determine the positive and negative ranges to be used by each
4797    offset range in the GOT.  FDCUR and CUR, that must be aligned to a
4798    double-word boundary, are the minimum (negative) and maximum
4799    (positive) GOT offsets already used by previous ranges, except for
4800    an ODD entry that may have been left behind.  GOT and FD indicate
4801    the size of GOT entries and function descriptors that must be
4802    placed within the range from -WRAP to WRAP.  If there's room left,
4803    up to FDPLT bytes should be reserved for additional function
4804    descriptors.  */
4805
4806 inline static bfd_signed_vma
4807 _frvfdpic_compute_got_alloc_data (struct _frvfdpic_dynamic_got_alloc_data *gad,
4808                                   bfd_signed_vma fdcur,
4809                                   bfd_signed_vma odd,
4810                                   bfd_signed_vma cur,
4811                                   bfd_vma got,
4812                                   bfd_vma fd,
4813                                   bfd_vma fdplt,
4814                                   bfd_vma tlsd,
4815                                   bfd_vma tlsdplt,
4816                                   bfd_vma wrap)
4817 {
4818   bfd_signed_vma wrapmin = -wrap;
4819   const bfd_vma tdescsz = 8;
4820
4821   /* Start at the given initial points.  */
4822   gad->fdcur = fdcur;
4823   gad->cur = cur;
4824
4825   /* If we had an incoming odd word and we have any got entries that
4826      are going to use it, consume it, otherwise leave gad->odd at
4827      zero.  We might force gad->odd to zero and return the incoming
4828      odd such that it is used by the next range, but then GOT entries
4829      might appear to be out of order and we wouldn't be able to
4830      shorten the GOT by one word if it turns out to end with an
4831      unpaired GOT entry.  */
4832   if (odd && got)
4833     {
4834       gad->odd = odd;
4835       got -= 4;
4836       odd = 0;
4837     }
4838   else
4839     gad->odd = 0;
4840
4841   /* If we're left with an unpaired GOT entry, compute its location
4842      such that we can return it.  Otherwise, if got doesn't require an
4843      odd number of words here, either odd was already zero in the
4844      block above, or it was set to zero because got was non-zero, or
4845      got was already zero.  In the latter case, we want the value of
4846      odd to carry over to the return statement, so we don't want to
4847      reset odd unless the condition below is true.  */
4848   if (got & 4)
4849     {
4850       odd = cur + got;
4851       got += 4;
4852     }
4853
4854   /* Compute the tentative boundaries of this range.  */
4855   gad->max = cur + got;
4856   gad->min = fdcur - fd;
4857   gad->fdplt = 0;
4858
4859   /* If function descriptors took too much space, wrap some of them
4860      around.  */
4861   if (gad->min < wrapmin)
4862     {
4863       gad->max += wrapmin - gad->min;
4864       gad->tmin = gad->min = wrapmin;
4865     }
4866
4867   /* If GOT entries took too much space, wrap some of them around.
4868      This may well cause gad->min to become lower than wrapmin.  This
4869      will cause a relocation overflow later on, so we don't have to
4870      report it here . */
4871   if ((bfd_vma) gad->max > wrap)
4872     {
4873       gad->min -= gad->max - wrap;
4874       gad->max = wrap;
4875     }
4876
4877   /* Add TLS descriptors.  */
4878   gad->tmax = gad->max + tlsd;
4879   gad->tmin = gad->min;
4880   gad->tlsdplt = 0;
4881
4882   /* If TLS descriptors took too much space, wrap an integral number
4883      of them around.  */
4884   if ((bfd_vma) gad->tmax > wrap)
4885     {
4886       bfd_vma wrapsize = gad->tmax - wrap;
4887
4888       wrapsize += tdescsz / 2;
4889       wrapsize &= ~ tdescsz / 2;
4890
4891       gad->tmin -= wrapsize;
4892       gad->tmax -= wrapsize;
4893     }
4894
4895   /* If there is space left and we have function descriptors
4896      referenced in PLT entries that could take advantage of shorter
4897      offsets, place them now.  */
4898   if (fdplt && gad->tmin > wrapmin)
4899     {
4900       bfd_vma fds;
4901
4902       if ((bfd_vma) (gad->tmin - wrapmin) < fdplt)
4903         fds = gad->tmin - wrapmin;
4904       else
4905         fds = fdplt;
4906
4907       fdplt -= fds;
4908       gad->min -= fds;
4909       gad->tmin -= fds;
4910       gad->fdplt += fds;
4911     }
4912
4913   /* If there is more space left, try to place some more function
4914      descriptors for PLT entries.  */
4915   if (fdplt && (bfd_vma) gad->tmax < wrap)
4916     {
4917       bfd_vma fds;
4918
4919       if ((bfd_vma) (wrap - gad->tmax) < fdplt)
4920         fds = wrap - gad->tmax;
4921       else
4922         fds = fdplt;
4923
4924       fdplt -= fds;
4925       gad->max += fds;
4926       gad->tmax += fds;
4927       gad->fdplt += fds;
4928     }
4929
4930   /* If there is space left and we have TLS descriptors referenced in
4931      PLT entries that could take advantage of shorter offsets, place
4932      them now.  */
4933   if (tlsdplt && gad->tmin > wrapmin)
4934     {
4935       bfd_vma tlsds;
4936
4937       if ((bfd_vma) (gad->tmin - wrapmin) < tlsdplt)
4938         tlsds = (gad->tmin - wrapmin) & ~ (tdescsz / 2);
4939       else
4940         tlsds = tlsdplt;
4941
4942       tlsdplt -= tlsds;
4943       gad->tmin -= tlsds;
4944       gad->tlsdplt += tlsds;
4945     }
4946
4947   /* If there is more space left, try to place some more TLS
4948      descriptors for PLT entries.  Although we could try to fit an
4949      additional TLS descriptor with half of it just before before the
4950      wrap point and another right past the wrap point, this might
4951      cause us to run out of space for the next region, so don't do
4952      it.  */
4953   if (tlsdplt && (bfd_vma) gad->tmax < wrap - tdescsz / 2)
4954     {
4955       bfd_vma tlsds;
4956
4957       if ((bfd_vma) (wrap - gad->tmax) < tlsdplt)
4958         tlsds = (wrap - gad->tmax) & ~ (tdescsz / 2);
4959       else
4960         tlsds = tlsdplt;
4961
4962       tlsdplt -= tlsds;
4963       gad->tmax += tlsds;
4964       gad->tlsdplt += tlsds;
4965     }
4966
4967   /* If odd was initially computed as an offset past the wrap point,
4968      wrap it around.  */
4969   if (odd > gad->max)
4970     odd = gad->min + odd - gad->max;
4971
4972   /* _frvfdpic_get_got_entry() below will always wrap gad->cur if needed
4973      before returning, so do it here too.  This guarantees that,
4974      should cur and fdcur meet at the wrap point, they'll both be
4975      equal to min.  */
4976   if (gad->cur == gad->max)
4977     gad->cur = gad->min;
4978
4979   /* Ditto for _frvfdpic_get_tlsdesc_entry().  */
4980   gad->tcur = gad->max;
4981   if (gad->tcur == gad->tmax)
4982     gad->tcur = gad->tmin;
4983
4984   return odd;
4985 }
4986
4987 /* Compute the location of the next GOT entry, given the allocation
4988    data for a range.  */
4989
4990 inline static bfd_signed_vma
4991 _frvfdpic_get_got_entry (struct _frvfdpic_dynamic_got_alloc_data *gad)
4992 {
4993   bfd_signed_vma ret;
4994
4995   if (gad->odd)
4996     {
4997       /* If there was an odd word left behind, use it.  */
4998       ret = gad->odd;
4999       gad->odd = 0;
5000     }
5001   else
5002     {
5003       /* Otherwise, use the word pointed to by cur, reserve the next
5004          as an odd word, and skip to the next pair of words, possibly
5005          wrapping around.  */
5006       ret = gad->cur;
5007       gad->odd = gad->cur + 4;
5008       gad->cur += 8;
5009       if (gad->cur == gad->max)
5010         gad->cur = gad->min;
5011     }
5012
5013   return ret;
5014 }
5015
5016 /* Compute the location of the next function descriptor entry in the
5017    GOT, given the allocation data for a range.  */
5018
5019 inline static bfd_signed_vma
5020 _frvfdpic_get_fd_entry (struct _frvfdpic_dynamic_got_alloc_data *gad)
5021 {
5022   /* If we're at the bottom, wrap around, and only then allocate the
5023      next pair of words.  */
5024   if (gad->fdcur == gad->min)
5025     gad->fdcur = gad->max;
5026   return gad->fdcur -= 8;
5027 }
5028
5029 /* Compute the location of the next TLS descriptor entry in the GOT,
5030    given the allocation data for a range.  */
5031 inline static bfd_signed_vma
5032 _frvfdpic_get_tlsdesc_entry (struct _frvfdpic_dynamic_got_alloc_data *gad)
5033 {
5034   bfd_signed_vma ret;
5035
5036   ret = gad->tcur;
5037
5038   gad->tcur += 8;
5039
5040   /* If we're at the top of the region, wrap around to the bottom.  */
5041   if (gad->tcur == gad->tmax)
5042     gad->tcur = gad->tmin;
5043
5044   return ret;
5045 }
5046
5047 /* Assign GOT offsets for every GOT entry and function descriptor.
5048    Doing everything in a single pass is tricky.  */
5049
5050 static int
5051 _frvfdpic_assign_got_entries (void **entryp, void *info_)
5052 {
5053   struct frvfdpic_relocs_info *entry = *entryp;
5054   struct _frvfdpic_dynamic_got_plt_info *dinfo = info_;
5055
5056   if (entry->got12)
5057     entry->got_entry = _frvfdpic_get_got_entry (&dinfo->got12);
5058   else if (entry->gotlos)
5059     entry->got_entry = _frvfdpic_get_got_entry (&dinfo->gotlos);
5060   else if (entry->gothilo)
5061     entry->got_entry = _frvfdpic_get_got_entry (&dinfo->gothilo);
5062
5063   if (entry->fdgot12)
5064     entry->fdgot_entry = _frvfdpic_get_got_entry (&dinfo->got12);
5065   else if (entry->fdgotlos)
5066     entry->fdgot_entry = _frvfdpic_get_got_entry (&dinfo->gotlos);
5067   else if (entry->fdgothilo)
5068     entry->fdgot_entry = _frvfdpic_get_got_entry (&dinfo->gothilo);
5069
5070   if (entry->fdgoff12)
5071     entry->fd_entry = _frvfdpic_get_fd_entry (&dinfo->got12);
5072   else if (entry->plt && dinfo->got12.fdplt)
5073     {
5074       dinfo->got12.fdplt -= 8;
5075       entry->fd_entry = _frvfdpic_get_fd_entry (&dinfo->got12);
5076     }
5077   else if (entry->fdgofflos)
5078     entry->fd_entry = _frvfdpic_get_fd_entry (&dinfo->gotlos);
5079   else if (entry->plt && dinfo->gotlos.fdplt)
5080     {
5081       dinfo->gotlos.fdplt -= 8;
5082       entry->fd_entry = _frvfdpic_get_fd_entry (&dinfo->gotlos);
5083     }
5084   else if (entry->plt)
5085     {
5086       dinfo->gothilo.fdplt -= 8;
5087       entry->fd_entry = _frvfdpic_get_fd_entry (&dinfo->gothilo);
5088     }
5089   else if (entry->privfd)
5090     entry->fd_entry = _frvfdpic_get_fd_entry (&dinfo->gothilo);
5091
5092   if (entry->tlsoff12)
5093     entry->tlsoff_entry = _frvfdpic_get_got_entry (&dinfo->got12);
5094   else if (entry->tlsofflos)
5095     entry->tlsoff_entry = _frvfdpic_get_got_entry (&dinfo->gotlos);
5096   else if (entry->tlsoffhilo)
5097     entry->tlsoff_entry = _frvfdpic_get_got_entry (&dinfo->gothilo);
5098
5099   if (entry->tlsdesc12)
5100     entry->tlsdesc_entry = _frvfdpic_get_tlsdesc_entry (&dinfo->got12);
5101   else if (entry->tlsplt && dinfo->got12.tlsdplt)
5102     {
5103       dinfo->got12.tlsdplt -= 8;
5104       entry->tlsdesc_entry = _frvfdpic_get_tlsdesc_entry (&dinfo->got12);
5105     }
5106   else if (entry->tlsdesclos)
5107     entry->tlsdesc_entry = _frvfdpic_get_tlsdesc_entry (&dinfo->gotlos);
5108   else if (entry->tlsplt && dinfo->gotlos.tlsdplt)
5109     {
5110       dinfo->gotlos.tlsdplt -= 8;
5111       entry->tlsdesc_entry = _frvfdpic_get_tlsdesc_entry (&dinfo->gotlos);
5112     }
5113   else if (entry->tlsplt)
5114     {
5115       dinfo->gothilo.tlsdplt -= 8;
5116       entry->tlsdesc_entry = _frvfdpic_get_tlsdesc_entry (&dinfo->gothilo);
5117     }
5118   else if (entry->tlsdeschilo)
5119     entry->tlsdesc_entry = _frvfdpic_get_tlsdesc_entry (&dinfo->gothilo);
5120
5121   return 1;
5122 }
5123
5124 /* Assign GOT offsets to private function descriptors used by PLT
5125    entries (or referenced by 32-bit offsets), as well as PLT entries
5126    and lazy PLT entries.  */
5127
5128 static int
5129 _frvfdpic_assign_plt_entries (void **entryp, void *info_)
5130 {
5131   struct frvfdpic_relocs_info *entry = *entryp;
5132   struct _frvfdpic_dynamic_got_plt_info *dinfo = info_;
5133
5134   if (entry->privfd)
5135     BFD_ASSERT (entry->fd_entry);
5136
5137   if (entry->plt)
5138     {
5139       int size;
5140
5141       /* We use the section's raw size to mark the location of the
5142          next PLT entry.  */
5143       entry->plt_entry = frvfdpic_plt_section (dinfo->g.info)->size;
5144
5145       /* Figure out the length of this PLT entry based on the
5146          addressing mode we need to reach the function descriptor.  */
5147       BFD_ASSERT (entry->fd_entry);
5148       if (entry->fd_entry >= -(1 << (12 - 1))
5149           && entry->fd_entry < (1 << (12 - 1)))
5150         size = 8;
5151       else if (entry->fd_entry >= -(1 << (16 - 1))
5152                && entry->fd_entry < (1 << (16 - 1)))
5153         size = 12;
5154       else
5155         size = 16;
5156
5157       frvfdpic_plt_section (dinfo->g.info)->size += size;
5158     }
5159
5160   if (entry->lazyplt)
5161     {
5162       entry->lzplt_entry = dinfo->g.lzplt;
5163       dinfo->g.lzplt += 8;
5164       /* If this entry is the one that gets the resolver stub, account
5165          for the additional instruction.  */
5166       if (entry->lzplt_entry % FRVFDPIC_LZPLT_BLOCK_SIZE
5167           == FRVFDPIC_LZPLT_RESOLV_LOC)
5168         dinfo->g.lzplt += 4;
5169     }
5170
5171   if (entry->tlsplt)
5172     {
5173       int size;
5174
5175       entry->tlsplt_entry
5176         = frvfdpic_plt_section (dinfo->g.info)->size;
5177
5178       if (dinfo->g.info->executable
5179           && (entry->symndx != -1
5180               || FRVFDPIC_SYM_LOCAL (dinfo->g.info, entry->d.h)))
5181         {
5182           if ((bfd_signed_vma)entry->addend >= -(1 << (16 - 1))
5183               /* FIXME: here we use the size of the TLS section
5184                  as an upper bound for the value of the TLS
5185                  symbol, because we may not know the exact value
5186                  yet.  If we get it wrong, we'll just waste a
5187                  word in the PLT, and we should never get even
5188                  close to 32 KiB of TLS anyway.  */
5189               && elf_hash_table (dinfo->g.info)->tls_sec
5190               && (elf_hash_table (dinfo->g.info)->tls_sec->size
5191                   + (bfd_signed_vma)(entry->addend) <= (1 << (16 - 1))))
5192             size = 8;
5193           else
5194             size = 12;
5195         }
5196       else if (entry->tlsoff_entry)
5197         {
5198           if (entry->tlsoff_entry >= -(1 << (12 - 1))
5199               && entry->tlsoff_entry < (1 << (12 - 1)))
5200             size = 8;
5201           else if (entry->tlsoff_entry >= -(1 << (16 - 1))
5202                    && entry->tlsoff_entry < (1 << (16 - 1)))
5203             size = 12;
5204           else
5205             size = 16;
5206         }
5207       else
5208         {
5209           BFD_ASSERT (entry->tlsdesc_entry);
5210
5211           if (entry->tlsdesc_entry >= -(1 << (12 - 1))
5212               && entry->tlsdesc_entry < (1 << (12 - 1)))
5213             size = 8;
5214           else if (entry->tlsdesc_entry >= -(1 << (16 - 1))
5215                    && entry->tlsdesc_entry < (1 << (16 - 1)))
5216             size = 12;
5217           else
5218             size = 16;
5219         }
5220       
5221       frvfdpic_plt_section (dinfo->g.info)->size += size;
5222     }
5223
5224   return 1;
5225 }
5226
5227 /* Cancel out any effects of calling _frvfdpic_assign_got_entries and
5228    _frvfdpic_assign_plt_entries.  */
5229
5230 static int
5231 _frvfdpic_reset_got_plt_entries (void **entryp, void *ignore ATTRIBUTE_UNUSED)
5232 {
5233   struct frvfdpic_relocs_info *entry = *entryp;
5234
5235   entry->got_entry = 0;
5236   entry->fdgot_entry = 0;
5237   entry->fd_entry = 0;
5238   entry->plt_entry = (bfd_vma)-1;
5239   entry->lzplt_entry = (bfd_vma)-1;
5240   entry->tlsoff_entry = 0;
5241   entry->tlsdesc_entry = 0;
5242   entry->tlsplt_entry = (bfd_vma)-1;
5243
5244   return 1;
5245 }
5246
5247 /* Follow indirect and warning hash entries so that each got entry
5248    points to the final symbol definition.  P must point to a pointer
5249    to the hash table we're traversing.  Since this traversal may
5250    modify the hash table, we set this pointer to NULL to indicate
5251    we've made a potentially-destructive change to the hash table, so
5252    the traversal must be restarted.  */
5253 static int
5254 _frvfdpic_resolve_final_relocs_info (void **entryp, void *p)
5255 {
5256   struct frvfdpic_relocs_info *entry = *entryp;
5257   htab_t *htab = p;
5258
5259   if (entry->symndx == -1)
5260     {
5261       struct elf_link_hash_entry *h = entry->d.h;
5262       struct frvfdpic_relocs_info *oentry;
5263
5264       while (h->root.type == bfd_link_hash_indirect
5265              || h->root.type == bfd_link_hash_warning)
5266         h = (struct elf_link_hash_entry *)h->root.u.i.link;
5267
5268       if (entry->d.h == h)
5269         return 1;
5270
5271       oentry = frvfdpic_relocs_info_for_global (*htab, 0, h, entry->addend,
5272                                                 NO_INSERT);
5273
5274       if (oentry)
5275         {
5276           /* Merge the two entries.  */
5277           frvfdpic_pic_merge_early_relocs_info (oentry, entry);
5278           htab_clear_slot (*htab, entryp);
5279           return 1;
5280         }
5281
5282       entry->d.h = h;
5283
5284       /* If we can't find this entry with the new bfd hash, re-insert
5285          it, and get the traversal restarted.  */
5286       if (! htab_find (*htab, entry))
5287         {
5288           htab_clear_slot (*htab, entryp);
5289           entryp = htab_find_slot (*htab, entry, INSERT);
5290           if (! *entryp)
5291             *entryp = entry;
5292           /* Abort the traversal, since the whole table may have
5293              moved, and leave it up to the parent to restart the
5294              process.  */
5295           *(htab_t *)p = NULL;
5296           return 0;
5297         }
5298     }
5299
5300   return 1;
5301 }
5302
5303 /* Compute the total size of the GOT, the PLT, the dynamic relocations
5304    section and the rofixup section.  Assign locations for GOT and PLT
5305    entries.  */
5306
5307 static bfd_boolean
5308 _frvfdpic_size_got_plt (bfd *output_bfd,
5309                         struct _frvfdpic_dynamic_got_plt_info *gpinfop)
5310 {
5311   bfd_signed_vma odd;
5312   bfd_vma limit, tlslimit;
5313   struct bfd_link_info *info = gpinfop->g.info;
5314   bfd *dynobj = elf_hash_table (info)->dynobj;
5315
5316   memcpy (frvfdpic_dynamic_got_plt_info (info), &gpinfop->g,
5317           sizeof (gpinfop->g));
5318
5319   odd = 12;
5320   /* Compute the total size taken by entries in the 12-bit and 16-bit
5321      ranges, to tell how many PLT function descriptors we can bring
5322      into the 12-bit range without causing the 16-bit range to
5323      overflow.  */
5324   limit = odd + gpinfop->g.got12 + gpinfop->g.gotlos
5325     + gpinfop->g.fd12 + gpinfop->g.fdlos
5326     + gpinfop->g.tlsd12 + gpinfop->g.tlsdlos;
5327   if (limit < (bfd_vma)1 << 16)
5328     limit = ((bfd_vma)1 << 16) - limit;
5329   else
5330     limit = 0;
5331   if (gpinfop->g.fdplt < limit)
5332     {
5333       tlslimit = (limit - gpinfop->g.fdplt) & ~ (bfd_vma) 8;
5334       limit = gpinfop->g.fdplt;
5335     }
5336   else
5337     tlslimit = 0;
5338   if (gpinfop->g.tlsdplt < tlslimit)
5339     tlslimit = gpinfop->g.tlsdplt;
5340
5341   /* Determine the ranges of GOT offsets that we can use for each
5342      range of addressing modes.  */
5343   odd = _frvfdpic_compute_got_alloc_data (&gpinfop->got12,
5344                                           0,
5345                                           odd,
5346                                           16,
5347                                           gpinfop->g.got12,
5348                                           gpinfop->g.fd12,
5349                                           limit,
5350                                           gpinfop->g.tlsd12,
5351                                           tlslimit,
5352                                           (bfd_vma)1 << (12-1));
5353   odd = _frvfdpic_compute_got_alloc_data (&gpinfop->gotlos,
5354                                           gpinfop->got12.tmin,
5355                                           odd,
5356                                           gpinfop->got12.tmax,
5357                                           gpinfop->g.gotlos,
5358                                           gpinfop->g.fdlos,
5359                                           gpinfop->g.fdplt
5360                                           - gpinfop->got12.fdplt,
5361                                           gpinfop->g.tlsdlos,
5362                                           gpinfop->g.tlsdplt
5363                                           - gpinfop->got12.tlsdplt,
5364                                           (bfd_vma)1 << (16-1));
5365   odd = _frvfdpic_compute_got_alloc_data (&gpinfop->gothilo,
5366                                           gpinfop->gotlos.tmin,
5367                                           odd,
5368                                           gpinfop->gotlos.tmax,
5369                                           gpinfop->g.gothilo,
5370                                           gpinfop->g.fdhilo,
5371                                           gpinfop->g.fdplt
5372                                           - gpinfop->got12.fdplt
5373                                           - gpinfop->gotlos.fdplt,
5374                                           gpinfop->g.tlsdhilo,
5375                                           gpinfop->g.tlsdplt
5376                                           - gpinfop->got12.tlsdplt
5377                                           - gpinfop->gotlos.tlsdplt,
5378                                           (bfd_vma)1 << (32-1));
5379
5380   /* Now assign (most) GOT offsets.  */
5381   htab_traverse (frvfdpic_relocs_info (info), _frvfdpic_assign_got_entries,
5382                  gpinfop);
5383
5384   frvfdpic_got_section (info)->size = gpinfop->gothilo.tmax
5385     - gpinfop->gothilo.tmin
5386     /* If an odd word is the last word of the GOT, we don't need this
5387        word to be part of the GOT.  */
5388     - (odd + 4 == gpinfop->gothilo.tmax ? 4 : 0);
5389   if (frvfdpic_got_section (info)->size == 0)
5390     frvfdpic_got_section (info)->flags |= SEC_EXCLUDE;
5391   else if (frvfdpic_got_section (info)->size == 12
5392            && ! elf_hash_table (info)->dynamic_sections_created)
5393     {
5394       frvfdpic_got_section (info)->flags |= SEC_EXCLUDE;
5395       frvfdpic_got_section (info)->size = 0;
5396     }
5397   /* This will be non-NULL during relaxation.  The assumption is that
5398      the size of one of these sections will never grow, only shrink,
5399      so we can use the larger buffer we allocated before.  */
5400   else if (frvfdpic_got_section (info)->contents == NULL)
5401     {
5402       frvfdpic_got_section (info)->contents =
5403         (bfd_byte *) bfd_zalloc (dynobj,
5404                                  frvfdpic_got_section (info)->size);
5405       if (frvfdpic_got_section (info)->contents == NULL)
5406         return FALSE;
5407     }
5408
5409   if (frvfdpic_gotrel_section (info))
5410     /* Subtract the number of lzplt entries, since those will generate
5411        relocations in the pltrel section.  */
5412     frvfdpic_gotrel_section (info)->size =
5413       (gpinfop->g.relocs - gpinfop->g.lzplt / 8)
5414       * get_elf_backend_data (output_bfd)->s->sizeof_rel;
5415   else
5416     BFD_ASSERT (gpinfop->g.relocs == 0);
5417   if (frvfdpic_gotrel_section (info)->size == 0)
5418     frvfdpic_gotrel_section (info)->flags |= SEC_EXCLUDE;
5419   else if (frvfdpic_gotrel_section (info)->contents == NULL)
5420     {
5421       frvfdpic_gotrel_section (info)->contents =
5422         (bfd_byte *) bfd_zalloc (dynobj,
5423                                  frvfdpic_gotrel_section (info)->size);
5424       if (frvfdpic_gotrel_section (info)->contents == NULL)
5425         return FALSE;
5426     }
5427
5428   frvfdpic_gotfixup_section (info)->size = (gpinfop->g.fixups + 1) * 4;
5429   if (frvfdpic_gotfixup_section (info)->size == 0)
5430     frvfdpic_gotfixup_section (info)->flags |= SEC_EXCLUDE;
5431   else if (frvfdpic_gotfixup_section (info)->contents == NULL)
5432     {
5433       frvfdpic_gotfixup_section (info)->contents =
5434         (bfd_byte *) bfd_zalloc (dynobj,
5435                                  frvfdpic_gotfixup_section (info)->size);
5436       if (frvfdpic_gotfixup_section (info)->contents == NULL)
5437         return FALSE;
5438     }
5439
5440   if (frvfdpic_pltrel_section (info))
5441     {
5442       frvfdpic_pltrel_section (info)->size =
5443         gpinfop->g.lzplt / 8
5444         * get_elf_backend_data (output_bfd)->s->sizeof_rel;
5445       if (frvfdpic_pltrel_section (info)->size == 0)
5446         frvfdpic_pltrel_section (info)->flags |= SEC_EXCLUDE;
5447       else if (frvfdpic_pltrel_section (info)->contents == NULL)
5448         {
5449           frvfdpic_pltrel_section (info)->contents =
5450             (bfd_byte *) bfd_zalloc (dynobj,
5451                                      frvfdpic_pltrel_section (info)->size);
5452           if (frvfdpic_pltrel_section (info)->contents == NULL)
5453             return FALSE;
5454         }
5455     }
5456
5457   /* Add 4 bytes for every block of at most 65535 lazy PLT entries,
5458      such that there's room for the additional instruction needed to
5459      call the resolver.  Since _frvfdpic_assign_got_entries didn't
5460      account for them, our block size is 4 bytes smaller than the real
5461      block size.  */
5462   if (frvfdpic_plt_section (info))
5463     {
5464       frvfdpic_plt_section (info)->size = gpinfop->g.lzplt
5465         + ((gpinfop->g.lzplt + (FRVFDPIC_LZPLT_BLOCK_SIZE - 4) - 8)
5466            / (FRVFDPIC_LZPLT_BLOCK_SIZE - 4) * 4);
5467     }
5468
5469   /* Reset it, such that _frvfdpic_assign_plt_entries() can use it to
5470      actually assign lazy PLT entries addresses.  */
5471   gpinfop->g.lzplt = 0;
5472
5473   /* Save information that we're going to need to generate GOT and PLT
5474      entries.  */
5475   frvfdpic_got_initial_offset (info) = -gpinfop->gothilo.tmin;
5476
5477   if (get_elf_backend_data (output_bfd)->want_got_sym)
5478     elf_hash_table (info)->hgot->root.u.def.value
5479       = frvfdpic_got_initial_offset (info);
5480
5481   if (frvfdpic_plt_section (info))
5482     frvfdpic_plt_initial_offset (info) =
5483       frvfdpic_plt_section (info)->size;
5484
5485   /* Allocate a ret statement at plt_initial_offset, to be used by
5486      locally-resolved TLS descriptors.  */
5487   if (gpinfop->g.tls_ret_refs)
5488     frvfdpic_plt_section (info)->size += 4;
5489
5490   htab_traverse (frvfdpic_relocs_info (info), _frvfdpic_assign_plt_entries,
5491                  gpinfop);
5492
5493   /* Allocate the PLT section contents only after
5494      _frvfdpic_assign_plt_entries has a chance to add the size of the
5495      non-lazy PLT entries.  */
5496   if (frvfdpic_plt_section (info))
5497     {
5498       if (frvfdpic_plt_section (info)->size == 0)
5499         frvfdpic_plt_section (info)->flags |= SEC_EXCLUDE;
5500       else if (frvfdpic_plt_section (info)->contents == NULL)
5501         {
5502           frvfdpic_plt_section (info)->contents =
5503             (bfd_byte *) bfd_zalloc (dynobj,
5504                                      frvfdpic_plt_section (info)->size);
5505           if (frvfdpic_plt_section (info)->contents == NULL)
5506             return FALSE;
5507         }
5508     }
5509
5510   return TRUE;
5511 }
5512
5513 /* Set the sizes of the dynamic sections.  */
5514
5515 static bfd_boolean
5516 elf32_frvfdpic_size_dynamic_sections (bfd *output_bfd,
5517                                       struct bfd_link_info *info)
5518 {
5519   bfd *dynobj;
5520   asection *s;
5521   struct _frvfdpic_dynamic_got_plt_info gpinfo;
5522
5523   dynobj = elf_hash_table (info)->dynobj;
5524   BFD_ASSERT (dynobj != NULL);
5525
5526   if (elf_hash_table (info)->dynamic_sections_created)
5527     {
5528       /* Set the contents of the .interp section to the interpreter.  */
5529       if (info->executable)
5530         {
5531           s = bfd_get_section_by_name (dynobj, ".interp");
5532           BFD_ASSERT (s != NULL);
5533           s->size = sizeof ELF_DYNAMIC_INTERPRETER;
5534           s->contents = (bfd_byte *) ELF_DYNAMIC_INTERPRETER;
5535         }
5536     }
5537
5538   memset (&gpinfo, 0, sizeof (gpinfo));
5539   gpinfo.g.info = info;
5540
5541   for (;;)
5542     {
5543       htab_t relocs = frvfdpic_relocs_info (info);
5544
5545       htab_traverse (relocs, _frvfdpic_resolve_final_relocs_info, &relocs);
5546
5547       if (relocs == frvfdpic_relocs_info (info))
5548         break;
5549     }
5550
5551   htab_traverse (frvfdpic_relocs_info (info), _frvfdpic_count_got_plt_entries,
5552                  &gpinfo.g);
5553
5554   /* Allocate space to save the summary information, we're going to
5555      use it if we're doing relaxations.  */
5556   frvfdpic_dynamic_got_plt_info (info) = bfd_alloc (dynobj, sizeof (gpinfo.g));
5557
5558   if (!_frvfdpic_size_got_plt (output_bfd, &gpinfo))
5559     return FALSE;
5560
5561   if (elf_hash_table (info)->dynamic_sections_created)
5562     {
5563       if (frvfdpic_got_section (info)->size)
5564         if (!_bfd_elf_add_dynamic_entry (info, DT_PLTGOT, 0))
5565           return FALSE;
5566
5567       if (frvfdpic_pltrel_section (info)->size)
5568         if (!_bfd_elf_add_dynamic_entry (info, DT_PLTRELSZ, 0)
5569             || !_bfd_elf_add_dynamic_entry (info, DT_PLTREL, DT_REL)
5570             || !_bfd_elf_add_dynamic_entry (info, DT_JMPREL, 0))
5571           return FALSE;
5572
5573       if (frvfdpic_gotrel_section (info)->size)
5574         if (!_bfd_elf_add_dynamic_entry (info, DT_REL, 0)
5575             || !_bfd_elf_add_dynamic_entry (info, DT_RELSZ, 0)
5576             || !_bfd_elf_add_dynamic_entry (info, DT_RELENT,
5577                                             sizeof (Elf32_External_Rel)))
5578           return FALSE;
5579     }
5580
5581   return TRUE;
5582 }
5583
5584 static bfd_boolean
5585 elf32_frvfdpic_always_size_sections (bfd *output_bfd,
5586                                      struct bfd_link_info *info)
5587 {
5588   if (!info->relocatable)
5589     {
5590       struct elf_link_hash_entry *h;
5591       asection *sec;
5592
5593       /* Force a PT_GNU_STACK segment to be created.  */
5594       if (! elf_tdata (output_bfd)->stack_flags)
5595         elf_tdata (output_bfd)->stack_flags = PF_R | PF_W | PF_X;
5596
5597       /* Define __stacksize if it's not defined yet.  */
5598       h = elf_link_hash_lookup (elf_hash_table (info), "__stacksize",
5599                                 FALSE, FALSE, FALSE);
5600       if (! h || h->root.type != bfd_link_hash_defined
5601           || h->type != STT_OBJECT
5602           || !h->def_regular)
5603         {
5604           struct bfd_link_hash_entry *bh = NULL;
5605
5606           if (!(_bfd_generic_link_add_one_symbol
5607                 (info, output_bfd, "__stacksize",
5608                  BSF_GLOBAL, bfd_abs_section_ptr, DEFAULT_STACK_SIZE,
5609                  (const char *) NULL, FALSE,
5610                  get_elf_backend_data (output_bfd)->collect, &bh)))
5611             return FALSE;
5612
5613           h = (struct elf_link_hash_entry *) bh;
5614           h->def_regular = 1;
5615           h->type = STT_OBJECT;
5616           /* This one must NOT be hidden.  */
5617         }
5618
5619       /* Create a stack section, and set its alignment.  */
5620       sec = bfd_make_section (output_bfd, ".stack");
5621
5622       if (sec == NULL
5623           || ! bfd_set_section_alignment (output_bfd, sec, 3))
5624         return FALSE;
5625     }
5626
5627   return TRUE;
5628 }
5629
5630 /* Look for opportunities to relax TLS relocations.  We can assume
5631    we're linking the main executable or a static-tls library, since
5632    otherwise we wouldn't have got here.  */
5633
5634 static int
5635 _frvfdpic_relax_got_plt_entries (void **entryp, void *dinfo_)
5636 {
5637   struct frvfdpic_relocs_info *entry = *entryp;
5638   struct _frvfdpic_dynamic_got_info *dinfo = dinfo_;
5639
5640   _frvfdpic_relax_tls_entries (entry, dinfo, TRUE);
5641
5642   return 1;
5643 }
5644
5645 static bfd_boolean
5646 elf32_frvfdpic_relax_section (bfd *abfd ATTRIBUTE_UNUSED, asection *sec,
5647                               struct bfd_link_info *info, bfd_boolean *again)
5648 {
5649   struct _frvfdpic_dynamic_got_plt_info gpinfo;
5650
5651   /* If we return early, we didn't change anything.  */
5652   *again = FALSE;
5653
5654   /* We'll do our thing when requested to relax the GOT section.  */
5655   if (sec != frvfdpic_got_section (info))
5656     return TRUE;
5657
5658   /* We can only relax when linking the main executable or a library
5659      that can't be dlopened.  */
5660   if (! info->executable && ! (info->flags & DF_STATIC_TLS))
5661     return TRUE;
5662
5663   /* If there isn't a TLS section for this binary, we can't do
5664      anything about its TLS relocations (it probably doesn't have
5665      any.  */
5666   if (elf_hash_table (info)->tls_sec == NULL)
5667     return TRUE;
5668
5669   memset (&gpinfo, 0, sizeof (gpinfo));
5670   memcpy (&gpinfo.g, frvfdpic_dynamic_got_plt_info (info), sizeof (gpinfo.g));
5671
5672   /* Now look for opportunities to relax, adjusting the GOT usage
5673      as needed.  */
5674   htab_traverse (frvfdpic_relocs_info (info),
5675                  _frvfdpic_relax_got_plt_entries,
5676                  &gpinfo.g);
5677
5678   /* If we changed anything, reset and re-assign GOT and PLT entries.  */
5679   if (memcmp (frvfdpic_dynamic_got_plt_info (info),
5680               &gpinfo.g, sizeof (gpinfo.g)) != 0)
5681     {
5682       /* Clear GOT and PLT assignments.  */
5683       htab_traverse (frvfdpic_relocs_info (info),
5684                      _frvfdpic_reset_got_plt_entries,
5685                      NULL);
5686
5687       /* The owner of the TLS section is the output bfd.  There should
5688          be a better way to get to it.  */
5689       if (!_frvfdpic_size_got_plt (elf_hash_table (info)->tls_sec->owner,
5690                                    &gpinfo))
5691         return FALSE;
5692
5693       /* Repeat until we don't make any further changes.  We could fail to
5694          introduce changes in a round if, for example, the 12-bit range is
5695          full, but we later release some space by getting rid of TLS
5696          descriptors in it.  We have to repeat the whole process because
5697          we might have changed the size of a section processed before this
5698          one.  */
5699       *again = TRUE;
5700     }
5701
5702   return TRUE;
5703 }
5704
5705 static bfd_boolean
5706 elf32_frvfdpic_modify_segment_map (bfd *output_bfd,
5707                                    struct bfd_link_info *info)
5708 {
5709   struct elf_segment_map *m;
5710
5711   /* objcopy and strip preserve what's already there using
5712      elf32_frvfdpic_copy_private_bfd_data ().  */
5713   if (! info)
5714     return TRUE;
5715
5716   for (m = elf_tdata (output_bfd)->segment_map; m != NULL; m = m->next)
5717     if (m->p_type == PT_GNU_STACK)
5718       break;
5719
5720   if (m)
5721     {
5722       asection *sec = bfd_get_section_by_name (output_bfd, ".stack");
5723       struct elf_link_hash_entry *h;
5724
5725       if (sec)
5726         {
5727           /* Obtain the pointer to the __stacksize symbol.  */
5728           h = elf_link_hash_lookup (elf_hash_table (info), "__stacksize",
5729                                     FALSE, FALSE, FALSE);
5730           while (h->root.type == bfd_link_hash_indirect
5731                  || h->root.type == bfd_link_hash_warning)
5732             h = (struct elf_link_hash_entry *)h->root.u.i.link;
5733           BFD_ASSERT (h->root.type == bfd_link_hash_defined);
5734
5735           /* Set the section size from the symbol value.  We
5736              intentionally ignore the symbol section.  */
5737           if (h->root.type == bfd_link_hash_defined)
5738             sec->size = h->root.u.def.value;
5739           else
5740             sec->size = DEFAULT_STACK_SIZE;
5741
5742           /* Add the stack section to the PT_GNU_STACK segment,
5743              such that its size and alignment requirements make it
5744              to the segment.  */
5745           m->sections[m->count] = sec;
5746           m->count++;
5747         }
5748     }
5749
5750   return TRUE;
5751 }
5752
5753 /* Fill in code and data in dynamic sections.  */
5754
5755 static bfd_boolean
5756 elf32_frv_finish_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
5757                                    struct bfd_link_info *info ATTRIBUTE_UNUSED)
5758 {
5759   /* Nothing to be done for non-FDPIC.  */
5760   return TRUE;
5761 }
5762
5763 static bfd_boolean
5764 elf32_frvfdpic_finish_dynamic_sections (bfd *output_bfd,
5765                                         struct bfd_link_info *info)
5766 {
5767   bfd *dynobj;
5768   asection *sdyn;
5769
5770   dynobj = elf_hash_table (info)->dynobj;
5771
5772   if (frvfdpic_dynamic_got_plt_info (info))
5773     {
5774       BFD_ASSERT (frvfdpic_dynamic_got_plt_info (info)->tls_ret_refs == 0);
5775     }
5776   if (frvfdpic_got_section (info))
5777     {
5778       BFD_ASSERT (frvfdpic_gotrel_section (info)->size
5779                   == (frvfdpic_gotrel_section (info)->reloc_count
5780                       * sizeof (Elf32_External_Rel)));
5781
5782       if (frvfdpic_gotfixup_section (info))
5783         {
5784           struct elf_link_hash_entry *hgot = elf_hash_table (info)->hgot;
5785           bfd_vma got_value = hgot->root.u.def.value
5786             + hgot->root.u.def.section->output_section->vma
5787             + hgot->root.u.def.section->output_offset;
5788           struct bfd_link_hash_entry *hend;
5789
5790           _frvfdpic_add_rofixup (output_bfd, frvfdpic_gotfixup_section (info),
5791                                  got_value, 0);
5792
5793           if (frvfdpic_gotfixup_section (info)->size
5794               != (frvfdpic_gotfixup_section (info)->reloc_count * 4))
5795             {
5796             error:
5797               (*_bfd_error_handler)
5798                 ("LINKER BUG: .rofixup section size mismatch");
5799               return FALSE;
5800             }
5801
5802           hend = bfd_link_hash_lookup (info->hash, "__ROFIXUP_END__",
5803                                        FALSE, FALSE, TRUE);
5804           if (hend
5805               && (hend->type == bfd_link_hash_defined
5806                   || hend->type == bfd_link_hash_defweak))
5807             {
5808               bfd_vma value = 
5809                 frvfdpic_gotfixup_section (info)->output_section->vma
5810                 + frvfdpic_gotfixup_section (info)->output_offset
5811                 + frvfdpic_gotfixup_section (info)->size
5812                 - hend->u.def.section->output_section->vma
5813                 - hend->u.def.section->output_offset;
5814               BFD_ASSERT (hend->u.def.value == value);
5815               if (hend->u.def.value != value)
5816                 goto error;
5817             }
5818         }
5819     }
5820   if (frvfdpic_pltrel_section (info))
5821     {
5822       BFD_ASSERT (frvfdpic_pltrel_section (info)->size
5823                   == (frvfdpic_pltrel_section (info)->reloc_count
5824                       * sizeof (Elf32_External_Rel)));
5825     }
5826
5827
5828   if (elf_hash_table (info)->dynamic_sections_created)
5829     {
5830       Elf32_External_Dyn * dyncon;
5831       Elf32_External_Dyn * dynconend;
5832
5833       sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
5834
5835       BFD_ASSERT (sdyn != NULL);
5836
5837       dyncon = (Elf32_External_Dyn *) sdyn->contents;
5838       dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
5839
5840       for (; dyncon < dynconend; dyncon++)
5841         {
5842           Elf_Internal_Dyn dyn;
5843
5844           bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
5845
5846           switch (dyn.d_tag)
5847             {
5848             default:
5849               break;
5850
5851             case DT_PLTGOT:
5852               dyn.d_un.d_ptr = frvfdpic_got_section (info)->output_section->vma
5853                 + frvfdpic_got_section (info)->output_offset
5854                 + frvfdpic_got_initial_offset (info);
5855               bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
5856               break;
5857
5858             case DT_JMPREL:
5859               dyn.d_un.d_ptr = frvfdpic_pltrel_section (info)
5860                 ->output_section->vma
5861                 + frvfdpic_pltrel_section (info)->output_offset;
5862               bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
5863               break;
5864
5865             case DT_PLTRELSZ:
5866               dyn.d_un.d_val = frvfdpic_pltrel_section (info)->size;
5867               bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
5868               break;
5869             }
5870         }
5871     }
5872
5873   return TRUE;
5874 }
5875
5876 /* Adjust a symbol defined by a dynamic object and referenced by a
5877    regular object.  */
5878
5879 static bfd_boolean
5880 elf32_frvfdpic_adjust_dynamic_symbol
5881 (struct bfd_link_info *info ATTRIBUTE_UNUSED,
5882  struct elf_link_hash_entry *h ATTRIBUTE_UNUSED)
5883 {
5884   bfd * dynobj;
5885
5886   dynobj = elf_hash_table (info)->dynobj;
5887
5888   /* Make sure we know what is going on here.  */
5889   BFD_ASSERT (dynobj != NULL
5890               && (h->u.weakdef != NULL
5891                   || (h->def_dynamic
5892                       && h->ref_regular
5893                       && !h->def_regular)));
5894
5895   /* If this is a weak symbol, and there is a real definition, the
5896      processor independent code will have arranged for us to see the
5897      real definition first, and we can just use the same value.  */
5898   if (h->u.weakdef != NULL)
5899     {
5900       BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
5901                   || h->u.weakdef->root.type == bfd_link_hash_defweak);
5902       h->root.u.def.section = h->u.weakdef->root.u.def.section;
5903       h->root.u.def.value = h->u.weakdef->root.u.def.value;
5904     }
5905
5906   return TRUE;
5907 }
5908
5909 /* Perform any actions needed for dynamic symbols.  */
5910
5911 static bfd_boolean
5912 elf32_frvfdpic_finish_dynamic_symbol
5913 (bfd *output_bfd ATTRIBUTE_UNUSED,
5914  struct bfd_link_info *info ATTRIBUTE_UNUSED,
5915  struct elf_link_hash_entry *h ATTRIBUTE_UNUSED,
5916  Elf_Internal_Sym *sym ATTRIBUTE_UNUSED)
5917 {
5918   return TRUE;
5919 }
5920
5921 /* Decide whether to attempt to turn absptr or lsda encodings in
5922    shared libraries into pcrel within the given input section.  */
5923
5924 static bfd_boolean
5925 frvfdpic_elf_use_relative_eh_frame
5926 (bfd *input_bfd ATTRIBUTE_UNUSED,
5927  struct bfd_link_info *info ATTRIBUTE_UNUSED,
5928  asection *eh_frame_section ATTRIBUTE_UNUSED)
5929 {
5930   /* We can't use PC-relative encodings in FDPIC binaries, in general.  */
5931   return FALSE;
5932 }
5933
5934 /* Adjust the contents of an eh_frame_hdr section before they're output.  */
5935
5936 static bfd_byte
5937 frvfdpic_elf_encode_eh_address (bfd *abfd,
5938                                 struct bfd_link_info *info,
5939                                 asection *osec, bfd_vma offset,
5940                                 asection *loc_sec, bfd_vma loc_offset,
5941                                 bfd_vma *encoded)
5942 {
5943   struct elf_link_hash_entry *h;
5944
5945   h = elf_hash_table (info)->hgot;
5946   BFD_ASSERT (h && h->root.type == bfd_link_hash_defined);
5947
5948   if (! h || (_frvfdpic_osec_to_segment (abfd, osec)
5949               == _frvfdpic_osec_to_segment (abfd, loc_sec->output_section)))
5950     return _bfd_elf_encode_eh_address (abfd, info, osec, offset,
5951                                        loc_sec, loc_offset, encoded);
5952
5953   BFD_ASSERT (_frvfdpic_osec_to_segment (abfd, osec)
5954               == (_frvfdpic_osec_to_segment
5955                   (abfd, h->root.u.def.section->output_section)));
5956
5957   *encoded = osec->vma + offset
5958     - (h->root.u.def.value
5959        + h->root.u.def.section->output_section->vma
5960        + h->root.u.def.section->output_offset);
5961
5962   return DW_EH_PE_datarel | DW_EH_PE_sdata4;
5963 }
5964
5965 /* Look through the relocs for a section during the first phase.
5966
5967    Besides handling virtual table relocs for gc, we have to deal with
5968    all sorts of PIC-related relocations.  We describe below the
5969    general plan on how to handle such relocations, even though we only
5970    collect information at this point, storing them in hash tables for
5971    perusal of later passes.
5972
5973    32 relocations are propagated to the linker output when creating
5974    position-independent output.  LO16 and HI16 relocations are not
5975    supposed to be encountered in this case.
5976
5977    LABEL16 should always be resolvable by the linker, since it's only
5978    used by branches.
5979
5980    LABEL24, on the other hand, is used by calls.  If it turns out that
5981    the target of a call is a dynamic symbol, a PLT entry must be
5982    created for it, which triggers the creation of a private function
5983    descriptor and, unless lazy binding is disabled, a lazy PLT entry.
5984
5985    GPREL relocations require the referenced symbol to be in the same
5986    segment as _gp, but this can only be checked later.
5987
5988    All GOT, GOTOFF and FUNCDESC relocations require a .got section to
5989    exist.  LABEL24 might as well, since it may require a PLT entry,
5990    that will require a got.
5991
5992    Non-FUNCDESC GOT relocations require a GOT entry to be created
5993    regardless of whether the symbol is dynamic.  However, since a
5994    global symbol that turns out to not be exported may have the same
5995    address of a non-dynamic symbol, we don't assign GOT entries at
5996    this point, such that we can share them in this case.  A relocation
5997    for the GOT entry always has to be created, be it to offset a
5998    private symbol by the section load address, be it to get the symbol
5999    resolved dynamically.
6000
6001    FUNCDESC GOT relocations require a GOT entry to be created, and
6002    handled as if a FUNCDESC relocation was applied to the GOT entry in
6003    an object file.
6004
6005    FUNCDESC relocations referencing a symbol that turns out to NOT be
6006    dynamic cause a private function descriptor to be created.  The
6007    FUNCDESC relocation then decays to a 32 relocation that points at
6008    the private descriptor.  If the symbol is dynamic, the FUNCDESC
6009    relocation is propagated to the linker output, such that the
6010    dynamic linker creates the canonical descriptor, pointing to the
6011    dynamically-resolved definition of the function.
6012
6013    Non-FUNCDESC GOTOFF relocations must always refer to non-dynamic
6014    symbols that are assigned to the same segment as the GOT, but we
6015    can only check this later, after we know the complete set of
6016    symbols defined and/or exported.
6017
6018    FUNCDESC GOTOFF relocations require a function descriptor to be
6019    created and, unless lazy binding is disabled or the symbol is not
6020    dynamic, a lazy PLT entry.  Since we can't tell at this point
6021    whether a symbol is going to be dynamic, we have to decide later
6022    whether to create a lazy PLT entry or bind the descriptor directly
6023    to the private function.
6024
6025    FUNCDESC_VALUE relocations are not supposed to be present in object
6026    files, but they may very well be simply propagated to the linker
6027    output, since they have no side effect.
6028
6029
6030    A function descriptor always requires a FUNCDESC_VALUE relocation.
6031    Whether it's in .plt.rel or not depends on whether lazy binding is
6032    enabled and on whether the referenced symbol is dynamic.
6033
6034    The existence of a lazy PLT requires the resolverStub lazy PLT
6035    entry to be present.
6036
6037
6038    As for assignment of GOT, PLT and lazy PLT entries, and private
6039    descriptors, we might do them all sequentially, but we can do
6040    better than that.  For example, we can place GOT entries and
6041    private function descriptors referenced using 12-bit operands
6042    closer to the PIC register value, such that these relocations don't
6043    overflow.  Those that are only referenced with LO16 relocations
6044    could come next, but we may as well place PLT-required function
6045    descriptors in the 12-bit range to make them shorter.  Symbols
6046    referenced with LO16/HI16 may come next, but we may place
6047    additional function descriptors in the 16-bit range if we can
6048    reliably tell that we've already placed entries that are ever
6049    referenced with only LO16.  PLT entries are therefore generated as
6050    small as possible, while not introducing relocation overflows in
6051    GOT or FUNCDESC_GOTOFF relocations.  Lazy PLT entries could be
6052    generated before or after PLT entries, but not intermingled with
6053    them, such that we can have more lazy PLT entries in range for a
6054    branch to the resolverStub.  The resolverStub should be emitted at
6055    the most distant location from the first lazy PLT entry such that
6056    it's still in range for a branch, or closer, if there isn't a need
6057    for so many lazy PLT entries.  Additional lazy PLT entries may be
6058    emitted after the resolverStub, as long as branches are still in
6059    range.  If the branch goes out of range, longer lazy PLT entries
6060    are emitted.
6061
6062    We could further optimize PLT and lazy PLT entries by giving them
6063    priority in assignment to closer-to-gr17 locations depending on the
6064    number of occurrences of references to them (assuming a function
6065    that's called more often is more important for performance, so its
6066    PLT entry should be faster), or taking hints from the compiler.
6067    Given infinite time and money... :-)  */
6068
6069 static bfd_boolean
6070 elf32_frv_check_relocs (abfd, info, sec, relocs)
6071      bfd *abfd;
6072      struct bfd_link_info *info;
6073      asection *sec;
6074      const Elf_Internal_Rela *relocs;
6075 {
6076   Elf_Internal_Shdr *symtab_hdr;
6077   struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
6078   const Elf_Internal_Rela *rel;
6079   const Elf_Internal_Rela *rel_end;
6080   bfd *dynobj;
6081   struct frvfdpic_relocs_info *picrel;
6082
6083   if (info->relocatable)
6084     return TRUE;
6085
6086   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
6087   sym_hashes = elf_sym_hashes (abfd);
6088   sym_hashes_end = sym_hashes + symtab_hdr->sh_size/sizeof(Elf32_External_Sym);
6089   if (!elf_bad_symtab (abfd))
6090     sym_hashes_end -= symtab_hdr->sh_info;
6091
6092   dynobj = elf_hash_table (info)->dynobj;
6093   rel_end = relocs + sec->reloc_count;
6094   for (rel = relocs; rel < rel_end; rel++)
6095     {
6096       struct elf_link_hash_entry *h;
6097       unsigned long r_symndx;
6098
6099       r_symndx = ELF32_R_SYM (rel->r_info);
6100       if (r_symndx < symtab_hdr->sh_info)
6101         h = NULL;
6102       else
6103         h = sym_hashes[r_symndx - symtab_hdr->sh_info];
6104
6105       switch (ELF32_R_TYPE (rel->r_info))
6106         {
6107         case R_FRV_GETTLSOFF:
6108         case R_FRV_TLSDESC_VALUE:
6109         case R_FRV_GOTTLSDESC12:
6110         case R_FRV_GOTTLSDESCHI:
6111         case R_FRV_GOTTLSDESCLO:
6112         case R_FRV_GOTTLSOFF12:
6113         case R_FRV_GOTTLSOFFHI:
6114         case R_FRV_GOTTLSOFFLO:
6115         case R_FRV_TLSOFF:
6116         case R_FRV_GOT12:
6117         case R_FRV_GOTHI:
6118         case R_FRV_GOTLO:
6119         case R_FRV_FUNCDESC_GOT12:
6120         case R_FRV_FUNCDESC_GOTHI:
6121         case R_FRV_FUNCDESC_GOTLO:
6122         case R_FRV_GOTOFF12:
6123         case R_FRV_GOTOFFHI:
6124         case R_FRV_GOTOFFLO:
6125         case R_FRV_FUNCDESC_GOTOFF12:
6126         case R_FRV_FUNCDESC_GOTOFFHI:
6127         case R_FRV_FUNCDESC_GOTOFFLO:
6128         case R_FRV_FUNCDESC:
6129         case R_FRV_FUNCDESC_VALUE:
6130         case R_FRV_TLSMOFF12:
6131         case R_FRV_TLSMOFFHI:
6132         case R_FRV_TLSMOFFLO:
6133         case R_FRV_TLSMOFF:
6134           if (! IS_FDPIC (abfd))
6135             goto bad_reloc;
6136           /* Fall through.  */
6137         case R_FRV_GPREL12:
6138         case R_FRV_GPRELU12:
6139         case R_FRV_GPRELHI:
6140         case R_FRV_GPRELLO:
6141         case R_FRV_LABEL24:
6142         case R_FRV_32:
6143           if (! dynobj)
6144             {
6145               elf_hash_table (info)->dynobj = dynobj = abfd;
6146               if (! _frv_create_got_section (abfd, info))
6147                 return FALSE;
6148             }
6149           if (! IS_FDPIC (abfd))
6150             {
6151               picrel = NULL;
6152               break;
6153             }
6154           if (h != NULL)
6155             {
6156               if (h->dynindx == -1)
6157                 switch (ELF_ST_VISIBILITY (h->other))
6158                   {
6159                   case STV_INTERNAL:
6160                   case STV_HIDDEN:
6161                     break;
6162                   default:
6163                     bfd_elf_link_record_dynamic_symbol (info, h);
6164                     break;
6165                   }
6166               picrel
6167                 = frvfdpic_relocs_info_for_global (frvfdpic_relocs_info (info),
6168                                                    abfd, h,
6169                                                    rel->r_addend, INSERT);
6170             }
6171           else
6172             picrel = frvfdpic_relocs_info_for_local (frvfdpic_relocs_info
6173                                                      (info), abfd, r_symndx,
6174                                                      rel->r_addend, INSERT);
6175           if (! picrel)
6176             return FALSE;
6177           break;
6178
6179         default:
6180           picrel = NULL;
6181           break;
6182         }
6183
6184       switch (ELF32_R_TYPE (rel->r_info))
6185         {
6186         case R_FRV_LABEL24:
6187           if (IS_FDPIC (abfd))
6188             picrel->call = 1;
6189           break;
6190
6191         case R_FRV_FUNCDESC_VALUE:
6192           picrel->relocsfdv++;
6193           if (bfd_get_section_flags (abfd, sec) & SEC_ALLOC)
6194             picrel->relocs32--;
6195           /* Fall through.  */
6196
6197         case R_FRV_32:
6198           if (! IS_FDPIC (abfd))
6199             break;
6200
6201           picrel->sym = 1;
6202           if (bfd_get_section_flags (abfd, sec) & SEC_ALLOC)
6203             picrel->relocs32++;
6204           break;
6205
6206         case R_FRV_GOT12:
6207           picrel->got12 = 1;
6208           break;
6209
6210         case R_FRV_GOTHI:
6211         case R_FRV_GOTLO:
6212           picrel->gothilo = 1;
6213           break;
6214
6215         case R_FRV_FUNCDESC_GOT12:
6216           picrel->fdgot12 = 1;
6217           break;
6218
6219         case R_FRV_FUNCDESC_GOTHI:
6220         case R_FRV_FUNCDESC_GOTLO:
6221           picrel->fdgothilo = 1;
6222           break;
6223
6224         case R_FRV_GOTOFF12:
6225         case R_FRV_GOTOFFHI:
6226         case R_FRV_GOTOFFLO:
6227           picrel->gotoff = 1;
6228           break;
6229
6230         case R_FRV_FUNCDESC_GOTOFF12:
6231           picrel->fdgoff12 = 1;
6232           break;
6233
6234         case R_FRV_FUNCDESC_GOTOFFHI:
6235         case R_FRV_FUNCDESC_GOTOFFLO:
6236           picrel->fdgoffhilo = 1;
6237           break;
6238
6239         case R_FRV_FUNCDESC:
6240           picrel->fd = 1;
6241           picrel->relocsfd++;
6242           break;
6243
6244         case R_FRV_GETTLSOFF:
6245           picrel->tlsplt = 1;
6246           break;
6247
6248         case R_FRV_TLSDESC_VALUE:
6249           picrel->relocstlsd++;
6250           goto bad_reloc;
6251           
6252         case R_FRV_GOTTLSDESC12:
6253           picrel->tlsdesc12 = 1;
6254           break;
6255
6256         case R_FRV_GOTTLSDESCHI:
6257         case R_FRV_GOTTLSDESCLO:
6258           picrel->tlsdeschilo = 1;
6259           break;
6260
6261         case R_FRV_TLSMOFF12:
6262         case R_FRV_TLSMOFFHI:
6263         case R_FRV_TLSMOFFLO:
6264         case R_FRV_TLSMOFF:
6265           break;
6266
6267         case R_FRV_GOTTLSOFF12:
6268           picrel->tlsoff12 = 1;
6269           info->flags |= DF_STATIC_TLS;
6270           break;
6271           
6272         case R_FRV_GOTTLSOFFHI:
6273         case R_FRV_GOTTLSOFFLO:
6274           picrel->tlsoffhilo = 1;
6275           info->flags |= DF_STATIC_TLS;
6276           break;
6277           
6278         case R_FRV_TLSOFF:
6279           picrel->relocstlsoff++;
6280           info->flags |= DF_STATIC_TLS;
6281           goto bad_reloc;
6282
6283         /* This relocation describes the C++ object vtable hierarchy.
6284            Reconstruct it for later use during GC.  */
6285         case R_FRV_GNU_VTINHERIT:
6286           if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
6287             return FALSE;
6288           break;
6289
6290         /* This relocation describes which C++ vtable entries are actually
6291            used.  Record for later use during GC.  */
6292         case R_FRV_GNU_VTENTRY:
6293           if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
6294             return FALSE;
6295           break;
6296
6297         case R_FRV_LABEL16:
6298         case R_FRV_LO16:
6299         case R_FRV_HI16:
6300         case R_FRV_GPREL12:
6301         case R_FRV_GPRELU12:
6302         case R_FRV_GPREL32:
6303         case R_FRV_GPRELHI:
6304         case R_FRV_GPRELLO:
6305         case R_FRV_TLSDESC_RELAX:
6306         case R_FRV_GETTLSOFF_RELAX:
6307         case R_FRV_TLSOFF_RELAX:
6308           break;
6309
6310         default:
6311         bad_reloc:
6312           (*_bfd_error_handler)
6313             (_("%B: unsupported relocation type %i"),
6314              abfd, ELF32_R_TYPE (rel->r_info));
6315           return FALSE;
6316         }
6317     }
6318
6319   return TRUE;
6320 }
6321
6322 \f
6323 /* Return the machine subcode from the ELF e_flags header.  */
6324
6325 static int
6326 elf32_frv_machine (abfd)
6327      bfd *abfd;
6328 {
6329   switch (elf_elfheader (abfd)->e_flags & EF_FRV_CPU_MASK)
6330     {
6331     default:                break;
6332     case EF_FRV_CPU_FR550:  return bfd_mach_fr550;
6333     case EF_FRV_CPU_FR500:  return bfd_mach_fr500;
6334     case EF_FRV_CPU_FR450:  return bfd_mach_fr450;
6335     case EF_FRV_CPU_FR405:  return bfd_mach_fr400;
6336     case EF_FRV_CPU_FR400:  return bfd_mach_fr400;
6337     case EF_FRV_CPU_FR300:  return bfd_mach_fr300;
6338     case EF_FRV_CPU_SIMPLE: return bfd_mach_frvsimple;
6339     case EF_FRV_CPU_TOMCAT: return bfd_mach_frvtomcat;
6340     }
6341
6342   return bfd_mach_frv;
6343 }
6344
6345 /* Set the right machine number for a FRV ELF file.  */
6346
6347 static bfd_boolean
6348 elf32_frv_object_p (abfd)
6349      bfd *abfd;
6350 {
6351   bfd_default_set_arch_mach (abfd, bfd_arch_frv, elf32_frv_machine (abfd));
6352   return (((elf_elfheader (abfd)->e_flags & EF_FRV_FDPIC) != 0)
6353           == (IS_FDPIC (abfd)));
6354 }
6355 \f
6356 /* Function to set the ELF flag bits.  */
6357
6358 static bfd_boolean
6359 frv_elf_set_private_flags (abfd, flags)
6360      bfd *abfd;
6361      flagword flags;
6362 {
6363   elf_elfheader (abfd)->e_flags = flags;
6364   elf_flags_init (abfd) = TRUE;
6365   return TRUE;
6366 }
6367
6368 /* Copy backend specific data from one object module to another.  */
6369
6370 static bfd_boolean
6371 frv_elf_copy_private_bfd_data (ibfd, obfd)
6372      bfd *ibfd;
6373      bfd *obfd;
6374 {
6375   if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
6376       || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
6377     return TRUE;
6378
6379   BFD_ASSERT (!elf_flags_init (obfd)
6380               || elf_elfheader (obfd)->e_flags == elf_elfheader (ibfd)->e_flags);
6381
6382   elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
6383   elf_flags_init (obfd) = TRUE;
6384   return TRUE;
6385 }
6386
6387 /* Return true if the architecture described by elf header flag
6388    EXTENSION is an extension of the architecture described by BASE.  */
6389
6390 static bfd_boolean
6391 frv_elf_arch_extension_p (flagword base, flagword extension)
6392 {
6393   if (base == extension)
6394     return TRUE;
6395
6396   /* CPU_GENERIC code can be merged with code for a specific
6397      architecture, in which case the result is marked as being
6398      for the specific architecture.  Everything is therefore
6399      an extension of CPU_GENERIC.  */
6400   if (base == EF_FRV_CPU_GENERIC)
6401     return TRUE;
6402
6403   if (extension == EF_FRV_CPU_FR450)
6404     if (base == EF_FRV_CPU_FR400 || base == EF_FRV_CPU_FR405)
6405       return TRUE;
6406
6407   if (extension == EF_FRV_CPU_FR405)
6408     if (base == EF_FRV_CPU_FR400)
6409       return TRUE;
6410
6411   return FALSE;
6412 }
6413
6414 static bfd_boolean
6415 elf32_frvfdpic_copy_private_bfd_data (bfd *ibfd, bfd *obfd)
6416 {
6417   unsigned i;
6418
6419   if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
6420       || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
6421     return TRUE;
6422
6423   if (! frv_elf_copy_private_bfd_data (ibfd, obfd))
6424     return FALSE;
6425
6426   if (! elf_tdata (ibfd) || ! elf_tdata (ibfd)->phdr
6427       || ! elf_tdata (obfd) || ! elf_tdata (obfd)->phdr)
6428     return TRUE;
6429
6430   /* Copy the stack size.  */
6431   for (i = 0; i < elf_elfheader (ibfd)->e_phnum; i++)
6432     if (elf_tdata (ibfd)->phdr[i].p_type == PT_GNU_STACK)
6433       {
6434         Elf_Internal_Phdr *iphdr = &elf_tdata (ibfd)->phdr[i];
6435
6436         for (i = 0; i < elf_elfheader (obfd)->e_phnum; i++)
6437           if (elf_tdata (obfd)->phdr[i].p_type == PT_GNU_STACK)
6438             {
6439               memcpy (&elf_tdata (obfd)->phdr[i], iphdr, sizeof (*iphdr));
6440
6441               /* Rewrite the phdrs, since we're only called after they
6442                  were first written.  */
6443               if (bfd_seek (obfd, (bfd_signed_vma) get_elf_backend_data (obfd)
6444                             ->s->sizeof_ehdr, SEEK_SET) != 0
6445                   || get_elf_backend_data (obfd)->s
6446                   ->write_out_phdrs (obfd, elf_tdata (obfd)->phdr,
6447                                      elf_elfheader (obfd)->e_phnum) != 0)
6448                 return FALSE;
6449               break;
6450             }
6451
6452         break;
6453       }
6454
6455   return TRUE;
6456 }
6457
6458 /* Merge backend specific data from an object file to the output
6459    object file when linking.  */
6460
6461 static bfd_boolean
6462 frv_elf_merge_private_bfd_data (ibfd, obfd)
6463      bfd *ibfd;
6464      bfd *obfd;
6465 {
6466   flagword old_flags, old_partial;
6467   flagword new_flags, new_partial;
6468   bfd_boolean error = FALSE;
6469   char new_opt[80];
6470   char old_opt[80];
6471
6472   new_opt[0] = old_opt[0] = '\0';
6473   new_flags = elf_elfheader (ibfd)->e_flags;
6474   old_flags = elf_elfheader (obfd)->e_flags;
6475
6476   if (new_flags & EF_FRV_FDPIC)
6477     new_flags &= ~EF_FRV_PIC;
6478
6479 #ifdef DEBUG
6480   (*_bfd_error_handler) ("old_flags = 0x%.8lx, new_flags = 0x%.8lx, init = %s, filename = %s",
6481                          old_flags, new_flags, elf_flags_init (obfd) ? "yes" : "no",
6482                          bfd_get_filename (ibfd));
6483 #endif
6484
6485   if (!elf_flags_init (obfd))                   /* First call, no flags set.  */
6486     {
6487       elf_flags_init (obfd) = TRUE;
6488       old_flags = new_flags;
6489     }
6490
6491   else if (new_flags == old_flags)              /* Compatible flags are ok.  */
6492     ;
6493
6494   else                                          /* Possibly incompatible flags.  */
6495     {
6496       /* Warn if different # of gprs are used.  Note, 0 means nothing is
6497          said about the size of gprs.  */
6498       new_partial = (new_flags & EF_FRV_GPR_MASK);
6499       old_partial = (old_flags & EF_FRV_GPR_MASK);
6500       if (new_partial == old_partial)
6501         ;
6502
6503       else if (new_partial == 0)
6504         ;
6505
6506       else if (old_partial == 0)
6507         old_flags |= new_partial;
6508
6509       else
6510         {
6511           switch (new_partial)
6512             {
6513             default:            strcat (new_opt, " -mgpr-??"); break;
6514             case EF_FRV_GPR_32: strcat (new_opt, " -mgpr-32"); break;
6515             case EF_FRV_GPR_64: strcat (new_opt, " -mgpr-64"); break;
6516             }
6517
6518           switch (old_partial)
6519             {
6520             default:            strcat (old_opt, " -mgpr-??"); break;
6521             case EF_FRV_GPR_32: strcat (old_opt, " -mgpr-32"); break;
6522             case EF_FRV_GPR_64: strcat (old_opt, " -mgpr-64"); break;
6523             }
6524         }
6525
6526       /* Warn if different # of fprs are used.  Note, 0 means nothing is
6527          said about the size of fprs.  */
6528       new_partial = (new_flags & EF_FRV_FPR_MASK);
6529       old_partial = (old_flags & EF_FRV_FPR_MASK);
6530       if (new_partial == old_partial)
6531         ;
6532
6533       else if (new_partial == 0)
6534         ;
6535
6536       else if (old_partial == 0)
6537         old_flags |= new_partial;
6538
6539       else
6540         {
6541           switch (new_partial)
6542             {
6543             default:              strcat (new_opt, " -mfpr-?");      break;
6544             case EF_FRV_FPR_32:   strcat (new_opt, " -mfpr-32");     break;
6545             case EF_FRV_FPR_64:   strcat (new_opt, " -mfpr-64");     break;
6546             case EF_FRV_FPR_NONE: strcat (new_opt, " -msoft-float"); break;
6547             }
6548
6549           switch (old_partial)
6550             {
6551             default:              strcat (old_opt, " -mfpr-?");      break;
6552             case EF_FRV_FPR_32:   strcat (old_opt, " -mfpr-32");     break;
6553             case EF_FRV_FPR_64:   strcat (old_opt, " -mfpr-64");     break;
6554             case EF_FRV_FPR_NONE: strcat (old_opt, " -msoft-float"); break;
6555             }
6556         }
6557
6558       /* Warn if different dword support was used.  Note, 0 means nothing is
6559          said about the dword support.  */
6560       new_partial = (new_flags & EF_FRV_DWORD_MASK);
6561       old_partial = (old_flags & EF_FRV_DWORD_MASK);
6562       if (new_partial == old_partial)
6563         ;
6564
6565       else if (new_partial == 0)
6566         ;
6567
6568       else if (old_partial == 0)
6569         old_flags |= new_partial;
6570
6571       else
6572         {
6573           switch (new_partial)
6574             {
6575             default:               strcat (new_opt, " -mdword-?");  break;
6576             case EF_FRV_DWORD_YES: strcat (new_opt, " -mdword");    break;
6577             case EF_FRV_DWORD_NO:  strcat (new_opt, " -mno-dword"); break;
6578             }
6579
6580           switch (old_partial)
6581             {
6582             default:               strcat (old_opt, " -mdword-?");  break;
6583             case EF_FRV_DWORD_YES: strcat (old_opt, " -mdword");    break;
6584             case EF_FRV_DWORD_NO:  strcat (old_opt, " -mno-dword"); break;
6585             }
6586         }
6587
6588       /* Or in flags that accumulate (ie, if one module uses it, mark that the
6589          feature is used.  */
6590       old_flags |= new_flags & (EF_FRV_DOUBLE
6591                                 | EF_FRV_MEDIA
6592                                 | EF_FRV_MULADD
6593                                 | EF_FRV_NON_PIC_RELOCS);
6594
6595       /* If any module was compiled without -G0, clear the G0 bit.  */
6596       old_flags = ((old_flags & ~ EF_FRV_G0)
6597                    | (old_flags & new_flags & EF_FRV_G0));
6598
6599       /* If any module was compiled without -mnopack, clear the mnopack bit.  */
6600       old_flags = ((old_flags & ~ EF_FRV_NOPACK)
6601                    | (old_flags & new_flags & EF_FRV_NOPACK));
6602
6603       /* We don't have to do anything if the pic flags are the same, or the new
6604          module(s) were compiled with -mlibrary-pic.  */
6605       new_partial = (new_flags & EF_FRV_PIC_FLAGS);
6606       old_partial = (old_flags & EF_FRV_PIC_FLAGS);
6607       if ((new_partial == old_partial) || ((new_partial & EF_FRV_LIBPIC) != 0))
6608         ;
6609
6610       /* If the old module(s) were compiled with -mlibrary-pic, copy in the pic
6611          flags if any from the new module.  */
6612       else if ((old_partial & EF_FRV_LIBPIC) != 0)
6613         old_flags = (old_flags & ~ EF_FRV_PIC_FLAGS) | new_partial;
6614
6615       /* If we have mixtures of -fpic and -fPIC, or in both bits.  */
6616       else if (new_partial != 0 && old_partial != 0)
6617         old_flags |= new_partial;
6618
6619       /* One module was compiled for pic and the other was not, see if we have
6620          had any relocations that are not pic-safe.  */
6621       else
6622         {
6623           if ((old_flags & EF_FRV_NON_PIC_RELOCS) == 0)
6624             old_flags |= new_partial;
6625           else
6626             {
6627               old_flags &= ~ EF_FRV_PIC_FLAGS;
6628 #ifndef FRV_NO_PIC_ERROR
6629               error = TRUE;
6630               (*_bfd_error_handler)
6631                 (_("%s: compiled with %s and linked with modules that use non-pic relocations"),
6632                  bfd_get_filename (ibfd),
6633                  (new_flags & EF_FRV_BIGPIC) ? "-fPIC" : "-fpic");
6634 #endif
6635             }
6636         }
6637
6638       /* Warn if different cpu is used (allow a specific cpu to override
6639          the generic cpu).  */
6640       new_partial = (new_flags & EF_FRV_CPU_MASK);
6641       old_partial = (old_flags & EF_FRV_CPU_MASK);
6642       if (frv_elf_arch_extension_p (new_partial, old_partial))
6643         ;
6644
6645       else if (frv_elf_arch_extension_p (old_partial, new_partial))
6646         old_flags = (old_flags & ~EF_FRV_CPU_MASK) | new_partial;
6647
6648       else
6649         {
6650           switch (new_partial)
6651             {
6652             default:                 strcat (new_opt, " -mcpu=?");      break;
6653             case EF_FRV_CPU_GENERIC: strcat (new_opt, " -mcpu=frv");    break;
6654             case EF_FRV_CPU_SIMPLE:  strcat (new_opt, " -mcpu=simple"); break;
6655             case EF_FRV_CPU_FR550:   strcat (new_opt, " -mcpu=fr550");  break;
6656             case EF_FRV_CPU_FR500:   strcat (new_opt, " -mcpu=fr500");  break;
6657             case EF_FRV_CPU_FR450:   strcat (new_opt, " -mcpu=fr450");  break;
6658             case EF_FRV_CPU_FR405:   strcat (new_opt, " -mcpu=fr405");  break;
6659             case EF_FRV_CPU_FR400:   strcat (new_opt, " -mcpu=fr400");  break;
6660             case EF_FRV_CPU_FR300:   strcat (new_opt, " -mcpu=fr300");  break;
6661             case EF_FRV_CPU_TOMCAT:  strcat (new_opt, " -mcpu=tomcat"); break;
6662             }
6663
6664           switch (old_partial)
6665             {
6666             default:                 strcat (old_opt, " -mcpu=?");      break;
6667             case EF_FRV_CPU_GENERIC: strcat (old_opt, " -mcpu=frv");    break;
6668             case EF_FRV_CPU_SIMPLE:  strcat (old_opt, " -mcpu=simple"); break;
6669             case EF_FRV_CPU_FR550:   strcat (old_opt, " -mcpu=fr550");  break;
6670             case EF_FRV_CPU_FR500:   strcat (old_opt, " -mcpu=fr500");  break;
6671             case EF_FRV_CPU_FR450:   strcat (old_opt, " -mcpu=fr450");  break;
6672             case EF_FRV_CPU_FR405:   strcat (old_opt, " -mcpu=fr405");  break;
6673             case EF_FRV_CPU_FR400:   strcat (old_opt, " -mcpu=fr400");  break;
6674             case EF_FRV_CPU_FR300:   strcat (old_opt, " -mcpu=fr300");  break;
6675             case EF_FRV_CPU_TOMCAT:  strcat (old_opt, " -mcpu=tomcat"); break;
6676             }
6677         }
6678
6679       /* Print out any mismatches from above.  */
6680       if (new_opt[0])
6681         {
6682           error = TRUE;
6683           (*_bfd_error_handler)
6684             (_("%s: compiled with %s and linked with modules compiled with %s"),
6685              bfd_get_filename (ibfd), new_opt, old_opt);
6686         }
6687
6688       /* Warn about any other mismatches */
6689       new_partial = (new_flags & ~ EF_FRV_ALL_FLAGS);
6690       old_partial = (old_flags & ~ EF_FRV_ALL_FLAGS);
6691       if (new_partial != old_partial)
6692         {
6693           old_flags |= new_partial;
6694           error = TRUE;
6695           (*_bfd_error_handler)
6696             (_("%s: uses different unknown e_flags (0x%lx) fields than previous modules (0x%lx)"),
6697              bfd_get_filename (ibfd), (long)new_partial, (long)old_partial);
6698         }
6699     }
6700
6701   /* If the cpu is -mcpu=simple, then set the -mnopack bit.  */
6702   if ((old_flags & EF_FRV_CPU_MASK) == EF_FRV_CPU_SIMPLE)
6703     old_flags |= EF_FRV_NOPACK;
6704
6705   /* Update the old flags now with changes made above.  */
6706   old_partial = elf_elfheader (obfd)->e_flags & EF_FRV_CPU_MASK;
6707   elf_elfheader (obfd)->e_flags = old_flags;
6708   if (old_partial != (old_flags & EF_FRV_CPU_MASK))
6709     bfd_default_set_arch_mach (obfd, bfd_arch_frv, elf32_frv_machine (obfd));
6710
6711   if (((new_flags & EF_FRV_FDPIC) == 0)
6712       != (! IS_FDPIC (ibfd)))
6713     {
6714       error = TRUE;
6715       if (IS_FDPIC (obfd))
6716         (*_bfd_error_handler)
6717           (_("%s: cannot link non-fdpic object file into fdpic executable"),
6718            bfd_get_filename (ibfd));
6719       else
6720         (*_bfd_error_handler)
6721           (_("%s: cannot link fdpic object file into non-fdpic executable"),
6722            bfd_get_filename (ibfd));
6723     }
6724
6725   if (error)
6726     bfd_set_error (bfd_error_bad_value);
6727
6728   return !error;
6729 }
6730
6731 \f
6732 bfd_boolean
6733 frv_elf_print_private_bfd_data (abfd, ptr)
6734      bfd *abfd;
6735      PTR ptr;
6736 {
6737   FILE *file = (FILE *) ptr;
6738   flagword flags;
6739
6740   BFD_ASSERT (abfd != NULL && ptr != NULL);
6741
6742   /* Print normal ELF private data.  */
6743   _bfd_elf_print_private_bfd_data (abfd, ptr);
6744
6745   flags = elf_elfheader (abfd)->e_flags;
6746   fprintf (file, _("private flags = 0x%lx:"), (long)flags);
6747
6748   switch (flags & EF_FRV_CPU_MASK)
6749     {
6750     default:                                                    break;
6751     case EF_FRV_CPU_SIMPLE: fprintf (file, " -mcpu=simple");    break;
6752     case EF_FRV_CPU_FR550:  fprintf (file, " -mcpu=fr550");     break;
6753     case EF_FRV_CPU_FR500:  fprintf (file, " -mcpu=fr500");     break;
6754     case EF_FRV_CPU_FR450:  fprintf (file, " -mcpu=fr450");     break;
6755     case EF_FRV_CPU_FR405:  fprintf (file, " -mcpu=fr405");     break;
6756     case EF_FRV_CPU_FR400:  fprintf (file, " -mcpu=fr400");     break;
6757     case EF_FRV_CPU_FR300:  fprintf (file, " -mcpu=fr300");     break;
6758     case EF_FRV_CPU_TOMCAT: fprintf (file, " -mcpu=tomcat");    break;
6759     }
6760
6761   switch (flags & EF_FRV_GPR_MASK)
6762     {
6763     default:                                                    break;
6764     case EF_FRV_GPR_32: fprintf (file, " -mgpr-32");            break;
6765     case EF_FRV_GPR_64: fprintf (file, " -mgpr-64");            break;
6766     }
6767
6768   switch (flags & EF_FRV_FPR_MASK)
6769     {
6770     default:                                                    break;
6771     case EF_FRV_FPR_32:   fprintf (file, " -mfpr-32");          break;
6772     case EF_FRV_FPR_64:   fprintf (file, " -mfpr-64");          break;
6773     case EF_FRV_FPR_NONE: fprintf (file, " -msoft-float");      break;
6774     }
6775
6776   switch (flags & EF_FRV_DWORD_MASK)
6777     {
6778     default:                                                    break;
6779     case EF_FRV_DWORD_YES: fprintf (file, " -mdword");          break;
6780     case EF_FRV_DWORD_NO:  fprintf (file, " -mno-dword");       break;
6781     }
6782
6783   if (flags & EF_FRV_DOUBLE)
6784     fprintf (file, " -mdouble");
6785
6786   if (flags & EF_FRV_MEDIA)
6787     fprintf (file, " -mmedia");
6788
6789   if (flags & EF_FRV_MULADD)
6790     fprintf (file, " -mmuladd");
6791
6792   if (flags & EF_FRV_PIC)
6793     fprintf (file, " -fpic");
6794
6795   if (flags & EF_FRV_BIGPIC)
6796     fprintf (file, " -fPIC");
6797
6798   if (flags & EF_FRV_LIBPIC)
6799     fprintf (file, " -mlibrary-pic");
6800
6801   if (flags & EF_FRV_FDPIC)
6802     fprintf (file, " -mfdpic");
6803
6804   if (flags & EF_FRV_NON_PIC_RELOCS)
6805     fprintf (file, " non-pic relocations");
6806
6807   if (flags & EF_FRV_G0)
6808     fprintf (file, " -G0");
6809
6810   fputc ('\n', file);
6811   return TRUE;
6812 }
6813
6814 \f
6815 #define ELF_ARCH                bfd_arch_frv
6816 #define ELF_MACHINE_CODE        EM_CYGNUS_FRV
6817 #define ELF_MAXPAGESIZE         0x1000
6818
6819 #define TARGET_BIG_SYM          bfd_elf32_frv_vec
6820 #define TARGET_BIG_NAME         "elf32-frv"
6821
6822 #define elf_info_to_howto                       frv_info_to_howto_rela
6823 #define elf_backend_relocate_section            elf32_frv_relocate_section
6824 #define elf_backend_gc_mark_hook                elf32_frv_gc_mark_hook
6825 #define elf_backend_gc_sweep_hook               elf32_frv_gc_sweep_hook
6826 #define elf_backend_check_relocs                elf32_frv_check_relocs
6827 #define elf_backend_object_p                    elf32_frv_object_p
6828 #define elf_backend_add_symbol_hook             elf32_frv_add_symbol_hook
6829
6830 #define elf_backend_can_gc_sections             1
6831 #define elf_backend_rela_normal                 1
6832
6833 #define bfd_elf32_bfd_reloc_type_lookup         frv_reloc_type_lookup
6834 #define bfd_elf32_bfd_set_private_flags         frv_elf_set_private_flags
6835 #define bfd_elf32_bfd_copy_private_bfd_data     frv_elf_copy_private_bfd_data
6836 #define bfd_elf32_bfd_merge_private_bfd_data    frv_elf_merge_private_bfd_data
6837 #define bfd_elf32_bfd_print_private_bfd_data    frv_elf_print_private_bfd_data
6838
6839 #define elf_backend_want_got_sym        1
6840 #define elf_backend_got_header_size     0
6841 #define elf_backend_want_got_plt        0
6842 #define elf_backend_plt_readonly        1
6843 #define elf_backend_want_plt_sym        0
6844 #define elf_backend_plt_header_size     0
6845
6846 #define elf_backend_finish_dynamic_sections \
6847                 elf32_frv_finish_dynamic_sections
6848
6849 #include "elf32-target.h"
6850
6851 #undef ELF_MAXPAGESIZE
6852 #define ELF_MAXPAGESIZE         0x4000
6853
6854 #undef TARGET_BIG_SYM
6855 #define TARGET_BIG_SYM          bfd_elf32_frvfdpic_vec
6856 #undef TARGET_BIG_NAME
6857 #define TARGET_BIG_NAME         "elf32-frvfdpic"
6858 #undef  elf32_bed
6859 #define elf32_bed               elf32_frvfdpic_bed
6860
6861 #undef elf_info_to_howto_rel
6862 #define elf_info_to_howto_rel   frvfdpic_info_to_howto_rel
6863
6864 #undef bfd_elf32_bfd_link_hash_table_create
6865 #define bfd_elf32_bfd_link_hash_table_create \
6866                 frvfdpic_elf_link_hash_table_create
6867 #undef elf_backend_always_size_sections
6868 #define elf_backend_always_size_sections \
6869                 elf32_frvfdpic_always_size_sections
6870 #undef elf_backend_modify_segment_map
6871 #define elf_backend_modify_segment_map \
6872                 elf32_frvfdpic_modify_segment_map
6873 #undef bfd_elf32_bfd_copy_private_bfd_data
6874 #define bfd_elf32_bfd_copy_private_bfd_data \
6875                 elf32_frvfdpic_copy_private_bfd_data
6876
6877 #undef elf_backend_create_dynamic_sections
6878 #define elf_backend_create_dynamic_sections \
6879                 elf32_frvfdpic_create_dynamic_sections
6880 #undef elf_backend_adjust_dynamic_symbol
6881 #define elf_backend_adjust_dynamic_symbol \
6882                 elf32_frvfdpic_adjust_dynamic_symbol
6883 #undef elf_backend_size_dynamic_sections
6884 #define elf_backend_size_dynamic_sections \
6885                 elf32_frvfdpic_size_dynamic_sections
6886 #undef bfd_elf32_bfd_relax_section
6887 #define bfd_elf32_bfd_relax_section \
6888   elf32_frvfdpic_relax_section
6889 #undef elf_backend_finish_dynamic_symbol
6890 #define elf_backend_finish_dynamic_symbol \
6891                 elf32_frvfdpic_finish_dynamic_symbol
6892 #undef elf_backend_finish_dynamic_sections
6893 #define elf_backend_finish_dynamic_sections \
6894                 elf32_frvfdpic_finish_dynamic_sections
6895
6896 #undef elf_backend_can_make_relative_eh_frame
6897 #define elf_backend_can_make_relative_eh_frame \
6898                 frvfdpic_elf_use_relative_eh_frame
6899 #undef elf_backend_can_make_lsda_relative_eh_frame
6900 #define elf_backend_can_make_lsda_relative_eh_frame \
6901                 frvfdpic_elf_use_relative_eh_frame
6902 #undef elf_backend_encode_eh_address
6903 #define elf_backend_encode_eh_address \
6904                 frvfdpic_elf_encode_eh_address
6905
6906 #undef elf_backend_may_use_rel_p
6907 #define elf_backend_may_use_rel_p       1
6908 #undef elf_backend_may_use_rela_p
6909 #define elf_backend_may_use_rela_p      1
6910 /* We use REL for dynamic relocations only.  */
6911 #undef elf_backend_default_use_rela_p
6912 #define elf_backend_default_use_rela_p  1
6913
6914 #undef elf_backend_omit_section_dynsym
6915 #define elf_backend_omit_section_dynsym _frvfdpic_link_omit_section_dynsym
6916
6917 #include "elf32-target.h"