Movatterモバイル変換


[0]ホーム

URL:


LLVM 20.0.0git
DbiModuleDescriptorBuilder.cpp
Go to the documentation of this file.
1//===- DbiModuleDescriptorBuilder.cpp - PDB Mod Info Creation ---*- C++ -*-===//
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
9#include "llvm/DebugInfo/PDB/Native/DbiModuleDescriptorBuilder.h"
10
11#include "llvm/ADT/ArrayRef.h"
12#include "llvm/BinaryFormat/COFF.h"
13#include "llvm/DebugInfo/CodeView/CodeView.h"
14#include "llvm/DebugInfo/CodeView/DebugSubsectionRecord.h"
15#include "llvm/DebugInfo/MSF/MSFBuilder.h"
16#include "llvm/DebugInfo/MSF/MappedBlockStream.h"
17#include "llvm/DebugInfo/PDB/Native/RawConstants.h"
18#include "llvm/DebugInfo/PDB/Native/RawError.h"
19#include "llvm/Support/BinaryStreamWriter.h"
20
21using namespacellvm;
22using namespacellvm::codeview;
23using namespacellvm::msf;
24using namespacellvm::pdb;
25
26namespacellvm {
27namespacecodeview {
28classDebugSubsection;
29}
30}// namespace llvm
31
32staticuint32_tcalculateDiSymbolStreamSize(uint32_t SymbolByteSize,
33uint32_t C13Size) {
34uint32_tSize =sizeof(uint32_t);// Signature
35Size +=alignTo(SymbolByteSize, 4);// Symbol Data
36Size += 0;// TODO: Layout.C11Bytes
37Size += C13Size;// C13 Debug Info Size
38Size +=sizeof(uint32_t);// GlobalRefs substream size (always 0)
39Size += 0;// GlobalRefs substream bytes
40returnSize;
41}
42
43DbiModuleDescriptorBuilder::DbiModuleDescriptorBuilder(StringRefModuleName,
44uint32_t ModIndex,
45msf::MSFBuilder &Msf)
46 : MSF(Msf),ModuleName(std::string(ModuleName)) {
47 ::memset(&Layout, 0,sizeof(Layout));
48 Layout.Mod = ModIndex;
49}
50
51DbiModuleDescriptorBuilder::~DbiModuleDescriptorBuilder() =default;
52
53uint16_tDbiModuleDescriptorBuilder::getStreamIndex() const{
54return Layout.ModDiStream;
55}
56
57voidDbiModuleDescriptorBuilder::setObjFileName(StringRefName) {
58 ObjFileName = std::string(Name);
59}
60
61voidDbiModuleDescriptorBuilder::setPdbFilePathNI(uint32_t NI) {
62 PdbFilePathNI = NI;
63}
64
65voidDbiModuleDescriptorBuilder::setFirstSectionContrib(
66constSectionContrib &SC) {
67 Layout.SC = SC;
68}
69
70voidDbiModuleDescriptorBuilder::addSymbol(CVSymbol Symbol) {
71// Defer to the bulk API. It does the same thing.
72addSymbolsInBulk(Symbol.data());
73}
74
75voidDbiModuleDescriptorBuilder::addSymbolsInBulk(
76ArrayRef<uint8_t> BulkSymbols) {
77// Do nothing for empty runs of symbols.
78if (BulkSymbols.empty())
79return;
80
81 Symbols.push_back(SymbolListWrapper(BulkSymbols));
82// Symbols written to a PDB file are required to be 4 byte aligned. The same
83// is not true of object files.
84assert(BulkSymbols.size() %alignOf(CodeViewContainer::Pdb) == 0 &&
85"Invalid Symbol alignment!");
86 SymbolByteSize += BulkSymbols.size();
87}
88
89voidDbiModuleDescriptorBuilder::addUnmergedSymbols(void *SymSrc,
90uint32_t SymLength) {
91assert(SymLength > 0);
92 Symbols.push_back(SymbolListWrapper(SymSrc, SymLength));
93
94// Symbols written to a PDB file are required to be 4 byte aligned. The same
95// is not true of object files.
96assert(SymLength %alignOf(CodeViewContainer::Pdb) == 0 &&
97"Invalid Symbol alignment!");
98 SymbolByteSize += SymLength;
99}
100
101void DbiModuleDescriptorBuilder::addSourceFile(StringRefPath) {
102SourceFiles.push_back(std::string(Path));
103}
104
105uint32_t DbiModuleDescriptorBuilder::calculateC13DebugInfoSize() const{
106uint32_t Result = 0;
107for (constauto &Builder : C13Builders) {
108 Result += Builder.calculateSerializedLength();
109 }
110return Result;
111}
112
113uint32_tDbiModuleDescriptorBuilder::calculateSerializedLength() const{
114uint32_t L =sizeof(Layout);
115uint32_t M =ModuleName.size() + 1;
116uint32_t O = ObjFileName.size() + 1;
117returnalignTo(L + M + O,sizeof(uint32_t));
118}
119
120voidDbiModuleDescriptorBuilder::finalize() {
121 Layout.FileNameOffs = 0;// TODO: Fix this
122 Layout.Flags = 0;// TODO: Fix this
123 Layout.C11Bytes = 0;
124 Layout.C13Bytes = calculateC13DebugInfoSize();
125 (void)Layout.Mod;// Set in constructor
126 (void)Layout.ModDiStream;// Set in finalizeMsfLayout
127 Layout.NumFiles =SourceFiles.size();
128 Layout.PdbFilePathNI = PdbFilePathNI;
129 Layout.SrcFileNameNI = 0;
130
131// This value includes both the signature field as well as the record bytes
132// from the symbol stream.
133 Layout.SymBytes =
134 Layout.ModDiStream ==kInvalidStreamIndex ? 0 :getNextSymbolOffset();
135}
136
137ErrorDbiModuleDescriptorBuilder::finalizeMsfLayout() {
138 this->Layout.ModDiStream =kInvalidStreamIndex;
139uint32_t C13Size = calculateC13DebugInfoSize();
140if (!C13Size && !SymbolByteSize)
141returnError::success();
142auto ExpectedSN =
143 MSF.addStream(calculateDiSymbolStreamSize(SymbolByteSize, C13Size));
144if (!ExpectedSN)
145return ExpectedSN.takeError();
146 Layout.ModDiStream = *ExpectedSN;
147returnError::success();
148}
149
150ErrorDbiModuleDescriptorBuilder::commit(BinaryStreamWriter &ModiWriter) {
151// We write the Modi record to the `ModiWriter`, but we additionally write its
152// symbol stream to a brand new stream.
153if (autoEC = ModiWriter.writeObject(Layout))
154returnEC;
155if (autoEC = ModiWriter.writeCString(ModuleName))
156returnEC;
157if (autoEC = ModiWriter.writeCString(ObjFileName))
158returnEC;
159if (autoEC = ModiWriter.padToAlignment(sizeof(uint32_t)))
160returnEC;
161returnError::success();
162}
163
164ErrorDbiModuleDescriptorBuilder::commitSymbolStream(
165constmsf::MSFLayout &MsfLayout,WritableBinaryStreamRef MsfBuffer) {
166if (Layout.ModDiStream ==kInvalidStreamIndex)
167returnError::success();
168
169auto NS =WritableMappedBlockStream::createIndexedStream(
170 MsfLayout, MsfBuffer, Layout.ModDiStream, MSF.getAllocator());
171WritableBinaryStreamRefRef(*NS);
172BinaryStreamWriter SymbolWriter(Ref);
173// Write the symbols.
174if (autoEC = SymbolWriter.writeInteger<uint32_t>(COFF::DEBUG_SECTION_MAGIC))
175returnEC;
176for (constSymbolListWrapper &Sym : Symbols) {
177if (Sym.NeedsToBeMerged) {
178assert(MergeSymsCallback);
179if (autoEC = MergeSymsCallback(MergeSymsCtx,Sym.SymPtr, SymbolWriter))
180returnEC;
181 }else {
182if (autoEC = SymbolWriter.writeBytes(Sym.asArray()))
183returnEC;
184 }
185 }
186
187// Apply the string table fixups.
188auto SavedOffset = SymbolWriter.getOffset();
189for (constStringTableFixup &Fixup : StringTableFixups) {
190 SymbolWriter.setOffset(Fixup.SymOffsetOfReference);
191if (autoE = SymbolWriter.writeInteger<uint32_t>(Fixup.StrTabOffset))
192returnE;
193 }
194 SymbolWriter.setOffset(SavedOffset);
195
196assert(SymbolWriter.getOffset() %alignOf(CodeViewContainer::Pdb) == 0 &&
197"Invalid debug section alignment!");
198// TODO: Write C11 Line data
199for (constauto &Builder : C13Builders) {
200if (autoEC = Builder.commit(SymbolWriter, CodeViewContainer::Pdb))
201returnEC;
202 }
203
204// TODO: Figure out what GlobalRefs substream actually is and populate it.
205if (autoEC = SymbolWriter.writeInteger<uint32_t>(0))
206returnEC;
207if (SymbolWriter.bytesRemaining() > 0)
208return make_error<RawError>(raw_error_code::stream_too_long);
209
210returnError::success();
211}
212
213voidDbiModuleDescriptorBuilder::addDebugSubsection(
214 std::shared_ptr<DebugSubsection> Subsection) {
215assert(Subsection);
216 C13Builders.push_back(DebugSubsectionRecordBuilder(std::move(Subsection)));
217}
218
219voidDbiModuleDescriptorBuilder::addDebugSubsection(
220constDebugSubsectionRecord &SubsectionContents) {
221 C13Builders.push_back(DebugSubsectionRecordBuilder(SubsectionContents));
222}
ArrayRef.h
COFF.h
BinaryStreamWriter.h
E
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
CodeView.h
calculateDiSymbolStreamSize
static uint32_t calculateDiSymbolStreamSize(uint32_t SymbolByteSize, uint32_t C13Size)
Definition:DbiModuleDescriptorBuilder.cpp:32
DbiModuleDescriptorBuilder.h
DebugSubsectionRecord.h
Name
std::string Name
Definition:ELFObjHandler.cpp:77
Size
uint64_t Size
Definition:ELFObjHandler.cpp:81
Sym
Symbol * Sym
Definition:ELF_riscv.cpp:479
SpecialSubKind::string
@ string
MSFBuilder.h
MappedBlockStream.h
RawConstants.h
RawError.h
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
llvm::ArrayRef
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition:ArrayRef.h:41
llvm::ArrayRef::size
size_t size() const
size - Get the array size.
Definition:ArrayRef.h:168
llvm::ArrayRef::empty
bool empty() const
empty - Check if the array is empty.
Definition:ArrayRef.h:163
llvm::BinaryStreamWriter
Provides write only access to a subclass of WritableBinaryStream.
Definition:BinaryStreamWriter.h:30
llvm::BinaryStreamWriter::writeCString
Error writeCString(StringRef Str)
Write the string Str to the underlying stream followed by a null terminator.
Definition:BinaryStreamWriter.cpp:47
llvm::BinaryStreamWriter::getOffset
uint64_t getOffset() const
Definition:BinaryStreamWriter.h:177
llvm::BinaryStreamWriter::writeInteger
Error writeInteger(T Value)
Write the integer Value to the underlying stream in the specified endianness.
Definition:BinaryStreamWriter.h:58
llvm::BinaryStreamWriter::bytesRemaining
uint64_t bytesRemaining() const
Definition:BinaryStreamWriter.h:179
llvm::BinaryStreamWriter::writeBytes
Error writeBytes(ArrayRef< uint8_t > Buffer)
Write the bytes specified in Buffer to the underlying stream.
Definition:BinaryStreamWriter.cpp:28
llvm::BinaryStreamWriter::setOffset
void setOffset(uint64_t Off)
Definition:BinaryStreamWriter.h:176
llvm::BinaryStreamWriter::writeObject
Error writeObject(const T &Obj)
Writes the object Obj to the underlying stream, as if by using memcpy.
Definition:BinaryStreamWriter.h:128
llvm::BinaryStreamWriter::padToAlignment
Error padToAlignment(uint32_t Align)
Definition:BinaryStreamWriter.cpp:95
llvm::Error
Lightweight error class with error context and mandatory checking.
Definition:Error.h:160
llvm::Error::success
static ErrorSuccess success()
Create a success value.
Definition:Error.h:337
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition:StringRef.h:51
llvm::WritableBinaryStreamRef
Definition:BinaryStreamRef.h:219
llvm::codeview::CVRecord
CVRecord is a fat pointer (base + size pair) to a symbol or type record.
Definition:CVRecord.h:29
llvm::codeview::DebugSubsectionRecordBuilder
Definition:DebugSubsectionRecord.h:51
llvm::codeview::DebugSubsectionRecord
Definition:DebugSubsectionRecord.h:35
llvm::codeview::DebugSubsection
Definition:DebugSubsection.h:34
llvm::msf::MSFBuilder
Definition:MSFBuilder.h:27
llvm::msf::MSFBuilder::getAllocator
BumpPtrAllocator & getAllocator()
Definition:MSFBuilder.h:119
llvm::msf::MSFBuilder::addStream
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.
Definition:MSFBuilder.cpp:156
llvm::msf::WritableMappedBlockStream::createIndexedStream
static std::unique_ptr< WritableMappedBlockStream > createIndexedStream(const MSFLayout &Layout, WritableBinaryStreamRef MsfData, uint32_t StreamIndex, BumpPtrAllocator &Allocator)
Definition:MappedBlockStream.cpp:324
llvm::pdb::DbiModuleDescriptorBuilder::addUnmergedSymbols
void addUnmergedSymbols(void *SymSrc, uint32_t SymLength)
Definition:DbiModuleDescriptorBuilder.cpp:89
llvm::pdb::DbiModuleDescriptorBuilder::setObjFileName
void setObjFileName(StringRef Name)
Definition:DbiModuleDescriptorBuilder.cpp:57
llvm::pdb::DbiModuleDescriptorBuilder::addDebugSubsection
void addDebugSubsection(std::shared_ptr< codeview::DebugSubsection > Subsection)
llvm::pdb::DbiModuleDescriptorBuilder::~DbiModuleDescriptorBuilder
~DbiModuleDescriptorBuilder()
llvm::pdb::DbiModuleDescriptorBuilder::setFirstSectionContrib
void setFirstSectionContrib(const SectionContrib &SC)
Definition:DbiModuleDescriptorBuilder.cpp:65
llvm::pdb::DbiModuleDescriptorBuilder::finalizeMsfLayout
Error finalizeMsfLayout()
Definition:DbiModuleDescriptorBuilder.cpp:137
llvm::pdb::DbiModuleDescriptorBuilder::finalize
void finalize()
Definition:DbiModuleDescriptorBuilder.cpp:120
llvm::pdb::DbiModuleDescriptorBuilder::getStreamIndex
uint16_t getStreamIndex() const
Definition:DbiModuleDescriptorBuilder.cpp:53
llvm::pdb::DbiModuleDescriptorBuilder::addSymbol
void addSymbol(codeview::CVSymbol Symbol)
Definition:DbiModuleDescriptorBuilder.cpp:70
llvm::pdb::DbiModuleDescriptorBuilder::setPdbFilePathNI
void setPdbFilePathNI(uint32_t NI)
Definition:DbiModuleDescriptorBuilder.cpp:61
llvm::pdb::DbiModuleDescriptorBuilder::commitSymbolStream
Error commitSymbolStream(const msf::MSFLayout &MsfLayout, WritableBinaryStreamRef MsfBuffer)
Commit the accumulated symbols to the module symbol stream.
Definition:DbiModuleDescriptorBuilder.cpp:164
llvm::pdb::DbiModuleDescriptorBuilder::commit
Error commit(BinaryStreamWriter &ModiWriter)
Commit the DBI descriptor to the DBI stream.
Definition:DbiModuleDescriptorBuilder.cpp:150
llvm::pdb::DbiModuleDescriptorBuilder::getNextSymbolOffset
uint32_t getNextSymbolOffset() const
Return the offset within the module symbol stream of the next symbol record passed to addSymbol.
Definition:DbiModuleDescriptorBuilder.h:117
llvm::pdb::DbiModuleDescriptorBuilder::addSymbolsInBulk
void addSymbolsInBulk(ArrayRef< uint8_t > BulkSymbols)
Definition:DbiModuleDescriptorBuilder.cpp:75
llvm::pdb::DbiModuleDescriptorBuilder::DbiModuleDescriptorBuilder
DbiModuleDescriptorBuilder(StringRef ModuleName, uint32_t ModIndex, msf::MSFBuilder &Msf)
Definition:DbiModuleDescriptorBuilder.cpp:43
llvm::pdb::DbiModuleDescriptorBuilder::calculateSerializedLength
uint32_t calculateSerializedLength() const
Definition:DbiModuleDescriptorBuilder.cpp:113
uint16_t
uint32_t
llvm::COFF::DEBUG_SECTION_MAGIC
@ DEBUG_SECTION_MAGIC
Definition:COFF.h:821
llvm::codeview
Definition:AppendingTypeTableBuilder.h:22
llvm::codeview::CompileSym2Flags::EC
@ EC
llvm::codeview::alignOf
uint32_t alignOf(CodeViewContainer Container)
Definition:CodeView.h:621
llvm::msf
Definition:IMSFFile.h:18
llvm::pdb
Definition:LVCodeViewReader.h:44
llvm::pdb::kInvalidStreamIndex
const uint16_t kInvalidStreamIndex
Definition:RawConstants.h:19
llvm::pdb::PDB_TableType::SourceFiles
@ SourceFiles
llvm::pdb::raw_error_code::stream_too_long
@ stream_too_long
llvm::pdb::DbgHeaderType::Fixup
@ Fixup
llvm::pdb::PDB_ColorItem::Path
@ Path
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition:AddressRanges.h:18
llvm::ModRefInfo::Ref
@ Ref
The access may reference the value stored in memory.
llvm::alignTo
uint64_t alignTo(uint64_t Size, Align A)
Returns a multiple of A needed to store Size bytes.
Definition:Alignment.h:155
std
Implement std::hash so that hash_code can be used in STL containers.
Definition:BitVector.h:858
ModuleName
Definition:ItaniumDemangle.h:1097
llvm::msf::MSFLayout
Definition:MSFCommon.h:51
llvm::pdb::ModuleInfoHeader::PdbFilePathNI
support::ulittle32_t PdbFilePathNI
Name Index for path to compiler PDB.
Definition:RawTypes.h:255
llvm::pdb::ModuleInfoHeader::FileNameOffs
support::ulittle32_t FileNameOffs
Array of [0..NumFiles) DBI name buffer offsets.
Definition:RawTypes.h:249
llvm::pdb::ModuleInfoHeader::ModDiStream
support::ulittle16_t ModDiStream
Stream Number of module debug info.
Definition:RawTypes.h:226
llvm::pdb::ModuleInfoHeader::NumFiles
support::ulittle16_t NumFiles
Number of files contributing to this module.
Definition:RawTypes.h:238
llvm::pdb::ModuleInfoHeader::Flags
support::ulittle16_t Flags
See ModInfoFlags definition.
Definition:RawTypes.h:223
llvm::pdb::ModuleInfoHeader::SrcFileNameNI
support::ulittle32_t SrcFileNameNI
Name Index for src file name.
Definition:RawTypes.h:252
llvm::pdb::ModuleInfoHeader::SC
SectionContrib SC
First section contribution of this module.
Definition:RawTypes.h:220
llvm::pdb::ModuleInfoHeader::C13Bytes
support::ulittle32_t C13Bytes
Size of C13 line number info in above stream.
Definition:RawTypes.h:235
llvm::pdb::ModuleInfoHeader::SymBytes
support::ulittle32_t SymBytes
Size of local symbol debug info in above stream.
Definition:RawTypes.h:229
llvm::pdb::ModuleInfoHeader::C11Bytes
support::ulittle32_t C11Bytes
Size of C11 line number info in above stream.
Definition:RawTypes.h:232
llvm::pdb::ModuleInfoHeader::Mod
support::ulittle32_t Mod
Currently opened module.
Definition:RawTypes.h:217
llvm::pdb::SectionContrib
Definition:RawTypes.h:46
llvm::pdb::StringTableFixup
Represents a string table reference at some offset in the module symbol stream.
Definition:DbiModuleDescriptorBuilder.h:58
llvm::pdb::SymbolListWrapper
Definition:DbiModuleDescriptorBuilder.h:38

Generated on Fri Jul 18 2025 11:16:51 for LLVM by doxygen 1.9.6
[8]ページ先頭

©2009-2025 Movatter.jp