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