Bfd support for generating IA-64 EFI binaries.
[binutils.git] / bfd / elf32-sh.c
blob6913fa47f1c7d56f4ceb49b4ba43ecf7192572fe
1 /* Hitachi SH specific support for 32-bit ELF
2 Copyright 1996, 97, 98, 1999, 2000 Free Software Foundation, Inc.
3 Contributed by Ian Lance Taylor, Cygnus Support.
5 This file is part of BFD, the Binary File Descriptor library.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
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"
28 static bfd_reloc_status_type sh_elf_reloc
29 PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
30 static bfd_reloc_status_type sh_elf_ignore_reloc
31 PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
32 static reloc_howto_type *sh_elf_reloc_type_lookup
33 PARAMS ((bfd *, bfd_reloc_code_real_type));
34 static void sh_elf_info_to_howto
35 PARAMS ((bfd *, arelent *, Elf_Internal_Rela *));
36 static boolean sh_elf_set_private_flags
37 PARAMS ((bfd *, flagword));
38 static boolean sh_elf_copy_private_data
39 PARAMS ((bfd *, bfd *));
40 static boolean sh_elf_merge_private_data
41 PARAMS ((bfd *, bfd *));
42 boolean sh_elf_set_mach_from_flags
43 PARAMS ((bfd *));
44 static boolean sh_elf_relax_section
45 PARAMS ((bfd *, asection *, struct bfd_link_info *, boolean *));
46 static boolean sh_elf_relax_delete_bytes
47 PARAMS ((bfd *, asection *, bfd_vma, int));
48 static boolean sh_elf_align_loads
49 PARAMS ((bfd *, asection *, Elf_Internal_Rela *, bfd_byte *, boolean *));
50 static boolean sh_elf_swap_insns
51 PARAMS ((bfd *, asection *, PTR, bfd_byte *, bfd_vma));
52 static boolean sh_elf_relocate_section
53 PARAMS ((bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
54 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **));
55 static bfd_byte *sh_elf_get_relocated_section_contents
56 PARAMS ((bfd *, struct bfd_link_info *, struct bfd_link_order *,
57 bfd_byte *, boolean, asymbol **));
59 static reloc_howto_type sh_elf_howto_table[] =
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 */
69 sh_elf_ignore_reloc, /* special_function */
70 "R_SH_NONE", /* name */
71 false, /* partial_inplace */
72 0, /* src_mask */
73 0, /* dst_mask */
74 false), /* pcrel_offset */
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 */
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 */
100 sh_elf_ignore_reloc, /* special_function */
101 "R_SH_REL32", /* name */
102 false, /* partial_inplace */
103 0, /* src_mask */
104 0xffffffff, /* dst_mask */
105 true), /* pcrel_offset */
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 */
115 sh_elf_ignore_reloc, /* special_function */
116 "R_SH_DIR8WPN", /* name */
117 true, /* partial_inplace */
118 0xff, /* src_mask */
119 0xff, /* dst_mask */
120 true), /* pcrel_offset */
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 */
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 */
145 sh_elf_ignore_reloc, /* special_function */
146 "R_SH_DIR8WPL", /* name */
147 true, /* partial_inplace */
148 0xff, /* src_mask */
149 0xff, /* dst_mask */
150 true), /* pcrel_offset */
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 */
160 sh_elf_ignore_reloc, /* special_function */
161 "R_SH_DIR8WPZ", /* name */
162 true, /* partial_inplace */
163 0xff, /* src_mask */
164 0xff, /* dst_mask */
165 true), /* pcrel_offset */
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 */
177 sh_elf_ignore_reloc, /* special_function */
178 "R_SH_DIR8BP", /* name */
179 false, /* partial_inplace */
180 0, /* src_mask */
181 0xff, /* dst_mask */
182 true), /* pcrel_offset */
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 */
194 sh_elf_ignore_reloc, /* special_function */
195 "R_SH_DIR8W", /* name */
196 false, /* partial_inplace */
197 0, /* src_mask */
198 0xff, /* dst_mask */
199 true), /* pcrel_offset */
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 */
211 sh_elf_ignore_reloc, /* special_function */
212 "R_SH_DIR8L", /* name */
213 false, /* partial_inplace */
214 0, /* src_mask */
215 0xff, /* dst_mask */
216 true), /* pcrel_offset */
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),
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. */
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 */
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 */
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 */
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 */
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 */
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 */
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 */
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 */
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 */
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 */
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 */
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 */
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 */
455 static bfd_reloc_status_type
456 sh_elf_reloc_loop (r_type, input_bfd, input_section, contents, addr,
457 symbol_section, start, end)
458 int r_type;
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;
466 static bfd_vma last_addr;
467 asection *last_symbol_section;
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;
474 /* Sanity check the address. */
475 if (addr > input_section->_raw_size)
476 return bfd_reloc_outofrange;
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)
482 last_addr = addr;
483 last_symbol_section = symbol_section;
484 return bfd_reloc_ok;
486 if (last_addr != addr)
487 abort ();
488 last_addr = 0;
490 if (! symbol_section || last_symbol_section != symbol_section || end < start)
491 return bfd_reloc_outofrange;
493 /* Get the symbol_section contents. */
494 if (symbol_section != input_section)
496 if (elf_section_data (symbol_section)->this_hdr.contents != NULL)
497 contents = elf_section_data (symbol_section)->this_hdr.contents;
498 else
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))
508 free (contents);
509 return bfd_reloc_outofrange;
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;)
517 for (last_ptr = ptr, ptr -= 4; ptr >= start_ptr && IS_PPI (ptr);)
518 ptr -= 2;
519 ptr += 2;
520 diff = last_ptr - ptr >> 1;
521 cum_diff += diff & 1;
522 cum_diff += diff;
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)
529 start -= 4;
530 end = (ptr + cum_diff * 2) - contents;
532 else
534 bfd_vma start0 = start - 4;
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;
543 if (free_contents)
544 free (free_contents);
546 insn = bfd_get_16 (input_bfd, contents + addr);
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;
557 x = insn & ~0xff | x & 0xff;
558 bfd_put_16 (input_bfd, x, contents + addr);
560 return bfd_reloc_ok;
563 /* This function is used for normal relocs. This used to be like the COFF
564 function, and is almost certainly incorrect for other ELF targets. */
566 static bfd_reloc_status_type
567 sh_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;
575 char **error_message ATTRIBUTE_UNUSED;
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;
583 r_type = (enum elf_sh_reloc_type) reloc_entry->howto->type;
585 if (output_bfd != NULL)
587 /* Partial linking--do nothing. */
588 reloc_entry->address += input_section->output_offset;
589 return bfd_reloc_ok;
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. */
594 if (r_type == R_SH_IND12W && (symbol_in->flags & BSF_LOCAL) != 0)
595 return bfd_reloc_ok;
597 if (symbol_in != NULL
598 && bfd_is_und_section (symbol_in->section))
599 return bfd_reloc_undefined;
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);
608 switch (r_type)
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;
635 return bfd_reloc_ok;
638 /* This function is used for relocs which are only used for relaxing,
639 which the linker should otherwise ignore. */
641 static bfd_reloc_status_type
642 sh_elf_ignore_reloc (abfd, reloc_entry, symbol, data, input_section,
643 output_bfd, error_message)
644 bfd *abfd ATTRIBUTE_UNUSED;
645 arelent *reloc_entry;
646 asymbol *symbol ATTRIBUTE_UNUSED;
647 PTR data ATTRIBUTE_UNUSED;
648 asection *input_section;
649 bfd *output_bfd;
650 char **error_message ATTRIBUTE_UNUSED;
652 if (output_bfd != NULL)
653 reloc_entry->address += input_section->output_offset;
654 return bfd_reloc_ok;
657 /* This structure is used to map BFD reloc codes to SH ELF relocs. */
659 struct elf_reloc_map
661 bfd_reloc_code_real_type bfd_reloc_val;
662 unsigned char elf_reloc_val;
665 /* An array mapping BFD reloc codes to SH ELF relocs. */
667 static const struct elf_reloc_map sh_reloc_map[] =
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 },
688 { BFD_RELOC_SH_LOOP_START, R_SH_LOOP_START },
689 { BFD_RELOC_SH_LOOP_END, R_SH_LOOP_END },
692 /* Given a BFD reloc code, return the howto structure for the
693 corresponding SH ELf reloc. */
695 static reloc_howto_type *
696 sh_elf_reloc_type_lookup (abfd, code)
697 bfd *abfd ATTRIBUTE_UNUSED;
698 bfd_reloc_code_real_type code;
700 unsigned int i;
702 for (i = 0; i < sizeof (sh_reloc_map) / sizeof (struct elf_reloc_map); i++)
704 if (sh_reloc_map[i].bfd_reloc_val == code)
705 return &sh_elf_howto_table[(int) sh_reloc_map[i].elf_reloc_val];
708 return NULL;
711 /* Given an ELF reloc, fill in the howto field of a relent. */
713 static void
714 sh_elf_info_to_howto (abfd, cache_ptr, dst)
715 bfd *abfd ATTRIBUTE_UNUSED;
716 arelent *cache_ptr;
717 Elf_Internal_Rela *dst;
719 unsigned int r;
721 r = ELF32_R_TYPE (dst->r_info);
723 BFD_ASSERT (r < (unsigned int) R_SH_max);
724 BFD_ASSERT (r < R_SH_FIRST_INVALID_RELOC || r > R_SH_LAST_INVALID_RELOC);
726 cache_ptr->howto = &sh_elf_howto_table[r];
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. */
739 static boolean
740 sh_elf_relax_section (abfd, sec, link_info, again)
741 bfd *abfd;
742 asection *sec;
743 struct bfd_link_info *link_info;
744 boolean *again;
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;
756 *again = false;
758 if (link_info->relocateable
759 || (sec->flags & SEC_RELOC) == 0
760 || sec->reloc_count == 0)
761 return true;
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;
768 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
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;
778 have_code = false;
780 irelend = internal_relocs + sec->reloc_count;
781 for (irel = internal_relocs; irel < irelend; irel++)
783 bfd_vma laddr, paddr, symval;
784 unsigned short insn;
785 Elf_Internal_Rela *irelfn, *irelscan, *irelcount;
786 bfd_signed_vma foff;
788 if (ELF32_R_TYPE (irel->r_info) == (int) R_SH_CODE)
789 have_code = true;
791 if (ELF32_R_TYPE (irel->r_info) != (int) R_SH_USES)
792 continue;
794 /* Get the section contents. */
795 if (contents == NULL)
797 if (elf_section_data (sec)->this_hdr.contents != NULL)
798 contents = elf_section_data (sec)->this_hdr.contents;
799 else
801 contents = (bfd_byte *) bfd_malloc (sec->_raw_size);
802 if (contents == NULL)
803 goto error_return;
804 free_contents = contents;
806 if (! bfd_get_section_contents (abfd, sec, contents,
807 (file_ptr) 0, sec->_raw_size))
808 goto error_return;
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)
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;
824 insn = bfd_get_16 (abfd, contents + laddr);
826 /* If the instruction is not mov.l NN,rN, we don't know what to
827 do. */
828 if ((insn & 0xf000) != 0xd000)
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;
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)
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;
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)
862 ((*_bfd_error_handler)
863 (_("%s: 0x%lx: warning: could not find expected reloc"),
864 bfd_get_filename (abfd), (unsigned long) paddr));
865 continue;
868 /* Read this BFD's symbols if we haven't done so already. */
869 if (extsyms == NULL)
871 if (symtab_hdr->contents != NULL)
872 extsyms = (Elf32_External_Sym *) symtab_hdr->contents;
873 else
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;
887 /* Get the value of the symbol referred to by the reloc. */
888 if (ELF32_R_SYM (irelfn->r_info) < symtab_hdr->sh_info)
890 Elf_Internal_Sym isym;
892 /* A local symbol. */
893 bfd_elf32_swap_symbol_in (abfd,
894 extsyms + ELF32_R_SYM (irelfn->r_info),
895 &isym);
897 if (isym.st_shndx != _bfd_elf_section_from_bfd_section (abfd, sec))
899 ((*_bfd_error_handler)
900 (_("%s: 0x%lx: warning: symbol in unexpected section"),
901 bfd_get_filename (abfd), (unsigned long) paddr));
902 continue;
905 symval = (isym.st_value
906 + sec->output_section->vma
907 + sec->output_offset);
909 else
911 unsigned long indx;
912 struct elf_link_hash_entry *h;
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)
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;
926 symval = (h->root.u.def.value
927 + h->root.u.def.section->output_section->vma
928 + h->root.u.def.section->output_offset);
931 symval += bfd_get_32 (abfd, contents + paddr);
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)
941 /* After all that work, we can't shorten this function call. */
942 continue;
945 /* Shorten the function call. */
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. */
955 elf_section_data (sec)->relocs = internal_relocs;
956 free_relocs = NULL;
958 elf_section_data (sec)->this_hdr.contents = contents;
959 free_contents = NULL;
961 symtab_hdr->contents = (bfd_byte *) extsyms;
962 free_extsyms = NULL;
964 /* Replace the jsr with a bsr. */
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)
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);
978 else
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);
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)
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;
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;
1009 /* Delete the register load. */
1010 if (! sh_elf_relax_delete_bytes (abfd, sec, laddr, 2))
1011 goto error_return;
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;
1018 /* Now check whether we got a COUNT reloc. */
1019 if (irelcount >= irelend)
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;
1027 /* The number of uses is stored in the r_addend field. We've
1028 just deleted one. */
1029 if (irelcount->r_addend == 0)
1031 ((*_bfd_error_handler) (_("%s: 0x%lx: warning: bad count"),
1032 bfd_get_filename (abfd),
1033 (unsigned long) paddr));
1034 continue;
1037 --irelcount->r_addend;
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)
1044 if (! sh_elf_relax_delete_bytes (abfd, sec, irelfn->r_offset, 4))
1045 goto error_return;
1048 /* We've done all we can with that function call. */
1051 /* Look for load and store instructions that we can align on four
1052 byte boundaries. */
1053 if (have_code)
1055 boolean swapped;
1057 /* Get the section contents. */
1058 if (contents == NULL)
1060 if (elf_section_data (sec)->this_hdr.contents != NULL)
1061 contents = elf_section_data (sec)->this_hdr.contents;
1062 else
1064 contents = (bfd_byte *) bfd_malloc (sec->_raw_size);
1065 if (contents == NULL)
1066 goto error_return;
1067 free_contents = contents;
1069 if (! bfd_get_section_contents (abfd, sec, contents,
1070 (file_ptr) 0, sec->_raw_size))
1071 goto error_return;
1075 if (! sh_elf_align_loads (abfd, sec, internal_relocs, contents,
1076 &swapped))
1077 goto error_return;
1079 if (swapped)
1081 elf_section_data (sec)->relocs = internal_relocs;
1082 free_relocs = NULL;
1084 elf_section_data (sec)->this_hdr.contents = contents;
1085 free_contents = NULL;
1087 symtab_hdr->contents = (bfd_byte *) extsyms;
1088 free_extsyms = NULL;
1092 if (free_relocs != NULL)
1094 free (free_relocs);
1095 free_relocs = NULL;
1098 if (free_contents != NULL)
1100 if (! link_info->keep_memory)
1101 free (free_contents);
1102 else
1104 /* Cache the section contents for elf_link_input_bfd. */
1105 elf_section_data (sec)->this_hdr.contents = contents;
1107 free_contents = NULL;
1110 if (free_extsyms != NULL)
1112 if (! link_info->keep_memory)
1113 free (free_extsyms);
1114 else
1116 /* Cache the symbols for elf_link_input_bfd. */
1117 symtab_hdr->contents = extsyms;
1119 free_extsyms = NULL;
1122 return true;
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;
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. */
1138 static boolean
1139 sh_elf_relax_delete_bytes (abfd, sec, addr, count)
1140 bfd *abfd;
1141 asection *sec;
1142 bfd_vma addr;
1143 int count;
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;
1156 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1157 extsyms = (Elf32_External_Sym *) symtab_hdr->contents;
1159 shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
1161 contents = elf_section_data (sec)->this_hdr.contents;
1163 /* The deletion must stop at the next ALIGN reloc for an aligment
1164 power larger than the number of bytes we are deleting. */
1166 irelalign = NULL;
1167 toaddr = sec->_cooked_size;
1169 irel = elf_section_data (sec)->relocs;
1170 irelend = irel + sec->reloc_count;
1171 for (; irel < irelend; irel++)
1173 if (ELF32_R_TYPE (irel->r_info) == (int) R_SH_ALIGN
1174 && irel->r_offset > addr
1175 && count < (1 << irel->r_addend))
1177 irelalign = irel;
1178 toaddr = irel->r_offset;
1179 break;
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
1189 int i;
1191 #define NOP_OPCODE (0x0009)
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);
1198 /* Adjust all the relocs. */
1199 for (irel = elf_section_data (sec)->relocs; irel < irelend; irel++)
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;
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;
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);
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))
1233 default:
1234 break;
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;
1245 switch ((enum elf_sh_reloc_type) ELF32_R_TYPE (irel->r_info))
1247 default:
1248 start = stop = addr;
1249 break;
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)
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))
1265 bfd_vma val;
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);
1273 start = stop = addr;
1274 break;
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;
1283 case R_SH_IND12W:
1284 if (ELF32_R_SYM (irel->r_info) >= symtab_hdr->sh_info)
1285 start = stop = addr;
1286 else
1288 off = insn & 0xfff;
1289 if (off & 0x800)
1290 off -= 0x1000;
1291 stop = (bfd_vma) ((bfd_signed_vma) start + 4 + off * 2);
1293 break;
1295 case R_SH_DIR8WPZ:
1296 off = insn & 0xff;
1297 stop = start + 4 + off * 2;
1298 break;
1300 case R_SH_DIR8WPL:
1301 off = insn & 0xff;
1302 stop = (start &~ (bfd_vma) 3) + 4 + off * 4;
1303 break;
1305 case R_SH_SWITCH8:
1306 case R_SH_SWITCH16:
1307 case R_SH_SWITCH32:
1308 /* These relocs types represent
1309 .word L2-L1
1310 The r_addend field holds the difference between the reloc
1311 address and L1. That is the start of the reloc, and
1312 adding in the contents gives us the top. We must adjust
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. */
1317 stop = irel->r_offset;
1318 start = (bfd_vma) ((bfd_signed_vma) stop - (long) irel->r_addend);
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;
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);
1337 break;
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;
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;
1358 if (adjust != 0)
1360 oinsn = insn;
1361 overflow = false;
1362 switch ((enum elf_sh_reloc_type) ELF32_R_TYPE (irel->r_info))
1364 default:
1365 abort ();
1366 break;
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;
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;
1383 case R_SH_DIR8WPL:
1384 BFD_ASSERT (adjust == count || count >= 4);
1385 if (count >= 4)
1386 insn += adjust / 4;
1387 else
1389 if ((irel->r_offset & 3) == 0)
1390 ++insn;
1392 if ((oinsn & 0xff00) != (insn & 0xff00))
1393 overflow = true;
1394 bfd_put_16 (abfd, insn, contents + nraddr);
1395 break;
1397 case R_SH_SWITCH16:
1398 voff += adjust;
1399 if (voff < - 0x8000 || voff >= 0x8000)
1400 overflow = true;
1401 bfd_put_signed_16 (abfd, voff, contents + nraddr);
1402 break;
1404 case R_SH_SWITCH32:
1405 voff += adjust;
1406 bfd_put_signed_32 (abfd, voff, contents + nraddr);
1407 break;
1409 case R_SH_USES:
1410 irel->r_addend += adjust;
1411 break;
1414 if (overflow)
1416 ((*_bfd_error_handler)
1417 (_("%s: 0x%lx: fatal: reloc overflow while relaxing"),
1418 bfd_get_filename (abfd), (unsigned long) irel->r_offset));
1419 bfd_set_error (bfd_error_bad_value);
1420 return false;
1424 irel->r_offset = nraddr;
1427 /* Look through all the other sections. If there contain any IMM32
1428 relocs against internal symbols which we are not going to adjust
1429 below, we may need to adjust the addends. */
1430 for (o = abfd->sections; o != NULL; o = o->next)
1432 Elf_Internal_Rela *internal_relocs;
1433 Elf_Internal_Rela *irelscan, *irelscanend;
1434 bfd_byte *ocontents;
1436 if (o == sec
1437 || (o->flags & SEC_RELOC) == 0
1438 || o->reloc_count == 0)
1439 continue;
1441 /* We always cache the relocs. Perhaps, if info->keep_memory is
1442 false, we should free them, if we are permitted to, when we
1443 leave sh_coff_relax_section. */
1444 internal_relocs = (_bfd_elf32_link_read_relocs
1445 (abfd, o, (PTR) NULL, (Elf_Internal_Rela *) NULL,
1446 true));
1447 if (internal_relocs == NULL)
1448 return false;
1450 ocontents = NULL;
1451 irelscanend = internal_relocs + o->reloc_count;
1452 for (irelscan = internal_relocs; irelscan < irelscanend; irelscan++)
1454 Elf_Internal_Sym sym;
1456 /* Dwarf line numbers use R_SH_SWITCH32 relocs. */
1457 if (ELF32_R_TYPE (irelscan->r_info) == (int) R_SH_SWITCH32)
1459 bfd_vma start, stop;
1460 bfd_signed_vma voff;
1462 if (ocontents == NULL)
1464 if (elf_section_data (o)->this_hdr.contents != NULL)
1465 ocontents = elf_section_data (o)->this_hdr.contents;
1466 else
1468 /* We always cache the section contents.
1469 Perhaps, if info->keep_memory is false, we
1470 should free them, if we are permitted to,
1471 when we leave sh_coff_relax_section. */
1472 ocontents = (bfd_byte *) bfd_malloc (o->_raw_size);
1473 if (ocontents == NULL)
1474 return false;
1475 if (! bfd_get_section_contents (abfd, o, ocontents,
1476 (file_ptr) 0,
1477 o->_raw_size))
1478 return false;
1479 elf_section_data (o)->this_hdr.contents = ocontents;
1483 stop = irelscan->r_offset;
1484 start
1485 = (bfd_vma) ((bfd_signed_vma) stop - (long) irelscan->r_addend);
1487 /* STOP is in a different section, so it won't change. */
1488 if (start > addr && start < toaddr)
1489 irelscan->r_addend += count;
1491 voff = bfd_get_signed_32 (abfd, ocontents + irelscan->r_offset);
1492 stop = (bfd_vma) ((bfd_signed_vma) start + voff);
1494 if (start > addr
1495 && start < toaddr
1496 && (stop <= addr || stop >= toaddr))
1497 bfd_put_signed_32 (abfd, voff + count,
1498 ocontents + irelscan->r_offset);
1499 else if (stop > addr
1500 && stop < toaddr
1501 && (start <= addr || start >= toaddr))
1502 bfd_put_signed_32 (abfd, voff - count,
1503 ocontents + irelscan->r_offset);
1506 if (ELF32_R_TYPE (irelscan->r_info) != (int) R_SH_DIR32)
1507 continue;
1509 if (ELF32_R_SYM (irelscan->r_info) >= symtab_hdr->sh_info)
1510 continue;
1512 bfd_elf32_swap_symbol_in (abfd,
1513 extsyms + ELF32_R_SYM (irelscan->r_info),
1514 &sym);
1516 if (sym.st_shndx == shndx
1517 && (sym.st_value <= addr
1518 || sym.st_value >= toaddr))
1520 bfd_vma val;
1522 if (ocontents == NULL)
1524 if (elf_section_data (o)->this_hdr.contents != NULL)
1525 ocontents = elf_section_data (o)->this_hdr.contents;
1526 else
1528 /* We always cache the section contents.
1529 Perhaps, if info->keep_memory is false, we
1530 should free them, if we are permitted to,
1531 when we leave sh_coff_relax_section. */
1532 ocontents = (bfd_byte *) bfd_malloc (o->_raw_size);
1533 if (ocontents == NULL)
1534 return false;
1535 if (! bfd_get_section_contents (abfd, o, ocontents,
1536 (file_ptr) 0,
1537 o->_raw_size))
1538 return false;
1539 elf_section_data (o)->this_hdr.contents = ocontents;
1543 val = bfd_get_32 (abfd, ocontents + irelscan->r_offset);
1544 val += sym.st_value;
1545 if (val > addr && val < toaddr)
1546 bfd_put_32 (abfd, val - count,
1547 ocontents + irelscan->r_offset);
1552 /* Adjust the local symbols defined in this section. */
1553 esym = extsyms;
1554 esymend = esym + symtab_hdr->sh_info;
1555 for (; esym < esymend; esym++)
1557 Elf_Internal_Sym isym;
1559 bfd_elf32_swap_symbol_in (abfd, esym, &isym);
1561 if (isym.st_shndx == shndx
1562 && isym.st_value > addr
1563 && isym.st_value < toaddr)
1565 isym.st_value -= count;
1566 bfd_elf32_swap_symbol_out (abfd, &isym, esym);
1570 /* Now adjust the global symbols defined in this section. */
1571 esym = extsyms + symtab_hdr->sh_info;
1572 esymend = extsyms + (symtab_hdr->sh_size / sizeof (Elf32_External_Sym));
1573 for (index = 0; esym < esymend; esym++, index++)
1575 Elf_Internal_Sym isym;
1577 bfd_elf32_swap_symbol_in (abfd, esym, &isym);
1578 sym_hash = elf_sym_hashes (abfd)[index];
1579 if (isym.st_shndx == shndx
1580 && ((sym_hash)->root.type == bfd_link_hash_defined
1581 || (sym_hash)->root.type == bfd_link_hash_defweak)
1582 && (sym_hash)->root.u.def.section == sec
1583 && (sym_hash)->root.u.def.value > addr
1584 && (sym_hash)->root.u.def.value < toaddr)
1586 (sym_hash)->root.u.def.value -= count;
1590 /* See if we can move the ALIGN reloc forward. We have adjusted
1591 r_offset for it already. */
1592 if (irelalign != NULL)
1594 bfd_vma alignto, alignaddr;
1596 alignto = BFD_ALIGN (toaddr, 1 << irelalign->r_addend);
1597 alignaddr = BFD_ALIGN (irelalign->r_offset,
1598 1 << irelalign->r_addend);
1599 if (alignto != alignaddr)
1601 /* Tail recursion. */
1602 return sh_elf_relax_delete_bytes (abfd, sec, alignaddr,
1603 alignto - alignaddr);
1607 return true;
1610 /* Look for loads and stores which we can align to four byte
1611 boundaries. This is like sh_align_loads in coff-sh.c. */
1613 static boolean
1614 sh_elf_align_loads (abfd, sec, internal_relocs, contents, pswapped)
1615 bfd *abfd;
1616 asection *sec;
1617 Elf_Internal_Rela *internal_relocs;
1618 bfd_byte *contents;
1619 boolean *pswapped;
1621 Elf_Internal_Rela *irel, *irelend;
1622 bfd_vma *labels = NULL;
1623 bfd_vma *label, *label_end;
1625 *pswapped = false;
1627 irelend = internal_relocs + sec->reloc_count;
1629 /* Get all the addresses with labels on them. */
1630 labels = (bfd_vma *) bfd_malloc (sec->reloc_count * sizeof (bfd_vma));
1631 if (labels == NULL)
1632 goto error_return;
1633 label_end = labels;
1634 for (irel = internal_relocs; irel < irelend; irel++)
1636 if (ELF32_R_TYPE (irel->r_info) == (int) R_SH_LABEL)
1638 *label_end = irel->r_offset;
1639 ++label_end;
1643 /* Note that the assembler currently always outputs relocs in
1644 address order. If that ever changes, this code will need to sort
1645 the label values and the relocs. */
1647 label = labels;
1649 for (irel = internal_relocs; irel < irelend; irel++)
1651 bfd_vma start, stop;
1653 if (ELF32_R_TYPE (irel->r_info) != (int) R_SH_CODE)
1654 continue;
1656 start = irel->r_offset;
1658 for (irel++; irel < irelend; irel++)
1659 if (ELF32_R_TYPE (irel->r_info) == (int) R_SH_DATA)
1660 break;
1661 if (irel < irelend)
1662 stop = irel->r_offset;
1663 else
1664 stop = sec->_cooked_size;
1666 if (! _bfd_sh_align_load_span (abfd, sec, contents, sh_elf_swap_insns,
1667 (PTR) internal_relocs, &label,
1668 label_end, start, stop, pswapped))
1669 goto error_return;
1672 free (labels);
1674 return true;
1676 error_return:
1677 if (labels != NULL)
1678 free (labels);
1679 return false;
1682 /* Swap two SH instructions. This is like sh_swap_insns in coff-sh.c. */
1684 static boolean
1685 sh_elf_swap_insns (abfd, sec, relocs, contents, addr)
1686 bfd *abfd;
1687 asection *sec;
1688 PTR relocs;
1689 bfd_byte *contents;
1690 bfd_vma addr;
1692 Elf_Internal_Rela *internal_relocs = (Elf_Internal_Rela *) relocs;
1693 unsigned short i1, i2;
1694 Elf_Internal_Rela *irel, *irelend;
1696 /* Swap the instructions themselves. */
1697 i1 = bfd_get_16 (abfd, contents + addr);
1698 i2 = bfd_get_16 (abfd, contents + addr + 2);
1699 bfd_put_16 (abfd, i2, contents + addr);
1700 bfd_put_16 (abfd, i1, contents + addr + 2);
1702 /* Adjust all reloc addresses. */
1703 irelend = internal_relocs + sec->reloc_count;
1704 for (irel = internal_relocs; irel < irelend; irel++)
1706 enum elf_sh_reloc_type type;
1707 int add;
1709 /* There are a few special types of relocs that we don't want to
1710 adjust. These relocs do not apply to the instruction itself,
1711 but are only associated with the address. */
1712 type = (enum elf_sh_reloc_type) ELF32_R_TYPE (irel->r_info);
1713 if (type == R_SH_ALIGN
1714 || type == R_SH_CODE
1715 || type == R_SH_DATA
1716 || type == R_SH_LABEL)
1717 continue;
1719 /* If an R_SH_USES reloc points to one of the addresses being
1720 swapped, we must adjust it. It would be incorrect to do this
1721 for a jump, though, since we want to execute both
1722 instructions after the jump. (We have avoided swapping
1723 around a label, so the jump will not wind up executing an
1724 instruction it shouldn't). */
1725 if (type == R_SH_USES)
1727 bfd_vma off;
1729 off = irel->r_offset + 4 + irel->r_addend;
1730 if (off == addr)
1731 irel->r_offset += 2;
1732 else if (off == addr + 2)
1733 irel->r_offset -= 2;
1736 if (irel->r_offset == addr)
1738 irel->r_offset += 2;
1739 add = -2;
1741 else if (irel->r_offset == addr + 2)
1743 irel->r_offset -= 2;
1744 add = 2;
1746 else
1747 add = 0;
1749 if (add != 0)
1751 bfd_byte *loc;
1752 unsigned short insn, oinsn;
1753 boolean overflow;
1755 loc = contents + irel->r_offset;
1756 overflow = false;
1757 switch (type)
1759 default:
1760 break;
1762 case R_SH_DIR8WPN:
1763 case R_SH_DIR8WPZ:
1764 insn = bfd_get_16 (abfd, loc);
1765 oinsn = insn;
1766 insn += add / 2;
1767 if ((oinsn & 0xff00) != (insn & 0xff00))
1768 overflow = true;
1769 bfd_put_16 (abfd, insn, loc);
1770 break;
1772 case R_SH_IND12W:
1773 insn = bfd_get_16 (abfd, loc);
1774 oinsn = insn;
1775 insn += add / 2;
1776 if ((oinsn & 0xf000) != (insn & 0xf000))
1777 overflow = true;
1778 bfd_put_16 (abfd, insn, loc);
1779 break;
1781 case R_SH_DIR8WPL:
1782 /* This reloc ignores the least significant 3 bits of
1783 the program counter before adding in the offset.
1784 This means that if ADDR is at an even address, the
1785 swap will not affect the offset. If ADDR is an at an
1786 odd address, then the instruction will be crossing a
1787 four byte boundary, and must be adjusted. */
1788 if ((addr & 3) != 0)
1790 insn = bfd_get_16 (abfd, loc);
1791 oinsn = insn;
1792 insn += add / 2;
1793 if ((oinsn & 0xff00) != (insn & 0xff00))
1794 overflow = true;
1795 bfd_put_16 (abfd, insn, loc);
1798 break;
1801 if (overflow)
1803 ((*_bfd_error_handler)
1804 (_("%s: 0x%lx: fatal: reloc overflow while relaxing"),
1805 bfd_get_filename (abfd), (unsigned long) irel->r_offset));
1806 bfd_set_error (bfd_error_bad_value);
1807 return false;
1812 return true;
1815 /* Relocate an SH ELF section. */
1817 static boolean
1818 sh_elf_relocate_section (output_bfd, info, input_bfd, input_section,
1819 contents, relocs, local_syms, local_sections)
1820 bfd *output_bfd ATTRIBUTE_UNUSED;
1821 struct bfd_link_info *info;
1822 bfd *input_bfd;
1823 asection *input_section;
1824 bfd_byte *contents;
1825 Elf_Internal_Rela *relocs;
1826 Elf_Internal_Sym *local_syms;
1827 asection **local_sections;
1829 Elf_Internal_Shdr *symtab_hdr;
1830 struct elf_link_hash_entry **sym_hashes;
1831 Elf_Internal_Rela *rel, *relend;
1833 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
1834 sym_hashes = elf_sym_hashes (input_bfd);
1836 rel = relocs;
1837 relend = relocs + input_section->reloc_count;
1838 for (; rel < relend; rel++)
1840 int r_type;
1841 reloc_howto_type *howto;
1842 unsigned long r_symndx;
1843 Elf_Internal_Sym *sym;
1844 asection *sec;
1845 struct elf_link_hash_entry *h;
1846 bfd_vma relocation;
1847 bfd_vma addend = (bfd_vma)0;
1848 bfd_reloc_status_type r;
1850 r_symndx = ELF32_R_SYM (rel->r_info);
1852 if (info->relocateable)
1854 /* This is a relocateable link. We don't have to change
1855 anything, unless the reloc is against a section symbol,
1856 in which case we have to adjust according to where the
1857 section symbol winds up in the output section. */
1858 if (r_symndx < symtab_hdr->sh_info)
1860 sym = local_syms + r_symndx;
1861 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
1863 sec = local_sections[r_symndx];
1864 rel->r_addend += sec->output_offset + sym->st_value;
1868 continue;
1871 r_type = ELF32_R_TYPE (rel->r_info);
1873 /* Many of the relocs are only used for relaxing, and are
1874 handled entirely by the relaxation code. */
1875 if (r_type > (int) R_SH_LAST_INVALID_RELOC
1876 && r_type < (int) R_SH_LOOP_START)
1877 continue;
1879 if (r_type < 0
1880 || (r_type >= (int) R_SH_FIRST_INVALID_RELOC
1881 && r_type <= (int) R_SH_LAST_INVALID_RELOC))
1883 bfd_set_error (bfd_error_bad_value);
1884 return false;
1887 /* FIXME: This is certainly incorrect. However, it is how the
1888 COFF linker works. */
1889 if (r_type != (int) R_SH_DIR32
1890 && r_type != (int) R_SH_IND12W
1891 && r_type != (int) R_SH_LOOP_START
1892 && r_type != (int) R_SH_LOOP_END)
1893 continue;
1895 howto = sh_elf_howto_table + r_type;
1897 /* This is a final link. */
1898 h = NULL;
1899 sym = NULL;
1900 sec = NULL;
1901 if (r_symndx < symtab_hdr->sh_info)
1903 /* There is nothing to be done for an internal IND12W
1904 relocation. FIXME: This is probably wrong, but it's how
1905 the COFF relocations work. */
1906 if (r_type == (int) R_SH_IND12W)
1907 continue;
1908 sym = local_syms + r_symndx;
1909 sec = local_sections[r_symndx];
1910 relocation = (sec->output_section->vma
1911 + sec->output_offset
1912 + sym->st_value);
1914 else
1916 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1917 while (h->root.type == bfd_link_hash_indirect
1918 || h->root.type == bfd_link_hash_warning)
1919 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1920 if (h->root.type == bfd_link_hash_defined
1921 || h->root.type == bfd_link_hash_defweak)
1923 sec = h->root.u.def.section;
1924 relocation = (h->root.u.def.value
1925 + sec->output_section->vma
1926 + sec->output_offset);
1928 else if (h->root.type == bfd_link_hash_undefweak)
1929 relocation = 0;
1930 else
1932 if (! ((*info->callbacks->undefined_symbol)
1933 (info, h->root.root.string, input_bfd,
1934 input_section, rel->r_offset, true)))
1935 return false;
1936 relocation = 0;
1940 /* FIXME: This is how the COFF relocations work. */
1941 if (r_type == (int) R_SH_IND12W)
1942 relocation -= 4;
1944 switch ((int)r_type)
1946 case (int)R_SH_DIR32:
1947 addend = rel->r_addend;
1948 /* Fall through. */
1949 default:
1950 /* COFF relocs don't use the addend. The addend is used for
1951 R_SH_DIR32 to be compatible with other compilers. */
1952 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
1953 contents, rel->r_offset,
1954 relocation, addend);
1955 break;
1956 case R_SH_LOOP_START:
1958 static bfd_vma start, end;
1960 start = (relocation + rel->r_addend
1961 - (sec->output_section->vma + sec->output_offset));
1962 r = sh_elf_reloc_loop (r_type, input_bfd, input_section, contents,
1963 rel->r_offset, sec, start, end);
1964 break;
1965 case R_SH_LOOP_END:
1966 end = (relocation + rel->r_addend
1967 - (sec->output_section->vma + sec->output_offset));
1968 r = sh_elf_reloc_loop (r_type, input_bfd, input_section, contents,
1969 rel->r_offset, sec, start, end);
1970 break;
1974 if (r != bfd_reloc_ok)
1976 switch (r)
1978 default:
1979 case bfd_reloc_outofrange:
1980 abort ();
1981 case bfd_reloc_overflow:
1983 const char *name;
1985 if (h != NULL)
1986 name = h->root.root.string;
1987 else
1989 name = (bfd_elf_string_from_elf_section
1990 (input_bfd, symtab_hdr->sh_link, sym->st_name));
1991 if (name == NULL)
1992 return false;
1993 if (*name == '\0')
1994 name = bfd_section_name (input_bfd, sec);
1996 if (! ((*info->callbacks->reloc_overflow)
1997 (info, name, howto->name, (bfd_vma) 0,
1998 input_bfd, input_section, rel->r_offset)))
1999 return false;
2001 break;
2006 return true;
2009 /* This is a version of bfd_generic_get_relocated_section_contents
2010 which uses sh_elf_relocate_section. */
2012 static bfd_byte *
2013 sh_elf_get_relocated_section_contents (output_bfd, link_info, link_order,
2014 data, relocateable, symbols)
2015 bfd *output_bfd;
2016 struct bfd_link_info *link_info;
2017 struct bfd_link_order *link_order;
2018 bfd_byte *data;
2019 boolean relocateable;
2020 asymbol **symbols;
2022 Elf_Internal_Shdr *symtab_hdr;
2023 asection *input_section = link_order->u.indirect.section;
2024 bfd *input_bfd = input_section->owner;
2025 asection **sections = NULL;
2026 Elf_Internal_Rela *internal_relocs = NULL;
2027 Elf32_External_Sym *external_syms = NULL;
2028 Elf_Internal_Sym *internal_syms = NULL;
2030 /* We only need to handle the case of relaxing, or of having a
2031 particular set of section contents, specially. */
2032 if (relocateable
2033 || elf_section_data (input_section)->this_hdr.contents == NULL)
2034 return bfd_generic_get_relocated_section_contents (output_bfd, link_info,
2035 link_order, data,
2036 relocateable,
2037 symbols);
2039 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
2041 memcpy (data, elf_section_data (input_section)->this_hdr.contents,
2042 input_section->_raw_size);
2044 if ((input_section->flags & SEC_RELOC) != 0
2045 && input_section->reloc_count > 0)
2047 Elf_Internal_Sym *isymp;
2048 asection **secpp;
2049 Elf32_External_Sym *esym, *esymend;
2051 if (symtab_hdr->contents != NULL)
2052 external_syms = (Elf32_External_Sym *) symtab_hdr->contents;
2053 else
2055 external_syms = ((Elf32_External_Sym *)
2056 bfd_malloc (symtab_hdr->sh_info
2057 * sizeof (Elf32_External_Sym)));
2058 if (external_syms == NULL && symtab_hdr->sh_info > 0)
2059 goto error_return;
2060 if (bfd_seek (input_bfd, symtab_hdr->sh_offset, SEEK_SET) != 0
2061 || (bfd_read (external_syms, sizeof (Elf32_External_Sym),
2062 symtab_hdr->sh_info, input_bfd)
2063 != (symtab_hdr->sh_info * sizeof (Elf32_External_Sym))))
2064 goto error_return;
2067 internal_relocs = (_bfd_elf32_link_read_relocs
2068 (input_bfd, input_section, (PTR) NULL,
2069 (Elf_Internal_Rela *) NULL, false));
2070 if (internal_relocs == NULL)
2071 goto error_return;
2073 internal_syms = ((Elf_Internal_Sym *)
2074 bfd_malloc (symtab_hdr->sh_info
2075 * sizeof (Elf_Internal_Sym)));
2076 if (internal_syms == NULL && symtab_hdr->sh_info > 0)
2077 goto error_return;
2079 sections = (asection **) bfd_malloc (symtab_hdr->sh_info
2080 * sizeof (asection *));
2081 if (sections == NULL && symtab_hdr->sh_info > 0)
2082 goto error_return;
2084 isymp = internal_syms;
2085 secpp = sections;
2086 esym = external_syms;
2087 esymend = esym + symtab_hdr->sh_info;
2088 for (; esym < esymend; ++esym, ++isymp, ++secpp)
2090 asection *isec;
2092 bfd_elf32_swap_symbol_in (input_bfd, esym, isymp);
2094 if (isymp->st_shndx == SHN_UNDEF)
2095 isec = bfd_und_section_ptr;
2096 else if (isymp->st_shndx > 0 && isymp->st_shndx < SHN_LORESERVE)
2097 isec = bfd_section_from_elf_index (input_bfd, isymp->st_shndx);
2098 else if (isymp->st_shndx == SHN_ABS)
2099 isec = bfd_abs_section_ptr;
2100 else if (isymp->st_shndx == SHN_COMMON)
2101 isec = bfd_com_section_ptr;
2102 else
2104 /* Who knows? */
2105 isec = NULL;
2108 *secpp = isec;
2111 if (! sh_elf_relocate_section (output_bfd, link_info, input_bfd,
2112 input_section, data, internal_relocs,
2113 internal_syms, sections))
2114 goto error_return;
2116 if (sections != NULL)
2117 free (sections);
2118 sections = NULL;
2119 if (internal_syms != NULL)
2120 free (internal_syms);
2121 internal_syms = NULL;
2122 if (external_syms != NULL && symtab_hdr->contents == NULL)
2123 free (external_syms);
2124 external_syms = NULL;
2125 if (internal_relocs != elf_section_data (input_section)->relocs)
2126 free (internal_relocs);
2127 internal_relocs = NULL;
2130 return data;
2132 error_return:
2133 if (internal_relocs != NULL
2134 && internal_relocs != elf_section_data (input_section)->relocs)
2135 free (internal_relocs);
2136 if (external_syms != NULL && symtab_hdr->contents == NULL)
2137 free (external_syms);
2138 if (internal_syms != NULL)
2139 free (internal_syms);
2140 if (sections != NULL)
2141 free (sections);
2142 return NULL;
2144 static asection *
2145 sh_elf_gc_mark_hook (abfd, info, rel, h, sym)
2146 bfd *abfd;
2147 struct bfd_link_info *info ATTRIBUTE_UNUSED;
2148 Elf_Internal_Rela *rel;
2149 struct elf_link_hash_entry *h;
2150 Elf_Internal_Sym *sym;
2152 if (h != NULL)
2154 switch (ELF32_R_TYPE (rel->r_info))
2156 case R_SH_GNU_VTINHERIT:
2157 case R_SH_GNU_VTENTRY:
2158 break;
2160 default:
2161 switch (h->root.type)
2163 case bfd_link_hash_defined:
2164 case bfd_link_hash_defweak:
2165 return h->root.u.def.section;
2167 case bfd_link_hash_common:
2168 return h->root.u.c.p->section;
2170 default:
2171 break;
2175 else
2177 if (!(elf_bad_symtab (abfd)
2178 && ELF_ST_BIND (sym->st_info) != STB_LOCAL)
2179 && ! ((sym->st_shndx <= 0 || sym->st_shndx >= SHN_LORESERVE)
2180 && sym->st_shndx != SHN_COMMON))
2182 return bfd_section_from_elf_index (abfd, sym->st_shndx);
2185 return NULL;
2188 static boolean
2189 sh_elf_gc_sweep_hook (abfd, info, sec, relocs)
2190 bfd *abfd ATTRIBUTE_UNUSED;
2191 struct bfd_link_info *info ATTRIBUTE_UNUSED;
2192 asection *sec ATTRIBUTE_UNUSED;
2193 const Elf_Internal_Rela *relocs ATTRIBUTE_UNUSED;
2195 /* we don't use got and plt entries for sh. */
2196 return true;
2199 /* Look through the relocs for a section during the first phase.
2200 Since we don't do .gots or .plts, we just need to consider the
2201 virtual table relocs for gc. */
2203 static boolean
2204 sh_elf_check_relocs (abfd, info, sec, relocs)
2205 bfd *abfd;
2206 struct bfd_link_info *info;
2207 asection *sec;
2208 const Elf_Internal_Rela *relocs;
2210 Elf_Internal_Shdr *symtab_hdr;
2211 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
2212 const Elf_Internal_Rela *rel;
2213 const Elf_Internal_Rela *rel_end;
2215 if (info->relocateable)
2216 return true;
2218 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2219 sym_hashes = elf_sym_hashes (abfd);
2220 sym_hashes_end = sym_hashes + symtab_hdr->sh_size/sizeof(Elf32_External_Sym);
2221 if (!elf_bad_symtab (abfd))
2222 sym_hashes_end -= symtab_hdr->sh_info;
2224 rel_end = relocs + sec->reloc_count;
2225 for (rel = relocs; rel < rel_end; rel++)
2227 struct elf_link_hash_entry *h;
2228 unsigned long r_symndx;
2230 r_symndx = ELF32_R_SYM (rel->r_info);
2231 if (r_symndx < symtab_hdr->sh_info)
2232 h = NULL;
2233 else
2234 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
2236 switch (ELF32_R_TYPE (rel->r_info))
2238 /* This relocation describes the C++ object vtable hierarchy.
2239 Reconstruct it for later use during GC. */
2240 case R_SH_GNU_VTINHERIT:
2241 if (!_bfd_elf32_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
2242 return false;
2243 break;
2245 /* This relocation describes which C++ vtable entries are actually
2246 used. Record for later use during GC. */
2247 case R_SH_GNU_VTENTRY:
2248 if (!_bfd_elf32_gc_record_vtentry (abfd, sec, h, rel->r_addend))
2249 return false;
2250 break;
2254 return true;
2257 boolean
2258 sh_elf_set_mach_from_flags (abfd)
2259 bfd * abfd;
2261 flagword flags = elf_elfheader (abfd)->e_flags;
2263 switch (flags & EF_SH_MACH_MASK)
2265 case EF_SH1:
2266 bfd_default_set_arch_mach (abfd, bfd_arch_sh, bfd_mach_sh);
2267 break;
2268 case EF_SH2:
2269 bfd_default_set_arch_mach (abfd, bfd_arch_sh, bfd_mach_sh2);
2270 break;
2271 case EF_SH_DSP:
2272 bfd_default_set_arch_mach (abfd, bfd_arch_sh, bfd_mach_sh_dsp);
2273 break;
2274 case EF_SH3:
2275 bfd_default_set_arch_mach (abfd, bfd_arch_sh, bfd_mach_sh3);
2276 break;
2277 case EF_SH3_DSP:
2278 bfd_default_set_arch_mach (abfd, bfd_arch_sh, bfd_mach_sh3_dsp);
2279 break;
2280 case EF_SH3E:
2281 bfd_default_set_arch_mach (abfd, bfd_arch_sh, bfd_mach_sh3e);
2282 break;
2283 case EF_SH_UNKNOWN:
2284 case EF_SH4:
2285 bfd_default_set_arch_mach (abfd, bfd_arch_sh, bfd_mach_sh4);
2286 break;
2287 default:
2288 return false;
2290 return true;
2293 /* Function to keep SH specific file flags. */
2294 static boolean
2295 sh_elf_set_private_flags (abfd, flags)
2296 bfd * abfd;
2297 flagword flags;
2299 BFD_ASSERT (! elf_flags_init (abfd)
2300 || elf_elfheader (abfd)->e_flags == flags);
2302 elf_elfheader (abfd)->e_flags = flags;
2303 elf_flags_init (abfd) = true;
2304 return sh_elf_set_mach_from_flags (abfd);
2307 /* Copy backend specific data from one object module to another */
2308 static boolean
2309 sh_elf_copy_private_data (ibfd, obfd)
2310 bfd * ibfd;
2311 bfd * obfd;
2313 if ( bfd_get_flavour (ibfd) != bfd_target_elf_flavour
2314 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
2315 return true;
2317 return sh_elf_set_private_flags (obfd, elf_elfheader (ibfd)->e_flags);
2320 /* This routine checks for linking big and little endian objects
2321 together, and for linking sh-dsp with sh3e / sh4 objects. */
2323 static boolean
2324 sh_elf_merge_private_data (ibfd, obfd)
2325 bfd *ibfd;
2326 bfd *obfd;
2328 flagword old_flags, new_flags;
2330 if (_bfd_generic_verify_endian_match (ibfd, obfd) == false)
2331 return false;
2333 if ( bfd_get_flavour (ibfd) != bfd_target_elf_flavour
2334 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
2335 return true;
2337 if (! elf_flags_init (obfd))
2339 elf_flags_init (obfd) = true;
2340 elf_elfheader (obfd)->e_flags = 0;
2342 old_flags = elf_elfheader (obfd)->e_flags;
2343 new_flags = elf_elfheader (ibfd)->e_flags;
2344 if ((EF_SH_HAS_DSP (old_flags) && EF_SH_HAS_FP (new_flags))
2345 || (EF_SH_HAS_DSP (new_flags) && EF_SH_HAS_FP (old_flags)))
2347 (*_bfd_error_handler)
2348 ("%s: uses %s instructions while previous modules use %s instructions",
2349 bfd_get_filename (ibfd),
2350 EF_SH_HAS_DSP (new_flags) ? "dsp" : "floating point",
2351 EF_SH_HAS_DSP (new_flags) ? "floating point" : "dsp");
2352 bfd_set_error (bfd_error_bad_value);
2353 return false;
2355 elf_elfheader (obfd)->e_flags = EF_SH_MERGE_MACH (old_flags, new_flags);
2357 return sh_elf_set_mach_from_flags (obfd);
2360 #define TARGET_BIG_SYM bfd_elf32_sh_vec
2361 #define TARGET_BIG_NAME "elf32-sh"
2362 #define TARGET_LITTLE_SYM bfd_elf32_shl_vec
2363 #define TARGET_LITTLE_NAME "elf32-shl"
2364 #define ELF_ARCH bfd_arch_sh
2365 #define ELF_MACHINE_CODE EM_SH
2366 #define ELF_MAXPAGESIZE 0x1
2368 #define elf_symbol_leading_char '_'
2370 #define bfd_elf32_bfd_reloc_type_lookup sh_elf_reloc_type_lookup
2371 #define elf_info_to_howto sh_elf_info_to_howto
2372 #define bfd_elf32_bfd_relax_section sh_elf_relax_section
2373 #define elf_backend_relocate_section sh_elf_relocate_section
2374 #define bfd_elf32_bfd_get_relocated_section_contents \
2375 sh_elf_get_relocated_section_contents
2376 #define elf_backend_object_p sh_elf_set_mach_from_flags
2377 #define bfd_elf32_bfd_set_private_bfd_flags \
2378 sh_elf_set_private_flags
2379 #define bfd_elf32_bfd_copy_private_bfd_data \
2380 sh_elf_copy_private_data
2381 #define bfd_elf32_bfd_merge_private_bfd_data \
2382 sh_elf_merge_private_data
2384 #define elf_backend_gc_mark_hook sh_elf_gc_mark_hook
2385 #define elf_backend_gc_sweep_hook sh_elf_gc_sweep_hook
2386 #define elf_backend_check_relocs sh_elf_check_relocs
2388 #define elf_backend_can_gc_sections 1
2389 #include "elf32-target.h"