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