Coverage Report

Created: 2026-03-10 08:46

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