]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - bfd/elf32-sh.c
2000-03-01 H.J. Lu <hjl@gnu.org>
[thirdparty/binutils-gdb.git] / bfd / elf32-sh.c
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.
4
5 This file is part of BFD, the Binary File Descriptor library.
6
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.
11
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.
16
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. */
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
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 **));
58
59 static 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 */
69 sh_elf_reloc, /* special_function */
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 */
100 sh_elf_reloc, /* special_function */
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 */
115 sh_elf_reloc, /* special_function */
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 */
145 sh_elf_reloc, /* special_function */
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 */
160 sh_elf_reloc, /* special_function */
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 */
177 sh_elf_reloc, /* special_function */
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 */
194 sh_elf_reloc, /* special_function */
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 */
211 sh_elf_reloc, /* special_function */
212 "R_SH_DIR8L", /* name */
213 false, /* partial_inplace */
214 0, /* src_mask */
215 0xff, /* dst_mask */
216 true), /* pcrel_offset */
217
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),
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
423 };
424
425 /* This function is used for normal relocs. This is like the COFF
426 function, and is almost certainly incorrect for other ELF targets. */
427
428 static bfd_reloc_status_type
429 sh_elf_reloc (abfd, reloc_entry, symbol_in, data, input_section, output_bfd,
430 error_message)
431 bfd *abfd;
432 arelent *reloc_entry;
433 asymbol *symbol_in;
434 PTR data;
435 asection *input_section;
436 bfd *output_bfd;
437 char **error_message ATTRIBUTE_UNUSED;
438 {
439 unsigned long insn;
440 bfd_vma sym_value;
441 enum elf_sh_reloc_type r_type;
442 bfd_vma addr = reloc_entry->address;
443 bfd_byte *hit_data = addr + (bfd_byte *) data;
444
445 r_type = (enum elf_sh_reloc_type) reloc_entry->howto->type;
446
447 if (output_bfd != NULL)
448 {
449 /* Partial linking--do nothing. */
450 reloc_entry->address += input_section->output_offset;
451 return bfd_reloc_ok;
452 }
453
454 /* Almost all relocs have to do with relaxing. If any work must be
455 done for them, it has been done in sh_relax_section. */
456 if (r_type != R_SH_DIR32
457 && (r_type != R_SH_IND12W
458 || (symbol_in->flags & BSF_LOCAL) != 0))
459 return bfd_reloc_ok;
460
461 if (symbol_in != NULL
462 && bfd_is_und_section (symbol_in->section))
463 return bfd_reloc_undefined;
464
465 if (bfd_is_com_section (symbol_in->section))
466 sym_value = 0;
467 else
468 sym_value = (symbol_in->value +
469 symbol_in->section->output_section->vma +
470 symbol_in->section->output_offset);
471
472 switch (r_type)
473 {
474 case R_SH_DIR32:
475 insn = bfd_get_32 (abfd, hit_data);
476 insn += sym_value + reloc_entry->addend;
477 bfd_put_32 (abfd, insn, hit_data);
478 break;
479 case R_SH_IND12W:
480 insn = bfd_get_16 (abfd, hit_data);
481 sym_value += reloc_entry->addend;
482 sym_value -= (input_section->output_section->vma
483 + input_section->output_offset
484 + addr
485 + 4);
486 sym_value += (insn & 0xfff) << 1;
487 if (insn & 0x800)
488 sym_value -= 0x1000;
489 insn = (insn & 0xf000) | (sym_value & 0xfff);
490 bfd_put_16 (abfd, insn, hit_data);
491 if (sym_value < (bfd_vma) -0x1000 || sym_value >= 0x1000)
492 return bfd_reloc_overflow;
493 break;
494 default:
495 abort ();
496 break;
497 }
498
499 return bfd_reloc_ok;
500 }
501
502 /* This function is used for relocs which are only used for relaxing,
503 which the linker should otherwise ignore. */
504
505 static bfd_reloc_status_type
506 sh_elf_ignore_reloc (abfd, reloc_entry, symbol, data, input_section,
507 output_bfd, error_message)
508 bfd *abfd ATTRIBUTE_UNUSED;
509 arelent *reloc_entry;
510 asymbol *symbol ATTRIBUTE_UNUSED;
511 PTR data ATTRIBUTE_UNUSED;
512 asection *input_section;
513 bfd *output_bfd;
514 char **error_message ATTRIBUTE_UNUSED;
515 {
516 if (output_bfd != NULL)
517 reloc_entry->address += input_section->output_offset;
518 return bfd_reloc_ok;
519 }
520
521 /* This structure is used to map BFD reloc codes to SH ELF relocs. */
522
523 struct elf_reloc_map
524 {
525 bfd_reloc_code_real_type bfd_reloc_val;
526 unsigned char elf_reloc_val;
527 };
528
529 /* An array mapping BFD reloc codes to SH ELF relocs. */
530
531 static const struct elf_reloc_map sh_reloc_map[] =
532 {
533 { BFD_RELOC_NONE, R_SH_NONE },
534 { BFD_RELOC_32, R_SH_DIR32 },
535 { BFD_RELOC_CTOR, R_SH_DIR32 },
536 { BFD_RELOC_32_PCREL, R_SH_REL32 },
537 { BFD_RELOC_SH_PCDISP8BY2, R_SH_DIR8WPN },
538 { BFD_RELOC_SH_PCDISP12BY2, R_SH_IND12W },
539 { BFD_RELOC_SH_PCRELIMM8BY2, R_SH_DIR8WPZ },
540 { BFD_RELOC_SH_PCRELIMM8BY4, R_SH_DIR8WPL },
541 { BFD_RELOC_8_PCREL, R_SH_SWITCH8 },
542 { BFD_RELOC_SH_SWITCH16, R_SH_SWITCH16 },
543 { BFD_RELOC_SH_SWITCH32, R_SH_SWITCH32 },
544 { BFD_RELOC_SH_USES, R_SH_USES },
545 { BFD_RELOC_SH_COUNT, R_SH_COUNT },
546 { BFD_RELOC_SH_ALIGN, R_SH_ALIGN },
547 { BFD_RELOC_SH_CODE, R_SH_CODE },
548 { BFD_RELOC_SH_DATA, R_SH_DATA },
549 { BFD_RELOC_SH_LABEL, R_SH_LABEL },
550 { BFD_RELOC_VTABLE_INHERIT, R_SH_GNU_VTINHERIT },
551 { BFD_RELOC_VTABLE_ENTRY, R_SH_GNU_VTENTRY },
552 };
553
554 /* Given a BFD reloc code, return the howto structure for the
555 corresponding SH ELf reloc. */
556
557 static reloc_howto_type *
558 sh_elf_reloc_type_lookup (abfd, code)
559 bfd *abfd ATTRIBUTE_UNUSED;
560 bfd_reloc_code_real_type code;
561 {
562 unsigned int i;
563
564 for (i = 0; i < sizeof (sh_reloc_map) / sizeof (struct elf_reloc_map); i++)
565 {
566 if (sh_reloc_map[i].bfd_reloc_val == code)
567 return &sh_elf_howto_table[(int) sh_reloc_map[i].elf_reloc_val];
568 }
569
570 return NULL;
571 }
572
573 /* Given an ELF reloc, fill in the howto field of a relent. */
574
575 static void
576 sh_elf_info_to_howto (abfd, cache_ptr, dst)
577 bfd *abfd ATTRIBUTE_UNUSED;
578 arelent *cache_ptr;
579 Elf_Internal_Rela *dst;
580 {
581 unsigned int r;
582
583 r = ELF32_R_TYPE (dst->r_info);
584
585 BFD_ASSERT (r < (unsigned int) R_SH_max);
586 BFD_ASSERT (r < R_SH_FIRST_INVALID_RELOC || r > R_SH_LAST_INVALID_RELOC);
587
588 cache_ptr->howto = &sh_elf_howto_table[r];
589 }
590 \f
591 /* This function handles relaxing for SH ELF. See the corresponding
592 function in coff-sh.c for a description of what this does. FIXME:
593 There is a lot of duplication here between this code and the COFF
594 specific code. The format of relocs and symbols is wound deeply
595 into this code, but it would still be better if the duplication
596 could be eliminated somehow. Note in particular that although both
597 functions use symbols like R_SH_CODE, those symbols have different
598 values; in coff-sh.c they come from include/coff/sh.h, whereas here
599 they come from enum elf_sh_reloc_type in include/elf/sh.h. */
600
601 static boolean
602 sh_elf_relax_section (abfd, sec, link_info, again)
603 bfd *abfd;
604 asection *sec;
605 struct bfd_link_info *link_info;
606 boolean *again;
607 {
608 Elf_Internal_Shdr *symtab_hdr;
609 Elf_Internal_Rela *internal_relocs;
610 Elf_Internal_Rela *free_relocs = NULL;
611 boolean have_code;
612 Elf_Internal_Rela *irel, *irelend;
613 bfd_byte *contents = NULL;
614 bfd_byte *free_contents = NULL;
615 Elf32_External_Sym *extsyms = NULL;
616 Elf32_External_Sym *free_extsyms = NULL;
617
618 *again = false;
619
620 if (link_info->relocateable
621 || (sec->flags & SEC_RELOC) == 0
622 || sec->reloc_count == 0)
623 return true;
624
625 /* If this is the first time we have been called for this section,
626 initialize the cooked size. */
627 if (sec->_cooked_size == 0)
628 sec->_cooked_size = sec->_raw_size;
629
630 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
631
632 internal_relocs = (_bfd_elf32_link_read_relocs
633 (abfd, sec, (PTR) NULL, (Elf_Internal_Rela *) NULL,
634 link_info->keep_memory));
635 if (internal_relocs == NULL)
636 goto error_return;
637 if (! link_info->keep_memory)
638 free_relocs = internal_relocs;
639
640 have_code = false;
641
642 irelend = internal_relocs + sec->reloc_count;
643 for (irel = internal_relocs; irel < irelend; irel++)
644 {
645 bfd_vma laddr, paddr, symval;
646 unsigned short insn;
647 Elf_Internal_Rela *irelfn, *irelscan, *irelcount;
648 bfd_signed_vma foff;
649
650 if (ELF32_R_TYPE (irel->r_info) == (int) R_SH_CODE)
651 have_code = true;
652
653 if (ELF32_R_TYPE (irel->r_info) != (int) R_SH_USES)
654 continue;
655
656 /* Get the section contents. */
657 if (contents == NULL)
658 {
659 if (elf_section_data (sec)->this_hdr.contents != NULL)
660 contents = elf_section_data (sec)->this_hdr.contents;
661 else
662 {
663 contents = (bfd_byte *) bfd_malloc (sec->_raw_size);
664 if (contents == NULL)
665 goto error_return;
666 free_contents = contents;
667
668 if (! bfd_get_section_contents (abfd, sec, contents,
669 (file_ptr) 0, sec->_raw_size))
670 goto error_return;
671 }
672 }
673
674 /* The r_addend field of the R_SH_USES reloc will point us to
675 the register load. The 4 is because the r_addend field is
676 computed as though it were a jump offset, which are based
677 from 4 bytes after the jump instruction. */
678 laddr = irel->r_offset + 4 + irel->r_addend;
679 if (laddr >= sec->_raw_size)
680 {
681 (*_bfd_error_handler) (_("%s: 0x%lx: warning: bad R_SH_USES offset"),
682 bfd_get_filename (abfd),
683 (unsigned long) irel->r_offset);
684 continue;
685 }
686 insn = bfd_get_16 (abfd, contents + laddr);
687
688 /* If the instruction is not mov.l NN,rN, we don't know what to
689 do. */
690 if ((insn & 0xf000) != 0xd000)
691 {
692 ((*_bfd_error_handler)
693 (_("%s: 0x%lx: warning: R_SH_USES points to unrecognized insn 0x%x"),
694 bfd_get_filename (abfd), (unsigned long) irel->r_offset, insn));
695 continue;
696 }
697
698 /* Get the address from which the register is being loaded. The
699 displacement in the mov.l instruction is quadrupled. It is a
700 displacement from four bytes after the movl instruction, but,
701 before adding in the PC address, two least significant bits
702 of the PC are cleared. We assume that the section is aligned
703 on a four byte boundary. */
704 paddr = insn & 0xff;
705 paddr *= 4;
706 paddr += (laddr + 4) &~ 3;
707 if (paddr >= sec->_raw_size)
708 {
709 ((*_bfd_error_handler)
710 (_("%s: 0x%lx: warning: bad R_SH_USES load offset"),
711 bfd_get_filename (abfd), (unsigned long) irel->r_offset));
712 continue;
713 }
714
715 /* Get the reloc for the address from which the register is
716 being loaded. This reloc will tell us which function is
717 actually being called. */
718 for (irelfn = internal_relocs; irelfn < irelend; irelfn++)
719 if (irelfn->r_offset == paddr
720 && ELF32_R_TYPE (irelfn->r_info) == (int) R_SH_DIR32)
721 break;
722 if (irelfn >= irelend)
723 {
724 ((*_bfd_error_handler)
725 (_("%s: 0x%lx: warning: could not find expected reloc"),
726 bfd_get_filename (abfd), (unsigned long) paddr));
727 continue;
728 }
729
730 /* Read this BFD's symbols if we haven't done so already. */
731 if (extsyms == NULL)
732 {
733 if (symtab_hdr->contents != NULL)
734 extsyms = (Elf32_External_Sym *) symtab_hdr->contents;
735 else
736 {
737 extsyms = ((Elf32_External_Sym *)
738 bfd_malloc (symtab_hdr->sh_size));
739 if (extsyms == NULL)
740 goto error_return;
741 free_extsyms = extsyms;
742 if (bfd_seek (abfd, symtab_hdr->sh_offset, SEEK_SET) != 0
743 || (bfd_read (extsyms, 1, symtab_hdr->sh_size, abfd)
744 != symtab_hdr->sh_size))
745 goto error_return;
746 }
747 }
748
749 /* Get the value of the symbol referred to by the reloc. */
750 if (ELF32_R_SYM (irelfn->r_info) < symtab_hdr->sh_info)
751 {
752 Elf_Internal_Sym isym;
753
754 /* A local symbol. */
755 bfd_elf32_swap_symbol_in (abfd,
756 extsyms + ELF32_R_SYM (irelfn->r_info),
757 &isym);
758
759 if (isym.st_shndx != _bfd_elf_section_from_bfd_section (abfd, sec))
760 {
761 ((*_bfd_error_handler)
762 (_("%s: 0x%lx: warning: symbol in unexpected section"),
763 bfd_get_filename (abfd), (unsigned long) paddr));
764 continue;
765 }
766
767 symval = (isym.st_value
768 + sec->output_section->vma
769 + sec->output_offset);
770 }
771 else
772 {
773 unsigned long indx;
774 struct elf_link_hash_entry *h;
775
776 indx = ELF32_R_SYM (irelfn->r_info) - symtab_hdr->sh_info;
777 h = elf_sym_hashes (abfd)[indx];
778 BFD_ASSERT (h != NULL);
779 if (h->root.type != bfd_link_hash_defined
780 && h->root.type != bfd_link_hash_defweak)
781 {
782 /* This appears to be a reference to an undefined
783 symbol. Just ignore it--it will be caught by the
784 regular reloc processing. */
785 continue;
786 }
787
788 symval = (h->root.u.def.value
789 + h->root.u.def.section->output_section->vma
790 + h->root.u.def.section->output_offset);
791 }
792
793 symval += bfd_get_32 (abfd, contents + paddr);
794
795 /* See if this function call can be shortened. */
796 foff = (symval
797 - (irel->r_offset
798 + sec->output_section->vma
799 + sec->output_offset
800 + 4));
801 if (foff < -0x1000 || foff >= 0x1000)
802 {
803 /* After all that work, we can't shorten this function call. */
804 continue;
805 }
806
807 /* Shorten the function call. */
808
809 /* For simplicity of coding, we are going to modify the section
810 contents, the section relocs, and the BFD symbol table. We
811 must tell the rest of the code not to free up this
812 information. It would be possible to instead create a table
813 of changes which have to be made, as is done in coff-mips.c;
814 that would be more work, but would require less memory when
815 the linker is run. */
816
817 elf_section_data (sec)->relocs = internal_relocs;
818 free_relocs = NULL;
819
820 elf_section_data (sec)->this_hdr.contents = contents;
821 free_contents = NULL;
822
823 symtab_hdr->contents = (bfd_byte *) extsyms;
824 free_extsyms = NULL;
825
826 /* Replace the jsr with a bsr. */
827
828 /* Change the R_SH_USES reloc into an R_SH_IND12W reloc, and
829 replace the jsr with a bsr. */
830 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irelfn->r_info), R_SH_IND12W);
831 if (ELF32_R_SYM (irelfn->r_info) < symtab_hdr->sh_info)
832 {
833 /* If this needs to be changed because of future relaxing,
834 it will be handled here like other internal IND12W
835 relocs. */
836 bfd_put_16 (abfd,
837 0xb000 | ((foff >> 1) & 0xfff),
838 contents + irel->r_offset);
839 }
840 else
841 {
842 /* We can't fully resolve this yet, because the external
843 symbol value may be changed by future relaxing. We let
844 the final link phase handle it. */
845 bfd_put_16 (abfd, 0xb000, contents + irel->r_offset);
846 }
847
848 /* See if there is another R_SH_USES reloc referring to the same
849 register load. */
850 for (irelscan = internal_relocs; irelscan < irelend; irelscan++)
851 if (ELF32_R_TYPE (irelscan->r_info) == (int) R_SH_USES
852 && laddr == irelscan->r_offset + 4 + irelscan->r_addend)
853 break;
854 if (irelscan < irelend)
855 {
856 /* Some other function call depends upon this register load,
857 and we have not yet converted that function call.
858 Indeed, we may never be able to convert it. There is
859 nothing else we can do at this point. */
860 continue;
861 }
862
863 /* Look for a R_SH_COUNT reloc on the location where the
864 function address is stored. Do this before deleting any
865 bytes, to avoid confusion about the address. */
866 for (irelcount = internal_relocs; irelcount < irelend; irelcount++)
867 if (irelcount->r_offset == paddr
868 && ELF32_R_TYPE (irelcount->r_info) == (int) R_SH_COUNT)
869 break;
870
871 /* Delete the register load. */
872 if (! sh_elf_relax_delete_bytes (abfd, sec, laddr, 2))
873 goto error_return;
874
875 /* That will change things, so, just in case it permits some
876 other function call to come within range, we should relax
877 again. Note that this is not required, and it may be slow. */
878 *again = true;
879
880 /* Now check whether we got a COUNT reloc. */
881 if (irelcount >= irelend)
882 {
883 ((*_bfd_error_handler)
884 (_("%s: 0x%lx: warning: could not find expected COUNT reloc"),
885 bfd_get_filename (abfd), (unsigned long) paddr));
886 continue;
887 }
888
889 /* The number of uses is stored in the r_addend field. We've
890 just deleted one. */
891 if (irelcount->r_addend == 0)
892 {
893 ((*_bfd_error_handler) (_("%s: 0x%lx: warning: bad count"),
894 bfd_get_filename (abfd),
895 (unsigned long) paddr));
896 continue;
897 }
898
899 --irelcount->r_addend;
900
901 /* If there are no more uses, we can delete the address. Reload
902 the address from irelfn, in case it was changed by the
903 previous call to sh_elf_relax_delete_bytes. */
904 if (irelcount->r_addend == 0)
905 {
906 if (! sh_elf_relax_delete_bytes (abfd, sec, irelfn->r_offset, 4))
907 goto error_return;
908 }
909
910 /* We've done all we can with that function call. */
911 }
912
913 /* Look for load and store instructions that we can align on four
914 byte boundaries. */
915 if (have_code)
916 {
917 boolean swapped;
918
919 /* Get the section contents. */
920 if (contents == NULL)
921 {
922 if (elf_section_data (sec)->this_hdr.contents != NULL)
923 contents = elf_section_data (sec)->this_hdr.contents;
924 else
925 {
926 contents = (bfd_byte *) bfd_malloc (sec->_raw_size);
927 if (contents == NULL)
928 goto error_return;
929 free_contents = contents;
930
931 if (! bfd_get_section_contents (abfd, sec, contents,
932 (file_ptr) 0, sec->_raw_size))
933 goto error_return;
934 }
935 }
936
937 if (! sh_elf_align_loads (abfd, sec, internal_relocs, contents,
938 &swapped))
939 goto error_return;
940
941 if (swapped)
942 {
943 elf_section_data (sec)->relocs = internal_relocs;
944 free_relocs = NULL;
945
946 elf_section_data (sec)->this_hdr.contents = contents;
947 free_contents = NULL;
948
949 symtab_hdr->contents = (bfd_byte *) extsyms;
950 free_extsyms = NULL;
951 }
952 }
953
954 if (free_relocs != NULL)
955 {
956 free (free_relocs);
957 free_relocs = NULL;
958 }
959
960 if (free_contents != NULL)
961 {
962 if (! link_info->keep_memory)
963 free (free_contents);
964 else
965 {
966 /* Cache the section contents for elf_link_input_bfd. */
967 elf_section_data (sec)->this_hdr.contents = contents;
968 }
969 free_contents = NULL;
970 }
971
972 if (free_extsyms != NULL)
973 {
974 if (! link_info->keep_memory)
975 free (free_extsyms);
976 else
977 {
978 /* Cache the symbols for elf_link_input_bfd. */
979 symtab_hdr->contents = extsyms;
980 }
981 free_extsyms = NULL;
982 }
983
984 return true;
985
986 error_return:
987 if (free_relocs != NULL)
988 free (free_relocs);
989 if (free_contents != NULL)
990 free (free_contents);
991 if (free_extsyms != NULL)
992 free (free_extsyms);
993 return false;
994 }
995
996 /* Delete some bytes from a section while relaxing. FIXME: There is a
997 lot of duplication between this function and sh_relax_delete_bytes
998 in coff-sh.c. */
999
1000 static boolean
1001 sh_elf_relax_delete_bytes (abfd, sec, addr, count)
1002 bfd *abfd;
1003 asection *sec;
1004 bfd_vma addr;
1005 int count;
1006 {
1007 Elf_Internal_Shdr *symtab_hdr;
1008 Elf32_External_Sym *extsyms;
1009 int shndx, index;
1010 bfd_byte *contents;
1011 Elf_Internal_Rela *irel, *irelend;
1012 Elf_Internal_Rela *irelalign;
1013 bfd_vma toaddr;
1014 Elf32_External_Sym *esym, *esymend;
1015 struct elf_link_hash_entry *sym_hash;
1016 asection *o;
1017
1018 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1019 extsyms = (Elf32_External_Sym *) symtab_hdr->contents;
1020
1021 shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
1022
1023 contents = elf_section_data (sec)->this_hdr.contents;
1024
1025 /* The deletion must stop at the next ALIGN reloc for an aligment
1026 power larger than the number of bytes we are deleting. */
1027
1028 irelalign = NULL;
1029 toaddr = sec->_cooked_size;
1030
1031 irel = elf_section_data (sec)->relocs;
1032 irelend = irel + sec->reloc_count;
1033 for (; irel < irelend; irel++)
1034 {
1035 if (ELF32_R_TYPE (irel->r_info) == (int) R_SH_ALIGN
1036 && irel->r_offset > addr
1037 && count < (1 << irel->r_addend))
1038 {
1039 irelalign = irel;
1040 toaddr = irel->r_offset;
1041 break;
1042 }
1043 }
1044
1045 /* Actually delete the bytes. */
1046 memmove (contents + addr, contents + addr + count, toaddr - addr - count);
1047 if (irelalign == NULL)
1048 sec->_cooked_size -= count;
1049 else
1050 {
1051 int i;
1052
1053 #define NOP_OPCODE (0x0009)
1054
1055 BFD_ASSERT ((count & 1) == 0);
1056 for (i = 0; i < count; i += 2)
1057 bfd_put_16 (abfd, NOP_OPCODE, contents + toaddr - count + i);
1058 }
1059
1060 /* Adjust all the relocs. */
1061 for (irel = elf_section_data (sec)->relocs; irel < irelend; irel++)
1062 {
1063 bfd_vma nraddr, stop;
1064 bfd_vma start = 0;
1065 int insn = 0;
1066 Elf_Internal_Sym sym;
1067 int off, adjust, oinsn;
1068 bfd_signed_vma voff = 0;
1069 boolean overflow;
1070
1071 /* Get the new reloc address. */
1072 nraddr = irel->r_offset;
1073 if ((irel->r_offset > addr
1074 && irel->r_offset < toaddr)
1075 || (ELF32_R_TYPE (irel->r_info) == (int) R_SH_ALIGN
1076 && irel->r_offset == toaddr))
1077 nraddr -= count;
1078
1079 /* See if this reloc was for the bytes we have deleted, in which
1080 case we no longer care about it. Don't delete relocs which
1081 represent addresses, though. */
1082 if (irel->r_offset >= addr
1083 && irel->r_offset < addr + count
1084 && ELF32_R_TYPE (irel->r_info) != (int) R_SH_ALIGN
1085 && ELF32_R_TYPE (irel->r_info) != (int) R_SH_CODE
1086 && ELF32_R_TYPE (irel->r_info) != (int) R_SH_DATA
1087 && ELF32_R_TYPE (irel->r_info) != (int) R_SH_LABEL)
1088 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
1089 (int) R_SH_NONE);
1090
1091 /* If this is a PC relative reloc, see if the range it covers
1092 includes the bytes we have deleted. */
1093 switch ((enum elf_sh_reloc_type) ELF32_R_TYPE (irel->r_info))
1094 {
1095 default:
1096 break;
1097
1098 case R_SH_DIR8WPN:
1099 case R_SH_IND12W:
1100 case R_SH_DIR8WPZ:
1101 case R_SH_DIR8WPL:
1102 start = irel->r_offset;
1103 insn = bfd_get_16 (abfd, contents + nraddr);
1104 break;
1105 }
1106
1107 switch ((enum elf_sh_reloc_type) ELF32_R_TYPE (irel->r_info))
1108 {
1109 default:
1110 start = stop = addr;
1111 break;
1112
1113 case R_SH_DIR32:
1114 /* If this reloc is against a symbol defined in this
1115 section, and the symbol will not be adjusted below, we
1116 must check the addend to see it will put the value in
1117 range to be adjusted, and hence must be changed. */
1118 if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
1119 {
1120 bfd_elf32_swap_symbol_in (abfd,
1121 extsyms + ELF32_R_SYM (irel->r_info),
1122 &sym);
1123 if (sym.st_shndx == shndx
1124 && (sym.st_value <= addr
1125 || sym.st_value >= toaddr))
1126 {
1127 bfd_vma val;
1128
1129 val = bfd_get_32 (abfd, contents + nraddr);
1130 val += sym.st_value;
1131 if (val > addr && val < toaddr)
1132 bfd_put_32 (abfd, val - count, contents + nraddr);
1133 }
1134 }
1135 start = stop = addr;
1136 break;
1137
1138 case R_SH_DIR8WPN:
1139 off = insn & 0xff;
1140 if (off & 0x80)
1141 off -= 0x100;
1142 stop = (bfd_vma) ((bfd_signed_vma) start + 4 + off * 2);
1143 break;
1144
1145 case R_SH_IND12W:
1146 if (ELF32_R_SYM (irel->r_info) >= symtab_hdr->sh_info)
1147 start = stop = addr;
1148 else
1149 {
1150 off = insn & 0xfff;
1151 if (off & 0x800)
1152 off -= 0x1000;
1153 stop = (bfd_vma) ((bfd_signed_vma) start + 4 + off * 2);
1154 }
1155 break;
1156
1157 case R_SH_DIR8WPZ:
1158 off = insn & 0xff;
1159 stop = start + 4 + off * 2;
1160 break;
1161
1162 case R_SH_DIR8WPL:
1163 off = insn & 0xff;
1164 stop = (start &~ (bfd_vma) 3) + 4 + off * 4;
1165 break;
1166
1167 case R_SH_SWITCH8:
1168 case R_SH_SWITCH16:
1169 case R_SH_SWITCH32:
1170 /* These relocs types represent
1171 .word L2-L1
1172 The r_addend field holds the difference between the reloc
1173 address and L1. That is the start of the reloc, and
1174 adding in the contents gives us the top. We must adjust
1175 both the r_offset field and the section contents.
1176 N.B. in gas / coff bfd, the elf bfd r_addend is called r_offset,
1177 and the elf bfd r_offset is called r_vaddr. */
1178
1179 stop = irel->r_offset;
1180 start = (bfd_vma) ((bfd_signed_vma) stop - (long) irel->r_addend);
1181
1182 if (start > addr
1183 && start < toaddr
1184 && (stop <= addr || stop >= toaddr))
1185 irel->r_addend += count;
1186 else if (stop > addr
1187 && stop < toaddr
1188 && (start <= addr || start >= toaddr))
1189 irel->r_addend -= count;
1190
1191 if (ELF32_R_TYPE (irel->r_info) == (int) R_SH_SWITCH16)
1192 voff = bfd_get_signed_16 (abfd, contents + nraddr);
1193 else if (ELF32_R_TYPE (irel->r_info) == (int) R_SH_SWITCH8)
1194 voff = bfd_get_8 (abfd, contents + nraddr);
1195 else
1196 voff = bfd_get_signed_32 (abfd, contents + nraddr);
1197 stop = (bfd_vma) ((bfd_signed_vma) start + voff);
1198
1199 break;
1200
1201 case R_SH_USES:
1202 start = irel->r_offset;
1203 stop = (bfd_vma) ((bfd_signed_vma) start
1204 + (long) irel->r_addend
1205 + 4);
1206 break;
1207 }
1208
1209 if (start > addr
1210 && start < toaddr
1211 && (stop <= addr || stop >= toaddr))
1212 adjust = count;
1213 else if (stop > addr
1214 && stop < toaddr
1215 && (start <= addr || start >= toaddr))
1216 adjust = - count;
1217 else
1218 adjust = 0;
1219
1220 if (adjust != 0)
1221 {
1222 oinsn = insn;
1223 overflow = false;
1224 switch ((enum elf_sh_reloc_type) ELF32_R_TYPE (irel->r_info))
1225 {
1226 default:
1227 abort ();
1228 break;
1229
1230 case R_SH_DIR8WPN:
1231 case R_SH_DIR8WPZ:
1232 insn += adjust / 2;
1233 if ((oinsn & 0xff00) != (insn & 0xff00))
1234 overflow = true;
1235 bfd_put_16 (abfd, insn, contents + nraddr);
1236 break;
1237
1238 case R_SH_IND12W:
1239 insn += adjust / 2;
1240 if ((oinsn & 0xf000) != (insn & 0xf000))
1241 overflow = true;
1242 bfd_put_16 (abfd, insn, contents + nraddr);
1243 break;
1244
1245 case R_SH_DIR8WPL:
1246 BFD_ASSERT (adjust == count || count >= 4);
1247 if (count >= 4)
1248 insn += adjust / 4;
1249 else
1250 {
1251 if ((irel->r_offset & 3) == 0)
1252 ++insn;
1253 }
1254 if ((oinsn & 0xff00) != (insn & 0xff00))
1255 overflow = true;
1256 bfd_put_16 (abfd, insn, contents + nraddr);
1257 break;
1258
1259 case R_SH_SWITCH16:
1260 voff += adjust;
1261 if (voff < - 0x8000 || voff >= 0x8000)
1262 overflow = true;
1263 bfd_put_signed_16 (abfd, voff, contents + nraddr);
1264 break;
1265
1266 case R_SH_SWITCH32:
1267 voff += adjust;
1268 bfd_put_signed_32 (abfd, voff, contents + nraddr);
1269 break;
1270
1271 case R_SH_USES:
1272 irel->r_addend += adjust;
1273 break;
1274 }
1275
1276 if (overflow)
1277 {
1278 ((*_bfd_error_handler)
1279 (_("%s: 0x%lx: fatal: reloc overflow while relaxing"),
1280 bfd_get_filename (abfd), (unsigned long) irel->r_offset));
1281 bfd_set_error (bfd_error_bad_value);
1282 return false;
1283 }
1284 }
1285
1286 irel->r_offset = nraddr;
1287 }
1288
1289 /* Look through all the other sections. If there contain any IMM32
1290 relocs against internal symbols which we are not going to adjust
1291 below, we may need to adjust the addends. */
1292 for (o = abfd->sections; o != NULL; o = o->next)
1293 {
1294 Elf_Internal_Rela *internal_relocs;
1295 Elf_Internal_Rela *irelscan, *irelscanend;
1296 bfd_byte *ocontents;
1297
1298 if (o == sec
1299 || (o->flags & SEC_RELOC) == 0
1300 || o->reloc_count == 0)
1301 continue;
1302
1303 /* We always cache the relocs. Perhaps, if info->keep_memory is
1304 false, we should free them, if we are permitted to, when we
1305 leave sh_coff_relax_section. */
1306 internal_relocs = (_bfd_elf32_link_read_relocs
1307 (abfd, o, (PTR) NULL, (Elf_Internal_Rela *) NULL,
1308 true));
1309 if (internal_relocs == NULL)
1310 return false;
1311
1312 ocontents = NULL;
1313 irelscanend = internal_relocs + o->reloc_count;
1314 for (irelscan = internal_relocs; irelscan < irelscanend; irelscan++)
1315 {
1316 Elf_Internal_Sym sym;
1317
1318 /* Dwarf line numbers use R_SH_SWITCH32 relocs. */
1319 if (ELF32_R_TYPE (irelscan->r_info) == (int) R_SH_SWITCH32)
1320 {
1321 bfd_vma start, stop;
1322 bfd_signed_vma voff;
1323
1324 if (ocontents == NULL)
1325 {
1326 if (elf_section_data (o)->this_hdr.contents != NULL)
1327 ocontents = elf_section_data (o)->this_hdr.contents;
1328 else
1329 {
1330 /* We always cache the section contents.
1331 Perhaps, if info->keep_memory is false, we
1332 should free them, if we are permitted to,
1333 when we leave sh_coff_relax_section. */
1334 ocontents = (bfd_byte *) bfd_malloc (o->_raw_size);
1335 if (ocontents == NULL)
1336 return false;
1337 if (! bfd_get_section_contents (abfd, o, ocontents,
1338 (file_ptr) 0,
1339 o->_raw_size))
1340 return false;
1341 elf_section_data (o)->this_hdr.contents = ocontents;
1342 }
1343 }
1344
1345 stop = irelscan->r_offset;
1346 start
1347 = (bfd_vma) ((bfd_signed_vma) stop - (long) irelscan->r_addend);
1348
1349 /* STOP is in a different section, so it won't change. */
1350 if (start > addr && start < toaddr)
1351 irelscan->r_addend += count;
1352
1353 voff = bfd_get_signed_32 (abfd, ocontents + irelscan->r_offset);
1354 stop = (bfd_vma) ((bfd_signed_vma) start + voff);
1355
1356 if (start > addr
1357 && start < toaddr
1358 && (stop <= addr || stop >= toaddr))
1359 bfd_put_signed_32 (abfd, voff + count,
1360 ocontents + irelscan->r_offset);
1361 else if (stop > addr
1362 && stop < toaddr
1363 && (start <= addr || start >= toaddr))
1364 bfd_put_signed_32 (abfd, voff - count,
1365 ocontents + irelscan->r_offset);
1366 }
1367
1368 if (ELF32_R_TYPE (irelscan->r_info) != (int) R_SH_DIR32)
1369 continue;
1370
1371 if (ELF32_R_SYM (irelscan->r_info) >= symtab_hdr->sh_info)
1372 continue;
1373
1374 bfd_elf32_swap_symbol_in (abfd,
1375 extsyms + ELF32_R_SYM (irelscan->r_info),
1376 &sym);
1377
1378 if (sym.st_shndx == shndx
1379 && (sym.st_value <= addr
1380 || sym.st_value >= toaddr))
1381 {
1382 bfd_vma val;
1383
1384 if (ocontents == NULL)
1385 {
1386 if (elf_section_data (o)->this_hdr.contents != NULL)
1387 ocontents = elf_section_data (o)->this_hdr.contents;
1388 else
1389 {
1390 /* We always cache the section contents.
1391 Perhaps, if info->keep_memory is false, we
1392 should free them, if we are permitted to,
1393 when we leave sh_coff_relax_section. */
1394 ocontents = (bfd_byte *) bfd_malloc (o->_raw_size);
1395 if (ocontents == NULL)
1396 return false;
1397 if (! bfd_get_section_contents (abfd, o, ocontents,
1398 (file_ptr) 0,
1399 o->_raw_size))
1400 return false;
1401 elf_section_data (o)->this_hdr.contents = ocontents;
1402 }
1403 }
1404
1405 val = bfd_get_32 (abfd, ocontents + irelscan->r_offset);
1406 val += sym.st_value;
1407 if (val > addr && val < toaddr)
1408 bfd_put_32 (abfd, val - count,
1409 ocontents + irelscan->r_offset);
1410 }
1411 }
1412 }
1413
1414 /* Adjust the local symbols defined in this section. */
1415 esym = extsyms;
1416 esymend = esym + symtab_hdr->sh_info;
1417 for (; esym < esymend; esym++)
1418 {
1419 Elf_Internal_Sym isym;
1420
1421 bfd_elf32_swap_symbol_in (abfd, esym, &isym);
1422
1423 if (isym.st_shndx == shndx
1424 && isym.st_value > addr
1425 && isym.st_value < toaddr)
1426 {
1427 isym.st_value -= count;
1428 bfd_elf32_swap_symbol_out (abfd, &isym, esym);
1429 }
1430 }
1431
1432 /* Now adjust the global symbols defined in this section. */
1433 esym = extsyms + symtab_hdr->sh_info;
1434 esymend = extsyms + (symtab_hdr->sh_size / sizeof (Elf32_External_Sym));
1435 for (index = 0; esym < esymend; esym++, index++)
1436 {
1437 Elf_Internal_Sym isym;
1438
1439 bfd_elf32_swap_symbol_in (abfd, esym, &isym);
1440 sym_hash = elf_sym_hashes (abfd)[index];
1441 if (isym.st_shndx == shndx
1442 && ((sym_hash)->root.type == bfd_link_hash_defined
1443 || (sym_hash)->root.type == bfd_link_hash_defweak)
1444 && (sym_hash)->root.u.def.section == sec
1445 && (sym_hash)->root.u.def.value > addr
1446 && (sym_hash)->root.u.def.value < toaddr)
1447 {
1448 (sym_hash)->root.u.def.value -= count;
1449 }
1450 }
1451
1452 /* See if we can move the ALIGN reloc forward. We have adjusted
1453 r_offset for it already. */
1454 if (irelalign != NULL)
1455 {
1456 bfd_vma alignto, alignaddr;
1457
1458 alignto = BFD_ALIGN (toaddr, 1 << irelalign->r_addend);
1459 alignaddr = BFD_ALIGN (irelalign->r_offset,
1460 1 << irelalign->r_addend);
1461 if (alignto != alignaddr)
1462 {
1463 /* Tail recursion. */
1464 return sh_elf_relax_delete_bytes (abfd, sec, alignaddr,
1465 alignto - alignaddr);
1466 }
1467 }
1468
1469 return true;
1470 }
1471
1472 /* Look for loads and stores which we can align to four byte
1473 boundaries. This is like sh_align_loads in coff-sh.c. */
1474
1475 static boolean
1476 sh_elf_align_loads (abfd, sec, internal_relocs, contents, pswapped)
1477 bfd *abfd;
1478 asection *sec;
1479 Elf_Internal_Rela *internal_relocs;
1480 bfd_byte *contents;
1481 boolean *pswapped;
1482 {
1483 Elf_Internal_Rela *irel, *irelend;
1484 bfd_vma *labels = NULL;
1485 bfd_vma *label, *label_end;
1486
1487 *pswapped = false;
1488
1489 irelend = internal_relocs + sec->reloc_count;
1490
1491 /* Get all the addresses with labels on them. */
1492 labels = (bfd_vma *) bfd_malloc (sec->reloc_count * sizeof (bfd_vma));
1493 if (labels == NULL)
1494 goto error_return;
1495 label_end = labels;
1496 for (irel = internal_relocs; irel < irelend; irel++)
1497 {
1498 if (ELF32_R_TYPE (irel->r_info) == (int) R_SH_LABEL)
1499 {
1500 *label_end = irel->r_offset;
1501 ++label_end;
1502 }
1503 }
1504
1505 /* Note that the assembler currently always outputs relocs in
1506 address order. If that ever changes, this code will need to sort
1507 the label values and the relocs. */
1508
1509 label = labels;
1510
1511 for (irel = internal_relocs; irel < irelend; irel++)
1512 {
1513 bfd_vma start, stop;
1514
1515 if (ELF32_R_TYPE (irel->r_info) != (int) R_SH_CODE)
1516 continue;
1517
1518 start = irel->r_offset;
1519
1520 for (irel++; irel < irelend; irel++)
1521 if (ELF32_R_TYPE (irel->r_info) == (int) R_SH_DATA)
1522 break;
1523 if (irel < irelend)
1524 stop = irel->r_offset;
1525 else
1526 stop = sec->_cooked_size;
1527
1528 if (! _bfd_sh_align_load_span (abfd, sec, contents, sh_elf_swap_insns,
1529 (PTR) internal_relocs, &label,
1530 label_end, start, stop, pswapped))
1531 goto error_return;
1532 }
1533
1534 free (labels);
1535
1536 return true;
1537
1538 error_return:
1539 if (labels != NULL)
1540 free (labels);
1541 return false;
1542 }
1543
1544 /* Swap two SH instructions. This is like sh_swap_insns in coff-sh.c. */
1545
1546 static boolean
1547 sh_elf_swap_insns (abfd, sec, relocs, contents, addr)
1548 bfd *abfd;
1549 asection *sec;
1550 PTR relocs;
1551 bfd_byte *contents;
1552 bfd_vma addr;
1553 {
1554 Elf_Internal_Rela *internal_relocs = (Elf_Internal_Rela *) relocs;
1555 unsigned short i1, i2;
1556 Elf_Internal_Rela *irel, *irelend;
1557
1558 /* Swap the instructions themselves. */
1559 i1 = bfd_get_16 (abfd, contents + addr);
1560 i2 = bfd_get_16 (abfd, contents + addr + 2);
1561 bfd_put_16 (abfd, i2, contents + addr);
1562 bfd_put_16 (abfd, i1, contents + addr + 2);
1563
1564 /* Adjust all reloc addresses. */
1565 irelend = internal_relocs + sec->reloc_count;
1566 for (irel = internal_relocs; irel < irelend; irel++)
1567 {
1568 enum elf_sh_reloc_type type;
1569 int add;
1570
1571 /* There are a few special types of relocs that we don't want to
1572 adjust. These relocs do not apply to the instruction itself,
1573 but are only associated with the address. */
1574 type = (enum elf_sh_reloc_type) ELF32_R_TYPE (irel->r_info);
1575 if (type == R_SH_ALIGN
1576 || type == R_SH_CODE
1577 || type == R_SH_DATA
1578 || type == R_SH_LABEL)
1579 continue;
1580
1581 /* If an R_SH_USES reloc points to one of the addresses being
1582 swapped, we must adjust it. It would be incorrect to do this
1583 for a jump, though, since we want to execute both
1584 instructions after the jump. (We have avoided swapping
1585 around a label, so the jump will not wind up executing an
1586 instruction it shouldn't). */
1587 if (type == R_SH_USES)
1588 {
1589 bfd_vma off;
1590
1591 off = irel->r_offset + 4 + irel->r_addend;
1592 if (off == addr)
1593 irel->r_offset += 2;
1594 else if (off == addr + 2)
1595 irel->r_offset -= 2;
1596 }
1597
1598 if (irel->r_offset == addr)
1599 {
1600 irel->r_offset += 2;
1601 add = -2;
1602 }
1603 else if (irel->r_offset == addr + 2)
1604 {
1605 irel->r_offset -= 2;
1606 add = 2;
1607 }
1608 else
1609 add = 0;
1610
1611 if (add != 0)
1612 {
1613 bfd_byte *loc;
1614 unsigned short insn, oinsn;
1615 boolean overflow;
1616
1617 loc = contents + irel->r_offset;
1618 overflow = false;
1619 switch (type)
1620 {
1621 default:
1622 break;
1623
1624 case R_SH_DIR8WPN:
1625 case R_SH_DIR8WPZ:
1626 insn = bfd_get_16 (abfd, loc);
1627 oinsn = insn;
1628 insn += add / 2;
1629 if ((oinsn & 0xff00) != (insn & 0xff00))
1630 overflow = true;
1631 bfd_put_16 (abfd, insn, loc);
1632 break;
1633
1634 case R_SH_IND12W:
1635 insn = bfd_get_16 (abfd, loc);
1636 oinsn = insn;
1637 insn += add / 2;
1638 if ((oinsn & 0xf000) != (insn & 0xf000))
1639 overflow = true;
1640 bfd_put_16 (abfd, insn, loc);
1641 break;
1642
1643 case R_SH_DIR8WPL:
1644 /* This reloc ignores the least significant 3 bits of
1645 the program counter before adding in the offset.
1646 This means that if ADDR is at an even address, the
1647 swap will not affect the offset. If ADDR is an at an
1648 odd address, then the instruction will be crossing a
1649 four byte boundary, and must be adjusted. */
1650 if ((addr & 3) != 0)
1651 {
1652 insn = bfd_get_16 (abfd, loc);
1653 oinsn = insn;
1654 insn += add / 2;
1655 if ((oinsn & 0xff00) != (insn & 0xff00))
1656 overflow = true;
1657 bfd_put_16 (abfd, insn, loc);
1658 }
1659
1660 break;
1661 }
1662
1663 if (overflow)
1664 {
1665 ((*_bfd_error_handler)
1666 (_("%s: 0x%lx: fatal: reloc overflow while relaxing"),
1667 bfd_get_filename (abfd), (unsigned long) irel->r_offset));
1668 bfd_set_error (bfd_error_bad_value);
1669 return false;
1670 }
1671 }
1672 }
1673
1674 return true;
1675 }
1676 \f
1677 /* Relocate an SH ELF section. */
1678
1679 static boolean
1680 sh_elf_relocate_section (output_bfd, info, input_bfd, input_section,
1681 contents, relocs, local_syms, local_sections)
1682 bfd *output_bfd ATTRIBUTE_UNUSED;
1683 struct bfd_link_info *info;
1684 bfd *input_bfd;
1685 asection *input_section;
1686 bfd_byte *contents;
1687 Elf_Internal_Rela *relocs;
1688 Elf_Internal_Sym *local_syms;
1689 asection **local_sections;
1690 {
1691 Elf_Internal_Shdr *symtab_hdr;
1692 struct elf_link_hash_entry **sym_hashes;
1693 Elf_Internal_Rela *rel, *relend;
1694
1695 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
1696 sym_hashes = elf_sym_hashes (input_bfd);
1697
1698 rel = relocs;
1699 relend = relocs + input_section->reloc_count;
1700 for (; rel < relend; rel++)
1701 {
1702 int r_type;
1703 reloc_howto_type *howto;
1704 unsigned long r_symndx;
1705 Elf_Internal_Sym *sym;
1706 asection *sec;
1707 struct elf_link_hash_entry *h;
1708 bfd_vma relocation;
1709 bfd_vma addend = (bfd_vma)0;
1710 bfd_reloc_status_type r;
1711
1712 r_symndx = ELF32_R_SYM (rel->r_info);
1713
1714 if (info->relocateable)
1715 {
1716 /* This is a relocateable link. We don't have to change
1717 anything, unless the reloc is against a section symbol,
1718 in which case we have to adjust according to where the
1719 section symbol winds up in the output section. */
1720 if (r_symndx < symtab_hdr->sh_info)
1721 {
1722 sym = local_syms + r_symndx;
1723 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
1724 {
1725 sec = local_sections[r_symndx];
1726 rel->r_addend += sec->output_offset + sym->st_value;
1727 }
1728 }
1729
1730 continue;
1731 }
1732
1733 r_type = ELF32_R_TYPE (rel->r_info);
1734
1735 /* Many of the relocs are only used for relaxing, and are
1736 handled entirely by the relaxation code. */
1737 if (r_type > (int) R_SH_LAST_INVALID_RELOC)
1738 continue;
1739
1740 if (r_type < 0
1741 || r_type >= (int) R_SH_FIRST_INVALID_RELOC)
1742 {
1743 bfd_set_error (bfd_error_bad_value);
1744 return false;
1745 }
1746
1747 /* FIXME: This is certainly incorrect. However, it is how the
1748 COFF linker works. */
1749 if (r_type != (int) R_SH_DIR32
1750 && r_type != (int) R_SH_IND12W)
1751 continue;
1752
1753 howto = sh_elf_howto_table + r_type;
1754
1755 /* This is a final link. */
1756 h = NULL;
1757 sym = NULL;
1758 sec = NULL;
1759 if (r_symndx < symtab_hdr->sh_info)
1760 {
1761 /* There is nothing to be done for an internal IND12W
1762 relocation. FIXME: This is probably wrong, but it's how
1763 the COFF relocations work. */
1764 if (r_type == (int) R_SH_IND12W)
1765 continue;
1766 sym = local_syms + r_symndx;
1767 sec = local_sections[r_symndx];
1768 relocation = (sec->output_section->vma
1769 + sec->output_offset
1770 + sym->st_value);
1771 }
1772 else
1773 {
1774 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1775 while (h->root.type == bfd_link_hash_indirect
1776 || h->root.type == bfd_link_hash_warning)
1777 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1778 if (h->root.type == bfd_link_hash_defined
1779 || h->root.type == bfd_link_hash_defweak)
1780 {
1781 sec = h->root.u.def.section;
1782 relocation = (h->root.u.def.value
1783 + sec->output_section->vma
1784 + sec->output_offset);
1785 }
1786 else if (h->root.type == bfd_link_hash_undefweak)
1787 relocation = 0;
1788 else
1789 {
1790 if (! ((*info->callbacks->undefined_symbol)
1791 (info, h->root.root.string, input_bfd,
1792 input_section, rel->r_offset, true)))
1793 return false;
1794 relocation = 0;
1795 }
1796 }
1797
1798 /* FIXME: This is how the COFF relocations work. */
1799 if (r_type == (int) R_SH_IND12W)
1800 relocation -= 4;
1801
1802 switch ((int)r_type)
1803 {
1804 case (int)R_SH_DIR32:
1805 addend = rel->r_addend;
1806 break;
1807 }
1808
1809 /* COFF relocs don't use the addend. The addend is used for R_SH_DIR32
1810 to be compatible with other compilers. */
1811 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
1812 contents, rel->r_offset,
1813 relocation, addend);
1814
1815 if (r != bfd_reloc_ok)
1816 {
1817 switch (r)
1818 {
1819 default:
1820 case bfd_reloc_outofrange:
1821 abort ();
1822 case bfd_reloc_overflow:
1823 {
1824 const char *name;
1825
1826 if (h != NULL)
1827 name = h->root.root.string;
1828 else
1829 {
1830 name = (bfd_elf_string_from_elf_section
1831 (input_bfd, symtab_hdr->sh_link, sym->st_name));
1832 if (name == NULL)
1833 return false;
1834 if (*name == '\0')
1835 name = bfd_section_name (input_bfd, sec);
1836 }
1837 if (! ((*info->callbacks->reloc_overflow)
1838 (info, name, howto->name, (bfd_vma) 0,
1839 input_bfd, input_section, rel->r_offset)))
1840 return false;
1841 }
1842 break;
1843 }
1844 }
1845 }
1846
1847 return true;
1848 }
1849
1850 /* This is a version of bfd_generic_get_relocated_section_contents
1851 which uses sh_elf_relocate_section. */
1852
1853 static bfd_byte *
1854 sh_elf_get_relocated_section_contents (output_bfd, link_info, link_order,
1855 data, relocateable, symbols)
1856 bfd *output_bfd;
1857 struct bfd_link_info *link_info;
1858 struct bfd_link_order *link_order;
1859 bfd_byte *data;
1860 boolean relocateable;
1861 asymbol **symbols;
1862 {
1863 Elf_Internal_Shdr *symtab_hdr;
1864 asection *input_section = link_order->u.indirect.section;
1865 bfd *input_bfd = input_section->owner;
1866 asection **sections = NULL;
1867 Elf_Internal_Rela *internal_relocs = NULL;
1868 Elf32_External_Sym *external_syms = NULL;
1869 Elf_Internal_Sym *internal_syms = NULL;
1870
1871 /* We only need to handle the case of relaxing, or of having a
1872 particular set of section contents, specially. */
1873 if (relocateable
1874 || elf_section_data (input_section)->this_hdr.contents == NULL)
1875 return bfd_generic_get_relocated_section_contents (output_bfd, link_info,
1876 link_order, data,
1877 relocateable,
1878 symbols);
1879
1880 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
1881
1882 memcpy (data, elf_section_data (input_section)->this_hdr.contents,
1883 input_section->_raw_size);
1884
1885 if ((input_section->flags & SEC_RELOC) != 0
1886 && input_section->reloc_count > 0)
1887 {
1888 Elf_Internal_Sym *isymp;
1889 asection **secpp;
1890 Elf32_External_Sym *esym, *esymend;
1891
1892 if (symtab_hdr->contents != NULL)
1893 external_syms = (Elf32_External_Sym *) symtab_hdr->contents;
1894 else
1895 {
1896 external_syms = ((Elf32_External_Sym *)
1897 bfd_malloc (symtab_hdr->sh_info
1898 * sizeof (Elf32_External_Sym)));
1899 if (external_syms == NULL && symtab_hdr->sh_info > 0)
1900 goto error_return;
1901 if (bfd_seek (input_bfd, symtab_hdr->sh_offset, SEEK_SET) != 0
1902 || (bfd_read (external_syms, sizeof (Elf32_External_Sym),
1903 symtab_hdr->sh_info, input_bfd)
1904 != (symtab_hdr->sh_info * sizeof (Elf32_External_Sym))))
1905 goto error_return;
1906 }
1907
1908 internal_relocs = (_bfd_elf32_link_read_relocs
1909 (input_bfd, input_section, (PTR) NULL,
1910 (Elf_Internal_Rela *) NULL, false));
1911 if (internal_relocs == NULL)
1912 goto error_return;
1913
1914 internal_syms = ((Elf_Internal_Sym *)
1915 bfd_malloc (symtab_hdr->sh_info
1916 * sizeof (Elf_Internal_Sym)));
1917 if (internal_syms == NULL && symtab_hdr->sh_info > 0)
1918 goto error_return;
1919
1920 sections = (asection **) bfd_malloc (symtab_hdr->sh_info
1921 * sizeof (asection *));
1922 if (sections == NULL && symtab_hdr->sh_info > 0)
1923 goto error_return;
1924
1925 isymp = internal_syms;
1926 secpp = sections;
1927 esym = external_syms;
1928 esymend = esym + symtab_hdr->sh_info;
1929 for (; esym < esymend; ++esym, ++isymp, ++secpp)
1930 {
1931 asection *isec;
1932
1933 bfd_elf32_swap_symbol_in (input_bfd, esym, isymp);
1934
1935 if (isymp->st_shndx == SHN_UNDEF)
1936 isec = bfd_und_section_ptr;
1937 else if (isymp->st_shndx > 0 && isymp->st_shndx < SHN_LORESERVE)
1938 isec = bfd_section_from_elf_index (input_bfd, isymp->st_shndx);
1939 else if (isymp->st_shndx == SHN_ABS)
1940 isec = bfd_abs_section_ptr;
1941 else if (isymp->st_shndx == SHN_COMMON)
1942 isec = bfd_com_section_ptr;
1943 else
1944 {
1945 /* Who knows? */
1946 isec = NULL;
1947 }
1948
1949 *secpp = isec;
1950 }
1951
1952 if (! sh_elf_relocate_section (output_bfd, link_info, input_bfd,
1953 input_section, data, internal_relocs,
1954 internal_syms, sections))
1955 goto error_return;
1956
1957 if (sections != NULL)
1958 free (sections);
1959 sections = NULL;
1960 if (internal_syms != NULL)
1961 free (internal_syms);
1962 internal_syms = NULL;
1963 if (external_syms != NULL && symtab_hdr->contents == NULL)
1964 free (external_syms);
1965 external_syms = NULL;
1966 if (internal_relocs != elf_section_data (input_section)->relocs)
1967 free (internal_relocs);
1968 internal_relocs = NULL;
1969 }
1970
1971 return data;
1972
1973 error_return:
1974 if (internal_relocs != NULL
1975 && internal_relocs != elf_section_data (input_section)->relocs)
1976 free (internal_relocs);
1977 if (external_syms != NULL && symtab_hdr->contents == NULL)
1978 free (external_syms);
1979 if (internal_syms != NULL)
1980 free (internal_syms);
1981 if (sections != NULL)
1982 free (sections);
1983 return NULL;
1984 }
1985 static asection *
1986 sh_elf_gc_mark_hook (abfd, info, rel, h, sym)
1987 bfd *abfd;
1988 struct bfd_link_info *info ATTRIBUTE_UNUSED;
1989 Elf_Internal_Rela *rel;
1990 struct elf_link_hash_entry *h;
1991 Elf_Internal_Sym *sym;
1992 {
1993 if (h != NULL)
1994 {
1995 switch (ELF32_R_TYPE (rel->r_info))
1996 {
1997 case R_SH_GNU_VTINHERIT:
1998 case R_SH_GNU_VTENTRY:
1999 break;
2000
2001 default:
2002 switch (h->root.type)
2003 {
2004 case bfd_link_hash_defined:
2005 case bfd_link_hash_defweak:
2006 return h->root.u.def.section;
2007
2008 case bfd_link_hash_common:
2009 return h->root.u.c.p->section;
2010
2011 default:
2012 break;
2013 }
2014 }
2015 }
2016 else
2017 {
2018 if (!(elf_bad_symtab (abfd)
2019 && ELF_ST_BIND (sym->st_info) != STB_LOCAL)
2020 && ! ((sym->st_shndx <= 0 || sym->st_shndx >= SHN_LORESERVE)
2021 && sym->st_shndx != SHN_COMMON))
2022 {
2023 return bfd_section_from_elf_index (abfd, sym->st_shndx);
2024 }
2025 }
2026 return NULL;
2027 }
2028
2029 static boolean
2030 sh_elf_gc_sweep_hook (abfd, info, sec, relocs)
2031 bfd *abfd ATTRIBUTE_UNUSED;
2032 struct bfd_link_info *info ATTRIBUTE_UNUSED;
2033 asection *sec ATTRIBUTE_UNUSED;
2034 const Elf_Internal_Rela *relocs ATTRIBUTE_UNUSED;
2035 {
2036 /* we don't use got and plt entries for sh. */
2037 return true;
2038 }
2039
2040 /* Look through the relocs for a section during the first phase.
2041 Since we don't do .gots or .plts, we just need to consider the
2042 virtual table relocs for gc. */
2043
2044 static boolean
2045 sh_elf_check_relocs (abfd, info, sec, relocs)
2046 bfd *abfd;
2047 struct bfd_link_info *info;
2048 asection *sec;
2049 const Elf_Internal_Rela *relocs;
2050 {
2051 Elf_Internal_Shdr *symtab_hdr;
2052 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
2053 const Elf_Internal_Rela *rel;
2054 const Elf_Internal_Rela *rel_end;
2055
2056 if (info->relocateable)
2057 return true;
2058
2059 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2060 sym_hashes = elf_sym_hashes (abfd);
2061 sym_hashes_end = sym_hashes + symtab_hdr->sh_size/sizeof(Elf32_External_Sym);
2062 if (!elf_bad_symtab (abfd))
2063 sym_hashes_end -= symtab_hdr->sh_info;
2064
2065 rel_end = relocs + sec->reloc_count;
2066 for (rel = relocs; rel < rel_end; rel++)
2067 {
2068 struct elf_link_hash_entry *h;
2069 unsigned long r_symndx;
2070
2071 r_symndx = ELF32_R_SYM (rel->r_info);
2072 if (r_symndx < symtab_hdr->sh_info)
2073 h = NULL;
2074 else
2075 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
2076
2077 switch (ELF32_R_TYPE (rel->r_info))
2078 {
2079 /* This relocation describes the C++ object vtable hierarchy.
2080 Reconstruct it for later use during GC. */
2081 case R_SH_GNU_VTINHERIT:
2082 if (!_bfd_elf32_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
2083 return false;
2084 break;
2085
2086 /* This relocation describes which C++ vtable entries are actually
2087 used. Record for later use during GC. */
2088 case R_SH_GNU_VTENTRY:
2089 if (!_bfd_elf32_gc_record_vtentry (abfd, sec, h, rel->r_addend))
2090 return false;
2091 break;
2092 }
2093 }
2094
2095 return true;
2096 }
2097
2098 boolean
2099 sh_elf_set_mach_from_flags (abfd)
2100 bfd * abfd;
2101 {
2102 flagword flags = elf_elfheader (abfd)->e_flags;
2103
2104 switch (elf_elfheader (abfd)->e_flags & EF_SH_MACH_MASK)
2105 {
2106 case EF_SH1:
2107 bfd_default_set_arch_mach (abfd, bfd_arch_sh, bfd_mach_sh);
2108 break;
2109 case EF_SH2:
2110 bfd_default_set_arch_mach (abfd, bfd_arch_sh, bfd_mach_sh2);
2111 break;
2112 case EF_SH_DSP:
2113 bfd_default_set_arch_mach (abfd, bfd_arch_sh, bfd_mach_sh_dsp);
2114 break;
2115 case EF_SH3:
2116 bfd_default_set_arch_mach (abfd, bfd_arch_sh, bfd_mach_sh3);
2117 break;
2118 case EF_SH3_DSP:
2119 bfd_default_set_arch_mach (abfd, bfd_arch_sh, bfd_mach_sh3_dsp);
2120 break;
2121 case EF_SH3E:
2122 bfd_default_set_arch_mach (abfd, bfd_arch_sh, bfd_mach_sh3e);
2123 break;
2124 case EF_SH_UNKNOWN:
2125 case EF_SH4:
2126 bfd_default_set_arch_mach (abfd, bfd_arch_sh, bfd_mach_sh4);
2127 break;
2128 default:
2129 return false;
2130 }
2131 return true;
2132 }
2133
2134 /* Function to keep SH specific file flags. */
2135 static boolean
2136 sh_elf_set_private_flags (abfd, flags)
2137 bfd * abfd;
2138 flagword flags;
2139 {
2140 BFD_ASSERT (! elf_flags_init (abfd)
2141 || elf_elfheader (abfd)->e_flags == flags);
2142
2143 elf_elfheader (abfd)->e_flags = flags;
2144 elf_flags_init (abfd) = true;
2145 return sh_elf_set_mach_from_flags (abfd);
2146 }
2147
2148 /* Copy backend specific data from one object module to another */
2149 static boolean
2150 sh_elf_copy_private_data (ibfd, obfd)
2151 bfd * ibfd;
2152 bfd * obfd;
2153 {
2154 if ( bfd_get_flavour (ibfd) != bfd_target_elf_flavour
2155 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
2156 return true;
2157
2158 return sh_elf_set_private_flags (obfd, elf_elfheader (ibfd)->e_flags);
2159 }
2160
2161 /* This routine checks for linking big and little endian objects
2162 together, and for linking sh-dsp with sh3e / sh4 objects. */
2163
2164 static boolean
2165 sh_elf_merge_private_data (ibfd, obfd)
2166 bfd *ibfd;
2167 bfd *obfd;
2168 {
2169 flagword old_flags, new_flags;
2170
2171 if (_bfd_generic_verify_endian_match (ibfd, obfd) == false)
2172 return false;
2173
2174 if ( bfd_get_flavour (ibfd) != bfd_target_elf_flavour
2175 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
2176 return true;
2177
2178 if (! elf_flags_init (obfd))
2179 {
2180 elf_flags_init (obfd) = true;
2181 elf_elfheader (obfd)->e_flags = 0;
2182 }
2183 old_flags = elf_elfheader (obfd)->e_flags;
2184 new_flags = elf_elfheader (ibfd)->e_flags;
2185 if ((EF_SH_HAS_DSP (old_flags) && EF_SH_HAS_FP (new_flags))
2186 || (EF_SH_HAS_DSP (new_flags) && EF_SH_HAS_FP (old_flags)))
2187 {
2188 (*_bfd_error_handler)
2189 ("%s: uses %s instructions while previous modules use %s instructions",
2190 bfd_get_filename (ibfd),
2191 EF_SH_HAS_DSP (new_flags) ? "dsp" : "floating point",
2192 EF_SH_HAS_DSP (new_flags) ? "floating point" : "dsp");
2193 bfd_set_error (bfd_error_bad_value);
2194 return false;
2195 }
2196 elf_elfheader (obfd)->e_flags = EF_SH_MERGE_MACH (old_flags, new_flags);
2197
2198 return sh_elf_set_mach_from_flags (obfd);
2199 }
2200
2201 #define TARGET_BIG_SYM bfd_elf32_sh_vec
2202 #define TARGET_BIG_NAME "elf32-sh"
2203 #define TARGET_LITTLE_SYM bfd_elf32_shl_vec
2204 #define TARGET_LITTLE_NAME "elf32-shl"
2205 #define ELF_ARCH bfd_arch_sh
2206 #define ELF_MACHINE_CODE EM_SH
2207 #define ELF_MAXPAGESIZE 0x1
2208
2209 #define elf_symbol_leading_char '_'
2210
2211 #define bfd_elf32_bfd_reloc_type_lookup sh_elf_reloc_type_lookup
2212 #define elf_info_to_howto sh_elf_info_to_howto
2213 #define bfd_elf32_bfd_relax_section sh_elf_relax_section
2214 #define elf_backend_relocate_section sh_elf_relocate_section
2215 #define bfd_elf32_bfd_get_relocated_section_contents \
2216 sh_elf_get_relocated_section_contents
2217 #define elf_backend_object_p sh_elf_set_mach_from_flags
2218 #define bfd_elf32_bfd_set_private_bfd_flags \
2219 sh_elf_set_private_flags
2220 #define bfd_elf32_bfd_copy_private_bfd_data \
2221 sh_elf_copy_private_data
2222 #define bfd_elf32_bfd_merge_private_bfd_data \
2223 sh_elf_merge_private_data
2224
2225 #define elf_backend_gc_mark_hook sh_elf_gc_mark_hook
2226 #define elf_backend_gc_sweep_hook sh_elf_gc_sweep_hook
2227 #define elf_backend_check_relocs sh_elf_check_relocs
2228
2229 #define elf_backend_can_gc_sections 1
2230 #include "elf32-target.h"