OpenCores
URL https://opencores.org/ocsvn/hf-risc/hf-risc/trunk

Subversion Repositories hf-risc

[/] [hf-risc/] [trunk/] [tools/] [riscv-gnu-toolchain-master/] [binutils/] [bfd/] [elfnn-riscv.c] - Blame information for rev 13

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 13 serginhofr
/* RISC-V-specific support for NN-bit ELF.
2
   Copyright 2011-2015 Free Software Foundation, Inc.
3
 
4
   Contributed by Andrew Waterman (waterman@cs.berkeley.edu) at UC Berkeley.
5
   Based on TILE-Gx and MIPS targets.
6
 
7
   This file is part of BFD, the Binary File Descriptor library.
8
 
9
   This program is free software; you can redistribute it and/or modify
10
   it under the terms of the GNU General Public License as published by
11
   the Free Software Foundation; either version 3 of the License, or
12
   (at your option) any later version.
13
 
14
   This program is distributed in the hope that it will be useful,
15
   but WITHOUT ANY WARRANTY; without even the implied warranty of
16
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
   GNU General Public License for more details.
18
 
19
   You should have received a copy of the GNU General Public License
20
   along with this program; see the file COPYING3. If not,
21
   see <http://www.gnu.org/licenses/>.  */
22
 
23
/* This file handles RISC-V ELF targets.  */
24
 
25
#include "sysdep.h"
26
#include "bfd.h"
27
#include "libbfd.h"
28
#include "bfdlink.h"
29
#include "genlink.h"
30
#include "elf-bfd.h"
31
#include "elfxx-riscv.h"
32
#include "elf/riscv.h"
33
#include "opcode/riscv.h"
34
 
35
#define ARCH_SIZE NN
36
 
37
#define MINUS_ONE ((bfd_vma)0 - 1)
38
 
39
#define RISCV_ELF_LOG_WORD_BYTES (ARCH_SIZE == 32 ? 2 : 3)
40
 
41
#define RISCV_ELF_WORD_BYTES (1 << RISCV_ELF_LOG_WORD_BYTES)
42
 
43
/* The name of the dynamic interpreter.  This is put in the .interp
44
   section.  */
45
 
46
#define ELF64_DYNAMIC_INTERPRETER "/lib/ld.so.1"
47
#define ELF32_DYNAMIC_INTERPRETER "/lib32/ld.so.1"
48
 
49
#define ELF_ARCH                        bfd_arch_riscv
50
#define ELF_TARGET_ID                   RISCV_ELF_DATA
51
#define ELF_MACHINE_CODE                EM_RISCV
52
#define ELF_MAXPAGESIZE                 0x1000
53
#define ELF_COMMONPAGESIZE              0x1000
54
 
55
/* The RISC-V linker needs to keep track of the number of relocs that it
56
   decides to copy as dynamic relocs in check_relocs for each symbol.
57
   This is so that it can later discard them if they are found to be
58
   unnecessary.  We store the information in a field extending the
59
   regular ELF linker hash table.  */
60
 
61
struct riscv_elf_dyn_relocs
62
{
63
  struct riscv_elf_dyn_relocs *next;
64
 
65
  /* The input section of the reloc.  */
66
  asection *sec;
67
 
68
  /* Total number of relocs copied for the input section.  */
69
  bfd_size_type count;
70
 
71
  /* Number of pc-relative relocs copied for the input section.  */
72
  bfd_size_type pc_count;
73
};
74
 
75
/* RISC-V ELF linker hash entry.  */
76
 
77
struct riscv_elf_link_hash_entry
78
{
79
  struct elf_link_hash_entry elf;
80
 
81
  /* Track dynamic relocs copied for this symbol.  */
82
  struct riscv_elf_dyn_relocs *dyn_relocs;
83
 
84
#define GOT_UNKNOWN     0
85
#define GOT_NORMAL      1
86
#define GOT_TLS_GD      2
87
#define GOT_TLS_IE      4
88
#define GOT_TLS_LE      8
89
  char tls_type;
90
};
91
 
92
#define riscv_elf_hash_entry(ent) \
93
  ((struct riscv_elf_link_hash_entry *)(ent))
94
 
95
struct _bfd_riscv_elf_obj_tdata
96
{
97
  struct elf_obj_tdata root;
98
 
99
  /* tls_type for each local got entry.  */
100
  char *local_got_tls_type;
101
};
102
 
103
#define _bfd_riscv_elf_tdata(abfd) \
104
  ((struct _bfd_riscv_elf_obj_tdata *) (abfd)->tdata.any)
105
 
106
#define _bfd_riscv_elf_local_got_tls_type(abfd) \
107
  (_bfd_riscv_elf_tdata (abfd)->local_got_tls_type)
108
 
109
#define _bfd_riscv_elf_tls_type(abfd, h, symndx)                \
110
  (*((h) != NULL ? &riscv_elf_hash_entry (h)->tls_type          \
111
     : &_bfd_riscv_elf_local_got_tls_type (abfd) [symndx]))
112
 
113
#define is_riscv_elf(bfd)                               \
114
  (bfd_get_flavour (bfd) == bfd_target_elf_flavour      \
115
   && elf_tdata (bfd) != NULL                           \
116
   && elf_object_id (bfd) == RISCV_ELF_DATA)
117
 
118
#include "elf/common.h"
119
#include "elf/internal.h"
120
 
121
struct riscv_elf_link_hash_table
122
{
123
  struct elf_link_hash_table elf;
124
 
125
  /* Short-cuts to get to dynamic linker sections.  */
126
  asection *sdynbss;
127
  asection *srelbss;
128
  asection *sdyntdata;
129
 
130
  /* Small local sym to section mapping cache.  */
131
  struct sym_cache sym_cache;
132
};
133
 
134
 
135
/* Get the RISC-V ELF linker hash table from a link_info structure.  */
136
#define riscv_elf_hash_table(p) \
137
  (elf_hash_table_id ((struct elf_link_hash_table *) ((p)->hash)) \
138
  == RISCV_ELF_DATA ? ((struct riscv_elf_link_hash_table *) ((p)->hash)) : NULL)
139
 
140
static void
141
riscv_info_to_howto_rela (bfd *abfd ATTRIBUTE_UNUSED,
142
                          arelent *cache_ptr,
143
                          Elf_Internal_Rela *dst)
144
{
145
  cache_ptr->howto = riscv_elf_rtype_to_howto (ELFNN_R_TYPE (dst->r_info));
146
}
147
 
148
static void
149
riscv_elf_append_rela (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
150
{
151
  const struct elf_backend_data *bed;
152
  bfd_byte *loc;
153
 
154
  bed = get_elf_backend_data (abfd);
155
  loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rela);
156
  bed->s->swap_reloca_out (abfd, rel, loc);
157
}
158
 
159
/* PLT/GOT stuff */
160
 
161
#define PLT_HEADER_INSNS 8
162
#define PLT_ENTRY_INSNS 4
163
#define PLT_HEADER_SIZE (PLT_HEADER_INSNS * 4)
164
#define PLT_ENTRY_SIZE (PLT_ENTRY_INSNS * 4)
165
 
166
#define GOT_ENTRY_SIZE RISCV_ELF_WORD_BYTES
167
 
168
#define GOTPLT_HEADER_SIZE (2 * GOT_ENTRY_SIZE)
169
 
170
#define sec_addr(sec) ((sec)->output_section->vma + (sec)->output_offset)
171
 
172
static bfd_vma
173
riscv_elf_got_plt_val (bfd_vma plt_index, struct bfd_link_info *info)
174
{
175
  return sec_addr (riscv_elf_hash_table (info)->elf.sgotplt)
176
         + GOTPLT_HEADER_SIZE + (plt_index * GOT_ENTRY_SIZE);
177
}
178
 
179
#if ARCH_SIZE == 32
180
# define MATCH_LREG MATCH_LW
181
#else
182
# define MATCH_LREG MATCH_LD
183
#endif
184
 
185
/* Generate a PLT header.  */
186
 
187
static void
188
riscv_make_plt_header (bfd_vma gotplt_addr, bfd_vma addr, uint32_t *entry)
189
{
190
  bfd_vma gotplt_offset_high = RISCV_PCREL_HIGH_PART (gotplt_addr, addr);
191
  bfd_vma gotplt_offset_low = RISCV_PCREL_LOW_PART (gotplt_addr, addr);
192
 
193
  /* auipc  t2, %hi(.got.plt)
194
     sub    t1, t1, t3               # shifted .got.plt offset + hdr size + 12
195
     l[w|d] t3, %lo(.got.plt)(t2)    # _dl_runtime_resolve
196
     addi   t1, t1, -(hdr size + 12) # shifted .got.plt offset
197
     addi   t0, t2, %lo(.got.plt)    # &.got.plt
198
     srli   t1, t1, log2(16/PTRSIZE) # .got.plt offset
199
     l[w|d] t0, PTRSIZE(t0)          # link map
200
     jr     t3 */
201
 
202
  entry[0] = RISCV_UTYPE (AUIPC, X_T2, gotplt_offset_high);
203
  entry[1] = RISCV_RTYPE (SUB, X_T1, X_T1, X_T3);
204
  entry[2] = RISCV_ITYPE (LREG, X_T3, X_T2, gotplt_offset_low);
205
  entry[3] = RISCV_ITYPE (ADDI, X_T1, X_T1, -(PLT_HEADER_SIZE + 12));
206
  entry[4] = RISCV_ITYPE (ADDI, X_T0, X_T2, gotplt_offset_low);
207
  entry[5] = RISCV_ITYPE (SRLI, X_T1, X_T1, 4 - RISCV_ELF_LOG_WORD_BYTES);
208
  entry[6] = RISCV_ITYPE (LREG, X_T0, X_T0, RISCV_ELF_WORD_BYTES);
209
  entry[7] = RISCV_ITYPE (JALR, 0, X_T3, 0);
210
}
211
 
212
/* Generate a PLT entry.  */
213
 
214
static void
215
riscv_make_plt_entry (bfd_vma got, bfd_vma addr, uint32_t *entry)
216
{
217
  /* auipc  t3, %hi(.got.plt entry)
218
     l[w|d] t3, %lo(.got.plt entry)(t3)
219
     jalr   t1, t3
220
     nop */
221
 
222
  entry[0] = RISCV_UTYPE (AUIPC, X_T3, RISCV_PCREL_HIGH_PART (got, addr));
223
  entry[1] = RISCV_ITYPE (LREG,  X_T3, X_T3, RISCV_PCREL_LOW_PART(got, addr));
224
  entry[2] = RISCV_ITYPE (JALR, X_T1, X_T3, 0);
225
  entry[3] = RISCV_NOP;
226
}
227
 
228
/* Create an entry in an RISC-V ELF linker hash table.  */
229
 
230
static struct bfd_hash_entry *
231
link_hash_newfunc (struct bfd_hash_entry *entry,
232
                   struct bfd_hash_table *table, const char *string)
233
{
234
  /* Allocate the structure if it has not already been allocated by a
235
     subclass.  */
236
  if (entry == NULL)
237
    {
238
      entry =
239
        bfd_hash_allocate (table,
240
                           sizeof (struct riscv_elf_link_hash_entry));
241
      if (entry == NULL)
242
        return entry;
243
    }
244
 
245
  /* Call the allocation method of the superclass.  */
246
  entry = _bfd_elf_link_hash_newfunc (entry, table, string);
247
  if (entry != NULL)
248
    {
249
      struct riscv_elf_link_hash_entry *eh;
250
 
251
      eh = (struct riscv_elf_link_hash_entry *) entry;
252
      eh->dyn_relocs = NULL;
253
      eh->tls_type = GOT_UNKNOWN;
254
    }
255
 
256
  return entry;
257
}
258
 
259
/* Create a RISC-V ELF linker hash table.  */
260
 
261
static struct bfd_link_hash_table *
262
riscv_elf_link_hash_table_create (bfd *abfd)
263
{
264
  struct riscv_elf_link_hash_table *ret;
265
  bfd_size_type amt = sizeof (struct riscv_elf_link_hash_table);
266
 
267
  ret = (struct riscv_elf_link_hash_table *) bfd_zmalloc (amt);
268
  if (ret == NULL)
269
    return NULL;
270
 
271
  if (!_bfd_elf_link_hash_table_init (&ret->elf, abfd, link_hash_newfunc,
272
                                      sizeof (struct riscv_elf_link_hash_entry),
273
                                      RISCV_ELF_DATA))
274
    {
275
      free (ret);
276
      return NULL;
277
    }
278
 
279
  return &ret->elf.root;
280
}
281
 
282
/* Create the .got section.  */
283
 
