Coverage Report

Created: 2025-06-24 06:45

/src/binutils-gdb/bfd/elf64-riscv.c
Line
Count
Source (jump to first uncovered line)
1
#line 1 "elfnn-riscv.c"
2
/* RISC-V-specific support for 64-bit ELF.
3
   Copyright (C) 2011-2025 Free Software Foundation, Inc.
4
5
   Contributed by Andrew Waterman (andrew@sifive.com).
6
   Based on TILE-Gx and MIPS targets.
7
8
   This file is part of BFD, the Binary File Descriptor library.
9
10
   This program is free software; you can redistribute it and/or modify
11
   it under the terms of the GNU General Public License as published by
12
   the Free Software Foundation; either version 3 of the License, or
13
   (at your option) any later version.
14
15
   This program is distributed in the hope that it will be useful,
16
   but WITHOUT ANY WARRANTY; without even the implied warranty of
17
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
   GNU General Public License for more details.
19
20
   You should have received a copy of the GNU General Public License
21
   along with this program; see the file COPYING3. If not,
22
   see <http://www.gnu.org/licenses/>.  */
23
24
/* This file handles RISC-V ELF targets.  */
25
26
#include "sysdep.h"
27
#include "bfd.h"
28
#include "libbfd.h"
29
#include "bfdlink.h"
30
#include "genlink.h"
31
#include "elf-bfd.h"
32
#include "elfxx-riscv.h"
33
#include "elf/riscv.h"
34
#include "opcode/riscv.h"
35
#include "objalloc.h"
36
37
#include <limits.h>
38
#ifndef CHAR_BIT
39
#define CHAR_BIT 8
40
#endif
41
42
/* True if dynamic relocation is needed.  If we are creating a shared library,
43
   and this is a reloc against a global symbol, or a non PC relative reloc
44
   against a local symbol, then we need to copy the reloc into the shared
45
   library.  However, if we are linking with -Bsymbolic, we do not need to
46
   copy a reloc against a global symbol which is defined in an object we are
47
   including in the link (i.e., DEF_REGULAR is set).
48
49
   At this point we have not seen all the input files, so it is possible that
50
   DEF_REGULAR is not set now but will be set later (it is never cleared).
51
   In case of a weak definition, DEF_REGULAR may be cleared later by a strong
52
   definition in a shared library.  We account for that possibility below by
53
   storing information in the relocs_copied field of the hash table entry.
54
   A similar situation occurs when creating shared libraries and symbol
55
   visibility changes render the symbol local.
56
57
   If on the other hand, we are creating an executable, we may need to keep
58
   relocations for symbols satisfied by a dynamic library if we manage to
59
   avoid copy relocs for the symbol.
60
61
   Generate dynamic pointer relocation against STT_GNU_IFUNC symbol in the
62
   non-code section (R_RISCV_32/R_RISCV_64).  */
63
#define RISCV_NEED_DYNAMIC_RELOC(PCREL, INFO, H, SEC) \
64
0
  ((bfd_link_pic (INFO) \
65
0
    && ((SEC)->flags & SEC_ALLOC) != 0 \
66
0
    && (!(PCREL) \
67
0
  || ((H) != NULL \
68
0
      && (!(INFO)->symbolic \
69
0
    || (H)->root.type == bfd_link_hash_defweak \
70
0
    || !(H)->def_regular)))) \
71
0
   || (!bfd_link_pic (INFO) \
72
0
       && ((SEC)->flags & SEC_ALLOC) != 0 \
73
0
       && (H) != NULL \
74
0
       && ((H)->root.type == bfd_link_hash_defweak \
75
0
     || !(H)->def_regular)) \
76
0
   || (!bfd_link_pic (INFO) \
77
0
       && (H) != NULL \
78
0
       && (H)->type == STT_GNU_IFUNC \
79
0
       && ((SEC)->flags & SEC_CODE) == 0))
80
81
/* True if dynamic relocation should be generated.  */
82
#define RISCV_GENERATE_DYNAMIC_RELOC(PCREL, INFO, H, RESOLVED_TO_ZERO) \
83
0
  ((bfd_link_pic (INFO) \
84
0
    && ((H) == NULL \
85
0
  || (ELF_ST_VISIBILITY ((H)->other) == STV_DEFAULT && !(RESOLVED_TO_ZERO)) \
86
0
  || (H)->root.type != bfd_link_hash_undefweak) \
87
0
    && (!(PCREL) \
88
0
  || !SYMBOL_CALLS_LOCAL ((INFO), (H)))) \
89
0
   || (!bfd_link_pic (INFO) \
90
0
       && (H) != NULL \
91
0
       && (H)->dynindx != -1 \
92
0
       && !(H)->non_got_ref \
93
0
       && (((H)->def_dynamic && !(H)->def_regular) \
94
0
     || (H)->root.type == bfd_link_hash_undefweak \
95
0
     || (H)->root.type == bfd_link_hash_undefined)))
96
97
/* True if this input relocation should be copied to output.  H->dynindx
98
   may be -1 if this symbol was marked to become local.  */
99
#define RISCV_COPY_INPUT_RELOC(INFO, H) \
100
0
  ((H) != NULL \
101
0
   && (H)->dynindx != -1 \
102
0
   && (!bfd_link_pic (INFO) \
103
0
       || !SYMBOLIC_BIND ((INFO), (H)) \
104
0
       || !(H)->def_regular))
105
106
/* True if this is actually a static link, or it is a -Bsymbolic link
107
   and the symbol is defined locally, or the symbol was forced to be
108
   local because of a version file.  */
109
#define RISCV_RESOLVED_LOCALLY(INFO, H) \
110
0
  (!WILL_CALL_FINISH_DYNAMIC_SYMBOL (elf_hash_table (INFO)->dynamic_sections_created, \
111
0
             bfd_link_pic (INFO), (H)) \
112
0
   || (bfd_link_pic (INFO) \
113
0
       && SYMBOL_REFERENCES_LOCAL ((INFO), (H))))
114
115
/* Set NEED_RELOC to true if TLS GD/IE needs dynamic relocations, and INDX will
116
   be the dynamic index.  PR22263, use the same check in allocate_dynrelocs and
117
   riscv_elf_relocate_section for TLS GD/IE.  */
118
#define RISCV_TLS_GD_IE_NEED_DYN_RELOC(INFO, DYN, H, INDX, NEED_RELOC) \
119
0
  do \
120
0
    { \
121
0
      if ((H) != NULL \
122
0
    && (H)->dynindx != -1 \
123
0
    && WILL_CALL_FINISH_DYNAMIC_SYMBOL ((DYN), bfd_link_pic (INFO), (H)) \
124
0
    && (bfd_link_dll (INFO) || !SYMBOL_REFERENCES_LOCAL ((INFO), (H)))) \
125
0
  (INDX) = (H)->dynindx; \
126
0
      if ((bfd_link_dll (INFO) || (INDX) != 0) \
127
0
    && ((H) == NULL \
128
0
        || ELF_ST_VISIBILITY ((H)->other) == STV_DEFAULT \
129
0
        || (H)->root.type != bfd_link_hash_undefweak)) \
130
0
  (NEED_RELOC) = true; \
131
0
    } \
132
0
  while (0)
133
134
0
#define ARCH_SIZE 64
135
136
0
#define MINUS_ONE ((bfd_vma)0 - 1)
137
138
0
#define RISCV_ELF_LOG_WORD_BYTES (ARCH_SIZE == 32 ? 2 : 3)
139
140
0
#define RISCV_ELF_WORD_BYTES (1 << RISCV_ELF_LOG_WORD_BYTES)
141
142
/* The name of the dynamic interpreter.  This is put in the .interp
143
   section.  */
144
145
0
#define ELF64_DYNAMIC_INTERPRETER "/lib/ld.so.1"
146
#define ELF32_DYNAMIC_INTERPRETER "/lib32/ld.so.1"
147
148
#define ELF_ARCH      bfd_arch_riscv
149
#define ELF_TARGET_ID     RISCV_ELF_DATA
150
#define ELF_MACHINE_CODE    EM_RISCV
151
0
#define ELF_MAXPAGESIZE     0x1000
152
0
#define ELF_COMMONPAGESIZE    0x1000
153
154
0
#define RISCV_ATTRIBUTES_SECTION_NAME ".riscv.attributes"
155
156
/* RISC-V ELF linker hash entry.  */
157
158
struct riscv_elf_link_hash_entry
159
{
160
  struct elf_link_hash_entry elf;
161
162
0
#define GOT_UNKNOWN 0
163
0
#define GOT_NORMAL  1
164
0
#define GOT_TLS_GD  2
165
0
#define GOT_TLS_IE  4
166
0
#define GOT_TLS_LE  8
167
0
#define GOT_TLSDESC 16
168
  char tls_type;
169
};
170
171
#define riscv_elf_hash_entry(ent) \
172
0
  ((struct riscv_elf_link_hash_entry *) (ent))
173
174
struct _bfd_riscv_elf_obj_tdata
175
{
176
  struct elf_obj_tdata root;
177
178
  /* tls_type for each local got entry.  */
179
  char *local_got_tls_type;
180
};
181
182
#define _bfd_riscv_elf_tdata(abfd) \
183
0
  ((struct _bfd_riscv_elf_obj_tdata *) (abfd)->tdata.any)
184
185
#define _bfd_riscv_elf_local_got_tls_type(abfd) \
186
0
  (_bfd_riscv_elf_tdata (abfd)->local_got_tls_type)
187
188
#define _bfd_riscv_elf_tls_type(abfd, h, symndx)    \
189
0
  (*((h) != NULL ? &riscv_elf_hash_entry (h)->tls_type    \
190
0
     : &_bfd_riscv_elf_local_got_tls_type (abfd) [symndx]))
191
192
#define is_riscv_elf(bfd)       \
193
0
  (bfd_get_flavour (bfd) == bfd_target_elf_flavour  \
194
0
   && elf_tdata (bfd) != NULL        \
195
0
   && elf_object_id (bfd) == RISCV_ELF_DATA)
196
197
static bool
198
elf64_riscv_mkobject (bfd *abfd)
199
286k
{
200
286k
  return bfd_elf_allocate_object (abfd,
201
286k
          sizeof (struct _bfd_riscv_elf_obj_tdata));
202
286k
}
203
204
#include "elf/common.h"
205
#include "elf/internal.h"
206
207
struct riscv_elf_link_hash_table
208
{
209
  struct elf_link_hash_table elf;
210
211
  /* Various options and other info passed from the linker.  */
212
  struct riscv_elf_params *params;
213
214
  /* Short-cuts to get to dynamic linker sections.  */
215
  asection *sdyntdata;
216
217
  /* The max alignment of output sections.  */
218
  bfd_vma max_alignment;
219
220
  /* The max alignment of output sections in [gp-2K, gp+2K) range.  */
221
  bfd_vma max_alignment_for_gp;
222
223
  /* Used by local STT_GNU_IFUNC symbols.  */
224
  htab_t loc_hash_table;
225
  void * loc_hash_memory;
226
227
  /* The index of the last unused .rel.iplt slot.  */
228
  bfd_vma last_iplt_index;
229
230
  /* The data segment phase, don't relax the section
231
     when it is exp_seg_relro_adjust.  */
232
  int *data_segment_phase;
233
234
  /* Relocations for variant CC symbols may be present.  */
235
  int variant_cc;
236
};
237
238
/* Instruction access functions. */
239
#define riscv_get_insn(bits, ptr)   \
240
0
  ((bits) == 16 ? bfd_getl16 (ptr)    \
241
0
   : (bits) == 32 ? bfd_getl32 (ptr)    \
242
0
   : (bits) == 64 ? bfd_getl64 (ptr)    \
243
0
   : (abort (), (bfd_vma) - 1))
244
#define riscv_put_insn(bits, val, ptr)    \
245
0
  ((bits) == 16 ? bfd_putl16 (val, ptr)    \
246
0
   : (bits) == 32 ? bfd_putl32 (val, ptr)  \
247
0
   : (bits) == 64 ? bfd_putl64 (val, ptr)  \
248
0
   : (abort (), (void) 0))
249
250
/* Get the RISC-V ELF linker hash table from a link_info structure.  */
251
#define riscv_elf_hash_table(p) \
252
0
  ((is_elf_hash_table ((p)->hash)          \
253
0
    && elf_hash_table_id (elf_hash_table (p)) == RISCV_ELF_DATA) \
254
0
   ? (struct riscv_elf_link_hash_table *) (p)->hash : NULL)
255
256
void
257
riscv_elf64_set_options (struct bfd_link_info *link_info,
258
       struct riscv_elf_params *params)
259
0
{
260
0
  riscv_elf_hash_table (link_info)->params = params;
261
0
}
262
263
static bool
264
riscv_info_to_howto_rela (bfd *abfd,
265
        arelent *cache_ptr,
266
        Elf_Internal_Rela *dst)
267
300
{
268
300
  cache_ptr->howto = riscv_elf_rtype_to_howto (abfd, ELF64_R_TYPE (dst->r_info));
269
300
  return cache_ptr->howto != NULL;
270
300
}
271
272
static void
273
riscv_elf_append_rela (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
274
0
{
275
0
  const struct elf_backend_data *bed;
276
0
  bfd_byte *loc;
277
278
0
  bed = get_elf_backend_data (abfd);
279
0
  loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rela);
280
0
  bed->s->swap_reloca_out (abfd, rel, loc);
281
0
}
282
283
/* Return true if a relocation is modifying an instruction. */
284
285
static bool
286
riscv_is_insn_reloc (const reloc_howto_type *howto)
287
0
{
288
  /* Heuristic: A multibyte destination with a nontrivial mask
289
     is an instruction */
290
0
  return (howto->bitsize > 8
291
0
    && howto->dst_mask != 0
292
0
    && ~(howto->dst_mask | (howto->bitsize < sizeof(bfd_vma) * CHAR_BIT
293
0
         ? (MINUS_ONE << howto->bitsize) : (bfd_vma)0)) != 0);
294
0
}
295
296
/* PLT/GOT stuff.  */
297
0
#define PLT_HEADER_INSNS 8
298
0
#define PLT_ENTRY_INSNS 4
299
0
#define PLT_HEADER_SIZE (PLT_HEADER_INSNS * 4)
300
0
#define PLT_ENTRY_SIZE (PLT_ENTRY_INSNS * 4)
301
0
#define GOT_ENTRY_SIZE RISCV_ELF_WORD_BYTES
302
0
#define TLS_GD_GOT_ENTRY_SIZE (RISCV_ELF_WORD_BYTES * 2)
303
0
#define TLS_IE_GOT_ENTRY_SIZE RISCV_ELF_WORD_BYTES
304
0
#define TLSDESC_GOT_ENTRY_SIZE (RISCV_ELF_WORD_BYTES * 2)
305
/* Reserve two entries of GOTPLT for ld.so, one is used for PLT resolver,
306
   the other is used for link map.  Other targets also reserve one more
307
   entry used for runtime profile?  */
308
0
#define GOTPLT_HEADER_SIZE (2 * GOT_ENTRY_SIZE)
309
310
0
#define sec_addr(sec) ((sec)->output_section->vma + (sec)->output_offset)
311
312
#if ARCH_SIZE == 32
313
# define MATCH_LREG MATCH_LW
314
#else
315
0
# define MATCH_LREG MATCH_LD
316
#endif
317
318
/* Generate a PLT header.  */
319
320
static bool
321
riscv_make_plt_header (bfd *output_bfd, bfd_vma gotplt_addr, bfd_vma addr,
322
           uint32_t *entry)
323
0
{
324
0
  bfd_vma gotplt_offset_high = RISCV_PCREL_HIGH_PART (gotplt_addr, addr);
325
0
  bfd_vma gotplt_offset_low = RISCV_PCREL_LOW_PART (gotplt_addr, addr);
326
327
  /* RVE has no t3 register, so this won't work, and is not supported.  */
328
0
  if (elf_elfheader (output_bfd)->e_flags & EF_RISCV_RVE)
329
0
    {
330
0
      _bfd_error_handler (_("%pB: warning: RVE PLT generation not supported"),
331
0
        output_bfd);
332
0
      return false;
333
0
    }
334
335
  /* auipc  t2, %hi(.got.plt)
336
     sub    t1, t1, t3         # shifted .got.plt offset + hdr size + 12
337
     l[w|d] t3, %lo(.got.plt)(t2)    # _dl_runtime_resolve
338
     addi   t1, t1, -(hdr size + 12) # shifted .got.plt offset
339
     addi   t0, t2, %lo(.got.plt)    # &.got.plt
340
     srli   t1, t1, log2(16/PTRSIZE) # .got.plt offset
341
     l[w|d] t0, PTRSIZE(t0)      # link map
342
     jr     t3  */
343
344
0
  entry[0] = RISCV_UTYPE (AUIPC, X_T2, gotplt_offset_high);
345
0
  entry[1] = RISCV_RTYPE (SUB, X_T1, X_T1, X_T3);
346
0
  entry[2] = RISCV_ITYPE (LREG, X_T3, X_T2, gotplt_offset_low);
347
0
  entry[3] = RISCV_ITYPE (ADDI, X_T1, X_T1, (uint32_t) -(PLT_HEADER_SIZE + 12));
348
0
  entry[4] = RISCV_ITYPE (ADDI, X_T0, X_T2, gotplt_offset_low);
349
0
  entry[5] = RISCV_ITYPE (SRLI, X_T1, X_T1, 4 - RISCV_ELF_LOG_WORD_BYTES);
350
0
  entry[6] = RISCV_ITYPE (LREG, X_T0, X_T0, RISCV_ELF_WORD_BYTES);
351
0
  entry[7] = RISCV_ITYPE (JALR, 0, X_T3, 0);
352
353
0
  return true;
354
0
}
355
356
/* Generate a PLT entry.  */
357
358
static bool
359
riscv_make_plt_entry (bfd *output_bfd, bfd_vma got, bfd_vma addr,
360
          uint32_t *entry)
361
0
{
362
  /* RVE has no t3 register, so this won't work, and is not supported.  */
363
0
  if (elf_elfheader (output_bfd)->e_flags & EF_RISCV_RVE)
364
0
    {
365
0
      _bfd_error_handler (_("%pB: warning: RVE PLT generation not supported"),
366
0
        output_bfd);
367
0
      return false;
368
0
    }
369
370
  /* auipc  t3, %hi(.got.plt entry)
371
     l[w|d] t3, %lo(.got.plt entry)(t3)
372
     jalr   t1, t3
373
     nop  */
374
375
0
  entry[0] = RISCV_UTYPE (AUIPC, X_T3, RISCV_PCREL_HIGH_PART (got, addr));
376
0
  entry[1] = RISCV_ITYPE (LREG,  X_T3, X_T3, RISCV_PCREL_LOW_PART (got, addr));
377
0
  entry[2] = RISCV_ITYPE (JALR, X_T1, X_T3, 0);
378
0
  entry[3] = RISCV_NOP;
379
380
0
  return true;
381
0
}
382
383
/* Create an entry in an RISC-V ELF linker hash table.  */
384
385
static struct bfd_hash_entry *
386
link_hash_newfunc (struct bfd_hash_entry *entry,
387
       struct bfd_hash_table *table, const char *string)
388
0
{
389
  /* Allocate the structure if it has not already been allocated by a
390
     subclass.  */
391
0
  if (entry == NULL)
392
0
    {
393
0
      entry =
394
0
  bfd_hash_allocate (table,
395
0
         sizeof (struct riscv_elf_link_hash_entry));
396
0
      if (entry == NULL)
397
0
  return entry;
398
0
    }
399
400
  /* Call the allocation method of the superclass.  */
401
0
  entry = _bfd_elf_link_hash_newfunc (entry, table, string);
402
0
  if (entry != NULL)
403
0
    {
404
0
      struct riscv_elf_link_hash_entry *eh;
405
406
0
      eh = (struct riscv_elf_link_hash_entry *) entry;
407
0
      eh->tls_type = GOT_UNKNOWN;
408
0
    }
409
410
0
  return entry;
411
0
}
412
413
/* Compute a hash of a local hash entry.  We use elf_link_hash_entry
414
   for local symbol so that we can handle local STT_GNU_IFUNC symbols
415
   as global symbol.  We reuse indx and dynstr_index for local symbol
416
   hash since they aren't used by global symbols in this backend.  */
417
418
static hashval_t
419
riscv_elf_local_htab_hash (const void *ptr)
420
0
{
421
0
  struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) ptr;
422
0
  return ELF_LOCAL_SYMBOL_HASH (h->indx, h->dynstr_index);
423
0
}
424
425
/* Compare local hash entries.  */
426
427
static int
428
riscv_elf_local_htab_eq (const void *ptr1, const void *ptr2)
429
0
{
430
0
  struct elf_link_hash_entry *h1 = (struct elf_link_hash_entry *) ptr1;
431
0
  struct elf_link_hash_entry *h2 = (struct elf_link_hash_entry *) ptr2;
432
433
0
  return h1->indx == h2->indx && h1->dynstr_index == h2->dynstr_index;
434
0
}
435
436
/* Find and/or create a hash entry for local symbol.  */
437
438
static struct elf_link_hash_entry *
439
riscv_elf_get_local_sym_hash (struct riscv_elf_link_hash_table *htab,
440
            bfd *abfd, const Elf_Internal_Rela *rel,
441
            bool create)
442
0
{
443
0
  struct riscv_elf_link_hash_entry eh, *ret;
444
0
  asection *sec = abfd->sections;
445
0
  hashval_t h = ELF_LOCAL_SYMBOL_HASH (sec->id,
446
0
               ELF64_R_SYM (rel->r_info));
447
0
  void **slot;
448
449
0
  eh.elf.indx = sec->id;
450
0
  eh.elf.dynstr_index = ELF64_R_SYM (rel->r_info);
451
0
  slot = htab_find_slot_with_hash (htab->loc_hash_table, &eh, h,
452
0
           create ? INSERT : NO_INSERT);
453
454
0
  if (!slot)
455
0
    return NULL;
456
457
0
  if (*slot)
458
0
    {
459
0
      ret = (struct riscv_elf_link_hash_entry *) *slot;
460
0
      return &ret->elf;
461
0
    }
462
463
0
  ret = (struct riscv_elf_link_hash_entry *)
464
0
  objalloc_alloc ((struct objalloc *) htab->loc_hash_memory,
465
0
      sizeof (struct riscv_elf_link_hash_entry));
466
0
  if (ret)
467
0
    {
468
0
      memset (ret, 0, sizeof (*ret));
469
0
      ret->elf.indx = sec->id;
470
0
      ret->elf.dynstr_index = ELF64_R_SYM (rel->r_info);
471
0
      ret->elf.dynindx = -1;
472
0
      *slot = ret;
473
0
    }
474
0
  return &ret->elf;
475
0
}
476
477
/* Destroy a RISC-V elf linker hash table.  */
478
479
static void
480
riscv_elf_link_hash_table_free (bfd *obfd)
481
0
{
482
0
  struct riscv_elf_link_hash_table *ret
483
0
    = (struct riscv_elf_link_hash_table *) obfd->link.hash;
484
485
0
  if (ret->loc_hash_table)
486
0
    htab_delete (ret->loc_hash_table);
487
0
  if (ret->loc_hash_memory)
488
0
    objalloc_free ((struct objalloc *) ret->loc_hash_memory);
489
490
0
  _bfd_elf_link_hash_table_free (obfd);
491
0
}
492
493
/* Create a RISC-V ELF linker hash table.  */
494
495
static struct bfd_link_hash_table *
496
riscv_elf_link_hash_table_create (bfd *abfd)
497
0
{
498
0
  struct riscv_elf_link_hash_table *ret;
499
0
  size_t amt = sizeof (struct riscv_elf_link_hash_table);
500
501
0
  ret = (struct riscv_elf_link_hash_table *) bfd_zmalloc (amt);
502
0
  if (ret == NULL)
503
0
    return NULL;
504
505
0
  if (!_bfd_elf_link_hash_table_init (&ret->elf, abfd, link_hash_newfunc,
506
0
              sizeof (struct riscv_elf_link_hash_entry)))
507
0
    {
508
0
      free (ret);
509
0
      return NULL;
510
0
    }
511
512
0
  ret->max_alignment = (bfd_vma) -1;
513
0
  ret->max_alignment_for_gp = (bfd_vma) -1;
514
515
  /* Create hash table for local ifunc.  */
516
0
  ret->loc_hash_table = htab_try_create (1024,
517
0
           riscv_elf_local_htab_hash,
518
0
           riscv_elf_local_htab_eq,
519
0
           NULL);
520
0
  ret->loc_hash_memory = objalloc_create ();
521
0
  if (!ret->loc_hash_table || !ret->loc_hash_memory)
522
0
    {
523
0
      riscv_elf_link_hash_table_free (abfd);
524
0
      return NULL;
525
0
    }
526
0
  ret->elf.root.hash_table_free = riscv_elf_link_hash_table_free;
527
528
0
  return &ret->elf.root;
529
0
}
530
531
/* Create the .got section.  */
532
533
static bool
534
riscv_elf_create_got_section (bfd *abfd, struct bfd_link_info *info)
535
0
{
536
0
  flagword flags;
537
0
  asection *s, *s_got;
538
0
  struct elf_link_hash_entry *h;
539
0
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
540
0
  struct elf_link_hash_table *htab = elf_hash_table (info);
541
542
  /* This function may be called more than once.  */
543
0
  if (htab->sgot != NULL)
544
0
    return true;
545
546
0
  flags = bed->dynamic_sec_flags;
547
548
0
  s = bfd_make_section_anyway_with_flags (abfd,
549
0
            (bed->rela_plts_and_copies_p
550
0
             ? ".rela.got" : ".rel.got"),
551
0
            (bed->dynamic_sec_flags
552
0
             | SEC_READONLY));
553
0
  if (s == NULL
554
0
      || !bfd_set_section_alignment (s, bed->s->log_file_align))
555
0
    return false;
556
0
  htab->srelgot = s;
557
558
0
  s = s_got = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
559
0
  if (s == NULL
560
0
      || !bfd_set_section_alignment (s, bed->s->log_file_align))
561
0
    return false;
562
0
  htab->sgot = s;
563
564
  /* The first bit of the global offset table is the header.  */
565
0
  s->size += bed->got_header_size;
566
567
0
  if (bed->want_got_plt)
568
0
    {
569
0
      s = bfd_make_section_anyway_with_flags (abfd, ".got.plt", flags);
570
0
      if (s == NULL
571
0
    || !bfd_set_section_alignment (s, bed->s->log_file_align))
572
0
  return false;
573
0
      htab->sgotplt = s;
574
575
      /* Reserve room for the header.  */
576
0
      s->size += GOTPLT_HEADER_SIZE;
577
0
    }
578
579
0
  if (bed->want_got_sym)
580
0
    {
581
      /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
582
   section.  We don't do this in the linker script because we don't want
583
   to define the symbol if we are not creating a global offset
584
   table.  */
585
0
      h = _bfd_elf_define_linkage_sym (abfd, info, s_got,
586
0
               "_GLOBAL_OFFSET_TABLE_");
587
0
      elf_hash_table (info)->hgot = h;
588
0
      if (h == NULL)
589
0
  return false;
590
0
    }
591
592
0
  return true;
593
0
}
594
595
/* Create .plt, .rela.plt, .got, .got.plt, .rela.got, .dynbss, and
596
   .rela.bss sections in DYNOBJ, and set up shortcuts to them in our
597
   hash table.  */
598
599
static bool
600
riscv_elf_create_dynamic_sections (bfd *dynobj,
601
           struct bfd_link_info *info)
602
0
{
603
0
  struct riscv_elf_link_hash_table *htab;
604
605
0
  htab = riscv_elf_hash_table (info);
606
0
  BFD_ASSERT (htab != NULL);
607
608
0
  if (!riscv_elf_create_got_section (dynobj, info))
609
0
    return false;
610
611
0
  if (!_bfd_elf_create_dynamic_sections (dynobj, info))
612
0
    return false;
613
614
0
  if (!bfd_link_pic (info))
615
0
    {
616
      /* Technically, this section doesn't have contents.  It is used as the
617
   target of TLS copy relocs, to copy TLS data from shared libraries into
618
   the executable.  However, if we don't mark it as loadable, then it
619
   matches the IS_TBSS test in ldlang.c, and there is no run-time address
620
   space allocated for it even though it has SEC_ALLOC.  That test is
621
   correct for .tbss, but not correct for this section.  There is also
622
   a second problem that having a section with no contents can only work
623
   if it comes after all sections with contents in the same segment,
624
   but the linker script does not guarantee that.  This is just mixed in
625
   with other .tdata.* sections.  We can fix both problems by lying and
626
   saying that there are contents.  This section is expected to be small
627
   so this should not cause a significant extra program startup cost.  */
628
0
      htab->sdyntdata =
629
0
  bfd_make_section_anyway_with_flags (dynobj, ".tdata.dyn",
630
0
              (SEC_ALLOC | SEC_THREAD_LOCAL
631
0
               | SEC_LOAD | SEC_DATA
632
0
               | SEC_HAS_CONTENTS
633
0
               | SEC_LINKER_CREATED));
634
0
    }
635
636
0
  if (!htab->elf.splt || !htab->elf.srelplt || !htab->elf.sdynbss
637
0
      || (!bfd_link_pic (info) && (!htab->elf.srelbss || !htab->sdyntdata)))
638
0
    abort ();
639
640
0
  return true;
641
0
}
642
643
/* Copy the extra info we tack onto an elf_link_hash_entry.  */
644
645
static void
646
riscv_elf_copy_indirect_symbol (struct bfd_link_info *info,
647
        struct elf_link_hash_entry *dir,
648
        struct elf_link_hash_entry *ind)
649
0
{
650
0
  struct riscv_elf_link_hash_entry *edir, *eind;
651
652
0
  edir = (struct riscv_elf_link_hash_entry *) dir;
653
0
  eind = (struct riscv_elf_link_hash_entry *) ind;
654
655
0
  if (ind->root.type == bfd_link_hash_indirect
656
0
      && dir->got.refcount <= 0)
657
0
    {
658
0
      edir->tls_type = eind->tls_type;
659
0
      eind->tls_type = GOT_UNKNOWN;
660
0
    }
661
0
  _bfd_elf_link_hash_copy_indirect (info, dir, ind);
662
0
}
663
664
static bool
665
riscv_elf_record_tls_type (bfd *abfd, struct elf_link_hash_entry *h,
666
         unsigned long symndx, char tls_type)
667
0
{
668
0
  char *new_tls_type = &_bfd_riscv_elf_tls_type (abfd, h, symndx);
669
670
0
  *new_tls_type |= tls_type;
671
0
  if ((*new_tls_type & GOT_NORMAL) && (*new_tls_type & ~GOT_NORMAL))
672
0
    {
673
0
      (*_bfd_error_handler)
674
0
  (_("%pB: `%s' accessed both as normal and thread local symbol"),
675
0
   abfd, h ? h->root.root.string : "<local>");
676
0
      return false;
677
0
    }
678
0
  return true;
679
0
}
680
681
static bool
682
riscv_elf_record_got_reference (bfd *abfd, struct bfd_link_info *info,
683
        struct elf_link_hash_entry *h, long symndx)
684
0
{
685
0
  struct riscv_elf_link_hash_table *htab = riscv_elf_hash_table (info);
686
0
  Elf_Internal_Shdr *symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
687
688
0
  if (htab->elf.sgot == NULL)
689
0
    {
690
0
      if (!riscv_elf_create_got_section (htab->elf.dynobj, info))
691
0
  return false;
692
0
    }
693
694
0
  if (h != NULL)
695
0
    {
696
0
      h->got.refcount += 1;
697
0
      return true;
698
0
    }
699
700
  /* This is a global offset table entry for a local symbol.  */
701
0
  if (elf_local_got_refcounts (abfd) == NULL)
702
0
    {
703
0
      bfd_size_type size = symtab_hdr->sh_info * (sizeof (bfd_vma) + 1);
704
0
      if (!(elf_local_got_refcounts (abfd) = bfd_zalloc (abfd, size)))
705
0
  return false;
706
0
      _bfd_riscv_elf_local_got_tls_type (abfd)
707
0
  = (char *) (elf_local_got_refcounts (abfd) + symtab_hdr->sh_info);
708
0
    }
709
0
  elf_local_got_refcounts (abfd) [symndx] += 1;
710
711
0
  return true;
712
0
}
713
714
static bool
715
bad_static_reloc (bfd *abfd, unsigned r_type, struct elf_link_hash_entry *h)
716
0
{
717
0
  reloc_howto_type * r = riscv_elf_rtype_to_howto (abfd, r_type);
718
719
  /* We propably can improve the information to tell users that they
720
     should be recompile the code with -fPIC or -fPIE, just like what
721
     x86 does.  */
722
0
  (*_bfd_error_handler)
723
0
    (_("%pB: relocation %s against `%s' can not be used when making a shared "
724
0
       "object; recompile with -fPIC"),
725
0
     abfd, r ? r->name : _("<unknown>"),
726
0
     h != NULL ? h->root.root.string : "a local symbol");
727
0
  bfd_set_error (bfd_error_bad_value);
728
0
  return false;
729
0
}
730
731
/* Look through the relocs for a section during the first phase, and
732
   allocate space in the global offset table or procedure linkage
733
   table.  */
