-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmips.cc
12702 lines (11111 loc) · 440 KB
/
mips.cc
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
// mips.cc -- mips target support for gold.
// Copyright (C) 2011-2019 Free Software Foundation, Inc.
// Written by Sasa Stankovic <[email protected]>
// and Aleksandar Simeonov <[email protected]>.
// This file contains borrowed and adapted code from bfd/elfxx-mips.c.
// This file is part of gold.
// 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 "gold.h"
#include <algorithm>
#include <set>
#include <sstream>
#include "demangle.h"
#include "elfcpp.h"
#include "parameters.h"
#include "reloc.h"
#include "mips.h"
#include "object.h"
#include "symtab.h"
#include "layout.h"
#include "output.h"
#include "copy-relocs.h"
#include "target.h"
#include "target-reloc.h"
#include "target-select.h"
#include "tls.h"
#include "errors.h"
#include "gc.h"
#include "attributes.h"
#include "nacl.h"
namespace
{
using namespace gold;
template<int size, bool big_endian>
class Mips_output_data_plt;
template<int size, bool big_endian>
class Mips_output_data_got;
template<int size, bool big_endian>
class Target_mips;
template<int size, bool big_endian>
class Mips_output_section_reginfo;
template<int size, bool big_endian>
class Mips_output_section_options;
template<int size, bool big_endian>
class Mips_output_data_la25_stub;
template<int size, bool big_endian>
class Mips_output_data_mips_stubs;
template<int size>
class Mips_symbol;
template<int size, bool big_endian>
class Mips_got_info;
template<int size, bool big_endian>
class Mips_relobj;
class Mips16_stub_section_base;
template<int size, bool big_endian>
class Mips16_stub_section;
// The ABI says that every symbol used by dynamic relocations must have
// a global GOT entry. Among other things, this provides the dynamic
// linker with a free, directly-indexed cache. The GOT can therefore
// contain symbols that are not referenced by GOT relocations themselves
// (in other words, it may have symbols that are not referenced by things
// like R_MIPS_GOT16 and R_MIPS_GOT_PAGE).
// GOT relocations are less likely to overflow if we put the associated
// GOT entries towards the beginning. We therefore divide the global
// GOT entries into two areas: "normal" and "reloc-only". Entries in
// the first area can be used for both dynamic relocations and GP-relative
// accesses, while those in the "reloc-only" area are for dynamic
// relocations only.
// These GGA_* ("Global GOT Area") values are organised so that lower
// values are more general than higher values. Also, non-GGA_NONE
// values are ordered by the position of the area in the GOT.
enum Global_got_area
{
GGA_NORMAL = 0,
GGA_RELOC_ONLY = 1,
GGA_NONE = 2
};
// The types of GOT entries needed for this platform.
// These values are exposed to the ABI in an incremental link.
// Do not renumber existing values without changing the version
// number of the .gnu_incremental_inputs section.
enum Got_type
{
GOT_TYPE_STANDARD = 0, // GOT entry for a regular symbol
GOT_TYPE_TLS_OFFSET = 1, // GOT entry for TLS offset
GOT_TYPE_TLS_PAIR = 2, // GOT entry for TLS module/offset pair
// GOT entries for multi-GOT. We support up to 1024 GOTs in multi-GOT links.
GOT_TYPE_STANDARD_MULTIGOT = 3,
GOT_TYPE_TLS_OFFSET_MULTIGOT = GOT_TYPE_STANDARD_MULTIGOT + 1024,
GOT_TYPE_TLS_PAIR_MULTIGOT = GOT_TYPE_TLS_OFFSET_MULTIGOT + 1024
};
// TLS type of GOT entry.
enum Got_tls_type
{
GOT_TLS_NONE = 0,
GOT_TLS_GD = 1,
GOT_TLS_LDM = 2,
GOT_TLS_IE = 4
};
// Values found in the r_ssym field of a relocation entry.
enum Special_relocation_symbol
{
RSS_UNDEF = 0, // None - value is zero.
RSS_GP = 1, // Value of GP.
RSS_GP0 = 2, // Value of GP in object being relocated.
RSS_LOC = 3 // Address of location being relocated.
};
// Whether the section is readonly.
static inline bool
is_readonly_section(Output_section* output_section)
{
elfcpp::Elf_Xword section_flags = output_section->flags();
elfcpp::Elf_Word section_type = output_section->type();
if (section_type == elfcpp::SHT_NOBITS)
return false;
if (section_flags & elfcpp::SHF_WRITE)
return false;
return true;
}
// Return TRUE if a relocation of type R_TYPE from OBJECT might
// require an la25 stub. See also local_pic_function, which determines
// whether the destination function ever requires a stub.
template<int size, bool big_endian>
static inline bool
relocation_needs_la25_stub(Mips_relobj<size, big_endian>* object,
unsigned int r_type, bool target_is_16_bit_code)
{
// We specifically ignore branches and jumps from EF_PIC objects,
// where the onus is on the compiler or programmer to perform any
// necessary initialization of $25. Sometimes such initialization
// is unnecessary; for example, -mno-shared functions do not use
// the incoming value of $25, and may therefore be called directly.
if (object->is_pic())
return false;
switch (r_type)
{
case elfcpp::R_MIPS_26:
case elfcpp::R_MIPS_PC16:
case elfcpp::R_MIPS_PC21_S2:
case elfcpp::R_MIPS_PC26_S2:
case elfcpp::R_MICROMIPS_26_S1:
case elfcpp::R_MICROMIPS_PC7_S1:
case elfcpp::R_MICROMIPS_PC10_S1:
case elfcpp::R_MICROMIPS_PC16_S1:
case elfcpp::R_MICROMIPS_PC23_S2:
return true;
case elfcpp::R_MIPS16_26:
return !target_is_16_bit_code;
default:
return false;
}
}
// Return true if SYM is a locally-defined PIC function, in the sense
// that it or its fn_stub might need $25 to be valid on entry.
// Note that MIPS16 functions set up $gp using PC-relative instructions,
// so they themselves never need $25 to be valid. Only non-MIPS16
// entry points are of interest here.
template<int size, bool big_endian>
static inline bool
local_pic_function(Mips_symbol<size>* sym)
{
bool def_regular = (sym->source() == Symbol::FROM_OBJECT
&& !sym->object()->is_dynamic()
&& !sym->is_undefined());
if (sym->is_defined() && def_regular)
{
Mips_relobj<size, big_endian>* object =
static_cast<Mips_relobj<size, big_endian>*>(sym->object());
if ((object->is_pic() || sym->is_pic())
&& (!sym->is_mips16()
|| (sym->has_mips16_fn_stub() && sym->need_fn_stub())))
return true;
}
return false;
}
static inline bool
hi16_reloc(int r_type)
{
return (r_type == elfcpp::R_MIPS_HI16
|| r_type == elfcpp::R_MIPS16_HI16
|| r_type == elfcpp::R_MICROMIPS_HI16
|| r_type == elfcpp::R_MIPS_PCHI16);
}
static inline bool
lo16_reloc(int r_type)
{
return (r_type == elfcpp::R_MIPS_LO16
|| r_type == elfcpp::R_MIPS16_LO16
|| r_type == elfcpp::R_MICROMIPS_LO16
|| r_type == elfcpp::R_MIPS_PCLO16);
}
static inline bool
got16_reloc(unsigned int r_type)
{
return (r_type == elfcpp::R_MIPS_GOT16
|| r_type == elfcpp::R_MIPS16_GOT16
|| r_type == elfcpp::R_MICROMIPS_GOT16);
}
static inline bool
call_lo16_reloc(unsigned int r_type)
{
return (r_type == elfcpp::R_MIPS_CALL_LO16
|| r_type == elfcpp::R_MICROMIPS_CALL_LO16);
}
static inline bool
got_lo16_reloc(unsigned int r_type)
{
return (r_type == elfcpp::R_MIPS_GOT_LO16
|| r_type == elfcpp::R_MICROMIPS_GOT_LO16);
}
static inline bool
eh_reloc(unsigned int r_type)
{
return (r_type == elfcpp::R_MIPS_EH);
}
static inline bool
got_disp_reloc(unsigned int r_type)
{
return (r_type == elfcpp::R_MIPS_GOT_DISP
|| r_type == elfcpp::R_MICROMIPS_GOT_DISP);
}
static inline bool
got_page_reloc(unsigned int r_type)
{
return (r_type == elfcpp::R_MIPS_GOT_PAGE
|| r_type == elfcpp::R_MICROMIPS_GOT_PAGE);
}
static inline bool
tls_gd_reloc(unsigned int r_type)
{
return (r_type == elfcpp::R_MIPS_TLS_GD
|| r_type == elfcpp::R_MIPS16_TLS_GD
|| r_type == elfcpp::R_MICROMIPS_TLS_GD);
}
static inline bool
tls_gottprel_reloc(unsigned int r_type)
{
return (r_type == elfcpp::R_MIPS_TLS_GOTTPREL
|| r_type == elfcpp::R_MIPS16_TLS_GOTTPREL
|| r_type == elfcpp::R_MICROMIPS_TLS_GOTTPREL);
}
static inline bool
tls_ldm_reloc(unsigned int r_type)
{
return (r_type == elfcpp::R_MIPS_TLS_LDM
|| r_type == elfcpp::R_MIPS16_TLS_LDM
|| r_type == elfcpp::R_MICROMIPS_TLS_LDM);
}
static inline bool
mips16_call_reloc(unsigned int r_type)
{
return (r_type == elfcpp::R_MIPS16_26
|| r_type == elfcpp::R_MIPS16_CALL16);
}
static inline bool
jal_reloc(unsigned int r_type)
{
return (r_type == elfcpp::R_MIPS_26
|| r_type == elfcpp::R_MIPS16_26
|| r_type == elfcpp::R_MICROMIPS_26_S1);
}
static inline bool
micromips_branch_reloc(unsigned int r_type)
{
return (r_type == elfcpp::R_MICROMIPS_26_S1
|| r_type == elfcpp::R_MICROMIPS_PC16_S1
|| r_type == elfcpp::R_MICROMIPS_PC10_S1
|| r_type == elfcpp::R_MICROMIPS_PC7_S1);
}
// Check if R_TYPE is a MIPS16 reloc.
static inline bool
mips16_reloc(unsigned int r_type)
{
switch (r_type)
{
case elfcpp::R_MIPS16_26:
case elfcpp::R_MIPS16_GPREL:
case elfcpp::R_MIPS16_GOT16:
case elfcpp::R_MIPS16_CALL16:
case elfcpp::R_MIPS16_HI16:
case elfcpp::R_MIPS16_LO16:
case elfcpp::R_MIPS16_TLS_GD:
case elfcpp::R_MIPS16_TLS_LDM:
case elfcpp::R_MIPS16_TLS_DTPREL_HI16:
case elfcpp::R_MIPS16_TLS_DTPREL_LO16:
case elfcpp::R_MIPS16_TLS_GOTTPREL:
case elfcpp::R_MIPS16_TLS_TPREL_HI16:
case elfcpp::R_MIPS16_TLS_TPREL_LO16:
return true;
default:
return false;
}
}
// Check if R_TYPE is a microMIPS reloc.
static inline bool
micromips_reloc(unsigned int r_type)
{
switch (r_type)
{
case elfcpp::R_MICROMIPS_26_S1:
case elfcpp::R_MICROMIPS_HI16:
case elfcpp::R_MICROMIPS_LO16:
case elfcpp::R_MICROMIPS_GPREL16:
case elfcpp::R_MICROMIPS_LITERAL:
case elfcpp::R_MICROMIPS_GOT16:
case elfcpp::R_MICROMIPS_PC7_S1:
case elfcpp::R_MICROMIPS_PC10_S1:
case elfcpp::R_MICROMIPS_PC16_S1:
case elfcpp::R_MICROMIPS_CALL16:
case elfcpp::R_MICROMIPS_GOT_DISP:
case elfcpp::R_MICROMIPS_GOT_PAGE:
case elfcpp::R_MICROMIPS_GOT_OFST:
case elfcpp::R_MICROMIPS_GOT_HI16:
case elfcpp::R_MICROMIPS_GOT_LO16:
case elfcpp::R_MICROMIPS_SUB:
case elfcpp::R_MICROMIPS_HIGHER:
case elfcpp::R_MICROMIPS_HIGHEST:
case elfcpp::R_MICROMIPS_CALL_HI16:
case elfcpp::R_MICROMIPS_CALL_LO16:
case elfcpp::R_MICROMIPS_SCN_DISP:
case elfcpp::R_MICROMIPS_JALR:
case elfcpp::R_MICROMIPS_HI0_LO16:
case elfcpp::R_MICROMIPS_TLS_GD:
case elfcpp::R_MICROMIPS_TLS_LDM:
case elfcpp::R_MICROMIPS_TLS_DTPREL_HI16:
case elfcpp::R_MICROMIPS_TLS_DTPREL_LO16:
case elfcpp::R_MICROMIPS_TLS_GOTTPREL:
case elfcpp::R_MICROMIPS_TLS_TPREL_HI16:
case elfcpp::R_MICROMIPS_TLS_TPREL_LO16:
case elfcpp::R_MICROMIPS_GPREL7_S2:
case elfcpp::R_MICROMIPS_PC23_S2:
return true;
default:
return false;
}
}
static inline bool
is_matching_lo16_reloc(unsigned int high_reloc, unsigned int lo16_reloc)
{
switch (high_reloc)
{
case elfcpp::R_MIPS_HI16:
case elfcpp::R_MIPS_GOT16:
return lo16_reloc == elfcpp::R_MIPS_LO16;
case elfcpp::R_MIPS_PCHI16:
return lo16_reloc == elfcpp::R_MIPS_PCLO16;
case elfcpp::R_MIPS16_HI16:
case elfcpp::R_MIPS16_GOT16:
return lo16_reloc == elfcpp::R_MIPS16_LO16;
case elfcpp::R_MICROMIPS_HI16:
case elfcpp::R_MICROMIPS_GOT16:
return lo16_reloc == elfcpp::R_MICROMIPS_LO16;
default:
return false;
}
}
// This class is used to hold information about one GOT entry.
// There are three types of entry:
//
// (1) a SYMBOL + OFFSET address, where SYMBOL is local to an input object
// (object != NULL, symndx >= 0, tls_type != GOT_TLS_LDM)
// (2) a SYMBOL address, where SYMBOL is not local to an input object
// (sym != NULL, symndx == -1)
// (3) a TLS LDM slot (there's only one of these per GOT.)
// (object != NULL, symndx == 0, tls_type == GOT_TLS_LDM)
template<int size, bool big_endian>
class Mips_got_entry
{
typedef typename elfcpp::Elf_types<size>::Elf_Addr Mips_address;
public:
Mips_got_entry(Mips_relobj<size, big_endian>* object, unsigned int symndx,
Mips_address addend, unsigned char tls_type,
unsigned int shndx, bool is_section_symbol)
: addend_(addend), symndx_(symndx), tls_type_(tls_type),
is_section_symbol_(is_section_symbol), shndx_(shndx)
{ this->d.object = object; }
Mips_got_entry(Mips_symbol<size>* sym, unsigned char tls_type)
: addend_(0), symndx_(-1U), tls_type_(tls_type),
is_section_symbol_(false), shndx_(-1U)
{ this->d.sym = sym; }
// Return whether this entry is for a local symbol.
bool
is_for_local_symbol() const
{ return this->symndx_ != -1U; }
// Return whether this entry is for a global symbol.
bool
is_for_global_symbol() const
{ return this->symndx_ == -1U; }
// Return the hash of this entry.
size_t
hash() const
{
if (this->tls_type_ == GOT_TLS_LDM)
return this->symndx_ + (1 << 18);
size_t name_hash_value = gold::string_hash<char>(
(this->symndx_ != -1U)
? this->d.object->name().c_str()
: this->d.sym->name());
size_t addend = this->addend_;
return name_hash_value ^ this->symndx_ ^ (addend << 16);
}
// Return whether this entry is equal to OTHER.
bool
equals(Mips_got_entry<size, big_endian>* other) const
{
if (this->symndx_ != other->symndx_
|| this->tls_type_ != other->tls_type_)
return false;
if (this->tls_type_ == GOT_TLS_LDM)
return true;
return (((this->symndx_ != -1U)
? (this->d.object == other->d.object)
: (this->d.sym == other->d.sym))
&& (this->addend_ == other->addend_));
}
// Return input object that needs this GOT entry.
Mips_relobj<size, big_endian>*
object() const
{
gold_assert(this->symndx_ != -1U);
return this->d.object;
}
// Return local symbol index for local GOT entries.
unsigned int
symndx() const
{
gold_assert(this->symndx_ != -1U);
return this->symndx_;
}
// Return the relocation addend for local GOT entries.
Mips_address
addend() const
{ return this->addend_; }
// Return global symbol for global GOT entries.
Mips_symbol<size>*
sym() const
{
gold_assert(this->symndx_ == -1U);
return this->d.sym;
}
// Return whether this is a TLS GOT entry.
bool
is_tls_entry() const
{ return this->tls_type_ != GOT_TLS_NONE; }
// Return TLS type of this GOT entry.
unsigned char
tls_type() const
{ return this->tls_type_; }
// Return section index of the local symbol for local GOT entries.
unsigned int
shndx() const
{ return this->shndx_; }
// Return whether this is a STT_SECTION symbol.
bool
is_section_symbol() const
{ return this->is_section_symbol_; }
private:
// The addend.
Mips_address addend_;
// The index of the symbol if we have a local symbol; -1 otherwise.
unsigned int symndx_;
union
{
// The input object for local symbols that needs the GOT entry.
Mips_relobj<size, big_endian>* object;
// If symndx == -1, the global symbol corresponding to this GOT entry. The
// symbol's entry is in the local area if mips_sym->global_got_area is
// GGA_NONE, otherwise it is in the global area.
Mips_symbol<size>* sym;
} d;
// The TLS type of this GOT entry. An LDM GOT entry will be a local
// symbol entry with r_symndx == 0.
unsigned char tls_type_;
// Whether this is a STT_SECTION symbol.
bool is_section_symbol_;
// For local GOT entries, section index of the local symbol.
unsigned int shndx_;
};
// Hash for Mips_got_entry.
template<int size, bool big_endian>
class Mips_got_entry_hash
{
public:
size_t
operator()(Mips_got_entry<size, big_endian>* entry) const
{ return entry->hash(); }
};
// Equality for Mips_got_entry.
template<int size, bool big_endian>
class Mips_got_entry_eq
{
public:
bool
operator()(Mips_got_entry<size, big_endian>* e1,
Mips_got_entry<size, big_endian>* e2) const
{ return e1->equals(e2); }
};
// Hash for Mips_symbol.
template<int size>
class Mips_symbol_hash
{
public:
size_t
operator()(Mips_symbol<size>* sym) const
{ return sym->hash(); }
};
// Got_page_range. This class describes a range of addends: [MIN_ADDEND,
// MAX_ADDEND]. The instances form a non-overlapping list that is sorted by
// increasing MIN_ADDEND.
struct Got_page_range
{
Got_page_range()
: next(NULL), min_addend(0), max_addend(0)
{ }
Got_page_range* next;
int min_addend;
int max_addend;
// Return the maximum number of GOT page entries required.
int
get_max_pages()
{ return (this->max_addend - this->min_addend + 0x1ffff) >> 16; }
};
// Got_page_entry. This class describes the range of addends that are applied
// to page relocations against a given symbol.
struct Got_page_entry
{
Got_page_entry()
: object(NULL), symndx(-1U), ranges(NULL)
{ }
Got_page_entry(Object* object_, unsigned int symndx_)
: object(object_), symndx(symndx_), ranges(NULL)
{ }
// The input object that needs the GOT page entry.
Object* object;
// The index of the symbol, as stored in the relocation r_info.
unsigned int symndx;
// The ranges for this page entry.
Got_page_range* ranges;
};
// Hash for Got_page_entry.
struct Got_page_entry_hash
{
size_t
operator()(Got_page_entry* entry) const
{ return reinterpret_cast<uintptr_t>(entry->object) + entry->symndx; }
};
// Equality for Got_page_entry.
struct Got_page_entry_eq
{
bool
operator()(Got_page_entry* entry1, Got_page_entry* entry2) const
{
return entry1->object == entry2->object && entry1->symndx == entry2->symndx;
}
};
// This class is used to hold .got information when linking.
template<int size, bool big_endian>
class Mips_got_info
{
typedef typename elfcpp::Elf_types<size>::Elf_Addr Mips_address;
typedef Output_data_reloc<elfcpp::SHT_REL, true, size, big_endian>
Reloc_section;
typedef Unordered_map<unsigned int, unsigned int> Got_page_offsets;
// Unordered set of GOT entries.
typedef Unordered_set<Mips_got_entry<size, big_endian>*,
Mips_got_entry_hash<size, big_endian>,
Mips_got_entry_eq<size, big_endian> > Got_entry_set;
// Unordered set of GOT page entries.
typedef Unordered_set<Got_page_entry*,
Got_page_entry_hash, Got_page_entry_eq> Got_page_entry_set;
// Unordered set of global GOT entries.
typedef Unordered_set<Mips_symbol<size>*, Mips_symbol_hash<size> >
Global_got_entry_set;
public:
Mips_got_info()
: local_gotno_(0), page_gotno_(0), global_gotno_(0), reloc_only_gotno_(0),
tls_gotno_(0), tls_ldm_offset_(-1U), global_got_symbols_(),
got_entries_(), got_page_entries_(), got_page_offset_start_(0),
got_page_offset_next_(0), got_page_offsets_(), next_(NULL), index_(-1U),
offset_(0)
{ }
// Reserve GOT entry for a GOT relocation of type R_TYPE against symbol
// SYMNDX + ADDEND, where SYMNDX is a local symbol in section SHNDX in OBJECT.
void
record_local_got_symbol(Mips_relobj<size, big_endian>* object,
unsigned int symndx, Mips_address addend,
unsigned int r_type, unsigned int shndx,
bool is_section_symbol);
// Reserve GOT entry for a GOT relocation of type R_TYPE against MIPS_SYM,
// in OBJECT. FOR_CALL is true if the caller is only interested in
// using the GOT entry for calls. DYN_RELOC is true if R_TYPE is a dynamic
// relocation.
void
record_global_got_symbol(Mips_symbol<size>* mips_sym,
Mips_relobj<size, big_endian>* object,
unsigned int r_type, bool dyn_reloc, bool for_call);
// Add ENTRY to master GOT and to OBJECT's GOT.
void
record_got_entry(Mips_got_entry<size, big_endian>* entry,
Mips_relobj<size, big_endian>* object);
// Record that OBJECT has a page relocation against symbol SYMNDX and
// that ADDEND is the addend for that relocation.
void
record_got_page_entry(Mips_relobj<size, big_endian>* object,
unsigned int symndx, int addend);
// Create all entries that should be in the local part of the GOT.
void
add_local_entries(Target_mips<size, big_endian>* target, Layout* layout);
// Create GOT page entries.
void
add_page_entries(Target_mips<size, big_endian>* target, Layout* layout);
// Create global GOT entries, both GGA_NORMAL and GGA_RELOC_ONLY.
void
add_global_entries(Target_mips<size, big_endian>* target, Layout* layout,
unsigned int non_reloc_only_global_gotno);
// Create global GOT entries that should be in the GGA_RELOC_ONLY area.
void
add_reloc_only_entries(Mips_output_data_got<size, big_endian>* got);
// Create TLS GOT entries.
void
add_tls_entries(Target_mips<size, big_endian>* target, Layout* layout);
// Decide whether the symbol needs an entry in the global part of the primary
// GOT, setting global_got_area accordingly. Count the number of global
// symbols that are in the primary GOT only because they have dynamic
// relocations R_MIPS_REL32 against them (reloc_only_gotno).
void
count_got_symbols(Symbol_table* symtab);
// Return the offset of GOT page entry for VALUE.
unsigned int
get_got_page_offset(Mips_address value,
Mips_output_data_got<size, big_endian>* got);
// Count the number of GOT entries required.
void
count_got_entries();
// Count the number of GOT entries required by ENTRY. Accumulate the result.
void
count_got_entry(Mips_got_entry<size, big_endian>* entry);
// Add FROM's GOT entries.
void
add_got_entries(Mips_got_info<size, big_endian>* from);
// Add FROM's GOT page entries.
void
add_got_page_count(Mips_got_info<size, big_endian>* from);
// Return GOT size.
unsigned int
got_size() const
{ return ((2 + this->local_gotno_ + this->page_gotno_ + this->global_gotno_
+ this->tls_gotno_) * size/8);
}
// Return the number of local GOT entries.
unsigned int
local_gotno() const
{ return this->local_gotno_; }
// Return the maximum number of page GOT entries needed.
unsigned int
page_gotno() const
{ return this->page_gotno_; }
// Return the number of global GOT entries.
unsigned int
global_gotno() const
{ return this->global_gotno_; }
// Set the number of global GOT entries.
void
set_global_gotno(unsigned int global_gotno)
{ this->global_gotno_ = global_gotno; }
// Return the number of GGA_RELOC_ONLY global GOT entries.
unsigned int
reloc_only_gotno() const
{ return this->reloc_only_gotno_; }
// Return the number of TLS GOT entries.
unsigned int
tls_gotno() const
{ return this->tls_gotno_; }
// Return the GOT type for this GOT. Used for multi-GOT links only.
unsigned int
multigot_got_type(unsigned int got_type) const
{
switch (got_type)
{
case GOT_TYPE_STANDARD:
return GOT_TYPE_STANDARD_MULTIGOT + this->index_;
case GOT_TYPE_TLS_OFFSET:
return GOT_TYPE_TLS_OFFSET_MULTIGOT + this->index_;
case GOT_TYPE_TLS_PAIR:
return GOT_TYPE_TLS_PAIR_MULTIGOT + this->index_;
default:
gold_unreachable();
}
}
// Remove lazy-binding stubs for global symbols in this GOT.
void
remove_lazy_stubs(Target_mips<size, big_endian>* target);
// Return offset of this GOT from the start of .got section.
unsigned int
offset() const
{ return this->offset_; }
// Set offset of this GOT from the start of .got section.
void
set_offset(unsigned int offset)
{ this->offset_ = offset; }
// Set index of this GOT in multi-GOT links.
void
set_index(unsigned int index)
{ this->index_ = index; }
// Return next GOT in multi-GOT links.
Mips_got_info<size, big_endian>*
next() const
{ return this->next_; }
// Set next GOT in multi-GOT links.
void
set_next(Mips_got_info<size, big_endian>* next)
{ this->next_ = next; }
// Return the offset of TLS LDM entry for this GOT.
unsigned int
tls_ldm_offset() const
{ return this->tls_ldm_offset_; }
// Set the offset of TLS LDM entry for this GOT.
void
set_tls_ldm_offset(unsigned int tls_ldm_offset)
{ this->tls_ldm_offset_ = tls_ldm_offset; }
Global_got_entry_set&
global_got_symbols()
{ return this->global_got_symbols_; }
// Return the GOT_TLS_* type required by relocation type R_TYPE.
static int
mips_elf_reloc_tls_type(unsigned int r_type)
{
if (tls_gd_reloc(r_type))
return GOT_TLS_GD;
if (tls_ldm_reloc(r_type))
return GOT_TLS_LDM;
if (tls_gottprel_reloc(r_type))
return GOT_TLS_IE;
return GOT_TLS_NONE;
}
// Return the number of GOT slots needed for GOT TLS type TYPE.
static int
mips_tls_got_entries(unsigned int type)
{
switch (type)
{
case GOT_TLS_GD:
case GOT_TLS_LDM:
return 2;
case GOT_TLS_IE:
return 1;
case GOT_TLS_NONE:
return 0;
default:
gold_unreachable();
}
}
private:
// The number of local GOT entries.
unsigned int local_gotno_;
// The maximum number of page GOT entries needed.
unsigned int page_gotno_;
// The number of global GOT entries.
unsigned int global_gotno_;
// The number of global GOT entries that are in the GGA_RELOC_ONLY area.
unsigned int reloc_only_gotno_;
// The number of TLS GOT entries.
unsigned int tls_gotno_;
// The offset of TLS LDM entry for this GOT.
unsigned int tls_ldm_offset_;
// All symbols that have global GOT entry.
Global_got_entry_set global_got_symbols_;
// A hash table holding GOT entries.
Got_entry_set got_entries_;
// A hash table of GOT page entries (only used in master GOT).
Got_page_entry_set got_page_entries_;
// The offset of first GOT page entry for this GOT.
unsigned int got_page_offset_start_;
// The offset of next available GOT page entry for this GOT.
unsigned int got_page_offset_next_;
// A hash table that maps GOT page entry value to the GOT offset where
// the entry is located.
Got_page_offsets got_page_offsets_;
// In multi-GOT links, a pointer to the next GOT.
Mips_got_info<size, big_endian>* next_;
// Index of this GOT in multi-GOT links.
unsigned int index_;
// The offset of this GOT in multi-GOT links.
unsigned int offset_;
};
// This is a helper class used during relocation scan. It records GOT16 addend.
template<int size, bool big_endian>
struct got16_addend
{
typedef typename elfcpp::Elf_types<size>::Elf_Addr Mips_address;
got16_addend(const Sized_relobj_file<size, big_endian>* _object,
unsigned int _shndx, unsigned int _r_type, unsigned int _r_sym,
Mips_address _addend)
: object(_object), shndx(_shndx), r_type(_r_type), r_sym(_r_sym),
addend(_addend)
{ }
const Sized_relobj_file<size, big_endian>* object;
unsigned int shndx;
unsigned int r_type;
unsigned int r_sym;
Mips_address addend;
};
// .MIPS.abiflags section content
template<bool big_endian>
struct Mips_abiflags
{
typedef typename elfcpp::Swap<8, big_endian>::Valtype Valtype8;
typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype16;
typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype32;
Mips_abiflags()
: version(0), isa_level(0), isa_rev(0), gpr_size(0), cpr1_size(0),
cpr2_size(0), fp_abi(0), isa_ext(0), ases(0), flags1(0), flags2(0)
{ }
// Version of flags structure.
Valtype16 version;
// The level of the ISA: 1-5, 32, 64.
Valtype8 isa_level;
// The revision of ISA: 0 for MIPS V and below, 1-n otherwise.
Valtype8 isa_rev;
// The size of general purpose registers.
Valtype8 gpr_size;
// The size of co-processor 1 registers.
Valtype8 cpr1_size;
// The size of co-processor 2 registers.
Valtype8 cpr2_size;
// The floating-point ABI.
Valtype8 fp_abi;
// Processor-specific extension.
Valtype32 isa_ext;
// Mask of ASEs used.
Valtype32 ases;
// Mask of general flags.
Valtype32 flags1;