284
static bfd_boolean
285
riscv_elf_create_got_section (bfd *abfd, struct bfd_link_info *info)
286
{
287
  flagword flags;
288
  asection *s, *s_got;
289
  struct elf_link_hash_entry *h;
290
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
291
  struct elf_link_hash_table *htab = elf_hash_table (info);
292
 
293
  /* This function may be called more than once.  */
294
  s = bfd_get_linker_section (abfd, ".got");
295
  if (s != NULL)
296
    return TRUE;
297
 
298
  flags = bed->dynamic_sec_flags;
299
 
300
  s = bfd_make_section_anyway_with_flags (abfd,
301
                                          (bed->rela_plts_and_copies_p
302
                                           ? ".rela.got" : ".rel.got"),
303
                                          (bed->dynamic_sec_flags
304
                                           | SEC_READONLY));
305
  if (s == NULL
306
      || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
307
    return FALSE;
308
  htab->srelgot = s;
309
 
310
  s = s_got = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
311
  if (s == NULL
312
      || !bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
313
    return FALSE;
314
  htab->sgot = s;
315
 
316
  /* The first bit of the global offset table is the header.  */
317
  s->size += bed->got_header_size;
318
 
319
  if (bed->want_got_plt)
320
    {
321
      s = bfd_make_section_anyway_with_flags (abfd, ".got.plt", flags);
322
      if (s == NULL
323
          || !bfd_set_section_alignment (abfd, s,
324
                                         bed->s->log_file_align))
325
        return FALSE;
326
      htab->sgotplt = s;
327
 
328
      /* Reserve room for the header.  */
329
      s->size += GOTPLT_HEADER_SIZE;
330
    }
331
 
332
  if (bed->want_got_sym)
333
    {
334
      /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
335
         section.  We don't do this in the linker script because we don't want
336
         to define the symbol if we are not creating a global offset
337
         table.  */
338
      h = _bfd_elf_define_linkage_sym (abfd, info, s_got,
339
                                       "_GLOBAL_OFFSET_TABLE_");
340
      elf_hash_table (info)->hgot = h;
341
      if (h == NULL)
342
        return FALSE;
343
    }
344
 
345
  return TRUE;
346
}
347
 
348
/* Create .plt, .rela.plt, .got, .got.plt, .rela.got, .dynbss, and
349
   .rela.bss sections in DYNOBJ, and set up shortcuts to them in our
350
   hash table.  */
351
 
352
static bfd_boolean
353
riscv_elf_create_dynamic_sections (bfd *dynobj,
354
                                   struct bfd_link_info *info)
355
{
356
  struct riscv_elf_link_hash_table *htab;
357
 
358
  htab = riscv_elf_hash_table (info);
359
  BFD_ASSERT (htab != NULL);
360
 
361
  if (!riscv_elf_create_got_section (dynobj, info))
362
    return FALSE;
363
 
364
  if (!_bfd_elf_create_dynamic_sections (dynobj, info))
365
    return FALSE;
366
 
367
  htab->sdynbss = bfd_get_linker_section (dynobj, ".dynbss");
368
  if (!info->shared)
369
    {
370
      htab->srelbss = bfd_get_linker_section (dynobj, ".rela.bss");
371
      htab->sdyntdata =
372
        bfd_make_section_anyway_with_flags (dynobj, ".tdata.dyn",
373
                                            SEC_ALLOC | SEC_THREAD_LOCAL);
374
    }
375
 
376
  if (!htab->elf.splt || !htab->elf.srelplt || !htab->sdynbss
377
      || (!info->shared && (!htab->srelbss || !htab->sdyntdata)))
378
    abort ();
379
 
380
  return TRUE;
381
}
382
 
383
/* Copy the extra info we tack onto an elf_link_hash_entry.  */
384
 
385
static void
386
riscv_elf_copy_indirect_symbol (struct bfd_link_info *info,
387
                                struct elf_link_hash_entry *dir,
388
                                struct elf_link_hash_entry *ind)
389
{
390
  struct riscv_elf_link_hash_entry *edir, *eind;
391
 
392
  edir = (struct riscv_elf_link_hash_entry *) dir;
393
  eind = (struct riscv_elf_link_hash_entry *) ind;
394
 
395
  if (eind->dyn_relocs != NULL)
396
    {
397
      if (edir->dyn_relocs != NULL)
398
        {
399
          struct riscv_elf_dyn_relocs **pp;
400
          struct riscv_elf_dyn_relocs *p;
401
 
402
          /* Add reloc counts against the indirect sym to the direct sym
403
             list.  Merge any entries against the same section.  */
404
          for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
405
            {
406
              struct riscv_elf_dyn_relocs *q;
407
 
408
              for (q = edir->dyn_relocs; q != NULL; q = q->next)
409
                if (q->sec == p->sec)
410
                  {
411
                    q->pc_count += p->pc_count;
412
                    q->count += p->count;
413
                    *pp = p->next;
414
                    break;
415
                  }
416
              if (q == NULL)
417
                pp = &p->next;
418
            }
419
          *pp = edir->dyn_relocs;
420
        }
421
 
422
      edir->dyn_relocs = eind->dyn_relocs;
423
      eind->dyn_relocs = NULL;
424
    }
425
 
426
  if (ind->root.type == bfd_link_hash_indirect
427
      && dir->got.refcount <= 0)
428
    {
429
      edir->tls_type = eind->tls_type;
430
      eind->tls_type = GOT_UNKNOWN;
431
    }
432
  _bfd_elf_link_hash_copy_indirect (info, dir, ind);
433
}
434
 
435
static bfd_boolean
436
riscv_elf_record_tls_type (bfd *abfd, struct elf_link_hash_entry *h,
437
                           unsigned long symndx, char tls_type)
438
{
439
  char *new_tls_type = &_bfd_riscv_elf_tls_type (abfd, h, symndx);
440
  *new_tls_type |= tls_type;
441
  if ((*new_tls_type & GOT_NORMAL) && (*new_tls_type & ~GOT_NORMAL))
442
    {
443
      (*_bfd_error_handler)
444
        (_("%B: `%s' accessed both as normal and thread local symbol"),
445
         abfd, h ? h->root.root.string : "<local>");
446
      return FALSE;
447
    }
448
  return TRUE;
449
}
450
 
451
static bfd_boolean
452
riscv_elf_record_got_reference (bfd *abfd, struct bfd_link_info *info,
453
                                struct elf_link_hash_entry *h, long symndx)
454
{
455
  struct riscv_elf_link_hash_table *htab = riscv_elf_hash_table (info);
456
  Elf_Internal_Shdr *symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
457
 
458
  if (htab->elf.sgot == NULL)
459
    {
460
      if (!riscv_elf_create_got_section (htab->elf.dynobj, info))
461
        return FALSE;
462
    }
463
 
464
  if (h != NULL)
465
    {
466
      h->got.refcount += 1;
467
      return TRUE;
468
    }
469
 
470
  /* This is a global offset table entry for a local symbol.  */
471
  if (elf_local_got_refcounts (abfd) == NULL)
472
    {
473
      bfd_size_type size = symtab_hdr->sh_info * (sizeof (bfd_vma) + 1);
474
      if (!(elf_local_got_refcounts (abfd) = bfd_zalloc (abfd, size)))
475
        return FALSE;
476
      _bfd_riscv_elf_local_got_tls_type (abfd)
477
        = (char *) (elf_local_got_refcounts (abfd) + symtab_hdr->sh_info);
478
    }
479
  elf_local_got_refcounts (abfd) [symndx] += 1;
480
 
481
  return TRUE;
482
}
483
 
484
static bfd_boolean
485
bad_static_reloc (bfd *abfd, unsigned r_type, struct elf_link_hash_entry *h)
486
{
487
  (*_bfd_error_handler)
488
    (_("%B: relocation %s against `%s' can not be used when making a shared "
489
       "object; recompile with -fPIC"),
490
      abfd, riscv_elf_rtype_to_howto (r_type)->name,
491
      h != NULL ? h->root.root.string : "a local symbol");
492
  bfd_set_error (bfd_error_bad_value);
493
  return FALSE;
494
}
495
/* Look through the relocs for a section during the first phase, and
496
   allocate space in the global offset table or procedure linkage
497
   table.  */
498
 
499
static bfd_boolean
500
riscv_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
501
                        asection *sec, const Elf_Internal_Rela *relocs)
502
{
503
  struct riscv_elf_link_hash_table *htab;
504
  Elf_Internal_Shdr *symtab_hdr;
505
  struct elf_link_hash_entry **sym_hashes;
506
  const Elf_Internal_Rela *rel;
507
  asection *sreloc = NULL;
508
 
509
  if (info->relocatable)
510
    return TRUE;
511
 
512
  htab = riscv_elf_hash_table (info);
513
  symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
514
  sym_hashes = elf_sym_hashes (abfd);
515
 
516
  if (htab->elf.dynobj == NULL)
517
    htab->elf.dynobj = abfd;
518
 
519
  for (rel = relocs; rel < relocs + sec->reloc_count; rel++)
520
    {
521
      unsigned int r_type;
522
      unsigned long r_symndx;
523
      struct elf_link_hash_entry *h;
524
 
525
      r_symndx = ELFNN_R_SYM (rel->r_info);
526
      r_type = ELFNN_R_TYPE (rel->r_info);
527
 
528
      if (r_symndx >= NUM_SHDR_ENTRIES (symtab_hdr))
529
        {
530
          (*_bfd_error_handler) (_("%B: bad symbol index: %d"),
531
                                 abfd, r_symndx);
532
          return FALSE;
533
        }
534
 
535
      if (r_symndx < symtab_hdr->sh_info)
536
        h = NULL;
537
      else
538
        {
539
          h = sym_hashes[r_symndx - symtab_hdr->sh_info];
540
          while (h->root.type == bfd_link_hash_indirect
541
                 || h->root.type == bfd_link_hash_warning)
542
            h = (struct elf_link_hash_entry *) h->root.u.i.link;
543
 
544
          /* PR15323, ref flags aren't set for references in the same
545
             object.  */
546
          h->root.non_ir_ref = 1;
547
        }
548
 
549
      switch (r_type)
550
        {
551
        case R_RISCV_TLS_GD_HI20:
552
          if (!riscv_elf_record_got_reference (abfd, info, h, r_symndx)
553
              || !riscv_elf_record_tls_type (abfd, h, r_symndx, GOT_TLS_GD))
554
            return FALSE;
555
          break;
556
 
557
        case R_RISCV_TLS_GOT_HI20:
558
          if (info->shared)
559
            info->flags |= DF_STATIC_TLS;
560
          if (!riscv_elf_record_got_reference (abfd, info, h, r_symndx)
561
              || !riscv_elf_record_tls_type (abfd, h, r_symndx, GOT_TLS_IE))
562
            return FALSE;
563
          break;
564
 
565
        case R_RISCV_GOT_HI20:
566
          if (!riscv_elf_record_got_reference (abfd, info, h, r_symndx)
567
              || !riscv_elf_record_tls_type (abfd, h, r_symndx, GOT_NORMAL))
568
            return FALSE;
569
          break;
570
 
571
        case R_RISCV_CALL_PLT:
572
          /* This symbol requires a procedure linkage table entry.  We
573
             actually build the entry in adjust_dynamic_symbol,
574
             because this might be a case of linking PIC code without
575
             linking in any dynamic objects, in which case we don't
576
             need to generate a procedure linkage table after all.  */
577
 
578
          if (h != NULL)
579
            {
580
              h->needs_plt = 1;
581
              h->plt.refcount += 1;
582
            }
583
          break;
584
 
585
        case R_RISCV_CALL:
586
        case R_RISCV_JAL:
587
        case R_RISCV_BRANCH:
588
        case R_RISCV_RVC_BRANCH:
589
        case R_RISCV_RVC_JUMP:
590
        case R_RISCV_PCREL_HI20:
591
          /* In shared libraries, these relocs are known to bind locally.  */
592
          if (info->shared)
593
            break;
594
          goto static_reloc;
595
 
596
        case R_RISCV_TPREL_HI20:
597
          if (!info->executable)
598
            return bad_static_reloc (abfd, r_type, h);
599
          if (h != NULL)
600
            riscv_elf_record_tls_type (abfd, h, r_symndx, GOT_TLS_LE);
601
          goto static_reloc;
602
 
603
        case R_RISCV_HI20:
604
          if (info->shared)
605
            return bad_static_reloc (abfd, r_type, h);
606
          /* Fall through.  */
607
 
608
        case R_RISCV_COPY:
609
        case R_RISCV_JUMP_SLOT:
610
        case R_RISCV_RELATIVE:
611
        case R_RISCV_64:
612
        case R_RISCV_32:
613
          /* Fall through.  */
614
 
615
        static_reloc:
616
          /* This reloc might not bind locally.  */
617
          if (h != NULL)
618
            h->non_got_ref = 1;
619
 
620
          if (h != NULL && !info->shared)
621
            {
622
              /* We may need a .plt entry if the function this reloc
623
                 refers to is in a shared lib.  */
624
              h->plt.refcount += 1;
625
            }
626
 
627
          /* If we are creating a shared library, and this is a reloc
628
             against a global symbol, or a non PC relative reloc
629
             against a local symbol, then we need to copy the reloc
630
             into the shared library.  However, if we are linking with
631
             -Bsymbolic, we do not need to copy a reloc against a
632
             global symbol which is defined in an object we are
633
             including in the link (i.e., DEF_REGULAR is set).  At
634
             this point we have not seen all the input files, so it is
635
             possible that DEF_REGULAR is not set now but will be set
636
             later (it is never cleared).  In case of a weak definition,
637
             DEF_REGULAR may be cleared later by a strong definition in
638
             a shared library.  We account for that possibility below by
639
             storing information in the relocs_copied field of the hash
640
             table entry.  A similar situation occurs when creating
641
             shared libraries and symbol visibility changes render the
642
             symbol local.
643
 
644
             If on the other hand, we are creating an executable, we
645
             may need to keep relocations for symbols satisfied by a
646
             dynamic library if we manage to avoid copy relocs for the
647
             symbol.  */
648
          if ((info->shared
649
               && (sec->flags & SEC_ALLOC) != 0
650
               && (! riscv_elf_rtype_to_howto (r_type)->pc_relative
651
                   || (h != NULL
652
                       && (! info->symbolic
653
                           || h->root.type == bfd_link_hash_defweak
654
                           || !h->def_regular))))
655
              || (!info->shared
656
                  && (sec->flags & SEC_ALLOC) != 0
657
                  && h != NULL
658
                  && (h->root.type == bfd_link_hash_defweak
659
                      || !h->def_regular)))
660
            {
661
              struct riscv_elf_dyn_relocs *p;
662
              struct riscv_elf_dyn_relocs **head;
663
 
664
              /* When creating a shared object, we must copy these
665
                 relocs into the output file.  We create a reloc
666
                 section in dynobj and make room for the reloc.  */
667
              if (sreloc == NULL)
668
                {
669
                  sreloc = _bfd_elf_make_dynamic_reloc_section
670
                    (sec, htab->elf.dynobj, RISCV_ELF_LOG_WORD_BYTES,
671
                    abfd, /*rela?*/ TRUE);
672
 
673
                  if (sreloc == NULL)
674
                    return FALSE;
675
                }
676
 
677
              /* If this is a global symbol, we count the number of
678
                 relocations we need for this symbol.  */
679
              if (h != NULL)
680
                head = &((struct riscv_elf_link_hash_entry *) h)->dyn_relocs;
681
              else
682
                {
683
                  /* Track dynamic relocs needed for local syms too.
684
                     We really need local syms available to do this
685
                     easily.  Oh well.  */
686
 
687
                  asection *s;
688
                  void *vpp;
689
                  Elf_Internal_Sym *isym;
690
 
691
                  isym = bfd_sym_from_r_symndx (&htab->sym_cache,
692
                                                abfd, r_symndx);
693
                  if (isym == NULL)
694
                    return FALSE;
695
 
696
                  s = bfd_section_from_elf_index (abfd, isym->st_shndx);
697
                  if (s == NULL)
698
                    s = sec;
699
 
700
                  vpp = &elf_section_data (s)->local_dynrel;
701
                  head = (struct riscv_elf_dyn_relocs **) vpp;
702
                }
703
 
704
              p = *head;
705
              if (p == NULL || p->sec != sec)
706
                {
707
                  bfd_size_type amt = sizeof *p;
708
                  p = ((struct riscv_elf_dyn_relocs *)
709
                       bfd_alloc (htab->elf.dynobj, amt));
710
                  if (p == NULL)
711
                    return FALSE;
712
                  p->next = *head;
713
                  *head = p;
714
                  p->sec = sec;
715
                  p->count = 0;
716
                  p->pc_count = 0;
717
                }
718
 
719
              p->count += 1;
720
              p->pc_count += riscv_elf_rtype_to_howto (r_type)->pc_relative;
721
            }
722
 
723
          break;
724
 
725
        case R_RISCV_GNU_VTINHERIT:
726
          if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
727
            return FALSE;
728
          break;
729
 
730
        case R_RISCV_GNU_VTENTRY:
731
          if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
732
            return FALSE;
733
          break;
734
 
735
        default:
736
          break;
737
        }
738
    }
739
 
740
  return TRUE;
741
}
742
 
743
static asection *
744
riscv_elf_gc_mark_hook (asection *sec,
745
                        struct bfd_link_info *info,
746
                        Elf_Internal_Rela *rel,
747
                        struct elf_link_hash_entry *h,
748
                        Elf_Internal_Sym *sym)
749
{
750
  if (h != NULL)
751
    switch (ELFNN_R_TYPE (rel->r_info))
752
      {
753
      case R_RISCV_GNU_VTINHERIT:
754
      case R_RISCV_GNU_VTENTRY:
755
        return NULL;
756
      }
757
 
758
  return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
759
}
760
 
761
/* Update the got entry reference counts for the section being removed.  */
762
static bfd_boolean
763
riscv_elf_gc_sweep_hook (bfd *abfd, struct bfd_link_info *info,
764
                         asection *sec, const Elf_Internal_Rela *relocs)
765
{
766
  const Elf_Internal_Rela *rel, *relend;
767
  Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (abfd);
768
  struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (abfd);
769
  bfd_signed_vma *local_got_refcounts = elf_local_got_refcounts (abfd);
770
 
771
  if (info->relocatable)
772
    return TRUE;
773
 
774
  elf_section_data (sec)->local_dynrel = NULL;
775
 
776
  for (rel = relocs, relend = relocs + sec->reloc_count; rel < relend; rel++)
777
    {
778
      unsigned long r_symndx;
779
      struct elf_link_hash_entry *h = NULL;
780
 
781
      r_symndx = ELFNN_R_SYM (rel->r_info);
782
      if (r_symndx >= symtab_hdr->sh_info)
783
        {
784
          struct riscv_elf_link_hash_entry *eh;
785
          struct riscv_elf_dyn_relocs **pp;
786
          struct riscv_elf_dyn_relocs *p;
787
 
788
          h = sym_hashes[r_symndx - symtab_hdr->sh_info];
789
          while (h->root.type == bfd_link_hash_indirect
790
                 || h->root.type == bfd_link_hash_warning)
791
            h = (struct elf_link_hash_entry *) h->root.u.i.link;
792
          eh = (struct riscv_elf_link_hash_entry *) h;
793
          for (pp = &eh->dyn_relocs; (p = *pp) != NULL; pp = &p->next)
794
            if (p->sec == sec)
795
              {
796
                /* Everything must go for SEC.  */
797
                *pp = p->next;
798
                break;
799
              }
800
        }
801
 
802
      switch (ELFNN_R_TYPE (rel->r_info))
803
        {
804
        case R_RISCV_GOT_HI20:
805
        case R_RISCV_TLS_GOT_HI20:
806
        case R_RISCV_TLS_GD_HI20:
807
          if (h != NULL)
808
            {
809
              if (h->got.refcount > 0)
810
                h->got.refcount--;
811
            }
812
          else
813
            {
814
              if (local_got_refcounts &&
815
                  local_got_refcounts[r_symndx] > 0)
816
                local_got_refcounts[r_symndx]--;
817
            }
818
          break;
819
 
820
        case R_RISCV_HI20:
821
        case R_RISCV_PCREL_HI20:
822
        case R_RISCV_COPY:
823
        case R_RISCV_JUMP_SLOT:
824
        case R_RISCV_RELATIVE:
825
        case R_RISCV_64:
826
        case R_RISCV_32:
827
        case R_RISCV_BRANCH:
828
        case R_RISCV_CALL:
829
        case R_RISCV_JAL:
830
        case R_RISCV_RVC_BRANCH:
831
        case R_RISCV_RVC_JUMP:
832
          if (info->shared)
833
            break;
834
          /* Fall through.  */
835
 
836
        case R_RISCV_CALL_PLT:
837
          if (h != NULL)
838
            {
839
              if (h->plt.refcount > 0)
840
                h->plt.refcount--;
841
            }
842
          break;
843
 
844
        default:
845
          break;
846
        }
847
    }
848
 
849
  return TRUE;
850
}
851
 
852
/* Adjust a symbol defined by a dynamic object and referenced by a
853
   regular object.  The current definition is in some section of the
854
   dynamic object, but we're not including those sections.  We have to
855
   change the definition to something the rest of the link can
856
   understand.  */
857
 
858
static bfd_boolean
859
riscv_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
860
                                 struct elf_link_hash_entry *h)
