* ld-mmix/bpo-1.d, ld-mmix/bpo-10.d, ld-mmix/bpo-11.d,
[binutils.git] / bfd / elf32-sh.c
blob368bbca37a178ea529a12c2bcae58c143ff7bd1d
1 /* Renesas / SuperH SH specific support for 32-bit ELF
2 Copyright 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003
3 Free Software Foundation, Inc.
4 Contributed by Ian Lance Taylor, Cygnus Support.
6 This file is part of BFD, the Binary File Descriptor library.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
22 #include "bfd.h"
23 #include "sysdep.h"
24 #include "bfdlink.h"
25 #include "libbfd.h"
26 #include "elf-bfd.h"
27 #include "elf/sh.h"
29 static bfd_reloc_status_type sh_elf_reloc
30 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
31 static bfd_reloc_status_type sh_elf_ignore_reloc
32 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
33 static reloc_howto_type *sh_elf_reloc_type_lookup
34 (bfd *, bfd_reloc_code_real_type);
35 static void sh_elf_info_to_howto
36 (bfd *, arelent *, Elf_Internal_Rela *);
37 static bfd_boolean sh_elf_set_private_flags
38 (bfd *, flagword);
39 static bfd_boolean sh_elf_copy_private_data
40 (bfd *, bfd *);
41 static bfd_boolean sh_elf_merge_private_data
42 (bfd *, bfd *);
43 static bfd_boolean sh_elf_set_mach_from_flags
44 (bfd *);
45 static bfd_boolean sh_elf_relax_section
46 (bfd *, asection *, struct bfd_link_info *, bfd_boolean *);
47 static bfd_boolean sh_elf_relax_delete_bytes
48 (bfd *, asection *, bfd_vma, int);
49 static bfd_boolean sh_elf_align_loads
50 (bfd *, asection *, Elf_Internal_Rela *, bfd_byte *, bfd_boolean *);
51 static bfd_boolean sh_elf_swap_insns
52 (bfd *, asection *, void *, bfd_byte *, bfd_vma);
53 static bfd_boolean sh_elf_relocate_section
54 (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
55 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
56 static bfd_byte *sh_elf_get_relocated_section_contents
57 (bfd *, struct bfd_link_info *, struct bfd_link_order *, bfd_byte *,
58 bfd_boolean, asymbol **);
59 static void sh_elf_copy_indirect_symbol
60 (const struct elf_backend_data *, struct elf_link_hash_entry *,
61 struct elf_link_hash_entry *);
62 static int sh_elf_optimized_tls_reloc
63 (struct bfd_link_info *, int, int);
64 static bfd_boolean sh_elf_mkobject
65 (bfd *);
66 static bfd_boolean sh_elf_object_p
67 (bfd *);
68 static bfd_boolean sh_elf_check_relocs
69 (bfd *, struct bfd_link_info *, asection *, const Elf_Internal_Rela *);
70 static struct bfd_hash_entry *sh_elf_link_hash_newfunc
71 (struct bfd_hash_entry *, struct bfd_hash_table *, const char *);
72 static struct bfd_link_hash_table *sh_elf_link_hash_table_create
73 (bfd *);
74 static bfd_boolean sh_elf_adjust_dynamic_symbol
75 (struct bfd_link_info *, struct elf_link_hash_entry *);
76 static bfd_boolean sh_elf_size_dynamic_sections
77 (bfd *, struct bfd_link_info *);
78 static bfd_boolean sh_elf_finish_dynamic_symbol
79 (bfd *, struct bfd_link_info *, struct elf_link_hash_entry *,
80 Elf_Internal_Sym *);
81 static bfd_boolean sh_elf_finish_dynamic_sections
82 (bfd *, struct bfd_link_info *);
83 static bfd_reloc_status_type sh_elf_reloc_loop
84 (int, bfd *, asection *, bfd_byte *, bfd_vma, asection *, bfd_vma,
85 bfd_vma);
86 static bfd_boolean create_got_section
87 (bfd *, struct bfd_link_info *);
88 static bfd_boolean sh_elf_create_dynamic_sections
89 (bfd *, struct bfd_link_info *);
90 static bfd_vma dtpoff_base
91 (struct bfd_link_info *);
92 static bfd_vma tpoff
93 (struct bfd_link_info *, bfd_vma);
94 static asection * sh_elf_gc_mark_hook
95 (asection *, struct bfd_link_info *, Elf_Internal_Rela *,
96 struct elf_link_hash_entry *, Elf_Internal_Sym *);
97 static bfd_boolean sh_elf_gc_sweep_hook
98 (bfd *, struct bfd_link_info *, asection *, const Elf_Internal_Rela *);
99 static bfd_boolean allocate_dynrelocs
100 (struct elf_link_hash_entry *, void *);
101 static bfd_boolean readonly_dynrelocs
102 (struct elf_link_hash_entry *, void *);
103 static enum elf_reloc_type_class sh_elf_reloc_type_class
104 (const Elf_Internal_Rela *);
105 #ifdef INCLUDE_SHMEDIA
106 inline static void movi_shori_putval (bfd *, unsigned long, char *);
107 #endif
108 static bfd_boolean elf32_shlin_grok_prstatus
109 (bfd *abfd, Elf_Internal_Note *note);
110 static bfd_boolean elf32_shlin_grok_psinfo
111 (bfd *abfd, Elf_Internal_Note *note);
113 /* The name of the dynamic interpreter. This is put in the .interp
114 section. */
116 #define ELF_DYNAMIC_INTERPRETER "/usr/lib/libc.so.1"
118 static reloc_howto_type sh_elf_howto_table[] =
120 /* No relocation. */
121 HOWTO (R_SH_NONE, /* type */
122 0, /* rightshift */
123 0, /* size (0 = byte, 1 = short, 2 = long) */
124 0, /* bitsize */
125 FALSE, /* pc_relative */
126 0, /* bitpos */
127 complain_overflow_dont, /* complain_on_overflow */
128 sh_elf_ignore_reloc, /* special_function */
129 "R_SH_NONE", /* name */
130 FALSE, /* partial_inplace */
131 0, /* src_mask */
132 0, /* dst_mask */
133 FALSE), /* pcrel_offset */
135 /* 32 bit absolute relocation. Setting partial_inplace to TRUE and
136 src_mask to a non-zero value is similar to the COFF toolchain. */
137 HOWTO (R_SH_DIR32, /* type */
138 0, /* rightshift */
139 2, /* size (0 = byte, 1 = short, 2 = long) */
140 32, /* bitsize */
141 FALSE, /* pc_relative */
142 0, /* bitpos */
143 complain_overflow_bitfield, /* complain_on_overflow */
144 sh_elf_reloc, /* special_function */
145 "R_SH_DIR32", /* name */
146 TRUE, /* partial_inplace */
147 0xffffffff, /* src_mask */
148 0xffffffff, /* dst_mask */
149 FALSE), /* pcrel_offset */
151 /* 32 bit PC relative relocation. */
152 HOWTO (R_SH_REL32, /* type */
153 0, /* rightshift */
154 2, /* size (0 = byte, 1 = short, 2 = long) */
155 32, /* bitsize */
156 TRUE, /* pc_relative */
157 0, /* bitpos */
158 complain_overflow_signed, /* complain_on_overflow */
159 sh_elf_ignore_reloc, /* special_function */
160 "R_SH_REL32", /* name */
161 TRUE, /* partial_inplace */
162 0xffffffff, /* src_mask */
163 0xffffffff, /* dst_mask */
164 TRUE), /* pcrel_offset */
166 /* 8 bit PC relative branch divided by 2. */
167 HOWTO (R_SH_DIR8WPN, /* type */
168 1, /* rightshift */
169 1, /* size (0 = byte, 1 = short, 2 = long) */
170 8, /* bitsize */
171 TRUE, /* pc_relative */
172 0, /* bitpos */
173 complain_overflow_signed, /* complain_on_overflow */
174 sh_elf_ignore_reloc, /* special_function */
175 "R_SH_DIR8WPN", /* name */
176 TRUE, /* partial_inplace */
177 0xff, /* src_mask */
178 0xff, /* dst_mask */
179 TRUE), /* pcrel_offset */
181 /* 12 bit PC relative branch divided by 2. */
182 /* This cannot be partial_inplace because relaxation can't know the
183 eventual value of a symbol. */
184 HOWTO (R_SH_IND12W, /* type */
185 1, /* rightshift */
186 1, /* size (0 = byte, 1 = short, 2 = long) */
187 12, /* bitsize */
188 TRUE, /* pc_relative */
189 0, /* bitpos */
190 complain_overflow_signed, /* complain_on_overflow */
191 NULL, /* special_function */
192 "R_SH_IND12W", /* name */
193 FALSE, /* partial_inplace */
194 0x0, /* src_mask */
195 0xfff, /* dst_mask */
196 TRUE), /* pcrel_offset */
198 /* 8 bit unsigned PC relative divided by 4. */
199 HOWTO (R_SH_DIR8WPL, /* type */
200 2, /* rightshift */
201 1, /* size (0 = byte, 1 = short, 2 = long) */
202 8, /* bitsize */
203 TRUE, /* pc_relative */
204 0, /* bitpos */
205 complain_overflow_unsigned, /* complain_on_overflow */
206 sh_elf_ignore_reloc, /* special_function */
207 "R_SH_DIR8WPL", /* name */
208 TRUE, /* partial_inplace */
209 0xff, /* src_mask */
210 0xff, /* dst_mask */
211 TRUE), /* pcrel_offset */
213 /* 8 bit unsigned PC relative divided by 2. */
214 HOWTO (R_SH_DIR8WPZ, /* type */
215 1, /* rightshift */
216 1, /* size (0 = byte, 1 = short, 2 = long) */
217 8, /* bitsize */
218 TRUE, /* pc_relative */
219 0, /* bitpos */
220 complain_overflow_unsigned, /* complain_on_overflow */
221 sh_elf_ignore_reloc, /* special_function */
222 "R_SH_DIR8WPZ", /* name */
223 TRUE, /* partial_inplace */
224 0xff, /* src_mask */
225 0xff, /* dst_mask */
226 TRUE), /* pcrel_offset */
228 /* 8 bit GBR relative. FIXME: This only makes sense if we have some
229 special symbol for the GBR relative area, and that is not
230 implemented. */
231 HOWTO (R_SH_DIR8BP, /* type */
232 0, /* rightshift */
233 1, /* size (0 = byte, 1 = short, 2 = long) */
234 8, /* bitsize */
235 FALSE, /* pc_relative */
236 0, /* bitpos */
237 complain_overflow_unsigned, /* complain_on_overflow */
238 sh_elf_ignore_reloc, /* special_function */
239 "R_SH_DIR8BP", /* name */
240 FALSE, /* partial_inplace */
241 0, /* src_mask */
242 0xff, /* dst_mask */
243 TRUE), /* pcrel_offset */
245 /* 8 bit GBR relative divided by 2. FIXME: This only makes sense if
246 we have some special symbol for the GBR relative area, and that
247 is not implemented. */
248 HOWTO (R_SH_DIR8W, /* type */
249 1, /* rightshift */
250 1, /* size (0 = byte, 1 = short, 2 = long) */
251 8, /* bitsize */
252 FALSE, /* pc_relative */
253 0, /* bitpos */
254 complain_overflow_unsigned, /* complain_on_overflow */
255 sh_elf_ignore_reloc, /* special_function */
256 "R_SH_DIR8W", /* name */
257 FALSE, /* partial_inplace */
258 0, /* src_mask */
259 0xff, /* dst_mask */
260 TRUE), /* pcrel_offset */
262 /* 8 bit GBR relative divided by 4. FIXME: This only makes sense if
263 we have some special symbol for the GBR relative area, and that
264 is not implemented. */
265 HOWTO (R_SH_DIR8L, /* type */
266 2, /* rightshift */
267 1, /* size (0 = byte, 1 = short, 2 = long) */
268 8, /* bitsize */
269 FALSE, /* pc_relative */
270 0, /* bitpos */
271 complain_overflow_unsigned, /* complain_on_overflow */
272 sh_elf_ignore_reloc, /* special_function */
273 "R_SH_DIR8L", /* name */
274 FALSE, /* partial_inplace */
275 0, /* src_mask */
276 0xff, /* dst_mask */
277 TRUE), /* pcrel_offset */
279 EMPTY_HOWTO (10),
280 EMPTY_HOWTO (11),
281 EMPTY_HOWTO (12),
282 EMPTY_HOWTO (13),
283 EMPTY_HOWTO (14),
284 EMPTY_HOWTO (15),
285 EMPTY_HOWTO (16),
286 EMPTY_HOWTO (17),
287 EMPTY_HOWTO (18),
288 EMPTY_HOWTO (19),
289 EMPTY_HOWTO (20),
290 EMPTY_HOWTO (21),
291 EMPTY_HOWTO (22),
292 EMPTY_HOWTO (23),
293 EMPTY_HOWTO (24),
295 /* The remaining relocs are a GNU extension used for relaxing. The
296 final pass of the linker never needs to do anything with any of
297 these relocs. Any required operations are handled by the
298 relaxation code. */
300 /* A 16 bit switch table entry. This is generated for an expression
301 such as ``.word L1 - L2''. The offset holds the difference
302 between the reloc address and L2. */
303 HOWTO (R_SH_SWITCH16, /* type */
304 0, /* rightshift */
305 1, /* size (0 = byte, 1 = short, 2 = long) */
306 16, /* bitsize */
307 FALSE, /* pc_relative */
308 0, /* bitpos */
309 complain_overflow_unsigned, /* complain_on_overflow */
310 sh_elf_ignore_reloc, /* special_function */
311 "R_SH_SWITCH16", /* name */
312 FALSE, /* partial_inplace */
313 0, /* src_mask */
314 0, /* dst_mask */
315 TRUE), /* pcrel_offset */
317 /* A 32 bit switch table entry. This is generated for an expression
318 such as ``.long L1 - L2''. The offset holds the difference
319 between the reloc address and L2. */
320 HOWTO (R_SH_SWITCH32, /* type */
321 0, /* rightshift */
322 2, /* size (0 = byte, 1 = short, 2 = long) */
323 32, /* bitsize */
324 FALSE, /* pc_relative */
325 0, /* bitpos */
326 complain_overflow_unsigned, /* complain_on_overflow */
327 sh_elf_ignore_reloc, /* special_function */
328 "R_SH_SWITCH32", /* name */
329 FALSE, /* partial_inplace */
330 0, /* src_mask */
331 0, /* dst_mask */
332 TRUE), /* pcrel_offset */
334 /* Indicates a .uses pseudo-op. The compiler will generate .uses
335 pseudo-ops when it finds a function call which can be relaxed.
336 The offset field holds the PC relative offset to the instruction
337 which loads the register used in the function call. */
338 HOWTO (R_SH_USES, /* type */
339 0, /* rightshift */
340 1, /* size (0 = byte, 1 = short, 2 = long) */
341 0, /* bitsize */
342 FALSE, /* pc_relative */
343 0, /* bitpos */
344 complain_overflow_unsigned, /* complain_on_overflow */
345 sh_elf_ignore_reloc, /* special_function */
346 "R_SH_USES", /* name */
347 FALSE, /* partial_inplace */
348 0, /* src_mask */
349 0, /* dst_mask */
350 TRUE), /* pcrel_offset */
352 /* The assembler will generate this reloc for addresses referred to
353 by the register loads associated with USES relocs. The offset
354 field holds the number of times the address is referenced in the
355 object file. */
356 HOWTO (R_SH_COUNT, /* type */
357 0, /* rightshift */
358 1, /* size (0 = byte, 1 = short, 2 = long) */
359 0, /* bitsize */
360 FALSE, /* pc_relative */
361 0, /* bitpos */
362 complain_overflow_unsigned, /* complain_on_overflow */
363 sh_elf_ignore_reloc, /* special_function */
364 "R_SH_COUNT", /* name */
365 FALSE, /* partial_inplace */
366 0, /* src_mask */
367 0, /* dst_mask */
368 TRUE), /* pcrel_offset */
370 /* Indicates an alignment statement. The offset field is the power
371 of 2 to which subsequent portions of the object file must be
372 aligned. */
373 HOWTO (R_SH_ALIGN, /* type */
374 0, /* rightshift */
375 1, /* size (0 = byte, 1 = short, 2 = long) */
376 0, /* bitsize */
377 FALSE, /* pc_relative */
378 0, /* bitpos */
379 complain_overflow_unsigned, /* complain_on_overflow */
380 sh_elf_ignore_reloc, /* special_function */
381 "R_SH_ALIGN", /* name */
382 FALSE, /* partial_inplace */
383 0, /* src_mask */
384 0, /* dst_mask */
385 TRUE), /* pcrel_offset */
387 /* The assembler will generate this reloc before a block of
388 instructions. A section should be processed as assumining it
389 contains data, unless this reloc is seen. */
390 HOWTO (R_SH_CODE, /* type */
391 0, /* rightshift */
392 1, /* size (0 = byte, 1 = short, 2 = long) */
393 0, /* bitsize */
394 FALSE, /* pc_relative */
395 0, /* bitpos */
396 complain_overflow_unsigned, /* complain_on_overflow */
397 sh_elf_ignore_reloc, /* special_function */
398 "R_SH_CODE", /* name */
399 FALSE, /* partial_inplace */
400 0, /* src_mask */
401 0, /* dst_mask */
402 TRUE), /* pcrel_offset */
404 /* The assembler will generate this reloc after a block of
405 instructions when it sees data that is not instructions. */
406 HOWTO (R_SH_DATA, /* type */
407 0, /* rightshift */
408 1, /* size (0 = byte, 1 = short, 2 = long) */
409 0, /* bitsize */
410 FALSE, /* pc_relative */
411 0, /* bitpos */
412 complain_overflow_unsigned, /* complain_on_overflow */
413 sh_elf_ignore_reloc, /* special_function */
414 "R_SH_DATA", /* name */
415 FALSE, /* partial_inplace */
416 0, /* src_mask */
417 0, /* dst_mask */
418 TRUE), /* pcrel_offset */
420 /* The assembler generates this reloc for each label within a block
421 of instructions. This permits the linker to avoid swapping
422 instructions which are the targets of branches. */
423 HOWTO (R_SH_LABEL, /* type */
424 0, /* rightshift */
425 1, /* size (0 = byte, 1 = short, 2 = long) */
426 0, /* bitsize */
427 FALSE, /* pc_relative */
428 0, /* bitpos */
429 complain_overflow_unsigned, /* complain_on_overflow */
430 sh_elf_ignore_reloc, /* special_function */
431 "R_SH_LABEL", /* name */
432 FALSE, /* partial_inplace */
433 0, /* src_mask */
434 0, /* dst_mask */
435 TRUE), /* pcrel_offset */
437 /* An 8 bit switch table entry. This is generated for an expression
438 such as ``.word L1 - L2''. The offset holds the difference
439 between the reloc address and L2. */
440 HOWTO (R_SH_SWITCH8, /* type */
441 0, /* rightshift */
442 0, /* size (0 = byte, 1 = short, 2 = long) */
443 8, /* bitsize */
444 FALSE, /* pc_relative */
445 0, /* bitpos */
446 complain_overflow_unsigned, /* complain_on_overflow */
447 sh_elf_ignore_reloc, /* special_function */
448 "R_SH_SWITCH8", /* name */
449 FALSE, /* partial_inplace */
450 0, /* src_mask */
451 0, /* dst_mask */
452 TRUE), /* pcrel_offset */
454 /* GNU extension to record C++ vtable hierarchy */
455 HOWTO (R_SH_GNU_VTINHERIT, /* type */
456 0, /* rightshift */
457 2, /* size (0 = byte, 1 = short, 2 = long) */
458 0, /* bitsize */
459 FALSE, /* pc_relative */
460 0, /* bitpos */
461 complain_overflow_dont, /* complain_on_overflow */
462 NULL, /* special_function */
463 "R_SH_GNU_VTINHERIT", /* name */
464 FALSE, /* partial_inplace */
465 0, /* src_mask */
466 0, /* dst_mask */
467 FALSE), /* pcrel_offset */
469 /* GNU extension to record C++ vtable member usage */
470 HOWTO (R_SH_GNU_VTENTRY, /* type */
471 0, /* rightshift */
472 2, /* size (0 = byte, 1 = short, 2 = long) */
473 0, /* bitsize */
474 FALSE, /* pc_relative */
475 0, /* bitpos */
476 complain_overflow_dont, /* complain_on_overflow */
477 _bfd_elf_rel_vtable_reloc_fn, /* special_function */
478 "R_SH_GNU_VTENTRY", /* name */
479 FALSE, /* partial_inplace */
480 0, /* src_mask */
481 0, /* dst_mask */
482 FALSE), /* pcrel_offset */
484 /* 8 bit PC relative divided by 2 - but specified in a very odd way. */
485 HOWTO (R_SH_LOOP_START, /* type */
486 1, /* rightshift */
487 1, /* size (0 = byte, 1 = short, 2 = long) */
488 8, /* bitsize */
489 FALSE, /* pc_relative */
490 0, /* bitpos */
491 complain_overflow_signed, /* complain_on_overflow */
492 sh_elf_ignore_reloc, /* special_function */
493 "R_SH_LOOP_START", /* name */
494 TRUE, /* partial_inplace */
495 0xff, /* src_mask */
496 0xff, /* dst_mask */
497 TRUE), /* pcrel_offset */
499 /* 8 bit PC relative divided by 2 - but specified in a very odd way. */
500 HOWTO (R_SH_LOOP_END, /* type */
501 1, /* rightshift */
502 1, /* size (0 = byte, 1 = short, 2 = long) */
503 8, /* bitsize */
504 FALSE, /* pc_relative */
505 0, /* bitpos */
506 complain_overflow_signed, /* complain_on_overflow */
507 sh_elf_ignore_reloc, /* special_function */
508 "R_SH_LOOP_END", /* name */
509 TRUE, /* partial_inplace */
510 0xff, /* src_mask */
511 0xff, /* dst_mask */
512 TRUE), /* pcrel_offset */
514 EMPTY_HOWTO (38),
515 EMPTY_HOWTO (39),
516 EMPTY_HOWTO (40),
517 EMPTY_HOWTO (41),
518 EMPTY_HOWTO (42),
519 EMPTY_HOWTO (43),
520 EMPTY_HOWTO (44),
522 #ifdef INCLUDE_SHMEDIA
523 /* Used in SHLLI.L and SHLRI.L. */
524 HOWTO (R_SH_DIR5U, /* type */
525 0, /* rightshift */
526 2, /* size (0 = byte, 1 = short, 2 = long) */
527 5, /* bitsize */
528 FALSE, /* pc_relative */
529 10, /* bitpos */
530 complain_overflow_unsigned, /* complain_on_overflow */
531 bfd_elf_generic_reloc, /* special_function */
532 "R_SH_DIR5U", /* name */
533 FALSE, /* partial_inplace */
534 0, /* src_mask */
535 0xfc00, /* dst_mask */
536 FALSE), /* pcrel_offset */
538 /* Used in SHARI, SHLLI et al. */
539 HOWTO (R_SH_DIR6U, /* type */
540 0, /* rightshift */
541 2, /* size (0 = byte, 1 = short, 2 = long) */
542 6, /* bitsize */
543 FALSE, /* pc_relative */
544 10, /* bitpos */
545 complain_overflow_unsigned, /* complain_on_overflow */
546 bfd_elf_generic_reloc, /* special_function */
547 "R_SH_DIR6U", /* name */
548 FALSE, /* partial_inplace */
549 0, /* src_mask */
550 0xfc00, /* dst_mask */
551 FALSE), /* pcrel_offset */
553 /* Used in BxxI, LDHI.L et al. */
554 HOWTO (R_SH_DIR6S, /* type */
555 0, /* rightshift */
556 2, /* size (0 = byte, 1 = short, 2 = long) */
557 6, /* bitsize */
558 FALSE, /* pc_relative */
559 10, /* bitpos */
560 complain_overflow_signed, /* complain_on_overflow */
561 bfd_elf_generic_reloc, /* special_function */
562 "R_SH_DIR6S", /* name */
563 FALSE, /* partial_inplace */
564 0, /* src_mask */
565 0xfc00, /* dst_mask */
566 FALSE), /* pcrel_offset */
568 /* Used in ADDI, ANDI et al. */
569 HOWTO (R_SH_DIR10S, /* type */
570 0, /* rightshift */
571 2, /* size (0 = byte, 1 = short, 2 = long) */
572 10, /* bitsize */
573 FALSE, /* pc_relative */
574 10, /* bitpos */
575 complain_overflow_signed, /* complain_on_overflow */
576 bfd_elf_generic_reloc, /* special_function */
577 "R_SH_DIR10S", /* name */
578 FALSE, /* partial_inplace */
579 0, /* src_mask */
580 0xffc00, /* dst_mask */
581 FALSE), /* pcrel_offset */
583 /* Used in LD.UW, ST.W et al. */
584 HOWTO (R_SH_DIR10SW, /* type */
585 1, /* rightshift */
586 2, /* size (0 = byte, 1 = short, 2 = long) */
587 11, /* bitsize */
588 FALSE, /* pc_relative */
589 10, /* bitpos */
590 complain_overflow_signed, /* complain_on_overflow */
591 bfd_elf_generic_reloc, /* special_function */
592 "R_SH_DIR10SW", /* name */
593 FALSE, /* partial_inplace */
594 0, /* src_mask */
595 0xffc00, /* dst_mask */
596 FALSE), /* pcrel_offset */
598 /* Used in LD.L, FLD.S et al. */
599 HOWTO (R_SH_DIR10SL, /* type */
600 2, /* rightshift */
601 2, /* size (0 = byte, 1 = short, 2 = long) */
602 12, /* bitsize */
603 FALSE, /* pc_relative */
604 10, /* bitpos */
605 complain_overflow_signed, /* complain_on_overflow */
606 bfd_elf_generic_reloc, /* special_function */
607 "R_SH_DIR10SL", /* name */
608 FALSE, /* partial_inplace */
609 0, /* src_mask */
610 0xffc00, /* dst_mask */
611 FALSE), /* pcrel_offset */
613 /* Used in FLD.D, FST.P et al. */
614 HOWTO (R_SH_DIR10SQ, /* type */
615 3, /* rightshift */
616 2, /* size (0 = byte, 1 = short, 2 = long) */
617 13, /* bitsize */
618 FALSE, /* pc_relative */
619 10, /* bitpos */
620 complain_overflow_signed, /* complain_on_overflow */
621 bfd_elf_generic_reloc, /* special_function */
622 "R_SH_DIR10SQ", /* name */
623 FALSE, /* partial_inplace */
624 0, /* src_mask */
625 0xffc00, /* dst_mask */
626 FALSE), /* pcrel_offset */
628 #else
629 EMPTY_HOWTO (45),
630 EMPTY_HOWTO (46),
631 EMPTY_HOWTO (47),
632 EMPTY_HOWTO (48),
633 EMPTY_HOWTO (49),
634 EMPTY_HOWTO (50),
635 EMPTY_HOWTO (51),
636 #endif
638 EMPTY_HOWTO (52),
639 EMPTY_HOWTO (53),
640 EMPTY_HOWTO (54),
641 EMPTY_HOWTO (55),
642 EMPTY_HOWTO (56),
643 EMPTY_HOWTO (57),
644 EMPTY_HOWTO (58),
645 EMPTY_HOWTO (59),
646 EMPTY_HOWTO (60),
647 EMPTY_HOWTO (61),
648 EMPTY_HOWTO (62),
649 EMPTY_HOWTO (63),
650 EMPTY_HOWTO (64),
651 EMPTY_HOWTO (65),
652 EMPTY_HOWTO (66),
653 EMPTY_HOWTO (67),
654 EMPTY_HOWTO (68),
655 EMPTY_HOWTO (69),
656 EMPTY_HOWTO (70),
657 EMPTY_HOWTO (71),
658 EMPTY_HOWTO (72),
659 EMPTY_HOWTO (73),
660 EMPTY_HOWTO (74),
661 EMPTY_HOWTO (75),
662 EMPTY_HOWTO (76),
663 EMPTY_HOWTO (77),
664 EMPTY_HOWTO (78),
665 EMPTY_HOWTO (79),
666 EMPTY_HOWTO (80),
667 EMPTY_HOWTO (81),
668 EMPTY_HOWTO (82),
669 EMPTY_HOWTO (83),
670 EMPTY_HOWTO (84),
671 EMPTY_HOWTO (85),
672 EMPTY_HOWTO (86),
673 EMPTY_HOWTO (87),
674 EMPTY_HOWTO (88),
675 EMPTY_HOWTO (89),
676 EMPTY_HOWTO (90),
677 EMPTY_HOWTO (91),
678 EMPTY_HOWTO (92),
679 EMPTY_HOWTO (93),
680 EMPTY_HOWTO (94),
681 EMPTY_HOWTO (95),
682 EMPTY_HOWTO (96),
683 EMPTY_HOWTO (97),
684 EMPTY_HOWTO (98),
685 EMPTY_HOWTO (99),
686 EMPTY_HOWTO (100),
687 EMPTY_HOWTO (101),
688 EMPTY_HOWTO (102),
689 EMPTY_HOWTO (103),
690 EMPTY_HOWTO (104),
691 EMPTY_HOWTO (105),
692 EMPTY_HOWTO (106),
693 EMPTY_HOWTO (107),
694 EMPTY_HOWTO (108),
695 EMPTY_HOWTO (109),
696 EMPTY_HOWTO (110),
697 EMPTY_HOWTO (111),
698 EMPTY_HOWTO (112),
699 EMPTY_HOWTO (113),
700 EMPTY_HOWTO (114),
701 EMPTY_HOWTO (115),
702 EMPTY_HOWTO (116),
703 EMPTY_HOWTO (117),
704 EMPTY_HOWTO (118),
705 EMPTY_HOWTO (119),
706 EMPTY_HOWTO (120),
707 EMPTY_HOWTO (121),
708 EMPTY_HOWTO (122),
709 EMPTY_HOWTO (123),
710 EMPTY_HOWTO (124),
711 EMPTY_HOWTO (125),
712 EMPTY_HOWTO (126),
713 EMPTY_HOWTO (127),
714 EMPTY_HOWTO (128),
715 EMPTY_HOWTO (129),
716 EMPTY_HOWTO (130),
717 EMPTY_HOWTO (131),
718 EMPTY_HOWTO (132),
719 EMPTY_HOWTO (133),
720 EMPTY_HOWTO (134),
721 EMPTY_HOWTO (135),
722 EMPTY_HOWTO (136),
723 EMPTY_HOWTO (137),
724 EMPTY_HOWTO (138),
725 EMPTY_HOWTO (139),
726 EMPTY_HOWTO (140),
727 EMPTY_HOWTO (141),
728 EMPTY_HOWTO (142),
729 EMPTY_HOWTO (143),
731 HOWTO (R_SH_TLS_GD_32, /* type */
732 0, /* rightshift */
733 2, /* size (0 = byte, 1 = short, 2 = long) */
734 32, /* bitsize */
735 FALSE, /* pc_relative */
736 0, /* bitpos */
737 complain_overflow_bitfield, /* complain_on_overflow */
738 bfd_elf_generic_reloc, /* */
739 "R_SH_TLS_GD_32", /* name */
740 TRUE, /* partial_inplace */
741 0xffffffff, /* src_mask */
742 0xffffffff, /* dst_mask */
743 FALSE), /* pcrel_offset */
745 HOWTO (R_SH_TLS_LD_32, /* type */
746 0, /* rightshift */
747 2, /* size (0 = byte, 1 = short, 2 = long) */
748 32, /* bitsize */
749 FALSE, /* pc_relative */
750 0, /* bitpos */
751 complain_overflow_bitfield, /* complain_on_overflow */
752 bfd_elf_generic_reloc, /* */
753 "R_SH_TLS_LD_32", /* name */
754 TRUE, /* partial_inplace */
755 0xffffffff, /* src_mask */
756 0xffffffff, /* dst_mask */
757 FALSE), /* pcrel_offset */
759 HOWTO (R_SH_TLS_LDO_32, /* type */
760 0, /* rightshift */
761 2, /* size (0 = byte, 1 = short, 2 = long) */
762 32, /* bitsize */
763 FALSE, /* pc_relative */
764 0, /* bitpos */
765 complain_overflow_bitfield, /* complain_on_overflow */
766 bfd_elf_generic_reloc, /* */
767 "R_SH_TLS_LDO_32", /* name */
768 TRUE, /* partial_inplace */
769 0xffffffff, /* src_mask */
770 0xffffffff, /* dst_mask */
771 FALSE), /* pcrel_offset */
773 HOWTO (R_SH_TLS_IE_32, /* type */
774 0, /* rightshift */
775 2, /* size (0 = byte, 1 = short, 2 = long) */
776 32, /* bitsize */
777 FALSE, /* pc_relative */
778 0, /* bitpos */
779 complain_overflow_bitfield, /* complain_on_overflow */
780 bfd_elf_generic_reloc, /* */
781 "R_SH_TLS_IE_32", /* name */
782 TRUE, /* partial_inplace */
783 0xffffffff, /* src_mask */
784 0xffffffff, /* dst_mask */
785 FALSE), /* pcrel_offset */
787 HOWTO (R_SH_TLS_LE_32, /* type */
788 0, /* rightshift */
789 2, /* size (0 = byte, 1 = short, 2 = long) */
790 32, /* bitsize */
791 FALSE, /* pc_relative */
792 0, /* bitpos */
793 complain_overflow_bitfield, /* complain_on_overflow */
794 bfd_elf_generic_reloc, /* */
795 "R_SH_TLS_LE_32", /* name */
796 TRUE, /* partial_inplace */
797 0xffffffff, /* src_mask */
798 0xffffffff, /* dst_mask */
799 FALSE), /* pcrel_offset */
801 HOWTO (R_SH_TLS_DTPMOD32, /* type */
802 0, /* rightshift */
803 2, /* size (0 = byte, 1 = short, 2 = long) */
804 32, /* bitsize */
805 FALSE, /* pc_relative */
806 0, /* bitpos */
807 complain_overflow_bitfield, /* complain_on_overflow */
808 bfd_elf_generic_reloc, /* */
809 "R_SH_TLS_DTPMOD32", /* name */
810 TRUE, /* partial_inplace */
811 0xffffffff, /* src_mask */
812 0xffffffff, /* dst_mask */
813 FALSE), /* pcrel_offset */
815 HOWTO (R_SH_TLS_DTPOFF32, /* type */
816 0, /* rightshift */
817 2, /* size (0 = byte, 1 = short, 2 = long) */
818 32, /* bitsize */
819 FALSE, /* pc_relative */
820 0, /* bitpos */
821 complain_overflow_bitfield, /* complain_on_overflow */
822 bfd_elf_generic_reloc, /* */
823 "R_SH_TLS_DTPOFF32", /* name */
824 TRUE, /* partial_inplace */
825 0xffffffff, /* src_mask */
826 0xffffffff, /* dst_mask */
827 FALSE), /* pcrel_offset */
829 HOWTO (R_SH_TLS_TPOFF32, /* type */
830 0, /* rightshift */
831 2, /* size (0 = byte, 1 = short, 2 = long) */
832 32, /* bitsize */
833 FALSE, /* pc_relative */
834 0, /* bitpos */
835 complain_overflow_bitfield, /* complain_on_overflow */
836 bfd_elf_generic_reloc, /* */
837 "R_SH_TLS_TPOFF32", /* name */
838 TRUE, /* partial_inplace */
839 0xffffffff, /* src_mask */
840 0xffffffff, /* dst_mask */
841 FALSE), /* pcrel_offset */
843 EMPTY_HOWTO (152),
844 EMPTY_HOWTO (153),
845 EMPTY_HOWTO (154),
846 EMPTY_HOWTO (155),
847 EMPTY_HOWTO (156),
848 EMPTY_HOWTO (157),
849 EMPTY_HOWTO (158),
850 EMPTY_HOWTO (159),
852 HOWTO (R_SH_GOT32, /* type */
853 0, /* rightshift */
854 2, /* size (0 = byte, 1 = short, 2 = long) */
855 32, /* bitsize */
856 FALSE, /* pc_relative */
857 0, /* bitpos */
858 complain_overflow_bitfield, /* complain_on_overflow */
859 bfd_elf_generic_reloc, /* */
860 "R_SH_GOT32", /* name */
861 TRUE, /* partial_inplace */
862 0xffffffff, /* src_mask */
863 0xffffffff, /* dst_mask */
864 FALSE), /* pcrel_offset */
866 HOWTO (R_SH_PLT32, /* type */
867 0, /* rightshift */
868 2, /* size (0 = byte, 1 = short, 2 = long) */
869 32, /* bitsize */
870 TRUE, /* pc_relative */
871 0, /* bitpos */
872 complain_overflow_bitfield, /* complain_on_overflow */
873 bfd_elf_generic_reloc, /* */
874 "R_SH_PLT32", /* name */
875 TRUE, /* partial_inplace */
876 0xffffffff, /* src_mask */
877 0xffffffff, /* dst_mask */
878 TRUE), /* pcrel_offset */
880 HOWTO (R_SH_COPY, /* type */
881 0, /* rightshift */
882 2, /* size (0 = byte, 1 = short, 2 = long) */
883 32, /* bitsize */
884 FALSE, /* pc_relative */
885 0, /* bitpos */
886 complain_overflow_bitfield, /* complain_on_overflow */
887 bfd_elf_generic_reloc, /* */
888 "R_SH_COPY", /* name */
889 TRUE, /* partial_inplace */
890 0xffffffff, /* src_mask */
891 0xffffffff, /* dst_mask */
892 FALSE), /* pcrel_offset */
894 HOWTO (R_SH_GLOB_DAT, /* type */
895 0, /* rightshift */
896 2, /* size (0 = byte, 1 = short, 2 = long) */
897 32, /* bitsize */
898 FALSE, /* pc_relative */
899 0, /* bitpos */
900 complain_overflow_bitfield, /* complain_on_overflow */
901 bfd_elf_generic_reloc, /* */
902 "R_SH_GLOB_DAT", /* name */
903 TRUE, /* partial_inplace */
904 0xffffffff, /* src_mask */
905 0xffffffff, /* dst_mask */
906 FALSE), /* pcrel_offset */
908 HOWTO (R_SH_JMP_SLOT, /* type */
909 0, /* rightshift */
910 2, /* size (0 = byte, 1 = short, 2 = long) */
911 32, /* bitsize */
912 FALSE, /* pc_relative */
913 0, /* bitpos */
914 complain_overflow_bitfield, /* complain_on_overflow */
915 bfd_elf_generic_reloc, /* */
916 "R_SH_JMP_SLOT", /* name */
917 TRUE, /* partial_inplace */
918 0xffffffff, /* src_mask */
919 0xffffffff, /* dst_mask */
920 FALSE), /* pcrel_offset */
922 HOWTO (R_SH_RELATIVE, /* type */
923 0, /* rightshift */
924 2, /* size (0 = byte, 1 = short, 2 = long) */
925 32, /* bitsize */
926 FALSE, /* pc_relative */
927 0, /* bitpos */
928 complain_overflow_bitfield, /* complain_on_overflow */
929 bfd_elf_generic_reloc, /* */
930 "R_SH_RELATIVE", /* name */
931 TRUE, /* partial_inplace */
932 0xffffffff, /* src_mask */
933 0xffffffff, /* dst_mask */
934 FALSE), /* pcrel_offset */
936 HOWTO (R_SH_GOTOFF, /* type */
937 0, /* rightshift */
938 2, /* size (0 = byte, 1 = short, 2 = long) */
939 32, /* bitsize */
940 FALSE, /* pc_relative */
941 0, /* bitpos */
942 complain_overflow_bitfield, /* complain_on_overflow */
943 bfd_elf_generic_reloc, /* */
944 "R_SH_GOTOFF", /* name */
945 TRUE, /* partial_inplace */
946 0xffffffff, /* src_mask */
947 0xffffffff, /* dst_mask */
948 FALSE), /* pcrel_offset */
950 HOWTO (R_SH_GOTPC, /* type */
951 0, /* rightshift */
952 2, /* size (0 = byte, 1 = short, 2 = long) */
953 32, /* bitsize */
954 TRUE, /* pc_relative */
955 0, /* bitpos */
956 complain_overflow_bitfield, /* complain_on_overflow */
957 bfd_elf_generic_reloc, /* */
958 "R_SH_GOTPC", /* name */
959 TRUE, /* partial_inplace */
960 0xffffffff, /* src_mask */
961 0xffffffff, /* dst_mask */
962 TRUE), /* pcrel_offset */
964 HOWTO (R_SH_GOTPLT32, /* type */
965 0, /* rightshift */
966 2, /* size (0 = byte, 1 = short, 2 = long) */
967 32, /* bitsize */
968 FALSE, /* pc_relative */
969 0, /* bitpos */
970 complain_overflow_bitfield, /* complain_on_overflow */
971 bfd_elf_generic_reloc, /* */
972 "R_SH_GOTPLT32", /* name */
973 FALSE, /* partial_inplace */
974 0xffffffff, /* src_mask */
975 0xffffffff, /* dst_mask */
976 FALSE), /* pcrel_offset */
978 #ifdef INCLUDE_SHMEDIA
979 /* Used in MOVI and SHORI (x & 65536). */
980 HOWTO (R_SH_GOT_LOW16, /* type */
981 0, /* rightshift */
982 2, /* size (0 = byte, 1 = short, 2 = long) */
983 64, /* bitsize */
984 FALSE, /* pc_relative */
985 10, /* bitpos */
986 complain_overflow_dont, /* complain_on_overflow */
987 bfd_elf_generic_reloc, /* special_function */
988 "R_SH_GOT_LOW16", /* name */
989 FALSE, /* partial_inplace */
990 0, /* src_mask */
991 0x3fffc00, /* dst_mask */
992 FALSE), /* pcrel_offset */
994 /* Used in MOVI and SHORI ((x >> 16) & 65536). */
995 HOWTO (R_SH_GOT_MEDLOW16, /* type */
996 16, /* rightshift */
997 2, /* size (0 = byte, 1 = short, 2 = long) */
998 64, /* bitsize */
999 FALSE, /* pc_relative */
1000 10, /* bitpos */
1001 complain_overflow_dont, /* complain_on_overflow */
1002 bfd_elf_generic_reloc, /* special_function */
1003 "R_SH_GOT_MEDLOW16", /* name */
1004 FALSE, /* partial_inplace */
1005 0, /* src_mask */
1006 0x3fffc00, /* dst_mask */
1007 FALSE), /* pcrel_offset */
1009 /* Used in MOVI and SHORI ((x >> 32) & 65536). */
1010 HOWTO (R_SH_GOT_MEDHI16, /* type */
1011 32, /* rightshift */
1012 2, /* size (0 = byte, 1 = short, 2 = long) */
1013 64, /* bitsize */
1014 FALSE, /* pc_relative */
1015 10, /* bitpos */
1016 complain_overflow_dont, /* complain_on_overflow */
1017 bfd_elf_generic_reloc, /* special_function */
1018 "R_SH_GOT_MEDHI16", /* name */
1019 FALSE, /* partial_inplace */
1020 0, /* src_mask */
1021 0x3fffc00, /* dst_mask */
1022 FALSE), /* pcrel_offset */
1024 /* Used in MOVI and SHORI ((x >> 48) & 65536). */
1025 HOWTO (R_SH_GOT_HI16, /* type */
1026 48, /* rightshift */
1027 2, /* size (0 = byte, 1 = short, 2 = long) */
1028 64, /* bitsize */
1029 FALSE, /* pc_relative */
1030 10, /* bitpos */
1031 complain_overflow_dont, /* complain_on_overflow */
1032 bfd_elf_generic_reloc, /* special_function */
1033 "R_SH_GOT_HI16", /* name */
1034 FALSE, /* partial_inplace */
1035 0, /* src_mask */
1036 0x3fffc00, /* dst_mask */
1037 FALSE), /* pcrel_offset */
1039 /* Used in MOVI and SHORI (x & 65536). */
1040 HOWTO (R_SH_GOTPLT_LOW16, /* type */
1041 0, /* rightshift */
1042 2, /* size (0 = byte, 1 = short, 2 = long) */
1043 64, /* bitsize */
1044 FALSE, /* pc_relative */
1045 10, /* bitpos */
1046 complain_overflow_dont, /* complain_on_overflow */
1047 bfd_elf_generic_reloc, /* special_function */
1048 "R_SH_GOTPLT_LOW16", /* name */
1049 FALSE, /* partial_inplace */
1050 0, /* src_mask */
1051 0x3fffc00, /* dst_mask */
1052 FALSE), /* pcrel_offset */
1054 /* Used in MOVI and SHORI ((x >> 16) & 65536). */
1055 HOWTO (R_SH_GOTPLT_MEDLOW16, /* type */
1056 16, /* rightshift */
1057 2, /* size (0 = byte, 1 = short, 2 = long) */
1058 64, /* bitsize */
1059 FALSE, /* pc_relative */
1060 10, /* bitpos */
1061 complain_overflow_dont, /* complain_on_overflow */
1062 bfd_elf_generic_reloc, /* special_function */
1063 "R_SH_GOTPLT_MEDLOW16", /* name */
1064 FALSE, /* partial_inplace */
1065 0, /* src_mask */
1066 0x3fffc00, /* dst_mask */
1067 FALSE), /* pcrel_offset */
1069 /* Used in MOVI and SHORI ((x >> 32) & 65536). */
1070 HOWTO (R_SH_GOTPLT_MEDHI16, /* type */
1071 32, /* rightshift */
1072 2, /* size (0 = byte, 1 = short, 2 = long) */
1073 64, /* bitsize */
1074 FALSE, /* pc_relative */
1075 10, /* bitpos */
1076 complain_overflow_dont, /* complain_on_overflow */
1077 bfd_elf_generic_reloc, /* special_function */
1078 "R_SH_GOTPLT_MEDHI16", /* name */
1079 FALSE, /* partial_inplace */
1080 0, /* src_mask */
1081 0x3fffc00, /* dst_mask */
1082 FALSE), /* pcrel_offset */
1084 /* Used in MOVI and SHORI ((x >> 48) & 65536). */
1085 HOWTO (R_SH_GOTPLT_HI16, /* type */
1086 48, /* rightshift */
1087 2, /* size (0 = byte, 1 = short, 2 = long) */
1088 64, /* bitsize */
1089 FALSE, /* pc_relative */
1090 10, /* bitpos */
1091 complain_overflow_dont, /* complain_on_overflow */
1092 bfd_elf_generic_reloc, /* special_function */
1093 "R_SH_GOTPLT_HI16", /* name */
1094 FALSE, /* partial_inplace */
1095 0, /* src_mask */
1096 0x3fffc00, /* dst_mask */
1097 FALSE), /* pcrel_offset */
1099 /* Used in MOVI and SHORI (x & 65536). */
1100 HOWTO (R_SH_PLT_LOW16, /* type */
1101 0, /* rightshift */
1102 2, /* size (0 = byte, 1 = short, 2 = long) */
1103 64, /* bitsize */
1104 TRUE, /* pc_relative */
1105 10, /* bitpos */
1106 complain_overflow_dont, /* complain_on_overflow */
1107 bfd_elf_generic_reloc, /* special_function */
1108 "R_SH_PLT_LOW16", /* name */
1109 FALSE, /* partial_inplace */
1110 0, /* src_mask */
1111 0x3fffc00, /* dst_mask */
1112 TRUE), /* pcrel_offset */
1114 /* Used in MOVI and SHORI ((x >> 16) & 65536). */
1115 HOWTO (R_SH_PLT_MEDLOW16, /* type */
1116 16, /* rightshift */
1117 2, /* size (0 = byte, 1 = short, 2 = long) */
1118 64, /* bitsize */
1119 TRUE, /* pc_relative */
1120 10, /* bitpos */
1121 complain_overflow_dont, /* complain_on_overflow */
1122 bfd_elf_generic_reloc, /* special_function */
1123 "R_SH_PLT_MEDLOW16", /* name */
1124 FALSE, /* partial_inplace */
1125 0, /* src_mask */
1126 0x3fffc00, /* dst_mask */
1127 TRUE), /* pcrel_offset */
1129 /* Used in MOVI and SHORI ((x >> 32) & 65536). */
1130 HOWTO (R_SH_PLT_MEDHI16, /* type */
1131 32, /* rightshift */
1132 2, /* size (0 = byte, 1 = short, 2 = long) */
1133 64, /* bitsize */
1134 TRUE, /* pc_relative */
1135 10, /* bitpos */
1136 complain_overflow_dont, /* complain_on_overflow */
1137 bfd_elf_generic_reloc, /* special_function */
1138 "R_SH_PLT_MEDHI16", /* name */
1139 FALSE, /* partial_inplace */
1140 0, /* src_mask */
1141 0x3fffc00, /* dst_mask */
1142 TRUE), /* pcrel_offset */
1144 /* Used in MOVI and SHORI ((x >> 48) & 65536). */
1145 HOWTO (R_SH_PLT_HI16, /* type */
1146 48, /* rightshift */
1147 2, /* size (0 = byte, 1 = short, 2 = long) */
1148 64, /* bitsize */
1149 TRUE, /* pc_relative */
1150 10, /* bitpos */
1151 complain_overflow_dont, /* complain_on_overflow */
1152 bfd_elf_generic_reloc, /* special_function */
1153 "R_SH_PLT_HI16", /* name */
1154 FALSE, /* partial_inplace */
1155 0, /* src_mask */
1156 0x3fffc00, /* dst_mask */
1157 TRUE), /* pcrel_offset */
1159 /* Used in MOVI and SHORI (x & 65536). */
1160 HOWTO (R_SH_GOTOFF_LOW16, /* type */
1161 0, /* rightshift */
1162 2, /* size (0 = byte, 1 = short, 2 = long) */
1163 64, /* bitsize */
1164 FALSE, /* pc_relative */
1165 10, /* bitpos */
1166 complain_overflow_dont, /* complain_on_overflow */
1167 bfd_elf_generic_reloc, /* special_function */
1168 "R_SH_GOTOFF_LOW16", /* name */
1169 FALSE, /* partial_inplace */
1170 0, /* src_mask */
1171 0x3fffc00, /* dst_mask */
1172 FALSE), /* pcrel_offset */
1174 /* Used in MOVI and SHORI ((x >> 16) & 65536). */
1175 HOWTO (R_SH_GOTOFF_MEDLOW16, /* type */
1176 16, /* rightshift */
1177 2, /* size (0 = byte, 1 = short, 2 = long) */
1178 64, /* bitsize */
1179 FALSE, /* pc_relative */
1180 10, /* bitpos */
1181 complain_overflow_dont, /* complain_on_overflow */
1182 bfd_elf_generic_reloc, /* special_function */
1183 "R_SH_GOTOFF_MEDLOW16", /* name */
1184 FALSE, /* partial_inplace */
1185 0, /* src_mask */
1186 0x3fffc00, /* dst_mask */
1187 FALSE), /* pcrel_offset */
1189 /* Used in MOVI and SHORI ((x >> 32) & 65536). */
1190 HOWTO (R_SH_GOTOFF_MEDHI16, /* type */
1191 32, /* rightshift */
1192 2, /* size (0 = byte, 1 = short, 2 = long) */
1193 64, /* bitsize */
1194 FALSE, /* pc_relative */
1195 10, /* bitpos */
1196 complain_overflow_dont, /* complain_on_overflow */
1197 bfd_elf_generic_reloc, /* special_function */
1198 "R_SH_GOTOFF_MEDHI16", /* name */
1199 FALSE, /* partial_inplace */
1200 0, /* src_mask */
1201 0x3fffc00, /* dst_mask */
1202 FALSE), /* pcrel_offset */
1204 /* Used in MOVI and SHORI ((x >> 48) & 65536). */
1205 HOWTO (R_SH_GOTOFF_HI16, /* type */
1206 48, /* rightshift */
1207 2, /* size (0 = byte, 1 = short, 2 = long) */
1208 64, /* bitsize */
1209 FALSE, /* pc_relative */
1210 10, /* bitpos */
1211 complain_overflow_dont, /* complain_on_overflow */
1212 bfd_elf_generic_reloc, /* special_function */
1213 "R_SH_GOTOFF_HI16", /* name */
1214 FALSE, /* partial_inplace */
1215 0, /* src_mask */
1216 0x3fffc00, /* dst_mask */
1217 FALSE), /* pcrel_offset */
1219 /* Used in MOVI and SHORI (x & 65536). */
1220 HOWTO (R_SH_GOTPC_LOW16, /* type */
1221 0, /* rightshift */
1222 2, /* size (0 = byte, 1 = short, 2 = long) */
1223 64, /* bitsize */
1224 TRUE, /* pc_relative */
1225 10, /* bitpos */
1226 complain_overflow_dont, /* complain_on_overflow */
1227 bfd_elf_generic_reloc, /* special_function */
1228 "R_SH_GOTPC_LOW16", /* name */
1229 FALSE, /* partial_inplace */
1230 0, /* src_mask */
1231 0x3fffc00, /* dst_mask */
1232 TRUE), /* pcrel_offset */
1234 /* Used in MOVI and SHORI ((x >> 16) & 65536). */
1235 HOWTO (R_SH_GOTPC_MEDLOW16, /* type */
1236 16, /* rightshift */
1237 2, /* size (0 = byte, 1 = short, 2 = long) */
1238 64, /* bitsize */
1239 TRUE, /* pc_relative */
1240 10, /* bitpos */
1241 complain_overflow_dont, /* complain_on_overflow */
1242 bfd_elf_generic_reloc, /* special_function */
1243 "R_SH_GOTPC_MEDLOW16", /* name */
1244 FALSE, /* partial_inplace */
1245 0, /* src_mask */
1246 0x3fffc00, /* dst_mask */
1247 TRUE), /* pcrel_offset */
1249 /* Used in MOVI and SHORI ((x >> 32) & 65536). */
1250 HOWTO (R_SH_GOTPC_MEDHI16, /* type */
1251 32, /* rightshift */
1252 2, /* size (0 = byte, 1 = short, 2 = long) */
1253 64, /* bitsize */
1254 TRUE, /* pc_relative */
1255 10, /* bitpos */
1256 complain_overflow_dont, /* complain_on_overflow */
1257 bfd_elf_generic_reloc, /* special_function */
1258 "R_SH_GOTPC_MEDHI16", /* name */
1259 FALSE, /* partial_inplace */
1260 0, /* src_mask */
1261 0x3fffc00, /* dst_mask */
1262 TRUE), /* pcrel_offset */
1264 /* Used in MOVI and SHORI ((x >> 48) & 65536). */
1265 HOWTO (R_SH_GOTPC_HI16, /* type */
1266 48, /* rightshift */
1267 2, /* size (0 = byte, 1 = short, 2 = long) */
1268 64, /* bitsize */
1269 TRUE, /* pc_relative */
1270 10, /* bitpos */
1271 complain_overflow_dont, /* complain_on_overflow */
1272 bfd_elf_generic_reloc, /* special_function */
1273 "R_SH_GOTPC_HI16", /* name */
1274 FALSE, /* partial_inplace */
1275 0, /* src_mask */
1276 0x3fffc00, /* dst_mask */
1277 TRUE), /* pcrel_offset */
1279 /* Used in LD.L, FLD.S et al. */
1280 HOWTO (R_SH_GOT10BY4, /* type */
1281 2, /* rightshift */
1282 2, /* size (0 = byte, 1 = short, 2 = long) */
1283 12, /* bitsize */
1284 FALSE, /* pc_relative */
1285 10, /* bitpos */
1286 complain_overflow_signed, /* complain_on_overflow */
1287 bfd_elf_generic_reloc, /* special_function */
1288 "R_SH_GOT10BY4", /* name */
1289 FALSE, /* partial_inplace */
1290 0, /* src_mask */
1291 0xffc00, /* dst_mask */
1292 FALSE), /* pcrel_offset */
1294 /* Used in LD.L, FLD.S et al. */
1295 HOWTO (R_SH_GOTPLT10BY4, /* type */
1296 2, /* rightshift */
1297 2, /* size (0 = byte, 1 = short, 2 = long) */
1298 12, /* bitsize */
1299 FALSE, /* pc_relative */
1300 10, /* bitpos */
1301 complain_overflow_signed, /* complain_on_overflow */
1302 bfd_elf_generic_reloc, /* special_function */
1303 "R_SH_GOTPLT10BY4", /* name */
1304 FALSE, /* partial_inplace */
1305 0, /* src_mask */
1306 0xffc00, /* dst_mask */
1307 FALSE), /* pcrel_offset */
1309 /* Used in FLD.D, FST.P et al. */
1310 HOWTO (R_SH_GOT10BY8, /* type */
1311 3, /* rightshift */
1312 2, /* size (0 = byte, 1 = short, 2 = long) */
1313 13, /* bitsize */
1314 FALSE, /* pc_relative */
1315 10, /* bitpos */
1316 complain_overflow_signed, /* complain_on_overflow */
1317 bfd_elf_generic_reloc, /* special_function */
1318 "R_SH_GOT10BY8", /* name */
1319 FALSE, /* partial_inplace */
1320 0, /* src_mask */
1321 0xffc00, /* dst_mask */
1322 FALSE), /* pcrel_offset */
1324 /* Used in FLD.D, FST.P et al. */
1325 HOWTO (R_SH_GOTPLT10BY8, /* type */
1326 3, /* rightshift */
1327 2, /* size (0 = byte, 1 = short, 2 = long) */
1328 13, /* bitsize */
1329 FALSE, /* pc_relative */
1330 10, /* bitpos */
1331 complain_overflow_signed, /* complain_on_overflow */
1332 bfd_elf_generic_reloc, /* special_function */
1333 "R_SH_GOTPLT10BY8", /* name */
1334 FALSE, /* partial_inplace */
1335 0, /* src_mask */
1336 0xffc00, /* dst_mask */
1337 FALSE), /* pcrel_offset */
1339 HOWTO (R_SH_COPY64, /* type */
1340 0, /* rightshift */
1341 4, /* size (0 = byte, 1 = short, 2 = long) */
1342 64, /* bitsize */
1343 FALSE, /* pc_relative */
1344 0, /* bitpos */
1345 complain_overflow_dont, /* complain_on_overflow */
1346 bfd_elf_generic_reloc, /* special_function */
1347 "R_SH_COPY64", /* name */
1348 FALSE, /* partial_inplace */
1349 0, /* src_mask */
1350 ((bfd_vma) 0) - 1, /* dst_mask */
1351 FALSE), /* pcrel_offset */
1353 HOWTO (R_SH_GLOB_DAT64, /* type */
1354 0, /* rightshift */
1355 4, /* size (0 = byte, 1 = short, 2 = long) */
1356 64, /* bitsize */
1357 FALSE, /* pc_relative */
1358 0, /* bitpos */
1359 complain_overflow_dont, /* complain_on_overflow */
1360 bfd_elf_generic_reloc, /* special_function */
1361 "R_SH_GLOB_DAT64", /* name */
1362 FALSE, /* partial_inplace */
1363 0, /* src_mask */
1364 ((bfd_vma) 0) - 1, /* dst_mask */
1365 FALSE), /* pcrel_offset */
1367 HOWTO (R_SH_JMP_SLOT64, /* type */
1368 0, /* rightshift */
1369 4, /* size (0 = byte, 1 = short, 2 = long) */
1370 64, /* bitsize */
1371 FALSE, /* pc_relative */
1372 0, /* bitpos */
1373 complain_overflow_dont, /* complain_on_overflow */
1374 bfd_elf_generic_reloc, /* special_function */
1375 "R_SH_JMP_SLOT64", /* name */
1376 FALSE, /* partial_inplace */
1377 0, /* src_mask */
1378 ((bfd_vma) 0) - 1, /* dst_mask */
1379 FALSE), /* pcrel_offset */
1381 HOWTO (R_SH_RELATIVE64, /* type */
1382 0, /* rightshift */
1383 4, /* size (0 = byte, 1 = short, 2 = long) */
1384 64, /* bitsize */
1385 FALSE, /* pc_relative */
1386 0, /* bitpos */
1387 complain_overflow_dont, /* complain_on_overflow */
1388 bfd_elf_generic_reloc, /* special_function */
1389 "R_SH_RELATIVE64", /* name */
1390 FALSE, /* partial_inplace */
1391 0, /* src_mask */
1392 ((bfd_vma) 0) - 1, /* dst_mask */
1393 FALSE), /* pcrel_offset */
1395 EMPTY_HOWTO (197),
1396 EMPTY_HOWTO (198),
1397 EMPTY_HOWTO (199),
1398 EMPTY_HOWTO (200),
1399 EMPTY_HOWTO (201),
1400 EMPTY_HOWTO (202),
1401 EMPTY_HOWTO (203),
1402 EMPTY_HOWTO (204),
1403 EMPTY_HOWTO (205),
1404 EMPTY_HOWTO (206),
1405 EMPTY_HOWTO (207),
1406 EMPTY_HOWTO (208),
1407 EMPTY_HOWTO (209),
1408 EMPTY_HOWTO (210),
1409 EMPTY_HOWTO (211),
1410 EMPTY_HOWTO (212),
1411 EMPTY_HOWTO (213),
1412 EMPTY_HOWTO (214),
1413 EMPTY_HOWTO (215),
1414 EMPTY_HOWTO (216),
1415 EMPTY_HOWTO (217),
1416 EMPTY_HOWTO (218),
1417 EMPTY_HOWTO (219),
1418 EMPTY_HOWTO (220),
1419 EMPTY_HOWTO (221),
1420 EMPTY_HOWTO (222),
1421 EMPTY_HOWTO (223),
1422 EMPTY_HOWTO (224),
1423 EMPTY_HOWTO (225),
1424 EMPTY_HOWTO (226),
1425 EMPTY_HOWTO (227),
1426 EMPTY_HOWTO (228),
1427 EMPTY_HOWTO (229),
1428 EMPTY_HOWTO (230),
1429 EMPTY_HOWTO (231),
1430 EMPTY_HOWTO (232),
1431 EMPTY_HOWTO (233),
1432 EMPTY_HOWTO (234),
1433 EMPTY_HOWTO (235),
1434 EMPTY_HOWTO (236),
1435 EMPTY_HOWTO (237),
1436 EMPTY_HOWTO (238),
1437 EMPTY_HOWTO (239),
1438 EMPTY_HOWTO (240),
1439 EMPTY_HOWTO (241),
1441 /* Relocations for SHmedia code. None of these are partial_inplace or
1442 use the field being relocated (except R_SH_PT_16). */
1444 /* The assembler will generate this reloc before a block of SHmedia
1445 instructions. A section should be processed as assuming it contains
1446 data, unless this reloc is seen. Note that a block of SHcompact
1447 instructions are instead preceded by R_SH_CODE.
1448 This is currently not implemented, but should be used for SHmedia
1449 linker relaxation. */
1450 HOWTO (R_SH_SHMEDIA_CODE, /* type */
1451 0, /* rightshift */
1452 1, /* size (0 = byte, 1 = short, 2 = long) */
1453 0, /* bitsize */
1454 FALSE, /* pc_relative */
1455 0, /* bitpos */
1456 complain_overflow_unsigned, /* complain_on_overflow */
1457 sh_elf_ignore_reloc, /* special_function */
1458 "R_SH_SHMEDIA_CODE", /* name */
1459 FALSE, /* partial_inplace */
1460 0, /* src_mask */
1461 0, /* dst_mask */
1462 FALSE), /* pcrel_offset */
1464 /* The assembler will generate this reloc at a PTA or PTB instruction,
1465 and the linker checks the right type of target, or changes a PTA to a
1466 PTB, if the original insn was PT. */
1467 HOWTO (R_SH_PT_16, /* type */
1468 2, /* rightshift */
1469 2, /* size (0 = byte, 1 = short, 2 = long) */
1470 18, /* bitsize */
1471 TRUE, /* pc_relative */
1472 10, /* bitpos */
1473 complain_overflow_signed, /* complain_on_overflow */
1474 bfd_elf_generic_reloc, /* special_function */
1475 "R_SH_PT_16", /* name */
1476 FALSE, /* partial_inplace */
1477 0, /* src_mask */
1478 0x3fffc00, /* dst_mask */
1479 TRUE), /* pcrel_offset */
1481 /* Used in unexpanded MOVI. */
1482 HOWTO (R_SH_IMMS16, /* type */
1483 0, /* rightshift */
1484 2, /* size (0 = byte, 1 = short, 2 = long) */
1485 16, /* bitsize */
1486 FALSE, /* pc_relative */
1487 10, /* bitpos */
1488 complain_overflow_signed, /* complain_on_overflow */
1489 bfd_elf_generic_reloc, /* special_function */
1490 "R_SH_IMMS16", /* name */
1491 FALSE, /* partial_inplace */
1492 0, /* src_mask */
1493 0x3fffc00, /* dst_mask */
1494 FALSE), /* pcrel_offset */
1496 /* Used in SHORI. */
1497 HOWTO (R_SH_IMMU16, /* type */
1498 0, /* rightshift */
1499 2, /* size (0 = byte, 1 = short, 2 = long) */
1500 16, /* bitsize */
1501 FALSE, /* pc_relative */
1502 10, /* bitpos */
1503 complain_overflow_unsigned, /* complain_on_overflow */
1504 bfd_elf_generic_reloc, /* special_function */
1505 "R_SH_IMMU16", /* name */
1506 FALSE, /* partial_inplace */
1507 0, /* src_mask */
1508 0x3fffc00, /* dst_mask */
1509 FALSE), /* pcrel_offset */
1511 /* Used in MOVI and SHORI (x & 65536). */
1512 HOWTO (R_SH_IMM_LOW16, /* type */
1513 0, /* rightshift */
1514 2, /* size (0 = byte, 1 = short, 2 = long) */
1515 64, /* bitsize */
1516 FALSE, /* pc_relative */
1517 10, /* bitpos */
1518 complain_overflow_dont, /* complain_on_overflow */
1519 bfd_elf_generic_reloc, /* special_function */
1520 "R_SH_IMM_LOW16", /* name */
1521 FALSE, /* partial_inplace */
1522 0, /* src_mask */
1523 0x3fffc00, /* dst_mask */
1524 FALSE), /* pcrel_offset */
1526 /* Used in MOVI and SHORI ((x - $) & 65536). */
1527 HOWTO (R_SH_IMM_LOW16_PCREL, /* type */
1528 0, /* rightshift */
1529 2, /* size (0 = byte, 1 = short, 2 = long) */
1530 64, /* bitsize */
1531 TRUE, /* pc_relative */
1532 10, /* bitpos */
1533 complain_overflow_dont, /* complain_on_overflow */
1534 bfd_elf_generic_reloc, /* special_function */
1535 "R_SH_IMM_LOW16_PCREL", /* name */
1536 FALSE, /* partial_inplace */
1537 0, /* src_mask */
1538 0x3fffc00, /* dst_mask */
1539 TRUE), /* pcrel_offset */
1541 /* Used in MOVI and SHORI ((x >> 16) & 65536). */
1542 HOWTO (R_SH_IMM_MEDLOW16, /* type */
1543 16, /* rightshift */
1544 2, /* size (0 = byte, 1 = short, 2 = long) */
1545 64, /* bitsize */
1546 FALSE, /* pc_relative */
1547 10, /* bitpos */
1548 complain_overflow_dont, /* complain_on_overflow */
1549 bfd_elf_generic_reloc, /* special_function */
1550 "R_SH_IMM_MEDLOW16", /* name */
1551 FALSE, /* partial_inplace */
1552 0, /* src_mask */
1553 0x3fffc00, /* dst_mask */
1554 FALSE), /* pcrel_offset */
1556 /* Used in MOVI and SHORI (((x - $) >> 16) & 65536). */
1557 HOWTO (R_SH_IMM_MEDLOW16_PCREL, /* type */
1558 16, /* rightshift */
1559 2, /* size (0 = byte, 1 = short, 2 = long) */
1560 64, /* bitsize */
1561 TRUE, /* pc_relative */
1562 10, /* bitpos */
1563 complain_overflow_dont, /* complain_on_overflow */
1564 bfd_elf_generic_reloc, /* special_function */
1565 "R_SH_IMM_MEDLOW16_PCREL", /* name */
1566 FALSE, /* partial_inplace */
1567 0, /* src_mask */
1568 0x3fffc00, /* dst_mask */
1569 TRUE), /* pcrel_offset */
1571 /* Used in MOVI and SHORI ((x >> 32) & 65536). */
1572 HOWTO (R_SH_IMM_MEDHI16, /* type */
1573 32, /* rightshift */
1574 2, /* size (0 = byte, 1 = short, 2 = long) */
1575 64, /* bitsize */
1576 FALSE, /* pc_relative */
1577 10, /* bitpos */
1578 complain_overflow_dont, /* complain_on_overflow */
1579 bfd_elf_generic_reloc, /* special_function */
1580 "R_SH_IMM_MEDHI16", /* name */
1581 FALSE, /* partial_inplace */
1582 0, /* src_mask */
1583 0x3fffc00, /* dst_mask */
1584 FALSE), /* pcrel_offset */
1586 /* Used in MOVI and SHORI (((x - $) >> 32) & 65536). */
1587 HOWTO (R_SH_IMM_MEDHI16_PCREL, /* type */
1588 32, /* rightshift */
1589 2, /* size (0 = byte, 1 = short, 2 = long) */
1590 64, /* bitsize */
1591 TRUE, /* pc_relative */
1592 10, /* bitpos */
1593 complain_overflow_dont, /* complain_on_overflow */
1594 bfd_elf_generic_reloc, /* special_function */
1595 "R_SH_IMM_MEDHI16_PCREL", /* name */
1596 FALSE, /* partial_inplace */
1597 0, /* src_mask */
1598 0x3fffc00, /* dst_mask */
1599 TRUE), /* pcrel_offset */
1601 /* Used in MOVI and SHORI ((x >> 48) & 65536). */
1602 HOWTO (R_SH_IMM_HI16, /* type */
1603 48, /* rightshift */
1604 2, /* size (0 = byte, 1 = short, 2 = long) */
1605 64, /* bitsize */
1606 FALSE, /* pc_relative */
1607 10, /* bitpos */
1608 complain_overflow_dont, /* complain_on_overflow */
1609 bfd_elf_generic_reloc, /* special_function */
1610 "R_SH_IMM_HI16", /* name */
1611 FALSE, /* partial_inplace */
1612 0, /* src_mask */
1613 0x3fffc00, /* dst_mask */
1614 FALSE), /* pcrel_offset */
1616 /* Used in MOVI and SHORI (((x - $) >> 48) & 65536). */
1617 HOWTO (R_SH_IMM_HI16_PCREL, /* type */
1618 48, /* rightshift */
1619 2, /* size (0 = byte, 1 = short, 2 = long) */
1620 64, /* bitsize */
1621 TRUE, /* pc_relative */
1622 10, /* bitpos */
1623 complain_overflow_dont, /* complain_on_overflow */
1624 bfd_elf_generic_reloc, /* special_function */
1625 "R_SH_IMM_HI16_PCREL", /* name */
1626 FALSE, /* partial_inplace */
1627 0, /* src_mask */
1628 0x3fffc00, /* dst_mask */
1629 TRUE), /* pcrel_offset */
1631 /* For the .uaquad pseudo. */
1632 HOWTO (R_SH_64, /* type */
1633 0, /* rightshift */
1634 4, /* size (0 = byte, 1 = short, 2 = long) */
1635 64, /* bitsize */
1636 FALSE, /* pc_relative */
1637 0, /* bitpos */
1638 complain_overflow_dont, /* complain_on_overflow */
1639 bfd_elf_generic_reloc, /* special_function */
1640 "R_SH_64", /* name */
1641 FALSE, /* partial_inplace */
1642 0, /* src_mask */
1643 ((bfd_vma) 0) - 1, /* dst_mask */
1644 FALSE), /* pcrel_offset */
1646 /* For the .uaquad pseudo, (x - $). */
1647 HOWTO (R_SH_64_PCREL, /* type */
1648 48, /* rightshift */
1649 2, /* size (0 = byte, 1 = short, 2 = long) */
1650 64, /* bitsize */
1651 TRUE, /* pc_relative */
1652 10, /* bitpos */
1653 complain_overflow_dont, /* complain_on_overflow */
1654 bfd_elf_generic_reloc, /* special_function */
1655 "R_SH_64_PCREL", /* name */
1656 FALSE, /* partial_inplace */
1657 0, /* src_mask */
1658 ((bfd_vma) 0) - 1, /* dst_mask */
1659 TRUE), /* pcrel_offset */
1661 #endif
1664 static bfd_reloc_status_type
1665 sh_elf_reloc_loop (int r_type ATTRIBUTE_UNUSED, bfd *input_bfd,
1666 asection *input_section, bfd_byte *contents,
1667 bfd_vma addr, asection *symbol_section,
1668 bfd_vma start, bfd_vma end)
1670 static bfd_vma last_addr;
1671 static asection *last_symbol_section;
1672 bfd_byte *start_ptr, *ptr, *last_ptr;
1673 int diff, cum_diff;
1674 bfd_signed_vma x;
1675 int insn;
1677 /* Sanity check the address. */
1678 if (addr > input_section->_raw_size)
1679 return bfd_reloc_outofrange;
1681 /* We require the start and end relocations to be processed consecutively -
1682 although we allow then to be processed forwards or backwards. */
1683 if (! last_addr)
1685 last_addr = addr;
1686 last_symbol_section = symbol_section;
1687 return bfd_reloc_ok;
1689 if (last_addr != addr)
1690 abort ();
1691 last_addr = 0;
1693 if (! symbol_section || last_symbol_section != symbol_section || end < start)
1694 return bfd_reloc_outofrange;
1696 /* Get the symbol_section contents. */
1697 if (symbol_section != input_section)
1699 if (elf_section_data (symbol_section)->this_hdr.contents != NULL)
1700 contents = elf_section_data (symbol_section)->this_hdr.contents;
1701 else
1703 contents = (bfd_byte *) bfd_malloc (symbol_section->_raw_size);
1704 if (contents == NULL)
1705 return bfd_reloc_outofrange;
1706 if (! bfd_get_section_contents (input_bfd, symbol_section, contents,
1707 (file_ptr) 0,
1708 symbol_section->_raw_size))
1710 free (contents);
1711 return bfd_reloc_outofrange;
1715 #define IS_PPI(PTR) ((bfd_get_16 (input_bfd, (PTR)) & 0xfc00) == 0xf800)
1716 start_ptr = contents + start;
1717 for (cum_diff = -6, ptr = contents + end; cum_diff < 0 && ptr > start_ptr;)
1719 for (last_ptr = ptr, ptr -= 4; ptr >= start_ptr && IS_PPI (ptr);)
1720 ptr -= 2;
1721 ptr += 2;
1722 diff = (last_ptr - ptr) >> 1;
1723 cum_diff += diff & 1;
1724 cum_diff += diff;
1726 /* Calculate the start / end values to load into rs / re minus four -
1727 so that will cancel out the four we would otherwise have to add to
1728 addr to get the value to subtract in order to get relative addressing. */
1729 if (cum_diff >= 0)
1731 start -= 4;
1732 end = (ptr + cum_diff * 2) - contents;
1734 else
1736 bfd_vma start0 = start - 4;
1738 while (start0 && IS_PPI (contents + start0))
1739 start0 -= 2;
1740 start0 = start - 2 - ((start - start0) & 2);
1741 start = start0 - cum_diff - 2;
1742 end = start0;
1745 if (contents != NULL
1746 && elf_section_data (symbol_section)->this_hdr.contents != contents)
1747 free (contents);
1749 insn = bfd_get_16 (input_bfd, contents + addr);
1751 x = (insn & 0x200 ? end : start) - addr;
1752 if (input_section != symbol_section)
1753 x += ((symbol_section->output_section->vma + symbol_section->output_offset)
1754 - (input_section->output_section->vma
1755 + input_section->output_offset));
1756 x >>= 1;
1757 if (x < -128 || x > 127)
1758 return bfd_reloc_overflow;
1760 x = (insn & ~0xff) | (x & 0xff);
1761 bfd_put_16 (input_bfd, (bfd_vma) x, contents + addr);
1763 return bfd_reloc_ok;
1766 /* This function is used for normal relocs. This used to be like the COFF
1767 function, and is almost certainly incorrect for other ELF targets. */
1769 static bfd_reloc_status_type
1770 sh_elf_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol_in,
1771 void *data, asection *input_section, bfd *output_bfd,
1772 char **error_message ATTRIBUTE_UNUSED)
1774 unsigned long insn;
1775 bfd_vma sym_value;
1776 enum elf_sh_reloc_type r_type;
1777 bfd_vma addr = reloc_entry->address;
1778 bfd_byte *hit_data = addr + (bfd_byte *) data;
1780 r_type = (enum elf_sh_reloc_type) reloc_entry->howto->type;
1782 if (output_bfd != NULL)
1784 /* Partial linking--do nothing. */
1785 reloc_entry->address += input_section->output_offset;
1786 return bfd_reloc_ok;
1789 /* Almost all relocs have to do with relaxing. If any work must be
1790 done for them, it has been done in sh_relax_section. */
1791 if (r_type == R_SH_IND12W && (symbol_in->flags & BSF_LOCAL) != 0)
1792 return bfd_reloc_ok;
1794 if (symbol_in != NULL
1795 && bfd_is_und_section (symbol_in->section))
1796 return bfd_reloc_undefined;
1798 if (bfd_is_com_section (symbol_in->section))
1799 sym_value = 0;
1800 else
1801 sym_value = (symbol_in->value +
1802 symbol_in->section->output_section->vma +
1803 symbol_in->section->output_offset);
1805 switch (r_type)
1807 case R_SH_DIR32:
1808 insn = bfd_get_32 (abfd, hit_data);
1809 insn += sym_value + reloc_entry->addend;
1810 bfd_put_32 (abfd, (bfd_vma) insn, hit_data);
1811 break;
1812 case R_SH_IND12W:
1813 insn = bfd_get_16 (abfd, hit_data);
1814 sym_value += reloc_entry->addend;
1815 sym_value -= (input_section->output_section->vma
1816 + input_section->output_offset
1817 + addr
1818 + 4);
1819 sym_value += (insn & 0xfff) << 1;
1820 if (insn & 0x800)
1821 sym_value -= 0x1000;
1822 insn = (insn & 0xf000) | (sym_value & 0xfff);
1823 bfd_put_16 (abfd, (bfd_vma) insn, hit_data);
1824 if (sym_value < (bfd_vma) -0x1000 || sym_value >= 0x1000)
1825 return bfd_reloc_overflow;
1826 break;
1827 default:
1828 abort ();
1829 break;
1832 return bfd_reloc_ok;
1835 /* This function is used for relocs which are only used for relaxing,
1836 which the linker should otherwise ignore. */
1838 static bfd_reloc_status_type
1839 sh_elf_ignore_reloc (bfd *abfd ATTRIBUTE_UNUSED, arelent *reloc_entry,
1840 asymbol *symbol ATTRIBUTE_UNUSED,
1841 void *data ATTRIBUTE_UNUSED, asection *input_section,
1842 bfd *output_bfd,
1843 char **error_message ATTRIBUTE_UNUSED)
1845 if (output_bfd != NULL)
1846 reloc_entry->address += input_section->output_offset;
1847 return bfd_reloc_ok;
1850 /* This structure is used to map BFD reloc codes to SH ELF relocs. */
1852 struct elf_reloc_map
1854 bfd_reloc_code_real_type bfd_reloc_val;
1855 unsigned char elf_reloc_val;
1858 /* An array mapping BFD reloc codes to SH ELF relocs. */
1860 static const struct elf_reloc_map sh_reloc_map[] =
1862 { BFD_RELOC_NONE, R_SH_NONE },
1863 { BFD_RELOC_32, R_SH_DIR32 },
1864 { BFD_RELOC_CTOR, R_SH_DIR32 },
1865 { BFD_RELOC_32_PCREL, R_SH_REL32 },
1866 { BFD_RELOC_SH_PCDISP8BY2, R_SH_DIR8WPN },
1867 { BFD_RELOC_SH_PCDISP12BY2, R_SH_IND12W },
1868 { BFD_RELOC_SH_PCRELIMM8BY2, R_SH_DIR8WPZ },
1869 { BFD_RELOC_SH_PCRELIMM8BY4, R_SH_DIR8WPL },
1870 { BFD_RELOC_8_PCREL, R_SH_SWITCH8 },
1871 { BFD_RELOC_SH_SWITCH16, R_SH_SWITCH16 },
1872 { BFD_RELOC_SH_SWITCH32, R_SH_SWITCH32 },
1873 { BFD_RELOC_SH_USES, R_SH_USES },
1874 { BFD_RELOC_SH_COUNT, R_SH_COUNT },
1875 { BFD_RELOC_SH_ALIGN, R_SH_ALIGN },
1876 { BFD_RELOC_SH_CODE, R_SH_CODE },
1877 { BFD_RELOC_SH_DATA, R_SH_DATA },
1878 { BFD_RELOC_SH_LABEL, R_SH_LABEL },
1879 { BFD_RELOC_VTABLE_INHERIT, R_SH_GNU_VTINHERIT },
1880 { BFD_RELOC_VTABLE_ENTRY, R_SH_GNU_VTENTRY },
1881 { BFD_RELOC_SH_LOOP_START, R_SH_LOOP_START },
1882 { BFD_RELOC_SH_LOOP_END, R_SH_LOOP_END },
1883 { BFD_RELOC_SH_TLS_GD_32, R_SH_TLS_GD_32 },
1884 { BFD_RELOC_SH_TLS_LD_32, R_SH_TLS_LD_32 },
1885 { BFD_RELOC_SH_TLS_LDO_32, R_SH_TLS_LDO_32 },
1886 { BFD_RELOC_SH_TLS_IE_32, R_SH_TLS_IE_32 },
1887 { BFD_RELOC_SH_TLS_LE_32, R_SH_TLS_LE_32 },
1888 { BFD_RELOC_SH_TLS_DTPMOD32, R_SH_TLS_DTPMOD32 },
1889 { BFD_RELOC_SH_TLS_DTPOFF32, R_SH_TLS_DTPOFF32 },
1890 { BFD_RELOC_SH_TLS_TPOFF32, R_SH_TLS_TPOFF32 },
1891 { BFD_RELOC_32_GOT_PCREL, R_SH_GOT32 },
1892 { BFD_RELOC_32_PLT_PCREL, R_SH_PLT32 },
1893 { BFD_RELOC_SH_COPY, R_SH_COPY },
1894 { BFD_RELOC_SH_GLOB_DAT, R_SH_GLOB_DAT },
1895 { BFD_RELOC_SH_JMP_SLOT, R_SH_JMP_SLOT },
1896 { BFD_RELOC_SH_RELATIVE, R_SH_RELATIVE },
1897 { BFD_RELOC_32_GOTOFF, R_SH_GOTOFF },
1898 { BFD_RELOC_SH_GOTPC, R_SH_GOTPC },
1899 { BFD_RELOC_SH_GOTPLT32, R_SH_GOTPLT32 },
1900 #ifdef INCLUDE_SHMEDIA
1901 { BFD_RELOC_SH_GOT_LOW16, R_SH_GOT_LOW16 },
1902 { BFD_RELOC_SH_GOT_MEDLOW16, R_SH_GOT_MEDLOW16 },
1903 { BFD_RELOC_SH_GOT_MEDHI16, R_SH_GOT_MEDHI16 },
1904 { BFD_RELOC_SH_GOT_HI16, R_SH_GOT_HI16 },
1905 { BFD_RELOC_SH_GOTPLT_LOW16, R_SH_GOTPLT_LOW16 },
1906 { BFD_RELOC_SH_GOTPLT_MEDLOW16, R_SH_GOTPLT_MEDLOW16 },
1907 { BFD_RELOC_SH_GOTPLT_MEDHI16, R_SH_GOTPLT_MEDHI16 },
1908 { BFD_RELOC_SH_GOTPLT_HI16, R_SH_GOTPLT_HI16 },
1909 { BFD_RELOC_SH_PLT_LOW16, R_SH_PLT_LOW16 },
1910 { BFD_RELOC_SH_PLT_MEDLOW16, R_SH_PLT_MEDLOW16 },
1911 { BFD_RELOC_SH_PLT_MEDHI16, R_SH_PLT_MEDHI16 },
1912 { BFD_RELOC_SH_PLT_HI16, R_SH_PLT_HI16 },
1913 { BFD_RELOC_SH_GOTOFF_LOW16, R_SH_GOTOFF_LOW16 },
1914 { BFD_RELOC_SH_GOTOFF_MEDLOW16, R_SH_GOTOFF_MEDLOW16 },
1915 { BFD_RELOC_SH_GOTOFF_MEDHI16, R_SH_GOTOFF_MEDHI16 },
1916 { BFD_RELOC_SH_GOTOFF_HI16, R_SH_GOTOFF_HI16 },
1917 { BFD_RELOC_SH_GOTPC_LOW16, R_SH_GOTPC_LOW16 },
1918 { BFD_RELOC_SH_GOTPC_MEDLOW16, R_SH_GOTPC_MEDLOW16 },
1919 { BFD_RELOC_SH_GOTPC_MEDHI16, R_SH_GOTPC_MEDHI16 },
1920 { BFD_RELOC_SH_GOTPC_HI16, R_SH_GOTPC_HI16 },
1921 { BFD_RELOC_SH_COPY64, R_SH_COPY64 },
1922 { BFD_RELOC_SH_GLOB_DAT64, R_SH_GLOB_DAT64 },
1923 { BFD_RELOC_SH_JMP_SLOT64, R_SH_JMP_SLOT64 },
1924 { BFD_RELOC_SH_RELATIVE64, R_SH_RELATIVE64 },
1925 { BFD_RELOC_SH_GOT10BY4, R_SH_GOT10BY4 },
1926 { BFD_RELOC_SH_GOT10BY8, R_SH_GOT10BY8 },
1927 { BFD_RELOC_SH_GOTPLT10BY4, R_SH_GOTPLT10BY4 },
1928 { BFD_RELOC_SH_GOTPLT10BY8, R_SH_GOTPLT10BY8 },
1929 { BFD_RELOC_SH_PT_16, R_SH_PT_16 },
1930 { BFD_RELOC_SH_SHMEDIA_CODE, R_SH_SHMEDIA_CODE },
1931 { BFD_RELOC_SH_IMMU5, R_SH_DIR5U },
1932 { BFD_RELOC_SH_IMMS6, R_SH_DIR6S },
1933 { BFD_RELOC_SH_IMMU6, R_SH_DIR6U },
1934 { BFD_RELOC_SH_IMMS10, R_SH_DIR10S },
1935 { BFD_RELOC_SH_IMMS10BY2, R_SH_DIR10SW },
1936 { BFD_RELOC_SH_IMMS10BY4, R_SH_DIR10SL },
1937 { BFD_RELOC_SH_IMMS10BY8, R_SH_DIR10SQ },
1938 { BFD_RELOC_SH_IMMS16, R_SH_IMMS16 },
1939 { BFD_RELOC_SH_IMMU16, R_SH_IMMU16 },
1940 { BFD_RELOC_SH_IMM_LOW16, R_SH_IMM_LOW16 },
1941 { BFD_RELOC_SH_IMM_LOW16_PCREL, R_SH_IMM_LOW16_PCREL },
1942 { BFD_RELOC_SH_IMM_MEDLOW16, R_SH_IMM_MEDLOW16 },
1943 { BFD_RELOC_SH_IMM_MEDLOW16_PCREL, R_SH_IMM_MEDLOW16_PCREL },
1944 { BFD_RELOC_SH_IMM_MEDHI16, R_SH_IMM_MEDHI16 },
1945 { BFD_RELOC_SH_IMM_MEDHI16_PCREL, R_SH_IMM_MEDHI16_PCREL },
1946 { BFD_RELOC_SH_IMM_HI16, R_SH_IMM_HI16 },
1947 { BFD_RELOC_SH_IMM_HI16_PCREL, R_SH_IMM_HI16_PCREL },
1948 { BFD_RELOC_64, R_SH_64 },
1949 { BFD_RELOC_64_PCREL, R_SH_64_PCREL },
1950 #endif /* not INCLUDE_SHMEDIA */
1953 /* Given a BFD reloc code, return the howto structure for the
1954 corresponding SH ELf reloc. */
1956 static reloc_howto_type *
1957 sh_elf_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
1958 bfd_reloc_code_real_type code)
1960 unsigned int i;
1962 for (i = 0; i < sizeof (sh_reloc_map) / sizeof (struct elf_reloc_map); i++)
1964 if (sh_reloc_map[i].bfd_reloc_val == code)
1965 return &sh_elf_howto_table[(int) sh_reloc_map[i].elf_reloc_val];
1968 return NULL;
1971 /* Given an ELF reloc, fill in the howto field of a relent. */
1973 static void
1974 sh_elf_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED, arelent *cache_ptr,
1975 Elf_Internal_Rela *dst)
1977 unsigned int r;
1979 r = ELF32_R_TYPE (dst->r_info);
1981 BFD_ASSERT (r < (unsigned int) R_SH_max);
1982 BFD_ASSERT (r < R_SH_FIRST_INVALID_RELOC || r > R_SH_LAST_INVALID_RELOC);
1983 BFD_ASSERT (r < R_SH_FIRST_INVALID_RELOC_2 || r > R_SH_LAST_INVALID_RELOC_2);
1984 BFD_ASSERT (r < R_SH_FIRST_INVALID_RELOC_3 || r > R_SH_LAST_INVALID_RELOC_3);
1985 BFD_ASSERT (r < R_SH_FIRST_INVALID_RELOC_4 || r > R_SH_LAST_INVALID_RELOC_4);
1986 BFD_ASSERT (r < R_SH_FIRST_INVALID_RELOC_5 || r > R_SH_LAST_INVALID_RELOC_5);
1988 cache_ptr->howto = &sh_elf_howto_table[r];
1991 /* This function handles relaxing for SH ELF. See the corresponding
1992 function in coff-sh.c for a description of what this does. FIXME:
1993 There is a lot of duplication here between this code and the COFF
1994 specific code. The format of relocs and symbols is wound deeply
1995 into this code, but it would still be better if the duplication
1996 could be eliminated somehow. Note in particular that although both
1997 functions use symbols like R_SH_CODE, those symbols have different
1998 values; in coff-sh.c they come from include/coff/sh.h, whereas here
1999 they come from enum elf_sh_reloc_type in include/elf/sh.h. */
2001 static bfd_boolean
2002 sh_elf_relax_section (bfd *abfd, asection *sec,
2003 struct bfd_link_info *link_info, bfd_boolean *again)
2005 Elf_Internal_Shdr *symtab_hdr;
2006 Elf_Internal_Rela *internal_relocs;
2007 bfd_boolean have_code;
2008 Elf_Internal_Rela *irel, *irelend;
2009 bfd_byte *contents = NULL;
2010 Elf_Internal_Sym *isymbuf = NULL;
2012 *again = FALSE;
2014 if (link_info->relocatable
2015 || (sec->flags & SEC_RELOC) == 0
2016 || sec->reloc_count == 0)
2017 return TRUE;
2019 #ifdef INCLUDE_SHMEDIA
2020 if (elf_section_data (sec)->this_hdr.sh_flags
2021 & (SHF_SH5_ISA32 | SHF_SH5_ISA32_MIXED))
2023 return TRUE;
2025 #endif
2027 /* If this is the first time we have been called for this section,
2028 initialize the cooked size. */
2029 if (sec->_cooked_size == 0)
2030 sec->_cooked_size = sec->_raw_size;
2032 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2034 internal_relocs = (_bfd_elf_link_read_relocs
2035 (abfd, sec, NULL, (Elf_Internal_Rela *) NULL,
2036 link_info->keep_memory));
2037 if (internal_relocs == NULL)
2038 goto error_return;
2040 have_code = FALSE;
2042 irelend = internal_relocs + sec->reloc_count;
2043 for (irel = internal_relocs; irel < irelend; irel++)
2045 bfd_vma laddr, paddr, symval;
2046 unsigned short insn;
2047 Elf_Internal_Rela *irelfn, *irelscan, *irelcount;
2048 bfd_signed_vma foff;
2050 if (ELF32_R_TYPE (irel->r_info) == (int) R_SH_CODE)
2051 have_code = TRUE;
2053 if (ELF32_R_TYPE (irel->r_info) != (int) R_SH_USES)
2054 continue;
2056 /* Get the section contents. */
2057 if (contents == NULL)
2059 if (elf_section_data (sec)->this_hdr.contents != NULL)
2060 contents = elf_section_data (sec)->this_hdr.contents;
2061 else
2063 contents = (bfd_byte *) bfd_malloc (sec->_raw_size);
2064 if (contents == NULL)
2065 goto error_return;
2067 if (! bfd_get_section_contents (abfd, sec, contents,
2068 (file_ptr) 0, sec->_raw_size))
2069 goto error_return;
2073 /* The r_addend field of the R_SH_USES reloc will point us to
2074 the register load. The 4 is because the r_addend field is
2075 computed as though it were a jump offset, which are based
2076 from 4 bytes after the jump instruction. */
2077 laddr = irel->r_offset + 4 + irel->r_addend;
2078 if (laddr >= sec->_raw_size)
2080 (*_bfd_error_handler) (_("%s: 0x%lx: warning: bad R_SH_USES offset"),
2081 bfd_archive_filename (abfd),
2082 (unsigned long) irel->r_offset);
2083 continue;
2085 insn = bfd_get_16 (abfd, contents + laddr);
2087 /* If the instruction is not mov.l NN,rN, we don't know what to
2088 do. */
2089 if ((insn & 0xf000) != 0xd000)
2091 ((*_bfd_error_handler)
2092 (_("%s: 0x%lx: warning: R_SH_USES points to unrecognized insn 0x%x"),
2093 bfd_archive_filename (abfd), (unsigned long) irel->r_offset, insn));
2094 continue;
2097 /* Get the address from which the register is being loaded. The
2098 displacement in the mov.l instruction is quadrupled. It is a
2099 displacement from four bytes after the movl instruction, but,
2100 before adding in the PC address, two least significant bits
2101 of the PC are cleared. We assume that the section is aligned
2102 on a four byte boundary. */
2103 paddr = insn & 0xff;
2104 paddr *= 4;
2105 paddr += (laddr + 4) &~ (bfd_vma) 3;
2106 if (paddr >= sec->_raw_size)
2108 ((*_bfd_error_handler)
2109 (_("%s: 0x%lx: warning: bad R_SH_USES load offset"),
2110 bfd_archive_filename (abfd), (unsigned long) irel->r_offset));
2111 continue;
2114 /* Get the reloc for the address from which the register is
2115 being loaded. This reloc will tell us which function is
2116 actually being called. */
2117 for (irelfn = internal_relocs; irelfn < irelend; irelfn++)
2118 if (irelfn->r_offset == paddr
2119 && ELF32_R_TYPE (irelfn->r_info) == (int) R_SH_DIR32)
2120 break;
2121 if (irelfn >= irelend)
2123 ((*_bfd_error_handler)
2124 (_("%s: 0x%lx: warning: could not find expected reloc"),
2125 bfd_archive_filename (abfd), (unsigned long) paddr));
2126 continue;
2129 /* Read this BFD's symbols if we haven't done so already. */
2130 if (isymbuf == NULL && symtab_hdr->sh_info != 0)
2132 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
2133 if (isymbuf == NULL)
2134 isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
2135 symtab_hdr->sh_info, 0,
2136 NULL, NULL, NULL);
2137 if (isymbuf == NULL)
2138 goto error_return;
2141 /* Get the value of the symbol referred to by the reloc. */
2142 if (ELF32_R_SYM (irelfn->r_info) < symtab_hdr->sh_info)
2144 /* A local symbol. */
2145 Elf_Internal_Sym *isym;
2147 isym = isymbuf + ELF32_R_SYM (irelfn->r_info);
2148 if (isym->st_shndx
2149 != (unsigned int) _bfd_elf_section_from_bfd_section (abfd, sec))
2151 ((*_bfd_error_handler)
2152 (_("%s: 0x%lx: warning: symbol in unexpected section"),
2153 bfd_archive_filename (abfd), (unsigned long) paddr));
2154 continue;
2157 symval = (isym->st_value
2158 + sec->output_section->vma
2159 + sec->output_offset);
2161 else
2163 unsigned long indx;
2164 struct elf_link_hash_entry *h;
2166 indx = ELF32_R_SYM (irelfn->r_info) - symtab_hdr->sh_info;
2167 h = elf_sym_hashes (abfd)[indx];
2168 BFD_ASSERT (h != NULL);
2169 if (h->root.type != bfd_link_hash_defined
2170 && h->root.type != bfd_link_hash_defweak)
2172 /* This appears to be a reference to an undefined
2173 symbol. Just ignore it--it will be caught by the
2174 regular reloc processing. */
2175 continue;
2178 symval = (h->root.u.def.value
2179 + h->root.u.def.section->output_section->vma
2180 + h->root.u.def.section->output_offset);
2183 symval += bfd_get_32 (abfd, contents + paddr);
2185 /* See if this function call can be shortened. */
2186 foff = (symval
2187 - (irel->r_offset
2188 + sec->output_section->vma
2189 + sec->output_offset
2190 + 4));
2191 if (foff < -0x1000 || foff >= 0x1000)
2193 /* After all that work, we can't shorten this function call. */
2194 continue;
2197 /* Shorten the function call. */
2199 /* For simplicity of coding, we are going to modify the section
2200 contents, the section relocs, and the BFD symbol table. We
2201 must tell the rest of the code not to free up this
2202 information. It would be possible to instead create a table
2203 of changes which have to be made, as is done in coff-mips.c;
2204 that would be more work, but would require less memory when
2205 the linker is run. */
2207 elf_section_data (sec)->relocs = internal_relocs;
2208 elf_section_data (sec)->this_hdr.contents = contents;
2209 symtab_hdr->contents = (unsigned char *) isymbuf;
2211 /* Replace the jsr with a bsr. */
2213 /* Change the R_SH_USES reloc into an R_SH_IND12W reloc, and
2214 replace the jsr with a bsr. */
2215 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irelfn->r_info), R_SH_IND12W);
2216 /* We used to test (ELF32_R_SYM (irelfn->r_info) < symtab_hdr->sh_info)
2217 here, but that only checks if the symbol is an external symbol,
2218 not if the symbol is in a different section. Besides, we need
2219 a consistent meaning for the relocation, so we just assume here that
2220 the value of the symbol is not available. */
2221 #if 0
2222 if (ELF32_R_SYM (irelfn->r_info) < symtab_hdr->sh_info)
2224 /* If this needs to be changed because of future relaxing,
2225 it will be handled here like other internal IND12W
2226 relocs. */
2227 bfd_put_16 (abfd,
2228 (bfd_vma) 0xb000 | ((foff >> 1) & 0xfff),
2229 contents + irel->r_offset);
2231 else
2232 #endif
2234 /* We can't fully resolve this yet, because the external
2235 symbol value may be changed by future relaxing. We let
2236 the final link phase handle it. */
2237 bfd_put_16 (abfd, (bfd_vma) 0xb000, contents + irel->r_offset);
2239 irel->r_addend = -4;
2241 /* See if there is another R_SH_USES reloc referring to the same
2242 register load. */
2243 for (irelscan = internal_relocs; irelscan < irelend; irelscan++)
2244 if (ELF32_R_TYPE (irelscan->r_info) == (int) R_SH_USES
2245 && laddr == irelscan->r_offset + 4 + irelscan->r_addend)
2246 break;
2247 if (irelscan < irelend)
2249 /* Some other function call depends upon this register load,
2250 and we have not yet converted that function call.
2251 Indeed, we may never be able to convert it. There is
2252 nothing else we can do at this point. */
2253 continue;
2256 /* Look for a R_SH_COUNT reloc on the location where the
2257 function address is stored. Do this before deleting any
2258 bytes, to avoid confusion about the address. */
2259 for (irelcount = internal_relocs; irelcount < irelend; irelcount++)
2260 if (irelcount->r_offset == paddr
2261 && ELF32_R_TYPE (irelcount->r_info) == (int) R_SH_COUNT)
2262 break;
2264 /* Delete the register load. */
2265 if (! sh_elf_relax_delete_bytes (abfd, sec, laddr, 2))
2266 goto error_return;
2268 /* That will change things, so, just in case it permits some
2269 other function call to come within range, we should relax
2270 again. Note that this is not required, and it may be slow. */
2271 *again = TRUE;
2273 /* Now check whether we got a COUNT reloc. */
2274 if (irelcount >= irelend)
2276 ((*_bfd_error_handler)
2277 (_("%s: 0x%lx: warning: could not find expected COUNT reloc"),
2278 bfd_archive_filename (abfd), (unsigned long) paddr));
2279 continue;
2282 /* The number of uses is stored in the r_addend field. We've
2283 just deleted one. */
2284 if (irelcount->r_addend == 0)
2286 ((*_bfd_error_handler) (_("%s: 0x%lx: warning: bad count"),
2287 bfd_archive_filename (abfd),
2288 (unsigned long) paddr));
2289 continue;
2292 --irelcount->r_addend;
2294 /* If there are no more uses, we can delete the address. Reload
2295 the address from irelfn, in case it was changed by the
2296 previous call to sh_elf_relax_delete_bytes. */
2297 if (irelcount->r_addend == 0)
2299 if (! sh_elf_relax_delete_bytes (abfd, sec, irelfn->r_offset, 4))
2300 goto error_return;
2303 /* We've done all we can with that function call. */
2306 /* Look for load and store instructions that we can align on four
2307 byte boundaries. */
2308 if ((elf_elfheader (abfd)->e_flags & EF_SH_MACH_MASK) != EF_SH4
2309 && have_code)
2311 bfd_boolean swapped;
2313 /* Get the section contents. */
2314 if (contents == NULL)
2316 if (elf_section_data (sec)->this_hdr.contents != NULL)
2317 contents = elf_section_data (sec)->this_hdr.contents;
2318 else
2320 contents = (bfd_byte *) bfd_malloc (sec->_raw_size);
2321 if (contents == NULL)
2322 goto error_return;
2324 if (! bfd_get_section_contents (abfd, sec, contents,
2325 (file_ptr) 0, sec->_raw_size))
2326 goto error_return;
2330 if (! sh_elf_align_loads (abfd, sec, internal_relocs, contents,
2331 &swapped))
2332 goto error_return;
2334 if (swapped)
2336 elf_section_data (sec)->relocs = internal_relocs;
2337 elf_section_data (sec)->this_hdr.contents = contents;
2338 symtab_hdr->contents = (unsigned char *) isymbuf;
2342 if (isymbuf != NULL
2343 && symtab_hdr->contents != (unsigned char *) isymbuf)
2345 if (! link_info->keep_memory)
2346 free (isymbuf);
2347 else
2349 /* Cache the symbols for elf_link_input_bfd. */
2350 symtab_hdr->contents = (unsigned char *) isymbuf;
2354 if (contents != NULL
2355 && elf_section_data (sec)->this_hdr.contents != contents)
2357 if (! link_info->keep_memory)
2358 free (contents);
2359 else
2361 /* Cache the section contents for elf_link_input_bfd. */
2362 elf_section_data (sec)->this_hdr.contents = contents;
2366 if (internal_relocs != NULL
2367 && elf_section_data (sec)->relocs != internal_relocs)
2368 free (internal_relocs);
2370 return TRUE;
2372 error_return:
2373 if (isymbuf != NULL
2374 && symtab_hdr->contents != (unsigned char *) isymbuf)
2375 free (isymbuf);
2376 if (contents != NULL
2377 && elf_section_data (sec)->this_hdr.contents != contents)
2378 free (contents);
2379 if (internal_relocs != NULL
2380 && elf_section_data (sec)->relocs != internal_relocs)
2381 free (internal_relocs);
2383 return FALSE;
2386 /* Delete some bytes from a section while relaxing. FIXME: There is a
2387 lot of duplication between this function and sh_relax_delete_bytes
2388 in coff-sh.c. */
2390 static bfd_boolean
2391 sh_elf_relax_delete_bytes (bfd *abfd, asection *sec, bfd_vma addr,
2392 int count)
2394 Elf_Internal_Shdr *symtab_hdr;
2395 unsigned int sec_shndx;
2396 bfd_byte *contents;
2397 Elf_Internal_Rela *irel, *irelend;
2398 Elf_Internal_Rela *irelalign;
2399 bfd_vma toaddr;
2400 Elf_Internal_Sym *isymbuf, *isym, *isymend;
2401 struct elf_link_hash_entry **sym_hashes;
2402 struct elf_link_hash_entry **end_hashes;
2403 unsigned int symcount;
2404 asection *o;
2406 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2407 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
2409 sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
2411 contents = elf_section_data (sec)->this_hdr.contents;
2413 /* The deletion must stop at the next ALIGN reloc for an aligment
2414 power larger than the number of bytes we are deleting. */
2416 irelalign = NULL;
2417 toaddr = sec->_cooked_size;
2419 irel = elf_section_data (sec)->relocs;
2420 irelend = irel + sec->reloc_count;
2421 for (; irel < irelend; irel++)
2423 if (ELF32_R_TYPE (irel->r_info) == (int) R_SH_ALIGN
2424 && irel->r_offset > addr
2425 && count < (1 << irel->r_addend))
2427 irelalign = irel;
2428 toaddr = irel->r_offset;
2429 break;
2433 /* Actually delete the bytes. */
2434 memmove (contents + addr, contents + addr + count,
2435 (size_t) (toaddr - addr - count));
2436 if (irelalign == NULL)
2437 sec->_cooked_size -= count;
2438 else
2440 int i;
2442 #define NOP_OPCODE (0x0009)
2444 BFD_ASSERT ((count & 1) == 0);
2445 for (i = 0; i < count; i += 2)
2446 bfd_put_16 (abfd, (bfd_vma) NOP_OPCODE, contents + toaddr - count + i);
2449 /* Adjust all the relocs. */
2450 for (irel = elf_section_data (sec)->relocs; irel < irelend; irel++)
2452 bfd_vma nraddr, stop;
2453 bfd_vma start = 0;
2454 int insn = 0;
2455 int off, adjust, oinsn;
2456 bfd_signed_vma voff = 0;
2457 bfd_boolean overflow;
2459 /* Get the new reloc address. */
2460 nraddr = irel->r_offset;
2461 if ((irel->r_offset > addr
2462 && irel->r_offset < toaddr)
2463 || (ELF32_R_TYPE (irel->r_info) == (int) R_SH_ALIGN
2464 && irel->r_offset == toaddr))
2465 nraddr -= count;
2467 /* See if this reloc was for the bytes we have deleted, in which
2468 case we no longer care about it. Don't delete relocs which
2469 represent addresses, though. */
2470 if (irel->r_offset >= addr
2471 && irel->r_offset < addr + count
2472 && ELF32_R_TYPE (irel->r_info) != (int) R_SH_ALIGN
2473 && ELF32_R_TYPE (irel->r_info) != (int) R_SH_CODE
2474 && ELF32_R_TYPE (irel->r_info) != (int) R_SH_DATA
2475 && ELF32_R_TYPE (irel->r_info) != (int) R_SH_LABEL)
2476 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
2477 (int) R_SH_NONE);
2479 /* If this is a PC relative reloc, see if the range it covers
2480 includes the bytes we have deleted. */
2481 switch ((enum elf_sh_reloc_type) ELF32_R_TYPE (irel->r_info))
2483 default:
2484 break;
2486 case R_SH_DIR8WPN:
2487 case R_SH_IND12W:
2488 case R_SH_DIR8WPZ:
2489 case R_SH_DIR8WPL:
2490 start = irel->r_offset;
2491 insn = bfd_get_16 (abfd, contents + nraddr);
2492 break;
2495 switch ((enum elf_sh_reloc_type) ELF32_R_TYPE (irel->r_info))
2497 default:
2498 start = stop = addr;
2499 break;
2501 case R_SH_DIR32:
2502 /* If this reloc is against a symbol defined in this
2503 section, and the symbol will not be adjusted below, we
2504 must check the addend to see it will put the value in
2505 range to be adjusted, and hence must be changed. */
2506 if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
2508 isym = isymbuf + ELF32_R_SYM (irel->r_info);
2509 if (isym->st_shndx == sec_shndx
2510 && (isym->st_value <= addr
2511 || isym->st_value >= toaddr))
2513 bfd_vma val;
2515 val = bfd_get_32 (abfd, contents + nraddr);
2516 val += isym->st_value;
2517 if (val > addr && val < toaddr)
2518 bfd_put_32 (abfd, val - count, contents + nraddr);
2521 start = stop = addr;
2522 break;
2524 case R_SH_DIR8WPN:
2525 off = insn & 0xff;
2526 if (off & 0x80)
2527 off -= 0x100;
2528 stop = (bfd_vma) ((bfd_signed_vma) start + 4 + off * 2);
2529 break;
2531 case R_SH_IND12W:
2532 off = insn & 0xfff;
2533 if (! off)
2535 /* This has been made by previous relaxation. Since the
2536 relocation will be against an external symbol, the
2537 final relocation will just do the right thing. */
2538 start = stop = addr;
2540 else
2542 if (off & 0x800)
2543 off -= 0x1000;
2544 stop = (bfd_vma) ((bfd_signed_vma) start + 4 + off * 2);
2546 /* The addend will be against the section symbol, thus
2547 for adjusting the addend, the relevant start is the
2548 start of the section.
2549 N.B. If we want to abandom in-place changes here and
2550 test directly using symbol + addend, we have to take into
2551 account that the addend has already been adjusted by -4. */
2552 if (stop > addr && stop < toaddr)
2553 irel->r_addend -= count;
2555 break;
2557 case R_SH_DIR8WPZ:
2558 off = insn & 0xff;
2559 stop = start + 4 + off * 2;
2560 break;
2562 case R_SH_DIR8WPL:
2563 off = insn & 0xff;
2564 stop = (start & ~(bfd_vma) 3) + 4 + off * 4;
2565 break;
2567 case R_SH_SWITCH8:
2568 case R_SH_SWITCH16:
2569 case R_SH_SWITCH32:
2570 /* These relocs types represent
2571 .word L2-L1
2572 The r_addend field holds the difference between the reloc
2573 address and L1. That is the start of the reloc, and
2574 adding in the contents gives us the top. We must adjust
2575 both the r_offset field and the section contents.
2576 N.B. in gas / coff bfd, the elf bfd r_addend is called r_offset,
2577 and the elf bfd r_offset is called r_vaddr. */
2579 stop = irel->r_offset;
2580 start = (bfd_vma) ((bfd_signed_vma) stop - (long) irel->r_addend);
2582 if (start > addr
2583 && start < toaddr
2584 && (stop <= addr || stop >= toaddr))
2585 irel->r_addend += count;
2586 else if (stop > addr
2587 && stop < toaddr
2588 && (start <= addr || start >= toaddr))
2589 irel->r_addend -= count;
2591 if (ELF32_R_TYPE (irel->r_info) == (int) R_SH_SWITCH16)
2592 voff = bfd_get_signed_16 (abfd, contents + nraddr);
2593 else if (ELF32_R_TYPE (irel->r_info) == (int) R_SH_SWITCH8)
2594 voff = bfd_get_8 (abfd, contents + nraddr);
2595 else
2596 voff = bfd_get_signed_32 (abfd, contents + nraddr);
2597 stop = (bfd_vma) ((bfd_signed_vma) start + voff);
2599 break;
2601 case R_SH_USES:
2602 start = irel->r_offset;
2603 stop = (bfd_vma) ((bfd_signed_vma) start
2604 + (long) irel->r_addend
2605 + 4);
2606 break;
2609 if (start > addr
2610 && start < toaddr
2611 && (stop <= addr || stop >= toaddr))
2612 adjust = count;
2613 else if (stop > addr
2614 && stop < toaddr
2615 && (start <= addr || start >= toaddr))
2616 adjust = - count;
2617 else
2618 adjust = 0;
2620 if (adjust != 0)
2622 oinsn = insn;
2623 overflow = FALSE;
2624 switch ((enum elf_sh_reloc_type) ELF32_R_TYPE (irel->r_info))
2626 default:
2627 abort ();
2628 break;
2630 case R_SH_DIR8WPN:
2631 case R_SH_DIR8WPZ:
2632 insn += adjust / 2;
2633 if ((oinsn & 0xff00) != (insn & 0xff00))
2634 overflow = TRUE;
2635 bfd_put_16 (abfd, (bfd_vma) insn, contents + nraddr);
2636 break;
2638 case R_SH_IND12W:
2639 insn += adjust / 2;
2640 if ((oinsn & 0xf000) != (insn & 0xf000))
2641 overflow = TRUE;
2642 bfd_put_16 (abfd, (bfd_vma) insn, contents + nraddr);
2643 break;
2645 case R_SH_DIR8WPL:
2646 BFD_ASSERT (adjust == count || count >= 4);
2647 if (count >= 4)
2648 insn += adjust / 4;
2649 else
2651 if ((irel->r_offset & 3) == 0)
2652 ++insn;
2654 if ((oinsn & 0xff00) != (insn & 0xff00))
2655 overflow = TRUE;
2656 bfd_put_16 (abfd, (bfd_vma) insn, contents + nraddr);
2657 break;
2659 case R_SH_SWITCH8:
2660 voff += adjust;
2661 if (voff < 0 || voff >= 0xff)
2662 overflow = TRUE;
2663 bfd_put_8 (abfd, voff, contents + nraddr);
2664 break;
2666 case R_SH_SWITCH16:
2667 voff += adjust;
2668 if (voff < - 0x8000 || voff >= 0x8000)
2669 overflow = TRUE;
2670 bfd_put_signed_16 (abfd, (bfd_vma) voff, contents + nraddr);
2671 break;
2673 case R_SH_SWITCH32:
2674 voff += adjust;
2675 bfd_put_signed_32 (abfd, (bfd_vma) voff, contents + nraddr);
2676 break;
2678 case R_SH_USES:
2679 irel->r_addend += adjust;
2680 break;
2683 if (overflow)
2685 ((*_bfd_error_handler)
2686 (_("%s: 0x%lx: fatal: reloc overflow while relaxing"),
2687 bfd_archive_filename (abfd), (unsigned long) irel->r_offset));
2688 bfd_set_error (bfd_error_bad_value);
2689 return FALSE;
2693 irel->r_offset = nraddr;
2696 /* Look through all the other sections. If there contain any IMM32
2697 relocs against internal symbols which we are not going to adjust
2698 below, we may need to adjust the addends. */
2699 for (o = abfd->sections; o != NULL; o = o->next)
2701 Elf_Internal_Rela *internal_relocs;
2702 Elf_Internal_Rela *irelscan, *irelscanend;
2703 bfd_byte *ocontents;
2705 if (o == sec
2706 || (o->flags & SEC_RELOC) == 0
2707 || o->reloc_count == 0)
2708 continue;
2710 /* We always cache the relocs. Perhaps, if info->keep_memory is
2711 FALSE, we should free them, if we are permitted to, when we
2712 leave sh_coff_relax_section. */
2713 internal_relocs = (_bfd_elf_link_read_relocs
2714 (abfd, o, NULL, (Elf_Internal_Rela *) NULL, TRUE));
2715 if (internal_relocs == NULL)
2716 return FALSE;
2718 ocontents = NULL;
2719 irelscanend = internal_relocs + o->reloc_count;
2720 for (irelscan = internal_relocs; irelscan < irelscanend; irelscan++)
2722 /* Dwarf line numbers use R_SH_SWITCH32 relocs. */
2723 if (ELF32_R_TYPE (irelscan->r_info) == (int) R_SH_SWITCH32)
2725 bfd_vma start, stop;
2726 bfd_signed_vma voff;
2728 if (ocontents == NULL)
2730 if (elf_section_data (o)->this_hdr.contents != NULL)
2731 ocontents = elf_section_data (o)->this_hdr.contents;
2732 else
2734 /* We always cache the section contents.
2735 Perhaps, if info->keep_memory is FALSE, we
2736 should free them, if we are permitted to,
2737 when we leave sh_coff_relax_section. */
2738 ocontents = (bfd_byte *) bfd_malloc (o->_raw_size);
2739 if (ocontents == NULL)
2740 return FALSE;
2741 if (! bfd_get_section_contents (abfd, o, ocontents,
2742 (file_ptr) 0,
2743 o->_raw_size))
2744 return FALSE;
2745 elf_section_data (o)->this_hdr.contents = ocontents;
2749 stop = irelscan->r_offset;
2750 start
2751 = (bfd_vma) ((bfd_signed_vma) stop - (long) irelscan->r_addend);
2753 /* STOP is in a different section, so it won't change. */
2754 if (start > addr && start < toaddr)
2755 irelscan->r_addend += count;
2757 voff = bfd_get_signed_32 (abfd, ocontents + irelscan->r_offset);
2758 stop = (bfd_vma) ((bfd_signed_vma) start + voff);
2760 if (start > addr
2761 && start < toaddr
2762 && (stop <= addr || stop >= toaddr))
2763 bfd_put_signed_32 (abfd, (bfd_vma) voff + count,
2764 ocontents + irelscan->r_offset);
2765 else if (stop > addr
2766 && stop < toaddr
2767 && (start <= addr || start >= toaddr))
2768 bfd_put_signed_32 (abfd, (bfd_vma) voff - count,
2769 ocontents + irelscan->r_offset);
2772 if (ELF32_R_TYPE (irelscan->r_info) != (int) R_SH_DIR32)
2773 continue;
2775 if (ELF32_R_SYM (irelscan->r_info) >= symtab_hdr->sh_info)
2776 continue;
2779 isym = isymbuf + ELF32_R_SYM (irelscan->r_info);
2780 if (isym->st_shndx == sec_shndx
2781 && (isym->st_value <= addr
2782 || isym->st_value >= toaddr))
2784 bfd_vma val;
2786 if (ocontents == NULL)
2788 if (elf_section_data (o)->this_hdr.contents != NULL)
2789 ocontents = elf_section_data (o)->this_hdr.contents;
2790 else
2792 /* We always cache the section contents.
2793 Perhaps, if info->keep_memory is FALSE, we
2794 should free them, if we are permitted to,
2795 when we leave sh_coff_relax_section. */
2796 ocontents = (bfd_byte *) bfd_malloc (o->_raw_size);
2797 if (ocontents == NULL)
2798 return FALSE;
2799 if (! bfd_get_section_contents (abfd, o, ocontents,
2800 (file_ptr) 0,
2801 o->_raw_size))
2802 return FALSE;
2803 elf_section_data (o)->this_hdr.contents = ocontents;
2807 val = bfd_get_32 (abfd, ocontents + irelscan->r_offset);
2808 val += isym->st_value;
2809 if (val > addr && val < toaddr)
2810 bfd_put_32 (abfd, val - count,
2811 ocontents + irelscan->r_offset);
2816 /* Adjust the local symbols defined in this section. */
2817 isymend = isymbuf + symtab_hdr->sh_info;
2818 for (isym = isymbuf; isym < isymend; isym++)
2820 if (isym->st_shndx == sec_shndx
2821 && isym->st_value > addr
2822 && isym->st_value < toaddr)
2823 isym->st_value -= count;
2826 /* Now adjust the global symbols defined in this section. */
2827 symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
2828 - symtab_hdr->sh_info);
2829 sym_hashes = elf_sym_hashes (abfd);
2830 end_hashes = sym_hashes + symcount;
2831 for (; sym_hashes < end_hashes; sym_hashes++)
2833 struct elf_link_hash_entry *sym_hash = *sym_hashes;
2834 if ((sym_hash->root.type == bfd_link_hash_defined
2835 || sym_hash->root.type == bfd_link_hash_defweak)
2836 && sym_hash->root.u.def.section == sec
2837 && sym_hash->root.u.def.value > addr
2838 && sym_hash->root.u.def.value < toaddr)
2840 sym_hash->root.u.def.value -= count;
2844 /* See if we can move the ALIGN reloc forward. We have adjusted
2845 r_offset for it already. */
2846 if (irelalign != NULL)
2848 bfd_vma alignto, alignaddr;
2850 alignto = BFD_ALIGN (toaddr, 1 << irelalign->r_addend);
2851 alignaddr = BFD_ALIGN (irelalign->r_offset,
2852 1 << irelalign->r_addend);
2853 if (alignto != alignaddr)
2855 /* Tail recursion. */
2856 return sh_elf_relax_delete_bytes (abfd, sec, alignaddr,
2857 (int) (alignto - alignaddr));
2861 return TRUE;
2864 /* Look for loads and stores which we can align to four byte
2865 boundaries. This is like sh_align_loads in coff-sh.c. */
2867 static bfd_boolean
2868 sh_elf_align_loads (bfd *abfd ATTRIBUTE_UNUSED, asection *sec,
2869 Elf_Internal_Rela *internal_relocs,
2870 bfd_byte *contents ATTRIBUTE_UNUSED,
2871 bfd_boolean *pswapped)
2873 Elf_Internal_Rela *irel, *irelend;
2874 bfd_vma *labels = NULL;
2875 bfd_vma *label, *label_end;
2876 bfd_size_type amt;
2878 *pswapped = FALSE;
2880 irelend = internal_relocs + sec->reloc_count;
2882 /* Get all the addresses with labels on them. */
2883 amt = sec->reloc_count;
2884 amt *= sizeof (bfd_vma);
2885 labels = (bfd_vma *) bfd_malloc (amt);
2886 if (labels == NULL)
2887 goto error_return;
2888 label_end = labels;
2889 for (irel = internal_relocs; irel < irelend; irel++)
2891 if (ELF32_R_TYPE (irel->r_info) == (int) R_SH_LABEL)
2893 *label_end = irel->r_offset;
2894 ++label_end;
2898 /* Note that the assembler currently always outputs relocs in
2899 address order. If that ever changes, this code will need to sort
2900 the label values and the relocs. */
2902 label = labels;
2904 for (irel = internal_relocs; irel < irelend; irel++)
2906 bfd_vma start, stop;
2908 if (ELF32_R_TYPE (irel->r_info) != (int) R_SH_CODE)
2909 continue;
2911 start = irel->r_offset;
2913 for (irel++; irel < irelend; irel++)
2914 if (ELF32_R_TYPE (irel->r_info) == (int) R_SH_DATA)
2915 break;
2916 if (irel < irelend)
2917 stop = irel->r_offset;
2918 else
2919 stop = sec->_cooked_size;
2921 if (! _bfd_sh_align_load_span (abfd, sec, contents, sh_elf_swap_insns,
2922 internal_relocs, &label,
2923 label_end, start, stop, pswapped))
2924 goto error_return;
2927 free (labels);
2929 return TRUE;
2931 error_return:
2932 if (labels != NULL)
2933 free (labels);
2934 return FALSE;
2937 /* Swap two SH instructions. This is like sh_swap_insns in coff-sh.c. */
2939 static bfd_boolean
2940 sh_elf_swap_insns (bfd *abfd, asection *sec, void *relocs,
2941 bfd_byte *contents, bfd_vma addr)
2943 Elf_Internal_Rela *internal_relocs = (Elf_Internal_Rela *) relocs;
2944 unsigned short i1, i2;
2945 Elf_Internal_Rela *irel, *irelend;
2947 /* Swap the instructions themselves. */
2948 i1 = bfd_get_16 (abfd, contents + addr);
2949 i2 = bfd_get_16 (abfd, contents + addr + 2);
2950 bfd_put_16 (abfd, (bfd_vma) i2, contents + addr);
2951 bfd_put_16 (abfd, (bfd_vma) i1, contents + addr + 2);
2953 /* Adjust all reloc addresses. */
2954 irelend = internal_relocs + sec->reloc_count;
2955 for (irel = internal_relocs; irel < irelend; irel++)
2957 enum elf_sh_reloc_type type;
2958 int add;
2960 /* There are a few special types of relocs that we don't want to
2961 adjust. These relocs do not apply to the instruction itself,
2962 but are only associated with the address. */
2963 type = (enum elf_sh_reloc_type) ELF32_R_TYPE (irel->r_info);
2964 if (type == R_SH_ALIGN
2965 || type == R_SH_CODE
2966 || type == R_SH_DATA
2967 || type == R_SH_LABEL)
2968 continue;
2970 /* If an R_SH_USES reloc points to one of the addresses being
2971 swapped, we must adjust it. It would be incorrect to do this
2972 for a jump, though, since we want to execute both
2973 instructions after the jump. (We have avoided swapping
2974 around a label, so the jump will not wind up executing an
2975 instruction it shouldn't). */
2976 if (type == R_SH_USES)
2978 bfd_vma off;
2980 off = irel->r_offset + 4 + irel->r_addend;
2981 if (off == addr)
2982 irel->r_offset += 2;
2983 else if (off == addr + 2)
2984 irel->r_offset -= 2;
2987 if (irel->r_offset == addr)
2989 irel->r_offset += 2;
2990 add = -2;
2992 else if (irel->r_offset == addr + 2)
2994 irel->r_offset -= 2;
2995 add = 2;
2997 else
2998 add = 0;
3000 if (add != 0)
3002 bfd_byte *loc;
3003 unsigned short insn, oinsn;
3004 bfd_boolean overflow;
3006 loc = contents + irel->r_offset;
3007 overflow = FALSE;
3008 switch (type)
3010 default:
3011 break;
3013 case R_SH_DIR8WPN:
3014 case R_SH_DIR8WPZ:
3015 insn = bfd_get_16 (abfd, loc);
3016 oinsn = insn;
3017 insn += add / 2;
3018 if ((oinsn & 0xff00) != (insn & 0xff00))
3019 overflow = TRUE;
3020 bfd_put_16 (abfd, (bfd_vma) insn, loc);
3021 break;
3023 case R_SH_IND12W:
3024 insn = bfd_get_16 (abfd, loc);
3025 oinsn = insn;
3026 insn += add / 2;
3027 if ((oinsn & 0xf000) != (insn & 0xf000))
3028 overflow = TRUE;
3029 bfd_put_16 (abfd, (bfd_vma) insn, loc);
3030 break;
3032 case R_SH_DIR8WPL:
3033 /* This reloc ignores the least significant 3 bits of
3034 the program counter before adding in the offset.
3035 This means that if ADDR is at an even address, the
3036 swap will not affect the offset. If ADDR is an at an
3037 odd address, then the instruction will be crossing a
3038 four byte boundary, and must be adjusted. */
3039 if ((addr & 3) != 0)
3041 insn = bfd_get_16 (abfd, loc);
3042 oinsn = insn;
3043 insn += add / 2;
3044 if ((oinsn & 0xff00) != (insn & 0xff00))
3045 overflow = TRUE;
3046 bfd_put_16 (abfd, (bfd_vma) insn, loc);
3049 break;
3052 if (overflow)
3054 ((*_bfd_error_handler)
3055 (_("%s: 0x%lx: fatal: reloc overflow while relaxing"),
3056 bfd_archive_filename (abfd), (unsigned long) irel->r_offset));
3057 bfd_set_error (bfd_error_bad_value);
3058 return FALSE;
3063 return TRUE;
3066 #ifdef INCLUDE_SHMEDIA
3068 /* The size in bytes of an entry in the procedure linkage table. */
3070 #define PLT_ENTRY_SIZE 64
3072 /* First entry in an absolute procedure linkage table look like this. */
3074 static const bfd_byte elf_sh_plt0_entry_be[PLT_ENTRY_SIZE] =
3076 0xcc, 0x00, 0x01, 0x10, /* movi .got.plt >> 16, r17 */
3077 0xc8, 0x00, 0x01, 0x10, /* shori .got.plt & 65535, r17 */
3078 0x89, 0x10, 0x09, 0x90, /* ld.l r17, 8, r25 */
3079 0x6b, 0xf1, 0x66, 0x00, /* ptabs r25, tr0 */
3080 0x89, 0x10, 0x05, 0x10, /* ld.l r17, 4, r17 */
3081 0x44, 0x01, 0xff, 0xf0, /* blink tr0, r63 */
3082 0x6f, 0xf0, 0xff, 0xf0, /* nop */
3083 0x6f, 0xf0, 0xff, 0xf0, /* nop */
3084 0x6f, 0xf0, 0xff, 0xf0, /* nop */
3085 0x6f, 0xf0, 0xff, 0xf0, /* nop */
3086 0x6f, 0xf0, 0xff, 0xf0, /* nop */
3087 0x6f, 0xf0, 0xff, 0xf0, /* nop */
3088 0x6f, 0xf0, 0xff, 0xf0, /* nop */
3089 0x6f, 0xf0, 0xff, 0xf0, /* nop */
3090 0x6f, 0xf0, 0xff, 0xf0, /* nop */
3091 0x6f, 0xf0, 0xff, 0xf0, /* nop */
3094 static const bfd_byte elf_sh_plt0_entry_le[PLT_ENTRY_SIZE] =
3096 0x10, 0x01, 0x00, 0xcc, /* movi .got.plt >> 16, r17 */
3097 0x10, 0x01, 0x00, 0xc8, /* shori .got.plt & 65535, r17 */
3098 0x90, 0x09, 0x10, 0x89, /* ld.l r17, 8, r25 */
3099 0x00, 0x66, 0xf1, 0x6b, /* ptabs r25, tr0 */
3100 0x10, 0x05, 0x10, 0x89, /* ld.l r17, 4, r17 */
3101 0xf0, 0xff, 0x01, 0x44, /* blink tr0, r63 */
3102 0xf0, 0xff, 0xf0, 0x6f, /* nop */
3103 0xf0, 0xff, 0xf0, 0x6f, /* nop */
3104 0xf0, 0xff, 0xf0, 0x6f, /* nop */
3105 0xf0, 0xff, 0xf0, 0x6f, /* nop */
3106 0xf0, 0xff, 0xf0, 0x6f, /* nop */
3107 0xf0, 0xff, 0xf0, 0x6f, /* nop */
3108 0xf0, 0xff, 0xf0, 0x6f, /* nop */
3109 0xf0, 0xff, 0xf0, 0x6f, /* nop */
3110 0xf0, 0xff, 0xf0, 0x6f, /* nop */
3111 0xf0, 0xff, 0xf0, 0x6f, /* nop */
3114 /* Sebsequent entries in an absolute procedure linkage table look like
3115 this. */
3117 static const bfd_byte elf_sh_plt_entry_be[PLT_ENTRY_SIZE] =
3119 0xcc, 0x00, 0x01, 0x90, /* movi nameN-in-GOT >> 16, r25 */
3120 0xc8, 0x00, 0x01, 0x90, /* shori nameN-in-GOT & 65535, r25 */
3121 0x89, 0x90, 0x01, 0x90, /* ld.l r25, 0, r25 */
3122 0x6b, 0xf1, 0x66, 0x00, /* ptabs r25, tr0 */
3123 0x44, 0x01, 0xff, 0xf0, /* blink tr0, r63 */
3124 0x6f, 0xf0, 0xff, 0xf0, /* nop */
3125 0x6f, 0xf0, 0xff, 0xf0, /* nop */
3126 0x6f, 0xf0, 0xff, 0xf0, /* nop */
3127 0xcc, 0x00, 0x01, 0x90, /* movi .PLT0 >> 16, r25 */
3128 0xc8, 0x00, 0x01, 0x90, /* shori .PLT0 & 65535, r25 */
3129 0x6b, 0xf1, 0x66, 0x00, /* ptabs r25, tr0 */
3130 0xcc, 0x00, 0x01, 0x50, /* movi reloc-offset >> 16, r21 */
3131 0xc8, 0x00, 0x01, 0x50, /* shori reloc-offset & 65535, r21 */
3132 0x44, 0x01, 0xff, 0xf0, /* blink tr0, r63 */
3133 0x6f, 0xf0, 0xff, 0xf0, /* nop */
3134 0x6f, 0xf0, 0xff, 0xf0, /* nop */
3137 static const bfd_byte elf_sh_plt_entry_le[PLT_ENTRY_SIZE] =
3139 0x90, 0x01, 0x00, 0xcc, /* movi nameN-in-GOT >> 16, r25 */
3140 0x90, 0x01, 0x00, 0xc8, /* shori nameN-in-GOT & 65535, r25 */
3141 0x90, 0x01, 0x90, 0x89, /* ld.l r25, 0, r25 */
3142 0x00, 0x66, 0xf1, 0x6b, /* ptabs r25, tr0 */
3143 0xf0, 0xff, 0x01, 0x44, /* blink tr0, r63 */
3144 0xf0, 0xff, 0xf0, 0x6f, /* nop */
3145 0xf0, 0xff, 0xf0, 0x6f, /* nop */
3146 0xf0, 0xff, 0xf0, 0x6f, /* nop */
3147 0x90, 0x01, 0x00, 0xcc, /* movi .PLT0 >> 16, r25 */
3148 0x90, 0x01, 0x00, 0xc8, /* shori .PLT0 & 65535, r25 */
3149 0x00, 0x66, 0xf1, 0x6b, /* ptabs r25, tr0 */
3150 0x50, 0x01, 0x00, 0xcc, /* movi reloc-offset >> 16, r21 */
3151 0x50, 0x01, 0x00, 0xc8, /* shori reloc-offset & 65535, r21 */
3152 0xf0, 0xff, 0x01, 0x44, /* blink tr0, r63 */
3153 0xf0, 0xff, 0xf0, 0x6f, /* nop */
3154 0xf0, 0xff, 0xf0, 0x6f, /* nop */
3157 /* Entries in a PIC procedure linkage table look like this. */
3159 static const bfd_byte elf_sh_pic_plt_entry_be[PLT_ENTRY_SIZE] =
3161 0xcc, 0x00, 0x01, 0x90, /* movi nameN@GOT >> 16, r25 */
3162 0xc8, 0x00, 0x01, 0x90, /* shori nameN@GOT & 65535, r25 */
3163 0x40, 0xc2, 0x65, 0x90, /* ldx.l r12, r25, r25 */
3164 0x6b, 0xf1, 0x66, 0x00, /* ptabs r25, tr0 */
3165 0x44, 0x01, 0xff, 0xf0, /* blink tr0, r63 */
3166 0x6f, 0xf0, 0xff, 0xf0, /* nop */
3167 0x6f, 0xf0, 0xff, 0xf0, /* nop */
3168 0x6f, 0xf0, 0xff, 0xf0, /* nop */
3169 0xce, 0x00, 0x01, 0x10, /* movi -GOT_BIAS, r17 */
3170 0x00, 0xc8, 0x45, 0x10, /* add.l r12, r17, r17 */
3171 0x89, 0x10, 0x09, 0x90, /* ld.l r17, 8, r25 */
3172 0x6b, 0xf1, 0x66, 0x00, /* ptabs r25, tr0 */
3173 0x89, 0x10, 0x05, 0x10, /* ld.l r17, 4, r17 */
3174 0xcc, 0x00, 0x01, 0x50, /* movi reloc-offset >> 16, r21 */
3175 0xc8, 0x00, 0x01, 0x50, /* shori reloc-offset & 65535, r21 */
3176 0x44, 0x01, 0xff, 0xf0, /* blink tr0, r63 */
3179 static const bfd_byte elf_sh_pic_plt_entry_le[PLT_ENTRY_SIZE] =
3181 0x90, 0x01, 0x00, 0xcc, /* movi nameN@GOT >> 16, r25 */
3182 0x90, 0x01, 0x00, 0xc8, /* shori nameN@GOT & 65535, r25 */
3183 0x90, 0x65, 0xc2, 0x40, /* ldx.l r12, r25, r25 */
3184 0x00, 0x66, 0xf1, 0x6b, /* ptabs r25, tr0 */
3185 0xf0, 0xff, 0x01, 0x44, /* blink tr0, r63 */
3186 0xf0, 0xff, 0xf0, 0x6f, /* nop */
3187 0xf0, 0xff, 0xf0, 0x6f, /* nop */
3188 0xf0, 0xff, 0xf0, 0x6f, /* nop */
3189 0x10, 0x01, 0x00, 0xce, /* movi -GOT_BIAS, r17 */
3190 0x10, 0x45, 0xc8, 0x00, /* add.l r12, r17, r17 */
3191 0x90, 0x09, 0x10, 0x89, /* ld.l r17, 8, r25 */
3192 0x00, 0x66, 0xf1, 0x6b, /* ptabs r25, tr0 */
3193 0x10, 0x05, 0x10, 0x89, /* ld.l r17, 4, r17 */
3194 0x50, 0x01, 0x00, 0xcc, /* movi reloc-offset >> 16, r21 */
3195 0x50, 0x01, 0x00, 0xc8, /* shori reloc-offset & 65535, r21 */
3196 0xf0, 0xff, 0x01, 0x44, /* blink tr0, r63 */
3199 static const bfd_byte *elf_sh_plt0_entry;
3200 static const bfd_byte *elf_sh_plt_entry;
3201 static const bfd_byte *elf_sh_pic_plt_entry;
3203 /* Return size of a PLT entry. */
3204 #define elf_sh_sizeof_plt(info) PLT_ENTRY_SIZE
3206 /* Return offset of the PLT0 address in an absolute PLT entry. */
3207 #define elf_sh_plt_plt0_offset(info) 32
3209 /* Return offset of the linker in PLT0 entry. */
3210 #define elf_sh_plt0_gotplt_offset(info) 0
3212 /* Return offset of the trampoline in PLT entry */
3213 #define elf_sh_plt_temp_offset(info) 33 /* Add one because it's SHmedia. */
3215 /* Return offset of the symbol in PLT entry. */
3216 #define elf_sh_plt_symbol_offset(info) 0
3218 /* Return offset of the relocation in PLT entry. */
3219 #define elf_sh_plt_reloc_offset(info) (info->shared ? 52 : 44)
3221 inline static void
3222 movi_shori_putval (bfd *output_bfd, unsigned long value, char *addr)
3224 bfd_put_32 (output_bfd,
3225 bfd_get_32 (output_bfd, addr)
3226 | ((value >> 6) & 0x3fffc00),
3227 addr);
3228 bfd_put_32 (output_bfd,
3229 bfd_get_32 (output_bfd, addr + 4)
3230 | ((value << 10) & 0x3fffc00),
3231 addr + 4);
3234 #else
3235 /* The size in bytes of an entry in the procedure linkage table. */
3237 #define PLT_ENTRY_SIZE 28
3239 /* First entry in an absolute procedure linkage table look like this. */
3241 #if 1
3242 /* Note - this code has been "optimised" not to use r2. r2 is used by
3243 GCC to return the address of large strutcures, so it should not be
3244 corrupted here. This does mean however, that this PLT does not conform
3245 to the SH PIC ABI. That spec says that r0 contains the type of the PLT
3246 and r2 contains the GOT id. This version stores the GOT id in r0 and
3247 ignores the type. Loaders can easily detect this difference however,
3248 since the type will always be 0 or 8, and the GOT ids will always be
3249 greater than or equal to 12. */
3250 static const bfd_byte elf_sh_plt0_entry_be[PLT_ENTRY_SIZE] =
3252 0xd0, 0x05, /* mov.l 2f,r0 */
3253 0x60, 0x02, /* mov.l @r0,r0 */
3254 0x2f, 0x06, /* mov.l r0,@-r15 */
3255 0xd0, 0x03, /* mov.l 1f,r0 */
3256 0x60, 0x02, /* mov.l @r0,r0 */
3257 0x40, 0x2b, /* jmp @r0 */
3258 0x60, 0xf6, /* mov.l @r15+,r0 */
3259 0x00, 0x09, /* nop */
3260 0x00, 0x09, /* nop */
3261 0x00, 0x09, /* nop */
3262 0, 0, 0, 0, /* 1: replaced with address of .got.plt + 8. */
3263 0, 0, 0, 0, /* 2: replaced with address of .got.plt + 4. */
3266 static const bfd_byte elf_sh_plt0_entry_le[PLT_ENTRY_SIZE] =
3268 0x05, 0xd0, /* mov.l 2f,r0 */
3269 0x02, 0x60, /* mov.l @r0,r0 */
3270 0x06, 0x2f, /* mov.l r0,@-r15 */
3271 0x03, 0xd0, /* mov.l 1f,r0 */
3272 0x02, 0x60, /* mov.l @r0,r0 */
3273 0x2b, 0x40, /* jmp @r0 */
3274 0xf6, 0x60, /* mov.l @r15+,r0 */
3275 0x09, 0x00, /* nop */
3276 0x09, 0x00, /* nop */
3277 0x09, 0x00, /* nop */
3278 0, 0, 0, 0, /* 1: replaced with address of .got.plt + 8. */
3279 0, 0, 0, 0, /* 2: replaced with address of .got.plt + 4. */
3282 /* Sebsequent entries in an absolute procedure linkage table look like
3283 this. */
3285 static const bfd_byte elf_sh_plt_entry_be[PLT_ENTRY_SIZE] =
3287 0xd0, 0x04, /* mov.l 1f,r0 */
3288 0x60, 0x02, /* mov.l @r0,r0 */
3289 0xd1, 0x02, /* mov.l 0f,r1 */
3290 0x40, 0x2b, /* jmp @r0 */
3291 0x60, 0x13, /* mov r1,r0 */
3292 0xd1, 0x03, /* mov.l 2f,r1 */
3293 0x40, 0x2b, /* jmp @r0 */
3294 0x00, 0x09, /* nop */
3295 0, 0, 0, 0, /* 0: replaced with address of .PLT0. */
3296 0, 0, 0, 0, /* 1: replaced with address of this symbol in .got. */
3297 0, 0, 0, 0, /* 2: replaced with offset into relocation table. */
3300 static const bfd_byte elf_sh_plt_entry_le[PLT_ENTRY_SIZE] =
3302 0x04, 0xd0, /* mov.l 1f,r0 */
3303 0x02, 0x60, /* mov.l @r0,r0 */
3304 0x02, 0xd1, /* mov.l 0f,r1 */
3305 0x2b, 0x40, /* jmp @r0 */
3306 0x13, 0x60, /* mov r1,r0 */
3307 0x03, 0xd1, /* mov.l 2f,r1 */
3308 0x2b, 0x40, /* jmp @r0 */
3309 0x09, 0x00, /* nop */
3310 0, 0, 0, 0, /* 0: replaced with address of .PLT0. */
3311 0, 0, 0, 0, /* 1: replaced with address of this symbol in .got. */
3312 0, 0, 0, 0, /* 2: replaced with offset into relocation table. */
3315 /* Entries in a PIC procedure linkage table look like this. */
3317 static const bfd_byte elf_sh_pic_plt_entry_be[PLT_ENTRY_SIZE] =
3319 0xd0, 0x04, /* mov.l 1f,r0 */
3320 0x00, 0xce, /* mov.l @(r0,r12),r0 */
3321 0x40, 0x2b, /* jmp @r0 */
3322 0x00, 0x09, /* nop */
3323 0x50, 0xc2, /* mov.l @(8,r12),r0 */
3324 0xd1, 0x03, /* mov.l 2f,r1 */
3325 0x40, 0x2b, /* jmp @r0 */
3326 0x50, 0xc1, /* mov.l @(4,r12),r0 */
3327 0x00, 0x09, /* nop */
3328 0x00, 0x09, /* nop */
3329 0, 0, 0, 0, /* 1: replaced with address of this symbol in .got. */
3330 0, 0, 0, 0 /* 2: replaced with offset into relocation table. */
3333 static const bfd_byte elf_sh_pic_plt_entry_le[PLT_ENTRY_SIZE] =
3335 0x04, 0xd0, /* mov.l 1f,r0 */
3336 0xce, 0x00, /* mov.l @(r0,r12),r0 */
3337 0x2b, 0x40, /* jmp @r0 */
3338 0x09, 0x00, /* nop */
3339 0xc2, 0x50, /* mov.l @(8,r12),r0 */
3340 0x03, 0xd1, /* mov.l 2f,r1 */
3341 0x2b, 0x40, /* jmp @r0 */
3342 0xc1, 0x50, /* mov.l @(4,r12),r0 */
3343 0x09, 0x00, /* nop */
3344 0x09, 0x00, /* nop */
3345 0, 0, 0, 0, /* 1: replaced with address of this symbol in .got. */
3346 0, 0, 0, 0 /* 2: replaced with offset into relocation table. */
3349 #else /* These are the old style PLT entries. */
3350 static const bfd_byte elf_sh_plt0_entry_be[PLT_ENTRY_SIZE] =
3352 0xd0, 0x04, /* mov.l 1f,r0 */
3353 0xd2, 0x05, /* mov.l 2f,r2 */
3354 0x60, 0x02, /* mov.l @r0,r0 */
3355 0x62, 0x22, /* mov.l @r2,r2 */
3356 0x40, 0x2b, /* jmp @r0 */
3357 0xe0, 0x00, /* mov #0,r0 */
3358 0x00, 0x09, /* nop */
3359 0x00, 0x09, /* nop */
3360 0x00, 0x09, /* nop */
3361 0x00, 0x09, /* nop */
3362 0, 0, 0, 0, /* 1: replaced with address of .got.plt + 8. */
3363 0, 0, 0, 0, /* 2: replaced with address of .got.plt + 4. */
3366 static const bfd_byte elf_sh_plt0_entry_le[PLT_ENTRY_SIZE] =
3368 0x04, 0xd0, /* mov.l 1f,r0 */
3369 0x05, 0xd2, /* mov.l 2f,r2 */
3370 0x02, 0x60, /* mov.l @r0,r0 */
3371 0x22, 0x62, /* mov.l @r2,r2 */
3372 0x2b, 0x40, /* jmp @r0 */
3373 0x00, 0xe0, /* mov #0,r0 */
3374 0x09, 0x00, /* nop */
3375 0x09, 0x00, /* nop */
3376 0x09, 0x00, /* nop */
3377 0x09, 0x00, /* nop */
3378 0, 0, 0, 0, /* 1: replaced with address of .got.plt + 8. */
3379 0, 0, 0, 0, /* 2: replaced with address of .got.plt + 4. */
3382 /* Sebsequent entries in an absolute procedure linkage table look like
3383 this. */
3385 static const bfd_byte elf_sh_plt_entry_be[PLT_ENTRY_SIZE] =
3387 0xd0, 0x04, /* mov.l 1f,r0 */
3388 0x60, 0x02, /* mov.l @r0,r0 */
3389 0xd2, 0x02, /* mov.l 0f,r2 */
3390 0x40, 0x2b, /* jmp @r0 */
3391 0x60, 0x23, /* mov r2,r0 */
3392 0xd1, 0x03, /* mov.l 2f,r1 */
3393 0x40, 0x2b, /* jmp @r0 */
3394 0x00, 0x09, /* nop */
3395 0, 0, 0, 0, /* 0: replaced with address of .PLT0. */
3396 0, 0, 0, 0, /* 1: replaced with address of this symbol in .got. */
3397 0, 0, 0, 0, /* 2: replaced with offset into relocation table. */
3400 static const bfd_byte elf_sh_plt_entry_le[PLT_ENTRY_SIZE] =
3402 0x04, 0xd0, /* mov.l 1f,r0 */
3403 0x02, 0x60, /* mov.l @r0,r0 */
3404 0x02, 0xd2, /* mov.l 0f,r2 */
3405 0x2b, 0x40, /* jmp @r0 */
3406 0x23, 0x60, /* mov r2,r0 */
3407 0x03, 0xd1, /* mov.l 2f,r1 */
3408 0x2b, 0x40, /* jmp @r0 */
3409 0x09, 0x00, /* nop */
3410 0, 0, 0, 0, /* 0: replaced with address of .PLT. */
3411 0, 0, 0, 0, /* 1: replaced with address of this symbol in .got. */
3412 0, 0, 0, 0, /* 2: replaced with offset into relocation table. */
3415 /* Entries in a PIC procedure linkage table look like this. */
3417 static const bfd_byte elf_sh_pic_plt_entry_be[PLT_ENTRY_SIZE] =
3419 0xd0, 0x04, /* mov.l 1f,r0 */
3420 0x00, 0xce, /* mov.l @(r0,r12),r0 */
3421 0x40, 0x2b, /* jmp @r0 */
3422 0x00, 0x09, /* nop */
3423 0x50, 0xc2, /* 0: mov.l @(8,r12),r0 */
3424 0x52, 0xc1, /* 1: mov.l @(4,r12),r2 */
3425 0xd1, 0x02, /* mov.l 2f,r1 */
3426 0x40, 0x2b, /* jmp @r0 */
3427 0xe0, 0x00, /* mov #0,r0 ! shows the type of PLT. */
3428 0x00, 0x09, /* nop */
3429 0, 0, 0, 0, /* 1: replaced with address of this symbol in .got. */
3430 0, 0, 0, 0 /* 2: replaced with offset into relocation table. */
3433 static const bfd_byte elf_sh_pic_plt_entry_le[PLT_ENTRY_SIZE] =
3435 0x04, 0xd0, /* mov.l 1f,r0 */
3436 0xce, 0x00, /* mov.l @(r0,r12),r0 */
3437 0x2b, 0x40, /* jmp @r0 */
3438 0x09, 0x00, /* nop */
3439 0xc2, 0x50, /* 0: mov.l @(8,r12),r0 */
3440 0xc1, 0x52, /* 1: mov.l @(4,r12),r2 */
3441 0x02, 0xd1, /* mov.l 2f,r1 */
3442 0x2b, 0x40, /* jmp @r0 */
3443 0x00, 0xe0, /* mov #0,r0 ! shows the type of PLT. */
3444 0x09, 0x00, /* nop */
3445 0, 0, 0, 0, /* 1: replaced with address of this symbol in .got. */
3446 0, 0, 0, 0 /* 2: replaced with offset into relocation table. */
3448 #endif /* old style PLT entries. */
3450 static const bfd_byte *elf_sh_plt0_entry;
3451 static const bfd_byte *elf_sh_plt_entry;
3452 static const bfd_byte *elf_sh_pic_plt_entry;
3454 /* Return size of a PLT entry. */
3455 #define elf_sh_sizeof_plt(info) PLT_ENTRY_SIZE
3457 /* Return offset of the PLT0 address in an absolute PLT entry. */
3458 #define elf_sh_plt_plt0_offset(info) 16
3460 /* Return offset of the linker in PLT0 entry. */
3461 #define elf_sh_plt0_linker_offset(info) 20
3463 /* Return offset of the GOT id in PLT0 entry. */
3464 #define elf_sh_plt0_gotid_offset(info) 24
3466 /* Return offset of the tempoline in PLT entry */
3467 #define elf_sh_plt_temp_offset(info) 8
3469 /* Return offset of the symbol in PLT entry. */
3470 #define elf_sh_plt_symbol_offset(info) 20
3472 /* Return offset of the relocation in PLT entry. */
3473 #define elf_sh_plt_reloc_offset(info) 24
3474 #endif
3476 /* The sh linker needs to keep track of the number of relocs that it
3477 decides to copy as dynamic relocs in check_relocs for each symbol.
3478 This is so that it can later discard them if they are found to be
3479 unnecessary. We store the information in a field extending the
3480 regular ELF linker hash table. */
3482 struct elf_sh_dyn_relocs
3484 struct elf_sh_dyn_relocs *next;
3486 /* The input section of the reloc. */
3487 asection *sec;
3489 /* Total number of relocs copied for the input section. */
3490 bfd_size_type count;
3492 /* Number of pc-relative relocs copied for the input section. */
3493 bfd_size_type pc_count;
3496 /* sh ELF linker hash entry. */
3498 struct elf_sh_link_hash_entry
3500 struct elf_link_hash_entry root;
3502 #ifdef INCLUDE_SHMEDIA
3503 union
3505 bfd_signed_vma refcount;
3506 bfd_vma offset;
3507 } datalabel_got;
3508 #endif
3510 /* Track dynamic relocs copied for this symbol. */
3511 struct elf_sh_dyn_relocs *dyn_relocs;
3513 bfd_signed_vma gotplt_refcount;
3515 enum {
3516 GOT_UNKNOWN = 0, GOT_NORMAL, GOT_TLS_GD, GOT_TLS_IE
3517 } tls_type;
3520 #define sh_elf_hash_entry(ent) ((struct elf_sh_link_hash_entry *)(ent))
3522 struct sh_elf_obj_tdata
3524 struct elf_obj_tdata root;
3526 /* tls_type for each local got entry. */
3527 char *local_got_tls_type;
3530 #define sh_elf_tdata(abfd) \
3531 ((struct sh_elf_obj_tdata *) (abfd)->tdata.any)
3533 #define sh_elf_local_got_tls_type(abfd) \
3534 (sh_elf_tdata (abfd)->local_got_tls_type)
3536 /* Override the generic function because we need to store sh_elf_obj_tdata
3537 as the specific tdata. */
3539 static bfd_boolean
3540 sh_elf_mkobject (bfd *abfd)
3542 bfd_size_type amt = sizeof (struct sh_elf_obj_tdata);
3543 abfd->tdata.any = bfd_zalloc (abfd, amt);
3544 if (abfd->tdata.any == NULL)
3545 return FALSE;
3546 return TRUE;
3549 /* sh ELF linker hash table. */
3551 struct elf_sh_link_hash_table
3553 struct elf_link_hash_table root;
3555 /* Short-cuts to get to dynamic linker sections. */
3556 asection *sgot;
3557 asection *sgotplt;
3558 asection *srelgot;
3559 asection *splt;
3560 asection *srelplt;
3561 asection *sdynbss;
3562 asection *srelbss;
3564 /* Small local sym to section mapping cache. */
3565 struct sym_sec_cache sym_sec;
3567 /* A counter or offset to track a TLS got entry. */
3568 union
3570 bfd_signed_vma refcount;
3571 bfd_vma offset;
3572 } tls_ldm_got;
3575 /* Traverse an sh ELF linker hash table. */
3577 #define sh_elf_link_hash_traverse(table, func, info) \
3578 (elf_link_hash_traverse \
3579 (&(table)->root, \
3580 (bfd_boolean (*) (struct elf_link_hash_entry *, void *)) (func), \
3581 (info)))
3583 /* Get the sh ELF linker hash table from a link_info structure. */
3585 #define sh_elf_hash_table(p) \
3586 ((struct elf_sh_link_hash_table *) ((p)->hash))
3588 /* Create an entry in an sh ELF linker hash table. */
3590 static struct bfd_hash_entry *
3591 sh_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
3592 struct bfd_hash_table *table,
3593 const char *string)
3595 struct elf_sh_link_hash_entry *ret =
3596 (struct elf_sh_link_hash_entry *) entry;
3598 /* Allocate the structure if it has not already been allocated by a
3599 subclass. */
3600 if (ret == (struct elf_sh_link_hash_entry *) NULL)
3601 ret = ((struct elf_sh_link_hash_entry *)
3602 bfd_hash_allocate (table,
3603 sizeof (struct elf_sh_link_hash_entry)));
3604 if (ret == (struct elf_sh_link_hash_entry *) NULL)
3605 return (struct bfd_hash_entry *) ret;
3607 /* Call the allocation method of the superclass. */
3608 ret = ((struct elf_sh_link_hash_entry *)
3609 _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
3610 table, string));
3611 if (ret != (struct elf_sh_link_hash_entry *) NULL)
3613 ret->dyn_relocs = NULL;
3614 ret->gotplt_refcount = 0;
3615 #ifdef INCLUDE_SHMEDIA
3616 ret->datalabel_got.refcount = ret->root.got.refcount;
3617 #endif
3618 ret->tls_type = GOT_UNKNOWN;
3621 return (struct bfd_hash_entry *) ret;
3624 /* Create an sh ELF linker hash table. */
3626 static struct bfd_link_hash_table *
3627 sh_elf_link_hash_table_create (bfd *abfd)
3629 struct elf_sh_link_hash_table *ret;
3630 bfd_size_type amt = sizeof (struct elf_sh_link_hash_table);
3632 ret = (struct elf_sh_link_hash_table *) bfd_malloc (amt);
3633 if (ret == (struct elf_sh_link_hash_table *) NULL)
3634 return NULL;
3636 if (! _bfd_elf_link_hash_table_init (&ret->root, abfd,
3637 sh_elf_link_hash_newfunc))
3639 free (ret);
3640 return NULL;
3643 ret->sgot = NULL;
3644 ret->sgotplt = NULL;
3645 ret->srelgot = NULL;
3646 ret->splt = NULL;
3647 ret->srelplt = NULL;
3648 ret->sdynbss = NULL;
3649 ret->srelbss = NULL;
3650 ret->sym_sec.abfd = NULL;
3651 ret->tls_ldm_got.refcount = 0;
3653 return &ret->root.root;
3656 /* Create .got, .gotplt, and .rela.got sections in DYNOBJ, and set up
3657 shortcuts to them in our hash table. */
3659 static bfd_boolean
3660 create_got_section (bfd *dynobj, struct bfd_link_info *info)
3662 struct elf_sh_link_hash_table *htab;
3664 if (! _bfd_elf_create_got_section (dynobj, info))
3665 return FALSE;
3667 htab = sh_elf_hash_table (info);
3668 htab->sgot = bfd_get_section_by_name (dynobj, ".got");
3669 htab->sgotplt = bfd_get_section_by_name (dynobj, ".got.plt");
3670 if (! htab->sgot || ! htab->sgotplt)
3671 abort ();
3673 htab->srelgot = bfd_make_section (dynobj, ".rela.got");
3674 if (htab->srelgot == NULL
3675 || ! bfd_set_section_flags (dynobj, htab->srelgot,
3676 (SEC_ALLOC
3677 | SEC_LOAD
3678 | SEC_HAS_CONTENTS
3679 | SEC_IN_MEMORY
3680 | SEC_LINKER_CREATED
3681 | SEC_READONLY))
3682 || ! bfd_set_section_alignment (dynobj, htab->srelgot, 2))
3683 return FALSE;
3684 return TRUE;
3687 /* Create dynamic sections when linking against a dynamic object. */
3689 static bfd_boolean
3690 sh_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
3692 struct elf_sh_link_hash_table *htab;
3693 flagword flags, pltflags;
3694 register asection *s;
3695 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3696 int ptralign = 0;
3698 switch (bed->s->arch_size)
3700 case 32:
3701 ptralign = 2;
3702 break;
3704 case 64:
3705 ptralign = 3;
3706 break;
3708 default:
3709 bfd_set_error (bfd_error_bad_value);
3710 return FALSE;
3713 htab = sh_elf_hash_table (info);
3714 if (htab->root.dynamic_sections_created)
3715 return TRUE;
3717 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
3718 .rel[a].bss sections. */
3720 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
3721 | SEC_LINKER_CREATED);
3723 pltflags = flags;
3724 pltflags |= SEC_CODE;
3725 if (bed->plt_not_loaded)
3726 pltflags &= ~ (SEC_LOAD | SEC_HAS_CONTENTS);
3727 if (bed->plt_readonly)
3728 pltflags |= SEC_READONLY;
3730 s = bfd_make_section (abfd, ".plt");
3731 htab->splt = s;
3732 if (s == NULL
3733 || ! bfd_set_section_flags (abfd, s, pltflags)
3734 || ! bfd_set_section_alignment (abfd, s, bed->plt_alignment))
3735 return FALSE;
3737 if (bed->want_plt_sym)
3739 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
3740 .plt section. */
3741 struct elf_link_hash_entry *h;
3742 struct bfd_link_hash_entry *bh = NULL;
3744 if (! (_bfd_generic_link_add_one_symbol
3745 (info, abfd, "_PROCEDURE_LINKAGE_TABLE_", BSF_GLOBAL, s,
3746 (bfd_vma) 0, (const char *) NULL, FALSE,
3747 get_elf_backend_data (abfd)->collect, &bh)))
3748 return FALSE;
3750 h = (struct elf_link_hash_entry *) bh;
3751 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
3752 h->type = STT_OBJECT;
3754 if (info->shared
3755 && ! _bfd_elf_link_record_dynamic_symbol (info, h))
3756 return FALSE;
3759 s = bfd_make_section (abfd,
3760 bed->default_use_rela_p ? ".rela.plt" : ".rel.plt");
3761 htab->srelplt = s;
3762 if (s == NULL
3763 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
3764 || ! bfd_set_section_alignment (abfd, s, ptralign))
3765 return FALSE;
3767 if (htab->sgot == NULL
3768 && !create_got_section (abfd, info))
3769 return FALSE;
3772 const char *secname;
3773 char *relname;
3774 flagword secflags;
3775 asection *sec;
3777 for (sec = abfd->sections; sec; sec = sec->next)
3779 secflags = bfd_get_section_flags (abfd, sec);
3780 if ((secflags & (SEC_DATA | SEC_LINKER_CREATED))
3781 || ((secflags & SEC_HAS_CONTENTS) != SEC_HAS_CONTENTS))
3782 continue;
3783 secname = bfd_get_section_name (abfd, sec);
3784 relname = (char *) bfd_malloc ((bfd_size_type) strlen (secname) + 6);
3785 strcpy (relname, ".rela");
3786 strcat (relname, secname);
3787 if (bfd_get_section_by_name (abfd, secname))
3788 continue;
3789 s = bfd_make_section (abfd, relname);
3790 if (s == NULL
3791 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
3792 || ! bfd_set_section_alignment (abfd, s, ptralign))
3793 return FALSE;
3797 if (bed->want_dynbss)
3799 /* The .dynbss section is a place to put symbols which are defined
3800 by dynamic objects, are referenced by regular objects, and are
3801 not functions. We must allocate space for them in the process
3802 image and use a R_*_COPY reloc to tell the dynamic linker to
3803 initialize them at run time. The linker script puts the .dynbss
3804 section into the .bss section of the final image. */
3805 s = bfd_make_section (abfd, ".dynbss");
3806 htab->sdynbss = s;
3807 if (s == NULL
3808 || ! bfd_set_section_flags (abfd, s, SEC_ALLOC))
3809 return FALSE;
3811 /* The .rel[a].bss section holds copy relocs. This section is not
3812 normally needed. We need to create it here, though, so that the
3813 linker will map it to an output section. We can't just create it
3814 only if we need it, because we will not know whether we need it
3815 until we have seen all the input files, and the first time the
3816 main linker code calls BFD after examining all the input files
3817 (size_dynamic_sections) the input sections have already been
3818 mapped to the output sections. If the section turns out not to
3819 be needed, we can discard it later. We will never need this
3820 section when generating a shared object, since they do not use
3821 copy relocs. */
3822 if (! info->shared)
3824 s = bfd_make_section (abfd,
3825 (bed->default_use_rela_p
3826 ? ".rela.bss" : ".rel.bss"));
3827 htab->srelbss = s;
3828 if (s == NULL
3829 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
3830 || ! bfd_set_section_alignment (abfd, s, ptralign))
3831 return FALSE;
3835 return TRUE;
3838 /* Adjust a symbol defined by a dynamic object and referenced by a
3839 regular object. The current definition is in some section of the
3840 dynamic object, but we're not including those sections. We have to
3841 change the definition to something the rest of the link can
3842 understand. */
3844 static bfd_boolean
3845 sh_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
3846 struct elf_link_hash_entry *h)
3848 struct elf_sh_link_hash_table *htab;
3849 struct elf_sh_link_hash_entry *eh;
3850 struct elf_sh_dyn_relocs *p;
3851 asection *s;
3852 unsigned int power_of_two;
3854 htab = sh_elf_hash_table (info);
3856 /* Make sure we know what is going on here. */
3857 BFD_ASSERT (htab->root.dynobj != NULL
3858 && ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT)
3859 || h->weakdef != NULL
3860 || ((h->elf_link_hash_flags
3861 & ELF_LINK_HASH_DEF_DYNAMIC) != 0
3862 && (h->elf_link_hash_flags
3863 & ELF_LINK_HASH_REF_REGULAR) != 0
3864 && (h->elf_link_hash_flags
3865 & ELF_LINK_HASH_DEF_REGULAR) == 0)));
3867 /* If this is a function, put it in the procedure linkage table. We
3868 will fill in the contents of the procedure linkage table later,
3869 when we know the address of the .got section. */
3870 if (h->type == STT_FUNC
3871 || (h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0)
3873 if (h->plt.refcount <= 0
3874 || SYMBOL_CALLS_LOCAL (info, h)
3875 || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
3876 && h->root.type == bfd_link_hash_undefweak))
3878 /* This case can occur if we saw a PLT reloc in an input
3879 file, but the symbol was never referred to by a dynamic
3880 object. In such a case, we don't actually need to build
3881 a procedure linkage table, and we can just do a REL32
3882 reloc instead. */
3883 h->plt.offset = (bfd_vma) -1;
3884 h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
3887 return TRUE;
3889 else
3890 h->plt.offset = (bfd_vma) -1;
3892 /* If this is a weak symbol, and there is a real definition, the
3893 processor independent code will have arranged for us to see the
3894 real definition first, and we can just use the same value. */
3895 if (h->weakdef != NULL)
3897 BFD_ASSERT (h->weakdef->root.type == bfd_link_hash_defined
3898 || h->weakdef->root.type == bfd_link_hash_defweak);
3899 h->root.u.def.section = h->weakdef->root.u.def.section;
3900 h->root.u.def.value = h->weakdef->root.u.def.value;
3901 if (info->nocopyreloc)
3902 h->elf_link_hash_flags
3903 = ((h->elf_link_hash_flags & ~ELF_LINK_NON_GOT_REF)
3904 | (h->weakdef->elf_link_hash_flags & ELF_LINK_NON_GOT_REF));
3905 return TRUE;
3908 /* This is a reference to a symbol defined by a dynamic object which
3909 is not a function. */
3911 /* If we are creating a shared library, we must presume that the
3912 only references to the symbol are via the global offset table.
3913 For such cases we need not do anything here; the relocations will
3914 be handled correctly by relocate_section. */
3915 if (info->shared)
3916 return TRUE;
3918 /* If there are no references to this symbol that do not use the
3919 GOT, we don't need to generate a copy reloc. */
3920 if ((h->elf_link_hash_flags & ELF_LINK_NON_GOT_REF) == 0)
3921 return TRUE;
3923 /* If -z nocopyreloc was given, we won't generate them either. */
3924 if (info->nocopyreloc)
3926 h->elf_link_hash_flags &= ~ELF_LINK_NON_GOT_REF;
3927 return TRUE;
3930 eh = (struct elf_sh_link_hash_entry *) h;
3931 for (p = eh->dyn_relocs; p != NULL; p = p->next)
3933 s = p->sec->output_section;
3934 if (s != NULL && (s->flags & (SEC_READONLY | SEC_HAS_CONTENTS)) != 0)
3935 break;
3938 /* If we didn't find any dynamic relocs in sections which needs the
3939 copy reloc, then we'll be keeping the dynamic relocs and avoiding
3940 the copy reloc. */
3941 if (p == NULL)
3943 h->elf_link_hash_flags &= ~ELF_LINK_NON_GOT_REF;
3944 return TRUE;
3947 /* We must allocate the symbol in our .dynbss section, which will
3948 become part of the .bss section of the executable. There will be
3949 an entry for this symbol in the .dynsym section. The dynamic
3950 object will contain position independent code, so all references
3951 from the dynamic object to this symbol will go through the global
3952 offset table. The dynamic linker will use the .dynsym entry to
3953 determine the address it must put in the global offset table, so
3954 both the dynamic object and the regular object will refer to the
3955 same memory location for the variable. */
3957 s = htab->sdynbss;
3958 BFD_ASSERT (s != NULL);
3960 /* We must generate a R_SH_COPY reloc to tell the dynamic linker to
3961 copy the initial value out of the dynamic object and into the
3962 runtime process image. We need to remember the offset into the
3963 .rela.bss section we are going to use. */
3964 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
3966 asection *srel;
3968 srel = htab->srelbss;
3969 BFD_ASSERT (srel != NULL);
3970 srel->_raw_size += sizeof (Elf32_External_Rela);
3971 h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_COPY;
3974 /* We need to figure out the alignment required for this symbol. I
3975 have no idea how ELF linkers handle this. */
3976 power_of_two = bfd_log2 (h->size);
3977 if (power_of_two > 3)
3978 power_of_two = 3;
3980 /* Apply the required alignment. */
3981 s->_raw_size = BFD_ALIGN (s->_raw_size, (bfd_size_type) (1 << power_of_two));
3982 if (power_of_two > bfd_get_section_alignment (htab->root.dynobj, s))
3984 if (! bfd_set_section_alignment (htab->root.dynobj, s, power_of_two))
3985 return FALSE;
3988 /* Define the symbol as being at this point in the section. */
3989 h->root.u.def.section = s;
3990 h->root.u.def.value = s->_raw_size;
3992 /* Increment the section size to make room for the symbol. */
3993 s->_raw_size += h->size;
3995 return TRUE;
3998 /* This is the condition under which sh_elf_finish_dynamic_symbol
3999 will be called from elflink.h. If elflink.h doesn't call our
4000 finish_dynamic_symbol routine, we'll need to do something about
4001 initializing any .plt and .got entries in sh_elf_relocate_section. */
4002 #define WILL_CALL_FINISH_DYNAMIC_SYMBOL(DYN, SHARED, H) \
4003 ((DYN) \
4004 && ((SHARED) \
4005 || ((H)->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0) \
4006 && ((H)->dynindx != -1 \
4007 || ((H)->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0))
4009 /* Allocate space in .plt, .got and associated reloc sections for
4010 dynamic relocs. */
4012 static bfd_boolean
4013 allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
4015 struct bfd_link_info *info;
4016 struct elf_sh_link_hash_table *htab;
4017 struct elf_sh_link_hash_entry *eh;
4018 struct elf_sh_dyn_relocs *p;
4020 if (h->root.type == bfd_link_hash_indirect)
4021 return TRUE;
4023 if (h->root.type == bfd_link_hash_warning)
4024 /* When warning symbols are created, they **replace** the "real"
4025 entry in the hash table, thus we never get to see the real
4026 symbol in a hash traversal. So look at it now. */
4027 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4029 info = (struct bfd_link_info *) inf;
4030 htab = sh_elf_hash_table (info);
4032 eh = (struct elf_sh_link_hash_entry *) h;
4033 if ((h->got.refcount > 0
4034 || (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL))
4035 && eh->gotplt_refcount > 0)
4037 /* The symbol has been forced local, or we have some direct got refs,
4038 so treat all the gotplt refs as got refs. */
4039 h->got.refcount += eh->gotplt_refcount;
4040 if (h->plt.refcount >= eh->gotplt_refcount)
4041 h->plt.refcount -= eh->gotplt_refcount;
4044 if (htab->root.dynamic_sections_created
4045 && h->plt.refcount > 0
4046 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
4047 || h->root.type != bfd_link_hash_undefweak))
4049 /* Make sure this symbol is output as a dynamic symbol.
4050 Undefined weak syms won't yet be marked as dynamic. */
4051 if (h->dynindx == -1
4052 && (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
4054 if (! bfd_elf32_link_record_dynamic_symbol (info, h))
4055 return FALSE;
4058 if (info->shared
4059 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, 0, h))
4061 asection *s = htab->splt;
4063 /* If this is the first .plt entry, make room for the special
4064 first entry. */
4065 if (s->_raw_size == 0)
4066 s->_raw_size += PLT_ENTRY_SIZE;
4068 h->plt.offset = s->_raw_size;
4070 /* If this symbol is not defined in a regular file, and we are
4071 not generating a shared library, then set the symbol to this
4072 location in the .plt. This is required to make function
4073 pointers compare as equal between the normal executable and
4074 the shared library. */
4075 if (! info->shared
4076 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
4078 h->root.u.def.section = s;
4079 h->root.u.def.value = h->plt.offset;
4082 /* Make room for this entry. */
4083 s->_raw_size += PLT_ENTRY_SIZE;
4085 /* We also need to make an entry in the .got.plt section, which
4086 will be placed in the .got section by the linker script. */
4087 htab->sgotplt->_raw_size += 4;
4089 /* We also need to make an entry in the .rel.plt section. */
4090 htab->srelplt->_raw_size += sizeof (Elf32_External_Rela);
4092 else
4094 h->plt.offset = (bfd_vma) -1;
4095 h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
4098 else
4100 h->plt.offset = (bfd_vma) -1;
4101 h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
4104 if (h->got.refcount > 0)
4106 asection *s;
4107 bfd_boolean dyn;
4108 int tls_type = sh_elf_hash_entry (h)->tls_type;
4110 /* Make sure this symbol is output as a dynamic symbol.
4111 Undefined weak syms won't yet be marked as dynamic. */
4112 if (h->dynindx == -1
4113 && (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
4115 if (! bfd_elf32_link_record_dynamic_symbol (info, h))
4116 return FALSE;
4119 s = htab->sgot;
4120 h->got.offset = s->_raw_size;
4121 s->_raw_size += 4;
4122 /* R_SH_TLS_GD needs 2 consecutive GOT slots. */
4123 if (tls_type == GOT_TLS_GD)
4124 s->_raw_size += 4;
4125 dyn = htab->root.dynamic_sections_created;
4126 /* R_SH_TLS_IE_32 needs one dynamic relocation if dynamic,
4127 R_SH_TLS_GD needs one if local symbol and two if global. */
4128 if ((tls_type == GOT_TLS_GD && h->dynindx == -1)
4129 || (tls_type == GOT_TLS_IE && dyn))
4130 htab->srelgot->_raw_size += sizeof (Elf32_External_Rela);
4131 else if (tls_type == GOT_TLS_GD)
4132 htab->srelgot->_raw_size += 2 * sizeof (Elf32_External_Rela);
4133 else if ((ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
4134 || h->root.type != bfd_link_hash_undefweak)
4135 && (info->shared
4136 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, h)))
4137 htab->srelgot->_raw_size += sizeof (Elf32_External_Rela);
4139 else
4140 h->got.offset = (bfd_vma) -1;
4142 #ifdef INCLUDE_SHMEDIA
4143 if (eh->datalabel_got.refcount > 0)
4145 asection *s;
4146 bfd_boolean dyn;
4148 /* Make sure this symbol is output as a dynamic symbol.
4149 Undefined weak syms won't yet be marked as dynamic. */
4150 if (h->dynindx == -1
4151 && (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
4153 if (! bfd_elf32_link_record_dynamic_symbol (info, h))
4154 return FALSE;
4157 s = htab->sgot;
4158 eh->datalabel_got.offset = s->_raw_size;
4159 s->_raw_size += 4;
4160 dyn = htab->root.dynamic_sections_created;
4161 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h))
4162 htab->srelgot->_raw_size += sizeof (Elf32_External_Rela);
4164 else
4165 eh->datalabel_got.offset = (bfd_vma) -1;
4166 #endif
4168 if (eh->dyn_relocs == NULL)
4169 return TRUE;
4171 /* In the shared -Bsymbolic case, discard space allocated for
4172 dynamic pc-relative relocs against symbols which turn out to be
4173 defined in regular objects. For the normal shared case, discard
4174 space for pc-relative relocs that have become local due to symbol
4175 visibility changes. */
4177 if (info->shared)
4179 if (SYMBOL_CALLS_LOCAL (info, h))
4181 struct elf_sh_dyn_relocs **pp;
4183 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
4185 p->count -= p->pc_count;
4186 p->pc_count = 0;
4187 if (p->count == 0)
4188 *pp = p->next;
4189 else
4190 pp = &p->next;
4194 /* Also discard relocs on undefined weak syms with non-default
4195 visibility. */
4196 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
4197 && h->root.type == bfd_link_hash_undefweak)
4198 eh->dyn_relocs = NULL;
4200 else
4202 /* For the non-shared case, discard space for relocs against
4203 symbols which turn out to need copy relocs or are not
4204 dynamic. */
4206 if ((h->elf_link_hash_flags & ELF_LINK_NON_GOT_REF) == 0
4207 && (((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
4208 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
4209 || (htab->root.dynamic_sections_created
4210 && (h->root.type == bfd_link_hash_undefweak
4211 || h->root.type == bfd_link_hash_undefined))))
4213 /* Make sure this symbol is output as a dynamic symbol.
4214 Undefined weak syms won't yet be marked as dynamic. */
4215 if (h->dynindx == -1
4216 && (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
4218 if (! bfd_elf32_link_record_dynamic_symbol (info, h))
4219 return FALSE;
4222 /* If that succeeded, we know we'll be keeping all the
4223 relocs. */
4224 if (h->dynindx != -1)
4225 goto keep;
4228 eh->dyn_relocs = NULL;
4230 keep: ;
4233 /* Finally, allocate space. */
4234 for (p = eh->dyn_relocs; p != NULL; p = p->next)
4236 asection *sreloc = elf_section_data (p->sec)->sreloc;
4237 sreloc->_raw_size += p->count * sizeof (Elf32_External_Rela);
4240 return TRUE;
4243 /* Find any dynamic relocs that apply to read-only sections. */
4245 static bfd_boolean
4246 readonly_dynrelocs (struct elf_link_hash_entry *h, void *inf)
4248 struct elf_sh_link_hash_entry *eh;
4249 struct elf_sh_dyn_relocs *p;
4251 if (h->root.type == bfd_link_hash_warning)
4252 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4254 eh = (struct elf_sh_link_hash_entry *) h;
4255 for (p = eh->dyn_relocs; p != NULL; p = p->next)
4257 asection *s = p->sec->output_section;
4259 if (s != NULL && (s->flags & SEC_READONLY) != 0)
4261 struct bfd_link_info *info = (struct bfd_link_info *) inf;
4263 info->flags |= DF_TEXTREL;
4265 /* Not an error, just cut short the traversal. */
4266 return FALSE;
4269 return TRUE;
4272 /* Set the sizes of the dynamic sections. */
4274 static bfd_boolean
4275 sh_elf_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
4276 struct bfd_link_info *info)
4278 struct elf_sh_link_hash_table *htab;
4279 bfd *dynobj;
4280 asection *s;
4281 bfd_boolean relocs;
4282 bfd *ibfd;
4284 htab = sh_elf_hash_table (info);
4285 dynobj = htab->root.dynobj;
4286 BFD_ASSERT (dynobj != NULL);
4288 if (htab->root.dynamic_sections_created)
4290 /* Set the contents of the .interp section to the interpreter. */
4291 if (info->executable)
4293 s = bfd_get_section_by_name (dynobj, ".interp");
4294 BFD_ASSERT (s != NULL);
4295 s->_raw_size = sizeof ELF_DYNAMIC_INTERPRETER;
4296 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
4300 /* Set up .got offsets for local syms, and space for local dynamic
4301 relocs. */
4302 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
4304 bfd_signed_vma *local_got;
4305 bfd_signed_vma *end_local_got;
4306 char *local_tls_type;
4307 bfd_size_type locsymcount;
4308 Elf_Internal_Shdr *symtab_hdr;
4309 asection *srel;
4311 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
4312 continue;
4314 for (s = ibfd->sections; s != NULL; s = s->next)
4316 struct elf_sh_dyn_relocs *p;
4318 for (p = ((struct elf_sh_dyn_relocs *)
4319 elf_section_data (s)->local_dynrel);
4320 p != NULL;
4321 p = p->next)
4323 if (! bfd_is_abs_section (p->sec)
4324 && bfd_is_abs_section (p->sec->output_section))
4326 /* Input section has been discarded, either because
4327 it is a copy of a linkonce section or due to
4328 linker script /DISCARD/, so we'll be discarding
4329 the relocs too. */
4331 else if (p->count != 0)
4333 srel = elf_section_data (p->sec)->sreloc;
4334 srel->_raw_size += p->count * sizeof (Elf32_External_Rela);
4335 if ((p->sec->output_section->flags & SEC_READONLY) != 0)
4336 info->flags |= DF_TEXTREL;
4341 local_got = elf_local_got_refcounts (ibfd);
4342 if (!local_got)
4343 continue;
4345 symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
4346 locsymcount = symtab_hdr->sh_info;
4347 #ifdef INCLUDE_SHMEDIA
4348 /* Count datalabel local GOT. */
4349 locsymcount *= 2;
4350 #endif
4351 end_local_got = local_got + locsymcount;
4352 local_tls_type = sh_elf_local_got_tls_type (ibfd);
4353 s = htab->sgot;
4354 srel = htab->srelgot;
4355 for (; local_got < end_local_got; ++local_got)
4357 if (*local_got > 0)
4359 *local_got = s->_raw_size;
4360 s->_raw_size += 4;
4361 if (*local_tls_type == GOT_TLS_GD)
4362 s->_raw_size += 4;
4363 if (info->shared)
4364 srel->_raw_size += sizeof (Elf32_External_Rela);
4366 else
4367 *local_got = (bfd_vma) -1;
4368 ++local_tls_type;
4372 if (htab->tls_ldm_got.refcount > 0)
4374 /* Allocate 2 got entries and 1 dynamic reloc for R_SH_TLS_LD_32
4375 relocs. */
4376 htab->tls_ldm_got.offset = htab->sgot->_raw_size;
4377 htab->sgot->_raw_size += 8;
4378 htab->srelgot->_raw_size += sizeof (Elf32_External_Rela);
4380 else
4381 htab->tls_ldm_got.offset = -1;
4383 /* Allocate global sym .plt and .got entries, and space for global
4384 sym dynamic relocs. */
4385 elf_link_hash_traverse (&htab->root, allocate_dynrelocs, info);
4387 /* We now have determined the sizes of the various dynamic sections.
4388 Allocate memory for them. */
4389 relocs = FALSE;
4390 for (s = dynobj->sections; s != NULL; s = s->next)
4392 if ((s->flags & SEC_LINKER_CREATED) == 0)
4393 continue;
4395 if (s == htab->splt
4396 || s == htab->sgot
4397 || s == htab->sgotplt)
4399 /* Strip this section if we don't need it; see the
4400 comment below. */
4402 else if (strncmp (bfd_get_section_name (dynobj, s), ".rela", 5) == 0)
4404 if (s->_raw_size != 0 && s != htab->srelplt)
4405 relocs = TRUE;
4407 /* We use the reloc_count field as a counter if we need
4408 to copy relocs into the output file. */
4409 s->reloc_count = 0;
4411 else
4413 /* It's not one of our sections, so don't allocate space. */
4414 continue;
4417 if (s->_raw_size == 0)
4419 /* If we don't need this section, strip it from the
4420 output file. This is mostly to handle .rela.bss and
4421 .rela.plt. We must create both sections in
4422 create_dynamic_sections, because they must be created
4423 before the linker maps input sections to output
4424 sections. The linker does that before
4425 adjust_dynamic_symbol is called, and it is that
4426 function which decides whether anything needs to go
4427 into these sections. */
4429 _bfd_strip_section_from_output (info, s);
4430 continue;
4433 /* Allocate memory for the section contents. We use bfd_zalloc
4434 here in case unused entries are not reclaimed before the
4435 section's contents are written out. This should not happen,
4436 but this way if it does, we get a R_SH_NONE reloc instead
4437 of garbage. */
4438 s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->_raw_size);
4439 if (s->contents == NULL)
4440 return FALSE;
4443 if (htab->root.dynamic_sections_created)
4445 /* Add some entries to the .dynamic section. We fill in the
4446 values later, in sh_elf_finish_dynamic_sections, but we
4447 must add the entries now so that we get the correct size for
4448 the .dynamic section. The DT_DEBUG entry is filled in by the
4449 dynamic linker and used by the debugger. */
4450 #define add_dynamic_entry(TAG, VAL) \
4451 bfd_elf32_add_dynamic_entry (info, (bfd_vma) (TAG), (bfd_vma) (VAL))
4453 if (info->executable)
4455 if (! add_dynamic_entry (DT_DEBUG, 0))
4456 return FALSE;
4459 if (htab->splt->_raw_size != 0)
4461 if (! add_dynamic_entry (DT_PLTGOT, 0)
4462 || ! add_dynamic_entry (DT_PLTRELSZ, 0)
4463 || ! add_dynamic_entry (DT_PLTREL, DT_RELA)
4464 || ! add_dynamic_entry (DT_JMPREL, 0))
4465 return FALSE;
4468 if (relocs)
4470 if (! add_dynamic_entry (DT_RELA, 0)
4471 || ! add_dynamic_entry (DT_RELASZ, 0)
4472 || ! add_dynamic_entry (DT_RELAENT,
4473 sizeof (Elf32_External_Rela)))
4474 return FALSE;
4476 /* If any dynamic relocs apply to a read-only section,
4477 then we need a DT_TEXTREL entry. */
4478 if ((info->flags & DF_TEXTREL) == 0)
4479 elf_link_hash_traverse (&htab->root, readonly_dynrelocs, info);
4481 if ((info->flags & DF_TEXTREL) != 0)
4483 if (! add_dynamic_entry (DT_TEXTREL, 0))
4484 return FALSE;
4488 #undef add_dynamic_entry
4490 return TRUE;
4493 /* Relocate an SH ELF section. */
4495 static bfd_boolean
4496 sh_elf_relocate_section (bfd *output_bfd, struct bfd_link_info *info,
4497 bfd *input_bfd, asection *input_section,
4498 bfd_byte *contents, Elf_Internal_Rela *relocs,
4499 Elf_Internal_Sym *local_syms,
4500 asection **local_sections)
4502 struct elf_sh_link_hash_table *htab;
4503 Elf_Internal_Shdr *symtab_hdr;
4504 struct elf_link_hash_entry **sym_hashes;
4505 Elf_Internal_Rela *rel, *relend;
4506 bfd *dynobj;
4507 bfd_vma *local_got_offsets;
4508 asection *sgot;
4509 asection *sgotplt;
4510 asection *splt;
4511 asection *sreloc;
4512 asection *srelgot;
4514 htab = sh_elf_hash_table (info);
4515 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
4516 sym_hashes = elf_sym_hashes (input_bfd);
4517 dynobj = htab->root.dynobj;
4518 local_got_offsets = elf_local_got_offsets (input_bfd);
4520 sgot = htab->sgot;
4521 sgotplt = htab->sgotplt;
4522 splt = htab->splt;
4523 sreloc = NULL;
4524 srelgot = NULL;
4526 rel = relocs;
4527 relend = relocs + input_section->reloc_count;
4528 for (; rel < relend; rel++)
4530 int r_type;
4531 reloc_howto_type *howto;
4532 unsigned long r_symndx;
4533 Elf_Internal_Sym *sym;
4534 asection *sec;
4535 struct elf_link_hash_entry *h;
4536 bfd_vma relocation;
4537 bfd_vma addend = (bfd_vma) 0;
4538 bfd_reloc_status_type r;
4539 int seen_stt_datalabel = 0;
4540 bfd_vma off;
4541 int tls_type;
4543 r_symndx = ELF32_R_SYM (rel->r_info);
4545 r_type = ELF32_R_TYPE (rel->r_info);
4547 /* Many of the relocs are only used for relaxing, and are
4548 handled entirely by the relaxation code. */
4549 if (r_type > (int) R_SH_LAST_INVALID_RELOC
4550 && r_type < (int) R_SH_LOOP_START)
4551 continue;
4552 if (r_type == (int) R_SH_NONE)
4553 continue;
4555 if (r_type < 0
4556 || r_type >= R_SH_max
4557 || (r_type >= (int) R_SH_FIRST_INVALID_RELOC
4558 && r_type <= (int) R_SH_LAST_INVALID_RELOC)
4559 || ( r_type >= (int) R_SH_FIRST_INVALID_RELOC_3
4560 && r_type <= (int) R_SH_LAST_INVALID_RELOC_3)
4561 || ( r_type >= (int) R_SH_FIRST_INVALID_RELOC_4
4562 && r_type <= (int) R_SH_LAST_INVALID_RELOC_4)
4563 || ( r_type >= (int) R_SH_FIRST_INVALID_RELOC_5
4564 && r_type <= (int) R_SH_LAST_INVALID_RELOC_5)
4565 || (r_type >= (int) R_SH_FIRST_INVALID_RELOC_2
4566 && r_type <= (int) R_SH_LAST_INVALID_RELOC_2))
4568 bfd_set_error (bfd_error_bad_value);
4569 return FALSE;
4572 howto = sh_elf_howto_table + r_type;
4574 /* For relocs that aren't partial_inplace, we get the addend from
4575 the relocation. */
4576 if (! howto->partial_inplace)
4577 addend = rel->r_addend;
4579 h = NULL;
4580 sym = NULL;
4581 sec = NULL;
4582 if (r_symndx < symtab_hdr->sh_info)
4584 sym = local_syms + r_symndx;
4585 sec = local_sections[r_symndx];
4586 relocation = (sec->output_section->vma
4587 + sec->output_offset
4588 + sym->st_value);
4589 /* A local symbol never has STO_SH5_ISA32, so we don't need
4590 datalabel processing here. Make sure this does not change
4591 without notice. */
4592 if ((sym->st_other & STO_SH5_ISA32) != 0)
4593 ((*info->callbacks->reloc_dangerous)
4594 (info,
4595 _("Unexpected STO_SH5_ISA32 on local symbol is not handled"),
4596 input_bfd, input_section, rel->r_offset));
4597 if (info->relocatable)
4599 /* This is a relocatable link. We don't have to change
4600 anything, unless the reloc is against a section symbol,
4601 in which case we have to adjust according to where the
4602 section symbol winds up in the output section. */
4603 sym = local_syms + r_symndx;
4604 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
4606 if (! howto->partial_inplace)
4608 /* For relocations with the addend in the
4609 relocation, we need just to update the addend.
4610 All real relocs are of type partial_inplace; this
4611 code is mostly for completeness. */
4612 rel->r_addend += sec->output_offset + sym->st_value;
4614 continue;
4617 /* Relocs of type partial_inplace need to pick up the
4618 contents in the contents and add the offset resulting
4619 from the changed location of the section symbol.
4620 Using _bfd_final_link_relocate (e.g. goto
4621 final_link_relocate) here would be wrong, because
4622 relocations marked pc_relative would get the current
4623 location subtracted, and we must only do that at the
4624 final link. */
4625 r = _bfd_relocate_contents (howto, input_bfd,
4626 sec->output_offset
4627 + sym->st_value,
4628 contents + rel->r_offset);
4629 goto relocation_done;
4632 continue;
4634 else if (! howto->partial_inplace)
4636 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, sec, rel);
4637 addend = rel->r_addend;
4639 else if ((sec->flags & SEC_MERGE)
4640 && ELF_ST_TYPE (sym->st_info) == STT_SECTION)
4642 asection *msec;
4644 if (howto->rightshift || howto->src_mask != 0xffffffff)
4646 (*_bfd_error_handler)
4647 (_("%s(%s+0x%lx): %s relocation against SEC_MERGE section"),
4648 bfd_archive_filename (input_bfd),
4649 bfd_get_section_name (input_bfd, input_section),
4650 (long) rel->r_offset, howto->name);
4651 return FALSE;
4654 addend = bfd_get_32 (input_bfd, contents + rel->r_offset);
4655 msec = sec;
4656 addend =
4657 _bfd_elf_rel_local_sym (output_bfd, sym, &msec, addend)
4658 - relocation;
4659 addend += msec->output_section->vma + msec->output_offset;
4660 bfd_put_32 (input_bfd, addend, contents + rel->r_offset);
4661 addend = 0;
4664 else
4666 /* FIXME: Ought to make use of the RELOC_FOR_GLOBAL_SYMBOL macro. */
4668 /* Section symbol are never (?) placed in the hash table, so
4669 we can just ignore hash relocations when creating a
4670 relocatable object file. */
4671 if (info->relocatable)
4672 continue;
4674 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
4675 while (h->root.type == bfd_link_hash_indirect
4676 || h->root.type == bfd_link_hash_warning)
4678 #ifdef INCLUDE_SHMEDIA
4679 /* If the reference passes a symbol marked with
4680 STT_DATALABEL, then any STO_SH5_ISA32 on the final value
4681 doesn't count. */
4682 seen_stt_datalabel |= h->type == STT_DATALABEL;
4683 #endif
4684 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4686 if (h->root.type == bfd_link_hash_defined
4687 || h->root.type == bfd_link_hash_defweak)
4689 bfd_boolean dyn;
4691 dyn = htab->root.dynamic_sections_created;
4692 sec = h->root.u.def.section;
4693 /* In these cases, we don't need the relocation value.
4694 We check specially because in some obscure cases
4695 sec->output_section will be NULL. */
4696 if (r_type == R_SH_GOTPC
4697 || r_type == R_SH_GOTPC_LOW16
4698 || r_type == R_SH_GOTPC_MEDLOW16
4699 || r_type == R_SH_GOTPC_MEDHI16
4700 || r_type == R_SH_GOTPC_HI16
4701 || ((r_type == R_SH_PLT32
4702 || r_type == R_SH_PLT_LOW16
4703 || r_type == R_SH_PLT_MEDLOW16
4704 || r_type == R_SH_PLT_MEDHI16
4705 || r_type == R_SH_PLT_HI16)
4706 && h->plt.offset != (bfd_vma) -1)
4707 || ((r_type == R_SH_GOT32
4708 || r_type == R_SH_GOT_LOW16
4709 || r_type == R_SH_GOT_MEDLOW16
4710 || r_type == R_SH_GOT_MEDHI16
4711 || r_type == R_SH_GOT_HI16)
4712 && WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h)
4713 && (! info->shared
4714 || (! info->symbolic && h->dynindx != -1)
4715 || (h->elf_link_hash_flags
4716 & ELF_LINK_HASH_DEF_REGULAR) == 0))
4717 /* The cases above are those in which relocation is
4718 overwritten in the switch block below. The cases
4719 below are those in which we must defer relocation
4720 to run-time, because we can't resolve absolute
4721 addresses when creating a shared library. */
4722 || (info->shared
4723 && ((! info->symbolic && h->dynindx != -1)
4724 || (h->elf_link_hash_flags
4725 & ELF_LINK_HASH_DEF_REGULAR) == 0)
4726 && ((r_type == R_SH_DIR32
4727 && (h->elf_link_hash_flags
4728 & ELF_LINK_FORCED_LOCAL) == 0)
4729 || r_type == R_SH_REL32)
4730 && ((input_section->flags & SEC_ALLOC) != 0
4731 /* DWARF will emit R_SH_DIR32 relocations in its
4732 sections against symbols defined externally
4733 in shared libraries. We can't do anything
4734 with them here. */
4735 || ((input_section->flags & SEC_DEBUGGING) != 0
4736 && (h->elf_link_hash_flags
4737 & ELF_LINK_HASH_DEF_DYNAMIC) != 0)))
4738 /* Dynamic relocs are not propagated for SEC_DEBUGGING
4739 sections because such sections are not SEC_ALLOC and
4740 thus ld.so will not process them. */
4741 || (sec->output_section == NULL
4742 && ((input_section->flags & SEC_DEBUGGING) != 0
4743 && (h->elf_link_hash_flags
4744 & ELF_LINK_HASH_DEF_DYNAMIC) != 0))
4745 || (sec->output_section == NULL
4746 && (sh_elf_hash_entry (h)->tls_type == GOT_TLS_IE
4747 || sh_elf_hash_entry (h)->tls_type == GOT_TLS_GD)))
4748 relocation = 0;
4749 else if (sec->output_section == NULL)
4751 (*_bfd_error_handler)
4752 (_("%s: unresolvable relocation against symbol `%s' from %s section"),
4753 bfd_archive_filename (input_bfd), h->root.root.string,
4754 bfd_get_section_name (input_bfd, input_section));
4755 return FALSE;
4757 else
4758 relocation = ((h->root.u.def.value
4759 + sec->output_section->vma
4760 + sec->output_offset)
4761 /* A STO_SH5_ISA32 causes a "bitor 1" to the
4762 symbol value, unless we've seen
4763 STT_DATALABEL on the way to it. */
4764 | ((h->other & STO_SH5_ISA32) != 0
4765 && ! seen_stt_datalabel));
4767 else if (h->root.type == bfd_link_hash_undefweak)
4768 relocation = 0;
4769 else if (! info->executable
4770 && info->unresolved_syms_in_objects == RM_IGNORE
4771 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
4772 relocation = 0;
4773 else
4775 if (! info->callbacks->undefined_symbol
4776 (info, h->root.root.string, input_bfd,
4777 input_section, rel->r_offset,
4778 ((info->shared && info->unresolved_syms_in_shared_libs == RM_GENERATE_ERROR)
4779 || (!info->shared && info->unresolved_syms_in_objects == RM_GENERATE_ERROR)
4780 || ELF_ST_VISIBILITY (h->other))))
4781 return FALSE;
4782 relocation = 0;
4786 switch ((int) r_type)
4788 final_link_relocate:
4789 /* COFF relocs don't use the addend. The addend is used for
4790 R_SH_DIR32 to be compatible with other compilers. */
4791 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
4792 contents, rel->r_offset,
4793 relocation, addend);
4794 break;
4796 case R_SH_IND12W:
4797 goto final_link_relocate;
4799 case R_SH_DIR8WPN:
4800 case R_SH_DIR8WPZ:
4801 case R_SH_DIR8WPL:
4802 /* If the reloc is against the start of this section, then
4803 the assembler has already taken care of it and the reloc
4804 is here only to assist in relaxing. If the reloc is not
4805 against the start of this section, then it's against an
4806 external symbol and we must deal with it ourselves. */
4807 if (input_section->output_section->vma + input_section->output_offset
4808 != relocation)
4810 int disp = (relocation
4811 - input_section->output_section->vma
4812 - input_section->output_offset
4813 - rel->r_offset);
4814 int mask = 0;
4815 switch (r_type)
4817 case R_SH_DIR8WPN:
4818 case R_SH_DIR8WPZ: mask = 1; break;
4819 case R_SH_DIR8WPL: mask = 3; break;
4820 default: mask = 0; break;
4822 if (disp & mask)
4824 ((*_bfd_error_handler)
4825 (_("%s: 0x%lx: fatal: unaligned branch target for relax-support relocation"),
4826 bfd_archive_filename (input_section->owner),
4827 (unsigned long) rel->r_offset));
4828 bfd_set_error (bfd_error_bad_value);
4829 return FALSE;
4831 relocation -= 4;
4832 goto final_link_relocate;
4834 r = bfd_reloc_ok;
4835 break;
4837 default:
4838 #ifdef INCLUDE_SHMEDIA
4839 if (shmedia_prepare_reloc (info, input_bfd, input_section,
4840 contents, rel, &relocation))
4841 goto final_link_relocate;
4842 #endif
4843 bfd_set_error (bfd_error_bad_value);
4844 return FALSE;
4846 case R_SH_DIR32:
4847 case R_SH_REL32:
4848 if (info->shared
4849 && (h == NULL
4850 || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
4851 || h->root.type != bfd_link_hash_undefweak)
4852 && r_symndx != 0
4853 && (input_section->flags & SEC_ALLOC) != 0
4854 && (r_type != R_SH_REL32
4855 || !SYMBOL_CALLS_LOCAL (info, h)))
4857 Elf_Internal_Rela outrel;
4858 bfd_byte *loc;
4859 bfd_boolean skip, relocate;
4861 /* When generating a shared object, these relocations
4862 are copied into the output file to be resolved at run
4863 time. */
4865 if (sreloc == NULL)
4867 const char *name;
4869 name = (bfd_elf_string_from_elf_section
4870 (input_bfd,
4871 elf_elfheader (input_bfd)->e_shstrndx,
4872 elf_section_data (input_section)->rel_hdr.sh_name));
4873 if (name == NULL)
4874 return FALSE;
4876 BFD_ASSERT (strncmp (name, ".rela", 5) == 0
4877 && strcmp (bfd_get_section_name (input_bfd,
4878 input_section),
4879 name + 5) == 0);
4881 sreloc = bfd_get_section_by_name (dynobj, name);
4882 BFD_ASSERT (sreloc != NULL);
4885 skip = FALSE;
4886 relocate = FALSE;
4888 outrel.r_offset =
4889 _bfd_elf_section_offset (output_bfd, info, input_section,
4890 rel->r_offset);
4891 if (outrel.r_offset == (bfd_vma) -1)
4892 skip = TRUE;
4893 else if (outrel.r_offset == (bfd_vma) -2)
4894 skip = TRUE, relocate = TRUE;
4895 outrel.r_offset += (input_section->output_section->vma
4896 + input_section->output_offset);
4898 if (skip)
4899 memset (&outrel, 0, sizeof outrel);
4900 else if (r_type == R_SH_REL32)
4902 BFD_ASSERT (h != NULL && h->dynindx != -1);
4903 outrel.r_info = ELF32_R_INFO (h->dynindx, R_SH_REL32);
4904 outrel.r_addend
4905 = bfd_get_32 (input_bfd, contents + rel->r_offset);
4907 else
4909 /* h->dynindx may be -1 if this symbol was marked to
4910 become local. */
4911 if (h == NULL
4912 || ((info->symbolic || h->dynindx == -1)
4913 && (h->elf_link_hash_flags
4914 & ELF_LINK_HASH_DEF_REGULAR) != 0))
4916 relocate = TRUE;
4917 outrel.r_info = ELF32_R_INFO (0, R_SH_RELATIVE);
4918 outrel.r_addend
4919 = relocation + bfd_get_32 (input_bfd,
4920 contents + rel->r_offset);
4922 else
4924 BFD_ASSERT (h->dynindx != -1);
4925 outrel.r_info = ELF32_R_INFO (h->dynindx, R_SH_DIR32);
4926 outrel.r_addend
4927 = relocation + bfd_get_32 (input_bfd,
4928 contents + rel->r_offset);
4932 loc = sreloc->contents;
4933 loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rela);
4934 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
4936 /* If this reloc is against an external symbol, we do
4937 not want to fiddle with the addend. Otherwise, we
4938 need to include the symbol value so that it becomes
4939 an addend for the dynamic reloc. */
4940 if (! relocate)
4941 continue;
4943 goto final_link_relocate;
4945 case R_SH_GOTPLT32:
4946 #ifdef INCLUDE_SHMEDIA
4947 case R_SH_GOTPLT_LOW16:
4948 case R_SH_GOTPLT_MEDLOW16:
4949 case R_SH_GOTPLT_MEDHI16:
4950 case R_SH_GOTPLT_HI16:
4951 case R_SH_GOTPLT10BY4:
4952 case R_SH_GOTPLT10BY8:
4953 #endif
4954 /* Relocation is to the entry for this symbol in the
4955 procedure linkage table. */
4957 if (h == NULL
4958 || (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL)
4959 || ! info->shared
4960 || info->symbolic
4961 || h->dynindx == -1
4962 || h->plt.offset == (bfd_vma) -1
4963 || h->got.offset != (bfd_vma) -1)
4964 goto force_got;
4966 /* Relocation is to the entry for this symbol in the global
4967 offset table extension for the procedure linkage table. */
4969 BFD_ASSERT (sgotplt != NULL);
4970 relocation = (sgotplt->output_offset
4971 + ((h->plt.offset / elf_sh_sizeof_plt (info)
4972 - 1 + 3) * 4));
4974 #ifdef GOT_BIAS
4975 relocation -= GOT_BIAS;
4976 #endif
4978 goto final_link_relocate;
4980 force_got:
4981 case R_SH_GOT32:
4982 #ifdef INCLUDE_SHMEDIA
4983 case R_SH_GOT_LOW16:
4984 case R_SH_GOT_MEDLOW16:
4985 case R_SH_GOT_MEDHI16:
4986 case R_SH_GOT_HI16:
4987 case R_SH_GOT10BY4:
4988 case R_SH_GOT10BY8:
4989 #endif
4990 /* Relocation is to the entry for this symbol in the global
4991 offset table. */
4993 BFD_ASSERT (sgot != NULL);
4995 if (h != NULL)
4997 bfd_boolean dyn;
4999 off = h->got.offset;
5000 #ifdef INCLUDE_SHMEDIA
5001 if (seen_stt_datalabel)
5003 struct elf_sh_link_hash_entry *hsh;
5005 hsh = (struct elf_sh_link_hash_entry *)h;
5006 off = hsh->datalabel_got.offset;
5008 #endif
5009 BFD_ASSERT (off != (bfd_vma) -1);
5011 dyn = htab->root.dynamic_sections_created;
5012 if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h)
5013 || (info->shared
5014 && SYMBOL_REFERENCES_LOCAL (info, h))
5015 || (ELF_ST_VISIBILITY (h->other)
5016 && h->root.type == bfd_link_hash_undefweak))
5018 /* This is actually a static link, or it is a
5019 -Bsymbolic link and the symbol is defined
5020 locally, or the symbol was forced to be local
5021 because of a version file. We must initialize
5022 this entry in the global offset table. Since the
5023 offset must always be a multiple of 4, we use the
5024 least significant bit to record whether we have
5025 initialized it already.
5027 When doing a dynamic link, we create a .rela.got
5028 relocation entry to initialize the value. This
5029 is done in the finish_dynamic_symbol routine. */
5030 if ((off & 1) != 0)
5031 off &= ~1;
5032 else
5034 bfd_put_32 (output_bfd, relocation,
5035 sgot->contents + off);
5036 #ifdef INCLUDE_SHMEDIA
5037 if (seen_stt_datalabel)
5039 struct elf_sh_link_hash_entry *hsh;
5041 hsh = (struct elf_sh_link_hash_entry *)h;
5042 hsh->datalabel_got.offset |= 1;
5044 else
5045 #endif
5046 h->got.offset |= 1;
5050 relocation = sgot->output_offset + off;
5052 else
5054 #ifdef INCLUDE_SHMEDIA
5055 if (rel->r_addend)
5057 BFD_ASSERT (local_got_offsets != NULL
5058 && (local_got_offsets[symtab_hdr->sh_info
5059 + r_symndx]
5060 != (bfd_vma) -1));
5062 off = local_got_offsets[symtab_hdr->sh_info
5063 + r_symndx];
5065 else
5067 #endif
5068 BFD_ASSERT (local_got_offsets != NULL
5069 && local_got_offsets[r_symndx] != (bfd_vma) -1);
5071 off = local_got_offsets[r_symndx];
5072 #ifdef INCLUDE_SHMEDIA
5074 #endif
5076 /* The offset must always be a multiple of 4. We use
5077 the least significant bit to record whether we have
5078 already generated the necessary reloc. */
5079 if ((off & 1) != 0)
5080 off &= ~1;
5081 else
5083 bfd_put_32 (output_bfd, relocation, sgot->contents + off);
5085 if (info->shared)
5087 Elf_Internal_Rela outrel;
5088 bfd_byte *loc;
5090 if (srelgot == NULL)
5092 srelgot = bfd_get_section_by_name (dynobj,
5093 ".rela.got");
5094 BFD_ASSERT (srelgot != NULL);
5097 outrel.r_offset = (sgot->output_section->vma
5098 + sgot->output_offset
5099 + off);
5100 outrel.r_info = ELF32_R_INFO (0, R_SH_RELATIVE);
5101 outrel.r_addend = relocation;
5102 loc = srelgot->contents;
5103 loc += srelgot->reloc_count++ * sizeof (Elf32_External_Rela);
5104 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
5107 #ifdef INCLUDE_SHMEDIA
5108 if (rel->r_addend)
5109 local_got_offsets[symtab_hdr->sh_info + r_symndx] |= 1;
5110 else
5111 #endif
5112 local_got_offsets[r_symndx] |= 1;
5115 relocation = sgot->output_offset + off;
5118 #ifdef GOT_BIAS
5119 relocation -= GOT_BIAS;
5120 #endif
5122 goto final_link_relocate;
5124 case R_SH_GOTOFF:
5125 #ifdef INCLUDE_SHMEDIA
5126 case R_SH_GOTOFF_LOW16:
5127 case R_SH_GOTOFF_MEDLOW16:
5128 case R_SH_GOTOFF_MEDHI16:
5129 case R_SH_GOTOFF_HI16:
5130 #endif
5131 /* Relocation is relative to the start of the global offset
5132 table. */
5134 BFD_ASSERT (sgot != NULL);
5136 /* Note that sgot->output_offset is not involved in this
5137 calculation. We always want the start of .got. If we
5138 defined _GLOBAL_OFFSET_TABLE in a different way, as is
5139 permitted by the ABI, we might have to change this
5140 calculation. */
5141 relocation -= sgot->output_section->vma;
5143 #ifdef GOT_BIAS
5144 relocation -= GOT_BIAS;
5145 #endif
5147 addend = rel->r_addend;
5149 goto final_link_relocate;
5151 case R_SH_GOTPC:
5152 #ifdef INCLUDE_SHMEDIA
5153 case R_SH_GOTPC_LOW16:
5154 case R_SH_GOTPC_MEDLOW16:
5155 case R_SH_GOTPC_MEDHI16:
5156 case R_SH_GOTPC_HI16:
5157 #endif
5158 /* Use global offset table as symbol value. */
5160 BFD_ASSERT (sgot != NULL);
5161 relocation = sgot->output_section->vma;
5163 #ifdef GOT_BIAS
5164 relocation += GOT_BIAS;
5165 #endif
5167 addend = rel->r_addend;
5169 goto final_link_relocate;
5171 case R_SH_PLT32:
5172 #ifdef INCLUDE_SHMEDIA
5173 case R_SH_PLT_LOW16:
5174 case R_SH_PLT_MEDLOW16:
5175 case R_SH_PLT_MEDHI16:
5176 case R_SH_PLT_HI16:
5177 #endif
5178 /* Relocation is to the entry for this symbol in the
5179 procedure linkage table. */
5181 /* Resolve a PLT reloc against a local symbol directly,
5182 without using the procedure linkage table. */
5183 if (h == NULL)
5184 goto final_link_relocate;
5186 if (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL)
5187 goto final_link_relocate;
5189 if (h->plt.offset == (bfd_vma) -1)
5191 /* We didn't make a PLT entry for this symbol. This
5192 happens when statically linking PIC code, or when
5193 using -Bsymbolic. */
5194 goto final_link_relocate;
5197 BFD_ASSERT (splt != NULL);
5198 relocation = (splt->output_section->vma
5199 + splt->output_offset
5200 + h->plt.offset);
5202 #ifdef INCLUDE_SHMEDIA
5203 relocation++;
5204 #endif
5206 addend = rel->r_addend;
5208 goto final_link_relocate;
5210 case R_SH_LOOP_START:
5212 static bfd_vma start, end;
5214 start = (relocation + rel->r_addend
5215 - (sec->output_section->vma + sec->output_offset));
5216 r = sh_elf_reloc_loop (r_type, input_bfd, input_section, contents,
5217 rel->r_offset, sec, start, end);
5218 break;
5220 case R_SH_LOOP_END:
5221 end = (relocation + rel->r_addend
5222 - (sec->output_section->vma + sec->output_offset));
5223 r = sh_elf_reloc_loop (r_type, input_bfd, input_section, contents,
5224 rel->r_offset, sec, start, end);
5225 break;
5228 case R_SH_TLS_GD_32:
5229 case R_SH_TLS_IE_32:
5230 r_type = sh_elf_optimized_tls_reloc (info, r_type, h == NULL);
5231 tls_type = GOT_UNKNOWN;
5232 if (h == NULL && local_got_offsets)
5233 tls_type = sh_elf_local_got_tls_type (input_bfd) [r_symndx];
5234 else if (h != NULL)
5236 tls_type = sh_elf_hash_entry (h)->tls_type;
5237 if (! info->shared
5238 && (h->dynindx == -1
5239 || (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR)))
5240 r_type = R_SH_TLS_LE_32;
5243 if (r_type == R_SH_TLS_GD_32 && tls_type == GOT_TLS_IE)
5244 r_type = R_SH_TLS_IE_32;
5246 if (r_type == R_SH_TLS_LE_32)
5248 bfd_vma offset;
5249 unsigned short insn;
5251 if (ELF32_R_TYPE (rel->r_info) == R_SH_TLS_GD_32)
5253 /* GD->LE transition:
5254 mov.l 1f,r4; mova 2f,r0; mov.l 2f,r1; add r0,r1;
5255 jsr @r1; add r12,r4; bra 3f; nop; .align 2;
5256 1: .long x$TLSGD; 2: .long __tls_get_addr@PLT; 3:
5257 We change it into:
5258 mov.l 1f,r4; stc gbr,r0; add r4,r0; nop;
5259 nop; nop; ...
5260 1: .long x@TPOFF; 2: .long __tls_get_addr@PLT; 3:. */
5262 offset = rel->r_offset;
5263 BFD_ASSERT (offset >= 16);
5264 /* Size of GD instructions is 16 or 18. */
5265 offset -= 16;
5266 insn = bfd_get_16 (input_bfd, contents + offset + 0);
5267 if ((insn & 0xff00) == 0xc700)
5269 BFD_ASSERT (offset >= 2);
5270 offset -= 2;
5271 insn = bfd_get_16 (input_bfd, contents + offset + 0);
5274 BFD_ASSERT ((insn & 0xff00) == 0xd400);
5275 insn = bfd_get_16 (input_bfd, contents + offset + 2);
5276 BFD_ASSERT ((insn & 0xff00) == 0xc700);
5277 insn = bfd_get_16 (input_bfd, contents + offset + 4);
5278 BFD_ASSERT ((insn & 0xff00) == 0xd100);
5279 insn = bfd_get_16 (input_bfd, contents + offset + 6);
5280 BFD_ASSERT (insn == 0x310c);
5281 insn = bfd_get_16 (input_bfd, contents + offset + 8);
5282 BFD_ASSERT (insn == 0x410b);
5283 insn = bfd_get_16 (input_bfd, contents + offset + 10);
5284 BFD_ASSERT (insn == 0x34cc);
5286 bfd_put_16 (output_bfd, 0x0012, contents + offset + 2);
5287 bfd_put_16 (output_bfd, 0x304c, contents + offset + 4);
5288 bfd_put_16 (output_bfd, 0x0009, contents + offset + 6);
5289 bfd_put_16 (output_bfd, 0x0009, contents + offset + 8);
5290 bfd_put_16 (output_bfd, 0x0009, contents + offset + 10);
5292 else
5294 int index;
5296 /* IE->LE transition:
5297 mov.l 1f,r0; stc gbr,rN; mov.l @(r0,r12),rM;
5298 bra 2f; add ...; .align 2; 1: x@GOTTPOFF; 2:
5299 We change it into:
5300 mov.l .Ln,rM; stc gbr,rN; nop; ...;
5301 1: x@TPOFF; 2:. */
5303 offset = rel->r_offset;
5304 BFD_ASSERT (offset >= 16);
5305 /* Size of IE instructions is 10 or 12. */
5306 offset -= 10;
5307 insn = bfd_get_16 (input_bfd, contents + offset + 0);
5308 if ((insn & 0xf0ff) == 0x0012)
5310 BFD_ASSERT (offset >= 2);
5311 offset -= 2;
5312 insn = bfd_get_16 (input_bfd, contents + offset + 0);
5315 BFD_ASSERT ((insn & 0xff00) == 0xd000);
5316 index = insn & 0x00ff;
5317 insn = bfd_get_16 (input_bfd, contents + offset + 2);
5318 BFD_ASSERT ((insn & 0xf0ff) == 0x0012);
5319 insn = bfd_get_16 (input_bfd, contents + offset + 4);
5320 BFD_ASSERT ((insn & 0xf0ff) == 0x00ce);
5321 insn = 0xd000 | (insn & 0x0f00) | index;
5322 bfd_put_16 (output_bfd, insn, contents + offset + 0);
5323 bfd_put_16 (output_bfd, 0x0009, contents + offset + 4);
5326 bfd_put_32 (output_bfd, tpoff (info, relocation),
5327 contents + rel->r_offset);
5328 continue;
5331 sgot = htab->sgot;
5332 if (sgot == NULL)
5333 abort ();
5335 if (h != NULL)
5336 off = h->got.offset;
5337 else
5339 if (local_got_offsets == NULL)
5340 abort ();
5342 off = local_got_offsets[r_symndx];
5345 /* Relocate R_SH_TLS_IE_32 directly when statically linking. */
5346 if (r_type == R_SH_TLS_IE_32
5347 && ! htab->root.dynamic_sections_created)
5349 off &= ~1;
5350 bfd_put_32 (output_bfd, tpoff (info, relocation),
5351 sgot->contents + off);
5352 bfd_put_32 (output_bfd, sgot->output_offset + off,
5353 contents + rel->r_offset);
5354 continue;
5357 if ((off & 1) != 0)
5358 off &= ~1;
5359 else
5361 Elf_Internal_Rela outrel;
5362 bfd_byte *loc;
5363 int dr_type, indx;
5365 if (srelgot == NULL)
5367 srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
5368 BFD_ASSERT (srelgot != NULL);
5371 outrel.r_offset = (sgot->output_section->vma
5372 + sgot->output_offset + off);
5374 if (h == NULL || h->dynindx == -1)
5375 indx = 0;
5376 else
5377 indx = h->dynindx;
5379 dr_type = (r_type == R_SH_TLS_GD_32 ? R_SH_TLS_DTPMOD32 :
5380 R_SH_TLS_TPOFF32);
5381 if (dr_type == R_SH_TLS_TPOFF32 && indx == 0)
5382 outrel.r_addend = relocation - dtpoff_base (info);
5383 else
5384 outrel.r_addend = 0;
5385 outrel.r_info = ELF32_R_INFO (indx, dr_type);
5386 loc = srelgot->contents;
5387 loc += srelgot->reloc_count++ * sizeof (Elf32_External_Rela);
5388 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
5390 if (r_type == R_SH_TLS_GD_32)
5392 if (indx == 0)
5394 bfd_put_32 (output_bfd,
5395 relocation - dtpoff_base (info),
5396 sgot->contents + off + 4);
5398 else
5400 outrel.r_info = ELF32_R_INFO (indx,
5401 R_SH_TLS_DTPOFF32);
5402 outrel.r_offset += 4;
5403 outrel.r_addend = 0;
5404 srelgot->reloc_count++;
5405 loc += sizeof (Elf32_External_Rela);
5406 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
5410 if (h != NULL)
5411 h->got.offset |= 1;
5412 else
5413 local_got_offsets[r_symndx] |= 1;
5416 if (off >= (bfd_vma) -2)
5417 abort ();
5419 if (r_type == (int) ELF32_R_TYPE (rel->r_info))
5420 relocation = sgot->output_offset + off;
5421 else
5423 bfd_vma offset;
5424 unsigned short insn;
5426 /* GD->IE transition:
5427 mov.l 1f,r4; mova 2f,r0; mov.l 2f,r1; add r0,r1;
5428 jsr @r1; add r12,r4; bra 3f; nop; .align 2;
5429 1: .long x$TLSGD; 2: .long __tls_get_addr@PLT; 3:
5430 We change it into:
5431 mov.l 1f,r0; stc gbr,r4; mov.l @(r0,r12),r0; add r4,r0;
5432 nop; nop; bra 3f; nop; .align 2;
5433 1: .long x@TPOFF; 2:...; 3:. */
5435 offset = rel->r_offset;
5436 BFD_ASSERT (offset >= 16);
5437 /* Size of GD instructions is 16 or 18. */
5438 offset -= 16;
5439 insn = bfd_get_16 (input_bfd, contents + offset + 0);
5440 if ((insn & 0xff00) == 0xc700)
5442 BFD_ASSERT (offset >= 2);
5443 offset -= 2;
5444 insn = bfd_get_16 (input_bfd, contents + offset + 0);
5447 BFD_ASSERT ((insn & 0xff00) == 0xd400);
5449 /* Replace mov.l 1f,R4 with mov.l 1f,r0. */
5450 bfd_put_16 (output_bfd, insn & 0xf0ff, contents + offset);
5452 insn = bfd_get_16 (input_bfd, contents + offset + 2);
5453 BFD_ASSERT ((insn & 0xff00) == 0xc700);
5454 insn = bfd_get_16 (input_bfd, contents + offset + 4);
5455 BFD_ASSERT ((insn & 0xff00) == 0xd100);
5456 insn = bfd_get_16 (input_bfd, contents + offset + 6);
5457 BFD_ASSERT (insn == 0x310c);
5458 insn = bfd_get_16 (input_bfd, contents + offset + 8);
5459 BFD_ASSERT (insn == 0x410b);
5460 insn = bfd_get_16 (input_bfd, contents + offset + 10);
5461 BFD_ASSERT (insn == 0x34cc);
5463 bfd_put_16 (output_bfd, 0x0412, contents + offset + 2);
5464 bfd_put_16 (output_bfd, 0x00ce, contents + offset + 4);
5465 bfd_put_16 (output_bfd, 0x304c, contents + offset + 6);
5466 bfd_put_16 (output_bfd, 0x0009, contents + offset + 8);
5467 bfd_put_16 (output_bfd, 0x0009, contents + offset + 10);
5469 bfd_put_32 (output_bfd, sgot->output_offset + off,
5470 contents + rel->r_offset);
5472 continue;
5475 addend = rel->r_addend;
5477 goto final_link_relocate;
5479 case R_SH_TLS_LD_32:
5480 if (! info->shared)
5482 bfd_vma offset;
5483 unsigned short insn;
5485 /* LD->LE transition:
5486 mov.l 1f,r4; mova 2f,r0; mov.l 2f,r1; add r0,r1;
5487 jsr @r1; add r12,r4; bra 3f; nop; .align 2;
5488 1: .long x$TLSLD; 2: .long __tls_get_addr@PLT; 3:
5489 We change it into:
5490 stc gbr,r0; nop; nop; nop;
5491 nop; nop; bra 3f; ...; 3:. */
5493 offset = rel->r_offset;
5494 BFD_ASSERT (offset >= 16);
5495 /* Size of LD instructions is 16 or 18. */
5496 offset -= 16;
5497 insn = bfd_get_16 (input_bfd, contents + offset + 0);
5498 if ((insn & 0xff00) == 0xc700)
5500 BFD_ASSERT (offset >= 2);
5501 offset -= 2;
5502 insn = bfd_get_16 (input_bfd, contents + offset + 0);
5505 BFD_ASSERT ((insn & 0xff00) == 0xd400);
5506 insn = bfd_get_16 (input_bfd, contents + offset + 2);
5507 BFD_ASSERT ((insn & 0xff00) == 0xc700);
5508 insn = bfd_get_16 (input_bfd, contents + offset + 4);
5509 BFD_ASSERT ((insn & 0xff00) == 0xd100);
5510 insn = bfd_get_16 (input_bfd, contents + offset + 6);
5511 BFD_ASSERT (insn == 0x310c);
5512 insn = bfd_get_16 (input_bfd, contents + offset + 8);
5513 BFD_ASSERT (insn == 0x410b);
5514 insn = bfd_get_16 (input_bfd, contents + offset + 10);
5515 BFD_ASSERT (insn == 0x34cc);
5517 bfd_put_16 (output_bfd, 0x0012, contents + offset + 0);
5518 bfd_put_16 (output_bfd, 0x0009, contents + offset + 2);
5519 bfd_put_16 (output_bfd, 0x0009, contents + offset + 4);
5520 bfd_put_16 (output_bfd, 0x0009, contents + offset + 6);
5521 bfd_put_16 (output_bfd, 0x0009, contents + offset + 8);
5522 bfd_put_16 (output_bfd, 0x0009, contents + offset + 10);
5524 continue;
5527 sgot = htab->sgot;
5528 if (sgot == NULL)
5529 abort ();
5531 off = htab->tls_ldm_got.offset;
5532 if (off & 1)
5533 off &= ~1;
5534 else
5536 Elf_Internal_Rela outrel;
5537 bfd_byte *loc;
5539 srelgot = htab->srelgot;
5540 if (srelgot == NULL)
5541 abort ();
5543 outrel.r_offset = (sgot->output_section->vma
5544 + sgot->output_offset + off);
5545 outrel.r_addend = 0;
5546 outrel.r_info = ELF32_R_INFO (0, R_SH_TLS_DTPMOD32);
5547 loc = srelgot->contents;
5548 loc += srelgot->reloc_count++ * sizeof (Elf32_External_Rela);
5549 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
5550 htab->tls_ldm_got.offset |= 1;
5553 relocation = sgot->output_offset + off;
5554 addend = rel->r_addend;
5556 goto final_link_relocate;
5558 case R_SH_TLS_LDO_32:
5559 if (! info->shared)
5560 relocation = tpoff (info, relocation);
5561 else
5562 relocation -= dtpoff_base (info);
5564 addend = rel->r_addend;
5565 goto final_link_relocate;
5567 case R_SH_TLS_LE_32:
5569 int indx;
5570 Elf_Internal_Rela outrel;
5571 bfd_byte *loc;
5573 if (! info->shared)
5575 relocation = tpoff (info, relocation);
5576 addend = rel->r_addend;
5577 goto final_link_relocate;
5580 if (sreloc == NULL)
5582 const char *name;
5584 name = (bfd_elf_string_from_elf_section
5585 (input_bfd,
5586 elf_elfheader (input_bfd)->e_shstrndx,
5587 elf_section_data (input_section)->rel_hdr.sh_name));
5588 if (name == NULL)
5589 return FALSE;
5591 BFD_ASSERT (strncmp (name, ".rela", 5) == 0
5592 && strcmp (bfd_get_section_name (input_bfd,
5593 input_section),
5594 name + 5) == 0);
5596 sreloc = bfd_get_section_by_name (dynobj, name);
5597 BFD_ASSERT (sreloc != NULL);
5600 if (h == NULL || h->dynindx == -1)
5601 indx = 0;
5602 else
5603 indx = h->dynindx;
5605 outrel.r_offset = (input_section->output_section->vma
5606 + input_section->output_offset
5607 + rel->r_offset);
5608 outrel.r_info = ELF32_R_INFO (indx, R_SH_TLS_TPOFF32);
5609 if (indx == 0)
5610 outrel.r_addend = relocation - dtpoff_base (info);
5611 else
5612 outrel.r_addend = 0;
5614 loc = sreloc->contents;
5615 loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rela);
5616 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
5617 continue;
5621 relocation_done:
5622 if (r != bfd_reloc_ok)
5624 switch (r)
5626 default:
5627 case bfd_reloc_outofrange:
5628 abort ();
5629 case bfd_reloc_overflow:
5631 const char *name;
5633 if (h != NULL)
5634 name = h->root.root.string;
5635 else
5637 name = (bfd_elf_string_from_elf_section
5638 (input_bfd, symtab_hdr->sh_link, sym->st_name));
5639 if (name == NULL)
5640 return FALSE;
5641 if (*name == '\0')
5642 name = bfd_section_name (input_bfd, sec);
5644 if (! ((*info->callbacks->reloc_overflow)
5645 (info, name, howto->name, (bfd_vma) 0,
5646 input_bfd, input_section, rel->r_offset)))
5647 return FALSE;
5649 break;
5654 return TRUE;
5657 /* This is a version of bfd_generic_get_relocated_section_contents
5658 which uses sh_elf_relocate_section. */
5660 static bfd_byte *
5661 sh_elf_get_relocated_section_contents (bfd *output_bfd,
5662 struct bfd_link_info *link_info,
5663 struct bfd_link_order *link_order,
5664 bfd_byte *data,
5665 bfd_boolean relocatable,
5666 asymbol **symbols)
5668 Elf_Internal_Shdr *symtab_hdr;
5669 asection *input_section = link_order->u.indirect.section;
5670 bfd *input_bfd = input_section->owner;
5671 asection **sections = NULL;
5672 Elf_Internal_Rela *internal_relocs = NULL;
5673 Elf_Internal_Sym *isymbuf = NULL;
5675 /* We only need to handle the case of relaxing, or of having a
5676 particular set of section contents, specially. */
5677 if (relocatable
5678 || elf_section_data (input_section)->this_hdr.contents == NULL)
5679 return bfd_generic_get_relocated_section_contents (output_bfd, link_info,
5680 link_order, data,
5681 relocatable,
5682 symbols);
5684 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
5686 memcpy (data, elf_section_data (input_section)->this_hdr.contents,
5687 (size_t) input_section->_raw_size);
5689 if ((input_section->flags & SEC_RELOC) != 0
5690 && input_section->reloc_count > 0)
5692 asection **secpp;
5693 Elf_Internal_Sym *isym, *isymend;
5694 bfd_size_type amt;
5696 internal_relocs = (_bfd_elf_link_read_relocs
5697 (input_bfd, input_section, NULL,
5698 (Elf_Internal_Rela *) NULL, FALSE));
5699 if (internal_relocs == NULL)
5700 goto error_return;
5702 if (symtab_hdr->sh_info != 0)
5704 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
5705 if (isymbuf == NULL)
5706 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr,
5707 symtab_hdr->sh_info, 0,
5708 NULL, NULL, NULL);
5709 if (isymbuf == NULL)
5710 goto error_return;
5713 amt = symtab_hdr->sh_info;
5714 amt *= sizeof (asection *);
5715 sections = (asection **) bfd_malloc (amt);
5716 if (sections == NULL && amt != 0)
5717 goto error_return;
5719 isymend = isymbuf + symtab_hdr->sh_info;
5720 for (isym = isymbuf, secpp = sections; isym < isymend; ++isym, ++secpp)
5722 asection *isec;
5724 if (isym->st_shndx == SHN_UNDEF)
5725 isec = bfd_und_section_ptr;
5726 else if (isym->st_shndx == SHN_ABS)
5727 isec = bfd_abs_section_ptr;
5728 else if (isym->st_shndx == SHN_COMMON)
5729 isec = bfd_com_section_ptr;
5730 else
5731 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
5733 *secpp = isec;
5736 if (! sh_elf_relocate_section (output_bfd, link_info, input_bfd,
5737 input_section, data, internal_relocs,
5738 isymbuf, sections))
5739 goto error_return;
5741 if (sections != NULL)
5742 free (sections);
5743 if (isymbuf != NULL
5744 && symtab_hdr->contents != (unsigned char *) isymbuf)
5745 free (isymbuf);
5746 if (elf_section_data (input_section)->relocs != internal_relocs)
5747 free (internal_relocs);
5750 return data;
5752 error_return:
5753 if (sections != NULL)
5754 free (sections);
5755 if (isymbuf != NULL
5756 && symtab_hdr->contents != (unsigned char *) isymbuf)
5757 free (isymbuf);
5758 if (internal_relocs != NULL
5759 && elf_section_data (input_section)->relocs != internal_relocs)
5760 free (internal_relocs);
5761 return NULL;
5764 /* Return the base VMA address which should be subtracted from real addresses
5765 when resolving @dtpoff relocation.
5766 This is PT_TLS segment p_vaddr. */
5768 static bfd_vma
5769 dtpoff_base (struct bfd_link_info *info)
5771 /* If tls_segment is NULL, we should have signalled an error already. */
5772 if (elf_hash_table (info)->tls_segment == NULL)
5773 return 0;
5774 return elf_hash_table (info)->tls_segment->start;
5777 /* Return the relocation value for R_SH_TLS_TPOFF32.. */
5779 static bfd_vma
5780 tpoff (struct bfd_link_info *info, bfd_vma address)
5782 /* If tls_segment is NULL, we should have signalled an error already. */
5783 if (elf_hash_table (info)->tls_segment == NULL)
5784 return 0;
5785 /* SH TLS ABI is variant I and static TLS block start just after tcbhead
5786 structure which has 2 pointer fields. */
5787 return (address - dtpoff_base (info) + 8);
5790 static asection *
5791 sh_elf_gc_mark_hook (asection *sec,
5792 struct bfd_link_info *info ATTRIBUTE_UNUSED,
5793 Elf_Internal_Rela *rel, struct elf_link_hash_entry *h,
5794 Elf_Internal_Sym *sym)
5796 if (h != NULL)
5798 switch (ELF32_R_TYPE (rel->r_info))
5800 case R_SH_GNU_VTINHERIT:
5801 case R_SH_GNU_VTENTRY:
5802 break;
5804 default:
5805 #ifdef INCLUDE_SHMEDIA
5806 while (h->root.type == bfd_link_hash_indirect
5807 && h->root.u.i.link)
5808 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5809 #endif
5810 switch (h->root.type)
5812 case bfd_link_hash_defined:
5813 case bfd_link_hash_defweak:
5814 return h->root.u.def.section;
5816 case bfd_link_hash_common:
5817 return h->root.u.c.p->section;
5819 default:
5820 break;
5824 else
5825 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
5827 return NULL;
5830 /* Update the got entry reference counts for the section being removed. */
5832 static bfd_boolean
5833 sh_elf_gc_sweep_hook (bfd *abfd, struct bfd_link_info *info,
5834 asection *sec, const Elf_Internal_Rela *relocs)
5836 Elf_Internal_Shdr *symtab_hdr;
5837 struct elf_link_hash_entry **sym_hashes;
5838 bfd_signed_vma *local_got_refcounts;
5839 const Elf_Internal_Rela *rel, *relend;
5841 elf_section_data (sec)->local_dynrel = NULL;
5843 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
5844 sym_hashes = elf_sym_hashes (abfd);
5845 local_got_refcounts = elf_local_got_refcounts (abfd);
5847 relend = relocs + sec->reloc_count;
5848 for (rel = relocs; rel < relend; rel++)
5850 unsigned long r_symndx;
5851 unsigned int r_type;
5852 struct elf_link_hash_entry *h = NULL;
5853 #ifdef INCLUDE_SHMEDIA
5854 int seen_stt_datalabel = 0;
5855 #endif
5857 r_symndx = ELF32_R_SYM (rel->r_info);
5858 if (r_symndx >= symtab_hdr->sh_info)
5860 struct elf_sh_link_hash_entry *eh;
5861 struct elf_sh_dyn_relocs **pp;
5862 struct elf_sh_dyn_relocs *p;
5864 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
5865 #ifdef INCLUDE_SHMEDIA
5866 while (h->root.type == bfd_link_hash_indirect
5867 || h->root.type == bfd_link_hash_warning)
5869 seen_stt_datalabel |= h->type == STT_DATALABEL;
5870 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5872 #endif
5873 eh = (struct elf_sh_link_hash_entry *) h;
5874 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; pp = &p->next)
5875 if (p->sec == sec)
5877 /* Everything must go for SEC. */
5878 *pp = p->next;
5879 break;
5883 r_type = ELF32_R_TYPE (rel->r_info);
5884 switch (sh_elf_optimized_tls_reloc (info, r_type, h != NULL))
5886 case R_SH_TLS_LD_32:
5887 if (sh_elf_hash_table (info)->tls_ldm_got.refcount > 0)
5888 sh_elf_hash_table (info)->tls_ldm_got.refcount -= 1;
5889 break;
5891 case R_SH_GOT32:
5892 case R_SH_GOTOFF:
5893 case R_SH_GOTPC:
5894 #ifdef INCLUDE_SHMEDIA
5895 case R_SH_GOT_LOW16:
5896 case R_SH_GOT_MEDLOW16:
5897 case R_SH_GOT_MEDHI16:
5898 case R_SH_GOT_HI16:
5899 case R_SH_GOT10BY4:
5900 case R_SH_GOT10BY8:
5901 case R_SH_GOTOFF_LOW16:
5902 case R_SH_GOTOFF_MEDLOW16:
5903 case R_SH_GOTOFF_MEDHI16:
5904 case R_SH_GOTOFF_HI16:
5905 case R_SH_GOTPC_LOW16:
5906 case R_SH_GOTPC_MEDLOW16:
5907 case R_SH_GOTPC_MEDHI16:
5908 case R_SH_GOTPC_HI16:
5909 #endif
5910 case R_SH_TLS_GD_32:
5911 case R_SH_TLS_IE_32:
5912 if (h != NULL)
5914 #ifdef INCLUDE_SHMEDIA
5915 if (seen_stt_datalabel)
5917 struct elf_sh_link_hash_entry *eh;
5918 eh = (struct elf_sh_link_hash_entry *) h;
5919 if (eh->datalabel_got.refcount > 0)
5920 eh->datalabel_got.refcount -= 1;
5922 else
5923 #endif
5924 if (h->got.refcount > 0)
5925 h->got.refcount -= 1;
5927 else if (local_got_refcounts != NULL)
5929 #ifdef INCLUDE_SHMEDIA
5930 if (rel->r_addend & 1)
5932 if (local_got_refcounts[symtab_hdr->sh_info + r_symndx] > 0)
5933 local_got_refcounts[symtab_hdr->sh_info + r_symndx] -= 1;
5935 else
5936 #endif
5937 if (local_got_refcounts[r_symndx] > 0)
5938 local_got_refcounts[r_symndx] -= 1;
5940 break;
5942 case R_SH_DIR32:
5943 case R_SH_REL32:
5944 if (info->shared)
5945 break;
5946 /* Fall thru */
5948 case R_SH_PLT32:
5949 #ifdef INCLUDE_SHMEDIA
5950 case R_SH_PLT_LOW16:
5951 case R_SH_PLT_MEDLOW16:
5952 case R_SH_PLT_MEDHI16:
5953 case R_SH_PLT_HI16:
5954 #endif
5955 if (h != NULL)
5957 if (h->plt.refcount > 0)
5958 h->plt.refcount -= 1;
5960 break;
5962 case R_SH_GOTPLT32:
5963 #ifdef INCLUDE_SHMEDIA
5964 case R_SH_GOTPLT_LOW16:
5965 case R_SH_GOTPLT_MEDLOW16:
5966 case R_SH_GOTPLT_MEDHI16:
5967 case R_SH_GOTPLT_HI16:
5968 case R_SH_GOTPLT10BY4:
5969 case R_SH_GOTPLT10BY8:
5970 #endif
5971 if (h != NULL)
5973 struct elf_sh_link_hash_entry *eh;
5974 eh = (struct elf_sh_link_hash_entry *) h;
5975 if (eh->gotplt_refcount > 0)
5977 eh->gotplt_refcount -= 1;
5978 if (h->plt.refcount > 0)
5979 h->plt.refcount -= 1;
5981 #ifdef INCLUDE_SHMEDIA
5982 else if (seen_stt_datalabel)
5984 if (eh->datalabel_got.refcount > 0)
5985 eh->datalabel_got.refcount -= 1;
5987 #endif
5988 else if (h->got.refcount > 0)
5989 h->got.refcount -= 1;
5991 else if (local_got_refcounts != NULL)
5993 #ifdef INCLUDE_SHMEDIA
5994 if (rel->r_addend & 1)
5996 if (local_got_refcounts[symtab_hdr->sh_info + r_symndx] > 0)
5997 local_got_refcounts[symtab_hdr->sh_info + r_symndx] -= 1;
5999 else
6000 #endif
6001 if (local_got_refcounts[r_symndx] > 0)
6002 local_got_refcounts[r_symndx] -= 1;
6004 break;
6006 default:
6007 break;
6011 return TRUE;
6014 /* Copy the extra info we tack onto an elf_link_hash_entry. */
6016 static void
6017 sh_elf_copy_indirect_symbol (const struct elf_backend_data *bed,
6018 struct elf_link_hash_entry *dir,
6019 struct elf_link_hash_entry *ind)
6021 struct elf_sh_link_hash_entry *edir, *eind;
6022 #ifdef INCLUDE_SHMEDIA
6023 bfd_signed_vma tmp;
6024 #endif
6026 edir = (struct elf_sh_link_hash_entry *) dir;
6027 eind = (struct elf_sh_link_hash_entry *) ind;
6029 if (eind->dyn_relocs != NULL)
6031 if (edir->dyn_relocs != NULL)
6033 struct elf_sh_dyn_relocs **pp;
6034 struct elf_sh_dyn_relocs *p;
6036 BFD_ASSERT (ind->root.type != bfd_link_hash_indirect);
6038 /* Add reloc counts against the weak sym to the strong sym
6039 list. Merge any entries against the same section. */
6040 for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
6042 struct elf_sh_dyn_relocs *q;
6044 for (q = edir->dyn_relocs; q != NULL; q = q->next)
6045 if (q->sec == p->sec)
6047 q->pc_count += p->pc_count;
6048 q->count += p->count;
6049 *pp = p->next;
6050 break;
6052 if (q == NULL)
6053 pp = &p->next;
6055 *pp = edir->dyn_relocs;
6058 edir->dyn_relocs = eind->dyn_relocs;
6059 eind->dyn_relocs = NULL;
6061 edir->gotplt_refcount = eind->gotplt_refcount;
6062 eind->gotplt_refcount = 0;
6063 #ifdef INCLUDE_SHMEDIA
6064 tmp = edir->datalabel_got.refcount;
6065 if (tmp < 1)
6067 edir->datalabel_got.refcount = eind->datalabel_got.refcount;
6068 eind->datalabel_got.refcount = tmp;
6070 else
6071 BFD_ASSERT (eind->datalabel_got.refcount < 1);
6072 #endif
6074 if (ind->root.type == bfd_link_hash_indirect
6075 && dir->got.refcount <= 0)
6077 edir->tls_type = eind->tls_type;
6078 eind->tls_type = GOT_UNKNOWN;
6081 if (ind->root.type != bfd_link_hash_indirect
6082 && (dir->elf_link_hash_flags & ELF_LINK_HASH_DYNAMIC_ADJUSTED) != 0)
6083 /* If called to transfer flags for a weakdef during processing
6084 of elf_adjust_dynamic_symbol, don't copy ELF_LINK_NON_GOT_REF.
6085 We clear it ourselves for ELIMINATE_COPY_RELOCS. */
6086 dir->elf_link_hash_flags |=
6087 (ind->elf_link_hash_flags & (ELF_LINK_HASH_REF_DYNAMIC
6088 | ELF_LINK_HASH_REF_REGULAR
6089 | ELF_LINK_HASH_REF_REGULAR_NONWEAK));
6090 else
6091 _bfd_elf_link_hash_copy_indirect (bed, dir, ind);
6094 static int
6095 sh_elf_optimized_tls_reloc (struct bfd_link_info *info, int r_type,
6096 int is_local)
6098 if (info->shared)
6099 return r_type;
6101 switch (r_type)
6103 case R_SH_TLS_GD_32:
6104 case R_SH_TLS_IE_32:
6105 if (is_local)
6106 return R_SH_TLS_LE_32;
6107 return R_SH_TLS_IE_32;
6108 case R_SH_TLS_LD_32:
6109 return R_SH_TLS_LE_32;
6112 return r_type;
6115 /* Look through the relocs for a section during the first phase.
6116 Since we don't do .gots or .plts, we just need to consider the
6117 virtual table relocs for gc. */
6119 static bfd_boolean
6120 sh_elf_check_relocs (bfd *abfd, struct bfd_link_info *info, asection *sec,
6121 const Elf_Internal_Rela *relocs)
6123 Elf_Internal_Shdr *symtab_hdr;
6124 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
6125 struct elf_sh_link_hash_table *htab;
6126 const Elf_Internal_Rela *rel;
6127 const Elf_Internal_Rela *rel_end;
6128 bfd_vma *local_got_offsets;
6129 asection *sgot;
6130 asection *srelgot;
6131 asection *sreloc;
6132 unsigned int r_type;
6133 int tls_type, old_tls_type;
6135 sgot = NULL;
6136 srelgot = NULL;
6137 sreloc = NULL;
6139 if (info->relocatable)
6140 return TRUE;
6142 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
6143 sym_hashes = elf_sym_hashes (abfd);
6144 sym_hashes_end = sym_hashes + symtab_hdr->sh_size/sizeof (Elf32_External_Sym);
6145 if (!elf_bad_symtab (abfd))
6146 sym_hashes_end -= symtab_hdr->sh_info;
6148 htab = sh_elf_hash_table (info);
6149 local_got_offsets = elf_local_got_offsets (abfd);
6151 rel_end = relocs + sec->reloc_count;
6152 for (rel = relocs; rel < rel_end; rel++)
6154 struct elf_link_hash_entry *h;
6155 unsigned long r_symndx;
6156 #ifdef INCLUDE_SHMEDIA
6157 int seen_stt_datalabel = 0;
6158 #endif
6160 r_symndx = ELF32_R_SYM (rel->r_info);
6161 r_type = ELF32_R_TYPE (rel->r_info);
6163 if (r_symndx < symtab_hdr->sh_info)
6164 h = NULL;
6165 else
6167 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
6168 #ifdef INCLUDE_SHMEDIA
6169 while (h->root.type == bfd_link_hash_indirect
6170 || h->root.type == bfd_link_hash_warning)
6172 seen_stt_datalabel |= h->type == STT_DATALABEL;
6173 h = (struct elf_link_hash_entry *) h->root.u.i.link;
6175 #endif
6178 r_type = sh_elf_optimized_tls_reloc (info, r_type, h == NULL);
6179 if (! info->shared
6180 && r_type == R_SH_TLS_IE_32
6181 && h != NULL
6182 && h->root.type != bfd_link_hash_undefined
6183 && h->root.type != bfd_link_hash_undefweak
6184 && (h->dynindx == -1
6185 || (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR)))
6186 r_type = R_SH_TLS_LE_32;
6188 /* Some relocs require a global offset table. */
6189 if (htab->sgot == NULL)
6191 switch (r_type)
6193 case R_SH_GOTPLT32:
6194 case R_SH_GOT32:
6195 case R_SH_GOTOFF:
6196 case R_SH_GOTPC:
6197 #ifdef INCLUDE_SHMEDIA
6198 case R_SH_GOTPLT_LOW16:
6199 case R_SH_GOTPLT_MEDLOW16:
6200 case R_SH_GOTPLT_MEDHI16:
6201 case R_SH_GOTPLT_HI16:
6202 case R_SH_GOTPLT10BY4:
6203 case R_SH_GOTPLT10BY8:
6204 case R_SH_GOT_LOW16:
6205 case R_SH_GOT_MEDLOW16:
6206 case R_SH_GOT_MEDHI16:
6207 case R_SH_GOT_HI16:
6208 case R_SH_GOT10BY4:
6209 case R_SH_GOT10BY8:
6210 case R_SH_GOTOFF_LOW16:
6211 case R_SH_GOTOFF_MEDLOW16:
6212 case R_SH_GOTOFF_MEDHI16:
6213 case R_SH_GOTOFF_HI16:
6214 case R_SH_GOTPC_LOW16:
6215 case R_SH_GOTPC_MEDLOW16:
6216 case R_SH_GOTPC_MEDHI16:
6217 case R_SH_GOTPC_HI16:
6218 #endif
6219 case R_SH_TLS_GD_32:
6220 case R_SH_TLS_LD_32:
6221 case R_SH_TLS_IE_32:
6222 if (htab->sgot == NULL)
6224 if (htab->root.dynobj == NULL)
6225 htab->root.dynobj = abfd;
6226 if (!create_got_section (htab->root.dynobj, info))
6227 return FALSE;
6229 break;
6231 default:
6232 break;
6236 switch (r_type)
6238 /* This relocation describes the C++ object vtable hierarchy.
6239 Reconstruct it for later use during GC. */
6240 case R_SH_GNU_VTINHERIT:
6241 if (!_bfd_elf32_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
6242 return FALSE;
6243 break;
6245 /* This relocation describes which C++ vtable entries are actually
6246 used. Record for later use during GC. */
6247 case R_SH_GNU_VTENTRY:
6248 if (!_bfd_elf32_gc_record_vtentry (abfd, sec, h, rel->r_addend))
6249 return FALSE;
6250 break;
6252 case R_SH_TLS_IE_32:
6253 if (info->shared)
6254 info->flags |= DF_STATIC_TLS;
6256 /* FALLTHROUGH */
6257 force_got:
6258 case R_SH_TLS_GD_32:
6259 case R_SH_GOT32:
6260 #ifdef INCLUDE_SHMEDIA
6261 case R_SH_GOT_LOW16:
6262 case R_SH_GOT_MEDLOW16:
6263 case R_SH_GOT_MEDHI16:
6264 case R_SH_GOT_HI16:
6265 case R_SH_GOT10BY4:
6266 case R_SH_GOT10BY8:
6267 #endif
6268 switch (r_type)
6270 default:
6271 tls_type = GOT_NORMAL;
6272 break;
6273 case R_SH_TLS_GD_32:
6274 tls_type = GOT_TLS_GD;
6275 break;
6276 case R_SH_TLS_IE_32:
6277 tls_type = GOT_TLS_IE;
6278 break;
6281 if (h != NULL)
6283 #ifdef INCLUDE_SHMEDIA
6284 if (seen_stt_datalabel)
6286 struct elf_sh_link_hash_entry *eh
6287 = (struct elf_sh_link_hash_entry *) h;
6289 eh->datalabel_got.refcount += 1;
6291 else
6292 #endif
6293 h->got.refcount += 1;
6294 old_tls_type = sh_elf_hash_entry (h)->tls_type;
6296 else
6298 bfd_signed_vma *local_got_refcounts;
6300 /* This is a global offset table entry for a local
6301 symbol. */
6302 local_got_refcounts = elf_local_got_refcounts (abfd);
6303 if (local_got_refcounts == NULL)
6305 bfd_size_type size;
6307 size = symtab_hdr->sh_info;
6308 size *= sizeof (bfd_signed_vma);
6309 #ifdef INCLUDE_SHMEDIA
6310 /* Reserve space for both the datalabel and
6311 codelabel local GOT offsets. */
6312 size *= 2;
6313 #endif
6314 size += symtab_hdr->sh_info;
6315 local_got_refcounts = ((bfd_signed_vma *)
6316 bfd_zalloc (abfd, size));
6317 if (local_got_refcounts == NULL)
6318 return FALSE;
6319 elf_local_got_refcounts (abfd) = local_got_refcounts;
6320 #ifdef INCLUDE_SHMEDIA
6321 /* Take care of both the datalabel and codelabel local
6322 GOT offsets. */
6323 sh_elf_local_got_tls_type (abfd)
6324 = (char *) (local_got_refcounts + 2 * symtab_hdr->sh_info);
6325 #else
6326 sh_elf_local_got_tls_type (abfd)
6327 = (char *) (local_got_refcounts + symtab_hdr->sh_info);
6328 #endif
6330 #ifdef INCLUDE_SHMEDIA
6331 if (rel->r_addend & 1)
6332 local_got_refcounts[symtab_hdr->sh_info + r_symndx] += 1;
6333 else
6334 #endif
6335 local_got_refcounts[r_symndx] += 1;
6336 old_tls_type = sh_elf_local_got_tls_type (abfd) [r_symndx];
6339 /* If a TLS symbol is accessed using IE at least once,
6340 there is no point to use dynamic model for it. */
6341 if (old_tls_type != tls_type && old_tls_type != GOT_UNKNOWN
6342 && (old_tls_type != GOT_TLS_GD || tls_type != GOT_TLS_IE))
6344 if (old_tls_type == GOT_TLS_IE && tls_type == GOT_TLS_GD)
6345 tls_type = GOT_TLS_IE;
6346 else
6348 (*_bfd_error_handler)
6349 (_("%s: `%s' accessed both as normal and thread local symbol"),
6350 bfd_archive_filename (abfd), h->root.root.string);
6351 return FALSE;
6355 if (old_tls_type != tls_type)
6357 if (h != NULL)
6358 sh_elf_hash_entry (h)->tls_type = tls_type;
6359 else
6360 sh_elf_local_got_tls_type (abfd) [r_symndx] = tls_type;
6363 break;
6365 case R_SH_TLS_LD_32:
6366 sh_elf_hash_table(info)->tls_ldm_got.refcount += 1;
6367 break;
6369 case R_SH_GOTPLT32:
6370 #ifdef INCLUDE_SHMEDIA
6371 case R_SH_GOTPLT_LOW16:
6372 case R_SH_GOTPLT_MEDLOW16:
6373 case R_SH_GOTPLT_MEDHI16:
6374 case R_SH_GOTPLT_HI16:
6375 case R_SH_GOTPLT10BY4:
6376 case R_SH_GOTPLT10BY8:
6377 #endif
6378 /* If this is a local symbol, we resolve it directly without
6379 creating a procedure linkage table entry. */
6381 if (h == NULL
6382 || (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL)
6383 || ! info->shared
6384 || info->symbolic
6385 || h->dynindx == -1)
6386 goto force_got;
6388 h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_PLT;
6389 h->plt.refcount += 1;
6390 ((struct elf_sh_link_hash_entry *) h)->gotplt_refcount += 1;
6392 break;
6394 case R_SH_PLT32:
6395 #ifdef INCLUDE_SHMEDIA
6396 case R_SH_PLT_LOW16:
6397 case R_SH_PLT_MEDLOW16:
6398 case R_SH_PLT_MEDHI16:
6399 case R_SH_PLT_HI16:
6400 #endif
6401 /* This symbol requires a procedure linkage table entry. We
6402 actually build the entry in adjust_dynamic_symbol,
6403 because this might be a case of linking PIC code which is
6404 never referenced by a dynamic object, in which case we
6405 don't need to generate a procedure linkage table entry
6406 after all. */
6408 /* If this is a local symbol, we resolve it directly without
6409 creating a procedure linkage table entry. */
6410 if (h == NULL)
6411 continue;
6413 if (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL)
6414 break;
6416 h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_PLT;
6417 h->plt.refcount += 1;
6418 break;
6420 case R_SH_DIR32:
6421 case R_SH_REL32:
6422 if (h != NULL && ! info->shared)
6424 h->elf_link_hash_flags |= ELF_LINK_NON_GOT_REF;
6425 h->plt.refcount += 1;
6428 /* If we are creating a shared library, and this is a reloc
6429 against a global symbol, or a non PC relative reloc
6430 against a local symbol, then we need to copy the reloc
6431 into the shared library. However, if we are linking with
6432 -Bsymbolic, we do not need to copy a reloc against a
6433 global symbol which is defined in an object we are
6434 including in the link (i.e., DEF_REGULAR is set). At
6435 this point we have not seen all the input files, so it is
6436 possible that DEF_REGULAR is not set now but will be set
6437 later (it is never cleared). We account for that
6438 possibility below by storing information in the
6439 dyn_relocs field of the hash table entry. A similar
6440 situation occurs when creating shared libraries and symbol
6441 visibility changes render the symbol local.
6443 If on the other hand, we are creating an executable, we
6444 may need to keep relocations for symbols satisfied by a
6445 dynamic library if we manage to avoid copy relocs for the
6446 symbol. */
6447 if ((info->shared
6448 && (sec->flags & SEC_ALLOC) != 0
6449 && (r_type != R_SH_REL32
6450 || (h != NULL
6451 && (! info->symbolic
6452 || h->root.type == bfd_link_hash_defweak
6453 || (h->elf_link_hash_flags
6454 & ELF_LINK_HASH_DEF_REGULAR) == 0))))
6455 || (! info->shared
6456 && (sec->flags & SEC_ALLOC) != 0
6457 && h != NULL
6458 && (h->root.type == bfd_link_hash_defweak
6459 || (h->elf_link_hash_flags
6460 & ELF_LINK_HASH_DEF_REGULAR) == 0)))
6462 struct elf_sh_dyn_relocs *p;
6463 struct elf_sh_dyn_relocs **head;
6465 if (htab->root.dynobj == NULL)
6466 htab->root.dynobj = abfd;
6468 /* When creating a shared object, we must copy these
6469 reloc types into the output file. We create a reloc
6470 section in dynobj and make room for this reloc. */
6471 if (sreloc == NULL)
6473 const char *name;
6475 name = (bfd_elf_string_from_elf_section
6476 (abfd,
6477 elf_elfheader (abfd)->e_shstrndx,
6478 elf_section_data (sec)->rel_hdr.sh_name));
6479 if (name == NULL)
6480 return FALSE;
6482 BFD_ASSERT (strncmp (name, ".rela", 5) == 0
6483 && strcmp (bfd_get_section_name (abfd, sec),
6484 name + 5) == 0);
6486 sreloc = bfd_get_section_by_name (htab->root.dynobj, name);
6487 if (sreloc == NULL)
6489 flagword flags;
6491 sreloc = bfd_make_section (htab->root.dynobj, name);
6492 flags = (SEC_HAS_CONTENTS | SEC_READONLY
6493 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
6494 if ((sec->flags & SEC_ALLOC) != 0)
6495 flags |= SEC_ALLOC | SEC_LOAD;
6496 if (sreloc == NULL
6497 || ! bfd_set_section_flags (htab->root.dynobj,
6498 sreloc, flags)
6499 || ! bfd_set_section_alignment (htab->root.dynobj,
6500 sreloc, 2))
6501 return FALSE;
6503 elf_section_data (sec)->sreloc = sreloc;
6506 /* If this is a global symbol, we count the number of
6507 relocations we need for this symbol. */
6508 if (h != NULL)
6509 head = &((struct elf_sh_link_hash_entry *) h)->dyn_relocs;
6510 else
6512 asection *s;
6514 /* Track dynamic relocs needed for local syms too. */
6515 s = bfd_section_from_r_symndx (abfd, &htab->sym_sec,
6516 sec, r_symndx);
6517 if (s == NULL)
6518 return FALSE;
6520 head = ((struct elf_sh_dyn_relocs **)
6521 &elf_section_data (s)->local_dynrel);
6524 p = *head;
6525 if (p == NULL || p->sec != sec)
6527 bfd_size_type amt = sizeof (*p);
6528 p = bfd_alloc (htab->root.dynobj, amt);
6529 if (p == NULL)
6530 return FALSE;
6531 p->next = *head;
6532 *head = p;
6533 p->sec = sec;
6534 p->count = 0;
6535 p->pc_count = 0;
6538 p->count += 1;
6539 if (r_type == R_SH_REL32)
6540 p->pc_count += 1;
6543 break;
6545 case R_SH_TLS_LE_32:
6546 if (info->shared)
6548 (*_bfd_error_handler) (_("%s: TLS local exec code cannot be linked into shared objects"),
6549 bfd_archive_filename (abfd));
6550 return FALSE;
6553 break;
6555 case R_SH_TLS_LDO_32:
6556 /* Nothing to do. */
6557 break;
6559 default:
6560 break;
6564 return TRUE;
6567 #ifndef sh_elf_set_mach_from_flags
6568 static bfd_boolean
6569 sh_elf_set_mach_from_flags (bfd *abfd)
6571 flagword flags = elf_elfheader (abfd)->e_flags;
6573 switch (flags & EF_SH_MACH_MASK)
6575 case EF_SH1:
6576 bfd_default_set_arch_mach (abfd, bfd_arch_sh, bfd_mach_sh);
6577 break;
6578 case EF_SH2:
6579 bfd_default_set_arch_mach (abfd, bfd_arch_sh, bfd_mach_sh2);
6580 break;
6581 case EF_SH2E:
6582 bfd_default_set_arch_mach (abfd, bfd_arch_sh, bfd_mach_sh2e);
6583 break;
6584 case EF_SH_DSP:
6585 bfd_default_set_arch_mach (abfd, bfd_arch_sh, bfd_mach_sh_dsp);
6586 break;
6587 case EF_SH3:
6588 bfd_default_set_arch_mach (abfd, bfd_arch_sh, bfd_mach_sh3);
6589 break;
6590 case EF_SH3_DSP:
6591 bfd_default_set_arch_mach (abfd, bfd_arch_sh, bfd_mach_sh3_dsp);
6592 break;
6593 case EF_SH3E:
6594 bfd_default_set_arch_mach (abfd, bfd_arch_sh, bfd_mach_sh3e);
6595 break;
6596 case EF_SH_UNKNOWN:
6597 case EF_SH4:
6598 bfd_default_set_arch_mach (abfd, bfd_arch_sh, bfd_mach_sh4);
6599 break;
6600 default:
6601 return FALSE;
6603 return TRUE;
6605 #endif /* not sh_elf_set_mach_from_flags */
6607 #ifndef sh_elf_set_private_flags
6608 /* Function to keep SH specific file flags. */
6610 static bfd_boolean
6611 sh_elf_set_private_flags (bfd *abfd, flagword flags)
6613 BFD_ASSERT (! elf_flags_init (abfd)
6614 || elf_elfheader (abfd)->e_flags == flags);
6616 elf_elfheader (abfd)->e_flags = flags;
6617 elf_flags_init (abfd) = TRUE;
6618 return sh_elf_set_mach_from_flags (abfd);
6620 #endif /* not sh_elf_set_private_flags */
6622 #ifndef sh_elf_copy_private_data
6623 /* Copy backend specific data from one object module to another */
6625 static bfd_boolean
6626 sh_elf_copy_private_data (bfd * ibfd, bfd * obfd)
6628 if ( bfd_get_flavour (ibfd) != bfd_target_elf_flavour
6629 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
6630 return TRUE;
6632 return sh_elf_set_private_flags (obfd, elf_elfheader (ibfd)->e_flags);
6634 #endif /* not sh_elf_copy_private_data */
6636 #ifndef sh_elf_merge_private_data
6637 /* This routine checks for linking big and little endian objects
6638 together, and for linking sh-dsp with sh3e / sh4 objects. */
6640 static bfd_boolean
6641 sh_elf_merge_private_data (bfd *ibfd, bfd *obfd)
6643 flagword old_flags, new_flags;
6645 if (! _bfd_generic_verify_endian_match (ibfd, obfd))
6646 return FALSE;
6648 if ( bfd_get_flavour (ibfd) != bfd_target_elf_flavour
6649 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
6650 return TRUE;
6652 if (! elf_flags_init (obfd))
6654 /* This happens when ld starts out with a 'blank' output file. */
6655 elf_flags_init (obfd) = TRUE;
6656 elf_elfheader (obfd)->e_flags = EF_SH1;
6658 old_flags = elf_elfheader (obfd)->e_flags;
6659 new_flags = elf_elfheader (ibfd)->e_flags;
6660 if ((EF_SH_HAS_DSP (old_flags) && EF_SH_HAS_FP (new_flags))
6661 || (EF_SH_HAS_DSP (new_flags) && EF_SH_HAS_FP (old_flags)))
6663 (*_bfd_error_handler)
6664 ("%s: uses %s instructions while previous modules use %s instructions",
6665 bfd_archive_filename (ibfd),
6666 EF_SH_HAS_DSP (new_flags) ? "dsp" : "floating point",
6667 EF_SH_HAS_DSP (new_flags) ? "floating point" : "dsp");
6668 bfd_set_error (bfd_error_bad_value);
6669 return FALSE;
6671 elf_elfheader (obfd)->e_flags = EF_SH_MERGE_MACH (old_flags, new_flags);
6673 return sh_elf_set_mach_from_flags (obfd);
6675 #endif /* not sh_elf_merge_private_data */
6677 /* Override the generic function because we need to store sh_elf_obj_tdata
6678 as the specific tdata. We set also the machine architecture from flags
6679 here. */
6681 static bfd_boolean
6682 sh_elf_object_p (bfd *abfd)
6684 struct sh_elf_obj_tdata *new_tdata;
6685 bfd_size_type amt = sizeof (struct sh_elf_obj_tdata);
6687 if (!sh_elf_set_mach_from_flags (abfd))
6688 return FALSE;
6690 /* Allocate our special target data. */
6691 new_tdata = bfd_zalloc (abfd, amt);
6692 if (new_tdata == NULL)
6693 return FALSE;
6694 new_tdata->root = *abfd->tdata.elf_obj_data;
6695 abfd->tdata.any = new_tdata;
6696 return TRUE;
6699 /* Finish up dynamic symbol handling. We set the contents of various
6700 dynamic sections here. */
6702 static bfd_boolean
6703 sh_elf_finish_dynamic_symbol (bfd *output_bfd, struct bfd_link_info *info,
6704 struct elf_link_hash_entry *h,
6705 Elf_Internal_Sym *sym)
6707 struct elf_sh_link_hash_table *htab;
6709 htab = sh_elf_hash_table (info);
6711 if (h->plt.offset != (bfd_vma) -1)
6713 asection *splt;
6714 asection *sgot;
6715 asection *srel;
6717 bfd_vma plt_index;
6718 bfd_vma got_offset;
6719 Elf_Internal_Rela rel;
6720 bfd_byte *loc;
6722 /* This symbol has an entry in the procedure linkage table. Set
6723 it up. */
6725 BFD_ASSERT (h->dynindx != -1);
6727 splt = htab->splt;
6728 sgot = htab->sgotplt;
6729 srel = htab->srelplt;
6730 BFD_ASSERT (splt != NULL && sgot != NULL && srel != NULL);
6732 /* Get the index in the procedure linkage table which
6733 corresponds to this symbol. This is the index of this symbol
6734 in all the symbols for which we are making plt entries. The
6735 first entry in the procedure linkage table is reserved. */
6736 plt_index = h->plt.offset / elf_sh_sizeof_plt (info) - 1;
6738 /* Get the offset into the .got table of the entry that
6739 corresponds to this function. Each .got entry is 4 bytes.
6740 The first three are reserved. */
6741 got_offset = (plt_index + 3) * 4;
6743 #ifdef GOT_BIAS
6744 if (info->shared)
6745 got_offset -= GOT_BIAS;
6746 #endif
6748 /* Fill in the entry in the procedure linkage table. */
6749 if (! info->shared)
6751 if (elf_sh_plt_entry == NULL)
6753 elf_sh_plt_entry = (bfd_big_endian (output_bfd) ?
6754 elf_sh_plt_entry_be : elf_sh_plt_entry_le);
6756 memcpy (splt->contents + h->plt.offset, elf_sh_plt_entry,
6757 elf_sh_sizeof_plt (info));
6758 #ifdef INCLUDE_SHMEDIA
6759 movi_shori_putval (output_bfd,
6760 (sgot->output_section->vma
6761 + sgot->output_offset
6762 + got_offset),
6763 (splt->contents + h->plt.offset
6764 + elf_sh_plt_symbol_offset (info)));
6766 /* Set bottom bit because its for a branch to SHmedia */
6767 movi_shori_putval (output_bfd,
6768 (splt->output_section->vma + splt->output_offset)
6769 | 1,
6770 (splt->contents + h->plt.offset
6771 + elf_sh_plt_plt0_offset (info)));
6772 #else
6773 bfd_put_32 (output_bfd,
6774 (sgot->output_section->vma
6775 + sgot->output_offset
6776 + got_offset),
6777 (splt->contents + h->plt.offset
6778 + elf_sh_plt_symbol_offset (info)));
6780 bfd_put_32 (output_bfd,
6781 (splt->output_section->vma + splt->output_offset),
6782 (splt->contents + h->plt.offset
6783 + elf_sh_plt_plt0_offset (info)));
6784 #endif
6786 else
6788 if (elf_sh_pic_plt_entry == NULL)
6790 elf_sh_pic_plt_entry = (bfd_big_endian (output_bfd) ?
6791 elf_sh_pic_plt_entry_be :
6792 elf_sh_pic_plt_entry_le);
6794 memcpy (splt->contents + h->plt.offset, elf_sh_pic_plt_entry,
6795 elf_sh_sizeof_plt (info));
6796 #ifdef INCLUDE_SHMEDIA
6797 movi_shori_putval (output_bfd, got_offset,
6798 (splt->contents + h->plt.offset
6799 + elf_sh_plt_symbol_offset (info)));
6800 #else
6801 bfd_put_32 (output_bfd, got_offset,
6802 (splt->contents + h->plt.offset
6803 + elf_sh_plt_symbol_offset (info)));
6804 #endif
6807 #ifdef GOT_BIAS
6808 if (info->shared)
6809 got_offset += GOT_BIAS;
6810 #endif
6812 #ifdef INCLUDE_SHMEDIA
6813 movi_shori_putval (output_bfd,
6814 plt_index * sizeof (Elf32_External_Rela),
6815 (splt->contents + h->plt.offset
6816 + elf_sh_plt_reloc_offset (info)));
6817 #else
6818 bfd_put_32 (output_bfd, plt_index * sizeof (Elf32_External_Rela),
6819 (splt->contents + h->plt.offset
6820 + elf_sh_plt_reloc_offset (info)));
6821 #endif
6823 /* Fill in the entry in the global offset table. */
6824 bfd_put_32 (output_bfd,
6825 (splt->output_section->vma
6826 + splt->output_offset
6827 + h->plt.offset
6828 + elf_sh_plt_temp_offset (info)),
6829 sgot->contents + got_offset);
6831 /* Fill in the entry in the .rela.plt section. */
6832 rel.r_offset = (sgot->output_section->vma
6833 + sgot->output_offset
6834 + got_offset);
6835 rel.r_info = ELF32_R_INFO (h->dynindx, R_SH_JMP_SLOT);
6836 rel.r_addend = 0;
6837 #ifdef GOT_BIAS
6838 rel.r_addend = GOT_BIAS;
6839 #endif
6840 loc = srel->contents + plt_index * sizeof (Elf32_External_Rela);
6841 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
6843 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
6845 /* Mark the symbol as undefined, rather than as defined in
6846 the .plt section. Leave the value alone. */
6847 sym->st_shndx = SHN_UNDEF;
6851 if (h->got.offset != (bfd_vma) -1
6852 && sh_elf_hash_entry (h)->tls_type != GOT_TLS_GD
6853 && sh_elf_hash_entry (h)->tls_type != GOT_TLS_IE)
6855 asection *sgot;
6856 asection *srel;
6857 Elf_Internal_Rela rel;
6858 bfd_byte *loc;
6860 /* This symbol has an entry in the global offset table. Set it
6861 up. */
6863 sgot = htab->sgot;
6864 srel = htab->srelgot;
6865 BFD_ASSERT (sgot != NULL && srel != NULL);
6867 rel.r_offset = (sgot->output_section->vma
6868 + sgot->output_offset
6869 + (h->got.offset &~ (bfd_vma) 1));
6871 /* If this is a static link, or it is a -Bsymbolic link and the
6872 symbol is defined locally or was forced to be local because
6873 of a version file, we just want to emit a RELATIVE reloc.
6874 The entry in the global offset table will already have been
6875 initialized in the relocate_section function. */
6876 if (info->shared
6877 && SYMBOL_REFERENCES_LOCAL (info, h))
6879 rel.r_info = ELF32_R_INFO (0, R_SH_RELATIVE);
6880 rel.r_addend = (h->root.u.def.value
6881 + h->root.u.def.section->output_section->vma
6882 + h->root.u.def.section->output_offset);
6884 else
6886 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + h->got.offset);
6887 rel.r_info = ELF32_R_INFO (h->dynindx, R_SH_GLOB_DAT);
6888 rel.r_addend = 0;
6891 loc = srel->contents;
6892 loc += srel->reloc_count++ * sizeof (Elf32_External_Rela);
6893 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
6896 #ifdef INCLUDE_SHMEDIA
6898 struct elf_sh_link_hash_entry *eh;
6900 eh = (struct elf_sh_link_hash_entry *) h;
6901 if (eh->datalabel_got.offset != (bfd_vma) -1)
6903 asection *sgot;
6904 asection *srel;
6905 Elf_Internal_Rela rel;
6906 bfd_byte *loc;
6908 /* This symbol has a datalabel entry in the global offset table.
6909 Set it up. */
6911 sgot = htab->sgot;
6912 srel = htab->srelgot;
6913 BFD_ASSERT (sgot != NULL && srel != NULL);
6915 rel.r_offset = (sgot->output_section->vma
6916 + sgot->output_offset
6917 + (eh->datalabel_got.offset &~ (bfd_vma) 1));
6919 /* If this is a static link, or it is a -Bsymbolic link and the
6920 symbol is defined locally or was forced to be local because
6921 of a version file, we just want to emit a RELATIVE reloc.
6922 The entry in the global offset table will already have been
6923 initialized in the relocate_section function. */
6924 if (info->shared
6925 && SYMBOL_REFERENCES_LOCAL (info, h))
6927 rel.r_info = ELF32_R_INFO (0, R_SH_RELATIVE);
6928 rel.r_addend = (h->root.u.def.value
6929 + h->root.u.def.section->output_section->vma
6930 + h->root.u.def.section->output_offset);
6932 else
6934 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents
6935 + eh->datalabel_got.offset);
6936 rel.r_info = ELF32_R_INFO (h->dynindx, R_SH_GLOB_DAT);
6937 rel.r_addend = 0;
6940 loc = srel->contents;
6941 loc += srel->reloc_count++ * sizeof (Elf32_External_Rela);
6942 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
6945 #endif
6947 if ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_COPY) != 0)
6949 asection *s;
6950 Elf_Internal_Rela rel;
6951 bfd_byte *loc;
6953 /* This symbol needs a copy reloc. Set it up. */
6955 BFD_ASSERT (h->dynindx != -1
6956 && (h->root.type == bfd_link_hash_defined
6957 || h->root.type == bfd_link_hash_defweak));
6959 s = bfd_get_section_by_name (h->root.u.def.section->owner,
6960 ".rela.bss");
6961 BFD_ASSERT (s != NULL);
6963 rel.r_offset = (h->root.u.def.value
6964 + h->root.u.def.section->output_section->vma
6965 + h->root.u.def.section->output_offset);
6966 rel.r_info = ELF32_R_INFO (h->dynindx, R_SH_COPY);
6967 rel.r_addend = 0;
6968 loc = s->contents + s->reloc_count++ * sizeof (Elf32_External_Rela);
6969 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
6972 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. */
6973 if (strcmp (h->root.root.string, "_DYNAMIC") == 0
6974 || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
6975 sym->st_shndx = SHN_ABS;
6977 return TRUE;
6980 /* Finish up the dynamic sections. */
6982 static bfd_boolean
6983 sh_elf_finish_dynamic_sections (bfd *output_bfd, struct bfd_link_info *info)
6985 struct elf_sh_link_hash_table *htab;
6986 asection *sgot;
6987 asection *sdyn;
6989 htab = sh_elf_hash_table (info);
6990 sgot = htab->sgotplt;
6991 sdyn = bfd_get_section_by_name (htab->root.dynobj, ".dynamic");
6993 if (htab->root.dynamic_sections_created)
6995 asection *splt;
6996 Elf32_External_Dyn *dyncon, *dynconend;
6998 BFD_ASSERT (sgot != NULL && sdyn != NULL);
7000 dyncon = (Elf32_External_Dyn *) sdyn->contents;
7001 dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->_raw_size);
7002 for (; dyncon < dynconend; dyncon++)
7004 Elf_Internal_Dyn dyn;
7005 asection *s;
7006 #ifdef INCLUDE_SHMEDIA
7007 const char *name;
7008 #endif
7010 bfd_elf32_swap_dyn_in (htab->root.dynobj, dyncon, &dyn);
7012 switch (dyn.d_tag)
7014 default:
7015 break;
7017 #ifdef INCLUDE_SHMEDIA
7018 case DT_INIT:
7019 name = info->init_function;
7020 goto get_sym;
7022 case DT_FINI:
7023 name = info->fini_function;
7024 get_sym:
7025 if (dyn.d_un.d_val != 0)
7027 struct elf_link_hash_entry *h;
7029 h = elf_link_hash_lookup (&htab->root, name,
7030 FALSE, FALSE, TRUE);
7031 if (h != NULL && (h->other & STO_SH5_ISA32))
7033 dyn.d_un.d_val |= 1;
7034 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
7037 break;
7038 #endif
7040 case DT_PLTGOT:
7041 s = htab->sgot->output_section;
7042 goto get_vma;
7044 case DT_JMPREL:
7045 s = htab->srelplt->output_section;
7046 get_vma:
7047 BFD_ASSERT (s != NULL);
7048 dyn.d_un.d_ptr = s->vma;
7049 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
7050 break;
7052 case DT_PLTRELSZ:
7053 s = htab->srelplt->output_section;
7054 BFD_ASSERT (s != NULL);
7055 if (s->_cooked_size != 0)
7056 dyn.d_un.d_val = s->_cooked_size;
7057 else
7058 dyn.d_un.d_val = s->_raw_size;
7059 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
7060 break;
7062 case DT_RELASZ:
7063 /* My reading of the SVR4 ABI indicates that the
7064 procedure linkage table relocs (DT_JMPREL) should be
7065 included in the overall relocs (DT_RELA). This is
7066 what Solaris does. However, UnixWare can not handle
7067 that case. Therefore, we override the DT_RELASZ entry
7068 here to make it not include the JMPREL relocs. Since
7069 the linker script arranges for .rela.plt to follow all
7070 other relocation sections, we don't have to worry
7071 about changing the DT_RELA entry. */
7072 if (htab->srelplt != NULL)
7074 s = htab->srelplt->output_section;
7075 if (s->_cooked_size != 0)
7076 dyn.d_un.d_val -= s->_cooked_size;
7077 else
7078 dyn.d_un.d_val -= s->_raw_size;
7080 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
7081 break;
7085 /* Fill in the first entry in the procedure linkage table. */
7086 splt = htab->splt;
7087 if (splt && splt->_raw_size > 0)
7089 if (info->shared)
7091 if (elf_sh_pic_plt_entry == NULL)
7093 elf_sh_pic_plt_entry = (bfd_big_endian (output_bfd) ?
7094 elf_sh_pic_plt_entry_be :
7095 elf_sh_pic_plt_entry_le);
7097 memcpy (splt->contents, elf_sh_pic_plt_entry,
7098 elf_sh_sizeof_plt (info));
7100 else
7102 if (elf_sh_plt0_entry == NULL)
7104 elf_sh_plt0_entry = (bfd_big_endian (output_bfd) ?
7105 elf_sh_plt0_entry_be :
7106 elf_sh_plt0_entry_le);
7108 memcpy (splt->contents, elf_sh_plt0_entry, PLT_ENTRY_SIZE);
7109 #ifdef INCLUDE_SHMEDIA
7110 movi_shori_putval (output_bfd,
7111 sgot->output_section->vma
7112 + sgot->output_offset,
7113 splt->contents
7114 + elf_sh_plt0_gotplt_offset (info));
7115 #else
7116 bfd_put_32 (output_bfd,
7117 sgot->output_section->vma + sgot->output_offset + 4,
7118 splt->contents + elf_sh_plt0_gotid_offset (info));
7119 bfd_put_32 (output_bfd,
7120 sgot->output_section->vma + sgot->output_offset + 8,
7121 splt->contents + elf_sh_plt0_linker_offset (info));
7122 #endif
7125 /* UnixWare sets the entsize of .plt to 4, although that doesn't
7126 really seem like the right value. */
7127 elf_section_data (splt->output_section)->this_hdr.sh_entsize = 4;
7131 /* Fill in the first three entries in the global offset table. */
7132 if (sgot && sgot->_raw_size > 0)
7134 if (sdyn == NULL)
7135 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents);
7136 else
7137 bfd_put_32 (output_bfd,
7138 sdyn->output_section->vma + sdyn->output_offset,
7139 sgot->contents);
7140 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 4);
7141 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 8);
7143 elf_section_data (sgot->output_section)->this_hdr.sh_entsize = 4;
7146 return TRUE;
7149 static enum elf_reloc_type_class
7150 sh_elf_reloc_type_class (const Elf_Internal_Rela *rela)
7152 switch ((int) ELF32_R_TYPE (rela->r_info))
7154 case R_SH_RELATIVE:
7155 return reloc_class_relative;
7156 case R_SH_JMP_SLOT:
7157 return reloc_class_plt;
7158 case R_SH_COPY:
7159 return reloc_class_copy;
7160 default:
7161 return reloc_class_normal;
7165 /* Support for Linux core dump NOTE sections. */
7166 static bfd_boolean
7167 elf32_shlin_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
7169 int offset;
7170 unsigned int raw_size;
7172 switch (note->descsz)
7174 default:
7175 return FALSE;
7177 case 168: /* Linux/SH */
7178 /* pr_cursig */
7179 elf_tdata (abfd)->core_signal = bfd_get_16 (abfd, note->descdata + 12);
7181 /* pr_pid */
7182 elf_tdata (abfd)->core_pid = bfd_get_32 (abfd, note->descdata + 24);
7184 /* pr_reg */
7185 offset = 72;
7186 raw_size = 92;
7188 break;
7191 /* Make a ".reg/999" section. */
7192 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
7193 raw_size, note->descpos + offset);
7196 static bfd_boolean
7197 elf32_shlin_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
7199 switch (note->descsz)
7201 default:
7202 return FALSE;
7204 case 124: /* Linux/SH elf_prpsinfo */
7205 elf_tdata (abfd)->core_program
7206 = _bfd_elfcore_strndup (abfd, note->descdata + 28, 16);
7207 elf_tdata (abfd)->core_command
7208 = _bfd_elfcore_strndup (abfd, note->descdata + 44, 80);
7211 /* Note that for some reason, a spurious space is tacked
7212 onto the end of the args in some (at least one anyway)
7213 implementations, so strip it off if it exists. */
7216 char *command = elf_tdata (abfd)->core_command;
7217 int n = strlen (command);
7219 if (0 < n && command[n - 1] == ' ')
7220 command[n - 1] = '\0';
7223 return TRUE;
7226 #define TARGET_BIG_SYM bfd_elf32_sh_vec
7227 #define TARGET_BIG_NAME "elf32-sh"
7228 #define TARGET_LITTLE_SYM bfd_elf32_shl_vec
7229 #define TARGET_LITTLE_NAME "elf32-shl"
7230 #define ELF_ARCH bfd_arch_sh
7231 #define ELF_MACHINE_CODE EM_SH
7232 #ifdef __QNXTARGET__
7233 #define ELF_MAXPAGESIZE 0x1000
7234 #else
7235 #define ELF_MAXPAGESIZE 0x80
7236 #endif
7238 #define elf_symbol_leading_char '_'
7240 #define bfd_elf32_bfd_reloc_type_lookup sh_elf_reloc_type_lookup
7241 #define elf_info_to_howto sh_elf_info_to_howto
7242 #define bfd_elf32_bfd_relax_section sh_elf_relax_section
7243 #define elf_backend_relocate_section sh_elf_relocate_section
7244 #define bfd_elf32_bfd_get_relocated_section_contents \
7245 sh_elf_get_relocated_section_contents
7246 #define bfd_elf32_mkobject sh_elf_mkobject
7247 #define elf_backend_object_p sh_elf_object_p
7248 #define bfd_elf32_bfd_set_private_bfd_flags \
7249 sh_elf_set_private_flags
7250 #define bfd_elf32_bfd_copy_private_bfd_data \
7251 sh_elf_copy_private_data
7252 #define bfd_elf32_bfd_merge_private_bfd_data \
7253 sh_elf_merge_private_data
7255 #define elf_backend_gc_mark_hook sh_elf_gc_mark_hook
7256 #define elf_backend_gc_sweep_hook sh_elf_gc_sweep_hook
7257 #define elf_backend_check_relocs sh_elf_check_relocs
7258 #define elf_backend_copy_indirect_symbol \
7259 sh_elf_copy_indirect_symbol
7260 #define elf_backend_create_dynamic_sections \
7261 sh_elf_create_dynamic_sections
7262 #define bfd_elf32_bfd_link_hash_table_create \
7263 sh_elf_link_hash_table_create
7264 #define elf_backend_adjust_dynamic_symbol \
7265 sh_elf_adjust_dynamic_symbol
7266 #define elf_backend_size_dynamic_sections \
7267 sh_elf_size_dynamic_sections
7268 #define elf_backend_finish_dynamic_symbol \
7269 sh_elf_finish_dynamic_symbol
7270 #define elf_backend_finish_dynamic_sections \
7271 sh_elf_finish_dynamic_sections
7272 #define elf_backend_reloc_type_class sh_elf_reloc_type_class
7274 #define elf_backend_can_gc_sections 1
7275 #define elf_backend_can_refcount 1
7276 #define elf_backend_want_got_plt 1
7277 #define elf_backend_plt_readonly 1
7278 #define elf_backend_want_plt_sym 0
7279 #define elf_backend_got_header_size 12
7280 #define elf_backend_plt_header_size PLT_ENTRY_SIZE
7282 #ifndef INCLUDE_SHMEDIA
7284 #include "elf32-target.h"
7286 /* NetBSD support. */
7287 #undef TARGET_BIG_SYM
7288 #define TARGET_BIG_SYM bfd_elf32_shnbsd_vec
7289 #undef TARGET_BIG_NAME
7290 #define TARGET_BIG_NAME "elf32-sh-nbsd"
7291 #undef TARGET_LITTLE_SYM
7292 #define TARGET_LITTLE_SYM bfd_elf32_shlnbsd_vec
7293 #undef TARGET_LITTLE_NAME
7294 #define TARGET_LITTLE_NAME "elf32-shl-nbsd"
7295 #undef ELF_MAXPAGESIZE
7296 #define ELF_MAXPAGESIZE 0x10000
7297 #undef elf_symbol_leading_char
7298 #define elf_symbol_leading_char 0
7299 #undef elf32_bed
7300 #define elf32_bed elf32_sh_nbsd_bed
7302 #include "elf32-target.h"
7305 /* Linux support. */
7306 #undef TARGET_BIG_SYM
7307 #define TARGET_BIG_SYM bfd_elf32_shblin_vec
7308 #undef TARGET_BIG_NAME
7309 #define TARGET_BIG_NAME "elf32-shbig-linux"
7310 #undef TARGET_LITTLE_SYM
7311 #define TARGET_LITTLE_SYM bfd_elf32_shlin_vec
7312 #undef TARGET_LITTLE_NAME
7313 #define TARGET_LITTLE_NAME "elf32-sh-linux"
7315 #undef elf_backend_grok_prstatus
7316 #define elf_backend_grok_prstatus elf32_shlin_grok_prstatus
7317 #undef elf_backend_grok_psinfo
7318 #define elf_backend_grok_psinfo elf32_shlin_grok_psinfo
7319 #undef elf32_bed
7320 #define elf32_bed elf32_sh_lin_bed
7322 #include "elf32-target.h"
7324 #endif /* INCLUDE_SHMEDIA */