Coverage Report

Created: 2026-04-04 08:16

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