LLVM  9.0.0svn
DebugChecksumsSubsection.cpp
Go to the documentation of this file.
1 //===- DebugChecksumsSubsection.cpp ---------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
10 #include "llvm/ADT/ArrayRef.h"
15 #include "llvm/Support/Endian.h"
16 #include "llvm/Support/Error.h"
18 #include <cassert>
19 #include <cstdint>
20 #include <cstring>
21 
22 using namespace llvm;
23 using namespace llvm::codeview;
24 
27 
28  ulittle32_t FileNameOffset; // Byte offset of filename in global string table.
29  uint8_t ChecksumSize; // Number of bytes of checksum.
30  uint8_t ChecksumKind; // FileChecksumKind
31  // Checksum bytes follow.
32 };
33 
36  BinaryStreamReader Reader(Stream);
37 
38  const FileChecksumEntryHeader *Header;
39  if (auto EC = Reader.readObject(Header))
40  return EC;
41 
42  Item.FileNameOffset = Header->FileNameOffset;
43  Item.Kind = static_cast<FileChecksumKind>(Header->ChecksumKind);
44  if (auto EC = Reader.readBytes(Item.Checksum, Header->ChecksumSize))
45  return EC;
46 
47  Len = alignTo(Header->ChecksumSize + sizeof(FileChecksumEntryHeader), 4);
48  return Error::success();
49 }
50 
52  if (auto EC = Reader.readArray(Checksums, Reader.bytesRemaining()))
53  return EC;
54 
55  return Error::success();
56 }
57 
59  BinaryStreamReader Reader(Section);
60  return initialize(Reader);
61 }
62 
65  : DebugSubsection(DebugSubsectionKind::FileChecksums), Strings(Strings) {}
66 
69  ArrayRef<uint8_t> Bytes) {
70  FileChecksumEntry Entry;
71  if (!Bytes.empty()) {
72  uint8_t *Copy = Storage.Allocate<uint8_t>(Bytes.size());
73  ::memcpy(Copy, Bytes.data(), Bytes.size());
74  Entry.Checksum = makeArrayRef(Copy, Bytes.size());
75  }
76 
77  Entry.FileNameOffset = Strings.insert(FileName);
78  Entry.Kind = Kind;
79  Checksums.push_back(Entry);
80 
81  // This maps the offset of this string in the string table to the offset
82  // of this checksum entry in the checksum buffer.
83  OffsetMap[Entry.FileNameOffset] = SerializedSize;
84  assert(SerializedSize % 4 == 0);
85 
86  uint32_t Len = alignTo(sizeof(FileChecksumEntryHeader) + Bytes.size(), 4);
87  SerializedSize += Len;
88 }
89 
91  return SerializedSize;
92 }
93 
95  for (const auto &FC : Checksums) {
97  Header.ChecksumKind = uint8_t(FC.Kind);
98  Header.ChecksumSize = FC.Checksum.size();
99  Header.FileNameOffset = FC.FileNameOffset;
100  if (auto EC = Writer.writeObject(Header))
101  return EC;
102  if (auto EC = Writer.writeArray(makeArrayRef(FC.Checksum)))
103  return EC;
104  if (auto EC = Writer.padToAlignment(4))
105  return EC;
106  }
107  return Error::success();
108 }
109 
111  uint32_t Offset = Strings.getIdForString(FileName);
112  auto Iter = OffsetMap.find(Offset);
113  assert(Iter != OffsetMap.end());
114  return Iter->second;
115 }
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:23
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:684
ArrayRef< T > makeArrayRef(const T &OneElt)
Construct an ArrayRef from a single element.
Definition: ArrayRef.h:450
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:270
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:148
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:214
Provides write only access to a subclass of WritableBinaryStream.
const T * data() const
Definition: ArrayRef.h:145
static ErrorSuccess success()
Create a success value.
Definition: Error.h:326
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:108
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:157
Provides read only access to a subclass of BinaryStream.
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:48
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:143