734
735
static bool
736
riscv_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
737
      asection *sec, const Elf_Internal_Rela *relocs)
738
0
{
739
0
  struct riscv_elf_link_hash_table *htab;
740
0
  Elf_Internal_Shdr *symtab_hdr;
741
0
  struct elf_link_hash_entry **sym_hashes;
742
0
  const Elf_Internal_Rela *rel;
743
0
  asection *sreloc = NULL;
744
745
0
  if (bfd_link_relocatable (info))
746
0
    return true;
747
748
0
  htab = riscv_elf_hash_table (info);
749
0
  symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
750
0
  sym_hashes = elf_sym_hashes (abfd);
751
752
0
  if (htab->elf.dynobj == NULL)
753
0
    htab->elf.dynobj = abfd;
754
755
0
  for (rel = relocs; rel < relocs + sec->reloc_count; rel++)
756
0
    {
757
0
      unsigned int r_type;
758
0
      unsigned int r_symndx;
759
0
      struct elf_link_hash_entry *h;
760
0
      bool is_abs_symbol = false;
761
762
0
      r_symndx = ELF64_R_SYM (rel->r_info);
763
0
      r_type = ELF64_R_TYPE (rel->r_info);
764
765
0
      if (r_symndx >= NUM_SHDR_ENTRIES (symtab_hdr))
766
0
  {
767
0
    (*_bfd_error_handler) (_("%pB: bad symbol index: %d"),
768
0
         abfd, r_symndx);
769
0
    return false;
770
0
  }
771
772
0
      if (r_symndx < symtab_hdr->sh_info)
773
0
  {
774
    /* A local symbol.  */
775
0
    Elf_Internal_Sym *isym = bfd_sym_from_r_symndx (&htab->elf.sym_cache,
776
0
                abfd, r_symndx);
777
0
    if (isym == NULL)
778
0
      return false;
779
780
0
    is_abs_symbol = isym->st_shndx == SHN_ABS ? true : false;
781
782
    /* Check relocation against local STT_GNU_IFUNC symbol.  */
783
0
    if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
784
0
      {
785
0
        h = riscv_elf_get_local_sym_hash (htab, abfd, rel, true);
786
0
        if (h == NULL)
787
0
    return false;
788
789
        /* Fake STT_GNU_IFUNC global symbol.  */
790
0
        h->root.root.string = bfd_elf_sym_name (abfd, symtab_hdr,
791
0
                  isym, NULL);
792
0
        h->type = STT_GNU_IFUNC;
793
0
        h->def_regular = 1;
794
0
        h->ref_regular = 1;
795
0
        h->forced_local = 1;
796
0
        h->root.type = bfd_link_hash_defined;
797
0
      }
798
0
    else
799
0
      h = NULL;
800
0
  }
801
0
      else
802
0
  {
803
0
    h = sym_hashes[r_symndx - symtab_hdr->sh_info];
804
0
    while (h->root.type == bfd_link_hash_indirect
805
0
     || h->root.type == bfd_link_hash_warning)
806
0
      h = (struct elf_link_hash_entry *) h->root.u.i.link;
807
808
0
    is_abs_symbol = bfd_is_abs_symbol (&h->root) ? true : false;
809
0
  }
810
811
0
      if (h != NULL)
812
0
  {
813
0
    switch (r_type)
814
0
      {
815
0
      case R_RISCV_32:
816
0
      case R_RISCV_64:
817
0
      case R_RISCV_CALL:
818
0
      case R_RISCV_CALL_PLT:
819
0
      case R_RISCV_HI20:
820
0
      case R_RISCV_GOT_HI20:
821
0
      case R_RISCV_PCREL_HI20:
822
        /* Create the ifunc sections, iplt and ipltgot, for static
823
     executables.  */
824
0
        if (h->type == STT_GNU_IFUNC
825
0
      && !_bfd_elf_create_ifunc_sections (htab->elf.dynobj, info))
826
0
    return false;
827
0
        break;
828
829
0
      default:
830
0
        break;
831
0
      }
832
833
    /* It is referenced by a non-shared object.  */
834
0
    h->ref_regular = 1;
835
0
  }
836
837
0
      switch (r_type)
838
0
  {
839
0
  case R_RISCV_TLS_GD_HI20:
840
0
    if (!riscv_elf_record_got_reference (abfd, info, h, r_symndx)
841
0
        || !riscv_elf_record_tls_type (abfd, h, r_symndx, GOT_TLS_GD))
842
0
      return false;
843
0
    break;
844
845
0
  case R_RISCV_TLS_GOT_HI20:
846
0
    if (bfd_link_dll (info))
847
0
      info->flags |= DF_STATIC_TLS;
848
0
    if (!riscv_elf_record_got_reference (abfd, info, h, r_symndx)
849
0
        || !riscv_elf_record_tls_type (abfd, h, r_symndx, GOT_TLS_IE))
850
0
      return false;
851
0
    break;
852
853
0
  case R_RISCV_GOT_HI20:
854
0
    if (!riscv_elf_record_got_reference (abfd, info, h, r_symndx)
855
0
        || !riscv_elf_record_tls_type (abfd, h, r_symndx, GOT_NORMAL))
856
0
      return false;
857
0
    break;
858
859
0
  case R_RISCV_TLSDESC_HI20:
860
0
    if (!riscv_elf_record_got_reference (abfd, info, h, r_symndx)
861
0
        || !riscv_elf_record_tls_type (abfd, h, r_symndx, GOT_TLSDESC))
862
0
      return false;
863
0
    break;
864
865
0
  case R_RISCV_CALL:
866
0
  case R_RISCV_CALL_PLT:
867
    /* These symbol requires a procedure linkage table entry.
868
       We actually build the entry in adjust_dynamic_symbol,
869
       because these might be a case of linking PIC code without
870
       linking in any dynamic objects, in which case we don't
871
       need to generate a procedure linkage table after all.  */
872
873
    /* If it is a local symbol, then we resolve it directly
874
       without creating a PLT entry.  */
875
0
    if (h == NULL)
876
0
      continue;
877
878
0
    h->needs_plt = 1;
879
0
    h->plt.refcount += 1;
880
0
    break;
881
882
0
  case R_RISCV_PCREL_HI20:
883
0
    if (h != NULL
884
0
        && h->type == STT_GNU_IFUNC)
885
0
      {
886
0
        h->non_got_ref = 1;
887
0
        h->pointer_equality_needed = 1;
888
889
        /* We don't use the PCREL_HI20 in the data section,
890
     so we always need the plt when it refers to
891
     ifunc symbol.  */
892
0
        h->plt.refcount += 1;
893
0
      }
894
895
    /* The non-preemptible absolute symbol shouldn't be referneced with
896
       pc-relative relocation when generating shared object.  However,
897
       PCREL_HI20/LO12 relocs are always bind locally when generating
898
       shared object, so all absolute symbol referenced need to be
899
       disallowed, except they are defined in linker script.
900
901
       Maybe we should add this check for all pc-relative relocations,
902
       please see pr28789 and pr25749 for details.  */
903
0
    if (bfd_link_pic (info)
904
        /* (h == NULL || SYMBOL_REFERENCES_LOCAL (info, h))  */
905
0
        && is_abs_symbol)
906
0
      {
907
0
        if (h != NULL && (h)->root.ldscript_def)
908
    /* Disallow the absolute symbol defined in linker script here
909
       will cause the glibc-linux toolchain build failed, so regard
910
       them as pc-relative symbols, just like what x86 did.  */
911
0
    ;
912
0
        else
913
0
    {
914
0
      const char *name;
915
0
      if (h->root.root.string)
916
0
        name = h->root.root.string;
917
0
      else
918
0
        {
919
0
          Elf_Internal_Sym *sym;
920
0
          sym = bfd_sym_from_r_symndx (&htab->elf.sym_cache, abfd,
921
0
               r_symndx);
922
0
          name = bfd_elf_sym_name (abfd, symtab_hdr, sym, NULL);
923
0
        }
924
925
0
      reloc_howto_type *r_t =
926
0
      riscv_elf_rtype_to_howto (abfd, r_type);
927
0
      _bfd_error_handler
928
0
        (_("%pB: relocation %s against absolute symbol `%s' can "
929
0
           "not be used when making a shared object"),
930
0
         abfd, r_t ? r_t->name : _("<unknown>"), name);
931
0
      bfd_set_error (bfd_error_bad_value);
932
0
      return false;
933
0
    }
934
0
      }
935
    /* Fall through.  */
936
937
0
  case R_RISCV_JAL:
938
0
  case R_RISCV_BRANCH:
939
0
  case R_RISCV_RVC_BRANCH:
940
0
  case R_RISCV_RVC_JUMP:
941
    /* In shared libraries and pie, these relocs are known
942
       to bind locally.  */
943
0
    if (bfd_link_pic (info))
944
0
      break;
945
0
    goto static_reloc;
946
947
0
  case R_RISCV_TPREL_HI20:
948
    /* This is not allowed in the pic, but okay in pie.  */
949
0
    if (!bfd_link_executable (info))
950
0
      return bad_static_reloc (abfd, r_type, h);
951
0
    if (h != NULL)
952
0
      riscv_elf_record_tls_type (abfd, h, r_symndx, GOT_TLS_LE);
953
0
    break;
954
955
0
  case R_RISCV_HI20:
956
0
    if (bfd_link_pic (info))
957
0
      return bad_static_reloc (abfd, r_type, h);
958
0
    goto static_reloc;
959
960
0
  case R_RISCV_32:
961
0
    if (ARCH_SIZE > 32
962
0
        && bfd_link_pic (info)
963
0
        && (sec->flags & SEC_ALLOC) != 0)
964
0
      {
965
0
        if (is_abs_symbol)
966
0
    break;
967
968
0
        reloc_howto_type *r_t = riscv_elf_rtype_to_howto (abfd, r_type);
969
0
        _bfd_error_handler
970
0
    (_("%pB: relocation %s against non-absolute symbol `%s' can "
971
0
       "not be used in RV64 when making a shared object"),
972
0
     abfd, r_t ? r_t->name : _("<unknown>"),
973
0
     h != NULL ? h->root.root.string : "a local symbol");
974
0
        bfd_set_error (bfd_error_bad_value);
975
0
        return false;
976
0
      }
977
0
    goto static_reloc;
978
979
0
  case R_RISCV_COPY:
980
0
  case R_RISCV_JUMP_SLOT:
981
0
  case R_RISCV_RELATIVE:
982
0
  case R_RISCV_64:
983
    /* Fall through.  */
984
985
0
  static_reloc:
986
987
0
    if (h != NULL
988
0
        && (!bfd_link_pic (info)
989
0
      || h->type == STT_GNU_IFUNC))
990
0
      {
991
        /* This reloc might not bind locally.  */
992
0
        h->non_got_ref = 1;
993
0
        h->pointer_equality_needed = 1;
994
995
0
        if (!h->def_regular
996
0
      || (sec->flags & (SEC_CODE | SEC_READONLY)) != 0)
997
0
    {
998
      /* We may need a .plt entry if the symbol is a function
999
         defined in a shared lib or is a function referenced
1000
         from the code or read-only section.  */
1001
0
      h->plt.refcount += 1;
1002
0
    }
1003
0
      }
1004
1005
0
    reloc_howto_type *r = riscv_elf_rtype_to_howto (abfd, r_type);
1006
0
    if (RISCV_NEED_DYNAMIC_RELOC (r->pc_relative, info, h, sec))
1007
0
      {
1008
0
        struct elf_dyn_relocs *p;
1009
0
        struct elf_dyn_relocs **head;
1010
1011
        /* When creating a shared object, we must copy these
1012
     relocs into the output file.  We create a reloc
1013
     section in dynobj and make room for the reloc.  */
1014
0
        if (sreloc == NULL)
1015
0
    {
1016
0
      sreloc = _bfd_elf_make_dynamic_reloc_section
1017
0
        (sec, htab->elf.dynobj, RISCV_ELF_LOG_WORD_BYTES,
1018
0
        abfd, /*rela?*/ true);
1019
1020
0
      if (sreloc == NULL)
1021
0
        return false;
1022
0
    }
1023
1024
        /* If this is a global symbol, we count the number of
1025
     relocations we need for this symbol.  */
1026
0
        if (h != NULL)
1027
0
    head = &h->dyn_relocs;
1028
0
        else
1029
0
    {
1030
      /* Track dynamic relocs needed for local syms too.
1031
         We really need local syms available to do this
1032
         easily.  Oh well.  */
1033
1034
0
      asection *s;
1035
0
      void *vpp;
1036
0
      Elf_Internal_Sym *isym;
1037
1038
0
      isym = bfd_sym_from_r_symndx (&htab->elf.sym_cache,
1039
0
            abfd, r_symndx);
1040
0
      if (isym == NULL)
1041
0
        return false;
1042
1043
0
      s = bfd_section_from_elf_index (abfd, isym->st_shndx);
1044
0
      if (s == NULL)
1045
0
        s = sec;
1046
1047
0
      vpp = &elf_section_data (s)->local_dynrel;
1048
0
      head = (struct elf_dyn_relocs **) vpp;
1049
0
    }
1050
1051
0
        p = *head;
1052
0
        if (p == NULL || p->sec != sec)
1053
0
    {
1054
0
      size_t amt = sizeof *p;
1055
0
      p = ((struct elf_dyn_relocs *)
1056
0
           bfd_alloc (htab->elf.dynobj, amt));
1057
0
      if (p == NULL)
1058
0
        return false;
1059
0
      p->next = *head;
1060
0
      *head = p;
1061
0
      p->sec = sec;
1062
0
      p->count = 0;
1063
0
      p->pc_count = 0;
1064
0
    }
1065
1066
0
        p->count += 1;
1067
0
        p->pc_count += r == NULL ? 0 : r->pc_relative;
1068
0
      }
1069
1070
0
    break;
1071
1072
0
  default:
1073
0
    break;
1074
0
  }
1075
0
    }
1076
1077
0
  return true;
1078
0
}
1079
1080
/* Adjust a symbol defined by a dynamic object and referenced by a
1081
   regular object.  The current definition is in some section of the
1082
   dynamic object, but we're not including those sections.  We have to
1083
   change the definition to something the rest of the link can
1084
   understand.  */
1085
1086
static bool
1087
riscv_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
1088
         struct elf_link_hash_entry *h)
1089
0
{
1090
0
  struct riscv_elf_link_hash_table *htab;
1091
0
  struct riscv_elf_link_hash_entry * eh;
1092
0
  bfd *dynobj;
1093
0
  asection *s, *srel;
1094
1095
0
  htab = riscv_elf_hash_table (info);
1096
0
  BFD_ASSERT (htab != NULL);
1097
1098
0
  dynobj = htab->elf.dynobj;
1099
1100
  /* Make sure we know what is going on here.  */
1101
0
  BFD_ASSERT (dynobj != NULL
1102
0
        && (h->needs_plt
1103
0
      || h->type == STT_GNU_IFUNC
1104
0
      || h->is_weakalias
1105
0
      || (h->def_dynamic
1106
0
          && h->ref_regular
1107
0
          && !h->def_regular)));
1108
1109
  /* If this is a function, put it in the procedure linkage table.  We
1110
     will fill in the contents of the procedure linkage table later
1111
     (although we could actually do it here).  */
1112
0
  if (h->type == STT_FUNC || h->type == STT_GNU_IFUNC || h->needs_plt)
1113
0
    {
1114
0
      if (h->plt.refcount <= 0
1115
0
    || (h->type != STT_GNU_IFUNC
1116
0
        && (SYMBOL_CALLS_LOCAL (info, h)
1117
0
      || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
1118
0
          && h->root.type == bfd_link_hash_undefweak))))
1119
0
  {
1120
    /* This case can occur if we saw a R_RISCV_CALL_PLT reloc in an
1121
       input file, but the symbol was never referred to by a dynamic
1122
       object, or if all references were garbage collected.  In such
1123
       a case, we don't actually need to build a PLT entry.  */
1124
0
    h->plt.offset = (bfd_vma) -1;
1125
0
    h->needs_plt = 0;
1126
0
  }
1127
1128
0
      return true;
1129
0
    }
1130
0
  else
1131
0
    h->plt.offset = (bfd_vma) -1;
1132
1133
  /* If this is a weak symbol, and there is a real definition, the
1134
     processor independent code will have arranged for us to see the
1135
     real definition first, and we can just use the same value.  */
1136
0
  if (h->is_weakalias)
1137
0
    {
1138
0
      struct elf_link_hash_entry *def = weakdef (h);
1139
0
      BFD_ASSERT (def->root.type == bfd_link_hash_defined);
1140
0
      h->root.u.def.section = def->root.u.def.section;
1141
0
      h->root.u.def.value = def->root.u.def.value;
1142
0
      return true;
1143
0
    }
1144
1145
  /* This is a reference to a symbol defined by a dynamic object which
1146
     is not a function.  */
1147
1148
  /* If we are creating a shared library, we must presume that the
1149
     only references to the symbol are via the global offset table.
1150
     For such cases we need not do anything here; the relocations will
1151
     be handled correctly by relocate_section.  */
1152
0
  if (bfd_link_pic (info))
1153
0
    return true;
1154
1155
  /* If there are no references to this symbol that do not use the
1156
     GOT, we don't need to generate a copy reloc.  */
1157
0
  if (!h->non_got_ref)
1158
0
    return true;
1159
1160
  /* If -z nocopyreloc was given, we won't generate them either.  */
1161
0
  if (info->nocopyreloc)
1162
0
    {
1163
0
      h->non_got_ref = 0;
1164
0
      return true;
1165
0
    }
1166
1167
  /* If we don't find any dynamic relocs in read-only sections, then
1168
     we'll be keeping the dynamic relocs and avoiding the copy reloc.  */
1169
0
  if (!_bfd_elf_readonly_dynrelocs (h))
1170
0
    {
1171
0
      h->non_got_ref = 0;
1172
0
      return true;
1173
0
    }
1174
1175
  /* We must allocate the symbol in our .dynbss section, which will
1176
     become part of the .bss section of the executable.  There will be
1177
     an entry for this symbol in the .dynsym section.  The dynamic
1178
     object will contain position independent code, so all references
1179
     from the dynamic object to this symbol will go through the global
1180
     offset table.  The dynamic linker will use the .dynsym entry to
1181
     determine the address it must put in the global offset table, so
1182
     both the dynamic object and the regular object will refer to the
1183
     same memory location for the variable.  */
1184
1185
  /* We must generate a R_RISCV_COPY reloc to tell the dynamic linker
1186
     to copy the initial value out of the dynamic object and into the
1187
     runtime process image.  We need to remember the offset into the
1188
     .rel.bss section we are going to use.  */
1189
0
  eh = (struct riscv_elf_link_hash_entry *) h;
1190
0
  if (eh->tls_type & ~GOT_NORMAL)
1191
0
    {
1192
0
      s = htab->sdyntdata;
1193
0
      srel = htab->elf.srelbss;
1194
0
    }
1195
0
  else if ((h->root.u.def.section->flags & SEC_READONLY) != 0)
1196
0
    {
1197
0
      s = htab->elf.sdynrelro;
1198
0
      srel = htab->elf.sreldynrelro;
1199
0
    }
1200
0
  else
1201
0
    {
1202
0
      s = htab->elf.sdynbss;
1203
0
      srel = htab->elf.srelbss;
1204
0
    }
1205
0
  if ((h->root.u.def.section->flags & SEC_ALLOC) != 0 && h->size != 0)
1206
0
    {
1207
0
      srel->size += sizeof (Elf64_External_Rela);
1208
0
      h->needs_copy = 1;
1209
0
    }
1210
1211
0
  return _bfd_elf_adjust_dynamic_copy (info, h, s);
1212
0
}
1213
1214
/* Allocate space in .plt, .got and associated reloc sections for
1215
   dynamic relocs.  */
1216
1217
static bool
1218
allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
1219
0
{
1220
0
  struct bfd_link_info *info;
1221
0
  struct riscv_elf_link_hash_table *htab;
1222
0
  struct elf_dyn_relocs *p;
1223
1224
0
  if (h->root.type == bfd_link_hash_indirect)
1225
0
    return true;
1226
1227
0
  info = (struct bfd_link_info *) inf;
1228
0
  htab = riscv_elf_hash_table (info);
1229
0
  BFD_ASSERT (htab != NULL);
1230
1231
  /* When we are generating pde, make sure gp symbol is output as a
1232
     dynamic symbol.  Then ld.so can set the gp register earlier, before
1233
     resolving the ifunc.  */
1234
0
  if (!bfd_link_pic (info)
1235
0
      && htab->elf.dynamic_sections_created
1236
0
      && strcmp (h->root.root.string, RISCV_GP_SYMBOL) == 0
1237
0
      && !bfd_elf_link_record_dynamic_symbol (info, h))
1238
0
    return false;
1239
1240
  /* Since STT_GNU_IFUNC symbols must go through PLT, we handle them
1241
     in the allocate_ifunc_dynrelocs and allocate_local_ifunc_dynrelocs,
1242
     if they are defined and referenced in a non-shared object.  */
1243
0
  if (h->type == STT_GNU_IFUNC
1244
0
      && h->def_regular)
1245
0
    return true;
1246
0
  else if (htab->elf.dynamic_sections_created
1247
0
     && h->plt.refcount > 0)
1248
0
    {
1249
      /* Make sure this symbol is output as a dynamic symbol.
1250
   Undefined weak syms won't yet be marked as dynamic.  */
1251
0
      if (h->dynindx == -1
1252
0
    && !h->forced_local)
1253
0
  {
1254
0
    if (! bfd_elf_link_record_dynamic_symbol (info, h))
1255
0
      return false;
1256
0
  }
1257
1258
0
      if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, bfd_link_pic (info), h))
1259
0
  {
1260
0
    asection *s = htab->elf.splt;
1261
1262
0
    if (s->size == 0)
1263
0
      s->size = PLT_HEADER_SIZE;
1264
1265
0
    h->plt.offset = s->size;
1266
1267
    /* Make room for this entry.  */
1268
0
    s->size += PLT_ENTRY_SIZE;
1269
1270
    /* We also need to make an entry in the .got.plt section.  */
1271
0
    htab->elf.sgotplt->size += GOT_ENTRY_SIZE;
1272
1273
    /* We also need to make an entry in the .rela.plt section.  */
1274
0
    htab->elf.srelplt->size += sizeof (Elf64_External_Rela);
1275
1276
    /* If this symbol is not defined in a regular file, and we are
1277
       not generating a shared library, then set the symbol to this
1278
       location in the .plt.  This is required to make function
1279
       pointers compare as equal between the normal executable and
1280
       the shared library.  */
1281
0
    if (! bfd_link_pic (info)
1282
0
        && !h->def_regular)
1283
0
      {
1284
0
        h->root.u.def.section = s;
1285
0
        h->root.u.def.value = h->plt.offset;
1286
0
      }
1287
1288
    /* If the symbol has STO_RISCV_VARIANT_CC flag, then raise the
1289
       variant_cc flag of riscv_elf_link_hash_table.  */
1290
0
    if (h->other & STO_RISCV_VARIANT_CC)
1291
0
      htab->variant_cc = 1;
1292
0
  }
1293
0
      else
1294
0
  {
1295
0
    h->plt.offset = (bfd_vma) -1;
1296
0
    h->needs_plt = 0;
1297
0
  }
1298
0
    }
1299
0
  else
1300
0
    {
1301
0
      h->plt.offset = (bfd_vma) -1;
1302
0
      h->needs_plt = 0;
1303
0
    }
1304
1305
0
  if (h->got.refcount > 0)
1306
0
    {
1307
0
      asection *s;
1308
0
      bool dyn;
1309
0
      int tls_type = riscv_elf_hash_entry (h)->tls_type;
1310
1311
      /* Make sure this symbol is output as a dynamic symbol.
1312
   Undefined weak syms won't yet be marked as dynamic.  */
1313
0
      if (h->dynindx == -1
1314
0
    && !h->forced_local)
1315
0
  {
1316
0
    if (! bfd_elf_link_record_dynamic_symbol (info, h))
1317
0
      return false;
1318
0
  }
1319
1320
0
      s = htab->elf.sgot;
1321
0
      h->got.offset = s->size;
1322
0
      dyn = htab->elf.dynamic_sections_created;
1323
0
      if (tls_type & (GOT_TLS_GD | GOT_TLS_IE | GOT_TLSDESC))
1324
0
  {
1325
0
    int indx = 0;
1326
0
    bool need_reloc = false;
1327
0
    RISCV_TLS_GD_IE_NEED_DYN_RELOC(info, dyn, h, indx, need_reloc);
1328
1329
    /* TLS_GD needs two dynamic relocs and two GOT slots.  */
1330
0
    if (tls_type & GOT_TLS_GD)
1331
0
      {
1332
0
        s->size += TLS_GD_GOT_ENTRY_SIZE;
1333
0
        if (need_reloc)
1334
0
    htab->elf.srelgot->size += 2 * sizeof (Elf64_External_Rela);
1335
0
      }
1336
1337
    /* TLS_IE needs one dynamic reloc and one GOT slot.  */
1338
0
    if (tls_type & GOT_TLS_IE)
1339
0
      {
1340
0
        s->size += TLS_IE_GOT_ENTRY_SIZE;
1341
0
        if (need_reloc)
1342
0
    htab->elf.srelgot->size += sizeof (Elf64_External_Rela);
1343
0
      }
1344
1345
    /* TLSDESC needs one dynamic reloc and two GOT slots.  */
1346
0
    if (tls_type & GOT_TLSDESC)
1347
0
      {
1348
0
        s->size += TLSDESC_GOT_ENTRY_SIZE;
1349
        /* TLSDESC always use dynamic relocs.  */
1350
0
        htab->elf.srelgot->size += sizeof (Elf64_External_Rela);
1351
0
      }
1352
0
  }
1353
0
      else
1354
0
  {
1355
0
    s->size += GOT_ENTRY_SIZE;
1356
0
    if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, bfd_link_pic (info), h)
1357
0
        && ! UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
1358
0
      htab->elf.srelgot->size += sizeof (Elf64_External_Rela);
1359
0
  }
1360
0
    }
1361
0
  else
1362
0
    h->got.offset = (bfd_vma) -1;
1363
1364
0
  if (h->dyn_relocs == NULL)
1365
0
    return true;
1366
1367
  /* In the shared -Bsymbolic case, discard space allocated for
1368
     dynamic pc-relative relocs against symbols which turn out to be
1369
     defined in regular objects.  For the normal shared case, discard
1370
     space for pc-relative relocs that have become local due to symbol
1371
     visibility changes.  */
1372
1373
0
  if (bfd_link_pic (info))
1374
0
    {
1375
0
      if (SYMBOL_CALLS_LOCAL (info, h))
1376
0
  {
1377
0
    struct elf_dyn_relocs **pp;
1378
1379
0
    for (pp = &h->dyn_relocs; (p = *pp) != NULL; )
1380
0
      {
1381
0
        p->count -= p->pc_count;
1382
0
        p->pc_count = 0;
1383
0
        if (p->count == 0)
1384
0
    *pp = p->next;
1385
0
        else
1386
0
    pp = &p->next;
1387
0
      }
1388
0
  }
1389
1390
      /* Also discard relocs on undefined weak syms with non-default
1391
   visibility.  */
1392
0
      if (h->dyn_relocs != NULL
1393
0
    && h->root.type == bfd_link_hash_undefweak)
1394
0
  {
1395
0
    if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
1396
0
        || UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
1397
0
      h->dyn_relocs = NULL;
1398
1399
    /* Make sure undefined weak symbols are output as a dynamic
1400
       symbol in PIEs.  */
1401
0
    else if (h->dynindx == -1
1402
0
       && !h->forced_local)
1403
0
      {
1404
0
        if (! bfd_elf_link_record_dynamic_symbol (info, h))
1405
0
    return false;
1406
0
      }
1407
0
  }
1408
0
    }
1409
0
  else
1410
0
    {
1411
      /* For the non-shared case, discard space for relocs against
1412
   symbols which turn out to need copy relocs or are not
1413
   dynamic.  */
1414
1415
0
      if (!h->non_got_ref
1416
0
    && ((h->def_dynamic
1417
0
         && !h->def_regular)
1418
0
        || (htab->elf.dynamic_sections_created
1419
0
      && (h->root.type == bfd_link_hash_undefweak
1420
0
          || h->root.type == bfd_link_hash_undefined))))
1421
0
  {
1422
    /* Make sure this symbol is output as a dynamic symbol.
1423
       Undefined weak syms won't yet be marked as dynamic.  */
1424
0
    if (h->dynindx == -1
1425
0
        && !h->forced_local)
1426
0
      {
1427
0
        if (! bfd_elf_link_record_dynamic_symbol (info, h))
1428
0
    return false;
1429
0
      }
1430
1431
    /* If that succeeded, we know we'll be keeping all the
1432
       relocs.  */
1433
0
    if (h->dynindx != -1)
1434
0
      goto keep;
1435
0
  }
1436
1437
0
      h->dyn_relocs = NULL;
1438
1439
0
    keep: ;
1440
0
    }
1441
1442
  /* Finally, allocate space.  */
1443
0
  for (p = h->dyn_relocs; p != NULL; p = p->next)
1444
0
    {
1445
0
      asection *sreloc = elf_section_data (p->sec)->sreloc;
1446
0
      sreloc->size += p->count * sizeof (Elf64_External_Rela);
1447
0
    }
1448
1449
0
  return true;
1450
0
}
1451
1452
/* Allocate space in .plt, .got and associated reloc sections for
1453
   ifunc dynamic relocs.  */
1454
1455
static bool
1456
allocate_ifunc_dynrelocs (struct elf_link_hash_entry *h,
1457
        void *inf)
1458
0
{
1459
0
  struct bfd_link_info *info;
1460
1461
0
  if (h->root.type == bfd_link_hash_indirect)
1462
0
    return true;
1463
1464
0
  if (h->root.type == bfd_link_hash_warning)
1465
0
    h = (struct elf_link_hash_entry *) h->root.u.i.link;
1466
1467
0
  info = (struct bfd_link_info *) inf;
1468
1469
  /* Since STT_GNU_IFUNC symbol must go through PLT, we handle it
1470
     here if it is defined and referenced in a non-shared object.  */
1471
0
  if (h->type == STT_GNU_IFUNC
1472
0
      && h->def_regular)
1473
0
    return _bfd_elf_allocate_ifunc_dyn_relocs (info, h,
1474
0
                 &h->dyn_relocs,
1475
0
                 PLT_ENTRY_SIZE,
1476
0
                 PLT_HEADER_SIZE,
1477
0
                 GOT_ENTRY_SIZE,
1478
0
                 true);
1479
0
  return true;
1480
0
}
1481
1482
/* Allocate space in .plt, .got and associated reloc sections for
1483
   local ifunc dynamic relocs.  */
