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