23 using namespace llvm::codeview;
24 using namespace llvm::msf;
25 using namespace llvm::pdb;
28 : Msf(Msf),
Allocator(Msf.getAllocator()), Age(1), BuildNumber(0),
53 SymRecordStreamIndex = Index;
57 PublicsStreamIndex = Index;
64 "The specified stream type already exists");
67 return ExpectedIndex.takeError();
68 uint32_t Index = std::move(*ExpectedIndex);
69 DbgStreams[(int)Type].Data = Data;
70 DbgStreams[(int)Type].StreamNumber = Index;
75 return ECNamesBuilder.
insert(Name);
81 calculateModiSubstreamSize() + calculateSectionContribsStreamSize() +
82 calculateSectionMapStreamSize() + calculateDbgStreamsSize() +
90 llvm::make_unique<DbiModuleDescriptorBuilder>(ModuleName, Index, Msf);
92 auto Result = ModiMap.insert(std::make_pair(ModuleName, std::move(MIB)));
96 "The specified module already exists");
97 ModiList.push_back(M);
102 auto ModIter = ModiMap.find(Module);
103 if (ModIter == ModiMap.end())
105 "The specified module was not found");
112 SourceFileNames.
insert(std::make_pair(File, Index));
113 Module.addSourceFile(File);
118 auto NameIter = SourceFileNames.
find(File);
119 if (NameIter == SourceFileNames.
end())
121 "The specified source file was not found");
122 return NameIter->getValue();
125 uint32_t DbiStreamBuilder::calculateModiSubstreamSize()
const {
127 for (
const auto &M : ModiList)
128 Size += M->calculateSerializedLength();
132 uint32_t DbiStreamBuilder::calculateSectionContribsStreamSize()
const {
133 if (SectionContribs.empty())
136 sizeof(SectionContribs[0]) * SectionContribs.size();
139 uint32_t DbiStreamBuilder::calculateSectionMapStreamSize()
const {
140 if (SectionMap.empty())
145 uint32_t DbiStreamBuilder::calculateNamesOffset()
const {
152 for (
const auto &M : ModiList)
153 NumFileInfos += M->source_files().size();
158 uint32_t DbiStreamBuilder::calculateFileInfoSubstreamSize()
const {
159 uint32_t Size = calculateNamesOffset();
160 Size += calculateNamesBufferSize();
164 uint32_t DbiStreamBuilder::calculateNamesBufferSize()
const {
166 for (
const auto &
F : SourceFileNames) {
167 Size +=
F.getKeyLength() + 1;
172 uint32_t DbiStreamBuilder::calculateDbgStreamsSize()
const {
173 return DbgStreams.
size() *
sizeof(uint16_t);
176 Error DbiStreamBuilder::generateFileInfoSubstream() {
177 uint32_t Size = calculateFileInfoSubstreamSize();
179 uint32_t NamesOffset = calculateNamesOffset();
188 uint16_t ModiCount = std::min<uint32_t>(UINT16_MAX, ModiList.size());
189 uint16_t FileCount = std::min<uint32_t>(UINT16_MAX, SourceFileNames.size());
190 if (
auto EC = MetadataWriter.writeInteger(ModiCount))
192 if (
auto EC = MetadataWriter.writeInteger(FileCount))
194 for (uint16_t
I = 0;
I < ModiCount; ++
I) {
195 if (
auto EC = MetadataWriter.writeInteger(
I))
198 for (
const auto &
MI : ModiList) {
199 FileCount =
static_cast<uint16_t
>(
MI->source_files().size());
200 if (
auto EC = MetadataWriter.writeInteger(FileCount))
210 for (
auto &
Name : SourceFileNames) {
211 Name.second = NameBufferWriter.getOffset();
212 if (
auto EC = NameBufferWriter.writeCString(
Name.getKey()))
216 for (
const auto &
MI : ModiList) {
218 auto Result = SourceFileNames.find(
Name);
219 if (Result == SourceFileNames.end())
221 "The source file was not found.");
222 if (
auto EC = MetadataWriter.writeInteger(Result->second))
227 if (
auto EC = NameBufferWriter.padToAlignment(
sizeof(
uint32_t)))
230 if (NameBufferWriter.bytesRemaining() > 0)
232 "The names buffer contained unexpected data.");
234 if (MetadataWriter.bytesRemaining() >
sizeof(
uint32_t))
235 return make_error<RawError>(
237 "The metadata buffer contained unexpected data.");
242 Error DbiStreamBuilder::finalize() {
246 for (
auto &
MI : ModiList)
249 if (
auto EC = generateFileInfoSubstream())
261 H->
MachineType =
static_cast<uint16_t
>(MachineType);
280 for (
auto &
MI : ModiList) {
281 if (
auto EC =
MI->finalizeMsfLayout())
299 if (Flags & COFF::IMAGE_SCN_MEM_EXECUTE)
313 memset(&SC, 0,
sizeof(SC));
314 SC.
ISect = (uint16_t)~0U;
320 SectionContribs.emplace_back(SC);
330 std::vector<SecMapEntry>
Ret;
335 auto &Entry = Ret.back();
336 memset(&Entry, 0,
sizeof(Entry));
338 Entry.Frame = Idx + 1;
341 Entry.SecName = UINT16_MAX;
342 Entry.ClassName = UINT16_MAX;
347 for (
auto &Hdr : SecHdrs) {
350 Entry.SecByteLength = Hdr.VirtualSize;
358 Entry.SecByteLength = UINT32_MAX;
365 if (
auto EC = finalize())
368 auto DbiS = WritableMappedBlockStream::createIndexedStream(
369 Layout, MsfBuffer,
StreamDBI, Allocator);
375 for (
auto &M : ModiList) {
376 if (
auto EC = M->commit(Writer, Layout, MsfBuffer))
380 if (!SectionContribs.empty()) {
387 if (!SectionMap.empty()) {
399 if (
auto EC = ECNamesBuilder.
commit(Writer))
402 for (
auto &Stream : DbgStreams)
406 for (
auto &Stream : DbgStreams) {
409 auto WritableStream = WritableMappedBlockStream::createIndexedStream(
410 Layout, MsfBuffer, Stream.StreamNumber, Allocator);
418 "Unexpected bytes found in DBI Stream");
Error writeObject(const T &Obj)
Writes the object Obj to the underlying stream, as if by using memcpy.
uint32_t addECName(StringRef Name)
Error finalizeMsfLayout()
An implementation of BinaryStream which holds its entire data set in a single contiguous buffer...
uint32_t calculateSerializedLength() const
A Module instance is used to store all the information related to an LLVM module. ...
void setSectionMap(ArrayRef< SecMapEntry > SecMap)
RefType drop_front(uint32_t N) const
Return a new BinaryStreamRef with the first N elements removed.
unsigned getModuleIndex() const
void setMachineType(PDB_Machine M)
static std::vector< SecMapEntry > createSectionMap(ArrayRef< llvm::object::coff_section > SecHdrs)
iterator find(StringRef Key)
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...
detail::packed_endian_specific_integral< uint16_t, little, unaligned > ulittle16_t
Error commit(BinaryStreamWriter &Writer) const
ArrayRef< T > makeArrayRef(const T &OneElt)
Construct an ArrayRef from a single element.
void addSectionContrib(DbiModuleDescriptorBuilder *ModuleDbi, const llvm::object::coff_section *SecHdr)
Tagged union holding either a T or a Error.
void setSymbolRecordStreamIndex(uint32_t Index)
Error addDbgStream(pdb::DbgHeaderType Type, ArrayRef< uint8_t > Data)
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
Error writeArray(ArrayRef< T > Array)
Writes an array of objects of type T to the underlying stream, as if by using memcpy.
size_t size() const
size - Get the array size.
const uint16_t kInvalidStreamIndex
support::ulittle32_t Characteristics
support::ulittle16_t Imod
The instances of the Type class are immutable: once they are created, they are never changed...
detail::packed_endian_specific_integral< uint32_t, little, unaligned > ulittle32_t
void setBuildNumber(uint16_t B)
uint32_t insert(StringRef S)
Expected< uint32_t > getSourceFileNameIndex(StringRef FileName)
LLVM_ATTRIBUTE_RETURNS_NONNULL LLVM_ATTRIBUTE_RETURNS_NOALIAS void * Allocate(size_t Size, size_t Alignment)
Allocate space at the specified alignment.
Error writeStreamRef(BinaryStreamRef Ref)
Efficiently reads all data from Ref, and writes it to this stream.
Provides write only access to a subclass of WritableBinaryStream.
void setFlags(uint16_t F)
void setVersionHeader(PdbRaw_DbiVer V)
void setPdbDllRbld(uint16_t R)
Error writeInteger(T Value)
Write the the integer Value to the underlying stream in the specified endianness. ...
support::ulittle32_t SizeOfRawData
Error commit(const msf::MSFLayout &Layout, WritableBinaryStreamRef MsfBuffer)
void setPdbDllVersion(uint16_t V)
Error setStreamSize(uint32_t Idx, uint32_t Size)
Update the size of an existing stream.
support::ulittle16_t ISect
static ErrorSuccess success()
Create a success value.
support::ulittle32_t PointerToRawData
support::ulittle32_t Characteristics
CHAIN = SC CHAIN, Imm128 - System call.
Expected< uint32_t > addStream(uint32_t Size, ArrayRef< uint32_t > Blocks)
Add a stream to the MSF file with the given size, occupying the given list of blocks.
bool insert(MapEntryTy *KeyValue)
insert - Insert the specified key/value pair into the map.
Expected< DbiModuleDescriptorBuilder & > addModuleInfo(StringRef ModuleName)
Error addModuleSourceFile(StringRef Module, StringRef File)
constexpr char Size[]
Key for Kernel::Arg::Metadata::mSize.
Error writeEnum(T Num)
Similar to writeInteger.
static std::vector< std::string > Flags
void setPublicsStreamIndex(uint32_t Index)
static uint16_t toSecMapFlags(uint32_t Flags)
uint32_t bytesRemaining() const
LLVM_ATTRIBUTE_ALWAYS_INLINE size_type size() const
Lightweight error class with error context and mandatory checking.
StringRef - Represent a constant reference to a string, i.e.
RefType keep_front(uint32_t N) const
Return a new BinaryStreamRef with only the first N elements remaining.
uint32_t getLength() override
Return the number of bytes of data in this stream.
uint32_t calculateSerializedSize() const