1484
1485
static int
1486
allocate_local_ifunc_dynrelocs (void **slot, void *inf)
1487
0
{
1488
0
  struct elf_link_hash_entry *h
1489
0
    = (struct elf_link_hash_entry *) *slot;
1490
1491
0
  if (h->type != STT_GNU_IFUNC
1492
0
      || !h->def_regular
1493
0
      || !h->ref_regular
1494
0
      || !h->forced_local
1495
0
      || h->root.type != bfd_link_hash_defined)
1496
0
    abort ();
1497
1498
0
  return allocate_ifunc_dynrelocs (h, inf);
1499
0
}
1500
1501
static bool
1502
riscv_elf_late_size_sections (bfd *output_bfd, struct bfd_link_info *info)
1503
0
{
1504
0
  struct riscv_elf_link_hash_table *htab;
1505
0
  bfd *dynobj;
1506
0
  asection *s;
1507
0
  bfd *ibfd;
1508
1509
0
  htab = riscv_elf_hash_table (info);
1510
0
  BFD_ASSERT (htab != NULL);
1511
0
  dynobj = htab->elf.dynobj;
1512
0
  if (dynobj == NULL)
1513
0
    return true;
1514
1515
0
  if (elf_hash_table (info)->dynamic_sections_created)
1516
0
    {
1517
      /* Set the contents of the .interp section to the interpreter.  */
1518
0
      if (bfd_link_executable (info) && !info->nointerp)
1519
0
  {
1520
0
    s = bfd_get_linker_section (dynobj, ".interp");
1521
0
    BFD_ASSERT (s != NULL);
1522
0
    s->size = strlen (ELF64_DYNAMIC_INTERPRETER) + 1;
1523
0
    s->contents = (unsigned char *) ELF64_DYNAMIC_INTERPRETER;
1524
0
    s->alloced = 1;
1525
0
  }
1526
0
    }
1527
1528
  /* Set up .got offsets for local syms, and space for local dynamic
1529
     relocs.  */
1530
0
  for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
1531
0
    {
1532
0
      bfd_signed_vma *local_got;
1533
0
      bfd_signed_vma *end_local_got;
1534
0
      char *local_tls_type;
1535
0
      bfd_size_type locsymcount;
1536
0
      Elf_Internal_Shdr *symtab_hdr;
1537
0
      asection *srel;
1538
1539
0
      if (! is_riscv_elf (ibfd))
1540
0
  continue;
1541
1542
0
      for (s = ibfd->sections; s != NULL; s = s->next)
1543
0
  {
1544
0
    struct elf_dyn_relocs *p;
1545
1546
0
    for (p = elf_section_data (s)->local_dynrel; p != NULL; p = p->next)
1547
0
      {
1548
0
        if (!bfd_is_abs_section (p->sec)
1549
0
      && bfd_is_abs_section (p->sec->output_section))
1550
0
    {
1551
      /* Input section has been discarded, either because
1552
         it is a copy of a linkonce section or due to
1553
         linker script /DISCARD/, so we'll be discarding
1554
         the relocs too.  */
1555
0
    }
1556
0
        else if (p->count != 0)
1557
0
    {
1558
0
      srel = elf_section_data (p->sec)->sreloc;
1559
0
      srel->size += p->count * sizeof (Elf64_External_Rela);
1560
0
      if ((p->sec->output_section->flags & SEC_READONLY) != 0)
1561
0
        info->flags |= DF_TEXTREL;
1562
0
    }
1563
0
      }
1564
0
  }
1565
1566
0
      local_got = elf_local_got_refcounts (ibfd);
1567
0
      if (!local_got)
1568
0
  continue;
1569
1570
0
      symtab_hdr = &elf_symtab_hdr (ibfd);
1571
0
      locsymcount = symtab_hdr->sh_info;
1572
0
      end_local_got = local_got + locsymcount;
1573
0
      local_tls_type = _bfd_riscv_elf_local_got_tls_type (ibfd);
1574
0
      s = htab->elf.sgot;
1575
0
      srel = htab->elf.srelgot;
1576
0
      for (; local_got < end_local_got; ++local_got, ++local_tls_type)
1577
0
  {
1578
0
    if (*local_got > 0)
1579
0
      {
1580
0
        *local_got = s->size;
1581
0
        if (*local_tls_type & (GOT_TLS_GD | GOT_TLS_IE | GOT_TLSDESC))
1582
0
    {
1583
0
      if (*local_tls_type & GOT_TLS_GD)
1584
0
        {
1585
0
          s->size += TLS_GD_GOT_ENTRY_SIZE;
1586
0
          if (bfd_link_dll (info))
1587
0
      srel->size += sizeof (Elf64_External_Rela);
1588
0
        }
1589
0
      if (*local_tls_type & GOT_TLS_IE)
1590
0
        {
1591
0
          s->size += TLS_IE_GOT_ENTRY_SIZE;
1592
0
          if (bfd_link_dll (info))
1593
0
      srel->size += sizeof (Elf64_External_Rela);
1594
0
        }
1595
0
      if (*local_tls_type & GOT_TLSDESC)
1596
0
        {
1597
0
          s->size += TLSDESC_GOT_ENTRY_SIZE;
1598
0
          srel->size += sizeof (Elf64_External_Rela);
1599
0
        }
1600
0
    }
1601
0
        else
1602
0
    {
1603
0
      s->size += GOT_ENTRY_SIZE;
1604
0
      if (bfd_link_pic (info))
1605
0
        srel->size += sizeof (Elf64_External_Rela);
1606
0
    }
1607
0
      }
1608
0
    else
1609
0
      *local_got = (bfd_vma) -1;
1610
0
  }
1611
0
    }
1612
1613
  /* Allocate .plt and .got entries and space dynamic relocs for
1614
     global symbols.  */
1615
0
  elf_link_hash_traverse (&htab->elf, allocate_dynrelocs, info);
1616
1617
  /* Allocate .plt and .got entries and space dynamic relocs for
1618
     global ifunc symbols.  */
1619
0
  elf_link_hash_traverse (&htab->elf, allocate_ifunc_dynrelocs, info);
1620
1621
  /* Allocate .plt and .got entries and space dynamic relocs for
1622
     local ifunc symbols.  */
1623
0
  htab_traverse (htab->loc_hash_table, allocate_local_ifunc_dynrelocs, info);
1624
1625
  /* Used to resolve the dynamic relocs overwite problems when
1626
     generating static executable.  */
1627
0
  if (htab->elf.irelplt)
1628
0
    htab->last_iplt_index = htab->elf.irelplt->reloc_count - 1;
1629
1630
0
  if (htab->elf.sgotplt)
1631
0
    {
1632
0
      struct elf_link_hash_entry *got;
1633
0
      got = elf_link_hash_lookup (elf_hash_table (info),
1634
0
          "_GLOBAL_OFFSET_TABLE_",
1635
0
          false, false, false);
1636
1637
      /* Don't allocate .got.plt section if there are no GOT nor PLT
1638
   entries and there is no refeence to _GLOBAL_OFFSET_TABLE_.  */
1639
0
      if ((got == NULL
1640
0
     || !got->ref_regular_nonweak)
1641
0
    && (htab->elf.sgotplt->size == GOTPLT_HEADER_SIZE)
1642
0
    && (htab->elf.splt == NULL
1643
0
        || htab->elf.splt->size == 0)
1644
0
    && (htab->elf.sgot == NULL
1645
0
        || (htab->elf.sgot->size
1646
0
      == get_elf_backend_data (output_bfd)->got_header_size)))
1647
0
  htab->elf.sgotplt->size = 0;
1648
0
    }
1649
1650
  /* The check_relocs and adjust_dynamic_symbol entry points have
1651
     determined the sizes of the various dynamic sections.  Allocate
1652
     memory for them.  */
1653
0
  for (s = dynobj->sections; s != NULL; s = s->next)
1654
0
    {
1655
0
      if ((s->flags & SEC_LINKER_CREATED) == 0)
1656
0
  continue;
1657
1658
0
      if (s == htab->elf.splt
1659
0
    || s == htab->elf.sgot
1660
0
    || s == htab->elf.sgotplt
1661
0
    || s == htab->elf.iplt
1662
0
    || s == htab->elf.igotplt
1663
0
    || s == htab->elf.sdynbss
1664
0
    || s == htab->elf.sdynrelro
1665
0
    || s == htab->sdyntdata)
1666
0
  {
1667
    /* Strip this section if we don't need it; see the
1668
       comment below.  */
1669
0
  }
1670
0
      else if (startswith (s->name, ".rela"))
1671
0
  {
1672
0
    if (s->size != 0)
1673
0
      {
1674
        /* We use the reloc_count field as a counter if we need
1675
     to copy relocs into the output file.  */
1676
0
        s->reloc_count = 0;
1677
0
      }
1678
0
  }
1679
0
      else
1680
0
  {
1681
    /* It's not one of our sections.  */
1682
0
    continue;
1683
0
  }
1684
1685
0
      if (s->size == 0)
1686
0
  {
1687
    /* If we don't need this section, strip it from the
1688
       output file.  This is mostly to handle .rela.bss and
1689
       .rela.plt.  We must create both sections in
1690
       create_dynamic_sections, because they must be created
1691
       before the linker maps input sections to output
1692
       sections.  The linker does that before
1693
       adjust_dynamic_symbol is called, and it is that
1694
       function which decides whether anything needs to go
1695
       into these sections.  */
1696
0
    s->flags |= SEC_EXCLUDE;
1697
0
    continue;
1698
0
  }
1699
1700
0
      if ((s->flags & SEC_HAS_CONTENTS) == 0)
1701
0
  continue;
1702
1703
      /* Allocate memory for the section contents.  Zero the memory
1704
   for the benefit of .rela.plt, which has 4 unused entries
1705
   at the beginning, and we don't want garbage.  */
1706
0
      s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->size);
1707
0
      if (s->contents == NULL)
1708
0
  return false;
1709
0
      s->alloced = 1;
1710
0
    }
1711
1712
  /* Add dynamic entries.  */
1713
0
  if (elf_hash_table (info)->dynamic_sections_created)
1714
0
    {
1715
0
      if (!_bfd_elf_add_dynamic_tags (output_bfd, info, true))
1716
0
  return false;
1717
1718
0
      if (htab->variant_cc
1719
0
    && !_bfd_elf_add_dynamic_entry (info, DT_RISCV_VARIANT_CC, 0))
1720
0
       return false;
1721
0
    }
1722
1723
0
  return true;
1724
0
}
1725
1726
0
#define TP_OFFSET 0
1727
0
#define DTP_OFFSET 0x800
1728
1729
/* Return the relocation value for a TLS dtp-relative reloc.  */
1730
1731
static bfd_vma
1732
dtpoff (struct bfd_link_info *info, bfd_vma address)
1733
0
{
1734
  /* If tls_sec is NULL, we should have signalled an error already.  */
1735
0
  if (elf_hash_table (info)->tls_sec == NULL)
1736
0
    return 0;
1737
0
  return address - elf_hash_table (info)->tls_sec->vma - DTP_OFFSET;
1738
0
}
1739
1740
/* Return the relocation value for a static TLS tp-relative relocation.  */
1741
1742
static bfd_vma
1743
tpoff (struct bfd_link_info *info, bfd_vma address)
1744
0
{
1745
  /* If tls_sec is NULL, we should have signalled an error already.  */
1746
0
  if (elf_hash_table (info)->tls_sec == NULL)
1747
0
    return 0;
1748
0
  return address - elf_hash_table (info)->tls_sec->vma - TP_OFFSET;
1749
0
}
1750
1751
/* Return the relocation value for a static TLSDESC relocation.  */
1752
1753
static bfd_vma
1754
tlsdescoff (struct bfd_link_info *info, bfd_vma address)
1755
0
{
1756
  /* If tls_sec is NULL, we should have signalled an error already.  */
1757
0
  if (elf_hash_table (info)->tls_sec == NULL)
1758
0
    return 0;
1759
0
  return address - elf_hash_table (info)->tls_sec->vma;
1760
0
}
1761
1762
/* Return the global pointer's value, or 0 if it is not in use.  */
1763
1764
static bfd_vma
1765
riscv_global_pointer_value (struct bfd_link_info *info)
1766
0
{
1767
0
  struct bfd_link_hash_entry *h;
1768
1769
0
  h = bfd_link_hash_lookup (info->hash, RISCV_GP_SYMBOL, false, false, true);
1770
0
  if (h == NULL || h->type != bfd_link_hash_defined)
1771
0
    return 0;
1772
1773
0
  return h->u.def.value + sec_addr (h->u.def.section);
1774
0
}
1775
1776
/* Emplace a static relocation.  */
1777
1778
static bfd_reloc_status_type
1779
perform_relocation (const reloc_howto_type *howto,
1780
        const Elf_Internal_Rela *rel,
1781
        bfd_vma value,
1782
        asection *input_section,
1783
        bfd *input_bfd,
1784
        bfd_byte *contents)
1785
0
{
1786
0
  if (howto->pc_relative)
1787
0
    value -= sec_addr (input_section) + rel->r_offset;
1788
1789
  /* PR31179, ignore the non-zero addend of R_RISCV_SUB_ULEB128.  */
1790
0
  if (ELF64_R_TYPE (rel->r_info) != R_RISCV_SUB_ULEB128)
1791
0
    value += rel->r_addend;
1792
1793
0
  switch (ELF64_R_TYPE (rel->r_info))
1794
0
    {
1795
0
    case R_RISCV_HI20:
1796
0
    case R_RISCV_TPREL_HI20:
1797
0
    case R_RISCV_PCREL_HI20:
1798
0
    case R_RISCV_GOT_HI20:
1799
0
    case R_RISCV_TLS_GOT_HI20:
1800
0
    case R_RISCV_TLS_GD_HI20:
1801
0
    case R_RISCV_TLSDESC_HI20:
1802
0
      if (ARCH_SIZE > 32 && !VALID_UTYPE_IMM (RISCV_CONST_HIGH_PART (value)))
1803
0
  return bfd_reloc_overflow;
1804
0
      value = ENCODE_UTYPE_IMM (RISCV_CONST_HIGH_PART (value));
1805
0
      break;
1806
1807
0
    case R_RISCV_LO12_I:
1808
0
    case R_RISCV_GPREL_I:
1809
0
    case R_RISCV_TPREL_LO12_I:
1810
0
    case R_RISCV_TPREL_I:
1811
0
    case R_RISCV_PCREL_LO12_I:
1812
0
    case R_RISCV_TLSDESC_LOAD_LO12:
1813
0
    case R_RISCV_TLSDESC_ADD_LO12:
1814
0
      value = ENCODE_ITYPE_IMM (value);
1815
0
      break;
1816
1817
0
    case R_RISCV_LO12_S:
1818
0
    case R_RISCV_GPREL_S:
1819
0
    case R_RISCV_TPREL_LO12_S:
1820
0
    case R_RISCV_TPREL_S:
1821
0
    case R_RISCV_PCREL_LO12_S:
1822
0
      value = ENCODE_STYPE_IMM (value);
1823
0
      break;
1824
1825
0
    case R_RISCV_CALL:
1826
0
    case R_RISCV_CALL_PLT:
1827
0
      if (ARCH_SIZE > 32 && !VALID_UTYPE_IMM (RISCV_CONST_HIGH_PART (value)))
1828
0
  return bfd_reloc_overflow;
1829
0
      value = ENCODE_UTYPE_IMM (RISCV_CONST_HIGH_PART (value))
1830
0
        | (ENCODE_ITYPE_IMM (value) << 32);
1831
0
      break;
1832
1833
0
    case R_RISCV_JAL:
1834
0
      if (!VALID_JTYPE_IMM (value))
1835
0
  return bfd_reloc_overflow;
1836
0
      value = ENCODE_JTYPE_IMM (value);
1837
0
      break;
1838
1839
0
    case R_RISCV_BRANCH:
1840
0
      if (!VALID_BTYPE_IMM (value))
1841
0
  return bfd_reloc_overflow;
1842
0
      value = ENCODE_BTYPE_IMM (value);
1843
0
      break;
1844
1845
0
    case R_RISCV_RVC_BRANCH:
1846
0
      if (!VALID_CBTYPE_IMM (value))
1847
0
  return bfd_reloc_overflow;
1848
0
      value = ENCODE_CBTYPE_IMM (value);
1849
0
      break;
1850
1851
0
    case R_RISCV_RVC_JUMP:
1852
0
      if (!VALID_CJTYPE_IMM (value))
1853
0
  return bfd_reloc_overflow;
1854
0
      value = ENCODE_CJTYPE_IMM (value);
1855
0
      break;
1856
1857
0
    case R_RISCV_RVC_LUI:
1858
0
      if (RISCV_CONST_HIGH_PART (value) == 0)
1859
0
  {
1860
    /* Linker relaxation can convert an address equal to or greater than
1861
       0x800 to slightly below 0x800.  C.LUI does not accept zero as a
1862
       valid immediate.  We can fix this by converting it to a C.LI.  */
1863
0
    bfd_vma insn = riscv_get_insn (howto->bitsize,
1864
0
           contents + rel->r_offset);
1865
0
    insn = (insn & ~MATCH_C_LUI) | MATCH_C_LI;
1866
0
    riscv_put_insn (howto->bitsize, insn, contents + rel->r_offset);
1867
0
    value = ENCODE_CITYPE_IMM (0);
1868
0
  }
1869
0
      else if (!VALID_CITYPE_LUI_IMM (RISCV_CONST_HIGH_PART (value)))
1870
0
  return bfd_reloc_overflow;
1871
0
      else
1872
0
  value = ENCODE_CITYPE_LUI_IMM (RISCV_CONST_HIGH_PART (value));
1873
0
      break;
1874
1875
    /* R_RISCV_SET_ULEB128 won't go into here.  */
1876
0
    case R_RISCV_SUB_ULEB128:
1877
0
      {
1878
0
  unsigned int len = 0;
1879
0
  _bfd_read_unsigned_leb128 (input_bfd, contents + rel->r_offset, &len);
1880
1881
  /* Clean the contents value to zero (0x80), but keep the original
1882
     length.  */
1883
0
  bfd_byte *p = contents + rel->r_offset;
1884
0
  bfd_byte *endp = p + len - 1;
1885
0
  memset (p, 0x80, len - 1);
1886
0
  *(endp) = 0;
1887
1888
  /* Make sure the length of the new uleb128 value within the
1889
     original (available) length.  */
1890
0
  unsigned int new_len = 0;
1891
0
  unsigned int val_t = value;
1892
0
  do
1893
0
    {
1894
0
      new_len++;
1895
0
      val_t >>= 7;
1896
0
    }
1897
0
  while (val_t);
1898
0
  if (new_len > len)
1899
0
    {
1900
0
      _bfd_error_handler
1901
0
        (_("final size of uleb128 value at offset 0x%lx in %pA from "
1902
0
     "%pB exceeds available space"),
1903
0
         (long) rel->r_offset, input_section, input_bfd);
1904
0
      return bfd_reloc_dangerous;
1905
0
    }
1906
0
  else
1907
0
    {
1908
0
      p = _bfd_write_unsigned_leb128 (p, endp, value);
1909
0
      BFD_ASSERT (p);
1910
1911
      /* If the length of the value is reduced and shorter than the
1912
         original uleb128 length, then _bfd_write_unsigned_leb128 may
1913
         clear the 0x80 to 0x0 for the last byte that was written.
1914
         So reset it to keep the the original uleb128 length.  */
1915
0
      if (--p < endp)
1916
0
        *p |= 0x80;
1917
0
    }
1918
0
  return bfd_reloc_ok;
1919
0
      }
1920
1921
0
    case R_RISCV_32:
1922
0
    case R_RISCV_64:
1923
0
    case R_RISCV_ADD8:
1924
0
    case R_RISCV_ADD16:
1925
0
    case R_RISCV_ADD32:
1926
0
    case R_RISCV_ADD64:
1927
0
    case R_RISCV_SUB6:
1928
0
    case R_RISCV_SUB8:
1929
0
    case R_RISCV_SUB16:
1930
0
    case R_RISCV_SUB32:
1931
0
    case R_RISCV_SUB64:
1932
0
    case R_RISCV_SET6:
1933
0
    case R_RISCV_SET8:
1934
0
    case R_RISCV_SET16:
1935
0
    case R_RISCV_SET32:
1936
0
    case R_RISCV_32_PCREL:
1937
0
    case R_RISCV_TLS_DTPREL32:
1938
0
    case R_RISCV_TLS_DTPREL64:
1939
0
      break;
1940
1941
0
    case R_RISCV_DELETE:
1942
0
      return bfd_reloc_ok;
1943
1944
0
    default:
1945
0
      return bfd_reloc_notsupported;
1946
0
    }
1947
1948
0
  bfd_vma word;
1949
0
  if (riscv_is_insn_reloc (howto))
1950
0
    word = riscv_get_insn (howto->bitsize, contents + rel->r_offset);
1951
0
  else
1952
0
    word = bfd_get (howto->bitsize, input_bfd, contents + rel->r_offset);
1953
0
  word = (word & ~howto->dst_mask) | (value & howto->dst_mask);
1954
0
  if (riscv_is_insn_reloc (howto))
1955
0
    riscv_put_insn (howto->bitsize, word, contents + rel->r_offset);
1956
0
  else
1957
0
    bfd_put (howto->bitsize, input_bfd, word, contents + rel->r_offset);
1958
1959
0
  return bfd_reloc_ok;
1960
0
}
1961
1962
/* Remember all PC-relative high-part relocs we've encountered to help us
1963
   later resolve the corresponding low-part relocs.  */
1964
1965
typedef struct
1966
{
1967
  /* PC value.  */
1968
  bfd_vma address;
1969
  /* Relocation value with addend.  */
1970
  bfd_vma value;
1971
  /* Original reloc type.  */
1972
  int type;
1973
  /* True if changed to R_RISCV_HI20.  */
1974
  bool absolute;
1975
} riscv_pcrel_hi_reloc;
1976
1977
typedef struct riscv_pcrel_lo_reloc
1978
{
1979
  /* PC value of auipc.  */
1980
  bfd_vma address;
1981
  /* Internal relocation.  */
1982
  Elf_Internal_Rela *reloc;
1983
  /* Record the following information helps to resolve the %pcrel
1984
     which cross different input section.  For now we build a hash
1985
     for pcrel at the start of riscv_elf_relocate_section, and then
1986
     free the hash at the end.  But riscv_elf_relocate_section only
1987
     handles an input section at a time, so that means we can only
1988
     resolve the %pcrel_hi and %pcrel_lo which are in the same input
1989
     section.  Otherwise, we will report dangerous relocation errors
1990
     for those %pcrel which are not in the same input section.  */
1991
  asection *input_section;
1992
  struct bfd_link_info *info;
1993
  reloc_howto_type *howto;
1994
  bfd_byte *contents;
1995
  /* The next riscv_pcrel_lo_reloc.  */
1996
  struct riscv_pcrel_lo_reloc *next;
1997
} riscv_pcrel_lo_reloc;
1998
1999
typedef struct
2000
{
2001
  /* Hash table for riscv_pcrel_hi_reloc.  */
2002
  htab_t hi_relocs;
2003
  /* Linked list for riscv_pcrel_lo_reloc.  */
2004
  riscv_pcrel_lo_reloc *lo_relocs;
2005
} riscv_pcrel_relocs;
2006
2007
static hashval_t
2008
riscv_pcrel_reloc_hash (const void *entry)
2009
0
{
2010
0
  const riscv_pcrel_hi_reloc *e = entry;
2011
0
  return (hashval_t)(e->address >> 2);
2012
0
}
2013
2014
static int
2015
riscv_pcrel_reloc_eq (const void *entry1, const void *entry2)
2016
0
{
2017
0
  const riscv_pcrel_hi_reloc *e1 = entry1, *e2 = entry2;
2018
0
  return e1->address == e2->address;
2019
0
}
2020
2021
static bool
2022
riscv_init_pcrel_relocs (riscv_pcrel_relocs *p)
2023
0
{
2024
0
  p->lo_relocs = NULL;
2025
0
  p->hi_relocs = htab_create (1024, riscv_pcrel_reloc_hash,
2026
0
            riscv_pcrel_reloc_eq, free);
2027
0
  return p->hi_relocs != NULL;
2028
0
}
2029
2030
static void
2031
riscv_free_pcrel_relocs (riscv_pcrel_relocs *p)
2032
0
{
2033
0
  riscv_pcrel_lo_reloc *cur = p->lo_relocs;
2034
2035
0
  while (cur != NULL)
2036
0
    {
2037
0
      riscv_pcrel_lo_reloc *next = cur->next;
2038
0
      free (cur);
2039
0
      cur = next;
2040
0
    }
2041
2042
0
  htab_delete (p->hi_relocs);
2043
0
}
2044
2045
static bool
2046
riscv_zero_pcrel_hi_reloc (Elf_Internal_Rela *rel,
2047
         struct bfd_link_info *info,
2048
         bfd_vma pc,
2049
         bfd_vma *addr,
2050
         bfd_byte *contents,
2051
         const reloc_howto_type *howto)
2052
0
{
2053
  /* We may need to reference low addreses in PC-relative modes even when the
2054
     PC is far away from these addresses.  For example, undefweak references
2055
     need to produce the address 0 when linked.  As 0 is far from the arbitrary
2056
     addresses that we can link PC-relative programs at, the linker can't
2057
     actually relocate references to those symbols.  In order to allow these
2058
     programs to work we simply convert the PC-relative auipc sequences to
2059
     0-relative lui sequences.  */
2060
0
  if (bfd_link_pic (info))
2061
0
    return false;
2062
2063
  /* If it's possible to reference the symbol using auipc we do so, as that's
2064
     more in the spirit of the PC-relative relocations we're processing.  */
2065
0
  bfd_vma offset = *addr - pc;
2066
0
  if (ARCH_SIZE == 32 || VALID_UTYPE_IMM (RISCV_CONST_HIGH_PART (offset)))
2067
0
    return false;
2068
2069
  /* If it's impossible to reference this with a LUI-based offset then don't
2070
     bother to convert it at all so users still see the PC-relative relocation
2071
     in the truncation message.  */
2072
0
  if (ARCH_SIZE > 32 && !VALID_UTYPE_IMM (RISCV_CONST_HIGH_PART (*addr)))
2073
0
    return false;
2074
2075
  /* PR27180, encode target absolute address into r_addend rather than
2076
     r_sym.  Clear the ADDR to avoid duplicate relocate in the
2077
     perform_relocation.  */
2078
0
  rel->r_info = ELF64_R_INFO (0, R_RISCV_HI20);
2079
0
  rel->r_addend += *addr;
2080
0
  *addr = 0;
2081
2082
0
  bfd_vma insn = riscv_get_insn (howto->bitsize, contents + rel->r_offset);
2083
0
  insn = (insn & ~MASK_AUIPC) | MATCH_LUI;
2084
0
  riscv_put_insn (howto->bitsize, insn, contents + rel->r_offset);
2085
0
  return true;
2086
0
}
2087
2088
static bool
2089
riscv_record_pcrel_hi_reloc (riscv_pcrel_relocs *p,
2090
           bfd_vma addr,
2091
           bfd_vma value,
2092
           int type,
2093
           bool absolute)
2094
0
{
2095
0
  bfd_vma offset = absolute ? value : value - addr;
2096
0
  riscv_pcrel_hi_reloc entry = {addr, offset, type, absolute};
2097
0
  riscv_pcrel_hi_reloc **slot =
2098
0
    (riscv_pcrel_hi_reloc **) htab_find_slot (p->hi_relocs, &entry, INSERT);
2099
2100
0
  BFD_ASSERT (*slot == NULL);
2101
0
  *slot = (riscv_pcrel_hi_reloc *) bfd_malloc (sizeof (riscv_pcrel_hi_reloc));
2102
0
  if (*slot == NULL)
2103
0
    return false;
2104
0
  **slot = entry;
2105
0
  return true;
2106
0
}
2107
2108
static bool
2109
riscv_record_pcrel_lo_reloc (riscv_pcrel_relocs *p,
2110
           bfd_vma addr,
2111
           Elf_Internal_Rela *reloc,
2112
           asection *input_section,
2113
           struct bfd_link_info *info,
2114
           reloc_howto_type *howto,
2115
           bfd_byte *contents)
2116
0
{
2117
0
  riscv_pcrel_lo_reloc *entry;
2118
0
  entry = (riscv_pcrel_lo_reloc *) bfd_malloc (sizeof (riscv_pcrel_lo_reloc));
2119
0
  if (entry == NULL)
2120
0
    return false;
2121
0
  *entry = (riscv_pcrel_lo_reloc) {addr, reloc, input_section, info,
2122
0
           howto, contents, p->lo_relocs};
2123
0
  p->lo_relocs = entry;
2124
0
  return true;
2125
0
}
2126
2127
static bool
2128
riscv_resolve_pcrel_lo_relocs (riscv_pcrel_relocs *p)
2129
0
{
2130
0
  riscv_pcrel_lo_reloc *r;
2131
2132
0
  for (r = p->lo_relocs; r != NULL; r = r->next)
2133
0
    {
2134
0
      bfd *input_bfd = r->input_section->owner;
2135
2136
0
      riscv_pcrel_hi_reloc search = {r->address, 0, 0, 0};
2137
0
      riscv_pcrel_hi_reloc *entry = htab_find (p->hi_relocs, &search);
2138
      /* There may be a risk if the %pcrel_lo with addend refers to
2139
   an IFUNC symbol.  The %pcrel_hi has been relocated to plt,
2140
   so the corresponding %pcrel_lo with addend looks wrong.  */
2141
0
      char *string = NULL;
2142
0
      if (entry == NULL)
2143
0
  string = _("%pcrel_lo missing matching %pcrel_hi");
2144
0
      else if (entry->type == R_RISCV_GOT_HI20
2145
0
         && r->reloc->r_addend != 0)
2146
0
  string = _("%pcrel_lo with addend isn't allowed for R_RISCV_GOT_HI20");
2147
0
      else if (RISCV_CONST_HIGH_PART (entry->value)
2148
0
         != RISCV_CONST_HIGH_PART (entry->value + r->reloc->r_addend))
2149
0
  {
2150
    /* Check the overflow when adding reloc addend.  */
2151
0
    string = bfd_asprintf (_("%%pcrel_lo overflow with an addend,"
2152
0
           " the value of %%pcrel_hi is 0x%" PRIx64
2153
0
           " without any addend, but may be 0x%" PRIx64
2154
0
           " after adding the %%pcrel_lo addend"),
2155
0
         (int64_t) RISCV_CONST_HIGH_PART (entry->value),
2156
0
         (int64_t) RISCV_CONST_HIGH_PART
2157
0
         (entry->value + r->reloc->r_addend));
2158
0
    if (string == NULL)
2159
0
      string = _("%pcrel_lo overflow with an addend");
2160
0
  }
2161
2162
0
      if (string != NULL)
2163
0
  {
2164
0
    (*r->info->callbacks->reloc_dangerous)
2165
0
      (r->info, string, input_bfd, r->input_section, r->reloc->r_offset);
2166
0
    return true;
2167
0
  }
2168
2169
0
      perform_relocation (r->howto, r->reloc, entry->value, r->input_section,
2170
0
        input_bfd, r->contents);
2171
2172
      /* The corresponding R_RISCV_GOT_PCREL_HI20 and R_RISCV_PCREL_HI20 are
2173
   converted to R_RISCV_HI20, so try to convert R_RISCV_PCREL_LO12_I/S
2174
   to R_RISCV_LO12_I/S.  */
2175
0
      if (entry->absolute)
2176
0
  {
2177
0
    switch (ELF64_R_TYPE (r->reloc->r_info))
2178
0
      {
2179
0
      case R_RISCV_PCREL_LO12_I:
2180
0
        r->reloc->r_info = ELF64_R_INFO (0, R_RISCV_LO12_I);
2181
0
        r->reloc->r_addend += entry->value;
2182
0
        break;
2183
0
      case R_RISCV_PCREL_LO12_S:
2184
0
        r->reloc->r_info = ELF64_R_INFO (0, R_RISCV_LO12_S);
2185
0
        r->reloc->r_addend += entry->value;
2186
0
        break;
2187
0
      default:
2188
        /* This shouldn't happen, so just skip it.  */
2189
0
        break;
2190
0
      }
2191
0
  }
2192
0
    }
2193
2194
0
  return true;
2195
0
}
2196
2197
/* Relocate a RISC-V ELF section.
2198
2199
   The RELOCATE_SECTION function is called by the new ELF backend linker
2200
   to handle the relocations for a section.
2201
2202
   The relocs are always passed as Rela structures.
2203
2204
   This function is responsible for adjusting the section contents as
2205
   necessary, and (if generating a relocatable output file) adjusting
2206
   the reloc addend as necessary.
2207
2208
   This function does not have to worry about setting the reloc
2209
   address or the reloc symbol index.
2210
2211
   LOCAL_SYMS is a pointer to the swapped in local symbols.
2212
2213
   LOCAL_SECTIONS is an array giving the section in the input file
2214
   corresponding to the st_shndx field of each local symbol.
2215
2216
   The global hash table entry for the global symbols can be found
2217
   via elf_sym_hashes (input_bfd).
2218
2219
   When generating relocatable output, this function must handle
2220
   STB_LOCAL/STT_SECTION symbols specially.  The output symbol is
2221
   going to be the section symbol corresponding to the output
2222
   section, which means that the addend must be adjusted
2223
   accordingly.  */
2224
2225
static int
2226
riscv_elf_relocate_section (bfd *output_bfd,
2227
          struct bfd_link_info *info,
2228
          bfd *input_bfd,
2229
          asection *input_section,
2230
          bfd_byte *contents,
2231
          Elf_Internal_Rela *relocs,
2232
          Elf_Internal_Sym *local_syms,
2233
          asection **local_sections)
