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, 2009, 2010 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 "dwarf2.h" 31 #include "libiberty.h" 32 #include "../opcodes/sh-opc.h" 33 34 static bfd_reloc_status_type sh_elf_reloc 35 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **); 36 static bfd_reloc_status_type sh_elf_ignore_reloc 37 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **); 38 static bfd_boolean sh_elf_relax_delete_bytes 39 (bfd *, asection *, bfd_vma, int); 40 static bfd_boolean sh_elf_align_loads 41 (bfd *, asection *, Elf_Internal_Rela *, bfd_byte *, bfd_boolean *); 42 #ifndef SH64_ELF 43 static bfd_boolean sh_elf_swap_insns 44 (bfd *, asection *, void *, bfd_byte *, bfd_vma); 45 #endif 46 static int sh_elf_optimized_tls_reloc 47 (struct bfd_link_info *, int, int); 48 static bfd_vma dtpoff_base 49 (struct bfd_link_info *); 50 static bfd_vma tpoff 51 (struct bfd_link_info *, bfd_vma); 52 53 /* The name of the dynamic interpreter. This is put in the .interp 54 section. */ 55 56 #define ELF_DYNAMIC_INTERPRETER "/usr/lib/libc.so.1" 57 58 /* FDPIC binaries have a default 128K stack. */ 59 #define DEFAULT_STACK_SIZE 0x20000 60 61 #define MINUS_ONE ((bfd_vma) 0 - 1) 62 63 /* Decide whether a reference to a symbol can be resolved locally or 64 not. If the symbol is protected, we want the local address, but 65 its function descriptor must be assigned by the dynamic linker. */ 66 #define SYMBOL_FUNCDESC_LOCAL(INFO, H) \ 67 (SYMBOL_REFERENCES_LOCAL (INFO, H) \ 68 || ! elf_hash_table (INFO)->dynamic_sections_created) 69 70 #define SH_PARTIAL32 TRUE 71 #define SH_SRC_MASK32 0xffffffff 72 #define SH_ELF_RELOC sh_elf_reloc 73 static reloc_howto_type sh_elf_howto_table[] = 74 { 75 #include "elf32-sh-relocs.h" 76 }; 77 78 #define SH_PARTIAL32 FALSE 79 #define SH_SRC_MASK32 0 80 #define SH_ELF_RELOC bfd_elf_generic_reloc 81 static reloc_howto_type sh_vxworks_howto_table[] = 82 { 83 #include "elf32-sh-relocs.h" 84 }; 85 86 /* Return true if OUTPUT_BFD is a VxWorks object. */ 87 88 static bfd_boolean 89 vxworks_object_p (bfd *abfd ATTRIBUTE_UNUSED) 90 { 91 #if !defined INCLUDE_SHMEDIA && !defined SH_TARGET_ALREADY_DEFINED 92 extern const bfd_target bfd_elf32_shlvxworks_vec; 93 extern const bfd_target bfd_elf32_shvxworks_vec; 94 95 return (abfd->xvec == &bfd_elf32_shlvxworks_vec 96 || abfd->xvec == &bfd_elf32_shvxworks_vec); 97 #else 98 return FALSE; 99 #endif 100 } 101 102 /* Return true if OUTPUT_BFD is an FDPIC object. */ 103 104 static bfd_boolean 105 fdpic_object_p (bfd *abfd ATTRIBUTE_UNUSED) 106 { 107 #if !defined INCLUDE_SHMEDIA && !defined SH_TARGET_ALREADY_DEFINED 108 extern const bfd_target bfd_elf32_shfd_vec; 109 extern const bfd_target bfd_elf32_shbfd_vec; 110 111 return (abfd->xvec == &bfd_elf32_shfd_vec 112 || abfd->xvec == &bfd_elf32_shbfd_vec); 113 #else 114 return FALSE; 115 #endif 116 } 117 118 /* Return the howto table for ABFD. */ 119 120 static reloc_howto_type * 121 get_howto_table (bfd *abfd) 122 { 123 if (vxworks_object_p (abfd)) 124 return sh_vxworks_howto_table; 125 return sh_elf_howto_table; 126 } 127 128 static bfd_reloc_status_type 129 sh_elf_reloc_loop (int r_type ATTRIBUTE_UNUSED, bfd *input_bfd, 130 asection *input_section, bfd_byte *contents, 131 bfd_vma addr, asection *symbol_section, 132 bfd_vma start, bfd_vma end) 133 { 134 static bfd_vma last_addr; 135 static asection *last_symbol_section; 136 bfd_byte *start_ptr, *ptr, *last_ptr; 137 int diff, cum_diff; 138 bfd_signed_vma x; 139 int insn; 140 141 /* Sanity check the address. */ 142 if (addr > bfd_get_section_limit (input_bfd, input_section)) 143 return bfd_reloc_outofrange; 144 145 /* We require the start and end relocations to be processed consecutively - 146 although we allow then to be processed forwards or backwards. */ 147 if (! last_addr) 148 { 149 last_addr = addr; 150 last_symbol_section = symbol_section; 151 return bfd_reloc_ok; 152 } 153 if (last_addr != addr) 154 abort (); 155 last_addr = 0; 156 157 if (! symbol_section || last_symbol_section != symbol_section || end < start) 158 return bfd_reloc_outofrange; 159 160 /* Get the symbol_section contents. */ 161 if (symbol_section != input_section) 162 { 163 if (elf_section_data (symbol_section)->this_hdr.contents != NULL) 164 contents = elf_section_data (symbol_section)->this_hdr.contents; 165 else 166 { 167 if (!bfd_malloc_and_get_section (input_bfd, symbol_section, 168 &contents)) 169 { 170 if (contents != NULL) 171 free (contents); 172 return bfd_reloc_outofrange; 173 } 174 } 175 } 176 #define IS_PPI(PTR) ((bfd_get_16 (input_bfd, (PTR)) & 0xfc00) == 0xf800) 177 start_ptr = contents + start; 178 for (cum_diff = -6, ptr = contents + end; cum_diff < 0 && ptr > start_ptr;) 179 { 180 for (last_ptr = ptr, ptr -= 4; ptr >= start_ptr && IS_PPI (ptr);) 181 ptr -= 2; 182 ptr += 2; 183 diff = (last_ptr - ptr) >> 1; 184 cum_diff += diff & 1; 185 cum_diff += diff; 186 } 187 /* Calculate the start / end values to load into rs / re minus four - 188 so that will cancel out the four we would otherwise have to add to 189 addr to get the value to subtract in order to get relative addressing. */ 190 if (cum_diff >= 0) 191 { 192 start -= 4; 193 end = (ptr + cum_diff * 2) - contents; 194 } 195 else 196 { 197 bfd_vma start0 = start - 4; 198 199 while (start0 && IS_PPI (contents + start0)) 200 start0 -= 2; 201 start0 = start - 2 - ((start - start0) & 2); 202 start = start0 - cum_diff - 2; 203 end = start0; 204 } 205 206 if (contents != NULL 207 && elf_section_data (symbol_section)->this_hdr.contents != contents) 208 free (contents); 209 210 insn = bfd_get_16 (input_bfd, contents + addr); 211 212 x = (insn & 0x200 ? end : start) - addr; 213 if (input_section != symbol_section) 214 x += ((symbol_section->output_section->vma + symbol_section->output_offset) 215 - (input_section->output_section->vma 216 + input_section->output_offset)); 217 x >>= 1; 218 if (x < -128 || x > 127) 219 return bfd_reloc_overflow; 220 221 x = (insn & ~0xff) | (x & 0xff); 222 bfd_put_16 (input_bfd, (bfd_vma) x, contents + addr); 223 224 return bfd_reloc_ok; 225 } 226 227 /* This function is used for normal relocs. This used to be like the COFF 228 function, and is almost certainly incorrect for other ELF targets. */ 229 230 static bfd_reloc_status_type 231 sh_elf_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol_in, 232 void *data, asection *input_section, bfd *output_bfd, 233 char **error_message ATTRIBUTE_UNUSED) 234 { 235 unsigned long insn; 236 bfd_vma sym_value; 237 enum elf_sh_reloc_type r_type; 238 bfd_vma addr = reloc_entry->address; 239 bfd_byte *hit_data = addr + (bfd_byte *) data; 240 241 r_type = (enum elf_sh_reloc_type) reloc_entry->howto->type; 242 243 if (output_bfd != NULL) 244 { 245 /* Partial linking--do nothing. */ 246 reloc_entry->address += input_section->output_offset; 247 return bfd_reloc_ok; 248 } 249 250 /* Almost all relocs have to do with relaxing. If any work must be 251 done for them, it has been done in sh_relax_section. */ 252 if (r_type == R_SH_IND12W && (symbol_in->flags & BSF_LOCAL) != 0) 253 return bfd_reloc_ok; 254 255 if (symbol_in != NULL 256 && bfd_is_und_section (symbol_in->section)) 257 return bfd_reloc_undefined; 258 259 if (bfd_is_com_section (symbol_in->section)) 260 sym_value = 0; 261 else 262 sym_value = (symbol_in->value + 263 symbol_in->section->output_section->vma + 264 symbol_in->section->output_offset); 265 266 switch (r_type) 267 { 268 case R_SH_DIR32: 269 insn = bfd_get_32 (abfd, hit_data); 270 insn += sym_value + reloc_entry->addend; 271 bfd_put_32 (abfd, (bfd_vma) insn, hit_data); 272 break; 273 case R_SH_IND12W: 274 insn = bfd_get_16 (abfd, hit_data); 275 sym_value += reloc_entry->addend; 276 sym_value -= (input_section->output_section->vma 277 + input_section->output_offset 278 + addr 279 + 4); 280 sym_value += (insn & 0xfff) << 1; 281 if (insn & 0x800) 282 sym_value -= 0x1000; 283 insn = (insn & 0xf000) | (sym_value & 0xfff); 284 bfd_put_16 (abfd, (bfd_vma) insn, hit_data); 285 if (sym_value < (bfd_vma) -0x1000 || sym_value >= 0x1000) 286 return bfd_reloc_overflow; 287 break; 288 default: 289 abort (); 290 break; 291 } 292 293 return bfd_reloc_ok; 294 } 295 296 /* This function is used for relocs which are only used for relaxing, 297 which the linker should otherwise ignore. */ 298 299 static bfd_reloc_status_type 300 sh_elf_ignore_reloc (bfd *abfd ATTRIBUTE_UNUSED, arelent *reloc_entry, 301 asymbol *symbol ATTRIBUTE_UNUSED, 302 void *data ATTRIBUTE_UNUSED, asection *input_section, 303 bfd *output_bfd, 304 char **error_message ATTRIBUTE_UNUSED) 305 { 306 if (output_bfd != NULL) 307 reloc_entry->address += input_section->output_offset; 308 return bfd_reloc_ok; 309 } 310 311 /* This structure is used to map BFD reloc codes to SH ELF relocs. */ 312 313 struct elf_reloc_map 314 { 315 bfd_reloc_code_real_type bfd_reloc_val; 316 unsigned char elf_reloc_val; 317 }; 318 319 /* An array mapping BFD reloc codes to SH ELF relocs. */ 320 321 static const struct elf_reloc_map sh_reloc_map[] = 322 { 323 { BFD_RELOC_NONE, R_SH_NONE }, 324 { BFD_RELOC_32, R_SH_DIR32 }, 325 { BFD_RELOC_16, R_SH_DIR16 }, 326 { BFD_RELOC_8, R_SH_DIR8 }, 327 { BFD_RELOC_CTOR, R_SH_DIR32 }, 328 { BFD_RELOC_32_PCREL, R_SH_REL32 }, 329 { BFD_RELOC_SH_PCDISP8BY2, R_SH_DIR8WPN }, 330 { BFD_RELOC_SH_PCDISP12BY2, R_SH_IND12W }, 331 { BFD_RELOC_SH_PCRELIMM8BY2, R_SH_DIR8WPZ }, 332 { BFD_RELOC_SH_PCRELIMM8BY4, R_SH_DIR8WPL }, 333 { BFD_RELOC_8_PCREL, R_SH_SWITCH8 }, 334 { BFD_RELOC_SH_SWITCH16, R_SH_SWITCH16 }, 335 { BFD_RELOC_SH_SWITCH32, R_SH_SWITCH32 }, 336 { BFD_RELOC_SH_USES, R_SH_USES }, 337 { BFD_RELOC_SH_COUNT, R_SH_COUNT }, 338 { BFD_RELOC_SH_ALIGN, R_SH_ALIGN }, 339 { BFD_RELOC_SH_CODE, R_SH_CODE }, 340 { BFD_RELOC_SH_DATA, R_SH_DATA }, 341 { BFD_RELOC_SH_LABEL, R_SH_LABEL }, 342 { BFD_RELOC_VTABLE_INHERIT, R_SH_GNU_VTINHERIT }, 343 { BFD_RELOC_VTABLE_ENTRY, R_SH_GNU_VTENTRY }, 344 { BFD_RELOC_SH_LOOP_START, R_SH_LOOP_START }, 345 { BFD_RELOC_SH_LOOP_END, R_SH_LOOP_END }, 346 { BFD_RELOC_SH_TLS_GD_32, R_SH_TLS_GD_32 }, 347 { BFD_RELOC_SH_TLS_LD_32, R_SH_TLS_LD_32 }, 348 { BFD_RELOC_SH_TLS_LDO_32, R_SH_TLS_LDO_32 }, 349 { BFD_RELOC_SH_TLS_IE_32, R_SH_TLS_IE_32 }, 350 { BFD_RELOC_SH_TLS_LE_32, R_SH_TLS_LE_32 }, 351 { BFD_RELOC_SH_TLS_DTPMOD32, R_SH_TLS_DTPMOD32 }, 352 { BFD_RELOC_SH_TLS_DTPOFF32, R_SH_TLS_DTPOFF32 }, 353 { BFD_RELOC_SH_TLS_TPOFF32, R_SH_TLS_TPOFF32 }, 354 { BFD_RELOC_32_GOT_PCREL, R_SH_GOT32 }, 355 { BFD_RELOC_32_PLT_PCREL, R_SH_PLT32 }, 356 { BFD_RELOC_SH_COPY, R_SH_COPY }, 357 { BFD_RELOC_SH_GLOB_DAT, R_SH_GLOB_DAT }, 358 { BFD_RELOC_SH_JMP_SLOT, R_SH_JMP_SLOT }, 359 { BFD_RELOC_SH_RELATIVE, R_SH_RELATIVE }, 360 { BFD_RELOC_32_GOTOFF, R_SH_GOTOFF }, 361 { BFD_RELOC_SH_GOTPC, R_SH_GOTPC }, 362 { BFD_RELOC_SH_GOTPLT32, R_SH_GOTPLT32 }, 363 { BFD_RELOC_SH_GOT20, R_SH_GOT20 }, 364 { BFD_RELOC_SH_GOTOFF20, R_SH_GOTOFF20 }, 365 { BFD_RELOC_SH_GOTFUNCDESC, R_SH_GOTFUNCDESC }, 366 { BFD_RELOC_SH_GOTFUNCDESC20, R_SH_GOTFUNCDESC20 }, 367 { BFD_RELOC_SH_GOTOFFFUNCDESC, R_SH_GOTOFFFUNCDESC }, 368 { BFD_RELOC_SH_GOTOFFFUNCDESC20, R_SH_GOTOFFFUNCDESC20 }, 369 { BFD_RELOC_SH_FUNCDESC, R_SH_FUNCDESC }, 370 #ifdef INCLUDE_SHMEDIA 371 { BFD_RELOC_SH_GOT_LOW16, R_SH_GOT_LOW16 }, 372 { BFD_RELOC_SH_GOT_MEDLOW16, R_SH_GOT_MEDLOW16 }, 373 { BFD_RELOC_SH_GOT_MEDHI16, R_SH_GOT_MEDHI16 }, 374 { BFD_RELOC_SH_GOT_HI16, R_SH_GOT_HI16 }, 375 { BFD_RELOC_SH_GOTPLT_LOW16, R_SH_GOTPLT_LOW16 }, 376 { BFD_RELOC_SH_GOTPLT_MEDLOW16, R_SH_GOTPLT_MEDLOW16 }, 377 { BFD_RELOC_SH_GOTPLT_MEDHI16, R_SH_GOTPLT_MEDHI16 }, 378 { BFD_RELOC_SH_GOTPLT_HI16, R_SH_GOTPLT_HI16 }, 379 { BFD_RELOC_SH_PLT_LOW16, R_SH_PLT_LOW16 }, 380 { BFD_RELOC_SH_PLT_MEDLOW16, R_SH_PLT_MEDLOW16 }, 381 { BFD_RELOC_SH_PLT_MEDHI16, R_SH_PLT_MEDHI16 }, 382 { BFD_RELOC_SH_PLT_HI16, R_SH_PLT_HI16 }, 383 { BFD_RELOC_SH_GOTOFF_LOW16, R_SH_GOTOFF_LOW16 }, 384 { BFD_RELOC_SH_GOTOFF_MEDLOW16, R_SH_GOTOFF_MEDLOW16 }, 385 { BFD_RELOC_SH_GOTOFF_MEDHI16, R_SH_GOTOFF_MEDHI16 }, 386 { BFD_RELOC_SH_GOTOFF_HI16, R_SH_GOTOFF_HI16 }, 387 { BFD_RELOC_SH_GOTPC_LOW16, R_SH_GOTPC_LOW16 }, 388 { BFD_RELOC_SH_GOTPC_MEDLOW16, R_SH_GOTPC_MEDLOW16 }, 389 { BFD_RELOC_SH_GOTPC_MEDHI16, R_SH_GOTPC_MEDHI16 }, 390 { BFD_RELOC_SH_GOTPC_HI16, R_SH_GOTPC_HI16 }, 391 { BFD_RELOC_SH_COPY64, R_SH_COPY64 }, 392 { BFD_RELOC_SH_GLOB_DAT64, R_SH_GLOB_DAT64 }, 393 { BFD_RELOC_SH_JMP_SLOT64, R_SH_JMP_SLOT64 }, 394 { BFD_RELOC_SH_RELATIVE64, R_SH_RELATIVE64 }, 395 { BFD_RELOC_SH_GOT10BY4, R_SH_GOT10BY4 }, 396 { BFD_RELOC_SH_GOT10BY8, R_SH_GOT10BY8 }, 397 { BFD_RELOC_SH_GOTPLT10BY4, R_SH_GOTPLT10BY4 }, 398 { BFD_RELOC_SH_GOTPLT10BY8, R_SH_GOTPLT10BY8 }, 399 { BFD_RELOC_SH_PT_16, R_SH_PT_16 }, 400 { BFD_RELOC_SH_SHMEDIA_CODE, R_SH_SHMEDIA_CODE }, 401 { BFD_RELOC_SH_IMMU5, R_SH_DIR5U }, 402 { BFD_RELOC_SH_IMMS6, R_SH_DIR6S }, 403 { BFD_RELOC_SH_IMMU6, R_SH_DIR6U }, 404 { BFD_RELOC_SH_IMMS10, R_SH_DIR10S }, 405 { BFD_RELOC_SH_IMMS10BY2, R_SH_DIR10SW }, 406 { BFD_RELOC_SH_IMMS10BY4, R_SH_DIR10SL }, 407 { BFD_RELOC_SH_IMMS10BY8, R_SH_DIR10SQ }, 408 { BFD_RELOC_SH_IMMS16, R_SH_IMMS16 }, 409 { BFD_RELOC_SH_IMMU16, R_SH_IMMU16 }, 410 { BFD_RELOC_SH_IMM_LOW16, R_SH_IMM_LOW16 }, 411 { BFD_RELOC_SH_IMM_LOW16_PCREL, R_SH_IMM_LOW16_PCREL }, 412 { BFD_RELOC_SH_IMM_MEDLOW16, R_SH_IMM_MEDLOW16 }, 413 { BFD_RELOC_SH_IMM_MEDLOW16_PCREL, R_SH_IMM_MEDLOW16_PCREL }, 414 { BFD_RELOC_SH_IMM_MEDHI16, R_SH_IMM_MEDHI16 }, 415 { BFD_RELOC_SH_IMM_MEDHI16_PCREL, R_SH_IMM_MEDHI16_PCREL }, 416 { BFD_RELOC_SH_IMM_HI16, R_SH_IMM_HI16 }, 417 { BFD_RELOC_SH_IMM_HI16_PCREL, R_SH_IMM_HI16_PCREL }, 418 { BFD_RELOC_64, R_SH_64 }, 419 { BFD_RELOC_64_PCREL, R_SH_64_PCREL }, 420 #endif /* not INCLUDE_SHMEDIA */ 421 }; 422 423 /* Given a BFD reloc code, return the howto structure for the 424 corresponding SH ELF reloc. */ 425 426 static reloc_howto_type * 427 sh_elf_reloc_type_lookup (bfd *abfd, bfd_reloc_code_real_type code) 428 { 429 unsigned int i; 430 431 for (i = 0; i < sizeof (sh_reloc_map) / sizeof (struct elf_reloc_map); i++) 432 { 433 if (sh_reloc_map[i].bfd_reloc_val == code) 434 return get_howto_table (abfd) + (int) sh_reloc_map[i].elf_reloc_val; 435 } 436 437 return NULL; 438 } 439 440 static reloc_howto_type * 441 sh_elf_reloc_name_lookup (bfd *abfd, const char *r_name) 442 { 443 unsigned int i; 444 445 if (vxworks_object_p (abfd)) 446 { 447 for (i = 0; 448 i < (sizeof (sh_vxworks_howto_table) 449 / sizeof (sh_vxworks_howto_table[0])); 450 i++) 451 if (sh_vxworks_howto_table[i].name != NULL 452 && strcasecmp (sh_vxworks_howto_table[i].name, r_name) == 0) 453 return &sh_vxworks_howto_table[i]; 454 } 455 else 456 { 457 for (i = 0; 458 i < (sizeof (sh_elf_howto_table) 459 / sizeof (sh_elf_howto_table[0])); 460 i++) 461 if (sh_elf_howto_table[i].name != NULL 462 && strcasecmp (sh_elf_howto_table[i].name, r_name) == 0) 463 return &sh_elf_howto_table[i]; 464 } 465 466 return NULL; 467 } 468 469 /* Given an ELF reloc, fill in the howto field of a relent. */ 470 471 static void 472 sh_elf_info_to_howto (bfd *abfd, arelent *cache_ptr, Elf_Internal_Rela *dst) 473 { 474 unsigned int r; 475 476 r = ELF32_R_TYPE (dst->r_info); 477 478 BFD_ASSERT (r < (unsigned int) R_SH_max); 479 BFD_ASSERT (r < R_SH_FIRST_INVALID_RELOC || r > R_SH_LAST_INVALID_RELOC); 480 BFD_ASSERT (r < R_SH_FIRST_INVALID_RELOC_2 || r > R_SH_LAST_INVALID_RELOC_2); 481 BFD_ASSERT (r < R_SH_FIRST_INVALID_RELOC_3 || r > R_SH_LAST_INVALID_RELOC_3); 482 BFD_ASSERT (r < R_SH_FIRST_INVALID_RELOC_4 || r > R_SH_LAST_INVALID_RELOC_4); 483 BFD_ASSERT (r < R_SH_FIRST_INVALID_RELOC_5 || r > R_SH_LAST_INVALID_RELOC_5); 484 BFD_ASSERT (r < R_SH_FIRST_INVALID_RELOC_6 || r > R_SH_LAST_INVALID_RELOC_6); 485 486 cache_ptr->howto = get_howto_table (abfd) + r; 487 } 488 489 /* This function handles relaxing for SH ELF. See the corresponding 490 function in coff-sh.c for a description of what this does. FIXME: 491 There is a lot of duplication here between this code and the COFF 492 specific code. The format of relocs and symbols is wound deeply 493 into this code, but it would still be better if the duplication 494 could be eliminated somehow. Note in particular that although both 495 functions use symbols like R_SH_CODE, those symbols have different 496 values; in coff-sh.c they come from include/coff/sh.h, whereas here 497 they come from enum elf_sh_reloc_type in include/elf/sh.h. */ 498 499 static bfd_boolean 500 sh_elf_relax_section (bfd *abfd, asection *sec, 501 struct bfd_link_info *link_info, bfd_boolean *again) 502 { 503 Elf_Internal_Shdr *symtab_hdr; 504 Elf_Internal_Rela *internal_relocs; 505 bfd_boolean have_code; 506 Elf_Internal_Rela *irel, *irelend; 507 bfd_byte *contents = NULL; 508 Elf_Internal_Sym *isymbuf = NULL; 509 510 *again = FALSE; 511 512 if (link_info->relocatable 513 || (sec->flags & SEC_RELOC) == 0 514 || sec->reloc_count == 0) 515 return TRUE; 516 517 #ifdef INCLUDE_SHMEDIA 518 if (elf_section_data (sec)->this_hdr.sh_flags 519 & (SHF_SH5_ISA32 | SHF_SH5_ISA32_MIXED)) 520 { 521 return TRUE; 522 } 523 #endif 524 525 symtab_hdr = &elf_symtab_hdr (abfd); 526 527 internal_relocs = (_bfd_elf_link_read_relocs 528 (abfd, sec, NULL, (Elf_Internal_Rela *) NULL, 529 link_info->keep_memory)); 530 if (internal_relocs == NULL) 531 goto error_return; 532 533 have_code = FALSE; 534 535 irelend = internal_relocs + sec->reloc_count; 536 for (irel = internal_relocs; irel < irelend; irel++) 537 { 538 bfd_vma laddr, paddr, symval; 539 unsigned short insn; 540 Elf_Internal_Rela *irelfn, *irelscan, *irelcount; 541 bfd_signed_vma foff; 542 543 if (ELF32_R_TYPE (irel->r_info) == (int) R_SH_CODE) 544 have_code = TRUE; 545 546 if (ELF32_R_TYPE (irel->r_info) != (int) R_SH_USES) 547 continue; 548 549 /* Get the section contents. */ 550 if (contents == NULL) 551 { 552 if (elf_section_data (sec)->this_hdr.contents != NULL) 553 contents = elf_section_data (sec)->this_hdr.contents; 554 else 555 { 556 if (!bfd_malloc_and_get_section (abfd, sec, &contents)) 557 goto error_return; 558 } 559 } 560 561 /* The r_addend field of the R_SH_USES reloc will point us to 562 the register load. The 4 is because the r_addend field is 563 computed as though it were a jump offset, which are based 564 from 4 bytes after the jump instruction. */ 565 laddr = irel->r_offset + 4 + irel->r_addend; 566 if (laddr >= sec->size) 567 { 568 (*_bfd_error_handler) (_("%B: 0x%lx: warning: bad R_SH_USES offset"), 569 abfd, 570 (unsigned long) irel->r_offset); 571 continue; 572 } 573 insn = bfd_get_16 (abfd, contents + laddr); 574 575 /* If the instruction is not mov.l NN,rN, we don't know what to 576 do. */ 577 if ((insn & 0xf000) != 0xd000) 578 { 579 ((*_bfd_error_handler) 580 (_("%B: 0x%lx: warning: R_SH_USES points to unrecognized insn 0x%x"), 581 abfd, (unsigned long) irel->r_offset, insn)); 582 continue; 583 } 584 585 /* Get the address from which the register is being loaded. The 586 displacement in the mov.l instruction is quadrupled. It is a 587 displacement from four bytes after the movl instruction, but, 588 before adding in the PC address, two least significant bits 589 of the PC are cleared. We assume that the section is aligned 590 on a four byte boundary. */ 591 paddr = insn & 0xff; 592 paddr *= 4; 593 paddr += (laddr + 4) &~ (bfd_vma) 3; 594 if (paddr >= sec->size) 595 { 596 ((*_bfd_error_handler) 597 (_("%B: 0x%lx: warning: bad R_SH_USES load offset"), 598 abfd, (unsigned long) irel->r_offset)); 599 continue; 600 } 601 602 /* Get the reloc for the address from which the register is 603 being loaded. This reloc will tell us which function is 604 actually being called. */ 605 for (irelfn = internal_relocs; irelfn < irelend; irelfn++) 606 if (irelfn->r_offset == paddr 607 && ELF32_R_TYPE (irelfn->r_info) == (int) R_SH_DIR32) 608 break; 609 if (irelfn >= irelend) 610 { 611 ((*_bfd_error_handler) 612 (_("%B: 0x%lx: warning: could not find expected reloc"), 613 abfd, (unsigned long) paddr)); 614 continue; 615 } 616 617 /* Read this BFD's symbols if we haven't done so already. */ 618 if (isymbuf == NULL && symtab_hdr->sh_info != 0) 619 { 620 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents; 621 if (isymbuf == NULL) 622 isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr, 623 symtab_hdr->sh_info, 0, 624 NULL, NULL, NULL); 625 if (isymbuf == NULL) 626 goto error_return; 627 } 628 629 /* Get the value of the symbol referred to by the reloc. */ 630 if (ELF32_R_SYM (irelfn->r_info) < symtab_hdr->sh_info) 631 { 632 /* A local symbol. */ 633 Elf_Internal_Sym *isym; 634 635 isym = isymbuf + ELF32_R_SYM (irelfn->r_info); 636 if (isym->st_shndx 637 != (unsigned int) _bfd_elf_section_from_bfd_section (abfd, sec)) 638 { 639 ((*_bfd_error_handler) 640 (_("%B: 0x%lx: warning: symbol in unexpected section"), 641 abfd, (unsigned long) paddr)); 642 continue; 643 } 644 645 symval = (isym->st_value 646 + sec->output_section->vma 647 + sec->output_offset); 648 } 649 else 650 { 651 unsigned long indx; 652 struct elf_link_hash_entry *h; 653 654 indx = ELF32_R_SYM (irelfn->r_info) - symtab_hdr->sh_info; 655 h = elf_sym_hashes (abfd)[indx]; 656 BFD_ASSERT (h != NULL); 657 if (h->root.type != bfd_link_hash_defined 658 && h->root.type != bfd_link_hash_defweak) 659 { 660 /* This appears to be a reference to an undefined 661 symbol. Just ignore it--it will be caught by the 662 regular reloc processing. */ 663 continue; 664 } 665 666 symval = (h->root.u.def.value 667 + h->root.u.def.section->output_section->vma 668 + h->root.u.def.section->output_offset); 669 } 670 671 if (get_howto_table (abfd)[R_SH_DIR32].partial_inplace) 672 symval += bfd_get_32 (abfd, contents + paddr); 673 else 674 symval += irelfn->r_addend; 675 676 /* See if this function call can be shortened. */ 677 foff = (symval 678 - (irel->r_offset 679 + sec->output_section->vma 680 + sec->output_offset 681 + 4)); 682 /* A branch to an address beyond ours might be increased by an 683 .align that doesn't move when bytes behind us are deleted. 684 So, we add some slop in this calculation to allow for 685 that. */ 686 if (foff < -0x1000 || foff >= 0x1000 - 8) 687 { 688 /* After all that work, we can't shorten this function call. */ 689 continue; 690 } 691 692 /* Shorten the function call. */ 693 694 /* For simplicity of coding, we are going to modify the section 695 contents, the section relocs, and the BFD symbol table. We 696 must tell the rest of the code not to free up this 697 information. It would be possible to instead create a table 698 of changes which have to be made, as is done in coff-mips.c; 699 that would be more work, but would require less memory when 700 the linker is run. */ 701 702 elf_section_data (sec)->relocs = internal_relocs; 703 elf_section_data (sec)->this_hdr.contents = contents; 704 symtab_hdr->contents = (unsigned char *) isymbuf; 705 706 /* Replace the jsr with a bsr. */ 707 708 /* Change the R_SH_USES reloc into an R_SH_IND12W reloc, and 709 replace the jsr with a bsr. */ 710 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irelfn->r_info), R_SH_IND12W); 711 /* We used to test (ELF32_R_SYM (irelfn->r_info) < symtab_hdr->sh_info) 712 here, but that only checks if the symbol is an external symbol, 713 not if the symbol is in a different section. Besides, we need 714 a consistent meaning for the relocation, so we just assume here that 715 the value of the symbol is not available. */ 716 717 /* We can't fully resolve this yet, because the external 718 symbol value may be changed by future relaxing. We let 719 the final link phase handle it. */ 720 bfd_put_16 (abfd, (bfd_vma) 0xb000, contents + irel->r_offset); 721 722 irel->r_addend = -4; 723 724 /* When we calculated the symbol "value" we had an offset in the 725 DIR32's word in memory (we read and add it above). However, 726 the jsr we create does NOT have this offset encoded, so we 727 have to add it to the addend to preserve it. */ 728 irel->r_addend += bfd_get_32 (abfd, contents + paddr); 729 730 /* See if there is another R_SH_USES reloc referring to the same 731 register load. */ 732 for (irelscan = internal_relocs; irelscan < irelend; irelscan++) 733 if (ELF32_R_TYPE (irelscan->r_info) == (int) R_SH_USES 734 && laddr == irelscan->r_offset + 4 + irelscan->r_addend) 735 break; 736 if (irelscan < irelend) 737 { 738 /* Some other function call depends upon this register load, 739 and we have not yet converted that function call. 740 Indeed, we may never be able to convert it. There is 741 nothing else we can do at this point. */ 742 continue; 743 } 744 745 /* Look for a R_SH_COUNT reloc on the location where the 746 function address is stored. Do this before deleting any 747 bytes, to avoid confusion about the address. */ 748 for (irelcount = internal_relocs; irelcount < irelend; irelcount++) 749 if (irelcount->r_offset == paddr 750 && ELF32_R_TYPE (irelcount->r_info) == (int) R_SH_COUNT) 751 break; 752 753 /* Delete the register load. */ 754 if (! sh_elf_relax_delete_bytes (abfd, sec, laddr, 2)) 755 goto error_return; 756 757 /* That will change things, so, just in case it permits some 758 other function call to come within range, we should relax 759 again. Note that this is not required, and it may be slow. */ 760 *again = TRUE; 761 762 /* Now check whether we got a COUNT reloc. */ 763 if (irelcount >= irelend) 764 { 765 ((*_bfd_error_handler) 766 (_("%B: 0x%lx: warning: could not find expected COUNT reloc"), 767 abfd, (unsigned long) paddr)); 768 continue; 769 } 770 771 /* The number of uses is stored in the r_addend field. We've 772 just deleted one. */ 773 if (irelcount->r_addend == 0) 774 { 775 ((*_bfd_error_handler) (_("%B: 0x%lx: warning: bad count"), 776 abfd, 777 (unsigned long) paddr)); 778 continue; 779 } 780 781 --irelcount->r_addend; 782 783 /* If there are no more uses, we can delete the address. Reload 784 the address from irelfn, in case it was changed by the 785 previous call to sh_elf_relax_delete_bytes. */ 786 if (irelcount->r_addend == 0) 787 { 788 if (! sh_elf_relax_delete_bytes (abfd, sec, irelfn->r_offset, 4)) 789 goto error_return; 790 } 791 792 /* We've done all we can with that function call. */ 793 } 794 795 /* Look for load and store instructions that we can align on four 796 byte boundaries. */ 797 if ((elf_elfheader (abfd)->e_flags & EF_SH_MACH_MASK) != EF_SH4 798 && have_code) 799 { 800 bfd_boolean swapped; 801 802 /* Get the section contents. */ 803 if (contents == NULL) 804 { 805 if (elf_section_data (sec)->this_hdr.contents != NULL) 806 contents = elf_section_data (sec)->this_hdr.contents; 807 else 808 { 809 if (!bfd_malloc_and_get_section (abfd, sec, &contents)) 810 goto error_return; 811 } 812 } 813 814 if (! sh_elf_align_loads (abfd, sec, internal_relocs, contents, 815 &swapped)) 816 goto error_return; 817 818 if (swapped) 819 { 820 elf_section_data (sec)->relocs = internal_relocs; 821 elf_section_data (sec)->this_hdr.contents = contents; 822 symtab_hdr->contents = (unsigned char *) isymbuf; 823 } 824 } 825 826 if (isymbuf != NULL 827 && symtab_hdr->contents != (unsigned char *) isymbuf) 828 { 829 if (! link_info->keep_memory) 830 free (isymbuf); 831 else 832 { 833 /* Cache the symbols for elf_link_input_bfd. */ 834 symtab_hdr->contents = (unsigned char *) isymbuf; 835 } 836 } 837 838 if (contents != NULL 839 && elf_section_data (sec)->this_hdr.contents != contents) 840 { 841 if (! link_info->keep_memory) 842 free (contents); 843 else 844 { 845 /* Cache the section contents for elf_link_input_bfd. */ 846 elf_section_data (sec)->this_hdr.contents = contents; 847 } 848 } 849 850 if (internal_relocs != NULL 851 && elf_section_data (sec)->relocs != internal_relocs) 852 free (internal_relocs); 853 854 return TRUE; 855 856 error_return: 857 if (isymbuf != NULL 858 && symtab_hdr->contents != (unsigned char *) isymbuf) 859 free (isymbuf); 860 if (contents != NULL 861 && elf_section_data (sec)->this_hdr.contents != contents) 862 free (contents); 863 if (internal_relocs != NULL 864 && elf_section_data (sec)->relocs != internal_relocs) 865 free (internal_relocs); 866 867 return FALSE; 868 } 869 870 /* Delete some bytes from a section while relaxing. FIXME: There is a 871 lot of duplication between this function and sh_relax_delete_bytes 872 in coff-sh.c. */ 873 874 static bfd_boolean 875 sh_elf_relax_delete_bytes (bfd *abfd, asection *sec, bfd_vma addr, 876 int count) 877 { 878 Elf_Internal_Shdr *symtab_hdr; 879 unsigned int sec_shndx; 880 bfd_byte *contents; 881 Elf_Internal_Rela *irel, *irelend; 882 Elf_Internal_Rela *irelalign; 883 bfd_vma toaddr; 884 Elf_Internal_Sym *isymbuf, *isym, *isymend; 885 struct elf_link_hash_entry **sym_hashes; 886 struct elf_link_hash_entry **end_hashes; 887 unsigned int symcount; 888 asection *o; 889 890 symtab_hdr = &elf_symtab_hdr (abfd); 891 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents; 892 893 sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec); 894 895 contents = elf_section_data (sec)->this_hdr.contents; 896 897 /* The deletion must stop at the next ALIGN reloc for an aligment 898 power larger than the number of bytes we are deleting. */ 899 900 irelalign = NULL; 901 toaddr = sec->size; 902 903 irel = elf_section_data (sec)->relocs; 904 irelend = irel + sec->reloc_count; 905 for (; irel < irelend; irel++) 906 { 907 if (ELF32_R_TYPE (irel->r_info) == (int) R_SH_ALIGN 908 && irel->r_offset > addr 909 && count < (1 << irel->r_addend)) 910 { 911 irelalign = irel; 912 toaddr = irel->r_offset; 913 break; 914 } 915 } 916 917 /* Actually delete the bytes. */ 918 memmove (contents + addr, contents + addr + count, 919 (size_t) (toaddr - addr - count)); 920 if (irelalign == NULL) 921 sec->size -= count; 922 else 923 { 924 int i; 925 926 #define NOP_OPCODE (0x0009) 927 928 BFD_ASSERT ((count & 1) == 0); 929 for (i = 0; i < count; i += 2) 930 bfd_put_16 (abfd, (bfd_vma) NOP_OPCODE, contents + toaddr - count + i); 931 } 932 933 /* Adjust all the relocs. */ 934 for (irel = elf_section_data (sec)->relocs; irel < irelend; irel++) 935 { 936 bfd_vma nraddr, stop; 937 bfd_vma start = 0; 938 int insn = 0; 939 int off, adjust, oinsn; 940 bfd_signed_vma voff = 0; 941 bfd_boolean overflow; 942 943 /* Get the new reloc address. */ 944 nraddr = irel->r_offset; 945 if ((irel->r_offset > addr 946 && irel->r_offset < toaddr) 947 || (ELF32_R_TYPE (irel->r_info) == (int) R_SH_ALIGN 948 && irel->r_offset == toaddr)) 949 nraddr -= count; 950 951 /* See if this reloc was for the bytes we have deleted, in which 952 case we no longer care about it. Don't delete relocs which 953 represent addresses, though. */ 954 if (irel->r_offset >= addr 955 && irel->r_offset < addr + count 956 && ELF32_R_TYPE (irel->r_info) != (int) R_SH_ALIGN 957 && ELF32_R_TYPE (irel->r_info) != (int) R_SH_CODE 958 && ELF32_R_TYPE (irel->r_info) != (int) R_SH_DATA 959 && ELF32_R_TYPE (irel->r_info) != (int) R_SH_LABEL) 960 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), 961 (int) R_SH_NONE); 962 963 /* If this is a PC relative reloc, see if the range it covers 964 includes the bytes we have deleted. */ 965 switch ((enum elf_sh_reloc_type) ELF32_R_TYPE (irel->r_info)) 966 { 967 default: 968 break; 969 970 case R_SH_DIR8WPN: 971 case R_SH_IND12W: 972 case R_SH_DIR8WPZ: 973 case R_SH_DIR8WPL: 974 start = irel->r_offset; 975 insn = bfd_get_16 (abfd, contents + nraddr); 976 break; 977 } 978 979 switch ((enum elf_sh_reloc_type) ELF32_R_TYPE (irel->r_info)) 980 { 981 default: 982 start = stop = addr; 983 break; 984 985 case R_SH_DIR32: 986 /* If this reloc is against a symbol defined in this 987 section, and the symbol will not be adjusted below, we 988 must check the addend to see it will put the value in 989 range to be adjusted, and hence must be changed. */ 990 if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info) 991 { 992 isym = isymbuf + ELF32_R_SYM (irel->r_info); 993 if (isym->st_shndx == sec_shndx 994 && (isym->st_value <= addr 995 || isym->st_value >= toaddr)) 996 { 997 bfd_vma val; 998 999 if (get_howto_table (abfd)[R_SH_DIR32].partial_inplace) 1000 { 1001 val = bfd_get_32 (abfd, contents + nraddr); 1002 val += isym->st_value; 1003 if (val > addr && val < toaddr) 1004 bfd_put_32 (abfd, val - count, contents + nraddr); 1005 } 1006 else 1007 { 1008 val = isym->st_value + irel->r_addend; 1009 if (val > addr && val < toaddr) 1010 irel->r_addend -= count; 1011 } 1012 } 1013 } 1014 start = stop = addr; 1015 break; 1016 1017 case R_SH_DIR8WPN: 1018 off = insn & 0xff; 1019 if (off & 0x80) 1020 off -= 0x100; 1021 stop = (bfd_vma) ((bfd_signed_vma) start + 4 + off * 2); 1022 break; 1023 1024 case R_SH_IND12W: 1025 off = insn & 0xfff; 1026 if (! off) 1027 { 1028 /* This has been made by previous relaxation. Since the 1029 relocation will be against an external symbol, the 1030 final relocation will just do the right thing. */ 1031 start = stop = addr; 1032 } 1033 else 1034 { 1035 if (off & 0x800) 1036 off -= 0x1000; 1037 stop = (bfd_vma) ((bfd_signed_vma) start + 4 + off * 2); 1038 1039 /* The addend will be against the section symbol, thus 1040 for adjusting the addend, the relevant start is the 1041 start of the section. 1042 N.B. If we want to abandon in-place changes here and 1043 test directly using symbol + addend, we have to take into 1044 account that the addend has already been adjusted by -4. */ 1045 if (stop > addr && stop < toaddr) 1046 irel->r_addend -= count; 1047 } 1048 break; 1049 1050 case R_SH_DIR8WPZ: 1051 off = insn & 0xff; 1052 stop = start + 4 + off * 2; 1053 break; 1054 1055 case R_SH_DIR8WPL: 1056 off = insn & 0xff; 1057 stop = (start & ~(bfd_vma) 3) + 4 + off * 4; 1058 break; 1059 1060 case R_SH_SWITCH8: 1061 case R_SH_SWITCH16: 1062 case R_SH_SWITCH32: 1063 /* These relocs types represent 1064 .word L2-L1 1065 The r_addend field holds the difference between the reloc 1066 address and L1. That is the start of the reloc, and 1067 adding in the contents gives us the top. We must adjust 1068 both the r_offset field and the section contents. 1069 N.B. in gas / coff bfd, the elf bfd r_addend is called r_offset, 1070 and the elf bfd r_offset is called r_vaddr. */ 1071 1072 stop = irel->r_offset; 1073 start = (bfd_vma) ((bfd_signed_vma) stop - (long) irel->r_addend); 1074 1075 if (start > addr 1076 && start < toaddr 1077 && (stop <= addr || stop >= toaddr)) 1078 irel->r_addend += count; 1079 else if (stop > addr 1080 && stop < toaddr 1081 && (start <= addr || start >= toaddr)) 1082 irel->r_addend -= count; 1083 1084 if (ELF32_R_TYPE (irel->r_info) == (int) R_SH_SWITCH16) 1085 voff = bfd_get_signed_16 (abfd, contents + nraddr); 1086 else if (ELF32_R_TYPE (irel->r_info) == (int) R_SH_SWITCH8) 1087 voff = bfd_get_8 (abfd, contents + nraddr); 1088 else 1089 voff = bfd_get_signed_32 (abfd, contents + nraddr); 1090 stop = (bfd_vma) ((bfd_signed_vma) start + voff); 1091 1092 break; 1093 1094 case R_SH_USES: 1095 start = irel->r_offset; 1096 stop = (bfd_vma) ((bfd_signed_vma) start 1097 + (long) irel->r_addend 1098 + 4); 1099 break; 1100 } 1101 1102 if (start > addr 1103 && start < toaddr 1104 && (stop <= addr || stop >= toaddr)) 1105 adjust = count; 1106 else if (stop > addr 1107 && stop < toaddr 1108 && (start <= addr || start >= toaddr)) 1109 adjust = - count; 1110 else 1111 adjust = 0; 1112 1113 if (adjust != 0) 1114 { 1115 oinsn = insn; 1116 overflow = FALSE; 1117 switch ((enum elf_sh_reloc_type) ELF32_R_TYPE (irel->r_info)) 1118 { 1119 default: 1120 abort (); 1121 break; 1122 1123 case R_SH_DIR8WPN: 1124 case R_SH_DIR8WPZ: 1125 insn += adjust / 2; 1126 if ((oinsn & 0xff00) != (insn & 0xff00)) 1127 overflow = TRUE; 1128 bfd_put_16 (abfd, (bfd_vma) insn, contents + nraddr); 1129 break; 1130 1131 case R_SH_IND12W: 1132 insn += adjust / 2; 1133 if ((oinsn & 0xf000) != (insn & 0xf000)) 1134 overflow = TRUE; 1135 bfd_put_16 (abfd, (bfd_vma) insn, contents + nraddr); 1136 break; 1137 1138 case R_SH_DIR8WPL: 1139 BFD_ASSERT (adjust == count || count >= 4); 1140 if (count >= 4) 1141 insn += adjust / 4; 1142 else 1143 { 1144 if ((irel->r_offset & 3) == 0) 1145 ++insn; 1146 } 1147 if ((oinsn & 0xff00) != (insn & 0xff00)) 1148 overflow = TRUE; 1149 bfd_put_16 (abfd, (bfd_vma) insn, contents + nraddr); 1150 break; 1151 1152 case R_SH_SWITCH8: 1153 voff += adjust; 1154 if (voff < 0 || voff >= 0xff) 1155 overflow = TRUE; 1156 bfd_put_8 (abfd, voff, contents + nraddr); 1157 break; 1158 1159 case R_SH_SWITCH16: 1160 voff += adjust; 1161 if (voff < - 0x8000 || voff >= 0x8000) 1162 overflow = TRUE; 1163 bfd_put_signed_16 (abfd, (bfd_vma) voff, contents + nraddr); 1164 break; 1165 1166 case R_SH_SWITCH32: 1167 voff += adjust; 1168 bfd_put_signed_32 (abfd, (bfd_vma) voff, contents + nraddr); 1169 break; 1170 1171 case R_SH_USES: 1172 irel->r_addend += adjust; 1173 break; 1174 } 1175 1176 if (overflow) 1177 { 1178 ((*_bfd_error_handler) 1179 (_("%B: 0x%lx: fatal: reloc overflow while relaxing"), 1180 abfd, (unsigned long) irel->r_offset)); 1181 bfd_set_error (bfd_error_bad_value); 1182 return FALSE; 1183 } 1184 } 1185 1186 irel->r_offset = nraddr; 1187 } 1188 1189 /* Look through all the other sections. If there contain any IMM32 1190 relocs against internal symbols which we are not going to adjust 1191 below, we may need to adjust the addends. */ 1192 for (o = abfd->sections; o != NULL; o = o->next) 1193 { 1194 Elf_Internal_Rela *internal_relocs; 1195 Elf_Internal_Rela *irelscan, *irelscanend; 1196 bfd_byte *ocontents; 1197 1198 if (o == sec 1199 || (o->flags & SEC_RELOC) == 0 1200 || o->reloc_count == 0) 1201 continue; 1202 1203 /* We always cache the relocs. Perhaps, if info->keep_memory is 1204 FALSE, we should free them, if we are permitted to, when we 1205 leave sh_coff_relax_section. */ 1206 internal_relocs = (_bfd_elf_link_read_relocs 1207 (abfd, o, NULL, (Elf_Internal_Rela *) NULL, TRUE)); 1208 if (internal_relocs == NULL) 1209 return FALSE; 1210 1211 ocontents = NULL; 1212 irelscanend = internal_relocs + o->reloc_count; 1213 for (irelscan = internal_relocs; irelscan < irelscanend; irelscan++) 1214 { 1215 /* Dwarf line numbers use R_SH_SWITCH32 relocs. */ 1216 if (ELF32_R_TYPE (irelscan->r_info) == (int) R_SH_SWITCH32) 1217 { 1218 bfd_vma start, stop; 1219 bfd_signed_vma voff; 1220 1221 if (ocontents == NULL) 1222 { 1223 if (elf_section_data (o)->this_hdr.contents != NULL) 1224 ocontents = elf_section_data (o)->this_hdr.contents; 1225 else 1226 { 1227 /* We always cache the section contents. 1228 Perhaps, if info->keep_memory is FALSE, we 1229 should free them, if we are permitted to, 1230 when we leave sh_coff_relax_section. */ 1231 if (!bfd_malloc_and_get_section (abfd, o, &ocontents)) 1232 { 1233 if (ocontents != NULL) 1234 free (ocontents); 1235 return FALSE; 1236 } 1237 1238 elf_section_data (o)->this_hdr.contents = ocontents; 1239 } 1240 } 1241 1242 stop = irelscan->r_offset; 1243 start 1244 = (bfd_vma) ((bfd_signed_vma) stop - (long) irelscan->r_addend); 1245 1246 /* STOP is in a different section, so it won't change. */ 1247 if (start > addr && start < toaddr) 1248 irelscan->r_addend += count; 1249 1250 voff = bfd_get_signed_32 (abfd, ocontents + irelscan->r_offset); 1251 stop = (bfd_vma) ((bfd_signed_vma) start + voff); 1252 1253 if (start > addr 1254 && start < toaddr 1255 && (stop <= addr || stop >= toaddr)) 1256 bfd_put_signed_32 (abfd, (bfd_vma) voff + count, 1257 ocontents + irelscan->r_offset); 1258 else if (stop > addr 1259 && stop < toaddr 1260 && (start <= addr || start >= toaddr)) 1261 bfd_put_signed_32 (abfd, (bfd_vma) voff - count, 1262 ocontents + irelscan->r_offset); 1263 } 1264 1265 if (ELF32_R_TYPE (irelscan->r_info) != (int) R_SH_DIR32) 1266 continue; 1267 1268 if (ELF32_R_SYM (irelscan->r_info) >= symtab_hdr->sh_info) 1269 continue; 1270 1271 1272 isym = isymbuf + ELF32_R_SYM (irelscan->r_info); 1273 if (isym->st_shndx == sec_shndx 1274 && (isym->st_value <= addr 1275 || isym->st_value >= toaddr)) 1276 { 1277 bfd_vma val; 1278 1279 if (ocontents == NULL) 1280 { 1281 if (elf_section_data (o)->this_hdr.contents != NULL) 1282 ocontents = elf_section_data (o)->this_hdr.contents; 1283 else 1284 { 1285 /* We always cache the section contents. 1286 Perhaps, if info->keep_memory is FALSE, we 1287 should free them, if we are permitted to, 1288 when we leave sh_coff_relax_section. */ 1289 if (!bfd_malloc_and_get_section (abfd, o, &ocontents)) 1290 { 1291 if (ocontents != NULL) 1292 free (ocontents); 1293 return FALSE; 1294 } 1295 1296 elf_section_data (o)->this_hdr.contents = ocontents; 1297 } 1298 } 1299 1300 val = bfd_get_32 (abfd, ocontents + irelscan->r_offset); 1301 val += isym->st_value; 1302 if (val > addr && val < toaddr) 1303 bfd_put_32 (abfd, val - count, 1304 ocontents + irelscan->r_offset); 1305 } 1306 } 1307 } 1308 1309 /* Adjust the local symbols defined in this section. */ 1310 isymend = isymbuf + symtab_hdr->sh_info; 1311 for (isym = isymbuf; isym < isymend; isym++) 1312 { 1313 if (isym->st_shndx == sec_shndx 1314 && isym->st_value > addr 1315 && isym->st_value < toaddr) 1316 isym->st_value -= count; 1317 } 1318 1319 /* Now adjust the global symbols defined in this section. */ 1320 symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym) 1321 - symtab_hdr->sh_info); 1322 sym_hashes = elf_sym_hashes (abfd); 1323 end_hashes = sym_hashes + symcount; 1324 for (; sym_hashes < end_hashes; sym_hashes++) 1325 { 1326 struct elf_link_hash_entry *sym_hash = *sym_hashes; 1327 if ((sym_hash->root.type == bfd_link_hash_defined 1328 || sym_hash->root.type == bfd_link_hash_defweak) 1329 && sym_hash->root.u.def.section == sec 1330 && sym_hash->root.u.def.value > addr 1331 && sym_hash->root.u.def.value < toaddr) 1332 { 1333 sym_hash->root.u.def.value -= count; 1334 } 1335 } 1336 1337 /* See if we can move the ALIGN reloc forward. We have adjusted 1338 r_offset for it already. */ 1339 if (irelalign != NULL) 1340 { 1341 bfd_vma alignto, alignaddr; 1342 1343 alignto = BFD_ALIGN (toaddr, 1 << irelalign->r_addend); 1344 alignaddr = BFD_ALIGN (irelalign->r_offset, 1345 1 << irelalign->r_addend); 1346 if (alignto != alignaddr) 1347 { 1348 /* Tail recursion. */ 1349 return sh_elf_relax_delete_bytes (abfd, sec, alignaddr, 1350 (int) (alignto - alignaddr)); 1351 } 1352 } 1353 1354 return TRUE; 1355 } 1356 1357 /* Look for loads and stores which we can align to four byte 1358 boundaries. This is like sh_align_loads in coff-sh.c. */ 1359 1360 static bfd_boolean 1361 sh_elf_align_loads (bfd *abfd ATTRIBUTE_UNUSED, asection *sec, 1362 Elf_Internal_Rela *internal_relocs, 1363 bfd_byte *contents ATTRIBUTE_UNUSED, 1364 bfd_boolean *pswapped) 1365 { 1366 Elf_Internal_Rela *irel, *irelend; 1367 bfd_vma *labels = NULL; 1368 bfd_vma *label, *label_end; 1369 bfd_size_type amt; 1370 1371 *pswapped = FALSE; 1372 1373 irelend = internal_relocs + sec->reloc_count; 1374 1375 /* Get all the addresses with labels on them. */ 1376 amt = sec->reloc_count; 1377 amt *= sizeof (bfd_vma); 1378 labels = (bfd_vma *) bfd_malloc (amt); 1379 if (labels == NULL) 1380 goto error_return; 1381 label_end = labels; 1382 for (irel = internal_relocs; irel < irelend; irel++) 1383 { 1384 if (ELF32_R_TYPE (irel->r_info) == (int) R_SH_LABEL) 1385 { 1386 *label_end = irel->r_offset; 1387 ++label_end; 1388 } 1389 } 1390 1391 /* Note that the assembler currently always outputs relocs in 1392 address order. If that ever changes, this code will need to sort 1393 the label values and the relocs. */ 1394 1395 label = labels; 1396 1397 for (irel = internal_relocs; irel < irelend; irel++) 1398 { 1399 bfd_vma start, stop; 1400 1401 if (ELF32_R_TYPE (irel->r_info) != (int) R_SH_CODE) 1402 continue; 1403 1404 start = irel->r_offset; 1405 1406 for (irel++; irel < irelend; irel++) 1407 if (ELF32_R_TYPE (irel->r_info) == (int) R_SH_DATA) 1408 break; 1409 if (irel < irelend) 1410 stop = irel->r_offset; 1411 else 1412 stop = sec->size; 1413 1414 if (! _bfd_sh_align_load_span (abfd, sec, contents, sh_elf_swap_insns, 1415 internal_relocs, &label, 1416 label_end, start, stop, pswapped)) 1417 goto error_return; 1418 } 1419 1420 free (labels); 1421 1422 return TRUE; 1423 1424 error_return: 1425 if (labels != NULL) 1426 free (labels); 1427 return FALSE; 1428 } 1429 1430 #ifndef SH64_ELF 1431 /* Swap two SH instructions. This is like sh_swap_insns in coff-sh.c. */ 1432 1433 static bfd_boolean 1434 sh_elf_swap_insns (bfd *abfd, asection *sec, void *relocs, 1435 bfd_byte *contents, bfd_vma addr) 1436 { 1437 Elf_Internal_Rela *internal_relocs = (Elf_Internal_Rela *) relocs; 1438 unsigned short i1, i2; 1439 Elf_Internal_Rela *irel, *irelend; 1440 1441 /* Swap the instructions themselves. */ 1442 i1 = bfd_get_16 (abfd, contents + addr); 1443 i2 = bfd_get_16 (abfd, contents + addr + 2); 1444 bfd_put_16 (abfd, (bfd_vma) i2, contents + addr); 1445 bfd_put_16 (abfd, (bfd_vma) i1, contents + addr + 2); 1446 1447 /* Adjust all reloc addresses. */ 1448 irelend = internal_relocs + sec->reloc_count; 1449 for (irel = internal_relocs; irel < irelend; irel++) 1450 { 1451 enum elf_sh_reloc_type type; 1452 int add; 1453 1454 /* There are a few special types of relocs that we don't want to 1455 adjust. These relocs do not apply to the instruction itself, 1456 but are only associated with the address. */ 1457 type = (enum elf_sh_reloc_type) ELF32_R_TYPE (irel->r_info); 1458 if (type == R_SH_ALIGN 1459 || type == R_SH_CODE 1460 || type == R_SH_DATA 1461 || type == R_SH_LABEL) 1462 continue; 1463 1464 /* If an R_SH_USES reloc points to one of the addresses being 1465 swapped, we must adjust it. It would be incorrect to do this 1466 for a jump, though, since we want to execute both 1467 instructions after the jump. (We have avoided swapping 1468 around a label, so the jump will not wind up executing an 1469 instruction it shouldn't). */ 1470 if (type == R_SH_USES) 1471 { 1472 bfd_vma off; 1473 1474 off = irel->r_offset + 4 + irel->r_addend; 1475 if (off == addr) 1476 irel->r_offset += 2; 1477 else if (off == addr + 2) 1478 irel->r_offset -= 2; 1479 } 1480 1481 if (irel->r_offset == addr) 1482 { 1483 irel->r_offset += 2; 1484 add = -2; 1485 } 1486 else if (irel->r_offset == addr + 2) 1487 { 1488 irel->r_offset -= 2; 1489 add = 2; 1490 } 1491 else 1492 add = 0; 1493 1494 if (add != 0) 1495 { 1496 bfd_byte *loc; 1497 unsigned short insn, oinsn; 1498 bfd_boolean overflow; 1499 1500 loc = contents + irel->r_offset; 1501 overflow = FALSE; 1502 switch (type) 1503 { 1504 default: 1505 break; 1506 1507 case R_SH_DIR8WPN: 1508 case R_SH_DIR8WPZ: 1509 insn = bfd_get_16 (abfd, loc); 1510 oinsn = insn; 1511 insn += add / 2; 1512 if ((oinsn & 0xff00) != (insn & 0xff00)) 1513 overflow = TRUE; 1514 bfd_put_16 (abfd, (bfd_vma) insn, loc); 1515 break; 1516 1517 case R_SH_IND12W: 1518 insn = bfd_get_16 (abfd, loc); 1519 oinsn = insn; 1520 insn += add / 2; 1521 if ((oinsn & 0xf000) != (insn & 0xf000)) 1522 overflow = TRUE; 1523 bfd_put_16 (abfd, (bfd_vma) insn, loc); 1524 break; 1525 1526 case R_SH_DIR8WPL: 1527 /* This reloc ignores the least significant 3 bits of 1528 the program counter before adding in the offset. 1529 This means that if ADDR is at an even address, the 1530 swap will not affect the offset. If ADDR is an at an 1531 odd address, then the instruction will be crossing a 1532 four byte boundary, and must be adjusted. */ 1533 if ((addr & 3) != 0) 1534 { 1535 insn = bfd_get_16 (abfd, loc); 1536 oinsn = insn; 1537 insn += add / 2; 1538 if ((oinsn & 0xff00) != (insn & 0xff00)) 1539 overflow = TRUE; 1540 bfd_put_16 (abfd, (bfd_vma) insn, loc); 1541 } 1542 1543 break; 1544 } 1545 1546 if (overflow) 1547 { 1548 ((*_bfd_error_handler) 1549 (_("%B: 0x%lx: fatal: reloc overflow while relaxing"), 1550 abfd, (unsigned long) irel->r_offset)); 1551 bfd_set_error (bfd_error_bad_value); 1552 return FALSE; 1553 } 1554 } 1555 } 1556 1557 return TRUE; 1558 } 1559 #endif /* defined SH64_ELF */ 1560 1561 /* Describes one of the various PLT styles. */ 1562 1563 struct elf_sh_plt_info 1564 { 1565 /* The template for the first PLT entry, or NULL if there is no special 1566 first entry. */ 1567 const bfd_byte *plt0_entry; 1568 1569 /* The size of PLT0_ENTRY in bytes, or 0 if PLT0_ENTRY is NULL. */ 1570 bfd_vma plt0_entry_size; 1571 1572 /* Index I is the offset into PLT0_ENTRY of a pointer to 1573 _GLOBAL_OFFSET_TABLE_ + I * 4. The value is MINUS_ONE 1574 if there is no such pointer. */ 1575 bfd_vma plt0_got_fields[3]; 1576 1577 /* The template for a symbol's PLT entry. */ 1578 const bfd_byte *symbol_entry; 1579 1580 /* The size of SYMBOL_ENTRY in bytes. */ 1581 bfd_vma symbol_entry_size; 1582 1583 /* Byte offsets of fields in SYMBOL_ENTRY. Not all fields are used 1584 on all targets. The comments by each member indicate the value 1585 that the field must hold. */ 1586 struct { 1587 bfd_vma got_entry; /* the address of the symbol's .got.plt entry */ 1588 bfd_vma plt; /* .plt (or a branch to .plt on VxWorks) */ 1589 bfd_vma reloc_offset; /* the offset of the symbol's JMP_SLOT reloc */ 1590 bfd_boolean got20; /* TRUE if got_entry points to a movi20 1591 instruction (instead of a constant pool 1592 entry). */ 1593 } symbol_fields; 1594 1595 /* The offset of the resolver stub from the start of SYMBOL_ENTRY. */ 1596 bfd_vma symbol_resolve_offset; 1597 1598 /* A different PLT layout which can be used for the first 1599 MAX_SHORT_PLT entries. It must share the same plt0. NULL in 1600 other cases. */ 1601 const struct elf_sh_plt_info *short_plt; 1602 }; 1603 1604 #ifdef INCLUDE_SHMEDIA 1605 1606 /* The size in bytes of an entry in the procedure linkage table. */ 1607 1608 #define ELF_PLT_ENTRY_SIZE 64 1609 1610 /* First entry in an absolute procedure linkage table look like this. */ 1611 1612 static const bfd_byte elf_sh_plt0_entry_be[ELF_PLT_ENTRY_SIZE] = 1613 { 1614 0xcc, 0x00, 0x01, 0x10, /* movi .got.plt >> 16, r17 */ 1615 0xc8, 0x00, 0x01, 0x10, /* shori .got.plt & 65535, r17 */ 1616 0x89, 0x10, 0x09, 0x90, /* ld.l r17, 8, r25 */ 1617 0x6b, 0xf1, 0x66, 0x00, /* ptabs r25, tr0 */ 1618 0x89, 0x10, 0x05, 0x10, /* ld.l r17, 4, r17 */ 1619 0x44, 0x01, 0xff, 0xf0, /* blink tr0, r63 */ 1620 0x6f, 0xf0, 0xff, 0xf0, /* nop */ 1621 0x6f, 0xf0, 0xff, 0xf0, /* nop */ 1622 0x6f, 0xf0, 0xff, 0xf0, /* nop */ 1623 0x6f, 0xf0, 0xff, 0xf0, /* nop */ 1624 0x6f, 0xf0, 0xff, 0xf0, /* nop */ 1625 0x6f, 0xf0, 0xff, 0xf0, /* nop */ 1626 0x6f, 0xf0, 0xff, 0xf0, /* nop */ 1627 0x6f, 0xf0, 0xff, 0xf0, /* nop */ 1628 0x6f, 0xf0, 0xff, 0xf0, /* nop */ 1629 0x6f, 0xf0, 0xff, 0xf0, /* nop */ 1630 }; 1631 1632 static const bfd_byte elf_sh_plt0_entry_le[ELF_PLT_ENTRY_SIZE] = 1633 { 1634 0x10, 0x01, 0x00, 0xcc, /* movi .got.plt >> 16, r17 */ 1635 0x10, 0x01, 0x00, 0xc8, /* shori .got.plt & 65535, r17 */ 1636 0x90, 0x09, 0x10, 0x89, /* ld.l r17, 8, r25 */ 1637 0x00, 0x66, 0xf1, 0x6b, /* ptabs r25, tr0 */ 1638 0x10, 0x05, 0x10, 0x89, /* ld.l r17, 4, r17 */ 1639 0xf0, 0xff, 0x01, 0x44, /* blink tr0, r63 */ 1640 0xf0, 0xff, 0xf0, 0x6f, /* nop */ 1641 0xf0, 0xff, 0xf0, 0x6f, /* nop */ 1642 0xf0, 0xff, 0xf0, 0x6f, /* nop */ 1643 0xf0, 0xff, 0xf0, 0x6f, /* nop */ 1644 0xf0, 0xff, 0xf0, 0x6f, /* nop */ 1645 0xf0, 0xff, 0xf0, 0x6f, /* nop */ 1646 0xf0, 0xff, 0xf0, 0x6f, /* nop */ 1647 0xf0, 0xff, 0xf0, 0x6f, /* nop */ 1648 0xf0, 0xff, 0xf0, 0x6f, /* nop */ 1649 0xf0, 0xff, 0xf0, 0x6f, /* nop */ 1650 }; 1651 1652 /* Sebsequent entries in an absolute procedure linkage table look like 1653 this. */ 1654 1655 static const bfd_byte elf_sh_plt_entry_be[ELF_PLT_ENTRY_SIZE] = 1656 { 1657 0xcc, 0x00, 0x01, 0x90, /* movi nameN-in-GOT >> 16, r25 */ 1658 0xc8, 0x00, 0x01, 0x90, /* shori nameN-in-GOT & 65535, r25 */ 1659 0x89, 0x90, 0x01, 0x90, /* ld.l r25, 0, r25 */ 1660 0x6b, 0xf1, 0x66, 0x00, /* ptabs r25, tr0 */ 1661 0x44, 0x01, 0xff, 0xf0, /* blink tr0, r63 */ 1662 0x6f, 0xf0, 0xff, 0xf0, /* nop */ 1663 0x6f, 0xf0, 0xff, 0xf0, /* nop */ 1664 0x6f, 0xf0, 0xff, 0xf0, /* nop */ 1665 0xcc, 0x00, 0x01, 0x90, /* movi .PLT0 >> 16, r25 */ 1666 0xc8, 0x00, 0x01, 0x90, /* shori .PLT0 & 65535, r25 */ 1667 0x6b, 0xf1, 0x66, 0x00, /* ptabs r25, tr0 */ 1668 0xcc, 0x00, 0x01, 0x50, /* movi reloc-offset >> 16, r21 */ 1669 0xc8, 0x00, 0x01, 0x50, /* shori reloc-offset & 65535, r21 */ 1670 0x44, 0x01, 0xff, 0xf0, /* blink tr0, r63 */ 1671 0x6f, 0xf0, 0xff, 0xf0, /* nop */ 1672 0x6f, 0xf0, 0xff, 0xf0, /* nop */ 1673 }; 1674 1675 static const bfd_byte elf_sh_plt_entry_le[ELF_PLT_ENTRY_SIZE] = 1676 { 1677 0x90, 0x01, 0x00, 0xcc, /* movi nameN-in-GOT >> 16, r25 */ 1678 0x90, 0x01, 0x00, 0xc8, /* shori nameN-in-GOT & 65535, r25 */ 1679 0x90, 0x01, 0x90, 0x89, /* ld.l r25, 0, r25 */ 1680 0x00, 0x66, 0xf1, 0x6b, /* ptabs r25, tr0 */ 1681 0xf0, 0xff, 0x01, 0x44, /* blink tr0, r63 */ 1682 0xf0, 0xff, 0xf0, 0x6f, /* nop */ 1683 0xf0, 0xff, 0xf0, 0x6f, /* nop */ 1684 0xf0, 0xff, 0xf0, 0x6f, /* nop */ 1685 0x90, 0x01, 0x00, 0xcc, /* movi .PLT0 >> 16, r25 */ 1686 0x90, 0x01, 0x00, 0xc8, /* shori .PLT0 & 65535, r25 */ 1687 0x00, 0x66, 0xf1, 0x6b, /* ptabs r25, tr0 */ 1688 0x50, 0x01, 0x00, 0xcc, /* movi reloc-offset >> 16, r21 */ 1689 0x50, 0x01, 0x00, 0xc8, /* shori reloc-offset & 65535, r21 */ 1690 0xf0, 0xff, 0x01, 0x44, /* blink tr0, r63 */ 1691 0xf0, 0xff, 0xf0, 0x6f, /* nop */ 1692 0xf0, 0xff, 0xf0, 0x6f, /* nop */ 1693 }; 1694 1695 /* Entries in a PIC procedure linkage table look like this. */ 1696 1697 static const bfd_byte elf_sh_pic_plt_entry_be[ELF_PLT_ENTRY_SIZE] = 1698 { 1699 0xcc, 0x00, 0x01, 0x90, /* movi nameN@GOT >> 16, r25 */ 1700 0xc8, 0x00, 0x01, 0x90, /* shori nameN@GOT & 65535, r25 */ 1701 0x40, 0xc2, 0x65, 0x90, /* ldx.l r12, r25, r25 */ 1702 0x6b, 0xf1, 0x66, 0x00, /* ptabs r25, tr0 */ 1703 0x44, 0x01, 0xff, 0xf0, /* blink tr0, r63 */ 1704 0x6f, 0xf0, 0xff, 0xf0, /* nop */ 1705 0x6f, 0xf0, 0xff, 0xf0, /* nop */ 1706 0x6f, 0xf0, 0xff, 0xf0, /* nop */ 1707 0xce, 0x00, 0x01, 0x10, /* movi -GOT_BIAS, r17 */ 1708 0x00, 0xc8, 0x45, 0x10, /* add.l r12, r17, r17 */ 1709 0x89, 0x10, 0x09, 0x90, /* ld.l r17, 8, r25 */ 1710 0x6b, 0xf1, 0x66, 0x00, /* ptabs r25, tr0 */ 1711 0x89, 0x10, 0x05, 0x10, /* ld.l r17, 4, r17 */ 1712 0xcc, 0x00, 0x01, 0x50, /* movi reloc-offset >> 16, r21 */ 1713 0xc8, 0x00, 0x01, 0x50, /* shori reloc-offset & 65535, r21 */ 1714 0x44, 0x01, 0xff, 0xf0, /* blink tr0, r63 */ 1715 }; 1716 1717 static const bfd_byte elf_sh_pic_plt_entry_le[ELF_PLT_ENTRY_SIZE] = 1718 { 1719 0x90, 0x01, 0x00, 0xcc, /* movi nameN@GOT >> 16, r25 */ 1720 0x90, 0x01, 0x00, 0xc8, /* shori nameN@GOT & 65535, r25 */ 1721 0x90, 0x65, 0xc2, 0x40, /* ldx.l r12, r25, r25 */ 1722 0x00, 0x66, 0xf1, 0x6b, /* ptabs r25, tr0 */ 1723 0xf0, 0xff, 0x01, 0x44, /* blink tr0, r63 */ 1724 0xf0, 0xff, 0xf0, 0x6f, /* nop */ 1725 0xf0, 0xff, 0xf0, 0x6f, /* nop */ 1726 0xf0, 0xff, 0xf0, 0x6f, /* nop */ 1727 0x10, 0x01, 0x00, 0xce, /* movi -GOT_BIAS, r17 */ 1728 0x10, 0x45, 0xc8, 0x00, /* add.l r12, r17, r17 */ 1729 0x90, 0x09, 0x10, 0x89, /* ld.l r17, 8, r25 */ 1730 0x00, 0x66, 0xf1, 0x6b, /* ptabs r25, tr0 */ 1731 0x10, 0x05, 0x10, 0x89, /* ld.l r17, 4, r17 */ 1732 0x50, 0x01, 0x00, 0xcc, /* movi reloc-offset >> 16, r21 */ 1733 0x50, 0x01, 0x00, 0xc8, /* shori reloc-offset & 65535, r21 */ 1734 0xf0, 0xff, 0x01, 0x44, /* blink tr0, r63 */ 1735 }; 1736 1737 static const struct elf_sh_plt_info elf_sh_plts[2][2] = { 1738 { 1739 { 1740 /* Big-endian non-PIC. */ 1741 elf_sh_plt0_entry_be, 1742 ELF_PLT_ENTRY_SIZE, 1743 { 0, MINUS_ONE, MINUS_ONE }, 1744 elf_sh_plt_entry_be, 1745 ELF_PLT_ENTRY_SIZE, 1746 { 0, 32, 48, FALSE }, 1747 33, /* includes ISA encoding */ 1748 NULL 1749 }, 1750 { 1751 /* Little-endian non-PIC. */ 1752 elf_sh_plt0_entry_le, 1753 ELF_PLT_ENTRY_SIZE, 1754 { 0, MINUS_ONE, MINUS_ONE }, 1755 elf_sh_plt_entry_le, 1756 ELF_PLT_ENTRY_SIZE, 1757 { 0, 32, 48, FALSE }, 1758 33, /* includes ISA encoding */ 1759 NULL 1760 }, 1761 }, 1762 { 1763 { 1764 /* Big-endian PIC. */ 1765 elf_sh_plt0_entry_be, 1766 ELF_PLT_ENTRY_SIZE, 1767 { MINUS_ONE, MINUS_ONE, MINUS_ONE }, 1768 elf_sh_pic_plt_entry_be, 1769 ELF_PLT_ENTRY_SIZE, 1770 { 0, MINUS_ONE, 52, FALSE }, 1771 33, /* includes ISA encoding */ 1772 NULL 1773 }, 1774 { 1775 /* Little-endian PIC. */ 1776 elf_sh_plt0_entry_le, 1777 ELF_PLT_ENTRY_SIZE, 1778 { MINUS_ONE, MINUS_ONE, MINUS_ONE }, 1779 elf_sh_pic_plt_entry_le, 1780 ELF_PLT_ENTRY_SIZE, 1781 { 0, MINUS_ONE, 52, FALSE }, 1782 33, /* includes ISA encoding */ 1783 NULL 1784 }, 1785 } 1786 }; 1787 1788 /* Return offset of the linker in PLT0 entry. */ 1789 #define elf_sh_plt0_gotplt_offset(info) 0 1790 1791 /* Install a 32-bit PLT field starting at ADDR, which occurs in OUTPUT_BFD. 1792 VALUE is the field's value and CODE_P is true if VALUE refers to code, 1793 not data. 1794 1795 On SH64, each 32-bit field is loaded by a movi/shori pair. */ 1796 1797 inline static void 1798 install_plt_field (bfd *output_bfd, bfd_boolean code_p, 1799 unsigned long value, bfd_byte *addr) 1800 { 1801 value |= code_p; 1802 bfd_put_32 (output_bfd, 1803 bfd_get_32 (output_bfd, addr) 1804 | ((value >> 6) & 0x3fffc00), 1805 addr); 1806 bfd_put_32 (output_bfd, 1807 bfd_get_32 (output_bfd, addr + 4) 1808 | ((value << 10) & 0x3fffc00), 1809 addr + 4); 1810 } 1811 1812 /* Return the type of PLT associated with ABFD. PIC_P is true if 1813 the object is position-independent. */ 1814 1815 static const struct elf_sh_plt_info * 1816 get_plt_info (bfd *abfd ATTRIBUTE_UNUSED, bfd_boolean pic_p) 1817 { 1818 return &elf_sh_plts[pic_p][!bfd_big_endian (abfd)]; 1819 } 1820 #else 1821 /* The size in bytes of an entry in the procedure linkage table. */ 1822 1823 #define ELF_PLT_ENTRY_SIZE 28 1824 1825 /* First entry in an absolute procedure linkage table look like this. */ 1826 1827 /* Note - this code has been "optimised" not to use r2. r2 is used by 1828 GCC to return the address of large structures, so it should not be 1829 corrupted here. This does mean however, that this PLT does not conform 1830 to the SH PIC ABI. That spec says that r0 contains the type of the PLT 1831 and r2 contains the GOT id. This version stores the GOT id in r0 and 1832 ignores the type. Loaders can easily detect this difference however, 1833 since the type will always be 0 or 8, and the GOT ids will always be 1834 greater than or equal to 12. */ 1835 static const bfd_byte elf_sh_plt0_entry_be[ELF_PLT_ENTRY_SIZE] = 1836 { 1837 0xd0, 0x05, /* mov.l 2f,r0 */ 1838 0x60, 0x02, /* mov.l @r0,r0 */ 1839 0x2f, 0x06, /* mov.l r0,@-r15 */ 1840 0xd0, 0x03, /* mov.l 1f,r0 */ 1841 0x60, 0x02, /* mov.l @r0,r0 */ 1842 0x40, 0x2b, /* jmp @r0 */ 1843 0x60, 0xf6, /* mov.l @r15+,r0 */ 1844 0x00, 0x09, /* nop */ 1845 0x00, 0x09, /* nop */ 1846 0x00, 0x09, /* nop */ 1847 0, 0, 0, 0, /* 1: replaced with address of .got.plt + 8. */ 1848 0, 0, 0, 0, /* 2: replaced with address of .got.plt + 4. */ 1849 }; 1850 1851 static const bfd_byte elf_sh_plt0_entry_le[ELF_PLT_ENTRY_SIZE] = 1852 { 1853 0x05, 0xd0, /* mov.l 2f,r0 */ 1854 0x02, 0x60, /* mov.l @r0,r0 */ 1855 0x06, 0x2f, /* mov.l r0,@-r15 */ 1856 0x03, 0xd0, /* mov.l 1f,r0 */ 1857 0x02, 0x60, /* mov.l @r0,r0 */ 1858 0x2b, 0x40, /* jmp @r0 */ 1859 0xf6, 0x60, /* mov.l @r15+,r0 */ 1860 0x09, 0x00, /* nop */ 1861 0x09, 0x00, /* nop */ 1862 0x09, 0x00, /* nop */ 1863 0, 0, 0, 0, /* 1: replaced with address of .got.plt + 8. */ 1864 0, 0, 0, 0, /* 2: replaced with address of .got.plt + 4. */ 1865 }; 1866 1867 /* Sebsequent entries in an absolute procedure linkage table look like 1868 this. */ 1869 1870 static const bfd_byte elf_sh_plt_entry_be[ELF_PLT_ENTRY_SIZE] = 1871 { 1872 0xd0, 0x04, /* mov.l 1f,r0 */ 1873 0x60, 0x02, /* mov.l @(r0,r12),r0 */ 1874 0xd1, 0x02, /* mov.l 0f,r1 */ 1875 0x40, 0x2b, /* jmp @r0 */ 1876 0x60, 0x13, /* mov r1,r0 */ 1877 0xd1, 0x03, /* mov.l 2f,r1 */ 1878 0x40, 0x2b, /* jmp @r0 */ 1879 0x00, 0x09, /* nop */ 1880 0, 0, 0, 0, /* 0: replaced with address of .PLT0. */ 1881 0, 0, 0, 0, /* 1: replaced with address of this symbol in .got. */ 1882 0, 0, 0, 0, /* 2: replaced with offset into relocation table. */ 1883 }; 1884 1885 static const bfd_byte elf_sh_plt_entry_le[ELF_PLT_ENTRY_SIZE] = 1886 { 1887 0x04, 0xd0, /* mov.l 1f,r0 */ 1888 0x02, 0x60, /* mov.l @r0,r0 */ 1889 0x02, 0xd1, /* mov.l 0f,r1 */ 1890 0x2b, 0x40, /* jmp @r0 */ 1891 0x13, 0x60, /* mov r1,r0 */ 1892 0x03, 0xd1, /* mov.l 2f,r1 */ 1893 0x2b, 0x40, /* jmp @r0 */ 1894 0x09, 0x00, /* nop */ 1895 0, 0, 0, 0, /* 0: replaced with address of .PLT0. */ 1896 0, 0, 0, 0, /* 1: replaced with address of this symbol in .got. */ 1897 0, 0, 0, 0, /* 2: replaced with offset into relocation table. */ 1898 }; 1899 1900 /* Entries in a PIC procedure linkage table look like this. */ 1901 1902 static const bfd_byte elf_sh_pic_plt_entry_be[ELF_PLT_ENTRY_SIZE] = 1903 { 1904 0xd0, 0x04, /* mov.l 1f,r0 */ 1905 0x00, 0xce, /* mov.l @(r0,r12),r0 */ 1906 0x40, 0x2b, /* jmp @r0 */ 1907 0x00, 0x09, /* nop */ 1908 0x50, 0xc2, /* mov.l @(8,r12),r0 */ 1909 0xd1, 0x03, /* mov.l 2f,r1 */ 1910 0x40, 0x2b, /* jmp @r0 */ 1911 0x50, 0xc1, /* mov.l @(4,r12),r0 */ 1912 0x00, 0x09, /* nop */ 1913 0x00, 0x09, /* nop */ 1914 0, 0, 0, 0, /* 1: replaced with address of this symbol in .got. */ 1915 0, 0, 0, 0 /* 2: replaced with offset into relocation table. */ 1916 }; 1917 1918 static const bfd_byte elf_sh_pic_plt_entry_le[ELF_PLT_ENTRY_SIZE] = 1919 { 1920 0x04, 0xd0, /* mov.l 1f,r0 */ 1921 0xce, 0x00, /* mov.l @(r0,r12),r0 */ 1922 0x2b, 0x40, /* jmp @r0 */ 1923 0x09, 0x00, /* nop */ 1924 0xc2, 0x50, /* mov.l @(8,r12),r0 */ 1925 0x03, 0xd1, /* mov.l 2f,r1 */ 1926 0x2b, 0x40, /* jmp @r0 */ 1927 0xc1, 0x50, /* mov.l @(4,r12),r0 */ 1928 0x09, 0x00, /* nop */ 1929 0x09, 0x00, /* nop */ 1930 0, 0, 0, 0, /* 1: replaced with address of this symbol in .got. */ 1931 0, 0, 0, 0 /* 2: replaced with offset into relocation table. */ 1932 }; 1933 1934 static const struct elf_sh_plt_info elf_sh_plts[2][2] = { 1935 { 1936 { 1937 /* Big-endian non-PIC. */ 1938 elf_sh_plt0_entry_be, 1939 ELF_PLT_ENTRY_SIZE, 1940 { MINUS_ONE, 24, 20 }, 1941 elf_sh_plt_entry_be, 1942 ELF_PLT_ENTRY_SIZE, 1943 { 20, 16, 24, FALSE }, 1944 8, 1945 NULL 1946 }, 1947 { 1948 /* Little-endian non-PIC. */ 1949 elf_sh_plt0_entry_le, 1950 ELF_PLT_ENTRY_SIZE, 1951 { MINUS_ONE, 24, 20 }, 1952 elf_sh_plt_entry_le, 1953 ELF_PLT_ENTRY_SIZE, 1954 { 20, 16, 24, FALSE }, 1955 8, 1956 NULL 1957 }, 1958 }, 1959 { 1960 { 1961 /* Big-endian PIC. */ 1962 elf_sh_plt0_entry_be, 1963 ELF_PLT_ENTRY_SIZE, 1964 { MINUS_ONE, MINUS_ONE, MINUS_ONE }, 1965 elf_sh_pic_plt_entry_be, 1966 ELF_PLT_ENTRY_SIZE, 1967 { 20, MINUS_ONE, 24, FALSE }, 1968 8, 1969 NULL 1970 }, 1971 { 1972 /* Little-endian PIC. */ 1973 elf_sh_plt0_entry_le, 1974 ELF_PLT_ENTRY_SIZE, 1975 { MINUS_ONE, MINUS_ONE, MINUS_ONE }, 1976 elf_sh_pic_plt_entry_le, 1977 ELF_PLT_ENTRY_SIZE, 1978 { 20, MINUS_ONE, 24, FALSE }, 1979 8, 1980 NULL 1981 }, 1982 } 1983 }; 1984 1985 #define VXWORKS_PLT_HEADER_SIZE 12 1986 #define VXWORKS_PLT_ENTRY_SIZE 24 1987 1988 static const bfd_byte vxworks_sh_plt0_entry_be[VXWORKS_PLT_HEADER_SIZE] = 1989 { 1990 0xd1, 0x01, /* mov.l @(8,pc),r1 */ 1991 0x61, 0x12, /* mov.l @r1,r1 */ 1992 0x41, 0x2b, /* jmp @r1 */ 1993 0x00, 0x09, /* nop */ 1994 0, 0, 0, 0 /* 0: replaced with _GLOBAL_OFFSET_TABLE+8. */ 1995 }; 1996 1997 static const bfd_byte vxworks_sh_plt0_entry_le[VXWORKS_PLT_HEADER_SIZE] = 1998 { 1999 0x01, 0xd1, /* mov.l @(8,pc),r1 */ 2000 0x12, 0x61, /* mov.l @r1,r1 */ 2001 0x2b, 0x41, /* jmp @r1 */ 2002 0x09, 0x00, /* nop */ 2003 0, 0, 0, 0 /* 0: replaced with _GLOBAL_OFFSET_TABLE+8. */ 2004 }; 2005 2006 static const bfd_byte vxworks_sh_plt_entry_be[VXWORKS_PLT_ENTRY_SIZE] = 2007 { 2008 0xd0, 0x01, /* mov.l @(8,pc),r0 */ 2009 0x60, 0x02, /* mov.l @r0,r0 */ 2010 0x40, 0x2b, /* jmp @r0 */ 2011 0x00, 0x09, /* nop */ 2012 0, 0, 0, 0, /* 0: replaced with address of this symbol in .got. */ 2013 0xd0, 0x01, /* mov.l @(8,pc),r0 */ 2014 0xa0, 0x00, /* bra PLT (We need to fix the offset.) */ 2015 0x00, 0x09, /* nop */ 2016 0x00, 0x09, /* nop */ 2017 0, 0, 0, 0, /* 1: replaced with offset into relocation table. */ 2018 }; 2019 2020 static const bfd_byte vxworks_sh_plt_entry_le[VXWORKS_PLT_ENTRY_SIZE] = 2021 { 2022 0x01, 0xd0, /* mov.l @(8,pc),r0 */ 2023 0x02, 0x60, /* mov.l @r0,r0 */ 2024 0x2b, 0x40, /* jmp @r0 */ 2025 0x09, 0x00, /* nop */ 2026 0, 0, 0, 0, /* 0: replaced with address of this symbol in .got. */ 2027 0x01, 0xd0, /* mov.l @(8,pc),r0 */ 2028 0x00, 0xa0, /* bra PLT (We need to fix the offset.) */ 2029 0x09, 0x00, /* nop */ 2030 0x09, 0x00, /* nop */ 2031 0, 0, 0, 0, /* 1: replaced with offset into relocation table. */ 2032 }; 2033 2034 static const bfd_byte vxworks_sh_pic_plt_entry_be[VXWORKS_PLT_ENTRY_SIZE] = 2035 { 2036 0xd0, 0x01, /* mov.l @(8,pc),r0 */ 2037 0x00, 0xce, /* mov.l @(r0,r12),r0 */ 2038 0x40, 0x2b, /* jmp @r0 */ 2039 0x00, 0x09, /* nop */ 2040 0, 0, 0, 0, /* 0: replaced with offset of this symbol in .got. */ 2041 0xd0, 0x01, /* mov.l @(8,pc),r0 */ 2042 0x51, 0xc2, /* mov.l @(8,r12),r1 */ 2043 0x41, 0x2b, /* jmp @r1 */ 2044 0x00, 0x09, /* nop */ 2045 0, 0, 0, 0, /* 1: replaced with offset into relocation table. */ 2046 }; 2047 2048 static const bfd_byte vxworks_sh_pic_plt_entry_le[VXWORKS_PLT_ENTRY_SIZE] = 2049 { 2050 0x01, 0xd0, /* mov.l @(8,pc),r0 */ 2051 0xce, 0x00, /* mov.l @(r0,r12),r0 */ 2052 0x2b, 0x40, /* jmp @r0 */ 2053 0x09, 0x00, /* nop */ 2054 0, 0, 0, 0, /* 0: replaced with offset of this symbol in .got. */ 2055 0x01, 0xd0, /* mov.l @(8,pc),r0 */ 2056 0xc2, 0x51, /* mov.l @(8,r12),r1 */ 2057 0x2b, 0x41, /* jmp @r1 */ 2058 0x09, 0x00, /* nop */ 2059 0, 0, 0, 0, /* 1: replaced with offset into relocation table. */ 2060 }; 2061 2062 static const struct elf_sh_plt_info vxworks_sh_plts[2][2] = { 2063 { 2064 { 2065 /* Big-endian non-PIC. */ 2066 vxworks_sh_plt0_entry_be, 2067 VXWORKS_PLT_HEADER_SIZE, 2068 { MINUS_ONE, MINUS_ONE, 8 }, 2069 vxworks_sh_plt_entry_be, 2070 VXWORKS_PLT_ENTRY_SIZE, 2071 { 8, 14, 20, FALSE }, 2072 12, 2073 NULL 2074 }, 2075 { 2076 /* Little-endian non-PIC. */ 2077 vxworks_sh_plt0_entry_le, 2078 VXWORKS_PLT_HEADER_SIZE, 2079 { MINUS_ONE, MINUS_ONE, 8 }, 2080 vxworks_sh_plt_entry_le, 2081 VXWORKS_PLT_ENTRY_SIZE, 2082 { 8, 14, 20, FALSE }, 2083 12, 2084 NULL 2085 }, 2086 }, 2087 { 2088 { 2089 /* Big-endian PIC. */ 2090 NULL, 2091 0, 2092 { MINUS_ONE, MINUS_ONE, MINUS_ONE }, 2093 vxworks_sh_pic_plt_entry_be, 2094 VXWORKS_PLT_ENTRY_SIZE, 2095 { 8, MINUS_ONE, 20, FALSE }, 2096 12, 2097 NULL 2098 }, 2099 { 2100 /* Little-endian PIC. */ 2101 NULL, 2102 0, 2103 { MINUS_ONE, MINUS_ONE, MINUS_ONE }, 2104 vxworks_sh_pic_plt_entry_le, 2105 VXWORKS_PLT_ENTRY_SIZE, 2106 { 8, MINUS_ONE, 20, FALSE }, 2107 12, 2108 NULL 2109 }, 2110 } 2111 }; 2112 2113 /* FDPIC PLT entries. Two unimplemented optimizations for lazy 2114 binding are to omit the lazy binding stub when linking with -z now 2115 and to move lazy binding stubs into a separate region for better 2116 cache behavior. */ 2117 2118 #define FDPIC_PLT_ENTRY_SIZE 28 2119 #define FDPIC_PLT_LAZY_OFFSET 20 2120 2121 /* FIXME: The lazy binding stub requires a plt0 - which may need to be 2122 duplicated if it is out of range, or which can be inlined. So 2123 right now it is always inlined, which wastes a word per stub. It 2124 might be easier to handle the duplication if we put the lazy 2125 stubs separately. */ 2126 2127 static const bfd_byte fdpic_sh_plt_entry_be[FDPIC_PLT_ENTRY_SIZE] = 2128 { 2129 0xd0, 0x02, /* mov.l @(12,pc),r0 */ 2130 0x01, 0xce, /* mov.l @(r0,r12),r1 */ 2131 0x70, 0x04, /* add #4, r0 */ 2132 0x41, 0x2b, /* jmp @r1 */ 2133 0x0c, 0xce, /* mov.l @(r0,r12),r12 */ 2134 0x00, 0x09, /* nop */ 2135 0, 0, 0, 0, /* 0: replaced with offset of this symbol's funcdesc */ 2136 0, 0, 0, 0, /* 1: replaced with offset into relocation table. */ 2137 0x60, 0xc2, /* mov.l @r12,r0 */ 2138 0x40, 0x2b, /* jmp @r0 */ 2139 0x53, 0xc1, /* mov.l @(4,r12),r3 */ 2140 0x00, 0x09, /* nop */ 2141 }; 2142 2143 static const bfd_byte fdpic_sh_plt_entry_le[FDPIC_PLT_ENTRY_SIZE] = 2144 { 2145 0x02, 0xd0, /* mov.l @(12,pc),r0 */ 2146 0xce, 0x01, /* mov.l @(r0,r12),r1 */ 2147 0x04, 0x70, /* add #4, r0 */ 2148 0x2b, 0x41, /* jmp @r1 */ 2149 0xce, 0x0c, /* mov.l @(r0,r12),r12 */ 2150 0x09, 0x00, /* nop */ 2151 0, 0, 0, 0, /* 0: replaced with offset of this symbol's funcdesc */ 2152 0, 0, 0, 0, /* 1: replaced with offset into relocation table. */ 2153 0xc2, 0x60, /* mov.l @r12,r0 */ 2154 0x2b, 0x40, /* jmp @r0 */ 2155 0xc1, 0x53, /* mov.l @(4,r12),r3 */ 2156 0x09, 0x00, /* nop */ 2157 }; 2158 2159 static const struct elf_sh_plt_info fdpic_sh_plts[2] = { 2160 { 2161 /* Big-endian PIC. */ 2162 NULL, 2163 0, 2164 { MINUS_ONE, MINUS_ONE, MINUS_ONE }, 2165 fdpic_sh_plt_entry_be, 2166 FDPIC_PLT_ENTRY_SIZE, 2167 { 12, MINUS_ONE, 16, FALSE }, 2168 FDPIC_PLT_LAZY_OFFSET, 2169 NULL 2170 }, 2171 { 2172 /* Little-endian PIC. */ 2173 NULL, 2174 0, 2175 { MINUS_ONE, MINUS_ONE, MINUS_ONE }, 2176 fdpic_sh_plt_entry_le, 2177 FDPIC_PLT_ENTRY_SIZE, 2178 { 12, MINUS_ONE, 16, FALSE }, 2179 FDPIC_PLT_LAZY_OFFSET, 2180 NULL 2181 }, 2182 }; 2183 2184 /* On SH2A, we can use the movi20 instruction to generate shorter PLT 2185 entries for the first 64K slots. We use the normal FDPIC PLT entry 2186 past that point; we could also use movi20s, which might be faster, 2187 but would not be any smaller. */ 2188 2189 #define FDPIC_SH2A_PLT_ENTRY_SIZE 24 2190 #define FDPIC_SH2A_PLT_LAZY_OFFSET 16 2191 2192 static const bfd_byte fdpic_sh2a_plt_entry_be[FDPIC_SH2A_PLT_ENTRY_SIZE] = 2193 { 2194 0, 0, 0, 0, /* movi20 #gotofffuncdesc,r0 */ 2195 0x01, 0xce, /* mov.l @(r0,r12),r1 */ 2196 0x70, 0x04, /* add #4, r0 */ 2197 0x41, 0x2b, /* jmp @r1 */ 2198 0x0c, 0xce, /* mov.l @(r0,r12),r12 */ 2199 0, 0, 0, 0, /* 1: replaced with offset into relocation table. */ 2200 0x60, 0xc2, /* mov.l @r12,r0 */ 2201 0x40, 0x2b, /* jmp @r0 */ 2202 0x53, 0xc1, /* mov.l @(4,r12),r3 */ 2203 0x00, 0x09, /* nop */ 2204 }; 2205 2206 static const bfd_byte fdpic_sh2a_plt_entry_le[FDPIC_SH2A_PLT_ENTRY_SIZE] = 2207 { 2208 0, 0, 0, 0, /* movi20 #gotofffuncdesc,r0 */ 2209 0xce, 0x01, /* mov.l @(r0,r12),r1 */ 2210 0x04, 0x70, /* add #4, r0 */ 2211 0x2b, 0x41, /* jmp @r1 */ 2212 0xce, 0x0c, /* mov.l @(r0,r12),r12 */ 2213 0, 0, 0, 0, /* 1: replaced with offset into relocation table. */ 2214 0xc2, 0x60, /* mov.l @r12,r0 */ 2215 0x2b, 0x40, /* jmp @r0 */ 2216 0xc1, 0x53, /* mov.l @(4,r12),r3 */ 2217 0x09, 0x00, /* nop */ 2218 }; 2219 2220 static const struct elf_sh_plt_info fdpic_sh2a_short_plt_be = { 2221 /* Big-endian FDPIC, max index 64K. */ 2222 NULL, 2223 0, 2224 { MINUS_ONE, MINUS_ONE, MINUS_ONE }, 2225 fdpic_sh2a_plt_entry_be, 2226 FDPIC_SH2A_PLT_ENTRY_SIZE, 2227 { 0, MINUS_ONE, 12, TRUE }, 2228 FDPIC_SH2A_PLT_LAZY_OFFSET, 2229 NULL 2230 }; 2231 2232 static const struct elf_sh_plt_info fdpic_sh2a_short_plt_le = { 2233 /* Little-endian FDPIC, max index 64K. */ 2234 NULL, 2235 0, 2236 { MINUS_ONE, MINUS_ONE, MINUS_ONE }, 2237 fdpic_sh2a_plt_entry_le, 2238 FDPIC_SH2A_PLT_ENTRY_SIZE, 2239 { 0, MINUS_ONE, 12, TRUE }, 2240 FDPIC_SH2A_PLT_LAZY_OFFSET, 2241 NULL 2242 }; 2243 2244 static const struct elf_sh_plt_info fdpic_sh2a_plts[2] = { 2245 { 2246 /* Big-endian PIC. */ 2247 NULL, 2248 0, 2249 { MINUS_ONE, MINUS_ONE, MINUS_ONE }, 2250 fdpic_sh_plt_entry_be, 2251 FDPIC_PLT_ENTRY_SIZE, 2252 { 12, MINUS_ONE, 16, FALSE }, 2253 FDPIC_PLT_LAZY_OFFSET, 2254 &fdpic_sh2a_short_plt_be 2255 }, 2256 { 2257 /* Little-endian PIC. */ 2258 NULL, 2259 0, 2260 { MINUS_ONE, MINUS_ONE, MINUS_ONE }, 2261 fdpic_sh_plt_entry_le, 2262 FDPIC_PLT_ENTRY_SIZE, 2263 { 12, MINUS_ONE, 16, FALSE }, 2264 FDPIC_PLT_LAZY_OFFSET, 2265 &fdpic_sh2a_short_plt_le 2266 }, 2267 }; 2268 2269 /* Return the type of PLT associated with ABFD. PIC_P is true if 2270 the object is position-independent. */ 2271 2272 static const struct elf_sh_plt_info * 2273 get_plt_info (bfd *abfd, bfd_boolean pic_p) 2274 { 2275 if (fdpic_object_p (abfd)) 2276 { 2277 /* If any input file requires SH2A we can use a shorter PLT 2278 sequence. */ 2279 if (sh_get_arch_from_bfd_mach (bfd_get_mach (abfd)) & arch_sh2a_base) 2280 return &fdpic_sh2a_plts[!bfd_big_endian (abfd)]; 2281 else 2282 return &fdpic_sh_plts[!bfd_big_endian (abfd)]; 2283 } 2284 if (vxworks_object_p (abfd)) 2285 return &vxworks_sh_plts[pic_p][!bfd_big_endian (abfd)]; 2286 return &elf_sh_plts[pic_p][!bfd_big_endian (abfd)]; 2287 } 2288 2289 /* Install a 32-bit PLT field starting at ADDR, which occurs in OUTPUT_BFD. 2290 VALUE is the field's value and CODE_P is true if VALUE refers to code, 2291 not data. */ 2292 2293 inline static void 2294 install_plt_field (bfd *output_bfd, bfd_boolean code_p ATTRIBUTE_UNUSED, 2295 unsigned long value, bfd_byte *addr) 2296 { 2297 bfd_put_32 (output_bfd, value, addr); 2298 } 2299 #endif 2300 2301 /* The number of PLT entries which can use a shorter PLT, if any. 2302 Currently always 64K, since only SH-2A FDPIC uses this; a 2303 20-bit movi20 can address that many function descriptors below 2304 _GLOBAL_OFFSET_TABLE_. */ 2305 #define MAX_SHORT_PLT 65536 2306 2307 /* Return the index of the PLT entry at byte offset OFFSET. */ 2308 2309 static bfd_vma 2310 get_plt_index (const struct elf_sh_plt_info *info, bfd_vma offset) 2311 { 2312 bfd_vma plt_index = 0; 2313 2314 offset -= info->plt0_entry_size; 2315 if (info->short_plt != NULL) 2316 { 2317 if (offset > MAX_SHORT_PLT * info->short_plt->symbol_entry_size) 2318 { 2319 plt_index = MAX_SHORT_PLT; 2320 offset -= MAX_SHORT_PLT * info->short_plt->symbol_entry_size; 2321 } 2322 else 2323 info = info->short_plt; 2324 } 2325 return plt_index + offset / info->symbol_entry_size; 2326 } 2327 2328 /* Do the inverse operation. */ 2329 2330 static bfd_vma 2331 get_plt_offset (const struct elf_sh_plt_info *info, bfd_vma plt_index) 2332 { 2333 bfd_vma offset = 0; 2334 2335 if (info->short_plt != NULL) 2336 { 2337 if (plt_index > MAX_SHORT_PLT) 2338 { 2339 offset = MAX_SHORT_PLT * info->short_plt->symbol_entry_size; 2340 plt_index -= MAX_SHORT_PLT; 2341 } 2342 else 2343 info = info->short_plt; 2344 } 2345 return (offset + info->plt0_entry_size 2346 + (plt_index * info->symbol_entry_size)); 2347 } 2348 2349 /* The sh linker needs to keep track of the number of relocs that it 2350 decides to copy as dynamic relocs in check_relocs for each symbol. 2351 This is so that it can later discard them if they are found to be 2352 unnecessary. We store the information in a field extending the 2353 regular ELF linker hash table. */ 2354 2355 struct elf_sh_dyn_relocs 2356 { 2357 struct elf_sh_dyn_relocs *next; 2358 2359 /* The input section of the reloc. */ 2360 asection *sec; 2361 2362 /* Total number of relocs copied for the input section. */ 2363 bfd_size_type count; 2364 2365 /* Number of pc-relative relocs copied for the input section. */ 2366 bfd_size_type pc_count; 2367 }; 2368 2369 union gotref 2370 { 2371 bfd_signed_vma refcount; 2372 bfd_vma offset; 2373 }; 2374 2375 /* sh ELF linker hash entry. */ 2376 2377 struct elf_sh_link_hash_entry 2378 { 2379 struct elf_link_hash_entry root; 2380 2381 #ifdef INCLUDE_SHMEDIA 2382 union 2383 { 2384 bfd_signed_vma refcount; 2385 bfd_vma offset; 2386 } datalabel_got; 2387 #endif 2388 2389 /* Track dynamic relocs copied for this symbol. */ 2390 struct elf_sh_dyn_relocs *dyn_relocs; 2391 2392 bfd_signed_vma gotplt_refcount; 2393 2394 /* A local function descriptor, for FDPIC. The refcount counts 2395 R_SH_FUNCDESC, R_SH_GOTOFFFUNCDESC, and R_SH_GOTOFFFUNCDESC20 2396 relocations; the PLT and GOT entry are accounted 2397 for separately. After adjust_dynamic_symbol, the offset is 2398 MINUS_ONE if there is no local descriptor (dynamic linker 2399 managed and no PLT entry, or undefined weak non-dynamic). 2400 During check_relocs we do not yet know whether the local 2401 descriptor will be canonical. */ 2402 union gotref funcdesc; 2403 2404 /* How many of the above refcounted relocations were R_SH_FUNCDESC, 2405 and thus require fixups or relocations. */ 2406 bfd_signed_vma abs_funcdesc_refcount; 2407 2408 enum { 2409 GOT_UNKNOWN = 0, GOT_NORMAL, GOT_TLS_GD, GOT_TLS_IE, GOT_FUNCDESC 2410 } got_type; 2411 }; 2412 2413 #define sh_elf_hash_entry(ent) ((struct elf_sh_link_hash_entry *)(ent)) 2414 2415 struct sh_elf_obj_tdata 2416 { 2417 struct elf_obj_tdata root; 2418 2419 /* got_type for each local got entry. */ 2420 char *local_got_type; 2421 2422 /* Function descriptor refcount and offset for each local symbol. */ 2423 union gotref *local_funcdesc; 2424 }; 2425 2426 #define sh_elf_tdata(abfd) \ 2427 ((struct sh_elf_obj_tdata *) (abfd)->tdata.any) 2428 2429 #define sh_elf_local_got_type(abfd) \ 2430 (sh_elf_tdata (abfd)->local_got_type) 2431 2432 #define sh_elf_local_funcdesc(abfd) \ 2433 (sh_elf_tdata (abfd)->local_funcdesc) 2434 2435 #define is_sh_elf(bfd) \ 2436 (bfd_get_flavour (bfd) == bfd_target_elf_flavour \ 2437 && elf_tdata (bfd) != NULL \ 2438 && elf_object_id (bfd) == SH_ELF_DATA) 2439 2440 /* Override the generic function because we need to store sh_elf_obj_tdata 2441 as the specific tdata. */ 2442 2443 static bfd_boolean 2444 sh_elf_mkobject (bfd *abfd) 2445 { 2446 return bfd_elf_allocate_object (abfd, sizeof (struct sh_elf_obj_tdata), 2447 SH_ELF_DATA); 2448 } 2449 2450 /* sh ELF linker hash table. */ 2451 2452 struct elf_sh_link_hash_table 2453 { 2454 struct elf_link_hash_table root; 2455 2456 /* Short-cuts to get to dynamic linker sections. */ 2457 asection *sgot; 2458 asection *sgotplt; 2459 asection *srelgot; 2460 asection *splt; 2461 asection *srelplt; 2462 asection *sdynbss; 2463 asection *srelbss; 2464 asection *sfuncdesc; 2465 asection *srelfuncdesc; 2466 asection *srofixup; 2467 2468 /* The (unloaded but important) VxWorks .rela.plt.unloaded section. */ 2469 asection *srelplt2; 2470 2471 /* Small local sym cache. */ 2472 struct sym_cache sym_cache; 2473 2474 /* A counter or offset to track a TLS got entry. */ 2475 union 2476 { 2477 bfd_signed_vma refcount; 2478 bfd_vma offset; 2479 } tls_ldm_got; 2480 2481 /* The type of PLT to use. */ 2482 const struct elf_sh_plt_info *plt_info; 2483 2484 /* True if the target system is VxWorks. */ 2485 bfd_boolean vxworks_p; 2486 2487 /* True if the target system uses FDPIC. */ 2488 bfd_boolean fdpic_p; 2489 }; 2490 2491 /* Traverse an sh ELF linker hash table. */ 2492 2493 #define sh_elf_link_hash_traverse(table, func, info) \ 2494 (elf_link_hash_traverse \ 2495 (&(table)->root, \ 2496 (bfd_boolean (*) (struct elf_link_hash_entry *, void *)) (func), \ 2497 (info))) 2498 2499 /* Get the sh ELF linker hash table from a link_info structure. */ 2500 2501 #define sh_elf_hash_table(p) \ 2502 (elf_hash_table_id ((struct elf_link_hash_table *) ((p)->hash)) \ 2503 == SH_ELF_DATA ? ((struct elf_sh_link_hash_table *) ((p)->hash)) : NULL) 2504 2505 /* Create an entry in an sh ELF linker hash table. */ 2506 2507 static struct bfd_hash_entry * 2508 sh_elf_link_hash_newfunc (struct bfd_hash_entry *entry, 2509 struct bfd_hash_table *table, 2510 const char *string) 2511 { 2512 struct elf_sh_link_hash_entry *ret = 2513 (struct elf_sh_link_hash_entry *) entry; 2514 2515 /* Allocate the structure if it has not already been allocated by a 2516 subclass. */ 2517 if (ret == (struct elf_sh_link_hash_entry *) NULL) 2518 ret = ((struct elf_sh_link_hash_entry *) 2519 bfd_hash_allocate (table, 2520 sizeof (struct elf_sh_link_hash_entry))); 2521 if (ret == (struct elf_sh_link_hash_entry *) NULL) 2522 return (struct bfd_hash_entry *) ret; 2523 2524 /* Call the allocation method of the superclass. */ 2525 ret = ((struct elf_sh_link_hash_entry *) 2526 _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret, 2527 table, string)); 2528 if (ret != (struct elf_sh_link_hash_entry *) NULL) 2529 { 2530 ret->dyn_relocs = NULL; 2531 ret->gotplt_refcount = 0; 2532 #ifdef INCLUDE_SHMEDIA 2533 ret->datalabel_got.refcount = ret->root.got.refcount; 2534 #endif 2535 ret->funcdesc.refcount = 0; 2536 ret->abs_funcdesc_refcount = 0; 2537 ret->got_type = GOT_UNKNOWN; 2538 } 2539 2540 return (struct bfd_hash_entry *) ret; 2541 } 2542 2543 /* Create an sh ELF linker hash table. */ 2544 2545 static struct bfd_link_hash_table * 2546 sh_elf_link_hash_table_create (bfd *abfd) 2547 { 2548 struct elf_sh_link_hash_table *ret; 2549 bfd_size_type amt = sizeof (struct elf_sh_link_hash_table); 2550 2551 ret = (struct elf_sh_link_hash_table *) bfd_malloc (amt); 2552 if (ret == (struct elf_sh_link_hash_table *) NULL) 2553 return NULL; 2554 2555 if (!_bfd_elf_link_hash_table_init (&ret->root, abfd, 2556 sh_elf_link_hash_newfunc, 2557 sizeof (struct elf_sh_link_hash_entry), 2558 SH_ELF_DATA)) 2559 { 2560 free (ret); 2561 return NULL; 2562 } 2563 2564 ret->sgot = NULL; 2565 ret->sgotplt = NULL; 2566 ret->srelgot = NULL; 2567 ret->splt = NULL; 2568 ret->srelplt = NULL; 2569 ret->sdynbss = NULL; 2570 ret->srelbss = NULL; 2571 ret->srelplt2 = NULL; 2572 ret->sym_cache.abfd = NULL; 2573 ret->tls_ldm_got.refcount = 0; 2574 ret->plt_info = NULL; 2575 ret->vxworks_p = vxworks_object_p (abfd); 2576 ret->fdpic_p = fdpic_object_p (abfd); 2577 2578 return &ret->root.root; 2579 } 2580 2581 static bfd_boolean 2582 sh_elf_omit_section_dynsym (bfd *output_bfd ATTRIBUTE_UNUSED, 2583 struct bfd_link_info *info, asection *p) 2584 { 2585 struct elf_sh_link_hash_table *htab = sh_elf_hash_table (info); 2586 2587 /* Non-FDPIC binaries do not need dynamic symbols for sections. */ 2588 if (!htab->fdpic_p) 2589 return TRUE; 2590 2591 /* We need dynamic symbols for every section, since segments can 2592 relocate independently. */ 2593 switch (elf_section_data (p)->this_hdr.sh_type) 2594 { 2595 case SHT_PROGBITS: 2596 case SHT_NOBITS: 2597 /* If sh_type is yet undecided, assume it could be 2598 SHT_PROGBITS/SHT_NOBITS. */ 2599 case SHT_NULL: 2600 return FALSE; 2601 2602 /* There shouldn't be section relative relocations 2603 against any other section. */ 2604 default: 2605 return TRUE; 2606 } 2607 } 2608 2609 /* Create .got, .gotplt, and .rela.got sections in DYNOBJ, and set up 2610 shortcuts to them in our hash table. */ 2611 2612 static bfd_boolean 2613 create_got_section (bfd *dynobj, struct bfd_link_info *info) 2614 { 2615 struct elf_sh_link_hash_table *htab; 2616 2617 if (! _bfd_elf_create_got_section (dynobj, info)) 2618 return FALSE; 2619 2620 htab = sh_elf_hash_table (info); 2621 if (htab == NULL) 2622 return FALSE; 2623 2624 htab->sgot = bfd_get_section_by_name (dynobj, ".got"); 2625 htab->sgotplt = bfd_get_section_by_name (dynobj, ".got.plt"); 2626 htab->srelgot = bfd_get_section_by_name (dynobj, ".rela.got"); 2627 if (! htab->sgot || ! htab->sgotplt || ! htab->srelgot) 2628 abort (); 2629 2630 htab->sfuncdesc = bfd_make_section_with_flags (dynobj, ".got.funcdesc", 2631 (SEC_ALLOC | SEC_LOAD 2632 | SEC_HAS_CONTENTS 2633 | SEC_IN_MEMORY 2634 | SEC_LINKER_CREATED)); 2635 if (htab->sfuncdesc == NULL 2636 || ! bfd_set_section_alignment (dynobj, htab->sfuncdesc, 2)) 2637 return FALSE; 2638 2639 htab->srelfuncdesc = bfd_make_section_with_flags (dynobj, 2640 ".rela.got.funcdesc", 2641 (SEC_ALLOC | SEC_LOAD 2642 | SEC_HAS_CONTENTS 2643 | SEC_IN_MEMORY 2644 | SEC_LINKER_CREATED 2645 | SEC_READONLY)); 2646 if (htab->srelfuncdesc == NULL 2647 || ! bfd_set_section_alignment (dynobj, htab->srelfuncdesc, 2)) 2648 return FALSE; 2649 2650 /* Also create .rofixup. */ 2651 htab->srofixup = bfd_make_section_with_flags (dynobj, ".rofixup", 2652 (SEC_ALLOC | SEC_LOAD 2653 | SEC_HAS_CONTENTS 2654 | SEC_IN_MEMORY 2655 | SEC_LINKER_CREATED 2656 | SEC_READONLY)); 2657 if (htab->srofixup == NULL 2658 || ! bfd_set_section_alignment (dynobj, htab->srofixup, 2)) 2659 return FALSE; 2660 2661 return TRUE; 2662 } 2663 2664 /* Create dynamic sections when linking against a dynamic object. */ 2665 2666 static bfd_boolean 2667 sh_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info) 2668 { 2669 struct elf_sh_link_hash_table *htab; 2670 flagword flags, pltflags; 2671 asection *s; 2672 const struct elf_backend_data *bed = get_elf_backend_data (abfd); 2673 int ptralign = 0; 2674 2675 switch (bed->s->arch_size) 2676 { 2677 case 32: 2678 ptralign = 2; 2679 break; 2680 2681 case 64: 2682 ptralign = 3; 2683 break; 2684 2685 default: 2686 bfd_set_error (bfd_error_bad_value); 2687 return FALSE; 2688 } 2689 2690 htab = sh_elf_hash_table (info); 2691 if (htab == NULL) 2692 return FALSE; 2693 2694 if (htab->root.dynamic_sections_created) 2695 return TRUE; 2696 2697 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and 2698 .rel[a].bss sections. */ 2699 2700 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY 2701 | SEC_LINKER_CREATED); 2702 2703 pltflags = flags; 2704 pltflags |= SEC_CODE; 2705 if (bed->plt_not_loaded) 2706 pltflags &= ~ (SEC_LOAD | SEC_HAS_CONTENTS); 2707 if (bed->plt_readonly) 2708 pltflags |= SEC_READONLY; 2709 2710 s = bfd_make_section_with_flags (abfd, ".plt", pltflags); 2711 htab->splt = s; 2712 if (s == NULL 2713 || ! bfd_set_section_alignment (abfd, s, bed->plt_alignment)) 2714 return FALSE; 2715 2716 if (bed->want_plt_sym) 2717 { 2718 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the 2719 .plt section. */ 2720 struct elf_link_hash_entry *h; 2721 struct bfd_link_hash_entry *bh = NULL; 2722 2723 if (! (_bfd_generic_link_add_one_symbol 2724 (info, abfd, "_PROCEDURE_LINKAGE_TABLE_", BSF_GLOBAL, s, 2725 (bfd_vma) 0, (const char *) NULL, FALSE, 2726 get_elf_backend_data (abfd)->collect, &bh))) 2727 return FALSE; 2728 2729 h = (struct elf_link_hash_entry *) bh; 2730 h->def_regular = 1; 2731 h->type = STT_OBJECT; 2732 htab->root.hplt = h; 2733 2734 if (info->shared 2735 && ! bfd_elf_link_record_dynamic_symbol (info, h)) 2736 return FALSE; 2737 } 2738 2739 s = bfd_make_section_with_flags (abfd, 2740 bed->default_use_rela_p ? ".rela.plt" : ".rel.plt", 2741 flags | SEC_READONLY); 2742 htab->srelplt = s; 2743 if (s == NULL 2744 || ! bfd_set_section_alignment (abfd, s, ptralign)) 2745 return FALSE; 2746 2747 if (htab->sgot == NULL 2748 && !create_got_section (abfd, info)) 2749 return FALSE; 2750 2751 { 2752 const char *secname; 2753 char *relname; 2754 flagword secflags; 2755 asection *sec; 2756 2757 for (sec = abfd->sections; sec; sec = sec->next) 2758 { 2759 secflags = bfd_get_section_flags (abfd, sec); 2760 if ((secflags & (SEC_DATA | SEC_LINKER_CREATED)) 2761 || ((secflags & SEC_HAS_CONTENTS) != SEC_HAS_CONTENTS)) 2762 continue; 2763 secname = bfd_get_section_name (abfd, sec); 2764 relname = (char *) bfd_malloc ((bfd_size_type) strlen (secname) + 6); 2765 strcpy (relname, ".rela"); 2766 strcat (relname, secname); 2767 if (bfd_get_section_by_name (abfd, secname)) 2768 continue; 2769 s = bfd_make_section_with_flags (abfd, relname, 2770 flags | SEC_READONLY); 2771 if (s == NULL 2772 || ! bfd_set_section_alignment (abfd, s, ptralign)) 2773 return FALSE; 2774 } 2775 } 2776 2777 if (bed->want_dynbss) 2778 { 2779 /* The .dynbss section is a place to put symbols which are defined 2780 by dynamic objects, are referenced by regular objects, and are 2781 not functions. We must allocate space for them in the process 2782 image and use a R_*_COPY reloc to tell the dynamic linker to 2783 initialize them at run time. The linker script puts the .dynbss 2784 section into the .bss section of the final image. */ 2785 s = bfd_make_section_with_flags (abfd, ".dynbss", 2786 SEC_ALLOC | SEC_LINKER_CREATED); 2787 htab->sdynbss = s; 2788 if (s == NULL) 2789 return FALSE; 2790 2791 /* The .rel[a].bss section holds copy relocs. This section is not 2792 normally needed. We need to create it here, though, so that the 2793 linker will map it to an output section. We can't just create it 2794 only if we need it, because we will not know whether we need it 2795 until we have seen all the input files, and the first time the 2796 main linker code calls BFD after examining all the input files 2797 (size_dynamic_sections) the input sections have already been 2798 mapped to the output sections. If the section turns out not to 2799 be needed, we can discard it later. We will never need this 2800 section when generating a shared object, since they do not use 2801 copy relocs. */ 2802 if (! info->shared) 2803 { 2804 s = bfd_make_section_with_flags (abfd, 2805 (bed->default_use_rela_p 2806 ? ".rela.bss" : ".rel.bss"), 2807 flags | SEC_READONLY); 2808 htab->srelbss = s; 2809 if (s == NULL 2810 || ! bfd_set_section_alignment (abfd, s, ptralign)) 2811 return FALSE; 2812 } 2813 } 2814 2815 if (htab->vxworks_p) 2816 { 2817 if (!elf_vxworks_create_dynamic_sections (abfd, info, &htab->srelplt2)) 2818 return FALSE; 2819 } 2820 2821 return TRUE; 2822 } 2823 2824 /* Adjust a symbol defined by a dynamic object and referenced by a 2825 regular object. The current definition is in some section of the 2826 dynamic object, but we're not including those sections. We have to 2827 change the definition to something the rest of the link can 2828 understand. */ 2829 2830 static bfd_boolean 2831 sh_elf_adjust_dynamic_symbol (struct bfd_link_info *info, 2832 struct elf_link_hash_entry *h) 2833 { 2834 struct elf_sh_link_hash_table *htab; 2835 struct elf_sh_link_hash_entry *eh; 2836 struct elf_sh_dyn_relocs *p; 2837 asection *s; 2838 2839 htab = sh_elf_hash_table (info); 2840 if (htab == NULL) 2841 return FALSE; 2842 2843 /* Make sure we know what is going on here. */ 2844 BFD_ASSERT (htab->root.dynobj != NULL 2845 && (h->needs_plt 2846 || h->u.weakdef != NULL 2847 || (h->def_dynamic 2848 && h->ref_regular 2849 && !h->def_regular))); 2850 2851 /* If this is a function, put it in the procedure linkage table. We 2852 will fill in the contents of the procedure linkage table later, 2853 when we know the address of the .got section. */ 2854 if (h->type == STT_FUNC 2855 || h->needs_plt) 2856 { 2857 if (h->plt.refcount <= 0 2858 || SYMBOL_CALLS_LOCAL (info, h) 2859 || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT 2860 && h->root.type == bfd_link_hash_undefweak)) 2861 { 2862 /* This case can occur if we saw a PLT reloc in an input 2863 file, but the symbol was never referred to by a dynamic 2864 object. In such a case, we don't actually need to build 2865 a procedure linkage table, and we can just do a REL32 2866 reloc instead. */ 2867 h->plt.offset = (bfd_vma) -1; 2868 h->needs_plt = 0; 2869 } 2870 2871 return TRUE; 2872 } 2873 else 2874 h->plt.offset = (bfd_vma) -1; 2875 2876 /* If this is a weak symbol, and there is a real definition, the 2877 processor independent code will have arranged for us to see the 2878 real definition first, and we can just use the same value. */ 2879 if (h->u.weakdef != NULL) 2880 { 2881 BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined 2882 || h->u.weakdef->root.type == bfd_link_hash_defweak); 2883 h->root.u.def.section = h->u.weakdef->root.u.def.section; 2884 h->root.u.def.value = h->u.weakdef->root.u.def.value; 2885 if (info->nocopyreloc) 2886 h->non_got_ref = h->u.weakdef->non_got_ref; 2887 return TRUE; 2888 } 2889 2890 /* This is a reference to a symbol defined by a dynamic object which 2891 is not a function. */ 2892 2893 /* If we are creating a shared library, we must presume that the 2894 only references to the symbol are via the global offset table. 2895 For such cases we need not do anything here; the relocations will 2896 be handled correctly by relocate_section. */ 2897 if (info->shared) 2898 return TRUE; 2899 2900 /* If there are no references to this symbol that do not use the 2901 GOT, we don't need to generate a copy reloc. */ 2902 if (!h->non_got_ref) 2903 return TRUE; 2904 2905 /* If -z nocopyreloc was given, we won't generate them either. */ 2906 if (info->nocopyreloc) 2907 { 2908 h->non_got_ref = 0; 2909 return TRUE; 2910 } 2911 2912 eh = (struct elf_sh_link_hash_entry *) h; 2913 for (p = eh->dyn_relocs; p != NULL; p = p->next) 2914 { 2915 s = p->sec->output_section; 2916 if (s != NULL && (s->flags & (SEC_READONLY | SEC_HAS_CONTENTS)) != 0) 2917 break; 2918 } 2919 2920 /* If we didn't find any dynamic relocs in sections which needs the 2921 copy reloc, then we'll be keeping the dynamic relocs and avoiding 2922 the copy reloc. */ 2923 if (p == NULL) 2924 { 2925 h->non_got_ref = 0; 2926 return TRUE; 2927 } 2928 2929 if (h->size == 0) 2930 { 2931 (*_bfd_error_handler) (_("dynamic variable `%s' is zero size"), 2932 h->root.root.string); 2933 return TRUE; 2934 } 2935 2936 /* We must allocate the symbol in our .dynbss section, which will 2937 become part of the .bss section of the executable. There will be 2938 an entry for this symbol in the .dynsym section. The dynamic 2939 object will contain position independent code, so all references 2940 from the dynamic object to this symbol will go through the global 2941 offset table. The dynamic linker will use the .dynsym entry to 2942 determine the address it must put in the global offset table, so 2943 both the dynamic object and the regular object will refer to the 2944 same memory location for the variable. */ 2945 2946 s = htab->sdynbss; 2947 BFD_ASSERT (s != NULL); 2948 2949 /* We must generate a R_SH_COPY reloc to tell the dynamic linker to 2950 copy the initial value out of the dynamic object and into the 2951 runtime process image. We need to remember the offset into the 2952 .rela.bss section we are going to use. */ 2953 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0) 2954 { 2955 asection *srel; 2956 2957 srel = htab->srelbss; 2958 BFD_ASSERT (srel != NULL); 2959 srel->size += sizeof (Elf32_External_Rela); 2960 h->needs_copy = 1; 2961 } 2962 2963 return _bfd_elf_adjust_dynamic_copy (h, s); 2964 } 2965 2966 /* Allocate space in .plt, .got and associated reloc sections for 2967 dynamic relocs. */ 2968 2969 static bfd_boolean 2970 allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf) 2971 { 2972 struct bfd_link_info *info; 2973 struct elf_sh_link_hash_table *htab; 2974 struct elf_sh_link_hash_entry *eh; 2975 struct elf_sh_dyn_relocs *p; 2976 2977 if (h->root.type == bfd_link_hash_indirect) 2978 return TRUE; 2979 2980 if (h->root.type == bfd_link_hash_warning) 2981 /* When warning symbols are created, they **replace** the "real" 2982 entry in the hash table, thus we never get to see the real 2983 symbol in a hash traversal. So look at it now. */ 2984 h = (struct elf_link_hash_entry *) h->root.u.i.link; 2985 2986 info = (struct bfd_link_info *) inf; 2987 htab = sh_elf_hash_table (info); 2988 if (htab == NULL) 2989 return FALSE; 2990 2991 eh = (struct elf_sh_link_hash_entry *) h; 2992 if ((h->got.refcount > 0 2993 || h->forced_local) 2994 && eh->gotplt_refcount > 0) 2995 { 2996 /* The symbol has been forced local, or we have some direct got refs, 2997 so treat all the gotplt refs as got refs. */ 2998 h->got.refcount += eh->gotplt_refcount; 2999 if (h->plt.refcount >= eh->gotplt_refcount) 3000 h->plt.refcount -= eh->gotplt_refcount; 3001 } 3002 3003 if (htab->root.dynamic_sections_created 3004 && h->plt.refcount > 0 3005 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT 3006 || h->root.type != bfd_link_hash_undefweak)) 3007 { 3008 /* Make sure this symbol is output as a dynamic symbol. 3009 Undefined weak syms won't yet be marked as dynamic. */ 3010 if (h->dynindx == -1 3011 && !h->forced_local) 3012 { 3013 if (! bfd_elf_link_record_dynamic_symbol (info, h)) 3014 return FALSE; 3015 } 3016 3017 if (info->shared 3018 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, 0, h)) 3019 { 3020 asection *s = htab->splt; 3021 const struct elf_sh_plt_info *plt_info; 3022 3023 /* If this is the first .plt entry, make room for the special 3024 first entry. */ 3025 if (s->size == 0) 3026 s->size += htab->plt_info->plt0_entry_size; 3027 3028 h->plt.offset = s->size; 3029 3030 /* If this symbol is not defined in a regular file, and we are 3031 not generating a shared library, then set the symbol to this 3032 location in the .plt. This is required to make function 3033 pointers compare as equal between the normal executable and 3034 the shared library. Skip this for FDPIC, since the 3035 function's address will be the address of the canonical 3036 function descriptor. */ 3037 if (!htab->fdpic_p && !info->shared && !h->def_regular) 3038 { 3039 h->root.u.def.section = s; 3040 h->root.u.def.value = h->plt.offset; 3041 } 3042 3043 /* Make room for this entry. */ 3044 plt_info = htab->plt_info; 3045 if (plt_info->short_plt != NULL 3046 && (get_plt_index (plt_info->short_plt, s->size) < MAX_SHORT_PLT)) 3047 plt_info = plt_info->short_plt; 3048 s->size += plt_info->symbol_entry_size; 3049 3050 /* We also need to make an entry in the .got.plt section, which 3051 will be placed in the .got section by the linker script. */ 3052 if (!htab->fdpic_p) 3053 htab->sgotplt->size += 4; 3054 else 3055 htab->sgotplt->size += 8; 3056 3057 /* We also need to make an entry in the .rel.plt section. */ 3058 htab->srelplt->size += sizeof (Elf32_External_Rela); 3059 3060 if (htab->vxworks_p && !info->shared) 3061 { 3062 /* VxWorks executables have a second set of relocations 3063 for each PLT entry. They go in a separate relocation 3064 section, which is processed by the kernel loader. */ 3065 3066 /* There is a relocation for the initial PLT entry: 3067 an R_SH_DIR32 relocation for _GLOBAL_OFFSET_TABLE_. */ 3068 if (h->plt.offset == htab->plt_info->plt0_entry_size) 3069 htab->srelplt2->size += sizeof (Elf32_External_Rela); 3070 3071 /* There are two extra relocations for each subsequent 3072 PLT entry: an R_SH_DIR32 relocation for the GOT entry, 3073 and an R_SH_DIR32 relocation for the PLT entry. */ 3074 htab->srelplt2->size += sizeof (Elf32_External_Rela) * 2; 3075 } 3076 } 3077 else 3078 { 3079 h->plt.offset = (bfd_vma) -1; 3080 h->needs_plt = 0; 3081 } 3082 } 3083 else 3084 { 3085 h->plt.offset = (bfd_vma) -1; 3086 h->needs_plt = 0; 3087 } 3088 3089 if (h->got.refcount > 0) 3090 { 3091 asection *s; 3092 bfd_boolean dyn; 3093 int got_type = sh_elf_hash_entry (h)->got_type; 3094 3095 /* Make sure this symbol is output as a dynamic symbol. 3096 Undefined weak syms won't yet be marked as dynamic. */ 3097 if (h->dynindx == -1 3098 && !h->forced_local) 3099 { 3100 if (! bfd_elf_link_record_dynamic_symbol (info, h)) 3101 return FALSE; 3102 } 3103 3104 s = htab->sgot; 3105 h->got.offset = s->size; 3106 s->size += 4; 3107 /* R_SH_TLS_GD needs 2 consecutive GOT slots. */ 3108 if (got_type == GOT_TLS_GD) 3109 s->size += 4; 3110 dyn = htab->root.dynamic_sections_created; 3111 if (!dyn) 3112 { 3113 /* No dynamic relocations required. */ 3114 if (htab->fdpic_p && !info->shared 3115 && h->root.type != bfd_link_hash_undefweak 3116 && (got_type == GOT_NORMAL || got_type == GOT_FUNCDESC)) 3117 htab->srofixup->size += 4; 3118 } 3119 /* R_SH_TLS_IE_32 needs one dynamic relocation if dynamic, 3120 R_SH_TLS_GD needs one if local symbol and two if global. */ 3121 else if ((got_type == GOT_TLS_GD && h->dynindx == -1) 3122 || got_type == GOT_TLS_IE) 3123 htab->srelgot->size += sizeof (Elf32_External_Rela); 3124 else if (got_type == GOT_TLS_GD) 3125 htab->srelgot->size += 2 * sizeof (Elf32_External_Rela); 3126 else if (got_type == GOT_FUNCDESC) 3127 { 3128 if (!info->shared && SYMBOL_FUNCDESC_LOCAL (info, h)) 3129 htab->srofixup->size += 4; 3130 else 3131 htab->srelgot->size += sizeof (Elf32_External_Rela); 3132 } 3133 else if ((ELF_ST_VISIBILITY (h->other) == STV_DEFAULT 3134 || h->root.type != bfd_link_hash_undefweak) 3135 && (info->shared 3136 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, h))) 3137 htab->srelgot->size += sizeof (Elf32_External_Rela); 3138 else if (htab->fdpic_p && !info->shared && got_type == GOT_NORMAL 3139 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT 3140 || h->root.type != bfd_link_hash_undefweak)) 3141 htab->srofixup->size += 4; 3142 } 3143 else 3144 h->got.offset = (bfd_vma) -1; 3145 3146 #ifdef INCLUDE_SHMEDIA 3147 if (eh->datalabel_got.refcount > 0) 3148 { 3149 asection *s; 3150 bfd_boolean dyn; 3151 3152 /* Make sure this symbol is output as a dynamic symbol. 3153 Undefined weak syms won't yet be marked as dynamic. */ 3154 if (h->dynindx == -1 3155 && !h->forced_local) 3156 { 3157 if (! bfd_elf_link_record_dynamic_symbol (info, h)) 3158 return FALSE; 3159 } 3160 3161 s = htab->sgot; 3162 eh->datalabel_got.offset = s->size; 3163 s->size += 4; 3164 dyn = htab->root.dynamic_sections_created; 3165 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h)) 3166 htab->srelgot->size += sizeof (Elf32_External_Rela); 3167 } 3168 else 3169 eh->datalabel_got.offset = (bfd_vma) -1; 3170 #endif 3171 3172 /* Allocate space for any dynamic relocations to function 3173 descriptors, canonical or otherwise. We need to relocate the 3174 reference unless it resolves to zero, which only happens for 3175 undefined weak symbols (either non-default visibility, or when 3176 static linking). Any GOT slot is accounted for elsewhere. */ 3177 if (eh->abs_funcdesc_refcount > 0 3178 && (h->root.type != bfd_link_hash_undefweak 3179 || (htab->root.dynamic_sections_created 3180 && ! SYMBOL_CALLS_LOCAL (info, h)))) 3181 { 3182 if (!info->shared && SYMBOL_FUNCDESC_LOCAL (info, h)) 3183 htab->srofixup->size += eh->abs_funcdesc_refcount * 4; 3184 else 3185 htab->srelgot->size 3186 += eh->abs_funcdesc_refcount * sizeof (Elf32_External_Rela); 3187 } 3188 3189 /* We must allocate a function descriptor if there are references to 3190 a canonical descriptor (R_SH_GOTFUNCDESC or R_SH_FUNCDESC) and 3191 the dynamic linker isn't going to allocate it. None of this 3192 applies if we already created one in .got.plt, but if the 3193 canonical function descriptor can be in this object, there 3194 won't be a PLT entry at all. */ 3195 if ((eh->funcdesc.refcount > 0 3196 || (h->got.offset != MINUS_ONE && eh->got_type == GOT_FUNCDESC)) 3197 && h->root.type != bfd_link_hash_undefweak 3198 && SYMBOL_FUNCDESC_LOCAL (info, h)) 3199 { 3200 /* Make room for this function descriptor. */ 3201 eh->funcdesc.offset = htab->sfuncdesc->size; 3202 htab->sfuncdesc->size += 8; 3203 3204 /* We will need a relocation or two fixups to initialize the 3205 function descriptor, so allocate those too. */ 3206 if (!info->shared && SYMBOL_CALLS_LOCAL (info, h)) 3207 htab->srofixup->size += 8; 3208 else 3209 htab->srelfuncdesc->size += sizeof (Elf32_External_Rela); 3210 } 3211 3212 if (eh->dyn_relocs == NULL) 3213 return TRUE; 3214 3215 /* In the shared -Bsymbolic case, discard space allocated for 3216 dynamic pc-relative relocs against symbols which turn out to be 3217 defined in regular objects. For the normal shared case, discard 3218 space for pc-relative relocs that have become local due to symbol 3219 visibility changes. */ 3220 3221 if (info->shared) 3222 { 3223 if (SYMBOL_CALLS_LOCAL (info, h)) 3224 { 3225 struct elf_sh_dyn_relocs **pp; 3226 3227 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; ) 3228 { 3229 p->count -= p->pc_count; 3230 p->pc_count = 0; 3231 if (p->count == 0) 3232 *pp = p->next; 3233 else 3234 pp = &p->next; 3235 } 3236 } 3237 3238 if (htab->vxworks_p) 3239 { 3240 struct elf_sh_dyn_relocs **pp; 3241 3242 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; ) 3243 { 3244 if (strcmp (p->sec->output_section->name, ".tls_vars") == 0) 3245 *pp = p->next; 3246 else 3247 pp = &p->next; 3248 } 3249 } 3250 3251 /* Also discard relocs on undefined weak syms with non-default 3252 visibility. */ 3253 if (eh->dyn_relocs != NULL 3254 && h->root.type == bfd_link_hash_undefweak) 3255 { 3256 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT) 3257 eh->dyn_relocs = NULL; 3258 3259 /* Make sure undefined weak symbols are output as a dynamic 3260 symbol in PIEs. */ 3261 else if (h->dynindx == -1 3262 && !h->forced_local) 3263 { 3264 if (! bfd_elf_link_record_dynamic_symbol (info, h)) 3265 return FALSE; 3266 } 3267 } 3268 } 3269 else 3270 { 3271 /* For the non-shared case, discard space for relocs against 3272 symbols which turn out to need copy relocs or are not 3273 dynamic. */ 3274 3275 if (!h->non_got_ref 3276 && ((h->def_dynamic 3277 && !h->def_regular) 3278 || (htab->root.dynamic_sections_created 3279 && (h->root.type == bfd_link_hash_undefweak 3280 || h->root.type == bfd_link_hash_undefined)))) 3281 { 3282 /* Make sure this symbol is output as a dynamic symbol. 3283 Undefined weak syms won't yet be marked as dynamic. */ 3284 if (h->dynindx == -1 3285 && !h->forced_local) 3286 { 3287 if (! bfd_elf_link_record_dynamic_symbol (info, h)) 3288 return FALSE; 3289 } 3290 3291 /* If that succeeded, we know we'll be keeping all the 3292 relocs. */ 3293 if (h->dynindx != -1) 3294 goto keep; 3295 } 3296 3297 eh->dyn_relocs = NULL; 3298 3299 keep: ; 3300 } 3301 3302 /* Finally, allocate space. */ 3303 for (p = eh->dyn_relocs; p != NULL; p = p->next) 3304 { 3305 asection *sreloc = elf_section_data (p->sec)->sreloc; 3306 sreloc->size += p->count * sizeof (Elf32_External_Rela); 3307 3308 /* If we need relocations, we do not need fixups. */ 3309 if (htab->fdpic_p && !info->shared) 3310 htab->srofixup->size -= 4 * (p->count - p->pc_count); 3311 } 3312 3313 return TRUE; 3314 } 3315 3316 /* Find any dynamic relocs that apply to read-only sections. */ 3317 3318 static bfd_boolean 3319 readonly_dynrelocs (struct elf_link_hash_entry *h, void *inf) 3320 { 3321 struct elf_sh_link_hash_entry *eh; 3322 struct elf_sh_dyn_relocs *p; 3323 3324 if (h->root.type == bfd_link_hash_warning) 3325 h = (struct elf_link_hash_entry *) h->root.u.i.link; 3326 3327 eh = (struct elf_sh_link_hash_entry *) h; 3328 for (p = eh->dyn_relocs; p != NULL; p = p->next) 3329 { 3330 asection *s = p->sec->output_section; 3331 3332 if (s != NULL && (s->flags & SEC_READONLY) != 0) 3333 { 3334 struct bfd_link_info *info = (struct bfd_link_info *) inf; 3335 3336 if (info->warn_shared_textrel) 3337 (*_bfd_error_handler) 3338 (_("warning: dynamic relocation in readonly section `%s'"), 3339 h->root.root.string); 3340 info->flags |= DF_TEXTREL; 3341 3342 /* Not an error, just cut short the traversal. */ 3343 return FALSE; 3344 } 3345 } 3346 return TRUE; 3347 } 3348 3349 /* This function is called after all the input files have been read, 3350 and the input sections have been assigned to output sections. 3351 It's a convenient place to determine the PLT style. */ 3352 3353 static bfd_boolean 3354 sh_elf_always_size_sections (bfd *output_bfd, struct bfd_link_info *info) 3355 { 3356 sh_elf_hash_table (info)->plt_info = get_plt_info (output_bfd, info->shared); 3357 3358 if (sh_elf_hash_table (info)->fdpic_p && !info->relocatable) 3359 { 3360 struct elf_link_hash_entry *h; 3361 3362 /* Force a PT_GNU_STACK segment to be created. */ 3363 if (! elf_tdata (output_bfd)->stack_flags) 3364 elf_tdata (output_bfd)->stack_flags = PF_R | PF_W | PF_X; 3365 3366 /* Define __stacksize if it's not defined yet. */ 3367 h = elf_link_hash_lookup (elf_hash_table (info), "__stacksize", 3368 FALSE, FALSE, FALSE); 3369 if (! h || h->root.type != bfd_link_hash_defined 3370 || h->type != STT_OBJECT 3371 || !h->def_regular) 3372 { 3373 struct bfd_link_hash_entry *bh = NULL; 3374 3375 if (!(_bfd_generic_link_add_one_symbol 3376 (info, output_bfd, "__stacksize", 3377 BSF_GLOBAL, bfd_abs_section_ptr, DEFAULT_STACK_SIZE, 3378 (const char *) NULL, FALSE, 3379 get_elf_backend_data (output_bfd)->collect, &bh))) 3380 return FALSE; 3381 3382 h = (struct elf_link_hash_entry *) bh; 3383 h->def_regular = 1; 3384 h->type = STT_OBJECT; 3385 } 3386 } 3387 return TRUE; 3388 } 3389 3390 #if !defined INCLUDE_SHMEDIA && !defined SH_TARGET_ALREADY_DEFINED 3391 3392 static bfd_boolean 3393 sh_elf_modify_program_headers (bfd *output_bfd, struct bfd_link_info *info) 3394 { 3395 struct elf_obj_tdata *tdata = elf_tdata (output_bfd); 3396 struct elf_segment_map *m; 3397 Elf_Internal_Phdr *p; 3398 3399 /* objcopy and strip preserve what's already there using 3400 sh_elf_copy_private_bfd_data (). */ 3401 if (! info) 3402 return TRUE; 3403 3404 for (p = tdata->phdr, m = tdata->segment_map; m != NULL; m = m->next, p++) 3405 if (m->p_type == PT_GNU_STACK) 3406 break; 3407 3408 if (m) 3409 { 3410 struct elf_link_hash_entry *h; 3411 3412 /* Obtain the pointer to the __stacksize symbol. */ 3413 h = elf_link_hash_lookup (elf_hash_table (info), "__stacksize", 3414 FALSE, FALSE, FALSE); 3415 if (h) 3416 { 3417 while (h->root.type == bfd_link_hash_indirect 3418 || h->root.type == bfd_link_hash_warning) 3419 h = (struct elf_link_hash_entry *) h->root.u.i.link; 3420 BFD_ASSERT (h->root.type == bfd_link_hash_defined); 3421 } 3422 3423 /* Set the header p_memsz from the symbol value. We 3424 intentionally ignore the symbol section. */ 3425 if (h && h->root.type == bfd_link_hash_defined) 3426 p->p_memsz = h->root.u.def.value; 3427 else 3428 p->p_memsz = DEFAULT_STACK_SIZE; 3429 3430 p->p_align = 8; 3431 } 3432 3433 return TRUE; 3434 } 3435 3436 #endif 3437 3438 /* Set the sizes of the dynamic sections. */ 3439 3440 static bfd_boolean 3441 sh_elf_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED, 3442 struct bfd_link_info *info) 3443 { 3444 struct elf_sh_link_hash_table *htab; 3445 bfd *dynobj; 3446 asection *s; 3447 bfd_boolean relocs; 3448 bfd *ibfd; 3449 3450 htab = sh_elf_hash_table (info); 3451 if (htab == NULL) 3452 return FALSE; 3453 3454 dynobj = htab->root.dynobj; 3455 BFD_ASSERT (dynobj != NULL); 3456 3457 if (htab->root.dynamic_sections_created) 3458 { 3459 /* Set the contents of the .interp section to the interpreter. */ 3460 if (info->executable) 3461 { 3462 s = bfd_get_section_by_name (dynobj, ".interp"); 3463 BFD_ASSERT (s != NULL); 3464 s->size = sizeof ELF_DYNAMIC_INTERPRETER; 3465 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER; 3466 } 3467 } 3468 3469 /* Set up .got offsets for local syms, and space for local dynamic 3470 relocs. */ 3471 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next) 3472 { 3473 bfd_signed_vma *local_got; 3474 bfd_signed_vma *end_local_got; 3475 union gotref *local_funcdesc, *end_local_funcdesc; 3476 char *local_got_type; 3477 bfd_size_type locsymcount; 3478 Elf_Internal_Shdr *symtab_hdr; 3479 asection *srel; 3480 3481 if (! is_sh_elf (ibfd)) 3482 continue; 3483 3484 for (s = ibfd->sections; s != NULL; s = s->next) 3485 { 3486 struct elf_sh_dyn_relocs *p; 3487 3488 for (p = ((struct elf_sh_dyn_relocs *) 3489 elf_section_data (s)->local_dynrel); 3490 p != NULL; 3491 p = p->next) 3492 { 3493 if (! bfd_is_abs_section (p->sec) 3494 && bfd_is_abs_section (p->sec->output_section)) 3495 { 3496 /* Input section has been discarded, either because 3497 it is a copy of a linkonce section or due to 3498 linker script /DISCARD/, so we'll be discarding 3499 the relocs too. */ 3500 } 3501 else if (htab->vxworks_p 3502 && strcmp (p->sec->output_section->name, 3503 ".tls_vars") == 0) 3504 { 3505 /* Relocations in vxworks .tls_vars sections are 3506 handled specially by the loader. */ 3507 } 3508 else if (p->count != 0) 3509 { 3510 srel = elf_section_data (p->sec)->sreloc; 3511 srel->size += p->count * sizeof (Elf32_External_Rela); 3512 if ((p->sec->output_section->flags & SEC_READONLY) != 0) 3513 info->flags |= DF_TEXTREL; 3514 3515 /* If we need relocations, we do not need fixups. */ 3516 if (htab->fdpic_p && !info->shared) 3517 htab->srofixup->size -= 4 * (p->count - p->pc_count); 3518 } 3519 } 3520 } 3521 3522 symtab_hdr = &elf_symtab_hdr (ibfd); 3523 locsymcount = symtab_hdr->sh_info; 3524 #ifdef INCLUDE_SHMEDIA 3525 /* Count datalabel local GOT. */ 3526 locsymcount *= 2; 3527 #endif 3528 s = htab->sgot; 3529 srel = htab->srelgot; 3530 3531 local_got = elf_local_got_refcounts (ibfd); 3532 if (local_got) 3533 { 3534 end_local_got = local_got + locsymcount; 3535 local_got_type = sh_elf_local_got_type (ibfd); 3536 local_funcdesc = sh_elf_local_funcdesc (ibfd); 3537 for (; local_got < end_local_got; ++local_got) 3538 { 3539 if (*local_got > 0) 3540 { 3541 *local_got = s->size; 3542 s->size += 4; 3543 if (*local_got_type == GOT_TLS_GD) 3544 s->size += 4; 3545 if (info->shared) 3546 srel->size += sizeof (Elf32_External_Rela); 3547 else 3548 htab->srofixup->size += 4; 3549 3550 if (*local_got_type == GOT_FUNCDESC) 3551 { 3552 if (local_funcdesc == NULL) 3553 { 3554 bfd_size_type size; 3555 3556 size = locsymcount * sizeof (union gotref); 3557 local_funcdesc = (union gotref *) bfd_zalloc (ibfd, 3558 size); 3559 if (local_funcdesc == NULL) 3560 return FALSE; 3561 sh_elf_local_funcdesc (ibfd) = local_funcdesc; 3562 local_funcdesc += (local_got 3563 - elf_local_got_refcounts (ibfd)); 3564 } 3565 local_funcdesc->refcount++; 3566 ++local_funcdesc; 3567 } 3568 } 3569 else 3570 *local_got = (bfd_vma) -1; 3571 ++local_got_type; 3572 } 3573 } 3574 3575 local_funcdesc = sh_elf_local_funcdesc (ibfd); 3576 if (local_funcdesc) 3577 { 3578 end_local_funcdesc = local_funcdesc + locsymcount; 3579 3580 for (; local_funcdesc < end_local_funcdesc; ++local_funcdesc) 3581 { 3582 if (local_funcdesc->refcount > 0) 3583 { 3584 local_funcdesc->offset = htab->sfuncdesc->size; 3585 htab->sfuncdesc->size += 8; 3586 if (!info->shared) 3587 htab->srofixup->size += 8; 3588 else 3589 htab->srelfuncdesc->size += sizeof (Elf32_External_Rela); 3590 } 3591 else 3592 local_funcdesc->offset = MINUS_ONE; 3593 } 3594 } 3595 3596 } 3597 3598 if (htab->tls_ldm_got.refcount > 0) 3599 { 3600 /* Allocate 2 got entries and 1 dynamic reloc for R_SH_TLS_LD_32 3601 relocs. */ 3602 htab->tls_ldm_got.offset = htab->sgot->size; 3603 htab->sgot->size += 8; 3604 htab->srelgot->size += sizeof (Elf32_External_Rela); 3605 } 3606 else 3607 htab->tls_ldm_got.offset = -1; 3608 3609 /* Only the reserved entries should be present. For FDPIC, they go at 3610 the end of .got.plt. */ 3611 if (htab->fdpic_p) 3612 { 3613 BFD_ASSERT (htab->sgotplt && htab->sgotplt->size == 12); 3614 htab->sgotplt->size = 0; 3615 } 3616 3617 /* Allocate global sym .plt and .got entries, and space for global 3618 sym dynamic relocs. */ 3619 elf_link_hash_traverse (&htab->root, allocate_dynrelocs, info); 3620 3621 /* Move the reserved entries and the _GLOBAL_OFFSET_TABLE_ symbol to the 3622 end of the FDPIC .got.plt. */ 3623 if (htab->fdpic_p) 3624 { 3625 htab->root.hgot->root.u.def.value = htab->sgotplt->size; 3626 htab->sgotplt->size += 12; 3627 } 3628 3629 /* At the very end of the .rofixup section is a pointer to the GOT. */ 3630 if (htab->fdpic_p && htab->srofixup != NULL) 3631 htab->srofixup->size += 4; 3632 3633 /* We now have determined the sizes of the various dynamic sections. 3634 Allocate memory for them. */ 3635 relocs = FALSE; 3636 for (s = dynobj->sections; s != NULL; s = s->next) 3637 { 3638 if ((s->flags & SEC_LINKER_CREATED) == 0) 3639 continue; 3640 3641 if (s == htab->splt 3642 || s == htab->sgot 3643 || s == htab->sgotplt 3644 || s == htab->sfuncdesc 3645 || s == htab->srofixup 3646 || s == htab->sdynbss) 3647 { 3648 /* Strip this section if we don't need it; see the 3649 comment below. */ 3650 } 3651 else if (CONST_STRNEQ (bfd_get_section_name (dynobj, s), ".rela")) 3652 { 3653 if (s->size != 0 && s != htab->srelplt && s != htab->srelplt2) 3654 relocs = TRUE; 3655 3656 /* We use the reloc_count field as a counter if we need 3657 to copy relocs into the output file. */ 3658 s->reloc_count = 0; 3659 } 3660 else 3661 { 3662 /* It's not one of our sections, so don't allocate space. */ 3663 continue; 3664 } 3665 3666 if (s->size == 0) 3667 { 3668 /* If we don't need this section, strip it from the 3669 output file. This is mostly to handle .rela.bss and 3670 .rela.plt. We must create both sections in 3671 create_dynamic_sections, because they must be created 3672 before the linker maps input sections to output 3673 sections. The linker does that before 3674 adjust_dynamic_symbol is called, and it is that 3675 function which decides whether anything needs to go 3676 into these sections. */ 3677 3678 s->flags |= SEC_EXCLUDE; 3679 continue; 3680 } 3681 3682 if ((s->flags & SEC_HAS_CONTENTS) == 0) 3683 continue; 3684 3685 /* Allocate memory for the section contents. We use bfd_zalloc 3686 here in case unused entries are not reclaimed before the 3687 section's contents are written out. This should not happen, 3688 but this way if it does, we get a R_SH_NONE reloc instead 3689 of garbage. */ 3690 s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->size); 3691 if (s->contents == NULL) 3692 return FALSE; 3693 } 3694 3695 if (htab->root.dynamic_sections_created) 3696 { 3697 /* Add some entries to the .dynamic section. We fill in the 3698 values later, in sh_elf_finish_dynamic_sections, but we 3699 must add the entries now so that we get the correct size for 3700 the .dynamic section. The DT_DEBUG entry is filled in by the 3701 dynamic linker and used by the debugger. */ 3702 #define add_dynamic_entry(TAG, VAL) \ 3703 _bfd_elf_add_dynamic_entry (info, TAG, VAL) 3704 3705 if (info->executable) 3706 { 3707 if (! add_dynamic_entry (DT_DEBUG, 0)) 3708 return FALSE; 3709 } 3710 3711 if (htab->splt->size != 0) 3712 { 3713 if (! add_dynamic_entry (DT_PLTGOT, 0) 3714 || ! add_dynamic_entry (DT_PLTRELSZ, 0) 3715 || ! add_dynamic_entry (DT_PLTREL, DT_RELA) 3716 || ! add_dynamic_entry (DT_JMPREL, 0)) 3717 return FALSE; 3718 } 3719 else if ((elf_elfheader (output_bfd)->e_flags & EF_SH_FDPIC) 3720 && htab->sgot->size != 0) 3721 { 3722 if (! add_dynamic_entry (DT_PLTGOT, 0)) 3723 return FALSE; 3724 } 3725 3726 if (relocs) 3727 { 3728 if (! add_dynamic_entry (DT_RELA, 0) 3729 || ! add_dynamic_entry (DT_RELASZ, 0) 3730 || ! add_dynamic_entry (DT_RELAENT, 3731 sizeof (Elf32_External_Rela))) 3732 return FALSE; 3733 3734 /* If any dynamic relocs apply to a read-only section, 3735 then we need a DT_TEXTREL entry. */ 3736 if ((info->flags & DF_TEXTREL) == 0) 3737 elf_link_hash_traverse (&htab->root, readonly_dynrelocs, info); 3738 3739 if ((info->flags & DF_TEXTREL) != 0) 3740 { 3741 if (! add_dynamic_entry (DT_TEXTREL, 0)) 3742 return FALSE; 3743 } 3744 } 3745 if (htab->vxworks_p 3746 && !elf_vxworks_add_dynamic_entries (output_bfd, info)) 3747 return FALSE; 3748 } 3749 #undef add_dynamic_entry 3750 3751 return TRUE; 3752 } 3753 3754 /* Add a dynamic relocation to the SRELOC section. */ 3755 3756 inline static bfd_vma 3757 sh_elf_add_dyn_reloc (bfd *output_bfd, asection *sreloc, bfd_vma offset, 3758 int reloc_type, long dynindx, bfd_vma addend) 3759 { 3760 Elf_Internal_Rela outrel; 3761 bfd_vma reloc_offset; 3762 3763 outrel.r_offset = offset; 3764 outrel.r_info = ELF32_R_INFO (dynindx, reloc_type); 3765 outrel.r_addend = addend; 3766 3767 reloc_offset = sreloc->reloc_count * sizeof (Elf32_External_Rela); 3768 BFD_ASSERT (reloc_offset < sreloc->size); 3769 bfd_elf32_swap_reloca_out (output_bfd, &outrel, 3770 sreloc->contents + reloc_offset); 3771 sreloc->reloc_count++; 3772 3773 return reloc_offset; 3774 } 3775 3776 /* Add an FDPIC read-only fixup. */ 3777 3778 inline static void 3779 sh_elf_add_rofixup (bfd *output_bfd, asection *srofixup, bfd_vma offset) 3780 { 3781 bfd_vma fixup_offset; 3782 3783 fixup_offset = srofixup->reloc_count++ * 4; 3784 BFD_ASSERT (fixup_offset < srofixup->size); 3785 bfd_put_32 (output_bfd, offset, srofixup->contents + fixup_offset); 3786 } 3787 3788 /* Return the offset of the generated .got section from the 3789 _GLOBAL_OFFSET_TABLE_ symbol. */ 3790 3791 static bfd_signed_vma 3792 sh_elf_got_offset (struct elf_sh_link_hash_table *htab) 3793 { 3794 return (htab->sgot->output_offset - htab->sgotplt->output_offset 3795 - htab->root.hgot->root.u.def.value); 3796 } 3797 3798 /* Find the segment number in which OSEC, and output section, is 3799 located. */ 3800 3801 static unsigned 3802 sh_elf_osec_to_segment (bfd *output_bfd, asection *osec) 3803 { 3804 Elf_Internal_Phdr *p = _bfd_elf_find_segment_containing_section (output_bfd, 3805 osec); 3806 3807 /* FIXME: Nothing ever says what this index is relative to. The kernel 3808 supplies data in terms of the number of load segments but this is 3809 a phdr index and the first phdr may not be a load segment. */ 3810 return (p != NULL) ? p - elf_tdata (output_bfd)->phdr : -1; 3811 } 3812 3813 static bfd_boolean 3814 sh_elf_osec_readonly_p (bfd *output_bfd, asection *osec) 3815 { 3816 unsigned seg = sh_elf_osec_to_segment (output_bfd, osec); 3817 3818 return ! (elf_tdata (output_bfd)->phdr[seg].p_flags & PF_W); 3819 } 3820 3821 /* Generate the initial contents of a local function descriptor, along 3822 with any relocations or fixups required. */ 3823 static bfd_boolean 3824 sh_elf_initialize_funcdesc (bfd *output_bfd, 3825 struct bfd_link_info *info, 3826 struct elf_link_hash_entry *h, 3827 bfd_vma offset, 3828 asection *section, 3829 bfd_vma value) 3830 { 3831 struct elf_sh_link_hash_table *htab; 3832 int dynindx; 3833 bfd_vma addr, seg; 3834 3835 htab = sh_elf_hash_table (info); 3836 3837 /* FIXME: The ABI says that the offset to the function goes in the 3838 descriptor, along with the segment index. We're RELA, so it could 3839 go in the reloc instead... */ 3840 3841 if (h != NULL && SYMBOL_CALLS_LOCAL (info, h)) 3842 { 3843 section = h->root.u.def.section; 3844 value = h->root.u.def.value; 3845 } 3846 3847 if (h == NULL || SYMBOL_CALLS_LOCAL (info, h)) 3848 { 3849 dynindx = elf_section_data (section->output_section)->dynindx; 3850 addr = value + section->output_offset; 3851 seg = sh_elf_osec_to_segment (output_bfd, section->output_section); 3852 } 3853 else 3854 { 3855 BFD_ASSERT (h->dynindx != -1); 3856 dynindx = h->dynindx; 3857 addr = seg = 0; 3858 } 3859 3860 if (!info->shared && SYMBOL_CALLS_LOCAL (info, h)) 3861 { 3862 if (h == NULL || h->root.type != bfd_link_hash_undefweak) 3863 { 3864 sh_elf_add_rofixup (output_bfd, htab->srofixup, 3865 offset 3866 + htab->sfuncdesc->output_section->vma 3867 + htab->sfuncdesc->output_offset); 3868 sh_elf_add_rofixup (output_bfd, htab->srofixup, 3869 offset + 4 3870 + htab->sfuncdesc->output_section->vma 3871 + htab->sfuncdesc->output_offset); 3872 } 3873 3874 /* There are no dynamic relocations so fill in the final 3875 address and gp value (barring fixups). */ 3876 addr += section->output_section->vma; 3877 seg = htab->root.hgot->root.u.def.value 3878 + htab->root.hgot->root.u.def.section->output_section->vma 3879 + htab->root.hgot->root.u.def.section->output_offset; 3880 } 3881 else 3882 sh_elf_add_dyn_reloc (output_bfd, htab->srelfuncdesc, 3883 offset 3884 + htab->sfuncdesc->output_section->vma 3885 + htab->sfuncdesc->output_offset, 3886 R_SH_FUNCDESC_VALUE, dynindx, 0); 3887 3888 bfd_put_32 (output_bfd, addr, htab->sfuncdesc->contents + offset); 3889 bfd_put_32 (output_bfd, seg, htab->sfuncdesc->contents + offset + 4); 3890 3891 return TRUE; 3892 } 3893 3894 /* Install a 20-bit movi20 field starting at ADDR, which occurs in OUTPUT_BFD. 3895 VALUE is the field's value. Return bfd_reloc_ok if successful or an error 3896 otherwise. */ 3897 3898 static bfd_reloc_status_type 3899 install_movi20_field (bfd *output_bfd, unsigned long relocation, 3900 bfd *input_bfd, asection *input_section, 3901 bfd_byte *contents, bfd_vma offset) 3902 { 3903 unsigned long cur_val; 3904 bfd_byte *addr; 3905 bfd_reloc_status_type r; 3906 3907 if (offset > bfd_get_section_limit (input_bfd, input_section)) 3908 return bfd_reloc_outofrange; 3909 3910 r = bfd_check_overflow (complain_overflow_signed, 20, 0, 3911 bfd_arch_bits_per_address (input_bfd), relocation); 3912 if (r != bfd_reloc_ok) 3913 return r; 3914 3915 addr = contents + offset; 3916 cur_val = bfd_get_16 (output_bfd, addr); 3917 bfd_put_16 (output_bfd, cur_val | ((relocation & 0xf0000) >> 12), addr); 3918 bfd_put_16 (output_bfd, relocation & 0xffff, addr + 2); 3919 3920 return bfd_reloc_ok; 3921 } 3922 3923 /* Relocate an SH ELF section. */ 3924 3925 static bfd_boolean 3926 sh_elf_relocate_section (bfd *output_bfd, struct bfd_link_info *info, 3927 bfd *input_bfd, asection *input_section, 3928 bfd_byte *contents, Elf_Internal_Rela *relocs, 3929 Elf_Internal_Sym *local_syms, 3930 asection **local_sections) 3931 { 3932 struct elf_sh_link_hash_table *htab; 3933 Elf_Internal_Shdr *symtab_hdr; 3934 struct elf_link_hash_entry **sym_hashes; 3935 Elf_Internal_Rela *rel, *relend; 3936 bfd *dynobj = NULL; 3937 bfd_vma *local_got_offsets; 3938 asection *sgot = NULL; 3939 asection *sgotplt = NULL; 3940 asection *splt = NULL; 3941 asection *sreloc = NULL; 3942 asection *srelgot = NULL; 3943 bfd_boolean is_vxworks_tls; 3944 unsigned isec_segment, got_segment, plt_segment, check_segment[2]; 3945 bfd_boolean fdpic_p = FALSE; 3946 3947 BFD_ASSERT (is_sh_elf (input_bfd)); 3948 3949 htab = sh_elf_hash_table (info); 3950 if (htab != NULL) 3951 { 3952 dynobj = htab->root.dynobj; 3953 sgot = htab->sgot; 3954 sgotplt = htab->sgotplt; 3955 splt = htab->splt; 3956 fdpic_p = htab->fdpic_p; 3957 } 3958 symtab_hdr = &elf_symtab_hdr (input_bfd); 3959 sym_hashes = elf_sym_hashes (input_bfd); 3960 local_got_offsets = elf_local_got_offsets (input_bfd); 3961 3962 isec_segment = sh_elf_osec_to_segment (output_bfd, 3963 input_section->output_section); 3964 if (fdpic_p && sgot) 3965 got_segment = sh_elf_osec_to_segment (output_bfd, 3966 sgot->output_section); 3967 else 3968 got_segment = -1; 3969 if (fdpic_p && splt) 3970 plt_segment = sh_elf_osec_to_segment (output_bfd, 3971 splt->output_section); 3972 else 3973 plt_segment = -1; 3974 3975 /* We have to handle relocations in vxworks .tls_vars sections 3976 specially, because the dynamic loader is 'weird'. */ 3977 is_vxworks_tls = (htab && htab->vxworks_p && info->shared 3978 && !strcmp (input_section->output_section->name, 3979 ".tls_vars")); 3980 3981 rel = relocs; 3982 relend = relocs + input_section->reloc_count; 3983 for (; rel < relend; rel++) 3984 { 3985 int r_type; 3986 reloc_howto_type *howto; 3987 unsigned long r_symndx; 3988 Elf_Internal_Sym *sym; 3989 asection *sec; 3990 struct elf_link_hash_entry *h; 3991 bfd_vma relocation; 3992 bfd_vma addend = (bfd_vma) 0; 3993 bfd_reloc_status_type r; 3994 int seen_stt_datalabel = 0; 3995 bfd_vma off; 3996 int got_type; 3997 const char *symname = NULL; 3998 3999 r_symndx = ELF32_R_SYM (rel->r_info); 4000 4001 r_type = ELF32_R_TYPE (rel->r_info); 4002 4003 /* Many of the relocs are only used for relaxing, and are 4004 handled entirely by the relaxation code. */ 4005 if (r_type >= (int) R_SH_GNU_VTINHERIT 4006 && r_type <= (int) R_SH_LABEL) 4007 continue; 4008 if (r_type == (int) R_SH_NONE) 4009 continue; 4010 4011 if (r_type < 0 4012 || r_type >= R_SH_max 4013 || (r_type >= (int) R_SH_FIRST_INVALID_RELOC 4014 && r_type <= (int) R_SH_LAST_INVALID_RELOC) 4015 || (r_type >= (int) R_SH_FIRST_INVALID_RELOC_2 4016 && r_type <= (int) R_SH_LAST_INVALID_RELOC_2) 4017 || ( r_type >= (int) R_SH_FIRST_INVALID_RELOC_3 4018 && r_type <= (int) R_SH_LAST_INVALID_RELOC_3) 4019 || ( r_type >= (int) R_SH_FIRST_INVALID_RELOC_4 4020 && r_type <= (int) R_SH_LAST_INVALID_RELOC_4) 4021 || ( r_type >= (int) R_SH_FIRST_INVALID_RELOC_5 4022 && r_type <= (int) R_SH_LAST_INVALID_RELOC_5) 4023 || ( r_type >= (int) R_SH_FIRST_INVALID_RELOC_6 4024 && r_type <= (int) R_SH_LAST_INVALID_RELOC_6)) 4025 { 4026 bfd_set_error (bfd_error_bad_value); 4027 return FALSE; 4028 } 4029 4030 howto = get_howto_table (output_bfd) + r_type; 4031 4032 /* For relocs that aren't partial_inplace, we get the addend from 4033 the relocation. */ 4034 if (! howto->partial_inplace) 4035 addend = rel->r_addend; 4036 4037 h = NULL; 4038 sym = NULL; 4039 sec = NULL; 4040 check_segment[0] = -1; 4041 check_segment[1] = -1; 4042 if (r_symndx < symtab_hdr->sh_info) 4043 { 4044 sym = local_syms + r_symndx; 4045 sec = local_sections[r_symndx]; 4046 4047 symname = bfd_elf_string_from_elf_section 4048 (input_bfd, symtab_hdr->sh_link, sym->st_name); 4049 if (symname == NULL || *symname == '\0') 4050 symname = bfd_section_name (input_bfd, sec); 4051 4052 relocation = (sec->output_section->vma 4053 + sec->output_offset 4054 + sym->st_value); 4055 /* A local symbol never has STO_SH5_ISA32, so we don't need 4056 datalabel processing here. Make sure this does not change 4057 without notice. */ 4058 if ((sym->st_other & STO_SH5_ISA32) != 0) 4059 ((*info->callbacks->reloc_dangerous) 4060 (info, 4061 _("Unexpected STO_SH5_ISA32 on local symbol is not handled"), 4062 input_bfd, input_section, rel->r_offset)); 4063 4064 if (sec != NULL && elf_discarded_section (sec)) 4065 /* Handled below. */ 4066 ; 4067 else if (info->relocatable) 4068 { 4069 /* This is a relocatable link. We don't have to change 4070 anything, unless the reloc is against a section symbol, 4071 in which case we have to adjust according to where the 4072 section symbol winds up in the output section. */ 4073 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION) 4074 { 4075 if (! howto->partial_inplace) 4076 { 4077 /* For relocations with the addend in the 4078 relocation, we need just to update the addend. 4079 All real relocs are of type partial_inplace; this 4080 code is mostly for completeness. */ 4081 rel->r_addend += sec->output_offset; 4082 4083 continue; 4084 } 4085 4086 /* Relocs of type partial_inplace need to pick up the 4087 contents in the contents and add the offset resulting 4088 from the changed location of the section symbol. 4089 Using _bfd_final_link_relocate (e.g. goto 4090 final_link_relocate) here would be wrong, because 4091 relocations marked pc_relative would get the current 4092 location subtracted, and we must only do that at the 4093 final link. */ 4094 r = _bfd_relocate_contents (howto, input_bfd, 4095 sec->output_offset 4096 + sym->st_value, 4097 contents + rel->r_offset); 4098 goto relocation_done; 4099 } 4100 4101 continue; 4102 } 4103 else if (! howto->partial_inplace) 4104 { 4105 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel); 4106 addend = rel->r_addend; 4107 } 4108 else if ((sec->flags & SEC_MERGE) 4109 && ELF_ST_TYPE (sym->st_info) == STT_SECTION) 4110 { 4111 asection *msec; 4112 4113 if (howto->rightshift || howto->src_mask != 0xffffffff) 4114 { 4115 (*_bfd_error_handler) 4116 (_("%B(%A+0x%lx): %s relocation against SEC_MERGE section"), 4117 input_bfd, input_section, 4118 (long) rel->r_offset, howto->name); 4119 return FALSE; 4120 } 4121 4122 addend = bfd_get_32 (input_bfd, contents + rel->r_offset); 4123 msec = sec; 4124 addend = 4125 _bfd_elf_rel_local_sym (output_bfd, sym, &msec, addend) 4126 - relocation; 4127 addend += msec->output_section->vma + msec->output_offset; 4128 bfd_put_32 (input_bfd, addend, contents + rel->r_offset); 4129 addend = 0; 4130 } 4131 } 4132 else 4133 { 4134 /* FIXME: Ought to make use of the RELOC_FOR_GLOBAL_SYMBOL macro. */ 4135 4136 relocation = 0; 4137 h = sym_hashes[r_symndx - symtab_hdr->sh_info]; 4138 symname = h->root.root.string; 4139 while (h->root.type == bfd_link_hash_indirect 4140 || h->root.type == bfd_link_hash_warning) 4141 { 4142 #ifdef INCLUDE_SHMEDIA 4143 /* If the reference passes a symbol marked with 4144 STT_DATALABEL, then any STO_SH5_ISA32 on the final value 4145 doesn't count. */ 4146 seen_stt_datalabel |= h->type == STT_DATALABEL; 4147 #endif 4148 h = (struct elf_link_hash_entry *) h->root.u.i.link; 4149 } 4150 if (h->root.type == bfd_link_hash_defined 4151 || h->root.type == bfd_link_hash_defweak) 4152 { 4153 bfd_boolean dyn; 4154 4155 dyn = htab ? htab->root.dynamic_sections_created : FALSE; 4156 sec = h->root.u.def.section; 4157 /* In these cases, we don't need the relocation value. 4158 We check specially because in some obscure cases 4159 sec->output_section will be NULL. */ 4160 if (r_type == R_SH_GOTPC 4161 || r_type == R_SH_GOTPC_LOW16 4162 || r_type == R_SH_GOTPC_MEDLOW16 4163 || r_type == R_SH_GOTPC_MEDHI16 4164 || r_type == R_SH_GOTPC_HI16 4165 || ((r_type == R_SH_PLT32 4166 || r_type == R_SH_PLT_LOW16 4167 || r_type == R_SH_PLT_MEDLOW16 4168 || r_type == R_SH_PLT_MEDHI16 4169 || r_type == R_SH_PLT_HI16) 4170 && h->plt.offset != (bfd_vma) -1) 4171 || ((r_type == R_SH_GOT32 4172 || r_type == R_SH_GOT20 4173 || r_type == R_SH_GOTFUNCDESC 4174 || r_type == R_SH_GOTFUNCDESC20 4175 || r_type == R_SH_GOTOFFFUNCDESC 4176 || r_type == R_SH_GOTOFFFUNCDESC20 4177 || r_type == R_SH_FUNCDESC 4178 || r_type == R_SH_GOT_LOW16 4179 || r_type == R_SH_GOT_MEDLOW16 4180 || r_type == R_SH_GOT_MEDHI16 4181 || r_type == R_SH_GOT_HI16) 4182 && WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h) 4183 && (! info->shared 4184 || (! info->symbolic && h->dynindx != -1) 4185 || !h->def_regular)) 4186 /* The cases above are those in which relocation is 4187 overwritten in the switch block below. The cases 4188 below are those in which we must defer relocation 4189 to run-time, because we can't resolve absolute 4190 addresses when creating a shared library. */ 4191 || (info->shared 4192 && ((! info->symbolic && h->dynindx != -1) 4193 || !h->def_regular) 4194 && ((r_type == R_SH_DIR32 4195 && !h->forced_local) 4196 || (r_type == R_SH_REL32 4197 && !SYMBOL_CALLS_LOCAL (info, h))) 4198 && ((input_section->flags & SEC_ALLOC) != 0 4199 /* DWARF will emit R_SH_DIR32 relocations in its 4200 sections against symbols defined externally 4201 in shared libraries. We can't do anything 4202 with them here. */ 4203 || ((input_section->flags & SEC_DEBUGGING) != 0 4204 && h->def_dynamic))) 4205 /* Dynamic relocs are not propagated for SEC_DEBUGGING 4206 sections because such sections are not SEC_ALLOC and 4207 thus ld.so will not process them. */ 4208 || (sec->output_section == NULL 4209 && ((input_section->flags & SEC_DEBUGGING) != 0 4210 && h->def_dynamic)) 4211 || (sec->output_section == NULL 4212 && (sh_elf_hash_entry (h)->got_type == GOT_TLS_IE 4213 || sh_elf_hash_entry (h)->got_type == GOT_TLS_GD))) 4214 ; 4215 else if (sec->output_section != NULL) 4216 relocation = ((h->root.u.def.value 4217 + sec->output_section->vma 4218 + sec->output_offset) 4219 /* A STO_SH5_ISA32 causes a "bitor 1" to the 4220 symbol value, unless we've seen 4221 STT_DATALABEL on the way to it. */ 4222 | ((h->other & STO_SH5_ISA32) != 0 4223 && ! seen_stt_datalabel)); 4224 else if (!info->relocatable) 4225 { 4226 (*_bfd_error_handler) 4227 (_("%B(%A+0x%lx): unresolvable %s relocation against symbol `%s'"), 4228 input_bfd, 4229 input_section, 4230 (long) rel->r_offset, 4231 howto->name, 4232 h->root.root.string); 4233 return FALSE; 4234 } 4235 } 4236 else if (h->root.type == bfd_link_hash_undefweak) 4237 ; 4238 else if (info->unresolved_syms_in_objects == RM_IGNORE 4239 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT) 4240 ; 4241 else if (!info->relocatable) 4242 { 4243 if (! info->callbacks->undefined_symbol 4244 (info, h->root.root.string, input_bfd, 4245 input_section, rel->r_offset, 4246 (info->unresolved_syms_in_objects == RM_GENERATE_ERROR 4247 || ELF_ST_VISIBILITY (h->other)))) 4248 return FALSE; 4249 } 4250 } 4251 4252 if (sec != NULL && elf_discarded_section (sec)) 4253 RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section, 4254 rel, relend, howto, contents); 4255 4256 if (info->relocatable) 4257 continue; 4258 4259 /* Check for inter-segment relocations in FDPIC files. Most 4260 relocations connect the relocation site to the location of 4261 the target symbol, but there are some exceptions below. */ 4262 check_segment[0] = isec_segment; 4263 if (sec != NULL) 4264 check_segment[1] = sh_elf_osec_to_segment (output_bfd, 4265 sec->output_section); 4266 else 4267 check_segment[1] = -1; 4268 4269 switch ((int) r_type) 4270 { 4271 final_link_relocate: 4272 /* COFF relocs don't use the addend. The addend is used for 4273 R_SH_DIR32 to be compatible with other compilers. */ 4274 r = _bfd_final_link_relocate (howto, input_bfd, input_section, 4275 contents, rel->r_offset, 4276 relocation, addend); 4277 break; 4278 4279 case R_SH_IND12W: 4280 goto final_link_relocate; 4281 4282 case R_SH_DIR8WPN: 4283 case R_SH_DIR8WPZ: 4284 case R_SH_DIR8WPL: 4285 /* If the reloc is against the start of this section, then 4286 the assembler has already taken care of it and the reloc 4287 is here only to assist in relaxing. If the reloc is not 4288 against the start of this section, then it's against an 4289 external symbol and we must deal with it ourselves. */ 4290 if (input_section->output_section->vma + input_section->output_offset 4291 != relocation) 4292 { 4293 int disp = (relocation 4294 - input_section->output_section->vma 4295 - input_section->output_offset 4296 - rel->r_offset); 4297 int mask = 0; 4298 switch (r_type) 4299 { 4300 case R_SH_DIR8WPN: 4301 case R_SH_DIR8WPZ: mask = 1; break; 4302 case R_SH_DIR8WPL: mask = 3; break; 4303 default: mask = 0; break; 4304 } 4305 if (disp & mask) 4306 { 4307 ((*_bfd_error_handler) 4308 (_("%B: 0x%lx: fatal: unaligned branch target for relax-support relocation"), 4309 input_section->owner, 4310 (unsigned long) rel->r_offset)); 4311 bfd_set_error (bfd_error_bad_value); 4312 return FALSE; 4313 } 4314 relocation -= 4; 4315 goto final_link_relocate; 4316 } 4317 r = bfd_reloc_ok; 4318 break; 4319 4320 default: 4321 #ifdef INCLUDE_SHMEDIA 4322 if (shmedia_prepare_reloc (info, input_bfd, input_section, 4323 contents, rel, &relocation)) 4324 goto final_link_relocate; 4325 #endif 4326 bfd_set_error (bfd_error_bad_value); 4327 return FALSE; 4328 4329 case R_SH_DIR16: 4330 case R_SH_DIR8: 4331 case R_SH_DIR8U: 4332 case R_SH_DIR8S: 4333 case R_SH_DIR4U: 4334 goto final_link_relocate; 4335 4336 case R_SH_DIR8UL: 4337 case R_SH_DIR4UL: 4338 if (relocation & 3) 4339 { 4340 ((*_bfd_error_handler) 4341 (_("%B: 0x%lx: fatal: unaligned %s relocation 0x%lx"), 4342 input_section->owner, 4343 (unsigned long) rel->r_offset, howto->name, 4344 (unsigned long) relocation)); 4345 bfd_set_error (bfd_error_bad_value); 4346 return FALSE; 4347 } 4348 goto final_link_relocate; 4349 4350 case R_SH_DIR8UW: 4351 case R_SH_DIR8SW: 4352 case R_SH_DIR4UW: 4353 if (relocation & 1) 4354 { 4355 ((*_bfd_error_handler) 4356 (_("%B: 0x%lx: fatal: unaligned %s relocation 0x%lx"), 4357 input_section->owner, 4358 (unsigned long) rel->r_offset, howto->name, 4359 (unsigned long) relocation)); 4360 bfd_set_error (bfd_error_bad_value); 4361 return FALSE; 4362 } 4363 goto final_link_relocate; 4364 4365 case R_SH_PSHA: 4366 if ((signed int)relocation < -32 4367 || (signed int)relocation > 32) 4368 { 4369 ((*_bfd_error_handler) 4370 (_("%B: 0x%lx: fatal: R_SH_PSHA relocation %d not in range -32..32"), 4371 input_section->owner, 4372 (unsigned long) rel->r_offset, 4373 (unsigned long) relocation)); 4374 bfd_set_error (bfd_error_bad_value); 4375 return FALSE; 4376 } 4377 goto final_link_relocate; 4378 4379 case R_SH_PSHL: 4380 if ((signed int)relocation < -16 4381 || (signed int)relocation > 16) 4382 { 4383 ((*_bfd_error_handler) 4384 (_("%B: 0x%lx: fatal: R_SH_PSHL relocation %d not in range -32..32"), 4385 input_section->owner, 4386 (unsigned long) rel->r_offset, 4387 (unsigned long) relocation)); 4388 bfd_set_error (bfd_error_bad_value); 4389 return FALSE; 4390 } 4391 goto final_link_relocate; 4392 4393 case R_SH_DIR32: 4394 case R_SH_REL32: 4395 #ifdef INCLUDE_SHMEDIA 4396 case R_SH_IMM_LOW16_PCREL: 4397 case R_SH_IMM_MEDLOW16_PCREL: 4398 case R_SH_IMM_MEDHI16_PCREL: 4399 case R_SH_IMM_HI16_PCREL: 4400 #endif 4401 if (info->shared 4402 && (h == NULL 4403 || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT 4404 || h->root.type != bfd_link_hash_undefweak) 4405 && r_symndx != STN_UNDEF 4406 && (input_section->flags & SEC_ALLOC) != 0 4407 && !is_vxworks_tls 4408 && (r_type == R_SH_DIR32 4409 || !SYMBOL_CALLS_LOCAL (info, h))) 4410 { 4411 Elf_Internal_Rela outrel; 4412 bfd_byte *loc; 4413 bfd_boolean skip, relocate; 4414 4415 /* When generating a shared object, these relocations 4416 are copied into the output file to be resolved at run 4417 time. */ 4418 4419 if (sreloc == NULL) 4420 { 4421 sreloc = _bfd_elf_get_dynamic_reloc_section 4422 (input_bfd, input_section, /*rela?*/ TRUE); 4423 if (sreloc == NULL) 4424 return FALSE; 4425 } 4426 4427 skip = FALSE; 4428 relocate = FALSE; 4429 4430 outrel.r_offset = 4431 _bfd_elf_section_offset (output_bfd, info, input_section, 4432 rel->r_offset); 4433 if (outrel.r_offset == (bfd_vma) -1) 4434 skip = TRUE; 4435 else if (outrel.r_offset == (bfd_vma) -2) 4436 skip = TRUE, relocate = TRUE; 4437 outrel.r_offset += (input_section->output_section->vma 4438 + input_section->output_offset); 4439 4440 if (skip) 4441 memset (&outrel, 0, sizeof outrel); 4442 else if (r_type == R_SH_REL32) 4443 { 4444 BFD_ASSERT (h != NULL && h->dynindx != -1); 4445 outrel.r_info = ELF32_R_INFO (h->dynindx, R_SH_REL32); 4446 outrel.r_addend 4447 = (howto->partial_inplace 4448 ? bfd_get_32 (input_bfd, contents + rel->r_offset) 4449 : addend); 4450 } 4451 #ifdef INCLUDE_SHMEDIA 4452 else if (r_type == R_SH_IMM_LOW16_PCREL 4453 || r_type == R_SH_IMM_MEDLOW16_PCREL 4454 || r_type == R_SH_IMM_MEDHI16_PCREL 4455 || r_type == R_SH_IMM_HI16_PCREL) 4456 { 4457 BFD_ASSERT (h != NULL && h->dynindx != -1); 4458 outrel.r_info = ELF32_R_INFO (h->dynindx, r_type); 4459 outrel.r_addend = addend; 4460 } 4461 #endif 4462 else if (fdpic_p 4463 && (h == NULL 4464 || ((info->symbolic || h->dynindx == -1) 4465 && h->def_regular))) 4466 { 4467 int dynindx; 4468 4469 BFD_ASSERT (sec != NULL); 4470 BFD_ASSERT (sec->output_section != NULL); 4471 dynindx = elf_section_data (sec->output_section)->dynindx; 4472 outrel.r_info = ELF32_R_INFO (dynindx, R_SH_DIR32); 4473 outrel.r_addend = relocation; 4474 outrel.r_addend 4475 += (howto->partial_inplace 4476 ? bfd_get_32 (input_bfd, contents + rel->r_offset) 4477 : addend); 4478 outrel.r_addend -= sec->output_section->vma; 4479 } 4480 else 4481 { 4482 /* h->dynindx may be -1 if this symbol was marked to 4483 become local. */ 4484 if (h == NULL 4485 || ((info->symbolic || h->dynindx == -1) 4486 && h->def_regular)) 4487 { 4488 relocate = howto->partial_inplace; 4489 outrel.r_info = ELF32_R_INFO (0, R_SH_RELATIVE); 4490 } 4491 else 4492 { 4493 BFD_ASSERT (h->dynindx != -1); 4494 outrel.r_info = ELF32_R_INFO (h->dynindx, R_SH_DIR32); 4495 } 4496 outrel.r_addend = relocation; 4497 outrel.r_addend 4498 += (howto->partial_inplace 4499 ? bfd_get_32 (input_bfd, contents + rel->r_offset) 4500 : addend); 4501 } 4502 4503 loc = sreloc->contents; 4504 loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rela); 4505 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc); 4506 4507 check_segment[0] = check_segment[1] = -1; 4508 4509 /* If this reloc is against an external symbol, we do 4510 not want to fiddle with the addend. Otherwise, we 4511 need to include the symbol value so that it becomes 4512 an addend for the dynamic reloc. */ 4513 if (! relocate) 4514 continue; 4515 } 4516 else if (fdpic_p && !info->shared 4517 && r_type == R_SH_DIR32 4518 && (input_section->flags & SEC_ALLOC) != 0) 4519 { 4520 bfd_vma offset; 4521 4522 BFD_ASSERT (htab); 4523 4524 if (sh_elf_osec_readonly_p (output_bfd, 4525 input_section->output_section)) 4526 { 4527 (*_bfd_error_handler) 4528 (_("%B(%A+0x%lx): cannot emit fixup to `%s' in read-only section"), 4529 input_bfd, 4530 input_section, 4531 (long) rel->r_offset, 4532 symname); 4533 return FALSE; 4534 } 4535 4536 offset = _bfd_elf_section_offset (output_bfd, info, 4537 input_section, rel->r_offset); 4538 if (offset != (bfd_vma)-1) 4539 sh_elf_add_rofixup (output_bfd, htab->srofixup, 4540 input_section->output_section->vma 4541 + input_section->output_offset 4542 + rel->r_offset); 4543 4544 check_segment[0] = check_segment[1] = -1; 4545 } 4546 goto final_link_relocate; 4547 4548 case R_SH_GOTPLT32: 4549 #ifdef INCLUDE_SHMEDIA 4550 case R_SH_GOTPLT_LOW16: 4551 case R_SH_GOTPLT_MEDLOW16: 4552 case R_SH_GOTPLT_MEDHI16: 4553 case R_SH_GOTPLT_HI16: 4554 case R_SH_GOTPLT10BY4: 4555 case R_SH_GOTPLT10BY8: 4556 #endif 4557 /* Relocation is to the entry for this symbol in the 4558 procedure linkage table. */ 4559 4560 if (h == NULL 4561 || h->forced_local 4562 || ! info->shared 4563 || info->symbolic 4564 || h->dynindx == -1 4565 || h->plt.offset == (bfd_vma) -1 4566 || h->got.offset != (bfd_vma) -1) 4567 goto force_got; 4568 4569 /* Relocation is to the entry for this symbol in the global 4570 offset table extension for the procedure linkage table. */ 4571 4572 BFD_ASSERT (htab); 4573 BFD_ASSERT (sgotplt != NULL); 4574 relocation = (sgotplt->output_offset 4575 + (get_plt_index (htab->plt_info, h->plt.offset) 4576 + 3) * 4); 4577 4578 #ifdef GOT_BIAS 4579 relocation -= GOT_BIAS; 4580 #endif 4581 4582 goto final_link_relocate; 4583 4584 force_got: 4585 case R_SH_GOT32: 4586 case R_SH_GOT20: 4587 #ifdef INCLUDE_SHMEDIA 4588 case R_SH_GOT_LOW16: 4589 case R_SH_GOT_MEDLOW16: 4590 case R_SH_GOT_MEDHI16: 4591 case R_SH_GOT_HI16: 4592 case R_SH_GOT10BY4: 4593 case R_SH_GOT10BY8: 4594 #endif 4595 /* Relocation is to the entry for this symbol in the global 4596 offset table. */ 4597 4598 BFD_ASSERT (htab); 4599 BFD_ASSERT (sgot != NULL); 4600 check_segment[0] = check_segment[1] = -1; 4601 4602 if (h != NULL) 4603 { 4604 bfd_boolean dyn; 4605 4606 off = h->got.offset; 4607 #ifdef INCLUDE_SHMEDIA 4608 if (seen_stt_datalabel) 4609 { 4610 struct elf_sh_link_hash_entry *hsh; 4611 4612 hsh = (struct elf_sh_link_hash_entry *)h; 4613 off = hsh->datalabel_got.offset; 4614 } 4615 #endif 4616 BFD_ASSERT (off != (bfd_vma) -1); 4617 4618 dyn = htab->root.dynamic_sections_created; 4619 if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h) 4620 || (info->shared 4621 && SYMBOL_REFERENCES_LOCAL (info, h)) 4622 || (ELF_ST_VISIBILITY (h->other) 4623 && h->root.type == bfd_link_hash_undefweak)) 4624 { 4625 /* This is actually a static link, or it is a 4626 -Bsymbolic link and the symbol is defined 4627 locally, or the symbol was forced to be local 4628 because of a version file. We must initialize 4629 this entry in the global offset table. Since the 4630 offset must always be a multiple of 4, we use the 4631 least significant bit to record whether we have 4632 initialized it already. 4633 4634 When doing a dynamic link, we create a .rela.got 4635 relocation entry to initialize the value. This 4636 is done in the finish_dynamic_symbol routine. */ 4637 if ((off & 1) != 0) 4638 off &= ~1; 4639 else 4640 { 4641 bfd_put_32 (output_bfd, relocation, 4642 sgot->contents + off); 4643 #ifdef INCLUDE_SHMEDIA 4644 if (seen_stt_datalabel) 4645 { 4646 struct elf_sh_link_hash_entry *hsh; 4647 4648 hsh = (struct elf_sh_link_hash_entry *)h; 4649 hsh->datalabel_got.offset |= 1; 4650 } 4651 else 4652 #endif 4653 h->got.offset |= 1; 4654 4655 /* If we initialize the GOT entry here with a valid 4656 symbol address, also add a fixup. */ 4657 if (fdpic_p && !info->shared 4658 && sh_elf_hash_entry (h)->got_type == GOT_NORMAL 4659 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT 4660 || h->root.type != bfd_link_hash_undefweak)) 4661 sh_elf_add_rofixup (output_bfd, htab->srofixup, 4662 sgot->output_section->vma 4663 + sgot->output_offset 4664 + off); 4665 } 4666 } 4667 4668 relocation = sh_elf_got_offset (htab) + off; 4669 } 4670 else 4671 { 4672 #ifdef INCLUDE_SHMEDIA 4673 if (rel->r_addend) 4674 { 4675 BFD_ASSERT (local_got_offsets != NULL 4676 && (local_got_offsets[symtab_hdr->sh_info 4677 + r_symndx] 4678 != (bfd_vma) -1)); 4679 4680 off = local_got_offsets[symtab_hdr->sh_info 4681 + r_symndx]; 4682 } 4683 else 4684 { 4685 #endif 4686 BFD_ASSERT (local_got_offsets != NULL 4687 && local_got_offsets[r_symndx] != (bfd_vma) -1); 4688 4689 off = local_got_offsets[r_symndx]; 4690 #ifdef INCLUDE_SHMEDIA 4691 } 4692 #endif 4693 4694 /* The offset must always be a multiple of 4. We use 4695 the least significant bit to record whether we have 4696 already generated the necessary reloc. */ 4697 if ((off & 1) != 0) 4698 off &= ~1; 4699 else 4700 { 4701 bfd_put_32 (output_bfd, relocation, sgot->contents + off); 4702 4703 if (info->shared) 4704 { 4705 Elf_Internal_Rela outrel; 4706 bfd_byte *loc; 4707 4708 if (srelgot == NULL) 4709 { 4710 srelgot = bfd_get_section_by_name (dynobj, 4711 ".rela.got"); 4712 BFD_ASSERT (srelgot != NULL); 4713 } 4714 4715 outrel.r_offset = (sgot->output_section->vma 4716 + sgot->output_offset 4717 + off); 4718 if (fdpic_p) 4719 { 4720 int dynindx 4721 = elf_section_data (sec->output_section)->dynindx; 4722 outrel.r_info = ELF32_R_INFO (dynindx, R_SH_DIR32); 4723 outrel.r_addend = relocation; 4724 outrel.r_addend -= sec->output_section->vma; 4725 } 4726 else 4727 { 4728 outrel.r_info = ELF32_R_INFO (0, R_SH_RELATIVE); 4729 outrel.r_addend = relocation; 4730 } 4731 loc = srelgot->contents; 4732 loc += srelgot->reloc_count++ * sizeof (Elf32_External_Rela); 4733 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc); 4734 } 4735 else if (fdpic_p 4736 && (sh_elf_local_got_type (input_bfd) [r_symndx] 4737 == GOT_NORMAL)) 4738 sh_elf_add_rofixup (output_bfd, htab->srofixup, 4739 sgot->output_section->vma 4740 + sgot->output_offset 4741 + off); 4742 4743 #ifdef INCLUDE_SHMEDIA 4744 if (rel->r_addend) 4745 local_got_offsets[symtab_hdr->sh_info + r_symndx] |= 1; 4746 else 4747 #endif 4748 local_got_offsets[r_symndx] |= 1; 4749 } 4750 4751 relocation = sh_elf_got_offset (htab) + off; 4752 } 4753 4754 #ifdef GOT_BIAS 4755 relocation -= GOT_BIAS; 4756 #endif 4757 4758 if (r_type == R_SH_GOT20) 4759 { 4760 r = install_movi20_field (output_bfd, relocation + addend, 4761 input_bfd, input_section, contents, 4762 rel->r_offset); 4763 break; 4764 } 4765 else 4766 goto final_link_relocate; 4767 4768 case R_SH_GOTOFF: 4769 case R_SH_GOTOFF20: 4770 #ifdef INCLUDE_SHMEDIA 4771 case R_SH_GOTOFF_LOW16: 4772 case R_SH_GOTOFF_MEDLOW16: 4773 case R_SH_GOTOFF_MEDHI16: 4774 case R_SH_GOTOFF_HI16: 4775 #endif 4776 /* GOTOFF relocations are relative to _GLOBAL_OFFSET_TABLE_, which 4777 we place at the start of the .got.plt section. This is the same 4778 as the start of the output .got section, unless there are function 4779 descriptors in front of it. */ 4780 BFD_ASSERT (htab); 4781 BFD_ASSERT (sgotplt != NULL); 4782 check_segment[0] = got_segment; 4783 relocation -= sgotplt->output_section->vma + sgotplt->output_offset 4784 + htab->root.hgot->root.u.def.value; 4785 4786 #ifdef GOT_BIAS 4787 relocation -= GOT_BIAS; 4788 #endif 4789 4790 addend = rel->r_addend; 4791 4792 if (r_type == R_SH_GOTOFF20) 4793 { 4794 r = install_movi20_field (output_bfd, relocation + addend, 4795 input_bfd, input_section, contents, 4796 rel->r_offset); 4797 break; 4798 } 4799 else 4800 goto final_link_relocate; 4801 4802 case R_SH_GOTPC: 4803 #ifdef INCLUDE_SHMEDIA 4804 case R_SH_GOTPC_LOW16: 4805 case R_SH_GOTPC_MEDLOW16: 4806 case R_SH_GOTPC_MEDHI16: 4807 case R_SH_GOTPC_HI16: 4808 #endif 4809 /* Use global offset table as symbol value. */ 4810 4811 BFD_ASSERT (sgotplt != NULL); 4812 relocation = sgotplt->output_section->vma + sgotplt->output_offset; 4813 4814 #ifdef GOT_BIAS 4815 relocation += GOT_BIAS; 4816 #endif 4817 4818 addend = rel->r_addend; 4819 4820 goto final_link_relocate; 4821 4822 case R_SH_PLT32: 4823 #ifdef INCLUDE_SHMEDIA 4824 case R_SH_PLT_LOW16: 4825 case R_SH_PLT_MEDLOW16: 4826 case R_SH_PLT_MEDHI16: 4827 case R_SH_PLT_HI16: 4828 #endif 4829 /* Relocation is to the entry for this symbol in the 4830 procedure linkage table. */ 4831 4832 /* Resolve a PLT reloc against a local symbol directly, 4833 without using the procedure linkage table. */ 4834 if (h == NULL) 4835 goto final_link_relocate; 4836 4837 /* We don't want to warn on calls to undefined weak symbols, 4838 as calls to them must be protected by non-NULL tests 4839 anyway, and unprotected calls would invoke undefined 4840 behavior. */ 4841 if (h->root.type == bfd_link_hash_undefweak) 4842 check_segment[0] = check_segment[1] = -1; 4843 4844 if (h->forced_local) 4845 goto final_link_relocate; 4846 4847 if (h->plt.offset == (bfd_vma) -1) 4848 { 4849 /* We didn't make a PLT entry for this symbol. This 4850 happens when statically linking PIC code, or when 4851 using -Bsymbolic. */ 4852 goto final_link_relocate; 4853 } 4854 4855 BFD_ASSERT (splt != NULL); 4856 check_segment[1] = plt_segment; 4857 relocation = (splt->output_section->vma 4858 + splt->output_offset 4859 + h->plt.offset); 4860 4861 #ifdef INCLUDE_SHMEDIA 4862 relocation++; 4863 #endif 4864 4865 addend = rel->r_addend; 4866 4867 goto final_link_relocate; 4868 4869 /* Relocation is to the canonical function descriptor for this 4870 symbol, possibly via the GOT. Initialize the GOT 4871 entry and function descriptor if necessary. */ 4872 case R_SH_GOTFUNCDESC: 4873 case R_SH_GOTFUNCDESC20: 4874 case R_SH_FUNCDESC: 4875 { 4876 int dynindx = -1; 4877 asection *reloc_section; 4878 bfd_vma reloc_offset; 4879 int reloc_type = R_SH_FUNCDESC; 4880 4881 BFD_ASSERT (htab); 4882 4883 check_segment[0] = check_segment[1] = -1; 4884 4885 /* FIXME: See what FRV does for global symbols in the 4886 executable, with --export-dynamic. Do they need ld.so 4887 to allocate official descriptors? See what this code 4888 does. */ 4889 4890 relocation = 0; 4891 addend = 0; 4892 4893 if (r_type == R_SH_FUNCDESC) 4894 { 4895 reloc_section = input_section; 4896 reloc_offset = rel->r_offset; 4897 } 4898 else 4899 { 4900 reloc_section = sgot; 4901 4902 if (h != NULL) 4903 reloc_offset = h->got.offset; 4904 else 4905 { 4906 BFD_ASSERT (local_got_offsets != NULL); 4907 reloc_offset = local_got_offsets[r_symndx]; 4908 } 4909 BFD_ASSERT (reloc_offset != MINUS_ONE); 4910 4911 if (reloc_offset & 1) 4912 { 4913 reloc_offset &= ~1; 4914 goto funcdesc_done_got; 4915 } 4916 } 4917 4918 if (h && h->root.type == bfd_link_hash_undefweak 4919 && (SYMBOL_CALLS_LOCAL (info, h) 4920 || !htab->root.dynamic_sections_created)) 4921 /* Undefined weak symbol which will not be dynamically 4922 resolved later; leave it at zero. */ 4923 goto funcdesc_leave_zero; 4924 else if (SYMBOL_CALLS_LOCAL (info, h) 4925 && ! SYMBOL_FUNCDESC_LOCAL (info, h)) 4926 { 4927 /* If the symbol needs a non-local function descriptor 4928 but binds locally (i.e., its visibility is 4929 protected), emit a dynamic relocation decayed to 4930 section+offset. This is an optimization; the dynamic 4931 linker would resolve our function descriptor request 4932 to our copy of the function anyway. */ 4933 dynindx = elf_section_data (h->root.u.def.section 4934 ->output_section)->dynindx; 4935 relocation += h->root.u.def.section->output_offset 4936 + h->root.u.def.value; 4937 } 4938 else if (! SYMBOL_FUNCDESC_LOCAL (info, h)) 4939 { 4940 /* If the symbol is dynamic and there will be dynamic 4941 symbol resolution because we are or are linked with a 4942 shared library, emit a FUNCDESC relocation such that 4943 the dynamic linker will allocate the function 4944 descriptor. */ 4945 BFD_ASSERT (h->dynindx != -1); 4946 dynindx = h->dynindx; 4947 } 4948 else 4949 { 4950 bfd_vma offset; 4951 4952 /* Otherwise, we know we have a private function 4953 descriptor, so reference it directly. */ 4954 reloc_type = R_SH_DIR32; 4955 dynindx = elf_section_data (htab->sfuncdesc 4956 ->output_section)->dynindx; 4957 4958 if (h) 4959 { 4960 offset = sh_elf_hash_entry (h)->funcdesc.offset; 4961 BFD_ASSERT (offset != MINUS_ONE); 4962 if ((offset & 1) == 0) 4963 { 4964 if (!sh_elf_initialize_funcdesc (output_bfd, info, h, 4965 offset, NULL, 0)) 4966 return FALSE; 4967 sh_elf_hash_entry (h)->funcdesc.offset |= 1; 4968 } 4969 } 4970 else 4971 { 4972 union gotref *local_funcdesc; 4973 4974 local_funcdesc = sh_elf_local_funcdesc (input_bfd); 4975 offset = local_funcdesc[r_symndx].offset; 4976 BFD_ASSERT (offset != MINUS_ONE); 4977 if ((offset & 1) == 0) 4978 { 4979 if (!sh_elf_initialize_funcdesc (output_bfd, info, NULL, 4980 offset, sec, 4981 sym->st_value)) 4982 return FALSE; 4983 local_funcdesc[r_symndx].offset |= 1; 4984 } 4985 } 4986 4987 relocation = htab->sfuncdesc->output_offset + (offset & ~1); 4988 } 4989 4990 if (!info->shared && SYMBOL_FUNCDESC_LOCAL (info, h)) 4991 { 4992 bfd_vma offset; 4993 4994 if (sh_elf_osec_readonly_p (output_bfd, 4995 reloc_section->output_section)) 4996 { 4997 (*_bfd_error_handler) 4998 (_("%B(%A+0x%lx): cannot emit fixup to `%s' in read-only section"), 4999 input_bfd, 5000 input_section, 5001 (long) rel->r_offset, 5002 symname); 5003 return FALSE; 5004 } 5005 5006 offset = _bfd_elf_section_offset (output_bfd, info, 5007 reloc_section, reloc_offset); 5008 5009 if (offset != (bfd_vma)-1) 5010 sh_elf_add_rofixup (output_bfd, htab->srofixup, 5011 offset 5012 + reloc_section->output_section->vma 5013 + reloc_section->output_offset); 5014 } 5015 else if ((reloc_section->output_section->flags 5016 & (SEC_ALLOC | SEC_LOAD)) == (SEC_ALLOC | SEC_LOAD)) 5017 { 5018 bfd_vma offset; 5019 5020 if (sh_elf_osec_readonly_p (output_bfd, 5021 reloc_section->output_section)) 5022 { 5023 info->callbacks->warning 5024 (info, 5025 _("cannot emit dynamic relocations in read-only section"), 5026 symname, input_bfd, reloc_section, reloc_offset); 5027 return FALSE; 5028 } 5029 5030 if (srelgot == NULL) 5031 { 5032 srelgot = bfd_get_section_by_name (dynobj, ".rela.got"); 5033 BFD_ASSERT (srelgot != NULL); 5034 } 5035 5036 offset = _bfd_elf_section_offset (output_bfd, info, 5037 reloc_section, reloc_offset); 5038 5039 if (offset != (bfd_vma)-1) 5040 sh_elf_add_dyn_reloc (output_bfd, srelgot, 5041 offset 5042 + reloc_section->output_section->vma 5043 + reloc_section->output_offset, 5044 reloc_type, dynindx, relocation); 5045 5046 if (r_type == R_SH_FUNCDESC) 5047 { 5048 r = bfd_reloc_ok; 5049 break; 5050 } 5051 else 5052 { 5053 relocation = 0; 5054 goto funcdesc_leave_zero; 5055 } 5056 } 5057 5058 if (SYMBOL_FUNCDESC_LOCAL (info, h)) 5059 relocation += htab->sfuncdesc->output_section->vma; 5060 funcdesc_leave_zero: 5061 if (r_type != R_SH_FUNCDESC) 5062 { 5063 bfd_put_32 (output_bfd, relocation, 5064 reloc_section->contents + reloc_offset); 5065 if (h != NULL) 5066 h->got.offset |= 1; 5067 else 5068 local_got_offsets[r_symndx] |= 1; 5069 5070 funcdesc_done_got: 5071 5072 relocation = sh_elf_got_offset (htab) + reloc_offset; 5073 #ifdef GOT_BIAS 5074 relocation -= GOT_BIAS; 5075 #endif 5076 } 5077 if (r_type == R_SH_GOTFUNCDESC20) 5078 { 5079 r = install_movi20_field (output_bfd, relocation + addend, 5080 input_bfd, input_section, contents, 5081 rel->r_offset); 5082 break; 5083 } 5084 else 5085 goto final_link_relocate; 5086 } 5087 break; 5088 5089 case R_SH_GOTOFFFUNCDESC: 5090 case R_SH_GOTOFFFUNCDESC20: 5091 /* FIXME: See R_SH_FUNCDESC comment about global symbols in the 5092 executable and --export-dynamic. If such symbols get 5093 ld.so-allocated descriptors we can not use R_SH_GOTOFFFUNCDESC 5094 for them. */ 5095 BFD_ASSERT (htab); 5096 5097 check_segment[0] = check_segment[1] = -1; 5098 relocation = 0; 5099 addend = rel->r_addend; 5100 5101 if (h && (h->root.type == bfd_link_hash_undefweak 5102 || !SYMBOL_FUNCDESC_LOCAL (info, h))) 5103 { 5104 _bfd_error_handler 5105 (_("%B(%A+0x%lx): %s relocation against external symbol \"%s\""), 5106 input_bfd, input_section, (long) rel->r_offset, howto->name, 5107 h->root.root.string); 5108 return FALSE; 5109 } 5110 else 5111 { 5112 bfd_vma offset; 5113 5114 /* Otherwise, we know we have a private function 5115 descriptor, so reference it directly. */ 5116 if (h) 5117 { 5118 offset = sh_elf_hash_entry (h)->funcdesc.offset; 5119 BFD_ASSERT (offset != MINUS_ONE); 5120 if ((offset & 1) == 0) 5121 { 5122 if (!sh_elf_initialize_funcdesc (output_bfd, info, h, 5123 offset, NULL, 0)) 5124 return FALSE; 5125 sh_elf_hash_entry (h)->funcdesc.offset |= 1; 5126 } 5127 } 5128 else 5129 { 5130 union gotref *local_funcdesc; 5131 5132 local_funcdesc = sh_elf_local_funcdesc (input_bfd); 5133 offset = local_funcdesc[r_symndx].offset; 5134 BFD_ASSERT (offset != MINUS_ONE); 5135 if ((offset & 1) == 0) 5136 { 5137 if (!sh_elf_initialize_funcdesc (output_bfd, info, NULL, 5138 offset, sec, 5139 sym->st_value)) 5140 return FALSE; 5141 local_funcdesc[r_symndx].offset |= 1; 5142 } 5143 } 5144 5145 relocation = htab->sfuncdesc->output_offset + (offset & ~1); 5146 } 5147 5148 relocation -= (htab->root.hgot->root.u.def.value 5149 + sgotplt->output_offset); 5150 #ifdef GOT_BIAS 5151 relocation -= GOT_BIAS; 5152 #endif 5153 5154 if (r_type == R_SH_GOTOFFFUNCDESC20) 5155 { 5156 r = install_movi20_field (output_bfd, relocation + addend, 5157 input_bfd, input_section, contents, 5158 rel->r_offset); 5159 break; 5160 } 5161 else 5162 goto final_link_relocate; 5163 5164 case R_SH_LOOP_START: 5165 { 5166 static bfd_vma start, end; 5167 5168 start = (relocation + rel->r_addend 5169 - (sec->output_section->vma + sec->output_offset)); 5170 r = sh_elf_reloc_loop (r_type, input_bfd, input_section, contents, 5171 rel->r_offset, sec, start, end); 5172 break; 5173 5174 case R_SH_LOOP_END: 5175 end = (relocation + rel->r_addend 5176 - (sec->output_section->vma + sec->output_offset)); 5177 r = sh_elf_reloc_loop (r_type, input_bfd, input_section, contents, 5178 rel->r_offset, sec, start, end); 5179 break; 5180 } 5181 5182 case R_SH_TLS_GD_32: 5183 case R_SH_TLS_IE_32: 5184 BFD_ASSERT (htab); 5185 check_segment[0] = check_segment[1] = -1; 5186 r_type = sh_elf_optimized_tls_reloc (info, r_type, h == NULL); 5187 got_type = GOT_UNKNOWN; 5188 if (h == NULL && local_got_offsets) 5189 got_type = sh_elf_local_got_type (input_bfd) [r_symndx]; 5190 else if (h != NULL) 5191 { 5192 got_type = sh_elf_hash_entry (h)->got_type; 5193 if (! info->shared 5194 && (h->dynindx == -1 5195 || h->def_regular)) 5196 r_type = R_SH_TLS_LE_32; 5197 } 5198 5199 if (r_type == R_SH_TLS_GD_32 && got_type == GOT_TLS_IE) 5200 r_type = R_SH_TLS_IE_32; 5201 5202 if (r_type == R_SH_TLS_LE_32) 5203 { 5204 bfd_vma offset; 5205 unsigned short insn; 5206 5207 if (ELF32_R_TYPE (rel->r_info) == R_SH_TLS_GD_32) 5208 { 5209 /* GD->LE transition: 5210 mov.l 1f,r4; mova 2f,r0; mov.l 2f,r1; add r0,r1; 5211 jsr @r1; add r12,r4; bra 3f; nop; .align 2; 5212 1: .long x$TLSGD; 2: .long __tls_get_addr@PLT; 3: 5213 We change it into: 5214 mov.l 1f,r4; stc gbr,r0; add r4,r0; nop; 5215 nop; nop; ... 5216 1: .long x@TPOFF; 2: .long __tls_get_addr@PLT; 3:. */ 5217 5218 offset = rel->r_offset; 5219 BFD_ASSERT (offset >= 16); 5220 /* Size of GD instructions is 16 or 18. */ 5221 offset -= 16; 5222 insn = bfd_get_16 (input_bfd, contents + offset + 0); 5223 if ((insn & 0xff00) == 0xc700) 5224 { 5225 BFD_ASSERT (offset >= 2); 5226 offset -= 2; 5227 insn = bfd_get_16 (input_bfd, contents + offset + 0); 5228 } 5229 5230 BFD_ASSERT ((insn & 0xff00) == 0xd400); 5231 insn = bfd_get_16 (input_bfd, contents + offset + 2); 5232 BFD_ASSERT ((insn & 0xff00) == 0xc700); 5233 insn = bfd_get_16 (input_bfd, contents + offset + 4); 5234 BFD_ASSERT ((insn & 0xff00) == 0xd100); 5235 insn = bfd_get_16 (input_bfd, contents + offset + 6); 5236 BFD_ASSERT (insn == 0x310c); 5237 insn = bfd_get_16 (input_bfd, contents + offset + 8); 5238 BFD_ASSERT (insn == 0x410b); 5239 insn = bfd_get_16 (input_bfd, contents + offset + 10); 5240 BFD_ASSERT (insn == 0x34cc); 5241 5242 bfd_put_16 (output_bfd, 0x0012, contents + offset + 2); 5243 bfd_put_16 (output_bfd, 0x304c, contents + offset + 4); 5244 bfd_put_16 (output_bfd, 0x0009, contents + offset + 6); 5245 bfd_put_16 (output_bfd, 0x0009, contents + offset + 8); 5246 bfd_put_16 (output_bfd, 0x0009, contents + offset + 10); 5247 } 5248 else 5249 { 5250 int target; 5251 5252 /* IE->LE transition: 5253 mov.l 1f,r0; stc gbr,rN; mov.l @(r0,r12),rM; 5254 bra 2f; add ...; .align 2; 1: x@GOTTPOFF; 2: 5255 We change it into: 5256 mov.l .Ln,rM; stc gbr,rN; nop; ...; 5257 1: x@TPOFF; 2:. */ 5258 5259 offset = rel->r_offset; 5260 BFD_ASSERT (offset >= 16); 5261 /* Size of IE instructions is 10 or 12. */ 5262 offset -= 10; 5263 insn = bfd_get_16 (input_bfd, contents + offset + 0); 5264 if ((insn & 0xf0ff) == 0x0012) 5265 { 5266 BFD_ASSERT (offset >= 2); 5267 offset -= 2; 5268 insn = bfd_get_16 (input_bfd, contents + offset + 0); 5269 } 5270 5271 BFD_ASSERT ((insn & 0xff00) == 0xd000); 5272 target = insn & 0x00ff; 5273 insn = bfd_get_16 (input_bfd, contents + offset + 2); 5274 BFD_ASSERT ((insn & 0xf0ff) == 0x0012); 5275 insn = bfd_get_16 (input_bfd, contents + offset + 4); 5276 BFD_ASSERT ((insn & 0xf0ff) == 0x00ce); 5277 insn = 0xd000 | (insn & 0x0f00) | target; 5278 bfd_put_16 (output_bfd, insn, contents + offset + 0); 5279 bfd_put_16 (output_bfd, 0x0009, contents + offset + 4); 5280 } 5281 5282 bfd_put_32 (output_bfd, tpoff (info, relocation), 5283 contents + rel->r_offset); 5284 continue; 5285 } 5286 5287 if (sgot == NULL || sgotplt == NULL) 5288 abort (); 5289 5290 if (h != NULL) 5291 off = h->got.offset; 5292 else 5293 { 5294 if (local_got_offsets == NULL) 5295 abort (); 5296 5297 off = local_got_offsets[r_symndx]; 5298 } 5299 5300 /* Relocate R_SH_TLS_IE_32 directly when statically linking. */ 5301 if (r_type == R_SH_TLS_IE_32 5302 && ! htab->root.dynamic_sections_created) 5303 { 5304 off &= ~1; 5305 bfd_put_32 (output_bfd, tpoff (info, relocation), 5306 sgot->contents + off); 5307 bfd_put_32 (output_bfd, sh_elf_got_offset (htab) + off, 5308 contents + rel->r_offset); 5309 continue; 5310 } 5311 5312 if ((off & 1) != 0) 5313 off &= ~1; 5314 else 5315 { 5316 Elf_Internal_Rela outrel; 5317 bfd_byte *loc; 5318 int dr_type, indx; 5319 5320 if (srelgot == NULL) 5321 { 5322 srelgot = bfd_get_section_by_name (dynobj, ".rela.got"); 5323 BFD_ASSERT (srelgot != NULL); 5324 } 5325 5326 outrel.r_offset = (sgot->output_section->vma 5327 + sgot->output_offset + off); 5328 5329 if (h == NULL || h->dynindx == -1) 5330 indx = 0; 5331 else 5332 indx = h->dynindx; 5333 5334 dr_type = (r_type == R_SH_TLS_GD_32 ? R_SH_TLS_DTPMOD32 : 5335 R_SH_TLS_TPOFF32); 5336 if (dr_type == R_SH_TLS_TPOFF32 && indx == 0) 5337 outrel.r_addend = relocation - dtpoff_base (info); 5338 else 5339 outrel.r_addend = 0; 5340 outrel.r_info = ELF32_R_INFO (indx, dr_type); 5341 loc = srelgot->contents; 5342 loc += srelgot->reloc_count++ * sizeof (Elf32_External_Rela); 5343 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc); 5344 5345 if (r_type == R_SH_TLS_GD_32) 5346 { 5347 if (indx == 0) 5348 { 5349 bfd_put_32 (output_bfd, 5350 relocation - dtpoff_base (info), 5351 sgot->contents + off + 4); 5352 } 5353 else 5354 { 5355 outrel.r_info = ELF32_R_INFO (indx, 5356 R_SH_TLS_DTPOFF32); 5357 outrel.r_offset += 4; 5358 outrel.r_addend = 0; 5359 srelgot->reloc_count++; 5360 loc += sizeof (Elf32_External_Rela); 5361 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc); 5362 } 5363 } 5364 5365 if (h != NULL) 5366 h->got.offset |= 1; 5367 else 5368 local_got_offsets[r_symndx] |= 1; 5369 } 5370 5371 if (off >= (bfd_vma) -2) 5372 abort (); 5373 5374 if (r_type == (int) ELF32_R_TYPE (rel->r_info)) 5375 relocation = sh_elf_got_offset (htab) + off; 5376 else 5377 { 5378 bfd_vma offset; 5379 unsigned short insn; 5380 5381 /* GD->IE transition: 5382 mov.l 1f,r4; mova 2f,r0; mov.l 2f,r1; add r0,r1; 5383 jsr @r1; add r12,r4; bra 3f; nop; .align 2; 5384 1: .long x$TLSGD; 2: .long __tls_get_addr@PLT; 3: 5385 We change it into: 5386 mov.l 1f,r0; stc gbr,r4; mov.l @(r0,r12),r0; add r4,r0; 5387 nop; nop; bra 3f; nop; .align 2; 5388 1: .long x@TPOFF; 2:...; 3:. */ 5389 5390 offset = rel->r_offset; 5391 BFD_ASSERT (offset >= 16); 5392 /* Size of GD instructions is 16 or 18. */ 5393 offset -= 16; 5394 insn = bfd_get_16 (input_bfd, contents + offset + 0); 5395 if ((insn & 0xff00) == 0xc700) 5396 { 5397 BFD_ASSERT (offset >= 2); 5398 offset -= 2; 5399 insn = bfd_get_16 (input_bfd, contents + offset + 0); 5400 } 5401 5402 BFD_ASSERT ((insn & 0xff00) == 0xd400); 5403 5404 /* Replace mov.l 1f,R4 with mov.l 1f,r0. */ 5405 bfd_put_16 (output_bfd, insn & 0xf0ff, contents + offset); 5406 5407 insn = bfd_get_16 (input_bfd, contents + offset + 2); 5408 BFD_ASSERT ((insn & 0xff00) == 0xc700); 5409 insn = bfd_get_16 (input_bfd, contents + offset + 4); 5410 BFD_ASSERT ((insn & 0xff00) == 0xd100); 5411 insn = bfd_get_16 (input_bfd, contents + offset + 6); 5412 BFD_ASSERT (insn == 0x310c); 5413 insn = bfd_get_16 (input_bfd, contents + offset + 8); 5414 BFD_ASSERT (insn == 0x410b); 5415 insn = bfd_get_16 (input_bfd, contents + offset + 10); 5416 BFD_ASSERT (insn == 0x34cc); 5417 5418 bfd_put_16 (output_bfd, 0x0412, contents + offset + 2); 5419 bfd_put_16 (output_bfd, 0x00ce, contents + offset + 4); 5420 bfd_put_16 (output_bfd, 0x304c, contents + offset + 6); 5421 bfd_put_16 (output_bfd, 0x0009, contents + offset + 8); 5422 bfd_put_16 (output_bfd, 0x0009, contents + offset + 10); 5423 5424 bfd_put_32 (output_bfd, sh_elf_got_offset (htab) + off, 5425 contents + rel->r_offset); 5426 5427 continue; 5428 } 5429 5430 addend = rel->r_addend; 5431 5432 goto final_link_relocate; 5433 5434 case R_SH_TLS_LD_32: 5435 BFD_ASSERT (htab); 5436 check_segment[0] = check_segment[1] = -1; 5437 if (! info->shared) 5438 { 5439 bfd_vma offset; 5440 unsigned short insn; 5441 5442 /* LD->LE transition: 5443 mov.l 1f,r4; mova 2f,r0; mov.l 2f,r1; add r0,r1; 5444 jsr @r1; add r12,r4; bra 3f; nop; .align 2; 5445 1: .long x$TLSLD; 2: .long __tls_get_addr@PLT; 3: 5446 We change it into: 5447 stc gbr,r0; nop; nop; nop; 5448 nop; nop; bra 3f; ...; 3:. */ 5449 5450 offset = rel->r_offset; 5451 BFD_ASSERT (offset >= 16); 5452 /* Size of LD instructions is 16 or 18. */ 5453 offset -= 16; 5454 insn = bfd_get_16 (input_bfd, contents + offset + 0); 5455 if ((insn & 0xff00) == 0xc700) 5456 { 5457 BFD_ASSERT (offset >= 2); 5458 offset -= 2; 5459 insn = bfd_get_16 (input_bfd, contents + offset + 0); 5460 } 5461 5462 BFD_ASSERT ((insn & 0xff00) == 0xd400); 5463 insn = bfd_get_16 (input_bfd, contents + offset + 2); 5464 BFD_ASSERT ((insn & 0xff00) == 0xc700); 5465 insn = bfd_get_16 (input_bfd, contents + offset + 4); 5466 BFD_ASSERT ((insn & 0xff00) == 0xd100); 5467 insn = bfd_get_16 (input_bfd, contents + offset + 6); 5468 BFD_ASSERT (insn == 0x310c); 5469 insn = bfd_get_16 (input_bfd, contents + offset + 8); 5470 BFD_ASSERT (insn == 0x410b); 5471 insn = bfd_get_16 (input_bfd, contents + offset + 10); 5472 BFD_ASSERT (insn == 0x34cc); 5473 5474 bfd_put_16 (output_bfd, 0x0012, contents + offset + 0); 5475 bfd_put_16 (output_bfd, 0x0009, contents + offset + 2); 5476 bfd_put_16 (output_bfd, 0x0009, contents + offset + 4); 5477 bfd_put_16 (output_bfd, 0x0009, contents + offset + 6); 5478 bfd_put_16 (output_bfd, 0x0009, contents + offset + 8); 5479 bfd_put_16 (output_bfd, 0x0009, contents + offset + 10); 5480 5481 continue; 5482 } 5483 5484 if (sgot == NULL || sgotplt == NULL) 5485 abort (); 5486 5487 off = htab->tls_ldm_got.offset; 5488 if (off & 1) 5489 off &= ~1; 5490 else 5491 { 5492 Elf_Internal_Rela outrel; 5493 bfd_byte *loc; 5494 5495 srelgot = htab->srelgot; 5496 if (srelgot == NULL) 5497 abort (); 5498 5499 outrel.r_offset = (sgot->output_section->vma 5500 + sgot->output_offset + off); 5501 outrel.r_addend = 0; 5502 outrel.r_info = ELF32_R_INFO (0, R_SH_TLS_DTPMOD32); 5503 loc = srelgot->contents; 5504 loc += srelgot->reloc_count++ * sizeof (Elf32_External_Rela); 5505 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc); 5506 htab->tls_ldm_got.offset |= 1; 5507 } 5508 5509 relocation = sh_elf_got_offset (htab) + off; 5510 addend = rel->r_addend; 5511 5512 goto final_link_relocate; 5513 5514 case R_SH_TLS_LDO_32: 5515 check_segment[0] = check_segment[1] = -1; 5516 if (! info->shared) 5517 relocation = tpoff (info, relocation); 5518 else 5519 relocation -= dtpoff_base (info); 5520 5521 addend = rel->r_addend; 5522 goto final_link_relocate; 5523 5524 case R_SH_TLS_LE_32: 5525 { 5526 int indx; 5527 Elf_Internal_Rela outrel; 5528 bfd_byte *loc; 5529 5530 check_segment[0] = check_segment[1] = -1; 5531 5532 if (! info->shared) 5533 { 5534 relocation = tpoff (info, relocation); 5535 addend = rel->r_addend; 5536 goto final_link_relocate; 5537 } 5538 5539 if (sreloc == NULL) 5540 { 5541 sreloc = _bfd_elf_get_dynamic_reloc_section 5542 (input_bfd, input_section, /*rela?*/ TRUE); 5543 if (sreloc == NULL) 5544 return FALSE; 5545 } 5546 5547 if (h == NULL || h->dynindx == -1) 5548 indx = 0; 5549 else 5550 indx = h->dynindx; 5551 5552 outrel.r_offset = (input_section->output_section->vma 5553 + input_section->output_offset 5554 + rel->r_offset); 5555 outrel.r_info = ELF32_R_INFO (indx, R_SH_TLS_TPOFF32); 5556 if (indx == 0) 5557 outrel.r_addend = relocation - dtpoff_base (info); 5558 else 5559 outrel.r_addend = 0; 5560 5561 loc = sreloc->contents; 5562 loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rela); 5563 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc); 5564 continue; 5565 } 5566 } 5567 5568 relocation_done: 5569 if (fdpic_p && check_segment[0] != (unsigned) -1 5570 && check_segment[0] != check_segment[1]) 5571 { 5572 /* We don't want duplicate errors for undefined symbols. */ 5573 if (!h || h->root.type != bfd_link_hash_undefined) 5574 { 5575 if (info->shared) 5576 { 5577 info->callbacks->einfo 5578 (_("%X%C: relocation to \"%s\" references a different segment\n"), 5579 input_bfd, input_section, rel->r_offset, symname); 5580 return FALSE; 5581 } 5582 else 5583 info->callbacks->einfo 5584 (_("%C: warning: relocation to \"%s\" references a different segment\n"), 5585 input_bfd, input_section, rel->r_offset, symname); 5586 } 5587 5588 elf_elfheader (output_bfd)->e_flags &= ~EF_SH_PIC; 5589 } 5590 5591 if (r != bfd_reloc_ok) 5592 { 5593 switch (r) 5594 { 5595 default: 5596 case bfd_reloc_outofrange: 5597 abort (); 5598 case bfd_reloc_overflow: 5599 { 5600 const char *name; 5601 5602 if (h != NULL) 5603 name = NULL; 5604 else 5605 { 5606 name = (bfd_elf_string_from_elf_section 5607 (input_bfd, symtab_hdr->sh_link, sym->st_name)); 5608 if (name == NULL) 5609 return FALSE; 5610 if (*name == '\0') 5611 name = bfd_section_name (input_bfd, sec); 5612 } 5613 if (! ((*info->callbacks->reloc_overflow) 5614 (info, (h ? &h->root : NULL), name, howto->name, 5615 (bfd_vma) 0, input_bfd, input_section, 5616 rel->r_offset))) 5617 return FALSE; 5618 } 5619 break; 5620 } 5621 } 5622 } 5623 5624 return TRUE; 5625 } 5626 5627 /* This is a version of bfd_generic_get_relocated_section_contents 5628 which uses sh_elf_relocate_section. */ 5629 5630 static bfd_byte * 5631 sh_elf_get_relocated_section_contents (bfd *output_bfd, 5632 struct bfd_link_info *link_info, 5633 struct bfd_link_order *link_order, 5634 bfd_byte *data, 5635 bfd_boolean relocatable, 5636 asymbol **symbols) 5637 { 5638 Elf_Internal_Shdr *symtab_hdr; 5639 asection *input_section = link_order->u.indirect.section; 5640 bfd *input_bfd = input_section->owner; 5641 asection **sections = NULL; 5642 Elf_Internal_Rela *internal_relocs = NULL; 5643 Elf_Internal_Sym *isymbuf = NULL; 5644 5645 /* We only need to handle the case of relaxing, or of having a 5646 particular set of section contents, specially. */ 5647 if (relocatable 5648 || elf_section_data (input_section)->this_hdr.contents == NULL) 5649 return bfd_generic_get_relocated_section_contents (output_bfd, link_info, 5650 link_order, data, 5651 relocatable, 5652 symbols); 5653 5654 symtab_hdr = &elf_symtab_hdr (input_bfd); 5655 5656 memcpy (data, elf_section_data (input_section)->this_hdr.contents, 5657 (size_t) input_section->size); 5658 5659 if ((input_section->flags & SEC_RELOC) != 0 5660 && input_section->reloc_count > 0) 5661 { 5662 asection **secpp; 5663 Elf_Internal_Sym *isym, *isymend; 5664 bfd_size_type amt; 5665 5666 internal_relocs = (_bfd_elf_link_read_relocs 5667 (input_bfd, input_section, NULL, 5668 (Elf_Internal_Rela *) NULL, FALSE)); 5669 if (internal_relocs == NULL) 5670 goto error_return; 5671 5672 if (symtab_hdr->sh_info != 0) 5673 { 5674 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents; 5675 if (isymbuf == NULL) 5676 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, 5677 symtab_hdr->sh_info, 0, 5678 NULL, NULL, NULL); 5679 if (isymbuf == NULL) 5680 goto error_return; 5681 } 5682 5683 amt = symtab_hdr->sh_info; 5684 amt *= sizeof (asection *); 5685 sections = (asection **) bfd_malloc (amt); 5686 if (sections == NULL && amt != 0) 5687 goto error_return; 5688 5689 isymend = isymbuf + symtab_hdr->sh_info; 5690 for (isym = isymbuf, secpp = sections; isym < isymend; ++isym, ++secpp) 5691 { 5692 asection *isec; 5693 5694 if (isym->st_shndx == SHN_UNDEF) 5695 isec = bfd_und_section_ptr; 5696 else if (isym->st_shndx == SHN_ABS) 5697 isec = bfd_abs_section_ptr; 5698 else if (isym->st_shndx == SHN_COMMON) 5699 isec = bfd_com_section_ptr; 5700 else 5701 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx); 5702 5703 *secpp = isec; 5704 } 5705 5706 if (! sh_elf_relocate_section (output_bfd, link_info, input_bfd, 5707 input_section, data, internal_relocs, 5708 isymbuf, sections)) 5709 goto error_return; 5710 5711 if (sections != NULL) 5712 free (sections); 5713 if (isymbuf != NULL 5714 && symtab_hdr->contents != (unsigned char *) isymbuf) 5715 free (isymbuf); 5716 if (elf_section_data (input_section)->relocs != internal_relocs) 5717 free (internal_relocs); 5718 } 5719 5720 return data; 5721 5722 error_return: 5723 if (sections != NULL) 5724 free (sections); 5725 if (isymbuf != NULL 5726 && symtab_hdr->contents != (unsigned char *) isymbuf) 5727 free (isymbuf); 5728 if (internal_relocs != NULL 5729 && elf_section_data (input_section)->relocs != internal_relocs) 5730 free (internal_relocs); 5731 return NULL; 5732 } 5733 5734 /* Return the base VMA address which should be subtracted from real addresses 5735 when resolving @dtpoff relocation. 5736 This is PT_TLS segment p_vaddr. */ 5737 5738 static bfd_vma 5739 dtpoff_base (struct bfd_link_info *info) 5740 { 5741 /* If tls_sec is NULL, we should have signalled an error already. */ 5742 if (elf_hash_table (info)->tls_sec == NULL) 5743 return 0; 5744 return elf_hash_table (info)->tls_sec->vma; 5745 } 5746 5747 /* Return the relocation value for R_SH_TLS_TPOFF32.. */ 5748 5749 static bfd_vma 5750 tpoff (struct bfd_link_info *info, bfd_vma address) 5751 { 5752 /* If tls_sec is NULL, we should have signalled an error already. */ 5753 if (elf_hash_table (info)->tls_sec == NULL) 5754 return 0; 5755 /* SH TLS ABI is variant I and static TLS block start just after tcbhead 5756 structure which has 2 pointer fields. */ 5757 return (address - elf_hash_table (info)->tls_sec->vma 5758 + align_power ((bfd_vma) 8, 5759 elf_hash_table (info)->tls_sec->alignment_power)); 5760 } 5761 5762 static asection * 5763 sh_elf_gc_mark_hook (asection *sec, 5764 struct bfd_link_info *info, 5765 Elf_Internal_Rela *rel, 5766 struct elf_link_hash_entry *h, 5767 Elf_Internal_Sym *sym) 5768 { 5769 if (h != NULL) 5770 switch (ELF32_R_TYPE (rel->r_info)) 5771 { 5772 case R_SH_GNU_VTINHERIT: 5773 case R_SH_GNU_VTENTRY: 5774 return NULL; 5775 } 5776 5777 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym); 5778 } 5779 5780 /* Update the got entry reference counts for the section being removed. */ 5781 5782 static bfd_boolean 5783 sh_elf_gc_sweep_hook (bfd *abfd, struct bfd_link_info *info, 5784 asection *sec, const Elf_Internal_Rela *relocs) 5785 { 5786 Elf_Internal_Shdr *symtab_hdr; 5787 struct elf_link_hash_entry **sym_hashes; 5788 bfd_signed_vma *local_got_refcounts; 5789 union gotref *local_funcdesc; 5790 const Elf_Internal_Rela *rel, *relend; 5791 5792 if (info->relocatable) 5793 return TRUE; 5794 5795 elf_section_data (sec)->local_dynrel = NULL; 5796 5797 symtab_hdr = &elf_symtab_hdr (abfd); 5798 sym_hashes = elf_sym_hashes (abfd); 5799 local_got_refcounts = elf_local_got_refcounts (abfd); 5800 local_funcdesc = sh_elf_local_funcdesc (abfd); 5801 5802 relend = relocs + sec->reloc_count; 5803 for (rel = relocs; rel < relend; rel++) 5804 { 5805 unsigned long r_symndx; 5806 unsigned int r_type; 5807 struct elf_link_hash_entry *h = NULL; 5808 #ifdef INCLUDE_SHMEDIA 5809 int seen_stt_datalabel = 0; 5810 #endif 5811 5812 r_symndx = ELF32_R_SYM (rel->r_info); 5813 if (r_symndx >= symtab_hdr->sh_info) 5814 { 5815 struct elf_sh_link_hash_entry *eh; 5816 struct elf_sh_dyn_relocs **pp; 5817 struct elf_sh_dyn_relocs *p; 5818 5819 h = sym_hashes[r_symndx - symtab_hdr->sh_info]; 5820 while (h->root.type == bfd_link_hash_indirect 5821 || h->root.type == bfd_link_hash_warning) 5822 { 5823 #ifdef INCLUDE_SHMEDIA 5824 seen_stt_datalabel |= h->type == STT_DATALABEL; 5825 #endif 5826 h = (struct elf_link_hash_entry *) h->root.u.i.link; 5827 } 5828 eh = (struct elf_sh_link_hash_entry *) h; 5829 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; pp = &p->next) 5830 if (p->sec == sec) 5831 { 5832 /* Everything must go for SEC. */ 5833 *pp = p->next; 5834 break; 5835 } 5836 } 5837 5838 r_type = ELF32_R_TYPE (rel->r_info); 5839 switch (sh_elf_optimized_tls_reloc (info, r_type, h != NULL)) 5840 { 5841 case R_SH_TLS_LD_32: 5842 if (sh_elf_hash_table (info)->tls_ldm_got.refcount > 0) 5843 sh_elf_hash_table (info)->tls_ldm_got.refcount -= 1; 5844 break; 5845 5846 case R_SH_GOT32: 5847 case R_SH_GOT20: 5848 case R_SH_GOTOFF: 5849 case R_SH_GOTOFF20: 5850 case R_SH_GOTPC: 5851 #ifdef INCLUDE_SHMEDIA 5852 case R_SH_GOT_LOW16: 5853 case R_SH_GOT_MEDLOW16: 5854 case R_SH_GOT_MEDHI16: 5855 case R_SH_GOT_HI16: 5856 case R_SH_GOT10BY4: 5857 case R_SH_GOT10BY8: 5858 case R_SH_GOTOFF_LOW16: 5859 case R_SH_GOTOFF_MEDLOW16: 5860 case R_SH_GOTOFF_MEDHI16: 5861 case R_SH_GOTOFF_HI16: 5862 case R_SH_GOTPC_LOW16: 5863 case R_SH_GOTPC_MEDLOW16: 5864 case R_SH_GOTPC_MEDHI16: 5865 case R_SH_GOTPC_HI16: 5866 #endif 5867 case R_SH_TLS_GD_32: 5868 case R_SH_TLS_IE_32: 5869 case R_SH_GOTFUNCDESC: 5870 case R_SH_GOTFUNCDESC20: 5871 if (h != NULL) 5872 { 5873 #ifdef INCLUDE_SHMEDIA 5874 if (seen_stt_datalabel) 5875 { 5876 struct elf_sh_link_hash_entry *eh; 5877 eh = (struct elf_sh_link_hash_entry *) h; 5878 if (eh->datalabel_got.refcount > 0) 5879 eh->datalabel_got.refcount -= 1; 5880 } 5881 else 5882 #endif 5883 if (h->got.refcount > 0) 5884 h->got.refcount -= 1; 5885 } 5886 else if (local_got_refcounts != NULL) 5887 { 5888 #ifdef INCLUDE_SHMEDIA 5889 if (rel->r_addend & 1) 5890 { 5891 if (local_got_refcounts[symtab_hdr->sh_info + r_symndx] > 0) 5892 local_got_refcounts[symtab_hdr->sh_info + r_symndx] -= 1; 5893 } 5894 else 5895 #endif 5896 if (local_got_refcounts[r_symndx] > 0) 5897 local_got_refcounts[r_symndx] -= 1; 5898 } 5899 break; 5900 5901 case R_SH_FUNCDESC: 5902 if (h != NULL) 5903 sh_elf_hash_entry (h)->abs_funcdesc_refcount -= 1; 5904 else if (sh_elf_hash_table (info)->fdpic_p && !info->shared) 5905 sh_elf_hash_table (info)->srofixup->size -= 4; 5906 5907 /* Fall through. */ 5908 5909 case R_SH_GOTOFFFUNCDESC: 5910 case R_SH_GOTOFFFUNCDESC20: 5911 if (h != NULL) 5912 sh_elf_hash_entry (h)->funcdesc.refcount -= 1; 5913 else 5914 local_funcdesc[r_symndx].refcount -= 1; 5915 break; 5916 5917 case R_SH_DIR32: 5918 if (sh_elf_hash_table (info)->fdpic_p && !info->shared 5919 && (sec->flags & SEC_ALLOC) != 0) 5920 sh_elf_hash_table (info)->srofixup->size -= 4; 5921 /* Fall thru */ 5922 5923 case R_SH_REL32: 5924 if (info->shared) 5925 break; 5926 /* Fall thru */ 5927 5928 case R_SH_PLT32: 5929 #ifdef INCLUDE_SHMEDIA 5930 case R_SH_PLT_LOW16: 5931 case R_SH_PLT_MEDLOW16: 5932 case R_SH_PLT_MEDHI16: 5933 case R_SH_PLT_HI16: 5934 #endif 5935 if (h != NULL) 5936 { 5937 if (h->plt.refcount > 0) 5938 h->plt.refcount -= 1; 5939 } 5940 break; 5941 5942 case R_SH_GOTPLT32: 5943 #ifdef INCLUDE_SHMEDIA 5944 case R_SH_GOTPLT_LOW16: 5945 case R_SH_GOTPLT_MEDLOW16: 5946 case R_SH_GOTPLT_MEDHI16: 5947 case R_SH_GOTPLT_HI16: 5948 case R_SH_GOTPLT10BY4: 5949 case R_SH_GOTPLT10BY8: 5950 #endif 5951 if (h != NULL) 5952 { 5953 struct elf_sh_link_hash_entry *eh; 5954 eh = (struct elf_sh_link_hash_entry *) h; 5955 if (eh->gotplt_refcount > 0) 5956 { 5957 eh->gotplt_refcount -= 1; 5958 if (h->plt.refcount > 0) 5959 h->plt.refcount -= 1; 5960 } 5961 #ifdef INCLUDE_SHMEDIA 5962 else if (seen_stt_datalabel) 5963 { 5964 if (eh->datalabel_got.refcount > 0) 5965 eh->datalabel_got.refcount -= 1; 5966 } 5967 #endif 5968 else if (h->got.refcount > 0) 5969 h->got.refcount -= 1; 5970 } 5971 else if (local_got_refcounts != NULL) 5972 { 5973 #ifdef INCLUDE_SHMEDIA 5974 if (rel->r_addend & 1) 5975 { 5976 if (local_got_refcounts[symtab_hdr->sh_info + r_symndx] > 0) 5977 local_got_refcounts[symtab_hdr->sh_info + r_symndx] -= 1; 5978 } 5979 else 5980 #endif 5981 if (local_got_refcounts[r_symndx] > 0) 5982 local_got_refcounts[r_symndx] -= 1; 5983 } 5984 break; 5985 5986 default: 5987 break; 5988 } 5989 } 5990 5991 return TRUE; 5992 } 5993 5994 /* Copy the extra info we tack onto an elf_link_hash_entry. */ 5995 5996 static void 5997 sh_elf_copy_indirect_symbol (struct bfd_link_info *info, 5998 struct elf_link_hash_entry *dir, 5999 struct elf_link_hash_entry *ind) 6000 { 6001 struct elf_sh_link_hash_entry *edir, *eind; 6002 6003 edir = (struct elf_sh_link_hash_entry *) dir; 6004 eind = (struct elf_sh_link_hash_entry *) ind; 6005 6006 if (eind->dyn_relocs != NULL) 6007 { 6008 if (edir->dyn_relocs != NULL) 6009 { 6010 struct elf_sh_dyn_relocs **pp; 6011 struct elf_sh_dyn_relocs *p; 6012 6013 /* Add reloc counts against the indirect sym to the direct sym 6014 list. Merge any entries against the same section. */ 6015 for (pp = &eind->dyn_relocs; (p = *pp) != NULL; ) 6016 { 6017 struct elf_sh_dyn_relocs *q; 6018 6019 for (q = edir->dyn_relocs; q != NULL; q = q->next) 6020 if (q->sec == p->sec) 6021 { 6022 q->pc_count += p->pc_count; 6023 q->count += p->count; 6024 *pp = p->next; 6025 break; 6026 } 6027 if (q == NULL) 6028 pp = &p->next; 6029 } 6030 *pp = edir->dyn_relocs; 6031 } 6032 6033 edir->dyn_relocs = eind->dyn_relocs; 6034 eind->dyn_relocs = NULL; 6035 } 6036 edir->gotplt_refcount = eind->gotplt_refcount; 6037 eind->gotplt_refcount = 0; 6038 #ifdef INCLUDE_SHMEDIA 6039 edir->datalabel_got.refcount += eind->datalabel_got.refcount; 6040 eind->datalabel_got.refcount = 0; 6041 #endif 6042 edir->funcdesc.refcount += eind->funcdesc.refcount; 6043 eind->funcdesc.refcount = 0; 6044 edir->abs_funcdesc_refcount += eind->abs_funcdesc_refcount; 6045 eind->abs_funcdesc_refcount = 0; 6046 6047 if (ind->root.type == bfd_link_hash_indirect 6048 && dir->got.refcount <= 0) 6049 { 6050 edir->got_type = eind->got_type; 6051 eind->got_type = GOT_UNKNOWN; 6052 } 6053 6054 if (ind->root.type != bfd_link_hash_indirect 6055 && dir->dynamic_adjusted) 6056 { 6057 /* If called to transfer flags for a weakdef during processing 6058 of elf_adjust_dynamic_symbol, don't copy non_got_ref. 6059 We clear it ourselves for ELIMINATE_COPY_RELOCS. */ 6060 dir->ref_dynamic |= ind->ref_dynamic; 6061 dir->ref_regular |= ind->ref_regular; 6062 dir->ref_regular_nonweak |= ind->ref_regular_nonweak; 6063 dir->needs_plt |= ind->needs_plt; 6064 } 6065 else 6066 _bfd_elf_link_hash_copy_indirect (info, dir, ind); 6067 } 6068 6069 static int 6070 sh_elf_optimized_tls_reloc (struct bfd_link_info *info, int r_type, 6071 int is_local) 6072 { 6073 if (info->shared) 6074 return r_type; 6075 6076 switch (r_type) 6077 { 6078 case R_SH_TLS_GD_32: 6079 case R_SH_TLS_IE_32: 6080 if (is_local) 6081 return R_SH_TLS_LE_32; 6082 return R_SH_TLS_IE_32; 6083 case R_SH_TLS_LD_32: 6084 return R_SH_TLS_LE_32; 6085 } 6086 6087 return r_type; 6088 } 6089 6090 /* Look through the relocs for a section during the first phase. 6091 Since we don't do .gots or .plts, we just need to consider the 6092 virtual table relocs for gc. */ 6093 6094 static bfd_boolean 6095 sh_elf_check_relocs (bfd *abfd, struct bfd_link_info *info, asection *sec, 6096 const Elf_Internal_Rela *relocs) 6097 { 6098 Elf_Internal_Shdr *symtab_hdr; 6099 struct elf_link_hash_entry **sym_hashes; 6100 struct elf_sh_link_hash_table *htab; 6101 const Elf_Internal_Rela *rel; 6102 const Elf_Internal_Rela *rel_end; 6103 asection *sreloc; 6104 unsigned int r_type; 6105 int got_type, old_got_type; 6106 6107 sreloc = NULL; 6108 6109 if (info->relocatable) 6110 return TRUE; 6111 6112 BFD_ASSERT (is_sh_elf (abfd)); 6113 6114 symtab_hdr = &elf_symtab_hdr (abfd); 6115 sym_hashes = elf_sym_hashes (abfd); 6116 6117 htab = sh_elf_hash_table (info); 6118 if (htab == NULL) 6119 return FALSE; 6120 6121 rel_end = relocs + sec->reloc_count; 6122 for (rel = relocs; rel < rel_end; rel++) 6123 { 6124 struct elf_link_hash_entry *h; 6125 unsigned long r_symndx; 6126 #ifdef INCLUDE_SHMEDIA 6127 int seen_stt_datalabel = 0; 6128 #endif 6129 6130 r_symndx = ELF32_R_SYM (rel->r_info); 6131 r_type = ELF32_R_TYPE (rel->r_info); 6132 6133 if (r_symndx < symtab_hdr->sh_info) 6134 h = NULL; 6135 else 6136 { 6137 h = sym_hashes[r_symndx - symtab_hdr->sh_info]; 6138 while (h->root.type == bfd_link_hash_indirect 6139 || h->root.type == bfd_link_hash_warning) 6140 { 6141 #ifdef INCLUDE_SHMEDIA 6142 seen_stt_datalabel |= h->type == STT_DATALABEL; 6143 #endif 6144 h = (struct elf_link_hash_entry *) h->root.u.i.link; 6145 } 6146 } 6147 6148 r_type = sh_elf_optimized_tls_reloc (info, r_type, h == NULL); 6149 if (! info->shared 6150 && r_type == R_SH_TLS_IE_32 6151 && h != NULL 6152 && h->root.type != bfd_link_hash_undefined 6153 && h->root.type != bfd_link_hash_undefweak 6154 && (h->dynindx == -1 6155 || h->def_regular)) 6156 r_type = R_SH_TLS_LE_32; 6157 6158 if (htab->fdpic_p) 6159 switch (r_type) 6160 { 6161 case R_SH_GOTOFFFUNCDESC: 6162 case R_SH_GOTOFFFUNCDESC20: 6163 case R_SH_FUNCDESC: 6164 case R_SH_GOTFUNCDESC: 6165 case R_SH_GOTFUNCDESC20: 6166 if (h != NULL) 6167 { 6168 if (h->dynindx == -1) 6169 switch (ELF_ST_VISIBILITY (h->other)) 6170 { 6171 case STV_INTERNAL: 6172 case STV_HIDDEN: 6173 break; 6174 default: 6175 bfd_elf_link_record_dynamic_symbol (info, h); 6176 break; 6177 } 6178 } 6179 break; 6180 } 6181 6182 /* Some relocs require a global offset table. */ 6183 if (htab->sgot == NULL) 6184 { 6185 switch (r_type) 6186 { 6187 case R_SH_DIR32: 6188 /* This may require an rofixup. */ 6189 if (!htab->fdpic_p) 6190 break; 6191 case R_SH_GOTPLT32: 6192 case R_SH_GOT32: 6193 case R_SH_GOT20: 6194 case R_SH_GOTOFF: 6195 case R_SH_GOTOFF20: 6196 case R_SH_FUNCDESC: 6197 case R_SH_GOTFUNCDESC: 6198 case R_SH_GOTFUNCDESC20: 6199 case R_SH_GOTOFFFUNCDESC: 6200 case R_SH_GOTOFFFUNCDESC20: 6201 case R_SH_GOTPC: 6202 #ifdef INCLUDE_SHMEDIA 6203 case R_SH_GOTPLT_LOW16: 6204 case R_SH_GOTPLT_MEDLOW16: 6205 case R_SH_GOTPLT_MEDHI16: 6206 case R_SH_GOTPLT_HI16: 6207 case R_SH_GOTPLT10BY4: 6208 case R_SH_GOTPLT10BY8: 6209 case R_SH_GOT_LOW16: 6210 case R_SH_GOT_MEDLOW16: 6211 case R_SH_GOT_MEDHI16: 6212 case R_SH_GOT_HI16: 6213 case R_SH_GOT10BY4: 6214 case R_SH_GOT10BY8: 6215 case R_SH_GOTOFF_LOW16: 6216 case R_SH_GOTOFF_MEDLOW16: 6217 case R_SH_GOTOFF_MEDHI16: 6218 case R_SH_GOTOFF_HI16: 6219 case R_SH_GOTPC_LOW16: 6220 case R_SH_GOTPC_MEDLOW16: 6221 case R_SH_GOTPC_MEDHI16: 6222 case R_SH_GOTPC_HI16: 6223 #endif 6224 case R_SH_TLS_GD_32: 6225 case R_SH_TLS_LD_32: 6226 case R_SH_TLS_IE_32: 6227 if (htab->root.dynobj == NULL) 6228 htab->root.dynobj = abfd; 6229 if (!create_got_section (htab->root.dynobj, info)) 6230 return FALSE; 6231 break; 6232 6233 default: 6234 break; 6235 } 6236 } 6237 6238 switch (r_type) 6239 { 6240 /* This relocation describes the C++ object vtable hierarchy. 6241 Reconstruct it for later use during GC. */ 6242 case R_SH_GNU_VTINHERIT: 6243 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset)) 6244 return FALSE; 6245 break; 6246 6247 /* This relocation describes which C++ vtable entries are actually 6248 used. Record for later use during GC. */ 6249 case R_SH_GNU_VTENTRY: 6250 BFD_ASSERT (h != NULL); 6251 if (h != NULL 6252 && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend)) 6253 return FALSE; 6254 break; 6255 6256 case R_SH_TLS_IE_32: 6257 if (info->shared) 6258 info->flags |= DF_STATIC_TLS; 6259 6260 /* FALLTHROUGH */ 6261 force_got: 6262 case R_SH_TLS_GD_32: 6263 case R_SH_GOT32: 6264 case R_SH_GOT20: 6265 #ifdef INCLUDE_SHMEDIA 6266 case R_SH_GOT_LOW16: 6267 case R_SH_GOT_MEDLOW16: 6268 case R_SH_GOT_MEDHI16: 6269 case R_SH_GOT_HI16: 6270 case R_SH_GOT10BY4: 6271 case R_SH_GOT10BY8: 6272 #endif 6273 case R_SH_GOTFUNCDESC: 6274 case R_SH_GOTFUNCDESC20: 6275 switch (r_type) 6276 { 6277 default: 6278 got_type = GOT_NORMAL; 6279 break; 6280 case R_SH_TLS_GD_32: 6281 got_type = GOT_TLS_GD; 6282 break; 6283 case R_SH_TLS_IE_32: 6284 got_type = GOT_TLS_IE; 6285 break; 6286 case R_SH_GOTFUNCDESC: 6287 case R_SH_GOTFUNCDESC20: 6288 got_type = GOT_FUNCDESC; 6289 break; 6290 } 6291 6292 if (h != NULL) 6293 { 6294 #ifdef INCLUDE_SHMEDIA 6295 if (seen_stt_datalabel) 6296 { 6297 struct elf_sh_link_hash_entry *eh 6298 = (struct elf_sh_link_hash_entry *) h; 6299 6300 eh->datalabel_got.refcount += 1; 6301 } 6302 else 6303 #endif 6304 h->got.refcount += 1; 6305 old_got_type = sh_elf_hash_entry (h)->got_type; 6306 } 6307 else 6308 { 6309 bfd_signed_vma *local_got_refcounts; 6310 6311 /* This is a global offset table entry for a local 6312 symbol. */ 6313 local_got_refcounts = elf_local_got_refcounts (abfd); 6314 if (local_got_refcounts == NULL) 6315 { 6316 bfd_size_type size; 6317 6318 size = symtab_hdr->sh_info; 6319 size *= sizeof (bfd_signed_vma); 6320 #ifdef INCLUDE_SHMEDIA 6321 /* Reserve space for both the datalabel and 6322 codelabel local GOT offsets. */ 6323 size *= 2; 6324 #endif 6325 size += symtab_hdr->sh_info; 6326 local_got_refcounts = ((bfd_signed_vma *) 6327 bfd_zalloc (abfd, size)); 6328 if (local_got_refcounts == NULL) 6329 return FALSE; 6330 elf_local_got_refcounts (abfd) = local_got_refcounts; 6331 #ifdef INCLUDE_SHMEDIA 6332 /* Take care of both the datalabel and codelabel local 6333 GOT offsets. */ 6334 sh_elf_local_got_type (abfd) 6335 = (char *) (local_got_refcounts + 2 * symtab_hdr->sh_info); 6336 #else 6337 sh_elf_local_got_type (abfd) 6338 = (char *) (local_got_refcounts + symtab_hdr->sh_info); 6339 #endif 6340 } 6341 #ifdef INCLUDE_SHMEDIA 6342 if (rel->r_addend & 1) 6343 local_got_refcounts[symtab_hdr->sh_info + r_symndx] += 1; 6344 else 6345 #endif 6346 local_got_refcounts[r_symndx] += 1; 6347 old_got_type = sh_elf_local_got_type (abfd) [r_symndx]; 6348 } 6349 6350 /* If a TLS symbol is accessed using IE at least once, 6351 there is no point to use dynamic model for it. */ 6352 if (old_got_type != got_type && old_got_type != GOT_UNKNOWN 6353 && (old_got_type != GOT_TLS_GD || got_type != GOT_TLS_IE)) 6354 { 6355 if (old_got_type == GOT_TLS_IE && got_type == GOT_TLS_GD) 6356 got_type = GOT_TLS_IE; 6357 else 6358 { 6359 if ((old_got_type == GOT_FUNCDESC || got_type == GOT_FUNCDESC) 6360 && (old_got_type == GOT_NORMAL || got_type == GOT_NORMAL)) 6361 (*_bfd_error_handler) 6362 (_("%B: `%s' accessed both as normal and FDPIC symbol"), 6363 abfd, h->root.root.string); 6364 else if (old_got_type == GOT_FUNCDESC 6365 || got_type == GOT_FUNCDESC) 6366 (*_bfd_error_handler) 6367 (_("%B: `%s' accessed both as FDPIC and thread local symbol"), 6368 abfd, h->root.root.string); 6369 else 6370 (*_bfd_error_handler) 6371 (_("%B: `%s' accessed both as normal and thread local symbol"), 6372 abfd, h->root.root.string); 6373 return FALSE; 6374 } 6375 } 6376 6377 if (old_got_type != got_type) 6378 { 6379 if (h != NULL) 6380 sh_elf_hash_entry (h)->got_type = got_type; 6381 else 6382 sh_elf_local_got_type (abfd) [r_symndx] = got_type; 6383 } 6384 6385 break; 6386 6387 case R_SH_TLS_LD_32: 6388 sh_elf_hash_table(info)->tls_ldm_got.refcount += 1; 6389 break; 6390 6391 case R_SH_FUNCDESC: 6392 case R_SH_GOTOFFFUNCDESC: 6393 case R_SH_GOTOFFFUNCDESC20: 6394 if (rel->r_addend) 6395 { 6396 (*_bfd_error_handler) 6397 (_("%B: Function descriptor relocation with non-zero addend"), 6398 abfd); 6399 return FALSE; 6400 } 6401 6402 if (h == NULL) 6403 { 6404 union gotref *local_funcdesc; 6405 6406 /* We need a function descriptor for a local symbol. */ 6407 local_funcdesc = sh_elf_local_funcdesc (abfd); 6408 if (local_funcdesc == NULL) 6409 { 6410 bfd_size_type size; 6411 6412 size = symtab_hdr->sh_info * sizeof (union gotref); 6413 #ifdef INCLUDE_SHMEDIA 6414 /* Count datalabel local GOT. */ 6415 size *= 2; 6416 #endif 6417 local_funcdesc = (union gotref *) bfd_zalloc (abfd, size); 6418 if (local_funcdesc == NULL) 6419 return FALSE; 6420 sh_elf_local_funcdesc (abfd) = local_funcdesc; 6421 } 6422 local_funcdesc[r_symndx].refcount += 1; 6423 6424 if (r_type == R_SH_FUNCDESC) 6425 { 6426 if (!info->shared) 6427 htab->srofixup->size += 4; 6428 else 6429 htab->srelgot->size += sizeof (Elf32_External_Rela); 6430 } 6431 } 6432 else 6433 { 6434 sh_elf_hash_entry (h)->funcdesc.refcount++; 6435 if (r_type == R_SH_FUNCDESC) 6436 sh_elf_hash_entry (h)->abs_funcdesc_refcount++; 6437 6438 /* If there is a function descriptor reference, then 6439 there should not be any non-FDPIC references. */ 6440 old_got_type = sh_elf_hash_entry (h)->got_type; 6441 if (old_got_type != GOT_FUNCDESC && old_got_type != GOT_UNKNOWN) 6442 { 6443 if (old_got_type == GOT_NORMAL) 6444 (*_bfd_error_handler) 6445 (_("%B: `%s' accessed both as normal and FDPIC symbol"), 6446 abfd, h->root.root.string); 6447 else 6448 (*_bfd_error_handler) 6449 (_("%B: `%s' accessed both as FDPIC and thread local symbol"), 6450 abfd, h->root.root.string); 6451 } 6452 } 6453 break; 6454 6455 case R_SH_GOTPLT32: 6456 #ifdef INCLUDE_SHMEDIA 6457 case R_SH_GOTPLT_LOW16: 6458 case R_SH_GOTPLT_MEDLOW16: 6459 case R_SH_GOTPLT_MEDHI16: 6460 case R_SH_GOTPLT_HI16: 6461 case R_SH_GOTPLT10BY4: 6462 case R_SH_GOTPLT10BY8: 6463 #endif 6464 /* If this is a local symbol, we resolve it directly without 6465 creating a procedure linkage table entry. */ 6466 6467 if (h == NULL 6468 || h->forced_local 6469 || ! info->shared 6470 || info->symbolic 6471 || h->dynindx == -1) 6472 goto force_got; 6473 6474 h->needs_plt = 1; 6475 h->plt.refcount += 1; 6476 ((struct elf_sh_link_hash_entry *) h)->gotplt_refcount += 1; 6477 6478 break; 6479 6480 case R_SH_PLT32: 6481 #ifdef INCLUDE_SHMEDIA 6482 case R_SH_PLT_LOW16: 6483 case R_SH_PLT_MEDLOW16: 6484 case R_SH_PLT_MEDHI16: 6485 case R_SH_PLT_HI16: 6486 #endif 6487 /* This symbol requires a procedure linkage table entry. We 6488 actually build the entry in adjust_dynamic_symbol, 6489 because this might be a case of linking PIC code which is 6490 never referenced by a dynamic object, in which case we 6491 don't need to generate a procedure linkage table entry 6492 after all. */ 6493 6494 /* If this is a local symbol, we resolve it directly without 6495 creating a procedure linkage table entry. */ 6496 if (h == NULL) 6497 continue; 6498 6499 if (h->forced_local) 6500 break; 6501 6502 h->needs_plt = 1; 6503 h->plt.refcount += 1; 6504 break; 6505 6506 case R_SH_DIR32: 6507 case R_SH_REL32: 6508 #ifdef INCLUDE_SHMEDIA 6509 case R_SH_IMM_LOW16_PCREL: 6510 case R_SH_IMM_MEDLOW16_PCREL: 6511 case R_SH_IMM_MEDHI16_PCREL: 6512 case R_SH_IMM_HI16_PCREL: 6513 #endif 6514 if (h != NULL && ! info->shared) 6515 { 6516 h->non_got_ref = 1; 6517 h->plt.refcount += 1; 6518 } 6519 6520 /* If we are creating a shared library, and this is a reloc 6521 against a global symbol, or a non PC relative reloc 6522 against a local symbol, then we need to copy the reloc 6523 into the shared library. However, if we are linking with 6524 -Bsymbolic, we do not need to copy a reloc against a 6525 global symbol which is defined in an object we are 6526 including in the link (i.e., DEF_REGULAR is set). At 6527 this point we have not seen all the input files, so it is 6528 possible that DEF_REGULAR is not set now but will be set 6529 later (it is never cleared). We account for that 6530 possibility below by storing information in the 6531 dyn_relocs field of the hash table entry. A similar 6532 situation occurs when creating shared libraries and symbol 6533 visibility changes render the symbol local. 6534 6535 If on the other hand, we are creating an executable, we 6536 may need to keep relocations for symbols satisfied by a 6537 dynamic library if we manage to avoid copy relocs for the 6538 symbol. */ 6539 if ((info->shared 6540 && (sec->flags & SEC_ALLOC) != 0 6541 && (r_type != R_SH_REL32 6542 || (h != NULL 6543 && (! info->symbolic 6544 || h->root.type == bfd_link_hash_defweak 6545 || !h->def_regular)))) 6546 || (! info->shared 6547 && (sec->flags & SEC_ALLOC) != 0 6548 && h != NULL 6549 && (h->root.type == bfd_link_hash_defweak 6550 || !h->def_regular))) 6551 { 6552 struct elf_sh_dyn_relocs *p; 6553 struct elf_sh_dyn_relocs **head; 6554 6555 if (htab->root.dynobj == NULL) 6556 htab->root.dynobj = abfd; 6557 6558 /* When creating a shared object, we must copy these 6559 reloc types into the output file. We create a reloc 6560 section in dynobj and make room for this reloc. */ 6561 if (sreloc == NULL) 6562 { 6563 sreloc = _bfd_elf_make_dynamic_reloc_section 6564 (sec, htab->root.dynobj, 2, abfd, /*rela?*/ TRUE); 6565 6566 if (sreloc == NULL) 6567 return FALSE; 6568 } 6569 6570 /* If this is a global symbol, we count the number of 6571 relocations we need for this symbol. */ 6572 if (h != NULL) 6573 head = &((struct elf_sh_link_hash_entry *) h)->dyn_relocs; 6574 else 6575 { 6576 /* Track dynamic relocs needed for local syms too. */ 6577 asection *s; 6578 void *vpp; 6579 Elf_Internal_Sym *isym; 6580 6581 isym = bfd_sym_from_r_symndx (&htab->sym_cache, 6582 abfd, r_symndx); 6583 if (isym == NULL) 6584 return FALSE; 6585 6586 s = bfd_section_from_elf_index (abfd, isym->st_shndx); 6587 if (s == NULL) 6588 s = sec; 6589 6590 vpp = &elf_section_data (s)->local_dynrel; 6591 head = (struct elf_sh_dyn_relocs **) vpp; 6592 } 6593 6594 p = *head; 6595 if (p == NULL || p->sec != sec) 6596 { 6597 bfd_size_type amt = sizeof (*p); 6598 p = bfd_alloc (htab->root.dynobj, amt); 6599 if (p == NULL) 6600 return FALSE; 6601 p->next = *head; 6602 *head = p; 6603 p->sec = sec; 6604 p->count = 0; 6605 p->pc_count = 0; 6606 } 6607 6608 p->count += 1; 6609 if (r_type == R_SH_REL32 6610 #ifdef INCLUDE_SHMEDIA 6611 || r_type == R_SH_IMM_LOW16_PCREL 6612 || r_type == R_SH_IMM_MEDLOW16_PCREL 6613 || r_type == R_SH_IMM_MEDHI16_PCREL 6614 || r_type == R_SH_IMM_HI16_PCREL 6615 #endif 6616 ) 6617 p->pc_count += 1; 6618 } 6619 6620 /* Allocate the fixup regardless of whether we need a relocation. 6621 If we end up generating the relocation, we'll unallocate the 6622 fixup. */ 6623 if (htab->fdpic_p && !info->shared 6624 && r_type == R_SH_DIR32 6625 && (sec->flags & SEC_ALLOC) != 0) 6626 htab->srofixup->size += 4; 6627 break; 6628 6629 case R_SH_TLS_LE_32: 6630 if (info->shared) 6631 { 6632 (*_bfd_error_handler) 6633 (_("%B: TLS local exec code cannot be linked into shared objects"), 6634 abfd); 6635 return FALSE; 6636 } 6637 6638 break; 6639 6640 case R_SH_TLS_LDO_32: 6641 /* Nothing to do. */ 6642 break; 6643 6644 default: 6645 break; 6646 } 6647 } 6648 6649 return TRUE; 6650 } 6651 6652 #ifndef sh_elf_set_mach_from_flags 6653 static unsigned int sh_ef_bfd_table[] = { EF_SH_BFD_TABLE }; 6654 6655 static bfd_boolean 6656 sh_elf_set_mach_from_flags (bfd *abfd) 6657 { 6658 flagword flags = elf_elfheader (abfd)->e_flags & EF_SH_MACH_MASK; 6659 6660 if (flags >= sizeof(sh_ef_bfd_table)) 6661 return FALSE; 6662 6663 if (sh_ef_bfd_table[flags] == 0) 6664 return FALSE; 6665 6666 bfd_default_set_arch_mach (abfd, bfd_arch_sh, sh_ef_bfd_table[flags]); 6667 6668 return TRUE; 6669 } 6670 6671 6672 /* Reverse table lookup for sh_ef_bfd_table[]. 6673 Given a bfd MACH value from archures.c 6674 return the equivalent ELF flags from the table. 6675 Return -1 if no match is found. */ 6676 6677 int 6678 sh_elf_get_flags_from_mach (unsigned long mach) 6679 { 6680 int i = ARRAY_SIZE (sh_ef_bfd_table) - 1; 6681 6682 for (; i>0; i--) 6683 if (sh_ef_bfd_table[i] == mach) 6684 return i; 6685 6686 /* shouldn't get here */ 6687 BFD_FAIL(); 6688 6689 return -1; 6690 } 6691 #endif /* not sh_elf_set_mach_from_flags */ 6692 6693 #ifndef sh_elf_set_private_flags 6694 /* Function to keep SH specific file flags. */ 6695 6696 static bfd_boolean 6697 sh_elf_set_private_flags (bfd *abfd, flagword flags) 6698 { 6699 BFD_ASSERT (! elf_flags_init (abfd) 6700 || elf_elfheader (abfd)->e_flags == flags); 6701 6702 elf_elfheader (abfd)->e_flags = flags; 6703 elf_flags_init (abfd) = TRUE; 6704 return sh_elf_set_mach_from_flags (abfd); 6705 } 6706 #endif /* not sh_elf_set_private_flags */ 6707 6708 #ifndef sh_elf_copy_private_data 6709 /* Copy backend specific data from one object module to another */ 6710 6711 static bfd_boolean 6712 sh_elf_copy_private_data (bfd * ibfd, bfd * obfd) 6713 { 6714 /* Copy object attributes. */ 6715 _bfd_elf_copy_obj_attributes (ibfd, obfd); 6716 6717 if (! is_sh_elf (ibfd) || ! is_sh_elf (obfd)) 6718 return TRUE; 6719 6720 /* Copy the stack size. */ 6721 if (elf_tdata (ibfd)->phdr && elf_tdata (obfd)->phdr 6722 && fdpic_object_p (ibfd) && fdpic_object_p (obfd)) 6723 { 6724 unsigned i; 6725 6726 for (i = 0; i < elf_elfheader (ibfd)->e_phnum; i++) 6727 if (elf_tdata (ibfd)->phdr[i].p_type == PT_GNU_STACK) 6728 { 6729 Elf_Internal_Phdr *iphdr = &elf_tdata (ibfd)->phdr[i]; 6730 6731 for (i = 0; i < elf_elfheader (obfd)->e_phnum; i++) 6732 if (elf_tdata (obfd)->phdr[i].p_type == PT_GNU_STACK) 6733 { 6734 memcpy (&elf_tdata (obfd)->phdr[i], iphdr, sizeof (*iphdr)); 6735 6736 /* Rewrite the phdrs, since we're only called after they 6737 were first written. */ 6738 if (bfd_seek (obfd, 6739 (bfd_signed_vma) get_elf_backend_data (obfd) 6740 ->s->sizeof_ehdr, SEEK_SET) != 0 6741 || get_elf_backend_data (obfd)->s 6742 ->write_out_phdrs (obfd, elf_tdata (obfd)->phdr, 6743 elf_elfheader (obfd)->e_phnum) != 0) 6744 return FALSE; 6745 break; 6746 } 6747 6748 break; 6749 } 6750 } 6751 6752 return sh_elf_set_private_flags (obfd, elf_elfheader (ibfd)->e_flags); 6753 } 6754 #endif /* not sh_elf_copy_private_data */ 6755 6756 #ifndef sh_elf_merge_private_data 6757 6758 /* This function returns the ELF architecture number that 6759 corresponds to the given arch_sh* flags. */ 6760 6761 int 6762 sh_find_elf_flags (unsigned int arch_set) 6763 { 6764 extern unsigned long sh_get_bfd_mach_from_arch_set (unsigned int); 6765 unsigned long bfd_mach = sh_get_bfd_mach_from_arch_set (arch_set); 6766 6767 return sh_elf_get_flags_from_mach (bfd_mach); 6768 } 6769 6770 /* This routine initialises the elf flags when required and 6771 calls sh_merge_bfd_arch() to check dsp/fpu compatibility. */ 6772 6773 static bfd_boolean 6774 sh_elf_merge_private_data (bfd *ibfd, bfd *obfd) 6775 { 6776 extern bfd_boolean sh_merge_bfd_arch (bfd *, bfd *); 6777 6778 if (! is_sh_elf (ibfd) || ! is_sh_elf (obfd)) 6779 return TRUE; 6780 6781 if (! elf_flags_init (obfd)) 6782 { 6783 /* This happens when ld starts out with a 'blank' output file. */ 6784 elf_flags_init (obfd) = TRUE; 6785 elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags; 6786 sh_elf_set_mach_from_flags (obfd); 6787 if (elf_elfheader (obfd)->e_flags & EF_SH_FDPIC) 6788 elf_elfheader (obfd)->e_flags |= EF_SH_PIC; 6789 } 6790 6791 if (! sh_merge_bfd_arch (ibfd, obfd)) 6792 { 6793 _bfd_error_handler ("%B: uses instructions which are incompatible " 6794 "with instructions used in previous modules", 6795 ibfd); 6796 bfd_set_error (bfd_error_bad_value); 6797 return FALSE; 6798 } 6799 6800 elf_elfheader (obfd)->e_flags &= ~EF_SH_MACH_MASK; 6801 elf_elfheader (obfd)->e_flags |= 6802 sh_elf_get_flags_from_mach (bfd_get_mach (obfd)); 6803 6804 if (fdpic_object_p (ibfd) != fdpic_object_p (obfd)) 6805 { 6806 _bfd_error_handler ("%B: attempt to mix FDPIC and non-FDPIC objects", 6807 ibfd); 6808 bfd_set_error (bfd_error_bad_value); 6809 return FALSE; 6810 } 6811 6812 return TRUE; 6813 } 6814 #endif /* not sh_elf_merge_private_data */ 6815 6816 /* Override the generic function because we need to store sh_elf_obj_tdata 6817 as the specific tdata. We set also the machine architecture from flags 6818 here. */ 6819 6820 static bfd_boolean 6821 sh_elf_object_p (bfd *abfd) 6822 { 6823 if (! sh_elf_set_mach_from_flags (abfd)) 6824 return FALSE; 6825 6826 return (((elf_elfheader (abfd)->e_flags & EF_SH_FDPIC) != 0) 6827 == fdpic_object_p (abfd)); 6828 } 6829 6830 /* Finish up dynamic symbol handling. We set the contents of various 6831 dynamic sections here. */ 6832 6833 static bfd_boolean 6834 sh_elf_finish_dynamic_symbol (bfd *output_bfd, struct bfd_link_info *info, 6835 struct elf_link_hash_entry *h, 6836 Elf_Internal_Sym *sym) 6837 { 6838 struct elf_sh_link_hash_table *htab; 6839 6840 htab = sh_elf_hash_table (info); 6841 if (htab == NULL) 6842 return FALSE; 6843 6844 if (h->plt.offset != (bfd_vma) -1) 6845 { 6846 asection *splt; 6847 asection *sgotplt; 6848 asection *srelplt; 6849 6850 bfd_vma plt_index; 6851 bfd_vma got_offset; 6852 Elf_Internal_Rela rel; 6853 bfd_byte *loc; 6854 const struct elf_sh_plt_info *plt_info; 6855 6856 /* This symbol has an entry in the procedure linkage table. Set 6857 it up. */ 6858 6859 BFD_ASSERT (h->dynindx != -1); 6860 6861 splt = htab->splt; 6862 sgotplt = htab->sgotplt; 6863 srelplt = htab->srelplt; 6864 BFD_ASSERT (splt != NULL && sgotplt != NULL && srelplt != NULL); 6865 6866 /* Get the index in the procedure linkage table which 6867 corresponds to this symbol. This is the index of this symbol 6868 in all the symbols for which we are making plt entries. The 6869 first entry in the procedure linkage table is reserved. */ 6870 plt_index = get_plt_index (htab->plt_info, h->plt.offset); 6871 6872 plt_info = htab->plt_info; 6873 if (plt_info->short_plt != NULL && plt_index <= MAX_SHORT_PLT) 6874 plt_info = plt_info->short_plt; 6875 6876 /* Get the offset into the .got table of the entry that 6877 corresponds to this function. */ 6878 if (htab->fdpic_p) 6879 /* The offset must be relative to the GOT symbol, twelve bytes 6880 before the end of .got.plt. Each descriptor is eight 6881 bytes. */ 6882 got_offset = plt_index * 8 + 12 - sgotplt->size; 6883 else 6884 /* Each .got entry is 4 bytes. The first three are 6885 reserved. */ 6886 got_offset = (plt_index + 3) * 4; 6887 6888 #ifdef GOT_BIAS 6889 if (info->shared) 6890 got_offset -= GOT_BIAS; 6891 #endif 6892 6893 /* Fill in the entry in the procedure linkage table. */ 6894 memcpy (splt->contents + h->plt.offset, 6895 plt_info->symbol_entry, 6896 plt_info->symbol_entry_size); 6897 6898 if (info->shared || htab->fdpic_p) 6899 { 6900 if (plt_info->symbol_fields.got20) 6901 { 6902 bfd_reloc_status_type r; 6903 r = install_movi20_field (output_bfd, got_offset, 6904 splt->owner, splt, splt->contents, 6905 h->plt.offset 6906 + plt_info->symbol_fields.got_entry); 6907 BFD_ASSERT (r == bfd_reloc_ok); 6908 } 6909 else 6910 install_plt_field (output_bfd, FALSE, got_offset, 6911 (splt->contents 6912 + h->plt.offset 6913 + plt_info->symbol_fields.got_entry)); 6914 } 6915 else 6916 { 6917 BFD_ASSERT (!plt_info->symbol_fields.got20); 6918 6919 install_plt_field (output_bfd, FALSE, 6920 (sgotplt->output_section->vma 6921 + sgotplt->output_offset 6922 + got_offset), 6923 (splt->contents 6924 + h->plt.offset 6925 + plt_info->symbol_fields.got_entry)); 6926 if (htab->vxworks_p) 6927 { 6928 unsigned int reachable_plts, plts_per_4k; 6929 int distance; 6930 6931 /* Divide the PLT into groups. The first group contains 6932 REACHABLE_PLTS entries and the other groups contain 6933 PLTS_PER_4K entries. Entries in the first group can 6934 branch directly to .plt; those in later groups branch 6935 to the last element of the previous group. */ 6936 /* ??? It would be better to create multiple copies of 6937 the common resolver stub. */ 6938 reachable_plts = ((4096 6939 - plt_info->plt0_entry_size 6940 - (plt_info->symbol_fields.plt + 4)) 6941 / plt_info->symbol_entry_size) + 1; 6942 plts_per_4k = (4096 / plt_info->symbol_entry_size); 6943 if (plt_index < reachable_plts) 6944 distance = -(h->plt.offset 6945 + plt_info->symbol_fields.plt); 6946 else 6947 distance = -(((plt_index - reachable_plts) % plts_per_4k + 1) 6948 * plt_info->symbol_entry_size); 6949 6950 /* Install the 'bra' with this offset. */ 6951 bfd_put_16 (output_bfd, 6952 0xa000 | (0x0fff & ((distance - 4) / 2)), 6953 (splt->contents 6954 + h->plt.offset 6955 + plt_info->symbol_fields.plt)); 6956 } 6957 else 6958 install_plt_field (output_bfd, TRUE, 6959 splt->output_section->vma + splt->output_offset, 6960 (splt->contents 6961 + h->plt.offset 6962 + plt_info->symbol_fields.plt)); 6963 } 6964 6965 /* Make got_offset relative to the start of .got.plt. */ 6966 #ifdef GOT_BIAS 6967 if (info->shared) 6968 got_offset += GOT_BIAS; 6969 #endif 6970 if (htab->fdpic_p) 6971 got_offset = plt_index * 8; 6972 6973 if (plt_info->symbol_fields.reloc_offset != MINUS_ONE) 6974 install_plt_field (output_bfd, FALSE, 6975 plt_index * sizeof (Elf32_External_Rela), 6976 (splt->contents 6977 + h->plt.offset 6978 + plt_info->symbol_fields.reloc_offset)); 6979 6980 /* Fill in the entry in the global offset table. */ 6981 bfd_put_32 (output_bfd, 6982 (splt->output_section->vma 6983 + splt->output_offset 6984 + h->plt.offset 6985 + plt_info->symbol_resolve_offset), 6986 sgotplt->contents + got_offset); 6987 if (htab->fdpic_p) 6988 bfd_put_32 (output_bfd, 6989 sh_elf_osec_to_segment (output_bfd, 6990 htab->splt->output_section), 6991 sgotplt->contents + got_offset + 4); 6992 6993 /* Fill in the entry in the .rela.plt section. */ 6994 rel.r_offset = (sgotplt->output_section->vma 6995 + sgotplt->output_offset 6996 + got_offset); 6997 if (htab->fdpic_p) 6998 rel.r_info = ELF32_R_INFO (h->dynindx, R_SH_FUNCDESC_VALUE); 6999 else 7000 rel.r_info = ELF32_R_INFO (h->dynindx, R_SH_JMP_SLOT); 7001 rel.r_addend = 0; 7002 #ifdef GOT_BIAS 7003 rel.r_addend = GOT_BIAS; 7004 #endif 7005 loc = srelplt->contents + plt_index * sizeof (Elf32_External_Rela); 7006 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc); 7007 7008 if (htab->vxworks_p && !info->shared) 7009 { 7010 /* Create the .rela.plt.unloaded relocations for this PLT entry. 7011 Begin by pointing LOC to the first such relocation. */ 7012 loc = (htab->srelplt2->contents 7013 + (plt_index * 2 + 1) * sizeof (Elf32_External_Rela)); 7014 7015 /* Create a .rela.plt.unloaded R_SH_DIR32 relocation 7016 for the PLT entry's pointer to the .got.plt entry. */ 7017 rel.r_offset = (htab->splt->output_section->vma 7018 + htab->splt->output_offset 7019 + h->plt.offset 7020 + plt_info->symbol_fields.got_entry); 7021 rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_SH_DIR32); 7022 rel.r_addend = got_offset; 7023 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc); 7024 loc += sizeof (Elf32_External_Rela); 7025 7026 /* Create a .rela.plt.unloaded R_SH_DIR32 relocation for 7027 the .got.plt entry, which initially points to .plt. */ 7028 rel.r_offset = (sgotplt->output_section->vma 7029 + sgotplt->output_offset 7030 + got_offset); 7031 rel.r_info = ELF32_R_INFO (htab->root.hplt->indx, R_SH_DIR32); 7032 rel.r_addend = 0; 7033 bfd_elf32_swap_reloc_out (output_bfd, &rel, loc); 7034 } 7035 7036 if (!h->def_regular) 7037 { 7038 /* Mark the symbol as undefined, rather than as defined in 7039 the .plt section. Leave the value alone. */ 7040 sym->st_shndx = SHN_UNDEF; 7041 } 7042 } 7043 7044 if (h->got.offset != (bfd_vma) -1 7045 && sh_elf_hash_entry (h)->got_type != GOT_TLS_GD 7046 && sh_elf_hash_entry (h)->got_type != GOT_TLS_IE 7047 && sh_elf_hash_entry (h)->got_type != GOT_FUNCDESC) 7048 { 7049 asection *sgot; 7050 asection *srelgot; 7051 Elf_Internal_Rela rel; 7052 bfd_byte *loc; 7053 7054 /* This symbol has an entry in the global offset table. Set it 7055 up. */ 7056 7057 sgot = htab->sgot; 7058 srelgot = htab->srelgot; 7059 BFD_ASSERT (sgot != NULL && srelgot != NULL); 7060 7061 rel.r_offset = (sgot->output_section->vma 7062 + sgot->output_offset 7063 + (h->got.offset &~ (bfd_vma) 1)); 7064 7065 /* If this is a static link, or it is a -Bsymbolic link and the 7066 symbol is defined locally or was forced to be local because 7067 of a version file, we just want to emit a RELATIVE reloc. 7068 The entry in the global offset table will already have been 7069 initialized in the relocate_section function. */ 7070 if (info->shared 7071 && SYMBOL_REFERENCES_LOCAL (info, h)) 7072 { 7073 if (htab->fdpic_p) 7074 { 7075 asection *sec = h->root.u.def.section; 7076 int dynindx 7077 = elf_section_data (sec->output_section)->dynindx; 7078 7079 rel.r_info = ELF32_R_INFO (dynindx, R_SH_DIR32); 7080 rel.r_addend = (h->root.u.def.value 7081 + h->root.u.def.section->output_offset); 7082 } 7083 else 7084 { 7085 rel.r_info = ELF32_R_INFO (0, R_SH_RELATIVE); 7086 rel.r_addend = (h->root.u.def.value 7087 + h->root.u.def.section->output_section->vma 7088 + h->root.u.def.section->output_offset); 7089 } 7090 } 7091 else 7092 { 7093 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + h->got.offset); 7094 rel.r_info = ELF32_R_INFO (h->dynindx, R_SH_GLOB_DAT); 7095 rel.r_addend = 0; 7096 } 7097 7098 loc = srelgot->contents; 7099 loc += srelgot->reloc_count++ * sizeof (Elf32_External_Rela); 7100 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc); 7101 } 7102 7103 #ifdef INCLUDE_SHMEDIA 7104 { 7105 struct elf_sh_link_hash_entry *eh; 7106 7107 eh = (struct elf_sh_link_hash_entry *) h; 7108 if (eh->datalabel_got.offset != (bfd_vma) -1) 7109 { 7110 asection *sgot; 7111 asection *srelgot; 7112 Elf_Internal_Rela rel; 7113 bfd_byte *loc; 7114 7115 /* This symbol has a datalabel entry in the global offset table. 7116 Set it up. */ 7117 7118 sgot = htab->sgot; 7119 srelgot = htab->srelgot; 7120 BFD_ASSERT (sgot != NULL && srelgot != NULL); 7121 7122 rel.r_offset = (sgot->output_section->vma 7123 + sgot->output_offset 7124 + (eh->datalabel_got.offset &~ (bfd_vma) 1)); 7125 7126 /* If this is a static link, or it is a -Bsymbolic link and the 7127 symbol is defined locally or was forced to be local because 7128 of a version file, we just want to emit a RELATIVE reloc. 7129 The entry in the global offset table will already have been 7130 initialized in the relocate_section function. */ 7131 if (info->shared 7132 && SYMBOL_REFERENCES_LOCAL (info, h)) 7133 { 7134 if (htab->fdpic_p) 7135 { 7136 asection *sec = h->root.u.def.section; 7137 int dynindx 7138 = elf_section_data (sec->output_section)->dynindx; 7139 7140 rel.r_info = ELF32_R_INFO (dynindx, R_SH_DIR32); 7141 rel.r_addend = (h->root.u.def.value 7142 + h->root.u.def.section->output_offset); 7143 } 7144 else 7145 { 7146 rel.r_info = ELF32_R_INFO (0, R_SH_RELATIVE); 7147 rel.r_addend = (h->root.u.def.value 7148 + h->root.u.def.section->output_section->vma 7149 + h->root.u.def.section->output_offset); 7150 } 7151 } 7152 else 7153 { 7154 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents 7155 + eh->datalabel_got.offset); 7156 rel.r_info = ELF32_R_INFO (h->dynindx, R_SH_GLOB_DAT); 7157 rel.r_addend = 0; 7158 } 7159 7160 loc = srelgot->contents; 7161 loc += srelgot->reloc_count++ * sizeof (Elf32_External_Rela); 7162 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc); 7163 } 7164 } 7165 #endif 7166 7167 if (h->needs_copy) 7168 { 7169 asection *s; 7170 Elf_Internal_Rela rel; 7171 bfd_byte *loc; 7172 7173 /* This symbol needs a copy reloc. Set it up. */ 7174 7175 BFD_ASSERT (h->dynindx != -1 7176 && (h->root.type == bfd_link_hash_defined 7177 || h->root.type == bfd_link_hash_defweak)); 7178 7179 s = bfd_get_section_by_name (h->root.u.def.section->owner, 7180 ".rela.bss"); 7181 BFD_ASSERT (s != NULL); 7182 7183 rel.r_offset = (h->root.u.def.value 7184 + h->root.u.def.section->output_section->vma 7185 + h->root.u.def.section->output_offset); 7186 rel.r_info = ELF32_R_INFO (h->dynindx, R_SH_COPY); 7187 rel.r_addend = 0; 7188 loc = s->contents + s->reloc_count++ * sizeof (Elf32_External_Rela); 7189 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc); 7190 } 7191 7192 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. On VxWorks, 7193 _GLOBAL_OFFSET_TABLE_ is not absolute: it is relative to the 7194 ".got" section. */ 7195 if (strcmp (h->root.root.string, "_DYNAMIC") == 0 7196 || (!htab->vxworks_p && h == htab->root.hgot)) 7197 sym->st_shndx = SHN_ABS; 7198 7199 return TRUE; 7200 } 7201 7202 /* Finish up the dynamic sections. */ 7203 7204 static bfd_boolean 7205 sh_elf_finish_dynamic_sections (bfd *output_bfd, struct bfd_link_info *info) 7206 { 7207 struct elf_sh_link_hash_table *htab; 7208 asection *sgotplt; 7209 asection *sdyn; 7210 7211 htab = sh_elf_hash_table (info); 7212 if (htab == NULL) 7213 return FALSE; 7214 7215 sgotplt = htab->sgotplt; 7216 sdyn = bfd_get_section_by_name (htab->root.dynobj, ".dynamic"); 7217 7218 if (htab->root.dynamic_sections_created) 7219 { 7220 asection *splt; 7221 Elf32_External_Dyn *dyncon, *dynconend; 7222 7223 BFD_ASSERT (sgotplt != NULL && sdyn != NULL); 7224 7225 dyncon = (Elf32_External_Dyn *) sdyn->contents; 7226 dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size); 7227 for (; dyncon < dynconend; dyncon++) 7228 { 7229 Elf_Internal_Dyn dyn; 7230 asection *s; 7231 #ifdef INCLUDE_SHMEDIA 7232 const char *name; 7233 #endif 7234 7235 bfd_elf32_swap_dyn_in (htab->root.dynobj, dyncon, &dyn); 7236 7237 switch (dyn.d_tag) 7238 { 7239 default: 7240 if (htab->vxworks_p 7241 && elf_vxworks_finish_dynamic_entry (output_bfd, &dyn)) 7242 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon); 7243 break; 7244 7245 #ifdef INCLUDE_SHMEDIA 7246 case DT_INIT: 7247 name = info->init_function; 7248 goto get_sym; 7249 7250 case DT_FINI: 7251 name = info->fini_function; 7252 get_sym: 7253 if (dyn.d_un.d_val != 0) 7254 { 7255 struct elf_link_hash_entry *h; 7256 7257 h = elf_link_hash_lookup (&htab->root, name, 7258 FALSE, FALSE, TRUE); 7259 if (h != NULL && (h->other & STO_SH5_ISA32)) 7260 { 7261 dyn.d_un.d_val |= 1; 7262 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon); 7263 } 7264 } 7265 break; 7266 #endif 7267 7268 case DT_PLTGOT: 7269 BFD_ASSERT (htab->root.hgot != NULL); 7270 s = htab->root.hgot->root.u.def.section; 7271 dyn.d_un.d_ptr = htab->root.hgot->root.u.def.value 7272 + s->output_section->vma + s->output_offset; 7273 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon); 7274 break; 7275 7276 case DT_JMPREL: 7277 s = htab->srelplt->output_section; 7278 BFD_ASSERT (s != NULL); 7279 dyn.d_un.d_ptr = s->vma; 7280 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon); 7281 break; 7282 7283 case DT_PLTRELSZ: 7284 s = htab->srelplt->output_section; 7285 BFD_ASSERT (s != NULL); 7286 dyn.d_un.d_val = s->size; 7287 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon); 7288 break; 7289 7290 case DT_RELASZ: 7291 /* My reading of the SVR4 ABI indicates that the 7292 procedure linkage table relocs (DT_JMPREL) should be 7293 included in the overall relocs (DT_RELA). This is 7294 what Solaris does. However, UnixWare can not handle 7295 that case. Therefore, we override the DT_RELASZ entry 7296 here to make it not include the JMPREL relocs. Since 7297 the linker script arranges for .rela.plt to follow all 7298 other relocation sections, we don't have to worry 7299 about changing the DT_RELA entry. */ 7300 if (htab->srelplt != NULL) 7301 { 7302 s = htab->srelplt->output_section; 7303 dyn.d_un.d_val -= s->size; 7304 } 7305 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon); 7306 break; 7307 } 7308 } 7309 7310 /* Fill in the first entry in the procedure linkage table. */ 7311 splt = htab->splt; 7312 if (splt && splt->size > 0 && htab->plt_info->plt0_entry) 7313 { 7314 unsigned int i; 7315 7316 memcpy (splt->contents, 7317 htab->plt_info->plt0_entry, 7318 htab->plt_info->plt0_entry_size); 7319 for (i = 0; i < ARRAY_SIZE (htab->plt_info->plt0_got_fields); i++) 7320 if (htab->plt_info->plt0_got_fields[i] != MINUS_ONE) 7321 install_plt_field (output_bfd, FALSE, 7322 (sgotplt->output_section->vma 7323 + sgotplt->output_offset 7324 + (i * 4)), 7325 (splt->contents 7326 + htab->plt_info->plt0_got_fields[i])); 7327 7328 if (htab->vxworks_p) 7329 { 7330 /* Finalize the .rela.plt.unloaded contents. */ 7331 Elf_Internal_Rela rel; 7332 bfd_byte *loc; 7333 7334 /* Create a .rela.plt.unloaded R_SH_DIR32 relocation for the 7335 first PLT entry's pointer to _GLOBAL_OFFSET_TABLE_ + 8. */ 7336 loc = htab->srelplt2->contents; 7337 rel.r_offset = (splt->output_section->vma 7338 + splt->output_offset 7339 + htab->plt_info->plt0_got_fields[2]); 7340 rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_SH_DIR32); 7341 rel.r_addend = 8; 7342 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc); 7343 loc += sizeof (Elf32_External_Rela); 7344 7345 /* Fix up the remaining .rela.plt.unloaded relocations. 7346 They may have the wrong symbol index for _G_O_T_ or 7347 _P_L_T_ depending on the order in which symbols were 7348 output. */ 7349 while (loc < htab->srelplt2->contents + htab->srelplt2->size) 7350 { 7351 /* The PLT entry's pointer to the .got.plt slot. */ 7352 bfd_elf32_swap_reloc_in (output_bfd, loc, &rel); 7353 rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, 7354 R_SH_DIR32); 7355 bfd_elf32_swap_reloc_out (output_bfd, &rel, loc); 7356 loc += sizeof (Elf32_External_Rela); 7357 7358 /* The .got.plt slot's pointer to .plt. */ 7359 bfd_elf32_swap_reloc_in (output_bfd, loc, &rel); 7360 rel.r_info = ELF32_R_INFO (htab->root.hplt->indx, 7361 R_SH_DIR32); 7362 bfd_elf32_swap_reloc_out (output_bfd, &rel, loc); 7363 loc += sizeof (Elf32_External_Rela); 7364 } 7365 } 7366 7367 /* UnixWare sets the entsize of .plt to 4, although that doesn't 7368 really seem like the right value. */ 7369 elf_section_data (splt->output_section)->this_hdr.sh_entsize = 4; 7370 } 7371 } 7372 7373 /* Fill in the first three entries in the global offset table. */ 7374 if (sgotplt && sgotplt->size > 0 && !htab->fdpic_p) 7375 { 7376 if (sdyn == NULL) 7377 bfd_put_32 (output_bfd, (bfd_vma) 0, sgotplt->contents); 7378 else 7379 bfd_put_32 (output_bfd, 7380 sdyn->output_section->vma + sdyn->output_offset, 7381 sgotplt->contents); 7382 bfd_put_32 (output_bfd, (bfd_vma) 0, sgotplt->contents + 4); 7383 bfd_put_32 (output_bfd, (bfd_vma) 0, sgotplt->contents + 8); 7384 } 7385 7386 if (sgotplt && sgotplt->size > 0) 7387 elf_section_data (sgotplt->output_section)->this_hdr.sh_entsize = 4; 7388 7389 /* At the very end of the .rofixup section is a pointer to the GOT. */ 7390 if (htab->fdpic_p && htab->srofixup != NULL) 7391 { 7392 struct elf_link_hash_entry *hgot = htab->root.hgot; 7393 bfd_vma got_value = hgot->root.u.def.value 7394 + hgot->root.u.def.section->output_section->vma 7395 + hgot->root.u.def.section->output_offset; 7396 7397 sh_elf_add_rofixup (output_bfd, htab->srofixup, got_value); 7398 7399 /* Make sure we allocated and generated the same number of fixups. */ 7400 BFD_ASSERT (htab->srofixup->reloc_count * 4 == htab->srofixup->size); 7401 } 7402 7403 if (htab->srelfuncdesc) 7404 BFD_ASSERT (htab->srelfuncdesc->reloc_count * sizeof (Elf32_External_Rela) 7405 == htab->srelfuncdesc->size); 7406 7407 if (htab->srelgot) 7408 BFD_ASSERT (htab->srelgot->reloc_count * sizeof (Elf32_External_Rela) 7409 == htab->srelgot->size); 7410 7411 return TRUE; 7412 } 7413 7414 static enum elf_reloc_type_class 7415 sh_elf_reloc_type_class (const Elf_Internal_Rela *rela) 7416 { 7417 switch ((int) ELF32_R_TYPE (rela->r_info)) 7418 { 7419 case R_SH_RELATIVE: 7420 return reloc_class_relative; 7421 case R_SH_JMP_SLOT: 7422 return reloc_class_plt; 7423 case R_SH_COPY: 7424 return reloc_class_copy; 7425 default: 7426 return reloc_class_normal; 7427 } 7428 } 7429 7430 #if !defined SH_TARGET_ALREADY_DEFINED 7431 /* Support for Linux core dump NOTE sections. */ 7432 7433 static bfd_boolean 7434 elf32_shlin_grok_prstatus (bfd *abfd, Elf_Internal_Note *note) 7435 { 7436 int offset; 7437 unsigned int size; 7438 7439 switch (note->descsz) 7440 { 7441 default: 7442 return FALSE; 7443 7444 case 168: /* Linux/SH */ 7445 /* pr_cursig */ 7446 elf_tdata (abfd)->core_signal = bfd_get_16 (abfd, note->descdata + 12); 7447 7448 /* pr_pid */ 7449 elf_tdata (abfd)->core_lwpid = bfd_get_32 (abfd, note->descdata + 24); 7450 7451 /* pr_reg */ 7452 offset = 72; 7453 size = 92; 7454 7455 break; 7456 } 7457 7458 /* Make a ".reg/999" section. */ 7459 return _bfd_elfcore_make_pseudosection (abfd, ".reg", 7460 size, note->descpos + offset); 7461 } 7462 7463 static bfd_boolean 7464 elf32_shlin_grok_psinfo (bfd *abfd, Elf_Internal_Note *note) 7465 { 7466 switch (note->descsz) 7467 { 7468 default: 7469 return FALSE; 7470 7471 case 124: /* Linux/SH elf_prpsinfo */ 7472 elf_tdata (abfd)->core_program 7473 = _bfd_elfcore_strndup (abfd, note->descdata + 28, 16); 7474 elf_tdata (abfd)->core_command 7475 = _bfd_elfcore_strndup (abfd, note->descdata + 44, 80); 7476 } 7477 7478 /* Note that for some reason, a spurious space is tacked 7479 onto the end of the args in some (at least one anyway) 7480 implementations, so strip it off if it exists. */ 7481 7482 { 7483 char *command = elf_tdata (abfd)->core_command; 7484 int n = strlen (command); 7485 7486 if (0 < n && command[n - 1] == ' ') 7487 command[n - 1] = '\0'; 7488 } 7489 7490 return TRUE; 7491 } 7492 #endif /* not SH_TARGET_ALREADY_DEFINED */ 7493 7494 7495 /* Return address for Ith PLT stub in section PLT, for relocation REL 7496 or (bfd_vma) -1 if it should not be included. */ 7497 7498 static bfd_vma 7499 sh_elf_plt_sym_val (bfd_vma i, const asection *plt, 7500 const arelent *rel ATTRIBUTE_UNUSED) 7501 { 7502 const struct elf_sh_plt_info *plt_info; 7503 7504 plt_info = get_plt_info (plt->owner, (plt->owner->flags & DYNAMIC) != 0); 7505 return plt->vma + get_plt_offset (plt_info, i); 7506 } 7507 7508 /* Decide whether to attempt to turn absptr or lsda encodings in 7509 shared libraries into pcrel within the given input section. */ 7510 7511 static bfd_boolean 7512 sh_elf_use_relative_eh_frame (bfd *input_bfd ATTRIBUTE_UNUSED, 7513 struct bfd_link_info *info, 7514 asection *eh_frame_section ATTRIBUTE_UNUSED) 7515 { 7516 struct elf_sh_link_hash_table *htab = sh_elf_hash_table (info); 7517 7518 /* We can't use PC-relative encodings in FDPIC binaries, in general. */ 7519 if (htab->fdpic_p) 7520 return FALSE; 7521 7522 return TRUE; 7523 } 7524 7525 /* Adjust the contents of an eh_frame_hdr section before they're output. */ 7526 7527 static bfd_byte 7528 sh_elf_encode_eh_address (bfd *abfd, 7529 struct bfd_link_info *info, 7530 asection *osec, bfd_vma offset, 7531 asection *loc_sec, bfd_vma loc_offset, 7532 bfd_vma *encoded) 7533 { 7534 struct elf_sh_link_hash_table *htab = sh_elf_hash_table (info); 7535 struct elf_link_hash_entry *h; 7536 7537 if (!htab->fdpic_p) 7538 return _bfd_elf_encode_eh_address (abfd, info, osec, offset, loc_sec, 7539 loc_offset, encoded); 7540 7541 h = htab->root.hgot; 7542 BFD_ASSERT (h && h->root.type == bfd_link_hash_defined); 7543 7544 if (! h || (sh_elf_osec_to_segment (abfd, osec) 7545 == sh_elf_osec_to_segment (abfd, loc_sec->output_section))) 7546 return _bfd_elf_encode_eh_address (abfd, info, osec, offset, 7547 loc_sec, loc_offset, encoded); 7548 7549 BFD_ASSERT (sh_elf_osec_to_segment (abfd, osec) 7550 == (sh_elf_osec_to_segment 7551 (abfd, h->root.u.def.section->output_section))); 7552 7553 *encoded = osec->vma + offset 7554 - (h->root.u.def.value 7555 + h->root.u.def.section->output_section->vma 7556 + h->root.u.def.section->output_offset); 7557 7558 return DW_EH_PE_datarel | DW_EH_PE_sdata4; 7559 } 7560 7561 #if !defined SH_TARGET_ALREADY_DEFINED 7562 #define TARGET_BIG_SYM bfd_elf32_sh_vec 7563 #define TARGET_BIG_NAME "elf32-sh" 7564 #define TARGET_LITTLE_SYM bfd_elf32_shl_vec 7565 #define TARGET_LITTLE_NAME "elf32-shl" 7566 #endif 7567 7568 #define ELF_ARCH bfd_arch_sh 7569 #define ELF_TARGET_ID SH_ELF_DATA 7570 #define ELF_MACHINE_CODE EM_SH 7571 #ifdef __QNXTARGET__ 7572 #define ELF_MAXPAGESIZE 0x1000 7573 #else 7574 #define ELF_MAXPAGESIZE 0x80 7575 #endif 7576 7577 #define elf_symbol_leading_char '_' 7578 7579 #define bfd_elf32_bfd_reloc_type_lookup sh_elf_reloc_type_lookup 7580 #define bfd_elf32_bfd_reloc_name_lookup \ 7581 sh_elf_reloc_name_lookup 7582 #define elf_info_to_howto sh_elf_info_to_howto 7583 #define bfd_elf32_bfd_relax_section sh_elf_relax_section 7584 #define elf_backend_relocate_section sh_elf_relocate_section 7585 #define bfd_elf32_bfd_get_relocated_section_contents \ 7586 sh_elf_get_relocated_section_contents 7587 #define bfd_elf32_mkobject sh_elf_mkobject 7588 #define elf_backend_object_p sh_elf_object_p 7589 #define bfd_elf32_bfd_set_private_bfd_flags \ 7590 sh_elf_set_private_flags 7591 #define bfd_elf32_bfd_copy_private_bfd_data \ 7592 sh_elf_copy_private_data 7593 #define bfd_elf32_bfd_merge_private_bfd_data \ 7594 sh_elf_merge_private_data 7595 7596 #define elf_backend_gc_mark_hook sh_elf_gc_mark_hook 7597 #define elf_backend_gc_sweep_hook sh_elf_gc_sweep_hook 7598 #define elf_backend_check_relocs sh_elf_check_relocs 7599 #define elf_backend_copy_indirect_symbol \ 7600 sh_elf_copy_indirect_symbol 7601 #define elf_backend_create_dynamic_sections \ 7602 sh_elf_create_dynamic_sections 7603 #define bfd_elf32_bfd_link_hash_table_create \ 7604 sh_elf_link_hash_table_create 7605 #define elf_backend_adjust_dynamic_symbol \ 7606 sh_elf_adjust_dynamic_symbol 7607 #define elf_backend_always_size_sections \ 7608 sh_elf_always_size_sections 7609 #define elf_backend_size_dynamic_sections \ 7610 sh_elf_size_dynamic_sections 7611 #define elf_backend_omit_section_dynsym sh_elf_omit_section_dynsym 7612 #define elf_backend_finish_dynamic_symbol \ 7613 sh_elf_finish_dynamic_symbol 7614 #define elf_backend_finish_dynamic_sections \ 7615 sh_elf_finish_dynamic_sections 7616 #define elf_backend_reloc_type_class sh_elf_reloc_type_class 7617 #define elf_backend_plt_sym_val sh_elf_plt_sym_val 7618 #define elf_backend_can_make_relative_eh_frame \ 7619 sh_elf_use_relative_eh_frame 7620 #define elf_backend_can_make_lsda_relative_eh_frame \ 7621 sh_elf_use_relative_eh_frame 7622 #define elf_backend_encode_eh_address \ 7623 sh_elf_encode_eh_address 7624 7625 #define elf_backend_can_gc_sections 1 7626 #define elf_backend_can_refcount 1 7627 #define elf_backend_want_got_plt 1 7628 #define elf_backend_plt_readonly 1 7629 #define elf_backend_want_plt_sym 0 7630 #define elf_backend_got_header_size 12 7631 7632 #if !defined INCLUDE_SHMEDIA && !defined SH_TARGET_ALREADY_DEFINED 7633 7634 #include "elf32-target.h" 7635 7636 /* NetBSD support. */ 7637 #undef TARGET_BIG_SYM 7638 #define TARGET_BIG_SYM bfd_elf32_shnbsd_vec 7639 #undef TARGET_BIG_NAME 7640 #define TARGET_BIG_NAME "elf32-sh-nbsd" 7641 #undef TARGET_LITTLE_SYM 7642 #define TARGET_LITTLE_SYM bfd_elf32_shlnbsd_vec 7643 #undef TARGET_LITTLE_NAME 7644 #define TARGET_LITTLE_NAME "elf32-shl-nbsd" 7645 #undef ELF_MAXPAGESIZE 7646 #define ELF_MAXPAGESIZE 0x10000 7647 #undef ELF_COMMONPAGESIZE 7648 #undef elf_symbol_leading_char 7649 #define elf_symbol_leading_char 0 7650 #undef elf32_bed 7651 #define elf32_bed elf32_sh_nbsd_bed 7652 7653 #include "elf32-target.h" 7654 7655 7656 /* Linux support. */ 7657 #undef TARGET_BIG_SYM 7658 #define TARGET_BIG_SYM bfd_elf32_shblin_vec 7659 #undef TARGET_BIG_NAME 7660 #define TARGET_BIG_NAME "elf32-shbig-linux" 7661 #undef TARGET_LITTLE_SYM 7662 #define TARGET_LITTLE_SYM bfd_elf32_shlin_vec 7663 #undef TARGET_LITTLE_NAME 7664 #define TARGET_LITTLE_NAME "elf32-sh-linux" 7665 #undef ELF_COMMONPAGESIZE 7666 #define ELF_COMMONPAGESIZE 0x1000 7667 7668 #undef elf_backend_grok_prstatus 7669 #define elf_backend_grok_prstatus elf32_shlin_grok_prstatus 7670 #undef elf_backend_grok_psinfo 7671 #define elf_backend_grok_psinfo elf32_shlin_grok_psinfo 7672 #undef elf32_bed 7673 #define elf32_bed elf32_sh_lin_bed 7674 7675 #include "elf32-target.h" 7676 7677 7678 /* FDPIC support. */ 7679 #undef TARGET_BIG_SYM 7680 #define TARGET_BIG_SYM bfd_elf32_shbfd_vec 7681 #undef TARGET_BIG_NAME 7682 #define TARGET_BIG_NAME "elf32-shbig-fdpic" 7683 #undef TARGET_LITTLE_SYM 7684 #define TARGET_LITTLE_SYM bfd_elf32_shfd_vec 7685 #undef TARGET_LITTLE_NAME 7686 #define TARGET_LITTLE_NAME "elf32-sh-fdpic" 7687 #undef elf_backend_modify_program_headers 7688 #define elf_backend_modify_program_headers \ 7689 sh_elf_modify_program_headers 7690 7691 #undef elf32_bed 7692 #define elf32_bed elf32_sh_fd_bed 7693 7694 #include "elf32-target.h" 7695 7696 #undef elf_backend_modify_program_headers 7697 7698 /* VxWorks support. */ 7699 #undef TARGET_BIG_SYM 7700 #define TARGET_BIG_SYM bfd_elf32_shvxworks_vec 7701 #undef TARGET_BIG_NAME 7702 #define TARGET_BIG_NAME "elf32-sh-vxworks" 7703 #undef TARGET_LITTLE_SYM 7704 #define TARGET_LITTLE_SYM bfd_elf32_shlvxworks_vec 7705 #undef TARGET_LITTLE_NAME 7706 #define TARGET_LITTLE_NAME "elf32-shl-vxworks" 7707 #undef elf32_bed 7708 #define elf32_bed elf32_sh_vxworks_bed 7709 7710 #undef elf_backend_want_plt_sym 7711 #define elf_backend_want_plt_sym 1 7712 #undef elf_symbol_leading_char 7713 #define elf_symbol_leading_char '_' 7714 #define elf_backend_want_got_underscore 1 7715 #undef elf_backend_grok_prstatus 7716 #undef elf_backend_grok_psinfo 7717 #undef elf_backend_add_symbol_hook 7718 #define elf_backend_add_symbol_hook elf_vxworks_add_symbol_hook 7719 #undef elf_backend_link_output_symbol_hook 7720 #define elf_backend_link_output_symbol_hook \ 7721 elf_vxworks_link_output_symbol_hook 7722 #undef elf_backend_emit_relocs 7723 #define elf_backend_emit_relocs elf_vxworks_emit_relocs 7724 #undef elf_backend_final_write_processing 7725 #define elf_backend_final_write_processing \ 7726 elf_vxworks_final_write_processing 7727 #undef ELF_MAXPAGESIZE 7728 #define ELF_MAXPAGESIZE 0x1000 7729 #undef ELF_COMMONPAGESIZE 7730 7731 #include "elf32-target.h" 7732 7733 #endif /* neither INCLUDE_SHMEDIA nor SH_TARGET_ALREADY_DEFINED */ 7734