Skip to content

Commit f17b149

Browse files
committed
MC: Change object writers to use endian::Writer. NFCI.
Part of PR37466. Differential Revision: https://reviews.llvm.org/D47040 llvm-svn: 332861
1 parent 168d04d commit f17b149

File tree

7 files changed

+383
-372
lines changed

7 files changed

+383
-372
lines changed

llvm/include/llvm/MC/MCMachObjectWriter.h

+6-1
Original file line numberDiff line numberDiff line change
@@ -116,11 +116,16 @@ class MachObjectWriter : public MCObjectWriter {
116116

117117
MachSymbolData *findSymbolData(const MCSymbol &Sym);
118118

119+
void writeWithPadding(StringRef Str, uint64_t Size);
120+
119121
public:
120122
MachObjectWriter(std::unique_ptr<MCMachObjectTargetWriter> MOTW,
121123
raw_pwrite_stream &OS, bool IsLittleEndian)
122124
: MCObjectWriter(OS, IsLittleEndian),
123-
TargetObjectWriter(std::move(MOTW)) {}
125+
TargetObjectWriter(std::move(MOTW)),
126+
W(OS, IsLittleEndian ? support::little : support::big) {}
127+
128+
support::endian::Writer W;
124129

125130
const MCSymbol &findAliasedSymbol(const MCSymbol &Sym) const;
126131

llvm/lib/MC/ELFObjectWriter.cpp

+69-67
Original file line numberDiff line numberDiff line change
@@ -160,10 +160,12 @@ class ELFObjectWriter : public MCObjectWriter {
160160
bool ZLibStyle, unsigned Alignment);
161161

162162
public:
163+
support::endian::Writer W;
164+
163165
ELFObjectWriter(std::unique_ptr<MCELFObjectTargetWriter> MOTW,
164166
raw_pwrite_stream &OS, bool IsLittleEndian)
165-
: MCObjectWriter(OS, IsLittleEndian),
166-
TargetObjectWriter(std::move(MOTW)) {}
167+
: MCObjectWriter(OS, IsLittleEndian), TargetObjectWriter(std::move(MOTW)),
168+
W(OS, IsLittleEndian ? support::little : support::big) {}
167169

168170
~ELFObjectWriter() override = default;
169171

@@ -175,16 +177,15 @@ class ELFObjectWriter : public MCObjectWriter {
175177
MCObjectWriter::reset();
176178
}
177179

178-
void WriteWord(uint64_t W) {
180+
void WriteWord(uint64_t Word) {
179181
if (is64Bit())
180-
write64(W);
182+
W.write<uint64_t>(Word);
181183
else
182-
write32(W);
184+
W.write<uint32_t>(Word);
183185
}
184186

185187
template <typename T> void write(T Val) {
186-
support::endian::write(getStream(), Val,
187-
IsLittleEndian ? support::little : support::big);
188+
W.write(Val);
188189
}
189190

190191
void writeHeader(const MCAssembler &Asm);
@@ -255,8 +256,8 @@ class ELFObjectWriter : public MCObjectWriter {
255256
} // end anonymous namespace
256257

257258
void ELFObjectWriter::align(unsigned Alignment) {
258-
uint64_t Padding = OffsetToAlignment(getStream().tell(), Alignment);
259-
WriteZeros(Padding);
259+
uint64_t Padding = OffsetToAlignment(W.OS.tell(), Alignment);
260+
W.OS.write_zeros(Padding);
260261
}
261262

262263
unsigned ELFObjectWriter::addToSectionTable(const MCSectionELF *Sec) {
@@ -325,47 +326,50 @@ void ELFObjectWriter::writeHeader(const MCAssembler &Asm) {
325326
// emitWord method behaves differently for ELF32 and ELF64, writing
326327
// 4 bytes in the former and 8 in the latter.
327328

328-
writeBytes(ELF::ElfMagic); // e_ident[EI_MAG0] to e_ident[EI_MAG3]
329+
W.OS << ELF::ElfMagic; // e_ident[EI_MAG0] to e_ident[EI_MAG3]
329330

330-
write8(is64Bit() ? ELF::ELFCLASS64 : ELF::ELFCLASS32); // e_ident[EI_CLASS]
331+
W.OS << char(is64Bit() ? ELF::ELFCLASS64 : ELF::ELFCLASS32); // e_ident[EI_CLASS]
331332

332333
// e_ident[EI_DATA]
333-
write8(isLittleEndian() ? ELF::ELFDATA2LSB : ELF::ELFDATA2MSB);
334+
W.OS << char(W.Endian == support::little ? ELF::ELFDATA2LSB
335+
: ELF::ELFDATA2MSB);
334336

335-
write8(ELF::EV_CURRENT); // e_ident[EI_VERSION]
337+
W.OS << char(ELF::EV_CURRENT); // e_ident[EI_VERSION]
336338
// e_ident[EI_OSABI]
337-
write8(TargetObjectWriter->getOSABI());
338-
write8(0); // e_ident[EI_ABIVERSION]
339+
W.OS << char(TargetObjectWriter->getOSABI());
340+
W.OS << char(0); // e_ident[EI_ABIVERSION]
339341

340-
WriteZeros(ELF::EI_NIDENT - ELF::EI_PAD);
342+
W.OS.write_zeros(ELF::EI_NIDENT - ELF::EI_PAD);
341343

342-
write16(ELF::ET_REL); // e_type
344+
W.write<uint16_t>(ELF::ET_REL); // e_type
343345

344-
write16(TargetObjectWriter->getEMachine()); // e_machine = target
346+
W.write<uint16_t>(TargetObjectWriter->getEMachine()); // e_machine = target
345347

346-
write32(ELF::EV_CURRENT); // e_version
348+
W.write<uint32_t>(ELF::EV_CURRENT); // e_version
347349
WriteWord(0); // e_entry, no entry point in .o file
348350
WriteWord(0); // e_phoff, no program header for .o
349351
WriteWord(0); // e_shoff = sec hdr table off in bytes
350352

351353
// e_flags = whatever the target wants
352-
write32(Asm.getELFHeaderEFlags());
354+
W.write<uint32_t>(Asm.getELFHeaderEFlags());
353355

354356
// e_ehsize = ELF header size
355-
write16(is64Bit() ? sizeof(ELF::Elf64_Ehdr) : sizeof(ELF::Elf32_Ehdr));
357+
W.write<uint16_t>(is64Bit() ? sizeof(ELF::Elf64_Ehdr)
358+
: sizeof(ELF::Elf32_Ehdr));
356359

357-
write16(0); // e_phentsize = prog header entry size
358-
write16(0); // e_phnum = # prog header entries = 0
360+
W.write<uint16_t>(0); // e_phentsize = prog header entry size
361+
W.write<uint16_t>(0); // e_phnum = # prog header entries = 0
359362

360363
// e_shentsize = Section header entry size
361-
write16(is64Bit() ? sizeof(ELF::Elf64_Shdr) : sizeof(ELF::Elf32_Shdr));
364+
W.write<uint16_t>(is64Bit() ? sizeof(ELF::Elf64_Shdr)
365+
: sizeof(ELF::Elf32_Shdr));
362366

363367
// e_shnum = # of section header ents
364-
write16(0);
368+
W.write<uint16_t>(0);
365369

366370
// e_shstrndx = Section # of '.shstrtab'
367371
assert(StringTableIndex < ELF::SHN_LORESERVE);
368-
write16(StringTableIndex);
372+
W.write<uint16_t>(StringTableIndex);
369373
}
370374

371375
uint64_t ELFObjectWriter::SymbolValue(const MCSymbol &Sym,
@@ -784,7 +788,7 @@ void ELFObjectWriter::computeSymbolTable(
784788
SymbolTableIndex = addToSectionTable(SymtabSection);
785789

786790
align(SymtabSection->getAlignment());
787-
uint64_t SecStart = getStream().tell();
791+
uint64_t SecStart = W.OS.tell();
788792

789793
// The first entry is the undefined symbol entry.
790794
Writer.writeSymbol(0, 0, 0, 0, 0, 0, false);
@@ -900,7 +904,7 @@ void ELFObjectWriter::computeSymbolTable(
900904
assert(MSD.Symbol->getBinding() != ELF::STB_LOCAL);
901905
}
902906

903-
uint64_t SecEnd = getStream().tell();
907+
uint64_t SecEnd = W.OS.tell();
904908
SectionOffsets[SymtabSection] = std::make_pair(SecStart, SecEnd);
905909

906910
ArrayRef<uint32_t> ShndxIndexes = Writer.getShndxIndexes();
@@ -910,12 +914,12 @@ void ELFObjectWriter::computeSymbolTable(
910914
}
911915
assert(SymtabShndxSectionIndex != 0);
912916

913-
SecStart = getStream().tell();
917+
SecStart = W.OS.tell();
914918
const MCSectionELF *SymtabShndxSection =
915919
SectionTable[SymtabShndxSectionIndex - 1];
916920
for (uint32_t Index : ShndxIndexes)
917921
write(Index);
918-
SecEnd = getStream().tell();
922+
SecEnd = W.OS.tell();
919923
SectionOffsets[SymtabShndxSection] = std::make_pair(SecStart, SecEnd);
920924
}
921925

@@ -976,8 +980,8 @@ bool ELFObjectWriter::maybeWriteCompression(
976980
const StringRef Magic = "ZLIB";
977981
if (Size <= Magic.size() + sizeof(Size) + CompressedContents.size())
978982
return false;
979-
write(ArrayRef<char>(Magic.begin(), Magic.size()));
980-
writeBE64(Size);
983+
W.OS << Magic;
984+
support::endian::write(W.OS, Size, support::big);
981985
return true;
982986
}
983987

@@ -996,7 +1000,7 @@ void ELFObjectWriter::writeSectionData(const MCAssembler &Asm, MCSection &Sec,
9961000
MAI->compressDebugSections() != DebugCompressionType::None;
9971001
if (!CompressionEnabled || !SectionName.startswith(".debug_") ||
9981002
SectionName == ".debug_frame") {
999-
Asm.writeSectionData(getStream(), &Section, Layout);
1003+
Asm.writeSectionData(W.OS, &Section, Layout);
10001004
return;
10011005
}
10021006

@@ -1013,14 +1017,14 @@ void ELFObjectWriter::writeSectionData(const MCAssembler &Asm, MCSection &Sec,
10131017
StringRef(UncompressedData.data(), UncompressedData.size()),
10141018
CompressedContents)) {
10151019
consumeError(std::move(E));
1016-
getStream() << UncompressedData;
1020+
W.OS << UncompressedData;
10171021
return;
10181022
}
10191023

10201024
bool ZlibStyle = MAI->compressDebugSections() == DebugCompressionType::Z;
10211025
if (!maybeWriteCompression(UncompressedData.size(), CompressedContents,
10221026
ZlibStyle, Sec.getAlignment())) {
1023-
getStream() << UncompressedData;
1027+
W.OS << UncompressedData;
10241028
return;
10251029
}
10261030

@@ -1030,7 +1034,7 @@ void ELFObjectWriter::writeSectionData(const MCAssembler &Asm, MCSection &Sec,
10301034
else
10311035
// Add "z" prefix to section name. This is zlib-gnu style.
10321036
MC.renameELFSection(&Section, (".z" + SectionName.drop_front(1)).str());
1033-
getStream() << CompressedContents;
1037+
W.OS << CompressedContents;
10341038
}
10351039

10361040
void ELFObjectWriter::WriteSecHdrEntry(uint32_t Name, uint32_t Type,
@@ -1039,14 +1043,14 @@ void ELFObjectWriter::WriteSecHdrEntry(uint32_t Name, uint32_t Type,
10391043
uint32_t Link, uint32_t Info,
10401044
uint64_t Alignment,
10411045
uint64_t EntrySize) {
1042-
write32(Name); // sh_name: index into string table
1043-
write32(Type); // sh_type
1046+
W.write<uint32_t>(Name); // sh_name: index into string table
1047+
W.write<uint32_t>(Type); // sh_type
10441048
WriteWord(Flags); // sh_flags
10451049
WriteWord(Address); // sh_addr
10461050
WriteWord(Offset); // sh_offset
10471051
WriteWord(Size); // sh_size
1048-
write32(Link); // sh_link
1049-
write32(Info); // sh_info
1052+
W.write<uint32_t>(Link); // sh_link
1053+
W.write<uint32_t>(Info); // sh_info
10501054
WriteWord(Alignment); // sh_addralign
10511055
WriteWord(EntrySize); // sh_entsize
10521056
}
@@ -1116,7 +1120,7 @@ void ELFObjectWriter::writeRelocations(const MCAssembler &Asm,
11161120

11171121
const MCSectionELF *ELFObjectWriter::createStringTable(MCContext &Ctx) {
11181122
const MCSectionELF *StrtabSection = SectionTable[StringTableIndex - 1];
1119-
StrTabBuilder.write(getStream());
1123+
StrTabBuilder.write(W.OS);
11201124
return StrtabSection;
11211125
}
11221126

@@ -1226,12 +1230,12 @@ void ELFObjectWriter::writeObject(MCAssembler &Asm,
12261230
align(Section.getAlignment());
12271231

12281232
// Remember the offset into the file for this section.
1229-
uint64_t SecStart = getStream().tell();
1233+
uint64_t SecStart = W.OS.tell();
12301234

12311235
const MCSymbolELF *SignatureSymbol = Section.getGroup();
12321236
writeSectionData(Asm, Section, Layout);
12331237

1234-
uint64_t SecEnd = getStream().tell();
1238+
uint64_t SecEnd = W.OS.tell();
12351239
SectionOffsets[&Section] = std::make_pair(SecStart, SecEnd);
12361240

12371241
MCSectionELF *RelSection = createRelocationSection(Ctx, Section);
@@ -1263,7 +1267,7 @@ void ELFObjectWriter::writeObject(MCAssembler &Asm,
12631267
align(Group->getAlignment());
12641268

12651269
// Remember the offset into the file for this section.
1266-
uint64_t SecStart = getStream().tell();
1270+
uint64_t SecStart = W.OS.tell();
12671271

12681272
const MCSymbol *SignatureSymbol = Group->getGroup();
12691273
assert(SignatureSymbol);
@@ -1273,7 +1277,7 @@ void ELFObjectWriter::writeObject(MCAssembler &Asm,
12731277
write(SecIndex);
12741278
}
12751279

1276-
uint64_t SecEnd = getStream().tell();
1280+
uint64_t SecEnd = W.OS.tell();
12771281
SectionOffsets[Group] = std::make_pair(SecStart, SecEnd);
12781282
}
12791283

@@ -1284,54 +1288,52 @@ void ELFObjectWriter::writeObject(MCAssembler &Asm,
12841288
align(RelSection->getAlignment());
12851289

12861290
// Remember the offset into the file for this section.
1287-
uint64_t SecStart = getStream().tell();
1291+
uint64_t SecStart = W.OS.tell();
12881292

12891293
writeRelocations(Asm,
12901294
cast<MCSectionELF>(*RelSection->getAssociatedSection()));
12911295

1292-
uint64_t SecEnd = getStream().tell();
1296+
uint64_t SecEnd = W.OS.tell();
12931297
SectionOffsets[RelSection] = std::make_pair(SecStart, SecEnd);
12941298
}
12951299

12961300
{
1297-
uint64_t SecStart = getStream().tell();
1301+
uint64_t SecStart = W.OS.tell();
12981302
const MCSectionELF *Sec = createStringTable(Ctx);
1299-
uint64_t SecEnd = getStream().tell();
1303+
uint64_t SecEnd = W.OS.tell();
13001304
SectionOffsets[Sec] = std::make_pair(SecStart, SecEnd);
13011305
}
13021306

13031307
uint64_t NaturalAlignment = is64Bit() ? 8 : 4;
13041308
align(NaturalAlignment);
13051309

1306-
const uint64_t SectionHeaderOffset = getStream().tell();
1310+
const uint64_t SectionHeaderOffset = W.OS.tell();
13071311

13081312
// ... then the section header table ...
13091313
writeSectionHeader(Layout, SectionIndexMap, SectionOffsets);
13101314

1311-
uint16_t NumSections = (SectionTable.size() + 1 >= ELF::SHN_LORESERVE)
1312-
? (uint16_t)ELF::SHN_UNDEF
1313-
: SectionTable.size() + 1;
1314-
if (sys::IsLittleEndianHost != IsLittleEndian)
1315-
sys::swapByteOrder(NumSections);
1315+
uint16_t NumSections = support::endian::byte_swap<uint16_t>(
1316+
(SectionTable.size() + 1 >= ELF::SHN_LORESERVE) ? (uint16_t)ELF::SHN_UNDEF
1317+
: SectionTable.size() + 1,
1318+
W.Endian);
13161319
unsigned NumSectionsOffset;
13171320

1321+
auto &Stream = static_cast<raw_pwrite_stream &>(W.OS);
13181322
if (is64Bit()) {
1319-
uint64_t Val = SectionHeaderOffset;
1320-
if (sys::IsLittleEndianHost != IsLittleEndian)
1321-
sys::swapByteOrder(Val);
1322-
getStream().pwrite(reinterpret_cast<char *>(&Val), sizeof(Val),
1323-
offsetof(ELF::Elf64_Ehdr, e_shoff));
1323+
uint64_t Val =
1324+
support::endian::byte_swap<uint64_t>(SectionHeaderOffset, W.Endian);
1325+
Stream.pwrite(reinterpret_cast<char *>(&Val), sizeof(Val),
1326+
offsetof(ELF::Elf64_Ehdr, e_shoff));
13241327
NumSectionsOffset = offsetof(ELF::Elf64_Ehdr, e_shnum);
13251328
} else {
1326-
uint32_t Val = SectionHeaderOffset;
1327-
if (sys::IsLittleEndianHost != IsLittleEndian)
1328-
sys::swapByteOrder(Val);
1329-
getStream().pwrite(reinterpret_cast<char *>(&Val), sizeof(Val),
1330-
offsetof(ELF::Elf32_Ehdr, e_shoff));
1329+
uint32_t Val =
1330+
support::endian::byte_swap<uint32_t>(SectionHeaderOffset, W.Endian);
1331+
Stream.pwrite(reinterpret_cast<char *>(&Val), sizeof(Val),
1332+
offsetof(ELF::Elf32_Ehdr, e_shoff));
13311333
NumSectionsOffset = offsetof(ELF::Elf32_Ehdr, e_shnum);
13321334
}
1333-
getStream().pwrite(reinterpret_cast<char *>(&NumSections),
1334-
sizeof(NumSections), NumSectionsOffset);
1335+
Stream.pwrite(reinterpret_cast<char *>(&NumSections), sizeof(NumSections),
1336+
NumSectionsOffset);
13351337
}
13361338

13371339
bool ELFObjectWriter::isSymbolRefDifferenceFullyResolvedImpl(

llvm/lib/MC/MCLinkerOptimizationHint.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ void MCLOHDirective::emit_impl(raw_ostream &OutStream,
3636

3737
void MCLOHDirective::emit(MachObjectWriter &ObjWriter,
3838
const MCAsmLayout &Layout) const {
39-
raw_ostream &OutStream = ObjWriter.getStream();
39+
raw_ostream &OutStream = ObjWriter.W.OS;
4040
emit_impl(OutStream, ObjWriter, Layout);
4141
}
4242

0 commit comments

Comments
 (0)