*** empty log message ***
[deliverable/binutils-gdb.git] / bfd / elf32-sh.c
CommitLineData
ef230218 1/* Renesas / SuperH SH specific support for 32-bit ELF
22d606e9
AM
2 Copyright 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
3 2006 Free Software Foundation, Inc.
252b5132
RH
4 Contributed by Ian Lance Taylor, Cygnus Support.
5
571fe01f 6 This file is part of BFD, the Binary File Descriptor library.
252b5132 7
571fe01f
NC
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.
252b5132 12
571fe01f
NC
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.
252b5132 17
571fe01f
NC
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
3e110533 20 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */
252b5132
RH
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"
f6f9408f 28#include "libiberty.h"
871ec896 29#include "../opcodes/sh-opc.h"
252b5132
RH
30
31static bfd_reloc_status_type sh_elf_reloc
09fd220b 32 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
252b5132 33static bfd_reloc_status_type sh_elf_ignore_reloc
09fd220b 34 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
b34976b6 35static bfd_boolean sh_elf_relax_delete_bytes
09fd220b 36 (bfd *, asection *, bfd_vma, int);
b34976b6 37static bfd_boolean sh_elf_align_loads
09fd220b 38 (bfd *, asection *, Elf_Internal_Rela *, bfd_byte *, bfd_boolean *);
85fbca6a 39#ifndef SH64_ELF
b34976b6 40static bfd_boolean sh_elf_swap_insns
09fd220b 41 (bfd *, asection *, void *, bfd_byte *, bfd_vma);
85fbca6a 42#endif
3376eaf5 43static int sh_elf_optimized_tls_reloc
09fd220b 44 (struct bfd_link_info *, int, int);
3376eaf5 45static bfd_vma dtpoff_base
09fd220b 46 (struct bfd_link_info *);
267fb3c1 47static bfd_vma tpoff
09fd220b 48 (struct bfd_link_info *, bfd_vma);
37c644f2
AO
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"
252b5132 54
38b1a46c
NC
55static reloc_howto_type sh_elf_howto_table[] =
56{
252b5132
RH
57 /* No relocation. */
58 HOWTO (R_SH_NONE, /* type */
59 0, /* rightshift */
60 0, /* size (0 = byte, 1 = short, 2 = long) */
61 0, /* bitsize */
b34976b6 62 FALSE, /* pc_relative */
252b5132
RH
63 0, /* bitpos */
64 complain_overflow_dont, /* complain_on_overflow */
015551fc 65 sh_elf_ignore_reloc, /* special_function */
252b5132 66 "R_SH_NONE", /* name */
b34976b6 67 FALSE, /* partial_inplace */
252b5132
RH
68 0, /* src_mask */
69 0, /* dst_mask */
b34976b6 70 FALSE), /* pcrel_offset */
252b5132 71
b34976b6 72 /* 32 bit absolute relocation. Setting partial_inplace to TRUE and
252b5132
RH
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 */
b34976b6 78 FALSE, /* pc_relative */
252b5132
RH
79 0, /* bitpos */
80 complain_overflow_bitfield, /* complain_on_overflow */
81 sh_elf_reloc, /* special_function */
82 "R_SH_DIR32", /* name */
b34976b6 83 TRUE, /* partial_inplace */
252b5132
RH
84 0xffffffff, /* src_mask */
85 0xffffffff, /* dst_mask */
b34976b6 86 FALSE), /* pcrel_offset */
252b5132
RH
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 */
b34976b6 93 TRUE, /* pc_relative */
252b5132
RH
94 0, /* bitpos */
95 complain_overflow_signed, /* complain_on_overflow */
015551fc 96 sh_elf_ignore_reloc, /* special_function */
252b5132 97 "R_SH_REL32", /* name */
b34976b6 98 TRUE, /* partial_inplace */
146be91a 99 0xffffffff, /* src_mask */
252b5132 100 0xffffffff, /* dst_mask */
b34976b6 101 TRUE), /* pcrel_offset */
252b5132
RH
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 */
b34976b6 108 TRUE, /* pc_relative */
252b5132
RH
109 0, /* bitpos */
110 complain_overflow_signed, /* complain_on_overflow */
015551fc 111 sh_elf_ignore_reloc, /* special_function */
252b5132 112 "R_SH_DIR8WPN", /* name */
b34976b6 113 TRUE, /* partial_inplace */
252b5132
RH
114 0xff, /* src_mask */
115 0xff, /* dst_mask */
b34976b6 116 TRUE), /* pcrel_offset */
252b5132
RH
117
118 /* 12 bit PC relative branch divided by 2. */
bdfaef52
JR
119 /* This cannot be partial_inplace because relaxation can't know the
120 eventual value of a symbol. */
252b5132
RH
121 HOWTO (R_SH_IND12W, /* type */
122 1, /* rightshift */
123 1, /* size (0 = byte, 1 = short, 2 = long) */
124 12, /* bitsize */
b34976b6 125 TRUE, /* pc_relative */
252b5132
RH
126 0, /* bitpos */
127 complain_overflow_signed, /* complain_on_overflow */
bdfaef52 128 NULL, /* special_function */
252b5132 129 "R_SH_IND12W", /* name */
bdfaef52
JR
130 FALSE, /* partial_inplace */
131 0x0, /* src_mask */
252b5132 132 0xfff, /* dst_mask */
b34976b6 133 TRUE), /* pcrel_offset */
252b5132
RH
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 */
b34976b6 140 TRUE, /* pc_relative */
252b5132
RH
141 0, /* bitpos */
142 complain_overflow_unsigned, /* complain_on_overflow */
015551fc 143 sh_elf_ignore_reloc, /* special_function */
252b5132 144 "R_SH_DIR8WPL", /* name */
b34976b6 145 TRUE, /* partial_inplace */
252b5132
RH
146 0xff, /* src_mask */
147 0xff, /* dst_mask */
b34976b6 148 TRUE), /* pcrel_offset */
252b5132
RH
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 */
b34976b6 155 TRUE, /* pc_relative */
252b5132
RH
156 0, /* bitpos */
157 complain_overflow_unsigned, /* complain_on_overflow */
015551fc 158 sh_elf_ignore_reloc, /* special_function */
252b5132 159 "R_SH_DIR8WPZ", /* name */
b34976b6 160 TRUE, /* partial_inplace */
252b5132
RH
161 0xff, /* src_mask */
162 0xff, /* dst_mask */
b34976b6 163 TRUE), /* pcrel_offset */
252b5132
RH
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 */
b34976b6 172 FALSE, /* pc_relative */
252b5132
RH
173 0, /* bitpos */
174 complain_overflow_unsigned, /* complain_on_overflow */
015551fc 175 sh_elf_ignore_reloc, /* special_function */
252b5132 176 "R_SH_DIR8BP", /* name */
b34976b6 177 FALSE, /* partial_inplace */
252b5132
RH
178 0, /* src_mask */
179 0xff, /* dst_mask */
b34976b6 180 TRUE), /* pcrel_offset */
252b5132
RH
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 */
b34976b6 189 FALSE, /* pc_relative */
252b5132
RH
190 0, /* bitpos */
191 complain_overflow_unsigned, /* complain_on_overflow */
015551fc 192 sh_elf_ignore_reloc, /* special_function */
252b5132 193 "R_SH_DIR8W", /* name */
b34976b6 194 FALSE, /* partial_inplace */
252b5132
RH
195 0, /* src_mask */
196 0xff, /* dst_mask */
b34976b6 197 TRUE), /* pcrel_offset */
252b5132
RH
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 */
b34976b6 206 FALSE, /* pc_relative */
252b5132
RH
207 0, /* bitpos */
208 complain_overflow_unsigned, /* complain_on_overflow */
015551fc 209 sh_elf_ignore_reloc, /* special_function */
252b5132 210 "R_SH_DIR8L", /* name */
b34976b6 211 FALSE, /* partial_inplace */
252b5132
RH
212 0, /* src_mask */
213 0xff, /* dst_mask */
b34976b6 214 TRUE), /* pcrel_offset */
252b5132 215
d38eb334
DD
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
06bb75c1
AO
246 EMPTY_HOWTO (12),
247 EMPTY_HOWTO (13),
248 EMPTY_HOWTO (14),
249 EMPTY_HOWTO (15),
250 EMPTY_HOWTO (16),
251 EMPTY_HOWTO (17),
5f771d47
ILT
252 EMPTY_HOWTO (18),
253 EMPTY_HOWTO (19),
254 EMPTY_HOWTO (20),
255 EMPTY_HOWTO (21),
252b5132
RH
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
d38eb334
DD
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
252b5132
RH
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 */
b34976b6 316 FALSE, /* pc_relative */
252b5132
RH
317 0, /* bitpos */
318 complain_overflow_unsigned, /* complain_on_overflow */
319 sh_elf_ignore_reloc, /* special_function */
320 "R_SH_SWITCH16", /* name */
b34976b6 321 FALSE, /* partial_inplace */
252b5132
RH
322 0, /* src_mask */
323 0, /* dst_mask */
b34976b6 324 TRUE), /* pcrel_offset */
252b5132
RH
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 */
b34976b6 333 FALSE, /* pc_relative */
252b5132
RH
334 0, /* bitpos */
335 complain_overflow_unsigned, /* complain_on_overflow */
336 sh_elf_ignore_reloc, /* special_function */
337 "R_SH_SWITCH32", /* name */
b34976b6 338 FALSE, /* partial_inplace */
252b5132
RH
339 0, /* src_mask */
340 0, /* dst_mask */
b34976b6 341 TRUE), /* pcrel_offset */
252b5132
RH
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 */
b34976b6 351 FALSE, /* pc_relative */
252b5132
RH
352 0, /* bitpos */
353 complain_overflow_unsigned, /* complain_on_overflow */
354 sh_elf_ignore_reloc, /* special_function */
355 "R_SH_USES", /* name */
b34976b6 356 FALSE, /* partial_inplace */
252b5132
RH
357 0, /* src_mask */
358 0, /* dst_mask */
b34976b6 359 TRUE), /* pcrel_offset */
252b5132
RH
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 */
b34976b6 369 FALSE, /* pc_relative */
252b5132
RH
370 0, /* bitpos */
371 complain_overflow_unsigned, /* complain_on_overflow */
372 sh_elf_ignore_reloc, /* special_function */
373 "R_SH_COUNT", /* name */
b34976b6 374 FALSE, /* partial_inplace */
252b5132
RH
375 0, /* src_mask */
376 0, /* dst_mask */
b34976b6 377 TRUE), /* pcrel_offset */
252b5132
RH
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 */
b34976b6 386 FALSE, /* pc_relative */
252b5132
RH
387 0, /* bitpos */
388 complain_overflow_unsigned, /* complain_on_overflow */
389 sh_elf_ignore_reloc, /* special_function */
390 "R_SH_ALIGN", /* name */
b34976b6 391 FALSE, /* partial_inplace */
252b5132
RH
392 0, /* src_mask */
393 0, /* dst_mask */
b34976b6 394 TRUE), /* pcrel_offset */
252b5132
RH
395
396 /* The assembler will generate this reloc before a block of
4cc11e76 397 instructions. A section should be processed as assuming it
252b5132
RH
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 */
b34976b6 403 FALSE, /* pc_relative */
252b5132
RH
404 0, /* bitpos */
405 complain_overflow_unsigned, /* complain_on_overflow */
406 sh_elf_ignore_reloc, /* special_function */
407 "R_SH_CODE", /* name */
b34976b6 408 FALSE, /* partial_inplace */
252b5132
RH
409 0, /* src_mask */
410 0, /* dst_mask */
b34976b6 411 TRUE), /* pcrel_offset */
252b5132
RH
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 */
b34976b6 419 FALSE, /* pc_relative */
252b5132
RH
420 0, /* bitpos */
421 complain_overflow_unsigned, /* complain_on_overflow */
422 sh_elf_ignore_reloc, /* special_function */
423 "R_SH_DATA", /* name */
b34976b6 424 FALSE, /* partial_inplace */
252b5132
RH
425 0, /* src_mask */
426 0, /* dst_mask */
b34976b6 427 TRUE), /* pcrel_offset */
252b5132
RH
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 */
b34976b6 436 FALSE, /* pc_relative */
252b5132
RH
437 0, /* bitpos */
438 complain_overflow_unsigned, /* complain_on_overflow */
439 sh_elf_ignore_reloc, /* special_function */
440 "R_SH_LABEL", /* name */
b34976b6 441 FALSE, /* partial_inplace */
252b5132
RH
442 0, /* src_mask */
443 0, /* dst_mask */
b34976b6 444 TRUE), /* pcrel_offset */
252b5132 445
d38eb334
DD
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 */
252b5132
RH
462 0, /* rightshift */
463 0, /* size (0 = byte, 1 = short, 2 = long) */
464 8, /* bitsize */
b34976b6 465 FALSE, /* pc_relative */
252b5132 466 0, /* bitpos */
d38eb334
DD
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 */
252b5132 481 complain_overflow_unsigned, /* complain_on_overflow */
d38eb334
DD
482 bfd_elf_generic_reloc, /* special_function */
483 "R_SH_DIR8UL", /* name */
b34976b6 484 FALSE, /* partial_inplace */
252b5132 485 0, /* src_mask */
d38eb334
DD
486 0xff, /* dst_mask */
487 FALSE), /* pcrel_offset */
252b5132 488
d38eb334
DD
489 HOWTO (R_SH_DIR8UW, /* type */
490 1, /* rightshift */
491 0, /* size (0 = byte, 1 = short, 2 = long) */
492 8, /* bitsize */
b34976b6 493 FALSE, /* pc_relative */
067653c5 494 0, /* bitpos */
d38eb334
DD
495 complain_overflow_unsigned, /* complain_on_overflow */
496 bfd_elf_generic_reloc, /* special_function */
497 "R_SH_DIR8UW", /* name */
b34976b6 498 FALSE, /* partial_inplace */
067653c5 499 0, /* src_mask */
d38eb334 500 0xff, /* dst_mask */
b34976b6 501 FALSE), /* pcrel_offset */
252b5132 502
d38eb334 503 HOWTO (R_SH_DIR8U, /* type */
067653c5 504 0, /* rightshift */
d38eb334
DD
505 0, /* size (0 = byte, 1 = short, 2 = long) */
506 8, /* bitsize */
b34976b6 507 FALSE, /* pc_relative */
067653c5 508 0, /* bitpos */
d38eb334
DD
509 complain_overflow_unsigned, /* complain_on_overflow */
510 bfd_elf_generic_reloc, /* special_function */
511 "R_SH_DIR8U", /* name */
b34976b6 512 FALSE, /* partial_inplace */
067653c5 513 0, /* src_mask */
d38eb334 514 0xff, /* dst_mask */
b34976b6 515 FALSE), /* pcrel_offset */
252b5132 516
d38eb334 517 HOWTO (R_SH_DIR8SW, /* type */
015551fc 518 1, /* rightshift */
d38eb334 519 0, /* size (0 = byte, 1 = short, 2 = long) */
015551fc 520 8, /* bitsize */
b34976b6 521 FALSE, /* pc_relative */
015551fc
JR
522 0, /* bitpos */
523 complain_overflow_signed, /* complain_on_overflow */
d38eb334
DD
524 bfd_elf_generic_reloc, /* special_function */
525 "R_SH_DIR8SW", /* name */
526 FALSE, /* partial_inplace */
527 0, /* src_mask */
015551fc 528 0xff, /* dst_mask */
d38eb334 529 FALSE), /* pcrel_offset */
015551fc 530
d38eb334
DD
531 HOWTO (R_SH_DIR8S, /* type */
532 0, /* rightshift */
533 0, /* size (0 = byte, 1 = short, 2 = long) */
015551fc 534 8, /* bitsize */
b34976b6 535 FALSE, /* pc_relative */
015551fc
JR
536 0, /* bitpos */
537 complain_overflow_signed, /* complain_on_overflow */
d38eb334
DD
538 bfd_elf_generic_reloc, /* special_function */
539 "R_SH_DIR8S", /* name */
540 FALSE, /* partial_inplace */
541 0, /* src_mask */
015551fc 542 0xff, /* dst_mask */
d38eb334
DD
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 */
015551fc 600
d38eb334
DD
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 */
fbca6ad9
AO
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 */
b34976b6 621 FALSE, /* pc_relative */
fbca6ad9
AO
622 10, /* bitpos */
623 complain_overflow_unsigned, /* complain_on_overflow */
624 bfd_elf_generic_reloc, /* special_function */
625 "R_SH_DIR5U", /* name */
b34976b6 626 FALSE, /* partial_inplace */
fbca6ad9
AO
627 0, /* src_mask */
628 0xfc00, /* dst_mask */
b34976b6 629 FALSE), /* pcrel_offset */
fbca6ad9
AO
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 */
b34976b6 636 FALSE, /* pc_relative */
fbca6ad9
AO
637 10, /* bitpos */
638 complain_overflow_unsigned, /* complain_on_overflow */
639 bfd_elf_generic_reloc, /* special_function */
640 "R_SH_DIR6U", /* name */
b34976b6 641 FALSE, /* partial_inplace */
fbca6ad9
AO
642 0, /* src_mask */
643 0xfc00, /* dst_mask */
b34976b6 644 FALSE), /* pcrel_offset */
fbca6ad9
AO
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 */
b34976b6 651 FALSE, /* pc_relative */
fbca6ad9
AO
652 10, /* bitpos */
653 complain_overflow_signed, /* complain_on_overflow */
654 bfd_elf_generic_reloc, /* special_function */
655 "R_SH_DIR6S", /* name */
b34976b6 656 FALSE, /* partial_inplace */
fbca6ad9
AO
657 0, /* src_mask */
658 0xfc00, /* dst_mask */
b34976b6 659 FALSE), /* pcrel_offset */
fbca6ad9
AO
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 */
b34976b6 666 FALSE, /* pc_relative */
fbca6ad9
AO
667 10, /* bitpos */
668 complain_overflow_signed, /* complain_on_overflow */
669 bfd_elf_generic_reloc, /* special_function */
670 "R_SH_DIR10S", /* name */
b34976b6 671 FALSE, /* partial_inplace */
fbca6ad9
AO
672 0, /* src_mask */
673 0xffc00, /* dst_mask */
b34976b6 674 FALSE), /* pcrel_offset */
fbca6ad9 675
067653c5 676 /* Used in LD.UW, ST.W et al. */
fbca6ad9
AO
677 HOWTO (R_SH_DIR10SW, /* type */
678 1, /* rightshift */
679 2, /* size (0 = byte, 1 = short, 2 = long) */
680 11, /* bitsize */
b34976b6 681 FALSE, /* pc_relative */
fbca6ad9
AO
682 10, /* bitpos */
683 complain_overflow_signed, /* complain_on_overflow */
684 bfd_elf_generic_reloc, /* special_function */
685 "R_SH_DIR10SW", /* name */
b34976b6 686 FALSE, /* partial_inplace */
fbca6ad9
AO
687 0, /* src_mask */
688 0xffc00, /* dst_mask */
b34976b6 689 FALSE), /* pcrel_offset */
fbca6ad9 690
067653c5 691 /* Used in LD.L, FLD.S et al. */
fbca6ad9
AO
692 HOWTO (R_SH_DIR10SL, /* type */
693 2, /* rightshift */
694 2, /* size (0 = byte, 1 = short, 2 = long) */
695 12, /* bitsize */
b34976b6 696 FALSE, /* pc_relative */
fbca6ad9
AO
697 10, /* bitpos */
698 complain_overflow_signed, /* complain_on_overflow */
699 bfd_elf_generic_reloc, /* special_function */
700 "R_SH_DIR10SL", /* name */
b34976b6 701 FALSE, /* partial_inplace */
fbca6ad9
AO
702 0, /* src_mask */
703 0xffc00, /* dst_mask */
b34976b6 704 FALSE), /* pcrel_offset */
fbca6ad9
AO
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 */
b34976b6 711 FALSE, /* pc_relative */
fbca6ad9
AO
712 10, /* bitpos */
713 complain_overflow_signed, /* complain_on_overflow */
714 bfd_elf_generic_reloc, /* special_function */
715 "R_SH_DIR10SQ", /* name */
b34976b6 716 FALSE, /* partial_inplace */
fbca6ad9
AO
717 0, /* src_mask */
718 0xffc00, /* dst_mask */
b34976b6 719 FALSE), /* pcrel_offset */
fbca6ad9
AO
720
721#else
06bb75c1
AO
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),
fbca6ad9
AO
729#endif
730
06bb75c1 731 EMPTY_HOWTO (52),
d38eb334
DD
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
06bb75c1
AO
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),
3376eaf5
KK
837
838 HOWTO (R_SH_TLS_GD_32, /* type */
839 0, /* rightshift */
840 2, /* size (0 = byte, 1 = short, 2 = long) */
841 32, /* bitsize */
b34976b6 842 FALSE, /* pc_relative */
3376eaf5
KK
843 0, /* bitpos */
844 complain_overflow_bitfield, /* complain_on_overflow */
845 bfd_elf_generic_reloc, /* */
846 "R_SH_TLS_GD_32", /* name */
b34976b6 847 TRUE, /* partial_inplace */
3376eaf5
KK
848 0xffffffff, /* src_mask */
849 0xffffffff, /* dst_mask */
b34976b6 850 FALSE), /* pcrel_offset */
3376eaf5
KK
851
852 HOWTO (R_SH_TLS_LD_32, /* type */
853 0, /* rightshift */
854 2, /* size (0 = byte, 1 = short, 2 = long) */
855 32, /* bitsize */
b34976b6 856 FALSE, /* pc_relative */
3376eaf5
KK
857 0, /* bitpos */
858 complain_overflow_bitfield, /* complain_on_overflow */
859 bfd_elf_generic_reloc, /* */
860 "R_SH_TLS_LD_32", /* name */
b34976b6 861 TRUE, /* partial_inplace */
3376eaf5
KK
862 0xffffffff, /* src_mask */
863 0xffffffff, /* dst_mask */
b34976b6 864 FALSE), /* pcrel_offset */
3376eaf5
KK
865
866 HOWTO (R_SH_TLS_LDO_32, /* type */
867 0, /* rightshift */
868 2, /* size (0 = byte, 1 = short, 2 = long) */
869 32, /* bitsize */
b34976b6 870 FALSE, /* pc_relative */
3376eaf5
KK
871 0, /* bitpos */
872 complain_overflow_bitfield, /* complain_on_overflow */
873 bfd_elf_generic_reloc, /* */
874 "R_SH_TLS_LDO_32", /* name */
b34976b6 875 TRUE, /* partial_inplace */
3376eaf5
KK
876 0xffffffff, /* src_mask */
877 0xffffffff, /* dst_mask */
b34976b6 878 FALSE), /* pcrel_offset */
3376eaf5
KK
879
880 HOWTO (R_SH_TLS_IE_32, /* type */
881 0, /* rightshift */
882 2, /* size (0 = byte, 1 = short, 2 = long) */
883 32, /* bitsize */
b34976b6 884 FALSE, /* pc_relative */
3376eaf5
KK
885 0, /* bitpos */
886 complain_overflow_bitfield, /* complain_on_overflow */
887 bfd_elf_generic_reloc, /* */
888 "R_SH_TLS_IE_32", /* name */
b34976b6 889 TRUE, /* partial_inplace */
3376eaf5
KK
890 0xffffffff, /* src_mask */
891 0xffffffff, /* dst_mask */
b34976b6 892 FALSE), /* pcrel_offset */
3376eaf5
KK
893
894 HOWTO (R_SH_TLS_LE_32, /* type */
895 0, /* rightshift */
896 2, /* size (0 = byte, 1 = short, 2 = long) */
897 32, /* bitsize */
b34976b6 898 FALSE, /* pc_relative */
3376eaf5
KK
899 0, /* bitpos */
900 complain_overflow_bitfield, /* complain_on_overflow */
901 bfd_elf_generic_reloc, /* */
902 "R_SH_TLS_LE_32", /* name */
b34976b6 903 TRUE, /* partial_inplace */
3376eaf5
KK
904 0xffffffff, /* src_mask */
905 0xffffffff, /* dst_mask */
b34976b6 906 FALSE), /* pcrel_offset */
3376eaf5
KK
907
908 HOWTO (R_SH_TLS_DTPMOD32, /* type */
909 0, /* rightshift */
910 2, /* size (0 = byte, 1 = short, 2 = long) */
911 32, /* bitsize */
b34976b6 912 FALSE, /* pc_relative */
3376eaf5
KK
913 0, /* bitpos */
914 complain_overflow_bitfield, /* complain_on_overflow */
915 bfd_elf_generic_reloc, /* */
916 "R_SH_TLS_DTPMOD32", /* name */
b34976b6 917 TRUE, /* partial_inplace */
3376eaf5
KK
918 0xffffffff, /* src_mask */
919 0xffffffff, /* dst_mask */
b34976b6 920 FALSE), /* pcrel_offset */
3376eaf5
KK
921
922 HOWTO (R_SH_TLS_DTPOFF32, /* type */
923 0, /* rightshift */
924 2, /* size (0 = byte, 1 = short, 2 = long) */
925 32, /* bitsize */
b34976b6 926 FALSE, /* pc_relative */
3376eaf5
KK
927 0, /* bitpos */
928 complain_overflow_bitfield, /* complain_on_overflow */
929 bfd_elf_generic_reloc, /* */
930 "R_SH_TLS_DTPOFF32", /* name */
b34976b6 931 TRUE, /* partial_inplace */
3376eaf5
KK
932 0xffffffff, /* src_mask */
933 0xffffffff, /* dst_mask */
b34976b6 934 FALSE), /* pcrel_offset */
3376eaf5
KK
935
936 HOWTO (R_SH_TLS_TPOFF32, /* type */
937 0, /* rightshift */
938 2, /* size (0 = byte, 1 = short, 2 = long) */
939 32, /* bitsize */
b34976b6 940 FALSE, /* pc_relative */
3376eaf5
KK
941 0, /* bitpos */
942 complain_overflow_bitfield, /* complain_on_overflow */
943 bfd_elf_generic_reloc, /* */
944 "R_SH_TLS_TPOFF32", /* name */
b34976b6 945 TRUE, /* partial_inplace */
3376eaf5
KK
946 0xffffffff, /* src_mask */
947 0xffffffff, /* dst_mask */
b34976b6 948 FALSE), /* pcrel_offset */
3376eaf5 949
06bb75c1
AO
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 */
b34976b6 963 FALSE, /* pc_relative */
06bb75c1
AO
964 0, /* bitpos */
965 complain_overflow_bitfield, /* complain_on_overflow */
966 bfd_elf_generic_reloc, /* */
967 "R_SH_GOT32", /* name */
b34976b6 968 TRUE, /* partial_inplace */
06bb75c1
AO
969 0xffffffff, /* src_mask */
970 0xffffffff, /* dst_mask */
b34976b6 971 FALSE), /* pcrel_offset */
06bb75c1
AO
972
973 HOWTO (R_SH_PLT32, /* type */
974 0, /* rightshift */
975 2, /* size (0 = byte, 1 = short, 2 = long) */
976 32, /* bitsize */
b34976b6 977 TRUE, /* pc_relative */
06bb75c1
AO
978 0, /* bitpos */
979 complain_overflow_bitfield, /* complain_on_overflow */
980 bfd_elf_generic_reloc, /* */
981 "R_SH_PLT32", /* name */
b34976b6 982 TRUE, /* partial_inplace */
06bb75c1
AO
983 0xffffffff, /* src_mask */
984 0xffffffff, /* dst_mask */
b34976b6 985 TRUE), /* pcrel_offset */
06bb75c1
AO
986
987 HOWTO (R_SH_COPY, /* type */
988 0, /* rightshift */
989 2, /* size (0 = byte, 1 = short, 2 = long) */
990 32, /* bitsize */
b34976b6 991 FALSE, /* pc_relative */
06bb75c1
AO
992 0, /* bitpos */
993 complain_overflow_bitfield, /* complain_on_overflow */
994 bfd_elf_generic_reloc, /* */
995 "R_SH_COPY", /* name */
b34976b6 996 TRUE, /* partial_inplace */
06bb75c1
AO
997 0xffffffff, /* src_mask */
998 0xffffffff, /* dst_mask */
b34976b6 999 FALSE), /* pcrel_offset */
06bb75c1
AO
1000
1001 HOWTO (R_SH_GLOB_DAT, /* type */
1002 0, /* rightshift */
1003 2, /* size (0 = byte, 1 = short, 2 = long) */
1004 32, /* bitsize */
b34976b6 1005 FALSE, /* pc_relative */
06bb75c1
AO
1006 0, /* bitpos */
1007 complain_overflow_bitfield, /* complain_on_overflow */
1008 bfd_elf_generic_reloc, /* */
1009 "R_SH_GLOB_DAT", /* name */
b34976b6 1010 TRUE, /* partial_inplace */
06bb75c1
AO
1011 0xffffffff, /* src_mask */
1012 0xffffffff, /* dst_mask */
b34976b6 1013 FALSE), /* pcrel_offset */
06bb75c1
AO
1014
1015 HOWTO (R_SH_JMP_SLOT, /* type */
1016 0, /* rightshift */
1017 2, /* size (0 = byte, 1 = short, 2 = long) */
1018 32, /* bitsize */
b34976b6 1019 FALSE, /* pc_relative */
06bb75c1
AO
1020 0, /* bitpos */
1021 complain_overflow_bitfield, /* complain_on_overflow */
1022 bfd_elf_generic_reloc, /* */
1023 "R_SH_JMP_SLOT", /* name */
b34976b6 1024 TRUE, /* partial_inplace */
06bb75c1
AO
1025 0xffffffff, /* src_mask */
1026 0xffffffff, /* dst_mask */
b34976b6 1027 FALSE), /* pcrel_offset */
06bb75c1
AO
1028
1029 HOWTO (R_SH_RELATIVE, /* type */
1030 0, /* rightshift */
1031 2, /* size (0 = byte, 1 = short, 2 = long) */
1032 32, /* bitsize */
b34976b6 1033 FALSE, /* pc_relative */
06bb75c1
AO
1034 0, /* bitpos */
1035 complain_overflow_bitfield, /* complain_on_overflow */
1036 bfd_elf_generic_reloc, /* */
1037 "R_SH_RELATIVE", /* name */
b34976b6 1038 TRUE, /* partial_inplace */
06bb75c1
AO
1039 0xffffffff, /* src_mask */
1040 0xffffffff, /* dst_mask */
b34976b6 1041 FALSE), /* pcrel_offset */
06bb75c1
AO
1042
1043 HOWTO (R_SH_GOTOFF, /* type */
1044 0, /* rightshift */
1045 2, /* size (0 = byte, 1 = short, 2 = long) */
1046 32, /* bitsize */
b34976b6 1047 FALSE, /* pc_relative */
06bb75c1
AO
1048 0, /* bitpos */
1049 complain_overflow_bitfield, /* complain_on_overflow */
1050 bfd_elf_generic_reloc, /* */
1051 "R_SH_GOTOFF", /* name */
b34976b6 1052 TRUE, /* partial_inplace */
06bb75c1
AO
1053 0xffffffff, /* src_mask */
1054 0xffffffff, /* dst_mask */
b34976b6 1055 FALSE), /* pcrel_offset */
06bb75c1
AO
1056
1057 HOWTO (R_SH_GOTPC, /* type */
1058 0, /* rightshift */
1059 2, /* size (0 = byte, 1 = short, 2 = long) */
1060 32, /* bitsize */
b34976b6 1061 TRUE, /* pc_relative */
06bb75c1
AO
1062 0, /* bitpos */
1063 complain_overflow_bitfield, /* complain_on_overflow */
1064 bfd_elf_generic_reloc, /* */
1065 "R_SH_GOTPC", /* name */
b34976b6 1066 TRUE, /* partial_inplace */
06bb75c1
AO
1067 0xffffffff, /* src_mask */
1068 0xffffffff, /* dst_mask */
b34976b6 1069 TRUE), /* pcrel_offset */
06bb75c1 1070
fbca6ad9
AO
1071 HOWTO (R_SH_GOTPLT32, /* type */
1072 0, /* rightshift */
1073 2, /* size (0 = byte, 1 = short, 2 = long) */
1074 32, /* bitsize */
b34976b6 1075 FALSE, /* pc_relative */
fbca6ad9
AO
1076 0, /* bitpos */
1077 complain_overflow_bitfield, /* complain_on_overflow */
1078 bfd_elf_generic_reloc, /* */
1079 "R_SH_GOTPLT32", /* name */
b34976b6 1080 FALSE, /* partial_inplace */
fbca6ad9
AO
1081 0xffffffff, /* src_mask */
1082 0xffffffff, /* dst_mask */
b34976b6 1083 FALSE), /* pcrel_offset */
fbca6ad9
AO
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 */
b34976b6 1091 FALSE, /* pc_relative */
fbca6ad9
AO
1092 10, /* bitpos */
1093 complain_overflow_dont, /* complain_on_overflow */
1094 bfd_elf_generic_reloc, /* special_function */
067653c5 1095 "R_SH_GOT_LOW16", /* name */
b34976b6 1096 FALSE, /* partial_inplace */
fbca6ad9
AO
1097 0, /* src_mask */
1098 0x3fffc00, /* dst_mask */
b34976b6 1099 FALSE), /* pcrel_offset */
fbca6ad9
AO
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 */
b34976b6 1106 FALSE, /* pc_relative */
fbca6ad9
AO
1107 10, /* bitpos */
1108 complain_overflow_dont, /* complain_on_overflow */
1109 bfd_elf_generic_reloc, /* special_function */
1110 "R_SH_GOT_MEDLOW16", /* name */
b34976b6 1111 FALSE, /* partial_inplace */
fbca6ad9
AO
1112 0, /* src_mask */
1113 0x3fffc00, /* dst_mask */
b34976b6 1114 FALSE), /* pcrel_offset */
fbca6ad9
AO
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 */
b34976b6 1121 FALSE, /* pc_relative */
fbca6ad9
AO
1122 10, /* bitpos */
1123 complain_overflow_dont, /* complain_on_overflow */
1124 bfd_elf_generic_reloc, /* special_function */
1125 "R_SH_GOT_MEDHI16", /* name */
b34976b6 1126 FALSE, /* partial_inplace */
fbca6ad9
AO
1127 0, /* src_mask */
1128 0x3fffc00, /* dst_mask */
b34976b6 1129 FALSE), /* pcrel_offset */
fbca6ad9
AO
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 */
b34976b6 1136 FALSE, /* pc_relative */
fbca6ad9
AO
1137 10, /* bitpos */
1138 complain_overflow_dont, /* complain_on_overflow */
1139 bfd_elf_generic_reloc, /* special_function */
1140 "R_SH_GOT_HI16", /* name */
b34976b6 1141 FALSE, /* partial_inplace */
fbca6ad9
AO
1142 0, /* src_mask */
1143 0x3fffc00, /* dst_mask */
b34976b6 1144 FALSE), /* pcrel_offset */
fbca6ad9
AO
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 */
b34976b6 1151 FALSE, /* pc_relative */
fbca6ad9
AO
1152 10, /* bitpos */
1153 complain_overflow_dont, /* complain_on_overflow */
1154 bfd_elf_generic_reloc, /* special_function */
067653c5 1155 "R_SH_GOTPLT_LOW16", /* name */
b34976b6 1156 FALSE, /* partial_inplace */
fbca6ad9
AO
1157 0, /* src_mask */
1158 0x3fffc00, /* dst_mask */
b34976b6 1159 FALSE), /* pcrel_offset */
fbca6ad9
AO
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 */
b34976b6 1166 FALSE, /* pc_relative */
fbca6ad9
AO
1167 10, /* bitpos */
1168 complain_overflow_dont, /* complain_on_overflow */
1169 bfd_elf_generic_reloc, /* special_function */
1170 "R_SH_GOTPLT_MEDLOW16", /* name */
b34976b6 1171 FALSE, /* partial_inplace */
fbca6ad9
AO
1172 0, /* src_mask */
1173 0x3fffc00, /* dst_mask */
b34976b6 1174 FALSE), /* pcrel_offset */
fbca6ad9
AO
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 */
b34976b6 1181 FALSE, /* pc_relative */
fbca6ad9
AO
1182 10, /* bitpos */
1183 complain_overflow_dont, /* complain_on_overflow */
1184 bfd_elf_generic_reloc, /* special_function */
1185 "R_SH_GOTPLT_MEDHI16", /* name */
b34976b6 1186 FALSE, /* partial_inplace */
fbca6ad9
AO
1187 0, /* src_mask */
1188 0x3fffc00, /* dst_mask */
b34976b6 1189 FALSE), /* pcrel_offset */
fbca6ad9
AO
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 */
b34976b6 1196 FALSE, /* pc_relative */
fbca6ad9
AO
1197 10, /* bitpos */
1198 complain_overflow_dont, /* complain_on_overflow */
1199 bfd_elf_generic_reloc, /* special_function */
1200 "R_SH_GOTPLT_HI16", /* name */
b34976b6 1201 FALSE, /* partial_inplace */
fbca6ad9
AO
1202 0, /* src_mask */
1203 0x3fffc00, /* dst_mask */
b34976b6 1204 FALSE), /* pcrel_offset */
fbca6ad9
AO
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 */
b34976b6 1211 TRUE, /* pc_relative */
fbca6ad9
AO
1212 10, /* bitpos */
1213 complain_overflow_dont, /* complain_on_overflow */
1214 bfd_elf_generic_reloc, /* special_function */
067653c5 1215 "R_SH_PLT_LOW16", /* name */
b34976b6 1216 FALSE, /* partial_inplace */
fbca6ad9
AO
1217 0, /* src_mask */
1218 0x3fffc00, /* dst_mask */
b34976b6 1219 TRUE), /* pcrel_offset */
fbca6ad9
AO
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 */
b34976b6 1226 TRUE, /* pc_relative */
fbca6ad9
AO
1227 10, /* bitpos */
1228 complain_overflow_dont, /* complain_on_overflow */
1229 bfd_elf_generic_reloc, /* special_function */
1230 "R_SH_PLT_MEDLOW16", /* name */
b34976b6 1231 FALSE, /* partial_inplace */
fbca6ad9
AO
1232 0, /* src_mask */
1233 0x3fffc00, /* dst_mask */
b34976b6 1234 TRUE), /* pcrel_offset */
fbca6ad9
AO
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 */
b34976b6 1241 TRUE, /* pc_relative */
fbca6ad9
AO
1242 10, /* bitpos */
1243 complain_overflow_dont, /* complain_on_overflow */
1244 bfd_elf_generic_reloc, /* special_function */
1245 "R_SH_PLT_MEDHI16", /* name */
b34976b6 1246 FALSE, /* partial_inplace */
fbca6ad9
AO
1247 0, /* src_mask */
1248 0x3fffc00, /* dst_mask */
b34976b6 1249 TRUE), /* pcrel_offset */
fbca6ad9
AO
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 */
b34976b6 1256 TRUE, /* pc_relative */
fbca6ad9
AO
1257 10, /* bitpos */
1258 complain_overflow_dont, /* complain_on_overflow */
1259 bfd_elf_generic_reloc, /* special_function */
1260 "R_SH_PLT_HI16", /* name */
b34976b6 1261 FALSE, /* partial_inplace */
fbca6ad9
AO
1262 0, /* src_mask */
1263 0x3fffc00, /* dst_mask */
b34976b6 1264 TRUE), /* pcrel_offset */
fbca6ad9
AO
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 */
b34976b6 1271 FALSE, /* pc_relative */
fbca6ad9
AO
1272 10, /* bitpos */
1273 complain_overflow_dont, /* complain_on_overflow */
1274 bfd_elf_generic_reloc, /* special_function */
067653c5 1275 "R_SH_GOTOFF_LOW16", /* name */
b34976b6 1276 FALSE, /* partial_inplace */
fbca6ad9
AO
1277 0, /* src_mask */
1278 0x3fffc00, /* dst_mask */
b34976b6 1279 FALSE), /* pcrel_offset */
fbca6ad9
AO
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 */
b34976b6 1286 FALSE, /* pc_relative */
fbca6ad9
AO
1287 10, /* bitpos */
1288 complain_overflow_dont, /* complain_on_overflow */
1289 bfd_elf_generic_reloc, /* special_function */
1290 "R_SH_GOTOFF_MEDLOW16", /* name */
b34976b6 1291 FALSE, /* partial_inplace */
fbca6ad9
AO
1292 0, /* src_mask */
1293 0x3fffc00, /* dst_mask */
b34976b6 1294 FALSE), /* pcrel_offset */
fbca6ad9
AO
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 */
b34976b6 1301 FALSE, /* pc_relative */
fbca6ad9
AO
1302 10, /* bitpos */
1303 complain_overflow_dont, /* complain_on_overflow */
1304 bfd_elf_generic_reloc, /* special_function */
1305 "R_SH_GOTOFF_MEDHI16", /* name */
b34976b6 1306 FALSE, /* partial_inplace */
fbca6ad9
AO
1307 0, /* src_mask */
1308 0x3fffc00, /* dst_mask */
b34976b6 1309 FALSE), /* pcrel_offset */
fbca6ad9
AO
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 */
b34976b6 1316 FALSE, /* pc_relative */
fbca6ad9
AO
1317 10, /* bitpos */
1318 complain_overflow_dont, /* complain_on_overflow */
1319 bfd_elf_generic_reloc, /* special_function */
1320 "R_SH_GOTOFF_HI16", /* name */
b34976b6 1321 FALSE, /* partial_inplace */
fbca6ad9
AO
1322 0, /* src_mask */
1323 0x3fffc00, /* dst_mask */
b34976b6 1324 FALSE), /* pcrel_offset */
fbca6ad9
AO
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 */
b34976b6 1331 TRUE, /* pc_relative */
fbca6ad9
AO
1332 10, /* bitpos */
1333 complain_overflow_dont, /* complain_on_overflow */
1334 bfd_elf_generic_reloc, /* special_function */
067653c5 1335 "R_SH_GOTPC_LOW16", /* name */
b34976b6 1336 FALSE, /* partial_inplace */
fbca6ad9
AO
1337 0, /* src_mask */
1338 0x3fffc00, /* dst_mask */
b34976b6 1339 TRUE), /* pcrel_offset */
fbca6ad9
AO
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 */
b34976b6 1346 TRUE, /* pc_relative */
fbca6ad9
AO
1347 10, /* bitpos */
1348 complain_overflow_dont, /* complain_on_overflow */
1349 bfd_elf_generic_reloc, /* special_function */
1350 "R_SH_GOTPC_MEDLOW16", /* name */
b34976b6 1351 FALSE, /* partial_inplace */
fbca6ad9
AO
1352 0, /* src_mask */
1353 0x3fffc00, /* dst_mask */
b34976b6 1354 TRUE), /* pcrel_offset */
fbca6ad9
AO
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 */
b34976b6 1361 TRUE, /* pc_relative */
fbca6ad9
AO
1362 10, /* bitpos */
1363 complain_overflow_dont, /* complain_on_overflow */
1364 bfd_elf_generic_reloc, /* special_function */
1365 "R_SH_GOTPC_MEDHI16", /* name */
b34976b6 1366 FALSE, /* partial_inplace */
fbca6ad9
AO
1367 0, /* src_mask */
1368 0x3fffc00, /* dst_mask */
b34976b6 1369 TRUE), /* pcrel_offset */
fbca6ad9
AO
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 */
b34976b6 1376 TRUE, /* pc_relative */
fbca6ad9
AO
1377 10, /* bitpos */
1378 complain_overflow_dont, /* complain_on_overflow */
1379 bfd_elf_generic_reloc, /* special_function */
1380 "R_SH_GOTPC_HI16", /* name */
b34976b6 1381 FALSE, /* partial_inplace */
fbca6ad9
AO
1382 0, /* src_mask */
1383 0x3fffc00, /* dst_mask */
b34976b6 1384 TRUE), /* pcrel_offset */
fbca6ad9 1385
067653c5 1386 /* Used in LD.L, FLD.S et al. */
fbca6ad9
AO
1387 HOWTO (R_SH_GOT10BY4, /* type */
1388 2, /* rightshift */
1389 2, /* size (0 = byte, 1 = short, 2 = long) */
1390 12, /* bitsize */
b34976b6 1391 FALSE, /* pc_relative */
fbca6ad9
AO
1392 10, /* bitpos */
1393 complain_overflow_signed, /* complain_on_overflow */
1394 bfd_elf_generic_reloc, /* special_function */
1395 "R_SH_GOT10BY4", /* name */
b34976b6 1396 FALSE, /* partial_inplace */
fbca6ad9
AO
1397 0, /* src_mask */
1398 0xffc00, /* dst_mask */
b34976b6 1399 FALSE), /* pcrel_offset */
fbca6ad9 1400
067653c5 1401 /* Used in LD.L, FLD.S et al. */
fbca6ad9
AO
1402 HOWTO (R_SH_GOTPLT10BY4, /* type */
1403 2, /* rightshift */
1404 2, /* size (0 = byte, 1 = short, 2 = long) */
1405 12, /* bitsize */
b34976b6 1406 FALSE, /* pc_relative */
fbca6ad9
AO
1407 10, /* bitpos */
1408 complain_overflow_signed, /* complain_on_overflow */
1409 bfd_elf_generic_reloc, /* special_function */
1410 "R_SH_GOTPLT10BY4", /* name */
b34976b6 1411 FALSE, /* partial_inplace */
fbca6ad9
AO
1412 0, /* src_mask */
1413 0xffc00, /* dst_mask */
b34976b6 1414 FALSE), /* pcrel_offset */
fbca6ad9
AO
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 */
b34976b6 1421 FALSE, /* pc_relative */
fbca6ad9
AO
1422 10, /* bitpos */
1423 complain_overflow_signed, /* complain_on_overflow */
1424 bfd_elf_generic_reloc, /* special_function */
1425 "R_SH_GOT10BY8", /* name */
b34976b6 1426 FALSE, /* partial_inplace */
fbca6ad9
AO
1427 0, /* src_mask */
1428 0xffc00, /* dst_mask */
b34976b6 1429 FALSE), /* pcrel_offset */
fbca6ad9
AO
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 */
b34976b6 1436 FALSE, /* pc_relative */
fbca6ad9
AO
1437 10, /* bitpos */
1438 complain_overflow_signed, /* complain_on_overflow */
1439 bfd_elf_generic_reloc, /* special_function */
1440 "R_SH_GOTPLT10BY8", /* name */
b34976b6 1441 FALSE, /* partial_inplace */
fbca6ad9
AO
1442 0, /* src_mask */
1443 0xffc00, /* dst_mask */
b34976b6 1444 FALSE), /* pcrel_offset */
fbca6ad9
AO
1445
1446 HOWTO (R_SH_COPY64, /* type */
1447 0, /* rightshift */
1448 4, /* size (0 = byte, 1 = short, 2 = long) */
1449 64, /* bitsize */
b34976b6 1450 FALSE, /* pc_relative */
fbca6ad9
AO
1451 0, /* bitpos */
1452 complain_overflow_dont, /* complain_on_overflow */
1453 bfd_elf_generic_reloc, /* special_function */
067653c5 1454 "R_SH_COPY64", /* name */
b34976b6 1455 FALSE, /* partial_inplace */
fbca6ad9
AO
1456 0, /* src_mask */
1457 ((bfd_vma) 0) - 1, /* dst_mask */
b34976b6 1458 FALSE), /* pcrel_offset */
fbca6ad9
AO
1459
1460 HOWTO (R_SH_GLOB_DAT64, /* type */
1461 0, /* rightshift */
1462 4, /* size (0 = byte, 1 = short, 2 = long) */
1463 64, /* bitsize */
b34976b6 1464 FALSE, /* pc_relative */
fbca6ad9
AO
1465 0, /* bitpos */
1466 complain_overflow_dont, /* complain_on_overflow */
1467 bfd_elf_generic_reloc, /* special_function */
067653c5 1468 "R_SH_GLOB_DAT64", /* name */
b34976b6 1469 FALSE, /* partial_inplace */
fbca6ad9
AO
1470 0, /* src_mask */
1471 ((bfd_vma) 0) - 1, /* dst_mask */
b34976b6 1472 FALSE), /* pcrel_offset */
fbca6ad9
AO
1473
1474 HOWTO (R_SH_JMP_SLOT64, /* type */
1475 0, /* rightshift */
1476 4, /* size (0 = byte, 1 = short, 2 = long) */
1477 64, /* bitsize */
b34976b6 1478 FALSE, /* pc_relative */
fbca6ad9
AO
1479 0, /* bitpos */
1480 complain_overflow_dont, /* complain_on_overflow */
1481 bfd_elf_generic_reloc, /* special_function */
067653c5 1482 "R_SH_JMP_SLOT64", /* name */
b34976b6 1483 FALSE, /* partial_inplace */
fbca6ad9
AO
1484 0, /* src_mask */
1485 ((bfd_vma) 0) - 1, /* dst_mask */
b34976b6 1486 FALSE), /* pcrel_offset */
fbca6ad9
AO
1487
1488 HOWTO (R_SH_RELATIVE64, /* type */
1489 0, /* rightshift */
1490 4, /* size (0 = byte, 1 = short, 2 = long) */
1491 64, /* bitsize */
b34976b6 1492 FALSE, /* pc_relative */
fbca6ad9
AO
1493 0, /* bitpos */
1494 complain_overflow_dont, /* complain_on_overflow */
1495 bfd_elf_generic_reloc, /* special_function */
067653c5 1496 "R_SH_RELATIVE64", /* name */
b34976b6 1497 FALSE, /* partial_inplace */
fbca6ad9
AO
1498 0, /* src_mask */
1499 ((bfd_vma) 0) - 1, /* dst_mask */
b34976b6 1500 FALSE), /* pcrel_offset */
fbca6ad9
AO
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 */
b34976b6 1561 FALSE, /* pc_relative */
fbca6ad9
AO
1562 0, /* bitpos */
1563 complain_overflow_unsigned, /* complain_on_overflow */
1564 sh_elf_ignore_reloc, /* special_function */
1565 "R_SH_SHMEDIA_CODE", /* name */
b34976b6 1566 FALSE, /* partial_inplace */
fbca6ad9
AO
1567 0, /* src_mask */
1568 0, /* dst_mask */
b34976b6 1569 FALSE), /* pcrel_offset */
fbca6ad9
AO
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 */
b34976b6 1578 TRUE, /* pc_relative */
fbca6ad9
AO
1579 10, /* bitpos */
1580 complain_overflow_signed, /* complain_on_overflow */
1581 bfd_elf_generic_reloc, /* special_function */
1582 "R_SH_PT_16", /* name */
b34976b6 1583 FALSE, /* partial_inplace */
fbca6ad9
AO
1584 0, /* src_mask */
1585 0x3fffc00, /* dst_mask */
b34976b6 1586 TRUE), /* pcrel_offset */
fbca6ad9
AO
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 */
b34976b6 1593 FALSE, /* pc_relative */
fbca6ad9
AO
1594 10, /* bitpos */
1595 complain_overflow_signed, /* complain_on_overflow */
1596 bfd_elf_generic_reloc, /* special_function */
1597 "R_SH_IMMS16", /* name */
b34976b6 1598 FALSE, /* partial_inplace */
fbca6ad9
AO
1599 0, /* src_mask */
1600 0x3fffc00, /* dst_mask */
b34976b6 1601 FALSE), /* pcrel_offset */
fbca6ad9
AO
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 */
b34976b6 1608 FALSE, /* pc_relative */
fbca6ad9
AO
1609 10, /* bitpos */
1610 complain_overflow_unsigned, /* complain_on_overflow */
1611 bfd_elf_generic_reloc, /* special_function */
1612 "R_SH_IMMU16", /* name */
b34976b6 1613 FALSE, /* partial_inplace */
fbca6ad9
AO
1614 0, /* src_mask */
1615 0x3fffc00, /* dst_mask */
b34976b6 1616 FALSE), /* pcrel_offset */
fbca6ad9
AO
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 */
b34976b6 1623 FALSE, /* pc_relative */
fbca6ad9
AO
1624 10, /* bitpos */
1625 complain_overflow_dont, /* complain_on_overflow */
1626 bfd_elf_generic_reloc, /* special_function */
067653c5 1627 "R_SH_IMM_LOW16", /* name */
b34976b6 1628 FALSE, /* partial_inplace */
fbca6ad9
AO
1629 0, /* src_mask */
1630 0x3fffc00, /* dst_mask */
b34976b6 1631 FALSE), /* pcrel_offset */
fbca6ad9
AO
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 */
b34976b6 1638 TRUE, /* pc_relative */
fbca6ad9
AO
1639 10, /* bitpos */
1640 complain_overflow_dont, /* complain_on_overflow */
1641 bfd_elf_generic_reloc, /* special_function */
1642 "R_SH_IMM_LOW16_PCREL", /* name */
b34976b6 1643 FALSE, /* partial_inplace */
fbca6ad9
AO
1644 0, /* src_mask */
1645 0x3fffc00, /* dst_mask */
b34976b6 1646 TRUE), /* pcrel_offset */
fbca6ad9
AO
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 */
b34976b6 1653 FALSE, /* pc_relative */
fbca6ad9
AO
1654 10, /* bitpos */
1655 complain_overflow_dont, /* complain_on_overflow */
1656 bfd_elf_generic_reloc, /* special_function */
1657 "R_SH_IMM_MEDLOW16", /* name */
b34976b6 1658 FALSE, /* partial_inplace */
fbca6ad9
AO
1659 0, /* src_mask */
1660 0x3fffc00, /* dst_mask */
b34976b6 1661 FALSE), /* pcrel_offset */
fbca6ad9
AO
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 */
b34976b6 1668 TRUE, /* pc_relative */
fbca6ad9
AO
1669 10, /* bitpos */
1670 complain_overflow_dont, /* complain_on_overflow */
1671 bfd_elf_generic_reloc, /* special_function */
1672 "R_SH_IMM_MEDLOW16_PCREL", /* name */
b34976b6 1673 FALSE, /* partial_inplace */
fbca6ad9
AO
1674 0, /* src_mask */
1675 0x3fffc00, /* dst_mask */
b34976b6 1676 TRUE), /* pcrel_offset */
fbca6ad9
AO
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 */
b34976b6 1683 FALSE, /* pc_relative */
fbca6ad9
AO
1684 10, /* bitpos */
1685 complain_overflow_dont, /* complain_on_overflow */
1686 bfd_elf_generic_reloc, /* special_function */
1687 "R_SH_IMM_MEDHI16", /* name */
b34976b6 1688 FALSE, /* partial_inplace */
fbca6ad9
AO
1689 0, /* src_mask */
1690 0x3fffc00, /* dst_mask */
b34976b6 1691 FALSE), /* pcrel_offset */
fbca6ad9
AO
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 */
b34976b6 1698 TRUE, /* pc_relative */
fbca6ad9
AO
1699 10, /* bitpos */
1700 complain_overflow_dont, /* complain_on_overflow */
1701 bfd_elf_generic_reloc, /* special_function */
1702 "R_SH_IMM_MEDHI16_PCREL", /* name */
b34976b6 1703 FALSE, /* partial_inplace */
fbca6ad9
AO
1704 0, /* src_mask */
1705 0x3fffc00, /* dst_mask */
b34976b6 1706 TRUE), /* pcrel_offset */
fbca6ad9
AO
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 */
b34976b6 1713 FALSE, /* pc_relative */
fbca6ad9
AO
1714 10, /* bitpos */
1715 complain_overflow_dont, /* complain_on_overflow */
1716 bfd_elf_generic_reloc, /* special_function */
1717 "R_SH_IMM_HI16", /* name */
b34976b6 1718 FALSE, /* partial_inplace */
fbca6ad9
AO
1719 0, /* src_mask */
1720 0x3fffc00, /* dst_mask */
b34976b6 1721 FALSE), /* pcrel_offset */
fbca6ad9
AO
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 */
b34976b6 1728 TRUE, /* pc_relative */
fbca6ad9
AO
1729 10, /* bitpos */
1730 complain_overflow_dont, /* complain_on_overflow */
1731 bfd_elf_generic_reloc, /* special_function */
1732 "R_SH_IMM_HI16_PCREL", /* name */
b34976b6 1733 FALSE, /* partial_inplace */
fbca6ad9
AO
1734 0, /* src_mask */
1735 0x3fffc00, /* dst_mask */
b34976b6 1736 TRUE), /* pcrel_offset */
fbca6ad9
AO
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 */
b34976b6 1743 FALSE, /* pc_relative */
fbca6ad9
AO
1744 0, /* bitpos */
1745 complain_overflow_dont, /* complain_on_overflow */
1746 bfd_elf_generic_reloc, /* special_function */
067653c5 1747 "R_SH_64", /* name */
b34976b6 1748 FALSE, /* partial_inplace */
fbca6ad9
AO
1749 0, /* src_mask */
1750 ((bfd_vma) 0) - 1, /* dst_mask */
b34976b6 1751 FALSE), /* pcrel_offset */
fbca6ad9
AO
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 */
b34976b6 1758 TRUE, /* pc_relative */
fbca6ad9
AO
1759 10, /* bitpos */
1760 complain_overflow_dont, /* complain_on_overflow */
1761 bfd_elf_generic_reloc, /* special_function */
1762 "R_SH_64_PCREL", /* name */
b34976b6 1763 FALSE, /* partial_inplace */
fbca6ad9
AO
1764 0, /* src_mask */
1765 ((bfd_vma) 0) - 1, /* dst_mask */
b34976b6 1766 TRUE), /* pcrel_offset */
fbca6ad9
AO
1767
1768#endif
252b5132
RH
1769};
1770
015551fc 1771static bfd_reloc_status_type
09fd220b
KK
1772sh_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)
015551fc
JR
1776{
1777 static bfd_vma last_addr;
00fdaf47 1778 static asection *last_symbol_section;
015551fc
JR
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. */
07515404 1785 if (addr > bfd_get_section_limit (input_bfd, input_section))
015551fc
JR
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 {
eea6121a
AM
1810 if (!bfd_malloc_and_get_section (input_bfd, symbol_section,
1811 &contents))
015551fc 1812 {
eea6121a
AM
1813 if (contents != NULL)
1814 free (contents);
015551fc
JR
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;
61ff1804 1826 diff = (last_ptr - ptr) >> 1;
015551fc
JR
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
a0fc8ba1 1842 while (start0 && IS_PPI (contents + start0))
015551fc
JR
1843 start0 -= 2;
1844 start0 = start - 2 - ((start - start0) & 2);
1845 start = start0 - cum_diff - 2;
1846 end = start0;
1847 }
1848
6cdc0ccc
AM
1849 if (contents != NULL
1850 && elf_section_data (symbol_section)->this_hdr.contents != contents)
1851 free (contents);
015551fc
JR
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
61ff1804 1864 x = (insn & ~0xff) | (x & 0xff);
dc810e39 1865 bfd_put_16 (input_bfd, (bfd_vma) x, contents + addr);
015551fc
JR
1866
1867 return bfd_reloc_ok;
1868}
1869
1870/* This function is used for normal relocs. This used to be like the COFF
252b5132
RH
1871 function, and is almost certainly incorrect for other ELF targets. */
1872
1873static bfd_reloc_status_type
09fd220b
KK
1874sh_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)
252b5132
RH
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. */
015551fc 1895 if (r_type == R_SH_IND12W && (symbol_in->flags & BSF_LOCAL) != 0)
252b5132
RH
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))
435b1e90
KH
1903 sym_value = 0;
1904 else
252b5132
RH
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;
dc810e39 1914 bfd_put_32 (abfd, (bfd_vma) insn, hit_data);
252b5132
RH
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);
dc810e39 1927 bfd_put_16 (abfd, (bfd_vma) insn, hit_data);
252b5132
RH
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
1942static bfd_reloc_status_type
09fd220b
KK
1943sh_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)
252b5132
RH
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
38b1a46c
NC
1956struct elf_reloc_map
1957{
252b5132
RH
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
38b1a46c
NC
1964static const struct elf_reloc_map sh_reloc_map[] =
1965{
252b5132
RH
1966 { BFD_RELOC_NONE, R_SH_NONE },
1967 { BFD_RELOC_32, R_SH_DIR32 },
d38eb334
DD
1968 { BFD_RELOC_16, R_SH_DIR16 },
1969 { BFD_RELOC_8, R_SH_DIR8 },
252b5132
RH
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 },
015551fc
JR
1987 { BFD_RELOC_SH_LOOP_START, R_SH_LOOP_START },
1988 { BFD_RELOC_SH_LOOP_END, R_SH_LOOP_END },
3376eaf5
KK
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 },
37c644f2
AO
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 },
fbca6ad9
AO
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 */
252b5132
RH
2057};
2058
2059/* Given a BFD reloc code, return the howto structure for the
2060 corresponding SH ELf reloc. */
2061
2062static reloc_howto_type *
09fd220b
KK
2063sh_elf_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
2064 bfd_reloc_code_real_type code)
252b5132
RH
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
2079static void
09fd220b
KK
2080sh_elf_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED, arelent *cache_ptr,
2081 Elf_Internal_Rela *dst)
252b5132
RH
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);
06bb75c1 2089 BFD_ASSERT (r < R_SH_FIRST_INVALID_RELOC_2 || r > R_SH_LAST_INVALID_RELOC_2);
fbca6ad9
AO
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);
3376eaf5 2092 BFD_ASSERT (r < R_SH_FIRST_INVALID_RELOC_5 || r > R_SH_LAST_INVALID_RELOC_5);
252b5132
RH
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
b34976b6 2107static bfd_boolean
09fd220b
KK
2108sh_elf_relax_section (bfd *abfd, asection *sec,
2109 struct bfd_link_info *link_info, bfd_boolean *again)
252b5132
RH
2110{
2111 Elf_Internal_Shdr *symtab_hdr;
2112 Elf_Internal_Rela *internal_relocs;
b34976b6 2113 bfd_boolean have_code;
252b5132
RH
2114 Elf_Internal_Rela *irel, *irelend;
2115 bfd_byte *contents = NULL;
6cdc0ccc 2116 Elf_Internal_Sym *isymbuf = NULL;
252b5132 2117
b34976b6 2118 *again = FALSE;
252b5132 2119
1049f94e 2120 if (link_info->relocatable
252b5132
RH
2121 || (sec->flags & SEC_RELOC) == 0
2122 || sec->reloc_count == 0)
b34976b6 2123 return TRUE;
252b5132 2124
fbca6ad9
AO
2125#ifdef INCLUDE_SHMEDIA
2126 if (elf_section_data (sec)->this_hdr.sh_flags
2127 & (SHF_SH5_ISA32 | SHF_SH5_ISA32_MIXED))
2128 {
b34976b6 2129 return TRUE;
fbca6ad9
AO
2130 }
2131#endif
2132
252b5132
RH
2133 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2134
45d6a902 2135 internal_relocs = (_bfd_elf_link_read_relocs
09fd220b 2136 (abfd, sec, NULL, (Elf_Internal_Rela *) NULL,
252b5132
RH
2137 link_info->keep_memory));
2138 if (internal_relocs == NULL)
2139 goto error_return;
252b5132 2140
b34976b6 2141 have_code = FALSE;
252b5132
RH
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)
b34976b6 2152 have_code = TRUE;
252b5132
RH
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 {
eea6121a 2164 if (!bfd_malloc_and_get_section (abfd, sec, &contents))
252b5132
RH
2165 goto error_return;
2166 }
2167 }
2168
2169 /* The r_addend field of the R_SH_USES reloc will point us to
067653c5
AM
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. */
252b5132 2173 laddr = irel->r_offset + 4 + irel->r_addend;
eea6121a 2174 if (laddr >= sec->size)
252b5132 2175 {
d003868e
AM
2176 (*_bfd_error_handler) (_("%B: 0x%lx: warning: bad R_SH_USES offset"),
2177 abfd,
252b5132
RH
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
067653c5 2184 do. */
252b5132
RH
2185 if ((insn & 0xf000) != 0xd000)
2186 {
2187 ((*_bfd_error_handler)
d003868e
AM
2188 (_("%B: 0x%lx: warning: R_SH_USES points to unrecognized insn 0x%x"),
2189 abfd, (unsigned long) irel->r_offset, insn));
252b5132
RH
2190 continue;
2191 }
2192
2193 /* Get the address from which the register is being loaded. The
99e4ae17
AJ
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. */
252b5132
RH
2199 paddr = insn & 0xff;
2200 paddr *= 4;
dc810e39 2201 paddr += (laddr + 4) &~ (bfd_vma) 3;
eea6121a 2202 if (paddr >= sec->size)
252b5132
RH
2203 {
2204 ((*_bfd_error_handler)
d003868e
AM
2205 (_("%B: 0x%lx: warning: bad R_SH_USES load offset"),
2206 abfd, (unsigned long) irel->r_offset));
252b5132
RH
2207 continue;
2208 }
2209
2210 /* Get the reloc for the address from which the register is
067653c5
AM
2211 being loaded. This reloc will tell us which function is
2212 actually being called. */
252b5132
RH
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)
d003868e
AM
2220 (_("%B: 0x%lx: warning: could not find expected reloc"),
2221 abfd, (unsigned long) paddr));
252b5132
RH
2222 continue;
2223 }
2224
2225 /* Read this BFD's symbols if we haven't done so already. */
6cdc0ccc 2226 if (isymbuf == NULL && symtab_hdr->sh_info != 0)
252b5132 2227 {
6cdc0ccc
AM
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;
252b5132
RH
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 {
9ad5cbcf 2240 /* A local symbol. */
6cdc0ccc 2241 Elf_Internal_Sym *isym;
252b5132 2242
6cdc0ccc
AM
2243 isym = isymbuf + ELF32_R_SYM (irelfn->r_info);
2244 if (isym->st_shndx
d426c6b0 2245 != (unsigned int) _bfd_elf_section_from_bfd_section (abfd, sec))
252b5132
RH
2246 {
2247 ((*_bfd_error_handler)
d003868e
AM
2248 (_("%B: 0x%lx: warning: symbol in unexpected section"),
2249 abfd, (unsigned long) paddr));
252b5132
RH
2250 continue;
2251 }
2252
6cdc0ccc 2253 symval = (isym->st_value
252b5132
RH
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
067653c5
AM
2269 symbol. Just ignore it--it will be caught by the
2270 regular reloc processing. */
252b5132
RH
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;
252b5132 2304 elf_section_data (sec)->this_hdr.contents = contents;
6cdc0ccc 2305 symtab_hdr->contents = (unsigned char *) isymbuf;
252b5132
RH
2306
2307 /* Replace the jsr with a bsr. */
2308
2309 /* Change the R_SH_USES reloc into an R_SH_IND12W reloc, and
067653c5 2310 replace the jsr with a bsr. */
252b5132 2311 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irelfn->r_info), R_SH_IND12W);
bdfaef52
JR
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. */
0e71e495
BE
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
bdfaef52 2323 irel->r_addend = -4;
252b5132
RH
2324
2325 /* See if there is another R_SH_USES reloc referring to the same
067653c5 2326 register load. */
252b5132
RH
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
067653c5
AM
2341 function address is stored. Do this before deleting any
2342 bytes, to avoid confusion about the address. */
252b5132
RH
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
067653c5
AM
2353 other function call to come within range, we should relax
2354 again. Note that this is not required, and it may be slow. */
b34976b6 2355 *again = TRUE;
252b5132
RH
2356
2357 /* Now check whether we got a COUNT reloc. */
2358 if (irelcount >= irelend)
2359 {
2360 ((*_bfd_error_handler)
d003868e
AM
2361 (_("%B: 0x%lx: warning: could not find expected COUNT reloc"),
2362 abfd, (unsigned long) paddr));
252b5132
RH
2363 continue;
2364 }
2365
2366 /* The number of uses is stored in the r_addend field. We've
067653c5 2367 just deleted one. */
252b5132
RH
2368 if (irelcount->r_addend == 0)
2369 {
d003868e
AM
2370 ((*_bfd_error_handler) (_("%B: 0x%lx: warning: bad count"),
2371 abfd,
252b5132
RH
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
067653c5
AM
2379 the address from irelfn, in case it was changed by the
2380 previous call to sh_elf_relax_delete_bytes. */
252b5132
RH
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. */
bdfaef52
JR
2392 if ((elf_elfheader (abfd)->e_flags & EF_SH_MACH_MASK) != EF_SH4
2393 && have_code)
252b5132 2394 {
b34976b6 2395 bfd_boolean swapped;
252b5132
RH
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 {
eea6121a 2404 if (!bfd_malloc_and_get_section (abfd, sec, &contents))
252b5132
RH
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;
252b5132 2416 elf_section_data (sec)->this_hdr.contents = contents;
6cdc0ccc 2417 symtab_hdr->contents = (unsigned char *) isymbuf;
252b5132
RH
2418 }
2419 }
2420
6cdc0ccc
AM
2421 if (isymbuf != NULL
2422 && symtab_hdr->contents != (unsigned char *) isymbuf)
252b5132
RH
2423 {
2424 if (! link_info->keep_memory)
6cdc0ccc 2425 free (isymbuf);
252b5132
RH
2426 else
2427 {
6cdc0ccc
AM
2428 /* Cache the symbols for elf_link_input_bfd. */
2429 symtab_hdr->contents = (unsigned char *) isymbuf;
252b5132 2430 }
9ad5cbcf
AM
2431 }
2432
6cdc0ccc
AM
2433 if (contents != NULL
2434 && elf_section_data (sec)->this_hdr.contents != contents)
252b5132
RH
2435 {
2436 if (! link_info->keep_memory)
6cdc0ccc
AM
2437 free (contents);
2438 else
252b5132 2439 {
6cdc0ccc
AM
2440 /* Cache the section contents for elf_link_input_bfd. */
2441 elf_section_data (sec)->this_hdr.contents = contents;
252b5132 2442 }
252b5132
RH
2443 }
2444
6cdc0ccc
AM
2445 if (internal_relocs != NULL
2446 && elf_section_data (sec)->relocs != internal_relocs)
2447 free (internal_relocs);
2448
b34976b6 2449 return TRUE;
252b5132
RH
2450
2451 error_return:
6cdc0ccc
AM
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);
9ad5cbcf 2461
b34976b6 2462 return FALSE;
252b5132
RH
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
b34976b6 2469static bfd_boolean
09fd220b
KK
2470sh_elf_relax_delete_bytes (bfd *abfd, asection *sec, bfd_vma addr,
2471 int count)
252b5132
RH
2472{
2473 Elf_Internal_Shdr *symtab_hdr;
9ad5cbcf 2474 unsigned int sec_shndx;
252b5132
RH
2475 bfd_byte *contents;
2476 Elf_Internal_Rela *irel, *irelend;
2477 Elf_Internal_Rela *irelalign;
2478 bfd_vma toaddr;
6cdc0ccc 2479 Elf_Internal_Sym *isymbuf, *isym, *isymend;
9ad5cbcf
AM
2480 struct elf_link_hash_entry **sym_hashes;
2481 struct elf_link_hash_entry **end_hashes;
2482 unsigned int symcount;
252b5132
RH
2483 asection *o;
2484
2485 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
6cdc0ccc 2486 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
9ad5cbcf
AM
2487
2488 sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
252b5132
RH
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;
eea6121a 2496 toaddr = sec->size;
252b5132
RH
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. */
dc810e39
AM
2513 memmove (contents + addr, contents + addr + count,
2514 (size_t) (toaddr - addr - count));
252b5132 2515 if (irelalign == NULL)
eea6121a 2516 sec->size -= count;
252b5132
RH
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)
dc810e39 2525 bfd_put_16 (abfd, (bfd_vma) NOP_OPCODE, contents + toaddr - count + i);
252b5132
RH
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;
252b5132
RH
2534 int off, adjust, oinsn;
2535 bfd_signed_vma voff = 0;
b34976b6 2536 bfd_boolean overflow;
252b5132
RH
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
067653c5 2559 includes the bytes we have deleted. */
252b5132
RH
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
067653c5
AM
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. */
252b5132
RH
2585 if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
2586 {
6cdc0ccc
AM
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))
252b5132
RH
2591 {
2592 bfd_vma val;
2593
2594 val = bfd_get_32 (abfd, contents + nraddr);
6cdc0ccc 2595 val += isym->st_value;
252b5132
RH
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:
bdfaef52
JR
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 }
252b5132
RH
2619 else
2620 {
252b5132
RH
2621 if (off & 0x800)
2622 off -= 0x1000;
2623 stop = (bfd_vma) ((bfd_signed_vma) start + 4 + off * 2);
bdfaef52
JR
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.
4cc11e76 2628 N.B. If we want to abandon in-place changes here and
bdfaef52
JR
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;
252b5132
RH
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;
435b1e90 2643 stop = (start & ~(bfd_vma) 3) + 4 + off * 4;
252b5132
RH
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
06e1ba78 2651 The r_addend field holds the difference between the reloc
252b5132
RH
2652 address and L1. That is the start of the reloc, and
2653 adding in the contents gives us the top. We must adjust
06e1ba78
JR
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. */
252b5132 2657
06e1ba78
JR
2658 stop = irel->r_offset;
2659 start = (bfd_vma) ((bfd_signed_vma) stop - (long) irel->r_addend);
252b5132
RH
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
252b5132
RH
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;
b34976b6 2702 overflow = FALSE;
252b5132
RH
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))
b34976b6 2713 overflow = TRUE;
dc810e39 2714 bfd_put_16 (abfd, (bfd_vma) insn, contents + nraddr);
252b5132
RH
2715 break;
2716
2717 case R_SH_IND12W:
2718 insn += adjust / 2;
2719 if ((oinsn & 0xf000) != (insn & 0xf000))
b34976b6 2720 overflow = TRUE;
dc810e39 2721 bfd_put_16 (abfd, (bfd_vma) insn, contents + nraddr);
252b5132
RH
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))
b34976b6 2734 overflow = TRUE;
dc810e39 2735 bfd_put_16 (abfd, (bfd_vma) insn, contents + nraddr);
252b5132
RH
2736 break;
2737
851cde10
JR
2738 case R_SH_SWITCH8:
2739 voff += adjust;
2740 if (voff < 0 || voff >= 0xff)
b34976b6 2741 overflow = TRUE;
851cde10
JR
2742 bfd_put_8 (abfd, voff, contents + nraddr);
2743 break;
2744
252b5132
RH
2745 case R_SH_SWITCH16:
2746 voff += adjust;
2747 if (voff < - 0x8000 || voff >= 0x8000)
b34976b6 2748 overflow = TRUE;
dc810e39 2749 bfd_put_signed_16 (abfd, (bfd_vma) voff, contents + nraddr);
252b5132
RH
2750 break;
2751
2752 case R_SH_SWITCH32:
2753 voff += adjust;
dc810e39 2754 bfd_put_signed_32 (abfd, (bfd_vma) voff, contents + nraddr);
252b5132
RH
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)
d003868e
AM
2765 (_("%B: 0x%lx: fatal: reloc overflow while relaxing"),
2766 abfd, (unsigned long) irel->r_offset));
252b5132 2767 bfd_set_error (bfd_error_bad_value);
b34976b6 2768 return FALSE;
252b5132
RH
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
b34976b6 2790 FALSE, we should free them, if we are permitted to, when we
067653c5 2791 leave sh_coff_relax_section. */
45d6a902 2792 internal_relocs = (_bfd_elf_link_read_relocs
09fd220b 2793 (abfd, o, NULL, (Elf_Internal_Rela *) NULL, TRUE));
252b5132 2794 if (internal_relocs == NULL)
b34976b6 2795 return FALSE;
252b5132
RH
2796
2797 ocontents = NULL;
2798 irelscanend = internal_relocs + o->reloc_count;
2799 for (irelscan = internal_relocs; irelscan < irelscanend; irelscan++)
2800 {
084aa3aa
JR
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.
b34976b6 2814 Perhaps, if info->keep_memory is FALSE, we
067653c5
AM
2815 should free them, if we are permitted to,
2816 when we leave sh_coff_relax_section. */
eea6121a
AM
2817 if (!bfd_malloc_and_get_section (abfd, o, &ocontents))
2818 {
2819 if (ocontents != NULL)
2820 free (ocontents);
2821 return FALSE;
2822 }
2823
084aa3aa
JR
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))
dc810e39 2842 bfd_put_signed_32 (abfd, (bfd_vma) voff + count,
084aa3aa
JR
2843 ocontents + irelscan->r_offset);
2844 else if (stop > addr
2845 && stop < toaddr
2846 && (start <= addr || start >= toaddr))
dc810e39 2847 bfd_put_signed_32 (abfd, (bfd_vma) voff - count,
084aa3aa
JR
2848 ocontents + irelscan->r_offset);
2849 }
2850
252b5132
RH
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
252b5132 2857
6cdc0ccc
AM
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))
252b5132
RH
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.
b34976b6 2872 Perhaps, if info->keep_memory is FALSE, we
067653c5
AM
2873 should free them, if we are permitted to,
2874 when we leave sh_coff_relax_section. */
eea6121a
AM
2875 if (!bfd_malloc_and_get_section (abfd, o, &ocontents))
2876 {
2877 if (ocontents != NULL)
2878 free (ocontents);
2879 return FALSE;
2880 }
2881
252b5132
RH
2882 elf_section_data (o)->this_hdr.contents = ocontents;
2883 }
2884 }
2885
2886 val = bfd_get_32 (abfd, ocontents + irelscan->r_offset);
6cdc0ccc 2887 val += isym->st_value;
252b5132
RH
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. */
6cdc0ccc
AM
2896 isymend = isymbuf + symtab_hdr->sh_info;
2897 for (isym = isymbuf; isym < isymend; isym++)
252b5132 2898 {
6cdc0ccc
AM
2899 if (isym->st_shndx == sec_shndx
2900 && isym->st_value > addr
2901 && isym->st_value < toaddr)
2902 isym->st_value -= count;
252b5132
RH
2903 }
2904
2905 /* Now adjust the global symbols defined in this section. */
9ad5cbcf
AM
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++)
252b5132 2911 {
9ad5cbcf
AM
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)
252b5132 2918 {
9ad5cbcf 2919 sym_hash->root.u.def.value -= count;
252b5132
RH
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,
dc810e39 2936 (int) (alignto - alignaddr));
252b5132
RH
2937 }
2938 }
2939
b34976b6 2940 return TRUE;
252b5132
RH
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
b34976b6 2946static bfd_boolean
09fd220b
KK
2947sh_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)
252b5132
RH
2951{
2952 Elf_Internal_Rela *irel, *irelend;
2953 bfd_vma *labels = NULL;
2954 bfd_vma *label, *label_end;
dc810e39 2955 bfd_size_type amt;
252b5132 2956
b34976b6 2957 *pswapped = FALSE;
252b5132
RH
2958
2959 irelend = internal_relocs + sec->reloc_count;
2960
2961 /* Get all the addresses with labels on them. */
dc810e39
AM
2962 amt = sec->reloc_count;
2963 amt *= sizeof (bfd_vma);
2964 labels = (bfd_vma *) bfd_malloc (amt);
252b5132
RH
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
eea6121a 2998 stop = sec->size;
252b5132
RH
2999
3000 if (! _bfd_sh_align_load_span (abfd, sec, contents, sh_elf_swap_insns,
09fd220b 3001 internal_relocs, &label,
252b5132
RH
3002 label_end, start, stop, pswapped))
3003 goto error_return;
3004 }
3005
3006 free (labels);
3007
b34976b6 3008 return TRUE;
252b5132
RH
3009
3010 error_return:
3011 if (labels != NULL)
3012 free (labels);
b34976b6 3013 return FALSE;
252b5132
RH
3014}
3015
85fbca6a 3016#ifndef SH64_ELF
252b5132
RH
3017/* Swap two SH instructions. This is like sh_swap_insns in coff-sh.c. */
3018
b34976b6 3019static bfd_boolean
09fd220b
KK
3020sh_elf_swap_insns (bfd *abfd, asection *sec, void *relocs,
3021 bfd_byte *contents, bfd_vma addr)
252b5132
RH
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);
dc810e39
AM
3030 bfd_put_16 (abfd, (bfd_vma) i2, contents + addr);
3031 bfd_put_16 (abfd, (bfd_vma) i1, contents + addr + 2);
252b5132
RH
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
067653c5
AM
3041 adjust. These relocs do not apply to the instruction itself,
3042 but are only associated with the address. */
252b5132
RH
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
067653c5
AM
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). */
252b5132
RH
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;
b34976b6 3084 bfd_boolean overflow;
252b5132
RH
3085
3086 loc = contents + irel->r_offset;
b34976b6 3087 overflow = FALSE;
252b5132
RH
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))
b34976b6 3099 overflow = TRUE;
dc810e39 3100 bfd_put_16 (abfd, (bfd_vma) insn, loc);
252b5132
RH
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))
b34976b6 3108 overflow = TRUE;
dc810e39 3109 bfd_put_16 (abfd, (bfd_vma) insn, loc);
252b5132
RH
3110 break;
3111
3112 case R_SH_DIR8WPL:
3113 /* This reloc ignores the least significant 3 bits of
067653c5
AM
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. */
252b5132
RH
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))
b34976b6 3125 overflow = TRUE;
dc810e39 3126 bfd_put_16 (abfd, (bfd_vma) insn, loc);
252b5132
RH
3127 }
3128
3129 break;
3130 }
3131
3132 if (overflow)
3133 {
3134 ((*_bfd_error_handler)
d003868e
AM
3135 (_("%B: 0x%lx: fatal: reloc overflow while relaxing"),
3136 abfd, (unsigned long) irel->r_offset));
252b5132 3137 bfd_set_error (bfd_error_bad_value);
b34976b6 3138 return FALSE;
252b5132
RH
3139 }
3140 }
3141 }
3142
b34976b6 3143 return TRUE;
252b5132 3144}
85fbca6a 3145#endif /* defined SH64_ELF */
252b5132 3146\f
fbca6ad9
AO
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
3155static 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 */
c8614e8e 3160 0x6b, 0xf1, 0x66, 0x00, /* ptabs r25, tr0 */
fbca6ad9
AO
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
3175static 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 */
c8614e8e 3180 0x00, 0x66, 0xf1, 0x6b, /* ptabs r25, tr0 */
fbca6ad9
AO
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
3198static 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
3218static 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
3240static 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 */
0a4ef3f4 3251 0x00, 0xc8, 0x45, 0x10, /* add.l r12, r17, r17 */
fbca6ad9
AO
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
3260static 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 */
0a4ef3f4 3271 0x10, 0x45, 0xc8, 0x00, /* add.l r12, r17, r17 */
fbca6ad9
AO
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
3280static const bfd_byte *elf_sh_plt0_entry;
3281static const bfd_byte *elf_sh_plt_entry;
3282static 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
3302inline static void
f075ee0c 3303movi_shori_putval (bfd *output_bfd, unsigned long value, bfd_byte *addr)
fbca6ad9
AO
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
37c644f2 3316/* The size in bytes of an entry in the procedure linkage table. */
252b5132 3317
37c644f2
AO
3318#define PLT_ENTRY_SIZE 28
3319
3320/* First entry in an absolute procedure linkage table look like this. */
3321
6c426cf3 3322/* Note - this code has been "optimised" not to use r2. r2 is used by
4cc11e76 3323 GCC to return the address of large structures, so it should not be
6c426cf3
NC
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. */
3330static 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
3346static 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
3365static 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
3380static 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
3397static 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
3413static 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
37c644f2
AO
3429static const bfd_byte *elf_sh_plt0_entry;
3430static const bfd_byte *elf_sh_plt_entry;
3431static const bfd_byte *elf_sh_pic_plt_entry;
252b5132 3432
37c644f2
AO
3433/* Return size of a PLT entry. */
3434#define elf_sh_sizeof_plt(info) PLT_ENTRY_SIZE
252b5132 3435
37c644f2
AO
3436/* Return offset of the PLT0 address in an absolute PLT entry. */
3437#define elf_sh_plt_plt0_offset(info) 16
252b5132 3438
37c644f2
AO
3439/* Return offset of the linker in PLT0 entry. */
3440#define elf_sh_plt0_linker_offset(info) 20
9bdafcce 3441
37c644f2
AO
3442/* Return offset of the GOT id in PLT0 entry. */
3443#define elf_sh_plt0_gotid_offset(info) 24
9bdafcce 3444
4cc11e76 3445/* Return offset of the temporary in PLT entry */
37c644f2 3446#define elf_sh_plt_temp_offset(info) 8
9bdafcce 3447
37c644f2
AO
3448/* Return offset of the symbol in PLT entry. */
3449#define elf_sh_plt_symbol_offset(info) 20
9bdafcce 3450
37c644f2
AO
3451/* Return offset of the relocation in PLT entry. */
3452#define elf_sh_plt_reloc_offset(info) 24
fbca6ad9 3453#endif
015551fc 3454
37c644f2 3455/* The sh linker needs to keep track of the number of relocs that it
067653c5
AM
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. */
252b5132 3460
067653c5 3461struct elf_sh_dyn_relocs
38b1a46c 3462{
067653c5
AM
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. */
37c644f2 3469 bfd_size_type count;
067653c5
AM
3470
3471 /* Number of pc-relative relocs copied for the input section. */
3472 bfd_size_type pc_count;
37c644f2 3473};
252b5132 3474
37c644f2
AO
3475/* sh ELF linker hash entry. */
3476
38b1a46c
NC
3477struct elf_sh_link_hash_entry
3478{
37c644f2
AO
3479 struct elf_link_hash_entry root;
3480
fbca6ad9 3481#ifdef INCLUDE_SHMEDIA
396a6083
SC
3482 union
3483 {
3484 bfd_signed_vma refcount;
3485 bfd_vma offset;
3486 } datalabel_got;
fbca6ad9
AO
3487#endif
3488
067653c5
AM
3489 /* Track dynamic relocs copied for this symbol. */
3490 struct elf_sh_dyn_relocs *dyn_relocs;
4989d792
SC
3491
3492 bfd_signed_vma gotplt_refcount;
3376eaf5
KK
3493
3494 enum {
3495 GOT_UNKNOWN = 0, GOT_NORMAL, GOT_TLS_GD, GOT_TLS_IE
3496 } tls_type;
3376eaf5
KK
3497};
3498
3499#define sh_elf_hash_entry(ent) ((struct elf_sh_link_hash_entry *)(ent))
3500
3501struct 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;
37c644f2
AO
3507};
3508
3376eaf5
KK
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
b34976b6 3518static bfd_boolean
09fd220b 3519sh_elf_mkobject (bfd *abfd)
3376eaf5 3520{
3376eaf5 3521 if (abfd->tdata.any == NULL)
62d7a5f6
AM
3522 {
3523 bfd_size_type amt = sizeof (struct sh_elf_obj_tdata);
3524 abfd->tdata.any = bfd_zalloc (abfd, amt);
3525 if (abfd->tdata.any == NULL)
3526 return FALSE;
3527 }
3528 return bfd_elf_mkobject (abfd);
3376eaf5 3529}
b34976b6 3530
37c644f2
AO
3531/* sh ELF linker hash table. */
3532
38b1a46c
NC
3533struct elf_sh_link_hash_table
3534{
37c644f2 3535 struct elf_link_hash_table root;
37c644f2 3536
067653c5
AM
3537 /* Short-cuts to get to dynamic linker sections. */
3538 asection *sgot;
3539 asection *sgotplt;
3540 asection *srelgot;
3541 asection *splt;
3542 asection *srelplt;
3543 asection *sdynbss;
3544 asection *srelbss;
37c644f2 3545
067653c5
AM
3546 /* Small local sym to section mapping cache. */
3547 struct sym_sec_cache sym_sec;
3376eaf5
KK
3548
3549 /* A counter or offset to track a TLS got entry. */
3550 union
3551 {
3552 bfd_signed_vma refcount;
3553 bfd_vma offset;
3554 } tls_ldm_got;
067653c5 3555};
37c644f2
AO
3556
3557/* Traverse an sh ELF linker hash table. */
3558
3559#define sh_elf_link_hash_traverse(table, func, info) \
3560 (elf_link_hash_traverse \
3561 (&(table)->root, \
09fd220b 3562 (bfd_boolean (*) (struct elf_link_hash_entry *, void *)) (func), \
37c644f2
AO
3563 (info)))
3564
3565/* Get the sh ELF linker hash table from a link_info structure. */
3566
3567#define sh_elf_hash_table(p) \
3568 ((struct elf_sh_link_hash_table *) ((p)->hash))
3569
3570/* Create an entry in an sh ELF linker hash table. */
3571
3572static struct bfd_hash_entry *
09fd220b
KK
3573sh_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
3574 struct bfd_hash_table *table,
3575 const char *string)
37c644f2
AO
3576{
3577 struct elf_sh_link_hash_entry *ret =
3578 (struct elf_sh_link_hash_entry *) entry;
3579
3580 /* Allocate the structure if it has not already been allocated by a
3581 subclass. */
3582 if (ret == (struct elf_sh_link_hash_entry *) NULL)
3583 ret = ((struct elf_sh_link_hash_entry *)
3584 bfd_hash_allocate (table,
3585 sizeof (struct elf_sh_link_hash_entry)));
3586 if (ret == (struct elf_sh_link_hash_entry *) NULL)
3587 return (struct bfd_hash_entry *) ret;
3588
3589 /* Call the allocation method of the superclass. */
3590 ret = ((struct elf_sh_link_hash_entry *)
3591 _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
3592 table, string));
3593 if (ret != (struct elf_sh_link_hash_entry *) NULL)
3594 {
396a6083
SC
3595 ret->dyn_relocs = NULL;
3596 ret->gotplt_refcount = 0;
fbca6ad9 3597#ifdef INCLUDE_SHMEDIA
396a6083 3598 ret->datalabel_got.refcount = ret->root.got.refcount;
fbca6ad9 3599#endif
3376eaf5 3600 ret->tls_type = GOT_UNKNOWN;
37c644f2
AO
3601 }
3602
3603 return (struct bfd_hash_entry *) ret;
3604}
3605
3606/* Create an sh ELF linker hash table. */
3607
3608static struct bfd_link_hash_table *
09fd220b 3609sh_elf_link_hash_table_create (bfd *abfd)
37c644f2
AO
3610{
3611 struct elf_sh_link_hash_table *ret;
dc810e39 3612 bfd_size_type amt = sizeof (struct elf_sh_link_hash_table);
37c644f2 3613
e2d34d7d 3614 ret = (struct elf_sh_link_hash_table *) bfd_malloc (amt);
37c644f2
AO
3615 if (ret == (struct elf_sh_link_hash_table *) NULL)
3616 return NULL;
3617
66eb6687
AM
3618 if (!_bfd_elf_link_hash_table_init (&ret->root, abfd,
3619 sh_elf_link_hash_newfunc,
3620 sizeof (struct elf_sh_link_hash_entry)))
37c644f2 3621 {
e2d34d7d 3622 free (ret);
37c644f2
AO
3623 return NULL;
3624 }
3625
067653c5
AM
3626 ret->sgot = NULL;
3627 ret->sgotplt = NULL;
3628 ret->srelgot = NULL;
3629 ret->splt = NULL;
3630 ret->srelplt = NULL;
3631 ret->sdynbss = NULL;
3632 ret->srelbss = NULL;
3633 ret->sym_sec.abfd = NULL;
3376eaf5 3634 ret->tls_ldm_got.refcount = 0;
067653c5 3635
37c644f2
AO
3636 return &ret->root.root;
3637}
3638
067653c5
AM
3639/* Create .got, .gotplt, and .rela.got sections in DYNOBJ, and set up
3640 shortcuts to them in our hash table. */
3641
b34976b6 3642static bfd_boolean
09fd220b 3643create_got_section (bfd *dynobj, struct bfd_link_info *info)
067653c5
AM
3644{
3645 struct elf_sh_link_hash_table *htab;
3646
3647 if (! _bfd_elf_create_got_section (dynobj, info))
b34976b6 3648 return FALSE;
067653c5
AM
3649
3650 htab = sh_elf_hash_table (info);
3651 htab->sgot = bfd_get_section_by_name (dynobj, ".got");
3652 htab->sgotplt = bfd_get_section_by_name (dynobj, ".got.plt");
3653 if (! htab->sgot || ! htab->sgotplt)
3654 abort ();
3655
3496cb2a
L
3656 htab->srelgot = bfd_make_section_with_flags (dynobj, ".rela.got",
3657 (SEC_ALLOC | SEC_LOAD
3658 | SEC_HAS_CONTENTS
3659 | SEC_IN_MEMORY
3660 | SEC_LINKER_CREATED
3661 | SEC_READONLY));
067653c5 3662 if (htab->srelgot == NULL
067653c5 3663 || ! bfd_set_section_alignment (dynobj, htab->srelgot, 2))
b34976b6
AM
3664 return FALSE;
3665 return TRUE;
067653c5
AM
3666}
3667
37c644f2
AO
3668/* Create dynamic sections when linking against a dynamic object. */
3669
b34976b6 3670static bfd_boolean
09fd220b 3671sh_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
37c644f2 3672{
067653c5 3673 struct elf_sh_link_hash_table *htab;
37c644f2
AO
3674 flagword flags, pltflags;
3675 register asection *s;
9c5bfbb7 3676 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
37c644f2
AO
3677 int ptralign = 0;
3678
3679 switch (bed->s->arch_size)
3680 {
3681 case 32:
3682 ptralign = 2;
3683 break;
3684
3685 case 64:
3686 ptralign = 3;
3687 break;
3688
3689 default:
3690 bfd_set_error (bfd_error_bad_value);
b34976b6 3691 return FALSE;
37c644f2
AO
3692 }
3693
067653c5 3694 htab = sh_elf_hash_table (info);
35cad4c4
KK
3695 if (htab->root.dynamic_sections_created)
3696 return TRUE;
067653c5 3697
37c644f2
AO
3698 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
3699 .rel[a].bss sections. */
3700
3701 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
3702 | SEC_LINKER_CREATED);
3703
3704 pltflags = flags;
3705 pltflags |= SEC_CODE;
3706 if (bed->plt_not_loaded)
3707 pltflags &= ~ (SEC_LOAD | SEC_HAS_CONTENTS);
3708 if (bed->plt_readonly)
3709 pltflags |= SEC_READONLY;
3710
3496cb2a 3711 s = bfd_make_section_with_flags (abfd, ".plt", pltflags);
35cad4c4
KK
3712 htab->splt = s;
3713 if (s == NULL
35cad4c4
KK
3714 || ! bfd_set_section_alignment (abfd, s, bed->plt_alignment))
3715 return FALSE;
37c644f2
AO
3716
3717 if (bed->want_plt_sym)
3718 {
3719 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
3720 .plt section. */
14a793b2
AM
3721 struct elf_link_hash_entry *h;
3722 struct bfd_link_hash_entry *bh = NULL;
3723
37c644f2
AO
3724 if (! (_bfd_generic_link_add_one_symbol
3725 (info, abfd, "_PROCEDURE_LINKAGE_TABLE_", BSF_GLOBAL, s,
b34976b6 3726 (bfd_vma) 0, (const char *) NULL, FALSE,
14a793b2 3727 get_elf_backend_data (abfd)->collect, &bh)))
b34976b6 3728 return FALSE;
14a793b2
AM
3729
3730 h = (struct elf_link_hash_entry *) bh;
f5385ebf 3731 h->def_regular = 1;
37c644f2 3732 h->type = STT_OBJECT;
7325306f 3733 htab->root.hplt = h;
37c644f2
AO
3734
3735 if (info->shared
c152c796 3736 && ! bfd_elf_link_record_dynamic_symbol (info, h))
b34976b6 3737 return FALSE;
37c644f2
AO
3738 }
3739
3496cb2a
L
3740 s = bfd_make_section_with_flags (abfd,
3741 bed->default_use_rela_p ? ".rela.plt" : ".rel.plt",
3742 flags | SEC_READONLY);
35cad4c4
KK
3743 htab->srelplt = s;
3744 if (s == NULL
35cad4c4
KK
3745 || ! bfd_set_section_alignment (abfd, s, ptralign))
3746 return FALSE;
37c644f2 3747
2293c499
AM
3748 if (htab->sgot == NULL
3749 && !create_got_section (abfd, info))
b34976b6 3750 return FALSE;
37c644f2
AO
3751
3752 {
3753 const char *secname;
3754 char *relname;
3755 flagword secflags;
3756 asection *sec;
3757
3758 for (sec = abfd->sections; sec; sec = sec->next)
3759 {
3760 secflags = bfd_get_section_flags (abfd, sec);
3761 if ((secflags & (SEC_DATA | SEC_LINKER_CREATED))
3762 || ((secflags & SEC_HAS_CONTENTS) != SEC_HAS_CONTENTS))
3763 continue;
3764 secname = bfd_get_section_name (abfd, sec);
dc810e39 3765 relname = (char *) bfd_malloc ((bfd_size_type) strlen (secname) + 6);
37c644f2
AO
3766 strcpy (relname, ".rela");
3767 strcat (relname, secname);
03151a5a
KK
3768 if (bfd_get_section_by_name (abfd, secname))
3769 continue;
3496cb2a
L
3770 s = bfd_make_section_with_flags (abfd, relname,
3771 flags | SEC_READONLY);
37c644f2 3772 if (s == NULL
37c644f2 3773 || ! bfd_set_section_alignment (abfd, s, ptralign))
b34976b6 3774 return FALSE;
37c644f2
AO
3775 }
3776 }
3777
3778 if (bed->want_dynbss)
3779 {
3780 /* The .dynbss section is a place to put symbols which are defined
3781 by dynamic objects, are referenced by regular objects, and are
3782 not functions. We must allocate space for them in the process
3783 image and use a R_*_COPY reloc to tell the dynamic linker to
3784 initialize them at run time. The linker script puts the .dynbss
3785 section into the .bss section of the final image. */
3496cb2a
L
3786 s = bfd_make_section_with_flags (abfd, ".dynbss",
3787 SEC_ALLOC | SEC_LINKER_CREATED);
35cad4c4 3788 htab->sdynbss = s;
3496cb2a 3789 if (s == NULL)
35cad4c4 3790 return FALSE;
37c644f2
AO
3791
3792 /* The .rel[a].bss section holds copy relocs. This section is not
3793 normally needed. We need to create it here, though, so that the
3794 linker will map it to an output section. We can't just create it
3795 only if we need it, because we will not know whether we need it
3796 until we have seen all the input files, and the first time the
3797 main linker code calls BFD after examining all the input files
3798 (size_dynamic_sections) the input sections have already been
3799 mapped to the output sections. If the section turns out not to
3800 be needed, we can discard it later. We will never need this
3801 section when generating a shared object, since they do not use
3802 copy relocs. */
35cad4c4 3803 if (! info->shared)
37c644f2 3804 {
3496cb2a
L
3805 s = bfd_make_section_with_flags (abfd,
3806 (bed->default_use_rela_p
3807 ? ".rela.bss" : ".rel.bss"),
3808 flags | SEC_READONLY);
067653c5 3809 htab->srelbss = s;
37c644f2 3810 if (s == NULL
37c644f2 3811 || ! bfd_set_section_alignment (abfd, s, ptralign))
b34976b6 3812 return FALSE;
37c644f2
AO
3813 }
3814 }
3815
b34976b6 3816 return TRUE;
37c644f2
AO
3817}
3818\f
37c644f2
AO
3819/* Adjust a symbol defined by a dynamic object and referenced by a
3820 regular object. The current definition is in some section of the
3821 dynamic object, but we're not including those sections. We have to
3822 change the definition to something the rest of the link can
3823 understand. */
3824
b34976b6 3825static bfd_boolean
09fd220b
KK
3826sh_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
3827 struct elf_link_hash_entry *h)
37c644f2 3828{
067653c5
AM
3829 struct elf_sh_link_hash_table *htab;
3830 struct elf_sh_link_hash_entry *eh;
3831 struct elf_sh_dyn_relocs *p;
37c644f2
AO
3832 asection *s;
3833 unsigned int power_of_two;
3834
2293c499 3835 htab = sh_elf_hash_table (info);
37c644f2
AO
3836
3837 /* Make sure we know what is going on here. */
2293c499 3838 BFD_ASSERT (htab->root.dynobj != NULL
f5385ebf 3839 && (h->needs_plt
f6e332e6 3840 || h->u.weakdef != NULL
f5385ebf
AM
3841 || (h->def_dynamic
3842 && h->ref_regular
3843 && !h->def_regular)));
37c644f2
AO
3844
3845 /* If this is a function, put it in the procedure linkage table. We
3846 will fill in the contents of the procedure linkage table later,
3847 when we know the address of the .got section. */
3848 if (h->type == STT_FUNC
f5385ebf 3849 || h->needs_plt)
37c644f2 3850 {
067653c5 3851 if (h->plt.refcount <= 0
3d85aebe
KK
3852 || SYMBOL_CALLS_LOCAL (info, h)
3853 || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
3854 && h->root.type == bfd_link_hash_undefweak))
37c644f2
AO
3855 {
3856 /* This case can occur if we saw a PLT reloc in an input
3857 file, but the symbol was never referred to by a dynamic
3858 object. In such a case, we don't actually need to build
3859 a procedure linkage table, and we can just do a REL32
3860 reloc instead. */
067653c5 3861 h->plt.offset = (bfd_vma) -1;
f5385ebf 3862 h->needs_plt = 0;
37c644f2
AO
3863 }
3864
b34976b6 3865 return TRUE;
37c644f2 3866 }
067653c5
AM
3867 else
3868 h->plt.offset = (bfd_vma) -1;
37c644f2
AO
3869
3870 /* If this is a weak symbol, and there is a real definition, the
3871 processor independent code will have arranged for us to see the
3872 real definition first, and we can just use the same value. */
f6e332e6 3873 if (h->u.weakdef != NULL)
37c644f2 3874 {
f6e332e6
AM
3875 BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
3876 || h->u.weakdef->root.type == bfd_link_hash_defweak);
3877 h->root.u.def.section = h->u.weakdef->root.u.def.section;
3878 h->root.u.def.value = h->u.weakdef->root.u.def.value;
04e534c3 3879 if (info->nocopyreloc)
f6e332e6 3880 h->non_got_ref = h->u.weakdef->non_got_ref;
b34976b6 3881 return TRUE;
37c644f2
AO
3882 }
3883
3884 /* This is a reference to a symbol defined by a dynamic object which
3885 is not a function. */
3886
3887 /* If we are creating a shared library, we must presume that the
3888 only references to the symbol are via the global offset table.
3889 For such cases we need not do anything here; the relocations will
3890 be handled correctly by relocate_section. */
3891 if (info->shared)
b34976b6 3892 return TRUE;
37c644f2
AO
3893
3894 /* If there are no references to this symbol that do not use the
3895 GOT, we don't need to generate a copy reloc. */
f5385ebf 3896 if (!h->non_got_ref)
b34976b6 3897 return TRUE;
37c644f2 3898
067653c5
AM
3899 /* If -z nocopyreloc was given, we won't generate them either. */
3900 if (info->nocopyreloc)
3901 {
f5385ebf 3902 h->non_got_ref = 0;
b34976b6 3903 return TRUE;
067653c5
AM
3904 }
3905
3906 eh = (struct elf_sh_link_hash_entry *) h;
3907 for (p = eh->dyn_relocs; p != NULL; p = p->next)
3908 {
3909 s = p->sec->output_section;
3910 if (s != NULL && (s->flags & (SEC_READONLY | SEC_HAS_CONTENTS)) != 0)
3911 break;
3912 }
3913
3914 /* If we didn't find any dynamic relocs in sections which needs the
3915 copy reloc, then we'll be keeping the dynamic relocs and avoiding
3916 the copy reloc. */
3917 if (p == NULL)
3918 {
f5385ebf 3919 h->non_got_ref = 0;
b34976b6 3920 return TRUE;
067653c5
AM
3921 }
3922
909272ee
AM
3923 if (h->size == 0)
3924 {
3925 (*_bfd_error_handler) (_("dynamic variable `%s' is zero size"),
3926 h->root.root.string);
3927 return TRUE;
3928 }
3929
37c644f2
AO
3930 /* We must allocate the symbol in our .dynbss section, which will
3931 become part of the .bss section of the executable. There will be
3932 an entry for this symbol in the .dynsym section. The dynamic
3933 object will contain position independent code, so all references
3934 from the dynamic object to this symbol will go through the global
3935 offset table. The dynamic linker will use the .dynsym entry to
3936 determine the address it must put in the global offset table, so
3937 both the dynamic object and the regular object will refer to the
3938 same memory location for the variable. */
3939
067653c5 3940 s = htab->sdynbss;
37c644f2
AO
3941 BFD_ASSERT (s != NULL);
3942
3943 /* We must generate a R_SH_COPY reloc to tell the dynamic linker to
3944 copy the initial value out of the dynamic object and into the
3945 runtime process image. We need to remember the offset into the
3946 .rela.bss section we are going to use. */
3947 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
3948 {
3949 asection *srel;
3950
067653c5 3951 srel = htab->srelbss;
37c644f2 3952 BFD_ASSERT (srel != NULL);
eea6121a 3953 srel->size += sizeof (Elf32_External_Rela);
f5385ebf 3954 h->needs_copy = 1;
37c644f2
AO
3955 }
3956
3957 /* We need to figure out the alignment required for this symbol. I
3958 have no idea how ELF linkers handle this. */
3959 power_of_two = bfd_log2 (h->size);
3960 if (power_of_two > 3)
3961 power_of_two = 3;
3962
3963 /* Apply the required alignment. */
eea6121a 3964 s->size = BFD_ALIGN (s->size, (bfd_size_type) (1 << power_of_two));
2293c499 3965 if (power_of_two > bfd_get_section_alignment (htab->root.dynobj, s))
37c644f2 3966 {
2293c499 3967 if (! bfd_set_section_alignment (htab->root.dynobj, s, power_of_two))
b34976b6 3968 return FALSE;
37c644f2
AO
3969 }
3970
3971 /* Define the symbol as being at this point in the section. */
3972 h->root.u.def.section = s;
eea6121a 3973 h->root.u.def.value = s->size;
37c644f2
AO
3974
3975 /* Increment the section size to make room for the symbol. */
eea6121a 3976 s->size += h->size;
37c644f2 3977
b34976b6 3978 return TRUE;
37c644f2
AO
3979}
3980
067653c5
AM
3981/* Allocate space in .plt, .got and associated reloc sections for
3982 dynamic relocs. */
3983
b34976b6 3984static bfd_boolean
09fd220b 3985allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
067653c5
AM
3986{
3987 struct bfd_link_info *info;
3988 struct elf_sh_link_hash_table *htab;
3989 struct elf_sh_link_hash_entry *eh;
3990 struct elf_sh_dyn_relocs *p;
3991
3992 if (h->root.type == bfd_link_hash_indirect)
b34976b6 3993 return TRUE;
067653c5
AM
3994
3995 if (h->root.type == bfd_link_hash_warning)
3996 /* When warning symbols are created, they **replace** the "real"
3997 entry in the hash table, thus we never get to see the real
3998 symbol in a hash traversal. So look at it now. */
3999 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4000
4001 info = (struct bfd_link_info *) inf;
4002 htab = sh_elf_hash_table (info);
4003
4989d792
SC
4004 eh = (struct elf_sh_link_hash_entry *) h;
4005 if ((h->got.refcount > 0
f5385ebf 4006 || h->forced_local)
4989d792
SC
4007 && eh->gotplt_refcount > 0)
4008 {
4009 /* The symbol has been forced local, or we have some direct got refs,
26e41594 4010 so treat all the gotplt refs as got refs. */
4989d792
SC
4011 h->got.refcount += eh->gotplt_refcount;
4012 if (h->plt.refcount >= eh->gotplt_refcount)
4013 h->plt.refcount -= eh->gotplt_refcount;
4014 }
4015
067653c5 4016 if (htab->root.dynamic_sections_created
04e534c3
KK
4017 && h->plt.refcount > 0
4018 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
4019 || h->root.type != bfd_link_hash_undefweak))
067653c5
AM
4020 {
4021 /* Make sure this symbol is output as a dynamic symbol.
4022 Undefined weak syms won't yet be marked as dynamic. */
4023 if (h->dynindx == -1
f5385ebf 4024 && !h->forced_local)
067653c5 4025 {
c152c796 4026 if (! bfd_elf_link_record_dynamic_symbol (info, h))
b34976b6 4027 return FALSE;
067653c5
AM
4028 }
4029
267fb3c1
KK
4030 if (info->shared
4031 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, 0, h))
067653c5
AM
4032 {
4033 asection *s = htab->splt;
4034
4035 /* If this is the first .plt entry, make room for the special
4036 first entry. */
eea6121a
AM
4037 if (s->size == 0)
4038 s->size += PLT_ENTRY_SIZE;
067653c5 4039
eea6121a 4040 h->plt.offset = s->size;
067653c5
AM
4041
4042 /* If this symbol is not defined in a regular file, and we are
4043 not generating a shared library, then set the symbol to this
4044 location in the .plt. This is required to make function
4045 pointers compare as equal between the normal executable and
4046 the shared library. */
4047 if (! info->shared
f5385ebf 4048 && !h->def_regular)
067653c5
AM
4049 {
4050 h->root.u.def.section = s;
4051 h->root.u.def.value = h->plt.offset;
4052 }
4053
4054 /* Make room for this entry. */
eea6121a 4055 s->size += PLT_ENTRY_SIZE;
067653c5
AM
4056
4057 /* We also need to make an entry in the .got.plt section, which
4058 will be placed in the .got section by the linker script. */
eea6121a 4059 htab->sgotplt->size += 4;
067653c5
AM
4060
4061 /* We also need to make an entry in the .rel.plt section. */
eea6121a 4062 htab->srelplt->size += sizeof (Elf32_External_Rela);
067653c5
AM
4063 }
4064 else
4065 {
4066 h->plt.offset = (bfd_vma) -1;
f5385ebf 4067 h->needs_plt = 0;
067653c5
AM
4068 }
4069 }
4070 else
4071 {
4072 h->plt.offset = (bfd_vma) -1;
f5385ebf 4073 h->needs_plt = 0;
067653c5
AM
4074 }
4075
4076 if (h->got.refcount > 0)
4077 {
4078 asection *s;
b34976b6 4079 bfd_boolean dyn;
3376eaf5 4080 int tls_type = sh_elf_hash_entry (h)->tls_type;
067653c5
AM
4081
4082 /* Make sure this symbol is output as a dynamic symbol.
4083 Undefined weak syms won't yet be marked as dynamic. */
4084 if (h->dynindx == -1
f5385ebf 4085 && !h->forced_local)
067653c5 4086 {
c152c796 4087 if (! bfd_elf_link_record_dynamic_symbol (info, h))
b34976b6 4088 return FALSE;
067653c5
AM
4089 }
4090
4091 s = htab->sgot;
eea6121a
AM
4092 h->got.offset = s->size;
4093 s->size += 4;
3376eaf5
KK
4094 /* R_SH_TLS_GD needs 2 consecutive GOT slots. */
4095 if (tls_type == GOT_TLS_GD)
eea6121a 4096 s->size += 4;
067653c5 4097 dyn = htab->root.dynamic_sections_created;
267fb3c1 4098 /* R_SH_TLS_IE_32 needs one dynamic relocation if dynamic,
3376eaf5
KK
4099 R_SH_TLS_GD needs one if local symbol and two if global. */
4100 if ((tls_type == GOT_TLS_GD && h->dynindx == -1)
267fb3c1 4101 || (tls_type == GOT_TLS_IE && dyn))
eea6121a 4102 htab->srelgot->size += sizeof (Elf32_External_Rela);
3376eaf5 4103 else if (tls_type == GOT_TLS_GD)
eea6121a 4104 htab->srelgot->size += 2 * sizeof (Elf32_External_Rela);
04e534c3
KK
4105 else if ((ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
4106 || h->root.type != bfd_link_hash_undefweak)
4107 && (info->shared
4108 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, h)))
eea6121a 4109 htab->srelgot->size += sizeof (Elf32_External_Rela);
067653c5
AM
4110 }
4111 else
4112 h->got.offset = (bfd_vma) -1;
4113
396a6083
SC
4114#ifdef INCLUDE_SHMEDIA
4115 if (eh->datalabel_got.refcount > 0)
4116 {
4117 asection *s;
b34976b6 4118 bfd_boolean dyn;
396a6083
SC
4119
4120 /* Make sure this symbol is output as a dynamic symbol.
4121 Undefined weak syms won't yet be marked as dynamic. */
4122 if (h->dynindx == -1
f5385ebf 4123 && !h->forced_local)
396a6083 4124 {
c152c796 4125 if (! bfd_elf_link_record_dynamic_symbol (info, h))
b34976b6 4126 return FALSE;
396a6083
SC
4127 }
4128
4129 s = htab->sgot;
eea6121a
AM
4130 eh->datalabel_got.offset = s->size;
4131 s->size += 4;
396a6083 4132 dyn = htab->root.dynamic_sections_created;
267fb3c1 4133 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h))
eea6121a 4134 htab->srelgot->size += sizeof (Elf32_External_Rela);
396a6083
SC
4135 }
4136 else
4137 eh->datalabel_got.offset = (bfd_vma) -1;
4138#endif
4139
067653c5 4140 if (eh->dyn_relocs == NULL)
b34976b6 4141 return TRUE;
067653c5
AM
4142
4143 /* In the shared -Bsymbolic case, discard space allocated for
4144 dynamic pc-relative relocs against symbols which turn out to be
4145 defined in regular objects. For the normal shared case, discard
4146 space for pc-relative relocs that have become local due to symbol
4147 visibility changes. */
4148
4149 if (info->shared)
4150 {
3d85aebe 4151 if (SYMBOL_CALLS_LOCAL (info, h))
067653c5
AM
4152 {
4153 struct elf_sh_dyn_relocs **pp;
4154
4155 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
4156 {
4157 p->count -= p->pc_count;
4158 p->pc_count = 0;
4159 if (p->count == 0)
4160 *pp = p->next;
4161 else
4162 pp = &p->next;
4163 }
4164 }
04e534c3
KK
4165
4166 /* Also discard relocs on undefined weak syms with non-default
4167 visibility. */
22d606e9 4168 if (eh->dyn_relocs != NULL
04e534c3 4169 && h->root.type == bfd_link_hash_undefweak)
22d606e9
AM
4170 {
4171 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
4172 eh->dyn_relocs = NULL;
4173
4174 /* Make sure undefined weak symbols are output as a dynamic
4175 symbol in PIEs. */
4176 else if (h->dynindx == -1
4177 && !h->forced_local)
4178 {
4179 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4180 return FALSE;
4181 }
4182 }
067653c5
AM
4183 }
4184 else
4185 {
4186 /* For the non-shared case, discard space for relocs against
4187 symbols which turn out to need copy relocs or are not
4188 dynamic. */
4189
f5385ebf
AM
4190 if (!h->non_got_ref
4191 && ((h->def_dynamic
4192 && !h->def_regular)
067653c5
AM
4193 || (htab->root.dynamic_sections_created
4194 && (h->root.type == bfd_link_hash_undefweak
4195 || h->root.type == bfd_link_hash_undefined))))
4196 {
4197 /* Make sure this symbol is output as a dynamic symbol.
4198 Undefined weak syms won't yet be marked as dynamic. */
4199 if (h->dynindx == -1
f5385ebf 4200 && !h->forced_local)
067653c5 4201 {
c152c796 4202 if (! bfd_elf_link_record_dynamic_symbol (info, h))
b34976b6 4203 return FALSE;
067653c5
AM
4204 }
4205
4206 /* If that succeeded, we know we'll be keeping all the
4207 relocs. */
4208 if (h->dynindx != -1)
4209 goto keep;
4210 }
4211
4212 eh->dyn_relocs = NULL;
4213
4214 keep: ;
4215 }
4216
4217 /* Finally, allocate space. */
4218 for (p = eh->dyn_relocs; p != NULL; p = p->next)
4219 {
4220 asection *sreloc = elf_section_data (p->sec)->sreloc;
eea6121a 4221 sreloc->size += p->count * sizeof (Elf32_External_Rela);
067653c5
AM
4222 }
4223
b34976b6 4224 return TRUE;
067653c5
AM
4225}
4226
4227/* Find any dynamic relocs that apply to read-only sections. */
4228
b34976b6 4229static bfd_boolean
09fd220b 4230readonly_dynrelocs (struct elf_link_hash_entry *h, void *inf)
067653c5
AM
4231{
4232 struct elf_sh_link_hash_entry *eh;
4233 struct elf_sh_dyn_relocs *p;
4234
4235 if (h->root.type == bfd_link_hash_warning)
4236 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4237
4238 eh = (struct elf_sh_link_hash_entry *) h;
4239 for (p = eh->dyn_relocs; p != NULL; p = p->next)
4240 {
4241 asection *s = p->sec->output_section;
4242
4243 if (s != NULL && (s->flags & SEC_READONLY) != 0)
4244 {
4245 struct bfd_link_info *info = (struct bfd_link_info *) inf;
4246
4247 info->flags |= DF_TEXTREL;
4248
4249 /* Not an error, just cut short the traversal. */
b34976b6 4250 return FALSE;
067653c5
AM
4251 }
4252 }
b34976b6 4253 return TRUE;
067653c5
AM
4254}
4255
37c644f2
AO
4256/* Set the sizes of the dynamic sections. */
4257
b34976b6 4258static bfd_boolean
09fd220b
KK
4259sh_elf_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
4260 struct bfd_link_info *info)
37c644f2 4261{
067653c5 4262 struct elf_sh_link_hash_table *htab;
37c644f2
AO
4263 bfd *dynobj;
4264 asection *s;
b34976b6 4265 bfd_boolean relocs;
067653c5 4266 bfd *ibfd;
37c644f2 4267
067653c5
AM
4268 htab = sh_elf_hash_table (info);
4269 dynobj = htab->root.dynobj;
37c644f2
AO
4270 BFD_ASSERT (dynobj != NULL);
4271
067653c5 4272 if (htab->root.dynamic_sections_created)
37c644f2
AO
4273 {
4274 /* Set the contents of the .interp section to the interpreter. */
2558b9a9 4275 if (info->executable)
37c644f2
AO
4276 {
4277 s = bfd_get_section_by_name (dynobj, ".interp");
4278 BFD_ASSERT (s != NULL);
eea6121a 4279 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
37c644f2
AO
4280 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
4281 }
4282 }
37c644f2 4283
067653c5
AM
4284 /* Set up .got offsets for local syms, and space for local dynamic
4285 relocs. */
4286 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
37c644f2 4287 {
067653c5
AM
4288 bfd_signed_vma *local_got;
4289 bfd_signed_vma *end_local_got;
3376eaf5 4290 char *local_tls_type;
067653c5
AM
4291 bfd_size_type locsymcount;
4292 Elf_Internal_Shdr *symtab_hdr;
4293 asection *srel;
37c644f2 4294
067653c5 4295 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
37c644f2
AO
4296 continue;
4297
067653c5 4298 for (s = ibfd->sections; s != NULL; s = s->next)
37c644f2 4299 {
067653c5
AM
4300 struct elf_sh_dyn_relocs *p;
4301
4302 for (p = ((struct elf_sh_dyn_relocs *)
4303 elf_section_data (s)->local_dynrel);
4304 p != NULL;
4305 p = p->next)
37c644f2 4306 {
067653c5
AM
4307 if (! bfd_is_abs_section (p->sec)
4308 && bfd_is_abs_section (p->sec->output_section))
4309 {
4310 /* Input section has been discarded, either because
4311 it is a copy of a linkonce section or due to
4312 linker script /DISCARD/, so we'll be discarding
4313 the relocs too. */
4314 }
4315 else if (p->count != 0)
4316 {
4317 srel = elf_section_data (p->sec)->sreloc;
eea6121a 4318 srel->size += p->count * sizeof (Elf32_External_Rela);
067653c5
AM
4319 if ((p->sec->output_section->flags & SEC_READONLY) != 0)
4320 info->flags |= DF_TEXTREL;
4321 }
37c644f2
AO
4322 }
4323 }
067653c5
AM
4324
4325 local_got = elf_local_got_refcounts (ibfd);
4326 if (!local_got)
4327 continue;
4328
4329 symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
4330 locsymcount = symtab_hdr->sh_info;
4331#ifdef INCLUDE_SHMEDIA
4332 /* Count datalabel local GOT. */
4333 locsymcount *= 2;
4334#endif
4335 end_local_got = local_got + locsymcount;
3376eaf5 4336 local_tls_type = sh_elf_local_got_tls_type (ibfd);
067653c5
AM
4337 s = htab->sgot;
4338 srel = htab->srelgot;
4339 for (; local_got < end_local_got; ++local_got)
37c644f2 4340 {
067653c5 4341 if (*local_got > 0)
37c644f2 4342 {
eea6121a
AM
4343 *local_got = s->size;
4344 s->size += 4;
3376eaf5 4345 if (*local_tls_type == GOT_TLS_GD)
eea6121a 4346 s->size += 4;
067653c5 4347 if (info->shared)
eea6121a 4348 srel->size += sizeof (Elf32_External_Rela);
37c644f2
AO
4349 }
4350 else
067653c5 4351 *local_got = (bfd_vma) -1;
3376eaf5 4352 ++local_tls_type;
37c644f2 4353 }
067653c5
AM
4354 }
4355
3376eaf5
KK
4356 if (htab->tls_ldm_got.refcount > 0)
4357 {
4358 /* Allocate 2 got entries and 1 dynamic reloc for R_SH_TLS_LD_32
4359 relocs. */
eea6121a
AM
4360 htab->tls_ldm_got.offset = htab->sgot->size;
4361 htab->sgot->size += 8;
4362 htab->srelgot->size += sizeof (Elf32_External_Rela);
3376eaf5
KK
4363 }
4364 else
4365 htab->tls_ldm_got.offset = -1;
4366
067653c5
AM
4367 /* Allocate global sym .plt and .got entries, and space for global
4368 sym dynamic relocs. */
09fd220b 4369 elf_link_hash_traverse (&htab->root, allocate_dynrelocs, info);
067653c5
AM
4370
4371 /* We now have determined the sizes of the various dynamic sections.
4372 Allocate memory for them. */
b34976b6 4373 relocs = FALSE;
067653c5
AM
4374 for (s = dynobj->sections; s != NULL; s = s->next)
4375 {
4376 if ((s->flags & SEC_LINKER_CREATED) == 0)
4377 continue;
4378
4379 if (s == htab->splt
4380 || s == htab->sgot
c456f082
AM
4381 || s == htab->sgotplt
4382 || s == htab->sdynbss)
067653c5
AM
4383 {
4384 /* Strip this section if we don't need it; see the
4385 comment below. */
4386 }
4387 else if (strncmp (bfd_get_section_name (dynobj, s), ".rela", 5) == 0)
4388 {
eea6121a 4389 if (s->size != 0 && s != htab->srelplt)
b34976b6 4390 relocs = TRUE;
067653c5
AM
4391
4392 /* We use the reloc_count field as a counter if we need
4393 to copy relocs into the output file. */
4394 s->reloc_count = 0;
4395 }
4396 else
37c644f2
AO
4397 {
4398 /* It's not one of our sections, so don't allocate space. */
4399 continue;
4400 }
4401
eea6121a 4402 if (s->size == 0)
37c644f2 4403 {
067653c5
AM
4404 /* If we don't need this section, strip it from the
4405 output file. This is mostly to handle .rela.bss and
4406 .rela.plt. We must create both sections in
4407 create_dynamic_sections, because they must be created
4408 before the linker maps input sections to output
4409 sections. The linker does that before
4410 adjust_dynamic_symbol is called, and it is that
4411 function which decides whether anything needs to go
4412 into these sections. */
4413
8423293d 4414 s->flags |= SEC_EXCLUDE;
37c644f2
AO
4415 continue;
4416 }
4417
c456f082
AM
4418 if ((s->flags & SEC_HAS_CONTENTS) == 0)
4419 continue;
4420
067653c5
AM
4421 /* Allocate memory for the section contents. We use bfd_zalloc
4422 here in case unused entries are not reclaimed before the
4423 section's contents are written out. This should not happen,
4424 but this way if it does, we get a R_SH_NONE reloc instead
4425 of garbage. */
eea6121a 4426 s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->size);
067653c5 4427 if (s->contents == NULL)
b34976b6 4428 return FALSE;
37c644f2
AO
4429 }
4430
067653c5 4431 if (htab->root.dynamic_sections_created)
37c644f2
AO
4432 {
4433 /* Add some entries to the .dynamic section. We fill in the
4434 values later, in sh_elf_finish_dynamic_sections, but we
4435 must add the entries now so that we get the correct size for
4436 the .dynamic section. The DT_DEBUG entry is filled in by the
4437 dynamic linker and used by the debugger. */
dc810e39 4438#define add_dynamic_entry(TAG, VAL) \
5a580b3a 4439 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
dc810e39 4440
2558b9a9 4441 if (info->executable)
37c644f2 4442 {
067653c5 4443 if (! add_dynamic_entry (DT_DEBUG, 0))
b34976b6 4444 return FALSE;
37c644f2
AO
4445 }
4446
eea6121a 4447 if (htab->splt->size != 0)
37c644f2 4448 {
067653c5
AM
4449 if (! add_dynamic_entry (DT_PLTGOT, 0)
4450 || ! add_dynamic_entry (DT_PLTRELSZ, 0)
4451 || ! add_dynamic_entry (DT_PLTREL, DT_RELA)
4452 || ! add_dynamic_entry (DT_JMPREL, 0))
b34976b6 4453 return FALSE;
37c644f2
AO
4454 }
4455
4456 if (relocs)
4457 {
067653c5
AM
4458 if (! add_dynamic_entry (DT_RELA, 0)
4459 || ! add_dynamic_entry (DT_RELASZ, 0)
4460 || ! add_dynamic_entry (DT_RELAENT,
4461 sizeof (Elf32_External_Rela)))
b34976b6 4462 return FALSE;
37c644f2 4463
067653c5
AM
4464 /* If any dynamic relocs apply to a read-only section,
4465 then we need a DT_TEXTREL entry. */
4466 if ((info->flags & DF_TEXTREL) == 0)
09fd220b 4467 elf_link_hash_traverse (&htab->root, readonly_dynrelocs, info);
067653c5
AM
4468
4469 if ((info->flags & DF_TEXTREL) != 0)
4470 {
4471 if (! add_dynamic_entry (DT_TEXTREL, 0))
b34976b6 4472 return FALSE;
067653c5 4473 }
37c644f2
AO
4474 }
4475 }
dc810e39 4476#undef add_dynamic_entry
37c644f2 4477
b34976b6 4478 return TRUE;
37c644f2 4479}
37c644f2
AO
4480\f
4481/* Relocate an SH ELF section. */
4482
b34976b6 4483static bfd_boolean
09fd220b
KK
4484sh_elf_relocate_section (bfd *output_bfd, struct bfd_link_info *info,
4485 bfd *input_bfd, asection *input_section,
4486 bfd_byte *contents, Elf_Internal_Rela *relocs,
4487 Elf_Internal_Sym *local_syms,
4488 asection **local_sections)
37c644f2 4489{
067653c5 4490 struct elf_sh_link_hash_table *htab;
37c644f2
AO
4491 Elf_Internal_Shdr *symtab_hdr;
4492 struct elf_link_hash_entry **sym_hashes;
4493 Elf_Internal_Rela *rel, *relend;
4494 bfd *dynobj;
4495 bfd_vma *local_got_offsets;
4496 asection *sgot;
fbca6ad9 4497 asection *sgotplt;
37c644f2
AO
4498 asection *splt;
4499 asection *sreloc;
3376eaf5 4500 asection *srelgot;
37c644f2 4501
067653c5 4502 htab = sh_elf_hash_table (info);
37c644f2
AO
4503 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
4504 sym_hashes = elf_sym_hashes (input_bfd);
067653c5 4505 dynobj = htab->root.dynobj;
37c644f2
AO
4506 local_got_offsets = elf_local_got_offsets (input_bfd);
4507
067653c5
AM
4508 sgot = htab->sgot;
4509 sgotplt = htab->sgotplt;
4510 splt = htab->splt;
37c644f2 4511 sreloc = NULL;
3376eaf5 4512 srelgot = NULL;
37c644f2
AO
4513
4514 rel = relocs;
4515 relend = relocs + input_section->reloc_count;
4516 for (; rel < relend; rel++)
4517 {
4518 int r_type;
4519 reloc_howto_type *howto;
4520 unsigned long r_symndx;
4521 Elf_Internal_Sym *sym;
4522 asection *sec;
4523 struct elf_link_hash_entry *h;
4524 bfd_vma relocation;
435b1e90 4525 bfd_vma addend = (bfd_vma) 0;
37c644f2 4526 bfd_reloc_status_type r;
fbca6ad9 4527 int seen_stt_datalabel = 0;
3376eaf5
KK
4528 bfd_vma off;
4529 int tls_type;
37c644f2
AO
4530
4531 r_symndx = ELF32_R_SYM (rel->r_info);
4532
37c644f2
AO
4533 r_type = ELF32_R_TYPE (rel->r_info);
4534
4535 /* Many of the relocs are only used for relaxing, and are
067653c5 4536 handled entirely by the relaxation code. */
d38eb334
DD
4537 if (r_type >= (int) R_SH_GNU_VTINHERIT
4538 && r_type <= (int) R_SH_LABEL)
37c644f2 4539 continue;
c5aeb40f
AO
4540 if (r_type == (int) R_SH_NONE)
4541 continue;
37c644f2
AO
4542
4543 if (r_type < 0
4544 || r_type >= R_SH_max
4545 || (r_type >= (int) R_SH_FIRST_INVALID_RELOC
06bb75c1 4546 && r_type <= (int) R_SH_LAST_INVALID_RELOC)
fbca6ad9
AO
4547 || ( r_type >= (int) R_SH_FIRST_INVALID_RELOC_3
4548 && r_type <= (int) R_SH_LAST_INVALID_RELOC_3)
4549 || ( r_type >= (int) R_SH_FIRST_INVALID_RELOC_4
4550 && r_type <= (int) R_SH_LAST_INVALID_RELOC_4)
3376eaf5
KK
4551 || ( r_type >= (int) R_SH_FIRST_INVALID_RELOC_5
4552 && r_type <= (int) R_SH_LAST_INVALID_RELOC_5)
06bb75c1
AO
4553 || (r_type >= (int) R_SH_FIRST_INVALID_RELOC_2
4554 && r_type <= (int) R_SH_LAST_INVALID_RELOC_2))
37c644f2
AO
4555 {
4556 bfd_set_error (bfd_error_bad_value);
b34976b6 4557 return FALSE;
37c644f2
AO
4558 }
4559
4560 howto = sh_elf_howto_table + r_type;
4561
146be91a 4562 /* For relocs that aren't partial_inplace, we get the addend from
067653c5 4563 the relocation. */
146be91a
HPN
4564 if (! howto->partial_inplace)
4565 addend = rel->r_addend;
4566
37c644f2
AO
4567 h = NULL;
4568 sym = NULL;
4569 sec = NULL;
4570 if (r_symndx < symtab_hdr->sh_info)
4571 {
4572 sym = local_syms + r_symndx;
4573 sec = local_sections[r_symndx];
4574 relocation = (sec->output_section->vma
4575 + sec->output_offset
4576 + sym->st_value);
fbca6ad9
AO
4577 /* A local symbol never has STO_SH5_ISA32, so we don't need
4578 datalabel processing here. Make sure this does not change
4579 without notice. */
4580 if ((sym->st_other & STO_SH5_ISA32) != 0)
4581 ((*info->callbacks->reloc_dangerous)
4582 (info,
4583 _("Unexpected STO_SH5_ISA32 on local symbol is not handled"),
4584 input_bfd, input_section, rel->r_offset));
1049f94e 4585 if (info->relocatable)
8a3975e6 4586 {
1049f94e 4587 /* This is a relocatable link. We don't have to change
8a3975e6
AO
4588 anything, unless the reloc is against a section symbol,
4589 in which case we have to adjust according to where the
4590 section symbol winds up in the output section. */
4591 sym = local_syms + r_symndx;
4592 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
146be91a
HPN
4593 {
4594 if (! howto->partial_inplace)
4595 {
4596 /* For relocations with the addend in the
4597 relocation, we need just to update the addend.
4598 All real relocs are of type partial_inplace; this
4599 code is mostly for completeness. */
4600 rel->r_addend += sec->output_offset + sym->st_value;
4601
4602 continue;
4603 }
4604
4605 /* Relocs of type partial_inplace need to pick up the
4606 contents in the contents and add the offset resulting
4607 from the changed location of the section symbol.
4608 Using _bfd_final_link_relocate (e.g. goto
4609 final_link_relocate) here would be wrong, because
4610 relocations marked pc_relative would get the current
4611 location subtracted, and we must only do that at the
4612 final link. */
4613 r = _bfd_relocate_contents (howto, input_bfd,
4614 sec->output_offset
4615 + sym->st_value,
4616 contents + rel->r_offset);
4617 goto relocation_done;
4618 }
8a3975e6
AO
4619
4620 continue;
4621 }
f8df10f4
JJ
4622 else if (! howto->partial_inplace)
4623 {
8517fae7 4624 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
f8df10f4
JJ
4625 addend = rel->r_addend;
4626 }
4627 else if ((sec->flags & SEC_MERGE)
4628 && ELF_ST_TYPE (sym->st_info) == STT_SECTION)
4629 {
4630 asection *msec;
4631
4632 if (howto->rightshift || howto->src_mask != 0xffffffff)
4633 {
4634 (*_bfd_error_handler)
d003868e
AM
4635 (_("%B(%A+0x%lx): %s relocation against SEC_MERGE section"),
4636 input_bfd, input_section,
f8df10f4 4637 (long) rel->r_offset, howto->name);
b34976b6 4638 return FALSE;
f8df10f4
JJ
4639 }
4640
067653c5
AM
4641 addend = bfd_get_32 (input_bfd, contents + rel->r_offset);
4642 msec = sec;
4643 addend =
c629eae0 4644 _bfd_elf_rel_local_sym (output_bfd, sym, &msec, addend)
f8df10f4
JJ
4645 - relocation;
4646 addend += msec->output_section->vma + msec->output_offset;
4647 bfd_put_32 (input_bfd, addend, contents + rel->r_offset);
4648 addend = 0;
4649 }
37c644f2
AO
4650 }
4651 else
4652 {
560e09e9
NC
4653 /* FIXME: Ought to make use of the RELOC_FOR_GLOBAL_SYMBOL macro. */
4654
8a3975e6
AO
4655 /* Section symbol are never (?) placed in the hash table, so
4656 we can just ignore hash relocations when creating a
1049f94e
AM
4657 relocatable object file. */
4658 if (info->relocatable)
8a3975e6
AO
4659 continue;
4660
37c644f2
AO
4661 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
4662 while (h->root.type == bfd_link_hash_indirect
4663 || h->root.type == bfd_link_hash_warning)
fbca6ad9
AO
4664 {
4665#ifdef INCLUDE_SHMEDIA
4666 /* If the reference passes a symbol marked with
4667 STT_DATALABEL, then any STO_SH5_ISA32 on the final value
4668 doesn't count. */
4669 seen_stt_datalabel |= h->type == STT_DATALABEL;
4670#endif
4671 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4672 }
37c644f2
AO
4673 if (h->root.type == bfd_link_hash_defined
4674 || h->root.type == bfd_link_hash_defweak)
4675 {
b34976b6 4676 bfd_boolean dyn;
067653c5
AM
4677
4678 dyn = htab->root.dynamic_sections_created;
37c644f2
AO
4679 sec = h->root.u.def.section;
4680 /* In these cases, we don't need the relocation value.
4681 We check specially because in some obscure cases
435b1e90 4682 sec->output_section will be NULL. */
37c644f2 4683 if (r_type == R_SH_GOTPC
fbca6ad9
AO
4684 || r_type == R_SH_GOTPC_LOW16
4685 || r_type == R_SH_GOTPC_MEDLOW16
4686 || r_type == R_SH_GOTPC_MEDHI16
4687 || r_type == R_SH_GOTPC_HI16
4688 || ((r_type == R_SH_PLT32
4689 || r_type == R_SH_PLT_LOW16
4690 || r_type == R_SH_PLT_MEDLOW16
4691 || r_type == R_SH_PLT_MEDHI16
4692 || r_type == R_SH_PLT_HI16)
37c644f2 4693 && h->plt.offset != (bfd_vma) -1)
fbca6ad9
AO
4694 || ((r_type == R_SH_GOT32
4695 || r_type == R_SH_GOT_LOW16
4696 || r_type == R_SH_GOT_MEDLOW16
4697 || r_type == R_SH_GOT_MEDHI16
4698 || r_type == R_SH_GOT_HI16)
267fb3c1 4699 && WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h)
37c644f2
AO
4700 && (! info->shared
4701 || (! info->symbolic && h->dynindx != -1)
f5385ebf 4702 || !h->def_regular))
37c644f2
AO
4703 /* The cases above are those in which relocation is
4704 overwritten in the switch block below. The cases
4705 below are those in which we must defer relocation
4706 to run-time, because we can't resolve absolute
4707 addresses when creating a shared library. */
4708 || (info->shared
4709 && ((! info->symbolic && h->dynindx != -1)
f5385ebf 4710 || !h->def_regular)
37c644f2 4711 && ((r_type == R_SH_DIR32
f5385ebf 4712 && !h->forced_local)
f50ad433
KK
4713 || (r_type == R_SH_REL32
4714 && !SYMBOL_CALLS_LOCAL (info, h)))
37c644f2
AO
4715 && ((input_section->flags & SEC_ALLOC) != 0
4716 /* DWARF will emit R_SH_DIR32 relocations in its
4717 sections against symbols defined externally
4718 in shared libraries. We can't do anything
4719 with them here. */
067653c5 4720 || ((input_section->flags & SEC_DEBUGGING) != 0
f5385ebf 4721 && h->def_dynamic)))
1448fa32
KK
4722 /* Dynamic relocs are not propagated for SEC_DEBUGGING
4723 sections because such sections are not SEC_ALLOC and
4724 thus ld.so will not process them. */
4725 || (sec->output_section == NULL
4726 && ((input_section->flags & SEC_DEBUGGING) != 0
f5385ebf 4727 && h->def_dynamic))
3376eaf5
KK
4728 || (sec->output_section == NULL
4729 && (sh_elf_hash_entry (h)->tls_type == GOT_TLS_IE
4730 || sh_elf_hash_entry (h)->tls_type == GOT_TLS_GD)))
37c644f2
AO
4731 relocation = 0;
4732 else if (sec->output_section == NULL)
4733 {
4734 (*_bfd_error_handler)
843fe662
L
4735 (_("%B(%A+0x%lx): unresolvable %s relocation against symbol `%s'"),
4736 input_bfd,
4737 input_section,
4738 (long) rel->r_offset,
4739 howto->name,
4740 h->root.root.string);
b34976b6 4741 return FALSE;
37c644f2
AO
4742 }
4743 else
fbca6ad9 4744 relocation = ((h->root.u.def.value
37c644f2 4745 + sec->output_section->vma
fbca6ad9
AO
4746 + sec->output_offset)
4747 /* A STO_SH5_ISA32 causes a "bitor 1" to the
4748 symbol value, unless we've seen
4749 STT_DATALABEL on the way to it. */
4750 | ((h->other & STO_SH5_ISA32) != 0
4751 && ! seen_stt_datalabel));
37c644f2
AO
4752 }
4753 else if (h->root.type == bfd_link_hash_undefweak)
4754 relocation = 0;
59c2e50f 4755 else if (info->unresolved_syms_in_objects == RM_IGNORE
067653c5 4756 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
37c644f2
AO
4757 relocation = 0;
4758 else
4759 {
560e09e9
NC
4760 if (! info->callbacks->undefined_symbol
4761 (info, h->root.root.string, input_bfd,
4762 input_section, rel->r_offset,
59c2e50f 4763 (info->unresolved_syms_in_objects == RM_GENERATE_ERROR
560e09e9 4764 || ELF_ST_VISIBILITY (h->other))))
b34976b6 4765 return FALSE;
37c644f2
AO
4766 relocation = 0;
4767 }
4768 }
4769
435b1e90 4770 switch ((int) r_type)
37c644f2
AO
4771 {
4772 final_link_relocate:
4773 /* COFF relocs don't use the addend. The addend is used for
435b1e90 4774 R_SH_DIR32 to be compatible with other compilers. */
37c644f2
AO
4775 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
4776 contents, rel->r_offset,
4777 relocation, addend);
4778 break;
4779
4780 case R_SH_IND12W:
cd6ec716
DD
4781 goto final_link_relocate;
4782
37c644f2
AO
4783 case R_SH_DIR8WPN:
4784 case R_SH_DIR8WPZ:
4785 case R_SH_DIR8WPL:
cd6ec716
DD
4786 /* If the reloc is against the start of this section, then
4787 the assembler has already taken care of it and the reloc
4788 is here only to assist in relaxing. If the reloc is not
4789 against the start of this section, then it's against an
4790 external symbol and we must deal with it ourselves. */
4791 if (input_section->output_section->vma + input_section->output_offset
4792 != relocation)
4793 {
4794 int disp = (relocation
4795 - input_section->output_section->vma
4796 - input_section->output_offset
4797 - rel->r_offset);
4798 int mask = 0;
4799 switch (r_type)
4800 {
4801 case R_SH_DIR8WPN:
4802 case R_SH_DIR8WPZ: mask = 1; break;
4803 case R_SH_DIR8WPL: mask = 3; break;
4804 default: mask = 0; break;
4805 }
4806 if (disp & mask)
4807 {
4808 ((*_bfd_error_handler)
d003868e
AM
4809 (_("%B: 0x%lx: fatal: unaligned branch target for relax-support relocation"),
4810 input_section->owner,
cd6ec716
DD
4811 (unsigned long) rel->r_offset));
4812 bfd_set_error (bfd_error_bad_value);
b34976b6 4813 return FALSE;
cd6ec716
DD
4814 }
4815 relocation -= 4;
4816 goto final_link_relocate;
4817 }
4818 r = bfd_reloc_ok;
4819 break;
37c644f2
AO
4820
4821 default:
fbca6ad9
AO
4822#ifdef INCLUDE_SHMEDIA
4823 if (shmedia_prepare_reloc (info, input_bfd, input_section,
4824 contents, rel, &relocation))
4825 goto final_link_relocate;
4826#endif
37c644f2 4827 bfd_set_error (bfd_error_bad_value);
b34976b6 4828 return FALSE;
37c644f2 4829
d38eb334
DD
4830 case R_SH_DIR16:
4831 case R_SH_DIR8:
4832 case R_SH_DIR8U:
4833 case R_SH_DIR8S:
4834 case R_SH_DIR4U:
4835 goto final_link_relocate;
4836
4837 case R_SH_DIR8UL:
4838 case R_SH_DIR4UL:
4839 if (relocation & 3)
4840 {
4841 ((*_bfd_error_handler)
d003868e
AM
4842 (_("%B: 0x%lx: fatal: unaligned %s relocation 0x%lx"),
4843 input_section->owner,
d38eb334 4844 (unsigned long) rel->r_offset, howto->name,
d003868e 4845 (unsigned long) relocation));
d38eb334
DD
4846 bfd_set_error (bfd_error_bad_value);
4847 return FALSE;
4848 }
4849 goto final_link_relocate;
4850
4851 case R_SH_DIR8UW:
4852 case R_SH_DIR8SW:
4853 case R_SH_DIR4UW:
4854 if (relocation & 1)
4855 {
4856 ((*_bfd_error_handler)
d003868e
AM
4857 (_("%B: 0x%lx: fatal: unaligned %s relocation 0x%lx"),
4858 input_section->owner,
d38eb334 4859 (unsigned long) rel->r_offset, howto->name,
d003868e 4860 (unsigned long) relocation));
d38eb334
DD
4861 bfd_set_error (bfd_error_bad_value);
4862 return FALSE;
4863 }
4864 goto final_link_relocate;
4865
4866 case R_SH_PSHA:
4867 if ((signed int)relocation < -32
4868 || (signed int)relocation > 32)
4869 {
4870 ((*_bfd_error_handler)
d003868e
AM
4871 (_("%B: 0x%lx: fatal: R_SH_PSHA relocation %d not in range -32..32"),
4872 input_section->owner,
d38eb334 4873 (unsigned long) rel->r_offset,
d003868e 4874 (unsigned long) relocation));
d38eb334
DD
4875 bfd_set_error (bfd_error_bad_value);
4876 return FALSE;
4877 }
4878 goto final_link_relocate;
4879
4880 case R_SH_PSHL:
4881 if ((signed int)relocation < -16
4882 || (signed int)relocation > 16)
4883 {
4884 ((*_bfd_error_handler)
d003868e
AM
4885 (_("%B: 0x%lx: fatal: R_SH_PSHL relocation %d not in range -32..32"),
4886 input_section->owner,
d38eb334 4887 (unsigned long) rel->r_offset,
d003868e 4888 (unsigned long) relocation));
d38eb334
DD
4889 bfd_set_error (bfd_error_bad_value);
4890 return FALSE;
4891 }
4892 goto final_link_relocate;
4893
37c644f2
AO
4894 case R_SH_DIR32:
4895 case R_SH_REL32:
46e993b9
KK
4896#ifdef INCLUDE_SHMEDIA
4897 case R_SH_IMM_LOW16_PCREL:
4898 case R_SH_IMM_MEDLOW16_PCREL:
4899 case R_SH_IMM_MEDHI16_PCREL:
4900 case R_SH_IMM_HI16_PCREL:
4901#endif
37c644f2 4902 if (info->shared
04e534c3
KK
4903 && (h == NULL
4904 || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
4905 || h->root.type != bfd_link_hash_undefweak)
ec338859 4906 && r_symndx != 0
37c644f2 4907 && (input_section->flags & SEC_ALLOC) != 0
46e993b9 4908 && (r_type == R_SH_DIR32
3d85aebe 4909 || !SYMBOL_CALLS_LOCAL (info, h)))
37c644f2
AO
4910 {
4911 Elf_Internal_Rela outrel;
947216bf 4912 bfd_byte *loc;
b34976b6 4913 bfd_boolean skip, relocate;
37c644f2
AO
4914
4915 /* When generating a shared object, these relocations
4916 are copied into the output file to be resolved at run
4917 time. */
4918
4919 if (sreloc == NULL)
4920 {
4921 const char *name;
4922
4923 name = (bfd_elf_string_from_elf_section
4924 (input_bfd,
4925 elf_elfheader (input_bfd)->e_shstrndx,
4926 elf_section_data (input_section)->rel_hdr.sh_name));
4927 if (name == NULL)
b34976b6 4928 return FALSE;
37c644f2
AO
4929
4930 BFD_ASSERT (strncmp (name, ".rela", 5) == 0
4931 && strcmp (bfd_get_section_name (input_bfd,
4932 input_section),
4933 name + 5) == 0);
4934
4935 sreloc = bfd_get_section_by_name (dynobj, name);
4936 BFD_ASSERT (sreloc != NULL);
4937 }
4938
b34976b6
AM
4939 skip = FALSE;
4940 relocate = FALSE;
37c644f2 4941
c629eae0
JJ
4942 outrel.r_offset =
4943 _bfd_elf_section_offset (output_bfd, info, input_section,
4944 rel->r_offset);
4945 if (outrel.r_offset == (bfd_vma) -1)
b34976b6 4946 skip = TRUE;
0bb2d96a 4947 else if (outrel.r_offset == (bfd_vma) -2)
b34976b6 4948 skip = TRUE, relocate = TRUE;
37c644f2
AO
4949 outrel.r_offset += (input_section->output_section->vma
4950 + input_section->output_offset);
4951
4952 if (skip)
0bb2d96a 4953 memset (&outrel, 0, sizeof outrel);
37c644f2
AO
4954 else if (r_type == R_SH_REL32)
4955 {
4956 BFD_ASSERT (h != NULL && h->dynindx != -1);
37c644f2 4957 outrel.r_info = ELF32_R_INFO (h->dynindx, R_SH_REL32);
146be91a
HPN
4958 outrel.r_addend
4959 = bfd_get_32 (input_bfd, contents + rel->r_offset);
37c644f2 4960 }
46e993b9
KK
4961#ifdef INCLUDE_SHMEDIA
4962 else if (r_type == R_SH_IMM_LOW16_PCREL
4963 || r_type == R_SH_IMM_MEDLOW16_PCREL
4964 || r_type == R_SH_IMM_MEDHI16_PCREL
4965 || r_type == R_SH_IMM_HI16_PCREL)
4966 {
4967 BFD_ASSERT (h != NULL && h->dynindx != -1);
4968 outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
4969 outrel.r_addend = addend;
4970 }
4971#endif
37c644f2
AO
4972 else
4973 {
4974 /* h->dynindx may be -1 if this symbol was marked to
4975 become local. */
4976 if (h == NULL
4977 || ((info->symbolic || h->dynindx == -1)
f5385ebf 4978 && h->def_regular))
37c644f2 4979 {
b34976b6 4980 relocate = TRUE;
37c644f2 4981 outrel.r_info = ELF32_R_INFO (0, R_SH_RELATIVE);
146be91a
HPN
4982 outrel.r_addend
4983 = relocation + bfd_get_32 (input_bfd,
4984 contents + rel->r_offset);
37c644f2
AO
4985 }
4986 else
4987 {
4988 BFD_ASSERT (h->dynindx != -1);
37c644f2 4989 outrel.r_info = ELF32_R_INFO (h->dynindx, R_SH_DIR32);
146be91a
HPN
4990 outrel.r_addend
4991 = relocation + bfd_get_32 (input_bfd,
4992 contents + rel->r_offset);
37c644f2
AO
4993 }
4994 }
4995
947216bf
AM
4996 loc = sreloc->contents;
4997 loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rela);
4998 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
37c644f2
AO
4999
5000 /* If this reloc is against an external symbol, we do
5001 not want to fiddle with the addend. Otherwise, we
5002 need to include the symbol value so that it becomes
5003 an addend for the dynamic reloc. */
5004 if (! relocate)
5005 continue;
5006 }
37c644f2
AO
5007 goto final_link_relocate;
5008
fbca6ad9
AO
5009 case R_SH_GOTPLT32:
5010#ifdef INCLUDE_SHMEDIA
5011 case R_SH_GOTPLT_LOW16:
5012 case R_SH_GOTPLT_MEDLOW16:
5013 case R_SH_GOTPLT_MEDHI16:
5014 case R_SH_GOTPLT_HI16:
5015 case R_SH_GOTPLT10BY4:
5016 case R_SH_GOTPLT10BY8:
5017#endif
5018 /* Relocation is to the entry for this symbol in the
5019 procedure linkage table. */
5020
5021 if (h == NULL
f5385ebf 5022 || h->forced_local
fbca6ad9
AO
5023 || ! info->shared
5024 || info->symbolic
5025 || h->dynindx == -1
5026 || h->plt.offset == (bfd_vma) -1
5027 || h->got.offset != (bfd_vma) -1)
5028 goto force_got;
5029
5030 /* Relocation is to the entry for this symbol in the global
5031 offset table extension for the procedure linkage table. */
fbca6ad9 5032
067653c5 5033 BFD_ASSERT (sgotplt != NULL);
fbca6ad9
AO
5034 relocation = (sgotplt->output_offset
5035 + ((h->plt.offset / elf_sh_sizeof_plt (info)
5036 - 1 + 3) * 4));
5037
5038#ifdef GOT_BIAS
5039 relocation -= GOT_BIAS;
5040#endif
5041
5042 goto final_link_relocate;
5043
5044 force_got:
37c644f2 5045 case R_SH_GOT32:
fbca6ad9
AO
5046#ifdef INCLUDE_SHMEDIA
5047 case R_SH_GOT_LOW16:
5048 case R_SH_GOT_MEDLOW16:
5049 case R_SH_GOT_MEDHI16:
5050 case R_SH_GOT_HI16:
5051 case R_SH_GOT10BY4:
5052 case R_SH_GOT10BY8:
5053#endif
37c644f2
AO
5054 /* Relocation is to the entry for this symbol in the global
5055 offset table. */
067653c5
AM
5056
5057 BFD_ASSERT (sgot != NULL);
37c644f2
AO
5058
5059 if (h != NULL)
5060 {
b34976b6 5061 bfd_boolean dyn;
37c644f2
AO
5062
5063 off = h->got.offset;
fbca6ad9
AO
5064#ifdef INCLUDE_SHMEDIA
5065 if (seen_stt_datalabel)
5066 {
5067 struct elf_sh_link_hash_entry *hsh;
5068
5069 hsh = (struct elf_sh_link_hash_entry *)h;
396a6083 5070 off = hsh->datalabel_got.offset;
fbca6ad9
AO
5071 }
5072#endif
37c644f2
AO
5073 BFD_ASSERT (off != (bfd_vma) -1);
5074
067653c5 5075 dyn = htab->root.dynamic_sections_created;
267fb3c1 5076 if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h)
37c644f2 5077 || (info->shared
3d85aebe 5078 && SYMBOL_REFERENCES_LOCAL (info, h))
04e534c3
KK
5079 || (ELF_ST_VISIBILITY (h->other)
5080 && h->root.type == bfd_link_hash_undefweak))
37c644f2
AO
5081 {
5082 /* This is actually a static link, or it is a
5083 -Bsymbolic link and the symbol is defined
5084 locally, or the symbol was forced to be local
5085 because of a version file. We must initialize
5086 this entry in the global offset table. Since the
5087 offset must always be a multiple of 4, we use the
5088 least significant bit to record whether we have
5089 initialized it already.
5090
5091 When doing a dynamic link, we create a .rela.got
5092 relocation entry to initialize the value. This
5093 is done in the finish_dynamic_symbol routine. */
5094 if ((off & 1) != 0)
5095 off &= ~1;
5096 else
5097 {
5098 bfd_put_32 (output_bfd, relocation,
5099 sgot->contents + off);
fbca6ad9
AO
5100#ifdef INCLUDE_SHMEDIA
5101 if (seen_stt_datalabel)
5102 {
5103 struct elf_sh_link_hash_entry *hsh;
5104
5105 hsh = (struct elf_sh_link_hash_entry *)h;
396a6083 5106 hsh->datalabel_got.offset |= 1;
fbca6ad9
AO
5107 }
5108 else
5109#endif
5110 h->got.offset |= 1;
37c644f2
AO
5111 }
5112 }
5113
5114 relocation = sgot->output_offset + off;
5115 }
5116 else
5117 {
fbca6ad9
AO
5118#ifdef INCLUDE_SHMEDIA
5119 if (rel->r_addend)
5120 {
5121 BFD_ASSERT (local_got_offsets != NULL
5122 && (local_got_offsets[symtab_hdr->sh_info
5123 + r_symndx]
5124 != (bfd_vma) -1));
5125
5126 off = local_got_offsets[symtab_hdr->sh_info
5127 + r_symndx];
5128 }
5129 else
5130 {
5131#endif
37c644f2
AO
5132 BFD_ASSERT (local_got_offsets != NULL
5133 && local_got_offsets[r_symndx] != (bfd_vma) -1);
5134
5135 off = local_got_offsets[r_symndx];
fbca6ad9
AO
5136#ifdef INCLUDE_SHMEDIA
5137 }
5138#endif
37c644f2
AO
5139
5140 /* The offset must always be a multiple of 4. We use
5141 the least significant bit to record whether we have
5142 already generated the necessary reloc. */
5143 if ((off & 1) != 0)
5144 off &= ~1;
5145 else
5146 {
5147 bfd_put_32 (output_bfd, relocation, sgot->contents + off);
5148
5149 if (info->shared)
5150 {
37c644f2 5151 Elf_Internal_Rela outrel;
947216bf 5152 bfd_byte *loc;
37c644f2 5153
3376eaf5
KK
5154 if (srelgot == NULL)
5155 {
5156 srelgot = bfd_get_section_by_name (dynobj,
5157 ".rela.got");
5158 BFD_ASSERT (srelgot != NULL);
5159 }
37c644f2
AO
5160
5161 outrel.r_offset = (sgot->output_section->vma
5162 + sgot->output_offset
5163 + off);
5164 outrel.r_info = ELF32_R_INFO (0, R_SH_RELATIVE);
5165 outrel.r_addend = relocation;
947216bf
AM
5166 loc = srelgot->contents;
5167 loc += srelgot->reloc_count++ * sizeof (Elf32_External_Rela);
5168 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
37c644f2
AO
5169 }
5170
fbca6ad9
AO
5171#ifdef INCLUDE_SHMEDIA
5172 if (rel->r_addend)
5173 local_got_offsets[symtab_hdr->sh_info + r_symndx] |= 1;
5174 else
5175#endif
5176 local_got_offsets[r_symndx] |= 1;
37c644f2
AO
5177 }
5178
5179 relocation = sgot->output_offset + off;
5180 }
5181
fbca6ad9 5182#ifdef GOT_BIAS
067653c5 5183 relocation -= GOT_BIAS;
fbca6ad9
AO
5184#endif
5185
37c644f2
AO
5186 goto final_link_relocate;
5187
5188 case R_SH_GOTOFF:
fbca6ad9
AO
5189#ifdef INCLUDE_SHMEDIA
5190 case R_SH_GOTOFF_LOW16:
5191 case R_SH_GOTOFF_MEDLOW16:
5192 case R_SH_GOTOFF_MEDHI16:
5193 case R_SH_GOTOFF_HI16:
5194#endif
37c644f2
AO
5195 /* Relocation is relative to the start of the global offset
5196 table. */
5197
067653c5 5198 BFD_ASSERT (sgot != NULL);
37c644f2
AO
5199
5200 /* Note that sgot->output_offset is not involved in this
5201 calculation. We always want the start of .got. If we
5202 defined _GLOBAL_OFFSET_TABLE in a different way, as is
5203 permitted by the ABI, we might have to change this
5204 calculation. */
5205 relocation -= sgot->output_section->vma;
5206
fbca6ad9
AO
5207#ifdef GOT_BIAS
5208 relocation -= GOT_BIAS;
5209#endif
5210
5211 addend = rel->r_addend;
5212
37c644f2
AO
5213 goto final_link_relocate;
5214
5215 case R_SH_GOTPC:
fbca6ad9
AO
5216#ifdef INCLUDE_SHMEDIA
5217 case R_SH_GOTPC_LOW16:
5218 case R_SH_GOTPC_MEDLOW16:
5219 case R_SH_GOTPC_MEDHI16:
5220 case R_SH_GOTPC_HI16:
5221#endif
37c644f2
AO
5222 /* Use global offset table as symbol value. */
5223
067653c5 5224 BFD_ASSERT (sgot != NULL);
37c644f2
AO
5225 relocation = sgot->output_section->vma;
5226
fbca6ad9
AO
5227#ifdef GOT_BIAS
5228 relocation += GOT_BIAS;
5229#endif
5230
5231 addend = rel->r_addend;
5232
37c644f2
AO
5233 goto final_link_relocate;
5234
5235 case R_SH_PLT32:
fbca6ad9
AO
5236#ifdef INCLUDE_SHMEDIA
5237 case R_SH_PLT_LOW16:
5238 case R_SH_PLT_MEDLOW16:
5239 case R_SH_PLT_MEDHI16:
5240 case R_SH_PLT_HI16:
5241#endif
37c644f2
AO
5242 /* Relocation is to the entry for this symbol in the
5243 procedure linkage table. */
5244
5245 /* Resolve a PLT reloc against a local symbol directly,
5246 without using the procedure linkage table. */
5247 if (h == NULL)
5248 goto final_link_relocate;
5249
f5385ebf 5250 if (h->forced_local)
37c644f2
AO
5251 goto final_link_relocate;
5252
5253 if (h->plt.offset == (bfd_vma) -1)
5254 {
5255 /* We didn't make a PLT entry for this symbol. This
5256 happens when statically linking PIC code, or when
5257 using -Bsymbolic. */
5258 goto final_link_relocate;
5259 }
5260
067653c5 5261 BFD_ASSERT (splt != NULL);
37c644f2
AO
5262 relocation = (splt->output_section->vma
5263 + splt->output_offset
5264 + h->plt.offset);
5265
fbca6ad9
AO
5266#ifdef INCLUDE_SHMEDIA
5267 relocation++;
5268#endif
5269
5270 addend = rel->r_addend;
5271
37c644f2
AO
5272 goto final_link_relocate;
5273
5274 case R_SH_LOOP_START:
5275 {
5276 static bfd_vma start, end;
5277
5278 start = (relocation + rel->r_addend
5279 - (sec->output_section->vma + sec->output_offset));
5280 r = sh_elf_reloc_loop (r_type, input_bfd, input_section, contents,
5281 rel->r_offset, sec, start, end);
5282 break;
5283
5284 case R_SH_LOOP_END:
5285 end = (relocation + rel->r_addend
5286 - (sec->output_section->vma + sec->output_offset));
5287 r = sh_elf_reloc_loop (r_type, input_bfd, input_section, contents,
5288 rel->r_offset, sec, start, end);
5289 break;
5290 }
3376eaf5
KK
5291
5292 case R_SH_TLS_GD_32:
5293 case R_SH_TLS_IE_32:
5294 r_type = sh_elf_optimized_tls_reloc (info, r_type, h == NULL);
5295 tls_type = GOT_UNKNOWN;
5296 if (h == NULL && local_got_offsets)
5297 tls_type = sh_elf_local_got_tls_type (input_bfd) [r_symndx];
5298 else if (h != NULL)
5299 {
5300 tls_type = sh_elf_hash_entry (h)->tls_type;
5301 if (! info->shared
5302 && (h->dynindx == -1
f5385ebf 5303 || h->def_regular))
267fb3c1 5304 r_type = R_SH_TLS_LE_32;
3376eaf5
KK
5305 }
5306
5307 if (r_type == R_SH_TLS_GD_32 && tls_type == GOT_TLS_IE)
5308 r_type = R_SH_TLS_IE_32;
5309
5310 if (r_type == R_SH_TLS_LE_32)
5311 {
5312 bfd_vma offset;
5313 unsigned short insn;
3376eaf5
KK
5314
5315 if (ELF32_R_TYPE (rel->r_info) == R_SH_TLS_GD_32)
5316 {
5317 /* GD->LE transition:
5318 mov.l 1f,r4; mova 2f,r0; mov.l 2f,r1; add r0,r1;
b34976b6 5319 jsr @r1; add r12,r4; bra 3f; nop; .align 2;
3376eaf5
KK
5320 1: .long x$TLSGD; 2: .long __tls_get_addr@PLT; 3:
5321 We change it into:
5322 mov.l 1f,r4; stc gbr,r0; add r4,r0; nop;
5323 nop; nop; ...
5324 1: .long x@TPOFF; 2: .long __tls_get_addr@PLT; 3:. */
5325
5326 offset = rel->r_offset;
5327 BFD_ASSERT (offset >= 16);
5328 /* Size of GD instructions is 16 or 18. */
5329 offset -= 16;
5330 insn = bfd_get_16 (input_bfd, contents + offset + 0);
5331 if ((insn & 0xff00) == 0xc700)
5332 {
5333 BFD_ASSERT (offset >= 2);
5334 offset -= 2;
5335 insn = bfd_get_16 (input_bfd, contents + offset + 0);
5336 }
5337
5338 BFD_ASSERT ((insn & 0xff00) == 0xd400);
5339 insn = bfd_get_16 (input_bfd, contents + offset + 2);
5340 BFD_ASSERT ((insn & 0xff00) == 0xc700);
5341 insn = bfd_get_16 (input_bfd, contents + offset + 4);
5342 BFD_ASSERT ((insn & 0xff00) == 0xd100);
5343 insn = bfd_get_16 (input_bfd, contents + offset + 6);
5344 BFD_ASSERT (insn == 0x310c);
5345 insn = bfd_get_16 (input_bfd, contents + offset + 8);
5346 BFD_ASSERT (insn == 0x410b);
5347 insn = bfd_get_16 (input_bfd, contents + offset + 10);
5348 BFD_ASSERT (insn == 0x34cc);
5349
5350 bfd_put_16 (output_bfd, 0x0012, contents + offset + 2);
5351 bfd_put_16 (output_bfd, 0x304c, contents + offset + 4);
5352 bfd_put_16 (output_bfd, 0x0009, contents + offset + 6);
5353 bfd_put_16 (output_bfd, 0x0009, contents + offset + 8);
5354 bfd_put_16 (output_bfd, 0x0009, contents + offset + 10);
5355 }
5356 else
5357 {
5358 int index;
5359
5360 /* IE->LE transition:
5361 mov.l 1f,r0; stc gbr,rN; mov.l @(r0,r12),rM;
5362 bra 2f; add ...; .align 2; 1: x@GOTTPOFF; 2:
5363 We change it into:
5364 mov.l .Ln,rM; stc gbr,rN; nop; ...;
5365 1: x@TPOFF; 2:. */
5366
5367 offset = rel->r_offset;
5368 BFD_ASSERT (offset >= 16);
5369 /* Size of IE instructions is 10 or 12. */
5370 offset -= 10;
5371 insn = bfd_get_16 (input_bfd, contents + offset + 0);
5372 if ((insn & 0xf0ff) == 0x0012)
5373 {
5374 BFD_ASSERT (offset >= 2);
5375 offset -= 2;
5376 insn = bfd_get_16 (input_bfd, contents + offset + 0);
5377 }
5378
5379 BFD_ASSERT ((insn & 0xff00) == 0xd000);
5380 index = insn & 0x00ff;
5381 insn = bfd_get_16 (input_bfd, contents + offset + 2);
5382 BFD_ASSERT ((insn & 0xf0ff) == 0x0012);
5383 insn = bfd_get_16 (input_bfd, contents + offset + 4);
5384 BFD_ASSERT ((insn & 0xf0ff) == 0x00ce);
5385 insn = 0xd000 | (insn & 0x0f00) | index;
5386 bfd_put_16 (output_bfd, insn, contents + offset + 0);
5387 bfd_put_16 (output_bfd, 0x0009, contents + offset + 4);
5388 }
5389
267fb3c1
KK
5390 bfd_put_32 (output_bfd, tpoff (info, relocation),
5391 contents + rel->r_offset);
3376eaf5
KK
5392 continue;
5393 }
5394
5395 sgot = htab->sgot;
5396 if (sgot == NULL)
5397 abort ();
5398
5399 if (h != NULL)
5400 off = h->got.offset;
5401 else
5402 {
5403 if (local_got_offsets == NULL)
5404 abort ();
5405
5406 off = local_got_offsets[r_symndx];
5407 }
5408
267fb3c1
KK
5409 /* Relocate R_SH_TLS_IE_32 directly when statically linking. */
5410 if (r_type == R_SH_TLS_IE_32
5411 && ! htab->root.dynamic_sections_created)
5412 {
5413 off &= ~1;
5414 bfd_put_32 (output_bfd, tpoff (info, relocation),
5415 sgot->contents + off);
5416 bfd_put_32 (output_bfd, sgot->output_offset + off,
5417 contents + rel->r_offset);
5418 continue;
5419 }
5420
3376eaf5
KK
5421 if ((off & 1) != 0)
5422 off &= ~1;
26e41594 5423 else
3376eaf5
KK
5424 {
5425 Elf_Internal_Rela outrel;
947216bf 5426 bfd_byte *loc;
3376eaf5
KK
5427 int dr_type, indx;
5428
5429 if (srelgot == NULL)
5430 {
5431 srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
5432 BFD_ASSERT (srelgot != NULL);
5433 }
5434
5435 outrel.r_offset = (sgot->output_section->vma
5436 + sgot->output_offset + off);
5437
267fb3c1 5438 if (h == NULL || h->dynindx == -1)
c95b8a7a
KK
5439 indx = 0;
5440 else
5441 indx = h->dynindx;
267fb3c1 5442
3376eaf5
KK
5443 dr_type = (r_type == R_SH_TLS_GD_32 ? R_SH_TLS_DTPMOD32 :
5444 R_SH_TLS_TPOFF32);
5445 if (dr_type == R_SH_TLS_TPOFF32 && indx == 0)
5446 outrel.r_addend = relocation - dtpoff_base (info);
5447 else
5448 outrel.r_addend = 0;
5449 outrel.r_info = ELF32_R_INFO (indx, dr_type);
947216bf
AM
5450 loc = srelgot->contents;
5451 loc += srelgot->reloc_count++ * sizeof (Elf32_External_Rela);
3376eaf5
KK
5452 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
5453
5454 if (r_type == R_SH_TLS_GD_32)
5455 {
5456 if (indx == 0)
5457 {
5458 bfd_put_32 (output_bfd,
5459 relocation - dtpoff_base (info),
5460 sgot->contents + off + 4);
5461 }
5462 else
5463 {
5464 outrel.r_info = ELF32_R_INFO (indx,
5465 R_SH_TLS_DTPOFF32);
5466 outrel.r_offset += 4;
5467 outrel.r_addend = 0;
5468 srelgot->reloc_count++;
947216bf
AM
5469 loc += sizeof (Elf32_External_Rela);
5470 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
3376eaf5
KK
5471 }
5472 }
5473
5474 if (h != NULL)
5475 h->got.offset |= 1;
5476 else
5477 local_got_offsets[r_symndx] |= 1;
5478 }
5479
5480 if (off >= (bfd_vma) -2)
5481 abort ();
5482
5483 if (r_type == (int) ELF32_R_TYPE (rel->r_info))
5484 relocation = sgot->output_offset + off;
5485 else
5486 {
5487 bfd_vma offset;
5488 unsigned short insn;
5489
5490 /* GD->IE transition:
5491 mov.l 1f,r4; mova 2f,r0; mov.l 2f,r1; add r0,r1;
5492 jsr @r1; add r12,r4; bra 3f; nop; .align 2;
5493 1: .long x$TLSGD; 2: .long __tls_get_addr@PLT; 3:
5494 We change it into:
5495 mov.l 1f,r0; stc gbr,r4; mov.l @(r0,r12),r0; add r4,r0;
5496 nop; nop; bra 3f; nop; .align 2;
5497 1: .long x@TPOFF; 2:...; 3:. */
5498
5499 offset = rel->r_offset;
5500 BFD_ASSERT (offset >= 16);
5501 /* Size of GD instructions is 16 or 18. */
5502 offset -= 16;
5503 insn = bfd_get_16 (input_bfd, contents + offset + 0);
5504 if ((insn & 0xff00) == 0xc700)
5505 {
5506 BFD_ASSERT (offset >= 2);
5507 offset -= 2;
5508 insn = bfd_get_16 (input_bfd, contents + offset + 0);
5509 }
5510
5511 BFD_ASSERT ((insn & 0xff00) == 0xd400);
5512
5513 /* Replace mov.l 1f,R4 with mov.l 1f,r0. */
5514 bfd_put_16 (output_bfd, insn & 0xf0ff, contents + offset);
5515
5516 insn = bfd_get_16 (input_bfd, contents + offset + 2);
5517 BFD_ASSERT ((insn & 0xff00) == 0xc700);
5518 insn = bfd_get_16 (input_bfd, contents + offset + 4);
5519 BFD_ASSERT ((insn & 0xff00) == 0xd100);
5520 insn = bfd_get_16 (input_bfd, contents + offset + 6);
5521 BFD_ASSERT (insn == 0x310c);
5522 insn = bfd_get_16 (input_bfd, contents + offset + 8);
5523 BFD_ASSERT (insn == 0x410b);
5524 insn = bfd_get_16 (input_bfd, contents + offset + 10);
5525 BFD_ASSERT (insn == 0x34cc);
5526
5527 bfd_put_16 (output_bfd, 0x0412, contents + offset + 2);
5528 bfd_put_16 (output_bfd, 0x00ce, contents + offset + 4);
5529 bfd_put_16 (output_bfd, 0x304c, contents + offset + 6);
5530 bfd_put_16 (output_bfd, 0x0009, contents + offset + 8);
5531 bfd_put_16 (output_bfd, 0x0009, contents + offset + 10);
5532
5533 bfd_put_32 (output_bfd, sgot->output_offset + off,
5534 contents + rel->r_offset);
5535
5536 continue;
5537 }
5538
5539 addend = rel->r_addend;
5540
5541 goto final_link_relocate;
5542
5543 case R_SH_TLS_LD_32:
5544 if (! info->shared)
5545 {
5546 bfd_vma offset;
5547 unsigned short insn;
5548
5549 /* LD->LE transition:
5550 mov.l 1f,r4; mova 2f,r0; mov.l 2f,r1; add r0,r1;
5551 jsr @r1; add r12,r4; bra 3f; nop; .align 2;
5552 1: .long x$TLSLD; 2: .long __tls_get_addr@PLT; 3:
5553 We change it into:
5554 stc gbr,r0; nop; nop; nop;
5555 nop; nop; bra 3f; ...; 3:. */
5556
5557 offset = rel->r_offset;
5558 BFD_ASSERT (offset >= 16);
5559 /* Size of LD instructions is 16 or 18. */
5560 offset -= 16;
5561 insn = bfd_get_16 (input_bfd, contents + offset + 0);
5562 if ((insn & 0xff00) == 0xc700)
5563 {
5564 BFD_ASSERT (offset >= 2);
5565 offset -= 2;
5566 insn = bfd_get_16 (input_bfd, contents + offset + 0);
5567 }
5568
5569 BFD_ASSERT ((insn & 0xff00) == 0xd400);
5570 insn = bfd_get_16 (input_bfd, contents + offset + 2);
5571 BFD_ASSERT ((insn & 0xff00) == 0xc700);
5572 insn = bfd_get_16 (input_bfd, contents + offset + 4);
5573 BFD_ASSERT ((insn & 0xff00) == 0xd100);
5574 insn = bfd_get_16 (input_bfd, contents + offset + 6);
5575 BFD_ASSERT (insn == 0x310c);
5576 insn = bfd_get_16 (input_bfd, contents + offset + 8);
5577 BFD_ASSERT (insn == 0x410b);
5578 insn = bfd_get_16 (input_bfd, contents + offset + 10);
5579 BFD_ASSERT (insn == 0x34cc);
5580
5581 bfd_put_16 (output_bfd, 0x0012, contents + offset + 0);
5582 bfd_put_16 (output_bfd, 0x0009, contents + offset + 2);
5583 bfd_put_16 (output_bfd, 0x0009, contents + offset + 4);
5584 bfd_put_16 (output_bfd, 0x0009, contents + offset + 6);
5585 bfd_put_16 (output_bfd, 0x0009, contents + offset + 8);
5586 bfd_put_16 (output_bfd, 0x0009, contents + offset + 10);
5587
5588 continue;
5589 }
5590
5591 sgot = htab->sgot;
5592 if (sgot == NULL)
5593 abort ();
5594
5595 off = htab->tls_ldm_got.offset;
5596 if (off & 1)
5597 off &= ~1;
5598 else
5599 {
5600 Elf_Internal_Rela outrel;
947216bf 5601 bfd_byte *loc;
3376eaf5
KK
5602
5603 srelgot = htab->srelgot;
5604 if (srelgot == NULL)
5605 abort ();
5606
5607 outrel.r_offset = (sgot->output_section->vma
5608 + sgot->output_offset + off);
5609 outrel.r_addend = 0;
5610 outrel.r_info = ELF32_R_INFO (0, R_SH_TLS_DTPMOD32);
947216bf
AM
5611 loc = srelgot->contents;
5612 loc += srelgot->reloc_count++ * sizeof (Elf32_External_Rela);
3376eaf5
KK
5613 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
5614 htab->tls_ldm_got.offset |= 1;
5615 }
5616
5617 relocation = sgot->output_offset + off;
5618 addend = rel->r_addend;
5619
5620 goto final_link_relocate;
5621
5622 case R_SH_TLS_LDO_32:
5623 if (! info->shared)
267fb3c1 5624 relocation = tpoff (info, relocation);
3376eaf5
KK
5625 else
5626 relocation -= dtpoff_base (info);
5627
5628 addend = rel->r_addend;
5629 goto final_link_relocate;
5630
5631 case R_SH_TLS_LE_32:
5632 {
5633 int indx;
5634 Elf_Internal_Rela outrel;
947216bf 5635 bfd_byte *loc;
3376eaf5 5636
267fb3c1
KK
5637 if (! info->shared)
5638 {
5639 relocation = tpoff (info, relocation);
5640 addend = rel->r_addend;
5641 goto final_link_relocate;
5642 }
5643
3376eaf5
KK
5644 if (sreloc == NULL)
5645 {
5646 const char *name;
5647
5648 name = (bfd_elf_string_from_elf_section
5649 (input_bfd,
5650 elf_elfheader (input_bfd)->e_shstrndx,
5651 elf_section_data (input_section)->rel_hdr.sh_name));
5652 if (name == NULL)
b34976b6 5653 return FALSE;
3376eaf5
KK
5654
5655 BFD_ASSERT (strncmp (name, ".rela", 5) == 0
5656 && strcmp (bfd_get_section_name (input_bfd,
5657 input_section),
5658 name + 5) == 0);
5659
5660 sreloc = bfd_get_section_by_name (dynobj, name);
5661 BFD_ASSERT (sreloc != NULL);
5662 }
5663
267fb3c1
KK
5664 if (h == NULL || h->dynindx == -1)
5665 indx = 0;
5666 else
5667 indx = h->dynindx;
5668
3376eaf5
KK
5669 outrel.r_offset = (input_section->output_section->vma
5670 + input_section->output_offset
5671 + rel->r_offset);
5672 outrel.r_info = ELF32_R_INFO (indx, R_SH_TLS_TPOFF32);
5673 if (indx == 0)
5674 outrel.r_addend = relocation - dtpoff_base (info);
5675 else
5676 outrel.r_addend = 0;
3376eaf5 5677
947216bf
AM
5678 loc = sreloc->contents;
5679 loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rela);
5680 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
3376eaf5
KK
5681 continue;
5682 }
37c644f2
AO
5683 }
5684
146be91a 5685 relocation_done:
37c644f2
AO
5686 if (r != bfd_reloc_ok)
5687 {
5688 switch (r)
5689 {
5690 default:
5691 case bfd_reloc_outofrange:
5692 abort ();
5693 case bfd_reloc_overflow:
5694 {
5695 const char *name;
5696
5697 if (h != NULL)
dfeffb9f 5698 name = NULL;
37c644f2
AO
5699 else
5700 {
252b5132
RH
5701 name = (bfd_elf_string_from_elf_section
5702 (input_bfd, symtab_hdr->sh_link, sym->st_name));
5703 if (name == NULL)
b34976b6 5704 return FALSE;
252b5132
RH
5705 if (*name == '\0')
5706 name = bfd_section_name (input_bfd, sec);
5707 }
5708 if (! ((*info->callbacks->reloc_overflow)
dfeffb9f
L
5709 (info, (h ? &h->root : NULL), name, howto->name,
5710 (bfd_vma) 0, input_bfd, input_section,
5711 rel->r_offset)))
b34976b6 5712 return FALSE;
252b5132
RH
5713 }
5714 break;
5715 }
5716 }
5717 }
5718
b34976b6 5719 return TRUE;
252b5132
RH
5720}
5721
5722/* This is a version of bfd_generic_get_relocated_section_contents
5723 which uses sh_elf_relocate_section. */
5724
5725static bfd_byte *
09fd220b
KK
5726sh_elf_get_relocated_section_contents (bfd *output_bfd,
5727 struct bfd_link_info *link_info,
5728 struct bfd_link_order *link_order,
5729 bfd_byte *data,
5730 bfd_boolean relocatable,
5731 asymbol **symbols)
252b5132
RH
5732{
5733 Elf_Internal_Shdr *symtab_hdr;
5734 asection *input_section = link_order->u.indirect.section;
5735 bfd *input_bfd = input_section->owner;
5736 asection **sections = NULL;
5737 Elf_Internal_Rela *internal_relocs = NULL;
6cdc0ccc 5738 Elf_Internal_Sym *isymbuf = NULL;
252b5132
RH
5739
5740 /* We only need to handle the case of relaxing, or of having a
5741 particular set of section contents, specially. */
1049f94e 5742 if (relocatable
252b5132
RH
5743 || elf_section_data (input_section)->this_hdr.contents == NULL)
5744 return bfd_generic_get_relocated_section_contents (output_bfd, link_info,
5745 link_order, data,
1049f94e 5746 relocatable,
252b5132
RH
5747 symbols);
5748
5749 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
5750
5751 memcpy (data, elf_section_data (input_section)->this_hdr.contents,
eea6121a 5752 (size_t) input_section->size);
252b5132
RH
5753
5754 if ((input_section->flags & SEC_RELOC) != 0
5755 && input_section->reloc_count > 0)
5756 {
252b5132 5757 asection **secpp;
6cdc0ccc 5758 Elf_Internal_Sym *isym, *isymend;
9ad5cbcf 5759 bfd_size_type amt;
252b5132 5760
45d6a902 5761 internal_relocs = (_bfd_elf_link_read_relocs
09fd220b 5762 (input_bfd, input_section, NULL,
b34976b6 5763 (Elf_Internal_Rela *) NULL, FALSE));
252b5132
RH
5764 if (internal_relocs == NULL)
5765 goto error_return;
5766
6cdc0ccc
AM
5767 if (symtab_hdr->sh_info != 0)
5768 {
5769 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
5770 if (isymbuf == NULL)
5771 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr,
5772 symtab_hdr->sh_info, 0,
5773 NULL, NULL, NULL);
5774 if (isymbuf == NULL)
5775 goto error_return;
5776 }
252b5132 5777
9ad5cbcf
AM
5778 amt = symtab_hdr->sh_info;
5779 amt *= sizeof (asection *);
5780 sections = (asection **) bfd_malloc (amt);
5781 if (sections == NULL && amt != 0)
252b5132
RH
5782 goto error_return;
5783
6cdc0ccc
AM
5784 isymend = isymbuf + symtab_hdr->sh_info;
5785 for (isym = isymbuf, secpp = sections; isym < isymend; ++isym, ++secpp)
252b5132
RH
5786 {
5787 asection *isec;
5788
6cdc0ccc 5789 if (isym->st_shndx == SHN_UNDEF)
252b5132 5790 isec = bfd_und_section_ptr;
6cdc0ccc 5791 else if (isym->st_shndx == SHN_ABS)
252b5132 5792 isec = bfd_abs_section_ptr;
6cdc0ccc 5793 else if (isym->st_shndx == SHN_COMMON)
252b5132
RH
5794 isec = bfd_com_section_ptr;
5795 else
6cdc0ccc 5796 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
252b5132
RH
5797
5798 *secpp = isec;
5799 }
5800
5801 if (! sh_elf_relocate_section (output_bfd, link_info, input_bfd,
5802 input_section, data, internal_relocs,
6cdc0ccc 5803 isymbuf, sections))
252b5132
RH
5804 goto error_return;
5805
5806 if (sections != NULL)
5807 free (sections);
6cdc0ccc
AM
5808 if (isymbuf != NULL
5809 && symtab_hdr->contents != (unsigned char *) isymbuf)
5810 free (isymbuf);
5811 if (elf_section_data (input_section)->relocs != internal_relocs)
252b5132 5812 free (internal_relocs);
252b5132
RH
5813 }
5814
5815 return data;
5816
5817 error_return:
252b5132
RH
5818 if (sections != NULL)
5819 free (sections);
6cdc0ccc
AM
5820 if (isymbuf != NULL
5821 && symtab_hdr->contents != (unsigned char *) isymbuf)
5822 free (isymbuf);
5823 if (internal_relocs != NULL
5824 && elf_section_data (input_section)->relocs != internal_relocs)
5825 free (internal_relocs);
252b5132
RH
5826 return NULL;
5827}
917583ad 5828
3376eaf5
KK
5829/* Return the base VMA address which should be subtracted from real addresses
5830 when resolving @dtpoff relocation.
5831 This is PT_TLS segment p_vaddr. */
5832
5833static bfd_vma
09fd220b 5834dtpoff_base (struct bfd_link_info *info)
3376eaf5 5835{
e1918d23
AM
5836 /* If tls_sec is NULL, we should have signalled an error already. */
5837 if (elf_hash_table (info)->tls_sec == NULL)
267fb3c1 5838 return 0;
e1918d23 5839 return elf_hash_table (info)->tls_sec->vma;
3376eaf5
KK
5840}
5841
267fb3c1
KK
5842/* Return the relocation value for R_SH_TLS_TPOFF32.. */
5843
5844static bfd_vma
09fd220b 5845tpoff (struct bfd_link_info *info, bfd_vma address)
267fb3c1 5846{
e1918d23
AM
5847 /* If tls_sec is NULL, we should have signalled an error already. */
5848 if (elf_hash_table (info)->tls_sec == NULL)
267fb3c1
KK
5849 return 0;
5850 /* SH TLS ABI is variant I and static TLS block start just after tcbhead
5851 structure which has 2 pointer fields. */
044ad7e3
KK
5852 return (address - elf_hash_table (info)->tls_sec->vma
5853 + align_power ((bfd_vma) 8,
5854 elf_hash_table (info)->tls_sec->alignment_power));
267fb3c1
KK
5855}
5856
252b5132 5857static asection *
09fd220b
KK
5858sh_elf_gc_mark_hook (asection *sec,
5859 struct bfd_link_info *info ATTRIBUTE_UNUSED,
5860 Elf_Internal_Rela *rel, struct elf_link_hash_entry *h,
5861 Elf_Internal_Sym *sym)
252b5132
RH
5862{
5863 if (h != NULL)
5864 {
5865 switch (ELF32_R_TYPE (rel->r_info))
435b1e90
KH
5866 {
5867 case R_SH_GNU_VTINHERIT:
5868 case R_SH_GNU_VTENTRY:
5869 break;
252b5132 5870
435b1e90 5871 default:
4972a8e9
SC
5872#ifdef INCLUDE_SHMEDIA
5873 while (h->root.type == bfd_link_hash_indirect
5874 && h->root.u.i.link)
5875 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5876#endif
435b1e90
KH
5877 switch (h->root.type)
5878 {
5879 case bfd_link_hash_defined:
5880 case bfd_link_hash_defweak:
5881 return h->root.u.def.section;
252b5132 5882
435b1e90
KH
5883 case bfd_link_hash_common:
5884 return h->root.u.c.p->section;
e049a0de 5885
435b1e90
KH
5886 default:
5887 break;
5888 }
5889 }
5890 }
5891 else
1e2f5b6e 5892 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
9ad5cbcf 5893
252b5132
RH
5894 return NULL;
5895}
5896
37c644f2
AO
5897/* Update the got entry reference counts for the section being removed. */
5898
b34976b6 5899static bfd_boolean
09fd220b
KK
5900sh_elf_gc_sweep_hook (bfd *abfd, struct bfd_link_info *info,
5901 asection *sec, const Elf_Internal_Rela *relocs)
252b5132 5902{
067653c5
AM
5903 Elf_Internal_Shdr *symtab_hdr;
5904 struct elf_link_hash_entry **sym_hashes;
5905 bfd_signed_vma *local_got_refcounts;
5906 const Elf_Internal_Rela *rel, *relend;
067653c5
AM
5907
5908 elf_section_data (sec)->local_dynrel = NULL;
5909
5910 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
5911 sym_hashes = elf_sym_hashes (abfd);
5912 local_got_refcounts = elf_local_got_refcounts (abfd);
5913
5914 relend = relocs + sec->reloc_count;
5915 for (rel = relocs; rel < relend; rel++)
396a6083 5916 {
26e41594
AM
5917 unsigned long r_symndx;
5918 unsigned int r_type;
5919 struct elf_link_hash_entry *h = NULL;
396a6083
SC
5920#ifdef INCLUDE_SHMEDIA
5921 int seen_stt_datalabel = 0;
5922#endif
3376eaf5 5923
396a6083 5924 r_symndx = ELF32_R_SYM (rel->r_info);
26e41594 5925 if (r_symndx >= symtab_hdr->sh_info)
396a6083 5926 {
26e41594
AM
5927 struct elf_sh_link_hash_entry *eh;
5928 struct elf_sh_dyn_relocs **pp;
5929 struct elf_sh_dyn_relocs *p;
5930
396a6083 5931 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
396a6083
SC
5932 while (h->root.type == bfd_link_hash_indirect
5933 || h->root.type == bfd_link_hash_warning)
5934 {
3eb128b2 5935#ifdef INCLUDE_SHMEDIA
396a6083 5936 seen_stt_datalabel |= h->type == STT_DATALABEL;
3eb128b2 5937#endif
396a6083
SC
5938 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5939 }
26e41594
AM
5940 eh = (struct elf_sh_link_hash_entry *) h;
5941 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; pp = &p->next)
5942 if (p->sec == sec)
5943 {
5944 /* Everything must go for SEC. */
5945 *pp = p->next;
5946 break;
5947 }
396a6083 5948 }
067653c5 5949
26e41594
AM
5950 r_type = ELF32_R_TYPE (rel->r_info);
5951 switch (sh_elf_optimized_tls_reloc (info, r_type, h != NULL))
396a6083
SC
5952 {
5953 case R_SH_TLS_LD_32:
5954 if (sh_elf_hash_table (info)->tls_ldm_got.refcount > 0)
5955 sh_elf_hash_table (info)->tls_ldm_got.refcount -= 1;
5956 break;
5957
5958 case R_SH_GOT32:
5959 case R_SH_GOTOFF:
5960 case R_SH_GOTPC:
5961#ifdef INCLUDE_SHMEDIA
5962 case R_SH_GOT_LOW16:
5963 case R_SH_GOT_MEDLOW16:
5964 case R_SH_GOT_MEDHI16:
5965 case R_SH_GOT_HI16:
5966 case R_SH_GOT10BY4:
5967 case R_SH_GOT10BY8:
5968 case R_SH_GOTOFF_LOW16:
5969 case R_SH_GOTOFF_MEDLOW16:
5970 case R_SH_GOTOFF_MEDHI16:
5971 case R_SH_GOTOFF_HI16:
5972 case R_SH_GOTPC_LOW16:
5973 case R_SH_GOTPC_MEDLOW16:
5974 case R_SH_GOTPC_MEDHI16:
5975 case R_SH_GOTPC_HI16:
5976#endif
5977 case R_SH_TLS_GD_32:
5978 case R_SH_TLS_IE_32:
5979 if (h != NULL)
5980 {
5981#ifdef INCLUDE_SHMEDIA
5982 if (seen_stt_datalabel)
5983 {
26e41594
AM
5984 struct elf_sh_link_hash_entry *eh;
5985 eh = (struct elf_sh_link_hash_entry *) h;
396a6083
SC
5986 if (eh->datalabel_got.refcount > 0)
5987 eh->datalabel_got.refcount -= 1;
5988 }
5989 else
5990#endif
5991 if (h->got.refcount > 0)
5992 h->got.refcount -= 1;
5993 }
5994 else if (local_got_refcounts != NULL)
5995 {
5996#ifdef INCLUDE_SHMEDIA
5997 if (rel->r_addend & 1)
5998 {
5999 if (local_got_refcounts[symtab_hdr->sh_info + r_symndx] > 0)
6000 local_got_refcounts[symtab_hdr->sh_info + r_symndx] -= 1;
6001 }
6002 else
6003#endif
6004 if (local_got_refcounts[r_symndx] > 0)
6005 local_got_refcounts[r_symndx] -= 1;
6006 }
6007 break;
6008
6009 case R_SH_DIR32:
6010 case R_SH_REL32:
26e41594
AM
6011 if (info->shared)
6012 break;
6013 /* Fall thru */
396a6083
SC
6014
6015 case R_SH_PLT32:
6016#ifdef INCLUDE_SHMEDIA
6017 case R_SH_PLT_LOW16:
6018 case R_SH_PLT_MEDLOW16:
6019 case R_SH_PLT_MEDHI16:
6020 case R_SH_PLT_HI16:
6021#endif
6022 if (h != NULL)
6023 {
6024 if (h->plt.refcount > 0)
6025 h->plt.refcount -= 1;
6026 }
6027 break;
067653c5 6028
396a6083
SC
6029 case R_SH_GOTPLT32:
6030#ifdef INCLUDE_SHMEDIA
6031 case R_SH_GOTPLT_LOW16:
6032 case R_SH_GOTPLT_MEDLOW16:
6033 case R_SH_GOTPLT_MEDHI16:
6034 case R_SH_GOTPLT_HI16:
6035 case R_SH_GOTPLT10BY4:
6036 case R_SH_GOTPLT10BY8:
6037#endif
6038 if (h != NULL)
6039 {
26e41594
AM
6040 struct elf_sh_link_hash_entry *eh;
6041 eh = (struct elf_sh_link_hash_entry *) h;
396a6083 6042 if (eh->gotplt_refcount > 0)
067653c5 6043 {
396a6083
SC
6044 eh->gotplt_refcount -= 1;
6045 if (h->plt.refcount > 0)
6046 h->plt.refcount -= 1;
067653c5 6047 }
067653c5 6048#ifdef INCLUDE_SHMEDIA
396a6083
SC
6049 else if (seen_stt_datalabel)
6050 {
6051 if (eh->datalabel_got.refcount > 0)
6052 eh->datalabel_got.refcount -= 1;
6053 }
067653c5 6054#endif
396a6083
SC
6055 else if (h->got.refcount > 0)
6056 h->got.refcount -= 1;
6057 }
6058 else if (local_got_refcounts != NULL)
6059 {
067653c5 6060#ifdef INCLUDE_SHMEDIA
396a6083
SC
6061 if (rel->r_addend & 1)
6062 {
6063 if (local_got_refcounts[symtab_hdr->sh_info + r_symndx] > 0)
6064 local_got_refcounts[symtab_hdr->sh_info + r_symndx] -= 1;
6065 }
6066 else
067653c5 6067#endif
396a6083
SC
6068 if (local_got_refcounts[r_symndx] > 0)
6069 local_got_refcounts[r_symndx] -= 1;
6070 }
6071 break;
067653c5 6072
396a6083
SC
6073 default:
6074 break;
6075 }
6076 }
067653c5 6077
b34976b6 6078 return TRUE;
252b5132
RH
6079}
6080
067653c5
AM
6081/* Copy the extra info we tack onto an elf_link_hash_entry. */
6082
6083static void
fcfa13d2 6084sh_elf_copy_indirect_symbol (struct bfd_link_info *info,
09fd220b
KK
6085 struct elf_link_hash_entry *dir,
6086 struct elf_link_hash_entry *ind)
067653c5
AM
6087{
6088 struct elf_sh_link_hash_entry *edir, *eind;
6089
6090 edir = (struct elf_sh_link_hash_entry *) dir;
6091 eind = (struct elf_sh_link_hash_entry *) ind;
6092
6093 if (eind->dyn_relocs != NULL)
6094 {
6095 if (edir->dyn_relocs != NULL)
6096 {
6097 struct elf_sh_dyn_relocs **pp;
6098 struct elf_sh_dyn_relocs *p;
6099
fcfa13d2 6100 /* Add reloc counts against the indirect sym to the direct sym
067653c5
AM
6101 list. Merge any entries against the same section. */
6102 for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
6103 {
6104 struct elf_sh_dyn_relocs *q;
6105
6106 for (q = edir->dyn_relocs; q != NULL; q = q->next)
6107 if (q->sec == p->sec)
6108 {
6109 q->pc_count += p->pc_count;
6110 q->count += p->count;
6111 *pp = p->next;
6112 break;
6113 }
6114 if (q == NULL)
6115 pp = &p->next;
6116 }
6117 *pp = edir->dyn_relocs;
6118 }
6119
6120 edir->dyn_relocs = eind->dyn_relocs;
6121 eind->dyn_relocs = NULL;
6122 }
4989d792
SC
6123 edir->gotplt_refcount = eind->gotplt_refcount;
6124 eind->gotplt_refcount = 0;
396a6083 6125#ifdef INCLUDE_SHMEDIA
fcfa13d2
AM
6126 edir->datalabel_got.refcount += eind->datalabel_got.refcount;
6127 eind->datalabel_got.refcount = 0;
396a6083 6128#endif
067653c5 6129
3376eaf5
KK
6130 if (ind->root.type == bfd_link_hash_indirect
6131 && dir->got.refcount <= 0)
6132 {
6133 edir->tls_type = eind->tls_type;
6134 eind->tls_type = GOT_UNKNOWN;
6135 }
6136
04e534c3 6137 if (ind->root.type != bfd_link_hash_indirect
f5385ebf
AM
6138 && dir->dynamic_adjusted)
6139 {
6140 /* If called to transfer flags for a weakdef during processing
6141 of elf_adjust_dynamic_symbol, don't copy non_got_ref.
6142 We clear it ourselves for ELIMINATE_COPY_RELOCS. */
6143 dir->ref_dynamic |= ind->ref_dynamic;
6144 dir->ref_regular |= ind->ref_regular;
6145 dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
6146 dir->needs_plt |= ind->needs_plt;
6147 }
04e534c3 6148 else
fcfa13d2 6149 _bfd_elf_link_hash_copy_indirect (info, dir, ind);
067653c5
AM
6150}
6151
3376eaf5 6152static int
09fd220b
KK
6153sh_elf_optimized_tls_reloc (struct bfd_link_info *info, int r_type,
6154 int is_local)
3376eaf5
KK
6155{
6156 if (info->shared)
6157 return r_type;
6158
6159 switch (r_type)
6160 {
6161 case R_SH_TLS_GD_32:
6162 case R_SH_TLS_IE_32:
6163 if (is_local)
6164 return R_SH_TLS_LE_32;
6165 return R_SH_TLS_IE_32;
6166 case R_SH_TLS_LD_32:
6167 return R_SH_TLS_LE_32;
6168 }
6169
6170 return r_type;
6171}
6172
252b5132
RH
6173/* Look through the relocs for a section during the first phase.
6174 Since we don't do .gots or .plts, we just need to consider the
6175 virtual table relocs for gc. */
435b1e90 6176
b34976b6 6177static bfd_boolean
09fd220b
KK
6178sh_elf_check_relocs (bfd *abfd, struct bfd_link_info *info, asection *sec,
6179 const Elf_Internal_Rela *relocs)
252b5132
RH
6180{
6181 Elf_Internal_Shdr *symtab_hdr;
6182 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
067653c5 6183 struct elf_sh_link_hash_table *htab;
252b5132
RH
6184 const Elf_Internal_Rela *rel;
6185 const Elf_Internal_Rela *rel_end;
37c644f2
AO
6186 bfd_vma *local_got_offsets;
6187 asection *sgot;
6188 asection *srelgot;
6189 asection *sreloc;
3376eaf5
KK
6190 unsigned int r_type;
6191 int tls_type, old_tls_type;
435b1e90 6192
37c644f2
AO
6193 sgot = NULL;
6194 srelgot = NULL;
6195 sreloc = NULL;
6196
1049f94e 6197 if (info->relocatable)
b34976b6 6198 return TRUE;
435b1e90 6199
252b5132
RH
6200 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
6201 sym_hashes = elf_sym_hashes (abfd);
435b1e90 6202 sym_hashes_end = sym_hashes + symtab_hdr->sh_size/sizeof (Elf32_External_Sym);
252b5132
RH
6203 if (!elf_bad_symtab (abfd))
6204 sym_hashes_end -= symtab_hdr->sh_info;
435b1e90 6205
067653c5 6206 htab = sh_elf_hash_table (info);
37c644f2
AO
6207 local_got_offsets = elf_local_got_offsets (abfd);
6208
252b5132
RH
6209 rel_end = relocs + sec->reloc_count;
6210 for (rel = relocs; rel < rel_end; rel++)
6211 {
6212 struct elf_link_hash_entry *h;
6213 unsigned long r_symndx;
396a6083
SC
6214#ifdef INCLUDE_SHMEDIA
6215 int seen_stt_datalabel = 0;
6216#endif
435b1e90 6217
252b5132 6218 r_symndx = ELF32_R_SYM (rel->r_info);
3376eaf5
KK
6219 r_type = ELF32_R_TYPE (rel->r_info);
6220
252b5132 6221 if (r_symndx < symtab_hdr->sh_info)
435b1e90 6222 h = NULL;
252b5132 6223 else
396a6083
SC
6224 {
6225 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
396a6083
SC
6226 while (h->root.type == bfd_link_hash_indirect
6227 || h->root.type == bfd_link_hash_warning)
6228 {
973a3492 6229#ifdef INCLUDE_SHMEDIA
396a6083 6230 seen_stt_datalabel |= h->type == STT_DATALABEL;
973a3492 6231#endif
396a6083
SC
6232 h = (struct elf_link_hash_entry *) h->root.u.i.link;
6233 }
396a6083 6234 }
435b1e90 6235
3376eaf5
KK
6236 r_type = sh_elf_optimized_tls_reloc (info, r_type, h == NULL);
6237 if (! info->shared
6238 && r_type == R_SH_TLS_IE_32
6239 && h != NULL
6240 && h->root.type != bfd_link_hash_undefined
6241 && h->root.type != bfd_link_hash_undefweak
6242 && (h->dynindx == -1
f5385ebf 6243 || h->def_regular))
3376eaf5
KK
6244 r_type = R_SH_TLS_LE_32;
6245
37c644f2 6246 /* Some relocs require a global offset table. */
067653c5 6247 if (htab->sgot == NULL)
37c644f2 6248 {
3376eaf5 6249 switch (r_type)
37c644f2 6250 {
fbca6ad9 6251 case R_SH_GOTPLT32:
37c644f2
AO
6252 case R_SH_GOT32:
6253 case R_SH_GOTOFF:
6254 case R_SH_GOTPC:
fbca6ad9
AO
6255#ifdef INCLUDE_SHMEDIA
6256 case R_SH_GOTPLT_LOW16:
6257 case R_SH_GOTPLT_MEDLOW16:
6258 case R_SH_GOTPLT_MEDHI16:
6259 case R_SH_GOTPLT_HI16:
6260 case R_SH_GOTPLT10BY4:
6261 case R_SH_GOTPLT10BY8:
6262 case R_SH_GOT_LOW16:
6263 case R_SH_GOT_MEDLOW16:
6264 case R_SH_GOT_MEDHI16:
6265 case R_SH_GOT_HI16:
6266 case R_SH_GOT10BY4:
6267 case R_SH_GOT10BY8:
6268 case R_SH_GOTOFF_LOW16:
6269 case R_SH_GOTOFF_MEDLOW16:
6270 case R_SH_GOTOFF_MEDHI16:
6271 case R_SH_GOTOFF_HI16:
6272 case R_SH_GOTPC_LOW16:
6273 case R_SH_GOTPC_MEDLOW16:
6274 case R_SH_GOTPC_MEDHI16:
6275 case R_SH_GOTPC_HI16:
6276#endif
3376eaf5
KK
6277 case R_SH_TLS_GD_32:
6278 case R_SH_TLS_LD_32:
6279 case R_SH_TLS_IE_32:
2293c499
AM
6280 if (htab->sgot == NULL)
6281 {
6282 if (htab->root.dynobj == NULL)
6283 htab->root.dynobj = abfd;
6284 if (!create_got_section (htab->root.dynobj, info))
6285 return FALSE;
6286 }
37c644f2
AO
6287 break;
6288
6289 default:
6290 break;
6291 }
6292 }
6293
3376eaf5 6294 switch (r_type)
067653c5
AM
6295 {
6296 /* This relocation describes the C++ object vtable hierarchy.
6297 Reconstruct it for later use during GC. */
435b1e90 6298 case R_SH_GNU_VTINHERIT:
c152c796 6299 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
b34976b6 6300 return FALSE;
435b1e90
KH
6301 break;
6302
067653c5
AM
6303 /* This relocation describes which C++ vtable entries are actually
6304 used. Record for later use during GC. */
435b1e90 6305 case R_SH_GNU_VTENTRY:
c152c796 6306 if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
b34976b6 6307 return FALSE;
435b1e90 6308 break;
37c644f2 6309
3376eaf5
KK
6310 case R_SH_TLS_IE_32:
6311 if (info->shared)
6312 info->flags |= DF_STATIC_TLS;
6313
6314 /* FALLTHROUGH */
267fb3c1 6315 force_got:
3376eaf5 6316 case R_SH_TLS_GD_32:
37c644f2 6317 case R_SH_GOT32:
fbca6ad9
AO
6318#ifdef INCLUDE_SHMEDIA
6319 case R_SH_GOT_LOW16:
6320 case R_SH_GOT_MEDLOW16:
6321 case R_SH_GOT_MEDHI16:
6322 case R_SH_GOT_HI16:
6323 case R_SH_GOT10BY4:
6324 case R_SH_GOT10BY8:
6325#endif
3376eaf5
KK
6326 switch (r_type)
6327 {
6328 default:
6329 tls_type = GOT_NORMAL;
6330 break;
6331 case R_SH_TLS_GD_32:
6332 tls_type = GOT_TLS_GD;
6333 break;
6334 case R_SH_TLS_IE_32:
6335 tls_type = GOT_TLS_IE;
6336 break;
6337 }
6338
37c644f2 6339 if (h != NULL)
3376eaf5 6340 {
396a6083
SC
6341#ifdef INCLUDE_SHMEDIA
6342 if (seen_stt_datalabel)
6343 {
b34976b6
AM
6344 struct elf_sh_link_hash_entry *eh
6345 = (struct elf_sh_link_hash_entry *) h;
396a6083
SC
6346
6347 eh->datalabel_got.refcount += 1;
6348 }
6349 else
6350#endif
6351 h->got.refcount += 1;
3376eaf5
KK
6352 old_tls_type = sh_elf_hash_entry (h)->tls_type;
6353 }
37c644f2
AO
6354 else
6355 {
067653c5
AM
6356 bfd_signed_vma *local_got_refcounts;
6357
435b1e90 6358 /* This is a global offset table entry for a local
067653c5
AM
6359 symbol. */
6360 local_got_refcounts = elf_local_got_refcounts (abfd);
6361 if (local_got_refcounts == NULL)
37c644f2 6362 {
dc810e39 6363 bfd_size_type size;
37c644f2 6364
dc810e39 6365 size = symtab_hdr->sh_info;
067653c5 6366 size *= sizeof (bfd_signed_vma);
fbca6ad9
AO
6367#ifdef INCLUDE_SHMEDIA
6368 /* Reserve space for both the datalabel and
6369 codelabel local GOT offsets. */
6370 size *= 2;
6371#endif
3376eaf5 6372 size += symtab_hdr->sh_info;
067653c5
AM
6373 local_got_refcounts = ((bfd_signed_vma *)
6374 bfd_zalloc (abfd, size));
6375 if (local_got_refcounts == NULL)
b34976b6 6376 return FALSE;
067653c5 6377 elf_local_got_refcounts (abfd) = local_got_refcounts;
3376eaf5
KK
6378#ifdef INCLUDE_SHMEDIA
6379 /* Take care of both the datalabel and codelabel local
6380 GOT offsets. */
6381 sh_elf_local_got_tls_type (abfd)
6382 = (char *) (local_got_refcounts + 2 * symtab_hdr->sh_info);
6383#else
6384 sh_elf_local_got_tls_type (abfd)
6385 = (char *) (local_got_refcounts + symtab_hdr->sh_info);
6386#endif
37c644f2 6387 }
396a6083
SC
6388#ifdef INCLUDE_SHMEDIA
6389 if (rel->r_addend & 1)
6390 local_got_refcounts[symtab_hdr->sh_info + r_symndx] += 1;
6391 else
6392#endif
6393 local_got_refcounts[r_symndx] += 1;
3376eaf5
KK
6394 old_tls_type = sh_elf_local_got_tls_type (abfd) [r_symndx];
6395 }
6396
6397 /* If a TLS symbol is accessed using IE at least once,
6398 there is no point to use dynamic model for it. */
6399 if (old_tls_type != tls_type && old_tls_type != GOT_UNKNOWN
6400 && (old_tls_type != GOT_TLS_GD || tls_type != GOT_TLS_IE))
6401 {
6402 if (old_tls_type == GOT_TLS_IE && tls_type == GOT_TLS_GD)
6403 tls_type = GOT_TLS_IE;
6404 else
6405 {
6406 (*_bfd_error_handler)
d003868e
AM
6407 (_("%B: `%s' accessed both as normal and thread local symbol"),
6408 abfd, h->root.root.string);
b34976b6 6409 return FALSE;
3376eaf5
KK
6410 }
6411 }
6412
6413 if (old_tls_type != tls_type)
6414 {
6415 if (h != NULL)
6416 sh_elf_hash_entry (h)->tls_type = tls_type;
6417 else
6418 sh_elf_local_got_tls_type (abfd) [r_symndx] = tls_type;
37c644f2 6419 }
3376eaf5
KK
6420
6421 break;
6422
6423 case R_SH_TLS_LD_32:
6424 sh_elf_hash_table(info)->tls_ldm_got.refcount += 1;
37c644f2
AO
6425 break;
6426
fbca6ad9
AO
6427 case R_SH_GOTPLT32:
6428#ifdef INCLUDE_SHMEDIA
6429 case R_SH_GOTPLT_LOW16:
6430 case R_SH_GOTPLT_MEDLOW16:
6431 case R_SH_GOTPLT_MEDHI16:
6432 case R_SH_GOTPLT_HI16:
6433 case R_SH_GOTPLT10BY4:
6434 case R_SH_GOTPLT10BY8:
6435#endif
6436 /* If this is a local symbol, we resolve it directly without
6437 creating a procedure linkage table entry. */
6438
6439 if (h == NULL
f5385ebf 6440 || h->forced_local
fbca6ad9
AO
6441 || ! info->shared
6442 || info->symbolic
067653c5 6443 || h->dynindx == -1)
fbca6ad9
AO
6444 goto force_got;
6445
f5385ebf 6446 h->needs_plt = 1;
067653c5 6447 h->plt.refcount += 1;
4989d792 6448 ((struct elf_sh_link_hash_entry *) h)->gotplt_refcount += 1;
fbca6ad9
AO
6449
6450 break;
6451
37c644f2 6452 case R_SH_PLT32:
fbca6ad9
AO
6453#ifdef INCLUDE_SHMEDIA
6454 case R_SH_PLT_LOW16:
6455 case R_SH_PLT_MEDLOW16:
6456 case R_SH_PLT_MEDHI16:
6457 case R_SH_PLT_HI16:
6458#endif
37c644f2
AO
6459 /* This symbol requires a procedure linkage table entry. We
6460 actually build the entry in adjust_dynamic_symbol,
6461 because this might be a case of linking PIC code which is
6462 never referenced by a dynamic object, in which case we
6463 don't need to generate a procedure linkage table entry
6464 after all. */
6465
6466 /* If this is a local symbol, we resolve it directly without
6467 creating a procedure linkage table entry. */
6468 if (h == NULL)
6469 continue;
6470
f5385ebf 6471 if (h->forced_local)
37c644f2
AO
6472 break;
6473
f5385ebf 6474 h->needs_plt = 1;
067653c5 6475 h->plt.refcount += 1;
37c644f2
AO
6476 break;
6477
6478 case R_SH_DIR32:
6479 case R_SH_REL32:
46e993b9
KK
6480#ifdef INCLUDE_SHMEDIA
6481 case R_SH_IMM_LOW16_PCREL:
6482 case R_SH_IMM_MEDLOW16_PCREL:
6483 case R_SH_IMM_MEDHI16_PCREL:
6484 case R_SH_IMM_HI16_PCREL:
6485#endif
067653c5
AM
6486 if (h != NULL && ! info->shared)
6487 {
f5385ebf 6488 h->non_got_ref = 1;
067653c5
AM
6489 h->plt.refcount += 1;
6490 }
37c644f2
AO
6491
6492 /* If we are creating a shared library, and this is a reloc
6493 against a global symbol, or a non PC relative reloc
6494 against a local symbol, then we need to copy the reloc
6495 into the shared library. However, if we are linking with
6496 -Bsymbolic, we do not need to copy a reloc against a
6497 global symbol which is defined in an object we are
6498 including in the link (i.e., DEF_REGULAR is set). At
6499 this point we have not seen all the input files, so it is
6500 possible that DEF_REGULAR is not set now but will be set
6501 later (it is never cleared). We account for that
6502 possibility below by storing information in the
067653c5
AM
6503 dyn_relocs field of the hash table entry. A similar
6504 situation occurs when creating shared libraries and symbol
6505 visibility changes render the symbol local.
6506
6507 If on the other hand, we are creating an executable, we
6508 may need to keep relocations for symbols satisfied by a
6509 dynamic library if we manage to avoid copy relocs for the
6510 symbol. */
6511 if ((info->shared
6512 && (sec->flags & SEC_ALLOC) != 0
3376eaf5 6513 && (r_type != R_SH_REL32
067653c5
AM
6514 || (h != NULL
6515 && (! info->symbolic
6516 || h->root.type == bfd_link_hash_defweak
f5385ebf 6517 || !h->def_regular))))
067653c5
AM
6518 || (! info->shared
6519 && (sec->flags & SEC_ALLOC) != 0
6520 && h != NULL
6521 && (h->root.type == bfd_link_hash_defweak
f5385ebf 6522 || !h->def_regular)))
37c644f2 6523 {
067653c5
AM
6524 struct elf_sh_dyn_relocs *p;
6525 struct elf_sh_dyn_relocs **head;
6526
2293c499
AM
6527 if (htab->root.dynobj == NULL)
6528 htab->root.dynobj = abfd;
067653c5 6529
37c644f2
AO
6530 /* When creating a shared object, we must copy these
6531 reloc types into the output file. We create a reloc
6532 section in dynobj and make room for this reloc. */
6533 if (sreloc == NULL)
6534 {
6535 const char *name;
6536
6537 name = (bfd_elf_string_from_elf_section
6538 (abfd,
6539 elf_elfheader (abfd)->e_shstrndx,
6540 elf_section_data (sec)->rel_hdr.sh_name));
6541 if (name == NULL)
b34976b6 6542 return FALSE;
37c644f2
AO
6543
6544 BFD_ASSERT (strncmp (name, ".rela", 5) == 0
6545 && strcmp (bfd_get_section_name (abfd, sec),
6546 name + 5) == 0);
6547
2293c499 6548 sreloc = bfd_get_section_by_name (htab->root.dynobj, name);
37c644f2
AO
6549 if (sreloc == NULL)
6550 {
6551 flagword flags;
6552
37c644f2
AO
6553 flags = (SEC_HAS_CONTENTS | SEC_READONLY
6554 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
6555 if ((sec->flags & SEC_ALLOC) != 0)
6556 flags |= SEC_ALLOC | SEC_LOAD;
3496cb2a
L
6557 sreloc = bfd_make_section_with_flags (htab->root.dynobj,
6558 name,
6559 flags);
37c644f2 6560 if (sreloc == NULL
2293c499
AM
6561 || ! bfd_set_section_alignment (htab->root.dynobj,
6562 sreloc, 2))
b34976b6 6563 return FALSE;
37c644f2 6564 }
067653c5 6565 elf_section_data (sec)->sreloc = sreloc;
37c644f2
AO
6566 }
6567
067653c5
AM
6568 /* If this is a global symbol, we count the number of
6569 relocations we need for this symbol. */
6570 if (h != NULL)
6571 head = &((struct elf_sh_link_hash_entry *) h)->dyn_relocs;
6572 else
37c644f2 6573 {
067653c5 6574 asection *s;
6edfbbad 6575 void *vpp;
37c644f2 6576
067653c5
AM
6577 /* Track dynamic relocs needed for local syms too. */
6578 s = bfd_section_from_r_symndx (abfd, &htab->sym_sec,
6579 sec, r_symndx);
6580 if (s == NULL)
b34976b6 6581 return FALSE;
37c644f2 6582
6edfbbad
DJ
6583 vpp = &elf_section_data (s)->local_dynrel;
6584 head = (struct elf_sh_dyn_relocs **) vpp;
067653c5 6585 }
37c644f2 6586
067653c5
AM
6587 p = *head;
6588 if (p == NULL || p->sec != sec)
6589 {
6590 bfd_size_type amt = sizeof (*p);
2293c499 6591 p = bfd_alloc (htab->root.dynobj, amt);
37c644f2 6592 if (p == NULL)
b34976b6 6593 return FALSE;
067653c5
AM
6594 p->next = *head;
6595 *head = p;
6596 p->sec = sec;
6597 p->count = 0;
6598 p->pc_count = 0;
37c644f2 6599 }
067653c5
AM
6600
6601 p->count += 1;
46e993b9
KK
6602 if (r_type == R_SH_REL32
6603#ifdef INCLUDE_SHMEDIA
6604 || r_type == R_SH_IMM_LOW16_PCREL
6605 || r_type == R_SH_IMM_MEDLOW16_PCREL
6606 || r_type == R_SH_IMM_MEDHI16_PCREL
6607 || r_type == R_SH_IMM_HI16_PCREL
6608#endif
6609 )
067653c5 6610 p->pc_count += 1;
37c644f2
AO
6611 }
6612
6613 break;
3376eaf5
KK
6614
6615 case R_SH_TLS_LE_32:
6616 if (info->shared)
6617 {
d003868e
AM
6618 (*_bfd_error_handler)
6619 (_("%B: TLS local exec code cannot be linked into shared objects"),
6620 abfd);
b34976b6 6621 return FALSE;
3376eaf5
KK
6622 }
6623
267fb3c1 6624 break;
3376eaf5 6625
3376eaf5 6626 case R_SH_TLS_LDO_32:
267fb3c1 6627 /* Nothing to do. */
3376eaf5
KK
6628 break;
6629
6630 default:
6631 break;
435b1e90 6632 }
252b5132 6633 }
435b1e90 6634
b34976b6 6635 return TRUE;
252b5132
RH
6636}
6637
fbca6ad9 6638#ifndef sh_elf_set_mach_from_flags
f6f9408f
JR
6639static unsigned int sh_ef_bfd_table[] = { EF_SH_BFD_TABLE };
6640
b34976b6 6641static bfd_boolean
09fd220b 6642sh_elf_set_mach_from_flags (bfd *abfd)
d4845d57 6643{
f6f9408f
JR
6644 flagword flags = elf_elfheader (abfd)->e_flags & EF_SH_MACH_MASK;
6645
6646 if (flags >= sizeof(sh_ef_bfd_table))
6647 return FALSE;
6648
6649 if (sh_ef_bfd_table[flags] == 0)
6650 return FALSE;
6651
6652 bfd_default_set_arch_mach (abfd, bfd_arch_sh, sh_ef_bfd_table[flags]);
d4845d57 6653
b34976b6 6654 return TRUE;
d4845d57 6655}
f6f9408f
JR
6656
6657
6658/* Reverse table lookup for sh_ef_bfd_table[].
6659 Given a bfd MACH value from archures.c
6660 return the equivalent ELF flags from the table.
6661 Return -1 if no match is found. */
6662
6663int
6664sh_elf_get_flags_from_mach (unsigned long mach)
6665{
4195f552 6666 int i = ARRAY_SIZE (sh_ef_bfd_table) - 1;
f6f9408f
JR
6667
6668 for (; i>0; i--)
6669 if (sh_ef_bfd_table[i] == mach)
6670 return i;
6671
6672 /* shouldn't get here */
6673 BFD_FAIL();
6674
6675 return -1;
6676}
fbca6ad9 6677#endif /* not sh_elf_set_mach_from_flags */
d4845d57 6678
fbca6ad9 6679#ifndef sh_elf_set_private_flags
435b1e90
KH
6680/* Function to keep SH specific file flags. */
6681
b34976b6 6682static bfd_boolean
09fd220b 6683sh_elf_set_private_flags (bfd *abfd, flagword flags)
d4845d57
JR
6684{
6685 BFD_ASSERT (! elf_flags_init (abfd)
6686 || elf_elfheader (abfd)->e_flags == flags);
6687
6688 elf_elfheader (abfd)->e_flags = flags;
b34976b6 6689 elf_flags_init (abfd) = TRUE;
d4845d57
JR
6690 return sh_elf_set_mach_from_flags (abfd);
6691}
fbca6ad9 6692#endif /* not sh_elf_set_private_flags */
d4845d57 6693
fbca6ad9 6694#ifndef sh_elf_copy_private_data
d4845d57 6695/* Copy backend specific data from one object module to another */
435b1e90 6696
b34976b6 6697static bfd_boolean
09fd220b 6698sh_elf_copy_private_data (bfd * ibfd, bfd * obfd)
d4845d57 6699{
38b1a46c 6700 if ( bfd_get_flavour (ibfd) != bfd_target_elf_flavour
d4845d57 6701 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
b34976b6 6702 return TRUE;
d4845d57
JR
6703
6704 return sh_elf_set_private_flags (obfd, elf_elfheader (ibfd)->e_flags);
6705}
fbca6ad9 6706#endif /* not sh_elf_copy_private_data */
d4845d57 6707
fbca6ad9 6708#ifndef sh_elf_merge_private_data
d4845d57 6709
f6f9408f
JR
6710/* This function returns the ELF architecture number that
6711 corresponds to the given arch_sh* flags. */
85fbca6a 6712
f6f9408f
JR
6713int
6714sh_find_elf_flags (unsigned int arch_set)
d4845d57 6715{
85fbca6a 6716 extern unsigned long sh_get_bfd_mach_from_arch_set (unsigned int);
f6f9408f 6717 unsigned long bfd_mach = sh_get_bfd_mach_from_arch_set (arch_set);
d4845d57 6718
f6f9408f
JR
6719 return sh_elf_get_flags_from_mach (bfd_mach);
6720}
f6f9408f
JR
6721
6722/* This routine initialises the elf flags when required and
6723 calls sh_merge_bfd_arch() to check dsp/fpu compatibility. */
d4845d57 6724
f6f9408f
JR
6725static bfd_boolean
6726sh_elf_merge_private_data (bfd *ibfd, bfd *obfd)
6727{
85fbca6a
NC
6728 extern bfd_boolean sh_merge_bfd_arch (bfd *, bfd *);
6729
d4845d57
JR
6730 if ( bfd_get_flavour (ibfd) != bfd_target_elf_flavour
6731 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
b34976b6 6732 return TRUE;
d4845d57
JR
6733
6734 if (! elf_flags_init (obfd))
6735 {
a39b79b9 6736 /* This happens when ld starts out with a 'blank' output file. */
b34976b6 6737 elf_flags_init (obfd) = TRUE;
a39b79b9 6738 elf_elfheader (obfd)->e_flags = EF_SH1;
f6f9408f 6739 sh_elf_set_mach_from_flags (obfd);
d4845d57 6740 }
d4845d57 6741
85fbca6a 6742 if (! sh_merge_bfd_arch (ibfd, obfd))
1d70c7fb 6743 {
d003868e 6744 _bfd_error_handler ("%B: uses instructions which are incompatible "
1d70c7fb 6745 "with instructions used in previous modules",
d003868e 6746 ibfd);
1d70c7fb
AO
6747 bfd_set_error (bfd_error_bad_value);
6748 return FALSE;
6749 }
f6f9408f
JR
6750
6751 elf_elfheader (obfd)->e_flags =
6752 sh_elf_get_flags_from_mach (bfd_get_mach (obfd));
6753
6754 return TRUE;
d4845d57 6755}
fbca6ad9 6756#endif /* not sh_elf_merge_private_data */
d4845d57 6757
3376eaf5
KK
6758/* Override the generic function because we need to store sh_elf_obj_tdata
6759 as the specific tdata. We set also the machine architecture from flags
6760 here. */
6761
b34976b6 6762static bfd_boolean
09fd220b 6763sh_elf_object_p (bfd *abfd)
3376eaf5 6764{
66becf32 6765 return sh_elf_set_mach_from_flags (abfd);
3376eaf5
KK
6766}
6767
37c644f2
AO
6768/* Finish up dynamic symbol handling. We set the contents of various
6769 dynamic sections here. */
6770
b34976b6 6771static bfd_boolean
09fd220b
KK
6772sh_elf_finish_dynamic_symbol (bfd *output_bfd, struct bfd_link_info *info,
6773 struct elf_link_hash_entry *h,
6774 Elf_Internal_Sym *sym)
37c644f2 6775{
067653c5 6776 struct elf_sh_link_hash_table *htab;
37c644f2 6777
067653c5 6778 htab = sh_elf_hash_table (info);
37c644f2
AO
6779
6780 if (h->plt.offset != (bfd_vma) -1)
6781 {
6782 asection *splt;
6783 asection *sgot;
6784 asection *srel;
6785
6786 bfd_vma plt_index;
6787 bfd_vma got_offset;
6788 Elf_Internal_Rela rel;
947216bf 6789 bfd_byte *loc;
37c644f2
AO
6790
6791 /* This symbol has an entry in the procedure linkage table. Set
6792 it up. */
6793
6794 BFD_ASSERT (h->dynindx != -1);
6795
067653c5
AM
6796 splt = htab->splt;
6797 sgot = htab->sgotplt;
6798 srel = htab->srelplt;
37c644f2
AO
6799 BFD_ASSERT (splt != NULL && sgot != NULL && srel != NULL);
6800
6801 /* Get the index in the procedure linkage table which
6802 corresponds to this symbol. This is the index of this symbol
6803 in all the symbols for which we are making plt entries. The
6804 first entry in the procedure linkage table is reserved. */
6805 plt_index = h->plt.offset / elf_sh_sizeof_plt (info) - 1;
6806
6807 /* Get the offset into the .got table of the entry that
6808 corresponds to this function. Each .got entry is 4 bytes.
6809 The first three are reserved. */
6810 got_offset = (plt_index + 3) * 4;
6811
fbca6ad9 6812#ifdef GOT_BIAS
c8614e8e
AM
6813 if (info->shared)
6814 got_offset -= GOT_BIAS;
fbca6ad9
AO
6815#endif
6816
37c644f2
AO
6817 /* Fill in the entry in the procedure linkage table. */
6818 if (! info->shared)
6819 {
6820 if (elf_sh_plt_entry == NULL)
6821 {
435b1e90 6822 elf_sh_plt_entry = (bfd_big_endian (output_bfd) ?
37c644f2
AO
6823 elf_sh_plt_entry_be : elf_sh_plt_entry_le);
6824 }
6825 memcpy (splt->contents + h->plt.offset, elf_sh_plt_entry,
6826 elf_sh_sizeof_plt (info));
fbca6ad9
AO
6827#ifdef INCLUDE_SHMEDIA
6828 movi_shori_putval (output_bfd,
6829 (sgot->output_section->vma
6830 + sgot->output_offset
6831 + got_offset),
6832 (splt->contents + h->plt.offset
6833 + elf_sh_plt_symbol_offset (info)));
6834
c8614e8e 6835 /* Set bottom bit because its for a branch to SHmedia */
fbca6ad9 6836 movi_shori_putval (output_bfd,
c8614e8e
AM
6837 (splt->output_section->vma + splt->output_offset)
6838 | 1,
fbca6ad9
AO
6839 (splt->contents + h->plt.offset
6840 + elf_sh_plt_plt0_offset (info)));
6841#else
37c644f2
AO
6842 bfd_put_32 (output_bfd,
6843 (sgot->output_section->vma
6844 + sgot->output_offset
6845 + got_offset),
6846 (splt->contents + h->plt.offset
6847 + elf_sh_plt_symbol_offset (info)));
6848
6849 bfd_put_32 (output_bfd,
6850 (splt->output_section->vma + splt->output_offset),
6851 (splt->contents + h->plt.offset
6852 + elf_sh_plt_plt0_offset (info)));
fbca6ad9 6853#endif
37c644f2
AO
6854 }
6855 else
6856 {
6857 if (elf_sh_pic_plt_entry == NULL)
6858 {
435b1e90 6859 elf_sh_pic_plt_entry = (bfd_big_endian (output_bfd) ?
37c644f2
AO
6860 elf_sh_pic_plt_entry_be :
6861 elf_sh_pic_plt_entry_le);
6862 }
6863 memcpy (splt->contents + h->plt.offset, elf_sh_pic_plt_entry,
6864 elf_sh_sizeof_plt (info));
fbca6ad9
AO
6865#ifdef INCLUDE_SHMEDIA
6866 movi_shori_putval (output_bfd, got_offset,
6867 (splt->contents + h->plt.offset
6868 + elf_sh_plt_symbol_offset (info)));
6869#else
37c644f2
AO
6870 bfd_put_32 (output_bfd, got_offset,
6871 (splt->contents + h->plt.offset
6872 + elf_sh_plt_symbol_offset (info)));
fbca6ad9 6873#endif
37c644f2
AO
6874 }
6875
fbca6ad9 6876#ifdef GOT_BIAS
c8614e8e
AM
6877 if (info->shared)
6878 got_offset += GOT_BIAS;
fbca6ad9
AO
6879#endif
6880
6881#ifdef INCLUDE_SHMEDIA
6882 movi_shori_putval (output_bfd,
6883 plt_index * sizeof (Elf32_External_Rela),
6884 (splt->contents + h->plt.offset
6885 + elf_sh_plt_reloc_offset (info)));
6886#else
37c644f2
AO
6887 bfd_put_32 (output_bfd, plt_index * sizeof (Elf32_External_Rela),
6888 (splt->contents + h->plt.offset
6889 + elf_sh_plt_reloc_offset (info)));
fbca6ad9 6890#endif
37c644f2
AO
6891
6892 /* Fill in the entry in the global offset table. */
6893 bfd_put_32 (output_bfd,
6894 (splt->output_section->vma
6895 + splt->output_offset
6896 + h->plt.offset
6897 + elf_sh_plt_temp_offset (info)),
6898 sgot->contents + got_offset);
6899
6900 /* Fill in the entry in the .rela.plt section. */
6901 rel.r_offset = (sgot->output_section->vma
6902 + sgot->output_offset
6903 + got_offset);
6904 rel.r_info = ELF32_R_INFO (h->dynindx, R_SH_JMP_SLOT);
6905 rel.r_addend = 0;
fbca6ad9
AO
6906#ifdef GOT_BIAS
6907 rel.r_addend = GOT_BIAS;
6908#endif
947216bf
AM
6909 loc = srel->contents + plt_index * sizeof (Elf32_External_Rela);
6910 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
37c644f2 6911
f5385ebf 6912 if (!h->def_regular)
37c644f2
AO
6913 {
6914 /* Mark the symbol as undefined, rather than as defined in
6915 the .plt section. Leave the value alone. */
6916 sym->st_shndx = SHN_UNDEF;
6917 }
6918 }
6919
3376eaf5
KK
6920 if (h->got.offset != (bfd_vma) -1
6921 && sh_elf_hash_entry (h)->tls_type != GOT_TLS_GD
6922 && sh_elf_hash_entry (h)->tls_type != GOT_TLS_IE)
37c644f2
AO
6923 {
6924 asection *sgot;
6925 asection *srel;
6926 Elf_Internal_Rela rel;
947216bf 6927 bfd_byte *loc;
37c644f2
AO
6928
6929 /* This symbol has an entry in the global offset table. Set it
6930 up. */
6931
067653c5
AM
6932 sgot = htab->sgot;
6933 srel = htab->srelgot;
37c644f2
AO
6934 BFD_ASSERT (sgot != NULL && srel != NULL);
6935
6936 rel.r_offset = (sgot->output_section->vma
6937 + sgot->output_offset
dc810e39 6938 + (h->got.offset &~ (bfd_vma) 1));
37c644f2 6939
067653c5
AM
6940 /* If this is a static link, or it is a -Bsymbolic link and the
6941 symbol is defined locally or was forced to be local because
6942 of a version file, we just want to emit a RELATIVE reloc.
37c644f2
AO
6943 The entry in the global offset table will already have been
6944 initialized in the relocate_section function. */
6945 if (info->shared
3d85aebe 6946 && SYMBOL_REFERENCES_LOCAL (info, h))
37c644f2
AO
6947 {
6948 rel.r_info = ELF32_R_INFO (0, R_SH_RELATIVE);
6949 rel.r_addend = (h->root.u.def.value
6950 + h->root.u.def.section->output_section->vma
6951 + h->root.u.def.section->output_offset);
6952 }
6953 else
6954 {
6955 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + h->got.offset);
6956 rel.r_info = ELF32_R_INFO (h->dynindx, R_SH_GLOB_DAT);
6957 rel.r_addend = 0;
6958 }
6959
947216bf
AM
6960 loc = srel->contents;
6961 loc += srel->reloc_count++ * sizeof (Elf32_External_Rela);
6962 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
37c644f2
AO
6963 }
6964
396a6083
SC
6965#ifdef INCLUDE_SHMEDIA
6966 {
6967 struct elf_sh_link_hash_entry *eh;
6968
6969 eh = (struct elf_sh_link_hash_entry *) h;
6970 if (eh->datalabel_got.offset != (bfd_vma) -1)
6971 {
6972 asection *sgot;
6973 asection *srel;
6974 Elf_Internal_Rela rel;
947216bf 6975 bfd_byte *loc;
396a6083
SC
6976
6977 /* This symbol has a datalabel entry in the global offset table.
6978 Set it up. */
6979
6980 sgot = htab->sgot;
6981 srel = htab->srelgot;
6982 BFD_ASSERT (sgot != NULL && srel != NULL);
6983
6984 rel.r_offset = (sgot->output_section->vma
6985 + sgot->output_offset
6986 + (eh->datalabel_got.offset &~ (bfd_vma) 1));
6987
6988 /* If this is a static link, or it is a -Bsymbolic link and the
6989 symbol is defined locally or was forced to be local because
6990 of a version file, we just want to emit a RELATIVE reloc.
6991 The entry in the global offset table will already have been
6992 initialized in the relocate_section function. */
6993 if (info->shared
3d85aebe 6994 && SYMBOL_REFERENCES_LOCAL (info, h))
396a6083
SC
6995 {
6996 rel.r_info = ELF32_R_INFO (0, R_SH_RELATIVE);
6997 rel.r_addend = (h->root.u.def.value
6998 + h->root.u.def.section->output_section->vma
6999 + h->root.u.def.section->output_offset);
7000 }
7001 else
7002 {
7003 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents
7004 + eh->datalabel_got.offset);
7005 rel.r_info = ELF32_R_INFO (h->dynindx, R_SH_GLOB_DAT);
7006 rel.r_addend = 0;
7007 }
7008
947216bf
AM
7009 loc = srel->contents;
7010 loc += srel->reloc_count++ * sizeof (Elf32_External_Rela);
7011 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
396a6083
SC
7012 }
7013 }
7014#endif
7015
f5385ebf 7016 if (h->needs_copy)
37c644f2
AO
7017 {
7018 asection *s;
7019 Elf_Internal_Rela rel;
947216bf 7020 bfd_byte *loc;
37c644f2
AO
7021
7022 /* This symbol needs a copy reloc. Set it up. */
7023
7024 BFD_ASSERT (h->dynindx != -1
7025 && (h->root.type == bfd_link_hash_defined
7026 || h->root.type == bfd_link_hash_defweak));
7027
7028 s = bfd_get_section_by_name (h->root.u.def.section->owner,
7029 ".rela.bss");
7030 BFD_ASSERT (s != NULL);
7031
7032 rel.r_offset = (h->root.u.def.value
7033 + h->root.u.def.section->output_section->vma
7034 + h->root.u.def.section->output_offset);
7035 rel.r_info = ELF32_R_INFO (h->dynindx, R_SH_COPY);
7036 rel.r_addend = 0;
947216bf
AM
7037 loc = s->contents + s->reloc_count++ * sizeof (Elf32_External_Rela);
7038 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
37c644f2
AO
7039 }
7040
7041 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. */
7042 if (strcmp (h->root.root.string, "_DYNAMIC") == 0
22edb2f1 7043 || h == htab->root.hgot)
37c644f2
AO
7044 sym->st_shndx = SHN_ABS;
7045
b34976b6 7046 return TRUE;
37c644f2
AO
7047}
7048
7049/* Finish up the dynamic sections. */
7050
b34976b6 7051static bfd_boolean
09fd220b 7052sh_elf_finish_dynamic_sections (bfd *output_bfd, struct bfd_link_info *info)
37c644f2 7053{
067653c5 7054 struct elf_sh_link_hash_table *htab;
37c644f2
AO
7055 asection *sgot;
7056 asection *sdyn;
7057
067653c5 7058 htab = sh_elf_hash_table (info);
067653c5 7059 sgot = htab->sgotplt;
2293c499 7060 sdyn = bfd_get_section_by_name (htab->root.dynobj, ".dynamic");
37c644f2 7061
067653c5 7062 if (htab->root.dynamic_sections_created)
37c644f2
AO
7063 {
7064 asection *splt;
7065 Elf32_External_Dyn *dyncon, *dynconend;
7066
067653c5 7067 BFD_ASSERT (sgot != NULL && sdyn != NULL);
37c644f2
AO
7068
7069 dyncon = (Elf32_External_Dyn *) sdyn->contents;
eea6121a 7070 dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
37c644f2
AO
7071 for (; dyncon < dynconend; dyncon++)
7072 {
7073 Elf_Internal_Dyn dyn;
37c644f2 7074 asection *s;
067653c5
AM
7075#ifdef INCLUDE_SHMEDIA
7076 const char *name;
7077#endif
37c644f2 7078
2293c499 7079 bfd_elf32_swap_dyn_in (htab->root.dynobj, dyncon, &dyn);
37c644f2
AO
7080
7081 switch (dyn.d_tag)
7082 {
7083 default:
7084 break;
7085
3b587c71
AM
7086#ifdef INCLUDE_SHMEDIA
7087 case DT_INIT:
7088 name = info->init_function;
7089 goto get_sym;
7090
7091 case DT_FINI:
7092 name = info->fini_function;
7093 get_sym:
7094 if (dyn.d_un.d_val != 0)
7095 {
067653c5
AM
7096 struct elf_link_hash_entry *h;
7097
7098 h = elf_link_hash_lookup (&htab->root, name,
b34976b6 7099 FALSE, FALSE, TRUE);
3b587c71
AM
7100 if (h != NULL && (h->other & STO_SH5_ISA32))
7101 {
7102 dyn.d_un.d_val |= 1;
7103 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
7104 }
7105 }
7106 break;
7107#endif
7108
37c644f2 7109 case DT_PLTGOT:
067653c5 7110 s = htab->sgot->output_section;
37c644f2
AO
7111 goto get_vma;
7112
7113 case DT_JMPREL:
067653c5 7114 s = htab->srelplt->output_section;
37c644f2 7115 get_vma:
37c644f2
AO
7116 BFD_ASSERT (s != NULL);
7117 dyn.d_un.d_ptr = s->vma;
7118 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
7119 break;
7120
7121 case DT_PLTRELSZ:
067653c5 7122 s = htab->srelplt->output_section;
37c644f2 7123 BFD_ASSERT (s != NULL);
eea6121a 7124 dyn.d_un.d_val = s->size;
37c644f2
AO
7125 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
7126 break;
7127
7128 case DT_RELASZ:
7129 /* My reading of the SVR4 ABI indicates that the
7130 procedure linkage table relocs (DT_JMPREL) should be
7131 included in the overall relocs (DT_RELA). This is
7132 what Solaris does. However, UnixWare can not handle
7133 that case. Therefore, we override the DT_RELASZ entry
7134 here to make it not include the JMPREL relocs. Since
7135 the linker script arranges for .rela.plt to follow all
7136 other relocation sections, we don't have to worry
7137 about changing the DT_RELA entry. */
067653c5 7138 if (htab->srelplt != NULL)
37c644f2 7139 {
067653c5 7140 s = htab->srelplt->output_section;
eea6121a 7141 dyn.d_un.d_val -= s->size;
37c644f2
AO
7142 }
7143 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
7144 break;
7145 }
7146 }
7147
7148 /* Fill in the first entry in the procedure linkage table. */
067653c5 7149 splt = htab->splt;
eea6121a 7150 if (splt && splt->size > 0)
37c644f2
AO
7151 {
7152 if (info->shared)
7153 {
7154 if (elf_sh_pic_plt_entry == NULL)
7155 {
435b1e90 7156 elf_sh_pic_plt_entry = (bfd_big_endian (output_bfd) ?
37c644f2
AO
7157 elf_sh_pic_plt_entry_be :
7158 elf_sh_pic_plt_entry_le);
7159 }
7160 memcpy (splt->contents, elf_sh_pic_plt_entry,
7161 elf_sh_sizeof_plt (info));
7162 }
7163 else
7164 {
7165 if (elf_sh_plt0_entry == NULL)
7166 {
435b1e90 7167 elf_sh_plt0_entry = (bfd_big_endian (output_bfd) ?
37c644f2
AO
7168 elf_sh_plt0_entry_be :
7169 elf_sh_plt0_entry_le);
7170 }
7171 memcpy (splt->contents, elf_sh_plt0_entry, PLT_ENTRY_SIZE);
fbca6ad9
AO
7172#ifdef INCLUDE_SHMEDIA
7173 movi_shori_putval (output_bfd,
7174 sgot->output_section->vma
7175 + sgot->output_offset,
7176 splt->contents
7177 + elf_sh_plt0_gotplt_offset (info));
7178#else
37c644f2
AO
7179 bfd_put_32 (output_bfd,
7180 sgot->output_section->vma + sgot->output_offset + 4,
7181 splt->contents + elf_sh_plt0_gotid_offset (info));
7182 bfd_put_32 (output_bfd,
7183 sgot->output_section->vma + sgot->output_offset + 8,
7184 splt->contents + elf_sh_plt0_linker_offset (info));
fbca6ad9 7185#endif
37c644f2
AO
7186 }
7187
7188 /* UnixWare sets the entsize of .plt to 4, although that doesn't
7189 really seem like the right value. */
7190 elf_section_data (splt->output_section)->this_hdr.sh_entsize = 4;
7191 }
7192 }
7193
7194 /* Fill in the first three entries in the global offset table. */
eea6121a 7195 if (sgot && sgot->size > 0)
37c644f2
AO
7196 {
7197 if (sdyn == NULL)
7198 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents);
7199 else
7200 bfd_put_32 (output_bfd,
7201 sdyn->output_section->vma + sdyn->output_offset,
7202 sgot->contents);
7203 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 4);
7204 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 8);
37c644f2 7205
067653c5
AM
7206 elf_section_data (sgot->output_section)->this_hdr.sh_entsize = 4;
7207 }
37c644f2 7208
b34976b6 7209 return TRUE;
37c644f2
AO
7210}
7211
99e4ae17 7212static enum elf_reloc_type_class
09fd220b 7213sh_elf_reloc_type_class (const Elf_Internal_Rela *rela)
99e4ae17 7214{
f51e552e 7215 switch ((int) ELF32_R_TYPE (rela->r_info))
99e4ae17
AJ
7216 {
7217 case R_SH_RELATIVE:
7218 return reloc_class_relative;
7219 case R_SH_JMP_SLOT:
7220 return reloc_class_plt;
7221 case R_SH_COPY:
7222 return reloc_class_copy;
7223 default:
7224 return reloc_class_normal;
7225 }
7226}
7227
85fbca6a 7228#if !defined SH_TARGET_ALREADY_DEFINED
571fe01f 7229/* Support for Linux core dump NOTE sections. */
85fbca6a 7230
b34976b6 7231static bfd_boolean
09fd220b 7232elf32_shlin_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
2bc3c89a
AM
7233{
7234 int offset;
eea6121a 7235 unsigned int size;
2bc3c89a
AM
7236
7237 switch (note->descsz)
7238 {
7239 default:
b34976b6 7240 return FALSE;
2bc3c89a
AM
7241
7242 case 168: /* Linux/SH */
7243 /* pr_cursig */
7244 elf_tdata (abfd)->core_signal = bfd_get_16 (abfd, note->descdata + 12);
7245
7246 /* pr_pid */
7247 elf_tdata (abfd)->core_pid = bfd_get_32 (abfd, note->descdata + 24);
7248
7249 /* pr_reg */
7250 offset = 72;
eea6121a 7251 size = 92;
2bc3c89a
AM
7252
7253 break;
7254 }
7255
7256 /* Make a ".reg/999" section. */
7257 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
eea6121a 7258 size, note->descpos + offset);
2bc3c89a
AM
7259}
7260
b34976b6 7261static bfd_boolean
09fd220b 7262elf32_shlin_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
2bc3c89a
AM
7263{
7264 switch (note->descsz)
7265 {
7266 default:
b34976b6 7267 return FALSE;
2bc3c89a
AM
7268
7269 case 124: /* Linux/SH elf_prpsinfo */
7270 elf_tdata (abfd)->core_program
7271 = _bfd_elfcore_strndup (abfd, note->descdata + 28, 16);
7272 elf_tdata (abfd)->core_command
7273 = _bfd_elfcore_strndup (abfd, note->descdata + 44, 80);
7274 }
7275
7276 /* Note that for some reason, a spurious space is tacked
7277 onto the end of the args in some (at least one anyway)
7278 implementations, so strip it off if it exists. */
7279
7280 {
7281 char *command = elf_tdata (abfd)->core_command;
7282 int n = strlen (command);
7283
7284 if (0 < n && command[n - 1] == ' ')
7285 command[n - 1] = '\0';
7286 }
7287
b34976b6 7288 return TRUE;
2bc3c89a 7289}
85fbca6a 7290#endif /* not SH_TARGET_ALREADY_DEFINED */
2bc3c89a 7291
1f1799d5
KK
7292
7293/* Return address for Ith PLT stub in section PLT, for relocation REL
7294 or (bfd_vma) -1 if it should not be included. */
7295
7296static bfd_vma
7297sh_elf_plt_sym_val (bfd_vma i, const asection *plt,
7298 const arelent *rel ATTRIBUTE_UNUSED)
7299{
7300 return plt->vma + (i + 1) * PLT_ENTRY_SIZE;
7301}
7302
85fbca6a 7303#if !defined SH_TARGET_ALREADY_DEFINED
252b5132
RH
7304#define TARGET_BIG_SYM bfd_elf32_sh_vec
7305#define TARGET_BIG_NAME "elf32-sh"
7306#define TARGET_LITTLE_SYM bfd_elf32_shl_vec
7307#define TARGET_LITTLE_NAME "elf32-shl"
85fbca6a
NC
7308#endif
7309
252b5132
RH
7310#define ELF_ARCH bfd_arch_sh
7311#define ELF_MACHINE_CODE EM_SH
d0facd1b
NC
7312#ifdef __QNXTARGET__
7313#define ELF_MAXPAGESIZE 0x1000
7314#else
7315#define ELF_MAXPAGESIZE 0x80
7316#endif
252b5132
RH
7317
7318#define elf_symbol_leading_char '_'
7319
7320#define bfd_elf32_bfd_reloc_type_lookup sh_elf_reloc_type_lookup
7321#define elf_info_to_howto sh_elf_info_to_howto
7322#define bfd_elf32_bfd_relax_section sh_elf_relax_section
7323#define elf_backend_relocate_section sh_elf_relocate_section
7324#define bfd_elf32_bfd_get_relocated_section_contents \
7325 sh_elf_get_relocated_section_contents
3376eaf5
KK
7326#define bfd_elf32_mkobject sh_elf_mkobject
7327#define elf_backend_object_p sh_elf_object_p
d4845d57
JR
7328#define bfd_elf32_bfd_set_private_bfd_flags \
7329 sh_elf_set_private_flags
7330#define bfd_elf32_bfd_copy_private_bfd_data \
7331 sh_elf_copy_private_data
875f7f69 7332#define bfd_elf32_bfd_merge_private_bfd_data \
d4845d57 7333 sh_elf_merge_private_data
252b5132 7334
067653c5
AM
7335#define elf_backend_gc_mark_hook sh_elf_gc_mark_hook
7336#define elf_backend_gc_sweep_hook sh_elf_gc_sweep_hook
7337#define elf_backend_check_relocs sh_elf_check_relocs
7338#define elf_backend_copy_indirect_symbol \
7339 sh_elf_copy_indirect_symbol
37c644f2
AO
7340#define elf_backend_create_dynamic_sections \
7341 sh_elf_create_dynamic_sections
7342#define bfd_elf32_bfd_link_hash_table_create \
7343 sh_elf_link_hash_table_create
7344#define elf_backend_adjust_dynamic_symbol \
7345 sh_elf_adjust_dynamic_symbol
7346#define elf_backend_size_dynamic_sections \
7347 sh_elf_size_dynamic_sections
7348#define elf_backend_finish_dynamic_symbol \
7349 sh_elf_finish_dynamic_symbol
7350#define elf_backend_finish_dynamic_sections \
7351 sh_elf_finish_dynamic_sections
99e4ae17 7352#define elf_backend_reloc_type_class sh_elf_reloc_type_class
1f1799d5 7353#define elf_backend_plt_sym_val sh_elf_plt_sym_val
37c644f2 7354
067653c5
AM
7355#define elf_backend_can_gc_sections 1
7356#define elf_backend_can_refcount 1
37c644f2
AO
7357#define elf_backend_want_got_plt 1
7358#define elf_backend_plt_readonly 1
7359#define elf_backend_want_plt_sym 0
7360#define elf_backend_got_header_size 12
ed71e111 7361
85fbca6a 7362#if !defined INCLUDE_SHMEDIA && !defined SH_TARGET_ALREADY_DEFINED
2bc3c89a 7363
252b5132 7364#include "elf32-target.h"
ed71e111 7365
2bc3c89a
AM
7366/* NetBSD support. */
7367#undef TARGET_BIG_SYM
7368#define TARGET_BIG_SYM bfd_elf32_shnbsd_vec
7369#undef TARGET_BIG_NAME
7370#define TARGET_BIG_NAME "elf32-sh-nbsd"
7371#undef TARGET_LITTLE_SYM
7372#define TARGET_LITTLE_SYM bfd_elf32_shlnbsd_vec
7373#undef TARGET_LITTLE_NAME
7374#define TARGET_LITTLE_NAME "elf32-shl-nbsd"
7375#undef ELF_MAXPAGESIZE
7376#define ELF_MAXPAGESIZE 0x10000
24718e3b 7377#undef ELF_COMMONPAGESIZE
2bc3c89a
AM
7378#undef elf_symbol_leading_char
7379#define elf_symbol_leading_char 0
571fe01f 7380#undef elf32_bed
2bc3c89a
AM
7381#define elf32_bed elf32_sh_nbsd_bed
7382
7383#include "elf32-target.h"
7384
2bc3c89a
AM
7385
7386/* Linux support. */
7387#undef TARGET_BIG_SYM
7388#define TARGET_BIG_SYM bfd_elf32_shblin_vec
7389#undef TARGET_BIG_NAME
7390#define TARGET_BIG_NAME "elf32-shbig-linux"
7391#undef TARGET_LITTLE_SYM
7392#define TARGET_LITTLE_SYM bfd_elf32_shlin_vec
7393#undef TARGET_LITTLE_NAME
7394#define TARGET_LITTLE_NAME "elf32-sh-linux"
24718e3b
L
7395#undef ELF_COMMONPAGESIZE
7396#define ELF_COMMONPAGESIZE 0x1000
2bc3c89a
AM
7397
7398#undef elf_backend_grok_prstatus
7399#define elf_backend_grok_prstatus elf32_shlin_grok_prstatus
7400#undef elf_backend_grok_psinfo
7401#define elf_backend_grok_psinfo elf32_shlin_grok_psinfo
571fe01f 7402#undef elf32_bed
2bc3c89a
AM
7403#define elf32_bed elf32_sh_lin_bed
7404
7405#include "elf32-target.h"
7406
85fbca6a 7407#endif /* neither INCLUDE_SHMEDIA nor SH_TARGET_ALREADY_DEFINED */
This page took 1.015782 seconds and 4 git commands to generate.