Coverage Report

Created: 2026-09-14 08:07

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