861
{
862
  struct riscv_elf_link_hash_table *htab;
863
  struct riscv_elf_link_hash_entry * eh;
864
  struct riscv_elf_dyn_relocs *p;
865
  bfd *dynobj;
866
  asection *s;
867
 
868
  htab = riscv_elf_hash_table (info);
869
  BFD_ASSERT (htab != NULL);
870
 
871
  dynobj = htab->elf.dynobj;
872
 
873
  /* Make sure we know what is going on here.  */
874
  BFD_ASSERT (dynobj != NULL
875
              && (h->needs_plt
876
                  || h->type == STT_GNU_IFUNC
877
                  || h->u.weakdef != NULL
878
                  || (h->def_dynamic
879
                      && h->ref_regular
880
                      && !h->def_regular)));
881
 
882
  /* If this is a function, put it in the procedure linkage table.  We
883
     will fill in the contents of the procedure linkage table later
884
     (although we could actually do it here).  */
885
  if (h->type == STT_FUNC || h->type == STT_GNU_IFUNC || h->needs_plt)
886
    {
887
      if (h->plt.refcount <= 0
888
          || SYMBOL_CALLS_LOCAL (info, h)
889
          || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
890
              && h->root.type == bfd_link_hash_undefweak))
891
        {
892
          /* This case can occur if we saw a R_RISCV_CALL_PLT reloc in an
893
             input file, but the symbol was never referred to by a dynamic
894
             object, or if all references were garbage collected.  In such
895
             a case, we don't actually need to build a PLT entry.  */
896
          h->plt.offset = (bfd_vma) -1;
897
          h->needs_plt = 0;
898
        }
899
 
900
      return TRUE;
901
    }
902
  else
903
    h->plt.offset = (bfd_vma) -1;
904
 
905
  /* If this is a weak symbol, and there is a real definition, the
906
     processor independent code will have arranged for us to see the
907
     real definition first, and we can just use the same value.  */
908
  if (h->u.weakdef != NULL)
909
    {
910
      BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
911
                  || h->u.weakdef->root.type == bfd_link_hash_defweak);
912
      h->root.u.def.section = h->u.weakdef->root.u.def.section;
913
      h->root.u.def.value = h->u.weakdef->root.u.def.value;
914
      return TRUE;
915
    }
916
 
917
  /* This is a reference to a symbol defined by a dynamic object which
918
     is not a function.  */
919
 
920
  /* If we are creating a shared library, we must presume that the
921
     only references to the symbol are via the global offset table.
922
     For such cases we need not do anything here; the relocations will
923
     be handled correctly by relocate_section.  */
924
  if (info->shared)
925
    return TRUE;
926
 
927
  /* If there are no references to this symbol that do not use the
928
     GOT, we don't need to generate a copy reloc.  */
929
  if (!h->non_got_ref)
930
    return TRUE;
931
 
932
  /* If -z nocopyreloc was given, we won't generate them either.  */
933
  if (info->nocopyreloc)
934
    {
935
      h->non_got_ref = 0;
936
      return TRUE;
937
    }
938
 
939
  eh = (struct riscv_elf_link_hash_entry *) h;
940
  for (p = eh->dyn_relocs; p != NULL; p = p->next)
941
    {
942
      s = p->sec->output_section;
943
      if (s != NULL && (s->flags & SEC_READONLY) != 0)
944
        break;
945
    }
946
 
947
  /* If we didn't find any dynamic relocs in read-only sections, then
948
     we'll be keeping the dynamic relocs and avoiding the copy reloc.  */
949
  if (p == NULL)
950
    {
951
      h->non_got_ref = 0;
952
      return TRUE;
953
    }
954
 
955
  /* We must allocate the symbol in our .dynbss section, which will
956
     become part of the .bss section of the executable.  There will be
957
     an entry for this symbol in the .dynsym section.  The dynamic
958
     object will contain position independent code, so all references
959
     from the dynamic object to this symbol will go through the global
960
     offset table.  The dynamic linker will use the .dynsym entry to
961
     determine the address it must put in the global offset table, so
962
     both the dynamic object and the regular object will refer to the
963
     same memory location for the variable.  */
964
 
965
  /* We must generate a R_RISCV_COPY reloc to tell the dynamic linker
966
     to copy the initial value out of the dynamic object and into the
967
     runtime process image.  We need to remember the offset into the
968
     .rel.bss section we are going to use.  */
969
  if ((h->root.u.def.section->flags & SEC_ALLOC) != 0 && h->size != 0)
970
    {
971
      htab->srelbss->size += sizeof (ElfNN_External_Rela);
972
      h->needs_copy = 1;
973
    }
974
 
975
  if (eh->tls_type & ~GOT_NORMAL)
976
    return _bfd_elf_adjust_dynamic_copy (info, h, htab->sdyntdata);
977
 
978
  return _bfd_elf_adjust_dynamic_copy (info, h, htab->sdynbss);
979
}
980
 
981
/* Allocate space in .plt, .got and associated reloc sections for
982
   dynamic relocs.  */
983
 
984
static bfd_boolean
985
allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
986
{
987
  struct bfd_link_info *info;
988
  struct riscv_elf_link_hash_table *htab;
989
  struct riscv_elf_link_hash_entry *eh;
990
  struct riscv_elf_dyn_relocs *p;
991
 
992
  if (h->root.type == bfd_link_hash_indirect)
993
    return TRUE;
994
 
995
  info = (struct bfd_link_info *) inf;
996
  htab = riscv_elf_hash_table (info);
997
  BFD_ASSERT (htab != NULL);
998
 
999
  if (htab->elf.dynamic_sections_created
1000
      && h->plt.refcount > 0)
1001
    {
1002
      /* Make sure this symbol is output as a dynamic symbol.
1003
         Undefined weak syms won't yet be marked as dynamic.  */
1004
      if (h->dynindx == -1
1005
          && !h->forced_local)
1006
        {
1007
          if (! bfd_elf_link_record_dynamic_symbol (info, h))
1008
            return FALSE;
1009
        }
1010
 
1011
      if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, info->shared, h))
1012
        {
1013
          asection *s = htab->elf.splt;
1014
 
1015
          if (s->size == 0)
1016
            s->size = PLT_HEADER_SIZE;
1017
 
1018
          h->plt.offset = s->size;
1019
 
1020
          /* Make room for this entry.  */
1021
          s->size += PLT_ENTRY_SIZE;
1022
 
1023
          /* We also need to make an entry in the .got.plt section.  */
1024
          htab->elf.sgotplt->size += GOT_ENTRY_SIZE;
1025
 
1026
          /* We also need to make an entry in the .rela.plt section.  */
1027
          htab->elf.srelplt->size += sizeof (ElfNN_External_Rela);
1028
 
1029
          /* If this symbol is not defined in a regular file, and we are
1030
             not generating a shared library, then set the symbol to this
1031
             location in the .plt.  This is required to make function
1032
             pointers compare as equal between the normal executable and
1033
             the shared library.  */
1034
          if (! info->shared
1035
              && !h->def_regular)
1036
            {
1037
              h->root.u.def.section = s;
1038
              h->root.u.def.value = h->plt.offset;
1039
            }
1040
        }
1041
      else
1042
        {
1043
          h->plt.offset = (bfd_vma) -1;
1044
          h->needs_plt = 0;
1045
        }
1046
    }
1047
  else
1048
    {
1049
      h->plt.offset = (bfd_vma) -1;
1050
      h->needs_plt = 0;
1051
    }
1052
 
1053
  if (h->got.refcount > 0)
1054
    {
1055
      asection *s;
1056
      bfd_boolean dyn;
1057
      int tls_type = riscv_elf_hash_entry (h)->tls_type;
1058
 
1059
      /* Make sure this symbol is output as a dynamic symbol.
1060
         Undefined weak syms won't yet be marked as dynamic.  */
1061
      if (h->dynindx == -1
1062
          && !h->forced_local)
1063
        {
1064
          if (! bfd_elf_link_record_dynamic_symbol (info, h))
1065
            return FALSE;
1066
        }
1067
 
1068
      s = htab->elf.sgot;
1069
      h->got.offset = s->size;
1070
      dyn = htab->elf.dynamic_sections_created;
1071
      if (tls_type & (GOT_TLS_GD | GOT_TLS_IE))
1072
        {
1073
          /* TLS_GD needs two dynamic relocs and two GOT slots.  */
1074
          if (tls_type & GOT_TLS_GD)
1075
            {
1076
              s->size += 2 * RISCV_ELF_WORD_BYTES;
1077
              htab->elf.srelgot->size += 2 * sizeof (ElfNN_External_Rela);
1078
            }
1079
 
1080
          /* TLS_IE needs one dynamic reloc and one GOT slot.  */
1081
          if (tls_type & GOT_TLS_IE)
1082
            {
1083
              s->size += RISCV_ELF_WORD_BYTES;
1084
              htab->elf.srelgot->size += sizeof (ElfNN_External_Rela);
1085
            }
1086
        }
1087
      else
1088
        {
1089
          s->size += RISCV_ELF_WORD_BYTES;
1090
          if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h))
1091
            htab->elf.srelgot->size += sizeof (ElfNN_External_Rela);
1092
        }
1093
    }
1094
  else
1095
    h->got.offset = (bfd_vma) -1;
1096
 
1097
  eh = (struct riscv_elf_link_hash_entry *) h;
1098
  if (eh->dyn_relocs == NULL)
1099
    return TRUE;
1100
 
1101
  /* In the shared -Bsymbolic case, discard space allocated for
1102
     dynamic pc-relative relocs against symbols which turn out to be
1103
     defined in regular objects.  For the normal shared case, discard
1104
     space for pc-relative relocs that have become local due to symbol
1105
     visibility changes.  */
1106
 
1107
  if (info->shared)
1108
    {
1109
      if (SYMBOL_CALLS_LOCAL (info, h))
1110
        {
1111
          struct riscv_elf_dyn_relocs **pp;
1112
 
1113
          for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
1114
            {
1115
              p->count -= p->pc_count;
1116
              p->pc_count = 0;
1117
              if (p->count == 0)
1118
                *pp = p->next;
1119
              else
1120
                pp = &p->next;
1121
            }
1122
        }
1123
 
1124
      /* Also discard relocs on undefined weak syms with non-default
1125
         visibility.  */
1126
      if (eh->dyn_relocs != NULL
1127
          && h->root.type == bfd_link_hash_undefweak)
1128
        {
1129
          if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
1130
            eh->dyn_relocs = NULL;
1131
 
1132
          /* Make sure undefined weak symbols are output as a dynamic
1133
             symbol in PIEs.  */
1134
          else if (h->dynindx == -1
1135
                   && !h->forced_local)
1136
            {
1137
              if (! bfd_elf_link_record_dynamic_symbol (info, h))
1138
                return FALSE;
1139
            }
1140
        }
1141
    }
1142
  else
1143
    {
1144
      /* For the non-shared case, discard space for relocs against
1145
         symbols which turn out to need copy relocs or are not
1146
         dynamic.  */
1147
 
1148
      if (!h->non_got_ref
1149
          && ((h->def_dynamic
1150
               && !h->def_regular)
1151
              || (htab->elf.dynamic_sections_created
1152
                  && (h->root.type == bfd_link_hash_undefweak
1153
                      || h->root.type == bfd_link_hash_undefined))))
1154
        {
1155
          /* Make sure this symbol is output as a dynamic symbol.
1156
             Undefined weak syms won't yet be marked as dynamic.  */
1157
          if (h->dynindx == -1
1158
              && !h->forced_local)
1159
            {
1160
              if (! bfd_elf_link_record_dynamic_symbol (info, h))
1161
                return FALSE;
1162
            }
1163
 
1164
          /* If that succeeded, we know we'll be keeping all the
1165
             relocs.  */
1166
          if (h->dynindx != -1)
1167
            goto keep;
1168
        }
1169
 
1170
      eh->dyn_relocs = NULL;
1171
 
1172
    keep: ;
1173
    }
1174
 
1175
  /* Finally, allocate space.  */
1176
  for (p = eh->dyn_relocs; p != NULL; p = p->next)
1177
    {
1178
      asection *sreloc = elf_section_data (p->sec)->sreloc;
1179
      sreloc->size += p->count * sizeof (ElfNN_External_Rela);
1180
    }
1181
 
1182
  return TRUE;
1183
}
1184
 
1185
/* Find any dynamic relocs that apply to read-only sections.  */
1186
 
1187
static bfd_boolean
1188
readonly_dynrelocs (struct elf_link_hash_entry *h, void *inf)
1189
{
1190
  struct riscv_elf_link_hash_entry *eh;
1191
  struct riscv_elf_dyn_relocs *p;
1192
 
1193
  eh = (struct riscv_elf_link_hash_entry *) h;
1194
  for (p = eh->dyn_relocs; p != NULL; p = p->next)
1195
    {
1196
      asection *s = p->sec->output_section;
1197
 
1198
      if (s != NULL && (s->flags & SEC_READONLY) != 0)
1199
        {
1200
          ((struct bfd_link_info *) inf)->flags |= DF_TEXTREL;
1201
          return FALSE;
1202
        }
1203
    }
1204
  return TRUE;
1205
}
1206
 
