PR 3958
[external/binutils.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 "bfd.h"
22 #include "sysdep.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 /* Set the howto pointer for an FRV ELF reloc.  */
2594
2595 static void
2596 frv_info_to_howto_rela (abfd, cache_ptr, dst)
2597      bfd *abfd ATTRIBUTE_UNUSED;
2598      arelent *cache_ptr;
2599      Elf_Internal_Rela *dst;
2600 {
2601   unsigned int r_type;
2602
2603   r_type = ELF32_R_TYPE (dst->r_info);
2604   switch (r_type)
2605     {
2606     case R_FRV_GNU_VTINHERIT:
2607       cache_ptr->howto = &elf32_frv_vtinherit_howto;
2608       break;
2609
2610     case R_FRV_GNU_VTENTRY:
2611       cache_ptr->howto = &elf32_frv_vtentry_howto;
2612       break;
2613
2614     default:
2615       cache_ptr->howto = & elf32_frv_howto_table [r_type];
2616       break;
2617     }
2618 }
2619
2620 /* Set the howto pointer for an FRV ELF REL reloc.  */
2621 static void
2622 frvfdpic_info_to_howto_rel (bfd *abfd ATTRIBUTE_UNUSED,
2623                             arelent *cache_ptr, Elf_Internal_Rela *dst)
2624 {
2625   unsigned int r_type;
2626
2627   r_type = ELF32_R_TYPE (dst->r_info);
2628   switch (r_type)
2629     {
2630     case R_FRV_32:
2631       cache_ptr->howto = &elf32_frv_rel_32_howto;
2632       break;
2633
2634     case R_FRV_FUNCDESC:
2635       cache_ptr->howto = &elf32_frv_rel_funcdesc_howto;
2636       break;
2637
2638     case R_FRV_FUNCDESC_VALUE:
2639       cache_ptr->howto = &elf32_frv_rel_funcdesc_value_howto;
2640       break;
2641
2642     case R_FRV_TLSDESC_VALUE:
2643       cache_ptr->howto = &elf32_frv_rel_tlsdesc_value_howto;
2644       break;
2645
2646     case R_FRV_TLSOFF:
2647       cache_ptr->howto = &elf32_frv_rel_tlsoff_howto;
2648       break;
2649
2650     default:
2651       cache_ptr->howto = NULL;
2652       break;
2653     }
2654 }
2655 \f
2656 /* Perform a single relocation.  By default we use the standard BFD
2657    routines, but a few relocs, we have to do them ourselves.  */
2658
2659 static bfd_reloc_status_type
2660 frv_final_link_relocate (howto, input_bfd, input_section, contents, rel,
2661                          relocation)
2662      reloc_howto_type *howto;
2663      bfd *input_bfd;
2664      asection *input_section;
2665      bfd_byte *contents;
2666      Elf_Internal_Rela *rel;
2667      bfd_vma relocation;
2668 {
2669   return _bfd_final_link_relocate (howto, input_bfd, input_section,
2670                                    contents, rel->r_offset, relocation,
2671                                    rel->r_addend);
2672 }
2673
2674 \f
2675 /* Relocate an FRV ELF section.
2676
2677    The RELOCATE_SECTION function is called by the new ELF backend linker
2678    to handle the relocations for a section.
2679
2680    The relocs are always passed as Rela structures; if the section
2681    actually uses Rel structures, the r_addend field will always be
2682    zero.
2683
2684    This function is responsible for adjusting the section contents as
2685    necessary, and (if using Rela relocs and generating a relocatable
2686    output file) adjusting the reloc addend as necessary.
2687
2688    This function does not have to worry about setting the reloc
2689    address or the reloc symbol index.
2690
2691    LOCAL_SYMS is a pointer to the swapped in local symbols.
2692
2693    LOCAL_SECTIONS is an array giving the section in the input file
2694    corresponding to the st_shndx field of each local symbol.
2695
2696    The global hash table entry for the global symbols can be found
2697    via elf_sym_hashes (input_bfd).
2698
2699    When generating relocatable output, this function must handle
2700    STB_LOCAL/STT_SECTION symbols specially.  The output symbol is
2701    going to be the section symbol corresponding to the output
2702    section, which means that the addend must be adjusted
2703    accordingly.  */
2704
2705 static bfd_boolean
2706 elf32_frv_relocate_section (output_bfd, info, input_bfd, input_section,
2707                             contents, relocs, local_syms, local_sections)
2708      bfd *output_bfd ATTRIBUTE_UNUSED;
2709      struct bfd_link_info *info;
2710      bfd *input_bfd;
2711      asection *input_section;
2712      bfd_byte *contents;
2713      Elf_Internal_Rela *relocs;
2714      Elf_Internal_Sym *local_syms;
2715      asection **local_sections;
2716 {
2717   Elf_Internal_Shdr *symtab_hdr;
2718   struct elf_link_hash_entry **sym_hashes;
2719   Elf_Internal_Rela *rel;
2720   Elf_Internal_Rela *relend;
2721   unsigned isec_segment, got_segment, plt_segment, gprel_segment, tls_segment,
2722     check_segment[2];
2723   int silence_segment_error = !(info->shared || info->pie);
2724   unsigned long insn;
2725
2726   symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
2727   sym_hashes = elf_sym_hashes (input_bfd);
2728   relend     = relocs + input_section->reloc_count;
2729
2730   isec_segment = _frvfdpic_osec_to_segment (output_bfd,
2731                                             input_section->output_section);
2732   if (IS_FDPIC (output_bfd) && frvfdpic_got_section (info))
2733     got_segment = _frvfdpic_osec_to_segment (output_bfd,
2734                                              frvfdpic_got_section (info)
2735                                              ->output_section);
2736   else
2737     got_segment = -1;
2738   if (IS_FDPIC (output_bfd) && frvfdpic_gotfixup_section (info))
2739     gprel_segment = _frvfdpic_osec_to_segment (output_bfd,
2740                                                frvfdpic_gotfixup_section (info)
2741                                                ->output_section);
2742   else
2743     gprel_segment = -1;
2744   if (IS_FDPIC (output_bfd) && frvfdpic_plt_section (info))
2745     plt_segment = _frvfdpic_osec_to_segment (output_bfd,
2746                                              frvfdpic_plt_section (info)
2747                                              ->output_section);
2748   else
2749     plt_segment = -1;
2750   if (elf_hash_table (info)->tls_sec)
2751     tls_segment = _frvfdpic_osec_to_segment (output_bfd,
2752                                              elf_hash_table (info)->tls_sec);
2753   else
2754     tls_segment = -1;
2755
2756   for (rel = relocs; rel < relend; rel ++)
2757     {
2758       reloc_howto_type *howto;
2759       unsigned long r_symndx;
2760       Elf_Internal_Sym *sym;
2761       asection *sec;
2762       struct elf_link_hash_entry *h;
2763       bfd_vma relocation;
2764       bfd_reloc_status_type r;
2765       const char * name = NULL;
2766       int r_type;
2767       asection *osec;
2768       struct frvfdpic_relocs_info *picrel;
2769       bfd_vma orig_addend = rel->r_addend;
2770
2771       r_type = ELF32_R_TYPE (rel->r_info);
2772
2773       if (   r_type == R_FRV_GNU_VTINHERIT
2774           || r_type == R_FRV_GNU_VTENTRY)
2775         continue;
2776
2777       r_symndx = ELF32_R_SYM (rel->r_info);
2778       howto  = elf32_frv_howto_table + ELF32_R_TYPE (rel->r_info);
2779       h      = NULL;
2780       sym    = NULL;
2781       sec    = NULL;
2782
2783       if (r_symndx < symtab_hdr->sh_info)
2784         {
2785           sym = local_syms + r_symndx;
2786           osec = sec = local_sections [r_symndx];
2787           relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
2788
2789           name = bfd_elf_string_from_elf_section
2790             (input_bfd, symtab_hdr->sh_link, sym->st_name);
2791           name = (name == NULL) ? bfd_section_name (input_bfd, sec) : name;
2792         }
2793       else
2794         {
2795           bfd_boolean warned;
2796           bfd_boolean unresolved_reloc;
2797
2798           RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
2799                                    r_symndx, symtab_hdr, sym_hashes,
2800                                    h, sec, relocation,
2801                                    unresolved_reloc, warned);
2802           osec = sec;
2803         }
2804
2805       if (sec != NULL && elf_discarded_section (sec))
2806         {
2807           /* For relocs against symbols from removed linkonce sections,
2808              or sections discarded by a linker script, we just want the
2809              section contents zeroed.  Avoid any special processing.  */
2810           _bfd_clear_contents (howto, input_bfd, contents + rel->r_offset);
2811           rel->r_info = 0;
2812           rel->r_addend = 0;
2813           continue;
2814         }
2815
2816       if (info->relocatable)
2817         continue;
2818
2819       if (r_type != R_FRV_TLSMOFF
2820           && h != NULL
2821           && (h->root.type == bfd_link_hash_defined
2822               || h->root.type == bfd_link_hash_defweak)
2823           && !FRVFDPIC_SYM_LOCAL (info, h))
2824         {
2825           osec = sec = NULL;
2826           relocation = 0;
2827         }
2828
2829       switch (r_type)
2830         {
2831         case R_FRV_LABEL24:
2832         case R_FRV_32:
2833           if (! IS_FDPIC (output_bfd))
2834             goto non_fdpic;
2835
2836         case R_FRV_GOT12:
2837         case R_FRV_GOTHI:
2838         case R_FRV_GOTLO:
2839         case R_FRV_FUNCDESC_GOT12:
2840         case R_FRV_FUNCDESC_GOTHI:
2841         case R_FRV_FUNCDESC_GOTLO:
2842         case R_FRV_GOTOFF12:
2843         case R_FRV_GOTOFFHI:
2844         case R_FRV_GOTOFFLO:
2845         case R_FRV_FUNCDESC_GOTOFF12:
2846         case R_FRV_FUNCDESC_GOTOFFHI:
2847         case R_FRV_FUNCDESC_GOTOFFLO:
2848         case R_FRV_FUNCDESC:
2849         case R_FRV_FUNCDESC_VALUE:
2850         case R_FRV_GETTLSOFF:
2851         case R_FRV_TLSDESC_VALUE:
2852         case R_FRV_GOTTLSDESC12:
2853         case R_FRV_GOTTLSDESCHI:
2854         case R_FRV_GOTTLSDESCLO:
2855         case R_FRV_TLSMOFF12:
2856         case R_FRV_TLSMOFFHI:
2857         case R_FRV_TLSMOFFLO:
2858         case R_FRV_GOTTLSOFF12:
2859         case R_FRV_GOTTLSOFFHI:
2860         case R_FRV_GOTTLSOFFLO:
2861         case R_FRV_TLSOFF:
2862         case R_FRV_TLSDESC_RELAX:
2863         case R_FRV_GETTLSOFF_RELAX:
2864         case R_FRV_TLSOFF_RELAX:
2865         case R_FRV_TLSMOFF:
2866           if (h != NULL)
2867             picrel = frvfdpic_relocs_info_for_global (frvfdpic_relocs_info
2868                                                       (info), input_bfd, h,
2869                                                       orig_addend, INSERT);
2870           else
2871             /* In order to find the entry we created before, we must
2872                use the original addend, not the one that may have been
2873                modified by _bfd_elf_rela_local_sym().  */
2874             picrel = frvfdpic_relocs_info_for_local (frvfdpic_relocs_info
2875                                                      (info), input_bfd, r_symndx,
2876                                                      orig_addend, INSERT);
2877           if (! picrel)
2878             return FALSE;
2879
2880           if (!_frvfdpic_emit_got_relocs_plt_entries (picrel, output_bfd, info,
2881                                                       osec, sym,
2882                                                       rel->r_addend))
2883             {
2884               (*_bfd_error_handler)
2885                 (_("%B(%A+0x%x): relocation to `%s+%x' may have caused the error above"),
2886                  input_bfd, input_section, rel->r_offset, name, rel->r_addend);
2887               return FALSE;
2888             }
2889
2890           break;
2891
2892         default:
2893         non_fdpic:
2894           picrel = NULL;
2895           if (h && ! FRVFDPIC_SYM_LOCAL (info, h))
2896             {
2897               info->callbacks->warning
2898                 (info, _("relocation references symbol not defined in the module"),
2899                  name, input_bfd, input_section, rel->r_offset);
2900               return FALSE;
2901             }
2902           break;
2903         }
2904
2905       switch (r_type)
2906         {
2907         case R_FRV_GETTLSOFF:
2908         case R_FRV_TLSDESC_VALUE:
2909         case R_FRV_GOTTLSDESC12:
2910         case R_FRV_GOTTLSDESCHI:
2911         case R_FRV_GOTTLSDESCLO:
2912         case R_FRV_TLSMOFF12:
2913         case R_FRV_TLSMOFFHI:
2914         case R_FRV_TLSMOFFLO:
2915         case R_FRV_GOTTLSOFF12:
2916         case R_FRV_GOTTLSOFFHI:
2917         case R_FRV_GOTTLSOFFLO:
2918         case R_FRV_TLSOFF:
2919         case R_FRV_TLSDESC_RELAX:
2920         case R_FRV_GETTLSOFF_RELAX:
2921         case R_FRV_TLSOFF_RELAX:
2922         case R_FRV_TLSMOFF:
2923           if (sec && (bfd_is_abs_section (sec) || bfd_is_und_section (sec)))
2924             relocation += tls_biased_base (info);
2925           break;
2926
2927         default:
2928           break;
2929         }
2930
2931       /* Try to apply TLS relaxations.  */
2932       if (1)
2933         switch (r_type)
2934           {
2935
2936 #define LOCAL_EXEC_P(info, picrel) \
2937   ((info)->executable \
2938    && (picrel->symndx != -1 || FRVFDPIC_SYM_LOCAL ((info), (picrel)->d.h)))
2939 #define INITIAL_EXEC_P(info, picrel) \
2940   (((info)->executable || (info)->flags & DF_STATIC_TLS) \
2941    && (picrel)->tlsoff_entry)
2942
2943 #define IN_RANGE_FOR_OFST12_P(value) \
2944   ((bfd_vma)((value) + 2048) < (bfd_vma)4096)
2945 #define IN_RANGE_FOR_SETLOS_P(value) \
2946   ((bfd_vma)((value) + 32768) < (bfd_vma)65536)
2947 #define TLSMOFF_IN_RANGE_FOR_SETLOS_P(value, info) \
2948   (IN_RANGE_FOR_SETLOS_P ((value) - tls_biased_base (info)))
2949
2950 #define RELAX_GETTLSOFF_LOCAL_EXEC_P(info, picrel, value) \
2951   (LOCAL_EXEC_P ((info), (picrel)) \
2952    && TLSMOFF_IN_RANGE_FOR_SETLOS_P((value), (info)))
2953 #define RELAX_GETTLSOFF_INITIAL_EXEC_P(info, picrel) \
2954   (INITIAL_EXEC_P ((info), (picrel)) \
2955    && IN_RANGE_FOR_OFST12_P ((picrel)->tlsoff_entry))
2956
2957 #define RELAX_TLSDESC_LOCAL_EXEC_P(info, picrel, value) \
2958   (LOCAL_EXEC_P ((info), (picrel)))
2959 #define RELAX_TLSDESC_INITIAL_EXEC_P(info, picrel) \
2960   (INITIAL_EXEC_P ((info), (picrel)))
2961
2962 #define RELAX_GOTTLSOFF_LOCAL_EXEC_P(info, picrel, value) \
2963   (LOCAL_EXEC_P ((info), (picrel)) \
2964    && TLSMOFF_IN_RANGE_FOR_SETLOS_P((value), (info)))
2965
2966           case R_FRV_GETTLSOFF:
2967             insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
2968
2969             /* Is this a call instruction?  */
2970             if ((insn & (unsigned long)0x01fc0000) != 0x003c0000)
2971               {
2972                 r = info->callbacks->warning
2973                   (info,
2974                    _("R_FRV_GETTLSOFF not applied to a call instruction"),
2975                    name, input_bfd, input_section, rel->r_offset);
2976                 return FALSE;
2977               }
2978
2979             if (RELAX_GETTLSOFF_LOCAL_EXEC_P (info, picrel,
2980                                               relocation + rel->r_addend))
2981               {
2982                 /* Replace the call instruction (except the packing bit)
2983                    with setlos #tlsmofflo(symbol+offset), gr9.  */
2984                 insn &= (unsigned long)0x80000000;
2985                 insn |= (unsigned long)0x12fc0000;
2986                 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
2987
2988                 r_type = R_FRV_TLSMOFFLO;
2989                 howto  = elf32_frv_howto_table + r_type;
2990                 rel->r_info = ELF32_R_INFO (r_symndx, r_type);
2991               }
2992
2993             else if (RELAX_GETTLSOFF_INITIAL_EXEC_P (info, picrel))
2994               {
2995                 /* Replace the call instruction (except the packing bit)
2996                    with ldi @(gr15, #gottlsoff12(symbol+addend)), gr9.  */
2997                 insn &= (unsigned long)0x80000000;
2998                 insn |= (unsigned long)0x12c8f000;
2999                 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
3000
3001                 r_type = R_FRV_GOTTLSOFF12;
3002                 howto  = elf32_frv_howto_table + r_type;
3003                 rel->r_info = ELF32_R_INFO (r_symndx, r_type);
3004               }
3005
3006             break;
3007
3008           case R_FRV_GOTTLSDESC12:
3009             insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
3010
3011             /* Is this an lddi instruction?  */
3012             if ((insn & (unsigned long)0x01fc0000) != 0x00cc0000)
3013               {
3014                 r = info->callbacks->warning
3015                   (info,
3016                    _("R_FRV_GOTTLSDESC12 not applied to an lddi instruction"),
3017                    name, input_bfd, input_section, rel->r_offset);
3018                 return FALSE;
3019               }
3020
3021             if (RELAX_TLSDESC_LOCAL_EXEC_P (info, picrel,
3022                                             relocation + rel->r_addend)
3023                 && TLSMOFF_IN_RANGE_FOR_SETLOS_P (relocation + rel->r_addend,
3024                                                   info))
3025               {
3026                 /* Replace lddi @(grB, #gottlsdesc12(symbol+offset), grC
3027                    with setlos #tlsmofflo(symbol+offset), gr<C+1>.
3028                    Preserve the packing bit.  */
3029                 insn = (insn & (unsigned long)0x80000000)
3030                   | ((insn + (unsigned long)0x02000000)
3031                      & (unsigned long)0x7e000000);
3032                 insn |= (unsigned long)0x00fc0000;
3033                 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
3034
3035                 r_type = R_FRV_TLSMOFFLO;
3036                 howto  = elf32_frv_howto_table + r_type;
3037                 rel->r_info = ELF32_R_INFO (r_symndx, r_type);
3038               }
3039
3040             else if (RELAX_TLSDESC_LOCAL_EXEC_P (info, picrel,
3041                                                  relocation + rel->r_addend))
3042               {
3043                 /* Replace lddi @(grB, #gottlsdesc12(symbol+offset), grC
3044                    with sethi #tlsmoffhi(symbol+offset), gr<C+1>.
3045                    Preserve the packing bit.  */
3046                 insn = (insn & (unsigned long)0x80000000)
3047                   | ((insn + (unsigned long)0x02000000)
3048                      & (unsigned long)0x7e000000);
3049                 insn |= (unsigned long)0x00f80000;
3050                 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
3051
3052                 r_type = R_FRV_TLSMOFFHI;
3053                 howto  = elf32_frv_howto_table + r_type;
3054                 rel->r_info = ELF32_R_INFO (r_symndx, r_type);
3055               }
3056
3057             else if (RELAX_TLSDESC_INITIAL_EXEC_P (info, picrel))
3058               {
3059                 /* Replace lddi @(grB, #gottlsdesc12(symbol+offset), grC
3060                    with ldi @(grB, #gottlsoff12(symbol+offset),
3061                    gr<C+1>.  Preserve the packing bit.  If gottlsoff12
3062                    overflows, we'll error out, but that's sort-of ok,
3063                    since we'd started with gottlsdesc12, that's actually
3064                    more demanding.  Compiling with -fPIE instead of
3065                    -fpie would fix it; linking with --relax should fix
3066                    it as well.  */
3067                 insn = (insn & (unsigned long)0x80cbf000)
3068                   | ((insn + (unsigned long)0x02000000)
3069                      & (unsigned long)0x7e000000);
3070                 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
3071
3072                 r_type = R_FRV_GOTTLSOFF12;
3073                 howto  = elf32_frv_howto_table + r_type;
3074                 rel->r_info = ELF32_R_INFO (r_symndx, r_type);
3075               }
3076
3077             break;
3078
3079           case R_FRV_GOTTLSDESCHI:
3080             insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
3081
3082             /* Is this a sethi instruction?  */
3083             if ((insn & (unsigned long)0x01ff0000) != 0x00f80000)
3084               {
3085                 r = info->callbacks->warning
3086                   (info,
3087                    _("R_FRV_GOTTLSDESCHI not applied to a sethi instruction"),
3088                    name, input_bfd, input_section, rel->r_offset);
3089                 return FALSE;
3090               }
3091
3092             if (RELAX_TLSDESC_LOCAL_EXEC_P (info, picrel,
3093                                             relocation + rel->r_addend)
3094                 || (RELAX_TLSDESC_INITIAL_EXEC_P (info, picrel)
3095                     && IN_RANGE_FOR_SETLOS_P (picrel->tlsoff_entry)))
3096               {
3097                 /* Replace sethi with a nop.  Preserve the packing bit.  */
3098                 insn &= (unsigned long)0x80000000;
3099                 insn |= (unsigned long)0x00880000;
3100                 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
3101
3102                 /* Nothing to relocate.  */
3103                 continue;
3104               }
3105
3106             else if (RELAX_TLSDESC_INITIAL_EXEC_P (info, picrel))
3107               {
3108                 /* Simply decay GOTTLSDESC to GOTTLSOFF.  */
3109                 r_type = R_FRV_GOTTLSOFFHI;
3110                 howto  = elf32_frv_howto_table + r_type;
3111                 rel->r_info = ELF32_R_INFO (r_symndx, r_type);
3112               }
3113
3114             break;
3115
3116           case R_FRV_GOTTLSDESCLO:
3117             insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
3118
3119             /* Is this a setlo or setlos instruction?  */
3120             if ((insn & (unsigned long)0x01f70000) != 0x00f40000)
3121               {
3122                 r = info->callbacks->warning
3123                   (info,
3124                    _("R_FRV_GOTTLSDESCLO"
3125                      " not applied to a setlo or setlos instruction"),
3126                    name, input_bfd, input_section, rel->r_offset);
3127                 return FALSE;
3128               }
3129
3130             if (RELAX_TLSDESC_LOCAL_EXEC_P (info, picrel,
3131                                             relocation + rel->r_addend)
3132                 || (RELAX_TLSDESC_INITIAL_EXEC_P (info, picrel)
3133                     && IN_RANGE_FOR_OFST12_P (picrel->tlsoff_entry)))
3134               {
3135                 /* Replace setlo/setlos with a nop.  Preserve the
3136                    packing bit.  */
3137                 insn &= (unsigned long)0x80000000;
3138                 insn |= (unsigned long)0x00880000;
3139                 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
3140
3141                 /* Nothing to relocate.  */
3142                 continue;
3143               }
3144
3145             else if (RELAX_TLSDESC_INITIAL_EXEC_P (info, picrel))
3146               {
3147                 /* If the corresponding sethi (if it exists) decayed
3148                    to a nop, make sure this becomes (or already is) a
3149                    setlos, not setlo.  */
3150                 if (IN_RANGE_FOR_SETLOS_P (picrel->tlsoff_entry))
3151                   {
3152                     insn |= (unsigned long)0x00080000;
3153                     bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
3154                   }
3155
3156                 /* Simply decay GOTTLSDESC to GOTTLSOFF.  */
3157                 r_type = R_FRV_GOTTLSOFFLO;
3158                 howto  = elf32_frv_howto_table + r_type;
3159                 rel->r_info = ELF32_R_INFO (r_symndx, r_type);
3160               }
3161
3162             break;
3163
3164           case R_FRV_TLSDESC_RELAX:
3165             insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
3166
3167             /* Is this an ldd instruction?  */
3168             if ((insn & (unsigned long)0x01fc0fc0) != 0x00080140)
3169               {
3170                 r = info->callbacks->warning
3171                   (info,
3172                    _("R_FRV_TLSDESC_RELAX not applied to an ldd instruction"),
3173                    name, input_bfd, input_section, rel->r_offset);
3174                 return FALSE;
3175               }
3176
3177             if (RELAX_TLSDESC_LOCAL_EXEC_P (info, picrel,
3178                                             relocation + rel->r_addend)
3179                 && TLSMOFF_IN_RANGE_FOR_SETLOS_P (relocation + rel->r_addend,
3180                                                   info))
3181               {
3182                 /* Replace ldd #tlsdesc(symbol+offset)@(grB, grA), grC
3183                    with setlos #tlsmofflo(symbol+offset), gr<C+1>.
3184                    Preserve the packing bit.  */
3185                 insn = (insn & (unsigned long)0x80000000)
3186                   | ((insn + (unsigned long)0x02000000)
3187                      & (unsigned long)0x7e000000);
3188                 insn |= (unsigned long)0x00fc0000;
3189                 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
3190
3191                 r_type = R_FRV_TLSMOFFLO;
3192                 howto  = elf32_frv_howto_table + r_type;
3193                 rel->r_info = ELF32_R_INFO (r_symndx, r_type);
3194               }
3195
3196             else if (RELAX_TLSDESC_LOCAL_EXEC_P (info, picrel,
3197                                                  relocation + rel->r_addend))
3198               {
3199                 /* Replace ldd #tlsdesc(symbol+offset)@(grB, grA), grC
3200                    with sethi #tlsmoffhi(symbol+offset), gr<C+1>.
3201                    Preserve the packing bit.  */
3202                 insn = (insn & (unsigned long)0x80000000)
3203                   | ((insn + (unsigned long)0x02000000)
3204                      & (unsigned long)0x7e000000);
3205                 insn |= (unsigned long)0x00f80000;
3206                 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
3207
3208                 r_type = R_FRV_TLSMOFFHI;
3209                 howto  = elf32_frv_howto_table + r_type;
3210                 rel->r_info = ELF32_R_INFO (r_symndx, r_type);
3211               }
3212
3213             else if (RELAX_TLSDESC_INITIAL_EXEC_P (info, picrel)
3214                      && IN_RANGE_FOR_OFST12_P (picrel->tlsoff_entry))
3215               {
3216                 /* Replace ldd #tlsdesc(symbol+offset)@(grB, grA), grC
3217                    with ldi @(grB, #gottlsoff12(symbol+offset), gr<C+1>.
3218                    Preserve the packing bit.  */
3219                 insn = (insn & (unsigned long)0x8003f000)
3220                   | (unsigned long)0x00c80000
3221                   | ((insn + (unsigned long)0x02000000)
3222                      & (unsigned long)0x7e000000);
3223                 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
3224
3225                 r_type = R_FRV_GOTTLSOFF12;
3226                 howto  = elf32_frv_howto_table + r_type;
3227                 rel->r_info = ELF32_R_INFO (r_symndx, r_type);
3228               }
3229
3230             else if (RELAX_TLSDESC_INITIAL_EXEC_P (info, picrel))
3231               {
3232                 /* Replace ldd #tlsdesc(symbol+offset)@(grB, grA), grC
3233                    with ld #tlsoff(symbol+offset)@(grB, grA), gr<C+1>.
3234                    Preserve the packing bit.  */
3235                 insn = (insn & (unsigned long)0x81ffffbf)
3236                   | ((insn + (unsigned long)0x02000000)
3237                      & (unsigned long)0x7e000000);
3238                 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
3239
3240                 /* #tlsoff(symbol+offset) is just a relaxation
3241                     annotation, so there's nothing left to
3242                     relocate.  */
3243                 continue;
3244               }
3245
3246             break;
3247
3248           case R_FRV_GETTLSOFF_RELAX:
3249             insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
3250
3251             /* Is this a calll or callil instruction?  */
3252             if ((insn & (unsigned long)0x7ff80fc0) != 0x02300000)
3253               {
3254                 r = info->callbacks->warning
3255                   (info,
3256                    _("R_FRV_GETTLSOFF_RELAX"
3257                      " not applied to a calll instruction"),
3258                    name, input_bfd, input_section, rel->r_offset);
3259                 return FALSE;
3260               }
3261
3262             if (RELAX_TLSDESC_LOCAL_EXEC_P (info, picrel,
3263                                             relocation + rel->r_addend)
3264                 && TLSMOFF_IN_RANGE_FOR_SETLOS_P (relocation + rel->r_addend,
3265                                                   info))
3266               {
3267                 /* Replace calll with a nop.  Preserve the packing bit.  */
3268                 insn &= (unsigned long)0x80000000;
3269                 insn |= (unsigned long)0x00880000;
3270                 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
3271
3272                 /* Nothing to relocate.  */
3273                 continue;
3274               }
3275
3276             else if (RELAX_TLSDESC_LOCAL_EXEC_P (info, picrel,
3277                                                  relocation + rel->r_addend))
3278               {
3279                 /* Replace calll with setlo #tlsmofflo(symbol+offset), gr9.
3280                    Preserve the packing bit.  */
3281                 insn &= (unsigned long)0x80000000;
3282                 insn |= (unsigned long)0x12f40000;
3283                 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
3284
3285                 r_type = R_FRV_TLSMOFFLO;
3286                 howto  = elf32_frv_howto_table + r_type;
3287                 rel->r_info = ELF32_R_INFO (r_symndx, r_type);
3288               }
3289
3290             else if (RELAX_TLSDESC_INITIAL_EXEC_P (info, picrel))
3291               {
3292                 /* Replace calll with a nop.  Preserve the packing bit.  */
3293                 insn &= (unsigned long)0x80000000;
3294                 insn |= (unsigned long)0x00880000;
3295                 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
3296
3297                 /* Nothing to relocate.  */
3298                 continue;
3299               }
3300
3301             break;
3302
3303           case R_FRV_GOTTLSOFF12:
3304             insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
3305
3306             /* Is this an ldi instruction?  */
3307             if ((insn & (unsigned long)0x01fc0000) != 0x00c80000)
3308               {
3309                 r = info->callbacks->warning
3310                   (info,
3311                    _("R_FRV_GOTTLSOFF12 not applied to an ldi instruction"),
3312                    name, input_bfd, input_section, rel->r_offset);
3313                 return FALSE;
3314               }
3315
3316             if (RELAX_GOTTLSOFF_LOCAL_EXEC_P (info, picrel,
3317                                               relocation + rel->r_addend))
3318               {
3319                 /* Replace ldi @(grB, #gottlsoff12(symbol+offset), grC
3320                    with setlos #tlsmofflo(symbol+offset), grC.
3321                    Preserve the packing bit.  */
3322                 insn &= (unsigned long)0xfe000000;
3323                 insn |= (unsigned long)0x00fc0000;
3324                 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
3325
3326                 r_type = R_FRV_TLSMOFFLO;
3327                 howto  = elf32_frv_howto_table + r_type;
3328                 rel->r_info = ELF32_R_INFO (r_symndx, r_type);
3329               }
3330
3331             break;
3332
3333           case R_FRV_GOTTLSOFFHI:
3334             insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
3335
3336             /* Is this a sethi instruction?  */
3337             if ((insn & (unsigned long)0x01ff0000) != 0x00f80000)
3338               {
3339                 r = info->callbacks->warning
3340                   (info,
3341                    _("R_FRV_GOTTLSOFFHI not applied to a sethi instruction"),
3342                    name, input_bfd, input_section, rel->r_offset);
3343                 return FALSE;
3344               }
3345
3346             if (RELAX_GOTTLSOFF_LOCAL_EXEC_P (info, picrel,
3347                                               relocation + rel->r_addend)
3348                 || (RELAX_TLSDESC_INITIAL_EXEC_P (info, picrel)
3349                     && IN_RANGE_FOR_OFST12_P (picrel->tlsoff_entry)))
3350               {
3351                 /* Replace sethi with a nop.  Preserve the packing bit.  */
3352                 insn &= (unsigned long)0x80000000;
3353                 insn |= (unsigned long)0x00880000;
3354                 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
3355
3356                 /* Nothing to relocate.  */
3357                 continue;
3358               }
3359
3360             break;
3361
3362           case R_FRV_GOTTLSOFFLO:
3363             insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
3364
3365             /* Is this a setlo or setlos instruction?  */
3366             if ((insn & (unsigned long)0x01f70000) != 0x00f40000)
3367               {
3368                 r = info->callbacks->warning
3369                   (info,
3370                    _("R_FRV_GOTTLSOFFLO"
3371                      " not applied to a setlo or setlos instruction"),
3372                    name, input_bfd, input_section, rel->r_offset);
3373                 return FALSE;
3374               }
3375
3376             if (RELAX_GOTTLSOFF_LOCAL_EXEC_P (info, picrel,
3377                                               relocation + rel->r_addend)
3378                 || (RELAX_TLSDESC_INITIAL_EXEC_P (info, picrel)
3379                     && IN_RANGE_FOR_OFST12_P (picrel->tlsoff_entry)))
3380               {
3381                 /* Replace setlo/setlos with a nop.  Preserve the
3382                    packing bit.  */
3383                 insn &= (unsigned long)0x80000000;
3384                 insn |= (unsigned long)0x00880000;
3385                 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
3386
3387                 /* Nothing to relocate.  */
3388                 continue;
3389               }
3390
3391             break;
3392
3393           case R_FRV_TLSOFF_RELAX:
3394             insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
3395
3396             /* Is this an ld instruction?  */
3397             if ((insn & (unsigned long)0x01fc0fc0) != 0x00080100)
3398               {
3399                 r = info->callbacks->warning
3400                   (info,
3401                    _("R_FRV_TLSOFF_RELAX not applied to an ld instruction"),
3402                    name, input_bfd, input_section, rel->r_offset);
3403                 return FALSE;
3404               }
3405
3406             if (RELAX_GOTTLSOFF_LOCAL_EXEC_P (info, picrel,
3407                                               relocation + rel->r_addend))
3408               {
3409                 /* Replace ld #gottlsoff(symbol+offset)@(grB, grA), grC
3410                    with setlos #tlsmofflo(symbol+offset), grC.
3411                    Preserve the packing bit.  */
3412                 insn &= (unsigned long)0xfe000000;
3413                 insn |= (unsigned long)0x00fc0000;
3414                 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
3415
3416                 r_type = R_FRV_TLSMOFFLO;
3417                 howto  = elf32_frv_howto_table + r_type;
3418                 rel->r_info = ELF32_R_INFO (r_symndx, r_type);
3419               }
3420
3421             else if (RELAX_TLSDESC_INITIAL_EXEC_P (info, picrel)
3422                      && IN_RANGE_FOR_OFST12_P (picrel->tlsoff_entry))
3423               {
3424                 /* Replace ld #tlsoff(symbol+offset)@(grB, grA), grC
3425                    with ldi @(grB, #gottlsoff12(symbol+offset), grC.
3426                    Preserve the packing bit.  */
3427                 insn = (insn & (unsigned long)0xfe03f000)
3428                   | (unsigned long)0x00c80000;;
3429                 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
3430
3431                 r_type = R_FRV_GOTTLSOFF12;
3432                 howto  = elf32_frv_howto_table + r_type;
3433                 rel->r_info = ELF32_R_INFO (r_symndx, r_type);
3434               }
3435
3436             break;
3437
3438           case R_FRV_TLSMOFFHI:
3439             insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
3440
3441             /* Is this a sethi instruction?  */
3442             if ((insn & (unsigned long)0x01ff0000) != 0x00f80000)
3443               {
3444                 r = info->callbacks->warning
3445                   (info,
3446                    _("R_FRV_TLSMOFFHI not applied to a sethi instruction"),
3447                    name, input_bfd, input_section, rel->r_offset);
3448                 return FALSE;
3449               }
3450
3451             if (TLSMOFF_IN_RANGE_FOR_SETLOS_P (relocation + rel->r_addend,
3452                                                info))
3453               {
3454                 /* Replace sethi with a nop.  Preserve the packing bit.  */
3455                 insn &= (unsigned long)0x80000000;
3456                 insn |= (unsigned long)0x00880000;
3457                 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
3458
3459                 /* Nothing to relocate.  */
3460                 continue;
3461               }
3462
3463             break;
3464
3465           case R_FRV_TLSMOFFLO:
3466             insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
3467
3468             /* Is this a setlo or setlos instruction?  */
3469             if ((insn & (unsigned long)0x01f70000) != 0x00f40000)
3470               {
3471                 r = info->callbacks->warning
3472                   (info,
3473                    _("R_FRV_TLSMOFFLO"
3474                      " not applied to a setlo or setlos instruction"),
3475                    name, input_bfd, input_section, rel->r_offset);
3476                 return FALSE;
3477               }
3478
3479             if (TLSMOFF_IN_RANGE_FOR_SETLOS_P (relocation + rel->r_addend,
3480                                                info))
3481               /* If the corresponding sethi (if it exists) decayed
3482                  to a nop, make sure this becomes (or already is) a
3483                  setlos, not setlo.  */
3484               {
3485                 insn |= (unsigned long)0x00080000;
3486                 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
3487               }
3488
3489             break;
3490
3491             /*
3492               There's nothing to relax in these:
3493                 R_FRV_TLSDESC_VALUE
3494                 R_FRV_TLSOFF
3495                 R_FRV_TLSMOFF12
3496                 R_FRV_TLSMOFFHI
3497                 R_FRV_TLSMOFFLO
3498                 R_FRV_TLSMOFF
3499             */
3500
3501           default:
3502             break;
3503           }
3504
3505       switch (r_type)
3506         {
3507         case R_FRV_LABEL24:
3508           check_segment[0] = isec_segment;
3509           if (! IS_FDPIC (output_bfd))
3510             check_segment[1] = isec_segment;
3511           else if (picrel->plt)
3512             {
3513               relocation = frvfdpic_plt_section (info)->output_section->vma
3514                 + frvfdpic_plt_section (info)->output_offset
3515                 + picrel->plt_entry;
3516               check_segment[1] = plt_segment;
3517             }
3518           /* We don't want to warn on calls to undefined weak symbols,
3519              as calls to them must be protected by non-NULL tests
3520              anyway, and unprotected calls would invoke undefined
3521              behavior.  */
3522           else if (picrel->symndx == -1
3523                    && picrel->d.h->root.type == bfd_link_hash_undefweak)
3524             check_segment[1] = check_segment[0];
3525           else
3526             check_segment[1] = sec
3527               ? _frvfdpic_osec_to_segment (output_bfd, sec->output_section)
3528               : (unsigned)-1;
3529           break;
3530
3531         case R_FRV_GOT12:
3532         case R_FRV_GOTHI:
3533         case R_FRV_GOTLO:
3534           relocation = picrel->got_entry;
3535           check_segment[0] = check_segment[1] = got_segment;
3536           break;
3537
3538         case R_FRV_FUNCDESC_GOT12:
3539         case R_FRV_FUNCDESC_GOTHI:
3540         case R_FRV_FUNCDESC_GOTLO:
3541           relocation = picrel->fdgot_entry;
3542           check_segment[0] = check_segment[1] = got_segment;
3543           break;
3544
3545         case R_FRV_GOTOFFHI:
3546         case R_FRV_GOTOFF12:
3547         case R_FRV_GOTOFFLO:
3548           relocation -= frvfdpic_got_section (info)->output_section->vma
3549             + frvfdpic_got_section (info)->output_offset
3550             + frvfdpic_got_initial_offset (info);
3551           check_segment[0] = got_segment;
3552           check_segment[1] = sec
3553             ? _frvfdpic_osec_to_segment (output_bfd, sec->output_section)
3554             : (unsigned)-1;
3555           break;
3556
3557         case R_FRV_FUNCDESC_GOTOFF12:
3558         case R_FRV_FUNCDESC_GOTOFFHI:
3559         case R_FRV_FUNCDESC_GOTOFFLO:
3560           relocation = picrel->fd_entry;
3561           check_segment[0] = check_segment[1] = got_segment;
3562           break;
3563
3564         case R_FRV_FUNCDESC:
3565           {
3566             int dynindx;
3567             bfd_vma addend = rel->r_addend;
3568
3569             if (! (h && h->root.type == bfd_link_hash_undefweak
3570                    && FRVFDPIC_SYM_LOCAL (info, h)))
3571               {
3572                 /* If the symbol is dynamic and there may be dynamic
3573                    symbol resolution because we are or are linked with a
3574                    shared library, emit a FUNCDESC relocation such that
3575                    the dynamic linker will allocate the function
3576                    descriptor.  If the symbol needs a non-local function
3577                    descriptor but binds locally (e.g., its visibility is
3578                    protected, emit a dynamic relocation decayed to
3579                    section+offset.  */
3580                 if (h && ! FRVFDPIC_FUNCDESC_LOCAL (info, h)
3581                     && FRVFDPIC_SYM_LOCAL (info, h)
3582                     && !(info->executable && !info->pie))
3583                   {
3584                     dynindx = elf_section_data (h->root.u.def.section
3585                                                 ->output_section)->dynindx;
3586                     addend += h->root.u.def.section->output_offset
3587                       + h->root.u.def.value;
3588                   }
3589                 else if (h && ! FRVFDPIC_FUNCDESC_LOCAL (info, h))
3590                   {
3591                     if (addend)
3592                       {
3593                         info->callbacks->warning
3594                           (info, _("R_FRV_FUNCDESC references dynamic symbol with nonzero addend"),
3595                            name, input_bfd, input_section, rel->r_offset);
3596                         return FALSE;
3597                       }
3598                     dynindx = h->dynindx;
3599                   }
3600                 else
3601                   {
3602                     /* Otherwise, we know we have a private function
3603                        descriptor, so reference it directly.  */
3604                     BFD_ASSERT (picrel->privfd);
3605                     r_type = R_FRV_32;
3606                     dynindx = elf_section_data (frvfdpic_got_section (info)
3607                                                 ->output_section)->dynindx;
3608                     addend = frvfdpic_got_section (info)->output_offset
3609                       + frvfdpic_got_initial_offset (info)
3610                       + picrel->fd_entry;
3611                   }
3612
3613                 /* If there is room for dynamic symbol resolution, emit
3614                    the dynamic relocation.  However, if we're linking an
3615                    executable at a fixed location, we won't have emitted a
3616                    dynamic symbol entry for the got section, so idx will
3617                    be zero, which means we can and should compute the
3618                    address of the private descriptor ourselves.  */
3619                 if (info->executable && !info->pie
3620                     && (!h || FRVFDPIC_FUNCDESC_LOCAL (info, h)))
3621                   {
3622                     addend += frvfdpic_got_section (info)->output_section->vma;
3623                     if ((bfd_get_section_flags (output_bfd,
3624                                                 input_section->output_section)
3625                          & (SEC_ALLOC | SEC_LOAD)) == (SEC_ALLOC | SEC_LOAD))
3626                       {
3627                         if (_frvfdpic_osec_readonly_p (output_bfd,
3628                                                        input_section
3629                                                        ->output_section))
3630                           {
3631                             info->callbacks->warning
3632                               (info,
3633                                _("cannot emit fixups in read-only section"),
3634                                name, input_bfd, input_section, rel->r_offset);
3635                             return FALSE;
3636                           }
3637                         _frvfdpic_add_rofixup (output_bfd,
3638                                                frvfdpic_gotfixup_section
3639                                                (info),
3640                                                _bfd_elf_section_offset
3641                                                (output_bfd, info,
3642                                                 input_section, rel->r_offset)
3643                                                + input_section
3644                                                ->output_section->vma
3645                                                + input_section->output_offset,
3646                                                picrel);
3647                       }
3648                   }
3649                 else if ((bfd_get_section_flags (output_bfd,
3650                                                  input_section->output_section)
3651                           & (SEC_ALLOC | SEC_LOAD)) == (SEC_ALLOC | SEC_LOAD))
3652                   {
3653                     if (_frvfdpic_osec_readonly_p (output_bfd,
3654                                                    input_section
3655                                                    ->output_section))
3656                       {
3657                         info->callbacks->warning
3658                           (info,
3659                            _("cannot emit dynamic relocations in read-only section"),
3660                            name, input_bfd, input_section, rel->r_offset);
3661                         return FALSE;
3662                       }
3663                     _frvfdpic_add_dyn_reloc (output_bfd,
3664                                              frvfdpic_gotrel_section (info),
3665                                              _bfd_elf_section_offset
3666                                              (output_bfd, info,
3667                                               input_section, rel->r_offset)
3668                                              + input_section
3669                                              ->output_section->vma
3670                                              + input_section->output_offset,
3671                                              r_type, dynindx, addend, picrel);
3672                   }
3673                 else
3674                   addend += frvfdpic_got_section (info)->output_section->vma;
3675               }
3676
3677             /* We want the addend in-place because dynamic
3678                relocations are REL.  Setting relocation to it should
3679                arrange for it to be installed.  */
3680             relocation = addend - rel->r_addend;
3681           }
3682           check_segment[0] = check_segment[1] = got_segment;
3683           break;
3684
3685         case R_FRV_32:
3686           if (! IS_FDPIC (output_bfd))
3687             {
3688               check_segment[0] = check_segment[1] = -1;
3689               break;
3690             }
3691           /* Fall through.  */
3692         case R_FRV_FUNCDESC_VALUE:
3693           {
3694             int dynindx;
3695             bfd_vma addend = rel->r_addend;
3696
3697             /* If the symbol is dynamic but binds locally, use
3698                section+offset.  */
3699             if (h && ! FRVFDPIC_SYM_LOCAL (info, h))
3700               {
3701                 if (addend && r_type == R_FRV_FUNCDESC_VALUE)
3702                   {
3703                     info->callbacks->warning
3704                       (info, _("R_FRV_FUNCDESC_VALUE references dynamic symbol with nonzero addend"),
3705                        name, input_bfd, input_section, rel->r_offset);
3706                     return FALSE;
3707                   }
3708                 dynindx = h->dynindx;
3709               }
3710             else
3711               {
3712                 if (h)
3713                   addend += h->root.u.def.value;
3714                 else
3715                   addend += sym->st_value;
3716                 if (osec)
3717                   addend += osec->output_offset;
3718                 if (osec && osec->output_section
3719                     && ! bfd_is_abs_section (osec->output_section)
3720                     && ! bfd_is_und_section (osec->output_section))
3721                   dynindx = elf_section_data (osec->output_section)->dynindx;
3722                 else
3723                   dynindx = 0;
3724               }
3725
3726             /* If we're linking an executable at a fixed address, we
3727                can omit the dynamic relocation as long as the symbol
3728                is defined in the current link unit (which is implied
3729                by its output section not being NULL).  */
3730             if (info->executable && !info->pie
3731                 && (!h || FRVFDPIC_SYM_LOCAL (info, h)))
3732               {
3733                 if (osec)
3734                   addend += osec->output_section->vma;
3735                 if (IS_FDPIC (input_bfd)
3736                     && (bfd_get_section_flags (output_bfd,
3737                                                input_section->output_section)
3738                         & (SEC_ALLOC | SEC_LOAD)) == (SEC_ALLOC | SEC_LOAD))
3739                   {
3740                     if (_frvfdpic_osec_readonly_p (output_bfd,
3741                                                    input_section
3742                                                    ->output_section))
3743                       {
3744                         info->callbacks->warning
3745                           (info,
3746                            _("cannot emit fixups in read-only section"),
3747                            name, input_bfd, input_section, rel->r_offset);
3748                         return FALSE;
3749                       }
3750                     if (!h || h->root.type != bfd_link_hash_undefweak)
3751                       {
3752                         _frvfdpic_add_rofixup (output_bfd,
3753                                                frvfdpic_gotfixup_section
3754                                                (info),
3755                                                _bfd_elf_section_offset
3756                                                (output_bfd, info,
3757                                                 input_section, rel->r_offset)
3758                                                + input_section
3759                                                ->output_section->vma
3760                                                + input_section->output_offset,
3761                                                picrel);
3762                         if (r_type == R_FRV_FUNCDESC_VALUE)
3763                           _frvfdpic_add_rofixup
3764                             (output_bfd,
3765                              frvfdpic_gotfixup_section (info),
3766                              _bfd_elf_section_offset
3767                              (output_bfd, info,
3768                               input_section, rel->r_offset)
3769                              + input_section->output_section->vma
3770                              + input_section->output_offset + 4, picrel);
3771                       }
3772                   }
3773               }
3774             else
3775               {
3776                 if ((bfd_get_section_flags (output_bfd,
3777                                             input_section->output_section)
3778                      & (SEC_ALLOC | SEC_LOAD)) == (SEC_ALLOC | SEC_LOAD))
3779                   {
3780                     if (_frvfdpic_osec_readonly_p (output_bfd,
3781                                                    input_section
3782                                                    ->output_section))
3783                       {
3784                         info->callbacks->warning
3785                           (info,
3786                            _("cannot emit dynamic relocations in read-only section"),
3787                            name, input_bfd, input_section, rel->r_offset);
3788                         return FALSE;
3789                       }
3790                     _frvfdpic_add_dyn_reloc (output_bfd,
3791                                              frvfdpic_gotrel_section (info),
3792                                              _bfd_elf_section_offset
3793                                              (output_bfd, info,
3794                                               input_section, rel->r_offset)
3795                                              + input_section
3796                                              ->output_section->vma
3797                                              + input_section->output_offset,
3798                                              r_type, dynindx, addend, picrel);
3799                   }
3800                 else if (osec)
3801                   addend += osec->output_section->vma;
3802                 /* We want the addend in-place because dynamic
3803                    relocations are REL.  Setting relocation to it
3804                    should arrange for it to be installed.  */
3805                 relocation = addend - rel->r_addend;
3806               }
3807
3808             if (r_type == R_FRV_FUNCDESC_VALUE)
3809               {
3810                 /* If we've omitted the dynamic relocation, just emit
3811                    the fixed addresses of the symbol and of the local
3812                    GOT base offset.  */
3813                 if (info->executable && !info->pie
3814                     && (!h || FRVFDPIC_SYM_LOCAL (info, h)))
3815                   bfd_put_32 (output_bfd,
3816                               frvfdpic_got_section (info)->output_section->vma
3817                               + frvfdpic_got_section (info)->output_offset
3818                               + frvfdpic_got_initial_offset (info),
3819                               contents + rel->r_offset + 4);
3820                 else
3821                   /* A function descriptor used for lazy or local
3822                      resolving is initialized such that its high word
3823                      contains the output section index in which the
3824                      PLT entries are located, and the low word
3825                      contains the offset of the lazy PLT entry entry
3826                      point into that section.  */
3827                   bfd_put_32 (output_bfd,
3828                               h && ! FRVFDPIC_SYM_LOCAL (info, h)
3829                               ? 0
3830                               : _frvfdpic_osec_to_segment (output_bfd,
3831                                                            sec
3832                                                            ->output_section),
3833                               contents + rel->r_offset + 4);
3834               }
3835           }
3836           check_segment[0] = check_segment[1] = got_segment;
3837           break;
3838
3839         case R_FRV_GPREL12:
3840         case R_FRV_GPRELU12:
3841         case R_FRV_GPREL32:
3842         case R_FRV_GPRELHI:
3843         case R_FRV_GPRELLO:
3844           check_segment[0] = gprel_segment;
3845           check_segment[1] = sec
3846             ? _frvfdpic_osec_to_segment (output_bfd, sec->output_section)
3847             : (unsigned)-1;
3848           break;
3849
3850         case R_FRV_GETTLSOFF:
3851           relocation = frvfdpic_plt_section (info)->output_section->vma
3852             + frvfdpic_plt_section (info)->output_offset
3853             + picrel->tlsplt_entry;
3854           BFD_ASSERT (picrel->tlsplt_entry != (bfd_vma)-1
3855                       && picrel->tlsdesc_entry);
3856           check_segment[0] = isec_segment;
3857           check_segment[1] = plt_segment;
3858           break;
3859
3860         case R_FRV_GOTTLSDESC12:
3861         case R_FRV_GOTTLSDESCHI:
3862         case R_FRV_GOTTLSDESCLO:
3863           BFD_ASSERT (picrel->tlsdesc_entry);
3864           relocation = picrel->tlsdesc_entry;
3865           check_segment[0] = tls_segment;
3866           check_segment[1] = sec
3867             && ! bfd_is_abs_section (sec)
3868             && ! bfd_is_und_section (sec)
3869             ? _frvfdpic_osec_to_segment (output_bfd, sec->output_section)
3870             : tls_segment;
3871           break;
3872
3873         case R_FRV_TLSMOFF12:
3874         case R_FRV_TLSMOFFHI:
3875         case R_FRV_TLSMOFFLO:
3876         case R_FRV_TLSMOFF:
3877           check_segment[0] = tls_segment;
3878           if (! sec)
3879             check_segment[1] = -1;
3880           else if (bfd_is_abs_section (sec)
3881                    || bfd_is_und_section (sec))
3882             {
3883               relocation = 0;
3884               check_segment[1] = tls_segment;
3885             }
3886           else if (sec->output_section)
3887             {
3888               relocation -= tls_biased_base (info);
3889               check_segment[1] =
3890                 _frvfdpic_osec_to_segment (output_bfd, sec->output_section);
3891             }
3892           else
3893             check_segment[1] = -1;
3894           break;
3895
3896         case R_FRV_GOTTLSOFF12:
3897         case R_FRV_GOTTLSOFFHI:
3898         case R_FRV_GOTTLSOFFLO:
3899           BFD_ASSERT (picrel->tlsoff_entry);
3900           relocation = picrel->tlsoff_entry;
3901           check_segment[0] = tls_segment;
3902           check_segment[1] = sec
3903             && ! bfd_is_abs_section (sec)
3904             && ! bfd_is_und_section (sec)
3905             ? _frvfdpic_osec_to_segment (output_bfd, sec->output_section)
3906             : tls_segment;
3907           break;
3908
3909         case R_FRV_TLSDESC_VALUE:
3910         case R_FRV_TLSOFF:
3911           /* These shouldn't be present in input object files.  */
3912           check_segment[0] = check_segment[1] = isec_segment;
3913           break;
3914
3915         case R_FRV_TLSDESC_RELAX:
3916         case R_FRV_GETTLSOFF_RELAX:
3917         case R_FRV_TLSOFF_RELAX:
3918           /* These are just annotations for relaxation, nothing to do
3919              here.  */
3920           continue;
3921
3922         default:
3923           check_segment[0] = isec_segment;
3924           check_segment[1] = sec
3925             ? _frvfdpic_osec_to_segment (output_bfd, sec->output_section)
3926             : (unsigned)-1;
3927           break;
3928         }
3929
3930       if (check_segment[0] != check_segment[1] && IS_FDPIC (output_bfd))
3931         {
3932           /* If you take this out, remove the #error from fdpic-static-6.d
3933              in the ld testsuite.  */
3934           /* This helps catch problems in GCC while we can't do more
3935              than static linking.  The idea is to test whether the
3936              input file basename is crt0.o only once.  */
3937           if (silence_segment_error == 1)
3938             silence_segment_error =
3939               (strlen (input_bfd->filename) == 6
3940                && strcmp (input_bfd->filename, "crt0.o") == 0)
3941               || (strlen (input_bfd->filename) > 6
3942                   && strcmp (input_bfd->filename
3943                              + strlen (input_bfd->filename) - 7,
3944                              "/crt0.o") == 0)
3945               ? -1 : 0;
3946           if (!silence_segment_error
3947               /* We don't want duplicate errors for undefined
3948                  symbols.  */
3949               && !(picrel && picrel->symndx == -1
3950                    && picrel->d.h->root.type == bfd_link_hash_undefined))
3951             {
3952               if (info->shared || info->pie)
3953                 (*_bfd_error_handler)
3954                   (_("%B(%A+0x%lx): reloc against `%s': %s"),
3955                    input_bfd, input_section, (long)rel->r_offset, name,
3956                    _("relocation references a different segment"));
3957               else
3958                 info->callbacks->warning
3959                   (info,
3960                    _("relocation references a different segment"),
3961                    name, input_bfd, input_section, rel->r_offset);
3962             }
3963           if (!silence_segment_error && (info->shared || info->pie))
3964             return FALSE;
3965           elf_elfheader (output_bfd)->e_flags |= EF_FRV_PIC;
3966         }
3967
3968       switch (r_type)
3969         {
3970         case R_FRV_GOTOFFHI:
3971         case R_FRV_TLSMOFFHI:
3972           /* We need the addend to be applied before we shift the
3973              value right.  */
3974           relocation += rel->r_addend;
3975           /* Fall through.  */
3976         case R_FRV_GOTHI:
3977         case R_FRV_FUNCDESC_GOTHI:
3978         case R_FRV_FUNCDESC_GOTOFFHI:
3979         case R_FRV_GOTTLSOFFHI:
3980         case R_FRV_GOTTLSDESCHI:
3981           relocation >>= 16;
3982           /* Fall through.  */
3983
3984         case R_FRV_GOTLO:
3985         case R_FRV_FUNCDESC_GOTLO:
3986         case R_FRV_GOTOFFLO:
3987         case R_FRV_FUNCDESC_GOTOFFLO:
3988         case R_FRV_GOTTLSOFFLO:
3989         case R_FRV_GOTTLSDESCLO:
3990         case R_FRV_TLSMOFFLO:
3991           relocation &= 0xffff;
3992           break;
3993
3994         default:
3995           break;
3996         }
3997
3998       switch (r_type)
3999         {
4000         case R_FRV_LABEL24:
4001           if (! IS_FDPIC (output_bfd) || ! picrel->plt)
4002             break;
4003           /* Fall through.  */
4004
4005           /* When referencing a GOT entry, a function descriptor or a
4006              PLT, we don't want the addend to apply to the reference,
4007              but rather to the referenced symbol.  The actual entry
4008              will have already been created taking the addend into
4009              account, so cancel it out here.  */
4010         case R_FRV_GOT12:
4011         case R_FRV_GOTHI:
4012         case R_FRV_GOTLO:
4013         case R_FRV_FUNCDESC_GOT12:
4014         case R_FRV_FUNCDESC_GOTHI:
4015         case R_FRV_FUNCDESC_GOTLO:
4016         case R_FRV_FUNCDESC_GOTOFF12:
4017         case R_FRV_FUNCDESC_GOTOFFHI:
4018         case R_FRV_FUNCDESC_GOTOFFLO:
4019         case R_FRV_GETTLSOFF:
4020         case R_FRV_GOTTLSDESC12:
4021         case R_FRV_GOTTLSDESCHI:
4022         case R_FRV_GOTTLSDESCLO:
4023         case R_FRV_GOTTLSOFF12:
4024         case R_FRV_GOTTLSOFFHI:
4025         case R_FRV_GOTTLSOFFLO:
4026           /* Note that we only want GOTOFFHI, not GOTOFFLO or GOTOFF12
4027              here, since we do want to apply the addend to the others.
4028              Note that we've applied the addend to GOTOFFHI before we
4029              shifted it right.  */
4030         case R_FRV_GOTOFFHI:
4031         case R_FRV_TLSMOFFHI:
4032           relocation -= rel->r_addend;
4033           break;
4034
4035         default:
4036           break;
4037         }
4038
4039      if (r_type == R_FRV_HI16)
4040        r = elf32_frv_relocate_hi16 (input_bfd, rel, contents, relocation);
4041
4042      else if (r_type == R_FRV_LO16)
4043        r = elf32_frv_relocate_lo16 (input_bfd, rel, contents, relocation);
4044
4045      else if (r_type == R_FRV_LABEL24 || r_type == R_FRV_GETTLSOFF)
4046        r = elf32_frv_relocate_label24 (input_bfd, input_section, rel,
4047                                        contents, relocation);
4048
4049      else if (r_type == R_FRV_GPREL12)
4050        r = elf32_frv_relocate_gprel12 (info, input_bfd, input_section, rel,
4051                                        contents, relocation);
4052
4053      else if (r_type == R_FRV_GPRELU12)
4054        r = elf32_frv_relocate_gprelu12 (info, input_bfd, input_section, rel,
4055                                         contents, relocation);
4056
4057      else if (r_type == R_FRV_GPRELLO)
4058        r = elf32_frv_relocate_gprello (info, input_bfd, input_section, rel,
4059                                        contents, relocation);
4060
4061      else if (r_type == R_FRV_GPRELHI)
4062        r = elf32_frv_relocate_gprelhi (info, input_bfd, input_section, rel,
4063                                        contents, relocation);
4064
4065      else if (r_type == R_FRV_TLSOFF
4066               || r_type == R_FRV_TLSDESC_VALUE)
4067        r = bfd_reloc_notsupported;
4068
4069      else
4070        r = frv_final_link_relocate (howto, input_bfd, input_section, contents,
4071                                     rel, relocation);
4072
4073       if (r != bfd_reloc_ok)
4074         {
4075           const char * msg = (const char *) NULL;
4076
4077           switch (r)
4078             {
4079             case bfd_reloc_overflow:
4080               r = info->callbacks->reloc_overflow
4081                 (info, (h ? &h->root : NULL), name, howto->name,
4082                  (bfd_vma) 0, input_bfd, input_section, rel->r_offset);
4083               break;
4084
4085             case bfd_reloc_undefined:
4086               r = info->callbacks->undefined_symbol
4087                 (info, name, input_bfd, input_section, rel->r_offset, TRUE);
4088               break;
4089
4090             case bfd_reloc_outofrange:
4091               msg = _("internal error: out of range error");
4092               break;
4093
4094             case bfd_reloc_notsupported:
4095               msg = _("internal error: unsupported relocation error");
4096               break;
4097
4098             case bfd_reloc_dangerous:
4099               msg = _("internal error: dangerous relocation");
4100               break;
4101
4102             default:
4103               msg = _("internal error: unknown error");
4104               break;
4105             }
4106
4107           if (msg)
4108             {
4109               (*_bfd_error_handler)
4110                 (_("%B(%A+0x%lx): reloc against `%s': %s"),
4111                  input_bfd, input_section, (long)rel->r_offset, name, msg);
4112               return FALSE;
4113             }
4114
4115           if (! r)
4116             return FALSE;
4117         }
4118     }
4119
4120   return TRUE;
4121 }
4122 \f
4123 /* Return the section that should be marked against GC for a given
4124    relocation.  */
4125
4126 static asection *
4127 elf32_frv_gc_mark_hook (asection *sec,
4128                         struct bfd_link_info *info,
4129                         Elf_Internal_Rela *rel,
4130                         struct elf_link_hash_entry *h,
4131                         Elf_Internal_Sym *sym)
4132 {
4133   if (h != NULL)
4134     switch (ELF32_R_TYPE (rel->r_info))
4135       {
4136       case R_FRV_GNU_VTINHERIT:
4137       case R_FRV_GNU_VTENTRY:
4138         return NULL;
4139       }
4140
4141   return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
4142 }
4143 \f
4144 /* Hook called by the linker routine which adds symbols from an object
4145    file.  We use it to put .comm items in .scomm, and not .comm.  */
4146
4147 static bfd_boolean
4148 elf32_frv_add_symbol_hook (abfd, info, sym, namep, flagsp, secp, valp)
4149      bfd *abfd;
4150      struct bfd_link_info *info;
4151      Elf_Internal_Sym *sym;
4152      const char **namep ATTRIBUTE_UNUSED;
4153      flagword *flagsp ATTRIBUTE_UNUSED;
4154      asection **secp;
4155      bfd_vma *valp;
4156 {
4157   if (sym->st_shndx == SHN_COMMON
4158       && !info->relocatable
4159       && (int)sym->st_size <= (int)bfd_get_gp_size (abfd))
4160     {
4161       /* Common symbols less than or equal to -G nn bytes are
4162          automatically put into .sbss.  */
4163
4164       asection *scomm = bfd_get_section_by_name (abfd, ".scommon");
4165
4166       if (scomm == NULL)
4167         {
4168           scomm = bfd_make_section_with_flags (abfd, ".scommon",
4169                                                (SEC_ALLOC
4170                                                 | SEC_IS_COMMON
4171                                                 | SEC_LINKER_CREATED));
4172           if (scomm == NULL)
4173             return FALSE;
4174         }
4175
4176       *secp = scomm;
4177       *valp = sym->st_size;
4178     }
4179
4180   return TRUE;
4181 }
4182
4183 /* We need dynamic symbols for every section, since segments can
4184    relocate independently.  */
4185 static bfd_boolean
4186 _frvfdpic_link_omit_section_dynsym (bfd *output_bfd ATTRIBUTE_UNUSED,
4187                                     struct bfd_link_info *info
4188                                     ATTRIBUTE_UNUSED,
4189                                     asection *p ATTRIBUTE_UNUSED)
4190 {
4191   switch (elf_section_data (p)->this_hdr.sh_type)
4192     {
4193     case SHT_PROGBITS:
4194     case SHT_NOBITS:
4195       /* If sh_type is yet undecided, assume it could be
4196          SHT_PROGBITS/SHT_NOBITS.  */
4197     case SHT_NULL:
4198       return FALSE;
4199
4200       /* There shouldn't be section relative relocations
4201          against any other section.  */
4202     default:
4203       return TRUE;
4204     }
4205 }
4206
4207 /* Create  a .got section, as well as its additional info field.  This
4208    is almost entirely copied from
4209    elflink.c:_bfd_elf_create_got_section().  */
4210
4211 static bfd_boolean
4212 _frv_create_got_section (bfd *abfd, struct bfd_link_info *info)
4213 {
4214   flagword flags, pltflags;
4215   asection *s;
4216   struct elf_link_hash_entry *h;
4217   struct bfd_link_hash_entry *bh;
4218   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4219   int ptralign;
4220   int offset;
4221
4222   /* This function may be called more than once.  */
4223   s = bfd_get_section_by_name (abfd, ".got");
4224   if (s != NULL && (s->flags & SEC_LINKER_CREATED) != 0)
4225     return TRUE;
4226
4227   /* Machine specific: although pointers are 32-bits wide, we want the
4228      GOT to be aligned to a 64-bit boundary, such that function
4229      descriptors in it can be accessed with 64-bit loads and
4230      stores.  */
4231   ptralign = 3;
4232
4233   flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
4234            | SEC_LINKER_CREATED);
4235   pltflags = flags;
4236
4237   s = bfd_make_section_with_flags (abfd, ".got", flags);
4238   if (s == NULL
4239       || !bfd_set_section_alignment (abfd, s, ptralign))
4240     return FALSE;
4241
4242   if (bed->want_got_plt)
4243     {
4244       s = bfd_make_section_with_flags (abfd, ".got.plt", flags);
4245       if (s == NULL
4246           || !bfd_set_section_alignment (abfd, s, ptralign))
4247         return FALSE;
4248     }
4249
4250   if (bed->want_got_sym)
4251     {
4252       /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
4253          (or .got.plt) section.  We don't do this in the linker script
4254          because we don't want to define the symbol if we are not creating
4255          a global offset table.  */
4256       h = _bfd_elf_define_linkage_sym (abfd, info, s, "_GLOBAL_OFFSET_TABLE_");
4257       elf_hash_table (info)->hgot = h;
4258       if (h == NULL)
4259         return FALSE;
4260
4261       /* Machine-specific: we want the symbol for executables as
4262          well.  */
4263       if (! bfd_elf_link_record_dynamic_symbol (info, h))
4264         return FALSE;
4265     }
4266
4267   /* The first bit of the global offset table is the header.  */
4268   s->size += bed->got_header_size;
4269
4270   /* This is the machine-specific part.  Create and initialize section
4271      data for the got.  */
4272   if (IS_FDPIC (abfd))
4273     {
4274       frvfdpic_got_section (info) = s;
4275       frvfdpic_relocs_info (info) = htab_try_create (1,
4276                                                      frvfdpic_relocs_info_hash,
4277                                                      frvfdpic_relocs_info_eq,
4278                                                      (htab_del) NULL);
4279       if (! frvfdpic_relocs_info (info))
4280         return FALSE;
4281
4282       s = bfd_make_section_with_flags (abfd, ".rel.got",
4283                                        (flags | SEC_READONLY));
4284       if (s == NULL
4285           || ! bfd_set_section_alignment (abfd, s, 2))
4286         return FALSE;
4287
4288       frvfdpic_gotrel_section (info) = s;
4289
4290       /* Machine-specific.  */
4291       s = bfd_make_section_with_flags (abfd, ".rofixup",
4292                                        (flags | SEC_READONLY));
4293       if (s == NULL
4294           || ! bfd_set_section_alignment (abfd, s, 2))
4295         return FALSE;
4296
4297       frvfdpic_gotfixup_section (info) = s;
4298       offset = -2048;
4299       flags = BSF_GLOBAL;
4300     }
4301   else
4302     {
4303       offset = 2048;
4304       flags = BSF_GLOBAL | BSF_WEAK;
4305     }
4306
4307   /* Define _gp in .rofixup, for FDPIC, or .got otherwise.  If it
4308      turns out that we're linking with a different linker script, the
4309      linker script will override it.  */
4310   bh = NULL;
4311   if (!(_bfd_generic_link_add_one_symbol
4312         (info, abfd, "_gp", flags, s, offset, (const char *) NULL, FALSE,
4313          bed->collect, &bh)))
4314     return FALSE;
4315   h = (struct elf_link_hash_entry *) bh;
4316   h->def_regular = 1;
4317   h->type = STT_OBJECT;
4318   /* h->other = STV_HIDDEN; */ /* Should we?  */
4319
4320   /* Machine-specific: we want the symbol for executables as well.  */
4321   if (IS_FDPIC (abfd) && ! bfd_elf_link_record_dynamic_symbol (info, h))
4322     return FALSE;
4323
4324   if (!IS_FDPIC (abfd))
4325     return TRUE;
4326
4327   /* FDPIC supports Thread Local Storage, and this may require a
4328      procedure linkage table for TLS PLT entries.  */
4329
4330   /* This is mostly copied from
4331      elflink.c:_bfd_elf_create_dynamic_sections().  */
4332
4333   flags = pltflags;
4334   pltflags |= SEC_CODE;
4335   if (bed->plt_not_loaded)
4336     pltflags &= ~ (SEC_CODE | SEC_LOAD | SEC_HAS_CONTENTS);
4337   if (bed->plt_readonly)
4338     pltflags |= SEC_READONLY;
4339
4340   s = bfd_make_section_with_flags (abfd, ".plt", pltflags);
4341   if (s == NULL
4342       || ! bfd_set_section_alignment (abfd, s, bed->plt_alignment))
4343     return FALSE;
4344   /* FRV-specific: remember it.  */
4345   frvfdpic_plt_section (info) = s;
4346
4347   /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
4348      .plt section.  */
4349   if (bed->want_plt_sym)
4350     {
4351       h = _bfd_elf_define_linkage_sym (abfd, info, s,
4352                                        "_PROCEDURE_LINKAGE_TABLE_");
4353       elf_hash_table (info)->hplt = h;
4354       if (h == NULL)
4355         return FALSE;
4356     }
4357
4358   /* FRV-specific: we want rel relocations for the plt.  */
4359   s = bfd_make_section_with_flags (abfd, ".rel.plt",
4360                                    flags | SEC_READONLY);
4361   if (s == NULL
4362       || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
4363     return FALSE;
4364   /* FRV-specific: remember it.  */
4365   frvfdpic_pltrel_section (info) = s;
4366
4367   return TRUE;
4368 }
4369
4370 /* Make sure the got and plt sections exist, and that our pointers in
4371    the link hash table point to them.  */
4372
4373 static bfd_boolean
4374 elf32_frvfdpic_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
4375 {
4376   /* This is mostly copied from
4377      elflink.c:_bfd_elf_create_dynamic_sections().  */
4378   flagword flags;
4379   asection *s;
4380   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4381
4382   flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
4383            | SEC_LINKER_CREATED);
4384
4385   /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
4386      .rel[a].bss sections.  */
4387
4388   /* FRV-specific: we want to create the GOT and the PLT in the FRV
4389      way.  */
4390   if (! _frv_create_got_section (abfd, info))
4391     return FALSE;
4392
4393   /* FRV-specific: make sure we created everything we wanted.  */
4394   BFD_ASSERT (frvfdpic_got_section (info) && frvfdpic_gotrel_section (info)
4395               && frvfdpic_gotfixup_section (info)
4396               && frvfdpic_plt_section (info)
4397               && frvfdpic_pltrel_section (info));
4398
4399   if (bed->want_dynbss)
4400     {
4401       /* The .dynbss section is a place to put symbols which are defined
4402          by dynamic objects, are referenced by regular objects, and are
4403          not functions.  We must allocate space for them in the process
4404          image and use a R_*_COPY reloc to tell the dynamic linker to
4405          initialize them at run time.  The linker script puts the .dynbss
4406          section into the .bss section of the final image.  */
4407       s = bfd_make_section_with_flags (abfd, ".dynbss",
4408                                        SEC_ALLOC | SEC_LINKER_CREATED);
4409       if (s == NULL)
4410         return FALSE;
4411
4412       /* The .rel[a].bss section holds copy relocs.  This section is not
4413      normally needed.  We need to create it here, though, so that the
4414      linker will map it to an output section.  We can't just create it
4415      only if we need it, because we will not know whether we need it
4416      until we have seen all the input files, and the first time the
4417      main linker code calls BFD after examining all the input files
4418      (size_dynamic_sections) the input sections have already been
4419      mapped to the output sections.  If the section turns out not to
4420      be needed, we can discard it later.  We will never need this
4421      section when generating a shared object, since they do not use
4422      copy relocs.  */
4423       if (! info->shared)
4424         {
4425           s = bfd_make_section_with_flags (abfd,
4426                                            (bed->default_use_rela_p
4427                                             ? ".rela.bss" : ".rel.bss"),
4428                                            flags | SEC_READONLY);
4429           if (s == NULL
4430               || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
4431             return FALSE;
4432         }
4433     }
4434
4435   return TRUE;
4436 }
4437
4438 /* Compute the total GOT and PLT size required by each symbol in each
4439    range.  Symbols may require up to 4 words in the GOT: an entry
4440    pointing to the symbol, an entry pointing to its function
4441    descriptor, and a private function descriptors taking two
4442    words.  */
4443
4444 static void
4445 _frvfdpic_count_nontls_entries (struct frvfdpic_relocs_info *entry,
4446                                 struct _frvfdpic_dynamic_got_info *dinfo)
4447 {
4448   /* Allocate space for a GOT entry pointing to the symbol.  */
4449   if (entry->got12)
4450     dinfo->got12 += 4;
4451   else if (entry->gotlos)
4452     dinfo->gotlos += 4;
4453   else if (entry->gothilo)
4454     dinfo->gothilo += 4;
4455   else
4456     entry->relocs32--;
4457   entry->relocs32++;
4458
4459   /* Allocate space for a GOT entry pointing to the function
4460      descriptor.  */
4461   if (entry->fdgot12)
4462     dinfo->got12 += 4;
4463   else if (entry->fdgotlos)
4464     dinfo->gotlos += 4;
4465   else if (entry->fdgothilo)
4466     dinfo->gothilo += 4;
4467   else
4468     entry->relocsfd--;
4469   entry->relocsfd++;
4470
4471   /* Decide whether we need a PLT entry, a function descriptor in the
4472      GOT, and a lazy PLT entry for this symbol.  */
4473   entry->plt = entry->call
4474     && entry->symndx == -1 && ! FRVFDPIC_SYM_LOCAL (dinfo->info, entry->d.h)
4475     && elf_hash_table (dinfo->info)->dynamic_sections_created;
4476   entry->privfd = entry->plt
4477     || entry->fdgoff12 || entry->fdgofflos || entry->fdgoffhilo
4478     || ((entry->fd || entry->fdgot12 || entry->fdgotlos || entry->fdgothilo)
4479         && (entry->symndx != -1
4480             || FRVFDPIC_FUNCDESC_LOCAL (dinfo->info, entry->d.h)));
4481   entry->lazyplt = entry->privfd
4482     && entry->symndx == -1 && ! FRVFDPIC_SYM_LOCAL (dinfo->info, entry->d.h)
4483     && ! (dinfo->info->flags & DF_BIND_NOW)
4484     && elf_hash_table (dinfo->info)->dynamic_sections_created;
4485
4486   /* Allocate space for a function descriptor.  */
4487   if (entry->fdgoff12)
4488     dinfo->fd12 += 8;
4489   else if (entry->fdgofflos)
4490     dinfo->fdlos += 8;
4491   else if (entry->privfd && entry->plt)
4492     dinfo->fdplt += 8;
4493   else if (entry->privfd)
4494     dinfo->fdhilo += 8;
4495   else
4496     entry->relocsfdv--;
4497   entry->relocsfdv++;
4498
4499   if (entry->lazyplt)
4500     dinfo->lzplt += 8;
4501 }
4502
4503 /* Compute the total GOT size required by each TLS symbol in each
4504    range.  Symbols may require up to 5 words in the GOT: an entry
4505    holding the TLS offset for the symbol, and an entry with a full TLS
4506    descriptor taking 4 words.  */
4507
4508 static void
4509 _frvfdpic_count_tls_entries (struct frvfdpic_relocs_info *entry,
4510                              struct _frvfdpic_dynamic_got_info *dinfo,
4511                              bfd_boolean subtract)
4512 {
4513   const int l = subtract ? -1 : 1;
4514
4515   /* Allocate space for a GOT entry with the TLS offset of the
4516      symbol.  */
4517   if (entry->tlsoff12)
4518     dinfo->got12 += 4 * l;
4519   else if (entry->tlsofflos)
4520     dinfo->gotlos += 4 * l;
4521   else if (entry->tlsoffhilo)
4522     dinfo->gothilo += 4 * l;
4523   else
4524     entry->relocstlsoff -= l;
4525   entry->relocstlsoff += l;
4526
4527   /* If there's any TLSOFF relocation, mark the output file as not
4528      suitable for dlopening.  This mark will remain even if we relax
4529      all such relocations, but this is not a problem, since we'll only
4530      do so for executables, and we definitely don't want anyone
4531      dlopening executables.  */
4532   if (entry->relocstlsoff)
4533     dinfo->info->flags |= DF_STATIC_TLS;
4534
4535   /* Allocate space for a TLS descriptor.  */
4536   if (entry->tlsdesc12)
4537     dinfo->tlsd12 += 8 * l;
4538   else if (entry->tlsdesclos)
4539     dinfo->tlsdlos += 8 * l;
4540   else if (entry->tlsplt)
4541     dinfo->tlsdplt += 8 * l;
4542   else if (entry->tlsdeschilo)
4543     dinfo->tlsdhilo += 8 * l;
4544   else
4545     entry->relocstlsd -= l;
4546   entry->relocstlsd += l;
4547 }
4548
4549 /* Compute the number of dynamic relocations and fixups that a symbol
4550    requires, and add (or subtract) from the grand and per-symbol
4551    totals.  */
4552
4553 static void
4554 _frvfdpic_count_relocs_fixups (struct frvfdpic_relocs_info *entry,
4555                                struct _frvfdpic_dynamic_got_info *dinfo,
4556                                bfd_boolean subtract)
4557 {
4558   bfd_vma relocs = 0, fixups = 0, tlsrets = 0;
4559
4560   if (!dinfo->info->executable || dinfo->info->pie)
4561     {
4562       relocs = entry->relocs32 + entry->relocsfd + entry->relocsfdv
4563         + entry->relocstlsd;
4564
4565       /* In the executable, TLS relocations to symbols that bind
4566          locally (including those that resolve to global TLS offsets)
4567          are resolved immediately, without any need for fixups or
4568          dynamic relocations.  In shared libraries, however, we must
4569          emit dynamic relocations even for local symbols, because we
4570          don't know the module id the library is going to get at
4571          run-time, nor its TLS base offset.  */
4572       if (!dinfo->info->executable
4573           || (entry->symndx == -1
4574               && ! FRVFDPIC_SYM_LOCAL (dinfo->info, entry->d.h)))
4575         relocs += entry->relocstlsoff;
4576     }
4577   else
4578     {
4579       if (entry->symndx != -1 || FRVFDPIC_SYM_LOCAL (dinfo->info, entry->d.h))
4580         {
4581           if (entry->symndx != -1
4582               || entry->d.h->root.type != bfd_link_hash_undefweak)
4583             fixups += entry->relocs32 + 2 * entry->relocsfdv;
4584           fixups += entry->relocstlsd;
4585           tlsrets += entry->relocstlsd;
4586         }
4587       else
4588         {
4589           relocs += entry->relocs32 + entry->relocsfdv
4590             + entry->relocstlsoff + entry->relocstlsd;
4591         }
4592
4593       if (entry->symndx != -1
4594           || FRVFDPIC_FUNCDESC_LOCAL (dinfo->info, entry->d.h))
4595         {
4596           if (entry->symndx != -1
4597               || entry->d.h->root.type != bfd_link_hash_undefweak)
4598             fixups += entry->relocsfd;
4599         }
4600       else
4601         relocs += entry->relocsfd;
4602     }
4603
4604   if (subtract)
4605     {
4606       relocs = - relocs;
4607       fixups = - fixups;
4608       tlsrets = - tlsrets;
4609     }
4610
4611   entry->dynrelocs += relocs;
4612   entry->fixups += fixups;
4613   dinfo->relocs += relocs;
4614   dinfo->fixups += fixups;
4615   dinfo->tls_ret_refs += tlsrets;
4616 }
4617
4618 /* Look for opportunities to relax TLS relocations.  We can assume
4619    we're linking the main executable or a static-tls library, since
4620    otherwise we wouldn't have got here.  When relaxing, we have to
4621    first undo any previous accounting of TLS uses of fixups, dynamic
4622    relocations, GOT and PLT entries.  */
4623
4624 static void
4625 _frvfdpic_relax_tls_entries (struct frvfdpic_relocs_info *entry,
4626                              struct _frvfdpic_dynamic_got_info *dinfo,
4627                              bfd_boolean relaxing)
4628 {
4629   bfd_boolean changed = ! relaxing;
4630
4631   BFD_ASSERT (dinfo->info->executable
4632               || (dinfo->info->flags & DF_STATIC_TLS));
4633
4634   if (entry->tlsdesc12 || entry->tlsdesclos || entry->tlsdeschilo)
4635     {
4636       if (! changed)
4637         {
4638           _frvfdpic_count_relocs_fixups (entry, dinfo, TRUE);
4639           _frvfdpic_count_tls_entries (entry, dinfo, TRUE);
4640           changed = TRUE;
4641         }
4642
4643       /* When linking an executable, we can always decay GOTTLSDESC to
4644          TLSMOFF, if the symbol is local, or GOTTLSOFF, otherwise.
4645          When linking a static-tls shared library, using TLSMOFF is
4646          not an option, but we can still use GOTTLSOFF.  When decaying
4647          to GOTTLSOFF, we must keep the GOT entry in range.  We know
4648          it has to fit because we'll be trading the 4 words of hte TLS
4649          descriptor for a single word in the same range.  */
4650       if (! dinfo->info->executable
4651           || (entry->symndx == -1
4652               && ! FRVFDPIC_SYM_LOCAL (dinfo->info, entry->d.h)))
4653         {
4654           entry->tlsoff12 |= entry->tlsdesc12;
4655           entry->tlsofflos |= entry->tlsdesclos;
4656           entry->tlsoffhilo |= entry->tlsdeschilo;
4657         }
4658
4659       entry->tlsdesc12 = entry->tlsdesclos = entry->tlsdeschilo = 0;
4660     }
4661
4662   /* We can only decay TLSOFFs or call #gettlsoff to TLSMOFF in the
4663      main executable.  We have to check whether the symbol's TLSOFF is
4664      in range for a setlos.  For symbols with a hash entry, we can
4665      determine exactly what to do; for others locals, we don't have
4666      addresses handy, so we use the size of the TLS section as an
4667      approximation.  If we get it wrong, we'll retain a GOT entry
4668      holding the TLS offset (without dynamic relocations or fixups),
4669      but we'll still optimize away the loads from it.  Since TLS sizes
4670      are generally very small, it's probably not worth attempting to
4671      do better than this.  */
4672   if ((entry->tlsplt
4673        || entry->tlsoff12 || entry->tlsofflos || entry->tlsoffhilo)
4674       && dinfo->info->executable && relaxing
4675       && ((entry->symndx == -1
4676            && FRVFDPIC_SYM_LOCAL (dinfo->info, entry->d.h)
4677            /* The above may hold for an undefweak TLS symbol, so make
4678               sure we don't have this case before accessing def.value
4679               and def.section.  */
4680            && (entry->d.h->root.type == bfd_link_hash_undefweak
4681                || (bfd_vma)(entry->d.h->root.u.def.value
4682                             + (entry->d.h->root.u.def.section
4683                                ->output_section->vma)
4684                             + entry->d.h->root.u.def.section->output_offset
4685                             + entry->addend
4686                             - tls_biased_base (dinfo->info)
4687                             + 32768) < (bfd_vma)65536))
4688           || (entry->symndx != -1
4689               && (elf_hash_table (dinfo->info)->tls_sec->size
4690                   + abs (entry->addend) < 32768 + FRVFDPIC_TLS_BIAS))))
4691     {
4692       if (! changed)
4693         {
4694           _frvfdpic_count_relocs_fixups (entry, dinfo, TRUE);
4695           _frvfdpic_count_tls_entries (entry, dinfo, TRUE);
4696           changed = TRUE;
4697         }
4698
4699       entry->tlsplt =
4700         entry->tlsoff12 = entry->tlsofflos = entry->tlsoffhilo = 0;
4701     }
4702
4703   /* We can decay `call #gettlsoff' to a ldi #tlsoff if we already
4704      have a #gottlsoff12 relocation for this entry, or if we can fit
4705      one more in the 12-bit (and 16-bit) ranges.  */
4706   if (entry->tlsplt
4707       && (entry->tlsoff12
4708           || (relaxing
4709               && dinfo->got12 + dinfo->fd12 + dinfo->tlsd12 <= 4096 - 12 - 4
4710               && (dinfo->got12 + dinfo->fd12 + dinfo->tlsd12
4711                   + dinfo->gotlos + dinfo->fdlos + dinfo->tlsdlos
4712                   <= 65536 - 12 - 4))))
4713     {
4714       if (! changed)
4715         {
4716           _frvfdpic_count_relocs_fixups (entry, dinfo, TRUE);
4717           _frvfdpic_count_tls_entries (entry, dinfo, TRUE);
4718           changed = TRUE;
4719         }
4720
4721       entry->tlsoff12 = 1;
4722       entry->tlsplt = 0;
4723     }
4724
4725   if (changed)
4726     {
4727       _frvfdpic_count_tls_entries (entry, dinfo, FALSE);
4728       _frvfdpic_count_relocs_fixups (entry, dinfo, FALSE);
4729     }
4730
4731   return;
4732 }
4733
4734 /* Compute the total GOT and PLT size required by each symbol in each range. *
4735    Symbols may require up to 4 words in the GOT: an entry pointing to
4736    the symbol, an entry pointing to its function descriptor, and a
4737    private function descriptors taking two words.  */
4738
4739 static int
4740 _frvfdpic_count_got_plt_entries (void **entryp, void *dinfo_)
4741 {
4742   struct frvfdpic_relocs_info *entry = *entryp;
4743   struct _frvfdpic_dynamic_got_info *dinfo = dinfo_;
4744
4745   _frvfdpic_count_nontls_entries (entry, dinfo);
4746
4747   if (dinfo->info->executable || (dinfo->info->flags & DF_STATIC_TLS))
4748     _frvfdpic_relax_tls_entries (entry, dinfo, FALSE);
4749   else
4750     {
4751       _frvfdpic_count_tls_entries (entry, dinfo, FALSE);
4752       _frvfdpic_count_relocs_fixups (entry, dinfo, FALSE);
4753     }
4754
4755   return 1;
4756 }
4757
4758 /* Determine the positive and negative ranges to be used by each
4759    offset range in the GOT.  FDCUR and CUR, that must be aligned to a
4760    double-word boundary, are the minimum (negative) and maximum
4761    (positive) GOT offsets already used by previous ranges, except for
4762    an ODD entry that may have been left behind.  GOT and FD indicate
4763    the size of GOT entries and function descriptors that must be
4764    placed within the range from -WRAP to WRAP.  If there's room left,
4765    up to FDPLT bytes should be reserved for additional function
4766    descriptors.  */
4767
4768 inline static bfd_signed_vma
4769 _frvfdpic_compute_got_alloc_data (struct _frvfdpic_dynamic_got_alloc_data *gad,
4770                                   bfd_signed_vma fdcur,
4771                                   bfd_signed_vma odd,
4772                                   bfd_signed_vma cur,
4773                                   bfd_vma got,
4774                                   bfd_vma fd,
4775                                   bfd_vma fdplt,
4776                                   bfd_vma tlsd,
4777                                   bfd_vma tlsdplt,
4778                                   bfd_vma wrap)
4779 {
4780   bfd_signed_vma wrapmin = -wrap;
4781   const bfd_vma tdescsz = 8;
4782
4783   /* Start at the given initial points.  */
4784   gad->fdcur = fdcur;
4785   gad->cur = cur;
4786
4787   /* If we had an incoming odd word and we have any got entries that
4788      are going to use it, consume it, otherwise leave gad->odd at
4789      zero.  We might force gad->odd to zero and return the incoming
4790      odd such that it is used by the next range, but then GOT entries
4791      might appear to be out of order and we wouldn't be able to
4792      shorten the GOT by one word if it turns out to end with an
4793      unpaired GOT entry.  */
4794   if (odd && got)
4795     {
4796       gad->odd = odd;
4797       got -= 4;
4798       odd = 0;
4799     }
4800   else
4801     gad->odd = 0;
4802
4803   /* If we're left with an unpaired GOT entry, compute its location
4804      such that we can return it.  Otherwise, if got doesn't require an
4805      odd number of words here, either odd was already zero in the
4806      block above, or it was set to zero because got was non-zero, or
4807      got was already zero.  In the latter case, we want the value of
4808      odd to carry over to the return statement, so we don't want to
4809      reset odd unless the condition below is true.  */
4810   if (got & 4)
4811     {
4812       odd = cur + got;
4813       got += 4;
4814     }
4815
4816   /* Compute the tentative boundaries of this range.  */
4817   gad->max = cur + got;
4818   gad->min = fdcur - fd;
4819   gad->fdplt = 0;
4820
4821   /* If function descriptors took too much space, wrap some of them
4822      around.  */
4823   if (gad->min < wrapmin)
4824     {
4825       gad->max += wrapmin - gad->min;
4826       gad->tmin = gad->min = wrapmin;
4827     }
4828
4829   /* If GOT entries took too much space, wrap some of them around.
4830      This may well cause gad->min to become lower than wrapmin.  This
4831      will cause a relocation overflow later on, so we don't have to
4832      report it here . */
4833   if ((bfd_vma) gad->max > wrap)
4834     {
4835       gad->min -= gad->max - wrap;
4836       gad->max = wrap;
4837     }
4838
4839   /* Add TLS descriptors.  */
4840   gad->tmax = gad->max + tlsd;
4841   gad->tmin = gad->min;
4842   gad->tlsdplt = 0;
4843
4844   /* If TLS descriptors took too much space, wrap an integral number
4845      of them around.  */
4846   if ((bfd_vma) gad->tmax > wrap)
4847     {
4848       bfd_vma wrapsize = gad->tmax - wrap;
4849
4850       wrapsize += tdescsz / 2;
4851       wrapsize &= ~ tdescsz / 2;
4852
4853       gad->tmin -= wrapsize;
4854       gad->tmax -= wrapsize;
4855     }
4856
4857   /* If there is space left and we have function descriptors
4858      referenced in PLT entries that could take advantage of shorter
4859      offsets, place them now.  */
4860   if (fdplt && gad->tmin > wrapmin)
4861     {
4862       bfd_vma fds;
4863
4864       if ((bfd_vma) (gad->tmin - wrapmin) < fdplt)
4865         fds = gad->tmin - wrapmin;
4866       else
4867         fds = fdplt;
4868
4869       fdplt -= fds;
4870       gad->min -= fds;
4871       gad->tmin -= fds;
4872       gad->fdplt += fds;
4873     }
4874
4875   /* If there is more space left, try to place some more function
4876      descriptors for PLT entries.  */
4877   if (fdplt && (bfd_vma) gad->tmax < wrap)
4878     {
4879       bfd_vma fds;
4880
4881       if ((bfd_vma) (wrap - gad->tmax) < fdplt)
4882         fds = wrap - gad->tmax;
4883       else
4884         fds = fdplt;
4885
4886       fdplt -= fds;
4887       gad->max += fds;
4888       gad->tmax += fds;
4889       gad->fdplt += fds;
4890     }
4891
4892   /* If there is space left and we have TLS descriptors referenced in
4893      PLT entries that could take advantage of shorter offsets, place
4894      them now.  */
4895   if (tlsdplt && gad->tmin > wrapmin)
4896     {
4897       bfd_vma tlsds;
4898
4899       if ((bfd_vma) (gad->tmin - wrapmin) < tlsdplt)
4900         tlsds = (gad->tmin - wrapmin) & ~ (tdescsz / 2);
4901       else
4902         tlsds = tlsdplt;
4903
4904       tlsdplt -= tlsds;
4905       gad->tmin -= tlsds;
4906       gad->tlsdplt += tlsds;
4907     }
4908
4909   /* If there is more space left, try to place some more TLS
4910      descriptors for PLT entries.  Although we could try to fit an
4911      additional TLS descriptor with half of it just before before the
4912      wrap point and another right past the wrap point, this might
4913      cause us to run out of space for the next region, so don't do
4914      it.  */
4915   if (tlsdplt && (bfd_vma) gad->tmax < wrap - tdescsz / 2)
4916     {
4917       bfd_vma tlsds;
4918
4919       if ((bfd_vma) (wrap - gad->tmax) < tlsdplt)
4920         tlsds = (wrap - gad->tmax) & ~ (tdescsz / 2);
4921       else
4922         tlsds = tlsdplt;
4923
4924       tlsdplt -= tlsds;
4925       gad->tmax += tlsds;
4926       gad->tlsdplt += tlsds;
4927     }
4928
4929   /* If odd was initially computed as an offset past the wrap point,
4930      wrap it around.  */
4931   if (odd > gad->max)
4932     odd = gad->min + odd - gad->max;
4933
4934   /* _frvfdpic_get_got_entry() below will always wrap gad->cur if needed
4935      before returning, so do it here too.  This guarantees that,
4936      should cur and fdcur meet at the wrap point, they'll both be
4937      equal to min.  */
4938   if (gad->cur == gad->max)
4939     gad->cur = gad->min;
4940
4941   /* Ditto for _frvfdpic_get_tlsdesc_entry().  */
4942   gad->tcur = gad->max;
4943   if (gad->tcur == gad->tmax)
4944     gad->tcur = gad->tmin;
4945
4946   return odd;
4947 }
4948
4949 /* Compute the location of the next GOT entry, given the allocation
4950    data for a range.  */
4951
4952 inline static bfd_signed_vma
4953 _frvfdpic_get_got_entry (struct _frvfdpic_dynamic_got_alloc_data *gad)
4954 {
4955   bfd_signed_vma ret;
4956
4957   if (gad->odd)
4958     {
4959       /* If there was an odd word left behind, use it.  */
4960       ret = gad->odd;
4961       gad->odd = 0;
4962     }
4963   else
4964     {
4965       /* Otherwise, use the word pointed to by cur, reserve the next
4966          as an odd word, and skip to the next pair of words, possibly
4967          wrapping around.  */
4968       ret = gad->cur;
4969       gad->odd = gad->cur + 4;
4970       gad->cur += 8;
4971       if (gad->cur == gad->max)
4972         gad->cur = gad->min;
4973     }
4974
4975   return ret;
4976 }
4977
4978 /* Compute the location of the next function descriptor entry in the
4979    GOT, given the allocation data for a range.  */
4980
4981 inline static bfd_signed_vma
4982 _frvfdpic_get_fd_entry (struct _frvfdpic_dynamic_got_alloc_data *gad)
4983 {
4984   /* If we're at the bottom, wrap around, and only then allocate the
4985      next pair of words.  */
4986   if (gad->fdcur == gad->min)
4987     gad->fdcur = gad->max;
4988   return gad->fdcur -= 8;
4989 }
4990
4991 /* Compute the location of the next TLS descriptor entry in the GOT,
4992    given the allocation data for a range.  */
4993 inline static bfd_signed_vma
4994 _frvfdpic_get_tlsdesc_entry (struct _frvfdpic_dynamic_got_alloc_data *gad)
4995 {
4996   bfd_signed_vma ret;
4997
4998   ret = gad->tcur;
4999
5000   gad->tcur += 8;
5001
5002   /* If we're at the top of the region, wrap around to the bottom.  */
5003   if (gad->tcur == gad->tmax)
5004     gad->tcur = gad->tmin;
5005
5006   return ret;
5007 }
5008
5009 /* Assign GOT offsets for every GOT entry and function descriptor.
5010    Doing everything in a single pass is tricky.  */
5011
5012 static int
5013 _frvfdpic_assign_got_entries (void **entryp, void *info_)
5014 {
5015   struct frvfdpic_relocs_info *entry = *entryp;
5016   struct _frvfdpic_dynamic_got_plt_info *dinfo = info_;
5017
5018   if (entry->got12)
5019     entry->got_entry = _frvfdpic_get_got_entry (&dinfo->got12);
5020   else if (entry->gotlos)
5021     entry->got_entry = _frvfdpic_get_got_entry (&dinfo->gotlos);
5022   else if (entry->gothilo)
5023     entry->got_entry = _frvfdpic_get_got_entry (&dinfo->gothilo);
5024
5025   if (entry->fdgot12)
5026     entry->fdgot_entry = _frvfdpic_get_got_entry (&dinfo->got12);
5027   else if (entry->fdgotlos)
5028     entry->fdgot_entry = _frvfdpic_get_got_entry (&dinfo->gotlos);
5029   else if (entry->fdgothilo)
5030     entry->fdgot_entry = _frvfdpic_get_got_entry (&dinfo->gothilo);
5031
5032   if (entry->fdgoff12)
5033     entry->fd_entry = _frvfdpic_get_fd_entry (&dinfo->got12);
5034   else if (entry->plt && dinfo->got12.fdplt)
5035     {
5036       dinfo->got12.fdplt -= 8;
5037       entry->fd_entry = _frvfdpic_get_fd_entry (&dinfo->got12);
5038     }
5039   else if (entry->fdgofflos)
5040     entry->fd_entry = _frvfdpic_get_fd_entry (&dinfo->gotlos);
5041   else if (entry->plt && dinfo->gotlos.fdplt)
5042     {
5043       dinfo->gotlos.fdplt -= 8;
5044       entry->fd_entry = _frvfdpic_get_fd_entry (&dinfo->gotlos);
5045     }
5046   else if (entry->plt)
5047     {
5048       dinfo->gothilo.fdplt -= 8;
5049       entry->fd_entry = _frvfdpic_get_fd_entry (&dinfo->gothilo);
5050     }
5051   else if (entry->privfd)
5052     entry->fd_entry = _frvfdpic_get_fd_entry (&dinfo->gothilo);
5053
5054   if (entry->tlsoff12)
5055     entry->tlsoff_entry = _frvfdpic_get_got_entry (&dinfo->got12);
5056   else if (entry->tlsofflos)
5057     entry->tlsoff_entry = _frvfdpic_get_got_entry (&dinfo->gotlos);
5058   else if (entry->tlsoffhilo)
5059     entry->tlsoff_entry = _frvfdpic_get_got_entry (&dinfo->gothilo);
5060
5061   if (entry->tlsdesc12)
5062     entry->tlsdesc_entry = _frvfdpic_get_tlsdesc_entry (&dinfo->got12);
5063   else if (entry->tlsplt && dinfo->got12.tlsdplt)
5064     {
5065       dinfo->got12.tlsdplt -= 8;
5066       entry->tlsdesc_entry = _frvfdpic_get_tlsdesc_entry (&dinfo->got12);
5067     }
5068   else if (entry->tlsdesclos)
5069     entry->tlsdesc_entry = _frvfdpic_get_tlsdesc_entry (&dinfo->gotlos);
5070   else if (entry->tlsplt && dinfo->gotlos.tlsdplt)
5071     {
5072       dinfo->gotlos.tlsdplt -= 8;
5073       entry->tlsdesc_entry = _frvfdpic_get_tlsdesc_entry (&dinfo->gotlos);
5074     }
5075   else if (entry->tlsplt)
5076     {
5077       dinfo->gothilo.tlsdplt -= 8;
5078       entry->tlsdesc_entry = _frvfdpic_get_tlsdesc_entry (&dinfo->gothilo);
5079     }
5080   else if (entry->tlsdeschilo)
5081     entry->tlsdesc_entry = _frvfdpic_get_tlsdesc_entry (&dinfo->gothilo);
5082
5083   return 1;
5084 }
5085
5086 /* Assign GOT offsets to private function descriptors used by PLT
5087    entries (or referenced by 32-bit offsets), as well as PLT entries
5088    and lazy PLT entries.  */
5089
5090 static int
5091 _frvfdpic_assign_plt_entries (void **entryp, void *info_)
5092 {
5093   struct frvfdpic_relocs_info *entry = *entryp;
5094   struct _frvfdpic_dynamic_got_plt_info *dinfo = info_;
5095
5096   if (entry->privfd)
5097     BFD_ASSERT (entry->fd_entry);
5098
5099   if (entry->plt)
5100     {
5101       int size;
5102
5103       /* We use the section's raw size to mark the location of the
5104          next PLT entry.  */
5105       entry->plt_entry = frvfdpic_plt_section (dinfo->g.info)->size;
5106
5107       /* Figure out the length of this PLT entry based on the
5108          addressing mode we need to reach the function descriptor.  */
5109       BFD_ASSERT (entry->fd_entry);
5110       if (entry->fd_entry >= -(1 << (12 - 1))
5111           && entry->fd_entry < (1 << (12 - 1)))
5112         size = 8;
5113       else if (entry->fd_entry >= -(1 << (16 - 1))
5114                && entry->fd_entry < (1 << (16 - 1)))
5115         size = 12;
5116       else
5117         size = 16;
5118
5119       frvfdpic_plt_section (dinfo->g.info)->size += size;
5120     }
5121
5122   if (entry->lazyplt)
5123     {
5124       entry->lzplt_entry = dinfo->g.lzplt;
5125       dinfo->g.lzplt += 8;
5126       /* If this entry is the one that gets the resolver stub, account
5127          for the additional instruction.  */
5128       if (entry->lzplt_entry % FRVFDPIC_LZPLT_BLOCK_SIZE
5129           == FRVFDPIC_LZPLT_RESOLV_LOC)
5130         dinfo->g.lzplt += 4;
5131     }
5132
5133   if (entry->tlsplt)
5134     {
5135       int size;
5136
5137       entry->tlsplt_entry
5138         = frvfdpic_plt_section (dinfo->g.info)->size;
5139
5140       if (dinfo->g.info->executable
5141           && (entry->symndx != -1
5142               || FRVFDPIC_SYM_LOCAL (dinfo->g.info, entry->d.h)))
5143         {
5144           if ((bfd_signed_vma)entry->addend >= -(1 << (16 - 1))
5145               /* FIXME: here we use the size of the TLS section
5146                  as an upper bound for the value of the TLS
5147                  symbol, because we may not know the exact value
5148                  yet.  If we get it wrong, we'll just waste a
5149                  word in the PLT, and we should never get even
5150                  close to 32 KiB of TLS anyway.  */
5151               && elf_hash_table (dinfo->g.info)->tls_sec
5152               && (elf_hash_table (dinfo->g.info)->tls_sec->size
5153                   + (bfd_signed_vma)(entry->addend) <= (1 << (16 - 1))))
5154             size = 8;
5155           else
5156             size = 12;
5157         }
5158       else if (entry->tlsoff_entry)
5159         {
5160           if (entry->tlsoff_entry >= -(1 << (12 - 1))
5161               && entry->tlsoff_entry < (1 << (12 - 1)))
5162             size = 8;
5163           else if (entry->tlsoff_entry >= -(1 << (16 - 1))
5164                    && entry->tlsoff_entry < (1 << (16 - 1)))
5165             size = 12;
5166           else
5167             size = 16;
5168         }
5169       else
5170         {
5171           BFD_ASSERT (entry->tlsdesc_entry);
5172
5173           if (entry->tlsdesc_entry >= -(1 << (12 - 1))
5174               && entry->tlsdesc_entry < (1 << (12 - 1)))
5175             size = 8;
5176           else if (entry->tlsdesc_entry >= -(1 << (16 - 1))
5177                    && entry->tlsdesc_entry < (1 << (16 - 1)))
5178             size = 12;
5179           else
5180             size = 16;
5181         }
5182
5183       frvfdpic_plt_section (dinfo->g.info)->size += size;
5184     }
5185
5186   return 1;
5187 }
5188
5189 /* Cancel out any effects of calling _frvfdpic_assign_got_entries and
5190    _frvfdpic_assign_plt_entries.  */
5191
5192 static int
5193 _frvfdpic_reset_got_plt_entries (void **entryp, void *ignore ATTRIBUTE_UNUSED)
5194 {
5195   struct frvfdpic_relocs_info *entry = *entryp;
5196
5197   entry->got_entry = 0;
5198   entry->fdgot_entry = 0;
5199   entry->fd_entry = 0;
5200   entry->plt_entry = (bfd_vma)-1;
5201   entry->lzplt_entry = (bfd_vma)-1;
5202   entry->tlsoff_entry = 0;
5203   entry->tlsdesc_entry = 0;
5204   entry->tlsplt_entry = (bfd_vma)-1;
5205
5206   return 1;
5207 }
5208
5209 /* Follow indirect and warning hash entries so that each got entry
5210    points to the final symbol definition.  P must point to a pointer
5211    to the hash table we're traversing.  Since this traversal may
5212    modify the hash table, we set this pointer to NULL to indicate
5213    we've made a potentially-destructive change to the hash table, so
5214    the traversal must be restarted.  */
5215 static int
5216 _frvfdpic_resolve_final_relocs_info (void **entryp, void *p)
5217 {
5218   struct frvfdpic_relocs_info *entry = *entryp;
5219   htab_t *htab = p;
5220
5221   if (entry->symndx == -1)
5222     {
5223       struct elf_link_hash_entry *h = entry->d.h;
5224       struct frvfdpic_relocs_info *oentry;
5225
5226       while (h->root.type == bfd_link_hash_indirect
5227              || h->root.type == bfd_link_hash_warning)
5228         h = (struct elf_link_hash_entry *)h->root.u.i.link;
5229
5230       if (entry->d.h == h)
5231         return 1;
5232
5233       oentry = frvfdpic_relocs_info_for_global (*htab, 0, h, entry->addend,
5234                                                 NO_INSERT);
5235
5236       if (oentry)
5237         {
5238           /* Merge the two entries.  */
5239           frvfdpic_pic_merge_early_relocs_info (oentry, entry);
5240           htab_clear_slot (*htab, entryp);
5241           return 1;
5242         }
5243
5244       entry->d.h = h;
5245
5246       /* If we can't find this entry with the new bfd hash, re-insert
5247          it, and get the traversal restarted.  */
5248       if (! htab_find (*htab, entry))
5249         {
5250           htab_clear_slot (*htab, entryp);
5251           entryp = htab_find_slot (*htab, entry, INSERT);
5252           if (! *entryp)
5253             *entryp = entry;
5254           /* Abort the traversal, since the whole table may have
5255              moved, and leave it up to the parent to restart the
5256              process.  */
5257           *(htab_t *)p = NULL;
5258           return 0;
5259         }
5260     }
5261
5262   return 1;
5263 }
5264
5265 /* Compute the total size of the GOT, the PLT, the dynamic relocations
5266    section and the rofixup section.  Assign locations for GOT and PLT
5267    entries.  */
5268
5269 static bfd_boolean
5270 _frvfdpic_size_got_plt (bfd *output_bfd,
5271                         struct _frvfdpic_dynamic_got_plt_info *gpinfop)
5272 {
5273   bfd_signed_vma odd;
5274   bfd_vma limit, tlslimit;
5275   struct bfd_link_info *info = gpinfop->g.info;
5276   bfd *dynobj = elf_hash_table (info)->dynobj;
5277
5278   memcpy (frvfdpic_dynamic_got_plt_info (info), &gpinfop->g,
5279           sizeof (gpinfop->g));
5280
5281   odd = 12;
5282   /* Compute the total size taken by entries in the 12-bit and 16-bit
5283      ranges, to tell how many PLT function descriptors we can bring
5284      into the 12-bit range without causing the 16-bit range to
5285      overflow.  */
5286   limit = odd + gpinfop->g.got12 + gpinfop->g.gotlos
5287     + gpinfop->g.fd12 + gpinfop->g.fdlos
5288     + gpinfop->g.tlsd12 + gpinfop->g.tlsdlos;
5289   if (limit < (bfd_vma)1 << 16)
5290     limit = ((bfd_vma)1 << 16) - limit;
5291   else
5292     limit = 0;
5293   if (gpinfop->g.fdplt < limit)
5294     {
5295       tlslimit = (limit - gpinfop->g.fdplt) & ~ (bfd_vma) 8;
5296       limit = gpinfop->g.fdplt;
5297     }
5298   else
5299     tlslimit = 0;
5300   if (gpinfop->g.tlsdplt < tlslimit)
5301     tlslimit = gpinfop->g.tlsdplt;
5302
5303   /* Determine the ranges of GOT offsets that we can use for each
5304      range of addressing modes.  */
5305   odd = _frvfdpic_compute_got_alloc_data (&gpinfop->got12,
5306                                           0,
5307                                           odd,
5308                                           16,
5309                                           gpinfop->g.got12,
5310                                           gpinfop->g.fd12,
5311                                           limit,
5312                                           gpinfop->g.tlsd12,
5313                                           tlslimit,
5314                                           (bfd_vma)1 << (12-1));
5315   odd = _frvfdpic_compute_got_alloc_data (&gpinfop->gotlos,
5316                                           gpinfop->got12.tmin,
5317                                           odd,
5318                                           gpinfop->got12.tmax,
5319                                           gpinfop->g.gotlos,
5320                                           gpinfop->g.fdlos,
5321                                           gpinfop->g.fdplt
5322                                           - gpinfop->got12.fdplt,
5323                                           gpinfop->g.tlsdlos,
5324                                           gpinfop->g.tlsdplt
5325                                           - gpinfop->got12.tlsdplt,
5326                                           (bfd_vma)1 << (16-1));
5327   odd = _frvfdpic_compute_got_alloc_data (&gpinfop->gothilo,
5328                                           gpinfop->gotlos.tmin,
5329                                           odd,
5330                                           gpinfop->gotlos.tmax,
5331                                           gpinfop->g.gothilo,
5332                                           gpinfop->g.fdhilo,
5333                                           gpinfop->g.fdplt
5334                                           - gpinfop->got12.fdplt
5335                                           - gpinfop->gotlos.fdplt,
5336                                           gpinfop->g.tlsdhilo,
5337                                           gpinfop->g.tlsdplt
5338                                           - gpinfop->got12.tlsdplt
5339                                           - gpinfop->gotlos.tlsdplt,
5340                                           (bfd_vma)1 << (32-1));
5341
5342   /* Now assign (most) GOT offsets.  */
5343   htab_traverse (frvfdpic_relocs_info (info), _frvfdpic_assign_got_entries,
5344                  gpinfop);
5345
5346   frvfdpic_got_section (info)->size = gpinfop->gothilo.tmax
5347     - gpinfop->gothilo.tmin
5348     /* If an odd word is the last word of the GOT, we don't need this
5349        word to be part of the GOT.  */
5350     - (odd + 4 == gpinfop->gothilo.tmax ? 4 : 0);
5351   if (frvfdpic_got_section (info)->size == 0)
5352     frvfdpic_got_section (info)->flags |= SEC_EXCLUDE;
5353   else if (frvfdpic_got_section (info)->size == 12
5354            && ! elf_hash_table (info)->dynamic_sections_created)
5355     {
5356       frvfdpic_got_section (info)->flags |= SEC_EXCLUDE;
5357       frvfdpic_got_section (info)->size = 0;
5358     }
5359   /* This will be non-NULL during relaxation.  The assumption is that
5360      the size of one of these sections will never grow, only shrink,
5361      so we can use the larger buffer we allocated before.  */
5362   else if (frvfdpic_got_section (info)->contents == NULL)
5363     {
5364       frvfdpic_got_section (info)->contents =
5365         (bfd_byte *) bfd_zalloc (dynobj,
5366                                  frvfdpic_got_section (info)->size);
5367       if (frvfdpic_got_section (info)->contents == NULL)
5368         return FALSE;
5369     }
5370
5371   if (frvfdpic_gotrel_section (info))
5372     /* Subtract the number of lzplt entries, since those will generate
5373        relocations in the pltrel section.  */
5374     frvfdpic_gotrel_section (info)->size =
5375       (gpinfop->g.relocs - gpinfop->g.lzplt / 8)
5376       * get_elf_backend_data (output_bfd)->s->sizeof_rel;
5377   else
5378     BFD_ASSERT (gpinfop->g.relocs == 0);
5379   if (frvfdpic_gotrel_section (info)->size == 0)
5380     frvfdpic_gotrel_section (info)->flags |= SEC_EXCLUDE;
5381   else if (frvfdpic_gotrel_section (info)->contents == NULL)
5382     {
5383       frvfdpic_gotrel_section (info)->contents =
5384         (bfd_byte *) bfd_zalloc (dynobj,
5385                                  frvfdpic_gotrel_section (info)->size);
5386       if (frvfdpic_gotrel_section (info)->contents == NULL)
5387         return FALSE;
5388     }
5389
5390   frvfdpic_gotfixup_section (info)->size = (gpinfop->g.fixups + 1) * 4;
5391   if (frvfdpic_gotfixup_section (info)->size == 0)
5392     frvfdpic_gotfixup_section (info)->flags |= SEC_EXCLUDE;
5393   else if (frvfdpic_gotfixup_section (info)->contents == NULL)
5394     {
5395       frvfdpic_gotfixup_section (info)->contents =
5396         (bfd_byte *) bfd_zalloc (dynobj,
5397                                  frvfdpic_gotfixup_section (info)->size);
5398       if (frvfdpic_gotfixup_section (info)->contents == NULL)
5399         return FALSE;
5400     }
5401
5402   if (frvfdpic_pltrel_section (info))
5403     {
5404       frvfdpic_pltrel_section (info)->size =
5405         gpinfop->g.lzplt / 8
5406         * get_elf_backend_data (output_bfd)->s->sizeof_rel;
5407       if (frvfdpic_pltrel_section (info)->size == 0)
5408         frvfdpic_pltrel_section (info)->flags |= SEC_EXCLUDE;
5409       else if (frvfdpic_pltrel_section (info)->contents == NULL)
5410         {
5411           frvfdpic_pltrel_section (info)->contents =
5412             (bfd_byte *) bfd_zalloc (dynobj,
5413                                      frvfdpic_pltrel_section (info)->size);
5414           if (frvfdpic_pltrel_section (info)->contents == NULL)
5415             return FALSE;
5416         }
5417     }
5418
5419   /* Add 4 bytes for every block of at most 65535 lazy PLT entries,
5420      such that there's room for the additional instruction needed to
5421      call the resolver.  Since _frvfdpic_assign_got_entries didn't
5422      account for them, our block size is 4 bytes smaller than the real
5423      block size.  */
5424   if (frvfdpic_plt_section (info))
5425     {
5426       frvfdpic_plt_section (info)->size = gpinfop->g.lzplt
5427         + ((gpinfop->g.lzplt + (FRVFDPIC_LZPLT_BLOCK_SIZE - 4) - 8)
5428            / (FRVFDPIC_LZPLT_BLOCK_SIZE - 4) * 4);
5429     }
5430
5431   /* Reset it, such that _frvfdpic_assign_plt_entries() can use it to
5432      actually assign lazy PLT entries addresses.  */
5433   gpinfop->g.lzplt = 0;
5434
5435   /* Save information that we're going to need to generate GOT and PLT
5436      entries.  */
5437   frvfdpic_got_initial_offset (info) = -gpinfop->gothilo.tmin;
5438
5439   if (get_elf_backend_data (output_bfd)->want_got_sym)
5440     elf_hash_table (info)->hgot->root.u.def.value
5441       = frvfdpic_got_initial_offset (info);
5442
5443   if (frvfdpic_plt_section (info))
5444     frvfdpic_plt_initial_offset (info) =
5445       frvfdpic_plt_section (info)->size;
5446
5447   /* Allocate a ret statement at plt_initial_offset, to be used by
5448      locally-resolved TLS descriptors.  */
5449   if (gpinfop->g.tls_ret_refs)
5450     frvfdpic_plt_section (info)->size += 4;
5451
5452   htab_traverse (frvfdpic_relocs_info (info), _frvfdpic_assign_plt_entries,
5453                  gpinfop);
5454
5455   /* Allocate the PLT section contents only after
5456      _frvfdpic_assign_plt_entries has a chance to add the size of the
5457      non-lazy PLT entries.  */
5458   if (frvfdpic_plt_section (info))
5459     {
5460       if (frvfdpic_plt_section (info)->size == 0)
5461         frvfdpic_plt_section (info)->flags |= SEC_EXCLUDE;
5462       else if (frvfdpic_plt_section (info)->contents == NULL)
5463         {
5464           frvfdpic_plt_section (info)->contents =
5465             (bfd_byte *) bfd_zalloc (dynobj,
5466                                      frvfdpic_plt_section (info)->size);
5467           if (frvfdpic_plt_section (info)->contents == NULL)
5468             return FALSE;
5469         }
5470     }
5471
5472   return TRUE;
5473 }
5474
5475 /* Set the sizes of the dynamic sections.  */
5476
5477 static bfd_boolean
5478 elf32_frvfdpic_size_dynamic_sections (bfd *output_bfd,
5479                                       struct bfd_link_info *info)
5480 {
5481   bfd *dynobj;
5482   asection *s;
5483   struct _frvfdpic_dynamic_got_plt_info gpinfo;
5484
5485   dynobj = elf_hash_table (info)->dynobj;
5486   BFD_ASSERT (dynobj != NULL);
5487
5488   if (elf_hash_table (info)->dynamic_sections_created)
5489     {
5490       /* Set the contents of the .interp section to the interpreter.  */
5491       if (info->executable)
5492         {
5493           s = bfd_get_section_by_name (dynobj, ".interp");
5494           BFD_ASSERT (s != NULL);
5495           s->size = sizeof ELF_DYNAMIC_INTERPRETER;
5496           s->contents = (bfd_byte *) ELF_DYNAMIC_INTERPRETER;
5497         }
5498     }
5499
5500   memset (&gpinfo, 0, sizeof (gpinfo));
5501   gpinfo.g.info = info;
5502
5503   for (;;)
5504     {
5505       htab_t relocs = frvfdpic_relocs_info (info);
5506
5507       htab_traverse (relocs, _frvfdpic_resolve_final_relocs_info, &relocs);
5508
5509       if (relocs == frvfdpic_relocs_info (info))
5510         break;
5511     }
5512
5513   htab_traverse (frvfdpic_relocs_info (info), _frvfdpic_count_got_plt_entries,
5514                  &gpinfo.g);
5515
5516   /* Allocate space to save the summary information, we're going to
5517      use it if we're doing relaxations.  */
5518   frvfdpic_dynamic_got_plt_info (info) = bfd_alloc (dynobj, sizeof (gpinfo.g));
5519
5520   if (!_frvfdpic_size_got_plt (output_bfd, &gpinfo))
5521     return FALSE;
5522
5523   if (elf_hash_table (info)->dynamic_sections_created)
5524     {
5525       if (frvfdpic_got_section (info)->size)
5526         if (!_bfd_elf_add_dynamic_entry (info, DT_PLTGOT, 0))
5527           return FALSE;
5528
5529       if (frvfdpic_pltrel_section (info)->size)
5530         if (!_bfd_elf_add_dynamic_entry (info, DT_PLTRELSZ, 0)
5531             || !_bfd_elf_add_dynamic_entry (info, DT_PLTREL, DT_REL)
5532             || !_bfd_elf_add_dynamic_entry (info, DT_JMPREL, 0))
5533           return FALSE;
5534
5535       if (frvfdpic_gotrel_section (info)->size)
5536         if (!_bfd_elf_add_dynamic_entry (info, DT_REL, 0)
5537             || !_bfd_elf_add_dynamic_entry (info, DT_RELSZ, 0)
5538             || !_bfd_elf_add_dynamic_entry (info, DT_RELENT,
5539                                             sizeof (Elf32_External_Rel)))
5540           return FALSE;
5541     }
5542
5543   return TRUE;
5544 }
5545
5546 static bfd_boolean
5547 elf32_frvfdpic_always_size_sections (bfd *output_bfd,
5548                                      struct bfd_link_info *info)
5549 {
5550   if (!info->relocatable)
5551     {
5552       struct elf_link_hash_entry *h;
5553
5554       /* Force a PT_GNU_STACK segment to be created.  */
5555       if (! elf_tdata (output_bfd)->stack_flags)
5556         elf_tdata (output_bfd)->stack_flags = PF_R | PF_W | PF_X;
5557
5558       /* Define __stacksize if it's not defined yet.  */
5559       h = elf_link_hash_lookup (elf_hash_table (info), "__stacksize",
5560                                 FALSE, FALSE, FALSE);
5561       if (! h || h->root.type != bfd_link_hash_defined
5562           || h->type != STT_OBJECT
5563           || !h->def_regular)
5564         {
5565           struct bfd_link_hash_entry *bh = NULL;
5566
5567           if (!(_bfd_generic_link_add_one_symbol
5568                 (info, output_bfd, "__stacksize",
5569                  BSF_GLOBAL, bfd_abs_section_ptr, DEFAULT_STACK_SIZE,
5570                  (const char *) NULL, FALSE,
5571                  get_elf_backend_data (output_bfd)->collect, &bh)))
5572             return FALSE;
5573
5574           h = (struct elf_link_hash_entry *) bh;
5575           h->def_regular = 1;
5576           h->type = STT_OBJECT;
5577           /* This one must NOT be hidden.  */
5578         }
5579     }
5580
5581   return TRUE;
5582 }
5583
5584 /* Look for opportunities to relax TLS relocations.  We can assume
5585    we're linking the main executable or a static-tls library, since
5586    otherwise we wouldn't have got here.  */
5587
5588 static int
5589 _frvfdpic_relax_got_plt_entries (void **entryp, void *dinfo_)
5590 {
5591   struct frvfdpic_relocs_info *entry = *entryp;
5592   struct _frvfdpic_dynamic_got_info *dinfo = dinfo_;
5593
5594   _frvfdpic_relax_tls_entries (entry, dinfo, TRUE);
5595
5596   return 1;
5597 }
5598
5599 static bfd_boolean
5600 elf32_frvfdpic_relax_section (bfd *abfd ATTRIBUTE_UNUSED, asection *sec,
5601                               struct bfd_link_info *info, bfd_boolean *again)
5602 {
5603   struct _frvfdpic_dynamic_got_plt_info gpinfo;
5604
5605   /* If we return early, we didn't change anything.  */
5606   *again = FALSE;
5607
5608   /* We'll do our thing when requested to relax the GOT section.  */
5609   if (sec != frvfdpic_got_section (info))
5610     return TRUE;
5611
5612   /* We can only relax when linking the main executable or a library
5613      that can't be dlopened.  */
5614   if (! info->executable && ! (info->flags & DF_STATIC_TLS))
5615     return TRUE;
5616
5617   /* If there isn't a TLS section for this binary, we can't do
5618      anything about its TLS relocations (it probably doesn't have
5619      any.  */
5620   if (elf_hash_table (info)->tls_sec == NULL)
5621     return TRUE;
5622
5623   memset (&gpinfo, 0, sizeof (gpinfo));
5624   memcpy (&gpinfo.g, frvfdpic_dynamic_got_plt_info (info), sizeof (gpinfo.g));
5625
5626   /* Now look for opportunities to relax, adjusting the GOT usage
5627      as needed.  */
5628   htab_traverse (frvfdpic_relocs_info (info),
5629                  _frvfdpic_relax_got_plt_entries,
5630                  &gpinfo.g);
5631
5632   /* If we changed anything, reset and re-assign GOT and PLT entries.  */
5633   if (memcmp (frvfdpic_dynamic_got_plt_info (info),
5634               &gpinfo.g, sizeof (gpinfo.g)) != 0)
5635     {
5636       /* Clear GOT and PLT assignments.  */
5637       htab_traverse (frvfdpic_relocs_info (info),
5638                      _frvfdpic_reset_got_plt_entries,
5639                      NULL);
5640
5641       /* The owner of the TLS section is the output bfd.  There should
5642          be a better way to get to it.  */
5643       if (!_frvfdpic_size_got_plt (elf_hash_table (info)->tls_sec->owner,
5644                                    &gpinfo))
5645         return FALSE;
5646
5647       /* Repeat until we don't make any further changes.  We could fail to
5648          introduce changes in a round if, for example, the 12-bit range is
5649          full, but we later release some space by getting rid of TLS
5650          descriptors in it.  We have to repeat the whole process because
5651          we might have changed the size of a section processed before this
5652          one.  */
5653       *again = TRUE;
5654     }
5655
5656   return TRUE;
5657 }
5658
5659 static bfd_boolean
5660 elf32_frvfdpic_modify_program_headers (bfd *output_bfd,
5661                                        struct bfd_link_info *info)
5662 {
5663   struct elf_obj_tdata *tdata = elf_tdata (output_bfd);
5664   struct elf_segment_map *m;
5665   Elf_Internal_Phdr *p;
5666
5667   /* objcopy and strip preserve what's already there using
5668      elf32_frvfdpic_copy_private_bfd_data ().  */
5669   if (! info)
5670     return TRUE;
5671
5672   for (p = tdata->phdr, m = tdata->segment_map; m != NULL; m = m->next, p++)
5673     if (m->p_type == PT_GNU_STACK)
5674       break;
5675
5676   if (m)
5677     {
5678       struct elf_link_hash_entry *h;
5679
5680       /* Obtain the pointer to the __stacksize symbol.  */
5681       h = elf_link_hash_lookup (elf_hash_table (info), "__stacksize",
5682                                 FALSE, FALSE, FALSE);
5683       if (h)
5684         {
5685           while (h->root.type == bfd_link_hash_indirect
5686                  || h->root.type == bfd_link_hash_warning)
5687             h = (struct elf_link_hash_entry *) h->root.u.i.link;
5688           BFD_ASSERT (h->root.type == bfd_link_hash_defined);
5689         }
5690
5691       /* Set the header p_memsz from the symbol value.  We
5692          intentionally ignore the symbol section.  */
5693       if (h && h->root.type == bfd_link_hash_defined)
5694         p->p_memsz = h->root.u.def.value;
5695       else
5696         p->p_memsz = DEFAULT_STACK_SIZE;
5697
5698       p->p_align = 8;
5699     }
5700
5701   return TRUE;
5702 }
5703
5704 /* Fill in code and data in dynamic sections.  */
5705
5706 static bfd_boolean
5707 elf32_frv_finish_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
5708                                    struct bfd_link_info *info ATTRIBUTE_UNUSED)
5709 {
5710   /* Nothing to be done for non-FDPIC.  */
5711   return TRUE;
5712 }
5713
5714 static bfd_boolean
5715 elf32_frvfdpic_finish_dynamic_sections (bfd *output_bfd,
5716                                         struct bfd_link_info *info)
5717 {
5718   bfd *dynobj;
5719   asection *sdyn;
5720
5721   dynobj = elf_hash_table (info)->dynobj;
5722
5723   if (frvfdpic_dynamic_got_plt_info (info))
5724     {
5725       BFD_ASSERT (frvfdpic_dynamic_got_plt_info (info)->tls_ret_refs == 0);
5726     }
5727   if (frvfdpic_got_section (info))
5728     {
5729       BFD_ASSERT (frvfdpic_gotrel_section (info)->size
5730                   == (frvfdpic_gotrel_section (info)->reloc_count
5731                       * sizeof (Elf32_External_Rel)));
5732
5733       if (frvfdpic_gotfixup_section (info))
5734         {
5735           struct elf_link_hash_entry *hgot = elf_hash_table (info)->hgot;
5736           bfd_vma got_value = hgot->root.u.def.value
5737             + hgot->root.u.def.section->output_section->vma
5738             + hgot->root.u.def.section->output_offset;
5739           struct bfd_link_hash_entry *hend;
5740
5741           _frvfdpic_add_rofixup (output_bfd, frvfdpic_gotfixup_section (info),
5742                                  got_value, 0);
5743
5744           if (frvfdpic_gotfixup_section (info)->size
5745               != (frvfdpic_gotfixup_section (info)->reloc_count * 4))
5746             {
5747             error:
5748               (*_bfd_error_handler)
5749                 ("LINKER BUG: .rofixup section size mismatch");
5750               return FALSE;
5751             }
5752
5753           hend = bfd_link_hash_lookup (info->hash, "__ROFIXUP_END__",
5754                                        FALSE, FALSE, TRUE);
5755           if (hend
5756               && (hend->type == bfd_link_hash_defined
5757                   || hend->type == bfd_link_hash_defweak))
5758             {
5759               bfd_vma value =
5760                 frvfdpic_gotfixup_section (info)->output_section->vma
5761                 + frvfdpic_gotfixup_section (info)->output_offset
5762                 + frvfdpic_gotfixup_section (info)->size
5763                 - hend->u.def.section->output_section->vma
5764                 - hend->u.def.section->output_offset;
5765               BFD_ASSERT (hend->u.def.value == value);
5766               if (hend->u.def.value != value)
5767                 goto error;
5768             }
5769         }
5770     }
5771   if (frvfdpic_pltrel_section (info))
5772     {
5773       BFD_ASSERT (frvfdpic_pltrel_section (info)->size
5774                   == (frvfdpic_pltrel_section (info)->reloc_count
5775                       * sizeof (Elf32_External_Rel)));
5776     }
5777
5778
5779   if (elf_hash_table (info)->dynamic_sections_created)
5780     {
5781       Elf32_External_Dyn * dyncon;
5782       Elf32_External_Dyn * dynconend;
5783
5784       sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
5785
5786       BFD_ASSERT (sdyn != NULL);
5787
5788       dyncon = (Elf32_External_Dyn *) sdyn->contents;
5789       dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
5790
5791       for (; dyncon < dynconend; dyncon++)
5792         {
5793           Elf_Internal_Dyn dyn;
5794
5795           bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
5796
5797           switch (dyn.d_tag)
5798             {
5799             default:
5800               break;
5801
5802             case DT_PLTGOT:
5803               dyn.d_un.d_ptr = frvfdpic_got_section (info)->output_section->vma
5804                 + frvfdpic_got_section (info)->output_offset
5805                 + frvfdpic_got_initial_offset (info);
5806               bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
5807               break;
5808
5809             case DT_JMPREL:
5810               dyn.d_un.d_ptr = frvfdpic_pltrel_section (info)
5811                 ->output_section->vma
5812                 + frvfdpic_pltrel_section (info)->output_offset;
5813               bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
5814               break;
5815
5816             case DT_PLTRELSZ:
5817               dyn.d_un.d_val = frvfdpic_pltrel_section (info)->size;
5818               bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
5819               break;
5820             }
5821         }
5822     }
5823
5824   return TRUE;
5825 }
5826
5827 /* Adjust a symbol defined by a dynamic object and referenced by a
5828    regular object.  */
5829
5830 static bfd_boolean
5831 elf32_frvfdpic_adjust_dynamic_symbol
5832 (struct bfd_link_info *info ATTRIBUTE_UNUSED,
5833  struct elf_link_hash_entry *h ATTRIBUTE_UNUSED)
5834 {
5835   bfd * dynobj;
5836
5837   dynobj = elf_hash_table (info)->dynobj;
5838
5839   /* Make sure we know what is going on here.  */
5840   BFD_ASSERT (dynobj != NULL
5841               && (h->u.weakdef != NULL
5842                   || (h->def_dynamic
5843                       && h->ref_regular
5844                       && !h->def_regular)));
5845
5846   /* If this is a weak symbol, and there is a real definition, the
5847      processor independent code will have arranged for us to see the
5848      real definition first, and we can just use the same value.  */
5849   if (h->u.weakdef != NULL)
5850     {
5851       BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
5852                   || h->u.weakdef->root.type == bfd_link_hash_defweak);
5853       h->root.u.def.section = h->u.weakdef->root.u.def.section;
5854       h->root.u.def.value = h->u.weakdef->root.u.def.value;
5855     }
5856
5857   return TRUE;
5858 }
5859
5860 /* Perform any actions needed for dynamic symbols.  */
5861
5862 static bfd_boolean
5863 elf32_frvfdpic_finish_dynamic_symbol
5864 (bfd *output_bfd ATTRIBUTE_UNUSED,
5865  struct bfd_link_info *info ATTRIBUTE_UNUSED,
5866  struct elf_link_hash_entry *h ATTRIBUTE_UNUSED,
5867  Elf_Internal_Sym *sym ATTRIBUTE_UNUSED)
5868 {
5869   return TRUE;
5870 }
5871
5872 /* Decide whether to attempt to turn absptr or lsda encodings in
5873    shared libraries into pcrel within the given input section.  */
5874
5875 static bfd_boolean
5876 frvfdpic_elf_use_relative_eh_frame
5877 (bfd *input_bfd ATTRIBUTE_UNUSED,
5878  struct bfd_link_info *info ATTRIBUTE_UNUSED,
5879  asection *eh_frame_section ATTRIBUTE_UNUSED)
5880 {
5881   /* We can't use PC-relative encodings in FDPIC binaries, in general.  */
5882   return FALSE;
5883 }
5884
5885 /* Adjust the contents of an eh_frame_hdr section before they're output.  */
5886
5887 static bfd_byte
5888 frvfdpic_elf_encode_eh_address (bfd *abfd,
5889                                 struct bfd_link_info *info,
5890                                 asection *osec, bfd_vma offset,
5891                                 asection *loc_sec, bfd_vma loc_offset,
5892                                 bfd_vma *encoded)
5893 {
5894   struct elf_link_hash_entry *h;
5895
5896   h = elf_hash_table (info)->hgot;
5897   BFD_ASSERT (h && h->root.type == bfd_link_hash_defined);
5898
5899   if (! h || (_frvfdpic_osec_to_segment (abfd, osec)
5900               == _frvfdpic_osec_to_segment (abfd, loc_sec->output_section)))
5901     return _bfd_elf_encode_eh_address (abfd, info, osec, offset,
5902                                        loc_sec, loc_offset, encoded);
5903
5904   BFD_ASSERT (_frvfdpic_osec_to_segment (abfd, osec)
5905               == (_frvfdpic_osec_to_segment
5906                   (abfd, h->root.u.def.section->output_section)));
5907
5908   *encoded = osec->vma + offset
5909     - (h->root.u.def.value
5910        + h->root.u.def.section->output_section->vma
5911        + h->root.u.def.section->output_offset);
5912
5913   return DW_EH_PE_datarel | DW_EH_PE_sdata4;
5914 }
5915
5916 /* Look through the relocs for a section during the first phase.
5917
5918    Besides handling virtual table relocs for gc, we have to deal with
5919    all sorts of PIC-related relocations.  We describe below the
5920    general plan on how to handle such relocations, even though we only
5921    collect information at this point, storing them in hash tables for
5922    perusal of later passes.
5923
5924    32 relocations are propagated to the linker output when creating
5925    position-independent output.  LO16 and HI16 relocations are not
5926    supposed to be encountered in this case.
5927
5928    LABEL16 should always be resolvable by the linker, since it's only
5929    used by branches.
5930
5931    LABEL24, on the other hand, is used by calls.  If it turns out that
5932    the target of a call is a dynamic symbol, a PLT entry must be
5933    created for it, which triggers the creation of a private function
5934    descriptor and, unless lazy binding is disabled, a lazy PLT entry.
5935
5936    GPREL relocations require the referenced symbol to be in the same
5937    segment as _gp, but this can only be checked later.
5938
5939    All GOT, GOTOFF and FUNCDESC relocations require a .got section to
5940    exist.  LABEL24 might as well, since it may require a PLT entry,
5941    that will require a got.
5942
5943    Non-FUNCDESC GOT relocations require a GOT entry to be created
5944    regardless of whether the symbol is dynamic.  However, since a
5945    global symbol that turns out to not be exported may have the same
5946    address of a non-dynamic symbol, we don't assign GOT entries at
5947    this point, such that we can share them in this case.  A relocation
5948    for the GOT entry always has to be created, be it to offset a
5949    private symbol by the section load address, be it to get the symbol
5950    resolved dynamically.
5951
5952    FUNCDESC GOT relocations require a GOT entry to be created, and
5953    handled as if a FUNCDESC relocation was applied to the GOT entry in
5954    an object file.
5955
5956    FUNCDESC relocations referencing a symbol that turns out to NOT be
5957    dynamic cause a private function descriptor to be created.  The
5958    FUNCDESC relocation then decays to a 32 relocation that points at
5959    the private descriptor.  If the symbol is dynamic, the FUNCDESC
5960    relocation is propagated to the linker output, such that the
5961    dynamic linker creates the canonical descriptor, pointing to the
5962    dynamically-resolved definition of the function.
5963
5964    Non-FUNCDESC GOTOFF relocations must always refer to non-dynamic
5965    symbols that are assigned to the same segment as the GOT, but we
5966    can only check this later, after we know the complete set of
5967    symbols defined and/or exported.
5968
5969    FUNCDESC GOTOFF relocations require a function descriptor to be
5970    created and, unless lazy binding is disabled or the symbol is not
5971    dynamic, a lazy PLT entry.  Since we can't tell at this point
5972    whether a symbol is going to be dynamic, we have to decide later
5973    whether to create a lazy PLT entry or bind the descriptor directly
5974    to the private function.
5975
5976    FUNCDESC_VALUE relocations are not supposed to be present in object
5977    files, but they may very well be simply propagated to the linker
5978    output, since they have no side effect.
5979
5980
5981    A function descriptor always requires a FUNCDESC_VALUE relocation.
5982    Whether it's in .plt.rel or not depends on whether lazy binding is
5983    enabled and on whether the referenced symbol is dynamic.
5984
5985    The existence of a lazy PLT requires the resolverStub lazy PLT
5986    entry to be present.
5987
5988
5989    As for assignment of GOT, PLT and lazy PLT entries, and private
5990    descriptors, we might do them all sequentially, but we can do
5991    better than that.  For example, we can place GOT entries and
5992    private function descriptors referenced using 12-bit operands
5993    closer to the PIC register value, such that these relocations don't
5994    overflow.  Those that are only referenced with LO16 relocations
5995    could come next, but we may as well place PLT-required function
5996    descriptors in the 12-bit range to make them shorter.  Symbols
5997    referenced with LO16/HI16 may come next, but we may place
5998    additional function descriptors in the 16-bit range if we can
5999    reliably tell that we've already placed entries that are ever
6000    referenced with only LO16.  PLT entries are therefore generated as
6001    small as possible, while not introducing relocation overflows in
6002    GOT or FUNCDESC_GOTOFF relocations.  Lazy PLT entries could be
6003    generated before or after PLT entries, but not intermingled with
6004    them, such that we can have more lazy PLT entries in range for a
6005    branch to the resolverStub.  The resolverStub should be emitted at
6006    the most distant location from the first lazy PLT entry such that
6007    it's still in range for a branch, or closer, if there isn't a need
6008    for so many lazy PLT entries.  Additional lazy PLT entries may be
6009    emitted after the resolverStub, as long as branches are still in
6010    range.  If the branch goes out of range, longer lazy PLT entries
6011    are emitted.
6012
6013    We could further optimize PLT and lazy PLT entries by giving them
6014    priority in assignment to closer-to-gr17 locations depending on the
6015    number of occurrences of references to them (assuming a function
6016    that's called more often is more important for performance, so its
6017    PLT entry should be faster), or taking hints from the compiler.
6018    Given infinite time and money... :-)  */
6019
6020 static bfd_boolean
6021 elf32_frv_check_relocs (abfd, info, sec, relocs)
6022      bfd *abfd;
6023      struct bfd_link_info *info;
6024      asection *sec;
6025      const Elf_Internal_Rela *relocs;
6026 {
6027   Elf_Internal_Shdr *symtab_hdr;
6028   struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
6029   const Elf_Internal_Rela *rel;
6030   const Elf_Internal_Rela *rel_end;
6031   bfd *dynobj;
6032   struct frvfdpic_relocs_info *picrel;
6033
6034   if (info->relocatable)
6035     return TRUE;
6036
6037   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
6038   sym_hashes = elf_sym_hashes (abfd);
6039   sym_hashes_end = sym_hashes + symtab_hdr->sh_size/sizeof(Elf32_External_Sym);
6040   if (!elf_bad_symtab (abfd))
6041     sym_hashes_end -= symtab_hdr->sh_info;
6042
6043   dynobj = elf_hash_table (info)->dynobj;
6044   rel_end = relocs + sec->reloc_count;
6045   for (rel = relocs; rel < rel_end; rel++)
6046     {
6047       struct elf_link_hash_entry *h;
6048       unsigned long r_symndx;
6049
6050       r_symndx = ELF32_R_SYM (rel->r_info);
6051       if (r_symndx < symtab_hdr->sh_info)
6052         h = NULL;
6053       else
6054         {
6055           h = sym_hashes[r_symndx - symtab_hdr->sh_info];
6056           while (h->root.type == bfd_link_hash_indirect
6057                  || h->root.type == bfd_link_hash_warning)
6058             h = (struct elf_link_hash_entry *) h->root.u.i.link;
6059         }
6060
6061       switch (ELF32_R_TYPE (rel->r_info))
6062         {
6063         case R_FRV_GETTLSOFF:
6064         case R_FRV_TLSDESC_VALUE:
6065         case R_FRV_GOTTLSDESC12:
6066         case R_FRV_GOTTLSDESCHI:
6067         case R_FRV_GOTTLSDESCLO:
6068         case R_FRV_GOTTLSOFF12:
6069         case R_FRV_GOTTLSOFFHI:
6070         case R_FRV_GOTTLSOFFLO:
6071         case R_FRV_TLSOFF:
6072         case R_FRV_GOT12:
6073         case R_FRV_GOTHI:
6074         case R_FRV_GOTLO:
6075         case R_FRV_FUNCDESC_GOT12:
6076         case R_FRV_FUNCDESC_GOTHI:
6077         case R_FRV_FUNCDESC_GOTLO:
6078         case R_FRV_GOTOFF12:
6079         case R_FRV_GOTOFFHI:
6080         case R_FRV_GOTOFFLO:
6081         case R_FRV_FUNCDESC_GOTOFF12:
6082         case R_FRV_FUNCDESC_GOTOFFHI:
6083         case R_FRV_FUNCDESC_GOTOFFLO:
6084         case R_FRV_FUNCDESC:
6085         case R_FRV_FUNCDESC_VALUE:
6086         case R_FRV_TLSMOFF12:
6087         case R_FRV_TLSMOFFHI:
6088         case R_FRV_TLSMOFFLO:
6089         case R_FRV_TLSMOFF:
6090           if (! IS_FDPIC (abfd))
6091             goto bad_reloc;
6092           /* Fall through.  */
6093         case R_FRV_GPREL12:
6094         case R_FRV_GPRELU12:
6095         case R_FRV_GPRELHI:
6096         case R_FRV_GPRELLO:
6097         case R_FRV_LABEL24:
6098         case R_FRV_32:
6099           if (! dynobj)
6100             {
6101               elf_hash_table (info)->dynobj = dynobj = abfd;
6102               if (! _frv_create_got_section (abfd, info))
6103                 return FALSE;
6104             }
6105           if (! IS_FDPIC (abfd))
6106             {
6107               picrel = NULL;
6108               break;
6109             }
6110           if (h != NULL)
6111             {
6112               if (h->dynindx == -1)
6113                 switch (ELF_ST_VISIBILITY (h->other))
6114                   {
6115                   case STV_INTERNAL:
6116                   case STV_HIDDEN:
6117                     break;
6118                   default:
6119                     bfd_elf_link_record_dynamic_symbol (info, h);
6120                     break;
6121                   }
6122               picrel
6123                 = frvfdpic_relocs_info_for_global (frvfdpic_relocs_info (info),
6124                                                    abfd, h,
6125                                                    rel->r_addend, INSERT);
6126             }
6127           else
6128             picrel = frvfdpic_relocs_info_for_local (frvfdpic_relocs_info
6129                                                      (info), abfd, r_symndx,
6130                                                      rel->r_addend, INSERT);
6131           if (! picrel)
6132             return FALSE;
6133           break;
6134
6135         default:
6136           picrel = NULL;
6137           break;
6138         }
6139
6140       switch (ELF32_R_TYPE (rel->r_info))
6141         {
6142         case R_FRV_LABEL24:
6143           if (IS_FDPIC (abfd))
6144             picrel->call = 1;
6145           break;
6146
6147         case R_FRV_FUNCDESC_VALUE:
6148           picrel->relocsfdv++;
6149           if (bfd_get_section_flags (abfd, sec) & SEC_ALLOC)
6150             picrel->relocs32--;
6151           /* Fall through.  */
6152
6153         case R_FRV_32:
6154           if (! IS_FDPIC (abfd))
6155             break;
6156
6157           picrel->sym = 1;
6158           if (bfd_get_section_flags (abfd, sec) & SEC_ALLOC)
6159             picrel->relocs32++;
6160           break;
6161
6162         case R_FRV_GOT12:
6163           picrel->got12 = 1;
6164           break;
6165
6166         case R_FRV_GOTHI:
6167         case R_FRV_GOTLO:
6168           picrel->gothilo = 1;
6169           break;
6170
6171         case R_FRV_FUNCDESC_GOT12:
6172           picrel->fdgot12 = 1;
6173           break;
6174
6175         case R_FRV_FUNCDESC_GOTHI:
6176         case R_FRV_FUNCDESC_GOTLO:
6177           picrel->fdgothilo = 1;
6178           break;
6179
6180         case R_FRV_GOTOFF12:
6181         case R_FRV_GOTOFFHI:
6182         case R_FRV_GOTOFFLO:
6183           picrel->gotoff = 1;
6184           break;
6185
6186         case R_FRV_FUNCDESC_GOTOFF12:
6187           picrel->fdgoff12 = 1;
6188           break;
6189
6190         case R_FRV_FUNCDESC_GOTOFFHI:
6191         case R_FRV_FUNCDESC_GOTOFFLO:
6192           picrel->fdgoffhilo = 1;
6193           break;
6194
6195         case R_FRV_FUNCDESC:
6196           picrel->fd = 1;
6197           picrel->relocsfd++;
6198           break;
6199
6200         case R_FRV_GETTLSOFF:
6201           picrel->tlsplt = 1;
6202           break;
6203
6204         case R_FRV_TLSDESC_VALUE:
6205           picrel->relocstlsd++;
6206           goto bad_reloc;
6207
6208         case R_FRV_GOTTLSDESC12:
6209           picrel->tlsdesc12 = 1;
6210           break;
6211
6212         case R_FRV_GOTTLSDESCHI:
6213         case R_FRV_GOTTLSDESCLO:
6214           picrel->tlsdeschilo = 1;
6215           break;
6216
6217         case R_FRV_TLSMOFF12:
6218         case R_FRV_TLSMOFFHI:
6219         case R_FRV_TLSMOFFLO:
6220         case R_FRV_TLSMOFF:
6221           break;
6222
6223         case R_FRV_GOTTLSOFF12:
6224           picrel->tlsoff12 = 1;
6225           info->flags |= DF_STATIC_TLS;
6226           break;
6227
6228         case R_FRV_GOTTLSOFFHI:
6229         case R_FRV_GOTTLSOFFLO:
6230           picrel->tlsoffhilo = 1;
6231           info->flags |= DF_STATIC_TLS;
6232           break;
6233
6234         case R_FRV_TLSOFF:
6235           picrel->relocstlsoff++;
6236           info->flags |= DF_STATIC_TLS;
6237           goto bad_reloc;
6238
6239         /* This relocation describes the C++ object vtable hierarchy.
6240            Reconstruct it for later use during GC.  */
6241         case R_FRV_GNU_VTINHERIT:
6242           if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
6243             return FALSE;
6244           break;
6245
6246         /* This relocation describes which C++ vtable entries are actually
6247            used.  Record for later use during GC.  */
6248         case R_FRV_GNU_VTENTRY:
6249           if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
6250             return FALSE;
6251           break;
6252
6253         case R_FRV_LABEL16:
6254         case R_FRV_LO16:
6255         case R_FRV_HI16:
6256         case R_FRV_GPREL12:
6257         case R_FRV_GPRELU12:
6258         case R_FRV_GPREL32:
6259         case R_FRV_GPRELHI:
6260         case R_FRV_GPRELLO:
6261         case R_FRV_TLSDESC_RELAX:
6262         case R_FRV_GETTLSOFF_RELAX:
6263         case R_FRV_TLSOFF_RELAX:
6264           break;
6265
6266         default:
6267         bad_reloc:
6268           (*_bfd_error_handler)
6269             (_("%B: unsupported relocation type %i"),
6270              abfd, ELF32_R_TYPE (rel->r_info));
6271           return FALSE;
6272         }
6273     }
6274
6275   return TRUE;
6276 }
6277
6278 \f
6279 /* Return the machine subcode from the ELF e_flags header.  */
6280
6281 static int
6282 elf32_frv_machine (abfd)
6283      bfd *abfd;
6284 {
6285   switch (elf_elfheader (abfd)->e_flags & EF_FRV_CPU_MASK)
6286     {
6287     default:                break;
6288     case EF_FRV_CPU_FR550:  return bfd_mach_fr550;
6289     case EF_FRV_CPU_FR500:  return bfd_mach_fr500;
6290     case EF_FRV_CPU_FR450:  return bfd_mach_fr450;
6291     case EF_FRV_CPU_FR405:  return bfd_mach_fr400;
6292     case EF_FRV_CPU_FR400:  return bfd_mach_fr400;
6293     case EF_FRV_CPU_FR300:  return bfd_mach_fr300;
6294     case EF_FRV_CPU_SIMPLE: return bfd_mach_frvsimple;
6295     case EF_FRV_CPU_TOMCAT: return bfd_mach_frvtomcat;
6296     }
6297
6298   return bfd_mach_frv;
6299 }
6300
6301 /* Set the right machine number for a FRV ELF file.  */
6302
6303 static bfd_boolean
6304 elf32_frv_object_p (abfd)
6305      bfd *abfd;
6306 {
6307   bfd_default_set_arch_mach (abfd, bfd_arch_frv, elf32_frv_machine (abfd));
6308   return (((elf_elfheader (abfd)->e_flags & EF_FRV_FDPIC) != 0)
6309           == (IS_FDPIC (abfd)));
6310 }
6311 \f
6312 /* Function to set the ELF flag bits.  */
6313
6314 static bfd_boolean
6315 frv_elf_set_private_flags (abfd, flags)
6316      bfd *abfd;
6317      flagword flags;
6318 {
6319   elf_elfheader (abfd)->e_flags = flags;
6320   elf_flags_init (abfd) = TRUE;
6321   return TRUE;
6322 }
6323
6324 /* Copy backend specific data from one object module to another.  */
6325
6326 static bfd_boolean
6327 frv_elf_copy_private_bfd_data (ibfd, obfd)
6328      bfd *ibfd;
6329      bfd *obfd;
6330 {
6331   if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
6332       || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
6333     return TRUE;
6334
6335   BFD_ASSERT (!elf_flags_init (obfd)
6336               || elf_elfheader (obfd)->e_flags == elf_elfheader (ibfd)->e_flags);
6337
6338   elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
6339   elf_flags_init (obfd) = TRUE;
6340   return TRUE;
6341 }
6342
6343 /* Return true if the architecture described by elf header flag
6344    EXTENSION is an extension of the architecture described by BASE.  */
6345
6346 static bfd_boolean
6347 frv_elf_arch_extension_p (flagword base, flagword extension)
6348 {
6349   if (base == extension)
6350     return TRUE;
6351
6352   /* CPU_GENERIC code can be merged with code for a specific
6353      architecture, in which case the result is marked as being
6354      for the specific architecture.  Everything is therefore
6355      an extension of CPU_GENERIC.  */
6356   if (base == EF_FRV_CPU_GENERIC)
6357     return TRUE;
6358
6359   if (extension == EF_FRV_CPU_FR450)
6360     if (base == EF_FRV_CPU_FR400 || base == EF_FRV_CPU_FR405)
6361       return TRUE;
6362
6363   if (extension == EF_FRV_CPU_FR405)
6364     if (base == EF_FRV_CPU_FR400)
6365       return TRUE;
6366
6367   return FALSE;
6368 }
6369
6370 static bfd_boolean
6371 elf32_frvfdpic_copy_private_bfd_data (bfd *ibfd, bfd *obfd)
6372 {
6373   unsigned i;
6374
6375   if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
6376       || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
6377     return TRUE;
6378
6379   if (! frv_elf_copy_private_bfd_data (ibfd, obfd))
6380     return FALSE;
6381
6382   if (! elf_tdata (ibfd) || ! elf_tdata (ibfd)->phdr
6383       || ! elf_tdata (obfd) || ! elf_tdata (obfd)->phdr)
6384     return TRUE;
6385
6386   /* Copy the stack size.  */
6387   for (i = 0; i < elf_elfheader (ibfd)->e_phnum; i++)
6388     if (elf_tdata (ibfd)->phdr[i].p_type == PT_GNU_STACK)
6389       {
6390         Elf_Internal_Phdr *iphdr = &elf_tdata (ibfd)->phdr[i];
6391
6392         for (i = 0; i < elf_elfheader (obfd)->e_phnum; i++)
6393           if (elf_tdata (obfd)->phdr[i].p_type == PT_GNU_STACK)
6394             {
6395               memcpy (&elf_tdata (obfd)->phdr[i], iphdr, sizeof (*iphdr));
6396
6397               /* Rewrite the phdrs, since we're only called after they
6398                  were first written.  */
6399               if (bfd_seek (obfd, (bfd_signed_vma) get_elf_backend_data (obfd)
6400                             ->s->sizeof_ehdr, SEEK_SET) != 0
6401                   || get_elf_backend_data (obfd)->s
6402                   ->write_out_phdrs (obfd, elf_tdata (obfd)->phdr,
6403                                      elf_elfheader (obfd)->e_phnum) != 0)
6404                 return FALSE;
6405               break;
6406             }
6407
6408         break;
6409       }
6410
6411   return TRUE;
6412 }
6413
6414 /* Merge backend specific data from an object file to the output
6415    object file when linking.  */
6416
6417 static bfd_boolean
6418 frv_elf_merge_private_bfd_data (ibfd, obfd)
6419      bfd *ibfd;
6420      bfd *obfd;
6421 {
6422   flagword old_flags, old_partial;
6423   flagword new_flags, new_partial;
6424   bfd_boolean error = FALSE;
6425   char new_opt[80];
6426   char old_opt[80];
6427
6428   new_opt[0] = old_opt[0] = '\0';
6429   new_flags = elf_elfheader (ibfd)->e_flags;
6430   old_flags = elf_elfheader (obfd)->e_flags;
6431
6432   if (new_flags & EF_FRV_FDPIC)
6433     new_flags &= ~EF_FRV_PIC;
6434
6435 #ifdef DEBUG
6436   (*_bfd_error_handler) ("old_flags = 0x%.8lx, new_flags = 0x%.8lx, init = %s, filename = %s",
6437                          old_flags, new_flags, elf_flags_init (obfd) ? "yes" : "no",
6438                          bfd_get_filename (ibfd));
6439 #endif
6440
6441   if (!elf_flags_init (obfd))                   /* First call, no flags set.  */
6442     {
6443       elf_flags_init (obfd) = TRUE;
6444       old_flags = new_flags;
6445     }
6446
6447   else if (new_flags == old_flags)              /* Compatible flags are ok.  */
6448     ;
6449
6450   else                                          /* Possibly incompatible flags.  */
6451     {
6452       /* Warn if different # of gprs are used.  Note, 0 means nothing is
6453          said about the size of gprs.  */
6454       new_partial = (new_flags & EF_FRV_GPR_MASK);
6455       old_partial = (old_flags & EF_FRV_GPR_MASK);
6456       if (new_partial == old_partial)
6457         ;
6458
6459       else if (new_partial == 0)
6460         ;
6461
6462       else if (old_partial == 0)
6463         old_flags |= new_partial;
6464
6465       else
6466         {
6467           switch (new_partial)
6468             {
6469             default:            strcat (new_opt, " -mgpr-??"); break;
6470             case EF_FRV_GPR_32: strcat (new_opt, " -mgpr-32"); break;
6471             case EF_FRV_GPR_64: strcat (new_opt, " -mgpr-64"); break;
6472             }
6473
6474           switch (old_partial)
6475             {
6476             default:            strcat (old_opt, " -mgpr-??"); break;
6477             case EF_FRV_GPR_32: strcat (old_opt, " -mgpr-32"); break;
6478             case EF_FRV_GPR_64: strcat (old_opt, " -mgpr-64"); break;
6479             }
6480         }
6481
6482       /* Warn if different # of fprs are used.  Note, 0 means nothing is
6483          said about the size of fprs.  */
6484       new_partial = (new_flags & EF_FRV_FPR_MASK);
6485       old_partial = (old_flags & EF_FRV_FPR_MASK);
6486       if (new_partial == old_partial)
6487         ;
6488
6489       else if (new_partial == 0)
6490         ;
6491
6492       else if (old_partial == 0)
6493         old_flags |= new_partial;
6494
6495       else
6496         {
6497           switch (new_partial)
6498             {
6499             default:              strcat (new_opt, " -mfpr-?");      break;
6500             case EF_FRV_FPR_32:   strcat (new_opt, " -mfpr-32");     break;
6501             case EF_FRV_FPR_64:   strcat (new_opt, " -mfpr-64");     break;
6502             case EF_FRV_FPR_NONE: strcat (new_opt, " -msoft-float"); break;
6503             }
6504
6505           switch (old_partial)
6506             {
6507             default:              strcat (old_opt, " -mfpr-?");      break;
6508             case EF_FRV_FPR_32:   strcat (old_opt, " -mfpr-32");     break;
6509             case EF_FRV_FPR_64:   strcat (old_opt, " -mfpr-64");     break;
6510             case EF_FRV_FPR_NONE: strcat (old_opt, " -msoft-float"); break;
6511             }
6512         }
6513
6514       /* Warn if different dword support was used.  Note, 0 means nothing is
6515          said about the dword support.  */
6516       new_partial = (new_flags & EF_FRV_DWORD_MASK);
6517       old_partial = (old_flags & EF_FRV_DWORD_MASK);
6518       if (new_partial == old_partial)
6519         ;
6520
6521       else if (new_partial == 0)
6522         ;
6523
6524       else if (old_partial == 0)
6525         old_flags |= new_partial;
6526
6527       else
6528         {
6529           switch (new_partial)
6530             {
6531             default:               strcat (new_opt, " -mdword-?");  break;
6532             case EF_FRV_DWORD_YES: strcat (new_opt, " -mdword");    break;
6533             case EF_FRV_DWORD_NO:  strcat (new_opt, " -mno-dword"); break;
6534             }
6535
6536           switch (old_partial)
6537             {
6538             default:               strcat (old_opt, " -mdword-?");  break;
6539             case EF_FRV_DWORD_YES: strcat (old_opt, " -mdword");    break;
6540             case EF_FRV_DWORD_NO:  strcat (old_opt, " -mno-dword"); break;
6541             }
6542         }
6543
6544       /* Or in flags that accumulate (ie, if one module uses it, mark that the
6545          feature is used.  */
6546       old_flags |= new_flags & (EF_FRV_DOUBLE
6547                                 | EF_FRV_MEDIA
6548                                 | EF_FRV_MULADD
6549                                 | EF_FRV_NON_PIC_RELOCS);
6550
6551       /* If any module was compiled without -G0, clear the G0 bit.  */
6552       old_flags = ((old_flags & ~ EF_FRV_G0)
6553                    | (old_flags & new_flags & EF_FRV_G0));
6554
6555       /* If any module was compiled without -mnopack, clear the mnopack bit.  */
6556       old_flags = ((old_flags & ~ EF_FRV_NOPACK)
6557                    | (old_flags & new_flags & EF_FRV_NOPACK));
6558
6559       /* We don't have to do anything if the pic flags are the same, or the new
6560          module(s) were compiled with -mlibrary-pic.  */
6561       new_partial = (new_flags & EF_FRV_PIC_FLAGS);
6562       old_partial = (old_flags & EF_FRV_PIC_FLAGS);
6563       if ((new_partial == old_partial) || ((new_partial & EF_FRV_LIBPIC) != 0))
6564         ;
6565
6566       /* If the old module(s) were compiled with -mlibrary-pic, copy in the pic
6567          flags if any from the new module.  */
6568       else if ((old_partial & EF_FRV_LIBPIC) != 0)
6569         old_flags = (old_flags & ~ EF_FRV_PIC_FLAGS) | new_partial;
6570
6571       /* If we have mixtures of -fpic and -fPIC, or in both bits.  */
6572       else if (new_partial != 0 && old_partial != 0)
6573         old_flags |= new_partial;
6574
6575       /* One module was compiled for pic and the other was not, see if we have
6576          had any relocations that are not pic-safe.  */
6577       else
6578         {
6579           if ((old_flags & EF_FRV_NON_PIC_RELOCS) == 0)
6580             old_flags |= new_partial;
6581           else
6582             {
6583               old_flags &= ~ EF_FRV_PIC_FLAGS;
6584 #ifndef FRV_NO_PIC_ERROR
6585               error = TRUE;
6586               (*_bfd_error_handler)
6587                 (_("%s: compiled with %s and linked with modules that use non-pic relocations"),
6588                  bfd_get_filename (ibfd),
6589                  (new_flags & EF_FRV_BIGPIC) ? "-fPIC" : "-fpic");
6590 #endif
6591             }
6592         }
6593
6594       /* Warn if different cpu is used (allow a specific cpu to override
6595          the generic cpu).  */
6596       new_partial = (new_flags & EF_FRV_CPU_MASK);
6597       old_partial = (old_flags & EF_FRV_CPU_MASK);
6598       if (frv_elf_arch_extension_p (new_partial, old_partial))
6599         ;
6600
6601       else if (frv_elf_arch_extension_p (old_partial, new_partial))
6602         old_flags = (old_flags & ~EF_FRV_CPU_MASK) | new_partial;
6603
6604       else
6605         {
6606           switch (new_partial)
6607             {
6608             default:                 strcat (new_opt, " -mcpu=?");      break;
6609             case EF_FRV_CPU_GENERIC: strcat (new_opt, " -mcpu=frv");    break;
6610             case EF_FRV_CPU_SIMPLE:  strcat (new_opt, " -mcpu=simple"); break;
6611             case EF_FRV_CPU_FR550:   strcat (new_opt, " -mcpu=fr550");  break;
6612             case EF_FRV_CPU_FR500:   strcat (new_opt, " -mcpu=fr500");  break;
6613             case EF_FRV_CPU_FR450:   strcat (new_opt, " -mcpu=fr450");  break;
6614             case EF_FRV_CPU_FR405:   strcat (new_opt, " -mcpu=fr405");  break;
6615             case EF_FRV_CPU_FR400:   strcat (new_opt, " -mcpu=fr400");  break;
6616             case EF_FRV_CPU_FR300:   strcat (new_opt, " -mcpu=fr300");  break;
6617             case EF_FRV_CPU_TOMCAT:  strcat (new_opt, " -mcpu=tomcat"); break;
6618             }
6619
6620           switch (old_partial)
6621             {
6622             default:                 strcat (old_opt, " -mcpu=?");      break;
6623             case EF_FRV_CPU_GENERIC: strcat (old_opt, " -mcpu=frv");    break;
6624             case EF_FRV_CPU_SIMPLE:  strcat (old_opt, " -mcpu=simple"); break;
6625             case EF_FRV_CPU_FR550:   strcat (old_opt, " -mcpu=fr550");  break;
6626             case EF_FRV_CPU_FR500:   strcat (old_opt, " -mcpu=fr500");  break;
6627             case EF_FRV_CPU_FR450:   strcat (old_opt, " -mcpu=fr450");  break;
6628             case EF_FRV_CPU_FR405:   strcat (old_opt, " -mcpu=fr405");  break;
6629             case EF_FRV_CPU_FR400:   strcat (old_opt, " -mcpu=fr400");  break;
6630             case EF_FRV_CPU_FR300:   strcat (old_opt, " -mcpu=fr300");  break;
6631             case EF_FRV_CPU_TOMCAT:  strcat (old_opt, " -mcpu=tomcat"); break;
6632             }
6633         }
6634
6635       /* Print out any mismatches from above.  */
6636       if (new_opt[0])
6637         {
6638           error = TRUE;
6639           (*_bfd_error_handler)
6640             (_("%s: compiled with %s and linked with modules compiled with %s"),
6641              bfd_get_filename (ibfd), new_opt, old_opt);
6642         }
6643
6644       /* Warn about any other mismatches */
6645       new_partial = (new_flags & ~ EF_FRV_ALL_FLAGS);
6646       old_partial = (old_flags & ~ EF_FRV_ALL_FLAGS);
6647       if (new_partial != old_partial)
6648         {
6649           old_flags |= new_partial;
6650           error = TRUE;
6651           (*_bfd_error_handler)
6652             (_("%s: uses different unknown e_flags (0x%lx) fields than previous modules (0x%lx)"),
6653              bfd_get_filename (ibfd), (long)new_partial, (long)old_partial);
6654         }
6655     }
6656
6657   /* If the cpu is -mcpu=simple, then set the -mnopack bit.  */
6658   if ((old_flags & EF_FRV_CPU_MASK) == EF_FRV_CPU_SIMPLE)
6659     old_flags |= EF_FRV_NOPACK;
6660
6661   /* Update the old flags now with changes made above.  */
6662   old_partial = elf_elfheader (obfd)->e_flags & EF_FRV_CPU_MASK;
6663   elf_elfheader (obfd)->e_flags = old_flags;
6664   if (old_partial != (old_flags & EF_FRV_CPU_MASK))
6665     bfd_default_set_arch_mach (obfd, bfd_arch_frv, elf32_frv_machine (obfd));
6666
6667   if (((new_flags & EF_FRV_FDPIC) == 0)
6668       != (! IS_FDPIC (ibfd)))
6669     {
6670       error = TRUE;
6671       if (IS_FDPIC (obfd))
6672         (*_bfd_error_handler)
6673           (_("%s: cannot link non-fdpic object file into fdpic executable"),
6674            bfd_get_filename (ibfd));
6675       else
6676         (*_bfd_error_handler)
6677           (_("%s: cannot link fdpic object file into non-fdpic executable"),
6678            bfd_get_filename (ibfd));
6679     }
6680
6681   if (error)
6682     bfd_set_error (bfd_error_bad_value);
6683
6684   return !error;
6685 }
6686
6687 \f
6688 bfd_boolean
6689 frv_elf_print_private_bfd_data (abfd, ptr)
6690      bfd *abfd;
6691      PTR ptr;
6692 {
6693   FILE *file = (FILE *) ptr;
6694   flagword flags;
6695
6696   BFD_ASSERT (abfd != NULL && ptr != NULL);
6697
6698   /* Print normal ELF private data.  */
6699   _bfd_elf_print_private_bfd_data (abfd, ptr);
6700
6701   flags = elf_elfheader (abfd)->e_flags;
6702   fprintf (file, _("private flags = 0x%lx:"), (long)flags);
6703
6704   switch (flags & EF_FRV_CPU_MASK)
6705     {
6706     default:                                                    break;
6707     case EF_FRV_CPU_SIMPLE: fprintf (file, " -mcpu=simple");    break;
6708     case EF_FRV_CPU_FR550:  fprintf (file, " -mcpu=fr550");     break;
6709     case EF_FRV_CPU_FR500:  fprintf (file, " -mcpu=fr500");     break;
6710     case EF_FRV_CPU_FR450:  fprintf (file, " -mcpu=fr450");     break;
6711     case EF_FRV_CPU_FR405:  fprintf (file, " -mcpu=fr405");     break;
6712     case EF_FRV_CPU_FR400:  fprintf (file, " -mcpu=fr400");     break;
6713     case EF_FRV_CPU_FR300:  fprintf (file, " -mcpu=fr300");     break;
6714     case EF_FRV_CPU_TOMCAT: fprintf (file, " -mcpu=tomcat");    break;
6715     }
6716
6717   switch (flags & EF_FRV_GPR_MASK)
6718     {
6719     default:                                                    break;
6720     case EF_FRV_GPR_32: fprintf (file, " -mgpr-32");            break;
6721     case EF_FRV_GPR_64: fprintf (file, " -mgpr-64");            break;
6722     }
6723
6724   switch (flags & EF_FRV_FPR_MASK)
6725     {
6726     default:                                                    break;
6727     case EF_FRV_FPR_32:   fprintf (file, " -mfpr-32");          break;
6728     case EF_FRV_FPR_64:   fprintf (file, " -mfpr-64");          break;
6729     case EF_FRV_FPR_NONE: fprintf (file, " -msoft-float");      break;
6730     }
6731
6732   switch (flags & EF_FRV_DWORD_MASK)
6733     {
6734     default:                                                    break;
6735     case EF_FRV_DWORD_YES: fprintf (file, " -mdword");          break;
6736     case EF_FRV_DWORD_NO:  fprintf (file, " -mno-dword");       break;
6737     }
6738
6739   if (flags & EF_FRV_DOUBLE)
6740     fprintf (file, " -mdouble");
6741
6742   if (flags & EF_FRV_MEDIA)
6743     fprintf (file, " -mmedia");
6744
6745   if (flags & EF_FRV_MULADD)
6746     fprintf (file, " -mmuladd");
6747
6748   if (flags & EF_FRV_PIC)
6749     fprintf (file, " -fpic");
6750
6751   if (flags & EF_FRV_BIGPIC)
6752     fprintf (file, " -fPIC");
6753
6754   if (flags & EF_FRV_LIBPIC)
6755     fprintf (file, " -mlibrary-pic");
6756
6757   if (flags & EF_FRV_FDPIC)
6758     fprintf (file, " -mfdpic");
6759
6760   if (flags & EF_FRV_NON_PIC_RELOCS)
6761     fprintf (file, " non-pic relocations");
6762
6763   if (flags & EF_FRV_G0)
6764     fprintf (file, " -G0");
6765
6766   fputc ('\n', file);
6767   return TRUE;
6768 }
6769
6770 \f
6771 /* Support for core dump NOTE sections.  */
6772
6773 static bfd_boolean
6774 elf32_frv_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
6775 {
6776   int offset;
6777   unsigned int raw_size;
6778
6779   switch (note->descsz)
6780     {
6781       default:
6782         return FALSE;
6783
6784       /* The Linux/FRV elf_prstatus struct is 268 bytes long.  The other
6785          hardcoded offsets and sizes listed below (and contained within
6786          this lexical block) refer to fields in the target's elf_prstatus
6787          struct.  */
6788       case 268: 
6789         /* `pr_cursig' is at offset 12.  */
6790         elf_tdata (abfd)->core_signal = bfd_get_16 (abfd, note->descdata + 12);
6791
6792         /* `pr_pid' is at offset 24.  */
6793         elf_tdata (abfd)->core_pid = bfd_get_32 (abfd, note->descdata + 24);
6794
6795         /* `pr_reg' is at offset 72.  */
6796         offset = 72;
6797
6798         /* Most grok_prstatus implementations set `raw_size' to the size
6799            of the pr_reg field.  For Linux/FRV, we set `raw_size' to be
6800            the size of `pr_reg' plus the size of `pr_exec_fdpic_loadmap'
6801            and `pr_interp_fdpic_loadmap', both of which (by design)
6802            immediately follow `pr_reg'.  This will allow these fields to
6803            be viewed by GDB as registers.
6804            
6805            `pr_reg' is 184 bytes long.  `pr_exec_fdpic_loadmap' and
6806            `pr_interp_fdpic_loadmap' are 4 bytes each.  */
6807         raw_size = 184 + 4 + 4;
6808
6809         break;
6810     }
6811
6812   /* Make a ".reg/999" section.  */
6813   return _bfd_elfcore_make_pseudosection (abfd, ".reg", raw_size,
6814                                           note->descpos + offset);
6815 }
6816
6817 static bfd_boolean
6818 elf32_frv_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
6819 {
6820   switch (note->descsz)
6821     {
6822       default:
6823         return FALSE;
6824
6825       /* The Linux/FRV elf_prpsinfo struct is 124 bytes long.  */
6826       case 124:
6827
6828         /* `pr_fname' is found at offset 28 and is 16 bytes long.  */
6829         elf_tdata (abfd)->core_program
6830           = _bfd_elfcore_strndup (abfd, note->descdata + 28, 16);
6831
6832         /* `pr_psargs' is found at offset 44 and is 80 bytes long.  */
6833         elf_tdata (abfd)->core_command
6834           = _bfd_elfcore_strndup (abfd, note->descdata + 44, 80);
6835     }
6836
6837   /* Note that for some reason, a spurious space is tacked
6838      onto the end of the args in some (at least one anyway)
6839      implementations, so strip it off if it exists.  */
6840
6841   {
6842     char *command = elf_tdata (abfd)->core_command;
6843     int n = strlen (command);
6844
6845     if (0 < n && command[n - 1] == ' ')
6846       command[n - 1] = '\0';
6847   }
6848
6849   return TRUE;
6850 }
6851 #define ELF_ARCH                bfd_arch_frv
6852 #define ELF_MACHINE_CODE        EM_CYGNUS_FRV
6853 #define ELF_MAXPAGESIZE         0x1000
6854
6855 #define TARGET_BIG_SYM          bfd_elf32_frv_vec
6856 #define TARGET_BIG_NAME         "elf32-frv"
6857
6858 #define elf_info_to_howto                       frv_info_to_howto_rela
6859 #define elf_backend_relocate_section            elf32_frv_relocate_section
6860 #define elf_backend_gc_mark_hook                elf32_frv_gc_mark_hook
6861 #define elf_backend_check_relocs                elf32_frv_check_relocs
6862 #define elf_backend_object_p                    elf32_frv_object_p
6863 #define elf_backend_add_symbol_hook             elf32_frv_add_symbol_hook
6864
6865 #define elf_backend_can_gc_sections             1
6866 #define elf_backend_rela_normal                 1
6867
6868 #define bfd_elf32_bfd_reloc_type_lookup         frv_reloc_type_lookup
6869 #define bfd_elf32_bfd_set_private_flags         frv_elf_set_private_flags
6870 #define bfd_elf32_bfd_copy_private_bfd_data     frv_elf_copy_private_bfd_data
6871 #define bfd_elf32_bfd_merge_private_bfd_data    frv_elf_merge_private_bfd_data
6872 #define bfd_elf32_bfd_print_private_bfd_data    frv_elf_print_private_bfd_data
6873
6874 #define elf_backend_want_got_sym        1
6875 #define elf_backend_got_header_size     0
6876 #define elf_backend_want_got_plt        0
6877 #define elf_backend_plt_readonly        1
6878 #define elf_backend_want_plt_sym        0
6879 #define elf_backend_plt_header_size     0
6880
6881 #define elf_backend_finish_dynamic_sections \
6882                 elf32_frv_finish_dynamic_sections
6883
6884 #define elf_backend_grok_prstatus       elf32_frv_grok_prstatus
6885 #define elf_backend_grok_psinfo         elf32_frv_grok_psinfo
6886
6887 #include "elf32-target.h"
6888
6889 #undef ELF_MAXPAGESIZE
6890 #define ELF_MAXPAGESIZE         0x4000
6891
6892 #undef TARGET_BIG_SYM
6893 #define TARGET_BIG_SYM          bfd_elf32_frvfdpic_vec
6894 #undef TARGET_BIG_NAME
6895 #define TARGET_BIG_NAME         "elf32-frvfdpic"
6896 #undef  elf32_bed
6897 #define elf32_bed               elf32_frvfdpic_bed
6898
6899 #undef elf_info_to_howto_rel
6900 #define elf_info_to_howto_rel   frvfdpic_info_to_howto_rel
6901
6902 #undef bfd_elf32_bfd_link_hash_table_create
6903 #define bfd_elf32_bfd_link_hash_table_create \
6904                 frvfdpic_elf_link_hash_table_create
6905 #undef elf_backend_always_size_sections
6906 #define elf_backend_always_size_sections \
6907                 elf32_frvfdpic_always_size_sections
6908 #undef elf_backend_modify_program_headers
6909 #define elf_backend_modify_program_headers \
6910                 elf32_frvfdpic_modify_program_headers
6911 #undef bfd_elf32_bfd_copy_private_bfd_data
6912 #define bfd_elf32_bfd_copy_private_bfd_data \
6913                 elf32_frvfdpic_copy_private_bfd_data
6914
6915 #undef elf_backend_create_dynamic_sections
6916 #define elf_backend_create_dynamic_sections \
6917                 elf32_frvfdpic_create_dynamic_sections
6918 #undef elf_backend_adjust_dynamic_symbol
6919 #define elf_backend_adjust_dynamic_symbol \
6920                 elf32_frvfdpic_adjust_dynamic_symbol
6921 #undef elf_backend_size_dynamic_sections
6922 #define elf_backend_size_dynamic_sections \
6923                 elf32_frvfdpic_size_dynamic_sections
6924 #undef bfd_elf32_bfd_relax_section
6925 #define bfd_elf32_bfd_relax_section \
6926   elf32_frvfdpic_relax_section
6927 #undef elf_backend_finish_dynamic_symbol
6928 #define elf_backend_finish_dynamic_symbol \
6929                 elf32_frvfdpic_finish_dynamic_symbol
6930 #undef elf_backend_finish_dynamic_sections
6931 #define elf_backend_finish_dynamic_sections \
6932                 elf32_frvfdpic_finish_dynamic_sections
6933
6934 #undef elf_backend_can_make_relative_eh_frame
6935 #define elf_backend_can_make_relative_eh_frame \
6936                 frvfdpic_elf_use_relative_eh_frame
6937 #undef elf_backend_can_make_lsda_relative_eh_frame
6938 #define elf_backend_can_make_lsda_relative_eh_frame \
6939                 frvfdpic_elf_use_relative_eh_frame
6940 #undef elf_backend_encode_eh_address
6941 #define elf_backend_encode_eh_address \
6942                 frvfdpic_elf_encode_eh_address
6943
6944 #undef elf_backend_may_use_rel_p
6945 #define elf_backend_may_use_rel_p       1
6946 #undef elf_backend_may_use_rela_p
6947 #define elf_backend_may_use_rela_p      1
6948 /* We use REL for dynamic relocations only.  */
6949 #undef elf_backend_default_use_rela_p
6950 #define elf_backend_default_use_rela_p  1
6951
6952 #undef elf_backend_omit_section_dynsym
6953 #define elf_backend_omit_section_dynsym _frvfdpic_link_omit_section_dynsym
6954
6955 #include "elf32-target.h"