Coverage Report

Created: 2026-07-25 10:20

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