source: trunk/src/binutils/bfd/elf32-sh.c@ 554

Last change on this file since 554 was 10, checked in by bird, 22 years ago

Initial revision

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