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