summaryrefslogtreecommitdiffstats
path: root/llvm/tools/yaml2obj/yaml2dwarf.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'llvm/tools/yaml2obj/yaml2dwarf.cpp')
-rw-r--r--llvm/tools/yaml2obj/yaml2dwarf.cpp55
1 files changed, 40 insertions, 15 deletions
diff --git a/llvm/tools/yaml2obj/yaml2dwarf.cpp b/llvm/tools/yaml2obj/yaml2dwarf.cpp
index 6096f4c5f19..525c44ce7ab 100644
--- a/llvm/tools/yaml2obj/yaml2dwarf.cpp
+++ b/llvm/tools/yaml2obj/yaml2dwarf.cpp
@@ -16,9 +16,31 @@
#include "llvm/Support/Error.h"
#include "llvm/Support/LEB128.h"
#include "llvm/Support/raw_ostream.h"
+#include "llvm/Support/SwapByteOrder.h"
using namespace llvm;
+template <typename T>
+void writeInteger(T Integer, raw_ostream &OS, bool IsLittleEndian) {
+ if (IsLittleEndian != sys::IsLittleEndianHost)
+ sys::swapByteOrder(Integer);
+ OS.write(reinterpret_cast<char *>(&Integer), sizeof(T));
+}
+
+void 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)
+ writeInteger((uint32_t)Integer, OS, IsLittleEndian);
+ else if (2 == Size)
+ writeInteger((uint16_t)Integer, OS, IsLittleEndian);
+ else if (1 == Size)
+ writeInteger((uint8_t)Integer, OS, IsLittleEndian);
+ else
+ assert(false && "Invalid integer write size.");
+}
+
void ZeroFillBytes(raw_ostream &OS, size_t Size) {
std::vector<uint8_t> FillData;
FillData.insert(FillData.begin(), Size, 0);
@@ -49,34 +71,37 @@ void yaml2debug_abbrev(raw_ostream &OS, const DWARFYAML::Data &DI) {
void yaml2debug_aranges(raw_ostream &OS, const DWARFYAML::Data &DI) {
for (auto Range : DI.ARanges) {
auto HeaderStart = OS.tell();
- OS.write(reinterpret_cast<char *>(&Range.Length), 4);
- OS.write(reinterpret_cast<char *>(&Range.Version), 2);
- OS.write(reinterpret_cast<char *>(&Range.CuOffset), 4);
- OS.write(reinterpret_cast<char *>(&Range.AddrSize), 1);
- OS.write(reinterpret_cast<char *>(&Range.SegSize), 1);
+ writeInteger((uint32_t)Range.Length, OS, DI.IsLittleEndian);
+ writeInteger((uint16_t)Range.Version, OS, DI.IsLittleEndian);
+ writeInteger((uint32_t)Range.CuOffset, OS, DI.IsLittleEndian);
+ writeInteger((uint8_t)Range.AddrSize, OS, DI.IsLittleEndian);
+ writeInteger((uint8_t)Range.SegSize, OS, DI.IsLittleEndian);
auto HeaderSize = OS.tell() - HeaderStart;
auto FirstDescriptor = alignTo(HeaderSize, Range.AddrSize * 2);
ZeroFillBytes(OS, FirstDescriptor - HeaderSize);
for (auto Descriptor : Range.Descriptors) {
- OS.write(reinterpret_cast<char *>(&Descriptor.Address), Range.AddrSize);
- OS.write(reinterpret_cast<char *>(&Descriptor.Length), Range.AddrSize);
+ writeVariableSizedInteger(Descriptor.Address, Range.AddrSize, OS,
+ DI.IsLittleEndian);
+ writeVariableSizedInteger(Descriptor.Length, Range.AddrSize, OS,
+ DI.IsLittleEndian);
}
ZeroFillBytes(OS, Range.AddrSize * 2);
}
}
-void yaml2pubsection(raw_ostream &OS, const DWARFYAML::PubSection &Sect) {
- OS.write(reinterpret_cast<const char *>(&Sect.Length), 4);
- OS.write(reinterpret_cast<const char *>(&Sect.Version), 2);
- OS.write(reinterpret_cast<const char *>(&Sect.UnitOffset), 4);
- OS.write(reinterpret_cast<const char *>(&Sect.UnitSize), 4);
+void yaml2pubsection(raw_ostream &OS, const DWARFYAML::PubSection &Sect,
+ bool IsLittleEndian) {
+ writeInteger((uint32_t)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) {
- OS.write(reinterpret_cast<const char *>(&Entry.DieOffset), 4);
+ writeInteger((uint32_t)Entry.DieOffset, OS, IsLittleEndian);
if (Sect.IsGNUStyle)
- OS.write(reinterpret_cast<const char *>(&Entry.Descriptor), 4);
+ writeInteger((uint32_t)Entry.Descriptor, OS, IsLittleEndian);
OS.write(Entry.Name.data(), Entry.Name.size());
OS.write('\0');
}
-}
+} \ No newline at end of file
OpenPOWER on IntegriCloud