Coverage Report

Created: 2026-11-21 09:00

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