Bug Summary

File:tools/dsymutil/DwarfLinker.cpp
Warning:line 2860, column 5
Called C++ object pointer is null

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -triple x86_64-pc-linux-gnu -analyze -disable-free -disable-llvm-verifier -discard-value-names -main-file-name DwarfLinker.cpp -analyzer-store=region -analyzer-opt-analyze-nested-blocks -analyzer-eagerly-assume -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=cplusplus -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -mrelocation-model pic -pic-level 2 -mthread-model posix -fmath-errno -masm-verbose -mconstructor-aliases -munwind-tables -fuse-init-array -target-cpu x86-64 -dwarf-column-info -debugger-tuning=gdb -momit-leaf-frame-pointer -ffunction-sections -fdata-sections -resource-dir /usr/lib/llvm-7/lib/clang/7.0.0 -D _DEBUG -D _GNU_SOURCE -D __STDC_CONSTANT_MACROS -D __STDC_FORMAT_MACROS -D __STDC_LIMIT_MACROS -I /build/llvm-toolchain-snapshot-7~svn326246/build-llvm/tools/dsymutil -I /build/llvm-toolchain-snapshot-7~svn326246/tools/dsymutil -I /build/llvm-toolchain-snapshot-7~svn326246/build-llvm/include -I /build/llvm-toolchain-snapshot-7~svn326246/include -U NDEBUG -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/7.3.0/../../../../include/c++/7.3.0 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/7.3.0/../../../../include/x86_64-linux-gnu/c++/7.3.0 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/7.3.0/../../../../include/x86_64-linux-gnu/c++/7.3.0 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/7.3.0/../../../../include/c++/7.3.0/backward -internal-isystem /usr/include/clang/7.0.0/include/ -internal-isystem /usr/local/include -internal-isystem /usr/lib/llvm-7/lib/clang/7.0.0/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -O2 -Wno-unused-parameter -Wwrite-strings -Wno-missing-field-initializers -Wno-long-long -Wno-maybe-uninitialized -Wno-comment -std=c++11 -fdeprecated-macro -fdebug-compilation-dir /build/llvm-toolchain-snapshot-7~svn326246/build-llvm/tools/dsymutil -ferror-limit 19 -fmessage-length 0 -fvisibility-inlines-hidden -fobjc-runtime=gcc -fdiagnostics-show-option -vectorize-loops -vectorize-slp -analyzer-checker optin.performance.Padding -analyzer-output=html -analyzer-config stable-report-filename=true -o /tmp/scan-build-2018-02-28-041547-14988-1 -x c++ /build/llvm-toolchain-snapshot-7~svn326246/tools/dsymutil/DwarfLinker.cpp
1//===- tools/dsymutil/DwarfLinker.cpp - Dwarf debug info linker -----------===//
2//
3// The LLVM Linker
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "BinaryHolder.h"
11#include "DebugMap.h"
12#include "MachOUtils.h"
13#include "NonRelocatableStringpool.h"
14#include "dsymutil.h"
15#include "llvm/ADT/ArrayRef.h"
16#include "llvm/ADT/DenseMap.h"
17#include "llvm/ADT/DenseMapInfo.h"
18#include "llvm/ADT/DenseSet.h"
19#include "llvm/ADT/FoldingSet.h"
20#include "llvm/ADT/Hashing.h"
21#include "llvm/ADT/IntervalMap.h"
22#include "llvm/ADT/None.h"
23#include "llvm/ADT/Optional.h"
24#include "llvm/ADT/PointerIntPair.h"
25#include "llvm/ADT/STLExtras.h"
26#include "llvm/ADT/SmallString.h"
27#include "llvm/ADT/StringMap.h"
28#include "llvm/ADT/StringRef.h"
29#include "llvm/ADT/Triple.h"
30#include "llvm/ADT/Twine.h"
31#include "llvm/BinaryFormat/Dwarf.h"
32#include "llvm/BinaryFormat/MachO.h"
33#include "llvm/CodeGen/AccelTable.h"
34#include "llvm/CodeGen/AsmPrinter.h"
35#include "llvm/CodeGen/DIE.h"
36#include "llvm/Config/config.h"
37#include "llvm/DebugInfo/DIContext.h"
38#include "llvm/DebugInfo/DWARF/DWARFAbbreviationDeclaration.h"
39#include "llvm/DebugInfo/DWARF/DWARFContext.h"
40#include "llvm/DebugInfo/DWARF/DWARFDataExtractor.h"
41#include "llvm/DebugInfo/DWARF/DWARFDebugLine.h"
42#include "llvm/DebugInfo/DWARF/DWARFDebugRangeList.h"
43#include "llvm/DebugInfo/DWARF/DWARFDie.h"
44#include "llvm/DebugInfo/DWARF/DWARFFormValue.h"
45#include "llvm/DebugInfo/DWARF/DWARFSection.h"
46#include "llvm/DebugInfo/DWARF/DWARFUnit.h"
47#include "llvm/MC/MCAsmBackend.h"
48#include "llvm/MC/MCAsmInfo.h"
49#include "llvm/MC/MCCodeEmitter.h"
50#include "llvm/MC/MCContext.h"
51#include "llvm/MC/MCDwarf.h"
52#include "llvm/MC/MCInstrInfo.h"
53#include "llvm/MC/MCObjectFileInfo.h"
54#include "llvm/MC/MCRegisterInfo.h"
55#include "llvm/MC/MCSection.h"
56#include "llvm/MC/MCStreamer.h"
57#include "llvm/MC/MCSubtargetInfo.h"
58#include "llvm/MC/MCTargetOptions.h"
59#include "llvm/MC/MCTargetOptionsCommandFlags.def"
60#include "llvm/Object/MachO.h"
61#include "llvm/Object/ObjectFile.h"
62#include "llvm/Object/SymbolicFile.h"
63#include "llvm/Support/Allocator.h"
64#include "llvm/Support/Casting.h"
65#include "llvm/Support/Compiler.h"
66#include "llvm/Support/DJB.h"
67#include "llvm/Support/DataExtractor.h"
68#include "llvm/Support/Error.h"
69#include "llvm/Support/ErrorHandling.h"
70#include "llvm/Support/ErrorOr.h"
71#include "llvm/Support/FileSystem.h"
72#include "llvm/Support/Format.h"
73#include "llvm/Support/LEB128.h"
74#include "llvm/Support/MathExtras.h"
75#include "llvm/Support/MemoryBuffer.h"
76#include "llvm/Support/Path.h"
77#include "llvm/Support/TargetRegistry.h"
78#include "llvm/Support/ToolOutputFile.h"
79#include "llvm/Support/raw_ostream.h"
80#include "llvm/Target/TargetMachine.h"
81#include "llvm/Target/TargetOptions.h"
82#include <algorithm>
83#include <cassert>
84#include <cinttypes>
85#include <climits>
86#include <cstdint>
87#include <cstdlib>
88#include <cstring>
89#include <limits>
90#include <map>
91#include <memory>
92#include <string>
93#include <system_error>
94#include <tuple>
95#include <utility>
96#include <vector>
97
98// For GNU Hurd
99#if defined(__GNU__) && !defined(PATH_MAX4096)
100# define PATH_MAX4096 4096
101#endif
102
103namespace llvm {
104namespace dsymutil {
105
106namespace {
107
108/// Small helper that resolves and caches file paths. This helps reduce the
109/// number of calls to realpath which is expensive. We assume the input are
110/// files, and cache the realpath of their parent. This way we can quickly
111/// resolve different files under the same path.
112class CachedPathResolver {
113public:
114 /// Resolve a path by calling realpath and cache its result. The returned
115 /// StringRef is interned in the given \p StringPool.
116 StringRef resolve(std::string Path, NonRelocatableStringpool &StringPool) {
117 StringRef FileName = sys::path::filename(Path);
118 SmallString<256> ParentPath = sys::path::parent_path(Path);
119
120 // If the ParentPath has not yet been resolved, resolve and cache it for
121 // future look-ups.
122 if (!ResolvedPaths.count(ParentPath)) {
123 SmallString<256> RealPath;
124 sys::fs::real_path(ParentPath, RealPath);
125 ResolvedPaths.insert({ParentPath, StringRef(RealPath).str()});
126 }
127
128 // Join the file name again with the resolved path.
129 SmallString<256> ResolvedPath(ResolvedPaths[ParentPath]);
130 sys::path::append(ResolvedPath, FileName);
131 return StringPool.internString(ResolvedPath);
132 }
133
134private:
135 StringMap<std::string> ResolvedPaths;
136};
137
138/// Retrieve the section named \a SecName in \a Obj.
139///
140/// To accommodate for platform discrepancies, the name passed should be
141/// (for example) 'debug_info' to match either '__debug_info' or '.debug_info'.
142/// This function will strip the initial platform-specific characters.
143static Optional<object::SectionRef>
144getSectionByName(const object::ObjectFile &Obj, StringRef SecName) {
145 for (const object::SectionRef &Section : Obj.sections()) {
146 StringRef SectionName;
147 Section.getName(SectionName);
148 SectionName = SectionName.substr(SectionName.find_first_not_of("._"));
149 if (SectionName != SecName)
150 continue;
151 return Section;
152 }
153 return None;
154}
155
156template <typename KeyT, typename ValT>
157using HalfOpenIntervalMap =
158 IntervalMap<KeyT, ValT, IntervalMapImpl::NodeSizer<KeyT, ValT>::LeafSize,
159 IntervalMapHalfOpenInfo<KeyT>>;
160
161using FunctionIntervals = HalfOpenIntervalMap<uint64_t, int64_t>;
162
163// FIXME: Delete this structure.
164struct PatchLocation {
165 DIE::value_iterator I;
166
167 PatchLocation() = default;
168 PatchLocation(DIE::value_iterator I) : I(I) {}
169
170 void set(uint64_t New) const {
171 assert(I)(static_cast <bool> (I) ? void (0) : __assert_fail ("I"
, "/build/llvm-toolchain-snapshot-7~svn326246/tools/dsymutil/DwarfLinker.cpp"
, 171, __extension__ __PRETTY_FUNCTION__))
;
172 const auto &Old = *I;
173 assert(Old.getType() == DIEValue::isInteger)(static_cast <bool> (Old.getType() == DIEValue::isInteger
) ? void (0) : __assert_fail ("Old.getType() == DIEValue::isInteger"
, "/build/llvm-toolchain-snapshot-7~svn326246/tools/dsymutil/DwarfLinker.cpp"
, 173, __extension__ __PRETTY_FUNCTION__))
;
174 *I = DIEValue(Old.getAttribute(), Old.getForm(), DIEInteger(New));
175 }
176
177 uint64_t get() const {
178 assert(I)(static_cast <bool> (I) ? void (0) : __assert_fail ("I"
, "/build/llvm-toolchain-snapshot-7~svn326246/tools/dsymutil/DwarfLinker.cpp"
, 178, __extension__ __PRETTY_FUNCTION__))
;
179 return I->getDIEInteger().getValue();
180 }
181};
182
183class CompileUnit;
184struct DeclMapInfo;
185
186/// A DeclContext is a named program scope that is used for ODR
187/// uniquing of types.
188/// The set of DeclContext for the ODR-subject parts of a Dwarf link
189/// is expanded (and uniqued) with each new object file processed. We
190/// need to determine the context of each DIE in an linked object file
191/// to see if the corresponding type has already been emitted.
192///
193/// The contexts are conceptually organized as a tree (eg. a function
194/// scope is contained in a namespace scope that contains other
195/// scopes), but storing/accessing them in an actual tree is too
196/// inefficient: we need to be able to very quickly query a context
197/// for a given child context by name. Storing a StringMap in each
198/// DeclContext would be too space inefficient.
199/// The solution here is to give each DeclContext a link to its parent
200/// (this allows to walk up the tree), but to query the existence of a
201/// specific DeclContext using a separate DenseMap keyed on the hash
202/// of the fully qualified name of the context.
203class DeclContext {
204 friend DeclMapInfo;
205
206 unsigned QualifiedNameHash = 0;
207 uint32_t Line = 0;
208 uint32_t ByteSize = 0;
209 uint16_t Tag = dwarf::DW_TAG_compile_unit;
210 unsigned DefinedInClangModule : 1;
211 StringRef Name;
212 StringRef File;
213 const DeclContext &Parent;
214 DWARFDie LastSeenDIE;
215 uint32_t LastSeenCompileUnitID = 0;
216 uint32_t CanonicalDIEOffset = 0;
217
218public:
219 using Map = DenseSet<DeclContext *, DeclMapInfo>;
220
221 DeclContext() : DefinedInClangModule(0), Parent(*this) {}
222
223 DeclContext(unsigned Hash, uint32_t Line, uint32_t ByteSize, uint16_t Tag,
224 StringRef Name, StringRef File, const DeclContext &Parent,
225 DWARFDie LastSeenDIE = DWARFDie(), unsigned CUId = 0)
226 : QualifiedNameHash(Hash), Line(Line), ByteSize(ByteSize), Tag(Tag),
227 DefinedInClangModule(0), Name(Name), File(File), Parent(Parent),
228 LastSeenDIE(LastSeenDIE), LastSeenCompileUnitID(CUId) {}
229
230 uint32_t getQualifiedNameHash() const { return QualifiedNameHash; }
231
232 bool setLastSeenDIE(CompileUnit &U, const DWARFDie &Die);
233
234 uint32_t getCanonicalDIEOffset() const { return CanonicalDIEOffset; }
235 void setCanonicalDIEOffset(uint32_t Offset) { CanonicalDIEOffset = Offset; }
236
237 bool isDefinedInClangModule() const { return DefinedInClangModule; }
238 void setDefinedInClangModule(bool Val) { DefinedInClangModule = Val; }
239
240 uint16_t getTag() const { return Tag; }
241 StringRef getName() const { return Name; }
242};
243
244/// Info type for the DenseMap storing the DeclContext pointers.
245struct DeclMapInfo : private DenseMapInfo<DeclContext *> {
246 using DenseMapInfo<DeclContext *>::getEmptyKey;
247 using DenseMapInfo<DeclContext *>::getTombstoneKey;
248
249 static unsigned getHashValue(const DeclContext *Ctxt) {
250 return Ctxt->QualifiedNameHash;
251 }
252
253 static bool isEqual(const DeclContext *LHS, const DeclContext *RHS) {
254 if (RHS == getEmptyKey() || RHS == getTombstoneKey())
255 return RHS == LHS;
256 return LHS->QualifiedNameHash == RHS->QualifiedNameHash &&
257 LHS->Line == RHS->Line && LHS->ByteSize == RHS->ByteSize &&
258 LHS->Name.data() == RHS->Name.data() &&
259 LHS->File.data() == RHS->File.data() &&
260 LHS->Parent.QualifiedNameHash == RHS->Parent.QualifiedNameHash;
261 }
262};
263
264/// This class gives a tree-like API to the DenseMap that stores the
265/// DeclContext objects. It also holds the BumpPtrAllocator where
266/// these objects will be allocated.
267class DeclContextTree {
268 BumpPtrAllocator Allocator;
269 DeclContext Root;
270 DeclContext::Map Contexts;
271
272 /// Cache resolved paths from the line table.
273 CachedPathResolver PathResolver;
274
275public:
276 /// Get the child of \a Context described by \a DIE in \a Unit. The
277 /// required strings will be interned in \a StringPool.
278 /// \returns The child DeclContext along with one bit that is set if
279 /// this context is invalid.
280 ///
281 /// An invalid context means it shouldn't be considered for uniquing, but its
282 /// not returning null, because some children of that context might be
283 /// uniquing candidates.
284 ///
285 /// FIXME: The invalid bit along the return value is to emulate some
286 /// dsymutil-classic functionality.
287 PointerIntPair<DeclContext *, 1>
288 getChildDeclContext(DeclContext &Context, const DWARFDie &DIE,
289 CompileUnit &Unit, NonRelocatableStringpool &StringPool,
290 bool InClangModule);
291
292 DeclContext &getRoot() { return Root; }
293};
294
295/// Stores all information relating to a compile unit, be it in its original
296/// instance in the object file to its brand new cloned and linked DIE tree.
297class CompileUnit {
298public:
299 /// Information gathered about a DIE in the object file.
300 struct DIEInfo {
301 /// Address offset to apply to the described entity.
302 int64_t AddrAdjust;
303
304 /// ODR Declaration context.
305 DeclContext *Ctxt;
306
307 /// Cloned version of that DIE.
308 DIE *Clone;
309
310 /// The index of this DIE's parent.
311 uint32_t ParentIdx;
312
313 /// Is the DIE part of the linked output?
314 bool Keep : 1;
315
316 /// Was this DIE's entity found in the map?
317 bool InDebugMap : 1;
318
319 /// Is this a pure forward declaration we can strip?
320 bool Prune : 1;
321
322 /// Does DIE transitively refer an incomplete decl?
323 bool Incomplete : 1;
324 };
325
326 CompileUnit(DWARFUnit &OrigUnit, unsigned ID, bool CanUseODR,
327 StringRef ClangModuleName)
328 : OrigUnit(OrigUnit), ID(ID), Ranges(RangeAlloc),
329 ClangModuleName(ClangModuleName) {
330 Info.resize(OrigUnit.getNumDIEs());
331
332 auto CUDie = OrigUnit.getUnitDIE(false);
333 if (auto Lang = dwarf::toUnsigned(CUDie.find(dwarf::DW_AT_language)))
334 HasODR = CanUseODR && (*Lang == dwarf::DW_LANG_C_plus_plus ||
335 *Lang == dwarf::DW_LANG_C_plus_plus_03 ||
336 *Lang == dwarf::DW_LANG_C_plus_plus_11 ||
337 *Lang == dwarf::DW_LANG_C_plus_plus_14 ||
338 *Lang == dwarf::DW_LANG_ObjC_plus_plus);
339 else
340 HasODR = false;
341 }
342
343 DWARFUnit &getOrigUnit() const { return OrigUnit; }
344
345 unsigned getUniqueID() const { return ID; }
346
347 void createOutputDIE() {
348 NewUnit.emplace(OrigUnit.getVersion(), OrigUnit.getAddressByteSize(),
349 OrigUnit.getUnitDIE().getTag());
350 }
351
352 DIE *getOutputUnitDIE() const {
353 if (NewUnit)
354 return &const_cast<BasicDIEUnit &>(*NewUnit).getUnitDie();
355 return nullptr;
356 }
357
358 bool hasODR() const { return HasODR; }
359 bool isClangModule() const { return !ClangModuleName.empty(); }
360 const std::string &getClangModuleName() const { return ClangModuleName; }
361
362 DIEInfo &getInfo(unsigned Idx) { return Info[Idx]; }
363 const DIEInfo &getInfo(unsigned Idx) const { return Info[Idx]; }
364
365 uint64_t getStartOffset() const { return StartOffset; }
366 uint64_t getNextUnitOffset() const { return NextUnitOffset; }
367 void setStartOffset(uint64_t DebugInfoSize) { StartOffset = DebugInfoSize; }
368
369 uint64_t getLowPc() const { return LowPc; }
370 uint64_t getHighPc() const { return HighPc; }
371 bool hasLabelAt(uint64_t Addr) const { return Labels.count(Addr); }
372
373 Optional<PatchLocation> getUnitRangesAttribute() const {
374 return UnitRangeAttribute;
375 }
376
377 const FunctionIntervals &getFunctionRanges() const { return Ranges; }
378
379 const std::vector<PatchLocation> &getRangesAttributes() const {
380 return RangeAttributes;
381 }
382
383 const std::vector<std::pair<PatchLocation, int64_t>> &
384 getLocationAttributes() const {
385 return LocationAttributes;
386 }
387
388 void setHasInterestingContent() { HasInterestingContent = true; }
389 bool hasInterestingContent() { return HasInterestingContent; }
390
391 /// Mark every DIE in this unit as kept. This function also
392 /// marks variables as InDebugMap so that they appear in the
393 /// reconstructed accelerator tables.
394 void markEverythingAsKept();
395
396 /// Compute the end offset for this unit. Must be called after the CU's DIEs
397 /// have been cloned. \returns the next unit offset (which is also the
398 /// current debug_info section size).
399 uint64_t computeNextUnitOffset();
400
401 /// Keep track of a forward reference to DIE \p Die in \p RefUnit by \p
402 /// Attr. The attribute should be fixed up later to point to the absolute
403 /// offset of \p Die in the debug_info section or to the canonical offset of
404 /// \p Ctxt if it is non-null.
405 void noteForwardReference(DIE *Die, const CompileUnit *RefUnit,
406 DeclContext *Ctxt, PatchLocation Attr);
407
408 /// Apply all fixups recorded by noteForwardReference().
409 void fixupForwardReferences();
410
411 /// Add the low_pc of a label that is relocated by applying
412 /// offset \p PCOffset.
413 void addLabelLowPc(uint64_t LabelLowPc, int64_t PcOffset);
414
415 /// Add a function range [\p LowPC, \p HighPC) that is relocated by applying
416 /// offset \p PCOffset.
417 void addFunctionRange(uint64_t LowPC, uint64_t HighPC, int64_t PCOffset);
418
419 /// Keep track of a DW_AT_range attribute that we will need to patch up later.
420 void noteRangeAttribute(const DIE &Die, PatchLocation Attr);
421
422 /// Keep track of a location attribute pointing to a location list in the
423 /// debug_loc section.
424 void noteLocationAttribute(PatchLocation Attr, int64_t PcOffset);
425
426 /// Add a name accelerator entry for \a Die with \a Name.
427 void addNamespaceAccelerator(const DIE *Die, DwarfStringPoolEntryRef Name);
428
429 /// Add a name accelerator entry for \a Die with \a Name.
430 void addNameAccelerator(const DIE *Die, DwarfStringPoolEntryRef Name,
431 bool SkipPubnamesSection = false);
432
433 /// Add various accelerator entries for \p Die with \p Name which is stored
434 /// in the string table at \p Offset. \p Name must be an Objective-C
435 /// selector.
436 void addObjCAccelerator(const DIE *Die, DwarfStringPoolEntryRef Name,
437 bool SkipPubnamesSection = false);
438
439 /// Add a type accelerator entry for \p Die with \p Name which is stored in
440 /// the string table at \p Offset.
441 void addTypeAccelerator(const DIE *Die, DwarfStringPoolEntryRef Name,
442 bool ObjcClassImplementation,
443 uint32_t QualifiedNameHash);
444
445 struct AccelInfo {
446 /// Name of the entry.
447 DwarfStringPoolEntryRef Name;
448
449 /// DIE this entry describes.
450 const DIE *Die;
451
452 /// Hash of the fully qualified name.
453 uint32_t QualifiedNameHash;
454
455 /// Emit this entry only in the apple_* sections.
456 bool SkipPubSection;
457
458 /// Is this an ObjC class implementation?
459 bool ObjcClassImplementation;
460
461 AccelInfo(DwarfStringPoolEntryRef Name, const DIE *Die,
462 bool SkipPubSection = false)
463 : Name(Name), Die(Die), SkipPubSection(SkipPubSection) {}
464
465 AccelInfo(DwarfStringPoolEntryRef Name, const DIE *Die,
466 uint32_t QualifiedNameHash, bool ObjCClassIsImplementation)
467 : Name(Name), Die(Die), QualifiedNameHash(QualifiedNameHash),
468 SkipPubSection(false),
469 ObjcClassImplementation(ObjCClassIsImplementation) {}
470 };
471
472 const std::vector<AccelInfo> &getPubnames() const { return Pubnames; }
473 const std::vector<AccelInfo> &getPubtypes() const { return Pubtypes; }
474 const std::vector<AccelInfo> &getNamespaces() const { return Namespaces; }
475 const std::vector<AccelInfo> &getObjC() const { return ObjC; }
476
477 /// Get the full path for file \a FileNum in the line table
478 StringRef getResolvedPath(unsigned FileNum) {
479 if (FileNum >= ResolvedPaths.size())
480 return StringRef();
481 return ResolvedPaths[FileNum];
482 }
483
484 /// Set the fully resolved path for the line-table's file \a FileNum
485 /// to \a Path.
486 void setResolvedPath(unsigned FileNum, StringRef Path) {
487 if (ResolvedPaths.size() <= FileNum)
488 ResolvedPaths.resize(FileNum + 1);
489 ResolvedPaths[FileNum] = Path;
490 }
491
492private:
493 DWARFUnit &OrigUnit;
494 unsigned ID;
495 std::vector<DIEInfo> Info; ///< DIE info indexed by DIE index.
496 Optional<BasicDIEUnit> NewUnit;
497
498 uint64_t StartOffset;
499 uint64_t NextUnitOffset;
500
501 uint64_t LowPc = std::numeric_limits<uint64_t>::max();
502 uint64_t HighPc = 0;
503
504 /// A list of attributes to fixup with the absolute offset of
505 /// a DIE in the debug_info section.
506 ///
507 /// The offsets for the attributes in this array couldn't be set while
508 /// cloning because for cross-cu forward references the target DIE's offset
509 /// isn't known you emit the reference attribute.
510 std::vector<
511 std::tuple<DIE *, const CompileUnit *, DeclContext *, PatchLocation>>
512 ForwardDIEReferences;
513
514 FunctionIntervals::Allocator RangeAlloc;
515
516 /// The ranges in that interval map are the PC ranges for
517 /// functions in this unit, associated with the PC offset to apply
518 /// to the addresses to get the linked address.
519 FunctionIntervals Ranges;
520
521 /// The DW_AT_low_pc of each DW_TAG_label.
522 SmallDenseMap<uint64_t, uint64_t, 1> Labels;
523
524 /// DW_AT_ranges attributes to patch after we have gathered
525 /// all the unit's function addresses.
526 /// @{
527 std::vector<PatchLocation> RangeAttributes;
528 Optional<PatchLocation> UnitRangeAttribute;
529 /// @}
530
531 /// Location attributes that need to be transferred from the
532 /// original debug_loc section to the liked one. They are stored
533 /// along with the PC offset that is to be applied to their
534 /// function's address.
535 std::vector<std::pair<PatchLocation, int64_t>> LocationAttributes;
536
537 /// Accelerator entries for the unit, both for the pub*
538 /// sections and the apple* ones.
539 /// @{
540 std::vector<AccelInfo> Pubnames;
541 std::vector<AccelInfo> Pubtypes;
542 std::vector<AccelInfo> Namespaces;
543 std::vector<AccelInfo> ObjC;
544 /// @}
545
546 /// Cached resolved paths from the line table.
547 /// Note, the StringRefs here point in to the intern (uniquing) string pool.
548 /// This means that a StringRef returned here doesn't need to then be uniqued
549 /// for the purposes of getting a unique address for each string.
550 std::vector<StringRef> ResolvedPaths;
551
552 /// Is this unit subject to the ODR rule?
553 bool HasODR;
554
555 /// Did a DIE actually contain a valid reloc?
556 bool HasInterestingContent;
557
558 /// If this is a Clang module, this holds the module's name.
559 std::string ClangModuleName;
560};
561
562/// Check if the DIE at \p Idx is in the scope of a function.
563static bool inFunctionScope(CompileUnit &U, unsigned Idx) {
564 while (Idx) {
565 if (U.getOrigUnit().getDIEAtIndex(Idx).getTag() == dwarf::DW_TAG_subprogram)
566 return true;
567 Idx = U.getInfo(Idx).ParentIdx;
568 }
569 return false;
570}
571
572} // end anonymous namespace
573
574void CompileUnit::markEverythingAsKept() {
575 unsigned Idx = 0;
576
577 setHasInterestingContent();
578
579 for (auto &I : Info) {
580 // Mark everything that wasn't explicit marked for pruning.
581 I.Keep = !I.Prune;
582 auto DIE = OrigUnit.getDIEAtIndex(Idx++);
583
584 // Try to guess which DIEs must go to the accelerator tables. We do that
585 // just for variables, because functions will be handled depending on
586 // whether they carry a DW_AT_low_pc attribute or not.
587 if (DIE.getTag() != dwarf::DW_TAG_variable &&
588 DIE.getTag() != dwarf::DW_TAG_constant)
589 continue;
590
591 Optional<DWARFFormValue> Value;
592 if (!(Value = DIE.find(dwarf::DW_AT_location))) {
593 if ((Value = DIE.find(dwarf::DW_AT_const_value)) &&
594 !inFunctionScope(*this, I.ParentIdx))
595 I.InDebugMap = true;
596 continue;
597 }
598 if (auto Block = Value->getAsBlock()) {
599 if (Block->size() > OrigUnit.getAddressByteSize() &&
600 (*Block)[0] == dwarf::DW_OP_addr)
601 I.InDebugMap = true;
602 }
603 }
604}
605
606uint64_t CompileUnit::computeNextUnitOffset() {
607 NextUnitOffset = StartOffset + 11 /* Header size */;
608 // The root DIE might be null, meaning that the Unit had nothing to
609 // contribute to the linked output. In that case, we will emit the
610 // unit header without any actual DIE.
611 if (NewUnit)
612 NextUnitOffset += NewUnit->getUnitDie().getSize();
613 return NextUnitOffset;
614}
615
616/// Keep track of a forward cross-cu reference from this unit
617/// to \p Die that lives in \p RefUnit.
618void CompileUnit::noteForwardReference(DIE *Die, const CompileUnit *RefUnit,
619 DeclContext *Ctxt, PatchLocation Attr) {
620 ForwardDIEReferences.emplace_back(Die, RefUnit, Ctxt, Attr);
621}
622
623void CompileUnit::fixupForwardReferences() {
624 for (const auto &Ref : ForwardDIEReferences) {
625 DIE *RefDie;
626 const CompileUnit *RefUnit;
627 PatchLocation Attr;
628 DeclContext *Ctxt;
629 std::tie(RefDie, RefUnit, Ctxt, Attr) = Ref;
630 if (Ctxt && Ctxt->getCanonicalDIEOffset())
631 Attr.set(Ctxt->getCanonicalDIEOffset());
632 else
633 Attr.set(RefDie->getOffset() + RefUnit->getStartOffset());
634 }
635}
636
637void CompileUnit::addLabelLowPc(uint64_t LabelLowPc, int64_t PcOffset) {
638 Labels.insert({LabelLowPc, PcOffset});
639}
640
641void CompileUnit::addFunctionRange(uint64_t FuncLowPc, uint64_t FuncHighPc,
642 int64_t PcOffset) {
643 Ranges.insert(FuncLowPc, FuncHighPc, PcOffset);
644 this->LowPc = std::min(LowPc, FuncLowPc + PcOffset);
645 this->HighPc = std::max(HighPc, FuncHighPc + PcOffset);
646}
647
648void CompileUnit::noteRangeAttribute(const DIE &Die, PatchLocation Attr) {
649 if (Die.getTag() != dwarf::DW_TAG_compile_unit)
650 RangeAttributes.push_back(Attr);
651 else
652 UnitRangeAttribute = Attr;
653}
654
655void CompileUnit::noteLocationAttribute(PatchLocation Attr, int64_t PcOffset) {
656 LocationAttributes.emplace_back(Attr, PcOffset);
657}
658
659void CompileUnit::addNamespaceAccelerator(const DIE *Die,
660 DwarfStringPoolEntryRef Name) {
661 Namespaces.emplace_back(Name, Die);
662}
663
664void CompileUnit::addObjCAccelerator(const DIE *Die,
665 DwarfStringPoolEntryRef Name,
666 bool SkipPubSection) {
667 ObjC.emplace_back(Name, Die, SkipPubSection);
668}
669
670void CompileUnit::addNameAccelerator(const DIE *Die,
671 DwarfStringPoolEntryRef Name,
672 bool SkipPubSection) {
673 Pubnames.emplace_back(Name, Die, SkipPubSection);
674}
675
676void CompileUnit::addTypeAccelerator(const DIE *Die,
677 DwarfStringPoolEntryRef Name,
678 bool ObjcClassImplementation,
679 uint32_t QualifiedNameHash) {
680 Pubtypes.emplace_back(Name, Die, QualifiedNameHash, ObjcClassImplementation);
681}
682
683namespace {
684
685/// The Dwarf streaming logic
686///
687/// All interactions with the MC layer that is used to build the debug
688/// information binary representation are handled in this class.
689class DwarfStreamer {
690 /// \defgroup MCObjects MC layer objects constructed by the streamer
691 /// @{
692 std::unique_ptr<MCRegisterInfo> MRI;
693 std::unique_ptr<MCAsmInfo> MAI;
694 std::unique_ptr<MCObjectFileInfo> MOFI;
695 std::unique_ptr<MCContext> MC;
696 MCAsmBackend *MAB; // Owned by MCStreamer
697 std::unique_ptr<MCInstrInfo> MII;
698 std::unique_ptr<MCSubtargetInfo> MSTI;
699 MCCodeEmitter *MCE; // Owned by MCStreamer
700 MCStreamer *MS; // Owned by AsmPrinter
701 std::unique_ptr<TargetMachine> TM;
702 std::unique_ptr<AsmPrinter> Asm;
703 /// @}
704
705 /// The file we stream the linked Dwarf to.
706 raw_fd_ostream &OutFile;
707
708 uint32_t RangesSectionSize;
709 uint32_t LocSectionSize;
710 uint32_t LineSectionSize;
711 uint32_t FrameSectionSize;
712
713 /// Emit the pubnames or pubtypes section contribution for \p
714 /// Unit into \p Sec. The data is provided in \p Names.
715 void emitPubSectionForUnit(MCSection *Sec, StringRef Name,
716 const CompileUnit &Unit,
717 const std::vector<CompileUnit::AccelInfo> &Names);
718
719public:
720 DwarfStreamer(raw_fd_ostream &OutFile) : OutFile(OutFile) {}
721 bool init(Triple TheTriple);
722
723 /// Dump the file to the disk.
724 bool finish(const DebugMap &);
725
726 AsmPrinter &getAsmPrinter() const { return *Asm; }
727
728 /// Set the current output section to debug_info and change
729 /// the MC Dwarf version to \p DwarfVersion.
730 void switchToDebugInfoSection(unsigned DwarfVersion);
731
732 /// Emit the compilation unit header for \p Unit in the
733 /// debug_info section.
734 ///
735 /// As a side effect, this also switches the current Dwarf version
736 /// of the MC layer to the one of U.getOrigUnit().
737 void emitCompileUnitHeader(CompileUnit &Unit);
738
739 /// Recursively emit the DIE tree rooted at \p Die.
740 void emitDIE(DIE &Die);
741
742 /// Emit the abbreviation table \p Abbrevs to the debug_abbrev section.
743 void emitAbbrevs(const std::vector<std::unique_ptr<DIEAbbrev>> &Abbrevs,
744 unsigned DwarfVersion);
745
746 /// Emit the string table described by \p Pool.
747 void emitStrings(const NonRelocatableStringpool &Pool);
748
749 /// Emit the swift_ast section stored in \p Buffer.
750 void emitSwiftAST(StringRef Buffer);
751
752 /// Emit debug_ranges for \p FuncRange by translating the
753 /// original \p Entries.
754 void emitRangesEntries(
755 int64_t UnitPcOffset, uint64_t OrigLowPc,
756 const FunctionIntervals::const_iterator &FuncRange,
757 const std::vector<DWARFDebugRangeList::RangeListEntry> &Entries,
758 unsigned AddressSize);
759
760 /// Emit debug_aranges entries for \p Unit and if \p DoRangesSection is true,
761 /// also emit the debug_ranges entries for the DW_TAG_compile_unit's
762 /// DW_AT_ranges attribute.
763 void emitUnitRangesEntries(CompileUnit &Unit, bool DoRangesSection);
764
765 uint32_t getRangesSectionSize() const { return RangesSectionSize; }
766
767 /// Emit the debug_loc contribution for \p Unit by copying the entries from
768 /// \p Dwarf and offsetting them. Update the location attributes to point to
769 /// the new entries.
770 void emitLocationsForUnit(const CompileUnit &Unit, DWARFContext &Dwarf);
771
772 /// Emit the line table described in \p Rows into the debug_line section.
773 void emitLineTableForUnit(MCDwarfLineTableParams Params,
774 StringRef PrologueBytes, unsigned MinInstLength,
775 std::vector<DWARFDebugLine::Row> &Rows,
776 unsigned AdddressSize);
777
778 /// Copy over the debug sections that are not modified when updating.
779 void copyInvariantDebugSection(const object::ObjectFile &Obj, LinkOptions &);
780
781 uint32_t getLineSectionSize() const { return LineSectionSize; }
782
783 /// Emit the .debug_pubnames contribution for \p Unit.
784 void emitPubNamesForUnit(const CompileUnit &Unit);
785
786 /// Emit the .debug_pubtypes contribution for \p Unit.
787 void emitPubTypesForUnit(const CompileUnit &Unit);
788
789 /// Emit a CIE.
790 void emitCIE(StringRef CIEBytes);
791
792 /// Emit an FDE with data \p Bytes.
793 void emitFDE(uint32_t CIEOffset, uint32_t AddreSize, uint32_t Address,
794 StringRef Bytes);
795
796 /// Emit Apple namespaces accelerator table.
797 void emitAppleNamespaces(AccelTable<AppleAccelTableStaticOffsetData> &Table);
798
799 /// Emit Apple names accelerator table.
800 void emitAppleNames(AccelTable<AppleAccelTableStaticOffsetData> &Table);
801
802 /// Emit Apple Objective-C accelerator table.
803 void emitAppleObjc(AccelTable<AppleAccelTableStaticOffsetData> &Table);
804
805 /// Emit Apple type accelerator table.
806 void emitAppleTypes(AccelTable<AppleAccelTableStaticTypeData> &Table);
807
808 uint32_t getFrameSectionSize() const { return FrameSectionSize; }
809};
810
811} // end anonymous namespace
812
813bool DwarfStreamer::init(Triple TheTriple) {
814 std::string ErrorStr;
815 std::string TripleName;
816 StringRef Context = "dwarf streamer init";
817
818 // Get the target.
819 const Target *TheTarget =
820 TargetRegistry::lookupTarget(TripleName, TheTriple, ErrorStr);
821 if (!TheTarget)
822 return error(ErrorStr, Context);
823 TripleName = TheTriple.getTriple();
824
825 // Create all the MC Objects.
826 MRI.reset(TheTarget->createMCRegInfo(TripleName));
827 if (!MRI)
828 return error(Twine("no register info for target ") + TripleName, Context);
829
830 MAI.reset(TheTarget->createMCAsmInfo(*MRI, TripleName));
831 if (!MAI)
832 return error("no asm info for target " + TripleName, Context);
833
834 MOFI.reset(new MCObjectFileInfo);
835 MC.reset(new MCContext(MAI.get(), MRI.get(), MOFI.get()));
836 MOFI->InitMCObjectFileInfo(TheTriple, /*PIC*/ false, *MC);
837
838 MSTI.reset(TheTarget->createMCSubtargetInfo(TripleName, "", ""));
839 if (!MSTI)
840 return error("no subtarget info for target " + TripleName, Context);
841
842 MCTargetOptions Options;
843 MAB = TheTarget->createMCAsmBackend(*MSTI, *MRI, Options);
844 if (!MAB)
845 return error("no asm backend for target " + TripleName, Context);
846
847 MII.reset(TheTarget->createMCInstrInfo());
848 if (!MII)
849 return error("no instr info info for target " + TripleName, Context);
850
851 MCE = TheTarget->createMCCodeEmitter(*MII, *MRI, *MC);
852 if (!MCE)
853 return error("no code emitter for target " + TripleName, Context);
854
855 MCTargetOptions MCOptions = InitMCTargetOptionsFromFlags();
856 MS = TheTarget->createMCObjectStreamer(
857 TheTriple, *MC, std::unique_ptr<MCAsmBackend>(MAB), OutFile,
858 std::unique_ptr<MCCodeEmitter>(MCE), *MSTI, MCOptions.MCRelaxAll,
859 MCOptions.MCIncrementalLinkerCompatible,
860 /*DWARFMustBeAtTheEnd*/ false);
861 if (!MS)
862 return error("no object streamer for target " + TripleName, Context);
863
864 // Finally create the AsmPrinter we'll use to emit the DIEs.
865 TM.reset(TheTarget->createTargetMachine(TripleName, "", "", TargetOptions(),
866 None));
867 if (!TM)
868 return error("no target machine for target " + TripleName, Context);
869
870 Asm.reset(TheTarget->createAsmPrinter(*TM, std::unique_ptr<MCStreamer>(MS)));
871 if (!Asm)
872 return error("no asm printer for target " + TripleName, Context);
873
874 RangesSectionSize = 0;
875 LocSectionSize = 0;
876 LineSectionSize = 0;
877 FrameSectionSize = 0;
878
879 return true;
880}
881
882bool DwarfStreamer::finish(const DebugMap &DM) {
883 bool Result = true;
884 if (DM.getTriple().isOSDarwin() && !DM.getBinaryPath().empty())
885 Result = MachOUtils::generateDsymCompanion(DM, *MS, OutFile);
886 else
887 MS->Finish();
888 return Result;
889}
890
891void DwarfStreamer::switchToDebugInfoSection(unsigned DwarfVersion) {
892 MS->SwitchSection(MOFI->getDwarfInfoSection());
893 MC->setDwarfVersion(DwarfVersion);
894}
895
896/// Emit the compilation unit header for \p Unit in the debug_info section.
897///
898/// A Dwarf section header is encoded as:
899/// uint32_t Unit length (omitting this field)
900/// uint16_t Version
901/// uint32_t Abbreviation table offset
902/// uint8_t Address size
903///
904/// Leading to a total of 11 bytes.
905void DwarfStreamer::emitCompileUnitHeader(CompileUnit &Unit) {
906 unsigned Version = Unit.getOrigUnit().getVersion();
907 switchToDebugInfoSection(Version);
908
909 // Emit size of content not including length itself. The size has already
910 // been computed in CompileUnit::computeOffsets(). Subtract 4 to that size to
911 // account for the length field.
912 Asm->EmitInt32(Unit.getNextUnitOffset() - Unit.getStartOffset() - 4);
913 Asm->EmitInt16(Version);
914 // We share one abbreviations table across all units so it's always at the
915 // start of the section.
916 Asm->EmitInt32(0);
917 Asm->EmitInt8(Unit.getOrigUnit().getAddressByteSize());
918}
919
920/// Emit the \p Abbrevs array as the shared abbreviation table
921/// for the linked Dwarf file.
922void DwarfStreamer::emitAbbrevs(
923 const std::vector<std::unique_ptr<DIEAbbrev>> &Abbrevs,
924 unsigned DwarfVersion) {
925 MS->SwitchSection(MOFI->getDwarfAbbrevSection());
926 MC->setDwarfVersion(DwarfVersion);
927 Asm->emitDwarfAbbrevs(Abbrevs);
928}
929
930/// Recursively emit the DIE tree rooted at \p Die.
931void DwarfStreamer::emitDIE(DIE &Die) {
932 MS->SwitchSection(MOFI->getDwarfInfoSection());
933 Asm->emitDwarfDIE(Die);
934}
935
936/// Emit the debug_str section stored in \p Pool.
937void DwarfStreamer::emitStrings(const NonRelocatableStringpool &Pool) {
938 Asm->OutStreamer->SwitchSection(MOFI->getDwarfStrSection());
939 std::vector<DwarfStringPoolEntryRef> Entries = Pool.getEntries();
940 for (auto Entry : Entries) {
941 if (Entry.getIndex() == -1U)
942 break;
943 // Emit the string itself.
944 Asm->OutStreamer->EmitBytes(Entry.getString());
945 // Emit a null terminator.
946 Asm->EmitInt8(0);
947 }
948}
949
950void DwarfStreamer::emitAppleNamespaces(
951 AccelTable<AppleAccelTableStaticOffsetData> &Table) {
952 Asm->OutStreamer->SwitchSection(MOFI->getDwarfAccelNamespaceSection());
953 auto *SectionBegin = Asm->createTempSymbol("namespac_begin");
954 Asm->OutStreamer->EmitLabel(SectionBegin);
955 emitAppleAccelTable(Asm.get(), Table, "namespac", SectionBegin);
956}
957
958void DwarfStreamer::emitAppleNames(
959 AccelTable<AppleAccelTableStaticOffsetData> &Table) {
960 Asm->OutStreamer->SwitchSection(MOFI->getDwarfAccelNamesSection());
961 auto *SectionBegin = Asm->createTempSymbol("names_begin");
962 Asm->OutStreamer->EmitLabel(SectionBegin);
963 emitAppleAccelTable(Asm.get(), Table, "names", SectionBegin);
964}
965
966void DwarfStreamer::emitAppleObjc(
967 AccelTable<AppleAccelTableStaticOffsetData> &Table) {
968 Asm->OutStreamer->SwitchSection(MOFI->getDwarfAccelObjCSection());
969 auto *SectionBegin = Asm->createTempSymbol("objc_begin");
970 Asm->OutStreamer->EmitLabel(SectionBegin);
971 emitAppleAccelTable(Asm.get(), Table, "objc", SectionBegin);
972}
973
974void DwarfStreamer::emitAppleTypes(
975 AccelTable<AppleAccelTableStaticTypeData> &Table) {
976 Asm->OutStreamer->SwitchSection(MOFI->getDwarfAccelTypesSection());
977 auto *SectionBegin = Asm->createTempSymbol("types_begin");
978 Asm->OutStreamer->EmitLabel(SectionBegin);
979 emitAppleAccelTable(Asm.get(), Table, "types", SectionBegin);
980}
981
982/// Emit the swift_ast section stored in \p Buffers.
983void DwarfStreamer::emitSwiftAST(StringRef Buffer) {
984 MCSection *SwiftASTSection = MOFI->getDwarfSwiftASTSection();
985 SwiftASTSection->setAlignment(1 << 5);
986 MS->SwitchSection(SwiftASTSection);
987 MS->EmitBytes(Buffer);
988}
989
990/// Emit the debug_range section contents for \p FuncRange by
991/// translating the original \p Entries. The debug_range section
992/// format is totally trivial, consisting just of pairs of address
993/// sized addresses describing the ranges.
994void DwarfStreamer::emitRangesEntries(
995 int64_t UnitPcOffset, uint64_t OrigLowPc,
996 const FunctionIntervals::const_iterator &FuncRange,
997 const std::vector<DWARFDebugRangeList::RangeListEntry> &Entries,
998 unsigned AddressSize) {
999 MS->SwitchSection(MC->getObjectFileInfo()->getDwarfRangesSection());
1000
1001 // Offset each range by the right amount.
1002 int64_t PcOffset = Entries.empty() ? 0 : FuncRange.value() + UnitPcOffset;
1003 for (const auto &Range : Entries) {
1004 if (Range.isBaseAddressSelectionEntry(AddressSize)) {
1005 warn("unsupported base address selection operation",
1006 "emitting debug_ranges");
1007 break;
1008 }
1009 // Do not emit empty ranges.
1010 if (Range.StartAddress == Range.EndAddress)
1011 continue;
1012
1013 // All range entries should lie in the function range.
1014 if (!(Range.StartAddress + OrigLowPc >= FuncRange.start() &&
1015 Range.EndAddress + OrigLowPc <= FuncRange.stop()))
1016 warn("inconsistent range data.", "emitting debug_ranges");
1017 MS->EmitIntValue(Range.StartAddress + PcOffset, AddressSize);
1018 MS->EmitIntValue(Range.EndAddress + PcOffset, AddressSize);
1019 RangesSectionSize += 2 * AddressSize;
1020 }
1021
1022 // Add the terminator entry.
1023 MS->EmitIntValue(0, AddressSize);
1024 MS->EmitIntValue(0, AddressSize);
1025 RangesSectionSize += 2 * AddressSize;
1026}
1027
1028/// Emit the debug_aranges contribution of a unit and
1029/// if \p DoDebugRanges is true the debug_range contents for a
1030/// compile_unit level DW_AT_ranges attribute (Which are basically the
1031/// same thing with a different base address).
1032/// Just aggregate all the ranges gathered inside that unit.
1033void DwarfStreamer::emitUnitRangesEntries(CompileUnit &Unit,
1034 bool DoDebugRanges) {
1035 unsigned AddressSize = Unit.getOrigUnit().getAddressByteSize();
1036 // Gather the ranges in a vector, so that we can simplify them. The
1037 // IntervalMap will have coalesced the non-linked ranges, but here
1038 // we want to coalesce the linked addresses.
1039 std::vector<std::pair<uint64_t, uint64_t>> Ranges;
1040 const auto &FunctionRanges = Unit.getFunctionRanges();
1041 for (auto Range = FunctionRanges.begin(), End = FunctionRanges.end();
1042 Range != End; ++Range)
1043 Ranges.push_back(std::make_pair(Range.start() + Range.value(),
1044 Range.stop() + Range.value()));
1045
1046 // The object addresses where sorted, but again, the linked
1047 // addresses might end up in a different order.
1048 std::sort(Ranges.begin(), Ranges.end());
1049
1050 if (!Ranges.empty()) {
1051 MS->SwitchSection(MC->getObjectFileInfo()->getDwarfARangesSection());
1052
1053 MCSymbol *BeginLabel = Asm->createTempSymbol("Barange");
1054 MCSymbol *EndLabel = Asm->createTempSymbol("Earange");
1055
1056 unsigned HeaderSize =
1057 sizeof(int32_t) + // Size of contents (w/o this field
1058 sizeof(int16_t) + // DWARF ARange version number
1059 sizeof(int32_t) + // Offset of CU in the .debug_info section
1060 sizeof(int8_t) + // Pointer Size (in bytes)
1061 sizeof(int8_t); // Segment Size (in bytes)
1062
1063 unsigned TupleSize = AddressSize * 2;
1064 unsigned Padding = OffsetToAlignment(HeaderSize, TupleSize);
1065
1066 Asm->EmitLabelDifference(EndLabel, BeginLabel, 4); // Arange length
1067 Asm->OutStreamer->EmitLabel(BeginLabel);
1068 Asm->EmitInt16(dwarf::DW_ARANGES_VERSION); // Version number
1069 Asm->EmitInt32(Unit.getStartOffset()); // Corresponding unit's offset
1070 Asm->EmitInt8(AddressSize); // Address size
1071 Asm->EmitInt8(0); // Segment size
1072
1073 Asm->OutStreamer->emitFill(Padding, 0x0);
1074
1075 for (auto Range = Ranges.begin(), End = Ranges.end(); Range != End;
1076 ++Range) {
1077 uint64_t RangeStart = Range->first;
1078 MS->EmitIntValue(RangeStart, AddressSize);
1079 while ((Range + 1) != End && Range->second == (Range + 1)->first)
1080 ++Range;
1081 MS->EmitIntValue(Range->second - RangeStart, AddressSize);
1082 }
1083
1084 // Emit terminator
1085 Asm->OutStreamer->EmitIntValue(0, AddressSize);
1086 Asm->OutStreamer->EmitIntValue(0, AddressSize);
1087 Asm->OutStreamer->EmitLabel(EndLabel);
1088 }
1089
1090 if (!DoDebugRanges)
1091 return;
1092
1093 MS->SwitchSection(MC->getObjectFileInfo()->getDwarfRangesSection());
1094 // Offset each range by the right amount.
1095 int64_t PcOffset = -Unit.getLowPc();
1096 // Emit coalesced ranges.
1097 for (auto Range = Ranges.begin(), End = Ranges.end(); Range != End; ++Range) {
1098 MS->EmitIntValue(Range->first + PcOffset, AddressSize);
1099 while (Range + 1 != End && Range->second == (Range + 1)->first)
1100 ++Range;
1101 MS->EmitIntValue(Range->second + PcOffset, AddressSize);
1102 RangesSectionSize += 2 * AddressSize;
1103 }
1104
1105 // Add the terminator entry.
1106 MS->EmitIntValue(0, AddressSize);
1107 MS->EmitIntValue(0, AddressSize);
1108 RangesSectionSize += 2 * AddressSize;
1109}
1110
1111/// Emit location lists for \p Unit and update attributes to point to the new
1112/// entries.
1113void DwarfStreamer::emitLocationsForUnit(const CompileUnit &Unit,
1114 DWARFContext &Dwarf) {
1115 const auto &Attributes = Unit.getLocationAttributes();
1116
1117 if (Attributes.empty())
1118 return;
1119
1120 MS->SwitchSection(MC->getObjectFileInfo()->getDwarfLocSection());
1121
1122 unsigned AddressSize = Unit.getOrigUnit().getAddressByteSize();
1123 const DWARFSection &InputSec = Dwarf.getDWARFObj().getLocSection();
1124 DataExtractor Data(InputSec.Data, Dwarf.isLittleEndian(), AddressSize);
1125 DWARFUnit &OrigUnit = Unit.getOrigUnit();
1126 auto OrigUnitDie = OrigUnit.getUnitDIE(false);
1127 int64_t UnitPcOffset = 0;
1128 if (auto OrigLowPc = dwarf::toAddress(OrigUnitDie.find(dwarf::DW_AT_low_pc)))
1129 UnitPcOffset = int64_t(*OrigLowPc) - Unit.getLowPc();
1130
1131 for (const auto &Attr : Attributes) {
1132 uint32_t Offset = Attr.first.get();
1133 Attr.first.set(LocSectionSize);
1134 // This is the quantity to add to the old location address to get
1135 // the correct address for the new one.
1136 int64_t LocPcOffset = Attr.second + UnitPcOffset;
1137 while (Data.isValidOffset(Offset)) {
1138 uint64_t Low = Data.getUnsigned(&Offset, AddressSize);
1139 uint64_t High = Data.getUnsigned(&Offset, AddressSize);
1140 LocSectionSize += 2 * AddressSize;
1141 if (Low == 0 && High == 0) {
1142 Asm->OutStreamer->EmitIntValue(0, AddressSize);
1143 Asm->OutStreamer->EmitIntValue(0, AddressSize);
1144 break;
1145 }
1146 Asm->OutStreamer->EmitIntValue(Low + LocPcOffset, AddressSize);
1147 Asm->OutStreamer->EmitIntValue(High + LocPcOffset, AddressSize);
1148 uint64_t Length = Data.getU16(&Offset);
1149 Asm->OutStreamer->EmitIntValue(Length, 2);
1150 // Just copy the bytes over.
1151 Asm->OutStreamer->EmitBytes(
1152 StringRef(InputSec.Data.substr(Offset, Length)));
1153 Offset += Length;
1154 LocSectionSize += Length + 2;
1155 }
1156 }
1157}
1158
1159void DwarfStreamer::emitLineTableForUnit(MCDwarfLineTableParams Params,
1160 StringRef PrologueBytes,
1161 unsigned MinInstLength,
1162 std::vector<DWARFDebugLine::Row> &Rows,
1163 unsigned PointerSize) {
1164 // Switch to the section where the table will be emitted into.
1165 MS->SwitchSection(MC->getObjectFileInfo()->getDwarfLineSection());
1166 MCSymbol *LineStartSym = MC->createTempSymbol();
1167 MCSymbol *LineEndSym = MC->createTempSymbol();
1168
1169 // The first 4 bytes is the total length of the information for this
1170 // compilation unit (not including these 4 bytes for the length).
1171 Asm->EmitLabelDifference(LineEndSym, LineStartSym, 4);
1172 Asm->OutStreamer->EmitLabel(LineStartSym);
1173 // Copy Prologue.
1174 MS->EmitBytes(PrologueBytes);
1175 LineSectionSize += PrologueBytes.size() + 4;
1176
1177 SmallString<128> EncodingBuffer;
1178 raw_svector_ostream EncodingOS(EncodingBuffer);
1179
1180 if (Rows.empty()) {
1181 // We only have the dummy entry, dsymutil emits an entry with a 0
1182 // address in that case.
1183 MCDwarfLineAddr::Encode(*MC, Params, std::numeric_limits<int64_t>::max(), 0,
1184 EncodingOS);
1185 MS->EmitBytes(EncodingOS.str());
1186 LineSectionSize += EncodingBuffer.size();
1187 MS->EmitLabel(LineEndSym);
1188 return;
1189 }
1190
1191 // Line table state machine fields
1192 unsigned FileNum = 1;
1193 unsigned LastLine = 1;
1194 unsigned Column = 0;
1195 unsigned IsStatement = 1;
1196 unsigned Isa = 0;
1197 uint64_t Address = -1ULL;
1198
1199 unsigned RowsSinceLastSequence = 0;
1200
1201 for (unsigned Idx = 0; Idx < Rows.size(); ++Idx) {
1202 auto &Row = Rows[Idx];
1203
1204 int64_t AddressDelta;
1205 if (Address == -1ULL) {
1206 MS->EmitIntValue(dwarf::DW_LNS_extended_op, 1);
1207 MS->EmitULEB128IntValue(PointerSize + 1);
1208 MS->EmitIntValue(dwarf::DW_LNE_set_address, 1);
1209 MS->EmitIntValue(Row.Address, PointerSize);
1210 LineSectionSize += 2 + PointerSize + getULEB128Size(PointerSize + 1);
1211 AddressDelta = 0;
1212 } else {
1213 AddressDelta = (Row.Address - Address) / MinInstLength;
1214 }
1215
1216 // FIXME: code copied and transformed from MCDwarf.cpp::EmitDwarfLineTable.
1217 // We should find a way to share this code, but the current compatibility
1218 // requirement with classic dsymutil makes it hard. Revisit that once this
1219 // requirement is dropped.
1220
1221 if (FileNum != Row.File) {
1222 FileNum = Row.File;
1223 MS->EmitIntValue(dwarf::DW_LNS_set_file, 1);
1224 MS->EmitULEB128IntValue(FileNum);
1225 LineSectionSize += 1 + getULEB128Size(FileNum);
1226 }
1227 if (Column != Row.Column) {
1228 Column = Row.Column;
1229 MS->EmitIntValue(dwarf::DW_LNS_set_column, 1);
1230 MS->EmitULEB128IntValue(Column);
1231 LineSectionSize += 1 + getULEB128Size(Column);
1232 }
1233
1234 // FIXME: We should handle the discriminator here, but dsymutil doesn't
1235 // consider it, thus ignore it for now.
1236
1237 if (Isa != Row.Isa) {
1238 Isa = Row.Isa;
1239 MS->EmitIntValue(dwarf::DW_LNS_set_isa, 1);
1240 MS->EmitULEB128IntValue(Isa);
1241 LineSectionSize += 1 + getULEB128Size(Isa);
1242 }
1243 if (IsStatement != Row.IsStmt) {
1244 IsStatement = Row.IsStmt;
1245 MS->EmitIntValue(dwarf::DW_LNS_negate_stmt, 1);
1246 LineSectionSize += 1;
1247 }
1248 if (Row.BasicBlock) {
1249 MS->EmitIntValue(dwarf::DW_LNS_set_basic_block, 1);
1250 LineSectionSize += 1;
1251 }
1252
1253 if (Row.PrologueEnd) {
1254 MS->EmitIntValue(dwarf::DW_LNS_set_prologue_end, 1);
1255 LineSectionSize += 1;
1256 }
1257
1258 if (Row.EpilogueBegin) {
1259 MS->EmitIntValue(dwarf::DW_LNS_set_epilogue_begin, 1);
1260 LineSectionSize += 1;
1261 }
1262
1263 int64_t LineDelta = int64_t(Row.Line) - LastLine;
1264 if (!Row.EndSequence) {
1265 MCDwarfLineAddr::Encode(*MC, Params, LineDelta, AddressDelta, EncodingOS);
1266 MS->EmitBytes(EncodingOS.str());
1267 LineSectionSize += EncodingBuffer.size();
1268 EncodingBuffer.resize(0);
1269 Address = Row.Address;
1270 LastLine = Row.Line;
1271 RowsSinceLastSequence++;
1272 } else {
1273 if (LineDelta) {
1274 MS->EmitIntValue(dwarf::DW_LNS_advance_line, 1);
1275 MS->EmitSLEB128IntValue(LineDelta);
1276 LineSectionSize += 1 + getSLEB128Size(LineDelta);
1277 }
1278 if (AddressDelta) {
1279 MS->EmitIntValue(dwarf::DW_LNS_advance_pc, 1);
1280 MS->EmitULEB128IntValue(AddressDelta);
1281 LineSectionSize += 1 + getULEB128Size(AddressDelta);
1282 }
1283 MCDwarfLineAddr::Encode(*MC, Params, std::numeric_limits<int64_t>::max(),
1284 0, EncodingOS);
1285 MS->EmitBytes(EncodingOS.str());
1286 LineSectionSize += EncodingBuffer.size();
1287 EncodingBuffer.resize(0);
1288 Address = -1ULL;
1289 LastLine = FileNum = IsStatement = 1;
1290 RowsSinceLastSequence = Column = Isa = 0;
1291 }
1292 }
1293
1294 if (RowsSinceLastSequence) {
1295 MCDwarfLineAddr::Encode(*MC, Params, std::numeric_limits<int64_t>::max(), 0,
1296 EncodingOS);
1297 MS->EmitBytes(EncodingOS.str());
1298 LineSectionSize += EncodingBuffer.size();
1299 EncodingBuffer.resize(0);
1300 }
1301
1302 MS->EmitLabel(LineEndSym);
1303}
1304
1305static void emitSectionContents(const object::ObjectFile &Obj,
1306 StringRef SecName, MCStreamer *MS) {
1307 StringRef Contents;
1308 if (auto Sec = getSectionByName(Obj, SecName))
1309 if (!Sec->getContents(Contents))
1310 MS->EmitBytes(Contents);
1311}
1312
1313void DwarfStreamer::copyInvariantDebugSection(const object::ObjectFile &Obj,
1314 LinkOptions &Options) {
1315 MS->SwitchSection(MC->getObjectFileInfo()->getDwarfLineSection());
1316 emitSectionContents(Obj, "debug_line", MS);
1317
1318 MS->SwitchSection(MC->getObjectFileInfo()->getDwarfLocSection());
1319 emitSectionContents(Obj, "debug_loc", MS);
1320
1321 MS->SwitchSection(MC->getObjectFileInfo()->getDwarfRangesSection());
1322 emitSectionContents(Obj, "debug_ranges", MS);
1323
1324 MS->SwitchSection(MC->getObjectFileInfo()->getDwarfFrameSection());
1325 emitSectionContents(Obj, "debug_frame", MS);
1326
1327 MS->SwitchSection(MC->getObjectFileInfo()->getDwarfARangesSection());
1328 emitSectionContents(Obj, "debug_aranges", MS);
1329}
1330
1331/// Emit the pubnames or pubtypes section contribution for \p
1332/// Unit into \p Sec. The data is provided in \p Names.
1333void DwarfStreamer::emitPubSectionForUnit(
1334 MCSection *Sec, StringRef SecName, const CompileUnit &Unit,
1335 const std::vector<CompileUnit::AccelInfo> &Names) {
1336 if (Names.empty())
1337 return;
1338
1339 // Start the dwarf pubnames section.
1340 Asm->OutStreamer->SwitchSection(Sec);
1341 MCSymbol *BeginLabel = Asm->createTempSymbol("pub" + SecName + "_begin");
1342 MCSymbol *EndLabel = Asm->createTempSymbol("pub" + SecName + "_end");
1343
1344 bool HeaderEmitted = false;
1345 // Emit the pubnames for this compilation unit.
1346 for (const auto &Name : Names) {
1347 if (Name.SkipPubSection)
1348 continue;
1349
1350 if (!HeaderEmitted) {
1351 // Emit the header.
1352 Asm->EmitLabelDifference(EndLabel, BeginLabel, 4); // Length
1353 Asm->OutStreamer->EmitLabel(BeginLabel);
1354 Asm->EmitInt16(dwarf::DW_PUBNAMES_VERSION); // Version
1355 Asm->EmitInt32(Unit.getStartOffset()); // Unit offset
1356 Asm->EmitInt32(Unit.getNextUnitOffset() - Unit.getStartOffset()); // Size
1357 HeaderEmitted = true;
1358 }
1359 Asm->EmitInt32(Name.Die->getOffset());
1360
1361 // Emit the string itself.
1362 Asm->OutStreamer->EmitBytes(Name.Name.getString());
1363 // Emit a null terminator.
1364 Asm->EmitInt8(0);
1365 }
1366
1367 if (!HeaderEmitted)
1368 return;
1369 Asm->EmitInt32(0); // End marker.
1370 Asm->OutStreamer->EmitLabel(EndLabel);
1371}
1372
1373/// Emit .debug_pubnames for \p Unit.
1374void DwarfStreamer::emitPubNamesForUnit(const CompileUnit &Unit) {
1375 emitPubSectionForUnit(MC->getObjectFileInfo()->getDwarfPubNamesSection(),
1376 "names", Unit, Unit.getPubnames());
1377}
1378
1379/// Emit .debug_pubtypes for \p Unit.
1380void DwarfStreamer::emitPubTypesForUnit(const CompileUnit &Unit) {
1381 emitPubSectionForUnit(MC->getObjectFileInfo()->getDwarfPubTypesSection(),
1382 "types", Unit, Unit.getPubtypes());
1383}
1384
1385/// Emit a CIE into the debug_frame section.
1386void DwarfStreamer::emitCIE(StringRef CIEBytes) {
1387 MS->SwitchSection(MC->getObjectFileInfo()->getDwarfFrameSection());
1388
1389 MS->EmitBytes(CIEBytes);
1390 FrameSectionSize += CIEBytes.size();
1391}
1392
1393/// Emit a FDE into the debug_frame section. \p FDEBytes
1394/// contains the FDE data without the length, CIE offset and address
1395/// which will be replaced with the parameter values.
1396void DwarfStreamer::emitFDE(uint32_t CIEOffset, uint32_t AddrSize,
1397 uint32_t Address, StringRef FDEBytes) {
1398 MS->SwitchSection(MC->getObjectFileInfo()->getDwarfFrameSection());
1399
1400 MS->EmitIntValue(FDEBytes.size() + 4 + AddrSize, 4);
1401 MS->EmitIntValue(CIEOffset, 4);
1402 MS->EmitIntValue(Address, AddrSize);
1403 MS->EmitBytes(FDEBytes);
1404 FrameSectionSize += FDEBytes.size() + 8 + AddrSize;
1405}
1406
1407namespace {
1408
1409/// The core of the Dwarf linking logic.
1410///
1411/// The link of the dwarf information from the object files will be
1412/// driven by the selection of 'root DIEs', which are DIEs that
1413/// describe variables or functions that are present in the linked
1414/// binary (and thus have entries in the debug map). All the debug
1415/// information that will be linked (the DIEs, but also the line
1416/// tables, ranges, ...) is derived from that set of root DIEs.
1417///
1418/// The root DIEs are identified because they contain relocations that
1419/// correspond to a debug map entry at specific places (the low_pc for
1420/// a function, the location for a variable). These relocations are
1421/// called ValidRelocs in the DwarfLinker and are gathered as a very
1422/// first step when we start processing a DebugMapObject.
1423class DwarfLinker {
1424public:
1425 DwarfLinker(raw_fd_ostream &OutFile, const LinkOptions &Options)
1426 : OutFile(OutFile), Options(Options), BinHolder(Options.Verbose) {}
1427
1428 /// Link the contents of the DebugMap.
1429 bool link(const DebugMap &);
1430
1431 void reportWarning(const Twine &Warning, const DWARFDie *DIE = nullptr) const;
1432
1433private:
1434 /// Called at the start of a debug object link.
1435 void startDebugObject(DWARFContext &, DebugMapObject &);
1436
1437 /// Called at the end of a debug object link.
1438 void endDebugObject();
1439
1440 /// Remembers the newest DWARF version we've seen in a unit.
1441 void maybeUpdateMaxDwarfVersion(unsigned Version) {
1442 if (MaxDwarfVersion < Version)
1443 MaxDwarfVersion = Version;
1444 }
1445
1446 /// Keeps track of relocations.
1447 class RelocationManager {
1448 struct ValidReloc {
1449 uint32_t Offset;
1450 uint32_t Size;
1451 uint64_t Addend;
1452 const DebugMapObject::DebugMapEntry *Mapping;
1453
1454 ValidReloc(uint32_t Offset, uint32_t Size, uint64_t Addend,
1455 const DebugMapObject::DebugMapEntry *Mapping)
1456 : Offset(Offset), Size(Size), Addend(Addend), Mapping(Mapping) {}
1457
1458 bool operator<(const ValidReloc &RHS) const {
1459 return Offset < RHS.Offset;
1460 }
1461 };
1462
1463 DwarfLinker &Linker;
1464
1465 /// The valid relocations for the current DebugMapObject.
1466 /// This vector is sorted by relocation offset.
1467 std::vector<ValidReloc> ValidRelocs;
1468
1469 /// Index into ValidRelocs of the next relocation to consider. As we walk
1470 /// the DIEs in acsending file offset and as ValidRelocs is sorted by file
1471 /// offset, keeping this index up to date is all we have to do to have a
1472 /// cheap lookup during the root DIE selection and during DIE cloning.
1473 unsigned NextValidReloc = 0;
1474
1475 public:
1476 RelocationManager(DwarfLinker &Linker) : Linker(Linker) {}
1477
1478 bool hasValidRelocs() const { return !ValidRelocs.empty(); }
1479
1480 /// Reset the NextValidReloc counter.
1481 void resetValidRelocs() { NextValidReloc = 0; }
1482
1483 /// \defgroup FindValidRelocations Translate debug map into a list
1484 /// of relevant relocations
1485 ///
1486 /// @{
1487 bool findValidRelocsInDebugInfo(const object::ObjectFile &Obj,
1488 const DebugMapObject &DMO);
1489
1490 bool findValidRelocs(const object::SectionRef &Section,
1491 const object::ObjectFile &Obj,
1492 const DebugMapObject &DMO);
1493
1494 void findValidRelocsMachO(const object::SectionRef &Section,
1495 const object::MachOObjectFile &Obj,
1496 const DebugMapObject &DMO);
1497 /// @}
1498
1499 bool hasValidRelocation(uint32_t StartOffset, uint32_t EndOffset,
1500 CompileUnit::DIEInfo &Info);
1501
1502 bool applyValidRelocs(MutableArrayRef<char> Data, uint32_t BaseOffset,
1503 bool isLittleEndian);
1504 };
1505
1506 /// \defgroup FindRootDIEs Find DIEs corresponding to debug map entries.
1507 ///
1508 /// @{
1509 /// Recursively walk the \p DIE tree and look for DIEs to
1510 /// keep. Store that information in \p CU's DIEInfo.
1511 ///
1512 /// The return value indicates whether the DIE is incomplete.
1513 bool lookForDIEsToKeep(RelocationManager &RelocMgr, const DWARFDie &DIE,
1514 const DebugMapObject &DMO, CompileUnit &CU,
1515 unsigned Flags);
1516
1517 /// If this compile unit is really a skeleton CU that points to a
1518 /// clang module, register it in ClangModules and return true.
1519 ///
1520 /// A skeleton CU is a CU without children, a DW_AT_gnu_dwo_name
1521 /// pointing to the module, and a DW_AT_gnu_dwo_id with the module
1522 /// hash.
1523 bool registerModuleReference(const DWARFDie &CUDie, const DWARFUnit &Unit,
1524 DebugMap &ModuleMap, unsigned Indent = 0);
1525
1526 /// Recursively add the debug info in this clang module .pcm
1527 /// file (and all the modules imported by it in a bottom-up fashion)
1528 /// to Units.
1529 Error loadClangModule(StringRef Filename, StringRef ModulePath,
1530 StringRef ModuleName, uint64_t DwoId,
1531 DebugMap &ModuleMap, unsigned Indent = 0);
1532
1533 /// Flags passed to DwarfLinker::lookForDIEsToKeep
1534 enum TravesalFlags {
1535 TF_Keep = 1 << 0, ///< Mark the traversed DIEs as kept.
1536 TF_InFunctionScope = 1 << 1, ///< Current scope is a function scope.
1537 TF_DependencyWalk = 1 << 2, ///< Walking the dependencies of a kept DIE.
1538 TF_ParentWalk = 1 << 3, ///< Walking up the parents of a kept DIE.
1539 TF_ODR = 1 << 4, ///< Use the ODR while keeping dependents.
1540 TF_SkipPC = 1 << 5, ///< Skip all location attributes.
1541 };
1542
1543 /// Mark the passed DIE as well as all the ones it depends on as kept.
1544 void keepDIEAndDependencies(RelocationManager &RelocMgr, const DWARFDie &DIE,
1545 CompileUnit::DIEInfo &MyInfo,
1546 const DebugMapObject &DMO, CompileUnit &CU,
1547 bool UseODR);
1548
1549 unsigned shouldKeepDIE(RelocationManager &RelocMgr, const DWARFDie &DIE,
1550 CompileUnit &Unit, CompileUnit::DIEInfo &MyInfo,
1551 unsigned Flags);
1552
1553 unsigned shouldKeepVariableDIE(RelocationManager &RelocMgr,
1554 const DWARFDie &DIE, CompileUnit &Unit,
1555 CompileUnit::DIEInfo &MyInfo, unsigned Flags);
1556
1557 unsigned shouldKeepSubprogramDIE(RelocationManager &RelocMgr,
1558 const DWARFDie &DIE, CompileUnit &Unit,
1559 CompileUnit::DIEInfo &MyInfo,
1560 unsigned Flags);
1561
1562 bool hasValidRelocation(uint32_t StartOffset, uint32_t EndOffset,
1563 CompileUnit::DIEInfo &Info);
1564 /// @}
1565
1566 /// \defgroup Linking Methods used to link the debug information
1567 ///
1568 /// @{
1569
1570 class DIECloner {
1571 DwarfLinker &Linker;
1572 RelocationManager &RelocMgr;
1573
1574 /// Allocator used for all the DIEValue objects.
1575 BumpPtrAllocator &DIEAlloc;
1576
1577 std::vector<std::unique_ptr<CompileUnit>> &CompileUnits;
1578 LinkOptions Options;
1579
1580 public:
1581 DIECloner(DwarfLinker &Linker, RelocationManager &RelocMgr,
1582 BumpPtrAllocator &DIEAlloc,
1583 std::vector<std::unique_ptr<CompileUnit>> &CompileUnits,
1584 LinkOptions &Options)
1585 : Linker(Linker), RelocMgr(RelocMgr), DIEAlloc(DIEAlloc),
1586 CompileUnits(CompileUnits), Options(Options) {}
1587
1588 /// Recursively clone \p InputDIE into an tree of DIE objects
1589 /// where useless (as decided by lookForDIEsToKeep()) bits have been
1590 /// stripped out and addresses have been rewritten according to the
1591 /// debug map.
1592 ///
1593 /// \param OutOffset is the offset the cloned DIE in the output
1594 /// compile unit.
1595 /// \param PCOffset (while cloning a function scope) is the offset
1596 /// applied to the entry point of the function to get the linked address.
1597 /// \param Die the output DIE to use, pass NULL to create one.
1598 /// \returns the root of the cloned tree or null if nothing was selected.
1599 DIE *cloneDIE(const DWARFDie &InputDIE, CompileUnit &U, int64_t PCOffset,
1600 uint32_t OutOffset, unsigned Flags, DIE *Die = nullptr);
1601
1602 /// Construct the output DIE tree by cloning the DIEs we
1603 /// chose to keep above. If there are no valid relocs, then there's
1604 /// nothing to clone/emit.
1605 void cloneAllCompileUnits(DWARFContext &DwarfContext);
1606
1607 private:
1608 using AttributeSpec = DWARFAbbreviationDeclaration::AttributeSpec;
1609
1610 /// Information gathered and exchanged between the various
1611 /// clone*Attributes helpers about the attributes of a particular DIE.
1612 struct AttributesInfo {
1613 /// Names.
1614 DwarfStringPoolEntryRef Name, MangledName, NameWithoutTemplate;
1615
1616 /// Offsets in the string pool.
1617 uint32_t NameOffset = 0;
1618 uint32_t MangledNameOffset = 0;
1619
1620 /// Value of AT_low_pc in the input DIE
1621 uint64_t OrigLowPc = std::numeric_limits<uint64_t>::max();
1622
1623 /// Value of AT_high_pc in the input DIE
1624 uint64_t OrigHighPc = 0;
1625
1626 /// Offset to apply to PC addresses inside a function.
1627 int64_t PCOffset = 0;
1628
1629 /// Does the DIE have a low_pc attribute?
1630 bool HasLowPc = false;
1631
1632 /// Does the DIE have a ranges attribute?
1633 bool HasRanges = false;
1634
1635 /// Is this DIE only a declaration?
1636 bool IsDeclaration = false;
1637
1638 AttributesInfo() = default;
1639 };
1640
1641 /// Helper for cloneDIE.
1642 unsigned cloneAttribute(DIE &Die, const DWARFDie &InputDIE, CompileUnit &U,
1643 const DWARFFormValue &Val,
1644 const AttributeSpec AttrSpec, unsigned AttrSize,
1645 AttributesInfo &AttrInfo);
1646
1647 /// Clone a string attribute described by \p AttrSpec and add
1648 /// it to \p Die.
1649 /// \returns the size of the new attribute.
1650 unsigned cloneStringAttribute(DIE &Die, AttributeSpec AttrSpec,
1651 const DWARFFormValue &Val, const DWARFUnit &U,
1652 AttributesInfo &Info);
1653
1654 /// Clone an attribute referencing another DIE and add
1655 /// it to \p Die.
1656 /// \returns the size of the new attribute.
1657 unsigned cloneDieReferenceAttribute(DIE &Die, const DWARFDie &InputDIE,
1658 AttributeSpec AttrSpec,
1659 unsigned AttrSize,
1660 const DWARFFormValue &Val,
1661 CompileUnit &Unit);
1662
1663 /// Clone an attribute referencing another DIE and add
1664 /// it to \p Die.
1665 /// \returns the size of the new attribute.
1666 unsigned cloneBlockAttribute(DIE &Die, AttributeSpec AttrSpec,
1667 const DWARFFormValue &Val, unsigned AttrSize);
1668
1669 /// Clone an attribute referencing another DIE and add
1670 /// it to \p Die.
1671 /// \returns the size of the new attribute.
1672 unsigned cloneAddressAttribute(DIE &Die, AttributeSpec AttrSpec,
1673 const DWARFFormValue &Val,
1674 const CompileUnit &Unit,
1675 AttributesInfo &Info);
1676
1677 /// Clone a scalar attribute and add it to \p Die.
1678 /// \returns the size of the new attribute.
1679 unsigned cloneScalarAttribute(DIE &Die, const DWARFDie &InputDIE,
1680 CompileUnit &U, AttributeSpec AttrSpec,
1681 const DWARFFormValue &Val, unsigned AttrSize,
1682 AttributesInfo &Info);
1683
1684 /// Get the potential name and mangled name for the entity
1685 /// described by \p Die and store them in \Info if they are not
1686 /// already there.
1687 /// \returns is a name was found.
1688 bool getDIENames(const DWARFDie &Die, AttributesInfo &Info,
1689 bool StripTemplate = false);
1690
1691 /// Create a copy of abbreviation Abbrev.
1692 void copyAbbrev(const DWARFAbbreviationDeclaration &Abbrev, bool hasODR);
1693
1694 uint32_t hashFullyQualifiedName(DWARFDie DIE, CompileUnit &U,
1695 int RecurseDepth = 0);
1696
1697 /// Helper for cloneDIE.
1698 void addObjCAccelerator(CompileUnit &Unit, const DIE *Die,
1699 DwarfStringPoolEntryRef Name, bool SkipPubSection);
1700 };
1701
1702 /// Assign an abbreviation number to \p Abbrev
1703 void AssignAbbrev(DIEAbbrev &Abbrev);
1704
1705 /// Compute and emit debug_ranges section for \p Unit, and
1706 /// patch the attributes referencing it.
1707 void patchRangesForUnit(const CompileUnit &Unit, DWARFContext &Dwarf) const;
1708
1709 /// Generate and emit the DW_AT_ranges attribute for a compile_unit if it had
1710 /// one.
1711 void generateUnitRanges(CompileUnit &Unit) const;
1712
1713 /// Extract the line tables from the original dwarf, extract the relevant
1714 /// parts according to the linked function ranges and emit the result in the
1715 /// debug_line section.
1716 void patchLineTableForUnit(CompileUnit &Unit, DWARFContext &OrigDwarf);
1717
1718 /// Emit the accelerator entries for \p Unit.
1719 void emitAcceleratorEntriesForUnit(CompileUnit &Unit);
1720
1721 /// Patch the frame info for an object file and emit it.
1722 void patchFrameInfoForObject(const DebugMapObject &, DWARFContext &,
1723 unsigned AddressSize);
1724
1725 /// FoldingSet that uniques the abbreviations.
1726 FoldingSet<DIEAbbrev> AbbreviationsSet;
1727
1728 /// Storage for the unique Abbreviations.
1729 /// This is passed to AsmPrinter::emitDwarfAbbrevs(), thus it cannot be
1730 /// changed to a vector of unique_ptrs.
1731 std::vector<std::unique_ptr<DIEAbbrev>> Abbreviations;
1732
1733 /// DIELoc objects that need to be destructed (but not freed!).
1734 std::vector<DIELoc *> DIELocs;
1735
1736 /// DIEBlock objects that need to be destructed (but not freed!).
1737 std::vector<DIEBlock *> DIEBlocks;
1738
1739 /// Allocator used for all the DIEValue objects.
1740 BumpPtrAllocator DIEAlloc;
1741 /// @}
1742
1743 /// ODR Contexts for that link.
1744 DeclContextTree ODRContexts;
1745
1746 /// \defgroup Helpers Various helper methods.
1747 ///
1748 /// @{
1749 bool createStreamer(const Triple &TheTriple, raw_fd_ostream &OutFile);
1750
1751 /// Attempt to load a debug object from disk.
1752 ErrorOr<const object::ObjectFile &> loadObject(BinaryHolder &BinaryHolder,
1753 DebugMapObject &Obj,
1754 const DebugMap &Map);
1755 /// @}
1756
1757 raw_fd_ostream &OutFile;
1758 LinkOptions Options;
1759 BinaryHolder BinHolder;
1760 std::unique_ptr<DwarfStreamer> Streamer;
1761 uint64_t OutputDebugInfoSize;
1762
1763 /// A unique ID that identifies each compile unit.
1764 unsigned UnitID;
1765
1766 unsigned MaxDwarfVersion = 0;
1767
1768 /// The units of the current debug map object.
1769 std::vector<std::unique_ptr<CompileUnit>> Units;
1770
1771 /// The debug map object currently under consideration.
1772 DebugMapObject *CurrentDebugObject;
1773
1774 /// The Dwarf string pool.
1775 NonRelocatableStringpool StringPool;
1776
1777 /// This map is keyed by the entry PC of functions in that
1778 /// debug object and the associated value is a pair storing the
1779 /// corresponding end PC and the offset to apply to get the linked
1780 /// address.
1781 ///
1782 /// See startDebugObject() for a more complete description of its use.
1783 std::map<uint64_t, std::pair<uint64_t, int64_t>> Ranges;
1784
1785 /// The CIEs that have been emitted in the output
1786 /// section. The actual CIE data serves a the key to this StringMap,
1787 /// this takes care of comparing the semantics of CIEs defined in
1788 /// different object files.
1789 StringMap<uint32_t> EmittedCIEs;
1790
1791 /// Offset of the last CIE that has been emitted in the output
1792 /// debug_frame section.
1793 uint32_t LastCIEOffset = 0;
1794
1795 /// Apple accelerator tables.
1796 AccelTable<AppleAccelTableStaticOffsetData> AppleNames;
1797 AccelTable<AppleAccelTableStaticOffsetData> AppleNamespaces;
1798 AccelTable<AppleAccelTableStaticOffsetData> AppleObjc;
1799 AccelTable<AppleAccelTableStaticTypeData> AppleTypes;
1800
1801 /// Mapping the PCM filename to the DwoId.
1802 StringMap<uint64_t> ClangModules;
1803
1804 bool ModuleCacheHintDisplayed = false;
1805 bool ArchiveHintDisplayed = false;
1806};
1807
1808} // end anonymous namespace
1809
1810/// Similar to DWARFUnitSection::getUnitForOffset(), but returning our
1811/// CompileUnit object instead.
1812static CompileUnit *
1813getUnitForOffset(std::vector<std::unique_ptr<CompileUnit>> &Units,
1814 unsigned Offset) {
1815 auto CU = std::upper_bound(
1816 Units.begin(), Units.end(), Offset,
1817 [](uint32_t LHS, const std::unique_ptr<CompileUnit> &RHS) {
1818 return LHS < RHS->getOrigUnit().getNextUnitOffset();
1819 });
1820 return CU != Units.end() ? CU->get() : nullptr;
1821}
1822
1823/// Resolve the DIE attribute reference that has been extracted in \p RefValue.
1824/// The resulting DIE might be in another CompileUnit which is stored into \p
1825/// ReferencedCU. \returns null if resolving fails for any reason.
1826static DWARFDie
1827resolveDIEReference(const DwarfLinker &Linker,
1828 std::vector<std::unique_ptr<CompileUnit>> &Units,
1829 const DWARFFormValue &RefValue, const DWARFUnit &Unit,
1830 const DWARFDie &DIE, CompileUnit *&RefCU) {
1831 assert(RefValue.isFormClass(DWARFFormValue::FC_Reference))(static_cast <bool> (RefValue.isFormClass(DWARFFormValue
::FC_Reference)) ? void (0) : __assert_fail ("RefValue.isFormClass(DWARFFormValue::FC_Reference)"
, "/build/llvm-toolchain-snapshot-7~svn326246/tools/dsymutil/DwarfLinker.cpp"
, 1831, __extension__ __PRETTY_FUNCTION__))
;
1832 uint64_t RefOffset = *RefValue.getAsReference();
1833
1834 if ((RefCU = getUnitForOffset(Units, RefOffset)))
1835 if (const auto RefDie = RefCU->getOrigUnit().getDIEForOffset(RefOffset)) {
1836 // In a file with broken references, an attribute might point to a NULL
1837 // DIE.
1838 if (!RefDie.isNULL())
1839 return RefDie;
1840 }
1841
1842 Linker.reportWarning("could not find referenced DIE", &DIE);
1843 return DWARFDie();
1844}
1845
1846/// \returns whether the passed \a Attr type might contain a DIE reference
1847/// suitable for ODR uniquing.
1848static bool isODRAttribute(uint16_t Attr) {
1849 switch (Attr) {
1850 default:
1851 return false;
1852 case dwarf::DW_AT_type:
1853 case dwarf::DW_AT_containing_type:
1854 case dwarf::DW_AT_specification:
1855 case dwarf::DW_AT_abstract_origin:
1856 case dwarf::DW_AT_import:
1857 return true;
1858 }
1859 llvm_unreachable("Improper attribute.")::llvm::llvm_unreachable_internal("Improper attribute.", "/build/llvm-toolchain-snapshot-7~svn326246/tools/dsymutil/DwarfLinker.cpp"
, 1859)
;
1860}
1861
1862/// Set the last DIE/CU a context was seen in and, possibly invalidate the
1863/// context if it is ambiguous.
1864///
1865/// In the current implementation, we don't handle overloaded functions well,
1866/// because the argument types are not taken into account when computing the
1867/// DeclContext tree.
1868///
1869/// Some of this is mitigated byt using mangled names that do contain the
1870/// arguments types, but sometimes (e.g. with function templates) we don't have
1871/// that. In that case, just do not unique anything that refers to the contexts
1872/// we are not able to distinguish.
1873///
1874/// If a context that is not a namespace appears twice in the same CU, we know
1875/// it is ambiguous. Make it invalid.
1876bool DeclContext::setLastSeenDIE(CompileUnit &U, const DWARFDie &Die) {
1877 if (LastSeenCompileUnitID == U.getUniqueID()) {
1878 DWARFUnit &OrigUnit = U.getOrigUnit();
1879 uint32_t FirstIdx = OrigUnit.getDIEIndex(LastSeenDIE);
1880 U.getInfo(FirstIdx).Ctxt = nullptr;
1881 return false;
1882 }
1883
1884 LastSeenCompileUnitID = U.getUniqueID();
1885 LastSeenDIE = Die;
1886 return true;
1887}
1888
1889PointerIntPair<DeclContext *, 1> DeclContextTree::getChildDeclContext(
1890 DeclContext &Context, const DWARFDie &DIE, CompileUnit &U,
1891 NonRelocatableStringpool &StringPool, bool InClangModule) {
1892 unsigned Tag = DIE.getTag();
1893
1894 // FIXME: dsymutil-classic compat: We should bail out here if we
1895 // have a specification or an abstract_origin. We will get the
1896 // parent context wrong here.
1897
1898 switch (Tag) {
1899 default:
1900 // By default stop gathering child contexts.
1901 return PointerIntPair<DeclContext *, 1>(nullptr);
1902 case dwarf::DW_TAG_module:
1903 break;
1904 case dwarf::DW_TAG_compile_unit:
1905 return PointerIntPair<DeclContext *, 1>(&Context);
1906 case dwarf::DW_TAG_subprogram:
1907 // Do not unique anything inside CU local functions.
1908 if ((Context.getTag() == dwarf::DW_TAG_namespace ||
1909 Context.getTag() == dwarf::DW_TAG_compile_unit) &&
1910 !dwarf::toUnsigned(DIE.find(dwarf::DW_AT_external), 0))
1911 return PointerIntPair<DeclContext *, 1>(nullptr);
1912 LLVM_FALLTHROUGH[[clang::fallthrough]];
1913 case dwarf::DW_TAG_member:
1914 case dwarf::DW_TAG_namespace:
1915 case dwarf::DW_TAG_structure_type:
1916 case dwarf::DW_TAG_class_type:
1917 case dwarf::DW_TAG_union_type:
1918 case dwarf::DW_TAG_enumeration_type:
1919 case dwarf::DW_TAG_typedef:
1920 // Artificial things might be ambiguous, because they might be created on
1921 // demand. For example implicitly defined constructors are ambiguous
1922 // because of the way we identify contexts, and they won't be generated
1923 // every time everywhere.
1924 if (dwarf::toUnsigned(DIE.find(dwarf::DW_AT_artificial), 0))
1925 return PointerIntPair<DeclContext *, 1>(nullptr);
1926 break;
1927 }
1928
1929 const char *Name = DIE.getName(DINameKind::LinkageName);
1930 const char *ShortName = DIE.getName(DINameKind::ShortName);
1931 StringRef NameRef;
1932 StringRef ShortNameRef;
1933 StringRef FileRef;
1934
1935 if (Name)
1936 NameRef = StringPool.internString(Name);
1937 else if (Tag == dwarf::DW_TAG_namespace)
1938 // FIXME: For dsymutil-classic compatibility. I think uniquing within
1939 // anonymous namespaces is wrong. There is no ODR guarantee there.
1940 NameRef = StringPool.internString("(anonymous namespace)");
1941
1942 if (ShortName && ShortName != Name)
1943 ShortNameRef = StringPool.internString(ShortName);
1944 else
1945 ShortNameRef = NameRef;
1946
1947 if (Tag != dwarf::DW_TAG_class_type && Tag != dwarf::DW_TAG_structure_type &&
1948 Tag != dwarf::DW_TAG_union_type &&
1949 Tag != dwarf::DW_TAG_enumeration_type && NameRef.empty())
1950 return PointerIntPair<DeclContext *, 1>(nullptr);
1951
1952 unsigned Line = 0;
1953 unsigned ByteSize = std::numeric_limits<uint32_t>::max();
1954
1955 if (!InClangModule) {
1956 // Gather some discriminating data about the DeclContext we will be
1957 // creating: File, line number and byte size. This shouldn't be necessary,
1958 // because the ODR is just about names, but given that we do some
1959 // approximations with overloaded functions and anonymous namespaces, use
1960 // these additional data points to make the process safer.
1961 //
1962 // This is disabled for clang modules, because forward declarations of
1963 // module-defined types do not have a file and line.
1964 ByteSize = dwarf::toUnsigned(DIE.find(dwarf::DW_AT_byte_size),
1965 std::numeric_limits<uint64_t>::max());
1966 if (Tag != dwarf::DW_TAG_namespace || !Name) {
1967 if (unsigned FileNum =
1968 dwarf::toUnsigned(DIE.find(dwarf::DW_AT_decl_file), 0)) {
1969 if (const auto *LT = U.getOrigUnit().getContext().getLineTableForUnit(
1970 &U.getOrigUnit())) {
1971 // FIXME: dsymutil-classic compatibility. I'd rather not
1972 // unique anything in anonymous namespaces, but if we do, then
1973 // verify that the file and line correspond.
1974 if (!Name && Tag == dwarf::DW_TAG_namespace)
1975 FileNum = 1;
1976
1977 if (LT->hasFileAtIndex(FileNum)) {
1978 Line = dwarf::toUnsigned(DIE.find(dwarf::DW_AT_decl_line), 0);
1979 // Cache the resolved paths based on the index in the line table,
1980 // because calling realpath is expansive.
1981 StringRef ResolvedPath = U.getResolvedPath(FileNum);
1982 if (!ResolvedPath.empty()) {
1983 FileRef = ResolvedPath;
1984 } else {
1985 std::string File;
1986 bool FoundFileName = LT->getFileNameByIndex(
1987 FileNum, U.getOrigUnit().getCompilationDir(),
1988 DILineInfoSpecifier::FileLineInfoKind::AbsoluteFilePath,
1989 File);
1990 (void)FoundFileName;
1991 assert(FoundFileName && "Must get file name from line table")(static_cast <bool> (FoundFileName && "Must get file name from line table"
) ? void (0) : __assert_fail ("FoundFileName && \"Must get file name from line table\""
, "/build/llvm-toolchain-snapshot-7~svn326246/tools/dsymutil/DwarfLinker.cpp"
, 1991, __extension__ __PRETTY_FUNCTION__))
;
1992 // Second level of caching, this time based on the file's parent
1993 // path.
1994 FileRef = PathResolver.resolve(File, StringPool);
1995 U.setResolvedPath(FileNum, FileRef);
1996 }
1997 }
1998 }
1999 }
2000 }
2001 }
2002
2003 if (!Line && NameRef.empty())
2004 return PointerIntPair<DeclContext *, 1>(nullptr);
2005
2006 // We hash NameRef, which is the mangled name, in order to get most
2007 // overloaded functions resolve correctly.
2008 //
2009 // Strictly speaking, hashing the Tag is only necessary for a
2010 // DW_TAG_module, to prevent uniquing of a module and a namespace
2011 // with the same name.
2012 //
2013 // FIXME: dsymutil-classic won't unique the same type presented
2014 // once as a struct and once as a class. Using the Tag in the fully
2015 // qualified name hash to get the same effect.
2016 unsigned Hash = hash_combine(Context.getQualifiedNameHash(), Tag, NameRef);
2017
2018 // FIXME: dsymutil-classic compatibility: when we don't have a name,
2019 // use the filename.
2020 if (Tag == dwarf::DW_TAG_namespace && NameRef == "(anonymous namespace)")
2021 Hash = hash_combine(Hash, FileRef);
2022
2023 // Now look if this context already exists.
2024 DeclContext Key(Hash, Line, ByteSize, Tag, NameRef, FileRef, Context);
2025 auto ContextIter = Contexts.find(&Key);
2026
2027 if (ContextIter == Contexts.end()) {
2028 // The context wasn't found.
2029 bool Inserted;
2030 DeclContext *NewContext =
2031 new (Allocator) DeclContext(Hash, Line, ByteSize, Tag, NameRef, FileRef,
2032 Context, DIE, U.getUniqueID());
2033 std::tie(ContextIter, Inserted) = Contexts.insert(NewContext);
2034 assert(Inserted && "Failed to insert DeclContext")(static_cast <bool> (Inserted && "Failed to insert DeclContext"
) ? void (0) : __assert_fail ("Inserted && \"Failed to insert DeclContext\""
, "/build/llvm-toolchain-snapshot-7~svn326246/tools/dsymutil/DwarfLinker.cpp"
, 2034, __extension__ __PRETTY_FUNCTION__))
;
2035 (void)Inserted;
2036 } else if (Tag != dwarf::DW_TAG_namespace &&
2037 !(*ContextIter)->setLastSeenDIE(U, DIE)) {
2038 // The context was found, but it is ambiguous with another context
2039 // in the same file. Mark it invalid.
2040 return PointerIntPair<DeclContext *, 1>(*ContextIter, /* Invalid= */ 1);
2041 }
2042
2043 assert(ContextIter != Contexts.end())(static_cast <bool> (ContextIter != Contexts.end()) ? void
(0) : __assert_fail ("ContextIter != Contexts.end()", "/build/llvm-toolchain-snapshot-7~svn326246/tools/dsymutil/DwarfLinker.cpp"
, 2043, __extension__ __PRETTY_FUNCTION__))
;
2044 // FIXME: dsymutil-classic compatibility. Union types aren't
2045 // uniques, but their children might be.
2046 if ((Tag == dwarf::DW_TAG_subprogram &&
2047 Context.getTag() != dwarf::DW_TAG_structure_type &&
2048 Context.getTag() != dwarf::DW_TAG_class_type) ||
2049 (Tag == dwarf::DW_TAG_union_type))
2050 return PointerIntPair<DeclContext *, 1>(*ContextIter, /* Invalid= */ 1);
2051
2052 return PointerIntPair<DeclContext *, 1>(*ContextIter);
2053}
2054
2055bool DwarfLinker::DIECloner::getDIENames(const DWARFDie &Die,
2056 AttributesInfo &Info,
2057 bool StripTemplate) {
2058 // This function will be called on DIEs having low_pcs and
2059 // ranges. As getting the name might be more expansive, filter out
2060 // blocks directly.
2061 if (Die.getTag() == dwarf::DW_TAG_lexical_block)
2062 return false;
2063
2064 // FIXME: a bit wasteful as the first getName might return the
2065 // short name.
2066 if (!Info.MangledName)
2067 if (const char *MangledName = Die.getName(DINameKind::LinkageName))
2068 Info.MangledName = Linker.StringPool.getEntry(MangledName);
2069
2070 if (!Info.Name)
2071 if (const char *Name = Die.getName(DINameKind::ShortName))
2072 Info.Name = Linker.StringPool.getEntry(Name);
2073
2074 if (StripTemplate && Info.Name && Info.MangledName != Info.Name) {
2075 // FIXME: dsymutil compatibility. This is wrong for operator<
2076 auto Split = Info.Name.getString().split('<');
2077 if (!Split.second.empty())
2078 Info.NameWithoutTemplate = Linker.StringPool.getEntry(Split.first);
2079 }
2080
2081 return Info.Name || Info.MangledName;
2082}
2083
2084/// Report a warning to the user, optionally including information about a
2085/// specific \p DIE related to the warning.
2086void DwarfLinker::reportWarning(const Twine &Warning,
2087 const DWARFDie *DIE) const {
2088 StringRef Context = "<debug map>";
2089 if (CurrentDebugObject)
2090 Context = CurrentDebugObject->getObjectFilename();
2091 warn(Warning, Context);
2092
2093 if (!Options.Verbose || !DIE)
2094 return;
2095
2096 DIDumpOptions DumpOpts;
2097 DumpOpts.RecurseDepth = 0;
2098 DumpOpts.Verbose = Options.Verbose;
2099
2100 errs() << " in DIE:\n";
2101 DIE->dump(errs(), 6 /* Indent */, DumpOpts);
2102}
2103
2104bool DwarfLinker::createStreamer(const Triple &TheTriple,
2105 raw_fd_ostream &OutFile) {
2106 if (Options.NoOutput)
2107 return true;
2108
2109 Streamer = llvm::make_unique<DwarfStreamer>(OutFile);
2110 return Streamer->init(TheTriple);
2111}
2112
2113/// Recursive helper to build the global DeclContext information and
2114/// gather the child->parent relationships in the original compile unit.
2115///
2116/// \return true when this DIE and all of its children are only
2117/// forward declarations to types defined in external clang modules
2118/// (i.e., forward declarations that are children of a DW_TAG_module).
2119static bool analyzeContextInfo(const DWARFDie &DIE, unsigned ParentIdx,
2120 CompileUnit &CU, DeclContext *CurrentDeclContext,
2121 NonRelocatableStringpool &StringPool,
2122 DeclContextTree &Contexts,
2123 bool InImportedModule = false) {
2124 unsigned MyIdx = CU.getOrigUnit().getDIEIndex(DIE);
2125 CompileUnit::DIEInfo &Info = CU.getInfo(MyIdx);
2126
2127 // Clang imposes an ODR on modules(!) regardless of the language:
2128 // "The module-id should consist of only a single identifier,
2129 // which provides the name of the module being defined. Each
2130 // module shall have a single definition."
2131 //
2132 // This does not extend to the types inside the modules:
2133 // "[I]n C, this implies that if two structs are defined in
2134 // different submodules with the same name, those two types are
2135 // distinct types (but may be compatible types if their
2136 // definitions match)."
2137 //
2138 // We treat non-C++ modules like namespaces for this reason.
2139 if (DIE.getTag() == dwarf::DW_TAG_module && ParentIdx == 0 &&
2140 dwarf::toString(DIE.find(dwarf::DW_AT_name), "") !=
2141 CU.getClangModuleName()) {
2142 InImportedModule = true;
2143 }
2144
2145 Info.ParentIdx = ParentIdx;
2146 bool InClangModule = CU.isClangModule() || InImportedModule;
2147 if (CU.hasODR() || InClangModule) {
2148 if (CurrentDeclContext) {
2149 auto PtrInvalidPair = Contexts.getChildDeclContext(
2150 *CurrentDeclContext, DIE, CU, StringPool, InClangModule);
2151 CurrentDeclContext = PtrInvalidPair.getPointer();
2152 Info.Ctxt =
2153 PtrInvalidPair.getInt() ? nullptr : PtrInvalidPair.getPointer();
2154 if (Info.Ctxt)
2155 Info.Ctxt->setDefinedInClangModule(InClangModule);
2156 } else
2157 Info.Ctxt = CurrentDeclContext = nullptr;
2158 }
2159
2160 Info.Prune = InImportedModule;
2161 if (DIE.hasChildren())
2162 for (auto Child : DIE.children())
2163 Info.Prune &= analyzeContextInfo(Child, MyIdx, CU, CurrentDeclContext,
2164 StringPool, Contexts, InImportedModule);
2165
2166 // Prune this DIE if it is either a forward declaration inside a
2167 // DW_TAG_module or a DW_TAG_module that contains nothing but
2168 // forward declarations.
2169 Info.Prune &= (DIE.getTag() == dwarf::DW_TAG_module) ||
2170 dwarf::toUnsigned(DIE.find(dwarf::DW_AT_declaration), 0);
2171
2172 // Don't prune it if there is no definition for the DIE.
2173 Info.Prune &= Info.Ctxt && Info.Ctxt->getCanonicalDIEOffset();
2174
2175 return Info.Prune;
2176}
2177
2178static bool dieNeedsChildrenToBeMeaningful(uint32_t Tag) {
2179 switch (Tag) {
2180 default:
2181 return false;
2182 case dwarf::DW_TAG_subprogram:
2183 case dwarf::DW_TAG_lexical_block:
2184 case dwarf::DW_TAG_subroutine_type:
2185 case dwarf::DW_TAG_structure_type:
2186 case dwarf::DW_TAG_class_type:
2187 case dwarf::DW_TAG_union_type:
2188 return true;
2189 }
2190 llvm_unreachable("Invalid Tag")::llvm::llvm_unreachable_internal("Invalid Tag", "/build/llvm-toolchain-snapshot-7~svn326246/tools/dsymutil/DwarfLinker.cpp"
, 2190)
;
2191}
2192
2193void DwarfLinker::startDebugObject(DWARFContext &Dwarf, DebugMapObject &Obj) {
2194 // Iterate over the debug map entries and put all the ones that are
2195 // functions (because they have a size) into the Ranges map. This
2196 // map is very similar to the FunctionRanges that are stored in each
2197 // unit, with 2 notable differences:
2198 // - obviously this one is global, while the other ones are per-unit.
2199 // - this one contains not only the functions described in the DIE
2200 // tree, but also the ones that are only in the debug map.
2201 // The latter information is required to reproduce dsymutil's logic
2202 // while linking line tables. The cases where this information
2203 // matters look like bugs that need to be investigated, but for now
2204 // we need to reproduce dsymutil's behavior.
2205 // FIXME: Once we understood exactly if that information is needed,
2206 // maybe totally remove this (or try to use it to do a real
2207 // -gline-tables-only on Darwin.
2208 for (const auto &Entry : Obj.symbols()) {
2209 const auto &Mapping = Entry.getValue();
2210 if (Mapping.Size && Mapping.ObjectAddress)
2211 Ranges[*Mapping.ObjectAddress] = std::make_pair(
2212 *Mapping.ObjectAddress + Mapping.Size,
2213 int64_t(Mapping.BinaryAddress) - *Mapping.ObjectAddress);
2214 }
2215}
2216
2217void DwarfLinker::endDebugObject() {
2218 Units.clear();
2219 Ranges.clear();
2220
2221 for (auto I = DIEBlocks.begin(), E = DIEBlocks.end(); I != E; ++I)
2222 (*I)->~DIEBlock();
2223 for (auto I = DIELocs.begin(), E = DIELocs.end(); I != E; ++I)
2224 (*I)->~DIELoc();
2225
2226 DIEBlocks.clear();
2227 DIELocs.clear();
2228 DIEAlloc.Reset();
2229}
2230
2231static bool isMachOPairedReloc(uint64_t RelocType, uint64_t Arch) {
2232 switch (Arch) {
2233 case Triple::x86:
2234 return RelocType == MachO::GENERIC_RELOC_SECTDIFF ||
2235 RelocType == MachO::GENERIC_RELOC_LOCAL_SECTDIFF;
2236 case Triple::x86_64:
2237 return RelocType == MachO::X86_64_RELOC_SUBTRACTOR;
2238 case Triple::arm:
2239 case Triple::thumb:
2240 return RelocType == MachO::ARM_RELOC_SECTDIFF ||
2241 RelocType == MachO::ARM_RELOC_LOCAL_SECTDIFF ||
2242 RelocType == MachO::ARM_RELOC_HALF ||
2243 RelocType == MachO::ARM_RELOC_HALF_SECTDIFF;
2244 case Triple::aarch64:
2245 return RelocType == MachO::ARM64_RELOC_SUBTRACTOR;
2246 default:
2247 return false;
2248 }
2249}
2250
2251/// Iterate over the relocations of the given \p Section and
2252/// store the ones that correspond to debug map entries into the
2253/// ValidRelocs array.
2254void DwarfLinker::RelocationManager::findValidRelocsMachO(
2255 const object::SectionRef &Section, const object::MachOObjectFile &Obj,
2256 const DebugMapObject &DMO) {
2257 StringRef Contents;
2258 Section.getContents(Contents);
2259 DataExtractor Data(Contents, Obj.isLittleEndian(), 0);
2260 bool SkipNext = false;
2261
2262 for (const object::RelocationRef &Reloc : Section.relocations()) {
2263 if (SkipNext) {
2264 SkipNext = false;
2265 continue;
2266 }
2267
2268 object::DataRefImpl RelocDataRef = Reloc.getRawDataRefImpl();
2269 MachO::any_relocation_info MachOReloc = Obj.getRelocation(RelocDataRef);
2270
2271 if (isMachOPairedReloc(Obj.getAnyRelocationType(MachOReloc),
2272 Obj.getArch())) {
2273 SkipNext = true;
2274 Linker.reportWarning(" unsupported relocation in debug_info section.");
2275 continue;
2276 }
2277
2278 unsigned RelocSize = 1 << Obj.getAnyRelocationLength(MachOReloc);
2279 uint64_t Offset64 = Reloc.getOffset();
2280 if ((RelocSize != 4 && RelocSize != 8)) {
2281 Linker.reportWarning(" unsupported relocation in debug_info section.");
2282 continue;
2283 }
2284 uint32_t Offset = Offset64;
2285 // Mach-o uses REL relocations, the addend is at the relocation offset.
2286 uint64_t Addend = Data.getUnsigned(&Offset, RelocSize);
2287 uint64_t SymAddress;
2288 int64_t SymOffset;
2289
2290 if (Obj.isRelocationScattered(MachOReloc)) {
2291 // The address of the base symbol for scattered relocations is
2292 // stored in the reloc itself. The actual addend will store the
2293 // base address plus the offset.
2294 SymAddress = Obj.getScatteredRelocationValue(MachOReloc);
2295 SymOffset = int64_t(Addend) - SymAddress;
2296 } else {
2297 SymAddress = Addend;
2298 SymOffset = 0;
2299 }
2300
2301 auto Sym = Reloc.getSymbol();
2302 if (Sym != Obj.symbol_end()) {
2303 Expected<StringRef> SymbolName = Sym->getName();
2304 if (!SymbolName) {
2305 consumeError(SymbolName.takeError());
2306 Linker.reportWarning("error getting relocation symbol name.");
2307 continue;
2308 }
2309 if (const auto *Mapping = DMO.lookupSymbol(*SymbolName))
2310 ValidRelocs.emplace_back(Offset64, RelocSize, Addend, Mapping);
2311 } else if (const auto *Mapping = DMO.lookupObjectAddress(SymAddress)) {
2312 // Do not store the addend. The addend was the address of the symbol in
2313 // the object file, the address in the binary that is stored in the debug
2314 // map doesn't need to be offset.
2315 ValidRelocs.emplace_back(Offset64, RelocSize, SymOffset, Mapping);
2316 }
2317 }
2318}
2319
2320/// Dispatch the valid relocation finding logic to the
2321/// appropriate handler depending on the object file format.
2322bool DwarfLinker::RelocationManager::findValidRelocs(
2323 const object::SectionRef &Section, const object::ObjectFile &Obj,
2324 const DebugMapObject &DMO) {
2325 // Dispatch to the right handler depending on the file type.
2326 if (auto *MachOObj = dyn_cast<object::MachOObjectFile>(&Obj))
2327 findValidRelocsMachO(Section, *MachOObj, DMO);
2328 else
2329 Linker.reportWarning(Twine("unsupported object file type: ") +
2330 Obj.getFileName());
2331
2332 if (ValidRelocs.empty())
2333 return false;
2334
2335 // Sort the relocations by offset. We will walk the DIEs linearly in
2336 // the file, this allows us to just keep an index in the relocation
2337 // array that we advance during our walk, rather than resorting to
2338 // some associative container. See DwarfLinker::NextValidReloc.
2339 std::sort(ValidRelocs.begin(), ValidRelocs.end());
2340 return true;
2341}
2342
2343/// Look for relocations in the debug_info section that match
2344/// entries in the debug map. These relocations will drive the Dwarf
2345/// link by indicating which DIEs refer to symbols present in the
2346/// linked binary.
2347/// \returns whether there are any valid relocations in the debug info.
2348bool DwarfLinker::RelocationManager::findValidRelocsInDebugInfo(
2349 const object::ObjectFile &Obj, const DebugMapObject &DMO) {
2350 // Find the debug_info section.
2351 for (const object::SectionRef &Section : Obj.sections()) {
2352 StringRef SectionName;
2353 Section.getName(SectionName);
2354 SectionName = SectionName.substr(SectionName.find_first_not_of("._"));
2355 if (SectionName != "debug_info")
2356 continue;
2357 return findValidRelocs(Section, Obj, DMO);
2358 }
2359 return false;
2360}
2361
2362/// Checks that there is a relocation against an actual debug
2363/// map entry between \p StartOffset and \p NextOffset.
2364///
2365/// This function must be called with offsets in strictly ascending
2366/// order because it never looks back at relocations it already 'went past'.
2367/// \returns true and sets Info.InDebugMap if it is the case.
2368bool DwarfLinker::RelocationManager::hasValidRelocation(
2369 uint32_t StartOffset, uint32_t EndOffset, CompileUnit::DIEInfo &Info) {
2370 assert(NextValidReloc == 0 ||(static_cast <bool> (NextValidReloc == 0 || StartOffset
> ValidRelocs[NextValidReloc - 1].Offset) ? void (0) : __assert_fail
("NextValidReloc == 0 || StartOffset > ValidRelocs[NextValidReloc - 1].Offset"
, "/build/llvm-toolchain-snapshot-7~svn326246/tools/dsymutil/DwarfLinker.cpp"
, 2371, __extension__ __PRETTY_FUNCTION__))
2371 StartOffset > ValidRelocs[NextValidReloc - 1].Offset)(static_cast <bool> (NextValidReloc == 0 || StartOffset
> ValidRelocs[NextValidReloc - 1].Offset) ? void (0) : __assert_fail
("NextValidReloc == 0 || StartOffset > ValidRelocs[NextValidReloc - 1].Offset"
, "/build/llvm-toolchain-snapshot-7~svn326246/tools/dsymutil/DwarfLinker.cpp"
, 2371, __extension__ __PRETTY_FUNCTION__))
;
2372 if (NextValidReloc >= ValidRelocs.size())
2373 return false;
2374
2375 uint64_t RelocOffset = ValidRelocs[NextValidReloc].Offset;
2376
2377 // We might need to skip some relocs that we didn't consider. For
2378 // example the high_pc of a discarded DIE might contain a reloc that
2379 // is in the list because it actually corresponds to the start of a
2380 // function that is in the debug map.
2381 while (RelocOffset < StartOffset && NextValidReloc < ValidRelocs.size() - 1)
2382 RelocOffset = ValidRelocs[++NextValidReloc].Offset;
2383
2384 if (RelocOffset < StartOffset || RelocOffset >= EndOffset)
2385 return false;
2386
2387 const auto &ValidReloc = ValidRelocs[NextValidReloc++];
2388 const auto &Mapping = ValidReloc.Mapping->getValue();
2389 uint64_t ObjectAddress = Mapping.ObjectAddress
2390 ? uint64_t(*Mapping.ObjectAddress)
2391 : std::numeric_limits<uint64_t>::max();
2392 if (Linker.Options.Verbose)
2393 outs() << "Found valid debug map entry: " << ValidReloc.Mapping->getKey()
2394 << " "
2395 << format("\t%016" PRIx64"l" "x" " => %016" PRIx64"l" "x", ObjectAddress,
2396 uint64_t(Mapping.BinaryAddress));
2397
2398 Info.AddrAdjust = int64_t(Mapping.BinaryAddress) + ValidReloc.Addend;
2399 if (Mapping.ObjectAddress)
2400 Info.AddrAdjust -= ObjectAddress;
2401 Info.InDebugMap = true;
2402 return true;
2403}
2404
2405/// Get the starting and ending (exclusive) offset for the
2406/// attribute with index \p Idx descibed by \p Abbrev. \p Offset is
2407/// supposed to point to the position of the first attribute described
2408/// by \p Abbrev.
2409/// \return [StartOffset, EndOffset) as a pair.
2410static std::pair<uint32_t, uint32_t>
2411getAttributeOffsets(const DWARFAbbreviationDeclaration *Abbrev, unsigned Idx,
2412 unsigned Offset, const DWARFUnit &Unit) {
2413 DataExtractor Data = Unit.getDebugInfoExtractor();
2414
2415 for (unsigned i = 0; i < Idx; ++i)
2416 DWARFFormValue::skipValue(Abbrev->getFormByIndex(i), Data, &Offset,
2417 Unit.getFormParams());
2418
2419 uint32_t End = Offset;
2420 DWARFFormValue::skipValue(Abbrev->getFormByIndex(Idx), Data, &End,
2421 Unit.getFormParams());
2422
2423 return std::make_pair(Offset, End);
2424}
2425
2426/// Check if a variable describing DIE should be kept.
2427/// \returns updated TraversalFlags.
2428unsigned DwarfLinker::shouldKeepVariableDIE(RelocationManager &RelocMgr,
2429 const DWARFDie &DIE,
2430 CompileUnit &Unit,
2431 CompileUnit::DIEInfo &MyInfo,
2432 unsigned Flags) {
2433 const auto *Abbrev = DIE.getAbbreviationDeclarationPtr();
2434
2435 // Global variables with constant value can always be kept.
2436 if (!(Flags & TF_InFunctionScope) &&
2437 Abbrev->findAttributeIndex(dwarf::DW_AT_const_value)) {
2438 MyInfo.InDebugMap = true;
2439 return Flags | TF_Keep;
2440 }
2441
2442 Optional<uint32_t> LocationIdx =
2443 Abbrev->findAttributeIndex(dwarf::DW_AT_location);
2444 if (!LocationIdx)
2445 return Flags;
2446
2447 uint32_t Offset = DIE.getOffset() + getULEB128Size(Abbrev->getCode());
2448 const DWARFUnit &OrigUnit = Unit.getOrigUnit();
2449 uint32_t LocationOffset, LocationEndOffset;
2450 std::tie(LocationOffset, LocationEndOffset) =
2451 getAttributeOffsets(Abbrev, *LocationIdx, Offset, OrigUnit);
2452
2453 // See if there is a relocation to a valid debug map entry inside
2454 // this variable's location. The order is important here. We want to
2455 // always check in the variable has a valid relocation, so that the
2456 // DIEInfo is filled. However, we don't want a static variable in a
2457 // function to force us to keep the enclosing function.
2458 if (!RelocMgr.hasValidRelocation(LocationOffset, LocationEndOffset, MyInfo) ||
2459 (Flags & TF_InFunctionScope))
2460 return Flags;
2461
2462 if (Options.Verbose) {
2463 DIDumpOptions DumpOpts;
2464 DumpOpts.RecurseDepth = 0;
2465 DumpOpts.Verbose = Options.Verbose;
2466 DIE.dump(outs(), 8 /* Indent */, DumpOpts);
2467 }
2468
2469 return Flags | TF_Keep;
2470}
2471
2472/// Check if a function describing DIE should be kept.
2473/// \returns updated TraversalFlags.
2474unsigned DwarfLinker::shouldKeepSubprogramDIE(RelocationManager &RelocMgr,
2475 const DWARFDie &DIE,
2476 CompileUnit &Unit,
2477 CompileUnit::DIEInfo &MyInfo,
2478 unsigned Flags) {
2479 const auto *Abbrev = DIE.getAbbreviationDeclarationPtr();
2480
2481 Flags |= TF_InFunctionScope;
2482
2483 Optional<uint32_t> LowPcIdx = Abbrev->findAttributeIndex(dwarf::DW_AT_low_pc);
2484 if (!LowPcIdx)
2485 return Flags;
2486
2487 uint32_t Offset = DIE.getOffset() + getULEB128Size(Abbrev->getCode());
2488 DWARFUnit &OrigUnit = Unit.getOrigUnit();
2489 uint32_t LowPcOffset, LowPcEndOffset;
2490 std::tie(LowPcOffset, LowPcEndOffset) =
2491 getAttributeOffsets(Abbrev, *LowPcIdx, Offset, OrigUnit);
2492
2493 auto LowPc = dwarf::toAddress(DIE.find(dwarf::DW_AT_low_pc));
2494 assert(LowPc.hasValue() && "low_pc attribute is not an address.")(static_cast <bool> (LowPc.hasValue() && "low_pc attribute is not an address."
) ? void (0) : __assert_fail ("LowPc.hasValue() && \"low_pc attribute is not an address.\""
, "/build/llvm-toolchain-snapshot-7~svn326246/tools/dsymutil/DwarfLinker.cpp"
, 2494, __extension__ __PRETTY_FUNCTION__))
;
2495 if (!LowPc ||
2496 !RelocMgr.hasValidRelocation(LowPcOffset, LowPcEndOffset, MyInfo))
2497 return Flags;
2498
2499 if (Options.Verbose) {
2500 DIDumpOptions DumpOpts;
2501 DumpOpts.RecurseDepth = 0;
2502 DumpOpts.Verbose = Options.Verbose;
2503 DIE.dump(outs(), 8 /* Indent */, DumpOpts);
2504 }
2505
2506 if (DIE.getTag() == dwarf::DW_TAG_label) {
2507 if (Unit.hasLabelAt(*LowPc))
2508 return Flags;
2509 // FIXME: dsymutil-classic compat. dsymutil-classic doesn't consider labels
2510 // that don't fall into the CU's aranges. This is wrong IMO. Debug info
2511 // generation bugs aside, this is really wrong in the case of labels, where
2512 // a label marking the end of a function will have a PC == CU's high_pc.
2513 if (dwarf::toAddress(OrigUnit.getUnitDIE().find(dwarf::DW_AT_high_pc))
2514 .getValueOr(UINT64_MAX(18446744073709551615UL)) <= LowPc)
2515 return Flags;
2516 Unit.addLabelLowPc(*LowPc, MyInfo.AddrAdjust);
2517 return Flags | TF_Keep;
2518 }
2519
2520 Flags |= TF_Keep;
2521
2522 Optional<uint64_t> HighPc = DIE.getHighPC(*LowPc);
2523 if (!HighPc) {
2524 reportWarning("Function without high_pc. Range will be discarded.\n", &DIE);
2525 return Flags;
2526 }
2527
2528 // Replace the debug map range with a more accurate one.
2529 Ranges[*LowPc] = std::make_pair(*HighPc, MyInfo.AddrAdjust);
2530 Unit.addFunctionRange(*LowPc, *HighPc, MyInfo.AddrAdjust);
2531 return Flags;
2532}
2533
2534/// Check if a DIE should be kept.
2535/// \returns updated TraversalFlags.
2536unsigned DwarfLinker::shouldKeepDIE(RelocationManager &RelocMgr,
2537 const DWARFDie &DIE, CompileUnit &Unit,
2538 CompileUnit::DIEInfo &MyInfo,
2539 unsigned Flags) {
2540 switch (DIE.getTag()) {
2541 case dwarf::DW_TAG_constant:
2542 case dwarf::DW_TAG_variable:
2543 return shouldKeepVariableDIE(RelocMgr, DIE, Unit, MyInfo, Flags);
2544 case dwarf::DW_TAG_subprogram:
2545 case dwarf::DW_TAG_label:
2546 return shouldKeepSubprogramDIE(RelocMgr, DIE, Unit, MyInfo, Flags);
2547 case dwarf::DW_TAG_imported_module:
2548 case dwarf::DW_TAG_imported_declaration:
2549 case dwarf::DW_TAG_imported_unit:
2550 // We always want to keep these.
2551 return Flags | TF_Keep;
2552 default:
2553 break;
2554 }
2555
2556 return Flags;
2557}
2558
2559/// Mark the passed DIE as well as all the ones it depends on
2560/// as kept.
2561///
2562/// This function is called by lookForDIEsToKeep on DIEs that are
2563/// newly discovered to be needed in the link. It recursively calls
2564/// back to lookForDIEsToKeep while adding TF_DependencyWalk to the
2565/// TraversalFlags to inform it that it's not doing the primary DIE
2566/// tree walk.
2567void DwarfLinker::keepDIEAndDependencies(RelocationManager &RelocMgr,
2568 const DWARFDie &Die,
2569 CompileUnit::DIEInfo &MyInfo,
2570 const DebugMapObject &DMO,
2571 CompileUnit &CU, bool UseODR) {
2572 DWARFUnit &Unit = CU.getOrigUnit();
2573 MyInfo.Keep = true;
2574
2575 // We're looking for incomplete types.
2576 MyInfo.Incomplete = Die.getTag() != dwarf::DW_TAG_subprogram &&
2577 Die.getTag() != dwarf::DW_TAG_member &&
2578 dwarf::toUnsigned(Die.find(dwarf::DW_AT_declaration), 0);
2579
2580 // First mark all the parent chain as kept.
2581 unsigned AncestorIdx = MyInfo.ParentIdx;
2582 while (!CU.getInfo(AncestorIdx).Keep) {
2583 unsigned ODRFlag = UseODR ? TF_ODR : 0;
2584 lookForDIEsToKeep(RelocMgr, Unit.getDIEAtIndex(AncestorIdx), DMO, CU,
2585 TF_ParentWalk | TF_Keep | TF_DependencyWalk | ODRFlag);
2586 AncestorIdx = CU.getInfo(AncestorIdx).ParentIdx;
2587 }
2588
2589 // Then we need to mark all the DIEs referenced by this DIE's
2590 // attributes as kept.
2591 DWARFDataExtractor Data = Unit.getDebugInfoExtractor();
2592 const auto *Abbrev = Die.getAbbreviationDeclarationPtr();
2593 uint32_t Offset = Die.getOffset() + getULEB128Size(Abbrev->getCode());
2594
2595 // Mark all DIEs referenced through attributes as kept.
2596 for (const auto &AttrSpec : Abbrev->attributes()) {
2597 DWARFFormValue Val(AttrSpec.Form);
2598
2599 if (!Val.isFormClass(DWARFFormValue::FC_Reference) ||
2600 AttrSpec.Attr == dwarf::DW_AT_sibling) {
2601 DWARFFormValue::skipValue(AttrSpec.Form, Data, &Offset,
2602 Unit.getFormParams());
2603 continue;
2604 }
2605
2606 Val.extractValue(Data, &Offset, Unit.getFormParams(), &Unit);
2607 CompileUnit *ReferencedCU;
2608 if (auto RefDie =
2609 resolveDIEReference(*this, Units, Val, Unit, Die, ReferencedCU)) {
2610 uint32_t RefIdx = ReferencedCU->getOrigUnit().getDIEIndex(RefDie);
2611 CompileUnit::DIEInfo &Info = ReferencedCU->getInfo(RefIdx);
2612 bool IsModuleRef = Info.Ctxt && Info.Ctxt->getCanonicalDIEOffset() &&
2613 Info.Ctxt->isDefinedInClangModule();
2614 // If the referenced DIE has a DeclContext that has already been
2615 // emitted, then do not keep the one in this CU. We'll link to
2616 // the canonical DIE in cloneDieReferenceAttribute.
2617 // FIXME: compatibility with dsymutil-classic. UseODR shouldn't
2618 // be necessary and could be advantageously replaced by
2619 // ReferencedCU->hasODR() && CU.hasODR().
2620 // FIXME: compatibility with dsymutil-classic. There is no
2621 // reason not to unique ref_addr references.
2622 if (AttrSpec.Form != dwarf::DW_FORM_ref_addr && (UseODR || IsModuleRef) &&
2623 Info.Ctxt &&
2624 Info.Ctxt != ReferencedCU->getInfo(Info.ParentIdx).Ctxt &&
2625 Info.Ctxt->getCanonicalDIEOffset() && isODRAttribute(AttrSpec.Attr))
2626 continue;
2627
2628 // Keep a module forward declaration if there is no definition.
2629 if (!(isODRAttribute(AttrSpec.Attr) && Info.Ctxt &&
2630 Info.Ctxt->getCanonicalDIEOffset()))
2631 Info.Prune = false;
2632
2633 unsigned ODRFlag = UseODR ? TF_ODR : 0;
2634 lookForDIEsToKeep(RelocMgr, RefDie, DMO, *ReferencedCU,
2635 TF_Keep | TF_DependencyWalk | ODRFlag);
2636
2637 // The incomplete property is propagated if the current DIE is complete
2638 // but references an incomplete DIE.
2639 if (Info.Incomplete && !MyInfo.Incomplete &&
2640 (Die.getTag() == dwarf::DW_TAG_typedef ||
2641 Die.getTag() == dwarf::DW_TAG_member ||
2642 Die.getTag() == dwarf::DW_TAG_reference_type ||
2643 Die.getTag() == dwarf::DW_TAG_ptr_to_member_type ||
2644 Die.getTag() == dwarf::DW_TAG_pointer_type))
2645 MyInfo.Incomplete = true;
2646 }
2647 }
2648}
2649
2650/// Recursively walk the \p DIE tree and look for DIEs to
2651/// keep. Store that information in \p CU's DIEInfo.
2652///
2653/// This function is the entry point of the DIE selection
2654/// algorithm. It is expected to walk the DIE tree in file order and
2655/// (though the mediation of its helper) call hasValidRelocation() on
2656/// each DIE that might be a 'root DIE' (See DwarfLinker class
2657/// comment).
2658/// While walking the dependencies of root DIEs, this function is
2659/// also called, but during these dependency walks the file order is
2660/// not respected. The TF_DependencyWalk flag tells us which kind of
2661/// traversal we are currently doing.
2662///
2663/// The return value indicates whether the DIE is incomplete.
2664bool DwarfLinker::lookForDIEsToKeep(RelocationManager &RelocMgr,
2665 const DWARFDie &Die,
2666 const DebugMapObject &DMO, CompileUnit &CU,
2667 unsigned Flags) {
2668 unsigned Idx = CU.getOrigUnit().getDIEIndex(Die);
2669 CompileUnit::DIEInfo &MyInfo = CU.getInfo(Idx);
2670 bool AlreadyKept = MyInfo.Keep;
2671 if (MyInfo.Prune)
2672 return true;
2673
2674 // If the Keep flag is set, we are marking a required DIE's
2675 // dependencies. If our target is already marked as kept, we're all
2676 // set.
2677 if ((Flags & TF_DependencyWalk) && AlreadyKept)
2678 return MyInfo.Incomplete;
2679
2680 // We must not call shouldKeepDIE while called from keepDIEAndDependencies,
2681 // because it would screw up the relocation finding logic.
2682 if (!(Flags & TF_DependencyWalk))
2683 Flags = shouldKeepDIE(RelocMgr, Die, CU, MyInfo, Flags);
2684
2685 // If it is a newly kept DIE mark it as well as all its dependencies as kept.
2686 if (!AlreadyKept && (Flags & TF_Keep)) {
2687 bool UseOdr = (Flags & TF_DependencyWalk) ? (Flags & TF_ODR) : CU.hasODR();
2688 keepDIEAndDependencies(RelocMgr, Die, MyInfo, DMO, CU, UseOdr);
2689 }
2690 // The TF_ParentWalk flag tells us that we are currently walking up
2691 // the parent chain of a required DIE, and we don't want to mark all
2692 // the children of the parents as kept (consider for example a
2693 // DW_TAG_namespace node in the parent chain). There are however a
2694 // set of DIE types for which we want to ignore that directive and still
2695 // walk their children.
2696 if (dieNeedsChildrenToBeMeaningful(Die.getTag()))
2697 Flags &= ~TF_ParentWalk;
2698
2699 if (!Die.hasChildren() || (Flags & TF_ParentWalk))
2700 return MyInfo.Incomplete;
2701
2702 bool Incomplete = false;
2703 for (auto Child : Die.children()) {
2704 Incomplete |= lookForDIEsToKeep(RelocMgr, Child, DMO, CU, Flags);
2705
2706 // If any of the members are incomplete we propagate the incompleteness.
2707 if (!MyInfo.Incomplete && Incomplete &&
2708 (Die.getTag() == dwarf::DW_TAG_structure_type ||
2709 Die.getTag() == dwarf::DW_TAG_class_type))
2710 MyInfo.Incomplete = true;
2711 }
2712 return MyInfo.Incomplete;
2713}
2714
2715/// Assign an abbreviation number to \p Abbrev.
2716///
2717/// Our DIEs get freed after every DebugMapObject has been processed,
2718/// thus the FoldingSet we use to unique DIEAbbrevs cannot refer to
2719/// the instances hold by the DIEs. When we encounter an abbreviation
2720/// that we don't know, we create a permanent copy of it.
2721void DwarfLinker::AssignAbbrev(DIEAbbrev &Abbrev) {
2722 // Check the set for priors.
2723 FoldingSetNodeID ID;
2724 Abbrev.Profile(ID);
2725 void *InsertToken;
2726 DIEAbbrev *InSet = AbbreviationsSet.FindNodeOrInsertPos(ID, InsertToken);
2727
2728 // If it's newly added.
2729 if (InSet) {
2730 // Assign existing abbreviation number.
2731 Abbrev.setNumber(InSet->getNumber());
2732 } else {
2733 // Add to abbreviation list.
2734 Abbreviations.push_back(
2735 llvm::make_unique<DIEAbbrev>(Abbrev.getTag(), Abbrev.hasChildren()));
2736 for (const auto &Attr : Abbrev.getData())
2737 Abbreviations.back()->AddAttribute(Attr.getAttribute(), Attr.getForm());
2738 AbbreviationsSet.InsertNode(Abbreviations.back().get(), InsertToken);
2739 // Assign the unique abbreviation number.
2740 Abbrev.setNumber(Abbreviations.size());
2741 Abbreviations.back()->setNumber(Abbreviations.size());
2742 }
2743}
2744
2745unsigned DwarfLinker::DIECloner::cloneStringAttribute(DIE &Die,
2746 AttributeSpec AttrSpec,
2747 const DWARFFormValue &Val,
2748 const DWARFUnit &U,
2749 AttributesInfo &Info) {
2750 // Switch everything to out of line strings.
2751 const char *String = *Val.getAsCString();
2752 auto StringEntry = Linker.StringPool.getEntry(String);
2753 if (AttrSpec.Attr == dwarf::DW_AT_name)
2754 Info.Name = StringEntry;
2755 else if (AttrSpec.Attr == dwarf::DW_AT_MIPS_linkage_name ||
2756 AttrSpec.Attr == dwarf::DW_AT_linkage_name)
2757 Info.MangledName = StringEntry;
2758 Die.addValue(DIEAlloc, dwarf::Attribute(AttrSpec.Attr), dwarf::DW_FORM_strp,
2759 DIEInteger(StringEntry.getOffset()));
2760 return 4;
2761}
2762
2763unsigned DwarfLinker::DIECloner::cloneDieReferenceAttribute(
2764 DIE &Die, const DWARFDie &InputDIE, AttributeSpec AttrSpec,
2765 unsigned AttrSize, const DWARFFormValue &Val, CompileUnit &Unit) {
2766 const DWARFUnit &U = Unit.getOrigUnit();
2767 uint32_t Ref = *Val.getAsReference();
2768 DIE *NewRefDie = nullptr;
2769 CompileUnit *RefUnit = nullptr;
2770 DeclContext *Ctxt = nullptr;
2771
2772 DWARFDie RefDie =
2773 resolveDIEReference(Linker, CompileUnits, Val, U, InputDIE, RefUnit);
2774
2775 // If the referenced DIE is not found, drop the attribute.
2776 if (!RefDie || AttrSpec.Attr == dwarf::DW_AT_sibling)
2777 return 0;
2778
2779 unsigned Idx = RefUnit->getOrigUnit().getDIEIndex(RefDie);
2780 CompileUnit::DIEInfo &RefInfo = RefUnit->getInfo(Idx);
2781
2782 // If we already have emitted an equivalent DeclContext, just point
2783 // at it.
2784 if (isODRAttribute(AttrSpec.Attr)) {
2785 Ctxt = RefInfo.Ctxt;
2786 if (Ctxt && Ctxt->getCanonicalDIEOffset()) {
2787 DIEInteger Attr(Ctxt->getCanonicalDIEOffset());
2788 Die.addValue(DIEAlloc, dwarf::Attribute(AttrSpec.Attr),
2789 dwarf::DW_FORM_ref_addr, Attr);
2790 return U.getRefAddrByteSize();
2791 }
2792 }
2793
2794 if (!RefInfo.Clone) {
2795 assert(Ref > InputDIE.getOffset())(static_cast <bool> (Ref > InputDIE.getOffset()) ? void
(0) : __assert_fail ("Ref > InputDIE.getOffset()", "/build/llvm-toolchain-snapshot-7~svn326246/tools/dsymutil/DwarfLinker.cpp"
, 2795, __extension__ __PRETTY_FUNCTION__))
;
2796 // We haven't cloned this DIE yet. Just create an empty one and
2797 // store it. It'll get really cloned when we process it.
2798 RefInfo.Clone = DIE::get(DIEAlloc, dwarf::Tag(RefDie.getTag()));
2799 }
2800 NewRefDie = RefInfo.Clone;
2801
2802 if (AttrSpec.Form == dwarf::DW_FORM_ref_addr ||
2803 (Unit.hasODR() && isODRAttribute(AttrSpec.Attr))) {
2804 // We cannot currently rely on a DIEEntry to emit ref_addr
2805 // references, because the implementation calls back to DwarfDebug
2806 // to find the unit offset. (We don't have a DwarfDebug)
2807 // FIXME: we should be able to design DIEEntry reliance on
2808 // DwarfDebug away.
2809 uint64_t Attr;
2810 if (Ref < InputDIE.getOffset()) {
2811 // We must have already cloned that DIE.
2812 uint32_t NewRefOffset =
2813 RefUnit->getStartOffset() + NewRefDie->getOffset();
2814 Attr = NewRefOffset;
2815 Die.addValue(DIEAlloc, dwarf::Attribute(AttrSpec.Attr),
2816 dwarf::DW_FORM_ref_addr, DIEInteger(Attr));
2817 } else {
2818 // A forward reference. Note and fixup later.
2819 Attr = 0xBADDEF;
2820 Unit.noteForwardReference(
2821 NewRefDie, RefUnit, Ctxt,
2822 Die.addValue(DIEAlloc, dwarf::Attribute(AttrSpec.Attr),
2823 dwarf::DW_FORM_ref_addr, DIEInteger(Attr)));
2824 }
2825 return U.getRefAddrByteSize();
2826 }
2827
2828 Die.addValue(DIEAlloc, dwarf::Attribute(AttrSpec.Attr),
2829 dwarf::Form(AttrSpec.Form), DIEEntry(*NewRefDie));
2830 return AttrSize;
2831}
2832
2833unsigned DwarfLinker::DIECloner::cloneBlockAttribute(DIE &Die,
2834 AttributeSpec AttrSpec,
2835 const DWARFFormValue &Val,
2836 unsigned AttrSize) {
2837 DIEValueList *Attr;
2838 DIEValue Value;
2839 DIELoc *Loc = nullptr;
2840 DIEBlock *Block = nullptr;
3
'Block' initialized to a null pointer value
2841 // Just copy the block data over.
2842 if (AttrSpec.Form == dwarf::DW_FORM_exprloc) {
4
Assuming the condition is true
5
Taking true branch
2843 Loc = new (DIEAlloc) DIELoc;
2844 Linker.DIELocs.push_back(Loc);
2845 } else {
2846 Block = new (DIEAlloc) DIEBlock;
2847 Linker.DIEBlocks.push_back(Block);
2848 }
2849 Attr = Loc ? static_cast<DIEValueList *>(Loc)
6
Assuming 'Loc' is null
7
'?' condition is false
8
Null pointer value stored to 'Attr'
2850 : static_cast<DIEValueList *>(Block);
2851
2852 if (Loc)
9
Taking false branch
2853 Value = DIEValue(dwarf::Attribute(AttrSpec.Attr),
2854 dwarf::Form(AttrSpec.Form), Loc);
2855 else
2856 Value = DIEValue(dwarf::Attribute(AttrSpec.Attr),
2857 dwarf::Form(AttrSpec.Form), Block);
2858 ArrayRef<uint8_t> Bytes = *Val.getAsBlock();
2859 for (auto Byte : Bytes)
10
Assuming '__begin2' is not equal to '__end2'
2860 Attr->addValue(DIEAlloc, static_cast<dwarf::Attribute>(0),
11
Called C++ object pointer is null
2861 dwarf::DW_FORM_data1, DIEInteger(Byte));
2862 // FIXME: If DIEBlock and DIELoc just reuses the Size field of
2863 // the DIE class, this if could be replaced by
2864 // Attr->setSize(Bytes.size()).
2865 if (Linker.Streamer) {
2866 auto *AsmPrinter = &Linker.Streamer->getAsmPrinter();
2867 if (Loc)
2868 Loc->ComputeSize(AsmPrinter);
2869 else
2870 Block->ComputeSize(AsmPrinter);
2871 }
2872 Die.addValue(DIEAlloc, Value);
2873 return AttrSize;
2874}
2875
2876unsigned DwarfLinker::DIECloner::cloneAddressAttribute(
2877 DIE &Die, AttributeSpec AttrSpec, const DWARFFormValue &Val,
2878 const CompileUnit &Unit, AttributesInfo &Info) {
2879 uint64_t Addr = *Val.getAsAddress();
2880
2881 if (LLVM_UNLIKELY(Linker.Options.Update)__builtin_expect((bool)(Linker.Options.Update), false)) {
2882 if (AttrSpec.Attr == dwarf::DW_AT_low_pc)
2883 Info.HasLowPc = true;
2884 Die.addValue(DIEAlloc, dwarf::Attribute(AttrSpec.Attr),
2885 dwarf::Form(AttrSpec.Form), DIEInteger(Addr));
2886 return Unit.getOrigUnit().getAddressByteSize();
2887 }
2888
2889 if (AttrSpec.Attr == dwarf::DW_AT_low_pc) {
2890 if (Die.getTag() == dwarf::DW_TAG_inlined_subroutine ||
2891 Die.getTag() == dwarf::DW_TAG_lexical_block)
2892 // The low_pc of a block or inline subroutine might get
2893 // relocated because it happens to match the low_pc of the
2894 // enclosing subprogram. To prevent issues with that, always use
2895 // the low_pc from the input DIE if relocations have been applied.
2896 Addr = (Info.OrigLowPc != std::numeric_limits<uint64_t>::max()
2897 ? Info.OrigLowPc
2898 : Addr) +
2899 Info.PCOffset;
2900 else if (Die.getTag() == dwarf::DW_TAG_compile_unit) {
2901 Addr = Unit.getLowPc();
2902 if (Addr == std::numeric_limits<uint64_t>::max())
2903 return 0;
2904 }
2905 Info.HasLowPc = true;
2906 } else if (AttrSpec.Attr == dwarf::DW_AT_high_pc) {
2907 if (Die.getTag() == dwarf::DW_TAG_compile_unit) {
2908 if (uint64_t HighPc = Unit.getHighPc())
2909 Addr = HighPc;
2910 else
2911 return 0;
2912 } else
2913 // If we have a high_pc recorded for the input DIE, use
2914 // it. Otherwise (when no relocations where applied) just use the
2915 // one we just decoded.
2916 Addr = (Info.OrigHighPc ? Info.OrigHighPc : Addr) + Info.PCOffset;
2917 }
2918
2919 Die.addValue(DIEAlloc, static_cast<dwarf::Attribute>(AttrSpec.Attr),
2920 static_cast<dwarf::Form>(AttrSpec.Form), DIEInteger(Addr));
2921 return Unit.getOrigUnit().getAddressByteSize();
2922}
2923
2924unsigned DwarfLinker::DIECloner::cloneScalarAttribute(
2925 DIE &Die, const DWARFDie &InputDIE, CompileUnit &Unit,
2926 AttributeSpec AttrSpec, const DWARFFormValue &Val, unsigned AttrSize,
2927 AttributesInfo &Info) {
2928 uint64_t Value;
2929
2930 if (LLVM_UNLIKELY(Linker.Options.Update)__builtin_expect((bool)(Linker.Options.Update), false)) {
2931 if (auto OptionalValue = Val.getAsUnsignedConstant())
2932 Value = *OptionalValue;
2933 else if (auto OptionalValue = Val.getAsSignedConstant())
2934 Value = *OptionalValue;
2935 else if (auto OptionalValue = Val.getAsSectionOffset())
2936 Value = *OptionalValue;
2937 else {
2938 Linker.reportWarning(
2939 "Unsupported scalar attribute form. Dropping attribute.", &InputDIE);
2940 return 0;
2941 }
2942 if (AttrSpec.Attr == dwarf::DW_AT_declaration && Value)
2943 Info.IsDeclaration = true;
2944 Die.addValue(DIEAlloc, dwarf::Attribute(AttrSpec.Attr),
2945 dwarf::Form(AttrSpec.Form), DIEInteger(Value));
2946 return AttrSize;
2947 }
2948
2949 if (AttrSpec.Attr == dwarf::DW_AT_high_pc &&
2950 Die.getTag() == dwarf::DW_TAG_compile_unit) {
2951 if (Unit.getLowPc() == -1ULL)
2952 return 0;
2953 // Dwarf >= 4 high_pc is an size, not an address.
2954 Value = Unit.getHighPc() - Unit.getLowPc();
2955 } else if (AttrSpec.Form == dwarf::DW_FORM_sec_offset)
2956 Value = *Val.getAsSectionOffset();
2957 else if (AttrSpec.Form == dwarf::DW_FORM_sdata)
2958 Value = *Val.getAsSignedConstant();
2959 else if (auto OptionalValue = Val.getAsUnsignedConstant())
2960 Value = *OptionalValue;
2961 else {
2962 Linker.reportWarning(
2963 "Unsupported scalar attribute form. Dropping attribute.", &InputDIE);
2964 return 0;
2965 }
2966 PatchLocation Patch =
2967 Die.addValue(DIEAlloc, dwarf::Attribute(AttrSpec.Attr),
2968 dwarf::Form(AttrSpec.Form), DIEInteger(Value));
2969 if (AttrSpec.Attr == dwarf::DW_AT_ranges) {
2970 Unit.noteRangeAttribute(Die, Patch);
2971 Info.HasRanges = true;
2972 }
2973
2974 // A more generic way to check for location attributes would be
2975 // nice, but it's very unlikely that any other attribute needs a
2976 // location list.
2977 else if (AttrSpec.Attr == dwarf::DW_AT_location ||
2978 AttrSpec.Attr == dwarf::DW_AT_frame_base)
2979 Unit.noteLocationAttribute(Patch, Info.PCOffset);
2980 else if (AttrSpec.Attr == dwarf::DW_AT_declaration && Value)
2981 Info.IsDeclaration = true;
2982
2983 return AttrSize;
2984}
2985
2986/// Clone \p InputDIE's attribute described by \p AttrSpec with
2987/// value \p Val, and add it to \p Die.
2988/// \returns the size of the cloned attribute.
2989unsigned DwarfLinker::DIECloner::cloneAttribute(
2990 DIE &Die, const DWARFDie &InputDIE, CompileUnit &Unit,
2991 const DWARFFormValue &Val, const AttributeSpec AttrSpec, unsigned AttrSize,
2992 AttributesInfo &Info) {
2993 const DWARFUnit &U = Unit.getOrigUnit();
2994
2995 switch (AttrSpec.Form) {
1
Control jumps to 'case DW_FORM_exprloc:' at line 3010
2996 case dwarf::DW_FORM_strp:
2997 case dwarf::DW_FORM_string:
2998 return cloneStringAttribute(Die, AttrSpec, Val, U, Info);
2999 case dwarf::DW_FORM_ref_addr:
3000 case dwarf::DW_FORM_ref1:
3001 case dwarf::DW_FORM_ref2:
3002 case dwarf::DW_FORM_ref4:
3003 case dwarf::DW_FORM_ref8:
3004 return cloneDieReferenceAttribute(Die, InputDIE, AttrSpec, AttrSize, Val,
3005 Unit);
3006 case dwarf::DW_FORM_block:
3007 case dwarf::DW_FORM_block1:
3008 case dwarf::DW_FORM_block2:
3009 case dwarf::DW_FORM_block4:
3010 case dwarf::DW_FORM_exprloc:
3011 return cloneBlockAttribute(Die, AttrSpec, Val, AttrSize);
2
Calling 'DIECloner::cloneBlockAttribute'
3012 case dwarf::DW_FORM_addr:
3013 return cloneAddressAttribute(Die, AttrSpec, Val, Unit, Info);
3014 case dwarf::DW_FORM_data1:
3015 case dwarf::DW_FORM_data2:
3016 case dwarf::DW_FORM_data4:
3017 case dwarf::DW_FORM_data8:
3018 case dwarf::DW_FORM_udata:
3019 case dwarf::DW_FORM_sdata:
3020 case dwarf::DW_FORM_sec_offset:
3021 case dwarf::DW_FORM_flag:
3022 case dwarf::DW_FORM_flag_present:
3023 return cloneScalarAttribute(Die, InputDIE, Unit, AttrSpec, Val, AttrSize,
3024 Info);
3025 default:
3026 Linker.reportWarning(
3027 "Unsupported attribute form in cloneAttribute. Dropping.", &InputDIE);
3028 }
3029
3030 return 0;
3031}
3032
3033/// Apply the valid relocations found by findValidRelocs() to
3034/// the buffer \p Data, taking into account that Data is at \p BaseOffset
3035/// in the debug_info section.
3036///
3037/// Like for findValidRelocs(), this function must be called with
3038/// monotonic \p BaseOffset values.
3039///
3040/// \returns whether any reloc has been applied.
3041bool DwarfLinker::RelocationManager::applyValidRelocs(
3042 MutableArrayRef<char> Data, uint32_t BaseOffset, bool isLittleEndian) {
3043 assert((NextValidReloc == 0 ||(static_cast <bool> ((NextValidReloc == 0 || BaseOffset
> ValidRelocs[NextValidReloc - 1].Offset) && "BaseOffset should only be increasing."
) ? void (0) : __assert_fail ("(NextValidReloc == 0 || BaseOffset > ValidRelocs[NextValidReloc - 1].Offset) && \"BaseOffset should only be increasing.\""
, "/build/llvm-toolchain-snapshot-7~svn326246/tools/dsymutil/DwarfLinker.cpp"
, 3045, __extension__ __PRETTY_FUNCTION__))
3044 BaseOffset > ValidRelocs[NextValidReloc - 1].Offset) &&(static_cast <bool> ((NextValidReloc == 0 || BaseOffset
> ValidRelocs[NextValidReloc - 1].Offset) && "BaseOffset should only be increasing."
) ? void (0) : __assert_fail ("(NextValidReloc == 0 || BaseOffset > ValidRelocs[NextValidReloc - 1].Offset) && \"BaseOffset should only be increasing.\""
, "/build/llvm-toolchain-snapshot-7~svn326246/tools/dsymutil/DwarfLinker.cpp"
, 3045, __extension__ __PRETTY_FUNCTION__))
3045 "BaseOffset should only be increasing.")(static_cast <bool> ((NextValidReloc == 0 || BaseOffset
> ValidRelocs[NextValidReloc - 1].Offset) && "BaseOffset should only be increasing."
) ? void (0) : __assert_fail ("(NextValidReloc == 0 || BaseOffset > ValidRelocs[NextValidReloc - 1].Offset) && \"BaseOffset should only be increasing.\""
, "/build/llvm-toolchain-snapshot-7~svn326246/tools/dsymutil/DwarfLinker.cpp"
, 3045, __extension__ __PRETTY_FUNCTION__))
;
3046 if (NextValidReloc >= ValidRelocs.size())
3047 return false;
3048
3049 // Skip relocs that haven't been applied.
3050 while (NextValidReloc < ValidRelocs.size() &&
3051 ValidRelocs[NextValidReloc].Offset < BaseOffset)
3052 ++NextValidReloc;
3053
3054 bool Applied = false;
3055 uint64_t EndOffset = BaseOffset + Data.size();
3056 while (NextValidReloc < ValidRelocs.size() &&
3057 ValidRelocs[NextValidReloc].Offset >= BaseOffset &&
3058 ValidRelocs[NextValidReloc].Offset < EndOffset) {
3059 const auto &ValidReloc = ValidRelocs[NextValidReloc++];
3060 assert(ValidReloc.Offset - BaseOffset < Data.size())(static_cast <bool> (ValidReloc.Offset - BaseOffset <
Data.size()) ? void (0) : __assert_fail ("ValidReloc.Offset - BaseOffset < Data.size()"
, "/build/llvm-toolchain-snapshot-7~svn326246/tools/dsymutil/DwarfLinker.cpp"
, 3060, __extension__ __PRETTY_FUNCTION__))
;
3061 assert(ValidReloc.Offset - BaseOffset + ValidReloc.Size <= Data.size())(static_cast <bool> (ValidReloc.Offset - BaseOffset + ValidReloc
.Size <= Data.size()) ? void (0) : __assert_fail ("ValidReloc.Offset - BaseOffset + ValidReloc.Size <= Data.size()"
, "/build/llvm-toolchain-snapshot-7~svn326246/tools/dsymutil/DwarfLinker.cpp"
, 3061, __extension__ __PRETTY_FUNCTION__))
;
3062 char Buf[8];
3063 uint64_t Value = ValidReloc.Mapping->getValue().BinaryAddress;
3064 Value += ValidReloc.Addend;
3065 for (unsigned i = 0; i != ValidReloc.Size; ++i) {
3066 unsigned Index = isLittleEndian ? i : (ValidReloc.Size - i - 1);
3067 Buf[i] = uint8_t(Value >> (Index * 8));
3068 }
3069 assert(ValidReloc.Size <= sizeof(Buf))(static_cast <bool> (ValidReloc.Size <= sizeof(Buf))
? void (0) : __assert_fail ("ValidReloc.Size <= sizeof(Buf)"
, "/build/llvm-toolchain-snapshot-7~svn326246/tools/dsymutil/DwarfLinker.cpp"
, 3069, __extension__ __PRETTY_FUNCTION__))
;
3070 memcpy(&Data[ValidReloc.Offset - BaseOffset], Buf, ValidReloc.Size);
3071 Applied = true;
3072 }
3073
3074 return Applied;
3075}
3076
3077static bool isTypeTag(uint16_t Tag) {
3078 switch (Tag) {
3079 case dwarf::DW_TAG_array_type:
3080 case dwarf::DW_TAG_class_type:
3081 case dwarf::DW_TAG_enumeration_type:
3082 case dwarf::DW_TAG_pointer_type:
3083 case dwarf::DW_TAG_reference_type:
3084 case dwarf::DW_TAG_string_type:
3085 case dwarf::DW_TAG_structure_type:
3086 case dwarf::DW_TAG_subroutine_type:
3087 case dwarf::DW_TAG_typedef:
3088 case dwarf::DW_TAG_union_type:
3089 case dwarf::DW_TAG_ptr_to_member_type:
3090 case dwarf::DW_TAG_set_type:
3091 case dwarf::DW_TAG_subrange_type:
3092 case dwarf::DW_TAG_base_type:
3093 case dwarf::DW_TAG_const_type:
3094 case dwarf::DW_TAG_constant:
3095 case dwarf::DW_TAG_file_type:
3096 case dwarf::DW_TAG_namelist:
3097 case dwarf::DW_TAG_packed_type:
3098 case dwarf::DW_TAG_volatile_type:
3099 case dwarf::DW_TAG_restrict_type:
3100 case dwarf::DW_TAG_atomic_type:
3101 case dwarf::DW_TAG_interface_type:
3102 case dwarf::DW_TAG_unspecified_type:
3103 case dwarf::DW_TAG_shared_type:
3104 return true;
3105 default:
3106 break;
3107 }
3108 return false;
3109}
3110
3111static bool isObjCSelector(StringRef Name) {
3112 return Name.size() > 2 && (Name[0] == '-' || Name[0] == '+') &&
3113 (Name[1] == '[');
3114}
3115
3116void DwarfLinker::DIECloner::addObjCAccelerator(CompileUnit &Unit,
3117 const DIE *Die,
3118 DwarfStringPoolEntryRef Name,
3119 bool SkipPubSection) {
3120 assert(isObjCSelector(Name.getString()) && "not an objc selector")(static_cast <bool> (isObjCSelector(Name.getString()) &&
"not an objc selector") ? void (0) : __assert_fail ("isObjCSelector(Name.getString()) && \"not an objc selector\""
, "/build/llvm-toolchain-snapshot-7~svn326246/tools/dsymutil/DwarfLinker.cpp"
, 3120, __extension__ __PRETTY_FUNCTION__))
;
3121 // Objective C method or class function.
3122 // "- [Class(Category) selector :withArg ...]"
3123 StringRef ClassNameStart(Name.getString().drop_front(2));
3124 size_t FirstSpace = ClassNameStart.find(' ');
3125 if (FirstSpace == StringRef::npos)
3126 return;
3127
3128 StringRef SelectorStart(ClassNameStart.data() + FirstSpace + 1);
3129 if (!SelectorStart.size())
3130 return;
3131
3132 StringRef Selector(SelectorStart.data(), SelectorStart.size() - 1);
3133 Unit.addNameAccelerator(Die, Linker.StringPool.getEntry(Selector),
3134 SkipPubSection);
3135
3136 // Add an entry for the class name that points to this
3137 // method/class function.
3138 StringRef ClassName(ClassNameStart.data(), FirstSpace);
3139 Unit.addObjCAccelerator(Die, Linker.StringPool.getEntry(ClassName),
3140 SkipPubSection);
3141
3142 if (ClassName[ClassName.size() - 1] == ')') {
3143 size_t OpenParens = ClassName.find('(');
3144 if (OpenParens != StringRef::npos) {
3145 StringRef ClassNameNoCategory(ClassName.data(), OpenParens);
3146 Unit.addObjCAccelerator(
3147 Die, Linker.StringPool.getEntry(ClassNameNoCategory), SkipPubSection);
3148
3149 std::string MethodNameNoCategory(Name.getString().data(), OpenParens + 2);
3150 // FIXME: The missing space here may be a bug, but
3151 // dsymutil-classic also does it this way.
3152 MethodNameNoCategory.append(SelectorStart);
3153 Unit.addNameAccelerator(Die,
3154 Linker.StringPool.getEntry(MethodNameNoCategory),
3155 SkipPubSection);
3156 }
3157 }
3158}
3159
3160static bool
3161shouldSkipAttribute(DWARFAbbreviationDeclaration::AttributeSpec AttrSpec,
3162 uint16_t Tag, bool InDebugMap, bool SkipPC,
3163 bool InFunctionScope) {
3164 switch (AttrSpec.Attr) {
3165 default:
3166 return false;
3167 case dwarf::DW_AT_low_pc:
3168 case dwarf::DW_AT_high_pc:
3169 case dwarf::DW_AT_ranges:
3170 return SkipPC;
3171 case dwarf::DW_AT_location:
3172 case dwarf::DW_AT_frame_base:
3173 // FIXME: for some reason dsymutil-classic keeps the location attributes
3174 // when they are of block type (i.e. not location lists). This is totally
3175 // wrong for globals where we will keep a wrong address. It is mostly
3176 // harmless for locals, but there is no point in keeping these anyway when
3177 // the function wasn't linked.
3178 return (SkipPC || (!InFunctionScope && Tag == dwarf::DW_TAG_variable &&
3179 !InDebugMap)) &&
3180 !DWARFFormValue(AttrSpec.Form).isFormClass(DWARFFormValue::FC_Block);
3181 }
3182}
3183
3184DIE *DwarfLinker::DIECloner::cloneDIE(const DWARFDie &InputDIE,
3185 CompileUnit &Unit, int64_t PCOffset,
3186 uint32_t OutOffset, unsigned Flags,
3187 DIE *Die) {
3188 DWARFUnit &U = Unit.getOrigUnit();
3189 unsigned Idx = U.getDIEIndex(InputDIE);
3190 CompileUnit::DIEInfo &Info = Unit.getInfo(Idx);
3191
3192 // Should the DIE appear in the output?
3193 if (!Unit.getInfo(Idx).Keep)
3194 return nullptr;
3195
3196 uint32_t Offset = InputDIE.getOffset();
3197 assert(!(Die && Info.Clone) && "Can't supply a DIE and a cloned DIE")(static_cast <bool> (!(Die && Info.Clone) &&
"Can't supply a DIE and a cloned DIE") ? void (0) : __assert_fail
("!(Die && Info.Clone) && \"Can't supply a DIE and a cloned DIE\""
, "/build/llvm-toolchain-snapshot-7~svn326246/tools/dsymutil/DwarfLinker.cpp"
, 3197, __extension__ __PRETTY_FUNCTION__))
;
3198 if (!Die) {
3199 // The DIE might have been already created by a forward reference
3200 // (see cloneDieReferenceAttribute()).
3201 if (!Info.Clone)
3202 Info.Clone = DIE::get(DIEAlloc, dwarf::Tag(InputDIE.getTag()));
3203 Die = Info.Clone;
3204 }
3205
3206 assert(Die->getTag() == InputDIE.getTag())(static_cast <bool> (Die->getTag() == InputDIE.getTag
()) ? void (0) : __assert_fail ("Die->getTag() == InputDIE.getTag()"
, "/build/llvm-toolchain-snapshot-7~svn326246/tools/dsymutil/DwarfLinker.cpp"
, 3206, __extension__ __PRETTY_FUNCTION__))
;
3207 Die->setOffset(OutOffset);
3208 if ((Unit.hasODR() || Unit.isClangModule()) && !Info.Incomplete &&
3209 Die->getTag() != dwarf::DW_TAG_namespace && Info.Ctxt &&
3210 Info.Ctxt != Unit.getInfo(Info.ParentIdx).Ctxt &&
3211 !Info.Ctxt->getCanonicalDIEOffset()) {
3212 // We are about to emit a DIE that is the root of its own valid
3213 // DeclContext tree. Make the current offset the canonical offset
3214 // for this context.
3215 Info.Ctxt->setCanonicalDIEOffset(OutOffset + Unit.getStartOffset());
3216 }
3217
3218 // Extract and clone every attribute.
3219 DWARFDataExtractor Data = U.getDebugInfoExtractor();
3220 // Point to the next DIE (generally there is always at least a NULL
3221 // entry after the current one). If this is a lone
3222 // DW_TAG_compile_unit without any children, point to the next unit.
3223 uint32_t NextOffset = (Idx + 1 < U.getNumDIEs())
3224 ? U.getDIEAtIndex(Idx + 1).getOffset()
3225 : U.getNextUnitOffset();
3226 AttributesInfo AttrInfo;
3227
3228 // We could copy the data only if we need to apply a relocation to it. After
3229 // testing, it seems there is no performance downside to doing the copy
3230 // unconditionally, and it makes the code simpler.
3231 SmallString<40> DIECopy(Data.getData().substr(Offset, NextOffset - Offset));
3232 Data =
3233 DWARFDataExtractor(DIECopy, Data.isLittleEndian(), Data.getAddressSize());
3234 // Modify the copy with relocated addresses.
3235 if (RelocMgr.applyValidRelocs(DIECopy, Offset, Data.isLittleEndian())) {
3236 // If we applied relocations, we store the value of high_pc that was
3237 // potentially stored in the input DIE. If high_pc is an address
3238 // (Dwarf version == 2), then it might have been relocated to a
3239 // totally unrelated value (because the end address in the object
3240 // file might be start address of another function which got moved
3241 // independently by the linker). The computation of the actual
3242 // high_pc value is done in cloneAddressAttribute().
3243 AttrInfo.OrigHighPc =
3244 dwarf::toAddress(InputDIE.find(dwarf::DW_AT_high_pc), 0);
3245 // Also store the low_pc. It might get relocated in an
3246 // inline_subprogram that happens at the beginning of its
3247 // inlining function.
3248 AttrInfo.OrigLowPc = dwarf::toAddress(InputDIE.find(dwarf::DW_AT_low_pc),
3249 std::numeric_limits<uint64_t>::max());
3250 }
3251
3252 // Reset the Offset to 0 as we will be working on the local copy of
3253 // the data.
3254 Offset = 0;
3255
3256 const auto *Abbrev = InputDIE.getAbbreviationDeclarationPtr();
3257 Offset += getULEB128Size(Abbrev->getCode());
3258
3259 // We are entering a subprogram. Get and propagate the PCOffset.
3260 if (Die->getTag() == dwarf::DW_TAG_subprogram)
3261 PCOffset = Info.AddrAdjust;
3262 AttrInfo.PCOffset = PCOffset;
3263
3264 if (Abbrev->getTag() == dwarf::DW_TAG_subprogram) {
3265 Flags |= TF_InFunctionScope;
3266 if (!Info.InDebugMap && LLVM_LIKELY(!Options.Update)__builtin_expect((bool)(!Options.Update), true))
3267 Flags |= TF_SkipPC;
3268 }
3269
3270 bool Copied = false;
3271 for (const auto &AttrSpec : Abbrev->attributes()) {
3272 if (LLVM_LIKELY(!Options.Update)__builtin_expect((bool)(!Options.Update), true) &&
3273 shouldSkipAttribute(AttrSpec, Die->getTag(), Info.InDebugMap,
3274 Flags & TF_SkipPC, Flags & TF_InFunctionScope)) {
3275 DWARFFormValue::skipValue(AttrSpec.Form, Data, &Offset,
3276 U.getFormParams());
3277 // FIXME: dsymutil-classic keeps the old abbreviation around
3278 // even if it's not used. We can remove this (and the copyAbbrev
3279 // helper) as soon as bit-for-bit compatibility is not a goal anymore.
3280 if (!Copied) {
3281 copyAbbrev(*InputDIE.getAbbreviationDeclarationPtr(), Unit.hasODR());
3282 Copied = true;
3283 }
3284 continue;
3285 }
3286
3287 DWARFFormValue Val(AttrSpec.Form);
3288 uint32_t AttrSize = Offset;
3289 Val.extractValue(Data, &Offset, U.getFormParams(), &U);
3290 AttrSize = Offset - AttrSize;
3291
3292 OutOffset +=
3293 cloneAttribute(*Die, InputDIE, Unit, Val, AttrSpec, AttrSize, AttrInfo);
3294 }
3295
3296 // Look for accelerator entries.
3297 uint16_t Tag = InputDIE.getTag();
3298 // FIXME: This is slightly wrong. An inline_subroutine without a
3299 // low_pc, but with AT_ranges might be interesting to get into the
3300 // accelerator tables too. For now stick with dsymutil's behavior.
3301 if ((Info.InDebugMap || AttrInfo.HasLowPc || AttrInfo.HasRanges) &&
3302 Tag != dwarf::DW_TAG_compile_unit &&
3303 getDIENames(InputDIE, AttrInfo,
3304 Tag != dwarf::DW_TAG_inlined_subroutine)) {
3305 if (AttrInfo.MangledName && AttrInfo.MangledName != AttrInfo.Name)
3306 Unit.addNameAccelerator(Die, AttrInfo.MangledName,
3307 Tag == dwarf::DW_TAG_inlined_subroutine);
3308 if (AttrInfo.Name) {
3309 if (AttrInfo.NameWithoutTemplate)
3310 Unit.addNameAccelerator(Die, AttrInfo.NameWithoutTemplate,
3311 /* SkipPubSection */ true);
3312 Unit.addNameAccelerator(Die, AttrInfo.Name,
3313 Tag == dwarf::DW_TAG_inlined_subroutine);
3314 }
3315 if (AttrInfo.Name && isObjCSelector(AttrInfo.Name.getString()))
3316 addObjCAccelerator(Unit, Die, AttrInfo.Name, /* SkipPubSection =*/true);
3317
3318 } else if (Tag == dwarf::DW_TAG_namespace) {
3319 if (!AttrInfo.Name)
3320 AttrInfo.Name = Linker.StringPool.getEntry("(anonymous namespace)");
3321 Unit.addNamespaceAccelerator(Die, AttrInfo.Name);
3322 } else if (isTypeTag(Tag) && !AttrInfo.IsDeclaration &&
3323 getDIENames(InputDIE, AttrInfo) && AttrInfo.Name &&
3324 AttrInfo.Name.getString()[0]) {
3325 uint32_t Hash = hashFullyQualifiedName(InputDIE, Unit);
3326 uint64_t RuntimeLang =
3327 dwarf::toUnsigned(InputDIE.find(dwarf::DW_AT_APPLE_runtime_class))
3328 .getValueOr(0);
3329 bool ObjCClassIsImplementation =
3330 (RuntimeLang == dwarf::DW_LANG_ObjC ||
3331 RuntimeLang == dwarf::DW_LANG_ObjC_plus_plus) &&
3332 dwarf::toUnsigned(InputDIE.find(dwarf::DW_AT_APPLE_objc_complete_type))
3333 .getValueOr(0);
3334 Unit.addTypeAccelerator(Die, AttrInfo.Name, ObjCClassIsImplementation,
3335 Hash);
3336 }
3337
3338 // Determine whether there are any children that we want to keep.
3339 bool HasChildren = false;
3340 for (auto Child : InputDIE.children()) {
3341 unsigned Idx = U.getDIEIndex(Child);
3342 if (Unit.getInfo(Idx).Keep) {
3343 HasChildren = true;
3344 break;
3345 }
3346 }
3347
3348 DIEAbbrev NewAbbrev = Die->generateAbbrev();
3349 if (HasChildren)
3350 NewAbbrev.setChildrenFlag(dwarf::DW_CHILDREN_yes);
3351 // Assign a permanent abbrev number
3352 Linker.AssignAbbrev(NewAbbrev);
3353 Die->setAbbrevNumber(NewAbbrev.getNumber());
3354
3355 // Add the size of the abbreviation number to the output offset.
3356 OutOffset += getULEB128Size(Die->getAbbrevNumber());
3357
3358 if (!HasChildren) {
3359 // Update our size.
3360 Die->setSize(OutOffset - Die->getOffset());
3361 return Die;
3362 }
3363
3364 // Recursively clone children.
3365 for (auto Child : InputDIE.children()) {
3366 if (DIE *Clone = cloneDIE(Child, Unit, PCOffset, OutOffset, Flags)) {
3367 Die->addChild(Clone);
3368 OutOffset = Clone->getOffset() + Clone->getSize();
3369 }
3370 }
3371
3372 // Account for the end of children marker.
3373 OutOffset += sizeof(int8_t);
3374 // Update our size.
3375 Die->setSize(OutOffset - Die->getOffset());
3376 return Die;
3377}
3378
3379/// Patch the input object file relevant debug_ranges entries
3380/// and emit them in the output file. Update the relevant attributes
3381/// to point at the new entries.
3382void DwarfLinker::patchRangesForUnit(const CompileUnit &Unit,
3383 DWARFContext &OrigDwarf) const {
3384 DWARFDebugRangeList RangeList;
3385 const auto &FunctionRanges = Unit.getFunctionRanges();
3386 unsigned AddressSize = Unit.getOrigUnit().getAddressByteSize();
3387 DWARFDataExtractor RangeExtractor(OrigDwarf.getDWARFObj(),
3388 OrigDwarf.getDWARFObj().getRangeSection(),
3389 OrigDwarf.isLittleEndian(), AddressSize);
3390 auto InvalidRange = FunctionRanges.end(), CurrRange = InvalidRange;
3391 DWARFUnit &OrigUnit = Unit.getOrigUnit();
3392 auto OrigUnitDie = OrigUnit.getUnitDIE(false);
3393 uint64_t OrigLowPc =
3394 dwarf::toAddress(OrigUnitDie.find(dwarf::DW_AT_low_pc), -1ULL);
3395 // Ranges addresses are based on the unit's low_pc. Compute the
3396 // offset we need to apply to adapt to the new unit's low_pc.
3397 int64_t UnitPcOffset = 0;
3398 if (OrigLowPc != -1ULL)
3399 UnitPcOffset = int64_t(OrigLowPc) - Unit.getLowPc();
3400
3401 for (const auto &RangeAttribute : Unit.getRangesAttributes()) {
3402 uint32_t Offset = RangeAttribute.get();
3403 RangeAttribute.set(Streamer->getRangesSectionSize());
3404 RangeList.extract(RangeExtractor, &Offset);
3405 const auto &Entries = RangeList.getEntries();
3406 if (!Entries.empty()) {
3407 const DWARFDebugRangeList::RangeListEntry &First = Entries.front();
3408
3409 if (CurrRange == InvalidRange ||
3410 First.StartAddress + OrigLowPc < CurrRange.start() ||
3411 First.StartAddress + OrigLowPc >= CurrRange.stop()) {
3412 CurrRange = FunctionRanges.find(First.StartAddress + OrigLowPc);
3413 if (CurrRange == InvalidRange ||
3414 CurrRange.start() > First.StartAddress + OrigLowPc) {
3415 reportWarning("no mapping for range.");
3416 continue;
3417 }
3418 }
3419 }
3420
3421 Streamer->emitRangesEntries(UnitPcOffset, OrigLowPc, CurrRange, Entries,
3422 AddressSize);
3423 }
3424}
3425
3426/// Generate the debug_aranges entries for \p Unit and if the
3427/// unit has a DW_AT_ranges attribute, also emit the debug_ranges
3428/// contribution for this attribute.
3429/// FIXME: this could actually be done right in patchRangesForUnit,
3430/// but for the sake of initial bit-for-bit compatibility with legacy
3431/// dsymutil, we have to do it in a delayed pass.
3432void DwarfLinker::generateUnitRanges(CompileUnit &Unit) const {
3433 auto Attr = Unit.getUnitRangesAttribute();
3434 if (Attr)
3435 Attr->set(Streamer->getRangesSectionSize());
3436 Streamer->emitUnitRangesEntries(Unit, static_cast<bool>(Attr));
3437}
3438
3439/// Insert the new line info sequence \p Seq into the current
3440/// set of already linked line info \p Rows.
3441static void insertLineSequence(std::vector<DWARFDebugLine::Row> &Seq,
3442 std::vector<DWARFDebugLine::Row> &Rows) {
3443 if (Seq.empty())
3444 return;
3445
3446 if (!Rows.empty() && Rows.back().Address < Seq.front().Address) {
3447 Rows.insert(Rows.end(), Seq.begin(), Seq.end());
3448 Seq.clear();
3449 return;
3450 }
3451
3452 auto InsertPoint = std::lower_bound(
3453 Rows.begin(), Rows.end(), Seq.front(),
3454 [](const DWARFDebugLine::Row &LHS, const DWARFDebugLine::Row &RHS) {
3455 return LHS.Address < RHS.Address;
3456 });
3457
3458 // FIXME: this only removes the unneeded end_sequence if the
3459 // sequences have been inserted in order. Using a global sort like
3460 // described in patchLineTableForUnit() and delaying the end_sequene
3461 // elimination to emitLineTableForUnit() we can get rid of all of them.
3462 if (InsertPoint != Rows.end() &&
3463 InsertPoint->Address == Seq.front().Address && InsertPoint->EndSequence) {
3464 *InsertPoint = Seq.front();
3465 Rows.insert(InsertPoint + 1, Seq.begin() + 1, Seq.end());
3466 } else {
3467 Rows.insert(InsertPoint, Seq.begin(), Seq.end());
3468 }
3469
3470 Seq.clear();
3471}
3472
3473static void patchStmtList(DIE &Die, DIEInteger Offset) {
3474 for (auto &V : Die.values())
3475 if (V.getAttribute() == dwarf::DW_AT_stmt_list) {
3476 V = DIEValue(V.getAttribute(), V.getForm(), Offset);
3477 return;
3478 }
3479
3480 llvm_unreachable("Didn't find DW_AT_stmt_list in cloned DIE!")::llvm::llvm_unreachable_internal("Didn't find DW_AT_stmt_list in cloned DIE!"
, "/build/llvm-toolchain-snapshot-7~svn326246/tools/dsymutil/DwarfLinker.cpp"
, 3480)
;
3481}
3482
3483/// Extract the line table for \p Unit from \p OrigDwarf, and
3484/// recreate a relocated version of these for the address ranges that
3485/// are present in the binary.
3486void DwarfLinker::patchLineTableForUnit(CompileUnit &Unit,
3487 DWARFContext &OrigDwarf) {
3488 DWARFDie CUDie = Unit.getOrigUnit().getUnitDIE();
3489 auto StmtList = dwarf::toSectionOffset(CUDie.find(dwarf::DW_AT_stmt_list));
3490 if (!StmtList)
3491 return;
3492
3493 // Update the cloned DW_AT_stmt_list with the correct debug_line offset.
3494 if (auto *OutputDIE = Unit.getOutputUnitDIE())
3495 patchStmtList(*OutputDIE, DIEInteger(Streamer->getLineSectionSize()));
3496
3497 // Parse the original line info for the unit.
3498 DWARFDebugLine::LineTable LineTable;
3499 uint32_t StmtOffset = *StmtList;
3500 DWARFDataExtractor LineExtractor(
3501 OrigDwarf.getDWARFObj(), OrigDwarf.getDWARFObj().getLineSection(),
3502 OrigDwarf.isLittleEndian(), Unit.getOrigUnit().getAddressByteSize());
3503 LineTable.parse(LineExtractor, &StmtOffset, OrigDwarf, &Unit.getOrigUnit());
3504
3505 // This vector is the output line table.
3506 std::vector<DWARFDebugLine::Row> NewRows;
3507 NewRows.reserve(LineTable.Rows.size());
3508
3509 // Current sequence of rows being extracted, before being inserted
3510 // in NewRows.
3511 std::vector<DWARFDebugLine::Row> Seq;
3512 const auto &FunctionRanges = Unit.getFunctionRanges();
3513 auto InvalidRange = FunctionRanges.end(), CurrRange = InvalidRange;
3514
3515 // FIXME: This logic is meant to generate exactly the same output as
3516 // Darwin's classic dsymutil. There is a nicer way to implement this
3517 // by simply putting all the relocated line info in NewRows and simply
3518 // sorting NewRows before passing it to emitLineTableForUnit. This
3519 // should be correct as sequences for a function should stay
3520 // together in the sorted output. There are a few corner cases that
3521 // look suspicious though, and that required to implement the logic
3522 // this way. Revisit that once initial validation is finished.
3523
3524 // Iterate over the object file line info and extract the sequences
3525 // that correspond to linked functions.
3526 for (auto &Row : LineTable.Rows) {
3527 // Check whether we stepped out of the range. The range is
3528 // half-open, but consider accept the end address of the range if
3529 // it is marked as end_sequence in the input (because in that
3530 // case, the relocation offset is accurate and that entry won't
3531 // serve as the start of another function).
3532 if (CurrRange == InvalidRange || Row.Address < CurrRange.start() ||
3533 Row.Address > CurrRange.stop() ||
3534 (Row.Address == CurrRange.stop() && !Row.EndSequence)) {
3535 // We just stepped out of a known range. Insert a end_sequence
3536 // corresponding to the end of the range.
3537 uint64_t StopAddress = CurrRange != InvalidRange
3538 ? CurrRange.stop() + CurrRange.value()
3539 : -1ULL;
3540 CurrRange = FunctionRanges.find(Row.Address);
3541 bool CurrRangeValid =
3542 CurrRange != InvalidRange && CurrRange.start() <= Row.Address;
3543 if (!CurrRangeValid) {
3544 CurrRange = InvalidRange;
3545 if (StopAddress != -1ULL) {
3546 // Try harder by looking in the DebugMapObject function
3547 // ranges map. There are corner cases where this finds a
3548 // valid entry. It's unclear if this is right or wrong, but
3549 // for now do as dsymutil.
3550 // FIXME: Understand exactly what cases this addresses and
3551 // potentially remove it along with the Ranges map.
3552 auto Range = Ranges.lower_bound(Row.Address);
3553 if (Range != Ranges.begin() && Range != Ranges.end())
3554 --Range;
3555
3556 if (Range != Ranges.end() && Range->first <= Row.Address &&
3557 Range->second.first >= Row.Address) {
3558 StopAddress = Row.Address + Range->second.second;
3559 }
3560 }
3561 }
3562 if (StopAddress != -1ULL && !Seq.empty()) {
3563 // Insert end sequence row with the computed end address, but
3564 // the same line as the previous one.
3565 auto NextLine = Seq.back();
3566 NextLine.Address = StopAddress;
3567 NextLine.EndSequence = 1;
3568 NextLine.PrologueEnd = 0;
3569 NextLine.BasicBlock = 0;
3570 NextLine.EpilogueBegin = 0;
3571 Seq.push_back(NextLine);
3572 insertLineSequence(Seq, NewRows);
3573 }
3574
3575 if (!CurrRangeValid)
3576 continue;
3577 }
3578
3579 // Ignore empty sequences.
3580 if (Row.EndSequence && Seq.empty())
3581 continue;
3582
3583 // Relocate row address and add it to the current sequence.
3584 Row.Address += CurrRange.value();
3585 Seq.emplace_back(Row);
3586
3587 if (Row.EndSequence)
3588 insertLineSequence(Seq, NewRows);
3589 }
3590
3591 // Finished extracting, now emit the line tables.
3592 // FIXME: LLVM hard-codes its prologue values. We just copy the
3593 // prologue over and that works because we act as both producer and
3594 // consumer. It would be nicer to have a real configurable line
3595 // table emitter.
3596 if (LineTable.Prologue.getVersion() < 2 ||
3597 LineTable.Prologue.getVersion() > 5 ||
3598 LineTable.Prologue.DefaultIsStmt != DWARF2_LINE_DEFAULT_IS_STMT1 ||
3599 LineTable.Prologue.OpcodeBase > 13)
3600 reportWarning("line table parameters mismatch. Cannot emit.");
3601 else {
3602 uint32_t PrologueEnd = *StmtList + 10 + LineTable.Prologue.PrologueLength;
3603 // DWARF v5 has an extra 2 bytes of information before the header_length
3604 // field.
3605 if (LineTable.Prologue.getVersion() == 5)
3606 PrologueEnd += 2;
3607 StringRef LineData = OrigDwarf.getDWARFObj().getLineSection().Data;
3608 MCDwarfLineTableParams Params;
3609 Params.DWARF2LineOpcodeBase = LineTable.Prologue.OpcodeBase;
3610 Params.DWARF2LineBase = LineTable.Prologue.LineBase;
3611 Params.DWARF2LineRange = LineTable.Prologue.LineRange;
3612 Streamer->emitLineTableForUnit(Params,
3613 LineData.slice(*StmtList + 4, PrologueEnd),
3614 LineTable.Prologue.MinInstLength, NewRows,
3615 Unit.getOrigUnit().getAddressByteSize());
3616 }
3617}
3618
3619void DwarfLinker::emitAcceleratorEntriesForUnit(CompileUnit &Unit) {
3620 // Add namespaces.
3621 for (const auto &Namespace : Unit.getNamespaces())
3622 AppleNamespaces.addName(Namespace.Name,
3623 Namespace.Die->getOffset() + Unit.getStartOffset());
3624
3625 /// Add names.
3626 if (!Options.Minimize)
3627 Streamer->emitPubNamesForUnit(Unit);
3628 for (const auto &Pubname : Unit.getPubnames())
3629 AppleNames.addName(Pubname.Name,
3630 Pubname.Die->getOffset() + Unit.getStartOffset());
3631
3632 /// Add types.
3633 if (!Options.Minimize)
3634 Streamer->emitPubTypesForUnit(Unit);
3635 for (const auto &Pubtype : Unit.getPubtypes())
3636 AppleTypes.addName(
3637 Pubtype.Name, Pubtype.Die->getOffset() + Unit.getStartOffset(),
3638 Pubtype.Die->getTag(),
3639 Pubtype.ObjcClassImplementation ? dwarf::DW_FLAG_type_implementation
3640 : 0,
3641 Pubtype.QualifiedNameHash);
3642
3643 /// Add ObjC names.
3644 for (const auto &ObjC : Unit.getObjC())
3645 AppleObjc.addName(ObjC.Name, ObjC.Die->getOffset() + Unit.getStartOffset());
3646}
3647
3648/// Read the frame info stored in the object, and emit the
3649/// patched frame descriptions for the linked binary.
3650///
3651/// This is actually pretty easy as the data of the CIEs and FDEs can
3652/// be considered as black boxes and moved as is. The only thing to do
3653/// is to patch the addresses in the headers.
3654void DwarfLinker::patchFrameInfoForObject(const DebugMapObject &DMO,
3655 DWARFContext &OrigDwarf,
3656 unsigned AddrSize) {
3657 StringRef FrameData = OrigDwarf.getDWARFObj().getDebugFrameSection();
3658 if (FrameData.empty())
3659 return;
3660
3661 DataExtractor Data(FrameData, OrigDwarf.isLittleEndian(), 0);
3662 uint32_t InputOffset = 0;
3663
3664 // Store the data of the CIEs defined in this object, keyed by their
3665 // offsets.
3666 DenseMap<uint32_t, StringRef> LocalCIES;
3667
3668 while (Data.isValidOffset(InputOffset)) {
3669 uint32_t EntryOffset = InputOffset;
3670 uint32_t InitialLength = Data.getU32(&InputOffset);
3671 if (InitialLength == 0xFFFFFFFF)
3672 return reportWarning("Dwarf64 bits no supported");
3673
3674 uint32_t CIEId = Data.getU32(&InputOffset);
3675 if (CIEId == 0xFFFFFFFF) {
3676 // This is a CIE, store it.
3677 StringRef CIEData = FrameData.substr(EntryOffset, InitialLength + 4);
3678 LocalCIES[EntryOffset] = CIEData;
3679 // The -4 is to account for the CIEId we just read.
3680 InputOffset += InitialLength - 4;
3681 continue;
3682 }
3683
3684 uint32_t Loc = Data.getUnsigned(&InputOffset, AddrSize);
3685
3686 // Some compilers seem to emit frame info that doesn't start at
3687 // the function entry point, thus we can't just lookup the address
3688 // in the debug map. Use the linker's range map to see if the FDE
3689 // describes something that we can relocate.
3690 auto Range = Ranges.upper_bound(Loc);
3691 if (Range != Ranges.begin())
3692 --Range;
3693 if (Range == Ranges.end() || Range->first > Loc ||
3694 Range->second.first <= Loc) {
3695 // The +4 is to account for the size of the InitialLength field itself.
3696 InputOffset = EntryOffset + InitialLength + 4;
3697 continue;
3698 }
3699
3700 // This is an FDE, and we have a mapping.
3701 // Have we already emitted a corresponding CIE?
3702 StringRef CIEData = LocalCIES[CIEId];
3703 if (CIEData.empty())
3704 return reportWarning("Inconsistent debug_frame content. Dropping.");
3705
3706 // Look if we already emitted a CIE that corresponds to the
3707 // referenced one (the CIE data is the key of that lookup).
3708 auto IteratorInserted = EmittedCIEs.insert(
3709 std::make_pair(CIEData, Streamer->getFrameSectionSize()));
3710 // If there is no CIE yet for this ID, emit it.
3711 if (IteratorInserted.second ||
3712 // FIXME: dsymutil-classic only caches the last used CIE for
3713 // reuse. Mimic that behavior for now. Just removing that
3714 // second half of the condition and the LastCIEOffset variable
3715 // makes the code DTRT.
3716 LastCIEOffset != IteratorInserted.first->getValue()) {
3717 LastCIEOffset = Streamer->getFrameSectionSize();
3718 IteratorInserted.first->getValue() = LastCIEOffset;
3719 Streamer->emitCIE(CIEData);
3720 }
3721
3722 // Emit the FDE with updated address and CIE pointer.
3723 // (4 + AddrSize) is the size of the CIEId + initial_location
3724 // fields that will get reconstructed by emitFDE().
3725 unsigned FDERemainingBytes = InitialLength - (4 + AddrSize);
3726 Streamer->emitFDE(IteratorInserted.first->getValue(), AddrSize,
3727 Loc + Range->second.second,
3728 FrameData.substr(InputOffset, FDERemainingBytes));
3729 InputOffset += FDERemainingBytes;
3730 }
3731}
3732
3733void DwarfLinker::DIECloner::copyAbbrev(
3734 const DWARFAbbreviationDeclaration &Abbrev, bool hasODR) {
3735 DIEAbbrev Copy(dwarf::Tag(Abbrev.getTag()),
3736 dwarf::Form(Abbrev.hasChildren()));
3737
3738 for (const auto &Attr : Abbrev.attributes()) {
3739 uint16_t Form = Attr.Form;
3740 if (hasODR && isODRAttribute(Attr.Attr))
3741 Form = dwarf::DW_FORM_ref_addr;
3742 Copy.AddAttribute(dwarf::Attribute(Attr.Attr), dwarf::Form(Form));
3743 }
3744
3745 Linker.AssignAbbrev(Copy);
3746}
3747
3748uint32_t DwarfLinker::DIECloner::hashFullyQualifiedName(DWARFDie DIE,
3749 CompileUnit &U,
3750 int RecurseDepth) {
3751 const char *Name = nullptr;
3752 DWARFUnit *OrigUnit = &U.getOrigUnit();
3753 CompileUnit *CU = &U;
3754 Optional<DWARFFormValue> Ref;
3755
3756 while (1) {
3757 if (const char *CurrentName = DIE.getName(DINameKind::ShortName))
3758 Name = CurrentName;
3759
3760 if (!(Ref = DIE.find(dwarf::DW_AT_specification)) &&
3761 !(Ref = DIE.find(dwarf::DW_AT_abstract_origin)))
3762 break;
3763
3764 if (!Ref->isFormClass(DWARFFormValue::FC_Reference))
3765 break;
3766
3767 CompileUnit *RefCU;
3768 if (auto RefDIE = resolveDIEReference(Linker, CompileUnits, *Ref,
3769 U.getOrigUnit(), DIE, RefCU)) {
3770 CU = RefCU;
3771 OrigUnit = &RefCU->getOrigUnit();
3772 DIE = RefDIE;
3773 }
3774 }
3775
3776 unsigned Idx = OrigUnit->getDIEIndex(DIE);
3777 if (!Name && DIE.getTag() == dwarf::DW_TAG_namespace)
3778 Name = "(anonymous namespace)";
3779
3780 if (CU->getInfo(Idx).ParentIdx == 0 ||
3781 // FIXME: dsymutil-classic compatibility. Ignore modules.
3782 CU->getOrigUnit().getDIEAtIndex(CU->getInfo(Idx).ParentIdx).getTag() ==
3783 dwarf::DW_TAG_module)
3784 return djbHash(Name ? Name : "", djbHash(RecurseDepth ? "" : "::"));
3785
3786 DWARFDie Die = OrigUnit->getDIEAtIndex(CU->getInfo(Idx).ParentIdx);
3787 return djbHash((Name ? Name : ""),
3788 djbHash((Name ? "::" : ""),
3789 hashFullyQualifiedName(Die, *CU, ++RecurseDepth)));
3790}
3791
3792static uint64_t getDwoId(const DWARFDie &CUDie, const DWARFUnit &Unit) {
3793 auto DwoId = dwarf::toUnsigned(
3794 CUDie.find({dwarf::DW_AT_dwo_id, dwarf::DW_AT_GNU_dwo_id}));
3795 if (DwoId)
3796 return *DwoId;
3797 return 0;
3798}
3799
3800bool DwarfLinker::registerModuleReference(const DWARFDie &CUDie,
3801 const DWARFUnit &Unit,
3802 DebugMap &ModuleMap,
3803 unsigned Indent) {
3804 std::string PCMfile = dwarf::toString(
3805 CUDie.find({dwarf::DW_AT_dwo_name, dwarf::DW_AT_GNU_dwo_name}), "");
3806 if (PCMfile.empty())
3807 return false;
3808
3809 // Clang module DWARF skeleton CUs abuse this for the path to the module.
3810 std::string PCMpath = dwarf::toString(CUDie.find(dwarf::DW_AT_comp_dir), "");
3811 uint64_t DwoId = getDwoId(CUDie, Unit);
3812
3813 std::string Name = dwarf::toString(CUDie.find(dwarf::DW_AT_name), "");
3814 if (Name.empty()) {
3815 reportWarning("Anonymous module skeleton CU for " + PCMfile);
3816 return true;
3817 }
3818
3819 if (Options.Verbose) {
3820 outs().indent(Indent);
3821 outs() << "Found clang module reference " << PCMfile;
3822 }
3823
3824 auto Cached = ClangModules.find(PCMfile);
3825 if (Cached != ClangModules.end()) {
3826 // FIXME: Until PR27449 (https://llvm.org/bugs/show_bug.cgi?id=27449) is
3827 // fixed in clang, only warn about DWO_id mismatches in verbose mode.
3828 // ASTFileSignatures will change randomly when a module is rebuilt.
3829 if (Options.Verbose && (Cached->second != DwoId))
3830 reportWarning(Twine("hash mismatch: this object file was built against a "
3831 "different version of the module ") +
3832 PCMfile);
3833 if (Options.Verbose)
3834 outs() << " [cached].\n";
3835 return true;
3836 }
3837 if (Options.Verbose)
3838 outs() << " ...\n";
3839
3840 // Cyclic dependencies are disallowed by Clang, but we still
3841 // shouldn't run into an infinite loop, so mark it as processed now.
3842 ClangModules.insert({PCMfile, DwoId});
3843 if (Error E = loadClangModule(PCMfile, PCMpath, Name, DwoId, ModuleMap,
3844 Indent + 2)) {
3845 consumeError(std::move(E));
3846 return false;
3847 }
3848 return true;
3849}
3850
3851ErrorOr<const object::ObjectFile &>
3852DwarfLinker::loadObject(BinaryHolder &BinaryHolder, DebugMapObject &Obj,
3853 const DebugMap &Map) {
3854 auto ErrOrObjs =
3855 BinaryHolder.GetObjectFiles(Obj.getObjectFilename(), Obj.getTimestamp());
3856 if (std::error_code EC = ErrOrObjs.getError()) {
3857 reportWarning(Twine(Obj.getObjectFilename()) + ": " + EC.message());
3858 return EC;
3859 }
3860 auto ErrOrObj = BinaryHolder.Get(Map.getTriple());
3861 if (std::error_code EC = ErrOrObj.getError())
3862 reportWarning(Twine(Obj.getObjectFilename()) + ": " + EC.message());
3863 return ErrOrObj;
3864}
3865
3866Error DwarfLinker::loadClangModule(StringRef Filename, StringRef ModulePath,
3867 StringRef ModuleName, uint64_t DwoId,
3868 DebugMap &ModuleMap, unsigned Indent) {
3869 SmallString<80> Path(Options.PrependPath);
3870 if (sys::path::is_relative(Filename))
3871 sys::path::append(Path, ModulePath, Filename);
3872 else
3873 sys::path::append(Path, Filename);
3874 BinaryHolder ObjHolder(Options.Verbose);
3875 auto &Obj = ModuleMap.addDebugMapObject(
3876 Path, sys::TimePoint<std::chrono::seconds>(), MachO::N_OSO);
3877 auto ErrOrObj = loadObject(ObjHolder, Obj, ModuleMap);
3878 if (!ErrOrObj) {
3879 // Try and emit more helpful warnings by applying some heuristics.
3880 StringRef ObjFile = CurrentDebugObject->getObjectFilename();
3881 bool isClangModule = sys::path::extension(Filename).equals(".pcm");
3882 bool isArchive = ObjFile.endswith(")");
3883 if (isClangModule) {
3884 StringRef ModuleCacheDir = sys::path::parent_path(Path);
3885 if (sys::fs::exists(ModuleCacheDir)) {
3886 // If the module's parent directory exists, we assume that the module
3887 // cache has expired and was pruned by clang. A more adventurous
3888 // dsymutil would invoke clang to rebuild the module now.
3889 if (!ModuleCacheHintDisplayed) {
3890 errs() << "note: The clang module cache may have expired since this "
3891 "object file was built. Rebuilding the object file will "
3892 "rebuild the module cache.\n";
3893 ModuleCacheHintDisplayed = true;
3894 }
3895 } else if (isArchive) {
3896 // If the module cache directory doesn't exist at all and the object
3897 // file is inside a static library, we assume that the static library
3898 // was built on a different machine. We don't want to discourage module
3899 // debugging for convenience libraries within a project though.
3900 if (!ArchiveHintDisplayed) {
3901 errs() << "note: Linking a static library that was built with "
3902 "-gmodules, but the module cache was not found. "
3903 "Redistributable static libraries should never be built "
3904 "with module debugging enabled. The debug experience will "
3905 "be degraded due to incomplete debug information.\n";
3906 ArchiveHintDisplayed = true;
3907 }
3908 }
3909 }
3910 return Error::success();
3911 }
3912
3913 std::unique_ptr<CompileUnit> Unit;
3914
3915 // Setup access to the debug info.
3916 auto DwarfContext = DWARFContext::create(*ErrOrObj);
3917 RelocationManager RelocMgr(*this);
3918 for (const auto &CU : DwarfContext->compile_units()) {
3919 maybeUpdateMaxDwarfVersion(CU->getVersion());
3920
3921 // Recursively get all modules imported by this one.
3922 auto CUDie = CU->getUnitDIE(false);
3923 if (!registerModuleReference(CUDie, *CU, ModuleMap, Indent)) {
3924 if (Unit) {
3925 std::string Err =
3926 (Filename +
3927 ": Clang modules are expected to have exactly 1 compile unit.\n")
3928 .str();
3929 errs() << Err;
3930 return make_error<StringError>(Err, inconvertibleErrorCode());
3931 }
3932 // FIXME: Until PR27449 (https://llvm.org/bugs/show_bug.cgi?id=27449) is
3933 // fixed in clang, only warn about DWO_id mismatches in verbose mode.
3934 // ASTFileSignatures will change randomly when a module is rebuilt.
3935 uint64_t PCMDwoId = getDwoId(CUDie, *CU);
3936 if (PCMDwoId != DwoId) {
3937 if (Options.Verbose)
3938 reportWarning(
3939 Twine("hash mismatch: this object file was built against a "
3940 "different version of the module ") +
3941 Filename);
3942 // Update the cache entry with the DwoId of the module loaded from disk.
3943 ClangModules[Filename] = PCMDwoId;
3944 }
3945
3946 // Add this module.
3947 Unit = llvm::make_unique<CompileUnit>(*CU, UnitID++, !Options.NoODR,
3948 ModuleName);
3949 Unit->setHasInterestingContent();
3950 analyzeContextInfo(CUDie, 0, *Unit, &ODRContexts.getRoot(), StringPool,
3951 ODRContexts);
3952 // Keep everything.
3953 Unit->markEverythingAsKept();
3954 }
3955 }
3956 if (!Unit->getOrigUnit().getUnitDIE().hasChildren())
3957 return Error::success();
3958 if (Options.Verbose) {
3959 outs().indent(Indent);
3960 outs() << "cloning .debug_info from " << Filename << "\n";
3961 }
3962
3963 std::vector<std::unique_ptr<CompileUnit>> CompileUnits;
3964 CompileUnits.push_back(std::move(Unit));
3965 DIECloner(*this, RelocMgr, DIEAlloc, CompileUnits, Options)
3966 .cloneAllCompileUnits(*DwarfContext);
3967 return Error::success();
3968}
3969
3970void DwarfLinker::DIECloner::cloneAllCompileUnits(DWARFContext &DwarfContext) {
3971 if (!Linker.Streamer)
3972 return;
3973
3974 for (auto &CurrentUnit : CompileUnits) {
3975 auto InputDIE = CurrentUnit->getOrigUnit().getUnitDIE();
3976 CurrentUnit->setStartOffset(Linker.OutputDebugInfoSize);
3977 if (CurrentUnit->getInfo(0).Keep) {
3978 // Clone the InputDIE into your Unit DIE in our compile unit since it
3979 // already has a DIE inside of it.
3980 CurrentUnit->createOutputDIE();
3981 cloneDIE(InputDIE, *CurrentUnit, 0 /* PC offset */,
3982 11 /* Unit Header size */, 0, CurrentUnit->getOutputUnitDIE());
3983 }
3984 Linker.OutputDebugInfoSize = CurrentUnit->computeNextUnitOffset();
3985 if (Linker.Options.NoOutput)
3986 continue;
3987
3988 if (LLVM_LIKELY(!Linker.Options.Update)__builtin_expect((bool)(!Linker.Options.Update), true)) {
3989 // FIXME: for compatibility with the classic dsymutil, we emit an empty
3990 // line table for the unit, even if the unit doesn't actually exist in
3991 // the DIE tree.
3992 Linker.patchLineTableForUnit(*CurrentUnit, DwarfContext);
3993 Linker.emitAcceleratorEntriesForUnit(*CurrentUnit);
3994 Linker.patchRangesForUnit(*CurrentUnit, DwarfContext);
3995 Linker.Streamer->emitLocationsForUnit(*CurrentUnit, DwarfContext);
3996 } else {
3997 Linker.emitAcceleratorEntriesForUnit(*CurrentUnit);
3998 }
3999 }
4000
4001 if (Linker.Options.NoOutput)
4002 return;
4003
4004 // Emit all the compile unit's debug information.
4005 for (auto &CurrentUnit : CompileUnits) {
4006 if (LLVM_LIKELY(!Linker.Options.Update)__builtin_expect((bool)(!Linker.Options.Update), true))
4007 Linker.generateUnitRanges(*CurrentUnit);
4008 CurrentUnit->fixupForwardReferences();
4009 Linker.Streamer->emitCompileUnitHeader(*CurrentUnit);
4010 if (!CurrentUnit->getOutputUnitDIE())
4011 continue;
4012 Linker.Streamer->emitDIE(*CurrentUnit->getOutputUnitDIE());
4013 }
4014}
4015
4016bool DwarfLinker::link(const DebugMap &Map) {
4017 if (!createStreamer(Map.getTriple(), OutFile))
4018 return false;
4019
4020 // Size of the DIEs (and headers) generated for the linked output.
4021 OutputDebugInfoSize = 0;
4022 // A unique ID that identifies each compile unit.
4023 UnitID = 0;
4024 DebugMap ModuleMap(Map.getTriple(), Map.getBinaryPath());
4025
4026 for (const auto &Obj : Map.objects()) {
4027 CurrentDebugObject = Obj.get();
4028
4029 if (Options.Verbose)
4030 outs() << "DEBUG MAP OBJECT: " << Obj->getObjectFilename() << "\n";
4031
4032 // N_AST objects (swiftmodule files) should get dumped directly into the
4033 // appropriate DWARF section.
4034 if (Obj->getType() == MachO::N_AST) {
4035 StringRef File = Obj->getObjectFilename();
4036 auto ErrorOrMem = MemoryBuffer::getFile(File);
4037 if (!ErrorOrMem) {
4038 errs() << "Warning: Could not open " << File << "\n";
4039 continue;
4040 }
4041 sys::fs::file_status Stat;
4042 if (auto errc = sys::fs::status(File, Stat)) {
4043 errs() << "Warning: " << errc.message() << "\n";
4044 continue;
4045 }
4046 if (!Options.NoTimestamp && Stat.getLastModificationTime() !=
4047 sys::TimePoint<>(Obj->getTimestamp())) {
4048 errs() << "Warning: Timestamp mismatch for " << File << ": "
4049 << Stat.getLastModificationTime() << " and "
4050 << sys::TimePoint<>(Obj->getTimestamp()) << "\n";
4051 continue;
4052 }
4053
4054 // Copy the module into the .swift_ast section.
4055 if (!Options.NoOutput)
4056 Streamer->emitSwiftAST((*ErrorOrMem)->getBuffer());
4057 continue;
4058 }
4059
4060 auto ErrOrObj = loadObject(BinHolder, *Obj, Map);
4061 if (!ErrOrObj)
4062 continue;
4063
4064 // Look for relocations that correspond to debug map entries.
4065 RelocationManager RelocMgr(*this);
4066 if (LLVM_LIKELY(!Options.Update)__builtin_expect((bool)(!Options.Update), true) &&
4067 !RelocMgr.findValidRelocsInDebugInfo(*ErrOrObj, *Obj)) {
4068 if (Options.Verbose)
4069 outs() << "No valid relocations found. Skipping.\n";
4070 continue;
4071 }
4072
4073 // Setup access to the debug info.
4074 auto DwarfContext = DWARFContext::create(*ErrOrObj);
4075 startDebugObject(*DwarfContext, *Obj);
4076
4077 // In a first phase, just read in the debug info and load all clang modules.
4078 for (const auto &CU : DwarfContext->compile_units()) {
4079 auto CUDie = CU->getUnitDIE(false);
4080 if (Options.Verbose) {
4081 outs() << "Input compilation unit:";
4082 DIDumpOptions DumpOpts;
4083 DumpOpts.RecurseDepth = 0;
4084 DumpOpts.Verbose = Options.Verbose;
4085 CUDie.dump(outs(), 0, DumpOpts);
4086 }
4087
4088 if (!CUDie || LLVM_UNLIKELY(Options.Update)__builtin_expect((bool)(Options.Update), false) ||
4089 !registerModuleReference(CUDie, *CU, ModuleMap)) {
4090 Units.push_back(llvm::make_unique<CompileUnit>(
4091 *CU, UnitID++, !Options.NoODR && !Options.Update, ""));
4092 maybeUpdateMaxDwarfVersion(CU->getVersion());
4093 }
4094 }
4095
4096 // Now build the DIE parent links that we will use during the next phase.
4097 for (auto &CurrentUnit : Units)
4098 analyzeContextInfo(CurrentUnit->getOrigUnit().getUnitDIE(), 0,
4099 *CurrentUnit, &ODRContexts.getRoot(), StringPool,
4100 ODRContexts);
4101
4102 // Then mark all the DIEs that need to be present in the linked
4103 // output and collect some information about them. Note that this
4104 // loop can not be merged with the previous one because cross-CU
4105 // references require the ParentIdx to be setup for every CU in
4106 // the object file before calling this.
4107 if (LLVM_UNLIKELY(Options.Update)__builtin_expect((bool)(Options.Update), false)) {
4108 for (auto &CurrentUnit : Units)
4109 CurrentUnit->markEverythingAsKept();
4110 Streamer->copyInvariantDebugSection(*ErrOrObj, Options);
4111 } else {
4112 for (auto &CurrentUnit : Units)
4113 lookForDIEsToKeep(RelocMgr, CurrentUnit->getOrigUnit().getUnitDIE(),
4114 *Obj, *CurrentUnit, 0);
4115 }
4116
4117 // The calls to applyValidRelocs inside cloneDIE will walk the
4118 // reloc array again (in the same way findValidRelocsInDebugInfo()
4119 // did). We need to reset the NextValidReloc index to the beginning.
4120 RelocMgr.resetValidRelocs();
4121 if (RelocMgr.hasValidRelocs() || LLVM_UNLIKELY(Options.Update)__builtin_expect((bool)(Options.Update), false))
4122 DIECloner(*this, RelocMgr, DIEAlloc, Units, Options)
4123 .cloneAllCompileUnits(*DwarfContext);
4124 if (!Options.NoOutput && !Units.empty() && LLVM_LIKELY(!Options.Update)__builtin_expect((bool)(!Options.Update), true))
4125 patchFrameInfoForObject(*Obj, *DwarfContext,
4126 Units[0]->getOrigUnit().getAddressByteSize());
4127
4128 // Clean-up before starting working on the next object.
4129 endDebugObject();
4130 }
4131
4132 // Emit everything that's global.
4133 if (!Options.NoOutput) {
4134 Streamer->emitAbbrevs(Abbreviations, MaxDwarfVersion);
4135 Streamer->emitStrings(StringPool);
4136 Streamer->emitAppleNames(AppleNames);
4137 Streamer->emitAppleNamespaces(AppleNamespaces);
4138 Streamer->emitAppleTypes(AppleTypes);
4139 Streamer->emitAppleObjc(AppleObjc);
4140 }
4141
4142 return Options.NoOutput ? true : Streamer->finish(Map);
4143}
4144
4145DwarfStringPoolEntryRef NonRelocatableStringpool::getEntry(StringRef S) {
4146 if (S.empty() && !Strings.empty())
4147 return EmptyString;
4148
4149 auto I = Strings.insert(std::make_pair(S, DwarfStringPoolEntry()));
4150 auto &Entry = I.first->second;
4151 if (I.second || Entry.Index == -1U) {
4152 Entry.Index = NumEntries++;
4153 Entry.Offset = CurrentEndOffset;
4154 Entry.Symbol = nullptr;
4155 CurrentEndOffset += S.size() + 1;
4156 }
4157 return DwarfStringPoolEntryRef(*I.first);
4158}
4159
4160uint32_t NonRelocatableStringpool::getStringOffset(StringRef S) {
4161 return getEntry(S).getOffset();
4162}
4163
4164/// Put \p S into the StringMap so that it gets permanent
4165/// storage, but do not actually link it in the chain of elements
4166/// that go into the output section. A latter call to
4167/// getStringOffset() with the same string will chain it though.
4168StringRef NonRelocatableStringpool::internString(StringRef S) {
4169 DwarfStringPoolEntry Entry{nullptr, 0, -1U};
4170 auto InsertResult = Strings.insert(std::make_pair(S, Entry));
4171 return InsertResult.first->getKey();
4172}
4173
4174std::vector<DwarfStringPoolEntryRef>
4175NonRelocatableStringpool::getEntries() const {
4176 std::vector<DwarfStringPoolEntryRef> Result;
4177 Result.reserve(Strings.size());
4178 for (const auto &E : Strings)
4179 Result.emplace_back(E);
4180 std::sort(
4181 Result.begin(), Result.end(),
4182 [](const DwarfStringPoolEntryRef A, const DwarfStringPoolEntryRef B) {
4183 return A.getIndex() < B.getIndex();
4184 });
4185 return Result;
4186}
4187
4188void warn(const Twine &Warning, const Twine &Context) {
4189 errs() << Twine("while processing ") + Context + ":\n";
4190 errs() << Twine("warning: ") + Warning + "\n";
4191}
4192
4193bool error(const Twine &Error, const Twine &Context) {
4194 errs() << Twine("while processing ") + Context + ":\n";
4195 errs() << Twine("error: ") + Error + "\n";
4196 return false;
4197}
4198
4199bool linkDwarf(raw_fd_ostream &OutFile, const DebugMap &DM,
4200 const LinkOptions &Options) {
4201 DwarfLinker Linker(OutFile, Options);
4202 return Linker.link(DM);
4203}
4204
4205} // end namespace dsymutil
4206} // end namespace llvm