xref: /freebsd/contrib/llvm-project/llvm/include/llvm/DWARFLinker/DWARFLinkerBase.h (revision 700637cbb5e582861067a11aaca4d053546871d2)
1 //===- DWARFLinkerBase.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_DWARFLINKER_DWARFLINKERBASE_H
10 #define LLVM_DWARFLINKER_DWARFLINKERBASE_H
11 #include "AddressesMap.h"
12 #include "DWARFFile.h"
13 #include "llvm/ADT/AddressRanges.h"
14 #include "llvm/ADT/DenseMap.h"
15 #include "llvm/DebugInfo/DWARF/DWARFContext.h"
16 #include "llvm/DebugInfo/DWARF/DWARFDebugLine.h"
17 #include "llvm/DebugInfo/DWARF/DWARFDebugRangeList.h"
18 #include "llvm/DebugInfo/DWARF/DWARFDie.h"
19 #include "llvm/DebugInfo/DWARF/LowLevel/DWARFExpression.h"
20 #include "llvm/Support/Compiler.h"
21 #include <map>
22 namespace llvm {
23 class DWARFUnit;
24 
25 namespace dwarf_linker {
26 
27 /// List of tracked debug tables.
28 enum class DebugSectionKind : uint8_t {
29   DebugInfo = 0,
30   DebugLine,
31   DebugFrame,
32   DebugRange,
33   DebugRngLists,
34   DebugLoc,
35   DebugLocLists,
36   DebugARanges,
37   DebugAbbrev,
38   DebugMacinfo,
39   DebugMacro,
40   DebugAddr,
41   DebugStr,
42   DebugLineStr,
43   DebugStrOffsets,
44   DebugPubNames,
45   DebugPubTypes,
46   DebugNames,
47   AppleNames,
48   AppleNamespaces,
49   AppleObjC,
50   AppleTypes,
51   NumberOfEnumEntries // must be last
52 };
53 
54 static constexpr size_t SectionKindsNum =
55     static_cast<size_t>(DebugSectionKind::NumberOfEnumEntries);
56 
57 static constexpr StringLiteral SectionNames[SectionKindsNum] = {
58     "debug_info",     "debug_line",     "debug_frame",       "debug_ranges",
59     "debug_rnglists", "debug_loc",      "debug_loclists",    "debug_aranges",
60     "debug_abbrev",   "debug_macinfo",  "debug_macro",       "debug_addr",
61     "debug_str",      "debug_line_str", "debug_str_offsets", "debug_pubnames",
62     "debug_pubtypes", "debug_names",    "apple_names",       "apple_namespac",
63     "apple_objc",     "apple_types"};
64 
65 /// Return the name of the section.
66 static constexpr const StringLiteral &
getSectionName(DebugSectionKind SectionKind)67 getSectionName(DebugSectionKind SectionKind) {
68   return SectionNames[static_cast<uint8_t>(SectionKind)];
69 }
70 
71 /// Recognise the table name and match it with the DebugSectionKind.
72 LLVM_ABI std::optional<DebugSectionKind> parseDebugTableName(StringRef Name);
73 
74 /// The base interface for DWARFLinker implementations.
75 class DWARFLinkerBase {
76 public:
77   virtual ~DWARFLinkerBase() = default;
78   using MessageHandlerTy = std::function<void(
79       const Twine &Warning, StringRef Context, const DWARFDie *DIE)>;
80   using ObjFileLoaderTy = std::function<ErrorOr<DWARFFile &>(
81       StringRef ContainerName, StringRef Path)>;
82   using InputVerificationHandlerTy =
83       std::function<void(const DWARFFile &File, llvm::StringRef Output)>;
84   using ObjectPrefixMapTy = std::map<std::string, std::string>;
85   using CompileUnitHandlerTy = function_ref<void(const DWARFUnit &Unit)>;
86   using SwiftInterfacesMapTy = std::map<std::string, std::string>;
87   /// Type of output file.
88   enum class OutputFileType : uint8_t {
89     Object,
90     Assembly,
91   };
92   /// The kind of accelerator tables to be emitted.
93   enum class AccelTableKind : uint8_t {
94     Apple,     ///< .apple_names, .apple_namespaces, .apple_types, .apple_objc.
95     Pub,       ///< .debug_pubnames, .debug_pubtypes
96     DebugNames ///< .debug_names.
97   };
98   /// Add an object file to be linked. Pre-load compile unit die. Call
99   /// \p OnCUDieLoaded for each compile unit die. If \p File has reference to
100   /// a Clang module and UpdateIndexTablesOnly == false then the module is be
101   /// pre-loaded by \p Loader.
102   ///
103   /// \pre a call to setNoODR(true) and/or setUpdateIndexTablesOnly(bool Update)
104   ///      must be made when required.
105   virtual void addObjectFile(
106       DWARFFile &File, ObjFileLoaderTy Loader = nullptr,
107       CompileUnitHandlerTy OnCUDieLoaded = [](const DWARFUnit &) {}) = 0;
108   /// Link the debug info for all object files added through calls to
109   /// addObjectFile.
110   virtual Error link() = 0;
111   /// A number of methods setting various linking options:
112   /// Enable logging to standard output.
113   virtual void setVerbosity(bool Verbose) = 0;
114   /// Print statistics to standard output.
115   virtual void setStatistics(bool Statistics) = 0;
116   /// Verify the input DWARF.
117   virtual void setVerifyInputDWARF(bool Verify) = 0;
118   /// Do not unique types according to ODR.
119   virtual void setNoODR(bool NoODR) = 0;
120   /// Update index tables only (do not modify rest of DWARF).
121   virtual void setUpdateIndexTablesOnly(bool Update) = 0;
122   /// Allows generating non-deterministic output in exchange for more
123   /// parallelism.
124   virtual void setAllowNonDeterministicOutput(bool) = 0;
125   /// Set whether to keep the enclosing function for a static variable.
126   virtual void setKeepFunctionForStatic(bool KeepFunctionForStatic) = 0;
127   /// Use specified number of threads for parallel files linking.
128   virtual void setNumThreads(unsigned NumThreads) = 0;
129   /// Add kind of accelerator tables to be generated.
130   virtual void addAccelTableKind(AccelTableKind Kind) = 0;
131   /// Set prepend path for clang modules.
132   virtual void setPrependPath(StringRef Ppath) = 0;
133   /// Set estimated objects files amount, for preliminary data allocation.
134   virtual void setEstimatedObjfilesAmount(unsigned ObjFilesNum) = 0;
135   /// Set verification handler used to report verification errors.
136   virtual void
137   setInputVerificationHandler(InputVerificationHandlerTy Handler) = 0;
138   /// Set map for Swift interfaces.
139   virtual void setSwiftInterfacesMap(SwiftInterfacesMapTy *Map) = 0;
140   /// Set prefix map for objects.
141   virtual void setObjectPrefixMap(ObjectPrefixMapTy *Map) = 0;
142   /// Set target DWARF version.
143   virtual Error setTargetDWARFVersion(uint16_t TargetDWARFVersion) = 0;
144 };
145 } // end namespace dwarf_linker
146 } // end namespace llvm
147 #endif // LLVM_DWARFLINKER_DWARFLINKERBASE_H
148