1207
static bfd_boolean
1208
riscv_elf_size_dynamic_sections (bfd *output_bfd, struct bfd_link_info *info)
1209
{
1210
  struct riscv_elf_link_hash_table *htab;
1211
  bfd *dynobj;
1212
  asection *s;
1213
  bfd *ibfd;
1214
 
1215
  htab = riscv_elf_hash_table (info);
1216
  BFD_ASSERT (htab != NULL);
1217
  dynobj = htab->elf.dynobj;
1218
  BFD_ASSERT (dynobj != NULL);
1219
 
1220
  if (elf_hash_table (info)->dynamic_sections_created)
1221
    {
1222
      /* Set the contents of the .interp section to the interpreter.  */
1223
      if (info->executable)
1224
        {
1225
          s = bfd_get_linker_section (dynobj, ".interp");
1226
          BFD_ASSERT (s != NULL);
1227
          s->size = strlen (ELFNN_DYNAMIC_INTERPRETER) + 1;
1228
          s->contents = (unsigned char *) ELFNN_DYNAMIC_INTERPRETER;
1229
        }
1230
    }
1231
 
1232
  /* Set up .got offsets for local syms, and space for local dynamic
1233
     relocs.  */
1234
  for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
1235
    {
1236
      bfd_signed_vma *local_got;
1237
      bfd_signed_vma *end_local_got;
1238
      char *local_tls_type;
1239
      bfd_size_type locsymcount;
1240
      Elf_Internal_Shdr *symtab_hdr;
1241
      asection *srel;
1242
 
1243
      if (! is_riscv_elf (ibfd))
1244
        continue;
1245
 
1246
      for (s = ibfd->sections; s != NULL; s = s->next)
1247
        {
1248
          struct riscv_elf_dyn_relocs *p;
1249
 
1250
          for (p = elf_section_data (s)->local_dynrel; p != NULL; p = p->next)
1251
            {
1252
              if (!bfd_is_abs_section (p->sec)
1253
                  && bfd_is_abs_section (p->sec->output_section))
1254
                {
1255
                  /* Input section has been discarded, either because
1256
                     it is a copy of a linkonce section or due to
1257
                     linker script /DISCARD/, so we'll be discarding
1258
                     the relocs too.  */
1259
                }
1260
              else if (p->count != 0)
1261
                {
1262
                  srel = elf_section_data (p->sec)->sreloc;
1263
                  srel->size += p->count * sizeof (ElfNN_External_Rela);
1264
                  if ((p->sec->output_section->flags & SEC_READONLY) != 0)
1265
                    info->flags |= DF_TEXTREL;
1266
                }
1267
            }
1268
        }
1269
 
1270
      local_got = elf_local_got_refcounts (ibfd);
1271
      if (!local_got)
1272
        continue;
1273
 
1274
      symtab_hdr = &elf_symtab_hdr (ibfd);
1275
      locsymcount = symtab_hdr->sh_info;
1276
      end_local_got = local_got + locsymcount;
1277
      local_tls_type = _bfd_riscv_elf_local_got_tls_type (ibfd);
1278
      s = htab->elf.sgot;
1279
      srel = htab->elf.srelgot;
1280
      for (; local_got < end_local_got; ++local_got, ++local_tls_type)
1281
        {
1282
          if (*local_got > 0)
1283
            {
1284
              *local_got = s->size;
1285
              s->size += RISCV_ELF_WORD_BYTES;
1286
              if (*local_tls_type & GOT_TLS_GD)
1287
                s->size += RISCV_ELF_WORD_BYTES;
1288
              if (info->shared
1289
                  || (*local_tls_type & (GOT_TLS_GD | GOT_TLS_IE)))
1290
                srel->size += sizeof (ElfNN_External_Rela);
1291
            }
1292
          else
1293
            *local_got = (bfd_vma) -1;
1294
        }
1295
    }
1296
 
1297
  /* Allocate global sym .plt and .got entries, and space for global
1298
     sym dynamic relocs.  */
1299
  elf_link_hash_traverse (&htab->elf, allocate_dynrelocs, info);
1300
 
1301
  if (htab->elf.sgotplt)
1302
    {
1303
      struct elf_link_hash_entry *got;
1304
      got = elf_link_hash_lookup (elf_hash_table (info),
1305
                                  "_GLOBAL_OFFSET_TABLE_",
1306
                                  FALSE, FALSE, FALSE);
1307
 
1308
      /* Don't allocate .got.plt section if there are no GOT nor PLT
1309
         entries and there is no refeence to _GLOBAL_OFFSET_TABLE_.  */
1310
      if ((got == NULL
1311
           || !got->ref_regular_nonweak)
1312
          && (htab->elf.sgotplt->size == GOTPLT_HEADER_SIZE)
1313
          && (htab->elf.splt == NULL
1314
              || htab->elf.splt->size == 0)
1315
          && (htab->elf.sgot == NULL
1316
              || (htab->elf.sgot->size
1317
                  == get_elf_backend_data (output_bfd)->got_header_size)))
1318
        htab->elf.sgotplt->size = 0;
1319
    }
1320
 
1321
  /* The check_relocs and adjust_dynamic_symbol entry points have
1322
     determined the sizes of the various dynamic sections.  Allocate
1323
     memory for them.  */
1324
  for (s = dynobj->sections; s != NULL; s = s->next)
1325
    {
1326
      if ((s->flags & SEC_LINKER_CREATED) == 0)
1327
        continue;
1328
 
1329
      if (s == htab->elf.splt
1330
          || s == htab->elf.sgot
1331
          || s == htab->elf.sgotplt
1332
          || s == htab->sdynbss)
1333
        {
1334
          /* Strip this section if we don't need it; see the
1335
             comment below.  */
1336
        }
1337
      else if (strncmp (s->name, ".rela", 5) == 0)
1338
        {
1339
          if (s->size != 0)
1340
            {
1341
              /* We use the reloc_count field as a counter if we need
1342
                 to copy relocs into the output file.  */
1343
              s->reloc_count = 0;
1344
            }
1345
        }
1346
      else
1347
        {
1348
          /* It's not one of our sections.  */
1349
          continue;
1350
        }
1351
 
1352
      if (s->size == 0)
1353
        {
1354
          /* If we don't need this section, strip it from the
1355
             output file.  This is mostly to handle .rela.bss and
1356
             .rela.plt.  We must create both sections in
1357
             create_dynamic_sections, because they must be created
1358
             before the linker maps input sections to output
1359
             sections.  The linker does that before
1360
             adjust_dynamic_symbol is called, and it is that
1361
             function which decides whether anything needs to go
1362
             into these sections.  */
1363
          s->flags |= SEC_EXCLUDE;
1364
          continue;
1365
        }
1366
 
1367
      if ((s->flags & SEC_HAS_CONTENTS) == 0)
1368
        continue;
1369
 
1370
      /* Allocate memory for the section contents.  Zero the memory
1371
         for the benefit of .rela.plt, which has 4 unused entries
1372
         at the beginning, and we don't want garbage.  */
1373
      s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->size);
1374
      if (s->contents == NULL)
1375
        return FALSE;
1376
    }
1377
 
1378
  if (elf_hash_table (info)->dynamic_sections_created)
1379
    {
1380
      /* Add some entries to the .dynamic section.  We fill in the
1381
         values later, in riscv_elf_finish_dynamic_sections, but we
1382
         must add the entries now so that we get the correct size for
1383
         the .dynamic section.  The DT_DEBUG entry is filled in by the
1384
         dynamic linker and used by the debugger.  */
1385
#define add_dynamic_entry(TAG, VAL) \
1386
  _bfd_elf_add_dynamic_entry (info, TAG, VAL)
1387
 
1388
      if (info->executable)
1389
        {
1390
          if (!add_dynamic_entry (DT_DEBUG, 0))
1391
            return FALSE;
1392
        }
1393
 
1394
      if (htab->elf.srelplt->size != 0)
1395
        {
1396
          if (!add_dynamic_entry (DT_PLTGOT, 0)
1397
              || !add_dynamic_entry (DT_PLTRELSZ, 0)
1398
              || !add_dynamic_entry (DT_PLTREL, DT_RELA)
1399
              || !add_dynamic_entry (DT_JMPREL, 0))
1400
            return FALSE;
1401
        }
1402
 
1403
      if (!add_dynamic_entry (DT_RELA, 0)
1404
          || !add_dynamic_entry (DT_RELASZ, 0)
1405
          || !add_dynamic_entry (DT_RELAENT, sizeof (ElfNN_External_Rela)))
1406
        return FALSE;
1407
 
1408
      /* If any dynamic relocs apply to a read-only section,
1409
         then we need a DT_TEXTREL entry.  */
1410
      if ((info->flags & DF_TEXTREL) == 0)
1411
        elf_link_hash_traverse (&htab->elf, readonly_dynrelocs, info);
1412
 
1413
      if (info->flags & DF_TEXTREL)
1414
        {
1415
          if (!add_dynamic_entry (DT_TEXTREL, 0))
1416
            return FALSE;
1417
        }
1418
    }
1419
#undef add_dynamic_entry
1420
 
1421
  return TRUE;
1422
}
1423
 
1424
#define TP_OFFSET 0
1425
#define DTP_OFFSET 0x800
1426
 
1427
/* Return the relocation value for a TLS dtp-relative reloc.  */
1428
 
1429
static bfd_vma
1430
dtpoff (struct bfd_link_info *info, bfd_vma address)
1431
{
1432
  /* If tls_sec is NULL, we should have signalled an error already.  */
1433
  if (elf_hash_table (info)->tls_sec == NULL)
1434
    return 0;
1435
  return address - elf_hash_table (info)->tls_sec->vma - DTP_OFFSET;
1436
}
1437
 
1438
/* Return the relocation value for a static TLS tp-relative relocation.  */
1439
 
1440
static bfd_vma
1441
tpoff (struct bfd_link_info *info, bfd_vma address)
1442
{
1443
  /* If tls_sec is NULL, we should have signalled an error already.  */
1444
  if (elf_hash_table (info)->tls_sec == NULL)
1445
    return 0;
1446
  return address - elf_hash_table (info)->tls_sec->vma - TP_OFFSET;
1447
}
1448
 
1449
/* Return the global pointer's value, or 0 if it is not in use.  */
1450
 
1451
static bfd_vma
1452
riscv_global_pointer_value (struct bfd_link_info *info)
1453
{
1454
  struct bfd_link_hash_entry *h;
1455
 
1456
  h = bfd_link_hash_lookup (info->hash, "_gp", FALSE, FALSE, TRUE);
1457
  if (h == NULL || h->type != bfd_link_hash_defined)
1458
    return 0;
1459
 
1460
  return h->u.def.value + sec_addr (h->u.def.section);
1461
}
1462
 
1463
/* Emplace a static relocation.  */
1464
 
1465
static bfd_reloc_status_type
1466
perform_relocation (const reloc_howto_type *howto,
1467
                    const Elf_Internal_Rela *rel,
1468
                    bfd_vma value,
1469
                    asection *input_section,
1470
                    bfd *input_bfd,
1471
                    bfd_byte *contents)
1472
{
1473
  if (howto->pc_relative)
1474
    value -= sec_addr (input_section) + rel->r_offset;
1475
  value += rel->r_addend;
1476
 
1477
  switch (ELFNN_R_TYPE (rel->r_info))
1478
    {
1479
    case R_RISCV_HI20:
1480
    case R_RISCV_TPREL_HI20:
1481
    case R_RISCV_PCREL_HI20:
1482
    case R_RISCV_GOT_HI20:
1483
    case R_RISCV_TLS_GOT_HI20:
1484
    case R_RISCV_TLS_GD_HI20:
1485
      if (ARCH_SIZE > 32 && !VALID_UTYPE_IMM (RISCV_CONST_HIGH_PART (value)))
1486
        return bfd_reloc_overflow;
1487
      value = ENCODE_UTYPE_IMM (RISCV_CONST_HIGH_PART (value));
1488
      break;
1489
 
1490
    case R_RISCV_LO12_I:
1491
    case R_RISCV_GPREL_I:
1492
    case R_RISCV_TPREL_LO12_I:
1493
    case R_RISCV_PCREL_LO12_I:
1494
      value = ENCODE_ITYPE_IMM (value);
1495
      break;
1496
 
1497
    case R_RISCV_LO12_S:
1498
    case R_RISCV_GPREL_S:
1499
    case R_RISCV_TPREL_LO12_S:
1500
    case R_RISCV_PCREL_LO12_S:
1501
      value = ENCODE_STYPE_IMM (value);
1502
      break;
1503
 
1504
    case R_RISCV_CALL:
1505
    case R_RISCV_CALL_PLT:
1506
      if (ARCH_SIZE > 32 && !VALID_UTYPE_IMM (RISCV_CONST_HIGH_PART (value)))
1507
        return bfd_reloc_overflow;
1508
      value = ENCODE_UTYPE_IMM (RISCV_CONST_HIGH_PART (value))
1509
              | (ENCODE_ITYPE_IMM (value) << 32);
1510
      break;
1511
 
1512
    case R_RISCV_JAL:
1513
      if (!VALID_UJTYPE_IMM (value))
1514
        return bfd_reloc_overflow;
1515
      value = ENCODE_UJTYPE_IMM (value);
1516
      break;
1517
 
1518
    case R_RISCV_BRANCH:
1519
      if (!VALID_SBTYPE_IMM (value))
1520
        return bfd_reloc_overflow;
1521
      value = ENCODE_SBTYPE_IMM (value);
1522
      break;
1523
 
1524
    case R_RISCV_RVC_BRANCH:
1525
      if (!VALID_RVC_B_IMM (value))
1526
        return bfd_reloc_overflow;
1527
      value = ENCODE_RVC_B_IMM (value);
1528
      break;
1529
 
1530
    case R_RISCV_RVC_JUMP:
1531
      if (!VALID_RVC_J_IMM (value))
1532
        return bfd_reloc_overflow;
1533
      value = ENCODE_RVC_J_IMM (value);
1534
      break;
1535
 
1536
    case R_RISCV_RVC_LUI:
1537
      if (!VALID_RVC_LUI_IMM (RISCV_CONST_HIGH_PART (value)))
1538
        return bfd_reloc_overflow;
1539
      value = ENCODE_RVC_LUI_IMM (RISCV_CONST_HIGH_PART (value));
1540
      break;
1541
 
1542
    case R_RISCV_32:
1543
    case R_RISCV_64:
1544
    case R_RISCV_ADD8:
1545
    case R_RISCV_ADD16:
1546
    case R_RISCV_ADD32:
1547
    case R_RISCV_ADD64:
1548
    case R_RISCV_SUB8:
1549
    case R_RISCV_SUB16:
1550
    case R_RISCV_SUB32:
1551
    case R_RISCV_SUB64:
1552
    case R_RISCV_TLS_DTPREL32:
1553
    case R_RISCV_TLS_DTPREL64:
1554
      break;
1555
 
1556
    default:
1557
      return bfd_reloc_notsupported;
1558
    }
1559
 
1560
  bfd_vma word = bfd_get (howto->bitsize, input_bfd, contents + rel->r_offset);
1561
  word = (word & ~howto->dst_mask) | (value & howto->dst_mask);
1562
  bfd_put (howto->bitsize, input_bfd, word, contents + rel->r_offset);
1563
 
1564
  return bfd_reloc_ok;
1565
}
1566
 
1567
/* Remember all PC-relative high-part relocs we've encountered to help us
1568
   later resolve the corresponding low-part relocs.  */
1569
 
1570
typedef struct {
1571
  bfd_vma address;
1572
  bfd_vma value;
1573
} riscv_pcrel_hi_reloc;
1574
 
1575
typedef struct riscv_pcrel_lo_reloc {
1576
  asection *input_section;
1577
  struct bfd_link_info *info;
1578
  reloc_howto_type *howto;
1579
  const Elf_Internal_Rela *reloc;
1580
  bfd_vma addr;
1581
  const char *name;
1582
  bfd_byte *contents;
1583
  struct riscv_pcrel_lo_reloc *next;
1584
} riscv_pcrel_lo_reloc;
1585
 
