LLVM 20.0.0git
DWARFLinkerImpl.h
Go to the documentation of this file.
1//===- DWARFLinkerImpl.h ----------------------------------------*- 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#ifndef LLVM_LIB_DWARFLINKER_PARALLEL_DWARFLINKERIMPL_H
10#define LLVM_LIB_DWARFLINKER_PARALLEL_DWARFLINKERIMPL_H
11
12#include "DWARFEmitterImpl.h"
14#include "DWARFLinkerTypeUnit.h"
20
21namespace llvm {
22namespace dwarf_linker {
23namespace parallel {
24
25/// This class links debug info.
27public:
29 MessageHandlerTy WarningHandler);
30
31 /// Add object file to be linked. Pre-load compile unit die. Call
32 /// \p OnCUDieLoaded for each compile unit die. If specified \p File
33 /// has reference to the Clang module then such module would be
34 /// pre-loaded by \p Loader for !Update case.
35 ///
36 /// \pre NoODR, Update options should be set before call to addObjectFile.
37 void addObjectFile(
38 DWARFFile &File, ObjFileLoaderTy Loader = nullptr,
39
40 CompileUnitHandlerTy OnCUDieLoaded = [](const DWARFUnit &) {}) override;
41
42 /// Link debug info for added files.
43 Error link() override;
44
45 /// Set output DWARF handler. May be not set if output generation is not
46 /// necessary.
47 void setOutputDWARFHandler(const Triple &TargetTriple,
49 GlobalData.setTargetTriple(TargetTriple);
50 this->SectionHandler = SectionHandler;
51 }
52
53 /// \defgroup Methods setting various linking options:
54 ///
55 /// @{
56 ///
57
58 /// Allows to generate log of linking process to the standard output.
59 void setVerbosity(bool Verbose) override {
61 }
62
63 /// Print statistics to standard output.
64 void setStatistics(bool Statistics) override {
65 GlobalData.Options.Statistics = Statistics;
66 }
67
68 /// Verify the input DWARF.
69 void setVerifyInputDWARF(bool Verify) override {
71 }
72
73 /// Do not unique types according to ODR.
74 void setNoODR(bool NoODR) override { GlobalData.Options.NoODR = NoODR; }
75
76 /// Update index tables only(do not modify rest of DWARF).
77 void setUpdateIndexTablesOnly(bool UpdateIndexTablesOnly) override {
78 GlobalData.Options.UpdateIndexTablesOnly = UpdateIndexTablesOnly;
79 }
80
81 /// Allow generating valid, but non-deterministic output.
82 void
83 setAllowNonDeterministicOutput(bool AllowNonDeterministicOutput) override {
85 AllowNonDeterministicOutput;
86 }
87
88 /// Set to keep the enclosing function for a static variable.
89 void setKeepFunctionForStatic(bool KeepFunctionForStatic) override {
90 GlobalData.Options.KeepFunctionForStatic = KeepFunctionForStatic;
91 }
92
93 /// Use specified number of threads for parallel files linking.
94 void setNumThreads(unsigned NumThreads) override {
95 GlobalData.Options.Threads = NumThreads;
96 }
97
98 /// Add kind of accelerator tables to be generated.
99 void addAccelTableKind(AccelTableKind Kind) override {
102 }
103
104 /// Set prepend path for clang modules.
105 void setPrependPath(StringRef Ppath) override {
107 }
108
109 /// Set estimated objects files amount, for preliminary data allocation.
110 void setEstimatedObjfilesAmount(unsigned ObjFilesNum) override;
111
112 /// Set verification handler which would be used to report verification
113 /// errors.
114 void
117 }
118
119 /// Set map for Swift interfaces.
122 }
123
124 /// Set prefix map for objects.
127 }
128
129 /// Set target DWARF version.
130 Error setTargetDWARFVersion(uint16_t TargetDWARFVersion) override {
131 if ((TargetDWARFVersion < 1) || (TargetDWARFVersion > 5))
132 return createStringError(std::errc::invalid_argument,
133 "unsupported DWARF version: %d",
134 TargetDWARFVersion);
135
136 GlobalData.Options.TargetDWARFVersion = TargetDWARFVersion;
137 return Error::success();
138 }
139 /// @}
140
141protected:
142 /// Verify input DWARF file.
143 void verifyInput(const DWARFFile &File);
144
145 /// Validate specified options.
147
148 /// Take already linked compile units and glue them into single file.
150
151 /// Hold the input and output of the debug info size in bytes.
155 };
156
157 friend class DependencyTracker;
158 /// Keeps track of data associated with one object during linking.
159 /// i.e. source file descriptor, compilation units, output data
160 /// for compilation units common tables.
161 struct LinkContext : public OutputSections {
163
164 /// Keep information for referenced clang module: already loaded DWARF info
165 /// of the clang module and a CompileUnit of the module.
167 RefModuleUnit(DWARFFile &File, std::unique_ptr<CompileUnit> Unit);
169 RefModuleUnit(const RefModuleUnit &) = delete;
170
172 std::unique_ptr<CompileUnit> Unit;
173 };
175
176 /// Object file descriptor.
178
179 /// Set of Compilation Units(may be accessed asynchroniously for reading).
181
182 /// Set of Compile Units for modules.
184
185 /// Size of Debug info before optimizing.
187
188 /// Flag indicating that all inter-connected units are loaded
189 /// and the dwarf linking process for these units is started.
191
193
194 /// Flag indicating that new inter-connected compilation units were
195 /// discovered. It is used for restarting units processing
196 /// if new inter-connected units were found.
197 std::atomic<bool> HasNewInterconnectedCUs = {false};
198
199 std::atomic<bool> HasNewGlobalDependency = {false};
200
201 /// Counter for compile units ID.
202 std::atomic<size_t> &UniqueUnitID;
203
206 std::atomic<size_t> &UniqueUnitID);
207
208 /// Check whether specified \p CUDie is a Clang module reference.
209 /// if \p Quiet is false then display error messages.
210 /// \return first == true if CUDie is a Clang module reference.
211 /// second == true if module is already loaded.
212 std::pair<bool, bool> isClangModuleRef(const DWARFDie &CUDie,
213 std::string &PCMFile,
214 unsigned Indent, bool Quiet);
215
216 /// If this compile unit is really a skeleton CU that points to a
217 /// clang module, register it in ClangModules and return true.
218 ///
219 /// A skeleton CU is a CU without children, a DW_AT_gnu_dwo_name
220 /// pointing to the module, and a DW_AT_gnu_dwo_id with the module
221 /// hash.
222 bool registerModuleReference(const DWARFDie &CUDie, ObjFileLoaderTy Loader,
223 CompileUnitHandlerTy OnCUDieLoaded,
224 unsigned Indent = 0);
225
226 /// Recursively add the debug info in this clang module .pcm
227 /// file (and all the modules imported by it in a bottom-up fashion)
228 /// to ModuleUnits.
229 Error loadClangModule(ObjFileLoaderTy Loader, const DWARFDie &CUDie,
230 const std::string &PCMFile,
231 CompileUnitHandlerTy OnCUDieLoaded,
232 unsigned Indent = 0);
233
234 /// Add Compile Unit corresponding to the module.
236
237 /// Computes the total size of the debug info.
239 uint64_t Size = 0;
240
241 if (InputDWARFFile.Dwarf == nullptr)
242 return Size;
243
244 for (auto &Unit : InputDWARFFile.Dwarf->compile_units())
245 Size += Unit->getLength();
246
247 return Size;
248 }
249
250 /// Link compile units for this context.
252
253 /// Link specified compile unit until specified stage.
257
258 /// Emit invariant sections.
260
261 /// Clone and emit .debug_frame.
263
264 /// Emit FDE record.
265 void emitFDE(uint32_t CIEOffset, uint32_t AddrSize, uint64_t Address,
266 StringRef FDEBytes, SectionDescriptor &Section);
267
269 [&](uint64_t Offset) -> CompileUnit * {
270 auto CU = llvm::upper_bound(
272 [](uint64_t LHS, const std::unique_ptr<CompileUnit> &RHS) {
273 return LHS < RHS->getOrigUnit().getNextUnitOffset();
274 });
275
276 return CU != CompileUnits.end() ? CU->get() : nullptr;
277 };
278 };
279
280 /// Enumerate all compile units and assign offsets to their sections and
281 /// strings.
282 void assignOffsets();
283
284 /// Enumerate all compile units and assign offsets to their sections.
286
287 /// Enumerate all compile units and assign offsets to their strings.
289
290 /// Print statistic for processed Debug Info.
291 void printStatistic();
292
294
295 /// Enumerates all strings.
298 StringHandler);
299
300 /// Enumerates sections for modules, invariant for object files, compile
301 /// units.
303 function_ref<void(OutputSections &SectionsSet)> SectionsSetHandler);
304
305 /// Enumerates all compile and type units.
306 void forEachCompileAndTypeUnit(function_ref<void(DwarfUnit *CU)> UnitHandler);
307
308 /// Enumerates all comple units.
309 void forEachCompileUnit(function_ref<void(CompileUnit *CU)> UnitHandler);
310
311 /// Enumerates all patches and update them with the correct values.
313
314 /// Emit debug sections common for all input files.
316
317 /// Emit apple accelerator sections.
318 void emitAppleAcceleratorSections(const Triple &TargetTriple);
319
320 /// Emit .debug_names section.
321 void emitDWARFv5DebugNamesSection(const Triple &TargetTriple);
322
323 /// Emit string sections.
324 void emitStringSections();
325
326 /// Cleanup data(string pools) after output sections are generated.
328
329 /// Enumerate all compile units and put their data into the output stream.
331
332 /// Enumerate common sections and put their data into the output stream.
334
335 /// \defgroup Data members accessed asinchroniously.
336 ///
337 /// @{
338
339 /// Unique ID for compile unit.
340 std::atomic<size_t> UniqueUnitID;
341
342 /// Mapping the PCM filename to the DwoId.
345
346 /// Type unit.
347 std::unique_ptr<TypeUnit> ArtificialTypeUnit;
348 /// @}
349
350 /// \defgroup Data members accessed sequentially.
351 ///
352 /// @{
353 /// Data global for the whole linking process.
355
356 /// DwarfStringPoolEntries for .debug_str section.
358
359 /// DwarfStringPoolEntries for .debug_line_str section.
361
362 /// Keeps all linking contexts.
364
365 /// Common sections.
367
368 /// Hanler for output sections.
370
371 /// Overall compile units number.
373 /// @}
374};
375
376} // end of namespace parallel
377} // end of namespace dwarf_linker
378} // end of namespace llvm
379
380#endif // LLVM_LIB_DWARFLINKER_PARALLEL_DWARFLINKERIMPL_H
This file contains support for writing accelerator tables.
uint64_t Size
static fatal_error_handler_t ErrorHandler
ppc ctr loops PowerPC CTR Loops Verify
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
Value * RHS
Value * LHS
Utility class that carries the DWARF compile/type unit and the debug info entry in an object.
Definition: DWARFDie.h:42
Lightweight error class with error context and mandatory checking.
Definition: Error.h:160
static ErrorSuccess success()
Create a success value.
Definition: Error.h:337
reference emplace_back(ArgTypes &&... Args)
Definition: SmallVector.h:937
StringMapEntry - This is used to represent one value that is inserted into a StringMap.
StringMap - This is an unconventional map that is specialized for handling keys that are "strings",...
Definition: StringMap.h:128
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:51
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:44
This class represents DWARF information for source file and it's address map.
Definition: DWARFFile.h:25
std::unique_ptr< DWARFContext > Dwarf
Source DWARF information.
Definition: DWARFFile.h:39
std::map< std::string, std::string > ObjectPrefixMapTy
AccelTableKind
The kind of accelerator tables to be emitted.
std::function< void(const Twine &Warning, StringRef Context, const DWARFDie *DIE)> MessageHandlerTy
std::function< void(const DWARFFile &File, llvm::StringRef Output)> InputVerificationHandlerTy
std::map< std::string, std::string > SwiftInterfacesMapTy
std::function< ErrorOr< DWARFFile & >(StringRef ContainerName, StringRef Path)> ObjFileLoaderTy
Stores all information related to a compile unit, be it in its original instance of the object file o...
Stage
The stages of new compile unit processing.
@ Cleaned
Resources(Input DWARF, Output DWARF tree) are released.
void forEachObjectSectionsSet(function_ref< void(OutputSections &SectionsSet)> SectionsSetHandler)
Enumerates sections for modules, invariant for object files, compile units.
void emitDWARFv5DebugNamesSection(const Triple &TargetTriple)
Emit .debug_names section.
void writeCompileUnitsToTheOutput()
Enumerate all compile units and put their data into the output stream.
void forEachCompileUnit(function_ref< void(CompileUnit *CU)> UnitHandler)
Enumerates all comple units.
void assignOffsetsToStrings()
Enumerate all compile units and assign offsets to their strings.
void assignOffsets()
Enumerate all compile units and assign offsets to their sections and strings.
Error link() override
Link debug info for added files.
Error validateAndUpdateOptions()
Validate specified options.
void writeCommonSectionsToTheOutput()
Enumerate common sections and put their data into the output stream.
void assignOffsetsToSections()
Enumerate all compile units and assign offsets to their sections.
void printStatistic()
Print statistic for processed Debug Info.
void glueCompileUnitsAndWriteToTheOutput()
Take already linked compile units and glue them into single file.
void emitAppleAcceleratorSections(const Triple &TargetTriple)
Emit apple accelerator sections.
void verifyInput(const DWARFFile &File)
Verify input DWARF file.
void forEachCompileAndTypeUnit(function_ref< void(DwarfUnit *CU)> UnitHandler)
Enumerates all compile and type units.
void addObjectFile(DWARFFile &File, ObjFileLoaderTy Loader=nullptr, CompileUnitHandlerTy OnCUDieLoaded=[](const DWARFUnit &) {}) override
Add object file to be linked.
void cleanupDataAfterDWARFOutputIsWritten()
Cleanup data(string pools) after output sections are generated.
void forEachOutputString(function_ref< void(StringDestinationKind, const StringEntry *)> StringHandler)
Enumerates all strings.
void setOutputDWARFHandler(const Triple &TargetTriple, SectionHandlerTy SectionHandler) override
Set output DWARF handler.
void emitCommonSectionsAndWriteCompileUnitsToTheOutput()
Emit debug sections common for all input files.
void patchOffsetsAndSizes()
Enumerates all patches and update them with the correct values.
This class discovers DIEs dependencies: marks "live" DIEs, marks DIE locations (whether DIE should be...
Base class for all Dwarf units(Compile unit/Type table unit).
This class keeps data and services common for the whole linking process.
const DWARFLinkerOptions & getOptions() const
Returns linking options.
void setTargetTriple(const Triple &TargetTriple)
Set target triple.
This class keeps contents and offsets to the debug sections.
This class creates a DwarfStringPoolEntry for the corresponding StringEntry.
Type Unit is used to represent an artificial compilation unit which keeps all type information.
An efficient, type-erasing, non-owning reference to a callable.
std::atomic< size_t > UniqueUnitID
Unique ID for compile unit.
uint64_t OverallNumberOfCU
Overall compile units number.
SmallVector< std::unique_ptr< LinkContext > > ObjectContexts
Keeps all linking contexts.
StringEntryToDwarfStringPoolEntryMap DebugLineStrStrings
DwarfStringPoolEntries for .debug_line_str section.
SectionHandlerTy SectionHandler
Hanler for output sections.
std::unique_ptr< TypeUnit > ArtificialTypeUnit
Type unit.
StringEntryToDwarfStringPoolEntryMap DebugStrStrings
DwarfStringPoolEntries for .debug_str section.
OutputSections CommonSections
Common sections.
StringMap< uint64_t > ClangModules
Mapping the PCM filename to the DwoId.
void setNumThreads(unsigned NumThreads) override
Use specified number of threads for parallel files linking.
Error setTargetDWARFVersion(uint16_t TargetDWARFVersion) override
Set target DWARF version.
void setVerbosity(bool Verbose) override
Allows to generate log of linking process to the standard output.
void setInputVerificationHandler(InputVerificationHandlerTy Handler) override
Set verification handler which would be used to report verification errors.
void setObjectPrefixMap(ObjectPrefixMapTy *Map) override
Set prefix map for objects.
void addAccelTableKind(AccelTableKind Kind) override
Add kind of accelerator tables to be generated.
void setAllowNonDeterministicOutput(bool AllowNonDeterministicOutput) override
Allow generating valid, but non-deterministic output.
void setVerifyInputDWARF(bool Verify) override
Verify the input DWARF.
void setKeepFunctionForStatic(bool KeepFunctionForStatic) override
Set to keep the enclosing function for a static variable.
void setUpdateIndexTablesOnly(bool UpdateIndexTablesOnly) override
Update index tables only(do not modify rest of DWARF).
void setSwiftInterfacesMap(SwiftInterfacesMapTy *Map) override
Set map for Swift interfaces.
void setStatistics(bool Statistics) override
Print statistics to standard output.
void setNoODR(bool NoODR) override
Do not unique types according to ODR.
void setPrependPath(StringRef Ppath) override
Set prepend path for clang modules.
void setEstimatedObjfilesAmount(unsigned ObjFilesNum) override
Set estimated objects files amount, for preliminary data allocation.
std::function< void(std::shared_ptr< SectionDescriptorBase > Section)> SectionHandlerTy
Definition: DWARFLinker.h:118
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
@ Offset
Definition: DWP.cpp:480
auto upper_bound(R &&Range, T &&Value)
Provide wrappers to std::upper_bound which take ranges instead of having to pass begin/end explicitly...
Definition: STLExtras.h:1991
Error createStringError(std::error_code EC, char const *Fmt, const Ts &... Vals)
Create formatted StringError object.
Definition: Error.h:1291
@ Other
Any other memory.
bool is_contained(R &&Range, const E &Element)
Returns true if Element is found in Range.
Definition: STLExtras.h:1903
Hold the input and output of the debug info size in bytes.
Keep information for referenced clang module: already loaded DWARF info of the clang module and a Com...
Keeps track of data associated with one object during linking.
uint64_t getInputDebugInfoSize() const
Computes the total size of the debug info.
bool InterCUProcessingStarted
Flag indicating that all inter-connected units are loaded and the dwarf linking process for these uni...
bool registerModuleReference(const DWARFDie &CUDie, ObjFileLoaderTy Loader, CompileUnitHandlerTy OnCUDieLoaded, unsigned Indent=0)
If this compile unit is really a skeleton CU that points to a clang module, register it in ClangModul...
Error loadClangModule(ObjFileLoaderTy Loader, const DWARFDie &CUDie, const std::string &PCMFile, CompileUnitHandlerTy OnCUDieLoaded, unsigned Indent=0)
Recursively add the debug info in this clang module .pcm file (and all the modules imported by it in ...
uint64_t OriginalDebugInfoSize
Size of Debug info before optimizing.
std::pair< bool, bool > isClangModuleRef(const DWARFDie &CUDie, std::string &PCMFile, unsigned Indent, bool Quiet)
Check whether specified CUDie is a Clang module reference.
void addModulesCompileUnit(RefModuleUnit &&Unit)
Add Compile Unit corresponding to the module.
void emitFDE(uint32_t CIEOffset, uint32_t AddrSize, uint64_t Address, StringRef FDEBytes, SectionDescriptor &Section)
Emit FDE record.
UnitListTy CompileUnits
Set of Compilation Units(may be accessed asynchroniously for reading).
void linkSingleCompileUnit(CompileUnit &CU, TypeUnit *ArtificialTypeUnit, enum CompileUnit::Stage DoUntilStage=CompileUnit::Stage::Cleaned)
Link specified compile unit until specified stage.
std::atomic< bool > HasNewInterconnectedCUs
Flag indicating that new inter-connected compilation units were discovered.
Error cloneAndEmitDebugFrame()
Clone and emit .debug_frame.
std::atomic< size_t > & UniqueUnitID
Counter for compile units ID.
std::function< CompileUnit *(uint64_t)> getUnitForOffset
ModuleUnitListTy ModulesCompileUnits
Set of Compile Units for modules.
bool KeepFunctionForStatic
Whether we want a static variable to force us to keep its enclosing function.
DWARFLinkerBase::ObjectPrefixMapTy * ObjectPrefixMap
A list of remappings to apply to file paths.
bool Verbose
Generate processing log to the standard output.
DWARFLinkerBase::SwiftInterfacesMapTy * ParseableSwiftInterfaces
A list of all .swiftinterface files referenced by the debug info, mapping Module name to path on disk...
SmallVector< DWARFLinkerBase::AccelTableKind, 1 > AccelTables
The accelerator table kinds.
uint16_t TargetDWARFVersion
DWARF version for the output.
bool NoODR
Do not unique types according to ODR.
bool AllowNonDeterministicOutput
Allow to generate valid, but non deterministic output.
std::string PrependPath
Prepend path for the clang modules.
DWARFLinkerBase::InputVerificationHandlerTy InputVerificationHandler
input verification handler(it might be called asynchronously).
This structure is used to keep data of the concrete section.