1 /* RISC-V-specific support for NN-bit ELF. 2 Copyright 2011-2014 Free Software Foundation, Inc. 3 4 Contributed by Andrew Waterman (waterman@cs.berkeley.edu) at UC Berkeley. 5 Based on TILE-Gx and MIPS targets. 6 7 This file is part of BFD, the Binary File Descriptor library. 8 9 This program is free software; you can redistribute it and/or modify 10 it under the terms of the GNU General Public License as published by 11 the Free Software Foundation; either version 3 of the License, or 12 (at your option) any later version. 13 14 This program is distributed in the hope that it will be useful, 15 but WITHOUT ANY WARRANTY; without even the implied warranty of 16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 GNU General Public License for more details. 18 19 You should have received a copy of the GNU General Public License 20 along with this program; if not, write to the Free Software 21 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, 22 MA 02110-1301, USA. */ 23 24 25 /* This file handles RISC-V ELF targets. */ 26 27 #include "sysdep.h" 28 #include "bfd.h" 29 #include "libbfd.h" 30 #include "bfdlink.h" 31 #include "genlink.h" 32 #include "elf-bfd.h" 33 #include "elfxx-riscv.h" 34 #include "elf/riscv.h" 35 #include "opcode/riscv.h" 36 37 #define ARCH_SIZE NN 38 39 #define MINUS_ONE ((bfd_vma)0 - 1) 40 41 #define RISCV_ELF_LOG_WORD_BYTES (ARCH_SIZE == 32 ? 2 : 3) 42 43 #define RISCV_ELF_WORD_BYTES (1 << RISCV_ELF_LOG_WORD_BYTES) 44 45 /* The name of the dynamic interpreter. This is put in the .interp 46 section. */ 47 48 #define ELF64_DYNAMIC_INTERPRETER "/lib/ld.so.1" 49 #define ELF32_DYNAMIC_INTERPRETER "/lib32/ld.so.1" 50 51 /* The RISC-V linker needs to keep track of the number of relocs that it 52 decides to copy as dynamic relocs in check_relocs for each symbol. 53 This is so that it can later discard them if they are found to be 54 unnecessary. We store the information in a field extending the 55 regular ELF linker hash table. */ 56 57 struct riscv_elf_dyn_relocs 58 { 59 struct riscv_elf_dyn_relocs *next; 60 61 /* The input section of the reloc. */ 62 asection *sec; 63 64 /* Total number of relocs copied for the input section. */ 65 bfd_size_type count; 66 67 /* Number of pc-relative relocs copied for the input section. */ 68 bfd_size_type pc_count; 69 }; 70 71 /* RISC-V ELF linker hash entry. */ 72 73 struct riscv_elf_link_hash_entry 74 { 75 struct elf_link_hash_entry elf; 76 77 /* Track dynamic relocs copied for this symbol. */ 78 struct riscv_elf_dyn_relocs *dyn_relocs; 79 80 #define GOT_UNKNOWN 0 81 #define GOT_NORMAL 1 82 #define GOT_TLS_GD 2 83 #define GOT_TLS_IE 4 84 #define GOT_TLS_LE 8 85 char tls_type; 86 }; 87 88 #define riscv_elf_hash_entry(ent) \ 89 ((struct riscv_elf_link_hash_entry *)(ent)) 90 91 struct _bfd_riscv_elf_obj_tdata 92 { 93 struct elf_obj_tdata root; 94 95 /* tls_type for each local got entry. */ 96 char *local_got_tls_type; 97 }; 98 99 #define _bfd_riscv_elf_tdata(abfd) \ 100 ((struct _bfd_riscv_elf_obj_tdata *) (abfd)->tdata.any) 101 102 #define _bfd_riscv_elf_local_got_tls_type(abfd) \ 103 (_bfd_riscv_elf_tdata (abfd)->local_got_tls_type) 104 105 #define _bfd_riscv_elf_tls_type(abfd, h, symndx) \ 106 (*((h) != NULL ? &riscv_elf_hash_entry(h)->tls_type \ 107 : &_bfd_riscv_elf_local_got_tls_type (abfd) [symndx])) 108 109 #define is_riscv_elf(bfd) \ 110 (bfd_get_flavour (bfd) == bfd_target_elf_flavour \ 111 && elf_tdata (bfd) != NULL \ 112 && elf_object_id (bfd) == RISCV_ELF_DATA) 113 114 #include "elf/common.h" 115 #include "elf/internal.h" 116 117 struct riscv_elf_link_hash_table 118 { 119 struct elf_link_hash_table elf; 120 121 /* Short-cuts to get to dynamic linker sections. */ 122 asection *sdynbss; 123 asection *srelbss; 124 asection *sdyntdata; 125 126 /* Small local sym to section mapping cache. */ 127 struct sym_cache sym_cache; 128 }; 129 130 131 /* Get the RISC-V ELF linker hash table from a link_info structure. */ 132 #define riscv_elf_hash_table(p) \ 133 (elf_hash_table_id ((struct elf_link_hash_table *) ((p)->hash)) \ 134 == RISCV_ELF_DATA ? ((struct riscv_elf_link_hash_table *) ((p)->hash)) : NULL) 135 136 static void 137 riscv_info_to_howto_rela (bfd *abfd ATTRIBUTE_UNUSED, 138 arelent *cache_ptr, 139 Elf_Internal_Rela *dst) 140 { 141 cache_ptr->howto = riscv_elf_rtype_to_howto (ELFNN_R_TYPE (dst->r_info)); 142 } 143 144 static void 145 riscv_elf_append_rela (bfd *abfd, asection *s, Elf_Internal_Rela *rel) 146 { 147 const struct elf_backend_data *bed; 148 bfd_byte *loc; 149 150 bed = get_elf_backend_data (abfd); 151 loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rela); 152 bed->s->swap_reloca_out (abfd, rel, loc); 153 } 154 155 /* PLT/GOT stuff */ 156 157 #define PLT_HEADER_INSNS 8 158 #define PLT_ENTRY_INSNS 4 159 #define PLT_HEADER_SIZE (PLT_HEADER_INSNS * 4) 160 #define PLT_ENTRY_SIZE (PLT_ENTRY_INSNS * 4) 161 162 #define GOT_ENTRY_SIZE RISCV_ELF_WORD_BYTES 163 164 #define GOTPLT_HEADER_SIZE (2 * GOT_ENTRY_SIZE) 165 166 #define sec_addr(sec) ((sec)->output_section->vma + (sec)->output_offset) 167 168 static bfd_vma 169 riscv_elf_got_plt_val (bfd_vma plt_index, struct bfd_link_info *info) 170 { 171 return sec_addr (riscv_elf_hash_table (info)->elf.sgotplt) 172 + GOTPLT_HEADER_SIZE + (plt_index * GOT_ENTRY_SIZE); 173 } 174 175 #if ARCH_SIZE == 32 176 # define MATCH_LREG MATCH_LW 177 #else 178 # define MATCH_LREG MATCH_LD 179 #endif 180 181 /* The format of the first PLT entry. */ 182 183 static void 184 riscv_make_plt0_entry(bfd_vma gotplt_addr, bfd_vma addr, uint32_t *entry) 185 { 186 /* auipc t2, %hi(.got.plt) 187 sub t1, t1, t0 # shifted .got.plt offset + hdr size + 12 188 l[w|d] t3, %lo(.got.plt)(t2) # _dl_runtime_resolve 189 addi t1, t1, -(hdr size + 12) # shifted .got.plt offset 190 addi t0, t2, %lo(.got.plt) # &.got.plt 191 srli t1, t1, log2(16/PTRSIZE) # .got.plt offset 192 l[w|d] t0, PTRSIZE(t0) # link map 193 jr t3 */ 194 195 entry[0] = RISCV_UTYPE (AUIPC, X_T2, RISCV_PCREL_HIGH_PART (gotplt_addr, addr)); 196 entry[1] = RISCV_RTYPE (SUB, X_T1, X_T1, X_T0); 197 entry[2] = RISCV_ITYPE (LREG, X_T3, X_T2, RISCV_PCREL_LOW_PART (gotplt_addr, addr)); 198 entry[3] = RISCV_ITYPE (ADDI, X_T1, X_T1, -(PLT_HEADER_SIZE + 12)); 199 entry[4] = RISCV_ITYPE (ADDI, X_T0, X_T2, RISCV_PCREL_LOW_PART (gotplt_addr, addr)); 200 entry[5] = RISCV_ITYPE (SRLI, X_T1, X_T1, 4 - RISCV_ELF_LOG_WORD_BYTES); 201 entry[6] = RISCV_ITYPE (LREG, X_T0, X_T0, RISCV_ELF_WORD_BYTES); 202 entry[7] = RISCV_ITYPE (JALR, 0, X_T3, 0); 203 } 204 205 /* The format of subsequent PLT entries. */ 206 207 static void 208 riscv_make_plt_entry(bfd_vma got_address, bfd_vma addr, uint32_t *entry) 209 { 210 /* auipc t1, %hi(.got.plt entry) 211 l[w|d] t0, %lo(.got.plt entry)(t1) 212 jalr t1, t0 213 nop */ 214 215 entry[0] = RISCV_UTYPE (AUIPC, X_T1, RISCV_PCREL_HIGH_PART (got_address, addr)); 216 entry[1] = RISCV_ITYPE (LREG, X_T0, X_T1, RISCV_PCREL_LOW_PART(got_address, addr)); 217 entry[2] = RISCV_ITYPE (JALR, X_T1, X_T0, 0); 218 entry[3] = RISCV_NOP; 219 } 220 221 /* Create an entry in an RISC-V ELF linker hash table. */ 222 223 static struct bfd_hash_entry * 224 link_hash_newfunc (struct bfd_hash_entry *entry, 225 struct bfd_hash_table *table, const char *string) 226 { 227 /* Allocate the structure if it has not already been allocated by a 228 subclass. */ 229 if (entry == NULL) 230 { 231 entry = 232 bfd_hash_allocate (table, 233 sizeof (struct riscv_elf_link_hash_entry)); 234 if (entry == NULL) 235 return entry; 236 } 237 238 /* Call the allocation method of the superclass. */ 239 entry = _bfd_elf_link_hash_newfunc (entry, table, string); 240 if (entry != NULL) 241 { 242 struct riscv_elf_link_hash_entry *eh; 243 244 eh = (struct riscv_elf_link_hash_entry *) entry; 245 eh->dyn_relocs = NULL; 246 eh->tls_type = GOT_UNKNOWN; 247 } 248 249 return entry; 250 } 251 252 /* Create a RISC-V ELF linker hash table. */ 253 254 static struct bfd_link_hash_table * 255 riscv_elf_link_hash_table_create (bfd *abfd) 256 { 257 struct riscv_elf_link_hash_table *ret; 258 bfd_size_type amt = sizeof (struct riscv_elf_link_hash_table); 259 260 ret = (struct riscv_elf_link_hash_table *) bfd_zmalloc (amt); 261 if (ret == NULL) 262 return NULL; 263 264 if (!_bfd_elf_link_hash_table_init (&ret->elf, abfd, link_hash_newfunc, 265 sizeof (struct riscv_elf_link_hash_entry), 266 RISCV_ELF_DATA)) 267 { 268 free (ret); 269 return NULL; 270 } 271 272 return &ret->elf.root; 273 } 274 275 /* Create the .got section. */ 276 277 static bfd_boolean 278 riscv_elf_create_got_section (bfd *abfd, struct bfd_link_info *info) 279 { 280 flagword flags; 281 asection *s, *s_got; 282 struct elf_link_hash_entry *h; 283 const struct elf_backend_data *bed = get_elf_backend_data (abfd); 284 struct elf_link_hash_table *htab = elf_hash_table (info); 285 286 /* This function may be called more than once. */ 287 s = bfd_get_linker_section (abfd, ".got"); 288 if (s != NULL) 289 return TRUE; 290 291 flags = bed->dynamic_sec_flags; 292 293 s = bfd_make_section_anyway_with_flags (abfd, 294 (bed->rela_plts_and_copies_p 295 ? ".rela.got" : ".rel.got"), 296 (bed->dynamic_sec_flags 297 | SEC_READONLY)); 298 if (s == NULL 299 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align)) 300 return FALSE; 301 htab->srelgot = s; 302 303 s = s_got = bfd_make_section_anyway_with_flags (abfd, ".got", flags); 304 if (s == NULL 305 || !bfd_set_section_alignment (abfd, s, bed->s->log_file_align)) 306 return FALSE; 307 htab->sgot = s; 308 309 /* The first bit of the global offset table is the header. */ 310 s->size += bed->got_header_size; 311 312 if (bed->want_got_plt) 313 { 314 s = bfd_make_section_anyway_with_flags (abfd, ".got.plt", flags); 315 if (s == NULL 316 || !bfd_set_section_alignment (abfd, s, 317 bed->s->log_file_align)) 318 return FALSE; 319 htab->sgotplt = s; 320 321 /* Reserve room for the header. */ 322 s->size += GOTPLT_HEADER_SIZE; 323 } 324 325 if (bed->want_got_sym) 326 { 327 /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got 328 section. We don't do this in the linker script because we don't want 329 to define the symbol if we are not creating a global offset 330 table. */ 331 h = _bfd_elf_define_linkage_sym (abfd, info, s_got, 332 "_GLOBAL_OFFSET_TABLE_"); 333 elf_hash_table (info)->hgot = h; 334 if (h == NULL) 335 return FALSE; 336 } 337 338 return TRUE; 339 } 340 341 /* Create .plt, .rela.plt, .got, .got.plt, .rela.got, .dynbss, and 342 .rela.bss sections in DYNOBJ, and set up shortcuts to them in our 343 hash table. */ 344 345 static bfd_boolean 346 riscv_elf_create_dynamic_sections (bfd *dynobj, 347 struct bfd_link_info *info) 348 { 349 struct riscv_elf_link_hash_table *htab; 350 351 htab = riscv_elf_hash_table (info); 352 BFD_ASSERT (htab != NULL); 353 354 if (!riscv_elf_create_got_section (dynobj, info)) 355 return FALSE; 356 357 if (!_bfd_elf_create_dynamic_sections (dynobj, info)) 358 return FALSE; 359 360 htab->sdynbss = bfd_get_linker_section (dynobj, ".dynbss"); 361 if (!info->shared) 362 { 363 htab->srelbss = bfd_get_linker_section (dynobj, ".rela.bss"); 364 htab->sdyntdata = 365 bfd_make_section_anyway_with_flags (dynobj, ".tdata.dyn", 366 SEC_ALLOC | SEC_THREAD_LOCAL); 367 } 368 369 if (!htab->elf.splt || !htab->elf.srelplt || !htab->sdynbss 370 || (!info->shared && (!htab->srelbss || !htab->sdyntdata))) 371 abort (); 372 373 return TRUE; 374 } 375 376 /* Copy the extra info we tack onto an elf_link_hash_entry. */ 377 378 static void 379 riscv_elf_copy_indirect_symbol (struct bfd_link_info *info, 380 struct elf_link_hash_entry *dir, 381 struct elf_link_hash_entry *ind) 382 { 383 struct riscv_elf_link_hash_entry *edir, *eind; 384 385 edir = (struct riscv_elf_link_hash_entry *) dir; 386 eind = (struct riscv_elf_link_hash_entry *) ind; 387 388 if (eind->dyn_relocs != NULL) 389 { 390 if (edir->dyn_relocs != NULL) 391 { 392 struct riscv_elf_dyn_relocs **pp; 393 struct riscv_elf_dyn_relocs *p; 394 395 /* Add reloc counts against the indirect sym to the direct sym 396 list. Merge any entries against the same section. */ 397 for (pp = &eind->dyn_relocs; (p = *pp) != NULL; ) 398 { 399 struct riscv_elf_dyn_relocs *q; 400 401 for (q = edir->dyn_relocs; q != NULL; q = q->next) 402 if (q->sec == p->sec) 403 { 404 q->pc_count += p->pc_count; 405 q->count += p->count; 406 *pp = p->next; 407 break; 408 } 409 if (q == NULL) 410 pp = &p->next; 411 } 412 *pp = edir->dyn_relocs; 413 } 414 415 edir->dyn_relocs = eind->dyn_relocs; 416 eind->dyn_relocs = NULL; 417 } 418 419 if (ind->root.type == bfd_link_hash_indirect 420 && dir->got.refcount <= 0) 421 { 422 edir->tls_type = eind->tls_type; 423 eind->tls_type = GOT_UNKNOWN; 424 } 425 _bfd_elf_link_hash_copy_indirect (info, dir, ind); 426 } 427 428 static bfd_boolean 429 riscv_elf_record_tls_type (bfd *abfd, struct elf_link_hash_entry *h, 430 unsigned long symndx, char tls_type) 431 { 432 char *new_tls_type = &_bfd_riscv_elf_tls_type (abfd, h, symndx); 433 *new_tls_type |= tls_type; 434 if ((*new_tls_type & GOT_NORMAL) && (*new_tls_type & ~GOT_NORMAL)) 435 { 436 (*_bfd_error_handler) 437 (_("%B: `%s' accessed both as normal and thread local symbol"), 438 abfd, h ? h->root.root.string : "<local>"); 439 return FALSE; 440 } 441 return TRUE; 442 } 443 444 static bfd_boolean 445 riscv_elf_record_got_reference (bfd *abfd, struct bfd_link_info *info, 446 struct elf_link_hash_entry *h, long symndx) 447 { 448 struct riscv_elf_link_hash_table *htab = riscv_elf_hash_table (info); 449 Elf_Internal_Shdr *symtab_hdr = &elf_tdata (abfd)->symtab_hdr; 450 451 if (htab->elf.sgot == NULL) 452 { 453 if (!riscv_elf_create_got_section (htab->elf.dynobj, info)) 454 return FALSE; 455 } 456 457 if (h != NULL) 458 { 459 h->got.refcount += 1; 460 return TRUE; 461 } 462 463 /* This is a global offset table entry for a local symbol. */ 464 if (elf_local_got_refcounts (abfd) == NULL) 465 { 466 bfd_size_type size = symtab_hdr->sh_info * (sizeof (bfd_vma) + 1); 467 if (!(elf_local_got_refcounts (abfd) = bfd_zalloc (abfd, size))) 468 return FALSE; 469 _bfd_riscv_elf_local_got_tls_type (abfd) 470 = (char *) (elf_local_got_refcounts (abfd) + symtab_hdr->sh_info); 471 } 472 elf_local_got_refcounts (abfd) [symndx] += 1; 473 474 return TRUE; 475 } 476 477 static bfd_boolean 478 bad_static_reloc (bfd *abfd, unsigned r_type, struct elf_link_hash_entry *h) 479 { 480 (*_bfd_error_handler) 481 (_("%B: relocation %s against `%s' can not be used when making a shared object; recompile with -fPIC"), 482 abfd, riscv_elf_rtype_to_howto (r_type)->name, 483 h != NULL ? h->root.root.string : "a local symbol"); 484 bfd_set_error (bfd_error_bad_value); 485 return FALSE; 486 } 487 /* Look through the relocs for a section during the first phase, and 488 allocate space in the global offset table or procedure linkage 489 table. */ 490 491 static bfd_boolean 492 riscv_elf_check_relocs (bfd *abfd, struct bfd_link_info *info, 493 asection *sec, const Elf_Internal_Rela *relocs) 494 { 495 struct riscv_elf_link_hash_table *htab; 496 Elf_Internal_Shdr *symtab_hdr; 497 struct elf_link_hash_entry **sym_hashes; 498 const Elf_Internal_Rela *rel; 499 asection *sreloc = NULL; 500 501 if (info->relocatable) 502 return TRUE; 503 504 htab = riscv_elf_hash_table (info); 505 symtab_hdr = &elf_tdata (abfd)->symtab_hdr; 506 sym_hashes = elf_sym_hashes (abfd); 507 508 if (htab->elf.dynobj == NULL) 509 htab->elf.dynobj = abfd; 510 511 for (rel = relocs; rel < relocs + sec->reloc_count; rel++) 512 { 513 unsigned int r_type; 514 unsigned long r_symndx; 515 struct elf_link_hash_entry *h; 516 517 r_symndx = ELFNN_R_SYM (rel->r_info); 518 r_type = ELFNN_R_TYPE (rel->r_info); 519 520 if (r_symndx >= NUM_SHDR_ENTRIES (symtab_hdr)) 521 { 522 (*_bfd_error_handler) (_("%B: bad symbol index: %d"), 523 abfd, r_symndx); 524 return FALSE; 525 } 526 527 if (r_symndx < symtab_hdr->sh_info) 528 h = NULL; 529 else 530 { 531 h = sym_hashes[r_symndx - symtab_hdr->sh_info]; 532 while (h->root.type == bfd_link_hash_indirect 533 || h->root.type == bfd_link_hash_warning) 534 h = (struct elf_link_hash_entry *) h->root.u.i.link; 535 536 /* PR15323, ref flags aren't set for references in the same 537 object. */ 538 h->root.non_ir_ref = 1; 539 } 540 541 switch (r_type) 542 { 543 case R_RISCV_TLS_GD_HI20: 544 if (!riscv_elf_record_got_reference (abfd, info, h, r_symndx) 545 || !riscv_elf_record_tls_type (abfd, h, r_symndx, GOT_TLS_GD)) 546 return FALSE; 547 break; 548 549 case R_RISCV_TLS_GOT_HI20: 550 if (info->shared) 551 info->flags |= DF_STATIC_TLS; 552 if (!riscv_elf_record_got_reference (abfd, info, h, r_symndx) 553 || !riscv_elf_record_tls_type (abfd, h, r_symndx, GOT_TLS_IE)) 554 return FALSE; 555 break; 556 557 case R_RISCV_GOT_HI20: 558 if (!riscv_elf_record_got_reference (abfd, info, h, r_symndx) 559 || !riscv_elf_record_tls_type (abfd, h, r_symndx, GOT_NORMAL)) 560 return FALSE; 561 break; 562 563 case R_RISCV_CALL_PLT: 564 /* This symbol requires a procedure linkage table entry. We 565 actually build the entry in adjust_dynamic_symbol, 566 because this might be a case of linking PIC code without 567 linking in any dynamic objects, in which case we don't 568 need to generate a procedure linkage table after all. */ 569 570 if (h != NULL) 571 { 572 h->needs_plt = 1; 573 h->plt.refcount += 1; 574 } 575 break; 576 577 case R_RISCV_CALL: 578 case R_RISCV_JAL: 579 case R_RISCV_BRANCH: 580 case R_RISCV_PCREL_HI20: 581 /* In shared libs, these relocs are known to bind locally. */ 582 if (info->shared) 583 break; 584 goto static_reloc; 585 586 case R_RISCV_TPREL_HI20: 587 if (!info->executable) 588 return bad_static_reloc (abfd, r_type, h); 589 if (h != NULL) 590 riscv_elf_record_tls_type (abfd, h, r_symndx, GOT_TLS_LE); 591 goto static_reloc; 592 593 case R_RISCV_HI20: 594 if (info->shared) 595 return bad_static_reloc (abfd, r_type, h); 596 /* Fall through. */ 597 598 case R_RISCV_COPY: 599 case R_RISCV_JUMP_SLOT: 600 case R_RISCV_RELATIVE: 601 case R_RISCV_64: 602 case R_RISCV_32: 603 /* Fall through. */ 604 605 static_reloc: 606 if (h != NULL) 607 h->non_got_ref = 1; 608 609 if (h != NULL && !info->shared) 610 { 611 /* We may need a .plt entry if the function this reloc 612 refers to is in a shared lib. */ 613 h->plt.refcount += 1; 614 } 615 616 /* If we are creating a shared library, and this is a reloc 617 against a global symbol, or a non PC relative reloc 618 against a local symbol, then we need to copy the reloc 619 into the shared library. However, if we are linking with 620 -Bsymbolic, we do not need to copy a reloc against a 621 global symbol which is defined in an object we are 622 including in the link (i.e., DEF_REGULAR is set). At 623 this point we have not seen all the input files, so it is 624 possible that DEF_REGULAR is not set now but will be set 625 later (it is never cleared). In case of a weak definition, 626 DEF_REGULAR may be cleared later by a strong definition in 627 a shared library. We account for that possibility below by 628 storing information in the relocs_copied field of the hash 629 table entry. A similar situation occurs when creating 630 shared libraries and symbol visibility changes render the 631 symbol local. 632 633 If on the other hand, we are creating an executable, we 634 may need to keep relocations for symbols satisfied by a 635 dynamic library if we manage to avoid copy relocs for the 636 symbol. */ 637 if ((info->shared 638 && (sec->flags & SEC_ALLOC) != 0 639 && (! riscv_elf_rtype_to_howto (r_type)->pc_relative 640 || (h != NULL 641 && (! info->symbolic 642 || h->root.type == bfd_link_hash_defweak 643 || !h->def_regular)))) 644 || (!info->shared 645 && (sec->flags & SEC_ALLOC) != 0 646 && h != NULL 647 && (h->root.type == bfd_link_hash_defweak 648 || !h->def_regular))) 649 { 650 struct riscv_elf_dyn_relocs *p; 651 struct riscv_elf_dyn_relocs **head; 652 653 /* When creating a shared object, we must copy these 654 relocs into the output file. We create a reloc 655 section in dynobj and make room for the reloc. */ 656 if (sreloc == NULL) 657 { 658 sreloc = _bfd_elf_make_dynamic_reloc_section 659 (sec, htab->elf.dynobj, RISCV_ELF_LOG_WORD_BYTES, 660 abfd, /*rela?*/ TRUE); 661 662 if (sreloc == NULL) 663 return FALSE; 664 } 665 666 /* If this is a global symbol, we count the number of 667 relocations we need for this symbol. */ 668 if (h != NULL) 669 head = &((struct riscv_elf_link_hash_entry *) h)->dyn_relocs; 670 else 671 { 672 /* Track dynamic relocs needed for local syms too. 673 We really need local syms available to do this 674 easily. Oh well. */ 675 676 asection *s; 677 void *vpp; 678 Elf_Internal_Sym *isym; 679 680 isym = bfd_sym_from_r_symndx (&htab->sym_cache, 681 abfd, r_symndx); 682 if (isym == NULL) 683 return FALSE; 684 685 s = bfd_section_from_elf_index (abfd, isym->st_shndx); 686 if (s == NULL) 687 s = sec; 688 689 vpp = &elf_section_data (s)->local_dynrel; 690 head = (struct riscv_elf_dyn_relocs **) vpp; 691 } 692 693 p = *head; 694 if (p == NULL || p->sec != sec) 695 { 696 bfd_size_type amt = sizeof *p; 697 p = ((struct riscv_elf_dyn_relocs *) 698 bfd_alloc (htab->elf.dynobj, amt)); 699 if (p == NULL) 700 return FALSE; 701 p->next = *head; 702 *head = p; 703 p->sec = sec; 704 p->count = 0; 705 p->pc_count = 0; 706 } 707 708 p->count += 1; 709 p->pc_count += riscv_elf_rtype_to_howto (r_type)->pc_relative; 710 } 711 712 break; 713 714 case R_RISCV_GNU_VTINHERIT: 715 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset)) 716 return FALSE; 717 break; 718 719 case R_RISCV_GNU_VTENTRY: 720 if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend)) 721 return FALSE; 722 break; 723 724 default: 725 break; 726 } 727 } 728 729 return TRUE; 730 } 731 732 static asection * 733 riscv_elf_gc_mark_hook (asection *sec, 734 struct bfd_link_info *info, 735 Elf_Internal_Rela *rel, 736 struct elf_link_hash_entry *h, 737 Elf_Internal_Sym *sym) 738 { 739 if (h != NULL) 740 switch (ELFNN_R_TYPE (rel->r_info)) 741 { 742 case R_RISCV_GNU_VTINHERIT: 743 case R_RISCV_GNU_VTENTRY: 744 return NULL; 745 } 746 747 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym); 748 } 749 750 /* Update the got entry reference counts for the section being removed. */ 751 static bfd_boolean 752 riscv_elf_gc_sweep_hook (bfd *abfd, struct bfd_link_info *info, 753 asection *sec, const Elf_Internal_Rela *relocs) 754 { 755 const Elf_Internal_Rela *rel, *relend; 756 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (abfd); 757 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (abfd); 758 bfd_signed_vma *local_got_refcounts = elf_local_got_refcounts (abfd); 759 760 if (info->relocatable) 761 return TRUE; 762 763 elf_section_data (sec)->local_dynrel = NULL; 764 765 for (rel = relocs, relend = relocs + sec->reloc_count; rel < relend; rel++) 766 { 767 unsigned long r_symndx; 768 struct elf_link_hash_entry *h = NULL; 769 770 r_symndx = ELFNN_R_SYM (rel->r_info); 771 if (r_symndx >= symtab_hdr->sh_info) 772 { 773 struct riscv_elf_link_hash_entry *eh; 774 struct riscv_elf_dyn_relocs **pp; 775 struct riscv_elf_dyn_relocs *p; 776 777 h = sym_hashes[r_symndx - symtab_hdr->sh_info]; 778 while (h->root.type == bfd_link_hash_indirect 779 || h->root.type == bfd_link_hash_warning) 780 h = (struct elf_link_hash_entry *) h->root.u.i.link; 781 eh = (struct riscv_elf_link_hash_entry *) h; 782 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; pp = &p->next) 783 if (p->sec == sec) 784 { 785 /* Everything must go for SEC. */ 786 *pp = p->next; 787 break; 788 } 789 } 790 791 switch (ELFNN_R_TYPE (rel->r_info)) 792 { 793 case R_RISCV_GOT_HI20: 794 case R_RISCV_TLS_GOT_HI20: 795 case R_RISCV_TLS_GD_HI20: 796 if (h != NULL) 797 { 798 if (h->got.refcount > 0) 799 h->got.refcount--; 800 } 801 else 802 { 803 if (local_got_refcounts && 804 local_got_refcounts[r_symndx] > 0) 805 local_got_refcounts[r_symndx]--; 806 } 807 break; 808 809 case R_RISCV_HI20: 810 case R_RISCV_PCREL_HI20: 811 case R_RISCV_COPY: 812 case R_RISCV_JUMP_SLOT: 813 case R_RISCV_RELATIVE: 814 case R_RISCV_64: 815 case R_RISCV_32: 816 case R_RISCV_BRANCH: 817 case R_RISCV_CALL: 818 case R_RISCV_JAL: 819 if (info->shared) 820 break; 821 /* Fall through. */ 822 823 case R_RISCV_CALL_PLT: 824 if (h != NULL) 825 { 826 if (h->plt.refcount > 0) 827 h->plt.refcount--; 828 } 829 break; 830 831 default: 832 break; 833 } 834 } 835 836 return TRUE; 837 } 838 839 /* Adjust a symbol defined by a dynamic object and referenced by a 840 regular object. The current definition is in some section of the 841 dynamic object, but we're not including those sections. We have to 842 change the definition to something the rest of the link can 843 understand. */ 844 845 static bfd_boolean 846 riscv_elf_adjust_dynamic_symbol (struct bfd_link_info *info, 847 struct elf_link_hash_entry *h) 848 { 849 struct riscv_elf_link_hash_table *htab; 850 struct riscv_elf_link_hash_entry * eh; 851 struct riscv_elf_dyn_relocs *p; 852 bfd *dynobj; 853 asection *s; 854 855 htab = riscv_elf_hash_table (info); 856 BFD_ASSERT (htab != NULL); 857 858 dynobj = htab->elf.dynobj; 859 860 /* Make sure we know what is going on here. */ 861 BFD_ASSERT (dynobj != NULL 862 && (h->needs_plt 863 || h->type == STT_GNU_IFUNC 864 || h->u.weakdef != NULL 865 || (h->def_dynamic 866 && h->ref_regular 867 && !h->def_regular))); 868 869 /* If this is a function, put it in the procedure linkage table. We 870 will fill in the contents of the procedure linkage table later 871 (although we could actually do it here). */ 872 if (h->type == STT_FUNC || h->type == STT_GNU_IFUNC || h->needs_plt) 873 { 874 if (h->plt.refcount <= 0 875 || SYMBOL_CALLS_LOCAL (info, h) 876 || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT 877 && h->root.type == bfd_link_hash_undefweak)) 878 { 879 /* This case can occur if we saw a R_RISCV_CALL_PLT reloc in an 880 input file, but the symbol was never referred to by a dynamic 881 object, or if all references were garbage collected. In such 882 a case, we don't actually need to build a PLT entry. */ 883 h->plt.offset = (bfd_vma) -1; 884 h->needs_plt = 0; 885 } 886 887 return TRUE; 888 } 889 else 890 h->plt.offset = (bfd_vma) -1; 891 892 /* If this is a weak symbol, and there is a real definition, the 893 processor independent code will have arranged for us to see the 894 real definition first, and we can just use the same value. */ 895 if (h->u.weakdef != NULL) 896 { 897 BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined 898 || h->u.weakdef->root.type == bfd_link_hash_defweak); 899 h->root.u.def.section = h->u.weakdef->root.u.def.section; 900 h->root.u.def.value = h->u.weakdef->root.u.def.value; 901 return TRUE; 902 } 903 904 /* This is a reference to a symbol defined by a dynamic object which 905 is not a function. */ 906 907 /* If we are creating a shared library, we must presume that the 908 only references to the symbol are via the global offset table. 909 For such cases we need not do anything here; the relocations will 910 be handled correctly by relocate_section. */ 911 if (info->shared) 912 return TRUE; 913 914 /* If there are no references to this symbol that do not use the 915 GOT, we don't need to generate a copy reloc. */ 916 if (!h->non_got_ref) 917 return TRUE; 918 919 /* If -z nocopyreloc was given, we won't generate them either. */ 920 if (info->nocopyreloc) 921 { 922 h->non_got_ref = 0; 923 return TRUE; 924 } 925 926 eh = (struct riscv_elf_link_hash_entry *) h; 927 for (p = eh->dyn_relocs; p != NULL; p = p->next) 928 { 929 s = p->sec->output_section; 930 if (s != NULL && (s->flags & SEC_READONLY) != 0) 931 break; 932 } 933 934 /* If we didn't find any dynamic relocs in read-only sections, then 935 we'll be keeping the dynamic relocs and avoiding the copy reloc. */ 936 if (p == NULL) 937 { 938 h->non_got_ref = 0; 939 return TRUE; 940 } 941 942 /* We must allocate the symbol in our .dynbss section, which will 943 become part of the .bss section of the executable. There will be 944 an entry for this symbol in the .dynsym section. The dynamic 945 object will contain position independent code, so all references 946 from the dynamic object to this symbol will go through the global 947 offset table. The dynamic linker will use the .dynsym entry to 948 determine the address it must put in the global offset table, so 949 both the dynamic object and the regular object will refer to the 950 same memory location for the variable. */ 951 952 /* We must generate a R_RISCV_COPY reloc to tell the dynamic linker 953 to copy the initial value out of the dynamic object and into the 954 runtime process image. We need to remember the offset into the 955 .rel.bss section we are going to use. */ 956 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0 && h->size != 0) 957 { 958 htab->srelbss->size += sizeof (ElfNN_External_Rela); 959 h->needs_copy = 1; 960 } 961 962 if (eh->tls_type & ~GOT_NORMAL) 963 return _bfd_elf_adjust_dynamic_copy (h, htab->sdyntdata); 964 965 return _bfd_elf_adjust_dynamic_copy (h, htab->sdynbss); 966 } 967 968 /* Allocate space in .plt, .got and associated reloc sections for 969 dynamic relocs. */ 970 971 static bfd_boolean 972 allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf) 973 { 974 struct bfd_link_info *info; 975 struct riscv_elf_link_hash_table *htab; 976 struct riscv_elf_link_hash_entry *eh; 977 struct riscv_elf_dyn_relocs *p; 978 979 if (h->root.type == bfd_link_hash_indirect) 980 return TRUE; 981 982 info = (struct bfd_link_info *) inf; 983 htab = riscv_elf_hash_table (info); 984 BFD_ASSERT (htab != NULL); 985 986 if (htab->elf.dynamic_sections_created 987 && h->plt.refcount > 0) 988 { 989 /* Make sure this symbol is output as a dynamic symbol. 990 Undefined weak syms won't yet be marked as dynamic. */ 991 if (h->dynindx == -1 992 && !h->forced_local) 993 { 994 if (! bfd_elf_link_record_dynamic_symbol (info, h)) 995 return FALSE; 996 } 997 998 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, info->shared, h)) 999 { 1000 asection *s = htab->elf.splt; 1001 1002 if (s->size == 0) 1003 s->size = PLT_HEADER_SIZE; 1004 1005 h->plt.offset = s->size; 1006 1007 /* Make room for this entry. */ 1008 s->size += PLT_ENTRY_SIZE; 1009 1010 /* We also need to make an entry in the .got.plt section. */ 1011 htab->elf.sgotplt->size += GOT_ENTRY_SIZE; 1012 1013 /* We also need to make an entry in the .rela.plt section. */ 1014 htab->elf.srelplt->size += sizeof (ElfNN_External_Rela); 1015 1016 /* If this symbol is not defined in a regular file, and we are 1017 not generating a shared library, then set the symbol to this 1018 location in the .plt. This is required to make function 1019 pointers compare as equal between the normal executable and 1020 the shared library. */ 1021 if (! info->shared 1022 && !h->def_regular) 1023 { 1024 h->root.u.def.section = s; 1025 h->root.u.def.value = h->plt.offset; 1026 } 1027 } 1028 else 1029 { 1030 h->plt.offset = (bfd_vma) -1; 1031 h->needs_plt = 0; 1032 } 1033 } 1034 else 1035 { 1036 h->plt.offset = (bfd_vma) -1; 1037 h->needs_plt = 0; 1038 } 1039 1040 if (h->got.refcount > 0) 1041 { 1042 asection *s; 1043 bfd_boolean dyn; 1044 int tls_type = riscv_elf_hash_entry(h)->tls_type; 1045 1046 /* Make sure this symbol is output as a dynamic symbol. 1047 Undefined weak syms won't yet be marked as dynamic. */ 1048 if (h->dynindx == -1 1049 && !h->forced_local) 1050 { 1051 if (! bfd_elf_link_record_dynamic_symbol (info, h)) 1052 return FALSE; 1053 } 1054 1055 s = htab->elf.sgot; 1056 h->got.offset = s->size; 1057 dyn = htab->elf.dynamic_sections_created; 1058 if (tls_type & (GOT_TLS_GD | GOT_TLS_IE)) 1059 { 1060 /* TLS_GD needs two dynamic relocs and two GOT slots. */ 1061 if (tls_type & GOT_TLS_GD) 1062 { 1063 s->size += 2 * RISCV_ELF_WORD_BYTES; 1064 htab->elf.srelgot->size += 2 * sizeof (ElfNN_External_Rela); 1065 } 1066 1067 /* TLS_IE needs one dynamic reloc and one GOT slot. */ 1068 if (tls_type & GOT_TLS_IE) 1069 { 1070 s->size += RISCV_ELF_WORD_BYTES; 1071 htab->elf.srelgot->size += sizeof (ElfNN_External_Rela); 1072 } 1073 } 1074 else 1075 { 1076 s->size += RISCV_ELF_WORD_BYTES; 1077 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h)) 1078 htab->elf.srelgot->size += sizeof (ElfNN_External_Rela); 1079 } 1080 } 1081 else 1082 h->got.offset = (bfd_vma) -1; 1083 1084 eh = (struct riscv_elf_link_hash_entry *) h; 1085 if (eh->dyn_relocs == NULL) 1086 return TRUE; 1087 1088 /* In the shared -Bsymbolic case, discard space allocated for 1089 dynamic pc-relative relocs against symbols which turn out to be 1090 defined in regular objects. For the normal shared case, discard 1091 space for pc-relative relocs that have become local due to symbol 1092 visibility changes. */ 1093 1094 if (info->shared) 1095 { 1096 if (SYMBOL_CALLS_LOCAL (info, h)) 1097 { 1098 struct riscv_elf_dyn_relocs **pp; 1099 1100 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; ) 1101 { 1102 p->count -= p->pc_count; 1103 p->pc_count = 0; 1104 if (p->count == 0) 1105 *pp = p->next; 1106 else 1107 pp = &p->next; 1108 } 1109 } 1110 1111 /* Also discard relocs on undefined weak syms with non-default 1112 visibility. */ 1113 if (eh->dyn_relocs != NULL 1114 && h->root.type == bfd_link_hash_undefweak) 1115 { 1116 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT) 1117 eh->dyn_relocs = NULL; 1118 1119 /* Make sure undefined weak symbols are output as a dynamic 1120 symbol in PIEs. */ 1121 else if (h->dynindx == -1 1122 && !h->forced_local) 1123 { 1124 if (! bfd_elf_link_record_dynamic_symbol (info, h)) 1125 return FALSE; 1126 } 1127 } 1128 } 1129 else 1130 { 1131 /* For the non-shared case, discard space for relocs against 1132 symbols which turn out to need copy relocs or are not 1133 dynamic. */ 1134 1135 if (!h->non_got_ref 1136 && ((h->def_dynamic 1137 && !h->def_regular) 1138 || (htab->elf.dynamic_sections_created 1139 && (h->root.type == bfd_link_hash_undefweak 1140 || h->root.type == bfd_link_hash_undefined)))) 1141 { 1142 /* Make sure this symbol is output as a dynamic symbol. 1143 Undefined weak syms won't yet be marked as dynamic. */ 1144 if (h->dynindx == -1 1145 && !h->forced_local) 1146 { 1147 if (! bfd_elf_link_record_dynamic_symbol (info, h)) 1148 return FALSE; 1149 } 1150 1151 /* If that succeeded, we know we'll be keeping all the 1152 relocs. */ 1153 if (h->dynindx != -1) 1154 goto keep; 1155 } 1156 1157 eh->dyn_relocs = NULL; 1158 1159 keep: ; 1160 } 1161 1162 /* Finally, allocate space. */ 1163 for (p = eh->dyn_relocs; p != NULL; p = p->next) 1164 { 1165 asection *sreloc = elf_section_data (p->sec)->sreloc; 1166 sreloc->size += p->count * sizeof (ElfNN_External_Rela); 1167 } 1168 1169 return TRUE; 1170 } 1171 1172 /* Find any dynamic relocs that apply to read-only sections. */ 1173 1174 static bfd_boolean 1175 readonly_dynrelocs (struct elf_link_hash_entry *h, void *inf) 1176 { 1177 struct riscv_elf_link_hash_entry *eh; 1178 struct riscv_elf_dyn_relocs *p; 1179 1180 eh = (struct riscv_elf_link_hash_entry *) h; 1181 for (p = eh->dyn_relocs; p != NULL; p = p->next) 1182 { 1183 asection *s = p->sec->output_section; 1184 1185 if (s != NULL && (s->flags & SEC_READONLY) != 0) 1186 { 1187 ((struct bfd_link_info *) inf)->flags |= DF_TEXTREL; 1188 1189 /* Short-circuit the traversal. */ 1190 return FALSE; 1191 } 1192 } 1193 return TRUE; 1194 } 1195 1196 static bfd_boolean 1197 riscv_elf_size_dynamic_sections (bfd *output_bfd, struct bfd_link_info *info) 1198 { 1199 struct riscv_elf_link_hash_table *htab; 1200 bfd *dynobj; 1201 asection *s; 1202 bfd *ibfd; 1203 1204 htab = riscv_elf_hash_table (info); 1205 BFD_ASSERT (htab != NULL); 1206 dynobj = htab->elf.dynobj; 1207 BFD_ASSERT (dynobj != NULL); 1208 1209 if (elf_hash_table (info)->dynamic_sections_created) 1210 { 1211 /* Set the contents of the .interp section to the interpreter. */ 1212 if (info->executable) 1213 { 1214 s = bfd_get_linker_section (dynobj, ".interp"); 1215 BFD_ASSERT (s != NULL); 1216 s->size = strlen (ELFNN_DYNAMIC_INTERPRETER) + 1; 1217 s->contents = (unsigned char *) ELFNN_DYNAMIC_INTERPRETER; 1218 } 1219 } 1220 1221 /* Set up .got offsets for local syms, and space for local dynamic 1222 relocs. */ 1223 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next) 1224 { 1225 bfd_signed_vma *local_got; 1226 bfd_signed_vma *end_local_got; 1227 char *local_tls_type; 1228 bfd_size_type locsymcount; 1229 Elf_Internal_Shdr *symtab_hdr; 1230 asection *srel; 1231 1232 if (! is_riscv_elf (ibfd)) 1233 continue; 1234 1235 for (s = ibfd->sections; s != NULL; s = s->next) 1236 { 1237 struct riscv_elf_dyn_relocs *p; 1238 1239 for (p = elf_section_data (s)->local_dynrel; p != NULL; p = p->next) 1240 { 1241 if (!bfd_is_abs_section (p->sec) 1242 && bfd_is_abs_section (p->sec->output_section)) 1243 { 1244 /* Input section has been discarded, either because 1245 it is a copy of a linkonce section or due to 1246 linker script /DISCARD/, so we'll be discarding 1247 the relocs too. */ 1248 } 1249 else if (p->count != 0) 1250 { 1251 srel = elf_section_data (p->sec)->sreloc; 1252 srel->size += p->count * sizeof (ElfNN_External_Rela); 1253 if ((p->sec->output_section->flags & SEC_READONLY) != 0) 1254 info->flags |= DF_TEXTREL; 1255 } 1256 } 1257 } 1258 1259 local_got = elf_local_got_refcounts (ibfd); 1260 if (!local_got) 1261 continue; 1262 1263 symtab_hdr = &elf_symtab_hdr (ibfd); 1264 locsymcount = symtab_hdr->sh_info; 1265 end_local_got = local_got + locsymcount; 1266 local_tls_type = _bfd_riscv_elf_local_got_tls_type (ibfd); 1267 s = htab->elf.sgot; 1268 srel = htab->elf.srelgot; 1269 for (; local_got < end_local_got; ++local_got, ++local_tls_type) 1270 { 1271 if (*local_got > 0) 1272 { 1273 *local_got = s->size; 1274 s->size += RISCV_ELF_WORD_BYTES; 1275 if (*local_tls_type & GOT_TLS_GD) 1276 s->size += RISCV_ELF_WORD_BYTES; 1277 if (info->shared 1278 || (*local_tls_type & (GOT_TLS_GD | GOT_TLS_IE))) 1279 srel->size += sizeof (ElfNN_External_Rela); 1280 } 1281 else 1282 *local_got = (bfd_vma) -1; 1283 } 1284 } 1285 1286 /* Allocate global sym .plt and .got entries, and space for global 1287 sym dynamic relocs. */ 1288 elf_link_hash_traverse (&htab->elf, allocate_dynrelocs, info); 1289 1290 if (htab->elf.sgotplt) 1291 { 1292 struct elf_link_hash_entry *got; 1293 got = elf_link_hash_lookup (elf_hash_table (info), 1294 "_GLOBAL_OFFSET_TABLE_", 1295 FALSE, FALSE, FALSE); 1296 1297 /* Don't allocate .got.plt section if there are no GOT nor PLT 1298 entries and there is no refeence to _GLOBAL_OFFSET_TABLE_. */ 1299 if ((got == NULL 1300 || !got->ref_regular_nonweak) 1301 && (htab->elf.sgotplt->size == GOTPLT_HEADER_SIZE) 1302 && (htab->elf.splt == NULL 1303 || htab->elf.splt->size == 0) 1304 && (htab->elf.sgot == NULL 1305 || (htab->elf.sgot->size 1306 == get_elf_backend_data (output_bfd)->got_header_size))) 1307 htab->elf.sgotplt->size = 0; 1308 } 1309 1310 /* The check_relocs and adjust_dynamic_symbol entry points have 1311 determined the sizes of the various dynamic sections. Allocate 1312 memory for them. */ 1313 for (s = dynobj->sections; s != NULL; s = s->next) 1314 { 1315 if ((s->flags & SEC_LINKER_CREATED) == 0) 1316 continue; 1317 1318 if (s == htab->elf.splt 1319 || s == htab->elf.sgot 1320 || s == htab->elf.sgotplt 1321 || s == htab->sdynbss) 1322 { 1323 /* Strip this section if we don't need it; see the 1324 comment below. */ 1325 } 1326 else if (strncmp (s->name, ".rela", 5) == 0) 1327 { 1328 if (s->size != 0) 1329 { 1330 /* We use the reloc_count field as a counter if we need 1331 to copy relocs into the output file. */ 1332 s->reloc_count = 0; 1333 } 1334 } 1335 else 1336 { 1337 /* It's not one of our sections. */ 1338 continue; 1339 } 1340 1341 if (s->size == 0) 1342 { 1343 /* If we don't need this section, strip it from the 1344 output file. This is mostly to handle .rela.bss and 1345 .rela.plt. We must create both sections in 1346 create_dynamic_sections, because they must be created 1347 before the linker maps input sections to output 1348 sections. The linker does that before 1349 adjust_dynamic_symbol is called, and it is that 1350 function which decides whether anything needs to go 1351 into these sections. */ 1352 s->flags |= SEC_EXCLUDE; 1353 continue; 1354 } 1355 1356 if ((s->flags & SEC_HAS_CONTENTS) == 0) 1357 continue; 1358 1359 /* Allocate memory for the section contents. Zero the memory 1360 for the benefit of .rela.plt, which has 4 unused entries 1361 at the beginning, and we don't want garbage. */ 1362 s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->size); 1363 if (s->contents == NULL) 1364 return FALSE; 1365 } 1366 1367 if (elf_hash_table (info)->dynamic_sections_created) 1368 { 1369 /* Add some entries to the .dynamic section. We fill in the 1370 values later, in riscv_elf_finish_dynamic_sections, but we 1371 must add the entries now so that we get the correct size for 1372 the .dynamic section. The DT_DEBUG entry is filled in by the 1373 dynamic linker and used by the debugger. */ 1374 #define add_dynamic_entry(TAG, VAL) \ 1375 _bfd_elf_add_dynamic_entry (info, TAG, VAL) 1376 1377 if (info->executable) 1378 { 1379 if (!add_dynamic_entry (DT_DEBUG, 0)) 1380 return FALSE; 1381 } 1382 1383 if (htab->elf.srelplt->size != 0) 1384 { 1385 if (!add_dynamic_entry (DT_PLTGOT, 0) 1386 || !add_dynamic_entry (DT_PLTRELSZ, 0) 1387 || !add_dynamic_entry (DT_PLTREL, DT_RELA) 1388 || !add_dynamic_entry (DT_JMPREL, 0)) 1389 return FALSE; 1390 } 1391 1392 if (!add_dynamic_entry (DT_RELA, 0) 1393 || !add_dynamic_entry (DT_RELASZ, 0) 1394 || !add_dynamic_entry (DT_RELAENT, sizeof (ElfNN_External_Rela))) 1395 return FALSE; 1396 1397 /* If any dynamic relocs apply to a read-only section, 1398 then we need a DT_TEXTREL entry. */ 1399 if ((info->flags & DF_TEXTREL) == 0) 1400 elf_link_hash_traverse (&htab->elf, readonly_dynrelocs, info); 1401 1402 if (info->flags & DF_TEXTREL) 1403 { 1404 if (!add_dynamic_entry (DT_TEXTREL, 0)) 1405 return FALSE; 1406 } 1407 } 1408 #undef add_dynamic_entry 1409 1410 return TRUE; 1411 } 1412 1413 #define TP_OFFSET 0 1414 #define DTP_OFFSET 0x800 1415 1416 /* Return the relocation value for a TLS dtp-relative reloc. */ 1417 1418 static bfd_vma 1419 dtpoff (struct bfd_link_info *info, bfd_vma address) 1420 { 1421 /* If tls_sec is NULL, we should have signalled an error already. */ 1422 if (elf_hash_table (info)->tls_sec == NULL) 1423 return 0; 1424 return address - elf_hash_table (info)->tls_sec->vma - DTP_OFFSET; 1425 } 1426 1427 /* Return the relocation value for a static TLS tp-relative relocation. */ 1428 1429 static bfd_vma 1430 tpoff (struct bfd_link_info *info, bfd_vma address) 1431 { 1432 /* If tls_sec is NULL, we should have signalled an error already. */ 1433 if (elf_hash_table (info)->tls_sec == NULL) 1434 return 0; 1435 return address - elf_hash_table (info)->tls_sec->vma - TP_OFFSET; 1436 } 1437 1438 /* Return the global pointer's value, or 0 if it is not in use. */ 1439 1440 static bfd_vma 1441 riscv_global_pointer_value (struct bfd_link_info *info) 1442 { 1443 struct bfd_link_hash_entry *h; 1444 1445 h = bfd_link_hash_lookup (info->hash, "_gp", FALSE, FALSE, TRUE); 1446 if (h == NULL || h->type != bfd_link_hash_defined) 1447 return 0; 1448 1449 return h->u.def.value + sec_addr (h->u.def.section); 1450 } 1451 1452 /* Emplace a static relocation. */ 1453 1454 static bfd_reloc_status_type 1455 perform_relocation (const reloc_howto_type *howto, 1456 const Elf_Internal_Rela *rel, 1457 bfd_vma value, 1458 asection *input_section, 1459 bfd *input_bfd, 1460 bfd_byte *contents) 1461 { 1462 if (howto->pc_relative) 1463 value -= sec_addr (input_section) + rel->r_offset; 1464 value += rel->r_addend; 1465 1466 switch (ELFNN_R_TYPE (rel->r_info)) 1467 { 1468 case R_RISCV_HI20: 1469 case R_RISCV_TPREL_HI20: 1470 case R_RISCV_PCREL_HI20: 1471 case R_RISCV_GOT_HI20: 1472 case R_RISCV_TLS_GOT_HI20: 1473 case R_RISCV_TLS_GD_HI20: 1474 value = ENCODE_UTYPE_IMM (RISCV_CONST_HIGH_PART (value)); 1475 break; 1476 1477 case R_RISCV_LO12_I: 1478 case R_RISCV_TPREL_LO12_I: 1479 case R_RISCV_PCREL_LO12_I: 1480 value = ENCODE_ITYPE_IMM (value); 1481 break; 1482 1483 case R_RISCV_LO12_S: 1484 case R_RISCV_TPREL_LO12_S: 1485 case R_RISCV_PCREL_LO12_S: 1486 value = ENCODE_STYPE_IMM (value); 1487 break; 1488 1489 case R_RISCV_CALL: 1490 case R_RISCV_CALL_PLT: 1491 if (!VALID_UTYPE_IMM (RISCV_CONST_HIGH_PART (value))) 1492 return bfd_reloc_overflow; 1493 value = ENCODE_UTYPE_IMM (RISCV_CONST_HIGH_PART (value)) 1494 | (ENCODE_ITYPE_IMM (value) << 32); 1495 break; 1496 1497 case R_RISCV_JAL: 1498 if (!VALID_UJTYPE_IMM (value)) 1499 return bfd_reloc_overflow; 1500 value = ENCODE_UJTYPE_IMM (value); 1501 break; 1502 1503 case R_RISCV_BRANCH: 1504 if (!VALID_SBTYPE_IMM (value)) 1505 return bfd_reloc_overflow; 1506 value = ENCODE_SBTYPE_IMM (value); 1507 break; 1508 1509 case R_RISCV_32: 1510 case R_RISCV_64: 1511 case R_RISCV_ADD8: 1512 case R_RISCV_ADD16: 1513 case R_RISCV_ADD32: 1514 case R_RISCV_ADD64: 1515 case R_RISCV_SUB8: 1516 case R_RISCV_SUB16: 1517 case R_RISCV_SUB32: 1518 case R_RISCV_SUB64: 1519 case R_RISCV_TLS_DTPREL32: 1520 case R_RISCV_TLS_DTPREL64: 1521 break; 1522 1523 default: 1524 return bfd_reloc_notsupported; 1525 } 1526 1527 bfd_vma word = bfd_get (howto->bitsize, input_bfd, contents + rel->r_offset); 1528 word = (word & ~howto->dst_mask) | (value & howto->dst_mask); 1529 bfd_put (howto->bitsize, input_bfd, word, contents + rel->r_offset); 1530 1531 return bfd_reloc_ok; 1532 } 1533 1534 /* Remember all PC-relative high-part relocs we've encountered to help us 1535 later resolve the corresponding low-part relocs. */ 1536 1537 typedef struct { 1538 bfd_vma address; 1539 bfd_vma value; 1540 } riscv_pcrel_hi_reloc; 1541 1542 typedef struct riscv_pcrel_lo_reloc { 1543 asection *input_section; 1544 struct bfd_link_info *info; 1545 reloc_howto_type *howto; 1546 const Elf_Internal_Rela *reloc; 1547 bfd_vma addr; 1548 const char *name; 1549 bfd_byte *contents; 1550 struct riscv_pcrel_lo_reloc *next; 1551 } riscv_pcrel_lo_reloc; 1552 1553 typedef struct { 1554 htab_t hi_relocs; 1555 riscv_pcrel_lo_reloc *lo_relocs; 1556 } riscv_pcrel_relocs; 1557 1558 static hashval_t 1559 riscv_pcrel_reloc_hash (const void *entry) 1560 { 1561 const riscv_pcrel_hi_reloc *e = entry; 1562 return (hashval_t)(e->address >> 2); 1563 } 1564 1565 static bfd_boolean 1566 riscv_pcrel_reloc_eq (const void *entry1, const void *entry2) 1567 { 1568 const riscv_pcrel_hi_reloc *e1 = entry1, *e2 = entry2; 1569 return e1->address == e2->address; 1570 } 1571 1572 static bfd_boolean 1573 riscv_init_pcrel_relocs (riscv_pcrel_relocs *p) 1574 { 1575 1576 p->lo_relocs = NULL; 1577 p->hi_relocs = htab_create (1024, riscv_pcrel_reloc_hash, 1578 riscv_pcrel_reloc_eq, free); 1579 return p->hi_relocs != NULL; 1580 } 1581 1582 static void 1583 riscv_free_pcrel_relocs (riscv_pcrel_relocs *p) 1584 { 1585 riscv_pcrel_lo_reloc *cur = p->lo_relocs; 1586 while (cur != NULL) 1587 { 1588 riscv_pcrel_lo_reloc *next = cur->next; 1589 free (cur); 1590 cur = next; 1591 } 1592 1593 htab_delete (p->hi_relocs); 1594 } 1595 1596 static bfd_boolean 1597 riscv_record_pcrel_hi_reloc (riscv_pcrel_relocs *p, bfd_vma addr, bfd_vma value) 1598 { 1599 riscv_pcrel_hi_reloc entry = {addr, value - addr}; 1600 riscv_pcrel_hi_reloc **slot = 1601 (riscv_pcrel_hi_reloc **) htab_find_slot (p->hi_relocs, &entry, INSERT); 1602 BFD_ASSERT (*slot == NULL); 1603 *slot = (riscv_pcrel_hi_reloc *) bfd_malloc (sizeof (riscv_pcrel_hi_reloc)); 1604 if (*slot == NULL) 1605 return FALSE; 1606 **slot = entry; 1607 return TRUE; 1608 } 1609 1610 static bfd_boolean 1611 riscv_record_pcrel_lo_reloc (riscv_pcrel_relocs *p, 1612 asection *input_section, 1613 struct bfd_link_info *info, 1614 reloc_howto_type *howto, 1615 const Elf_Internal_Rela *reloc, 1616 bfd_vma addr, 1617 const char *name, 1618 bfd_byte *contents) 1619 { 1620 riscv_pcrel_lo_reloc *entry; 1621 entry = (riscv_pcrel_lo_reloc *) bfd_malloc (sizeof (riscv_pcrel_lo_reloc)); 1622 if (entry == NULL) 1623 return FALSE; 1624 *entry = (riscv_pcrel_lo_reloc) {input_section, info, howto, reloc, addr, 1625 name, contents, p->lo_relocs}; 1626 p->lo_relocs = entry; 1627 return TRUE; 1628 } 1629 1630 static bfd_boolean 1631 riscv_resolve_pcrel_lo_relocs (riscv_pcrel_relocs *p) 1632 { 1633 riscv_pcrel_lo_reloc *r; 1634 for (r = p->lo_relocs; r != NULL; r = r->next) 1635 { 1636 bfd *input_bfd = r->input_section->owner; 1637 riscv_pcrel_hi_reloc search = {r->addr, 0}; 1638 riscv_pcrel_hi_reloc *entry = htab_find (p->hi_relocs, &search); 1639 if (entry == NULL) 1640 return ((*r->info->callbacks->reloc_overflow) 1641 (r->info, NULL, r->name, r->howto->name, (bfd_vma) 0, 1642 input_bfd, r->input_section, r->reloc->r_offset)); 1643 1644 perform_relocation (r->howto, r->reloc, entry->value, r->input_section, 1645 input_bfd, r->contents); 1646 } 1647 1648 return TRUE; 1649 } 1650 1651 /* Relocate a RISC-V ELF section. 1652 1653 The RELOCATE_SECTION function is called by the new ELF backend linker 1654 to handle the relocations for a section. 1655 1656 The relocs are always passed as Rela structures. 1657 1658 This function is responsible for adjusting the section contents as 1659 necessary, and (if generating a relocatable output file) adjusting 1660 the reloc addend as necessary. 1661 1662 This function does not have to worry about setting the reloc 1663 address or the reloc symbol index. 1664 1665 LOCAL_SYMS is a pointer to the swapped in local symbols. 1666 1667 LOCAL_SECTIONS is an array giving the section in the input file 1668 corresponding to the st_shndx field of each local symbol. 1669 1670 The global hash table entry for the global symbols can be found 1671 via elf_sym_hashes (input_bfd). 1672 1673 When generating relocatable output, this function must handle 1674 STB_LOCAL/STT_SECTION symbols specially. The output symbol is 1675 going to be the section symbol corresponding to the output 1676 section, which means that the addend must be adjusted 1677 accordingly. */ 1678 1679 static bfd_boolean 1680 riscv_elf_relocate_section (bfd *output_bfd, struct bfd_link_info *info, 1681 bfd *input_bfd, asection *input_section, 1682 bfd_byte *contents, Elf_Internal_Rela *relocs, 1683 Elf_Internal_Sym *local_syms, 1684 asection **local_sections) 1685 { 1686 Elf_Internal_Rela *rel; 1687 Elf_Internal_Rela *relend; 1688 riscv_pcrel_relocs pcrel_relocs; 1689 bfd_boolean ret = FALSE; 1690 asection *sreloc = elf_section_data (input_section)->sreloc; 1691 struct riscv_elf_link_hash_table *htab = riscv_elf_hash_table (info); 1692 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (input_bfd); 1693 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (input_bfd); 1694 bfd_vma *local_got_offsets = elf_local_got_offsets (input_bfd); 1695 1696 if (!riscv_init_pcrel_relocs (&pcrel_relocs)) 1697 return FALSE; 1698 1699 relend = relocs + input_section->reloc_count; 1700 for (rel = relocs; rel < relend; rel++) 1701 { 1702 unsigned long r_symndx; 1703 struct elf_link_hash_entry *h; 1704 Elf_Internal_Sym *sym; 1705 asection *sec; 1706 bfd_vma relocation; 1707 bfd_reloc_status_type r = bfd_reloc_ok; 1708 const char *name; 1709 bfd_vma off, ie_off; 1710 bfd_boolean unresolved_reloc, is_ie = FALSE; 1711 bfd_vma pc = sec_addr (input_section) + rel->r_offset; 1712 int r_type = ELFNN_R_TYPE (rel->r_info), tls_type; 1713 reloc_howto_type *howto = riscv_elf_rtype_to_howto (r_type); 1714 const char *msg = NULL; 1715 1716 if (r_type == R_RISCV_GNU_VTINHERIT || r_type == R_RISCV_GNU_VTENTRY) 1717 continue; 1718 1719 /* This is a final link. */ 1720 r_symndx = ELFNN_R_SYM (rel->r_info); 1721 h = NULL; 1722 sym = NULL; 1723 sec = NULL; 1724 unresolved_reloc = FALSE; 1725 if (r_symndx < symtab_hdr->sh_info) 1726 { 1727 sym = local_syms + r_symndx; 1728 sec = local_sections[r_symndx]; 1729 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel); 1730 } 1731 else 1732 { 1733 bfd_boolean warned; 1734 /* bfd_boolean ignored; */ 1735 1736 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel, 1737 r_symndx, symtab_hdr, sym_hashes, 1738 h, sec, relocation, 1739 unresolved_reloc, warned /*, ignored */); 1740 if (warned) 1741 { 1742 /* To avoid generating warning messages about truncated 1743 relocations, set the relocation's address to be the same as 1744 the start of this section. */ 1745 if (input_section->output_section != NULL) 1746 relocation = input_section->output_section->vma; 1747 else 1748 relocation = 0; 1749 } 1750 } 1751 1752 if (sec != NULL && discarded_section (sec)) 1753 RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section, 1754 rel, 1, relend, howto, 0, contents); 1755 1756 if (info->relocatable) 1757 continue; 1758 1759 if (h != NULL) 1760 name = h->root.root.string; 1761 else 1762 { 1763 name = (bfd_elf_string_from_elf_section 1764 (input_bfd, symtab_hdr->sh_link, sym->st_name)); 1765 if (name == NULL || *name == '\0') 1766 name = bfd_section_name (input_bfd, sec); 1767 } 1768 1769 switch (r_type) 1770 { 1771 case R_RISCV_NONE: 1772 case R_RISCV_TPREL_ADD: 1773 case R_RISCV_COPY: 1774 case R_RISCV_JUMP_SLOT: 1775 case R_RISCV_RELATIVE: 1776 /* These require nothing of us at all. */ 1777 continue; 1778 1779 case R_RISCV_BRANCH: 1780 case R_RISCV_HI20: 1781 /* These require no special handling beyond perform_relocation. */ 1782 break; 1783 1784 case R_RISCV_GOT_HI20: 1785 if (h != NULL) 1786 { 1787 bfd_boolean dyn; 1788 1789 off = h->got.offset; 1790 BFD_ASSERT (off != (bfd_vma) -1); 1791 dyn = elf_hash_table (info)->dynamic_sections_created; 1792 1793 if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h) 1794 || (info->shared 1795 && SYMBOL_REFERENCES_LOCAL (info, h))) 1796 { 1797 /* This is actually a static link, or it is a 1798 -Bsymbolic link and the symbol is defined 1799 locally, or the symbol was forced to be local 1800 because of a version file. We must initialize 1801 this entry in the global offset table. Since the 1802 offset must always be a multiple of the word size, 1803 we use the least significant bit to record whether 1804 we have initialized it already. 1805 1806 When doing a dynamic link, we create a .rela.got 1807 relocation entry to initialize the value. This 1808 is done in the finish_dynamic_symbol routine. */ 1809 if ((off & 1) != 0) 1810 off &= ~1; 1811 else 1812 { 1813 bfd_put_NN (output_bfd, relocation, 1814 htab->elf.sgot->contents + off); 1815 h->got.offset |= 1; 1816 } 1817 } 1818 else 1819 unresolved_reloc = FALSE; 1820 } 1821 else 1822 { 1823 BFD_ASSERT (local_got_offsets != NULL 1824 && local_got_offsets[r_symndx] != (bfd_vma) -1); 1825 1826 off = local_got_offsets[r_symndx]; 1827 1828 /* The offset must always be a multiple of 8 on 64-bit. 1829 We use the least significant bit to record 1830 whether we have already processed this entry. */ 1831 if ((off & 1) != 0) 1832 off &= ~1; 1833 else 1834 { 1835 if (info->shared) 1836 { 1837 asection *s; 1838 Elf_Internal_Rela outrel; 1839 1840 /* We need to generate a R_RISCV_RELATIVE reloc 1841 for the dynamic linker. */ 1842 s = htab->elf.srelgot; 1843 BFD_ASSERT (s != NULL); 1844 1845 outrel.r_offset = sec_addr (htab->elf.sgot) + off; 1846 outrel.r_info = 1847 ELFNN_R_INFO (0, R_RISCV_RELATIVE); 1848 outrel.r_addend = relocation; 1849 relocation = 0; 1850 riscv_elf_append_rela (output_bfd, s, &outrel); 1851 } 1852 1853 bfd_put_NN (output_bfd, relocation, 1854 htab->elf.sgot->contents + off); 1855 local_got_offsets[r_symndx] |= 1; 1856 } 1857 } 1858 relocation = sec_addr (htab->elf.sgot) + off; 1859 if (!riscv_record_pcrel_hi_reloc (&pcrel_relocs, pc, relocation)) 1860 r = bfd_reloc_overflow; 1861 break; 1862 1863 case R_RISCV_ADD8: 1864 case R_RISCV_ADD16: 1865 case R_RISCV_ADD32: 1866 case R_RISCV_ADD64: 1867 { 1868 bfd_vma old_value = bfd_get (howto->bitsize, input_bfd, 1869 contents + rel->r_offset); 1870 relocation = old_value + relocation; 1871 } 1872 break; 1873 1874 case R_RISCV_SUB8: 1875 case R_RISCV_SUB16: 1876 case R_RISCV_SUB32: 1877 case R_RISCV_SUB64: 1878 { 1879 bfd_vma old_value = bfd_get (howto->bitsize, input_bfd, 1880 contents + rel->r_offset); 1881 relocation = old_value - relocation; 1882 } 1883 break; 1884 1885 case R_RISCV_CALL_PLT: 1886 case R_RISCV_CALL: 1887 case R_RISCV_JAL: 1888 if (info->shared && h != NULL && h->plt.offset != MINUS_ONE) 1889 { 1890 /* Refer to the PLT entry. */ 1891 relocation = sec_addr (htab->elf.splt) + h->plt.offset; 1892 unresolved_reloc = FALSE; 1893 } 1894 break; 1895 1896 case R_RISCV_TPREL_HI20: 1897 relocation = tpoff (info, relocation); 1898 break; 1899 1900 case R_RISCV_TPREL_LO12_I: 1901 case R_RISCV_TPREL_LO12_S: 1902 relocation = tpoff (info, relocation); 1903 if (VALID_ITYPE_IMM (relocation + rel->r_addend)) 1904 { 1905 /* We can use tp as the base register. */ 1906 bfd_vma insn = bfd_get_32 (input_bfd, contents + rel->r_offset); 1907 insn &= ~(OP_MASK_RS1 << OP_SH_RS1); 1908 insn |= X_TP << OP_SH_RS1; 1909 bfd_put_32 (input_bfd, insn, contents + rel->r_offset); 1910 } 1911 break; 1912 1913 case R_RISCV_LO12_I: 1914 case R_RISCV_LO12_S: 1915 { 1916 bfd_vma gp = riscv_global_pointer_value (info); 1917 bfd_boolean x0_base = VALID_ITYPE_IMM (relocation + rel->r_addend); 1918 if (x0_base || VALID_ITYPE_IMM (relocation + rel->r_addend - gp)) 1919 { 1920 /* We can use x0 or gp as the base register. */ 1921 bfd_vma insn = bfd_get_32 (input_bfd, contents + rel->r_offset); 1922 insn &= ~(OP_MASK_RS1 << OP_SH_RS1); 1923 if (!x0_base) 1924 { 1925 rel->r_addend -= gp; 1926 insn |= X_GP << OP_SH_RS1; 1927 } 1928 bfd_put_32 (input_bfd, insn, contents + rel->r_offset); 1929 } 1930 break; 1931 } 1932 1933 case R_RISCV_PCREL_HI20: 1934 if (!riscv_record_pcrel_hi_reloc (&pcrel_relocs, pc, 1935 relocation + rel->r_addend)) 1936 r = bfd_reloc_overflow; 1937 break; 1938 1939 case R_RISCV_PCREL_LO12_I: 1940 case R_RISCV_PCREL_LO12_S: 1941 if (riscv_record_pcrel_lo_reloc (&pcrel_relocs, input_section, info, 1942 howto, rel, relocation, name, 1943 contents)) 1944 continue; 1945 r = bfd_reloc_overflow; 1946 break; 1947 1948 case R_RISCV_TLS_DTPREL32: 1949 case R_RISCV_TLS_DTPREL64: 1950 relocation = dtpoff (info, relocation); 1951 break; 1952 1953 case R_RISCV_32: 1954 case R_RISCV_64: 1955 if ((input_section->flags & SEC_ALLOC) == 0) 1956 break; 1957 1958 if ((info->shared 1959 && (h == NULL 1960 || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT 1961 || h->root.type != bfd_link_hash_undefweak) 1962 && (! howto->pc_relative 1963 || !SYMBOL_CALLS_LOCAL (info, h))) 1964 || (!info->shared 1965 && h != NULL 1966 && h->dynindx != -1 1967 && !h->non_got_ref 1968 && ((h->def_dynamic 1969 && !h->def_regular) 1970 || h->root.type == bfd_link_hash_undefweak 1971 || h->root.type == bfd_link_hash_undefined))) 1972 { 1973 Elf_Internal_Rela outrel; 1974 bfd_boolean skip_static_relocation, skip_dynamic_relocation; 1975 1976 /* When generating a shared object, these relocations 1977 are copied into the output file to be resolved at run 1978 time. */ 1979 1980 outrel.r_offset = 1981 _bfd_elf_section_offset (output_bfd, info, input_section, 1982 rel->r_offset); 1983 skip_static_relocation = outrel.r_offset != (bfd_vma) -2; 1984 skip_dynamic_relocation = outrel.r_offset >= (bfd_vma) -2; 1985 outrel.r_offset += sec_addr (input_section); 1986 1987 if (skip_dynamic_relocation) 1988 memset (&outrel, 0, sizeof outrel); 1989 else if (h != NULL && h->dynindx != -1 1990 && !(info->shared 1991 && SYMBOLIC_BIND (info, h) 1992 && h->def_regular)) 1993 { 1994 outrel.r_info = ELFNN_R_INFO (h->dynindx, r_type); 1995 outrel.r_addend = rel->r_addend; 1996 } 1997 else 1998 { 1999 outrel.r_info = ELFNN_R_INFO (0, R_RISCV_RELATIVE); 2000 outrel.r_addend = relocation + rel->r_addend; 2001 } 2002 2003 riscv_elf_append_rela (output_bfd, sreloc, &outrel); 2004 if (skip_static_relocation) 2005 continue; 2006 } 2007 break; 2008 2009 case R_RISCV_TLS_GOT_HI20: 2010 is_ie = TRUE; 2011 /* Fall through. */ 2012 2013 case R_RISCV_TLS_GD_HI20: 2014 if (h != NULL) 2015 { 2016 off = h->got.offset; 2017 h->got.offset |= 1; 2018 } 2019 else 2020 { 2021 off = local_got_offsets[r_symndx]; 2022 local_got_offsets[r_symndx] |= 1; 2023 } 2024 2025 tls_type = _bfd_riscv_elf_tls_type (input_bfd, h, r_symndx); 2026 BFD_ASSERT (tls_type & (GOT_TLS_IE | GOT_TLS_GD)); 2027 /* If this symbol is referenced by both GD and IE TLS, the IE 2028 reference's GOT slot follows the GD reference's slots. */ 2029 ie_off = 0; 2030 if ((tls_type & GOT_TLS_GD) && (tls_type & GOT_TLS_IE)) 2031 ie_off = 2 * GOT_ENTRY_SIZE; 2032 2033 if ((off & 1) != 0) 2034 off &= ~1; 2035 else 2036 { 2037 Elf_Internal_Rela outrel; 2038 int indx = 0; 2039 bfd_boolean need_relocs = FALSE; 2040 2041 if (htab->elf.srelgot == NULL) 2042 abort (); 2043 2044 if (h != NULL) 2045 { 2046 bfd_boolean dyn; 2047 dyn = htab->elf.dynamic_sections_created; 2048 2049 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h) 2050 && (!info->shared 2051 || !SYMBOL_REFERENCES_LOCAL (info, h))) 2052 { 2053 indx = h->dynindx; 2054 } 2055 } 2056 2057 /* The GOT entries have not been initialized yet. Do it 2058 now, and emit any relocations. */ 2059 if ((info->shared || indx != 0) 2060 && (h == NULL 2061 || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT 2062 || h->root.type != bfd_link_hash_undefweak)) 2063 need_relocs = TRUE; 2064 2065 if (tls_type & GOT_TLS_GD) 2066 { 2067 if (need_relocs) 2068 { 2069 outrel.r_offset = sec_addr (htab->elf.sgot) + off; 2070 outrel.r_addend = 0; 2071 outrel.r_info = ELFNN_R_INFO (indx, R_RISCV_TLS_DTPMODNN); 2072 bfd_put_NN (output_bfd, 0, 2073 htab->elf.sgot->contents + off); 2074 riscv_elf_append_rela (output_bfd, htab->elf.srelgot, &outrel); 2075 if (indx == 0) 2076 { 2077 BFD_ASSERT (! unresolved_reloc); 2078 bfd_put_NN (output_bfd, 2079 dtpoff (info, relocation), 2080 (htab->elf.sgot->contents + off + 2081 RISCV_ELF_WORD_BYTES)); 2082 } 2083 else 2084 { 2085 bfd_put_NN (output_bfd, 0, 2086 (htab->elf.sgot->contents + off + 2087 RISCV_ELF_WORD_BYTES)); 2088 outrel.r_info = ELFNN_R_INFO (indx, R_RISCV_TLS_DTPRELNN); 2089 outrel.r_offset += RISCV_ELF_WORD_BYTES; 2090 riscv_elf_append_rela (output_bfd, htab->elf.srelgot, &outrel); 2091 } 2092 } 2093 else 2094 { 2095 /* If we are not emitting relocations for a 2096 general dynamic reference, then we must be in a 2097 static link or an executable link with the 2098 symbol binding locally. Mark it as belonging 2099 to module 1, the executable. */ 2100 bfd_put_NN (output_bfd, 1, 2101 htab->elf.sgot->contents + off); 2102 bfd_put_NN (output_bfd, 2103 dtpoff (info, relocation), 2104 (htab->elf.sgot->contents + off + 2105 RISCV_ELF_WORD_BYTES)); 2106 } 2107 } 2108 2109 if (tls_type & GOT_TLS_IE) 2110 { 2111 if (need_relocs) 2112 { 2113 bfd_put_NN (output_bfd, 0, 2114 htab->elf.sgot->contents + off + ie_off); 2115 outrel.r_offset = sec_addr (htab->elf.sgot) 2116 + off + ie_off; 2117 outrel.r_addend = 0; 2118 if (indx == 0) 2119 outrel.r_addend = tpoff (info, relocation); 2120 outrel.r_info = ELFNN_R_INFO (indx, R_RISCV_TLS_TPRELNN); 2121 riscv_elf_append_rela (output_bfd, htab->elf.srelgot, &outrel); 2122 } 2123 else 2124 { 2125 bfd_put_NN (output_bfd, tpoff (info, relocation), 2126 htab->elf.sgot->contents + off + ie_off); 2127 } 2128 } 2129 } 2130 2131 BFD_ASSERT (off < (bfd_vma) -2); 2132 relocation = sec_addr (htab->elf.sgot) + off + (is_ie ? ie_off : 0); 2133 if (!riscv_record_pcrel_hi_reloc (&pcrel_relocs, pc, relocation)) 2134 r = bfd_reloc_overflow; 2135 unresolved_reloc = FALSE; 2136 break; 2137 2138 default: 2139 r = bfd_reloc_notsupported; 2140 } 2141 2142 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections 2143 because such sections are not SEC_ALLOC and thus ld.so will 2144 not process them. */ 2145 if (unresolved_reloc 2146 && !((input_section->flags & SEC_DEBUGGING) != 0 2147 && h->def_dynamic) 2148 && _bfd_elf_section_offset (output_bfd, info, input_section, 2149 rel->r_offset) != (bfd_vma) -1) 2150 { 2151 (*_bfd_error_handler) 2152 (_("%B(%A+0x%lx): unresolvable %s relocation against symbol `%s'"), 2153 input_bfd, 2154 input_section, 2155 (long) rel->r_offset, 2156 howto->name, 2157 h->root.root.string); 2158 continue; 2159 } 2160 2161 if (r == bfd_reloc_ok) 2162 r = perform_relocation (howto, rel, relocation, input_section, 2163 input_bfd, contents); 2164 2165 switch (r) 2166 { 2167 case bfd_reloc_ok: 2168 continue; 2169 2170 case bfd_reloc_overflow: 2171 r = info->callbacks->reloc_overflow 2172 (info, (h ? &h->root : NULL), name, howto->name, 2173 (bfd_vma) 0, input_bfd, input_section, rel->r_offset); 2174 break; 2175 2176 case bfd_reloc_undefined: 2177 r = info->callbacks->undefined_symbol 2178 (info, name, input_bfd, input_section, rel->r_offset, 2179 TRUE); 2180 break; 2181 2182 case bfd_reloc_outofrange: 2183 msg = _("internal error: out of range error"); 2184 break; 2185 2186 case bfd_reloc_notsupported: 2187 msg = _("internal error: unsupported relocation error"); 2188 break; 2189 2190 case bfd_reloc_dangerous: 2191 msg = _("internal error: dangerous relocation"); 2192 break; 2193 2194 default: 2195 msg = _("internal error: unknown error"); 2196 break; 2197 } 2198 2199 if (msg) 2200 r = info->callbacks->warning 2201 (info, msg, name, input_bfd, input_section, rel->r_offset); 2202 goto out; 2203 } 2204 2205 ret = riscv_resolve_pcrel_lo_relocs (&pcrel_relocs); 2206 out: 2207 riscv_free_pcrel_relocs (&pcrel_relocs); 2208 return ret; 2209 } 2210 2211 /* Finish up dynamic symbol handling. We set the contents of various 2212 dynamic sections here. */ 2213 2214 static bfd_boolean 2215 riscv_elf_finish_dynamic_symbol (bfd *output_bfd, 2216 struct bfd_link_info *info, 2217 struct elf_link_hash_entry *h, 2218 Elf_Internal_Sym *sym) 2219 { 2220 struct riscv_elf_link_hash_table *htab = riscv_elf_hash_table (info); 2221 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd); 2222 2223 if (h->plt.offset != (bfd_vma) -1) 2224 { 2225 /* We've decided to create a PLT entry for this symbol. */ 2226 bfd_byte *loc; 2227 bfd_vma i, header_address, plt_idx, got_address; 2228 uint32_t plt_entry[PLT_ENTRY_INSNS]; 2229 Elf_Internal_Rela rela; 2230 2231 BFD_ASSERT (h->dynindx != -1); 2232 2233 /* Calculate the address of the PLT header. */ 2234 header_address = sec_addr (htab->elf.splt); 2235 2236 /* Calculate the index of the entry. */ 2237 plt_idx = (h->plt.offset - PLT_HEADER_SIZE) / PLT_ENTRY_SIZE; 2238 2239 /* Calculate the address of the .got.plt entry. */ 2240 got_address = riscv_elf_got_plt_val (plt_idx, info); 2241 2242 /* Find out where the .plt entry should go. */ 2243 loc = htab->elf.splt->contents + h->plt.offset; 2244 2245 /* Fill in the PLT entry itself. */ 2246 riscv_make_plt_entry (got_address, header_address + h->plt.offset, 2247 plt_entry); 2248 for (i = 0; i < PLT_ENTRY_INSNS; i++) 2249 bfd_put_32 (output_bfd, plt_entry[i], loc + 4*i); 2250 2251 /* Fill in the initial value of the .got.plt entry. */ 2252 loc = htab->elf.sgotplt->contents 2253 + (got_address - sec_addr (htab->elf.sgotplt)); 2254 bfd_put_NN (output_bfd, sec_addr (htab->elf.splt), loc); 2255 2256 /* Fill in the entry in the .rela.plt section. */ 2257 rela.r_offset = got_address; 2258 rela.r_addend = 0; 2259 rela.r_info = ELFNN_R_INFO (h->dynindx, R_RISCV_JUMP_SLOT); 2260 2261 loc = htab->elf.srelplt->contents + plt_idx * sizeof (ElfNN_External_Rela); 2262 bed->s->swap_reloca_out (output_bfd, &rela, loc); 2263 2264 if (!h->def_regular) 2265 { 2266 /* Mark the symbol as undefined, rather than as defined in 2267 the .plt section. Leave the value alone. */ 2268 sym->st_shndx = SHN_UNDEF; 2269 /* If the symbol is weak, we do need to clear the value. 2270 Otherwise, the PLT entry would provide a definition for 2271 the symbol even if the symbol wasn't defined anywhere, 2272 and so the symbol would never be NULL. */ 2273 if (!h->ref_regular_nonweak) 2274 sym->st_value = 0; 2275 } 2276 } 2277 2278 if (h->got.offset != (bfd_vma) -1 2279 && !(riscv_elf_hash_entry(h)->tls_type & (GOT_TLS_GD | GOT_TLS_IE))) 2280 { 2281 asection *sgot; 2282 asection *srela; 2283 Elf_Internal_Rela rela; 2284 2285 /* This symbol has an entry in the GOT. Set it up. */ 2286 2287 sgot = htab->elf.sgot; 2288 srela = htab->elf.srelgot; 2289 BFD_ASSERT (sgot != NULL && srela != NULL); 2290 2291 rela.r_offset = sec_addr (sgot) + (h->got.offset &~ (bfd_vma) 1); 2292 2293 /* If this is a -Bsymbolic link, and the symbol is defined 2294 locally, we just want to emit a RELATIVE reloc. Likewise if 2295 the symbol was forced to be local because of a version file. 2296 The entry in the global offset table will already have been 2297 initialized in the relocate_section function. */ 2298 if (info->shared 2299 && (info->symbolic || h->dynindx == -1) 2300 && h->def_regular) 2301 { 2302 asection *sec = h->root.u.def.section; 2303 rela.r_info = ELFNN_R_INFO (0, R_RISCV_RELATIVE); 2304 rela.r_addend = (h->root.u.def.value 2305 + sec->output_section->vma 2306 + sec->output_offset); 2307 } 2308 else 2309 { 2310 BFD_ASSERT (h->dynindx != -1); 2311 rela.r_info = ELFNN_R_INFO (h->dynindx, R_RISCV_NN); 2312 rela.r_addend = 0; 2313 } 2314 2315 bfd_put_NN (output_bfd, 0, 2316 sgot->contents + (h->got.offset & ~(bfd_vma) 1)); 2317 riscv_elf_append_rela (output_bfd, srela, &rela); 2318 } 2319 2320 if (h->needs_copy) 2321 { 2322 Elf_Internal_Rela rela; 2323 2324 /* This symbols needs a copy reloc. Set it up. */ 2325 BFD_ASSERT (h->dynindx != -1); 2326 2327 rela.r_offset = sec_addr (h->root.u.def.section) + h->root.u.def.value; 2328 rela.r_info = ELFNN_R_INFO (h->dynindx, R_RISCV_COPY); 2329 rela.r_addend = 0; 2330 riscv_elf_append_rela (output_bfd, htab->srelbss, &rela); 2331 } 2332 2333 /* Mark some specially defined symbols as absolute. */ 2334 if (/*h == htab->elf.hdynamic*/ 2335 strcmp (h->root.root.string, "_DYNAMIC") == 0 2336 || (h == htab->elf.hgot || h == htab->elf.hplt)) 2337 sym->st_shndx = SHN_ABS; 2338 2339 return TRUE; 2340 } 2341 2342 /* Finish up the dynamic sections. */ 2343 2344 static bfd_boolean 2345 riscv_finish_dyn (bfd *output_bfd, struct bfd_link_info *info, 2346 bfd *dynobj, asection *sdyn) 2347 { 2348 struct riscv_elf_link_hash_table *htab = riscv_elf_hash_table (info); 2349 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd); 2350 size_t dynsize = bed->s->sizeof_dyn; 2351 bfd_byte *dyncon, *dynconend; 2352 2353 dynconend = sdyn->contents + sdyn->size; 2354 for (dyncon = sdyn->contents; dyncon < dynconend; dyncon += dynsize) 2355 { 2356 Elf_Internal_Dyn dyn; 2357 asection *s; 2358 2359 bed->s->swap_dyn_in (dynobj, dyncon, &dyn); 2360 2361 switch (dyn.d_tag) 2362 { 2363 case DT_PLTGOT: 2364 s = htab->elf.sgotplt; 2365 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset; 2366 break; 2367 case DT_JMPREL: 2368 s = htab->elf.srelplt; 2369 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset; 2370 break; 2371 case DT_PLTRELSZ: 2372 s = htab->elf.srelplt; 2373 dyn.d_un.d_val = s->size; 2374 break; 2375 default: 2376 continue; 2377 } 2378 2379 bed->s->swap_dyn_out (output_bfd, &dyn, dyncon); 2380 } 2381 return TRUE; 2382 } 2383 2384 static bfd_boolean 2385 riscv_elf_finish_dynamic_sections (bfd *output_bfd, 2386 struct bfd_link_info *info) 2387 { 2388 bfd *dynobj; 2389 asection *sdyn; 2390 struct riscv_elf_link_hash_table *htab; 2391 2392 htab = riscv_elf_hash_table (info); 2393 BFD_ASSERT (htab != NULL); 2394 dynobj = htab->elf.dynobj; 2395 2396 sdyn = bfd_get_linker_section (dynobj, ".dynamic"); 2397 2398 if (elf_hash_table (info)->dynamic_sections_created) 2399 { 2400 asection *splt; 2401 bfd_boolean ret; 2402 2403 splt = htab->elf.splt; 2404 BFD_ASSERT (splt != NULL && sdyn != NULL); 2405 2406 ret = riscv_finish_dyn (output_bfd, info, dynobj, sdyn); 2407 2408 if (ret != TRUE) 2409 return ret; 2410 2411 /* Fill in the head and tail entries in the procedure linkage table. */ 2412 if (splt->size > 0) 2413 { 2414 int i; 2415 uint32_t plt_header[PLT_HEADER_INSNS]; 2416 riscv_make_plt0_entry (sec_addr (htab->elf.sgotplt), 2417 sec_addr (splt), plt_header); 2418 2419 for (i = 0; i < PLT_HEADER_INSNS; i++) 2420 bfd_put_32 (output_bfd, plt_header[i], splt->contents + 4*i); 2421 } 2422 2423 elf_section_data (splt->output_section)->this_hdr.sh_entsize 2424 = PLT_ENTRY_SIZE; 2425 } 2426 2427 if (htab->elf.sgotplt) 2428 { 2429 if (bfd_is_abs_section (htab->elf.sgotplt->output_section)) 2430 { 2431 (*_bfd_error_handler) 2432 (_("discarded output section: `%A'"), htab->elf.sgotplt); 2433 return FALSE; 2434 } 2435 2436 if (htab->elf.sgotplt->size > 0) 2437 { 2438 /* Write the first two entries in .got.plt, needed for the dynamic 2439 linker. */ 2440 bfd_put_NN (output_bfd, (bfd_vma) -1, htab->elf.sgotplt->contents); 2441 bfd_put_NN (output_bfd, (bfd_vma) 0, 2442 htab->elf.sgotplt->contents + GOT_ENTRY_SIZE); 2443 } 2444 2445 elf_section_data (htab->elf.sgotplt->output_section)->this_hdr.sh_entsize = 2446 GOT_ENTRY_SIZE; 2447 } 2448 2449 if (htab->elf.sgot) 2450 { 2451 if (htab->elf.sgot->size > 0) 2452 { 2453 /* Set the first entry in the global offset table to the address of 2454 the dynamic section. */ 2455 bfd_vma val = sdyn ? sec_addr (sdyn) : 0; 2456 bfd_put_NN (output_bfd, val, htab->elf.sgot->contents); 2457 } 2458 2459 elf_section_data (htab->elf.sgot->output_section)->this_hdr.sh_entsize = 2460 GOT_ENTRY_SIZE; 2461 } 2462 2463 return TRUE; 2464 } 2465 2466 /* Return address for Ith PLT stub in section PLT, for relocation REL 2467 or (bfd_vma) -1 if it should not be included. */ 2468 2469 static bfd_vma 2470 riscv_elf_plt_sym_val (bfd_vma i, const asection *plt, 2471 const arelent *rel ATTRIBUTE_UNUSED) 2472 { 2473 return plt->vma + PLT_HEADER_SIZE + i * PLT_ENTRY_SIZE; 2474 } 2475 2476 static enum elf_reloc_type_class 2477 riscv_reloc_type_class (/*const struct bfd_link_info *info ATTRIBUTE_UNUSED, 2478 const asection *rel_sec ATTRIBUTE_UNUSED,*/ 2479 const Elf_Internal_Rela *rela) 2480 { 2481 switch (ELFNN_R_TYPE (rela->r_info)) 2482 { 2483 case R_RISCV_RELATIVE: 2484 return reloc_class_relative; 2485 case R_RISCV_JUMP_SLOT: 2486 return reloc_class_plt; 2487 case R_RISCV_COPY: 2488 return reloc_class_copy; 2489 default: 2490 return reloc_class_normal; 2491 } 2492 } 2493 2494 /* Return true if bfd machine EXTENSION is an extension of machine BASE. */ 2495 2496 static bfd_boolean 2497 riscv_mach_extends_p (unsigned long base, unsigned long extension) 2498 { 2499 return extension == base; 2500 } 2501 2502 /* Merge backend specific data from an object file to the output 2503 object file when linking. */ 2504 2505 static bfd_boolean 2506 _bfd_riscv_elf_merge_private_bfd_data (bfd *ibfd, bfd *obfd) 2507 { 2508 flagword old_flags; 2509 flagword new_flags; 2510 2511 if (!is_riscv_elf (ibfd) || !is_riscv_elf (obfd)) 2512 return TRUE; 2513 2514 if (strcmp (bfd_get_target (ibfd), bfd_get_target (obfd)) != 0) 2515 { 2516 (*_bfd_error_handler) 2517 (_("%B: ABI is incompatible with that of the selected emulation"), 2518 ibfd); 2519 return FALSE; 2520 } 2521 2522 if (!_bfd_elf_merge_object_attributes (ibfd, obfd)) 2523 return FALSE; 2524 2525 new_flags = elf_elfheader (ibfd)->e_flags; 2526 old_flags = elf_elfheader (obfd)->e_flags; 2527 2528 if (! elf_flags_init (obfd)) 2529 { 2530 elf_flags_init (obfd) = TRUE; 2531 elf_elfheader (obfd)->e_flags = new_flags; 2532 elf_elfheader (obfd)->e_ident[EI_CLASS] 2533 = elf_elfheader (ibfd)->e_ident[EI_CLASS]; 2534 2535 if (bfd_get_arch (obfd) == bfd_get_arch (ibfd) 2536 && (bfd_get_arch_info (obfd)->the_default 2537 || riscv_mach_extends_p (bfd_get_mach (obfd), 2538 bfd_get_mach (ibfd)))) 2539 { 2540 if (! bfd_set_arch_mach (obfd, bfd_get_arch (ibfd), 2541 bfd_get_mach (ibfd))) 2542 return FALSE; 2543 } 2544 2545 return TRUE; 2546 } 2547 2548 /* Check flag compatibility. */ 2549 2550 if (new_flags == old_flags) 2551 return TRUE; 2552 2553 /* Don't link RV32 and RV64. */ 2554 if (elf_elfheader (ibfd)->e_ident[EI_CLASS] 2555 != elf_elfheader (obfd)->e_ident[EI_CLASS]) 2556 { 2557 (*_bfd_error_handler) 2558 (_("%B: ELF class mismatch: can't link 32- and 64-bit modules"), ibfd); 2559 goto fail; 2560 } 2561 2562 /* Warn about any other mismatches. */ 2563 if (new_flags != old_flags) 2564 { 2565 if (!EF_IS_RISCV_EXT_Xcustom (new_flags) && 2566 !EF_IS_RISCV_EXT_Xcustom (old_flags)) 2567 { 2568 (*_bfd_error_handler) 2569 (_("%B: uses different e_flags (0x%lx) fields than previous modules (0x%lx)"), 2570 ibfd, (unsigned long) new_flags, 2571 (unsigned long) old_flags); 2572 goto fail; 2573 } 2574 else if (EF_IS_RISCV_EXT_Xcustom(new_flags)) 2575 EF_SET_RISCV_EXT (elf_elfheader (obfd)->e_flags, 2576 EF_GET_RISCV_EXT (old_flags)); 2577 } 2578 2579 return TRUE; 2580 2581 fail: 2582 bfd_set_error (bfd_error_bad_value); 2583 return FALSE; 2584 } 2585 2586 /* Delete some bytes from a section while relaxing. */ 2587 2588 static bfd_boolean 2589 riscv_relax_delete_bytes (bfd *abfd, asection *sec, bfd_vma addr, size_t count) 2590 { 2591 unsigned int i, symcount; 2592 bfd_vma toaddr = sec->size; 2593 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (abfd); 2594 Elf_Internal_Shdr *symtab_hdr = &elf_tdata (abfd)->symtab_hdr; 2595 unsigned int sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec); 2596 struct bfd_elf_section_data *data = elf_section_data (sec); 2597 bfd_byte *contents = data->this_hdr.contents; 2598 2599 /* Actually delete the bytes. */ 2600 sec->size -= count; 2601 memmove (contents + addr, contents + addr + count, toaddr - addr - count); 2602 2603 /* Adjust the location of all of the relocs. Note that we need not 2604 adjust the addends, since all PC-relative references must be against 2605 symbols, which we will adjust below. */ 2606 for (i = 0; i < sec->reloc_count; i++) 2607 if (data->relocs[i].r_offset > addr && data->relocs[i].r_offset < toaddr) 2608 data->relocs[i].r_offset -= count; 2609 2610 /* Adjust the local symbols defined in this section. */ 2611 for (i = 0; i < symtab_hdr->sh_info; i++) 2612 { 2613 Elf_Internal_Sym *sym = (Elf_Internal_Sym *) symtab_hdr->contents + i; 2614 if (sym->st_shndx == sec_shndx) 2615 { 2616 /* If the symbol is in the range of memory we just moved, we 2617 have to adjust its value. */ 2618 if (sym->st_value > addr && sym->st_value <= toaddr) 2619 sym->st_value -= count; 2620 2621 /* If the symbol *spans* the bytes we just deleted (i.e. its 2622 *end* is in the moved bytes but its *start* isn't), then we 2623 must adjust its size. */ 2624 if (sym->st_value <= addr 2625 && sym->st_value + sym->st_size > addr 2626 && sym->st_value + sym->st_size <= toaddr) 2627 sym->st_size -= count; 2628 } 2629 } 2630 2631 /* Now adjust the global symbols defined in this section. */ 2632 symcount = ((symtab_hdr->sh_size / sizeof(ElfNN_External_Sym)) 2633 - symtab_hdr->sh_info); 2634 2635 for (i = 0; i < symcount; i++) 2636 { 2637 struct elf_link_hash_entry *sym_hash = sym_hashes[i]; 2638 2639 if ((sym_hash->root.type == bfd_link_hash_defined 2640 || sym_hash->root.type == bfd_link_hash_defweak) 2641 && sym_hash->root.u.def.section == sec) 2642 { 2643 /* As above, adjust the value if needed. */ 2644 if (sym_hash->root.u.def.value > addr 2645 && sym_hash->root.u.def.value <= toaddr) 2646 sym_hash->root.u.def.value -= count; 2647 2648 /* As above, adjust the size if needed. */ 2649 if (sym_hash->root.u.def.value <= addr 2650 && sym_hash->root.u.def.value + sym_hash->size > addr 2651 && sym_hash->root.u.def.value + sym_hash->size <= toaddr) 2652 sym_hash->size -= count; 2653 } 2654 } 2655 2656 return TRUE; 2657 } 2658 2659 /* Relax AUIPC + JALR into JAL. */ 2660 2661 static bfd_boolean 2662 _bfd_riscv_relax_call (bfd *abfd, asection *sec, 2663 struct bfd_link_info *link_info, 2664 Elf_Internal_Rela *rel, 2665 bfd_vma symval, 2666 bfd_boolean *again) 2667 { 2668 bfd_byte *contents = elf_section_data (sec)->this_hdr.contents; 2669 bfd_signed_vma foff = symval - (sec_addr (sec) + rel->r_offset); 2670 bfd_boolean near_zero = (symval + RISCV_IMM_REACH/2) < RISCV_IMM_REACH; 2671 bfd_vma auipc, jalr; 2672 int r_type; 2673 2674 /* See if this function call can be shortened. */ 2675 if (!VALID_UJTYPE_IMM (foff) && !(!link_info->shared && near_zero)) 2676 return TRUE; 2677 2678 /* Shorten the function call. */ 2679 BFD_ASSERT (rel->r_offset + 8 <= sec->size); 2680 2681 auipc = bfd_get_32 (abfd, contents + rel->r_offset); 2682 jalr = bfd_get_32 (abfd, contents + rel->r_offset + 4); 2683 2684 if (VALID_UJTYPE_IMM (foff)) 2685 { 2686 /* Relax to JAL rd, addr. */ 2687 r_type = R_RISCV_JAL; 2688 auipc = (jalr & (OP_MASK_RD << OP_SH_RD)) | MATCH_JAL; 2689 } 2690 else /* near_zero */ 2691 { 2692 /* Relax to JALR rd, x0, addr. */ 2693 r_type = R_RISCV_LO12_I; 2694 auipc = (jalr & (OP_MASK_RD << OP_SH_RD)) | MATCH_JALR; 2695 } 2696 2697 /* Replace the R_RISCV_CALL reloc. */ 2698 rel->r_info = ELFNN_R_INFO (ELFNN_R_SYM (rel->r_info), r_type); 2699 /* Replace the AUIPC. */ 2700 bfd_put_32 (abfd, auipc, contents + rel->r_offset); 2701 2702 /* Delete unnecessary JALR. */ 2703 *again = TRUE; 2704 return riscv_relax_delete_bytes (abfd, sec, rel->r_offset + 4, 4); 2705 } 2706 2707 /* Relax non-PIC global variable references. */ 2708 2709 static bfd_boolean 2710 _bfd_riscv_relax_lui (bfd *abfd, asection *sec, 2711 struct bfd_link_info *link_info, 2712 Elf_Internal_Rela *rel, 2713 bfd_vma symval, 2714 bfd_boolean *again) 2715 { 2716 bfd_vma gp = riscv_global_pointer_value (link_info); 2717 2718 /* Bail out if this symbol isn't in range of either gp or x0. */ 2719 if (!VALID_ITYPE_IMM (symval - gp) && !(symval < RISCV_IMM_REACH/2)) 2720 return TRUE; 2721 2722 /* We can delete the unnecessary AUIPC. The corresponding LO12 reloc 2723 will be converted to GPREL during relocation. */ 2724 BFD_ASSERT (rel->r_offset + 4 <= sec->size); 2725 rel->r_info = ELFNN_R_INFO (0, R_RISCV_NONE); 2726 2727 *again = TRUE; 2728 return riscv_relax_delete_bytes (abfd, sec, rel->r_offset, 4); 2729 } 2730 2731 /* Relax non-PIC TLS references. */ 2732 2733 static bfd_boolean 2734 _bfd_riscv_relax_tls_le (bfd *abfd, asection *sec, 2735 struct bfd_link_info *link_info, 2736 Elf_Internal_Rela *rel, 2737 bfd_vma symval, 2738 bfd_boolean *again) 2739 { 2740 /* See if this symbol is in range of tp. */ 2741 if (RISCV_CONST_HIGH_PART (tpoff (link_info, symval)) != 0) 2742 return TRUE; 2743 2744 /* We can delete the unnecessary LUI and tp add. The LO12 reloc will be 2745 made directly tp-relative. */ 2746 BFD_ASSERT (rel->r_offset + 4 <= sec->size); 2747 rel->r_info = ELFNN_R_INFO (0, R_RISCV_NONE); 2748 2749 *again = TRUE; 2750 return riscv_relax_delete_bytes (abfd, sec, rel->r_offset, 4); 2751 } 2752 2753 /* Implement R_RISCV_ALIGN by deleting excess alignment NOPs. */ 2754 2755 static bfd_boolean 2756 _bfd_riscv_relax_align (bfd *abfd, asection *sec, 2757 struct bfd_link_info *link_info ATTRIBUTE_UNUSED, 2758 Elf_Internal_Rela *rel, 2759 bfd_vma symval, 2760 bfd_boolean *again ATTRIBUTE_UNUSED) 2761 { 2762 bfd_vma alignment = 1; 2763 while (alignment <= rel->r_addend) 2764 alignment *= 2; 2765 2766 symval -= rel->r_addend; 2767 bfd_vma aligned_addr = ((symval - 1) & ~(alignment - 1)) + alignment; 2768 bfd_vma nop_bytes_needed = aligned_addr - symval; 2769 2770 /* Make sure there are enough NOPs to actually achieve the alignment. */ 2771 if (rel->r_addend < nop_bytes_needed) 2772 return FALSE; 2773 2774 /* Delete the reloc. */ 2775 rel->r_info = ELFNN_R_INFO (0, R_RISCV_NONE); 2776 2777 /* If the number of NOPs is already correct, there's nothing to do. */ 2778 if (nop_bytes_needed == rel->r_addend) 2779 return TRUE; 2780 2781 /* Delete the excess NOPs. */ 2782 return riscv_relax_delete_bytes (abfd, sec, rel->r_offset, 2783 rel->r_addend - nop_bytes_needed); 2784 } 2785 2786 /* Relax a section. Pass 0 shortens code sequences unless disabled. 2787 Pass 1, which cannot be disabled, handles code alignment directives. */ 2788 2789 static bfd_boolean 2790 _bfd_riscv_relax_section (bfd *abfd, asection *sec, 2791 struct bfd_link_info *info, bfd_boolean *again) 2792 { 2793 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (abfd); 2794 struct riscv_elf_link_hash_table *htab = riscv_elf_hash_table (info); 2795 struct bfd_elf_section_data *data = elf_section_data (sec); 2796 Elf_Internal_Rela *relocs; 2797 bfd_boolean ret = FALSE; 2798 unsigned int i; 2799 2800 *again = FALSE; 2801 2802 if (info->relocatable 2803 || (sec->flags & SEC_RELOC) == 0 2804 || sec->reloc_count == 0 2805 || (/*info->disable_target_specific_optimizations*/ 0 2806 && info->relax_pass == 0)) 2807 return TRUE; 2808 2809 /* Read this BFD's relocs if we haven't done so already. */ 2810 if (data->relocs) 2811 relocs = data->relocs; 2812 else if (!(relocs = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL, 2813 info->keep_memory))) 2814 goto fail; 2815 2816 /* Examine and consider relaxing each reloc. */ 2817 for (i = 0; i < sec->reloc_count; i++) 2818 { 2819 Elf_Internal_Rela *rel = data->relocs + i; 2820 typeof(&_bfd_riscv_relax_call) relax_func = NULL; 2821 int type = ELFNN_R_TYPE (rel->r_info); 2822 bfd_vma symval; 2823 2824 if (info->relax_pass == 0) 2825 { 2826 if (type == R_RISCV_CALL || type == R_RISCV_CALL_PLT) 2827 relax_func = _bfd_riscv_relax_call; 2828 else if (type == R_RISCV_HI20) 2829 relax_func = _bfd_riscv_relax_lui; 2830 else if (type == R_RISCV_TPREL_HI20 || type == R_RISCV_TPREL_ADD) 2831 relax_func = _bfd_riscv_relax_tls_le; 2832 } 2833 else if (type == R_RISCV_ALIGN) 2834 relax_func = _bfd_riscv_relax_align; 2835 2836 if (!relax_func) 2837 continue; 2838 2839 data->relocs = relocs; 2840 2841 /* Read this BFD's contents if we haven't done so already. */ 2842 if (!data->this_hdr.contents 2843 && !bfd_malloc_and_get_section (abfd, sec, &data->this_hdr.contents)) 2844 goto fail; 2845 2846 /* Read this BFD's symbols if we haven't done so already. */ 2847 if (symtab_hdr->sh_info != 0 2848 && !symtab_hdr->contents 2849 && !(symtab_hdr->contents = 2850 (unsigned char *) bfd_elf_get_elf_syms (abfd, symtab_hdr, 2851 symtab_hdr->sh_info, 2852 0, NULL, NULL, NULL))) 2853 goto fail; 2854 2855 /* Get the value of the symbol referred to by the reloc. */ 2856 if (ELFNN_R_SYM (rel->r_info) < symtab_hdr->sh_info) 2857 { 2858 /* A local symbol. */ 2859 Elf_Internal_Sym *isym = ((Elf_Internal_Sym *) symtab_hdr->contents 2860 + ELFNN_R_SYM (rel->r_info)); 2861 2862 if (isym->st_shndx == SHN_UNDEF) 2863 symval = sec_addr (sec) + rel->r_offset; 2864 else 2865 { 2866 asection *isec; 2867 BFD_ASSERT (isym->st_shndx < elf_numsections (abfd)); 2868 isec = elf_elfsections (abfd)[isym->st_shndx]->bfd_section; 2869 if (sec_addr (isec) == 0) 2870 continue; 2871 symval = sec_addr (isec) + isym->st_value; 2872 } 2873 } 2874 else 2875 { 2876 unsigned long indx; 2877 struct elf_link_hash_entry *h; 2878 2879 indx = ELFNN_R_SYM (rel->r_info) - symtab_hdr->sh_info; 2880 h = elf_sym_hashes (abfd)[indx]; 2881 2882 while (h->root.type == bfd_link_hash_indirect 2883 || h->root.type == bfd_link_hash_warning) 2884 h = (struct elf_link_hash_entry *) h->root.u.i.link; 2885 2886 if (h->plt.offset != MINUS_ONE) 2887 symval = sec_addr (htab->elf.splt) + h->plt.offset; 2888 else if (h->root.type == bfd_link_hash_undefweak) 2889 symval = 0; 2890 else if (h->root.u.def.section->output_section == NULL 2891 || (h->root.type != bfd_link_hash_defined 2892 && h->root.type != bfd_link_hash_defweak)) 2893 continue; 2894 else 2895 symval = sec_addr (h->root.u.def.section) + h->root.u.def.value; 2896 } 2897 2898 symval += rel->r_addend; 2899 2900 if (!relax_func (abfd, sec, info, rel, symval, again)) 2901 goto fail; 2902 } 2903 2904 ret = TRUE; 2905 2906 fail: 2907 if (relocs != data->relocs) 2908 free (relocs); 2909 2910 return ret; 2911 } 2912 2913 #define ELF_ARCH bfd_arch_riscv 2914 #define ELF_TARGET_ID RISCV_ELF_DATA 2915 #define ELF_MACHINE_CODE EM_RISCV 2916 #define ELF_MAXPAGESIZE 0x2000 2917 #define ELF_COMMONPAGESIZE 0x2000 2918 2919 #define TARGET_LITTLE_SYM bfd_elfNN_riscv_vec 2920 #define TARGET_LITTLE_NAME "elfNN-littleriscv" 2921 2922 #define elf_backend_reloc_type_class riscv_reloc_type_class 2923 2924 #define bfd_elfNN_bfd_reloc_name_lookup riscv_reloc_name_lookup 2925 #define bfd_elfNN_bfd_link_hash_table_create riscv_elf_link_hash_table_create 2926 #define bfd_elfNN_bfd_reloc_type_lookup riscv_reloc_type_lookup 2927 #define bfd_elfNN_bfd_merge_private_bfd_data \ 2928 _bfd_riscv_elf_merge_private_bfd_data 2929 2930 #define elf_backend_copy_indirect_symbol riscv_elf_copy_indirect_symbol 2931 #define elf_backend_create_dynamic_sections riscv_elf_create_dynamic_sections 2932 #define elf_backend_check_relocs riscv_elf_check_relocs 2933 #define elf_backend_adjust_dynamic_symbol riscv_elf_adjust_dynamic_symbol 2934 #define elf_backend_size_dynamic_sections riscv_elf_size_dynamic_sections 2935 #define elf_backend_relocate_section riscv_elf_relocate_section 2936 #define elf_backend_finish_dynamic_symbol riscv_elf_finish_dynamic_symbol 2937 #define elf_backend_finish_dynamic_sections riscv_elf_finish_dynamic_sections 2938 #define elf_backend_gc_mark_hook riscv_elf_gc_mark_hook 2939 #define elf_backend_gc_sweep_hook riscv_elf_gc_sweep_hook 2940 #define elf_backend_plt_sym_val riscv_elf_plt_sym_val 2941 #define elf_info_to_howto_rel NULL 2942 #define elf_info_to_howto riscv_info_to_howto_rela 2943 #define bfd_elfNN_bfd_relax_section _bfd_riscv_relax_section 2944 2945 #define elf_backend_init_index_section _bfd_elf_init_1_index_section 2946 2947 #define elf_backend_can_gc_sections 1 2948 #define elf_backend_can_refcount 1 2949 #define elf_backend_want_got_plt 1 2950 #define elf_backend_plt_readonly 1 2951 #define elf_backend_plt_alignment 4 2952 #define elf_backend_want_plt_sym 1 2953 #define elf_backend_got_header_size (ARCH_SIZE / 8) 2954 #define elf_backend_rela_normal 1 2955 #define elf_backend_default_execstack 0 2956 2957 #include "elfNN-target.h" 2958