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