LLVM  8.0.1
DebugChecksumsSubsection.cpp
Go to the documentation of this file.
1 //===- DebugChecksumsSubsection.cpp ---------------------------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
11 #include "llvm/ADT/ArrayRef.h"
16 #include "llvm/Support/Endian.h"
17 #include "llvm/Support/Error.h"
19 #include <cassert>
20 #include <cstdint>
21 #include <cstring>
22 
23 using namespace llvm;
24 using namespace llvm::codeview;
25 
28 
29  ulittle32_t FileNameOffset; // Byte offset of filename in global string table.
30  uint8_t ChecksumSize; // Number of bytes of checksum.
31  uint8_t ChecksumKind; // FileChecksumKind
32  // Checksum bytes follow.
33 };
34 
37  BinaryStreamReader Reader(Stream);
38 
39  const FileChecksumEntryHeader *Header;
40  if (auto EC = Reader.readObject(Header))
41  return EC;
42 
43  Item.FileNameOffset = Header->FileNameOffset;
44  Item.Kind = static_cast<FileChecksumKind>(Header->ChecksumKind);
45  if (auto EC = Reader.readBytes(Item.Checksum, Header->ChecksumSize))
46  return EC;
47 
48  Len = alignTo(Header->ChecksumSize + sizeof(FileChecksumEntryHeader), 4);
49  return Error::success();
50 }
51 
53  if (auto EC = Reader.readArray(Checksums, Reader.bytesRemaining()))
54  return EC;
55 
56  return Error::success();
57 }
58 
60  BinaryStreamReader Reader(Section);
61  return initialize(Reader);
62 }
63 
66  : DebugSubsection(DebugSubsectionKind::FileChecksums), Strings(Strings) {}
67 
70  ArrayRef<uint8_t> Bytes) {
71  FileChecksumEntry Entry;
72  if (!Bytes.empty()) {
73  uint8_t *Copy = Storage.Allocate<uint8_t>(Bytes.size());
74  ::memcpy(Copy, Bytes.data(), Bytes.size());
75  Entry.Checksum = makeArrayRef(Copy, Bytes.size());
76  }
77 
78  Entry.FileNameOffset = Strings.insert(FileName);
79  Entry.Kind = Kind;
80  Checksums.push_back(Entry);
81 
82  // This maps the offset of this string in the string table to the offset
83  // of this checksum entry in the checksum buffer.
84  OffsetMap[Entry.FileNameOffset] = SerializedSize;
85  assert(SerializedSize % 4 == 0);
86 
87  uint32_t Len = alignTo(sizeof(FileChecksumEntryHeader) + Bytes.size(), 4);
88  SerializedSize += Len;
89 }
90 
92  return SerializedSize;
93 }
94 
96  for (const auto &FC : Checksums) {
98  Header.ChecksumKind = uint8_t(FC.Kind);
99  Header.ChecksumSize = FC.Checksum.size();
100  Header.FileNameOffset = FC.FileNameOffset;
101  if (auto EC = Writer.writeObject(Header))
102  return EC;
103  if (auto EC = Writer.writeArray(makeArrayRef(FC.Checksum)))
104  return EC;
105  if (auto EC = Writer.padToAlignment(4))
106  return EC;
107  }
108  return Error::success();
109 }
110 
112  uint32_t Offset = Strings.getIdForString(FileName);
113  auto Iter = OffsetMap.find(Offset);
114  assert(Iter != OffsetMap.end());
115  return Iter->second;
116 }
Error writeObject(const T &Obj)
Writes the object Obj to the underlying stream, as if by using memcpy.
DebugChecksumsSubsection(DebugStringTableSubsection &Strings)
This class represents lattice values for constants.
Definition: AllocatorList.h:24
void addChecksum(StringRef FileName, FileChecksumKind Kind, ArrayRef< uint8_t > Bytes)
Error readObject(const T *&Dest)
Get a pointer to an object of type T from the underlying stream, as if by memcpy, and store the resul...
uint64_t alignTo(uint64_t Value, uint64_t Align, uint64_t Skew=0)
Returns the next integer (mod 2**64) that is greater than or equal to Value and is a multiple of Alig...
Definition: MathExtras.h:685
ArrayRef< T > makeArrayRef(const T &OneElt)
Construct an ArrayRef from a single element.
Definition: ArrayRef.h:451
Error commit(BinaryStreamWriter &Writer) const override
Error writeArray(ArrayRef< T > Array)
Writes an array of objects of type T to the underlying stream, as if by using memcpy.
iterator find(const_arg_type_t< KeyT > Val)
Definition: DenseMap.h:176
uint32_t mapChecksumOffset(StringRef FileName) const
detail::packed_endian_specific_integral< uint32_t, little, unaligned > ulittle32_t
Definition: Endian.h:271
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:149
LLVM_ATTRIBUTE_RETURNS_NONNULL LLVM_ATTRIBUTE_RETURNS_NOALIAS void * Allocate(size_t Size, size_t Alignment)
Allocate space at the specified alignment.
Definition: Allocator.h:215
Provides write only access to a subclass of WritableBinaryStream.
const T * data() const
Definition: ArrayRef.h:146
static ErrorSuccess success()
Create a success value.
Definition: Error.h:327
Represents a read-write view of a CodeView string table.
BinaryStreamRef is to BinaryStream what ArrayRef is to an Array.
static void initialize(TargetLibraryInfoImpl &TLI, const Triple &T, ArrayRef< StringRef > StandardNames)
Initialize the set of available library functions based on the specified target triple.
Error readBytes(ArrayRef< uint8_t > &Buffer, uint32_t Size)
Read Size bytes from the underlying stream at the current offset and and set Buffer to the resulting ...
Error padToAlignment(uint32_t Align)
iterator end()
Definition: DenseMap.h:109
uint32_t bytesRemaining() const
Error operator()(BinaryStreamRef Stream, uint32_t &Len, T &Item) const =delete
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
Lightweight error class with error context and mandatory checking.
Definition: Error.h:158
Provides read only access to a subclass of BinaryStream.
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
Error readArray(ArrayRef< T > &Array, uint32_t NumElements)
Get a reference to a NumElements element array of objects of type T from the underlying stream as if ...
bool empty() const
empty - Check if the array is empty.
Definition: ArrayRef.h:144