@@ -1001,19 +1001,18 @@ void ThunkCreator::mergeThunks() {
1001
1001
}
1002
1002
}
1003
1003
1004
- ThunkSection *ThunkCreator::getOSThunkSec (ThunkSection *&TS,
1005
- OutputSection *OS) {
1006
- if (TS == nullptr ) {
1004
+ ThunkSection *ThunkCreator::getOSThunkSec (OutputSection *OS) {
1005
+ if (CurTS == nullptr ) {
1007
1006
uint32_t Off = 0 ;
1008
1007
for (auto *IS : OS->Sections ) {
1009
1008
Off = IS->OutSecOff + IS->getSize ();
1010
1009
if ((IS->Flags & SHF_EXECINSTR) == 0 )
1011
1010
break ;
1012
1011
}
1013
- TS = make<ThunkSection>(OS, Off);
1014
- ThunkSections[&OS->Sections ].push_back (TS );
1012
+ CurTS = make<ThunkSection>(OS, Off);
1013
+ ThunkSections[&OS->Sections ].push_back (CurTS );
1015
1014
}
1016
- return TS ;
1015
+ return CurTS ;
1017
1016
}
1018
1017
1019
1018
ThunkSection *ThunkCreator::getISThunkSec (InputSection *IS, OutputSection *OS) {
@@ -1035,6 +1034,20 @@ std::pair<Thunk *, bool> ThunkCreator::getThunk(SymbolBody &Body,
1035
1034
return std::make_pair (res.first ->second , res.second );
1036
1035
}
1037
1036
1037
+ // Call Fn on every executable InputSection accessed via the linker script
1038
+ // InputSectionDescription::Sections.
1039
+ void ThunkCreator::forEachExecInputSection (
1040
+ ArrayRef<OutputSection *> OutputSections,
1041
+ std::function<void (OutputSection *, InputSection *)> Fn) {
1042
+ for (OutputSection *OS : OutputSections) {
1043
+ if (!(OS->Flags & SHF_ALLOC) || !(OS->Flags & SHF_EXECINSTR))
1044
+ continue ;
1045
+ CurTS = nullptr ;
1046
+ for (InputSection *IS : OS->Sections )
1047
+ Fn (OS, IS);
1048
+ }
1049
+ }
1050
+
1038
1051
// Process all relocations from the InputSections that have been assigned
1039
1052
// to OutputSections and redirect through Thunks if needed.
1040
1053
//
@@ -1052,31 +1065,29 @@ bool ThunkCreator::createThunks(ArrayRef<OutputSection *> OutputSections) {
1052
1065
// We separate the creation of ThunkSections from the insertion of the
1053
1066
// ThunkSections back into the OutputSection as ThunkSections are not always
1054
1067
// inserted into the same OutputSection as the caller.
1055
- for (OutputSection *OS : OutputSections) {
1056
- ThunkSection *OSTS = nullptr ;
1057
- for (InputSection *IS : OS->Sections ) {
1058
- for (Relocation &Rel : IS->Relocations ) {
1059
- SymbolBody &Body = *Rel.Sym ;
1060
- if (!Target->needsThunk (Rel.Expr , Rel.Type , IS->File , Body))
1061
- continue ;
1062
- Thunk *T;
1063
- bool IsNew;
1064
- std::tie (T, IsNew) = getThunk (Body, Rel.Type );
1065
- if (IsNew) {
1066
- // Find or create a ThunkSection for the new Thunk
1067
- ThunkSection *TS;
1068
- if (auto *TIS = T->getTargetInputSection ())
1069
- TS = getISThunkSec (TIS, OS);
1070
- else
1071
- TS = getOSThunkSec (OSTS, OS);
1072
- TS->addThunk (T);
1068
+ forEachExecInputSection (
1069
+ OutputSections, [=](OutputSection *OS, InputSection *IS) {
1070
+ for (Relocation &Rel : IS->Relocations ) {
1071
+ SymbolBody &Body = *Rel.Sym ;
1072
+ if (!Target->needsThunk (Rel.Expr , Rel.Type , IS->File , Body))
1073
+ continue ;
1074
+ Thunk *T;
1075
+ bool IsNew;
1076
+ std::tie (T, IsNew) = getThunk (Body, Rel.Type );
1077
+ if (IsNew) {
1078
+ // Find or create a ThunkSection for the new Thunk
1079
+ ThunkSection *TS;
1080
+ if (auto *TIS = T->getTargetInputSection ())
1081
+ TS = getISThunkSec (TIS, OS);
1082
+ else
1083
+ TS = getOSThunkSec (OS);
1084
+ TS->addThunk (T);
1085
+ }
1086
+ // Redirect relocation to Thunk, we never go via the PLT to a Thunk
1087
+ Rel.Sym = T->ThunkSym ;
1088
+ Rel.Expr = fromPlt (Rel.Expr );
1073
1089
}
1074
- // Redirect relocation to Thunk, we never go via the PLT to a Thunk
1075
- Rel.Sym = T->ThunkSym ;
1076
- Rel.Expr = fromPlt (Rel.Expr );
1077
- }
1078
- }
1079
- }
1090
+ });
1080
1091
1081
1092
// Merge all created synthetic ThunkSections back into OutputSection
1082
1093
mergeThunks ();
0 commit comments