1 //===- tools/dsymutil/CompileUnit.h - Dwarf debug info linker ---*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 #ifndef LLVM_TOOLS_DSYMUTIL_COMPILEUNIT_H
10 #define LLVM_TOOLS_DSYMUTIL_COMPILEUNIT_H
12 #include "llvm/ADT/IntervalMap.h"
13 #include "llvm/CodeGen/DIE.h"
14 #include "llvm/DebugInfo/DWARF/DWARFUnit.h"
21 template <typename KeyT
, typename ValT
>
22 using HalfOpenIntervalMap
=
23 IntervalMap
<KeyT
, ValT
, IntervalMapImpl::NodeSizer
<KeyT
, ValT
>::LeafSize
,
24 IntervalMapHalfOpenInfo
<KeyT
>>;
26 using FunctionIntervals
= HalfOpenIntervalMap
<uint64_t, int64_t>;
28 // FIXME: Delete this structure.
29 struct PatchLocation
{
30 DIE::value_iterator I
;
32 PatchLocation() = default;
33 PatchLocation(DIE::value_iterator I
) : I(I
) {}
35 void set(uint64_t New
) const {
38 assert(Old
.getType() == DIEValue::isInteger
);
39 *I
= DIEValue(Old
.getAttribute(), Old
.getForm(), DIEInteger(New
));
42 uint64_t get() const {
44 return I
->getDIEInteger().getValue();
48 /// Stores all information relating to a compile unit, be it in its original
49 /// instance in the object file to its brand new cloned and linked DIE tree.
52 /// Information gathered about a DIE in the object file.
54 /// Address offset to apply to the described entity.
57 /// ODR Declaration context.
60 /// Cloned version of that DIE.
63 /// The index of this DIE's parent.
66 /// Is the DIE part of the linked output?
69 /// Was this DIE's entity found in the map?
72 /// Is this a pure forward declaration we can strip?
75 /// Does DIE transitively refer an incomplete decl?
79 CompileUnit(DWARFUnit
&OrigUnit
, unsigned ID
, bool CanUseODR
,
80 StringRef ClangModuleName
)
81 : OrigUnit(OrigUnit
), ID(ID
), Ranges(RangeAlloc
),
82 ClangModuleName(ClangModuleName
) {
83 Info
.resize(OrigUnit
.getNumDIEs());
85 auto CUDie
= OrigUnit
.getUnitDIE(false);
90 if (auto Lang
= dwarf::toUnsigned(CUDie
.find(dwarf::DW_AT_language
)))
91 HasODR
= CanUseODR
&& (*Lang
== dwarf::DW_LANG_C_plus_plus
||
92 *Lang
== dwarf::DW_LANG_C_plus_plus_03
||
93 *Lang
== dwarf::DW_LANG_C_plus_plus_11
||
94 *Lang
== dwarf::DW_LANG_C_plus_plus_14
||
95 *Lang
== dwarf::DW_LANG_ObjC_plus_plus
);
100 DWARFUnit
&getOrigUnit() const { return OrigUnit
; }
102 unsigned getUniqueID() const { return ID
; }
104 void createOutputDIE() {
105 NewUnit
.emplace(OrigUnit
.getVersion(), OrigUnit
.getAddressByteSize(),
106 OrigUnit
.getUnitDIE().getTag());
109 DIE
*getOutputUnitDIE() const {
111 return &const_cast<BasicDIEUnit
&>(*NewUnit
).getUnitDie();
115 bool hasODR() const { return HasODR
; }
116 bool isClangModule() const { return !ClangModuleName
.empty(); }
117 const std::string
&getClangModuleName() const { return ClangModuleName
; }
119 DIEInfo
&getInfo(unsigned Idx
) { return Info
[Idx
]; }
120 const DIEInfo
&getInfo(unsigned Idx
) const { return Info
[Idx
]; }
122 uint64_t getStartOffset() const { return StartOffset
; }
123 uint64_t getNextUnitOffset() const { return NextUnitOffset
; }
124 void setStartOffset(uint64_t DebugInfoSize
) { StartOffset
= DebugInfoSize
; }
126 uint64_t getLowPc() const { return LowPc
; }
127 uint64_t getHighPc() const { return HighPc
; }
128 bool hasLabelAt(uint64_t Addr
) const { return Labels
.count(Addr
); }
130 Optional
<PatchLocation
> getUnitRangesAttribute() const {
131 return UnitRangeAttribute
;
134 const FunctionIntervals
&getFunctionRanges() const { return Ranges
; }
136 const std::vector
<PatchLocation
> &getRangesAttributes() const {
137 return RangeAttributes
;
140 const std::vector
<std::pair
<PatchLocation
, int64_t>> &
141 getLocationAttributes() const {
142 return LocationAttributes
;
145 void setHasInterestingContent() { HasInterestingContent
= true; }
146 bool hasInterestingContent() { return HasInterestingContent
; }
148 /// Mark every DIE in this unit as kept. This function also
149 /// marks variables as InDebugMap so that they appear in the
150 /// reconstructed accelerator tables.
151 void markEverythingAsKept();
153 /// Compute the end offset for this unit. Must be called after the CU's DIEs
154 /// have been cloned. \returns the next unit offset (which is also the
155 /// current debug_info section size).
156 uint64_t computeNextUnitOffset();
158 /// Keep track of a forward reference to DIE \p Die in \p RefUnit by \p
159 /// Attr. The attribute should be fixed up later to point to the absolute
160 /// offset of \p Die in the debug_info section or to the canonical offset of
161 /// \p Ctxt if it is non-null.
162 void noteForwardReference(DIE
*Die
, const CompileUnit
*RefUnit
,
163 DeclContext
*Ctxt
, PatchLocation Attr
);
165 /// Apply all fixups recorded by noteForwardReference().
166 void fixupForwardReferences();
168 /// Add the low_pc of a label that is relocated by applying
169 /// offset \p PCOffset.
170 void addLabelLowPc(uint64_t LabelLowPc
, int64_t PcOffset
);
172 /// Add a function range [\p LowPC, \p HighPC) that is relocated by applying
173 /// offset \p PCOffset.
174 void addFunctionRange(uint64_t LowPC
, uint64_t HighPC
, int64_t PCOffset
);
176 /// Keep track of a DW_AT_range attribute that we will need to patch up later.
177 void noteRangeAttribute(const DIE
&Die
, PatchLocation Attr
);
179 /// Keep track of a location attribute pointing to a location list in the
180 /// debug_loc section.
181 void noteLocationAttribute(PatchLocation Attr
, int64_t PcOffset
);
183 /// Add a name accelerator entry for \a Die with \a Name.
184 void addNamespaceAccelerator(const DIE
*Die
, DwarfStringPoolEntryRef Name
);
186 /// Add a name accelerator entry for \a Die with \a Name.
187 void addNameAccelerator(const DIE
*Die
, DwarfStringPoolEntryRef Name
,
188 bool SkipPubnamesSection
= false);
190 /// Add various accelerator entries for \p Die with \p Name which is stored
191 /// in the string table at \p Offset. \p Name must be an Objective-C
193 void addObjCAccelerator(const DIE
*Die
, DwarfStringPoolEntryRef Name
,
194 bool SkipPubnamesSection
= false);
196 /// Add a type accelerator entry for \p Die with \p Name which is stored in
197 /// the string table at \p Offset.
198 void addTypeAccelerator(const DIE
*Die
, DwarfStringPoolEntryRef Name
,
199 bool ObjcClassImplementation
,
200 uint32_t QualifiedNameHash
);
203 /// Name of the entry.
204 DwarfStringPoolEntryRef Name
;
206 /// DIE this entry describes.
209 /// Hash of the fully qualified name.
210 uint32_t QualifiedNameHash
;
212 /// Emit this entry only in the apple_* sections.
215 /// Is this an ObjC class implementation?
216 bool ObjcClassImplementation
;
218 AccelInfo(DwarfStringPoolEntryRef Name
, const DIE
*Die
,
219 bool SkipPubSection
= false)
220 : Name(Name
), Die(Die
), SkipPubSection(SkipPubSection
) {}
222 AccelInfo(DwarfStringPoolEntryRef Name
, const DIE
*Die
,
223 uint32_t QualifiedNameHash
, bool ObjCClassIsImplementation
)
224 : Name(Name
), Die(Die
), QualifiedNameHash(QualifiedNameHash
),
225 SkipPubSection(false),
226 ObjcClassImplementation(ObjCClassIsImplementation
) {}
229 const std::vector
<AccelInfo
> &getPubnames() const { return Pubnames
; }
230 const std::vector
<AccelInfo
> &getPubtypes() const { return Pubtypes
; }
231 const std::vector
<AccelInfo
> &getNamespaces() const { return Namespaces
; }
232 const std::vector
<AccelInfo
> &getObjC() const { return ObjC
; }
234 /// Get the full path for file \a FileNum in the line table
235 StringRef
getResolvedPath(unsigned FileNum
) {
236 if (FileNum
>= ResolvedPaths
.size())
238 return ResolvedPaths
[FileNum
];
241 /// Set the fully resolved path for the line-table's file \a FileNum
243 void setResolvedPath(unsigned FileNum
, StringRef Path
) {
244 if (ResolvedPaths
.size() <= FileNum
)
245 ResolvedPaths
.resize(FileNum
+ 1);
246 ResolvedPaths
[FileNum
] = Path
;
249 MCSymbol
*getLabelBegin() { return LabelBegin
; }
250 void setLabelBegin(MCSymbol
*S
) { LabelBegin
= S
; }
255 std::vector
<DIEInfo
> Info
; ///< DIE info indexed by DIE index.
256 Optional
<BasicDIEUnit
> NewUnit
;
257 MCSymbol
*LabelBegin
= nullptr;
259 uint64_t StartOffset
;
260 uint64_t NextUnitOffset
;
262 uint64_t LowPc
= std::numeric_limits
<uint64_t>::max();
265 /// A list of attributes to fixup with the absolute offset of
266 /// a DIE in the debug_info section.
268 /// The offsets for the attributes in this array couldn't be set while
269 /// cloning because for cross-cu forward references the target DIE's offset
270 /// isn't known you emit the reference attribute.
272 std::tuple
<DIE
*, const CompileUnit
*, DeclContext
*, PatchLocation
>>
273 ForwardDIEReferences
;
275 FunctionIntervals::Allocator RangeAlloc
;
277 /// The ranges in that interval map are the PC ranges for
278 /// functions in this unit, associated with the PC offset to apply
279 /// to the addresses to get the linked address.
280 FunctionIntervals Ranges
;
282 /// The DW_AT_low_pc of each DW_TAG_label.
283 SmallDenseMap
<uint64_t, uint64_t, 1> Labels
;
285 /// DW_AT_ranges attributes to patch after we have gathered
286 /// all the unit's function addresses.
288 std::vector
<PatchLocation
> RangeAttributes
;
289 Optional
<PatchLocation
> UnitRangeAttribute
;
292 /// Location attributes that need to be transferred from the
293 /// original debug_loc section to the liked one. They are stored
294 /// along with the PC offset that is to be applied to their
295 /// function's address.
296 std::vector
<std::pair
<PatchLocation
, int64_t>> LocationAttributes
;
298 /// Accelerator entries for the unit, both for the pub*
299 /// sections and the apple* ones.
301 std::vector
<AccelInfo
> Pubnames
;
302 std::vector
<AccelInfo
> Pubtypes
;
303 std::vector
<AccelInfo
> Namespaces
;
304 std::vector
<AccelInfo
> ObjC
;
307 /// Cached resolved paths from the line table.
308 /// Note, the StringRefs here point in to the intern (uniquing) string pool.
309 /// This means that a StringRef returned here doesn't need to then be uniqued
310 /// for the purposes of getting a unique address for each string.
311 std::vector
<StringRef
> ResolvedPaths
;
313 /// Is this unit subject to the ODR rule?
316 /// Did a DIE actually contain a valid reloc?
317 bool HasInterestingContent
;
319 /// If this is a Clang module, this holds the module's name.
320 std::string ClangModuleName
;
323 } // end namespace dsymutil
324 } // end namespace llvm
326 #endif // LLVM_TOOLS_DSYMUTIL_COMPILEUNIT_H