789ee7826d4ac506aadf4fa783b8101ae0ccba9c
[deliverable/binutils-gdb.git] / bfd / elf32-sh.c
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.
5
6 This file is part of BFD, the Binary File Descriptor library.
7
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.
12
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.
17
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. */
21
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"
28
29 static bfd_reloc_status_type sh_elf_reloc
30 PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
31 static bfd_reloc_status_type sh_elf_ignore_reloc
32 PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
33 static reloc_howto_type *sh_elf_reloc_type_lookup
34 PARAMS ((bfd *, bfd_reloc_code_real_type));
35 static void sh_elf_info_to_howto
36 PARAMS ((bfd *, arelent *, Elf_Internal_Rela *));
37 static bfd_boolean sh_elf_set_private_flags
38 PARAMS ((bfd *, flagword));
39 static bfd_boolean sh_elf_copy_private_data
40 PARAMS ((bfd *, bfd *));
41 static bfd_boolean sh_elf_merge_private_data
42 PARAMS ((bfd *, bfd *));
43 static bfd_boolean sh_elf_set_mach_from_flags
44 PARAMS ((bfd *));
45 static bfd_boolean sh_elf_relax_section
46 PARAMS ((bfd *, asection *, struct bfd_link_info *, bfd_boolean *));
47 static bfd_boolean sh_elf_relax_delete_bytes
48 PARAMS ((bfd *, asection *, bfd_vma, int));
49 static bfd_boolean sh_elf_align_loads
50 PARAMS ((bfd *, asection *, Elf_Internal_Rela *, bfd_byte *, bfd_boolean *));
51 static bfd_boolean sh_elf_swap_insns
52 PARAMS ((bfd *, asection *, PTR, bfd_byte *, bfd_vma));
53 static bfd_boolean sh_elf_relocate_section
54 PARAMS ((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 PARAMS ((bfd *, struct bfd_link_info *, struct bfd_link_order *,
58 bfd_byte *, bfd_boolean, asymbol **));
59 static void sh_elf_copy_indirect_symbol
60 PARAMS ((struct elf_backend_data *, struct elf_link_hash_entry *,
61 struct elf_link_hash_entry *));
62 static int sh_elf_optimized_tls_reloc
63 PARAMS ((struct bfd_link_info *, int, int));
64 static bfd_boolean sh_elf_mkobject
65 PARAMS ((bfd *));
66 static bfd_boolean sh_elf_object_p
67 PARAMS ((bfd *));
68 static bfd_boolean sh_elf_check_relocs
69 PARAMS ((bfd *, struct bfd_link_info *, asection *,
70 const Elf_Internal_Rela *));
71 static struct bfd_hash_entry *sh_elf_link_hash_newfunc
72 PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *, const char *));
73 static struct bfd_link_hash_table *sh_elf_link_hash_table_create
74 PARAMS ((bfd *));
75 static bfd_boolean sh_elf_adjust_dynamic_symbol
76 PARAMS ((struct bfd_link_info *, struct elf_link_hash_entry *));
77 static bfd_boolean sh_elf_size_dynamic_sections
78 PARAMS ((bfd *, struct bfd_link_info *));
79 static bfd_boolean sh_elf_finish_dynamic_symbol
80 PARAMS ((bfd *, struct bfd_link_info *, struct elf_link_hash_entry *,
81 Elf_Internal_Sym *));
82 static bfd_boolean sh_elf_finish_dynamic_sections
83 PARAMS ((bfd *, struct bfd_link_info *));
84 static bfd_reloc_status_type sh_elf_reloc_loop
85 PARAMS ((int, bfd *, asection *, bfd_byte *, bfd_vma, asection *,
86 bfd_vma, bfd_vma));
87 static bfd_boolean create_got_section
88 PARAMS ((bfd *, struct bfd_link_info *));
89 static bfd_boolean sh_elf_create_dynamic_sections
90 PARAMS ((bfd *, struct bfd_link_info *));
91 static bfd_vma dtpoff_base
92 PARAMS ((struct bfd_link_info *));
93 static asection * sh_elf_gc_mark_hook
94 PARAMS ((asection *, struct bfd_link_info *, Elf_Internal_Rela *,
95 struct elf_link_hash_entry *, Elf_Internal_Sym *));
96 static bfd_boolean sh_elf_gc_sweep_hook
97 PARAMS ((bfd *, struct bfd_link_info *, asection *,
98 const Elf_Internal_Rela *));
99 static bfd_boolean allocate_dynrelocs
100 PARAMS ((struct elf_link_hash_entry *, PTR));
101 static bfd_boolean readonly_dynrelocs
102 PARAMS ((struct elf_link_hash_entry *, PTR));
103 static enum elf_reloc_type_class sh_elf_reloc_type_class
104 PARAMS ((const Elf_Internal_Rela *));
105 #ifdef INCLUDE_SHMEDIA
106 inline static void movi_shori_putval PARAMS ((bfd *, unsigned long, char *));
107 #endif
108 static bfd_boolean elf32_shlin_grok_prstatus
109 PARAMS ((bfd *abfd, Elf_Internal_Note *note));
110 static bfd_boolean elf32_shlin_grok_psinfo
111 PARAMS ((bfd *abfd, Elf_Internal_Note *note));
112
113 /* The name of the dynamic interpreter. This is put in the .interp
114 section. */
115
116 #define ELF_DYNAMIC_INTERPRETER "/usr/lib/libc.so.1"
117
118 static reloc_howto_type sh_elf_howto_table[] =
119 {
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 */
134
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 */
150
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 */
165
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 */
180
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 */
197
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 */
212
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 */
227
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 */
244
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 */
261
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 */
278
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),
294
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. */
299
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 */
316
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 */
333
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 */
351
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 */
369
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 */
386
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 */
403
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 */
419
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 */
436
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 */
453
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 */
468
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 */
483
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 */
498
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 */
513
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),
521
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 */
537
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 */
552
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 */
567
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 */
582
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 */
597
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 */
612
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 */
627
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
637
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),
730
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 */
744
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 */
758
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 */
772
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 */
786
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 */
800
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 */
814
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 */
828
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 */
842
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),
851
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 */
865
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 */
879
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 */
893
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 */
907
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 */
921
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 */
935
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 */
949
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 */
963
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 */
977
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 */
993
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 */
1008
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 */
1023
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 */
1038
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 */
1053
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 */
1068
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 */
1083
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 */
1098
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 */
1113
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 */
1128
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 */
1143
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 */
1158
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 */
1173
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 */
1188
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 */
1203
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 */
1218
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 */
1233
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 */
1248
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 */
1263
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 */
1278
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 */
1293
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 */
1308
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 */
1323
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 */
1338
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 */
1352
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 */
1366
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 */
1380
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 */
1394
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),
1440
1441 /* Relocations for SHmedia code. None of these are partial_inplace or
1442 use the field being relocated (except R_SH_PT_16). */
1443
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 */
1463
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 */
1480
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 */
1495
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 */
1510
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 */
1525
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 */
1540
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 */
1555
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 */
1570
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 */
1585
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 */
1600
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 */
1615
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 */
1630
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 */
1645
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 */
1660
1661 #endif
1662 };
1663
1664 static bfd_reloc_status_type
1665 sh_elf_reloc_loop (r_type, input_bfd, input_section, contents, addr,
1666 symbol_section, start, end)
1667 int r_type ATTRIBUTE_UNUSED;
1668 bfd *input_bfd;
1669 asection *input_section;
1670 bfd_byte *contents;
1671 bfd_vma addr;
1672 asection *symbol_section;
1673 bfd_vma start, end;
1674 {
1675 static bfd_vma last_addr;
1676 static asection *last_symbol_section;
1677 bfd_byte *start_ptr, *ptr, *last_ptr;
1678 int diff, cum_diff;
1679 bfd_signed_vma x;
1680 int insn;
1681
1682 /* Sanity check the address. */
1683 if (addr > input_section->_raw_size)
1684 return bfd_reloc_outofrange;
1685
1686 /* We require the start and end relocations to be processed consecutively -
1687 although we allow then to be processed forwards or backwards. */
1688 if (! last_addr)
1689 {
1690 last_addr = addr;
1691 last_symbol_section = symbol_section;
1692 return bfd_reloc_ok;
1693 }
1694 if (last_addr != addr)
1695 abort ();
1696 last_addr = 0;
1697
1698 if (! symbol_section || last_symbol_section != symbol_section || end < start)
1699 return bfd_reloc_outofrange;
1700
1701 /* Get the symbol_section contents. */
1702 if (symbol_section != input_section)
1703 {
1704 if (elf_section_data (symbol_section)->this_hdr.contents != NULL)
1705 contents = elf_section_data (symbol_section)->this_hdr.contents;
1706 else
1707 {
1708 contents = (bfd_byte *) bfd_malloc (symbol_section->_raw_size);
1709 if (contents == NULL)
1710 return bfd_reloc_outofrange;
1711 if (! bfd_get_section_contents (input_bfd, symbol_section, contents,
1712 (file_ptr) 0,
1713 symbol_section->_raw_size))
1714 {
1715 free (contents);
1716 return bfd_reloc_outofrange;
1717 }
1718 }
1719 }
1720 #define IS_PPI(PTR) ((bfd_get_16 (input_bfd, (PTR)) & 0xfc00) == 0xf800)
1721 start_ptr = contents + start;
1722 for (cum_diff = -6, ptr = contents + end; cum_diff < 0 && ptr > start_ptr;)
1723 {
1724 for (last_ptr = ptr, ptr -= 4; ptr >= start_ptr && IS_PPI (ptr);)
1725 ptr -= 2;
1726 ptr += 2;
1727 diff = (last_ptr - ptr) >> 1;
1728 cum_diff += diff & 1;
1729 cum_diff += diff;
1730 }
1731 /* Calculate the start / end values to load into rs / re minus four -
1732 so that will cancel out the four we would otherwise have to add to
1733 addr to get the value to subtract in order to get relative addressing. */
1734 if (cum_diff >= 0)
1735 {
1736 start -= 4;
1737 end = (ptr + cum_diff * 2) - contents;
1738 }
1739 else
1740 {
1741 bfd_vma start0 = start - 4;
1742
1743 while (start0 && IS_PPI (contents + start0))
1744 start0 -= 2;
1745 start0 = start - 2 - ((start - start0) & 2);
1746 start = start0 - cum_diff - 2;
1747 end = start0;
1748 }
1749
1750 if (contents != NULL
1751 && elf_section_data (symbol_section)->this_hdr.contents != contents)
1752 free (contents);
1753
1754 insn = bfd_get_16 (input_bfd, contents + addr);
1755
1756 x = (insn & 0x200 ? end : start) - addr;
1757 if (input_section != symbol_section)
1758 x += ((symbol_section->output_section->vma + symbol_section->output_offset)
1759 - (input_section->output_section->vma
1760 + input_section->output_offset));
1761 x >>= 1;
1762 if (x < -128 || x > 127)
1763 return bfd_reloc_overflow;
1764
1765 x = (insn & ~0xff) | (x & 0xff);
1766 bfd_put_16 (input_bfd, (bfd_vma) x, contents + addr);
1767
1768 return bfd_reloc_ok;
1769 }
1770
1771 /* This function is used for normal relocs. This used to be like the COFF
1772 function, and is almost certainly incorrect for other ELF targets. */
1773
1774 static bfd_reloc_status_type
1775 sh_elf_reloc (abfd, reloc_entry, symbol_in, data, input_section, output_bfd,
1776 error_message)
1777 bfd *abfd;
1778 arelent *reloc_entry;
1779 asymbol *symbol_in;
1780 PTR data;
1781 asection *input_section;
1782 bfd *output_bfd;
1783 char **error_message ATTRIBUTE_UNUSED;
1784 {
1785 unsigned long insn;
1786 bfd_vma sym_value;
1787 enum elf_sh_reloc_type r_type;
1788 bfd_vma addr = reloc_entry->address;
1789 bfd_byte *hit_data = addr + (bfd_byte *) data;
1790
1791 r_type = (enum elf_sh_reloc_type) reloc_entry->howto->type;
1792
1793 if (output_bfd != NULL)
1794 {
1795 /* Partial linking--do nothing. */
1796 reloc_entry->address += input_section->output_offset;
1797 return bfd_reloc_ok;
1798 }
1799
1800 /* Almost all relocs have to do with relaxing. If any work must be
1801 done for them, it has been done in sh_relax_section. */
1802 if (r_type == R_SH_IND12W && (symbol_in->flags & BSF_LOCAL) != 0)
1803 return bfd_reloc_ok;
1804
1805 if (symbol_in != NULL
1806 && bfd_is_und_section (symbol_in->section))
1807 return bfd_reloc_undefined;
1808
1809 if (bfd_is_com_section (symbol_in->section))
1810 sym_value = 0;
1811 else
1812 sym_value = (symbol_in->value +
1813 symbol_in->section->output_section->vma +
1814 symbol_in->section->output_offset);
1815
1816 switch (r_type)
1817 {
1818 case R_SH_DIR32:
1819 insn = bfd_get_32 (abfd, hit_data);
1820 insn += sym_value + reloc_entry->addend;
1821 bfd_put_32 (abfd, (bfd_vma) insn, hit_data);
1822 break;
1823 case R_SH_IND12W:
1824 insn = bfd_get_16 (abfd, hit_data);
1825 sym_value += reloc_entry->addend;
1826 sym_value -= (input_section->output_section->vma
1827 + input_section->output_offset
1828 + addr
1829 + 4);
1830 sym_value += (insn & 0xfff) << 1;
1831 if (insn & 0x800)
1832 sym_value -= 0x1000;
1833 insn = (insn & 0xf000) | (sym_value & 0xfff);
1834 bfd_put_16 (abfd, (bfd_vma) insn, hit_data);
1835 if (sym_value < (bfd_vma) -0x1000 || sym_value >= 0x1000)
1836 return bfd_reloc_overflow;
1837 break;
1838 default:
1839 abort ();
1840 break;
1841 }
1842
1843 return bfd_reloc_ok;
1844 }
1845
1846 /* This function is used for relocs which are only used for relaxing,
1847 which the linker should otherwise ignore. */
1848
1849 static bfd_reloc_status_type
1850 sh_elf_ignore_reloc (abfd, reloc_entry, symbol, data, input_section,
1851 output_bfd, error_message)
1852 bfd *abfd ATTRIBUTE_UNUSED;
1853 arelent *reloc_entry;
1854 asymbol *symbol ATTRIBUTE_UNUSED;
1855 PTR data ATTRIBUTE_UNUSED;
1856 asection *input_section;
1857 bfd *output_bfd;
1858 char **error_message ATTRIBUTE_UNUSED;
1859 {
1860 if (output_bfd != NULL)
1861 reloc_entry->address += input_section->output_offset;
1862 return bfd_reloc_ok;
1863 }
1864
1865 /* This structure is used to map BFD reloc codes to SH ELF relocs. */
1866
1867 struct elf_reloc_map
1868 {
1869 bfd_reloc_code_real_type bfd_reloc_val;
1870 unsigned char elf_reloc_val;
1871 };
1872
1873 /* An array mapping BFD reloc codes to SH ELF relocs. */
1874
1875 static const struct elf_reloc_map sh_reloc_map[] =
1876 {
1877 { BFD_RELOC_NONE, R_SH_NONE },
1878 { BFD_RELOC_32, R_SH_DIR32 },
1879 { BFD_RELOC_CTOR, R_SH_DIR32 },
1880 { BFD_RELOC_32_PCREL, R_SH_REL32 },
1881 { BFD_RELOC_SH_PCDISP8BY2, R_SH_DIR8WPN },
1882 { BFD_RELOC_SH_PCDISP12BY2, R_SH_IND12W },
1883 { BFD_RELOC_SH_PCRELIMM8BY2, R_SH_DIR8WPZ },
1884 { BFD_RELOC_SH_PCRELIMM8BY4, R_SH_DIR8WPL },
1885 { BFD_RELOC_8_PCREL, R_SH_SWITCH8 },
1886 { BFD_RELOC_SH_SWITCH16, R_SH_SWITCH16 },
1887 { BFD_RELOC_SH_SWITCH32, R_SH_SWITCH32 },
1888 { BFD_RELOC_SH_USES, R_SH_USES },
1889 { BFD_RELOC_SH_COUNT, R_SH_COUNT },
1890 { BFD_RELOC_SH_ALIGN, R_SH_ALIGN },
1891 { BFD_RELOC_SH_CODE, R_SH_CODE },
1892 { BFD_RELOC_SH_DATA, R_SH_DATA },
1893 { BFD_RELOC_SH_LABEL, R_SH_LABEL },
1894 { BFD_RELOC_VTABLE_INHERIT, R_SH_GNU_VTINHERIT },
1895 { BFD_RELOC_VTABLE_ENTRY, R_SH_GNU_VTENTRY },
1896 { BFD_RELOC_SH_LOOP_START, R_SH_LOOP_START },
1897 { BFD_RELOC_SH_LOOP_END, R_SH_LOOP_END },
1898 { BFD_RELOC_SH_TLS_GD_32, R_SH_TLS_GD_32 },
1899 { BFD_RELOC_SH_TLS_LD_32, R_SH_TLS_LD_32 },
1900 { BFD_RELOC_SH_TLS_LDO_32, R_SH_TLS_LDO_32 },
1901 { BFD_RELOC_SH_TLS_IE_32, R_SH_TLS_IE_32 },
1902 { BFD_RELOC_SH_TLS_LE_32, R_SH_TLS_LE_32 },
1903 { BFD_RELOC_SH_TLS_DTPMOD32, R_SH_TLS_DTPMOD32 },
1904 { BFD_RELOC_SH_TLS_DTPOFF32, R_SH_TLS_DTPOFF32 },
1905 { BFD_RELOC_SH_TLS_TPOFF32, R_SH_TLS_TPOFF32 },
1906 { BFD_RELOC_32_GOT_PCREL, R_SH_GOT32 },
1907 { BFD_RELOC_32_PLT_PCREL, R_SH_PLT32 },
1908 { BFD_RELOC_SH_COPY, R_SH_COPY },
1909 { BFD_RELOC_SH_GLOB_DAT, R_SH_GLOB_DAT },
1910 { BFD_RELOC_SH_JMP_SLOT, R_SH_JMP_SLOT },
1911 { BFD_RELOC_SH_RELATIVE, R_SH_RELATIVE },
1912 { BFD_RELOC_32_GOTOFF, R_SH_GOTOFF },
1913 { BFD_RELOC_SH_GOTPC, R_SH_GOTPC },
1914 { BFD_RELOC_SH_GOTPLT32, R_SH_GOTPLT32 },
1915 #ifdef INCLUDE_SHMEDIA
1916 { BFD_RELOC_SH_GOT_LOW16, R_SH_GOT_LOW16 },
1917 { BFD_RELOC_SH_GOT_MEDLOW16, R_SH_GOT_MEDLOW16 },
1918 { BFD_RELOC_SH_GOT_MEDHI16, R_SH_GOT_MEDHI16 },
1919 { BFD_RELOC_SH_GOT_HI16, R_SH_GOT_HI16 },
1920 { BFD_RELOC_SH_GOTPLT_LOW16, R_SH_GOTPLT_LOW16 },
1921 { BFD_RELOC_SH_GOTPLT_MEDLOW16, R_SH_GOTPLT_MEDLOW16 },
1922 { BFD_RELOC_SH_GOTPLT_MEDHI16, R_SH_GOTPLT_MEDHI16 },
1923 { BFD_RELOC_SH_GOTPLT_HI16, R_SH_GOTPLT_HI16 },
1924 { BFD_RELOC_SH_PLT_LOW16, R_SH_PLT_LOW16 },
1925 { BFD_RELOC_SH_PLT_MEDLOW16, R_SH_PLT_MEDLOW16 },
1926 { BFD_RELOC_SH_PLT_MEDHI16, R_SH_PLT_MEDHI16 },
1927 { BFD_RELOC_SH_PLT_HI16, R_SH_PLT_HI16 },
1928 { BFD_RELOC_SH_GOTOFF_LOW16, R_SH_GOTOFF_LOW16 },
1929 { BFD_RELOC_SH_GOTOFF_MEDLOW16, R_SH_GOTOFF_MEDLOW16 },
1930 { BFD_RELOC_SH_GOTOFF_MEDHI16, R_SH_GOTOFF_MEDHI16 },
1931 { BFD_RELOC_SH_GOTOFF_HI16, R_SH_GOTOFF_HI16 },
1932 { BFD_RELOC_SH_GOTPC_LOW16, R_SH_GOTPC_LOW16 },
1933 { BFD_RELOC_SH_GOTPC_MEDLOW16, R_SH_GOTPC_MEDLOW16 },
1934 { BFD_RELOC_SH_GOTPC_MEDHI16, R_SH_GOTPC_MEDHI16 },
1935 { BFD_RELOC_SH_GOTPC_HI16, R_SH_GOTPC_HI16 },
1936 { BFD_RELOC_SH_COPY64, R_SH_COPY64 },
1937 { BFD_RELOC_SH_GLOB_DAT64, R_SH_GLOB_DAT64 },
1938 { BFD_RELOC_SH_JMP_SLOT64, R_SH_JMP_SLOT64 },
1939 { BFD_RELOC_SH_RELATIVE64, R_SH_RELATIVE64 },
1940 { BFD_RELOC_SH_GOT10BY4, R_SH_GOT10BY4 },
1941 { BFD_RELOC_SH_GOT10BY8, R_SH_GOT10BY8 },
1942 { BFD_RELOC_SH_GOTPLT10BY4, R_SH_GOTPLT10BY4 },
1943 { BFD_RELOC_SH_GOTPLT10BY8, R_SH_GOTPLT10BY8 },
1944 { BFD_RELOC_SH_PT_16, R_SH_PT_16 },
1945 { BFD_RELOC_SH_SHMEDIA_CODE, R_SH_SHMEDIA_CODE },
1946 { BFD_RELOC_SH_IMMU5, R_SH_DIR5U },
1947 { BFD_RELOC_SH_IMMS6, R_SH_DIR6S },
1948 { BFD_RELOC_SH_IMMU6, R_SH_DIR6U },
1949 { BFD_RELOC_SH_IMMS10, R_SH_DIR10S },
1950 { BFD_RELOC_SH_IMMS10BY2, R_SH_DIR10SW },
1951 { BFD_RELOC_SH_IMMS10BY4, R_SH_DIR10SL },
1952 { BFD_RELOC_SH_IMMS10BY8, R_SH_DIR10SQ },
1953 { BFD_RELOC_SH_IMMS16, R_SH_IMMS16 },
1954 { BFD_RELOC_SH_IMMU16, R_SH_IMMU16 },
1955 { BFD_RELOC_SH_IMM_LOW16, R_SH_IMM_LOW16 },
1956 { BFD_RELOC_SH_IMM_LOW16_PCREL, R_SH_IMM_LOW16_PCREL },
1957 { BFD_RELOC_SH_IMM_MEDLOW16, R_SH_IMM_MEDLOW16 },
1958 { BFD_RELOC_SH_IMM_MEDLOW16_PCREL, R_SH_IMM_MEDLOW16_PCREL },
1959 { BFD_RELOC_SH_IMM_MEDHI16, R_SH_IMM_MEDHI16 },
1960 { BFD_RELOC_SH_IMM_MEDHI16_PCREL, R_SH_IMM_MEDHI16_PCREL },
1961 { BFD_RELOC_SH_IMM_HI16, R_SH_IMM_HI16 },
1962 { BFD_RELOC_SH_IMM_HI16_PCREL, R_SH_IMM_HI16_PCREL },
1963 { BFD_RELOC_64, R_SH_64 },
1964 { BFD_RELOC_64_PCREL, R_SH_64_PCREL },
1965 #endif /* not INCLUDE_SHMEDIA */
1966 };
1967
1968 /* Given a BFD reloc code, return the howto structure for the
1969 corresponding SH ELf reloc. */
1970
1971 static reloc_howto_type *
1972 sh_elf_reloc_type_lookup (abfd, code)
1973 bfd *abfd ATTRIBUTE_UNUSED;
1974 bfd_reloc_code_real_type code;
1975 {
1976 unsigned int i;
1977
1978 for (i = 0; i < sizeof (sh_reloc_map) / sizeof (struct elf_reloc_map); i++)
1979 {
1980 if (sh_reloc_map[i].bfd_reloc_val == code)
1981 return &sh_elf_howto_table[(int) sh_reloc_map[i].elf_reloc_val];
1982 }
1983
1984 return NULL;
1985 }
1986
1987 /* Given an ELF reloc, fill in the howto field of a relent. */
1988
1989 static void
1990 sh_elf_info_to_howto (abfd, cache_ptr, dst)
1991 bfd *abfd ATTRIBUTE_UNUSED;
1992 arelent *cache_ptr;
1993 Elf_Internal_Rela *dst;
1994 {
1995 unsigned int r;
1996
1997 r = ELF32_R_TYPE (dst->r_info);
1998
1999 BFD_ASSERT (r < (unsigned int) R_SH_max);
2000 BFD_ASSERT (r < R_SH_FIRST_INVALID_RELOC || r > R_SH_LAST_INVALID_RELOC);
2001 BFD_ASSERT (r < R_SH_FIRST_INVALID_RELOC_2 || r > R_SH_LAST_INVALID_RELOC_2);
2002 BFD_ASSERT (r < R_SH_FIRST_INVALID_RELOC_3 || r > R_SH_LAST_INVALID_RELOC_3);
2003 BFD_ASSERT (r < R_SH_FIRST_INVALID_RELOC_4 || r > R_SH_LAST_INVALID_RELOC_4);
2004 BFD_ASSERT (r < R_SH_FIRST_INVALID_RELOC_5 || r > R_SH_LAST_INVALID_RELOC_5);
2005
2006 cache_ptr->howto = &sh_elf_howto_table[r];
2007 }
2008 \f
2009 /* This function handles relaxing for SH ELF. See the corresponding
2010 function in coff-sh.c for a description of what this does. FIXME:
2011 There is a lot of duplication here between this code and the COFF
2012 specific code. The format of relocs and symbols is wound deeply
2013 into this code, but it would still be better if the duplication
2014 could be eliminated somehow. Note in particular that although both
2015 functions use symbols like R_SH_CODE, those symbols have different
2016 values; in coff-sh.c they come from include/coff/sh.h, whereas here
2017 they come from enum elf_sh_reloc_type in include/elf/sh.h. */
2018
2019 static bfd_boolean
2020 sh_elf_relax_section (abfd, sec, link_info, again)
2021 bfd *abfd;
2022 asection *sec;
2023 struct bfd_link_info *link_info;
2024 bfd_boolean *again;
2025 {
2026 Elf_Internal_Shdr *symtab_hdr;
2027 Elf_Internal_Rela *internal_relocs;
2028 bfd_boolean have_code;
2029 Elf_Internal_Rela *irel, *irelend;
2030 bfd_byte *contents = NULL;
2031 Elf_Internal_Sym *isymbuf = NULL;
2032
2033 *again = FALSE;
2034
2035 if (link_info->relocateable
2036 || (sec->flags & SEC_RELOC) == 0
2037 || sec->reloc_count == 0)
2038 return TRUE;
2039
2040 #ifdef INCLUDE_SHMEDIA
2041 if (elf_section_data (sec)->this_hdr.sh_flags
2042 & (SHF_SH5_ISA32 | SHF_SH5_ISA32_MIXED))
2043 {
2044 return TRUE;
2045 }
2046 #endif
2047
2048 /* If this is the first time we have been called for this section,
2049 initialize the cooked size. */
2050 if (sec->_cooked_size == 0)
2051 sec->_cooked_size = sec->_raw_size;
2052
2053 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2054
2055 internal_relocs = (_bfd_elf32_link_read_relocs
2056 (abfd, sec, (PTR) NULL, (Elf_Internal_Rela *) NULL,
2057 link_info->keep_memory));
2058 if (internal_relocs == NULL)
2059 goto error_return;
2060
2061 have_code = FALSE;
2062
2063 irelend = internal_relocs + sec->reloc_count;
2064 for (irel = internal_relocs; irel < irelend; irel++)
2065 {
2066 bfd_vma laddr, paddr, symval;
2067 unsigned short insn;
2068 Elf_Internal_Rela *irelfn, *irelscan, *irelcount;
2069 bfd_signed_vma foff;
2070
2071 if (ELF32_R_TYPE (irel->r_info) == (int) R_SH_CODE)
2072 have_code = TRUE;
2073
2074 if (ELF32_R_TYPE (irel->r_info) != (int) R_SH_USES)
2075 continue;
2076
2077 /* Get the section contents. */
2078 if (contents == NULL)
2079 {
2080 if (elf_section_data (sec)->this_hdr.contents != NULL)
2081 contents = elf_section_data (sec)->this_hdr.contents;
2082 else
2083 {
2084 contents = (bfd_byte *) bfd_malloc (sec->_raw_size);
2085 if (contents == NULL)
2086 goto error_return;
2087
2088 if (! bfd_get_section_contents (abfd, sec, contents,
2089 (file_ptr) 0, sec->_raw_size))
2090 goto error_return;
2091 }
2092 }
2093
2094 /* The r_addend field of the R_SH_USES reloc will point us to
2095 the register load. The 4 is because the r_addend field is
2096 computed as though it were a jump offset, which are based
2097 from 4 bytes after the jump instruction. */
2098 laddr = irel->r_offset + 4 + irel->r_addend;
2099 if (laddr >= sec->_raw_size)
2100 {
2101 (*_bfd_error_handler) (_("%s: 0x%lx: warning: bad R_SH_USES offset"),
2102 bfd_archive_filename (abfd),
2103 (unsigned long) irel->r_offset);
2104 continue;
2105 }
2106 insn = bfd_get_16 (abfd, contents + laddr);
2107
2108 /* If the instruction is not mov.l NN,rN, we don't know what to
2109 do. */
2110 if ((insn & 0xf000) != 0xd000)
2111 {
2112 ((*_bfd_error_handler)
2113 (_("%s: 0x%lx: warning: R_SH_USES points to unrecognized insn 0x%x"),
2114 bfd_archive_filename (abfd), (unsigned long) irel->r_offset, insn));
2115 continue;
2116 }
2117
2118 /* Get the address from which the register is being loaded. The
2119 displacement in the mov.l instruction is quadrupled. It is a
2120 displacement from four bytes after the movl instruction, but,
2121 before adding in the PC address, two least significant bits
2122 of the PC are cleared. We assume that the section is aligned
2123 on a four byte boundary. */
2124 paddr = insn & 0xff;
2125 paddr *= 4;
2126 paddr += (laddr + 4) &~ (bfd_vma) 3;
2127 if (paddr >= sec->_raw_size)
2128 {
2129 ((*_bfd_error_handler)
2130 (_("%s: 0x%lx: warning: bad R_SH_USES load offset"),
2131 bfd_archive_filename (abfd), (unsigned long) irel->r_offset));
2132 continue;
2133 }
2134
2135 /* Get the reloc for the address from which the register is
2136 being loaded. This reloc will tell us which function is
2137 actually being called. */
2138 for (irelfn = internal_relocs; irelfn < irelend; irelfn++)
2139 if (irelfn->r_offset == paddr
2140 && ELF32_R_TYPE (irelfn->r_info) == (int) R_SH_DIR32)
2141 break;
2142 if (irelfn >= irelend)
2143 {
2144 ((*_bfd_error_handler)
2145 (_("%s: 0x%lx: warning: could not find expected reloc"),
2146 bfd_archive_filename (abfd), (unsigned long) paddr));
2147 continue;
2148 }
2149
2150 /* Read this BFD's symbols if we haven't done so already. */
2151 if (isymbuf == NULL && symtab_hdr->sh_info != 0)
2152 {
2153 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
2154 if (isymbuf == NULL)
2155 isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
2156 symtab_hdr->sh_info, 0,
2157 NULL, NULL, NULL);
2158 if (isymbuf == NULL)
2159 goto error_return;
2160 }
2161
2162 /* Get the value of the symbol referred to by the reloc. */
2163 if (ELF32_R_SYM (irelfn->r_info) < symtab_hdr->sh_info)
2164 {
2165 /* A local symbol. */
2166 Elf_Internal_Sym *isym;
2167
2168 isym = isymbuf + ELF32_R_SYM (irelfn->r_info);
2169 if (isym->st_shndx
2170 != (unsigned int) _bfd_elf_section_from_bfd_section (abfd, sec))
2171 {
2172 ((*_bfd_error_handler)
2173 (_("%s: 0x%lx: warning: symbol in unexpected section"),
2174 bfd_archive_filename (abfd), (unsigned long) paddr));
2175 continue;
2176 }
2177
2178 symval = (isym->st_value
2179 + sec->output_section->vma
2180 + sec->output_offset);
2181 }
2182 else
2183 {
2184 unsigned long indx;
2185 struct elf_link_hash_entry *h;
2186
2187 indx = ELF32_R_SYM (irelfn->r_info) - symtab_hdr->sh_info;
2188 h = elf_sym_hashes (abfd)[indx];
2189 BFD_ASSERT (h != NULL);
2190 if (h->root.type != bfd_link_hash_defined
2191 && h->root.type != bfd_link_hash_defweak)
2192 {
2193 /* This appears to be a reference to an undefined
2194 symbol. Just ignore it--it will be caught by the
2195 regular reloc processing. */
2196 continue;
2197 }
2198
2199 symval = (h->root.u.def.value
2200 + h->root.u.def.section->output_section->vma
2201 + h->root.u.def.section->output_offset);
2202 }
2203
2204 symval += bfd_get_32 (abfd, contents + paddr);
2205
2206 /* See if this function call can be shortened. */
2207 foff = (symval
2208 - (irel->r_offset
2209 + sec->output_section->vma
2210 + sec->output_offset
2211 + 4));
2212 if (foff < -0x1000 || foff >= 0x1000)
2213 {
2214 /* After all that work, we can't shorten this function call. */
2215 continue;
2216 }
2217
2218 /* Shorten the function call. */
2219
2220 /* For simplicity of coding, we are going to modify the section
2221 contents, the section relocs, and the BFD symbol table. We
2222 must tell the rest of the code not to free up this
2223 information. It would be possible to instead create a table
2224 of changes which have to be made, as is done in coff-mips.c;
2225 that would be more work, but would require less memory when
2226 the linker is run. */
2227
2228 elf_section_data (sec)->relocs = internal_relocs;
2229 elf_section_data (sec)->this_hdr.contents = contents;
2230 symtab_hdr->contents = (unsigned char *) isymbuf;
2231
2232 /* Replace the jsr with a bsr. */
2233
2234 /* Change the R_SH_USES reloc into an R_SH_IND12W reloc, and
2235 replace the jsr with a bsr. */
2236 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irelfn->r_info), R_SH_IND12W);
2237 /* We used to test (ELF32_R_SYM (irelfn->r_info) < symtab_hdr->sh_info)
2238 here, but that only checks if the symbol is an external symbol,
2239 not if the symbol is in a different section. Besides, we need
2240 a consistent meaning for the relocation, so we just assume here that
2241 the value of the symbol is not available. */
2242 #if 0
2243 if (ELF32_R_SYM (irelfn->r_info) < symtab_hdr->sh_info)
2244 {
2245 /* If this needs to be changed because of future relaxing,
2246 it will be handled here like other internal IND12W
2247 relocs. */
2248 bfd_put_16 (abfd,
2249 (bfd_vma) 0xb000 | ((foff >> 1) & 0xfff),
2250 contents + irel->r_offset);
2251 }
2252 else
2253 #endif
2254 {
2255 /* We can't fully resolve this yet, because the external
2256 symbol value may be changed by future relaxing. We let
2257 the final link phase handle it. */
2258 bfd_put_16 (abfd, (bfd_vma) 0xb000, contents + irel->r_offset);
2259 }
2260 irel->r_addend = -4;
2261
2262 /* See if there is another R_SH_USES reloc referring to the same
2263 register load. */
2264 for (irelscan = internal_relocs; irelscan < irelend; irelscan++)
2265 if (ELF32_R_TYPE (irelscan->r_info) == (int) R_SH_USES
2266 && laddr == irelscan->r_offset + 4 + irelscan->r_addend)
2267 break;
2268 if (irelscan < irelend)
2269 {
2270 /* Some other function call depends upon this register load,
2271 and we have not yet converted that function call.
2272 Indeed, we may never be able to convert it. There is
2273 nothing else we can do at this point. */
2274 continue;
2275 }
2276
2277 /* Look for a R_SH_COUNT reloc on the location where the
2278 function address is stored. Do this before deleting any
2279 bytes, to avoid confusion about the address. */
2280 for (irelcount = internal_relocs; irelcount < irelend; irelcount++)
2281 if (irelcount->r_offset == paddr
2282 && ELF32_R_TYPE (irelcount->r_info) == (int) R_SH_COUNT)
2283 break;
2284
2285 /* Delete the register load. */
2286 if (! sh_elf_relax_delete_bytes (abfd, sec, laddr, 2))
2287 goto error_return;
2288
2289 /* That will change things, so, just in case it permits some
2290 other function call to come within range, we should relax
2291 again. Note that this is not required, and it may be slow. */
2292 *again = TRUE;
2293
2294 /* Now check whether we got a COUNT reloc. */
2295 if (irelcount >= irelend)
2296 {
2297 ((*_bfd_error_handler)
2298 (_("%s: 0x%lx: warning: could not find expected COUNT reloc"),
2299 bfd_archive_filename (abfd), (unsigned long) paddr));
2300 continue;
2301 }
2302
2303 /* The number of uses is stored in the r_addend field. We've
2304 just deleted one. */
2305 if (irelcount->r_addend == 0)
2306 {
2307 ((*_bfd_error_handler) (_("%s: 0x%lx: warning: bad count"),
2308 bfd_archive_filename (abfd),
2309 (unsigned long) paddr));
2310 continue;
2311 }
2312
2313 --irelcount->r_addend;
2314
2315 /* If there are no more uses, we can delete the address. Reload
2316 the address from irelfn, in case it was changed by the
2317 previous call to sh_elf_relax_delete_bytes. */
2318 if (irelcount->r_addend == 0)
2319 {
2320 if (! sh_elf_relax_delete_bytes (abfd, sec, irelfn->r_offset, 4))
2321 goto error_return;
2322 }
2323
2324 /* We've done all we can with that function call. */
2325 }
2326
2327 /* Look for load and store instructions that we can align on four
2328 byte boundaries. */
2329 if ((elf_elfheader (abfd)->e_flags & EF_SH_MACH_MASK) != EF_SH4
2330 && have_code)
2331 {
2332 bfd_boolean swapped;
2333
2334 /* Get the section contents. */
2335 if (contents == NULL)
2336 {
2337 if (elf_section_data (sec)->this_hdr.contents != NULL)
2338 contents = elf_section_data (sec)->this_hdr.contents;
2339 else
2340 {
2341 contents = (bfd_byte *) bfd_malloc (sec->_raw_size);
2342 if (contents == NULL)
2343 goto error_return;
2344
2345 if (! bfd_get_section_contents (abfd, sec, contents,
2346 (file_ptr) 0, sec->_raw_size))
2347 goto error_return;
2348 }
2349 }
2350
2351 if (! sh_elf_align_loads (abfd, sec, internal_relocs, contents,
2352 &swapped))
2353 goto error_return;
2354
2355 if (swapped)
2356 {
2357 elf_section_data (sec)->relocs = internal_relocs;
2358 elf_section_data (sec)->this_hdr.contents = contents;
2359 symtab_hdr->contents = (unsigned char *) isymbuf;
2360 }
2361 }
2362
2363 if (isymbuf != NULL
2364 && symtab_hdr->contents != (unsigned char *) isymbuf)
2365 {
2366 if (! link_info->keep_memory)
2367 free (isymbuf);
2368 else
2369 {
2370 /* Cache the symbols for elf_link_input_bfd. */
2371 symtab_hdr->contents = (unsigned char *) isymbuf;
2372 }
2373 }
2374
2375 if (contents != NULL
2376 && elf_section_data (sec)->this_hdr.contents != contents)
2377 {
2378 if (! link_info->keep_memory)
2379 free (contents);
2380 else
2381 {
2382 /* Cache the section contents for elf_link_input_bfd. */
2383 elf_section_data (sec)->this_hdr.contents = contents;
2384 }
2385 }
2386
2387 if (internal_relocs != NULL
2388 && elf_section_data (sec)->relocs != internal_relocs)
2389 free (internal_relocs);
2390
2391 return TRUE;
2392
2393 error_return:
2394 if (isymbuf != NULL
2395 && symtab_hdr->contents != (unsigned char *) isymbuf)
2396 free (isymbuf);
2397 if (contents != NULL
2398 && elf_section_data (sec)->this_hdr.contents != contents)
2399 free (contents);
2400 if (internal_relocs != NULL
2401 && elf_section_data (sec)->relocs != internal_relocs)
2402 free (internal_relocs);
2403
2404 return FALSE;
2405 }
2406
2407 /* Delete some bytes from a section while relaxing. FIXME: There is a
2408 lot of duplication between this function and sh_relax_delete_bytes
2409 in coff-sh.c. */
2410
2411 static bfd_boolean
2412 sh_elf_relax_delete_bytes (abfd, sec, addr, count)
2413 bfd *abfd;
2414 asection *sec;
2415 bfd_vma addr;
2416 int count;
2417 {
2418 Elf_Internal_Shdr *symtab_hdr;
2419 unsigned int sec_shndx;
2420 bfd_byte *contents;
2421 Elf_Internal_Rela *irel, *irelend;
2422 Elf_Internal_Rela *irelalign;
2423 bfd_vma toaddr;
2424 Elf_Internal_Sym *isymbuf, *isym, *isymend;
2425 struct elf_link_hash_entry **sym_hashes;
2426 struct elf_link_hash_entry **end_hashes;
2427 unsigned int symcount;
2428 asection *o;
2429
2430 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2431 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
2432
2433 sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
2434
2435 contents = elf_section_data (sec)->this_hdr.contents;
2436
2437 /* The deletion must stop at the next ALIGN reloc for an aligment
2438 power larger than the number of bytes we are deleting. */
2439
2440 irelalign = NULL;
2441 toaddr = sec->_cooked_size;
2442
2443 irel = elf_section_data (sec)->relocs;
2444 irelend = irel + sec->reloc_count;
2445 for (; irel < irelend; irel++)
2446 {
2447 if (ELF32_R_TYPE (irel->r_info) == (int) R_SH_ALIGN
2448 && irel->r_offset > addr
2449 && count < (1 << irel->r_addend))
2450 {
2451 irelalign = irel;
2452 toaddr = irel->r_offset;
2453 break;
2454 }
2455 }
2456
2457 /* Actually delete the bytes. */
2458 memmove (contents + addr, contents + addr + count,
2459 (size_t) (toaddr - addr - count));
2460 if (irelalign == NULL)
2461 sec->_cooked_size -= count;
2462 else
2463 {
2464 int i;
2465
2466 #define NOP_OPCODE (0x0009)
2467
2468 BFD_ASSERT ((count & 1) == 0);
2469 for (i = 0; i < count; i += 2)
2470 bfd_put_16 (abfd, (bfd_vma) NOP_OPCODE, contents + toaddr - count + i);
2471 }
2472
2473 /* Adjust all the relocs. */
2474 for (irel = elf_section_data (sec)->relocs; irel < irelend; irel++)
2475 {
2476 bfd_vma nraddr, stop;
2477 bfd_vma start = 0;
2478 int insn = 0;
2479 int off, adjust, oinsn;
2480 bfd_signed_vma voff = 0;
2481 bfd_boolean overflow;
2482
2483 /* Get the new reloc address. */
2484 nraddr = irel->r_offset;
2485 if ((irel->r_offset > addr
2486 && irel->r_offset < toaddr)
2487 || (ELF32_R_TYPE (irel->r_info) == (int) R_SH_ALIGN
2488 && irel->r_offset == toaddr))
2489 nraddr -= count;
2490
2491 /* See if this reloc was for the bytes we have deleted, in which
2492 case we no longer care about it. Don't delete relocs which
2493 represent addresses, though. */
2494 if (irel->r_offset >= addr
2495 && irel->r_offset < addr + count
2496 && ELF32_R_TYPE (irel->r_info) != (int) R_SH_ALIGN
2497 && ELF32_R_TYPE (irel->r_info) != (int) R_SH_CODE
2498 && ELF32_R_TYPE (irel->r_info) != (int) R_SH_DATA
2499 && ELF32_R_TYPE (irel->r_info) != (int) R_SH_LABEL)
2500 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
2501 (int) R_SH_NONE);
2502
2503 /* If this is a PC relative reloc, see if the range it covers
2504 includes the bytes we have deleted. */
2505 switch ((enum elf_sh_reloc_type) ELF32_R_TYPE (irel->r_info))
2506 {
2507 default:
2508 break;
2509
2510 case R_SH_DIR8WPN:
2511 case R_SH_IND12W:
2512 case R_SH_DIR8WPZ:
2513 case R_SH_DIR8WPL:
2514 start = irel->r_offset;
2515 insn = bfd_get_16 (abfd, contents + nraddr);
2516 break;
2517 }
2518
2519 switch ((enum elf_sh_reloc_type) ELF32_R_TYPE (irel->r_info))
2520 {
2521 default:
2522 start = stop = addr;
2523 break;
2524
2525 case R_SH_DIR32:
2526 /* If this reloc is against a symbol defined in this
2527 section, and the symbol will not be adjusted below, we
2528 must check the addend to see it will put the value in
2529 range to be adjusted, and hence must be changed. */
2530 if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
2531 {
2532 isym = isymbuf + ELF32_R_SYM (irel->r_info);
2533 if (isym->st_shndx == sec_shndx
2534 && (isym->st_value <= addr
2535 || isym->st_value >= toaddr))
2536 {
2537 bfd_vma val;
2538
2539 val = bfd_get_32 (abfd, contents + nraddr);
2540 val += isym->st_value;
2541 if (val > addr && val < toaddr)
2542 bfd_put_32 (abfd, val - count, contents + nraddr);
2543 }
2544 }
2545 start = stop = addr;
2546 break;
2547
2548 case R_SH_DIR8WPN:
2549 off = insn & 0xff;
2550 if (off & 0x80)
2551 off -= 0x100;
2552 stop = (bfd_vma) ((bfd_signed_vma) start + 4 + off * 2);
2553 break;
2554
2555 case R_SH_IND12W:
2556 off = insn & 0xfff;
2557 if (! off)
2558 {
2559 /* This has been made by previous relaxation. Since the
2560 relocation will be against an external symbol, the
2561 final relocation will just do the right thing. */
2562 start = stop = addr;
2563 }
2564 else
2565 {
2566 if (off & 0x800)
2567 off -= 0x1000;
2568 stop = (bfd_vma) ((bfd_signed_vma) start + 4 + off * 2);
2569
2570 /* The addend will be against the section symbol, thus
2571 for adjusting the addend, the relevant start is the
2572 start of the section.
2573 N.B. If we want to abandom in-place changes here and
2574 test directly using symbol + addend, we have to take into
2575 account that the addend has already been adjusted by -4. */
2576 if (stop > addr && stop < toaddr)
2577 irel->r_addend -= count;
2578 }
2579 break;
2580
2581 case R_SH_DIR8WPZ:
2582 off = insn & 0xff;
2583 stop = start + 4 + off * 2;
2584 break;
2585
2586 case R_SH_DIR8WPL:
2587 off = insn & 0xff;
2588 stop = (start & ~(bfd_vma) 3) + 4 + off * 4;
2589 break;
2590
2591 case R_SH_SWITCH8:
2592 case R_SH_SWITCH16:
2593 case R_SH_SWITCH32:
2594 /* These relocs types represent
2595 .word L2-L1
2596 The r_addend field holds the difference between the reloc
2597 address and L1. That is the start of the reloc, and
2598 adding in the contents gives us the top. We must adjust
2599 both the r_offset field and the section contents.
2600 N.B. in gas / coff bfd, the elf bfd r_addend is called r_offset,
2601 and the elf bfd r_offset is called r_vaddr. */
2602
2603 stop = irel->r_offset;
2604 start = (bfd_vma) ((bfd_signed_vma) stop - (long) irel->r_addend);
2605
2606 if (start > addr
2607 && start < toaddr
2608 && (stop <= addr || stop >= toaddr))
2609 irel->r_addend += count;
2610 else if (stop > addr
2611 && stop < toaddr
2612 && (start <= addr || start >= toaddr))
2613 irel->r_addend -= count;
2614
2615 if (ELF32_R_TYPE (irel->r_info) == (int) R_SH_SWITCH16)
2616 voff = bfd_get_signed_16 (abfd, contents + nraddr);
2617 else if (ELF32_R_TYPE (irel->r_info) == (int) R_SH_SWITCH8)
2618 voff = bfd_get_8 (abfd, contents + nraddr);
2619 else
2620 voff = bfd_get_signed_32 (abfd, contents + nraddr);
2621 stop = (bfd_vma) ((bfd_signed_vma) start + voff);
2622
2623 break;
2624
2625 case R_SH_USES:
2626 start = irel->r_offset;
2627 stop = (bfd_vma) ((bfd_signed_vma) start
2628 + (long) irel->r_addend
2629 + 4);
2630 break;
2631 }
2632
2633 if (start > addr
2634 && start < toaddr
2635 && (stop <= addr || stop >= toaddr))
2636 adjust = count;
2637 else if (stop > addr
2638 && stop < toaddr
2639 && (start <= addr || start >= toaddr))
2640 adjust = - count;
2641 else
2642 adjust = 0;
2643
2644 if (adjust != 0)
2645 {
2646 oinsn = insn;
2647 overflow = FALSE;
2648 switch ((enum elf_sh_reloc_type) ELF32_R_TYPE (irel->r_info))
2649 {
2650 default:
2651 abort ();
2652 break;
2653
2654 case R_SH_DIR8WPN:
2655 case R_SH_DIR8WPZ:
2656 insn += adjust / 2;
2657 if ((oinsn & 0xff00) != (insn & 0xff00))
2658 overflow = TRUE;
2659 bfd_put_16 (abfd, (bfd_vma) insn, contents + nraddr);
2660 break;
2661
2662 case R_SH_IND12W:
2663 insn += adjust / 2;
2664 if ((oinsn & 0xf000) != (insn & 0xf000))
2665 overflow = TRUE;
2666 bfd_put_16 (abfd, (bfd_vma) insn, contents + nraddr);
2667 break;
2668
2669 case R_SH_DIR8WPL:
2670 BFD_ASSERT (adjust == count || count >= 4);
2671 if (count >= 4)
2672 insn += adjust / 4;
2673 else
2674 {
2675 if ((irel->r_offset & 3) == 0)
2676 ++insn;
2677 }
2678 if ((oinsn & 0xff00) != (insn & 0xff00))
2679 overflow = TRUE;
2680 bfd_put_16 (abfd, (bfd_vma) insn, contents + nraddr);
2681 break;
2682
2683 case R_SH_SWITCH8:
2684 voff += adjust;
2685 if (voff < 0 || voff >= 0xff)
2686 overflow = TRUE;
2687 bfd_put_8 (abfd, voff, contents + nraddr);
2688 break;
2689
2690 case R_SH_SWITCH16:
2691 voff += adjust;
2692 if (voff < - 0x8000 || voff >= 0x8000)
2693 overflow = TRUE;
2694 bfd_put_signed_16 (abfd, (bfd_vma) voff, contents + nraddr);
2695 break;
2696
2697 case R_SH_SWITCH32:
2698 voff += adjust;
2699 bfd_put_signed_32 (abfd, (bfd_vma) voff, contents + nraddr);
2700 break;
2701
2702 case R_SH_USES:
2703 irel->r_addend += adjust;
2704 break;
2705 }
2706
2707 if (overflow)
2708 {
2709 ((*_bfd_error_handler)
2710 (_("%s: 0x%lx: fatal: reloc overflow while relaxing"),
2711 bfd_archive_filename (abfd), (unsigned long) irel->r_offset));
2712 bfd_set_error (bfd_error_bad_value);
2713 return FALSE;
2714 }
2715 }
2716
2717 irel->r_offset = nraddr;
2718 }
2719
2720 /* Look through all the other sections. If there contain any IMM32
2721 relocs against internal symbols which we are not going to adjust
2722 below, we may need to adjust the addends. */
2723 for (o = abfd->sections; o != NULL; o = o->next)
2724 {
2725 Elf_Internal_Rela *internal_relocs;
2726 Elf_Internal_Rela *irelscan, *irelscanend;
2727 bfd_byte *ocontents;
2728
2729 if (o == sec
2730 || (o->flags & SEC_RELOC) == 0
2731 || o->reloc_count == 0)
2732 continue;
2733
2734 /* We always cache the relocs. Perhaps, if info->keep_memory is
2735 FALSE, we should free them, if we are permitted to, when we
2736 leave sh_coff_relax_section. */
2737 internal_relocs = (_bfd_elf32_link_read_relocs
2738 (abfd, o, (PTR) NULL, (Elf_Internal_Rela *) NULL,
2739 TRUE));
2740 if (internal_relocs == NULL)
2741 return FALSE;
2742
2743 ocontents = NULL;
2744 irelscanend = internal_relocs + o->reloc_count;
2745 for (irelscan = internal_relocs; irelscan < irelscanend; irelscan++)
2746 {
2747 /* Dwarf line numbers use R_SH_SWITCH32 relocs. */
2748 if (ELF32_R_TYPE (irelscan->r_info) == (int) R_SH_SWITCH32)
2749 {
2750 bfd_vma start, stop;
2751 bfd_signed_vma voff;
2752
2753 if (ocontents == NULL)
2754 {
2755 if (elf_section_data (o)->this_hdr.contents != NULL)
2756 ocontents = elf_section_data (o)->this_hdr.contents;
2757 else
2758 {
2759 /* We always cache the section contents.
2760 Perhaps, if info->keep_memory is FALSE, we
2761 should free them, if we are permitted to,
2762 when we leave sh_coff_relax_section. */
2763 ocontents = (bfd_byte *) bfd_malloc (o->_raw_size);
2764 if (ocontents == NULL)
2765 return FALSE;
2766 if (! bfd_get_section_contents (abfd, o, ocontents,
2767 (file_ptr) 0,
2768 o->_raw_size))
2769 return FALSE;
2770 elf_section_data (o)->this_hdr.contents = ocontents;
2771 }
2772 }
2773
2774 stop = irelscan->r_offset;
2775 start
2776 = (bfd_vma) ((bfd_signed_vma) stop - (long) irelscan->r_addend);
2777
2778 /* STOP is in a different section, so it won't change. */
2779 if (start > addr && start < toaddr)
2780 irelscan->r_addend += count;
2781
2782 voff = bfd_get_signed_32 (abfd, ocontents + irelscan->r_offset);
2783 stop = (bfd_vma) ((bfd_signed_vma) start + voff);
2784
2785 if (start > addr
2786 && start < toaddr
2787 && (stop <= addr || stop >= toaddr))
2788 bfd_put_signed_32 (abfd, (bfd_vma) voff + count,
2789 ocontents + irelscan->r_offset);
2790 else if (stop > addr
2791 && stop < toaddr
2792 && (start <= addr || start >= toaddr))
2793 bfd_put_signed_32 (abfd, (bfd_vma) voff - count,
2794 ocontents + irelscan->r_offset);
2795 }
2796
2797 if (ELF32_R_TYPE (irelscan->r_info) != (int) R_SH_DIR32)
2798 continue;
2799
2800 if (ELF32_R_SYM (irelscan->r_info) >= symtab_hdr->sh_info)
2801 continue;
2802
2803
2804 isym = isymbuf + ELF32_R_SYM (irelscan->r_info);
2805 if (isym->st_shndx == sec_shndx
2806 && (isym->st_value <= addr
2807 || isym->st_value >= toaddr))
2808 {
2809 bfd_vma val;
2810
2811 if (ocontents == NULL)
2812 {
2813 if (elf_section_data (o)->this_hdr.contents != NULL)
2814 ocontents = elf_section_data (o)->this_hdr.contents;
2815 else
2816 {
2817 /* We always cache the section contents.
2818 Perhaps, if info->keep_memory is FALSE, we
2819 should free them, if we are permitted to,
2820 when we leave sh_coff_relax_section. */
2821 ocontents = (bfd_byte *) bfd_malloc (o->_raw_size);
2822 if (ocontents == NULL)
2823 return FALSE;
2824 if (! bfd_get_section_contents (abfd, o, ocontents,
2825 (file_ptr) 0,
2826 o->_raw_size))
2827 return FALSE;
2828 elf_section_data (o)->this_hdr.contents = ocontents;
2829 }
2830 }
2831
2832 val = bfd_get_32 (abfd, ocontents + irelscan->r_offset);
2833 val += isym->st_value;
2834 if (val > addr && val < toaddr)
2835 bfd_put_32 (abfd, val - count,
2836 ocontents + irelscan->r_offset);
2837 }
2838 }
2839 }
2840
2841 /* Adjust the local symbols defined in this section. */
2842 isymend = isymbuf + symtab_hdr->sh_info;
2843 for (isym = isymbuf; isym < isymend; isym++)
2844 {
2845 if (isym->st_shndx == sec_shndx
2846 && isym->st_value > addr
2847 && isym->st_value < toaddr)
2848 isym->st_value -= count;
2849 }
2850
2851 /* Now adjust the global symbols defined in this section. */
2852 symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
2853 - symtab_hdr->sh_info);
2854 sym_hashes = elf_sym_hashes (abfd);
2855 end_hashes = sym_hashes + symcount;
2856 for (; sym_hashes < end_hashes; sym_hashes++)
2857 {
2858 struct elf_link_hash_entry *sym_hash = *sym_hashes;
2859 if ((sym_hash->root.type == bfd_link_hash_defined
2860 || sym_hash->root.type == bfd_link_hash_defweak)
2861 && sym_hash->root.u.def.section == sec
2862 && sym_hash->root.u.def.value > addr
2863 && sym_hash->root.u.def.value < toaddr)
2864 {
2865 sym_hash->root.u.def.value -= count;
2866 }
2867 }
2868
2869 /* See if we can move the ALIGN reloc forward. We have adjusted
2870 r_offset for it already. */
2871 if (irelalign != NULL)
2872 {
2873 bfd_vma alignto, alignaddr;
2874
2875 alignto = BFD_ALIGN (toaddr, 1 << irelalign->r_addend);
2876 alignaddr = BFD_ALIGN (irelalign->r_offset,
2877 1 << irelalign->r_addend);
2878 if (alignto != alignaddr)
2879 {
2880 /* Tail recursion. */
2881 return sh_elf_relax_delete_bytes (abfd, sec, alignaddr,
2882 (int) (alignto - alignaddr));
2883 }
2884 }
2885
2886 return TRUE;
2887 }
2888
2889 /* Look for loads and stores which we can align to four byte
2890 boundaries. This is like sh_align_loads in coff-sh.c. */
2891
2892 static bfd_boolean
2893 sh_elf_align_loads (abfd, sec, internal_relocs, contents, pswapped)
2894 bfd *abfd ATTRIBUTE_UNUSED;
2895 asection *sec;
2896 Elf_Internal_Rela *internal_relocs;
2897 bfd_byte *contents ATTRIBUTE_UNUSED;
2898 bfd_boolean *pswapped;
2899 {
2900 Elf_Internal_Rela *irel, *irelend;
2901 bfd_vma *labels = NULL;
2902 bfd_vma *label, *label_end;
2903 bfd_size_type amt;
2904
2905 *pswapped = FALSE;
2906
2907 irelend = internal_relocs + sec->reloc_count;
2908
2909 /* Get all the addresses with labels on them. */
2910 amt = sec->reloc_count;
2911 amt *= sizeof (bfd_vma);
2912 labels = (bfd_vma *) bfd_malloc (amt);
2913 if (labels == NULL)
2914 goto error_return;
2915 label_end = labels;
2916 for (irel = internal_relocs; irel < irelend; irel++)
2917 {
2918 if (ELF32_R_TYPE (irel->r_info) == (int) R_SH_LABEL)
2919 {
2920 *label_end = irel->r_offset;
2921 ++label_end;
2922 }
2923 }
2924
2925 /* Note that the assembler currently always outputs relocs in
2926 address order. If that ever changes, this code will need to sort
2927 the label values and the relocs. */
2928
2929 label = labels;
2930
2931 for (irel = internal_relocs; irel < irelend; irel++)
2932 {
2933 bfd_vma start, stop;
2934
2935 if (ELF32_R_TYPE (irel->r_info) != (int) R_SH_CODE)
2936 continue;
2937
2938 start = irel->r_offset;
2939
2940 for (irel++; irel < irelend; irel++)
2941 if (ELF32_R_TYPE (irel->r_info) == (int) R_SH_DATA)
2942 break;
2943 if (irel < irelend)
2944 stop = irel->r_offset;
2945 else
2946 stop = sec->_cooked_size;
2947
2948 if (! _bfd_sh_align_load_span (abfd, sec, contents, sh_elf_swap_insns,
2949 (PTR) internal_relocs, &label,
2950 label_end, start, stop, pswapped))
2951 goto error_return;
2952 }
2953
2954 free (labels);
2955
2956 return TRUE;
2957
2958 error_return:
2959 if (labels != NULL)
2960 free (labels);
2961 return FALSE;
2962 }
2963
2964 /* Swap two SH instructions. This is like sh_swap_insns in coff-sh.c. */
2965
2966 static bfd_boolean
2967 sh_elf_swap_insns (abfd, sec, relocs, contents, addr)
2968 bfd *abfd;
2969 asection *sec;
2970 PTR relocs;
2971 bfd_byte *contents;
2972 bfd_vma addr;
2973 {
2974 Elf_Internal_Rela *internal_relocs = (Elf_Internal_Rela *) relocs;
2975 unsigned short i1, i2;
2976 Elf_Internal_Rela *irel, *irelend;
2977
2978 /* Swap the instructions themselves. */
2979 i1 = bfd_get_16 (abfd, contents + addr);
2980 i2 = bfd_get_16 (abfd, contents + addr + 2);
2981 bfd_put_16 (abfd, (bfd_vma) i2, contents + addr);
2982 bfd_put_16 (abfd, (bfd_vma) i1, contents + addr + 2);
2983
2984 /* Adjust all reloc addresses. */
2985 irelend = internal_relocs + sec->reloc_count;
2986 for (irel = internal_relocs; irel < irelend; irel++)
2987 {
2988 enum elf_sh_reloc_type type;
2989 int add;
2990
2991 /* There are a few special types of relocs that we don't want to
2992 adjust. These relocs do not apply to the instruction itself,
2993 but are only associated with the address. */
2994 type = (enum elf_sh_reloc_type) ELF32_R_TYPE (irel->r_info);
2995 if (type == R_SH_ALIGN
2996 || type == R_SH_CODE
2997 || type == R_SH_DATA
2998 || type == R_SH_LABEL)
2999 continue;
3000
3001 /* If an R_SH_USES reloc points to one of the addresses being
3002 swapped, we must adjust it. It would be incorrect to do this
3003 for a jump, though, since we want to execute both
3004 instructions after the jump. (We have avoided swapping
3005 around a label, so the jump will not wind up executing an
3006 instruction it shouldn't). */
3007 if (type == R_SH_USES)
3008 {
3009 bfd_vma off;
3010
3011 off = irel->r_offset + 4 + irel->r_addend;
3012 if (off == addr)
3013 irel->r_offset += 2;
3014 else if (off == addr + 2)
3015 irel->r_offset -= 2;
3016 }
3017
3018 if (irel->r_offset == addr)
3019 {
3020 irel->r_offset += 2;
3021 add = -2;
3022 }
3023 else if (irel->r_offset == addr + 2)
3024 {
3025 irel->r_offset -= 2;
3026 add = 2;
3027 }
3028 else
3029 add = 0;
3030
3031 if (add != 0)
3032 {
3033 bfd_byte *loc;
3034 unsigned short insn, oinsn;
3035 bfd_boolean overflow;
3036
3037 loc = contents + irel->r_offset;
3038 overflow = FALSE;
3039 switch (type)
3040 {
3041 default:
3042 break;
3043
3044 case R_SH_DIR8WPN:
3045 case R_SH_DIR8WPZ:
3046 insn = bfd_get_16 (abfd, loc);
3047 oinsn = insn;
3048 insn += add / 2;
3049 if ((oinsn & 0xff00) != (insn & 0xff00))
3050 overflow = TRUE;
3051 bfd_put_16 (abfd, (bfd_vma) insn, loc);
3052 break;
3053
3054 case R_SH_IND12W:
3055 insn = bfd_get_16 (abfd, loc);
3056 oinsn = insn;
3057 insn += add / 2;
3058 if ((oinsn & 0xf000) != (insn & 0xf000))
3059 overflow = TRUE;
3060 bfd_put_16 (abfd, (bfd_vma) insn, loc);
3061 break;
3062
3063 case R_SH_DIR8WPL:
3064 /* This reloc ignores the least significant 3 bits of
3065 the program counter before adding in the offset.
3066 This means that if ADDR is at an even address, the
3067 swap will not affect the offset. If ADDR is an at an
3068 odd address, then the instruction will be crossing a
3069 four byte boundary, and must be adjusted. */
3070 if ((addr & 3) != 0)
3071 {
3072 insn = bfd_get_16 (abfd, loc);
3073 oinsn = insn;
3074 insn += add / 2;
3075 if ((oinsn & 0xff00) != (insn & 0xff00))
3076 overflow = TRUE;
3077 bfd_put_16 (abfd, (bfd_vma) insn, loc);
3078 }
3079
3080 break;
3081 }
3082
3083 if (overflow)
3084 {
3085 ((*_bfd_error_handler)
3086 (_("%s: 0x%lx: fatal: reloc overflow while relaxing"),
3087 bfd_archive_filename (abfd), (unsigned long) irel->r_offset));
3088 bfd_set_error (bfd_error_bad_value);
3089 return FALSE;
3090 }
3091 }
3092 }
3093
3094 return TRUE;
3095 }
3096 \f
3097 #ifdef INCLUDE_SHMEDIA
3098
3099 /* The size in bytes of an entry in the procedure linkage table. */
3100
3101 #define PLT_ENTRY_SIZE 64
3102
3103 /* First entry in an absolute procedure linkage table look like this. */
3104
3105 static const bfd_byte elf_sh_plt0_entry_be[PLT_ENTRY_SIZE] =
3106 {
3107 0xcc, 0x00, 0x01, 0x10, /* movi .got.plt >> 16, r17 */
3108 0xc8, 0x00, 0x01, 0x10, /* shori .got.plt & 65535, r17 */
3109 0x89, 0x10, 0x09, 0x90, /* ld.l r17, 8, r25 */
3110 0x6b, 0xf1, 0x66, 0x00, /* ptabs r25, tr0 */
3111 0x89, 0x10, 0x05, 0x10, /* ld.l r17, 4, r17 */
3112 0x44, 0x01, 0xff, 0xf0, /* blink tr0, r63 */
3113 0x6f, 0xf0, 0xff, 0xf0, /* nop */
3114 0x6f, 0xf0, 0xff, 0xf0, /* nop */
3115 0x6f, 0xf0, 0xff, 0xf0, /* nop */
3116 0x6f, 0xf0, 0xff, 0xf0, /* nop */
3117 0x6f, 0xf0, 0xff, 0xf0, /* nop */
3118 0x6f, 0xf0, 0xff, 0xf0, /* nop */
3119 0x6f, 0xf0, 0xff, 0xf0, /* nop */
3120 0x6f, 0xf0, 0xff, 0xf0, /* nop */
3121 0x6f, 0xf0, 0xff, 0xf0, /* nop */
3122 0x6f, 0xf0, 0xff, 0xf0, /* nop */
3123 };
3124
3125 static const bfd_byte elf_sh_plt0_entry_le[PLT_ENTRY_SIZE] =
3126 {
3127 0x10, 0x01, 0x00, 0xcc, /* movi .got.plt >> 16, r17 */
3128 0x10, 0x01, 0x00, 0xc8, /* shori .got.plt & 65535, r17 */
3129 0x90, 0x09, 0x10, 0x89, /* ld.l r17, 8, r25 */
3130 0x00, 0x66, 0xf1, 0x6b, /* ptabs r25, tr0 */
3131 0x10, 0x05, 0x10, 0x89, /* ld.l r17, 4, r17 */
3132 0xf0, 0xff, 0x01, 0x44, /* blink tr0, r63 */
3133 0xf0, 0xff, 0xf0, 0x6f, /* nop */
3134 0xf0, 0xff, 0xf0, 0x6f, /* nop */
3135 0xf0, 0xff, 0xf0, 0x6f, /* nop */
3136 0xf0, 0xff, 0xf0, 0x6f, /* nop */
3137 0xf0, 0xff, 0xf0, 0x6f, /* nop */
3138 0xf0, 0xff, 0xf0, 0x6f, /* nop */
3139 0xf0, 0xff, 0xf0, 0x6f, /* nop */
3140 0xf0, 0xff, 0xf0, 0x6f, /* nop */
3141 0xf0, 0xff, 0xf0, 0x6f, /* nop */
3142 0xf0, 0xff, 0xf0, 0x6f, /* nop */
3143 };
3144
3145 /* Sebsequent entries in an absolute procedure linkage table look like
3146 this. */
3147
3148 static const bfd_byte elf_sh_plt_entry_be[PLT_ENTRY_SIZE] =
3149 {
3150 0xcc, 0x00, 0x01, 0x90, /* movi nameN-in-GOT >> 16, r25 */
3151 0xc8, 0x00, 0x01, 0x90, /* shori nameN-in-GOT & 65535, r25 */
3152 0x89, 0x90, 0x01, 0x90, /* ld.l r25, 0, r25 */
3153 0x6b, 0xf1, 0x66, 0x00, /* ptabs r25, tr0 */
3154 0x44, 0x01, 0xff, 0xf0, /* blink tr0, r63 */
3155 0x6f, 0xf0, 0xff, 0xf0, /* nop */
3156 0x6f, 0xf0, 0xff, 0xf0, /* nop */
3157 0x6f, 0xf0, 0xff, 0xf0, /* nop */
3158 0xcc, 0x00, 0x01, 0x90, /* movi .PLT0 >> 16, r25 */
3159 0xc8, 0x00, 0x01, 0x90, /* shori .PLT0 & 65535, r25 */
3160 0x6b, 0xf1, 0x66, 0x00, /* ptabs r25, tr0 */
3161 0xcc, 0x00, 0x01, 0x50, /* movi reloc-offset >> 16, r21 */
3162 0xc8, 0x00, 0x01, 0x50, /* shori reloc-offset & 65535, r21 */
3163 0x44, 0x01, 0xff, 0xf0, /* blink tr0, r63 */
3164 0x6f, 0xf0, 0xff, 0xf0, /* nop */
3165 0x6f, 0xf0, 0xff, 0xf0, /* nop */
3166 };
3167
3168 static const bfd_byte elf_sh_plt_entry_le[PLT_ENTRY_SIZE] =
3169 {
3170 0x90, 0x01, 0x00, 0xcc, /* movi nameN-in-GOT >> 16, r25 */
3171 0x90, 0x01, 0x00, 0xc8, /* shori nameN-in-GOT & 65535, r25 */
3172 0x90, 0x01, 0x90, 0x89, /* ld.l r25, 0, r25 */
3173 0x00, 0x66, 0xf1, 0x6b, /* ptabs r25, tr0 */
3174 0xf0, 0xff, 0x01, 0x44, /* blink tr0, r63 */
3175 0xf0, 0xff, 0xf0, 0x6f, /* nop */
3176 0xf0, 0xff, 0xf0, 0x6f, /* nop */
3177 0xf0, 0xff, 0xf0, 0x6f, /* nop */
3178 0x90, 0x01, 0x00, 0xcc, /* movi .PLT0 >> 16, r25 */
3179 0x90, 0x01, 0x00, 0xc8, /* shori .PLT0 & 65535, r25 */
3180 0x00, 0x66, 0xf1, 0x6b, /* ptabs r25, tr0 */
3181 0x50, 0x01, 0x00, 0xcc, /* movi reloc-offset >> 16, r21 */
3182 0x50, 0x01, 0x00, 0xc8, /* shori reloc-offset & 65535, r21 */
3183 0xf0, 0xff, 0x01, 0x44, /* blink tr0, r63 */
3184 0xf0, 0xff, 0xf0, 0x6f, /* nop */
3185 0xf0, 0xff, 0xf0, 0x6f, /* nop */
3186 };
3187
3188 /* Entries in a PIC procedure linkage table look like this. */
3189
3190 static const bfd_byte elf_sh_pic_plt_entry_be[PLT_ENTRY_SIZE] =
3191 {
3192 0xcc, 0x00, 0x01, 0x90, /* movi nameN@GOT >> 16, r25 */
3193 0xc8, 0x00, 0x01, 0x90, /* shori nameN@GOT & 65535, r25 */
3194 0x40, 0xc2, 0x65, 0x90, /* ldx.l r12, r25, r25 */
3195 0x6b, 0xf1, 0x66, 0x00, /* ptabs r25, tr0 */
3196 0x44, 0x01, 0xff, 0xf0, /* blink tr0, r63 */
3197 0x6f, 0xf0, 0xff, 0xf0, /* nop */
3198 0x6f, 0xf0, 0xff, 0xf0, /* nop */
3199 0x6f, 0xf0, 0xff, 0xf0, /* nop */
3200 0xce, 0x00, 0x01, 0x10, /* movi -GOT_BIAS, r17 */
3201 0x00, 0xc8, 0x45, 0x10, /* add.l r12, r17, r17 */
3202 0x89, 0x10, 0x09, 0x90, /* ld.l r17, 8, r25 */
3203 0x6b, 0xf1, 0x66, 0x00, /* ptabs r25, tr0 */
3204 0x89, 0x10, 0x05, 0x10, /* ld.l r17, 4, r17 */
3205 0xcc, 0x00, 0x01, 0x50, /* movi reloc-offset >> 16, r21 */
3206 0xc8, 0x00, 0x01, 0x50, /* shori reloc-offset & 65535, r21 */
3207 0x44, 0x01, 0xff, 0xf0, /* blink tr0, r63 */
3208 };
3209
3210 static const bfd_byte elf_sh_pic_plt_entry_le[PLT_ENTRY_SIZE] =
3211 {
3212 0x90, 0x01, 0x00, 0xcc, /* movi nameN@GOT >> 16, r25 */
3213 0x90, 0x01, 0x00, 0xc8, /* shori nameN@GOT & 65535, r25 */
3214 0x90, 0x65, 0xc2, 0x40, /* ldx.l r12, r25, r25 */
3215 0x00, 0x66, 0xf1, 0x6b, /* ptabs r25, tr0 */
3216 0xf0, 0xff, 0x01, 0x44, /* blink tr0, r63 */
3217 0xf0, 0xff, 0xf0, 0x6f, /* nop */
3218 0xf0, 0xff, 0xf0, 0x6f, /* nop */
3219 0xf0, 0xff, 0xf0, 0x6f, /* nop */
3220 0x10, 0x01, 0x00, 0xce, /* movi -GOT_BIAS, r17 */
3221 0x10, 0x45, 0xc8, 0x00, /* add.l r12, r17, r17 */
3222 0x90, 0x09, 0x10, 0x89, /* ld.l r17, 8, r25 */
3223 0x00, 0x66, 0xf1, 0x6b, /* ptabs r25, tr0 */
3224 0x10, 0x05, 0x10, 0x89, /* ld.l r17, 4, r17 */
3225 0x50, 0x01, 0x00, 0xcc, /* movi reloc-offset >> 16, r21 */
3226 0x50, 0x01, 0x00, 0xc8, /* shori reloc-offset & 65535, r21 */
3227 0xf0, 0xff, 0x01, 0x44, /* blink tr0, r63 */
3228 };
3229
3230 static const bfd_byte *elf_sh_plt0_entry;
3231 static const bfd_byte *elf_sh_plt_entry;
3232 static const bfd_byte *elf_sh_pic_plt_entry;
3233
3234 /* Return size of a PLT entry. */
3235 #define elf_sh_sizeof_plt(info) PLT_ENTRY_SIZE
3236
3237 /* Return offset of the PLT0 address in an absolute PLT entry. */
3238 #define elf_sh_plt_plt0_offset(info) 32
3239
3240 /* Return offset of the linker in PLT0 entry. */
3241 #define elf_sh_plt0_gotplt_offset(info) 0
3242
3243 /* Return offset of the trampoline in PLT entry */
3244 #define elf_sh_plt_temp_offset(info) 33 /* Add one because it's SHmedia. */
3245
3246 /* Return offset of the symbol in PLT entry. */
3247 #define elf_sh_plt_symbol_offset(info) 0
3248
3249 /* Return offset of the relocation in PLT entry. */
3250 #define elf_sh_plt_reloc_offset(info) (info->shared ? 52 : 44)
3251
3252 inline static void
3253 movi_shori_putval (output_bfd, value, addr)
3254 bfd *output_bfd;
3255 unsigned long value;
3256 char *addr;
3257 {
3258 bfd_put_32 (output_bfd,
3259 bfd_get_32 (output_bfd, addr)
3260 | ((value >> 6) & 0x3fffc00),
3261 addr);
3262 bfd_put_32 (output_bfd,
3263 bfd_get_32 (output_bfd, addr + 4)
3264 | ((value << 10) & 0x3fffc00),
3265 addr + 4);
3266 }
3267
3268 #else
3269 /* The size in bytes of an entry in the procedure linkage table. */
3270
3271 #define PLT_ENTRY_SIZE 28
3272
3273 /* First entry in an absolute procedure linkage table look like this. */
3274
3275 #if 1
3276 /* Note - this code has been "optimised" not to use r2. r2 is used by
3277 GCC to return the address of large strutcures, so it should not be
3278 corrupted here. This does mean however, that this PLT does not conform
3279 to the SH PIC ABI. That spec says that r0 contains the type of the PLT
3280 and r2 contains the GOT id. This version stores the GOT id in r0 and
3281 ignores the type. Loaders can easily detect this difference however,
3282 since the type will always be 0 or 8, and the GOT ids will always be
3283 greater than or equal to 12. */
3284 static const bfd_byte elf_sh_plt0_entry_be[PLT_ENTRY_SIZE] =
3285 {
3286 0xd0, 0x05, /* mov.l 2f,r0 */
3287 0x60, 0x02, /* mov.l @r0,r0 */
3288 0x2f, 0x06, /* mov.l r0,@-r15 */
3289 0xd0, 0x03, /* mov.l 1f,r0 */
3290 0x60, 0x02, /* mov.l @r0,r0 */
3291 0x40, 0x2b, /* jmp @r0 */
3292 0x60, 0xf6, /* mov.l @r15+,r0 */
3293 0x00, 0x09, /* nop */
3294 0x00, 0x09, /* nop */
3295 0x00, 0x09, /* nop */
3296 0, 0, 0, 0, /* 1: replaced with address of .got.plt + 8. */
3297 0, 0, 0, 0, /* 2: replaced with address of .got.plt + 4. */
3298 };
3299
3300 static const bfd_byte elf_sh_plt0_entry_le[PLT_ENTRY_SIZE] =
3301 {
3302 0x05, 0xd0, /* mov.l 2f,r0 */
3303 0x02, 0x60, /* mov.l @r0,r0 */
3304 0x06, 0x2f, /* mov.l r0,@-r15 */
3305 0x03, 0xd0, /* mov.l 1f,r0 */
3306 0x02, 0x60, /* mov.l @r0,r0 */
3307 0x2b, 0x40, /* jmp @r0 */
3308 0xf6, 0x60, /* mov.l @r15+,r0 */
3309 0x09, 0x00, /* nop */
3310 0x09, 0x00, /* nop */
3311 0x09, 0x00, /* nop */
3312 0, 0, 0, 0, /* 1: replaced with address of .got.plt + 8. */
3313 0, 0, 0, 0, /* 2: replaced with address of .got.plt + 4. */
3314 };
3315
3316 /* Sebsequent entries in an absolute procedure linkage table look like
3317 this. */
3318
3319 static const bfd_byte elf_sh_plt_entry_be[PLT_ENTRY_SIZE] =
3320 {
3321 0xd0, 0x04, /* mov.l 1f,r0 */
3322 0x60, 0x02, /* mov.l @r0,r0 */
3323 0xd1, 0x02, /* mov.l 0f,r1 */
3324 0x40, 0x2b, /* jmp @r0 */
3325 0x60, 0x13, /* mov r1,r0 */
3326 0xd1, 0x03, /* mov.l 2f,r1 */
3327 0x40, 0x2b, /* jmp @r0 */
3328 0x00, 0x09, /* nop */
3329 0, 0, 0, 0, /* 0: replaced with address of .PLT0. */
3330 0, 0, 0, 0, /* 1: replaced with address of this symbol in .got. */
3331 0, 0, 0, 0, /* 2: replaced with offset into relocation table. */
3332 };
3333
3334 static const bfd_byte elf_sh_plt_entry_le[PLT_ENTRY_SIZE] =
3335 {
3336 0x04, 0xd0, /* mov.l 1f,r0 */
3337 0x02, 0x60, /* mov.l @r0,r0 */
3338 0x02, 0xd1, /* mov.l 0f,r1 */
3339 0x2b, 0x40, /* jmp @r0 */
3340 0x13, 0x60, /* mov r1,r0 */
3341 0x03, 0xd1, /* mov.l 2f,r1 */
3342 0x2b, 0x40, /* jmp @r0 */
3343 0x09, 0x00, /* nop */
3344 0, 0, 0, 0, /* 0: replaced with address of .PLT0. */
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. */
3347 };
3348
3349 /* Entries in a PIC procedure linkage table look like this. */
3350
3351 static const bfd_byte elf_sh_pic_plt_entry_be[PLT_ENTRY_SIZE] =
3352 {
3353 0xd0, 0x04, /* mov.l 1f,r0 */
3354 0x00, 0xce, /* mov.l @(r0,r12),r0 */
3355 0x40, 0x2b, /* jmp @r0 */
3356 0x00, 0x09, /* nop */
3357 0x50, 0xc2, /* mov.l @(8,r12),r0 */
3358 0xd1, 0x03, /* mov.l 2f,r1 */
3359 0x40, 0x2b, /* jmp @r0 */
3360 0x50, 0xc1, /* mov.l @(4,r12),r0 */
3361 0x00, 0x09, /* nop */
3362 0x00, 0x09, /* nop */
3363 0, 0, 0, 0, /* 1: replaced with address of this symbol in .got. */
3364 0, 0, 0, 0 /* 2: replaced with offset into relocation table. */
3365 };
3366
3367 static const bfd_byte elf_sh_pic_plt_entry_le[PLT_ENTRY_SIZE] =
3368 {
3369 0x04, 0xd0, /* mov.l 1f,r0 */
3370 0xce, 0x00, /* mov.l @(r0,r12),r0 */
3371 0x2b, 0x40, /* jmp @r0 */
3372 0x09, 0x00, /* nop */
3373 0xc2, 0x50, /* mov.l @(8,r12),r0 */
3374 0x03, 0xd1, /* mov.l 2f,r1 */
3375 0x2b, 0x40, /* jmp @r0 */
3376 0xc1, 0x50, /* mov.l @(4,r12),r0 */
3377 0x09, 0x00, /* nop */
3378 0x09, 0x00, /* nop */
3379 0, 0, 0, 0, /* 1: replaced with address of this symbol in .got. */
3380 0, 0, 0, 0 /* 2: replaced with offset into relocation table. */
3381 };
3382
3383 #else /* These are the old style PLT entries. */
3384 static const bfd_byte elf_sh_plt0_entry_be[PLT_ENTRY_SIZE] =
3385 {
3386 0xd0, 0x04, /* mov.l 1f,r0 */
3387 0xd2, 0x05, /* mov.l 2f,r2 */
3388 0x60, 0x02, /* mov.l @r0,r0 */
3389 0x62, 0x22, /* mov.l @r2,r2 */
3390 0x40, 0x2b, /* jmp @r0 */
3391 0xe0, 0x00, /* mov #0,r0 */
3392 0x00, 0x09, /* nop */
3393 0x00, 0x09, /* nop */
3394 0x00, 0x09, /* nop */
3395 0x00, 0x09, /* nop */
3396 0, 0, 0, 0, /* 1: replaced with address of .got.plt + 8. */
3397 0, 0, 0, 0, /* 2: replaced with address of .got.plt + 4. */
3398 };
3399
3400 static const bfd_byte elf_sh_plt0_entry_le[PLT_ENTRY_SIZE] =
3401 {
3402 0x04, 0xd0, /* mov.l 1f,r0 */
3403 0x05, 0xd2, /* mov.l 2f,r2 */
3404 0x02, 0x60, /* mov.l @r0,r0 */
3405 0x22, 0x62, /* mov.l @r2,r2 */
3406 0x2b, 0x40, /* jmp @r0 */
3407 0x00, 0xe0, /* mov #0,r0 */
3408 0x09, 0x00, /* nop */
3409 0x09, 0x00, /* nop */
3410 0x09, 0x00, /* nop */
3411 0x09, 0x00, /* nop */
3412 0, 0, 0, 0, /* 1: replaced with address of .got.plt + 8. */
3413 0, 0, 0, 0, /* 2: replaced with address of .got.plt + 4. */
3414 };
3415
3416 /* Sebsequent entries in an absolute procedure linkage table look like
3417 this. */
3418
3419 static const bfd_byte elf_sh_plt_entry_be[PLT_ENTRY_SIZE] =
3420 {
3421 0xd0, 0x04, /* mov.l 1f,r0 */
3422 0x60, 0x02, /* mov.l @r0,r0 */
3423 0xd2, 0x02, /* mov.l 0f,r2 */
3424 0x40, 0x2b, /* jmp @r0 */
3425 0x60, 0x23, /* mov r2,r0 */
3426 0xd1, 0x03, /* mov.l 2f,r1 */
3427 0x40, 0x2b, /* jmp @r0 */
3428 0x00, 0x09, /* nop */
3429 0, 0, 0, 0, /* 0: replaced with address of .PLT0. */
3430 0, 0, 0, 0, /* 1: replaced with address of this symbol in .got. */
3431 0, 0, 0, 0, /* 2: replaced with offset into relocation table. */
3432 };
3433
3434 static const bfd_byte elf_sh_plt_entry_le[PLT_ENTRY_SIZE] =
3435 {
3436 0x04, 0xd0, /* mov.l 1f,r0 */
3437 0x02, 0x60, /* mov.l @r0,r0 */
3438 0x02, 0xd2, /* mov.l 0f,r2 */
3439 0x2b, 0x40, /* jmp @r0 */
3440 0x23, 0x60, /* mov r2,r0 */
3441 0x03, 0xd1, /* mov.l 2f,r1 */
3442 0x2b, 0x40, /* jmp @r0 */
3443 0x09, 0x00, /* nop */
3444 0, 0, 0, 0, /* 0: replaced with address of .PLT. */
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. */
3447 };
3448
3449 /* Entries in a PIC procedure linkage table look like this. */
3450
3451 static const bfd_byte elf_sh_pic_plt_entry_be[PLT_ENTRY_SIZE] =
3452 {
3453 0xd0, 0x04, /* mov.l 1f,r0 */
3454 0x00, 0xce, /* mov.l @(r0,r12),r0 */
3455 0x40, 0x2b, /* jmp @r0 */
3456 0x00, 0x09, /* nop */
3457 0x50, 0xc2, /* 0: mov.l @(8,r12),r0 */
3458 0x52, 0xc1, /* 1: mov.l @(4,r12),r2 */
3459 0xd1, 0x02, /* mov.l 2f,r1 */
3460 0x40, 0x2b, /* jmp @r0 */
3461 0xe0, 0x00, /* mov #0,r0 ! shows the type of PLT. */
3462 0x00, 0x09, /* nop */
3463 0, 0, 0, 0, /* 1: replaced with address of this symbol in .got. */
3464 0, 0, 0, 0 /* 2: replaced with offset into relocation table. */
3465 };
3466
3467 static const bfd_byte elf_sh_pic_plt_entry_le[PLT_ENTRY_SIZE] =
3468 {
3469 0x04, 0xd0, /* mov.l 1f,r0 */
3470 0xce, 0x00, /* mov.l @(r0,r12),r0 */
3471 0x2b, 0x40, /* jmp @r0 */
3472 0x09, 0x00, /* nop */
3473 0xc2, 0x50, /* 0: mov.l @(8,r12),r0 */
3474 0xc1, 0x52, /* 1: mov.l @(4,r12),r2 */
3475 0x02, 0xd1, /* mov.l 2f,r1 */
3476 0x2b, 0x40, /* jmp @r0 */
3477 0x00, 0xe0, /* mov #0,r0 ! shows the type of PLT. */
3478 0x09, 0x00, /* nop */
3479 0, 0, 0, 0, /* 1: replaced with address of this symbol in .got. */
3480 0, 0, 0, 0 /* 2: replaced with offset into relocation table. */
3481 };
3482 #endif /* old style PLT entries. */
3483
3484 static const bfd_byte *elf_sh_plt0_entry;
3485 static const bfd_byte *elf_sh_plt_entry;
3486 static const bfd_byte *elf_sh_pic_plt_entry;
3487
3488 /* Return size of a PLT entry. */
3489 #define elf_sh_sizeof_plt(info) PLT_ENTRY_SIZE
3490
3491 /* Return offset of the PLT0 address in an absolute PLT entry. */
3492 #define elf_sh_plt_plt0_offset(info) 16
3493
3494 /* Return offset of the linker in PLT0 entry. */
3495 #define elf_sh_plt0_linker_offset(info) 20
3496
3497 /* Return offset of the GOT id in PLT0 entry. */
3498 #define elf_sh_plt0_gotid_offset(info) 24
3499
3500 /* Return offset of the tempoline in PLT entry */
3501 #define elf_sh_plt_temp_offset(info) 8
3502
3503 /* Return offset of the symbol in PLT entry. */
3504 #define elf_sh_plt_symbol_offset(info) 20
3505
3506 /* Return offset of the relocation in PLT entry. */
3507 #define elf_sh_plt_reloc_offset(info) 24
3508 #endif
3509
3510 /* The sh linker needs to keep track of the number of relocs that it
3511 decides to copy as dynamic relocs in check_relocs for each symbol.
3512 This is so that it can later discard them if they are found to be
3513 unnecessary. We store the information in a field extending the
3514 regular ELF linker hash table. */
3515
3516 struct elf_sh_dyn_relocs
3517 {
3518 struct elf_sh_dyn_relocs *next;
3519
3520 /* The input section of the reloc. */
3521 asection *sec;
3522
3523 /* Total number of relocs copied for the input section. */
3524 bfd_size_type count;
3525
3526 /* Number of pc-relative relocs copied for the input section. */
3527 bfd_size_type pc_count;
3528
3529 /* If TRUE, R_SH_TLS_TPOFF32 relocation is generated. */
3530 bfd_boolean tls_tpoff32;
3531 };
3532
3533 /* sh ELF linker hash entry. */
3534
3535 struct elf_sh_link_hash_entry
3536 {
3537 struct elf_link_hash_entry root;
3538
3539 #ifdef INCLUDE_SHMEDIA
3540 union
3541 {
3542 bfd_signed_vma refcount;
3543 bfd_vma offset;
3544 } datalabel_got;
3545 #endif
3546
3547 /* Track dynamic relocs copied for this symbol. */
3548 struct elf_sh_dyn_relocs *dyn_relocs;
3549
3550 bfd_signed_vma gotplt_refcount;
3551
3552 enum {
3553 GOT_UNKNOWN = 0, GOT_NORMAL, GOT_TLS_GD, GOT_TLS_IE
3554 } tls_type;
3555 };
3556
3557 #define sh_elf_hash_entry(ent) ((struct elf_sh_link_hash_entry *)(ent))
3558
3559 struct sh_elf_obj_tdata
3560 {
3561 struct elf_obj_tdata root;
3562
3563 /* tls_type for each local got entry. */
3564 char *local_got_tls_type;
3565 };
3566
3567 #define sh_elf_tdata(abfd) \
3568 ((struct sh_elf_obj_tdata *) (abfd)->tdata.any)
3569
3570 #define sh_elf_local_got_tls_type(abfd) \
3571 (sh_elf_tdata (abfd)->local_got_tls_type)
3572
3573 /* Override the generic function because we need to store sh_elf_obj_tdata
3574 as the specific tdata. */
3575
3576 static bfd_boolean
3577 sh_elf_mkobject (abfd)
3578 bfd *abfd;
3579 {
3580 bfd_size_type amt = sizeof (struct sh_elf_obj_tdata);
3581 abfd->tdata.any = bfd_zalloc (abfd, amt);
3582 if (abfd->tdata.any == NULL)
3583 return FALSE;
3584 return TRUE;
3585 }
3586
3587 /* sh ELF linker hash table. */
3588
3589 struct elf_sh_link_hash_table
3590 {
3591 struct elf_link_hash_table root;
3592
3593 /* Short-cuts to get to dynamic linker sections. */
3594 asection *sgot;
3595 asection *sgotplt;
3596 asection *srelgot;
3597 asection *splt;
3598 asection *srelplt;
3599 asection *sdynbss;
3600 asection *srelbss;
3601
3602 /* Small local sym to section mapping cache. */
3603 struct sym_sec_cache sym_sec;
3604
3605 /* A counter or offset to track a TLS got entry. */
3606 union
3607 {
3608 bfd_signed_vma refcount;
3609 bfd_vma offset;
3610 } tls_ldm_got;
3611 };
3612
3613 /* Traverse an sh ELF linker hash table. */
3614
3615 #define sh_elf_link_hash_traverse(table, func, info) \
3616 (elf_link_hash_traverse \
3617 (&(table)->root, \
3618 (bfd_boolean (*) PARAMS ((struct elf_link_hash_entry *, PTR))) (func), \
3619 (info)))
3620
3621 /* Get the sh ELF linker hash table from a link_info structure. */
3622
3623 #define sh_elf_hash_table(p) \
3624 ((struct elf_sh_link_hash_table *) ((p)->hash))
3625
3626 /* Create an entry in an sh ELF linker hash table. */
3627
3628 static struct bfd_hash_entry *
3629 sh_elf_link_hash_newfunc (entry, table, string)
3630 struct bfd_hash_entry *entry;
3631 struct bfd_hash_table *table;
3632 const char *string;
3633 {
3634 struct elf_sh_link_hash_entry *ret =
3635 (struct elf_sh_link_hash_entry *) entry;
3636
3637 /* Allocate the structure if it has not already been allocated by a
3638 subclass. */
3639 if (ret == (struct elf_sh_link_hash_entry *) NULL)
3640 ret = ((struct elf_sh_link_hash_entry *)
3641 bfd_hash_allocate (table,
3642 sizeof (struct elf_sh_link_hash_entry)));
3643 if (ret == (struct elf_sh_link_hash_entry *) NULL)
3644 return (struct bfd_hash_entry *) ret;
3645
3646 /* Call the allocation method of the superclass. */
3647 ret = ((struct elf_sh_link_hash_entry *)
3648 _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
3649 table, string));
3650 if (ret != (struct elf_sh_link_hash_entry *) NULL)
3651 {
3652 ret->dyn_relocs = NULL;
3653 ret->gotplt_refcount = 0;
3654 #ifdef INCLUDE_SHMEDIA
3655 ret->datalabel_got.refcount = ret->root.got.refcount;
3656 #endif
3657 ret->tls_type = GOT_UNKNOWN;
3658 }
3659
3660 return (struct bfd_hash_entry *) ret;
3661 }
3662
3663 /* Create an sh ELF linker hash table. */
3664
3665 static struct bfd_link_hash_table *
3666 sh_elf_link_hash_table_create (abfd)
3667 bfd *abfd;
3668 {
3669 struct elf_sh_link_hash_table *ret;
3670 bfd_size_type amt = sizeof (struct elf_sh_link_hash_table);
3671
3672 ret = (struct elf_sh_link_hash_table *) bfd_malloc (amt);
3673 if (ret == (struct elf_sh_link_hash_table *) NULL)
3674 return NULL;
3675
3676 if (! _bfd_elf_link_hash_table_init (&ret->root, abfd,
3677 sh_elf_link_hash_newfunc))
3678 {
3679 free (ret);
3680 return NULL;
3681 }
3682
3683 ret->sgot = NULL;
3684 ret->sgotplt = NULL;
3685 ret->srelgot = NULL;
3686 ret->splt = NULL;
3687 ret->srelplt = NULL;
3688 ret->sdynbss = NULL;
3689 ret->srelbss = NULL;
3690 ret->sym_sec.abfd = NULL;
3691 ret->tls_ldm_got.refcount = 0;
3692
3693 return &ret->root.root;
3694 }
3695
3696 /* Create .got, .gotplt, and .rela.got sections in DYNOBJ, and set up
3697 shortcuts to them in our hash table. */
3698
3699 static bfd_boolean
3700 create_got_section (dynobj, info)
3701 bfd *dynobj;
3702 struct bfd_link_info *info;
3703 {
3704 struct elf_sh_link_hash_table *htab;
3705
3706 if (! _bfd_elf_create_got_section (dynobj, info))
3707 return FALSE;
3708
3709 htab = sh_elf_hash_table (info);
3710 htab->sgot = bfd_get_section_by_name (dynobj, ".got");
3711 htab->sgotplt = bfd_get_section_by_name (dynobj, ".got.plt");
3712 if (! htab->sgot || ! htab->sgotplt)
3713 abort ();
3714
3715 htab->srelgot = bfd_make_section (dynobj, ".rela.got");
3716 if (htab->srelgot == NULL
3717 || ! bfd_set_section_flags (dynobj, htab->srelgot,
3718 (SEC_ALLOC
3719 | SEC_LOAD
3720 | SEC_HAS_CONTENTS
3721 | SEC_IN_MEMORY
3722 | SEC_LINKER_CREATED
3723 | SEC_READONLY))
3724 || ! bfd_set_section_alignment (dynobj, htab->srelgot, 2))
3725 return FALSE;
3726 return TRUE;
3727 }
3728
3729 /* Create dynamic sections when linking against a dynamic object. */
3730
3731 static bfd_boolean
3732 sh_elf_create_dynamic_sections (abfd, info)
3733 bfd *abfd;
3734 struct bfd_link_info *info;
3735 {
3736 struct elf_sh_link_hash_table *htab;
3737 flagword flags, pltflags;
3738 register asection *s;
3739 struct elf_backend_data *bed = get_elf_backend_data (abfd);
3740 int ptralign = 0;
3741
3742 switch (bed->s->arch_size)
3743 {
3744 case 32:
3745 ptralign = 2;
3746 break;
3747
3748 case 64:
3749 ptralign = 3;
3750 break;
3751
3752 default:
3753 bfd_set_error (bfd_error_bad_value);
3754 return FALSE;
3755 }
3756
3757 htab = sh_elf_hash_table (info);
3758
3759 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
3760 .rel[a].bss sections. */
3761
3762 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
3763 | SEC_LINKER_CREATED);
3764
3765 pltflags = flags;
3766 pltflags |= SEC_CODE;
3767 if (bed->plt_not_loaded)
3768 pltflags &= ~ (SEC_LOAD | SEC_HAS_CONTENTS);
3769 if (bed->plt_readonly)
3770 pltflags |= SEC_READONLY;
3771
3772 s = bfd_make_section (abfd, ".plt");
3773 htab->splt = s;
3774 if (s == NULL
3775 || ! bfd_set_section_flags (abfd, s, pltflags)
3776 || ! bfd_set_section_alignment (abfd, s, bed->plt_alignment))
3777 return FALSE;
3778
3779 if (bed->want_plt_sym)
3780 {
3781 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
3782 .plt section. */
3783 struct elf_link_hash_entry *h;
3784 struct bfd_link_hash_entry *bh = NULL;
3785
3786 if (! (_bfd_generic_link_add_one_symbol
3787 (info, abfd, "_PROCEDURE_LINKAGE_TABLE_", BSF_GLOBAL, s,
3788 (bfd_vma) 0, (const char *) NULL, FALSE,
3789 get_elf_backend_data (abfd)->collect, &bh)))
3790 return FALSE;
3791
3792 h = (struct elf_link_hash_entry *) bh;
3793 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
3794 h->type = STT_OBJECT;
3795
3796 if (info->shared
3797 && ! _bfd_elf_link_record_dynamic_symbol (info, h))
3798 return FALSE;
3799 }
3800
3801 s = bfd_make_section (abfd,
3802 bed->default_use_rela_p ? ".rela.plt" : ".rel.plt");
3803 htab->srelplt = s;
3804 if (s == NULL
3805 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
3806 || ! bfd_set_section_alignment (abfd, s, ptralign))
3807 return FALSE;
3808
3809 if (! create_got_section (abfd, info))
3810 return FALSE;
3811
3812 {
3813 const char *secname;
3814 char *relname;
3815 flagword secflags;
3816 asection *sec;
3817
3818 for (sec = abfd->sections; sec; sec = sec->next)
3819 {
3820 secflags = bfd_get_section_flags (abfd, sec);
3821 if ((secflags & (SEC_DATA | SEC_LINKER_CREATED))
3822 || ((secflags & SEC_HAS_CONTENTS) != SEC_HAS_CONTENTS))
3823 continue;
3824 secname = bfd_get_section_name (abfd, sec);
3825 relname = (char *) bfd_malloc ((bfd_size_type) strlen (secname) + 6);
3826 strcpy (relname, ".rela");
3827 strcat (relname, secname);
3828 s = bfd_make_section (abfd, relname);
3829 if (s == NULL
3830 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
3831 || ! bfd_set_section_alignment (abfd, s, ptralign))
3832 return FALSE;
3833 }
3834 }
3835
3836 if (bed->want_dynbss)
3837 {
3838 /* The .dynbss section is a place to put symbols which are defined
3839 by dynamic objects, are referenced by regular objects, and are
3840 not functions. We must allocate space for them in the process
3841 image and use a R_*_COPY reloc to tell the dynamic linker to
3842 initialize them at run time. The linker script puts the .dynbss
3843 section into the .bss section of the final image. */
3844 s = bfd_make_section (abfd, ".dynbss");
3845 htab->sdynbss = s;
3846 if (s == NULL
3847 || ! bfd_set_section_flags (abfd, s, SEC_ALLOC))
3848 return FALSE;
3849
3850 /* The .rel[a].bss section holds copy relocs. This section is not
3851 normally needed. We need to create it here, though, so that the
3852 linker will map it to an output section. We can't just create it
3853 only if we need it, because we will not know whether we need it
3854 until we have seen all the input files, and the first time the
3855 main linker code calls BFD after examining all the input files
3856 (size_dynamic_sections) the input sections have already been
3857 mapped to the output sections. If the section turns out not to
3858 be needed, we can discard it later. We will never need this
3859 section when generating a shared object, since they do not use
3860 copy relocs. */
3861 if (! info->shared)
3862 {
3863 s = bfd_make_section (abfd,
3864 (bed->default_use_rela_p
3865 ? ".rela.bss" : ".rel.bss"));
3866 htab->srelbss = s;
3867 if (s == NULL
3868 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
3869 || ! bfd_set_section_alignment (abfd, s, ptralign))
3870 return FALSE;
3871 }
3872 }
3873
3874 return TRUE;
3875 }
3876 \f
3877 /* Adjust a symbol defined by a dynamic object and referenced by a
3878 regular object. The current definition is in some section of the
3879 dynamic object, but we're not including those sections. We have to
3880 change the definition to something the rest of the link can
3881 understand. */
3882
3883 static bfd_boolean
3884 sh_elf_adjust_dynamic_symbol (info, h)
3885 struct bfd_link_info *info;
3886 struct elf_link_hash_entry *h;
3887 {
3888 struct elf_sh_link_hash_table *htab;
3889 struct elf_sh_link_hash_entry *eh;
3890 struct elf_sh_dyn_relocs *p;
3891 bfd *dynobj;
3892 asection *s;
3893 unsigned int power_of_two;
3894
3895 dynobj = elf_hash_table (info)->dynobj;
3896
3897 /* Make sure we know what is going on here. */
3898 BFD_ASSERT (dynobj != NULL
3899 && ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT)
3900 || h->weakdef != NULL
3901 || ((h->elf_link_hash_flags
3902 & ELF_LINK_HASH_DEF_DYNAMIC) != 0
3903 && (h->elf_link_hash_flags
3904 & ELF_LINK_HASH_REF_REGULAR) != 0
3905 && (h->elf_link_hash_flags
3906 & ELF_LINK_HASH_DEF_REGULAR) == 0)));
3907
3908 /* If this is a function, put it in the procedure linkage table. We
3909 will fill in the contents of the procedure linkage table later,
3910 when we know the address of the .got section. */
3911 if (h->type == STT_FUNC
3912 || (h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0)
3913 {
3914 if (h->plt.refcount <= 0
3915 || (! info->shared
3916 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) == 0
3917 && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) == 0
3918 && h->root.type != bfd_link_hash_undefweak
3919 && h->root.type != bfd_link_hash_undefined))
3920 {
3921 /* This case can occur if we saw a PLT reloc in an input
3922 file, but the symbol was never referred to by a dynamic
3923 object. In such a case, we don't actually need to build
3924 a procedure linkage table, and we can just do a REL32
3925 reloc instead. */
3926 h->plt.offset = (bfd_vma) -1;
3927 h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
3928 }
3929
3930 return TRUE;
3931 }
3932 else
3933 h->plt.offset = (bfd_vma) -1;
3934
3935 /* If this is a weak symbol, and there is a real definition, the
3936 processor independent code will have arranged for us to see the
3937 real definition first, and we can just use the same value. */
3938 if (h->weakdef != NULL)
3939 {
3940 BFD_ASSERT (h->weakdef->root.type == bfd_link_hash_defined
3941 || h->weakdef->root.type == bfd_link_hash_defweak);
3942 h->root.u.def.section = h->weakdef->root.u.def.section;
3943 h->root.u.def.value = h->weakdef->root.u.def.value;
3944 return TRUE;
3945 }
3946
3947 /* This is a reference to a symbol defined by a dynamic object which
3948 is not a function. */
3949
3950 /* If we are creating a shared library, we must presume that the
3951 only references to the symbol are via the global offset table.
3952 For such cases we need not do anything here; the relocations will
3953 be handled correctly by relocate_section. */
3954 if (info->shared)
3955 return TRUE;
3956
3957 /* If there are no references to this symbol that do not use the
3958 GOT, we don't need to generate a copy reloc. */
3959 if ((h->elf_link_hash_flags & ELF_LINK_NON_GOT_REF) == 0)
3960 return TRUE;
3961
3962 /* If -z nocopyreloc was given, we won't generate them either. */
3963 if (info->nocopyreloc)
3964 {
3965 h->elf_link_hash_flags &= ~ELF_LINK_NON_GOT_REF;
3966 return TRUE;
3967 }
3968
3969 eh = (struct elf_sh_link_hash_entry *) h;
3970 for (p = eh->dyn_relocs; p != NULL; p = p->next)
3971 {
3972 s = p->sec->output_section;
3973 if (s != NULL && (s->flags & (SEC_READONLY | SEC_HAS_CONTENTS)) != 0)
3974 break;
3975 }
3976
3977 /* If we didn't find any dynamic relocs in sections which needs the
3978 copy reloc, then we'll be keeping the dynamic relocs and avoiding
3979 the copy reloc. */
3980 if (p == NULL)
3981 {
3982 h->elf_link_hash_flags &= ~ELF_LINK_NON_GOT_REF;
3983 return TRUE;
3984 }
3985
3986 /* We must allocate the symbol in our .dynbss section, which will
3987 become part of the .bss section of the executable. There will be
3988 an entry for this symbol in the .dynsym section. The dynamic
3989 object will contain position independent code, so all references
3990 from the dynamic object to this symbol will go through the global
3991 offset table. The dynamic linker will use the .dynsym entry to
3992 determine the address it must put in the global offset table, so
3993 both the dynamic object and the regular object will refer to the
3994 same memory location for the variable. */
3995
3996 htab = sh_elf_hash_table (info);
3997 s = htab->sdynbss;
3998 BFD_ASSERT (s != NULL);
3999
4000 /* We must generate a R_SH_COPY reloc to tell the dynamic linker to
4001 copy the initial value out of the dynamic object and into the
4002 runtime process image. We need to remember the offset into the
4003 .rela.bss section we are going to use. */
4004 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
4005 {
4006 asection *srel;
4007
4008 srel = htab->srelbss;
4009 BFD_ASSERT (srel != NULL);
4010 srel->_raw_size += sizeof (Elf32_External_Rela);
4011 h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_COPY;
4012 }
4013
4014 /* We need to figure out the alignment required for this symbol. I
4015 have no idea how ELF linkers handle this. */
4016 power_of_two = bfd_log2 (h->size);
4017 if (power_of_two > 3)
4018 power_of_two = 3;
4019
4020 /* Apply the required alignment. */
4021 s->_raw_size = BFD_ALIGN (s->_raw_size, (bfd_size_type) (1 << power_of_two));
4022 if (power_of_two > bfd_get_section_alignment (dynobj, s))
4023 {
4024 if (! bfd_set_section_alignment (dynobj, s, power_of_two))
4025 return FALSE;
4026 }
4027
4028 /* Define the symbol as being at this point in the section. */
4029 h->root.u.def.section = s;
4030 h->root.u.def.value = s->_raw_size;
4031
4032 /* Increment the section size to make room for the symbol. */
4033 s->_raw_size += h->size;
4034
4035 return TRUE;
4036 }
4037
4038 /* This is the condition under which sh_elf_finish_dynamic_symbol
4039 will be called from elflink.h. If elflink.h doesn't call our
4040 finish_dynamic_symbol routine, we'll need to do something about
4041 initializing any .plt and .got entries in sh_elf_relocate_section. */
4042 #define WILL_CALL_FINISH_DYNAMIC_SYMBOL(DYN, INFO, H) \
4043 ((DYN) \
4044 && ((INFO)->shared \
4045 || ((H)->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0) \
4046 && ((H)->dynindx != -1 \
4047 || ((H)->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0))
4048
4049 /* Allocate space in .plt, .got and associated reloc sections for
4050 dynamic relocs. */
4051
4052 static bfd_boolean
4053 allocate_dynrelocs (h, inf)
4054 struct elf_link_hash_entry *h;
4055 PTR inf;
4056 {
4057 struct bfd_link_info *info;
4058 struct elf_sh_link_hash_table *htab;
4059 struct elf_sh_link_hash_entry *eh;
4060 struct elf_sh_dyn_relocs *p;
4061
4062 if (h->root.type == bfd_link_hash_indirect)
4063 return TRUE;
4064
4065 if (h->root.type == bfd_link_hash_warning)
4066 /* When warning symbols are created, they **replace** the "real"
4067 entry in the hash table, thus we never get to see the real
4068 symbol in a hash traversal. So look at it now. */
4069 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4070
4071 info = (struct bfd_link_info *) inf;
4072 htab = sh_elf_hash_table (info);
4073
4074 eh = (struct elf_sh_link_hash_entry *) h;
4075 if ((h->got.refcount > 0
4076 || (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL))
4077 && eh->gotplt_refcount > 0)
4078 {
4079 /* The symbol has been forced local, or we have some direct got refs,
4080 so treat all the gotplt refs as got refs. */
4081 h->got.refcount += eh->gotplt_refcount;
4082 if (h->plt.refcount >= eh->gotplt_refcount)
4083 h->plt.refcount -= eh->gotplt_refcount;
4084 }
4085
4086 if (htab->root.dynamic_sections_created
4087 && h->plt.refcount > 0)
4088 {
4089 /* Make sure this symbol is output as a dynamic symbol.
4090 Undefined weak syms won't yet be marked as dynamic. */
4091 if (h->dynindx == -1
4092 && (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
4093 {
4094 if (! bfd_elf32_link_record_dynamic_symbol (info, h))
4095 return FALSE;
4096 }
4097
4098 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, info, h))
4099 {
4100 asection *s = htab->splt;
4101
4102 /* If this is the first .plt entry, make room for the special
4103 first entry. */
4104 if (s->_raw_size == 0)
4105 s->_raw_size += PLT_ENTRY_SIZE;
4106
4107 h->plt.offset = s->_raw_size;
4108
4109 /* If this symbol is not defined in a regular file, and we are
4110 not generating a shared library, then set the symbol to this
4111 location in the .plt. This is required to make function
4112 pointers compare as equal between the normal executable and
4113 the shared library. */
4114 if (! info->shared
4115 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
4116 {
4117 h->root.u.def.section = s;
4118 h->root.u.def.value = h->plt.offset;
4119 }
4120
4121 /* Make room for this entry. */
4122 s->_raw_size += PLT_ENTRY_SIZE;
4123
4124 /* We also need to make an entry in the .got.plt section, which
4125 will be placed in the .got section by the linker script. */
4126 htab->sgotplt->_raw_size += 4;
4127
4128 /* We also need to make an entry in the .rel.plt section. */
4129 htab->srelplt->_raw_size += sizeof (Elf32_External_Rela);
4130 }
4131 else
4132 {
4133 h->plt.offset = (bfd_vma) -1;
4134 h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
4135 }
4136 }
4137 else
4138 {
4139 h->plt.offset = (bfd_vma) -1;
4140 h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
4141 }
4142
4143 if (h->got.refcount > 0)
4144 {
4145 asection *s;
4146 bfd_boolean dyn;
4147 int tls_type = sh_elf_hash_entry (h)->tls_type;
4148
4149 /* Make sure this symbol is output as a dynamic symbol.
4150 Undefined weak syms won't yet be marked as dynamic. */
4151 if (h->dynindx == -1
4152 && (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
4153 {
4154 if (! bfd_elf32_link_record_dynamic_symbol (info, h))
4155 return FALSE;
4156 }
4157
4158 s = htab->sgot;
4159 h->got.offset = s->_raw_size;
4160 s->_raw_size += 4;
4161 /* R_SH_TLS_GD needs 2 consecutive GOT slots. */
4162 if (tls_type == GOT_TLS_GD)
4163 s->_raw_size += 4;
4164 dyn = htab->root.dynamic_sections_created;
4165 /* R_SH_TLS_IE_32 needs one dynamic relocation,
4166 R_SH_TLS_GD needs one if local symbol and two if global. */
4167 if ((tls_type == GOT_TLS_GD && h->dynindx == -1)
4168 || tls_type == GOT_TLS_IE)
4169 htab->srelgot->_raw_size += sizeof (Elf32_External_Rela);
4170 else if (tls_type == GOT_TLS_GD)
4171 htab->srelgot->_raw_size += 2 * sizeof (Elf32_External_Rela);
4172 else if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info, h))
4173 htab->srelgot->_raw_size += sizeof (Elf32_External_Rela);
4174 }
4175 else
4176 h->got.offset = (bfd_vma) -1;
4177
4178 #ifdef INCLUDE_SHMEDIA
4179 if (eh->datalabel_got.refcount > 0)
4180 {
4181 asection *s;
4182 bfd_boolean dyn;
4183
4184 /* Make sure this symbol is output as a dynamic symbol.
4185 Undefined weak syms won't yet be marked as dynamic. */
4186 if (h->dynindx == -1
4187 && (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
4188 {
4189 if (! bfd_elf32_link_record_dynamic_symbol (info, h))
4190 return FALSE;
4191 }
4192
4193 s = htab->sgot;
4194 eh->datalabel_got.offset = s->_raw_size;
4195 s->_raw_size += 4;
4196 dyn = htab->root.dynamic_sections_created;
4197 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info, h))
4198 htab->srelgot->_raw_size += sizeof (Elf32_External_Rela);
4199 }
4200 else
4201 eh->datalabel_got.offset = (bfd_vma) -1;
4202 #endif
4203
4204 if (eh->dyn_relocs == NULL)
4205 return TRUE;
4206
4207 /* In the shared -Bsymbolic case, discard space allocated for
4208 dynamic pc-relative relocs against symbols which turn out to be
4209 defined in regular objects. For the normal shared case, discard
4210 space for pc-relative relocs that have become local due to symbol
4211 visibility changes. */
4212
4213 if (info->shared)
4214 {
4215 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0
4216 && ((h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0
4217 || info->symbolic))
4218 {
4219 struct elf_sh_dyn_relocs **pp;
4220
4221 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
4222 {
4223 p->count -= p->pc_count;
4224 p->pc_count = 0;
4225 if (p->count == 0)
4226 *pp = p->next;
4227 else
4228 pp = &p->next;
4229 }
4230 }
4231 }
4232 else
4233 {
4234 for (p = eh->dyn_relocs; p; p = p->next)
4235 if (p->tls_tpoff32)
4236 goto keep;
4237
4238 /* For the non-shared case, discard space for relocs against
4239 symbols which turn out to need copy relocs or are not
4240 dynamic. */
4241
4242 if ((h->elf_link_hash_flags & ELF_LINK_NON_GOT_REF) == 0
4243 && (((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
4244 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
4245 || (htab->root.dynamic_sections_created
4246 && (h->root.type == bfd_link_hash_undefweak
4247 || h->root.type == bfd_link_hash_undefined))))
4248 {
4249 /* Make sure this symbol is output as a dynamic symbol.
4250 Undefined weak syms won't yet be marked as dynamic. */
4251 if (h->dynindx == -1
4252 && (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
4253 {
4254 if (! bfd_elf32_link_record_dynamic_symbol (info, h))
4255 return FALSE;
4256 }
4257
4258 /* If that succeeded, we know we'll be keeping all the
4259 relocs. */
4260 if (h->dynindx != -1)
4261 goto keep;
4262 }
4263
4264 eh->dyn_relocs = NULL;
4265
4266 keep: ;
4267 }
4268
4269 /* Finally, allocate space. */
4270 for (p = eh->dyn_relocs; p != NULL; p = p->next)
4271 {
4272 asection *sreloc = elf_section_data (p->sec)->sreloc;
4273 sreloc->_raw_size += p->count * sizeof (Elf32_External_Rela);
4274 }
4275
4276 return TRUE;
4277 }
4278
4279 /* Find any dynamic relocs that apply to read-only sections. */
4280
4281 static bfd_boolean
4282 readonly_dynrelocs (h, inf)
4283 struct elf_link_hash_entry *h;
4284 PTR inf;
4285 {
4286 struct elf_sh_link_hash_entry *eh;
4287 struct elf_sh_dyn_relocs *p;
4288
4289 if (h->root.type == bfd_link_hash_warning)
4290 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4291
4292 eh = (struct elf_sh_link_hash_entry *) h;
4293 for (p = eh->dyn_relocs; p != NULL; p = p->next)
4294 {
4295 asection *s = p->sec->output_section;
4296
4297 if (s != NULL && (s->flags & SEC_READONLY) != 0)
4298 {
4299 struct bfd_link_info *info = (struct bfd_link_info *) inf;
4300
4301 info->flags |= DF_TEXTREL;
4302
4303 /* Not an error, just cut short the traversal. */
4304 return FALSE;
4305 }
4306 }
4307 return TRUE;
4308 }
4309
4310 /* Set the sizes of the dynamic sections. */
4311
4312 static bfd_boolean
4313 sh_elf_size_dynamic_sections (output_bfd, info)
4314 bfd *output_bfd ATTRIBUTE_UNUSED;
4315 struct bfd_link_info *info;
4316 {
4317 struct elf_sh_link_hash_table *htab;
4318 bfd *dynobj;
4319 asection *s;
4320 bfd_boolean relocs;
4321 bfd *ibfd;
4322
4323 htab = sh_elf_hash_table (info);
4324 dynobj = htab->root.dynobj;
4325 BFD_ASSERT (dynobj != NULL);
4326
4327 if (htab->root.dynamic_sections_created)
4328 {
4329 /* Set the contents of the .interp section to the interpreter. */
4330 if (! info->shared)
4331 {
4332 s = bfd_get_section_by_name (dynobj, ".interp");
4333 BFD_ASSERT (s != NULL);
4334 s->_raw_size = sizeof ELF_DYNAMIC_INTERPRETER;
4335 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
4336 }
4337 }
4338
4339 /* Set up .got offsets for local syms, and space for local dynamic
4340 relocs. */
4341 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
4342 {
4343 bfd_signed_vma *local_got;
4344 bfd_signed_vma *end_local_got;
4345 char *local_tls_type;
4346 bfd_size_type locsymcount;
4347 Elf_Internal_Shdr *symtab_hdr;
4348 asection *srel;
4349
4350 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
4351 continue;
4352
4353 for (s = ibfd->sections; s != NULL; s = s->next)
4354 {
4355 struct elf_sh_dyn_relocs *p;
4356
4357 for (p = ((struct elf_sh_dyn_relocs *)
4358 elf_section_data (s)->local_dynrel);
4359 p != NULL;
4360 p = p->next)
4361 {
4362 if (! bfd_is_abs_section (p->sec)
4363 && bfd_is_abs_section (p->sec->output_section))
4364 {
4365 /* Input section has been discarded, either because
4366 it is a copy of a linkonce section or due to
4367 linker script /DISCARD/, so we'll be discarding
4368 the relocs too. */
4369 }
4370 else if (p->count != 0)
4371 {
4372 srel = elf_section_data (p->sec)->sreloc;
4373 srel->_raw_size += p->count * sizeof (Elf32_External_Rela);
4374 if ((p->sec->output_section->flags & SEC_READONLY) != 0)
4375 info->flags |= DF_TEXTREL;
4376 }
4377 }
4378 }
4379
4380 local_got = elf_local_got_refcounts (ibfd);
4381 if (!local_got)
4382 continue;
4383
4384 symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
4385 locsymcount = symtab_hdr->sh_info;
4386 #ifdef INCLUDE_SHMEDIA
4387 /* Count datalabel local GOT. */
4388 locsymcount *= 2;
4389 #endif
4390 end_local_got = local_got + locsymcount;
4391 local_tls_type = sh_elf_local_got_tls_type (ibfd);
4392 s = htab->sgot;
4393 srel = htab->srelgot;
4394 for (; local_got < end_local_got; ++local_got)
4395 {
4396 if (*local_got > 0)
4397 {
4398 *local_got = s->_raw_size;
4399 s->_raw_size += 4;
4400 if (*local_tls_type == GOT_TLS_GD)
4401 s->_raw_size += 4;
4402 if (info->shared)
4403 srel->_raw_size += sizeof (Elf32_External_Rela);
4404 }
4405 else
4406 *local_got = (bfd_vma) -1;
4407 ++local_tls_type;
4408 }
4409 }
4410
4411 if (htab->tls_ldm_got.refcount > 0)
4412 {
4413 /* Allocate 2 got entries and 1 dynamic reloc for R_SH_TLS_LD_32
4414 relocs. */
4415 htab->tls_ldm_got.offset = htab->sgot->_raw_size;
4416 htab->sgot->_raw_size += 8;
4417 htab->srelgot->_raw_size += sizeof (Elf32_External_Rela);
4418 }
4419 else
4420 htab->tls_ldm_got.offset = -1;
4421
4422 /* Allocate global sym .plt and .got entries, and space for global
4423 sym dynamic relocs. */
4424 elf_link_hash_traverse (&htab->root, allocate_dynrelocs, (PTR) info);
4425
4426 /* We now have determined the sizes of the various dynamic sections.
4427 Allocate memory for them. */
4428 relocs = FALSE;
4429 for (s = dynobj->sections; s != NULL; s = s->next)
4430 {
4431 if ((s->flags & SEC_LINKER_CREATED) == 0)
4432 continue;
4433
4434 if (s == htab->splt
4435 || s == htab->sgot
4436 || s == htab->sgotplt)
4437 {
4438 /* Strip this section if we don't need it; see the
4439 comment below. */
4440 }
4441 else if (strncmp (bfd_get_section_name (dynobj, s), ".rela", 5) == 0)
4442 {
4443 if (s->_raw_size != 0 && s != htab->srelplt)
4444 relocs = TRUE;
4445
4446 /* We use the reloc_count field as a counter if we need
4447 to copy relocs into the output file. */
4448 s->reloc_count = 0;
4449 }
4450 else
4451 {
4452 /* It's not one of our sections, so don't allocate space. */
4453 continue;
4454 }
4455
4456 if (s->_raw_size == 0)
4457 {
4458 /* If we don't need this section, strip it from the
4459 output file. This is mostly to handle .rela.bss and
4460 .rela.plt. We must create both sections in
4461 create_dynamic_sections, because they must be created
4462 before the linker maps input sections to output
4463 sections. The linker does that before
4464 adjust_dynamic_symbol is called, and it is that
4465 function which decides whether anything needs to go
4466 into these sections. */
4467
4468 _bfd_strip_section_from_output (info, s);
4469 continue;
4470 }
4471
4472 /* Allocate memory for the section contents. We use bfd_zalloc
4473 here in case unused entries are not reclaimed before the
4474 section's contents are written out. This should not happen,
4475 but this way if it does, we get a R_SH_NONE reloc instead
4476 of garbage. */
4477 s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->_raw_size);
4478 if (s->contents == NULL)
4479 return FALSE;
4480 }
4481
4482 if (htab->root.dynamic_sections_created)
4483 {
4484 /* Add some entries to the .dynamic section. We fill in the
4485 values later, in sh_elf_finish_dynamic_sections, but we
4486 must add the entries now so that we get the correct size for
4487 the .dynamic section. The DT_DEBUG entry is filled in by the
4488 dynamic linker and used by the debugger. */
4489 #define add_dynamic_entry(TAG, VAL) \
4490 bfd_elf32_add_dynamic_entry (info, (bfd_vma) (TAG), (bfd_vma) (VAL))
4491
4492 if (! info->shared)
4493 {
4494 if (! add_dynamic_entry (DT_DEBUG, 0))
4495 return FALSE;
4496 }
4497
4498 if (htab->splt->_raw_size != 0)
4499 {
4500 if (! add_dynamic_entry (DT_PLTGOT, 0)
4501 || ! add_dynamic_entry (DT_PLTRELSZ, 0)
4502 || ! add_dynamic_entry (DT_PLTREL, DT_RELA)
4503 || ! add_dynamic_entry (DT_JMPREL, 0))
4504 return FALSE;
4505 }
4506
4507 if (relocs)
4508 {
4509 if (! add_dynamic_entry (DT_RELA, 0)
4510 || ! add_dynamic_entry (DT_RELASZ, 0)
4511 || ! add_dynamic_entry (DT_RELAENT,
4512 sizeof (Elf32_External_Rela)))
4513 return FALSE;
4514
4515 /* If any dynamic relocs apply to a read-only section,
4516 then we need a DT_TEXTREL entry. */
4517 if ((info->flags & DF_TEXTREL) == 0)
4518 elf_link_hash_traverse (&htab->root, readonly_dynrelocs,
4519 (PTR) info);
4520
4521 if ((info->flags & DF_TEXTREL) != 0)
4522 {
4523 if (! add_dynamic_entry (DT_TEXTREL, 0))
4524 return FALSE;
4525 }
4526 }
4527 }
4528 #undef add_dynamic_entry
4529
4530 return TRUE;
4531 }
4532 \f
4533 /* Relocate an SH ELF section. */
4534
4535 static bfd_boolean
4536 sh_elf_relocate_section (output_bfd, info, input_bfd, input_section,
4537 contents, relocs, local_syms, local_sections)
4538 bfd *output_bfd;
4539 struct bfd_link_info *info;
4540 bfd *input_bfd;
4541 asection *input_section;
4542 bfd_byte *contents;
4543 Elf_Internal_Rela *relocs;
4544 Elf_Internal_Sym *local_syms;
4545 asection **local_sections;
4546 {
4547 struct elf_sh_link_hash_table *htab;
4548 Elf_Internal_Shdr *symtab_hdr;
4549 struct elf_link_hash_entry **sym_hashes;
4550 Elf_Internal_Rela *rel, *relend;
4551 bfd *dynobj;
4552 bfd_vma *local_got_offsets;
4553 asection *sgot;
4554 asection *sgotplt;
4555 asection *splt;
4556 asection *sreloc;
4557 asection *srelgot;
4558
4559 htab = sh_elf_hash_table (info);
4560 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
4561 sym_hashes = elf_sym_hashes (input_bfd);
4562 dynobj = htab->root.dynobj;
4563 local_got_offsets = elf_local_got_offsets (input_bfd);
4564
4565 sgot = htab->sgot;
4566 sgotplt = htab->sgotplt;
4567 splt = htab->splt;
4568 sreloc = NULL;
4569 srelgot = NULL;
4570
4571 rel = relocs;
4572 relend = relocs + input_section->reloc_count;
4573 for (; rel < relend; rel++)
4574 {
4575 int r_type;
4576 reloc_howto_type *howto;
4577 unsigned long r_symndx;
4578 Elf_Internal_Sym *sym;
4579 asection *sec;
4580 struct elf_link_hash_entry *h;
4581 bfd_vma relocation;
4582 bfd_vma addend = (bfd_vma) 0;
4583 bfd_reloc_status_type r;
4584 int seen_stt_datalabel = 0;
4585 bfd_vma off;
4586 int tls_type;
4587
4588 r_symndx = ELF32_R_SYM (rel->r_info);
4589
4590 r_type = ELF32_R_TYPE (rel->r_info);
4591
4592 /* Many of the relocs are only used for relaxing, and are
4593 handled entirely by the relaxation code. */
4594 if (r_type > (int) R_SH_LAST_INVALID_RELOC
4595 && r_type < (int) R_SH_LOOP_START)
4596 continue;
4597 if (r_type == (int) R_SH_NONE)
4598 continue;
4599
4600 if (r_type < 0
4601 || r_type >= R_SH_max
4602 || (r_type >= (int) R_SH_FIRST_INVALID_RELOC
4603 && r_type <= (int) R_SH_LAST_INVALID_RELOC)
4604 || ( r_type >= (int) R_SH_FIRST_INVALID_RELOC_3
4605 && r_type <= (int) R_SH_LAST_INVALID_RELOC_3)
4606 || ( r_type >= (int) R_SH_FIRST_INVALID_RELOC_4
4607 && r_type <= (int) R_SH_LAST_INVALID_RELOC_4)
4608 || ( r_type >= (int) R_SH_FIRST_INVALID_RELOC_5
4609 && r_type <= (int) R_SH_LAST_INVALID_RELOC_5)
4610 || (r_type >= (int) R_SH_FIRST_INVALID_RELOC_2
4611 && r_type <= (int) R_SH_LAST_INVALID_RELOC_2))
4612 {
4613 bfd_set_error (bfd_error_bad_value);
4614 return FALSE;
4615 }
4616
4617 howto = sh_elf_howto_table + r_type;
4618
4619 /* For relocs that aren't partial_inplace, we get the addend from
4620 the relocation. */
4621 if (! howto->partial_inplace)
4622 addend = rel->r_addend;
4623
4624 h = NULL;
4625 sym = NULL;
4626 sec = NULL;
4627 if (r_symndx < symtab_hdr->sh_info)
4628 {
4629 sym = local_syms + r_symndx;
4630 sec = local_sections[r_symndx];
4631 relocation = (sec->output_section->vma
4632 + sec->output_offset
4633 + sym->st_value);
4634 /* A local symbol never has STO_SH5_ISA32, so we don't need
4635 datalabel processing here. Make sure this does not change
4636 without notice. */
4637 if ((sym->st_other & STO_SH5_ISA32) != 0)
4638 ((*info->callbacks->reloc_dangerous)
4639 (info,
4640 _("Unexpected STO_SH5_ISA32 on local symbol is not handled"),
4641 input_bfd, input_section, rel->r_offset));
4642 if (info->relocateable)
4643 {
4644 /* This is a relocateable link. We don't have to change
4645 anything, unless the reloc is against a section symbol,
4646 in which case we have to adjust according to where the
4647 section symbol winds up in the output section. */
4648 sym = local_syms + r_symndx;
4649 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
4650 {
4651 if (! howto->partial_inplace)
4652 {
4653 /* For relocations with the addend in the
4654 relocation, we need just to update the addend.
4655 All real relocs are of type partial_inplace; this
4656 code is mostly for completeness. */
4657 rel->r_addend += sec->output_offset + sym->st_value;
4658
4659 continue;
4660 }
4661
4662 /* Relocs of type partial_inplace need to pick up the
4663 contents in the contents and add the offset resulting
4664 from the changed location of the section symbol.
4665 Using _bfd_final_link_relocate (e.g. goto
4666 final_link_relocate) here would be wrong, because
4667 relocations marked pc_relative would get the current
4668 location subtracted, and we must only do that at the
4669 final link. */
4670 r = _bfd_relocate_contents (howto, input_bfd,
4671 sec->output_offset
4672 + sym->st_value,
4673 contents + rel->r_offset);
4674 goto relocation_done;
4675 }
4676
4677 continue;
4678 }
4679 else if (! howto->partial_inplace)
4680 {
4681 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, sec, rel);
4682 addend = rel->r_addend;
4683 }
4684 else if ((sec->flags & SEC_MERGE)
4685 && ELF_ST_TYPE (sym->st_info) == STT_SECTION)
4686 {
4687 asection *msec;
4688
4689 if (howto->rightshift || howto->src_mask != 0xffffffff)
4690 {
4691 (*_bfd_error_handler)
4692 (_("%s(%s+0x%lx): %s relocation against SEC_MERGE section"),
4693 bfd_archive_filename (input_bfd),
4694 bfd_get_section_name (input_bfd, input_section),
4695 (long) rel->r_offset, howto->name);
4696 return FALSE;
4697 }
4698
4699 addend = bfd_get_32 (input_bfd, contents + rel->r_offset);
4700 msec = sec;
4701 addend =
4702 _bfd_elf_rel_local_sym (output_bfd, sym, &msec, addend)
4703 - relocation;
4704 addend += msec->output_section->vma + msec->output_offset;
4705 bfd_put_32 (input_bfd, addend, contents + rel->r_offset);
4706 addend = 0;
4707 }
4708 }
4709 else
4710 {
4711 /* Section symbol are never (?) placed in the hash table, so
4712 we can just ignore hash relocations when creating a
4713 relocateable object file. */
4714 if (info->relocateable)
4715 continue;
4716
4717 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
4718 while (h->root.type == bfd_link_hash_indirect
4719 || h->root.type == bfd_link_hash_warning)
4720 {
4721 #ifdef INCLUDE_SHMEDIA
4722 /* If the reference passes a symbol marked with
4723 STT_DATALABEL, then any STO_SH5_ISA32 on the final value
4724 doesn't count. */
4725 seen_stt_datalabel |= h->type == STT_DATALABEL;
4726 #endif
4727 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4728 }
4729 if (h->root.type == bfd_link_hash_defined
4730 || h->root.type == bfd_link_hash_defweak)
4731 {
4732 bfd_boolean dyn;
4733
4734 dyn = htab->root.dynamic_sections_created;
4735 sec = h->root.u.def.section;
4736 /* In these cases, we don't need the relocation value.
4737 We check specially because in some obscure cases
4738 sec->output_section will be NULL. */
4739 if (r_type == R_SH_GOTPC
4740 || r_type == R_SH_GOTPC_LOW16
4741 || r_type == R_SH_GOTPC_MEDLOW16
4742 || r_type == R_SH_GOTPC_MEDHI16
4743 || r_type == R_SH_GOTPC_HI16
4744 || ((r_type == R_SH_PLT32
4745 || r_type == R_SH_PLT_LOW16
4746 || r_type == R_SH_PLT_MEDLOW16
4747 || r_type == R_SH_PLT_MEDHI16
4748 || r_type == R_SH_PLT_HI16)
4749 && h->plt.offset != (bfd_vma) -1)
4750 || ((r_type == R_SH_GOT32
4751 || r_type == R_SH_GOT_LOW16
4752 || r_type == R_SH_GOT_MEDLOW16
4753 || r_type == R_SH_GOT_MEDHI16
4754 || r_type == R_SH_GOT_HI16)
4755 && WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info, h)
4756 && (! info->shared
4757 || (! info->symbolic && h->dynindx != -1)
4758 || (h->elf_link_hash_flags
4759 & ELF_LINK_HASH_DEF_REGULAR) == 0))
4760 /* The cases above are those in which relocation is
4761 overwritten in the switch block below. The cases
4762 below are those in which we must defer relocation
4763 to run-time, because we can't resolve absolute
4764 addresses when creating a shared library. */
4765 || (info->shared
4766 && ((! info->symbolic && h->dynindx != -1)
4767 || (h->elf_link_hash_flags
4768 & ELF_LINK_HASH_DEF_REGULAR) == 0)
4769 && ((r_type == R_SH_DIR32
4770 && (h->elf_link_hash_flags
4771 & ELF_LINK_FORCED_LOCAL) == 0)
4772 || r_type == R_SH_REL32)
4773 && ((input_section->flags & SEC_ALLOC) != 0
4774 /* DWARF will emit R_SH_DIR32 relocations in its
4775 sections against symbols defined externally
4776 in shared libraries. We can't do anything
4777 with them here. */
4778 || ((input_section->flags & SEC_DEBUGGING) != 0
4779 && (h->elf_link_hash_flags
4780 & ELF_LINK_HASH_DEF_DYNAMIC) != 0)))
4781 /* Dynamic relocs are not propagated for SEC_DEBUGGING
4782 sections because such sections are not SEC_ALLOC and
4783 thus ld.so will not process them. */
4784 || (sec->output_section == NULL
4785 && ((input_section->flags & SEC_DEBUGGING) != 0
4786 && (h->elf_link_hash_flags
4787 & ELF_LINK_HASH_DEF_DYNAMIC) != 0))
4788 || (sec->output_section == NULL
4789 && (sh_elf_hash_entry (h)->tls_type == GOT_TLS_IE
4790 || sh_elf_hash_entry (h)->tls_type == GOT_TLS_GD)))
4791 relocation = 0;
4792 else if (sec->output_section == NULL)
4793 {
4794 (*_bfd_error_handler)
4795 (_("%s: unresolvable relocation against symbol `%s' from %s section"),
4796 bfd_archive_filename (input_bfd), h->root.root.string,
4797 bfd_get_section_name (input_bfd, input_section));
4798 return FALSE;
4799 }
4800 else
4801 relocation = ((h->root.u.def.value
4802 + sec->output_section->vma
4803 + sec->output_offset)
4804 /* A STO_SH5_ISA32 causes a "bitor 1" to the
4805 symbol value, unless we've seen
4806 STT_DATALABEL on the way to it. */
4807 | ((h->other & STO_SH5_ISA32) != 0
4808 && ! seen_stt_datalabel));
4809 }
4810 else if (h->root.type == bfd_link_hash_undefweak)
4811 relocation = 0;
4812 else if (info->shared
4813 && ! info->no_undefined
4814 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
4815 relocation = 0;
4816 else
4817 {
4818 if (! ((*info->callbacks->undefined_symbol)
4819 (info, h->root.root.string, input_bfd,
4820 input_section, rel->r_offset,
4821 (!info->shared || info->no_undefined
4822 || ELF_ST_VISIBILITY (h->other)))))
4823 return FALSE;
4824 relocation = 0;
4825 }
4826 }
4827
4828 switch ((int) r_type)
4829 {
4830 final_link_relocate:
4831 /* COFF relocs don't use the addend. The addend is used for
4832 R_SH_DIR32 to be compatible with other compilers. */
4833 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
4834 contents, rel->r_offset,
4835 relocation, addend);
4836 break;
4837
4838 case R_SH_IND12W:
4839 goto final_link_relocate;
4840
4841 case R_SH_DIR8WPN:
4842 case R_SH_DIR8WPZ:
4843 case R_SH_DIR8WPL:
4844 /* If the reloc is against the start of this section, then
4845 the assembler has already taken care of it and the reloc
4846 is here only to assist in relaxing. If the reloc is not
4847 against the start of this section, then it's against an
4848 external symbol and we must deal with it ourselves. */
4849 if (input_section->output_section->vma + input_section->output_offset
4850 != relocation)
4851 {
4852 int disp = (relocation
4853 - input_section->output_section->vma
4854 - input_section->output_offset
4855 - rel->r_offset);
4856 int mask = 0;
4857 switch (r_type)
4858 {
4859 case R_SH_DIR8WPN:
4860 case R_SH_DIR8WPZ: mask = 1; break;
4861 case R_SH_DIR8WPL: mask = 3; break;
4862 default: mask = 0; break;
4863 }
4864 if (disp & mask)
4865 {
4866 ((*_bfd_error_handler)
4867 (_("%s: 0x%lx: fatal: unaligned branch target for relax-support relocation"),
4868 bfd_archive_filename (input_section->owner),
4869 (unsigned long) rel->r_offset));
4870 bfd_set_error (bfd_error_bad_value);
4871 return FALSE;
4872 }
4873 relocation -= 4;
4874 goto final_link_relocate;
4875 }
4876 r = bfd_reloc_ok;
4877 break;
4878
4879 default:
4880 #ifdef INCLUDE_SHMEDIA
4881 if (shmedia_prepare_reloc (info, input_bfd, input_section,
4882 contents, rel, &relocation))
4883 goto final_link_relocate;
4884 #endif
4885 bfd_set_error (bfd_error_bad_value);
4886 return FALSE;
4887
4888 case R_SH_DIR32:
4889 case R_SH_REL32:
4890 if (info->shared
4891 && r_symndx != 0
4892 && (input_section->flags & SEC_ALLOC) != 0
4893 && (r_type != R_SH_REL32
4894 || (h != NULL
4895 && h->dynindx != -1
4896 && (! info->symbolic
4897 || (h->elf_link_hash_flags
4898 & ELF_LINK_HASH_DEF_REGULAR) == 0))))
4899 {
4900 Elf_Internal_Rela outrel;
4901 bfd_byte *loc;
4902 bfd_boolean skip, relocate;
4903
4904 /* When generating a shared object, these relocations
4905 are copied into the output file to be resolved at run
4906 time. */
4907
4908 if (sreloc == NULL)
4909 {
4910 const char *name;
4911
4912 name = (bfd_elf_string_from_elf_section
4913 (input_bfd,
4914 elf_elfheader (input_bfd)->e_shstrndx,
4915 elf_section_data (input_section)->rel_hdr.sh_name));
4916 if (name == NULL)
4917 return FALSE;
4918
4919 BFD_ASSERT (strncmp (name, ".rela", 5) == 0
4920 && strcmp (bfd_get_section_name (input_bfd,
4921 input_section),
4922 name + 5) == 0);
4923
4924 sreloc = bfd_get_section_by_name (dynobj, name);
4925 BFD_ASSERT (sreloc != NULL);
4926 }
4927
4928 skip = FALSE;
4929 relocate = FALSE;
4930
4931 outrel.r_offset =
4932 _bfd_elf_section_offset (output_bfd, info, input_section,
4933 rel->r_offset);
4934 if (outrel.r_offset == (bfd_vma) -1)
4935 skip = TRUE;
4936 else if (outrel.r_offset == (bfd_vma) -2)
4937 skip = TRUE, relocate = TRUE;
4938 outrel.r_offset += (input_section->output_section->vma
4939 + input_section->output_offset);
4940
4941 if (skip)
4942 memset (&outrel, 0, sizeof outrel);
4943 else if (r_type == R_SH_REL32)
4944 {
4945 BFD_ASSERT (h != NULL && h->dynindx != -1);
4946 outrel.r_info = ELF32_R_INFO (h->dynindx, R_SH_REL32);
4947 outrel.r_addend
4948 = bfd_get_32 (input_bfd, contents + rel->r_offset);
4949 }
4950 else
4951 {
4952 /* h->dynindx may be -1 if this symbol was marked to
4953 become local. */
4954 if (h == NULL
4955 || ((info->symbolic || h->dynindx == -1)
4956 && (h->elf_link_hash_flags
4957 & ELF_LINK_HASH_DEF_REGULAR) != 0))
4958 {
4959 relocate = TRUE;
4960 outrel.r_info = ELF32_R_INFO (0, R_SH_RELATIVE);
4961 outrel.r_addend
4962 = relocation + bfd_get_32 (input_bfd,
4963 contents + rel->r_offset);
4964 }
4965 else
4966 {
4967 BFD_ASSERT (h->dynindx != -1);
4968 outrel.r_info = ELF32_R_INFO (h->dynindx, R_SH_DIR32);
4969 outrel.r_addend
4970 = relocation + bfd_get_32 (input_bfd,
4971 contents + rel->r_offset);
4972 }
4973 }
4974
4975 loc = sreloc->contents;
4976 loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rela);
4977 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
4978
4979 /* If this reloc is against an external symbol, we do
4980 not want to fiddle with the addend. Otherwise, we
4981 need to include the symbol value so that it becomes
4982 an addend for the dynamic reloc. */
4983 if (! relocate)
4984 continue;
4985 }
4986 goto final_link_relocate;
4987
4988 case R_SH_GOTPLT32:
4989 #ifdef INCLUDE_SHMEDIA
4990 case R_SH_GOTPLT_LOW16:
4991 case R_SH_GOTPLT_MEDLOW16:
4992 case R_SH_GOTPLT_MEDHI16:
4993 case R_SH_GOTPLT_HI16:
4994 case R_SH_GOTPLT10BY4:
4995 case R_SH_GOTPLT10BY8:
4996 #endif
4997 /* Relocation is to the entry for this symbol in the
4998 procedure linkage table. */
4999
5000 if (h == NULL
5001 || (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL)
5002 || ! info->shared
5003 || info->symbolic
5004 || h->dynindx == -1
5005 || h->plt.offset == (bfd_vma) -1
5006 || h->got.offset != (bfd_vma) -1)
5007 goto force_got;
5008
5009 /* Relocation is to the entry for this symbol in the global
5010 offset table extension for the procedure linkage table. */
5011
5012 BFD_ASSERT (sgotplt != NULL);
5013 relocation = (sgotplt->output_offset
5014 + ((h->plt.offset / elf_sh_sizeof_plt (info)
5015 - 1 + 3) * 4));
5016
5017 #ifdef GOT_BIAS
5018 relocation -= GOT_BIAS;
5019 #endif
5020
5021 goto final_link_relocate;
5022
5023 force_got:
5024 case R_SH_GOT32:
5025 #ifdef INCLUDE_SHMEDIA
5026 case R_SH_GOT_LOW16:
5027 case R_SH_GOT_MEDLOW16:
5028 case R_SH_GOT_MEDHI16:
5029 case R_SH_GOT_HI16:
5030 case R_SH_GOT10BY4:
5031 case R_SH_GOT10BY8:
5032 #endif
5033 /* Relocation is to the entry for this symbol in the global
5034 offset table. */
5035
5036 BFD_ASSERT (sgot != NULL);
5037
5038 if (h != NULL)
5039 {
5040 bfd_boolean dyn;
5041
5042 off = h->got.offset;
5043 #ifdef INCLUDE_SHMEDIA
5044 if (seen_stt_datalabel)
5045 {
5046 struct elf_sh_link_hash_entry *hsh;
5047
5048 hsh = (struct elf_sh_link_hash_entry *)h;
5049 off = hsh->datalabel_got.offset;
5050 }
5051 #endif
5052 BFD_ASSERT (off != (bfd_vma) -1);
5053
5054 dyn = htab->root.dynamic_sections_created;
5055 if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info, h)
5056 || (info->shared
5057 && (info->symbolic || h->dynindx == -1
5058 || (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL))
5059 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR)))
5060 {
5061 /* This is actually a static link, or it is a
5062 -Bsymbolic link and the symbol is defined
5063 locally, or the symbol was forced to be local
5064 because of a version file. We must initialize
5065 this entry in the global offset table. Since the
5066 offset must always be a multiple of 4, we use the
5067 least significant bit to record whether we have
5068 initialized it already.
5069
5070 When doing a dynamic link, we create a .rela.got
5071 relocation entry to initialize the value. This
5072 is done in the finish_dynamic_symbol routine. */
5073 if ((off & 1) != 0)
5074 off &= ~1;
5075 else
5076 {
5077 bfd_put_32 (output_bfd, relocation,
5078 sgot->contents + off);
5079 #ifdef INCLUDE_SHMEDIA
5080 if (seen_stt_datalabel)
5081 {
5082 struct elf_sh_link_hash_entry *hsh;
5083
5084 hsh = (struct elf_sh_link_hash_entry *)h;
5085 hsh->datalabel_got.offset |= 1;
5086 }
5087 else
5088 #endif
5089 h->got.offset |= 1;
5090 }
5091 }
5092
5093 relocation = sgot->output_offset + off;
5094 }
5095 else
5096 {
5097 #ifdef INCLUDE_SHMEDIA
5098 if (rel->r_addend)
5099 {
5100 BFD_ASSERT (local_got_offsets != NULL
5101 && (local_got_offsets[symtab_hdr->sh_info
5102 + r_symndx]
5103 != (bfd_vma) -1));
5104
5105 off = local_got_offsets[symtab_hdr->sh_info
5106 + r_symndx];
5107 }
5108 else
5109 {
5110 #endif
5111 BFD_ASSERT (local_got_offsets != NULL
5112 && local_got_offsets[r_symndx] != (bfd_vma) -1);
5113
5114 off = local_got_offsets[r_symndx];
5115 #ifdef INCLUDE_SHMEDIA
5116 }
5117 #endif
5118
5119 /* The offset must always be a multiple of 4. We use
5120 the least significant bit to record whether we have
5121 already generated the necessary reloc. */
5122 if ((off & 1) != 0)
5123 off &= ~1;
5124 else
5125 {
5126 bfd_put_32 (output_bfd, relocation, sgot->contents + off);
5127
5128 if (info->shared)
5129 {
5130 Elf_Internal_Rela outrel;
5131 bfd_byte *loc;
5132
5133 if (srelgot == NULL)
5134 {
5135 srelgot = bfd_get_section_by_name (dynobj,
5136 ".rela.got");
5137 BFD_ASSERT (srelgot != NULL);
5138 }
5139
5140 outrel.r_offset = (sgot->output_section->vma
5141 + sgot->output_offset
5142 + off);
5143 outrel.r_info = ELF32_R_INFO (0, R_SH_RELATIVE);
5144 outrel.r_addend = relocation;
5145 loc = srelgot->contents;
5146 loc += srelgot->reloc_count++ * sizeof (Elf32_External_Rela);
5147 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
5148 }
5149
5150 #ifdef INCLUDE_SHMEDIA
5151 if (rel->r_addend)
5152 local_got_offsets[symtab_hdr->sh_info + r_symndx] |= 1;
5153 else
5154 #endif
5155 local_got_offsets[r_symndx] |= 1;
5156 }
5157
5158 relocation = sgot->output_offset + off;
5159 }
5160
5161 #ifdef GOT_BIAS
5162 relocation -= GOT_BIAS;
5163 #endif
5164
5165 goto final_link_relocate;
5166
5167 case R_SH_GOTOFF:
5168 #ifdef INCLUDE_SHMEDIA
5169 case R_SH_GOTOFF_LOW16:
5170 case R_SH_GOTOFF_MEDLOW16:
5171 case R_SH_GOTOFF_MEDHI16:
5172 case R_SH_GOTOFF_HI16:
5173 #endif
5174 /* Relocation is relative to the start of the global offset
5175 table. */
5176
5177 BFD_ASSERT (sgot != NULL);
5178
5179 /* Note that sgot->output_offset is not involved in this
5180 calculation. We always want the start of .got. If we
5181 defined _GLOBAL_OFFSET_TABLE in a different way, as is
5182 permitted by the ABI, we might have to change this
5183 calculation. */
5184 relocation -= sgot->output_section->vma;
5185
5186 #ifdef GOT_BIAS
5187 relocation -= GOT_BIAS;
5188 #endif
5189
5190 addend = rel->r_addend;
5191
5192 goto final_link_relocate;
5193
5194 case R_SH_GOTPC:
5195 #ifdef INCLUDE_SHMEDIA
5196 case R_SH_GOTPC_LOW16:
5197 case R_SH_GOTPC_MEDLOW16:
5198 case R_SH_GOTPC_MEDHI16:
5199 case R_SH_GOTPC_HI16:
5200 #endif
5201 /* Use global offset table as symbol value. */
5202
5203 BFD_ASSERT (sgot != NULL);
5204 relocation = sgot->output_section->vma;
5205
5206 #ifdef GOT_BIAS
5207 relocation += GOT_BIAS;
5208 #endif
5209
5210 addend = rel->r_addend;
5211
5212 goto final_link_relocate;
5213
5214 case R_SH_PLT32:
5215 #ifdef INCLUDE_SHMEDIA
5216 case R_SH_PLT_LOW16:
5217 case R_SH_PLT_MEDLOW16:
5218 case R_SH_PLT_MEDHI16:
5219 case R_SH_PLT_HI16:
5220 #endif
5221 /* Relocation is to the entry for this symbol in the
5222 procedure linkage table. */
5223
5224 /* Resolve a PLT reloc against a local symbol directly,
5225 without using the procedure linkage table. */
5226 if (h == NULL)
5227 goto final_link_relocate;
5228
5229 if (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL)
5230 goto final_link_relocate;
5231
5232 if (h->plt.offset == (bfd_vma) -1)
5233 {
5234 /* We didn't make a PLT entry for this symbol. This
5235 happens when statically linking PIC code, or when
5236 using -Bsymbolic. */
5237 goto final_link_relocate;
5238 }
5239
5240 BFD_ASSERT (splt != NULL);
5241 relocation = (splt->output_section->vma
5242 + splt->output_offset
5243 + h->plt.offset);
5244
5245 #ifdef INCLUDE_SHMEDIA
5246 relocation++;
5247 #endif
5248
5249 addend = rel->r_addend;
5250
5251 goto final_link_relocate;
5252
5253 case R_SH_LOOP_START:
5254 {
5255 static bfd_vma start, end;
5256
5257 start = (relocation + rel->r_addend
5258 - (sec->output_section->vma + sec->output_offset));
5259 r = sh_elf_reloc_loop (r_type, input_bfd, input_section, contents,
5260 rel->r_offset, sec, start, end);
5261 break;
5262
5263 case R_SH_LOOP_END:
5264 end = (relocation + rel->r_addend
5265 - (sec->output_section->vma + sec->output_offset));
5266 r = sh_elf_reloc_loop (r_type, input_bfd, input_section, contents,
5267 rel->r_offset, sec, start, end);
5268 break;
5269 }
5270
5271 case R_SH_TLS_GD_32:
5272 case R_SH_TLS_IE_32:
5273 r_type = sh_elf_optimized_tls_reloc (info, r_type, h == NULL);
5274 tls_type = GOT_UNKNOWN;
5275 if (h == NULL && local_got_offsets)
5276 tls_type = sh_elf_local_got_tls_type (input_bfd) [r_symndx];
5277 else if (h != NULL)
5278 {
5279 tls_type = sh_elf_hash_entry (h)->tls_type;
5280 if (! info->shared
5281 && (h->dynindx == -1
5282 || (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR)))
5283 {
5284 struct elf_sh_dyn_relocs *p;
5285
5286 /* If TPOFF32 relocation can be created, convert it. */
5287 for (p = sh_elf_hash_entry (h)->dyn_relocs; p; p = p->next)
5288 if (p->sec == input_section && p->tls_tpoff32)
5289 {
5290 r_type = R_SH_TLS_LE_32;
5291 break;
5292 }
5293 }
5294 }
5295
5296 if (r_type == R_SH_TLS_GD_32 && tls_type == GOT_TLS_IE)
5297 r_type = R_SH_TLS_IE_32;
5298
5299 if (r_type == R_SH_TLS_LE_32)
5300 {
5301 bfd_vma offset;
5302 unsigned short insn;
5303 int indx;
5304 Elf_Internal_Rela outrel;
5305 bfd_byte *loc;
5306
5307 if (ELF32_R_TYPE (rel->r_info) == R_SH_TLS_GD_32)
5308 {
5309 /* GD->LE transition:
5310 mov.l 1f,r4; mova 2f,r0; mov.l 2f,r1; add r0,r1;
5311 jsr @r1; add r12,r4; bra 3f; nop; .align 2;
5312 1: .long x$TLSGD; 2: .long __tls_get_addr@PLT; 3:
5313 We change it into:
5314 mov.l 1f,r4; stc gbr,r0; add r4,r0; nop;
5315 nop; nop; ...
5316 1: .long x@TPOFF; 2: .long __tls_get_addr@PLT; 3:. */
5317
5318 offset = rel->r_offset;
5319 BFD_ASSERT (offset >= 16);
5320 /* Size of GD instructions is 16 or 18. */
5321 offset -= 16;
5322 insn = bfd_get_16 (input_bfd, contents + offset + 0);
5323 if ((insn & 0xff00) == 0xc700)
5324 {
5325 BFD_ASSERT (offset >= 2);
5326 offset -= 2;
5327 insn = bfd_get_16 (input_bfd, contents + offset + 0);
5328 }
5329
5330 BFD_ASSERT ((insn & 0xff00) == 0xd400);
5331 insn = bfd_get_16 (input_bfd, contents + offset + 2);
5332 BFD_ASSERT ((insn & 0xff00) == 0xc700);
5333 insn = bfd_get_16 (input_bfd, contents + offset + 4);
5334 BFD_ASSERT ((insn & 0xff00) == 0xd100);
5335 insn = bfd_get_16 (input_bfd, contents + offset + 6);
5336 BFD_ASSERT (insn == 0x310c);
5337 insn = bfd_get_16 (input_bfd, contents + offset + 8);
5338 BFD_ASSERT (insn == 0x410b);
5339 insn = bfd_get_16 (input_bfd, contents + offset + 10);
5340 BFD_ASSERT (insn == 0x34cc);
5341
5342 bfd_put_16 (output_bfd, 0x0012, contents + offset + 2);
5343 bfd_put_16 (output_bfd, 0x304c, contents + offset + 4);
5344 bfd_put_16 (output_bfd, 0x0009, contents + offset + 6);
5345 bfd_put_16 (output_bfd, 0x0009, contents + offset + 8);
5346 bfd_put_16 (output_bfd, 0x0009, contents + offset + 10);
5347 }
5348 else
5349 {
5350 int index;
5351
5352 /* IE->LE transition:
5353 mov.l 1f,r0; stc gbr,rN; mov.l @(r0,r12),rM;
5354 bra 2f; add ...; .align 2; 1: x@GOTTPOFF; 2:
5355 We change it into:
5356 mov.l .Ln,rM; stc gbr,rN; nop; ...;
5357 1: x@TPOFF; 2:. */
5358
5359 offset = rel->r_offset;
5360 BFD_ASSERT (offset >= 16);
5361 /* Size of IE instructions is 10 or 12. */
5362 offset -= 10;
5363 insn = bfd_get_16 (input_bfd, contents + offset + 0);
5364 if ((insn & 0xf0ff) == 0x0012)
5365 {
5366 BFD_ASSERT (offset >= 2);
5367 offset -= 2;
5368 insn = bfd_get_16 (input_bfd, contents + offset + 0);
5369 }
5370
5371 BFD_ASSERT ((insn & 0xff00) == 0xd000);
5372 index = insn & 0x00ff;
5373 insn = bfd_get_16 (input_bfd, contents + offset + 2);
5374 BFD_ASSERT ((insn & 0xf0ff) == 0x0012);
5375 insn = bfd_get_16 (input_bfd, contents + offset + 4);
5376 BFD_ASSERT ((insn & 0xf0ff) == 0x00ce);
5377 insn = 0xd000 | (insn & 0x0f00) | index;
5378 bfd_put_16 (output_bfd, insn, contents + offset + 0);
5379 bfd_put_16 (output_bfd, 0x0009, contents + offset + 4);
5380 }
5381
5382 if (sreloc == NULL)
5383 {
5384 const char *name;
5385
5386 name = (bfd_elf_string_from_elf_section
5387 (input_bfd,
5388 elf_elfheader (input_bfd)->e_shstrndx,
5389 elf_section_data (input_section)->rel_hdr.sh_name));
5390 if (name == NULL)
5391 return FALSE;
5392
5393 BFD_ASSERT (strncmp (name, ".rela", 5) == 0
5394 && strcmp (bfd_get_section_name (input_bfd,
5395 input_section),
5396 name + 5) == 0);
5397
5398 sreloc = bfd_get_section_by_name (dynobj, name);
5399 BFD_ASSERT (sreloc != NULL);
5400 }
5401
5402 if (h == NULL
5403 || h->dynindx == -1
5404 || (! info->shared
5405 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR)))
5406 indx = 0;
5407 else
5408 indx = h->dynindx;
5409 outrel.r_offset = (input_section->output_section->vma
5410 + input_section->output_offset
5411 + rel->r_offset);
5412 outrel.r_info = ELF32_R_INFO (indx, R_SH_TLS_TPOFF32);
5413 if (indx == 0)
5414 outrel.r_addend = relocation - dtpoff_base (info);
5415 else
5416 outrel.r_addend = 0;
5417
5418 loc = sreloc->contents;
5419 loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rela);
5420 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
5421 continue;
5422 }
5423
5424 sgot = htab->sgot;
5425 if (sgot == NULL)
5426 abort ();
5427
5428 if (h != NULL)
5429 off = h->got.offset;
5430 else
5431 {
5432 if (local_got_offsets == NULL)
5433 abort ();
5434
5435 off = local_got_offsets[r_symndx];
5436 }
5437
5438 if ((off & 1) != 0)
5439 off &= ~1;
5440 else
5441 {
5442 Elf_Internal_Rela outrel;
5443 bfd_byte *loc;
5444 int dr_type, indx;
5445
5446 if (srelgot == NULL)
5447 {
5448 srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
5449 BFD_ASSERT (srelgot != NULL);
5450 }
5451
5452 outrel.r_offset = (sgot->output_section->vma
5453 + sgot->output_offset + off);
5454
5455 if (h == NULL
5456 || h->dynindx == -1
5457 || (! info->shared
5458 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR)))
5459 indx = 0;
5460 else
5461 indx = h->dynindx;
5462 dr_type = (r_type == R_SH_TLS_GD_32 ? R_SH_TLS_DTPMOD32 :
5463 R_SH_TLS_TPOFF32);
5464 if (dr_type == R_SH_TLS_TPOFF32 && indx == 0)
5465 outrel.r_addend = relocation - dtpoff_base (info);
5466 else
5467 outrel.r_addend = 0;
5468 outrel.r_info = ELF32_R_INFO (indx, dr_type);
5469 loc = srelgot->contents;
5470 loc += srelgot->reloc_count++ * sizeof (Elf32_External_Rela);
5471 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
5472
5473 if (r_type == R_SH_TLS_GD_32)
5474 {
5475 if (indx == 0)
5476 {
5477 bfd_put_32 (output_bfd,
5478 relocation - dtpoff_base (info),
5479 sgot->contents + off + 4);
5480 }
5481 else
5482 {
5483 outrel.r_info = ELF32_R_INFO (indx,
5484 R_SH_TLS_DTPOFF32);
5485 outrel.r_offset += 4;
5486 outrel.r_addend = 0;
5487 srelgot->reloc_count++;
5488 loc += sizeof (Elf32_External_Rela);
5489 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
5490 }
5491 }
5492
5493 if (h != NULL)
5494 h->got.offset |= 1;
5495 else
5496 local_got_offsets[r_symndx] |= 1;
5497 }
5498
5499 if (off >= (bfd_vma) -2)
5500 abort ();
5501
5502 if (r_type == (int) ELF32_R_TYPE (rel->r_info))
5503 relocation = sgot->output_offset + off;
5504 else
5505 {
5506 bfd_vma offset;
5507 unsigned short insn;
5508
5509 /* GD->IE transition:
5510 mov.l 1f,r4; mova 2f,r0; mov.l 2f,r1; add r0,r1;
5511 jsr @r1; add r12,r4; bra 3f; nop; .align 2;
5512 1: .long x$TLSGD; 2: .long __tls_get_addr@PLT; 3:
5513 We change it into:
5514 mov.l 1f,r0; stc gbr,r4; mov.l @(r0,r12),r0; add r4,r0;
5515 nop; nop; bra 3f; nop; .align 2;
5516 1: .long x@TPOFF; 2:...; 3:. */
5517
5518 offset = rel->r_offset;
5519 BFD_ASSERT (offset >= 16);
5520 /* Size of GD instructions is 16 or 18. */
5521 offset -= 16;
5522 insn = bfd_get_16 (input_bfd, contents + offset + 0);
5523 if ((insn & 0xff00) == 0xc700)
5524 {
5525 BFD_ASSERT (offset >= 2);
5526 offset -= 2;
5527 insn = bfd_get_16 (input_bfd, contents + offset + 0);
5528 }
5529
5530 BFD_ASSERT ((insn & 0xff00) == 0xd400);
5531
5532 /* Replace mov.l 1f,R4 with mov.l 1f,r0. */
5533 bfd_put_16 (output_bfd, insn & 0xf0ff, contents + offset);
5534
5535 insn = bfd_get_16 (input_bfd, contents + offset + 2);
5536 BFD_ASSERT ((insn & 0xff00) == 0xc700);
5537 insn = bfd_get_16 (input_bfd, contents + offset + 4);
5538 BFD_ASSERT ((insn & 0xff00) == 0xd100);
5539 insn = bfd_get_16 (input_bfd, contents + offset + 6);
5540 BFD_ASSERT (insn == 0x310c);
5541 insn = bfd_get_16 (input_bfd, contents + offset + 8);
5542 BFD_ASSERT (insn == 0x410b);
5543 insn = bfd_get_16 (input_bfd, contents + offset + 10);
5544 BFD_ASSERT (insn == 0x34cc);
5545
5546 bfd_put_16 (output_bfd, 0x0412, contents + offset + 2);
5547 bfd_put_16 (output_bfd, 0x00ce, contents + offset + 4);
5548 bfd_put_16 (output_bfd, 0x304c, contents + offset + 6);
5549 bfd_put_16 (output_bfd, 0x0009, contents + offset + 8);
5550 bfd_put_16 (output_bfd, 0x0009, contents + offset + 10);
5551
5552 bfd_put_32 (output_bfd, sgot->output_offset + off,
5553 contents + rel->r_offset);
5554
5555 continue;
5556 }
5557
5558 addend = rel->r_addend;
5559
5560 goto final_link_relocate;
5561
5562 case R_SH_TLS_LD_32:
5563 if (! info->shared)
5564 {
5565 bfd_vma offset;
5566 unsigned short insn;
5567
5568 /* LD->LE transition:
5569 mov.l 1f,r4; mova 2f,r0; mov.l 2f,r1; add r0,r1;
5570 jsr @r1; add r12,r4; bra 3f; nop; .align 2;
5571 1: .long x$TLSLD; 2: .long __tls_get_addr@PLT; 3:
5572 We change it into:
5573 stc gbr,r0; nop; nop; nop;
5574 nop; nop; bra 3f; ...; 3:. */
5575
5576 offset = rel->r_offset;
5577 BFD_ASSERT (offset >= 16);
5578 /* Size of LD instructions is 16 or 18. */
5579 offset -= 16;
5580 insn = bfd_get_16 (input_bfd, contents + offset + 0);
5581 if ((insn & 0xff00) == 0xc700)
5582 {
5583 BFD_ASSERT (offset >= 2);
5584 offset -= 2;
5585 insn = bfd_get_16 (input_bfd, contents + offset + 0);
5586 }
5587
5588 BFD_ASSERT ((insn & 0xff00) == 0xd400);
5589 insn = bfd_get_16 (input_bfd, contents + offset + 2);
5590 BFD_ASSERT ((insn & 0xff00) == 0xc700);
5591 insn = bfd_get_16 (input_bfd, contents + offset + 4);
5592 BFD_ASSERT ((insn & 0xff00) == 0xd100);
5593 insn = bfd_get_16 (input_bfd, contents + offset + 6);
5594 BFD_ASSERT (insn == 0x310c);
5595 insn = bfd_get_16 (input_bfd, contents + offset + 8);
5596 BFD_ASSERT (insn == 0x410b);
5597 insn = bfd_get_16 (input_bfd, contents + offset + 10);
5598 BFD_ASSERT (insn == 0x34cc);
5599
5600 bfd_put_16 (output_bfd, 0x0012, contents + offset + 0);
5601 bfd_put_16 (output_bfd, 0x0009, contents + offset + 2);
5602 bfd_put_16 (output_bfd, 0x0009, contents + offset + 4);
5603 bfd_put_16 (output_bfd, 0x0009, contents + offset + 6);
5604 bfd_put_16 (output_bfd, 0x0009, contents + offset + 8);
5605 bfd_put_16 (output_bfd, 0x0009, contents + offset + 10);
5606
5607 continue;
5608 }
5609
5610 sgot = htab->sgot;
5611 if (sgot == NULL)
5612 abort ();
5613
5614 off = htab->tls_ldm_got.offset;
5615 if (off & 1)
5616 off &= ~1;
5617 else
5618 {
5619 Elf_Internal_Rela outrel;
5620 bfd_byte *loc;
5621
5622 srelgot = htab->srelgot;
5623 if (srelgot == NULL)
5624 abort ();
5625
5626 outrel.r_offset = (sgot->output_section->vma
5627 + sgot->output_offset + off);
5628 outrel.r_addend = 0;
5629 outrel.r_info = ELF32_R_INFO (0, R_SH_TLS_DTPMOD32);
5630 loc = srelgot->contents;
5631 loc += srelgot->reloc_count++ * sizeof (Elf32_External_Rela);
5632 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
5633 htab->tls_ldm_got.offset |= 1;
5634 }
5635
5636 relocation = sgot->output_offset + off;
5637 addend = rel->r_addend;
5638
5639 goto final_link_relocate;
5640
5641 case R_SH_TLS_LDO_32:
5642 if (! info->shared)
5643 {
5644 int indx;
5645 Elf_Internal_Rela outrel;
5646 bfd_byte *loc;
5647
5648 if (sreloc == NULL)
5649 {
5650 const char *name;
5651
5652 name = (bfd_elf_string_from_elf_section
5653 (input_bfd,
5654 elf_elfheader (input_bfd)->e_shstrndx,
5655 elf_section_data (input_section)->rel_hdr.sh_name));
5656 if (name == NULL)
5657 return FALSE;
5658
5659 BFD_ASSERT (strncmp (name, ".rela", 5) == 0
5660 && strcmp (bfd_get_section_name (input_bfd,
5661 input_section),
5662 name + 5) == 0);
5663
5664 sreloc = bfd_get_section_by_name (dynobj, name);
5665 BFD_ASSERT (sreloc != NULL);
5666 }
5667
5668 indx = (h && h->dynindx != -1) ? h->dynindx : 0;
5669 outrel.r_offset = (input_section->output_section->vma
5670 + input_section->output_offset
5671 + rel->r_offset);
5672 outrel.r_info = ELF32_R_INFO (indx, R_SH_TLS_TPOFF32);
5673 if (indx == 0)
5674 outrel.r_addend = relocation - dtpoff_base (info);
5675 else
5676 outrel.r_addend = 0;
5677
5678 loc = sreloc->contents;
5679 loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rela);
5680 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
5681 continue;
5682 }
5683 else
5684 relocation -= dtpoff_base (info);
5685
5686 addend = rel->r_addend;
5687 goto final_link_relocate;
5688
5689 case R_SH_TLS_LE_32:
5690 {
5691 int indx;
5692 Elf_Internal_Rela outrel;
5693 bfd_byte *loc;
5694
5695 if (sreloc == NULL)
5696 {
5697 const char *name;
5698
5699 name = (bfd_elf_string_from_elf_section
5700 (input_bfd,
5701 elf_elfheader (input_bfd)->e_shstrndx,
5702 elf_section_data (input_section)->rel_hdr.sh_name));
5703 if (name == NULL)
5704 return FALSE;
5705
5706 BFD_ASSERT (strncmp (name, ".rela", 5) == 0
5707 && strcmp (bfd_get_section_name (input_bfd,
5708 input_section),
5709 name + 5) == 0);
5710
5711 sreloc = bfd_get_section_by_name (dynobj, name);
5712 BFD_ASSERT (sreloc != NULL);
5713 }
5714
5715 indx = (h && h->dynindx != -1) ? h->dynindx : 0;
5716 outrel.r_offset = (input_section->output_section->vma
5717 + input_section->output_offset
5718 + rel->r_offset);
5719 outrel.r_info = ELF32_R_INFO (indx, R_SH_TLS_TPOFF32);
5720 if (indx == 0)
5721 outrel.r_addend = relocation - dtpoff_base (info);
5722 else
5723 outrel.r_addend = 0;
5724
5725 loc = sreloc->contents;
5726 loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rela);
5727 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
5728 continue;
5729 }
5730 }
5731
5732 relocation_done:
5733 if (r != bfd_reloc_ok)
5734 {
5735 switch (r)
5736 {
5737 default:
5738 case bfd_reloc_outofrange:
5739 abort ();
5740 case bfd_reloc_overflow:
5741 {
5742 const char *name;
5743
5744 if (h != NULL)
5745 name = h->root.root.string;
5746 else
5747 {
5748 name = (bfd_elf_string_from_elf_section
5749 (input_bfd, symtab_hdr->sh_link, sym->st_name));
5750 if (name == NULL)
5751 return FALSE;
5752 if (*name == '\0')
5753 name = bfd_section_name (input_bfd, sec);
5754 }
5755 if (! ((*info->callbacks->reloc_overflow)
5756 (info, name, howto->name, (bfd_vma) 0,
5757 input_bfd, input_section, rel->r_offset)))
5758 return FALSE;
5759 }
5760 break;
5761 }
5762 }
5763 }
5764
5765 return TRUE;
5766 }
5767
5768 /* This is a version of bfd_generic_get_relocated_section_contents
5769 which uses sh_elf_relocate_section. */
5770
5771 static bfd_byte *
5772 sh_elf_get_relocated_section_contents (output_bfd, link_info, link_order,
5773 data, relocateable, symbols)
5774 bfd *output_bfd;
5775 struct bfd_link_info *link_info;
5776 struct bfd_link_order *link_order;
5777 bfd_byte *data;
5778 bfd_boolean relocateable;
5779 asymbol **symbols;
5780 {
5781 Elf_Internal_Shdr *symtab_hdr;
5782 asection *input_section = link_order->u.indirect.section;
5783 bfd *input_bfd = input_section->owner;
5784 asection **sections = NULL;
5785 Elf_Internal_Rela *internal_relocs = NULL;
5786 Elf_Internal_Sym *isymbuf = NULL;
5787
5788 /* We only need to handle the case of relaxing, or of having a
5789 particular set of section contents, specially. */
5790 if (relocateable
5791 || elf_section_data (input_section)->this_hdr.contents == NULL)
5792 return bfd_generic_get_relocated_section_contents (output_bfd, link_info,
5793 link_order, data,
5794 relocateable,
5795 symbols);
5796
5797 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
5798
5799 memcpy (data, elf_section_data (input_section)->this_hdr.contents,
5800 (size_t) input_section->_raw_size);
5801
5802 if ((input_section->flags & SEC_RELOC) != 0
5803 && input_section->reloc_count > 0)
5804 {
5805 asection **secpp;
5806 Elf_Internal_Sym *isym, *isymend;
5807 bfd_size_type amt;
5808
5809 internal_relocs = (_bfd_elf32_link_read_relocs
5810 (input_bfd, input_section, (PTR) NULL,
5811 (Elf_Internal_Rela *) NULL, FALSE));
5812 if (internal_relocs == NULL)
5813 goto error_return;
5814
5815 if (symtab_hdr->sh_info != 0)
5816 {
5817 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
5818 if (isymbuf == NULL)
5819 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr,
5820 symtab_hdr->sh_info, 0,
5821 NULL, NULL, NULL);
5822 if (isymbuf == NULL)
5823 goto error_return;
5824 }
5825
5826 amt = symtab_hdr->sh_info;
5827 amt *= sizeof (asection *);
5828 sections = (asection **) bfd_malloc (amt);
5829 if (sections == NULL && amt != 0)
5830 goto error_return;
5831
5832 isymend = isymbuf + symtab_hdr->sh_info;
5833 for (isym = isymbuf, secpp = sections; isym < isymend; ++isym, ++secpp)
5834 {
5835 asection *isec;
5836
5837 if (isym->st_shndx == SHN_UNDEF)
5838 isec = bfd_und_section_ptr;
5839 else if (isym->st_shndx == SHN_ABS)
5840 isec = bfd_abs_section_ptr;
5841 else if (isym->st_shndx == SHN_COMMON)
5842 isec = bfd_com_section_ptr;
5843 else
5844 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
5845
5846 *secpp = isec;
5847 }
5848
5849 if (! sh_elf_relocate_section (output_bfd, link_info, input_bfd,
5850 input_section, data, internal_relocs,
5851 isymbuf, sections))
5852 goto error_return;
5853
5854 if (sections != NULL)
5855 free (sections);
5856 if (isymbuf != NULL
5857 && symtab_hdr->contents != (unsigned char *) isymbuf)
5858 free (isymbuf);
5859 if (elf_section_data (input_section)->relocs != internal_relocs)
5860 free (internal_relocs);
5861 }
5862
5863 return data;
5864
5865 error_return:
5866 if (sections != NULL)
5867 free (sections);
5868 if (isymbuf != NULL
5869 && symtab_hdr->contents != (unsigned char *) isymbuf)
5870 free (isymbuf);
5871 if (internal_relocs != NULL
5872 && elf_section_data (input_section)->relocs != internal_relocs)
5873 free (internal_relocs);
5874 return NULL;
5875 }
5876
5877 /* Return the base VMA address which should be subtracted from real addresses
5878 when resolving @dtpoff relocation.
5879 This is PT_TLS segment p_vaddr. */
5880
5881 static bfd_vma
5882 dtpoff_base (info)
5883 struct bfd_link_info *info;
5884 {
5885 /* If tls_segment is NULL, we should have signalled an error already. */
5886 if (elf_hash_table (info)->tls_segment == NULL)
5887 return 0;
5888 return elf_hash_table (info)->tls_segment->start;
5889 }
5890
5891 static asection *
5892 sh_elf_gc_mark_hook (sec, info, rel, h, sym)
5893 asection *sec;
5894 struct bfd_link_info *info ATTRIBUTE_UNUSED;
5895 Elf_Internal_Rela *rel;
5896 struct elf_link_hash_entry *h;
5897 Elf_Internal_Sym *sym;
5898 {
5899 if (h != NULL)
5900 {
5901 switch (ELF32_R_TYPE (rel->r_info))
5902 {
5903 case R_SH_GNU_VTINHERIT:
5904 case R_SH_GNU_VTENTRY:
5905 break;
5906
5907 default:
5908 #ifdef INCLUDE_SHMEDIA
5909 while (h->root.type == bfd_link_hash_indirect
5910 && h->root.u.i.link)
5911 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5912 #endif
5913 switch (h->root.type)
5914 {
5915 case bfd_link_hash_defined:
5916 case bfd_link_hash_defweak:
5917 return h->root.u.def.section;
5918
5919 case bfd_link_hash_common:
5920 return h->root.u.c.p->section;
5921
5922 default:
5923 break;
5924 }
5925 }
5926 }
5927 else
5928 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
5929
5930 return NULL;
5931 }
5932
5933 /* Update the got entry reference counts for the section being removed. */
5934
5935 static bfd_boolean
5936 sh_elf_gc_sweep_hook (abfd, info, sec, relocs)
5937 bfd *abfd;
5938 struct bfd_link_info *info;
5939 asection *sec;
5940 const Elf_Internal_Rela *relocs;
5941 {
5942 Elf_Internal_Shdr *symtab_hdr;
5943 struct elf_link_hash_entry **sym_hashes;
5944 bfd_signed_vma *local_got_refcounts;
5945 const Elf_Internal_Rela *rel, *relend;
5946
5947 elf_section_data (sec)->local_dynrel = NULL;
5948
5949 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
5950 sym_hashes = elf_sym_hashes (abfd);
5951 local_got_refcounts = elf_local_got_refcounts (abfd);
5952
5953 relend = relocs + sec->reloc_count;
5954 for (rel = relocs; rel < relend; rel++)
5955 {
5956 unsigned long r_symndx;
5957 unsigned int r_type;
5958 struct elf_link_hash_entry *h = NULL;
5959 #ifdef INCLUDE_SHMEDIA
5960 int seen_stt_datalabel = 0;
5961 #endif
5962
5963 r_symndx = ELF32_R_SYM (rel->r_info);
5964 if (r_symndx >= symtab_hdr->sh_info)
5965 {
5966 struct elf_sh_link_hash_entry *eh;
5967 struct elf_sh_dyn_relocs **pp;
5968 struct elf_sh_dyn_relocs *p;
5969
5970 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
5971 #ifdef INCLUDE_SHMEDIA
5972 while (h->root.type == bfd_link_hash_indirect
5973 || h->root.type == bfd_link_hash_warning)
5974 {
5975 seen_stt_datalabel |= h->type == STT_DATALABEL;
5976 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5977 }
5978 #endif
5979 eh = (struct elf_sh_link_hash_entry *) h;
5980 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; pp = &p->next)
5981 if (p->sec == sec)
5982 {
5983 /* Everything must go for SEC. */
5984 *pp = p->next;
5985 break;
5986 }
5987 }
5988
5989 r_type = ELF32_R_TYPE (rel->r_info);
5990 switch (sh_elf_optimized_tls_reloc (info, r_type, h != NULL))
5991 {
5992 case R_SH_TLS_LD_32:
5993 if (sh_elf_hash_table (info)->tls_ldm_got.refcount > 0)
5994 sh_elf_hash_table (info)->tls_ldm_got.refcount -= 1;
5995 break;
5996
5997 case R_SH_GOT32:
5998 case R_SH_GOTOFF:
5999 case R_SH_GOTPC:
6000 #ifdef INCLUDE_SHMEDIA
6001 case R_SH_GOT_LOW16:
6002 case R_SH_GOT_MEDLOW16:
6003 case R_SH_GOT_MEDHI16:
6004 case R_SH_GOT_HI16:
6005 case R_SH_GOT10BY4:
6006 case R_SH_GOT10BY8:
6007 case R_SH_GOTOFF_LOW16:
6008 case R_SH_GOTOFF_MEDLOW16:
6009 case R_SH_GOTOFF_MEDHI16:
6010 case R_SH_GOTOFF_HI16:
6011 case R_SH_GOTPC_LOW16:
6012 case R_SH_GOTPC_MEDLOW16:
6013 case R_SH_GOTPC_MEDHI16:
6014 case R_SH_GOTPC_HI16:
6015 #endif
6016 case R_SH_TLS_GD_32:
6017 case R_SH_TLS_IE_32:
6018 if (h != NULL)
6019 {
6020 #ifdef INCLUDE_SHMEDIA
6021 if (seen_stt_datalabel)
6022 {
6023 struct elf_sh_link_hash_entry *eh;
6024 eh = (struct elf_sh_link_hash_entry *) h;
6025 if (eh->datalabel_got.refcount > 0)
6026 eh->datalabel_got.refcount -= 1;
6027 }
6028 else
6029 #endif
6030 if (h->got.refcount > 0)
6031 h->got.refcount -= 1;
6032 }
6033 else if (local_got_refcounts != NULL)
6034 {
6035 #ifdef INCLUDE_SHMEDIA
6036 if (rel->r_addend & 1)
6037 {
6038 if (local_got_refcounts[symtab_hdr->sh_info + r_symndx] > 0)
6039 local_got_refcounts[symtab_hdr->sh_info + r_symndx] -= 1;
6040 }
6041 else
6042 #endif
6043 if (local_got_refcounts[r_symndx] > 0)
6044 local_got_refcounts[r_symndx] -= 1;
6045 }
6046 break;
6047
6048 case R_SH_DIR32:
6049 case R_SH_REL32:
6050 if (info->shared)
6051 break;
6052 /* Fall thru */
6053
6054 case R_SH_PLT32:
6055 #ifdef INCLUDE_SHMEDIA
6056 case R_SH_PLT_LOW16:
6057 case R_SH_PLT_MEDLOW16:
6058 case R_SH_PLT_MEDHI16:
6059 case R_SH_PLT_HI16:
6060 #endif
6061 if (h != NULL)
6062 {
6063 if (h->plt.refcount > 0)
6064 h->plt.refcount -= 1;
6065 }
6066 break;
6067
6068 case R_SH_GOTPLT32:
6069 #ifdef INCLUDE_SHMEDIA
6070 case R_SH_GOTPLT_LOW16:
6071 case R_SH_GOTPLT_MEDLOW16:
6072 case R_SH_GOTPLT_MEDHI16:
6073 case R_SH_GOTPLT_HI16:
6074 case R_SH_GOTPLT10BY4:
6075 case R_SH_GOTPLT10BY8:
6076 #endif
6077 if (h != NULL)
6078 {
6079 struct elf_sh_link_hash_entry *eh;
6080 eh = (struct elf_sh_link_hash_entry *) h;
6081 if (eh->gotplt_refcount > 0)
6082 {
6083 eh->gotplt_refcount -= 1;
6084 if (h->plt.refcount > 0)
6085 h->plt.refcount -= 1;
6086 }
6087 #ifdef INCLUDE_SHMEDIA
6088 else if (seen_stt_datalabel)
6089 {
6090 if (eh->datalabel_got.refcount > 0)
6091 eh->datalabel_got.refcount -= 1;
6092 }
6093 #endif
6094 else if (h->got.refcount > 0)
6095 h->got.refcount -= 1;
6096 }
6097 else if (local_got_refcounts != NULL)
6098 {
6099 #ifdef INCLUDE_SHMEDIA
6100 if (rel->r_addend & 1)
6101 {
6102 if (local_got_refcounts[symtab_hdr->sh_info + r_symndx] > 0)
6103 local_got_refcounts[symtab_hdr->sh_info + r_symndx] -= 1;
6104 }
6105 else
6106 #endif
6107 if (local_got_refcounts[r_symndx] > 0)
6108 local_got_refcounts[r_symndx] -= 1;
6109 }
6110 break;
6111
6112 default:
6113 break;
6114 }
6115 }
6116
6117 return TRUE;
6118 }
6119
6120 /* Copy the extra info we tack onto an elf_link_hash_entry. */
6121
6122 static void
6123 sh_elf_copy_indirect_symbol (bed, dir, ind)
6124 struct elf_backend_data *bed;
6125 struct elf_link_hash_entry *dir, *ind;
6126 {
6127 struct elf_sh_link_hash_entry *edir, *eind;
6128 #ifdef INCLUDE_SHMEDIA
6129 bfd_signed_vma tmp;
6130 #endif
6131
6132 edir = (struct elf_sh_link_hash_entry *) dir;
6133 eind = (struct elf_sh_link_hash_entry *) ind;
6134
6135 if (eind->dyn_relocs != NULL)
6136 {
6137 if (edir->dyn_relocs != NULL)
6138 {
6139 struct elf_sh_dyn_relocs **pp;
6140 struct elf_sh_dyn_relocs *p;
6141
6142 BFD_ASSERT (ind->root.type != bfd_link_hash_indirect);
6143
6144 /* Add reloc counts against the weak sym to the strong sym
6145 list. Merge any entries against the same section. */
6146 for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
6147 {
6148 struct elf_sh_dyn_relocs *q;
6149
6150 for (q = edir->dyn_relocs; q != NULL; q = q->next)
6151 if (q->sec == p->sec)
6152 {
6153 q->pc_count += p->pc_count;
6154 q->count += p->count;
6155 *pp = p->next;
6156 break;
6157 }
6158 if (q == NULL)
6159 pp = &p->next;
6160 }
6161 *pp = edir->dyn_relocs;
6162 }
6163
6164 edir->dyn_relocs = eind->dyn_relocs;
6165 eind->dyn_relocs = NULL;
6166 }
6167 edir->gotplt_refcount = eind->gotplt_refcount;
6168 eind->gotplt_refcount = 0;
6169 #ifdef INCLUDE_SHMEDIA
6170 tmp = edir->datalabel_got.refcount;
6171 if (tmp < 1)
6172 {
6173 edir->datalabel_got.refcount = eind->datalabel_got.refcount;
6174 eind->datalabel_got.refcount = tmp;
6175 }
6176 else
6177 BFD_ASSERT (eind->datalabel_got.refcount < 1);
6178 #endif
6179
6180 if (ind->root.type == bfd_link_hash_indirect
6181 && dir->got.refcount <= 0)
6182 {
6183 edir->tls_type = eind->tls_type;
6184 eind->tls_type = GOT_UNKNOWN;
6185 }
6186
6187 _bfd_elf_link_hash_copy_indirect (bed, dir, ind);
6188 }
6189
6190 static int
6191 sh_elf_optimized_tls_reloc (info, r_type, is_local)
6192 struct bfd_link_info *info;
6193 int r_type;
6194 int is_local;
6195 {
6196 if (info->shared)
6197 return r_type;
6198
6199 switch (r_type)
6200 {
6201 case R_SH_TLS_GD_32:
6202 case R_SH_TLS_IE_32:
6203 if (is_local)
6204 return R_SH_TLS_LE_32;
6205 return R_SH_TLS_IE_32;
6206 case R_SH_TLS_LD_32:
6207 return R_SH_TLS_LE_32;
6208 }
6209
6210 return r_type;
6211 }
6212
6213 /* Look through the relocs for a section during the first phase.
6214 Since we don't do .gots or .plts, we just need to consider the
6215 virtual table relocs for gc. */
6216
6217 static bfd_boolean
6218 sh_elf_check_relocs (abfd, info, sec, relocs)
6219 bfd *abfd;
6220 struct bfd_link_info *info;
6221 asection *sec;
6222 const Elf_Internal_Rela *relocs;
6223 {
6224 Elf_Internal_Shdr *symtab_hdr;
6225 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
6226 struct elf_sh_link_hash_table *htab;
6227 const Elf_Internal_Rela *rel;
6228 const Elf_Internal_Rela *rel_end;
6229 bfd *dynobj;
6230 bfd_vma *local_got_offsets;
6231 asection *sgot;
6232 asection *srelgot;
6233 asection *sreloc;
6234 unsigned int r_type;
6235 int tls_type, old_tls_type;
6236
6237 sgot = NULL;
6238 srelgot = NULL;
6239 sreloc = NULL;
6240
6241 if (info->relocateable)
6242 return TRUE;
6243
6244 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
6245 sym_hashes = elf_sym_hashes (abfd);
6246 sym_hashes_end = sym_hashes + symtab_hdr->sh_size/sizeof (Elf32_External_Sym);
6247 if (!elf_bad_symtab (abfd))
6248 sym_hashes_end -= symtab_hdr->sh_info;
6249
6250 htab = sh_elf_hash_table (info);
6251 dynobj = htab->root.dynobj;
6252 local_got_offsets = elf_local_got_offsets (abfd);
6253
6254 rel_end = relocs + sec->reloc_count;
6255 for (rel = relocs; rel < rel_end; rel++)
6256 {
6257 struct elf_link_hash_entry *h;
6258 unsigned long r_symndx;
6259 #ifdef INCLUDE_SHMEDIA
6260 int seen_stt_datalabel = 0;
6261 #endif
6262
6263 r_symndx = ELF32_R_SYM (rel->r_info);
6264 r_type = ELF32_R_TYPE (rel->r_info);
6265
6266 if (r_symndx < symtab_hdr->sh_info)
6267 h = NULL;
6268 else
6269 {
6270 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
6271 #ifdef INCLUDE_SHMEDIA
6272 while (h->root.type == bfd_link_hash_indirect
6273 || h->root.type == bfd_link_hash_warning)
6274 {
6275 seen_stt_datalabel |= h->type == STT_DATALABEL;
6276 h = (struct elf_link_hash_entry *) h->root.u.i.link;
6277 }
6278 #endif
6279 }
6280
6281 r_type = sh_elf_optimized_tls_reloc (info, r_type, h == NULL);
6282 if (! info->shared
6283 && r_type == R_SH_TLS_IE_32
6284 && h != NULL
6285 && h->root.type != bfd_link_hash_undefined
6286 && h->root.type != bfd_link_hash_undefweak
6287 && (h->dynindx == -1
6288 || (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR)))
6289 r_type = R_SH_TLS_LE_32;
6290
6291 /* Some relocs require a global offset table. */
6292 if (htab->sgot == NULL)
6293 {
6294 switch (r_type)
6295 {
6296 case R_SH_GOTPLT32:
6297 case R_SH_GOT32:
6298 case R_SH_GOTOFF:
6299 case R_SH_GOTPC:
6300 #ifdef INCLUDE_SHMEDIA
6301 case R_SH_GOTPLT_LOW16:
6302 case R_SH_GOTPLT_MEDLOW16:
6303 case R_SH_GOTPLT_MEDHI16:
6304 case R_SH_GOTPLT_HI16:
6305 case R_SH_GOTPLT10BY4:
6306 case R_SH_GOTPLT10BY8:
6307 case R_SH_GOT_LOW16:
6308 case R_SH_GOT_MEDLOW16:
6309 case R_SH_GOT_MEDHI16:
6310 case R_SH_GOT_HI16:
6311 case R_SH_GOT10BY4:
6312 case R_SH_GOT10BY8:
6313 case R_SH_GOTOFF_LOW16:
6314 case R_SH_GOTOFF_MEDLOW16:
6315 case R_SH_GOTOFF_MEDHI16:
6316 case R_SH_GOTOFF_HI16:
6317 case R_SH_GOTPC_LOW16:
6318 case R_SH_GOTPC_MEDLOW16:
6319 case R_SH_GOTPC_MEDHI16:
6320 case R_SH_GOTPC_HI16:
6321 #endif
6322 case R_SH_TLS_GD_32:
6323 case R_SH_TLS_LD_32:
6324 case R_SH_TLS_IE_32:
6325 if (dynobj == NULL)
6326 htab->root.dynobj = dynobj = abfd;
6327 if (! create_got_section (dynobj, info))
6328 return FALSE;
6329 break;
6330
6331 default:
6332 break;
6333 }
6334 }
6335
6336 switch (r_type)
6337 {
6338 /* This relocation describes the C++ object vtable hierarchy.
6339 Reconstruct it for later use during GC. */
6340 case R_SH_GNU_VTINHERIT:
6341 if (!_bfd_elf32_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
6342 return FALSE;
6343 break;
6344
6345 /* This relocation describes which C++ vtable entries are actually
6346 used. Record for later use during GC. */
6347 case R_SH_GNU_VTENTRY:
6348 if (!_bfd_elf32_gc_record_vtentry (abfd, sec, h, rel->r_addend))
6349 return FALSE;
6350 break;
6351
6352 force_got:
6353 case R_SH_TLS_IE_32:
6354 if (info->shared)
6355 info->flags |= DF_STATIC_TLS;
6356
6357 /* FALLTHROUGH */
6358 case R_SH_TLS_GD_32:
6359 case R_SH_GOT32:
6360 #ifdef INCLUDE_SHMEDIA
6361 case R_SH_GOT_LOW16:
6362 case R_SH_GOT_MEDLOW16:
6363 case R_SH_GOT_MEDHI16:
6364 case R_SH_GOT_HI16:
6365 case R_SH_GOT10BY4:
6366 case R_SH_GOT10BY8:
6367 #endif
6368 switch (r_type)
6369 {
6370 default:
6371 tls_type = GOT_NORMAL;
6372 break;
6373 case R_SH_TLS_GD_32:
6374 tls_type = GOT_TLS_GD;
6375 break;
6376 case R_SH_TLS_IE_32:
6377 tls_type = GOT_TLS_IE;
6378 break;
6379 }
6380
6381 if (h != NULL)
6382 {
6383 #ifdef INCLUDE_SHMEDIA
6384 if (seen_stt_datalabel)
6385 {
6386 struct elf_sh_link_hash_entry *eh
6387 = (struct elf_sh_link_hash_entry *) h;
6388
6389 eh->datalabel_got.refcount += 1;
6390 }
6391 else
6392 #endif
6393 h->got.refcount += 1;
6394 old_tls_type = sh_elf_hash_entry (h)->tls_type;
6395 }
6396 else
6397 {
6398 bfd_signed_vma *local_got_refcounts;
6399
6400 /* This is a global offset table entry for a local
6401 symbol. */
6402 local_got_refcounts = elf_local_got_refcounts (abfd);
6403 if (local_got_refcounts == NULL)
6404 {
6405 bfd_size_type size;
6406
6407 size = symtab_hdr->sh_info;
6408 size *= sizeof (bfd_signed_vma);
6409 #ifdef INCLUDE_SHMEDIA
6410 /* Reserve space for both the datalabel and
6411 codelabel local GOT offsets. */
6412 size *= 2;
6413 #endif
6414 size += symtab_hdr->sh_info;
6415 local_got_refcounts = ((bfd_signed_vma *)
6416 bfd_zalloc (abfd, size));
6417 if (local_got_refcounts == NULL)
6418 return FALSE;
6419 elf_local_got_refcounts (abfd) = local_got_refcounts;
6420 #ifdef INCLUDE_SHMEDIA
6421 /* Take care of both the datalabel and codelabel local
6422 GOT offsets. */
6423 sh_elf_local_got_tls_type (abfd)
6424 = (char *) (local_got_refcounts + 2 * symtab_hdr->sh_info);
6425 #else
6426 sh_elf_local_got_tls_type (abfd)
6427 = (char *) (local_got_refcounts + symtab_hdr->sh_info);
6428 #endif
6429 }
6430 #ifdef INCLUDE_SHMEDIA
6431 if (rel->r_addend & 1)
6432 local_got_refcounts[symtab_hdr->sh_info + r_symndx] += 1;
6433 else
6434 #endif
6435 local_got_refcounts[r_symndx] += 1;
6436 old_tls_type = sh_elf_local_got_tls_type (abfd) [r_symndx];
6437 }
6438
6439 /* If a TLS symbol is accessed using IE at least once,
6440 there is no point to use dynamic model for it. */
6441 if (old_tls_type != tls_type && old_tls_type != GOT_UNKNOWN
6442 && (old_tls_type != GOT_TLS_GD || tls_type != GOT_TLS_IE))
6443 {
6444 if (old_tls_type == GOT_TLS_IE && tls_type == GOT_TLS_GD)
6445 tls_type = GOT_TLS_IE;
6446 else
6447 {
6448 (*_bfd_error_handler)
6449 (_("%s: `%s' accessed both as normal and thread local symbol"),
6450 bfd_archive_filename (abfd), h->root.root.string);
6451 return FALSE;
6452 }
6453 }
6454
6455 if (old_tls_type != tls_type)
6456 {
6457 if (h != NULL)
6458 sh_elf_hash_entry (h)->tls_type = tls_type;
6459 else
6460 sh_elf_local_got_tls_type (abfd) [r_symndx] = tls_type;
6461 }
6462
6463 break;
6464
6465 case R_SH_TLS_LD_32:
6466 sh_elf_hash_table(info)->tls_ldm_got.refcount += 1;
6467 break;
6468
6469 case R_SH_GOTPLT32:
6470 #ifdef INCLUDE_SHMEDIA
6471 case R_SH_GOTPLT_LOW16:
6472 case R_SH_GOTPLT_MEDLOW16:
6473 case R_SH_GOTPLT_MEDHI16:
6474 case R_SH_GOTPLT_HI16:
6475 case R_SH_GOTPLT10BY4:
6476 case R_SH_GOTPLT10BY8:
6477 #endif
6478 /* If this is a local symbol, we resolve it directly without
6479 creating a procedure linkage table entry. */
6480
6481 if (h == NULL
6482 || (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL)
6483 || ! info->shared
6484 || info->symbolic
6485 || h->dynindx == -1)
6486 goto force_got;
6487
6488 h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_PLT;
6489 h->plt.refcount += 1;
6490 ((struct elf_sh_link_hash_entry *) h)->gotplt_refcount += 1;
6491
6492 break;
6493
6494 case R_SH_PLT32:
6495 #ifdef INCLUDE_SHMEDIA
6496 case R_SH_PLT_LOW16:
6497 case R_SH_PLT_MEDLOW16:
6498 case R_SH_PLT_MEDHI16:
6499 case R_SH_PLT_HI16:
6500 #endif
6501 /* This symbol requires a procedure linkage table entry. We
6502 actually build the entry in adjust_dynamic_symbol,
6503 because this might be a case of linking PIC code which is
6504 never referenced by a dynamic object, in which case we
6505 don't need to generate a procedure linkage table entry
6506 after all. */
6507
6508 /* If this is a local symbol, we resolve it directly without
6509 creating a procedure linkage table entry. */
6510 if (h == NULL)
6511 continue;
6512
6513 if (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL)
6514 break;
6515
6516 h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_PLT;
6517 h->plt.refcount += 1;
6518 break;
6519
6520 case R_SH_DIR32:
6521 case R_SH_REL32:
6522 if (h != NULL && ! info->shared)
6523 {
6524 h->elf_link_hash_flags |= ELF_LINK_NON_GOT_REF;
6525 h->plt.refcount += 1;
6526 }
6527
6528 /* If we are creating a shared library, and this is a reloc
6529 against a global symbol, or a non PC relative reloc
6530 against a local symbol, then we need to copy the reloc
6531 into the shared library. However, if we are linking with
6532 -Bsymbolic, we do not need to copy a reloc against a
6533 global symbol which is defined in an object we are
6534 including in the link (i.e., DEF_REGULAR is set). At
6535 this point we have not seen all the input files, so it is
6536 possible that DEF_REGULAR is not set now but will be set
6537 later (it is never cleared). We account for that
6538 possibility below by storing information in the
6539 dyn_relocs field of the hash table entry. A similar
6540 situation occurs when creating shared libraries and symbol
6541 visibility changes render the symbol local.
6542
6543 If on the other hand, we are creating an executable, we
6544 may need to keep relocations for symbols satisfied by a
6545 dynamic library if we manage to avoid copy relocs for the
6546 symbol. */
6547 if ((info->shared
6548 && (sec->flags & SEC_ALLOC) != 0
6549 && (r_type != R_SH_REL32
6550 || (h != NULL
6551 && (! info->symbolic
6552 || h->root.type == bfd_link_hash_defweak
6553 || (h->elf_link_hash_flags
6554 & ELF_LINK_HASH_DEF_REGULAR) == 0))))
6555 || (! info->shared
6556 && (sec->flags & SEC_ALLOC) != 0
6557 && h != NULL
6558 && (h->root.type == bfd_link_hash_defweak
6559 || (h->elf_link_hash_flags
6560 & ELF_LINK_HASH_DEF_REGULAR) == 0)))
6561 {
6562 struct elf_sh_dyn_relocs *p;
6563 struct elf_sh_dyn_relocs **head;
6564
6565 if (dynobj == NULL)
6566 htab->root.dynobj = dynobj = abfd;
6567
6568 /* When creating a shared object, we must copy these
6569 reloc types into the output file. We create a reloc
6570 section in dynobj and make room for this reloc. */
6571 if (sreloc == NULL)
6572 {
6573 const char *name;
6574
6575 name = (bfd_elf_string_from_elf_section
6576 (abfd,
6577 elf_elfheader (abfd)->e_shstrndx,
6578 elf_section_data (sec)->rel_hdr.sh_name));
6579 if (name == NULL)
6580 return FALSE;
6581
6582 BFD_ASSERT (strncmp (name, ".rela", 5) == 0
6583 && strcmp (bfd_get_section_name (abfd, sec),
6584 name + 5) == 0);
6585
6586 sreloc = bfd_get_section_by_name (dynobj, name);
6587 if (sreloc == NULL)
6588 {
6589 flagword flags;
6590
6591 sreloc = bfd_make_section (dynobj, name);
6592 flags = (SEC_HAS_CONTENTS | SEC_READONLY
6593 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
6594 if ((sec->flags & SEC_ALLOC) != 0)
6595 flags |= SEC_ALLOC | SEC_LOAD;
6596 if (sreloc == NULL
6597 || ! bfd_set_section_flags (dynobj, sreloc, flags)
6598 || ! bfd_set_section_alignment (dynobj, sreloc, 2))
6599 return FALSE;
6600 }
6601 if (sec->flags & SEC_READONLY)
6602 info->flags |= DF_TEXTREL;
6603 elf_section_data (sec)->sreloc = sreloc;
6604 }
6605
6606 /* If this is a global symbol, we count the number of
6607 relocations we need for this symbol. */
6608 if (h != NULL)
6609 head = &((struct elf_sh_link_hash_entry *) h)->dyn_relocs;
6610 else
6611 {
6612 asection *s;
6613
6614 /* Track dynamic relocs needed for local syms too. */
6615 s = bfd_section_from_r_symndx (abfd, &htab->sym_sec,
6616 sec, r_symndx);
6617 if (s == NULL)
6618 return FALSE;
6619
6620 head = ((struct elf_sh_dyn_relocs **)
6621 &elf_section_data (s)->local_dynrel);
6622 }
6623
6624 p = *head;
6625 if (p == NULL || p->sec != sec)
6626 {
6627 bfd_size_type amt = sizeof (*p);
6628 p = ((struct elf_sh_dyn_relocs *) bfd_alloc (dynobj, amt));
6629 if (p == NULL)
6630 return FALSE;
6631 p->next = *head;
6632 *head = p;
6633 p->sec = sec;
6634 p->count = 0;
6635 p->pc_count = 0;
6636 p->tls_tpoff32 = FALSE;
6637 }
6638
6639 p->count += 1;
6640 if (r_type == R_SH_REL32)
6641 p->pc_count += 1;
6642 }
6643
6644 break;
6645
6646 case R_SH_TLS_LE_32:
6647 if (info->shared)
6648 {
6649 (*_bfd_error_handler) (_("%s: TLS local exec code cannot be linked into shared objects"),
6650 bfd_archive_filename (abfd));
6651 return FALSE;
6652 }
6653
6654 if (ELF32_R_TYPE (rel->r_info) == R_SH_TLS_LD_32)
6655 break;
6656
6657 /* FALLTHROUGH */
6658 case R_SH_TLS_LDO_32:
6659 /* We make a R_SH_TLS_TPOFF32 relocation. Count it as a
6660 copy relocation. */
6661 if (! info->shared)
6662 {
6663 struct elf_sh_dyn_relocs *p;
6664 struct elf_sh_dyn_relocs **head;
6665
6666 if (dynobj == NULL)
6667 htab->root.dynobj = dynobj = abfd;
6668
6669 if (sreloc == NULL)
6670 {
6671 const char *name;
6672
6673 name = (bfd_elf_string_from_elf_section
6674 (abfd,
6675 elf_elfheader (abfd)->e_shstrndx,
6676 elf_section_data (sec)->rel_hdr.sh_name));
6677 if (name == NULL)
6678 return FALSE;
6679
6680 BFD_ASSERT (strncmp (name, ".rela", 5) == 0
6681 && strcmp (bfd_get_section_name (abfd, sec),
6682 name + 5) == 0);
6683
6684 sreloc = bfd_get_section_by_name (dynobj, name);
6685 if (sreloc == NULL)
6686 {
6687 flagword flags;
6688
6689 sreloc = bfd_make_section (dynobj, name);
6690 flags = (SEC_HAS_CONTENTS | SEC_READONLY
6691 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
6692 if ((sec->flags & SEC_ALLOC) != 0)
6693 flags |= SEC_ALLOC | SEC_LOAD;
6694 if (sreloc == NULL
6695 || ! bfd_set_section_flags (dynobj, sreloc, flags)
6696 || ! bfd_set_section_alignment (dynobj, sreloc, 2))
6697 return FALSE;
6698 }
6699 elf_section_data (sec)->sreloc = sreloc;
6700 if (sec->flags & SEC_READONLY)
6701 info->flags |= DF_TEXTREL;
6702 }
6703
6704 /* If this is a global symbol, we count the number of
6705 relocations we need for this symbol. */
6706 if (h != NULL)
6707 head = &((struct elf_sh_link_hash_entry *) h)->dyn_relocs;
6708 else
6709 {
6710 asection *s;
6711
6712 /* Track dynamic relocs needed for local syms too. */
6713 s = bfd_section_from_r_symndx (abfd, &htab->sym_sec,
6714 sec, r_symndx);
6715 if (s == NULL)
6716 return FALSE;
6717
6718 head = ((struct elf_sh_dyn_relocs **)
6719 &elf_section_data (s)->local_dynrel);
6720 }
6721
6722 p = *head;
6723 if (p == NULL || p->sec != sec)
6724 {
6725 bfd_size_type amt = sizeof (*p);
6726 p = ((struct elf_sh_dyn_relocs *) bfd_alloc (dynobj, amt));
6727 if (p == NULL)
6728 return FALSE;
6729 p->next = *head;
6730 *head = p;
6731 p->sec = sec;
6732 p->count = 0;
6733 p->pc_count = 0;
6734 p->tls_tpoff32 = FALSE;
6735 }
6736
6737 p->count += 1;
6738 p->tls_tpoff32 = TRUE;
6739 }
6740 break;
6741
6742 default:
6743 break;
6744 }
6745 }
6746
6747 return TRUE;
6748 }
6749
6750 #ifndef sh_elf_set_mach_from_flags
6751 static bfd_boolean
6752 sh_elf_set_mach_from_flags (abfd)
6753 bfd *abfd;
6754 {
6755 flagword flags = elf_elfheader (abfd)->e_flags;
6756
6757 switch (flags & EF_SH_MACH_MASK)
6758 {
6759 case EF_SH1:
6760 bfd_default_set_arch_mach (abfd, bfd_arch_sh, bfd_mach_sh);
6761 break;
6762 case EF_SH2:
6763 bfd_default_set_arch_mach (abfd, bfd_arch_sh, bfd_mach_sh2);
6764 break;
6765 case EF_SH2E:
6766 bfd_default_set_arch_mach (abfd, bfd_arch_sh, bfd_mach_sh2e);
6767 break;
6768 case EF_SH_DSP:
6769 bfd_default_set_arch_mach (abfd, bfd_arch_sh, bfd_mach_sh_dsp);
6770 break;
6771 case EF_SH3:
6772 bfd_default_set_arch_mach (abfd, bfd_arch_sh, bfd_mach_sh3);
6773 break;
6774 case EF_SH3_DSP:
6775 bfd_default_set_arch_mach (abfd, bfd_arch_sh, bfd_mach_sh3_dsp);
6776 break;
6777 case EF_SH3E:
6778 bfd_default_set_arch_mach (abfd, bfd_arch_sh, bfd_mach_sh3e);
6779 break;
6780 case EF_SH_UNKNOWN:
6781 case EF_SH4:
6782 bfd_default_set_arch_mach (abfd, bfd_arch_sh, bfd_mach_sh4);
6783 break;
6784 default:
6785 return FALSE;
6786 }
6787 return TRUE;
6788 }
6789 #endif /* not sh_elf_set_mach_from_flags */
6790
6791 #ifndef sh_elf_set_private_flags
6792 /* Function to keep SH specific file flags. */
6793
6794 static bfd_boolean
6795 sh_elf_set_private_flags (abfd, flags)
6796 bfd *abfd;
6797 flagword flags;
6798 {
6799 BFD_ASSERT (! elf_flags_init (abfd)
6800 || elf_elfheader (abfd)->e_flags == flags);
6801
6802 elf_elfheader (abfd)->e_flags = flags;
6803 elf_flags_init (abfd) = TRUE;
6804 return sh_elf_set_mach_from_flags (abfd);
6805 }
6806 #endif /* not sh_elf_set_private_flags */
6807
6808 #ifndef sh_elf_copy_private_data
6809 /* Copy backend specific data from one object module to another */
6810
6811 static bfd_boolean
6812 sh_elf_copy_private_data (ibfd, obfd)
6813 bfd * ibfd;
6814 bfd * obfd;
6815 {
6816 if ( bfd_get_flavour (ibfd) != bfd_target_elf_flavour
6817 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
6818 return TRUE;
6819
6820 return sh_elf_set_private_flags (obfd, elf_elfheader (ibfd)->e_flags);
6821 }
6822 #endif /* not sh_elf_copy_private_data */
6823
6824 #ifndef sh_elf_merge_private_data
6825 /* This routine checks for linking big and little endian objects
6826 together, and for linking sh-dsp with sh3e / sh4 objects. */
6827
6828 static bfd_boolean
6829 sh_elf_merge_private_data (ibfd, obfd)
6830 bfd *ibfd;
6831 bfd *obfd;
6832 {
6833 flagword old_flags, new_flags;
6834
6835 if (! _bfd_generic_verify_endian_match (ibfd, obfd))
6836 return FALSE;
6837
6838 if ( bfd_get_flavour (ibfd) != bfd_target_elf_flavour
6839 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
6840 return TRUE;
6841
6842 if (! elf_flags_init (obfd))
6843 {
6844 /* This happens when ld starts out with a 'blank' output file. */
6845 elf_flags_init (obfd) = TRUE;
6846 elf_elfheader (obfd)->e_flags = EF_SH1;
6847 }
6848 old_flags = elf_elfheader (obfd)->e_flags;
6849 new_flags = elf_elfheader (ibfd)->e_flags;
6850 if ((EF_SH_HAS_DSP (old_flags) && EF_SH_HAS_FP (new_flags))
6851 || (EF_SH_HAS_DSP (new_flags) && EF_SH_HAS_FP (old_flags)))
6852 {
6853 (*_bfd_error_handler)
6854 ("%s: uses %s instructions while previous modules use %s instructions",
6855 bfd_archive_filename (ibfd),
6856 EF_SH_HAS_DSP (new_flags) ? "dsp" : "floating point",
6857 EF_SH_HAS_DSP (new_flags) ? "floating point" : "dsp");
6858 bfd_set_error (bfd_error_bad_value);
6859 return FALSE;
6860 }
6861 elf_elfheader (obfd)->e_flags = EF_SH_MERGE_MACH (old_flags, new_flags);
6862
6863 return sh_elf_set_mach_from_flags (obfd);
6864 }
6865 #endif /* not sh_elf_merge_private_data */
6866
6867 /* Override the generic function because we need to store sh_elf_obj_tdata
6868 as the specific tdata. We set also the machine architecture from flags
6869 here. */
6870
6871 static bfd_boolean
6872 sh_elf_object_p (abfd)
6873 bfd *abfd;
6874 {
6875 struct sh_elf_obj_tdata *new_tdata;
6876 bfd_size_type amt = sizeof (struct sh_elf_obj_tdata);
6877
6878 if (!sh_elf_set_mach_from_flags (abfd))
6879 return FALSE;
6880
6881 /* Allocate our special target data. */
6882 new_tdata = bfd_zalloc (abfd, amt);
6883 if (new_tdata == NULL)
6884 return FALSE;
6885 new_tdata->root = *abfd->tdata.elf_obj_data;
6886 abfd->tdata.any = new_tdata;
6887 return TRUE;
6888 }
6889
6890 /* Finish up dynamic symbol handling. We set the contents of various
6891 dynamic sections here. */
6892
6893 static bfd_boolean
6894 sh_elf_finish_dynamic_symbol (output_bfd, info, h, sym)
6895 bfd *output_bfd;
6896 struct bfd_link_info *info;
6897 struct elf_link_hash_entry *h;
6898 Elf_Internal_Sym *sym;
6899 {
6900 struct elf_sh_link_hash_table *htab;
6901 bfd *dynobj;
6902
6903 htab = sh_elf_hash_table (info);
6904 dynobj = htab->root.dynobj;
6905
6906 if (h->plt.offset != (bfd_vma) -1)
6907 {
6908 asection *splt;
6909 asection *sgot;
6910 asection *srel;
6911
6912 bfd_vma plt_index;
6913 bfd_vma got_offset;
6914 Elf_Internal_Rela rel;
6915 bfd_byte *loc;
6916
6917 /* This symbol has an entry in the procedure linkage table. Set
6918 it up. */
6919
6920 BFD_ASSERT (h->dynindx != -1);
6921
6922 splt = htab->splt;
6923 sgot = htab->sgotplt;
6924 srel = htab->srelplt;
6925 BFD_ASSERT (splt != NULL && sgot != NULL && srel != NULL);
6926
6927 /* Get the index in the procedure linkage table which
6928 corresponds to this symbol. This is the index of this symbol
6929 in all the symbols for which we are making plt entries. The
6930 first entry in the procedure linkage table is reserved. */
6931 plt_index = h->plt.offset / elf_sh_sizeof_plt (info) - 1;
6932
6933 /* Get the offset into the .got table of the entry that
6934 corresponds to this function. Each .got entry is 4 bytes.
6935 The first three are reserved. */
6936 got_offset = (plt_index + 3) * 4;
6937
6938 #ifdef GOT_BIAS
6939 if (info->shared)
6940 got_offset -= GOT_BIAS;
6941 #endif
6942
6943 /* Fill in the entry in the procedure linkage table. */
6944 if (! info->shared)
6945 {
6946 if (elf_sh_plt_entry == NULL)
6947 {
6948 elf_sh_plt_entry = (bfd_big_endian (output_bfd) ?
6949 elf_sh_plt_entry_be : elf_sh_plt_entry_le);
6950 }
6951 memcpy (splt->contents + h->plt.offset, elf_sh_plt_entry,
6952 elf_sh_sizeof_plt (info));
6953 #ifdef INCLUDE_SHMEDIA
6954 movi_shori_putval (output_bfd,
6955 (sgot->output_section->vma
6956 + sgot->output_offset
6957 + got_offset),
6958 (splt->contents + h->plt.offset
6959 + elf_sh_plt_symbol_offset (info)));
6960
6961 /* Set bottom bit because its for a branch to SHmedia */
6962 movi_shori_putval (output_bfd,
6963 (splt->output_section->vma + splt->output_offset)
6964 | 1,
6965 (splt->contents + h->plt.offset
6966 + elf_sh_plt_plt0_offset (info)));
6967 #else
6968 bfd_put_32 (output_bfd,
6969 (sgot->output_section->vma
6970 + sgot->output_offset
6971 + got_offset),
6972 (splt->contents + h->plt.offset
6973 + elf_sh_plt_symbol_offset (info)));
6974
6975 bfd_put_32 (output_bfd,
6976 (splt->output_section->vma + splt->output_offset),
6977 (splt->contents + h->plt.offset
6978 + elf_sh_plt_plt0_offset (info)));
6979 #endif
6980 }
6981 else
6982 {
6983 if (elf_sh_pic_plt_entry == NULL)
6984 {
6985 elf_sh_pic_plt_entry = (bfd_big_endian (output_bfd) ?
6986 elf_sh_pic_plt_entry_be :
6987 elf_sh_pic_plt_entry_le);
6988 }
6989 memcpy (splt->contents + h->plt.offset, elf_sh_pic_plt_entry,
6990 elf_sh_sizeof_plt (info));
6991 #ifdef INCLUDE_SHMEDIA
6992 movi_shori_putval (output_bfd, got_offset,
6993 (splt->contents + h->plt.offset
6994 + elf_sh_plt_symbol_offset (info)));
6995 #else
6996 bfd_put_32 (output_bfd, got_offset,
6997 (splt->contents + h->plt.offset
6998 + elf_sh_plt_symbol_offset (info)));
6999 #endif
7000 }
7001
7002 #ifdef GOT_BIAS
7003 if (info->shared)
7004 got_offset += GOT_BIAS;
7005 #endif
7006
7007 #ifdef INCLUDE_SHMEDIA
7008 movi_shori_putval (output_bfd,
7009 plt_index * sizeof (Elf32_External_Rela),
7010 (splt->contents + h->plt.offset
7011 + elf_sh_plt_reloc_offset (info)));
7012 #else
7013 bfd_put_32 (output_bfd, plt_index * sizeof (Elf32_External_Rela),
7014 (splt->contents + h->plt.offset
7015 + elf_sh_plt_reloc_offset (info)));
7016 #endif
7017
7018 /* Fill in the entry in the global offset table. */
7019 bfd_put_32 (output_bfd,
7020 (splt->output_section->vma
7021 + splt->output_offset
7022 + h->plt.offset
7023 + elf_sh_plt_temp_offset (info)),
7024 sgot->contents + got_offset);
7025
7026 /* Fill in the entry in the .rela.plt section. */
7027 rel.r_offset = (sgot->output_section->vma
7028 + sgot->output_offset
7029 + got_offset);
7030 rel.r_info = ELF32_R_INFO (h->dynindx, R_SH_JMP_SLOT);
7031 rel.r_addend = 0;
7032 #ifdef GOT_BIAS
7033 rel.r_addend = GOT_BIAS;
7034 #endif
7035 loc = srel->contents + plt_index * sizeof (Elf32_External_Rela);
7036 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
7037
7038 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
7039 {
7040 /* Mark the symbol as undefined, rather than as defined in
7041 the .plt section. Leave the value alone. */
7042 sym->st_shndx = SHN_UNDEF;
7043 }
7044 }
7045
7046 if (h->got.offset != (bfd_vma) -1
7047 && sh_elf_hash_entry (h)->tls_type != GOT_TLS_GD
7048 && sh_elf_hash_entry (h)->tls_type != GOT_TLS_IE)
7049 {
7050 asection *sgot;
7051 asection *srel;
7052 Elf_Internal_Rela rel;
7053 bfd_byte *loc;
7054
7055 /* This symbol has an entry in the global offset table. Set it
7056 up. */
7057
7058 sgot = htab->sgot;
7059 srel = htab->srelgot;
7060 BFD_ASSERT (sgot != NULL && srel != NULL);
7061
7062 rel.r_offset = (sgot->output_section->vma
7063 + sgot->output_offset
7064 + (h->got.offset &~ (bfd_vma) 1));
7065
7066 /* If this is a static link, or it is a -Bsymbolic link and the
7067 symbol is defined locally or was forced to be local because
7068 of a version file, we just want to emit a RELATIVE reloc.
7069 The entry in the global offset table will already have been
7070 initialized in the relocate_section function. */
7071 if (info->shared
7072 && (info->symbolic
7073 || h->dynindx == -1
7074 || (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL))
7075 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR))
7076 {
7077 rel.r_info = ELF32_R_INFO (0, R_SH_RELATIVE);
7078 rel.r_addend = (h->root.u.def.value
7079 + h->root.u.def.section->output_section->vma
7080 + h->root.u.def.section->output_offset);
7081 }
7082 else
7083 {
7084 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + h->got.offset);
7085 rel.r_info = ELF32_R_INFO (h->dynindx, R_SH_GLOB_DAT);
7086 rel.r_addend = 0;
7087 }
7088
7089 loc = srel->contents;
7090 loc += srel->reloc_count++ * sizeof (Elf32_External_Rela);
7091 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
7092 }
7093
7094 #ifdef INCLUDE_SHMEDIA
7095 {
7096 struct elf_sh_link_hash_entry *eh;
7097
7098 eh = (struct elf_sh_link_hash_entry *) h;
7099 if (eh->datalabel_got.offset != (bfd_vma) -1)
7100 {
7101 asection *sgot;
7102 asection *srel;
7103 Elf_Internal_Rela rel;
7104 bfd_byte *loc;
7105
7106 /* This symbol has a datalabel entry in the global offset table.
7107 Set it up. */
7108
7109 sgot = htab->sgot;
7110 srel = htab->srelgot;
7111 BFD_ASSERT (sgot != NULL && srel != NULL);
7112
7113 rel.r_offset = (sgot->output_section->vma
7114 + sgot->output_offset
7115 + (eh->datalabel_got.offset &~ (bfd_vma) 1));
7116
7117 /* If this is a static link, or it is a -Bsymbolic link and the
7118 symbol is defined locally or was forced to be local because
7119 of a version file, we just want to emit a RELATIVE reloc.
7120 The entry in the global offset table will already have been
7121 initialized in the relocate_section function. */
7122 if (info->shared
7123 && (info->symbolic
7124 || h->dynindx == -1
7125 || (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL))
7126 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR))
7127 {
7128 rel.r_info = ELF32_R_INFO (0, R_SH_RELATIVE);
7129 rel.r_addend = (h->root.u.def.value
7130 + h->root.u.def.section->output_section->vma
7131 + h->root.u.def.section->output_offset);
7132 }
7133 else
7134 {
7135 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents
7136 + eh->datalabel_got.offset);
7137 rel.r_info = ELF32_R_INFO (h->dynindx, R_SH_GLOB_DAT);
7138 rel.r_addend = 0;
7139 }
7140
7141 loc = srel->contents;
7142 loc += srel->reloc_count++ * sizeof (Elf32_External_Rela);
7143 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
7144 }
7145 }
7146 #endif
7147
7148 if ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_COPY) != 0)
7149 {
7150 asection *s;
7151 Elf_Internal_Rela rel;
7152 bfd_byte *loc;
7153
7154 /* This symbol needs a copy reloc. Set it up. */
7155
7156 BFD_ASSERT (h->dynindx != -1
7157 && (h->root.type == bfd_link_hash_defined
7158 || h->root.type == bfd_link_hash_defweak));
7159
7160 s = bfd_get_section_by_name (h->root.u.def.section->owner,
7161 ".rela.bss");
7162 BFD_ASSERT (s != NULL);
7163
7164 rel.r_offset = (h->root.u.def.value
7165 + h->root.u.def.section->output_section->vma
7166 + h->root.u.def.section->output_offset);
7167 rel.r_info = ELF32_R_INFO (h->dynindx, R_SH_COPY);
7168 rel.r_addend = 0;
7169 loc = s->contents + s->reloc_count++ * sizeof (Elf32_External_Rela);
7170 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
7171 }
7172
7173 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. */
7174 if (strcmp (h->root.root.string, "_DYNAMIC") == 0
7175 || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
7176 sym->st_shndx = SHN_ABS;
7177
7178 return TRUE;
7179 }
7180
7181 /* Finish up the dynamic sections. */
7182
7183 static bfd_boolean
7184 sh_elf_finish_dynamic_sections (output_bfd, info)
7185 bfd *output_bfd;
7186 struct bfd_link_info *info;
7187 {
7188 struct elf_sh_link_hash_table *htab;
7189 bfd *dynobj;
7190 asection *sgot;
7191 asection *sdyn;
7192
7193 htab = sh_elf_hash_table (info);
7194 dynobj = htab->root.dynobj;
7195
7196 sgot = htab->sgotplt;
7197 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
7198
7199 if (htab->root.dynamic_sections_created)
7200 {
7201 asection *splt;
7202 Elf32_External_Dyn *dyncon, *dynconend;
7203
7204 BFD_ASSERT (sgot != NULL && sdyn != NULL);
7205
7206 dyncon = (Elf32_External_Dyn *) sdyn->contents;
7207 dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->_raw_size);
7208 for (; dyncon < dynconend; dyncon++)
7209 {
7210 Elf_Internal_Dyn dyn;
7211 asection *s;
7212 #ifdef INCLUDE_SHMEDIA
7213 const char *name;
7214 #endif
7215
7216 bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
7217
7218 switch (dyn.d_tag)
7219 {
7220 default:
7221 break;
7222
7223 #ifdef INCLUDE_SHMEDIA
7224 case DT_INIT:
7225 name = info->init_function;
7226 goto get_sym;
7227
7228 case DT_FINI:
7229 name = info->fini_function;
7230 get_sym:
7231 if (dyn.d_un.d_val != 0)
7232 {
7233 struct elf_link_hash_entry *h;
7234
7235 h = elf_link_hash_lookup (&htab->root, name,
7236 FALSE, FALSE, TRUE);
7237 if (h != NULL && (h->other & STO_SH5_ISA32))
7238 {
7239 dyn.d_un.d_val |= 1;
7240 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
7241 }
7242 }
7243 break;
7244 #endif
7245
7246 case DT_PLTGOT:
7247 s = htab->sgot->output_section;
7248 goto get_vma;
7249
7250 case DT_JMPREL:
7251 s = htab->srelplt->output_section;
7252 get_vma:
7253 BFD_ASSERT (s != NULL);
7254 dyn.d_un.d_ptr = s->vma;
7255 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
7256 break;
7257
7258 case DT_PLTRELSZ:
7259 s = htab->srelplt->output_section;
7260 BFD_ASSERT (s != NULL);
7261 if (s->_cooked_size != 0)
7262 dyn.d_un.d_val = s->_cooked_size;
7263 else
7264 dyn.d_un.d_val = s->_raw_size;
7265 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
7266 break;
7267
7268 case DT_RELASZ:
7269 /* My reading of the SVR4 ABI indicates that the
7270 procedure linkage table relocs (DT_JMPREL) should be
7271 included in the overall relocs (DT_RELA). This is
7272 what Solaris does. However, UnixWare can not handle
7273 that case. Therefore, we override the DT_RELASZ entry
7274 here to make it not include the JMPREL relocs. Since
7275 the linker script arranges for .rela.plt to follow all
7276 other relocation sections, we don't have to worry
7277 about changing the DT_RELA entry. */
7278 if (htab->srelplt != NULL)
7279 {
7280 s = htab->srelplt->output_section;
7281 if (s->_cooked_size != 0)
7282 dyn.d_un.d_val -= s->_cooked_size;
7283 else
7284 dyn.d_un.d_val -= s->_raw_size;
7285 }
7286 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
7287 break;
7288 }
7289 }
7290
7291 /* Fill in the first entry in the procedure linkage table. */
7292 splt = htab->splt;
7293 if (splt && splt->_raw_size > 0)
7294 {
7295 if (info->shared)
7296 {
7297 if (elf_sh_pic_plt_entry == NULL)
7298 {
7299 elf_sh_pic_plt_entry = (bfd_big_endian (output_bfd) ?
7300 elf_sh_pic_plt_entry_be :
7301 elf_sh_pic_plt_entry_le);
7302 }
7303 memcpy (splt->contents, elf_sh_pic_plt_entry,
7304 elf_sh_sizeof_plt (info));
7305 }
7306 else
7307 {
7308 if (elf_sh_plt0_entry == NULL)
7309 {
7310 elf_sh_plt0_entry = (bfd_big_endian (output_bfd) ?
7311 elf_sh_plt0_entry_be :
7312 elf_sh_plt0_entry_le);
7313 }
7314 memcpy (splt->contents, elf_sh_plt0_entry, PLT_ENTRY_SIZE);
7315 #ifdef INCLUDE_SHMEDIA
7316 movi_shori_putval (output_bfd,
7317 sgot->output_section->vma
7318 + sgot->output_offset,
7319 splt->contents
7320 + elf_sh_plt0_gotplt_offset (info));
7321 #else
7322 bfd_put_32 (output_bfd,
7323 sgot->output_section->vma + sgot->output_offset + 4,
7324 splt->contents + elf_sh_plt0_gotid_offset (info));
7325 bfd_put_32 (output_bfd,
7326 sgot->output_section->vma + sgot->output_offset + 8,
7327 splt->contents + elf_sh_plt0_linker_offset (info));
7328 #endif
7329 }
7330
7331 /* UnixWare sets the entsize of .plt to 4, although that doesn't
7332 really seem like the right value. */
7333 elf_section_data (splt->output_section)->this_hdr.sh_entsize = 4;
7334 }
7335 }
7336
7337 /* Fill in the first three entries in the global offset table. */
7338 if (sgot && sgot->_raw_size > 0)
7339 {
7340 if (sdyn == NULL)
7341 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents);
7342 else
7343 bfd_put_32 (output_bfd,
7344 sdyn->output_section->vma + sdyn->output_offset,
7345 sgot->contents);
7346 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 4);
7347 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 8);
7348
7349 elf_section_data (sgot->output_section)->this_hdr.sh_entsize = 4;
7350 }
7351
7352 return TRUE;
7353 }
7354
7355 static enum elf_reloc_type_class
7356 sh_elf_reloc_type_class (rela)
7357 const Elf_Internal_Rela *rela;
7358 {
7359 switch ((int) ELF32_R_TYPE (rela->r_info))
7360 {
7361 case R_SH_RELATIVE:
7362 return reloc_class_relative;
7363 case R_SH_JMP_SLOT:
7364 return reloc_class_plt;
7365 case R_SH_COPY:
7366 return reloc_class_copy;
7367 default:
7368 return reloc_class_normal;
7369 }
7370 }
7371
7372 /* Support for Linux core dump NOTE sections. */
7373 static bfd_boolean
7374 elf32_shlin_grok_prstatus (abfd, note)
7375 bfd *abfd;
7376 Elf_Internal_Note *note;
7377 {
7378 int offset;
7379 unsigned int raw_size;
7380
7381 switch (note->descsz)
7382 {
7383 default:
7384 return FALSE;
7385
7386 case 168: /* Linux/SH */
7387 /* pr_cursig */
7388 elf_tdata (abfd)->core_signal = bfd_get_16 (abfd, note->descdata + 12);
7389
7390 /* pr_pid */
7391 elf_tdata (abfd)->core_pid = bfd_get_32 (abfd, note->descdata + 24);
7392
7393 /* pr_reg */
7394 offset = 72;
7395 raw_size = 92;
7396
7397 break;
7398 }
7399
7400 /* Make a ".reg/999" section. */
7401 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
7402 raw_size, note->descpos + offset);
7403 }
7404
7405 static bfd_boolean
7406 elf32_shlin_grok_psinfo (abfd, note)
7407 bfd *abfd;
7408 Elf_Internal_Note *note;
7409 {
7410 switch (note->descsz)
7411 {
7412 default:
7413 return FALSE;
7414
7415 case 124: /* Linux/SH elf_prpsinfo */
7416 elf_tdata (abfd)->core_program
7417 = _bfd_elfcore_strndup (abfd, note->descdata + 28, 16);
7418 elf_tdata (abfd)->core_command
7419 = _bfd_elfcore_strndup (abfd, note->descdata + 44, 80);
7420 }
7421
7422 /* Note that for some reason, a spurious space is tacked
7423 onto the end of the args in some (at least one anyway)
7424 implementations, so strip it off if it exists. */
7425
7426 {
7427 char *command = elf_tdata (abfd)->core_command;
7428 int n = strlen (command);
7429
7430 if (0 < n && command[n - 1] == ' ')
7431 command[n - 1] = '\0';
7432 }
7433
7434 return TRUE;
7435 }
7436
7437 #define TARGET_BIG_SYM bfd_elf32_sh_vec
7438 #define TARGET_BIG_NAME "elf32-sh"
7439 #define TARGET_LITTLE_SYM bfd_elf32_shl_vec
7440 #define TARGET_LITTLE_NAME "elf32-shl"
7441 #define ELF_ARCH bfd_arch_sh
7442 #define ELF_MACHINE_CODE EM_SH
7443 #define ELF_MAXPAGESIZE 128
7444
7445 #define elf_symbol_leading_char '_'
7446
7447 #define bfd_elf32_bfd_reloc_type_lookup sh_elf_reloc_type_lookup
7448 #define elf_info_to_howto sh_elf_info_to_howto
7449 #define bfd_elf32_bfd_relax_section sh_elf_relax_section
7450 #define elf_backend_relocate_section sh_elf_relocate_section
7451 #define bfd_elf32_bfd_get_relocated_section_contents \
7452 sh_elf_get_relocated_section_contents
7453 #define bfd_elf32_mkobject sh_elf_mkobject
7454 #define elf_backend_object_p sh_elf_object_p
7455 #define bfd_elf32_bfd_set_private_bfd_flags \
7456 sh_elf_set_private_flags
7457 #define bfd_elf32_bfd_copy_private_bfd_data \
7458 sh_elf_copy_private_data
7459 #define bfd_elf32_bfd_merge_private_bfd_data \
7460 sh_elf_merge_private_data
7461
7462 #define elf_backend_gc_mark_hook sh_elf_gc_mark_hook
7463 #define elf_backend_gc_sweep_hook sh_elf_gc_sweep_hook
7464 #define elf_backend_check_relocs sh_elf_check_relocs
7465 #define elf_backend_copy_indirect_symbol \
7466 sh_elf_copy_indirect_symbol
7467 #define elf_backend_create_dynamic_sections \
7468 sh_elf_create_dynamic_sections
7469 #define bfd_elf32_bfd_link_hash_table_create \
7470 sh_elf_link_hash_table_create
7471 #define elf_backend_adjust_dynamic_symbol \
7472 sh_elf_adjust_dynamic_symbol
7473 #define elf_backend_size_dynamic_sections \
7474 sh_elf_size_dynamic_sections
7475 #define elf_backend_finish_dynamic_symbol \
7476 sh_elf_finish_dynamic_symbol
7477 #define elf_backend_finish_dynamic_sections \
7478 sh_elf_finish_dynamic_sections
7479 #define elf_backend_reloc_type_class sh_elf_reloc_type_class
7480
7481 #define elf_backend_can_gc_sections 1
7482 #define elf_backend_can_refcount 1
7483 #define elf_backend_want_got_plt 1
7484 #define elf_backend_plt_readonly 1
7485 #define elf_backend_want_plt_sym 0
7486 #define elf_backend_got_header_size 12
7487 #define elf_backend_plt_header_size PLT_ENTRY_SIZE
7488
7489 #ifndef INCLUDE_SHMEDIA
7490
7491 #include "elf32-target.h"
7492
7493 /* NetBSD support. */
7494 #undef TARGET_BIG_SYM
7495 #define TARGET_BIG_SYM bfd_elf32_shnbsd_vec
7496 #undef TARGET_BIG_NAME
7497 #define TARGET_BIG_NAME "elf32-sh-nbsd"
7498 #undef TARGET_LITTLE_SYM
7499 #define TARGET_LITTLE_SYM bfd_elf32_shlnbsd_vec
7500 #undef TARGET_LITTLE_NAME
7501 #define TARGET_LITTLE_NAME "elf32-shl-nbsd"
7502 #undef ELF_MAXPAGESIZE
7503 #define ELF_MAXPAGESIZE 0x10000
7504 #undef elf_symbol_leading_char
7505 #define elf_symbol_leading_char 0
7506 #undef elf32_bed
7507 #define elf32_bed elf32_sh_nbsd_bed
7508
7509 #include "elf32-target.h"
7510
7511
7512 /* Linux support. */
7513 #undef TARGET_BIG_SYM
7514 #define TARGET_BIG_SYM bfd_elf32_shblin_vec
7515 #undef TARGET_BIG_NAME
7516 #define TARGET_BIG_NAME "elf32-shbig-linux"
7517 #undef TARGET_LITTLE_SYM
7518 #define TARGET_LITTLE_SYM bfd_elf32_shlin_vec
7519 #undef TARGET_LITTLE_NAME
7520 #define TARGET_LITTLE_NAME "elf32-sh-linux"
7521
7522 #undef elf_backend_grok_prstatus
7523 #define elf_backend_grok_prstatus elf32_shlin_grok_prstatus
7524 #undef elf_backend_grok_psinfo
7525 #define elf_backend_grok_psinfo elf32_shlin_grok_psinfo
7526 #undef elf32_bed
7527 #define elf32_bed elf32_sh_lin_bed
7528
7529 #include "elf32-target.h"
7530
7531 #endif /* INCLUDE_SHMEDIA */
This page took 0.186677 seconds and 4 git commands to generate.