OSDN Git Service

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