@@ -66,8 +66,6 @@ using namespace llvm;
66
66
67
67
namespace {
68
68
69
- using SectionIndexMapTy = DenseMap<const MCSectionELF *, uint32_t >;
70
-
71
69
class ELFObjectWriter ;
72
70
struct ELFWriter ;
73
71
@@ -136,8 +134,8 @@ struct ELFWriter {
136
134
unsigned SymbolTableIndex = ~0u ;
137
135
138
136
// Sections in the order they are to be output in the section table.
139
- std::vector<const MCSectionELF *> SectionTable;
140
- unsigned addToSectionTable (const MCSectionELF *Sec);
137
+ std::vector<MCSectionELF *> SectionTable;
138
+ unsigned addToSectionTable (MCSectionELF *Sec);
141
139
142
140
// TargetObjectWriter wrappers.
143
141
bool is64Bit () const ;
@@ -171,31 +169,21 @@ struct ELFWriter {
171
169
void writeSymbol (const MCAssembler &Asm, SymbolTableWriter &Writer,
172
170
uint32_t StringIndex, ELFSymbolData &MSD);
173
171
174
- // Start and end offset of each section
175
- using SectionOffsetsTy =
176
- std::map<const MCSectionELF *, std::pair<uint64_t , uint64_t >>;
177
-
178
172
// Map from a signature symbol to the group section index
179
173
using RevGroupMapTy = DenseMap<const MCSymbol *, unsigned >;
180
174
181
175
// / Compute the symbol table data
182
176
// /
183
177
// / \param Asm - The assembler.
184
- // / \param SectionIndexMap - Maps a section to its index.
185
178
// / \param RevGroupMap - Maps a signature symbol to the group section.
186
- void computeSymbolTable (MCAssembler &Asm,
187
- const SectionIndexMapTy &SectionIndexMap,
188
- const RevGroupMapTy &RevGroupMap,
189
- SectionOffsetsTy &SectionOffsets);
179
+ void computeSymbolTable (MCAssembler &Asm, const RevGroupMapTy &RevGroupMap);
190
180
191
181
void writeAddrsigSection ();
192
182
193
183
MCSectionELF *createRelocationSection (MCContext &Ctx,
194
184
const MCSectionELF &Sec);
195
185
196
- void writeSectionHeader (const MCAssembler &Asm,
197
- const SectionIndexMapTy &SectionIndexMap,
198
- const SectionOffsetsTy &SectionOffsets);
186
+ void writeSectionHeader (const MCAssembler &Asm);
199
187
200
188
void writeSectionData (const MCAssembler &Asm, MCSection &Sec);
201
189
@@ -207,8 +195,7 @@ struct ELFWriter {
207
195
void writeRelocations (const MCAssembler &Asm, const MCSectionELF &Sec);
208
196
209
197
uint64_t writeObject (MCAssembler &Asm);
210
- void writeSection (const SectionIndexMapTy &SectionIndexMap,
211
- uint32_t GroupSymbolIndex, uint64_t Offset, uint64_t Size ,
198
+ void writeSection (uint32_t GroupSymbolIndex, uint64_t Offset, uint64_t Size ,
212
199
const MCSectionELF &Section);
213
200
};
214
201
@@ -330,7 +317,7 @@ uint64_t ELFWriter::align(Align Alignment) {
330
317
return NewOffset;
331
318
}
332
319
333
- unsigned ELFWriter::addToSectionTable (const MCSectionELF *Sec) {
320
+ unsigned ELFWriter::addToSectionTable (MCSectionELF *Sec) {
334
321
SectionTable.push_back (Sec);
335
322
StrTabBuilder.add (Sec->getName ());
336
323
return SectionTable.size ();
@@ -612,9 +599,7 @@ bool ELFWriter::isInSymtab(const MCAssembler &Asm, const MCSymbolELF &Symbol,
612
599
}
613
600
614
601
void ELFWriter::computeSymbolTable (MCAssembler &Asm,
615
- const SectionIndexMapTy &SectionIndexMap,
616
- const RevGroupMapTy &RevGroupMap,
617
- SectionOffsetsTy &SectionOffsets) {
602
+ const RevGroupMapTy &RevGroupMap) {
618
603
MCContext &Ctx = Asm.getContext ();
619
604
SymbolTableWriter Writer (*this , is64Bit ());
620
605
@@ -697,7 +682,7 @@ void ELFWriter::computeSymbolTable(MCAssembler &Asm,
697
682
698
683
if (Mode == NonDwoOnly && isDwoSection (Section))
699
684
continue ;
700
- MSD.SectionIndex = SectionIndexMap. lookup (&Section );
685
+ MSD.SectionIndex = Section. getOrdinal ( );
701
686
assert (MSD.SectionIndex && " Invalid section index!" );
702
687
if (MSD.SectionIndex >= ELF::SHN_LORESERVE)
703
688
HasLargeSectionIndex = true ;
@@ -775,7 +760,7 @@ void ELFWriter::computeSymbolTable(MCAssembler &Asm,
775
760
}
776
761
777
762
uint64_t SecEnd = W.OS .tell ();
778
- SectionOffsets[ SymtabSection] = std::make_pair (SecStart, SecEnd);
763
+ SymtabSection-> setOffsets (SecStart, SecEnd);
779
764
780
765
ArrayRef<uint32_t > ShndxIndexes = Writer.getShndxIndexes ();
781
766
if (ShndxIndexes.empty ()) {
@@ -785,12 +770,11 @@ void ELFWriter::computeSymbolTable(MCAssembler &Asm,
785
770
assert (SymtabShndxSectionIndex != 0 );
786
771
787
772
SecStart = W.OS .tell ();
788
- const MCSectionELF *SymtabShndxSection =
789
- SectionTable[SymtabShndxSectionIndex - 1 ];
773
+ MCSectionELF *SymtabShndxSection = SectionTable[SymtabShndxSectionIndex - 1 ];
790
774
for (uint32_t Index : ShndxIndexes)
791
775
write (Index);
792
776
SecEnd = W.OS .tell ();
793
- SectionOffsets[ SymtabShndxSection] = std::make_pair (SecStart, SecEnd);
777
+ SymtabShndxSection-> setOffsets (SecStart, SecEnd);
794
778
}
795
779
796
780
void ELFWriter::writeAddrsigSection () {
@@ -1030,8 +1014,7 @@ void ELFWriter::writeRelocations(const MCAssembler &Asm,
1030
1014
}
1031
1015
}
1032
1016
1033
- void ELFWriter::writeSection (const SectionIndexMapTy &SectionIndexMap,
1034
- uint32_t GroupSymbolIndex, uint64_t Offset,
1017
+ void ELFWriter::writeSection (uint32_t GroupSymbolIndex, uint64_t Offset,
1035
1018
uint64_t Size , const MCSectionELF &Section) {
1036
1019
uint64_t sh_link = 0 ;
1037
1020
uint64_t sh_info = 0 ;
@@ -1050,7 +1033,7 @@ void ELFWriter::writeSection(const SectionIndexMapTy &SectionIndexMap,
1050
1033
sh_link = SymbolTableIndex;
1051
1034
assert (sh_link && " .symtab not found" );
1052
1035
const MCSection *InfoSection = Section.getLinkedToSection ();
1053
- sh_info = SectionIndexMap. lookup (cast<MCSectionELF>(InfoSection) );
1036
+ sh_info = InfoSection-> getOrdinal ( );
1054
1037
break ;
1055
1038
}
1056
1039
@@ -1075,10 +1058,8 @@ void ELFWriter::writeSection(const SectionIndexMapTy &SectionIndexMap,
1075
1058
// If the value in the associated metadata is not a definition, Sym will be
1076
1059
// undefined. Represent this with sh_link=0.
1077
1060
const MCSymbol *Sym = Section.getLinkedToSymbol ();
1078
- if (Sym && Sym->isInSection ()) {
1079
- const MCSectionELF *Sec = cast<MCSectionELF>(&Sym->getSection ());
1080
- sh_link = SectionIndexMap.lookup (Sec);
1081
- }
1061
+ if (Sym && Sym->isInSection ())
1062
+ sh_link = Sym->getSection ().getOrdinal ();
1082
1063
}
1083
1064
1084
1065
WriteSecHdrEntry (StrTabBuilder.getOffset (Section.getName ()),
@@ -1087,9 +1068,7 @@ void ELFWriter::writeSection(const SectionIndexMapTy &SectionIndexMap,
1087
1068
Section.getEntrySize ());
1088
1069
}
1089
1070
1090
- void ELFWriter::writeSectionHeader (const MCAssembler &Asm,
1091
- const SectionIndexMapTy &SectionIndexMap,
1092
- const SectionOffsetsTy &SectionOffsets) {
1071
+ void ELFWriter::writeSectionHeader (const MCAssembler &Asm) {
1093
1072
const unsigned NumSections = SectionTable.size ();
1094
1073
1095
1074
// Null section first.
@@ -1105,16 +1084,14 @@ void ELFWriter::writeSectionHeader(const MCAssembler &Asm,
1105
1084
else
1106
1085
GroupSymbolIndex = Section->getGroup ()->getIndex ();
1107
1086
1108
- const std::pair<uint64_t , uint64_t > &Offsets =
1109
- SectionOffsets.find (Section)->second ;
1087
+ std::pair<uint64_t , uint64_t > Offsets = Section->getOffsets ();
1110
1088
uint64_t Size ;
1111
1089
if (Type == ELF::SHT_NOBITS)
1112
1090
Size = Asm.getSectionAddressSize (*Section);
1113
1091
else
1114
1092
Size = Offsets.second - Offsets.first ;
1115
1093
1116
- writeSection (SectionIndexMap, GroupSymbolIndex, Offsets.first , Size ,
1117
- *Section);
1094
+ writeSection (GroupSymbolIndex, Offsets.first , Size , *Section);
1118
1095
}
1119
1096
}
1120
1097
@@ -1127,17 +1104,15 @@ uint64_t ELFWriter::writeObject(MCAssembler &Asm) {
1127
1104
StringTableIndex = addToSectionTable (StrtabSection);
1128
1105
1129
1106
RevGroupMapTy RevGroupMap;
1130
- SectionIndexMapTy SectionIndexMap;
1131
-
1132
- DenseMap<const MCSymbol *, SmallVector<const MCSectionELF *, 0 >> GroupMembers;
1133
1107
1134
1108
// Write out the ELF header ...
1135
1109
writeHeader (Asm);
1136
1110
1137
1111
// ... then the sections ...
1138
- SectionOffsetsTy SectionOffsets;
1139
- std::vector<MCSectionELF *> Groups;
1140
- std::vector<MCSectionELF *> Relocations;
1112
+ SmallVector<std::pair<MCSectionELF *, SmallVector<unsigned >>, 0 > Groups;
1113
+ // Map from group section index to group
1114
+ SmallVector<unsigned , 0 > GroupMap;
1115
+ SmallVector<MCSectionELF *> Relocations;
1141
1116
for (MCSection &Sec : Asm) {
1142
1117
MCSectionELF &Section = static_cast <MCSectionELF &>(Sec);
1143
1118
if (Mode == NonDwoOnly && isDwoSection (Section))
@@ -1152,49 +1127,50 @@ uint64_t ELFWriter::writeObject(MCAssembler &Asm) {
1152
1127
writeSectionData (Asm, Section);
1153
1128
1154
1129
uint64_t SecEnd = W.OS .tell ();
1155
- SectionOffsets[& Section] = std::make_pair (SecStart, SecEnd);
1130
+ Section. setOffsets (SecStart, SecEnd);
1156
1131
1157
1132
MCSectionELF *RelSection = createRelocationSection (Ctx, Section);
1158
1133
1134
+ unsigned *GroupIdxEntry = nullptr ;
1159
1135
if (SignatureSymbol) {
1160
- unsigned &GroupIdx = RevGroupMap[SignatureSymbol];
1161
- if (!GroupIdx ) {
1136
+ GroupIdxEntry = & RevGroupMap[SignatureSymbol];
1137
+ if (!*GroupIdxEntry ) {
1162
1138
MCSectionELF *Group =
1163
1139
Ctx.createELFGroupSection (SignatureSymbol, Section.isComdat ());
1164
- GroupIdx = addToSectionTable (Group);
1140
+ *GroupIdxEntry = addToSectionTable (Group);
1165
1141
Group->setAlignment (Align (4 ));
1166
- Groups.push_back (Group);
1142
+
1143
+ GroupMap.resize (*GroupIdxEntry + 1 );
1144
+ GroupMap[*GroupIdxEntry] = Groups.size ();
1145
+ Groups.emplace_back (Group, SmallVector<unsigned >{});
1167
1146
}
1168
- SmallVector<const MCSectionELF *, 0 > &Members =
1169
- GroupMembers[SignatureSymbol];
1170
- Members.push_back (&Section);
1171
- if (RelSection)
1172
- Members.push_back (RelSection);
1173
1147
}
1174
1148
1175
- SectionIndexMap[& Section] = addToSectionTable (&Section);
1149
+ Section. setOrdinal ( addToSectionTable (&Section) );
1176
1150
if (RelSection) {
1177
- SectionIndexMap[ RelSection] = addToSectionTable (RelSection);
1151
+ RelSection-> setOrdinal ( addToSectionTable (RelSection) );
1178
1152
Relocations.push_back (RelSection);
1179
1153
}
1180
1154
1155
+ if (GroupIdxEntry) {
1156
+ auto &Members = Groups[GroupMap[*GroupIdxEntry]];
1157
+ Members.second .push_back (Section.getOrdinal ());
1158
+ if (RelSection)
1159
+ Members.second .push_back (RelSection->getOrdinal ());
1160
+ }
1161
+
1181
1162
OWriter.TargetObjectWriter ->addTargetSectionFlags (Ctx, Section);
1182
1163
}
1183
1164
1184
- for (MCSectionELF * Group : Groups) {
1165
+ for (auto &[ Group, Members] : Groups) {
1185
1166
// Remember the offset into the file for this section.
1186
1167
const uint64_t SecStart = align (Group->getAlign ());
1187
1168
1188
- const MCSymbol *SignatureSymbol = Group->getGroup ();
1189
- assert (SignatureSymbol);
1190
1169
write (uint32_t (Group->isComdat () ? unsigned (ELF::GRP_COMDAT) : 0 ));
1191
- for (const MCSectionELF *Member : GroupMembers[SignatureSymbol]) {
1192
- uint32_t SecIndex = SectionIndexMap.lookup (Member);
1193
- write (SecIndex);
1194
- }
1170
+ W.write <unsigned >(Members);
1195
1171
1196
1172
uint64_t SecEnd = W.OS .tell ();
1197
- SectionOffsets[ Group] = std::make_pair (SecStart, SecEnd);
1173
+ Group-> setOffsets (SecStart, SecEnd);
1198
1174
}
1199
1175
1200
1176
if (Mode == DwoOnly) {
@@ -1210,7 +1186,7 @@ uint64_t ELFWriter::writeObject(MCAssembler &Asm) {
1210
1186
}
1211
1187
1212
1188
// Compute symbol table information.
1213
- computeSymbolTable (Asm, SectionIndexMap, RevGroupMap, SectionOffsets );
1189
+ computeSymbolTable (Asm, RevGroupMap);
1214
1190
1215
1191
for (MCSectionELF *RelSection : Relocations) {
1216
1192
// Remember the offset into the file for this section.
@@ -1220,27 +1196,27 @@ uint64_t ELFWriter::writeObject(MCAssembler &Asm) {
1220
1196
cast<MCSectionELF>(*RelSection->getLinkedToSection ()));
1221
1197
1222
1198
uint64_t SecEnd = W.OS .tell ();
1223
- SectionOffsets[ RelSection] = std::make_pair (SecStart, SecEnd);
1199
+ RelSection-> setOffsets (SecStart, SecEnd);
1224
1200
}
1225
1201
1226
1202
if (OWriter.EmitAddrsigSection ) {
1227
1203
uint64_t SecStart = W.OS .tell ();
1228
1204
writeAddrsigSection ();
1229
1205
uint64_t SecEnd = W.OS .tell ();
1230
- SectionOffsets[ AddrsigSection] = std::make_pair (SecStart, SecEnd);
1206
+ AddrsigSection-> setOffsets (SecStart, SecEnd);
1231
1207
}
1232
1208
}
1233
1209
1234
1210
{
1235
1211
uint64_t SecStart = W.OS .tell ();
1236
1212
StrTabBuilder.write (W.OS );
1237
- SectionOffsets[ StrtabSection] = std::make_pair (SecStart, W.OS .tell ());
1213
+ StrtabSection-> setOffsets (SecStart, W.OS .tell ());
1238
1214
}
1239
1215
1240
1216
const uint64_t SectionHeaderOffset = align (is64Bit () ? Align (8 ) : Align (4 ));
1241
1217
1242
1218
// ... then the section header table ...
1243
- writeSectionHeader (Asm, SectionIndexMap, SectionOffsets );
1219
+ writeSectionHeader (Asm);
1244
1220
1245
1221
uint16_t NumSections = support::endian::byte_swap<uint16_t >(
1246
1222
(SectionTable.size () + 1 >= ELF::SHN_LORESERVE) ? (uint16_t )ELF::SHN_UNDEF
0 commit comments