elf64-s390.c revision 107492
1/* IBM S/390-specific support for 64-bit ELF 2 Copyright 2000, 2001, 2002 Free Software Foundation, Inc. 3 Contributed Martin Schwidefsky (schwidefsky@de.ibm.com). 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 2 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., 59 Temple Place - Suite 330, Boston, MA 20 02111-1307, USA. */ 21 22#include "bfd.h" 23#include "sysdep.h" 24#include "bfdlink.h" 25#include "libbfd.h" 26#include "elf-bfd.h" 27 28static reloc_howto_type *elf_s390_reloc_type_lookup 29 PARAMS ((bfd *, bfd_reloc_code_real_type)); 30static void elf_s390_info_to_howto 31 PARAMS ((bfd *, arelent *, Elf_Internal_Rela *)); 32static boolean elf_s390_is_local_label_name 33 PARAMS ((bfd *, const char *)); 34static struct bfd_hash_entry *link_hash_newfunc 35 PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *, const char *)); 36static struct bfd_link_hash_table *elf_s390_link_hash_table_create 37 PARAMS ((bfd *)); 38static boolean create_got_section 39 PARAMS((bfd *, struct bfd_link_info *)); 40static boolean elf_s390_create_dynamic_sections 41 PARAMS((bfd *, struct bfd_link_info *)); 42static void elf_s390_copy_indirect_symbol 43 PARAMS ((struct elf_backend_data *, struct elf_link_hash_entry *, 44 struct elf_link_hash_entry *)); 45static boolean elf_s390_check_relocs 46 PARAMS ((bfd *, struct bfd_link_info *, asection *, 47 const Elf_Internal_Rela *)); 48static asection *elf_s390_gc_mark_hook 49 PARAMS ((asection *, struct bfd_link_info *, Elf_Internal_Rela *, 50 struct elf_link_hash_entry *, Elf_Internal_Sym *)); 51static boolean elf_s390_gc_sweep_hook 52 PARAMS ((bfd *, struct bfd_link_info *, asection *, 53 const Elf_Internal_Rela *)); 54static boolean elf_s390_adjust_dynamic_symbol 55 PARAMS ((struct bfd_link_info *, struct elf_link_hash_entry *)); 56static boolean allocate_dynrelocs 57 PARAMS ((struct elf_link_hash_entry *, PTR)); 58static boolean readonly_dynrelocs 59 PARAMS ((struct elf_link_hash_entry *, PTR)); 60static boolean elf_s390_size_dynamic_sections 61 PARAMS ((bfd *, struct bfd_link_info *)); 62static boolean elf_s390_relocate_section 63 PARAMS ((bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *, 64 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **)); 65static boolean elf_s390_finish_dynamic_symbol 66 PARAMS ((bfd *, struct bfd_link_info *, struct elf_link_hash_entry *, 67 Elf_Internal_Sym *)); 68static enum elf_reloc_type_class elf_s390_reloc_type_class 69 PARAMS ((const Elf_Internal_Rela *)); 70static boolean elf_s390_finish_dynamic_sections 71 PARAMS ((bfd *, struct bfd_link_info *)); 72static boolean elf_s390_object_p PARAMS ((bfd *)); 73 74#define USE_RELA 1 /* We want RELA relocations, not REL. */ 75 76#include "elf/s390.h" 77 78/* In case we're on a 32-bit machine, construct a 64-bit "-1" value 79 from smaller values. Start with zero, widen, *then* decrement. */ 80#define MINUS_ONE (((bfd_vma)0) - 1) 81 82/* The relocation "howto" table. */ 83static reloc_howto_type elf_howto_table[] = 84{ 85 HOWTO (R_390_NONE, /* type */ 86 0, /* rightshift */ 87 0, /* size (0 = byte, 1 = short, 2 = long) */ 88 0, /* bitsize */ 89 false, /* pc_relative */ 90 0, /* bitpos */ 91 complain_overflow_dont, /* complain_on_overflow */ 92 bfd_elf_generic_reloc, /* special_function */ 93 "R_390_NONE", /* name */ 94 false, /* partial_inplace */ 95 0, /* src_mask */ 96 0, /* dst_mask */ 97 false), /* pcrel_offset */ 98 99 HOWTO(R_390_8, 0, 0, 8, false, 0, complain_overflow_bitfield, bfd_elf_generic_reloc, "R_390_8", false, 0,0x000000ff, false), 100 HOWTO(R_390_12, 0, 1, 12, false, 0, complain_overflow_dont, bfd_elf_generic_reloc, "R_390_12", false, 0,0x00000fff, false), 101 HOWTO(R_390_16, 0, 1, 16, false, 0, complain_overflow_bitfield, bfd_elf_generic_reloc, "R_390_16", false, 0,0x0000ffff, false), 102 HOWTO(R_390_32, 0, 2, 32, false, 0, complain_overflow_bitfield, bfd_elf_generic_reloc, "R_390_32", false, 0,0xffffffff, false), 103 HOWTO(R_390_PC32, 0, 2, 32, true, 0, complain_overflow_bitfield, bfd_elf_generic_reloc, "R_390_PC32", false, 0,0xffffffff, true), 104 HOWTO(R_390_GOT12, 0, 1, 12, false, 0, complain_overflow_dont, bfd_elf_generic_reloc, "R_390_GOT12", false, 0,0x00000fff, false), 105 HOWTO(R_390_GOT32, 0, 2, 32, false, 0, complain_overflow_bitfield, bfd_elf_generic_reloc, "R_390_GOT32", false, 0,0xffffffff, false), 106 HOWTO(R_390_PLT32, 0, 2, 32, true, 0, complain_overflow_bitfield, bfd_elf_generic_reloc, "R_390_PLT32", false, 0,0xffffffff, true), 107 HOWTO(R_390_COPY, 0, 4, 64, false, 0, complain_overflow_bitfield, bfd_elf_generic_reloc, "R_390_COPY", false, 0,MINUS_ONE, false), 108 HOWTO(R_390_GLOB_DAT, 0, 4, 64, false, 0, complain_overflow_bitfield, bfd_elf_generic_reloc, "R_390_GLOB_DAT",false, 0,MINUS_ONE, false), 109 HOWTO(R_390_JMP_SLOT, 0, 4, 64, false, 0, complain_overflow_bitfield, bfd_elf_generic_reloc, "R_390_JMP_SLOT",false, 0,MINUS_ONE, false), 110 HOWTO(R_390_RELATIVE, 0, 4, 64, true, 0, complain_overflow_bitfield, bfd_elf_generic_reloc, "R_390_RELATIVE",false, 0,MINUS_ONE, false), 111 HOWTO(R_390_GOTOFF, 0, 4, 64, false, 0, complain_overflow_bitfield, bfd_elf_generic_reloc, "R_390_GOTOFF", false, 0,MINUS_ONE, false), 112 HOWTO(R_390_GOTPC, 0, 4, 64, true, 0, complain_overflow_bitfield, bfd_elf_generic_reloc, "R_390_GOTPC", false, 0,MINUS_ONE, true), 113 HOWTO(R_390_GOT16, 0, 1, 16, false, 0, complain_overflow_bitfield, bfd_elf_generic_reloc, "R_390_GOT16", false, 0,0x0000ffff, false), 114 HOWTO(R_390_PC16, 0, 1, 16, true, 0, complain_overflow_bitfield, bfd_elf_generic_reloc, "R_390_PC16", false, 0,0x0000ffff, true), 115 HOWTO(R_390_PC16DBL, 1, 1, 16, true, 0, complain_overflow_bitfield, bfd_elf_generic_reloc, "R_390_PC16DBL", false, 0,0x0000ffff, true), 116 HOWTO(R_390_PLT16DBL, 1, 1, 16, true, 0, complain_overflow_bitfield, bfd_elf_generic_reloc, "R_390_PLT16DBL", false, 0,0x0000ffff, true), 117 HOWTO(R_390_PC32DBL, 1, 2, 32, true, 0, complain_overflow_bitfield, bfd_elf_generic_reloc, "R_390_PC32DBL", false, 0,0xffffffff, true), 118 HOWTO(R_390_PLT32DBL, 1, 2, 32, true, 0, complain_overflow_bitfield, bfd_elf_generic_reloc, "R_390_PLT32DBL", false, 0,0xffffffff, true), 119 HOWTO(R_390_GOTPCDBL, 1, 2, 32, true, 0, complain_overflow_bitfield, bfd_elf_generic_reloc, "R_390_GOTPCDBL", false, 0,MINUS_ONE, true), 120 HOWTO(R_390_64, 0, 4, 64, false, 0, complain_overflow_bitfield, bfd_elf_generic_reloc, "R_390_64", false, 0,MINUS_ONE, false), 121 HOWTO(R_390_PC64, 0, 4, 64, true, 0, complain_overflow_bitfield, bfd_elf_generic_reloc, "R_390_PC64", false, 0,MINUS_ONE, true), 122 HOWTO(R_390_GOT64, 0, 4, 64, false, 0, complain_overflow_bitfield, bfd_elf_generic_reloc, "R_390_GOT64", false, 0,MINUS_ONE, false), 123 HOWTO(R_390_PLT64, 0, 4, 64, true, 0, complain_overflow_bitfield, bfd_elf_generic_reloc, "R_390_PLT64", false, 0,MINUS_ONE, true), 124 HOWTO(R_390_GOTENT, 1, 2, 32, true, 0, complain_overflow_bitfield, bfd_elf_generic_reloc, "R_390_GOTENT", false, 0,MINUS_ONE, true), 125}; 126 127/* GNU extension to record C++ vtable hierarchy. */ 128static reloc_howto_type elf64_s390_vtinherit_howto = 129 HOWTO (R_390_GNU_VTINHERIT, 0,4,0,false,0,complain_overflow_dont, NULL, "R_390_GNU_VTINHERIT", false,0, 0, false); 130static reloc_howto_type elf64_s390_vtentry_howto = 131 HOWTO (R_390_GNU_VTENTRY, 0,4,0,false,0,complain_overflow_dont, _bfd_elf_rel_vtable_reloc_fn,"R_390_GNU_VTENTRY", false,0,0, false); 132 133static reloc_howto_type * 134elf_s390_reloc_type_lookup (abfd, code) 135 bfd *abfd ATTRIBUTE_UNUSED; 136 bfd_reloc_code_real_type code; 137{ 138 switch (code) 139 { 140 case BFD_RELOC_NONE: 141 return &elf_howto_table[(int) R_390_NONE]; 142 case BFD_RELOC_8: 143 return &elf_howto_table[(int) R_390_8]; 144 case BFD_RELOC_390_12: 145 return &elf_howto_table[(int) R_390_12]; 146 case BFD_RELOC_16: 147 return &elf_howto_table[(int) R_390_16]; 148 case BFD_RELOC_32: 149 return &elf_howto_table[(int) R_390_32]; 150 case BFD_RELOC_CTOR: 151 return &elf_howto_table[(int) R_390_32]; 152 case BFD_RELOC_32_PCREL: 153 return &elf_howto_table[(int) R_390_PC32]; 154 case BFD_RELOC_390_GOT12: 155 return &elf_howto_table[(int) R_390_GOT12]; 156 case BFD_RELOC_32_GOT_PCREL: 157 return &elf_howto_table[(int) R_390_GOT32]; 158 case BFD_RELOC_390_PLT32: 159 return &elf_howto_table[(int) R_390_PLT32]; 160 case BFD_RELOC_390_COPY: 161 return &elf_howto_table[(int) R_390_COPY]; 162 case BFD_RELOC_390_GLOB_DAT: 163 return &elf_howto_table[(int) R_390_GLOB_DAT]; 164 case BFD_RELOC_390_JMP_SLOT: 165 return &elf_howto_table[(int) R_390_JMP_SLOT]; 166 case BFD_RELOC_390_RELATIVE: 167 return &elf_howto_table[(int) R_390_RELATIVE]; 168 case BFD_RELOC_32_GOTOFF: 169 return &elf_howto_table[(int) R_390_GOTOFF]; 170 case BFD_RELOC_390_GOTPC: 171 return &elf_howto_table[(int) R_390_GOTPC]; 172 case BFD_RELOC_390_GOT16: 173 return &elf_howto_table[(int) R_390_GOT16]; 174 case BFD_RELOC_16_PCREL: 175 return &elf_howto_table[(int) R_390_PC16]; 176 case BFD_RELOC_390_PC16DBL: 177 return &elf_howto_table[(int) R_390_PC16DBL]; 178 case BFD_RELOC_390_PLT16DBL: 179 return &elf_howto_table[(int) R_390_PLT16DBL]; 180 case BFD_RELOC_VTABLE_INHERIT: 181 return &elf64_s390_vtinherit_howto; 182 case BFD_RELOC_VTABLE_ENTRY: 183 return &elf64_s390_vtentry_howto; 184 case BFD_RELOC_390_PC32DBL: 185 return &elf_howto_table[(int) R_390_PC32DBL]; 186 case BFD_RELOC_390_PLT32DBL: 187 return &elf_howto_table[(int) R_390_PLT32DBL]; 188 case BFD_RELOC_390_GOTPCDBL: 189 return &elf_howto_table[(int) R_390_GOTPCDBL]; 190 case BFD_RELOC_64: 191 return &elf_howto_table[(int) R_390_64]; 192 case BFD_RELOC_64_PCREL: 193 return &elf_howto_table[(int) R_390_PC64]; 194 case BFD_RELOC_390_GOT64: 195 return &elf_howto_table[(int) R_390_GOT64]; 196 case BFD_RELOC_390_PLT64: 197 return &elf_howto_table[(int) R_390_PLT64]; 198 case BFD_RELOC_390_GOTENT: 199 return &elf_howto_table[(int) R_390_GOTENT]; 200 default: 201 break; 202 } 203 return 0; 204} 205 206/* We need to use ELF64_R_TYPE so we have our own copy of this function, 207 and elf64-s390.c has its own copy. */ 208 209static void 210elf_s390_info_to_howto (abfd, cache_ptr, dst) 211 bfd *abfd ATTRIBUTE_UNUSED; 212 arelent *cache_ptr; 213 Elf_Internal_Rela *dst; 214{ 215 switch (ELF64_R_TYPE(dst->r_info)) 216 { 217 case R_390_GNU_VTINHERIT: 218 cache_ptr->howto = &elf64_s390_vtinherit_howto; 219 break; 220 221 case R_390_GNU_VTENTRY: 222 cache_ptr->howto = &elf64_s390_vtentry_howto; 223 break; 224 225 default: 226 BFD_ASSERT (ELF64_R_TYPE(dst->r_info) < (unsigned int) R_390_max); 227 cache_ptr->howto = &elf_howto_table[ELF64_R_TYPE(dst->r_info)]; 228 } 229} 230 231static boolean 232elf_s390_is_local_label_name (abfd, name) 233 bfd *abfd; 234 const char *name; 235{ 236 if (name[0] == '.' && (name[1] == 'X' || name[1] == 'L')) 237 return true; 238 239 return _bfd_elf_is_local_label_name (abfd, name); 240} 241 242/* Functions for the 390 ELF linker. */ 243 244/* The name of the dynamic interpreter. This is put in the .interp 245 section. */ 246 247#define ELF_DYNAMIC_INTERPRETER "/usr/lib/ld.so.1" 248 249/* The size in bytes of the first entry in the procedure linkage table. */ 250#define PLT_FIRST_ENTRY_SIZE 32 251/* The size in bytes of an entry in the procedure linkage table. */ 252#define PLT_ENTRY_SIZE 32 253 254#define GOT_ENTRY_SIZE 8 255 256/* The first three entries in a procedure linkage table are reserved, 257 and the initial contents are unimportant (we zero them out). 258 Subsequent entries look like this. See the SVR4 ABI 386 259 supplement to see how this works. */ 260 261/* For the s390, simple addr offset can only be 0 - 4096. 262 To use the full 16777216 TB address space, several instructions 263 are needed to load an address in a register and execute 264 a branch( or just saving the address) 265 266 Furthermore, only r 0 and 1 are free to use!!! */ 267 268/* The first 3 words in the GOT are then reserved. 269 Word 0 is the address of the dynamic table. 270 Word 1 is a pointer to a structure describing the object 271 Word 2 is used to point to the loader entry address. 272 273 The code for PLT entries looks like this: 274 275 The GOT holds the address in the PLT to be executed. 276 The loader then gets: 277 24(15) = Pointer to the structure describing the object. 278 28(15) = Offset in symbol table 279 The loader must then find the module where the function is 280 and insert the address in the GOT. 281 282 PLT1: LARL 1,<fn>@GOTENT # 6 bytes Load address of GOT entry in r1 283 LG 1,0(1) # 6 bytes Load address from GOT in r1 284 BCR 15,1 # 2 bytes Jump to address 285 RET1: BASR 1,0 # 2 bytes Return from GOT 1st time 286 LGF 1,12(1) # 6 bytes Load offset in symbl table in r1 287 BRCL 15,-x # 6 bytes Jump to start of PLT 288 .long ? # 4 bytes offset into symbol table 289 290 Total = 32 bytes per PLT entry 291 Fixup at offset 2: relative address to GOT entry 292 Fixup at offset 22: relative branch to PLT0 293 Fixup at offset 28: 32 bit offset into symbol table 294 295 A 32 bit offset into the symbol table is enough. It allows for symbol 296 tables up to a size of 2 gigabyte. A single dynamic object (the main 297 program, any shared library) is limited to 4GB in size and I want to see 298 the program that manages to have a symbol table of more than 2 GB with a 299 total size of at max 4 GB. */ 300 301#define PLT_ENTRY_WORD0 (bfd_vma) 0xc0100000 302#define PLT_ENTRY_WORD1 (bfd_vma) 0x0000e310 303#define PLT_ENTRY_WORD2 (bfd_vma) 0x10000004 304#define PLT_ENTRY_WORD3 (bfd_vma) 0x07f10d10 305#define PLT_ENTRY_WORD4 (bfd_vma) 0xe310100c 306#define PLT_ENTRY_WORD5 (bfd_vma) 0x0014c0f4 307#define PLT_ENTRY_WORD6 (bfd_vma) 0x00000000 308#define PLT_ENTRY_WORD7 (bfd_vma) 0x00000000 309 310/* The first PLT entry pushes the offset into the symbol table 311 from R1 onto the stack at 8(15) and the loader object info 312 at 12(15), loads the loader address in R1 and jumps to it. */ 313 314/* The first entry in the PLT: 315 316 PLT0: 317 STG 1,56(15) # r1 contains the offset into the symbol table 318 LARL 1,_GLOBAL_OFFSET_TABLE # load address of global offset table 319 MVC 48(8,15),8(1) # move loader ino (object struct address) to stack 320 LG 1,16(1) # get entry address of loader 321 BCR 15,1 # jump to loader 322 323 Fixup at offset 8: relative address to start of GOT. */ 324 325#define PLT_FIRST_ENTRY_WORD0 (bfd_vma) 0xe310f038 326#define PLT_FIRST_ENTRY_WORD1 (bfd_vma) 0x0024c010 327#define PLT_FIRST_ENTRY_WORD2 (bfd_vma) 0x00000000 328#define PLT_FIRST_ENTRY_WORD3 (bfd_vma) 0xd207f030 329#define PLT_FIRST_ENTRY_WORD4 (bfd_vma) 0x1008e310 330#define PLT_FIRST_ENTRY_WORD5 (bfd_vma) 0x10100004 331#define PLT_FIRST_ENTRY_WORD6 (bfd_vma) 0x07f10700 332#define PLT_FIRST_ENTRY_WORD7 (bfd_vma) 0x07000700 333 334/* The s390 linker needs to keep track of the number of relocs that it 335 decides to copy as dynamic relocs in check_relocs for each symbol. 336 This is so that it can later discard them if they are found to be 337 unnecessary. We store the information in a field extending the 338 regular ELF linker hash table. */ 339 340struct elf_s390_dyn_relocs 341{ 342 struct elf_s390_dyn_relocs *next; 343 344 /* The input section of the reloc. */ 345 asection *sec; 346 347 /* Total number of relocs copied for the input section. */ 348 bfd_size_type count; 349 350 /* Number of pc-relative relocs copied for the input section. */ 351 bfd_size_type pc_count; 352}; 353 354/* s390 ELF linker hash entry. */ 355 356struct elf_s390_link_hash_entry 357{ 358 struct elf_link_hash_entry elf; 359 360 /* Track dynamic relocs copied for this symbol. */ 361 struct elf_s390_dyn_relocs *dyn_relocs; 362}; 363 364/* s390 ELF linker hash table. */ 365 366struct elf_s390_link_hash_table 367{ 368 struct elf_link_hash_table elf; 369 370 /* Short-cuts to get to dynamic linker sections. */ 371 asection *sgot; 372 asection *sgotplt; 373 asection *srelgot; 374 asection *splt; 375 asection *srelplt; 376 asection *sdynbss; 377 asection *srelbss; 378 379 /* Small local sym to section mapping cache. */ 380 struct sym_sec_cache sym_sec; 381}; 382 383/* Get the s390 ELF linker hash table from a link_info structure. */ 384 385#define elf_s390_hash_table(p) \ 386 ((struct elf_s390_link_hash_table *) ((p)->hash)) 387 388/* Create an entry in an s390 ELF linker hash table. */ 389 390static struct bfd_hash_entry * 391link_hash_newfunc (entry, table, string) 392 struct bfd_hash_entry *entry; 393 struct bfd_hash_table *table; 394 const char *string; 395{ 396 /* Allocate the structure if it has not already been allocated by a 397 subclass. */ 398 if (entry == NULL) 399 { 400 entry = bfd_hash_allocate (table, 401 sizeof (struct elf_s390_link_hash_entry)); 402 if (entry == NULL) 403 return entry; 404 } 405 406 /* Call the allocation method of the superclass. */ 407 entry = _bfd_elf_link_hash_newfunc (entry, table, string); 408 if (entry != NULL) 409 { 410 struct elf_s390_link_hash_entry *eh; 411 412 eh = (struct elf_s390_link_hash_entry *) entry; 413 eh->dyn_relocs = NULL; 414 } 415 416 return entry; 417} 418 419/* Create an s390 ELF linker hash table. */ 420 421static struct bfd_link_hash_table * 422elf_s390_link_hash_table_create (abfd) 423 bfd *abfd; 424{ 425 struct elf_s390_link_hash_table *ret; 426 bfd_size_type amt = sizeof (struct elf_s390_link_hash_table); 427 428 ret = (struct elf_s390_link_hash_table *) bfd_malloc (amt); 429 if (ret == NULL) 430 return NULL; 431 432 if (! _bfd_elf_link_hash_table_init (&ret->elf, abfd, link_hash_newfunc)) 433 { 434 free (ret); 435 return NULL; 436 } 437 438 ret->sgot = NULL; 439 ret->sgotplt = NULL; 440 ret->srelgot = NULL; 441 ret->splt = NULL; 442 ret->srelplt = NULL; 443 ret->sdynbss = NULL; 444 ret->srelbss = NULL; 445 ret->sym_sec.abfd = NULL; 446 447 return &ret->elf.root; 448} 449 450/* Create .got, .gotplt, and .rela.got sections in DYNOBJ, and set up 451 shortcuts to them in our hash table. */ 452 453static boolean 454create_got_section (dynobj, info) 455 bfd *dynobj; 456 struct bfd_link_info *info; 457{ 458 struct elf_s390_link_hash_table *htab; 459 460 if (! _bfd_elf_create_got_section (dynobj, info)) 461 return false; 462 463 htab = elf_s390_hash_table (info); 464 htab->sgot = bfd_get_section_by_name (dynobj, ".got"); 465 htab->sgotplt = bfd_get_section_by_name (dynobj, ".got.plt"); 466 if (!htab->sgot || !htab->sgotplt) 467 abort (); 468 469 htab->srelgot = bfd_make_section (dynobj, ".rela.got"); 470 if (htab->srelgot == NULL 471 || ! bfd_set_section_flags (dynobj, htab->srelgot, 472 (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS 473 | SEC_IN_MEMORY | SEC_LINKER_CREATED 474 | SEC_READONLY)) 475 || ! bfd_set_section_alignment (dynobj, htab->srelgot, 3)) 476 return false; 477 return true; 478} 479 480/* Create .plt, .rela.plt, .got, .got.plt, .rela.got, .dynbss, and 481 .rela.bss sections in DYNOBJ, and set up shortcuts to them in our 482 hash table. */ 483 484static boolean 485elf_s390_create_dynamic_sections (dynobj, info) 486 bfd *dynobj; 487 struct bfd_link_info *info; 488{ 489 struct elf_s390_link_hash_table *htab; 490 491 htab = elf_s390_hash_table (info); 492 if (!htab->sgot && !create_got_section (dynobj, info)) 493 return false; 494 495 if (!_bfd_elf_create_dynamic_sections (dynobj, info)) 496 return false; 497 498 htab->splt = bfd_get_section_by_name (dynobj, ".plt"); 499 htab->srelplt = bfd_get_section_by_name (dynobj, ".rela.plt"); 500 htab->sdynbss = bfd_get_section_by_name (dynobj, ".dynbss"); 501 if (!info->shared) 502 htab->srelbss = bfd_get_section_by_name (dynobj, ".rela.bss"); 503 504 if (!htab->splt || !htab->srelplt || !htab->sdynbss 505 || (!info->shared && !htab->srelbss)) 506 abort (); 507 508 return true; 509} 510 511/* Copy the extra info we tack onto an elf_link_hash_entry. */ 512 513static void 514elf_s390_copy_indirect_symbol (bed, dir, ind) 515 struct elf_backend_data *bed; 516 struct elf_link_hash_entry *dir, *ind; 517{ 518 struct elf_s390_link_hash_entry *edir, *eind; 519 520 edir = (struct elf_s390_link_hash_entry *) dir; 521 eind = (struct elf_s390_link_hash_entry *) ind; 522 523 if (eind->dyn_relocs != NULL) 524 { 525 if (edir->dyn_relocs != NULL) 526 { 527 struct elf_s390_dyn_relocs **pp; 528 struct elf_s390_dyn_relocs *p; 529 530 if (ind->root.type == bfd_link_hash_indirect) 531 abort (); 532 533 /* Add reloc counts against the weak sym to the strong sym 534 list. Merge any entries against the same section. */ 535 for (pp = &eind->dyn_relocs; (p = *pp) != NULL; ) 536 { 537 struct elf_s390_dyn_relocs *q; 538 539 for (q = edir->dyn_relocs; q != NULL; q = q->next) 540 if (q->sec == p->sec) 541 { 542 q->pc_count += p->pc_count; 543 q->count += p->count; 544 *pp = p->next; 545 break; 546 } 547 if (q == NULL) 548 pp = &p->next; 549 } 550 *pp = edir->dyn_relocs; 551 } 552 553 edir->dyn_relocs = eind->dyn_relocs; 554 eind->dyn_relocs = NULL; 555 } 556 557 _bfd_elf_link_hash_copy_indirect (bed, dir, ind); 558} 559 560/* Look through the relocs for a section during the first phase, and 561 allocate space in the global offset table or procedure linkage 562 table. */ 563 564static boolean 565elf_s390_check_relocs (abfd, info, sec, relocs) 566 bfd *abfd; 567 struct bfd_link_info *info; 568 asection *sec; 569 const Elf_Internal_Rela *relocs; 570{ 571 struct elf_s390_link_hash_table *htab; 572 Elf_Internal_Shdr *symtab_hdr; 573 struct elf_link_hash_entry **sym_hashes; 574 const Elf_Internal_Rela *rel; 575 const Elf_Internal_Rela *rel_end; 576 asection *sreloc; 577 578 if (info->relocateable) 579 return true; 580 581 htab = elf_s390_hash_table (info); 582 symtab_hdr = &elf_tdata (abfd)->symtab_hdr; 583 sym_hashes = elf_sym_hashes (abfd); 584 585 sreloc = NULL; 586 587 rel_end = relocs + sec->reloc_count; 588 for (rel = relocs; rel < rel_end; rel++) 589 { 590 unsigned long r_symndx; 591 struct elf_link_hash_entry *h; 592 593 r_symndx = ELF64_R_SYM (rel->r_info); 594 595 if (r_symndx >= NUM_SHDR_ENTRIES (symtab_hdr)) 596 { 597 (*_bfd_error_handler) (_("%s: bad symbol index: %d"), 598 bfd_archive_filename (abfd), 599 r_symndx); 600 return false; 601 } 602 603 if (r_symndx < symtab_hdr->sh_info) 604 h = NULL; 605 else 606 h = sym_hashes[r_symndx - symtab_hdr->sh_info]; 607 608 switch (ELF64_R_TYPE (rel->r_info)) 609 { 610 case R_390_GOT12: 611 case R_390_GOT16: 612 case R_390_GOT32: 613 case R_390_GOT64: 614 case R_390_GOTENT: 615 /* This symbol requires a global offset table entry. */ 616 if (h != NULL) 617 { 618 h->got.refcount += 1; 619 } 620 else 621 { 622 bfd_signed_vma *local_got_refcounts; 623 624 /* This is a global offset table entry for a local symbol. */ 625 local_got_refcounts = elf_local_got_refcounts (abfd); 626 if (local_got_refcounts == NULL) 627 { 628 bfd_size_type size; 629 630 size = symtab_hdr->sh_info; 631 size *= sizeof (bfd_signed_vma); 632 local_got_refcounts = ((bfd_signed_vma *) 633 bfd_zalloc (abfd, size)); 634 if (local_got_refcounts == NULL) 635 return false; 636 elf_local_got_refcounts (abfd) = local_got_refcounts; 637 } 638 local_got_refcounts[r_symndx] += 1; 639 } 640 /* Fall through */ 641 642 case R_390_GOTOFF: 643 case R_390_GOTPC: 644 case R_390_GOTPCDBL: 645 if (htab->sgot == NULL) 646 { 647 if (htab->elf.dynobj == NULL) 648 htab->elf.dynobj = abfd; 649 if (!create_got_section (htab->elf.dynobj, info)) 650 return false; 651 } 652 break; 653 654 case R_390_PLT16DBL: 655 case R_390_PLT32: 656 case R_390_PLT32DBL: 657 case R_390_PLT64: 658 /* This symbol requires a procedure linkage table entry. We 659 actually build the entry in adjust_dynamic_symbol, 660 because this might be a case of linking PIC code which is 661 never referenced by a dynamic object, in which case we 662 don't need to generate a procedure linkage table entry 663 after all. */ 664 665 /* If this is a local symbol, we resolve it directly without 666 creating a procedure linkage table entry. */ 667 if (h == NULL) 668 continue; 669 670 h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_PLT; 671 h->plt.refcount += 1; 672 break; 673 674 case R_390_8: 675 case R_390_16: 676 case R_390_32: 677 case R_390_64: 678 case R_390_PC16: 679 case R_390_PC16DBL: 680 case R_390_PC32: 681 case R_390_PC32DBL: 682 case R_390_PC64: 683 if (h != NULL && !info->shared) 684 { 685 /* If this reloc is in a read-only section, we might 686 need a copy reloc. We can't check reliably at this 687 stage whether the section is read-only, as input 688 sections have not yet been mapped to output sections. 689 Tentatively set the flag for now, and correct in 690 adjust_dynamic_symbol. */ 691 h->elf_link_hash_flags |= ELF_LINK_NON_GOT_REF; 692 693 /* We may need a .plt entry if the function this reloc 694 refers to is in a shared lib. */ 695 h->plt.refcount += 1; 696 } 697 698 /* If we are creating a shared library, and this is a reloc 699 against a global symbol, or a non PC relative reloc 700 against a local symbol, then we need to copy the reloc 701 into the shared library. However, if we are linking with 702 -Bsymbolic, we do not need to copy a reloc against a 703 global symbol which is defined in an object we are 704 including in the link (i.e., DEF_REGULAR is set). At 705 this point we have not seen all the input files, so it is 706 possible that DEF_REGULAR is not set now but will be set 707 later (it is never cleared). In case of a weak definition, 708 DEF_REGULAR may be cleared later by a strong definition in 709 a shared library. We account for that possibility below by 710 storing information in the relocs_copied field of the hash 711 table entry. A similar situation occurs when creating 712 shared libraries and symbol visibility changes render the 713 symbol local. 714 715 If on the other hand, we are creating an executable, we 716 may need to keep relocations for symbols satisfied by a 717 dynamic library if we manage to avoid copy relocs for the 718 symbol. */ 719 if ((info->shared 720 && (sec->flags & SEC_ALLOC) != 0 721 && ((ELF64_R_TYPE (rel->r_info) != R_390_PC16 722 && ELF64_R_TYPE (rel->r_info) != R_390_PC16DBL 723 && ELF64_R_TYPE (rel->r_info) != R_390_PC32 724 && ELF64_R_TYPE (rel->r_info) != R_390_PC32DBL 725 && ELF64_R_TYPE (rel->r_info) != R_390_PC64) 726 || (h != NULL 727 && (! info->symbolic 728 || h->root.type == bfd_link_hash_defweak 729 || (h->elf_link_hash_flags 730 & ELF_LINK_HASH_DEF_REGULAR) == 0)))) 731 || (!info->shared 732 && (sec->flags & SEC_ALLOC) != 0 733 && h != NULL 734 && (h->root.type == bfd_link_hash_defweak 735 || (h->elf_link_hash_flags 736 & ELF_LINK_HASH_DEF_REGULAR) == 0))) 737 { 738 struct elf_s390_dyn_relocs *p; 739 struct elf_s390_dyn_relocs **head; 740 741 /* We must copy these reloc types into the output file. 742 Create a reloc section in dynobj and make room for 743 this reloc. */ 744 if (sreloc == NULL) 745 { 746 const char *name; 747 bfd *dynobj; 748 749 name = (bfd_elf_string_from_elf_section 750 (abfd, 751 elf_elfheader (abfd)->e_shstrndx, 752 elf_section_data (sec)->rel_hdr.sh_name)); 753 if (name == NULL) 754 return false; 755 756 if (strncmp (name, ".rela", 5) != 0 757 || strcmp (bfd_get_section_name (abfd, sec), 758 name + 5) != 0) 759 { 760 (*_bfd_error_handler) 761 (_("%s: bad relocation section name `%s\'"), 762 bfd_archive_filename (abfd), name); 763 } 764 765 if (htab->elf.dynobj == NULL) 766 htab->elf.dynobj = abfd; 767 768 dynobj = htab->elf.dynobj; 769 sreloc = bfd_get_section_by_name (dynobj, name); 770 if (sreloc == NULL) 771 { 772 flagword flags; 773 774 sreloc = bfd_make_section (dynobj, name); 775 flags = (SEC_HAS_CONTENTS | SEC_READONLY 776 | SEC_IN_MEMORY | SEC_LINKER_CREATED); 777 if ((sec->flags & SEC_ALLOC) != 0) 778 flags |= SEC_ALLOC | SEC_LOAD; 779 if (sreloc == NULL 780 || ! bfd_set_section_flags (dynobj, sreloc, flags) 781 || ! bfd_set_section_alignment (dynobj, sreloc, 3)) 782 return false; 783 } 784 elf_section_data (sec)->sreloc = sreloc; 785 } 786 787 /* If this is a global symbol, we count the number of 788 relocations we need for this symbol. */ 789 if (h != NULL) 790 { 791 head = &((struct elf_s390_link_hash_entry *) h)->dyn_relocs; 792 } 793 else 794 { 795 /* Track dynamic relocs needed for local syms too. 796 We really need local syms available to do this 797 easily. Oh well. */ 798 799 asection *s; 800 s = bfd_section_from_r_symndx (abfd, &htab->sym_sec, 801 sec, r_symndx); 802 if (s == NULL) 803 return false; 804 805 head = ((struct elf_s390_dyn_relocs **) 806 &elf_section_data (s)->local_dynrel); 807 } 808 809 p = *head; 810 if (p == NULL || p->sec != sec) 811 { 812 bfd_size_type amt = sizeof *p; 813 p = ((struct elf_s390_dyn_relocs *) 814 bfd_alloc (htab->elf.dynobj, amt)); 815 if (p == NULL) 816 return false; 817 p->next = *head; 818 *head = p; 819 p->sec = sec; 820 p->count = 0; 821 p->pc_count = 0; 822 } 823 824 p->count += 1; 825 if (ELF64_R_TYPE (rel->r_info) == R_390_PC16 826 || ELF64_R_TYPE (rel->r_info) == R_390_PC16DBL 827 || ELF64_R_TYPE (rel->r_info) == R_390_PC32 828 || ELF64_R_TYPE (rel->r_info) == R_390_PC32DBL 829 || ELF64_R_TYPE (rel->r_info) == R_390_PC64) 830 p->pc_count += 1; 831 } 832 break; 833 834 /* This relocation describes the C++ object vtable hierarchy. 835 Reconstruct it for later use during GC. */ 836 case R_390_GNU_VTINHERIT: 837 if (!_bfd_elf64_gc_record_vtinherit (abfd, sec, h, rel->r_offset)) 838 return false; 839 break; 840 841 /* This relocation describes which C++ vtable entries are actually 842 used. Record for later use during GC. */ 843 case R_390_GNU_VTENTRY: 844 if (!_bfd_elf64_gc_record_vtentry (abfd, sec, h, rel->r_addend)) 845 return false; 846 break; 847 848 default: 849 break; 850 } 851 } 852 853 return true; 854} 855 856/* Return the section that should be marked against GC for a given 857 relocation. */ 858 859static asection * 860elf_s390_gc_mark_hook (sec, info, rel, h, sym) 861 asection *sec; 862 struct bfd_link_info *info ATTRIBUTE_UNUSED; 863 Elf_Internal_Rela *rel; 864 struct elf_link_hash_entry *h; 865 Elf_Internal_Sym *sym; 866{ 867 if (h != NULL) 868 { 869 switch (ELF64_R_TYPE (rel->r_info)) 870 { 871 case R_390_GNU_VTINHERIT: 872 case R_390_GNU_VTENTRY: 873 break; 874 875 default: 876 switch (h->root.type) 877 { 878 case bfd_link_hash_defined: 879 case bfd_link_hash_defweak: 880 return h->root.u.def.section; 881 882 case bfd_link_hash_common: 883 return h->root.u.c.p->section; 884 885 default: 886 break; 887 } 888 } 889 } 890 else 891 return bfd_section_from_elf_index (sec->owner, sym->st_shndx); 892 893 return NULL; 894} 895 896/* Update the got entry reference counts for the section being removed. */ 897 898static boolean 899elf_s390_gc_sweep_hook (abfd, info, sec, relocs) 900 bfd *abfd; 901 struct bfd_link_info *info; 902 asection *sec; 903 const Elf_Internal_Rela *relocs; 904{ 905 Elf_Internal_Shdr *symtab_hdr; 906 struct elf_link_hash_entry **sym_hashes; 907 bfd_signed_vma *local_got_refcounts; 908 const Elf_Internal_Rela *rel, *relend; 909 unsigned long r_symndx; 910 struct elf_link_hash_entry *h; 911 912 elf_section_data (sec)->local_dynrel = NULL; 913 914 symtab_hdr = &elf_tdata (abfd)->symtab_hdr; 915 sym_hashes = elf_sym_hashes (abfd); 916 local_got_refcounts = elf_local_got_refcounts (abfd); 917 918 relend = relocs + sec->reloc_count; 919 for (rel = relocs; rel < relend; rel++) 920 switch (ELF64_R_TYPE (rel->r_info)) 921 { 922 case R_390_GOT12: 923 case R_390_GOT16: 924 case R_390_GOT32: 925 case R_390_GOT64: 926 case R_390_GOTOFF: 927 case R_390_GOTPC: 928 case R_390_GOTPCDBL: 929 case R_390_GOTENT: 930 r_symndx = ELF64_R_SYM (rel->r_info); 931 if (r_symndx >= symtab_hdr->sh_info) 932 { 933 h = sym_hashes[r_symndx - symtab_hdr->sh_info]; 934 if (h->got.refcount > 0) 935 h->got.refcount -= 1; 936 } 937 else if (local_got_refcounts != NULL) 938 { 939 if (local_got_refcounts[r_symndx] > 0) 940 local_got_refcounts[r_symndx] -= 1; 941 } 942 break; 943 944 case R_390_8: 945 case R_390_12: 946 case R_390_16: 947 case R_390_32: 948 case R_390_64: 949 case R_390_PC16: 950 case R_390_PC16DBL: 951 case R_390_PC32: 952 case R_390_PC32DBL: 953 case R_390_PC64: 954 r_symndx = ELF64_R_SYM (rel->r_info); 955 if (r_symndx >= symtab_hdr->sh_info) 956 { 957 struct elf_s390_link_hash_entry *eh; 958 struct elf_s390_dyn_relocs **pp; 959 struct elf_s390_dyn_relocs *p; 960 961 h = sym_hashes[r_symndx - symtab_hdr->sh_info]; 962 963 if (!info->shared && h->plt.refcount > 0) 964 h->plt.refcount -= 1; 965 966 eh = (struct elf_s390_link_hash_entry *) h; 967 968 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; pp = &p->next) 969 if (p->sec == sec) 970 { 971 if (ELF64_R_TYPE (rel->r_info) == R_390_PC16 972 || ELF64_R_TYPE (rel->r_info) == R_390_PC16DBL 973 || ELF64_R_TYPE (rel->r_info) == R_390_PC32) 974 p->pc_count -= 1; 975 p->count -= 1; 976 if (p->count == 0) 977 *pp = p->next; 978 break; 979 } 980 } 981 break; 982 983 case R_390_PLT16DBL: 984 case R_390_PLT32: 985 case R_390_PLT32DBL: 986 case R_390_PLT64: 987 r_symndx = ELF64_R_SYM (rel->r_info); 988 if (r_symndx >= symtab_hdr->sh_info) 989 { 990 h = sym_hashes[r_symndx - symtab_hdr->sh_info]; 991 if (h->plt.refcount > 0) 992 h->plt.refcount -= 1; 993 } 994 break; 995 996 default: 997 break; 998 } 999 1000 return true; 1001} 1002 1003/* Adjust a symbol defined by a dynamic object and referenced by a 1004 regular object. The current definition is in some section of the 1005 dynamic object, but we're not including those sections. We have to 1006 change the definition to something the rest of the link can 1007 understand. */ 1008 1009static boolean 1010elf_s390_adjust_dynamic_symbol (info, h) 1011 struct bfd_link_info *info; 1012 struct elf_link_hash_entry *h; 1013{ 1014 struct elf_s390_link_hash_table *htab; 1015 struct elf_s390_link_hash_entry * eh; 1016 struct elf_s390_dyn_relocs *p; 1017 asection *s; 1018 unsigned int power_of_two; 1019 1020 /* If this is a function, put it in the procedure linkage table. We 1021 will fill in the contents of the procedure linkage table later 1022 (although we could actually do it here). */ 1023 if (h->type == STT_FUNC 1024 || (h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0) 1025 { 1026 if (h->plt.refcount <= 0 1027 || (! info->shared 1028 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) == 0 1029 && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) == 0 1030 && h->root.type != bfd_link_hash_undefweak 1031 && h->root.type != bfd_link_hash_undefined)) 1032 { 1033 /* This case can occur if we saw a PLT32 reloc in an input 1034 file, but the symbol was never referred to by a dynamic 1035 object, or if all references were garbage collected. In 1036 such a case, we don't actually need to build a procedure 1037 linkage table, and we can just do a PC32 reloc instead. */ 1038 h->plt.offset = (bfd_vma) -1; 1039 h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT; 1040 } 1041 1042 return true; 1043 } 1044 else 1045 /* It's possible that we incorrectly decided a .plt reloc was 1046 needed for an R_390_PC32 reloc to a non-function sym in 1047 check_relocs. We can't decide accurately between function and 1048 non-function syms in check-relocs; Objects loaded later in 1049 the link may change h->type. So fix it now. */ 1050 h->plt.offset = (bfd_vma) -1; 1051 1052 /* If this is a weak symbol, and there is a real definition, the 1053 processor independent code will have arranged for us to see the 1054 real definition first, and we can just use the same value. */ 1055 if (h->weakdef != NULL) 1056 { 1057 BFD_ASSERT (h->weakdef->root.type == bfd_link_hash_defined 1058 || h->weakdef->root.type == bfd_link_hash_defweak); 1059 h->root.u.def.section = h->weakdef->root.u.def.section; 1060 h->root.u.def.value = h->weakdef->root.u.def.value; 1061 return true; 1062 } 1063 1064 /* This is a reference to a symbol defined by a dynamic object which 1065 is not a function. */ 1066 1067 /* If we are creating a shared library, we must presume that the 1068 only references to the symbol are via the global offset table. 1069 For such cases we need not do anything here; the relocations will 1070 be handled correctly by relocate_section. */ 1071 if (info->shared) 1072 return true; 1073 1074 /* If there are no references to this symbol that do not use the 1075 GOT, we don't need to generate a copy reloc. */ 1076 if ((h->elf_link_hash_flags & ELF_LINK_NON_GOT_REF) == 0) 1077 return true; 1078 1079 /* If -z nocopyreloc was given, we won't generate them either. */ 1080 if (info->nocopyreloc) 1081 { 1082 h->elf_link_hash_flags &= ~ELF_LINK_NON_GOT_REF; 1083 return true; 1084 } 1085 1086 eh = (struct elf_s390_link_hash_entry *) h; 1087 for (p = eh->dyn_relocs; p != NULL; p = p->next) 1088 { 1089 s = p->sec->output_section; 1090 if (s != NULL && (s->flags & SEC_READONLY) != 0) 1091 break; 1092 } 1093 1094 /* If we didn't find any dynamic relocs in read-only sections, then 1095 we'll be keeping the dynamic relocs and avoiding the copy reloc. */ 1096 if (p == NULL) 1097 { 1098 h->elf_link_hash_flags &= ~ELF_LINK_NON_GOT_REF; 1099 return true; 1100 } 1101 1102 /* We must allocate the symbol in our .dynbss section, which will 1103 become part of the .bss section of the executable. There will be 1104 an entry for this symbol in the .dynsym section. The dynamic 1105 object will contain position independent code, so all references 1106 from the dynamic object to this symbol will go through the global 1107 offset table. The dynamic linker will use the .dynsym entry to 1108 determine the address it must put in the global offset table, so 1109 both the dynamic object and the regular object will refer to the 1110 same memory location for the variable. */ 1111 1112 htab = elf_s390_hash_table (info); 1113 1114 /* We must generate a R_390_COPY reloc to tell the dynamic linker to 1115 copy the initial value out of the dynamic object and into the 1116 runtime process image. */ 1117 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0) 1118 { 1119 htab->srelbss->_raw_size += sizeof (Elf64_External_Rela); 1120 h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_COPY; 1121 } 1122 1123 /* We need to figure out the alignment required for this symbol. I 1124 have no idea how ELF linkers handle this. */ 1125 power_of_two = bfd_log2 (h->size); 1126 if (power_of_two > 3) 1127 power_of_two = 3; 1128 1129 /* Apply the required alignment. */ 1130 s = htab->sdynbss; 1131 s->_raw_size = BFD_ALIGN (s->_raw_size, (bfd_size_type) (1 << power_of_two)); 1132 if (power_of_two > bfd_get_section_alignment (htab->elf.dynobj, s)) 1133 { 1134 if (! bfd_set_section_alignment (htab->elf.dynobj, s, power_of_two)) 1135 return false; 1136 } 1137 1138 /* Define the symbol as being at this point in the section. */ 1139 h->root.u.def.section = s; 1140 h->root.u.def.value = s->_raw_size; 1141 1142 /* Increment the section size to make room for the symbol. */ 1143 s->_raw_size += h->size; 1144 1145 return true; 1146} 1147 1148/* This is the condition under which elf_s390_finish_dynamic_symbol 1149 will be called from elflink.h. If elflink.h doesn't call our 1150 finish_dynamic_symbol routine, we'll need to do something about 1151 initializing any .plt and .got entries in elf_s390_relocate_section. */ 1152#define WILL_CALL_FINISH_DYNAMIC_SYMBOL(DYN, INFO, H) \ 1153 ((DYN) \ 1154 && ((INFO)->shared \ 1155 || ((H)->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0) \ 1156 && ((H)->dynindx != -1 \ 1157 || ((H)->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0)) 1158 1159/* Allocate space in .plt, .got and associated reloc sections for 1160 dynamic relocs. */ 1161 1162static boolean 1163allocate_dynrelocs (h, inf) 1164 struct elf_link_hash_entry *h; 1165 PTR inf; 1166{ 1167 struct bfd_link_info *info; 1168 struct elf_s390_link_hash_table *htab; 1169 struct elf_s390_link_hash_entry *eh; 1170 struct elf_s390_dyn_relocs *p; 1171 1172 if (h->root.type == bfd_link_hash_indirect) 1173 return true; 1174 1175 if (h->root.type == bfd_link_hash_warning) 1176 h = (struct elf_link_hash_entry *) h->root.u.i.link; 1177 1178 info = (struct bfd_link_info *) inf; 1179 htab = elf_s390_hash_table (info); 1180 1181 if (htab->elf.dynamic_sections_created 1182 && h->plt.refcount > 0) 1183 { 1184 /* Make sure this symbol is output as a dynamic symbol. 1185 Undefined weak syms won't yet be marked as dynamic. */ 1186 if (h->dynindx == -1 1187 && (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0) 1188 { 1189 if (! bfd_elf64_link_record_dynamic_symbol (info, h)) 1190 return false; 1191 } 1192 1193 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, info, h)) 1194 { 1195 asection *s = htab->splt; 1196 1197 /* If this is the first .plt entry, make room for the special 1198 first entry. */ 1199 if (s->_raw_size == 0) 1200 s->_raw_size += PLT_FIRST_ENTRY_SIZE; 1201 1202 h->plt.offset = s->_raw_size; 1203 1204 /* If this symbol is not defined in a regular file, and we are 1205 not generating a shared library, then set the symbol to this 1206 location in the .plt. This is required to make function 1207 pointers compare as equal between the normal executable and 1208 the shared library. */ 1209 if (! info->shared 1210 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0) 1211 { 1212 h->root.u.def.section = s; 1213 h->root.u.def.value = h->plt.offset; 1214 } 1215 1216 /* Make room for this entry. */ 1217 s->_raw_size += PLT_ENTRY_SIZE; 1218 1219 /* We also need to make an entry in the .got.plt section, which 1220 will be placed in the .got section by the linker script. */ 1221 htab->sgotplt->_raw_size += GOT_ENTRY_SIZE; 1222 1223 /* We also need to make an entry in the .rela.plt section. */ 1224 htab->srelplt->_raw_size += sizeof (Elf64_External_Rela); 1225 } 1226 else 1227 { 1228 h->plt.offset = (bfd_vma) -1; 1229 h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT; 1230 } 1231 } 1232 else 1233 { 1234 h->plt.offset = (bfd_vma) -1; 1235 h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT; 1236 } 1237 1238 if (h->got.refcount > 0) 1239 { 1240 asection *s; 1241 boolean dyn; 1242 1243 /* Make sure this symbol is output as a dynamic symbol. 1244 Undefined weak syms won't yet be marked as dynamic. */ 1245 if (h->dynindx == -1 1246 && (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0) 1247 { 1248 if (! bfd_elf64_link_record_dynamic_symbol (info, h)) 1249 return false; 1250 } 1251 1252 s = htab->sgot; 1253 h->got.offset = s->_raw_size; 1254 s->_raw_size += GOT_ENTRY_SIZE; 1255 dyn = htab->elf.dynamic_sections_created; 1256 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info, h)) 1257 htab->srelgot->_raw_size += sizeof (Elf64_External_Rela); 1258 } 1259 else 1260 h->got.offset = (bfd_vma) -1; 1261 1262 eh = (struct elf_s390_link_hash_entry *) h; 1263 if (eh->dyn_relocs == NULL) 1264 return true; 1265 1266 /* In the shared -Bsymbolic case, discard space allocated for 1267 dynamic pc-relative relocs against symbols which turn out to be 1268 defined in regular objects. For the normal shared case, discard 1269 space for pc-relative relocs that have become local due to symbol 1270 visibility changes. */ 1271 1272 if (info->shared) 1273 { 1274 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0 1275 && ((h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0 1276 || info->symbolic)) 1277 { 1278 struct elf_s390_dyn_relocs **pp; 1279 1280 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; ) 1281 { 1282 p->count -= p->pc_count; 1283 p->pc_count = 0; 1284 if (p->count == 0) 1285 *pp = p->next; 1286 else 1287 pp = &p->next; 1288 } 1289 } 1290 } 1291 else 1292 { 1293 /* For the non-shared case, discard space for relocs against 1294 symbols which turn out to need copy relocs or are not 1295 dynamic. */ 1296 1297 if ((h->elf_link_hash_flags & ELF_LINK_NON_GOT_REF) == 0 1298 && (((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0 1299 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0) 1300 || (htab->elf.dynamic_sections_created 1301 && (h->root.type == bfd_link_hash_undefweak 1302 || h->root.type == bfd_link_hash_undefined)))) 1303 { 1304 /* Make sure this symbol is output as a dynamic symbol. 1305 Undefined weak syms won't yet be marked as dynamic. */ 1306 if (h->dynindx == -1 1307 && (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0) 1308 { 1309 if (! bfd_elf64_link_record_dynamic_symbol (info, h)) 1310 return false; 1311 } 1312 1313 /* If that succeeded, we know we'll be keeping all the 1314 relocs. */ 1315 if (h->dynindx != -1) 1316 goto keep; 1317 } 1318 1319 eh->dyn_relocs = NULL; 1320 1321 keep: ; 1322 } 1323 1324 /* Finally, allocate space. */ 1325 for (p = eh->dyn_relocs; p != NULL; p = p->next) 1326 { 1327 asection *sreloc = elf_section_data (p->sec)->sreloc; 1328 sreloc->_raw_size += p->count * sizeof (Elf64_External_Rela); 1329 } 1330 1331 return true; 1332} 1333 1334/* Find any dynamic relocs that apply to read-only sections. */ 1335 1336static boolean 1337readonly_dynrelocs (h, inf) 1338 struct elf_link_hash_entry *h; 1339 PTR inf; 1340{ 1341 struct elf_s390_link_hash_entry *eh; 1342 struct elf_s390_dyn_relocs *p; 1343 1344 if (h->root.type == bfd_link_hash_warning) 1345 h = (struct elf_link_hash_entry *) h->root.u.i.link; 1346 1347 eh = (struct elf_s390_link_hash_entry *) h; 1348 for (p = eh->dyn_relocs; p != NULL; p = p->next) 1349 { 1350 asection *s = p->sec->output_section; 1351 1352 if (s != NULL && (s->flags & SEC_READONLY) != 0) 1353 { 1354 struct bfd_link_info *info = (struct bfd_link_info *) inf; 1355 1356 info->flags |= DF_TEXTREL; 1357 1358 /* Not an error, just cut short the traversal. */ 1359 return false; 1360 } 1361 } 1362 return true; 1363} 1364 1365/* Set the sizes of the dynamic sections. */ 1366 1367static boolean 1368elf_s390_size_dynamic_sections (output_bfd, info) 1369 bfd *output_bfd ATTRIBUTE_UNUSED; 1370 struct bfd_link_info *info; 1371{ 1372 struct elf_s390_link_hash_table *htab; 1373 bfd *dynobj; 1374 asection *s; 1375 boolean relocs; 1376 bfd *ibfd; 1377 1378 htab = elf_s390_hash_table (info); 1379 dynobj = htab->elf.dynobj; 1380 if (dynobj == NULL) 1381 abort (); 1382 1383 if (htab->elf.dynamic_sections_created) 1384 { 1385 /* Set the contents of the .interp section to the interpreter. */ 1386 if (! info->shared) 1387 { 1388 s = bfd_get_section_by_name (dynobj, ".interp"); 1389 if (s == NULL) 1390 abort (); 1391 s->_raw_size = sizeof ELF_DYNAMIC_INTERPRETER; 1392 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER; 1393 } 1394 } 1395 1396 /* Set up .got offsets for local syms, and space for local dynamic 1397 relocs. */ 1398 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next) 1399 { 1400 bfd_signed_vma *local_got; 1401 bfd_signed_vma *end_local_got; 1402 bfd_size_type locsymcount; 1403 Elf_Internal_Shdr *symtab_hdr; 1404 asection *srela; 1405 1406 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour) 1407 continue; 1408 1409 for (s = ibfd->sections; s != NULL; s = s->next) 1410 { 1411 struct elf_s390_dyn_relocs *p; 1412 1413 for (p = *((struct elf_s390_dyn_relocs **) 1414 &elf_section_data (s)->local_dynrel); 1415 p != NULL; 1416 p = p->next) 1417 { 1418 if (!bfd_is_abs_section (p->sec) 1419 && bfd_is_abs_section (p->sec->output_section)) 1420 { 1421 /* Input section has been discarded, either because 1422 it is a copy of a linkonce section or due to 1423 linker script /DISCARD/, so we'll be discarding 1424 the relocs too. */ 1425 } 1426 else if (p->count != 0) 1427 { 1428 srela = elf_section_data (p->sec)->sreloc; 1429 srela->_raw_size += p->count * sizeof (Elf64_External_Rela); 1430 if ((p->sec->output_section->flags & SEC_READONLY) != 0) 1431 info->flags |= DF_TEXTREL; 1432 } 1433 } 1434 } 1435 1436 local_got = elf_local_got_refcounts (ibfd); 1437 if (!local_got) 1438 continue; 1439 1440 symtab_hdr = &elf_tdata (ibfd)->symtab_hdr; 1441 locsymcount = symtab_hdr->sh_info; 1442 end_local_got = local_got + locsymcount; 1443 s = htab->sgot; 1444 srela = htab->srelgot; 1445 for (; local_got < end_local_got; ++local_got) 1446 { 1447 if (*local_got > 0) 1448 { 1449 *local_got = s->_raw_size; 1450 s->_raw_size += GOT_ENTRY_SIZE; 1451 if (info->shared) 1452 srela->_raw_size += sizeof (Elf64_External_Rela); 1453 } 1454 else 1455 *local_got = (bfd_vma) -1; 1456 } 1457 } 1458 1459 /* Allocate global sym .plt and .got entries, and space for global 1460 sym dynamic relocs. */ 1461 elf_link_hash_traverse (&htab->elf, allocate_dynrelocs, (PTR) info); 1462 1463 /* We now have determined the sizes of the various dynamic sections. 1464 Allocate memory for them. */ 1465 relocs = false; 1466 for (s = dynobj->sections; s != NULL; s = s->next) 1467 { 1468 if ((s->flags & SEC_LINKER_CREATED) == 0) 1469 continue; 1470 1471 if (s == htab->splt 1472 || s == htab->sgot 1473 || s == htab->sgotplt) 1474 { 1475 /* Strip this section if we don't need it; see the 1476 comment below. */ 1477 } 1478 else if (strncmp (bfd_get_section_name (dynobj, s), ".rela", 5) == 0) 1479 { 1480 if (s->_raw_size != 0 && s != htab->srelplt) 1481 relocs = true; 1482 1483 /* We use the reloc_count field as a counter if we need 1484 to copy relocs into the output file. */ 1485 s->reloc_count = 0; 1486 } 1487 else 1488 { 1489 /* It's not one of our sections, so don't allocate space. */ 1490 continue; 1491 } 1492 1493 if (s->_raw_size == 0) 1494 { 1495 /* If we don't need this section, strip it from the 1496 output file. This is to handle .rela.bss and 1497 .rela.plt. We must create it in 1498 create_dynamic_sections, because it must be created 1499 before the linker maps input sections to output 1500 sections. The linker does that before 1501 adjust_dynamic_symbol is called, and it is that 1502 function which decides whether anything needs to go 1503 into these sections. */ 1504 1505 _bfd_strip_section_from_output (info, s); 1506 continue; 1507 } 1508 1509 /* Allocate memory for the section contents. We use bfd_zalloc 1510 here in case unused entries are not reclaimed before the 1511 section's contents are written out. This should not happen, 1512 but this way if it does, we get a R_390_NONE reloc instead 1513 of garbage. */ 1514 s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->_raw_size); 1515 if (s->contents == NULL) 1516 return false; 1517 } 1518 1519 if (htab->elf.dynamic_sections_created) 1520 { 1521 /* Add some entries to the .dynamic section. We fill in the 1522 values later, in elf_s390_finish_dynamic_sections, but we 1523 must add the entries now so that we get the correct size for 1524 the .dynamic section. The DT_DEBUG entry is filled in by the 1525 dynamic linker and used by the debugger. */ 1526#define add_dynamic_entry(TAG, VAL) \ 1527 bfd_elf64_add_dynamic_entry (info, (bfd_vma) (TAG), (bfd_vma) (VAL)) 1528 1529 if (! info->shared) 1530 { 1531 if (!add_dynamic_entry (DT_DEBUG, 0)) 1532 return false; 1533 } 1534 1535 if (htab->splt->_raw_size != 0) 1536 { 1537 if (!add_dynamic_entry (DT_PLTGOT, 0) 1538 || !add_dynamic_entry (DT_PLTRELSZ, 0) 1539 || !add_dynamic_entry (DT_PLTREL, DT_RELA) 1540 || !add_dynamic_entry (DT_JMPREL, 0)) 1541 return false; 1542 } 1543 1544 if (relocs) 1545 { 1546 if (!add_dynamic_entry (DT_RELA, 0) 1547 || !add_dynamic_entry (DT_RELASZ, 0) 1548 || !add_dynamic_entry (DT_RELAENT, sizeof (Elf64_External_Rela))) 1549 return false; 1550 1551 /* If any dynamic relocs apply to a read-only section, 1552 then we need a DT_TEXTREL entry. */ 1553 if ((info->flags & DF_TEXTREL) == 0) 1554 elf_link_hash_traverse (&htab->elf, readonly_dynrelocs, 1555 (PTR) info); 1556 1557 if ((info->flags & DF_TEXTREL) != 0) 1558 { 1559 if (!add_dynamic_entry (DT_TEXTREL, 0)) 1560 return false; 1561 } 1562 } 1563 } 1564#undef add_dynamic_entry 1565 1566 return true; 1567} 1568 1569/* Relocate a 390 ELF section. */ 1570 1571static boolean 1572elf_s390_relocate_section (output_bfd, info, input_bfd, input_section, 1573 contents, relocs, local_syms, local_sections) 1574 bfd *output_bfd; 1575 struct bfd_link_info *info; 1576 bfd *input_bfd; 1577 asection *input_section; 1578 bfd_byte *contents; 1579 Elf_Internal_Rela *relocs; 1580 Elf_Internal_Sym *local_syms; 1581 asection **local_sections; 1582{ 1583 struct elf_s390_link_hash_table *htab; 1584 Elf_Internal_Shdr *symtab_hdr; 1585 struct elf_link_hash_entry **sym_hashes; 1586 bfd_vma *local_got_offsets; 1587 Elf_Internal_Rela *rel; 1588 Elf_Internal_Rela *relend; 1589 1590 if (info->relocateable) 1591 return true; 1592 1593 htab = elf_s390_hash_table (info); 1594 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr; 1595 sym_hashes = elf_sym_hashes (input_bfd); 1596 local_got_offsets = elf_local_got_offsets (input_bfd); 1597 1598 rel = relocs; 1599 relend = relocs + input_section->reloc_count; 1600 for (; rel < relend; rel++) 1601 { 1602 int r_type; 1603 reloc_howto_type *howto; 1604 unsigned long r_symndx; 1605 struct elf_link_hash_entry *h; 1606 Elf_Internal_Sym *sym; 1607 asection *sec; 1608 bfd_vma off; 1609 bfd_vma relocation; 1610 boolean unresolved_reloc; 1611 bfd_reloc_status_type r; 1612 1613 r_type = ELF64_R_TYPE (rel->r_info); 1614 if (r_type == (int) R_390_GNU_VTINHERIT 1615 || r_type == (int) R_390_GNU_VTENTRY) 1616 continue; 1617 if (r_type < 0 || r_type >= (int) R_390_max) 1618 { 1619 bfd_set_error (bfd_error_bad_value); 1620 return false; 1621 } 1622 1623 howto = elf_howto_table + r_type; 1624 r_symndx = ELF64_R_SYM (rel->r_info); 1625 h = NULL; 1626 sym = NULL; 1627 sec = NULL; 1628 unresolved_reloc = false; 1629 if (r_symndx < symtab_hdr->sh_info) 1630 { 1631 sym = local_syms + r_symndx; 1632 sec = local_sections[r_symndx]; 1633 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, sec, rel); 1634 } 1635 else 1636 { 1637 h = sym_hashes[r_symndx - symtab_hdr->sh_info]; 1638 while (h->root.type == bfd_link_hash_indirect 1639 || h->root.type == bfd_link_hash_warning) 1640 h = (struct elf_link_hash_entry *) h->root.u.i.link; 1641 1642 if (h->root.type == bfd_link_hash_defined 1643 || h->root.type == bfd_link_hash_defweak) 1644 { 1645 sec = h->root.u.def.section; 1646 if (sec->output_section == NULL) 1647 { 1648 /* Set a flag that will be cleared later if we find a 1649 relocation value for this symbol. output_section 1650 is typically NULL for symbols satisfied by a shared 1651 library. */ 1652 unresolved_reloc = true; 1653 relocation = 0; 1654 } 1655 else 1656 relocation = (h->root.u.def.value 1657 + sec->output_section->vma 1658 + sec->output_offset); 1659 } 1660 else if (h->root.type == bfd_link_hash_undefweak) 1661 relocation = 0; 1662 else if (info->shared 1663 && (!info->symbolic || info->allow_shlib_undefined) 1664 && !info->no_undefined 1665 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT) 1666 relocation = 0; 1667 else 1668 { 1669 if (! ((*info->callbacks->undefined_symbol) 1670 (info, h->root.root.string, input_bfd, 1671 input_section, rel->r_offset, 1672 (!info->shared || info->no_undefined 1673 || ELF_ST_VISIBILITY (h->other))))) 1674 return false; 1675 relocation = 0; 1676 } 1677 } 1678 1679 switch (r_type) 1680 { 1681 case R_390_GOT12: 1682 case R_390_GOT16: 1683 case R_390_GOT32: 1684 case R_390_GOT64: 1685 case R_390_GOTENT: 1686 /* Relocation is to the entry for this symbol in the global 1687 offset table. */ 1688 if (htab->sgot == NULL) 1689 abort (); 1690 1691 if (h != NULL) 1692 { 1693 boolean dyn; 1694 1695 off = h->got.offset; 1696 dyn = htab->elf.dynamic_sections_created; 1697 if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info, h) 1698 || (info->shared 1699 && (info->symbolic 1700 || h->dynindx == -1 1701 || (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL)) 1702 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR))) 1703 { 1704 /* This is actually a static link, or it is a 1705 -Bsymbolic link and the symbol is defined 1706 locally, or the symbol was forced to be local 1707 because of a version file. We must initialize 1708 this entry in the global offset table. Since the 1709 offset must always be a multiple of 2, we use the 1710 least significant bit to record whether we have 1711 initialized it already. 1712 1713 When doing a dynamic link, we create a .rel.got 1714 relocation entry to initialize the value. This 1715 is done in the finish_dynamic_symbol routine. */ 1716 if ((off & 1) != 0) 1717 off &= ~1; 1718 else 1719 { 1720 bfd_put_64 (output_bfd, relocation, 1721 htab->sgot->contents + off); 1722 h->got.offset |= 1; 1723 } 1724 } 1725 else 1726 unresolved_reloc = false; 1727 } 1728 else 1729 { 1730 if (local_got_offsets == NULL) 1731 abort (); 1732 1733 off = local_got_offsets[r_symndx]; 1734 1735 /* The offset must always be a multiple of 8. We use 1736 the least significant bit to record whether we have 1737 already generated the necessary reloc. */ 1738 if ((off & 1) != 0) 1739 off &= ~1; 1740 else 1741 { 1742 bfd_put_64 (output_bfd, relocation, 1743 htab->sgot->contents + off); 1744 1745 if (info->shared) 1746 { 1747 asection *srelgot; 1748 Elf_Internal_Rela outrel; 1749 Elf64_External_Rela *loc; 1750 1751 srelgot = htab->srelgot; 1752 if (srelgot == NULL) 1753 abort (); 1754 1755 outrel.r_offset = (htab->sgot->output_section->vma 1756 + htab->sgot->output_offset 1757 + off); 1758 outrel.r_info = ELF64_R_INFO (0, R_390_RELATIVE); 1759 outrel.r_addend = relocation; 1760 loc = (Elf64_External_Rela *) srelgot->contents; 1761 loc += srelgot->reloc_count++; 1762 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc); 1763 } 1764 1765 local_got_offsets[r_symndx] |= 1; 1766 } 1767 } 1768 1769 if (off >= (bfd_vma) -2) 1770 abort (); 1771 1772 relocation = htab->sgot->output_offset + off; 1773 1774 /* 1775 * For @GOTENT the relocation is against the offset between 1776 * the instruction and the symbols entry in the GOT and not 1777 * between the start of the GOT and the symbols entry. We 1778 * add the vma of the GOT to get the correct value. 1779 */ 1780 if (r_type == R_390_GOTENT) 1781 relocation += htab->sgot->output_section->vma; 1782 1783 break; 1784 1785 case R_390_GOTOFF: 1786 /* Relocation is relative to the start of the global offset 1787 table. */ 1788 1789 /* Note that sgot->output_offset is not involved in this 1790 calculation. We always want the start of .got. If we 1791 defined _GLOBAL_OFFSET_TABLE in a different way, as is 1792 permitted by the ABI, we might have to change this 1793 calculation. */ 1794 relocation -= htab->sgot->output_section->vma; 1795 1796 break; 1797 1798 case R_390_GOTPC: 1799 case R_390_GOTPCDBL: 1800 /* Use global offset table as symbol value. */ 1801 relocation = htab->sgot->output_section->vma; 1802 unresolved_reloc = false; 1803 break; 1804 1805 case R_390_PLT16DBL: 1806 case R_390_PLT32: 1807 case R_390_PLT32DBL: 1808 case R_390_PLT64: 1809 /* Relocation is to the entry for this symbol in the 1810 procedure linkage table. */ 1811 1812 /* Resolve a PLT32 reloc against a local symbol directly, 1813 without using the procedure linkage table. */ 1814 if (h == NULL) 1815 break; 1816 1817 if (h->plt.offset == (bfd_vma) -1 1818 || htab->splt == NULL) 1819 { 1820 /* We didn't make a PLT entry for this symbol. This 1821 happens when statically linking PIC code, or when 1822 using -Bsymbolic. */ 1823 break; 1824 } 1825 1826 relocation = (htab->splt->output_section->vma 1827 + htab->splt->output_offset 1828 + h->plt.offset); 1829 unresolved_reloc = false; 1830 break; 1831 1832 case R_390_8: 1833 case R_390_16: 1834 case R_390_32: 1835 case R_390_64: 1836 case R_390_PC16: 1837 case R_390_PC16DBL: 1838 case R_390_PC32: 1839 case R_390_PC32DBL: 1840 case R_390_PC64: 1841 /* r_symndx will be zero only for relocs against symbols 1842 from removed linkonce sections, or sections discarded by 1843 a linker script. */ 1844 if (r_symndx == 0 1845 || (input_section->flags & SEC_ALLOC) == 0) 1846 break; 1847 1848 if ((info->shared 1849 && ((r_type != R_390_PC16 1850 && r_type != R_390_PC16DBL 1851 && r_type != R_390_PC32 1852 && r_type != R_390_PC32DBL 1853 && r_type != R_390_PC64) 1854 || (h != NULL 1855 && h->dynindx != -1 1856 && (! info->symbolic 1857 || (h->elf_link_hash_flags 1858 & ELF_LINK_HASH_DEF_REGULAR) == 0)))) 1859 || (!info->shared 1860 && h != NULL 1861 && h->dynindx != -1 1862 && (h->elf_link_hash_flags & ELF_LINK_NON_GOT_REF) == 0 1863 && (((h->elf_link_hash_flags 1864 & ELF_LINK_HASH_DEF_DYNAMIC) != 0 1865 && (h->elf_link_hash_flags 1866 & ELF_LINK_HASH_DEF_REGULAR) == 0) 1867 || h->root.type == bfd_link_hash_undefweak 1868 || h->root.type == bfd_link_hash_undefined))) 1869 { 1870 Elf_Internal_Rela outrel; 1871 boolean skip, relocate; 1872 asection *sreloc; 1873 Elf64_External_Rela *loc; 1874 1875 /* When generating a shared object, these relocations 1876 are copied into the output file to be resolved at run 1877 time. */ 1878 1879 skip = false; 1880 relocate = false; 1881 1882 outrel.r_offset = 1883 _bfd_elf_section_offset (output_bfd, info, input_section, 1884 rel->r_offset); 1885 if (outrel.r_offset == (bfd_vma) -1) 1886 skip = true; 1887 else if (outrel.r_offset == (bfd_vma) -2) 1888 skip = true, relocate = true; 1889 1890 outrel.r_offset += (input_section->output_section->vma 1891 + input_section->output_offset); 1892 1893 if (skip) 1894 memset (&outrel, 0, sizeof outrel); 1895 else if (h != NULL 1896 && h->dynindx != -1 1897 && (r_type == R_390_PC16 1898 || r_type == R_390_PC16DBL 1899 || r_type == R_390_PC32 1900 || r_type == R_390_PC32DBL 1901 || r_type == R_390_PC64 1902 || !info->shared 1903 || !info->symbolic 1904 || (h->elf_link_hash_flags 1905 & ELF_LINK_HASH_DEF_REGULAR) == 0)) 1906 { 1907 outrel.r_info = ELF64_R_INFO (h->dynindx, r_type); 1908 outrel.r_addend = rel->r_addend; 1909 } 1910 else 1911 { 1912 /* This symbol is local, or marked to become local. */ 1913 relocate = true; 1914 outrel.r_info = ELF64_R_INFO (0, R_390_RELATIVE); 1915 outrel.r_addend = relocation + rel->r_addend; 1916 } 1917 1918 sreloc = elf_section_data (input_section)->sreloc; 1919 if (sreloc == NULL) 1920 abort (); 1921 1922 loc = (Elf64_External_Rela *) sreloc->contents; 1923 loc += sreloc->reloc_count++; 1924 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc); 1925 1926 /* If this reloc is against an external symbol, we do 1927 not want to fiddle with the addend. Otherwise, we 1928 need to include the symbol value so that it becomes 1929 an addend for the dynamic reloc. */ 1930 if (! relocate) 1931 continue; 1932 } 1933 1934 break; 1935 1936 default: 1937 break; 1938 } 1939 1940 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections 1941 because such sections are not SEC_ALLOC and thus ld.so will 1942 not process them. */ 1943 if (unresolved_reloc 1944 && !((input_section->flags & SEC_DEBUGGING) != 0 1945 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0)) 1946 (*_bfd_error_handler) 1947 (_("%s(%s+0x%lx): unresolvable relocation against symbol `%s'"), 1948 bfd_archive_filename (input_bfd), 1949 bfd_get_section_name (input_bfd, input_section), 1950 (long) rel->r_offset, 1951 h->root.root.string); 1952 1953 r = _bfd_final_link_relocate (howto, input_bfd, input_section, 1954 contents, rel->r_offset, 1955 relocation, rel->r_addend); 1956 1957 if (r != bfd_reloc_ok) 1958 { 1959 const char *name; 1960 1961 if (h != NULL) 1962 name = h->root.root.string; 1963 else 1964 { 1965 name = bfd_elf_string_from_elf_section (input_bfd, 1966 symtab_hdr->sh_link, 1967 sym->st_name); 1968 if (name == NULL) 1969 return false; 1970 if (*name == '\0') 1971 name = bfd_section_name (input_bfd, sec); 1972 } 1973 1974 if (r == bfd_reloc_overflow) 1975 { 1976 1977 if (! ((*info->callbacks->reloc_overflow) 1978 (info, name, howto->name, (bfd_vma) 0, 1979 input_bfd, input_section, rel->r_offset))) 1980 return false; 1981 } 1982 else 1983 { 1984 (*_bfd_error_handler) 1985 (_("%s(%s+0x%lx): reloc against `%s': error %d"), 1986 bfd_archive_filename (input_bfd), 1987 bfd_get_section_name (input_bfd, input_section), 1988 (long) rel->r_offset, name, (int) r); 1989 return false; 1990 } 1991 } 1992 } 1993 1994 return true; 1995} 1996 1997/* Finish up dynamic symbol handling. We set the contents of various 1998 dynamic sections here. */ 1999 2000static boolean 2001elf_s390_finish_dynamic_symbol (output_bfd, info, h, sym) 2002 bfd *output_bfd; 2003 struct bfd_link_info *info; 2004 struct elf_link_hash_entry *h; 2005 Elf_Internal_Sym *sym; 2006{ 2007 struct elf_s390_link_hash_table *htab; 2008 2009 htab = elf_s390_hash_table (info); 2010 2011 if (h->plt.offset != (bfd_vma) -1) 2012 { 2013 bfd_vma plt_index; 2014 bfd_vma got_offset; 2015 Elf_Internal_Rela rela; 2016 Elf64_External_Rela *loc; 2017 2018 /* This symbol has an entry in the procedure linkage table. Set 2019 it up. */ 2020 2021 if (h->dynindx == -1 2022 || htab->splt == NULL 2023 || htab->sgotplt == NULL 2024 || htab->srelplt == NULL) 2025 abort (); 2026 2027 /* Calc. index no. 2028 Current offset - size first entry / entry size. */ 2029 plt_index = (h->plt.offset - PLT_FIRST_ENTRY_SIZE) / PLT_ENTRY_SIZE; 2030 2031 /* Offset in GOT is PLT index plus GOT headers(3) times 8, 2032 addr & GOT addr. */ 2033 got_offset = (plt_index + 3) * GOT_ENTRY_SIZE; 2034 2035 /* Fill in the blueprint of a PLT. */ 2036 bfd_put_32 (output_bfd, (bfd_vma) PLT_ENTRY_WORD0, 2037 htab->splt->contents + h->plt.offset); 2038 bfd_put_32 (output_bfd, (bfd_vma) PLT_ENTRY_WORD1, 2039 htab->splt->contents + h->plt.offset + 4); 2040 bfd_put_32 (output_bfd, (bfd_vma) PLT_ENTRY_WORD2, 2041 htab->splt->contents + h->plt.offset + 8); 2042 bfd_put_32 (output_bfd, (bfd_vma) PLT_ENTRY_WORD3, 2043 htab->splt->contents + h->plt.offset + 12); 2044 bfd_put_32 (output_bfd, (bfd_vma) PLT_ENTRY_WORD4, 2045 htab->splt->contents + h->plt.offset + 16); 2046 bfd_put_32 (output_bfd, (bfd_vma) PLT_ENTRY_WORD5, 2047 htab->splt->contents + h->plt.offset + 20); 2048 bfd_put_32 (output_bfd, (bfd_vma) PLT_ENTRY_WORD6, 2049 htab->splt->contents + h->plt.offset + 24); 2050 bfd_put_32 (output_bfd, (bfd_vma) PLT_ENTRY_WORD7, 2051 htab->splt->contents + h->plt.offset + 28); 2052 /* Fixup the relative address to the GOT entry */ 2053 bfd_put_32 (output_bfd, 2054 (htab->sgotplt->output_section->vma + 2055 htab->sgotplt->output_offset + got_offset 2056 - (htab->splt->output_section->vma + h->plt.offset))/2, 2057 htab->splt->contents + h->plt.offset + 2); 2058 /* Fixup the relative branch to PLT 0 */ 2059 bfd_put_32 (output_bfd, - (PLT_FIRST_ENTRY_SIZE + 2060 (PLT_ENTRY_SIZE * plt_index) + 22)/2, 2061 htab->splt->contents + h->plt.offset + 24); 2062 /* Fixup offset into symbol table */ 2063 bfd_put_32 (output_bfd, plt_index * sizeof (Elf64_External_Rela), 2064 htab->splt->contents + h->plt.offset + 28); 2065 2066 /* Fill in the entry in the global offset table. 2067 Points to instruction after GOT offset. */ 2068 bfd_put_64 (output_bfd, 2069 (htab->splt->output_section->vma 2070 + htab->splt->output_offset 2071 + h->plt.offset 2072 + 14), 2073 htab->sgotplt->contents + got_offset); 2074 2075 /* Fill in the entry in the .rela.plt section. */ 2076 rela.r_offset = (htab->sgotplt->output_section->vma 2077 + htab->sgotplt->output_offset 2078 + got_offset); 2079 rela.r_info = ELF64_R_INFO (h->dynindx, R_390_JMP_SLOT); 2080 rela.r_addend = 0; 2081 loc = (Elf64_External_Rela *) htab->srelplt->contents + plt_index; 2082 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc); 2083 2084 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0) 2085 { 2086 /* Mark the symbol as undefined, rather than as defined in 2087 the .plt section. Leave the value alone. This is a clue 2088 for the dynamic linker, to make function pointer 2089 comparisons work between an application and shared 2090 library. */ 2091 sym->st_shndx = SHN_UNDEF; 2092 } 2093 } 2094 2095 if (h->got.offset != (bfd_vma) -1) 2096 { 2097 Elf_Internal_Rela rela; 2098 Elf64_External_Rela *loc; 2099 2100 /* This symbol has an entry in the global offset table. Set it 2101 up. */ 2102 2103 if (htab->sgot == NULL || htab->srelgot == NULL) 2104 abort (); 2105 2106 rela.r_offset = (htab->sgot->output_section->vma 2107 + htab->sgot->output_offset 2108 + (h->got.offset &~ (bfd_vma) 1)); 2109 2110 /* If this is a static link, or it is a -Bsymbolic link and the 2111 symbol is defined locally or was forced to be local because 2112 of a version file, we just want to emit a RELATIVE reloc. 2113 The entry in the global offset table will already have been 2114 initialized in the relocate_section function. */ 2115 if (info->shared 2116 && (info->symbolic 2117 || h->dynindx == -1 2118 || (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL)) 2119 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR)) 2120 { 2121 BFD_ASSERT((h->got.offset & 1) != 0); 2122 rela.r_info = ELF64_R_INFO (0, R_390_RELATIVE); 2123 rela.r_addend = (h->root.u.def.value 2124 + h->root.u.def.section->output_section->vma 2125 + h->root.u.def.section->output_offset); 2126 } 2127 else 2128 { 2129 BFD_ASSERT((h->got.offset & 1) == 0); 2130 bfd_put_64 (output_bfd, (bfd_vma) 0, htab->sgot->contents + h->got.offset); 2131 rela.r_info = ELF64_R_INFO (h->dynindx, R_390_GLOB_DAT); 2132 rela.r_addend = 0; 2133 } 2134 2135 loc = (Elf64_External_Rela *) htab->srelgot->contents; 2136 loc += htab->srelgot->reloc_count++; 2137 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc); 2138 } 2139 2140 if ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_COPY) != 0) 2141 { 2142 Elf_Internal_Rela rela; 2143 Elf64_External_Rela *loc; 2144 2145 /* This symbols needs a copy reloc. Set it up. */ 2146 2147 if (h->dynindx == -1 2148 || (h->root.type != bfd_link_hash_defined 2149 && h->root.type != bfd_link_hash_defweak) 2150 || htab->srelbss == NULL) 2151 abort (); 2152 2153 rela.r_offset = (h->root.u.def.value 2154 + h->root.u.def.section->output_section->vma 2155 + h->root.u.def.section->output_offset); 2156 rela.r_info = ELF64_R_INFO (h->dynindx, R_390_COPY); 2157 rela.r_addend = 0; 2158 loc = (Elf64_External_Rela *) htab->srelbss->contents; 2159 loc += htab->srelbss->reloc_count++; 2160 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc); 2161 } 2162 2163 /* Mark some specially defined symbols as absolute. */ 2164 if (strcmp (h->root.root.string, "_DYNAMIC") == 0 2165 || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0 2166 || strcmp (h->root.root.string, "_PROCEDURE_LINKAGE_TABLE_") == 0) 2167 sym->st_shndx = SHN_ABS; 2168 2169 return true; 2170} 2171 2172/* Used to decide how to sort relocs in an optimal manner for the 2173 dynamic linker, before writing them out. */ 2174 2175static enum elf_reloc_type_class 2176elf_s390_reloc_type_class (rela) 2177 const Elf_Internal_Rela *rela; 2178{ 2179 switch ((int) ELF64_R_TYPE (rela->r_info)) 2180 { 2181 case R_390_RELATIVE: 2182 return reloc_class_relative; 2183 case R_390_JMP_SLOT: 2184 return reloc_class_plt; 2185 case R_390_COPY: 2186 return reloc_class_copy; 2187 default: 2188 return reloc_class_normal; 2189 } 2190} 2191 2192/* Finish up the dynamic sections. */ 2193 2194static boolean 2195elf_s390_finish_dynamic_sections (output_bfd, info) 2196 bfd *output_bfd; 2197 struct bfd_link_info *info; 2198{ 2199 struct elf_s390_link_hash_table *htab; 2200 bfd *dynobj; 2201 asection *sdyn; 2202 2203 htab = elf_s390_hash_table (info); 2204 dynobj = htab->elf.dynobj; 2205 sdyn = bfd_get_section_by_name (dynobj, ".dynamic"); 2206 2207 if (htab->elf.dynamic_sections_created) 2208 { 2209 Elf64_External_Dyn *dyncon, *dynconend; 2210 2211 if (sdyn == NULL || htab->sgot == NULL) 2212 abort (); 2213 2214 dyncon = (Elf64_External_Dyn *) sdyn->contents; 2215 dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->_raw_size); 2216 for (; dyncon < dynconend; dyncon++) 2217 { 2218 Elf_Internal_Dyn dyn; 2219 asection *s; 2220 2221 bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn); 2222 2223 switch (dyn.d_tag) 2224 { 2225 default: 2226 continue; 2227 2228 case DT_PLTGOT: 2229 dyn.d_un.d_ptr = htab->sgot->output_section->vma; 2230 break; 2231 2232 case DT_JMPREL: 2233 dyn.d_un.d_ptr = htab->srelplt->output_section->vma; 2234 break; 2235 2236 case DT_PLTRELSZ: 2237 s = htab->srelplt->output_section; 2238 if (s->_cooked_size != 0) 2239 dyn.d_un.d_val = s->_cooked_size; 2240 else 2241 dyn.d_un.d_val = s->_raw_size; 2242 break; 2243 2244 case DT_RELASZ: 2245 /* The procedure linkage table relocs (DT_JMPREL) should 2246 not be included in the overall relocs (DT_RELA). 2247 Therefore, we override the DT_RELASZ entry here to 2248 make it not include the JMPREL relocs. Since the 2249 linker script arranges for .rela.plt to follow all 2250 other relocation sections, we don't have to worry 2251 about changing the DT_RELA entry. */ 2252 s = htab->srelplt->output_section; 2253 if (s->_cooked_size != 0) 2254 dyn.d_un.d_val -= s->_cooked_size; 2255 else 2256 dyn.d_un.d_val -= s->_raw_size; 2257 break; 2258 } 2259 2260 bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon); 2261 } 2262 2263 /* Fill in the special first entry in the procedure linkage table. */ 2264 if (htab->splt && htab->splt->_raw_size > 0) 2265 { 2266 /* fill in blueprint for plt 0 entry */ 2267 bfd_put_32 (output_bfd, (bfd_vma) PLT_FIRST_ENTRY_WORD0, 2268 htab->splt->contents ); 2269 bfd_put_32 (output_bfd, (bfd_vma) PLT_FIRST_ENTRY_WORD1, 2270 htab->splt->contents +4 ); 2271 bfd_put_32 (output_bfd, (bfd_vma) PLT_FIRST_ENTRY_WORD3, 2272 htab->splt->contents +12 ); 2273 bfd_put_32 (output_bfd, (bfd_vma) PLT_FIRST_ENTRY_WORD4, 2274 htab->splt->contents +16 ); 2275 bfd_put_32 (output_bfd, (bfd_vma) PLT_FIRST_ENTRY_WORD5, 2276 htab->splt->contents +20 ); 2277 bfd_put_32 (output_bfd, (bfd_vma) PLT_FIRST_ENTRY_WORD6, 2278 htab->splt->contents + 24); 2279 bfd_put_32 (output_bfd, (bfd_vma) PLT_FIRST_ENTRY_WORD7, 2280 htab->splt->contents + 28 ); 2281 /* Fixup relative address to start of GOT */ 2282 bfd_put_32 (output_bfd, 2283 (htab->sgotplt->output_section->vma + 2284 htab->sgotplt->output_offset 2285 - htab->splt->output_section->vma - 6)/2, 2286 htab->splt->contents + 8); 2287 } 2288 elf_section_data (htab->splt->output_section) 2289 ->this_hdr.sh_entsize = PLT_ENTRY_SIZE; 2290 } 2291 2292 if (htab->sgotplt) 2293 { 2294 /* Fill in the first three entries in the global offset table. */ 2295 if (htab->sgotplt->_raw_size > 0) 2296 { 2297 bfd_put_64 (output_bfd, 2298 (sdyn == NULL ? (bfd_vma) 0 2299 : sdyn->output_section->vma + sdyn->output_offset), 2300 htab->sgotplt->contents); 2301 /* One entry for shared object struct ptr. */ 2302 bfd_put_64 (output_bfd, (bfd_vma) 0, htab->sgotplt->contents + 8); 2303 /* One entry for _dl_runtime_resolve. */ 2304 bfd_put_64 (output_bfd, (bfd_vma) 0, htab->sgotplt->contents + 12); 2305 } 2306 2307 elf_section_data (htab->sgot->output_section) 2308 ->this_hdr.sh_entsize = 8; 2309 } 2310 return true; 2311} 2312 2313static boolean 2314elf_s390_object_p (abfd) 2315 bfd *abfd; 2316{ 2317 return bfd_default_set_arch_mach (abfd, bfd_arch_s390, bfd_mach_s390_64); 2318} 2319 2320/* 2321 * Why was the hash table entry size definition changed from 2322 * ARCH_SIZE/8 to 4? This breaks the 64 bit dynamic linker and 2323 * this is the only reason for the s390_elf64_size_info structure. 2324 */ 2325 2326const struct elf_size_info s390_elf64_size_info = 2327{ 2328 sizeof (Elf64_External_Ehdr), 2329 sizeof (Elf64_External_Phdr), 2330 sizeof (Elf64_External_Shdr), 2331 sizeof (Elf64_External_Rel), 2332 sizeof (Elf64_External_Rela), 2333 sizeof (Elf64_External_Sym), 2334 sizeof (Elf64_External_Dyn), 2335 sizeof (Elf_External_Note), 2336 8, /* hash-table entry size */ 2337 1, /* internal relocations per external relocations */ 2338 64, /* arch_size */ 2339 8, /* file_align */ 2340 ELFCLASS64, EV_CURRENT, 2341 bfd_elf64_write_out_phdrs, 2342 bfd_elf64_write_shdrs_and_ehdr, 2343 bfd_elf64_write_relocs, 2344 bfd_elf64_swap_symbol_in, 2345 bfd_elf64_swap_symbol_out, 2346 bfd_elf64_slurp_reloc_table, 2347 bfd_elf64_slurp_symbol_table, 2348 bfd_elf64_swap_dyn_in, 2349 bfd_elf64_swap_dyn_out, 2350 NULL, 2351 NULL, 2352 NULL, 2353 NULL 2354}; 2355 2356#define TARGET_BIG_SYM bfd_elf64_s390_vec 2357#define TARGET_BIG_NAME "elf64-s390" 2358#define ELF_ARCH bfd_arch_s390 2359#define ELF_MACHINE_CODE EM_S390 2360#define ELF_MACHINE_ALT1 EM_S390_OLD 2361#define ELF_MAXPAGESIZE 0x1000 2362 2363#define elf_backend_size_info s390_elf64_size_info 2364 2365#define elf_backend_can_gc_sections 1 2366#define elf_backend_can_refcount 1 2367#define elf_backend_want_got_plt 1 2368#define elf_backend_plt_readonly 1 2369#define elf_backend_want_plt_sym 0 2370#define elf_backend_got_header_size 24 2371#define elf_backend_plt_header_size PLT_ENTRY_SIZE 2372#define elf_backend_rela_normal 1 2373 2374#define elf_info_to_howto elf_s390_info_to_howto 2375 2376#define bfd_elf64_bfd_is_local_label_name elf_s390_is_local_label_name 2377#define bfd_elf64_bfd_link_hash_table_create elf_s390_link_hash_table_create 2378#define bfd_elf64_bfd_reloc_type_lookup elf_s390_reloc_type_lookup 2379 2380#define elf_backend_adjust_dynamic_symbol elf_s390_adjust_dynamic_symbol 2381#define elf_backend_check_relocs elf_s390_check_relocs 2382#define elf_backend_copy_indirect_symbol elf_s390_copy_indirect_symbol 2383#define elf_backend_create_dynamic_sections elf_s390_create_dynamic_sections 2384#define elf_backend_finish_dynamic_sections elf_s390_finish_dynamic_sections 2385#define elf_backend_finish_dynamic_symbol elf_s390_finish_dynamic_symbol 2386#define elf_backend_gc_mark_hook elf_s390_gc_mark_hook 2387#define elf_backend_gc_sweep_hook elf_s390_gc_sweep_hook 2388#define elf_backend_reloc_type_class elf_s390_reloc_type_class 2389#define elf_backend_relocate_section elf_s390_relocate_section 2390#define elf_backend_size_dynamic_sections elf_s390_size_dynamic_sections 2391#define elf_backend_reloc_type_class elf_s390_reloc_type_class 2392 2393#define elf_backend_object_p elf_s390_object_p 2394 2395#include "elf64-target.h" 2396