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