]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - bfd/elf32-sh.c
* write.c (TC_FIX_ADJUSTABLE): Define to 1, if not defined.
[thirdparty/binutils-gdb.git] / bfd / elf32-sh.c
CommitLineData
252b5132 1/* Hitachi SH specific support for 32-bit ELF
d4845d57 2 Copyright 1996, 97, 98, 1999, 2000 Free Software Foundation, Inc.
252b5132
RH
3 Contributed by Ian Lance Taylor, Cygnus Support.
4
5This file is part of BFD, the Binary File Descriptor library.
6
7This program is free software; you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
9the Free Software Foundation; either version 2 of the License, or
10(at your option) any later version.
11
12This program is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along with this program; if not, write to the Free Software
19Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
20
21#include "bfd.h"
22#include "sysdep.h"
23#include "bfdlink.h"
24#include "libbfd.h"
25#include "elf-bfd.h"
26#include "elf/sh.h"
27
28static bfd_reloc_status_type sh_elf_reloc
29 PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
30static bfd_reloc_status_type sh_elf_ignore_reloc
31 PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
32static reloc_howto_type *sh_elf_reloc_type_lookup
33 PARAMS ((bfd *, bfd_reloc_code_real_type));
34static void sh_elf_info_to_howto
35 PARAMS ((bfd *, arelent *, Elf_Internal_Rela *));
d4845d57
JR
36static boolean sh_elf_set_private_flags
37 PARAMS ((bfd *, flagword));
38static boolean sh_elf_copy_private_data
39 PARAMS ((bfd *, bfd *));
40static boolean sh_elf_merge_private_data
41 PARAMS ((bfd *, bfd *));
1630fd2b 42static boolean sh_elf_set_mach_from_flags
d4845d57 43 PARAMS ((bfd *));
252b5132
RH
44static boolean sh_elf_relax_section
45 PARAMS ((bfd *, asection *, struct bfd_link_info *, boolean *));
46static boolean sh_elf_relax_delete_bytes
47 PARAMS ((bfd *, asection *, bfd_vma, int));
48static boolean sh_elf_align_loads
49 PARAMS ((bfd *, asection *, Elf_Internal_Rela *, bfd_byte *, boolean *));
50static boolean sh_elf_swap_insns
51 PARAMS ((bfd *, asection *, PTR, bfd_byte *, bfd_vma));
52static boolean sh_elf_relocate_section
53 PARAMS ((bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
54 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **));
55static bfd_byte *sh_elf_get_relocated_section_contents
56 PARAMS ((bfd *, struct bfd_link_info *, struct bfd_link_order *,
57 bfd_byte *, boolean, asymbol **));
58
59static reloc_howto_type sh_elf_howto_table[] =
60{
61 /* No relocation. */
62 HOWTO (R_SH_NONE, /* type */
63 0, /* rightshift */
64 0, /* size (0 = byte, 1 = short, 2 = long) */
65 0, /* bitsize */
66 false, /* pc_relative */
67 0, /* bitpos */
68 complain_overflow_dont, /* complain_on_overflow */
015551fc 69 sh_elf_ignore_reloc, /* special_function */
252b5132
RH
70 "R_SH_NONE", /* name */
71 false, /* partial_inplace */
72 0, /* src_mask */
73 0, /* dst_mask */
74 false), /* pcrel_offset */
75
76 /* 32 bit absolute relocation. Setting partial_inplace to true and
77 src_mask to a non-zero value is similar to the COFF toolchain. */
78 HOWTO (R_SH_DIR32, /* type */
79 0, /* rightshift */
80 2, /* size (0 = byte, 1 = short, 2 = long) */
81 32, /* bitsize */
82 false, /* pc_relative */
83 0, /* bitpos */
84 complain_overflow_bitfield, /* complain_on_overflow */
85 sh_elf_reloc, /* special_function */
86 "R_SH_DIR32", /* name */
87 true, /* partial_inplace */
88 0xffffffff, /* src_mask */
89 0xffffffff, /* dst_mask */
90 false), /* pcrel_offset */
91
92 /* 32 bit PC relative relocation. */
93 HOWTO (R_SH_REL32, /* type */
94 0, /* rightshift */
95 2, /* size (0 = byte, 1 = short, 2 = long) */
96 32, /* bitsize */
97 true, /* pc_relative */
98 0, /* bitpos */
99 complain_overflow_signed, /* complain_on_overflow */
015551fc 100 sh_elf_ignore_reloc, /* special_function */
252b5132
RH
101 "R_SH_REL32", /* name */
102 false, /* partial_inplace */
103 0, /* src_mask */
104 0xffffffff, /* dst_mask */
105 true), /* pcrel_offset */
106
107 /* 8 bit PC relative branch divided by 2. */
108 HOWTO (R_SH_DIR8WPN, /* type */
109 1, /* rightshift */
110 1, /* size (0 = byte, 1 = short, 2 = long) */
111 8, /* bitsize */
112 true, /* pc_relative */
113 0, /* bitpos */
114 complain_overflow_signed, /* complain_on_overflow */
015551fc 115 sh_elf_ignore_reloc, /* special_function */
252b5132
RH
116 "R_SH_DIR8WPN", /* name */
117 true, /* partial_inplace */
118 0xff, /* src_mask */
119 0xff, /* dst_mask */
120 true), /* pcrel_offset */
121
122 /* 12 bit PC relative branch divided by 2. */
123 HOWTO (R_SH_IND12W, /* type */
124 1, /* rightshift */
125 1, /* size (0 = byte, 1 = short, 2 = long) */
126 12, /* bitsize */
127 true, /* pc_relative */
128 0, /* bitpos */
129 complain_overflow_signed, /* complain_on_overflow */
130 sh_elf_reloc, /* special_function */
131 "R_SH_IND12W", /* name */
132 true, /* partial_inplace */
133 0xfff, /* src_mask */
134 0xfff, /* dst_mask */
135 true), /* pcrel_offset */
136
137 /* 8 bit unsigned PC relative divided by 4. */
138 HOWTO (R_SH_DIR8WPL, /* type */
139 2, /* rightshift */
140 1, /* size (0 = byte, 1 = short, 2 = long) */
141 8, /* bitsize */
142 true, /* pc_relative */
143 0, /* bitpos */
144 complain_overflow_unsigned, /* complain_on_overflow */
015551fc 145 sh_elf_ignore_reloc, /* special_function */
252b5132
RH
146 "R_SH_DIR8WPL", /* name */
147 true, /* partial_inplace */
148 0xff, /* src_mask */
149 0xff, /* dst_mask */
150 true), /* pcrel_offset */
151
152 /* 8 bit unsigned PC relative divided by 2. */
153 HOWTO (R_SH_DIR8WPZ, /* type */
154 1, /* rightshift */
155 1, /* size (0 = byte, 1 = short, 2 = long) */
156 8, /* bitsize */
157 true, /* pc_relative */
158 0, /* bitpos */
159 complain_overflow_unsigned, /* complain_on_overflow */
015551fc 160 sh_elf_ignore_reloc, /* special_function */
252b5132
RH
161 "R_SH_DIR8WPZ", /* name */
162 true, /* partial_inplace */
163 0xff, /* src_mask */
164 0xff, /* dst_mask */
165 true), /* pcrel_offset */
166
167 /* 8 bit GBR relative. FIXME: This only makes sense if we have some
168 special symbol for the GBR relative area, and that is not
169 implemented. */
170 HOWTO (R_SH_DIR8BP, /* type */
171 0, /* rightshift */
172 1, /* size (0 = byte, 1 = short, 2 = long) */
173 8, /* bitsize */
174 false, /* pc_relative */
175 0, /* bitpos */
176 complain_overflow_unsigned, /* complain_on_overflow */
015551fc 177 sh_elf_ignore_reloc, /* special_function */
252b5132
RH
178 "R_SH_DIR8BP", /* name */
179 false, /* partial_inplace */
180 0, /* src_mask */
181 0xff, /* dst_mask */
182 true), /* pcrel_offset */
183
184 /* 8 bit GBR relative divided by 2. FIXME: This only makes sense if
185 we have some special symbol for the GBR relative area, and that
186 is not implemented. */
187 HOWTO (R_SH_DIR8W, /* type */
188 1, /* rightshift */
189 1, /* size (0 = byte, 1 = short, 2 = long) */
190 8, /* bitsize */
191 false, /* pc_relative */
192 0, /* bitpos */
193 complain_overflow_unsigned, /* complain_on_overflow */
015551fc 194 sh_elf_ignore_reloc, /* special_function */
252b5132
RH
195 "R_SH_DIR8W", /* name */
196 false, /* partial_inplace */
197 0, /* src_mask */
198 0xff, /* dst_mask */
199 true), /* pcrel_offset */
200
201 /* 8 bit GBR relative divided by 4. FIXME: This only makes sense if
202 we have some special symbol for the GBR relative area, and that
203 is not implemented. */
204 HOWTO (R_SH_DIR8L, /* type */
205 2, /* rightshift */
206 1, /* size (0 = byte, 1 = short, 2 = long) */
207 8, /* bitsize */
208 false, /* pc_relative */
209 0, /* bitpos */
210 complain_overflow_unsigned, /* complain_on_overflow */
015551fc 211 sh_elf_ignore_reloc, /* special_function */
252b5132
RH
212 "R_SH_DIR8L", /* name */
213 false, /* partial_inplace */
214 0, /* src_mask */
215 0xff, /* dst_mask */
216 true), /* pcrel_offset */
217
5f771d47
ILT
218 EMPTY_HOWTO (10),
219 EMPTY_HOWTO (11),
220 EMPTY_HOWTO (12),
221 EMPTY_HOWTO (13),
222 EMPTY_HOWTO (14),
223 EMPTY_HOWTO (15),
224 EMPTY_HOWTO (16),
225 EMPTY_HOWTO (17),
226 EMPTY_HOWTO (18),
227 EMPTY_HOWTO (19),
228 EMPTY_HOWTO (20),
229 EMPTY_HOWTO (21),
230 EMPTY_HOWTO (22),
231 EMPTY_HOWTO (23),
232 EMPTY_HOWTO (24),
252b5132
RH
233
234 /* The remaining relocs are a GNU extension used for relaxing. The
235 final pass of the linker never needs to do anything with any of
236 these relocs. Any required operations are handled by the
237 relaxation code. */
238
239 /* A 16 bit switch table entry. This is generated for an expression
240 such as ``.word L1 - L2''. The offset holds the difference
241 between the reloc address and L2. */
242 HOWTO (R_SH_SWITCH16, /* type */
243 0, /* rightshift */
244 1, /* size (0 = byte, 1 = short, 2 = long) */
245 16, /* bitsize */
246 false, /* pc_relative */
247 0, /* bitpos */
248 complain_overflow_unsigned, /* complain_on_overflow */
249 sh_elf_ignore_reloc, /* special_function */
250 "R_SH_SWITCH16", /* name */
251 false, /* partial_inplace */
252 0, /* src_mask */
253 0, /* dst_mask */
254 true), /* pcrel_offset */
255
256 /* A 32 bit switch table entry. This is generated for an expression
257 such as ``.long L1 - L2''. The offset holds the difference
258 between the reloc address and L2. */
259 HOWTO (R_SH_SWITCH32, /* type */
260 0, /* rightshift */
261 2, /* size (0 = byte, 1 = short, 2 = long) */
262 32, /* bitsize */
263 false, /* pc_relative */
264 0, /* bitpos */
265 complain_overflow_unsigned, /* complain_on_overflow */
266 sh_elf_ignore_reloc, /* special_function */
267 "R_SH_SWITCH32", /* name */
268 false, /* partial_inplace */
269 0, /* src_mask */
270 0, /* dst_mask */
271 true), /* pcrel_offset */
272
273 /* Indicates a .uses pseudo-op. The compiler will generate .uses
274 pseudo-ops when it finds a function call which can be relaxed.
275 The offset field holds the PC relative offset to the instruction
276 which loads the register used in the function call. */
277 HOWTO (R_SH_USES, /* type */
278 0, /* rightshift */
279 1, /* size (0 = byte, 1 = short, 2 = long) */
280 0, /* bitsize */
281 false, /* pc_relative */
282 0, /* bitpos */
283 complain_overflow_unsigned, /* complain_on_overflow */
284 sh_elf_ignore_reloc, /* special_function */
285 "R_SH_USES", /* name */
286 false, /* partial_inplace */
287 0, /* src_mask */
288 0, /* dst_mask */
289 true), /* pcrel_offset */
290
291 /* The assembler will generate this reloc for addresses referred to
292 by the register loads associated with USES relocs. The offset
293 field holds the number of times the address is referenced in the
294 object file. */
295 HOWTO (R_SH_COUNT, /* type */
296 0, /* rightshift */
297 1, /* size (0 = byte, 1 = short, 2 = long) */
298 0, /* bitsize */
299 false, /* pc_relative */
300 0, /* bitpos */
301 complain_overflow_unsigned, /* complain_on_overflow */
302 sh_elf_ignore_reloc, /* special_function */
303 "R_SH_COUNT", /* name */
304 false, /* partial_inplace */
305 0, /* src_mask */
306 0, /* dst_mask */
307 true), /* pcrel_offset */
308
309 /* Indicates an alignment statement. The offset field is the power
310 of 2 to which subsequent portions of the object file must be
311 aligned. */
312 HOWTO (R_SH_ALIGN, /* type */
313 0, /* rightshift */
314 1, /* size (0 = byte, 1 = short, 2 = long) */
315 0, /* bitsize */
316 false, /* pc_relative */
317 0, /* bitpos */
318 complain_overflow_unsigned, /* complain_on_overflow */
319 sh_elf_ignore_reloc, /* special_function */
320 "R_SH_ALIGN", /* name */
321 false, /* partial_inplace */
322 0, /* src_mask */
323 0, /* dst_mask */
324 true), /* pcrel_offset */
325
326 /* The assembler will generate this reloc before a block of
327 instructions. A section should be processed as assumining it
328 contains data, unless this reloc is seen. */
329 HOWTO (R_SH_CODE, /* type */
330 0, /* rightshift */
331 1, /* size (0 = byte, 1 = short, 2 = long) */
332 0, /* bitsize */
333 false, /* pc_relative */
334 0, /* bitpos */
335 complain_overflow_unsigned, /* complain_on_overflow */
336 sh_elf_ignore_reloc, /* special_function */
337 "R_SH_CODE", /* name */
338 false, /* partial_inplace */
339 0, /* src_mask */
340 0, /* dst_mask */
341 true), /* pcrel_offset */
342
343 /* The assembler will generate this reloc after a block of
344 instructions when it sees data that is not instructions. */
345 HOWTO (R_SH_DATA, /* type */
346 0, /* rightshift */
347 1, /* size (0 = byte, 1 = short, 2 = long) */
348 0, /* bitsize */
349 false, /* pc_relative */
350 0, /* bitpos */
351 complain_overflow_unsigned, /* complain_on_overflow */
352 sh_elf_ignore_reloc, /* special_function */
353 "R_SH_DATA", /* name */
354 false, /* partial_inplace */
355 0, /* src_mask */
356 0, /* dst_mask */
357 true), /* pcrel_offset */
358
359 /* The assembler generates this reloc for each label within a block
360 of instructions. This permits the linker to avoid swapping
361 instructions which are the targets of branches. */
362 HOWTO (R_SH_LABEL, /* type */
363 0, /* rightshift */
364 1, /* size (0 = byte, 1 = short, 2 = long) */
365 0, /* bitsize */
366 false, /* pc_relative */
367 0, /* bitpos */
368 complain_overflow_unsigned, /* complain_on_overflow */
369 sh_elf_ignore_reloc, /* special_function */
370 "R_SH_LABEL", /* name */
371 false, /* partial_inplace */
372 0, /* src_mask */
373 0, /* dst_mask */
374 true), /* pcrel_offset */
375
376 /* An 8 bit switch table entry. This is generated for an expression
377 such as ``.word L1 - L2''. The offset holds the difference
378 between the reloc address and L2. */
379 HOWTO (R_SH_SWITCH8, /* type */
380 0, /* rightshift */
381 0, /* size (0 = byte, 1 = short, 2 = long) */
382 8, /* bitsize */
383 false, /* pc_relative */
384 0, /* bitpos */
385 complain_overflow_unsigned, /* complain_on_overflow */
386 sh_elf_ignore_reloc, /* special_function */
387 "R_SH_SWITCH8", /* name */
388 false, /* partial_inplace */
389 0, /* src_mask */
390 0, /* dst_mask */
391 true), /* pcrel_offset */
392
393 /* GNU extension to record C++ vtable hierarchy */
394 HOWTO (R_SH_GNU_VTINHERIT, /* type */
395 0, /* rightshift */
396 2, /* size (0 = byte, 1 = short, 2 = long) */
397 0, /* bitsize */
398 false, /* pc_relative */
399 0, /* bitpos */
400 complain_overflow_dont, /* complain_on_overflow */
401 NULL, /* special_function */
402 "R_SH_GNU_VTINHERIT", /* name */
403 false, /* partial_inplace */
404 0, /* src_mask */
405 0, /* dst_mask */
406 false), /* pcrel_offset */
407
408 /* GNU extension to record C++ vtable member usage */
409 HOWTO (R_SH_GNU_VTENTRY, /* type */
410 0, /* rightshift */
411 2, /* size (0 = byte, 1 = short, 2 = long) */
412 0, /* bitsize */
413 false, /* pc_relative */
414 0, /* bitpos */
415 complain_overflow_dont, /* complain_on_overflow */
416 _bfd_elf_rel_vtable_reloc_fn, /* special_function */
417 "R_SH_GNU_VTENTRY", /* name */
418 false, /* partial_inplace */
419 0, /* src_mask */
420 0, /* dst_mask */
421 false), /* pcrel_offset */
422
015551fc
JR
423 /* 8 bit PC relative divided by 2 - but specified in a very odd way. */
424 HOWTO (R_SH_LOOP_START, /* type */
425 1, /* rightshift */
426 1, /* size (0 = byte, 1 = short, 2 = long) */
427 8, /* bitsize */
428 false, /* pc_relative */
429 0, /* bitpos */
430 complain_overflow_signed, /* complain_on_overflow */
431 sh_elf_ignore_reloc, /* special_function */
432 "R_SH_LOOP_START", /* name */
433 true, /* partial_inplace */
434 0xff, /* src_mask */
435 0xff, /* dst_mask */
436 true), /* pcrel_offset */
437
438 /* 8 bit PC relative divided by 2 - but specified in a very odd way. */
439 HOWTO (R_SH_LOOP_END, /* type */
440 1, /* rightshift */
441 1, /* size (0 = byte, 1 = short, 2 = long) */
442 8, /* bitsize */
443 false, /* pc_relative */
444 0, /* bitpos */
445 complain_overflow_signed, /* complain_on_overflow */
446 sh_elf_ignore_reloc, /* special_function */
447 "R_SH_LOOP_END", /* name */
448 true, /* partial_inplace */
449 0xff, /* src_mask */
450 0xff, /* dst_mask */
451 true), /* pcrel_offset */
452
252b5132
RH
453};
454
015551fc
JR
455static bfd_reloc_status_type
456sh_elf_reloc_loop (r_type, input_bfd, input_section, contents, addr,
457 symbol_section, start, end)
61ff1804 458 int r_type ATTRIBUTE_UNUSED;
015551fc
JR
459 bfd *input_bfd;
460 asection *input_section;
461 bfd_byte *contents;
462 bfd_vma addr;
463 asection *symbol_section;
464 bfd_vma start, end;
465{
466 static bfd_vma last_addr;
00fdaf47 467 static asection *last_symbol_section;
015551fc
JR
468 bfd_byte *free_contents = NULL;
469 bfd_byte *start_ptr, *ptr, *last_ptr;
470 int diff, cum_diff;
471 bfd_signed_vma x;
472 int insn;
473
474 /* Sanity check the address. */
475 if (addr > input_section->_raw_size)
476 return bfd_reloc_outofrange;
477
478 /* We require the start and end relocations to be processed consecutively -
479 although we allow then to be processed forwards or backwards. */
480 if (! last_addr)
481 {
482 last_addr = addr;
483 last_symbol_section = symbol_section;
484 return bfd_reloc_ok;
485 }
486 if (last_addr != addr)
487 abort ();
488 last_addr = 0;
489
490 if (! symbol_section || last_symbol_section != symbol_section || end < start)
491 return bfd_reloc_outofrange;
492
493 /* Get the symbol_section contents. */
494 if (symbol_section != input_section)
495 {
496 if (elf_section_data (symbol_section)->this_hdr.contents != NULL)
497 contents = elf_section_data (symbol_section)->this_hdr.contents;
498 else
499 {
500 free_contents = contents
501 = (bfd_byte *) bfd_malloc (symbol_section->_raw_size);
502 if (contents == NULL)
503 return bfd_reloc_outofrange;
504 if (! bfd_get_section_contents (input_bfd, symbol_section, contents,
505 (file_ptr) 0,
506 symbol_section->_raw_size))
507 {
508 free (contents);
509 return bfd_reloc_outofrange;
510 }
511 }
512 }
513#define IS_PPI(PTR) ((bfd_get_16 (input_bfd, (PTR)) & 0xfc00) == 0xf800)
514 start_ptr = contents + start;
515 for (cum_diff = -6, ptr = contents + end; cum_diff < 0 && ptr > start_ptr;)
516 {
517 for (last_ptr = ptr, ptr -= 4; ptr >= start_ptr && IS_PPI (ptr);)
518 ptr -= 2;
519 ptr += 2;
61ff1804 520 diff = (last_ptr - ptr) >> 1;
015551fc
JR
521 cum_diff += diff & 1;
522 cum_diff += diff;
523 }
524 /* Calculate the start / end values to load into rs / re minus four -
525 so that will cancel out the four we would otherwise have to add to
526 addr to get the value to subtract in order to get relative addressing. */
527 if (cum_diff >= 0)
528 {
529 start -= 4;
530 end = (ptr + cum_diff * 2) - contents;
531 }
532 else
533 {
534 bfd_vma start0 = start - 4;
535
536 while (start0 >= 0 && IS_PPI (contents + start0))
537 start0 -= 2;
538 start0 = start - 2 - ((start - start0) & 2);
539 start = start0 - cum_diff - 2;
540 end = start0;
541 }
542
543 if (free_contents)
544 free (free_contents);
545
546 insn = bfd_get_16 (input_bfd, contents + addr);
547
548 x = (insn & 0x200 ? end : start) - addr;
549 if (input_section != symbol_section)
550 x += ((symbol_section->output_section->vma + symbol_section->output_offset)
551 - (input_section->output_section->vma
552 + input_section->output_offset));
553 x >>= 1;
554 if (x < -128 || x > 127)
555 return bfd_reloc_overflow;
556
61ff1804 557 x = (insn & ~0xff) | (x & 0xff);
015551fc
JR
558 bfd_put_16 (input_bfd, x, contents + addr);
559
560 return bfd_reloc_ok;
561}
562
563/* This function is used for normal relocs. This used to be like the COFF
252b5132
RH
564 function, and is almost certainly incorrect for other ELF targets. */
565
566static bfd_reloc_status_type
567sh_elf_reloc (abfd, reloc_entry, symbol_in, data, input_section, output_bfd,
568 error_message)
569 bfd *abfd;
570 arelent *reloc_entry;
571 asymbol *symbol_in;
572 PTR data;
573 asection *input_section;
574 bfd *output_bfd;
5f771d47 575 char **error_message ATTRIBUTE_UNUSED;
252b5132
RH
576{
577 unsigned long insn;
578 bfd_vma sym_value;
579 enum elf_sh_reloc_type r_type;
580 bfd_vma addr = reloc_entry->address;
581 bfd_byte *hit_data = addr + (bfd_byte *) data;
582
583 r_type = (enum elf_sh_reloc_type) reloc_entry->howto->type;
584
585 if (output_bfd != NULL)
586 {
587 /* Partial linking--do nothing. */
588 reloc_entry->address += input_section->output_offset;
589 return bfd_reloc_ok;
590 }
591
592 /* Almost all relocs have to do with relaxing. If any work must be
593 done for them, it has been done in sh_relax_section. */
015551fc 594 if (r_type == R_SH_IND12W && (symbol_in->flags & BSF_LOCAL) != 0)
252b5132
RH
595 return bfd_reloc_ok;
596
597 if (symbol_in != NULL
598 && bfd_is_und_section (symbol_in->section))
599 return bfd_reloc_undefined;
600
601 if (bfd_is_com_section (symbol_in->section))
602 sym_value = 0;
603 else
604 sym_value = (symbol_in->value +
605 symbol_in->section->output_section->vma +
606 symbol_in->section->output_offset);
607
608 switch (r_type)
609 {
610 case R_SH_DIR32:
611 insn = bfd_get_32 (abfd, hit_data);
612 insn += sym_value + reloc_entry->addend;
613 bfd_put_32 (abfd, insn, hit_data);
614 break;
615 case R_SH_IND12W:
616 insn = bfd_get_16 (abfd, hit_data);
617 sym_value += reloc_entry->addend;
618 sym_value -= (input_section->output_section->vma
619 + input_section->output_offset
620 + addr
621 + 4);
622 sym_value += (insn & 0xfff) << 1;
623 if (insn & 0x800)
624 sym_value -= 0x1000;
625 insn = (insn & 0xf000) | (sym_value & 0xfff);
626 bfd_put_16 (abfd, insn, hit_data);
627 if (sym_value < (bfd_vma) -0x1000 || sym_value >= 0x1000)
628 return bfd_reloc_overflow;
629 break;
630 default:
631 abort ();
632 break;
633 }
634
635 return bfd_reloc_ok;
636}
637
638/* This function is used for relocs which are only used for relaxing,
639 which the linker should otherwise ignore. */
640
641static bfd_reloc_status_type
642sh_elf_ignore_reloc (abfd, reloc_entry, symbol, data, input_section,
643 output_bfd, error_message)
5f771d47 644 bfd *abfd ATTRIBUTE_UNUSED;
252b5132 645 arelent *reloc_entry;
5f771d47
ILT
646 asymbol *symbol ATTRIBUTE_UNUSED;
647 PTR data ATTRIBUTE_UNUSED;
252b5132
RH
648 asection *input_section;
649 bfd *output_bfd;
5f771d47 650 char **error_message ATTRIBUTE_UNUSED;
252b5132
RH
651{
652 if (output_bfd != NULL)
653 reloc_entry->address += input_section->output_offset;
654 return bfd_reloc_ok;
655}
656
657/* This structure is used to map BFD reloc codes to SH ELF relocs. */
658
659struct elf_reloc_map
660{
661 bfd_reloc_code_real_type bfd_reloc_val;
662 unsigned char elf_reloc_val;
663};
664
665/* An array mapping BFD reloc codes to SH ELF relocs. */
666
667static const struct elf_reloc_map sh_reloc_map[] =
668{
669 { BFD_RELOC_NONE, R_SH_NONE },
670 { BFD_RELOC_32, R_SH_DIR32 },
671 { BFD_RELOC_CTOR, R_SH_DIR32 },
672 { BFD_RELOC_32_PCREL, R_SH_REL32 },
673 { BFD_RELOC_SH_PCDISP8BY2, R_SH_DIR8WPN },
674 { BFD_RELOC_SH_PCDISP12BY2, R_SH_IND12W },
675 { BFD_RELOC_SH_PCRELIMM8BY2, R_SH_DIR8WPZ },
676 { BFD_RELOC_SH_PCRELIMM8BY4, R_SH_DIR8WPL },
677 { BFD_RELOC_8_PCREL, R_SH_SWITCH8 },
678 { BFD_RELOC_SH_SWITCH16, R_SH_SWITCH16 },
679 { BFD_RELOC_SH_SWITCH32, R_SH_SWITCH32 },
680 { BFD_RELOC_SH_USES, R_SH_USES },
681 { BFD_RELOC_SH_COUNT, R_SH_COUNT },
682 { BFD_RELOC_SH_ALIGN, R_SH_ALIGN },
683 { BFD_RELOC_SH_CODE, R_SH_CODE },
684 { BFD_RELOC_SH_DATA, R_SH_DATA },
685 { BFD_RELOC_SH_LABEL, R_SH_LABEL },
686 { BFD_RELOC_VTABLE_INHERIT, R_SH_GNU_VTINHERIT },
687 { BFD_RELOC_VTABLE_ENTRY, R_SH_GNU_VTENTRY },
015551fc
JR
688 { BFD_RELOC_SH_LOOP_START, R_SH_LOOP_START },
689 { BFD_RELOC_SH_LOOP_END, R_SH_LOOP_END },
252b5132
RH
690};
691
692/* Given a BFD reloc code, return the howto structure for the
693 corresponding SH ELf reloc. */
694
695static reloc_howto_type *
696sh_elf_reloc_type_lookup (abfd, code)
5f771d47 697 bfd *abfd ATTRIBUTE_UNUSED;
252b5132
RH
698 bfd_reloc_code_real_type code;
699{
700 unsigned int i;
701
702 for (i = 0; i < sizeof (sh_reloc_map) / sizeof (struct elf_reloc_map); i++)
703 {
704 if (sh_reloc_map[i].bfd_reloc_val == code)
705 return &sh_elf_howto_table[(int) sh_reloc_map[i].elf_reloc_val];
706 }
707
708 return NULL;
709}
710
711/* Given an ELF reloc, fill in the howto field of a relent. */
712
713static void
714sh_elf_info_to_howto (abfd, cache_ptr, dst)
5f771d47 715 bfd *abfd ATTRIBUTE_UNUSED;
252b5132
RH
716 arelent *cache_ptr;
717 Elf_Internal_Rela *dst;
718{
719 unsigned int r;
720
721 r = ELF32_R_TYPE (dst->r_info);
722
723 BFD_ASSERT (r < (unsigned int) R_SH_max);
724 BFD_ASSERT (r < R_SH_FIRST_INVALID_RELOC || r > R_SH_LAST_INVALID_RELOC);
725
726 cache_ptr->howto = &sh_elf_howto_table[r];
727}
728\f
729/* This function handles relaxing for SH ELF. See the corresponding
730 function in coff-sh.c for a description of what this does. FIXME:
731 There is a lot of duplication here between this code and the COFF
732 specific code. The format of relocs and symbols is wound deeply
733 into this code, but it would still be better if the duplication
734 could be eliminated somehow. Note in particular that although both
735 functions use symbols like R_SH_CODE, those symbols have different
736 values; in coff-sh.c they come from include/coff/sh.h, whereas here
737 they come from enum elf_sh_reloc_type in include/elf/sh.h. */
738
739static boolean
740sh_elf_relax_section (abfd, sec, link_info, again)
741 bfd *abfd;
742 asection *sec;
743 struct bfd_link_info *link_info;
744 boolean *again;
745{
746 Elf_Internal_Shdr *symtab_hdr;
747 Elf_Internal_Rela *internal_relocs;
748 Elf_Internal_Rela *free_relocs = NULL;
749 boolean have_code;
750 Elf_Internal_Rela *irel, *irelend;
751 bfd_byte *contents = NULL;
752 bfd_byte *free_contents = NULL;
753 Elf32_External_Sym *extsyms = NULL;
754 Elf32_External_Sym *free_extsyms = NULL;
755
756 *again = false;
757
758 if (link_info->relocateable
759 || (sec->flags & SEC_RELOC) == 0
760 || sec->reloc_count == 0)
761 return true;
762
763 /* If this is the first time we have been called for this section,
764 initialize the cooked size. */
765 if (sec->_cooked_size == 0)
766 sec->_cooked_size = sec->_raw_size;
767
768 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
769
770 internal_relocs = (_bfd_elf32_link_read_relocs
771 (abfd, sec, (PTR) NULL, (Elf_Internal_Rela *) NULL,
772 link_info->keep_memory));
773 if (internal_relocs == NULL)
774 goto error_return;
775 if (! link_info->keep_memory)
776 free_relocs = internal_relocs;
777
778 have_code = false;
779
780 irelend = internal_relocs + sec->reloc_count;
781 for (irel = internal_relocs; irel < irelend; irel++)
782 {
783 bfd_vma laddr, paddr, symval;
784 unsigned short insn;
785 Elf_Internal_Rela *irelfn, *irelscan, *irelcount;
786 bfd_signed_vma foff;
787
788 if (ELF32_R_TYPE (irel->r_info) == (int) R_SH_CODE)
789 have_code = true;
790
791 if (ELF32_R_TYPE (irel->r_info) != (int) R_SH_USES)
792 continue;
793
794 /* Get the section contents. */
795 if (contents == NULL)
796 {
797 if (elf_section_data (sec)->this_hdr.contents != NULL)
798 contents = elf_section_data (sec)->this_hdr.contents;
799 else
800 {
801 contents = (bfd_byte *) bfd_malloc (sec->_raw_size);
802 if (contents == NULL)
803 goto error_return;
804 free_contents = contents;
805
806 if (! bfd_get_section_contents (abfd, sec, contents,
807 (file_ptr) 0, sec->_raw_size))
808 goto error_return;
809 }
810 }
811
812 /* The r_addend field of the R_SH_USES reloc will point us to
813 the register load. The 4 is because the r_addend field is
814 computed as though it were a jump offset, which are based
815 from 4 bytes after the jump instruction. */
816 laddr = irel->r_offset + 4 + irel->r_addend;
817 if (laddr >= sec->_raw_size)
818 {
819 (*_bfd_error_handler) (_("%s: 0x%lx: warning: bad R_SH_USES offset"),
820 bfd_get_filename (abfd),
821 (unsigned long) irel->r_offset);
822 continue;
823 }
824 insn = bfd_get_16 (abfd, contents + laddr);
825
826 /* If the instruction is not mov.l NN,rN, we don't know what to
827 do. */
828 if ((insn & 0xf000) != 0xd000)
829 {
830 ((*_bfd_error_handler)
831 (_("%s: 0x%lx: warning: R_SH_USES points to unrecognized insn 0x%x"),
832 bfd_get_filename (abfd), (unsigned long) irel->r_offset, insn));
833 continue;
834 }
835
836 /* Get the address from which the register is being loaded. The
837 displacement in the mov.l instruction is quadrupled. It is a
838 displacement from four bytes after the movl instruction, but,
839 before adding in the PC address, two least significant bits
840 of the PC are cleared. We assume that the section is aligned
841 on a four byte boundary. */
842 paddr = insn & 0xff;
843 paddr *= 4;
844 paddr += (laddr + 4) &~ 3;
845 if (paddr >= sec->_raw_size)
846 {
847 ((*_bfd_error_handler)
848 (_("%s: 0x%lx: warning: bad R_SH_USES load offset"),
849 bfd_get_filename (abfd), (unsigned long) irel->r_offset));
850 continue;
851 }
852
853 /* Get the reloc for the address from which the register is
854 being loaded. This reloc will tell us which function is
855 actually being called. */
856 for (irelfn = internal_relocs; irelfn < irelend; irelfn++)
857 if (irelfn->r_offset == paddr
858 && ELF32_R_TYPE (irelfn->r_info) == (int) R_SH_DIR32)
859 break;
860 if (irelfn >= irelend)
861 {
862 ((*_bfd_error_handler)
863 (_("%s: 0x%lx: warning: could not find expected reloc"),
864 bfd_get_filename (abfd), (unsigned long) paddr));
865 continue;
866 }
867
868 /* Read this BFD's symbols if we haven't done so already. */
869 if (extsyms == NULL)
870 {
871 if (symtab_hdr->contents != NULL)
872 extsyms = (Elf32_External_Sym *) symtab_hdr->contents;
873 else
874 {
875 extsyms = ((Elf32_External_Sym *)
876 bfd_malloc (symtab_hdr->sh_size));
877 if (extsyms == NULL)
878 goto error_return;
879 free_extsyms = extsyms;
880 if (bfd_seek (abfd, symtab_hdr->sh_offset, SEEK_SET) != 0
881 || (bfd_read (extsyms, 1, symtab_hdr->sh_size, abfd)
882 != symtab_hdr->sh_size))
883 goto error_return;
884 }
885 }
886
887 /* Get the value of the symbol referred to by the reloc. */
888 if (ELF32_R_SYM (irelfn->r_info) < symtab_hdr->sh_info)
889 {
890 Elf_Internal_Sym isym;
891
892 /* A local symbol. */
893 bfd_elf32_swap_symbol_in (abfd,
894 extsyms + ELF32_R_SYM (irelfn->r_info),
895 &isym);
896
897 if (isym.st_shndx != _bfd_elf_section_from_bfd_section (abfd, sec))
898 {
899 ((*_bfd_error_handler)
900 (_("%s: 0x%lx: warning: symbol in unexpected section"),
901 bfd_get_filename (abfd), (unsigned long) paddr));
902 continue;
903 }
904
905 symval = (isym.st_value
906 + sec->output_section->vma
907 + sec->output_offset);
908 }
909 else
910 {
911 unsigned long indx;
912 struct elf_link_hash_entry *h;
913
914 indx = ELF32_R_SYM (irelfn->r_info) - symtab_hdr->sh_info;
915 h = elf_sym_hashes (abfd)[indx];
916 BFD_ASSERT (h != NULL);
917 if (h->root.type != bfd_link_hash_defined
918 && h->root.type != bfd_link_hash_defweak)
919 {
920 /* This appears to be a reference to an undefined
921 symbol. Just ignore it--it will be caught by the
922 regular reloc processing. */
923 continue;
924 }
925
926 symval = (h->root.u.def.value
927 + h->root.u.def.section->output_section->vma
928 + h->root.u.def.section->output_offset);
929 }
930
931 symval += bfd_get_32 (abfd, contents + paddr);
932
933 /* See if this function call can be shortened. */
934 foff = (symval
935 - (irel->r_offset
936 + sec->output_section->vma
937 + sec->output_offset
938 + 4));
939 if (foff < -0x1000 || foff >= 0x1000)
940 {
941 /* After all that work, we can't shorten this function call. */
942 continue;
943 }
944
945 /* Shorten the function call. */
946
947 /* For simplicity of coding, we are going to modify the section
948 contents, the section relocs, and the BFD symbol table. We
949 must tell the rest of the code not to free up this
950 information. It would be possible to instead create a table
951 of changes which have to be made, as is done in coff-mips.c;
952 that would be more work, but would require less memory when
953 the linker is run. */
954
955 elf_section_data (sec)->relocs = internal_relocs;
956 free_relocs = NULL;
957
958 elf_section_data (sec)->this_hdr.contents = contents;
959 free_contents = NULL;
960
961 symtab_hdr->contents = (bfd_byte *) extsyms;
962 free_extsyms = NULL;
963
964 /* Replace the jsr with a bsr. */
965
966 /* Change the R_SH_USES reloc into an R_SH_IND12W reloc, and
967 replace the jsr with a bsr. */
968 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irelfn->r_info), R_SH_IND12W);
969 if (ELF32_R_SYM (irelfn->r_info) < symtab_hdr->sh_info)
970 {
971 /* If this needs to be changed because of future relaxing,
972 it will be handled here like other internal IND12W
973 relocs. */
974 bfd_put_16 (abfd,
975 0xb000 | ((foff >> 1) & 0xfff),
976 contents + irel->r_offset);
977 }
978 else
979 {
980 /* We can't fully resolve this yet, because the external
981 symbol value may be changed by future relaxing. We let
982 the final link phase handle it. */
983 bfd_put_16 (abfd, 0xb000, contents + irel->r_offset);
984 }
985
986 /* See if there is another R_SH_USES reloc referring to the same
987 register load. */
988 for (irelscan = internal_relocs; irelscan < irelend; irelscan++)
989 if (ELF32_R_TYPE (irelscan->r_info) == (int) R_SH_USES
990 && laddr == irelscan->r_offset + 4 + irelscan->r_addend)
991 break;
992 if (irelscan < irelend)
993 {
994 /* Some other function call depends upon this register load,
995 and we have not yet converted that function call.
996 Indeed, we may never be able to convert it. There is
997 nothing else we can do at this point. */
998 continue;
999 }
1000
1001 /* Look for a R_SH_COUNT reloc on the location where the
1002 function address is stored. Do this before deleting any
1003 bytes, to avoid confusion about the address. */
1004 for (irelcount = internal_relocs; irelcount < irelend; irelcount++)
1005 if (irelcount->r_offset == paddr
1006 && ELF32_R_TYPE (irelcount->r_info) == (int) R_SH_COUNT)
1007 break;
1008
1009 /* Delete the register load. */
1010 if (! sh_elf_relax_delete_bytes (abfd, sec, laddr, 2))
1011 goto error_return;
1012
1013 /* That will change things, so, just in case it permits some
1014 other function call to come within range, we should relax
1015 again. Note that this is not required, and it may be slow. */
1016 *again = true;
1017
1018 /* Now check whether we got a COUNT reloc. */
1019 if (irelcount >= irelend)
1020 {
1021 ((*_bfd_error_handler)
1022 (_("%s: 0x%lx: warning: could not find expected COUNT reloc"),
1023 bfd_get_filename (abfd), (unsigned long) paddr));
1024 continue;
1025 }
1026
1027 /* The number of uses is stored in the r_addend field. We've
1028 just deleted one. */
1029 if (irelcount->r_addend == 0)
1030 {
1031 ((*_bfd_error_handler) (_("%s: 0x%lx: warning: bad count"),
1032 bfd_get_filename (abfd),
1033 (unsigned long) paddr));
1034 continue;
1035 }
1036
1037 --irelcount->r_addend;
1038
1039 /* If there are no more uses, we can delete the address. Reload
1040 the address from irelfn, in case it was changed by the
1041 previous call to sh_elf_relax_delete_bytes. */
1042 if (irelcount->r_addend == 0)
1043 {
1044 if (! sh_elf_relax_delete_bytes (abfd, sec, irelfn->r_offset, 4))
1045 goto error_return;
1046 }
1047
1048 /* We've done all we can with that function call. */
1049 }
1050
1051 /* Look for load and store instructions that we can align on four
1052 byte boundaries. */
1053 if (have_code)
1054 {
1055 boolean swapped;
1056
1057 /* Get the section contents. */
1058 if (contents == NULL)
1059 {
1060 if (elf_section_data (sec)->this_hdr.contents != NULL)
1061 contents = elf_section_data (sec)->this_hdr.contents;
1062 else
1063 {
1064 contents = (bfd_byte *) bfd_malloc (sec->_raw_size);
1065 if (contents == NULL)
1066 goto error_return;
1067 free_contents = contents;
1068
1069 if (! bfd_get_section_contents (abfd, sec, contents,
1070 (file_ptr) 0, sec->_raw_size))
1071 goto error_return;
1072 }
1073 }
1074
1075 if (! sh_elf_align_loads (abfd, sec, internal_relocs, contents,
1076 &swapped))
1077 goto error_return;
1078
1079 if (swapped)
1080 {
1081 elf_section_data (sec)->relocs = internal_relocs;
1082 free_relocs = NULL;
1083
1084 elf_section_data (sec)->this_hdr.contents = contents;
1085 free_contents = NULL;
1086
1087 symtab_hdr->contents = (bfd_byte *) extsyms;
1088 free_extsyms = NULL;
1089 }
1090 }
1091
1092 if (free_relocs != NULL)
1093 {
1094 free (free_relocs);
1095 free_relocs = NULL;
1096 }
1097
1098 if (free_contents != NULL)
1099 {
1100 if (! link_info->keep_memory)
1101 free (free_contents);
1102 else
1103 {
1104 /* Cache the section contents for elf_link_input_bfd. */
1105 elf_section_data (sec)->this_hdr.contents = contents;
1106 }
1107 free_contents = NULL;
1108 }
1109
1110 if (free_extsyms != NULL)
1111 {
1112 if (! link_info->keep_memory)
1113 free (free_extsyms);
1114 else
1115 {
1116 /* Cache the symbols for elf_link_input_bfd. */
1117 symtab_hdr->contents = extsyms;
1118 }
1119 free_extsyms = NULL;
1120 }
1121
1122 return true;
1123
1124 error_return:
1125 if (free_relocs != NULL)
1126 free (free_relocs);
1127 if (free_contents != NULL)
1128 free (free_contents);
1129 if (free_extsyms != NULL)
1130 free (free_extsyms);
1131 return false;
1132}
1133
1134/* Delete some bytes from a section while relaxing. FIXME: There is a
1135 lot of duplication between this function and sh_relax_delete_bytes
1136 in coff-sh.c. */
1137
1138static boolean
1139sh_elf_relax_delete_bytes (abfd, sec, addr, count)
1140 bfd *abfd;
1141 asection *sec;
1142 bfd_vma addr;
1143 int count;
1144{
1145 Elf_Internal_Shdr *symtab_hdr;
1146 Elf32_External_Sym *extsyms;
1147 int shndx, index;
1148 bfd_byte *contents;
1149 Elf_Internal_Rela *irel, *irelend;
1150 Elf_Internal_Rela *irelalign;
1151 bfd_vma toaddr;
1152 Elf32_External_Sym *esym, *esymend;
1153 struct elf_link_hash_entry *sym_hash;
1154 asection *o;
1155
1156 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1157 extsyms = (Elf32_External_Sym *) symtab_hdr->contents;
1158
1159 shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
1160
1161 contents = elf_section_data (sec)->this_hdr.contents;
1162
1163 /* The deletion must stop at the next ALIGN reloc for an aligment
1164 power larger than the number of bytes we are deleting. */
1165
1166 irelalign = NULL;
1167 toaddr = sec->_cooked_size;
1168
1169 irel = elf_section_data (sec)->relocs;
1170 irelend = irel + sec->reloc_count;
1171 for (; irel < irelend; irel++)
1172 {
1173 if (ELF32_R_TYPE (irel->r_info) == (int) R_SH_ALIGN
1174 && irel->r_offset > addr
1175 && count < (1 << irel->r_addend))
1176 {
1177 irelalign = irel;
1178 toaddr = irel->r_offset;
1179 break;
1180 }
1181 }
1182
1183 /* Actually delete the bytes. */
1184 memmove (contents + addr, contents + addr + count, toaddr - addr - count);
1185 if (irelalign == NULL)
1186 sec->_cooked_size -= count;
1187 else
1188 {
1189 int i;
1190
1191#define NOP_OPCODE (0x0009)
1192
1193 BFD_ASSERT ((count & 1) == 0);
1194 for (i = 0; i < count; i += 2)
1195 bfd_put_16 (abfd, NOP_OPCODE, contents + toaddr - count + i);
1196 }
1197
1198 /* Adjust all the relocs. */
1199 for (irel = elf_section_data (sec)->relocs; irel < irelend; irel++)
1200 {
1201 bfd_vma nraddr, stop;
1202 bfd_vma start = 0;
1203 int insn = 0;
1204 Elf_Internal_Sym sym;
1205 int off, adjust, oinsn;
1206 bfd_signed_vma voff = 0;
1207 boolean overflow;
1208
1209 /* Get the new reloc address. */
1210 nraddr = irel->r_offset;
1211 if ((irel->r_offset > addr
1212 && irel->r_offset < toaddr)
1213 || (ELF32_R_TYPE (irel->r_info) == (int) R_SH_ALIGN
1214 && irel->r_offset == toaddr))
1215 nraddr -= count;
1216
1217 /* See if this reloc was for the bytes we have deleted, in which
1218 case we no longer care about it. Don't delete relocs which
1219 represent addresses, though. */
1220 if (irel->r_offset >= addr
1221 && irel->r_offset < addr + count
1222 && ELF32_R_TYPE (irel->r_info) != (int) R_SH_ALIGN
1223 && ELF32_R_TYPE (irel->r_info) != (int) R_SH_CODE
1224 && ELF32_R_TYPE (irel->r_info) != (int) R_SH_DATA
1225 && ELF32_R_TYPE (irel->r_info) != (int) R_SH_LABEL)
1226 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
1227 (int) R_SH_NONE);
1228
1229 /* If this is a PC relative reloc, see if the range it covers
1230 includes the bytes we have deleted. */
1231 switch ((enum elf_sh_reloc_type) ELF32_R_TYPE (irel->r_info))
1232 {
1233 default:
1234 break;
1235
1236 case R_SH_DIR8WPN:
1237 case R_SH_IND12W:
1238 case R_SH_DIR8WPZ:
1239 case R_SH_DIR8WPL:
1240 start = irel->r_offset;
1241 insn = bfd_get_16 (abfd, contents + nraddr);
1242 break;
1243 }
1244
1245 switch ((enum elf_sh_reloc_type) ELF32_R_TYPE (irel->r_info))
1246 {
1247 default:
1248 start = stop = addr;
1249 break;
1250
1251 case R_SH_DIR32:
1252 /* If this reloc is against a symbol defined in this
1253 section, and the symbol will not be adjusted below, we
1254 must check the addend to see it will put the value in
1255 range to be adjusted, and hence must be changed. */
1256 if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
1257 {
1258 bfd_elf32_swap_symbol_in (abfd,
1259 extsyms + ELF32_R_SYM (irel->r_info),
1260 &sym);
1261 if (sym.st_shndx == shndx
1262 && (sym.st_value <= addr
1263 || sym.st_value >= toaddr))
1264 {
1265 bfd_vma val;
1266
1267 val = bfd_get_32 (abfd, contents + nraddr);
1268 val += sym.st_value;
1269 if (val > addr && val < toaddr)
1270 bfd_put_32 (abfd, val - count, contents + nraddr);
1271 }
1272 }
1273 start = stop = addr;
1274 break;
1275
1276 case R_SH_DIR8WPN:
1277 off = insn & 0xff;
1278 if (off & 0x80)
1279 off -= 0x100;
1280 stop = (bfd_vma) ((bfd_signed_vma) start + 4 + off * 2);
1281 break;
1282
1283 case R_SH_IND12W:
1284 if (ELF32_R_SYM (irel->r_info) >= symtab_hdr->sh_info)
1285 start = stop = addr;
1286 else
1287 {
1288 off = insn & 0xfff;
1289 if (off & 0x800)
1290 off -= 0x1000;
1291 stop = (bfd_vma) ((bfd_signed_vma) start + 4 + off * 2);
1292 }
1293 break;
1294
1295 case R_SH_DIR8WPZ:
1296 off = insn & 0xff;
1297 stop = start + 4 + off * 2;
1298 break;
1299
1300 case R_SH_DIR8WPL:
1301 off = insn & 0xff;
1302 stop = (start &~ (bfd_vma) 3) + 4 + off * 4;
1303 break;
1304
1305 case R_SH_SWITCH8:
1306 case R_SH_SWITCH16:
1307 case R_SH_SWITCH32:
1308 /* These relocs types represent
1309 .word L2-L1
06e1ba78 1310 The r_addend field holds the difference between the reloc
252b5132
RH
1311 address and L1. That is the start of the reloc, and
1312 adding in the contents gives us the top. We must adjust
06e1ba78
JR
1313 both the r_offset field and the section contents.
1314 N.B. in gas / coff bfd, the elf bfd r_addend is called r_offset,
1315 and the elf bfd r_offset is called r_vaddr. */
252b5132 1316
06e1ba78
JR
1317 stop = irel->r_offset;
1318 start = (bfd_vma) ((bfd_signed_vma) stop - (long) irel->r_addend);
252b5132
RH
1319
1320 if (start > addr
1321 && start < toaddr
1322 && (stop <= addr || stop >= toaddr))
1323 irel->r_addend += count;
1324 else if (stop > addr
1325 && stop < toaddr
1326 && (start <= addr || start >= toaddr))
1327 irel->r_addend -= count;
1328
252b5132
RH
1329 if (ELF32_R_TYPE (irel->r_info) == (int) R_SH_SWITCH16)
1330 voff = bfd_get_signed_16 (abfd, contents + nraddr);
1331 else if (ELF32_R_TYPE (irel->r_info) == (int) R_SH_SWITCH8)
1332 voff = bfd_get_8 (abfd, contents + nraddr);
1333 else
1334 voff = bfd_get_signed_32 (abfd, contents + nraddr);
1335 stop = (bfd_vma) ((bfd_signed_vma) start + voff);
1336
1337 break;
1338
1339 case R_SH_USES:
1340 start = irel->r_offset;
1341 stop = (bfd_vma) ((bfd_signed_vma) start
1342 + (long) irel->r_addend
1343 + 4);
1344 break;
1345 }
1346
1347 if (start > addr
1348 && start < toaddr
1349 && (stop <= addr || stop >= toaddr))
1350 adjust = count;
1351 else if (stop > addr
1352 && stop < toaddr
1353 && (start <= addr || start >= toaddr))
1354 adjust = - count;
1355 else
1356 adjust = 0;
1357
1358 if (adjust != 0)
1359 {
1360 oinsn = insn;
1361 overflow = false;
1362 switch ((enum elf_sh_reloc_type) ELF32_R_TYPE (irel->r_info))
1363 {
1364 default:
1365 abort ();
1366 break;
1367
1368 case R_SH_DIR8WPN:
1369 case R_SH_DIR8WPZ:
1370 insn += adjust / 2;
1371 if ((oinsn & 0xff00) != (insn & 0xff00))
1372 overflow = true;
1373 bfd_put_16 (abfd, insn, contents + nraddr);
1374 break;
1375
1376 case R_SH_IND12W:
1377 insn += adjust / 2;
1378 if ((oinsn & 0xf000) != (insn & 0xf000))
1379 overflow = true;
1380 bfd_put_16 (abfd, insn, contents + nraddr);
1381 break;
1382
1383 case R_SH_DIR8WPL:
1384 BFD_ASSERT (adjust == count || count >= 4);
1385 if (count >= 4)
1386 insn += adjust / 4;
1387 else
1388 {
1389 if ((irel->r_offset & 3) == 0)
1390 ++insn;
1391 }
1392 if ((oinsn & 0xff00) != (insn & 0xff00))
1393 overflow = true;
1394 bfd_put_16 (abfd, insn, contents + nraddr);
1395 break;
1396
851cde10
JR
1397 case R_SH_SWITCH8:
1398 voff += adjust;
1399 if (voff < 0 || voff >= 0xff)
1400 overflow = true;
1401 bfd_put_8 (abfd, voff, contents + nraddr);
1402 break;
1403
252b5132
RH
1404 case R_SH_SWITCH16:
1405 voff += adjust;
1406 if (voff < - 0x8000 || voff >= 0x8000)
1407 overflow = true;
1408 bfd_put_signed_16 (abfd, voff, contents + nraddr);
1409 break;
1410
1411 case R_SH_SWITCH32:
1412 voff += adjust;
1413 bfd_put_signed_32 (abfd, voff, contents + nraddr);
1414 break;
1415
1416 case R_SH_USES:
1417 irel->r_addend += adjust;
1418 break;
1419 }
1420
1421 if (overflow)
1422 {
1423 ((*_bfd_error_handler)
1424 (_("%s: 0x%lx: fatal: reloc overflow while relaxing"),
1425 bfd_get_filename (abfd), (unsigned long) irel->r_offset));
1426 bfd_set_error (bfd_error_bad_value);
1427 return false;
1428 }
1429 }
1430
1431 irel->r_offset = nraddr;
1432 }
1433
1434 /* Look through all the other sections. If there contain any IMM32
1435 relocs against internal symbols which we are not going to adjust
1436 below, we may need to adjust the addends. */
1437 for (o = abfd->sections; o != NULL; o = o->next)
1438 {
1439 Elf_Internal_Rela *internal_relocs;
1440 Elf_Internal_Rela *irelscan, *irelscanend;
1441 bfd_byte *ocontents;
1442
1443 if (o == sec
1444 || (o->flags & SEC_RELOC) == 0
1445 || o->reloc_count == 0)
1446 continue;
1447
1448 /* We always cache the relocs. Perhaps, if info->keep_memory is
1449 false, we should free them, if we are permitted to, when we
1450 leave sh_coff_relax_section. */
1451 internal_relocs = (_bfd_elf32_link_read_relocs
1452 (abfd, o, (PTR) NULL, (Elf_Internal_Rela *) NULL,
1453 true));
1454 if (internal_relocs == NULL)
1455 return false;
1456
1457 ocontents = NULL;
1458 irelscanend = internal_relocs + o->reloc_count;
1459 for (irelscan = internal_relocs; irelscan < irelscanend; irelscan++)
1460 {
1461 Elf_Internal_Sym sym;
1462
084aa3aa
JR
1463 /* Dwarf line numbers use R_SH_SWITCH32 relocs. */
1464 if (ELF32_R_TYPE (irelscan->r_info) == (int) R_SH_SWITCH32)
1465 {
1466 bfd_vma start, stop;
1467 bfd_signed_vma voff;
1468
1469 if (ocontents == NULL)
1470 {
1471 if (elf_section_data (o)->this_hdr.contents != NULL)
1472 ocontents = elf_section_data (o)->this_hdr.contents;
1473 else
1474 {
1475 /* We always cache the section contents.
1476 Perhaps, if info->keep_memory is false, we
1477 should free them, if we are permitted to,
1478 when we leave sh_coff_relax_section. */
1479 ocontents = (bfd_byte *) bfd_malloc (o->_raw_size);
1480 if (ocontents == NULL)
1481 return false;
1482 if (! bfd_get_section_contents (abfd, o, ocontents,
1483 (file_ptr) 0,
1484 o->_raw_size))
1485 return false;
1486 elf_section_data (o)->this_hdr.contents = ocontents;
1487 }
1488 }
1489
1490 stop = irelscan->r_offset;
1491 start
1492 = (bfd_vma) ((bfd_signed_vma) stop - (long) irelscan->r_addend);
1493
1494 /* STOP is in a different section, so it won't change. */
1495 if (start > addr && start < toaddr)
1496 irelscan->r_addend += count;
1497
1498 voff = bfd_get_signed_32 (abfd, ocontents + irelscan->r_offset);
1499 stop = (bfd_vma) ((bfd_signed_vma) start + voff);
1500
1501 if (start > addr
1502 && start < toaddr
1503 && (stop <= addr || stop >= toaddr))
1504 bfd_put_signed_32 (abfd, voff + count,
1505 ocontents + irelscan->r_offset);
1506 else if (stop > addr
1507 && stop < toaddr
1508 && (start <= addr || start >= toaddr))
1509 bfd_put_signed_32 (abfd, voff - count,
1510 ocontents + irelscan->r_offset);
1511 }
1512
252b5132
RH
1513 if (ELF32_R_TYPE (irelscan->r_info) != (int) R_SH_DIR32)
1514 continue;
1515
1516 if (ELF32_R_SYM (irelscan->r_info) >= symtab_hdr->sh_info)
1517 continue;
1518
1519 bfd_elf32_swap_symbol_in (abfd,
1520 extsyms + ELF32_R_SYM (irelscan->r_info),
1521 &sym);
1522
1523 if (sym.st_shndx == shndx
1524 && (sym.st_value <= addr
1525 || sym.st_value >= toaddr))
1526 {
1527 bfd_vma val;
1528
1529 if (ocontents == NULL)
1530 {
1531 if (elf_section_data (o)->this_hdr.contents != NULL)
1532 ocontents = elf_section_data (o)->this_hdr.contents;
1533 else
1534 {
1535 /* We always cache the section contents.
1536 Perhaps, if info->keep_memory is false, we
1537 should free them, if we are permitted to,
1538 when we leave sh_coff_relax_section. */
1539 ocontents = (bfd_byte *) bfd_malloc (o->_raw_size);
1540 if (ocontents == NULL)
1541 return false;
1542 if (! bfd_get_section_contents (abfd, o, ocontents,
1543 (file_ptr) 0,
1544 o->_raw_size))
1545 return false;
1546 elf_section_data (o)->this_hdr.contents = ocontents;
1547 }
1548 }
1549
1550 val = bfd_get_32 (abfd, ocontents + irelscan->r_offset);
1551 val += sym.st_value;
1552 if (val > addr && val < toaddr)
1553 bfd_put_32 (abfd, val - count,
1554 ocontents + irelscan->r_offset);
1555 }
1556 }
1557 }
1558
1559 /* Adjust the local symbols defined in this section. */
1560 esym = extsyms;
1561 esymend = esym + symtab_hdr->sh_info;
1562 for (; esym < esymend; esym++)
1563 {
1564 Elf_Internal_Sym isym;
1565
1566 bfd_elf32_swap_symbol_in (abfd, esym, &isym);
1567
1568 if (isym.st_shndx == shndx
1569 && isym.st_value > addr
1570 && isym.st_value < toaddr)
1571 {
1572 isym.st_value -= count;
1573 bfd_elf32_swap_symbol_out (abfd, &isym, esym);
1574 }
1575 }
1576
1577 /* Now adjust the global symbols defined in this section. */
1578 esym = extsyms + symtab_hdr->sh_info;
1579 esymend = extsyms + (symtab_hdr->sh_size / sizeof (Elf32_External_Sym));
1580 for (index = 0; esym < esymend; esym++, index++)
1581 {
1582 Elf_Internal_Sym isym;
1583
1584 bfd_elf32_swap_symbol_in (abfd, esym, &isym);
1585 sym_hash = elf_sym_hashes (abfd)[index];
1586 if (isym.st_shndx == shndx
1587 && ((sym_hash)->root.type == bfd_link_hash_defined
1588 || (sym_hash)->root.type == bfd_link_hash_defweak)
1589 && (sym_hash)->root.u.def.section == sec
1590 && (sym_hash)->root.u.def.value > addr
1591 && (sym_hash)->root.u.def.value < toaddr)
1592 {
1593 (sym_hash)->root.u.def.value -= count;
1594 }
1595 }
1596
1597 /* See if we can move the ALIGN reloc forward. We have adjusted
1598 r_offset for it already. */
1599 if (irelalign != NULL)
1600 {
1601 bfd_vma alignto, alignaddr;
1602
1603 alignto = BFD_ALIGN (toaddr, 1 << irelalign->r_addend);
1604 alignaddr = BFD_ALIGN (irelalign->r_offset,
1605 1 << irelalign->r_addend);
1606 if (alignto != alignaddr)
1607 {
1608 /* Tail recursion. */
1609 return sh_elf_relax_delete_bytes (abfd, sec, alignaddr,
1610 alignto - alignaddr);
1611 }
1612 }
1613
1614 return true;
1615}
1616
1617/* Look for loads and stores which we can align to four byte
1618 boundaries. This is like sh_align_loads in coff-sh.c. */
1619
1620static boolean
1621sh_elf_align_loads (abfd, sec, internal_relocs, contents, pswapped)
1622 bfd *abfd;
1623 asection *sec;
1624 Elf_Internal_Rela *internal_relocs;
1625 bfd_byte *contents;
1626 boolean *pswapped;
1627{
1628 Elf_Internal_Rela *irel, *irelend;
1629 bfd_vma *labels = NULL;
1630 bfd_vma *label, *label_end;
1631
1632 *pswapped = false;
1633
1634 irelend = internal_relocs + sec->reloc_count;
1635
1636 /* Get all the addresses with labels on them. */
1637 labels = (bfd_vma *) bfd_malloc (sec->reloc_count * sizeof (bfd_vma));
1638 if (labels == NULL)
1639 goto error_return;
1640 label_end = labels;
1641 for (irel = internal_relocs; irel < irelend; irel++)
1642 {
1643 if (ELF32_R_TYPE (irel->r_info) == (int) R_SH_LABEL)
1644 {
1645 *label_end = irel->r_offset;
1646 ++label_end;
1647 }
1648 }
1649
1650 /* Note that the assembler currently always outputs relocs in
1651 address order. If that ever changes, this code will need to sort
1652 the label values and the relocs. */
1653
1654 label = labels;
1655
1656 for (irel = internal_relocs; irel < irelend; irel++)
1657 {
1658 bfd_vma start, stop;
1659
1660 if (ELF32_R_TYPE (irel->r_info) != (int) R_SH_CODE)
1661 continue;
1662
1663 start = irel->r_offset;
1664
1665 for (irel++; irel < irelend; irel++)
1666 if (ELF32_R_TYPE (irel->r_info) == (int) R_SH_DATA)
1667 break;
1668 if (irel < irelend)
1669 stop = irel->r_offset;
1670 else
1671 stop = sec->_cooked_size;
1672
1673 if (! _bfd_sh_align_load_span (abfd, sec, contents, sh_elf_swap_insns,
1674 (PTR) internal_relocs, &label,
1675 label_end, start, stop, pswapped))
1676 goto error_return;
1677 }
1678
1679 free (labels);
1680
1681 return true;
1682
1683 error_return:
1684 if (labels != NULL)
1685 free (labels);
1686 return false;
1687}
1688
1689/* Swap two SH instructions. This is like sh_swap_insns in coff-sh.c. */
1690
1691static boolean
1692sh_elf_swap_insns (abfd, sec, relocs, contents, addr)
1693 bfd *abfd;
1694 asection *sec;
1695 PTR relocs;
1696 bfd_byte *contents;
1697 bfd_vma addr;
1698{
1699 Elf_Internal_Rela *internal_relocs = (Elf_Internal_Rela *) relocs;
1700 unsigned short i1, i2;
1701 Elf_Internal_Rela *irel, *irelend;
1702
1703 /* Swap the instructions themselves. */
1704 i1 = bfd_get_16 (abfd, contents + addr);
1705 i2 = bfd_get_16 (abfd, contents + addr + 2);
1706 bfd_put_16 (abfd, i2, contents + addr);
1707 bfd_put_16 (abfd, i1, contents + addr + 2);
1708
1709 /* Adjust all reloc addresses. */
1710 irelend = internal_relocs + sec->reloc_count;
1711 for (irel = internal_relocs; irel < irelend; irel++)
1712 {
1713 enum elf_sh_reloc_type type;
1714 int add;
1715
1716 /* There are a few special types of relocs that we don't want to
1717 adjust. These relocs do not apply to the instruction itself,
1718 but are only associated with the address. */
1719 type = (enum elf_sh_reloc_type) ELF32_R_TYPE (irel->r_info);
1720 if (type == R_SH_ALIGN
1721 || type == R_SH_CODE
1722 || type == R_SH_DATA
1723 || type == R_SH_LABEL)
1724 continue;
1725
1726 /* If an R_SH_USES reloc points to one of the addresses being
1727 swapped, we must adjust it. It would be incorrect to do this
1728 for a jump, though, since we want to execute both
1729 instructions after the jump. (We have avoided swapping
1730 around a label, so the jump will not wind up executing an
1731 instruction it shouldn't). */
1732 if (type == R_SH_USES)
1733 {
1734 bfd_vma off;
1735
1736 off = irel->r_offset + 4 + irel->r_addend;
1737 if (off == addr)
1738 irel->r_offset += 2;
1739 else if (off == addr + 2)
1740 irel->r_offset -= 2;
1741 }
1742
1743 if (irel->r_offset == addr)
1744 {
1745 irel->r_offset += 2;
1746 add = -2;
1747 }
1748 else if (irel->r_offset == addr + 2)
1749 {
1750 irel->r_offset -= 2;
1751 add = 2;
1752 }
1753 else
1754 add = 0;
1755
1756 if (add != 0)
1757 {
1758 bfd_byte *loc;
1759 unsigned short insn, oinsn;
1760 boolean overflow;
1761
1762 loc = contents + irel->r_offset;
1763 overflow = false;
1764 switch (type)
1765 {
1766 default:
1767 break;
1768
1769 case R_SH_DIR8WPN:
1770 case R_SH_DIR8WPZ:
1771 insn = bfd_get_16 (abfd, loc);
1772 oinsn = insn;
1773 insn += add / 2;
1774 if ((oinsn & 0xff00) != (insn & 0xff00))
1775 overflow = true;
1776 bfd_put_16 (abfd, insn, loc);
1777 break;
1778
1779 case R_SH_IND12W:
1780 insn = bfd_get_16 (abfd, loc);
1781 oinsn = insn;
1782 insn += add / 2;
1783 if ((oinsn & 0xf000) != (insn & 0xf000))
1784 overflow = true;
1785 bfd_put_16 (abfd, insn, loc);
1786 break;
1787
1788 case R_SH_DIR8WPL:
1789 /* This reloc ignores the least significant 3 bits of
1790 the program counter before adding in the offset.
1791 This means that if ADDR is at an even address, the
1792 swap will not affect the offset. If ADDR is an at an
1793 odd address, then the instruction will be crossing a
1794 four byte boundary, and must be adjusted. */
1795 if ((addr & 3) != 0)
1796 {
1797 insn = bfd_get_16 (abfd, loc);
1798 oinsn = insn;
1799 insn += add / 2;
1800 if ((oinsn & 0xff00) != (insn & 0xff00))
1801 overflow = true;
1802 bfd_put_16 (abfd, insn, loc);
1803 }
1804
1805 break;
1806 }
1807
1808 if (overflow)
1809 {
1810 ((*_bfd_error_handler)
1811 (_("%s: 0x%lx: fatal: reloc overflow while relaxing"),
1812 bfd_get_filename (abfd), (unsigned long) irel->r_offset));
1813 bfd_set_error (bfd_error_bad_value);
1814 return false;
1815 }
1816 }
1817 }
1818
1819 return true;
1820}
1821\f
1822/* Relocate an SH ELF section. */
1823
1824static boolean
1825sh_elf_relocate_section (output_bfd, info, input_bfd, input_section,
1826 contents, relocs, local_syms, local_sections)
5f771d47 1827 bfd *output_bfd ATTRIBUTE_UNUSED;
252b5132
RH
1828 struct bfd_link_info *info;
1829 bfd *input_bfd;
1830 asection *input_section;
1831 bfd_byte *contents;
1832 Elf_Internal_Rela *relocs;
1833 Elf_Internal_Sym *local_syms;
1834 asection **local_sections;
1835{
1836 Elf_Internal_Shdr *symtab_hdr;
1837 struct elf_link_hash_entry **sym_hashes;
1838 Elf_Internal_Rela *rel, *relend;
1839
1840 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
1841 sym_hashes = elf_sym_hashes (input_bfd);
1842
1843 rel = relocs;
1844 relend = relocs + input_section->reloc_count;
1845 for (; rel < relend; rel++)
1846 {
1847 int r_type;
1848 reloc_howto_type *howto;
1849 unsigned long r_symndx;
1850 Elf_Internal_Sym *sym;
1851 asection *sec;
1852 struct elf_link_hash_entry *h;
1853 bfd_vma relocation;
1854 bfd_vma addend = (bfd_vma)0;
1855 bfd_reloc_status_type r;
1856
1857 r_symndx = ELF32_R_SYM (rel->r_info);
1858
1859 if (info->relocateable)
1860 {
1861 /* This is a relocateable link. We don't have to change
1862 anything, unless the reloc is against a section symbol,
1863 in which case we have to adjust according to where the
1864 section symbol winds up in the output section. */
1865 if (r_symndx < symtab_hdr->sh_info)
1866 {
1867 sym = local_syms + r_symndx;
1868 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
1869 {
1870 sec = local_sections[r_symndx];
1871 rel->r_addend += sec->output_offset + sym->st_value;
1872 }
1873 }
1874
1875 continue;
1876 }
1877
1878 r_type = ELF32_R_TYPE (rel->r_info);
1879
1880 /* Many of the relocs are only used for relaxing, and are
1881 handled entirely by the relaxation code. */
015551fc
JR
1882 if (r_type > (int) R_SH_LAST_INVALID_RELOC
1883 && r_type < (int) R_SH_LOOP_START)
252b5132
RH
1884 continue;
1885
1886 if (r_type < 0
015551fc
JR
1887 || (r_type >= (int) R_SH_FIRST_INVALID_RELOC
1888 && r_type <= (int) R_SH_LAST_INVALID_RELOC))
252b5132
RH
1889 {
1890 bfd_set_error (bfd_error_bad_value);
1891 return false;
1892 }
1893
1894 /* FIXME: This is certainly incorrect. However, it is how the
1895 COFF linker works. */
1896 if (r_type != (int) R_SH_DIR32
015551fc
JR
1897 && r_type != (int) R_SH_IND12W
1898 && r_type != (int) R_SH_LOOP_START
1899 && r_type != (int) R_SH_LOOP_END)
252b5132
RH
1900 continue;
1901
1902 howto = sh_elf_howto_table + r_type;
1903
1904 /* This is a final link. */
1905 h = NULL;
1906 sym = NULL;
1907 sec = NULL;
1908 if (r_symndx < symtab_hdr->sh_info)
1909 {
1910 /* There is nothing to be done for an internal IND12W
1911 relocation. FIXME: This is probably wrong, but it's how
1912 the COFF relocations work. */
1913 if (r_type == (int) R_SH_IND12W)
1914 continue;
1915 sym = local_syms + r_symndx;
1916 sec = local_sections[r_symndx];
1917 relocation = (sec->output_section->vma
1918 + sec->output_offset
1919 + sym->st_value);
1920 }
1921 else
1922 {
1923 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1924 while (h->root.type == bfd_link_hash_indirect
1925 || h->root.type == bfd_link_hash_warning)
1926 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1927 if (h->root.type == bfd_link_hash_defined
1928 || h->root.type == bfd_link_hash_defweak)
1929 {
1930 sec = h->root.u.def.section;
1931 relocation = (h->root.u.def.value
1932 + sec->output_section->vma
1933 + sec->output_offset);
1934 }
1935 else if (h->root.type == bfd_link_hash_undefweak)
1936 relocation = 0;
1937 else
1938 {
1939 if (! ((*info->callbacks->undefined_symbol)
1940 (info, h->root.root.string, input_bfd,
5cc7c785 1941 input_section, rel->r_offset, true)))
252b5132
RH
1942 return false;
1943 relocation = 0;
1944 }
1945 }
1946
1947 /* FIXME: This is how the COFF relocations work. */
1948 if (r_type == (int) R_SH_IND12W)
1949 relocation -= 4;
1950
1951 switch ((int)r_type)
1952 {
1953 case (int)R_SH_DIR32:
1954 addend = rel->r_addend;
015551fc
JR
1955 /* Fall through. */
1956 default:
1957 /* COFF relocs don't use the addend. The addend is used for
1958 R_SH_DIR32 to be compatible with other compilers. */
1959 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
1960 contents, rel->r_offset,
1961 relocation, addend);
252b5132 1962 break;
015551fc
JR
1963 case R_SH_LOOP_START:
1964 {
1965 static bfd_vma start, end;
1966
1967 start = (relocation + rel->r_addend
1968 - (sec->output_section->vma + sec->output_offset));
1969 r = sh_elf_reloc_loop (r_type, input_bfd, input_section, contents,
1970 rel->r_offset, sec, start, end);
1971 break;
1972 case R_SH_LOOP_END:
1973 end = (relocation + rel->r_addend
1974 - (sec->output_section->vma + sec->output_offset));
1975 r = sh_elf_reloc_loop (r_type, input_bfd, input_section, contents,
1976 rel->r_offset, sec, start, end);
1977 break;
1978 }
252b5132
RH
1979 }
1980
252b5132
RH
1981 if (r != bfd_reloc_ok)
1982 {
1983 switch (r)
1984 {
1985 default:
1986 case bfd_reloc_outofrange:
1987 abort ();
1988 case bfd_reloc_overflow:
1989 {
1990 const char *name;
1991
1992 if (h != NULL)
1993 name = h->root.root.string;
1994 else
1995 {
1996 name = (bfd_elf_string_from_elf_section
1997 (input_bfd, symtab_hdr->sh_link, sym->st_name));
1998 if (name == NULL)
1999 return false;
2000 if (*name == '\0')
2001 name = bfd_section_name (input_bfd, sec);
2002 }
2003 if (! ((*info->callbacks->reloc_overflow)
2004 (info, name, howto->name, (bfd_vma) 0,
2005 input_bfd, input_section, rel->r_offset)))
2006 return false;
2007 }
2008 break;
2009 }
2010 }
2011 }
2012
2013 return true;
2014}
2015
2016/* This is a version of bfd_generic_get_relocated_section_contents
2017 which uses sh_elf_relocate_section. */
2018
2019static bfd_byte *
2020sh_elf_get_relocated_section_contents (output_bfd, link_info, link_order,
2021 data, relocateable, symbols)
2022 bfd *output_bfd;
2023 struct bfd_link_info *link_info;
2024 struct bfd_link_order *link_order;
2025 bfd_byte *data;
2026 boolean relocateable;
2027 asymbol **symbols;
2028{
2029 Elf_Internal_Shdr *symtab_hdr;
2030 asection *input_section = link_order->u.indirect.section;
2031 bfd *input_bfd = input_section->owner;
2032 asection **sections = NULL;
2033 Elf_Internal_Rela *internal_relocs = NULL;
2034 Elf32_External_Sym *external_syms = NULL;
2035 Elf_Internal_Sym *internal_syms = NULL;
2036
2037 /* We only need to handle the case of relaxing, or of having a
2038 particular set of section contents, specially. */
2039 if (relocateable
2040 || elf_section_data (input_section)->this_hdr.contents == NULL)
2041 return bfd_generic_get_relocated_section_contents (output_bfd, link_info,
2042 link_order, data,
2043 relocateable,
2044 symbols);
2045
2046 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
2047
2048 memcpy (data, elf_section_data (input_section)->this_hdr.contents,
2049 input_section->_raw_size);
2050
2051 if ((input_section->flags & SEC_RELOC) != 0
2052 && input_section->reloc_count > 0)
2053 {
2054 Elf_Internal_Sym *isymp;
2055 asection **secpp;
2056 Elf32_External_Sym *esym, *esymend;
2057
2058 if (symtab_hdr->contents != NULL)
2059 external_syms = (Elf32_External_Sym *) symtab_hdr->contents;
2060 else
2061 {
2062 external_syms = ((Elf32_External_Sym *)
2063 bfd_malloc (symtab_hdr->sh_info
2064 * sizeof (Elf32_External_Sym)));
2065 if (external_syms == NULL && symtab_hdr->sh_info > 0)
2066 goto error_return;
2067 if (bfd_seek (input_bfd, symtab_hdr->sh_offset, SEEK_SET) != 0
2068 || (bfd_read (external_syms, sizeof (Elf32_External_Sym),
2069 symtab_hdr->sh_info, input_bfd)
2070 != (symtab_hdr->sh_info * sizeof (Elf32_External_Sym))))
2071 goto error_return;
2072 }
2073
2074 internal_relocs = (_bfd_elf32_link_read_relocs
2075 (input_bfd, input_section, (PTR) NULL,
2076 (Elf_Internal_Rela *) NULL, false));
2077 if (internal_relocs == NULL)
2078 goto error_return;
2079
2080 internal_syms = ((Elf_Internal_Sym *)
2081 bfd_malloc (symtab_hdr->sh_info
2082 * sizeof (Elf_Internal_Sym)));
2083 if (internal_syms == NULL && symtab_hdr->sh_info > 0)
2084 goto error_return;
2085
2086 sections = (asection **) bfd_malloc (symtab_hdr->sh_info
2087 * sizeof (asection *));
2088 if (sections == NULL && symtab_hdr->sh_info > 0)
2089 goto error_return;
2090
2091 isymp = internal_syms;
2092 secpp = sections;
2093 esym = external_syms;
2094 esymend = esym + symtab_hdr->sh_info;
2095 for (; esym < esymend; ++esym, ++isymp, ++secpp)
2096 {
2097 asection *isec;
2098
2099 bfd_elf32_swap_symbol_in (input_bfd, esym, isymp);
2100
2101 if (isymp->st_shndx == SHN_UNDEF)
2102 isec = bfd_und_section_ptr;
2103 else if (isymp->st_shndx > 0 && isymp->st_shndx < SHN_LORESERVE)
2104 isec = bfd_section_from_elf_index (input_bfd, isymp->st_shndx);
2105 else if (isymp->st_shndx == SHN_ABS)
2106 isec = bfd_abs_section_ptr;
2107 else if (isymp->st_shndx == SHN_COMMON)
2108 isec = bfd_com_section_ptr;
2109 else
2110 {
2111 /* Who knows? */
2112 isec = NULL;
2113 }
2114
2115 *secpp = isec;
2116 }
2117
2118 if (! sh_elf_relocate_section (output_bfd, link_info, input_bfd,
2119 input_section, data, internal_relocs,
2120 internal_syms, sections))
2121 goto error_return;
2122
2123 if (sections != NULL)
2124 free (sections);
2125 sections = NULL;
2126 if (internal_syms != NULL)
2127 free (internal_syms);
2128 internal_syms = NULL;
2129 if (external_syms != NULL && symtab_hdr->contents == NULL)
2130 free (external_syms);
2131 external_syms = NULL;
2132 if (internal_relocs != elf_section_data (input_section)->relocs)
2133 free (internal_relocs);
2134 internal_relocs = NULL;
2135 }
2136
2137 return data;
2138
2139 error_return:
2140 if (internal_relocs != NULL
2141 && internal_relocs != elf_section_data (input_section)->relocs)
2142 free (internal_relocs);
2143 if (external_syms != NULL && symtab_hdr->contents == NULL)
2144 free (external_syms);
2145 if (internal_syms != NULL)
2146 free (internal_syms);
2147 if (sections != NULL)
2148 free (sections);
2149 return NULL;
2150}
2151static asection *
2152sh_elf_gc_mark_hook (abfd, info, rel, h, sym)
2153 bfd *abfd;
5f771d47 2154 struct bfd_link_info *info ATTRIBUTE_UNUSED;
252b5132
RH
2155 Elf_Internal_Rela *rel;
2156 struct elf_link_hash_entry *h;
2157 Elf_Internal_Sym *sym;
2158{
2159 if (h != NULL)
2160 {
2161 switch (ELF32_R_TYPE (rel->r_info))
2162 {
2163 case R_SH_GNU_VTINHERIT:
2164 case R_SH_GNU_VTENTRY:
2165 break;
2166
2167 default:
2168 switch (h->root.type)
2169 {
2170 case bfd_link_hash_defined:
2171 case bfd_link_hash_defweak:
2172 return h->root.u.def.section;
2173
2174 case bfd_link_hash_common:
2175 return h->root.u.c.p->section;
e049a0de
ILT
2176
2177 default:
2178 break;
252b5132
RH
2179 }
2180 }
2181 }
2182 else
2183 {
2184 if (!(elf_bad_symtab (abfd)
2185 && ELF_ST_BIND (sym->st_info) != STB_LOCAL)
2186 && ! ((sym->st_shndx <= 0 || sym->st_shndx >= SHN_LORESERVE)
2187 && sym->st_shndx != SHN_COMMON))
2188 {
2189 return bfd_section_from_elf_index (abfd, sym->st_shndx);
2190 }
2191 }
2192 return NULL;
2193}
2194
2195static boolean
2196sh_elf_gc_sweep_hook (abfd, info, sec, relocs)
5f771d47
ILT
2197 bfd *abfd ATTRIBUTE_UNUSED;
2198 struct bfd_link_info *info ATTRIBUTE_UNUSED;
2199 asection *sec ATTRIBUTE_UNUSED;
2200 const Elf_Internal_Rela *relocs ATTRIBUTE_UNUSED;
252b5132
RH
2201{
2202 /* we don't use got and plt entries for sh. */
2203 return true;
2204}
2205
2206/* Look through the relocs for a section during the first phase.
2207 Since we don't do .gots or .plts, we just need to consider the
2208 virtual table relocs for gc. */
2209
2210static boolean
2211sh_elf_check_relocs (abfd, info, sec, relocs)
2212 bfd *abfd;
2213 struct bfd_link_info *info;
2214 asection *sec;
2215 const Elf_Internal_Rela *relocs;
2216{
2217 Elf_Internal_Shdr *symtab_hdr;
2218 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
2219 const Elf_Internal_Rela *rel;
2220 const Elf_Internal_Rela *rel_end;
2221
2222 if (info->relocateable)
2223 return true;
2224
2225 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2226 sym_hashes = elf_sym_hashes (abfd);
2227 sym_hashes_end = sym_hashes + symtab_hdr->sh_size/sizeof(Elf32_External_Sym);
2228 if (!elf_bad_symtab (abfd))
2229 sym_hashes_end -= symtab_hdr->sh_info;
2230
2231 rel_end = relocs + sec->reloc_count;
2232 for (rel = relocs; rel < rel_end; rel++)
2233 {
2234 struct elf_link_hash_entry *h;
2235 unsigned long r_symndx;
2236
2237 r_symndx = ELF32_R_SYM (rel->r_info);
2238 if (r_symndx < symtab_hdr->sh_info)
2239 h = NULL;
2240 else
2241 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
2242
2243 switch (ELF32_R_TYPE (rel->r_info))
2244 {
2245 /* This relocation describes the C++ object vtable hierarchy.
2246 Reconstruct it for later use during GC. */
2247 case R_SH_GNU_VTINHERIT:
2248 if (!_bfd_elf32_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
2249 return false;
2250 break;
2251
2252 /* This relocation describes which C++ vtable entries are actually
2253 used. Record for later use during GC. */
2254 case R_SH_GNU_VTENTRY:
2255 if (!_bfd_elf32_gc_record_vtentry (abfd, sec, h, rel->r_addend))
2256 return false;
2257 break;
2258 }
2259 }
2260
2261 return true;
2262}
2263
1630fd2b 2264static boolean
d4845d57
JR
2265sh_elf_set_mach_from_flags (abfd)
2266 bfd * abfd;
2267{
2268 flagword flags = elf_elfheader (abfd)->e_flags;
2269
86033394 2270 switch (flags & EF_SH_MACH_MASK)
d4845d57
JR
2271 {
2272 case EF_SH1:
2273 bfd_default_set_arch_mach (abfd, bfd_arch_sh, bfd_mach_sh);
2274 break;
2275 case EF_SH2:
2276 bfd_default_set_arch_mach (abfd, bfd_arch_sh, bfd_mach_sh2);
2277 break;
2278 case EF_SH_DSP:
2279 bfd_default_set_arch_mach (abfd, bfd_arch_sh, bfd_mach_sh_dsp);
2280 break;
2281 case EF_SH3:
2282 bfd_default_set_arch_mach (abfd, bfd_arch_sh, bfd_mach_sh3);
2283 break;
2284 case EF_SH3_DSP:
2285 bfd_default_set_arch_mach (abfd, bfd_arch_sh, bfd_mach_sh3_dsp);
2286 break;
2287 case EF_SH3E:
2288 bfd_default_set_arch_mach (abfd, bfd_arch_sh, bfd_mach_sh3e);
2289 break;
2290 case EF_SH_UNKNOWN:
2291 case EF_SH4:
2292 bfd_default_set_arch_mach (abfd, bfd_arch_sh, bfd_mach_sh4);
2293 break;
2294 default:
2295 return false;
2296 }
2297 return true;
2298}
2299
2300/* Function to keep SH specific file flags. */
2301static boolean
2302sh_elf_set_private_flags (abfd, flags)
2303 bfd * abfd;
2304 flagword flags;
2305{
2306 BFD_ASSERT (! elf_flags_init (abfd)
2307 || elf_elfheader (abfd)->e_flags == flags);
2308
2309 elf_elfheader (abfd)->e_flags = flags;
2310 elf_flags_init (abfd) = true;
2311 return sh_elf_set_mach_from_flags (abfd);
2312}
2313
2314/* Copy backend specific data from one object module to another */
2315static boolean
2316sh_elf_copy_private_data (ibfd, obfd)
2317 bfd * ibfd;
2318 bfd * obfd;
2319{
2320 if ( bfd_get_flavour (ibfd) != bfd_target_elf_flavour
2321 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
2322 return true;
2323
2324 return sh_elf_set_private_flags (obfd, elf_elfheader (ibfd)->e_flags);
2325}
2326
2327/* This routine checks for linking big and little endian objects
2328 together, and for linking sh-dsp with sh3e / sh4 objects. */
2329
2330static boolean
2331sh_elf_merge_private_data (ibfd, obfd)
2332 bfd *ibfd;
2333 bfd *obfd;
2334{
2335 flagword old_flags, new_flags;
2336
2337 if (_bfd_generic_verify_endian_match (ibfd, obfd) == false)
2338 return false;
2339
2340 if ( bfd_get_flavour (ibfd) != bfd_target_elf_flavour
2341 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
2342 return true;
2343
2344 if (! elf_flags_init (obfd))
2345 {
2346 elf_flags_init (obfd) = true;
2347 elf_elfheader (obfd)->e_flags = 0;
2348 }
2349 old_flags = elf_elfheader (obfd)->e_flags;
2350 new_flags = elf_elfheader (ibfd)->e_flags;
2351 if ((EF_SH_HAS_DSP (old_flags) && EF_SH_HAS_FP (new_flags))
2352 || (EF_SH_HAS_DSP (new_flags) && EF_SH_HAS_FP (old_flags)))
2353 {
2354 (*_bfd_error_handler)
2355 ("%s: uses %s instructions while previous modules use %s instructions",
2356 bfd_get_filename (ibfd),
2357 EF_SH_HAS_DSP (new_flags) ? "dsp" : "floating point",
2358 EF_SH_HAS_DSP (new_flags) ? "floating point" : "dsp");
2359 bfd_set_error (bfd_error_bad_value);
2360 return false;
2361 }
2362 elf_elfheader (obfd)->e_flags = EF_SH_MERGE_MACH (old_flags, new_flags);
2363
2364 return sh_elf_set_mach_from_flags (obfd);
2365}
2366
252b5132
RH
2367#define TARGET_BIG_SYM bfd_elf32_sh_vec
2368#define TARGET_BIG_NAME "elf32-sh"
2369#define TARGET_LITTLE_SYM bfd_elf32_shl_vec
2370#define TARGET_LITTLE_NAME "elf32-shl"
2371#define ELF_ARCH bfd_arch_sh
2372#define ELF_MACHINE_CODE EM_SH
2373#define ELF_MAXPAGESIZE 0x1
2374
2375#define elf_symbol_leading_char '_'
2376
2377#define bfd_elf32_bfd_reloc_type_lookup sh_elf_reloc_type_lookup
2378#define elf_info_to_howto sh_elf_info_to_howto
2379#define bfd_elf32_bfd_relax_section sh_elf_relax_section
2380#define elf_backend_relocate_section sh_elf_relocate_section
2381#define bfd_elf32_bfd_get_relocated_section_contents \
2382 sh_elf_get_relocated_section_contents
d4845d57
JR
2383#define elf_backend_object_p sh_elf_set_mach_from_flags
2384#define bfd_elf32_bfd_set_private_bfd_flags \
2385 sh_elf_set_private_flags
2386#define bfd_elf32_bfd_copy_private_bfd_data \
2387 sh_elf_copy_private_data
875f7f69 2388#define bfd_elf32_bfd_merge_private_bfd_data \
d4845d57 2389 sh_elf_merge_private_data
252b5132
RH
2390
2391#define elf_backend_gc_mark_hook sh_elf_gc_mark_hook
2392#define elf_backend_gc_sweep_hook sh_elf_gc_sweep_hook
2393#define elf_backend_check_relocs sh_elf_check_relocs
2394
2395#define elf_backend_can_gc_sections 1
2396#include "elf32-target.h"