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