2234
0
{
2235
0
  Elf_Internal_Rela *rel;
2236
0
  Elf_Internal_Rela *relend;
2237
0
  riscv_pcrel_relocs pcrel_relocs;
2238
0
  bool ret = false;
2239
0
  struct riscv_elf_link_hash_table *htab = riscv_elf_hash_table (info);
2240
0
  Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (input_bfd);
2241
0
  struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (input_bfd);
2242
0
  bfd_vma *local_got_offsets = elf_local_got_offsets (input_bfd);
2243
0
  bfd_vma uleb128_set_vma = 0;
2244
0
  Elf_Internal_Rela *uleb128_set_rel = NULL;
2245
0
  bool absolute;
2246
2247
0
  if (!riscv_init_pcrel_relocs (&pcrel_relocs))
2248
0
    return false;
2249
2250
0
  relend = relocs + input_section->reloc_count;
2251
0
  for (rel = relocs; rel < relend; rel++)
2252
0
    {
2253
0
      unsigned long r_symndx;
2254
0
      struct elf_link_hash_entry *h;
2255
0
      Elf_Internal_Sym *sym;
2256
0
      asection *sec;
2257
0
      bfd_vma relocation;
2258
0
      bfd_reloc_status_type r = bfd_reloc_ok;
2259
0
      const char *name = NULL;
2260
0
      bfd_vma off, ie_off, desc_off;
2261
0
      bool unresolved_reloc, is_ie = false, is_desc = false;
2262
0
      bfd_vma pc = sec_addr (input_section) + rel->r_offset;
2263
0
      int r_type = ELF64_R_TYPE (rel->r_info), tls_type;
2264
0
      reloc_howto_type *howto = riscv_elf_rtype_to_howto (input_bfd, r_type);
2265
0
      const char *msg = NULL;
2266
0
      bool resolved_to_zero;
2267
0
      bool via_plt = false;
2268
2269
0
      if (howto == NULL)
2270
0
  continue;
2271
2272
      /* This is a final link.  */
2273
0
      r_symndx = ELF64_R_SYM (rel->r_info);
2274
0
      h = NULL;
2275
0
      sym = NULL;
2276
0
      sec = NULL;
2277
0
      unresolved_reloc = false;
2278
0
      if (r_symndx < symtab_hdr->sh_info)
2279
0
  {
2280
0
    sym = local_syms + r_symndx;
2281
0
    sec = local_sections[r_symndx];
2282
0
    relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
2283
2284
    /* Relocate against local STT_GNU_IFUNC symbol.  */
2285
0
    if (!bfd_link_relocatable (info)
2286
0
        && ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
2287
0
      {
2288
0
        h = riscv_elf_get_local_sym_hash (htab, input_bfd, rel, false);
2289
0
        if (h == NULL)
2290
0
    abort ();
2291
2292
        /* Set STT_GNU_IFUNC symbol value.  */
2293
0
        h->root.u.def.value = sym->st_value;
2294
0
        h->root.u.def.section = sec;
2295
0
      }
2296
0
  }
2297
0
      else
2298
0
  {
2299
0
    bool warned, ignored;
2300
2301
0
    RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
2302
0
           r_symndx, symtab_hdr, sym_hashes,
2303
0
           h, sec, relocation,
2304
0
           unresolved_reloc, warned, ignored);
2305
0
    if (warned)
2306
0
      {
2307
        /* To avoid generating warning messages about truncated
2308
     relocations, set the relocation's address to be the same as
2309
     the start of this section.  */
2310
0
        if (input_section->output_section != NULL)
2311
0
    relocation = input_section->output_section->vma;
2312
0
        else
2313
0
    relocation = 0;
2314
0
      }
2315
0
  }
2316
2317
0
      if (sec != NULL && discarded_section (sec))
2318
0
  RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
2319
0
           rel, 1, relend, howto, 0, contents);
2320
2321
0
      if (bfd_link_relocatable (info))
2322
0
  continue;
2323
2324
      /* Since STT_GNU_IFUNC symbol must go through PLT, we handle
2325
   it here if it is defined in a non-shared object.  */
2326
0
      if (h != NULL
2327
0
    && h->type == STT_GNU_IFUNC
2328
0
    && h->def_regular)
2329
0
  {
2330
0
    asection *plt, *base_got;
2331
2332
0
    if ((input_section->flags & SEC_ALLOC) == 0)
2333
0
      {
2334
        /* If this is a SHT_NOTE section without SHF_ALLOC, treat
2335
     STT_GNU_IFUNC symbol as STT_FUNC.  */
2336
0
        if (elf_section_type (input_section) == SHT_NOTE)
2337
0
    goto skip_ifunc;
2338
2339
        /* Dynamic relocs are not propagated for SEC_DEBUGGING
2340
     sections because such sections are not SEC_ALLOC and
2341
     thus ld.so will not process them.  */
2342
0
        if ((input_section->flags & SEC_DEBUGGING) != 0)
2343
0
    continue;
2344
2345
0
        abort ();
2346
0
      }
2347
0
    else if (h->plt.offset == (bfd_vma) -1
2348
       /* The following relocation may not need the .plt entries
2349
          when all references to a STT_GNU_IFUNC symbols are done
2350
          via GOT or static function pointers.  */
2351
0
       && r_type != R_RISCV_32
2352
0
       && r_type != R_RISCV_64
2353
0
       && r_type != R_RISCV_HI20
2354
0
       && r_type != R_RISCV_GOT_HI20
2355
0
       && r_type != R_RISCV_LO12_I
2356
0
       && r_type != R_RISCV_LO12_S)
2357
0
      goto bad_ifunc_reloc;
2358
2359
    /* STT_GNU_IFUNC symbol must go through PLT.  */
2360
0
    plt = htab->elf.splt ? htab->elf.splt : htab->elf.iplt;
2361
0
    relocation = plt->output_section->vma
2362
0
           + plt->output_offset
2363
0
           + h->plt.offset;
2364
2365
0
    switch (r_type)
2366
0
      {
2367
0
      case R_RISCV_32:
2368
0
      case R_RISCV_64:
2369
0
        if (rel->r_addend != 0)
2370
0
    {
2371
0
      if (h->root.root.string)
2372
0
        name = h->root.root.string;
2373
0
      else
2374
0
        name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym, NULL);
2375
2376
0
      _bfd_error_handler
2377
        /* xgettext:c-format */
2378
0
        (_("%pB: relocation %s against STT_GNU_IFUNC "
2379
0
           "symbol `%s' has non-zero addend: %" PRId64),
2380
0
         input_bfd, howto->name, name, (int64_t) rel->r_addend);
2381
0
      bfd_set_error (bfd_error_bad_value);
2382
0
      return false;
2383
0
    }
2384
2385
    /* Generate dynamic relocation only when there is a non-GOT
2386
       reference in a shared object or there is no PLT.  */
2387
0
    if ((bfd_link_pic (info) && h->non_got_ref)
2388
0
        || h->plt.offset == (bfd_vma) -1)
2389
0
      {
2390
0
        Elf_Internal_Rela outrel;
2391
2392
        /* Need a dynamic relocation to get the real function
2393
           address.  */
2394
0
        outrel.r_offset = _bfd_elf_section_offset (output_bfd,
2395
0
                     info,
2396
0
                     input_section,
2397
0
                     rel->r_offset);
2398
0
        if (outrel.r_offset == (bfd_vma) -1
2399
0
      || outrel.r_offset == (bfd_vma) -2)
2400
0
          abort ();
2401
2402
0
        outrel.r_offset += input_section->output_section->vma
2403
0
               + input_section->output_offset;
2404
2405
0
        if (h->dynindx == -1
2406
0
      || h->forced_local
2407
0
      || bfd_link_executable (info))
2408
0
          {
2409
0
      info->callbacks->minfo
2410
0
        (_("Local IFUNC function `%s' in %pB\n"),
2411
0
         h->root.root.string,
2412
0
         h->root.u.def.section->owner);
2413
2414
      /* This symbol is resolved locally.  */
2415
0
      outrel.r_info = ELF64_R_INFO (0, R_RISCV_IRELATIVE);
2416
0
      outrel.r_addend = h->root.u.def.value
2417
0
        + h->root.u.def.section->output_section->vma
2418
0
        + h->root.u.def.section->output_offset;
2419
0
          }
2420
0
        else
2421
0
          {
2422
0
      outrel.r_info = ELF64_R_INFO (h->dynindx, r_type);
2423
0
      outrel.r_addend = 0;
2424
0
          }
2425
2426
        /* Dynamic relocations are stored in
2427
           1. .rela.ifunc section in PIC object.
2428
           2. .rela.got section in dynamic executable.
2429
           3. .rela.iplt section in static executable.  */
2430
0
        if (bfd_link_pic (info))
2431
0
          riscv_elf_append_rela (output_bfd, htab->elf.irelifunc,
2432
0
               &outrel);
2433
0
        else if (htab->elf.splt != NULL)
2434
0
          riscv_elf_append_rela (output_bfd, htab->elf.srelgot,
2435
0
               &outrel);
2436
0
        else
2437
0
          {
2438
      /* Do not use riscv_elf_append_rela to add dynamic
2439
         relocs into .rela.iplt, since it may cause the
2440
         overwrite problems.  This is same as what we did
2441
         in the riscv_elf_finish_dynamic_symbol.  */
2442
0
      const struct elf_backend_data *bed =
2443
0
        get_elf_backend_data (output_bfd);
2444
0
      bfd_vma iplt_idx = htab->last_iplt_index--;
2445
0
      bfd_byte *loc = htab->elf.irelplt->contents
2446
0
          + iplt_idx * sizeof (Elf64_External_Rela);
2447
0
      bed->s->swap_reloca_out (output_bfd, &outrel, loc);
2448
0
          }
2449
2450
        /* If this reloc is against an external symbol, we
2451
           do not want to fiddle with the addend.  Otherwise,
2452
           we need to include the symbol value so that it
2453
           becomes an addend for the dynamic reloc.  For an
2454
           internal symbol, we have updated addend.  */
2455
0
        continue;
2456
0
      }
2457
0
    goto do_relocation;
2458
2459
0
        case R_RISCV_GOT_HI20:
2460
0
    base_got = htab->elf.sgot;
2461
0
    off = h->got.offset;
2462
2463
0
    if (base_got == NULL)
2464
0
      abort ();
2465
2466
0
    if (off == (bfd_vma) -1)
2467
0
      {
2468
0
        bfd_vma plt_idx;
2469
2470
        /* We can't use h->got.offset here to save state, or
2471
           even just remember the offset, as finish_dynamic_symbol
2472
           would use that as offset into .got.  */
2473
2474
0
        if (htab->elf.splt != NULL)
2475
0
          {
2476
0
      plt_idx = (h->plt.offset - PLT_HEADER_SIZE)
2477
0
          / PLT_ENTRY_SIZE;
2478
0
      off = GOTPLT_HEADER_SIZE + (plt_idx * GOT_ENTRY_SIZE);
2479
0
      base_got = htab->elf.sgotplt;
2480
0
          }
2481
0
        else
2482
0
          {
2483
0
      plt_idx = h->plt.offset / PLT_ENTRY_SIZE;
2484
0
      off = plt_idx * GOT_ENTRY_SIZE;
2485
0
      base_got = htab->elf.igotplt;
2486
0
          }
2487
2488
0
        if (h->dynindx == -1
2489
0
      || h->forced_local
2490
0
      || info->symbolic)
2491
0
          {
2492
      /* This references the local definition.  We must
2493
         initialize this entry in the global offset table.
2494
         Since the offset must always be a multiple of 8,
2495
         we use the least significant bit to record
2496
         whether we have initialized it already.
2497
2498
         When doing a dynamic link, we create a .rela.got
2499
         relocation entry to initialize the value.  This
2500
         is done in the finish_dynamic_symbol routine.   */
2501
0
      if ((off & 1) != 0)
2502
0
        off &= ~1;
2503
0
      else
2504
0
        {
2505
0
          bfd_put_64 (output_bfd, relocation,
2506
0
          base_got->contents + off);
2507
          /* Note that this is harmless for the case,
2508
             as -1 | 1 still is -1.  */
2509
0
          h->got.offset |= 1;
2510
0
        }
2511
0
          }
2512
0
      }
2513
2514
0
    relocation = base_got->output_section->vma
2515
0
           + base_got->output_offset + off;
2516
2517
0
    if (!riscv_record_pcrel_hi_reloc (&pcrel_relocs, pc,
2518
0
              relocation, r_type,
2519
0
              false))
2520
0
      r = bfd_reloc_overflow;
2521
0
    goto do_relocation;
2522
2523
0
        case R_RISCV_CALL:
2524
0
        case R_RISCV_CALL_PLT:
2525
0
        case R_RISCV_HI20:
2526
0
        case R_RISCV_LO12_I:
2527
0
        case R_RISCV_LO12_S:
2528
0
    goto do_relocation;
2529
2530
0
        case R_RISCV_PCREL_HI20:
2531
0
    if (!riscv_record_pcrel_hi_reloc (&pcrel_relocs, pc,
2532
0
              relocation, r_type,
2533
0
              false))
2534
0
      r = bfd_reloc_overflow;
2535
0
    goto do_relocation;
2536
2537
0
      default:
2538
0
      bad_ifunc_reloc:
2539
0
        if (h->root.root.string)
2540
0
    name = h->root.root.string;
2541
0
        else
2542
    /* The entry of local ifunc is fake in global hash table,
2543
       we should find the name by the original local symbol.  */
2544
0
    name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym, NULL);
2545
2546
0
        _bfd_error_handler
2547
        /* xgettext:c-format */
2548
0
        (_("%pB: relocation %s against STT_GNU_IFUNC "
2549
0
     "symbol `%s' isn't supported"), input_bfd,
2550
0
         howto->name, name);
2551
0
        bfd_set_error (bfd_error_bad_value);
2552
0
        return false;
2553
0
      }
2554
0
  }
2555
2556
0
    skip_ifunc:
2557
0
      if (h != NULL)
2558
0
  name = h->root.root.string;
2559
0
      else
2560
0
  {
2561
0
    name = (bfd_elf_string_from_elf_section
2562
0
      (input_bfd, symtab_hdr->sh_link, sym->st_name));
2563
0
    if (name == NULL || *name == '\0')
2564
0
      name = bfd_section_name (sec);
2565
0
  }
2566
2567
0
      resolved_to_zero = (h != NULL
2568
0
        && UNDEFWEAK_NO_DYNAMIC_RELOC (info, h));
2569
2570
      /* Refer to the PLT entry.  This check has to match the check in
2571
   _bfd_riscv_relax_section.  */
2572
0
      via_plt = (htab->elf.splt != NULL
2573
0
     && h != NULL
2574
0
     && h->plt.offset != MINUS_ONE);
2575
2576
0
      switch (r_type)
2577
0
  {
2578
0
  case R_RISCV_NONE:
2579
0
  case R_RISCV_RELAX:
2580
0
  case R_RISCV_TPREL_ADD:
2581
0
  case R_RISCV_TLSDESC_CALL:
2582
0
  case R_RISCV_COPY:
2583
0
  case R_RISCV_JUMP_SLOT:
2584
0
  case R_RISCV_RELATIVE:
2585
    /* These require nothing of us at all.  */
2586
0
    continue;
2587
2588
0
  case R_RISCV_HI20:
2589
0
  case R_RISCV_BRANCH:
2590
0
  case R_RISCV_RVC_BRANCH:
2591
0
  case R_RISCV_RVC_LUI:
2592
0
  case R_RISCV_LO12_I:
2593
0
  case R_RISCV_LO12_S:
2594
0
  case R_RISCV_SET6:
2595
0
  case R_RISCV_SET8:
2596
0
  case R_RISCV_SET16:
2597
0
  case R_RISCV_SET32:
2598
0
  case R_RISCV_32_PCREL:
2599
0
  case R_RISCV_DELETE:
2600
    /* These require no special handling beyond perform_relocation.  */
2601
0
    break;
2602
2603
0
  case R_RISCV_SET_ULEB128:
2604
0
    if (uleb128_set_rel == NULL)
2605
0
      {
2606
        /* Saved for later usage.  */
2607
0
        uleb128_set_vma = relocation;
2608
0
        uleb128_set_rel = rel;
2609
0
        continue;
2610
0
      }
2611
0
    else
2612
0
      {
2613
0
        msg = ("Mismatched R_RISCV_SET_ULEB128, it must be paired with"
2614
0
         " and applied before R_RISCV_SUB_ULEB128");
2615
0
        r = bfd_reloc_dangerous;
2616
0
      }
2617
0
    break;
2618
2619
0
  case R_RISCV_SUB_ULEB128:
2620
0
    if (uleb128_set_rel != NULL
2621
0
        && uleb128_set_rel->r_offset == rel->r_offset)
2622
0
      {
2623
0
        relocation = uleb128_set_vma - relocation
2624
0
         + uleb128_set_rel->r_addend;
2625
0
        uleb128_set_vma = 0;
2626
0
        uleb128_set_rel = NULL;
2627
2628
        /* PR31179, the addend of SUB_ULEB128 should be zero if using
2629
     .uleb128, but we make it non-zero by accident in assembler,
2630
     so just ignore it in perform_relocation, and make assembler
2631
     continue doing the right thing.  Don't reset the addend of
2632
     SUB_ULEB128 to zero here since it will break the --emit-reloc,
2633
     even though the non-zero addend is unexpected.
2634
2635
     We encourage people to rebuild their stuff to get the
2636
     non-zero addend of SUB_ULEB128, but that might need some
2637
     times, so report warnings to inform people need to rebuild
2638
     if --check-uleb128 is enabled.  However, since the failed
2639
     .reloc cases for ADD/SET/SUB/ULEB128 are rarely to use, it
2640
     may acceptable that stop supproting them until people rebuld
2641
     their stuff, maybe half-year or one year later.  I believe
2642
     this might be the least harmful option that we should go.
2643
2644
     Or maybe we should teach people that don't write the
2645
     .reloc R_RISCV_SUB* with non-zero constant, and report
2646
     warnings/errors in assembler.  */
2647
0
        if (htab->params->check_uleb128
2648
0
      && rel->r_addend != 0)
2649
0
    _bfd_error_handler (_("%pB: warning: R_RISCV_SUB_ULEB128 with"
2650
0
              " non-zero addend, please rebuild by"
2651
0
              " binutils 2.42 or up"), input_bfd);
2652
0
      }
2653
0
    else
2654
0
      {
2655
0
        msg = ("Mismatched R_RISCV_SUB_ULEB128, it must be paired with"
2656
0
         " and applied after R_RISCV_SET_ULEB128");
2657
0
        r = bfd_reloc_dangerous;
2658
0
      }
2659
0
    break;
2660
2661
0
  case R_RISCV_GOT_HI20:
2662
0
    if (h != NULL)
2663
0
      {
2664
0
        off = h->got.offset;
2665
0
        BFD_ASSERT (off != (bfd_vma) -1);
2666
2667
0
        if (RISCV_RESOLVED_LOCALLY (info, h))
2668
0
    {
2669
      /* We must initialize this entry in the global offset table.
2670
         Since the offset must always be a multiple of the word
2671
         size, we use the least significant bit to record whether
2672
         we have initialized it already.
2673
2674
         When doing a dynamic link, we create a .rela.got
2675
         relocation entry to initialize the value.  This
2676
         is done in the finish_dynamic_symbol routine.  */
2677
0
      if ((off & 1) != 0)
2678
0
        off &= ~1;
2679
0
      else
2680
0
        {
2681
0
          bfd_put_64 (output_bfd, relocation,
2682
0
          htab->elf.sgot->contents + off);
2683
0
          h->got.offset |= 1;
2684
0
        }
2685
0
    }
2686
0
        else
2687
0
    unresolved_reloc = false;
2688
0
      }
2689
0
    else
2690
0
      {
2691
0
        BFD_ASSERT (local_got_offsets != NULL
2692
0
        && local_got_offsets[r_symndx] != (bfd_vma) -1);
2693
2694
0
        off = local_got_offsets[r_symndx];
2695
2696
        /* The offset must always be a multiple of the word size.
2697
     So, we can use the least significant bit to record
2698
     whether we have already processed this entry.  */
2699
0
        if ((off & 1) != 0)
2700
0
    off &= ~1;
2701
0
        else
2702
0
    {
2703
0
      if (bfd_link_pic (info))
2704
0
        {
2705
0
          asection *s;
2706
0
          Elf_Internal_Rela outrel;
2707
2708
          /* We need to generate a R_RISCV_RELATIVE reloc
2709
       for the dynamic linker.  */
2710
0
          s = htab->elf.srelgot;
2711
0
          BFD_ASSERT (s != NULL);
2712
2713
0
          outrel.r_offset = sec_addr (htab->elf.sgot) + off;
2714
0
          outrel.r_info =
2715
0
      ELF64_R_INFO (0, R_RISCV_RELATIVE);
2716
0
          outrel.r_addend = relocation;
2717
0
          relocation = 0;
2718
0
          riscv_elf_append_rela (output_bfd, s, &outrel);
2719
0
        }
2720
2721
0
      bfd_put_64 (output_bfd, relocation,
2722
0
            htab->elf.sgot->contents + off);
2723
0
      local_got_offsets[r_symndx] |= 1;
2724
0
    }
2725
0
      }
2726
2727
0
    if (rel->r_addend != 0)
2728
0
      {
2729
0
        msg = _("The addend isn't allowed for R_RISCV_GOT_HI20");
2730
0
        r = bfd_reloc_dangerous;
2731
0
      }
2732
0
    else
2733
0
      {
2734
        /* Address of got entry.  */
2735
0
        relocation = sec_addr (htab->elf.sgot) + off;
2736
0
        absolute = riscv_zero_pcrel_hi_reloc (rel, info, pc,
2737
0
                &relocation, contents,
2738
0
                howto);
2739
        /* Update howto if relocation is changed.  */
2740
0
        howto = riscv_elf_rtype_to_howto (input_bfd,
2741
0
            ELF64_R_TYPE (rel->r_info));
2742
0
        if (howto == NULL)
2743
0
    r = bfd_reloc_notsupported;
2744
0
        else if (!riscv_record_pcrel_hi_reloc (&pcrel_relocs, pc,
2745
0
                 relocation + rel->r_addend,
2746
0
                 r_type, absolute))
2747
0
    r = bfd_reloc_overflow;
2748
0
      }
2749
0
    break;
2750
2751
0
  case R_RISCV_ADD8:
2752
0
  case R_RISCV_ADD16:
2753
0
  case R_RISCV_ADD32:
2754
0
  case R_RISCV_ADD64:
2755
0
    {
2756
0
      bfd_vma old_value = bfd_get (howto->bitsize, input_bfd,
2757
0
           contents + rel->r_offset);
2758
0
      relocation = old_value + relocation;
2759
0
    }
2760
0
    break;
2761
2762
0
  case R_RISCV_SUB6:
2763
0
    {
2764
0
      bfd_vma old_value = bfd_get (howto->bitsize, input_bfd,
2765
0
           contents + rel->r_offset);
2766
0
      relocation = (old_value & ~howto->dst_mask)
2767
0
       | (((old_value & howto->dst_mask) - relocation)
2768
0
          & howto->dst_mask);
2769
0
    }
2770
0
    break;
2771
2772
0
  case R_RISCV_SUB8:
2773
0
  case R_RISCV_SUB16:
2774
0
  case R_RISCV_SUB32:
2775
0
  case R_RISCV_SUB64:
2776
0
    {
2777
0
      bfd_vma old_value = bfd_get (howto->bitsize, input_bfd,
2778
0
           contents + rel->r_offset);
2779
0
      relocation = old_value - relocation;
2780
0
    }
2781
0
    break;
2782
2783
0
  case R_RISCV_CALL:
2784
0
  case R_RISCV_CALL_PLT:
2785
    /* Handle a call to an undefined weak function.  This won't be
2786
       relaxed, so we have to handle it here.  */
2787
0
    if (h != NULL && h->root.type == bfd_link_hash_undefweak && !via_plt)
2788
0
      {
2789
        /* We can use x0 as the base register.  */
2790
0
        bfd_vma insn = bfd_getl32 (contents + rel->r_offset + 4);
2791
0
        insn &= ~(OP_MASK_RS1 << OP_SH_RS1);
2792
0
        bfd_putl32 (insn, contents + rel->r_offset + 4);
2793
        /* Set the relocation value so that we get 0 after the pc
2794
     relative adjustment.  */
2795
0
        relocation = sec_addr (input_section) + rel->r_offset;
2796
0
      }
2797
    /* Fall through.  */
2798
2799
0
  case R_RISCV_JAL:
2800
0
  case R_RISCV_RVC_JUMP:
2801
0
    if (via_plt)
2802
0
      {
2803
0
        relocation = sec_addr (htab->elf.splt) + h->plt.offset;
2804
0
        unresolved_reloc = false;
2805
0
      }
2806
0
    else if (bfd_link_pic (info)
2807
0
       && h != NULL
2808
0
       && h->plt.offset == MINUS_ONE
2809
0
       && !SYMBOL_REFERENCES_LOCAL (info, h)
2810
0
       && (input_section->flags & SEC_ALLOC) != 0
2811
0
       && (input_section->flags & SEC_READONLY) != 0
2812
0
       && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
2813
0
      {
2814
        /* PR 28509, when generating the shared object, these
2815
     referenced symbols may bind externally, which means
2816
     they will be exported to the dynamic symbol table,
2817
     and are preemptible by default.  These symbols cannot
2818
     be referenced by the non-pic relocations, like
2819
     R_RISCV_JAL and R_RISCV_RVC_JUMP relocations.
2820
2821
     However, consider that linker may relax the R_RISCV_CALL
2822
     relocations to R_RISCV_JAL or R_RISCV_RVC_JUMP, if
2823
     these relocations are relocated to the plt entries,
2824
     then we won't report error for them.
2825
2826
     Perhaps we also need the similar checks for the
2827
     R_RISCV_BRANCH and R_RISCV_RVC_BRANCH relocations.  */
2828
0
        msg = bfd_asprintf (_("%%X%%P: relocation %s against `%s'"
2829
0
            " which may bind externally"
2830
0
            " can not be used"
2831
0
            " when making a shared object;"
2832
0
            " recompile with -fPIC\n"),
2833
0
          howto->name, h->root.root.string);
2834
0
        r = bfd_reloc_notsupported;
2835
0
      }
2836
0
    break;
2837
2838
0
  case R_RISCV_TPREL_HI20:
2839
0
    relocation = tpoff (info, relocation);
2840
0
    break;
2841
2842
0
  case R_RISCV_TPREL_LO12_I:
2843
0
  case R_RISCV_TPREL_LO12_S:
2844
0
    relocation = tpoff (info, relocation);
2845
0
    break;
2846
2847
0
  case R_RISCV_TPREL_I:
2848
0
  case R_RISCV_TPREL_S:
2849
0
    relocation = tpoff (info, relocation);
2850
0
    if (VALID_ITYPE_IMM (relocation + rel->r_addend))
2851
0
      {
2852
        /* We can use tp as the base register.  */
2853
0
        bfd_vma insn = bfd_getl32 (contents + rel->r_offset);
2854
0
        insn &= ~(OP_MASK_RS1 << OP_SH_RS1);
2855
0
        insn |= X_TP << OP_SH_RS1;
2856
0
        bfd_putl32 (insn, contents + rel->r_offset);
2857
0
      }
2858
0
    else
2859
0
      r = bfd_reloc_overflow;
2860
0
    break;
2861
2862
0
  case R_RISCV_GPREL_I:
2863
0
  case R_RISCV_GPREL_S:
2864
0
    {
2865
0
      bfd_vma gp = riscv_global_pointer_value (info);
2866
0
      bool x0_base = VALID_ITYPE_IMM (relocation + rel->r_addend);
2867
0
      if (x0_base || VALID_ITYPE_IMM (relocation + rel->r_addend - gp))
2868
0
        {
2869
    /* We can use x0 or gp as the base register.  */
2870
0
    bfd_vma insn = bfd_getl32 (contents + rel->r_offset);
2871
0
    insn &= ~(OP_MASK_RS1 << OP_SH_RS1);
2872
0
    if (!x0_base)
2873
0
      {
2874
0
        rel->r_addend -= gp;
2875
0
        insn |= X_GP << OP_SH_RS1;
2876
0
      }
2877
0
    bfd_putl32 (insn, contents + rel->r_offset);
2878
0
        }
2879
0
      else
2880
0
        r = bfd_reloc_overflow;
2881
0
      break;
2882
0
    }
2883
2884
0
  case R_RISCV_PCREL_HI20:
2885
0
    absolute = riscv_zero_pcrel_hi_reloc (rel, info, pc, &relocation,
2886
0
            contents, howto);
2887
    /* Update howto if relocation is changed.  */
2888
0
    howto = riscv_elf_rtype_to_howto (input_bfd,
2889
0
              ELF64_R_TYPE (rel->r_info));
2890
0
    if (howto == NULL)
2891
0
      r = bfd_reloc_notsupported;
2892
0
    else if (!riscv_record_pcrel_hi_reloc (&pcrel_relocs, pc,
2893
0
             relocation + rel->r_addend,
2894
0
             r_type, absolute))
2895
0
      r = bfd_reloc_overflow;
2896
0
    break;
2897
2898
0
  case R_RISCV_PCREL_LO12_I:
2899
0
  case R_RISCV_PCREL_LO12_S:
2900
    /* We don't allow section symbols plus addends as the auipc address,
2901
       because then riscv_relax_delete_bytes would have to search through
2902
       all relocs to update these addends.  This is also ambiguous, as
2903
       we do allow offsets to be added to the target address, which are
2904
       not to be used to find the auipc address.  */
2905
0
    if (((sym != NULL && (ELF_ST_TYPE (sym->st_info) == STT_SECTION))
2906
0
         || (h != NULL && h->type == STT_SECTION))
2907
0
        && rel->r_addend)
2908
0
      {
2909
0
        msg = _("%pcrel_lo section symbol with an addend");
2910
0
        r = bfd_reloc_dangerous;
2911
0
        break;
2912
0
      }
2913
2914
0
    if (riscv_record_pcrel_lo_reloc (&pcrel_relocs, relocation, rel,
2915
0
             input_section, info, howto,
2916
0
             contents))
2917
0
      continue;
2918
0
    r = bfd_reloc_overflow;
2919
0
    break;
2920
2921
0
  case R_RISCV_TLS_DTPREL32:
2922
0
  case R_RISCV_TLS_DTPREL64:
2923
0
    relocation = dtpoff (info, relocation);
2924
0
    break;
2925
2926
0
  case R_RISCV_TLSDESC_LOAD_LO12:
2927
0
  case R_RISCV_TLSDESC_ADD_LO12:
2928
0
    if (rel->r_addend)
2929
0
      {
2930
0
        msg = _("%tlsdesc_lo with addend");
2931
0
        r = bfd_reloc_dangerous;
2932
0
        break;
2933
0
      }
2934
2935
0
    if (riscv_record_pcrel_lo_reloc (&pcrel_relocs, relocation, rel,
2936
0
             input_section, info, howto,
2937
0
             contents))
2938
0
        continue;
2939
0
    r = bfd_reloc_overflow;
2940
0
    break;
2941
2942
0
  case R_RISCV_32:
2943
    /* Non ABS symbol should be blocked in check_relocs.  */
2944
0
    if (ARCH_SIZE > 32)
2945
0
      break;
2946
    /* Fall through.  */
2947
2948
0
  case R_RISCV_64:
2949
0
    if ((input_section->flags & SEC_ALLOC) == 0)
2950
0
      break;
2951
2952
0
    if (RISCV_GENERATE_DYNAMIC_RELOC (howto->pc_relative, info, h,
2953
0
              resolved_to_zero))
2954
0
      {
2955
0
        Elf_Internal_Rela outrel;
2956
0
        asection *sreloc;
2957
2958
        /* When generating a shared object, these relocations
2959
     are copied into the output file to be resolved at run
2960
     time.  */
2961
2962
0
        outrel.r_offset =
2963
0
    _bfd_elf_section_offset (output_bfd, info, input_section,
2964
0
           rel->r_offset);
2965
0
        bool skip = false;
2966
0
        bool relocate = false;
2967
0
        if (outrel.r_offset == (bfd_vma) -1)
2968
0
    skip = true;
2969
0
        else if (outrel.r_offset == (bfd_vma) -2)
2970
0
    {
2971
0
      skip = true;
2972
0
      relocate = true;
2973
0
    }
2974
0
        else if (h != NULL && bfd_is_abs_symbol (&h->root))
2975
0
    {
2976
      /* Don't need dynamic reloc when the ABS symbol is
2977
         non-dynamic or forced to local.  Maybe just use
2978
         SYMBOL_REFERENCES_LOCAL to check?  */
2979
0
      skip = (h->forced_local || (h->dynindx == -1));
2980
0
      relocate = skip;
2981
0
    }
2982
2983
0
        outrel.r_offset += sec_addr (input_section);
2984
2985
0
        if (skip)
2986
0
    memset (&outrel, 0, sizeof outrel); /* R_RISCV_NONE.  */
2987
0
        else if (RISCV_COPY_INPUT_RELOC (info, h))
2988
0
    {
2989
      /* Maybe just use !SYMBOL_REFERENCES_LOCAL to check?  */
2990
0
      outrel.r_info = ELF64_R_INFO (h->dynindx, r_type);
2991
0
      outrel.r_addend = rel->r_addend;
2992
0
    }
2993
0
        else
2994
0
    {
2995
      /* This symbol is local, or marked to become local.  */
2996
0
      outrel.r_info = ELF64_R_INFO (0, R_RISCV_RELATIVE);
2997
0
      outrel.r_addend = relocation + rel->r_addend;
2998
0
    }
2999
3000
0
        sreloc = elf_section_data (input_section)->sreloc;
3001
0
        riscv_elf_append_rela (output_bfd, sreloc, &outrel);
3002
0
        if (!relocate)
3003
0
    continue;
3004
0
      }
3005
0
    break;
3006
3007
0
  case R_RISCV_TLSDESC_HI20:
3008
0
    is_desc = true;
3009
0
    goto tls;
3010
3011
0
  case R_RISCV_TLS_GOT_HI20:
3012
0
    is_ie = true;
3013
0
    goto tls;
3014
3015
0
  case R_RISCV_TLS_GD_HI20:
3016
0
  tls:
3017
0
    if (h != NULL)
3018
0
      {
3019
0
        off = h->got.offset;
3020
0
        h->got.offset |= 1;
3021
0
      }
3022
0
    else
3023
0
      {
3024
0
        off = local_got_offsets[r_symndx];
3025
0
        local_got_offsets[r_symndx] |= 1;
3026
0
      }
3027
3028
0
    tls_type = _bfd_riscv_elf_tls_type (input_bfd, h, r_symndx);
3029
0
    BFD_ASSERT (tls_type & (GOT_TLS_IE | GOT_TLS_GD | GOT_TLSDESC));
3030
    /* When more than one TLS type is used, the GD slot comes first,
3031
       then IE, then finally TLSDESC.  */
3032
0
    ie_off = 0;
3033
0
    if (tls_type & GOT_TLS_GD)
3034
0
      ie_off += TLS_GD_GOT_ENTRY_SIZE;
3035
3036
0
    desc_off = ie_off;
3037
0
    if (tls_type & GOT_TLS_IE)
3038
0
      desc_off += TLS_IE_GOT_ENTRY_SIZE;
3039
3040
0
    if ((off & 1) != 0)
3041
0
      off &= ~1;
3042
0
    else
3043
0
      {
3044
0
        Elf_Internal_Rela outrel;
3045
0
        int indx = 0;
3046
0
        bool need_relocs = false;
3047
3048
0
        if (htab->elf.srelgot == NULL)
3049
0
    abort ();
3050
3051
0
        bool dyn = elf_hash_table (info)->dynamic_sections_created;
3052
0
        RISCV_TLS_GD_IE_NEED_DYN_RELOC (info, dyn, h, indx, need_relocs);
3053
3054
        /* The GOT entries have not been initialized yet.  Do it
3055
     now, and emit any relocations.  */
3056
0
        if (tls_type & GOT_TLS_GD)
3057
0
    {
3058
0
      if (need_relocs)
3059
0
        {
3060
0
          outrel.r_offset = sec_addr (htab->elf.sgot) + off;
3061
0
          outrel.r_addend = 0;
3062
0
          outrel.r_info = ELF64_R_INFO (indx, R_RISCV_TLS_DTPMOD64);
3063
0
          bfd_put_64 (output_bfd, 0,
3064
0
          htab->elf.sgot->contents + off);
3065
0
          riscv_elf_append_rela (output_bfd, htab->elf.srelgot, &outrel);
3066
0
          if (indx == 0)
3067
0
      {
3068
0
        BFD_ASSERT (! unresolved_reloc);
3069
0
        bfd_put_64 (output_bfd,
3070
0
              dtpoff (info, relocation),
3071
0
              (htab->elf.sgot->contents
3072
0
               + off + RISCV_ELF_WORD_BYTES));
3073
0
      }
3074
0
          else
3075
0
      {
3076
0
        bfd_put_64 (output_bfd, 0,
3077
0
              (htab->elf.sgot->contents
3078
0
               + off + RISCV_ELF_WORD_BYTES));
3079
0
        outrel.r_info = ELF64_R_INFO (indx, R_RISCV_TLS_DTPREL64);
3080
0
        outrel.r_offset += RISCV_ELF_WORD_BYTES;
3081
0
        riscv_elf_append_rela (output_bfd, htab->elf.srelgot, &outrel);
3082
0
      }
3083
0
        }
3084
0
      else
3085
0
        {
3086
          /* If we are not emitting relocations for a
3087
       general dynamic reference, then we must be in a
3088
       static link or an executable link with the
3089
       symbol binding locally.  Mark it as belonging
3090
       to module 1, the executable.  */
3091
0
          bfd_put_64 (output_bfd, 1,
3092
0
          htab->elf.sgot->contents + off);
3093
0
          bfd_put_64 (output_bfd,
3094
0
          dtpoff (info, relocation),
3095
0
          (htab->elf.sgot->contents
3096
0
           + off + RISCV_ELF_WORD_BYTES));
3097
0
       }
3098
0
    }
3099
3100
0
        if (tls_type & GOT_TLS_IE)
3101
0
    {
3102
0
      if (need_relocs)
3103
0
        {
3104
0
          bfd_put_64 (output_bfd, 0,
3105
0
          htab->elf.sgot->contents + off + ie_off);
3106
0
          outrel.r_offset = sec_addr (htab->elf.sgot)
3107
0
          + off + ie_off;
3108
0
          outrel.r_addend = 0;
3109
0
          if (indx == 0)
3110
0
      outrel.r_addend = tpoff (info, relocation);
3111
0
          outrel.r_info = ELF64_R_INFO (indx, R_RISCV_TLS_TPREL64);
3112
0
          riscv_elf_append_rela (output_bfd, htab->elf.srelgot, &outrel);
3113
0
        }
3114
0
      else
3115
0
        {
3116
0
          bfd_put_64 (output_bfd, tpoff (info, relocation),
3117
0
          htab->elf.sgot->contents + off + ie_off);
3118
0
        }
3119
0
    }
3120
3121
0
        if (tls_type & GOT_TLSDESC)
3122
0
    {
3123
      /* TLSDESC is always handled by the dynamic linker and always need
3124
       * a relocation.  */
3125
0
      bfd_put_64 (output_bfd, 0,
3126
0
            htab->elf.sgot->contents + off + desc_off);
3127
0
      outrel.r_offset = sec_addr (htab->elf.sgot)
3128
0
            + off + desc_off;
3129
0
      outrel.r_addend = 0;
3130
0
      if (indx == 0)
3131
0
        outrel.r_addend = tlsdescoff (info, relocation);
3132
0
      outrel.r_info = ELF64_R_INFO (indx, R_RISCV_TLSDESC);
3133
0
      riscv_elf_append_rela (output_bfd, htab->elf.srelgot, &outrel);
3134
0
    }
3135
0
      }
3136
3137
0
    BFD_ASSERT (off < (bfd_vma) -2);
3138
0
    relocation = sec_addr (htab->elf.sgot) + off;
3139
0
    if (is_ie)
3140
0
      relocation += ie_off;
3141
0
    else if (is_desc)
3142
0
      relocation += desc_off;
3143
0
    if (!riscv_record_pcrel_hi_reloc (&pcrel_relocs, pc,
3144
0
              relocation, r_type,
3145
0
              false))
3146
0
      r = bfd_reloc_overflow;
3147
0
    unresolved_reloc = false;
3148
0
    break;
3149
3150
0
  default:
3151
0
    r = bfd_reloc_notsupported;
3152
0
  }
3153
3154
      /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
3155
   because such sections are not SEC_ALLOC and thus ld.so will
3156
   not process them.  */
3157
0
      if (unresolved_reloc
3158
0
    && !((input_section->flags & SEC_DEBUGGING) != 0
3159
0
         && h->def_dynamic)
3160
0
    && _bfd_elf_section_offset (output_bfd, info, input_section,
3161
0
              rel->r_offset) != (bfd_vma) -1)
3162
0
  {
3163
0
    msg = bfd_asprintf (_("%%X%%P: unresolvable %s relocation against "
3164
0
        "symbol `%s'\n"),
3165
0
            howto->name,
3166
0
            h->root.root.string);
3167
0
    r = bfd_reloc_notsupported;
3168
0
  }
3169
3170
0
 do_relocation:
3171
0
      if (r == bfd_reloc_ok)
3172
0
  r = perform_relocation (howto, rel, relocation, input_section,
3173
0
        input_bfd, contents);
3174
3175
      /* We should have already detected the error and set message before.
3176
   If the error message isn't set since the linker runs out of memory
3177
   or we don't set it before, then we should set the default message
3178
   with the "internal error" string here.  */
3179
0
      switch (r)
3180
0
  {
3181
0
  case bfd_reloc_ok:
3182
0
    continue;
3183
3184
0
  case bfd_reloc_overflow:
3185
0
    info->callbacks->reloc_overflow
3186
0
      (info, (h ? &h->root : NULL), name, howto->name,
3187
0
       (bfd_vma) 0, input_bfd, input_section, rel->r_offset);
3188
0
    break;
3189
3190
0
  case bfd_reloc_undefined:
3191
0
    info->callbacks->undefined_symbol
3192
0
      (info, name, input_bfd, input_section, rel->r_offset,
3193
0
       true);
3194
0
    break;
3195
3196
0
  case bfd_reloc_outofrange:
3197
0
    if (msg == NULL)
3198
0
      msg = _("%X%P: internal error: out of range error\n");
3199
0
    break;
3200
3201
0
  case bfd_reloc_notsupported:
3202
0
    if (msg == NULL)
3203
0
      msg = _("%X%P: internal error: unsupported relocation error\n");
3204
0
    break;
3205
3206
0
  case bfd_reloc_dangerous:
3207
    /* The error message should already be set.  */
3208
0
    if (msg == NULL)
3209
0
      msg = _("dangerous relocation error");
3210
0
    info->callbacks->reloc_dangerous
3211
0
      (info, msg, input_bfd, input_section, rel->r_offset);
3212
0
    break;
3213
3214
0
  default:
3215
0
    msg = _("%X%P: internal error: unknown error\n");
3216
0
    break;
3217
0
  }
3218
3219
      /* Do not report error message for the dangerous relocation again.  */
3220
0
      if (msg && r != bfd_reloc_dangerous)
3221
0
  info->callbacks->einfo (msg);
3222
3223
      /* We already reported the error via a callback, so don't try to report
3224
   it again by returning false.  That leads to spurious errors.  */
3225
0
      ret = true;
3226
0
      goto out;
3227
0
    }
