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