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