Don't assume little endian in StreamReader / StreamWriter.
authorZachary Turner <zturner@google.com>
Sat, 18 Feb 2017 01:35:33 +0000 (01:35 +0000)
committerZachary Turner <zturner@google.com>
Sat, 18 Feb 2017 01:35:33 +0000 (01:35 +0000)
In an effort to generalize this so it can be used by more than
just PDB code, we shouldn't assume little endian.

llvm-svn: 295525

21 files changed:
llvm/include/llvm/DebugInfo/CodeView/CodeViewRecordIO.h
llvm/include/llvm/DebugInfo/CodeView/TypeSerializer.h
llvm/include/llvm/DebugInfo/MSF/StreamReader.h
llvm/include/llvm/DebugInfo/MSF/StreamWriter.h
llvm/lib/DebugInfo/CodeView/CVTypeVisitor.cpp
llvm/lib/DebugInfo/CodeView/CodeViewRecordIO.cpp
llvm/lib/DebugInfo/CodeView/RecordSerialization.cpp
llvm/lib/DebugInfo/CodeView/TypeSerializer.cpp
llvm/lib/DebugInfo/MSF/StreamReader.cpp
llvm/lib/DebugInfo/MSF/StreamWriter.cpp
llvm/lib/DebugInfo/PDB/Native/DbiStream.cpp
llvm/lib/DebugInfo/PDB/Native/DbiStreamBuilder.cpp
llvm/lib/DebugInfo/PDB/Native/HashTable.cpp
llvm/lib/DebugInfo/PDB/Native/ModStream.cpp
llvm/lib/DebugInfo/PDB/Native/NamedStreamMap.cpp
llvm/lib/DebugInfo/PDB/Native/PDBFile.cpp
llvm/lib/DebugInfo/PDB/Native/PDBFileBuilder.cpp
llvm/lib/DebugInfo/PDB/Native/StringTable.cpp
llvm/lib/DebugInfo/PDB/Native/StringTableBuilder.cpp
llvm/tools/llvm-readobj/COFFDumper.cpp
llvm/unittests/DebugInfo/PDB/MappedBlockStreamTest.cpp

