1 //===- tools/dsymutil/CompileUnit.h - Dwarf debug info linker ---*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "llvm/ADT/IntervalMap.h"
11 #include "llvm/CodeGen/DIE.h"
12 #include "llvm/DebugInfo/DWARF/DWARFUnit.h"
14 #ifndef LLVM_TOOLS_DSYMUTIL_COMPILEUNIT_H
15 #define LLVM_TOOLS_DSYMUTIL_COMPILEUNIT_H
22 template <typename KeyT, typename ValT>
23 using HalfOpenIntervalMap =
24 IntervalMap<KeyT, ValT, IntervalMapImpl::NodeSizer<KeyT, ValT>::LeafSize,
25 IntervalMapHalfOpenInfo<KeyT>>;
27 using FunctionIntervals = HalfOpenIntervalMap<uint64_t, int64_t>;
29 // FIXME: Delete this structure.
30 struct PatchLocation {
31 DIE::value_iterator I;
33 PatchLocation() = default;
34 PatchLocation(DIE::value_iterator I) : I(I) {}
36 void set(uint64_t New) const {
39 assert(Old.getType() == DIEValue::isInteger);
40 *I = DIEValue(Old.getAttribute(), Old.getForm(), DIEInteger(New));
43 uint64_t get() const {
45 return I->getDIEInteger().getValue();
49 /// Stores all information relating to a compile unit, be it in its original
50 /// instance in the object file to its brand new cloned and linked DIE tree.
53 /// Information gathered about a DIE in the object file.
55 /// Address offset to apply to the described entity.
58 /// ODR Declaration context.
61 /// Cloned version of that DIE.
64 /// The index of this DIE's parent.
67 /// Is the DIE part of the linked output?
70 /// Was this DIE's entity found in the map?
73 /// Is this a pure forward declaration we can strip?
76 /// Does DIE transitively refer an incomplete decl?
80 CompileUnit(DWARFUnit &OrigUnit, unsigned ID, bool CanUseODR,
81 StringRef ClangModuleName)
82 : OrigUnit(OrigUnit), ID(ID), Ranges(RangeAlloc),
83 ClangModuleName(ClangModuleName) {
84 Info.resize(OrigUnit.getNumDIEs());
86 auto CUDie = OrigUnit.getUnitDIE(false);
91 if (auto Lang = dwarf::toUnsigned(CUDie.find(dwarf::DW_AT_language)))
92 HasODR = CanUseODR && (*Lang == dwarf::DW_LANG_C_plus_plus ||
93 *Lang == dwarf::DW_LANG_C_plus_plus_03 ||
94 *Lang == dwarf::DW_LANG_C_plus_plus_11 ||
95 *Lang == dwarf::DW_LANG_C_plus_plus_14 ||
96 *Lang == dwarf::DW_LANG_ObjC_plus_plus);
101 DWARFUnit &getOrigUnit() const { return OrigUnit; }
103 unsigned getUniqueID() const { return ID; }
105 void createOutputDIE() {
106 NewUnit.emplace(OrigUnit.getVersion(), OrigUnit.getAddressByteSize(),
107 OrigUnit.getUnitDIE().getTag());
110 DIE *getOutputUnitDIE() const {
112 return &const_cast<BasicDIEUnit &>(*NewUnit).getUnitDie();
116 bool hasODR() const { return HasODR; }
117 bool isClangModule() const { return !ClangModuleName.empty(); }
118 const std::string &getClangModuleName() const { return ClangModuleName; }
120 DIEInfo &getInfo(unsigned Idx) { return Info[Idx]; }
121 const DIEInfo &getInfo(unsigned Idx) const { return Info[Idx]; }
123 uint64_t getStartOffset() const { return StartOffset; }
124 uint64_t getNextUnitOffset() const { return NextUnitOffset; }
125 void setStartOffset(uint64_t DebugInfoSize) { StartOffset = DebugInfoSize; }
127 uint64_t getLowPc() const { return LowPc; }
128 uint64_t getHighPc() const { return HighPc; }
129 bool hasLabelAt(uint64_t Addr) const { return Labels.count(Addr); }
131 Optional<PatchLocation> getUnitRangesAttribute() const {
132 return UnitRangeAttribute;
135 const FunctionIntervals &getFunctionRanges() const { return Ranges; }
137 const std::vector<PatchLocation> &getRangesAttributes() const {
138 return RangeAttributes;
141 const std::vector<std::pair<PatchLocation, int64_t>> &
142 getLocationAttributes() const {
143 return LocationAttributes;
146 void setHasInterestingContent() { HasInterestingContent = true; }
147 bool hasInterestingContent() { return HasInterestingContent; }
149 /// Mark every DIE in this unit as kept. This function also
150 /// marks variables as InDebugMap so that they appear in the
151 /// reconstructed accelerator tables.
152 void markEverythingAsKept();
154 /// Compute the end offset for this unit. Must be called after the CU's DIEs
155 /// have been cloned. \returns the next unit offset (which is also the
156 /// current debug_info section size).
157 uint64_t computeNextUnitOffset();
159 /// Keep track of a forward reference to DIE \p Die in \p RefUnit by \p
160 /// Attr. The attribute should be fixed up later to point to the absolute
161 /// offset of \p Die in the debug_info section or to the canonical offset of
162 /// \p Ctxt if it is non-null.
163 void noteForwardReference(DIE *Die, const CompileUnit *RefUnit,
164 DeclContext *Ctxt, PatchLocation Attr);
166 /// Apply all fixups recorded by noteForwardReference().
167 void fixupForwardReferences();
169 /// Add the low_pc of a label that is relocated by applying
170 /// offset \p PCOffset.
171 void addLabelLowPc(uint64_t LabelLowPc, int64_t PcOffset);
173 /// Add a function range [\p LowPC, \p HighPC) that is relocated by applying
174 /// offset \p PCOffset.
175 void addFunctionRange(uint64_t LowPC, uint64_t HighPC, int64_t PCOffset);
177 /// Keep track of a DW_AT_range attribute that we will need to patch up later.
178 void noteRangeAttribute(const DIE &Die, PatchLocation Attr);
180 /// Keep track of a location attribute pointing to a location list in the
181 /// debug_loc section.
182 void noteLocationAttribute(PatchLocation Attr, int64_t PcOffset);
184 /// Add a name accelerator entry for \a Die with \a Name.
185 void addNamespaceAccelerator(const DIE *Die, DwarfStringPoolEntryRef Name);
187 /// Add a name accelerator entry for \a Die with \a Name.
188 void addNameAccelerator(const DIE *Die, DwarfStringPoolEntryRef Name,
189 bool SkipPubnamesSection = false);
191 /// Add various accelerator entries for \p Die with \p Name which is stored
192 /// in the string table at \p Offset. \p Name must be an Objective-C
194 void addObjCAccelerator(const DIE *Die, DwarfStringPoolEntryRef Name,
195 bool SkipPubnamesSection = false);
197 /// Add a type accelerator entry for \p Die with \p Name which is stored in
198 /// the string table at \p Offset.
199 void addTypeAccelerator(const DIE *Die, DwarfStringPoolEntryRef Name,
200 bool ObjcClassImplementation,
201 uint32_t QualifiedNameHash);
204 /// Name of the entry.
205 DwarfStringPoolEntryRef Name;
207 /// DIE this entry describes.
210 /// Hash of the fully qualified name.
211 uint32_t QualifiedNameHash;
213 /// Emit this entry only in the apple_* sections.
216 /// Is this an ObjC class implementation?
217 bool ObjcClassImplementation;
219 AccelInfo(DwarfStringPoolEntryRef Name, const DIE *Die,
220 bool SkipPubSection = false)
221 : Name(Name), Die(Die), SkipPubSection(SkipPubSection) {}
223 AccelInfo(DwarfStringPoolEntryRef Name, const DIE *Die,
224 uint32_t QualifiedNameHash, bool ObjCClassIsImplementation)
225 : Name(Name), Die(Die), QualifiedNameHash(QualifiedNameHash),
226 SkipPubSection(false),
227 ObjcClassImplementation(ObjCClassIsImplementation) {}
230 const std::vector<AccelInfo> &getPubnames() const { return Pubnames; }
231 const std::vector<AccelInfo> &getPubtypes() const { return Pubtypes; }
232 const std::vector<AccelInfo> &getNamespaces() const { return Namespaces; }
233 const std::vector<AccelInfo> &getObjC() const { return ObjC; }
235 /// Get the full path for file \a FileNum in the line table
236 StringRef getResolvedPath(unsigned FileNum) {
237 if (FileNum >= ResolvedPaths.size())
239 return ResolvedPaths[FileNum];
242 /// Set the fully resolved path for the line-table's file \a FileNum
244 void setResolvedPath(unsigned FileNum, StringRef Path) {
245 if (ResolvedPaths.size() <= FileNum)
246 ResolvedPaths.resize(FileNum + 1);
247 ResolvedPaths[FileNum] = Path;
253 std::vector<DIEInfo> Info; ///< DIE info indexed by DIE index.
254 Optional<BasicDIEUnit> NewUnit;
256 uint64_t StartOffset;
257 uint64_t NextUnitOffset;
259 uint64_t LowPc = std::numeric_limits<uint64_t>::max();
262 /// A list of attributes to fixup with the absolute offset of
263 /// a DIE in the debug_info section.
265 /// The offsets for the attributes in this array couldn't be set while
266 /// cloning because for cross-cu forward references the target DIE's offset
267 /// isn't known you emit the reference attribute.
269 std::tuple<DIE *, const CompileUnit *, DeclContext *, PatchLocation>>
270 ForwardDIEReferences;
272 FunctionIntervals::Allocator RangeAlloc;
274 /// The ranges in that interval map are the PC ranges for
275 /// functions in this unit, associated with the PC offset to apply
276 /// to the addresses to get the linked address.
277 FunctionIntervals Ranges;
279 /// The DW_AT_low_pc of each DW_TAG_label.
280 SmallDenseMap<uint64_t, uint64_t, 1> Labels;
282 /// DW_AT_ranges attributes to patch after we have gathered
283 /// all the unit's function addresses.
285 std::vector<PatchLocation> RangeAttributes;
286 Optional<PatchLocation> UnitRangeAttribute;
289 /// Location attributes that need to be transferred from the
290 /// original debug_loc section to the liked one. They are stored
291 /// along with the PC offset that is to be applied to their
292 /// function's address.
293 std::vector<std::pair<PatchLocation, int64_t>> LocationAttributes;
295 /// Accelerator entries for the unit, both for the pub*
296 /// sections and the apple* ones.
298 std::vector<AccelInfo> Pubnames;
299 std::vector<AccelInfo> Pubtypes;
300 std::vector<AccelInfo> Namespaces;
301 std::vector<AccelInfo> ObjC;
304 /// Cached resolved paths from the line table.
305 /// Note, the StringRefs here point in to the intern (uniquing) string pool.
306 /// This means that a StringRef returned here doesn't need to then be uniqued
307 /// for the purposes of getting a unique address for each string.
308 std::vector<StringRef> ResolvedPaths;
310 /// Is this unit subject to the ODR rule?
313 /// Did a DIE actually contain a valid reloc?
314 bool HasInterestingContent;
316 /// If this is a Clang module, this holds the module's name.
317 std::string ClangModuleName;
320 } // end namespace dsymutil
321 } // end namespace llvm
323 #endif // LLVM_TOOLS_DSYMUTIL_COMPILEUNIT_H