Coverage Report

Created: 2025-07-08 11:15

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