-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
elf32-xtensa.c
11446 lines (9583 loc) · 321 KB
/
elf32-xtensa.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
/* Xtensa-specific support for 32-bit ELF.
Copyright (C) 2003-2015 Free Software Foundation, Inc.
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; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
02110-1301, USA. */
#include "sysdep.h"
#include "disas-asm.h"
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include "bfdlink.h"
//#include "libbfd.h"
#include "elf-bfd.h"
#include "elf/xtensa.h"
//#include "splay-tree.h"
#include "xtensa-isa.h"
//#include "xtensa-config.h"
#define XTENSA_NO_NOP_REMOVAL 0
/* Local helper functions. */
#if 0
static bfd_boolean add_extra_plt_sections (struct bfd_link_info *, int);
static char *vsprint_msg (const char *, const char *, int, ...) ATTRIBUTE_PRINTF(2,4);
static bfd_reloc_status_type bfd_elf_xtensa_reloc
(bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
static bfd_boolean do_fix_for_relocatable_link
(Elf_Internal_Rela *, bfd *, asection *, bfd_byte *);
static void do_fix_for_final_link
(Elf_Internal_Rela *, bfd *, asection *, bfd_byte *, bfd_vma *);
/* Local functions to handle Xtensa configurability. */
static bfd_boolean is_indirect_call_opcode (xtensa_opcode);
static bfd_boolean is_direct_call_opcode (xtensa_opcode);
static bfd_boolean is_windowed_call_opcode (xtensa_opcode);
static xtensa_opcode get_const16_opcode (void);
static xtensa_opcode get_l32r_opcode (void);
static bfd_vma l32r_offset (bfd_vma, bfd_vma);
static int get_relocation_opnd (xtensa_opcode, int);
static int get_relocation_slot (int);
static xtensa_opcode get_relocation_opcode
(bfd *, asection *, bfd_byte *, Elf_Internal_Rela *);
static bfd_boolean is_l32r_relocation
(bfd *, asection *, bfd_byte *, Elf_Internal_Rela *);
static bfd_boolean is_alt_relocation (int);
static bfd_boolean is_operand_relocation (int);
static bfd_size_type insn_decode_len
(bfd_byte *, bfd_size_type, bfd_size_type);
static xtensa_opcode insn_decode_opcode
(bfd_byte *, bfd_size_type, bfd_size_type, int);
static bfd_boolean check_branch_target_aligned
(bfd_byte *, bfd_size_type, bfd_vma, bfd_vma);
static bfd_boolean check_loop_aligned
(bfd_byte *, bfd_size_type, bfd_vma, bfd_vma);
static bfd_boolean check_branch_target_aligned_address (bfd_vma, int);
static bfd_size_type get_asm_simplify_size
(bfd_byte *, bfd_size_type, bfd_size_type);
/* Functions for link-time code simplifications. */
static bfd_reloc_status_type elf_xtensa_do_asm_simplify
(bfd_byte *, bfd_vma, bfd_vma, char **);
static bfd_reloc_status_type contract_asm_expansion
(bfd_byte *, bfd_vma, Elf_Internal_Rela *, char **);
static xtensa_opcode swap_callx_for_call_opcode (xtensa_opcode);
static xtensa_opcode get_expanded_call_opcode (bfd_byte *, int, bfd_boolean *);
/* Access to internal relocations, section contents and symbols. */
static Elf_Internal_Rela *retrieve_internal_relocs
(bfd *, asection *, bfd_boolean);
static void pin_internal_relocs (asection *, Elf_Internal_Rela *);
static void release_internal_relocs (asection *, Elf_Internal_Rela *);
static bfd_byte *retrieve_contents (bfd *, asection *, bfd_boolean);
static void pin_contents (asection *, bfd_byte *);
static void release_contents (asection *, bfd_byte *);
static Elf_Internal_Sym *retrieve_local_syms (bfd *);
/* Miscellaneous utility functions. */
static asection *elf_xtensa_get_plt_section (struct bfd_link_info *, int);
static asection *elf_xtensa_get_gotplt_section (struct bfd_link_info *, int);
static asection *get_elf_r_symndx_section (bfd *, unsigned long);
static struct elf_link_hash_entry *get_elf_r_symndx_hash_entry
(bfd *, unsigned long);
static bfd_vma get_elf_r_symndx_offset (bfd *, unsigned long);
static bfd_boolean is_reloc_sym_weak (bfd *, Elf_Internal_Rela *);
static bfd_boolean pcrel_reloc_fits (xtensa_opcode, int, bfd_vma, bfd_vma);
static bfd_boolean xtensa_is_property_section (asection *);
static bfd_boolean xtensa_is_insntable_section (asection *);
static bfd_boolean xtensa_is_littable_section (asection *);
static bfd_boolean xtensa_is_proptable_section (asection *);
static int internal_reloc_compare (const void *, const void *);
static int internal_reloc_matches (const void *, const void *);
static asection *xtensa_get_property_section (asection *, const char *);
extern asection *xtensa_make_property_section (asection *, const char *);
static flagword xtensa_get_property_predef_flags (asection *);
/* Other functions called directly by the linker. */
typedef void (*deps_callback_t)
(asection *, bfd_vma, asection *, bfd_vma, void *);
extern bfd_boolean xtensa_callback_required_dependence
(bfd *, asection *, struct bfd_link_info *, deps_callback_t, void *);
/* Globally visible flag for choosing size optimization of NOP removal
instead of branch-target-aware minimization for NOP removal.
When nonzero, narrow all instructions and remove all NOPs possible
around longcall expansions. */
int elf32xtensa_size_opt;
/* The "new_section_hook" is used to set up a per-section
"xtensa_relax_info" data structure with additional information used
during relaxation. */
typedef struct xtensa_relax_info_struct xtensa_relax_info;
/* The GNU tools do not easily allow extending interfaces to pass around
the pointer to the Xtensa ISA information, so instead we add a global
variable here (in BFD) that can be used by any of the tools that need
this information. */
#endif
xtensa_isa xtensa_default_isa;
// xtensa_default_isa = xtensa_isa_init (0, 0);
int
filename_cmp (const char *s1, const char *s2)
{
#if !defined(HAVE_DOS_BASED_FILE_SYSTEM) \
&& !defined(HAVE_CASE_INSENSITIVE_FILE_SYSTEM)
return strcmp(s1, s2);
#else
for (;;)
{
int c1 = *s1;
int c2 = *s2;
#if defined (HAVE_CASE_INSENSITIVE_FILE_SYSTEM)
c1 = TOLOWER (c1);
c2 = TOLOWER (c2);
#endif
#if defined (HAVE_DOS_BASED_FILE_SYSTEM)
/* On DOS-based file systems, the '/' and the '\' are equivalent. */
if (c1 == '/')
c1 = '\\';
if (c2 == '/')
c2 = '\\';
#endif
if (c1 != c2)
return (c1 - c2);
if (c1 == '\0')
return 0;
s1++;
s2++;
}
#endif
}
#if 0
/* When this is true, relocations may have been modified to refer to
symbols from other input files. The per-section list of "fix"
records needs to be checked when resolving relocations. */
static bfd_boolean relaxing_section = FALSE;
/* When this is true, during final links, literals that cannot be
coalesced and their relocations may be moved to other sections. */
int elf32xtensa_no_literal_movement = 1;
/* Rename one of the generic section flags to better document how it
is used here. */
/* Whether relocations have been processed. */
#define reloc_done sec_flg0
static reloc_howto_type elf_howto_table[] =
{
HOWTO (R_XTENSA_NONE, 0, 3, 0, FALSE, 0, complain_overflow_dont,
bfd_elf_xtensa_reloc, "R_XTENSA_NONE",
FALSE, 0, 0, FALSE),
HOWTO (R_XTENSA_32, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
bfd_elf_xtensa_reloc, "R_XTENSA_32",
TRUE, 0xffffffff, 0xffffffff, FALSE),
/* Replace a 32-bit value with a value from the runtime linker (only
used by linker-generated stub functions). The r_addend value is
special: 1 means to substitute a pointer to the runtime linker's
dynamic resolver function; 2 means to substitute the link map for
the shared object. */
HOWTO (R_XTENSA_RTLD, 0, 2, 32, FALSE, 0, complain_overflow_dont,
NULL, "R_XTENSA_RTLD", FALSE, 0, 0, FALSE),
HOWTO (R_XTENSA_GLOB_DAT, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
bfd_elf_generic_reloc, "R_XTENSA_GLOB_DAT",
FALSE, 0, 0xffffffff, FALSE),
HOWTO (R_XTENSA_JMP_SLOT, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
bfd_elf_generic_reloc, "R_XTENSA_JMP_SLOT",
FALSE, 0, 0xffffffff, FALSE),
HOWTO (R_XTENSA_RELATIVE, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
bfd_elf_generic_reloc, "R_XTENSA_RELATIVE",
FALSE, 0, 0xffffffff, FALSE),
HOWTO (R_XTENSA_PLT, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
bfd_elf_xtensa_reloc, "R_XTENSA_PLT",
FALSE, 0, 0xffffffff, FALSE),
EMPTY_HOWTO (7),
/* Old relocations for backward compatibility. */
HOWTO (R_XTENSA_OP0, 0, 0, 0, TRUE, 0, complain_overflow_dont,
bfd_elf_xtensa_reloc, "R_XTENSA_OP0", FALSE, 0, 0, TRUE),
HOWTO (R_XTENSA_OP1, 0, 0, 0, TRUE, 0, complain_overflow_dont,
bfd_elf_xtensa_reloc, "R_XTENSA_OP1", FALSE, 0, 0, TRUE),
HOWTO (R_XTENSA_OP2, 0, 0, 0, TRUE, 0, complain_overflow_dont,
bfd_elf_xtensa_reloc, "R_XTENSA_OP2", FALSE, 0, 0, TRUE),
/* Assembly auto-expansion. */
HOWTO (R_XTENSA_ASM_EXPAND, 0, 0, 0, TRUE, 0, complain_overflow_dont,
bfd_elf_xtensa_reloc, "R_XTENSA_ASM_EXPAND", FALSE, 0, 0, TRUE),
/* Relax assembly auto-expansion. */
HOWTO (R_XTENSA_ASM_SIMPLIFY, 0, 0, 0, TRUE, 0, complain_overflow_dont,
bfd_elf_xtensa_reloc, "R_XTENSA_ASM_SIMPLIFY", FALSE, 0, 0, TRUE),
EMPTY_HOWTO (13),
HOWTO (R_XTENSA_32_PCREL, 0, 2, 32, TRUE, 0, complain_overflow_bitfield,
bfd_elf_xtensa_reloc, "R_XTENSA_32_PCREL",
FALSE, 0, 0xffffffff, TRUE),
/* GNU extension to record C++ vtable hierarchy. */
HOWTO (R_XTENSA_GNU_VTINHERIT, 0, 2, 0, FALSE, 0, complain_overflow_dont,
NULL, "R_XTENSA_GNU_VTINHERIT",
FALSE, 0, 0, FALSE),
/* GNU extension to record C++ vtable member usage. */
HOWTO (R_XTENSA_GNU_VTENTRY, 0, 2, 0, FALSE, 0, complain_overflow_dont,
_bfd_elf_rel_vtable_reloc_fn, "R_XTENSA_GNU_VTENTRY",
FALSE, 0, 0, FALSE),
/* Relocations for supporting difference of symbols. */
HOWTO (R_XTENSA_DIFF8, 0, 0, 8, FALSE, 0, complain_overflow_signed,
bfd_elf_xtensa_reloc, "R_XTENSA_DIFF8", FALSE, 0, 0xff, FALSE),
HOWTO (R_XTENSA_DIFF16, 0, 1, 16, FALSE, 0, complain_overflow_signed,
bfd_elf_xtensa_reloc, "R_XTENSA_DIFF16", FALSE, 0, 0xffff, FALSE),
HOWTO (R_XTENSA_DIFF32, 0, 2, 32, FALSE, 0, complain_overflow_signed,
bfd_elf_xtensa_reloc, "R_XTENSA_DIFF32", FALSE, 0, 0xffffffff, FALSE),
/* General immediate operand relocations. */
HOWTO (R_XTENSA_SLOT0_OP, 0, 0, 0, TRUE, 0, complain_overflow_dont,
bfd_elf_xtensa_reloc, "R_XTENSA_SLOT0_OP", FALSE, 0, 0, TRUE),
HOWTO (R_XTENSA_SLOT1_OP, 0, 0, 0, TRUE, 0, complain_overflow_dont,
bfd_elf_xtensa_reloc, "R_XTENSA_SLOT1_OP", FALSE, 0, 0, TRUE),
HOWTO (R_XTENSA_SLOT2_OP, 0, 0, 0, TRUE, 0, complain_overflow_dont,
bfd_elf_xtensa_reloc, "R_XTENSA_SLOT2_OP", FALSE, 0, 0, TRUE),
HOWTO (R_XTENSA_SLOT3_OP, 0, 0, 0, TRUE, 0, complain_overflow_dont,
bfd_elf_xtensa_reloc, "R_XTENSA_SLOT3_OP", FALSE, 0, 0, TRUE),
HOWTO (R_XTENSA_SLOT4_OP, 0, 0, 0, TRUE, 0, complain_overflow_dont,
bfd_elf_xtensa_reloc, "R_XTENSA_SLOT4_OP", FALSE, 0, 0, TRUE),
HOWTO (R_XTENSA_SLOT5_OP, 0, 0, 0, TRUE, 0, complain_overflow_dont,
bfd_elf_xtensa_reloc, "R_XTENSA_SLOT5_OP", FALSE, 0, 0, TRUE),
HOWTO (R_XTENSA_SLOT6_OP, 0, 0, 0, TRUE, 0, complain_overflow_dont,
bfd_elf_xtensa_reloc, "R_XTENSA_SLOT6_OP", FALSE, 0, 0, TRUE),
HOWTO (R_XTENSA_SLOT7_OP, 0, 0, 0, TRUE, 0, complain_overflow_dont,
bfd_elf_xtensa_reloc, "R_XTENSA_SLOT7_OP", FALSE, 0, 0, TRUE),
HOWTO (R_XTENSA_SLOT8_OP, 0, 0, 0, TRUE, 0, complain_overflow_dont,
bfd_elf_xtensa_reloc, "R_XTENSA_SLOT8_OP", FALSE, 0, 0, TRUE),
HOWTO (R_XTENSA_SLOT9_OP, 0, 0, 0, TRUE, 0, complain_overflow_dont,
bfd_elf_xtensa_reloc, "R_XTENSA_SLOT9_OP", FALSE, 0, 0, TRUE),
HOWTO (R_XTENSA_SLOT10_OP, 0, 0, 0, TRUE, 0, complain_overflow_dont,
bfd_elf_xtensa_reloc, "R_XTENSA_SLOT10_OP", FALSE, 0, 0, TRUE),
HOWTO (R_XTENSA_SLOT11_OP, 0, 0, 0, TRUE, 0, complain_overflow_dont,
bfd_elf_xtensa_reloc, "R_XTENSA_SLOT11_OP", FALSE, 0, 0, TRUE),
HOWTO (R_XTENSA_SLOT12_OP, 0, 0, 0, TRUE, 0, complain_overflow_dont,
bfd_elf_xtensa_reloc, "R_XTENSA_SLOT12_OP", FALSE, 0, 0, TRUE),
HOWTO (R_XTENSA_SLOT13_OP, 0, 0, 0, TRUE, 0, complain_overflow_dont,
bfd_elf_xtensa_reloc, "R_XTENSA_SLOT13_OP", FALSE, 0, 0, TRUE),
HOWTO (R_XTENSA_SLOT14_OP, 0, 0, 0, TRUE, 0, complain_overflow_dont,
bfd_elf_xtensa_reloc, "R_XTENSA_SLOT14_OP", FALSE, 0, 0, TRUE),
/* "Alternate" relocations. The meaning of these is opcode-specific. */
HOWTO (R_XTENSA_SLOT0_ALT, 0, 0, 0, TRUE, 0, complain_overflow_dont,
bfd_elf_xtensa_reloc, "R_XTENSA_SLOT0_ALT", FALSE, 0, 0, TRUE),
HOWTO (R_XTENSA_SLOT1_ALT, 0, 0, 0, TRUE, 0, complain_overflow_dont,
bfd_elf_xtensa_reloc, "R_XTENSA_SLOT1_ALT", FALSE, 0, 0, TRUE),
HOWTO (R_XTENSA_SLOT2_ALT, 0, 0, 0, TRUE, 0, complain_overflow_dont,
bfd_elf_xtensa_reloc, "R_XTENSA_SLOT2_ALT", FALSE, 0, 0, TRUE),
HOWTO (R_XTENSA_SLOT3_ALT, 0, 0, 0, TRUE, 0, complain_overflow_dont,
bfd_elf_xtensa_reloc, "R_XTENSA_SLOT3_ALT", FALSE, 0, 0, TRUE),
HOWTO (R_XTENSA_SLOT4_ALT, 0, 0, 0, TRUE, 0, complain_overflow_dont,
bfd_elf_xtensa_reloc, "R_XTENSA_SLOT4_ALT", FALSE, 0, 0, TRUE),
HOWTO (R_XTENSA_SLOT5_ALT, 0, 0, 0, TRUE, 0, complain_overflow_dont,
bfd_elf_xtensa_reloc, "R_XTENSA_SLOT5_ALT", FALSE, 0, 0, TRUE),
HOWTO (R_XTENSA_SLOT6_ALT, 0, 0, 0, TRUE, 0, complain_overflow_dont,
bfd_elf_xtensa_reloc, "R_XTENSA_SLOT6_ALT", FALSE, 0, 0, TRUE),
HOWTO (R_XTENSA_SLOT7_ALT, 0, 0, 0, TRUE, 0, complain_overflow_dont,
bfd_elf_xtensa_reloc, "R_XTENSA_SLOT7_ALT", FALSE, 0, 0, TRUE),
HOWTO (R_XTENSA_SLOT8_ALT, 0, 0, 0, TRUE, 0, complain_overflow_dont,
bfd_elf_xtensa_reloc, "R_XTENSA_SLOT8_ALT", FALSE, 0, 0, TRUE),
HOWTO (R_XTENSA_SLOT9_ALT, 0, 0, 0, TRUE, 0, complain_overflow_dont,
bfd_elf_xtensa_reloc, "R_XTENSA_SLOT9_ALT", FALSE, 0, 0, TRUE),
HOWTO (R_XTENSA_SLOT10_ALT, 0, 0, 0, TRUE, 0, complain_overflow_dont,
bfd_elf_xtensa_reloc, "R_XTENSA_SLOT10_ALT", FALSE, 0, 0, TRUE),
HOWTO (R_XTENSA_SLOT11_ALT, 0, 0, 0, TRUE, 0, complain_overflow_dont,
bfd_elf_xtensa_reloc, "R_XTENSA_SLOT11_ALT", FALSE, 0, 0, TRUE),
HOWTO (R_XTENSA_SLOT12_ALT, 0, 0, 0, TRUE, 0, complain_overflow_dont,
bfd_elf_xtensa_reloc, "R_XTENSA_SLOT12_ALT", FALSE, 0, 0, TRUE),
HOWTO (R_XTENSA_SLOT13_ALT, 0, 0, 0, TRUE, 0, complain_overflow_dont,
bfd_elf_xtensa_reloc, "R_XTENSA_SLOT13_ALT", FALSE, 0, 0, TRUE),
HOWTO (R_XTENSA_SLOT14_ALT, 0, 0, 0, TRUE, 0, complain_overflow_dont,
bfd_elf_xtensa_reloc, "R_XTENSA_SLOT14_ALT", FALSE, 0, 0, TRUE),
/* TLS relocations. */
HOWTO (R_XTENSA_TLSDESC_FN, 0, 2, 32, FALSE, 0, complain_overflow_dont,
bfd_elf_xtensa_reloc, "R_XTENSA_TLSDESC_FN",
FALSE, 0, 0xffffffff, FALSE),
HOWTO (R_XTENSA_TLSDESC_ARG, 0, 2, 32, FALSE, 0, complain_overflow_dont,
bfd_elf_xtensa_reloc, "R_XTENSA_TLSDESC_ARG",
FALSE, 0, 0xffffffff, FALSE),
HOWTO (R_XTENSA_TLS_DTPOFF, 0, 2, 32, FALSE, 0, complain_overflow_dont,
bfd_elf_xtensa_reloc, "R_XTENSA_TLS_DTPOFF",
FALSE, 0, 0xffffffff, FALSE),
HOWTO (R_XTENSA_TLS_TPOFF, 0, 2, 32, FALSE, 0, complain_overflow_dont,
bfd_elf_xtensa_reloc, "R_XTENSA_TLS_TPOFF",
FALSE, 0, 0xffffffff, FALSE),
HOWTO (R_XTENSA_TLS_FUNC, 0, 0, 0, FALSE, 0, complain_overflow_dont,
bfd_elf_xtensa_reloc, "R_XTENSA_TLS_FUNC",
FALSE, 0, 0, FALSE),
HOWTO (R_XTENSA_TLS_ARG, 0, 0, 0, FALSE, 0, complain_overflow_dont,
bfd_elf_xtensa_reloc, "R_XTENSA_TLS_ARG",
FALSE, 0, 0, FALSE),
HOWTO (R_XTENSA_TLS_CALL, 0, 0, 0, FALSE, 0, complain_overflow_dont,
bfd_elf_xtensa_reloc, "R_XTENSA_TLS_CALL",
FALSE, 0, 0, FALSE),
};
#if DEBUG_GEN_RELOC
#define TRACE(str) \
fprintf (stderr, "Xtensa bfd reloc lookup %d (%s)\n", code, str)
#else
#define TRACE(str)
#endif
static reloc_howto_type *
elf_xtensa_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
bfd_reloc_code_real_type code)
{
switch (code)
{
case BFD_RELOC_NONE:
TRACE ("BFD_RELOC_NONE");
return &elf_howto_table[(unsigned) R_XTENSA_NONE ];
case BFD_RELOC_32:
TRACE ("BFD_RELOC_32");
return &elf_howto_table[(unsigned) R_XTENSA_32 ];
case BFD_RELOC_32_PCREL:
TRACE ("BFD_RELOC_32_PCREL");
return &elf_howto_table[(unsigned) R_XTENSA_32_PCREL ];
case BFD_RELOC_XTENSA_DIFF8:
TRACE ("BFD_RELOC_XTENSA_DIFF8");
return &elf_howto_table[(unsigned) R_XTENSA_DIFF8 ];
case BFD_RELOC_XTENSA_DIFF16:
TRACE ("BFD_RELOC_XTENSA_DIFF16");
return &elf_howto_table[(unsigned) R_XTENSA_DIFF16 ];
case BFD_RELOC_XTENSA_DIFF32:
TRACE ("BFD_RELOC_XTENSA_DIFF32");
return &elf_howto_table[(unsigned) R_XTENSA_DIFF32 ];
case BFD_RELOC_XTENSA_RTLD:
TRACE ("BFD_RELOC_XTENSA_RTLD");
return &elf_howto_table[(unsigned) R_XTENSA_RTLD ];
case BFD_RELOC_XTENSA_GLOB_DAT:
TRACE ("BFD_RELOC_XTENSA_GLOB_DAT");
return &elf_howto_table[(unsigned) R_XTENSA_GLOB_DAT ];
case BFD_RELOC_XTENSA_JMP_SLOT:
TRACE ("BFD_RELOC_XTENSA_JMP_SLOT");
return &elf_howto_table[(unsigned) R_XTENSA_JMP_SLOT ];
case BFD_RELOC_XTENSA_RELATIVE:
TRACE ("BFD_RELOC_XTENSA_RELATIVE");
return &elf_howto_table[(unsigned) R_XTENSA_RELATIVE ];
case BFD_RELOC_XTENSA_PLT:
TRACE ("BFD_RELOC_XTENSA_PLT");
return &elf_howto_table[(unsigned) R_XTENSA_PLT ];
case BFD_RELOC_XTENSA_OP0:
TRACE ("BFD_RELOC_XTENSA_OP0");
return &elf_howto_table[(unsigned) R_XTENSA_OP0 ];
case BFD_RELOC_XTENSA_OP1:
TRACE ("BFD_RELOC_XTENSA_OP1");
return &elf_howto_table[(unsigned) R_XTENSA_OP1 ];
case BFD_RELOC_XTENSA_OP2:
TRACE ("BFD_RELOC_XTENSA_OP2");
return &elf_howto_table[(unsigned) R_XTENSA_OP2 ];
case BFD_RELOC_XTENSA_ASM_EXPAND:
TRACE ("BFD_RELOC_XTENSA_ASM_EXPAND");
return &elf_howto_table[(unsigned) R_XTENSA_ASM_EXPAND ];
case BFD_RELOC_XTENSA_ASM_SIMPLIFY:
TRACE ("BFD_RELOC_XTENSA_ASM_SIMPLIFY");
return &elf_howto_table[(unsigned) R_XTENSA_ASM_SIMPLIFY ];
case BFD_RELOC_VTABLE_INHERIT:
TRACE ("BFD_RELOC_VTABLE_INHERIT");
return &elf_howto_table[(unsigned) R_XTENSA_GNU_VTINHERIT ];
case BFD_RELOC_VTABLE_ENTRY:
TRACE ("BFD_RELOC_VTABLE_ENTRY");
return &elf_howto_table[(unsigned) R_XTENSA_GNU_VTENTRY ];
case BFD_RELOC_XTENSA_TLSDESC_FN:
TRACE ("BFD_RELOC_XTENSA_TLSDESC_FN");
return &elf_howto_table[(unsigned) R_XTENSA_TLSDESC_FN ];
case BFD_RELOC_XTENSA_TLSDESC_ARG:
TRACE ("BFD_RELOC_XTENSA_TLSDESC_ARG");
return &elf_howto_table[(unsigned) R_XTENSA_TLSDESC_ARG ];
case BFD_RELOC_XTENSA_TLS_DTPOFF:
TRACE ("BFD_RELOC_XTENSA_TLS_DTPOFF");
return &elf_howto_table[(unsigned) R_XTENSA_TLS_DTPOFF ];
case BFD_RELOC_XTENSA_TLS_TPOFF:
TRACE ("BFD_RELOC_XTENSA_TLS_TPOFF");
return &elf_howto_table[(unsigned) R_XTENSA_TLS_TPOFF ];
case BFD_RELOC_XTENSA_TLS_FUNC:
TRACE ("BFD_RELOC_XTENSA_TLS_FUNC");
return &elf_howto_table[(unsigned) R_XTENSA_TLS_FUNC ];
case BFD_RELOC_XTENSA_TLS_ARG:
TRACE ("BFD_RELOC_XTENSA_TLS_ARG");
return &elf_howto_table[(unsigned) R_XTENSA_TLS_ARG ];
case BFD_RELOC_XTENSA_TLS_CALL:
TRACE ("BFD_RELOC_XTENSA_TLS_CALL");
return &elf_howto_table[(unsigned) R_XTENSA_TLS_CALL ];
default:
if (code >= BFD_RELOC_XTENSA_SLOT0_OP
&& code <= BFD_RELOC_XTENSA_SLOT14_OP)
{
unsigned n = (R_XTENSA_SLOT0_OP +
(code - BFD_RELOC_XTENSA_SLOT0_OP));
return &elf_howto_table[n];
}
if (code >= BFD_RELOC_XTENSA_SLOT0_ALT
&& code <= BFD_RELOC_XTENSA_SLOT14_ALT)
{
unsigned n = (R_XTENSA_SLOT0_ALT +
(code - BFD_RELOC_XTENSA_SLOT0_ALT));
return &elf_howto_table[n];
}
break;
}
TRACE ("Unknown");
return NULL;
}
static reloc_howto_type *
elf_xtensa_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
const char *r_name)
{
unsigned int i;
for (i = 0; i < sizeof (elf_howto_table) / sizeof (elf_howto_table[0]); i++)
if (elf_howto_table[i].name != NULL
&& r_str_casecmp (elf_howto_table[i].name, r_name) == 0)
return &elf_howto_table[i];
return NULL;
}
/* Given an ELF "rela" relocation, find the corresponding howto and record
it in the BFD internal arelent representation of the relocation. */
static void
elf_xtensa_info_to_howto_rela (bfd *abfd ATTRIBUTE_UNUSED,
arelent *cache_ptr,
Elf_Internal_Rela *dst)
{
unsigned int r_type = ELF32_R_TYPE (dst->r_info);
if (r_type >= (unsigned int) R_XTENSA_max)
{
_bfd_error_handler (_("%B: invalid XTENSA reloc number: %d"), abfd, r_type);
r_type = 0;
}
cache_ptr->howto = &elf_howto_table[r_type];
}
/* Functions for the Xtensa ELF linker. */
/* The name of the dynamic interpreter. This is put in the .interp
section. */
#define ELF_DYNAMIC_INTERPRETER "/lib/ld.so"
/* The size in bytes of an entry in the procedure linkage table.
(This does _not_ include the space for the literals associated with
the PLT entry.) */
#define PLT_ENTRY_SIZE 16
/* For _really_ large PLTs, we may need to alternate between literals
and code to keep the literals within the 256K range of the L32R
instructions in the code. It's unlikely that anyone would ever need
such a big PLT, but an arbitrary limit on the PLT size would be bad.
Thus, we split the PLT into chunks. Since there's very little
overhead (2 extra literals) for each chunk, the chunk size is kept
small so that the code for handling multiple chunks get used and
tested regularly. With 254 entries, there are 1K of literals for
each chunk, and that seems like a nice round number. */
#define PLT_ENTRIES_PER_CHUNK 254
/* PLT entries are actually used as stub functions for lazy symbol
resolution. Once the symbol is resolved, the stub function is never
invoked. Note: the 32-byte frame size used here cannot be changed
without a corresponding change in the runtime linker. */
static const bfd_byte elf_xtensa_be_plt_entry[PLT_ENTRY_SIZE] =
{
0x6c, 0x10, 0x04, /* entry sp, 32 */
0x18, 0x00, 0x00, /* l32r a8, [got entry for rtld's resolver] */
0x1a, 0x00, 0x00, /* l32r a10, [got entry for rtld's link map] */
0x1b, 0x00, 0x00, /* l32r a11, [literal for reloc index] */
0x0a, 0x80, 0x00, /* jx a8 */
0 /* unused */
};
static const bfd_byte elf_xtensa_le_plt_entry[PLT_ENTRY_SIZE] =
{
0x36, 0x41, 0x00, /* entry sp, 32 */
0x81, 0x00, 0x00, /* l32r a8, [got entry for rtld's resolver] */
0xa1, 0x00, 0x00, /* l32r a10, [got entry for rtld's link map] */
0xb1, 0x00, 0x00, /* l32r a11, [literal for reloc index] */
0xa0, 0x08, 0x00, /* jx a8 */
0 /* unused */
};
/* The size of the thread control block. */
#define TCB_SIZE 8
struct elf_xtensa_link_hash_entry
{
struct elf_link_hash_entry elf;
bfd_signed_vma tlsfunc_refcount;
#define GOT_UNKNOWN 0
#define GOT_NORMAL 1
#define GOT_TLS_GD 2 /* global or local dynamic */
#define GOT_TLS_IE 4 /* initial or local exec */
#define GOT_TLS_ANY (GOT_TLS_GD | GOT_TLS_IE)
unsigned char tls_type;
};
#define elf_xtensa_hash_entry(ent) ((struct elf_xtensa_link_hash_entry *)(ent))
struct elf_xtensa_obj_tdata
{
struct elf_obj_tdata root;
/* tls_type for each local got entry. */
char *local_got_tls_type;
bfd_signed_vma *local_tlsfunc_refcounts;
};
#define elf_xtensa_tdata(abfd) \
((struct elf_xtensa_obj_tdata *) (abfd)->tdata.any)
#define elf_xtensa_local_got_tls_type(abfd) \
(elf_xtensa_tdata (abfd)->local_got_tls_type)
#define elf_xtensa_local_tlsfunc_refcounts(abfd) \
(elf_xtensa_tdata (abfd)->local_tlsfunc_refcounts)
#define is_xtensa_elf(bfd) \
(bfd_get_flavour (bfd) == bfd_target_elf_flavour \
&& elf_tdata (bfd) != NULL \
&& elf_object_id (bfd) == XTENSA_ELF_DATA)
static bfd_boolean
elf_xtensa_mkobject (bfd *abfd)
{
return bfd_elf_allocate_object (abfd, sizeof (struct elf_xtensa_obj_tdata),
XTENSA_ELF_DATA);
}
/* Xtensa ELF linker hash table. */
struct elf_xtensa_link_hash_table
{
struct elf_link_hash_table elf;
/* Short-cuts to get to dynamic linker sections. */
asection *sgot;
asection *sgotplt;
asection *srelgot;
asection *splt;
asection *srelplt;
asection *sgotloc;
asection *spltlittbl;
/* Total count of PLT relocations seen during check_relocs.
The actual PLT code must be split into multiple sections and all
the sections have to be created before size_dynamic_sections,
where we figure out the exact number of PLT entries that will be
needed. It is OK if this count is an overestimate, e.g., some
relocations may be removed by GC. */
int plt_reloc_count;
struct elf_xtensa_link_hash_entry *tlsbase;
};
/* Get the Xtensa ELF linker hash table from a link_info structure. */
#define elf_xtensa_hash_table(p) \
(elf_hash_table_id ((struct elf_link_hash_table *) ((p)->hash)) \
== XTENSA_ELF_DATA ? ((struct elf_xtensa_link_hash_table *) ((p)->hash)) : NULL)
/* Create an entry in an Xtensa ELF linker hash table. */
static struct bfd_hash_entry *
elf_xtensa_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 elf_xtensa_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 elf_xtensa_link_hash_entry *eh = elf_xtensa_hash_entry (entry);
eh->tlsfunc_refcount = 0;
eh->tls_type = GOT_UNKNOWN;
}
return entry;
}
/* Create an Xtensa ELF linker hash table. */
static struct bfd_link_hash_table *
elf_xtensa_link_hash_table_create (bfd *abfd)
{
struct elf_link_hash_entry *tlsbase;
struct elf_xtensa_link_hash_table *ret;
bfd_size_type amt = sizeof (struct elf_xtensa_link_hash_table);
ret = bfd_zmalloc (amt);
if (ret == NULL)
return NULL;
if (!_bfd_elf_link_hash_table_init (&ret->elf, abfd,
elf_xtensa_link_hash_newfunc,
sizeof (struct elf_xtensa_link_hash_entry),
XTENSA_ELF_DATA))
{
free (ret);
return NULL;
}
/* Create a hash entry for "_TLS_MODULE_BASE_" to speed up checking
for it later. */
tlsbase = elf_link_hash_lookup (&ret->elf, "_TLS_MODULE_BASE_",
TRUE, FALSE, FALSE);
tlsbase->root.type = bfd_link_hash_new;
tlsbase->root.u.undef.abfd = NULL;
tlsbase->non_elf = 0;
ret->tlsbase = elf_xtensa_hash_entry (tlsbase);
ret->tlsbase->tls_type = GOT_UNKNOWN;
return &ret->elf.root;
}
/* Copy the extra info we tack onto an elf_link_hash_entry. */
static void
elf_xtensa_copy_indirect_symbol (struct bfd_link_info *info,
struct elf_link_hash_entry *dir,
struct elf_link_hash_entry *ind)
{
struct elf_xtensa_link_hash_entry *edir, *eind;
edir = elf_xtensa_hash_entry (dir);
eind = elf_xtensa_hash_entry (ind);
if (ind->root.type == bfd_link_hash_indirect)
{
edir->tlsfunc_refcount += eind->tlsfunc_refcount;
eind->tlsfunc_refcount = 0;
if (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 inline bfd_boolean
elf_xtensa_dynamic_symbol_p (struct elf_link_hash_entry *h,
struct bfd_link_info *info)
{
/* Check if we should do dynamic things to this symbol. The
"ignore_protected" argument need not be set, because Xtensa code
does not require special handling of STV_PROTECTED to make function
pointer comparisons work properly. The PLT addresses are never
used for function pointers. */
return _bfd_elf_dynamic_symbol_p (h, info, 0);
}
static int
property_table_compare (const void *ap, const void *bp)
{
const property_table_entry *a = (const property_table_entry *) ap;
const property_table_entry *b = (const property_table_entry *) bp;
if (a->address == b->address)
{
if (a->size != b->size)
return (a->size - b->size);
if ((a->flags & XTENSA_PROP_ALIGN) != (b->flags & XTENSA_PROP_ALIGN))
return ((b->flags & XTENSA_PROP_ALIGN)
- (a->flags & XTENSA_PROP_ALIGN));
if ((a->flags & XTENSA_PROP_ALIGN)
&& (GET_XTENSA_PROP_ALIGNMENT (a->flags)
!= GET_XTENSA_PROP_ALIGNMENT (b->flags)))
return (GET_XTENSA_PROP_ALIGNMENT (a->flags)
- GET_XTENSA_PROP_ALIGNMENT (b->flags));
if ((a->flags & XTENSA_PROP_UNREACHABLE)
!= (b->flags & XTENSA_PROP_UNREACHABLE))
return ((b->flags & XTENSA_PROP_UNREACHABLE)
- (a->flags & XTENSA_PROP_UNREACHABLE));
return (a->flags - b->flags);
}
return (a->address - b->address);
}
static int
property_table_matches (const void *ap, const void *bp)
{
const property_table_entry *a = (const property_table_entry *) ap;
const property_table_entry *b = (const property_table_entry *) bp;
/* Check if one entry overlaps with the other. */
if ((b->address >= a->address && b->address < (a->address + a->size))
|| (a->address >= b->address && a->address < (b->address + b->size)))
return 0;
return (a->address - b->address);
}
/* Get the literal table or property table entries for the given
section. Sets TABLE_P and returns the number of entries. On
error, returns a negative value. */
static int
xtensa_read_table_entries (bfd *abfd,
asection *section,
property_table_entry **table_p,
const char *sec_name,
bfd_boolean output_addr)
{
asection *table_section;
bfd_size_type table_size = 0;
bfd_byte *table_data;
property_table_entry *blocks;
int blk, block_count;
bfd_size_type num_records;
Elf_Internal_Rela *internal_relocs, *irel, *rel_end;
bfd_vma section_addr, off;
flagword predef_flags;
bfd_size_type table_entry_size, section_limit;
if (!section
|| !(section->flags & SEC_ALLOC)
|| (section->flags & SEC_DEBUGGING))
{
*table_p = NULL;
return 0;
}
table_section = xtensa_get_property_section (section, sec_name);
if (table_section)
table_size = table_section->size;
if (table_size == 0)
{
*table_p = NULL;
return 0;
}
predef_flags = xtensa_get_property_predef_flags (table_section);
table_entry_size = 12;
if (predef_flags)
table_entry_size -= 4;
num_records = table_size / table_entry_size;
table_data = retrieve_contents (abfd, table_section, TRUE);
blocks = (property_table_entry *)
bfd_malloc (num_records * sizeof (property_table_entry));
block_count = 0;
if (output_addr)
section_addr = section->output_section->vma + section->output_offset;
else
section_addr = section->vma;
internal_relocs = retrieve_internal_relocs (abfd, table_section, TRUE);
if (internal_relocs && !table_section->reloc_done)
{
qsort (internal_relocs, table_section->reloc_count,
sizeof (Elf_Internal_Rela), internal_reloc_compare);
irel = internal_relocs;
}
else
irel = NULL;
section_limit = bfd_get_section_limit (abfd, section);
rel_end = internal_relocs + table_section->reloc_count;
for (off = 0; off < table_size; off += table_entry_size)
{
bfd_vma address = bfd_get_32 (abfd, table_data + off);
/* Skip any relocations before the current offset. This should help
avoid confusion caused by unexpected relocations for the preceding
table entry. */
while (irel &&
(irel->r_offset < off
|| (irel->r_offset == off
&& ELF32_R_TYPE (irel->r_info) == R_XTENSA_NONE)))
{
irel += 1;
if (irel >= rel_end)
irel = 0;
}
if (irel && irel->r_offset == off)
{
bfd_vma sym_off;
unsigned long r_symndx = ELF32_R_SYM (irel->r_info);
BFD_ASSERT (ELF32_R_TYPE (irel->r_info) == R_XTENSA_32);
if (get_elf_r_symndx_section (abfd, r_symndx) != section)
continue;
sym_off = get_elf_r_symndx_offset (abfd, r_symndx);
BFD_ASSERT (sym_off == 0);
address += (section_addr + sym_off + irel->r_addend);
}
else
{
if (address < section_addr
|| address >= section_addr + section_limit)
continue;
}
blocks[block_count].address = address;
blocks[block_count].size = bfd_get_32 (abfd, table_data + off + 4);
if (predef_flags)
blocks[block_count].flags = predef_flags;
else
blocks[block_count].flags = bfd_get_32 (abfd, table_data + off + 8);
block_count++;
}
release_contents (table_section, table_data);
release_internal_relocs (table_section, internal_relocs);
if (block_count > 0)
{
/* Now sort them into address order for easy reference. */
qsort (blocks, block_count, sizeof (property_table_entry),
property_table_compare);
/* Check that the table contents are valid. Problems may occur,
for example, if an unrelocated object file is stripped. */
for (blk = 1; blk < block_count; blk++)
{
/* The only circumstance where two entries may legitimately
have the same address is when one of them is a zero-size
placeholder to mark a place where fill can be inserted.
The zero-size entry should come first. */
if (blocks[blk - 1].address == blocks[blk].address &&
blocks[blk - 1].size != 0)
{
(*_bfd_error_handler) (_("%B(%A): invalid property table"),
abfd, section);
bfd_set_error (bfd_error_bad_value);
free (blocks);
return -1;
}
}
}
*table_p = blocks;
return block_count;
}
static property_table_entry *
elf_xtensa_find_property_entry (property_table_entry *property_table,
int property_table_size,
bfd_vma addr)
{
property_table_entry entry;
property_table_entry *rv;
if (property_table_size == 0)
return NULL;
entry.address = addr;
entry.size = 1;
entry.flags = 0;
rv = bsearch (&entry, property_table, property_table_size,
sizeof (property_table_entry), property_table_matches);
return rv;
}
static bfd_boolean
elf_xtensa_in_literal_pool (property_table_entry *lit_table,
int lit_table_size,
bfd_vma addr)
{
if (elf_xtensa_find_property_entry (lit_table, lit_table_size, addr))
return TRUE;
return FALSE;
}