Coverage Report

Created: 2024-05-21 06:29

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