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