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

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

Initial revision

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