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