3228
3229
0
  ret = riscv_resolve_pcrel_lo_relocs (&pcrel_relocs);
3230
0
 out:
3231
0
  riscv_free_pcrel_relocs (&pcrel_relocs);
3232
0
  return ret;
3233
0
}
3234
3235
/* Finish up dynamic symbol handling.  We set the contents of various
3236
   dynamic sections here.  */
3237
3238
static bool
3239
riscv_elf_finish_dynamic_symbol (bfd *output_bfd,
3240
         struct bfd_link_info *info,
3241
         struct elf_link_hash_entry *h,
3242
         Elf_Internal_Sym *sym)
3243
0
{
3244
0
  struct riscv_elf_link_hash_table *htab = riscv_elf_hash_table (info);
3245
0
  const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
3246
3247
0
  if (h->plt.offset != (bfd_vma) -1)
3248
0
    {
3249
      /* We've decided to create a PLT entry for this symbol.  */
3250
0
      bfd_byte *loc;
3251
0
      bfd_vma i, header_address, plt_idx, got_offset, got_address;
3252
0
      uint32_t plt_entry[PLT_ENTRY_INSNS];
3253
0
      Elf_Internal_Rela rela;
3254
0
      asection *plt, *gotplt, *relplt;
3255
3256
      /* When building a static executable, use .iplt, .igot.plt and
3257
   .rela.iplt sections for STT_GNU_IFUNC symbols.  */
3258
0
      if (htab->elf.splt != NULL)
3259
0
        {
3260
0
          plt = htab->elf.splt;
3261
0
          gotplt = htab->elf.sgotplt;
3262
0
          relplt = htab->elf.srelplt;
3263
0
        }
3264
0
      else
3265
0
        {
3266
0
          plt = htab->elf.iplt;
3267
0
          gotplt = htab->elf.igotplt;
3268
0
          relplt = htab->elf.irelplt;
3269
0
        }
3270
3271
      /* This symbol has an entry in the procedure linkage table.  Set
3272
         it up.  */
3273
0
      if ((h->dynindx == -1
3274
0
     && !((h->forced_local || bfd_link_executable (info))
3275
0
    && h->def_regular
3276
0
    && h->type == STT_GNU_IFUNC))
3277
0
    || plt == NULL
3278
0
    || gotplt == NULL
3279
0
    || relplt == NULL)
3280
0
  abort ();
3281
3282
      /* Calculate the address of the PLT header.  */
3283
0
      header_address = sec_addr (plt);
3284
3285
      /* Calculate the index of the entry and the offset of .got.plt entry.
3286
   For static executables, we don't reserve anything.  */
3287
0
      if (plt == htab->elf.splt)
3288
0
  {
3289
0
    plt_idx = (h->plt.offset - PLT_HEADER_SIZE) / PLT_ENTRY_SIZE;
3290
0
    got_offset = GOTPLT_HEADER_SIZE + (plt_idx * GOT_ENTRY_SIZE);
3291
0
  }
3292
0
      else
3293
0
  {
3294
0
    plt_idx = h->plt.offset / PLT_ENTRY_SIZE;
3295
0
    got_offset = plt_idx * GOT_ENTRY_SIZE;
3296
0
  }
3297
3298
      /* Calculate the address of the .got.plt entry.  */
3299
0
      got_address = sec_addr (gotplt) + got_offset;
3300
3301
      /* Find out where the .plt entry should go.  */
3302
0
      loc = plt->contents + h->plt.offset;
3303
3304
      /* Fill in the PLT entry itself.  */
3305
0
      if (! riscv_make_plt_entry (output_bfd, got_address,
3306
0
          header_address + h->plt.offset,
3307
0
          plt_entry))
3308
0
  return false;
3309
3310
0
      for (i = 0; i < PLT_ENTRY_INSNS; i++)
3311
0
  bfd_putl32 (plt_entry[i], loc + 4*i);
3312
3313
      /* Fill in the initial value of the .got.plt entry.  */
3314
0
      loc = gotplt->contents + (got_address - sec_addr (gotplt));
3315
0
      bfd_put_64 (output_bfd, sec_addr (plt), loc);
3316
3317
0
      rela.r_offset = got_address;
3318
3319
0
      if (h->dynindx == -1
3320
0
    || ((bfd_link_executable (info)
3321
0
         || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
3322
0
        && h->def_regular
3323
0
        && h->type == STT_GNU_IFUNC))
3324
0
  {
3325
0
    info->callbacks->minfo (_("Local IFUNC function `%s' in %pB\n"),
3326
0
          h->root.root.string,
3327
0
          h->root.u.def.section->owner);
3328
3329
    /* If an STT_GNU_IFUNC symbol is locally defined, generate
3330
       R_RISCV_IRELATIVE instead of R_RISCV_JUMP_SLOT.  */
3331
0
    asection *sec = h->root.u.def.section;
3332
0
    rela.r_info = ELF64_R_INFO (0, R_RISCV_IRELATIVE);
3333
0
    rela.r_addend = h->root.u.def.value
3334
0
        + sec->output_section->vma
3335
0
        + sec->output_offset;
3336
0
  }
3337
0
      else
3338
0
  {
3339
    /* Fill in the entry in the .rela.plt section.  */
3340
0
    rela.r_info = ELF64_R_INFO (h->dynindx, R_RISCV_JUMP_SLOT);
3341
0
    rela.r_addend = 0;
3342
0
  }
3343
3344
0
      loc = relplt->contents + plt_idx * sizeof (Elf64_External_Rela);
3345
0
      bed->s->swap_reloca_out (output_bfd, &rela, loc);
3346
3347
0
      if (!h->def_regular)
3348
0
  {
3349
    /* Mark the symbol as undefined, rather than as defined in
3350
       the .plt section.  Leave the value alone.  */
3351
0
    sym->st_shndx = SHN_UNDEF;
3352
    /* If the symbol is weak, we do need to clear the value.
3353
       Otherwise, the PLT entry would provide a definition for
3354
       the symbol even if the symbol wasn't defined anywhere,
3355
       and so the symbol would never be NULL.  */
3356
0
    if (!h->ref_regular_nonweak)
3357
0
      sym->st_value = 0;
3358
0
  }
3359
0
    }
3360
3361
0
  if (h->got.offset != (bfd_vma) -1
3362
0
      && !(riscv_elf_hash_entry (h)->tls_type & (GOT_TLS_GD | GOT_TLS_IE | GOT_TLSDESC))
3363
0
      && !UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
3364
0
    {
3365
0
      asection *sgot;
3366
0
      asection *srela;
3367
0
      Elf_Internal_Rela rela;
3368
0
      bool use_elf_append_rela = true;
3369
3370
      /* This symbol has an entry in the GOT.  Set it up.  */
3371
3372
0
      sgot = htab->elf.sgot;
3373
0
      srela = htab->elf.srelgot;
3374
0
      BFD_ASSERT (sgot != NULL && srela != NULL);
3375
3376
0
      rela.r_offset = sec_addr (sgot) + (h->got.offset &~ (bfd_vma) 1);
3377
3378
      /* Handle the ifunc symbol in GOT entry.  */
3379
0
      if (h->def_regular
3380
0
    && h->type == STT_GNU_IFUNC)
3381
0
  {
3382
0
    if (h->plt.offset == (bfd_vma) -1)
3383
0
      {
3384
        /* STT_GNU_IFUNC is referenced without PLT.  */
3385
3386
0
        if (htab->elf.splt == NULL)
3387
0
    {
3388
      /* Use .rela.iplt section to store .got relocations
3389
         in static executable.  */
3390
0
      srela = htab->elf.irelplt;
3391
3392
      /* Do not use riscv_elf_append_rela to add dynamic
3393
         relocs.  */
3394
0
      use_elf_append_rela = false;
3395
0
    }
3396
3397
0
        if (SYMBOL_REFERENCES_LOCAL (info, h))
3398
0
    {
3399
0
      info->callbacks->minfo (_("Local IFUNC function `%s' in %pB\n"),
3400
0
            h->root.root.string,
3401
0
            h->root.u.def.section->owner);
3402
3403
0
      rela.r_info = ELF64_R_INFO (0, R_RISCV_IRELATIVE);
3404
0
      rela.r_addend = (h->root.u.def.value
3405
0
           + h->root.u.def.section->output_section->vma
3406
0
           + h->root.u.def.section->output_offset);
3407
0
    }
3408
0
        else
3409
0
    {
3410
      /* Generate R_RISCV_64.  */
3411
0
      BFD_ASSERT ((h->got.offset & 1) == 0);
3412
0
      BFD_ASSERT (h->dynindx != -1);
3413
0
      rela.r_info = ELF64_R_INFO (h->dynindx, R_RISCV_64);
3414
0
      rela.r_addend = 0;
3415
0
    }
3416
0
      }
3417
0
    else if (bfd_link_pic (info))
3418
0
      {
3419
        /* Generate R_RISCV_64.  */
3420
0
        BFD_ASSERT ((h->got.offset & 1) == 0);
3421
0
        BFD_ASSERT (h->dynindx != -1);
3422
0
        rela.r_info = ELF64_R_INFO (h->dynindx, R_RISCV_64);
3423
0
        rela.r_addend = 0;
3424
0
      }
3425
0
    else
3426
0
      {
3427
0
        asection *plt;
3428
3429
0
        if (!h->pointer_equality_needed)
3430
0
    abort ();
3431
3432
        /* For non-shared object, we can't use .got.plt, which
3433
     contains the real function address if we need pointer
3434
     equality.  We load the GOT entry with the PLT entry.  */
3435
0
        plt = htab->elf.splt ? htab->elf.splt : htab->elf.iplt;
3436
0
        bfd_put_64 (output_bfd, (plt->output_section->vma
3437
0
               + plt->output_offset
3438
0
               + h->plt.offset),
3439
0
        htab->elf.sgot->contents
3440
0
        + (h->got.offset & ~(bfd_vma) 1));
3441
0
        return true;
3442
0
      }
3443
0
  }
3444
0
      else if (bfd_link_pic (info)
3445
0
         && SYMBOL_REFERENCES_LOCAL (info, h))
3446
0
  {
3447
    /* If this is a local symbol reference, we just want to emit
3448
       a RELATIVE reloc.  This can happen if it is a -Bsymbolic link,
3449
       or a pie link, or the symbol was forced to be local because
3450
       of a version file.  The entry in the global offset table will
3451
       already have been initialized in the relocate_section function.  */
3452
0
    BFD_ASSERT ((h->got.offset & 1) != 0);
3453
0
    asection *sec = h->root.u.def.section;
3454
0
    rela.r_info = ELF64_R_INFO (0, R_RISCV_RELATIVE);
3455
0
    rela.r_addend = (h->root.u.def.value
3456
0
         + sec->output_section->vma
3457
0
         + sec->output_offset);
3458
0
  }
3459
0
      else
3460
0
  {
3461
0
    BFD_ASSERT ((h->got.offset & 1) == 0);
3462
0
    BFD_ASSERT (h->dynindx != -1);
3463
0
    rela.r_info = ELF64_R_INFO (h->dynindx, R_RISCV_64);
3464
0
    rela.r_addend = 0;
3465
0
  }
3466
3467
0
      bfd_put_64 (output_bfd, 0,
3468
0
      sgot->contents + (h->got.offset & ~(bfd_vma) 1));
3469
3470
0
      if (use_elf_append_rela)
3471
0
  riscv_elf_append_rela (output_bfd, srela, &rela);
3472
0
      else
3473
0
  {
3474
    /* Use riscv_elf_append_rela to add the dynamic relocs into
3475
       .rela.iplt may cause the overwrite problems.  Since we insert
3476
       the relocs for PLT didn't handle the reloc_index of .rela.iplt,
3477
       but the riscv_elf_append_rela adds the relocs to the place
3478
       that are calculated from the reloc_index (in seqential).
3479
3480
       One solution is that add these dynamic relocs (GOT IFUNC)
3481
       from the last of .rela.iplt section.  */
3482
0
    bfd_vma iplt_idx = htab->last_iplt_index--;
3483
0
    bfd_byte *loc = srela->contents
3484
0
        + iplt_idx * sizeof (Elf64_External_Rela);
3485
0
    bed->s->swap_reloca_out (output_bfd, &rela, loc);
3486
0
  }
3487
0
    }
3488
3489
0
  if (h->needs_copy)
3490
0
    {
3491
0
      Elf_Internal_Rela rela;
3492
0
      asection *s;
3493
3494
      /* This symbols needs a copy reloc.  Set it up.  */
3495
0
      BFD_ASSERT (h->dynindx != -1);
3496
3497
0
      rela.r_offset = sec_addr (h->root.u.def.section) + h->root.u.def.value;
3498
0
      rela.r_info = ELF64_R_INFO (h->dynindx, R_RISCV_COPY);
3499
0
      rela.r_addend = 0;
3500
0
      if (h->root.u.def.section == htab->elf.sdynrelro)
3501
0
  s = htab->elf.sreldynrelro;
3502
0
      else
3503
0
  s = htab->elf.srelbss;
3504
0
      riscv_elf_append_rela (output_bfd, s, &rela);
3505
0
    }
3506
3507
  /* Mark some specially defined symbols as absolute.  */
3508
0
  if (h == htab->elf.hdynamic
3509
0
      || (h == htab->elf.hgot || h == htab->elf.hplt))
3510
0
    sym->st_shndx = SHN_ABS;
3511
3512
0
  return true;
3513
0
}
3514
3515
/* Finish up local dynamic symbol handling.  We set the contents of
3516
   various dynamic sections here.  */
3517
3518
static int
3519
riscv_elf_finish_local_dynamic_symbol (void **slot, void *inf)
3520
0
{
3521
0
  struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) *slot;
3522
0
  struct bfd_link_info *info = (struct bfd_link_info *) inf;
3523
3524
0
  return riscv_elf_finish_dynamic_symbol (info->output_bfd, info, h, NULL);
3525
0
}
3526
3527
/* Finish up the dynamic sections.  */
3528
3529
static bool
3530
riscv_finish_dyn (bfd *output_bfd, struct bfd_link_info *info,
3531
      bfd *dynobj, asection *sdyn)
3532
0
{
3533
0
  struct riscv_elf_link_hash_table *htab = riscv_elf_hash_table (info);
3534
0
  const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
3535
0
  size_t dynsize = bed->s->sizeof_dyn;
3536
0
  bfd_byte *dyncon, *dynconend;
3537
3538
0
  dynconend = sdyn->contents + sdyn->size;
3539
0
  for (dyncon = sdyn->contents; dyncon < dynconend; dyncon += dynsize)
3540
0
    {
3541
0
      Elf_Internal_Dyn dyn;
3542
0
      asection *s;
3543
3544
0
      bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
3545
3546
0
      switch (dyn.d_tag)
3547
0
  {
3548
0
  case DT_PLTGOT:
3549
0
    s = htab->elf.sgotplt;
3550
0
    dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
3551
0
    break;
3552
0
  case DT_JMPREL:
3553
0
    s = htab->elf.srelplt;
3554
0
    dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
3555
0
    break;
3556
0
  case DT_PLTRELSZ:
3557
0
    s = htab->elf.srelplt;
3558
0
    dyn.d_un.d_val = s->size;
3559
0
    break;
3560
0
  default:
3561
0
    continue;
3562
0
  }
3563
3564
0
      bed->s->swap_dyn_out (output_bfd, &dyn, dyncon);
3565
0
    }
3566
0
  return true;
3567
0
}
3568
3569
static bool
3570
riscv_elf_finish_dynamic_sections (bfd *output_bfd,
3571
           struct bfd_link_info *info)
3572
0
{
3573
0
  bfd *dynobj;
3574
0
  asection *sdyn;
3575
0
  struct riscv_elf_link_hash_table *htab;
3576
3577
0
  htab = riscv_elf_hash_table (info);
3578
0
  BFD_ASSERT (htab != NULL);
3579
0
  dynobj = htab->elf.dynobj;
3580
3581
0
  sdyn = bfd_get_linker_section (dynobj, ".dynamic");
3582
3583
0
  if (elf_hash_table (info)->dynamic_sections_created)
3584
0
    {
3585
0
      asection *splt;
3586
0
      bool ret;
3587
3588
0
      splt = htab->elf.splt;
3589
0
      BFD_ASSERT (splt != NULL && sdyn != NULL);
3590
3591
0
      ret = riscv_finish_dyn (output_bfd, info, dynobj, sdyn);
3592
3593
0
      if (!ret)
3594
0
  return ret;
3595
3596
      /* Fill in the head and tail entries in the procedure linkage table.  */
3597
0
      if (splt->size > 0)
3598
0
  {
3599
0
    int i;
3600
0
    uint32_t plt_header[PLT_HEADER_INSNS];
3601
0
    ret = riscv_make_plt_header (output_bfd,
3602
0
               sec_addr (htab->elf.sgotplt),
3603
0
               sec_addr (splt), plt_header);
3604
0
    if (!ret)
3605
0
      return ret;
3606
3607
0
    for (i = 0; i < PLT_HEADER_INSNS; i++)
3608
0
      bfd_putl32 (plt_header[i], splt->contents + 4*i);
3609
3610
0
    elf_section_data (splt->output_section)->this_hdr.sh_entsize
3611
0
      = PLT_ENTRY_SIZE;
3612
0
  }
3613
0
    }
3614
3615
0
  if (htab->elf.sgotplt && htab->elf.sgotplt->size > 0)
3616
0
    {
3617
0
      asection *output_section = htab->elf.sgotplt->output_section;
3618
3619
0
      if (bfd_is_abs_section (output_section))
3620
0
  {
3621
0
    (*_bfd_error_handler)
3622
0
      (_("discarded output section: `%pA'"), htab->elf.sgotplt);
3623
0
    return false;
3624
0
  }
3625
3626
      /* Write the first two entries in .got.plt, needed for the dynamic
3627
   linker.  */
3628
0
      bfd_put_64 (output_bfd, (bfd_vma) -1, htab->elf.sgotplt->contents);
3629
0
      bfd_put_64 (output_bfd, (bfd_vma) 0,
3630
0
      htab->elf.sgotplt->contents + GOT_ENTRY_SIZE);
3631
3632
0
      elf_section_data (output_section)->this_hdr.sh_entsize = GOT_ENTRY_SIZE;
3633
0
    }
3634
3635
0
  if (htab->elf.sgot && htab->elf.sgot->size > 0)
3636
0
    {
3637
0
      asection *output_section = htab->elf.sgot->output_section;
3638
3639
0
      if (!bfd_is_abs_section (output_section))
3640
0
  {
3641
    /* Set the first entry in the global offset table to the address of
3642
       the dynamic section.  */
3643
0
    bfd_vma val = sdyn ? sec_addr (sdyn) : 0;
3644
0
    bfd_put_64 (output_bfd, val, htab->elf.sgot->contents);
3645
3646
0
    elf_section_data (output_section)->this_hdr.sh_entsize = GOT_ENTRY_SIZE;
3647
0
  }
3648
0
    }
3649
3650
  /* Fill PLT and GOT entries for local STT_GNU_IFUNC symbols.  */
3651
0
  htab_traverse (htab->loc_hash_table,
3652
0
     riscv_elf_finish_local_dynamic_symbol,
3653
0
     info);
3654
3655
0
  return true;
3656
0
}
3657
3658
/* Return address for Ith PLT stub in section PLT, for relocation REL
3659
   or (bfd_vma) -1 if it should not be included.  */
3660
3661
static bfd_vma
3662
riscv_elf_plt_sym_val (bfd_vma i, const asection *plt,
3663
           const arelent *rel ATTRIBUTE_UNUSED)
3664
0
{
3665
0
  return plt->vma + PLT_HEADER_SIZE + i * PLT_ENTRY_SIZE;
3666
0
}
3667
3668
/* Used to decide how to sort relocs in an optimal manner for the
3669
   dynamic linker, before writing them out.  */
3670
3671
static enum elf_reloc_type_class
3672
riscv_reloc_type_class (const struct bfd_link_info *info,
3673
      const asection *rel_sec ATTRIBUTE_UNUSED,
3674
      const Elf_Internal_Rela *rela)
3675
0
{
3676
0
  struct riscv_elf_link_hash_table *htab = riscv_elf_hash_table (info);
3677
3678
0
  if (htab->elf.dynsym != NULL
3679
0
      && htab->elf.dynsym->contents != NULL)
3680
0
    {
3681
      /* Check relocation against STT_GNU_IFUNC symbol if there are
3682
   dynamic symbols.  */
3683
0
      bfd *abfd = info->output_bfd;
3684
0
      const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3685
0
      unsigned long r_symndx = ELF64_R_SYM (rela->r_info);
3686
0
      if (r_symndx != STN_UNDEF)
3687
0
  {
3688
0
    Elf_Internal_Sym sym;
3689
0
    if (!bed->s->swap_symbol_in (abfd,
3690
0
               (htab->elf.dynsym->contents
3691
0
          + r_symndx * bed->s->sizeof_sym),
3692
0
               0, &sym))
3693
0
      {
3694
        /* xgettext:c-format */
3695
0
        _bfd_error_handler (_("%pB symbol number %lu references"
3696
0
            " nonexistent SHT_SYMTAB_SHNDX section"),
3697
0
          abfd, r_symndx);
3698
        /* Ideally an error class should be returned here.  */
3699
0
      }
3700
0
    else if (ELF_ST_TYPE (sym.st_info) == STT_GNU_IFUNC)
3701
0
      return reloc_class_ifunc;
3702
0
  }
3703
0
    }
3704
3705
0
  switch (ELF64_R_TYPE (rela->r_info))
3706
0
    {
3707
0
    case R_RISCV_IRELATIVE:
3708
0
      return reloc_class_ifunc;
3709
0
    case R_RISCV_RELATIVE:
3710
0
      return reloc_class_relative;
3711
0
    case R_RISCV_JUMP_SLOT:
3712
0
      return reloc_class_plt;
3713
0
    case R_RISCV_COPY:
3714
0
      return reloc_class_copy;
3715
0
    default:
3716
0
      return reloc_class_normal;
3717
0
    }
3718
0
}
3719
3720
/* Given the ELF header flags in FLAGS, it returns a string that describes the
3721
   float ABI.  */
3722
3723
static const char *
3724
riscv_float_abi_string (flagword flags)
3725
0
{
3726
0
  switch (flags & EF_RISCV_FLOAT_ABI)
3727
0
    {
3728
0
    case EF_RISCV_FLOAT_ABI_SOFT:
3729
0
      return "soft-float";
3730
0
      break;
3731
0
    case EF_RISCV_FLOAT_ABI_SINGLE:
3732
0
      return "single-float";
3733
0
      break;
3734
0
    case EF_RISCV_FLOAT_ABI_DOUBLE:
3735
0
      return "double-float";
3736
0
      break;
3737
0
    case EF_RISCV_FLOAT_ABI_QUAD:
3738
0
      return "quad-float";
3739
0
      break;
3740
0
    default:
3741
0
      abort ();
3742
0
    }
3743
0
}
3744
3745
/* The information of architecture elf attributes.  */
3746
static riscv_subset_list_t in_subsets;
3747
static riscv_subset_list_t out_subsets;
3748
static riscv_subset_list_t merged_subsets;
3749
3750
/* Predicator for standard extension.  */
3751
3752
static bool
3753
riscv_std_ext_p (const char *name)
3754
0
{
3755
0
  return (strlen (name) == 1) && (name[0] != 'x') && (name[0] != 's');
3756
0
}
3757
3758
/* Update the output subset's version to match the input when the input
3759
   subset's version is newer.  */
3760
3761
static void
3762
riscv_update_subset_version (struct riscv_subset_t *in,
3763
           struct riscv_subset_t *out)
3764
0
{
3765
0
  if (in == NULL || out == NULL)
3766
0
    return;
3767
3768
  /* Update the output ISA versions to the newest ones, but otherwise don't
3769
     provide any errors or warnings about mis-matched ISA versions as it's
3770
     generally too tricky to check for these at link time. */
3771
0
  if ((in->major_version > out->major_version)
3772
0
      || (in->major_version == out->major_version
3773
0
    && in->minor_version > out->minor_version)
3774
0
      || (out->major_version == RISCV_UNKNOWN_VERSION))
3775
0
    {
3776
0
      out->major_version = in->major_version;
3777
0
      out->minor_version = in->minor_version;
3778
0
    }
3779
0
}
3780
3781
/* Return true if subset is 'i' or 'e'.  */
3782
3783
static bool
3784
riscv_i_or_e_p (bfd *ibfd,
3785
    const char *arch,
3786
    struct riscv_subset_t *subset)
3787
0
{
3788
0
  if ((strcasecmp (subset->name, "e") != 0)
3789
0
      && (strcasecmp (subset->name, "i") != 0))
3790
0
    {
3791
0
      _bfd_error_handler
3792
0
  (_("error: %pB: corrupted ISA string '%s'.  "
3793
0
     "First letter should be 'i' or 'e' but got '%s'"),
3794
0
     ibfd, arch, subset->name);
3795
0
      return false;
3796
0
    }
3797
0
  return true;
3798
0
}
3799
3800
/* Merge standard extensions.
3801
3802
   Return Value:
3803
     Return FALSE if failed to merge.
3804
3805
   Arguments:
3806
     `bfd`: bfd handler.
3807
     `in_arch`: Raw ISA string for input object.
3808
     `out_arch`: Raw ISA string for output object.
3809
     `pin`: Subset list for input object.
3810
     `pout`: Subset list for output object.  */
3811
3812
static bool
3813
riscv_merge_std_ext (bfd *ibfd,
3814
         const char *in_arch,
3815
         const char *out_arch,
3816
         struct riscv_subset_t **pin,
3817
         struct riscv_subset_t **pout)
3818
0
{
3819
0
  const char *standard_exts = "mafdqlcbjtpvnh";
3820
0
  const char *p;
3821
0
  struct riscv_subset_t *in = *pin;
3822
0
  struct riscv_subset_t *out = *pout;
3823
3824
  /* First letter should be 'i' or 'e'.  */
3825
0
  if (!riscv_i_or_e_p (ibfd, in_arch, in))
3826
0
    return false;
3827
3828
0
  if (!riscv_i_or_e_p (ibfd, out_arch, out))
3829
0
    return false;
3830
3831
0
  if (strcasecmp (in->name, out->name) != 0)
3832
0
    {
3833
      /* TODO: We might allow merge 'i' with 'e'.  */
3834
0
      _bfd_error_handler
3835
0
  (_("error: %pB: mis-matched ISA string to merge '%s' and '%s'"),
3836
0
   ibfd, in->name, out->name);
3837
0
      return false;
3838
0
    }
3839
3840
0
  riscv_update_subset_version(in, out);
3841
0
  riscv_add_subset (&merged_subsets,
3842
0
        out->name, out->major_version, out->minor_version);
3843
3844
0
  in = in->next;
3845
0
  out = out->next;
3846
3847
  /* Handle standard extension first.  */
3848
0
  for (p = standard_exts; *p; ++p)
3849
0
    {
3850
0
      struct riscv_subset_t *ext_in, *ext_out, *ext_merged;
3851
0
      char find_ext[2] = {*p, '\0'};
3852
0
      bool find_in, find_out;
3853
3854
0
      find_in = riscv_lookup_subset (&in_subsets, find_ext, &ext_in);
3855
0
      find_out = riscv_lookup_subset (&out_subsets, find_ext, &ext_out);
3856
3857
0
      if (!find_in && !find_out)
3858
0
  continue;
3859
3860
0
      if (find_in && find_out)
3861
0
  riscv_update_subset_version(ext_in, ext_out);
3862
3863
0
      ext_merged = find_out ? ext_out : ext_in;
3864
0
      riscv_add_subset (&merged_subsets, ext_merged->name,
3865
0
      ext_merged->major_version, ext_merged->minor_version);
3866
0
    }
3867
3868
  /* Skip all standard extensions.  */
3869
0
  while ((in != NULL) && riscv_std_ext_p (in->name)) in = in->next;
3870
0
  while ((out != NULL) && riscv_std_ext_p (out->name)) out = out->next;
3871
3872
0
  *pin = in;
3873
0
  *pout = out;
3874
3875
0
  return true;
3876
0
}
3877
3878
/* Merge multi letter extensions.  PIN is a pointer to the head of the input
3879
   object subset list.  Likewise for POUT and the output object.  Return TRUE
3880
   on success and FALSE when a conflict is found.  */
3881
3882
static bool
3883
riscv_merge_multi_letter_ext (riscv_subset_t **pin,
3884
            riscv_subset_t **pout)
3885
0
{
3886
0
  riscv_subset_t *in = *pin;
3887
0
  riscv_subset_t *out = *pout;
3888
0
  riscv_subset_t *tail;
3889
3890
0
  int cmp;
3891
3892
0
  while (in && out)
3893
0
    {
3894
0
      cmp = riscv_compare_subsets (in->name, out->name);
3895
3896
0
      if (cmp < 0)
3897
0
  {
3898
    /* `in' comes before `out', append `in' and increment.  */
3899
0
    riscv_add_subset (&merged_subsets, in->name, in->major_version,
3900
0
          in->minor_version);
3901
0
    in = in->next;
3902
0
  }
3903
0
      else if (cmp > 0)
3904
0
  {
3905
    /* `out' comes before `in', append `out' and increment.  */
3906
0
    riscv_add_subset (&merged_subsets, out->name, out->major_version,
3907
0
          out->minor_version);
3908
0
    out = out->next;
3909
0
  }
3910
0
      else
3911
0
  {
3912
    /* Both present, check version and increment both.  */
3913
0
    riscv_update_subset_version (in, out);
3914
3915
0
    riscv_add_subset (&merged_subsets, out->name, out->major_version,
3916
0
          out->minor_version);
3917
0
    out = out->next;
3918
0
    in = in->next;
3919
0
  }
3920
0
    }
3921
3922
0
  if (in || out)
3923
0
    {
3924
      /* If we're here, either `in' or `out' is running longer than
3925
   the other. So, we need to append the corresponding tail.  */
3926
0
      tail = in ? in : out;
3927
0
      while (tail)
3928
0
  {
3929
0
    riscv_add_subset (&merged_subsets, tail->name, tail->major_version,
3930
0
          tail->minor_version);
3931
0
    tail = tail->next;
3932
0
  }
3933
0
    }
3934
3935
0
  return true;
3936
0
}
3937
3938
/* Merge Tag_RISCV_arch attribute.  */
3939
3940
static char *
3941
riscv_merge_arch_attr_info (bfd *ibfd, char *in_arch, char *out_arch)
3942
0
{
3943
0
  riscv_subset_t *in, *out;
3944
0
  static char *merged_arch_str = NULL;
3945
3946
0
  unsigned xlen_in, xlen_out;
3947
0
  merged_subsets.head = NULL;
3948
0
  merged_subsets.tail = NULL;
3949
3950
0
  riscv_parse_subset_t riscv_rps_ld_in =
3951
0
    {&in_subsets, _bfd_error_handler, &xlen_in, NULL, false};
3952
0
  riscv_parse_subset_t riscv_rps_ld_out =
3953
0
    {&out_subsets, _bfd_error_handler, &xlen_out, NULL, false};
3954
3955
0
  if (in_arch == NULL && out_arch == NULL)
3956
0
    return NULL;
3957
0
  if (in_arch == NULL && out_arch != NULL)
3958
0
    return out_arch;
3959
0
  if (in_arch != NULL && out_arch == NULL)
3960
0
    return in_arch;
3961
3962
  /* Parse subset from ISA string.  */
3963
0
  if (!riscv_parse_subset (&riscv_rps_ld_in, in_arch))
3964
0
    return NULL;
3965
0
  if (!riscv_parse_subset (&riscv_rps_ld_out, out_arch))
3966
0
    return NULL;
3967
3968
  /* Checking XLEN.  */
3969
0
  if (xlen_out != xlen_in)
3970
0
    {
3971
0
      _bfd_error_handler
3972
0
  (_("error: %pB: ISA string of input (%s) doesn't match "
3973
0
     "output (%s)"), ibfd, in_arch, out_arch);
3974
0
      return NULL;
3975
0
    }
3976
3977
  /* Merge subset list.  */
3978
0
  in = in_subsets.head;
3979
0
  out = out_subsets.head;
3980
3981
  /* Merge standard extension.  */
3982
0
  if (!riscv_merge_std_ext (ibfd, in_arch, out_arch, &in, &out))
3983
0
    return NULL;
3984
3985
  /* Merge all non-single letter extensions with single call.  */
3986
0
  if (!riscv_merge_multi_letter_ext (&in, &out))
3987
0
    return NULL;
3988
3989
0
  if (xlen_in != xlen_out)
3990
0
    {
3991
0
      _bfd_error_handler
3992
0
  (_("error: %pB: XLEN of input (%u) doesn't match "
3993
0
     "output (%u)"), ibfd, xlen_in, xlen_out);
3994
0
      return NULL;
3995
0
    }
3996
3997
0
  if (xlen_in != ARCH_SIZE)
3998
0
    {
3999
0
      _bfd_error_handler
4000
0
  (_("error: %pB: unsupported XLEN (%u), you might be "
4001
0
     "using wrong emulation"), ibfd, xlen_in);
4002
0
      return NULL;
4003
0
    }
4004
4005
  /* Free the previous merged_arch_str which called xmalloc.  */
4006
0
  free (merged_arch_str);
4007
4008
0
  merged_arch_str = riscv_arch_str (ARCH_SIZE, &merged_subsets,
4009
0
            false/* update */);
4010
4011
  /* Release the subset lists.  */
4012
0
  riscv_release_subset_list (&in_subsets);
4013
0
  riscv_release_subset_list (&out_subsets);
4014
0
  riscv_release_subset_list (&merged_subsets);
4015
4016
0
  return merged_arch_str;
4017
0
}
4018
4019
/* Merge object attributes from IBFD into output_bfd of INFO.
4020
   Raise an error if there are conflicting attributes.  */
4021
4022
static bool
4023
riscv_merge_attributes (bfd *ibfd, struct bfd_link_info *info)
4024
0
{
4025
0
  bfd *obfd = info->output_bfd;
4026
0
  obj_attribute *in_attr;
4027
0
  obj_attribute *out_attr;
4028
0
  bool result = true;
4029
0
  bool priv_attrs_merged = false;
4030
0
  const char *sec_name = get_elf_backend_data (ibfd)->obj_attrs_section;
4031
0
  unsigned int i;
4032
4033
  /* Skip linker created files.  */
4034
0
  if (ibfd->flags & BFD_LINKER_CREATED)
4035
0
    return true;
4036
4037
  /* Skip any input that doesn't have an attribute section.
4038
     This enables to link object files without attribute section with
4039
     any others.  */
4040
0
  if (bfd_get_section_by_name (ibfd, sec_name) == NULL)
4041
0
    return true;
4042
4043
0
  if (!elf_known_obj_attributes_proc (obfd)[0].i)
4044
0
    {
4045
      /* This is the first object.  Copy the attributes.  */
4046
0
      _bfd_elf_copy_obj_attributes (ibfd, obfd);
4047
4048
0
      out_attr = elf_known_obj_attributes_proc (obfd);
4049
4050
      /* Use the Tag_null value to indicate the attributes have been
4051
   initialized.  */
4052
0
      out_attr[0].i = 1;
4053
4054
0
      return true;
4055
0
    }
4056
4057
0
  in_attr = elf_known_obj_attributes_proc (ibfd);
4058
0
  out_attr = elf_known_obj_attributes_proc (obfd);
4059
4060
0
  for (i = LEAST_KNOWN_OBJ_ATTRIBUTE; i < NUM_KNOWN_OBJ_ATTRIBUTES; i++)
4061
0
    {
4062
0
    switch (i)
4063
0
      {
4064
0
      case Tag_RISCV_arch:
4065
0
  if (!out_attr[Tag_RISCV_arch].s)
4066
0
    out_attr[Tag_RISCV_arch].s = in_attr[Tag_RISCV_arch].s;
4067
0
  else if (in_attr[Tag_RISCV_arch].s
4068
0
     && out_attr[Tag_RISCV_arch].s)
4069
0
    {
4070
      /* Check compatible.  */
4071
0
      char *merged_arch =
4072
0
    riscv_merge_arch_attr_info (ibfd,
4073
0
              in_attr[Tag_RISCV_arch].s,
4074
0
              out_attr[Tag_RISCV_arch].s);
4075
0
      if (merged_arch == NULL)
4076
0
        {
4077
0
    result = false;
4078
0
    out_attr[Tag_RISCV_arch].s = "";
4079
0
        }
4080
0
      else
4081
0
        out_attr[Tag_RISCV_arch].s = merged_arch;
4082
0
    }
4083
0
  break;
4084
4085
0
      case Tag_RISCV_priv_spec:
4086
0
      case Tag_RISCV_priv_spec_minor:
4087
0
      case Tag_RISCV_priv_spec_revision:
4088
  /* If we have handled the privileged elf attributes, then skip it.  */
4089
0
  if (!priv_attrs_merged)
4090
0
    {
4091
0
      unsigned int Tag_a = Tag_RISCV_priv_spec;
4092
0
      unsigned int Tag_b = Tag_RISCV_priv_spec_minor;
4093
0
      unsigned int Tag_c = Tag_RISCV_priv_spec_revision;
4094
0
      enum riscv_spec_class in_priv_spec = PRIV_SPEC_CLASS_NONE;
4095
0
      enum riscv_spec_class out_priv_spec = PRIV_SPEC_CLASS_NONE;
4096
4097
      /* Get the privileged spec class from elf attributes.  */
4098
0
      riscv_get_priv_spec_class_from_numbers (in_attr[Tag_a].i,
4099
0
                in_attr[Tag_b].i,
4100
0
                in_attr[Tag_c].i,
4101
0
                &in_priv_spec);
4102
0
      riscv_get_priv_spec_class_from_numbers (out_attr[Tag_a].i,
4103
0
                out_attr[Tag_b].i,
4104
0
                out_attr[Tag_c].i,
4105
0
                &out_priv_spec);
4106
4107
      /* Allow to link the object without the privileged specs.  */
4108
0
      if (out_priv_spec == PRIV_SPEC_CLASS_NONE)
4109
0
        {
4110
0
    out_attr[Tag_a].i = in_attr[Tag_a].i;
4111
0
    out_attr[Tag_b].i = in_attr[Tag_b].i;
4112
0
    out_attr[Tag_c].i = in_attr[Tag_c].i;
4113
0
        }
4114
0
      else if (in_priv_spec != PRIV_SPEC_CLASS_NONE
4115
0
         && in_priv_spec != out_priv_spec)
4116
0
        {
4117
    /* The abandoned privileged spec v1.9.1 can not be linked with
4118
       others since the conflicts.  Keep the check since compatible
4119
       issue.  */
4120
0
    if (in_priv_spec == PRIV_SPEC_CLASS_1P9P1
4121
0
        || out_priv_spec == PRIV_SPEC_CLASS_1P9P1)
4122
0
      {
4123
0
        _bfd_error_handler
4124
0
          (_("warning: privileged spec version 1.9.1 can not be "
4125
0
       "linked with other spec versions"));
4126
0
      }
4127
4128
    /* Update the output privileged spec to the newest one.  */
4129
0
    if (in_priv_spec > out_priv_spec)
4130
0
      {
4131
0
        out_attr[Tag_a].i = in_attr[Tag_a].i;
4132
0
        out_attr[Tag_b].i = in_attr[Tag_b].i;
4133
0
        out_attr[Tag_c].i = in_attr[Tag_c].i;
4134
0
      }
4135
0
        }
4136
0
      priv_attrs_merged = true;
4137
0
    }
4138
0
  break;
4139
4140
0
      case Tag_RISCV_unaligned_access:
4141
0
  out_attr[i].i |= in_attr[i].i;
4142
0
  break;
4143
4144
0
      case Tag_RISCV_stack_align:
4145
0
  if (out_attr[i].i == 0)
4146
0
    out_attr[i].i = in_attr[i].i;
4147
0
  else if (in_attr[i].i != 0
4148
0
     && out_attr[i].i != 0
4149
0
     && out_attr[i].i != in_attr[i].i)
4150
0
    {
4151
0
      _bfd_error_handler
4152
0
        (_("error: %pB use %u-byte stack aligned but the output "
4153
0
     "use %u-byte stack aligned"),
4154
0
         ibfd, in_attr[i].i, out_attr[i].i);
4155
0
      result = false;
4156
0
    }
4157
0
  break;
4158
4159
0
      default:
4160
0
  result &= _bfd_elf_merge_unknown_attribute_low (ibfd, obfd, i);
4161
0
      }
4162
4163
      /* If out_attr was copied from in_attr then it won't have a type yet.  */
4164
0
      if (in_attr[i].type && !out_attr[i].type)
4165
0
  out_attr[i].type = in_attr[i].type;
4166
0
    }
4167
4168
  /* Merge Tag_compatibility attributes and any common GNU ones.  */
4169
0
  if (!_bfd_elf_merge_object_attributes (ibfd, info))
4170
0
    return false;
4171
4172
  /* Check for any attributes not known on RISC-V.  */
4173
0
  result &= _bfd_elf_merge_unknown_attribute_list (ibfd, obfd);
4174
4175
0
  return result;
4176
0
}
4177
4178
/* Merge backend specific data from an object file to the output
4179
   object file when linking.  */
4180
4181
static bool
4182
_bfd_riscv_elf_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
4183
0
{
4184
0
  bfd *obfd = info->output_bfd;
4185
0
  flagword new_flags, old_flags;
4186
4187
0
  if (!is_riscv_elf (ibfd) || !is_riscv_elf (obfd))
4188
0
    return true;
4189
4190
0
  if (strcmp (bfd_get_target (ibfd), bfd_get_target (obfd)) != 0)
4191
0
    {
4192
0
      (*_bfd_error_handler)
4193
0
  (_("%pB: ABI is incompatible with that of the selected emulation:\n"
4194
0
     "  target emulation `%s' does not match `%s'"),
4195
0
   ibfd, bfd_get_target (ibfd), bfd_get_target (obfd));
4196
0
      return false;
4197
0
    }
4198
4199
0
  if (!_bfd_elf_merge_object_attributes (ibfd, info))
4200
0
    return false;
4201
4202
0
  if (!riscv_merge_attributes (ibfd, info))
4203
0
    return false;
4204
4205
  /* Check to see if the input BFD actually contains any sections.  If not,
4206
     its flags may not have been initialized either, but it cannot actually
4207
     cause any incompatibility.  Do not short-circuit dynamic objects; their
4208
     section list may be emptied by elf_link_add_object_symbols.
4209
4210
     Also check to see if there are no code sections in the input.  In this
4211
     case, there is no need to check for code specific flags.  */
4212
0
  if (!(ibfd->flags & DYNAMIC))
4213
0
    {
4214
0
      bool null_input_bfd = true;
4215
0
      bool only_data_sections = true;
4216
0
      asection *sec;
4217
4218
0
      for (sec = ibfd->sections; sec != NULL; sec = sec->next)
4219
0
  {
4220
0
    null_input_bfd = false;
4221
4222
0
    if ((bfd_section_flags (sec)
4223
0
         & (SEC_LOAD | SEC_CODE | SEC_HAS_CONTENTS))
4224
0
        == (SEC_LOAD | SEC_CODE | SEC_HAS_CONTENTS))
4225
0
      {
4226
0
        only_data_sections = false;
4227
0
        break;
4228
0
      }
4229
0
  }
4230
4231
0
      if (null_input_bfd || only_data_sections)
4232
0
  return true;
4233
0
    }
4234
4235
0
  new_flags = elf_elfheader (ibfd)->e_flags;
4236
0
  old_flags = elf_elfheader (obfd)->e_flags;
4237
4238
0
  if (!elf_flags_init (obfd))
4239
0
    {
4240
0
      elf_flags_init (obfd) = true;
4241
0
      elf_elfheader (obfd)->e_flags = new_flags;
4242
0
      return true;
4243
0
    }
4244
4245
  /* Disallow linking different float ABIs.  */
4246
0
  if ((old_flags ^ new_flags) & EF_RISCV_FLOAT_ABI)
4247
0
    {
4248
0
      (*_bfd_error_handler)
4249
0
  (_("%pB: can't link %s modules with %s modules"), ibfd,
4250
0
   riscv_float_abi_string (new_flags),
4251
0
   riscv_float_abi_string (old_flags));
4252
0
      goto fail;
4253
0
    }
4254
4255
  /* Disallow linking RVE and non-RVE.  */
4256
0
  if ((old_flags ^ new_flags) & EF_RISCV_RVE)
4257
0
    {
4258
0
      (*_bfd_error_handler)
4259
0
       (_("%pB: can't link RVE with other target"), ibfd);
4260
0
      goto fail;
4261
0
    }
4262
4263
  /* Allow linking RVC and non-RVC, and keep the RVC flag.  */
4264
0
  elf_elfheader (obfd)->e_flags |= new_flags & EF_RISCV_RVC;
4265
4266
  /* Allow linking TSO and non-TSO, and keep the TSO flag.  */
4267
0
  elf_elfheader (obfd)->e_flags |= new_flags & EF_RISCV_TSO;
4268
4269
0
  return true;
4270
4271
0
 fail:
4272
0
  bfd_set_error (bfd_error_bad_value);
4273
0
  return false;
4274
0
}
4275
4276
/* Ignore and report warning for the unknwon elf attribute.  */
4277
4278
static bool
4279
riscv_elf_obj_attrs_handle_unknown (bfd *abfd, int tag)
4280
0
{
4281
0
  _bfd_error_handler
4282
    /* xgettext:c-format */
4283
0
    (_("warning: %pB: unknown RISCV ABI object attribute %d"),
4284
0
     abfd, tag);
4285
0
  return true;
4286
0
}
4287
4288
/* A second format for recording PC-relative hi relocations.  This stores the
4289
   information required to relax them to GP-relative addresses.  */
4290
4291
typedef struct riscv_pcgp_hi_reloc riscv_pcgp_hi_reloc;
4292
struct riscv_pcgp_hi_reloc
4293
{
4294
  bfd_vma hi_sec_off;
4295
  bfd_vma hi_addend;
4296
  bfd_vma hi_addr;
4297
  unsigned hi_sym;
4298
  asection *sym_sec;
4299
  bool undefined_weak;
4300
  riscv_pcgp_hi_reloc *next;
4301
};
4302
4303
typedef struct riscv_pcgp_lo_reloc riscv_pcgp_lo_reloc;
4304
struct riscv_pcgp_lo_reloc
4305
{
4306
  bfd_vma hi_sec_off;
4307
  riscv_pcgp_lo_reloc *next;
4308
};
4309
4310
typedef struct
4311
{
4312
  riscv_pcgp_hi_reloc *hi;
4313
  riscv_pcgp_lo_reloc *lo;
4314
} riscv_pcgp_relocs;
4315
4316
/* Initialize the pcgp reloc info in P.  */
4317
4318
static bool
4319
riscv_init_pcgp_relocs (riscv_pcgp_relocs *p)
4320
0
{
4321
0
  p->hi = NULL;
4322
0
  p->lo = NULL;
4323
0
  return true;
4324
0
}
4325
4326
/* Free the pcgp reloc info in P.  */
4327
4328
static void
4329
riscv_free_pcgp_relocs (riscv_pcgp_relocs *p,
4330
      bfd *abfd ATTRIBUTE_UNUSED,
4331
      asection *sec ATTRIBUTE_UNUSED)
4332
0
{
4333
0
  riscv_pcgp_hi_reloc *c;
4334
0
  riscv_pcgp_lo_reloc *l;
4335
4336
0
  for (c = p->hi; c != NULL; )
4337
0
    {
4338
0
      riscv_pcgp_hi_reloc *next = c->next;
4339
0
      free (c);
4340
0
      c = next;
4341
0
    }
4342
4343
0
  for (l = p->lo; l != NULL; )
4344
0
    {
4345
0
      riscv_pcgp_lo_reloc *next = l->next;
4346
0
      free (l);
4347
0
      l = next;
4348
0
    }
4349
0
}
4350
4351
/* Record pcgp hi part reloc info in P, using HI_SEC_OFF as the lookup index.
4352
   The HI_ADDEND, HI_ADDR, HI_SYM, and SYM_SEC args contain info required to
4353
   relax the corresponding lo part reloc.  */
4354
4355
static bool
4356
riscv_record_pcgp_hi_reloc (riscv_pcgp_relocs *p, bfd_vma hi_sec_off,
4357
          bfd_vma hi_addend, bfd_vma hi_addr,
4358
          unsigned hi_sym, asection *sym_sec,
4359
          bool undefined_weak)
4360
0
{
4361
0
  riscv_pcgp_hi_reloc *new = bfd_malloc (sizeof (*new));
4362
0
  if (!new)
4363
0
    return false;
4364
0
  new->hi_sec_off = hi_sec_off;
4365
0
  new->hi_addend = hi_addend;
4366
0
  new->hi_addr = hi_addr;
4367
0
  new->hi_sym = hi_sym;
4368
0
  new->sym_sec = sym_sec;
4369
0
  new->undefined_weak = undefined_weak;
4370
0
  new->next = p->hi;
4371
0
  p->hi = new;
4372
0
  return true;
4373
0
}
4374
4375
/* Look up hi part pcgp reloc info in P, using HI_SEC_OFF as the lookup index.
4376
   This is used by a lo part reloc to find the corresponding hi part reloc.  */
4377
4378
static riscv_pcgp_hi_reloc *
4379
riscv_find_pcgp_hi_reloc (riscv_pcgp_relocs *p, bfd_vma hi_sec_off)
4380
0
{
4381
0
  riscv_pcgp_hi_reloc *c;
4382
4383
0
  for (c = p->hi; c != NULL; c = c->next)
4384
0
    if (c->hi_sec_off == hi_sec_off)
4385
0
      return c;
4386
0
  return NULL;
4387
0
}
4388
4389
/* Record pcgp lo part reloc info in P, using HI_SEC_OFF as the lookup info.
4390
   This is used to record relocs that can't be relaxed.  */
4391
4392
static bool
4393
riscv_record_pcgp_lo_reloc (riscv_pcgp_relocs *p, bfd_vma hi_sec_off)
4394
0
{
4395
0
  riscv_pcgp_lo_reloc *new = bfd_malloc (sizeof (*new));
4396
0
  if (!new)
4397
0
    return false;
4398
0
  new->hi_sec_off = hi_sec_off;
4399
0
  new->next = p->lo;
4400
0
  p->lo = new;
4401
0
  return true;
4402
0
}
4403
4404
/* Look up lo part pcgp reloc info in P, using HI_SEC_OFF as the lookup index.
4405
   This is used by a hi part reloc to find the corresponding lo part reloc.  */
4406
4407
static bool
4408
riscv_find_pcgp_lo_reloc (riscv_pcgp_relocs *p, bfd_vma hi_sec_off)
4409
0
{
4410
0
  riscv_pcgp_lo_reloc *c;
4411
4412
0
  for (c = p->lo; c != NULL; c = c->next)
4413
0
    if (c->hi_sec_off == hi_sec_off)
4414
0
      return true;
4415
0
  return false;
4416
0
}
4417
4418
static void
4419
riscv_update_pcgp_relocs (riscv_pcgp_relocs *p, asection *deleted_sec,
4420
        bfd_vma deleted_addr, size_t deleted_count)
4421
0
{
4422
  /* Bytes have already been deleted and toaddr should match the old section
4423
     size for our checks, so adjust it here.  */
4424
0
  bfd_vma toaddr = deleted_sec->size + deleted_count;
4425
0
  riscv_pcgp_lo_reloc *l;
4426
0
  riscv_pcgp_hi_reloc *h;
4427
4428
  /* Update section offsets of corresponding pcrel_hi relocs for the pcrel_lo
4429
     entries where they occur after the deleted bytes.  */
4430
0
  for (l = p->lo; l != NULL; l = l->next)
4431
0
    if (l->hi_sec_off > deleted_addr
4432
0
  && l->hi_sec_off < toaddr)
4433
0
      l->hi_sec_off -= deleted_count;
4434
4435
  /* Update both section offsets, and symbol values of pcrel_hi relocs where
4436
     these values occur after the deleted bytes.  */
4437
0
  for (h = p->hi; h != NULL; h = h->next)
4438
0
    {
4439
0
      if (h->hi_sec_off > deleted_addr
4440
0
    && h->hi_sec_off < toaddr)
4441
0
  h->hi_sec_off -= deleted_count;
4442
0
      if (h->sym_sec == deleted_sec
4443
0
    && h->hi_addr > deleted_addr
4444
0
    && h->hi_addr < toaddr)
4445
0
      h->hi_addr -= deleted_count;
4446
0
    }
4447
0
}
4448
4449
/* Delete some bytes, adjust relcocations and symbol table from a section.  */
4450
4451
static bool
4452
_riscv_relax_delete_bytes (bfd *abfd,
4453
         asection *sec,
4454
         bfd_vma addr,
4455
         size_t count,
4456
         struct bfd_link_info *link_info,
4457
         riscv_pcgp_relocs *p,
4458
         bfd_vma delete_total,
4459
         bfd_vma toaddr)
4460
0
{
4461
0
  unsigned int i, symcount;
4462
0
  struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (abfd);
4463
0
  Elf_Internal_Shdr *symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
4464
0
  unsigned int sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
4465
0
  struct bfd_elf_section_data *data = elf_section_data (sec);
4466
0
  bfd_byte *contents = data->this_hdr.contents;
4467
0
  size_t bytes_to_move = toaddr - addr - count;
4468
4469
  /* Actually delete the bytes.  */
4470
0
  sec->size -= count;
4471
0
  memmove (contents + addr, contents + addr + count + delete_total, bytes_to_move);
4472
4473
  /* Still adjust relocations and symbols in non-linear times.  */
4474
0
  toaddr = sec->size + count;
4475
4476
  /* Adjust the location of all of the relocs.  Note that we need not
4477
     adjust the addends, since all PC-relative references must be against
4478
     symbols, which we will adjust below.  */
4479
0
  for (i = 0; i < sec->reloc_count; i++)
4480
0
    if (data->relocs[i].r_offset > addr && data->relocs[i].r_offset < toaddr)
4481
0
      data->relocs[i].r_offset -= count;
4482
4483
  /* Adjust the hi_sec_off, and the hi_addr of any entries in the pcgp relocs
4484
     table for which these values occur after the deleted bytes.  */
4485
0
  if (p)
4486
0
    riscv_update_pcgp_relocs (p, sec, addr, count);
4487
4488
  /* Adjust the local symbols defined in this section.  */
4489
0
  for (i = 0; i < symtab_hdr->sh_info; i++)
4490
0
    {
4491
0
      Elf_Internal_Sym *sym = (Elf_Internal_Sym *) symtab_hdr->contents + i;
4492
0
      if (sym->st_shndx == sec_shndx)
4493
0
  {
4494
    /* If the symbol is in the range of memory we just moved, we
4495
       have to adjust its value.  */
4496
0
    if (sym->st_value > addr && sym->st_value <= toaddr)
4497
0
      sym->st_value -= count;
4498
4499
    /* If the symbol *spans* the bytes we just deleted (i.e. its
4500
       *end* is in the moved bytes but its *start* isn't), then we
4501
       must adjust its size.
4502
4503
       This test needs to use the original value of st_value, otherwise
4504
       we might accidentally decrease size when deleting bytes right
4505
       before the symbol.  But since deleted relocs can't span across
4506
       symbols, we can't have both a st_value and a st_size decrease,
4507
       so it is simpler to just use an else.  */
4508
0
    else if (sym->st_value <= addr
4509
0
       && sym->st_value + sym->st_size > addr
4510
0
       && sym->st_value + sym->st_size <= toaddr)
4511
0
      sym->st_size -= count;
4512
0
  }
4513
0
    }
4514
4515
  /* Now adjust the global symbols defined in this section.  */
4516
0
  symcount = ((symtab_hdr->sh_size / sizeof (Elf64_External_Sym))
4517
0
        - symtab_hdr->sh_info);
4518
4519
0
  for (i = 0; i < symcount; i++)
4520
0
    {
4521
0
      struct elf_link_hash_entry *sym_hash = sym_hashes[i];
4522
4523
      /* The '--wrap SYMBOL' option is causing a pain when the object file,
4524
   containing the definition of __wrap_SYMBOL, includes a direct
4525
   call to SYMBOL as well. Since both __wrap_SYMBOL and SYMBOL reference
4526
   the same symbol (which is __wrap_SYMBOL), but still exist as two
4527
   different symbols in 'sym_hashes', we don't want to adjust
4528
   the global symbol __wrap_SYMBOL twice.
4529
4530
   The same problem occurs with symbols that are versioned_hidden, as
4531
   foo becomes an alias for foo@BAR, and hence they need the same
4532
   treatment.  */
4533
0
      if (link_info->wrap_hash != NULL
4534
0
    || sym_hash->versioned != unversioned)
4535
0
  {
4536
0
    struct elf_link_hash_entry **cur_sym_hashes;
4537
4538
    /* Loop only over the symbols which have already been checked.  */
4539
0
    for (cur_sym_hashes = sym_hashes; cur_sym_hashes < &sym_hashes[i];
4540
0
         cur_sym_hashes++)
4541
0
      {
4542
        /* If the current symbol is identical to 'sym_hash', that means
4543
     the symbol was already adjusted (or at least checked).  */
4544
0
        if (*cur_sym_hashes == sym_hash)
4545
0
    break;
4546
0
      }
4547
    /* Don't adjust the symbol again.  */
4548
0
    if (cur_sym_hashes < &sym_hashes[i])
4549
0
      continue;
4550
0
  }
4551
4552
0
      if ((sym_hash->root.type == bfd_link_hash_defined
4553
0
     || sym_hash->root.type == bfd_link_hash_defweak)
4554
0
    && sym_hash->root.u.def.section == sec)
4555
0
  {
4556
    /* As above, adjust the value if needed.  */
4557
0
    if (sym_hash->root.u.def.value > addr
4558
0
        && sym_hash->root.u.def.value <= toaddr)
4559
0
      sym_hash->root.u.def.value -= count;
4560
4561
    /* As above, adjust the size if needed.  */
4562
0
    else if (sym_hash->root.u.def.value <= addr
4563
0
       && sym_hash->root.u.def.value + sym_hash->size > addr
4564
0
       && sym_hash->root.u.def.value + sym_hash->size <= toaddr)
4565
0
      sym_hash->size -= count;
4566
0
  }
4567
0
    }
4568
4569
0
  return true;
4570
0
}
4571
4572
typedef bool (*relax_delete_t) (bfd *, asection *,
4573
        bfd_vma, size_t,
4574
        struct bfd_link_info *,
4575
        riscv_pcgp_relocs *,
4576
        Elf_Internal_Rela *);
4577
4578
static relax_delete_t riscv_relax_delete_bytes;
4579
4580
/* Do not delete some bytes from a section while relaxing.
4581
   Just mark the deleted bytes as R_RISCV_DELETE.  */
4582
4583
static bool
4584
_riscv_relax_delete_piecewise (bfd *abfd ATTRIBUTE_UNUSED,
4585
             asection *sec ATTRIBUTE_UNUSED,
4586
             bfd_vma addr,
4587
             size_t count,
4588
             struct bfd_link_info *link_info ATTRIBUTE_UNUSED,
4589
             riscv_pcgp_relocs *p ATTRIBUTE_UNUSED,
4590
             Elf_Internal_Rela *rel)
4591
0
{
4592
0
  if (rel == NULL)
4593
0
    return false;
4594
0
  rel->r_info = ELF64_R_INFO (0, R_RISCV_DELETE);
4595
0
  rel->r_offset = addr;
4596
0
  rel->r_addend = count;
4597
0
  return true;
4598
0
}
4599
4600
/* Delete some bytes from a section while relaxing.  */
4601
4602
static bool
4603
_riscv_relax_delete_immediate (bfd *abfd,
4604
             asection *sec,
4605
             bfd_vma addr,
4606
             size_t count,
4607
             struct bfd_link_info *link_info,
4608
             riscv_pcgp_relocs *p,
4609
             Elf_Internal_Rela *rel)
4610
0
{
4611
0
  if (rel != NULL)
4612
0
    rel->r_info = ELF64_R_INFO (0, R_RISCV_NONE);
4613
0
  return _riscv_relax_delete_bytes (abfd, sec, addr, count,
4614
0
            link_info, p, 0, sec->size);
4615
0
}
4616
4617
/* Delete the bytes for R_RISCV_DELETE relocs.  */
4618
4619
static bool
4620
riscv_relax_resolve_delete_relocs (bfd *abfd,
4621
           asection *sec,
4622
           struct bfd_link_info *link_info,
4623
           Elf_Internal_Rela *relocs)
4624
0
{
4625
0
  bfd_vma delete_total = 0;
4626
0
  unsigned int i;
4627
4628
0
  for (i = 0; i < sec->reloc_count; i++)
4629
0
    {
4630
0
      Elf_Internal_Rela *rel = relocs + i;
4631
0
      if (ELF64_R_TYPE (rel->r_info) != R_RISCV_DELETE)
4632
0
  continue;
4633
4634
      /* Find the next R_RISCV_DELETE reloc if possible.  */
4635
0
      Elf_Internal_Rela *rel_next = NULL;
4636
0
      unsigned int start = rel - relocs;
4637
0
      for (i = start; i < sec->reloc_count; i++)
4638
0
  {
4639
    /* Since we only replace existing relocs and don't add new relocs, the
4640
       relocs are in sequential order. We can skip the relocs prior to this
4641
       one, making this search linear time.  */
4642
0
    rel_next = relocs + i;
4643
0
    if (ELF64_R_TYPE ((rel_next)->r_info) == R_RISCV_DELETE
4644
0
        && (rel_next)->r_offset > rel->r_offset)
4645
0
      {
4646
0
        BFD_ASSERT (rel_next - rel > 0);
4647
0
        break;
4648
0
      }
4649
0
    else
4650
0
      rel_next = NULL;
4651
0
  }
4652
4653
0
      bfd_vma toaddr = rel_next == NULL ? sec->size : rel_next->r_offset;
4654
0
      if (!_riscv_relax_delete_bytes (abfd, sec, rel->r_offset, rel->r_addend,
4655
0
              link_info, NULL, delete_total, toaddr))
4656
0
  return false;
4657
4658
0
      delete_total += rel->r_addend;
4659
0
      rel->r_info = ELF64_R_INFO (0, R_RISCV_NONE);
4660
4661
      /* Skip ahead to the next delete reloc.  */
4662
0
      i = rel_next != NULL ? (unsigned int) (rel_next - relocs - 1)
4663
0
         : sec->reloc_count;
4664
0
    }
4665
4666
0
  return true;
4667
0
}
4668
4669
typedef bool (*relax_func_t) (bfd *, asection *, asection *,
4670
            struct bfd_link_info *,
4671
            Elf_Internal_Rela *,
4672
            bfd_vma, bfd_vma, bfd_vma, bool *,
4673
            riscv_pcgp_relocs *,
4674
            bool undefined_weak);
4675
4676
/* Relax AUIPC + JALR into JAL.  */
4677
4678
static bool
4679
_bfd_riscv_relax_call (bfd *abfd, asection *sec, asection *sym_sec,
4680
           struct bfd_link_info *link_info,
4681
           Elf_Internal_Rela *rel,
4682
           bfd_vma symval,
4683
           bfd_vma max_alignment,
4684
           bfd_vma reserve_size ATTRIBUTE_UNUSED,
4685
           bool *again,
4686
           riscv_pcgp_relocs *pcgp_relocs,
4687
           bool undefined_weak ATTRIBUTE_UNUSED)
4688
0
{
4689
0
  bfd_byte *contents = elf_section_data (sec)->this_hdr.contents;
4690
0
  bfd_vma foff = symval - (sec_addr (sec) + rel->r_offset);
4691
0
  bool near_zero = (symval + RISCV_IMM_REACH / 2) < RISCV_IMM_REACH;
4692
0
  bfd_vma auipc, jalr;
4693
0
  int rd, r_type, len = 4, rvc = elf_elfheader (abfd)->e_flags & EF_RISCV_RVC;
4694
4695
  /* If the call crosses section boundaries, an alignment directive could
4696
     cause the PC-relative offset to later increase, so we need to add in the
4697
     max alignment of any section inclusive from the call to the target.
4698
     Otherwise, we only need to use the alignment of the current section.  */
4699
0
  if (VALID_JTYPE_IMM (foff))
4700
0
    {
4701
0
      if (sym_sec->output_section == sec->output_section
4702
0
    && sym_sec->output_section != bfd_abs_section_ptr)
4703
0
  max_alignment = (bfd_vma) 1 << sym_sec->output_section->alignment_power;
4704
0
      foff += ((bfd_signed_vma) foff < 0 ? -max_alignment : max_alignment);
4705
0
    }
4706
4707
  /* See if this function call can be shortened.  */
4708
0
  if (!VALID_JTYPE_IMM (foff) && !(!bfd_link_pic (link_info) && near_zero))
4709
0
    return true;
4710
4711
  /* Shorten the function call.  */
4712
0
  BFD_ASSERT (rel->r_offset + 8 <= sec->size);
4713
4714
0
  auipc = bfd_getl32 (contents + rel->r_offset);
4715
0
  jalr = bfd_getl32 (contents + rel->r_offset + 4);
4716
0
  rd = (jalr >> OP_SH_RD) & OP_MASK_RD;
4717
0
  rvc = rvc && VALID_CJTYPE_IMM (foff);
4718
4719
  /* C.J exists on RV32 and RV64, but C.JAL is RV32-only.  */
4720
0
  rvc = rvc && (rd == 0 || (rd == X_RA && ARCH_SIZE == 32));
4721
4722
0
  if (rvc)
4723
0
    {
4724
      /* Relax to C.J[AL] rd, addr.  */
4725
0
      r_type = R_RISCV_RVC_JUMP;
4726
0
      auipc = rd == 0 ? MATCH_C_J : MATCH_C_JAL;
4727
0
      len = 2;
4728
0
    }
4729
0
  else if (VALID_JTYPE_IMM (foff))
4730
0
    {
4731
      /* Relax to JAL rd, addr.  */
4732
0
      r_type = R_RISCV_JAL;
4733
0
      auipc = MATCH_JAL | (rd << OP_SH_RD);
4734
0
    }
4735
0
  else
4736
0
    {
4737
      /* Near zero, relax to JALR rd, x0, addr.  */
4738
0
      r_type = R_RISCV_LO12_I;
4739
0
      auipc = MATCH_JALR | (rd << OP_SH_RD);
4740
0
    }
4741
4742
  /* Replace the R_RISCV_CALL reloc.  */
4743
0
  rel->r_info = ELF64_R_INFO (ELF64_R_SYM (rel->r_info), r_type);
4744
  /* Replace the AUIPC.  */
4745
0
  riscv_put_insn (8 * len, auipc, contents + rel->r_offset);
4746
4747
  /* Delete unnecessary JALR and reuse the R_RISCV_RELAX reloc.  */
4748
0
  *again = true;
4749
0
  return riscv_relax_delete_bytes (abfd, sec, rel->r_offset + len, 8 - len,
4750
0
           link_info, pcgp_relocs, rel + 1);
4751
0
}
4752
4753
/* Traverse all output sections and return the max alignment.
4754
4755
   If gp is zero, then all the output section alignments are
4756
   possible candidates;  Otherwise, only the output sections
4757
   which are in the [gp-2K, gp+2K) range need to be considered.  */
4758
4759
static bfd_vma
4760
_bfd_riscv_get_max_alignment (asection *sec, bfd_vma gp)
4761
0
{
4762
0
  unsigned int max_alignment_power = 0;
4763
0
  asection *o;
4764
4765
0
  for (o = sec->output_section->owner->sections; o != NULL; o = o->next)
4766
0
    {
4767
0
      bool valid = true;
4768
0
      if (gp
4769
0
    && !(VALID_ITYPE_IMM (sec_addr (o) - gp)
4770
0
         || VALID_ITYPE_IMM (sec_addr (o) + o->size - gp)))
4771
0
  valid = false;
4772
4773
0
      if (valid && o->alignment_power > max_alignment_power)
4774
0
  max_alignment_power = o->alignment_power;
4775
0
    }
4776
4777
0
  return (bfd_vma) 1 << max_alignment_power;
4778
0
}
4779
4780
/* Relax non-PIC global variable references to GP-relative references.  */
4781
4782
static bool
4783
_bfd_riscv_relax_lui (bfd *abfd,
4784
          asection *sec,
4785
          asection *sym_sec,
4786
          struct bfd_link_info *link_info,
4787
          Elf_Internal_Rela *rel,
4788
          bfd_vma symval,
4789
          bfd_vma max_alignment,
4790
          bfd_vma reserve_size,
4791
          bool *again,
4792
          riscv_pcgp_relocs *pcgp_relocs,
4793
          bool undefined_weak)
4794
0
{
4795
0
  struct riscv_elf_link_hash_table *htab = riscv_elf_hash_table (link_info);
4796
0
  bfd_byte *contents = elf_section_data (sec)->this_hdr.contents;
4797
  /* Can relax to x0 even when gp relaxation is disabled.  */
4798
0
  bfd_vma gp = htab->params->relax_gp
4799
0
         ? riscv_global_pointer_value (link_info)
4800
0
         : 0;
4801
0
  bfd_vma data_segment_alignment = link_info->relro
4802
0
           ? ELF_MAXPAGESIZE + ELF_COMMONPAGESIZE
4803
0
           : ELF_MAXPAGESIZE;
4804
0
  int use_rvc = elf_elfheader (abfd)->e_flags & EF_RISCV_RVC;
4805
4806
0
  BFD_ASSERT (rel->r_offset + 4 <= sec->size);
4807
4808
0
  if (!undefined_weak && gp)
4809
0
    {
4810
      /* If gp and the symbol are in the same output section, which is not the
4811
   abs section, then consider only that output section's alignment.  */
4812
0
      struct bfd_link_hash_entry *h =
4813
0
  bfd_link_hash_lookup (link_info->hash, RISCV_GP_SYMBOL, false, false,
4814
0
            true);
4815
0
      if (h->u.def.section->output_section == sym_sec->output_section
4816
0
    && sym_sec->output_section != bfd_abs_section_ptr)
4817
0
  max_alignment = (bfd_vma) 1 << sym_sec->output_section->alignment_power;
4818
0
      else
4819
0
  {
4820
    /* Consider output section alignments which are in [gp-2K, gp+2K). */
4821
0
    max_alignment = htab->max_alignment_for_gp;
4822
0
    if (max_alignment == (bfd_vma) -1)
4823
0
      {
4824
0
        max_alignment = _bfd_riscv_get_max_alignment (sec, gp);
4825
0
        htab->max_alignment_for_gp = max_alignment;
4826
0
      }
4827
0
  }
4828
4829
      /* PR27566, for default linker script, if a symbol's value outsides the
4830
   bounds of the defined section, then it may cross the data segment
4831
   alignment, so we should reserve more size about MAXPAGESIZE and
4832
   COMMONPAGESIZE, since the data segment alignment might move the
4833
   section forward.  */
4834
0
      if (symval < sec_addr (sym_sec)
4835
0
    || symval > (sec_addr (sym_sec) + sym_sec->size))
4836
0
  max_alignment = data_segment_alignment > max_alignment
4837
0
      ? data_segment_alignment : max_alignment;
4838
0
    }
4839
4840
  /* Is the reference in range of x0 or gp?
4841
     Valid gp range conservatively because of alignment issue.
4842
4843
     Should we also consider the alignment issue for x0 base?  */
4844
0
  if (undefined_weak
4845
0
      || VALID_ITYPE_IMM (symval)
4846
0
      || (symval >= gp
4847
0
    && VALID_ITYPE_IMM (symval - gp + max_alignment + reserve_size))
4848
0
      || (symval < gp
4849
0
    && VALID_ITYPE_IMM (symval - gp - max_alignment - reserve_size)))
4850
0
    {
4851
0
      unsigned sym = ELF64_R_SYM (rel->r_info);
4852
0
      switch (ELF64_R_TYPE (rel->r_info))
4853
0
  {
4854
0
  case R_RISCV_LO12_I:
4855
0
    rel->r_info = ELF64_R_INFO (sym, R_RISCV_GPREL_I);
4856
0
    return true;
4857
4858
0
  case R_RISCV_LO12_S:
4859
0
    rel->r_info = ELF64_R_INFO (sym, R_RISCV_GPREL_S);
4860
0
    return true;
4861
4862
0
  case R_RISCV_HI20:
4863
    /* Delete unnecessary LUI and reuse the reloc.  */
4864
0
    *again = true;
4865
0
    return riscv_relax_delete_bytes (abfd, sec, rel->r_offset, 4,
4866
0
             link_info, pcgp_relocs, rel);
4867
4868
0
  default:
4869
0
    abort ();
4870
0
  }
4871
0
    }
4872
4873
  /* Can we relax LUI to C.LUI?  Alignment might move the section forward;
4874
     account for this assuming page alignment at worst. In the presence of 
4875
     RELRO segment the linker aligns it by one page size, therefore sections
4876
     after the segment can be moved more than one page. */
4877
4878
0
  if (use_rvc
4879
0
      && ELF64_R_TYPE (rel->r_info) == R_RISCV_HI20
4880
0
      && VALID_CITYPE_LUI_IMM (RISCV_CONST_HIGH_PART (symval))
4881
0
      && VALID_CITYPE_LUI_IMM (RISCV_CONST_HIGH_PART (symval)
4882
0
             + data_segment_alignment))
4883
0
    {
4884
      /* Replace LUI with C.LUI if legal (i.e., rd != x0 and rd != x2/sp).  */
4885
0
      bfd_vma lui = bfd_getl32 (contents + rel->r_offset);
4886
0
      unsigned rd = ((unsigned)lui >> OP_SH_RD) & OP_MASK_RD;
4887
0
      if (rd == 0 || rd == X_SP)
4888
0
  return true;
4889
4890
0
      lui = (lui & (OP_MASK_RD << OP_SH_RD)) | MATCH_C_LUI;
4891
0
      bfd_putl32 (lui, contents + rel->r_offset);
4892
4893
      /* Replace the R_RISCV_HI20 reloc.  */
4894
0
      rel->r_info = ELF64_R_INFO (ELF64_R_SYM (rel->r_info), R_RISCV_RVC_LUI);
4895
4896
      /* Delete extra bytes and reuse the R_RISCV_RELAX reloc.  */
4897
0
      *again = true;
4898
0
      return riscv_relax_delete_bytes (abfd, sec, rel->r_offset + 2, 2,
4899
0
               link_info, pcgp_relocs, rel + 1);
4900
0
    }
4901
4902
0
  return true;
4903
0
}
4904
4905
/* Relax non-PIC TLS references to TP-relative references.  */
4906
4907
static bool
4908
_bfd_riscv_relax_tls_le (bfd *abfd,
4909
       asection *sec,
4910
       asection *sym_sec ATTRIBUTE_UNUSED,
4911
       struct bfd_link_info *link_info,
4912
       Elf_Internal_Rela *rel,
4913
       bfd_vma symval,
4914
       bfd_vma max_alignment ATTRIBUTE_UNUSED,
4915
       bfd_vma reserve_size ATTRIBUTE_UNUSED,
4916
       bool *again,
4917
       riscv_pcgp_relocs *pcgp_relocs,
4918
       bool undefined_weak ATTRIBUTE_UNUSED)
4919
0
{
4920
  /* See if this symbol is in range of tp.  */
4921
0
  if (RISCV_CONST_HIGH_PART (tpoff (link_info, symval)) != 0)
4922
0
    return true;
4923
4924
0
  BFD_ASSERT (rel->r_offset + 4 <= sec->size);
4925
0
  switch (ELF64_R_TYPE (rel->r_info))
4926
0
    {
4927
0
    case R_RISCV_TPREL_LO12_I:
4928
0
      rel->r_info = ELF64_R_INFO (ELF64_R_SYM (rel->r_info), R_RISCV_TPREL_I);
4929
0
      return true;
4930
4931
0
    case R_RISCV_TPREL_LO12_S:
4932
0
      rel->r_info = ELF64_R_INFO (ELF64_R_SYM (rel->r_info), R_RISCV_TPREL_S);
4933
0
      return true;
4934
4935
0
    case R_RISCV_TPREL_HI20:
4936
0
    case R_RISCV_TPREL_ADD:
4937
      /* Delete unnecessary instruction and reuse the reloc.  */
4938
0
      *again = true;
4939
0
      return riscv_relax_delete_bytes (abfd, sec, rel->r_offset, 4, link_info,
4940
0
               pcgp_relocs, rel);
4941
4942
0
    default:
4943
0
      abort ();
4944
0
    }
4945
0
}
4946
4947
/* Implement R_RISCV_ALIGN by deleting excess alignment NOPs.
4948
   Once we've handled an R_RISCV_ALIGN, we can't relax anything else.  */
4949
4950
static bool
4951
_bfd_riscv_relax_align (bfd *abfd, asection *sec,
4952
      asection *sym_sec,
4953
      struct bfd_link_info *link_info,
4954
      Elf_Internal_Rela *rel,
4955
      bfd_vma symval,
4956
      bfd_vma max_alignment ATTRIBUTE_UNUSED,
4957
      bfd_vma reserve_size ATTRIBUTE_UNUSED,
4958
      bool *again ATTRIBUTE_UNUSED,
4959
      riscv_pcgp_relocs *pcgp_relocs ATTRIBUTE_UNUSED,
4960
      bool undefined_weak ATTRIBUTE_UNUSED)
4961
0
{
4962
0
  bfd_byte *contents = elf_section_data (sec)->this_hdr.contents;
4963
0
  bfd_vma alignment = 1, pos;
4964
0
  while (alignment <= rel->r_addend)
4965
0
    alignment *= 2;
4966
4967
0
  symval -= rel->r_addend;
4968
0
  bfd_vma aligned_addr = ((symval - 1) & ~(alignment - 1)) + alignment;
4969
0
  bfd_vma nop_bytes = aligned_addr - symval;
4970
4971
  /* Once we've handled an R_RISCV_ALIGN, we can't relax anything else.  */
4972
0
  sec->sec_flg0 = true;
4973
4974
  /* Make sure there are enough NOPs to actually achieve the alignment.  */
4975
0
  if (rel->r_addend < nop_bytes)
4976
0
    {
4977
0
      _bfd_error_handler
4978
0
  (_("%pB(%pA+%#" PRIx64 "): %" PRId64 " bytes required for alignment "
4979
0
     "to %" PRId64 "-byte boundary, but only %" PRId64 " present"),
4980
0
   abfd, sym_sec, (uint64_t) rel->r_offset,
4981
0
   (int64_t) nop_bytes, (int64_t) alignment, (int64_t) rel->r_addend);
4982
0
      bfd_set_error (bfd_error_bad_value);
4983
0
      return false;
4984
0
    }
4985
4986
  /* Delete the reloc.  */
4987
0
  rel->r_info = ELF64_R_INFO (0, R_RISCV_NONE);
4988
4989
  /* If the number of NOPs is already correct, there's nothing to do.  */
4990
0
  if (nop_bytes == rel->r_addend)
4991
0
    return true;
4992
4993
  /* Write as many RISC-V NOPs as we need.  */
4994
0
  for (pos = 0; pos < (nop_bytes & -4); pos += 4)
4995
0
    bfd_putl32 (RISCV_NOP, contents + rel->r_offset + pos);
4996
4997
  /* Write a final RVC NOP if need be.  */
4998
0
  if (nop_bytes % 4 != 0)
4999
0
    bfd_putl16 (RVC_NOP, contents + rel->r_offset + pos);
5000
5001
  /* Delete excess bytes.  */
5002
0
  return riscv_relax_delete_bytes (abfd, sec, rel->r_offset + nop_bytes,
5003
0
           rel->r_addend - nop_bytes, link_info,
5004
0
           NULL, NULL);
5005
0
}
5006
5007
/* Relax PC-relative references to GP-relative references.  */
5008
5009
static bool
5010
_bfd_riscv_relax_pc (bfd *abfd ATTRIBUTE_UNUSED,
5011
         asection *sec,
5012
         asection *sym_sec,
5013
         struct bfd_link_info *link_info,
5014
         Elf_Internal_Rela *rel,
5015
         bfd_vma symval,
5016
         bfd_vma max_alignment,
5017
         bfd_vma reserve_size,
5018
         bool *again,
5019
         riscv_pcgp_relocs *pcgp_relocs,
5020
         bool undefined_weak)
5021
0
{
5022
0
  struct riscv_elf_link_hash_table *htab = riscv_elf_hash_table (link_info);
5023
  /* Can relax to x0 even when gp relaxation is disabled.  */
5024
0
  bfd_vma gp = htab->params->relax_gp
5025
0
         ? riscv_global_pointer_value (link_info)
5026
0
         : 0;
5027
0
  bfd_vma data_segment_alignment = link_info->relro
5028
0
           ? ELF_MAXPAGESIZE + ELF_COMMONPAGESIZE
5029
0
           : ELF_MAXPAGESIZE;
5030
5031
0
  BFD_ASSERT (rel->r_offset + 4 <= sec->size);
5032
5033
  /* Chain the _LO relocs to their cooresponding _HI reloc to compute the
5034
     actual target address.  */
5035
0
  riscv_pcgp_hi_reloc hi_reloc;
5036
0
  memset (&hi_reloc, 0, sizeof (hi_reloc));
5037
0
  switch (ELF64_R_TYPE (rel->r_info))
5038
0
    {
5039
0
    case R_RISCV_PCREL_LO12_I:
5040
0
    case R_RISCV_PCREL_LO12_S:
5041
0
      {
5042
  /* If the %lo has an addend, it isn't for the label pointing at the
5043
     hi part instruction, but rather for the symbol pointed at by the
5044
     hi part instruction.  So we must subtract it here for the lookup.
5045
     It is still used below in the final symbol address.  */
5046
0
  bfd_vma hi_sec_off = symval - sec_addr (sym_sec) - rel->r_addend;
5047
0
  riscv_pcgp_hi_reloc *hi = riscv_find_pcgp_hi_reloc (pcgp_relocs,
5048
0
                  hi_sec_off);
5049
0
  if (hi == NULL)
5050
0
    {
5051
0
      riscv_record_pcgp_lo_reloc (pcgp_relocs, hi_sec_off);
5052
0
      return true;
5053
0
    }
5054
5055
0
  hi_reloc = *hi;
5056
0
  symval = hi_reloc.hi_addr;
5057
0
  sym_sec = hi_reloc.sym_sec;
5058
5059
  /* We can not know whether the undefined weak symbol is referenced
5060
     according to the information of R_RISCV_PCREL_LO12_I/S.  Therefore,
5061
     we have to record the 'undefined_weak' flag when handling the
5062
     corresponding R_RISCV_HI20 reloc in riscv_record_pcgp_hi_reloc.  */
5063
0
  undefined_weak = hi_reloc.undefined_weak;
5064
0
      }
5065
0
      break;
5066
5067
0
    case R_RISCV_PCREL_HI20:
5068
      /* Mergeable symbols and code might later move out of range.  */
5069
0
      if (! undefined_weak
5070
0
    && sym_sec->flags & (SEC_MERGE | SEC_CODE))
5071
0
  return true;
5072
5073
      /* If the cooresponding lo relocation has already been seen then it's not
5074
         safe to relax this relocation.  */
5075
0
      if (riscv_find_pcgp_lo_reloc (pcgp_relocs, rel->r_offset))
5076
0
  return true;
5077
5078
0
      break;
5079
5080
0
    default:
5081
0
      abort ();
5082
0
    }
5083
5084
0
  if (!undefined_weak && gp)
5085
0
    {
5086
      /* If gp and the symbol are in the same output section, which is not the
5087
   abs section, then consider only that output section's alignment.  */
5088
0
      struct bfd_link_hash_entry *h =
5089
0
  bfd_link_hash_lookup (link_info->hash, RISCV_GP_SYMBOL, false, false,
5090
0
            true);
5091
0
      if (h->u.def.section->output_section == sym_sec->output_section
5092
0
    && sym_sec->output_section != bfd_abs_section_ptr)
5093
0
  max_alignment = (bfd_vma) 1 << sym_sec->output_section->alignment_power;
5094
0
      else
5095
0
  {
5096
    /* Consider output section alignments which are in [gp-2K, gp+2K). */
5097
0
    max_alignment = htab->max_alignment_for_gp;
5098
0
    if (max_alignment == (bfd_vma) -1)
5099
0
      {
5100
0
        max_alignment = _bfd_riscv_get_max_alignment (sec, gp);
5101
0
        htab->max_alignment_for_gp = max_alignment;
5102
0
      }
5103
0
  }
5104
5105
      /* PR27566, for default linker script, if a symbol's value outsides the
5106
   bounds of the defined section, then it may cross the data segment
5107
   alignment, so we should reserve more size about MAXPAGESIZE and
5108
   COMMONPAGESIZE, since the data segment alignment might move the
5109
   section forward.  */
5110
0
      if (symval < sec_addr (sym_sec)
5111
0
    || symval > (sec_addr (sym_sec) + sym_sec->size))
5112
0
  max_alignment = data_segment_alignment > max_alignment
5113
0
      ? data_segment_alignment : max_alignment;
5114
0
    }
5115
5116
  /* Is the reference in range of x0 or gp?
5117
     Valid gp range conservatively because of alignment issue.
5118
5119
     Should we also consider the alignment issue for x0 base?  */
5120
0
  if (undefined_weak
5121
0
      || VALID_ITYPE_IMM (symval)
5122
0
      || (symval >= gp
5123
0
    && VALID_ITYPE_IMM (symval - gp + max_alignment + reserve_size))
5124
0
      || (symval < gp
5125
0
    && VALID_ITYPE_IMM (symval - gp - max_alignment - reserve_size)))
5126
0
    {
5127
0
      unsigned sym = hi_reloc.hi_sym;
5128
0
      switch (ELF64_R_TYPE (rel->r_info))
5129
0
  {
5130
0
  case R_RISCV_PCREL_LO12_I:
5131
0
    rel->r_info = ELF64_R_INFO (sym, R_RISCV_GPREL_I);
5132
0
    rel->r_addend += hi_reloc.hi_addend;
5133
0
    return true;
5134
5135
0
  case R_RISCV_PCREL_LO12_S:
5136
0
    rel->r_info = ELF64_R_INFO (sym, R_RISCV_GPREL_S);
5137
0
    rel->r_addend += hi_reloc.hi_addend;
5138
0
    return true;
5139
5140
0
  case R_RISCV_PCREL_HI20:
5141
0
    riscv_record_pcgp_hi_reloc (pcgp_relocs,
5142
0
              rel->r_offset,
5143
0
              rel->r_addend,
5144
0
              symval,
5145
0
              ELF64_R_SYM(rel->r_info),
5146
0
              sym_sec,
5147
0
              undefined_weak);
5148
    /* Delete unnecessary AUIPC and reuse the reloc.  */
5149
0
    *again = true;
5150
0
    riscv_relax_delete_bytes (abfd, sec, rel->r_offset, 4, link_info,
5151
0
            pcgp_relocs, rel);
5152
0
    return true;
5153
5154
0
  default:
5155
0
    abort ();
5156
0
  }
5157
0
    }
5158
5159
0
  return true;
5160
0
}
5161
5162
/* Called by after_allocation to set the information of data segment
5163
   before relaxing.  */
5164
5165
void
5166
bfd_elf64_riscv_set_data_segment_info (struct bfd_link_info *info,
5167
                                       int *data_segment_phase)
5168
0
{
5169
0
  struct riscv_elf_link_hash_table *htab = riscv_elf_hash_table (info);
5170
0
  htab->data_segment_phase = data_segment_phase;
5171
0
}
5172
5173
/* Relax a section.
5174
5175
   Pass 0: Shortens code sequences for LUI/CALL/TPREL/PCREL relocs and
5176
     deletes the obsolete bytes.
5177
   Pass 1: Which cannot be disabled, handles code alignment directives.  */
5178
5179
static bool
5180
_bfd_riscv_relax_section (bfd *abfd, asection *sec,
5181
        struct bfd_link_info *info,
5182
        bool *again)
5183
0
{
5184
0
  Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (abfd);
5185
0
  struct riscv_elf_link_hash_table *htab = riscv_elf_hash_table (info);
5186
0
  struct bfd_elf_section_data *data = elf_section_data (sec);
5187
0
  Elf_Internal_Rela *relocs;
5188
0
  bool ret = false;
5189
0
  unsigned int i;
5190
0
  bfd_vma max_alignment, reserve_size = 0;
5191
0
  riscv_pcgp_relocs pcgp_relocs;
5192
0
  static asection *first_section = NULL;
5193
5194
0
  *again = false;
5195
5196
0
  if (bfd_link_relocatable (info)
5197
0
      || sec->sec_flg0
5198
0
      || sec->reloc_count == 0
5199
0
      || (sec->flags & SEC_RELOC) == 0
5200
0
      || (sec->flags & SEC_HAS_CONTENTS) == 0
5201
0
      || (info->disable_target_specific_optimizations
5202
0
    && info->relax_pass == 0)
5203
      /* The exp_seg_relro_adjust is enum phase_enum (0x4),
5204
   and defined in ld/ldexp.h.  */
5205
0
      || *(htab->data_segment_phase) == 4)
5206
0
    return true;
5207
5208
  /* Record the first relax section, so that we can reset the
5209
     max_alignment_for_gp for the repeated relax passes.  */
5210
0
  if (first_section == NULL)
5211
0
    first_section = sec;
5212
0
  else if (first_section == sec)
5213
0
    htab->max_alignment_for_gp = -1;
5214
5215
0
  riscv_init_pcgp_relocs (&pcgp_relocs);
5216
5217
  /* Read this BFD's relocs if we haven't done so already.  */
5218
0
  if (data->relocs)
5219
0
    relocs = data->relocs;
5220
0
  else if (!(relocs = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
5221
0
             info->keep_memory)))
5222
0
    goto fail;
5223
5224
  /* Estimate the maximum alignment for all output sections once time
5225
     should be enough.  */
5226
0
  max_alignment = htab->max_alignment;
5227
0
  if (max_alignment == (bfd_vma) -1)
5228
0
    {
5229
0
      max_alignment = _bfd_riscv_get_max_alignment (sec, 0/* gp */);
5230
0
      htab->max_alignment = max_alignment;
5231
0
    }
5232
5233
  /* Examine and consider relaxing each reloc.  */
5234
0
  for (i = 0; i < sec->reloc_count; i++)
5235
0
    {
5236
0
      asection *sym_sec;
5237
0
      Elf_Internal_Rela *rel = relocs + i;
5238
0
      relax_func_t relax_func;
5239
0
      int type = ELF64_R_TYPE (rel->r_info);
5240
0
      bfd_vma symval;
5241
0
      char symtype;
5242
0
      bool undefined_weak = false;
5243
5244
0
      relax_func = NULL;
5245
0
      riscv_relax_delete_bytes = NULL;
5246
0
      if (info->relax_pass == 0)
5247
0
  {
5248
0
    if (type == R_RISCV_CALL
5249
0
        || type == R_RISCV_CALL_PLT)
5250
0
      relax_func = _bfd_riscv_relax_call;
5251
0
    else if (type == R_RISCV_HI20
5252
0
       || type == R_RISCV_LO12_I
5253
0
       || type == R_RISCV_LO12_S)
5254
0
      relax_func = _bfd_riscv_relax_lui;
5255
0
    else if (type == R_RISCV_TPREL_HI20
5256
0
       || type == R_RISCV_TPREL_ADD
5257
0
       || type == R_RISCV_TPREL_LO12_I
5258
0
       || type == R_RISCV_TPREL_LO12_S)
5259
0
      relax_func = _bfd_riscv_relax_tls_le;
5260
0
    else if (!bfd_link_pic (info)
5261
0
       && (type == R_RISCV_PCREL_HI20
5262
0
           || type == R_RISCV_PCREL_LO12_I
5263
0
           || type == R_RISCV_PCREL_LO12_S))
5264
0
      relax_func = _bfd_riscv_relax_pc;
5265
0
    else
5266
0
      continue;
5267
0
    riscv_relax_delete_bytes = _riscv_relax_delete_piecewise;
5268
5269
    /* Only relax this reloc if it is paired with R_RISCV_RELAX.  */
5270
0
    if (i == sec->reloc_count - 1
5271
0
        || ELF64_R_TYPE ((rel + 1)->r_info) != R_RISCV_RELAX
5272
0
        || rel->r_offset != (rel + 1)->r_offset)
5273
0
      continue;
5274
5275
    /* Skip over the R_RISCV_RELAX.  */
5276
0
    i++;
5277
0
  }
5278
0
      else if (info->relax_pass == 1 && type == R_RISCV_ALIGN)
5279
0
  {
5280
0
    relax_func = _bfd_riscv_relax_align;
5281
0
    riscv_relax_delete_bytes = _riscv_relax_delete_immediate;
5282
0
  }
5283
0
      else
5284
0
  continue;
5285
5286
0
      data->relocs = relocs;
5287
5288
      /* Read this BFD's contents if we haven't done so already.  */
5289
0
      if (!data->this_hdr.contents
5290
0
    && !bfd_malloc_and_get_section (abfd, sec, &data->this_hdr.contents))
5291
0
  goto fail;
5292
5293
      /* Read this BFD's symbols if we haven't done so already.  */
5294
0
      if (symtab_hdr->sh_info != 0
5295
0
    && !symtab_hdr->contents
5296
0
    && !(symtab_hdr->contents =
5297
0
         (unsigned char *) bfd_elf_get_elf_syms (abfd, symtab_hdr,
5298
0
                   symtab_hdr->sh_info,
5299
0
                   0, NULL, NULL, NULL)))
5300
0
  goto fail;
5301
5302
      /* Get the value of the symbol referred to by the reloc.  */
5303
0
      if (ELF64_R_SYM (rel->r_info) < symtab_hdr->sh_info)
5304
0
  {
5305
    /* A local symbol.  */
5306
0
    Elf_Internal_Sym *isym = ((Elf_Internal_Sym *) symtab_hdr->contents
5307
0
            + ELF64_R_SYM (rel->r_info));
5308
0
    reserve_size = (isym->st_size - rel->r_addend) > isym->st_size
5309
0
      ? 0 : isym->st_size - rel->r_addend;
5310
5311
    /* Relocate against local STT_GNU_IFUNC symbol.  we have created
5312
       a fake global symbol entry for this, so deal with the local ifunc
5313
       as a global.  */
5314
0
    if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
5315
0
      continue;
5316
5317
0
    if (isym->st_shndx == SHN_UNDEF)
5318
0
      sym_sec = sec, symval = rel->r_offset;
5319
0
    else
5320
0
      {
5321
0
        BFD_ASSERT (isym->st_shndx < elf_numsections (abfd));
5322
0
        sym_sec = elf_elfsections (abfd)[isym->st_shndx]->bfd_section;
5323
#if 0
5324
        /* The purpose of this code is unknown.  It breaks linker scripts
5325
     for embedded development that place sections at address zero.
5326
     This code is believed to be unnecessary.  Disabling it but not
5327
     yet removing it, in case something breaks.  */
5328
        if (sec_addr (sym_sec) == 0)
5329
    continue;
5330
#endif
5331
0
        symval = isym->st_value;
5332
0
      }
5333
0
    symtype = ELF_ST_TYPE (isym->st_info);
5334
0
  }
5335
0
      else
5336
0
  {
5337
0
    unsigned long indx;
5338
0
    struct elf_link_hash_entry *h;
5339
5340
0
    indx = ELF64_R_SYM (rel->r_info) - symtab_hdr->sh_info;
5341
0
    h = elf_sym_hashes (abfd)[indx];
5342
5343
0
    while (h->root.type == bfd_link_hash_indirect
5344
0
     || h->root.type == bfd_link_hash_warning)
5345
0
      h = (struct elf_link_hash_entry *) h->root.u.i.link;
5346
5347
    /* Disable the relaxation for ifunc.  */
5348
0
    if (h != NULL && h->type == STT_GNU_IFUNC)
5349
0
      continue;
5350
5351
    /* Maybe we should check UNDEFWEAK_NO_DYNAMIC_RELOC here?  But that
5352
       will break the undefweak relaxation testcases, so just make sure
5353
       we won't do relaxations for linker_def symbols in short-term.  */
5354
0
    if (h->root.type == bfd_link_hash_undefweak
5355
        /* The linker_def symbol like __ehdr_start that may be undefweak
5356
     for now, but will be guaranteed to be defined later.  */
5357
0
        && !h->root.linker_def
5358
0
        && (relax_func == _bfd_riscv_relax_lui
5359
0
      || relax_func == _bfd_riscv_relax_pc))
5360
0
      {
5361
        /* For the lui and auipc relaxations, since the symbol
5362
     value of an undefined weak symbol is always be zero,
5363
     we can optimize the patterns into a single LI/MV/ADDI
5364
     instruction.
5365
5366
     Note that, creating shared libraries and pie output may
5367
     break the rule above.  Fortunately, since we do not relax
5368
     pc relocs when creating shared libraries and pie output,
5369
     and the absolute address access for R_RISCV_HI20 isn't
5370
     allowed when "-fPIC" is set, the problem of creating shared
5371
     libraries can not happen currently.  Once we support the
5372
     auipc relaxations when creating shared libraries, then we will
5373
     need the more rigorous checking for this optimization.  */
5374
0
        undefined_weak = true;
5375
0
      }
5376
5377
    /* This line has to match the via_pltcheck in
5378
       riscv_elf_relocate_section in the R_RISCV_CALL[_PLT] case.  */
5379
0
    if (h->plt.offset != MINUS_ONE)
5380
0
      {
5381
0
        sym_sec = htab->elf.splt;
5382
0
        symval = h->plt.offset;
5383
0
      }
5384
0
    else if (undefined_weak)
5385
0
      {
5386
0
        symval = 0;
5387
0
        sym_sec = bfd_und_section_ptr;
5388
0
      }
5389
0
    else if ((h->root.type == bfd_link_hash_defined
5390
0
        || h->root.type == bfd_link_hash_defweak)
5391
0
       && h->root.u.def.section != NULL
5392
0
       && h->root.u.def.section->output_section != NULL)
5393
0
      {
5394
0
        symval = h->root.u.def.value;
5395
0
        sym_sec = h->root.u.def.section;
5396
0
      }
5397
0
    else
5398
0
      continue;
5399
5400
0
    if (h->type != STT_FUNC)
5401
0
      reserve_size =
5402
0
        (h->size - rel->r_addend) > h->size ? 0 : h->size - rel->r_addend;
5403
0
    symtype = h->type;
5404
0
  }
5405
5406
0
      if (sym_sec->sec_info_type == SEC_INFO_TYPE_MERGE
5407
0
          && (sym_sec->flags & SEC_MERGE))
5408
0
  {
5409
    /* At this stage in linking, no SEC_MERGE symbol has been
5410
       adjusted, so all references to such symbols need to be
5411
       passed through _bfd_merged_section_offset.  (Later, in
5412
       relocate_section, all SEC_MERGE symbols *except* for
5413
       section symbols have been adjusted.)
5414
5415
       gas may reduce relocations against symbols in SEC_MERGE
5416
       sections to a relocation against the section symbol when
5417
       the original addend was zero.  When the reloc is against
5418
       a section symbol we should include the addend in the
5419
       offset passed to _bfd_merged_section_offset, since the
5420
       location of interest is the original symbol.  On the
5421
       other hand, an access to "sym+addend" where "sym" is not
5422
       a section symbol should not include the addend;  Such an
5423
       access is presumed to be an offset from "sym";  The
5424
       location of interest is just "sym".  */
5425
0
     if (symtype == STT_SECTION)
5426
0
       symval += rel->r_addend;
5427
5428
0
     symval = _bfd_merged_section_offset (abfd, &sym_sec,
5429
0
            elf_section_data (sym_sec)->sec_info,
5430
0
            symval);
5431
5432
0
     if (symtype != STT_SECTION)
5433
0
       symval += rel->r_addend;
5434
0
  }
5435
0
      else
5436
0
  symval += rel->r_addend;
5437
5438
0
      symval += sec_addr (sym_sec);
5439
5440
0
      if (!relax_func (abfd, sec, sym_sec, info, rel, symval,
5441
0
           max_alignment, reserve_size, again,
5442
0
           &pcgp_relocs, undefined_weak))
5443
0
  goto fail;
5444
0
    }
5445
5446
  /* Resolve R_RISCV_DELETE relocations.  */
5447
0
  if (!riscv_relax_resolve_delete_relocs (abfd, sec, info, relocs))
5448
0
    goto fail;
5449
5450
0
  ret = true;
5451
5452
0
 fail:
5453
0
  if (relocs != data->relocs)
5454
0
    free (relocs);
5455
0
  riscv_free_pcgp_relocs (&pcgp_relocs, abfd, sec);
5456
5457
0
  return ret;
5458
0
}
5459
5460
#if ARCH_SIZE == 32
5461
# define PRSTATUS_SIZE      204
5462
# define PRSTATUS_OFFSET_PR_CURSIG  12
5463
# define PRSTATUS_OFFSET_PR_PID   24
5464
# define PRSTATUS_OFFSET_PR_REG   72
5465
# define ELF_GREGSET_T_SIZE   128
5466
# define PRPSINFO_SIZE      128
5467
# define PRPSINFO_OFFSET_PR_PID   16
5468
# define PRPSINFO_OFFSET_PR_FNAME 32
5469
# define PRPSINFO_OFFSET_PR_PSARGS  48
5470
# define PRPSINFO_PR_FNAME_LENGTH 16
5471
# define PRPSINFO_PR_PSARGS_LENGTH  80
5472
#else
5473
7
# define PRSTATUS_SIZE      376
5474
# define PRSTATUS_OFFSET_PR_CURSIG  12
5475
# define PRSTATUS_OFFSET_PR_PID   32
5476
7
# define PRSTATUS_OFFSET_PR_REG   112
5477
7
# define ELF_GREGSET_T_SIZE   256
5478
0
# define PRPSINFO_SIZE      136
5479
# define PRPSINFO_OFFSET_PR_PID   24
5480
0
# define PRPSINFO_OFFSET_PR_FNAME 40
5481
0
# define PRPSINFO_OFFSET_PR_PSARGS  56
5482
0
# define PRPSINFO_PR_FNAME_LENGTH 16
5483
0
# define PRPSINFO_PR_PSARGS_LENGTH  80
5484
#endif
5485
5486
/* Write PRSTATUS and PRPSINFO note into core file.  This will be called
5487
   before the generic code in elf.c.  By checking the compiler defines we
5488
   only perform any action here if the generic code would otherwise not be
5489
   able to help us.  The intention is that bare metal core dumps (where the
5490
   prstatus_t and/or prpsinfo_t might not be available) will use this code,
5491
   while non bare metal tools will use the generic elf code.  */
