2004-08-05 Michael Chastain <mec.gnu@mindspring.com>
[deliverable/binutils-gdb.git] / bfd / elf32-frv.c
CommitLineData
4e5ba5b7 1/* FRV-specific support for 32-bit ELF.
5a580b3a 2 Copyright 2002, 2003, 2004 Free Software Foundation, Inc.
4e5ba5b7
DB
3
4This file is part of BFD, the Binary File Descriptor library.
5
6This program is free software; you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
8the Free Software Foundation; either version 2 of the License, or
9(at your option) any later version.
10
11This program is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with this program; if not, write to the Free Software
18Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
19
20#include "bfd.h"
21#include "sysdep.h"
22#include "libbfd.h"
23#include "elf-bfd.h"
24#include "elf/frv.h"
ec3391e7 25#include "elf/dwarf2.h"
51532845 26#include "hashtab.h"
4e5ba5b7
DB
27
28/* Forward declarations. */
29static bfd_reloc_status_type elf32_frv_relocate_lo16
30 PARAMS ((bfd *, Elf_Internal_Rela *, bfd_byte *, bfd_vma));
31static bfd_reloc_status_type elf32_frv_relocate_hi16
32 PARAMS ((bfd *, Elf_Internal_Rela *, bfd_byte *, bfd_vma));
33static bfd_reloc_status_type elf32_frv_relocate_label24
34 PARAMS ((bfd *, asection *, Elf_Internal_Rela *, bfd_byte *, bfd_vma));
35static bfd_reloc_status_type elf32_frv_relocate_gprel12
b34976b6
AM
36 PARAMS ((struct bfd_link_info *, bfd *, asection *, Elf_Internal_Rela *,
37 bfd_byte *, bfd_vma));
4e5ba5b7 38static bfd_reloc_status_type elf32_frv_relocate_gprelu12
b34976b6
AM
39 PARAMS ((struct bfd_link_info *, bfd *, asection *, Elf_Internal_Rela *,
40 bfd_byte *, bfd_vma));
4e5ba5b7 41static bfd_reloc_status_type elf32_frv_relocate_gprello
b34976b6
AM
42 PARAMS ((struct bfd_link_info *, bfd *, asection *, Elf_Internal_Rela *,
43 bfd_byte *, bfd_vma));
4e5ba5b7 44static bfd_reloc_status_type elf32_frv_relocate_gprelhi
b34976b6
AM
45 PARAMS ((struct bfd_link_info *, bfd *, asection *, Elf_Internal_Rela *,
46 bfd_byte *, bfd_vma));
4e5ba5b7
DB
47static reloc_howto_type *frv_reloc_type_lookup
48 PARAMS ((bfd *, bfd_reloc_code_real_type));
b34976b6 49static void frv_info_to_howto_rela
947216bf 50 PARAMS ((bfd *, arelent *, Elf_Internal_Rela *));
b34976b6
AM
51static bfd_boolean elf32_frv_relocate_section
52 PARAMS ((bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
53 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **));
54static bfd_boolean elf32_frv_add_symbol_hook
555cd476 55 PARAMS (( bfd *, struct bfd_link_info *, Elf_Internal_Sym *,
b34976b6 56 const char **, flagword *, asection **, bfd_vma *));
4e5ba5b7 57static bfd_reloc_status_type frv_final_link_relocate
b34976b6
AM
58 PARAMS ((reloc_howto_type *, bfd *, asection *, bfd_byte *,
59 Elf_Internal_Rela *, bfd_vma));
60static bfd_boolean elf32_frv_gc_sweep_hook
61 PARAMS ((bfd *, struct bfd_link_info *, asection *, const
62 Elf_Internal_Rela *));
4e5ba5b7 63static asection * elf32_frv_gc_mark_hook
b34976b6
AM
64 PARAMS ((asection *, struct bfd_link_info *, Elf_Internal_Rela *,
65 struct elf_link_hash_entry *, Elf_Internal_Sym *));
66static bfd_boolean elf32_frv_check_relocs
67 PARAMS ((bfd *, struct bfd_link_info *, asection *,
68 const Elf_Internal_Rela *));
69static int elf32_frv_machine
70 PARAMS ((bfd *));
71static bfd_boolean elf32_frv_object_p
72 PARAMS ((bfd *));
73static bfd_boolean frv_elf_set_private_flags
74 PARAMS ((bfd *, flagword));
75static bfd_boolean frv_elf_copy_private_bfd_data
76 PARAMS ((bfd *, bfd *));
77static bfd_boolean frv_elf_merge_private_bfd_data
78 PARAMS ((bfd *, bfd *));
79static bfd_boolean frv_elf_print_private_bfd_data
80 PARAMS ((bfd *, PTR));
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. */
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 */
b34976b6 127 TRUE), /* pcrel_offset */
4e5ba5b7
DB
128
129 /* A 24-bit pc-relative relocation. */
130 HOWTO (R_FRV_LABEL24, /* type */
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 */
b34976b6 142 TRUE), /* pcrel_offset */
4e5ba5b7
DB
143
144 HOWTO (R_FRV_LO16, /* type */
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 */
b34976b6 156 FALSE), /* pcrel_offset */
4e5ba5b7
DB
157
158 HOWTO (R_FRV_HI16, /* type */
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 */
b34976b6 170 FALSE), /* pcrel_offset */
4e5ba5b7
DB
171
172 HOWTO (R_FRV_GPREL12, /* type */
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 */
180 "R_FRV_GPREL12", /* name */
b34976b6 181 FALSE, /* partial_inplace */
4e5ba5b7
DB
182 0xfff, /* src_mask */
183 0xfff, /* dst_mask */
b34976b6 184 FALSE), /* pcrel_offset */
4e5ba5b7
DB
185
186 HOWTO (R_FRV_GPRELU12, /* type */
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 */
194 "R_FRV_GPRELU12", /* name */
b34976b6 195 FALSE, /* partial_inplace */
4e5ba5b7
DB
196 0xfff, /* src_mask */
197 0x3f03f, /* dst_mask */
b34976b6 198 FALSE), /* pcrel_offset */
4e5ba5b7
DB
199
200 HOWTO (R_FRV_GPREL32, /* type */
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 */
4e5ba5b7
DB
210 0xffffffff, /* src_mask */
211 0xffffffff, /* dst_mask */
b34976b6 212 FALSE), /* pcrel_offset */
4e5ba5b7
DB
213
214 HOWTO (R_FRV_GPRELHI, /* type */
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 */
4e5ba5b7
DB
224 0xffff, /* src_mask */
225 0xffff, /* dst_mask */
b34976b6 226 FALSE), /* pcrel_offset */
4e5ba5b7
DB
227
228 HOWTO (R_FRV_GPRELLO, /* type */
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 */
4e5ba5b7
DB
238 0xffff, /* src_mask */
239 0xffff, /* dst_mask */
b34976b6 240 FALSE), /* pcrel_offset */
51532845
AO
241
242 /* A 12-bit signed operand with the GOT offset for the address of
243 the symbol. */
244 HOWTO (R_FRV_GOT12, /* type */
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 */
254 0xfff, /* src_mask */
255 0xfff, /* dst_mask */
256 FALSE), /* pcrel_offset */
257
258 /* The upper 16 bits of the GOT offset for the address of the
259 symbol. */
260 HOWTO (R_FRV_GOTHI, /* type */
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 */
270 0xffff, /* src_mask */
271 0xffff, /* dst_mask */
272 FALSE), /* pcrel_offset */
273
274 /* The lower 16 bits of the GOT offset for the address of the
275 symbol. */
276 HOWTO (R_FRV_GOTLO, /* type */
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 */
288 FALSE), /* pcrel_offset */
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 */
317 0xfff, /* src_mask */
318 0xfff, /* dst_mask */
319 FALSE), /* pcrel_offset */
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 */
335 FALSE), /* pcrel_offset */
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 */
351 FALSE), /* pcrel_offset */
352
353 /* The 32-bit address of the canonical descriptor of a function. */
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 */
380 0xfff, /* src_mask */
381 0xfff, /* dst_mask */
382 FALSE), /* pcrel_offset */
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 */
398 FALSE), /* pcrel_offset */
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 */
414 FALSE), /* pcrel_offset */
415
416 /* A 12-bit signed operand with the GOT offset for the address of
417 the symbol. */
418 HOWTO (R_FRV_GOTOFF12, /* type */
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 */
428 0xfff, /* src_mask */
429 0xfff, /* dst_mask */
430 FALSE), /* pcrel_offset */
431
432 /* The upper 16 bits of the GOT offset for the address of the
433 symbol. */
434 HOWTO (R_FRV_GOTOFFHI, /* type */
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 */
446 FALSE), /* pcrel_offset */
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 */
462 FALSE), /* pcrel_offset */
463
4e5ba5b7
DB
464};
465
466/* GNU extension to record C++ vtable hierarchy. */
467static reloc_howto_type elf32_frv_vtinherit_howto =
468 HOWTO (R_FRV_GNU_VTINHERIT, /* type */
469 0, /* rightshift */
470 2, /* size (0 = byte, 1 = short, 2 = long) */
471 0, /* bitsize */
b34976b6 472 FALSE, /* pc_relative */
4e5ba5b7
DB
473 0, /* bitpos */
474 complain_overflow_dont, /* complain_on_overflow */
475 NULL, /* special_function */
476 "R_FRV_GNU_VTINHERIT", /* name */
b34976b6 477 FALSE, /* partial_inplace */
4e5ba5b7
DB
478 0, /* src_mask */
479 0, /* dst_mask */
b34976b6 480 FALSE); /* pcrel_offset */
4e5ba5b7
DB
481
482 /* GNU extension to record C++ vtable member usage. */
483static reloc_howto_type elf32_frv_vtentry_howto =
484 HOWTO (R_FRV_GNU_VTENTRY, /* type */
485 0, /* rightshift */
486 2, /* size (0 = byte, 1 = short, 2 = long) */
487 0, /* bitsize */
b34976b6 488 FALSE, /* pc_relative */
4e5ba5b7
DB
489 0, /* bitpos */
490 complain_overflow_dont, /* complain_on_overflow */
491 _bfd_elf_rel_vtable_reloc_fn, /* special_function */
492 "R_FRV_GNU_VTENTRY", /* name */
b34976b6 493 FALSE, /* partial_inplace */
4e5ba5b7
DB
494 0, /* src_mask */
495 0, /* dst_mask */
b34976b6 496 FALSE); /* pcrel_offset */
51532845
AO
497
498/* The following 3 relocations are REL. The only difference to the
499 entries in the table above are that partial_inplace is TRUE. */
500static reloc_howto_type elf32_frv_rel_32_howto =
501 HOWTO (R_FRV_32, /* type */
502 0, /* rightshift */
503 2, /* size (0 = byte, 1 = short, 2 = long) */
504 32, /* bitsize */
505 FALSE, /* pc_relative */
506 0, /* bitpos */
507 complain_overflow_bitfield, /* complain_on_overflow */
508 bfd_elf_generic_reloc, /* special_function */
509 "R_FRV_32", /* name */
510 TRUE, /* partial_inplace */
511 0xffffffff, /* src_mask */
512 0xffffffff, /* dst_mask */
513 FALSE); /* pcrel_offset */
514
515static reloc_howto_type elf32_frv_rel_funcdesc_howto =
516 HOWTO (R_FRV_FUNCDESC, /* type */
517 0, /* rightshift */
518 2, /* size (0 = byte, 1 = short, 2 = long) */
519 32, /* bitsize */
520 FALSE, /* pc_relative */
521 0, /* bitpos */
522 complain_overflow_bitfield, /* complain_on_overflow */
523 bfd_elf_generic_reloc, /* special_function */
524 "R_FRV_FUNCDESC", /* name */
525 TRUE, /* partial_inplace */
526 0xffffffff, /* src_mask */
527 0xffffffff, /* dst_mask */
528 FALSE); /* pcrel_offset */
529
530static reloc_howto_type elf32_frv_rel_funcdesc_value_howto =
531 HOWTO (R_FRV_FUNCDESC_VALUE, /* type */
532 0, /* rightshift */
533 2, /* size (0 = byte, 1 = short, 2 = long) */
534 64, /* bitsize */
535 FALSE, /* pc_relative */
536 0, /* bitpos */
537 complain_overflow_bitfield, /* complain_on_overflow */
538 bfd_elf_generic_reloc, /* special_function */
539 "R_FRV_FUNCDESC_VALUE", /* name */
540 TRUE, /* partial_inplace */
541 0xffffffff, /* src_mask */
542 0xffffffff, /* dst_mask */
543 FALSE); /* pcrel_offset */
544
4e5ba5b7
DB
545\f
546/* Map BFD reloc types to FRV ELF reloc types. */
547#if 0
548struct frv_reloc_map
549{
550 unsigned int bfd_reloc_val;
551 unsigned int frv_reloc_val;
552};
553
554static const struct frv_reloc_map frv_reloc_map [] =
555{
556 { BFD_RELOC_NONE, R_FRV_NONE },
557 { BFD_RELOC_32, R_FRV_32 },
558 { BFD_RELOC_FRV_LABEL16, R_FRV_LABEL16 },
559 { BFD_RELOC_FRV_LABEL24, R_FRV_LABEL24 },
560 { BFD_RELOC_FRV_LO16, R_FRV_LO16 },
561 { BFD_RELOC_FRV_HI16, R_FRV_HI16 },
562 { BFD_RELOC_FRV_GPREL12, R_FRV_GPREL12 },
563 { BFD_RELOC_FRV_GPRELU12, R_FRV_GPRELU12 },
564 { BFD_RELOC_FRV_GPREL32, R_FRV_GPREL32 },
565 { BFD_RELOC_FRV_GPRELHI, R_FRV_GPRELHI },
566 { BFD_RELOC_FRV_GPRELLO, R_FRV_GPRELLO },
51532845
AO
567 { BFD_RELOC_FRV_GOT12, R_FRV_GOT12 },
568 { BFD_RELOC_FRV_GOTHI, R_FRV_GOTHI },
569 { BFD_RELOC_FRV_GOTLO, R_FRV_GOTLO },
570 { BFD_RELOC_FRV_FUNCDESC, R_FRV_FUNCDESC },
571 { BFD_RELOC_FRV_FUNCDESC_GOT12, R_FRV_FUNCDESC_GOT12 },
572 { BFD_RELOC_FRV_FUNCDESC_GOTHI, R_FRV_FUNCDESC_GOTHI },
573 { BFD_RELOC_FRV_FUNCDESC_GOTLO, R_FRV_FUNCDESC_GOTLO },
574 { BFD_RELOC_FRV_FUNCDESC_VALUE, R_FRV_FUNCDESC_VALUE },
575 { BFD_RELOC_FRV_FUNCDESC_GOTOFF12, R_FRV_FUNCDESC_GOTOFF12 },
576 { BFD_RELOC_FRV_FUNCDESC_GOTOFFHI, R_FRV_FUNCDESC_GOTOFFHI },
577 { BFD_RELOC_FRV_FUNCDESC_GOTOFFLO, R_FRV_FUNCDESC_GOTOFFLO },
578 { BFD_RELOC_FRV_GOTOFF12, R_FRV_GOTOFF12 },
579 { BFD_RELOC_FRV_GOTOFFHI, R_FRV_GOTOFFHI },
580 { BFD_RELOC_FRV_GOTOFFLO, R_FRV_GOTOFFLO },
4e5ba5b7
DB
581 { BFD_RELOC_VTABLE_INHERIT, R_FRV_GNU_VTINHERIT },
582 { BFD_RELOC_VTABLE_ENTRY, R_FRV_GNU_VTENTRY },
583};
584#endif
585
43850d5b
AO
586extern const bfd_target bfd_elf32_frvfdpic_vec;
587#define IS_FDPIC(bfd) ((bfd)->xvec == &bfd_elf32_frvfdpic_vec)
4e5ba5b7 588
51532845
AO
589/* An extension of the elf hash table data structure, containing some
590 additional FRV-specific data. */
43850d5b 591struct frvfdpic_elf_link_hash_table
4e5ba5b7 592{
51532845
AO
593 struct elf_link_hash_table elf;
594
595 /* A pointer to the .got section. */
596 asection *sgot;
597 /* A pointer to the .rel.got section. */
598 asection *sgotrel;
599 /* A pointer to the .rofixup section. */
600 asection *sgotfixup;
601 /* A pointer to the .plt section. */
602 asection *splt;
603 /* A pointer to the .rel.plt section. */
604 asection *spltrel;
605 /* GOT base offset. */
606 bfd_vma got0;
607 /* Location of the first non-lazy PLT entry, i.e., the number of
608 bytes taken by lazy PLT entries. */
609 bfd_vma plt0;
610 /* A hash table holding information about which symbols were
611 referenced with which PIC-related relocations. */
612 struct htab *relocs_info;
613};
4e5ba5b7 614
51532845
AO
615/* Get the FRV ELF linker hash table from a link_info structure. */
616
43850d5b
AO
617#define frvfdpic_hash_table(info) \
618 ((struct frvfdpic_elf_link_hash_table *) ((info)->hash))
619
620#define frvfdpic_got_section(info) \
621 (frvfdpic_hash_table (info)->sgot)
622#define frvfdpic_gotrel_section(info) \
623 (frvfdpic_hash_table (info)->sgotrel)
624#define frvfdpic_gotfixup_section(info) \
625 (frvfdpic_hash_table (info)->sgotfixup)
626#define frvfdpic_plt_section(info) \
627 (frvfdpic_hash_table (info)->splt)
628#define frvfdpic_pltrel_section(info) \
629 (frvfdpic_hash_table (info)->spltrel)
630#define frvfdpic_relocs_info(info) \
631 (frvfdpic_hash_table (info)->relocs_info)
632#define frvfdpic_got_initial_offset(info) \
633 (frvfdpic_hash_table (info)->got0)
634#define frvfdpic_plt_initial_offset(info) \
635 (frvfdpic_hash_table (info)->plt0)
51532845
AO
636
637/* Create an FRV ELF linker hash table. */
638
639static struct bfd_link_hash_table *
43850d5b 640frvfdpic_elf_link_hash_table_create (bfd *abfd)
51532845 641{
43850d5b
AO
642 struct frvfdpic_elf_link_hash_table *ret;
643 bfd_size_type amt = sizeof (struct frvfdpic_elf_link_hash_table);
4e5ba5b7 644
51532845
AO
645 ret = bfd_zalloc (abfd, amt);
646 if (ret == NULL)
647 return NULL;
4e5ba5b7 648
51532845
AO
649 if (! _bfd_elf_link_hash_table_init (&ret->elf, abfd,
650 _bfd_elf_link_hash_newfunc))
651 {
652 free (ret);
653 return NULL;
654 }
4e5ba5b7 655
51532845
AO
656 return &ret->elf.root;
657}
4e5ba5b7 658
51532845
AO
659/* Decide whether a reference to a symbol can be resolved locally or
660 not. If the symbol is protected, we want the local address, but
661 its function descriptor must be assigned by the dynamic linker. */
43850d5b 662#define FRVFDPIC_SYM_LOCAL(INFO, H) \
51532845 663 (_bfd_elf_symbol_refs_local_p ((H), (INFO), 1) \
7e2294f9 664 || ! elf_hash_table (INFO)->dynamic_sections_created)
43850d5b 665#define FRVFDPIC_FUNCDESC_LOCAL(INFO, H) \
51532845
AO
666 ((H)->dynindx == -1 || ! elf_hash_table (INFO)->dynamic_sections_created)
667
668/* This structure collects information on what kind of GOT, PLT or
669 function descriptors are required by relocations that reference a
670 certain symbol. */
43850d5b 671struct frvfdpic_relocs_info
51532845
AO
672{
673 /* The index of the symbol, as stored in the relocation r_info, if
674 we have a local symbol; -1 otherwise. */
675 long symndx;
676 union
677 {
678 /* The input bfd in which the symbol is defined, if it's a local
679 symbol. */
680 bfd *abfd;
681 /* If symndx == -1, the hash table entry corresponding to a global
682 symbol (even if it turns out to bind locally, in which case it
683 should ideally be replaced with section's symndx + addend). */
684 struct elf_link_hash_entry *h;
685 } d;
686 /* The addend of the relocation that references the symbol. */
687 bfd_vma addend;
688
689 /* The fields above are used to identify an entry. The fields below
690 contain information on how an entry is used and, later on, which
691 locations it was assigned. */
692 /* The following 3 fields record whether the symbol+addend above was
693 ever referenced with a GOT relocation. The 12 suffix indicates a
694 GOT12 relocation; los is used for GOTLO relocations that are not
695 matched by a GOTHI relocation; hilo is used for GOTLO/GOTHI
696 pairs. */
697 unsigned got12:1;
698 unsigned gotlos:1;
699 unsigned gothilo:1;
700 /* Whether a FUNCDESC relocation references symbol+addend. */
701 unsigned fd:1;
702 /* Whether a FUNCDESC_GOT relocation references symbol+addend. */
703 unsigned fdgot12:1;
704 unsigned fdgotlos:1;
705 unsigned fdgothilo:1;
706 /* Whether a FUNCDESC_GOTOFF relocation references symbol+addend. */
707 unsigned fdgoff12:1;
708 unsigned fdgofflos:1;
709 unsigned fdgoffhilo:1;
710 /* Whether symbol+addend is referenced with GOTOFF12, GOTOFFLO or
711 GOTOFFHI relocations. The addend doesn't really matter, since we
712 envision that this will only be used to check whether the symbol
713 is mapped to the same segment as the got. */
714 unsigned gotoff:1;
715 /* Whether symbol+addend is referenced by a LABEL24 relocation. */
716 unsigned call:1;
717 /* Whether symbol+addend is referenced by a 32 or FUNCDESC_VALUE
718 relocation. */
719 unsigned sym:1;
720 /* Whether we need a PLT entry for a symbol. Should be implied by
721 something like:
43850d5b 722 (call && symndx == -1 && ! FRVFDPIC_SYM_LOCAL (info, d.h)) */
51532845
AO
723 unsigned plt:1;
724 /* Whether a function descriptor should be created in this link unit
725 for symbol+addend. Should be implied by something like:
726 (plt || fdgotoff12 || fdgotofflos || fdgotofflohi
727 || ((fd || fdgot12 || fdgotlos || fdgothilo)
43850d5b 728 && (symndx != -1 || FRVFDPIC_FUNCDESC_LOCAL (info, d.h)))) */
51532845
AO
729 unsigned privfd:1;
730 /* Whether a lazy PLT entry is needed for this symbol+addend.
731 Should be implied by something like:
43850d5b 732 (privfd && symndx == -1 && ! FRVFDPIC_SYM_LOCAL (info, d.h)
51532845
AO
733 && ! (info->flags & DF_BIND_NOW)) */
734 unsigned lazyplt:1;
735 /* Whether we've already emitted GOT relocations and PLT entries as
736 needed for this symbol. */
737 unsigned done:1;
738
739 /* The number of R_FRV_32, R_FRV_FUNCDESC and R_FRV_FUNCDESC_VALUE
740 relocations referencing the symbol. */
741 unsigned relocs32, relocsfd, relocsfdv;
742
3b712a1a
AO
743 /* The number of .rofixups entries and dynamic relocations allocated
744 for this symbol, minus any that might have already been used. */
745 unsigned fixups, dynrelocs;
746
51532845
AO
747 /* The offsets of the GOT entries assigned to symbol+addend, to the
748 function descriptor's address, and to a function descriptor,
749 respectively. Should be zero if unassigned. The offsets are
750 counted from the value that will be assigned to the PIC register,
751 not from the beginning of the .got section. */
752 bfd_signed_vma got_entry, fdgot_entry, fd_entry;
753 /* The offsets of the PLT entries assigned to symbol+addend,
754 non-lazy and lazy, respectively. If unassigned, should be
755 (bfd_vma)-1. */
756 bfd_vma plt_entry, lzplt_entry;
757};
4e5ba5b7 758
43850d5b 759/* Compute a hash with the key fields of an frvfdpic_relocs_info entry. */
51532845 760static hashval_t
43850d5b 761frvfdpic_relocs_info_hash (const void *entry_)
51532845 762{
43850d5b 763 const struct frvfdpic_relocs_info *entry = entry_;
4e5ba5b7 764
51532845
AO
765 return (entry->symndx == -1
766 ? entry->d.h->root.root.hash
767 : entry->symndx + entry->d.abfd->id * 257) + entry->addend;
4e5ba5b7
DB
768}
769
43850d5b 770/* Test whether the key fields of two frvfdpic_relocs_info entries are
51532845
AO
771 identical. */
772static int
43850d5b 773frvfdpic_relocs_info_eq (const void *entry1, const void *entry2)
51532845 774{
43850d5b
AO
775 const struct frvfdpic_relocs_info *e1 = entry1;
776 const struct frvfdpic_relocs_info *e2 = entry2;
4e5ba5b7 777
51532845
AO
778 return e1->symndx == e2->symndx && e1->addend == e2->addend
779 && (e1->symndx == -1 ? e1->d.h == e2->d.h : e1->d.abfd == e2->d.abfd);
780}
781
782/* Find or create an entry in a hash table HT that matches the key
783 fields of the given ENTRY. If it's not found, memory for a new
784 entry is allocated in ABFD's obstack. */
43850d5b
AO
785static struct frvfdpic_relocs_info *
786frvfdpic_relocs_info_find (struct htab *ht,
787 bfd *abfd,
788 const struct frvfdpic_relocs_info *entry,
789 enum insert_option insert)
4e5ba5b7 790{
43850d5b
AO
791 struct frvfdpic_relocs_info **loc =
792 (struct frvfdpic_relocs_info **) htab_find_slot (ht, entry, insert);
3b712a1a
AO
793
794 if (! loc)
795 return NULL;
4e5ba5b7 796
51532845
AO
797 if (*loc)
798 return *loc;
4e5ba5b7 799
51532845 800 *loc = bfd_zalloc (abfd, sizeof (**loc));
b34976b6 801
51532845
AO
802 if (! *loc)
803 return *loc;
804
805 (*loc)->symndx = entry->symndx;
806 (*loc)->d = entry->d;
807 (*loc)->addend = entry->addend;
808 (*loc)->plt_entry = (bfd_vma)-1;
809 (*loc)->lzplt_entry = (bfd_vma)-1;
810
811 return *loc;
812}
813
814/* Obtain the address of the entry in HT associated with H's symbol +
815 addend, creating a new entry if none existed. ABFD is only used
816 for memory allocation purposes. */
43850d5b
AO
817inline static struct frvfdpic_relocs_info *
818frvfdpic_relocs_info_for_global (struct htab *ht,
819 bfd *abfd,
820 struct elf_link_hash_entry *h,
821 bfd_vma addend,
822 enum insert_option insert)
51532845 823{
43850d5b 824 struct frvfdpic_relocs_info entry;
51532845
AO
825
826 entry.symndx = -1;
827 entry.d.h = h;
828 entry.addend = addend;
829
43850d5b 830 return frvfdpic_relocs_info_find (ht, abfd, &entry, insert);
51532845
AO
831}
832
833/* Obtain the address of the entry in HT associated with the SYMNDXth
834 local symbol of the input bfd ABFD, plus the addend, creating a new
835 entry if none existed. */
43850d5b
AO
836inline static struct frvfdpic_relocs_info *
837frvfdpic_relocs_info_for_local (struct htab *ht,
838 bfd *abfd,
839 long symndx,
840 bfd_vma addend,
841 enum insert_option insert)
51532845 842{
43850d5b 843 struct frvfdpic_relocs_info entry;
51532845
AO
844
845 entry.symndx = symndx;
846 entry.d.abfd = abfd;
847 entry.addend = addend;
848
43850d5b 849 return frvfdpic_relocs_info_find (ht, abfd, &entry, insert);
3b712a1a
AO
850}
851
852/* Merge fields set by check_relocs() of two entries that end up being
853 mapped to the same (presumably global) symbol. */
854
855inline static void
43850d5b
AO
856frvfdpic_pic_merge_early_relocs_info (struct frvfdpic_relocs_info *e2,
857 struct frvfdpic_relocs_info const *e1)
3b712a1a
AO
858{
859 e2->got12 |= e1->got12;
860 e2->gotlos |= e1->gotlos;
861 e2->gothilo |= e1->gothilo;
862 e2->fd |= e1->fd;
863 e2->fdgot12 |= e1->fdgot12;
864 e2->fdgotlos |= e1->fdgotlos;
865 e2->fdgothilo |= e1->fdgothilo;
866 e2->fdgoff12 |= e1->fdgoff12;
867 e2->fdgofflos |= e1->fdgofflos;
868 e2->fdgoffhilo |= e1->fdgoffhilo;
869 e2->gotoff |= e1->gotoff;
870 e2->call |= e1->call;
871 e2->sym |= e1->sym;
872
873#if 0
43850d5b
AO
874 /* These are set in _frvfdpic_count_got_plt_entries() or later, and this
875 function is only called in _frvfdpic_resolve_final_relocs_info(), that
3b712a1a
AO
876 runs just before it, so we don't have to worry about the fields
877 below. */
878
879 e2->plt |= e1->plt;
880 e2->privfd |= e1->privfd;
881 e2->lazyplt |= e1->lazyplt;
882 e2->done |= e1->done;
883
884 e2->relocs32 += e1->relocs32;
885 e2->relocsfd += e1->relocsfd;
886 e2->relocsfdv += e1->relocsfdv;
887 e2->fixups += e1->fixups;
888 e2->dynrelocs += e1->dynrelocs;
889
890 if (abs (e1->got_entry) < abs (e2->got_entry))
891 e2->got_entry = e1->got_entry;
892 if (abs (e1->fdgot_entry) < abs (e2->fdgot_entry))
893 e2->fdgot_entry = e1->fdgot_entry;
894 if (abs (e1->fd_entry) < abs (e2->fd_entry))
895 e2->fd_entry = e1->fd_entry;
896
897 if (e1->plt_entry < e2->plt_entry)
898 e2->plt_entry = e1->plt_entry;
899 if (e1->lzplt_entry < e2->lzplt_entry)
900 e2->lzplt_entry = e1->lzplt_entry;
901#endif
51532845
AO
902}
903
904/* Every block of 65535 lazy PLT entries shares a single call to the
905 resolver, inserted in the 32768th lazy PLT entry (i.e., entry #
906 32767, counting from 0). All other lazy PLT entries branch to it
907 in a single instruction. */
908
43850d5b
AO
909#define FRVFDPIC_LZPLT_BLOCK_SIZE ((bfd_vma) 8 * 65535 + 4)
910#define FRVFDPIC_LZPLT_RESOLV_LOC (8 * 32767)
51532845
AO
911
912/* Add a dynamic relocation to the SRELOC section. */
913
914inline static bfd_vma
43850d5b
AO
915_frvfdpic_add_dyn_reloc (bfd *output_bfd, asection *sreloc, bfd_vma offset,
916 int reloc_type, long dynindx, bfd_vma addend,
917 struct frvfdpic_relocs_info *entry)
51532845
AO
918{
919 Elf_Internal_Rela outrel;
920 bfd_vma reloc_offset;
921
922 outrel.r_offset = offset;
923 outrel.r_info = ELF32_R_INFO (dynindx, reloc_type);
924 outrel.r_addend = addend;
925
926 reloc_offset = sreloc->reloc_count * sizeof (Elf32_External_Rel);
eea6121a 927 BFD_ASSERT (reloc_offset < sreloc->size);
51532845
AO
928 bfd_elf32_swap_reloc_out (output_bfd, &outrel,
929 sreloc->contents + reloc_offset);
930 sreloc->reloc_count++;
931
3f980e41
AO
932 /* If the entry's index is zero, this relocation was probably to a
933 linkonce section that got discarded. We reserved a dynamic
934 relocation, but it was for another entry than the one we got at
935 the time of emitting the relocation. Unfortunately there's no
936 simple way for us to catch this situation, since the relocation
937 is cleared right before calling relocate_section, at which point
938 we no longer know what the relocation used to point to. */
939 if (entry->symndx)
940 {
941 BFD_ASSERT (entry->dynrelocs > 0);
942 entry->dynrelocs--;
943 }
3b712a1a 944
51532845
AO
945 return reloc_offset;
946}
947
948/* Add a fixup to the ROFIXUP section. */
949
950static bfd_vma
43850d5b
AO
951_frvfdpic_add_rofixup (bfd *output_bfd, asection *rofixup, bfd_vma offset,
952 struct frvfdpic_relocs_info *entry)
51532845
AO
953{
954 bfd_vma fixup_offset;
955
956 if (rofixup->flags & SEC_EXCLUDE)
957 return -1;
958
959 fixup_offset = rofixup->reloc_count * 4;
960 if (rofixup->contents)
961 {
eea6121a 962 BFD_ASSERT (fixup_offset < rofixup->size);
51532845
AO
963 bfd_put_32 (output_bfd, offset, rofixup->contents + fixup_offset);
964 }
965 rofixup->reloc_count++;
3b712a1a 966
3f980e41 967 if (entry && entry->symndx)
3b712a1a 968 {
3f980e41
AO
969 /* See discussion about symndx == 0 in _frvfdpic_add_dyn_reloc
970 above. */
3b712a1a
AO
971 BFD_ASSERT (entry->fixups > 0);
972 entry->fixups--;
973 }
974
51532845
AO
975 return fixup_offset;
976}
977
978/* Find the segment number in which OSEC, and output section, is
979 located. */
980
981static unsigned
43850d5b 982_frvfdpic_osec_to_segment (bfd *output_bfd, asection *osec)
51532845
AO
983{
984 struct elf_segment_map *m;
985 Elf_Internal_Phdr *p;
986
987 /* Find the segment that contains the output_section. */
988 for (m = elf_tdata (output_bfd)->segment_map,
989 p = elf_tdata (output_bfd)->phdr;
990 m != NULL;
991 m = m->next, p++)
992 {
993 int i;
994
995 for (i = m->count - 1; i >= 0; i--)
996 if (m->sections[i] == osec)
997 break;
998
999 if (i >= 0)
1000 break;
1001 }
1002
1003 return p - elf_tdata (output_bfd)->phdr;
1004}
1005
1006inline static bfd_boolean
43850d5b 1007_frvfdpic_osec_readonly_p (bfd *output_bfd, asection *osec)
51532845 1008{
43850d5b 1009 unsigned seg = _frvfdpic_osec_to_segment (output_bfd, osec);
51532845
AO
1010
1011 return ! (elf_tdata (output_bfd)->phdr[seg].p_flags & PF_W);
1012}
1013
1014/* Generate relocations for GOT entries, function descriptors, and
1015 code for PLT and lazy PLT entries. */
1016
1017inline static bfd_boolean
43850d5b
AO
1018_frvfdpic_emit_got_relocs_plt_entries (struct frvfdpic_relocs_info *entry,
1019 bfd *output_bfd,
1020 struct bfd_link_info *info,
1021 asection *sec,
1022 Elf_Internal_Sym *sym,
1023 bfd_vma addend)
51532845
AO
1024
1025{
1026 bfd_vma fd_lazy_rel_offset = (bfd_vma)-1;
1027 int dynindx = -1;
1028
1029 if (entry->done)
1030 return TRUE;
1031 entry->done = 1;
1032
1033 if (entry->got_entry || entry->fdgot_entry || entry->fd_entry)
1034 {
1035 /* If the symbol is dynamic, consider it for dynamic
1036 relocations, otherwise decay to section + offset. */
1037 if (entry->symndx == -1 && entry->d.h->dynindx != -1)
1038 dynindx = entry->d.h->dynindx;
1039 else
1040 {
1041 if (sec->output_section
1042 && ! bfd_is_abs_section (sec->output_section)
1043 && ! bfd_is_und_section (sec->output_section))
1044 dynindx = elf_section_data (sec->output_section)->dynindx;
1045 else
1046 dynindx = 0;
1047 }
1048 }
1049
1050 /* Generate relocation for GOT entry pointing to the symbol. */
1051 if (entry->got_entry)
1052 {
1053 int idx = dynindx;
1054 bfd_vma ad = addend;
1055
1056 /* If the symbol is dynamic but binds locally, use
1057 section+offset. */
43850d5b
AO
1058 if (sec && (entry->symndx != -1
1059 || FRVFDPIC_SYM_LOCAL (info, entry->d.h)))
51532845
AO
1060 {
1061 if (entry->symndx == -1)
1062 ad += entry->d.h->root.u.def.value;
1063 else
1064 ad += sym->st_value;
1065 ad += sec->output_offset;
1066 if (sec->output_section && elf_section_data (sec->output_section))
1067 idx = elf_section_data (sec->output_section)->dynindx;
1068 else
1069 idx = 0;
1070 }
1071
1072 /* If we're linking an executable at a fixed address, we can
1073 omit the dynamic relocation as long as the symbol is local to
1074 this module. */
1075 if (info->executable && !info->pie
43850d5b
AO
1076 && (entry->symndx != -1
1077 || FRVFDPIC_SYM_LOCAL (info, entry->d.h)))
51532845
AO
1078 {
1079 if (sec)
1080 ad += sec->output_section->vma;
3b712a1a
AO
1081 if (entry->symndx != -1
1082 || entry->d.h->root.type != bfd_link_hash_undefweak)
43850d5b
AO
1083 _frvfdpic_add_rofixup (output_bfd,
1084 frvfdpic_gotfixup_section (info),
1085 frvfdpic_got_section (info)->output_section
1086 ->vma
1087 + frvfdpic_got_section (info)->output_offset
1088 + frvfdpic_got_initial_offset (info)
1089 + entry->got_entry, entry);
51532845
AO
1090 }
1091 else
43850d5b
AO
1092 _frvfdpic_add_dyn_reloc (output_bfd, frvfdpic_gotrel_section (info),
1093 _bfd_elf_section_offset
1094 (output_bfd, info,
1095 frvfdpic_got_section (info),
1096 frvfdpic_got_initial_offset (info)
1097 + entry->got_entry)
1098 + frvfdpic_got_section (info)
1099 ->output_section->vma
1100 + frvfdpic_got_section (info)->output_offset,
1101 R_FRV_32, idx, ad, entry);
51532845
AO
1102
1103 bfd_put_32 (output_bfd, ad,
43850d5b
AO
1104 frvfdpic_got_section (info)->contents
1105 + frvfdpic_got_initial_offset (info)
51532845
AO
1106 + entry->got_entry);
1107 }
1108
1109 /* Generate relocation for GOT entry pointing to a canonical
1110 function descriptor. */
1111 if (entry->fdgot_entry)
1112 {
1113 int reloc, idx;
072c8903 1114 bfd_vma ad = 0;
51532845 1115
072c8903
AO
1116 if (! (entry->symndx == -1
1117 && entry->d.h->root.type == bfd_link_hash_undefweak
43850d5b 1118 && FRVFDPIC_SYM_LOCAL (info, entry->d.h)))
51532845 1119 {
072c8903
AO
1120 /* If the symbol is dynamic and there may be dynamic symbol
1121 resolution because we are, or are linked with, a shared
1122 library, emit a FUNCDESC relocation such that the dynamic
1123 linker will allocate the function descriptor. If the
1124 symbol needs a non-local function descriptor but binds
1125 locally (e.g., its visibility is protected, emit a
1126 dynamic relocation decayed to section+offset. */
43850d5b
AO
1127 if (entry->symndx == -1
1128 && ! FRVFDPIC_FUNCDESC_LOCAL (info, entry->d.h)
1129 && FRVFDPIC_SYM_LOCAL (info, entry->d.h)
072c8903
AO
1130 && !(info->executable && !info->pie))
1131 {
1132 reloc = R_FRV_FUNCDESC;
1133 idx = elf_section_data (entry->d.h->root.u.def.section
1134 ->output_section)->dynindx;
1135 ad = entry->d.h->root.u.def.section->output_offset
1136 + entry->d.h->root.u.def.value;
1137 }
1138 else if (entry->symndx == -1
43850d5b 1139 && ! FRVFDPIC_FUNCDESC_LOCAL (info, entry->d.h))
072c8903
AO
1140 {
1141 reloc = R_FRV_FUNCDESC;
1142 idx = dynindx;
1143 ad = addend;
1144 if (ad)
1145 return FALSE;
1146 }
51532845 1147 else
072c8903
AO
1148 {
1149 /* Otherwise, we know we have a private function descriptor,
1150 so reference it directly. */
1151 if (elf_hash_table (info)->dynamic_sections_created)
1152 BFD_ASSERT (entry->privfd);
1153 reloc = R_FRV_32;
43850d5b 1154 idx = elf_section_data (frvfdpic_got_section (info)
072c8903 1155 ->output_section)->dynindx;
43850d5b
AO
1156 ad = frvfdpic_got_section (info)->output_offset
1157 + frvfdpic_got_initial_offset (info) + entry->fd_entry;
072c8903
AO
1158 }
1159
1160 /* If there is room for dynamic symbol resolution, emit the
1161 dynamic relocation. However, if we're linking an
1162 executable at a fixed location, we won't have emitted a
1163 dynamic symbol entry for the got section, so idx will be
1164 zero, which means we can and should compute the address
1165 of the private descriptor ourselves. */
1166 if (info->executable && !info->pie
1167 && (entry->symndx != -1
43850d5b 1168 || FRVFDPIC_FUNCDESC_LOCAL (info, entry->d.h)))
51532845 1169 {
43850d5b
AO
1170 ad += frvfdpic_got_section (info)->output_section->vma;
1171 _frvfdpic_add_rofixup (output_bfd,
1172 frvfdpic_gotfixup_section (info),
1173 frvfdpic_got_section (info)
1174 ->output_section->vma
1175 + frvfdpic_got_section (info)
1176 ->output_offset
1177 + frvfdpic_got_initial_offset (info)
1178 + entry->fdgot_entry, entry);
51532845 1179 }
072c8903 1180 else
43850d5b
AO
1181 _frvfdpic_add_dyn_reloc (output_bfd,
1182 frvfdpic_gotrel_section (info),
1183 _bfd_elf_section_offset
1184 (output_bfd, info,
1185 frvfdpic_got_section (info),
1186 frvfdpic_got_initial_offset (info)
1187 + entry->fdgot_entry)
1188 + frvfdpic_got_section (info)
1189 ->output_section->vma
1190 + frvfdpic_got_section (info)
1191 ->output_offset,
1192 reloc, idx, ad, entry);
51532845 1193 }
51532845
AO
1194
1195 bfd_put_32 (output_bfd, ad,
43850d5b
AO
1196 frvfdpic_got_section (info)->contents
1197 + frvfdpic_got_initial_offset (info)
51532845
AO
1198 + entry->fdgot_entry);
1199 }
1200
1201 /* Generate relocation to fill in a private function descriptor in
1202 the GOT. */
1203 if (entry->fd_entry)
1204 {
1205 int idx = dynindx;
1206 bfd_vma ad = addend;
1207 bfd_vma ofst;
1208 long lowword, highword;
1209
1210 /* If the symbol is dynamic but binds locally, use
1211 section+offset. */
43850d5b
AO
1212 if (sec && (entry->symndx != -1
1213 || FRVFDPIC_SYM_LOCAL (info, entry->d.h)))
51532845
AO
1214 {
1215 if (entry->symndx == -1)
1216 ad += entry->d.h->root.u.def.value;
1217 else
1218 ad += sym->st_value;
1219 ad += sec->output_offset;
1220 if (sec->output_section && elf_section_data (sec->output_section))
1221 idx = elf_section_data (sec->output_section)->dynindx;
1222 else
1223 idx = 0;
1224 }
1225
1226 /* If we're linking an executable at a fixed address, we can
1227 omit the dynamic relocation as long as the symbol is local to
1228 this module. */
1229 if (info->executable && !info->pie
43850d5b 1230 && (entry->symndx != -1 || FRVFDPIC_SYM_LOCAL (info, entry->d.h)))
51532845
AO
1231 {
1232 if (sec)
1233 ad += sec->output_section->vma;
1234 ofst = 0;
3b712a1a
AO
1235 if (entry->symndx != -1
1236 || entry->d.h->root.type != bfd_link_hash_undefweak)
51532845 1237 {
43850d5b
AO
1238 _frvfdpic_add_rofixup (output_bfd,
1239 frvfdpic_gotfixup_section (info),
1240 frvfdpic_got_section (info)
1241 ->output_section->vma
1242 + frvfdpic_got_section (info)
1243 ->output_offset
1244 + frvfdpic_got_initial_offset (info)
1245 + entry->fd_entry, entry);
1246 _frvfdpic_add_rofixup (output_bfd,
1247 frvfdpic_gotfixup_section (info),
1248 frvfdpic_got_section (info)
1249 ->output_section->vma
1250 + frvfdpic_got_section (info)
1251 ->output_offset
1252 + frvfdpic_got_initial_offset (info)
1253 + entry->fd_entry + 4, entry);
51532845
AO
1254 }
1255 }
1256 else
1257 {
1258 ofst =
43850d5b
AO
1259 _frvfdpic_add_dyn_reloc (output_bfd,
1260 entry->lazyplt
1261 ? frvfdpic_pltrel_section (info)
1262 : frvfdpic_gotrel_section (info),
1263 _bfd_elf_section_offset
1264 (output_bfd, info,
1265 frvfdpic_got_section (info),
1266 frvfdpic_got_initial_offset (info)
1267 + entry->fd_entry)
1268 + frvfdpic_got_section (info)
1269 ->output_section->vma
1270 + frvfdpic_got_section (info)
1271 ->output_offset,
1272 R_FRV_FUNCDESC_VALUE, idx, ad, entry);
51532845
AO
1273 }
1274
1275 /* If we've omitted the dynamic relocation, just emit the fixed
1276 addresses of the symbol and of the local GOT base offset. */
1277 if (info->executable && !info->pie && sec && sec->output_section)
1278 {
1279 lowword = ad;
43850d5b
AO
1280 highword = frvfdpic_got_section (info)->output_section->vma
1281 + frvfdpic_got_section (info)->output_offset
1282 + frvfdpic_got_initial_offset (info);
51532845
AO
1283 }
1284 else if (entry->lazyplt)
1285 {
1286 if (ad)
1287 return FALSE;
1288
1289 fd_lazy_rel_offset = ofst;
1290
1291 /* A function descriptor used for lazy or local resolving is
1292 initialized such that its high word contains the output
1293 section index in which the PLT entries are located, and
1294 the low word contains the address of the lazy PLT entry
1295 entry point, that must be within the memory region
1296 assigned to that section. */
1297 lowword = entry->lzplt_entry + 4
43850d5b
AO
1298 + frvfdpic_plt_section (info)->output_offset
1299 + frvfdpic_plt_section (info)->output_section->vma;
1300 highword = _frvfdpic_osec_to_segment
1301 (output_bfd, frvfdpic_plt_section (info)->output_section);
51532845
AO
1302 }
1303 else
1304 {
1305 /* A function descriptor for a local function gets the index
1306 of the section. For a non-local function, it's
1307 disregarded. */
1308 lowword = ad;
1309 if (entry->symndx == -1 && entry->d.h->dynindx != -1
1310 && entry->d.h->dynindx == idx)
1311 highword = 0;
1312 else
43850d5b
AO
1313 highword = _frvfdpic_osec_to_segment
1314 (output_bfd, sec->output_section);
51532845
AO
1315 }
1316
1317 bfd_put_32 (output_bfd, lowword,
43850d5b
AO
1318 frvfdpic_got_section (info)->contents
1319 + frvfdpic_got_initial_offset (info)
51532845
AO
1320 + entry->fd_entry);
1321 bfd_put_32 (output_bfd, highword,
43850d5b
AO
1322 frvfdpic_got_section (info)->contents
1323 + frvfdpic_got_initial_offset (info)
51532845
AO
1324 + entry->fd_entry + 4);
1325 }
1326
1327 /* Generate code for the PLT entry. */
1328 if (entry->plt_entry != (bfd_vma) -1)
1329 {
43850d5b
AO
1330 bfd_byte *plt_code = frvfdpic_plt_section (info)->contents
1331 + entry->plt_entry;
51532845
AO
1332
1333 BFD_ASSERT (entry->fd_entry);
1334
1335 /* Figure out what kind of PLT entry we need, depending on the
1336 location of the function descriptor within the GOT. */
1337 if (entry->fd_entry >= -(1 << (12 - 1))
1338 && entry->fd_entry < (1 << (12 - 1)))
1339 {
1340 /* lddi @(gr15, fd_entry), gr14 */
1341 bfd_put_32 (output_bfd,
1342 0x9cccf000 | (entry->fd_entry & ((1 << 12) - 1)),
1343 plt_code);
1344 plt_code += 4;
1345 }
1346 else
1347 {
1348 if (entry->fd_entry >= -(1 << (16 - 1))
1349 && entry->fd_entry < (1 << (16 - 1)))
1350 {
1351 /* setlos lo(fd_entry), gr14 */
1352 bfd_put_32 (output_bfd,
1353 0x9cfc0000
1354 | (entry->fd_entry & (((bfd_vma)1 << 16) - 1)),
1355 plt_code);
1356 plt_code += 4;
1357 }
1358 else
1359 {
1360 /* sethi.p hi(fd_entry), gr14
1361 setlo lo(fd_entry), gr14 */
1362 bfd_put_32 (output_bfd,
1363 0x1cf80000
1364 | ((entry->fd_entry >> 16)
1365 & (((bfd_vma)1 << 16) - 1)),
1366 plt_code);
1367 bfd_put_32 (output_bfd,
1368 0x9cf40000
1369 | (entry->fd_entry & (((bfd_vma)1 << 16) - 1)),
1370 plt_code);
1371 plt_code += 8;
1372 }
1373 /* ldd @(gr14,gr15),gr14 */
1374 bfd_put_32 (output_bfd, 0x9c08e14f, plt_code);
1375 plt_code += 4;
1376 }
1377 /* jmpl @(gr14,gr0) */
1378 bfd_put_32 (output_bfd, 0x8030e000, plt_code);
1379 }
1380
1381 /* Generate code for the lazy PLT entry. */
1382 if (entry->lzplt_entry != (bfd_vma) -1)
1383 {
43850d5b 1384 bfd_byte *lzplt_code = frvfdpic_plt_section (info)->contents
51532845
AO
1385 + entry->lzplt_entry;
1386 bfd_vma resolverStub_addr;
1387
1388 bfd_put_32 (output_bfd, fd_lazy_rel_offset, lzplt_code);
1389 lzplt_code += 4;
1390
43850d5b
AO
1391 resolverStub_addr = entry->lzplt_entry / FRVFDPIC_LZPLT_BLOCK_SIZE
1392 * FRVFDPIC_LZPLT_BLOCK_SIZE + FRVFDPIC_LZPLT_RESOLV_LOC;
1393 if (resolverStub_addr >= frvfdpic_plt_initial_offset (info))
1394 resolverStub_addr = frvfdpic_plt_initial_offset (info) - 12;
51532845
AO
1395
1396 if (entry->lzplt_entry == resolverStub_addr)
1397 {
1398 /* This is a lazy PLT entry that includes a resolver call. */
1399 /* ldd @(gr15,gr0), gr4
1400 jmpl @(gr4,gr0) */
1401 bfd_put_32 (output_bfd, 0x8808f140, lzplt_code);
1402 bfd_put_32 (output_bfd, 0x80304000, lzplt_code + 4);
1403 }
1404 else
1405 {
1406 /* bra resolverStub */
1407 bfd_put_32 (output_bfd,
1408 0xc01a0000
1409 | (((resolverStub_addr - entry->lzplt_entry)
1410 / 4) & (((bfd_vma)1 << 16) - 1)),
1411 lzplt_code);
1412 }
1413 }
1414
1415 return TRUE;
1416}
1417
1418/* Handle an FRV small data reloc. */
1419
1420static bfd_reloc_status_type
1421elf32_frv_relocate_gprel12 (info, input_bfd, input_section, relocation,
1422 contents, value)
1423 struct bfd_link_info *info;
1424 bfd *input_bfd;
1425 asection *input_section;
1426 Elf_Internal_Rela *relocation;
1427 bfd_byte *contents;
1428 bfd_vma value;
1429{
1430 bfd_vma insn;
1431 bfd_vma gp;
1432 struct bfd_link_hash_entry *h;
1433
1434 h = bfd_link_hash_lookup (info->hash, "_gp", FALSE, FALSE, TRUE);
1435
1436 gp = (h->u.def.value
1437 + h->u.def.section->output_section->vma
1438 + h->u.def.section->output_offset);
1439
1440 value -= input_section->output_section->vma;
1441 value -= (gp - input_section->output_section->vma);
1442
1443 insn = bfd_get_32 (input_bfd, contents + relocation->r_offset);
1444
1445 value += relocation->r_addend;
1446
1447 if ((long) value > 0x7ff || (long) value < -0x800)
1448 return bfd_reloc_overflow;
1449
1450 bfd_put_32 (input_bfd,
1451 (insn & 0xfffff000) | (value & 0xfff),
1452 contents + relocation->r_offset);
1453
1454 return bfd_reloc_ok;
1455}
1456
1457/* Handle an FRV small data reloc. for the u12 field. */
1458
1459static bfd_reloc_status_type
1460elf32_frv_relocate_gprelu12 (info, input_bfd, input_section, relocation,
1461 contents, value)
1462 struct bfd_link_info *info;
1463 bfd *input_bfd;
1464 asection *input_section;
1465 Elf_Internal_Rela *relocation;
1466 bfd_byte *contents;
1467 bfd_vma value;
1468{
1469 bfd_vma insn;
1470 bfd_vma gp;
1471 struct bfd_link_hash_entry *h;
1472 bfd_vma mask;
1473
1474 h = bfd_link_hash_lookup (info->hash, "_gp", FALSE, FALSE, TRUE);
1475
1476 gp = (h->u.def.value
1477 + h->u.def.section->output_section->vma
1478 + h->u.def.section->output_offset);
1479
1480 value -= input_section->output_section->vma;
1481 value -= (gp - input_section->output_section->vma);
4e5ba5b7
DB
1482
1483 insn = bfd_get_32 (input_bfd, contents + relocation->r_offset);
1484
1485 value += relocation->r_addend;
1486
1487 if ((long) value > 0x7ff || (long) value < -0x800)
1488 return bfd_reloc_overflow;
1489
1490 /* The high 6 bits go into bits 17-12. The low 6 bits go into bits 5-0. */
1491 mask = 0x3f03f;
1492 insn = (insn & ~mask) | ((value & 0xfc0) << 12) | (value & 0x3f);
1493
1494 bfd_put_32 (input_bfd, insn, contents + relocation->r_offset);
1495
1496 return bfd_reloc_ok;
1497}
1498
1499/* Handle an FRV ELF HI16 reloc. */
1500
1501static bfd_reloc_status_type
1502elf32_frv_relocate_hi16 (input_bfd, relhi, contents, value)
1503 bfd *input_bfd;
1504 Elf_Internal_Rela *relhi;
1505 bfd_byte *contents;
1506 bfd_vma value;
1507{
1508 bfd_vma insn;
1509
1510 insn = bfd_get_32 (input_bfd, contents + relhi->r_offset);
1511
1512 value += relhi->r_addend;
1513 value = ((value >> 16) & 0xffff);
1514
1515 insn = (insn & 0xffff0000) | value;
1516
1517 if ((long) value > 0xffff || (long) value < -0x10000)
1518 return bfd_reloc_overflow;
1519
1520 bfd_put_32 (input_bfd, insn, contents + relhi->r_offset);
1521 return bfd_reloc_ok;
1522
1523}
1524static bfd_reloc_status_type
1525elf32_frv_relocate_lo16 (input_bfd, rello, contents, value)
1526 bfd *input_bfd;
1527 Elf_Internal_Rela *rello;
1528 bfd_byte *contents;
1529 bfd_vma value;
1530{
1531 bfd_vma insn;
1532
1533 insn = bfd_get_32 (input_bfd, contents + rello->r_offset);
1534
1535 value += rello->r_addend;
1536 value = value & 0xffff;
1537
1538 insn = (insn & 0xffff0000) | value;
1539
1540 if ((long) value > 0xffff || (long) value < -0x10000)
1541 return bfd_reloc_overflow;
1542
1543 bfd_put_32 (input_bfd, insn, contents + rello->r_offset);
1544 return bfd_reloc_ok;
1545}
1546
1547/* Perform the relocation for the CALL label24 instruction. */
1548
1549static bfd_reloc_status_type
1550elf32_frv_relocate_label24 (input_bfd, input_section, rello, contents, value)
1551 bfd *input_bfd;
1552 asection *input_section;
1553 Elf_Internal_Rela *rello;
1554 bfd_byte *contents;
1555 bfd_vma value;
1556{
1557 bfd_vma insn;
1558 bfd_vma label6;
1559 bfd_vma label18;
1560
1561 /* The format for the call instruction is:
1562
b34976b6 1563 0 000000 0001111 000000000000000000
4e5ba5b7
DB
1564 label6 opcode label18
1565
1566 The branch calculation is: pc + (4*label24)
1567 where label24 is the concatenation of label6 and label18. */
1568
1569 /* Grab the instruction. */
1570 insn = bfd_get_32 (input_bfd, contents + rello->r_offset);
1571
1572 value -= input_section->output_section->vma + input_section->output_offset;
1573 value -= rello->r_offset;
1574 value += rello->r_addend;
1575
1576 value = value >> 2;
1577
1578 label6 = value & 0xfc0000;
1579 label6 = label6 << 7;
1580
1581 label18 = value & 0x3ffff;
1582
1583 insn = insn & 0x803c0000;
1584 insn = insn | label6;
1585 insn = insn | label18;
1586
1587 bfd_put_32 (input_bfd, insn, contents + rello->r_offset);
1588
1589 return bfd_reloc_ok;
1590}
1591
1592static bfd_reloc_status_type
b34976b6
AM
1593elf32_frv_relocate_gprelhi (info, input_bfd, input_section, relocation,
1594 contents, value)
4e5ba5b7
DB
1595 struct bfd_link_info *info;
1596 bfd *input_bfd;
1597 asection *input_section;
1598 Elf_Internal_Rela *relocation;
1599 bfd_byte *contents;
1600 bfd_vma value;
1601{
1602 bfd_vma insn;
1603 bfd_vma gp;
1604 struct bfd_link_hash_entry *h;
1605
b34976b6 1606 h = bfd_link_hash_lookup (info->hash, "_gp", FALSE, FALSE, TRUE);
4e5ba5b7
DB
1607
1608 gp = (h->u.def.value
1609 + h->u.def.section->output_section->vma
1610 + h->u.def.section->output_offset);
1611
1612 value -= input_section->output_section->vma;
1613 value -= (gp - input_section->output_section->vma);
1614 value += relocation->r_addend;
1615 value = ((value >> 16) & 0xffff);
1616
1617 if ((long) value > 0xffff || (long) value < -0x10000)
1618 return bfd_reloc_overflow;
1619
1620 insn = bfd_get_32 (input_bfd, contents + relocation->r_offset);
1621 insn = (insn & 0xffff0000) | value;
1622
1623 bfd_put_32 (input_bfd, insn, contents + relocation->r_offset);
1624 return bfd_reloc_ok;
1625}
1626
1627static bfd_reloc_status_type
b34976b6
AM
1628elf32_frv_relocate_gprello (info, input_bfd, input_section, relocation,
1629 contents, value)
4e5ba5b7
DB
1630 struct bfd_link_info *info;
1631 bfd *input_bfd;
1632 asection *input_section;
1633 Elf_Internal_Rela *relocation;
1634 bfd_byte *contents;
1635 bfd_vma value;
1636{
1637 bfd_vma insn;
1638 bfd_vma gp;
1639 struct bfd_link_hash_entry *h;
1640
b34976b6 1641 h = bfd_link_hash_lookup (info->hash, "_gp", FALSE, FALSE, TRUE);
4e5ba5b7
DB
1642
1643 gp = (h->u.def.value
1644 + h->u.def.section->output_section->vma
1645 + h->u.def.section->output_offset);
1646
1647 value -= input_section->output_section->vma;
1648 value -= (gp - input_section->output_section->vma);
1649 value += relocation->r_addend;
1650 value = value & 0xffff;
1651
1652 if ((long) value > 0xffff || (long) value < -0x10000)
1653 return bfd_reloc_overflow;
1654
1655 insn = bfd_get_32 (input_bfd, contents + relocation->r_offset);
1656 insn = (insn & 0xffff0000) | value;
1657
1658 bfd_put_32 (input_bfd, insn, contents + relocation->r_offset);
1659
1660 return bfd_reloc_ok;
1661}
1662
1663static reloc_howto_type *
1664frv_reloc_type_lookup (abfd, code)
b34976b6 1665 bfd *abfd ATTRIBUTE_UNUSED;
4e5ba5b7
DB
1666 bfd_reloc_code_real_type code;
1667{
1668 switch (code)
1669 {
1670 default:
1671 break;
1672
1673 case BFD_RELOC_NONE:
1674 return &elf32_frv_howto_table[ (int) R_FRV_NONE];
b34976b6 1675
4e5ba5b7 1676 case BFD_RELOC_32:
51532845
AO
1677 if (elf_elfheader (abfd)->e_type == ET_EXEC
1678 || elf_elfheader (abfd)->e_type == ET_DYN)
1679 return &elf32_frv_rel_32_howto;
1680 /* Fall through. */
4e5ba5b7
DB
1681 case BFD_RELOC_CTOR:
1682 return &elf32_frv_howto_table[ (int) R_FRV_32];
1683
1684 case BFD_RELOC_FRV_LABEL16:
1685 return &elf32_frv_howto_table[ (int) R_FRV_LABEL16];
1686
1687 case BFD_RELOC_FRV_LABEL24:
1688 return &elf32_frv_howto_table[ (int) R_FRV_LABEL24];
1689
1690 case BFD_RELOC_FRV_LO16:
1691 return &elf32_frv_howto_table[ (int) R_FRV_LO16];
1692
1693 case BFD_RELOC_FRV_HI16:
1694 return &elf32_frv_howto_table[ (int) R_FRV_HI16];
1695
1696 case BFD_RELOC_FRV_GPREL12:
1697 return &elf32_frv_howto_table[ (int) R_FRV_GPREL12];
1698
1699 case BFD_RELOC_FRV_GPRELU12:
1700 return &elf32_frv_howto_table[ (int) R_FRV_GPRELU12];
1701
1702 case BFD_RELOC_FRV_GPREL32:
1703 return &elf32_frv_howto_table[ (int) R_FRV_GPREL32];
1704
1705 case BFD_RELOC_FRV_GPRELHI:
1706 return &elf32_frv_howto_table[ (int) R_FRV_GPRELHI];
1707
1708 case BFD_RELOC_FRV_GPRELLO:
1709 return &elf32_frv_howto_table[ (int) R_FRV_GPRELLO];
1710
51532845
AO
1711 case BFD_RELOC_FRV_GOT12:
1712 return &elf32_frv_howto_table[ (int) R_FRV_GOT12];
1713
1714 case BFD_RELOC_FRV_GOTHI:
1715 return &elf32_frv_howto_table[ (int) R_FRV_GOTHI];
1716
1717 case BFD_RELOC_FRV_GOTLO:
1718 return &elf32_frv_howto_table[ (int) R_FRV_GOTLO];
1719
1720 case BFD_RELOC_FRV_FUNCDESC:
1721 if (elf_elfheader (abfd)->e_type == ET_EXEC
1722 || elf_elfheader (abfd)->e_type == ET_DYN)
1723 return &elf32_frv_rel_funcdesc_howto;
1724 return &elf32_frv_howto_table[ (int) R_FRV_FUNCDESC];
1725
1726 case BFD_RELOC_FRV_FUNCDESC_GOT12:
1727 return &elf32_frv_howto_table[ (int) R_FRV_FUNCDESC_GOT12];
1728
1729 case BFD_RELOC_FRV_FUNCDESC_GOTHI:
1730 return &elf32_frv_howto_table[ (int) R_FRV_FUNCDESC_GOTHI];
1731
1732 case BFD_RELOC_FRV_FUNCDESC_GOTLO:
1733 return &elf32_frv_howto_table[ (int) R_FRV_FUNCDESC_GOTLO];
1734
1735 case BFD_RELOC_FRV_FUNCDESC_VALUE:
1736 if (elf_elfheader (abfd)->e_type == ET_EXEC
1737 || elf_elfheader (abfd)->e_type == ET_DYN)
1738 return &elf32_frv_rel_funcdesc_value_howto;
1739 return &elf32_frv_howto_table[ (int) R_FRV_FUNCDESC_VALUE];
1740
1741 case BFD_RELOC_FRV_FUNCDESC_GOTOFF12:
1742 return &elf32_frv_howto_table[ (int) R_FRV_FUNCDESC_GOTOFF12];
1743
1744 case BFD_RELOC_FRV_FUNCDESC_GOTOFFHI:
1745 return &elf32_frv_howto_table[ (int) R_FRV_FUNCDESC_GOTOFFHI];
1746
1747 case BFD_RELOC_FRV_FUNCDESC_GOTOFFLO:
1748 return &elf32_frv_howto_table[ (int) R_FRV_FUNCDESC_GOTOFFLO];
1749
1750 case BFD_RELOC_FRV_GOTOFF12:
1751 return &elf32_frv_howto_table[ (int) R_FRV_GOTOFF12];
1752
1753 case BFD_RELOC_FRV_GOTOFFHI:
1754 return &elf32_frv_howto_table[ (int) R_FRV_GOTOFFHI];
1755
1756 case BFD_RELOC_FRV_GOTOFFLO:
1757 return &elf32_frv_howto_table[ (int) R_FRV_GOTOFFLO];
1758
4e5ba5b7
DB
1759 case BFD_RELOC_VTABLE_INHERIT:
1760 return &elf32_frv_vtinherit_howto;
1761
1762 case BFD_RELOC_VTABLE_ENTRY:
1763 return &elf32_frv_vtentry_howto;
1764 }
1765
1766 return NULL;
1767}
1768
1769/* Set the howto pointer for an FRV ELF reloc. */
1770
1771static void
1772frv_info_to_howto_rela (abfd, cache_ptr, dst)
b34976b6
AM
1773 bfd *abfd ATTRIBUTE_UNUSED;
1774 arelent *cache_ptr;
1775 Elf_Internal_Rela *dst;
4e5ba5b7
DB
1776{
1777 unsigned int r_type;
1778
1779 r_type = ELF32_R_TYPE (dst->r_info);
1780 switch (r_type)
1781 {
1782 case R_FRV_GNU_VTINHERIT:
1783 cache_ptr->howto = &elf32_frv_vtinherit_howto;
1784 break;
1785
1786 case R_FRV_GNU_VTENTRY:
1787 cache_ptr->howto = &elf32_frv_vtentry_howto;
1788 break;
1789
1790 default:
1791 cache_ptr->howto = & elf32_frv_howto_table [r_type];
1792 break;
1793 }
1794}
51532845
AO
1795
1796/* Set the howto pointer for an FRV ELF REL reloc. */
1797static void
43850d5b
AO
1798frvfdpic_info_to_howto_rel (bfd *abfd ATTRIBUTE_UNUSED,
1799 arelent *cache_ptr, Elf_Internal_Rela *dst)
51532845
AO
1800{
1801 unsigned int r_type;
1802
1803 r_type = ELF32_R_TYPE (dst->r_info);
1804 switch (r_type)
1805 {
1806 case R_FRV_32:
1807 cache_ptr->howto = &elf32_frv_rel_32_howto;
1808 break;
1809
1810 case R_FRV_FUNCDESC:
1811 cache_ptr->howto = &elf32_frv_rel_funcdesc_howto;
1812 break;
1813
1814 case R_FRV_FUNCDESC_VALUE:
1815 cache_ptr->howto = &elf32_frv_rel_funcdesc_value_howto;
1816 break;
1817
1818 default:
1819 cache_ptr->howto = NULL;
1820 break;
1821 }
1822}
4e5ba5b7
DB
1823\f
1824/* Perform a single relocation. By default we use the standard BFD
1825 routines, but a few relocs, we have to do them ourselves. */
1826
1827static bfd_reloc_status_type
b34976b6
AM
1828frv_final_link_relocate (howto, input_bfd, input_section, contents, rel,
1829 relocation)
1830 reloc_howto_type *howto;
1831 bfd *input_bfd;
1832 asection *input_section;
1833 bfd_byte *contents;
1834 Elf_Internal_Rela *rel;
1835 bfd_vma relocation;
4e5ba5b7
DB
1836{
1837 return _bfd_final_link_relocate (howto, input_bfd, input_section,
1838 contents, rel->r_offset, relocation,
1839 rel->r_addend);
1840}
1841
1842\f
1843/* Relocate an FRV ELF section.
4e5ba5b7
DB
1844
1845 The RELOCATE_SECTION function is called by the new ELF backend linker
1846 to handle the relocations for a section.
1847
1848 The relocs are always passed as Rela structures; if the section
1849 actually uses Rel structures, the r_addend field will always be
1850 zero.
1851
1852 This function is responsible for adjusting the section contents as
1049f94e 1853 necessary, and (if using Rela relocs and generating a relocatable
4e5ba5b7
DB
1854 output file) adjusting the reloc addend as necessary.
1855
1856 This function does not have to worry about setting the reloc
1857 address or the reloc symbol index.
1858
1859 LOCAL_SYMS is a pointer to the swapped in local symbols.
1860
1861 LOCAL_SECTIONS is an array giving the section in the input file
1862 corresponding to the st_shndx field of each local symbol.
1863
1864 The global hash table entry for the global symbols can be found
1865 via elf_sym_hashes (input_bfd).
1866
1049f94e 1867 When generating relocatable output, this function must handle
4e5ba5b7
DB
1868 STB_LOCAL/STT_SECTION symbols specially. The output symbol is
1869 going to be the section symbol corresponding to the output
1870 section, which means that the addend must be adjusted
1871 accordingly. */
1872
b34976b6 1873static bfd_boolean
4e5ba5b7 1874elf32_frv_relocate_section (output_bfd, info, input_bfd, input_section,
de2d743e 1875 contents, relocs, local_syms, local_sections)
b34976b6
AM
1876 bfd *output_bfd ATTRIBUTE_UNUSED;
1877 struct bfd_link_info *info;
1878 bfd *input_bfd;
1879 asection *input_section;
1880 bfd_byte *contents;
1881 Elf_Internal_Rela *relocs;
1882 Elf_Internal_Sym *local_syms;
1883 asection **local_sections;
4e5ba5b7 1884{
b34976b6
AM
1885 Elf_Internal_Shdr *symtab_hdr;
1886 struct elf_link_hash_entry **sym_hashes;
1887 Elf_Internal_Rela *rel;
1888 Elf_Internal_Rela *relend;
51532845
AO
1889 unsigned isec_segment, got_segment, plt_segment, gprel_segment,
1890 check_segment[2];
1891 int silence_segment_error = !(info->shared || info->pie);
4e5ba5b7 1892
1049f94e 1893 if (info->relocatable)
b34976b6 1894 return TRUE;
de2d743e 1895
4e5ba5b7
DB
1896 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
1897 sym_hashes = elf_sym_hashes (input_bfd);
1898 relend = relocs + input_section->reloc_count;
1899
43850d5b
AO
1900 isec_segment = _frvfdpic_osec_to_segment (output_bfd,
1901 input_section->output_section);
1902 if (IS_FDPIC (output_bfd) && frvfdpic_got_section (info))
1903 got_segment = _frvfdpic_osec_to_segment (output_bfd,
1904 frvfdpic_got_section (info)
1905 ->output_section);
51532845
AO
1906 else
1907 got_segment = -1;
43850d5b
AO
1908 if (IS_FDPIC (output_bfd) && frvfdpic_gotfixup_section (info))
1909 gprel_segment = _frvfdpic_osec_to_segment (output_bfd,
1910 frvfdpic_gotfixup_section (info)
1911 ->output_section);
51532845
AO
1912 else
1913 gprel_segment = -1;
43850d5b
AO
1914 if (IS_FDPIC (output_bfd) && elf_hash_table (info)->dynamic_sections_created)
1915 plt_segment = _frvfdpic_osec_to_segment (output_bfd,
1916 frvfdpic_plt_section (info)
1917 ->output_section);
51532845
AO
1918 else
1919 plt_segment = -1;
1920
4e5ba5b7
DB
1921 for (rel = relocs; rel < relend; rel ++)
1922 {
b34976b6
AM
1923 reloc_howto_type *howto;
1924 unsigned long r_symndx;
1925 Elf_Internal_Sym *sym;
1926 asection *sec;
1927 struct elf_link_hash_entry *h;
1928 bfd_vma relocation;
1929 bfd_reloc_status_type r;
1930 const char * name = NULL;
1931 int r_type;
51532845 1932 asection *osec;
43850d5b 1933 struct frvfdpic_relocs_info *picrel;
51532845
AO
1934 bfd_vma orig_addend = rel->r_addend;
1935
1936 r_type = ELF32_R_TYPE (rel->r_info);
1937
1938 if ( r_type == R_FRV_GNU_VTINHERIT
1939 || r_type == R_FRV_GNU_VTENTRY)
1940 continue;
1941
1942 /* This is a final link. */
1943 r_symndx = ELF32_R_SYM (rel->r_info);
1944 howto = elf32_frv_howto_table + ELF32_R_TYPE (rel->r_info);
1945 h = NULL;
1946 sym = NULL;
1947 sec = NULL;
1948
1949 if (r_symndx < symtab_hdr->sh_info)
1950 {
1951 sym = local_syms + r_symndx;
1952 osec = sec = local_sections [r_symndx];
1953 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
1954
1955 name = bfd_elf_string_from_elf_section
1956 (input_bfd, symtab_hdr->sh_link, sym->st_name);
1957 name = (name == NULL) ? bfd_section_name (input_bfd, sec) : name;
1958 }
1959 else
1960 {
1961 h = sym_hashes [r_symndx - symtab_hdr->sh_info];
1962
1963 while (h->root.type == bfd_link_hash_indirect
1964 || h->root.type == bfd_link_hash_warning)
1965 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1966
1967 name = h->root.root.string;
1968
1969 if ((h->root.type == bfd_link_hash_defined
1970 || h->root.type == bfd_link_hash_defweak)
43850d5b 1971 && ! FRVFDPIC_SYM_LOCAL (info, h))
51532845
AO
1972 {
1973 sec = NULL;
1974 relocation = 0;
1975 }
1976 else
1977 if (h->root.type == bfd_link_hash_defined
1978 || h->root.type == bfd_link_hash_defweak)
1979 {
1980 sec = h->root.u.def.section;
1981 relocation = (h->root.u.def.value
1982 + sec->output_section->vma
1983 + sec->output_offset);
1984 }
1985 else if (h->root.type == bfd_link_hash_undefweak)
1986 {
1987 relocation = 0;
1988 }
59c2e50f 1989 else if (info->unresolved_syms_in_objects == RM_IGNORE
51532845
AO
1990 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
1991 relocation = 0;
1992 else
1993 {
1994 if (! ((*info->callbacks->undefined_symbol)
1995 (info, h->root.root.string, input_bfd,
59c2e50f
L
1996 input_section, rel->r_offset,
1997 (info->unresolved_syms_in_objects == RM_GENERATE_ERROR
1998 || ELF_ST_VISIBILITY (h->other)))))
51532845
AO
1999 return FALSE;
2000 relocation = 0;
2001 }
2002 osec = sec;
2003 }
2004
2005 switch (r_type)
2006 {
2007 case R_FRV_LABEL24:
2008 case R_FRV_32:
43850d5b
AO
2009 if (! IS_FDPIC (output_bfd))
2010 goto non_fdpic;
2011
51532845
AO
2012 case R_FRV_GOT12:
2013 case R_FRV_GOTHI:
2014 case R_FRV_GOTLO:
2015 case R_FRV_FUNCDESC_GOT12:
2016 case R_FRV_FUNCDESC_GOTHI:
2017 case R_FRV_FUNCDESC_GOTLO:
2018 case R_FRV_GOTOFF12:
2019 case R_FRV_GOTOFFHI:
2020 case R_FRV_GOTOFFLO:
2021 case R_FRV_FUNCDESC_GOTOFF12:
2022 case R_FRV_FUNCDESC_GOTOFFHI:
2023 case R_FRV_FUNCDESC_GOTOFFLO:
2024 case R_FRV_FUNCDESC:
2025 case R_FRV_FUNCDESC_VALUE:
2026 if (h != NULL)
43850d5b
AO
2027 picrel = frvfdpic_relocs_info_for_global (frvfdpic_relocs_info
2028 (info), input_bfd, h,
2029 orig_addend, INSERT);
51532845
AO
2030 else
2031 /* In order to find the entry we created before, we must
2032 use the original addend, not the one that may have been
2033 modified by _bfd_elf_rela_local_sym(). */
43850d5b
AO
2034 picrel = frvfdpic_relocs_info_for_local (frvfdpic_relocs_info
2035 (info), input_bfd, r_symndx,
2036 orig_addend, INSERT);
51532845
AO
2037 if (! picrel)
2038 return FALSE;
2039
43850d5b
AO
2040 if (!_frvfdpic_emit_got_relocs_plt_entries (picrel, output_bfd, info,
2041 osec, sym,
2042 rel->r_addend))
51532845 2043 {
43850d5b
AO
2044 (*_bfd_error_handler)
2045 (_("%s: relocation at `%s+0x%x' references symbol `%s' with nonzero addend"),
2046 bfd_archive_filename (input_bfd), input_section->name,
2047 rel->r_offset, name);
51532845
AO
2048 return FALSE;
2049
2050 }
2051
2052 break;
2053
2054 default:
43850d5b 2055 non_fdpic:
51532845 2056 picrel = NULL;
43850d5b 2057 if (h && ! FRVFDPIC_SYM_LOCAL (info, h))
51532845
AO
2058 {
2059 info->callbacks->warning
2060 (info, _("relocation references symbol not defined in the module"),
2061 name, input_bfd, input_section, rel->r_offset);
2062 return FALSE;
2063 }
2064 break;
2065 }
2066
2067 switch (r_type)
2068 {
2069 case R_FRV_LABEL24:
2070 check_segment[0] = isec_segment;
43850d5b
AO
2071 if (! IS_FDPIC (output_bfd))
2072 check_segment[1] = isec_segment;
2073 else if (picrel->plt)
51532845 2074 {
43850d5b
AO
2075 relocation = frvfdpic_plt_section (info)->output_section->vma
2076 + frvfdpic_plt_section (info)->output_offset
51532845
AO
2077 + picrel->plt_entry;
2078 check_segment[1] = plt_segment;
2079 }
2080 /* We don't want to warn on calls to undefined weak symbols,
2081 as calls to them must be protected by non-NULL tests
2082 anyway, and unprotected calls would invoke undefined
2083 behavior. */
2084 else if (picrel->symndx == -1
2085 && picrel->d.h->root.type == bfd_link_hash_undefweak)
2086 check_segment[1] = check_segment[0];
2087 else
2088 check_segment[1] = sec
43850d5b 2089 ? _frvfdpic_osec_to_segment (output_bfd, sec->output_section)
51532845
AO
2090 : (unsigned)-1;
2091 break;
2092
2093 case R_FRV_GOT12:
2094 case R_FRV_GOTHI:
2095 case R_FRV_GOTLO:
2096 relocation = picrel->got_entry;
2097 check_segment[0] = check_segment[1] = got_segment;
2098 break;
2099
2100 case R_FRV_FUNCDESC_GOT12:
2101 case R_FRV_FUNCDESC_GOTHI:
2102 case R_FRV_FUNCDESC_GOTLO:
2103 relocation = picrel->fdgot_entry;
2104 check_segment[0] = check_segment[1] = got_segment;
2105 break;
2106
2107 case R_FRV_GOTOFFHI:
2108 case R_FRV_GOTOFF12:
2109 case R_FRV_GOTOFFLO:
43850d5b
AO
2110 relocation -= frvfdpic_got_section (info)->output_section->vma
2111 + frvfdpic_got_section (info)->output_offset
2112 + frvfdpic_got_initial_offset (info);
51532845
AO
2113 check_segment[0] = got_segment;
2114 check_segment[1] = sec
43850d5b 2115 ? _frvfdpic_osec_to_segment (output_bfd, sec->output_section)
51532845
AO
2116 : (unsigned)-1;
2117 break;
2118
2119 case R_FRV_FUNCDESC_GOTOFF12:
2120 case R_FRV_FUNCDESC_GOTOFFHI:
2121 case R_FRV_FUNCDESC_GOTOFFLO:
2122 relocation = picrel->fd_entry;
2123 check_segment[0] = check_segment[1] = got_segment;
2124 break;
2125
2126 case R_FRV_FUNCDESC:
2127 {
2128 int dynindx;
2129 bfd_vma addend = rel->r_addend;
2130
072c8903 2131 if (! (h && h->root.type == bfd_link_hash_undefweak
43850d5b 2132 && FRVFDPIC_SYM_LOCAL (info, h)))
51532845 2133 {
072c8903
AO
2134 /* If the symbol is dynamic and there may be dynamic
2135 symbol resolution because we are or are linked with a
2136 shared library, emit a FUNCDESC relocation such that
2137 the dynamic linker will allocate the function
2138 descriptor. If the symbol needs a non-local function
2139 descriptor but binds locally (e.g., its visibility is
2140 protected, emit a dynamic relocation decayed to
2141 section+offset. */
43850d5b
AO
2142 if (h && ! FRVFDPIC_FUNCDESC_LOCAL (info, h)
2143 && FRVFDPIC_SYM_LOCAL (info, h)
072c8903 2144 && !(info->executable && !info->pie))
51532845 2145 {
072c8903
AO
2146 dynindx = elf_section_data (h->root.u.def.section
2147 ->output_section)->dynindx;
2148 addend += h->root.u.def.section->output_offset
2149 + h->root.u.def.value;
2150 }
43850d5b 2151 else if (h && ! FRVFDPIC_FUNCDESC_LOCAL (info, h))
072c8903
AO
2152 {
2153 if (addend)
2154 {
2155 info->callbacks->warning
2156 (info, _("R_FRV_FUNCDESC references dynamic symbol with nonzero addend"),
2157 name, input_bfd, input_section, rel->r_offset);
2158 return FALSE;
2159 }
2160 dynindx = h->dynindx;
2161 }
2162 else
2163 {
2164 /* Otherwise, we know we have a private function
2165 descriptor, so reference it directly. */
2166 BFD_ASSERT (picrel->privfd);
2167 r_type = R_FRV_32;
43850d5b 2168 dynindx = elf_section_data (frvfdpic_got_section (info)
072c8903 2169 ->output_section)->dynindx;
43850d5b
AO
2170 addend = frvfdpic_got_section (info)->output_offset
2171 + frvfdpic_got_initial_offset (info)
072c8903 2172 + picrel->fd_entry;
51532845 2173 }
51532845 2174
072c8903
AO
2175 /* If there is room for dynamic symbol resolution, emit
2176 the dynamic relocation. However, if we're linking an
2177 executable at a fixed location, we won't have emitted a
2178 dynamic symbol entry for the got section, so idx will
2179 be zero, which means we can and should compute the
2180 address of the private descriptor ourselves. */
2181 if (info->executable && !info->pie
43850d5b 2182 && (!h || FRVFDPIC_FUNCDESC_LOCAL (info, h)))
072c8903 2183 {
43850d5b 2184 addend += frvfdpic_got_section (info)->output_section->vma;
072c8903
AO
2185 if ((bfd_get_section_flags (output_bfd,
2186 input_section->output_section)
2187 & (SEC_ALLOC | SEC_LOAD)) == (SEC_ALLOC | SEC_LOAD))
2188 {
43850d5b
AO
2189 if (_frvfdpic_osec_readonly_p (output_bfd,
2190 input_section
2191 ->output_section))
072c8903
AO
2192 {
2193 info->callbacks->warning
2194 (info,
2195 _("cannot emit fixups in read-only section"),
2196 name, input_bfd, input_section, rel->r_offset);
2197 return FALSE;
2198 }
43850d5b
AO
2199 _frvfdpic_add_rofixup (output_bfd,
2200 frvfdpic_gotfixup_section
2201 (info),
2202 _bfd_elf_section_offset
2203 (output_bfd, info,
2204 input_section, rel->r_offset)
2205 + input_section
2206 ->output_section->vma
2207 + input_section->output_offset,
2208 picrel);
072c8903
AO
2209 }
2210 }
2211 else if ((bfd_get_section_flags (output_bfd,
2212 input_section->output_section)
2213 & (SEC_ALLOC | SEC_LOAD)) == (SEC_ALLOC | SEC_LOAD))
51532845 2214 {
43850d5b
AO
2215 if (_frvfdpic_osec_readonly_p (output_bfd,
2216 input_section
2217 ->output_section))
51532845
AO
2218 {
2219 info->callbacks->warning
2220 (info,
072c8903 2221 _("cannot emit dynamic relocations in read-only section"),
51532845
AO
2222 name, input_bfd, input_section, rel->r_offset);
2223 return FALSE;
2224 }
43850d5b
AO
2225 _frvfdpic_add_dyn_reloc (output_bfd,
2226 frvfdpic_gotrel_section (info),
2227 _bfd_elf_section_offset
2228 (output_bfd, info,
2229 input_section, rel->r_offset)
2230 + input_section
2231 ->output_section->vma
2232 + input_section->output_offset,
2233 r_type, dynindx, addend, picrel);
51532845 2234 }
43850d5b
AO
2235 else
2236 addend += frvfdpic_got_section (info)->output_section->vma;
51532845
AO
2237 }
2238
2239 /* We want the addend in-place because dynamic
2240 relocations are REL. Setting relocation to it should
2241 arrange for it to be installed. */
2242 relocation = addend - rel->r_addend;
2243 }
2244 check_segment[0] = check_segment[1] = got_segment;
2245 break;
2246
2247 case R_FRV_32:
43850d5b
AO
2248 if (! IS_FDPIC (output_bfd))
2249 {
2250 check_segment[0] = check_segment[1] = -1;
2251 break;
2252 }
2253 /* Fall through. */
51532845
AO
2254 case R_FRV_FUNCDESC_VALUE:
2255 {
2256 int dynindx;
2257 bfd_vma addend = rel->r_addend;
2258
2259 /* If the symbol is dynamic but binds locally, use
2260 section+offset. */
43850d5b 2261 if (h && ! FRVFDPIC_SYM_LOCAL (info, h))
51532845
AO
2262 {
2263 if (addend && r_type == R_FRV_FUNCDESC_VALUE)
2264 {
2265 info->callbacks->warning
2266 (info, _("R_FRV_FUNCDESC_VALUE references dynamic symbol with nonzero addend"),
2267 name, input_bfd, input_section, rel->r_offset);
2268 return FALSE;
2269 }
2270 dynindx = h->dynindx;
2271 }
2272 else
2273 {
2274 if (h)
2275 addend += h->root.u.def.value;
2276 else
2277 addend += sym->st_value;
2278 if (osec)
2279 addend += osec->output_offset;
2280 if (osec && osec->output_section
2281 && ! bfd_is_abs_section (osec->output_section)
2282 && ! bfd_is_und_section (osec->output_section))
2283 dynindx = elf_section_data (osec->output_section)->dynindx;
2284 else
2285 dynindx = 0;
2286 }
2287
2288 /* If we're linking an executable at a fixed address, we
2289 can omit the dynamic relocation as long as the symbol
2290 is defined in the current link unit (which is implied
2291 by its output section not being NULL). */
2292 if (info->executable && !info->pie
43850d5b 2293 && (!h || FRVFDPIC_SYM_LOCAL (info, h)))
51532845
AO
2294 {
2295 if (osec)
2296 addend += osec->output_section->vma;
43850d5b 2297 if (IS_FDPIC (input_bfd)
51532845
AO
2298 && (bfd_get_section_flags (output_bfd,
2299 input_section->output_section)
2300 & (SEC_ALLOC | SEC_LOAD)) == (SEC_ALLOC | SEC_LOAD))
2301 {
43850d5b
AO
2302 if (_frvfdpic_osec_readonly_p (output_bfd,
2303 input_section
2304 ->output_section))
51532845
AO
2305 {
2306 info->callbacks->warning
2307 (info,
2308 _("cannot emit fixups in read-only section"),
2309 name, input_bfd, input_section, rel->r_offset);
2310 return FALSE;
2311 }
2312 if (!h || h->root.type != bfd_link_hash_undefweak)
2313 {
43850d5b
AO
2314 _frvfdpic_add_rofixup (output_bfd,
2315 frvfdpic_gotfixup_section
2316 (info),
2317 _bfd_elf_section_offset
2318 (output_bfd, info,
2319 input_section, rel->r_offset)
2320 + input_section
2321 ->output_section->vma
2322 + input_section->output_offset,
2323 picrel);
51532845 2324 if (r_type == R_FRV_FUNCDESC_VALUE)
43850d5b 2325 _frvfdpic_add_rofixup
51532845 2326 (output_bfd,
43850d5b 2327 frvfdpic_gotfixup_section (info),
51532845
AO
2328 _bfd_elf_section_offset
2329 (output_bfd, info,
2330 input_section, rel->r_offset)
2331 + input_section->output_section->vma
3b712a1a 2332 + input_section->output_offset + 4, picrel);
51532845
AO
2333 }
2334 }
2335 }
2336 else
2337 {
2338 if ((bfd_get_section_flags (output_bfd,
2339 input_section->output_section)
2340 & (SEC_ALLOC | SEC_LOAD)) == (SEC_ALLOC | SEC_LOAD))
2341 {
43850d5b
AO
2342 if (_frvfdpic_osec_readonly_p (output_bfd,
2343 input_section
2344 ->output_section))
51532845
AO
2345 {
2346 info->callbacks->warning
2347 (info,
2348 _("cannot emit dynamic relocations in read-only section"),
2349 name, input_bfd, input_section, rel->r_offset);
2350 return FALSE;
2351 }
43850d5b
AO
2352 _frvfdpic_add_dyn_reloc (output_bfd,
2353 frvfdpic_gotrel_section (info),
2354 _bfd_elf_section_offset
2355 (output_bfd, info,
2356 input_section, rel->r_offset)
2357 + input_section
2358 ->output_section->vma
2359 + input_section->output_offset,
2360 r_type, dynindx, addend, picrel);
51532845 2361 }
43850d5b
AO
2362 else if (osec)
2363 addend += osec->output_section->vma;
51532845
AO
2364 /* We want the addend in-place because dynamic
2365 relocations are REL. Setting relocation to it
2366 should arrange for it to be installed. */
2367 relocation = addend - rel->r_addend;
2368 }
2369
2370 if (r_type == R_FRV_FUNCDESC_VALUE)
2371 {
2372 /* If we've omitted the dynamic relocation, just emit
2373 the fixed addresses of the symbol and of the local
2374 GOT base offset. */
2375 if (info->executable && !info->pie
43850d5b 2376 && (!h || FRVFDPIC_SYM_LOCAL (info, h)))
51532845 2377 bfd_put_32 (output_bfd,
43850d5b
AO
2378 frvfdpic_got_section (info)->output_section->vma
2379 + frvfdpic_got_section (info)->output_offset
2380 + frvfdpic_got_initial_offset (info),
51532845
AO
2381 contents + rel->r_offset + 4);
2382 else
2383 /* A function descriptor used for lazy or local
2384 resolving is initialized such that its high word
2385 contains the output section index in which the
2386 PLT entries are located, and the low word
2387 contains the offset of the lazy PLT entry entry
2388 point into that section. */
2389 bfd_put_32 (output_bfd,
43850d5b 2390 h && ! FRVFDPIC_SYM_LOCAL (info, h)
51532845 2391 ? 0
43850d5b
AO
2392 : _frvfdpic_osec_to_segment (output_bfd,
2393 sec
2394 ->output_section),
51532845
AO
2395 contents + rel->r_offset + 4);
2396 }
2397 }
2398 check_segment[0] = check_segment[1] = got_segment;
2399 break;
2400
2401 case R_FRV_GPREL12:
2402 case R_FRV_GPRELU12:
2403 case R_FRV_GPREL32:
2404 case R_FRV_GPRELHI:
2405 case R_FRV_GPRELLO:
2406 check_segment[0] = gprel_segment;
2407 check_segment[1] = sec
43850d5b 2408 ? _frvfdpic_osec_to_segment (output_bfd, sec->output_section)
51532845
AO
2409 : (unsigned)-1;
2410 break;
2411
2412 default:
2413 check_segment[0] = isec_segment;
2414 check_segment[1] = sec
43850d5b 2415 ? _frvfdpic_osec_to_segment (output_bfd, sec->output_section)
51532845
AO
2416 : (unsigned)-1;
2417 break;
2418 }
2419
43850d5b 2420 if (check_segment[0] != check_segment[1] && IS_FDPIC (output_bfd))
51532845 2421 {
43850d5b
AO
2422#if 1 /* If you take this out, remove the #error from fdpic-static-6.d
2423 in the ld testsuite. */
51532845
AO
2424 /* This helps catch problems in GCC while we can't do more
2425 than static linking. The idea is to test whether the
2426 input file basename is crt0.o only once. */
2427 if (silence_segment_error == 1)
2428 silence_segment_error =
2429 (strlen (input_bfd->filename) == 6
2430 && strcmp (input_bfd->filename, "crt0.o") == 0)
2431 || (strlen (input_bfd->filename) > 6
2432 && strcmp (input_bfd->filename
2433 + strlen (input_bfd->filename) - 7,
2434 "/crt0.o") == 0)
2435 ? -1 : 0;
2436#endif
2437 if (!silence_segment_error
2438 /* We don't want duplicate errors for undefined
2439 symbols. */
2440 && !(picrel && picrel->symndx == -1
2441 && picrel->d.h->root.type == bfd_link_hash_undefined))
2442 info->callbacks->warning
2443 (info,
2444 (info->shared || info->pie)
2445 ? _("relocations between different segments are not supported")
2446 : _("warning: relocation references a different segment"),
2447 name, input_bfd, input_section, rel->r_offset);
2448 if (!silence_segment_error && (info->shared || info->pie))
2449 return FALSE;
2450 elf_elfheader (output_bfd)->e_flags |= EF_FRV_PIC;
2451 }
2452
2453 switch (r_type)
2454 {
2455 case R_FRV_GOTOFFHI:
2456 /* We need the addend to be applied before we shift the
2457 value right. */
2458 relocation += rel->r_addend;
2459 /* Fall through. */
2460 case R_FRV_GOTHI:
2461 case R_FRV_FUNCDESC_GOTHI:
2462 case R_FRV_FUNCDESC_GOTOFFHI:
2463 relocation >>= 16;
2464 /* Fall through. */
2465
2466 case R_FRV_GOTLO:
2467 case R_FRV_FUNCDESC_GOTLO:
2468 case R_FRV_GOTOFFLO:
2469 case R_FRV_FUNCDESC_GOTOFFLO:
2470 relocation &= 0xffff;
2471 break;
2472
2473 default:
2474 break;
2475 }
2476
2477 switch (r_type)
2478 {
2479 case R_FRV_LABEL24:
43850d5b 2480 if (! IS_FDPIC (output_bfd) || ! picrel->plt)
51532845
AO
2481 break;
2482 /* Fall through. */
2483
2484 /* When referencing a GOT entry, a function descriptor or a
2485 PLT, we don't want the addend to apply to the reference,
2486 but rather to the referenced symbol. The actual entry
2487 will have already been created taking the addend into
2488 account, so cancel it out here. */
2489 case R_FRV_GOT12:
2490 case R_FRV_GOTHI:
2491 case R_FRV_GOTLO:
2492 case R_FRV_FUNCDESC_GOT12:
2493 case R_FRV_FUNCDESC_GOTHI:
2494 case R_FRV_FUNCDESC_GOTLO:
2495 case R_FRV_FUNCDESC_GOTOFF12:
2496 case R_FRV_FUNCDESC_GOTOFFHI:
2497 case R_FRV_FUNCDESC_GOTOFFLO:
2498 /* Note that we only want GOTOFFHI, not GOTOFFLO or GOTOFF12
2499 here, since we do want to apply the addend to the others.
2500 Note that we've applied the addend to GOTOFFHI before we
2501 shifted it right. */
2502 case R_FRV_GOTOFFHI:
2503 relocation -= rel->r_addend;
2504 break;
2505
2506 default:
2507 break;
2508 }
2509
2510 if (r_type == R_FRV_HI16)
2511 r = elf32_frv_relocate_hi16 (input_bfd, rel, contents, relocation);
2512
2513 else if (r_type == R_FRV_LO16)
2514 r = elf32_frv_relocate_lo16 (input_bfd, rel, contents, relocation);
2515
2516 else if (r_type == R_FRV_LABEL24)
2517 r = elf32_frv_relocate_label24 (input_bfd, input_section, rel,
2518 contents, relocation);
2519
2520 else if (r_type == R_FRV_GPREL12)
2521 r = elf32_frv_relocate_gprel12 (info, input_bfd, input_section, rel,
2522 contents, relocation);
2523
2524 else if (r_type == R_FRV_GPRELU12)
2525 r = elf32_frv_relocate_gprelu12 (info, input_bfd, input_section, rel,
2526 contents, relocation);
2527
2528 else if (r_type == R_FRV_GPRELLO)
2529 r = elf32_frv_relocate_gprello (info, input_bfd, input_section, rel,
2530 contents, relocation);
2531
2532 else if (r_type == R_FRV_GPRELHI)
2533 r = elf32_frv_relocate_gprelhi (info, input_bfd, input_section, rel,
2534 contents, relocation);
2535
2536 else
2537 r = frv_final_link_relocate (howto, input_bfd, input_section, contents,
2538 rel, relocation);
2539
2540 if (r != bfd_reloc_ok)
2541 {
2542 const char * msg = (const char *) NULL;
2543
2544 switch (r)
2545 {
2546 case bfd_reloc_overflow:
2547 r = info->callbacks->reloc_overflow
2548 (info, name, howto->name, (bfd_vma) 0,
2549 input_bfd, input_section, rel->r_offset);
2550 break;
2551
2552 case bfd_reloc_undefined:
2553 r = info->callbacks->undefined_symbol
2554 (info, name, input_bfd, input_section, rel->r_offset, TRUE);
2555 break;
2556
2557 case bfd_reloc_outofrange:
2558 msg = _("internal error: out of range error");
2559 break;
2560
2561 case bfd_reloc_notsupported:
2562 msg = _("internal error: unsupported relocation error");
2563 break;
2564
2565 case bfd_reloc_dangerous:
2566 msg = _("internal error: dangerous relocation");
2567 break;
2568
2569 default:
2570 msg = _("internal error: unknown error");
2571 break;
2572 }
2573
2574 if (msg)
2575 r = info->callbacks->warning
2576 (info, msg, name, input_bfd, input_section, rel->r_offset);
2577
2578 if (! r)
2579 return FALSE;
2580 }
2581 }
2582
2583 return TRUE;
2584}
2585\f
2586/* Return the section that should be marked against GC for a given
2587 relocation. */
2588
2589static asection *
2590elf32_frv_gc_mark_hook (sec, info, rel, h, sym)
2591 asection *sec;
2592 struct bfd_link_info *info ATTRIBUTE_UNUSED;
2593 Elf_Internal_Rela *rel;
2594 struct elf_link_hash_entry *h;
2595 Elf_Internal_Sym *sym;
2596{
2597 if (h != NULL)
2598 {
2599 switch (ELF32_R_TYPE (rel->r_info))
2600 {
2601 case R_FRV_GNU_VTINHERIT:
2602 case R_FRV_GNU_VTENTRY:
2603 break;
2604
2605 default:
2606 switch (h->root.type)
2607 {
2608 default:
2609 break;
2610
2611 case bfd_link_hash_defined:
2612 case bfd_link_hash_defweak:
2613 return h->root.u.def.section;
2614
2615 case bfd_link_hash_common:
2616 return h->root.u.c.p->section;
2617 }
2618 }
2619 }
2620 else
2621 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
2622
2623 return NULL;
2624}
2625
2626/* Update the got entry reference counts for the section being removed. */
2627
2628static bfd_boolean
2629elf32_frv_gc_sweep_hook (abfd, info, sec, relocs)
2630 bfd *abfd ATTRIBUTE_UNUSED;
2631 struct bfd_link_info *info ATTRIBUTE_UNUSED;
2632 asection *sec ATTRIBUTE_UNUSED;
2633 const Elf_Internal_Rela *relocs ATTRIBUTE_UNUSED;
2634{
2635 return TRUE;
2636}
2637
2638\f
2639/* Hook called by the linker routine which adds symbols from an object
2640 file. We use it to put .comm items in .scomm, and not .comm. */
2641
2642static bfd_boolean
2643elf32_frv_add_symbol_hook (abfd, info, sym, namep, flagsp, secp, valp)
2644 bfd *abfd;
2645 struct bfd_link_info *info;
555cd476 2646 Elf_Internal_Sym *sym;
51532845
AO
2647 const char **namep ATTRIBUTE_UNUSED;
2648 flagword *flagsp ATTRIBUTE_UNUSED;
2649 asection **secp;
2650 bfd_vma *valp;
2651{
2652 if (sym->st_shndx == SHN_COMMON
2653 && !info->relocatable
2654 && (int)sym->st_size <= (int)bfd_get_gp_size (abfd))
2655 {
2656 /* Common symbols less than or equal to -G nn bytes are
2657 automatically put into .sbss. */
2658
2659 asection *scomm = bfd_get_section_by_name (abfd, ".scommon");
2660
2661 if (scomm == NULL)
2662 {
2663 scomm = bfd_make_section (abfd, ".scommon");
2664 if (scomm == NULL
2665 || !bfd_set_section_flags (abfd, scomm, (SEC_ALLOC
2666 | SEC_IS_COMMON
2667 | SEC_LINKER_CREATED)))
2668 return FALSE;
2669 }
2670
2671 *secp = scomm;
2672 *valp = sym->st_size;
2673 }
2674
2675 return TRUE;
2676}
43850d5b 2677
aee6f5b4
AO
2678/* We need dynamic symbols for every section, since segments can
2679 relocate independently. */
2680static bfd_boolean
2681_frvfdpic_link_omit_section_dynsym (bfd *output_bfd ATTRIBUTE_UNUSED,
2682 struct bfd_link_info *info
2683 ATTRIBUTE_UNUSED,
2684 asection *p ATTRIBUTE_UNUSED)
2685{
2686 switch (elf_section_data (p)->this_hdr.sh_type)
2687 {
2688 case SHT_PROGBITS:
2689 case SHT_NOBITS:
2690 /* If sh_type is yet undecided, assume it could be
2691 SHT_PROGBITS/SHT_NOBITS. */
2692 case SHT_NULL:
2693 return FALSE;
2694
2695 /* There shouldn't be section relative relocations
2696 against any other section. */
2697 default:
2698 return TRUE;
2699 }
2700}
2701
43850d5b 2702/* Create a .got section, as well as its additional info field. This
51532845
AO
2703 is almost entirely copied from
2704 elflink.c:_bfd_elf_create_got_section(). */
2705
2706static bfd_boolean
2707_frv_create_got_section (bfd *abfd, struct bfd_link_info *info)
2708{
2709 flagword flags;
2710 asection *s;
2711 struct elf_link_hash_entry *h;
2712 struct bfd_link_hash_entry *bh;
2713 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2714 int ptralign;
43850d5b 2715 int offset;
51532845
AO
2716
2717 /* This function may be called more than once. */
2718 s = bfd_get_section_by_name (abfd, ".got");
2719 if (s != NULL && (s->flags & SEC_LINKER_CREATED) != 0)
2720 return TRUE;
2721
2722 /* Machine specific: although pointers are 32-bits wide, we want the
2723 GOT to be aligned to a 64-bit boundary, such that function
2724 descriptors in it can be accessed with 64-bit loads and
2725 stores. */
2726 ptralign = 3;
2727
2728 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
2729 | SEC_LINKER_CREATED);
2730
2731 s = bfd_make_section (abfd, ".got");
2732 if (s == NULL
2733 || !bfd_set_section_flags (abfd, s, flags)
2734 || !bfd_set_section_alignment (abfd, s, ptralign))
2735 return FALSE;
2736
2737 if (bed->want_got_plt)
2738 {
2739 s = bfd_make_section (abfd, ".got.plt");
2740 if (s == NULL
2741 || !bfd_set_section_flags (abfd, s, flags)
2742 || !bfd_set_section_alignment (abfd, s, ptralign))
2743 return FALSE;
2744 }
2745
2746 if (bed->want_got_sym)
2747 {
2748 /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
2749 (or .got.plt) section. We don't do this in the linker script
2750 because we don't want to define the symbol if we are not creating
2751 a global offset table. */
2752 bh = NULL;
2753 if (!(_bfd_generic_link_add_one_symbol
2754 (info, abfd, "_GLOBAL_OFFSET_TABLE_", BSF_GLOBAL, s,
2755 bed->got_symbol_offset, (const char *) NULL, FALSE,
2756 bed->collect, &bh)))
2757 return FALSE;
2758 h = (struct elf_link_hash_entry *) bh;
2759 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
2760 h->type = STT_OBJECT;
2761
2762 /* Machine-specific: we want the symbol for executables as
2763 well. */
c152c796 2764 if (! bfd_elf_link_record_dynamic_symbol (info, h))
51532845
AO
2765 return FALSE;
2766
2767 elf_hash_table (info)->hgot = h;
2768 }
2769
2770 /* The first bit of the global offset table is the header. */
eea6121a 2771 s->size += bed->got_header_size + bed->got_symbol_offset;
51532845
AO
2772
2773 /* This is the machine-specific part. Create and initialize section
2774 data for the got. */
43850d5b
AO
2775 if (IS_FDPIC (abfd))
2776 {
2777 frvfdpic_got_section (info) = s;
2778 frvfdpic_relocs_info (info) = htab_try_create (1, frvfdpic_relocs_info_hash,
2779 frvfdpic_relocs_info_eq,
2780 (htab_del) NULL);
2781 if (! frvfdpic_relocs_info (info))
2782 return FALSE;
51532845 2783
43850d5b
AO
2784 s = bfd_make_section (abfd, ".rel.got");
2785 if (s == NULL
2786 || ! bfd_set_section_flags (abfd, s, (flags | SEC_READONLY))
2787 || ! bfd_set_section_alignment (abfd, s, 2))
2788 return FALSE;
51532845 2789
43850d5b 2790 frvfdpic_gotrel_section (info) = s;
51532845 2791
43850d5b
AO
2792 /* Machine-specific. */
2793 s = bfd_make_section (abfd, ".rofixup");
2794 if (s == NULL
2795 || ! bfd_set_section_flags (abfd, s, (flags | SEC_READONLY))
2796 || ! bfd_set_section_alignment (abfd, s, 2))
2797 return FALSE;
51532845 2798
43850d5b
AO
2799 frvfdpic_gotfixup_section (info) = s;
2800 offset = -2048;
2801 flags = BSF_GLOBAL;
2802 }
2803 else
2804 {
2805 offset = 2048;
2806 flags = BSF_GLOBAL | BSF_WEAK;
2807 }
51532845 2808
43850d5b
AO
2809 /* Define _gp in .rofixup, for FDPIC, or .got otherwise. If it
2810 turns out that we're linking with a different linker script, the
2811 linker script will override it. */
51532845
AO
2812 bh = NULL;
2813 if (!(_bfd_generic_link_add_one_symbol
43850d5b 2814 (info, abfd, "_gp", flags, s, offset, (const char *) NULL, FALSE,
51532845
AO
2815 bed->collect, &bh)))
2816 return FALSE;
2817 h = (struct elf_link_hash_entry *) bh;
2818 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
2819 h->type = STT_OBJECT;
2820
2821 /* Machine-specific: we want the symbol for executables as well. */
43850d5b 2822 if (IS_FDPIC (abfd) && ! bfd_elf_link_record_dynamic_symbol (info, h))
51532845 2823 return FALSE;
43850d5b 2824
51532845
AO
2825 return TRUE;
2826}
2827
2828/* Make sure the got and plt sections exist, and that our pointers in
2829 the link hash table point to them. */
2830
2831static bfd_boolean
43850d5b 2832elf32_frvfdpic_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
51532845
AO
2833{
2834 /* This is mostly copied from
2835 elflink.c:_bfd_elf_create_dynamic_sections(). */
2836 flagword flags, pltflags;
2837 asection *s;
2838 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2839
2840 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
2841 .rel[a].bss sections. */
2842
2843 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
2844 | SEC_LINKER_CREATED);
2845
2846 pltflags = flags;
2847 pltflags |= SEC_CODE;
2848 if (bed->plt_not_loaded)
2849 pltflags &= ~ (SEC_CODE | SEC_LOAD | SEC_HAS_CONTENTS);
2850 if (bed->plt_readonly)
2851 pltflags |= SEC_READONLY;
2852
2853 s = bfd_make_section (abfd, ".plt");
2854 if (s == NULL
2855 || ! bfd_set_section_flags (abfd, s, pltflags)
2856 || ! bfd_set_section_alignment (abfd, s, bed->plt_alignment))
2857 return FALSE;
2858 /* FRV-specific: remember it. */
43850d5b 2859 frvfdpic_plt_section (info) = s;
51532845
AO
2860
2861 if (bed->want_plt_sym)
2862 {
2863 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
2864 .plt section. */
2865 struct elf_link_hash_entry *h;
2866 struct bfd_link_hash_entry *bh = NULL;
2867
2868 if (! (_bfd_generic_link_add_one_symbol
2869 (info, abfd, "_PROCEDURE_LINKAGE_TABLE_", BSF_GLOBAL, s, 0, NULL,
2870 FALSE, get_elf_backend_data (abfd)->collect, &bh)))
2871 return FALSE;
2872 h = (struct elf_link_hash_entry *) bh;
2873 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
2874 h->type = STT_OBJECT;
2875
2876 if (! info->executable
c152c796 2877 && ! bfd_elf_link_record_dynamic_symbol (info, h))
51532845
AO
2878 return FALSE;
2879 }
2880
2881 /* FRV-specific: we want rel relocations for the plt. */
2882 s = bfd_make_section (abfd, ".rel.plt");
2883 if (s == NULL
2884 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
2885 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
2886 return FALSE;
2887 /* FRV-specific: remember it. */
43850d5b 2888 frvfdpic_pltrel_section (info) = s;
51532845
AO
2889
2890 /* FRV-specific: we want to create the GOT in the FRV way. */
2891 if (! _frv_create_got_section (abfd, info))
2892 return FALSE;
2893
2894 /* FRV-specific: make sure we created everything we wanted. */
43850d5b
AO
2895 BFD_ASSERT (frvfdpic_got_section (info) && frvfdpic_gotrel_section (info)
2896 && frvfdpic_gotfixup_section (info)
2897 && frvfdpic_plt_section (info)
2898 && frvfdpic_pltrel_section (info));
51532845
AO
2899
2900 if (bed->want_dynbss)
2901 {
2902 /* The .dynbss section is a place to put symbols which are defined
2903 by dynamic objects, are referenced by regular objects, and are
2904 not functions. We must allocate space for them in the process
2905 image and use a R_*_COPY reloc to tell the dynamic linker to
2906 initialize them at run time. The linker script puts the .dynbss
2907 section into the .bss section of the final image. */
2908 s = bfd_make_section (abfd, ".dynbss");
2909 if (s == NULL
2910 || ! bfd_set_section_flags (abfd, s, SEC_ALLOC | SEC_LINKER_CREATED))
2911 return FALSE;
2912
2913 /* The .rel[a].bss section holds copy relocs. This section is not
2914 normally needed. We need to create it here, though, so that the
2915 linker will map it to an output section. We can't just create it
2916 only if we need it, because we will not know whether we need it
2917 until we have seen all the input files, and the first time the
2918 main linker code calls BFD after examining all the input files
2919 (size_dynamic_sections) the input sections have already been
2920 mapped to the output sections. If the section turns out not to
2921 be needed, we can discard it later. We will never need this
2922 section when generating a shared object, since they do not use
2923 copy relocs. */
2924 if (! info->shared)
2925 {
2926 s = bfd_make_section (abfd,
2927 (bed->default_use_rela_p
2928 ? ".rela.bss" : ".rel.bss"));
2929 if (s == NULL
2930 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
2931 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
2932 return FALSE;
2933 }
2934 }
2935
2936 return TRUE;
2937}
2938
2939/* The name of the dynamic interpreter. This is put in the .interp
2940 section. */
2941
2942#define ELF_DYNAMIC_INTERPRETER "/lib/ld.so.1"
2943
2944#define DEFAULT_STACK_SIZE 0x20000
2945
2946/* This structure is used to collect the number of entries present in
2947 each addressable range of the got. */
43850d5b 2948struct _frvfdpic_dynamic_got_info
51532845
AO
2949{
2950 /* Several bits of information about the current link. */
2951 struct bfd_link_info *info;
2952 /* Total size needed for GOT entries within the 12-, 16- or 32-bit
2953 ranges. */
2954 bfd_vma got12, gotlos, gothilo;
2955 /* Total size needed for function descriptor entries within the 12-,
2956 16- or 32-bit ranges. */
2957 bfd_vma fd12, fdlos, fdhilo;
2958 /* Total size needed function descriptor entries referenced in PLT
2959 entries, that would be profitable to place in offsets close to
2960 the PIC register. */
2961 bfd_vma fdplt;
2962 /* Total size needed by lazy PLT entries. */
2963 bfd_vma lzplt;
2964 /* Number of relocations carried over from input object files. */
2965 unsigned long relocs;
2966 /* Number of fixups introduced by relocations in input object files. */
2967 unsigned long fixups;
2968};
2969
2970/* Compute the total GOT size required by each symbol in each range.
2971 Symbols may require up to 4 words in the GOT: an entry pointing to
2972 the symbol, an entry pointing to its function descriptor, and a
2973 private function descriptors taking two words. */
2974
2975static int
43850d5b 2976_frvfdpic_count_got_plt_entries (void **entryp, void *dinfo_)
51532845 2977{
43850d5b
AO
2978 struct frvfdpic_relocs_info *entry = *entryp;
2979 struct _frvfdpic_dynamic_got_info *dinfo = dinfo_;
3b712a1a 2980 unsigned relocs = 0, fixups = 0;
51532845
AO
2981
2982 /* Allocate space for a GOT entry pointing to the symbol. */
2983 if (entry->got12)
2984 dinfo->got12 += 4;
2985 else if (entry->gotlos)
2986 dinfo->gotlos += 4;
2987 else if (entry->gothilo)
2988 dinfo->gothilo += 4;
2989 else
2990 entry->relocs32--;
2991 entry->relocs32++;
2992
2993 /* Allocate space for a GOT entry pointing to the function
2994 descriptor. */
2995 if (entry->fdgot12)
2996 dinfo->got12 += 4;
2997 else if (entry->fdgotlos)
2998 dinfo->gotlos += 4;
2999 else if (entry->fdgothilo)
3000 dinfo->gothilo += 4;
3001 else
3002 entry->relocsfd--;
3003 entry->relocsfd++;
3004
3005 /* Decide whether we need a PLT entry, a function descriptor in the
3006 GOT, and a lazy PLT entry for this symbol. */
3007 entry->plt = entry->call
43850d5b 3008 && entry->symndx == -1 && ! FRVFDPIC_SYM_LOCAL (dinfo->info, entry->d.h)
51532845
AO
3009 && elf_hash_table (dinfo->info)->dynamic_sections_created;
3010 entry->privfd = entry->plt
3011 || entry->fdgoff12 || entry->fdgofflos || entry->fdgoffhilo
3012 || ((entry->fd || entry->fdgot12 || entry->fdgotlos || entry->fdgothilo)
3013 && (entry->symndx != -1
43850d5b 3014 || FRVFDPIC_FUNCDESC_LOCAL (dinfo->info, entry->d.h)));
51532845 3015 entry->lazyplt = entry->privfd
43850d5b 3016 && entry->symndx == -1 && ! FRVFDPIC_SYM_LOCAL (dinfo->info, entry->d.h)
51532845
AO
3017 && ! (dinfo->info->flags & DF_BIND_NOW)
3018 && elf_hash_table (dinfo->info)->dynamic_sections_created;
3019
3020 /* Allocate space for a function descriptor. */
3021 if (entry->fdgoff12)
3022 dinfo->fd12 += 8;
3023 else if (entry->fdgofflos)
3024 dinfo->fdlos += 8;
3025 else if (entry->privfd && entry->plt)
3026 dinfo->fdplt += 8;
3027 else if (entry->privfd)
3028 dinfo->fdhilo += 8;
3029 else
3030 entry->relocsfdv--;
3031 entry->relocsfdv++;
3032
3033 if (entry->lazyplt)
3034 dinfo->lzplt += 8;
3035
3036 if (!dinfo->info->executable || dinfo->info->pie)
3b712a1a 3037 relocs = entry->relocs32 + entry->relocsfd + entry->relocsfdv;
51532845
AO
3038 else
3039 {
43850d5b 3040 if (entry->symndx != -1 || FRVFDPIC_SYM_LOCAL (dinfo->info, entry->d.h))
51532845
AO
3041 {
3042 if (entry->symndx != -1
3b712a1a
AO
3043 || entry->d.h->root.type != bfd_link_hash_undefweak)
3044 fixups += entry->relocs32 + 2 * entry->relocsfdv;
51532845
AO
3045 }
3046 else
3b712a1a
AO
3047 relocs += entry->relocs32 + entry->relocsfdv;
3048
43850d5b
AO
3049 if (entry->symndx != -1
3050 || FRVFDPIC_FUNCDESC_LOCAL (dinfo->info, entry->d.h))
51532845
AO
3051 {
3052 if (entry->symndx != -1
3053 || entry->d.h->root.type != bfd_link_hash_undefweak)
3b712a1a 3054 fixups += entry->relocsfd;
51532845
AO
3055 }
3056 else
3b712a1a 3057 relocs += entry->relocsfd;
51532845
AO
3058 }
3059
3b712a1a
AO
3060 entry->dynrelocs += relocs;
3061 entry->fixups += fixups;
3062 dinfo->relocs += relocs;
3063 dinfo->fixups += fixups;
3064
51532845
AO
3065 return 1;
3066}
3067
3068/* This structure is used to assign offsets to got entries, function
3069 descriptors, plt entries and lazy plt entries. */
3070
43850d5b 3071struct _frvfdpic_dynamic_got_plt_info
51532845 3072{
43850d5b
AO
3073 /* Summary information collected with _frvfdpic_count_got_plt_entries. */
3074 struct _frvfdpic_dynamic_got_info g;
51532845
AO
3075
3076 /* For each addressable range, we record a MAX (positive) and MIN
3077 (negative) value. CUR is used to assign got entries, and it's
3078 incremented from an initial positive value to MAX, then from MIN
3079 to FDCUR (unless FDCUR wraps around first). FDCUR is used to
3080 assign function descriptors, and it's decreased from an initial
3081 non-positive value to MIN, then from MAX down to CUR (unless CUR
3082 wraps around first). All of MIN, MAX, CUR and FDCUR always point
3083 to even words. ODD, if non-zero, indicates an odd word to be
3084 used for the next got entry, otherwise CUR is used and
3085 incremented by a pair of words, wrapping around when it reaches
3086 MAX. FDCUR is decremented (and wrapped) before the next function
3087 descriptor is chosen. FDPLT indicates the number of remaining
3088 slots that can be used for function descriptors used only by PLT
3089 entries. */
43850d5b 3090 struct _frvfdpic_dynamic_got_alloc_data
51532845
AO
3091 {
3092 bfd_signed_vma max, cur, odd, fdcur, min;
3093 bfd_vma fdplt;
3094 } got12, gotlos, gothilo;
3095};
3096
3097/* Determine the positive and negative ranges to be used by each
3098 offset range in the GOT. FDCUR and CUR, that must be aligned to a
3099 double-word boundary, are the minimum (negative) and maximum
3100 (positive) GOT offsets already used by previous ranges, except for
3101 an ODD entry that may have been left behind. GOT and FD indicate
3102 the size of GOT entries and function descriptors that must be
3103 placed within the range from -WRAP to WRAP. If there's room left,
3104 up to FDPLT bytes should be reserved for additional function
3105 descriptors. */
3106
3107inline static bfd_signed_vma
43850d5b
AO
3108_frvfdpic_compute_got_alloc_data (struct _frvfdpic_dynamic_got_alloc_data *gad,
3109 bfd_signed_vma fdcur,
3110 bfd_signed_vma odd,
3111 bfd_signed_vma cur,
3112 bfd_vma got,
3113 bfd_vma fd,
3114 bfd_vma fdplt,
3115 bfd_vma wrap)
51532845
AO
3116{
3117 bfd_signed_vma wrapmin = -wrap;
3118
3119 /* Start at the given initial points. */
3120 gad->fdcur = fdcur;
3121 gad->cur = cur;
3122
3123 /* If we had an incoming odd word and we have any got entries that
3124 are going to use it, consume it, otherwise leave gad->odd at
3125 zero. We might force gad->odd to zero and return the incoming
3126 odd such that it is used by the next range, but then GOT entries
3127 might appear to be out of order and we wouldn't be able to
3128 shorten the GOT by one word if it turns out to end with an
3129 unpaired GOT entry. */
3130 if (odd && got)
3131 {
3132 gad->odd = odd;
3133 got -= 4;
3134 odd = 0;
3135 }
3136 else
3137 gad->odd = 0;
3138
3139 /* If we're left with an unpaired GOT entry, compute its location
3140 such that we can return it. Otherwise, if got doesn't require an
3141 odd number of words here, either odd was already zero in the
3142 block above, or it was set to zero because got was non-zero, or
3143 got was already zero. In the latter case, we want the value of
3144 odd to carry over to the return statement, so we don't want to
3145 reset odd unless the condition below is true. */
3146 if (got & 4)
3147 {
3148 odd = cur + got;
3149 got += 4;
3150 }
3151
3152 /* Compute the tentative boundaries of this range. */
3153 gad->max = cur + got;
3154 gad->min = fdcur - fd;
3155 gad->fdplt = 0;
3156
3157 /* If function descriptors took too much space, wrap some of them
3158 around. */
3159 if (gad->min < wrapmin)
3160 {
3161 gad->max += wrapmin - gad->min;
3162 gad->min = wrapmin;
3163 }
3164 /* If there is space left and we have function descriptors
3165 referenced in PLT entries that could take advantage of shorter
3166 offsets, place them here. */
3167 else if (fdplt && gad->min > wrapmin)
3168 {
3169 bfd_vma fds;
3170 if ((bfd_vma) (gad->min - wrapmin) < fdplt)
3171 fds = gad->min - wrapmin;
3172 else
3173 fds = fdplt;
3174
3175 fdplt -= fds;
3176 gad->min -= fds;
3177 gad->fdplt += fds;
3178 }
3179
3180 /* If GOT entries took too much space, wrap some of them around.
3181 This may well cause gad->min to become lower than wrapmin. This
3182 will cause a relocation overflow later on, so we don't have to
3183 report it here . */
3184 if ((bfd_vma) gad->max > wrap)
3185 {
3186 gad->min -= gad->max - wrap;
3187 gad->max = wrap;
3188 }
3189 /* If there is more space left, try to place some more function
3190 descriptors for PLT entries. */
3191 else if (fdplt && (bfd_vma) gad->max < wrap)
3192 {
3193 bfd_vma fds;
3194 if ((bfd_vma) (wrap - gad->max) < fdplt)
3195 fds = wrap - gad->max;
3196 else
3197 fds = fdplt;
3198
3199 fdplt -= fds;
3200 gad->max += fds;
3201 gad->fdplt += fds;
3202 }
3203
3204 /* If odd was initially computed as an offset past the wrap point,
3205 wrap it around. */
3206 if (odd > gad->max)
3207 odd = gad->min + odd - gad->max;
3208
43850d5b 3209 /* _frvfdpic_get_got_entry() below will always wrap gad->cur if needed
51532845
AO
3210 before returning, so do it here too. This guarantees that,
3211 should cur and fdcur meet at the wrap point, they'll both be
3212 equal to min. */
3213 if (gad->cur == gad->max)
3214 gad->cur = gad->min;
3215
3216 return odd;
3217}
3218
3219/* Compute the location of the next GOT entry, given the allocation
3220 data for a range. */
3221
3222inline static bfd_signed_vma
43850d5b 3223_frvfdpic_get_got_entry (struct _frvfdpic_dynamic_got_alloc_data *gad)
51532845
AO
3224{
3225 bfd_signed_vma ret;
3226
3227 if (gad->odd)
3228 {
3229 /* If there was an odd word left behind, use it. */
3230 ret = gad->odd;
3231 gad->odd = 0;
3232 }
3233 else
3234 {
3235 /* Otherwise, use the word pointed to by cur, reserve the next
3236 as an odd word, and skip to the next pair of words, possibly
3237 wrapping around. */
3238 ret = gad->cur;
3239 gad->odd = gad->cur + 4;
3240 gad->cur += 8;
3241 if (gad->cur == gad->max)
3242 gad->cur = gad->min;
3243 }
3244
3245 return ret;
3246}
3247
3248/* Compute the location of the next function descriptor entry in the
3249 GOT, given the allocation data for a range. */
3250
3251inline static bfd_signed_vma
43850d5b 3252_frvfdpic_get_fd_entry (struct _frvfdpic_dynamic_got_alloc_data *gad)
51532845
AO
3253{
3254 /* If we're at the bottom, wrap around, and only then allocate the
3255 next pair of words. */
3256 if (gad->fdcur == gad->min)
3257 gad->fdcur = gad->max;
3258 return gad->fdcur -= 8;
3259}
3260
3261/* Assign GOT offsets for every GOT entry and function descriptor.
3262 Doing everything in a single pass is tricky. */
3263
3264static int
43850d5b 3265_frvfdpic_assign_got_entries (void **entryp, void *info_)
51532845 3266{
43850d5b
AO
3267 struct frvfdpic_relocs_info *entry = *entryp;
3268 struct _frvfdpic_dynamic_got_plt_info *dinfo = info_;
51532845
AO
3269
3270 if (entry->got12)
43850d5b 3271 entry->got_entry = _frvfdpic_get_got_entry (&dinfo->got12);
51532845 3272 else if (entry->gotlos)
43850d5b 3273 entry->got_entry = _frvfdpic_get_got_entry (&dinfo->gotlos);
51532845 3274 else if (entry->gothilo)
43850d5b 3275 entry->got_entry = _frvfdpic_get_got_entry (&dinfo->gothilo);
51532845
AO
3276
3277 if (entry->fdgot12)
43850d5b 3278 entry->fdgot_entry = _frvfdpic_get_got_entry (&dinfo->got12);
51532845 3279 else if (entry->fdgotlos)
43850d5b 3280 entry->fdgot_entry = _frvfdpic_get_got_entry (&dinfo->gotlos);
51532845 3281 else if (entry->fdgothilo)
43850d5b 3282 entry->fdgot_entry = _frvfdpic_get_got_entry (&dinfo->gothilo);
51532845
AO
3283
3284 if (entry->fdgoff12)
43850d5b 3285 entry->fd_entry = _frvfdpic_get_fd_entry (&dinfo->got12);
51532845
AO
3286 else if (entry->plt && dinfo->got12.fdplt)
3287 {
3288 dinfo->got12.fdplt -= 8;
43850d5b 3289 entry->fd_entry = _frvfdpic_get_fd_entry (&dinfo->got12);
51532845
AO
3290 }
3291 else if (entry->fdgofflos)
43850d5b 3292 entry->fd_entry = _frvfdpic_get_fd_entry (&dinfo->gotlos);
51532845
AO
3293 else if (entry->plt && dinfo->gotlos.fdplt)
3294 {
3295 dinfo->gotlos.fdplt -= 8;
43850d5b 3296 entry->fd_entry = _frvfdpic_get_fd_entry (&dinfo->gotlos);
51532845
AO
3297 }
3298 else if (entry->plt)
3299 {
3300 dinfo->gothilo.fdplt -= 8;
43850d5b 3301 entry->fd_entry = _frvfdpic_get_fd_entry (&dinfo->gothilo);
51532845
AO
3302 }
3303 else if (entry->privfd)
43850d5b 3304 entry->fd_entry = _frvfdpic_get_fd_entry (&dinfo->gothilo);
51532845
AO
3305
3306 return 1;
3307}
3308
3309/* Assign GOT offsets to private function descriptors used by PLT
3310 entries (or referenced by 32-bit offsets), as well as PLT entries
3311 and lazy PLT entries. */
3312
3313static int
43850d5b 3314_frvfdpic_assign_plt_entries (void **entryp, void *info_)
51532845 3315{
43850d5b
AO
3316 struct frvfdpic_relocs_info *entry = *entryp;
3317 struct _frvfdpic_dynamic_got_plt_info *dinfo = info_;
51532845
AO
3318
3319 /* If this symbol requires a local function descriptor, allocate
3320 one. */
3321 if (entry->privfd && entry->fd_entry == 0)
3322 {
3323 if (dinfo->got12.fdplt)
3324 {
43850d5b 3325 entry->fd_entry = _frvfdpic_get_fd_entry (&dinfo->got12);
51532845
AO
3326 dinfo->got12.fdplt -= 8;
3327 }
3328 else if (dinfo->gotlos.fdplt)
3329 {
43850d5b 3330 entry->fd_entry = _frvfdpic_get_fd_entry (&dinfo->gotlos);
51532845
AO
3331 dinfo->gotlos.fdplt -= 8;
3332 }
3333 else
3334 {
3335 BFD_ASSERT (dinfo->gothilo.fdplt)
43850d5b 3336 entry->fd_entry = _frvfdpic_get_fd_entry (&dinfo->gothilo);
51532845
AO
3337 dinfo->gothilo.fdplt -= 8;
3338 }
3339 }
3340
3341 if (entry->plt)
3342 {
3343 int size;
3344
3345 /* We use the section's raw size to mark the location of the
3346 next PLT entry. */
eea6121a 3347 entry->plt_entry = frvfdpic_plt_section (dinfo->g.info)->size;
51532845
AO
3348
3349 /* Figure out the length of this PLT entry based on the
3350 addressing mode we need to reach the function descriptor. */
3351 BFD_ASSERT (entry->fd_entry);
3352 if (entry->fd_entry >= -(1 << (12 - 1))
3353 && entry->fd_entry < (1 << (12 - 1)))
3354 size = 8;
3355 else if (entry->fd_entry >= -(1 << (16 - 1))
3356 && entry->fd_entry < (1 << (16 - 1)))
3357 size = 12;
3358 else
3359 size = 16;
3360
eea6121a 3361 frvfdpic_plt_section (dinfo->g.info)->size += size;
51532845
AO
3362 }
3363
3364 if (entry->lazyplt)
3365 {
3366 entry->lzplt_entry = dinfo->g.lzplt;
3367 dinfo->g.lzplt += 8;
3368 /* If this entry is the one that gets the resolver stub, account
3369 for the additional instruction. */
43850d5b
AO
3370 if (entry->lzplt_entry % FRVFDPIC_LZPLT_BLOCK_SIZE
3371 == FRVFDPIC_LZPLT_RESOLV_LOC)
51532845
AO
3372 dinfo->g.lzplt += 4;
3373 }
3374
3375 return 1;
3376}
3377
3378/* Follow indirect and warning hash entries so that each got entry
3379 points to the final symbol definition. P must point to a pointer
3380 to the hash table we're traversing. Since this traversal may
3381 modify the hash table, we set this pointer to NULL to indicate
3382 we've made a potentially-destructive change to the hash table, so
3383 the traversal must be restarted. */
3384static int
43850d5b 3385_frvfdpic_resolve_final_relocs_info (void **entryp, void *p)
51532845 3386{
43850d5b 3387 struct frvfdpic_relocs_info *entry = *entryp;
51532845
AO
3388 htab_t *htab = p;
3389
3390 if (entry->symndx == -1)
3391 {
3392 struct elf_link_hash_entry *h = entry->d.h;
43850d5b 3393 struct frvfdpic_relocs_info *oentry;
51532845
AO
3394
3395 while (h->root.type == bfd_link_hash_indirect
3396 || h->root.type == bfd_link_hash_warning)
3397 h = (struct elf_link_hash_entry *)h->root.u.i.link;
3398
3399 if (entry->d.h == h)
3400 return 1;
3401
43850d5b
AO
3402 oentry = frvfdpic_relocs_info_for_global (*htab, 0, h, entry->addend,
3403 NO_INSERT);
3b712a1a
AO
3404
3405 if (oentry)
3406 {
3407 /* Merge the two entries. */
43850d5b 3408 frvfdpic_pic_merge_early_relocs_info (oentry, entry);
3b712a1a
AO
3409 htab_clear_slot (*htab, entryp);
3410 return 1;
3411 }
3412
51532845
AO
3413 entry->d.h = h;
3414
3415 /* If we can't find this entry with the new bfd hash, re-insert
3416 it, and get the traversal restarted. */
3417 if (! htab_find (*htab, entry))
3418 {
3419 htab_clear_slot (*htab, entryp);
3420 entryp = htab_find_slot (*htab, entry, INSERT);
3421 if (! *entryp)
3422 *entryp = entry;
3423 /* Abort the traversal, since the whole table may have
3424 moved, and leave it up to the parent to restart the
3425 process. */
3426 *(htab_t *)p = NULL;
3427 return 0;
3428 }
3429 }
3430
3431 return 1;
3432}
3433
3434/* Set the sizes of the dynamic sections. */
3435
3436static bfd_boolean
43850d5b
AO
3437elf32_frvfdpic_size_dynamic_sections (bfd *output_bfd,
3438 struct bfd_link_info *info)
51532845
AO
3439{
3440 bfd *dynobj;
3441 asection *s;
43850d5b 3442 struct _frvfdpic_dynamic_got_plt_info gpinfo;
51532845
AO
3443 bfd_signed_vma odd;
3444 bfd_vma limit;
3445
3446 dynobj = elf_hash_table (info)->dynobj;
3447 BFD_ASSERT (dynobj != NULL);
3448
3449 if (elf_hash_table (info)->dynamic_sections_created)
3450 {
3451 /* Set the contents of the .interp section to the interpreter. */
3452 if (info->executable)
3453 {
3454 s = bfd_get_section_by_name (dynobj, ".interp");
3455 BFD_ASSERT (s != NULL);
eea6121a 3456 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
51532845
AO
3457 s->contents = (bfd_byte *) ELF_DYNAMIC_INTERPRETER;
3458 }
3459 }
3460
3461 memset (&gpinfo, 0, sizeof (gpinfo));
3462 gpinfo.g.info = info;
3463
3464 for (;;)
3465 {
43850d5b 3466 htab_t relocs = frvfdpic_relocs_info (info);
51532845 3467
43850d5b 3468 htab_traverse (relocs, _frvfdpic_resolve_final_relocs_info, &relocs);
51532845 3469
43850d5b 3470 if (relocs == frvfdpic_relocs_info (info))
51532845
AO
3471 break;
3472 }
3473
43850d5b 3474 htab_traverse (frvfdpic_relocs_info (info), _frvfdpic_count_got_plt_entries,
51532845
AO
3475 &gpinfo.g);
3476
3477 odd = 12;
3478 /* Compute the total size taken by entries in the 12-bit and 16-bit
3479 ranges, to tell how many PLT function descriptors we can bring
3480 into the 12-bit range without causing the 16-bit range to
3481 overflow. */
3482 limit = odd + gpinfo.g.got12 + gpinfo.g.gotlos
3483 + gpinfo.g.fd12 + gpinfo.g.fdlos;
3484 if (limit < (bfd_vma)1 << 16)
3485 limit = ((bfd_vma)1 << 16) - limit;
3486 else
3487 limit = 0;
3488 if (gpinfo.g.fdplt < limit)
3489 limit = gpinfo.g.fdplt;
3490
3491 /* Determine the ranges of GOT offsets that we can use for each
3492 range of addressing modes. */
43850d5b
AO
3493 odd = _frvfdpic_compute_got_alloc_data (&gpinfo.got12,
3494 0,
3495 odd,
3496 16,
3497 gpinfo.g.got12,
3498 gpinfo.g.fd12,
3499 limit,
3500 (bfd_vma)1 << (12-1));
3501 odd = _frvfdpic_compute_got_alloc_data (&gpinfo.gotlos,
3502 gpinfo.got12.min,
3503 odd,
3504 gpinfo.got12.max,
3505 gpinfo.g.gotlos,
3506 gpinfo.g.fdlos,
3507 gpinfo.g.fdplt - gpinfo.got12.fdplt,
3508 (bfd_vma)1 << (16-1));
3509 odd = _frvfdpic_compute_got_alloc_data (&gpinfo.gothilo,
3510 gpinfo.gotlos.min,
3511 odd,
3512 gpinfo.gotlos.max,
3513 gpinfo.g.gothilo,
3514 gpinfo.g.fdhilo,
3515 gpinfo.g.fdplt - gpinfo.got12.fdplt
3516 - gpinfo.gotlos.fdplt,
3517 (bfd_vma)1 << (32-1));
51532845
AO
3518
3519 /* Now assign (most) GOT offsets. */
43850d5b
AO
3520 htab_traverse (frvfdpic_relocs_info (info), _frvfdpic_assign_got_entries,
3521 &gpinfo);
51532845 3522
eea6121a 3523 frvfdpic_got_section (info)->size = gpinfo.gothilo.max
43850d5b 3524 - gpinfo.gothilo.min
51532845
AO
3525 /* If an odd word is the last word of the GOT, we don't need this
3526 word to be part of the GOT. */
3527 - (odd + 4 == gpinfo.gothilo.max ? 4 : 0);
eea6121a 3528 if (frvfdpic_got_section (info)->size == 0)
43850d5b 3529 frvfdpic_got_section (info)->flags |= SEC_EXCLUDE;
eea6121a 3530 else if (frvfdpic_got_section (info)->size == 12
51532845
AO
3531 && ! elf_hash_table (info)->dynamic_sections_created)
3532 {
43850d5b 3533 frvfdpic_got_section (info)->flags |= SEC_EXCLUDE;
eea6121a 3534 frvfdpic_got_section (info)->size = 0;
51532845
AO
3535 }
3536 else
3537 {
43850d5b
AO
3538 frvfdpic_got_section (info)->contents =
3539 (bfd_byte *) bfd_zalloc (dynobj,
eea6121a 3540 frvfdpic_got_section (info)->size);
43850d5b 3541 if (frvfdpic_got_section (info)->contents == NULL)
51532845
AO
3542 return FALSE;
3543 }
3544
3545 if (elf_hash_table (info)->dynamic_sections_created)
3546 /* Subtract the number of lzplt entries, since those will generate
3547 relocations in the pltrel section. */
eea6121a 3548 frvfdpic_gotrel_section (info)->size =
51532845
AO
3549 (gpinfo.g.relocs - gpinfo.g.lzplt / 8)
3550 * get_elf_backend_data (output_bfd)->s->sizeof_rel;
3551 else
3552 BFD_ASSERT (gpinfo.g.relocs == 0);
eea6121a 3553 if (frvfdpic_gotrel_section (info)->size == 0)
43850d5b 3554 frvfdpic_gotrel_section (info)->flags |= SEC_EXCLUDE;
51532845
AO
3555 else
3556 {
43850d5b
AO
3557 frvfdpic_gotrel_section (info)->contents =
3558 (bfd_byte *) bfd_zalloc (dynobj,
eea6121a 3559 frvfdpic_gotrel_section (info)->size);
43850d5b 3560 if (frvfdpic_gotrel_section (info)->contents == NULL)
51532845
AO
3561 return FALSE;
3562 }
3563
eea6121a
AM
3564 frvfdpic_gotfixup_section (info)->size = (gpinfo.g.fixups + 1) * 4;
3565 if (frvfdpic_gotfixup_section (info)->size == 0)
43850d5b 3566 frvfdpic_gotfixup_section (info)->flags |= SEC_EXCLUDE;
51532845
AO
3567 else
3568 {
43850d5b 3569 frvfdpic_gotfixup_section (info)->contents =
51532845 3570 (bfd_byte *) bfd_zalloc (dynobj,
eea6121a 3571 frvfdpic_gotfixup_section (info)->size);
43850d5b 3572 if (frvfdpic_gotfixup_section (info)->contents == NULL)
51532845
AO
3573 return FALSE;
3574 }
3575
3576 if (elf_hash_table (info)->dynamic_sections_created)
3577 {
eea6121a 3578 frvfdpic_pltrel_section (info)->size =
51532845 3579 gpinfo.g.lzplt / 8 * get_elf_backend_data (output_bfd)->s->sizeof_rel;
eea6121a 3580 if (frvfdpic_pltrel_section (info)->size == 0)
43850d5b 3581 frvfdpic_pltrel_section (info)->flags |= SEC_EXCLUDE;
51532845
AO
3582 else
3583 {
43850d5b 3584 frvfdpic_pltrel_section (info)->contents =
51532845 3585 (bfd_byte *) bfd_zalloc (dynobj,
eea6121a 3586 frvfdpic_pltrel_section (info)->size);
43850d5b 3587 if (frvfdpic_pltrel_section (info)->contents == NULL)
51532845
AO
3588 return FALSE;
3589 }
3590 }
3591
3592 /* Add 4 bytes for every block of at most 65535 lazy PLT entries,
3593 such that there's room for the additional instruction needed to
43850d5b
AO
3594 call the resolver. Since _frvfdpic_assign_got_entries didn't
3595 account for them, our block size is 4 bytes smaller than the real
3596 block size. */
51532845
AO
3597 if (elf_hash_table (info)->dynamic_sections_created)
3598 {
eea6121a 3599 frvfdpic_plt_section (info)->size = gpinfo.g.lzplt
43850d5b
AO
3600 + ((gpinfo.g.lzplt + (FRVFDPIC_LZPLT_BLOCK_SIZE - 4) - 8)
3601 / (FRVFDPIC_LZPLT_BLOCK_SIZE - 4) * 4);
51532845 3602 }
b34976b6 3603
43850d5b 3604 /* Reset it, such that _frvfdpic_assign_plt_entries() can use it to
51532845
AO
3605 actually assign lazy PLT entries addresses. */
3606 gpinfo.g.lzplt = 0;
b34976b6 3607
51532845
AO
3608 /* Save information that we're going to need to generate GOT and PLT
3609 entries. */
43850d5b 3610 frvfdpic_got_initial_offset (info) = -gpinfo.gothilo.min;
b34976b6 3611
51532845
AO
3612 if (get_elf_backend_data (output_bfd)->want_got_sym)
3613 elf_hash_table (info)->hgot->root.u.def.value
43850d5b 3614 += frvfdpic_got_initial_offset (info);
b34976b6 3615
51532845 3616 if (elf_hash_table (info)->dynamic_sections_created)
43850d5b 3617 frvfdpic_plt_initial_offset (info) =
eea6121a 3618 frvfdpic_plt_section (info)->size;
b34976b6 3619
43850d5b
AO
3620 htab_traverse (frvfdpic_relocs_info (info), _frvfdpic_assign_plt_entries,
3621 &gpinfo);
51532845
AO
3622
3623 /* Allocate the PLT section contents only after
43850d5b 3624 _frvfdpic_assign_plt_entries has a chance to add the size of the
51532845
AO
3625 non-lazy PLT entries. */
3626 if (elf_hash_table (info)->dynamic_sections_created)
3627 {
eea6121a 3628 if (frvfdpic_plt_section (info)->size == 0)
43850d5b 3629 frvfdpic_plt_section (info)->flags |= SEC_EXCLUDE;
4e5ba5b7
DB
3630 else
3631 {
43850d5b
AO
3632 frvfdpic_plt_section (info)->contents =
3633 (bfd_byte *) bfd_zalloc (dynobj,
eea6121a 3634 frvfdpic_plt_section (info)->size);
43850d5b 3635 if (frvfdpic_plt_section (info)->contents == NULL)
51532845
AO
3636 return FALSE;
3637 }
3638 }
b34976b6 3639
51532845
AO
3640 if (elf_hash_table (info)->dynamic_sections_created)
3641 {
eea6121a 3642 if (frvfdpic_got_section (info)->size)
5a580b3a 3643 if (!_bfd_elf_add_dynamic_entry (info, DT_PLTGOT, 0))
51532845
AO
3644 return FALSE;
3645
eea6121a 3646 if (frvfdpic_pltrel_section (info)->size)
5a580b3a
AM
3647 if (!_bfd_elf_add_dynamic_entry (info, DT_PLTRELSZ, 0)
3648 || !_bfd_elf_add_dynamic_entry (info, DT_PLTREL, DT_REL)
3649 || !_bfd_elf_add_dynamic_entry (info, DT_JMPREL, 0))
51532845
AO
3650 return FALSE;
3651
eea6121a 3652 if (frvfdpic_gotrel_section (info)->size)
5a580b3a
AM
3653 if (!_bfd_elf_add_dynamic_entry (info, DT_REL, 0)
3654 || !_bfd_elf_add_dynamic_entry (info, DT_RELSZ, 0)
3655 || !_bfd_elf_add_dynamic_entry (info, DT_RELENT,
3656 sizeof (Elf32_External_Rel)))
51532845
AO
3657 return FALSE;
3658 }
4e5ba5b7 3659
51532845
AO
3660 return TRUE;
3661}
b34976b6 3662
51532845 3663static bfd_boolean
43850d5b
AO
3664elf32_frvfdpic_always_size_sections (bfd *output_bfd,
3665 struct bfd_link_info *info)
51532845 3666{
43850d5b 3667 if (!info->relocatable)
51532845
AO
3668 {
3669 struct elf_link_hash_entry *h;
3670 asection *sec;
b34976b6 3671
51532845
AO
3672 /* Force a PT_GNU_STACK segment to be created. */
3673 if (! elf_tdata (output_bfd)->stack_flags)
3674 elf_tdata (output_bfd)->stack_flags = PF_R | PF_W | PF_X;
4e5ba5b7 3675
51532845
AO
3676 /* Define __stacksize if it's not defined yet. */
3677 h = elf_link_hash_lookup (elf_hash_table (info), "__stacksize",
3678 FALSE, FALSE, FALSE);
3679 if (! h || h->root.type != bfd_link_hash_defined
3680 || h->type != STT_OBJECT
3681 || !(h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR))
3682 {
ea01195e 3683 struct bfd_link_hash_entry *bh = NULL;
4e5ba5b7 3684
51532845
AO
3685 if (!(_bfd_generic_link_add_one_symbol
3686 (info, output_bfd, "__stacksize",
3687 BSF_GLOBAL, bfd_abs_section_ptr, DEFAULT_STACK_SIZE,
3688 (const char *) NULL, FALSE,
3689 get_elf_backend_data (output_bfd)->collect, &bh)))
3690 return FALSE;
4e5ba5b7 3691
51532845
AO
3692 h = (struct elf_link_hash_entry *) bh;
3693 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
3694 h->type = STT_OBJECT;
3695 }
4e5ba5b7 3696
51532845
AO
3697 /* Create a stack section, and set its alignment. */
3698 sec = bfd_make_section (output_bfd, ".stack");
4e5ba5b7 3699
51532845
AO
3700 if (sec == NULL
3701 || ! bfd_set_section_alignment (output_bfd, sec, 3))
3702 return FALSE;
3703 }
4e5ba5b7 3704
51532845
AO
3705 return TRUE;
3706}
4e5ba5b7 3707
51532845 3708static bfd_boolean
43850d5b
AO
3709elf32_frvfdpic_modify_segment_map (bfd *output_bfd,
3710 struct bfd_link_info *info)
51532845 3711{
43850d5b 3712 struct elf_segment_map *m;
4e5ba5b7 3713
7e9f3bd6
AO
3714 /* objcopy and strip preserve what's already there using
3715 elf32_frvfdpic_copy_private_bfd_data (). */
3716 if (! info)
3717 return TRUE;
3718
43850d5b
AO
3719 for (m = elf_tdata (output_bfd)->segment_map; m != NULL; m = m->next)
3720 if (m->p_type == PT_GNU_STACK)
3721 break;
3722
3723 if (m)
3724 {
3725 asection *sec = bfd_get_section_by_name (output_bfd, ".stack");
3726 struct elf_link_hash_entry *h;
51532845 3727
43850d5b 3728 if (sec)
4e5ba5b7 3729 {
43850d5b
AO
3730 /* Obtain the pointer to the __stacksize symbol. */
3731 h = elf_link_hash_lookup (elf_hash_table (info), "__stacksize",
3732 FALSE, FALSE, FALSE);
3733 while (h->root.type == bfd_link_hash_indirect
3734 || h->root.type == bfd_link_hash_warning)
3735 h = (struct elf_link_hash_entry *)h->root.u.i.link;
3736 BFD_ASSERT (h->root.type == bfd_link_hash_defined);
4e5ba5b7 3737
43850d5b
AO
3738 /* Set the section size from the symbol value. We
3739 intentionally ignore the symbol section. */
3740 if (h->root.type == bfd_link_hash_defined)
eea6121a 3741 sec->size = h->root.u.def.value;
43850d5b 3742 else
eea6121a 3743 sec->size = DEFAULT_STACK_SIZE;
51532845 3744
43850d5b
AO
3745 /* Add the stack section to the PT_GNU_STACK segment,
3746 such that its size and alignment requirements make it
3747 to the segment. */
3748 m->sections[m->count] = sec;
3749 m->count++;
51532845
AO
3750 }
3751 }
b34976b6 3752
51532845
AO
3753 return TRUE;
3754}
b34976b6 3755
51532845 3756/* Fill in code and data in dynamic sections. */
4e5ba5b7 3757
51532845 3758static bfd_boolean
43850d5b
AO
3759elf32_frv_finish_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
3760 struct bfd_link_info *info ATTRIBUTE_UNUSED)
3761{
3762 /* Nothing to be done for non-FDPIC. */
3763 return TRUE;
3764}
3765
3766static bfd_boolean
3767elf32_frvfdpic_finish_dynamic_sections (bfd *output_bfd,
3768 struct bfd_link_info *info)
51532845
AO
3769{
3770 bfd *dynobj;
3771 asection *sdyn;
4e5ba5b7 3772
51532845 3773 dynobj = elf_hash_table (info)->dynobj;
4e5ba5b7 3774
43850d5b 3775 if (frvfdpic_got_section (info))
51532845 3776 {
eea6121a 3777 BFD_ASSERT (frvfdpic_gotrel_section (info)->size
43850d5b 3778 == (frvfdpic_gotrel_section (info)->reloc_count
51532845 3779 * sizeof (Elf32_External_Rel)));
4e5ba5b7 3780
43850d5b 3781 if (frvfdpic_gotfixup_section (info))
51532845 3782 {
43850d5b
AO
3783 struct elf_link_hash_entry *hgot = elf_hash_table (info)->hgot;
3784 bfd_vma got_value = hgot->root.u.def.value
3785 + hgot->root.u.def.section->output_section->vma
3786 + hgot->root.u.def.section->output_offset;
4e5ba5b7 3787
43850d5b
AO
3788 _frvfdpic_add_rofixup (output_bfd, frvfdpic_gotfixup_section (info),
3789 got_value, 0);
51532845 3790
eea6121a 3791 if (frvfdpic_gotfixup_section (info)->size
43850d5b 3792 != (frvfdpic_gotfixup_section (info)->reloc_count * 4))
51532845 3793 {
43850d5b
AO
3794 (*_bfd_error_handler)
3795 ("LINKER BUG: .rofixup section size mismatch");
3796 return FALSE;
51532845 3797 }
4e5ba5b7
DB
3798 }
3799 }
51532845
AO
3800 if (elf_hash_table (info)->dynamic_sections_created)
3801 {
eea6121a 3802 BFD_ASSERT (frvfdpic_pltrel_section (info)->size
43850d5b 3803 == (frvfdpic_pltrel_section (info)->reloc_count
51532845
AO
3804 * sizeof (Elf32_External_Rel)));
3805 }
4e5ba5b7 3806
51532845 3807 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
4e5ba5b7 3808
51532845 3809 if (elf_hash_table (info)->dynamic_sections_created)
4e5ba5b7 3810 {
51532845
AO
3811 Elf32_External_Dyn * dyncon;
3812 Elf32_External_Dyn * dynconend;
3813
3814 BFD_ASSERT (sdyn != NULL);
3815
3816 dyncon = (Elf32_External_Dyn *) sdyn->contents;
eea6121a 3817 dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
51532845
AO
3818
3819 for (; dyncon < dynconend; dyncon++)
4e5ba5b7 3820 {
51532845 3821 Elf_Internal_Dyn dyn;
4e5ba5b7 3822
51532845
AO
3823 bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
3824
3825 switch (dyn.d_tag)
4e5ba5b7
DB
3826 {
3827 default:
3828 break;
3829
51532845 3830 case DT_PLTGOT:
43850d5b
AO
3831 dyn.d_un.d_ptr = frvfdpic_got_section (info)->output_section->vma
3832 + frvfdpic_got_section (info)->output_offset
3833 + frvfdpic_got_initial_offset (info);
51532845
AO
3834 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
3835 break;
4e5ba5b7 3836
51532845 3837 case DT_JMPREL:
43850d5b
AO
3838 dyn.d_un.d_ptr = frvfdpic_pltrel_section (info)
3839 ->output_section->vma
3840 + frvfdpic_pltrel_section (info)->output_offset;
51532845
AO
3841 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
3842 break;
3843
3844 case DT_PLTRELSZ:
eea6121a 3845 dyn.d_un.d_val = frvfdpic_pltrel_section (info)->size;
51532845
AO
3846 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
3847 break;
4e5ba5b7
DB
3848 }
3849 }
3850 }
4e5ba5b7 3851
51532845 3852 return TRUE;
4e5ba5b7
DB
3853}
3854
51532845
AO
3855/* Adjust a symbol defined by a dynamic object and referenced by a
3856 regular object. */
4e5ba5b7 3857
b34976b6 3858static bfd_boolean
43850d5b
AO
3859elf32_frvfdpic_adjust_dynamic_symbol
3860(struct bfd_link_info *info ATTRIBUTE_UNUSED,
3861 struct elf_link_hash_entry *h ATTRIBUTE_UNUSED)
4e5ba5b7 3862{
51532845
AO
3863 bfd * dynobj;
3864
3865 dynobj = elf_hash_table (info)->dynobj;
3866
3867 /* Make sure we know what is going on here. */
3868 BFD_ASSERT (dynobj != NULL
3869 && (h->weakdef != NULL
3870 || ((h->elf_link_hash_flags
3871 & ELF_LINK_HASH_DEF_DYNAMIC) != 0
3872 && (h->elf_link_hash_flags
3873 & ELF_LINK_HASH_REF_REGULAR) != 0
3874 && (h->elf_link_hash_flags
3875 & ELF_LINK_HASH_DEF_REGULAR) == 0)));
3876
3877 /* If this is a weak symbol, and there is a real definition, the
3878 processor independent code will have arranged for us to see the
3879 real definition first, and we can just use the same value. */
3880 if (h->weakdef != NULL)
3881 {
3882 BFD_ASSERT (h->weakdef->root.type == bfd_link_hash_defined
3883 || h->weakdef->root.type == bfd_link_hash_defweak);
3884 h->root.u.def.section = h->weakdef->root.u.def.section;
3885 h->root.u.def.value = h->weakdef->root.u.def.value;
3886 }
3887
b34976b6 3888 return TRUE;
4e5ba5b7
DB
3889}
3890
51532845 3891/* Perform any actions needed for dynamic symbols. */
4e5ba5b7 3892
b34976b6 3893static bfd_boolean
43850d5b
AO
3894elf32_frvfdpic_finish_dynamic_symbol
3895(bfd *output_bfd ATTRIBUTE_UNUSED,
3896 struct bfd_link_info *info ATTRIBUTE_UNUSED,
3897 struct elf_link_hash_entry *h ATTRIBUTE_UNUSED,
3898 Elf_Internal_Sym *sym ATTRIBUTE_UNUSED)
4e5ba5b7 3899{
b34976b6 3900 return TRUE;
4e5ba5b7 3901}
51532845 3902
ec3391e7
AO
3903/* Decide whether to attempt to turn absptr or lsda encodings in
3904 shared libraries into pcrel within the given input section. */
3905
3906static bfd_boolean
43850d5b
AO
3907frvfdpic_elf_use_relative_eh_frame
3908(bfd *input_bfd ATTRIBUTE_UNUSED,
3909 struct bfd_link_info *info ATTRIBUTE_UNUSED,
3910 asection *eh_frame_section ATTRIBUTE_UNUSED)
ec3391e7
AO
3911{
3912 /* We can't use PC-relative encodings in FDPIC binaries, in general. */
43850d5b 3913 return FALSE;
ec3391e7
AO
3914}
3915
3916/* Adjust the contents of an eh_frame_hdr section before they're output. */
3917
3918static bfd_byte
43850d5b
AO
3919frvfdpic_elf_encode_eh_address (bfd *abfd,
3920 struct bfd_link_info *info,
3921 asection *osec, bfd_vma offset,
3922 asection *loc_sec, bfd_vma loc_offset,
3923 bfd_vma *encoded)
ec3391e7
AO
3924{
3925 struct elf_link_hash_entry *h;
3926
ec3391e7
AO
3927 h = elf_hash_table (info)->hgot;
3928 BFD_ASSERT (h && h->root.type == bfd_link_hash_defined);
3929
43850d5b
AO
3930 if (! h || (_frvfdpic_osec_to_segment (abfd, osec)
3931 == _frvfdpic_osec_to_segment (abfd, loc_sec->output_section)))
ec3391e7
AO
3932 return _bfd_elf_encode_eh_address (abfd, info, osec, offset,
3933 loc_sec, loc_offset, encoded);
3934
43850d5b
AO
3935 BFD_ASSERT (_frvfdpic_osec_to_segment (abfd, osec)
3936 == (_frvfdpic_osec_to_segment
3937 (abfd, h->root.u.def.section->output_section)));
ec3391e7
AO
3938
3939 *encoded = osec->vma + offset
3940 - (h->root.u.def.value
3941 + h->root.u.def.section->output_section->vma
3942 + h->root.u.def.section->output_offset);
3943
3944 return DW_EH_PE_datarel | DW_EH_PE_sdata4;
3945}
3946
4e5ba5b7 3947/* Look through the relocs for a section during the first phase.
51532845
AO
3948
3949 Besides handling virtual table relocs for gc, we have to deal with
3950 all sorts of PIC-related relocations. We describe below the
3951 general plan on how to handle such relocations, even though we only
3952 collect information at this point, storing them in hash tables for
3953 perusal of later passes.
3954
3955 32 relocations are propagated to the linker output when creating
3956 position-independent output. LO16 and HI16 relocations are not
3957 supposed to be encountered in this case.
3958
3959 LABEL16 should always be resolvable by the linker, since it's only
3960 used by branches.
3961
3962 LABEL24, on the other hand, is used by calls. If it turns out that
3963 the target of a call is a dynamic symbol, a PLT entry must be
3964 created for it, which triggers the creation of a private function
3965 descriptor and, unless lazy binding is disabled, a lazy PLT entry.
3966
3967 GPREL relocations require the referenced symbol to be in the same
3968 segment as _gp, but this can only be checked later.
3969
3970 All GOT, GOTOFF and FUNCDESC relocations require a .got section to
3971 exist. LABEL24 might as well, since it may require a PLT entry,
3972 that will require a got.
3973
3974 Non-FUNCDESC GOT relocations require a GOT entry to be created
3975 regardless of whether the symbol is dynamic. However, since a
3976 global symbol that turns out to not be exported may have the same
3977 address of a non-dynamic symbol, we don't assign GOT entries at
3978 this point, such that we can share them in this case. A relocation
3979 for the GOT entry always has to be created, be it to offset a
3980 private symbol by the section load address, be it to get the symbol
3981 resolved dynamically.
3982
3983 FUNCDESC GOT relocations require a GOT entry to be created, and
3984 handled as if a FUNCDESC relocation was applied to the GOT entry in
3985 an object file.
3986
3987 FUNCDESC relocations referencing a symbol that turns out to NOT be
3988 dynamic cause a private function descriptor to be created. The
3989 FUNCDESC relocation then decays to a 32 relocation that points at
3990 the private descriptor. If the symbol is dynamic, the FUNCDESC
3991 relocation is propagated to the linker output, such that the
3992 dynamic linker creates the canonical descriptor, pointing to the
3993 dynamically-resolved definition of the function.
3994
3995 Non-FUNCDESC GOTOFF relocations must always refer to non-dynamic
3996 symbols that are assigned to the same segment as the GOT, but we
3997 can only check this later, after we know the complete set of
3998 symbols defined and/or exported.
3999
4000 FUNCDESC GOTOFF relocations require a function descriptor to be
4001 created and, unless lazy binding is disabled or the symbol is not
4002 dynamic, a lazy PLT entry. Since we can't tell at this point
4003 whether a symbol is going to be dynamic, we have to decide later
4004 whether to create a lazy PLT entry or bind the descriptor directly
4005 to the private function.
4006
4007 FUNCDESC_VALUE relocations are not supposed to be present in object
4008 files, but they may very well be simply propagated to the linker
4009 output, since they have no side effect.
4010
4011
4012 A function descriptor always requires a FUNCDESC_VALUE relocation.
4013 Whether it's in .plt.rel or not depends on whether lazy binding is
4014 enabled and on whether the referenced symbol is dynamic.
4015
4016 The existence of a lazy PLT requires the resolverStub lazy PLT
4017 entry to be present.
4018
4019
4020 As for assignment of GOT, PLT and lazy PLT entries, and private
4021 descriptors, we might do them all sequentially, but we can do
4022 better than that. For example, we can place GOT entries and
4023 private function descriptors referenced using 12-bit operands
4024 closer to the PIC register value, such that these relocations don't
4025 overflow. Those that are only referenced with LO16 relocations
4026 could come next, but we may as well place PLT-required function
4027 descriptors in the 12-bit range to make them shorter. Symbols
4028 referenced with LO16/HI16 may come next, but we may place
4029 additional function descriptors in the 16-bit range if we can
4030 reliably tell that we've already placed entries that are ever
4031 referenced with only LO16. PLT entries are therefore generated as
4032 small as possible, while not introducing relocation overflows in
4033 GOT or FUNCDESC_GOTOFF relocations. Lazy PLT entries could be
4034 generated before or after PLT entries, but not intermingled with
4035 them, such that we can have more lazy PLT entries in range for a
4036 branch to the resolverStub. The resolverStub should be emitted at
4037 the most distant location from the first lazy PLT entry such that
4038 it's still in range for a branch, or closer, if there isn't a need
4039 for so many lazy PLT entries. Additional lazy PLT entries may be
4040 emitted after the resolverStub, as long as branches are still in
4041 range. If the branch goes out of range, longer lazy PLT entries
4042 are emitted.
4043
4044 We could further optimize PLT and lazy PLT entries by giving them
4045 priority in assignment to closer-to-gr17 locations depending on the
4046 number of occurrences of references to them (assuming a function
4047 that's called more often is more important for performance, so its
4048 PLT entry should be faster), or taking hints from the compiler.
4049 Given infinite time and money... :-) */
b34976b6
AM
4050
4051static bfd_boolean
4e5ba5b7
DB
4052elf32_frv_check_relocs (abfd, info, sec, relocs)
4053 bfd *abfd;
4054 struct bfd_link_info *info;
4055 asection *sec;
4056 const Elf_Internal_Rela *relocs;
4057{
4058 Elf_Internal_Shdr *symtab_hdr;
4059 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
4060 const Elf_Internal_Rela *rel;
4061 const Elf_Internal_Rela *rel_end;
51532845 4062 bfd *dynobj;
43850d5b 4063 struct frvfdpic_relocs_info *picrel;
b34976b6 4064
1049f94e 4065 if (info->relocatable)
b34976b6
AM
4066 return TRUE;
4067
4e5ba5b7
DB
4068 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
4069 sym_hashes = elf_sym_hashes (abfd);
4070 sym_hashes_end = sym_hashes + symtab_hdr->sh_size/sizeof(Elf32_External_Sym);
4071 if (!elf_bad_symtab (abfd))
4072 sym_hashes_end -= symtab_hdr->sh_info;
b34976b6 4073
51532845 4074 dynobj = elf_hash_table (info)->dynobj;
4e5ba5b7
DB
4075 rel_end = relocs + sec->reloc_count;
4076 for (rel = relocs; rel < rel_end; rel++)
4077 {
4078 struct elf_link_hash_entry *h;
4079 unsigned long r_symndx;
b34976b6 4080
4e5ba5b7
DB
4081 r_symndx = ELF32_R_SYM (rel->r_info);
4082 if (r_symndx < symtab_hdr->sh_info)
4083 h = NULL;
4084 else
4085 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
b34976b6 4086
51532845
AO
4087 switch (ELF32_R_TYPE (rel->r_info))
4088 {
51532845
AO
4089 case R_FRV_GOT12:
4090 case R_FRV_GOTHI:
4091 case R_FRV_GOTLO:
4092 case R_FRV_FUNCDESC_GOT12:
4093 case R_FRV_FUNCDESC_GOTHI:
4094 case R_FRV_FUNCDESC_GOTLO:
4095 case R_FRV_GOTOFF12:
4096 case R_FRV_GOTOFFHI:
4097 case R_FRV_GOTOFFLO:
4098 case R_FRV_FUNCDESC_GOTOFF12:
4099 case R_FRV_FUNCDESC_GOTOFFHI:
4100 case R_FRV_FUNCDESC_GOTOFFLO:
4101 case R_FRV_FUNCDESC:
4102 case R_FRV_FUNCDESC_VALUE:
43850d5b
AO
4103 if (! IS_FDPIC (abfd))
4104 goto bad_reloc;
4105 /* Fall through. */
4106 case R_FRV_GPREL12:
4107 case R_FRV_GPRELU12:
4108 case R_FRV_GPRELHI:
4109 case R_FRV_GPRELLO:
4110 case R_FRV_LABEL24:
4111 case R_FRV_32:
51532845
AO
4112 if (! dynobj)
4113 {
4114 elf_hash_table (info)->dynobj = dynobj = abfd;
4115 if (! _frv_create_got_section (abfd, info))
4116 return FALSE;
4117 }
43850d5b
AO
4118 if (! IS_FDPIC (abfd))
4119 {
4120 picrel = NULL;
4121 break;
4122 }
51532845
AO
4123 if (h != NULL)
4124 {
4125 if (h->dynindx == -1)
4126 switch (ELF_ST_VISIBILITY (h->other))
4127 {
4128 case STV_INTERNAL:
4129 case STV_HIDDEN:
4130 break;
4131 default:
c152c796 4132 bfd_elf_link_record_dynamic_symbol (info, h);
51532845
AO
4133 break;
4134 }
4135 picrel
43850d5b
AO
4136 = frvfdpic_relocs_info_for_global (frvfdpic_relocs_info (info),
4137 abfd, h,
4138 rel->r_addend, INSERT);
51532845
AO
4139 }
4140 else
43850d5b
AO
4141 picrel = frvfdpic_relocs_info_for_local (frvfdpic_relocs_info
4142 (info), abfd, r_symndx,
4143 rel->r_addend, INSERT);
51532845
AO
4144 if (! picrel)
4145 return FALSE;
4146 break;
4147
4148 default:
4149 picrel = NULL;
4150 break;
4151 }
4152
4e5ba5b7
DB
4153 switch (ELF32_R_TYPE (rel->r_info))
4154 {
51532845 4155 case R_FRV_LABEL24:
43850d5b
AO
4156 if (IS_FDPIC (abfd))
4157 picrel->call = 1;
51532845
AO
4158 break;
4159
4160 case R_FRV_FUNCDESC_VALUE:
4161 picrel->relocsfdv++;
4162 if (bfd_get_section_flags (abfd, sec) & SEC_ALLOC)
4163 picrel->relocs32--;
4164 /* Fall through. */
43850d5b 4165
51532845 4166 case R_FRV_32:
43850d5b
AO
4167 if (! IS_FDPIC (abfd))
4168 break;
4169
51532845
AO
4170 picrel->sym = 1;
4171 if (bfd_get_section_flags (abfd, sec) & SEC_ALLOC)
4172 picrel->relocs32++;
4173 break;
4174
4175 case R_FRV_GOT12:
4176 picrel->got12 = 1;
4177 break;
4178
4179 case R_FRV_GOTHI:
4180 case R_FRV_GOTLO:
4181 picrel->gothilo = 1;
4182 break;
4183
4184 case R_FRV_FUNCDESC_GOT12:
4185 picrel->fdgot12 = 1;
4186 break;
4187
4188 case R_FRV_FUNCDESC_GOTHI:
4189 case R_FRV_FUNCDESC_GOTLO:
4190 picrel->fdgothilo = 1;
4191 break;
4192
4193 case R_FRV_GOTOFF12:
4194 case R_FRV_GOTOFFHI:
4195 case R_FRV_GOTOFFLO:
4196 picrel->gotoff = 1;
4197 break;
4198
4199 case R_FRV_FUNCDESC_GOTOFF12:
4200 picrel->fdgoff12 = 1;
4201 break;
4202
4203 case R_FRV_FUNCDESC_GOTOFFHI:
4204 case R_FRV_FUNCDESC_GOTOFFLO:
4205 picrel->fdgoffhilo = 1;
4206 break;
4207
4208 case R_FRV_FUNCDESC:
4209 picrel->fd = 1;
4210 picrel->relocsfd++;
4211 break;
4212
4e5ba5b7
DB
4213 /* This relocation describes the C++ object vtable hierarchy.
4214 Reconstruct it for later use during GC. */
4215 case R_FRV_GNU_VTINHERIT:
c152c796 4216 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
b34976b6 4217 return FALSE;
4e5ba5b7 4218 break;
b34976b6 4219
4e5ba5b7
DB
4220 /* This relocation describes which C++ vtable entries are actually
4221 used. Record for later use during GC. */
4222 case R_FRV_GNU_VTENTRY:
c152c796 4223 if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
b34976b6 4224 return FALSE;
4e5ba5b7 4225 break;
43850d5b
AO
4226
4227 case R_FRV_LABEL16:
4228 case R_FRV_LO16:
4229 case R_FRV_HI16:
4230 case R_FRV_GPREL12:
4231 case R_FRV_GPRELU12:
4232 case R_FRV_GPREL32:
4233 case R_FRV_GPRELHI:
4234 case R_FRV_GPRELLO:
4235 break;
4236
4237 default:
4238 bad_reloc:
4239 (*_bfd_error_handler)
4240 (_("%s: unsupported relocation type %i"),
4241 bfd_archive_filename (abfd), ELF32_R_TYPE (rel->r_info));
4242 return FALSE;
4e5ba5b7
DB
4243 }
4244 }
b34976b6
AM
4245
4246 return TRUE;
4e5ba5b7
DB
4247}
4248
4249\f
4250/* Return the machine subcode from the ELF e_flags header. */
4251
4252static int
4253elf32_frv_machine (abfd)
4254 bfd *abfd;
4255{
4256 switch (elf_elfheader (abfd)->e_flags & EF_FRV_CPU_MASK)
4257 {
4258 default: break;
9c8ee639 4259 case EF_FRV_CPU_FR550: return bfd_mach_fr550;
4e5ba5b7 4260 case EF_FRV_CPU_FR500: return bfd_mach_fr500;
676a64f4
RS
4261 case EF_FRV_CPU_FR450: return bfd_mach_fr450;
4262 case EF_FRV_CPU_FR405: return bfd_mach_fr400;
4e5ba5b7
DB
4263 case EF_FRV_CPU_FR400: return bfd_mach_fr400;
4264 case EF_FRV_CPU_FR300: return bfd_mach_fr300;
4265 case EF_FRV_CPU_SIMPLE: return bfd_mach_frvsimple;
4266 case EF_FRV_CPU_TOMCAT: return bfd_mach_frvtomcat;
4267 }
4268
4269 return bfd_mach_frv;
4270}
4271
4272/* Set the right machine number for a FRV ELF file. */
4273
b34976b6 4274static bfd_boolean
4e5ba5b7
DB
4275elf32_frv_object_p (abfd)
4276 bfd *abfd;
4277{
4278 bfd_default_set_arch_mach (abfd, bfd_arch_frv, elf32_frv_machine (abfd));
43850d5b
AO
4279 return (((elf_elfheader (abfd)->e_flags & EF_FRV_FDPIC) != 0)
4280 == (IS_FDPIC (abfd)));
4e5ba5b7
DB
4281}
4282\f
4283/* Function to set the ELF flag bits. */
4284
b34976b6 4285static bfd_boolean
4e5ba5b7
DB
4286frv_elf_set_private_flags (abfd, flags)
4287 bfd *abfd;
4288 flagword flags;
4289{
4290 elf_elfheader (abfd)->e_flags = flags;
b34976b6
AM
4291 elf_flags_init (abfd) = TRUE;
4292 return TRUE;
4e5ba5b7
DB
4293}
4294
4295/* Copy backend specific data from one object module to another. */
4296
b34976b6 4297static bfd_boolean
4e5ba5b7
DB
4298frv_elf_copy_private_bfd_data (ibfd, obfd)
4299 bfd *ibfd;
4300 bfd *obfd;
4301{
4302 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
4303 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
b34976b6 4304 return TRUE;
4e5ba5b7
DB
4305
4306 BFD_ASSERT (!elf_flags_init (obfd)
4307 || elf_elfheader (obfd)->e_flags == elf_elfheader (ibfd)->e_flags);
4308
4309 elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
b34976b6
AM
4310 elf_flags_init (obfd) = TRUE;
4311 return TRUE;
4e5ba5b7
DB
4312}
4313
676a64f4
RS
4314/* Return true if the architecture described by elf header flag
4315 EXTENSION is an extension of the architecture described by BASE. */
4316
4317static bfd_boolean
4318frv_elf_arch_extension_p (flagword base, flagword extension)
4319{
4320 if (base == extension)
4321 return TRUE;
4322
4323 /* CPU_GENERIC code can be merged with code for a specific
4324 architecture, in which case the result is marked as being
4325 for the specific architecture. Everything is therefore
4326 an extension of CPU_GENERIC. */
4327 if (base == EF_FRV_CPU_GENERIC)
4328 return TRUE;
4329
4330 if (extension == EF_FRV_CPU_FR450)
4331 if (base == EF_FRV_CPU_FR400 || base == EF_FRV_CPU_FR405)
4332 return TRUE;
4333
4334 if (extension == EF_FRV_CPU_FR405)
4335 if (base == EF_FRV_CPU_FR400)
4336 return TRUE;
4337
4338 return FALSE;
4339}
4340
7e9f3bd6
AO
4341static bfd_boolean
4342elf32_frvfdpic_copy_private_bfd_data (bfd *ibfd, bfd *obfd)
4343{
4344 unsigned i;
4345
4346 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
4347 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
4348 return TRUE;
4349
4350 if (! frv_elf_copy_private_bfd_data (ibfd, obfd))
4351 return FALSE;
4352
4353 if (! elf_tdata (ibfd) || ! elf_tdata (ibfd)->phdr
4354 || ! elf_tdata (obfd) || ! elf_tdata (obfd)->phdr)
4355 return TRUE;
4356
4357 /* Copy the stack size. */
4358 for (i = 0; i < elf_elfheader (ibfd)->e_phnum; i++)
4359 if (elf_tdata (ibfd)->phdr[i].p_type == PT_GNU_STACK)
4360 {
4361 Elf_Internal_Phdr *iphdr = &elf_tdata (ibfd)->phdr[i];
4362
4363 for (i = 0; i < elf_elfheader (obfd)->e_phnum; i++)
4364 if (elf_tdata (obfd)->phdr[i].p_type == PT_GNU_STACK)
4365 {
4366 memcpy (&elf_tdata (obfd)->phdr[i], iphdr, sizeof (*iphdr));
4367
4368 /* Rewrite the phdrs, since we're only called after they
4369 were first written. */
4370 if (bfd_seek (obfd, (bfd_signed_vma) get_elf_backend_data (obfd)
4371 ->s->sizeof_ehdr, SEEK_SET) != 0
4372 || get_elf_backend_data (obfd)->s
4373 ->write_out_phdrs (obfd, elf_tdata (obfd)->phdr,
4374 elf_elfheader (obfd)->e_phnum) != 0)
4375 return FALSE;
4376 break;
4377 }
4378
4379 break;
4380 }
4381
4382 return TRUE;
4383}
4384
4e5ba5b7
DB
4385/* Merge backend specific data from an object file to the output
4386 object file when linking. */
4387
b34976b6 4388static bfd_boolean
4e5ba5b7
DB
4389frv_elf_merge_private_bfd_data (ibfd, obfd)
4390 bfd *ibfd;
4391 bfd *obfd;
4392{
4393 flagword old_flags, old_partial;
4394 flagword new_flags, new_partial;
b34976b6 4395 bfd_boolean error = FALSE;
4e5ba5b7
DB
4396 char new_opt[80];
4397 char old_opt[80];
4398
4399 new_opt[0] = old_opt[0] = '\0';
4400 new_flags = elf_elfheader (ibfd)->e_flags;
4401 old_flags = elf_elfheader (obfd)->e_flags;
4402
51532845
AO
4403 if (new_flags & EF_FRV_FDPIC)
4404 new_flags &= ~EF_FRV_PIC;
4405
4e5ba5b7
DB
4406#ifdef DEBUG
4407 (*_bfd_error_handler) ("old_flags = 0x%.8lx, new_flags = 0x%.8lx, init = %s, filename = %s",
4408 old_flags, new_flags, elf_flags_init (obfd) ? "yes" : "no",
4409 bfd_get_filename (ibfd));
4410#endif
4411
4412 if (!elf_flags_init (obfd)) /* First call, no flags set. */
4413 {
b34976b6 4414 elf_flags_init (obfd) = TRUE;
4e5ba5b7
DB
4415 old_flags = new_flags;
4416 }
4417
4418 else if (new_flags == old_flags) /* Compatible flags are ok. */
4419 ;
4420
4421 else /* Possibly incompatible flags. */
4422 {
4423 /* Warn if different # of gprs are used. Note, 0 means nothing is
4424 said about the size of gprs. */
4425 new_partial = (new_flags & EF_FRV_GPR_MASK);
4426 old_partial = (old_flags & EF_FRV_GPR_MASK);
4427 if (new_partial == old_partial)
4428 ;
4429
4430 else if (new_partial == 0)
4431 ;
4432
4433 else if (old_partial == 0)
4434 old_flags |= new_partial;
4435
4436 else
4437 {
4438 switch (new_partial)
4439 {
4440 default: strcat (new_opt, " -mgpr-??"); break;
4441 case EF_FRV_GPR_32: strcat (new_opt, " -mgpr-32"); break;
4442 case EF_FRV_GPR_64: strcat (new_opt, " -mgpr-64"); break;
4443 }
4444
4445 switch (old_partial)
4446 {
4447 default: strcat (old_opt, " -mgpr-??"); break;
4448 case EF_FRV_GPR_32: strcat (old_opt, " -mgpr-32"); break;
4449 case EF_FRV_GPR_64: strcat (old_opt, " -mgpr-64"); break;
4450 }
4451 }
4452
4453 /* Warn if different # of fprs are used. Note, 0 means nothing is
4454 said about the size of fprs. */
4455 new_partial = (new_flags & EF_FRV_FPR_MASK);
4456 old_partial = (old_flags & EF_FRV_FPR_MASK);
4457 if (new_partial == old_partial)
4458 ;
4459
4460 else if (new_partial == 0)
4461 ;
4462
4463 else if (old_partial == 0)
4464 old_flags |= new_partial;
4465
4466 else
4467 {
4468 switch (new_partial)
4469 {
4470 default: strcat (new_opt, " -mfpr-?"); break;
4471 case EF_FRV_FPR_32: strcat (new_opt, " -mfpr-32"); break;
4472 case EF_FRV_FPR_64: strcat (new_opt, " -mfpr-64"); break;
4473 case EF_FRV_FPR_NONE: strcat (new_opt, " -msoft-float"); break;
4474 }
4475
4476 switch (old_partial)
4477 {
4478 default: strcat (old_opt, " -mfpr-?"); break;
4479 case EF_FRV_FPR_32: strcat (old_opt, " -mfpr-32"); break;
4480 case EF_FRV_FPR_64: strcat (old_opt, " -mfpr-64"); break;
4481 case EF_FRV_FPR_NONE: strcat (old_opt, " -msoft-float"); break;
4482 }
4483 }
4484
4485 /* Warn if different dword support was used. Note, 0 means nothing is
4486 said about the dword support. */
4487 new_partial = (new_flags & EF_FRV_DWORD_MASK);
4488 old_partial = (old_flags & EF_FRV_DWORD_MASK);
4489 if (new_partial == old_partial)
4490 ;
4491
4492 else if (new_partial == 0)
4493 ;
4494
4495 else if (old_partial == 0)
4496 old_flags |= new_partial;
4497
4498 else
4499 {
4500 switch (new_partial)
4501 {
4502 default: strcat (new_opt, " -mdword-?"); break;
4503 case EF_FRV_DWORD_YES: strcat (new_opt, " -mdword"); break;
4504 case EF_FRV_DWORD_NO: strcat (new_opt, " -mno-dword"); break;
4505 }
4506
4507 switch (old_partial)
4508 {
4509 default: strcat (old_opt, " -mdword-?"); break;
4510 case EF_FRV_DWORD_YES: strcat (old_opt, " -mdword"); break;
4511 case EF_FRV_DWORD_NO: strcat (old_opt, " -mno-dword"); break;
4512 }
4513 }
4514
4515 /* Or in flags that accumulate (ie, if one module uses it, mark that the
4516 feature is used. */
4517 old_flags |= new_flags & (EF_FRV_DOUBLE
4518 | EF_FRV_MEDIA
4519 | EF_FRV_MULADD
4520 | EF_FRV_NON_PIC_RELOCS);
4521
4522 /* If any module was compiled without -G0, clear the G0 bit. */
4523 old_flags = ((old_flags & ~ EF_FRV_G0)
4524 | (old_flags & new_flags & EF_FRV_G0));
4525
4526 /* If any module was compiled without -mnopack, clear the mnopack bit. */
4527 old_flags = ((old_flags & ~ EF_FRV_NOPACK)
4528 | (old_flags & new_flags & EF_FRV_NOPACK));
4529
4530 /* We don't have to do anything if the pic flags are the same, or the new
4531 module(s) were compiled with -mlibrary-pic. */
4532 new_partial = (new_flags & EF_FRV_PIC_FLAGS);
4533 old_partial = (old_flags & EF_FRV_PIC_FLAGS);
4534 if ((new_partial == old_partial) || ((new_partial & EF_FRV_LIBPIC) != 0))
4535 ;
4536
4537 /* If the old module(s) were compiled with -mlibrary-pic, copy in the pic
4538 flags if any from the new module. */
4539 else if ((old_partial & EF_FRV_LIBPIC) != 0)
4540 old_flags = (old_flags & ~ EF_FRV_PIC_FLAGS) | new_partial;
4541
4542 /* If we have mixtures of -fpic and -fPIC, or in both bits. */
4543 else if (new_partial != 0 && old_partial != 0)
4544 old_flags |= new_partial;
4545
4546 /* One module was compiled for pic and the other was not, see if we have
4547 had any relocations that are not pic-safe. */
4548 else
4549 {
4550 if ((old_flags & EF_FRV_NON_PIC_RELOCS) == 0)
4551 old_flags |= new_partial;
4552 else
4553 {
4554 old_flags &= ~ EF_FRV_PIC_FLAGS;
4555#ifndef FRV_NO_PIC_ERROR
b34976b6 4556 error = TRUE;
4e5ba5b7
DB
4557 (*_bfd_error_handler)
4558 (_("%s: compiled with %s and linked with modules that use non-pic relocations"),
4559 bfd_get_filename (ibfd),
4560 (new_flags & EF_FRV_BIGPIC) ? "-fPIC" : "-fpic");
4561#endif
4562 }
4563 }
4564
4565 /* Warn if different cpu is used (allow a specific cpu to override
4566 the generic cpu). */
4567 new_partial = (new_flags & EF_FRV_CPU_MASK);
4568 old_partial = (old_flags & EF_FRV_CPU_MASK);
676a64f4 4569 if (frv_elf_arch_extension_p (new_partial, old_partial))
4e5ba5b7
DB
4570 ;
4571
676a64f4 4572 else if (frv_elf_arch_extension_p (old_partial, new_partial))
4e5ba5b7
DB
4573 old_flags = (old_flags & ~EF_FRV_CPU_MASK) | new_partial;
4574
4575 else
4576 {
4577 switch (new_partial)
4578 {
4579 default: strcat (new_opt, " -mcpu=?"); break;
4580 case EF_FRV_CPU_GENERIC: strcat (new_opt, " -mcpu=frv"); break;
4581 case EF_FRV_CPU_SIMPLE: strcat (new_opt, " -mcpu=simple"); break;
9c8ee639 4582 case EF_FRV_CPU_FR550: strcat (new_opt, " -mcpu=fr550"); break;
4e5ba5b7 4583 case EF_FRV_CPU_FR500: strcat (new_opt, " -mcpu=fr500"); break;
676a64f4
RS
4584 case EF_FRV_CPU_FR450: strcat (new_opt, " -mcpu=fr450"); break;
4585 case EF_FRV_CPU_FR405: strcat (new_opt, " -mcpu=fr405"); break;
4e5ba5b7
DB
4586 case EF_FRV_CPU_FR400: strcat (new_opt, " -mcpu=fr400"); break;
4587 case EF_FRV_CPU_FR300: strcat (new_opt, " -mcpu=fr300"); break;
4588 case EF_FRV_CPU_TOMCAT: strcat (new_opt, " -mcpu=tomcat"); break;
4589 }
4590
4591 switch (old_partial)
4592 {
4593 default: strcat (old_opt, " -mcpu=?"); break;
4594 case EF_FRV_CPU_GENERIC: strcat (old_opt, " -mcpu=frv"); break;
4595 case EF_FRV_CPU_SIMPLE: strcat (old_opt, " -mcpu=simple"); break;
9c8ee639 4596 case EF_FRV_CPU_FR550: strcat (old_opt, " -mcpu=fr550"); break;
4e5ba5b7 4597 case EF_FRV_CPU_FR500: strcat (old_opt, " -mcpu=fr500"); break;
676a64f4
RS
4598 case EF_FRV_CPU_FR450: strcat (old_opt, " -mcpu=fr450"); break;
4599 case EF_FRV_CPU_FR405: strcat (old_opt, " -mcpu=fr405"); break;
4e5ba5b7
DB
4600 case EF_FRV_CPU_FR400: strcat (old_opt, " -mcpu=fr400"); break;
4601 case EF_FRV_CPU_FR300: strcat (old_opt, " -mcpu=fr300"); break;
4602 case EF_FRV_CPU_TOMCAT: strcat (old_opt, " -mcpu=tomcat"); break;
4603 }
4604 }
b34976b6 4605
4e5ba5b7
DB
4606 /* Print out any mismatches from above. */
4607 if (new_opt[0])
4608 {
b34976b6 4609 error = TRUE;
4e5ba5b7
DB
4610 (*_bfd_error_handler)
4611 (_("%s: compiled with %s and linked with modules compiled with %s"),
4612 bfd_get_filename (ibfd), new_opt, old_opt);
4613 }
4614
4615 /* Warn about any other mismatches */
4616 new_partial = (new_flags & ~ EF_FRV_ALL_FLAGS);
4617 old_partial = (old_flags & ~ EF_FRV_ALL_FLAGS);
4618 if (new_partial != old_partial)
4619 {
4620 old_flags |= new_partial;
b34976b6 4621 error = TRUE;
4e5ba5b7
DB
4622 (*_bfd_error_handler)
4623 (_("%s: uses different unknown e_flags (0x%lx) fields than previous modules (0x%lx)"),
4624 bfd_get_filename (ibfd), (long)new_partial, (long)old_partial);
4625 }
4626 }
4627
4628 /* If the cpu is -mcpu=simple, then set the -mnopack bit. */
4629 if ((old_flags & EF_FRV_CPU_MASK) == EF_FRV_CPU_SIMPLE)
4630 old_flags |= EF_FRV_NOPACK;
4631
4632 /* Update the old flags now with changes made above. */
4633 old_partial = elf_elfheader (obfd)->e_flags & EF_FRV_CPU_MASK;
4634 elf_elfheader (obfd)->e_flags = old_flags;
4635 if (old_partial != (old_flags & EF_FRV_CPU_MASK))
4636 bfd_default_set_arch_mach (obfd, bfd_arch_frv, elf32_frv_machine (obfd));
4637
43850d5b
AO
4638 if (((new_flags & EF_FRV_FDPIC) == 0)
4639 != (! IS_FDPIC (ibfd)))
4640 {
4641 error = TRUE;
4642 if (IS_FDPIC (obfd))
4643 (*_bfd_error_handler)
4644 (_("%s: cannot link non-fdpic object file into fdpic executable"),
4645 bfd_get_filename (ibfd));
4646 else
4647 (*_bfd_error_handler)
4648 (_("%s: cannot link fdpic object file into non-fdpic executable"),
4649 bfd_get_filename (ibfd));
4650 }
4651
4e5ba5b7
DB
4652 if (error)
4653 bfd_set_error (bfd_error_bad_value);
4654
4655 return !error;
4656}
4657
4658\f
b34976b6 4659bfd_boolean
4e5ba5b7
DB
4660frv_elf_print_private_bfd_data (abfd, ptr)
4661 bfd *abfd;
4662 PTR ptr;
4663{
4664 FILE *file = (FILE *) ptr;
4665 flagword flags;
4666
4667 BFD_ASSERT (abfd != NULL && ptr != NULL);
4668
4669 /* Print normal ELF private data. */
4670 _bfd_elf_print_private_bfd_data (abfd, ptr);
4671
4672 flags = elf_elfheader (abfd)->e_flags;
4673 fprintf (file, _("private flags = 0x%lx:"), (long)flags);
4674
4675 switch (flags & EF_FRV_CPU_MASK)
4676 {
4677 default: break;
4678 case EF_FRV_CPU_SIMPLE: fprintf (file, " -mcpu=simple"); break;
9c8ee639 4679 case EF_FRV_CPU_FR550: fprintf (file, " -mcpu=fr550"); break;
4e5ba5b7 4680 case EF_FRV_CPU_FR500: fprintf (file, " -mcpu=fr500"); break;
676a64f4
RS
4681 case EF_FRV_CPU_FR450: fprintf (file, " -mcpu=fr450"); break;
4682 case EF_FRV_CPU_FR405: fprintf (file, " -mcpu=fr405"); break;
4e5ba5b7
DB
4683 case EF_FRV_CPU_FR400: fprintf (file, " -mcpu=fr400"); break;
4684 case EF_FRV_CPU_FR300: fprintf (file, " -mcpu=fr300"); break;
4685 case EF_FRV_CPU_TOMCAT: fprintf (file, " -mcpu=tomcat"); break;
4686 }
4687
4688 switch (flags & EF_FRV_GPR_MASK)
4689 {
4690 default: break;
4691 case EF_FRV_GPR_32: fprintf (file, " -mgpr-32"); break;
4692 case EF_FRV_GPR_64: fprintf (file, " -mgpr-64"); break;
4693 }
4694
4695 switch (flags & EF_FRV_FPR_MASK)
4696 {
4697 default: break;
4698 case EF_FRV_FPR_32: fprintf (file, " -mfpr-32"); break;
4699 case EF_FRV_FPR_64: fprintf (file, " -mfpr-64"); break;
4700 case EF_FRV_FPR_NONE: fprintf (file, " -msoft-float"); break;
4701 }
4702
4703 switch (flags & EF_FRV_DWORD_MASK)
4704 {
4705 default: break;
4706 case EF_FRV_DWORD_YES: fprintf (file, " -mdword"); break;
4707 case EF_FRV_DWORD_NO: fprintf (file, " -mno-dword"); break;
4708 }
4709
4710 if (flags & EF_FRV_DOUBLE)
4711 fprintf (file, " -mdouble");
4712
4713 if (flags & EF_FRV_MEDIA)
4714 fprintf (file, " -mmedia");
4715
4716 if (flags & EF_FRV_MULADD)
4717 fprintf (file, " -mmuladd");
4718
4719 if (flags & EF_FRV_PIC)
4720 fprintf (file, " -fpic");
4721
4722 if (flags & EF_FRV_BIGPIC)
4723 fprintf (file, " -fPIC");
4724
51532845
AO
4725 if (flags & EF_FRV_LIBPIC)
4726 fprintf (file, " -mlibrary-pic");
4727
4728 if (flags & EF_FRV_FDPIC)
4729 fprintf (file, " -mfdpic");
4730
4e5ba5b7
DB
4731 if (flags & EF_FRV_NON_PIC_RELOCS)
4732 fprintf (file, " non-pic relocations");
4733
4734 if (flags & EF_FRV_G0)
4735 fprintf (file, " -G0");
4736
4737 fputc ('\n', file);
b34976b6 4738 return TRUE;
4e5ba5b7
DB
4739}
4740
4741\f
4742#define ELF_ARCH bfd_arch_frv
4743#define ELF_MACHINE_CODE EM_CYGNUS_FRV
4744#define ELF_MAXPAGESIZE 0x1000
4745
4746#define TARGET_BIG_SYM bfd_elf32_frv_vec
4747#define TARGET_BIG_NAME "elf32-frv"
4748
4e5ba5b7
DB
4749#define elf_info_to_howto frv_info_to_howto_rela
4750#define elf_backend_relocate_section elf32_frv_relocate_section
4751#define elf_backend_gc_mark_hook elf32_frv_gc_mark_hook
4752#define elf_backend_gc_sweep_hook elf32_frv_gc_sweep_hook
4753#define elf_backend_check_relocs elf32_frv_check_relocs
4754#define elf_backend_object_p elf32_frv_object_p
4755#define elf_backend_add_symbol_hook elf32_frv_add_symbol_hook
4756
4757#define elf_backend_can_gc_sections 1
de2d743e 4758#define elf_backend_rela_normal 1
4e5ba5b7
DB
4759
4760#define bfd_elf32_bfd_reloc_type_lookup frv_reloc_type_lookup
4761#define bfd_elf32_bfd_set_private_flags frv_elf_set_private_flags
4762#define bfd_elf32_bfd_copy_private_bfd_data frv_elf_copy_private_bfd_data
4763#define bfd_elf32_bfd_merge_private_bfd_data frv_elf_merge_private_bfd_data
4764#define bfd_elf32_bfd_print_private_bfd_data frv_elf_print_private_bfd_data
4765
43850d5b
AO
4766#define elf_backend_want_got_sym 1
4767#define elf_backend_got_header_size 0
4768#define elf_backend_want_got_plt 0
4769#define elf_backend_plt_readonly 1
4770#define elf_backend_want_plt_sym 0
4771#define elf_backend_plt_header_size 0
4772
4773#define elf_backend_finish_dynamic_sections \
4774 elf32_frv_finish_dynamic_sections
4775
4776#include "elf32-target.h"
4777
4778#undef ELF_MAXPAGESIZE
4779#define ELF_MAXPAGESIZE 0x4000
4780
4781#undef TARGET_BIG_SYM
4782#define TARGET_BIG_SYM bfd_elf32_frvfdpic_vec
4783#undef TARGET_BIG_NAME
4784#define TARGET_BIG_NAME "elf32-frvfdpic"
4785#undef elf32_bed
4786#define elf32_bed elf32_frvfdpic_bed
4787
4788#undef elf_info_to_howto_rel
4789#define elf_info_to_howto_rel frvfdpic_info_to_howto_rel
4790
4791#undef bfd_elf32_bfd_link_hash_table_create
4792#define bfd_elf32_bfd_link_hash_table_create \
4793 frvfdpic_elf_link_hash_table_create
4794#undef elf_backend_always_size_sections
51532845 4795#define elf_backend_always_size_sections \
43850d5b
AO
4796 elf32_frvfdpic_always_size_sections
4797#undef elf_backend_modify_segment_map
51532845 4798#define elf_backend_modify_segment_map \
43850d5b 4799 elf32_frvfdpic_modify_segment_map
7e9f3bd6
AO
4800#undef bfd_elf32_bfd_copy_private_bfd_data
4801#define bfd_elf32_bfd_copy_private_bfd_data \
4802 elf32_frvfdpic_copy_private_bfd_data
51532845 4803
43850d5b 4804#undef elf_backend_create_dynamic_sections
51532845 4805#define elf_backend_create_dynamic_sections \
43850d5b
AO
4806 elf32_frvfdpic_create_dynamic_sections
4807#undef elf_backend_adjust_dynamic_symbol
51532845 4808#define elf_backend_adjust_dynamic_symbol \
43850d5b
AO
4809 elf32_frvfdpic_adjust_dynamic_symbol
4810#undef elf_backend_size_dynamic_sections
51532845 4811#define elf_backend_size_dynamic_sections \
43850d5b
AO
4812 elf32_frvfdpic_size_dynamic_sections
4813#undef elf_backend_finish_dynamic_symbol
51532845 4814#define elf_backend_finish_dynamic_symbol \
43850d5b
AO
4815 elf32_frvfdpic_finish_dynamic_symbol
4816#undef elf_backend_finish_dynamic_sections
51532845 4817#define elf_backend_finish_dynamic_sections \
43850d5b 4818 elf32_frvfdpic_finish_dynamic_sections
51532845 4819
43850d5b 4820#undef elf_backend_can_make_relative_eh_frame
ec3391e7 4821#define elf_backend_can_make_relative_eh_frame \
43850d5b
AO
4822 frvfdpic_elf_use_relative_eh_frame
4823#undef elf_backend_can_make_lsda_relative_eh_frame
ec3391e7 4824#define elf_backend_can_make_lsda_relative_eh_frame \
43850d5b
AO
4825 frvfdpic_elf_use_relative_eh_frame
4826#undef elf_backend_encode_eh_address
4827#define elf_backend_encode_eh_address \
4828 frvfdpic_elf_encode_eh_address
ec3391e7 4829
43850d5b 4830#undef elf_backend_may_use_rel_p
51532845 4831#define elf_backend_may_use_rel_p 1
43850d5b 4832#undef elf_backend_may_use_rela_p
51532845
AO
4833#define elf_backend_may_use_rela_p 1
4834/* We use REL for dynamic relocations only. */
43850d5b 4835#undef elf_backend_default_use_rela_p
51532845
AO
4836#define elf_backend_default_use_rela_p 1
4837
aee6f5b4
AO
4838#undef elf_backend_omit_section_dynsym
4839#define elf_backend_omit_section_dynsym _frvfdpic_link_omit_section_dynsym
4840
4e5ba5b7 4841#include "elf32-target.h"
This page took 0.350422 seconds and 4 git commands to generate.