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