5492
5493
static char *
5494
riscv_write_core_note (bfd *abfd ATTRIBUTE_UNUSED,
5495
                       char *buf ATTRIBUTE_UNUSED,
5496
                       int *bufsiz ATTRIBUTE_UNUSED,
5497
                       int note_type ATTRIBUTE_UNUSED, ...)
5498
0
{
5499
0
  switch (note_type)
5500
0
    {
5501
0
    default:
5502
0
      return NULL;
5503
5504
#if !defined (HAVE_PRPSINFO_T)
5505
    case NT_PRPSINFO:
5506
      {
5507
  char data[PRPSINFO_SIZE] ATTRIBUTE_NONSTRING;
5508
  va_list ap;
5509
5510
  va_start (ap, note_type);
5511
  memset (data, 0, sizeof (data));
5512
  strncpy (data + PRPSINFO_OFFSET_PR_FNAME, va_arg (ap, const char *),
5513
                 PRPSINFO_PR_FNAME_LENGTH);
5514
#if GCC_VERSION == 8000 || GCC_VERSION == 8001
5515
  DIAGNOSTIC_PUSH;
5516
  /* GCC 8.0 and 8.1 warn about 80 equals destination size with
5517
     -Wstringop-truncation:
5518
     https://gcc.gnu.org/bugzilla/show_bug.cgi?id=85643
5519
   */
5520
  DIAGNOSTIC_IGNORE_STRINGOP_TRUNCATION;
5521
#endif
5522
  strncpy (data + PRPSINFO_OFFSET_PR_PSARGS, va_arg (ap, const char *),
5523
                 PRPSINFO_PR_PSARGS_LENGTH);
5524
#if GCC_VERSION == 8000 || GCC_VERSION == 8001
5525
  DIAGNOSTIC_POP;
5526
#endif
5527
  va_end (ap);
5528
  return elfcore_write_note (abfd, buf, bufsiz,
5529
           "CORE", note_type, data, sizeof (data));
5530
      }
5531
#endif /* !HAVE_PRPSINFO_T */
5532
5533
#if !defined (HAVE_PRSTATUS_T)
5534
    case NT_PRSTATUS:
5535
      {
5536
        char data[PRSTATUS_SIZE];
5537
        va_list ap;
5538
        long pid;
5539
        int cursig;
5540
        const void *greg;
5541
5542
        va_start (ap, note_type);
5543
        memset (data, 0, sizeof(data));
5544
        pid = va_arg (ap, long);
5545
        bfd_put_32 (abfd, pid, data + PRSTATUS_OFFSET_PR_PID);
5546
        cursig = va_arg (ap, int);
5547
        bfd_put_16 (abfd, cursig, data + PRSTATUS_OFFSET_PR_CURSIG);
5548
        greg = va_arg (ap, const void *);
5549
        memcpy (data + PRSTATUS_OFFSET_PR_REG, greg,
5550
                PRSTATUS_SIZE - PRSTATUS_OFFSET_PR_REG - ARCH_SIZE / 8);
5551
        va_end (ap);
5552
        return elfcore_write_note (abfd, buf, bufsiz,
5553
                                   "CORE", note_type, data, sizeof (data));
5554
      }
5555
#endif /* !HAVE_PRSTATUS_T */
5556
0
    }
5557
0
}
5558
5559
/* Support for core dump NOTE sections.  */
5560
5561
static bool
5562
riscv_elf_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
5563
109
{
5564
109
  switch (note->descsz)
5565
109
    {
5566
102
      default:
5567
102
  return false;
5568
5569
7
      case PRSTATUS_SIZE: /* sizeof(struct elf_prstatus) on Linux/RISC-V.  */
5570
  /* pr_cursig */
5571
7
  elf_tdata (abfd)->core->signal
5572
7
    = bfd_get_16 (abfd, note->descdata + PRSTATUS_OFFSET_PR_CURSIG);
5573
5574
  /* pr_pid */
5575
7
  elf_tdata (abfd)->core->lwpid
5576
7
    = bfd_get_32 (abfd, note->descdata + PRSTATUS_OFFSET_PR_PID);
5577
7
  break;
5578
109
    }
5579
5580
  /* Make a ".reg/999" section.  */
5581
7
  return _bfd_elfcore_make_pseudosection (abfd, ".reg", ELF_GREGSET_T_SIZE,
5582
7
            note->descpos + PRSTATUS_OFFSET_PR_REG);
5583
109
}
5584
5585
static bool
5586
riscv_elf_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
5587
44
{
5588
44
  switch (note->descsz)
5589
44
    {
5590
44
      default:
5591
44
  return false;
5592
5593
0
      case PRPSINFO_SIZE: /* sizeof(struct elf_prpsinfo) on Linux/RISC-V.  */
5594
  /* pr_pid */
5595
0
  elf_tdata (abfd)->core->pid
5596
0
    = bfd_get_32 (abfd, note->descdata + PRPSINFO_OFFSET_PR_PID);
5597
5598
  /* pr_fname */
5599
0
  elf_tdata (abfd)->core->program = _bfd_elfcore_strndup
5600
0
    (abfd, note->descdata + PRPSINFO_OFFSET_PR_FNAME,
5601
0
           PRPSINFO_PR_FNAME_LENGTH);
5602
5603
  /* pr_psargs */
5604
0
  elf_tdata (abfd)->core->command = _bfd_elfcore_strndup
5605
0
    (abfd, note->descdata + PRPSINFO_OFFSET_PR_PSARGS,
5606
0
           PRPSINFO_PR_PSARGS_LENGTH);
5607
0
  break;
5608
44
    }
5609
5610
  /* Note that for some reason, a spurious space is tacked
5611
     onto the end of the args in some (at least one anyway)
5612
     implementations, so strip it off if it exists.  */
5613
5614
0
  {
5615
0
    char *command = elf_tdata (abfd)->core->command;
5616
0
    int n = strlen (command);
5617
5618
0
    if (0 < n && command[n - 1] == ' ')
5619
0
      command[n - 1] = '\0';
5620
0
  }
5621
5622
0
  return true;
5623
44
}
5624
5625
/* Set the right mach type.  */
5626
5627
static bool
5628
riscv_elf_object_p (bfd *abfd)
5629
8.74k
{
5630
  /* There are only two mach types in RISCV currently.  */
5631
8.74k
  if (strcmp (abfd->xvec->name, "elf32-littleriscv") == 0
5632
8.74k
      || strcmp (abfd->xvec->name, "elf32-bigriscv") == 0)
5633
0
    bfd_default_set_arch_mach (abfd, bfd_arch_riscv, bfd_mach_riscv32);
5634
8.74k
  else
5635
8.74k
    bfd_default_set_arch_mach (abfd, bfd_arch_riscv, bfd_mach_riscv64);
5636
5637
8.74k
  return true;
5638
8.74k
}
5639
5640
/* Determine whether an object attribute tag takes an integer, a
5641
   string or both.  */
