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