-
Notifications
You must be signed in to change notification settings - Fork 510
/
elfnn-riscv.c
5584 lines (4801 loc) · 164 KB
/
elfnn-riscv.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* RISC-V-specific support for NN-bit ELF.
Copyright (C) 2011-2023 Free Software Foundation, Inc.
Contributed by Andrew Waterman (andrew@sifive.com).
Based on TILE-Gx and MIPS targets.
This file is part of BFD, the Binary File Descriptor library.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; see the file COPYING3. If not,
see <http://www.gnu.org/licenses/>. */
/* This file handles RISC-V ELF targets. */
#include "sysdep.h"
#include "bfd.h"
#include "libbfd.h"
#include "bfdlink.h"
#include "genlink.h"
#include "elf-bfd.h"
#include "elfxx-riscv.h"
#include "elf/riscv.h"
#include "opcode/riscv.h"
#include "objalloc.h"
#include <limits.h>
#ifndef CHAR_BIT
#define CHAR_BIT 8
#endif
/* True if dynamic relocation is needed. If we are creating a shared library,
and this is a reloc against a global symbol, or a non PC relative reloc
against a local symbol, then we need to copy the reloc into the shared
library. However, if we are linking with -Bsymbolic, we do not need to
copy a reloc against a global symbol which is defined in an object we are
including in the link (i.e., DEF_REGULAR is set).
At this point we have not seen all the input files, so it is possible that
DEF_REGULAR is not set now but will be set later (it is never cleared).
In case of a weak definition, DEF_REGULAR may be cleared later by a strong
definition in a shared library. We account for that possibility below by
storing information in the relocs_copied field of the hash table entry.
A similar situation occurs when creating shared libraries and symbol
visibility changes render the symbol local.
If on the other hand, we are creating an executable, we may need to keep
relocations for symbols satisfied by a dynamic library if we manage to
avoid copy relocs for the symbol.
Generate dynamic pointer relocation against STT_GNU_IFUNC symbol in the
non-code section (R_RISCV_32/R_RISCV_64). */
#define RISCV_NEED_DYNAMIC_RELOC(PCREL, INFO, H, SEC) \
((bfd_link_pic (INFO) \
&& ((SEC)->flags & SEC_ALLOC) != 0 \
&& (!(PCREL) \
|| ((H) != NULL \
&& (!(INFO)->symbolic \
|| (H)->root.type == bfd_link_hash_defweak \
|| !(H)->def_regular)))) \
|| (!bfd_link_pic (INFO) \
&& ((SEC)->flags & SEC_ALLOC) != 0 \
&& (H) != NULL \
&& ((H)->root.type == bfd_link_hash_defweak \
|| !(H)->def_regular)) \
|| (!bfd_link_pic (INFO) \
&& (H) != NULL \
&& (H)->type == STT_GNU_IFUNC \
&& ((SEC)->flags & SEC_CODE) == 0))
/* True if dynamic relocation should be generated. */
#define RISCV_GENERATE_DYNAMIC_RELOC(PCREL, INFO, H, RESOLVED_TO_ZERO) \
((bfd_link_pic (INFO) \
&& ((H) == NULL \
|| (ELF_ST_VISIBILITY ((H)->other) == STV_DEFAULT && !(RESOLVED_TO_ZERO)) \
|| (H)->root.type != bfd_link_hash_undefweak) \
&& (!(PCREL) \
|| !SYMBOL_CALLS_LOCAL ((INFO), (H)))) \
|| (!bfd_link_pic (INFO) \
&& (H) != NULL \
&& (H)->dynindx != -1 \
&& !(H)->non_got_ref \
&& (((H)->def_dynamic && !(H)->def_regular) \
|| (H)->root.type == bfd_link_hash_undefweak \
|| (H)->root.type == bfd_link_hash_undefined)))
/* True if this input relocation should be copied to output. H->dynindx
may be -1 if this symbol was marked to become local. */
#define RISCV_COPY_INPUT_RELOC(INFO, H) \
((H) != NULL \
&& (H)->dynindx != -1 \
&& (!bfd_link_pic (INFO) \
|| !SYMBOLIC_BIND ((INFO), (H)) \
|| !(H)->def_regular))
/* True if this is actually a static link, or it is a -Bsymbolic link
and the symbol is defined locally, or the symbol was forced to be
local because of a version file. */
#define RISCV_RESOLVED_LOCALLY(INFO, H) \
(!WILL_CALL_FINISH_DYNAMIC_SYMBOL (elf_hash_table (INFO)->dynamic_sections_created, \
bfd_link_pic (INFO), (H)) \
|| (bfd_link_pic (INFO) \
&& SYMBOL_REFERENCES_LOCAL ((INFO), (H))))
/* Set NEED_RELOC to true if TLS GD/IE needs dynamic relocations, and INDX will
be the dynamic index. PR22263, use the same check in allocate_dynrelocs and
riscv_elf_relocate_section for TLS GD/IE. */
#define RISCV_TLS_GD_IE_NEED_DYN_RELOC(INFO, DYN, H, INDX, NEED_RELOC) \
do \
{ \
if ((H) != NULL \
&& (H)->dynindx != -1 \
&& WILL_CALL_FINISH_DYNAMIC_SYMBOL ((DYN), bfd_link_pic (INFO), (H)) \
&& (bfd_link_dll (INFO) || !SYMBOL_REFERENCES_LOCAL ((INFO), (H)))) \
(INDX) = (H)->dynindx; \
if ((bfd_link_dll (INFO) || (INDX) != 0) \
&& ((H) == NULL \
|| ELF_ST_VISIBILITY ((H)->other) == STV_DEFAULT \
|| (H)->root.type != bfd_link_hash_undefweak)) \
(NEED_RELOC) = true; \
} \
while (0)
/* Internal relocations used exclusively by the relaxation pass. */
#define R_RISCV_DELETE (R_RISCV_max + 1)
#define ARCH_SIZE NN
#define MINUS_ONE ((bfd_vma)0 - 1)
#define RISCV_ELF_LOG_WORD_BYTES (ARCH_SIZE == 32 ? 2 : 3)
#define RISCV_ELF_WORD_BYTES (1 << RISCV_ELF_LOG_WORD_BYTES)
/* The name of the dynamic interpreter. This is put in the .interp
section. */
#define ELF64_DYNAMIC_INTERPRETER "/lib/ld.so.1"
#define ELF32_DYNAMIC_INTERPRETER "/lib32/ld.so.1"
#define ELF_ARCH bfd_arch_riscv
#define ELF_TARGET_ID RISCV_ELF_DATA
#define ELF_MACHINE_CODE EM_RISCV
#define ELF_MAXPAGESIZE 0x1000
#define ELF_COMMONPAGESIZE 0x1000
#define RISCV_ATTRIBUTES_SECTION_NAME ".riscv.attributes"
/* RISC-V ELF linker hash entry. */
struct riscv_elf_link_hash_entry
{
struct elf_link_hash_entry elf;
#define GOT_UNKNOWN 0
#define GOT_NORMAL 1
#define GOT_TLS_GD 2
#define GOT_TLS_IE 4
#define GOT_TLS_LE 8
char tls_type;
};
#define riscv_elf_hash_entry(ent) \
((struct riscv_elf_link_hash_entry *) (ent))
struct _bfd_riscv_elf_obj_tdata
{
struct elf_obj_tdata root;
/* tls_type for each local got entry. */
char *local_got_tls_type;
};
#define _bfd_riscv_elf_tdata(abfd) \
((struct _bfd_riscv_elf_obj_tdata *) (abfd)->tdata.any)
#define _bfd_riscv_elf_local_got_tls_type(abfd) \
(_bfd_riscv_elf_tdata (abfd)->local_got_tls_type)
#define _bfd_riscv_elf_tls_type(abfd, h, symndx) \
(*((h) != NULL ? &riscv_elf_hash_entry (h)->tls_type \
: &_bfd_riscv_elf_local_got_tls_type (abfd) [symndx]))
#define is_riscv_elf(bfd) \
(bfd_get_flavour (bfd) == bfd_target_elf_flavour \
&& elf_tdata (bfd) != NULL \
&& elf_object_id (bfd) == RISCV_ELF_DATA)
static bool
elfNN_riscv_mkobject (bfd *abfd)
{
return bfd_elf_allocate_object (abfd,
sizeof (struct _bfd_riscv_elf_obj_tdata),
RISCV_ELF_DATA);
}
#include "elf/common.h"
#include "elf/internal.h"
struct riscv_elf_link_hash_table
{
struct elf_link_hash_table elf;
/* Various options and other info passed from the linker. */
struct riscv_elf_params *params;
/* Short-cuts to get to dynamic linker sections. */
asection *sdyntdata;
/* The max alignment of output sections. */
bfd_vma max_alignment;
/* The max alignment of output sections in [gp-2K, gp+2K) range. */
bfd_vma max_alignment_for_gp;
/* Used by local STT_GNU_IFUNC symbols. */
htab_t loc_hash_table;
void * loc_hash_memory;
/* The index of the last unused .rel.iplt slot. */
bfd_vma last_iplt_index;
/* The data segment phase, don't relax the section
when it is exp_seg_relro_adjust. */
int *data_segment_phase;
/* Relocations for variant CC symbols may be present. */
int variant_cc;
};
/* Instruction access functions. */
#define riscv_get_insn(bits, ptr) \
((bits) == 16 ? bfd_getl16 (ptr) \
: (bits) == 32 ? bfd_getl32 (ptr) \
: (bits) == 64 ? bfd_getl64 (ptr) \
: (abort (), (bfd_vma) - 1))
#define riscv_put_insn(bits, val, ptr) \
((bits) == 16 ? bfd_putl16 (val, ptr) \
: (bits) == 32 ? bfd_putl32 (val, ptr) \
: (bits) == 64 ? bfd_putl64 (val, ptr) \
: (abort (), (void) 0))
/* Get the RISC-V ELF linker hash table from a link_info structure. */
#define riscv_elf_hash_table(p) \
((is_elf_hash_table ((p)->hash) \
&& elf_hash_table_id (elf_hash_table (p)) == RISCV_ELF_DATA) \
? (struct riscv_elf_link_hash_table *) (p)->hash : NULL)
void
riscv_elfNN_set_options (struct bfd_link_info *link_info,
struct riscv_elf_params *params)
{
riscv_elf_hash_table (link_info)->params = params;
}
static bool
riscv_info_to_howto_rela (bfd *abfd,
arelent *cache_ptr,
Elf_Internal_Rela *dst)
{
cache_ptr->howto = riscv_elf_rtype_to_howto (abfd, ELFNN_R_TYPE (dst->r_info));
return cache_ptr->howto != NULL;
}
static void
riscv_elf_append_rela (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
{
const struct elf_backend_data *bed;
bfd_byte *loc;
bed = get_elf_backend_data (abfd);
loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rela);
bed->s->swap_reloca_out (abfd, rel, loc);
}
/* Return true if a relocation is modifying an instruction. */
static bool
riscv_is_insn_reloc (const reloc_howto_type *howto)
{
/* Heuristic: A multibyte destination with a nontrivial mask
is an instruction */
return (howto->bitsize > 8
&& howto->dst_mask != 0
&& ~(howto->dst_mask | (howto->bitsize < sizeof(bfd_vma) * CHAR_BIT
? (MINUS_ONE << howto->bitsize) : (bfd_vma)0)) != 0);
}
/* PLT/GOT stuff. */
#define PLT_HEADER_INSNS 8
#define PLT_ENTRY_INSNS 4
#define PLT_HEADER_SIZE (PLT_HEADER_INSNS * 4)
#define PLT_ENTRY_SIZE (PLT_ENTRY_INSNS * 4)
#define GOT_ENTRY_SIZE RISCV_ELF_WORD_BYTES
/* Reserve two entries of GOTPLT for ld.so, one is used for PLT resolver,
the other is used for link map. Other targets also reserve one more
entry used for runtime profile? */
#define GOTPLT_HEADER_SIZE (2 * GOT_ENTRY_SIZE)
#define sec_addr(sec) ((sec)->output_section->vma + (sec)->output_offset)
#if ARCH_SIZE == 32
# define MATCH_LREG MATCH_LW
#else
# define MATCH_LREG MATCH_LD
#endif
/* Generate a PLT header. */
static bool
riscv_make_plt_header (bfd *output_bfd, bfd_vma gotplt_addr, bfd_vma addr,
uint32_t *entry)
{
bfd_vma gotplt_offset_high = RISCV_PCREL_HIGH_PART (gotplt_addr, addr);
bfd_vma gotplt_offset_low = RISCV_PCREL_LOW_PART (gotplt_addr, addr);
/* RVE has no t3 register, so this won't work, and is not supported. */
if (elf_elfheader (output_bfd)->e_flags & EF_RISCV_RVE)
{
_bfd_error_handler (_("%pB: warning: RVE PLT generation not supported"),
output_bfd);
return false;
}
/* auipc t2, %hi(.got.plt)
sub t1, t1, t3 # shifted .got.plt offset + hdr size + 12
l[w|d] t3, %lo(.got.plt)(t2) # _dl_runtime_resolve
addi t1, t1, -(hdr size + 12) # shifted .got.plt offset
addi t0, t2, %lo(.got.plt) # &.got.plt
srli t1, t1, log2(16/PTRSIZE) # .got.plt offset
l[w|d] t0, PTRSIZE(t0) # link map
jr t3 */
entry[0] = RISCV_UTYPE (AUIPC, X_T2, gotplt_offset_high);
entry[1] = RISCV_RTYPE (SUB, X_T1, X_T1, X_T3);
entry[2] = RISCV_ITYPE (LREG, X_T3, X_T2, gotplt_offset_low);
entry[3] = RISCV_ITYPE (ADDI, X_T1, X_T1, (uint32_t) -(PLT_HEADER_SIZE + 12));
entry[4] = RISCV_ITYPE (ADDI, X_T0, X_T2, gotplt_offset_low);
entry[5] = RISCV_ITYPE (SRLI, X_T1, X_T1, 4 - RISCV_ELF_LOG_WORD_BYTES);
entry[6] = RISCV_ITYPE (LREG, X_T0, X_T0, RISCV_ELF_WORD_BYTES);
entry[7] = RISCV_ITYPE (JALR, 0, X_T3, 0);
return true;
}
/* Generate a PLT entry. */
static bool
riscv_make_plt_entry (bfd *output_bfd, bfd_vma got, bfd_vma addr,
uint32_t *entry)
{
/* RVE has no t3 register, so this won't work, and is not supported. */
if (elf_elfheader (output_bfd)->e_flags & EF_RISCV_RVE)
{
_bfd_error_handler (_("%pB: warning: RVE PLT generation not supported"),
output_bfd);
return false;
}
/* auipc t3, %hi(.got.plt entry)
l[w|d] t3, %lo(.got.plt entry)(t3)
jalr t1, t3
nop */
entry[0] = RISCV_UTYPE (AUIPC, X_T3, RISCV_PCREL_HIGH_PART (got, addr));
entry[1] = RISCV_ITYPE (LREG, X_T3, X_T3, RISCV_PCREL_LOW_PART (got, addr));
entry[2] = RISCV_ITYPE (JALR, X_T1, X_T3, 0);
entry[3] = RISCV_NOP;
return true;
}
/* Create an entry in an RISC-V ELF linker hash table. */
static struct bfd_hash_entry *
link_hash_newfunc (struct bfd_hash_entry *entry,
struct bfd_hash_table *table, const char *string)
{
/* Allocate the structure if it has not already been allocated by a
subclass. */
if (entry == NULL)
{
entry =
bfd_hash_allocate (table,
sizeof (struct riscv_elf_link_hash_entry));
if (entry == NULL)
return entry;
}
/* Call the allocation method of the superclass. */
entry = _bfd_elf_link_hash_newfunc (entry, table, string);
if (entry != NULL)
{
struct riscv_elf_link_hash_entry *eh;
eh = (struct riscv_elf_link_hash_entry *) entry;
eh->tls_type = GOT_UNKNOWN;
}
return entry;
}
/* Compute a hash of a local hash entry. We use elf_link_hash_entry
for local symbol so that we can handle local STT_GNU_IFUNC symbols
as global symbol. We reuse indx and dynstr_index for local symbol
hash since they aren't used by global symbols in this backend. */
static hashval_t
riscv_elf_local_htab_hash (const void *ptr)
{
struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) ptr;
return ELF_LOCAL_SYMBOL_HASH (h->indx, h->dynstr_index);
}
/* Compare local hash entries. */
static int
riscv_elf_local_htab_eq (const void *ptr1, const void *ptr2)
{
struct elf_link_hash_entry *h1 = (struct elf_link_hash_entry *) ptr1;
struct elf_link_hash_entry *h2 = (struct elf_link_hash_entry *) ptr2;
return h1->indx == h2->indx && h1->dynstr_index == h2->dynstr_index;
}
/* Find and/or create a hash entry for local symbol. */
static struct elf_link_hash_entry *
riscv_elf_get_local_sym_hash (struct riscv_elf_link_hash_table *htab,
bfd *abfd, const Elf_Internal_Rela *rel,
bool create)
{
struct riscv_elf_link_hash_entry eh, *ret;
asection *sec = abfd->sections;
hashval_t h = ELF_LOCAL_SYMBOL_HASH (sec->id,
ELFNN_R_SYM (rel->r_info));
void **slot;
eh.elf.indx = sec->id;
eh.elf.dynstr_index = ELFNN_R_SYM (rel->r_info);
slot = htab_find_slot_with_hash (htab->loc_hash_table, &eh, h,
create ? INSERT : NO_INSERT);
if (!slot)
return NULL;
if (*slot)
{
ret = (struct riscv_elf_link_hash_entry *) *slot;
return &ret->elf;
}
ret = (struct riscv_elf_link_hash_entry *)
objalloc_alloc ((struct objalloc *) htab->loc_hash_memory,
sizeof (struct riscv_elf_link_hash_entry));
if (ret)
{
memset (ret, 0, sizeof (*ret));
ret->elf.indx = sec->id;
ret->elf.dynstr_index = ELFNN_R_SYM (rel->r_info);
ret->elf.dynindx = -1;
*slot = ret;
}
return &ret->elf;
}
/* Destroy a RISC-V elf linker hash table. */
static void
riscv_elf_link_hash_table_free (bfd *obfd)
{
struct riscv_elf_link_hash_table *ret
= (struct riscv_elf_link_hash_table *) obfd->link.hash;
if (ret->loc_hash_table)
htab_delete (ret->loc_hash_table);
if (ret->loc_hash_memory)
objalloc_free ((struct objalloc *) ret->loc_hash_memory);
_bfd_elf_link_hash_table_free (obfd);
}
/* Create a RISC-V ELF linker hash table. */
static struct bfd_link_hash_table *
riscv_elf_link_hash_table_create (bfd *abfd)
{
struct riscv_elf_link_hash_table *ret;
size_t amt = sizeof (struct riscv_elf_link_hash_table);
ret = (struct riscv_elf_link_hash_table *) bfd_zmalloc (amt);
if (ret == NULL)
return NULL;
if (!_bfd_elf_link_hash_table_init (&ret->elf, abfd, link_hash_newfunc,
sizeof (struct riscv_elf_link_hash_entry),
RISCV_ELF_DATA))
{
free (ret);
return NULL;
}
ret->max_alignment = (bfd_vma) -1;
ret->max_alignment_for_gp = (bfd_vma) -1;
/* Create hash table for local ifunc. */
ret->loc_hash_table = htab_try_create (1024,
riscv_elf_local_htab_hash,
riscv_elf_local_htab_eq,
NULL);
ret->loc_hash_memory = objalloc_create ();
if (!ret->loc_hash_table || !ret->loc_hash_memory)
{
riscv_elf_link_hash_table_free (abfd);
return NULL;
}
ret->elf.root.hash_table_free = riscv_elf_link_hash_table_free;
return &ret->elf.root;
}
/* Create the .got section. */
static bool
riscv_elf_create_got_section (bfd *abfd, struct bfd_link_info *info)
{
flagword flags;
asection *s, *s_got;
struct elf_link_hash_entry *h;
const struct elf_backend_data *bed = get_elf_backend_data (abfd);
struct elf_link_hash_table *htab = elf_hash_table (info);
/* This function may be called more than once. */
if (htab->sgot != NULL)
return true;
flags = bed->dynamic_sec_flags;
s = bfd_make_section_anyway_with_flags (abfd,
(bed->rela_plts_and_copies_p
? ".rela.got" : ".rel.got"),
(bed->dynamic_sec_flags
| SEC_READONLY));
if (s == NULL
|| !bfd_set_section_alignment (s, bed->s->log_file_align))
return false;
htab->srelgot = s;
s = s_got = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
if (s == NULL
|| !bfd_set_section_alignment (s, bed->s->log_file_align))
return false;
htab->sgot = s;
/* The first bit of the global offset table is the header. */
s->size += bed->got_header_size;
if (bed->want_got_plt)
{
s = bfd_make_section_anyway_with_flags (abfd, ".got.plt", flags);
if (s == NULL
|| !bfd_set_section_alignment (s, bed->s->log_file_align))
return false;
htab->sgotplt = s;
/* Reserve room for the header. */
s->size += GOTPLT_HEADER_SIZE;
}
if (bed->want_got_sym)
{
/* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
section. We don't do this in the linker script because we don't want
to define the symbol if we are not creating a global offset
table. */
h = _bfd_elf_define_linkage_sym (abfd, info, s_got,
"_GLOBAL_OFFSET_TABLE_");
elf_hash_table (info)->hgot = h;
if (h == NULL)
return false;
}
return true;
}
/* Create .plt, .rela.plt, .got, .got.plt, .rela.got, .dynbss, and
.rela.bss sections in DYNOBJ, and set up shortcuts to them in our
hash table. */
static bool
riscv_elf_create_dynamic_sections (bfd *dynobj,
struct bfd_link_info *info)
{
struct riscv_elf_link_hash_table *htab;
htab = riscv_elf_hash_table (info);
BFD_ASSERT (htab != NULL);
if (!riscv_elf_create_got_section (dynobj, info))
return false;
if (!_bfd_elf_create_dynamic_sections (dynobj, info))
return false;
if (!bfd_link_pic (info))
{
/* Technically, this section doesn't have contents. It is used as the
target of TLS copy relocs, to copy TLS data from shared libraries into
the executable. However, if we don't mark it as loadable, then it
matches the IS_TBSS test in ldlang.c, and there is no run-time address
space allocated for it even though it has SEC_ALLOC. That test is
correct for .tbss, but not correct for this section. There is also
a second problem that having a section with no contents can only work
if it comes after all sections with contents in the same segment,
but the linker script does not guarantee that. This is just mixed in
with other .tdata.* sections. We can fix both problems by lying and
saying that there are contents. This section is expected to be small
so this should not cause a significant extra program startup cost. */
htab->sdyntdata =
bfd_make_section_anyway_with_flags (dynobj, ".tdata.dyn",
(SEC_ALLOC | SEC_THREAD_LOCAL
| SEC_LOAD | SEC_DATA
| SEC_HAS_CONTENTS
| SEC_LINKER_CREATED));
}
if (!htab->elf.splt || !htab->elf.srelplt || !htab->elf.sdynbss
|| (!bfd_link_pic (info) && (!htab->elf.srelbss || !htab->sdyntdata)))
abort ();
return true;
}
/* Copy the extra info we tack onto an elf_link_hash_entry. */
static void
riscv_elf_copy_indirect_symbol (struct bfd_link_info *info,
struct elf_link_hash_entry *dir,
struct elf_link_hash_entry *ind)
{
struct riscv_elf_link_hash_entry *edir, *eind;
edir = (struct riscv_elf_link_hash_entry *) dir;
eind = (struct riscv_elf_link_hash_entry *) ind;
if (ind->root.type == bfd_link_hash_indirect
&& dir->got.refcount <= 0)
{
edir->tls_type = eind->tls_type;
eind->tls_type = GOT_UNKNOWN;
}
_bfd_elf_link_hash_copy_indirect (info, dir, ind);
}
static bool
riscv_elf_record_tls_type (bfd *abfd, struct elf_link_hash_entry *h,
unsigned long symndx, char tls_type)
{
char *new_tls_type = &_bfd_riscv_elf_tls_type (abfd, h, symndx);
*new_tls_type |= tls_type;
if ((*new_tls_type & GOT_NORMAL) && (*new_tls_type & ~GOT_NORMAL))
{
(*_bfd_error_handler)
(_("%pB: `%s' accessed both as normal and thread local symbol"),
abfd, h ? h->root.root.string : "<local>");
return false;
}
return true;
}
static bool
riscv_elf_record_got_reference (bfd *abfd, struct bfd_link_info *info,
struct elf_link_hash_entry *h, long symndx)
{
struct riscv_elf_link_hash_table *htab = riscv_elf_hash_table (info);
Elf_Internal_Shdr *symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
if (htab->elf.sgot == NULL)
{
if (!riscv_elf_create_got_section (htab->elf.dynobj, info))
return false;
}
if (h != NULL)
{
h->got.refcount += 1;
return true;
}
/* This is a global offset table entry for a local symbol. */
if (elf_local_got_refcounts (abfd) == NULL)
{
bfd_size_type size = symtab_hdr->sh_info * (sizeof (bfd_vma) + 1);
if (!(elf_local_got_refcounts (abfd) = bfd_zalloc (abfd, size)))
return false;
_bfd_riscv_elf_local_got_tls_type (abfd)
= (char *) (elf_local_got_refcounts (abfd) + symtab_hdr->sh_info);
}
elf_local_got_refcounts (abfd) [symndx] += 1;
return true;
}
static bool
bad_static_reloc (bfd *abfd, unsigned r_type, struct elf_link_hash_entry *h)
{
reloc_howto_type * r = riscv_elf_rtype_to_howto (abfd, r_type);
/* We propably can improve the information to tell users that they
should be recompile the code with -fPIC or -fPIE, just like what
x86 does. */
(*_bfd_error_handler)
(_("%pB: relocation %s against `%s' can not be used when making a shared "
"object; recompile with -fPIC"),
abfd, r ? r->name : _("<unknown>"),
h != NULL ? h->root.root.string : "a local symbol");
bfd_set_error (bfd_error_bad_value);
return false;
}
/* Look through the relocs for a section during the first phase, and
allocate space in the global offset table or procedure linkage
table. */
static bool
riscv_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
asection *sec, const Elf_Internal_Rela *relocs)
{
struct riscv_elf_link_hash_table *htab;
Elf_Internal_Shdr *symtab_hdr;
struct elf_link_hash_entry **sym_hashes;
const Elf_Internal_Rela *rel;
asection *sreloc = NULL;
if (bfd_link_relocatable (info))
return true;
htab = riscv_elf_hash_table (info);
symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
sym_hashes = elf_sym_hashes (abfd);
if (htab->elf.dynobj == NULL)
htab->elf.dynobj = abfd;
for (rel = relocs; rel < relocs + sec->reloc_count; rel++)
{
unsigned int r_type;
unsigned int r_symndx;
struct elf_link_hash_entry *h;
bool is_abs_symbol = false;
r_symndx = ELFNN_R_SYM (rel->r_info);
r_type = ELFNN_R_TYPE (rel->r_info);
if (r_symndx >= NUM_SHDR_ENTRIES (symtab_hdr))
{
(*_bfd_error_handler) (_("%pB: bad symbol index: %d"),
abfd, r_symndx);
return false;
}
if (r_symndx < symtab_hdr->sh_info)
{
/* A local symbol. */
Elf_Internal_Sym *isym = bfd_sym_from_r_symndx (&htab->elf.sym_cache,
abfd, r_symndx);
if (isym == NULL)
return false;
is_abs_symbol = isym->st_shndx == SHN_ABS ? true : false;
/* Check relocation against local STT_GNU_IFUNC symbol. */
if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
{
h = riscv_elf_get_local_sym_hash (htab, abfd, rel, true);
if (h == NULL)
return false;
/* Fake STT_GNU_IFUNC global symbol. */
h->root.root.string = bfd_elf_sym_name (abfd, symtab_hdr,
isym, NULL);
h->type = STT_GNU_IFUNC;
h->def_regular = 1;
h->ref_regular = 1;
h->forced_local = 1;
h->root.type = bfd_link_hash_defined;
}
else
h = NULL;
}
else
{
h = sym_hashes[r_symndx - symtab_hdr->sh_info];
while (h->root.type == bfd_link_hash_indirect
|| h->root.type == bfd_link_hash_warning)
h = (struct elf_link_hash_entry *) h->root.u.i.link;
is_abs_symbol = bfd_is_abs_symbol (&h->root) ? true : false;
}
if (h != NULL)
{
switch (r_type)
{
case R_RISCV_32:
case R_RISCV_64:
case R_RISCV_CALL:
case R_RISCV_CALL_PLT:
case R_RISCV_HI20:
case R_RISCV_GOT_HI20:
case R_RISCV_PCREL_HI20:
/* Create the ifunc sections, iplt and ipltgot, for static
executables. */
if (h->type == STT_GNU_IFUNC
&& !_bfd_elf_create_ifunc_sections (htab->elf.dynobj, info))
return false;
break;
default:
break;
}
/* It is referenced by a non-shared object. */
h->ref_regular = 1;
}
switch (r_type)
{
case R_RISCV_TLS_GD_HI20:
if (!riscv_elf_record_got_reference (abfd, info, h, r_symndx)
|| !riscv_elf_record_tls_type (abfd, h, r_symndx, GOT_TLS_GD))
return false;
break;
case R_RISCV_TLS_GOT_HI20:
if (bfd_link_dll (info))
info->flags |= DF_STATIC_TLS;
if (!riscv_elf_record_got_reference (abfd, info, h, r_symndx)
|| !riscv_elf_record_tls_type (abfd, h, r_symndx, GOT_TLS_IE))
return false;
break;
case R_RISCV_GOT_HI20:
if (!riscv_elf_record_got_reference (abfd, info, h, r_symndx)
|| !riscv_elf_record_tls_type (abfd, h, r_symndx, GOT_NORMAL))
return false;
break;
case R_RISCV_CALL:
case R_RISCV_CALL_PLT:
/* These symbol requires a procedure linkage table entry.
We actually build the entry in adjust_dynamic_symbol,
because these might be a case of linking PIC code without
linking in any dynamic objects, in which case we don't
need to generate a procedure linkage table after all. */
/* If it is a local symbol, then we resolve it directly
without creating a PLT entry. */
if (h == NULL)
continue;
h->needs_plt = 1;
h->plt.refcount += 1;
break;
case R_RISCV_PCREL_HI20:
if (h != NULL
&& h->type == STT_GNU_IFUNC)
{
h->non_got_ref = 1;
h->pointer_equality_needed = 1;
/* We don't use the PCREL_HI20 in the data section,
so we always need the plt when it refers to
ifunc symbol. */
h->plt.refcount += 1;
}
/* The non-preemptible absolute symbol shouldn't be referneced with
pc-relative relocation when generating shared object. However,
PCREL_HI20/LO12 relocs are always bind locally when generating
shared object, so all absolute symbol referenced need to be
disallowed, except they are defined in linker script.
Maybe we should add this check for all pc-relative relocations,
please see pr28789 and pr25749 for details. */
if (bfd_link_pic (info)
/* (h == NULL || SYMBOL_REFERENCES_LOCAL (info, h)) */
&& is_abs_symbol)
{
if (h != NULL && (h)->root.ldscript_def)
/* Disallow the absolute symbol defined in linker script here
will cause the glibc-linux toolchain build failed, so regard
them as pc-relative symbols, just like what x86 did. */
;
else
{
const char *name;
if (h->root.root.string)
name = h->root.root.string;
else
{
Elf_Internal_Sym *sym;
sym = bfd_sym_from_r_symndx (&htab->elf.sym_cache, abfd,
r_symndx);
name = bfd_elf_sym_name (abfd, symtab_hdr, sym, NULL);
}
reloc_howto_type *r_t =
riscv_elf_rtype_to_howto (abfd, r_type);
_bfd_error_handler
(_("%pB: relocation %s against absolute symbol `%s' can "
"not be used when making a shared object"),
abfd, r_t ? r_t->name : _("<unknown>"), name);
bfd_set_error (bfd_error_bad_value);
return false;
}
}
/* Fall through. */
case R_RISCV_JAL:
case R_RISCV_BRANCH:
case R_RISCV_RVC_BRANCH:
case R_RISCV_RVC_JUMP:
/* In shared libraries and pie, these relocs are known
to bind locally. */
if (bfd_link_pic (info))
break;
goto static_reloc;
case R_RISCV_TPREL_HI20:
/* This is not allowed in the pic, but okay in pie. */
if (!bfd_link_executable (info))
return bad_static_reloc (abfd, r_type, h);
if (h != NULL)
riscv_elf_record_tls_type (abfd, h, r_symndx, GOT_TLS_LE);
break;
case R_RISCV_HI20:
if (bfd_link_pic (info))
return bad_static_reloc (abfd, r_type, h);
goto static_reloc;
case R_RISCV_32:
if (ARCH_SIZE > 32
&& bfd_link_pic (info)
&& (sec->flags & SEC_ALLOC) != 0)
{
if (is_abs_symbol)
break;
reloc_howto_type *r_t = riscv_elf_rtype_to_howto (abfd, r_type);
_bfd_error_handler
(_("%pB: relocation %s against non-absolute symbol `%s' can "
"not be used in RVNN when making a shared object"),
abfd, r_t ? r_t->name : _("<unknown>"),
h != NULL ? h->root.root.string : "a local symbol");
bfd_set_error (bfd_error_bad_value);
return false;
}
goto static_reloc;
case R_RISCV_COPY:
case R_RISCV_JUMP_SLOT:
case R_RISCV_RELATIVE:
case R_RISCV_64:
/* Fall through. */
static_reloc:
if (h != NULL
&& (!bfd_link_pic (info)
|| h->type == STT_GNU_IFUNC))
{
/* This reloc might not bind locally. */
h->non_got_ref = 1;
h->pointer_equality_needed = 1;
if (!h->def_regular
|| (sec->flags & (SEC_CODE | SEC_READONLY)) != 0)
{
/* We may need a .plt entry if the symbol is a function
defined in a shared lib or is a function referenced
from the code or read-only section. */
h->plt.refcount += 1;
}
}
reloc_howto_type *r = riscv_elf_rtype_to_howto (abfd, r_type);
if (RISCV_NEED_DYNAMIC_RELOC (r->pc_relative, info, h, sec))