* ltcf-cxx.sh: Added file, required by 2000-11-18 merge.
[binutils.git] / bfd / elf32-sh.c
bloba3483035ed530e9496e4739d89ddb81f7107de71
1 /* Hitachi SH specific support for 32-bit ELF
2 Copyright 1996, 97, 98, 1999, 2000 Free Software Foundation, Inc.
3 Contributed by Ian Lance Taylor, Cygnus Support.
5 This file is part of BFD, the Binary File Descriptor library.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
21 #include "bfd.h"
22 #include "sysdep.h"
23 #include "bfdlink.h"
24 #include "libbfd.h"
25 #include "elf-bfd.h"
26 #include "elf/sh.h"
28 static bfd_reloc_status_type sh_elf_reloc
29 PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
30 static bfd_reloc_status_type sh_elf_ignore_reloc
31 PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
32 static reloc_howto_type *sh_elf_reloc_type_lookup
33 PARAMS ((bfd *, bfd_reloc_code_real_type));
34 static void sh_elf_info_to_howto
35 PARAMS ((bfd *, arelent *, Elf_Internal_Rela *));
36 static boolean sh_elf_set_private_flags
37 PARAMS ((bfd *, flagword));
38 static boolean sh_elf_copy_private_data
39 PARAMS ((bfd *, bfd *));
40 static boolean sh_elf_merge_private_data
41 PARAMS ((bfd *, bfd *));
42 static 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 static boolean sh_elf_check_relocs
59 PARAMS ((bfd *, struct bfd_link_info *, asection *,
60 const Elf_Internal_Rela *));
61 static struct bfd_hash_entry *sh_elf_link_hash_newfunc
62 PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *, const char *));
63 static struct bfd_link_hash_table *sh_elf_link_hash_table_create
64 PARAMS ((bfd *));
65 static boolean sh_elf_adjust_dynamic_symbol
66 PARAMS ((struct bfd_link_info *, struct elf_link_hash_entry *));
67 static boolean sh_elf_size_dynamic_sections
68 PARAMS ((bfd *, struct bfd_link_info *));
69 static boolean sh_elf_finish_dynamic_symbol
70 PARAMS ((bfd *, struct bfd_link_info *, struct elf_link_hash_entry *,
71 Elf_Internal_Sym *));
72 static boolean sh_elf_finish_dynamic_sections
73 PARAMS ((bfd *, struct bfd_link_info *));
75 /* The name of the dynamic interpreter. This is put in the .interp
76 section. */
78 #define ELF_DYNAMIC_INTERPRETER "/usr/lib/libc.so.1"
80 static reloc_howto_type sh_elf_howto_table[] =
82 /* No relocation. */
83 HOWTO (R_SH_NONE, /* type */
84 0, /* rightshift */
85 0, /* size (0 = byte, 1 = short, 2 = long) */
86 0, /* bitsize */
87 false, /* pc_relative */
88 0, /* bitpos */
89 complain_overflow_dont, /* complain_on_overflow */
90 sh_elf_ignore_reloc, /* special_function */
91 "R_SH_NONE", /* name */
92 false, /* partial_inplace */
93 0, /* src_mask */
94 0, /* dst_mask */
95 false), /* pcrel_offset */
97 /* 32 bit absolute relocation. Setting partial_inplace to true and
98 src_mask to a non-zero value is similar to the COFF toolchain. */
99 HOWTO (R_SH_DIR32, /* type */
100 0, /* rightshift */
101 2, /* size (0 = byte, 1 = short, 2 = long) */
102 32, /* bitsize */
103 false, /* pc_relative */
104 0, /* bitpos */
105 complain_overflow_bitfield, /* complain_on_overflow */
106 sh_elf_reloc, /* special_function */
107 "R_SH_DIR32", /* name */
108 true, /* partial_inplace */
109 0xffffffff, /* src_mask */
110 0xffffffff, /* dst_mask */
111 false), /* pcrel_offset */
113 /* 32 bit PC relative relocation. */
114 HOWTO (R_SH_REL32, /* type */
115 0, /* rightshift */
116 2, /* size (0 = byte, 1 = short, 2 = long) */
117 32, /* bitsize */
118 true, /* pc_relative */
119 0, /* bitpos */
120 complain_overflow_signed, /* complain_on_overflow */
121 sh_elf_ignore_reloc, /* special_function */
122 "R_SH_REL32", /* name */
123 false, /* partial_inplace */
124 0, /* src_mask */
125 0xffffffff, /* dst_mask */
126 true), /* pcrel_offset */
128 /* 8 bit PC relative branch divided by 2. */
129 HOWTO (R_SH_DIR8WPN, /* type */
130 1, /* rightshift */
131 1, /* size (0 = byte, 1 = short, 2 = long) */
132 8, /* bitsize */
133 true, /* pc_relative */
134 0, /* bitpos */
135 complain_overflow_signed, /* complain_on_overflow */
136 sh_elf_ignore_reloc, /* special_function */
137 "R_SH_DIR8WPN", /* name */
138 true, /* partial_inplace */
139 0xff, /* src_mask */
140 0xff, /* dst_mask */
141 true), /* pcrel_offset */
143 /* 12 bit PC relative branch divided by 2. */
144 HOWTO (R_SH_IND12W, /* type */
145 1, /* rightshift */
146 1, /* size (0 = byte, 1 = short, 2 = long) */
147 12, /* bitsize */
148 true, /* pc_relative */
149 0, /* bitpos */
150 complain_overflow_signed, /* complain_on_overflow */
151 sh_elf_reloc, /* special_function */
152 "R_SH_IND12W", /* name */
153 true, /* partial_inplace */
154 0xfff, /* src_mask */
155 0xfff, /* dst_mask */
156 true), /* pcrel_offset */
158 /* 8 bit unsigned PC relative divided by 4. */
159 HOWTO (R_SH_DIR8WPL, /* type */
160 2, /* rightshift */
161 1, /* size (0 = byte, 1 = short, 2 = long) */
162 8, /* bitsize */
163 true, /* pc_relative */
164 0, /* bitpos */
165 complain_overflow_unsigned, /* complain_on_overflow */
166 sh_elf_ignore_reloc, /* special_function */
167 "R_SH_DIR8WPL", /* name */
168 true, /* partial_inplace */
169 0xff, /* src_mask */
170 0xff, /* dst_mask */
171 true), /* pcrel_offset */
173 /* 8 bit unsigned PC relative divided by 2. */
174 HOWTO (R_SH_DIR8WPZ, /* type */
175 1, /* rightshift */
176 1, /* size (0 = byte, 1 = short, 2 = long) */
177 8, /* bitsize */
178 true, /* pc_relative */
179 0, /* bitpos */
180 complain_overflow_unsigned, /* complain_on_overflow */
181 sh_elf_ignore_reloc, /* special_function */
182 "R_SH_DIR8WPZ", /* name */
183 true, /* partial_inplace */
184 0xff, /* src_mask */
185 0xff, /* dst_mask */
186 true), /* pcrel_offset */
188 /* 8 bit GBR relative. FIXME: This only makes sense if we have some
189 special symbol for the GBR relative area, and that is not
190 implemented. */
191 HOWTO (R_SH_DIR8BP, /* type */
192 0, /* rightshift */
193 1, /* size (0 = byte, 1 = short, 2 = long) */
194 8, /* bitsize */
195 false, /* pc_relative */
196 0, /* bitpos */
197 complain_overflow_unsigned, /* complain_on_overflow */
198 sh_elf_ignore_reloc, /* special_function */
199 "R_SH_DIR8BP", /* name */
200 false, /* partial_inplace */
201 0, /* src_mask */
202 0xff, /* dst_mask */
203 true), /* pcrel_offset */
205 /* 8 bit GBR relative divided by 2. FIXME: This only makes sense if
206 we have some special symbol for the GBR relative area, and that
207 is not implemented. */
208 HOWTO (R_SH_DIR8W, /* type */
209 1, /* rightshift */
210 1, /* size (0 = byte, 1 = short, 2 = long) */
211 8, /* bitsize */
212 false, /* pc_relative */
213 0, /* bitpos */
214 complain_overflow_unsigned, /* complain_on_overflow */
215 sh_elf_ignore_reloc, /* special_function */
216 "R_SH_DIR8W", /* name */
217 false, /* partial_inplace */
218 0, /* src_mask */
219 0xff, /* dst_mask */
220 true), /* pcrel_offset */
222 /* 8 bit GBR relative divided by 4. FIXME: This only makes sense if
223 we have some special symbol for the GBR relative area, and that
224 is not implemented. */
225 HOWTO (R_SH_DIR8L, /* type */
226 2, /* rightshift */
227 1, /* size (0 = byte, 1 = short, 2 = long) */
228 8, /* bitsize */
229 false, /* pc_relative */
230 0, /* bitpos */
231 complain_overflow_unsigned, /* complain_on_overflow */
232 sh_elf_ignore_reloc, /* special_function */
233 "R_SH_DIR8L", /* name */
234 false, /* partial_inplace */
235 0, /* src_mask */
236 0xff, /* dst_mask */
237 true), /* pcrel_offset */
239 EMPTY_HOWTO (10),
240 EMPTY_HOWTO (11),
241 EMPTY_HOWTO (12),
242 EMPTY_HOWTO (13),
243 EMPTY_HOWTO (14),
244 EMPTY_HOWTO (15),
245 EMPTY_HOWTO (16),
246 EMPTY_HOWTO (17),
247 EMPTY_HOWTO (18),
248 EMPTY_HOWTO (19),
249 EMPTY_HOWTO (20),
250 EMPTY_HOWTO (21),
251 EMPTY_HOWTO (22),
252 EMPTY_HOWTO (23),
253 EMPTY_HOWTO (24),
255 /* The remaining relocs are a GNU extension used for relaxing. The
256 final pass of the linker never needs to do anything with any of
257 these relocs. Any required operations are handled by the
258 relaxation code. */
260 /* A 16 bit switch table entry. This is generated for an expression
261 such as ``.word L1 - L2''. The offset holds the difference
262 between the reloc address and L2. */
263 HOWTO (R_SH_SWITCH16, /* type */
264 0, /* rightshift */
265 1, /* size (0 = byte, 1 = short, 2 = long) */
266 16, /* bitsize */
267 false, /* pc_relative */
268 0, /* bitpos */
269 complain_overflow_unsigned, /* complain_on_overflow */
270 sh_elf_ignore_reloc, /* special_function */
271 "R_SH_SWITCH16", /* name */
272 false, /* partial_inplace */
273 0, /* src_mask */
274 0, /* dst_mask */
275 true), /* pcrel_offset */
277 /* A 32 bit switch table entry. This is generated for an expression
278 such as ``.long L1 - L2''. The offset holds the difference
279 between the reloc address and L2. */
280 HOWTO (R_SH_SWITCH32, /* type */
281 0, /* rightshift */
282 2, /* size (0 = byte, 1 = short, 2 = long) */
283 32, /* bitsize */
284 false, /* pc_relative */
285 0, /* bitpos */
286 complain_overflow_unsigned, /* complain_on_overflow */
287 sh_elf_ignore_reloc, /* special_function */
288 "R_SH_SWITCH32", /* name */
289 false, /* partial_inplace */
290 0, /* src_mask */
291 0, /* dst_mask */
292 true), /* pcrel_offset */
294 /* Indicates a .uses pseudo-op. The compiler will generate .uses
295 pseudo-ops when it finds a function call which can be relaxed.
296 The offset field holds the PC relative offset to the instruction
297 which loads the register used in the function call. */
298 HOWTO (R_SH_USES, /* type */
299 0, /* rightshift */
300 1, /* size (0 = byte, 1 = short, 2 = long) */
301 0, /* bitsize */
302 false, /* pc_relative */
303 0, /* bitpos */
304 complain_overflow_unsigned, /* complain_on_overflow */
305 sh_elf_ignore_reloc, /* special_function */
306 "R_SH_USES", /* name */
307 false, /* partial_inplace */
308 0, /* src_mask */
309 0, /* dst_mask */
310 true), /* pcrel_offset */
312 /* The assembler will generate this reloc for addresses referred to
313 by the register loads associated with USES relocs. The offset
314 field holds the number of times the address is referenced in the
315 object file. */
316 HOWTO (R_SH_COUNT, /* type */
317 0, /* rightshift */
318 1, /* size (0 = byte, 1 = short, 2 = long) */
319 0, /* bitsize */
320 false, /* pc_relative */
321 0, /* bitpos */
322 complain_overflow_unsigned, /* complain_on_overflow */
323 sh_elf_ignore_reloc, /* special_function */
324 "R_SH_COUNT", /* name */
325 false, /* partial_inplace */
326 0, /* src_mask */
327 0, /* dst_mask */
328 true), /* pcrel_offset */
330 /* Indicates an alignment statement. The offset field is the power
331 of 2 to which subsequent portions of the object file must be
332 aligned. */
333 HOWTO (R_SH_ALIGN, /* type */
334 0, /* rightshift */
335 1, /* size (0 = byte, 1 = short, 2 = long) */
336 0, /* bitsize */
337 false, /* pc_relative */
338 0, /* bitpos */
339 complain_overflow_unsigned, /* complain_on_overflow */
340 sh_elf_ignore_reloc, /* special_function */
341 "R_SH_ALIGN", /* name */
342 false, /* partial_inplace */
343 0, /* src_mask */
344 0, /* dst_mask */
345 true), /* pcrel_offset */
347 /* The assembler will generate this reloc before a block of
348 instructions. A section should be processed as assumining it
349 contains data, unless this reloc is seen. */
350 HOWTO (R_SH_CODE, /* type */
351 0, /* rightshift */
352 1, /* size (0 = byte, 1 = short, 2 = long) */
353 0, /* bitsize */
354 false, /* pc_relative */
355 0, /* bitpos */
356 complain_overflow_unsigned, /* complain_on_overflow */
357 sh_elf_ignore_reloc, /* special_function */
358 "R_SH_CODE", /* name */
359 false, /* partial_inplace */
360 0, /* src_mask */
361 0, /* dst_mask */
362 true), /* pcrel_offset */
364 /* The assembler will generate this reloc after a block of
365 instructions when it sees data that is not instructions. */
366 HOWTO (R_SH_DATA, /* type */
367 0, /* rightshift */
368 1, /* size (0 = byte, 1 = short, 2 = long) */
369 0, /* bitsize */
370 false, /* pc_relative */
371 0, /* bitpos */
372 complain_overflow_unsigned, /* complain_on_overflow */
373 sh_elf_ignore_reloc, /* special_function */
374 "R_SH_DATA", /* name */
375 false, /* partial_inplace */
376 0, /* src_mask */
377 0, /* dst_mask */
378 true), /* pcrel_offset */
380 /* The assembler generates this reloc for each label within a block
381 of instructions. This permits the linker to avoid swapping
382 instructions which are the targets of branches. */
383 HOWTO (R_SH_LABEL, /* type */
384 0, /* rightshift */
385 1, /* size (0 = byte, 1 = short, 2 = long) */
386 0, /* bitsize */
387 false, /* pc_relative */
388 0, /* bitpos */
389 complain_overflow_unsigned, /* complain_on_overflow */
390 sh_elf_ignore_reloc, /* special_function */
391 "R_SH_LABEL", /* name */
392 false, /* partial_inplace */
393 0, /* src_mask */
394 0, /* dst_mask */
395 true), /* pcrel_offset */
397 /* An 8 bit switch table entry. This is generated for an expression
398 such as ``.word L1 - L2''. The offset holds the difference
399 between the reloc address and L2. */
400 HOWTO (R_SH_SWITCH8, /* type */
401 0, /* rightshift */
402 0, /* size (0 = byte, 1 = short, 2 = long) */
403 8, /* bitsize */
404 false, /* pc_relative */
405 0, /* bitpos */
406 complain_overflow_unsigned, /* complain_on_overflow */
407 sh_elf_ignore_reloc, /* special_function */
408 "R_SH_SWITCH8", /* name */
409 false, /* partial_inplace */
410 0, /* src_mask */
411 0, /* dst_mask */
412 true), /* pcrel_offset */
414 /* GNU extension to record C++ vtable hierarchy */
415 HOWTO (R_SH_GNU_VTINHERIT, /* type */
416 0, /* rightshift */
417 2, /* size (0 = byte, 1 = short, 2 = long) */
418 0, /* bitsize */
419 false, /* pc_relative */
420 0, /* bitpos */
421 complain_overflow_dont, /* complain_on_overflow */
422 NULL, /* special_function */
423 "R_SH_GNU_VTINHERIT", /* name */
424 false, /* partial_inplace */
425 0, /* src_mask */
426 0, /* dst_mask */
427 false), /* pcrel_offset */
429 /* GNU extension to record C++ vtable member usage */
430 HOWTO (R_SH_GNU_VTENTRY, /* type */
431 0, /* rightshift */
432 2, /* size (0 = byte, 1 = short, 2 = long) */
433 0, /* bitsize */
434 false, /* pc_relative */
435 0, /* bitpos */
436 complain_overflow_dont, /* complain_on_overflow */
437 _bfd_elf_rel_vtable_reloc_fn, /* special_function */
438 "R_SH_GNU_VTENTRY", /* name */
439 false, /* partial_inplace */
440 0, /* src_mask */
441 0, /* dst_mask */
442 false), /* pcrel_offset */
444 /* 8 bit PC relative divided by 2 - but specified in a very odd way. */
445 HOWTO (R_SH_LOOP_START, /* type */
446 1, /* rightshift */
447 1, /* size (0 = byte, 1 = short, 2 = long) */
448 8, /* bitsize */
449 false, /* pc_relative */
450 0, /* bitpos */
451 complain_overflow_signed, /* complain_on_overflow */
452 sh_elf_ignore_reloc, /* special_function */
453 "R_SH_LOOP_START", /* name */
454 true, /* partial_inplace */
455 0xff, /* src_mask */
456 0xff, /* dst_mask */
457 true), /* pcrel_offset */
459 /* 8 bit PC relative divided by 2 - but specified in a very odd way. */
460 HOWTO (R_SH_LOOP_END, /* type */
461 1, /* rightshift */
462 1, /* size (0 = byte, 1 = short, 2 = long) */
463 8, /* bitsize */
464 false, /* pc_relative */
465 0, /* bitpos */
466 complain_overflow_signed, /* complain_on_overflow */
467 sh_elf_ignore_reloc, /* special_function */
468 "R_SH_LOOP_END", /* name */
469 true, /* partial_inplace */
470 0xff, /* src_mask */
471 0xff, /* dst_mask */
472 true), /* pcrel_offset */
474 EMPTY_HOWTO (38),
475 EMPTY_HOWTO (39),
476 EMPTY_HOWTO (40),
477 EMPTY_HOWTO (41),
478 EMPTY_HOWTO (42),
479 EMPTY_HOWTO (43),
480 EMPTY_HOWTO (44),
481 EMPTY_HOWTO (45),
482 EMPTY_HOWTO (46),
483 EMPTY_HOWTO (47),
484 EMPTY_HOWTO (48),
485 EMPTY_HOWTO (49),
486 EMPTY_HOWTO (50),
487 EMPTY_HOWTO (51),
488 EMPTY_HOWTO (52),
489 EMPTY_HOWTO (53),
490 EMPTY_HOWTO (54),
491 EMPTY_HOWTO (55),
492 EMPTY_HOWTO (56),
493 EMPTY_HOWTO (57),
494 EMPTY_HOWTO (58),
495 EMPTY_HOWTO (59),
496 EMPTY_HOWTO (60),
497 EMPTY_HOWTO (61),
498 EMPTY_HOWTO (62),
499 EMPTY_HOWTO (63),
500 EMPTY_HOWTO (64),
501 EMPTY_HOWTO (65),
502 EMPTY_HOWTO (66),
503 EMPTY_HOWTO (67),
504 EMPTY_HOWTO (68),
505 EMPTY_HOWTO (69),
506 EMPTY_HOWTO (70),
507 EMPTY_HOWTO (71),
508 EMPTY_HOWTO (72),
509 EMPTY_HOWTO (73),
510 EMPTY_HOWTO (74),
511 EMPTY_HOWTO (75),
512 EMPTY_HOWTO (76),
513 EMPTY_HOWTO (77),
514 EMPTY_HOWTO (78),
515 EMPTY_HOWTO (79),
516 EMPTY_HOWTO (80),
517 EMPTY_HOWTO (81),
518 EMPTY_HOWTO (82),
519 EMPTY_HOWTO (83),
520 EMPTY_HOWTO (84),
521 EMPTY_HOWTO (85),
522 EMPTY_HOWTO (86),
523 EMPTY_HOWTO (87),
524 EMPTY_HOWTO (88),
525 EMPTY_HOWTO (89),
526 EMPTY_HOWTO (90),
527 EMPTY_HOWTO (91),
528 EMPTY_HOWTO (92),
529 EMPTY_HOWTO (93),
530 EMPTY_HOWTO (94),
531 EMPTY_HOWTO (95),
532 EMPTY_HOWTO (96),
533 EMPTY_HOWTO (97),
534 EMPTY_HOWTO (98),
535 EMPTY_HOWTO (99),
536 EMPTY_HOWTO (100),
537 EMPTY_HOWTO (101),
538 EMPTY_HOWTO (102),
539 EMPTY_HOWTO (103),
540 EMPTY_HOWTO (104),
541 EMPTY_HOWTO (105),
542 EMPTY_HOWTO (106),
543 EMPTY_HOWTO (107),
544 EMPTY_HOWTO (108),
545 EMPTY_HOWTO (109),
546 EMPTY_HOWTO (110),
547 EMPTY_HOWTO (111),
548 EMPTY_HOWTO (112),
549 EMPTY_HOWTO (113),
550 EMPTY_HOWTO (114),
551 EMPTY_HOWTO (115),
552 EMPTY_HOWTO (116),
553 EMPTY_HOWTO (117),
554 EMPTY_HOWTO (118),
555 EMPTY_HOWTO (119),
556 EMPTY_HOWTO (120),
557 EMPTY_HOWTO (121),
558 EMPTY_HOWTO (122),
559 EMPTY_HOWTO (123),
560 EMPTY_HOWTO (124),
561 EMPTY_HOWTO (125),
562 EMPTY_HOWTO (126),
563 EMPTY_HOWTO (127),
564 EMPTY_HOWTO (128),
565 EMPTY_HOWTO (129),
566 EMPTY_HOWTO (130),
567 EMPTY_HOWTO (131),
568 EMPTY_HOWTO (132),
569 EMPTY_HOWTO (133),
570 EMPTY_HOWTO (134),
571 EMPTY_HOWTO (135),
572 EMPTY_HOWTO (136),
573 EMPTY_HOWTO (137),
574 EMPTY_HOWTO (138),
575 EMPTY_HOWTO (139),
576 EMPTY_HOWTO (140),
577 EMPTY_HOWTO (141),
578 EMPTY_HOWTO (142),
579 EMPTY_HOWTO (143),
580 EMPTY_HOWTO (144),
581 EMPTY_HOWTO (145),
582 EMPTY_HOWTO (146),
583 EMPTY_HOWTO (147),
584 EMPTY_HOWTO (148),
585 EMPTY_HOWTO (149),
586 EMPTY_HOWTO (150),
587 EMPTY_HOWTO (151),
588 EMPTY_HOWTO (152),
589 EMPTY_HOWTO (153),
590 EMPTY_HOWTO (154),
591 EMPTY_HOWTO (155),
592 EMPTY_HOWTO (156),
593 EMPTY_HOWTO (157),
594 EMPTY_HOWTO (158),
595 EMPTY_HOWTO (159),
597 HOWTO (R_SH_GOT32, /* type */
598 0, /* rightshift */
599 2, /* size (0 = byte, 1 = short, 2 = long) */
600 32, /* bitsize */
601 false, /* pc_relative */
602 0, /* bitpos */
603 complain_overflow_bitfield, /* complain_on_overflow */
604 bfd_elf_generic_reloc, /* */
605 "R_SH_GOT32", /* name */
606 true, /* partial_inplace */
607 0xffffffff, /* src_mask */
608 0xffffffff, /* dst_mask */
609 false), /* pcrel_offset */
611 HOWTO (R_SH_PLT32, /* type */
612 0, /* rightshift */
613 2, /* size (0 = byte, 1 = short, 2 = long) */
614 32, /* bitsize */
615 true, /* pc_relative */
616 0, /* bitpos */
617 complain_overflow_bitfield, /* complain_on_overflow */
618 bfd_elf_generic_reloc, /* */
619 "R_SH_PLT32", /* name */
620 true, /* partial_inplace */
621 0xffffffff, /* src_mask */
622 0xffffffff, /* dst_mask */
623 true), /* pcrel_offset */
625 HOWTO (R_SH_COPY, /* type */
626 0, /* rightshift */
627 2, /* size (0 = byte, 1 = short, 2 = long) */
628 32, /* bitsize */
629 false, /* pc_relative */
630 0, /* bitpos */
631 complain_overflow_bitfield, /* complain_on_overflow */
632 bfd_elf_generic_reloc, /* */
633 "R_SH_COPY", /* name */
634 true, /* partial_inplace */
635 0xffffffff, /* src_mask */
636 0xffffffff, /* dst_mask */
637 false), /* pcrel_offset */
639 HOWTO (R_SH_GLOB_DAT, /* type */
640 0, /* rightshift */
641 2, /* size (0 = byte, 1 = short, 2 = long) */
642 32, /* bitsize */
643 false, /* pc_relative */
644 0, /* bitpos */
645 complain_overflow_bitfield, /* complain_on_overflow */
646 bfd_elf_generic_reloc, /* */
647 "R_SH_GLOB_DAT", /* name */
648 true, /* partial_inplace */
649 0xffffffff, /* src_mask */
650 0xffffffff, /* dst_mask */
651 false), /* pcrel_offset */
653 HOWTO (R_SH_JMP_SLOT, /* type */
654 0, /* rightshift */
655 2, /* size (0 = byte, 1 = short, 2 = long) */
656 32, /* bitsize */
657 false, /* pc_relative */
658 0, /* bitpos */
659 complain_overflow_bitfield, /* complain_on_overflow */
660 bfd_elf_generic_reloc, /* */
661 "R_SH_JMP_SLOT", /* name */
662 true, /* partial_inplace */
663 0xffffffff, /* src_mask */
664 0xffffffff, /* dst_mask */
665 false), /* pcrel_offset */
667 HOWTO (R_SH_RELATIVE, /* type */
668 0, /* rightshift */
669 2, /* size (0 = byte, 1 = short, 2 = long) */
670 32, /* bitsize */
671 false, /* pc_relative */
672 0, /* bitpos */
673 complain_overflow_bitfield, /* complain_on_overflow */
674 bfd_elf_generic_reloc, /* */
675 "R_SH_RELATIVE", /* name */
676 true, /* partial_inplace */
677 0xffffffff, /* src_mask */
678 0xffffffff, /* dst_mask */
679 false), /* pcrel_offset */
681 HOWTO (R_SH_GOTOFF, /* type */
682 0, /* rightshift */
683 2, /* size (0 = byte, 1 = short, 2 = long) */
684 32, /* bitsize */
685 false, /* pc_relative */
686 0, /* bitpos */
687 complain_overflow_bitfield, /* complain_on_overflow */
688 bfd_elf_generic_reloc, /* */
689 "R_SH_GOTOFF", /* name */
690 true, /* partial_inplace */
691 0xffffffff, /* src_mask */
692 0xffffffff, /* dst_mask */
693 false), /* pcrel_offset */
695 HOWTO (R_SH_GOTPC, /* type */
696 0, /* rightshift */
697 2, /* size (0 = byte, 1 = short, 2 = long) */
698 32, /* bitsize */
699 true, /* pc_relative */
700 0, /* bitpos */
701 complain_overflow_bitfield, /* complain_on_overflow */
702 bfd_elf_generic_reloc, /* */
703 "R_SH_GOTPC", /* name */
704 true, /* partial_inplace */
705 0xffffffff, /* src_mask */
706 0xffffffff, /* dst_mask */
707 true), /* pcrel_offset */
711 static bfd_reloc_status_type
712 sh_elf_reloc_loop (r_type, input_bfd, input_section, contents, addr,
713 symbol_section, start, end)
714 int r_type ATTRIBUTE_UNUSED;
715 bfd *input_bfd;
716 asection *input_section;
717 bfd_byte *contents;
718 bfd_vma addr;
719 asection *symbol_section;
720 bfd_vma start, end;
722 static bfd_vma last_addr;
723 static asection *last_symbol_section;
724 bfd_byte *free_contents = NULL;
725 bfd_byte *start_ptr, *ptr, *last_ptr;
726 int diff, cum_diff;
727 bfd_signed_vma x;
728 int insn;
730 /* Sanity check the address. */
731 if (addr > input_section->_raw_size)
732 return bfd_reloc_outofrange;
734 /* We require the start and end relocations to be processed consecutively -
735 although we allow then to be processed forwards or backwards. */
736 if (! last_addr)
738 last_addr = addr;
739 last_symbol_section = symbol_section;
740 return bfd_reloc_ok;
742 if (last_addr != addr)
743 abort ();
744 last_addr = 0;
746 if (! symbol_section || last_symbol_section != symbol_section || end < start)
747 return bfd_reloc_outofrange;
749 /* Get the symbol_section contents. */
750 if (symbol_section != input_section)
752 if (elf_section_data (symbol_section)->this_hdr.contents != NULL)
753 contents = elf_section_data (symbol_section)->this_hdr.contents;
754 else
756 free_contents = contents
757 = (bfd_byte *) bfd_malloc (symbol_section->_raw_size);
758 if (contents == NULL)
759 return bfd_reloc_outofrange;
760 if (! bfd_get_section_contents (input_bfd, symbol_section, contents,
761 (file_ptr) 0,
762 symbol_section->_raw_size))
764 free (contents);
765 return bfd_reloc_outofrange;
769 #define IS_PPI(PTR) ((bfd_get_16 (input_bfd, (PTR)) & 0xfc00) == 0xf800)
770 start_ptr = contents + start;
771 for (cum_diff = -6, ptr = contents + end; cum_diff < 0 && ptr > start_ptr;)
773 for (last_ptr = ptr, ptr -= 4; ptr >= start_ptr && IS_PPI (ptr);)
774 ptr -= 2;
775 ptr += 2;
776 diff = (last_ptr - ptr) >> 1;
777 cum_diff += diff & 1;
778 cum_diff += diff;
780 /* Calculate the start / end values to load into rs / re minus four -
781 so that will cancel out the four we would otherwise have to add to
782 addr to get the value to subtract in order to get relative addressing. */
783 if (cum_diff >= 0)
785 start -= 4;
786 end = (ptr + cum_diff * 2) - contents;
788 else
790 bfd_vma start0 = start - 4;
792 while (start0 >= 0 && IS_PPI (contents + start0))
793 start0 -= 2;
794 start0 = start - 2 - ((start - start0) & 2);
795 start = start0 - cum_diff - 2;
796 end = start0;
799 if (free_contents)
800 free (free_contents);
802 insn = bfd_get_16 (input_bfd, contents + addr);
804 x = (insn & 0x200 ? end : start) - addr;
805 if (input_section != symbol_section)
806 x += ((symbol_section->output_section->vma + symbol_section->output_offset)
807 - (input_section->output_section->vma
808 + input_section->output_offset));
809 x >>= 1;
810 if (x < -128 || x > 127)
811 return bfd_reloc_overflow;
813 x = (insn & ~0xff) | (x & 0xff);
814 bfd_put_16 (input_bfd, x, contents + addr);
816 return bfd_reloc_ok;
819 /* This function is used for normal relocs. This used to be like the COFF
820 function, and is almost certainly incorrect for other ELF targets. */
822 static bfd_reloc_status_type
823 sh_elf_reloc (abfd, reloc_entry, symbol_in, data, input_section, output_bfd,
824 error_message)
825 bfd *abfd;
826 arelent *reloc_entry;
827 asymbol *symbol_in;
828 PTR data;
829 asection *input_section;
830 bfd *output_bfd;
831 char **error_message ATTRIBUTE_UNUSED;
833 unsigned long insn;
834 bfd_vma sym_value;
835 enum elf_sh_reloc_type r_type;
836 bfd_vma addr = reloc_entry->address;
837 bfd_byte *hit_data = addr + (bfd_byte *) data;
839 r_type = (enum elf_sh_reloc_type) reloc_entry->howto->type;
841 if (output_bfd != NULL)
843 /* Partial linking--do nothing. */
844 reloc_entry->address += input_section->output_offset;
845 return bfd_reloc_ok;
848 /* Almost all relocs have to do with relaxing. If any work must be
849 done for them, it has been done in sh_relax_section. */
850 if (r_type == R_SH_IND12W && (symbol_in->flags & BSF_LOCAL) != 0)
851 return bfd_reloc_ok;
853 if (symbol_in != NULL
854 && bfd_is_und_section (symbol_in->section))
855 return bfd_reloc_undefined;
857 if (bfd_is_com_section (symbol_in->section))
858 sym_value = 0;
859 else
860 sym_value = (symbol_in->value +
861 symbol_in->section->output_section->vma +
862 symbol_in->section->output_offset);
864 switch (r_type)
866 case R_SH_DIR32:
867 insn = bfd_get_32 (abfd, hit_data);
868 insn += sym_value + reloc_entry->addend;
869 bfd_put_32 (abfd, insn, hit_data);
870 break;
871 case R_SH_IND12W:
872 insn = bfd_get_16 (abfd, hit_data);
873 sym_value += reloc_entry->addend;
874 sym_value -= (input_section->output_section->vma
875 + input_section->output_offset
876 + addr
877 + 4);
878 sym_value += (insn & 0xfff) << 1;
879 if (insn & 0x800)
880 sym_value -= 0x1000;
881 insn = (insn & 0xf000) | (sym_value & 0xfff);
882 bfd_put_16 (abfd, insn, hit_data);
883 if (sym_value < (bfd_vma) -0x1000 || sym_value >= 0x1000)
884 return bfd_reloc_overflow;
885 break;
886 default:
887 abort ();
888 break;
891 return bfd_reloc_ok;
894 /* This function is used for relocs which are only used for relaxing,
895 which the linker should otherwise ignore. */
897 static bfd_reloc_status_type
898 sh_elf_ignore_reloc (abfd, reloc_entry, symbol, data, input_section,
899 output_bfd, error_message)
900 bfd *abfd ATTRIBUTE_UNUSED;
901 arelent *reloc_entry;
902 asymbol *symbol ATTRIBUTE_UNUSED;
903 PTR data ATTRIBUTE_UNUSED;
904 asection *input_section;
905 bfd *output_bfd;
906 char **error_message ATTRIBUTE_UNUSED;
908 if (output_bfd != NULL)
909 reloc_entry->address += input_section->output_offset;
910 return bfd_reloc_ok;
913 /* This structure is used to map BFD reloc codes to SH ELF relocs. */
915 struct elf_reloc_map
917 bfd_reloc_code_real_type bfd_reloc_val;
918 unsigned char elf_reloc_val;
921 /* An array mapping BFD reloc codes to SH ELF relocs. */
923 static const struct elf_reloc_map sh_reloc_map[] =
925 { BFD_RELOC_NONE, R_SH_NONE },
926 { BFD_RELOC_32, R_SH_DIR32 },
927 { BFD_RELOC_CTOR, R_SH_DIR32 },
928 { BFD_RELOC_32_PCREL, R_SH_REL32 },
929 { BFD_RELOC_SH_PCDISP8BY2, R_SH_DIR8WPN },
930 { BFD_RELOC_SH_PCDISP12BY2, R_SH_IND12W },
931 { BFD_RELOC_SH_PCRELIMM8BY2, R_SH_DIR8WPZ },
932 { BFD_RELOC_SH_PCRELIMM8BY4, R_SH_DIR8WPL },
933 { BFD_RELOC_8_PCREL, R_SH_SWITCH8 },
934 { BFD_RELOC_SH_SWITCH16, R_SH_SWITCH16 },
935 { BFD_RELOC_SH_SWITCH32, R_SH_SWITCH32 },
936 { BFD_RELOC_SH_USES, R_SH_USES },
937 { BFD_RELOC_SH_COUNT, R_SH_COUNT },
938 { BFD_RELOC_SH_ALIGN, R_SH_ALIGN },
939 { BFD_RELOC_SH_CODE, R_SH_CODE },
940 { BFD_RELOC_SH_DATA, R_SH_DATA },
941 { BFD_RELOC_SH_LABEL, R_SH_LABEL },
942 { BFD_RELOC_VTABLE_INHERIT, R_SH_GNU_VTINHERIT },
943 { BFD_RELOC_VTABLE_ENTRY, R_SH_GNU_VTENTRY },
944 { BFD_RELOC_SH_LOOP_START, R_SH_LOOP_START },
945 { BFD_RELOC_SH_LOOP_END, R_SH_LOOP_END },
946 { BFD_RELOC_32_GOT_PCREL, R_SH_GOT32 },
947 { BFD_RELOC_32_PLT_PCREL, R_SH_PLT32 },
948 { BFD_RELOC_SH_COPY, R_SH_COPY },
949 { BFD_RELOC_SH_GLOB_DAT, R_SH_GLOB_DAT },
950 { BFD_RELOC_SH_JMP_SLOT, R_SH_JMP_SLOT },
951 { BFD_RELOC_SH_RELATIVE, R_SH_RELATIVE },
952 { BFD_RELOC_32_GOTOFF, R_SH_GOTOFF },
953 { BFD_RELOC_SH_GOTPC, R_SH_GOTPC },
956 /* Given a BFD reloc code, return the howto structure for the
957 corresponding SH ELf reloc. */
959 static reloc_howto_type *
960 sh_elf_reloc_type_lookup (abfd, code)
961 bfd *abfd ATTRIBUTE_UNUSED;
962 bfd_reloc_code_real_type code;
964 unsigned int i;
966 for (i = 0; i < sizeof (sh_reloc_map) / sizeof (struct elf_reloc_map); i++)
968 if (sh_reloc_map[i].bfd_reloc_val == code)
969 return &sh_elf_howto_table[(int) sh_reloc_map[i].elf_reloc_val];
972 return NULL;
975 /* Given an ELF reloc, fill in the howto field of a relent. */
977 static void
978 sh_elf_info_to_howto (abfd, cache_ptr, dst)
979 bfd *abfd ATTRIBUTE_UNUSED;
980 arelent *cache_ptr;
981 Elf_Internal_Rela *dst;
983 unsigned int r;
985 r = ELF32_R_TYPE (dst->r_info);
987 BFD_ASSERT (r < (unsigned int) R_SH_max);
988 BFD_ASSERT (r < R_SH_FIRST_INVALID_RELOC || r > R_SH_LAST_INVALID_RELOC);
989 BFD_ASSERT (r < R_SH_FIRST_INVALID_RELOC_2 || r > R_SH_LAST_INVALID_RELOC_2);
991 cache_ptr->howto = &sh_elf_howto_table[r];
994 /* This function handles relaxing for SH ELF. See the corresponding
995 function in coff-sh.c for a description of what this does. FIXME:
996 There is a lot of duplication here between this code and the COFF
997 specific code. The format of relocs and symbols is wound deeply
998 into this code, but it would still be better if the duplication
999 could be eliminated somehow. Note in particular that although both
1000 functions use symbols like R_SH_CODE, those symbols have different
1001 values; in coff-sh.c they come from include/coff/sh.h, whereas here
1002 they come from enum elf_sh_reloc_type in include/elf/sh.h. */
1004 static boolean
1005 sh_elf_relax_section (abfd, sec, link_info, again)
1006 bfd *abfd;
1007 asection *sec;
1008 struct bfd_link_info *link_info;
1009 boolean *again;
1011 Elf_Internal_Shdr *symtab_hdr;
1012 Elf_Internal_Rela *internal_relocs;
1013 Elf_Internal_Rela *free_relocs = NULL;
1014 boolean have_code;
1015 Elf_Internal_Rela *irel, *irelend;
1016 bfd_byte *contents = NULL;
1017 bfd_byte *free_contents = NULL;
1018 Elf32_External_Sym *extsyms = NULL;
1019 Elf32_External_Sym *free_extsyms = NULL;
1021 *again = false;
1023 if (link_info->relocateable
1024 || (sec->flags & SEC_RELOC) == 0
1025 || sec->reloc_count == 0)
1026 return true;
1028 /* If this is the first time we have been called for this section,
1029 initialize the cooked size. */
1030 if (sec->_cooked_size == 0)
1031 sec->_cooked_size = sec->_raw_size;
1033 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1035 internal_relocs = (_bfd_elf32_link_read_relocs
1036 (abfd, sec, (PTR) NULL, (Elf_Internal_Rela *) NULL,
1037 link_info->keep_memory));
1038 if (internal_relocs == NULL)
1039 goto error_return;
1040 if (! link_info->keep_memory)
1041 free_relocs = internal_relocs;
1043 have_code = false;
1045 irelend = internal_relocs + sec->reloc_count;
1046 for (irel = internal_relocs; irel < irelend; irel++)
1048 bfd_vma laddr, paddr, symval;
1049 unsigned short insn;
1050 Elf_Internal_Rela *irelfn, *irelscan, *irelcount;
1051 bfd_signed_vma foff;
1053 if (ELF32_R_TYPE (irel->r_info) == (int) R_SH_CODE)
1054 have_code = true;
1056 if (ELF32_R_TYPE (irel->r_info) != (int) R_SH_USES)
1057 continue;
1059 /* Get the section contents. */
1060 if (contents == NULL)
1062 if (elf_section_data (sec)->this_hdr.contents != NULL)
1063 contents = elf_section_data (sec)->this_hdr.contents;
1064 else
1066 contents = (bfd_byte *) bfd_malloc (sec->_raw_size);
1067 if (contents == NULL)
1068 goto error_return;
1069 free_contents = contents;
1071 if (! bfd_get_section_contents (abfd, sec, contents,
1072 (file_ptr) 0, sec->_raw_size))
1073 goto error_return;
1077 /* The r_addend field of the R_SH_USES reloc will point us to
1078 the register load. The 4 is because the r_addend field is
1079 computed as though it were a jump offset, which are based
1080 from 4 bytes after the jump instruction. */
1081 laddr = irel->r_offset + 4 + irel->r_addend;
1082 if (laddr >= sec->_raw_size)
1084 (*_bfd_error_handler) (_("%s: 0x%lx: warning: bad R_SH_USES offset"),
1085 bfd_get_filename (abfd),
1086 (unsigned long) irel->r_offset);
1087 continue;
1089 insn = bfd_get_16 (abfd, contents + laddr);
1091 /* If the instruction is not mov.l NN,rN, we don't know what to
1092 do. */
1093 if ((insn & 0xf000) != 0xd000)
1095 ((*_bfd_error_handler)
1096 (_("%s: 0x%lx: warning: R_SH_USES points to unrecognized insn 0x%x"),
1097 bfd_get_filename (abfd), (unsigned long) irel->r_offset, insn));
1098 continue;
1101 /* Get the address from which the register is being loaded. The
1102 displacement in the mov.l instruction is quadrupled. It is a
1103 displacement from four bytes after the movl instruction, but,
1104 before adding in the PC address, two least significant bits
1105 of the PC are cleared. We assume that the section is aligned
1106 on a four byte boundary. */
1107 paddr = insn & 0xff;
1108 paddr *= 4;
1109 paddr += (laddr + 4) &~ 3;
1110 if (paddr >= sec->_raw_size)
1112 ((*_bfd_error_handler)
1113 (_("%s: 0x%lx: warning: bad R_SH_USES load offset"),
1114 bfd_get_filename (abfd), (unsigned long) irel->r_offset));
1115 continue;
1118 /* Get the reloc for the address from which the register is
1119 being loaded. This reloc will tell us which function is
1120 actually being called. */
1121 for (irelfn = internal_relocs; irelfn < irelend; irelfn++)
1122 if (irelfn->r_offset == paddr
1123 && ELF32_R_TYPE (irelfn->r_info) == (int) R_SH_DIR32)
1124 break;
1125 if (irelfn >= irelend)
1127 ((*_bfd_error_handler)
1128 (_("%s: 0x%lx: warning: could not find expected reloc"),
1129 bfd_get_filename (abfd), (unsigned long) paddr));
1130 continue;
1133 /* Read this BFD's symbols if we haven't done so already. */
1134 if (extsyms == NULL)
1136 if (symtab_hdr->contents != NULL)
1137 extsyms = (Elf32_External_Sym *) symtab_hdr->contents;
1138 else
1140 extsyms = ((Elf32_External_Sym *)
1141 bfd_malloc (symtab_hdr->sh_size));
1142 if (extsyms == NULL)
1143 goto error_return;
1144 free_extsyms = extsyms;
1145 if (bfd_seek (abfd, symtab_hdr->sh_offset, SEEK_SET) != 0
1146 || (bfd_read (extsyms, 1, symtab_hdr->sh_size, abfd)
1147 != symtab_hdr->sh_size))
1148 goto error_return;
1152 /* Get the value of the symbol referred to by the reloc. */
1153 if (ELF32_R_SYM (irelfn->r_info) < symtab_hdr->sh_info)
1155 Elf_Internal_Sym isym;
1157 /* A local symbol. */
1158 bfd_elf32_swap_symbol_in (abfd,
1159 extsyms + ELF32_R_SYM (irelfn->r_info),
1160 &isym);
1162 if (isym.st_shndx != _bfd_elf_section_from_bfd_section (abfd, sec))
1164 ((*_bfd_error_handler)
1165 (_("%s: 0x%lx: warning: symbol in unexpected section"),
1166 bfd_get_filename (abfd), (unsigned long) paddr));
1167 continue;
1170 symval = (isym.st_value
1171 + sec->output_section->vma
1172 + sec->output_offset);
1174 else
1176 unsigned long indx;
1177 struct elf_link_hash_entry *h;
1179 indx = ELF32_R_SYM (irelfn->r_info) - symtab_hdr->sh_info;
1180 h = elf_sym_hashes (abfd)[indx];
1181 BFD_ASSERT (h != NULL);
1182 if (h->root.type != bfd_link_hash_defined
1183 && h->root.type != bfd_link_hash_defweak)
1185 /* This appears to be a reference to an undefined
1186 symbol. Just ignore it--it will be caught by the
1187 regular reloc processing. */
1188 continue;
1191 symval = (h->root.u.def.value
1192 + h->root.u.def.section->output_section->vma
1193 + h->root.u.def.section->output_offset);
1196 symval += bfd_get_32 (abfd, contents + paddr);
1198 /* See if this function call can be shortened. */
1199 foff = (symval
1200 - (irel->r_offset
1201 + sec->output_section->vma
1202 + sec->output_offset
1203 + 4));
1204 if (foff < -0x1000 || foff >= 0x1000)
1206 /* After all that work, we can't shorten this function call. */
1207 continue;
1210 /* Shorten the function call. */
1212 /* For simplicity of coding, we are going to modify the section
1213 contents, the section relocs, and the BFD symbol table. We
1214 must tell the rest of the code not to free up this
1215 information. It would be possible to instead create a table
1216 of changes which have to be made, as is done in coff-mips.c;
1217 that would be more work, but would require less memory when
1218 the linker is run. */
1220 elf_section_data (sec)->relocs = internal_relocs;
1221 free_relocs = NULL;
1223 elf_section_data (sec)->this_hdr.contents = contents;
1224 free_contents = NULL;
1226 symtab_hdr->contents = (bfd_byte *) extsyms;
1227 free_extsyms = NULL;
1229 /* Replace the jsr with a bsr. */
1231 /* Change the R_SH_USES reloc into an R_SH_IND12W reloc, and
1232 replace the jsr with a bsr. */
1233 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irelfn->r_info), R_SH_IND12W);
1234 if (ELF32_R_SYM (irelfn->r_info) < symtab_hdr->sh_info)
1236 /* If this needs to be changed because of future relaxing,
1237 it will be handled here like other internal IND12W
1238 relocs. */
1239 bfd_put_16 (abfd,
1240 0xb000 | ((foff >> 1) & 0xfff),
1241 contents + irel->r_offset);
1243 else
1245 /* We can't fully resolve this yet, because the external
1246 symbol value may be changed by future relaxing. We let
1247 the final link phase handle it. */
1248 bfd_put_16 (abfd, 0xb000, contents + irel->r_offset);
1251 /* See if there is another R_SH_USES reloc referring to the same
1252 register load. */
1253 for (irelscan = internal_relocs; irelscan < irelend; irelscan++)
1254 if (ELF32_R_TYPE (irelscan->r_info) == (int) R_SH_USES
1255 && laddr == irelscan->r_offset + 4 + irelscan->r_addend)
1256 break;
1257 if (irelscan < irelend)
1259 /* Some other function call depends upon this register load,
1260 and we have not yet converted that function call.
1261 Indeed, we may never be able to convert it. There is
1262 nothing else we can do at this point. */
1263 continue;
1266 /* Look for a R_SH_COUNT reloc on the location where the
1267 function address is stored. Do this before deleting any
1268 bytes, to avoid confusion about the address. */
1269 for (irelcount = internal_relocs; irelcount < irelend; irelcount++)
1270 if (irelcount->r_offset == paddr
1271 && ELF32_R_TYPE (irelcount->r_info) == (int) R_SH_COUNT)
1272 break;
1274 /* Delete the register load. */
1275 if (! sh_elf_relax_delete_bytes (abfd, sec, laddr, 2))
1276 goto error_return;
1278 /* That will change things, so, just in case it permits some
1279 other function call to come within range, we should relax
1280 again. Note that this is not required, and it may be slow. */
1281 *again = true;
1283 /* Now check whether we got a COUNT reloc. */
1284 if (irelcount >= irelend)
1286 ((*_bfd_error_handler)
1287 (_("%s: 0x%lx: warning: could not find expected COUNT reloc"),
1288 bfd_get_filename (abfd), (unsigned long) paddr));
1289 continue;
1292 /* The number of uses is stored in the r_addend field. We've
1293 just deleted one. */
1294 if (irelcount->r_addend == 0)
1296 ((*_bfd_error_handler) (_("%s: 0x%lx: warning: bad count"),
1297 bfd_get_filename (abfd),
1298 (unsigned long) paddr));
1299 continue;
1302 --irelcount->r_addend;
1304 /* If there are no more uses, we can delete the address. Reload
1305 the address from irelfn, in case it was changed by the
1306 previous call to sh_elf_relax_delete_bytes. */
1307 if (irelcount->r_addend == 0)
1309 if (! sh_elf_relax_delete_bytes (abfd, sec, irelfn->r_offset, 4))
1310 goto error_return;
1313 /* We've done all we can with that function call. */
1316 /* Look for load and store instructions that we can align on four
1317 byte boundaries. */
1318 if (have_code)
1320 boolean swapped;
1322 /* Get the section contents. */
1323 if (contents == NULL)
1325 if (elf_section_data (sec)->this_hdr.contents != NULL)
1326 contents = elf_section_data (sec)->this_hdr.contents;
1327 else
1329 contents = (bfd_byte *) bfd_malloc (sec->_raw_size);
1330 if (contents == NULL)
1331 goto error_return;
1332 free_contents = contents;
1334 if (! bfd_get_section_contents (abfd, sec, contents,
1335 (file_ptr) 0, sec->_raw_size))
1336 goto error_return;
1340 if (! sh_elf_align_loads (abfd, sec, internal_relocs, contents,
1341 &swapped))
1342 goto error_return;
1344 if (swapped)
1346 elf_section_data (sec)->relocs = internal_relocs;
1347 free_relocs = NULL;
1349 elf_section_data (sec)->this_hdr.contents = contents;
1350 free_contents = NULL;
1352 symtab_hdr->contents = (bfd_byte *) extsyms;
1353 free_extsyms = NULL;
1357 if (free_relocs != NULL)
1359 free (free_relocs);
1360 free_relocs = NULL;
1363 if (free_contents != NULL)
1365 if (! link_info->keep_memory)
1366 free (free_contents);
1367 else
1369 /* Cache the section contents for elf_link_input_bfd. */
1370 elf_section_data (sec)->this_hdr.contents = contents;
1372 free_contents = NULL;
1375 if (free_extsyms != NULL)
1377 if (! link_info->keep_memory)
1378 free (free_extsyms);
1379 else
1381 /* Cache the symbols for elf_link_input_bfd. */
1382 symtab_hdr->contents = extsyms;
1384 free_extsyms = NULL;
1387 return true;
1389 error_return:
1390 if (free_relocs != NULL)
1391 free (free_relocs);
1392 if (free_contents != NULL)
1393 free (free_contents);
1394 if (free_extsyms != NULL)
1395 free (free_extsyms);
1396 return false;
1399 /* Delete some bytes from a section while relaxing. FIXME: There is a
1400 lot of duplication between this function and sh_relax_delete_bytes
1401 in coff-sh.c. */
1403 static boolean
1404 sh_elf_relax_delete_bytes (abfd, sec, addr, count)
1405 bfd *abfd;
1406 asection *sec;
1407 bfd_vma addr;
1408 int count;
1410 Elf_Internal_Shdr *symtab_hdr;
1411 Elf32_External_Sym *extsyms;
1412 int shndx, index;
1413 bfd_byte *contents;
1414 Elf_Internal_Rela *irel, *irelend;
1415 Elf_Internal_Rela *irelalign;
1416 bfd_vma toaddr;
1417 Elf32_External_Sym *esym, *esymend;
1418 struct elf_link_hash_entry *sym_hash;
1419 asection *o;
1421 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1422 extsyms = (Elf32_External_Sym *) symtab_hdr->contents;
1424 shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
1426 contents = elf_section_data (sec)->this_hdr.contents;
1428 /* The deletion must stop at the next ALIGN reloc for an aligment
1429 power larger than the number of bytes we are deleting. */
1431 irelalign = NULL;
1432 toaddr = sec->_cooked_size;
1434 irel = elf_section_data (sec)->relocs;
1435 irelend = irel + sec->reloc_count;
1436 for (; irel < irelend; irel++)
1438 if (ELF32_R_TYPE (irel->r_info) == (int) R_SH_ALIGN
1439 && irel->r_offset > addr
1440 && count < (1 << irel->r_addend))
1442 irelalign = irel;
1443 toaddr = irel->r_offset;
1444 break;
1448 /* Actually delete the bytes. */
1449 memmove (contents + addr, contents + addr + count, toaddr - addr - count);
1450 if (irelalign == NULL)
1451 sec->_cooked_size -= count;
1452 else
1454 int i;
1456 #define NOP_OPCODE (0x0009)
1458 BFD_ASSERT ((count & 1) == 0);
1459 for (i = 0; i < count; i += 2)
1460 bfd_put_16 (abfd, NOP_OPCODE, contents + toaddr - count + i);
1463 /* Adjust all the relocs. */
1464 for (irel = elf_section_data (sec)->relocs; irel < irelend; irel++)
1466 bfd_vma nraddr, stop;
1467 bfd_vma start = 0;
1468 int insn = 0;
1469 Elf_Internal_Sym sym;
1470 int off, adjust, oinsn;
1471 bfd_signed_vma voff = 0;
1472 boolean overflow;
1474 /* Get the new reloc address. */
1475 nraddr = irel->r_offset;
1476 if ((irel->r_offset > addr
1477 && irel->r_offset < toaddr)
1478 || (ELF32_R_TYPE (irel->r_info) == (int) R_SH_ALIGN
1479 && irel->r_offset == toaddr))
1480 nraddr -= count;
1482 /* See if this reloc was for the bytes we have deleted, in which
1483 case we no longer care about it. Don't delete relocs which
1484 represent addresses, though. */
1485 if (irel->r_offset >= addr
1486 && irel->r_offset < addr + count
1487 && ELF32_R_TYPE (irel->r_info) != (int) R_SH_ALIGN
1488 && ELF32_R_TYPE (irel->r_info) != (int) R_SH_CODE
1489 && ELF32_R_TYPE (irel->r_info) != (int) R_SH_DATA
1490 && ELF32_R_TYPE (irel->r_info) != (int) R_SH_LABEL)
1491 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
1492 (int) R_SH_NONE);
1494 /* If this is a PC relative reloc, see if the range it covers
1495 includes the bytes we have deleted. */
1496 switch ((enum elf_sh_reloc_type) ELF32_R_TYPE (irel->r_info))
1498 default:
1499 break;
1501 case R_SH_DIR8WPN:
1502 case R_SH_IND12W:
1503 case R_SH_DIR8WPZ:
1504 case R_SH_DIR8WPL:
1505 start = irel->r_offset;
1506 insn = bfd_get_16 (abfd, contents + nraddr);
1507 break;
1510 switch ((enum elf_sh_reloc_type) ELF32_R_TYPE (irel->r_info))
1512 default:
1513 start = stop = addr;
1514 break;
1516 case R_SH_DIR32:
1517 /* If this reloc is against a symbol defined in this
1518 section, and the symbol will not be adjusted below, we
1519 must check the addend to see it will put the value in
1520 range to be adjusted, and hence must be changed. */
1521 if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
1523 bfd_elf32_swap_symbol_in (abfd,
1524 extsyms + ELF32_R_SYM (irel->r_info),
1525 &sym);
1526 if (sym.st_shndx == shndx
1527 && (sym.st_value <= addr
1528 || sym.st_value >= toaddr))
1530 bfd_vma val;
1532 val = bfd_get_32 (abfd, contents + nraddr);
1533 val += sym.st_value;
1534 if (val > addr && val < toaddr)
1535 bfd_put_32 (abfd, val - count, contents + nraddr);
1538 start = stop = addr;
1539 break;
1541 case R_SH_DIR8WPN:
1542 off = insn & 0xff;
1543 if (off & 0x80)
1544 off -= 0x100;
1545 stop = (bfd_vma) ((bfd_signed_vma) start + 4 + off * 2);
1546 break;
1548 case R_SH_IND12W:
1549 if (ELF32_R_SYM (irel->r_info) >= symtab_hdr->sh_info)
1550 start = stop = addr;
1551 else
1553 off = insn & 0xfff;
1554 if (off & 0x800)
1555 off -= 0x1000;
1556 stop = (bfd_vma) ((bfd_signed_vma) start + 4 + off * 2);
1558 break;
1560 case R_SH_DIR8WPZ:
1561 off = insn & 0xff;
1562 stop = start + 4 + off * 2;
1563 break;
1565 case R_SH_DIR8WPL:
1566 off = insn & 0xff;
1567 stop = (start &~ (bfd_vma) 3) + 4 + off * 4;
1568 break;
1570 case R_SH_SWITCH8:
1571 case R_SH_SWITCH16:
1572 case R_SH_SWITCH32:
1573 /* These relocs types represent
1574 .word L2-L1
1575 The r_addend field holds the difference between the reloc
1576 address and L1. That is the start of the reloc, and
1577 adding in the contents gives us the top. We must adjust
1578 both the r_offset field and the section contents.
1579 N.B. in gas / coff bfd, the elf bfd r_addend is called r_offset,
1580 and the elf bfd r_offset is called r_vaddr. */
1582 stop = irel->r_offset;
1583 start = (bfd_vma) ((bfd_signed_vma) stop - (long) irel->r_addend);
1585 if (start > addr
1586 && start < toaddr
1587 && (stop <= addr || stop >= toaddr))
1588 irel->r_addend += count;
1589 else if (stop > addr
1590 && stop < toaddr
1591 && (start <= addr || start >= toaddr))
1592 irel->r_addend -= count;
1594 if (ELF32_R_TYPE (irel->r_info) == (int) R_SH_SWITCH16)
1595 voff = bfd_get_signed_16 (abfd, contents + nraddr);
1596 else if (ELF32_R_TYPE (irel->r_info) == (int) R_SH_SWITCH8)
1597 voff = bfd_get_8 (abfd, contents + nraddr);
1598 else
1599 voff = bfd_get_signed_32 (abfd, contents + nraddr);
1600 stop = (bfd_vma) ((bfd_signed_vma) start + voff);
1602 break;
1604 case R_SH_USES:
1605 start = irel->r_offset;
1606 stop = (bfd_vma) ((bfd_signed_vma) start
1607 + (long) irel->r_addend
1608 + 4);
1609 break;
1612 if (start > addr
1613 && start < toaddr
1614 && (stop <= addr || stop >= toaddr))
1615 adjust = count;
1616 else if (stop > addr
1617 && stop < toaddr
1618 && (start <= addr || start >= toaddr))
1619 adjust = - count;
1620 else
1621 adjust = 0;
1623 if (adjust != 0)
1625 oinsn = insn;
1626 overflow = false;
1627 switch ((enum elf_sh_reloc_type) ELF32_R_TYPE (irel->r_info))
1629 default:
1630 abort ();
1631 break;
1633 case R_SH_DIR8WPN:
1634 case R_SH_DIR8WPZ:
1635 insn += adjust / 2;
1636 if ((oinsn & 0xff00) != (insn & 0xff00))
1637 overflow = true;
1638 bfd_put_16 (abfd, insn, contents + nraddr);
1639 break;
1641 case R_SH_IND12W:
1642 insn += adjust / 2;
1643 if ((oinsn & 0xf000) != (insn & 0xf000))
1644 overflow = true;
1645 bfd_put_16 (abfd, insn, contents + nraddr);
1646 break;
1648 case R_SH_DIR8WPL:
1649 BFD_ASSERT (adjust == count || count >= 4);
1650 if (count >= 4)
1651 insn += adjust / 4;
1652 else
1654 if ((irel->r_offset & 3) == 0)
1655 ++insn;
1657 if ((oinsn & 0xff00) != (insn & 0xff00))
1658 overflow = true;
1659 bfd_put_16 (abfd, insn, contents + nraddr);
1660 break;
1662 case R_SH_SWITCH8:
1663 voff += adjust;
1664 if (voff < 0 || voff >= 0xff)
1665 overflow = true;
1666 bfd_put_8 (abfd, voff, contents + nraddr);
1667 break;
1669 case R_SH_SWITCH16:
1670 voff += adjust;
1671 if (voff < - 0x8000 || voff >= 0x8000)
1672 overflow = true;
1673 bfd_put_signed_16 (abfd, voff, contents + nraddr);
1674 break;
1676 case R_SH_SWITCH32:
1677 voff += adjust;
1678 bfd_put_signed_32 (abfd, voff, contents + nraddr);
1679 break;
1681 case R_SH_USES:
1682 irel->r_addend += adjust;
1683 break;
1686 if (overflow)
1688 ((*_bfd_error_handler)
1689 (_("%s: 0x%lx: fatal: reloc overflow while relaxing"),
1690 bfd_get_filename (abfd), (unsigned long) irel->r_offset));
1691 bfd_set_error (bfd_error_bad_value);
1692 return false;
1696 irel->r_offset = nraddr;
1699 /* Look through all the other sections. If there contain any IMM32
1700 relocs against internal symbols which we are not going to adjust
1701 below, we may need to adjust the addends. */
1702 for (o = abfd->sections; o != NULL; o = o->next)
1704 Elf_Internal_Rela *internal_relocs;
1705 Elf_Internal_Rela *irelscan, *irelscanend;
1706 bfd_byte *ocontents;
1708 if (o == sec
1709 || (o->flags & SEC_RELOC) == 0
1710 || o->reloc_count == 0)
1711 continue;
1713 /* We always cache the relocs. Perhaps, if info->keep_memory is
1714 false, we should free them, if we are permitted to, when we
1715 leave sh_coff_relax_section. */
1716 internal_relocs = (_bfd_elf32_link_read_relocs
1717 (abfd, o, (PTR) NULL, (Elf_Internal_Rela *) NULL,
1718 true));
1719 if (internal_relocs == NULL)
1720 return false;
1722 ocontents = NULL;
1723 irelscanend = internal_relocs + o->reloc_count;
1724 for (irelscan = internal_relocs; irelscan < irelscanend; irelscan++)
1726 Elf_Internal_Sym sym;
1728 /* Dwarf line numbers use R_SH_SWITCH32 relocs. */
1729 if (ELF32_R_TYPE (irelscan->r_info) == (int) R_SH_SWITCH32)
1731 bfd_vma start, stop;
1732 bfd_signed_vma voff;
1734 if (ocontents == NULL)
1736 if (elf_section_data (o)->this_hdr.contents != NULL)
1737 ocontents = elf_section_data (o)->this_hdr.contents;
1738 else
1740 /* We always cache the section contents.
1741 Perhaps, if info->keep_memory is false, we
1742 should free them, if we are permitted to,
1743 when we leave sh_coff_relax_section. */
1744 ocontents = (bfd_byte *) bfd_malloc (o->_raw_size);
1745 if (ocontents == NULL)
1746 return false;
1747 if (! bfd_get_section_contents (abfd, o, ocontents,
1748 (file_ptr) 0,
1749 o->_raw_size))
1750 return false;
1751 elf_section_data (o)->this_hdr.contents = ocontents;
1755 stop = irelscan->r_offset;
1756 start
1757 = (bfd_vma) ((bfd_signed_vma) stop - (long) irelscan->r_addend);
1759 /* STOP is in a different section, so it won't change. */
1760 if (start > addr && start < toaddr)
1761 irelscan->r_addend += count;
1763 voff = bfd_get_signed_32 (abfd, ocontents + irelscan->r_offset);
1764 stop = (bfd_vma) ((bfd_signed_vma) start + voff);
1766 if (start > addr
1767 && start < toaddr
1768 && (stop <= addr || stop >= toaddr))
1769 bfd_put_signed_32 (abfd, voff + count,
1770 ocontents + irelscan->r_offset);
1771 else if (stop > addr
1772 && stop < toaddr
1773 && (start <= addr || start >= toaddr))
1774 bfd_put_signed_32 (abfd, voff - count,
1775 ocontents + irelscan->r_offset);
1778 if (ELF32_R_TYPE (irelscan->r_info) != (int) R_SH_DIR32)
1779 continue;
1781 if (ELF32_R_SYM (irelscan->r_info) >= symtab_hdr->sh_info)
1782 continue;
1784 bfd_elf32_swap_symbol_in (abfd,
1785 extsyms + ELF32_R_SYM (irelscan->r_info),
1786 &sym);
1788 if (sym.st_shndx == shndx
1789 && (sym.st_value <= addr
1790 || sym.st_value >= toaddr))
1792 bfd_vma val;
1794 if (ocontents == NULL)
1796 if (elf_section_data (o)->this_hdr.contents != NULL)
1797 ocontents = elf_section_data (o)->this_hdr.contents;
1798 else
1800 /* We always cache the section contents.
1801 Perhaps, if info->keep_memory is false, we
1802 should free them, if we are permitted to,
1803 when we leave sh_coff_relax_section. */
1804 ocontents = (bfd_byte *) bfd_malloc (o->_raw_size);
1805 if (ocontents == NULL)
1806 return false;
1807 if (! bfd_get_section_contents (abfd, o, ocontents,
1808 (file_ptr) 0,
1809 o->_raw_size))
1810 return false;
1811 elf_section_data (o)->this_hdr.contents = ocontents;
1815 val = bfd_get_32 (abfd, ocontents + irelscan->r_offset);
1816 val += sym.st_value;
1817 if (val > addr && val < toaddr)
1818 bfd_put_32 (abfd, val - count,
1819 ocontents + irelscan->r_offset);
1824 /* Adjust the local symbols defined in this section. */
1825 esym = extsyms;
1826 esymend = esym + symtab_hdr->sh_info;
1827 for (; esym < esymend; esym++)
1829 Elf_Internal_Sym isym;
1831 bfd_elf32_swap_symbol_in (abfd, esym, &isym);
1833 if (isym.st_shndx == shndx
1834 && isym.st_value > addr
1835 && isym.st_value < toaddr)
1837 isym.st_value -= count;
1838 bfd_elf32_swap_symbol_out (abfd, &isym, esym);
1842 /* Now adjust the global symbols defined in this section. */
1843 esym = extsyms + symtab_hdr->sh_info;
1844 esymend = extsyms + (symtab_hdr->sh_size / sizeof (Elf32_External_Sym));
1845 for (index = 0; esym < esymend; esym++, index++)
1847 Elf_Internal_Sym isym;
1849 bfd_elf32_swap_symbol_in (abfd, esym, &isym);
1850 sym_hash = elf_sym_hashes (abfd)[index];
1851 if (isym.st_shndx == shndx
1852 && ((sym_hash)->root.type == bfd_link_hash_defined
1853 || (sym_hash)->root.type == bfd_link_hash_defweak)
1854 && (sym_hash)->root.u.def.section == sec
1855 && (sym_hash)->root.u.def.value > addr
1856 && (sym_hash)->root.u.def.value < toaddr)
1858 (sym_hash)->root.u.def.value -= count;
1862 /* See if we can move the ALIGN reloc forward. We have adjusted
1863 r_offset for it already. */
1864 if (irelalign != NULL)
1866 bfd_vma alignto, alignaddr;
1868 alignto = BFD_ALIGN (toaddr, 1 << irelalign->r_addend);
1869 alignaddr = BFD_ALIGN (irelalign->r_offset,
1870 1 << irelalign->r_addend);
1871 if (alignto != alignaddr)
1873 /* Tail recursion. */
1874 return sh_elf_relax_delete_bytes (abfd, sec, alignaddr,
1875 alignto - alignaddr);
1879 return true;
1882 /* Look for loads and stores which we can align to four byte
1883 boundaries. This is like sh_align_loads in coff-sh.c. */
1885 static boolean
1886 sh_elf_align_loads (abfd, sec, internal_relocs, contents, pswapped)
1887 bfd *abfd;
1888 asection *sec;
1889 Elf_Internal_Rela *internal_relocs;
1890 bfd_byte *contents;
1891 boolean *pswapped;
1893 Elf_Internal_Rela *irel, *irelend;
1894 bfd_vma *labels = NULL;
1895 bfd_vma *label, *label_end;
1897 *pswapped = false;
1899 irelend = internal_relocs + sec->reloc_count;
1901 /* Get all the addresses with labels on them. */
1902 labels = (bfd_vma *) bfd_malloc (sec->reloc_count * sizeof (bfd_vma));
1903 if (labels == NULL)
1904 goto error_return;
1905 label_end = labels;
1906 for (irel = internal_relocs; irel < irelend; irel++)
1908 if (ELF32_R_TYPE (irel->r_info) == (int) R_SH_LABEL)
1910 *label_end = irel->r_offset;
1911 ++label_end;
1915 /* Note that the assembler currently always outputs relocs in
1916 address order. If that ever changes, this code will need to sort
1917 the label values and the relocs. */
1919 label = labels;
1921 for (irel = internal_relocs; irel < irelend; irel++)
1923 bfd_vma start, stop;
1925 if (ELF32_R_TYPE (irel->r_info) != (int) R_SH_CODE)
1926 continue;
1928 start = irel->r_offset;
1930 for (irel++; irel < irelend; irel++)
1931 if (ELF32_R_TYPE (irel->r_info) == (int) R_SH_DATA)
1932 break;
1933 if (irel < irelend)
1934 stop = irel->r_offset;
1935 else
1936 stop = sec->_cooked_size;
1938 if (! _bfd_sh_align_load_span (abfd, sec, contents, sh_elf_swap_insns,
1939 (PTR) internal_relocs, &label,
1940 label_end, start, stop, pswapped))
1941 goto error_return;
1944 free (labels);
1946 return true;
1948 error_return:
1949 if (labels != NULL)
1950 free (labels);
1951 return false;
1954 /* Swap two SH instructions. This is like sh_swap_insns in coff-sh.c. */
1956 static boolean
1957 sh_elf_swap_insns (abfd, sec, relocs, contents, addr)
1958 bfd *abfd;
1959 asection *sec;
1960 PTR relocs;
1961 bfd_byte *contents;
1962 bfd_vma addr;
1964 Elf_Internal_Rela *internal_relocs = (Elf_Internal_Rela *) relocs;
1965 unsigned short i1, i2;
1966 Elf_Internal_Rela *irel, *irelend;
1968 /* Swap the instructions themselves. */
1969 i1 = bfd_get_16 (abfd, contents + addr);
1970 i2 = bfd_get_16 (abfd, contents + addr + 2);
1971 bfd_put_16 (abfd, i2, contents + addr);
1972 bfd_put_16 (abfd, i1, contents + addr + 2);
1974 /* Adjust all reloc addresses. */
1975 irelend = internal_relocs + sec->reloc_count;
1976 for (irel = internal_relocs; irel < irelend; irel++)
1978 enum elf_sh_reloc_type type;
1979 int add;
1981 /* There are a few special types of relocs that we don't want to
1982 adjust. These relocs do not apply to the instruction itself,
1983 but are only associated with the address. */
1984 type = (enum elf_sh_reloc_type) ELF32_R_TYPE (irel->r_info);
1985 if (type == R_SH_ALIGN
1986 || type == R_SH_CODE
1987 || type == R_SH_DATA
1988 || type == R_SH_LABEL)
1989 continue;
1991 /* If an R_SH_USES reloc points to one of the addresses being
1992 swapped, we must adjust it. It would be incorrect to do this
1993 for a jump, though, since we want to execute both
1994 instructions after the jump. (We have avoided swapping
1995 around a label, so the jump will not wind up executing an
1996 instruction it shouldn't). */
1997 if (type == R_SH_USES)
1999 bfd_vma off;
2001 off = irel->r_offset + 4 + irel->r_addend;
2002 if (off == addr)
2003 irel->r_offset += 2;
2004 else if (off == addr + 2)
2005 irel->r_offset -= 2;
2008 if (irel->r_offset == addr)
2010 irel->r_offset += 2;
2011 add = -2;
2013 else if (irel->r_offset == addr + 2)
2015 irel->r_offset -= 2;
2016 add = 2;
2018 else
2019 add = 0;
2021 if (add != 0)
2023 bfd_byte *loc;
2024 unsigned short insn, oinsn;
2025 boolean overflow;
2027 loc = contents + irel->r_offset;
2028 overflow = false;
2029 switch (type)
2031 default:
2032 break;
2034 case R_SH_DIR8WPN:
2035 case R_SH_DIR8WPZ:
2036 insn = bfd_get_16 (abfd, loc);
2037 oinsn = insn;
2038 insn += add / 2;
2039 if ((oinsn & 0xff00) != (insn & 0xff00))
2040 overflow = true;
2041 bfd_put_16 (abfd, insn, loc);
2042 break;
2044 case R_SH_IND12W:
2045 insn = bfd_get_16 (abfd, loc);
2046 oinsn = insn;
2047 insn += add / 2;
2048 if ((oinsn & 0xf000) != (insn & 0xf000))
2049 overflow = true;
2050 bfd_put_16 (abfd, insn, loc);
2051 break;
2053 case R_SH_DIR8WPL:
2054 /* This reloc ignores the least significant 3 bits of
2055 the program counter before adding in the offset.
2056 This means that if ADDR is at an even address, the
2057 swap will not affect the offset. If ADDR is an at an
2058 odd address, then the instruction will be crossing a
2059 four byte boundary, and must be adjusted. */
2060 if ((addr & 3) != 0)
2062 insn = bfd_get_16 (abfd, loc);
2063 oinsn = insn;
2064 insn += add / 2;
2065 if ((oinsn & 0xff00) != (insn & 0xff00))
2066 overflow = true;
2067 bfd_put_16 (abfd, insn, loc);
2070 break;
2073 if (overflow)
2075 ((*_bfd_error_handler)
2076 (_("%s: 0x%lx: fatal: reloc overflow while relaxing"),
2077 bfd_get_filename (abfd), (unsigned long) irel->r_offset));
2078 bfd_set_error (bfd_error_bad_value);
2079 return false;
2084 return true;
2087 /* The size in bytes of an entry in the procedure linkage table. */
2089 #define PLT_ENTRY_SIZE 28
2091 /* First entry in an absolute procedure linkage table look like this. */
2093 static const bfd_byte elf_sh_plt0_entry_be[PLT_ENTRY_SIZE] =
2095 0xd0, 0x04, /* mov.l 1f,r0 */
2096 0xd2, 0x05, /* mov.l 2f,r2 */
2097 0x60, 0x02, /* mov.l @r0,r0 */
2098 0x62, 0x22, /* mov.l @r2,r2 */
2099 0x40, 0x2b, /* jmp @r0 */
2100 0xe0, 0x00, /* mov #0,r0 */
2101 0x00, 0x09, /* nop */
2102 0x00, 0x09, /* nop */
2103 0x00, 0x09, /* nop */
2104 0x00, 0x09, /* nop */
2105 0, 0, 0, 0, /* 1: replaced with address of .got.plt + 8. */
2106 0, 0, 0, 0, /* 2: replaced with address of .got.plt + 4. */
2109 static const bfd_byte elf_sh_plt0_entry_le[PLT_ENTRY_SIZE] =
2111 0x04, 0xd0, /* mov.l 1f,r0 */
2112 0x05, 0xd2, /* mov.l 2f,r2 */
2113 0x02, 0x60, /* mov.l @r0,r0 */
2114 0x22, 0x62, /* mov.l @r2,r2 */
2115 0x2b, 0x40, /* jmp @r0 */
2116 0x00, 0xe0, /* mov #0,r0 */
2117 0x09, 0x00, /* nop */
2118 0x09, 0x00, /* nop */
2119 0x09, 0x00, /* nop */
2120 0x09, 0x00, /* nop */
2121 0, 0, 0, 0, /* 1: replaced with address of .got.plt + 8. */
2122 0, 0, 0, 0, /* 2: replaced with address of .got.plt + 4. */
2125 /* Sebsequent entries in an absolute procedure linkage table look like
2126 this. */
2128 static const bfd_byte elf_sh_plt_entry_be[PLT_ENTRY_SIZE] =
2130 0xd0, 0x04, /* mov.l 1f,r0 */
2131 0x60, 0x02, /* mov.l @r0,r0 */
2132 0xd2, 0x02, /* mov.l 0f,r2 */
2133 0x40, 0x2b, /* jmp @r0 */
2134 0x60, 0x23, /* mov r2,r0 */
2135 0xd1, 0x03, /* mov.l 2f,r1 */
2136 0x40, 0x2b, /* jmp @r0 */
2137 0x00, 0x09, /* nop */
2138 0, 0, 0, 0, /* 0: replaced with address of .PLT0. */
2139 0, 0, 0, 0, /* 1: replaced with address of this symbol in .got. */
2140 0, 0, 0, 0, /* 2: replaced with offset into relocation table. */
2143 static const bfd_byte elf_sh_plt_entry_le[PLT_ENTRY_SIZE] =
2145 0x04, 0xd0, /* mov.l 1f,r0 */
2146 0x02, 0x60, /* mov.l @r0,r0 */
2147 0x02, 0xd2, /* mov.l 0f,r2 */
2148 0x2b, 0x40, /* jmp @r0 */
2149 0x23, 0x60, /* mov r2,r0 */
2150 0x03, 0xd1, /* mov.l 2f,r1 */
2151 0x2b, 0x40, /* jmp @r0 */
2152 0x09, 0x00, /* nop */
2153 0, 0, 0, 0, /* 0: replaced with address of .PLT. */
2154 0, 0, 0, 0, /* 1: replaced with address of this symbol in .got. */
2155 0, 0, 0, 0, /* 2: replaced with offset into relocation table. */
2158 /* Entries in a PIC procedure linkage table look like this. */
2160 static const bfd_byte elf_sh_pic_plt_entry_be[PLT_ENTRY_SIZE] =
2162 0xd0, 0x04, /* mov.l 1f,r0 */
2163 0x00, 0xce, /* mov.l @(r0,r12),r0 */
2164 0x40, 0x2b, /* jmp @r0 */
2165 0x00, 0x09, /* nop */
2166 0x50, 0xc2, /* 0: mov.l @(8,r12),r0 */
2167 0x52, 0xc1, /* 1: mov.l @(4,r12),r2 */
2168 0xd1, 0x02, /* mov.l 2f,r1 */
2169 0x40, 0x2b, /* jmp @r0 */
2170 0xe0, 0x00, /* mov #0,r0 ! shows the type of PLT. */
2171 0x00, 0x09, /* nop */
2172 0, 0, 0, 0, /* 1: replaced with address of this symbol in .got. */
2173 0, 0, 0, 0 /* 2: replaced with offset into relocation table. */
2176 static const bfd_byte elf_sh_pic_plt_entry_le[PLT_ENTRY_SIZE] =
2178 0x04, 0xd0, /* mov.l 1f,r0 */
2179 0xce, 0x00, /* mov.l @(r0,r12),r0 */
2180 0x2b, 0x40, /* jmp @r0 */
2181 0x09, 0x00, /* nop */
2182 0xc2, 0x50, /* 0: mov.l @(8,r12),r0 */
2183 0xc1, 0x52, /* 1: mov.l @(4,r12),r2 */
2184 0x02, 0xd1, /* mov.l 2f,r1 */
2185 0x2b, 0x40, /* jmp @r0 */
2186 0x00, 0xe0, /* mov #0,r0 ! shows the type of PLT. */
2187 0x09, 0x00, /* nop */
2188 0, 0, 0, 0, /* 1: replaced with address of this symbol in .got. */
2189 0, 0, 0, 0 /* 2: replaced with offset into relocation table. */
2192 static const bfd_byte *elf_sh_plt0_entry;
2193 static const bfd_byte *elf_sh_plt_entry;
2194 static const bfd_byte *elf_sh_pic_plt_entry;
2196 /* Return size of a PLT entry. */
2197 #define elf_sh_sizeof_plt(info) PLT_ENTRY_SIZE
2199 /* Return offset of the PLT0 address in an absolute PLT entry. */
2200 #define elf_sh_plt_plt0_offset(info) 16
2202 /* Return offset of the linker in PLT0 entry. */
2203 #define elf_sh_plt0_linker_offset(info) 20
2205 /* Return offset of the GOT id in PLT0 entry. */
2206 #define elf_sh_plt0_gotid_offset(info) 24
2208 /* Return offset of the tempoline in PLT entry */
2209 #define elf_sh_plt_temp_offset(info) 8
2211 /* Return offset of the symbol in PLT entry. */
2212 #define elf_sh_plt_symbol_offset(info) 20
2214 /* Return offset of the relocation in PLT entry. */
2215 #define elf_sh_plt_reloc_offset(info) 24
2217 /* The sh linker needs to keep track of the number of relocs that it
2218 decides to copy in check_relocs for each symbol. This is so that
2219 it can discard PC relative relocs if it doesn't need them when
2220 linking with -Bsymbolic. We store the information in a field
2221 extending the regular ELF linker hash table. */
2223 /* This structure keeps track of the number of PC relative relocs we
2224 have copied for a given symbol. */
2226 struct elf_sh_pcrel_relocs_copied
2228 /* Next section. */
2229 struct elf_sh_pcrel_relocs_copied *next;
2230 /* A section in dynobj. */
2231 asection *section;
2232 /* Number of relocs copied in this section. */
2233 bfd_size_type count;
2236 /* sh ELF linker hash entry. */
2238 struct elf_sh_link_hash_entry
2240 struct elf_link_hash_entry root;
2242 /* Number of PC relative relocs copied for this symbol. */
2243 struct elf_sh_pcrel_relocs_copied *pcrel_relocs_copied;
2246 /* sh ELF linker hash table. */
2248 struct elf_sh_link_hash_table
2250 struct elf_link_hash_table root;
2253 /* Declare this now that the above structures are defined. */
2255 static boolean sh_elf_discard_copies
2256 PARAMS ((struct elf_sh_link_hash_entry *, PTR));
2258 /* Traverse an sh ELF linker hash table. */
2260 #define sh_elf_link_hash_traverse(table, func, info) \
2261 (elf_link_hash_traverse \
2262 (&(table)->root, \
2263 (boolean (*) PARAMS ((struct elf_link_hash_entry *, PTR))) (func), \
2264 (info)))
2266 /* Get the sh ELF linker hash table from a link_info structure. */
2268 #define sh_elf_hash_table(p) \
2269 ((struct elf_sh_link_hash_table *) ((p)->hash))
2271 /* Create an entry in an sh ELF linker hash table. */
2273 static struct bfd_hash_entry *
2274 sh_elf_link_hash_newfunc (entry, table, string)
2275 struct bfd_hash_entry *entry;
2276 struct bfd_hash_table *table;
2277 const char *string;
2279 struct elf_sh_link_hash_entry *ret =
2280 (struct elf_sh_link_hash_entry *) entry;
2282 /* Allocate the structure if it has not already been allocated by a
2283 subclass. */
2284 if (ret == (struct elf_sh_link_hash_entry *) NULL)
2285 ret = ((struct elf_sh_link_hash_entry *)
2286 bfd_hash_allocate (table,
2287 sizeof (struct elf_sh_link_hash_entry)));
2288 if (ret == (struct elf_sh_link_hash_entry *) NULL)
2289 return (struct bfd_hash_entry *) ret;
2291 /* Call the allocation method of the superclass. */
2292 ret = ((struct elf_sh_link_hash_entry *)
2293 _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
2294 table, string));
2295 if (ret != (struct elf_sh_link_hash_entry *) NULL)
2297 ret->pcrel_relocs_copied = NULL;
2300 return (struct bfd_hash_entry *) ret;
2303 /* Create an sh ELF linker hash table. */
2305 static struct bfd_link_hash_table *
2306 sh_elf_link_hash_table_create (abfd)
2307 bfd *abfd;
2309 struct elf_sh_link_hash_table *ret;
2311 ret = ((struct elf_sh_link_hash_table *)
2312 bfd_alloc (abfd, sizeof (struct elf_sh_link_hash_table)));
2313 if (ret == (struct elf_sh_link_hash_table *) NULL)
2314 return NULL;
2316 if (! _bfd_elf_link_hash_table_init (&ret->root, abfd,
2317 sh_elf_link_hash_newfunc))
2319 bfd_release (abfd, ret);
2320 return NULL;
2323 return &ret->root.root;
2326 /* Create dynamic sections when linking against a dynamic object. */
2328 static boolean
2329 sh_elf_create_dynamic_sections (abfd, info)
2330 bfd *abfd;
2331 struct bfd_link_info *info;
2333 flagword flags, pltflags;
2334 register asection *s;
2335 struct elf_backend_data *bed = get_elf_backend_data (abfd);
2336 int ptralign = 0;
2338 switch (bed->s->arch_size)
2340 case 32:
2341 ptralign = 2;
2342 break;
2344 case 64:
2345 ptralign = 3;
2346 break;
2348 default:
2349 bfd_set_error (bfd_error_bad_value);
2350 return false;
2353 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
2354 .rel[a].bss sections. */
2356 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
2357 | SEC_LINKER_CREATED);
2359 pltflags = flags;
2360 pltflags |= SEC_CODE;
2361 if (bed->plt_not_loaded)
2362 pltflags &= ~ (SEC_LOAD | SEC_HAS_CONTENTS);
2363 if (bed->plt_readonly)
2364 pltflags |= SEC_READONLY;
2366 s = bfd_make_section (abfd, ".plt");
2367 if (s == NULL
2368 || ! bfd_set_section_flags (abfd, s, pltflags)
2369 || ! bfd_set_section_alignment (abfd, s, bed->plt_alignment))
2370 return false;
2372 if (bed->want_plt_sym)
2374 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
2375 .plt section. */
2376 struct elf_link_hash_entry *h = NULL;
2377 if (! (_bfd_generic_link_add_one_symbol
2378 (info, abfd, "_PROCEDURE_LINKAGE_TABLE_", BSF_GLOBAL, s,
2379 (bfd_vma) 0, (const char *) NULL, false,
2380 get_elf_backend_data (abfd)->collect,
2381 (struct bfd_link_hash_entry **) &h)))
2382 return false;
2383 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
2384 h->type = STT_OBJECT;
2386 if (info->shared
2387 && ! _bfd_elf_link_record_dynamic_symbol (info, h))
2388 return false;
2391 s = bfd_make_section (abfd,
2392 bed->default_use_rela_p ? ".rela.plt" : ".rel.plt");
2393 if (s == NULL
2394 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
2395 || ! bfd_set_section_alignment (abfd, s, ptralign))
2396 return false;
2398 if (! _bfd_elf_create_got_section (abfd, info))
2399 return false;
2402 const char *secname;
2403 char *relname;
2404 flagword secflags;
2405 asection *sec;
2407 for (sec = abfd->sections; sec; sec = sec->next)
2409 secflags = bfd_get_section_flags (abfd, sec);
2410 if ((secflags & (SEC_DATA | SEC_LINKER_CREATED))
2411 || ((secflags & SEC_HAS_CONTENTS) != SEC_HAS_CONTENTS))
2412 continue;
2413 secname = bfd_get_section_name (abfd, sec);
2414 relname = (char *) bfd_malloc (strlen (secname) + 6);
2415 strcpy (relname, ".rela");
2416 strcat (relname, secname);
2417 s = bfd_make_section (abfd, relname);
2418 if (s == NULL
2419 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
2420 || ! bfd_set_section_alignment (abfd, s, ptralign))
2421 return false;
2425 if (bed->want_dynbss)
2427 /* The .dynbss section is a place to put symbols which are defined
2428 by dynamic objects, are referenced by regular objects, and are
2429 not functions. We must allocate space for them in the process
2430 image and use a R_*_COPY reloc to tell the dynamic linker to
2431 initialize them at run time. The linker script puts the .dynbss
2432 section into the .bss section of the final image. */
2433 s = bfd_make_section (abfd, ".dynbss");
2434 if (s == NULL
2435 || ! bfd_set_section_flags (abfd, s, SEC_ALLOC))
2436 return false;
2438 /* The .rel[a].bss section holds copy relocs. This section is not
2439 normally needed. We need to create it here, though, so that the
2440 linker will map it to an output section. We can't just create it
2441 only if we need it, because we will not know whether we need it
2442 until we have seen all the input files, and the first time the
2443 main linker code calls BFD after examining all the input files
2444 (size_dynamic_sections) the input sections have already been
2445 mapped to the output sections. If the section turns out not to
2446 be needed, we can discard it later. We will never need this
2447 section when generating a shared object, since they do not use
2448 copy relocs. */
2449 if (! info->shared)
2451 s = bfd_make_section (abfd,
2452 (bed->default_use_rela_p
2453 ? ".rela.bss" : ".rel.bss"));
2454 if (s == NULL
2455 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
2456 || ! bfd_set_section_alignment (abfd, s, ptralign))
2457 return false;
2461 return true;
2465 /* Adjust a symbol defined by a dynamic object and referenced by a
2466 regular object. The current definition is in some section of the
2467 dynamic object, but we're not including those sections. We have to
2468 change the definition to something the rest of the link can
2469 understand. */
2471 static boolean
2472 sh_elf_adjust_dynamic_symbol (info, h)
2473 struct bfd_link_info *info;
2474 struct elf_link_hash_entry *h;
2476 bfd *dynobj;
2477 asection *s;
2478 unsigned int power_of_two;
2480 dynobj = elf_hash_table (info)->dynobj;
2482 /* Make sure we know what is going on here. */
2483 BFD_ASSERT (dynobj != NULL
2484 && ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT)
2485 || h->weakdef != NULL
2486 || ((h->elf_link_hash_flags
2487 & ELF_LINK_HASH_DEF_DYNAMIC) != 0
2488 && (h->elf_link_hash_flags
2489 & ELF_LINK_HASH_REF_REGULAR) != 0
2490 && (h->elf_link_hash_flags
2491 & ELF_LINK_HASH_DEF_REGULAR) == 0)));
2493 /* If this is a function, put it in the procedure linkage table. We
2494 will fill in the contents of the procedure linkage table later,
2495 when we know the address of the .got section. */
2496 if (h->type == STT_FUNC
2497 || (h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0)
2499 if (! info->shared
2500 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) == 0
2501 && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) == 0)
2503 /* This case can occur if we saw a PLT reloc in an input
2504 file, but the symbol was never referred to by a dynamic
2505 object. In such a case, we don't actually need to build
2506 a procedure linkage table, and we can just do a REL32
2507 reloc instead. */
2508 BFD_ASSERT ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0);
2509 return true;
2512 /* Make sure this symbol is output as a dynamic symbol. */
2513 if (h->dynindx == -1)
2515 if (! bfd_elf32_link_record_dynamic_symbol (info, h))
2516 return false;
2519 s = bfd_get_section_by_name (dynobj, ".plt");
2520 BFD_ASSERT (s != NULL);
2522 /* If this is the first .plt entry, make room for the special
2523 first entry. */
2524 if (s->_raw_size == 0)
2525 s->_raw_size += PLT_ENTRY_SIZE;
2527 /* If this symbol is not defined in a regular file, and we are
2528 not generating a shared library, then set the symbol to this
2529 location in the .plt. This is required to make function
2530 pointers compare as equal between the normal executable and
2531 the shared library. */
2532 if (! info->shared
2533 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
2535 h->root.u.def.section = s;
2536 h->root.u.def.value = s->_raw_size;
2539 h->plt.offset = s->_raw_size;
2541 /* Make room for this entry. */
2542 s->_raw_size += elf_sh_sizeof_plt (info);
2544 /* We also need to make an entry in the .got.plt section, which
2545 will be placed in the .got section by the linker script. */
2547 s = bfd_get_section_by_name (dynobj, ".got.plt");
2548 BFD_ASSERT (s != NULL);
2549 s->_raw_size += 4;
2551 /* We also need to make an entry in the .rela.plt section. */
2553 s = bfd_get_section_by_name (dynobj, ".rela.plt");
2554 BFD_ASSERT (s != NULL);
2555 s->_raw_size += sizeof (Elf32_External_Rela);
2557 return true;
2560 /* If this is a weak symbol, and there is a real definition, the
2561 processor independent code will have arranged for us to see the
2562 real definition first, and we can just use the same value. */
2563 if (h->weakdef != NULL)
2565 BFD_ASSERT (h->weakdef->root.type == bfd_link_hash_defined
2566 || h->weakdef->root.type == bfd_link_hash_defweak);
2567 h->root.u.def.section = h->weakdef->root.u.def.section;
2568 h->root.u.def.value = h->weakdef->root.u.def.value;
2569 return true;
2572 /* This is a reference to a symbol defined by a dynamic object which
2573 is not a function. */
2575 /* If we are creating a shared library, we must presume that the
2576 only references to the symbol are via the global offset table.
2577 For such cases we need not do anything here; the relocations will
2578 be handled correctly by relocate_section. */
2579 if (info->shared)
2580 return true;
2582 /* If there are no references to this symbol that do not use the
2583 GOT, we don't need to generate a copy reloc. */
2584 if ((h->elf_link_hash_flags & ELF_LINK_NON_GOT_REF) == 0)
2585 return true;
2587 /* We must allocate the symbol in our .dynbss section, which will
2588 become part of the .bss section of the executable. There will be
2589 an entry for this symbol in the .dynsym section. The dynamic
2590 object will contain position independent code, so all references
2591 from the dynamic object to this symbol will go through the global
2592 offset table. The dynamic linker will use the .dynsym entry to
2593 determine the address it must put in the global offset table, so
2594 both the dynamic object and the regular object will refer to the
2595 same memory location for the variable. */
2597 s = bfd_get_section_by_name (dynobj, ".dynbss");
2598 BFD_ASSERT (s != NULL);
2600 /* We must generate a R_SH_COPY reloc to tell the dynamic linker to
2601 copy the initial value out of the dynamic object and into the
2602 runtime process image. We need to remember the offset into the
2603 .rela.bss section we are going to use. */
2604 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
2606 asection *srel;
2608 srel = bfd_get_section_by_name (dynobj, ".rela.bss");
2609 BFD_ASSERT (srel != NULL);
2610 srel->_raw_size += sizeof (Elf32_External_Rela);
2611 h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_COPY;
2614 /* We need to figure out the alignment required for this symbol. I
2615 have no idea how ELF linkers handle this. */
2616 power_of_two = bfd_log2 (h->size);
2617 if (power_of_two > 3)
2618 power_of_two = 3;
2620 /* Apply the required alignment. */
2621 s->_raw_size = BFD_ALIGN (s->_raw_size,
2622 (bfd_size_type) (1 << power_of_two));
2623 if (power_of_two > bfd_get_section_alignment (dynobj, s))
2625 if (! bfd_set_section_alignment (dynobj, s, power_of_two))
2626 return false;
2629 /* Define the symbol as being at this point in the section. */
2630 h->root.u.def.section = s;
2631 h->root.u.def.value = s->_raw_size;
2633 /* Increment the section size to make room for the symbol. */
2634 s->_raw_size += h->size;
2636 return true;
2639 /* Set the sizes of the dynamic sections. */
2641 static boolean
2642 sh_elf_size_dynamic_sections (output_bfd, info)
2643 bfd *output_bfd;
2644 struct bfd_link_info *info;
2646 bfd *dynobj;
2647 asection *s;
2648 boolean plt;
2649 boolean relocs;
2650 boolean reltext;
2652 dynobj = elf_hash_table (info)->dynobj;
2653 BFD_ASSERT (dynobj != NULL);
2655 if (elf_hash_table (info)->dynamic_sections_created)
2657 /* Set the contents of the .interp section to the interpreter. */
2658 if (! info->shared)
2660 s = bfd_get_section_by_name (dynobj, ".interp");
2661 BFD_ASSERT (s != NULL);
2662 s->_raw_size = sizeof ELF_DYNAMIC_INTERPRETER;
2663 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
2666 else
2668 /* We may have created entries in the .rela.got section.
2669 However, if we are not creating the dynamic sections, we will
2670 not actually use these entries. Reset the size of .rela.got,
2671 which will cause it to get stripped from the output file
2672 below. */
2673 s = bfd_get_section_by_name (dynobj, ".rela.got");
2674 if (s != NULL)
2675 s->_raw_size = 0;
2678 /* If this is a -Bsymbolic shared link, then we need to discard all
2679 PC relative relocs against symbols defined in a regular object.
2680 We allocated space for them in the check_relocs routine, but we
2681 will not fill them in in the relocate_section routine. */
2682 if (info->shared && info->symbolic)
2683 sh_elf_link_hash_traverse (sh_elf_hash_table (info),
2684 sh_elf_discard_copies,
2685 (PTR) NULL);
2687 /* The check_relocs and adjust_dynamic_symbol entry points have
2688 determined the sizes of the various dynamic sections. Allocate
2689 memory for them. */
2690 plt = false;
2691 relocs = false;
2692 reltext = false;
2693 for (s = dynobj->sections; s != NULL; s = s->next)
2695 const char *name;
2696 boolean strip;
2698 if ((s->flags & SEC_LINKER_CREATED) == 0)
2699 continue;
2701 /* It's OK to base decisions on the section name, because none
2702 of the dynobj section names depend upon the input files. */
2703 name = bfd_get_section_name (dynobj, s);
2705 strip = false;
2707 if (strcmp (name, ".plt") == 0)
2709 if (s->_raw_size == 0)
2711 /* Strip this section if we don't need it; see the
2712 comment below. */
2713 strip = true;
2715 else
2717 /* Remember whether there is a PLT. */
2718 plt = true;
2721 else if (strncmp (name, ".rela", 5) == 0)
2723 if (s->_raw_size == 0)
2725 /* If we don't need this section, strip it from the
2726 output file. This is mostly to handle .rela.bss and
2727 .rela.plt. We must create both sections in
2728 create_dynamic_sections, because they must be created
2729 before the linker maps input sections to output
2730 sections. The linker does that before
2731 adjust_dynamic_symbol is called, and it is that
2732 function which decides whether anything needs to go
2733 into these sections. */
2734 strip = true;
2736 else
2738 asection *target;
2740 /* Remember whether there are any reloc sections other
2741 than .rela.plt. */
2742 if (strcmp (name, ".rela.plt") != 0)
2744 const char *outname;
2746 relocs = true;
2748 /* If this relocation section applies to a read only
2749 section, then we probably need a DT_TEXTREL
2750 entry. The entries in the .rela.plt section
2751 really apply to the .got section, which we
2752 created ourselves and so know is not readonly. */
2753 outname = bfd_get_section_name (output_bfd,
2754 s->output_section);
2755 target = bfd_get_section_by_name (output_bfd, outname + 5);
2756 if (target != NULL
2757 && (target->flags & SEC_READONLY) != 0
2758 && (target->flags & SEC_ALLOC) != 0)
2759 reltext = true;
2762 /* We use the reloc_count field as a counter if we need
2763 to copy relocs into the output file. */
2764 s->reloc_count = 0;
2767 else if (strncmp (name, ".got", 4) != 0)
2769 /* It's not one of our sections, so don't allocate space. */
2770 continue;
2773 if (strip)
2775 _bfd_strip_section_from_output (info, s);
2776 continue;
2779 /* Allocate memory for the section contents. */
2780 s->contents = (bfd_byte *) bfd_alloc (dynobj, s->_raw_size);
2781 if (s->contents == NULL && s->_raw_size != 0)
2782 return false;
2785 if (elf_hash_table (info)->dynamic_sections_created)
2787 /* Add some entries to the .dynamic section. We fill in the
2788 values later, in sh_elf_finish_dynamic_sections, but we
2789 must add the entries now so that we get the correct size for
2790 the .dynamic section. The DT_DEBUG entry is filled in by the
2791 dynamic linker and used by the debugger. */
2792 if (! info->shared)
2794 if (! bfd_elf32_add_dynamic_entry (info, DT_DEBUG, 0))
2795 return false;
2798 if (plt)
2800 if (! bfd_elf32_add_dynamic_entry (info, DT_PLTGOT, 0)
2801 || ! bfd_elf32_add_dynamic_entry (info, DT_PLTRELSZ, 0)
2802 || ! bfd_elf32_add_dynamic_entry (info, DT_PLTREL, DT_RELA)
2803 || ! bfd_elf32_add_dynamic_entry (info, DT_JMPREL, 0))
2804 return false;
2807 if (relocs)
2809 if (! bfd_elf32_add_dynamic_entry (info, DT_RELA, 0)
2810 || ! bfd_elf32_add_dynamic_entry (info, DT_RELASZ, 0)
2811 || ! bfd_elf32_add_dynamic_entry (info, DT_RELAENT,
2812 sizeof (Elf32_External_Rela)))
2813 return false;
2816 if (reltext)
2818 if (! bfd_elf32_add_dynamic_entry (info, DT_TEXTREL, 0))
2819 return false;
2823 return true;
2826 /* This function is called via sh_elf_link_hash_traverse if we are
2827 creating a shared object with -Bsymbolic. It discards the space
2828 allocated to copy PC relative relocs against symbols which are
2829 defined in regular objects. We allocated space for them in the
2830 check_relocs routine, but we won't fill them in in the
2831 relocate_section routine. */
2833 /*ARGSUSED*/
2834 static boolean
2835 sh_elf_discard_copies (h, ignore)
2836 struct elf_sh_link_hash_entry *h;
2837 PTR ignore ATTRIBUTE_UNUSED;
2839 struct elf_sh_pcrel_relocs_copied *s;
2841 /* We only discard relocs for symbols defined in a regular object. */
2842 if ((h->root.elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
2843 return true;
2845 for (s = h->pcrel_relocs_copied; s != NULL; s = s->next)
2846 s->section->_raw_size -= s->count * sizeof (Elf32_External_Rela);
2848 return true;
2852 /* Relocate an SH ELF section. */
2854 static boolean
2855 sh_elf_relocate_section (output_bfd, info, input_bfd, input_section,
2856 contents, relocs, local_syms, local_sections)
2857 bfd *output_bfd ATTRIBUTE_UNUSED;
2858 struct bfd_link_info *info;
2859 bfd *input_bfd;
2860 asection *input_section;
2861 bfd_byte *contents;
2862 Elf_Internal_Rela *relocs;
2863 Elf_Internal_Sym *local_syms;
2864 asection **local_sections;
2866 Elf_Internal_Shdr *symtab_hdr;
2867 struct elf_link_hash_entry **sym_hashes;
2868 Elf_Internal_Rela *rel, *relend;
2869 bfd *dynobj;
2870 bfd_vma *local_got_offsets;
2871 asection *sgot;
2872 asection *splt;
2873 asection *sreloc;
2875 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
2876 sym_hashes = elf_sym_hashes (input_bfd);
2877 dynobj = elf_hash_table (info)->dynobj;
2878 local_got_offsets = elf_local_got_offsets (input_bfd);
2880 sgot = NULL;
2881 splt = NULL;
2882 sreloc = NULL;
2884 rel = relocs;
2885 relend = relocs + input_section->reloc_count;
2886 for (; rel < relend; rel++)
2888 int r_type;
2889 reloc_howto_type *howto;
2890 unsigned long r_symndx;
2891 Elf_Internal_Sym *sym;
2892 asection *sec;
2893 struct elf_link_hash_entry *h;
2894 bfd_vma relocation;
2895 bfd_vma addend = (bfd_vma)0;
2896 bfd_reloc_status_type r;
2898 r_symndx = ELF32_R_SYM (rel->r_info);
2900 r_type = ELF32_R_TYPE (rel->r_info);
2902 /* Many of the relocs are only used for relaxing, and are
2903 handled entirely by the relaxation code. */
2904 if (r_type > (int) R_SH_LAST_INVALID_RELOC
2905 && r_type < (int) R_SH_LOOP_START)
2906 continue;
2907 if (r_type == (int) R_SH_NONE)
2908 continue;
2910 if (r_type < 0
2911 || r_type >= R_SH_max
2912 || (r_type >= (int) R_SH_FIRST_INVALID_RELOC
2913 && r_type <= (int) R_SH_LAST_INVALID_RELOC)
2914 || (r_type >= (int) R_SH_FIRST_INVALID_RELOC_2
2915 && r_type <= (int) R_SH_LAST_INVALID_RELOC_2))
2917 bfd_set_error (bfd_error_bad_value);
2918 return false;
2921 howto = sh_elf_howto_table + r_type;
2923 /* This is a final link. */
2924 h = NULL;
2925 sym = NULL;
2926 sec = NULL;
2927 if (r_symndx < symtab_hdr->sh_info)
2929 sym = local_syms + r_symndx;
2930 sec = local_sections[r_symndx];
2931 relocation = (sec->output_section->vma
2932 + sec->output_offset
2933 + sym->st_value);
2935 if (info->relocateable)
2937 /* This is a relocateable link. We don't have to change
2938 anything, unless the reloc is against a section symbol,
2939 in which case we have to adjust according to where the
2940 section symbol winds up in the output section. */
2941 sym = local_syms + r_symndx;
2942 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
2943 goto final_link_relocate;
2945 continue;
2948 else
2950 /* Section symbol are never (?) placed in the hash table, so
2951 we can just ignore hash relocations when creating a
2952 relocateable object file. */
2953 if (info->relocateable)
2954 continue;
2956 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
2957 while (h->root.type == bfd_link_hash_indirect
2958 || h->root.type == bfd_link_hash_warning)
2959 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2960 if (h->root.type == bfd_link_hash_defined
2961 || h->root.type == bfd_link_hash_defweak)
2963 sec = h->root.u.def.section;
2964 /* In these cases, we don't need the relocation value.
2965 We check specially because in some obscure cases
2966 sec->output_section will be NULL. */
2967 if (r_type == R_SH_GOTPC
2968 || (r_type == R_SH_PLT32
2969 && h->plt.offset != (bfd_vma) -1)
2970 || (r_type == R_SH_GOT32
2971 && elf_hash_table (info)->dynamic_sections_created
2972 && (! info->shared
2973 || (! info->symbolic && h->dynindx != -1)
2974 || (h->elf_link_hash_flags
2975 & ELF_LINK_HASH_DEF_REGULAR) == 0))
2976 /* The cases above are those in which relocation is
2977 overwritten in the switch block below. The cases
2978 below are those in which we must defer relocation
2979 to run-time, because we can't resolve absolute
2980 addresses when creating a shared library. */
2981 || (info->shared
2982 && ((! info->symbolic && h->dynindx != -1)
2983 || (h->elf_link_hash_flags
2984 & ELF_LINK_HASH_DEF_REGULAR) == 0)
2985 && ((r_type == R_SH_DIR32
2986 && !(ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
2987 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN))
2988 || r_type == R_SH_REL32)
2989 && ((input_section->flags & SEC_ALLOC) != 0
2990 /* DWARF will emit R_SH_DIR32 relocations in its
2991 sections against symbols defined externally
2992 in shared libraries. We can't do anything
2993 with them here. */
2994 || (input_section->flags & SEC_DEBUGGING) != 0)))
2995 relocation = 0;
2996 else if (sec->output_section == NULL)
2998 (*_bfd_error_handler)
2999 (_("%s: warning: unresolvable relocation against symbol `%s' from %s section"),
3000 bfd_get_filename (input_bfd), h->root.root.string,
3001 bfd_get_section_name (input_bfd, input_section));
3002 relocation = 0;
3004 else
3005 relocation = (h->root.u.def.value
3006 + sec->output_section->vma
3007 + sec->output_offset);
3009 else if (h->root.type == bfd_link_hash_undefweak)
3010 relocation = 0;
3011 else if (info->shared && !info->symbolic && !info->no_undefined)
3012 relocation = 0;
3013 else
3015 if (! ((*info->callbacks->undefined_symbol)
3016 (info, h->root.root.string, input_bfd,
3017 input_section, rel->r_offset, true)))
3018 return false;
3019 relocation = 0;
3023 switch ((int)r_type)
3025 final_link_relocate:
3026 /* COFF relocs don't use the addend. The addend is used for
3027 R_SH_DIR32 to be compatible with other compilers. */
3028 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
3029 contents, rel->r_offset,
3030 relocation, addend);
3031 break;
3033 case R_SH_IND12W:
3034 case R_SH_DIR8WPN:
3035 case R_SH_DIR8WPZ:
3036 case R_SH_DIR8WPL:
3037 /* These should normally be handled by the assembler, but at
3038 least IND12W is generated by ourselves, so we must deal
3039 with it. */
3040 relocation -= 4;
3041 goto final_link_relocate;
3043 default:
3044 bfd_set_error (bfd_error_bad_value);
3045 return false;
3047 case R_SH_DIR32:
3048 case R_SH_REL32:
3049 if (info->shared
3050 && (input_section->flags & SEC_ALLOC) != 0
3051 && (r_type != R_SH_REL32
3052 || (h != NULL
3053 && h->dynindx != -1
3054 && (! info->symbolic
3055 || (h->elf_link_hash_flags
3056 & ELF_LINK_HASH_DEF_REGULAR) == 0))))
3058 Elf_Internal_Rela outrel;
3059 boolean skip, relocate;
3061 /* When generating a shared object, these relocations
3062 are copied into the output file to be resolved at run
3063 time. */
3065 if (sreloc == NULL)
3067 const char *name;
3069 name = (bfd_elf_string_from_elf_section
3070 (input_bfd,
3071 elf_elfheader (input_bfd)->e_shstrndx,
3072 elf_section_data (input_section)->rel_hdr.sh_name));
3073 if (name == NULL)
3074 return false;
3076 BFD_ASSERT (strncmp (name, ".rela", 5) == 0
3077 && strcmp (bfd_get_section_name (input_bfd,
3078 input_section),
3079 name + 5) == 0);
3081 sreloc = bfd_get_section_by_name (dynobj, name);
3082 BFD_ASSERT (sreloc != NULL);
3085 skip = false;
3087 if (elf_section_data (input_section)->stab_info == NULL)
3088 outrel.r_offset = rel->r_offset;
3089 else
3091 bfd_vma off;
3093 off = (_bfd_stab_section_offset
3094 (output_bfd, &elf_hash_table (info)->stab_info,
3095 input_section,
3096 &elf_section_data (input_section)->stab_info,
3097 rel->r_offset));
3098 if (off == (bfd_vma) -1)
3099 skip = true;
3100 outrel.r_offset = off;
3103 outrel.r_offset += (input_section->output_section->vma
3104 + input_section->output_offset);
3106 if (skip)
3108 memset (&outrel, 0, sizeof outrel);
3109 relocate = false;
3111 else if (r_type == R_SH_REL32)
3113 BFD_ASSERT (h != NULL && h->dynindx != -1);
3114 relocate = false;
3115 outrel.r_info = ELF32_R_INFO (h->dynindx, R_SH_REL32);
3116 outrel.r_addend = rel->r_addend;
3118 else
3120 /* h->dynindx may be -1 if this symbol was marked to
3121 become local. */
3122 if (h == NULL
3123 || ((info->symbolic || h->dynindx == -1)
3124 && (h->elf_link_hash_flags
3125 & ELF_LINK_HASH_DEF_REGULAR) != 0))
3127 relocate = true;
3128 outrel.r_info = ELF32_R_INFO (0, R_SH_RELATIVE);
3129 outrel.r_addend = relocation + rel->r_addend;
3131 else
3133 BFD_ASSERT (h->dynindx != -1);
3134 relocate = false;
3135 outrel.r_info = ELF32_R_INFO (h->dynindx, R_SH_DIR32);
3136 outrel.r_addend = relocation + rel->r_addend;
3140 bfd_elf32_swap_reloca_out (output_bfd, &outrel,
3141 (((Elf32_External_Rela *)
3142 sreloc->contents)
3143 + sreloc->reloc_count));
3144 ++sreloc->reloc_count;
3146 /* If this reloc is against an external symbol, we do
3147 not want to fiddle with the addend. Otherwise, we
3148 need to include the symbol value so that it becomes
3149 an addend for the dynamic reloc. */
3150 if (! relocate)
3151 continue;
3153 else if (r_type == R_SH_DIR32)
3154 addend = rel->r_addend;
3155 goto final_link_relocate;
3157 case R_SH_GOT32:
3158 /* Relocation is to the entry for this symbol in the global
3159 offset table. */
3160 if (sgot == NULL)
3162 sgot = bfd_get_section_by_name (dynobj, ".got");
3163 BFD_ASSERT (sgot != NULL);
3166 if (h != NULL)
3168 bfd_vma off;
3170 off = h->got.offset;
3171 BFD_ASSERT (off != (bfd_vma) -1);
3173 if (! elf_hash_table (info)->dynamic_sections_created
3174 || (info->shared
3175 && (info->symbolic || h->dynindx == -1
3176 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
3177 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN)
3178 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR)))
3180 /* This is actually a static link, or it is a
3181 -Bsymbolic link and the symbol is defined
3182 locally, or the symbol was forced to be local
3183 because of a version file. We must initialize
3184 this entry in the global offset table. Since the
3185 offset must always be a multiple of 4, we use the
3186 least significant bit to record whether we have
3187 initialized it already.
3189 When doing a dynamic link, we create a .rela.got
3190 relocation entry to initialize the value. This
3191 is done in the finish_dynamic_symbol routine. */
3192 if ((off & 1) != 0)
3193 off &= ~1;
3194 else
3196 bfd_put_32 (output_bfd, relocation,
3197 sgot->contents + off);
3198 h->got.offset |= 1;
3202 relocation = sgot->output_offset + off;
3204 else
3206 bfd_vma off;
3208 BFD_ASSERT (local_got_offsets != NULL
3209 && local_got_offsets[r_symndx] != (bfd_vma) -1);
3211 off = local_got_offsets[r_symndx];
3213 /* The offset must always be a multiple of 4. We use
3214 the least significant bit to record whether we have
3215 already generated the necessary reloc. */
3216 if ((off & 1) != 0)
3217 off &= ~1;
3218 else
3220 bfd_put_32 (output_bfd, relocation, sgot->contents + off);
3222 if (info->shared)
3224 asection *srelgot;
3225 Elf_Internal_Rela outrel;
3227 srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
3228 BFD_ASSERT (srelgot != NULL);
3230 outrel.r_offset = (sgot->output_section->vma
3231 + sgot->output_offset
3232 + off);
3233 outrel.r_info = ELF32_R_INFO (0, R_SH_RELATIVE);
3234 outrel.r_addend = relocation;
3235 bfd_elf32_swap_reloca_out (output_bfd, &outrel,
3236 (((Elf32_External_Rela *)
3237 srelgot->contents)
3238 + srelgot->reloc_count));
3239 ++srelgot->reloc_count;
3242 local_got_offsets[r_symndx] |= 1;
3245 relocation = sgot->output_offset + off;
3248 goto final_link_relocate;
3250 case R_SH_GOTOFF:
3251 /* Relocation is relative to the start of the global offset
3252 table. */
3254 if (sgot == NULL)
3256 sgot = bfd_get_section_by_name (dynobj, ".got");
3257 BFD_ASSERT (sgot != NULL);
3260 /* Note that sgot->output_offset is not involved in this
3261 calculation. We always want the start of .got. If we
3262 defined _GLOBAL_OFFSET_TABLE in a different way, as is
3263 permitted by the ABI, we might have to change this
3264 calculation. */
3265 relocation -= sgot->output_section->vma;
3267 goto final_link_relocate;
3269 case R_SH_GOTPC:
3270 /* Use global offset table as symbol value. */
3272 if (sgot == NULL)
3274 sgot = bfd_get_section_by_name (dynobj, ".got");
3275 BFD_ASSERT (sgot != NULL);
3278 relocation = sgot->output_section->vma;
3280 goto final_link_relocate;
3282 case R_SH_PLT32:
3283 /* Relocation is to the entry for this symbol in the
3284 procedure linkage table. */
3286 /* Resolve a PLT reloc against a local symbol directly,
3287 without using the procedure linkage table. */
3288 if (h == NULL)
3289 goto final_link_relocate;
3291 if (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
3292 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN)
3293 goto final_link_relocate;
3295 if (h->plt.offset == (bfd_vma) -1)
3297 /* We didn't make a PLT entry for this symbol. This
3298 happens when statically linking PIC code, or when
3299 using -Bsymbolic. */
3300 goto final_link_relocate;
3303 if (splt == NULL)
3305 splt = bfd_get_section_by_name (dynobj, ".plt");
3306 BFD_ASSERT (splt != NULL);
3309 relocation = (splt->output_section->vma
3310 + splt->output_offset
3311 + h->plt.offset);
3313 goto final_link_relocate;
3315 case R_SH_LOOP_START:
3317 static bfd_vma start, end;
3319 start = (relocation + rel->r_addend
3320 - (sec->output_section->vma + sec->output_offset));
3321 r = sh_elf_reloc_loop (r_type, input_bfd, input_section, contents,
3322 rel->r_offset, sec, start, end);
3323 break;
3325 case R_SH_LOOP_END:
3326 end = (relocation + rel->r_addend
3327 - (sec->output_section->vma + sec->output_offset));
3328 r = sh_elf_reloc_loop (r_type, input_bfd, input_section, contents,
3329 rel->r_offset, sec, start, end);
3330 break;
3334 if (r != bfd_reloc_ok)
3336 switch (r)
3338 default:
3339 case bfd_reloc_outofrange:
3340 abort ();
3341 case bfd_reloc_overflow:
3343 const char *name;
3345 if (h != NULL)
3346 name = h->root.root.string;
3347 else
3349 name = (bfd_elf_string_from_elf_section
3350 (input_bfd, symtab_hdr->sh_link, sym->st_name));
3351 if (name == NULL)
3352 return false;
3353 if (*name == '\0')
3354 name = bfd_section_name (input_bfd, sec);
3356 if (! ((*info->callbacks->reloc_overflow)
3357 (info, name, howto->name, (bfd_vma) 0,
3358 input_bfd, input_section, rel->r_offset)))
3359 return false;
3361 break;
3366 return true;
3369 /* This is a version of bfd_generic_get_relocated_section_contents
3370 which uses sh_elf_relocate_section. */
3372 static bfd_byte *
3373 sh_elf_get_relocated_section_contents (output_bfd, link_info, link_order,
3374 data, relocateable, symbols)
3375 bfd *output_bfd;
3376 struct bfd_link_info *link_info;
3377 struct bfd_link_order *link_order;
3378 bfd_byte *data;
3379 boolean relocateable;
3380 asymbol **symbols;
3382 Elf_Internal_Shdr *symtab_hdr;
3383 asection *input_section = link_order->u.indirect.section;
3384 bfd *input_bfd = input_section->owner;
3385 asection **sections = NULL;
3386 Elf_Internal_Rela *internal_relocs = NULL;
3387 Elf32_External_Sym *external_syms = NULL;
3388 Elf_Internal_Sym *internal_syms = NULL;
3390 /* We only need to handle the case of relaxing, or of having a
3391 particular set of section contents, specially. */
3392 if (relocateable
3393 || elf_section_data (input_section)->this_hdr.contents == NULL)
3394 return bfd_generic_get_relocated_section_contents (output_bfd, link_info,
3395 link_order, data,
3396 relocateable,
3397 symbols);
3399 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
3401 memcpy (data, elf_section_data (input_section)->this_hdr.contents,
3402 input_section->_raw_size);
3404 if ((input_section->flags & SEC_RELOC) != 0
3405 && input_section->reloc_count > 0)
3407 Elf_Internal_Sym *isymp;
3408 asection **secpp;
3409 Elf32_External_Sym *esym, *esymend;
3411 if (symtab_hdr->contents != NULL)
3412 external_syms = (Elf32_External_Sym *) symtab_hdr->contents;
3413 else
3415 external_syms = ((Elf32_External_Sym *)
3416 bfd_malloc (symtab_hdr->sh_info
3417 * sizeof (Elf32_External_Sym)));
3418 if (external_syms == NULL && symtab_hdr->sh_info > 0)
3419 goto error_return;
3420 if (bfd_seek (input_bfd, symtab_hdr->sh_offset, SEEK_SET) != 0
3421 || (bfd_read (external_syms, sizeof (Elf32_External_Sym),
3422 symtab_hdr->sh_info, input_bfd)
3423 != (symtab_hdr->sh_info * sizeof (Elf32_External_Sym))))
3424 goto error_return;
3427 internal_relocs = (_bfd_elf32_link_read_relocs
3428 (input_bfd, input_section, (PTR) NULL,
3429 (Elf_Internal_Rela *) NULL, false));
3430 if (internal_relocs == NULL)
3431 goto error_return;
3433 internal_syms = ((Elf_Internal_Sym *)
3434 bfd_malloc (symtab_hdr->sh_info
3435 * sizeof (Elf_Internal_Sym)));
3436 if (internal_syms == NULL && symtab_hdr->sh_info > 0)
3437 goto error_return;
3439 sections = (asection **) bfd_malloc (symtab_hdr->sh_info
3440 * sizeof (asection *));
3441 if (sections == NULL && symtab_hdr->sh_info > 0)
3442 goto error_return;
3444 isymp = internal_syms;
3445 secpp = sections;
3446 esym = external_syms;
3447 esymend = esym + symtab_hdr->sh_info;
3448 for (; esym < esymend; ++esym, ++isymp, ++secpp)
3450 asection *isec;
3452 bfd_elf32_swap_symbol_in (input_bfd, esym, isymp);
3454 if (isymp->st_shndx == SHN_UNDEF)
3455 isec = bfd_und_section_ptr;
3456 else if (isymp->st_shndx > 0 && isymp->st_shndx < SHN_LORESERVE)
3457 isec = bfd_section_from_elf_index (input_bfd, isymp->st_shndx);
3458 else if (isymp->st_shndx == SHN_ABS)
3459 isec = bfd_abs_section_ptr;
3460 else if (isymp->st_shndx == SHN_COMMON)
3461 isec = bfd_com_section_ptr;
3462 else
3464 /* Who knows? */
3465 isec = NULL;
3468 *secpp = isec;
3471 if (! sh_elf_relocate_section (output_bfd, link_info, input_bfd,
3472 input_section, data, internal_relocs,
3473 internal_syms, sections))
3474 goto error_return;
3476 if (sections != NULL)
3477 free (sections);
3478 sections = NULL;
3479 if (internal_syms != NULL)
3480 free (internal_syms);
3481 internal_syms = NULL;
3482 if (external_syms != NULL && symtab_hdr->contents == NULL)
3483 free (external_syms);
3484 external_syms = NULL;
3485 if (internal_relocs != elf_section_data (input_section)->relocs)
3486 free (internal_relocs);
3487 internal_relocs = NULL;
3490 return data;
3492 error_return:
3493 if (internal_relocs != NULL
3494 && internal_relocs != elf_section_data (input_section)->relocs)
3495 free (internal_relocs);
3496 if (external_syms != NULL && symtab_hdr->contents == NULL)
3497 free (external_syms);
3498 if (internal_syms != NULL)
3499 free (internal_syms);
3500 if (sections != NULL)
3501 free (sections);
3502 return NULL;
3504 static asection *
3505 sh_elf_gc_mark_hook (abfd, info, rel, h, sym)
3506 bfd *abfd;
3507 struct bfd_link_info *info ATTRIBUTE_UNUSED;
3508 Elf_Internal_Rela *rel;
3509 struct elf_link_hash_entry *h;
3510 Elf_Internal_Sym *sym;
3512 if (h != NULL)
3514 switch (ELF32_R_TYPE (rel->r_info))
3516 case R_SH_GNU_VTINHERIT:
3517 case R_SH_GNU_VTENTRY:
3518 break;
3520 default:
3521 switch (h->root.type)
3523 case bfd_link_hash_defined:
3524 case bfd_link_hash_defweak:
3525 return h->root.u.def.section;
3527 case bfd_link_hash_common:
3528 return h->root.u.c.p->section;
3530 default:
3531 break;
3535 else
3537 if (!(elf_bad_symtab (abfd)
3538 && ELF_ST_BIND (sym->st_info) != STB_LOCAL)
3539 && ! ((sym->st_shndx <= 0 || sym->st_shndx >= SHN_LORESERVE)
3540 && sym->st_shndx != SHN_COMMON))
3542 return bfd_section_from_elf_index (abfd, sym->st_shndx);
3545 return NULL;
3548 /* Update the got entry reference counts for the section being removed. */
3550 static boolean
3551 sh_elf_gc_sweep_hook (abfd, info, sec, relocs)
3552 bfd *abfd ATTRIBUTE_UNUSED;
3553 struct bfd_link_info *info ATTRIBUTE_UNUSED;
3554 asection *sec ATTRIBUTE_UNUSED;
3555 const Elf_Internal_Rela *relocs ATTRIBUTE_UNUSED;
3557 /* We use got and plt entries for sh, but it would seem that the
3558 existing SH code does no sort of reference counting or whatnot on
3559 its GOT and PLT entries, so it is not possible to garbage collect
3560 them at this time. */
3561 return true;
3564 /* Look through the relocs for a section during the first phase.
3565 Since we don't do .gots or .plts, we just need to consider the
3566 virtual table relocs for gc. */
3568 static boolean
3569 sh_elf_check_relocs (abfd, info, sec, relocs)
3570 bfd *abfd;
3571 struct bfd_link_info *info;
3572 asection *sec;
3573 const Elf_Internal_Rela *relocs;
3575 Elf_Internal_Shdr *symtab_hdr;
3576 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
3577 const Elf_Internal_Rela *rel;
3578 const Elf_Internal_Rela *rel_end;
3579 bfd *dynobj;
3580 bfd_vma *local_got_offsets;
3581 asection *sgot;
3582 asection *srelgot;
3583 asection *sreloc;
3585 sgot = NULL;
3586 srelgot = NULL;
3587 sreloc = NULL;
3590 if (info->relocateable)
3591 return true;
3593 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
3594 sym_hashes = elf_sym_hashes (abfd);
3595 sym_hashes_end = sym_hashes + symtab_hdr->sh_size/sizeof(Elf32_External_Sym);
3596 if (!elf_bad_symtab (abfd))
3597 sym_hashes_end -= symtab_hdr->sh_info;
3599 dynobj = elf_hash_table (info)->dynobj;
3600 local_got_offsets = elf_local_got_offsets (abfd);
3602 rel_end = relocs + sec->reloc_count;
3603 for (rel = relocs; rel < rel_end; rel++)
3605 struct elf_link_hash_entry *h;
3606 unsigned long r_symndx;
3608 r_symndx = ELF32_R_SYM (rel->r_info);
3609 if (r_symndx < symtab_hdr->sh_info)
3610 h = NULL;
3611 else
3612 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
3614 /* Some relocs require a global offset table. */
3615 if (dynobj == NULL)
3617 switch (ELF32_R_TYPE (rel->r_info))
3619 case R_SH_GOT32:
3620 case R_SH_GOTOFF:
3621 case R_SH_GOTPC:
3622 elf_hash_table (info)->dynobj = dynobj = abfd;
3623 if (! _bfd_elf_create_got_section (dynobj, info))
3624 return false;
3625 break;
3627 default:
3628 break;
3632 switch (ELF32_R_TYPE (rel->r_info))
3634 /* This relocation describes the C++ object vtable hierarchy.
3635 Reconstruct it for later use during GC. */
3636 case R_SH_GNU_VTINHERIT:
3637 if (!_bfd_elf32_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
3638 return false;
3639 break;
3641 /* This relocation describes which C++ vtable entries are actually
3642 used. Record for later use during GC. */
3643 case R_SH_GNU_VTENTRY:
3644 if (!_bfd_elf32_gc_record_vtentry (abfd, sec, h, rel->r_addend))
3645 return false;
3646 break;
3648 case R_SH_GOT32:
3649 /* This symbol requires a global offset table entry. */
3651 if (sgot == NULL)
3653 sgot = bfd_get_section_by_name (dynobj, ".got");
3654 BFD_ASSERT (sgot != NULL);
3657 if (srelgot == NULL
3658 && (h != NULL || info->shared))
3660 srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
3661 if (srelgot == NULL)
3663 srelgot = bfd_make_section (dynobj, ".rela.got");
3664 if (srelgot == NULL
3665 || ! bfd_set_section_flags (dynobj, srelgot,
3666 (SEC_ALLOC
3667 | SEC_LOAD
3668 | SEC_HAS_CONTENTS
3669 | SEC_IN_MEMORY
3670 | SEC_LINKER_CREATED
3671 | SEC_READONLY))
3672 || ! bfd_set_section_alignment (dynobj, srelgot, 2))
3673 return false;
3677 if (h != NULL)
3679 if (h->got.offset != (bfd_vma) -1)
3681 /* We have already allocated space in the .got. */
3682 break;
3684 h->got.offset = sgot->_raw_size;
3686 /* Make sure this symbol is output as a dynamic symbol. */
3687 if (h->dynindx == -1)
3689 if (! bfd_elf32_link_record_dynamic_symbol (info, h))
3690 return false;
3693 srelgot->_raw_size += sizeof (Elf32_External_Rela);
3695 else
3697 /* This is a global offset table entry for a local
3698 symbol. */
3699 if (local_got_offsets == NULL)
3701 size_t size;
3702 register unsigned int i;
3704 size = symtab_hdr->sh_info * sizeof (bfd_vma);
3705 local_got_offsets = (bfd_vma *) bfd_alloc (abfd, size);
3706 if (local_got_offsets == NULL)
3707 return false;
3708 elf_local_got_offsets (abfd) = local_got_offsets;
3709 for (i = 0; i < symtab_hdr->sh_info; i++)
3710 local_got_offsets[i] = (bfd_vma) -1;
3712 if (local_got_offsets[r_symndx] != (bfd_vma) -1)
3714 /* We have already allocated space in the .got. */
3715 break;
3717 local_got_offsets[r_symndx] = sgot->_raw_size;
3719 if (info->shared)
3721 /* If we are generating a shared object, we need to
3722 output a R_SH_RELATIVE reloc so that the dynamic
3723 linker can adjust this GOT entry. */
3724 srelgot->_raw_size += sizeof (Elf32_External_Rela);
3728 sgot->_raw_size += 4;
3730 break;
3732 case R_SH_PLT32:
3733 /* This symbol requires a procedure linkage table entry. We
3734 actually build the entry in adjust_dynamic_symbol,
3735 because this might be a case of linking PIC code which is
3736 never referenced by a dynamic object, in which case we
3737 don't need to generate a procedure linkage table entry
3738 after all. */
3740 /* If this is a local symbol, we resolve it directly without
3741 creating a procedure linkage table entry. */
3742 if (h == NULL)
3743 continue;
3745 if (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
3746 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN)
3747 break;
3749 h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_PLT;
3751 break;
3753 case R_SH_DIR32:
3754 case R_SH_REL32:
3755 if (h != NULL)
3756 h->elf_link_hash_flags |= ELF_LINK_NON_GOT_REF;
3758 /* If we are creating a shared library, and this is a reloc
3759 against a global symbol, or a non PC relative reloc
3760 against a local symbol, then we need to copy the reloc
3761 into the shared library. However, if we are linking with
3762 -Bsymbolic, we do not need to copy a reloc against a
3763 global symbol which is defined in an object we are
3764 including in the link (i.e., DEF_REGULAR is set). At
3765 this point we have not seen all the input files, so it is
3766 possible that DEF_REGULAR is not set now but will be set
3767 later (it is never cleared). We account for that
3768 possibility below by storing information in the
3769 pcrel_relocs_copied field of the hash table entry. */
3770 if (info->shared
3771 && (sec->flags & SEC_ALLOC) != 0
3772 && (ELF32_R_TYPE (rel->r_info) != R_SH_REL32
3773 || (h != NULL
3774 && (! info->symbolic
3775 || (h->elf_link_hash_flags
3776 & ELF_LINK_HASH_DEF_REGULAR) == 0))))
3778 /* When creating a shared object, we must copy these
3779 reloc types into the output file. We create a reloc
3780 section in dynobj and make room for this reloc. */
3781 if (sreloc == NULL)
3783 const char *name;
3785 name = (bfd_elf_string_from_elf_section
3786 (abfd,
3787 elf_elfheader (abfd)->e_shstrndx,
3788 elf_section_data (sec)->rel_hdr.sh_name));
3789 if (name == NULL)
3790 return false;
3792 BFD_ASSERT (strncmp (name, ".rela", 5) == 0
3793 && strcmp (bfd_get_section_name (abfd, sec),
3794 name + 5) == 0);
3796 sreloc = bfd_get_section_by_name (dynobj, name);
3797 if (sreloc == NULL)
3799 flagword flags;
3801 sreloc = bfd_make_section (dynobj, name);
3802 flags = (SEC_HAS_CONTENTS | SEC_READONLY
3803 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3804 if ((sec->flags & SEC_ALLOC) != 0)
3805 flags |= SEC_ALLOC | SEC_LOAD;
3806 if (sreloc == NULL
3807 || ! bfd_set_section_flags (dynobj, sreloc, flags)
3808 || ! bfd_set_section_alignment (dynobj, sreloc, 2))
3809 return false;
3813 sreloc->_raw_size += sizeof (Elf32_External_Rela);
3815 /* If we are linking with -Bsymbolic, and this is a
3816 global symbol, we count the number of PC relative
3817 relocations we have entered for this symbol, so that
3818 we can discard them again if the symbol is later
3819 defined by a regular object. Note that this function
3820 is only called if we are using an elf_sh linker
3821 hash table, which means that h is really a pointer to
3822 an elf_sh_link_hash_entry. */
3823 if (h != NULL && info->symbolic
3824 && ELF32_R_TYPE (rel->r_info) == R_SH_REL32)
3826 struct elf_sh_link_hash_entry *eh;
3827 struct elf_sh_pcrel_relocs_copied *p;
3829 eh = (struct elf_sh_link_hash_entry *) h;
3831 for (p = eh->pcrel_relocs_copied; p != NULL; p = p->next)
3832 if (p->section == sreloc)
3833 break;
3835 if (p == NULL)
3837 p = ((struct elf_sh_pcrel_relocs_copied *)
3838 bfd_alloc (dynobj, sizeof *p));
3839 if (p == NULL)
3840 return false;
3841 p->next = eh->pcrel_relocs_copied;
3842 eh->pcrel_relocs_copied = p;
3843 p->section = sreloc;
3844 p->count = 0;
3847 ++p->count;
3851 break;
3855 return true;
3858 static boolean
3859 sh_elf_set_mach_from_flags (abfd)
3860 bfd * abfd;
3862 flagword flags = elf_elfheader (abfd)->e_flags;
3864 switch (flags & EF_SH_MACH_MASK)
3866 case EF_SH1:
3867 bfd_default_set_arch_mach (abfd, bfd_arch_sh, bfd_mach_sh);
3868 break;
3869 case EF_SH2:
3870 bfd_default_set_arch_mach (abfd, bfd_arch_sh, bfd_mach_sh2);
3871 break;
3872 case EF_SH_DSP:
3873 bfd_default_set_arch_mach (abfd, bfd_arch_sh, bfd_mach_sh_dsp);
3874 break;
3875 case EF_SH3:
3876 bfd_default_set_arch_mach (abfd, bfd_arch_sh, bfd_mach_sh3);
3877 break;
3878 case EF_SH3_DSP:
3879 bfd_default_set_arch_mach (abfd, bfd_arch_sh, bfd_mach_sh3_dsp);
3880 break;
3881 case EF_SH3E:
3882 bfd_default_set_arch_mach (abfd, bfd_arch_sh, bfd_mach_sh3e);
3883 break;
3884 case EF_SH_UNKNOWN:
3885 case EF_SH4:
3886 bfd_default_set_arch_mach (abfd, bfd_arch_sh, bfd_mach_sh4);
3887 break;
3888 default:
3889 return false;
3891 return true;
3894 /* Function to keep SH specific file flags. */
3895 static boolean
3896 sh_elf_set_private_flags (abfd, flags)
3897 bfd * abfd;
3898 flagword flags;
3900 BFD_ASSERT (! elf_flags_init (abfd)
3901 || elf_elfheader (abfd)->e_flags == flags);
3903 elf_elfheader (abfd)->e_flags = flags;
3904 elf_flags_init (abfd) = true;
3905 return sh_elf_set_mach_from_flags (abfd);
3908 /* Copy backend specific data from one object module to another */
3909 static boolean
3910 sh_elf_copy_private_data (ibfd, obfd)
3911 bfd * ibfd;
3912 bfd * obfd;
3914 if ( bfd_get_flavour (ibfd) != bfd_target_elf_flavour
3915 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
3916 return true;
3918 return sh_elf_set_private_flags (obfd, elf_elfheader (ibfd)->e_flags);
3921 /* This routine checks for linking big and little endian objects
3922 together, and for linking sh-dsp with sh3e / sh4 objects. */
3924 static boolean
3925 sh_elf_merge_private_data (ibfd, obfd)
3926 bfd *ibfd;
3927 bfd *obfd;
3929 flagword old_flags, new_flags;
3931 if (_bfd_generic_verify_endian_match (ibfd, obfd) == false)
3932 return false;
3934 if ( bfd_get_flavour (ibfd) != bfd_target_elf_flavour
3935 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
3936 return true;
3938 if (! elf_flags_init (obfd))
3940 /* This happens when ld starts out with a 'blank' output file. */
3941 elf_flags_init (obfd) = true;
3942 elf_elfheader (obfd)->e_flags = EF_SH1;
3944 old_flags = elf_elfheader (obfd)->e_flags;
3945 new_flags = elf_elfheader (ibfd)->e_flags;
3946 if ((EF_SH_HAS_DSP (old_flags) && EF_SH_HAS_FP (new_flags))
3947 || (EF_SH_HAS_DSP (new_flags) && EF_SH_HAS_FP (old_flags)))
3949 (*_bfd_error_handler)
3950 ("%s: uses %s instructions while previous modules use %s instructions",
3951 bfd_get_filename (ibfd),
3952 EF_SH_HAS_DSP (new_flags) ? "dsp" : "floating point",
3953 EF_SH_HAS_DSP (new_flags) ? "floating point" : "dsp");
3954 bfd_set_error (bfd_error_bad_value);
3955 return false;
3957 elf_elfheader (obfd)->e_flags = EF_SH_MERGE_MACH (old_flags, new_flags);
3959 return sh_elf_set_mach_from_flags (obfd);
3962 /* Finish up dynamic symbol handling. We set the contents of various
3963 dynamic sections here. */
3965 static boolean
3966 sh_elf_finish_dynamic_symbol (output_bfd, info, h, sym)
3967 bfd *output_bfd;
3968 struct bfd_link_info *info;
3969 struct elf_link_hash_entry *h;
3970 Elf_Internal_Sym *sym;
3972 bfd *dynobj;
3974 dynobj = elf_hash_table (info)->dynobj;
3976 if (h->plt.offset != (bfd_vma) -1)
3978 asection *splt;
3979 asection *sgot;
3980 asection *srel;
3982 bfd_vma plt_index;
3983 bfd_vma got_offset;
3984 Elf_Internal_Rela rel;
3986 /* This symbol has an entry in the procedure linkage table. Set
3987 it up. */
3989 BFD_ASSERT (h->dynindx != -1);
3991 splt = bfd_get_section_by_name (dynobj, ".plt");
3992 sgot = bfd_get_section_by_name (dynobj, ".got.plt");
3993 srel = bfd_get_section_by_name (dynobj, ".rela.plt");
3994 BFD_ASSERT (splt != NULL && sgot != NULL && srel != NULL);
3996 /* Get the index in the procedure linkage table which
3997 corresponds to this symbol. This is the index of this symbol
3998 in all the symbols for which we are making plt entries. The
3999 first entry in the procedure linkage table is reserved. */
4000 plt_index = h->plt.offset / elf_sh_sizeof_plt (info) - 1;
4002 /* Get the offset into the .got table of the entry that
4003 corresponds to this function. Each .got entry is 4 bytes.
4004 The first three are reserved. */
4005 got_offset = (plt_index + 3) * 4;
4007 /* Fill in the entry in the procedure linkage table. */
4008 if (! info->shared)
4010 if (elf_sh_plt_entry == NULL)
4012 elf_sh_plt_entry = (bfd_big_endian (output_bfd)?
4013 elf_sh_plt_entry_be : elf_sh_plt_entry_le);
4015 memcpy (splt->contents + h->plt.offset, elf_sh_plt_entry,
4016 elf_sh_sizeof_plt (info));
4017 bfd_put_32 (output_bfd,
4018 (sgot->output_section->vma
4019 + sgot->output_offset
4020 + got_offset),
4021 (splt->contents + h->plt.offset
4022 + elf_sh_plt_symbol_offset (info)));
4024 bfd_put_32 (output_bfd,
4025 (splt->output_section->vma + splt->output_offset),
4026 (splt->contents + h->plt.offset
4027 + elf_sh_plt_plt0_offset (info)));
4029 else
4031 if (elf_sh_pic_plt_entry == NULL)
4033 elf_sh_pic_plt_entry = (bfd_big_endian (output_bfd)?
4034 elf_sh_pic_plt_entry_be :
4035 elf_sh_pic_plt_entry_le);
4037 memcpy (splt->contents + h->plt.offset, elf_sh_pic_plt_entry,
4038 elf_sh_sizeof_plt (info));
4039 bfd_put_32 (output_bfd, got_offset,
4040 (splt->contents + h->plt.offset
4041 + elf_sh_plt_symbol_offset (info)));
4044 bfd_put_32 (output_bfd, plt_index * sizeof (Elf32_External_Rela),
4045 (splt->contents + h->plt.offset
4046 + elf_sh_plt_reloc_offset (info)));
4048 /* Fill in the entry in the global offset table. */
4049 bfd_put_32 (output_bfd,
4050 (splt->output_section->vma
4051 + splt->output_offset
4052 + h->plt.offset
4053 + elf_sh_plt_temp_offset (info)),
4054 sgot->contents + got_offset);
4056 /* Fill in the entry in the .rela.plt section. */
4057 rel.r_offset = (sgot->output_section->vma
4058 + sgot->output_offset
4059 + got_offset);
4060 rel.r_info = ELF32_R_INFO (h->dynindx, R_SH_JMP_SLOT);
4061 rel.r_addend = 0;
4062 bfd_elf32_swap_reloca_out (output_bfd, &rel,
4063 ((Elf32_External_Rela *) srel->contents
4064 + plt_index));
4066 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
4068 /* Mark the symbol as undefined, rather than as defined in
4069 the .plt section. Leave the value alone. */
4070 sym->st_shndx = SHN_UNDEF;
4074 if (h->got.offset != (bfd_vma) -1)
4076 asection *sgot;
4077 asection *srel;
4078 Elf_Internal_Rela rel;
4080 /* This symbol has an entry in the global offset table. Set it
4081 up. */
4083 sgot = bfd_get_section_by_name (dynobj, ".got");
4084 srel = bfd_get_section_by_name (dynobj, ".rela.got");
4085 BFD_ASSERT (sgot != NULL && srel != NULL);
4087 rel.r_offset = (sgot->output_section->vma
4088 + sgot->output_offset
4089 + (h->got.offset &~ 1));
4091 /* If this is a -Bsymbolic link, and the symbol is defined
4092 locally, we just want to emit a RELATIVE reloc. Likewise if
4093 the symbol was forced to be local because of a version file.
4094 The entry in the global offset table will already have been
4095 initialized in the relocate_section function. */
4096 if (info->shared
4097 && (info->symbolic || h->dynindx == -1)
4098 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR))
4100 rel.r_info = ELF32_R_INFO (0, R_SH_RELATIVE);
4101 rel.r_addend = (h->root.u.def.value
4102 + h->root.u.def.section->output_section->vma
4103 + h->root.u.def.section->output_offset);
4105 else
4107 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + h->got.offset);
4108 rel.r_info = ELF32_R_INFO (h->dynindx, R_SH_GLOB_DAT);
4109 rel.r_addend = 0;
4112 bfd_elf32_swap_reloca_out (output_bfd, &rel,
4113 ((Elf32_External_Rela *) srel->contents
4114 + srel->reloc_count));
4115 ++srel->reloc_count;
4118 if ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_COPY) != 0)
4120 asection *s;
4121 Elf_Internal_Rela rel;
4123 /* This symbol needs a copy reloc. Set it up. */
4125 BFD_ASSERT (h->dynindx != -1
4126 && (h->root.type == bfd_link_hash_defined
4127 || h->root.type == bfd_link_hash_defweak));
4129 s = bfd_get_section_by_name (h->root.u.def.section->owner,
4130 ".rela.bss");
4131 BFD_ASSERT (s != NULL);
4133 rel.r_offset = (h->root.u.def.value
4134 + h->root.u.def.section->output_section->vma
4135 + h->root.u.def.section->output_offset);
4136 rel.r_info = ELF32_R_INFO (h->dynindx, R_SH_COPY);
4137 rel.r_addend = 0;
4138 bfd_elf32_swap_reloca_out (output_bfd, &rel,
4139 ((Elf32_External_Rela *) s->contents
4140 + s->reloc_count));
4141 ++s->reloc_count;
4144 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. */
4145 if (strcmp (h->root.root.string, "_DYNAMIC") == 0
4146 || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
4147 sym->st_shndx = SHN_ABS;
4149 return true;
4152 /* Finish up the dynamic sections. */
4154 static boolean
4155 sh_elf_finish_dynamic_sections (output_bfd, info)
4156 bfd *output_bfd;
4157 struct bfd_link_info *info;
4159 bfd *dynobj;
4160 asection *sgot;
4161 asection *sdyn;
4163 dynobj = elf_hash_table (info)->dynobj;
4165 sgot = bfd_get_section_by_name (dynobj, ".got.plt");
4166 BFD_ASSERT (sgot != NULL);
4167 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
4169 if (elf_hash_table (info)->dynamic_sections_created)
4171 asection *splt;
4172 Elf32_External_Dyn *dyncon, *dynconend;
4174 BFD_ASSERT (sdyn != NULL);
4176 dyncon = (Elf32_External_Dyn *) sdyn->contents;
4177 dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->_raw_size);
4178 for (; dyncon < dynconend; dyncon++)
4180 Elf_Internal_Dyn dyn;
4181 const char *name;
4182 asection *s;
4184 bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
4186 switch (dyn.d_tag)
4188 default:
4189 break;
4191 case DT_PLTGOT:
4192 name = ".got";
4193 goto get_vma;
4195 case DT_JMPREL:
4196 name = ".rela.plt";
4197 get_vma:
4198 s = bfd_get_section_by_name (output_bfd, name);
4199 BFD_ASSERT (s != NULL);
4200 dyn.d_un.d_ptr = s->vma;
4201 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
4202 break;
4204 case DT_PLTRELSZ:
4205 s = bfd_get_section_by_name (output_bfd, ".rela.plt");
4206 BFD_ASSERT (s != NULL);
4207 if (s->_cooked_size != 0)
4208 dyn.d_un.d_val = s->_cooked_size;
4209 else
4210 dyn.d_un.d_val = s->_raw_size;
4211 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
4212 break;
4214 case DT_RELASZ:
4215 /* My reading of the SVR4 ABI indicates that the
4216 procedure linkage table relocs (DT_JMPREL) should be
4217 included in the overall relocs (DT_RELA). This is
4218 what Solaris does. However, UnixWare can not handle
4219 that case. Therefore, we override the DT_RELASZ entry
4220 here to make it not include the JMPREL relocs. Since
4221 the linker script arranges for .rela.plt to follow all
4222 other relocation sections, we don't have to worry
4223 about changing the DT_RELA entry. */
4224 s = bfd_get_section_by_name (output_bfd, ".rela.plt");
4225 if (s != NULL)
4227 if (s->_cooked_size != 0)
4228 dyn.d_un.d_val -= s->_cooked_size;
4229 else
4230 dyn.d_un.d_val -= s->_raw_size;
4232 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
4233 break;
4237 /* Fill in the first entry in the procedure linkage table. */
4238 splt = bfd_get_section_by_name (dynobj, ".plt");
4239 if (splt && splt->_raw_size > 0)
4241 if (info->shared)
4243 if (elf_sh_pic_plt_entry == NULL)
4245 elf_sh_pic_plt_entry = (bfd_big_endian (output_bfd)?
4246 elf_sh_pic_plt_entry_be :
4247 elf_sh_pic_plt_entry_le);
4249 memcpy (splt->contents, elf_sh_pic_plt_entry,
4250 elf_sh_sizeof_plt (info));
4252 else
4254 if (elf_sh_plt0_entry == NULL)
4256 elf_sh_plt0_entry = (bfd_big_endian (output_bfd)?
4257 elf_sh_plt0_entry_be :
4258 elf_sh_plt0_entry_le);
4260 memcpy (splt->contents, elf_sh_plt0_entry, PLT_ENTRY_SIZE);
4261 bfd_put_32 (output_bfd,
4262 sgot->output_section->vma + sgot->output_offset + 4,
4263 splt->contents + elf_sh_plt0_gotid_offset (info));
4264 bfd_put_32 (output_bfd,
4265 sgot->output_section->vma + sgot->output_offset + 8,
4266 splt->contents + elf_sh_plt0_linker_offset (info));
4269 /* UnixWare sets the entsize of .plt to 4, although that doesn't
4270 really seem like the right value. */
4271 elf_section_data (splt->output_section)->this_hdr.sh_entsize = 4;
4275 /* Fill in the first three entries in the global offset table. */
4276 if (sgot->_raw_size > 0)
4278 if (sdyn == NULL)
4279 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents);
4280 else
4281 bfd_put_32 (output_bfd,
4282 sdyn->output_section->vma + sdyn->output_offset,
4283 sgot->contents);
4284 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 4);
4285 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 8);
4288 elf_section_data (sgot->output_section)->this_hdr.sh_entsize = 4;
4290 return true;
4293 #ifndef ELF_ARCH
4294 #define TARGET_BIG_SYM bfd_elf32_sh_vec
4295 #define TARGET_BIG_NAME "elf32-sh"
4296 #define TARGET_LITTLE_SYM bfd_elf32_shl_vec
4297 #define TARGET_LITTLE_NAME "elf32-shl"
4298 #define ELF_ARCH bfd_arch_sh
4299 #define ELF_MACHINE_CODE EM_SH
4300 #define ELF_MAXPAGESIZE 128
4302 #define elf_symbol_leading_char '_'
4303 #endif /* ELF_ARCH */
4305 #define bfd_elf32_bfd_reloc_type_lookup sh_elf_reloc_type_lookup
4306 #define elf_info_to_howto sh_elf_info_to_howto
4307 #define bfd_elf32_bfd_relax_section sh_elf_relax_section
4308 #define elf_backend_relocate_section sh_elf_relocate_section
4309 #define bfd_elf32_bfd_get_relocated_section_contents \
4310 sh_elf_get_relocated_section_contents
4311 #define elf_backend_object_p sh_elf_set_mach_from_flags
4312 #define bfd_elf32_bfd_set_private_bfd_flags \
4313 sh_elf_set_private_flags
4314 #define bfd_elf32_bfd_copy_private_bfd_data \
4315 sh_elf_copy_private_data
4316 #define bfd_elf32_bfd_merge_private_bfd_data \
4317 sh_elf_merge_private_data
4319 #define elf_backend_gc_mark_hook sh_elf_gc_mark_hook
4320 #define elf_backend_gc_sweep_hook sh_elf_gc_sweep_hook
4321 #define elf_backend_check_relocs sh_elf_check_relocs
4323 #define elf_backend_can_gc_sections 1
4324 #define elf_backend_create_dynamic_sections \
4325 sh_elf_create_dynamic_sections
4326 #define bfd_elf32_bfd_link_hash_table_create \
4327 sh_elf_link_hash_table_create
4328 #define elf_backend_adjust_dynamic_symbol \
4329 sh_elf_adjust_dynamic_symbol
4330 #define elf_backend_size_dynamic_sections \
4331 sh_elf_size_dynamic_sections
4332 #define elf_backend_finish_dynamic_symbol \
4333 sh_elf_finish_dynamic_symbol
4334 #define elf_backend_finish_dynamic_sections \
4335 sh_elf_finish_dynamic_sections
4337 #define elf_backend_want_got_plt 1
4338 #define elf_backend_plt_readonly 1
4339 #define elf_backend_want_plt_sym 0
4340 #define elf_backend_got_header_size 12
4341 #define elf_backend_plt_header_size PLT_ENTRY_SIZE
4342 #include "elf32-target.h"