5642
5643
static int
5644
riscv_elf_obj_attrs_arg_type (int tag)
5645
201k
{
5646
201k
  return (tag & 1) != 0 ? ATTR_TYPE_FLAG_STR_VAL : ATTR_TYPE_FLAG_INT_VAL;
5647
201k
}
5648
5649
/* Do not choose mapping symbols as a function name.  */
5650
5651
static bfd_size_type
5652
riscv_maybe_function_sym (const asymbol *sym,
5653
        asection *sec,
5654
        bfd_vma *code_off)
5655
18.9k
{
5656
18.9k
  if (sym->flags & BSF_LOCAL
5657
18.9k
      && (riscv_elf_is_mapping_symbols (sym->name)
5658
14.7k
    || _bfd_elf_is_local_label_name (sec->owner, sym->name)))
5659
112
    return 0;
5660
5661
18.8k
  return _bfd_elf_maybe_function_sym (sym, sec, code_off);
5662
18.9k
}
5663
5664
/* Treat the following cases as target special symbols, they are
5665
   usually omitted.  */
5666
5667
static bool
5668
riscv_elf_is_target_special_symbol (bfd *abfd, asymbol *sym)
5669
0
{
5670
  /* PR27584, local and empty symbols.  Since they are usually
5671
     generated for pcrel relocations.  */
5672
0
  return (!sym->name[0]
5673
0
    || _bfd_elf_is_local_label_name (abfd, sym->name)
5674
    /* PR27916, mapping symbols.  */
5675
0
    || riscv_elf_is_mapping_symbols (sym->name));
5676
0
}
5677
5678
static int
5679
riscv_elf_additional_program_headers (bfd *abfd,
5680
              struct bfd_link_info *info ATTRIBUTE_UNUSED)
