Develop and Download Open Source Software

Browse Subversion Repository

Contents of /trunk/bfd/elf32-sh.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 21 - (show annotations) (download) (as text)
Mon Jul 27 20:34:36 2009 UTC (14 years, 8 months ago) by monamour
File MIME type: text/x-csrc
File size: 178037 byte(s)
Update to HEAD.
1 /* Renesas / SuperH SH specific support for 32-bit ELF
2 Copyright 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
3 2006, 2007, 2008 Free Software Foundation, Inc.
4 Contributed by Ian Lance Taylor, Cygnus Support.
5
6 This file is part of BFD, the Binary File Descriptor library.
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21 MA 02110-1301, USA. */
22
23 #include "sysdep.h"
24 #include "bfd.h"
25 #include "bfdlink.h"
26 #include "libbfd.h"
27 #include "elf-bfd.h"
28 #include "elf-vxworks.h"
29 #include "elf/sh.h"
30 #include "libiberty.h"
31 #include "../opcodes/sh-opc.h"
32
33 static bfd_reloc_status_type sh_elf_reloc
34 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
35 static bfd_reloc_status_type sh_elf_ignore_reloc
36 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
37 static bfd_boolean sh_elf_relax_delete_bytes
38 (bfd *, asection *, bfd_vma, int);
39 static bfd_boolean sh_elf_align_loads
40 (bfd *, asection *, Elf_Internal_Rela *, bfd_byte *, bfd_boolean *);
41 #ifndef SH64_ELF
42 static bfd_boolean sh_elf_swap_insns
43 (bfd *, asection *, void *, bfd_byte *, bfd_vma);
44 #endif
45 static int sh_elf_optimized_tls_reloc
46 (struct bfd_link_info *, int, int);
47 static bfd_vma dtpoff_base
48 (struct bfd_link_info *);
49 static bfd_vma tpoff
50 (struct bfd_link_info *, bfd_vma);
51
52 /* The name of the dynamic interpreter. This is put in the .interp
53 section. */
54
55 #define ELF_DYNAMIC_INTERPRETER "/usr/lib/libc.so.1"
56
57 #define MINUS_ONE ((bfd_vma) 0 - 1)
58
59 #define SH_PARTIAL32 TRUE
60 #define SH_SRC_MASK32 0xffffffff
61 #define SH_ELF_RELOC sh_elf_reloc
62 static reloc_howto_type sh_elf_howto_table[] =
63 {
64 #include "elf32-sh-relocs.h"
65 };
66
67 #define SH_PARTIAL32 FALSE
68 #define SH_SRC_MASK32 0
69 #define SH_ELF_RELOC bfd_elf_generic_reloc
70 static reloc_howto_type sh_vxworks_howto_table[] =
71 {
72 #include "elf32-sh-relocs.h"
73 };
74
75 /* Return true if OUTPUT_BFD is a VxWorks object. */
76
77 static bfd_boolean
78 vxworks_object_p (bfd *abfd ATTRIBUTE_UNUSED)
79 {
80 #if !defined INCLUDE_SHMEDIA && !defined SH_TARGET_ALREADY_DEFINED
81 extern const bfd_target bfd_elf32_shlvxworks_vec;
82 extern const bfd_target bfd_elf32_shvxworks_vec;
83
84 return (abfd->xvec == &bfd_elf32_shlvxworks_vec
85 || abfd->xvec == &bfd_elf32_shvxworks_vec);
86 #else
87 return FALSE;
88 #endif
89 }
90
91 /* Return the howto table for ABFD. */
92
93 static reloc_howto_type *
94 get_howto_table (bfd *abfd)
95 {
96 if (vxworks_object_p (abfd))
97 return sh_vxworks_howto_table;
98 return sh_elf_howto_table;
99 }
100
101 static bfd_reloc_status_type
102 sh_elf_reloc_loop (int r_type ATTRIBUTE_UNUSED, bfd *input_bfd,
103 asection *input_section, bfd_byte *contents,
104 bfd_vma addr, asection *symbol_section,
105 bfd_vma start, bfd_vma end)
106 {
107 static bfd_vma last_addr;
108 static asection *last_symbol_section;
109 bfd_byte *start_ptr, *ptr, *last_ptr;
110 int diff, cum_diff;
111 bfd_signed_vma x;
112 int insn;
113
114 /* Sanity check the address. */
115 if (addr > bfd_get_section_limit (input_bfd, input_section))
116 return bfd_reloc_outofrange;
117
118 /* We require the start and end relocations to be processed consecutively -
119 although we allow then to be processed forwards or backwards. */
120 if (! last_addr)
121 {
122 last_addr = addr;
123 last_symbol_section = symbol_section;
124 return bfd_reloc_ok;
125 }
126 if (last_addr != addr)
127 abort ();
128 last_addr = 0;
129
130 if (! symbol_section || last_symbol_section != symbol_section || end < start)
131 return bfd_reloc_outofrange;
132
133 /* Get the symbol_section contents. */
134 if (symbol_section != input_section)
135 {
136 if (elf_section_data (symbol_section)->this_hdr.contents != NULL)
137 contents = elf_section_data (symbol_section)->this_hdr.contents;
138 else
139 {
140 if (!bfd_malloc_and_get_section (input_bfd, symbol_section,
141 &contents))
142 {
143 if (contents != NULL)
144 free (contents);
145 return bfd_reloc_outofrange;
146 }
147 }
148 }
149 #define IS_PPI(PTR) ((bfd_get_16 (input_bfd, (PTR)) & 0xfc00) == 0xf800)
150 start_ptr = contents + start;
151 for (cum_diff = -6, ptr = contents + end; cum_diff < 0 && ptr > start_ptr;)
152 {
153 for (last_ptr = ptr, ptr -= 4; ptr >= start_ptr && IS_PPI (ptr);)
154 ptr -= 2;
155 ptr += 2;
156 diff = (last_ptr - ptr) >> 1;
157 cum_diff += diff & 1;
158 cum_diff += diff;
159 }
160 /* Calculate the start / end values to load into rs / re minus four -
161 so that will cancel out the four we would otherwise have to add to
162 addr to get the value to subtract in order to get relative addressing. */
163 if (cum_diff >= 0)
164 {
165 start -= 4;
166 end = (ptr + cum_diff * 2) - contents;
167 }
168 else
169 {
170 bfd_vma start0 = start - 4;
171
172 while (start0 && IS_PPI (contents + start0))
173 start0 -= 2;
174 start0 = start - 2 - ((start - start0) & 2);
175 start = start0 - cum_diff - 2;
176 end = start0;
177 }
178
179 if (contents != NULL
180 && elf_section_data (symbol_section)->this_hdr.contents != contents)
181 free (contents);
182
183 insn = bfd_get_16 (input_bfd, contents + addr);
184
185 x = (insn & 0x200 ? end : start) - addr;
186 if (input_section != symbol_section)
187 x += ((symbol_section->output_section->vma + symbol_section->output_offset)
188 - (input_section->output_section->vma
189 + input_section->output_offset));
190 x >>= 1;
191 if (x < -128 || x > 127)
192 return bfd_reloc_overflow;
193
194 x = (insn & ~0xff) | (x & 0xff);
195 bfd_put_16 (input_bfd, (bfd_vma) x, contents + addr);
196
197 return bfd_reloc_ok;
198 }
199
200 /* This function is used for normal relocs. This used to be like the COFF
201 function, and is almost certainly incorrect for other ELF targets. */
202
203 static bfd_reloc_status_type
204 sh_elf_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol_in,
205 void *data, asection *input_section, bfd *output_bfd,
206 char **error_message ATTRIBUTE_UNUSED)
207 {
208 unsigned long insn;
209 bfd_vma sym_value;
210 enum elf_sh_reloc_type r_type;
211 bfd_vma addr = reloc_entry->address;
212 bfd_byte *hit_data = addr + (bfd_byte *) data;
213
214 r_type = (enum elf_sh_reloc_type) reloc_entry->howto->type;
215
216 if (output_bfd != NULL)
217 {
218 /* Partial linking--do nothing. */
219 reloc_entry->address += input_section->output_offset;
220 return bfd_reloc_ok;
221 }
222
223 /* Almost all relocs have to do with relaxing. If any work must be
224 done for them, it has been done in sh_relax_section. */
225 if (r_type == R_SH_IND12W && (symbol_in->flags & BSF_LOCAL) != 0)
226 return bfd_reloc_ok;
227
228 if (symbol_in != NULL
229 && bfd_is_und_section (symbol_in->section))
230 return bfd_reloc_undefined;
231
232 if (bfd_is_com_section (symbol_in->section))
233 sym_value = 0;
234 else
235 sym_value = (symbol_in->value +
236 symbol_in->section->output_section->vma +
237 symbol_in->section->output_offset);
238
239 switch (r_type)
240 {
241 case R_SH_DIR32:
242 insn = bfd_get_32 (abfd, hit_data);
243 insn += sym_value + reloc_entry->addend;
244 bfd_put_32 (abfd, (bfd_vma) insn, hit_data);
245 break;
246 case R_SH_IND12W:
247 insn = bfd_get_16 (abfd, hit_data);
248 sym_value += reloc_entry->addend;
249 sym_value -= (input_section->output_section->vma
250 + input_section->output_offset
251 + addr
252 + 4);
253 sym_value += (insn & 0xfff) << 1;
254 if (insn & 0x800)
255 sym_value -= 0x1000;
256 insn = (insn & 0xf000) | (sym_value & 0xfff);
257 bfd_put_16 (abfd, (bfd_vma) insn, hit_data);
258 if (sym_value < (bfd_vma) -0x1000 || sym_value >= 0x1000)
259 return bfd_reloc_overflow;
260 break;
261 default:
262 abort ();
263 break;
264 }
265
266 return bfd_reloc_ok;
267 }
268
269 /* This function is used for relocs which are only used for relaxing,
270 which the linker should otherwise ignore. */
271
272 static bfd_reloc_status_type
273 sh_elf_ignore_reloc (bfd *abfd ATTRIBUTE_UNUSED, arelent *reloc_entry,
274 asymbol *symbol ATTRIBUTE_UNUSED,
275 void *data ATTRIBUTE_UNUSED, asection *input_section,
276 bfd *output_bfd,
277 char **error_message ATTRIBUTE_UNUSED)
278 {
279 if (output_bfd != NULL)
280 reloc_entry->address += input_section->output_offset;
281 return bfd_reloc_ok;
282 }
283
284 /* This structure is used to map BFD reloc codes to SH ELF relocs. */
285
286 struct elf_reloc_map
287 {
288 bfd_reloc_code_real_type bfd_reloc_val;
289 unsigned char elf_reloc_val;
290 };
291
292 /* An array mapping BFD reloc codes to SH ELF relocs. */
293
294 static const struct elf_reloc_map sh_reloc_map[] =
295 {
296 { BFD_RELOC_NONE, R_SH_NONE },
297 { BFD_RELOC_32, R_SH_DIR32 },
298 { BFD_RELOC_16, R_SH_DIR16 },
299 { BFD_RELOC_8, R_SH_DIR8 },
300 { BFD_RELOC_CTOR, R_SH_DIR32 },
301 { BFD_RELOC_32_PCREL, R_SH_REL32 },
302 { BFD_RELOC_SH_PCDISP8BY2, R_SH_DIR8WPN },
303 { BFD_RELOC_SH_PCDISP12BY2, R_SH_IND12W },
304 { BFD_RELOC_SH_PCRELIMM8BY2, R_SH_DIR8WPZ },
305 { BFD_RELOC_SH_PCRELIMM8BY4, R_SH_DIR8WPL },
306 { BFD_RELOC_8_PCREL, R_SH_SWITCH8 },
307 { BFD_RELOC_SH_SWITCH16, R_SH_SWITCH16 },
308 { BFD_RELOC_SH_SWITCH32, R_SH_SWITCH32 },
309 { BFD_RELOC_SH_USES, R_SH_USES },
310 { BFD_RELOC_SH_COUNT, R_SH_COUNT },
311 { BFD_RELOC_SH_ALIGN, R_SH_ALIGN },
312 { BFD_RELOC_SH_CODE, R_SH_CODE },
313 { BFD_RELOC_SH_DATA, R_SH_DATA },
314 { BFD_RELOC_SH_LABEL, R_SH_LABEL },
315 { BFD_RELOC_VTABLE_INHERIT, R_SH_GNU_VTINHERIT },
316 { BFD_RELOC_VTABLE_ENTRY, R_SH_GNU_VTENTRY },
317 { BFD_RELOC_SH_LOOP_START, R_SH_LOOP_START },
318 { BFD_RELOC_SH_LOOP_END, R_SH_LOOP_END },
319 { BFD_RELOC_SH_TLS_GD_32, R_SH_TLS_GD_32 },
320 { BFD_RELOC_SH_TLS_LD_32, R_SH_TLS_LD_32 },
321 { BFD_RELOC_SH_TLS_LDO_32, R_SH_TLS_LDO_32 },
322 { BFD_RELOC_SH_TLS_IE_32, R_SH_TLS_IE_32 },
323 { BFD_RELOC_SH_TLS_LE_32, R_SH_TLS_LE_32 },
324 { BFD_RELOC_SH_TLS_DTPMOD32, R_SH_TLS_DTPMOD32 },
325 { BFD_RELOC_SH_TLS_DTPOFF32, R_SH_TLS_DTPOFF32 },
326 { BFD_RELOC_SH_TLS_TPOFF32, R_SH_TLS_TPOFF32 },
327 { BFD_RELOC_32_GOT_PCREL, R_SH_GOT32 },
328 { BFD_RELOC_32_PLT_PCREL, R_SH_PLT32 },
329 { BFD_RELOC_SH_COPY, R_SH_COPY },
330 { BFD_RELOC_SH_GLOB_DAT, R_SH_GLOB_DAT },
331 { BFD_RELOC_SH_JMP_SLOT, R_SH_JMP_SLOT },
332 { BFD_RELOC_SH_RELATIVE, R_SH_RELATIVE },
333 { BFD_RELOC_32_GOTOFF, R_SH_GOTOFF },
334 { BFD_RELOC_SH_GOTPC, R_SH_GOTPC },
335 { BFD_RELOC_SH_GOTPLT32, R_SH_GOTPLT32 },
336 #ifdef INCLUDE_SHMEDIA
337 { BFD_RELOC_SH_GOT_LOW16, R_SH_GOT_LOW16 },
338 { BFD_RELOC_SH_GOT_MEDLOW16, R_SH_GOT_MEDLOW16 },
339 { BFD_RELOC_SH_GOT_MEDHI16, R_SH_GOT_MEDHI16 },
340 { BFD_RELOC_SH_GOT_HI16, R_SH_GOT_HI16 },
341 { BFD_RELOC_SH_GOTPLT_LOW16, R_SH_GOTPLT_LOW16 },
342 { BFD_RELOC_SH_GOTPLT_MEDLOW16, R_SH_GOTPLT_MEDLOW16 },
343 { BFD_RELOC_SH_GOTPLT_MEDHI16, R_SH_GOTPLT_MEDHI16 },
344 { BFD_RELOC_SH_GOTPLT_HI16, R_SH_GOTPLT_HI16 },
345 { BFD_RELOC_SH_PLT_LOW16, R_SH_PLT_LOW16 },
346 { BFD_RELOC_SH_PLT_MEDLOW16, R_SH_PLT_MEDLOW16 },
347 { BFD_RELOC_SH_PLT_MEDHI16, R_SH_PLT_MEDHI16 },
348 { BFD_RELOC_SH_PLT_HI16, R_SH_PLT_HI16 },
349 { BFD_RELOC_SH_GOTOFF_LOW16, R_SH_GOTOFF_LOW16 },
350 { BFD_RELOC_SH_GOTOFF_MEDLOW16, R_SH_GOTOFF_MEDLOW16 },
351 { BFD_RELOC_SH_GOTOFF_MEDHI16, R_SH_GOTOFF_MEDHI16 },
352 { BFD_RELOC_SH_GOTOFF_HI16, R_SH_GOTOFF_HI16 },
353 { BFD_RELOC_SH_GOTPC_LOW16, R_SH_GOTPC_LOW16 },
354 { BFD_RELOC_SH_GOTPC_MEDLOW16, R_SH_GOTPC_MEDLOW16 },
355 { BFD_RELOC_SH_GOTPC_MEDHI16, R_SH_GOTPC_MEDHI16 },
356 { BFD_RELOC_SH_GOTPC_HI16, R_SH_GOTPC_HI16 },
357 { BFD_RELOC_SH_COPY64, R_SH_COPY64 },
358 { BFD_RELOC_SH_GLOB_DAT64, R_SH_GLOB_DAT64 },
359 { BFD_RELOC_SH_JMP_SLOT64, R_SH_JMP_SLOT64 },
360 { BFD_RELOC_SH_RELATIVE64, R_SH_RELATIVE64 },
361 { BFD_RELOC_SH_GOT10BY4, R_SH_GOT10BY4 },
362 { BFD_RELOC_SH_GOT10BY8, R_SH_GOT10BY8 },
363 { BFD_RELOC_SH_GOTPLT10BY4, R_SH_GOTPLT10BY4 },
364 { BFD_RELOC_SH_GOTPLT10BY8, R_SH_GOTPLT10BY8 },
365 { BFD_RELOC_SH_PT_16, R_SH_PT_16 },
366 { BFD_RELOC_SH_SHMEDIA_CODE, R_SH_SHMEDIA_CODE },
367 { BFD_RELOC_SH_IMMU5, R_SH_DIR5U },
368 { BFD_RELOC_SH_IMMS6, R_SH_DIR6S },
369 { BFD_RELOC_SH_IMMU6, R_SH_DIR6U },
370 { BFD_RELOC_SH_IMMS10, R_SH_DIR10S },
371 { BFD_RELOC_SH_IMMS10BY2, R_SH_DIR10SW },
372 { BFD_RELOC_SH_IMMS10BY4, R_SH_DIR10SL },
373 { BFD_RELOC_SH_IMMS10BY8, R_SH_DIR10SQ },
374 { BFD_RELOC_SH_IMMS16, R_SH_IMMS16 },
375 { BFD_RELOC_SH_IMMU16, R_SH_IMMU16 },
376 { BFD_RELOC_SH_IMM_LOW16, R_SH_IMM_LOW16 },
377 { BFD_RELOC_SH_IMM_LOW16_PCREL, R_SH_IMM_LOW16_PCREL },
378 { BFD_RELOC_SH_IMM_MEDLOW16, R_SH_IMM_MEDLOW16 },
379 { BFD_RELOC_SH_IMM_MEDLOW16_PCREL, R_SH_IMM_MEDLOW16_PCREL },
380 { BFD_RELOC_SH_IMM_MEDHI16, R_SH_IMM_MEDHI16 },
381 { BFD_RELOC_SH_IMM_MEDHI16_PCREL, R_SH_IMM_MEDHI16_PCREL },
382 { BFD_RELOC_SH_IMM_HI16, R_SH_IMM_HI16 },
383 { BFD_RELOC_SH_IMM_HI16_PCREL, R_SH_IMM_HI16_PCREL },
384 { BFD_RELOC_64, R_SH_64 },
385 { BFD_RELOC_64_PCREL, R_SH_64_PCREL },
386 #endif /* not INCLUDE_SHMEDIA */
387 };
388
389 /* Given a BFD reloc code, return the howto structure for the
390 corresponding SH ELF reloc. */
391
392 static reloc_howto_type *
393 sh_elf_reloc_type_lookup (bfd *abfd, bfd_reloc_code_real_type code)
394 {
395 unsigned int i;
396
397 for (i = 0; i < sizeof (sh_reloc_map) / sizeof (struct elf_reloc_map); i++)
398 {
399 if (sh_reloc_map[i].bfd_reloc_val == code)
400 return get_howto_table (abfd) + (int) sh_reloc_map[i].elf_reloc_val;
401 }
402
403 return NULL;
404 }
405
406 static reloc_howto_type *
407 sh_elf_reloc_name_lookup (bfd *abfd, const char *r_name)
408 {
409 unsigned int i;
410
411 if (vxworks_object_p (abfd))
412 {
413 for (i = 0;
414 i < (sizeof (sh_vxworks_howto_table)
415 / sizeof (sh_vxworks_howto_table[0]));
416 i++)
417 if (sh_vxworks_howto_table[i].name != NULL
418 && strcasecmp (sh_vxworks_howto_table[i].name, r_name) == 0)
419 return &sh_vxworks_howto_table[i];
420 }
421 else
422 {
423 for (i = 0;
424 i < (sizeof (sh_elf_howto_table)
425 / sizeof (sh_elf_howto_table[0]));
426 i++)
427 if (sh_elf_howto_table[i].name != NULL
428 && strcasecmp (sh_elf_howto_table[i].name, r_name) == 0)
429 return &sh_elf_howto_table[i];
430 }
431
432 return NULL;
433 }
434
435 /* Given an ELF reloc, fill in the howto field of a relent. */
436
437 static void
438 sh_elf_info_to_howto (bfd *abfd, arelent *cache_ptr, Elf_Internal_Rela *dst)
439 {
440 unsigned int r;
441
442 r = ELF32_R_TYPE (dst->r_info);
443
444 BFD_ASSERT (r < (unsigned int) R_SH_max);
445 BFD_ASSERT (r < R_SH_FIRST_INVALID_RELOC || r > R_SH_LAST_INVALID_RELOC);
446 BFD_ASSERT (r < R_SH_FIRST_INVALID_RELOC_2 || r > R_SH_LAST_INVALID_RELOC_2);
447 BFD_ASSERT (r < R_SH_FIRST_INVALID_RELOC_3 || r > R_SH_LAST_INVALID_RELOC_3);
448 BFD_ASSERT (r < R_SH_FIRST_INVALID_RELOC_4 || r > R_SH_LAST_INVALID_RELOC_4);
449 BFD_ASSERT (r < R_SH_FIRST_INVALID_RELOC_5 || r > R_SH_LAST_INVALID_RELOC_5);
450
451 cache_ptr->howto = get_howto_table (abfd) + r;
452 }
453
454 /* This function handles relaxing for SH ELF. See the corresponding
455 function in coff-sh.c for a description of what this does. FIXME:
456 There is a lot of duplication here between this code and the COFF
457 specific code. The format of relocs and symbols is wound deeply
458 into this code, but it would still be better if the duplication
459 could be eliminated somehow. Note in particular that although both
460 functions use symbols like R_SH_CODE, those symbols have different
461 values; in coff-sh.c they come from include/coff/sh.h, whereas here
462 they come from enum elf_sh_reloc_type in include/elf/sh.h. */
463
464 static bfd_boolean
465 sh_elf_relax_section (bfd *abfd, asection *sec,
466 struct bfd_link_info *link_info, bfd_boolean *again)
467 {
468 Elf_Internal_Shdr *symtab_hdr;
469 Elf_Internal_Rela *internal_relocs;
470 bfd_boolean have_code;
471 Elf_Internal_Rela *irel, *irelend;
472 bfd_byte *contents = NULL;
473 Elf_Internal_Sym *isymbuf = NULL;
474
475 *again = FALSE;
476
477 if (link_info->relocatable
478 || (sec->flags & SEC_RELOC) == 0
479 || sec->reloc_count == 0)
480 return TRUE;
481
482 #ifdef INCLUDE_SHMEDIA
483 if (elf_section_data (sec)->this_hdr.sh_flags
484 & (SHF_SH5_ISA32 | SHF_SH5_ISA32_MIXED))
485 {
486 return TRUE;
487 }
488 #endif
489
490 symtab_hdr = &elf_symtab_hdr (abfd);
491
492 internal_relocs = (_bfd_elf_link_read_relocs
493 (abfd, sec, NULL, (Elf_Internal_Rela *) NULL,
494 link_info->keep_memory));
495 if (internal_relocs == NULL)
496 goto error_return;
497
498 have_code = FALSE;
499
500 irelend = internal_relocs + sec->reloc_count;
501 for (irel = internal_relocs; irel < irelend; irel++)
502 {
503 bfd_vma laddr, paddr, symval;
504 unsigned short insn;
505 Elf_Internal_Rela *irelfn, *irelscan, *irelcount;
506 bfd_signed_vma foff;
507
508 if (ELF32_R_TYPE (irel->r_info) == (int) R_SH_CODE)
509 have_code = TRUE;
510
511 if (ELF32_R_TYPE (irel->r_info) != (int) R_SH_USES)
512 continue;
513
514 /* Get the section contents. */
515 if (contents == NULL)
516 {
517 if (elf_section_data (sec)->this_hdr.contents != NULL)
518 contents = elf_section_data (sec)->this_hdr.contents;
519 else
520 {
521 if (!bfd_malloc_and_get_section (abfd, sec, &contents))
522 goto error_return;
523 }
524 }
525
526 /* The r_addend field of the R_SH_USES reloc will point us to
527 the register load. The 4 is because the r_addend field is
528 computed as though it were a jump offset, which are based
529 from 4 bytes after the jump instruction. */
530 laddr = irel->r_offset + 4 + irel->r_addend;
531 if (laddr >= sec->size)
532 {
533 (*_bfd_error_handler) (_("%B: 0x%lx: warning: bad R_SH_USES offset"),
534 abfd,
535 (unsigned long) irel->r_offset);
536 continue;
537 }
538 insn = bfd_get_16 (abfd, contents + laddr);
539
540 /* If the instruction is not mov.l NN,rN, we don't know what to
541 do. */
542 if ((insn & 0xf000) != 0xd000)
543 {
544 ((*_bfd_error_handler)
545 (_("%B: 0x%lx: warning: R_SH_USES points to unrecognized insn 0x%x"),
546 abfd, (unsigned long) irel->r_offset, insn));
547 continue;
548 }
549
550 /* Get the address from which the register is being loaded. The
551 displacement in the mov.l instruction is quadrupled. It is a
552 displacement from four bytes after the movl instruction, but,
553 before adding in the PC address, two least significant bits
554 of the PC are cleared. We assume that the section is aligned
555 on a four byte boundary. */
556 paddr = insn & 0xff;
557 paddr *= 4;
558 paddr += (laddr + 4) &~ (bfd_vma) 3;
559 if (paddr >= sec->size)
560 {
561 ((*_bfd_error_handler)
562 (_("%B: 0x%lx: warning: bad R_SH_USES load offset"),
563 abfd, (unsigned long) irel->r_offset));
564 continue;
565 }
566
567 /* Get the reloc for the address from which the register is
568 being loaded. This reloc will tell us which function is
569 actually being called. */
570 for (irelfn = internal_relocs; irelfn < irelend; irelfn++)
571 if (irelfn->r_offset == paddr
572 && ELF32_R_TYPE (irelfn->r_info) == (int) R_SH_DIR32)
573 break;
574 if (irelfn >= irelend)
575 {
576 ((*_bfd_error_handler)
577 (_("%B: 0x%lx: warning: could not find expected reloc"),
578 abfd, (unsigned long) paddr));
579 continue;
580 }
581
582 /* Read this BFD's symbols if we haven't done so already. */
583 if (isymbuf == NULL && symtab_hdr->sh_info != 0)
584 {
585 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
586 if (isymbuf == NULL)
587 isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
588 symtab_hdr->sh_info, 0,
589 NULL, NULL, NULL);
590 if (isymbuf == NULL)
591 goto error_return;
592 }
593
594 /* Get the value of the symbol referred to by the reloc. */
595 if (ELF32_R_SYM (irelfn->r_info) < symtab_hdr->sh_info)
596 {
597 /* A local symbol. */
598 Elf_Internal_Sym *isym;
599
600 isym = isymbuf + ELF32_R_SYM (irelfn->r_info);
601 if (isym->st_shndx
602 != (unsigned int) _bfd_elf_section_from_bfd_section (abfd, sec))
603 {
604 ((*_bfd_error_handler)
605 (_("%B: 0x%lx: warning: symbol in unexpected section"),
606 abfd, (unsigned long) paddr));
607 continue;
608 }
609
610 symval = (isym->st_value
611 + sec->output_section->vma
612 + sec->output_offset);
613 }
614 else
615 {
616 unsigned long indx;
617 struct elf_link_hash_entry *h;
618
619 indx = ELF32_R_SYM (irelfn->r_info) - symtab_hdr->sh_info;
620 h = elf_sym_hashes (abfd)[indx];
621 BFD_ASSERT (h != NULL);
622 if (h->root.type != bfd_link_hash_defined
623 && h->root.type != bfd_link_hash_defweak)
624 {
625 /* This appears to be a reference to an undefined
626 symbol. Just ignore it--it will be caught by the
627 regular reloc processing. */
628 continue;
629 }
630
631 symval = (h->root.u.def.value
632 + h->root.u.def.section->output_section->vma
633 + h->root.u.def.section->output_offset);
634 }
635
636 if (get_howto_table (abfd)[R_SH_DIR32].partial_inplace)
637 symval += bfd_get_32 (abfd, contents + paddr);
638 else
639 symval += irelfn->r_addend;
640
641 /* See if this function call can be shortened. */
642 foff = (symval
643 - (irel->r_offset
644 + sec->output_section->vma
645 + sec->output_offset
646 + 4));
647 /* A branch to an address beyond ours might be increased by an
648 .align that doesn't move when bytes behind us are deleted.
649 So, we add some slop in this calculation to allow for
650 that. */
651 if (foff < -0x1000 || foff >= 0x1000 - 8)
652 {
653 /* After all that work, we can't shorten this function call. */
654 continue;
655 }
656
657 /* Shorten the function call. */
658
659 /* For simplicity of coding, we are going to modify the section
660 contents, the section relocs, and the BFD symbol table. We
661 must tell the rest of the code not to free up this
662 information. It would be possible to instead create a table
663 of changes which have to be made, as is done in coff-mips.c;
664 that would be more work, but would require less memory when
665 the linker is run. */
666
667 elf_section_data (sec)->relocs = internal_relocs;
668 elf_section_data (sec)->this_hdr.contents = contents;
669 symtab_hdr->contents = (unsigned char *) isymbuf;
670
671 /* Replace the jsr with a bsr. */
672
673 /* Change the R_SH_USES reloc into an R_SH_IND12W reloc, and
674 replace the jsr with a bsr. */
675 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irelfn->r_info), R_SH_IND12W);
676 /* We used to test (ELF32_R_SYM (irelfn->r_info) < symtab_hdr->sh_info)
677 here, but that only checks if the symbol is an external symbol,
678 not if the symbol is in a different section. Besides, we need
679 a consistent meaning for the relocation, so we just assume here that
680 the value of the symbol is not available. */
681
682 /* We can't fully resolve this yet, because the external
683 symbol value may be changed by future relaxing. We let
684 the final link phase handle it. */
685 bfd_put_16 (abfd, (bfd_vma) 0xb000, contents + irel->r_offset);
686
687 irel->r_addend = -4;
688
689 /* When we calculated the symbol "value" we had an offset in the
690 DIR32's word in memory (we read and add it above). However,
691 the jsr we create does NOT have this offset encoded, so we
692 have to add it to the addend to preserve it. */
693 irel->r_addend += bfd_get_32 (abfd, contents + paddr);
694
695 /* See if there is another R_SH_USES reloc referring to the same
696 register load. */
697 for (irelscan = internal_relocs; irelscan < irelend; irelscan++)
698 if (ELF32_R_TYPE (irelscan->r_info) == (int) R_SH_USES
699 && laddr == irelscan->r_offset + 4 + irelscan->r_addend)
700 break;
701 if (irelscan < irelend)
702 {
703 /* Some other function call depends upon this register load,
704 and we have not yet converted that function call.
705 Indeed, we may never be able to convert it. There is
706 nothing else we can do at this point. */
707 continue;
708 }
709
710 /* Look for a R_SH_COUNT reloc on the location where the
711 function address is stored. Do this before deleting any
712 bytes, to avoid confusion about the address. */
713 for (irelcount = internal_relocs; irelcount < irelend; irelcount++)
714 if (irelcount->r_offset == paddr
715 && ELF32_R_TYPE (irelcount->r_info) == (int) R_SH_COUNT)
716 break;
717
718 /* Delete the register load. */
719 if (! sh_elf_relax_delete_bytes (abfd, sec, laddr, 2))
720 goto error_return;
721
722 /* That will change things, so, just in case it permits some
723 other function call to come within range, we should relax
724 again. Note that this is not required, and it may be slow. */
725 *again = TRUE;
726
727 /* Now check whether we got a COUNT reloc. */
728 if (irelcount >= irelend)
729 {
730 ((*_bfd_error_handler)
731 (_("%B: 0x%lx: warning: could not find expected COUNT reloc"),
732 abfd, (unsigned long) paddr));
733 continue;
734 }
735
736 /* The number of uses is stored in the r_addend field. We've
737 just deleted one. */
738 if (irelcount->r_addend == 0)
739 {
740 ((*_bfd_error_handler) (_("%B: 0x%lx: warning: bad count"),
741 abfd,
742 (unsigned long) paddr));
743 continue;
744 }
745
746 --irelcount->r_addend;
747
748 /* If there are no more uses, we can delete the address. Reload
749 the address from irelfn, in case it was changed by the
750 previous call to sh_elf_relax_delete_bytes. */
751 if (irelcount->r_addend == 0)
752 {
753 if (! sh_elf_relax_delete_bytes (abfd, sec, irelfn->r_offset, 4))
754 goto error_return;
755 }
756
757 /* We've done all we can with that function call. */
758 }
759
760 /* Look for load and store instructions that we can align on four
761 byte boundaries. */
762 if ((elf_elfheader (abfd)->e_flags & EF_SH_MACH_MASK) != EF_SH4
763 && have_code)
764 {
765 bfd_boolean swapped;
766
767 /* Get the section contents. */
768 if (contents == NULL)
769 {
770 if (elf_section_data (sec)->this_hdr.contents != NULL)
771 contents = elf_section_data (sec)->this_hdr.contents;
772 else
773 {
774 if (!bfd_malloc_and_get_section (abfd, sec, &contents))
775 goto error_return;
776 }
777 }
778
779 if (! sh_elf_align_loads (abfd, sec, internal_relocs, contents,
780 &swapped))
781 goto error_return;
782
783 if (swapped)
784 {
785 elf_section_data (sec)->relocs = internal_relocs;
786 elf_section_data (sec)->this_hdr.contents = contents;
787 symtab_hdr->contents = (unsigned char *) isymbuf;
788 }
789 }
790
791 if (isymbuf != NULL
792 && symtab_hdr->contents != (unsigned char *) isymbuf)
793 {
794 if (! link_info->keep_memory)
795 free (isymbuf);
796 else
797 {
798 /* Cache the symbols for elf_link_input_bfd. */
799 symtab_hdr->contents = (unsigned char *) isymbuf;
800 }
801 }
802
803 if (contents != NULL
804 && elf_section_data (sec)->this_hdr.contents != contents)
805 {
806 if (! link_info->keep_memory)
807 free (contents);
808 else
809 {
810 /* Cache the section contents for elf_link_input_bfd. */
811 elf_section_data (sec)->this_hdr.contents = contents;
812 }
813 }
814
815 if (internal_relocs != NULL
816 && elf_section_data (sec)->relocs != internal_relocs)
817 free (internal_relocs);
818
819 return TRUE;
820
821 error_return:
822 if (isymbuf != NULL
823 && symtab_hdr->contents != (unsigned char *) isymbuf)
824 free (isymbuf);
825 if (contents != NULL
826 && elf_section_data (sec)->this_hdr.contents != contents)
827 free (contents);
828 if (internal_relocs != NULL
829 && elf_section_data (sec)->relocs != internal_relocs)
830 free (internal_relocs);
831
832 return FALSE;
833 }
834
835 /* Delete some bytes from a section while relaxing. FIXME: There is a
836 lot of duplication between this function and sh_relax_delete_bytes
837 in coff-sh.c. */
838
839 static bfd_boolean
840 sh_elf_relax_delete_bytes (bfd *abfd, asection *sec, bfd_vma addr,
841 int count)
842 {
843 Elf_Internal_Shdr *symtab_hdr;
844 unsigned int sec_shndx;
845 bfd_byte *contents;
846 Elf_Internal_Rela *irel, *irelend;
847 Elf_Internal_Rela *irelalign;
848 bfd_vma toaddr;
849 Elf_Internal_Sym *isymbuf, *isym, *isymend;
850 struct elf_link_hash_entry **sym_hashes;
851 struct elf_link_hash_entry **end_hashes;
852 unsigned int symcount;
853 asection *o;
854
855 symtab_hdr = &elf_symtab_hdr (abfd);
856 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
857
858 sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
859
860 contents = elf_section_data (sec)->this_hdr.contents;
861
862 /* The deletion must stop at the next ALIGN reloc for an aligment
863 power larger than the number of bytes we are deleting. */
864
865 irelalign = NULL;
866 toaddr = sec->size;
867
868 irel = elf_section_data (sec)->relocs;
869 irelend = irel + sec->reloc_count;
870 for (; irel < irelend; irel++)
871 {
872 if (ELF32_R_TYPE (irel->r_info) == (int) R_SH_ALIGN
873 && irel->r_offset > addr
874 && count < (1 << irel->r_addend))
875 {
876 irelalign = irel;
877 toaddr = irel->r_offset;
878 break;
879 }
880 }
881
882 /* Actually delete the bytes. */
883 memmove (contents + addr, contents + addr + count,
884 (size_t) (toaddr - addr - count));
885 if (irelalign == NULL)
886 sec->size -= count;
887 else
888 {
889 int i;
890
891 #define NOP_OPCODE (0x0009)
892
893 BFD_ASSERT ((count & 1) == 0);
894 for (i = 0; i < count; i += 2)
895 bfd_put_16 (abfd, (bfd_vma) NOP_OPCODE, contents + toaddr - count + i);
896 }
897
898 /* Adjust all the relocs. */
899 for (irel = elf_section_data (sec)->relocs; irel < irelend; irel++)
900 {
901 bfd_vma nraddr, stop;
902 bfd_vma start = 0;
903 int insn = 0;
904 int off, adjust, oinsn;
905 bfd_signed_vma voff = 0;
906 bfd_boolean overflow;
907
908 /* Get the new reloc address. */
909 nraddr = irel->r_offset;
910 if ((irel->r_offset > addr
911 && irel->r_offset < toaddr)
912 || (ELF32_R_TYPE (irel->r_info) == (int) R_SH_ALIGN
913 && irel->r_offset == toaddr))
914 nraddr -= count;
915
916 /* See if this reloc was for the bytes we have deleted, in which
917 case we no longer care about it. Don't delete relocs which
918 represent addresses, though. */
919 if (irel->r_offset >= addr
920 && irel->r_offset < addr + count
921 && ELF32_R_TYPE (irel->r_info) != (int) R_SH_ALIGN
922 && ELF32_R_TYPE (irel->r_info) != (int) R_SH_CODE
923 && ELF32_R_TYPE (irel->r_info) != (int) R_SH_DATA
924 && ELF32_R_TYPE (irel->r_info) != (int) R_SH_LABEL)
925 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
926 (int) R_SH_NONE);
927
928 /* If this is a PC relative reloc, see if the range it covers
929 includes the bytes we have deleted. */
930 switch ((enum elf_sh_reloc_type) ELF32_R_TYPE (irel->r_info))
931 {
932 default:
933 break;
934
935 case R_SH_DIR8WPN:
936 case R_SH_IND12W:
937 case R_SH_DIR8WPZ:
938 case R_SH_DIR8WPL:
939 start = irel->r_offset;
940 insn = bfd_get_16 (abfd, contents + nraddr);
941 break;
942 }
943
944 switch ((enum elf_sh_reloc_type) ELF32_R_TYPE (irel->r_info))
945 {
946 default:
947 start = stop = addr;
948 break;
949
950 case R_SH_DIR32:
951 /* If this reloc is against a symbol defined in this
952 section, and the symbol will not be adjusted below, we
953 must check the addend to see it will put the value in
954 range to be adjusted, and hence must be changed. */
955 if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
956 {
957 isym = isymbuf + ELF32_R_SYM (irel->r_info);
958 if (isym->st_shndx == sec_shndx
959 && (isym->st_value <= addr
960 || isym->st_value >= toaddr))
961 {
962 bfd_vma val;
963
964 if (get_howto_table (abfd)[R_SH_DIR32].partial_inplace)
965 {
966 val = bfd_get_32 (abfd, contents + nraddr);
967 val += isym->st_value;
968 if (val > addr && val < toaddr)
969 bfd_put_32 (abfd, val - count, contents + nraddr);
970 }
971 else
972 {
973 val = isym->st_value + irel->r_addend;
974 if (val > addr && val < toaddr)
975 irel->r_addend -= count;
976 }
977 }
978 }
979 start = stop = addr;
980 break;
981
982 case R_SH_DIR8WPN:
983 off = insn & 0xff;
984 if (off & 0x80)
985 off -= 0x100;
986 stop = (bfd_vma) ((bfd_signed_vma) start + 4 + off * 2);
987 break;
988
989 case R_SH_IND12W:
990 off = insn & 0xfff;
991 if (! off)
992 {
993 /* This has been made by previous relaxation. Since the
994 relocation will be against an external symbol, the
995 final relocation will just do the right thing. */
996 start = stop = addr;
997 }
998 else
999 {
1000 if (off & 0x800)
1001 off -= 0x1000;
1002 stop = (bfd_vma) ((bfd_signed_vma) start + 4 + off * 2);
1003
1004 /* The addend will be against the section symbol, thus
1005 for adjusting the addend, the relevant start is the
1006 start of the section.
1007 N.B. If we want to abandon in-place changes here and
1008 test directly using symbol + addend, we have to take into
1009 account that the addend has already been adjusted by -4. */
1010 if (stop > addr && stop < toaddr)
1011 irel->r_addend -= count;
1012 }
1013 break;
1014
1015 case R_SH_DIR8WPZ:
1016 off = insn & 0xff;
1017 stop = start + 4 + off * 2;
1018 break;
1019
1020 case R_SH_DIR8WPL:
1021 off = insn & 0xff;
1022 stop = (start & ~(bfd_vma) 3) + 4 + off * 4;
1023 break;
1024
1025 case R_SH_SWITCH8:
1026 case R_SH_SWITCH16:
1027 case R_SH_SWITCH32:
1028 /* These relocs types represent
1029 .word L2-L1
1030 The r_addend field holds the difference between the reloc
1031 address and L1. That is the start of the reloc, and
1032 adding in the contents gives us the top. We must adjust
1033 both the r_offset field and the section contents.
1034 N.B. in gas / coff bfd, the elf bfd r_addend is called r_offset,
1035 and the elf bfd r_offset is called r_vaddr. */
1036
1037 stop = irel->r_offset;
1038 start = (bfd_vma) ((bfd_signed_vma) stop - (long) irel->r_addend);
1039
1040 if (start > addr
1041 && start < toaddr
1042 && (stop <= addr || stop >= toaddr))
1043 irel->r_addend += count;
1044 else if (stop > addr
1045 && stop < toaddr
1046 && (start <= addr || start >= toaddr))
1047 irel->r_addend -= count;
1048
1049 if (ELF32_R_TYPE (irel->r_info) == (int) R_SH_SWITCH16)
1050 voff = bfd_get_signed_16 (abfd, contents + nraddr);
1051 else if (ELF32_R_TYPE (irel->r_info) == (int) R_SH_SWITCH8)
1052 voff = bfd_get_8 (abfd, contents + nraddr);
1053 else
1054 voff = bfd_get_signed_32 (abfd, contents + nraddr);
1055 stop = (bfd_vma) ((bfd_signed_vma) start + voff);
1056
1057 break;
1058
1059 case R_SH_USES:
1060 start = irel->r_offset;
1061 stop = (bfd_vma) ((bfd_signed_vma) start
1062 + (long) irel->r_addend
1063 + 4);
1064 break;
1065 }
1066
1067 if (start > addr
1068 && start < toaddr
1069 && (stop <= addr || stop >= toaddr))
1070 adjust = count;
1071 else if (stop > addr
1072 && stop < toaddr
1073 && (start <= addr || start >= toaddr))
1074 adjust = - count;
1075 else
1076 adjust = 0;
1077
1078 if (adjust != 0)
1079 {
1080 oinsn = insn;
1081 overflow = FALSE;
1082 switch ((enum elf_sh_reloc_type) ELF32_R_TYPE (irel->r_info))
1083 {
1084 default:
1085 abort ();
1086 break;
1087
1088 case R_SH_DIR8WPN:
1089 case R_SH_DIR8WPZ:
1090 insn += adjust / 2;
1091 if ((oinsn & 0xff00) != (insn & 0xff00))
1092 overflow = TRUE;
1093 bfd_put_16 (abfd, (bfd_vma) insn, contents + nraddr);
1094 break;
1095
1096 case R_SH_IND12W:
1097 insn += adjust / 2;
1098 if ((oinsn & 0xf000) != (insn & 0xf000))
1099 overflow = TRUE;
1100 bfd_put_16 (abfd, (bfd_vma) insn, contents + nraddr);
1101 break;
1102
1103 case R_SH_DIR8WPL:
1104 BFD_ASSERT (adjust == count || count >= 4);
1105 if (count >= 4)
1106 insn += adjust / 4;
1107 else
1108 {
1109 if ((irel->r_offset & 3) == 0)
1110 ++insn;
1111 }
1112 if ((oinsn & 0xff00) != (insn & 0xff00))
1113 overflow = TRUE;
1114 bfd_put_16 (abfd, (bfd_vma) insn, contents + nraddr);
1115 break;
1116
1117 case R_SH_SWITCH8:
1118 voff += adjust;
1119 if (voff < 0 || voff >= 0xff)
1120 overflow = TRUE;
1121 bfd_put_8 (abfd, voff, contents + nraddr);
1122 break;
1123
1124 case R_SH_SWITCH16:
1125 voff += adjust;
1126 if (voff < - 0x8000 || voff >= 0x8000)
1127 overflow = TRUE;
1128 bfd_put_signed_16 (abfd, (bfd_vma) voff, contents + nraddr);
1129 break;
1130
1131 case R_SH_SWITCH32:
1132 voff += adjust;
1133 bfd_put_signed_32 (abfd, (bfd_vma) voff, contents + nraddr);
1134 break;
1135
1136 case R_SH_USES:
1137 irel->r_addend += adjust;
1138 break;
1139 }
1140
1141 if (overflow)
1142 {
1143 ((*_bfd_error_handler)
1144 (_("%B: 0x%lx: fatal: reloc overflow while relaxing"),
1145 abfd, (unsigned long) irel->r_offset));
1146 bfd_set_error (bfd_error_bad_value);
1147 return FALSE;
1148 }
1149 }
1150
1151 irel->r_offset = nraddr;
1152 }
1153
1154 /* Look through all the other sections. If there contain any IMM32
1155 relocs against internal symbols which we are not going to adjust
1156 below, we may need to adjust the addends. */
1157 for (o = abfd->sections; o != NULL; o = o->next)
1158 {
1159 Elf_Internal_Rela *internal_relocs;
1160 Elf_Internal_Rela *irelscan, *irelscanend;
1161 bfd_byte *ocontents;
1162
1163 if (o == sec
1164 || (o->flags & SEC_RELOC) == 0
1165 || o->reloc_count == 0)
1166 continue;
1167
1168 /* We always cache the relocs. Perhaps, if info->keep_memory is
1169 FALSE, we should free them, if we are permitted to, when we
1170 leave sh_coff_relax_section. */
1171 internal_relocs = (_bfd_elf_link_read_relocs
1172 (abfd, o, NULL, (Elf_Internal_Rela *) NULL, TRUE));
1173 if (internal_relocs == NULL)
1174 return FALSE;
1175
1176 ocontents = NULL;
1177 irelscanend = internal_relocs + o->reloc_count;
1178 for (irelscan = internal_relocs; irelscan < irelscanend; irelscan++)
1179 {
1180 /* Dwarf line numbers use R_SH_SWITCH32 relocs. */
1181 if (ELF32_R_TYPE (irelscan->r_info) == (int) R_SH_SWITCH32)
1182 {
1183 bfd_vma start, stop;
1184 bfd_signed_vma voff;
1185
1186 if (ocontents == NULL)
1187 {
1188 if (elf_section_data (o)->this_hdr.contents != NULL)
1189 ocontents = elf_section_data (o)->this_hdr.contents;
1190 else
1191 {
1192 /* We always cache the section contents.
1193 Perhaps, if info->keep_memory is FALSE, we
1194 should free them, if we are permitted to,
1195 when we leave sh_coff_relax_section. */
1196 if (!bfd_malloc_and_get_section (abfd, o, &ocontents))
1197 {
1198 if (ocontents != NULL)
1199 free (ocontents);
1200 return FALSE;
1201 }
1202
1203 elf_section_data (o)->this_hdr.contents = ocontents;
1204 }
1205 }
1206
1207 stop = irelscan->r_offset;
1208 start
1209 = (bfd_vma) ((bfd_signed_vma) stop - (long) irelscan->r_addend);
1210
1211 /* STOP is in a different section, so it won't change. */
1212 if (start > addr && start < toaddr)
1213 irelscan->r_addend += count;
1214
1215 voff = bfd_get_signed_32 (abfd, ocontents + irelscan->r_offset);
1216 stop = (bfd_vma) ((bfd_signed_vma) start + voff);
1217
1218 if (start > addr
1219 && start < toaddr
1220 && (stop <= addr || stop >= toaddr))
1221 bfd_put_signed_32 (abfd, (bfd_vma) voff + count,
1222 ocontents + irelscan->r_offset);
1223 else if (stop > addr
1224 && stop < toaddr
1225 && (start <= addr || start >= toaddr))
1226 bfd_put_signed_32 (abfd, (bfd_vma) voff - count,
1227 ocontents + irelscan->r_offset);
1228 }
1229
1230 if (ELF32_R_TYPE (irelscan->r_info) != (int) R_SH_DIR32)
1231 continue;
1232
1233 if (ELF32_R_SYM (irelscan->r_info) >= symtab_hdr->sh_info)
1234 continue;
1235
1236
1237 isym = isymbuf + ELF32_R_SYM (irelscan->r_info);
1238 if (isym->st_shndx == sec_shndx
1239 && (isym->st_value <= addr
1240 || isym->st_value >= toaddr))
1241 {
1242 bfd_vma val;
1243
1244 if (ocontents == NULL)
1245 {
1246 if (elf_section_data (o)->this_hdr.contents != NULL)
1247 ocontents = elf_section_data (o)->this_hdr.contents;
1248 else
1249 {
1250 /* We always cache the section contents.
1251 Perhaps, if info->keep_memory is FALSE, we
1252 should free them, if we are permitted to,
1253 when we leave sh_coff_relax_section. */
1254 if (!bfd_malloc_and_get_section (abfd, o, &ocontents))
1255 {
1256 if (ocontents != NULL)
1257 free (ocontents);
1258 return FALSE;
1259 }
1260
1261 elf_section_data (o)->this_hdr.contents = ocontents;
1262 }
1263 }
1264
1265 val = bfd_get_32 (abfd, ocontents + irelscan->r_offset);
1266 val += isym->st_value;
1267 if (val > addr && val < toaddr)
1268 bfd_put_32 (abfd, val - count,
1269 ocontents + irelscan->r_offset);
1270 }
1271 }
1272 }
1273
1274 /* Adjust the local symbols defined in this section. */
1275 isymend = isymbuf + symtab_hdr->sh_info;
1276 for (isym = isymbuf; isym < isymend; isym++)
1277 {
1278 if (isym->st_shndx == sec_shndx
1279 && isym->st_value > addr
1280 && isym->st_value < toaddr)
1281 isym->st_value -= count;
1282 }
1283
1284 /* Now adjust the global symbols defined in this section. */
1285 symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
1286 - symtab_hdr->sh_info);
1287 sym_hashes = elf_sym_hashes (abfd);
1288 end_hashes = sym_hashes + symcount;
1289 for (; sym_hashes < end_hashes; sym_hashes++)
1290 {
1291 struct elf_link_hash_entry *sym_hash = *sym_hashes;
1292 if ((sym_hash->root.type == bfd_link_hash_defined
1293 || sym_hash->root.type == bfd_link_hash_defweak)
1294 && sym_hash->root.u.def.section == sec
1295 && sym_hash->root.u.def.value > addr
1296 && sym_hash->root.u.def.value < toaddr)
1297 {
1298 sym_hash->root.u.def.value -= count;
1299 }
1300 }
1301
1302 /* See if we can move the ALIGN reloc forward. We have adjusted
1303 r_offset for it already. */
1304 if (irelalign != NULL)
1305 {
1306 bfd_vma alignto, alignaddr;
1307
1308 alignto = BFD_ALIGN (toaddr, 1 << irelalign->r_addend);
1309 alignaddr = BFD_ALIGN (irelalign->r_offset,
1310 1 << irelalign->r_addend);
1311 if (alignto != alignaddr)
1312 {
1313 /* Tail recursion. */
1314 return sh_elf_relax_delete_bytes (abfd, sec, alignaddr,
1315 (int) (alignto - alignaddr));
1316 }
1317 }
1318
1319 return TRUE;
1320 }
1321
1322 /* Look for loads and stores which we can align to four byte
1323 boundaries. This is like sh_align_loads in coff-sh.c. */
1324
1325 static bfd_boolean
1326 sh_elf_align_loads (bfd *abfd ATTRIBUTE_UNUSED, asection *sec,
1327 Elf_Internal_Rela *internal_relocs,
1328 bfd_byte *contents ATTRIBUTE_UNUSED,
1329 bfd_boolean *pswapped)
1330 {
1331 Elf_Internal_Rela *irel, *irelend;
1332 bfd_vma *labels = NULL;
1333 bfd_vma *label, *label_end;
1334 bfd_size_type amt;
1335
1336 *pswapped = FALSE;
1337
1338 irelend = internal_relocs + sec->reloc_count;
1339
1340 /* Get all the addresses with labels on them. */
1341 amt = sec->reloc_count;
1342 amt *= sizeof (bfd_vma);
1343 labels = (bfd_vma *) bfd_malloc (amt);
1344 if (labels == NULL)
1345 goto error_return;
1346 label_end = labels;
1347 for (irel = internal_relocs; irel < irelend; irel++)
1348 {
1349 if (ELF32_R_TYPE (irel->r_info) == (int) R_SH_LABEL)
1350 {
1351 *label_end = irel->r_offset;
1352 ++label_end;
1353 }
1354 }
1355
1356 /* Note that the assembler currently always outputs relocs in
1357 address order. If that ever changes, this code will need to sort
1358 the label values and the relocs. */
1359
1360 label = labels;
1361
1362 for (irel = internal_relocs; irel < irelend; irel++)
1363 {
1364 bfd_vma start, stop;
1365
1366 if (ELF32_R_TYPE (irel->r_info) != (int) R_SH_CODE)
1367 continue;
1368
1369 start = irel->r_offset;
1370
1371 for (irel++; irel < irelend; irel++)
1372 if (ELF32_R_TYPE (irel->r_info) == (int) R_SH_DATA)
1373 break;
1374 if (irel < irelend)
1375 stop = irel->r_offset;
1376 else
1377 stop = sec->size;
1378
1379 if (! _bfd_sh_align_load_span (abfd, sec, contents, sh_elf_swap_insns,
1380 internal_relocs, &label,
1381 label_end, start, stop, pswapped))
1382 goto error_return;
1383 }
1384
1385 free (labels);
1386
1387 return TRUE;
1388
1389 error_return:
1390 if (labels != NULL)
1391 free (labels);
1392 return FALSE;
1393 }
1394
1395 #ifndef SH64_ELF
1396 /* Swap two SH instructions. This is like sh_swap_insns in coff-sh.c. */
1397
1398 static bfd_boolean
1399 sh_elf_swap_insns (bfd *abfd, asection *sec, void *relocs,
1400 bfd_byte *contents, bfd_vma addr)
1401 {
1402 Elf_Internal_Rela *internal_relocs = (Elf_Internal_Rela *) relocs;
1403 unsigned short i1, i2;
1404 Elf_Internal_Rela *irel, *irelend;
1405
1406 /* Swap the instructions themselves. */
1407 i1 = bfd_get_16 (abfd, contents + addr);
1408 i2 = bfd_get_16 (abfd, contents + addr + 2);
1409 bfd_put_16 (abfd, (bfd_vma) i2, contents + addr);
1410 bfd_put_16 (abfd, (bfd_vma) i1, contents + addr + 2);
1411
1412 /* Adjust all reloc addresses. */
1413 irelend = internal_relocs + sec->reloc_count;
1414 for (irel = internal_relocs; irel < irelend; irel++)
1415 {
1416 enum elf_sh_reloc_type type;
1417 int add;
1418
1419 /* There are a few special types of relocs that we don't want to
1420 adjust. These relocs do not apply to the instruction itself,
1421 but are only associated with the address. */
1422 type = (enum elf_sh_reloc_type) ELF32_R_TYPE (irel->r_info);
1423 if (type == R_SH_ALIGN
1424 || type == R_SH_CODE
1425 || type == R_SH_DATA
1426 || type == R_SH_LABEL)
1427 continue;
1428
1429 /* If an R_SH_USES reloc points to one of the addresses being
1430 swapped, we must adjust it. It would be incorrect to do this
1431 for a jump, though, since we want to execute both
1432 instructions after the jump. (We have avoided swapping
1433 around a label, so the jump will not wind up executing an
1434 instruction it shouldn't). */
1435 if (type == R_SH_USES)
1436 {
1437 bfd_vma off;
1438
1439 off = irel->r_offset + 4 + irel->r_addend;
1440 if (off == addr)
1441 irel->r_offset += 2;
1442 else if (off == addr + 2)
1443 irel->r_offset -= 2;
1444 }
1445
1446 if (irel->r_offset == addr)
1447 {
1448 irel->r_offset += 2;
1449 add = -2;
1450 }
1451 else if (irel->r_offset == addr + 2)
1452 {
1453 irel->r_offset -= 2;
1454 add = 2;
1455 }
1456 else
1457 add = 0;
1458
1459 if (add != 0)
1460 {
1461 bfd_byte *loc;
1462 unsigned short insn, oinsn;
1463 bfd_boolean overflow;
1464
1465 loc = contents + irel->r_offset;
1466 overflow = FALSE;
1467 switch (type)
1468 {
1469 default:
1470 break;
1471
1472 case R_SH_DIR8WPN:
1473 case R_SH_DIR8WPZ:
1474 insn = bfd_get_16 (abfd, loc);
1475 oinsn = insn;
1476 insn += add / 2;
1477 if ((oinsn & 0xff00) != (insn & 0xff00))
1478 overflow = TRUE;
1479 bfd_put_16 (abfd, (bfd_vma) insn, loc);
1480 break;
1481
1482 case R_SH_IND12W:
1483 insn = bfd_get_16 (abfd, loc);
1484 oinsn = insn;
1485 insn += add / 2;
1486 if ((oinsn & 0xf000) != (insn & 0xf000))
1487 overflow = TRUE;
1488 bfd_put_16 (abfd, (bfd_vma) insn, loc);
1489 break;
1490
1491 case R_SH_DIR8WPL:
1492 /* This reloc ignores the least significant 3 bits of
1493 the program counter before adding in the offset.
1494 This means that if ADDR is at an even address, the
1495 swap will not affect the offset. If ADDR is an at an
1496 odd address, then the instruction will be crossing a
1497 four byte boundary, and must be adjusted. */
1498 if ((addr & 3) != 0)
1499 {
1500 insn = bfd_get_16 (abfd, loc);
1501 oinsn = insn;
1502 insn += add / 2;
1503 if ((oinsn & 0xff00) != (insn & 0xff00))
1504 overflow = TRUE;
1505 bfd_put_16 (abfd, (bfd_vma) insn, loc);
1506 }
1507
1508 break;
1509 }
1510
1511 if (overflow)
1512 {
1513 ((*_bfd_error_handler)
1514 (_("%B: 0x%lx: fatal: reloc overflow while relaxing"),
1515 abfd, (unsigned long) irel->r_offset));
1516 bfd_set_error (bfd_error_bad_value);
1517 return FALSE;
1518 }
1519 }
1520 }
1521
1522 return TRUE;
1523 }
1524 #endif /* defined SH64_ELF */
1525
1526 /* Describes one of the various PLT styles. */
1527
1528 struct elf_sh_plt_info
1529 {
1530 /* The template for the first PLT entry, or NULL if there is no special
1531 first entry. */
1532 const bfd_byte *plt0_entry;
1533
1534 /* The size of PLT0_ENTRY in bytes, or 0 if PLT0_ENTRY is NULL. */
1535 bfd_vma plt0_entry_size;
1536
1537 /* Index I is the offset into PLT0_ENTRY of a pointer to
1538 _GLOBAL_OFFSET_TABLE_ + I * 4. The value is MINUS_ONE
1539 if there is no such pointer. */
1540 bfd_vma plt0_got_fields[3];
1541
1542 /* The template for a symbol's PLT entry. */
1543 const bfd_byte *symbol_entry;
1544
1545 /* The size of SYMBOL_ENTRY in bytes. */
1546 bfd_vma symbol_entry_size;
1547
1548 /* Byte offsets of fields in SYMBOL_ENTRY. Not all fields are used
1549 on all targets. The comments by each member indicate the value
1550 that the field must hold. */
1551 struct {
1552 bfd_vma got_entry; /* the address of the symbol's .got.plt entry */
1553 bfd_vma plt; /* .plt (or a branch to .plt on VxWorks) */
1554 bfd_vma reloc_offset; /* the offset of the symbol's JMP_SLOT reloc */
1555 } symbol_fields;
1556
1557 /* The offset of the resolver stub from the start of SYMBOL_ENTRY. */
1558 bfd_vma symbol_resolve_offset;
1559 };
1560
1561 #ifdef INCLUDE_SHMEDIA
1562
1563 /* The size in bytes of an entry in the procedure linkage table. */
1564
1565 #define ELF_PLT_ENTRY_SIZE 64
1566
1567 /* First entry in an absolute procedure linkage table look like this. */
1568
1569 static const bfd_byte elf_sh_plt0_entry_be[ELF_PLT_ENTRY_SIZE] =
1570 {
1571 0xcc, 0x00, 0x01, 0x10, /* movi .got.plt >> 16, r17 */
1572 0xc8, 0x00, 0x01, 0x10, /* shori .got.plt & 65535, r17 */
1573 0x89, 0x10, 0x09, 0x90, /* ld.l r17, 8, r25 */
1574 0x6b, 0xf1, 0x66, 0x00, /* ptabs r25, tr0 */
1575 0x89, 0x10, 0x05, 0x10, /* ld.l r17, 4, r17 */
1576 0x44, 0x01, 0xff, 0xf0, /* blink tr0, r63 */
1577 0x6f, 0xf0, 0xff, 0xf0, /* nop */
1578 0x6f, 0xf0, 0xff, 0xf0, /* nop */
1579 0x6f, 0xf0, 0xff, 0xf0, /* nop */
1580 0x6f, 0xf0, 0xff, 0xf0, /* nop */
1581 0x6f, 0xf0, 0xff, 0xf0, /* nop */
1582 0x6f, 0xf0, 0xff, 0xf0, /* nop */
1583 0x6f, 0xf0, 0xff, 0xf0, /* nop */
1584 0x6f, 0xf0, 0xff, 0xf0, /* nop */
1585 0x6f, 0xf0, 0xff, 0xf0, /* nop */
1586 0x6f, 0xf0, 0xff, 0xf0, /* nop */
1587 };
1588
1589 static const bfd_byte elf_sh_plt0_entry_le[ELF_PLT_ENTRY_SIZE] =
1590 {
1591 0x10, 0x01, 0x00, 0xcc, /* movi .got.plt >> 16, r17 */
1592 0x10, 0x01, 0x00, 0xc8, /* shori .got.plt & 65535, r17 */
1593 0x90, 0x09, 0x10, 0x89, /* ld.l r17, 8, r25 */
1594 0x00, 0x66, 0xf1, 0x6b, /* ptabs r25, tr0 */
1595 0x10, 0x05, 0x10, 0x89, /* ld.l r17, 4, r17 */
1596 0xf0, 0xff, 0x01, 0x44, /* blink tr0, r63 */
1597 0xf0, 0xff, 0xf0, 0x6f, /* nop */
1598 0xf0, 0xff, 0xf0, 0x6f, /* nop */
1599 0xf0, 0xff, 0xf0, 0x6f, /* nop */
1600 0xf0, 0xff, 0xf0, 0x6f, /* nop */
1601 0xf0, 0xff, 0xf0, 0x6f, /* nop */
1602 0xf0, 0xff, 0xf0, 0x6f, /* nop */
1603 0xf0, 0xff, 0xf0, 0x6f, /* nop */
1604 0xf0, 0xff, 0xf0, 0x6f, /* nop */
1605 0xf0, 0xff, 0xf0, 0x6f, /* nop */
1606 0xf0, 0xff, 0xf0, 0x6f, /* nop */
1607 };
1608
1609 /* Sebsequent entries in an absolute procedure linkage table look like
1610 this. */
1611
1612 static const bfd_byte elf_sh_plt_entry_be[ELF_PLT_ENTRY_SIZE] =
1613 {
1614 0xcc, 0x00, 0x01, 0x90, /* movi nameN-in-GOT >> 16, r25 */
1615 0xc8, 0x00, 0x01, 0x90, /* shori nameN-in-GOT & 65535, r25 */
1616 0x89, 0x90, 0x01, 0x90, /* ld.l r25, 0, r25 */
1617 0x6b, 0xf1, 0x66, 0x00, /* ptabs r25, tr0 */
1618 0x44, 0x01, 0xff, 0xf0, /* blink tr0, r63 */
1619 0x6f, 0xf0, 0xff, 0xf0, /* nop */
1620 0x6f, 0xf0, 0xff, 0xf0, /* nop */
1621 0x6f, 0xf0, 0xff, 0xf0, /* nop */
1622 0xcc, 0x00, 0x01, 0x90, /* movi .PLT0 >> 16, r25 */
1623 0xc8, 0x00, 0x01, 0x90, /* shori .PLT0 & 65535, r25 */
1624 0x6b, 0xf1, 0x66, 0x00, /* ptabs r25, tr0 */
1625 0xcc, 0x00, 0x01, 0x50, /* movi reloc-offset >> 16, r21 */
1626 0xc8, 0x00, 0x01, 0x50, /* shori reloc-offset & 65535, r21 */
1627 0x44, 0x01, 0xff, 0xf0, /* blink tr0, r63 */
1628 0x6f, 0xf0, 0xff, 0xf0, /* nop */
1629 0x6f, 0xf0, 0xff, 0xf0, /* nop */
1630 };
1631
1632 static const bfd_byte elf_sh_plt_entry_le[ELF_PLT_ENTRY_SIZE] =
1633 {
1634 0x90, 0x01, 0x00, 0xcc, /* movi nameN-in-GOT >> 16, r25 */
1635 0x90, 0x01, 0x00, 0xc8, /* shori nameN-in-GOT & 65535, r25 */
1636 0x90, 0x01, 0x90, 0x89, /* ld.l r25, 0, r25 */
1637 0x00, 0x66, 0xf1, 0x6b, /* ptabs r25, tr0 */
1638 0xf0, 0xff, 0x01, 0x44, /* blink tr0, r63 */
1639 0xf0, 0xff, 0xf0, 0x6f, /* nop */
1640 0xf0, 0xff, 0xf0, 0x6f, /* nop */
1641 0xf0, 0xff, 0xf0, 0x6f, /* nop */
1642 0x90, 0x01, 0x00, 0xcc, /* movi .PLT0 >> 16, r25 */
1643 0x90, 0x01, 0x00, 0xc8, /* shori .PLT0 & 65535, r25 */
1644 0x00, 0x66, 0xf1, 0x6b, /* ptabs r25, tr0 */
1645 0x50, 0x01, 0x00, 0xcc, /* movi reloc-offset >> 16, r21 */
1646 0x50, 0x01, 0x00, 0xc8, /* shori reloc-offset & 65535, r21 */
1647 0xf0, 0xff, 0x01, 0x44, /* blink tr0, r63 */
1648 0xf0, 0xff, 0xf0, 0x6f, /* nop */
1649 0xf0, 0xff, 0xf0, 0x6f, /* nop */
1650 };
1651
1652 /* Entries in a PIC procedure linkage table look like this. */
1653
1654 static const bfd_byte elf_sh_pic_plt_entry_be[ELF_PLT_ENTRY_SIZE] =
1655 {
1656 0xcc, 0x00, 0x01, 0x90, /* movi nameN@GOT >> 16, r25 */
1657 0xc8, 0x00, 0x01, 0x90, /* shori nameN@GOT & 65535, r25 */
1658 0x40, 0xc2, 0x65, 0x90, /* ldx.l r12, r25, r25 */
1659 0x6b, 0xf1, 0x66, 0x00, /* ptabs r25, tr0 */
1660 0x44, 0x01, 0xff, 0xf0, /* blink tr0, r63 */
1661 0x6f, 0xf0, 0xff, 0xf0, /* nop */
1662 0x6f, 0xf0, 0xff, 0xf0, /* nop */
1663 0x6f, 0xf0, 0xff, 0xf0, /* nop */
1664 0xce, 0x00, 0x01, 0x10, /* movi -GOT_BIAS, r17 */
1665 0x00, 0xc8, 0x45, 0x10, /* add.l r12, r17, r17 */
1666 0x89, 0x10, 0x09, 0x90, /* ld.l r17, 8, r25 */
1667 0x6b, 0xf1, 0x66, 0x00, /* ptabs r25, tr0 */
1668 0x89, 0x10, 0x05, 0x10, /* ld.l r17, 4, r17 */
1669 0xcc, 0x00, 0x01, 0x50, /* movi reloc-offset >> 16, r21 */
1670 0xc8, 0x00, 0x01, 0x50, /* shori reloc-offset & 65535, r21 */
1671 0x44, 0x01, 0xff, 0xf0, /* blink tr0, r63 */
1672 };
1673
1674 static const bfd_byte elf_sh_pic_plt_entry_le[ELF_PLT_ENTRY_SIZE] =
1675 {
1676 0x90, 0x01, 0x00, 0xcc, /* movi nameN@GOT >> 16, r25 */
1677 0x90, 0x01, 0x00, 0xc8, /* shori nameN@GOT & 65535, r25 */
1678 0x90, 0x65, 0xc2, 0x40, /* ldx.l r12, r25, r25 */
1679 0x00, 0x66, 0xf1, 0x6b, /* ptabs r25, tr0 */
1680 0xf0, 0xff, 0x01, 0x44, /* blink tr0, r63 */
1681 0xf0, 0xff, 0xf0, 0x6f, /* nop */
1682 0xf0, 0xff, 0xf0, 0x6f, /* nop */
1683 0xf0, 0xff, 0xf0, 0x6f, /* nop */
1684 0x10, 0x01, 0x00, 0xce, /* movi -GOT_BIAS, r17 */
1685 0x10, 0x45, 0xc8, 0x00, /* add.l r12, r17, r17 */
1686 0x90, 0x09, 0x10, 0x89, /* ld.l r17, 8, r25 */
1687 0x00, 0x66, 0xf1, 0x6b, /* ptabs r25, tr0 */
1688 0x10, 0x05, 0x10, 0x89, /* ld.l r17, 4, r17 */
1689 0x50, 0x01, 0x00, 0xcc, /* movi reloc-offset >> 16, r21 */
1690 0x50, 0x01, 0x00, 0xc8, /* shori reloc-offset & 65535, r21 */
1691 0xf0, 0xff, 0x01, 0x44, /* blink tr0, r63 */
1692 };
1693
1694 static const struct elf_sh_plt_info elf_sh_plts[2][2] = {
1695 {
1696 {
1697 /* Big-endian non-PIC. */
1698 elf_sh_plt0_entry_be,
1699 ELF_PLT_ENTRY_SIZE,
1700 { 0, MINUS_ONE, MINUS_ONE },
1701 elf_sh_plt_entry_be,
1702 ELF_PLT_ENTRY_SIZE,
1703 { 0, 32, 48 },
1704 33 /* includes ISA encoding */
1705 },
1706 {
1707 /* Little-endian non-PIC. */
1708 elf_sh_plt0_entry_le,
1709 ELF_PLT_ENTRY_SIZE,
1710 { 0, MINUS_ONE, MINUS_ONE },
1711 elf_sh_plt_entry_le,
1712 ELF_PLT_ENTRY_SIZE,
1713 { 0, 32, 48 },
1714 33 /* includes ISA encoding */
1715 },
1716 },
1717 {
1718 {
1719 /* Big-endian PIC. */
1720 elf_sh_plt0_entry_be,
1721 ELF_PLT_ENTRY_SIZE,
1722 { MINUS_ONE, MINUS_ONE, MINUS_ONE },
1723 elf_sh_pic_plt_entry_be,
1724 ELF_PLT_ENTRY_SIZE,
1725 { 0, MINUS_ONE, 52 },
1726 33 /* includes ISA encoding */
1727 },
1728 {
1729 /* Little-endian PIC. */
1730 elf_sh_plt0_entry_le,
1731 ELF_PLT_ENTRY_SIZE,
1732 { MINUS_ONE, MINUS_ONE, MINUS_ONE },
1733 elf_sh_pic_plt_entry_le,
1734 ELF_PLT_ENTRY_SIZE,
1735 { 0, MINUS_ONE, 52 },
1736 33 /* includes ISA encoding */
1737 },
1738 }
1739 };
1740
1741 /* Return offset of the linker in PLT0 entry. */
1742 #define elf_sh_plt0_gotplt_offset(info) 0
1743
1744 /* Install a 32-bit PLT field starting at ADDR, which occurs in OUTPUT_BFD.
1745 VALUE is the field's value and CODE_P is true if VALUE refers to code,
1746 not data.
1747
1748 On SH64, each 32-bit field is loaded by a movi/shori pair. */
1749
1750 inline static void
1751 install_plt_field (bfd *output_bfd, bfd_boolean code_p,
1752 unsigned long value, bfd_byte *addr)
1753 {
1754 value |= code_p;
1755 bfd_put_32 (output_bfd,
1756 bfd_get_32 (output_bfd, addr)
1757 | ((value >> 6) & 0x3fffc00),
1758 addr);
1759 bfd_put_32 (output_bfd,
1760 bfd_get_32 (output_bfd, addr + 4)
1761 | ((value << 10) & 0x3fffc00),
1762 addr + 4);
1763 }
1764
1765 /* Return the type of PLT associated with ABFD. PIC_P is true if
1766 the object is position-independent. */
1767
1768 static const struct elf_sh_plt_info *
1769 get_plt_info (bfd *abfd ATTRIBUTE_UNUSED, bfd_boolean pic_p)
1770 {
1771 return &elf_sh_plts[pic_p][!bfd_big_endian (abfd)];
1772 }
1773 #else
1774 /* The size in bytes of an entry in the procedure linkage table. */
1775
1776 #define ELF_PLT_ENTRY_SIZE 28
1777
1778 /* First entry in an absolute procedure linkage table look like this. */
1779
1780 /* Note - this code has been "optimised" not to use r2. r2 is used by
1781 GCC to return the address of large structures, so it should not be
1782 corrupted here. This does mean however, that this PLT does not conform
1783 to the SH PIC ABI. That spec says that r0 contains the type of the PLT
1784 and r2 contains the GOT id. This version stores the GOT id in r0 and
1785 ignores the type. Loaders can easily detect this difference however,
1786 since the type will always be 0 or 8, and the GOT ids will always be
1787 greater than or equal to 12. */
1788 static const bfd_byte elf_sh_plt0_entry_be[ELF_PLT_ENTRY_SIZE] =
1789 {
1790 0xd0, 0x05, /* mov.l 2f,r0 */
1791 0x60, 0x02, /* mov.l @r0,r0 */
1792 0x2f, 0x06, /* mov.l r0,@-r15 */
1793 0xd0, 0x03, /* mov.l 1f,r0 */
1794 0x60, 0x02, /* mov.l @r0,r0 */
1795 0x40, 0x2b, /* jmp @r0 */
1796 0x60, 0xf6, /* mov.l @r15+,r0 */
1797 0x00, 0x09, /* nop */
1798 0x00, 0x09, /* nop */
1799 0x00, 0x09, /* nop */
1800 0, 0, 0, 0, /* 1: replaced with address of .got.plt + 8. */
1801 0, 0, 0, 0, /* 2: replaced with address of .got.plt + 4. */
1802 };
1803
1804 static const bfd_byte elf_sh_plt0_entry_le[ELF_PLT_ENTRY_SIZE] =
1805 {
1806 0x05, 0xd0, /* mov.l 2f,r0 */
1807 0x02, 0x60, /* mov.l @r0,r0 */
1808 0x06, 0x2f, /* mov.l r0,@-r15 */
1809 0x03, 0xd0, /* mov.l 1f,r0 */
1810 0x02, 0x60, /* mov.l @r0,r0 */
1811 0x2b, 0x40, /* jmp @r0 */
1812 0xf6, 0x60, /* mov.l @r15+,r0 */
1813 0x09, 0x00, /* nop */
1814 0x09, 0x00, /* nop */
1815 0x09, 0x00, /* nop */
1816 0, 0, 0, 0, /* 1: replaced with address of .got.plt + 8. */
1817 0, 0, 0, 0, /* 2: replaced with address of .got.plt + 4. */
1818 };
1819
1820 /* Sebsequent entries in an absolute procedure linkage table look like
1821 this. */
1822
1823 static const bfd_byte elf_sh_plt_entry_be[ELF_PLT_ENTRY_SIZE] =
1824 {
1825 0xd0, 0x04, /* mov.l 1f,r0 */
1826 0x60, 0x02, /* mov.l @(r0,r12),r0 */
1827 0xd1, 0x02, /* mov.l 0f,r1 */
1828 0x40, 0x2b, /* jmp @r0 */
1829 0x60, 0x13, /* mov r1,r0 */
1830 0xd1, 0x03, /* mov.l 2f,r1 */
1831 0x40, 0x2b, /* jmp @r0 */
1832 0x00, 0x09, /* nop */
1833 0, 0, 0, 0, /* 0: replaced with address of .PLT0. */
1834 0, 0, 0, 0, /* 1: replaced with address of this symbol in .got. */
1835 0, 0, 0, 0, /* 2: replaced with offset into relocation table. */
1836 };
1837
1838 static const bfd_byte elf_sh_plt_entry_le[ELF_PLT_ENTRY_SIZE] =
1839 {
1840 0x04, 0xd0, /* mov.l 1f,r0 */
1841 0x02, 0x60, /* mov.l @r0,r0 */
1842 0x02, 0xd1, /* mov.l 0f,r1 */
1843 0x2b, 0x40, /* jmp @r0 */
1844 0x13, 0x60, /* mov r1,r0 */
1845 0x03, 0xd1, /* mov.l 2f,r1 */
1846 0x2b, 0x40, /* jmp @r0 */
1847 0x09, 0x00, /* nop */
1848 0, 0, 0, 0, /* 0: replaced with address of .PLT0. */
1849 0, 0, 0, 0, /* 1: replaced with address of this symbol in .got. */
1850 0, 0, 0, 0, /* 2: replaced with offset into relocation table. */
1851 };
1852
1853 /* Entries in a PIC procedure linkage table look like this. */
1854
1855 static const bfd_byte elf_sh_pic_plt_entry_be[ELF_PLT_ENTRY_SIZE] =
1856 {
1857 0xd0, 0x04, /* mov.l 1f,r0 */
1858 0x00, 0xce, /* mov.l @(r0,r12),r0 */
1859 0x40, 0x2b, /* jmp @r0 */
1860 0x00, 0x09, /* nop */
1861 0x50, 0xc2, /* mov.l @(8,r12),r0 */
1862 0xd1, 0x03, /* mov.l 2f,r1 */
1863 0x40, 0x2b, /* jmp @r0 */
1864 0x50, 0xc1, /* mov.l @(4,r12),r0 */
1865 0x00, 0x09, /* nop */
1866 0x00, 0x09, /* nop */
1867 0, 0, 0, 0, /* 1: replaced with address of this symbol in .got. */
1868 0, 0, 0, 0 /* 2: replaced with offset into relocation table. */
1869 };
1870
1871 static const bfd_byte elf_sh_pic_plt_entry_le[ELF_PLT_ENTRY_SIZE] =
1872 {
1873 0x04, 0xd0, /* mov.l 1f,r0 */
1874 0xce, 0x00, /* mov.l @(r0,r12),r0 */
1875 0x2b, 0x40, /* jmp @r0 */
1876 0x09, 0x00, /* nop */
1877 0xc2, 0x50, /* mov.l @(8,r12),r0 */
1878 0x03, 0xd1, /* mov.l 2f,r1 */
1879 0x2b, 0x40, /* jmp @r0 */
1880 0xc1, 0x50, /* mov.l @(4,r12),r0 */
1881 0x09, 0x00, /* nop */
1882 0x09, 0x00, /* nop */
1883 0, 0, 0, 0, /* 1: replaced with address of this symbol in .got. */
1884 0, 0, 0, 0 /* 2: replaced with offset into relocation table. */
1885 };
1886
1887 static const struct elf_sh_plt_info elf_sh_plts[2][2] = {
1888 {
1889 {
1890 /* Big-endian non-PIC. */
1891 elf_sh_plt0_entry_be,
1892 ELF_PLT_ENTRY_SIZE,
1893 { MINUS_ONE, 24, 20 },
1894 elf_sh_plt_entry_be,
1895 ELF_PLT_ENTRY_SIZE,
1896 { 20, 16, 24 },
1897 8
1898 },
1899 {
1900 /* Little-endian non-PIC. */
1901 elf_sh_plt0_entry_le,
1902 ELF_PLT_ENTRY_SIZE,
1903 { MINUS_ONE, 24, 20 },
1904 elf_sh_plt_entry_le,
1905 ELF_PLT_ENTRY_SIZE,
1906 { 20, 16, 24 },
1907 8
1908 },
1909 },
1910 {
1911 {
1912 /* Big-endian PIC. */
1913 elf_sh_plt0_entry_be,
1914 ELF_PLT_ENTRY_SIZE,
1915 { MINUS_ONE, MINUS_ONE, MINUS_ONE },
1916 elf_sh_pic_plt_entry_be,
1917 ELF_PLT_ENTRY_SIZE,
1918 { 20, MINUS_ONE, 24 },
1919 8
1920 },
1921 {
1922 /* Little-endian PIC. */
1923 elf_sh_plt0_entry_le,
1924 ELF_PLT_ENTRY_SIZE,
1925 { MINUS_ONE, MINUS_ONE, MINUS_ONE },
1926 elf_sh_pic_plt_entry_le,
1927 ELF_PLT_ENTRY_SIZE,
1928 { 20, MINUS_ONE, 24 },
1929 8
1930 },
1931 }
1932 };
1933
1934 #define VXWORKS_PLT_HEADER_SIZE 12
1935 #define VXWORKS_PLT_ENTRY_SIZE 24
1936
1937 static const bfd_byte vxworks_sh_plt0_entry_be[VXWORKS_PLT_HEADER_SIZE] =
1938 {
1939 0xd1, 0x01, /* mov.l @(8,pc),r1 */
1940 0x61, 0x12, /* mov.l @r1,r1 */
1941 0x41, 0x2b, /* jmp @r1 */
1942 0x00, 0x09, /* nop */
1943 0, 0, 0, 0 /* 0: replaced with _GLOBAL_OFFSET_TABLE+8. */
1944 };
1945
1946 static const bfd_byte vxworks_sh_plt0_entry_le[VXWORKS_PLT_HEADER_SIZE] =
1947 {
1948 0x01, 0xd1, /* mov.l @(8,pc),r1 */
1949 0x12, 0x61, /* mov.l @r1,r1 */
1950 0x2b, 0x41, /* jmp @r1 */
1951 0x09, 0x00, /* nop */
1952 0, 0, 0, 0 /* 0: replaced with _GLOBAL_OFFSET_TABLE+8. */
1953 };
1954
1955 static const bfd_byte vxworks_sh_plt_entry_be[VXWORKS_PLT_ENTRY_SIZE] =
1956 {
1957 0xd0, 0x01, /* mov.l @(8,pc),r0 */
1958 0x60, 0x02, /* mov.l @r0,r0 */
1959 0x40, 0x2b, /* jmp @r0 */
1960 0x00, 0x09, /* nop */
1961 0, 0, 0, 0, /* 0: replaced with address of this symbol in .got. */
1962 0xd0, 0x01, /* mov.l @(8,pc),r0 */
1963 0xa0, 0x00, /* bra PLT (We need to fix the offset.) */
1964 0x00, 0x09, /* nop */
1965 0x00, 0x09, /* nop */
1966 0, 0, 0, 0, /* 1: replaced with offset into relocation table. */
1967 };
1968
1969 static const bfd_byte vxworks_sh_plt_entry_le[VXWORKS_PLT_ENTRY_SIZE] =
1970 {
1971 0x01, 0xd0, /* mov.l @(8,pc),r0 */
1972 0x02, 0x60, /* mov.l @r0,r0 */
1973 0x2b, 0x40, /* jmp @r0 */
1974 0x09, 0x00, /* nop */
1975 0, 0, 0, 0, /* 0: replaced with address of this symbol in .got. */
1976 0x01, 0xd0, /* mov.l @(8,pc),r0 */
1977 0x00, 0xa0, /* bra PLT (We need to fix the offset.) */
1978 0x09, 0x00, /* nop */
1979 0x09, 0x00, /* nop */
1980 0, 0, 0, 0, /* 1: replaced with offset into relocation table. */
1981 };
1982
1983 static const bfd_byte vxworks_sh_pic_plt_entry_be[VXWORKS_PLT_ENTRY_SIZE] =
1984 {
1985 0xd0, 0x01, /* mov.l @(8,pc),r0 */
1986 0x00, 0xce, /* mov.l @(r0,r12),r0 */
1987 0x40, 0x2b, /* jmp @r0 */
1988 0x00, 0x09, /* nop */
1989 0, 0, 0, 0, /* 0: replaced with offset of this symbol in .got. */
1990 0xd0, 0x01, /* mov.l @(8,pc),r0 */
1991 0x51, 0xc2, /* mov.l @(8,r12),r1 */
1992 0x41, 0x2b, /* jmp @r1 */
1993 0x00, 0x09, /* nop */
1994 0, 0, 0, 0, /* 1: replaced with offset into relocation table. */
1995 };
1996
1997 static const bfd_byte vxworks_sh_pic_plt_entry_le[VXWORKS_PLT_ENTRY_SIZE] =
1998 {
1999 0x01, 0xd0, /* mov.l @(8,pc),r0 */
2000 0xce, 0x00, /* mov.l @(r0,r12),r0 */
2001 0x2b, 0x40, /* jmp @r0 */
2002 0x09, 0x00, /* nop */
2003 0, 0, 0, 0, /* 0: replaced with offset of this symbol in .got. */
2004 0x01, 0xd0, /* mov.l @(8,pc),r0 */
2005 0xc2, 0x51, /* mov.l @(8,r12),r1 */
2006 0x2b, 0x41, /* jmp @r1 */
2007 0x09, 0x00, /* nop */
2008 0, 0, 0, 0, /* 1: replaced with offset into relocation table. */
2009 };
2010
2011 static const struct elf_sh_plt_info vxworks_sh_plts[2][2] = {
2012 {
2013 {
2014 /* Big-endian non-PIC. */
2015 vxworks_sh_plt0_entry_be,
2016 VXWORKS_PLT_HEADER_SIZE,
2017 { MINUS_ONE, MINUS_ONE, 8 },
2018 vxworks_sh_plt_entry_be,
2019 VXWORKS_PLT_ENTRY_SIZE,
2020 { 8, 14, 20 },
2021 12
2022 },
2023 {
2024 /* Little-endian non-PIC. */
2025 vxworks_sh_plt0_entry_le,
2026 VXWORKS_PLT_HEADER_SIZE,
2027 { MINUS_ONE, MINUS_ONE, 8 },
2028 vxworks_sh_plt_entry_le,
2029 VXWORKS_PLT_ENTRY_SIZE,
2030 { 8, 14, 20 },
2031 12
2032 },
2033 },
2034 {
2035 {
2036 /* Big-endian PIC. */
2037 NULL,
2038 0,
2039 { MINUS_ONE, MINUS_ONE, MINUS_ONE },
2040 vxworks_sh_pic_plt_entry_be,
2041 VXWORKS_PLT_ENTRY_SIZE,
2042 { 8, MINUS_ONE, 20 },
2043 12
2044 },
2045 {
2046 /* Little-endian PIC. */
2047 NULL,
2048 0,
2049 { MINUS_ONE, MINUS_ONE, MINUS_ONE },
2050 vxworks_sh_pic_plt_entry_le,
2051 VXWORKS_PLT_ENTRY_SIZE,
2052 { 8, MINUS_ONE, 20 },
2053 12
2054 },
2055 }
2056 };
2057
2058 /* Return the type of PLT associated with ABFD. PIC_P is true if
2059 the object is position-independent. */
2060
2061 static const struct elf_sh_plt_info *
2062 get_plt_info (bfd *abfd ATTRIBUTE_UNUSED, bfd_boolean pic_p)
2063 {
2064 if (vxworks_object_p (abfd))
2065 return &vxworks_sh_plts[pic_p][!bfd_big_endian (abfd)];
2066 return &elf_sh_plts[pic_p][!bfd_big_endian (abfd)];
2067 }
2068
2069 /* Install a 32-bit PLT field starting at ADDR, which occurs in OUTPUT_BFD.
2070 VALUE is the field's value and CODE_P is true if VALUE refers to code,
2071 not data. */
2072
2073 inline static void
2074 install_plt_field (bfd *output_bfd, bfd_boolean code_p ATTRIBUTE_UNUSED,
2075 unsigned long value, bfd_byte *addr)
2076 {
2077 bfd_put_32 (output_bfd, value, addr);
2078 }
2079 #endif
2080
2081 /* Return the index of the PLT entry at byte offset OFFSET. */
2082
2083 static bfd_vma
2084 get_plt_index (const struct elf_sh_plt_info *info, bfd_vma offset)
2085 {
2086 return (offset - info->plt0_entry_size) / info->symbol_entry_size;
2087 }
2088
2089 /* Do the inverse operation. */
2090
2091 static bfd_vma
2092 get_plt_offset (const struct elf_sh_plt_info *info, bfd_vma index)
2093 {
2094 return info->plt0_entry_size + (index * info->symbol_entry_size);
2095 }
2096
2097 /* The sh linker needs to keep track of the number of relocs that it
2098 decides to copy as dynamic relocs in check_relocs for each symbol.
2099 This is so that it can later discard them if they are found to be
2100 unnecessary. We store the information in a field extending the
2101 regular ELF linker hash table. */
2102
2103 struct elf_sh_dyn_relocs
2104 {
2105 struct elf_sh_dyn_relocs *next;
2106
2107 /* The input section of the reloc. */
2108 asection *sec;
2109
2110 /* Total number of relocs copied for the input section. */
2111 bfd_size_type count;
2112
2113 /* Number of pc-relative relocs copied for the input section. */
2114 bfd_size_type pc_count;
2115 };
2116
2117 /* sh ELF linker hash entry. */
2118
2119 struct elf_sh_link_hash_entry
2120 {
2121 struct elf_link_hash_entry root;
2122
2123 #ifdef INCLUDE_SHMEDIA
2124 union
2125 {
2126 bfd_signed_vma refcount;
2127 bfd_vma offset;
2128 } datalabel_got;
2129 #endif
2130
2131 /* Track dynamic relocs copied for this symbol. */
2132 struct elf_sh_dyn_relocs *dyn_relocs;
2133
2134 bfd_signed_vma gotplt_refcount;
2135
2136 enum {
2137 GOT_UNKNOWN = 0, GOT_NORMAL, GOT_TLS_GD, GOT_TLS_IE
2138 } tls_type;
2139 };
2140
2141 #define sh_elf_hash_entry(ent) ((struct elf_sh_link_hash_entry *)(ent))
2142
2143 struct sh_elf_obj_tdata
2144 {
2145 struct elf_obj_tdata root;
2146
2147 /* tls_type for each local got entry. */
2148 char *local_got_tls_type;
2149 };
2150
2151 #define sh_elf_tdata(abfd) \
2152 ((struct sh_elf_obj_tdata *) (abfd)->tdata.any)
2153
2154 #define sh_elf_local_got_tls_type(abfd) \
2155 (sh_elf_tdata (abfd)->local_got_tls_type)
2156
2157 #define is_sh_elf(bfd) \
2158 (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
2159 && elf_tdata (bfd) != NULL \
2160 && elf_object_id (bfd) == SH_ELF_TDATA)
2161
2162 /* Override the generic function because we need to store sh_elf_obj_tdata
2163 as the specific tdata. */
2164
2165 static bfd_boolean
2166 sh_elf_mkobject (bfd *abfd)
2167 {
2168 return bfd_elf_allocate_object (abfd, sizeof (struct sh_elf_obj_tdata),
2169 SH_ELF_TDATA);
2170 }
2171
2172 /* sh ELF linker hash table. */
2173
2174 struct elf_sh_link_hash_table
2175 {
2176 struct elf_link_hash_table root;
2177
2178 /* Short-cuts to get to dynamic linker sections. */
2179 asection *sgot;
2180 asection *sgotplt;
2181 asection *srelgot;
2182 asection *splt;
2183 asection *srelplt;
2184 asection *sdynbss;
2185 asection *srelbss;
2186
2187 /* The (unloaded but important) VxWorks .rela.plt.unloaded section. */
2188 asection *srelplt2;
2189
2190 /* Small local sym cache. */
2191 struct sym_cache sym_cache;
2192
2193 /* A counter or offset to track a TLS got entry. */
2194 union
2195 {
2196 bfd_signed_vma refcount;
2197 bfd_vma offset;
2198 } tls_ldm_got;
2199
2200 /* The type of PLT to use. */
2201 const struct elf_sh_plt_info *plt_info;
2202
2203 /* True if the target system is VxWorks. */
2204 bfd_boolean vxworks_p;
2205 };
2206
2207 /* Traverse an sh ELF linker hash table. */
2208
2209 #define sh_elf_link_hash_traverse(table, func, info) \
2210 (elf_link_hash_traverse \
2211 (&(table)->root, \
2212 (bfd_boolean (*) (struct elf_link_hash_entry *, void *)) (func), \
2213 (info)))
2214
2215 /* Get the sh ELF linker hash table from a link_info structure. */
2216
2217 #define sh_elf_hash_table(p) \
2218 ((struct elf_sh_link_hash_table *) ((p)->hash))
2219
2220 /* Create an entry in an sh ELF linker hash table. */
2221
2222 static struct bfd_hash_entry *
2223 sh_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
2224 struct bfd_hash_table *table,
2225 const char *string)
2226 {
2227 struct elf_sh_link_hash_entry *ret =
2228 (struct elf_sh_link_hash_entry *) entry;
2229
2230 /* Allocate the structure if it has not already been allocated by a
2231 subclass. */
2232 if (ret == (struct elf_sh_link_hash_entry *) NULL)
2233 ret = ((struct elf_sh_link_hash_entry *)
2234 bfd_hash_allocate (table,
2235 sizeof (struct elf_sh_link_hash_entry)));
2236 if (ret == (struct elf_sh_link_hash_entry *) NULL)
2237 return (struct bfd_hash_entry *) ret;
2238
2239 /* Call the allocation method of the superclass. */
2240 ret = ((struct elf_sh_link_hash_entry *)
2241 _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
2242 table, string));
2243 if (ret != (struct elf_sh_link_hash_entry *) NULL)
2244 {
2245 ret->dyn_relocs = NULL;
2246 ret->gotplt_refcount = 0;
2247 #ifdef INCLUDE_SHMEDIA
2248 ret->datalabel_got.refcount = ret->root.got.refcount;
2249 #endif
2250 ret->tls_type = GOT_UNKNOWN;
2251 }
2252
2253 return (struct bfd_hash_entry *) ret;
2254 }
2255
2256 /* Create an sh ELF linker hash table. */
2257
2258 static struct bfd_link_hash_table *
2259 sh_elf_link_hash_table_create (bfd *abfd)
2260 {
2261 struct elf_sh_link_hash_table *ret;
2262 bfd_size_type amt = sizeof (struct elf_sh_link_hash_table);
2263
2264 ret = (struct elf_sh_link_hash_table *) bfd_malloc (amt);
2265 if (ret == (struct elf_sh_link_hash_table *) NULL)
2266 return NULL;
2267
2268 if (!_bfd_elf_link_hash_table_init (&ret->root, abfd,
2269 sh_elf_link_hash_newfunc,
2270 sizeof (struct elf_sh_link_hash_entry)))
2271 {
2272 free (ret);
2273 return NULL;
2274 }
2275
2276 ret->sgot = NULL;
2277 ret->sgotplt = NULL;
2278 ret->srelgot = NULL;
2279 ret->splt = NULL;
2280 ret->srelplt = NULL;
2281 ret->sdynbss = NULL;
2282 ret->srelbss = NULL;
2283 ret->srelplt2 = NULL;
2284 ret->sym_cache.abfd = NULL;
2285 ret->tls_ldm_got.refcount = 0;
2286 ret->plt_info = NULL;
2287 ret->vxworks_p = vxworks_object_p (abfd);
2288
2289 return &ret->root.root;
2290 }
2291
2292 /* Create .got, .gotplt, and .rela.got sections in DYNOBJ, and set up
2293 shortcuts to them in our hash table. */
2294
2295 static bfd_boolean
2296 create_got_section (bfd *dynobj, struct bfd_link_info *info)
2297 {
2298 struct elf_sh_link_hash_table *htab;
2299
2300 if (! _bfd_elf_create_got_section (dynobj, info))
2301 return FALSE;
2302
2303 htab = sh_elf_hash_table (info);
2304 htab->sgot = bfd_get_section_by_name (dynobj, ".got");
2305 htab->sgotplt = bfd_get_section_by_name (dynobj, ".got.plt");
2306 htab->srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
2307 if (! htab->sgot || ! htab->sgotplt || ! htab->srelgot)
2308 abort ();
2309 return TRUE;
2310 }
2311
2312 /* Create dynamic sections when linking against a dynamic object. */
2313
2314 static bfd_boolean
2315 sh_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
2316 {
2317 struct elf_sh_link_hash_table *htab;
2318 flagword flags, pltflags;
2319 register asection *s;
2320 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2321 int ptralign = 0;
2322
2323 switch (bed->s->arch_size)
2324 {
2325 case 32:
2326 ptralign = 2;
2327 break;
2328
2329 case 64:
2330 ptralign = 3;
2331 break;
2332
2333 default:
2334 bfd_set_error (bfd_error_bad_value);
2335 return FALSE;
2336 }
2337
2338 htab = sh_elf_hash_table (info);
2339 if (htab->root.dynamic_sections_created)
2340 return TRUE;
2341
2342 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
2343 .rel[a].bss sections. */
2344
2345 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
2346 | SEC_LINKER_CREATED);
2347
2348 pltflags = flags;
2349 pltflags |= SEC_CODE;
2350 if (bed->plt_not_loaded)
2351 pltflags &= ~ (SEC_LOAD | SEC_HAS_CONTENTS);
2352 if (bed->plt_readonly)
2353 pltflags |= SEC_READONLY;
2354
2355 s = bfd_make_section_with_flags (abfd, ".plt", pltflags);
2356 htab->splt = s;
2357 if (s == NULL
2358 || ! bfd_set_section_alignment (abfd, s, bed->plt_alignment))
2359 return FALSE;
2360
2361 if (bed->want_plt_sym)
2362 {
2363 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
2364 .plt section. */
2365 struct elf_link_hash_entry *h;
2366 struct bfd_link_hash_entry *bh = NULL;
2367
2368 if (! (_bfd_generic_link_add_one_symbol
2369 (info, abfd, "_PROCEDURE_LINKAGE_TABLE_", BSF_GLOBAL, s,
2370 (bfd_vma) 0, (const char *) NULL, FALSE,
2371 get_elf_backend_data (abfd)->collect, &bh)))
2372 return FALSE;
2373
2374 h = (struct elf_link_hash_entry *) bh;
2375 h->def_regular = 1;
2376 h->type = STT_OBJECT;
2377 htab->root.hplt = h;
2378
2379 if (info->shared
2380 && ! bfd_elf_link_record_dynamic_symbol (info, h))
2381 return FALSE;
2382 }
2383
2384 s = bfd_make_section_with_flags (abfd,
2385 bed->default_use_rela_p ? ".rela.plt" : ".rel.plt",
2386 flags | SEC_READONLY);
2387 htab->srelplt = s;
2388 if (s == NULL
2389 || ! bfd_set_section_alignment (abfd, s, ptralign))
2390 return FALSE;
2391
2392 if (htab->sgot == NULL
2393 && !create_got_section (abfd, info))
2394 return FALSE;
2395
2396 {
2397 const char *secname;
2398 char *relname;
2399 flagword secflags;
2400 asection *sec;
2401
2402 for (sec = abfd->sections; sec; sec = sec->next)
2403 {
2404 secflags = bfd_get_section_flags (abfd, sec);
2405 if ((secflags & (SEC_DATA | SEC_LINKER_CREATED))
2406 || ((secflags & SEC_HAS_CONTENTS) != SEC_HAS_CONTENTS))
2407 continue;
2408 secname = bfd_get_section_name (abfd, sec);
2409 relname = (char *) bfd_malloc ((bfd_size_type) strlen (secname) + 6);
2410 strcpy (relname, ".rela");
2411 strcat (relname, secname);
2412 if (bfd_get_section_by_name (abfd, secname))
2413 continue;
2414 s = bfd_make_section_with_flags (abfd, relname,
2415 flags | SEC_READONLY);
2416 if (s == NULL
2417 || ! bfd_set_section_alignment (abfd, s, ptralign))
2418 return FALSE;
2419 }
2420 }
2421
2422 if (bed->want_dynbss)
2423 {
2424 /* The .dynbss section is a place to put symbols which are defined
2425 by dynamic objects, are referenced by regular objects, and are
2426 not functions. We must allocate space for them in the process
2427 image and use a R_*_COPY reloc to tell the dynamic linker to
2428 initialize them at run time. The linker script puts the .dynbss
2429 section into the .bss section of the final image. */
2430 s = bfd_make_section_with_flags (abfd, ".dynbss",
2431 SEC_ALLOC | SEC_LINKER_CREATED);
2432 htab->sdynbss = s;
2433 if (s == NULL)
2434 return FALSE;
2435
2436 /* The .rel[a].bss section holds copy relocs. This section is not
2437 normally needed. We need to create it here, though, so that the
2438 linker will map it to an output section. We can't just create it
2439 only if we need it, because we will not know whether we need it
2440 until we have seen all the input files, and the first time the
2441 main linker code calls BFD after examining all the input files
2442 (size_dynamic_sections) the input sections have already been
2443 mapped to the output sections. If the section turns out not to
2444 be needed, we can discard it later. We will never need this
2445 section when generating a shared object, since they do not use
2446 copy relocs. */
2447 if (! info->shared)
2448 {
2449 s = bfd_make_section_with_flags (abfd,
2450 (bed->default_use_rela_p
2451 ? ".rela.bss" : ".rel.bss"),
2452 flags | SEC_READONLY);
2453 htab->srelbss = s;
2454 if (s == NULL
2455 || ! bfd_set_section_alignment (abfd, s, ptralign))
2456 return FALSE;
2457 }
2458 }
2459
2460 if (htab->vxworks_p)
2461 {
2462 if (!elf_vxworks_create_dynamic_sections (abfd, info, &htab->srelplt2))
2463 return FALSE;
2464 }
2465
2466 return TRUE;
2467 }
2468
2469 /* Adjust a symbol defined by a dynamic object and referenced by a
2470 regular object. The current definition is in some section of the
2471 dynamic object, but we're not including those sections. We have to
2472 change the definition to something the rest of the link can
2473 understand. */
2474
2475 static bfd_boolean
2476 sh_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
2477 struct elf_link_hash_entry *h)
2478 {
2479 struct elf_sh_link_hash_table *htab;
2480 struct elf_sh_link_hash_entry *eh;
2481 struct elf_sh_dyn_relocs *p;
2482 asection *s;
2483
2484 htab = sh_elf_hash_table (info);
2485
2486 /* Make sure we know what is going on here. */
2487 BFD_ASSERT (htab->root.dynobj != NULL
2488 && (h->needs_plt
2489 || h->u.weakdef != NULL
2490 || (h->def_dynamic
2491 && h->ref_regular
2492 && !h->def_regular)));
2493
2494 /* If this is a function, put it in the procedure linkage table. We
2495 will fill in the contents of the procedure linkage table later,
2496 when we know the address of the .got section. */
2497 if (h->type == STT_FUNC
2498 || h->needs_plt)
2499 {
2500 if (h->plt.refcount <= 0
2501 || SYMBOL_CALLS_LOCAL (info, h)
2502 || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
2503 && h->root.type == bfd_link_hash_undefweak))
2504 {
2505 /* This case can occur if we saw a PLT reloc in an input
2506 file, but the symbol was never referred to by a dynamic
2507 object. In such a case, we don't actually need to build
2508 a procedure linkage table, and we can just do a REL32
2509 reloc instead. */
2510 h->plt.offset = (bfd_vma) -1;
2511 h->needs_plt = 0;
2512 }
2513
2514 return TRUE;
2515 }
2516 else
2517 h->plt.offset = (bfd_vma) -1;
2518
2519 /* If this is a weak symbol, and there is a real definition, the
2520 processor independent code will have arranged for us to see the
2521 real definition first, and we can just use the same value. */
2522 if (h->u.weakdef != NULL)
2523 {
2524 BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
2525 || h->u.weakdef->root.type == bfd_link_hash_defweak);
2526 h->root.u.def.section = h->u.weakdef->root.u.def.section;
2527 h->root.u.def.value = h->u.weakdef->root.u.def.value;
2528 if (info->nocopyreloc)
2529 h->non_got_ref = h->u.weakdef->non_got_ref;
2530 return TRUE;
2531 }
2532
2533 /* This is a reference to a symbol defined by a dynamic object which
2534 is not a function. */
2535
2536 /* If we are creating a shared library, we must presume that the
2537 only references to the symbol are via the global offset table.
2538 For such cases we need not do anything here; the relocations will
2539 be handled correctly by relocate_section. */
2540 if (info->shared)
2541 return TRUE;
2542
2543 /* If there are no references to this symbol that do not use the
2544 GOT, we don't need to generate a copy reloc. */
2545 if (!h->non_got_ref)
2546 return TRUE;
2547
2548 /* If -z nocopyreloc was given, we won't generate them either. */
2549 if (info->nocopyreloc)
2550 {
2551 h->non_got_ref = 0;
2552 return TRUE;
2553 }
2554
2555 eh = (struct elf_sh_link_hash_entry *) h;
2556 for (p = eh->dyn_relocs; p != NULL; p = p->next)
2557 {
2558 s = p->sec->output_section;
2559 if (s != NULL && (s->flags & (SEC_READONLY | SEC_HAS_CONTENTS)) != 0)
2560 break;
2561 }
2562
2563 /* If we didn't find any dynamic relocs in sections which needs the
2564 copy reloc, then we'll be keeping the dynamic relocs and avoiding
2565 the copy reloc. */
2566 if (p == NULL)
2567 {
2568 h->non_got_ref = 0;
2569 return TRUE;
2570 }
2571
2572 if (h->size == 0)
2573 {
2574 (*_bfd_error_handler) (_("dynamic variable `%s' is zero size"),
2575 h->root.root.string);
2576 return TRUE;
2577 }
2578
2579 /* We must allocate the symbol in our .dynbss section, which will
2580 become part of the .bss section of the executable. There will be
2581 an entry for this symbol in the .dynsym section. The dynamic
2582 object will contain position independent code, so all references
2583 from the dynamic object to this symbol will go through the global
2584 offset table. The dynamic linker will use the .dynsym entry to
2585 determine the address it must put in the global offset table, so
2586 both the dynamic object and the regular object will refer to the
2587 same memory location for the variable. */
2588
2589 s = htab->sdynbss;
2590 BFD_ASSERT (s != NULL);
2591
2592 /* We must generate a R_SH_COPY reloc to tell the dynamic linker to
2593 copy the initial value out of the dynamic object and into the
2594 runtime process image. We need to remember the offset into the
2595 .rela.bss section we are going to use. */
2596 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
2597 {
2598 asection *srel;
2599
2600 srel = htab->srelbss;
2601 BFD_ASSERT (srel != NULL);
2602 srel->size += sizeof (Elf32_External_Rela);
2603 h->needs_copy = 1;
2604 }
2605
2606 return _bfd_elf_adjust_dynamic_copy (h, s);
2607 }
2608
2609 /* Allocate space in .plt, .got and associated reloc sections for
2610 dynamic relocs. */
2611
2612 static bfd_boolean
2613 allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
2614 {
2615 struct bfd_link_info *info;
2616 struct elf_sh_link_hash_table *htab;
2617 struct elf_sh_link_hash_entry *eh;
2618 struct elf_sh_dyn_relocs *p;
2619
2620 if (h->root.type == bfd_link_hash_indirect)
2621 return TRUE;
2622
2623 if (h->root.type == bfd_link_hash_warning)
2624 /* When warning symbols are created, they **replace** the "real"
2625 entry in the hash table, thus we never get to see the real
2626 symbol in a hash traversal. So look at it now. */
2627 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2628
2629 info = (struct bfd_link_info *) inf;
2630 htab = sh_elf_hash_table (info);
2631
2632 eh = (struct elf_sh_link_hash_entry *) h;
2633 if ((h->got.refcount > 0
2634 || h->forced_local)
2635 && eh->gotplt_refcount > 0)
2636 {
2637 /* The symbol has been forced local, or we have some direct got refs,
2638 so treat all the gotplt refs as got refs. */
2639 h->got.refcount += eh->gotplt_refcount;
2640 if (h->plt.refcount >= eh->gotplt_refcount)
2641 h->plt.refcount -= eh->gotplt_refcount;
2642 }
2643
2644 if (htab->root.dynamic_sections_created
2645 && h->plt.refcount > 0
2646 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
2647 || h->root.type != bfd_link_hash_undefweak))
2648 {
2649 /* Make sure this symbol is output as a dynamic symbol.
2650 Undefined weak syms won't yet be marked as dynamic. */
2651 if (h->dynindx == -1
2652 && !h->forced_local)
2653 {
2654 if (! bfd_elf_link_record_dynamic_symbol (info, h))
2655 return FALSE;
2656 }
2657
2658 if (info->shared
2659 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, 0, h))
2660 {
2661 asection *s = htab->splt;
2662
2663 /* If this is the first .plt entry, make room for the special
2664 first entry. */
2665 if (s->size == 0)
2666 s->size += htab->plt_info->plt0_entry_size;
2667
2668 h->plt.offset = s->size;
2669
2670 /* If this symbol is not defined in a regular file, and we are
2671 not generating a shared library, then set the symbol to this
2672 location in the .plt. This is required to make function
2673 pointers compare as equal between the normal executable and
2674 the shared library. */
2675 if (! info->shared
2676 && !h->def_regular)
2677 {
2678 h->root.u.def.section = s;
2679 h->root.u.def.value = h->plt.offset;
2680 }
2681
2682 /* Make room for this entry. */
2683 s->size += htab->plt_info->symbol_entry_size;
2684
2685 /* We also need to make an entry in the .got.plt section, which
2686 will be placed in the .got section by the linker script. */
2687 htab->sgotplt->size += 4;
2688
2689 /* We also need to make an entry in the .rel.plt section. */
2690 htab->srelplt->size += sizeof (Elf32_External_Rela);
2691
2692 if (htab->vxworks_p && !info->shared)
2693 {
2694 /* VxWorks executables have a second set of relocations
2695 for each PLT entry. They go in a separate relocation
2696 section, which is processed by the kernel loader. */
2697
2698 /* There is a relocation for the initial PLT entry:
2699 an R_SH_DIR32 relocation for _GLOBAL_OFFSET_TABLE_. */
2700 if (h->plt.offset == htab->plt_info->plt0_entry_size)
2701 htab->srelplt2->size += sizeof (Elf32_External_Rela);
2702
2703 /* There are two extra relocations for each subsequent
2704 PLT entry: an R_SH_DIR32 relocation for the GOT entry,
2705 and an R_SH_DIR32 relocation for the PLT entry. */
2706 htab->srelplt2->size += sizeof (Elf32_External_Rela) * 2;
2707 }
2708 }
2709 else
2710 {
2711 h->plt.offset = (bfd_vma) -1;
2712 h->needs_plt = 0;
2713 }
2714 }
2715 else
2716 {
2717 h->plt.offset = (bfd_vma) -1;
2718 h->needs_plt = 0;
2719 }
2720
2721 if (h->got.refcount > 0)
2722 {
2723 asection *s;
2724 bfd_boolean dyn;
2725 int tls_type = sh_elf_hash_entry (h)->tls_type;
2726
2727 /* Make sure this symbol is output as a dynamic symbol.
2728 Undefined weak syms won't yet be marked as dynamic. */
2729 if (h->dynindx == -1
2730 && !h->forced_local)
2731 {
2732 if (! bfd_elf_link_record_dynamic_symbol (info, h))
2733 return FALSE;
2734 }
2735
2736 s = htab->sgot;
2737 h->got.offset = s->size;
2738 s->size += 4;
2739 /* R_SH_TLS_GD needs 2 consecutive GOT slots. */
2740 if (tls_type == GOT_TLS_GD)
2741 s->size += 4;
2742 dyn = htab->root.dynamic_sections_created;
2743 /* R_SH_TLS_IE_32 needs one dynamic relocation if dynamic,
2744 R_SH_TLS_GD needs one if local symbol and two if global. */
2745 if ((tls_type == GOT_TLS_GD && h->dynindx == -1)
2746 || (tls_type == GOT_TLS_IE && dyn))
2747 htab->srelgot->size += sizeof (Elf32_External_Rela);
2748 else if (tls_type == GOT_TLS_GD)
2749 htab->srelgot->size += 2 * sizeof (Elf32_External_Rela);
2750 else if ((ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
2751 || h->root.type != bfd_link_hash_undefweak)
2752 && (info->shared
2753 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, h)))
2754 htab->srelgot->size += sizeof (Elf32_External_Rela);
2755 }
2756 else
2757 h->got.offset = (bfd_vma) -1;
2758
2759 #ifdef INCLUDE_SHMEDIA
2760 if (eh->datalabel_got.refcount > 0)
2761 {
2762 asection *s;
2763 bfd_boolean dyn;
2764
2765 /* Make sure this symbol is output as a dynamic symbol.
2766 Undefined weak syms won't yet be marked as dynamic. */
2767 if (h->dynindx == -1
2768 && !h->forced_local)
2769 {
2770 if (! bfd_elf_link_record_dynamic_symbol (info, h))
2771 return FALSE;
2772 }
2773
2774 s = htab->sgot;
2775 eh->datalabel_got.offset = s->size;
2776 s->size += 4;
2777 dyn = htab->root.dynamic_sections_created;
2778 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h))
2779 htab->srelgot->size += sizeof (Elf32_External_Rela);
2780 }
2781 else
2782 eh->datalabel_got.offset = (bfd_vma) -1;
2783 #endif
2784
2785 if (eh->dyn_relocs == NULL)
2786 return TRUE;
2787
2788 /* In the shared -Bsymbolic case, discard space allocated for
2789 dynamic pc-relative relocs against symbols which turn out to be
2790 defined in regular objects. For the normal shared case, discard
2791 space for pc-relative relocs that have become local due to symbol
2792 visibility changes. */
2793
2794 if (info->shared)
2795 {
2796 if (SYMBOL_CALLS_LOCAL (info, h))
2797 {
2798 struct elf_sh_dyn_relocs **pp;
2799
2800 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
2801 {
2802 p->count -= p->pc_count;
2803 p->pc_count = 0;
2804 if (p->count == 0)
2805 *pp = p->next;
2806 else
2807 pp = &p->next;
2808 }
2809 }
2810
2811 if (htab->vxworks_p)
2812 {
2813 struct elf_sh_dyn_relocs **pp;
2814
2815 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
2816 {
2817 if (strcmp (p->sec->output_section->name, ".tls_vars") == 0)
2818 *pp = p->next;
2819 else
2820 pp = &p->next;
2821 }
2822 }
2823
2824 /* Also discard relocs on undefined weak syms with non-default
2825 visibility. */
2826 if (eh->dyn_relocs != NULL
2827 && h->root.type == bfd_link_hash_undefweak)
2828 {
2829 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
2830 eh->dyn_relocs = NULL;
2831
2832 /* Make sure undefined weak symbols are output as a dynamic
2833 symbol in PIEs. */
2834 else if (h->dynindx == -1
2835 && !h->forced_local)
2836 {
2837 if (! bfd_elf_link_record_dynamic_symbol (info, h))
2838 return FALSE;
2839 }
2840 }
2841 }
2842 else
2843 {
2844 /* For the non-shared case, discard space for relocs against
2845 symbols which turn out to need copy relocs or are not
2846 dynamic. */
2847
2848 if (!h->non_got_ref
2849 && ((h->def_dynamic
2850 && !h->def_regular)
2851 || (htab->root.dynamic_sections_created
2852 && (h->root.type == bfd_link_hash_undefweak
2853 || h->root.type == bfd_link_hash_undefined))))
2854 {
2855 /* Make sure this symbol is output as a dynamic symbol.
2856 Undefined weak syms won't yet be marked as dynamic. */
2857 if (h->dynindx == -1
2858 && !h->forced_local)
2859 {
2860 if (! bfd_elf_link_record_dynamic_symbol (info, h))
2861 return FALSE;
2862 }
2863
2864 /* If that succeeded, we know we'll be keeping all the
2865 relocs. */
2866 if (h->dynindx != -1)
2867 goto keep;
2868 }
2869
2870 eh->dyn_relocs = NULL;
2871
2872 keep: ;
2873 }
2874
2875 /* Finally, allocate space. */
2876 for (p = eh->dyn_relocs; p != NULL; p = p->next)
2877 {
2878 asection *sreloc = elf_section_data (p->sec)->sreloc;
2879 sreloc->size += p->count * sizeof (Elf32_External_Rela);
2880 }
2881
2882 return TRUE;
2883 }
2884
2885 /* Find any dynamic relocs that apply to read-only sections. */
2886
2887 static bfd_boolean
2888 readonly_dynrelocs (struct elf_link_hash_entry *h, void *inf)
2889 {
2890 struct elf_sh_link_hash_entry *eh;
2891 struct elf_sh_dyn_relocs *p;
2892
2893 if (h->root.type == bfd_link_hash_warning)
2894 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2895
2896 eh = (struct elf_sh_link_hash_entry *) h;
2897 for (p = eh->dyn_relocs; p != NULL; p = p->next)
2898 {
2899 asection *s = p->sec->output_section;
2900
2901 if (s != NULL && (s->flags & SEC_READONLY) != 0)
2902 {
2903 struct bfd_link_info *info = (struct bfd_link_info *) inf;
2904
2905 info->flags |= DF_TEXTREL;
2906
2907 /* Not an error, just cut short the traversal. */
2908 return FALSE;
2909 }
2910 }
2911 return TRUE;
2912 }
2913
2914 /* This function is called after all the input files have been read,
2915 and the input sections have been assigned to output sections.
2916 It's a convenient place to determine the PLT style. */
2917
2918 static bfd_boolean
2919 sh_elf_always_size_sections (bfd *output_bfd, struct bfd_link_info *info)
2920 {
2921 sh_elf_hash_table (info)->plt_info = get_plt_info (output_bfd, info->shared);
2922 return TRUE;
2923 }
2924
2925 /* Set the sizes of the dynamic sections. */
2926
2927 static bfd_boolean
2928 sh_elf_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
2929 struct bfd_link_info *info)
2930 {
2931 struct elf_sh_link_hash_table *htab;
2932 bfd *dynobj;
2933 asection *s;
2934 bfd_boolean relocs;
2935 bfd *ibfd;
2936
2937 htab = sh_elf_hash_table (info);
2938 dynobj = htab->root.dynobj;
2939 BFD_ASSERT (dynobj != NULL);
2940
2941 if (htab->root.dynamic_sections_created)
2942 {
2943 /* Set the contents of the .interp section to the interpreter. */
2944 if (info->executable)
2945 {
2946 s = bfd_get_section_by_name (dynobj, ".interp");
2947 BFD_ASSERT (s != NULL);
2948 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
2949 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
2950 }
2951 }
2952
2953 /* Set up .got offsets for local syms, and space for local dynamic
2954 relocs. */
2955 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
2956 {
2957 bfd_signed_vma *local_got;
2958 bfd_signed_vma *end_local_got;
2959 char *local_tls_type;
2960 bfd_size_type locsymcount;
2961 Elf_Internal_Shdr *symtab_hdr;
2962 asection *srel;
2963
2964 if (! is_sh_elf (ibfd))
2965 continue;
2966
2967 for (s = ibfd->sections; s != NULL; s = s->next)
2968 {
2969 struct elf_sh_dyn_relocs *p;
2970
2971 for (p = ((struct elf_sh_dyn_relocs *)
2972 elf_section_data (s)->local_dynrel);
2973 p != NULL;
2974 p = p->next)
2975 {
2976 if (! bfd_is_abs_section (p->sec)
2977 && bfd_is_abs_section (p->sec->output_section))
2978 {
2979 /* Input section has been discarded, either because
2980 it is a copy of a linkonce section or due to
2981 linker script /DISCARD/, so we'll be discarding
2982 the relocs too. */
2983 }
2984 else if (htab->vxworks_p
2985 && strcmp (p->sec->output_section->name,
2986 ".tls_vars") == 0)
2987 {
2988 /* Relocations in vxworks .tls_vars sections are
2989 handled specially by the loader. */
2990 }
2991 else if (p->count != 0)
2992 {
2993 srel = elf_section_data (p->sec)->sreloc;
2994 srel->size += p->count * sizeof (Elf32_External_Rela);
2995 if ((p->sec->output_section->flags & SEC_READONLY) != 0)
2996 info->flags |= DF_TEXTREL;
2997 }
2998 }
2999 }
3000
3001 local_got = elf_local_got_refcounts (ibfd);
3002 if (!local_got)
3003 continue;
3004
3005 symtab_hdr = &elf_symtab_hdr (ibfd);
3006 locsymcount = symtab_hdr->sh_info;
3007 #ifdef INCLUDE_SHMEDIA
3008 /* Count datalabel local GOT. */
3009 locsymcount *= 2;
3010 #endif
3011 end_local_got = local_got + locsymcount;
3012 local_tls_type = sh_elf_local_got_tls_type (ibfd);
3013 s = htab->sgot;
3014 srel = htab->srelgot;
3015 for (; local_got < end_local_got; ++local_got)
3016 {
3017 if (*local_got > 0)
3018 {
3019 *local_got = s->size;
3020 s->size += 4;
3021 if (*local_tls_type == GOT_TLS_GD)
3022 s->size += 4;
3023 if (info->shared)
3024 srel->size += sizeof (Elf32_External_Rela);
3025 }
3026 else
3027 *local_got = (bfd_vma) -1;
3028 ++local_tls_type;
3029 }
3030 }
3031
3032 if (htab->tls_ldm_got.refcount > 0)
3033 {
3034 /* Allocate 2 got entries and 1 dynamic reloc for R_SH_TLS_LD_32
3035 relocs. */
3036 htab->tls_ldm_got.offset = htab->sgot->size;
3037 htab->sgot->size += 8;
3038 htab->srelgot->size += sizeof (Elf32_External_Rela);
3039 }
3040 else
3041 htab->tls_ldm_got.offset = -1;
3042
3043 /* Allocate global sym .plt and .got entries, and space for global
3044 sym dynamic relocs. */
3045 elf_link_hash_traverse (&htab->root, allocate_dynrelocs, info);
3046
3047 /* We now have determined the sizes of the various dynamic sections.
3048 Allocate memory for them. */
3049 relocs = FALSE;
3050 for (s = dynobj->sections; s != NULL; s = s->next)
3051 {
3052 if ((s->flags & SEC_LINKER_CREATED) == 0)
3053 continue;
3054
3055 if (s == htab->splt
3056 || s == htab->sgot
3057 || s == htab->sgotplt
3058 || s == htab->sdynbss)
3059 {
3060 /* Strip this section if we don't need it; see the
3061 comment below. */
3062 }
3063 else if (CONST_STRNEQ (bfd_get_section_name (dynobj, s), ".rela"))
3064 {
3065 if (s->size != 0 && s != htab->srelplt && s != htab->srelplt2)
3066 relocs = TRUE;
3067
3068 /* We use the reloc_count field as a counter if we need
3069 to copy relocs into the output file. */
3070 s->reloc_count = 0;
3071 }
3072 else
3073 {
3074 /* It's not one of our sections, so don't allocate space. */
3075 continue;
3076 }
3077
3078 if (s->size == 0)
3079 {
3080 /* If we don't need this section, strip it from the
3081 output file. This is mostly to handle .rela.bss and
3082 .rela.plt. We must create both sections in
3083 create_dynamic_sections, because they must be created
3084 before the linker maps input sections to output
3085 sections. The linker does that before
3086 adjust_dynamic_symbol is called, and it is that
3087 function which decides whether anything needs to go
3088 into these sections. */
3089
3090 s->flags |= SEC_EXCLUDE;
3091 continue;
3092 }
3093
3094 if ((s->flags & SEC_HAS_CONTENTS) == 0)
3095 continue;
3096
3097 /* Allocate memory for the section contents. We use bfd_zalloc
3098 here in case unused entries are not reclaimed before the
3099 section's contents are written out. This should not happen,
3100 but this way if it does, we get a R_SH_NONE reloc instead
3101 of garbage. */
3102 s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->size);
3103 if (s->contents == NULL)
3104 return FALSE;
3105 }
3106
3107 if (htab->root.dynamic_sections_created)
3108 {
3109 /* Add some entries to the .dynamic section. We fill in the
3110 values later, in sh_elf_finish_dynamic_sections, but we
3111 must add the entries now so that we get the correct size for
3112 the .dynamic section. The DT_DEBUG entry is filled in by the
3113 dynamic linker and used by the debugger. */
3114 #define add_dynamic_entry(TAG, VAL) \
3115 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
3116
3117 if (info->executable)
3118 {
3119 if (! add_dynamic_entry (DT_DEBUG, 0))
3120 return FALSE;
3121 }
3122
3123 if (htab->splt->size != 0)
3124 {
3125 if (! add_dynamic_entry (DT_PLTGOT, 0)
3126 || ! add_dynamic_entry (DT_PLTRELSZ, 0)
3127 || ! add_dynamic_entry (DT_PLTREL, DT_RELA)
3128 || ! add_dynamic_entry (DT_JMPREL, 0))
3129 return FALSE;
3130 }
3131
3132 if (relocs)
3133 {
3134 if (! add_dynamic_entry (DT_RELA, 0)
3135 || ! add_dynamic_entry (DT_RELASZ, 0)
3136 || ! add_dynamic_entry (DT_RELAENT,
3137 sizeof (Elf32_External_Rela)))
3138 return FALSE;
3139
3140 /* If any dynamic relocs apply to a read-only section,
3141 then we need a DT_TEXTREL entry. */
3142 if ((info->flags & DF_TEXTREL) == 0)
3143 elf_link_hash_traverse (&htab->root, readonly_dynrelocs, info);
3144
3145 if ((info->flags & DF_TEXTREL) != 0)
3146 {
3147 if (! add_dynamic_entry (DT_TEXTREL, 0))
3148 return FALSE;
3149 }
3150 }
3151 if (htab->vxworks_p
3152 && !elf_vxworks_add_dynamic_entries (output_bfd, info))
3153 return FALSE;
3154 }
3155 #undef add_dynamic_entry
3156
3157 return TRUE;
3158 }
3159
3160 /* Relocate an SH ELF section. */
3161
3162 static bfd_boolean
3163 sh_elf_relocate_section (bfd *output_bfd, struct bfd_link_info *info,
3164 bfd *input_bfd, asection *input_section,
3165 bfd_byte *contents, Elf_Internal_Rela *relocs,
3166 Elf_Internal_Sym *local_syms,
3167 asection **local_sections)
3168 {
3169 struct elf_sh_link_hash_table *htab;
3170 Elf_Internal_Shdr *symtab_hdr;
3171 struct elf_link_hash_entry **sym_hashes;
3172 Elf_Internal_Rela *rel, *relend;
3173 bfd *dynobj;
3174 bfd_vma *local_got_offsets;
3175 asection *sgot;
3176 asection *sgotplt;
3177 asection *splt;
3178 asection *sreloc;
3179 asection *srelgot;
3180 bfd_boolean is_vxworks_tls;
3181
3182 BFD_ASSERT (is_sh_elf (input_bfd));
3183
3184 htab = sh_elf_hash_table (info);
3185 symtab_hdr = &elf_symtab_hdr (input_bfd);
3186 sym_hashes = elf_sym_hashes (input_bfd);
3187 dynobj = htab->root.dynobj;
3188 local_got_offsets = elf_local_got_offsets (input_bfd);
3189
3190 sgot = htab->sgot;
3191 sgotplt = htab->sgotplt;
3192 splt = htab->splt;
3193 sreloc = NULL;
3194 srelgot = NULL;
3195 /* We have to handle relocations in vxworks .tls_vars sections
3196 specially, because the dynamic loader is 'weird'. */
3197 is_vxworks_tls = (htab->vxworks_p && info->shared
3198 && !strcmp (input_section->output_section->name,
3199 ".tls_vars"));
3200
3201 rel = relocs;
3202 relend = relocs + input_section->reloc_count;
3203 for (; rel < relend; rel++)
3204 {
3205 int r_type;
3206 reloc_howto_type *howto;
3207 unsigned long r_symndx;
3208 Elf_Internal_Sym *sym;
3209 asection *sec;
3210 struct elf_link_hash_entry *h;
3211 bfd_vma relocation;
3212 bfd_vma addend = (bfd_vma) 0;
3213 bfd_reloc_status_type r;
3214 int seen_stt_datalabel = 0;
3215 bfd_vma off;
3216 int tls_type;
3217
3218 r_symndx = ELF32_R_SYM (rel->r_info);
3219
3220 r_type = ELF32_R_TYPE (rel->r_info);
3221
3222 /* Many of the relocs are only used for relaxing, and are
3223 handled entirely by the relaxation code. */
3224 if (r_type >= (int) R_SH_GNU_VTINHERIT
3225 && r_type <= (int) R_SH_LABEL)
3226 continue;
3227 if (r_type == (int) R_SH_NONE)
3228 continue;
3229
3230 if (r_type < 0
3231 || r_type >= R_SH_max
3232 || (r_type >= (int) R_SH_FIRST_INVALID_RELOC
3233 && r_type <= (int) R_SH_LAST_INVALID_RELOC)
3234 || ( r_type >= (int) R_SH_FIRST_INVALID_RELOC_3
3235 && r_type <= (int) R_SH_LAST_INVALID_RELOC_3)
3236 || ( r_type >= (int) R_SH_FIRST_INVALID_RELOC_4
3237 && r_type <= (int) R_SH_LAST_INVALID_RELOC_4)
3238 || ( r_type >= (int) R_SH_FIRST_INVALID_RELOC_5
3239 && r_type <= (int) R_SH_LAST_INVALID_RELOC_5)
3240 || (r_type >= (int) R_SH_FIRST_INVALID_RELOC_2
3241 && r_type <= (int) R_SH_LAST_INVALID_RELOC_2))
3242 {
3243 bfd_set_error (bfd_error_bad_value);
3244 return FALSE;
3245 }
3246
3247 howto = get_howto_table (output_bfd) + r_type;
3248
3249 /* For relocs that aren't partial_inplace, we get the addend from
3250 the relocation. */
3251 if (! howto->partial_inplace)
3252 addend = rel->r_addend;
3253
3254 h = NULL;
3255 sym = NULL;
3256 sec = NULL;
3257 if (r_symndx < symtab_hdr->sh_info)
3258 {
3259 sym = local_syms + r_symndx;
3260 sec = local_sections[r_symndx];
3261 relocation = (sec->output_section->vma
3262 + sec->output_offset
3263 + sym->st_value);
3264 /* A local symbol never has STO_SH5_ISA32, so we don't need
3265 datalabel processing here. Make sure this does not change
3266 without notice. */
3267 if ((sym->st_other & STO_SH5_ISA32) != 0)
3268 ((*info->callbacks->reloc_dangerous)
3269 (info,
3270 _("Unexpected STO_SH5_ISA32 on local symbol is not handled"),
3271 input_bfd, input_section, rel->r_offset));
3272
3273 if (sec != NULL && elf_discarded_section (sec))
3274 /* Handled below. */
3275 ;
3276 else if (info->relocatable)
3277 {
3278 /* This is a relocatable link. We don't have to change
3279 anything, unless the reloc is against a section symbol,
3280 in which case we have to adjust according to where the
3281 section symbol winds up in the output section. */
3282 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
3283 {
3284 if (! howto->partial_inplace)
3285 {
3286 /* For relocations with the addend in the
3287 relocation, we need just to update the addend.
3288 All real relocs are of type partial_inplace; this
3289 code is mostly for completeness. */
3290 rel->r_addend += sec->output_offset;
3291
3292 continue;
3293 }
3294
3295 /* Relocs of type partial_inplace need to pick up the
3296 contents in the contents and add the offset resulting
3297 from the changed location of the section symbol.
3298 Using _bfd_final_link_relocate (e.g. goto
3299 final_link_relocate) here would be wrong, because
3300 relocations marked pc_relative would get the current
3301 location subtracted, and we must only do that at the
3302 final link. */
3303 r = _bfd_relocate_contents (howto, input_bfd,
3304 sec->output_offset
3305 + sym->st_value,
3306 contents + rel->r_offset);
3307 goto relocation_done;
3308 }
3309
3310 continue;
3311 }
3312 else if (! howto->partial_inplace)
3313 {
3314 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
3315 addend = rel->r_addend;
3316 }
3317 else if ((sec->flags & SEC_MERGE)
3318 && ELF_ST_TYPE (sym->st_info) == STT_SECTION)
3319 {
3320 asection *msec;
3321
3322 if (howto->rightshift || howto->src_mask != 0xffffffff)
3323 {
3324 (*_bfd_error_handler)
3325 (_("%B(%A+0x%lx): %s relocation against SEC_MERGE section"),
3326 input_bfd, input_section,
3327 (long) rel->r_offset, howto->name);
3328 return FALSE;
3329 }
3330
3331 addend = bfd_get_32 (input_bfd, contents + rel->r_offset);
3332 msec = sec;
3333 addend =
3334 _bfd_elf_rel_local_sym (output_bfd, sym, &msec, addend)
3335 - relocation;
3336 addend += msec->output_section->vma + msec->output_offset;
3337 bfd_put_32 (input_bfd, addend, contents + rel->r_offset);
3338 addend = 0;
3339 }
3340 }
3341 else
3342 {
3343 /* FIXME: Ought to make use of the RELOC_FOR_GLOBAL_SYMBOL macro. */
3344
3345 relocation = 0;
3346 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
3347 while (h->root.type == bfd_link_hash_indirect
3348 || h->root.type == bfd_link_hash_warning)
3349 {
3350 #ifdef INCLUDE_SHMEDIA
3351 /* If the reference passes a symbol marked with
3352 STT_DATALABEL, then any STO_SH5_ISA32 on the final value
3353 doesn't count. */
3354 seen_stt_datalabel |= h->type == STT_DATALABEL;
3355 #endif
3356 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3357 }
3358 if (h->root.type == bfd_link_hash_defined
3359 || h->root.type == bfd_link_hash_defweak)
3360 {
3361 bfd_boolean dyn;
3362
3363 dyn = htab->root.dynamic_sections_created;
3364 sec = h->root.u.def.section;
3365 /* In these cases, we don't need the relocation value.
3366 We check specially because in some obscure cases
3367 sec->output_section will be NULL. */
3368 if (r_type == R_SH_GOTPC
3369 || r_type == R_SH_GOTPC_LOW16
3370 || r_type == R_SH_GOTPC_MEDLOW16
3371 || r_type == R_SH_GOTPC_MEDHI16
3372 || r_type == R_SH_GOTPC_HI16
3373 || ((r_type == R_SH_PLT32
3374 || r_type == R_SH_PLT_LOW16
3375 || r_type == R_SH_PLT_MEDLOW16
3376 || r_type == R_SH_PLT_MEDHI16
3377 || r_type == R_SH_PLT_HI16)
3378 && h->plt.offset != (bfd_vma) -1)
3379 || ((r_type == R_SH_GOT32
3380 || r_type == R_SH_GOT_LOW16
3381 || r_type == R_SH_GOT_MEDLOW16
3382 || r_type == R_SH_GOT_MEDHI16
3383 || r_type == R_SH_GOT_HI16)
3384 && WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h)
3385 && (! info->shared
3386 || (! info->symbolic && h->dynindx != -1)
3387 || !h->def_regular))
3388 /* The cases above are those in which relocation is
3389 overwritten in the switch block below. The cases
3390 below are those in which we must defer relocation
3391