1586
typedef struct {
1587
  htab_t hi_relocs;
1588
  riscv_pcrel_lo_reloc *lo_relocs;
1589
} riscv_pcrel_relocs;
1590
 
1591
static hashval_t
1592
riscv_pcrel_reloc_hash (const void *entry)
1593
{
1594
  const riscv_pcrel_hi_reloc *e = entry;
1595
  return (hashval_t)(e->address >> 2);
1596
}
1597
 
1598
static bfd_boolean
1599
riscv_pcrel_reloc_eq (const void *entry1, const void *entry2)
1600
{
1601
  const riscv_pcrel_hi_reloc *e1 = entry1, *e2 = entry2;
1602
  return e1->address == e2->address;
1603
}
1604
 
1605
static bfd_boolean
1606
riscv_init_pcrel_relocs (riscv_pcrel_relocs *p)
1607
{
1608
 
1609
  p->lo_relocs = NULL;
1610
  p->hi_relocs = htab_create (1024, riscv_pcrel_reloc_hash,
1611
                              riscv_pcrel_reloc_eq, free);
1612
  return p->hi_relocs != NULL;
1613
}
1614
 
1615
static void
1616
riscv_free_pcrel_relocs (riscv_pcrel_relocs *p)
1617
{
1618
  riscv_pcrel_lo_reloc *cur = p->lo_relocs;
1619
  while (cur != NULL)
1620
    {
1621
      riscv_pcrel_lo_reloc *next = cur->next;
1622
      free (cur);
1623
      cur = next;
1624
    }
1625
 
1626
  htab_delete (p->hi_relocs);
1627
}
1628
 
1629
static bfd_boolean
1630
riscv_record_pcrel_hi_reloc (riscv_pcrel_relocs *p, bfd_vma addr, bfd_vma value)
1631
{
1632
  riscv_pcrel_hi_reloc entry = {addr, value - addr};
1633
  riscv_pcrel_hi_reloc **slot =
1634
    (riscv_pcrel_hi_reloc **) htab_find_slot (p->hi_relocs, &entry, INSERT);
1635
  BFD_ASSERT (*slot == NULL);
1636
  *slot = (riscv_pcrel_hi_reloc *) bfd_malloc (sizeof (riscv_pcrel_hi_reloc));
1637
  if (*slot == NULL)
1638
    return FALSE;
1639
  **slot = entry;
1640
  return TRUE;
1641
}
1642
 
1643
static bfd_boolean
1644
riscv_record_pcrel_lo_reloc (riscv_pcrel_relocs *p,
1645
                             asection *input_section,
1646
                             struct bfd_link_info *info,
1647
                             reloc_howto_type *howto,
1648
                             const Elf_Internal_Rela *reloc,
1649
                             bfd_vma addr,
1650
                             const char *name,
1651
                             bfd_byte *contents)
1652
{
1653
  riscv_pcrel_lo_reloc *entry;
1654
  entry = (riscv_pcrel_lo_reloc *) bfd_malloc (sizeof (riscv_pcrel_lo_reloc));
1655
  if (entry == NULL)
1656
    return FALSE;
1657
  *entry = (riscv_pcrel_lo_reloc) {input_section, info, howto, reloc, addr,
1658
                                   name, contents, p->lo_relocs};
1659
  p->lo_relocs = entry;
1660
  return TRUE;
1661
}
1662
 
1663
static bfd_boolean
1664
riscv_resolve_pcrel_lo_relocs (riscv_pcrel_relocs *p)
1665
{
1666
  riscv_pcrel_lo_reloc *r;
1667
  for (r = p->lo_relocs; r != NULL; r = r->next)
1668
    {
1669
      bfd *input_bfd = r->input_section->owner;
1670
      riscv_pcrel_hi_reloc search = {r->addr, 0};
1671
      riscv_pcrel_hi_reloc *entry = htab_find (p->hi_relocs, &search);
1672
      if (entry == NULL)
1673
        return ((*r->info->callbacks->reloc_overflow)
1674
                 (r->info, NULL, r->name, r->howto->name, (bfd_vma) 0,
1675
                  input_bfd, r->input_section, r->reloc->r_offset));
1676
 
1677
      perform_relocation (r->howto, r->reloc, entry->value, r->input_section,
1678
                          input_bfd, r->contents);
1679
    }
1680
 
1681
  return TRUE;
1682
}
1683
 
1684
/* Relocate a RISC-V ELF section.
1685
 
1686
   The RELOCATE_SECTION function is called by the new ELF backend linker
1687
   to handle the relocations for a section.
1688
 
1689
   The relocs are always passed as Rela structures.
1690
 
1691
   This function is responsible for adjusting the section contents as
1692
   necessary, and (if generating a relocatable output file) adjusting
1693
   the reloc addend as necessary.
1694
 
1695
   This function does not have to worry about setting the reloc
1696
   address or the reloc symbol index.
1697
 
1698
   LOCAL_SYMS is a pointer to the swapped in local symbols.
1699
 
1700
   LOCAL_SECTIONS is an array giving the section in the input file
1701
   corresponding to the st_shndx field of each local symbol.
1702
 
1703
   The global hash table entry for the global symbols can be found
1704
   via elf_sym_hashes (input_bfd).
1705
 
1706
   When generating relocatable output, this function must handle
1707
   STB_LOCAL/STT_SECTION symbols specially.  The output symbol is
1708
   going to be the section symbol corresponding to the output
1709
   section, which means that the addend must be adjusted
1710
   accordingly.  */
1711
 
1712
static bfd_boolean
1713
riscv_elf_relocate_section (bfd *output_bfd, struct bfd_link_info *info,
1714
                            bfd *input_bfd, asection *input_section,
1715
                            bfd_byte *contents, Elf_Internal_Rela *relocs,
1716
                            Elf_Internal_Sym *local_syms,
1717
                            asection **local_sections)
1718
{
1719
  Elf_Internal_Rela *rel;
1720
  Elf_Internal_Rela *relend;
1721
  riscv_pcrel_relocs pcrel_relocs;
1722
  bfd_boolean ret = FALSE;
1723
  asection *sreloc = elf_section_data (input_section)->sreloc;
1724
  struct riscv_elf_link_hash_table *htab = riscv_elf_hash_table (info);
1725
  Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (input_bfd);
1726
  struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (input_bfd);
1727
  bfd_vma *local_got_offsets = elf_local_got_offsets (input_bfd);
1728
 
1729
  if (!riscv_init_pcrel_relocs (&pcrel_relocs))
1730
    return FALSE;
1731
 
1732
  relend = relocs + input_section->reloc_count;
1733
  for (rel = relocs; rel < relend; rel++)
1734
    {
1735
      unsigned long r_symndx;
1736
      struct elf_link_hash_entry *h;
1737
      Elf_Internal_Sym *sym;
1738
      asection *sec;
1739
      bfd_vma relocation;
1740
      bfd_reloc_status_type r = bfd_reloc_ok;
1741
      const char *name;
1742
      bfd_vma off, ie_off;
1743
      bfd_boolean unresolved_reloc, is_ie = FALSE;
1744
      bfd_vma pc = sec_addr (input_section) + rel->r_offset;
1745
      int r_type = ELFNN_R_TYPE (rel->r_info), tls_type;
1746
      reloc_howto_type *howto = riscv_elf_rtype_to_howto (r_type);
1747
      const char *msg = NULL;
1748
 
1749
      if (r_type == R_RISCV_GNU_VTINHERIT || r_type == R_RISCV_GNU_VTENTRY)
1750
        continue;
1751
 
1752
      /* This is a final link.  */
1753
      r_symndx = ELFNN_R_SYM (rel->r_info);
1754
      h = NULL;
1755
      sym = NULL;
1756
      sec = NULL;
1757
      unresolved_reloc = FALSE;
1758
      if (r_symndx < symtab_hdr->sh_info)
1759
        {
1760
          sym = local_syms + r_symndx;
1761
          sec = local_sections[r_symndx];
1762
          relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
1763
        }
1764
      else
1765
        {
1766
          bfd_boolean warned, ignored;
1767
 
1768
          RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
1769
                                   r_symndx, symtab_hdr, sym_hashes,
1770
                                   h, sec, relocation,
1771
                                   unresolved_reloc, warned, ignored);
1772
          if (warned)
1773
            {
1774
              /* To avoid generating warning messages about truncated
1775
                 relocations, set the relocation's address to be the same as
1776
                 the start of this section.  */
1777
              if (input_section->output_section != NULL)
1778
                relocation = input_section->output_section->vma;
1779
              else
1780
                relocation = 0;
1781
            }
1782
        }
1783
 
1784
      if (sec != NULL && discarded_section (sec))
1785
        RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
1786
                                         rel, 1, relend, howto, 0, contents);
1787
 
1788
      if (info->relocatable)
1789
        continue;
1790
 
1791
      if (h != NULL)
1792
        name = h->root.root.string;
1793
      else
1794
        {
1795
          name = (bfd_elf_string_from_elf_section
1796
                  (input_bfd, symtab_hdr->sh_link, sym->st_name));
1797
          if (name == NULL || *name == '\0')
1798
            name = bfd_section_name (input_bfd, sec);
1799
        }
1800
 
1801
      switch (r_type)
1802
        {
1803
        case R_RISCV_NONE:
1804
        case R_RISCV_TPREL_ADD:
1805
        case R_RISCV_COPY:
1806
        case R_RISCV_JUMP_SLOT:
1807
        case R_RISCV_RELATIVE:
1808
          /* These require nothing of us at all.  */
1809
          continue;
1810
 
1811
        case R_RISCV_HI20:
1812
        case R_RISCV_BRANCH:
1813
        case R_RISCV_RVC_BRANCH:
1814
        case R_RISCV_RVC_LUI:
1815
        case R_RISCV_LO12_I:
1816
        case R_RISCV_LO12_S:
1817
          /* These require no special handling beyond perform_relocation.  */
1818
          break;
1819
 
1820
        case R_RISCV_GOT_HI20:
1821
          if (h != NULL)
1822
            {
1823
              bfd_boolean dyn;
1824
 
1825
              off = h->got.offset;
1826
              BFD_ASSERT (off != (bfd_vma) -1);
1827
              dyn = elf_hash_table (info)->dynamic_sections_created;
1828
 
1829
              if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h)
1830
                  || (info->shared
1831
                      && SYMBOL_REFERENCES_LOCAL (info, h)))
1832
                {
1833
                  /* This is actually a static link, or it is a
1834
                     -Bsymbolic link and the symbol is defined
1835
                     locally, or the symbol was forced to be local
1836
                     because of a version file.  We must initialize
1837
                     this entry in the global offset table.  Since the
1838
                     offset must always be a multiple of the word size,
1839
                     we use the least significant bit to record whether
1840
                     we have initialized it already.
1841
 
1842
                     When doing a dynamic link, we create a .rela.got
1843
                     relocation entry to initialize the value.  This
1844
                     is done in the finish_dynamic_symbol routine.  */
1845
                  if ((off & 1) != 0)
1846
                    off &= ~1;
1847
                  else
1848
                    {
1849
                      bfd_put_NN (output_bfd, relocation,
1850
                                  htab->elf.sgot->contents + off);
1851
                      h->got.offset |= 1;
1852
                    }
1853
                }
1854
              else
1855
                unresolved_reloc = FALSE;
1856
            }
1857
          else
1858
            {
1859
              BFD_ASSERT (local_got_offsets != NULL
1860
                          && local_got_offsets[r_symndx] != (bfd_vma) -1);
1861
 
1862
              off = local_got_offsets[r_symndx];
1863
 
1864
              /* The offset must always be a multiple of the word size.
1865
                 So, we can use the least significant bit to record
1866
                 whether we have already processed this entry.  */
1867
              if ((off & 1) != 0)
1868
                off &= ~1;
1869
              else
1870
                {
1871
                  if (info->shared)
1872
                    {
1873
                      asection *s;
1874
                      Elf_Internal_Rela outrel;
1875
 
1876
                      /* We need to generate a R_RISCV_RELATIVE reloc
1877
                         for the dynamic linker.  */
1878
                      s = htab->elf.srelgot;
1879
                      BFD_ASSERT (s != NULL);
1880
 
1881
                      outrel.r_offset = sec_addr (htab->elf.sgot) + off;
1882
                      outrel.r_info =
1883
                        ELFNN_R_INFO (0, R_RISCV_RELATIVE);
1884
                      outrel.r_addend = relocation;
1885
                      relocation = 0;
1886
                      riscv_elf_append_rela (output_bfd, s, &outrel);
1887
                    }
1888
 
1889
                  bfd_put_NN (output_bfd, relocation,
1890
                              htab->elf.sgot->contents + off);
1891
                  local_got_offsets[r_symndx] |= 1;
1892
                }
1893
            }
1894
          relocation = sec_addr (htab->elf.sgot) + off;
1895
          if (!riscv_record_pcrel_hi_reloc (&pcrel_relocs, pc, relocation))
1896
            r = bfd_reloc_overflow;
1897
          break;
1898
 
1899
        case R_RISCV_ADD8:
1900
        case R_RISCV_ADD16:
1901
        case R_RISCV_ADD32:
1902
        case R_RISCV_ADD64:
1903
          {
1904
            bfd_vma old_value = bfd_get (howto->bitsize, input_bfd,
1905
                                         contents + rel->r_offset);
1906
            relocation = old_value + relocation;
1907
          }
1908
          break;
1909
 
1910
        case R_RISCV_SUB8:
1911
        case R_RISCV_SUB16:
1912
        case R_RISCV_SUB32:
1913
        case R_RISCV_SUB64:
1914
          {
1915
            bfd_vma old_value = bfd_get (howto->bitsize, input_bfd,
1916
                                         contents + rel->r_offset);
1917
            relocation = old_value - relocation;
1918
          }
1919
          break;
1920
 
1921
        case R_RISCV_CALL_PLT:
1922
        case R_RISCV_CALL:
1923
        case R_RISCV_JAL:
1924
        case R_RISCV_RVC_JUMP:
1925
          if (info->shared && h != NULL && h->plt.offset != MINUS_ONE)
1926
            {
1927
              /* Refer to the PLT entry.  */
1928
              relocation = sec_addr (htab->elf.splt) + h->plt.offset;
1929
              unresolved_reloc = FALSE;
1930
            }
1931
          break;
1932
 
1933
        case R_RISCV_TPREL_HI20:
1934
          relocation = tpoff (info, relocation);
1935
          break;
1936
 
1937
        case R_RISCV_TPREL_LO12_I:
1938
        case R_RISCV_TPREL_LO12_S:
1939
          relocation = tpoff (info, relocation);
1940
          if (VALID_ITYPE_IMM (relocation + rel->r_addend))
1941
            {
1942
              /* We can use tp as the base register.  */
1943
              bfd_vma insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
1944
              insn &= ~(OP_MASK_RS1 << OP_SH_RS1);
1945
              insn |= X_TP << OP_SH_RS1;
1946
              bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
1947
            }
1948
          break;
1949
 
1950
        case R_RISCV_GPREL_I:
1951
        case R_RISCV_GPREL_S:
1952
          {
1953
            bfd_vma gp = riscv_global_pointer_value (info);
1954
            bfd_boolean x0_base = VALID_ITYPE_IMM (relocation + rel->r_addend);
1955
            if (x0_base || VALID_ITYPE_IMM (relocation + rel->r_addend - gp))
1956
              {
1957
                /* We can use x0 or gp as the base register.  */
1958
                bfd_vma insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
1959
                insn &= ~(OP_MASK_RS1 << OP_SH_RS1);
1960
                if (!x0_base)
1961
                  {
1962
                    rel->r_addend -= gp;
1963
                    insn |= X_GP << OP_SH_RS1;
1964
                  }
1965
                bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
1966
              }
1967
            else
1968
              r = bfd_reloc_overflow;
1969
            break;
1970
          }
