Coverage Report

Created: 2026-10-02 09:53

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