index 5a036b9..292f43f 100644 (file)
@@ -59,9 +59,9 @@ public:
 
   template <typename T> Error mapInteger(T &Value) {
     if (isWriting())
-      return Writer->writeInteger(Value);
+      return Writer->writeInteger(Value, llvm::support::little);
 
-    return Reader->readInteger(Value);
+    return Reader->readInteger(Value, llvm::support::little);
   }
 
   template <typename T> Error mapEnum(T &Value) {
@@ -93,7 +93,7 @@ public:
     SizeType Size;
     if (isWriting()) {
       Size = static_cast<SizeType>(Items.size());
-      if (auto EC = Writer->writeInteger(Size))
+      if (auto EC = Writer->writeInteger(Size, llvm::support::little))
         return EC;
 
       for (auto &X : Items) {
@@ -101,7 +101,7 @@ public:
           return EC;
       }
     } else {
-      if (auto EC = Reader->readInteger(Size))
+      if (auto EC = Reader->readInteger(Size, llvm::support::little))
         return EC;
       for (SizeType I = 0; I < Size; ++I) {
         typename T::value_type Item;
index e059221..f1bfffe 100644 (file)
@@ -109,7 +109,7 @@ private:
   Error visitKnownMemberImpl(CVMemberRecord &CVR, RecordType &Record) {
     assert(CVR.Kind == static_cast<TypeLeafKind>(Record.getKind()));
 
-    if (auto EC = Writer.writeEnum(CVR.Kind))
+    if (auto EC = Writer.writeEnum(CVR.Kind, llvm::support::little))
       return EC;
 
     if (auto EC = Mapping.visitKnownMember(CVR, Record))
index fc2ca78..0126122 100644 (file)
@@ -29,28 +29,42 @@ public:
 
   Error readLongestContiguousChunk(ArrayRef<uint8_t> &Buffer);
   Error readBytes(ArrayRef<uint8_t> &Buffer, uint32_t Size);
-  Error readInteger(uint8_t &Dest);
-  Error readInteger(uint16_t &Dest);
-  Error readInteger(uint32_t &Dest);
-  Error readInteger(uint64_t &Dest);
-  Error readInteger(int8_t &Dest);
-  Error readInteger(int16_t &Dest);
-  Error readInteger(int32_t &Dest);
-  Error readInteger(int64_t &Dest);
+
+  template <typename T>
+  Error readInteger(T &Dest,
+                    llvm::support::endianness Endian = llvm::support::native) {
+    static_assert(std::is_integral<T>::value,
+                  "Cannot call readInteger with non-integral value!");
+
+    ArrayRef<uint8_t> Bytes;
+    if (auto EC = readBytes(Bytes, sizeof(T)))
+      return EC;
+
+    Dest = llvm::support::endian::read<T, llvm::support::unaligned>(
+        Bytes.data(), Endian);
+    return Error::success();
+  }
+
   Error readZeroString(StringRef &Dest);
   Error readFixedString(StringRef &Dest, uint32_t Length);
   Error readStreamRef(ReadableStreamRef &Ref);
   Error readStreamRef(ReadableStreamRef &Ref, uint32_t Length);
 
-  template <typename T> Error readEnum(T &Dest) {
+  template <typename T>
+  Error readEnum(T &Dest,
+                 llvm::support::endianness Endian = llvm::support::native) {
+    static_assert(std::is_enum<T>::value,
+                  "Cannot call readEnum with non-enum value!");
     typename std::underlying_type<T>::type N;
-    if (auto EC = readInteger(N))
+    if (auto EC = readInteger(N, Endian))
       return EC;
     Dest = static_cast<T>(N);
     return Error::success();
   }
 
   template <typename T> Error readObject(const T *&Dest) {
+    static_assert(std::is_trivially_copyable<T>::value,
+                  "Can only read trivially copyable object types!");
     ArrayRef<uint8_t> Buffer;
     if (auto EC = readBytes(Buffer, sizeof(T)))
       return EC;
@@ -60,6 +74,8 @@ public:
 
   template <typename T>
   Error readArray(ArrayRef<T> &Array, uint32_t NumElements) {
+    static_assert(std::is_trivially_copyable<T>::value,
+                  "Can only read trivially copyable object types!");
     ArrayRef<uint8_t> Bytes;
     if (NumElements == 0) {
       Array = ArrayRef<T>();
@@ -86,6 +102,8 @@ public:
 
   template <typename T>
   Error readArray(FixedStreamArray<T> &Array, uint32_t NumItems) {
+    static_assert(std::is_trivially_copyable<T>::value,
+                  "Can only read trivially copyable object types!");
     if (NumItems == 0) {
       Array = FixedStreamArray<T>();
       return Error::success();
index 2bb1443..87af18f 100644 (file)
@@ -15,6 +15,7 @@
 #include "llvm/DebugInfo/MSF/MSFError.h"
 #include "llvm/DebugInfo/MSF/StreamArray.h"
 #include "llvm/DebugInfo/MSF/StreamRef.h"
+#include "llvm/Support/Endian.h"
 #include "llvm/Support/Error.h"
 #include <cstdint>
 #include <type_traits>
@@ -28,22 +29,31 @@ public:
   explicit StreamWriter(WritableStreamRef Stream);
 
   Error writeBytes(ArrayRef<uint8_t> Buffer);
-  Error writeInteger(uint8_t Int);
-  Error writeInteger(uint16_t Dest);
-  Error writeInteger(uint32_t Dest);
-  Error writeInteger(uint64_t Dest);
-  Error writeInteger(int8_t Int);
-  Error writeInteger(int16_t Dest);
-  Error writeInteger(int32_t Dest);
-  Error writeInteger(int64_t Dest);
+
+  template <typename T>
+  Error writeInteger(T Value,
+                     llvm::support::endianness Endian = llvm::support::native) {
+    static_assert(std::is_integral<T>::value,
+                  "Cannot call writeInteger with non-integral value!");
+    uint8_t Buffer[sizeof(T)];
+    llvm::support::endian::write<T, llvm::support::unaligned>(Buffer, Value,
+                                                              Endian);
+    return writeBytes(Buffer);
+  }
+
   Error writeZeroString(StringRef Str);
   Error writeFixedString(StringRef Str);
   Error writeStreamRef(ReadableStreamRef Ref);
   Error writeStreamRef(ReadableStreamRef Ref, uint32_t Size);
 
-  template <typename T> Error writeEnum(T Num) {
-    return writeInteger(
-        static_cast<typename std::underlying_type<T>::type>(Num));
+  template <typename T>
+  Error writeEnum(T Num,
+                  llvm::support::endianness Endian = llvm::support::native) {
+    static_assert(std::is_enum<T>::value,
+                  "Cannot call writeEnum with non-Enum type");
+
+    using U = typename std::underlying_type<T>::type;
+    return writeInteger<U>(static_cast<U>(Num), Endian);
   }
 
   template <typename T> Error writeObject(const T &Obj) {
@@ -51,11 +61,15 @@ public:
                   "writeObject should not be used with pointers, to write "
                   "the pointed-to value dereference the pointer before calling "
                   "writeObject");
+    static_assert(std::is_trivially_copyable<T>::value,
+                  "Can only serialize trivially copyable object types");
     return writeBytes(
         ArrayRef<uint8_t>(reinterpret_cast<const uint8_t *>(&Obj), sizeof(T)));
   }
 
   template <typename T> Error writeArray(ArrayRef<T> Array) {
+    static_assert(std::is_trivially_copyable<T>::value,
+                  "Can only serialize trivially copyable object types");
     if (Array.empty())
       return Error::success();
 
@@ -73,6 +87,8 @@ public:
   }
 
   template <typename T> Error writeArray(FixedStreamArray<T> Array) {
+    static_assert(std::is_trivially_copyable<T>::value,
+                  "Can only serialize trivially copyable object types");
     return writeStreamRef(Array.getUnderlyingStream());
   }
 
index 4816025..db5db3e 100644 (file)
@@ -182,7 +182,7 @@ Error CVTypeVisitor::visitFieldListMemberStream(msf::StreamReader Reader) {
 
   TypeLeafKind Leaf;
   while (!Reader.empty()) {
-    if (auto EC = Reader.readEnum(Leaf))
+    if (auto EC = Reader.readEnum(Leaf, llvm::support::little))
       return EC;
 
     CVMemberRecord Record;
index 9bd85cf..b7e74a1 100644 (file)
@@ -87,13 +87,14 @@ Error CodeViewRecordIO::mapByteVectorTail(std::vector<uint8_t> &Bytes) {
 
 Error CodeViewRecordIO::mapInteger(TypeIndex &TypeInd) {
   if (isWriting()) {
-    if (auto EC = Writer->writeInteger(TypeInd.getIndex()))
+    if (auto EC =
+            Writer->writeInteger(TypeInd.getIndex(), llvm::support::little))
       return EC;
     return Error::success();
   }
 
   uint32_t I;
-  if (auto EC = Reader->readInteger(I))
+  if (auto EC = Reader->readInteger(I, llvm::support::little))
     return EC;
   TypeInd.setIndex(I);
   return Error::success();
@@ -176,7 +177,7 @@ Error CodeViewRecordIO::mapStringZVectorZ(std::vector<StringRef> &Value) {
       if (auto EC = mapStringZ(V))
         return EC;
     }
-    if (auto EC = Writer->writeInteger(uint8_t(0)))
+    if (auto EC = Writer->writeInteger<uint8_t>(0, llvm::support::little))
       return EC;
   } else {
     StringRef S;
@@ -194,24 +195,28 @@ Error CodeViewRecordIO::mapStringZVectorZ(std::vector<StringRef> &Value) {
 Error CodeViewRecordIO::writeEncodedSignedInteger(const int64_t &Value) {
   assert(Value < 0 && "Encoded integer is not signed!");
   if (Value >= std::numeric_limits<int8_t>::min()) {
-    if (auto EC = Writer->writeInteger(static_cast<uint16_t>(LF_CHAR)))
+    if (auto EC =
+            Writer->writeInteger<uint16_t>(LF_CHAR, llvm::support::little))
       return EC;
-    if (auto EC = Writer->writeInteger(static_cast<int8_t>(Value)))
+    if (auto EC = Writer->writeInteger<int8_t>(Value, llvm::support::little))
       return EC;
   } else if (Value >= std::numeric_limits<int16_t>::min()) {
-    if (auto EC = Writer->writeInteger(static_cast<uint16_t>(LF_SHORT)))
+    if (auto EC =
+            Writer->writeInteger<uint16_t>(LF_SHORT, llvm::support::little))
       return EC;
-    if (auto EC = Writer->writeInteger(static_cast<int16_t>(Value)))
+    if (auto EC = Writer->writeInteger<int16_t>(Value, llvm::support::little))
       return EC;
   } else if (Value >= std::numeric_limits<int32_t>::min()) {
-    if (auto EC = Writer->writeInteger(static_cast<uint16_t>(LF_LONG)))
+    if (auto EC =
+            Writer->writeInteger<uint16_t>(LF_LONG, llvm::support::little))
       return EC;
-    if (auto EC = Writer->writeInteger(static_cast<int32_t>(Value)))
+    if (auto EC = Writer->writeInteger<int32_t>(Value, llvm::support::little))
       return EC;
   } else {
-    if (auto EC = Writer->writeInteger(static_cast<uint16_t>(LF_QUADWORD)))
+    if (auto EC =
+            Writer->writeInteger<uint16_t>(LF_QUADWORD, llvm::support::little))
       return EC;
-    if (auto EC = Writer->writeInteger(Value))
+    if (auto EC = Writer->writeInteger(Value, llvm::support::little))
       return EC;
   }
   return Error::success();
@@ -219,22 +224,25 @@ Error CodeViewRecordIO::writeEncodedSignedInteger(const int64_t &Value) {
 
 Error CodeViewRecordIO::writeEncodedUnsignedInteger(const uint64_t &Value) {
   if (Value < LF_NUMERIC) {
-    if (auto EC = Writer->writeInteger(static_cast<uint16_t>(Value)))
+    if (auto EC = Writer->writeInteger<uint16_t>(Value, llvm::support::little))
       return EC;
   } else if (Value <= std::numeric_limits<uint16_t>::max()) {
-    if (auto EC = Writer->writeInteger(static_cast<uint16_t>(LF_USHORT)))
+    if (auto EC =
+            Writer->writeInteger<uint16_t>(LF_USHORT, llvm::support::little))
       return EC;
-    if (auto EC = Writer->writeInteger(static_cast<uint16_t>(Value)))
+    if (auto EC = Writer->writeInteger<uint16_t>(Value, llvm::support::little))
       return EC;
   } else if (Value <= std::numeric_limits<uint32_t>::max()) {
-    if (auto EC = Writer->writeInteger(static_cast<uint16_t>(LF_ULONG)))
+    if (auto EC =
+            Writer->writeInteger<uint16_t>(LF_ULONG, llvm::support::little))
       return EC;
-    if (auto EC = Writer->writeInteger(static_cast<uint32_t>(Value)))
+    if (auto EC = Writer->writeInteger<uint32_t>(Value, llvm::support::little))
       return EC;
   } else {
-    if (auto EC = Writer->writeInteger(static_cast<uint16_t>(LF_UQUADWORD)))
+    if (auto EC =
+            Writer->writeInteger<uint16_t>(LF_UQUADWORD, llvm::support::little))
       return EC;
-    if (auto EC = Writer->writeInteger(Value))
+    if (auto EC = Writer->writeInteger(Value, llvm::support::little))
       return EC;
   }
 
index 6f29caa..6be9a05 100644 (file)
@@ -37,7 +37,7 @@ Error llvm::codeview::consume(msf::StreamReader &Reader, APSInt &Num) {
   // Used to avoid overload ambiguity on APInt construtor.
   bool FalseVal = false;
   uint16_t Short;
-  if (auto EC = Reader.readInteger(Short))
+  if (auto EC = Reader.readInteger(Short, llvm::support::little))
     return EC;
 
   if (Short < LF_NUMERIC) {
@@ -49,49 +49,49 @@ Error llvm::codeview::consume(msf::StreamReader &Reader, APSInt &Num) {
   switch (Short) {
   case LF_CHAR: {
     int8_t N;
-    if (auto EC = Reader.readInteger(N))
+    if (auto EC = Reader.readInteger(N, llvm::support::little))
       return EC;
     Num = APSInt(APInt(8, N, true), false);
     return Error::success();
   }
   case LF_SHORT: {
     int16_t N;
-    if (auto EC = Reader.readInteger(N))
+    if (auto EC = Reader.readInteger(N, llvm::support::little))
       return EC;
     Num = APSInt(APInt(16, N, true), false);
     return Error::success();
   }
   case LF_USHORT: {
     uint16_t N;
-    if (auto EC = Reader.readInteger(N))
+    if (auto EC = Reader.readInteger(N, llvm::support::little))
       return EC;
     Num = APSInt(APInt(16, N, false), true);
     return Error::success();
   }
   case LF_LONG: {
     int32_t N;
-    if (auto EC = Reader.readInteger(N))
+    if (auto EC = Reader.readInteger(N, llvm::support::little))
       return EC;
     Num = APSInt(APInt(32, N, true), false);
     return Error::success();
   }
   case LF_ULONG: {
     uint32_t N;
-    if (auto EC = Reader.readInteger(N))
+    if (auto EC = Reader.readInteger(N, llvm::support::little))
       return EC;
     Num = APSInt(APInt(32, N, FalseVal), true);
     return Error::success();
   }
   case LF_QUADWORD: {
     int64_t N;
-    if (auto EC = Reader.readInteger(N))
+    if (auto EC = Reader.readInteger(N, llvm::support::little))
       return EC;
     Num = APSInt(APInt(64, N, true), false);
     return Error::success();
   }
   case LF_UQUADWORD: {
     uint64_t N;
-    if (auto EC = Reader.readInteger(N))
+    if (auto EC = Reader.readInteger(N, llvm::support::little))
       return EC;
     Num = APSInt(APInt(64, N, false), true);
     return Error::success();
@@ -124,7 +124,7 @@ Error llvm::codeview::consume_numeric(msf::StreamReader &Reader,
 }
 
 Error llvm::codeview::consume(msf::StreamReader &Reader, uint32_t &Item) {
-  return Reader.readInteger(Item);
+  return Reader.readInteger(Item, llvm::support::little);
 }
 
 Error llvm::codeview::consume(StringRef &Data, uint32_t &Item) {
@@ -137,7 +137,7 @@ Error llvm::codeview::consume(StringRef &Data, uint32_t &Item) {
 }
 
 Error llvm::codeview::consume(msf::StreamReader &Reader, int32_t &Item) {
-  return Reader.readInteger(Item);
+  return Reader.readInteger(Item, llvm::support::little);
 }
 
 Error llvm::codeview::consume(msf::StreamReader &Reader, StringRef &Item) {
index f24fcff..299e9ad 100644 (file)
@@ -76,7 +76,7 @@ TypeSerializer::addPadding(MutableArrayRef<uint8_t> Record) {
   int N = PaddingBytes;
   while (PaddingBytes > 0) {
     uint8_t Pad = static_cast<uint8_t>(LF_PAD0 + PaddingBytes);
-    if (auto EC = Writer.writeInteger(Pad))
+    if (auto EC = Writer.writeInteger(Pad, llvm::support::little))
       return std::move(EC);
     --PaddingBytes;
   }
@@ -207,11 +207,11 @@ Error TypeSerializer::visitMemberEnd(CVMemberRecord &Record) {
     msf::StreamWriter CW(CS);
     if (auto EC = CW.writeBytes(CopyData))
       return EC;
-    if (auto EC = CW.writeEnum(TypeLeafKind::LF_INDEX))
+    if (auto EC = CW.writeEnum(TypeLeafKind::LF_INDEX, llvm::support::little))
       return EC;
-    if (auto EC = CW.writeInteger(uint16_t(0)))
+    if (auto EC = CW.writeInteger<uint16_t>(0, llvm::support::little))
       return EC;
-    if (auto EC = CW.writeInteger(uint32_t(0xB0C0B0C0)))
+    if (auto EC = CW.writeInteger<uint32_t>(0xB0C0B0C0, llvm::support::little))
       return EC;
     FieldListSegments.push_back(SavedSegment);
 
index b85fd14..d460a4e 100644 (file)
@@ -31,70 +31,6 @@ Error StreamReader::readBytes(ArrayRef<uint8_t> &Buffer, uint32_t Size) {
   return Error::success();
 }
 
-Error StreamReader::readInteger(uint8_t &Dest) {
-  const uint8_t *P;
-  if (auto EC = readObject(P))
-    return EC;
-  Dest = *P;
-  return Error::success();
-}
-
-Error StreamReader::readInteger(uint16_t &Dest) {
-  const support::ulittle16_t *P;
-  if (auto EC = readObject(P))
-    return EC;
-  Dest = *P;
-  return Error::success();
-}
-
-Error StreamReader::readInteger(uint32_t &Dest) {
-  const support::ulittle32_t *P;
-  if (auto EC = readObject(P))
-    return EC;
-  Dest = *P;
-  return Error::success();
-}
-
-Error StreamReader::readInteger(uint64_t &Dest) {
-  const support::ulittle64_t *P;
-  if (auto EC = readObject(P))
-    return EC;
-  Dest = *P;
-  return Error::success();
-}
-
-Error StreamReader::readInteger(int8_t &Dest) {
-  const int8_t *P;
-  if (auto EC = readObject(P))
-    return EC;
-  Dest = *P;
-  return Error::success();
-}
-
-Error StreamReader::readInteger(int16_t &Dest) {
-  const support::little16_t *P;
-  if (auto EC = readObject(P))
-    return EC;
-  Dest = *P;
-  return Error::success();
-}
-
-Error StreamReader::readInteger(int32_t &Dest) {
-  const support::little32_t *P;
-  if (auto EC = readObject(P))
-    return EC;
-  Dest = *P;
-  return Error::success();
-}
-
-Error StreamReader::readInteger(int64_t &Dest) {
-  const support::little64_t *P;
-  if (auto EC = readObject(P))
-    return EC;
-  Dest = *P;
-  return Error::success();
-}
-
 Error StreamReader::readZeroString(StringRef &Dest) {
   uint32_t Length = 0;
   // First compute the length of the string by reading 1 byte at a time.
index cdae7c5..a24fb04 100644 (file)
@@ -25,34 +25,6 @@ Error StreamWriter::writeBytes(ArrayRef<uint8_t> Buffer) {
   return Error::success();
 }
 
-Error StreamWriter::writeInteger(uint8_t Int) { return writeObject(Int); }
-
-Error StreamWriter::writeInteger(uint16_t Int) {
-  return writeObject(support::ulittle16_t(Int));
-}
-
-Error StreamWriter::writeInteger(uint32_t Int) {
-  return writeObject(support::ulittle32_t(Int));
-}
-
-Error StreamWriter::writeInteger(uint64_t Int) {
-  return writeObject(support::ulittle64_t(Int));
-}
-
-Error StreamWriter::writeInteger(int8_t Int) { return writeObject(Int); }
-
-Error StreamWriter::writeInteger(int16_t Int) {
-  return writeObject(support::little16_t(Int));
-}
-
-Error StreamWriter::writeInteger(int32_t Int) {
-  return writeObject(support::little32_t(Int));
-}
-
-Error StreamWriter::writeInteger(int64_t Int) {
-  return writeObject(support::little64_t(Int));
-}
-
 Error StreamWriter::writeZeroString(StringRef Str) {
   if (auto EC = writeFixedString(Str))
     return EC;
index 5d15e62..f2f25c2 100644 (file)
@@ -236,7 +236,7 @@ Error DbiStream::initializeSectionContributionData() {
     return Error::success();
 
   StreamReader SCReader(SecContrSubstream);
-  if (auto EC = SCReader.readEnum(SectionContribVersion))
+  if (auto EC = SCReader.readEnum(SectionContribVersion, llvm::support::little))
     return EC;
 
   if (SectionContribVersion == DbiSecContribVer60)
index df6e9b0..0b56d05 100644 (file)
@@ -187,17 +187,21 @@ Error DbiStreamBuilder::generateFileInfoSubstream() {
 
   uint16_t ModiCount = std::min<uint32_t>(UINT16_MAX, ModuleInfos.size());
   uint16_t FileCount = std::min<uint32_t>(UINT16_MAX, SourceFileNames.size());
-  if (auto EC = MetadataWriter.writeInteger(ModiCount)) // NumModules
+  if (auto EC = MetadataWriter.writeInteger(
+          ModiCount, llvm::support::little)) // NumModules
     return EC;
-  if (auto EC = MetadataWriter.writeInteger(FileCount)) // NumSourceFiles
+  if (auto EC = MetadataWriter.writeInteger(
+          FileCount, llvm::support::little)) // NumSourceFiles
     return EC;
   for (uint16_t I = 0; I < ModiCount; ++I) {
-    if (auto EC = MetadataWriter.writeInteger(I)) // Mod Indices
+    if (auto EC = MetadataWriter.writeInteger(
+            I, llvm::support::little)) // Mod Indices
       return EC;
   }
   for (const auto MI : ModuleInfoList) {
     FileCount = static_cast<uint16_t>(MI->SourceFiles.size());
-    if (auto EC = MetadataWriter.writeInteger(FileCount)) // Mod File Counts
+    if (auto EC = MetadataWriter.writeInteger(
+            FileCount, llvm::support::little)) // Mod File Counts
       return EC;
   }
 
@@ -219,7 +223,8 @@ Error DbiStreamBuilder::generateFileInfoSubstream() {
       if (Result == SourceFileNames.end())
         return make_error<RawError>(raw_error_code::no_entry,
                                     "The source file was not found.");
-      if (auto EC = MetadataWriter.writeInteger(Result->second))
+      if (auto EC = MetadataWriter.writeInteger(Result->second,
+                                                llvm::support::little))
         return EC;
     }
   }
@@ -373,7 +378,7 @@ Error DbiStreamBuilder::commit(const msf::MSFLayout &Layout,
     return EC;
 
   if (!SectionContribs.empty()) {
-    if (auto EC = Writer.writeEnum(DbiSecContribVer60))
+    if (auto EC = Writer.writeEnum(DbiSecContribVer60, llvm::support::little))
       return EC;
     if (auto EC = Writer.writeArray(SectionContribs))
       return EC;
@@ -392,7 +397,8 @@ Error DbiStreamBuilder::commit(const msf::MSFLayout &Layout,
     return EC;
 
   for (auto &Stream : DbgStreams)
-    if (auto EC = Writer.writeInteger(Stream.StreamNumber))
+    if (auto EC =
+            Writer.writeInteger(Stream.StreamNumber, llvm::support::little))
       return EC;
 
   for (auto &Stream : DbgStreams) {
index b3fe6fa..dd95c07 100644 (file)
@@ -48,9 +48,9 @@ Error HashTable::load(msf::StreamReader &Stream) {
                                 "Present bit vector interesects deleted!");
 
   for (uint32_t P : Present) {
-    if (auto EC = Stream.readInteger(Buckets[P].first))
+    if (auto EC = Stream.readInteger(Buckets[P].first, llvm::support::little))
       return EC;
-    if (auto EC = Stream.readInteger(Buckets[P].second))
+    if (auto EC = Stream.readInteger(Buckets[P].second, llvm::support::little))
       return EC;
   }
 
@@ -91,9 +91,9 @@ Error HashTable::commit(msf::StreamWriter &Writer) const {
     return EC;
 
   for (const auto &Entry : *this) {
-    if (auto EC = Writer.writeInteger(Entry.first))
+    if (auto EC = Writer.writeInteger(Entry.first, llvm::support::little))
       return EC;
-    if (auto EC = Writer.writeInteger(Entry.second))
+    if (auto EC = Writer.writeInteger(Entry.second, llvm::support::little))
       return EC;
   }
   return Error::success();
@@ -212,7 +212,7 @@ void HashTable::grow() {
 Error HashTable::readSparseBitVector(msf::StreamReader &Stream,
                                      SparseBitVector<> &V) {
   uint32_t NumWords;
-  if (auto EC = Stream.readInteger(NumWords))
+  if (auto EC = Stream.readInteger(NumWords, llvm::support::little))
     return joinErrors(
         std::move(EC),
         make_error<RawError>(raw_error_code::corrupt_file,
@@ -220,7 +220,7 @@ Error HashTable::readSparseBitVector(msf::StreamReader &Stream,
 
   for (uint32_t I = 0; I != NumWords; ++I) {
     uint32_t Word;
-    if (auto EC = Stream.readInteger(Word))
+    if (auto EC = Stream.readInteger(Word, llvm::support::little))
       return joinErrors(std::move(EC),
                         make_error<RawError>(raw_error_code::corrupt_file,
                                              "Expected hash table word"));
@@ -235,7 +235,7 @@ Error HashTable::writeSparseBitVector(msf::StreamWriter &Writer,
                                       SparseBitVector<> &Vec) {
   int ReqBits = Vec.find_last() + 1;
   uint32_t NumWords = alignTo(ReqBits, sizeof(uint32_t)) / sizeof(uint32_t);
-  if (auto EC = Writer.writeInteger(NumWords))
+  if (auto EC = Writer.writeInteger(NumWords, llvm::support::little))
     return joinErrors(
         std::move(EC),
         make_error<RawError>(raw_error_code::corrupt_file,
@@ -248,7 +248,7 @@ Error HashTable::writeSparseBitVector(msf::StreamWriter &Writer,
       if (Vec.test(Idx))
         Word |= (1 << WordIdx);
     }
-    if (auto EC = Writer.writeInteger(Word))
+    if (auto EC = Writer.writeInteger(Word, llvm::support::little))
       return joinErrors(std::move(EC), make_error<RawError>(
                                            raw_error_code::corrupt_file,
                                            "Could not write linear map word"));
index 25370f2..ee8374f 100644 (file)
@@ -43,7 +43,7 @@ Error ModStream::reload() {
 
   ReadableStreamRef S;
 
-  if (auto EC = Reader.readInteger(Signature))
+  if (auto EC = Reader.readInteger(Signature, llvm::support::little))
     return EC;
   if (auto EC = Reader.readArray(SymbolsSubstream, SymbolSize - 4))
     return EC;
@@ -58,7 +58,7 @@ Error ModStream::reload() {
     return EC;
 
   uint32_t GlobalRefsSize;
-  if (auto EC = Reader.readInteger(GlobalRefsSize))
+  if (auto EC = Reader.readInteger(GlobalRefsSize, llvm::support::little))
     return EC;
   if (auto EC = Reader.readStreamRef(GlobalRefsSubstream, GlobalRefsSize))
     return EC;
index 47fd0e1..8a9579c 100644 (file)
@@ -32,7 +32,7 @@ Error NamedStreamMap::load(StreamReader &Stream) {
   FinalizedInfo.reset();
 
   uint32_t StringBufferSize;
-  if (auto EC = Stream.readInteger(StringBufferSize))
+  if (auto EC = Stream.readInteger(StringBufferSize, llvm::support::little))
     return joinErrors(std::move(EC),
                       make_error<RawError>(raw_error_code::corrupt_file,
                                            "Expected string buffer size"));
@@ -71,8 +71,8 @@ Error NamedStreamMap::commit(msf::StreamWriter &Writer) const {
   assert(FinalizedInfo.hasValue());
 
   // The first field is the number of bytes of string data.
-  if (auto EC = Writer.writeInteger(
-          FinalizedInfo->StringDataBytes)) // Number of bytes of string data
+  if (auto EC = Writer.writeInteger(FinalizedInfo->StringDataBytes,
+                                    llvm::support::little))
     return EC;
 
   // Now all of the string data itself.
index 3a3692d..26d9e0e 100644 (file)
@@ -186,7 +186,7 @@ Error PDBFile::parseStreamData() {
   // been parsed, we can avoid this and reuse MappedBlockStream.
   auto DS = MappedBlockStream::createDirectoryStream(ContainerLayout, *Buffer);
   StreamReader Reader(*DS);
-  if (auto EC = Reader.readInteger(NumStreams))
+  if (auto EC = Reader.readInteger(NumStreams, llvm::support::little))
     return EC;
 
   if (auto EC = Reader.readArray(ContainerLayout.StreamSizes, NumStreams))
index 57ca2ba..fb8e34e 100644 (file)
@@ -132,8 +132,8 @@ Error PDBFileBuilder::commit(StringRef Filename) {
   auto DirStream =
       WritableMappedBlockStream::createDirectoryStream(Layout, Buffer);
   StreamWriter DW(*DirStream);
-  if (auto EC =
-          DW.writeInteger(static_cast<uint32_t>(Layout.StreamSizes.size())))
+  if (auto EC = DW.writeInteger<uint32_t>(Layout.StreamSizes.size(),
+                                          llvm::support::little))
     return EC;
 
   if (auto EC = DW.writeArray(Layout.StreamSizes))
index 5b8ae9b..34f2b48 100644 (file)
@@ -55,7 +55,7 @@ Error StringTable::load(StreamReader &Stream) {
     return make_error<RawError>(raw_error_code::corrupt_file,
                                 "Missing name count");
 
-  if (auto EC = Stream.readInteger(NameCount))
+  if (auto EC = Stream.readInteger(NameCount, llvm::support::little))
     return EC;
   return Error::success();
 }
index ef9caee..304f3a6 100644 (file)
@@ -74,7 +74,7 @@ Error StringTableBuilder::commit(msf::StreamWriter &Writer) const {
 
   // Write a hash table.
   uint32_t BucketCount = computeBucketCount(Strings.size());
-  if (auto EC = Writer.writeInteger(BucketCount))
+  if (auto EC = Writer.writeInteger(BucketCount, llvm::support::little))
     return EC;
   std::vector<ulittle32_t> Buckets(BucketCount);
 
@@ -96,7 +96,8 @@ Error StringTableBuilder::commit(msf::StreamWriter &Writer) const {
 
   if (auto EC = Writer.writeArray(ArrayRef<ulittle32_t>(Buckets)))
     return EC;
-  if (auto EC = Writer.writeInteger(static_cast<uint32_t>(Strings.size())))
+  if (auto EC = Writer.writeInteger(static_cast<uint32_t>(Strings.size()),
+                                    llvm::support::little))
     return EC;
   return Error::success();
 }
index a0274c0..d089206 100644 (file)
@@ -1015,7 +1015,7 @@ void COFFDumper::printCodeViewInlineeLines(StringRef Subsection) {
   msf::ByteStream S(Subsection);
   msf::StreamReader SR(S);
   uint32_t Signature;
-  error(SR.readInteger(Signature));
+  error(SR.readInteger(Signature, llvm::support::little));
   bool HasExtraFiles = Signature == unsigned(InlineeLinesSignature::ExtraFiles);
 
   while (!SR.empty()) {
@@ -1028,12 +1028,12 @@ void COFFDumper::printCodeViewInlineeLines(StringRef Subsection) {
 
     if (HasExtraFiles) {
       uint32_t ExtraFileCount;
-      error(SR.readInteger(ExtraFileCount));
+      error(SR.readInteger(ExtraFileCount, llvm::support::little));
       W.printNumber("ExtraFileCount", ExtraFileCount);
       ListScope ExtraFiles(W, "ExtraFiles");
       for (unsigned I = 0; I < ExtraFileCount; ++I) {
         uint32_t FileID;
-        error(SR.readInteger(FileID));
+        error(SR.readInteger(FileID, llvm::support::little));
         printFileNameForOffset("FileID", FileID);
       }
     }
index 07591ca..4593f3c 100644 (file)
@@ -325,8 +325,8 @@ TEST(MappedBlockStreamTest, TestWriteThenRead) {
 
   StreamReader Reader(*S);
   StreamWriter Writer(*S);
-  EXPECT_NO_ERROR(Writer.writeInteger(u16[0]));
-  EXPECT_NO_ERROR(Reader.readInteger(u16[1]));
+  EXPECT_NO_ERROR(Writer.writeInteger(u16[0], llvm::support::little));
+  EXPECT_NO_ERROR(Reader.readInteger(u16[1], llvm::support::little));
   EXPECT_EQ(u16[0], u16[1]);
   EXPECT_EQ(std::vector<uint8_t>({0, 0x7A, 0xEC, 0, 0, 0, 0, 0, 0, 0}),
             DataBytes);
@@ -334,8 +334,8 @@ TEST(MappedBlockStreamTest, TestWriteThenRead) {
   Reader.setOffset(0);
   Writer.setOffset(0);
   ::memset(DataBytes.data(), 0, 10);
-  EXPECT_NO_ERROR(Writer.writeInteger(u32[0]));
-  EXPECT_NO_ERROR(Reader.readInteger(u32[1]));
+  EXPECT_NO_ERROR(Writer.writeInteger(u32[0], llvm::support::little));
+  EXPECT_NO_ERROR(Reader.readInteger(u32[1], llvm::support::little));
   EXPECT_EQ(u32[0], u32[1]);
   EXPECT_EQ(std::vector<uint8_t>({0x17, 0x5C, 0x50, 0, 0, 0, 0x35, 0, 0, 0}),
             DataBytes);
@@ -343,8 +343,8 @@ TEST(MappedBlockStreamTest, TestWriteThenRead) {
   Reader.setOffset(0);
   Writer.setOffset(0);
   ::memset(DataBytes.data(), 0, 10);
-  EXPECT_NO_ERROR(Writer.writeEnum(Enum[0]));
-  EXPECT_NO_ERROR(Reader.readEnum(Enum[1]));
+  EXPECT_NO_ERROR(Writer.writeEnum(Enum[0], llvm::support::little));
+  EXPECT_NO_ERROR(Reader.readEnum(Enum[1], llvm::support::little));
   EXPECT_EQ(Enum[0], Enum[1]);
   EXPECT_EQ(std::vector<uint8_t>({0x2C, 0x60, 0x4A, 0, 0, 0, 0, 0, 0, 0}),
             DataBytes);