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