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