5681
0
{
5682
0
  int ret = 0;
5683
5684
  /* See if we need a PT_RISCV_ATTRIBUTES segment.  */
5685
0
  if (bfd_get_section_by_name (abfd, RISCV_ATTRIBUTES_SECTION_NAME))
5686
0
    ++ret;
5687
5688
0
  return ret;
5689
0
}
5690
5691
static bool
5692
riscv_elf_modify_segment_map (bfd *abfd,
5693
            struct bfd_link_info *info ATTRIBUTE_UNUSED)
5694
0
{
5695
0
  asection *s;
5696
0
  struct elf_segment_map *m, **pm;
5697
0
  size_t amt;
5698
5699
  /* If there is a .riscv.attributes section, we need a PT_RISCV_ATTRIBUTES
5700
     segment.  */
5701
0
  s = bfd_get_section_by_name (abfd, RISCV_ATTRIBUTES_SECTION_NAME);
5702
0
  if (s != NULL)
5703
0
    {
5704
0
      for (m = elf_seg_map (abfd); m != NULL; m = m->next)
5705
0
  if (m->p_type == PT_RISCV_ATTRIBUTES)
5706
0
    break;
5707
      /* If there is already a PT_RISCV_ATTRIBUTES header, avoid adding
5708
   another.  */
5709
0
      if (m == NULL)
5710
0
  {
5711
0
    amt = sizeof (*m);
5712
0
    m = bfd_zalloc (abfd, amt);
5713
0
    if (m == NULL)
5714
0
      return false;
5715
5716
0
    m->p_type = PT_RISCV_ATTRIBUTES;
5717
0
    m->count = 1;
5718
0
    m->sections[0] = s;
5719
5720
    /* We want to put it after the PHDR and INTERP segments.  */
5721
0
    pm = &elf_seg_map (abfd);
5722
0
    while (*pm != NULL
5723
0
     && ((*pm)->p_type == PT_PHDR
5724
0
         || (*pm)->p_type == PT_INTERP))
5725
0
      pm = &(*pm)->next;
5726
5727
0
    m->next = *pm;
5728
0
    *pm = m;
5729
0
  }
5730
0
    }
5731
5732
0
  return true;
5733
0
}
5734
5735
/* Merge non-visibility st_other attributes.  */
5736
5737
static void
5738
riscv_elf_merge_symbol_attribute (struct elf_link_hash_entry *h,
5739
          unsigned int st_other,
5740
          bool definition ATTRIBUTE_UNUSED,
5741
          bool dynamic ATTRIBUTE_UNUSED)
5742
0
{
5743
0
  unsigned int isym_sto = st_other & ~ELF_ST_VISIBILITY (-1);
5744
0
  unsigned int h_sto = h->other & ~ELF_ST_VISIBILITY (-1);
5745
5746
0
  if (isym_sto == h_sto)
5747
0
    return;
5748
5749
0
  if (isym_sto & ~STO_RISCV_VARIANT_CC)
5750
0
    _bfd_error_handler (_("unknown attribute for symbol `%s': 0x%02x"),
5751
0
      h->root.root.string, isym_sto);
5752
5753
0
  if (isym_sto & STO_RISCV_VARIANT_CC)
5754
0
    h->other |= STO_RISCV_VARIANT_CC;
5755
0
}
5756
5757
#define TARGET_LITTLE_SYM     riscv_elf64_vec
5758
#define TARGET_LITTLE_NAME      "elf64-littleriscv"
5759
#define TARGET_BIG_SYM        riscv_elf64_be_vec
5760
#define TARGET_BIG_NAME       "elf64-bigriscv"
5761
5762
#define elf_backend_reloc_type_class    riscv_reloc_type_class
5763
5764
#define bfd_elf64_bfd_reloc_name_lookup   riscv_reloc_name_lookup
5765
#define bfd_elf64_bfd_link_hash_table_create  riscv_elf_link_hash_table_create
5766
#define bfd_elf64_bfd_reloc_type_lookup   riscv_reloc_type_lookup
5767
#define bfd_elf64_bfd_merge_private_bfd_data \
5768
  _bfd_riscv_elf_merge_private_bfd_data
5769
#define bfd_elf64_bfd_is_target_special_symbol  riscv_elf_is_target_special_symbol
5770
5771
#define elf_backend_copy_indirect_symbol  riscv_elf_copy_indirect_symbol
5772
#define elf_backend_create_dynamic_sections riscv_elf_create_dynamic_sections
5773
#define elf_backend_check_relocs    riscv_elf_check_relocs
5774
#define elf_backend_adjust_dynamic_symbol riscv_elf_adjust_dynamic_symbol
5775
#define elf_backend_late_size_sections    riscv_elf_late_size_sections
5776
#define elf_backend_relocate_section    riscv_elf_relocate_section
5777
#define elf_backend_finish_dynamic_symbol riscv_elf_finish_dynamic_symbol
5778
#define elf_backend_finish_dynamic_sections riscv_elf_finish_dynamic_sections
5779
#define elf_backend_plt_sym_val     riscv_elf_plt_sym_val
5780
#define elf_backend_grok_prstatus   riscv_elf_grok_prstatus
5781
#define elf_backend_grok_psinfo     riscv_elf_grok_psinfo
5782
#define elf_backend_object_p      riscv_elf_object_p
5783
#define elf_backend_write_core_note   riscv_write_core_note
5784
#define elf_backend_maybe_function_sym    riscv_maybe_function_sym
5785
#define elf_info_to_howto_rel     NULL
5786
#define elf_info_to_howto     riscv_info_to_howto_rela
5787
#define bfd_elf64_bfd_relax_section   _bfd_riscv_relax_section
5788
#define bfd_elf64_mkobject      elf64_riscv_mkobject
5789
#define elf_backend_additional_program_headers \
5790
  riscv_elf_additional_program_headers
5791
#define elf_backend_modify_segment_map    riscv_elf_modify_segment_map
5792
#define elf_backend_merge_symbol_attribute  riscv_elf_merge_symbol_attribute
5793
5794
#define elf_backend_init_index_section    _bfd_elf_init_1_index_section
5795
5796
#define elf_backend_can_gc_sections   1
5797
#define elf_backend_can_refcount    1
5798
#define elf_backend_want_got_plt    1
5799
#define elf_backend_plt_readonly    1
5800
#define elf_backend_plt_alignment   4
5801
#define elf_backend_want_plt_sym    1
5802
#define elf_backend_got_header_size   (ARCH_SIZE / 8)
5803
#define elf_backend_want_dynrelro   1
5804
#define elf_backend_rela_normal     1
5805
#define elf_backend_default_execstack   0
5806
5807
#undef  elf_backend_obj_attrs_vendor
5808
#define elf_backend_obj_attrs_vendor    "riscv"
5809
#undef  elf_backend_obj_attrs_arg_type
5810
#define elf_backend_obj_attrs_arg_type    riscv_elf_obj_attrs_arg_type
5811
#undef  elf_backend_obj_attrs_section_type
5812
#define elf_backend_obj_attrs_section_type  SHT_RISCV_ATTRIBUTES
5813
#undef  elf_backend_obj_attrs_section
5814
#define elf_backend_obj_attrs_section   RISCV_ATTRIBUTES_SECTION_NAME
5815
#define elf_backend_obj_attrs_handle_unknown  riscv_elf_obj_attrs_handle_unknown
5816
5817
#include "elf64-target.h"