LLVM 23.0.0git
DWARFLinkerUnit.cpp
Go to the documentation of this file.
1//===- DWARFLinkerUnit.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
9#include "DWARFLinkerUnit.h"
10#include "DWARFEmitterImpl.h"
12
13using namespace llvm;
14using namespace dwarf_linker;
15using namespace dwarf_linker::parallel;
16
18 // Check the set for priors.
20 Abbrev.Profile(ID);
21 void *InsertToken;
22
23 DIEAbbrev *InSet = AbbreviationsSet.FindNodeOrInsertPos(ID, InsertToken);
24 // If it's newly added.
25 if (InSet) {
26 // Assign existing abbreviation number.
27 Abbrev.setNumber(InSet->getNumber());
28 } else {
29 // Add to abbreviation list.
30 Abbreviations.push_back(
31 std::make_unique<DIEAbbrev>(Abbrev.getTag(), Abbrev.hasChildren()));
32 for (const auto &Attr : Abbrev.getData())
33 Abbreviations.back()->AddAttribute(Attr);
34 AbbreviationsSet.InsertNode(Abbreviations.back().get(), InsertToken);
35 // Assign the unique abbreviation number.
36 Abbrev.setNumber(Abbreviations.size());
37 Abbreviations.back()->setNumber(Abbreviations.size());
38 }
39}
40
42 const std::vector<std::unique_ptr<DIEAbbrev>> &Abbrevs = getAbbreviations();
43 if (Abbrevs.empty())
44 return Error::success();
45
46 SectionDescriptor &AbbrevSection =
48
49 // For each abbreviation.
50 for (const auto &Abbrev : Abbrevs)
51 emitDwarfAbbrevEntry(*Abbrev, AbbrevSection);
52
53 // Mark end of abbreviations.
54 encodeULEB128(0, AbbrevSection.OS);
55
56 return Error::success();
57}
58
60 SectionDescriptor &AbbrevSection) {
61 // Emit the abbreviations code (base 1 index.)
62 encodeULEB128(Abbrev.getNumber(), AbbrevSection.OS);
63
64 // Emit the abbreviations data.
65 // Emit its Dwarf tag type.
66 encodeULEB128(Abbrev.getTag(), AbbrevSection.OS);
67
68 // Emit whether it has children DIEs.
69 encodeULEB128((unsigned)Abbrev.hasChildren(), AbbrevSection.OS);
70
71 // For each attribute description.
73 for (const DIEAbbrevData &AttrData : Data) {
74 // Emit attribute type.
75 encodeULEB128(AttrData.getAttribute(), AbbrevSection.OS);
76
77 // Emit form type.
78 encodeULEB128(AttrData.getForm(), AbbrevSection.OS);
79
80 // Emit value for DW_FORM_implicit_const.
81 if (AttrData.getForm() == dwarf::DW_FORM_implicit_const)
82 encodeSLEB128(AttrData.getValue(), AbbrevSection.OS);
83 }
84
85 // Mark end of abbreviation.
86 encodeULEB128(0, AbbrevSection.OS);
87 encodeULEB128(0, AbbrevSection.OS);
88}
89
92 if (OutUnitDIE == nullptr)
93 return Error::success();
94
95 // FIXME: Remove dependence on DwarfEmitterImpl/AsmPrinter and emit DIEs
96 // directly.
97
98 SectionDescriptor &OutSection =
101 if (Error Err = Emitter.init(TargetTriple, "__DWARF"))
102 return Err;
103
104 // Emit compile unit header.
105 Emitter.emitCompileUnitHeader(*this);
106 size_t OffsetToAbbreviationTableOffset =
107 (getFormParams().Version >= 5) ? 8 : 6;
108 OutSection.notePatch(DebugOffsetPatch{
109 OffsetToAbbreviationTableOffset,
111
112 // Emit DIEs.
113 Emitter.emitDIE(*OutUnitDIE);
114 Emitter.finish();
115
116 // Set start offset ans size for .debug_info section.
118 return Error::success();
119}
120
122 const Triple &TargetTriple, const DWARFDebugLine::LineTable &OutLineTable,
123 ArrayRef<uint64_t> OrigRowIndices,
124 DenseMap<uint64_t, uint64_t> *RowIndexToSeqStartOffset) {
125 DebugLineSectionEmitter DebugLineEmitter(TargetTriple, *this);
126
127 return DebugLineEmitter.emit(OutLineTable, OrigRowIndices,
128 RowIndexToSeqStartOffset);
129}
130
132 if (getVersion() < 5)
133 return Error::success();
134
135 if (DebugStringIndexMap.empty())
136 return Error::success();
137
138 SectionDescriptor &OutDebugStrOffsetsSection =
140
141 // Emit section header.
142
143 // Emit length.
144 OutDebugStrOffsetsSection.emitUnitLength(0xBADDEF);
145 uint64_t OffsetAfterSectionLength = OutDebugStrOffsetsSection.OS.tell();
146
147 // Emit version.
148 OutDebugStrOffsetsSection.emitIntVal(5, 2);
149
150 // Emit padding.
151 OutDebugStrOffsetsSection.emitIntVal(0, 2);
152
153 // Emit index to offset map.
154 for (const StringEntry *String : DebugStringIndexMap.getValues()) {
155 // Note patch for string offset value.
156 OutDebugStrOffsetsSection.notePatch(
157 DebugStrPatch{{OutDebugStrOffsetsSection.OS.tell()}, String});
158
159 // Emit placeholder for offset value.
160 OutDebugStrOffsetsSection.emitOffset(0xBADDEF);
161 }
162
163 // Patch section length.
164 OutDebugStrOffsetsSection.apply(
165 OffsetAfterSectionLength -
166 OutDebugStrOffsetsSection.getFormParams().getDwarfOffsetByteSize(),
167 dwarf::DW_FORM_sec_offset,
168 OutDebugStrOffsetsSection.OS.tell() - OffsetAfterSectionLength);
169
170 return Error::success();
171}
172
173/// Emit the pubnames or pubtypes section contribution for \p
174/// Unit into \p Sec. The data is provided in \p Info.
175std::optional<uint64_t>
177 const DwarfUnit::AccelInfo &Info,
178 std::optional<uint64_t> LengthOffset) {
179 if (!LengthOffset) {
180 // Emit the header.
181 OutSection.emitIntVal(0xBADDEF,
182 getFormParams().getDwarfOffsetByteSize()); // Length
183 LengthOffset = OutSection.OS.tell();
184
185 OutSection.emitIntVal(dwarf::DW_PUBNAMES_VERSION, 2); // Version
186
187 OutSection.notePatch(DebugOffsetPatch{
188 OutSection.OS.tell(),
190 OutSection.emitOffset(0xBADDEF); // Unit offset
191
192 OutSection.emitIntVal(getUnitSize(), 4); // Size
193 }
194 OutSection.emitOffset(Info.OutOffset);
195
196 // Emit the string itself.
197 OutSection.emitInplaceString(Info.String->first());
198
199 return LengthOffset;
200}
201
202/// Emit .debug_pubnames and .debug_pubtypes for \p Unit.
204 std::optional<uint64_t> NamesLengthOffset;
205 std::optional<uint64_t> TypesLengthOffset;
206
208 if (Info.AvoidForPubSections)
209 return;
210
211 switch (Info.Type) {
213 NamesLengthOffset = emitPubAcceleratorEntry(
215 NamesLengthOffset);
216 } break;
218 TypesLengthOffset = emitPubAcceleratorEntry(
220 TypesLengthOffset);
221 } break;
222 default: {
223 // Nothing to do.
224 } break;
225 }
226 });
227
228 if (NamesLengthOffset) {
229 SectionDescriptor &OutSection =
231 OutSection.emitIntVal(0, 4); // End marker.
232
233 OutSection.apply(*NamesLengthOffset -
235 dwarf::DW_FORM_sec_offset,
236 OutSection.OS.tell() - *NamesLengthOffset);
237 }
238
239 if (TypesLengthOffset) {
240 SectionDescriptor &OutSection =
242 OutSection.emitIntVal(0, 4); // End marker.
243
244 OutSection.apply(*TypesLengthOffset -
246 dwarf::DW_FORM_sec_offset,
247 OutSection.OS.tell() - *TypesLengthOffset);
248 }
249}
dxil DXContainer Global Emitter
Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
Dwarf abbreviation data, describes one attribute of a Dwarf abbreviation.
Definition DIE.h:50
Dwarf abbreviation, describes the organization of a debug information object.
Definition DIE.h:80
unsigned getNumber() const
Definition DIE.h:102
const SmallVectorImpl< DIEAbbrevData > & getData() const
Definition DIE.h:104
dwarf::Tag getTag() const
Accessors.
Definition DIE.h:101
void setNumber(unsigned N)
Definition DIE.h:106
LLVM_ABI void Profile(FoldingSetNodeID &ID) const
Used to gather unique data for the abbreviation folding set.
Definition DIE.cpp:51
bool hasChildren() const
Definition DIE.h:103
A structured debug information entry.
Definition DIE.h:828
Lightweight error class with error context and mandatory checking.
Definition Error.h:159
static ErrorSuccess success()
Create a success value.
Definition Error.h:336
This class is used to gather all the unique data bits of a node.
Definition FoldingSet.h:208
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Triple - Helper class for working with autoconf configuration names.
Definition Triple.h:47
This class emits specified line table into the .debug_line section.
Error emit(const DWARFDebugLine::LineTable &LineTable, ArrayRef< uint64_t > OrigRowIndices={}, DenseMap< uint64_t, uint64_t > *RowIndexToSeqStartOffset=nullptr)
This class emits DWARF data to the output stream.
std::vector< std::unique_ptr< DIEAbbrev > > Abbreviations
Storage for the unique Abbreviations.
uint64_t getUnitSize() const
Returns size of this(newly generated) compile unit.
void assignAbbrev(DIEAbbrev &Abbrev)
Adds Abbrev into unit`s abbreviation table.
IndexedValuesMap< const StringEntry * > DebugStringIndexMap
Maps a string into the index inside .debug_str_offsets section.
std::optional< uint64_t > emitPubAcceleratorEntry(SectionDescriptor &OutSection, const AccelInfo &Info, std::optional< uint64_t > LengthOffset)
Emit single pubnames/pubtypes accelerator entry.
unsigned ID
Unique ID for the unit.
const std::vector< std::unique_ptr< DIEAbbrev > > & getAbbreviations() const
Returns abbreviations for this compile unit.
void emitDwarfAbbrevEntry(const DIEAbbrev &Abbrev, SectionDescriptor &AbbrevSection)
Emit single abbreviation entry.
FoldingSet< DIEAbbrev > AbbreviationsSet
FoldingSet that uniques the abbreviations.
DIE * getOutUnitDIE()
Returns output unit DIE.
const dwarf::FormParams & getFormParams() const
Return size of address.
uint16_t getVersion() const
Return DWARF version.
SectionDescriptor & getOrCreateSectionDescriptor(DebugSectionKind SectionKind)
Returns descriptor for the specified section of SectionKind.
uint64_t tell() const
tell - Return the current offset with the file.
Error emitDebugInfo(const Triple &TargetTriple)
Emit .debug_info section for unit DIEs.
Error emitDebugLine(const Triple &TargetTriple, const DWARFDebugLine::LineTable &OutLineTable, ArrayRef< uint64_t > OrigRowIndices={}, DenseMap< uint64_t, uint64_t > *RowIndexToSeqStartOffset=nullptr)
Emit .debug_line section.
Error emitDebugStringOffsetSection()
Emit the .debug_str_offsets section for current unit.
virtual void forEachAcceleratorRecord(function_ref< void(AccelInfo &)> Handler)=0
Enumerates accelerator data.
void emitPubAccelerators()
Emit .debug_pubnames and .debug_pubtypes for Unit.
StringMapEntry< EmptyStringSetTag > StringEntry
StringEntry keeps data of the string: the length, external offset and a string body which is placed r...
Definition StringPool.h:23
@ DW_PUBNAMES_VERSION
Section version number for .debug_pubnames.
Definition Dwarf.h:65
This is an optimization pass for GlobalISel generic memory operations.
FunctionAddr VTableAddr uintptr_t uintptr_t Data
Definition InstrProf.h:221
unsigned encodeSLEB128(int64_t Value, raw_ostream &OS, unsigned PadTo=0)
Utility function to encode a SLEB128 value to an output stream.
Definition LEB128.h:24
unsigned encodeULEB128(uint64_t Value, raw_ostream &OS, unsigned PadTo=0)
Utility function to encode a ULEB128 value to an output stream.
Definition LEB128.h:79
uint8_t getDwarfOffsetByteSize() const
The size of a reference is determined by the DWARF 32/64-bit format.
Definition Dwarf.h:1132
This structure is used to update strings offsets into .debug_str.
This structure keeps fields which would be used for creating accelerator table.
dwarf::FormParams getFormParams() const
Returns FormParams used by section.
This structure is used to keep data of the concrete section.
raw_svector_ostream OS
Stream which stores data to the Contents.
void setSizesForSectionCreatedByAsmPrinter()
Some sections are emitted using AsmPrinter.
void emitUnitLength(uint64_t Length)
Emit unit length into the current section contents.
void emitOffset(uint64_t Val)
Emit specified offset value into the current section contents.
void emitIntVal(uint64_t Val, unsigned Size)
Emit specified integer value into the current section contents.
void apply(uint64_t PatchOffset, dwarf::Form AttrForm, uint64_t Val)
Write specified Value of AttrForm to the PatchOffset.
void emitInplaceString(StringRef String)
Emit specified inplace string value into the current section contents.