@@ -157,9 +157,9 @@ class ELFObjectWriter : public MCObjectWriter {
157
157
158
158
template <typename T> void write (T Val) {
159
159
if (IsLittleEndian)
160
- support::endian::Writer<support::little>(OS ).write (Val);
160
+ support::endian::Writer<support::little>(getStream () ).write (Val);
161
161
else
162
- support::endian::Writer<support::big>(OS ).write (Val);
162
+ support::endian::Writer<support::big>(getStream () ).write (Val);
163
163
}
164
164
165
165
void writeHeader (const MCAssembler &Asm);
@@ -232,7 +232,7 @@ class ELFObjectWriter : public MCObjectWriter {
232
232
}
233
233
234
234
void ELFObjectWriter::align (unsigned Alignment) {
235
- uint64_t Padding = OffsetToAlignment (OS .tell (), Alignment);
235
+ uint64_t Padding = OffsetToAlignment (getStream () .tell (), Alignment);
236
236
WriteZeros (Padding);
237
237
}
238
238
@@ -764,7 +764,7 @@ void ELFObjectWriter::computeSymbolTable(
764
764
SymbolTableIndex = addToSectionTable (SymtabSection);
765
765
766
766
align (SymtabSection->getAlignment ());
767
- uint64_t SecStart = OS .tell ();
767
+ uint64_t SecStart = getStream () .tell ();
768
768
769
769
// The first entry is the undefined symbol entry.
770
770
Writer.writeSymbol (0 , 0 , 0 , 0 , 0 , 0 , false );
@@ -911,7 +911,7 @@ void ELFObjectWriter::computeSymbolTable(
911
911
assert (MSD.Symbol ->getBinding () != ELF::STB_LOCAL);
912
912
}
913
913
914
- uint64_t SecEnd = OS .tell ();
914
+ uint64_t SecEnd = getStream () .tell ();
915
915
SectionOffsets[SymtabSection] = std::make_pair (SecStart, SecEnd);
916
916
917
917
ArrayRef<uint32_t > ShndxIndexes = Writer.getShndxIndexes ();
@@ -921,12 +921,12 @@ void ELFObjectWriter::computeSymbolTable(
921
921
}
922
922
assert (SymtabShndxSectionIndex != 0 );
923
923
924
- SecStart = OS .tell ();
924
+ SecStart = getStream () .tell ();
925
925
const MCSectionELF *SymtabShndxSection =
926
926
SectionTable[SymtabShndxSectionIndex - 1 ];
927
927
for (uint32_t Index : ShndxIndexes)
928
928
write (Index);
929
- SecEnd = OS .tell ();
929
+ SecEnd = getStream () .tell ();
930
930
SectionOffsets[SymtabShndxSection] = std::make_pair (SecStart, SecEnd);
931
931
}
932
932
@@ -957,31 +957,6 @@ ELFObjectWriter::createRelocationSection(MCContext &Ctx,
957
957
return RelaSection;
958
958
}
959
959
960
- static SmallVector<char , 128 >
961
- getUncompressedData (const MCAsmLayout &Layout,
962
- const MCSection::FragmentListType &Fragments) {
963
- SmallVector<char , 128 > UncompressedData;
964
- for (const MCFragment &F : Fragments) {
965
- const SmallVectorImpl<char > *Contents;
966
- switch (F.getKind ()) {
967
- case MCFragment::FT_Data:
968
- Contents = &cast<MCDataFragment>(F).getContents ();
969
- break ;
970
- case MCFragment::FT_Dwarf:
971
- Contents = &cast<MCDwarfLineAddrFragment>(F).getContents ();
972
- break ;
973
- case MCFragment::FT_DwarfFrame:
974
- Contents = &cast<MCDwarfCallFrameFragment>(F).getContents ();
975
- break ;
976
- default :
977
- llvm_unreachable (
978
- " Not expecting any other fragment types in a debug_* section" );
979
- }
980
- UncompressedData.append (Contents->begin (), Contents->end ());
981
- }
982
- return UncompressedData;
983
- }
984
-
985
960
// Include the debug info compression header:
986
961
// "ZLIB" followed by 8 bytes representing the uncompressed size of the section,
987
962
// useful for consumers to preallocate a buffer to decompress into.
@@ -1016,27 +991,29 @@ void ELFObjectWriter::writeSectionData(const MCAssembler &Asm, MCSection &Sec,
1016
991
return ;
1017
992
}
1018
993
1019
- // Gather the uncompressed data from all the fragments.
1020
- const MCSection::FragmentListType &Fragments = Section.getFragmentList ();
1021
- SmallVector<char , 128 > UncompressedData =
1022
- getUncompressedData (Layout, Fragments);
994
+ SmallVector<char , 128 > UncompressedData;
995
+ raw_svector_ostream VecOS (UncompressedData);
996
+ raw_pwrite_stream &OldStream = getStream ();
997
+ setStream (VecOS);
998
+ Asm.writeSectionData (&Section, Layout);
999
+ setStream (OldStream);
1023
1000
1024
1001
SmallVector<char , 128 > CompressedContents;
1025
1002
zlib::Status Success = zlib::compress (
1026
1003
StringRef (UncompressedData.data (), UncompressedData.size ()),
1027
1004
CompressedContents);
1028
1005
if (Success != zlib::StatusOK) {
1029
- Asm. writeSectionData (&Section, Layout) ;
1006
+ getStream () << UncompressedData ;
1030
1007
return ;
1031
1008
}
1032
1009
1033
1010
if (!prependCompressionHeader (UncompressedData.size (), CompressedContents)) {
1034
- Asm. writeSectionData (&Section, Layout) ;
1011
+ getStream () << UncompressedData ;
1035
1012
return ;
1036
1013
}
1037
1014
Asm.getContext ().renameELFSection (&Section,
1038
1015
(" .z" + SectionName.drop_front (1 )).str ());
1039
- OS << CompressedContents;
1016
+ getStream () << CompressedContents;
1040
1017
}
1041
1018
1042
1019
void ELFObjectWriter::WriteSecHdrEntry (uint32_t Name, uint32_t Type,
@@ -1100,7 +1077,7 @@ void ELFObjectWriter::writeRelocations(const MCAssembler &Asm,
1100
1077
1101
1078
const MCSectionELF *ELFObjectWriter::createStringTable (MCContext &Ctx) {
1102
1079
const MCSectionELF *StrtabSection = SectionTable[StringTableIndex - 1 ];
1103
- OS << StrTabBuilder.data ();
1080
+ getStream () << StrTabBuilder.data ();
1104
1081
return StrtabSection;
1105
1082
}
1106
1083
@@ -1209,12 +1186,12 @@ void ELFObjectWriter::writeObject(MCAssembler &Asm,
1209
1186
align (Section.getAlignment ());
1210
1187
1211
1188
// Remember the offset into the file for this section.
1212
- uint64_t SecStart = OS .tell ();
1189
+ uint64_t SecStart = getStream () .tell ();
1213
1190
1214
1191
const MCSymbolELF *SignatureSymbol = Section.getGroup ();
1215
1192
writeSectionData (Asm, Section, Layout);
1216
1193
1217
- uint64_t SecEnd = OS .tell ();
1194
+ uint64_t SecEnd = getStream () .tell ();
1218
1195
SectionOffsets[&Section] = std::make_pair (SecStart, SecEnd);
1219
1196
1220
1197
MCSectionELF *RelSection = createRelocationSection (Ctx, Section);
@@ -1246,7 +1223,7 @@ void ELFObjectWriter::writeObject(MCAssembler &Asm,
1246
1223
align (Group->getAlignment ());
1247
1224
1248
1225
// Remember the offset into the file for this section.
1249
- uint64_t SecStart = OS .tell ();
1226
+ uint64_t SecStart = getStream () .tell ();
1250
1227
1251
1228
const MCSymbol *SignatureSymbol = Group->getGroup ();
1252
1229
assert (SignatureSymbol);
@@ -1256,7 +1233,7 @@ void ELFObjectWriter::writeObject(MCAssembler &Asm,
1256
1233
write (SecIndex);
1257
1234
}
1258
1235
1259
- uint64_t SecEnd = OS .tell ();
1236
+ uint64_t SecEnd = getStream () .tell ();
1260
1237
SectionOffsets[Group] = std::make_pair (SecStart, SecEnd);
1261
1238
}
1262
1239
@@ -1267,25 +1244,25 @@ void ELFObjectWriter::writeObject(MCAssembler &Asm,
1267
1244
align (RelSection->getAlignment ());
1268
1245
1269
1246
// Remember the offset into the file for this section.
1270
- uint64_t SecStart = OS .tell ();
1247
+ uint64_t SecStart = getStream () .tell ();
1271
1248
1272
1249
writeRelocations (Asm, *RelSection->getAssociatedSection ());
1273
1250
1274
- uint64_t SecEnd = OS .tell ();
1251
+ uint64_t SecEnd = getStream () .tell ();
1275
1252
SectionOffsets[RelSection] = std::make_pair (SecStart, SecEnd);
1276
1253
}
1277
1254
1278
1255
{
1279
- uint64_t SecStart = OS .tell ();
1256
+ uint64_t SecStart = getStream () .tell ();
1280
1257
const MCSectionELF *Sec = createStringTable (Ctx);
1281
- uint64_t SecEnd = OS .tell ();
1258
+ uint64_t SecEnd = getStream () .tell ();
1282
1259
SectionOffsets[Sec] = std::make_pair (SecStart, SecEnd);
1283
1260
}
1284
1261
1285
1262
uint64_t NaturalAlignment = is64Bit () ? 8 : 4 ;
1286
1263
align (NaturalAlignment);
1287
1264
1288
- const unsigned SectionHeaderOffset = OS .tell ();
1265
+ const unsigned SectionHeaderOffset = getStream () .tell ();
1289
1266
1290
1267
// ... then the section header table ...
1291
1268
writeSectionHeader (Layout, SectionIndexMap, SectionOffsets);
@@ -1301,19 +1278,19 @@ void ELFObjectWriter::writeObject(MCAssembler &Asm,
1301
1278
uint64_t Val = SectionHeaderOffset;
1302
1279
if (sys::IsLittleEndianHost != IsLittleEndian)
1303
1280
sys::swapByteOrder (Val);
1304
- OS .pwrite (reinterpret_cast <char *>(&Val), sizeof (Val),
1305
- offsetof (ELF::Elf64_Ehdr, e_shoff));
1281
+ getStream () .pwrite (reinterpret_cast <char *>(&Val), sizeof (Val),
1282
+ offsetof (ELF::Elf64_Ehdr, e_shoff));
1306
1283
NumSectionsOffset = offsetof (ELF::Elf64_Ehdr, e_shnum);
1307
1284
} else {
1308
1285
uint32_t Val = SectionHeaderOffset;
1309
1286
if (sys::IsLittleEndianHost != IsLittleEndian)
1310
1287
sys::swapByteOrder (Val);
1311
- OS .pwrite (reinterpret_cast <char *>(&Val), sizeof (Val),
1312
- offsetof (ELF::Elf32_Ehdr, e_shoff));
1288
+ getStream () .pwrite (reinterpret_cast <char *>(&Val), sizeof (Val),
1289
+ offsetof (ELF::Elf32_Ehdr, e_shoff));
1313
1290
NumSectionsOffset = offsetof (ELF::Elf32_Ehdr, e_shnum);
1314
1291
}
1315
- OS .pwrite (reinterpret_cast <char *>(&NumSections), sizeof ( NumSections),
1316
- NumSectionsOffset);
1292
+ getStream () .pwrite (reinterpret_cast <char *>(&NumSections),
1293
+ sizeof (NumSections), NumSectionsOffset);
1317
1294
}
1318
1295
1319
1296
bool ELFObjectWriter::isSymbolRefDifferenceFullyResolvedImpl (
0 commit comments