1971
 
1972
        case R_RISCV_PCREL_HI20:
1973
          if (!riscv_record_pcrel_hi_reloc (&pcrel_relocs, pc,
1974
                                            relocation + rel->r_addend))
1975
            r = bfd_reloc_overflow;
1976
          break;
1977
 
1978
        case R_RISCV_PCREL_LO12_I:
1979
        case R_RISCV_PCREL_LO12_S:
1980
          if (riscv_record_pcrel_lo_reloc (&pcrel_relocs, input_section, info,
1981
                                           howto, rel, relocation, name,
1982
                                           contents))
1983
            continue;
1984
          r = bfd_reloc_overflow;
1985
          break;
1986
 
1987
        case R_RISCV_TLS_DTPREL32:
1988
        case R_RISCV_TLS_DTPREL64:
1989
          relocation = dtpoff (info, relocation);
1990
          break;
1991
 
1992
        case R_RISCV_32:
1993
        case R_RISCV_64:
1994
          if ((input_section->flags & SEC_ALLOC) == 0)
1995
            break;
1996
 
1997
          if ((info->shared
1998
               && (h == NULL
1999
                   || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
2000
                   || h->root.type != bfd_link_hash_undefweak)
2001
               && (! howto->pc_relative
2002
                   || !SYMBOL_CALLS_LOCAL (info, h)))
2003
              || (!info->shared
2004
                  && h != NULL
2005
                  && h->dynindx != -1
2006
                  && !h->non_got_ref
2007
                  && ((h->def_dynamic
2008
                       && !h->def_regular)
2009
                      || h->root.type == bfd_link_hash_undefweak
2010
                      || h->root.type == bfd_link_hash_undefined)))
2011
            {
2012
              Elf_Internal_Rela outrel;
2013
              bfd_boolean skip_static_relocation, skip_dynamic_relocation;
2014
 
2015
              /* When generating a shared object, these relocations
2016
                 are copied into the output file to be resolved at run
2017
                 time.  */
2018
 
2019
              outrel.r_offset =
2020
                _bfd_elf_section_offset (output_bfd, info, input_section,
2021
                                         rel->r_offset);
2022
              skip_static_relocation = outrel.r_offset != (bfd_vma) -2;
2023
              skip_dynamic_relocation = outrel.r_offset >= (bfd_vma) -2;
2024
              outrel.r_offset += sec_addr (input_section);
2025
 
2026
              if (skip_dynamic_relocation)
2027
                memset (&outrel, 0, sizeof outrel);
2028
              else if (h != NULL && h->dynindx != -1
2029
                       && !(info->shared
2030
                            && SYMBOLIC_BIND (info, h)
2031
                            && h->def_regular))
2032
                {
2033
                  outrel.r_info = ELFNN_R_INFO (h->dynindx, r_type);
2034
                  outrel.r_addend = rel->r_addend;
2035
                }
2036
              else
2037
                {
2038
                  outrel.r_info = ELFNN_R_INFO (0, R_RISCV_RELATIVE);
2039
                  outrel.r_addend = relocation + rel->r_addend;
2040
                }
2041
 
2042
              riscv_elf_append_rela (output_bfd, sreloc, &outrel);
2043
              if (skip_static_relocation)
2044
                continue;
2045
            }
2046
          break;
2047
 
2048
        case R_RISCV_TLS_GOT_HI20:
2049
          is_ie = TRUE;
2050
          /* Fall through.  */
2051
 
2052
        case R_RISCV_TLS_GD_HI20:
2053
          if (h != NULL)
2054
            {
2055
              off = h->got.offset;
2056
              h->got.offset |= 1;
2057
            }
2058
          else
2059
            {
2060
              off = local_got_offsets[r_symndx];
2061
              local_got_offsets[r_symndx] |= 1;
2062
            }
2063
 
2064
          tls_type = _bfd_riscv_elf_tls_type (input_bfd, h, r_symndx);
2065
          BFD_ASSERT (tls_type & (GOT_TLS_IE | GOT_TLS_GD));
2066
          /* If this symbol is referenced by both GD and IE TLS, the IE
2067
             reference's GOT slot follows the GD reference's slots.  */
2068
          ie_off = 0;
2069
          if ((tls_type & GOT_TLS_GD) && (tls_type & GOT_TLS_IE))
2070
            ie_off = 2 * GOT_ENTRY_SIZE;
2071
 
2072
          if ((off & 1) != 0)
2073
            off &= ~1;
2074
          else
2075
            {
2076
              Elf_Internal_Rela outrel;
2077
              int indx = 0;
2078
              bfd_boolean need_relocs = FALSE;
2079
 
2080
              if (htab->elf.srelgot == NULL)
2081
                abort ();
2082
 
2083
              if (h != NULL)
2084
                {
2085
                  bfd_boolean dyn;
2086
                  dyn = htab->elf.dynamic_sections_created;
2087
 
2088
                  if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h)
2089
                      && (!info->shared
2090
                          || !SYMBOL_REFERENCES_LOCAL (info, h)))
2091
                  {
2092
                    indx = h->dynindx;
2093
                  }
2094
                }
2095
 
2096
              /* The GOT entries have not been initialized yet.  Do it
2097
                 now, and emit any relocations.  */
2098
              if ((info->shared || indx != 0)
2099
                  && (h == NULL
2100
                      || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
2101
                      || h->root.type != bfd_link_hash_undefweak))
2102
                    need_relocs = TRUE;
2103
 
2104
              if (tls_type & GOT_TLS_GD)
2105
                {
2106
                  if (need_relocs)
2107
                    {
2108
                      outrel.r_offset = sec_addr (htab->elf.sgot) + off;
2109
                      outrel.r_addend = 0;
2110
                      outrel.r_info = ELFNN_R_INFO (indx, R_RISCV_TLS_DTPMODNN);
2111
                      bfd_put_NN (output_bfd, 0,
2112
                                  htab->elf.sgot->contents + off);
2113
                      riscv_elf_append_rela (output_bfd, htab->elf.srelgot, &outrel);
2114
                      if (indx == 0)
2115
                        {
2116
                          BFD_ASSERT (! unresolved_reloc);
2117
                          bfd_put_NN (output_bfd,
2118
                                      dtpoff (info, relocation),
2119
                                      (htab->elf.sgot->contents + off +
2120
                                       RISCV_ELF_WORD_BYTES));
2121
                        }
2122
                      else
2123
                        {
2124
                          bfd_put_NN (output_bfd, 0,
2125
                                      (htab->elf.sgot->contents + off +
2126
                                       RISCV_ELF_WORD_BYTES));
2127
                          outrel.r_info = ELFNN_R_INFO (indx, R_RISCV_TLS_DTPRELNN);
2128
                          outrel.r_offset += RISCV_ELF_WORD_BYTES;
2129
                          riscv_elf_append_rela (output_bfd, htab->elf.srelgot, &outrel);
2130
                        }
2131
                    }
2132
                  else
2133
                    {
2134
                      /* If we are not emitting relocations for a
2135
                         general dynamic reference, then we must be in a
2136
                         static link or an executable link with the
2137
                         symbol binding locally.  Mark it as belonging
2138
                         to module 1, the executable.  */
2139
                      bfd_put_NN (output_bfd, 1,
2140
                                  htab->elf.sgot->contents + off);
2141
                      bfd_put_NN (output_bfd,
2142
                                  dtpoff (info, relocation),
2143
                                  (htab->elf.sgot->contents + off +
2144
                                   RISCV_ELF_WORD_BYTES));
2145
                   }
2146
                }
2147
 
2148
              if (tls_type & GOT_TLS_IE)
2149
                {
2150
                  if (need_relocs)
2151
                    {
2152
                      bfd_put_NN (output_bfd, 0,
2153
                                  htab->elf.sgot->contents + off + ie_off);
2154
                      outrel.r_offset = sec_addr (htab->elf.sgot)
2155
                                       + off + ie_off;
2156
                      outrel.r_addend = 0;
2157
                      if (indx == 0)
2158
                        outrel.r_addend = tpoff (info, relocation);
2159
                      outrel.r_info = ELFNN_R_INFO (indx, R_RISCV_TLS_TPRELNN);
2160
                      riscv_elf_append_rela (output_bfd, htab->elf.srelgot, &outrel);
2161
                    }
2162
                  else
2163
                    {
2164
                      bfd_put_NN (output_bfd, tpoff (info, relocation),
2165
                                  htab->elf.sgot->contents + off + ie_off);
2166
                    }
2167
                }
2168
            }
2169
 
2170
          BFD_ASSERT (off < (bfd_vma) -2);
2171
          relocation = sec_addr (htab->elf.sgot) + off + (is_ie ? ie_off : 0);
2172
          if (!riscv_record_pcrel_hi_reloc (&pcrel_relocs, pc, relocation))
2173
            r = bfd_reloc_overflow;
2174
          unresolved_reloc = FALSE;
2175
          break;
2176
 
2177
        default:
2178
          r = bfd_reloc_notsupported;
2179
        }
2180
 
2181
      /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
2182
         because such sections are not SEC_ALLOC and thus ld.so will
2183
         not process them.  */
2184
      if (unresolved_reloc
2185
          && !((input_section->flags & SEC_DEBUGGING) != 0
2186
               && h->def_dynamic)
2187
          && _bfd_elf_section_offset (output_bfd, info, input_section,
2188
                                      rel->r_offset) != (bfd_vma) -1)
2189
        {
2190
          (*_bfd_error_handler)
2191
            (_("%B(%A+0x%lx): unresolvable %s relocation against symbol `%s'"),
2192
             input_bfd,
2193
             input_section,
2194
             (long) rel->r_offset,
2195
             howto->name,
2196
             h->root.root.string);
2197
          continue;
2198
        }
2199
 
2200
      if (r == bfd_reloc_ok)
2201
        r = perform_relocation (howto, rel, relocation, input_section,
2202
                                input_bfd, contents);
2203
 
2204
      switch (r)
2205
        {
2206
        case bfd_reloc_ok:
2207
          continue;
2208
 
2209
        case bfd_reloc_overflow:
2210
          r = info->callbacks->reloc_overflow
2211
            (info, (h ? &h->root : NULL), name, howto->name,
2212
             (bfd_vma) 0, input_bfd, input_section, rel->r_offset);
2213
          break;
2214
 
2215
        case bfd_reloc_undefined:
2216
          r = info->callbacks->undefined_symbol
2217
            (info, name, input_bfd, input_section, rel->r_offset,
2218
             TRUE);
2219
          break;
2220
 
2221
        case bfd_reloc_outofrange:
2222
          msg = _("internal error: out of range error");
2223
          break;
2224
 
2225
        case bfd_reloc_notsupported:
2226
          msg = _("internal error: unsupported relocation error");
2227
          break;
2228
 
2229
        case bfd_reloc_dangerous:
2230
          msg = _("internal error: dangerous relocation");
2231
          break;
2232
 
2233
        default:
2234
          msg = _("internal error: unknown error");
2235
          break;
2236
        }
2237
 
2238
      if (msg)
2239
        r = info->callbacks->warning
2240
          (info, msg, name, input_bfd, input_section, rel->r_offset);
2241
      goto out;
2242
    }
2243
 
2244
  ret = riscv_resolve_pcrel_lo_relocs (&pcrel_relocs);
2245
out:
2246
  riscv_free_pcrel_relocs (&pcrel_relocs);
2247
  return ret;
2248
}
2249
 
2250
/* Finish up dynamic symbol handling.  We set the contents of various
2251
   dynamic sections here.  */
2252
 
2253
static bfd_boolean
2254
riscv_elf_finish_dynamic_symbol (bfd *output_bfd,
2255
                                 struct bfd_link_info *info,
2256
                                 struct elf_link_hash_entry *h,
2257
                                 Elf_Internal_Sym *sym)
2258
{
2259
  struct riscv_elf_link_hash_table *htab = riscv_elf_hash_table (info);
2260
  const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
2261
 
2262
  if (h->plt.offset != (bfd_vma) -1)
2263
    {
2264
      /* We've decided to create a PLT entry for this symbol.  */
2265
      bfd_byte *loc;
2266
      bfd_vma i, header_address, plt_idx, got_address;
2267
      uint32_t plt_entry[PLT_ENTRY_INSNS];
2268
      Elf_Internal_Rela rela;
2269
 
2270
      BFD_ASSERT (h->dynindx != -1);
2271
 
2272
      /* Calculate the address of the PLT header.  */
2273
      header_address = sec_addr (htab->elf.splt);
2274
 
2275
      /* Calculate the index of the entry.  */
2276
      plt_idx = (h->plt.offset - PLT_HEADER_SIZE) / PLT_ENTRY_SIZE;
2277
 
2278
      /* Calculate the address of the .got.plt entry.  */
2279
      got_address = riscv_elf_got_plt_val (plt_idx, info);
2280
 
2281
      /* Find out where the .plt entry should go.  */
2282
      loc = htab->elf.splt->contents + h->plt.offset;
2283
 
2284
      /* Fill in the PLT entry itself.  */
2285
      riscv_make_plt_entry (got_address, header_address + h->plt.offset,
2286
                            plt_entry);
2287
      for (i = 0; i < PLT_ENTRY_INSNS; i++)
2288
        bfd_put_32 (output_bfd, plt_entry[i], loc + 4*i);
2289
 
2290
      /* Fill in the initial value of the .got.plt entry.  */
2291
      loc = htab->elf.sgotplt->contents
2292
            + (got_address - sec_addr (htab->elf.sgotplt));
2293
      bfd_put_NN (output_bfd, sec_addr (htab->elf.splt), loc);
2294
 
2295
      /* Fill in the entry in the .rela.plt section.  */
2296
      rela.r_offset = got_address;
2297
      rela.r_addend = 0;
2298
      rela.r_info = ELFNN_R_INFO (h->dynindx, R_RISCV_JUMP_SLOT);
2299
 
2300
      loc = htab->elf.srelplt->contents + plt_idx * sizeof (ElfNN_External_Rela);
2301
      bed->s->swap_reloca_out (output_bfd, &rela, loc);
2302
 
2303
      if (!h->def_regular)
2304
        {
2305
          /* Mark the symbol as undefined, rather than as defined in
2306
             the .plt section.  Leave the value alone.  */
2307
          sym->st_shndx = SHN_UNDEF;
2308
          /* If the symbol is weak, we do need to clear the value.
2309
             Otherwise, the PLT entry would provide a definition for
2310
             the symbol even if the symbol wasn't defined anywhere,
2311
             and so the symbol would never be NULL.  */
2312
          if (!h->ref_regular_nonweak)
2313
            sym->st_value = 0;
2314
        }
2315
    }
2316
 
2317
  if (h->got.offset != (bfd_vma) -1
2318
      && !(riscv_elf_hash_entry(h)->tls_type & (GOT_TLS_GD | GOT_TLS_IE)))
2319
    {
2320
      asection *sgot;
2321
      asection *srela;
2322
      Elf_Internal_Rela rela;
2323
 
2324
      /* This symbol has an entry in the GOT.  Set it up.  */
2325
 
2326
      sgot = htab->elf.sgot;
2327
      srela = htab->elf.srelgot;
2328
      BFD_ASSERT (sgot != NULL && srela != NULL);
2329
 
2330
      rela.r_offset = sec_addr (sgot) + (h->got.offset &~ (bfd_vma) 1);
2331
 
2332
      /* If this is a -Bsymbolic link, and the symbol is defined
2333
         locally, we just want to emit a RELATIVE reloc.  Likewise if
2334
         the symbol was forced to be local because of a version file.
2335
         The entry in the global offset table will already have been
2336
         initialized in the relocate_section function.  */
2337
      if (info->shared
2338
          && (info->symbolic || h->dynindx == -1)
2339
          && h->def_regular)
2340
        {
2341
          asection *sec = h->root.u.def.section;
2342
          rela.r_info = ELFNN_R_INFO (0, R_RISCV_RELATIVE);
2343
          rela.r_addend = (h->root.u.def.value
2344
                           + sec->output_section->vma
2345
                           + sec->output_offset);
2346
        }
2347
      else
2348
        {
2349
          BFD_ASSERT (h->dynindx != -1);
2350
          rela.r_info = ELFNN_R_INFO (h->dynindx, R_RISCV_NN);
2351
          rela.r_addend = 0;
2352
        }
2353
 
2354
      bfd_put_NN (output_bfd, 0,
2355
                  sgot->contents + (h->got.offset & ~(bfd_vma) 1));
2356
      riscv_elf_append_rela (output_bfd, srela, &rela);
2357
    }
2358
 
2359
  if (h->needs_copy)
2360
    {
2361
      Elf_Internal_Rela rela;
2362
 
2363
      /* This symbols needs a copy reloc.  Set it up.  */
2364
      BFD_ASSERT (h->dynindx != -1);
2365
 
2366
      rela.r_offset = sec_addr (h->root.u.def.section) + h->root.u.def.value;
2367
      rela.r_info = ELFNN_R_INFO (h->dynindx, R_RISCV_COPY);
2368
      rela.r_addend = 0;
2369
      riscv_elf_append_rela (output_bfd, htab->srelbss, &rela);
2370
    }
2371
 
2372
  /* Mark some specially defined symbols as absolute.  */
2373
  if (h == htab->elf.hdynamic
2374
      || (h == htab->elf.hgot || h == htab->elf.hplt))
2375
    sym->st_shndx = SHN_ABS;
2376
 
2377
  return TRUE;
2378
}
2379
 
