LLVM 18.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_DWARFLINKERPARALLEL_DWARFLINKERIMPL_H
10#define LLVM_LIB_DWARFLINKERPARALLEL_DWARFLINKERIMPL_H
11
12#include "DWARFEmitterImpl.h"
14#include "DWARFLinkerTypeUnit.h"
20
21namespace llvm {
22namespace dwarflinker_parallel {
23
24/// This class links debug info.
26public:
28 MessageHandlerTy WarningHandler,
29 TranslatorFuncTy StringsTranslator);
30
31 /// Create debug info emitter.
32 Error createEmitter(const Triple &TheTriple, OutputFileType FileType,
33 raw_pwrite_stream &OutFile) override;
34
35 ExtraDwarfEmitter *getEmitter() override;
36
37 /// Add object file to be linked. Pre-load compile unit die. Call
38 /// \p OnCUDieLoaded for each compile unit die. If specified \p File
39 /// has reference to the Clang module then such module would be
40 /// pre-loaded by \p Loader for !Update case.
41 ///
42 /// \pre NoODR, Update options should be set before call to addObjectFile.
43 void addObjectFile(
44 DWARFFile &File, ObjFileLoaderTy Loader = nullptr,
45
46 CompileUnitHandlerTy OnCUDieLoaded = [](const DWARFUnit &) {}) override;
47
48 /// Link debug info for added files.
49 Error link() override;
50
51 /// \defgroup Methods setting various linking options:
52 ///
53 /// @{
54 ///
55
56 /// Allows to generate log of linking process to the standard output.
57 void setVerbosity(bool Verbose) override {
59 }
60
61 /// Print statistics to standard output.
62 void setStatistics(bool Statistics) override {
63 GlobalData.Options.Statistics = Statistics;
64 }
65
66 /// Verify the input DWARF.
67 void setVerifyInputDWARF(bool Verify) override {
69 }
70
71 /// Do not unique types according to ODR.
72 void setNoODR(bool NoODR) override { GlobalData.Options.NoODR = NoODR; }
73
74 /// Update index tables only(do not modify rest of DWARF).
75 void setUpdateIndexTablesOnly(bool UpdateIndexTablesOnly) override {
76 GlobalData.Options.UpdateIndexTablesOnly = UpdateIndexTablesOnly;
77 }
78
79 /// Allow generating valid, but non-deterministic output.
80 void
81 setAllowNonDeterministicOutput(bool AllowNonDeterministicOutput) override {
83 AllowNonDeterministicOutput;
84 }
85
86 /// Set to keep the enclosing function for a static variable.
87 void setKeepFunctionForStatic(bool KeepFunctionForStatic) override {
88 GlobalData.Options.KeepFunctionForStatic = KeepFunctionForStatic;
89 }
90
91 /// Use specified number of threads for parallel files linking.
92 void setNumThreads(unsigned NumThreads) override {
93 GlobalData.Options.Threads = NumThreads;
94 }
95
96 /// Add kind of accelerator tables to be generated.
97 void addAccelTableKind(AccelTableKind Kind) override {
100 }
101
102 /// Set prepend path for clang modules.
103 void setPrependPath(const std::string &Ppath) override {
105 }
106
107 /// Set estimated objects files amount, for preliminary data allocation.
108 void setEstimatedObjfilesAmount(unsigned ObjFilesNum) override;
109
110 /// Set verification handler which would be used to report verification
111 /// errors.
112 void
115 }
116
117 /// Set map for Swift interfaces.
120 }
121
122 /// Set prefix map for objects.
125 }
126
127 /// Set target DWARF version.
128 Error setTargetDWARFVersion(uint16_t TargetDWARFVersion) override {
129 if ((TargetDWARFVersion < 1) || (TargetDWARFVersion > 5))
130 return createStringError(std::errc::invalid_argument,
131 "unsupported DWARF version: %d",
132 TargetDWARFVersion);
133
134 GlobalData.Options.TargetDWARFVersion = TargetDWARFVersion;
135 return Error::success();
136 }
137 /// @}
138
139protected:
140 /// Verify input DWARF file.
141 void verifyInput(const DWARFFile &File);
142
143 /// Validate specified options.
145
146 /// Take already linked compile units and glue them into single file.
148
149 /// Hold the input and output of the debug info size in bytes.
153 };
154
155 friend class DependencyTracker;
156 /// Keeps track of data associated with one object during linking.
157 /// i.e. source file descriptor, compilation units, output data
158 /// for compilation units common tables.
159 struct LinkContext : public OutputSections {
161
162 /// Keep information for referenced clang module: already loaded DWARF info
163 /// of the clang module and a CompileUnit of the module.
165 RefModuleUnit(DWARFFile &File, std::unique_ptr<CompileUnit> Unit);
167 RefModuleUnit(const RefModuleUnit &) = delete;
168
170 std::unique_ptr<CompileUnit> Unit;
171 };
173
174 /// Object file descriptor.
176
177 /// Set of Compilation Units(may be accessed asynchroniously for reading).
179
180 /// Set of Compile Units for modules.
182
183 /// Size of Debug info before optimizing.
185
186 /// Flag indicating that all inter-connected units are loaded
187 /// and the dwarf linking process for these units is started.
189
191
192 std::optional<Triple> TargetTriple;
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 std::optional<Triple> TargetTriple);
208
209 /// Check whether specified \p CUDie is a Clang module reference.
210 /// if \p Quiet is false then display error messages.
211 /// \return first == true if CUDie is a Clang module reference.
212 /// second == true if module is already loaded.
213 std::pair<bool, bool> isClangModuleRef(const DWARFDie &CUDie,
214 std::string &PCMFile,
215 unsigned Indent, bool Quiet);
216
217 /// If this compile unit is really a skeleton CU that points to a
218 /// clang module, register it in ClangModules and return true.
219 ///
220 /// A skeleton CU is a CU without children, a DW_AT_gnu_dwo_name
221 /// pointing to the module, and a DW_AT_gnu_dwo_id with the module
222 /// hash.
223 bool registerModuleReference(const DWARFDie &CUDie, ObjFileLoaderTy Loader,
224 CompileUnitHandlerTy OnCUDieLoaded,
225 unsigned Indent = 0);
226
227 /// Recursively add the debug info in this clang module .pcm
228 /// file (and all the modules imported by it in a bottom-up fashion)
229 /// to ModuleUnits.
230 Error loadClangModule(ObjFileLoaderTy Loader, const DWARFDie &CUDie,
231 const std::string &PCMFile,
232 CompileUnitHandlerTy OnCUDieLoaded,
233 unsigned Indent = 0);
234
235 /// Add Compile Unit corresponding to the module.
237
238 /// Computes the total size of the debug info.
240 uint64_t Size = 0;
241
242 if (InputDWARFFile.Dwarf == nullptr)
243 return Size;
244
245 for (auto &Unit : InputDWARFFile.Dwarf->compile_units())
246 Size += Unit->getLength();
247
248 return Size;
249 }
250
251 /// Link compile units for this context.
253
254 /// Link specified compile unit until specified stage.
258
259 /// Emit invariant sections.
261
262 /// Clone and emit .debug_frame.
264
265 /// Emit FDE record.
266 void emitFDE(uint32_t CIEOffset, uint32_t AddrSize, uint64_t Address,
267 StringRef FDEBytes, SectionDescriptor &Section);
268
270 [&](uint64_t Offset) -> CompileUnit * {
271 auto CU = llvm::upper_bound(
273 [](uint64_t LHS, const std::unique_ptr<CompileUnit> &RHS) {
274 return LHS < RHS->getOrigUnit().getNextUnitOffset();
275 });
276
277 return CU != CompileUnits.end() ? CU->get() : nullptr;
278 };
279 };
280
281 /// Enumerate all compile units and assign offsets to their sections and
282 /// strings.
283 void assignOffsets();
284
285 /// Enumerate all compile units and assign offsets to their sections.
287
288 /// Enumerate all compile units and assign offsets to their strings.
290
291 /// Print statistic for processed Debug Info.
292 void printStatistic();
293
295
296 /// Enumerates all strings.
299 StringHandler);
300
301 /// Enumerates sections for modules, invariant for object files, compile
302 /// units.
304 function_ref<void(OutputSections &SectionsSet)> SectionsSetHandler);
305
306 /// Enumerates all compile and type units.
307 void forEachCompileAndTypeUnit(function_ref<void(DwarfUnit *CU)> UnitHandler);
308
309 /// Enumerates all comple units.
310 void forEachCompileUnit(function_ref<void(CompileUnit *CU)> UnitHandler);
311
312 /// Enumerates all patches and update them with the correct values.
314
315 /// Emit debug sections common for all input files.
317
318 /// Emit apple accelerator sections.
319 void emitAppleAcceleratorSections(const Triple &TargetTriple);
320
321 /// Emit .debug_names section.
322 void emitDWARFv5DebugNamesSection(const Triple &TargetTriple);
323
324 /// Emit string sections.
325 void emitStringSections();
326
327 /// Cleanup data(string pools) after output sections are generated.
329
330 /// Enumerate all compile units and put their data into the output stream.
332
333 /// Enumerate common sections and put their data into the output stream.
335
336 /// \defgroup Data members accessed asinchroniously.
337 ///
338 /// @{
339
340 /// Unique ID for compile unit.
341 std::atomic<size_t> UniqueUnitID;
342
343 /// Mapping the PCM filename to the DwoId.
346
347 /// Type unit.
348 std::unique_ptr<TypeUnit> ArtificialTypeUnit;
349 /// @}
350
351 /// \defgroup Data members accessed sequentially.
352 ///
353 /// @{
354 /// DwarfStringPoolEntries for .debug_str section.
356
357 /// DwarfStringPoolEntries for .debug_line_str section.
359
360 /// Keeps all linking contexts.
362
363 /// Common sections.
365
366 /// The emitter of final dwarf file.
367 std::unique_ptr<DwarfEmitterImpl> TheDwarfEmitter;
368
369 /// Overall compile units number.
371
372 /// Data global for the whole linking process.
374 /// @}
375};
376
377} // end namespace dwarflinker_parallel
378} // end namespace llvm
379
380#endif // LLVM_LIB_DWARFLINKERPARALLEL_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:334
reference emplace_back(ArgTypes &&... Args)
Definition: SmallVector.h:941
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:112
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:44
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.
This class represents DWARF information for source file and it's address map.
Definition: DWARFFile.h:26
std::unique_ptr< DWARFContext > Dwarf
Source DWARF information.
Definition: DWARFFile.h:38
void glueCompileUnitsAndWriteToTheOutput()
Take already linked compile units and glue them into single file.
void verifyInput(const DWARFFile &File)
Verify input DWARF file.
void forEachCompileUnit(function_ref< void(CompileUnit *CU)> UnitHandler)
Enumerates all comple units.
void addObjectFile(DWARFFile &File, ObjFileLoaderTy Loader=nullptr, CompileUnitHandlerTy OnCUDieLoaded=[](const DWARFUnit &) {}) override
Add object file to be linked.
void printStatistic()
Print statistic for processed Debug Info.
void forEachObjectSectionsSet(function_ref< void(OutputSections &SectionsSet)> SectionsSetHandler)
Enumerates sections for modules, invariant for object files, compile units.
void emitCommonSectionsAndWriteCompileUnitsToTheOutput()
Emit debug sections common for all input files.
void forEachOutputString(function_ref< void(StringDestinationKind, const StringEntry *)> StringHandler)
Enumerates all strings.
void emitDWARFv5DebugNamesSection(const Triple &TargetTriple)
Emit .debug_names section.
void writeCommonSectionsToTheOutput()
Enumerate common sections and put their data into the output stream.
void emitAppleAcceleratorSections(const Triple &TargetTriple)
Emit apple accelerator sections.
Error createEmitter(const Triple &TheTriple, OutputFileType FileType, raw_pwrite_stream &OutFile) override
Create debug info emitter.
void emitStringSections()
Emit string sections.
ExtraDwarfEmitter * getEmitter() override
Returns previously created dwarf emitter. May be nullptr.
void writeCompileUnitsToTheOutput()
Enumerate all compile units and put their data into the output stream.
Error link() override
Link debug info for added files.
void assignOffsetsToSections()
Enumerate all compile units and assign offsets to their sections.
Error validateAndUpdateOptions()
Validate specified options.
void forEachCompileAndTypeUnit(function_ref< void(DwarfUnit *CU)> UnitHandler)
Enumerates all compile and type units.
void cleanupDataAfterDWARFOutputIsWritten()
Cleanup data(string pools) after output sections are generated.
void assignOffsets()
Enumerate all compile units and assign offsets to their sections and strings.
void patchOffsetsAndSizes()
Enumerates all patches and update them with the correct values.
void assignOffsetsToStrings()
Enumerate all compile units and assign offsets to their strings.
std::function< void(const DWARFFile &File, llvm::StringRef Output)> InputVerificationHandlerTy
Definition: DWARFLinker.h:133
std::map< std::string, std::string > SwiftInterfacesMapTy
Definition: DWARFLinker.h:137
std::function< ErrorOr< DWARFFile & >(StringRef ContainerName, StringRef Path)> ObjFileLoaderTy
Definition: DWARFLinker.h:132
std::function< void(const Twine &Warning, StringRef Context, const DWARFDie *DIE)> MessageHandlerTy
Definition: DWARFLinker.h:130
std::map< std::string, std::string > ObjectPrefixMapTy
Definition: DWARFLinker.h:134
std::function< StringRef(StringRef)> TranslatorFuncTy
Definition: DWARFLinker.h:136
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).
ExtraDwarfEmitter allows adding extra data to the DWARFLinker output.
Definition: DWARFLinker.h:92
This class keeps data and services common for the whole linking process.
const DWARFLinkerOptions & getOptions() const
Returns linking options.
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.
An abstract base class for streams implementations that also support a pwrite operation.
Definition: raw_ostream.h:428
std::atomic< size_t > UniqueUnitID
Unique ID for compile unit.
OutputSections CommonSections
Common sections.
std::unique_ptr< DwarfEmitterImpl > TheDwarfEmitter
The emitter of final dwarf file.
uint64_t OverallNumberOfCU
Overall compile units number.
StringEntryToDwarfStringPoolEntryMap DebugLineStrStrings
DwarfStringPoolEntries for .debug_line_str section.
SmallVector< std::unique_ptr< LinkContext > > ObjectContexts
Keeps all linking contexts.
std::unique_ptr< TypeUnit > ArtificialTypeUnit
Type unit.
LinkingGlobalData GlobalData
Data global for the whole linking process.
StringEntryToDwarfStringPoolEntryMap DebugStrStrings
StringMap< uint64_t > ClangModules
Mapping the PCM filename to the DwoId.
void setEstimatedObjfilesAmount(unsigned ObjFilesNum) override
Set estimated objects files amount, for preliminary data allocation.
void setSwiftInterfacesMap(SwiftInterfacesMapTy *Map) override
Set map for Swift interfaces.
void setUpdateIndexTablesOnly(bool UpdateIndexTablesOnly) override
Update index tables only(do not modify rest of DWARF).
void setVerifyInputDWARF(bool Verify) override
Verify the input DWARF.
void setInputVerificationHandler(InputVerificationHandlerTy Handler) override
Set verification handler which would be used to report verification errors.
void setVerbosity(bool Verbose) override
Allows to generate log of linking process to the standard output.
void setAllowNonDeterministicOutput(bool AllowNonDeterministicOutput) override
Allow generating valid, but non-deterministic output.
void addAccelTableKind(AccelTableKind Kind) override
Add kind of accelerator tables to be generated.
void setStatistics(bool Statistics) override
Print statistics to standard output.
void setNumThreads(unsigned NumThreads) override
Use specified number of threads for parallel files linking.
void setPrependPath(const std::string &Ppath) override
Set prepend path for clang modules.
void setNoODR(bool NoODR) override
Do not unique types according to ODR.
void setKeepFunctionForStatic(bool KeepFunctionForStatic) override
Set to keep the enclosing function for a static variable.
void setObjectPrefixMap(ObjectPrefixMapTy *Map) override
Set prefix map for objects.
Error setTargetDWARFVersion(uint16_t TargetDWARFVersion) override
Set target DWARF version.
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
@ Offset
Definition: DWP.cpp:440
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:1958
Error createStringError(std::error_code EC, char const *Fmt, const Ts &... Vals)
Create formatted StringError object.
Definition: Error.h:1244
AccelTableKind
The kind of accelerator tables we should emit.
Definition: DwarfDebug.h:343
@ Other
Any other memory.
bool is_contained(R &&Range, const E &Element)
Returns true if Element is found in Range.
Definition: STLExtras.h:1883
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.
void linkSingleCompileUnit(CompileUnit &CU, TypeUnit *ArtificialTypeUnit, enum CompileUnit::Stage DoUntilStage=CompileUnit::Stage::Cleaned)
Link specified compile unit until specified stage.
UnitListTy CompileUnits
Set of Compilation Units(may be accessed asynchroniously for reading).
ModuleUnitListTy ModulesCompileUnits
Set of Compile Units for modules.
void emitFDE(uint32_t CIEOffset, uint32_t AddrSize, uint64_t Address, StringRef FDEBytes, SectionDescriptor &Section)
Emit FDE record.
bool InterCUProcessingStarted
Flag indicating that all inter-connected units are loaded and the dwarf linking process for these uni...
uint64_t getInputDebugInfoSize() const
Computes the total size of the debug info.
Error cloneAndEmitDebugFrame()
Clone and emit .debug_frame.
void addModulesCompileUnit(RefModuleUnit &&Unit)
Add Compile Unit corresponding to the module.
std::atomic< size_t > & UniqueUnitID
Counter for compile units ID.
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 ...
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...
uint64_t OriginalDebugInfoSize
Size of Debug info before optimizing.
std::atomic< bool > HasNewInterconnectedCUs
Flag indicating that new inter-connected compilation units were discovered.
std::function< CompileUnit *(uint64_t)> getUnitForOffset
std::pair< bool, bool > isClangModuleRef(const DWARFDie &CUDie, std::string &PCMFile, unsigned Indent, bool Quiet)
Check whether specified CUDie is a Clang module reference.
uint16_t TargetDWARFVersion
DWARF version for the output.
std::string PrependPath
Prepend path for the clang modules.
bool Verbose
Generate processing log to the standard output.
bool KeepFunctionForStatic
Whether we want a static variable to force us to keep its enclosing function.
SmallVector< DWARFLinker::AccelTableKind, 1 > AccelTables
The accelerator table kinds.
bool AllowNonDeterministicOutput
Allow to generate valid, but non deterministic output.
bool NoODR
Do not unique types according to ODR.
DWARFLinker::InputVerificationHandlerTy InputVerificationHandler
input verification handler(it might be called asynchronously).
DWARFLinker::SwiftInterfacesMapTy * ParseableSwiftInterfaces
A list of all .swiftinterface files referenced by the debug info, mapping Module name to path on disk...
DWARFLinker::ObjectPrefixMapTy * ObjectPrefixMap
A list of remappings to apply to file paths.
This structure is used to keep data of the concrete section.