summaryrefslogtreecommitdiff
path: root/llvm/lib/ObjectYAML
diff options
context:
space:
mode:
authorDimitry Andric <dim@FreeBSD.org>2020-07-26 19:36:28 +0000
committerDimitry Andric <dim@FreeBSD.org>2020-07-26 19:36:28 +0000
commitcfca06d7963fa0909f90483b42a6d7d194d01e08 (patch)
tree209fb2a2d68f8f277793fc8df46c753d31bc853b /llvm/lib/ObjectYAML
parent706b4fc47bbc608932d3b491ae19a3b9cde9497b (diff)
Notes
Diffstat (limited to 'llvm/lib/ObjectYAML')
-rw-r--r--llvm/lib/ObjectYAML/COFFEmitter.cpp4
-rw-r--r--llvm/lib/ObjectYAML/DWARFEmitter.cpp274
-rw-r--r--llvm/lib/ObjectYAML/DWARFVisitor.cpp29
-rw-r--r--llvm/lib/ObjectYAML/DWARFVisitor.h3
-rw-r--r--llvm/lib/ObjectYAML/DWARFYAML.cpp110
-rw-r--r--llvm/lib/ObjectYAML/ELFEmitter.cpp1035
-rw-r--r--llvm/lib/ObjectYAML/ELFYAML.cpp157
-rw-r--r--llvm/lib/ObjectYAML/MachOEmitter.cpp159
-rw-r--r--llvm/lib/ObjectYAML/MachOYAML.cpp15
-rw-r--r--llvm/lib/ObjectYAML/WasmEmitter.cpp39
-rw-r--r--llvm/lib/ObjectYAML/WasmYAML.cpp27
-rw-r--r--llvm/lib/ObjectYAML/yaml2obj.cpp4
12 files changed, 1384 insertions, 472 deletions
diff --git a/llvm/lib/ObjectYAML/COFFEmitter.cpp b/llvm/lib/ObjectYAML/COFFEmitter.cpp
index ec3ec55011f95..734e1be4b2d5c 100644
--- a/llvm/lib/ObjectYAML/COFFEmitter.cpp
+++ b/llvm/lib/ObjectYAML/COFFEmitter.cpp
@@ -187,7 +187,7 @@ toDebugS(ArrayRef<CodeViewYAML::YAMLDebugSubsection> Subsections,
std::vector<DebugSubsectionRecordBuilder> Builders;
uint32_t Size = sizeof(uint32_t);
for (auto &SS : CVSS) {
- DebugSubsectionRecordBuilder B(SS, CodeViewContainer::ObjectFile);
+ DebugSubsectionRecordBuilder B(SS);
Size += B.calculateSerializedLength();
Builders.push_back(std::move(B));
}
@@ -197,7 +197,7 @@ toDebugS(ArrayRef<CodeViewYAML::YAMLDebugSubsection> Subsections,
Err(Writer.writeInteger<uint32_t>(COFF::DEBUG_SECTION_MAGIC));
for (const auto &B : Builders) {
- Err(B.commit(Writer));
+ Err(B.commit(Writer, CodeViewContainer::ObjectFile));
}
return {Output};
}
diff --git a/llvm/lib/ObjectYAML/DWARFEmitter.cpp b/llvm/lib/ObjectYAML/DWARFEmitter.cpp
index b410fed16f09c..ed3732ba29f6c 100644
--- a/llvm/lib/ObjectYAML/DWARFEmitter.cpp
+++ b/llvm/lib/ObjectYAML/DWARFEmitter.cpp
@@ -15,12 +15,15 @@
#include "DWARFVisitor.h"
#include "llvm/ADT/StringMap.h"
#include "llvm/ADT/StringRef.h"
+#include "llvm/BinaryFormat/Dwarf.h"
#include "llvm/ObjectYAML/DWARFYAML.h"
+#include "llvm/Support/Errc.h"
#include "llvm/Support/Error.h"
#include "llvm/Support/Host.h"
#include "llvm/Support/LEB128.h"
#include "llvm/Support/MathExtras.h"
#include "llvm/Support/MemoryBuffer.h"
+#include "llvm/Support/SourceMgr.h"
#include "llvm/Support/SwapByteOrder.h"
#include "llvm/Support/YAMLTraits.h"
#include "llvm/Support/raw_ostream.h"
@@ -41,8 +44,8 @@ static void writeInteger(T Integer, raw_ostream &OS, bool IsLittleEndian) {
OS.write(reinterpret_cast<char *>(&Integer), sizeof(T));
}
-static void writeVariableSizedInteger(uint64_t Integer, size_t Size,
- raw_ostream &OS, bool IsLittleEndian) {
+static Error writeVariableSizedInteger(uint64_t Integer, size_t Size,
+ raw_ostream &OS, bool IsLittleEndian) {
if (8 == Size)
writeInteger((uint64_t)Integer, OS, IsLittleEndian);
else if (4 == Size)
@@ -52,7 +55,10 @@ static void writeVariableSizedInteger(uint64_t Integer, size_t Size,
else if (1 == Size)
writeInteger((uint8_t)Integer, OS, IsLittleEndian);
else
- assert(false && "Invalid integer write size.");
+ return createStringError(errc::not_supported,
+ "invalid integer write size: %zu", Size);
+
+ return Error::success();
}
static void ZeroFillBytes(raw_ostream &OS, size_t Size) {
@@ -68,16 +74,31 @@ static void writeInitialLength(const DWARFYAML::InitialLength &Length,
writeInteger((uint64_t)Length.TotalLength64, OS, IsLittleEndian);
}
-void DWARFYAML::EmitDebugStr(raw_ostream &OS, const DWARFYAML::Data &DI) {
+static void writeInitialLength(const dwarf::DwarfFormat Format,
+ const uint64_t Length, raw_ostream &OS,
+ bool IsLittleEndian) {
+ bool IsDWARF64 = Format == dwarf::DWARF64;
+ if (IsDWARF64)
+ cantFail(writeVariableSizedInteger(dwarf::DW_LENGTH_DWARF64, 4, OS,
+ IsLittleEndian));
+ cantFail(
+ writeVariableSizedInteger(Length, IsDWARF64 ? 8 : 4, OS, IsLittleEndian));
+}
+
+Error DWARFYAML::emitDebugStr(raw_ostream &OS, const DWARFYAML::Data &DI) {
for (auto Str : DI.DebugStrings) {
OS.write(Str.data(), Str.size());
OS.write('\0');
}
+
+ return Error::success();
}
-void DWARFYAML::EmitDebugAbbrev(raw_ostream &OS, const DWARFYAML::Data &DI) {
+Error DWARFYAML::emitDebugAbbrev(raw_ostream &OS, const DWARFYAML::Data &DI) {
+ uint64_t AbbrevCode = 0;
for (auto AbbrevDecl : DI.AbbrevDecls) {
- encodeULEB128(AbbrevDecl.Code, OS);
+ AbbrevCode = AbbrevDecl.Code ? (uint64_t)*AbbrevDecl.Code : AbbrevCode + 1;
+ encodeULEB128(AbbrevCode, OS);
encodeULEB128(AbbrevDecl.Tag, OS);
OS.write(AbbrevDecl.Children);
for (auto Attr : AbbrevDecl.Attributes) {
@@ -89,14 +110,23 @@ void DWARFYAML::EmitDebugAbbrev(raw_ostream &OS, const DWARFYAML::Data &DI) {
encodeULEB128(0, OS);
encodeULEB128(0, OS);
}
+
+ // The abbreviations for a given compilation unit end with an entry consisting
+ // of a 0 byte for the abbreviation code.
+ OS.write_zeros(1);
+
+ return Error::success();
}
-void DWARFYAML::EmitDebugAranges(raw_ostream &OS, const DWARFYAML::Data &DI) {
+Error DWARFYAML::emitDebugAranges(raw_ostream &OS, const DWARFYAML::Data &DI) {
for (auto Range : DI.ARanges) {
auto HeaderStart = OS.tell();
- writeInitialLength(Range.Length, OS, DI.IsLittleEndian);
+ writeInitialLength(Range.Format, Range.Length, OS, DI.IsLittleEndian);
writeInteger((uint16_t)Range.Version, OS, DI.IsLittleEndian);
- writeInteger((uint32_t)Range.CuOffset, OS, DI.IsLittleEndian);
+ if (Range.Format == dwarf::DWARF64)
+ writeInteger((uint64_t)Range.CuOffset, OS, DI.IsLittleEndian);
+ else
+ writeInteger((uint32_t)Range.CuOffset, OS, DI.IsLittleEndian);
writeInteger((uint8_t)Range.AddrSize, OS, DI.IsLittleEndian);
writeInteger((uint8_t)Range.SegSize, OS, DI.IsLittleEndian);
@@ -105,29 +135,73 @@ void DWARFYAML::EmitDebugAranges(raw_ostream &OS, const DWARFYAML::Data &DI) {
ZeroFillBytes(OS, FirstDescriptor - HeaderSize);
for (auto Descriptor : Range.Descriptors) {
- writeVariableSizedInteger(Descriptor.Address, Range.AddrSize, OS,
- DI.IsLittleEndian);
- writeVariableSizedInteger(Descriptor.Length, Range.AddrSize, OS,
- DI.IsLittleEndian);
+ if (Error Err = writeVariableSizedInteger(
+ Descriptor.Address, Range.AddrSize, OS, DI.IsLittleEndian))
+ return createStringError(errc::not_supported,
+ "unable to write debug_aranges address: %s",
+ toString(std::move(Err)).c_str());
+ cantFail(writeVariableSizedInteger(Descriptor.Length, Range.AddrSize, OS,
+ DI.IsLittleEndian));
}
ZeroFillBytes(OS, Range.AddrSize * 2);
}
+
+ return Error::success();
}
-void DWARFYAML::EmitPubSection(raw_ostream &OS,
- const DWARFYAML::PubSection &Sect,
- bool IsLittleEndian) {
+Error DWARFYAML::emitDebugRanges(raw_ostream &OS, const DWARFYAML::Data &DI) {
+ const size_t RangesOffset = OS.tell();
+ uint64_t EntryIndex = 0;
+ for (auto DebugRanges : DI.DebugRanges) {
+ const size_t CurrOffset = OS.tell() - RangesOffset;
+ if (DebugRanges.Offset && (uint64_t)*DebugRanges.Offset < CurrOffset)
+ return createStringError(errc::invalid_argument,
+ "'Offset' for 'debug_ranges' with index " +
+ Twine(EntryIndex) +
+ " must be greater than or equal to the "
+ "number of bytes written already (0x" +
+ Twine::utohexstr(CurrOffset) + ")");
+ if (DebugRanges.Offset)
+ ZeroFillBytes(OS, *DebugRanges.Offset - CurrOffset);
+
+ uint8_t AddrSize;
+ if (DebugRanges.AddrSize)
+ AddrSize = *DebugRanges.AddrSize;
+ else
+ AddrSize = DI.Is64BitAddrSize ? 8 : 4;
+ for (auto Entry : DebugRanges.Entries) {
+ if (Error Err = writeVariableSizedInteger(Entry.LowOffset, AddrSize, OS,
+ DI.IsLittleEndian))
+ return createStringError(
+ errc::not_supported,
+ "unable to write debug_ranges address offset: %s",
+ toString(std::move(Err)).c_str());
+ cantFail(writeVariableSizedInteger(Entry.HighOffset, AddrSize, OS,
+ DI.IsLittleEndian));
+ }
+ ZeroFillBytes(OS, AddrSize * 2);
+ ++EntryIndex;
+ }
+
+ return Error::success();
+}
+
+Error DWARFYAML::emitPubSection(raw_ostream &OS,
+ const DWARFYAML::PubSection &Sect,
+ bool IsLittleEndian, bool IsGNUPubSec) {
writeInitialLength(Sect.Length, OS, IsLittleEndian);
writeInteger((uint16_t)Sect.Version, OS, IsLittleEndian);
writeInteger((uint32_t)Sect.UnitOffset, OS, IsLittleEndian);
writeInteger((uint32_t)Sect.UnitSize, OS, IsLittleEndian);
for (auto Entry : Sect.Entries) {
writeInteger((uint32_t)Entry.DieOffset, OS, IsLittleEndian);
- if (Sect.IsGNUStyle)
- writeInteger((uint32_t)Entry.Descriptor, OS, IsLittleEndian);
+ if (IsGNUPubSec)
+ writeInteger((uint8_t)Entry.Descriptor, OS, IsLittleEndian);
OS.write(Entry.Name.data(), Entry.Name.size());
OS.write('\0');
}
+
+ return Error::success();
}
namespace {
@@ -138,14 +212,18 @@ class DumpVisitor : public DWARFYAML::ConstVisitor {
protected:
void onStartCompileUnit(const DWARFYAML::Unit &CU) override {
- writeInitialLength(CU.Length, OS, DebugInfo.IsLittleEndian);
+ writeInitialLength(CU.Format, CU.Length, OS, DebugInfo.IsLittleEndian);
writeInteger((uint16_t)CU.Version, OS, DebugInfo.IsLittleEndian);
- if(CU.Version >= 5) {
+ if (CU.Version >= 5) {
writeInteger((uint8_t)CU.Type, OS, DebugInfo.IsLittleEndian);
writeInteger((uint8_t)CU.AddrSize, OS, DebugInfo.IsLittleEndian);
- writeInteger((uint32_t)CU.AbbrOffset, OS, DebugInfo.IsLittleEndian);
- }else {
- writeInteger((uint32_t)CU.AbbrOffset, OS, DebugInfo.IsLittleEndian);
+ cantFail(writeVariableSizedInteger(CU.AbbrOffset,
+ CU.Format == dwarf::DWARF64 ? 8 : 4,
+ OS, DebugInfo.IsLittleEndian));
+ } else {
+ cantFail(writeVariableSizedInteger(CU.AbbrOffset,
+ CU.Format == dwarf::DWARF64 ? 8 : 4,
+ OS, DebugInfo.IsLittleEndian));
writeInteger((uint8_t)CU.AddrSize, OS, DebugInfo.IsLittleEndian);
}
}
@@ -196,12 +274,12 @@ public:
};
} // namespace
-void DWARFYAML::EmitDebugInfo(raw_ostream &OS, const DWARFYAML::Data &DI) {
+Error DWARFYAML::emitDebugInfo(raw_ostream &OS, const DWARFYAML::Data &DI) {
DumpVisitor Visitor(DI, OS);
- Visitor.traverseDebugInfo();
+ return Visitor.traverseDebugInfo();
}
-static void EmitFileEntry(raw_ostream &OS, const DWARFYAML::File &File) {
+static void emitFileEntry(raw_ostream &OS, const DWARFYAML::File &File) {
OS.write(File.Name.data(), File.Name.size());
OS.write('\0');
encodeULEB128(File.DirIdx, OS);
@@ -209,13 +287,14 @@ static void EmitFileEntry(raw_ostream &OS, const DWARFYAML::File &File) {
encodeULEB128(File.Length, OS);
}
-void DWARFYAML::EmitDebugLine(raw_ostream &OS, const DWARFYAML::Data &DI) {
+Error DWARFYAML::emitDebugLine(raw_ostream &OS, const DWARFYAML::Data &DI) {
for (const auto &LineTable : DI.DebugLines) {
- writeInitialLength(LineTable.Length, OS, DI.IsLittleEndian);
- uint64_t SizeOfPrologueLength = LineTable.Length.isDWARF64() ? 8 : 4;
+ writeInitialLength(LineTable.Format, LineTable.Length, OS,
+ DI.IsLittleEndian);
+ uint64_t SizeOfPrologueLength = LineTable.Format == dwarf::DWARF64 ? 8 : 4;
writeInteger((uint16_t)LineTable.Version, OS, DI.IsLittleEndian);
- writeVariableSizedInteger(LineTable.PrologueLength, SizeOfPrologueLength,
- OS, DI.IsLittleEndian);
+ cantFail(writeVariableSizedInteger(
+ LineTable.PrologueLength, SizeOfPrologueLength, OS, DI.IsLittleEndian));
writeInteger((uint8_t)LineTable.MinInstLength, OS, DI.IsLittleEndian);
if (LineTable.Version >= 4)
writeInteger((uint8_t)LineTable.MaxOpsPerInst, OS, DI.IsLittleEndian);
@@ -234,7 +313,7 @@ void DWARFYAML::EmitDebugLine(raw_ostream &OS, const DWARFYAML::Data &DI) {
OS.write('\0');
for (auto File : LineTable.Files)
- EmitFileEntry(OS, File);
+ emitFileEntry(OS, File);
OS.write('\0');
for (auto Op : LineTable.Opcodes) {
@@ -245,11 +324,13 @@ void DWARFYAML::EmitDebugLine(raw_ostream &OS, const DWARFYAML::Data &DI) {
switch (Op.SubOpcode) {
case dwarf::DW_LNE_set_address:
case dwarf::DW_LNE_set_discriminator:
- writeVariableSizedInteger(Op.Data, DI.CompileUnits[0].AddrSize, OS,
- DI.IsLittleEndian);
+ // TODO: Test this error.
+ if (Error Err = writeVariableSizedInteger(
+ Op.Data, DI.CompileUnits[0].AddrSize, OS, DI.IsLittleEndian))
+ return Err;
break;
case dwarf::DW_LNE_define_file:
- EmitFileEntry(OS, Op.FileEntry);
+ emitFileEntry(OS, Op.FileEntry);
break;
case dwarf::DW_LNE_end_sequence:
break;
@@ -290,20 +371,66 @@ void DWARFYAML::EmitDebugLine(raw_ostream &OS, const DWARFYAML::Data &DI) {
}
}
}
+
+ return Error::success();
+}
+
+Error DWARFYAML::emitDebugAddr(raw_ostream &OS, const Data &DI) {
+ for (const AddrTableEntry &TableEntry : DI.DebugAddr) {
+ uint8_t AddrSize;
+ if (TableEntry.AddrSize)
+ AddrSize = *TableEntry.AddrSize;
+ else
+ AddrSize = DI.Is64BitAddrSize ? 8 : 4;
+
+ uint64_t Length;
+ if (TableEntry.Length)
+ Length = (uint64_t)*TableEntry.Length;
+ else
+ // 2 (version) + 1 (address_size) + 1 (segment_selector_size) = 4
+ Length = 4 + (AddrSize + TableEntry.SegSelectorSize) *
+ TableEntry.SegAddrPairs.size();
+
+ writeInitialLength(TableEntry.Format, Length, OS, DI.IsLittleEndian);
+ writeInteger((uint16_t)TableEntry.Version, OS, DI.IsLittleEndian);
+ writeInteger((uint8_t)AddrSize, OS, DI.IsLittleEndian);
+ writeInteger((uint8_t)TableEntry.SegSelectorSize, OS, DI.IsLittleEndian);
+
+ for (const SegAddrPair &Pair : TableEntry.SegAddrPairs) {
+ if (TableEntry.SegSelectorSize != 0)
+ if (Error Err = writeVariableSizedInteger(Pair.Segment,
+ TableEntry.SegSelectorSize,
+ OS, DI.IsLittleEndian))
+ return createStringError(errc::not_supported,
+ "unable to write debug_addr segment: %s",
+ toString(std::move(Err)).c_str());
+ if (AddrSize != 0)
+ if (Error Err = writeVariableSizedInteger(Pair.Address, AddrSize, OS,
+ DI.IsLittleEndian))
+ return createStringError(errc::not_supported,
+ "unable to write debug_addr address: %s",
+ toString(std::move(Err)).c_str());
+ }
+ }
+
+ return Error::success();
}
-using EmitFuncType = void (*)(raw_ostream &, const DWARFYAML::Data &);
+using EmitFuncType = Error (*)(raw_ostream &, const DWARFYAML::Data &);
-static void
-EmitDebugSectionImpl(const DWARFYAML::Data &DI, EmitFuncType EmitFunc,
+static Error
+emitDebugSectionImpl(const DWARFYAML::Data &DI, EmitFuncType EmitFunc,
StringRef Sec,
StringMap<std::unique_ptr<MemoryBuffer>> &OutputBuffers) {
std::string Data;
raw_string_ostream DebugInfoStream(Data);
- EmitFunc(DebugInfoStream, DI);
+ if (Error Err = EmitFunc(DebugInfoStream, DI))
+ return Err;
DebugInfoStream.flush();
if (!Data.empty())
OutputBuffers[Sec] = MemoryBuffer::getMemBufferCopy(Data);
+
+ return Error::success();
}
namespace {
@@ -313,69 +440,84 @@ class DIEFixupVisitor : public DWARFYAML::Visitor {
public:
DIEFixupVisitor(DWARFYAML::Data &DI) : DWARFYAML::Visitor(DI){};
-private:
- virtual void onStartCompileUnit(DWARFYAML::Unit &CU) {
+protected:
+ void onStartCompileUnit(DWARFYAML::Unit &CU) override {
// Size of the unit header, excluding the length field itself.
Length = CU.Version >= 5 ? 8 : 7;
}
- virtual void onEndCompileUnit(DWARFYAML::Unit &CU) {
- CU.Length.setLength(Length);
- }
+ void onEndCompileUnit(DWARFYAML::Unit &CU) override { CU.Length = Length; }
- virtual void onStartDIE(DWARFYAML::Unit &CU, DWARFYAML::Entry &DIE) {
+ void onStartDIE(DWARFYAML::Unit &CU, DWARFYAML::Entry &DIE) override {
Length += getULEB128Size(DIE.AbbrCode);
}
- virtual void onValue(const uint8_t U) { Length += 1; }
- virtual void onValue(const uint16_t U) { Length += 2; }
- virtual void onValue(const uint32_t U) { Length += 4; }
- virtual void onValue(const uint64_t U, const bool LEB = false) {
+ void onValue(const uint8_t U) override { Length += 1; }
+ void onValue(const uint16_t U) override { Length += 2; }
+ void onValue(const uint32_t U) override { Length += 4; }
+ void onValue(const uint64_t U, const bool LEB = false) override {
if (LEB)
Length += getULEB128Size(U);
else
Length += 8;
}
- virtual void onValue(const int64_t S, const bool LEB = false) {
+ void onValue(const int64_t S, const bool LEB = false) override {
if (LEB)
Length += getSLEB128Size(S);
else
Length += 8;
}
- virtual void onValue(const StringRef String) { Length += String.size() + 1; }
+ void onValue(const StringRef String) override { Length += String.size() + 1; }
- virtual void onValue(const MemoryBufferRef MBR) {
+ void onValue(const MemoryBufferRef MBR) override {
Length += MBR.getBufferSize();
}
};
} // namespace
Expected<StringMap<std::unique_ptr<MemoryBuffer>>>
-DWARFYAML::EmitDebugSections(StringRef YAMLString, bool ApplyFixups,
+DWARFYAML::emitDebugSections(StringRef YAMLString, bool ApplyFixups,
bool IsLittleEndian) {
- yaml::Input YIn(YAMLString);
+ auto CollectDiagnostic = [](const SMDiagnostic &Diag, void *DiagContext) {
+ *static_cast<SMDiagnostic *>(DiagContext) = Diag;
+ };
+
+ SMDiagnostic GeneratedDiag;
+ yaml::Input YIn(YAMLString, /*Ctxt=*/nullptr, CollectDiagnostic,
+ &GeneratedDiag);
DWARFYAML::Data DI;
DI.IsLittleEndian = IsLittleEndian;
YIn >> DI;
if (YIn.error())
- return errorCodeToError(YIn.error());
+ return createStringError(YIn.error(), GeneratedDiag.getMessage());
if (ApplyFixups) {
DIEFixupVisitor DIFixer(DI);
- DIFixer.traverseDebugInfo();
+ if (Error Err = DIFixer.traverseDebugInfo())
+ return std::move(Err);
}
StringMap<std::unique_ptr<MemoryBuffer>> DebugSections;
- EmitDebugSectionImpl(DI, &DWARFYAML::EmitDebugInfo, "debug_info",
- DebugSections);
- EmitDebugSectionImpl(DI, &DWARFYAML::EmitDebugLine, "debug_line",
- DebugSections);
- EmitDebugSectionImpl(DI, &DWARFYAML::EmitDebugStr, "debug_str",
- DebugSections);
- EmitDebugSectionImpl(DI, &DWARFYAML::EmitDebugAbbrev, "debug_abbrev",
- DebugSections);
- EmitDebugSectionImpl(DI, &DWARFYAML::EmitDebugAranges, "debug_aranges",
- DebugSections);
+ Error Err = emitDebugSectionImpl(DI, &DWARFYAML::emitDebugInfo, "debug_info",
+ DebugSections);
+ Err = joinErrors(std::move(Err),
+ emitDebugSectionImpl(DI, &DWARFYAML::emitDebugLine,
+ "debug_line", DebugSections));
+ Err = joinErrors(std::move(Err),
+ emitDebugSectionImpl(DI, &DWARFYAML::emitDebugStr,
+ "debug_str", DebugSections));
+ Err = joinErrors(std::move(Err),
+ emitDebugSectionImpl(DI, &DWARFYAML::emitDebugAbbrev,
+ "debug_abbrev", DebugSections));
+ Err = joinErrors(std::move(Err),
+ emitDebugSectionImpl(DI, &DWARFYAML::emitDebugAranges,
+ "debug_aranges", DebugSections));
+ Err = joinErrors(std::move(Err),
+ emitDebugSectionImpl(DI, &DWARFYAML::emitDebugRanges,
+ "debug_ranges", DebugSections));
+
+ if (Err)
+ return std::move(Err);
return std::move(DebugSections);
}
diff --git a/llvm/lib/ObjectYAML/DWARFVisitor.cpp b/llvm/lib/ObjectYAML/DWARFVisitor.cpp
index ecb5967ac532d..a2dd37b5fe324 100644
--- a/llvm/lib/ObjectYAML/DWARFVisitor.cpp
+++ b/llvm/lib/ObjectYAML/DWARFVisitor.cpp
@@ -9,7 +9,10 @@
//===----------------------------------------------------------------------===//
#include "DWARFVisitor.h"
+#include "llvm/BinaryFormat/Dwarf.h"
#include "llvm/ObjectYAML/DWARFYAML.h"
+#include "llvm/Support/Errc.h"
+#include "llvm/Support/Error.h"
using namespace llvm;
@@ -34,7 +37,7 @@ void DWARFYAML::VisitorImpl<T>::onVariableSizeValue(uint64_t U, unsigned Size) {
}
static unsigned getOffsetSize(const DWARFYAML::Unit &Unit) {
- return Unit.Length.isDWARF64() ? 8 : 4;
+ return Unit.Format == dwarf::DWARF64 ? 8 : 4;
}
static unsigned getRefSize(const DWARFYAML::Unit &Unit) {
@@ -43,16 +46,24 @@ static unsigned getRefSize(const DWARFYAML::Unit &Unit) {
return getOffsetSize(Unit);
}
-template <typename T> void DWARFYAML::VisitorImpl<T>::traverseDebugInfo() {
+template <typename T> Error DWARFYAML::VisitorImpl<T>::traverseDebugInfo() {
for (auto &Unit : DebugInfo.CompileUnits) {
onStartCompileUnit(Unit);
- auto FirstAbbrevCode = Unit.Entries[0].AbbrCode;
+ if (Unit.Entries.empty())
+ continue;
for (auto &Entry : Unit.Entries) {
onStartDIE(Unit, Entry);
- if (Entry.AbbrCode == 0u)
+ uint32_t AbbrCode = Entry.AbbrCode;
+ if (AbbrCode == 0 || Entry.Values.empty())
continue;
- auto &Abbrev = DebugInfo.AbbrevDecls[Entry.AbbrCode - FirstAbbrevCode];
+
+ if (AbbrCode > DebugInfo.AbbrevDecls.size())
+ return createStringError(
+ errc::invalid_argument,
+ "abbrev code must be less than or equal to the number of "
+ "entries in abbreviation table");
+ const DWARFYAML::Abbrev &Abbrev = DebugInfo.AbbrevDecls[AbbrCode - 1];
auto FormVal = Entry.Values.begin();
auto AbbrForm = Abbrev.Attributes.begin();
for (;
@@ -105,6 +116,12 @@ template <typename T> void DWARFYAML::VisitorImpl<T>::traverseDebugInfo() {
""));
break;
}
+ case dwarf::DW_FORM_strx:
+ case dwarf::DW_FORM_addrx:
+ case dwarf::DW_FORM_rnglistx:
+ case dwarf::DW_FORM_loclistx:
+ onValue((uint64_t)FormVal->Value, /*LEB=*/true);
+ break;
case dwarf::DW_FORM_data1:
case dwarf::DW_FORM_ref1:
case dwarf::DW_FORM_flag:
@@ -170,6 +187,8 @@ template <typename T> void DWARFYAML::VisitorImpl<T>::traverseDebugInfo() {
}
onEndCompileUnit(Unit);
}
+
+ return Error::success();
}
// Explicitly instantiate the two template expansions.
diff --git a/llvm/lib/ObjectYAML/DWARFVisitor.h b/llvm/lib/ObjectYAML/DWARFVisitor.h
index 50e88aa7a26bf..3b2c4303c7f7f 100644
--- a/llvm/lib/ObjectYAML/DWARFVisitor.h
+++ b/llvm/lib/ObjectYAML/DWARFVisitor.h
@@ -16,6 +16,7 @@
#include "llvm/Support/MemoryBuffer.h"
namespace llvm {
+class Error;
namespace DWARFYAML {
@@ -68,7 +69,7 @@ public:
virtual ~VisitorImpl() {}
- void traverseDebugInfo();
+ Error traverseDebugInfo();
private:
void onVariableSizeValue(uint64_t U, unsigned Size);
diff --git a/llvm/lib/ObjectYAML/DWARFYAML.cpp b/llvm/lib/ObjectYAML/DWARFYAML.cpp
index bb3b1422eb62d..bedf31dc8179f 100644
--- a/llvm/lib/ObjectYAML/DWARFYAML.cpp
+++ b/llvm/lib/ObjectYAML/DWARFYAML.cpp
@@ -12,38 +12,69 @@
//===----------------------------------------------------------------------===//
#include "llvm/ObjectYAML/DWARFYAML.h"
+#include "llvm/BinaryFormat/Dwarf.h"
namespace llvm {
bool DWARFYAML::Data::isEmpty() const {
- return 0 == DebugStrings.size() + AbbrevDecls.size();
+ return DebugStrings.empty() && AbbrevDecls.empty() && ARanges.empty() &&
+ DebugRanges.empty() && !PubNames && !PubTypes && !GNUPubNames &&
+ !GNUPubTypes && CompileUnits.empty() && DebugLines.empty();
+}
+
+SetVector<StringRef> DWARFYAML::Data::getUsedSectionNames() const {
+ SetVector<StringRef> SecNames;
+ if (!DebugStrings.empty())
+ SecNames.insert("debug_str");
+ if (!ARanges.empty())
+ SecNames.insert("debug_aranges");
+ if (!DebugRanges.empty())
+ SecNames.insert("debug_ranges");
+ if (!DebugLines.empty())
+ SecNames.insert("debug_line");
+ if (!DebugAddr.empty())
+ SecNames.insert("debug_addr");
+ if (!AbbrevDecls.empty())
+ SecNames.insert("debug_abbrev");
+ if (!CompileUnits.empty())
+ SecNames.insert("debug_info");
+ if (PubNames)
+ SecNames.insert("debug_pubnames");
+ if (PubTypes)
+ SecNames.insert("debug_pubtypes");
+ if (GNUPubNames)
+ SecNames.insert("debug_gnu_pubnames");
+ if (GNUPubTypes)
+ SecNames.insert("debug_gnu_pubtypes");
+ return SecNames;
}
namespace yaml {
void MappingTraits<DWARFYAML::Data>::mapping(IO &IO, DWARFYAML::Data &DWARF) {
- auto oldContext = IO.getContext();
- IO.setContext(&DWARF);
+ void *OldContext = IO.getContext();
+ DWARFYAML::DWARFContext DWARFCtx;
+ IO.setContext(&DWARFCtx);
IO.mapOptional("debug_str", DWARF.DebugStrings);
IO.mapOptional("debug_abbrev", DWARF.AbbrevDecls);
if (!DWARF.ARanges.empty() || !IO.outputting())
IO.mapOptional("debug_aranges", DWARF.ARanges);
- if (!DWARF.PubNames.Entries.empty() || !IO.outputting())
- IO.mapOptional("debug_pubnames", DWARF.PubNames);
- if (!DWARF.PubTypes.Entries.empty() || !IO.outputting())
- IO.mapOptional("debug_pubtypes", DWARF.PubTypes);
- if (!DWARF.GNUPubNames.Entries.empty() || !IO.outputting())
- IO.mapOptional("debug_gnu_pubnames", DWARF.GNUPubNames);
- if (!DWARF.GNUPubTypes.Entries.empty() || !IO.outputting())
- IO.mapOptional("debug_gnu_pubtypes", DWARF.GNUPubTypes);
+ if (!DWARF.DebugRanges.empty() || !IO.outputting())
+ IO.mapOptional("debug_ranges", DWARF.DebugRanges);
+ IO.mapOptional("debug_pubnames", DWARF.PubNames);
+ IO.mapOptional("debug_pubtypes", DWARF.PubTypes);
+ DWARFCtx.IsGNUPubSec = true;
+ IO.mapOptional("debug_gnu_pubnames", DWARF.GNUPubNames);
+ IO.mapOptional("debug_gnu_pubtypes", DWARF.GNUPubTypes);
IO.mapOptional("debug_info", DWARF.CompileUnits);
IO.mapOptional("debug_line", DWARF.DebugLines);
- IO.setContext(&oldContext);
+ IO.mapOptional("debug_addr", DWARF.DebugAddr);
+ IO.setContext(OldContext);
}
void MappingTraits<DWARFYAML::Abbrev>::mapping(IO &IO,
DWARFYAML::Abbrev &Abbrev) {
- IO.mapRequired("Code", Abbrev.Code);
+ IO.mapOptional("Code", Abbrev.Code);
IO.mapRequired("Tag", Abbrev.Tag);
IO.mapRequired("Children", Abbrev.Children);
IO.mapRequired("Attributes", Abbrev.Attributes);
@@ -64,38 +95,48 @@ void MappingTraits<DWARFYAML::ARangeDescriptor>::mapping(
}
void MappingTraits<DWARFYAML::ARange>::mapping(IO &IO,
- DWARFYAML::ARange &Range) {
- IO.mapRequired("Length", Range.Length);
- IO.mapRequired("Version", Range.Version);
- IO.mapRequired("CuOffset", Range.CuOffset);
- IO.mapRequired("AddrSize", Range.AddrSize);
- IO.mapRequired("SegSize", Range.SegSize);
- IO.mapRequired("Descriptors", Range.Descriptors);
+ DWARFYAML::ARange &ARange) {
+ IO.mapOptional("Format", ARange.Format, dwarf::DWARF32);
+ IO.mapRequired("Length", ARange.Length);
+ IO.mapRequired("Version", ARange.Version);
+ IO.mapRequired("CuOffset", ARange.CuOffset);
+ IO.mapRequired("AddrSize", ARange.AddrSize);
+ IO.mapRequired("SegSize", ARange.SegSize);
+ IO.mapRequired("Descriptors", ARange.Descriptors);
+}
+
+void MappingTraits<DWARFYAML::RangeEntry>::mapping(
+ IO &IO, DWARFYAML::RangeEntry &Descriptor) {
+ IO.mapRequired("LowOffset", Descriptor.LowOffset);
+ IO.mapRequired("HighOffset", Descriptor.HighOffset);
+}
+
+void MappingTraits<DWARFYAML::Ranges>::mapping(IO &IO,
+ DWARFYAML::Ranges &DebugRanges) {
+ IO.mapOptional("Offset", DebugRanges.Offset);
+ IO.mapOptional("AddrSize", DebugRanges.AddrSize);
+ IO.mapRequired("Entries", DebugRanges.Entries);
}
void MappingTraits<DWARFYAML::PubEntry>::mapping(IO &IO,
DWARFYAML::PubEntry &Entry) {
IO.mapRequired("DieOffset", Entry.DieOffset);
- if (reinterpret_cast<DWARFYAML::PubSection *>(IO.getContext())->IsGNUStyle)
+ if (static_cast<DWARFYAML::DWARFContext *>(IO.getContext())->IsGNUPubSec)
IO.mapRequired("Descriptor", Entry.Descriptor);
IO.mapRequired("Name", Entry.Name);
}
void MappingTraits<DWARFYAML::PubSection>::mapping(
IO &IO, DWARFYAML::PubSection &Section) {
- auto OldContext = IO.getContext();
- IO.setContext(&Section);
-
IO.mapRequired("Length", Section.Length);
IO.mapRequired("Version", Section.Version);
IO.mapRequired("UnitOffset", Section.UnitOffset);
IO.mapRequired("UnitSize", Section.UnitSize);
IO.mapRequired("Entries", Section.Entries);
-
- IO.setContext(OldContext);
}
void MappingTraits<DWARFYAML::Unit>::mapping(IO &IO, DWARFYAML::Unit &Unit) {
+ IO.mapOptional("Format", Unit.Format, dwarf::DWARF32);
IO.mapRequired("Length", Unit.Length);
IO.mapRequired("Version", Unit.Version);
if (Unit.Version >= 5)
@@ -147,6 +188,7 @@ void MappingTraits<DWARFYAML::LineTableOpcode>::mapping(
void MappingTraits<DWARFYAML::LineTable>::mapping(
IO &IO, DWARFYAML::LineTable &LineTable) {
+ IO.mapOptional("Format", LineTable.Format, dwarf::DWARF32);
IO.mapRequired("Length", LineTable.Length);
IO.mapRequired("Version", LineTable.Version);
IO.mapRequired("PrologueLength", LineTable.PrologueLength);
@@ -163,6 +205,22 @@ void MappingTraits<DWARFYAML::LineTable>::mapping(
IO.mapRequired("Opcodes", LineTable.Opcodes);
}
+void MappingTraits<DWARFYAML::SegAddrPair>::mapping(
+ IO &IO, DWARFYAML::SegAddrPair &SegAddrPair) {
+ IO.mapOptional("Segment", SegAddrPair.Segment, 0);
+ IO.mapOptional("Address", SegAddrPair.Address, 0);
+}
+
+void MappingTraits<DWARFYAML::AddrTableEntry>::mapping(
+ IO &IO, DWARFYAML::AddrTableEntry &AddrTable) {
+ IO.mapOptional("Format", AddrTable.Format, dwarf::DWARF32);
+ IO.mapOptional("Length", AddrTable.Length);
+ IO.mapRequired("Version", AddrTable.Version);
+ IO.mapOptional("AddressSize", AddrTable.AddrSize);
+ IO.mapOptional("SegmentSelectorSize", AddrTable.SegSelectorSize, 0);
+ IO.mapOptional("Entries", AddrTable.SegAddrPairs);
+}
+
void MappingTraits<DWARFYAML::InitialLength>::mapping(
IO &IO, DWARFYAML::InitialLength &InitialLength) {
IO.mapRequired("TotalLength", InitialLength.TotalLength);
diff --git a/llvm/lib/ObjectYAML/ELFEmitter.cpp b/llvm/lib/ObjectYAML/ELFEmitter.cpp
index ee7d5f616a737..f9f2f128e2e82 100644
--- a/llvm/lib/ObjectYAML/ELFEmitter.cpp
+++ b/llvm/lib/ObjectYAML/ELFEmitter.cpp
@@ -13,13 +13,18 @@
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/DenseMap.h"
+#include "llvm/ADT/SetVector.h"
#include "llvm/ADT/StringSet.h"
#include "llvm/BinaryFormat/ELF.h"
#include "llvm/MC/StringTableBuilder.h"
#include "llvm/Object/ELFObjectFile.h"
+#include "llvm/ObjectYAML/DWARFEmitter.h"
+#include "llvm/ObjectYAML/DWARFYAML.h"
#include "llvm/ObjectYAML/ELFYAML.h"
#include "llvm/ObjectYAML/yaml2obj.h"
#include "llvm/Support/EndianStream.h"
+#include "llvm/Support/Errc.h"
+#include "llvm/Support/Error.h"
#include "llvm/Support/LEB128.h"
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/WithColor.h"
@@ -31,33 +36,94 @@ using namespace llvm;
// This class is used to build up a contiguous binary blob while keeping
// track of an offset in the output (which notionally begins at
// `InitialOffset`).
+// The blob might be limited to an arbitrary size. All attempts to write data
+// are ignored and the error condition is remembered once the limit is reached.
+// Such an approach allows us to simplify the code by delaying error reporting
+// and doing it at a convenient time.
namespace {
class ContiguousBlobAccumulator {
const uint64_t InitialOffset;
+ const uint64_t MaxSize;
+
SmallVector<char, 128> Buf;
raw_svector_ostream OS;
+ Error ReachedLimitErr = Error::success();
+
+ bool checkLimit(uint64_t Size) {
+ if (!ReachedLimitErr && getOffset() + Size <= MaxSize)
+ return true;
+ if (!ReachedLimitErr)
+ ReachedLimitErr = createStringError(errc::invalid_argument,
+ "reached the output size limit");
+ return false;
+ }
public:
- ContiguousBlobAccumulator(uint64_t InitialOffset_)
- : InitialOffset(InitialOffset_), Buf(), OS(Buf) {}
+ ContiguousBlobAccumulator(uint64_t BaseOffset, uint64_t SizeLimit)
+ : InitialOffset(BaseOffset), MaxSize(SizeLimit), OS(Buf) {}
+
+ uint64_t tell() const { return OS.tell(); }
+ uint64_t getOffset() const { return InitialOffset + OS.tell(); }
+ void writeBlobToStream(raw_ostream &Out) const { Out << OS.str(); }
- template <class Integer>
- raw_ostream &getOSAndAlignedOffset(Integer &Offset, unsigned Align) {
- Offset = padToAlignment(Align);
- return OS;
+ Error takeLimitError() {
+ // Request to write 0 bytes to check we did not reach the limit.
+ checkLimit(0);
+ return std::move(ReachedLimitErr);
}
/// \returns The new offset.
uint64_t padToAlignment(unsigned Align) {
- if (Align == 0)
- Align = 1;
- uint64_t CurrentOffset = InitialOffset + OS.tell();
- uint64_t AlignedOffset = alignTo(CurrentOffset, Align);
- OS.write_zeros(AlignedOffset - CurrentOffset);
- return AlignedOffset; // == CurrentOffset;
+ uint64_t CurrentOffset = getOffset();
+ if (ReachedLimitErr)
+ return CurrentOffset;
+
+ uint64_t AlignedOffset = alignTo(CurrentOffset, Align == 0 ? 1 : Align);
+ uint64_t PaddingSize = AlignedOffset - CurrentOffset;
+ if (!checkLimit(PaddingSize))
+ return CurrentOffset;
+
+ writeZeros(PaddingSize);
+ return AlignedOffset;
+ }
+
+ raw_ostream *getRawOS(uint64_t Size) {
+ if (checkLimit(Size))
+ return &OS;
+ return nullptr;
+ }
+
+ void writeAsBinary(const yaml::BinaryRef &Bin, uint64_t N = UINT64_MAX) {
+ if (!checkLimit(Bin.binary_size()))
+ return;
+ Bin.writeAsBinary(OS, N);
+ }
+
+ void writeZeros(uint64_t Num) {
+ if (checkLimit(Num))
+ OS.write_zeros(Num);
+ }
+
+ void write(const char *Ptr, size_t Size) {
+ if (checkLimit(Size))
+ OS.write(Ptr, Size);
+ }
+
+ void write(unsigned char C) {
+ if (checkLimit(1))
+ OS.write(C);
+ }
+
+ unsigned writeULEB128(uint64_t Val) {
+ if (!checkLimit(sizeof(uint64_t)))
+ return 0;
+ return encodeULEB128(Val, OS);
}
- void writeBlobToStream(raw_ostream &Out) { Out << OS.str(); }
+ template <typename T> void write(T Val, support::endianness E) {
+ if (checkLimit(sizeof(T)))
+ support::endian::write<T>(OS, Val, E);
+ }
};
// Used to keep track of section and symbol names, so that in the YAML file
@@ -128,9 +194,13 @@ template <class ELFT> class ELFState {
NameToIdxMap DynSymN2I;
ELFYAML::Object &Doc;
+ StringSet<> ExcludedSectionHeaders;
+
+ uint64_t LocationCounter = 0;
bool HasError = false;
yaml::ErrorHandler ErrHandler;
void reportError(const Twine &Msg);
+ void reportError(Error Err);
std::vector<Elf_Sym> toELFSymbols(ArrayRef<ELFYAML::Symbol> Symbols,
const StringTableBuilder &Strtab);
@@ -151,6 +221,9 @@ template <class ELFT> class ELFState {
StringTableBuilder &STB,
ContiguousBlobAccumulator &CBA,
ELFYAML::Section *YAMLSec);
+ void initDWARFSectionHeader(Elf_Shdr &SHeader, StringRef Name,
+ ContiguousBlobAccumulator &CBA,
+ ELFYAML::Section *YAMLSec);
void setProgramHeaderLayout(std::vector<Elf_Phdr> &PHeaders,
std::vector<Elf_Shdr> &SHeaders);
@@ -159,7 +232,10 @@ template <class ELFT> class ELFState {
ArrayRef<typename ELFT::Shdr> SHeaders);
void finalizeStrings();
- void writeELFHeader(ContiguousBlobAccumulator &CBA, raw_ostream &OS);
+ void writeELFHeader(raw_ostream &OS, uint64_t SHOff);
+ void writeSectionContent(Elf_Shdr &SHeader,
+ const ELFYAML::NoBitsSection &Section,
+ ContiguousBlobAccumulator &CBA);
void writeSectionContent(Elf_Shdr &SHeader,
const ELFYAML::RawContentSection &Section,
ContiguousBlobAccumulator &CBA);
@@ -210,14 +286,27 @@ template <class ELFT> class ELFState {
void writeSectionContent(Elf_Shdr &SHeader,
const ELFYAML::DependentLibrariesSection &Section,
ContiguousBlobAccumulator &CBA);
+ void writeSectionContent(Elf_Shdr &SHeader,
+ const ELFYAML::CallGraphProfileSection &Section,
+ ContiguousBlobAccumulator &CBA);
void writeFill(ELFYAML::Fill &Fill, ContiguousBlobAccumulator &CBA);
ELFState(ELFYAML::Object &D, yaml::ErrorHandler EH);
+ void assignSectionAddress(Elf_Shdr &SHeader, ELFYAML::Section *YAMLSec);
+
+ DenseMap<StringRef, size_t> buildSectionHeaderReorderMap();
+
+ BumpPtrAllocator StringAlloc;
+ uint64_t alignToOffset(ContiguousBlobAccumulator &CBA, uint64_t Align,
+ llvm::Optional<llvm::yaml::Hex64> Offset);
+
+ uint64_t getSectionNameOffset(StringRef Name);
+
public:
static bool writeELF(raw_ostream &OS, ELFYAML::Object &Doc,
- yaml::ErrorHandler EH);
+ yaml::ErrorHandler EH, uint64_t MaxSize);
};
} // end anonymous namespace
@@ -235,11 +324,6 @@ template <class ELFT>
ELFState<ELFT>::ELFState(ELFYAML::Object &D, yaml::ErrorHandler EH)
: Doc(D), ErrHandler(EH) {
std::vector<ELFYAML::Section *> Sections = Doc.getSections();
- StringSet<> DocSections;
- for (const ELFYAML::Section *Sec : Sections)
- if (!Sec->Name.empty())
- DocSections.insert(Sec->Name);
-
// Insert SHT_NULL section implicitly when it is not defined in YAML.
if (Sections.empty() || Sections.front()->Type != ELF::SHT_NULL)
Doc.Chunks.insert(
@@ -247,14 +331,36 @@ ELFState<ELFT>::ELFState(ELFYAML::Object &D, yaml::ErrorHandler EH)
std::make_unique<ELFYAML::Section>(
ELFYAML::Chunk::ChunkKind::RawContent, /*IsImplicit=*/true));
+ // We add a technical suffix for each unnamed section/fill. It does not affect
+ // the output, but allows us to map them by name in the code and report better
+ // error messages.
+ StringSet<> DocSections;
+ for (size_t I = 0; I < Doc.Chunks.size(); ++I) {
+ const std::unique_ptr<ELFYAML::Chunk> &C = Doc.Chunks[I];
+ if (C->Name.empty()) {
+ std::string NewName = ELFYAML::appendUniqueSuffix(
+ /*Name=*/"", "index " + Twine(I));
+ C->Name = StringRef(NewName).copy(StringAlloc);
+ assert(ELFYAML::dropUniqueSuffix(C->Name).empty());
+ }
+
+ if (!DocSections.insert(C->Name).second)
+ reportError("repeated section/fill name: '" + C->Name +
+ "' at YAML section/fill number " + Twine(I));
+ }
+
std::vector<StringRef> ImplicitSections;
+ if (Doc.DynamicSymbols)
+ ImplicitSections.insert(ImplicitSections.end(), {".dynsym", ".dynstr"});
if (Doc.Symbols)
ImplicitSections.push_back(".symtab");
+ if (Doc.DWARF)
+ for (StringRef DebugSecName : Doc.DWARF->getUsedSectionNames()) {
+ std::string SecName = ("." + DebugSecName).str();
+ ImplicitSections.push_back(StringRef(SecName).copy(StringAlloc));
+ }
ImplicitSections.insert(ImplicitSections.end(), {".strtab", ".shstrtab"});
- if (Doc.DynamicSymbols)
- ImplicitSections.insert(ImplicitSections.end(), {".dynsym", ".dynstr"});
-
// Insert placeholders for implicit sections that are not
// defined explicitly in YAML.
for (StringRef SecName : ImplicitSections) {
@@ -269,7 +375,7 @@ ELFState<ELFT>::ELFState(ELFYAML::Object &D, yaml::ErrorHandler EH)
}
template <class ELFT>
-void ELFState<ELFT>::writeELFHeader(ContiguousBlobAccumulator &CBA, raw_ostream &OS) {
+void ELFState<ELFT>::writeELFHeader(raw_ostream &OS, uint64_t SHOff) {
using namespace llvm::ELF;
Elf_Ehdr Header;
@@ -287,55 +393,134 @@ void ELFState<ELFT>::writeELFHeader(ContiguousBlobAccumulator &CBA, raw_ostream
Header.e_machine = Doc.Header.Machine;
Header.e_version = EV_CURRENT;
Header.e_entry = Doc.Header.Entry;
- Header.e_phoff = Doc.ProgramHeaders.size() ? sizeof(Header) : 0;
Header.e_flags = Doc.Header.Flags;
Header.e_ehsize = sizeof(Elf_Ehdr);
- Header.e_phentsize = Doc.ProgramHeaders.size() ? sizeof(Elf_Phdr) : 0;
- Header.e_phnum = Doc.ProgramHeaders.size();
-
- Header.e_shentsize =
- Doc.Header.SHEntSize ? (uint16_t)*Doc.Header.SHEntSize : sizeof(Elf_Shdr);
- // Immediately following the ELF header and program headers.
- // Align the start of the section header and write the ELF header.
- uint64_t SHOff;
- CBA.getOSAndAlignedOffset(SHOff, sizeof(typename ELFT::uint));
- Header.e_shoff =
- Doc.Header.SHOff ? typename ELFT::uint(*Doc.Header.SHOff) : SHOff;
- Header.e_shnum =
- Doc.Header.SHNum ? (uint16_t)*Doc.Header.SHNum : Doc.getSections().size();
- Header.e_shstrndx = Doc.Header.SHStrNdx ? (uint16_t)*Doc.Header.SHStrNdx
- : SN2I.get(".shstrtab");
+
+ if (Doc.Header.EPhOff)
+ Header.e_phoff = *Doc.Header.EPhOff;
+ else if (!Doc.ProgramHeaders.empty())
+ Header.e_phoff = sizeof(Header);
+ else
+ Header.e_phoff = 0;
+
+ if (Doc.Header.EPhEntSize)
+ Header.e_phentsize = *Doc.Header.EPhEntSize;
+ else if (!Doc.ProgramHeaders.empty())
+ Header.e_phentsize = sizeof(Elf_Phdr);
+ else
+ Header.e_phentsize = 0;
+
+ if (Doc.Header.EPhNum)
+ Header.e_phnum = *Doc.Header.EPhNum;
+ else if (!Doc.ProgramHeaders.empty())
+ Header.e_phnum = Doc.ProgramHeaders.size();
+ else
+ Header.e_phnum = 0;
+
+ Header.e_shentsize = Doc.Header.EShEntSize ? (uint16_t)*Doc.Header.EShEntSize
+ : sizeof(Elf_Shdr);
+
+ const bool NoShdrs =
+ Doc.SectionHeaders && Doc.SectionHeaders->NoHeaders.getValueOr(false);
+
+ if (Doc.Header.EShOff)
+ Header.e_shoff = *Doc.Header.EShOff;
+ else if (NoShdrs)
+ Header.e_shoff = 0;
+ else
+ Header.e_shoff = SHOff;
+
+ if (Doc.Header.EShNum)
+ Header.e_shnum = *Doc.Header.EShNum;
+ else if (!Doc.SectionHeaders)
+ Header.e_shnum = Doc.getSections().size();
+ else if (NoShdrs)
+ Header.e_shnum = 0;
+ else
+ Header.e_shnum =
+ (Doc.SectionHeaders->Sections ? Doc.SectionHeaders->Sections->size()
+ : 0) +
+ /*Null section*/ 1;
+
+ if (Doc.Header.EShStrNdx)
+ Header.e_shstrndx = *Doc.Header.EShStrNdx;
+ else if (NoShdrs || ExcludedSectionHeaders.count(".shstrtab"))
+ Header.e_shstrndx = 0;
+ else
+ Header.e_shstrndx = SN2I.get(".shstrtab");
OS.write((const char *)&Header, sizeof(Header));
}
template <class ELFT>
void ELFState<ELFT>::initProgramHeaders(std::vector<Elf_Phdr> &PHeaders) {
- for (const auto &YamlPhdr : Doc.ProgramHeaders) {
+ DenseMap<StringRef, ELFYAML::Fill *> NameToFill;
+ for (const std::unique_ptr<ELFYAML::Chunk> &D : Doc.Chunks)
+ if (auto S = dyn_cast<ELFYAML::Fill>(D.get()))
+ NameToFill[S->Name] = S;
+
+ std::vector<ELFYAML::Section *> Sections = Doc.getSections();
+ for (ELFYAML::ProgramHeader &YamlPhdr : Doc.ProgramHeaders) {
Elf_Phdr Phdr;
+ zero(Phdr);
Phdr.p_type = YamlPhdr.Type;
Phdr.p_flags = YamlPhdr.Flags;
Phdr.p_vaddr = YamlPhdr.VAddr;
Phdr.p_paddr = YamlPhdr.PAddr;
PHeaders.push_back(Phdr);
+
+ // Map Sections list to corresponding chunks.
+ for (const ELFYAML::SectionName &SecName : YamlPhdr.Sections) {
+ if (ELFYAML::Fill *Fill = NameToFill.lookup(SecName.Section)) {
+ YamlPhdr.Chunks.push_back(Fill);
+ continue;
+ }
+
+ unsigned Index;
+ if (SN2I.lookup(SecName.Section, Index)) {
+ YamlPhdr.Chunks.push_back(Sections[Index]);
+ continue;
+ }
+
+ reportError("unknown section or fill referenced: '" + SecName.Section +
+ "' by program header");
+ }
}
}
template <class ELFT>
unsigned ELFState<ELFT>::toSectionIndex(StringRef S, StringRef LocSec,
StringRef LocSym) {
+ assert(LocSec.empty() || LocSym.empty());
+
unsigned Index;
- if (SN2I.lookup(S, Index) || to_integer(S, Index))
+ if (!SN2I.lookup(S, Index) && !to_integer(S, Index)) {
+ if (!LocSym.empty())
+ reportError("unknown section referenced: '" + S + "' by YAML symbol '" +
+ LocSym + "'");
+ else
+ reportError("unknown section referenced: '" + S + "' by YAML section '" +
+ LocSec + "'");
+ return 0;
+ }
+
+ if (!Doc.SectionHeaders || (Doc.SectionHeaders->NoHeaders &&
+ !Doc.SectionHeaders->NoHeaders.getValue()))
return Index;
- assert(LocSec.empty() || LocSym.empty());
- if (!LocSym.empty())
- reportError("unknown section referenced: '" + S + "' by YAML symbol '" +
- LocSym + "'");
- else
- reportError("unknown section referenced: '" + S + "' by YAML section '" +
- LocSec + "'");
- return 0;
+ assert(!Doc.SectionHeaders->NoHeaders.getValueOr(false) ||
+ !Doc.SectionHeaders->Sections);
+ size_t FirstExcluded =
+ Doc.SectionHeaders->Sections ? Doc.SectionHeaders->Sections->size() : 0;
+ if (Index >= FirstExcluded) {
+ if (LocSym.empty())
+ reportError("unable to link '" + LocSec + "' to excluded section '" + S +
+ "'");
+ else
+ reportError("excluded section referenced: '" + S + "' by symbol '" +
+ LocSym + "'");
+ }
+ return Index;
}
template <class ELFT>
@@ -385,19 +570,53 @@ bool ELFState<ELFT>::initImplicitHeader(ContiguousBlobAccumulator &CBA,
initSymtabSectionHeader(Header, SymtabType::Dynamic, CBA, YAMLSec);
else if (SecName == ".dynstr")
initStrtabSectionHeader(Header, SecName, DotDynstr, CBA, YAMLSec);
- else
+ else if (SecName.startswith(".debug_")) {
+ // If a ".debug_*" section's type is a preserved one, e.g., SHT_DYNAMIC, we
+ // will not treat it as a debug section.
+ if (YAMLSec && !isa<ELFYAML::RawContentSection>(YAMLSec))
+ return false;
+ initDWARFSectionHeader(Header, SecName, CBA, YAMLSec);
+ } else
return false;
+ LocationCounter += Header.sh_size;
+
// Override section fields if requested.
overrideFields<ELFT>(YAMLSec, Header);
return true;
}
+constexpr char SuffixStart = '(';
+constexpr char SuffixEnd = ')';
+
+std::string llvm::ELFYAML::appendUniqueSuffix(StringRef Name,
+ const Twine &Msg) {
+ // Do not add a space when a Name is empty.
+ std::string Ret = Name.empty() ? "" : Name.str() + ' ';
+ return Ret + (Twine(SuffixStart) + Msg + Twine(SuffixEnd)).str();
+}
+
StringRef llvm::ELFYAML::dropUniqueSuffix(StringRef S) {
- size_t SuffixPos = S.rfind(" [");
- if (SuffixPos == StringRef::npos)
+ if (S.empty() || S.back() != SuffixEnd)
return S;
- return S.substr(0, SuffixPos);
+
+ // A special case for empty names. See appendUniqueSuffix() above.
+ size_t SuffixPos = S.rfind(SuffixStart);
+ if (SuffixPos == 0)
+ return "";
+
+ if (SuffixPos == StringRef::npos || S[SuffixPos - 1] != ' ')
+ return S;
+ return S.substr(0, SuffixPos - 1);
+}
+
+template <class ELFT>
+uint64_t ELFState<ELFT>::getSectionNameOffset(StringRef Name) {
+ // If a section is excluded from section headers, we do not save its name in
+ // the string table.
+ if (ExcludedSectionHeaders.count(Name))
+ return 0;
+ return DotShStrtab.getOffset(Name);
}
template <class ELFT>
@@ -407,23 +626,24 @@ void ELFState<ELFT>::initSectionHeaders(std::vector<Elf_Shdr> &SHeaders,
// valid SHN_UNDEF entry since SHT_NULL == 0.
SHeaders.resize(Doc.getSections().size());
- size_t SecNdx = -1;
for (const std::unique_ptr<ELFYAML::Chunk> &D : Doc.Chunks) {
- if (auto S = dyn_cast<ELFYAML::Fill>(D.get())) {
+ if (ELFYAML::Fill *S = dyn_cast<ELFYAML::Fill>(D.get())) {
+ S->Offset = alignToOffset(CBA, /*Align=*/1, S->Offset);
writeFill(*S, CBA);
+ LocationCounter += S->Size;
continue;
}
- ++SecNdx;
ELFYAML::Section *Sec = cast<ELFYAML::Section>(D.get());
- if (SecNdx == 0 && Sec->IsImplicit)
+ bool IsFirstUndefSection = D == Doc.Chunks.front();
+ if (IsFirstUndefSection && Sec->IsImplicit)
continue;
// We have a few sections like string or symbol tables that are usually
// added implicitly to the end. However, if they are explicitly specified
// in the YAML, we need to write them here. This ensures the file offset
// remains correct.
- Elf_Shdr &SHeader = SHeaders[SecNdx];
+ Elf_Shdr &SHeader = SHeaders[SN2I.get(Sec->Name)];
if (initImplicitHeader(CBA, SHeader, Sec->Name,
Sec->IsImplicit ? nullptr : Sec))
continue;
@@ -432,17 +652,23 @@ void ELFState<ELFT>::initSectionHeaders(std::vector<Elf_Shdr> &SHeaders,
"implicit sections should already have been handled above.");
SHeader.sh_name =
- DotShStrtab.getOffset(ELFYAML::dropUniqueSuffix(Sec->Name));
+ getSectionNameOffset(ELFYAML::dropUniqueSuffix(Sec->Name));
SHeader.sh_type = Sec->Type;
if (Sec->Flags)
SHeader.sh_flags = *Sec->Flags;
- SHeader.sh_addr = Sec->Address;
SHeader.sh_addralign = Sec->AddressAlign;
+ // Set the offset for all sections, except the SHN_UNDEF section with index
+ // 0 when not explicitly requested.
+ if (!IsFirstUndefSection || Sec->Offset)
+ SHeader.sh_offset = alignToOffset(CBA, SHeader.sh_addralign, Sec->Offset);
+
+ assignSectionAddress(SHeader, Sec);
+
if (!Sec->Link.empty())
SHeader.sh_link = toSectionIndex(Sec->Link, Sec->Name);
- if (SecNdx == 0) {
+ if (IsFirstUndefSection) {
if (auto RawSec = dyn_cast<ELFYAML::RawContentSection>(Sec)) {
// We do not write any content for special SHN_UNDEF section.
if (RawSec->Size)
@@ -465,11 +691,7 @@ void ELFState<ELFT>::initSectionHeaders(std::vector<Elf_Shdr> &SHeaders,
} else if (auto S = dyn_cast<ELFYAML::MipsABIFlags>(Sec)) {
writeSectionContent(SHeader, *S, CBA);
} else if (auto S = dyn_cast<ELFYAML::NoBitsSection>(Sec)) {
- SHeader.sh_entsize = 0;
- SHeader.sh_size = S->Size;
- // SHT_NOBITS section does not have content
- // so just to setup the section offset.
- CBA.getOSAndAlignedOffset(SHeader.sh_offset, SHeader.sh_addralign);
+ writeSectionContent(SHeader, *S, CBA);
} else if (auto S = dyn_cast<ELFYAML::DynamicSection>(Sec)) {
writeSectionContent(SHeader, *S, CBA);
} else if (auto S = dyn_cast<ELFYAML::SymverSection>(Sec)) {
@@ -492,15 +714,40 @@ void ELFState<ELFT>::initSectionHeaders(std::vector<Elf_Shdr> &SHeaders,
writeSectionContent(SHeader, *S, CBA);
} else if (auto S = dyn_cast<ELFYAML::DependentLibrariesSection>(Sec)) {
writeSectionContent(SHeader, *S, CBA);
+ } else if (auto S = dyn_cast<ELFYAML::CallGraphProfileSection>(Sec)) {
+ writeSectionContent(SHeader, *S, CBA);
} else {
llvm_unreachable("Unknown section type");
}
+ LocationCounter += SHeader.sh_size;
+
// Override section fields if requested.
overrideFields<ELFT>(Sec, SHeader);
}
}
+template <class ELFT>
+void ELFState<ELFT>::assignSectionAddress(Elf_Shdr &SHeader,
+ ELFYAML::Section *YAMLSec) {
+ if (YAMLSec && YAMLSec->Address) {
+ SHeader.sh_addr = *YAMLSec->Address;
+ LocationCounter = *YAMLSec->Address;
+ return;
+ }
+
+ // sh_addr represents the address in the memory image of a process. Sections
+ // in a relocatable object file or non-allocatable sections do not need
+ // sh_addr assignment.
+ if (Doc.Header.Type.value == ELF::ET_REL ||
+ !(SHeader.sh_flags & ELF::SHF_ALLOC))
+ return;
+
+ LocationCounter =
+ alignTo(LocationCounter, SHeader.sh_addralign ? SHeader.sh_addralign : 1);
+ SHeader.sh_addr = LocationCounter;
+}
+
static size_t findFirstNonGlobal(ArrayRef<ELFYAML::Symbol> Symbols) {
for (size_t I = 0; I < Symbols.size(); ++I)
if (Symbols[I].Binding.value != ELF::STB_LOCAL)
@@ -508,19 +755,19 @@ static size_t findFirstNonGlobal(ArrayRef<ELFYAML::Symbol> Symbols) {
return Symbols.size();
}
-static uint64_t writeContent(raw_ostream &OS,
+static uint64_t writeContent(ContiguousBlobAccumulator &CBA,
const Optional<yaml::BinaryRef> &Content,
const Optional<llvm::yaml::Hex64> &Size) {
size_t ContentSize = 0;
if (Content) {
- Content->writeAsBinary(OS);
+ CBA.writeAsBinary(*Content);
ContentSize = Content->binary_size();
}
if (!Size)
return ContentSize;
- OS.write_zeros(*Size - ContentSize);
+ CBA.writeZeros(*Size - ContentSize);
return *Size;
}
@@ -538,8 +785,8 @@ ELFState<ELFT>::toELFSymbols(ArrayRef<ELFYAML::Symbol> Symbols,
// If NameIndex, which contains the name offset, is explicitly specified, we
// use it. This is useful for preparing broken objects. Otherwise, we add
// the specified Name to the string table builder to get its offset.
- if (Sym.NameIndex)
- Symbol.st_name = *Sym.NameIndex;
+ if (Sym.StName)
+ Symbol.st_name = *Sym.StName;
else if (!Sym.Name.empty())
Symbol.st_name = Strtab.getOffset(ELFYAML::dropUniqueSuffix(Sym.Name));
@@ -588,7 +835,7 @@ void ELFState<ELFT>::initSymtabSectionHeader(Elf_Shdr &SHeader,
}
zero(SHeader);
- SHeader.sh_name = DotShStrtab.getOffset(IsStatic ? ".symtab" : ".dynsym");
+ SHeader.sh_name = getSectionNameOffset(IsStatic ? ".symtab" : ".dynsym");
if (YAMLSec)
SHeader.sh_type = YAMLSec->Type;
@@ -605,10 +852,13 @@ void ELFState<ELFT>::initSymtabSectionHeader(Elf_Shdr &SHeader,
// added implicitly and we should be able to leave the Link zeroed if
// .dynstr is not defined.
unsigned Link = 0;
- if (IsStatic)
- Link = SN2I.get(".strtab");
- else
- SN2I.lookup(".dynstr", Link);
+ if (IsStatic) {
+ if (!ExcludedSectionHeaders.count(".strtab"))
+ Link = SN2I.get(".strtab");
+ } else {
+ if (!ExcludedSectionHeaders.count(".dynstr"))
+ SN2I.lookup(".dynstr", Link);
+ }
SHeader.sh_link = Link;
}
@@ -625,19 +875,21 @@ void ELFState<ELFT>::initSymtabSectionHeader(Elf_Shdr &SHeader,
? (uint64_t)(*YAMLSec->EntSize)
: sizeof(Elf_Sym);
SHeader.sh_addralign = YAMLSec ? (uint64_t)YAMLSec->AddressAlign : 8;
- SHeader.sh_addr = YAMLSec ? (uint64_t)YAMLSec->Address : 0;
- auto &OS = CBA.getOSAndAlignedOffset(SHeader.sh_offset, SHeader.sh_addralign);
+ assignSectionAddress(SHeader, YAMLSec);
+
+ SHeader.sh_offset = alignToOffset(CBA, SHeader.sh_addralign, /*Offset=*/None);
+
if (RawSec && (RawSec->Content || RawSec->Size)) {
assert(Symbols.empty());
- SHeader.sh_size = writeContent(OS, RawSec->Content, RawSec->Size);
+ SHeader.sh_size = writeContent(CBA, RawSec->Content, RawSec->Size);
return;
}
std::vector<Elf_Sym> Syms =
toELFSymbols(Symbols, IsStatic ? DotStrtab : DotDynstr);
- writeArrayData(OS, makeArrayRef(Syms));
- SHeader.sh_size = arrayDataSize(makeArrayRef(Syms));
+ SHeader.sh_size = Syms.size() * sizeof(Elf_Sym);
+ CBA.write((const char *)Syms.data(), SHeader.sh_size);
}
template <class ELFT>
@@ -646,18 +898,20 @@ void ELFState<ELFT>::initStrtabSectionHeader(Elf_Shdr &SHeader, StringRef Name,
ContiguousBlobAccumulator &CBA,
ELFYAML::Section *YAMLSec) {
zero(SHeader);
- SHeader.sh_name = DotShStrtab.getOffset(Name);
+ SHeader.sh_name = getSectionNameOffset(Name);
SHeader.sh_type = YAMLSec ? YAMLSec->Type : ELF::SHT_STRTAB;
SHeader.sh_addralign = YAMLSec ? (uint64_t)YAMLSec->AddressAlign : 1;
ELFYAML::RawContentSection *RawSec =
dyn_cast_or_null<ELFYAML::RawContentSection>(YAMLSec);
- auto &OS = CBA.getOSAndAlignedOffset(SHeader.sh_offset, SHeader.sh_addralign);
+ SHeader.sh_offset = alignToOffset(CBA, SHeader.sh_addralign, /*Offset=*/None);
+
if (RawSec && (RawSec->Content || RawSec->Size)) {
- SHeader.sh_size = writeContent(OS, RawSec->Content, RawSec->Size);
+ SHeader.sh_size = writeContent(CBA, RawSec->Content, RawSec->Size);
} else {
- STB.write(OS);
+ if (raw_ostream *OS = CBA.getRawOS(STB.getSize()))
+ STB.write(*OS);
SHeader.sh_size = STB.getSize();
}
@@ -672,10 +926,110 @@ void ELFState<ELFT>::initStrtabSectionHeader(Elf_Shdr &SHeader, StringRef Name,
else if (Name == ".dynstr")
SHeader.sh_flags = ELF::SHF_ALLOC;
- // If the section is explicitly described in the YAML
- // then we want to use its section address.
- if (YAMLSec)
- SHeader.sh_addr = YAMLSec->Address;
+ assignSectionAddress(SHeader, YAMLSec);
+}
+
+static bool shouldEmitDWARF(DWARFYAML::Data &DWARF, StringRef Name) {
+ SetVector<StringRef> DebugSecNames = DWARF.getUsedSectionNames();
+ return Name.consume_front(".") && DebugSecNames.count(Name);
+}
+
+template <class ELFT>
+Expected<uint64_t> emitDWARF(typename ELFT::Shdr &SHeader, StringRef Name,
+ const DWARFYAML::Data &DWARF,
+ ContiguousBlobAccumulator &CBA) {
+ // We are unable to predict the size of debug data, so we request to write 0
+ // bytes. This should always return us an output stream unless CBA is already
+ // in an error state.
+ raw_ostream *OS = CBA.getRawOS(0);
+ if (!OS)
+ return 0;
+
+ uint64_t BeginOffset = CBA.tell();
+ Error Err = Error::success();
+ cantFail(std::move(Err));
+
+ if (Name == ".debug_str")
+ Err = DWARFYAML::emitDebugStr(*OS, DWARF);
+ else if (Name == ".debug_aranges")
+ Err = DWARFYAML::emitDebugAranges(*OS, DWARF);
+ else if (Name == ".debug_ranges")
+ Err = DWARFYAML::emitDebugRanges(*OS, DWARF);
+ else if (Name == ".debug_line")
+ Err = DWARFYAML::emitDebugLine(*OS, DWARF);
+ else if (Name == ".debug_addr")
+ Err = DWARFYAML::emitDebugAddr(*OS, DWARF);
+ else if (Name == ".debug_abbrev")
+ Err = DWARFYAML::emitDebugAbbrev(*OS, DWARF);
+ else if (Name == ".debug_info")
+ Err = DWARFYAML::emitDebugInfo(*OS, DWARF);
+ else if (Name == ".debug_pubnames")
+ Err = DWARFYAML::emitPubSection(*OS, *DWARF.PubNames, DWARF.IsLittleEndian);
+ else if (Name == ".debug_pubtypes")
+ Err = DWARFYAML::emitPubSection(*OS, *DWARF.PubTypes, DWARF.IsLittleEndian);
+ else if (Name == ".debug_gnu_pubnames")
+ Err = DWARFYAML::emitPubSection(*OS, *DWARF.GNUPubNames,
+ DWARF.IsLittleEndian, /*IsGNUStyle=*/true);
+ else if (Name == ".debug_gnu_pubtypes")
+ Err = DWARFYAML::emitPubSection(*OS, *DWARF.GNUPubTypes,
+ DWARF.IsLittleEndian, /*IsGNUStyle=*/true);
+ else
+ llvm_unreachable("unexpected emitDWARF() call");
+
+ if (Err)
+ return std::move(Err);
+
+ return CBA.tell() - BeginOffset;
+}
+
+template <class ELFT>
+void ELFState<ELFT>::initDWARFSectionHeader(Elf_Shdr &SHeader, StringRef Name,
+ ContiguousBlobAccumulator &CBA,
+ ELFYAML::Section *YAMLSec) {
+ zero(SHeader);
+ SHeader.sh_name = getSectionNameOffset(ELFYAML::dropUniqueSuffix(Name));
+ SHeader.sh_type = YAMLSec ? YAMLSec->Type : ELF::SHT_PROGBITS;
+ SHeader.sh_addralign = YAMLSec ? (uint64_t)YAMLSec->AddressAlign : 1;
+ SHeader.sh_offset = alignToOffset(CBA, SHeader.sh_addralign,
+ YAMLSec ? YAMLSec->Offset : None);
+
+ ELFYAML::RawContentSection *RawSec =
+ dyn_cast_or_null<ELFYAML::RawContentSection>(YAMLSec);
+ if (Doc.DWARF && shouldEmitDWARF(*Doc.DWARF, Name)) {
+ if (RawSec && (RawSec->Content || RawSec->Size))
+ reportError("cannot specify section '" + Name +
+ "' contents in the 'DWARF' entry and the 'Content' "
+ "or 'Size' in the 'Sections' entry at the same time");
+ else {
+ if (Expected<uint64_t> ShSizeOrErr =
+ emitDWARF<ELFT>(SHeader, Name, *Doc.DWARF, CBA))
+ SHeader.sh_size = *ShSizeOrErr;
+ else
+ reportError(ShSizeOrErr.takeError());
+ }
+ } else if (RawSec)
+ SHeader.sh_size = writeContent(CBA, RawSec->Content, RawSec->Size);
+ else
+ llvm_unreachable("debug sections can only be initialized via the 'DWARF' "
+ "entry or a RawContentSection");
+
+ if (YAMLSec && YAMLSec->EntSize)
+ SHeader.sh_entsize = *YAMLSec->EntSize;
+ else if (Name == ".debug_str")
+ SHeader.sh_entsize = 1;
+
+ if (RawSec && RawSec->Info)
+ SHeader.sh_info = *RawSec->Info;
+
+ if (YAMLSec && YAMLSec->Flags)
+ SHeader.sh_flags = *YAMLSec->Flags;
+ else if (Name == ".debug_str")
+ SHeader.sh_flags = ELF::SHF_MERGE | ELF::SHF_STRINGS;
+
+ if (YAMLSec && !YAMLSec->Link.empty())
+ SHeader.sh_link = toSectionIndex(YAMLSec->Link, Name);
+
+ assignSectionAddress(SHeader, YAMLSec);
}
template <class ELFT> void ELFState<ELFT>::reportError(const Twine &Msg) {
@@ -683,34 +1037,28 @@ template <class ELFT> void ELFState<ELFT>::reportError(const Twine &Msg) {
HasError = true;
}
+template <class ELFT> void ELFState<ELFT>::reportError(Error Err) {
+ handleAllErrors(std::move(Err), [&](const ErrorInfoBase &Err) {
+ reportError(Err.message());
+ });
+}
+
template <class ELFT>
std::vector<Fragment>
ELFState<ELFT>::getPhdrFragments(const ELFYAML::ProgramHeader &Phdr,
- ArrayRef<typename ELFT::Shdr> SHeaders) {
- DenseMap<StringRef, ELFYAML::Fill *> NameToFill;
- for (const std::unique_ptr<ELFYAML::Chunk> &D : Doc.Chunks)
- if (auto S = dyn_cast<ELFYAML::Fill>(D.get()))
- NameToFill[S->Name] = S;
-
+ ArrayRef<Elf_Shdr> SHeaders) {
std::vector<Fragment> Ret;
- for (const ELFYAML::SectionName &SecName : Phdr.Sections) {
- unsigned Index;
- if (SN2I.lookup(SecName.Section, Index)) {
- const typename ELFT::Shdr &H = SHeaders[Index];
- Ret.push_back({H.sh_offset, H.sh_size, H.sh_type, H.sh_addralign});
- continue;
- }
-
- if (ELFYAML::Fill *Fill = NameToFill.lookup(SecName.Section)) {
- Ret.push_back({Fill->ShOffset, Fill->Size, llvm::ELF::SHT_PROGBITS,
+ for (const ELFYAML::Chunk *C : Phdr.Chunks) {
+ if (const ELFYAML::Fill *F = dyn_cast<ELFYAML::Fill>(C)) {
+ Ret.push_back({*F->Offset, F->Size, llvm::ELF::SHT_PROGBITS,
/*ShAddrAlign=*/1});
continue;
}
- reportError("unknown section or fill referenced: '" + SecName.Section +
- "' by program header");
+ const ELFYAML::Section *S = cast<ELFYAML::Section>(C);
+ const Elf_Shdr &H = SHeaders[SN2I.get(S->Name)];
+ Ret.push_back({H.sh_offset, H.sh_size, H.sh_type, H.sh_addralign});
}
-
return Ret;
}
@@ -721,35 +1069,41 @@ void ELFState<ELFT>::setProgramHeaderLayout(std::vector<Elf_Phdr> &PHeaders,
for (auto &YamlPhdr : Doc.ProgramHeaders) {
Elf_Phdr &PHeader = PHeaders[PhdrIdx++];
std::vector<Fragment> Fragments = getPhdrFragments(YamlPhdr, SHeaders);
+ if (!llvm::is_sorted(Fragments, [](const Fragment &A, const Fragment &B) {
+ return A.Offset < B.Offset;
+ }))
+ reportError("sections in the program header with index " +
+ Twine(PhdrIdx) + " are not sorted by their file offset");
if (YamlPhdr.Offset) {
+ if (!Fragments.empty() && *YamlPhdr.Offset > Fragments.front().Offset)
+ reportError("'Offset' for segment with index " + Twine(PhdrIdx) +
+ " must be less than or equal to the minimum file offset of "
+ "all included sections (0x" +
+ Twine::utohexstr(Fragments.front().Offset) + ")");
PHeader.p_offset = *YamlPhdr.Offset;
- } else {
- if (YamlPhdr.Sections.size())
- PHeader.p_offset = UINT32_MAX;
- else
- PHeader.p_offset = 0;
-
- // Find the minimum offset for the program header.
- for (const Fragment &F : Fragments)
- PHeader.p_offset = std::min((uint64_t)PHeader.p_offset, F.Offset);
+ } else if (!Fragments.empty()) {
+ PHeader.p_offset = Fragments.front().Offset;
}
- // Find the maximum offset of the end of a section in order to set p_filesz
- // and p_memsz. When setting p_filesz, trailing SHT_NOBITS sections are not
- // counted.
- uint64_t FileOffset = PHeader.p_offset, MemOffset = PHeader.p_offset;
- for (const Fragment &F : Fragments) {
- uint64_t End = F.Offset + F.Size;
- MemOffset = std::max(MemOffset, End);
-
- if (F.Type != llvm::ELF::SHT_NOBITS)
- FileOffset = std::max(FileOffset, End);
+ // Set the file size if not set explicitly.
+ if (YamlPhdr.FileSize) {
+ PHeader.p_filesz = *YamlPhdr.FileSize;
+ } else if (!Fragments.empty()) {
+ uint64_t FileSize = Fragments.back().Offset - PHeader.p_offset;
+ // SHT_NOBITS sections occupy no physical space in a file, we should not
+ // take their sizes into account when calculating the file size of a
+ // segment.
+ if (Fragments.back().Type != llvm::ELF::SHT_NOBITS)
+ FileSize += Fragments.back().Size;
+ PHeader.p_filesz = FileSize;
}
- // Set the file size and the memory size if not set explicitly.
- PHeader.p_filesz = YamlPhdr.FileSize ? uint64_t(*YamlPhdr.FileSize)
- : FileOffset - PHeader.p_offset;
+ // Find the maximum offset of the end of a section in order to set p_memsz.
+ uint64_t MemOffset = PHeader.p_offset;
+ for (const Fragment &F : Fragments)
+ MemOffset = std::max(MemOffset, F.Offset + F.Size);
+ // Set the memory size if not set explicitly.
PHeader.p_memsz = YamlPhdr.MemSize ? uint64_t(*YamlPhdr.MemSize)
: MemOffset - PHeader.p_offset;
@@ -766,13 +1120,40 @@ void ELFState<ELFT>::setProgramHeaderLayout(std::vector<Elf_Phdr> &PHeaders,
}
}
+static bool shouldAllocateFileSpace(ArrayRef<ELFYAML::ProgramHeader> Phdrs,
+ const ELFYAML::NoBitsSection &S) {
+ for (const ELFYAML::ProgramHeader &PH : Phdrs) {
+ auto It = llvm::find_if(
+ PH.Chunks, [&](ELFYAML::Chunk *C) { return C->Name == S.Name; });
+ if (std::any_of(It, PH.Chunks.end(), [](ELFYAML::Chunk *C) {
+ return (isa<ELFYAML::Fill>(C) ||
+ cast<ELFYAML::Section>(C)->Type != ELF::SHT_NOBITS);
+ }))
+ return true;
+ }
+ return false;
+}
+
+template <class ELFT>
+void ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
+ const ELFYAML::NoBitsSection &S,
+ ContiguousBlobAccumulator &CBA) {
+ // SHT_NOBITS sections do not have any content to write.
+ SHeader.sh_entsize = 0;
+ SHeader.sh_size = S.Size;
+
+ // When a nobits section is followed by a non-nobits section or fill
+ // in the same segment, we allocate the file space for it. This behavior
+ // matches linkers.
+ if (shouldAllocateFileSpace(Doc.ProgramHeaders, S))
+ CBA.writeZeros(S.Size);
+}
+
template <class ELFT>
void ELFState<ELFT>::writeSectionContent(
Elf_Shdr &SHeader, const ELFYAML::RawContentSection &Section,
ContiguousBlobAccumulator &CBA) {
- raw_ostream &OS =
- CBA.getOSAndAlignedOffset(SHeader.sh_offset, SHeader.sh_addralign);
- SHeader.sh_size = writeContent(OS, Section.Content, Section.Size);
+ SHeader.sh_size = writeContent(CBA, Section.Content, Section.Size);
if (Section.EntSize)
SHeader.sh_entsize = *Section.EntSize;
@@ -796,18 +1177,22 @@ void ELFState<ELFT>::writeSectionContent(
"Section type is not SHT_REL nor SHT_RELA");
bool IsRela = Section.Type == llvm::ELF::SHT_RELA;
- SHeader.sh_entsize = IsRela ? sizeof(Elf_Rela) : sizeof(Elf_Rel);
- SHeader.sh_size = SHeader.sh_entsize * Section.Relocations.size();
+ if (Section.EntSize)
+ SHeader.sh_entsize = *Section.EntSize;
+ else
+ SHeader.sh_entsize = IsRela ? sizeof(Elf_Rela) : sizeof(Elf_Rel);
+ SHeader.sh_size = (IsRela ? sizeof(Elf_Rela) : sizeof(Elf_Rel)) *
+ Section.Relocations.size();
// For relocation section set link to .symtab by default.
unsigned Link = 0;
- if (Section.Link.empty() && SN2I.lookup(".symtab", Link))
+ if (Section.Link.empty() && !ExcludedSectionHeaders.count(".symtab") &&
+ SN2I.lookup(".symtab", Link))
SHeader.sh_link = Link;
if (!Section.RelocatableSec.empty())
SHeader.sh_info = toSectionIndex(Section.RelocatableSec, Section.Name);
- auto &OS = CBA.getOSAndAlignedOffset(SHeader.sh_offset, SHeader.sh_addralign);
for (const auto &Rel : Section.Relocations) {
unsigned SymIdx = Rel.Symbol ? toSymbolIndex(*Rel.Symbol, Section.Name,
Section.Link == ".dynsym")
@@ -818,13 +1203,13 @@ void ELFState<ELFT>::writeSectionContent(
REntry.r_offset = Rel.Offset;
REntry.r_addend = Rel.Addend;
REntry.setSymbolAndType(SymIdx, Rel.Type, isMips64EL(Doc));
- OS.write((const char *)&REntry, sizeof(REntry));
+ CBA.write((const char *)&REntry, sizeof(REntry));
} else {
Elf_Rel REntry;
zero(REntry);
REntry.r_offset = Rel.Offset;
REntry.setSymbolAndType(SymIdx, Rel.Type, isMips64EL(Doc));
- OS.write((const char *)&REntry, sizeof(REntry));
+ CBA.write((const char *)&REntry, sizeof(REntry));
}
}
}
@@ -833,13 +1218,11 @@ template <class ELFT>
void ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
const ELFYAML::RelrSection &Section,
ContiguousBlobAccumulator &CBA) {
- raw_ostream &OS =
- CBA.getOSAndAlignedOffset(SHeader.sh_offset, SHeader.sh_addralign);
SHeader.sh_entsize =
Section.EntSize ? uint64_t(*Section.EntSize) : sizeof(Elf_Relr);
if (Section.Content) {
- SHeader.sh_size = writeContent(OS, Section.Content, None);
+ SHeader.sh_size = writeContent(CBA, Section.Content, None);
return;
}
@@ -850,7 +1233,7 @@ void ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
if (!ELFT::Is64Bits && E > UINT32_MAX)
reportError(Section.Name + ": the value is too large for 32-bits: 0x" +
Twine::utohexstr(E));
- support::endian::write<uintX_t>(OS, E, ELFT::TargetEndianness);
+ CBA.write<uintX_t>(E, ELFT::TargetEndianness);
}
SHeader.sh_size = sizeof(uintX_t) * Section.Entries->size();
@@ -860,11 +1243,8 @@ template <class ELFT>
void ELFState<ELFT>::writeSectionContent(
Elf_Shdr &SHeader, const ELFYAML::SymtabShndxSection &Shndx,
ContiguousBlobAccumulator &CBA) {
- raw_ostream &OS =
- CBA.getOSAndAlignedOffset(SHeader.sh_offset, SHeader.sh_addralign);
-
for (uint32_t E : Shndx.Entries)
- support::endian::write<uint32_t>(OS, E, ELFT::TargetEndianness);
+ CBA.write<uint32_t>(E, ELFT::TargetEndianness);
SHeader.sh_entsize = Shndx.EntSize ? (uint64_t)*Shndx.EntSize : 4;
SHeader.sh_size = Shndx.Entries.size() * SHeader.sh_entsize;
@@ -878,7 +1258,8 @@ void ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
"Section type is not SHT_GROUP");
unsigned Link = 0;
- if (Section.Link.empty() && SN2I.lookup(".symtab", Link))
+ if (Section.Link.empty() && !ExcludedSectionHeaders.count(".symtab") &&
+ SN2I.lookup(".symtab", Link))
SHeader.sh_link = Link;
SHeader.sh_entsize = 4;
@@ -888,16 +1269,13 @@ void ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
SHeader.sh_info =
toSymbolIndex(*Section.Signature, Section.Name, /*IsDynamic=*/false);
- raw_ostream &OS =
- CBA.getOSAndAlignedOffset(SHeader.sh_offset, SHeader.sh_addralign);
-
for (const ELFYAML::SectionOrType &Member : Section.Members) {
unsigned int SectionIndex = 0;
if (Member.sectionNameOrType == "GRP_COMDAT")
SectionIndex = llvm::ELF::GRP_COMDAT;
else
SectionIndex = toSectionIndex(Member.sectionNameOrType, Section.Name);
- support::endian::write<uint32_t>(OS, SectionIndex, ELFT::TargetEndianness);
+ CBA.write<uint32_t>(SectionIndex, ELFT::TargetEndianness);
}
}
@@ -905,10 +1283,8 @@ template <class ELFT>
void ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
const ELFYAML::SymverSection &Section,
ContiguousBlobAccumulator &CBA) {
- raw_ostream &OS =
- CBA.getOSAndAlignedOffset(SHeader.sh_offset, SHeader.sh_addralign);
for (uint16_t Version : Section.Entries)
- support::endian::write<uint16_t>(OS, Version, ELFT::TargetEndianness);
+ CBA.write<uint16_t>(Version, ELFT::TargetEndianness);
SHeader.sh_entsize = Section.EntSize ? (uint64_t)*Section.EntSize : 2;
SHeader.sh_size = Section.Entries.size() * SHeader.sh_entsize;
@@ -918,17 +1294,14 @@ template <class ELFT>
void ELFState<ELFT>::writeSectionContent(
Elf_Shdr &SHeader, const ELFYAML::StackSizesSection &Section,
ContiguousBlobAccumulator &CBA) {
- raw_ostream &OS =
- CBA.getOSAndAlignedOffset(SHeader.sh_offset, SHeader.sh_addralign);
-
if (Section.Content || Section.Size) {
- SHeader.sh_size = writeContent(OS, Section.Content, Section.Size);
+ SHeader.sh_size = writeContent(CBA, Section.Content, Section.Size);
return;
}
for (const ELFYAML::StackSizeEntry &E : *Section.Entries) {
- support::endian::write<uintX_t>(OS, E.Address, ELFT::TargetEndianness);
- SHeader.sh_size += sizeof(uintX_t) + encodeULEB128(E.Size, OS);
+ CBA.write<uintX_t>(E.Address, ELFT::TargetEndianness);
+ SHeader.sh_size += sizeof(uintX_t) + CBA.writeULEB128(E.Size);
}
}
@@ -936,11 +1309,8 @@ template <class ELFT>
void ELFState<ELFT>::writeSectionContent(
Elf_Shdr &SHeader, const ELFYAML::LinkerOptionsSection &Section,
ContiguousBlobAccumulator &CBA) {
- raw_ostream &OS =
- CBA.getOSAndAlignedOffset(SHeader.sh_offset, SHeader.sh_addralign);
-
if (Section.Content) {
- SHeader.sh_size = writeContent(OS, Section.Content, None);
+ SHeader.sh_size = writeContent(CBA, Section.Content, None);
return;
}
@@ -948,10 +1318,10 @@ void ELFState<ELFT>::writeSectionContent(
return;
for (const ELFYAML::LinkerOption &LO : *Section.Options) {
- OS.write(LO.Key.data(), LO.Key.size());
- OS.write('\0');
- OS.write(LO.Value.data(), LO.Value.size());
- OS.write('\0');
+ CBA.write(LO.Key.data(), LO.Key.size());
+ CBA.write('\0');
+ CBA.write(LO.Value.data(), LO.Value.size());
+ CBA.write('\0');
SHeader.sh_size += (LO.Key.size() + LO.Value.size() + 2);
}
}
@@ -960,11 +1330,8 @@ template <class ELFT>
void ELFState<ELFT>::writeSectionContent(
Elf_Shdr &SHeader, const ELFYAML::DependentLibrariesSection &Section,
ContiguousBlobAccumulator &CBA) {
- raw_ostream &OS =
- CBA.getOSAndAlignedOffset(SHeader.sh_offset, SHeader.sh_addralign);
-
if (Section.Content) {
- SHeader.sh_size = writeContent(OS, Section.Content, None);
+ SHeader.sh_size = writeContent(CBA, Section.Content, None);
return;
}
@@ -972,36 +1339,94 @@ void ELFState<ELFT>::writeSectionContent(
return;
for (StringRef Lib : *Section.Libs) {
- OS.write(Lib.data(), Lib.size());
- OS.write('\0');
+ CBA.write(Lib.data(), Lib.size());
+ CBA.write('\0');
SHeader.sh_size += Lib.size() + 1;
}
}
template <class ELFT>
+uint64_t
+ELFState<ELFT>::alignToOffset(ContiguousBlobAccumulator &CBA, uint64_t Align,
+ llvm::Optional<llvm::yaml::Hex64> Offset) {
+ uint64_t CurrentOffset = CBA.getOffset();
+ uint64_t AlignedOffset;
+
+ if (Offset) {
+ if ((uint64_t)*Offset < CurrentOffset) {
+ reportError("the 'Offset' value (0x" +
+ Twine::utohexstr((uint64_t)*Offset) + ") goes backward");
+ return CurrentOffset;
+ }
+
+ // We ignore an alignment when an explicit offset has been requested.
+ AlignedOffset = *Offset;
+ } else {
+ AlignedOffset = alignTo(CurrentOffset, std::max(Align, (uint64_t)1));
+ }
+
+ CBA.writeZeros(AlignedOffset - CurrentOffset);
+ return AlignedOffset;
+}
+
+template <class ELFT>
+void ELFState<ELFT>::writeSectionContent(
+ Elf_Shdr &SHeader, const ELFYAML::CallGraphProfileSection &Section,
+ ContiguousBlobAccumulator &CBA) {
+ if (Section.EntSize)
+ SHeader.sh_entsize = *Section.EntSize;
+ else
+ SHeader.sh_entsize = 16;
+
+ unsigned Link = 0;
+ if (Section.Link.empty() && !ExcludedSectionHeaders.count(".symtab") &&
+ SN2I.lookup(".symtab", Link))
+ SHeader.sh_link = Link;
+
+ if (Section.Content) {
+ SHeader.sh_size = writeContent(CBA, Section.Content, None);
+ return;
+ }
+
+ if (!Section.Entries)
+ return;
+
+ for (const ELFYAML::CallGraphEntry &E : *Section.Entries) {
+ unsigned From = toSymbolIndex(E.From, Section.Name, /*IsDynamic=*/false);
+ unsigned To = toSymbolIndex(E.To, Section.Name, /*IsDynamic=*/false);
+
+ CBA.write<uint32_t>(From, ELFT::TargetEndianness);
+ CBA.write<uint32_t>(To, ELFT::TargetEndianness);
+ CBA.write<uint64_t>(E.Weight, ELFT::TargetEndianness);
+ SHeader.sh_size += 16;
+ }
+}
+
+template <class ELFT>
void ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
const ELFYAML::HashSection &Section,
ContiguousBlobAccumulator &CBA) {
- raw_ostream &OS =
- CBA.getOSAndAlignedOffset(SHeader.sh_offset, SHeader.sh_addralign);
-
unsigned Link = 0;
- if (Section.Link.empty() && SN2I.lookup(".dynsym", Link))
+ if (Section.Link.empty() && !ExcludedSectionHeaders.count(".dynsym") &&
+ SN2I.lookup(".dynsym", Link))
SHeader.sh_link = Link;
if (Section.Content || Section.Size) {
- SHeader.sh_size = writeContent(OS, Section.Content, Section.Size);
+ SHeader.sh_size = writeContent(CBA, Section.Content, Section.Size);
return;
}
- support::endian::write<uint32_t>(OS, Section.Bucket->size(),
- ELFT::TargetEndianness);
- support::endian::write<uint32_t>(OS, Section.Chain->size(),
- ELFT::TargetEndianness);
+ CBA.write<uint32_t>(
+ Section.NBucket.getValueOr(llvm::yaml::Hex64(Section.Bucket->size())),
+ ELFT::TargetEndianness);
+ CBA.write<uint32_t>(
+ Section.NChain.getValueOr(llvm::yaml::Hex64(Section.Chain->size())),
+ ELFT::TargetEndianness);
+
for (uint32_t Val : *Section.Bucket)
- support::endian::write<uint32_t>(OS, Val, ELFT::TargetEndianness);
+ CBA.write<uint32_t>(Val, ELFT::TargetEndianness);
for (uint32_t Val : *Section.Chain)
- support::endian::write<uint32_t>(OS, Val, ELFT::TargetEndianness);
+ CBA.write<uint32_t>(Val, ELFT::TargetEndianness);
SHeader.sh_size = (2 + Section.Bucket->size() + Section.Chain->size()) * 4;
}
@@ -1012,13 +1437,11 @@ void ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
ContiguousBlobAccumulator &CBA) {
typedef typename ELFT::Verdef Elf_Verdef;
typedef typename ELFT::Verdaux Elf_Verdaux;
- raw_ostream &OS =
- CBA.getOSAndAlignedOffset(SHeader.sh_offset, SHeader.sh_addralign);
SHeader.sh_info = Section.Info;
if (Section.Content) {
- SHeader.sh_size = writeContent(OS, Section.Content, None);
+ SHeader.sh_size = writeContent(CBA, Section.Content, None);
return;
}
@@ -1041,7 +1464,7 @@ void ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
else
VerDef.vd_next =
sizeof(Elf_Verdef) + E.VerNames.size() * sizeof(Elf_Verdaux);
- OS.write((const char *)&VerDef, sizeof(Elf_Verdef));
+ CBA.write((const char *)&VerDef, sizeof(Elf_Verdef));
for (size_t J = 0; J < E.VerNames.size(); ++J, ++AuxCnt) {
Elf_Verdaux VernAux;
@@ -1050,7 +1473,7 @@ void ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
VernAux.vda_next = 0;
else
VernAux.vda_next = sizeof(Elf_Verdaux);
- OS.write((const char *)&VernAux, sizeof(Elf_Verdaux));
+ CBA.write((const char *)&VernAux, sizeof(Elf_Verdaux));
}
}
@@ -1065,11 +1488,10 @@ void ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
typedef typename ELFT::Verneed Elf_Verneed;
typedef typename ELFT::Vernaux Elf_Vernaux;
- auto &OS = CBA.getOSAndAlignedOffset(SHeader.sh_offset, SHeader.sh_addralign);
SHeader.sh_info = Section.Info;
if (Section.Content) {
- SHeader.sh_size = writeContent(OS, Section.Content, None);
+ SHeader.sh_size = writeContent(CBA, Section.Content, None);
return;
}
@@ -1090,7 +1512,7 @@ void ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
sizeof(Elf_Verneed) + VE.AuxV.size() * sizeof(Elf_Vernaux);
VerNeed.vn_cnt = VE.AuxV.size();
VerNeed.vn_aux = sizeof(Elf_Verneed);
- OS.write((const char *)&VerNeed, sizeof(Elf_Verneed));
+ CBA.write((const char *)&VerNeed, sizeof(Elf_Verneed));
for (size_t J = 0; J < VE.AuxV.size(); ++J, ++AuxCnt) {
const ELFYAML::VernauxEntry &VAuxE = VE.AuxV[J];
@@ -1104,7 +1526,7 @@ void ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
VernAux.vna_next = 0;
else
VernAux.vna_next = sizeof(Elf_Vernaux);
- OS.write((const char *)&VernAux, sizeof(Elf_Vernaux));
+ CBA.write((const char *)&VernAux, sizeof(Elf_Vernaux));
}
}
@@ -1124,7 +1546,6 @@ void ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
SHeader.sh_entsize = sizeof(Flags);
SHeader.sh_size = SHeader.sh_entsize;
- auto &OS = CBA.getOSAndAlignedOffset(SHeader.sh_offset, SHeader.sh_addralign);
Flags.version = Section.Version;
Flags.isa_level = Section.ISALevel;
Flags.isa_rev = Section.ISARevision;
@@ -1136,7 +1557,7 @@ void ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
Flags.ases = Section.ASEs;
Flags.flags1 = Section.Flags1;
Flags.flags2 = Section.Flags2;
- OS.write((const char *)&Flags, sizeof(Flags));
+ CBA.write((const char *)&Flags, sizeof(Flags));
}
template <class ELFT>
@@ -1160,102 +1581,87 @@ void ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
else
SHeader.sh_entsize = sizeof(Elf_Dyn);
- raw_ostream &OS =
- CBA.getOSAndAlignedOffset(SHeader.sh_offset, SHeader.sh_addralign);
for (const ELFYAML::DynamicEntry &DE : Section.Entries) {
- support::endian::write<uintX_t>(OS, DE.Tag, ELFT::TargetEndianness);
- support::endian::write<uintX_t>(OS, DE.Val, ELFT::TargetEndianness);
+ CBA.write<uintX_t>(DE.Tag, ELFT::TargetEndianness);
+ CBA.write<uintX_t>(DE.Val, ELFT::TargetEndianness);
}
if (Section.Content)
- Section.Content->writeAsBinary(OS);
+ CBA.writeAsBinary(*Section.Content);
}
template <class ELFT>
void ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
const ELFYAML::AddrsigSection &Section,
ContiguousBlobAccumulator &CBA) {
- raw_ostream &OS =
- CBA.getOSAndAlignedOffset(SHeader.sh_offset, SHeader.sh_addralign);
-
unsigned Link = 0;
- if (Section.Link.empty() && SN2I.lookup(".symtab", Link))
+ if (Section.Link.empty() && !ExcludedSectionHeaders.count(".symtab") &&
+ SN2I.lookup(".symtab", Link))
SHeader.sh_link = Link;
if (Section.Content || Section.Size) {
- SHeader.sh_size = writeContent(OS, Section.Content, Section.Size);
+ SHeader.sh_size = writeContent(CBA, Section.Content, Section.Size);
return;
}
- for (const ELFYAML::AddrsigSymbol &Sym : *Section.Symbols) {
- uint64_t Val =
- Sym.Name ? toSymbolIndex(*Sym.Name, Section.Name, /*IsDynamic=*/false)
- : (uint32_t)*Sym.Index;
- SHeader.sh_size += encodeULEB128(Val, OS);
- }
+ for (StringRef Sym : *Section.Symbols)
+ SHeader.sh_size +=
+ CBA.writeULEB128(toSymbolIndex(Sym, Section.Name, /*IsDynamic=*/false));
}
template <class ELFT>
void ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
const ELFYAML::NoteSection &Section,
ContiguousBlobAccumulator &CBA) {
- raw_ostream &OS =
- CBA.getOSAndAlignedOffset(SHeader.sh_offset, SHeader.sh_addralign);
- uint64_t Offset = OS.tell();
-
+ uint64_t Offset = CBA.tell();
if (Section.Content || Section.Size) {
- SHeader.sh_size = writeContent(OS, Section.Content, Section.Size);
+ SHeader.sh_size = writeContent(CBA, Section.Content, Section.Size);
return;
}
for (const ELFYAML::NoteEntry &NE : *Section.Notes) {
// Write name size.
if (NE.Name.empty())
- support::endian::write<uint32_t>(OS, 0, ELFT::TargetEndianness);
+ CBA.write<uint32_t>(0, ELFT::TargetEndianness);
else
- support::endian::write<uint32_t>(OS, NE.Name.size() + 1,
- ELFT::TargetEndianness);
+ CBA.write<uint32_t>(NE.Name.size() + 1, ELFT::TargetEndianness);
// Write description size.
if (NE.Desc.binary_size() == 0)
- support::endian::write<uint32_t>(OS, 0, ELFT::TargetEndianness);
+ CBA.write<uint32_t>(0, ELFT::TargetEndianness);
else
- support::endian::write<uint32_t>(OS, NE.Desc.binary_size(),
- ELFT::TargetEndianness);
+ CBA.write<uint32_t>(NE.Desc.binary_size(), ELFT::TargetEndianness);
// Write type.
- support::endian::write<uint32_t>(OS, NE.Type, ELFT::TargetEndianness);
+ CBA.write<uint32_t>(NE.Type, ELFT::TargetEndianness);
// Write name, null terminator and padding.
if (!NE.Name.empty()) {
- support::endian::write<uint8_t>(OS, arrayRefFromStringRef(NE.Name),
- ELFT::TargetEndianness);
- support::endian::write<uint8_t>(OS, 0, ELFT::TargetEndianness);
+ CBA.write(NE.Name.data(), NE.Name.size());
+ CBA.write('\0');
CBA.padToAlignment(4);
}
// Write description and padding.
if (NE.Desc.binary_size() != 0) {
- NE.Desc.writeAsBinary(OS);
+ CBA.writeAsBinary(NE.Desc);
CBA.padToAlignment(4);
}
}
- SHeader.sh_size = OS.tell() - Offset;
+ SHeader.sh_size = CBA.tell() - Offset;
}
template <class ELFT>
void ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
const ELFYAML::GnuHashSection &Section,
ContiguousBlobAccumulator &CBA) {
- raw_ostream &OS =
- CBA.getOSAndAlignedOffset(SHeader.sh_offset, SHeader.sh_addralign);
-
unsigned Link = 0;
- if (Section.Link.empty() && SN2I.lookup(".dynsym", Link))
+ if (Section.Link.empty() && !ExcludedSectionHeaders.count(".dynsym") &&
+ SN2I.lookup(".dynsym", Link))
SHeader.sh_link = Link;
if (Section.Content) {
- SHeader.sh_size = writeContent(OS, Section.Content, None);
+ SHeader.sh_size = writeContent(CBA, Section.Content, None);
return;
}
@@ -1264,42 +1670,35 @@ void ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
// be used to override this field, which is useful for producing broken
// objects.
if (Section.Header->NBuckets)
- support::endian::write<uint32_t>(OS, *Section.Header->NBuckets,
- ELFT::TargetEndianness);
+ CBA.write<uint32_t>(*Section.Header->NBuckets, ELFT::TargetEndianness);
else
- support::endian::write<uint32_t>(OS, Section.HashBuckets->size(),
- ELFT::TargetEndianness);
+ CBA.write<uint32_t>(Section.HashBuckets->size(), ELFT::TargetEndianness);
// Write the index of the first symbol in the dynamic symbol table accessible
// via the hash table.
- support::endian::write<uint32_t>(OS, Section.Header->SymNdx,
- ELFT::TargetEndianness);
+ CBA.write<uint32_t>(Section.Header->SymNdx, ELFT::TargetEndianness);
// Write the number of words in the Bloom filter. As above, the "MaskWords"
// property can be used to set this field to any value.
if (Section.Header->MaskWords)
- support::endian::write<uint32_t>(OS, *Section.Header->MaskWords,
- ELFT::TargetEndianness);
+ CBA.write<uint32_t>(*Section.Header->MaskWords, ELFT::TargetEndianness);
else
- support::endian::write<uint32_t>(OS, Section.BloomFilter->size(),
- ELFT::TargetEndianness);
+ CBA.write<uint32_t>(Section.BloomFilter->size(), ELFT::TargetEndianness);
// Write the shift constant used by the Bloom filter.
- support::endian::write<uint32_t>(OS, Section.Header->Shift2,
- ELFT::TargetEndianness);
+ CBA.write<uint32_t>(Section.Header->Shift2, ELFT::TargetEndianness);
// We've finished writing the header. Now write the Bloom filter.
for (llvm::yaml::Hex64 Val : *Section.BloomFilter)
- support::endian::write<typename ELFT::uint>(OS, Val,
- ELFT::TargetEndianness);
+ CBA.write<uintX_t>(Val, ELFT::TargetEndianness);
// Write an array of hash buckets.
for (llvm::yaml::Hex32 Val : *Section.HashBuckets)
- support::endian::write<uint32_t>(OS, Val, ELFT::TargetEndianness);
+ CBA.write<uint32_t>(Val, ELFT::TargetEndianness);
// Write an array of hash values.
for (llvm::yaml::Hex32 Val : *Section.HashValues)
- support::endian::write<uint32_t>(OS, Val, ELFT::TargetEndianness);
+ CBA.write<uint32_t>(Val, ELFT::TargetEndianness);
SHeader.sh_size = 16 /*Header size*/ +
Section.BloomFilter->size() * sizeof(typename ELFT::uint) +
@@ -1310,42 +1709,91 @@ void ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
template <class ELFT>
void ELFState<ELFT>::writeFill(ELFYAML::Fill &Fill,
ContiguousBlobAccumulator &CBA) {
- raw_ostream &OS = CBA.getOSAndAlignedOffset(Fill.ShOffset, /*Align=*/1);
-
size_t PatternSize = Fill.Pattern ? Fill.Pattern->binary_size() : 0;
if (!PatternSize) {
- OS.write_zeros(Fill.Size);
+ CBA.writeZeros(Fill.Size);
return;
}
// Fill the content with the specified pattern.
uint64_t Written = 0;
for (; Written + PatternSize <= Fill.Size; Written += PatternSize)
- Fill.Pattern->writeAsBinary(OS);
- Fill.Pattern->writeAsBinary(OS, Fill.Size - Written);
+ CBA.writeAsBinary(*Fill.Pattern);
+ CBA.writeAsBinary(*Fill.Pattern, Fill.Size - Written);
}
-template <class ELFT> void ELFState<ELFT>::buildSectionIndex() {
- size_t SecNdx = -1;
+template <class ELFT>
+DenseMap<StringRef, size_t> ELFState<ELFT>::buildSectionHeaderReorderMap() {
+ if (!Doc.SectionHeaders || Doc.SectionHeaders->NoHeaders)
+ return DenseMap<StringRef, size_t>();
+
+ DenseMap<StringRef, size_t> Ret;
+ size_t SecNdx = 0;
StringSet<> Seen;
- for (size_t I = 0; I < Doc.Chunks.size(); ++I) {
- const std::unique_ptr<ELFYAML::Chunk> &C = Doc.Chunks[I];
- bool IsSection = isa<ELFYAML::Section>(C.get());
- if (IsSection)
- ++SecNdx;
- if (C->Name.empty())
- continue;
+ auto AddSection = [&](const ELFYAML::SectionHeader &Hdr) {
+ if (!Ret.try_emplace(Hdr.Name, ++SecNdx).second)
+ reportError("repeated section name: '" + Hdr.Name +
+ "' in the section header description");
+ Seen.insert(Hdr.Name);
+ };
- if (!Seen.insert(C->Name).second)
- reportError("repeated section/fill name: '" + C->Name +
- "' at YAML section/fill number " + Twine(I));
- if (!IsSection || HasError)
+ if (Doc.SectionHeaders->Sections)
+ for (const ELFYAML::SectionHeader &Hdr : *Doc.SectionHeaders->Sections)
+ AddSection(Hdr);
+
+ if (Doc.SectionHeaders->Excluded)
+ for (const ELFYAML::SectionHeader &Hdr : *Doc.SectionHeaders->Excluded)
+ AddSection(Hdr);
+
+ for (const ELFYAML::Section *S : Doc.getSections()) {
+ // Ignore special first SHT_NULL section.
+ if (S == Doc.getSections().front())
continue;
+ if (!Seen.count(S->Name))
+ reportError("section '" + S->Name +
+ "' should be present in the 'Sections' or 'Excluded' lists");
+ Seen.erase(S->Name);
+ }
- if (!SN2I.addName(C->Name, SecNdx))
+ for (const auto &It : Seen)
+ reportError("section header contains undefined section '" + It.getKey() +
+ "'");
+ return Ret;
+}
+
+template <class ELFT> void ELFState<ELFT>::buildSectionIndex() {
+ // A YAML description can have an explicit section header declaration that
+ // allows to change the order of section headers.
+ DenseMap<StringRef, size_t> ReorderMap = buildSectionHeaderReorderMap();
+
+ if (HasError)
+ return;
+
+ // Build excluded section headers map.
+ std::vector<ELFYAML::Section *> Sections = Doc.getSections();
+ if (Doc.SectionHeaders) {
+ if (Doc.SectionHeaders->Excluded)
+ for (const ELFYAML::SectionHeader &Hdr : *Doc.SectionHeaders->Excluded)
+ if (!ExcludedSectionHeaders.insert(Hdr.Name).second)
+ llvm_unreachable("buildSectionIndex() failed");
+
+ if (Doc.SectionHeaders->NoHeaders.getValueOr(false))
+ for (const ELFYAML::Section *S : Sections)
+ if (!ExcludedSectionHeaders.insert(S->Name).second)
+ llvm_unreachable("buildSectionIndex() failed");
+ }
+
+ size_t SecNdx = -1;
+ for (const ELFYAML::Section *S : Sections) {
+ ++SecNdx;
+
+ size_t Index = ReorderMap.empty() ? SecNdx : ReorderMap.lookup(S->Name);
+ if (!SN2I.addName(S->Name, Index))
llvm_unreachable("buildSectionIndex() failed");
- DotShStrtab.add(ELFYAML::dropUniqueSuffix(C->Name));
+
+ if (!ExcludedSectionHeaders.count(S->Name))
+ DotShStrtab.add(ELFYAML::dropUniqueSuffix(S->Name));
}
DotShStrtab.finalize();
@@ -1402,8 +1850,10 @@ template <class ELFT> void ELFState<ELFT>::finalizeStrings() {
template <class ELFT>
bool ELFState<ELFT>::writeELF(raw_ostream &OS, ELFYAML::Object &Doc,
- yaml::ErrorHandler EH) {
+ yaml::ErrorHandler EH, uint64_t MaxSize) {
ELFState<ELFT> State(Doc, EH);
+ if (State.HasError)
+ return false;
// Finalize .strtab and .dynstr sections. We do that early because want to
// finalize the string table builders before writing the content of the
@@ -1411,11 +1861,11 @@ bool ELFState<ELFT>::writeELF(raw_ostream &OS, ELFYAML::Object &Doc,
State.finalizeStrings();
State.buildSectionIndex();
+ State.buildSymbolIndexes();
+
if (State.HasError)
return false;
- State.buildSymbolIndexes();
-
std::vector<Elf_Phdr> PHeaders;
State.initProgramHeaders(PHeaders);
@@ -1423,7 +1873,11 @@ bool ELFState<ELFT>::writeELF(raw_ostream &OS, ELFYAML::Object &Doc,
// things to `OS`.
const size_t SectionContentBeginOffset =
sizeof(Elf_Ehdr) + sizeof(Elf_Phdr) * Doc.ProgramHeaders.size();
- ContiguousBlobAccumulator CBA(SectionContentBeginOffset);
+ // It is quite easy to accidentally create output with yaml2obj that is larger
+ // than intended, for example, due to an issue in the YAML description.
+ // We limit the maximum allowed output size, but also provide a command line
+ // option to change this limitation.
+ ContiguousBlobAccumulator CBA(SectionContentBeginOffset, MaxSize);
std::vector<Elf_Shdr> SHeaders;
State.initSectionHeaders(SHeaders, CBA);
@@ -1431,10 +1885,26 @@ bool ELFState<ELFT>::writeELF(raw_ostream &OS, ELFYAML::Object &Doc,
// Now we can decide segment offsets.
State.setProgramHeaderLayout(PHeaders, SHeaders);
+ // Align the start of the section header table, which is written after all
+ // section data.
+ uint64_t SHOff =
+ State.alignToOffset(CBA, sizeof(typename ELFT::uint), /*Offset=*/None);
+ bool ReachedLimit = SHOff + arrayDataSize(makeArrayRef(SHeaders)) > MaxSize;
+ if (Error E = CBA.takeLimitError()) {
+ // We report a custom error message instead below.
+ consumeError(std::move(E));
+ ReachedLimit = true;
+ }
+
+ if (ReachedLimit)
+ State.reportError(
+ "the desired output size is greater than permitted. Use the "
+ "--max-size option to change the limit");
+
if (State.HasError)
return false;
- State.writeELFHeader(CBA, OS);
+ State.writeELFHeader(OS, SHOff);
writeArrayData(OS, makeArrayRef(PHeaders));
CBA.writeBlobToStream(OS);
writeArrayData(OS, makeArrayRef(SHeaders));
@@ -1444,17 +1914,18 @@ bool ELFState<ELFT>::writeELF(raw_ostream &OS, ELFYAML::Object &Doc,
namespace llvm {
namespace yaml {
-bool yaml2elf(llvm::ELFYAML::Object &Doc, raw_ostream &Out, ErrorHandler EH) {
+bool yaml2elf(llvm::ELFYAML::Object &Doc, raw_ostream &Out, ErrorHandler EH,
+ uint64_t MaxSize) {
bool IsLE = Doc.Header.Data == ELFYAML::ELF_ELFDATA(ELF::ELFDATA2LSB);
bool Is64Bit = Doc.Header.Class == ELFYAML::ELF_ELFCLASS(ELF::ELFCLASS64);
if (Is64Bit) {
if (IsLE)
- return ELFState<object::ELF64LE>::writeELF(Out, Doc, EH);
- return ELFState<object::ELF64BE>::writeELF(Out, Doc, EH);
+ return ELFState<object::ELF64LE>::writeELF(Out, Doc, EH, MaxSize);
+ return ELFState<object::ELF64BE>::writeELF(Out, Doc, EH, MaxSize);
}
if (IsLE)
- return ELFState<object::ELF32LE>::writeELF(Out, Doc, EH);
- return ELFState<object::ELF32BE>::writeELF(Out, Doc, EH);
+ return ELFState<object::ELF32LE>::writeELF(Out, Doc, EH, MaxSize);
+ return ELFState<object::ELF32BE>::writeELF(Out, Doc, EH, MaxSize);
}
} // namespace yaml
diff --git a/llvm/lib/ObjectYAML/ELFYAML.cpp b/llvm/lib/ObjectYAML/ELFYAML.cpp
index efa7ecb4728b2..2353b34f188b1 100644
--- a/llvm/lib/ObjectYAML/ELFYAML.cpp
+++ b/llvm/lib/ObjectYAML/ELFYAML.cpp
@@ -221,6 +221,7 @@ void ScalarEnumerationTraits<ELFYAML::ELF_EM>::enumeration(
ECase(EM_RISCV);
ECase(EM_LANAI);
ECase(EM_BPF);
+ ECase(EM_VE);
#undef ECase
IO.enumFallback<Hex16>(Value);
}
@@ -349,6 +350,9 @@ void ScalarBitSetTraits<ELFYAML::ELF_EF>::bitset(IO &IO,
BCase(EF_HEXAGON_MACH_V60);
BCase(EF_HEXAGON_MACH_V62);
BCase(EF_HEXAGON_MACH_V65);
+ BCase(EF_HEXAGON_MACH_V66);
+ BCase(EF_HEXAGON_MACH_V67);
+ BCase(EF_HEXAGON_MACH_V67T);
BCase(EF_HEXAGON_ISA_V2);
BCase(EF_HEXAGON_ISA_V3);
BCase(EF_HEXAGON_ISA_V4);
@@ -357,6 +361,8 @@ void ScalarBitSetTraits<ELFYAML::ELF_EF>::bitset(IO &IO,
BCase(EF_HEXAGON_ISA_V60);
BCase(EF_HEXAGON_ISA_V62);
BCase(EF_HEXAGON_ISA_V65);
+ BCase(EF_HEXAGON_ISA_V66);
+ BCase(EF_HEXAGON_ISA_V67);
break;
case ELF::EM_AVR:
BCase(EF_AVR_ARCH_AVR1);
@@ -423,6 +429,7 @@ void ScalarBitSetTraits<ELFYAML::ELF_EF>::bitset(IO &IO,
BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX1010, EF_AMDGPU_MACH);
BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX1011, EF_AMDGPU_MACH);
BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX1012, EF_AMDGPU_MACH);
+ BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX1030, EF_AMDGPU_MACH);
BCase(EF_AMDGPU_XNACK);
BCase(EF_AMDGPU_SRAM_ECC);
break;
@@ -495,6 +502,9 @@ void ScalarEnumerationTraits<ELFYAML::ELF_SHT>::enumeration(
ECase(SHT_MIPS_DWARF);
ECase(SHT_MIPS_ABIFLAGS);
break;
+ case ELF::EM_RISCV:
+ ECase(SHT_RISCV_ATTRIBUTES);
+ break;
default:
// Nothing to do.
break;
@@ -654,6 +664,9 @@ void ScalarEnumerationTraits<ELFYAML::ELF_REL>::enumeration(
case ELF::EM_BPF:
#include "llvm/BinaryFormat/ELFRelocs/BPF.def"
break;
+ case ELF::EM_VE:
+#include "llvm/BinaryFormat/ELFRelocs/VE.def"
+ break;
case ELF::EM_PPC64:
#include "llvm/BinaryFormat/ELFRelocs/PowerPC64.def"
break;
@@ -820,6 +833,28 @@ void ScalarBitSetTraits<ELFYAML::MIPS_AFL_FLAGS1>::bitset(
#undef BCase
}
+void MappingTraits<ELFYAML::SectionHeader>::mapping(
+ IO &IO, ELFYAML::SectionHeader &SHdr) {
+ IO.mapRequired("Name", SHdr.Name);
+}
+
+void MappingTraits<ELFYAML::SectionHeaderTable>::mapping(
+ IO &IO, ELFYAML::SectionHeaderTable &SectionHeader) {
+ IO.mapOptional("Sections", SectionHeader.Sections);
+ IO.mapOptional("Excluded", SectionHeader.Excluded);
+ IO.mapOptional("NoHeaders", SectionHeader.NoHeaders);
+}
+
+StringRef MappingTraits<ELFYAML::SectionHeaderTable>::validate(
+ IO &IO, ELFYAML::SectionHeaderTable &SecHdrTable) {
+ if (SecHdrTable.NoHeaders && (SecHdrTable.Sections || SecHdrTable.Excluded))
+ return "NoHeaders can't be used together with Sections/Excluded";
+ if (!SecHdrTable.NoHeaders && !SecHdrTable.Sections && !SecHdrTable.Excluded)
+ return "SectionHeaderTable can't be empty. Use 'NoHeaders' key to drop the "
+ "section header table";
+ return StringRef();
+}
+
void MappingTraits<ELFYAML::FileHeader>::mapping(IO &IO,
ELFYAML::FileHeader &FileHdr) {
IO.mapRequired("Class", FileHdr.Class);
@@ -831,10 +866,16 @@ void MappingTraits<ELFYAML::FileHeader>::mapping(IO &IO,
IO.mapOptional("Flags", FileHdr.Flags, ELFYAML::ELF_EF(0));
IO.mapOptional("Entry", FileHdr.Entry, Hex64(0));
- IO.mapOptional("SHEntSize", FileHdr.SHEntSize);
- IO.mapOptional("SHOff", FileHdr.SHOff);
- IO.mapOptional("SHNum", FileHdr.SHNum);
- IO.mapOptional("SHStrNdx", FileHdr.SHStrNdx);
+ // obj2yaml does not dump these fields.
+ assert(!IO.outputting() ||
+ (!FileHdr.EPhOff && !FileHdr.EPhEntSize && !FileHdr.EPhNum));
+ IO.mapOptional("EPhOff", FileHdr.EPhOff);
+ IO.mapOptional("EPhEntSize", FileHdr.EPhEntSize);
+ IO.mapOptional("EPhNum", FileHdr.EPhNum);
+ IO.mapOptional("EShEntSize", FileHdr.EShEntSize);
+ IO.mapOptional("EShOff", FileHdr.EShOff);
+ IO.mapOptional("EShNum", FileHdr.EShNum);
+ IO.mapOptional("EShStrNdx", FileHdr.EShStrNdx);
}
void MappingTraits<ELFYAML::ProgramHeader>::mapping(
@@ -843,7 +884,7 @@ void MappingTraits<ELFYAML::ProgramHeader>::mapping(
IO.mapOptional("Flags", Phdr.Flags, ELFYAML::ELF_PF(0));
IO.mapOptional("Sections", Phdr.Sections);
IO.mapOptional("VAddr", Phdr.VAddr, Hex64(0));
- IO.mapOptional("PAddr", Phdr.PAddr, Hex64(0));
+ IO.mapOptional("PAddr", Phdr.PAddr, Phdr.VAddr);
IO.mapOptional("Align", Phdr.Align);
IO.mapOptional("FileSize", Phdr.FileSize);
IO.mapOptional("MemSize", Phdr.MemSize);
@@ -977,9 +1018,41 @@ struct NormalizedOther {
} // end anonymous namespace
+void ScalarTraits<ELFYAML::YAMLIntUInt>::output(const ELFYAML::YAMLIntUInt &Val,
+ void *Ctx, raw_ostream &Out) {
+ Out << Val;
+}
+
+StringRef ScalarTraits<ELFYAML::YAMLIntUInt>::input(StringRef Scalar, void *Ctx,
+ ELFYAML::YAMLIntUInt &Val) {
+ const bool Is64 = static_cast<ELFYAML::Object *>(Ctx)->Header.Class ==
+ ELFYAML::ELF_ELFCLASS(ELF::ELFCLASS64);
+ StringRef ErrMsg = "invalid number";
+ // We do not accept negative hex numbers because their meaning is ambiguous.
+ // For example, would -0xfffffffff mean 1 or INT32_MIN?
+ if (Scalar.empty() || Scalar.startswith("-0x"))
+ return ErrMsg;
+
+ if (Scalar.startswith("-")) {
+ const int64_t MinVal = Is64 ? INT64_MIN : INT32_MIN;
+ long long Int;
+ if (getAsSignedInteger(Scalar, /*Radix=*/0, Int) || (Int < MinVal))
+ return ErrMsg;
+ Val = Int;
+ return "";
+ }
+
+ const uint64_t MaxVal = Is64 ? UINT64_MAX : UINT32_MAX;
+ unsigned long long UInt;
+ if (getAsUnsignedInteger(Scalar, /*Radix=*/0, UInt) || (UInt > MaxVal))
+ return ErrMsg;
+ Val = UInt;
+ return "";
+}
+
void MappingTraits<ELFYAML::Symbol>::mapping(IO &IO, ELFYAML::Symbol &Symbol) {
IO.mapOptional("Name", Symbol.Name, StringRef());
- IO.mapOptional("NameIndex", Symbol.NameIndex);
+ IO.mapOptional("StName", Symbol.StName);
IO.mapOptional("Type", Symbol.Type, ELFYAML::ELF_STT(0));
IO.mapOptional("Section", Symbol.Section, StringRef());
IO.mapOptional("Index", Symbol.Index);
@@ -1001,8 +1074,6 @@ StringRef MappingTraits<ELFYAML::Symbol>::validate(IO &IO,
ELFYAML::Symbol &Symbol) {
if (Symbol.Index && Symbol.Section.data())
return "Index and Section cannot both be specified for Symbol";
- if (Symbol.NameIndex && !Symbol.Name.empty())
- return "Name and NameIndex cannot both be specified for Symbol";
return StringRef();
}
@@ -1010,10 +1081,11 @@ static void commonSectionMapping(IO &IO, ELFYAML::Section &Section) {
IO.mapOptional("Name", Section.Name, StringRef());
IO.mapRequired("Type", Section.Type);
IO.mapOptional("Flags", Section.Flags);
- IO.mapOptional("Address", Section.Address, Hex64(0));
+ IO.mapOptional("Address", Section.Address);
IO.mapOptional("Link", Section.Link, StringRef());
IO.mapOptional("AddressAlign", Section.AddressAlign, Hex64(0));
IO.mapOptional("EntSize", Section.EntSize);
+ IO.mapOptional("Offset", Section.Offset);
// obj2yaml does not dump these fields. They are expected to be empty when we
// are producing YAML, because yaml2obj sets appropriate values for them
@@ -1036,6 +1108,17 @@ static void sectionMapping(IO &IO, ELFYAML::DynamicSection &Section) {
static void sectionMapping(IO &IO, ELFYAML::RawContentSection &Section) {
commonSectionMapping(IO, Section);
IO.mapOptional("Content", Section.Content);
+
+ // We also support reading a content as array of bytes using the ContentArray
+ // key. obj2yaml never prints this field.
+ assert(!IO.outputting() || !Section.ContentBuf.hasValue());
+ IO.mapOptional("ContentArray", Section.ContentBuf);
+ if (Section.ContentBuf) {
+ if (Section.Content)
+ IO.setError("Content and ContentArray can't be used together");
+ Section.Content = yaml::BinaryRef(*Section.ContentBuf);
+ }
+
IO.mapOptional("Size", Section.Size);
IO.mapOptional("Info", Section.Info);
}
@@ -1053,6 +1136,13 @@ static void sectionMapping(IO &IO, ELFYAML::HashSection &Section) {
IO.mapOptional("Bucket", Section.Bucket);
IO.mapOptional("Chain", Section.Chain);
IO.mapOptional("Size", Section.Size);
+
+ // obj2yaml does not dump these fields. They can be used to override nchain
+ // and nbucket values for creating broken sections.
+ assert(!IO.outputting() ||
+ (!Section.NBucket.hasValue() && !Section.NChain.hasValue()));
+ IO.mapOptional("NChain", Section.NChain);
+ IO.mapOptional("NBucket", Section.NBucket);
}
static void sectionMapping(IO &IO, ELFYAML::NoteSection &Section) {
@@ -1128,6 +1218,7 @@ static void sectionMapping(IO &IO, ELFYAML::AddrsigSection &Section) {
static void fillMapping(IO &IO, ELFYAML::Fill &Fill) {
IO.mapOptional("Name", Fill.Name, StringRef());
IO.mapOptional("Pattern", Fill.Pattern);
+ IO.mapOptional("Offset", Fill.Offset);
IO.mapRequired("Size", Fill.Size);
}
@@ -1144,6 +1235,12 @@ static void sectionMapping(IO &IO,
IO.mapOptional("Content", Section.Content);
}
+static void sectionMapping(IO &IO, ELFYAML::CallGraphProfileSection &Section) {
+ commonSectionMapping(IO, Section);
+ IO.mapOptional("Entries", Section.Entries);
+ IO.mapOptional("Content", Section.Content);
+}
+
void MappingTraits<ELFYAML::SectionOrType>::mapping(
IO &IO, ELFYAML::SectionOrType &sectionOrType) {
IO.mapRequired("SectionOrType", sectionOrType.sectionNameOrType);
@@ -1277,6 +1374,11 @@ void MappingTraits<std::unique_ptr<ELFYAML::Chunk>>::mapping(
sectionMapping(IO,
*cast<ELFYAML::DependentLibrariesSection>(Section.get()));
break;
+ case ELF::SHT_LLVM_CALL_GRAPH_PROFILE:
+ if (!IO.outputting())
+ Section.reset(new ELFYAML::CallGraphProfileSection());
+ sectionMapping(IO, *cast<ELFYAML::CallGraphProfileSection>(Section.get()));
+ break;
default:
if (!IO.outputting()) {
StringRef Name;
@@ -1367,11 +1469,6 @@ StringRef MappingTraits<std::unique_ptr<ELFYAML::Chunk>>::validate(
if (!Sec->Symbols)
return {};
-
- for (const ELFYAML::AddrsigSymbol &AS : *Sec->Symbols)
- if (AS.Index && AS.Name)
- return "\"Index\" and \"Name\" cannot be used together when defining a "
- "symbol";
return {};
}
@@ -1458,6 +1555,12 @@ StringRef MappingTraits<std::unique_ptr<ELFYAML::Chunk>>::validate(
return {};
}
+ if (const auto *CGP = dyn_cast<ELFYAML::CallGraphProfileSection>(C.get())) {
+ if (CGP->Entries && CGP->Content)
+ return "\"Entries\" and \"Content\" can't be used together";
+ return {};
+ }
+
return {};
}
@@ -1553,7 +1656,7 @@ void MappingTraits<ELFYAML::Relocation>::mapping(IO &IO,
const auto *Object = static_cast<ELFYAML::Object *>(IO.getContext());
assert(Object && "The IO context is not initialized");
- IO.mapRequired("Offset", Rel.Offset);
+ IO.mapOptional("Offset", Rel.Offset, (Hex64)0);
IO.mapOptional("Symbol", Rel.Symbol);
if (Object->Header.Machine == ELFYAML::ELF_EM(ELF::EM_MIPS) &&
@@ -1567,7 +1670,7 @@ void MappingTraits<ELFYAML::Relocation>::mapping(IO &IO,
} else
IO.mapRequired("Type", Rel.Type);
- IO.mapOptional("Addend", Rel.Addend, (int64_t)0);
+ IO.mapOptional("Addend", Rel.Addend, (ELFYAML::YAMLIntUInt)0);
}
void MappingTraits<ELFYAML::Object>::mapping(IO &IO, ELFYAML::Object &Object) {
@@ -1575,19 +1678,21 @@ void MappingTraits<ELFYAML::Object>::mapping(IO &IO, ELFYAML::Object &Object) {
IO.setContext(&Object);
IO.mapTag("!ELF", true);
IO.mapRequired("FileHeader", Object.Header);
+ IO.mapOptional("SectionHeaderTable", Object.SectionHeaders);
IO.mapOptional("ProgramHeaders", Object.ProgramHeaders);
IO.mapOptional("Sections", Object.Chunks);
IO.mapOptional("Symbols", Object.Symbols);
IO.mapOptional("DynamicSymbols", Object.DynamicSymbols);
+ IO.mapOptional("DWARF", Object.DWARF);
+ if (Object.DWARF) {
+ Object.DWARF->IsLittleEndian =
+ Object.Header.Data == ELFYAML::ELF_ELFDATA(ELF::ELFDATA2LSB);
+ Object.DWARF->Is64BitAddrSize =
+ Object.Header.Class == ELFYAML::ELF_ELFCLASS(ELF::ELFCLASS64);
+ }
IO.setContext(nullptr);
}
-void MappingTraits<ELFYAML::AddrsigSymbol>::mapping(IO &IO, ELFYAML::AddrsigSymbol &Sym) {
- assert(IO.getContext() && "The IO context is not initialized");
- IO.mapOptional("Name", Sym.Name);
- IO.mapOptional("Index", Sym.Index);
-}
-
void MappingTraits<ELFYAML::LinkerOption>::mapping(IO &IO,
ELFYAML::LinkerOption &Opt) {
assert(IO.getContext() && "The IO context is not initialized");
@@ -1595,6 +1700,14 @@ void MappingTraits<ELFYAML::LinkerOption>::mapping(IO &IO,
IO.mapRequired("Value", Opt.Value);
}
+void MappingTraits<ELFYAML::CallGraphEntry>::mapping(
+ IO &IO, ELFYAML::CallGraphEntry &E) {
+ assert(IO.getContext() && "The IO context is not initialized");
+ IO.mapRequired("From", E.From);
+ IO.mapRequired("To", E.To);
+ IO.mapRequired("Weight", E.Weight);
+}
+
LLVM_YAML_STRONG_TYPEDEF(uint8_t, MIPS_AFL_REG)
LLVM_YAML_STRONG_TYPEDEF(uint8_t, MIPS_ABI_FP)
LLVM_YAML_STRONG_TYPEDEF(uint32_t, MIPS_AFL_EXT)
diff --git a/llvm/lib/ObjectYAML/MachOEmitter.cpp b/llvm/lib/ObjectYAML/MachOEmitter.cpp
index bda4aed885b48..680264484704b 100644
--- a/llvm/lib/ObjectYAML/MachOEmitter.cpp
+++ b/llvm/lib/ObjectYAML/MachOEmitter.cpp
@@ -15,6 +15,8 @@
#include "llvm/ObjectYAML/DWARFEmitter.h"
#include "llvm/ObjectYAML/ObjectYAML.h"
#include "llvm/ObjectYAML/yaml2obj.h"
+#include "llvm/Support/Errc.h"
+#include "llvm/Support/Error.h"
#include "llvm/Support/LEB128.h"
#include "llvm/Support/YAMLTraits.h"
#include "llvm/Support/raw_ostream.h"
@@ -33,12 +35,13 @@ public:
memset(reinterpret_cast<void *>(&Header), 0, sizeof(MachO::mach_header_64));
}
- void writeMachO(raw_ostream &OS);
+ Error writeMachO(raw_ostream &OS);
private:
void writeHeader(raw_ostream &OS);
void writeLoadCommands(raw_ostream &OS);
- void writeSectionData(raw_ostream &OS);
+ Error writeSectionData(raw_ostream &OS);
+ void writeRelocations(raw_ostream &OS);
void writeLinkEditData(raw_ostream &OS);
void writeBindOpcodes(raw_ostream &OS,
@@ -58,15 +61,23 @@ private:
MachOYAML::Object &Obj;
bool is64Bit;
uint64_t fileStart;
-
MachO::mach_header_64 Header;
+
+ // Old PPC Object Files didn't have __LINKEDIT segments, the data was just
+ // stuck at the end of the file.
+ bool FoundLinkEditSeg = false;
};
-void MachOWriter::writeMachO(raw_ostream &OS) {
+Error MachOWriter::writeMachO(raw_ostream &OS) {
fileStart = OS.tell();
writeHeader(OS);
writeLoadCommands(OS);
- writeSectionData(OS);
+ if (Error Err = writeSectionData(OS))
+ return Err;
+ writeRelocations(OS);
+ if (!FoundLinkEditSeg)
+ writeLinkEditData(OS);
+ return Error::success();
}
void MachOWriter::writeHeader(raw_ostream &OS) {
@@ -254,8 +265,7 @@ void MachOWriter::writeLoadCommands(raw_ostream &OS) {
}
}
-void MachOWriter::writeSectionData(raw_ostream &OS) {
- bool FoundLinkEditSeg = false;
+Error MachOWriter::writeSectionData(raw_ostream &OS) {
for (auto &LC : Obj.LoadCommands) {
switch (LC.Data.load_command_data.cmd) {
case MachO::LC_SEGMENT:
@@ -271,27 +281,37 @@ void MachOWriter::writeSectionData(raw_ostream &OS) {
ZeroToOffset(OS, Sec.offset);
// Zero Fill any data between the end of the last thing we wrote and the
// start of this section.
- assert((OS.tell() - fileStart <= Sec.offset ||
- Sec.offset == (uint32_t)0) &&
- "Wrote too much data somewhere, section offsets don't line up.");
+ if (OS.tell() - fileStart > Sec.offset && Sec.offset != (uint32_t)0)
+ return createStringError(
+ errc::invalid_argument,
+ "wrote too much data somewhere, section offsets don't line up");
if (0 == strncmp(&Sec.segname[0], "__DWARF", 16)) {
- if (0 == strncmp(&Sec.sectname[0], "__debug_str", 16)) {
- DWARFYAML::EmitDebugStr(OS, Obj.DWARF);
- } else if (0 == strncmp(&Sec.sectname[0], "__debug_abbrev", 16)) {
- DWARFYAML::EmitDebugAbbrev(OS, Obj.DWARF);
- } else if (0 == strncmp(&Sec.sectname[0], "__debug_aranges", 16)) {
- DWARFYAML::EmitDebugAranges(OS, Obj.DWARF);
- } else if (0 == strncmp(&Sec.sectname[0], "__debug_pubnames", 16)) {
- DWARFYAML::EmitPubSection(OS, Obj.DWARF.PubNames,
- Obj.IsLittleEndian);
+ Error Err = Error::success();
+ cantFail(std::move(Err));
+
+ if (0 == strncmp(&Sec.sectname[0], "__debug_str", 16))
+ Err = DWARFYAML::emitDebugStr(OS, Obj.DWARF);
+ else if (0 == strncmp(&Sec.sectname[0], "__debug_abbrev", 16))
+ Err = DWARFYAML::emitDebugAbbrev(OS, Obj.DWARF);
+ else if (0 == strncmp(&Sec.sectname[0], "__debug_aranges", 16))
+ Err = DWARFYAML::emitDebugAranges(OS, Obj.DWARF);
+ else if (0 == strncmp(&Sec.sectname[0], "__debug_ranges", 16))
+ Err = DWARFYAML::emitDebugRanges(OS, Obj.DWARF);
+ else if (0 == strncmp(&Sec.sectname[0], "__debug_pubnames", 16)) {
+ if (Obj.DWARF.PubNames)
+ Err = DWARFYAML::emitPubSection(OS, *Obj.DWARF.PubNames,
+ Obj.IsLittleEndian);
} else if (0 == strncmp(&Sec.sectname[0], "__debug_pubtypes", 16)) {
- DWARFYAML::EmitPubSection(OS, Obj.DWARF.PubTypes,
- Obj.IsLittleEndian);
- } else if (0 == strncmp(&Sec.sectname[0], "__debug_info", 16)) {
- DWARFYAML::EmitDebugInfo(OS, Obj.DWARF);
- } else if (0 == strncmp(&Sec.sectname[0], "__debug_line", 16)) {
- DWARFYAML::EmitDebugLine(OS, Obj.DWARF);
- }
+ if (Obj.DWARF.PubTypes)
+ Err = DWARFYAML::emitPubSection(OS, *Obj.DWARF.PubTypes,
+ Obj.IsLittleEndian);
+ } else if (0 == strncmp(&Sec.sectname[0], "__debug_info", 16))
+ Err = DWARFYAML::emitDebugInfo(OS, Obj.DWARF);
+ else if (0 == strncmp(&Sec.sectname[0], "__debug_line", 16))
+ Err = DWARFYAML::emitDebugLine(OS, Obj.DWARF);
+
+ if (Err)
+ return Err;
continue;
}
@@ -315,10 +335,62 @@ void MachOWriter::writeSectionData(raw_ostream &OS) {
break;
}
}
- // Old PPC Object Files didn't have __LINKEDIT segments, the data was just
- // stuck at the end of the file.
- if (!FoundLinkEditSeg)
- writeLinkEditData(OS);
+
+ return Error::success();
+}
+
+// The implementation of makeRelocationInfo and makeScatteredRelocationInfo is
+// consistent with how libObject parses MachO binary files. For the reference
+// see getStruct, getRelocation, getPlainRelocationPCRel,
+// getPlainRelocationLength and related methods in MachOObjectFile.cpp
+static MachO::any_relocation_info
+makeRelocationInfo(const MachOYAML::Relocation &R, bool IsLE) {
+ assert(!R.is_scattered && "non-scattered relocation expected");
+ MachO::any_relocation_info MRE;
+ MRE.r_word0 = R.address;
+ if (IsLE)
+ MRE.r_word1 = ((unsigned)R.symbolnum << 0) | ((unsigned)R.is_pcrel << 24) |
+ ((unsigned)R.length << 25) | ((unsigned)R.is_extern << 27) |
+ ((unsigned)R.type << 28);
+ else
+ MRE.r_word1 = ((unsigned)R.symbolnum << 8) | ((unsigned)R.is_pcrel << 7) |
+ ((unsigned)R.length << 5) | ((unsigned)R.is_extern << 4) |
+ ((unsigned)R.type << 0);
+ return MRE;
+}
+
+static MachO::any_relocation_info
+makeScatteredRelocationInfo(const MachOYAML::Relocation &R) {
+ assert(R.is_scattered && "scattered relocation expected");
+ MachO::any_relocation_info MRE;
+ MRE.r_word0 = (((unsigned)R.address << 0) | ((unsigned)R.type << 24) |
+ ((unsigned)R.length << 28) | ((unsigned)R.is_pcrel << 30) |
+ MachO::R_SCATTERED);
+ MRE.r_word1 = R.value;
+ return MRE;
+}
+
+void MachOWriter::writeRelocations(raw_ostream &OS) {
+ for (const MachOYAML::LoadCommand &LC : Obj.LoadCommands) {
+ switch (LC.Data.load_command_data.cmd) {
+ case MachO::LC_SEGMENT:
+ case MachO::LC_SEGMENT_64:
+ for (const MachOYAML::Section &Sec : LC.Sections) {
+ if (Sec.relocations.empty())
+ continue;
+ ZeroToOffset(OS, Sec.reloff);
+ for (const MachOYAML::Relocation &R : Sec.relocations) {
+ MachO::any_relocation_info MRE =
+ R.is_scattered ? makeScatteredRelocationInfo(R)
+ : makeRelocationInfo(R, Obj.IsLittleEndian);
+ if (Obj.IsLittleEndian != sys::IsLittleEndianHost)
+ MachO::swapStruct(MRE);
+ OS.write(reinterpret_cast<const char *>(&MRE),
+ sizeof(MachO::any_relocation_info));
+ }
+ }
+ }
+ }
}
void MachOWriter::writeBindOpcodes(
@@ -470,7 +542,7 @@ public:
UniversalWriter(yaml::YamlObjectFile &ObjectFile)
: ObjectFile(ObjectFile), fileStart(0) {}
- void writeMachO(raw_ostream &OS);
+ Error writeMachO(raw_ostream &OS);
private:
void writeFatHeader(raw_ostream &OS);
@@ -482,28 +554,33 @@ private:
uint64_t fileStart;
};
-void UniversalWriter::writeMachO(raw_ostream &OS) {
+Error UniversalWriter::writeMachO(raw_ostream &OS) {
fileStart = OS.tell();
if (ObjectFile.MachO) {
MachOWriter Writer(*ObjectFile.MachO);
- Writer.writeMachO(OS);
- return;
+ return Writer.writeMachO(OS);
}
writeFatHeader(OS);
writeFatArchs(OS);
auto &FatFile = *ObjectFile.FatMachO;
- assert(FatFile.FatArchs.size() >= FatFile.Slices.size() &&
- "Cannot write Slices if not decribed in FatArches");
+ if (FatFile.FatArchs.size() < FatFile.Slices.size())
+ return createStringError(
+ errc::invalid_argument,
+ "cannot write 'Slices' if not described in 'FatArches'");
+
for (size_t i = 0; i < FatFile.Slices.size(); i++) {
ZeroToOffset(OS, FatFile.FatArchs[i].offset);
MachOWriter Writer(FatFile.Slices[i]);
- Writer.writeMachO(OS);
+ if (Error Err = Writer.writeMachO(OS))
+ return Err;
auto SliceEnd = FatFile.FatArchs[i].offset + FatFile.FatArchs[i].size;
ZeroToOffset(OS, SliceEnd);
}
+
+ return Error::success();
}
void UniversalWriter::writeFatHeader(raw_ostream &OS) {
@@ -571,9 +648,13 @@ void UniversalWriter::ZeroToOffset(raw_ostream &OS, size_t Offset) {
namespace llvm {
namespace yaml {
-bool yaml2macho(YamlObjectFile &Doc, raw_ostream &Out, ErrorHandler /*EH*/) {
+bool yaml2macho(YamlObjectFile &Doc, raw_ostream &Out, ErrorHandler EH) {
UniversalWriter Writer(Doc);
- Writer.writeMachO(Out);
+ if (Error Err = Writer.writeMachO(Out)) {
+ handleAllErrors(std::move(Err),
+ [&](const ErrorInfoBase &Err) { EH(Err.message()); });
+ return false;
+ }
return true;
}
diff --git a/llvm/lib/ObjectYAML/MachOYAML.cpp b/llvm/lib/ObjectYAML/MachOYAML.cpp
index 0f7cd1e1495cb..86aad0233767e 100644
--- a/llvm/lib/ObjectYAML/MachOYAML.cpp
+++ b/llvm/lib/ObjectYAML/MachOYAML.cpp
@@ -107,6 +107,8 @@ void MappingTraits<MachOYAML::Object>::mapping(IO &IO,
Object.DWARF.IsLittleEndian = Object.IsLittleEndian;
IO.mapRequired("FileHeader", Object.Header);
+ Object.DWARF.Is64BitAddrSize = Object.Header.magic == MachO::MH_MAGIC_64 ||
+ Object.Header.magic == MachO::MH_CIGAM_64;
IO.mapOptional("LoadCommands", Object.LoadCommands);
if(!Object.LinkEdit.isEmpty() || !IO.outputting())
IO.mapOptional("LinkEditData", Object.LinkEdit);
@@ -273,6 +275,18 @@ void MappingTraits<MachO::dyld_info_command>::mapping(
IO.mapRequired("export_size", LoadCommand.export_size);
}
+void MappingTraits<MachOYAML::Relocation>::mapping(
+ IO &IO, MachOYAML::Relocation &Relocation) {
+ IO.mapRequired("address", Relocation.address);
+ IO.mapRequired("symbolnum", Relocation.symbolnum);
+ IO.mapRequired("pcrel", Relocation.is_pcrel);
+ IO.mapRequired("length", Relocation.length);
+ IO.mapRequired("extern", Relocation.is_extern);
+ IO.mapRequired("type", Relocation.type);
+ IO.mapRequired("scattered", Relocation.is_scattered);
+ IO.mapRequired("value", Relocation.value);
+}
+
void MappingTraits<MachOYAML::Section>::mapping(IO &IO,
MachOYAML::Section &Section) {
IO.mapRequired("sectname", Section.sectname);
@@ -288,6 +302,7 @@ void MappingTraits<MachOYAML::Section>::mapping(IO &IO,
IO.mapRequired("reserved2", Section.reserved2);
IO.mapOptional("reserved3", Section.reserved3);
IO.mapOptional("content", Section.content);
+ IO.mapOptional("relocations", Section.relocations);
}
StringRef
diff --git a/llvm/lib/ObjectYAML/WasmEmitter.cpp b/llvm/lib/ObjectYAML/WasmEmitter.cpp
index debc040587a87..cbb062d87ae63 100644
--- a/llvm/lib/ObjectYAML/WasmEmitter.cpp
+++ b/llvm/lib/ObjectYAML/WasmEmitter.cpp
@@ -41,8 +41,8 @@ private:
void writeSectionContent(raw_ostream &OS, WasmYAML::FunctionSection &Section);
void writeSectionContent(raw_ostream &OS, WasmYAML::TableSection &Section);
void writeSectionContent(raw_ostream &OS, WasmYAML::MemorySection &Section);
- void writeSectionContent(raw_ostream &OS, WasmYAML::GlobalSection &Section);
void writeSectionContent(raw_ostream &OS, WasmYAML::EventSection &Section);
+ void writeSectionContent(raw_ostream &OS, WasmYAML::GlobalSection &Section);
void writeSectionContent(raw_ostream &OS, WasmYAML::ExportSection &Section);
void writeSectionContent(raw_ostream &OS, WasmYAML::StartSection &Section);
void writeSectionContent(raw_ostream &OS, WasmYAML::ElemSection &Section);
@@ -415,6 +415,21 @@ void WasmWriter::writeSectionContent(raw_ostream &OS,
}
void WasmWriter::writeSectionContent(raw_ostream &OS,
+ WasmYAML::EventSection &Section) {
+ encodeULEB128(Section.Events.size(), OS);
+ uint32_t ExpectedIndex = NumImportedEvents;
+ for (auto &Event : Section.Events) {
+ if (Event.Index != ExpectedIndex) {
+ reportError("unexpected event index: " + Twine(Event.Index));
+ return;
+ }
+ ++ExpectedIndex;
+ encodeULEB128(Event.Attribute, OS);
+ encodeULEB128(Event.SigIndex, OS);
+ }
+}
+
+void WasmWriter::writeSectionContent(raw_ostream &OS,
WasmYAML::GlobalSection &Section) {
encodeULEB128(Section.Globals.size(), OS);
uint32_t ExpectedIndex = NumImportedGlobals;
@@ -431,21 +446,6 @@ void WasmWriter::writeSectionContent(raw_ostream &OS,
}
void WasmWriter::writeSectionContent(raw_ostream &OS,
- WasmYAML::EventSection &Section) {
- encodeULEB128(Section.Events.size(), OS);
- uint32_t ExpectedIndex = NumImportedEvents;
- for (auto &Event : Section.Events) {
- if (Event.Index != ExpectedIndex) {
- reportError("unexpected event index: " + Twine(Event.Index));
- return;
- }
- ++ExpectedIndex;
- encodeULEB128(Event.Attribute, OS);
- encodeULEB128(Event.SigIndex, OS);
- }
-}
-
-void WasmWriter::writeSectionContent(raw_ostream &OS,
WasmYAML::ElemSection &Section) {
encodeULEB128(Section.Segments.size(), OS);
for (auto &Segment : Section.Segments) {
@@ -532,8 +532,11 @@ void WasmWriter::writeRelocSection(raw_ostream &OS, WasmYAML::Section &Sec,
encodeULEB128(Reloc.Index, OS);
switch (Reloc.Type) {
case wasm::R_WASM_MEMORY_ADDR_LEB:
+ case wasm::R_WASM_MEMORY_ADDR_LEB64:
case wasm::R_WASM_MEMORY_ADDR_SLEB:
+ case wasm::R_WASM_MEMORY_ADDR_SLEB64:
case wasm::R_WASM_MEMORY_ADDR_I32:
+ case wasm::R_WASM_MEMORY_ADDR_I64:
case wasm::R_WASM_FUNCTION_OFFSET_I32:
case wasm::R_WASM_SECTION_OFFSET_I32:
encodeULEB128(Reloc.Addend, OS);
@@ -571,10 +574,10 @@ bool WasmWriter::writeWasm(raw_ostream &OS) {
writeSectionContent(StringStream, *S);
else if (auto S = dyn_cast<WasmYAML::MemorySection>(Sec.get()))
writeSectionContent(StringStream, *S);
- else if (auto S = dyn_cast<WasmYAML::GlobalSection>(Sec.get()))
- writeSectionContent(StringStream, *S);
else if (auto S = dyn_cast<WasmYAML::EventSection>(Sec.get()))
writeSectionContent(StringStream, *S);
+ else if (auto S = dyn_cast<WasmYAML::GlobalSection>(Sec.get()))
+ writeSectionContent(StringStream, *S);
else if (auto S = dyn_cast<WasmYAML::ExportSection>(Sec.get()))
writeSectionContent(StringStream, *S);
else if (auto S = dyn_cast<WasmYAML::StartSection>(Sec.get()))
diff --git a/llvm/lib/ObjectYAML/WasmYAML.cpp b/llvm/lib/ObjectYAML/WasmYAML.cpp
index 232d5122004a7..d1aa1181a3445 100644
--- a/llvm/lib/ObjectYAML/WasmYAML.cpp
+++ b/llvm/lib/ObjectYAML/WasmYAML.cpp
@@ -118,14 +118,14 @@ static void sectionMapping(IO &IO, WasmYAML::MemorySection &Section) {
IO.mapOptional("Memories", Section.Memories);
}
-static void sectionMapping(IO &IO, WasmYAML::GlobalSection &Section) {
+static void sectionMapping(IO &IO, WasmYAML::EventSection &Section) {
commonSectionMapping(IO, Section);
- IO.mapOptional("Globals", Section.Globals);
+ IO.mapOptional("Events", Section.Events);
}
-static void sectionMapping(IO &IO, WasmYAML::EventSection &Section) {
+static void sectionMapping(IO &IO, WasmYAML::GlobalSection &Section) {
commonSectionMapping(IO, Section);
- IO.mapOptional("Events", Section.Events);
+ IO.mapOptional("Globals", Section.Globals);
}
static void sectionMapping(IO &IO, WasmYAML::ExportSection &Section) {
@@ -227,16 +227,16 @@ void MappingTraits<std::unique_ptr<WasmYAML::Section>>::mapping(
Section.reset(new WasmYAML::MemorySection());
sectionMapping(IO, *cast<WasmYAML::MemorySection>(Section.get()));
break;
- case wasm::WASM_SEC_GLOBAL:
- if (!IO.outputting())
- Section.reset(new WasmYAML::GlobalSection());
- sectionMapping(IO, *cast<WasmYAML::GlobalSection>(Section.get()));
- break;
case wasm::WASM_SEC_EVENT:
if (!IO.outputting())
Section.reset(new WasmYAML::EventSection());
sectionMapping(IO, *cast<WasmYAML::EventSection>(Section.get()));
break;
+ case wasm::WASM_SEC_GLOBAL:
+ if (!IO.outputting())
+ Section.reset(new WasmYAML::GlobalSection());
+ sectionMapping(IO, *cast<WasmYAML::GlobalSection>(Section.get()));
+ break;
case wasm::WASM_SEC_EXPORT:
if (!IO.outputting())
Section.reset(new WasmYAML::ExportSection());
@@ -433,6 +433,11 @@ void MappingTraits<wasm::WasmInitExpr>::mapping(IO &IO,
case wasm::WASM_OPCODE_GLOBAL_GET:
IO.mapRequired("Index", Expr.Value.Global);
break;
+ case wasm::WASM_OPCODE_REF_NULL: {
+ WasmYAML::ValueType Ty = wasm::WASM_TYPE_EXTERNREF;
+ IO.mapRequired("Type", Ty);
+ break;
+ }
}
}
@@ -517,6 +522,7 @@ void ScalarBitSetTraits<WasmYAML::LimitFlags>::bitset(
#define BCase(X) IO.bitSetCase(Value, #X, wasm::WASM_LIMITS_FLAG_##X)
BCase(HAS_MAX);
BCase(IS_SHARED);
+ BCase(IS_64);
#undef BCase
}
@@ -559,6 +565,8 @@ void ScalarEnumerationTraits<WasmYAML::ValueType>::enumeration(
ECase(F64);
ECase(V128);
ECase(FUNCREF);
+ ECase(EXNREF);
+ ECase(EXTERNREF);
ECase(FUNC);
#undef ECase
}
@@ -583,6 +591,7 @@ void ScalarEnumerationTraits<WasmYAML::Opcode>::enumeration(
ECase(F64_CONST);
ECase(F32_CONST);
ECase(GLOBAL_GET);
+ ECase(REF_NULL);
#undef ECase
}
diff --git a/llvm/lib/ObjectYAML/yaml2obj.cpp b/llvm/lib/ObjectYAML/yaml2obj.cpp
index c18fa5cfdb5e7..a04345f1294a2 100644
--- a/llvm/lib/ObjectYAML/yaml2obj.cpp
+++ b/llvm/lib/ObjectYAML/yaml2obj.cpp
@@ -19,7 +19,7 @@ namespace llvm {
namespace yaml {
bool convertYAML(yaml::Input &YIn, raw_ostream &Out, ErrorHandler ErrHandler,
- unsigned DocNum) {
+ unsigned DocNum, uint64_t MaxSize) {
unsigned CurDocNum = 0;
do {
if (++CurDocNum != DocNum)
@@ -33,7 +33,7 @@ bool convertYAML(yaml::Input &YIn, raw_ostream &Out, ErrorHandler ErrHandler,
}
if (Doc.Elf)
- return yaml2elf(*Doc.Elf, Out, ErrHandler);
+ return yaml2elf(*Doc.Elf, Out, ErrHandler, MaxSize);
if (Doc.Coff)
return yaml2coff(*Doc.Coff, Out, ErrHandler);
if (Doc.MachO || Doc.FatMachO)