2380
/* Finish up the dynamic sections.  */
2381
 
2382
static bfd_boolean
2383
riscv_finish_dyn (bfd *output_bfd, struct bfd_link_info *info,
2384
                  bfd *dynobj, asection *sdyn)
2385
{
2386
  struct riscv_elf_link_hash_table *htab = riscv_elf_hash_table (info);
2387
  const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
2388
  size_t dynsize = bed->s->sizeof_dyn;
2389
  bfd_byte *dyncon, *dynconend;
2390
 
2391
  dynconend = sdyn->contents + sdyn->size;
2392
  for (dyncon = sdyn->contents; dyncon < dynconend; dyncon += dynsize)
2393
    {
2394
      Elf_Internal_Dyn dyn;
2395
      asection *s;
2396
 
2397
      bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
2398
 
2399
      switch (dyn.d_tag)
2400
        {
2401
        case DT_PLTGOT:
2402
          s = htab->elf.sgotplt;
2403
          dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
2404
          break;
2405
        case DT_JMPREL:
2406
          s = htab->elf.srelplt;
2407
          dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
2408
          break;
2409
        case DT_PLTRELSZ:
2410
          s = htab->elf.srelplt;
2411
          dyn.d_un.d_val = s->size;
2412
          break;
2413
        default:
2414
          continue;
2415
        }
2416
 
2417
      bed->s->swap_dyn_out (output_bfd, &dyn, dyncon);
2418
    }
2419
  return TRUE;
2420
}
2421
 
2422
static bfd_boolean
2423
riscv_elf_finish_dynamic_sections (bfd *output_bfd,
2424
                                   struct bfd_link_info *info)
2425
{
2426
  bfd *dynobj;
2427
  asection *sdyn;
2428
  struct riscv_elf_link_hash_table *htab;
2429
 
2430
  htab = riscv_elf_hash_table (info);
2431
  BFD_ASSERT (htab != NULL);
2432
  dynobj = htab->elf.dynobj;
2433
 
2434
  sdyn = bfd_get_linker_section (dynobj, ".dynamic");
2435
 
2436
  if (elf_hash_table (info)->dynamic_sections_created)
2437
    {
2438
      asection *splt;
2439
      bfd_boolean ret;
2440
 
2441
      splt = htab->elf.splt;
2442
      BFD_ASSERT (splt != NULL && sdyn != NULL);
2443
 
2444
      ret = riscv_finish_dyn (output_bfd, info, dynobj, sdyn);
2445
 
2446
      if (ret != TRUE)
2447
        return ret;
2448
 
2449
      /* Fill in the head and tail entries in the procedure linkage table.  */
2450
      if (splt->size > 0)
2451
        {
2452
          int i;
2453
          uint32_t plt_header[PLT_HEADER_INSNS];
2454
          riscv_make_plt_header (sec_addr (htab->elf.sgotplt),
2455
                                 sec_addr (splt), plt_header);
2456
 
2457
          for (i = 0; i < PLT_HEADER_INSNS; i++)
2458
            bfd_put_32 (output_bfd, plt_header[i], splt->contents + 4*i);
2459
        }
2460
 
2461
      elf_section_data (splt->output_section)->this_hdr.sh_entsize
2462
        = PLT_ENTRY_SIZE;
2463
    }
2464
 
2465
  if (htab->elf.sgotplt)
2466
    {
2467
      asection *output_section = htab->elf.sgotplt->output_section;
2468
 
2469
      if (bfd_is_abs_section (output_section))
2470
        {
2471
          (*_bfd_error_handler)
2472
            (_("discarded output section: `%A'"), htab->elf.sgotplt);
2473
          return FALSE;
2474
        }
2475
 
2476
      if (htab->elf.sgotplt->size > 0)
2477
        {
2478
          /* Write the first two entries in .got.plt, needed for the dynamic
2479
             linker.  */
2480
          bfd_put_NN (output_bfd, (bfd_vma) -1, htab->elf.sgotplt->contents);
2481
          bfd_put_NN (output_bfd, (bfd_vma) 0,
2482
                      htab->elf.sgotplt->contents + GOT_ENTRY_SIZE);
2483
        }
2484
 
2485
      elf_section_data (output_section)->this_hdr.sh_entsize = GOT_ENTRY_SIZE;
2486
    }
2487
 
2488
  if (htab->elf.sgot)
2489
    {
2490
      asection *output_section = htab->elf.sgot->output_section;
2491
 
2492
      if (htab->elf.sgot->size > 0)
2493
        {
2494
          /* Set the first entry in the global offset table to the address of
2495
             the dynamic section.  */
2496
          bfd_vma val = sdyn ? sec_addr (sdyn) : 0;
2497
          bfd_put_NN (output_bfd, val, htab->elf.sgot->contents);
2498
        }
2499
 
2500
      elf_section_data (output_section)->this_hdr.sh_entsize = GOT_ENTRY_SIZE;
2501
    }
2502
 
2503
  return TRUE;
2504
}
2505
 
2506
/* Return address for Ith PLT stub in section PLT, for relocation REL
2507
   or (bfd_vma) -1 if it should not be included.  */
2508
 
2509
static bfd_vma
2510
riscv_elf_plt_sym_val (bfd_vma i, const asection *plt,
2511
                       const arelent *rel ATTRIBUTE_UNUSED)
2512
{
2513
  return plt->vma + PLT_HEADER_SIZE + i * PLT_ENTRY_SIZE;
2514
}
2515
 
2516
static enum elf_reloc_type_class
2517
riscv_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED,
2518
                        const asection *rel_sec ATTRIBUTE_UNUSED,
2519
                        const Elf_Internal_Rela *rela)
2520
{
2521
  switch (ELFNN_R_TYPE (rela->r_info))
2522
    {
2523
    case R_RISCV_RELATIVE:
2524
      return reloc_class_relative;
2525
    case R_RISCV_JUMP_SLOT:
2526
      return reloc_class_plt;
2527
    case R_RISCV_COPY:
2528
      return reloc_class_copy;
2529
    default:
2530
      return reloc_class_normal;
2531
    }
2532
}
2533
 
2534
/* Merge backend specific data from an object file to the output
2535
   object file when linking.  */
2536
 
2537
static bfd_boolean
2538
_bfd_riscv_elf_merge_private_bfd_data (bfd *ibfd, bfd *obfd)
2539
{
2540
  flagword new_flags = elf_elfheader (ibfd)->e_flags;
2541
  flagword old_flags = elf_elfheader (obfd)->e_flags;
2542
 
2543
  if (!is_riscv_elf (ibfd) || !is_riscv_elf (obfd))
2544
    return TRUE;
2545
 
2546
  if (strcmp (bfd_get_target (ibfd), bfd_get_target (obfd)) != 0)
2547
    {
2548
      (*_bfd_error_handler)
2549
        (_("%B: ABI is incompatible with that of the selected emulation"),
2550
         ibfd);
2551
      return FALSE;
2552
    }
2553
 
2554
  if (!_bfd_elf_merge_object_attributes (ibfd, obfd))
2555
    return FALSE;
2556
 
2557
  if (! elf_flags_init (obfd))
2558
    {
2559
      elf_flags_init (obfd) = TRUE;
2560
      elf_elfheader (obfd)->e_flags = new_flags;
2561
      return TRUE;
2562
    }
2563
 
2564
  /* Disallow linking soft-float and hard-float.  */
2565
  if ((old_flags ^ new_flags) & EF_RISCV_SOFT_FLOAT)
2566
    {
2567
      (*_bfd_error_handler)
2568
        (_("%B: can't link hard-float modules with soft-float modules"), ibfd);
2569
      goto fail;
2570
    }
2571
 
2572
  /* Allow linking RVC and non-RVC, and keep the RVC flag.  */
2573
  elf_elfheader (obfd)->e_flags |= new_flags & EF_RISCV_RVC;
2574
 
2575
  return TRUE;
2576
 
2577
fail:
2578
  bfd_set_error (bfd_error_bad_value);
2579
  return FALSE;
2580
}
2581
 
2582
/* Delete some bytes from a section while relaxing.  */
2583
 
2584
static bfd_boolean
2585
riscv_relax_delete_bytes (bfd *abfd, asection *sec, bfd_vma addr, size_t count)
2586
{
2587
  unsigned int i, symcount;
2588
  bfd_vma toaddr = sec->size;
2589
  struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (abfd);
2590
  Elf_Internal_Shdr *symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2591
  unsigned int sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
2592
  struct bfd_elf_section_data *data = elf_section_data (sec);
2593
  bfd_byte *contents = data->this_hdr.contents;
2594
 
2595
  /* Actually delete the bytes.  */
2596
  sec->size -= count;
2597
  memmove (contents + addr, contents + addr + count, toaddr - addr - count);
2598
 
2599
  /* Adjust the location of all of the relocs.  Note that we need not
2600
     adjust the addends, since all PC-relative references must be against
2601
     symbols, which we will adjust below.  */
2602
  for (i = 0; i < sec->reloc_count; i++)
2603
    if (data->relocs[i].r_offset > addr && data->relocs[i].r_offset < toaddr)
2604
      data->relocs[i].r_offset -= count;
2605
 
2606
  /* Adjust the local symbols defined in this section.  */
2607
  for (i = 0; i < symtab_hdr->sh_info; i++)
2608
    {
2609
      Elf_Internal_Sym *sym = (Elf_Internal_Sym *) symtab_hdr->contents + i;
2610
      if (sym->st_shndx == sec_shndx)
2611
        {
2612
          /* If the symbol is in the range of memory we just moved, we
2613
             have to adjust its value.  */
2614
          if (sym->st_value > addr && sym->st_value <= toaddr)
2615
            sym->st_value -= count;
2616
 
2617
          /* If the symbol *spans* the bytes we just deleted (i.e. its
2618
             *end* is in the moved bytes but its *start* isn't), then we
2619
             must adjust its size.  */
2620
          if (sym->st_value <= addr
2621
              && sym->st_value + sym->st_size > addr
2622
              && sym->st_value + sym->st_size <= toaddr)
2623
            sym->st_size -= count;
2624
        }
2625
    }
2626
 
2627
  /* Now adjust the global symbols defined in this section.  */
2628
  symcount = ((symtab_hdr->sh_size / sizeof (ElfNN_External_Sym))
2629
              - symtab_hdr->sh_info);
2630
 
2631
  for (i = 0; i < symcount; i++)
2632
    {
2633
      struct elf_link_hash_entry *sym_hash = sym_hashes[i];
2634
 
2635
      if ((sym_hash->root.type == bfd_link_hash_defined
2636
           || sym_hash->root.type == bfd_link_hash_defweak)
2637
          && sym_hash->root.u.def.section == sec)
2638
        {
2639
          /* As above, adjust the value if needed.  */
2640
          if (sym_hash->root.u.def.value > addr
2641
              && sym_hash->root.u.def.value <= toaddr)
2642
            sym_hash->root.u.def.value -= count;
2643
 
2644
          /* As above, adjust the size if needed.  */
2645
          if (sym_hash->root.u.def.value <= addr
2646
              && sym_hash->root.u.def.value + sym_hash->size > addr
2647
              && sym_hash->root.u.def.value + sym_hash->size <= toaddr)
2648
            sym_hash->size -= count;
2649
        }
2650
    }
2651
 
2652
  return TRUE;
2653
}
2654
 
2655
/* Relax AUIPC + JALR into JAL.  */
2656
 
2657
static bfd_boolean
2658
_bfd_riscv_relax_call (bfd *abfd, asection *sec, asection *sym_sec,
2659
                       struct bfd_link_info *link_info,
2660
                       Elf_Internal_Rela *rel,
2661
                       bfd_vma symval,
2662
                       bfd_boolean *again)
2663
{
2664
  bfd_byte *contents = elf_section_data (sec)->this_hdr.contents;
2665
  bfd_signed_vma foff = symval - (sec_addr (sec) + rel->r_offset);
2666
  bfd_boolean near_zero = (symval + RISCV_IMM_REACH/2) < RISCV_IMM_REACH;
2667
  bfd_vma auipc, jalr;
2668
  int rd, r_type, len = 4, rvc = elf_elfheader (abfd)->e_flags & EF_RISCV_RVC;
2669
 
2670
  /* If the call crosses section boundaries, an alignment directive could
2671
     cause the PC-relative offset to later increase.  Assume at most
2672
     page-alignment, and account for this by adding some slop.  */
2673
  if (VALID_UJTYPE_IMM (foff) && sym_sec->output_section != sec->output_section)
2674
    foff += (foff < 0 ? -ELF_MAXPAGESIZE : ELF_MAXPAGESIZE);
2675
 
2676
  /* See if this function call can be shortened.  */
2677
  if (!VALID_UJTYPE_IMM (foff) && !(!link_info->shared && near_zero))
2678
    return TRUE;
2679
 
2680
  /* Shorten the function call.  */
2681
  BFD_ASSERT (rel->r_offset + 8 <= sec->size);
2682
 
2683
  auipc = bfd_get_32 (abfd, contents + rel->r_offset);
2684
  jalr = bfd_get_32 (abfd, contents + rel->r_offset + 4);
2685
  rd = (jalr >> OP_SH_RD) & OP_MASK_RD;
2686
  rvc = rvc && VALID_RVC_J_IMM (foff) && ARCH_SIZE == 32;
2687
 
2688
  if (rvc && (rd == 0 || rd == X_RA))
2689
    {
2690
      /* Relax to C.J[AL] rd, addr.  */
2691
      r_type = R_RISCV_RVC_JUMP;
2692
      auipc = rd == 0 ? MATCH_C_J : MATCH_C_JAL;
2693
      len = 2;
2694
    }
2695
  else if (VALID_UJTYPE_IMM (foff))
2696
    {
2697
      /* Relax to JAL rd, addr.  */
2698
      r_type = R_RISCV_JAL;
2699
      auipc = MATCH_JAL | (rd << OP_SH_RD);
2700
    }
2701
  else /* near_zero */
2702
    {
2703
      /* Relax to JALR rd, x0, addr.  */
2704
      r_type = R_RISCV_LO12_I;
2705
      auipc = MATCH_JALR | (rd << OP_SH_RD);
2706
    }
2707
 
2708
  /* Replace the R_RISCV_CALL reloc.  */
2709
  rel->r_info = ELFNN_R_INFO (ELFNN_R_SYM (rel->r_info), r_type);
2710
  /* Replace the AUIPC.  */
2711
  bfd_put (8 * len, abfd, auipc, contents + rel->r_offset);
2712
 
2713
  /* Delete unnecessary JALR.  */
2714
  *again = TRUE;
2715
  return riscv_relax_delete_bytes (abfd, sec, rel->r_offset + len, 8 - len);
2716
}
2717
 
