OSDN Git Service

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