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