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