@@ -1065,22 +1065,19 @@ template <class ELFT> void SymbolTableSection<ELFT>::finalize() {
1065
1065
this ->OutSec ->Info = this ->Info = NumLocals + 1 ;
1066
1066
this ->OutSec ->Entsize = this ->Entsize ;
1067
1067
1068
- if (Config->Relocatable ) {
1069
- size_t I = NumLocals;
1070
- for (const SymbolTableEntry &S : Symbols)
1071
- S.Symbol ->DynsymIndex = ++I;
1068
+ if (Config->Relocatable )
1072
1069
return ;
1073
- }
1074
1070
1075
1071
if (!StrTabSec.isDynamic ()) {
1076
- std::stable_sort (
1077
- Symbols. begin () , Symbols.end (),
1078
- []( const SymbolTableEntry &L, const SymbolTableEntry &R) {
1079
- return L.Symbol ->symbol ()->computeBinding () == STB_LOCAL &&
1080
- R.Symbol ->symbol ()->computeBinding () != STB_LOCAL;
1081
- });
1072
+ auto GlobBegin = Symbols. begin () + NumLocals;
1073
+ std::stable_sort (GlobBegin , Symbols.end (), []( const SymbolTableEntry &L ,
1074
+ const SymbolTableEntry &R) {
1075
+ return L.Symbol ->symbol ()->computeBinding () == STB_LOCAL &&
1076
+ R.Symbol ->symbol ()->computeBinding () != STB_LOCAL;
1077
+ });
1082
1078
return ;
1083
1079
}
1080
+
1084
1081
if (In<ELFT>::GnuHashTab)
1085
1082
// NB: It also sorts Symbols to meet the GNU hash table requirements.
1086
1083
In<ELFT>::GnuHashTab->addSymbols (Symbols);
@@ -1094,10 +1091,23 @@ template <class ELFT> void SymbolTableSection<ELFT>::finalize() {
1094
1091
S.Symbol ->DynsymIndex = ++I;
1095
1092
}
1096
1093
1097
- template <class ELFT > void SymbolTableSection<ELFT>::addSymbol (SymbolBody *B) {
1094
+ template <class ELFT > void SymbolTableSection<ELFT>::addGlobal (SymbolBody *B) {
1098
1095
Symbols.push_back ({B, StrTabSec.addString (B->getName (), false )});
1099
1096
}
1100
1097
1098
+ template <class ELFT > void SymbolTableSection<ELFT>::addLocal(SymbolBody *B) {
1099
+ assert (!StrTabSec.isDynamic ());
1100
+ ++NumLocals;
1101
+ Symbols.push_back ({B, StrTabSec.addString (B->getName ())});
1102
+ }
1103
+
1104
+ template <class ELFT >
1105
+ size_t SymbolTableSection<ELFT>::getSymbolIndex(SymbolBody *Body) {
1106
+ auto I = llvm::find_if (
1107
+ Symbols, [&](const SymbolTableEntry &E) { return E.Symbol == Body; });
1108
+ return I - Symbols.begin () + 1 ;
1109
+ }
1110
+
1101
1111
template <class ELFT > void SymbolTableSection<ELFT>::writeTo(uint8_t *Buf) {
1102
1112
Buf += sizeof (Elf_Sym);
1103
1113
@@ -1113,26 +1123,24 @@ template <class ELFT>
1113
1123
void SymbolTableSection<ELFT>::writeLocalSymbols(uint8_t *&Buf) {
1114
1124
// Iterate over all input object files to copy their local symbols
1115
1125
// to the output symbol table pointed by Buf.
1116
- for (ObjectFile<ELFT> *File : Symtab<ELFT>::X->getObjectFiles ()) {
1117
- for (const std::pair<const DefinedRegular<ELFT> *, size_t > &P :
1118
- File->KeptLocalSyms ) {
1119
- const DefinedRegular<ELFT> &Body = *P.first ;
1120
- InputSectionBase<ELFT> *Section = Body.Section ;
1121
- auto *ESym = reinterpret_cast <Elf_Sym *>(Buf);
1122
-
1123
- if (!Section) {
1124
- ESym->st_shndx = SHN_ABS;
1125
- ESym->st_value = Body.Value ;
1126
- } else {
1127
- const OutputSectionBase *OutSec = Section->OutSec ;
1128
- ESym->st_shndx = OutSec->SectionIndex ;
1129
- ESym->st_value = OutSec->Addr + Section->getOffset (Body);
1130
- }
1131
- ESym->st_name = P.second ;
1132
- ESym->st_size = Body.template getSize <ELFT>();
1133
- ESym->setBindingAndType (STB_LOCAL, Body.Type );
1134
- Buf += sizeof (*ESym);
1126
+
1127
+ for (auto I = Symbols.begin (); I != Symbols.begin () + NumLocals; ++I) {
1128
+ const DefinedRegular<ELFT> &Body = *cast<DefinedRegular<ELFT>>(I->Symbol );
1129
+ InputSectionBase<ELFT> *Section = Body.Section ;
1130
+ auto *ESym = reinterpret_cast <Elf_Sym *>(Buf);
1131
+
1132
+ if (!Section) {
1133
+ ESym->st_shndx = SHN_ABS;
1134
+ ESym->st_value = Body.Value ;
1135
+ } else {
1136
+ const OutputSectionBase *OutSec = Section->OutSec ;
1137
+ ESym->st_shndx = OutSec->SectionIndex ;
1138
+ ESym->st_value = OutSec->Addr + Section->getOffset (Body);
1135
1139
}
1140
+ ESym->st_name = I->StrTabOffset ;
1141
+ ESym->st_size = Body.template getSize <ELFT>();
1142
+ ESym->setBindingAndType (STB_LOCAL, Body.Type );
1143
+ Buf += sizeof (*ESym);
1136
1144
}
1137
1145
}
1138
1146
@@ -1141,7 +1149,9 @@ void SymbolTableSection<ELFT>::writeGlobalSymbols(uint8_t *Buf) {
1141
1149
// Write the internal symbol table contents to the output symbol table
1142
1150
// pointed by Buf.
1143
1151
auto *ESym = reinterpret_cast <Elf_Sym *>(Buf);
1144
- for (const SymbolTableEntry &S : Symbols) {
1152
+
1153
+ for (auto I = Symbols.begin () + NumLocals; I != Symbols.end (); ++I) {
1154
+ const SymbolTableEntry &S = *I;
1145
1155
SymbolBody *Body = S.Symbol ;
1146
1156
size_t StrOff = S.StrTabOffset ;
1147
1157
0 commit comments