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