2718
/* Relax non-PIC global variable references.  */
2719
 
2720
static bfd_boolean
2721
_bfd_riscv_relax_lui (bfd *abfd, asection *sec, asection *sym_sec,
2722
                      struct bfd_link_info *link_info,
2723
                      Elf_Internal_Rela *rel,
2724
                      bfd_vma symval,
2725
                      bfd_boolean *again)
2726
{
2727
  bfd_byte *contents = elf_section_data (sec)->this_hdr.contents;
2728
  bfd_vma gp = riscv_global_pointer_value (link_info);
2729
  int use_rvc = elf_elfheader (abfd)->e_flags & EF_RISCV_RVC;
2730
 
2731
  /* Mergeable symbols might later move out of range.  */
2732
  if (sym_sec->flags & SEC_MERGE)
2733
    return TRUE;
2734
 
2735
  BFD_ASSERT (rel->r_offset + 4 <= sec->size);
2736
 
2737
  /* Is the reference in range of x0 or gp?  */
2738
  if (VALID_ITYPE_IMM (symval) || VALID_ITYPE_IMM (symval - gp))
2739
    {
2740
      unsigned sym = ELFNN_R_SYM (rel->r_info);
2741
      switch (ELFNN_R_TYPE (rel->r_info))
2742
        {
2743
        case R_RISCV_LO12_I:
2744
          rel->r_info = ELFNN_R_INFO (sym, R_RISCV_GPREL_I);
2745
          return TRUE;
2746
 
2747
        case R_RISCV_LO12_S:
2748
          rel->r_info = ELFNN_R_INFO (sym, R_RISCV_GPREL_S);
2749
          return TRUE;
2750
 
2751
        case R_RISCV_HI20:
2752
          /* We can delete the unnecessary LUI and reloc.  */
2753
          rel->r_info = ELFNN_R_INFO (0, R_RISCV_NONE);
2754
          *again = TRUE;
2755
          return riscv_relax_delete_bytes (abfd, sec, rel->r_offset, 4);
2756
 
2757
        default:
2758
          abort ();
2759
        }
2760
    }
2761
 
2762
  /* Can we relax LUI to C.LUI?  Alignment might move the section forward;
2763
     account for this assuming page alignment at worst.  */
2764
  if (use_rvc
2765
      && ELFNN_R_TYPE (rel->r_info) == R_RISCV_HI20
2766
      && VALID_RVC_LUI_IMM (RISCV_CONST_HIGH_PART (symval))
2767
      && VALID_RVC_LUI_IMM (RISCV_CONST_HIGH_PART (symval + ELF_MAXPAGESIZE)))
2768
    {
2769
      /* Replace LUI with C.LUI if legal (i.e., rd != x2/sp).  */
2770
      bfd_vma lui = bfd_get_32 (abfd, contents + rel->r_offset);
2771
      if (((lui >> OP_SH_RD) & OP_MASK_RD) == X_SP)
2772
        return TRUE;
2773
 
2774
      lui = (lui & (OP_MASK_RD << OP_SH_RD)) | MATCH_C_LUI;
2775
      bfd_put_32 (abfd, lui, contents + rel->r_offset);
2776
 
2777
      /* Replace the R_RISCV_HI20 reloc.  */
2778
      rel->r_info = ELFNN_R_INFO (ELFNN_R_SYM (rel->r_info), R_RISCV_RVC_LUI);
2779
 
2780
      *again = TRUE;
2781
      return riscv_relax_delete_bytes (abfd, sec, rel->r_offset + 2, 2);
2782
    }
2783
 
2784
  return TRUE;
2785
}
2786
 
2787
/* Relax non-PIC TLS references.  */
2788
 
2789
static bfd_boolean
2790
_bfd_riscv_relax_tls_le (bfd *abfd, asection *sec,
2791
                         asection *sym_sec ATTRIBUTE_UNUSED,
2792
                         struct bfd_link_info *link_info,
2793
                         Elf_Internal_Rela *rel,
2794
                         bfd_vma symval,
2795
                         bfd_boolean *again)
2796
{
2797
  /* See if this symbol is in range of tp.  */
2798
  if (RISCV_CONST_HIGH_PART (tpoff (link_info, symval)) != 0)
2799
    return TRUE;
2800
 
2801
  /* We can delete the unnecessary LUI and tp add.  The LO12 reloc will be
2802
     made directly tp-relative.  */
2803
  BFD_ASSERT (rel->r_offset + 4 <= sec->size);
2804
  rel->r_info = ELFNN_R_INFO (0, R_RISCV_NONE);
2805
 
2806
  *again = TRUE;
2807
  return riscv_relax_delete_bytes (abfd, sec, rel->r_offset, 4);
2808
}
2809
 
2810
/* Implement R_RISCV_ALIGN by deleting excess alignment NOPs.  */
2811
 
2812
static bfd_boolean
2813
_bfd_riscv_relax_align (bfd *abfd, asection *sec,
2814
                        asection *sym_sec ATTRIBUTE_UNUSED,
2815
                        struct bfd_link_info *link_info ATTRIBUTE_UNUSED,
2816
                        Elf_Internal_Rela *rel,
2817
                        bfd_vma symval,
2818
                        bfd_boolean *again ATTRIBUTE_UNUSED)
2819
{
2820
  bfd_byte *contents = elf_section_data (sec)->this_hdr.contents;
2821
  bfd_vma alignment = 1, pos;
2822
  while (alignment <= rel->r_addend)
2823
    alignment *= 2;
2824
 
2825
  symval -= rel->r_addend;
2826
  bfd_vma aligned_addr = ((symval - 1) & ~(alignment - 1)) + alignment;
2827
  bfd_vma nop_bytes = aligned_addr - symval;
2828
 
2829
  /* Once we've handled an R_RISCV_ALIGN, we can't relax anything else.  */
2830
  sec->sec_flg0 = TRUE;
2831
 
2832
  /* Make sure there are enough NOPs to actually achieve the alignment.  */
2833
  if (rel->r_addend < nop_bytes)
2834
    return FALSE;
2835
 
2836
  /* Delete the reloc.  */
2837
  rel->r_info = ELFNN_R_INFO (0, R_RISCV_NONE);
2838
 
2839
  /* If the number of NOPs is already correct, there's nothing to do.  */
2840
  if (nop_bytes == rel->r_addend)
2841
    return TRUE;
2842
 
2843
  /* Write as many RISC-V NOPs as we need.  */
2844
  for (pos = 0; pos < (nop_bytes & -4); pos += 4)
2845
    bfd_put_32 (abfd, RISCV_NOP, contents + rel->r_offset + pos);
2846
 
2847
  /* Write a final RVC NOP if need be.  */
2848
  if (nop_bytes % 4 != 0)
2849
    bfd_put_16 (abfd, RVC_NOP, contents + rel->r_offset + pos);
2850
 
2851
  /* Delete the excess bytes.  */
2852
  return riscv_relax_delete_bytes (abfd, sec, rel->r_offset + nop_bytes,
2853
                                   rel->r_addend - nop_bytes);
2854
}
2855
 
2856
/* Relax a section.  Pass 0 shortens code sequences unless disabled.
2857
   Pass 1, which cannot be disabled, handles code alignment directives.  */
2858
 
2859
static bfd_boolean
2860
_bfd_riscv_relax_section (bfd *abfd, asection *sec,
2861
                          struct bfd_link_info *info, bfd_boolean *again)
2862
{
2863
  Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (abfd);
2864
  struct riscv_elf_link_hash_table *htab = riscv_elf_hash_table (info);
2865
  struct bfd_elf_section_data *data = elf_section_data (sec);
2866
  Elf_Internal_Rela *relocs;
2867
  bfd_boolean ret = FALSE;
2868
  unsigned int i;
2869
 
2870
  *again = FALSE;
2871
 
2872
  if (info->relocatable
2873
      || sec->sec_flg0
2874
      || (sec->flags & SEC_RELOC) == 0
2875
      || sec->reloc_count == 0
2876
      || (info->disable_target_specific_optimizations
2877
          && info->relax_pass == 0))
2878
    return TRUE;
2879
 
2880
  /* Read this BFD's relocs if we haven't done so already.  */
2881
  if (data->relocs)
2882
    relocs = data->relocs;
2883
  else if (!(relocs = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
2884
                                                 info->keep_memory)))
2885
    goto fail;
2886
 
2887
  /* Examine and consider relaxing each reloc.  */
2888
  for (i = 0; i < sec->reloc_count; i++)
2889
    {
2890
      asection *sym_sec;
2891
      Elf_Internal_Rela *rel = relocs + i;
2892
      typeof (&_bfd_riscv_relax_call) relax_func = NULL;
2893
      int type = ELFNN_R_TYPE (rel->r_info);
2894
      bfd_vma symval;
2895
 
2896
      if (info->relax_pass == 0)
2897
        {
2898
          if (type == R_RISCV_CALL || type == R_RISCV_CALL_PLT)
2899
            relax_func = _bfd_riscv_relax_call;
2900
          else if (type == R_RISCV_HI20
2901
                   || type == R_RISCV_LO12_I
2902
                   || type == R_RISCV_LO12_S)
2903
            relax_func = _bfd_riscv_relax_lui;
2904
          else if (type == R_RISCV_TPREL_HI20 || type == R_RISCV_TPREL_ADD)
2905
            relax_func = _bfd_riscv_relax_tls_le;
2906
        }
2907
      else if (type == R_RISCV_ALIGN)
2908
        relax_func = _bfd_riscv_relax_align;
2909
 
2910
      if (!relax_func)
2911
        continue;
2912
 
2913
      data->relocs = relocs;
2914
 
2915
      /* Read this BFD's contents if we haven't done so already.  */
2916
      if (!data->this_hdr.contents
2917
          && !bfd_malloc_and_get_section (abfd, sec, &data->this_hdr.contents))
2918
        goto fail;
2919
 
2920
      /* Read this BFD's symbols if we haven't done so already.  */
2921
      if (symtab_hdr->sh_info != 0
2922
          && !symtab_hdr->contents
2923
          && !(symtab_hdr->contents =
2924
               (unsigned char *) bfd_elf_get_elf_syms (abfd, symtab_hdr,
2925
                                                       symtab_hdr->sh_info,
2926
                                                       0, NULL, NULL, NULL)))
2927
        goto fail;
2928
 
2929
      /* Get the value of the symbol referred to by the reloc.  */
2930
      if (ELFNN_R_SYM (rel->r_info) < symtab_hdr->sh_info)
2931
        {
2932
          /* A local symbol.  */
2933
          Elf_Internal_Sym *isym = ((Elf_Internal_Sym *) symtab_hdr->contents
2934
                                    + ELFNN_R_SYM (rel->r_info));
2935
 
2936
          if (isym->st_shndx == SHN_UNDEF)
2937
            sym_sec = sec, symval = sec_addr (sec) + rel->r_offset;
2938
          else
2939
            {
2940
              BFD_ASSERT (isym->st_shndx < elf_numsections (abfd));
2941
              sym_sec = elf_elfsections (abfd)[isym->st_shndx]->bfd_section;
2942
              if (sec_addr (sym_sec) == 0)
2943
                continue;
2944
              symval = sec_addr (sym_sec) + isym->st_value;
2945
            }
2946
        }
2947
      else
2948
        {
2949
          unsigned long indx;
2950
          struct elf_link_hash_entry *h;
2951
 
2952
          indx = ELFNN_R_SYM (rel->r_info) - symtab_hdr->sh_info;
2953
          h = elf_sym_hashes (abfd)[indx];
2954
 
2955
          while (h->root.type == bfd_link_hash_indirect
2956
                 || h->root.type == bfd_link_hash_warning)
2957
            h = (struct elf_link_hash_entry *) h->root.u.i.link;
2958
 
2959
          if (h->plt.offset != MINUS_ONE)
2960
            symval = sec_addr (htab->elf.splt) + h->plt.offset;
2961
          else if (h->root.u.def.section->output_section == NULL
2962
                   || (h->root.type != bfd_link_hash_defined
2963
                       && h->root.type != bfd_link_hash_defweak))
2964
            continue;
2965
          else
2966
            symval = sec_addr (h->root.u.def.section) + h->root.u.def.value;
2967
 
2968
          sym_sec = h->root.u.def.section;
2969
        }
2970
 
2971
      symval += rel->r_addend;
2972
 
2973
      if (!relax_func (abfd, sec, sym_sec, info, rel, symval, again))
2974
        goto fail;
2975
    }
2976
 
2977
  ret = TRUE;
2978
 
2979
fail:
2980
  if (relocs != data->relocs)
2981
    free (relocs);
2982
 
2983
  return ret;
2984
}
2985
 
2986
#define TARGET_LITTLE_SYM               riscv_elfNN_vec
2987
#define TARGET_LITTLE_NAME              "elfNN-littleriscv"
2988
 
2989
#define elf_backend_reloc_type_class         riscv_reloc_type_class
2990
 
2991
#define bfd_elfNN_bfd_reloc_name_lookup      riscv_reloc_name_lookup
2992
#define bfd_elfNN_bfd_link_hash_table_create riscv_elf_link_hash_table_create
2993
#define bfd_elfNN_bfd_reloc_type_lookup      riscv_reloc_type_lookup
2994
#define bfd_elfNN_bfd_merge_private_bfd_data \
2995
  _bfd_riscv_elf_merge_private_bfd_data
2996
 
2997
#define elf_backend_copy_indirect_symbol     riscv_elf_copy_indirect_symbol
2998
#define elf_backend_create_dynamic_sections  riscv_elf_create_dynamic_sections
2999
#define elf_backend_check_relocs             riscv_elf_check_relocs
3000
#define elf_backend_adjust_dynamic_symbol    riscv_elf_adjust_dynamic_symbol
3001
#define elf_backend_size_dynamic_sections    riscv_elf_size_dynamic_sections
3002
#define elf_backend_relocate_section         riscv_elf_relocate_section
3003
#define elf_backend_finish_dynamic_symbol    riscv_elf_finish_dynamic_symbol
3004
#define elf_backend_finish_dynamic_sections  riscv_elf_finish_dynamic_sections
3005
#define elf_backend_gc_mark_hook             riscv_elf_gc_mark_hook
3006
#define elf_backend_gc_sweep_hook            riscv_elf_gc_sweep_hook
3007
#define elf_backend_plt_sym_val              riscv_elf_plt_sym_val
3008
#define elf_info_to_howto_rel                NULL
3009
#define elf_info_to_howto                    riscv_info_to_howto_rela
3010
#define bfd_elfNN_bfd_relax_section          _bfd_riscv_relax_section
3011
 
3012
#define elf_backend_init_index_section       _bfd_elf_init_1_index_section
3013
 
3014
#define elf_backend_can_gc_sections     1
3015
#define elf_backend_can_refcount        1
3016
#define elf_backend_want_got_plt        1
3017
#define elf_backend_plt_readonly        1
3018
#define elf_backend_plt_alignment       4
3019
#define elf_backend_want_plt_sym        1
3020
#define elf_backend_got_header_size     (ARCH_SIZE / 8)
3021
#define elf_backend_rela_normal         1
3022
#define elf_backend_default_execstack   0
3023
 
3024
#include "elfNN-target.h"

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.