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