Frederic Riss | 231f714 | 2014-12-12 17:31:24 +0000 | [diff] [blame] | 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 | #include "DebugMap.h" |
Frederic Riss | d345518 | 2015-01-28 18:27:01 +0000 | [diff] [blame] | 10 | #include "BinaryHolder.h" |
| 11 | #include "DebugMap.h" |
Frederic Riss | 231f714 | 2014-12-12 17:31:24 +0000 | [diff] [blame] | 12 | #include "dsymutil.h" |
Frederic Riss | 24faade | 2015-09-02 16:49:13 +0000 | [diff] [blame^] | 13 | #include "MachOUtils.h" |
Frederic Riss | 30711fb | 2015-08-26 05:09:52 +0000 | [diff] [blame] | 14 | #include "NonRelocatableStringpool.h" |
Frederic Riss | 1af75f7 | 2015-03-12 18:45:10 +0000 | [diff] [blame] | 15 | #include "llvm/ADT/IntervalMap.h" |
Frederic Riss | 1b9be2c | 2015-03-11 18:46:01 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/StringMap.h" |
| 17 | #include "llvm/ADT/STLExtras.h" |
Frederic Riss | c99ea20 | 2015-02-28 00:29:11 +0000 | [diff] [blame] | 18 | #include "llvm/CodeGen/AsmPrinter.h" |
Frederic Riss | b8b43d5 | 2015-03-04 22:07:44 +0000 | [diff] [blame] | 19 | #include "llvm/CodeGen/DIE.h" |
Frederic Riss | 1c65094 | 2015-07-21 22:41:43 +0000 | [diff] [blame] | 20 | #include "llvm/Config/config.h" |
Zachary Turner | 82af943 | 2015-01-30 18:07:45 +0000 | [diff] [blame] | 21 | #include "llvm/DebugInfo/DWARF/DWARFContext.h" |
| 22 | #include "llvm/DebugInfo/DWARF/DWARFDebugInfoEntry.h" |
Frederic Riss | 1b9da42 | 2015-02-13 23:18:29 +0000 | [diff] [blame] | 23 | #include "llvm/DebugInfo/DWARF/DWARFFormValue.h" |
Frederic Riss | c99ea20 | 2015-02-28 00:29:11 +0000 | [diff] [blame] | 24 | #include "llvm/MC/MCAsmBackend.h" |
| 25 | #include "llvm/MC/MCAsmInfo.h" |
| 26 | #include "llvm/MC/MCContext.h" |
| 27 | #include "llvm/MC/MCCodeEmitter.h" |
Frederic Riss | 63786b0 | 2015-03-15 20:45:43 +0000 | [diff] [blame] | 28 | #include "llvm/MC/MCDwarf.h" |
Frederic Riss | c99ea20 | 2015-02-28 00:29:11 +0000 | [diff] [blame] | 29 | #include "llvm/MC/MCInstrInfo.h" |
| 30 | #include "llvm/MC/MCObjectFileInfo.h" |
| 31 | #include "llvm/MC/MCRegisterInfo.h" |
| 32 | #include "llvm/MC/MCStreamer.h" |
Pete Cooper | 81902a3 | 2015-05-15 22:19:42 +0000 | [diff] [blame] | 33 | #include "llvm/MC/MCSubtargetInfo.h" |
Frederic Riss | 1036e64 | 2015-02-13 23:18:22 +0000 | [diff] [blame] | 34 | #include "llvm/Object/MachO.h" |
Frederic Riss | 84c09a5 | 2015-02-13 23:18:34 +0000 | [diff] [blame] | 35 | #include "llvm/Support/Dwarf.h" |
| 36 | #include "llvm/Support/LEB128.h" |
Frederic Riss | c99ea20 | 2015-02-28 00:29:11 +0000 | [diff] [blame] | 37 | #include "llvm/Support/TargetRegistry.h" |
| 38 | #include "llvm/Target/TargetMachine.h" |
| 39 | #include "llvm/Target/TargetOptions.h" |
Frederic Riss | d345518 | 2015-01-28 18:27:01 +0000 | [diff] [blame] | 40 | #include <string> |
Frederic Riss | 6afcfce | 2015-03-13 18:35:57 +0000 | [diff] [blame] | 41 | #include <tuple> |
Frederic Riss | 231f714 | 2014-12-12 17:31:24 +0000 | [diff] [blame] | 42 | |
| 43 | namespace llvm { |
| 44 | namespace dsymutil { |
| 45 | |
Frederic Riss | d345518 | 2015-01-28 18:27:01 +0000 | [diff] [blame] | 46 | namespace { |
| 47 | |
Frederic Riss | 1af75f7 | 2015-03-12 18:45:10 +0000 | [diff] [blame] | 48 | template <typename KeyT, typename ValT> |
| 49 | using HalfOpenIntervalMap = |
| 50 | IntervalMap<KeyT, ValT, IntervalMapImpl::NodeSizer<KeyT, ValT>::LeafSize, |
| 51 | IntervalMapHalfOpenInfo<KeyT>>; |
| 52 | |
Frederic Riss | 2544087 | 2015-03-13 23:30:31 +0000 | [diff] [blame] | 53 | typedef HalfOpenIntervalMap<uint64_t, int64_t> FunctionIntervals; |
| 54 | |
Duncan P. N. Exon Smith | 4fb1f9c | 2015-06-25 23:46:41 +0000 | [diff] [blame] | 55 | // FIXME: Delete this structure. |
Duncan P. N. Exon Smith | e7e1d0c | 2015-05-27 22:14:58 +0000 | [diff] [blame] | 56 | struct PatchLocation { |
Duncan P. N. Exon Smith | 4fb1f9c | 2015-06-25 23:46:41 +0000 | [diff] [blame] | 57 | DIE::value_iterator I; |
Duncan P. N. Exon Smith | e7e1d0c | 2015-05-27 22:14:58 +0000 | [diff] [blame] | 58 | |
Duncan P. N. Exon Smith | 4fb1f9c | 2015-06-25 23:46:41 +0000 | [diff] [blame] | 59 | PatchLocation() = default; |
| 60 | PatchLocation(DIE::value_iterator I) : I(I) {} |
Duncan P. N. Exon Smith | e7e1d0c | 2015-05-27 22:14:58 +0000 | [diff] [blame] | 61 | |
| 62 | void set(uint64_t New) const { |
Duncan P. N. Exon Smith | 4fb1f9c | 2015-06-25 23:46:41 +0000 | [diff] [blame] | 63 | assert(I); |
| 64 | const auto &Old = *I; |
Duncan P. N. Exon Smith | 815a6eb5 | 2015-05-27 22:31:41 +0000 | [diff] [blame] | 65 | assert(Old.getType() == DIEValue::isInteger); |
Duncan P. N. Exon Smith | 4fb1f9c | 2015-06-25 23:46:41 +0000 | [diff] [blame] | 66 | *I = DIEValue(Old.getAttribute(), Old.getForm(), DIEInteger(New)); |
Duncan P. N. Exon Smith | e7e1d0c | 2015-05-27 22:14:58 +0000 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | uint64_t get() const { |
Duncan P. N. Exon Smith | 4fb1f9c | 2015-06-25 23:46:41 +0000 | [diff] [blame] | 70 | assert(I); |
| 71 | return I->getDIEInteger().getValue(); |
Duncan P. N. Exon Smith | e7e1d0c | 2015-05-27 22:14:58 +0000 | [diff] [blame] | 72 | } |
| 73 | }; |
| 74 | |
Frederic Riss | 1c65094 | 2015-07-21 22:41:43 +0000 | [diff] [blame] | 75 | class CompileUnit; |
| 76 | struct DeclMapInfo; |
Frederic Riss | 1c65094 | 2015-07-21 22:41:43 +0000 | [diff] [blame] | 77 | |
| 78 | /// A DeclContext is a named program scope that is used for ODR |
| 79 | /// uniquing of types. |
| 80 | /// The set of DeclContext for the ODR-subject parts of a Dwarf link |
| 81 | /// is expanded (and uniqued) with each new object file processed. We |
| 82 | /// need to determine the context of each DIE in an linked object file |
| 83 | /// to see if the corresponding type has already been emitted. |
| 84 | /// |
| 85 | /// The contexts are conceptually organised as a tree (eg. a function |
| 86 | /// scope is contained in a namespace scope that contains other |
| 87 | /// scopes), but storing/accessing them in an actual tree is too |
| 88 | /// inefficient: we need to be able to very quickly query a context |
| 89 | /// for a given child context by name. Storing a StringMap in each |
| 90 | /// DeclContext would be too space inefficient. |
| 91 | /// The solution here is to give each DeclContext a link to its parent |
| 92 | /// (this allows to walk up the tree), but to query the existance of a |
| 93 | /// specific DeclContext using a separate DenseMap keyed on the hash |
| 94 | /// of the fully qualified name of the context. |
| 95 | class DeclContext { |
| 96 | unsigned QualifiedNameHash; |
| 97 | uint32_t Line; |
| 98 | uint32_t ByteSize; |
| 99 | uint16_t Tag; |
| 100 | StringRef Name; |
| 101 | StringRef File; |
| 102 | const DeclContext &Parent; |
| 103 | const DWARFDebugInfoEntryMinimal *LastSeenDIE; |
| 104 | uint32_t LastSeenCompileUnitID; |
| 105 | uint32_t CanonicalDIEOffset; |
| 106 | |
| 107 | friend DeclMapInfo; |
| 108 | |
| 109 | public: |
| 110 | typedef DenseSet<DeclContext *, DeclMapInfo> Map; |
| 111 | |
| 112 | DeclContext() |
| 113 | : QualifiedNameHash(0), Line(0), ByteSize(0), |
| 114 | Tag(dwarf::DW_TAG_compile_unit), Name(), File(), Parent(*this), |
| 115 | LastSeenDIE(nullptr), LastSeenCompileUnitID(0), CanonicalDIEOffset(0) {} |
| 116 | |
| 117 | DeclContext(unsigned Hash, uint32_t Line, uint32_t ByteSize, uint16_t Tag, |
| 118 | StringRef Name, StringRef File, const DeclContext &Parent, |
| 119 | const DWARFDebugInfoEntryMinimal *LastSeenDIE = nullptr, |
| 120 | unsigned CUId = 0) |
| 121 | : QualifiedNameHash(Hash), Line(Line), ByteSize(ByteSize), Tag(Tag), |
| 122 | Name(Name), File(File), Parent(Parent), LastSeenDIE(LastSeenDIE), |
| 123 | LastSeenCompileUnitID(CUId), CanonicalDIEOffset(0) {} |
| 124 | |
| 125 | uint32_t getQualifiedNameHash() const { return QualifiedNameHash; } |
| 126 | |
| 127 | bool setLastSeenDIE(CompileUnit &U, const DWARFDebugInfoEntryMinimal *Die); |
| 128 | |
| 129 | uint32_t getCanonicalDIEOffset() const { return CanonicalDIEOffset; } |
| 130 | void setCanonicalDIEOffset(uint32_t Offset) { CanonicalDIEOffset = Offset; } |
| 131 | |
| 132 | uint16_t getTag() const { return Tag; } |
| 133 | StringRef getName() const { return Name; } |
| 134 | }; |
| 135 | |
| 136 | /// Info type for the DenseMap storing the DeclContext pointers. |
| 137 | struct DeclMapInfo : private DenseMapInfo<DeclContext *> { |
| 138 | using DenseMapInfo<DeclContext *>::getEmptyKey; |
| 139 | using DenseMapInfo<DeclContext *>::getTombstoneKey; |
| 140 | |
| 141 | static unsigned getHashValue(const DeclContext *Ctxt) { |
| 142 | return Ctxt->QualifiedNameHash; |
| 143 | } |
| 144 | |
| 145 | static bool isEqual(const DeclContext *LHS, const DeclContext *RHS) { |
| 146 | if (RHS == getEmptyKey() || RHS == getTombstoneKey()) |
| 147 | return RHS == LHS; |
| 148 | return LHS->QualifiedNameHash == RHS->QualifiedNameHash && |
| 149 | LHS->Line == RHS->Line && LHS->ByteSize == RHS->ByteSize && |
| 150 | LHS->Name.data() == RHS->Name.data() && |
| 151 | LHS->File.data() == RHS->File.data() && |
| 152 | LHS->Parent.QualifiedNameHash == RHS->Parent.QualifiedNameHash; |
| 153 | } |
| 154 | }; |
| 155 | |
| 156 | /// This class gives a tree-like API to the DenseMap that stores the |
| 157 | /// DeclContext objects. It also holds the BumpPtrAllocator where |
| 158 | /// these objects will be allocated. |
| 159 | class DeclContextTree { |
| 160 | BumpPtrAllocator Allocator; |
| 161 | DeclContext Root; |
| 162 | DeclContext::Map Contexts; |
| 163 | |
| 164 | public: |
| 165 | /// Get the child of \a Context described by \a DIE in \a Unit. The |
| 166 | /// required strings will be interned in \a StringPool. |
| 167 | /// \returns The child DeclContext along with one bit that is set if |
| 168 | /// this context is invalid. |
| 169 | /// FIXME: the invalid bit along the return value is to emulate some |
| 170 | /// dsymutil-classic functionality. See the fucntion definition for |
| 171 | /// a more thorough discussion of its use. |
| 172 | PointerIntPair<DeclContext *, 1> |
| 173 | getChildDeclContext(DeclContext &Context, |
| 174 | const DWARFDebugInfoEntryMinimal *DIE, CompileUnit &Unit, |
| 175 | NonRelocatableStringpool &StringPool); |
| 176 | |
| 177 | DeclContext &getRoot() { return Root; } |
| 178 | }; |
| 179 | |
Frederic Riss | 563cba6 | 2015-01-28 22:15:14 +0000 | [diff] [blame] | 180 | /// \brief Stores all information relating to a compile unit, be it in |
| 181 | /// its original instance in the object file to its brand new cloned |
| 182 | /// and linked DIE tree. |
| 183 | class CompileUnit { |
| 184 | public: |
| 185 | /// \brief Information gathered about a DIE in the object file. |
| 186 | struct DIEInfo { |
Frederic Riss | 31da324 | 2015-03-11 18:45:52 +0000 | [diff] [blame] | 187 | int64_t AddrAdjust; ///< Address offset to apply to the described entity. |
Frederic Riss | 1c65094 | 2015-07-21 22:41:43 +0000 | [diff] [blame] | 188 | DeclContext *Ctxt; ///< ODR Declaration context. |
Frederic Riss | 9833de6 | 2015-03-06 23:22:53 +0000 | [diff] [blame] | 189 | DIE *Clone; ///< Cloned version of that DIE. |
Frederic Riss | 84c09a5 | 2015-02-13 23:18:34 +0000 | [diff] [blame] | 190 | uint32_t ParentIdx; ///< The index of this DIE's parent. |
| 191 | bool Keep; ///< Is the DIE part of the linked output? |
| 192 | bool InDebugMap; ///< Was this DIE's entity found in the map? |
Frederic Riss | 563cba6 | 2015-01-28 22:15:14 +0000 | [diff] [blame] | 193 | }; |
| 194 | |
Frederic Riss | 1c65094 | 2015-07-21 22:41:43 +0000 | [diff] [blame] | 195 | CompileUnit(DWARFUnit &OrigUnit, unsigned ID, bool CanUseODR) |
Frederic Riss | 3cced05 | 2015-03-14 03:46:40 +0000 | [diff] [blame] | 196 | : OrigUnit(OrigUnit), ID(ID), LowPc(UINT64_MAX), HighPc(0), RangeAlloc(), |
Duncan P. N. Exon Smith | e7e1d0c | 2015-05-27 22:14:58 +0000 | [diff] [blame] | 197 | Ranges(RangeAlloc) { |
Frederic Riss | 563cba6 | 2015-01-28 22:15:14 +0000 | [diff] [blame] | 198 | Info.resize(OrigUnit.getNumDIEs()); |
Frederic Riss | 1c65094 | 2015-07-21 22:41:43 +0000 | [diff] [blame] | 199 | |
| 200 | const auto *CUDie = OrigUnit.getUnitDIE(false); |
| 201 | unsigned Lang = CUDie->getAttributeValueAsUnsignedConstant( |
| 202 | &OrigUnit, dwarf::DW_AT_language, 0); |
| 203 | HasODR = CanUseODR && (Lang == dwarf::DW_LANG_C_plus_plus || |
| 204 | Lang == dwarf::DW_LANG_C_plus_plus_03 || |
| 205 | Lang == dwarf::DW_LANG_C_plus_plus_11 || |
| 206 | Lang == dwarf::DW_LANG_C_plus_plus_14 || |
| 207 | Lang == dwarf::DW_LANG_ObjC_plus_plus); |
Frederic Riss | 563cba6 | 2015-01-28 22:15:14 +0000 | [diff] [blame] | 208 | } |
| 209 | |
Frederic Riss | 2838f9e | 2015-03-05 05:29:05 +0000 | [diff] [blame] | 210 | CompileUnit(CompileUnit &&RHS) |
| 211 | : OrigUnit(RHS.OrigUnit), Info(std::move(RHS.Info)), |
| 212 | CUDie(std::move(RHS.CUDie)), StartOffset(RHS.StartOffset), |
Frederic Riss | 1af75f7 | 2015-03-12 18:45:10 +0000 | [diff] [blame] | 213 | NextUnitOffset(RHS.NextUnitOffset), RangeAlloc(), Ranges(RangeAlloc) { |
| 214 | // The CompileUnit container has been 'reserve()'d with the right |
| 215 | // size. We cannot move the IntervalMap anyway. |
| 216 | llvm_unreachable("CompileUnits should not be moved."); |
| 217 | } |
David Blaikie | a8adc13 | 2015-03-04 22:20:52 +0000 | [diff] [blame] | 218 | |
Frederic Riss | c3349d4 | 2015-02-13 23:18:27 +0000 | [diff] [blame] | 219 | DWARFUnit &getOrigUnit() const { return OrigUnit; } |
Frederic Riss | 563cba6 | 2015-01-28 22:15:14 +0000 | [diff] [blame] | 220 | |
Frederic Riss | 3cced05 | 2015-03-14 03:46:40 +0000 | [diff] [blame] | 221 | unsigned getUniqueID() const { return ID; } |
| 222 | |
Duncan P. N. Exon Smith | 827200c | 2015-06-25 23:52:10 +0000 | [diff] [blame] | 223 | DIE *getOutputUnitDIE() const { return CUDie; } |
| 224 | void setOutputUnitDIE(DIE *Die) { CUDie = Die; } |
Frederic Riss | b8b43d5 | 2015-03-04 22:07:44 +0000 | [diff] [blame] | 225 | |
Frederic Riss | 1c65094 | 2015-07-21 22:41:43 +0000 | [diff] [blame] | 226 | bool hasODR() const { return HasODR; } |
| 227 | |
Frederic Riss | 563cba6 | 2015-01-28 22:15:14 +0000 | [diff] [blame] | 228 | DIEInfo &getInfo(unsigned Idx) { return Info[Idx]; } |
| 229 | const DIEInfo &getInfo(unsigned Idx) const { return Info[Idx]; } |
| 230 | |
Frederic Riss | b8b43d5 | 2015-03-04 22:07:44 +0000 | [diff] [blame] | 231 | uint64_t getStartOffset() const { return StartOffset; } |
| 232 | uint64_t getNextUnitOffset() const { return NextUnitOffset; } |
Frederic Riss | 9552948 | 2015-03-13 23:30:27 +0000 | [diff] [blame] | 233 | void setStartOffset(uint64_t DebugInfoSize) { StartOffset = DebugInfoSize; } |
Frederic Riss | b8b43d5 | 2015-03-04 22:07:44 +0000 | [diff] [blame] | 234 | |
Frederic Riss | 5a62dc3 | 2015-03-13 18:35:54 +0000 | [diff] [blame] | 235 | uint64_t getLowPc() const { return LowPc; } |
| 236 | uint64_t getHighPc() const { return HighPc; } |
| 237 | |
Duncan P. N. Exon Smith | e7e1d0c | 2015-05-27 22:14:58 +0000 | [diff] [blame] | 238 | Optional<PatchLocation> getUnitRangesAttribute() const { |
| 239 | return UnitRangeAttribute; |
| 240 | } |
Frederic Riss | 2544087 | 2015-03-13 23:30:31 +0000 | [diff] [blame] | 241 | const FunctionIntervals &getFunctionRanges() const { return Ranges; } |
Duncan P. N. Exon Smith | e7e1d0c | 2015-05-27 22:14:58 +0000 | [diff] [blame] | 242 | const std::vector<PatchLocation> &getRangesAttributes() const { |
Frederic Riss | 2544087 | 2015-03-13 23:30:31 +0000 | [diff] [blame] | 243 | return RangeAttributes; |
| 244 | } |
Frederic Riss | 9d441b6 | 2015-03-06 23:22:50 +0000 | [diff] [blame] | 245 | |
Duncan P. N. Exon Smith | e7e1d0c | 2015-05-27 22:14:58 +0000 | [diff] [blame] | 246 | const std::vector<std::pair<PatchLocation, int64_t>> & |
Frederic Riss | dfb9790 | 2015-03-14 15:49:07 +0000 | [diff] [blame] | 247 | getLocationAttributes() const { |
| 248 | return LocationAttributes; |
| 249 | } |
| 250 | |
Frederic Riss | 9d441b6 | 2015-03-06 23:22:50 +0000 | [diff] [blame] | 251 | /// \brief Compute the end offset for this unit. Must be |
| 252 | /// called after the CU's DIEs have been cloned. |
Frederic Riss | b8b43d5 | 2015-03-04 22:07:44 +0000 | [diff] [blame] | 253 | /// \returns the next unit offset (which is also the current |
| 254 | /// debug_info section size). |
Frederic Riss | 9d441b6 | 2015-03-06 23:22:50 +0000 | [diff] [blame] | 255 | uint64_t computeNextUnitOffset(); |
Frederic Riss | b8b43d5 | 2015-03-04 22:07:44 +0000 | [diff] [blame] | 256 | |
Frederic Riss | 6afcfce | 2015-03-13 18:35:57 +0000 | [diff] [blame] | 257 | /// \brief Keep track of a forward reference to DIE \p Die in \p |
| 258 | /// RefUnit by \p Attr. The attribute should be fixed up later to |
Frederic Riss | 1c65094 | 2015-07-21 22:41:43 +0000 | [diff] [blame] | 259 | /// point to the absolute offset of \p Die in the debug_info section |
| 260 | /// or to the canonical offset of \p Ctxt if it is non-null. |
Frederic Riss | 6afcfce | 2015-03-13 18:35:57 +0000 | [diff] [blame] | 261 | void noteForwardReference(DIE *Die, const CompileUnit *RefUnit, |
Frederic Riss | 1c65094 | 2015-07-21 22:41:43 +0000 | [diff] [blame] | 262 | DeclContext *Ctxt, PatchLocation Attr); |
Frederic Riss | 9833de6 | 2015-03-06 23:22:53 +0000 | [diff] [blame] | 263 | |
| 264 | /// \brief Apply all fixups recored by noteForwardReference(). |
| 265 | void fixupForwardReferences(); |
| 266 | |
Frederic Riss | 1af75f7 | 2015-03-12 18:45:10 +0000 | [diff] [blame] | 267 | /// \brief Add a function range [\p LowPC, \p HighPC) that is |
| 268 | /// relocatad by applying offset \p PCOffset. |
| 269 | void addFunctionRange(uint64_t LowPC, uint64_t HighPC, int64_t PCOffset); |
| 270 | |
Frederic Riss | 5c9c706 | 2015-03-13 23:55:29 +0000 | [diff] [blame] | 271 | /// \brief Keep track of a DW_AT_range attribute that we will need to |
Frederic Riss | 2544087 | 2015-03-13 23:30:31 +0000 | [diff] [blame] | 272 | /// patch up later. |
Duncan P. N. Exon Smith | e7e1d0c | 2015-05-27 22:14:58 +0000 | [diff] [blame] | 273 | void noteRangeAttribute(const DIE &Die, PatchLocation Attr); |
Frederic Riss | 2544087 | 2015-03-13 23:30:31 +0000 | [diff] [blame] | 274 | |
Frederic Riss | dfb9790 | 2015-03-14 15:49:07 +0000 | [diff] [blame] | 275 | /// \brief Keep track of a location attribute pointing to a location |
| 276 | /// list in the debug_loc section. |
Duncan P. N. Exon Smith | e7e1d0c | 2015-05-27 22:14:58 +0000 | [diff] [blame] | 277 | void noteLocationAttribute(PatchLocation Attr, int64_t PcOffset); |
Frederic Riss | dfb9790 | 2015-03-14 15:49:07 +0000 | [diff] [blame] | 278 | |
Frederic Riss | bce93ff | 2015-03-16 02:05:10 +0000 | [diff] [blame] | 279 | /// \brief Add a name accelerator entry for \p Die with \p Name |
| 280 | /// which is stored in the string table at \p Offset. |
| 281 | void addNameAccelerator(const DIE *Die, const char *Name, uint32_t Offset, |
| 282 | bool SkipPubnamesSection = false); |
| 283 | |
| 284 | /// \brief Add a type accelerator entry for \p Die with \p Name |
| 285 | /// which is stored in the string table at \p Offset. |
| 286 | void addTypeAccelerator(const DIE *Die, const char *Name, uint32_t Offset); |
| 287 | |
| 288 | struct AccelInfo { |
Frederic Riss | f37964c | 2015-06-05 20:27:07 +0000 | [diff] [blame] | 289 | StringRef Name; ///< Name of the entry. |
| 290 | const DIE *Die; ///< DIE this entry describes. |
Frederic Riss | bce93ff | 2015-03-16 02:05:10 +0000 | [diff] [blame] | 291 | uint32_t NameOffset; ///< Offset of Name in the string pool. |
| 292 | bool SkipPubSection; ///< Emit this entry only in the apple_* sections. |
| 293 | |
| 294 | AccelInfo(StringRef Name, const DIE *Die, uint32_t NameOffset, |
| 295 | bool SkipPubSection = false) |
| 296 | : Name(Name), Die(Die), NameOffset(NameOffset), |
| 297 | SkipPubSection(SkipPubSection) {} |
| 298 | }; |
| 299 | |
| 300 | const std::vector<AccelInfo> &getPubnames() const { return Pubnames; } |
| 301 | const std::vector<AccelInfo> &getPubtypes() const { return Pubtypes; } |
| 302 | |
Frederic Riss | 1c65094 | 2015-07-21 22:41:43 +0000 | [diff] [blame] | 303 | /// Get the full path for file \a FileNum in the line table |
| 304 | const char *getResolvedPath(unsigned FileNum) { |
| 305 | if (FileNum >= ResolvedPaths.size()) |
| 306 | return nullptr; |
| 307 | return ResolvedPaths[FileNum].size() ? ResolvedPaths[FileNum].c_str() |
| 308 | : nullptr; |
| 309 | } |
| 310 | |
| 311 | /// Set the fully resolved path for the line-table's file \a FileNum |
| 312 | /// to \a Path. |
| 313 | void setResolvedPath(unsigned FileNum, const std::string &Path) { |
| 314 | if (ResolvedPaths.size() <= FileNum) |
| 315 | ResolvedPaths.resize(FileNum + 1); |
| 316 | ResolvedPaths[FileNum] = Path; |
| 317 | } |
| 318 | |
Frederic Riss | 563cba6 | 2015-01-28 22:15:14 +0000 | [diff] [blame] | 319 | private: |
| 320 | DWARFUnit &OrigUnit; |
Frederic Riss | 3cced05 | 2015-03-14 03:46:40 +0000 | [diff] [blame] | 321 | unsigned ID; |
Frederic Riss | 1c65094 | 2015-07-21 22:41:43 +0000 | [diff] [blame] | 322 | std::vector<DIEInfo> Info; ///< DIE info indexed by DIE index. |
| 323 | DIE *CUDie; ///< Root of the linked DIE tree. |
Frederic Riss | b8b43d5 | 2015-03-04 22:07:44 +0000 | [diff] [blame] | 324 | |
| 325 | uint64_t StartOffset; |
| 326 | uint64_t NextUnitOffset; |
Frederic Riss | 9833de6 | 2015-03-06 23:22:53 +0000 | [diff] [blame] | 327 | |
Frederic Riss | 5a62dc3 | 2015-03-13 18:35:54 +0000 | [diff] [blame] | 328 | uint64_t LowPc; |
| 329 | uint64_t HighPc; |
| 330 | |
Frederic Riss | 9833de6 | 2015-03-06 23:22:53 +0000 | [diff] [blame] | 331 | /// \brief A list of attributes to fixup with the absolute offset of |
| 332 | /// a DIE in the debug_info section. |
| 333 | /// |
| 334 | /// The offsets for the attributes in this array couldn't be set while |
Frederic Riss | 6afcfce | 2015-03-13 18:35:57 +0000 | [diff] [blame] | 335 | /// cloning because for cross-cu forward refences the target DIE's |
| 336 | /// offset isn't known you emit the reference attribute. |
Frederic Riss | 1c65094 | 2015-07-21 22:41:43 +0000 | [diff] [blame] | 337 | std::vector<std::tuple<DIE *, const CompileUnit *, DeclContext *, |
| 338 | PatchLocation>> ForwardDIEReferences; |
Frederic Riss | 1af75f7 | 2015-03-12 18:45:10 +0000 | [diff] [blame] | 339 | |
Frederic Riss | 2544087 | 2015-03-13 23:30:31 +0000 | [diff] [blame] | 340 | FunctionIntervals::Allocator RangeAlloc; |
Frederic Riss | 1af75f7 | 2015-03-12 18:45:10 +0000 | [diff] [blame] | 341 | /// \brief The ranges in that interval map are the PC ranges for |
| 342 | /// functions in this unit, associated with the PC offset to apply |
| 343 | /// to the addresses to get the linked address. |
Frederic Riss | 2544087 | 2015-03-13 23:30:31 +0000 | [diff] [blame] | 344 | FunctionIntervals Ranges; |
| 345 | |
| 346 | /// \brief DW_AT_ranges attributes to patch after we have gathered |
| 347 | /// all the unit's function addresses. |
| 348 | /// @{ |
Duncan P. N. Exon Smith | e7e1d0c | 2015-05-27 22:14:58 +0000 | [diff] [blame] | 349 | std::vector<PatchLocation> RangeAttributes; |
| 350 | Optional<PatchLocation> UnitRangeAttribute; |
Frederic Riss | 2544087 | 2015-03-13 23:30:31 +0000 | [diff] [blame] | 351 | /// @} |
Frederic Riss | dfb9790 | 2015-03-14 15:49:07 +0000 | [diff] [blame] | 352 | |
| 353 | /// \brief Location attributes that need to be transfered from th |
| 354 | /// original debug_loc section to the liked one. They are stored |
| 355 | /// along with the PC offset that is to be applied to their |
| 356 | /// function's address. |
Duncan P. N. Exon Smith | e7e1d0c | 2015-05-27 22:14:58 +0000 | [diff] [blame] | 357 | std::vector<std::pair<PatchLocation, int64_t>> LocationAttributes; |
Frederic Riss | bce93ff | 2015-03-16 02:05:10 +0000 | [diff] [blame] | 358 | |
| 359 | /// \brief Accelerator entries for the unit, both for the pub* |
| 360 | /// sections and the apple* ones. |
| 361 | /// @{ |
| 362 | std::vector<AccelInfo> Pubnames; |
| 363 | std::vector<AccelInfo> Pubtypes; |
| 364 | /// @} |
Frederic Riss | 1c65094 | 2015-07-21 22:41:43 +0000 | [diff] [blame] | 365 | |
| 366 | /// Cached resolved paths from the line table. |
| 367 | std::vector<std::string> ResolvedPaths; |
| 368 | |
| 369 | /// Is this unit subject to the ODR rule? |
| 370 | bool HasODR; |
Frederic Riss | 563cba6 | 2015-01-28 22:15:14 +0000 | [diff] [blame] | 371 | }; |
| 372 | |
Frederic Riss | 9d441b6 | 2015-03-06 23:22:50 +0000 | [diff] [blame] | 373 | uint64_t CompileUnit::computeNextUnitOffset() { |
Frederic Riss | b8b43d5 | 2015-03-04 22:07:44 +0000 | [diff] [blame] | 374 | NextUnitOffset = StartOffset + 11 /* Header size */; |
| 375 | // The root DIE might be null, meaning that the Unit had nothing to |
| 376 | // contribute to the linked output. In that case, we will emit the |
| 377 | // unit header without any actual DIE. |
| 378 | if (CUDie) |
| 379 | NextUnitOffset += CUDie->getSize(); |
| 380 | return NextUnitOffset; |
| 381 | } |
| 382 | |
Frederic Riss | 6afcfce | 2015-03-13 18:35:57 +0000 | [diff] [blame] | 383 | /// \brief Keep track of a forward cross-cu reference from this unit |
| 384 | /// to \p Die that lives in \p RefUnit. |
| 385 | void CompileUnit::noteForwardReference(DIE *Die, const CompileUnit *RefUnit, |
Frederic Riss | 1c65094 | 2015-07-21 22:41:43 +0000 | [diff] [blame] | 386 | DeclContext *Ctxt, PatchLocation Attr) { |
| 387 | ForwardDIEReferences.emplace_back(Die, RefUnit, Ctxt, Attr); |
Frederic Riss | 9833de6 | 2015-03-06 23:22:53 +0000 | [diff] [blame] | 388 | } |
| 389 | |
| 390 | /// \brief Apply all fixups recorded by noteForwardReference(). |
| 391 | void CompileUnit::fixupForwardReferences() { |
Frederic Riss | 6afcfce | 2015-03-13 18:35:57 +0000 | [diff] [blame] | 392 | for (const auto &Ref : ForwardDIEReferences) { |
| 393 | DIE *RefDie; |
| 394 | const CompileUnit *RefUnit; |
Duncan P. N. Exon Smith | e7e1d0c | 2015-05-27 22:14:58 +0000 | [diff] [blame] | 395 | PatchLocation Attr; |
Frederic Riss | 1c65094 | 2015-07-21 22:41:43 +0000 | [diff] [blame] | 396 | DeclContext *Ctxt; |
| 397 | std::tie(RefDie, RefUnit, Ctxt, Attr) = Ref; |
| 398 | if (Ctxt && Ctxt->getCanonicalDIEOffset()) |
| 399 | Attr.set(Ctxt->getCanonicalDIEOffset()); |
| 400 | else |
| 401 | Attr.set(RefDie->getOffset() + RefUnit->getStartOffset()); |
Frederic Riss | 6afcfce | 2015-03-13 18:35:57 +0000 | [diff] [blame] | 402 | } |
Frederic Riss | 9833de6 | 2015-03-06 23:22:53 +0000 | [diff] [blame] | 403 | } |
| 404 | |
Frederic Riss | 5a62dc3 | 2015-03-13 18:35:54 +0000 | [diff] [blame] | 405 | void CompileUnit::addFunctionRange(uint64_t FuncLowPc, uint64_t FuncHighPc, |
| 406 | int64_t PcOffset) { |
| 407 | Ranges.insert(FuncLowPc, FuncHighPc, PcOffset); |
| 408 | this->LowPc = std::min(LowPc, FuncLowPc + PcOffset); |
| 409 | this->HighPc = std::max(HighPc, FuncHighPc + PcOffset); |
Frederic Riss | 1af75f7 | 2015-03-12 18:45:10 +0000 | [diff] [blame] | 410 | } |
| 411 | |
Duncan P. N. Exon Smith | e7e1d0c | 2015-05-27 22:14:58 +0000 | [diff] [blame] | 412 | void CompileUnit::noteRangeAttribute(const DIE &Die, PatchLocation Attr) { |
Frederic Riss | 2544087 | 2015-03-13 23:30:31 +0000 | [diff] [blame] | 413 | if (Die.getTag() != dwarf::DW_TAG_compile_unit) |
| 414 | RangeAttributes.push_back(Attr); |
| 415 | else |
| 416 | UnitRangeAttribute = Attr; |
| 417 | } |
| 418 | |
Duncan P. N. Exon Smith | e7e1d0c | 2015-05-27 22:14:58 +0000 | [diff] [blame] | 419 | void CompileUnit::noteLocationAttribute(PatchLocation Attr, int64_t PcOffset) { |
Frederic Riss | dfb9790 | 2015-03-14 15:49:07 +0000 | [diff] [blame] | 420 | LocationAttributes.emplace_back(Attr, PcOffset); |
| 421 | } |
| 422 | |
Frederic Riss | bce93ff | 2015-03-16 02:05:10 +0000 | [diff] [blame] | 423 | /// \brief Add a name accelerator entry for \p Die with \p Name |
| 424 | /// which is stored in the string table at \p Offset. |
| 425 | void CompileUnit::addNameAccelerator(const DIE *Die, const char *Name, |
| 426 | uint32_t Offset, bool SkipPubSection) { |
| 427 | Pubnames.emplace_back(Name, Die, Offset, SkipPubSection); |
| 428 | } |
| 429 | |
| 430 | /// \brief Add a type accelerator entry for \p Die with \p Name |
| 431 | /// which is stored in the string table at \p Offset. |
| 432 | void CompileUnit::addTypeAccelerator(const DIE *Die, const char *Name, |
| 433 | uint32_t Offset) { |
| 434 | Pubtypes.emplace_back(Name, Die, Offset, false); |
| 435 | } |
| 436 | |
Frederic Riss | c99ea20 | 2015-02-28 00:29:11 +0000 | [diff] [blame] | 437 | /// \brief The Dwarf streaming logic |
| 438 | /// |
| 439 | /// All interactions with the MC layer that is used to build the debug |
| 440 | /// information binary representation are handled in this class. |
| 441 | class DwarfStreamer { |
| 442 | /// \defgroup MCObjects MC layer objects constructed by the streamer |
| 443 | /// @{ |
| 444 | std::unique_ptr<MCRegisterInfo> MRI; |
| 445 | std::unique_ptr<MCAsmInfo> MAI; |
| 446 | std::unique_ptr<MCObjectFileInfo> MOFI; |
| 447 | std::unique_ptr<MCContext> MC; |
| 448 | MCAsmBackend *MAB; // Owned by MCStreamer |
| 449 | std::unique_ptr<MCInstrInfo> MII; |
| 450 | std::unique_ptr<MCSubtargetInfo> MSTI; |
| 451 | MCCodeEmitter *MCE; // Owned by MCStreamer |
| 452 | MCStreamer *MS; // Owned by AsmPrinter |
| 453 | std::unique_ptr<TargetMachine> TM; |
| 454 | std::unique_ptr<AsmPrinter> Asm; |
| 455 | /// @} |
| 456 | |
| 457 | /// \brief the file we stream the linked Dwarf to. |
| 458 | std::unique_ptr<raw_fd_ostream> OutFile; |
| 459 | |
Frederic Riss | 2544087 | 2015-03-13 23:30:31 +0000 | [diff] [blame] | 460 | uint32_t RangesSectionSize; |
Frederic Riss | dfb9790 | 2015-03-14 15:49:07 +0000 | [diff] [blame] | 461 | uint32_t LocSectionSize; |
Frederic Riss | 63786b0 | 2015-03-15 20:45:43 +0000 | [diff] [blame] | 462 | uint32_t LineSectionSize; |
Frederic Riss | 5a64207 | 2015-06-05 23:06:11 +0000 | [diff] [blame] | 463 | uint32_t FrameSectionSize; |
Frederic Riss | 2544087 | 2015-03-13 23:30:31 +0000 | [diff] [blame] | 464 | |
Frederic Riss | bce93ff | 2015-03-16 02:05:10 +0000 | [diff] [blame] | 465 | /// \brief Emit the pubnames or pubtypes section contribution for \p |
| 466 | /// Unit into \p Sec. The data is provided in \p Names. |
Rafael Espindola | 0709a7b | 2015-05-21 19:20:38 +0000 | [diff] [blame] | 467 | void emitPubSectionForUnit(MCSection *Sec, StringRef Name, |
Frederic Riss | bce93ff | 2015-03-16 02:05:10 +0000 | [diff] [blame] | 468 | const CompileUnit &Unit, |
| 469 | const std::vector<CompileUnit::AccelInfo> &Names); |
| 470 | |
Frederic Riss | c99ea20 | 2015-02-28 00:29:11 +0000 | [diff] [blame] | 471 | public: |
| 472 | /// \brief Actually create the streamer and the ouptut file. |
| 473 | /// |
| 474 | /// This could be done directly in the constructor, but it feels |
| 475 | /// more natural to handle errors through return value. |
| 476 | bool init(Triple TheTriple, StringRef OutputFilename); |
| 477 | |
Frederic Riss | b8b43d5 | 2015-03-04 22:07:44 +0000 | [diff] [blame] | 478 | /// \brief Dump the file to the disk. |
Frederic Riss | 24faade | 2015-09-02 16:49:13 +0000 | [diff] [blame^] | 479 | bool finish(const DebugMap &); |
Frederic Riss | b8b43d5 | 2015-03-04 22:07:44 +0000 | [diff] [blame] | 480 | |
| 481 | AsmPrinter &getAsmPrinter() const { return *Asm; } |
| 482 | |
| 483 | /// \brief Set the current output section to debug_info and change |
| 484 | /// the MC Dwarf version to \p DwarfVersion. |
| 485 | void switchToDebugInfoSection(unsigned DwarfVersion); |
| 486 | |
| 487 | /// \brief Emit the compilation unit header for \p Unit in the |
| 488 | /// debug_info section. |
| 489 | /// |
| 490 | /// As a side effect, this also switches the current Dwarf version |
| 491 | /// of the MC layer to the one of U.getOrigUnit(). |
| 492 | void emitCompileUnitHeader(CompileUnit &Unit); |
| 493 | |
| 494 | /// \brief Recursively emit the DIE tree rooted at \p Die. |
| 495 | void emitDIE(DIE &Die); |
| 496 | |
| 497 | /// \brief Emit the abbreviation table \p Abbrevs to the |
| 498 | /// debug_abbrev section. |
| 499 | void emitAbbrevs(const std::vector<DIEAbbrev *> &Abbrevs); |
Frederic Riss | ef64846 | 2015-03-06 17:56:30 +0000 | [diff] [blame] | 500 | |
| 501 | /// \brief Emit the string table described by \p Pool. |
| 502 | void emitStrings(const NonRelocatableStringpool &Pool); |
Frederic Riss | 2544087 | 2015-03-13 23:30:31 +0000 | [diff] [blame] | 503 | |
| 504 | /// \brief Emit debug_ranges for \p FuncRange by translating the |
| 505 | /// original \p Entries. |
| 506 | void emitRangesEntries( |
| 507 | int64_t UnitPcOffset, uint64_t OrigLowPc, |
| 508 | FunctionIntervals::const_iterator FuncRange, |
| 509 | const std::vector<DWARFDebugRangeList::RangeListEntry> &Entries, |
| 510 | unsigned AddressSize); |
| 511 | |
Frederic Riss | 563b1b0 | 2015-03-14 03:46:51 +0000 | [diff] [blame] | 512 | /// \brief Emit debug_aranges entries for \p Unit and if \p |
| 513 | /// DoRangesSection is true, also emit the debug_ranges entries for |
| 514 | /// the DW_TAG_compile_unit's DW_AT_ranges attribute. |
| 515 | void emitUnitRangesEntries(CompileUnit &Unit, bool DoRangesSection); |
Frederic Riss | 2544087 | 2015-03-13 23:30:31 +0000 | [diff] [blame] | 516 | |
| 517 | uint32_t getRangesSectionSize() const { return RangesSectionSize; } |
Frederic Riss | dfb9790 | 2015-03-14 15:49:07 +0000 | [diff] [blame] | 518 | |
| 519 | /// \brief Emit the debug_loc contribution for \p Unit by copying |
| 520 | /// the entries from \p Dwarf and offseting them. Update the |
| 521 | /// location attributes to point to the new entries. |
| 522 | void emitLocationsForUnit(const CompileUnit &Unit, DWARFContext &Dwarf); |
Frederic Riss | 63786b0 | 2015-03-15 20:45:43 +0000 | [diff] [blame] | 523 | |
| 524 | /// \brief Emit the line table described in \p Rows into the |
| 525 | /// debug_line section. |
Frederic Riss | a5e1453 | 2015-08-07 15:14:13 +0000 | [diff] [blame] | 526 | void emitLineTableForUnit(MCDwarfLineTableParams Params, |
| 527 | StringRef PrologueBytes, unsigned MinInstLength, |
Frederic Riss | 63786b0 | 2015-03-15 20:45:43 +0000 | [diff] [blame] | 528 | std::vector<DWARFDebugLine::Row> &Rows, |
| 529 | unsigned AdddressSize); |
| 530 | |
| 531 | uint32_t getLineSectionSize() const { return LineSectionSize; } |
Frederic Riss | bce93ff | 2015-03-16 02:05:10 +0000 | [diff] [blame] | 532 | |
| 533 | /// \brief Emit the .debug_pubnames contribution for \p Unit. |
| 534 | void emitPubNamesForUnit(const CompileUnit &Unit); |
| 535 | |
| 536 | /// \brief Emit the .debug_pubtypes contribution for \p Unit. |
| 537 | void emitPubTypesForUnit(const CompileUnit &Unit); |
Frederic Riss | 5a64207 | 2015-06-05 23:06:11 +0000 | [diff] [blame] | 538 | |
| 539 | /// \brief Emit a CIE. |
| 540 | void emitCIE(StringRef CIEBytes); |
| 541 | |
| 542 | /// \brief Emit an FDE with data \p Bytes. |
| 543 | void emitFDE(uint32_t CIEOffset, uint32_t AddreSize, uint32_t Address, |
| 544 | StringRef Bytes); |
| 545 | |
| 546 | uint32_t getFrameSectionSize() const { return FrameSectionSize; } |
Frederic Riss | c99ea20 | 2015-02-28 00:29:11 +0000 | [diff] [blame] | 547 | }; |
| 548 | |
| 549 | bool DwarfStreamer::init(Triple TheTriple, StringRef OutputFilename) { |
| 550 | std::string ErrorStr; |
| 551 | std::string TripleName; |
| 552 | StringRef Context = "dwarf streamer init"; |
| 553 | |
| 554 | // Get the target. |
| 555 | const Target *TheTarget = |
| 556 | TargetRegistry::lookupTarget(TripleName, TheTriple, ErrorStr); |
| 557 | if (!TheTarget) |
| 558 | return error(ErrorStr, Context); |
| 559 | TripleName = TheTriple.getTriple(); |
| 560 | |
| 561 | // Create all the MC Objects. |
| 562 | MRI.reset(TheTarget->createMCRegInfo(TripleName)); |
| 563 | if (!MRI) |
| 564 | return error(Twine("no register info for target ") + TripleName, Context); |
| 565 | |
| 566 | MAI.reset(TheTarget->createMCAsmInfo(*MRI, TripleName)); |
| 567 | if (!MAI) |
| 568 | return error("no asm info for target " + TripleName, Context); |
| 569 | |
| 570 | MOFI.reset(new MCObjectFileInfo); |
| 571 | MC.reset(new MCContext(MAI.get(), MRI.get(), MOFI.get())); |
Daniel Sanders | 8d8b13d | 2015-06-16 12:18:07 +0000 | [diff] [blame] | 572 | MOFI->InitMCObjectFileInfo(TheTriple, Reloc::Default, CodeModel::Default, |
Frederic Riss | c99ea20 | 2015-02-28 00:29:11 +0000 | [diff] [blame] | 573 | *MC); |
| 574 | |
| 575 | MAB = TheTarget->createMCAsmBackend(*MRI, TripleName, ""); |
| 576 | if (!MAB) |
| 577 | return error("no asm backend for target " + TripleName, Context); |
| 578 | |
| 579 | MII.reset(TheTarget->createMCInstrInfo()); |
| 580 | if (!MII) |
| 581 | return error("no instr info info for target " + TripleName, Context); |
| 582 | |
| 583 | MSTI.reset(TheTarget->createMCSubtargetInfo(TripleName, "", "")); |
| 584 | if (!MSTI) |
| 585 | return error("no subtarget info for target " + TripleName, Context); |
| 586 | |
Eric Christopher | 0169e42 | 2015-03-10 22:03:14 +0000 | [diff] [blame] | 587 | MCE = TheTarget->createMCCodeEmitter(*MII, *MRI, *MC); |
Frederic Riss | c99ea20 | 2015-02-28 00:29:11 +0000 | [diff] [blame] | 588 | if (!MCE) |
| 589 | return error("no code emitter for target " + TripleName, Context); |
| 590 | |
| 591 | // Create the output file. |
| 592 | std::error_code EC; |
Frederic Riss | b8b43d5 | 2015-03-04 22:07:44 +0000 | [diff] [blame] | 593 | OutFile = |
| 594 | llvm::make_unique<raw_fd_ostream>(OutputFilename, EC, sys::fs::F_None); |
Frederic Riss | c99ea20 | 2015-02-28 00:29:11 +0000 | [diff] [blame] | 595 | if (EC) |
| 596 | return error(Twine(OutputFilename) + ": " + EC.message(), Context); |
| 597 | |
Rafael Espindola | f696df1 | 2015-03-16 22:29:29 +0000 | [diff] [blame] | 598 | MS = TheTarget->createMCObjectStreamer(TheTriple, *MC, *MAB, *OutFile, MCE, |
Rafael Espindola | 36a15cb | 2015-03-20 20:00:01 +0000 | [diff] [blame] | 599 | *MSTI, false, |
| 600 | /*DWARFMustBeAtTheEnd*/ false); |
Frederic Riss | c99ea20 | 2015-02-28 00:29:11 +0000 | [diff] [blame] | 601 | if (!MS) |
| 602 | return error("no object streamer for target " + TripleName, Context); |
| 603 | |
| 604 | // Finally create the AsmPrinter we'll use to emit the DIEs. |
| 605 | TM.reset(TheTarget->createTargetMachine(TripleName, "", "", TargetOptions())); |
| 606 | if (!TM) |
| 607 | return error("no target machine for target " + TripleName, Context); |
| 608 | |
| 609 | Asm.reset(TheTarget->createAsmPrinter(*TM, std::unique_ptr<MCStreamer>(MS))); |
| 610 | if (!Asm) |
| 611 | return error("no asm printer for target " + TripleName, Context); |
| 612 | |
Frederic Riss | 2544087 | 2015-03-13 23:30:31 +0000 | [diff] [blame] | 613 | RangesSectionSize = 0; |
Frederic Riss | dfb9790 | 2015-03-14 15:49:07 +0000 | [diff] [blame] | 614 | LocSectionSize = 0; |
Frederic Riss | 63786b0 | 2015-03-15 20:45:43 +0000 | [diff] [blame] | 615 | LineSectionSize = 0; |
Frederic Riss | 5a64207 | 2015-06-05 23:06:11 +0000 | [diff] [blame] | 616 | FrameSectionSize = 0; |
Frederic Riss | 2544087 | 2015-03-13 23:30:31 +0000 | [diff] [blame] | 617 | |
Frederic Riss | c99ea20 | 2015-02-28 00:29:11 +0000 | [diff] [blame] | 618 | return true; |
| 619 | } |
| 620 | |
Frederic Riss | 24faade | 2015-09-02 16:49:13 +0000 | [diff] [blame^] | 621 | bool DwarfStreamer::finish(const DebugMap &DM) { |
| 622 | if (DM.getTriple().isOSDarwin() && !DM.getBinaryPath().empty()) |
| 623 | return MachOUtils::generateDsymCompanion(DM, *MS, *OutFile); |
| 624 | |
Frederic Riss | c99ea20 | 2015-02-28 00:29:11 +0000 | [diff] [blame] | 625 | MS->Finish(); |
| 626 | return true; |
| 627 | } |
| 628 | |
Frederic Riss | b8b43d5 | 2015-03-04 22:07:44 +0000 | [diff] [blame] | 629 | /// \brief Set the current output section to debug_info and change |
| 630 | /// the MC Dwarf version to \p DwarfVersion. |
| 631 | void DwarfStreamer::switchToDebugInfoSection(unsigned DwarfVersion) { |
| 632 | MS->SwitchSection(MOFI->getDwarfInfoSection()); |
| 633 | MC->setDwarfVersion(DwarfVersion); |
| 634 | } |
| 635 | |
| 636 | /// \brief Emit the compilation unit header for \p Unit in the |
| 637 | /// debug_info section. |
| 638 | /// |
| 639 | /// A Dwarf scetion header is encoded as: |
| 640 | /// uint32_t Unit length (omiting this field) |
| 641 | /// uint16_t Version |
| 642 | /// uint32_t Abbreviation table offset |
| 643 | /// uint8_t Address size |
| 644 | /// |
| 645 | /// Leading to a total of 11 bytes. |
| 646 | void DwarfStreamer::emitCompileUnitHeader(CompileUnit &Unit) { |
| 647 | unsigned Version = Unit.getOrigUnit().getVersion(); |
| 648 | switchToDebugInfoSection(Version); |
| 649 | |
| 650 | // Emit size of content not including length itself. The size has |
| 651 | // already been computed in CompileUnit::computeOffsets(). Substract |
| 652 | // 4 to that size to account for the length field. |
| 653 | Asm->EmitInt32(Unit.getNextUnitOffset() - Unit.getStartOffset() - 4); |
| 654 | Asm->EmitInt16(Version); |
| 655 | // We share one abbreviations table across all units so it's always at the |
| 656 | // start of the section. |
| 657 | Asm->EmitInt32(0); |
| 658 | Asm->EmitInt8(Unit.getOrigUnit().getAddressByteSize()); |
| 659 | } |
| 660 | |
| 661 | /// \brief Emit the \p Abbrevs array as the shared abbreviation table |
| 662 | /// for the linked Dwarf file. |
| 663 | void DwarfStreamer::emitAbbrevs(const std::vector<DIEAbbrev *> &Abbrevs) { |
| 664 | MS->SwitchSection(MOFI->getDwarfAbbrevSection()); |
| 665 | Asm->emitDwarfAbbrevs(Abbrevs); |
| 666 | } |
| 667 | |
| 668 | /// \brief Recursively emit the DIE tree rooted at \p Die. |
| 669 | void DwarfStreamer::emitDIE(DIE &Die) { |
| 670 | MS->SwitchSection(MOFI->getDwarfInfoSection()); |
| 671 | Asm->emitDwarfDIE(Die); |
| 672 | } |
| 673 | |
Frederic Riss | ef64846 | 2015-03-06 17:56:30 +0000 | [diff] [blame] | 674 | /// \brief Emit the debug_str section stored in \p Pool. |
| 675 | void DwarfStreamer::emitStrings(const NonRelocatableStringpool &Pool) { |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 676 | Asm->OutStreamer->SwitchSection(MOFI->getDwarfStrSection()); |
Frederic Riss | ef64846 | 2015-03-06 17:56:30 +0000 | [diff] [blame] | 677 | for (auto *Entry = Pool.getFirstEntry(); Entry; |
| 678 | Entry = Pool.getNextEntry(Entry)) |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 679 | Asm->OutStreamer->EmitBytes( |
Frederic Riss | ef64846 | 2015-03-06 17:56:30 +0000 | [diff] [blame] | 680 | StringRef(Entry->getKey().data(), Entry->getKey().size() + 1)); |
| 681 | } |
| 682 | |
Frederic Riss | 2544087 | 2015-03-13 23:30:31 +0000 | [diff] [blame] | 683 | /// \brief Emit the debug_range section contents for \p FuncRange by |
| 684 | /// translating the original \p Entries. The debug_range section |
| 685 | /// format is totally trivial, consisting just of pairs of address |
| 686 | /// sized addresses describing the ranges. |
| 687 | void DwarfStreamer::emitRangesEntries( |
| 688 | int64_t UnitPcOffset, uint64_t OrigLowPc, |
| 689 | FunctionIntervals::const_iterator FuncRange, |
| 690 | const std::vector<DWARFDebugRangeList::RangeListEntry> &Entries, |
| 691 | unsigned AddressSize) { |
| 692 | MS->SwitchSection(MC->getObjectFileInfo()->getDwarfRangesSection()); |
| 693 | |
| 694 | // Offset each range by the right amount. |
Frederic Riss | 9454620 | 2015-08-31 05:09:32 +0000 | [diff] [blame] | 695 | int64_t PcOffset = Entries.empty() ? 0 : FuncRange.value() + UnitPcOffset; |
Frederic Riss | 2544087 | 2015-03-13 23:30:31 +0000 | [diff] [blame] | 696 | for (const auto &Range : Entries) { |
| 697 | if (Range.isBaseAddressSelectionEntry(AddressSize)) { |
| 698 | warn("unsupported base address selection operation", |
| 699 | "emitting debug_ranges"); |
| 700 | break; |
| 701 | } |
| 702 | // Do not emit empty ranges. |
| 703 | if (Range.StartAddress == Range.EndAddress) |
| 704 | continue; |
| 705 | |
| 706 | // All range entries should lie in the function range. |
| 707 | if (!(Range.StartAddress + OrigLowPc >= FuncRange.start() && |
| 708 | Range.EndAddress + OrigLowPc <= FuncRange.stop())) |
| 709 | warn("inconsistent range data.", "emitting debug_ranges"); |
| 710 | MS->EmitIntValue(Range.StartAddress + PcOffset, AddressSize); |
| 711 | MS->EmitIntValue(Range.EndAddress + PcOffset, AddressSize); |
| 712 | RangesSectionSize += 2 * AddressSize; |
| 713 | } |
| 714 | |
| 715 | // Add the terminator entry. |
| 716 | MS->EmitIntValue(0, AddressSize); |
| 717 | MS->EmitIntValue(0, AddressSize); |
| 718 | RangesSectionSize += 2 * AddressSize; |
| 719 | } |
| 720 | |
Frederic Riss | 563b1b0 | 2015-03-14 03:46:51 +0000 | [diff] [blame] | 721 | /// \brief Emit the debug_aranges contribution of a unit and |
| 722 | /// if \p DoDebugRanges is true the debug_range contents for a |
| 723 | /// compile_unit level DW_AT_ranges attribute (Which are basically the |
| 724 | /// same thing with a different base address). |
| 725 | /// Just aggregate all the ranges gathered inside that unit. |
| 726 | void DwarfStreamer::emitUnitRangesEntries(CompileUnit &Unit, |
| 727 | bool DoDebugRanges) { |
Frederic Riss | 2544087 | 2015-03-13 23:30:31 +0000 | [diff] [blame] | 728 | unsigned AddressSize = Unit.getOrigUnit().getAddressByteSize(); |
| 729 | // Gather the ranges in a vector, so that we can simplify them. The |
| 730 | // IntervalMap will have coalesced the non-linked ranges, but here |
| 731 | // we want to coalesce the linked addresses. |
| 732 | std::vector<std::pair<uint64_t, uint64_t>> Ranges; |
| 733 | const auto &FunctionRanges = Unit.getFunctionRanges(); |
| 734 | for (auto Range = FunctionRanges.begin(), End = FunctionRanges.end(); |
| 735 | Range != End; ++Range) |
Frederic Riss | 563b1b0 | 2015-03-14 03:46:51 +0000 | [diff] [blame] | 736 | Ranges.push_back(std::make_pair(Range.start() + Range.value(), |
| 737 | Range.stop() + Range.value())); |
Frederic Riss | 2544087 | 2015-03-13 23:30:31 +0000 | [diff] [blame] | 738 | |
| 739 | // The object addresses where sorted, but again, the linked |
| 740 | // addresses might end up in a different order. |
| 741 | std::sort(Ranges.begin(), Ranges.end()); |
| 742 | |
Frederic Riss | 563b1b0 | 2015-03-14 03:46:51 +0000 | [diff] [blame] | 743 | if (!Ranges.empty()) { |
| 744 | MS->SwitchSection(MC->getObjectFileInfo()->getDwarfARangesSection()); |
| 745 | |
Rafael Espindola | 9ab0923 | 2015-03-17 20:07:06 +0000 | [diff] [blame] | 746 | MCSymbol *BeginLabel = Asm->createTempSymbol("Barange"); |
| 747 | MCSymbol *EndLabel = Asm->createTempSymbol("Earange"); |
Frederic Riss | 563b1b0 | 2015-03-14 03:46:51 +0000 | [diff] [blame] | 748 | |
| 749 | unsigned HeaderSize = |
| 750 | sizeof(int32_t) + // Size of contents (w/o this field |
| 751 | sizeof(int16_t) + // DWARF ARange version number |
| 752 | sizeof(int32_t) + // Offset of CU in the .debug_info section |
| 753 | sizeof(int8_t) + // Pointer Size (in bytes) |
| 754 | sizeof(int8_t); // Segment Size (in bytes) |
| 755 | |
| 756 | unsigned TupleSize = AddressSize * 2; |
| 757 | unsigned Padding = OffsetToAlignment(HeaderSize, TupleSize); |
| 758 | |
| 759 | Asm->EmitLabelDifference(EndLabel, BeginLabel, 4); // Arange length |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 760 | Asm->OutStreamer->EmitLabel(BeginLabel); |
Frederic Riss | 563b1b0 | 2015-03-14 03:46:51 +0000 | [diff] [blame] | 761 | Asm->EmitInt16(dwarf::DW_ARANGES_VERSION); // Version number |
| 762 | Asm->EmitInt32(Unit.getStartOffset()); // Corresponding unit's offset |
| 763 | Asm->EmitInt8(AddressSize); // Address size |
| 764 | Asm->EmitInt8(0); // Segment size |
| 765 | |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 766 | Asm->OutStreamer->EmitFill(Padding, 0x0); |
Frederic Riss | 563b1b0 | 2015-03-14 03:46:51 +0000 | [diff] [blame] | 767 | |
| 768 | for (auto Range = Ranges.begin(), End = Ranges.end(); Range != End; |
| 769 | ++Range) { |
| 770 | uint64_t RangeStart = Range->first; |
| 771 | MS->EmitIntValue(RangeStart, AddressSize); |
| 772 | while ((Range + 1) != End && Range->second == (Range + 1)->first) |
| 773 | ++Range; |
| 774 | MS->EmitIntValue(Range->second - RangeStart, AddressSize); |
| 775 | } |
| 776 | |
| 777 | // Emit terminator |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 778 | Asm->OutStreamer->EmitIntValue(0, AddressSize); |
| 779 | Asm->OutStreamer->EmitIntValue(0, AddressSize); |
| 780 | Asm->OutStreamer->EmitLabel(EndLabel); |
Frederic Riss | 563b1b0 | 2015-03-14 03:46:51 +0000 | [diff] [blame] | 781 | } |
| 782 | |
| 783 | if (!DoDebugRanges) |
| 784 | return; |
| 785 | |
| 786 | MS->SwitchSection(MC->getObjectFileInfo()->getDwarfRangesSection()); |
| 787 | // Offset each range by the right amount. |
| 788 | int64_t PcOffset = -Unit.getLowPc(); |
Frederic Riss | 2544087 | 2015-03-13 23:30:31 +0000 | [diff] [blame] | 789 | // Emit coalesced ranges. |
| 790 | for (auto Range = Ranges.begin(), End = Ranges.end(); Range != End; ++Range) { |
Frederic Riss | 563b1b0 | 2015-03-14 03:46:51 +0000 | [diff] [blame] | 791 | MS->EmitIntValue(Range->first + PcOffset, AddressSize); |
Frederic Riss | 2544087 | 2015-03-13 23:30:31 +0000 | [diff] [blame] | 792 | while (Range + 1 != End && Range->second == (Range + 1)->first) |
| 793 | ++Range; |
Frederic Riss | 563b1b0 | 2015-03-14 03:46:51 +0000 | [diff] [blame] | 794 | MS->EmitIntValue(Range->second + PcOffset, AddressSize); |
Frederic Riss | 2544087 | 2015-03-13 23:30:31 +0000 | [diff] [blame] | 795 | RangesSectionSize += 2 * AddressSize; |
| 796 | } |
| 797 | |
| 798 | // Add the terminator entry. |
| 799 | MS->EmitIntValue(0, AddressSize); |
| 800 | MS->EmitIntValue(0, AddressSize); |
| 801 | RangesSectionSize += 2 * AddressSize; |
| 802 | } |
| 803 | |
Frederic Riss | dfb9790 | 2015-03-14 15:49:07 +0000 | [diff] [blame] | 804 | /// \brief Emit location lists for \p Unit and update attribtues to |
| 805 | /// point to the new entries. |
| 806 | void DwarfStreamer::emitLocationsForUnit(const CompileUnit &Unit, |
| 807 | DWARFContext &Dwarf) { |
Duncan P. N. Exon Smith | e7e1d0c | 2015-05-27 22:14:58 +0000 | [diff] [blame] | 808 | const auto &Attributes = Unit.getLocationAttributes(); |
Frederic Riss | dfb9790 | 2015-03-14 15:49:07 +0000 | [diff] [blame] | 809 | |
| 810 | if (Attributes.empty()) |
| 811 | return; |
| 812 | |
| 813 | MS->SwitchSection(MC->getObjectFileInfo()->getDwarfLocSection()); |
| 814 | |
| 815 | unsigned AddressSize = Unit.getOrigUnit().getAddressByteSize(); |
| 816 | const DWARFSection &InputSec = Dwarf.getLocSection(); |
| 817 | DataExtractor Data(InputSec.Data, Dwarf.isLittleEndian(), AddressSize); |
| 818 | DWARFUnit &OrigUnit = Unit.getOrigUnit(); |
Alexey Samsonov | 7a18c06 | 2015-05-19 21:54:32 +0000 | [diff] [blame] | 819 | const auto *OrigUnitDie = OrigUnit.getUnitDIE(false); |
Frederic Riss | dfb9790 | 2015-03-14 15:49:07 +0000 | [diff] [blame] | 820 | int64_t UnitPcOffset = 0; |
| 821 | uint64_t OrigLowPc = OrigUnitDie->getAttributeValueAsAddress( |
| 822 | &OrigUnit, dwarf::DW_AT_low_pc, -1ULL); |
| 823 | if (OrigLowPc != -1ULL) |
| 824 | UnitPcOffset = int64_t(OrigLowPc) - Unit.getLowPc(); |
| 825 | |
| 826 | for (const auto &Attr : Attributes) { |
Duncan P. N. Exon Smith | e7e1d0c | 2015-05-27 22:14:58 +0000 | [diff] [blame] | 827 | uint32_t Offset = Attr.first.get(); |
| 828 | Attr.first.set(LocSectionSize); |
Frederic Riss | dfb9790 | 2015-03-14 15:49:07 +0000 | [diff] [blame] | 829 | // This is the quantity to add to the old location address to get |
| 830 | // the correct address for the new one. |
| 831 | int64_t LocPcOffset = Attr.second + UnitPcOffset; |
| 832 | while (Data.isValidOffset(Offset)) { |
| 833 | uint64_t Low = Data.getUnsigned(&Offset, AddressSize); |
| 834 | uint64_t High = Data.getUnsigned(&Offset, AddressSize); |
| 835 | LocSectionSize += 2 * AddressSize; |
| 836 | if (Low == 0 && High == 0) { |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 837 | Asm->OutStreamer->EmitIntValue(0, AddressSize); |
| 838 | Asm->OutStreamer->EmitIntValue(0, AddressSize); |
Frederic Riss | dfb9790 | 2015-03-14 15:49:07 +0000 | [diff] [blame] | 839 | break; |
| 840 | } |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 841 | Asm->OutStreamer->EmitIntValue(Low + LocPcOffset, AddressSize); |
| 842 | Asm->OutStreamer->EmitIntValue(High + LocPcOffset, AddressSize); |
Frederic Riss | dfb9790 | 2015-03-14 15:49:07 +0000 | [diff] [blame] | 843 | uint64_t Length = Data.getU16(&Offset); |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 844 | Asm->OutStreamer->EmitIntValue(Length, 2); |
Frederic Riss | dfb9790 | 2015-03-14 15:49:07 +0000 | [diff] [blame] | 845 | // Just copy the bytes over. |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 846 | Asm->OutStreamer->EmitBytes( |
Frederic Riss | dfb9790 | 2015-03-14 15:49:07 +0000 | [diff] [blame] | 847 | StringRef(InputSec.Data.substr(Offset, Length))); |
| 848 | Offset += Length; |
| 849 | LocSectionSize += Length + 2; |
| 850 | } |
| 851 | } |
| 852 | } |
| 853 | |
Frederic Riss | a5e1453 | 2015-08-07 15:14:13 +0000 | [diff] [blame] | 854 | void DwarfStreamer::emitLineTableForUnit(MCDwarfLineTableParams Params, |
| 855 | StringRef PrologueBytes, |
Frederic Riss | 63786b0 | 2015-03-15 20:45:43 +0000 | [diff] [blame] | 856 | unsigned MinInstLength, |
| 857 | std::vector<DWARFDebugLine::Row> &Rows, |
| 858 | unsigned PointerSize) { |
| 859 | // Switch to the section where the table will be emitted into. |
| 860 | MS->SwitchSection(MC->getObjectFileInfo()->getDwarfLineSection()); |
Jim Grosbach | 6f48200 | 2015-05-18 18:43:14 +0000 | [diff] [blame] | 861 | MCSymbol *LineStartSym = MC->createTempSymbol(); |
| 862 | MCSymbol *LineEndSym = MC->createTempSymbol(); |
Frederic Riss | 63786b0 | 2015-03-15 20:45:43 +0000 | [diff] [blame] | 863 | |
| 864 | // The first 4 bytes is the total length of the information for this |
| 865 | // compilation unit (not including these 4 bytes for the length). |
| 866 | Asm->EmitLabelDifference(LineEndSym, LineStartSym, 4); |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 867 | Asm->OutStreamer->EmitLabel(LineStartSym); |
Frederic Riss | 63786b0 | 2015-03-15 20:45:43 +0000 | [diff] [blame] | 868 | // Copy Prologue. |
| 869 | MS->EmitBytes(PrologueBytes); |
| 870 | LineSectionSize += PrologueBytes.size() + 4; |
| 871 | |
Frederic Riss | c3820d0 | 2015-03-15 22:20:28 +0000 | [diff] [blame] | 872 | SmallString<128> EncodingBuffer; |
Frederic Riss | 63786b0 | 2015-03-15 20:45:43 +0000 | [diff] [blame] | 873 | raw_svector_ostream EncodingOS(EncodingBuffer); |
| 874 | |
| 875 | if (Rows.empty()) { |
| 876 | // We only have the dummy entry, dsymutil emits an entry with a 0 |
| 877 | // address in that case. |
Frederic Riss | a5e1453 | 2015-08-07 15:14:13 +0000 | [diff] [blame] | 878 | MCDwarfLineAddr::Encode(*MC, Params, INT64_MAX, 0, EncodingOS); |
Frederic Riss | 63786b0 | 2015-03-15 20:45:43 +0000 | [diff] [blame] | 879 | MS->EmitBytes(EncodingOS.str()); |
| 880 | LineSectionSize += EncodingBuffer.size(); |
Frederic Riss | 63786b0 | 2015-03-15 20:45:43 +0000 | [diff] [blame] | 881 | MS->EmitLabel(LineEndSym); |
| 882 | return; |
| 883 | } |
| 884 | |
| 885 | // Line table state machine fields |
| 886 | unsigned FileNum = 1; |
| 887 | unsigned LastLine = 1; |
| 888 | unsigned Column = 0; |
| 889 | unsigned IsStatement = 1; |
| 890 | unsigned Isa = 0; |
| 891 | uint64_t Address = -1ULL; |
| 892 | |
| 893 | unsigned RowsSinceLastSequence = 0; |
| 894 | |
| 895 | for (unsigned Idx = 0; Idx < Rows.size(); ++Idx) { |
| 896 | auto &Row = Rows[Idx]; |
| 897 | |
| 898 | int64_t AddressDelta; |
| 899 | if (Address == -1ULL) { |
| 900 | MS->EmitIntValue(dwarf::DW_LNS_extended_op, 1); |
| 901 | MS->EmitULEB128IntValue(PointerSize + 1); |
| 902 | MS->EmitIntValue(dwarf::DW_LNE_set_address, 1); |
| 903 | MS->EmitIntValue(Row.Address, PointerSize); |
| 904 | LineSectionSize += 2 + PointerSize + getULEB128Size(PointerSize + 1); |
| 905 | AddressDelta = 0; |
| 906 | } else { |
| 907 | AddressDelta = (Row.Address - Address) / MinInstLength; |
| 908 | } |
| 909 | |
| 910 | // FIXME: code copied and transfromed from |
| 911 | // MCDwarf.cpp::EmitDwarfLineTable. We should find a way to share |
| 912 | // this code, but the current compatibility requirement with |
| 913 | // classic dsymutil makes it hard. Revisit that once this |
| 914 | // requirement is dropped. |
| 915 | |
| 916 | if (FileNum != Row.File) { |
| 917 | FileNum = Row.File; |
| 918 | MS->EmitIntValue(dwarf::DW_LNS_set_file, 1); |
| 919 | MS->EmitULEB128IntValue(FileNum); |
| 920 | LineSectionSize += 1 + getULEB128Size(FileNum); |
| 921 | } |
| 922 | if (Column != Row.Column) { |
| 923 | Column = Row.Column; |
| 924 | MS->EmitIntValue(dwarf::DW_LNS_set_column, 1); |
| 925 | MS->EmitULEB128IntValue(Column); |
| 926 | LineSectionSize += 1 + getULEB128Size(Column); |
| 927 | } |
| 928 | |
| 929 | // FIXME: We should handle the discriminator here, but dsymutil |
| 930 | // doesn' consider it, thus ignore it for now. |
| 931 | |
| 932 | if (Isa != Row.Isa) { |
| 933 | Isa = Row.Isa; |
| 934 | MS->EmitIntValue(dwarf::DW_LNS_set_isa, 1); |
| 935 | MS->EmitULEB128IntValue(Isa); |
| 936 | LineSectionSize += 1 + getULEB128Size(Isa); |
| 937 | } |
| 938 | if (IsStatement != Row.IsStmt) { |
| 939 | IsStatement = Row.IsStmt; |
| 940 | MS->EmitIntValue(dwarf::DW_LNS_negate_stmt, 1); |
| 941 | LineSectionSize += 1; |
| 942 | } |
| 943 | if (Row.BasicBlock) { |
| 944 | MS->EmitIntValue(dwarf::DW_LNS_set_basic_block, 1); |
| 945 | LineSectionSize += 1; |
| 946 | } |
| 947 | |
| 948 | if (Row.PrologueEnd) { |
| 949 | MS->EmitIntValue(dwarf::DW_LNS_set_prologue_end, 1); |
| 950 | LineSectionSize += 1; |
| 951 | } |
| 952 | |
| 953 | if (Row.EpilogueBegin) { |
| 954 | MS->EmitIntValue(dwarf::DW_LNS_set_epilogue_begin, 1); |
| 955 | LineSectionSize += 1; |
| 956 | } |
| 957 | |
| 958 | int64_t LineDelta = int64_t(Row.Line) - LastLine; |
| 959 | if (!Row.EndSequence) { |
Frederic Riss | a5e1453 | 2015-08-07 15:14:13 +0000 | [diff] [blame] | 960 | MCDwarfLineAddr::Encode(*MC, Params, LineDelta, AddressDelta, EncodingOS); |
Frederic Riss | 63786b0 | 2015-03-15 20:45:43 +0000 | [diff] [blame] | 961 | MS->EmitBytes(EncodingOS.str()); |
| 962 | LineSectionSize += EncodingBuffer.size(); |
| 963 | EncodingBuffer.resize(0); |
| 964 | Address = Row.Address; |
| 965 | LastLine = Row.Line; |
| 966 | RowsSinceLastSequence++; |
| 967 | } else { |
| 968 | if (LineDelta) { |
| 969 | MS->EmitIntValue(dwarf::DW_LNS_advance_line, 1); |
| 970 | MS->EmitSLEB128IntValue(LineDelta); |
| 971 | LineSectionSize += 1 + getSLEB128Size(LineDelta); |
| 972 | } |
| 973 | if (AddressDelta) { |
| 974 | MS->EmitIntValue(dwarf::DW_LNS_advance_pc, 1); |
| 975 | MS->EmitULEB128IntValue(AddressDelta); |
| 976 | LineSectionSize += 1 + getULEB128Size(AddressDelta); |
| 977 | } |
Frederic Riss | a5e1453 | 2015-08-07 15:14:13 +0000 | [diff] [blame] | 978 | MCDwarfLineAddr::Encode(*MC, Params, INT64_MAX, 0, EncodingOS); |
Frederic Riss | 63786b0 | 2015-03-15 20:45:43 +0000 | [diff] [blame] | 979 | MS->EmitBytes(EncodingOS.str()); |
| 980 | LineSectionSize += EncodingBuffer.size(); |
| 981 | EncodingBuffer.resize(0); |
Frederic Riss | 63786b0 | 2015-03-15 20:45:43 +0000 | [diff] [blame] | 982 | Address = -1ULL; |
| 983 | LastLine = FileNum = IsStatement = 1; |
| 984 | RowsSinceLastSequence = Column = Isa = 0; |
| 985 | } |
| 986 | } |
| 987 | |
| 988 | if (RowsSinceLastSequence) { |
Frederic Riss | a5e1453 | 2015-08-07 15:14:13 +0000 | [diff] [blame] | 989 | MCDwarfLineAddr::Encode(*MC, Params, INT64_MAX, 0, EncodingOS); |
Frederic Riss | 63786b0 | 2015-03-15 20:45:43 +0000 | [diff] [blame] | 990 | MS->EmitBytes(EncodingOS.str()); |
| 991 | LineSectionSize += EncodingBuffer.size(); |
| 992 | EncodingBuffer.resize(0); |
| 993 | } |
| 994 | |
| 995 | MS->EmitLabel(LineEndSym); |
| 996 | } |
| 997 | |
Frederic Riss | bce93ff | 2015-03-16 02:05:10 +0000 | [diff] [blame] | 998 | /// \brief Emit the pubnames or pubtypes section contribution for \p |
| 999 | /// Unit into \p Sec. The data is provided in \p Names. |
| 1000 | void DwarfStreamer::emitPubSectionForUnit( |
Rafael Espindola | 0709a7b | 2015-05-21 19:20:38 +0000 | [diff] [blame] | 1001 | MCSection *Sec, StringRef SecName, const CompileUnit &Unit, |
Frederic Riss | bce93ff | 2015-03-16 02:05:10 +0000 | [diff] [blame] | 1002 | const std::vector<CompileUnit::AccelInfo> &Names) { |
| 1003 | if (Names.empty()) |
| 1004 | return; |
| 1005 | |
| 1006 | // Start the dwarf pubnames section. |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 1007 | Asm->OutStreamer->SwitchSection(Sec); |
Rafael Espindola | 9ab0923 | 2015-03-17 20:07:06 +0000 | [diff] [blame] | 1008 | MCSymbol *BeginLabel = Asm->createTempSymbol("pub" + SecName + "_begin"); |
| 1009 | MCSymbol *EndLabel = Asm->createTempSymbol("pub" + SecName + "_end"); |
Frederic Riss | bce93ff | 2015-03-16 02:05:10 +0000 | [diff] [blame] | 1010 | |
| 1011 | bool HeaderEmitted = false; |
| 1012 | // Emit the pubnames for this compilation unit. |
| 1013 | for (const auto &Name : Names) { |
| 1014 | if (Name.SkipPubSection) |
| 1015 | continue; |
| 1016 | |
| 1017 | if (!HeaderEmitted) { |
| 1018 | // Emit the header. |
| 1019 | Asm->EmitLabelDifference(EndLabel, BeginLabel, 4); // Length |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 1020 | Asm->OutStreamer->EmitLabel(BeginLabel); |
Frederic Riss | bce93ff | 2015-03-16 02:05:10 +0000 | [diff] [blame] | 1021 | Asm->EmitInt16(dwarf::DW_PUBNAMES_VERSION); // Version |
Frederic Riss | f37964c | 2015-06-05 20:27:07 +0000 | [diff] [blame] | 1022 | Asm->EmitInt32(Unit.getStartOffset()); // Unit offset |
Frederic Riss | bce93ff | 2015-03-16 02:05:10 +0000 | [diff] [blame] | 1023 | Asm->EmitInt32(Unit.getNextUnitOffset() - Unit.getStartOffset()); // Size |
| 1024 | HeaderEmitted = true; |
| 1025 | } |
| 1026 | Asm->EmitInt32(Name.Die->getOffset()); |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 1027 | Asm->OutStreamer->EmitBytes( |
Frederic Riss | bce93ff | 2015-03-16 02:05:10 +0000 | [diff] [blame] | 1028 | StringRef(Name.Name.data(), Name.Name.size() + 1)); |
| 1029 | } |
| 1030 | |
| 1031 | if (!HeaderEmitted) |
| 1032 | return; |
| 1033 | Asm->EmitInt32(0); // End marker. |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 1034 | Asm->OutStreamer->EmitLabel(EndLabel); |
Frederic Riss | bce93ff | 2015-03-16 02:05:10 +0000 | [diff] [blame] | 1035 | } |
| 1036 | |
| 1037 | /// \brief Emit .debug_pubnames for \p Unit. |
| 1038 | void DwarfStreamer::emitPubNamesForUnit(const CompileUnit &Unit) { |
| 1039 | emitPubSectionForUnit(MC->getObjectFileInfo()->getDwarfPubNamesSection(), |
| 1040 | "names", Unit, Unit.getPubnames()); |
| 1041 | } |
| 1042 | |
| 1043 | /// \brief Emit .debug_pubtypes for \p Unit. |
| 1044 | void DwarfStreamer::emitPubTypesForUnit(const CompileUnit &Unit) { |
| 1045 | emitPubSectionForUnit(MC->getObjectFileInfo()->getDwarfPubTypesSection(), |
| 1046 | "types", Unit, Unit.getPubtypes()); |
| 1047 | } |
| 1048 | |
Frederic Riss | 5a64207 | 2015-06-05 23:06:11 +0000 | [diff] [blame] | 1049 | /// \brief Emit a CIE into the debug_frame section. |
| 1050 | void DwarfStreamer::emitCIE(StringRef CIEBytes) { |
| 1051 | MS->SwitchSection(MC->getObjectFileInfo()->getDwarfFrameSection()); |
| 1052 | |
| 1053 | MS->EmitBytes(CIEBytes); |
| 1054 | FrameSectionSize += CIEBytes.size(); |
| 1055 | } |
| 1056 | |
| 1057 | /// \brief Emit a FDE into the debug_frame section. \p FDEBytes |
| 1058 | /// contains the FDE data without the length, CIE offset and address |
| 1059 | /// which will be replaced with the paramter values. |
| 1060 | void DwarfStreamer::emitFDE(uint32_t CIEOffset, uint32_t AddrSize, |
| 1061 | uint32_t Address, StringRef FDEBytes) { |
| 1062 | MS->SwitchSection(MC->getObjectFileInfo()->getDwarfFrameSection()); |
| 1063 | |
| 1064 | MS->EmitIntValue(FDEBytes.size() + 4 + AddrSize, 4); |
| 1065 | MS->EmitIntValue(CIEOffset, 4); |
| 1066 | MS->EmitIntValue(Address, AddrSize); |
| 1067 | MS->EmitBytes(FDEBytes); |
| 1068 | FrameSectionSize += FDEBytes.size() + 8 + AddrSize; |
| 1069 | } |
| 1070 | |
Frederic Riss | d345518 | 2015-01-28 18:27:01 +0000 | [diff] [blame] | 1071 | /// \brief The core of the Dwarf linking logic. |
Frederic Riss | 1036e64 | 2015-02-13 23:18:22 +0000 | [diff] [blame] | 1072 | /// |
| 1073 | /// The link of the dwarf information from the object files will be |
| 1074 | /// driven by the selection of 'root DIEs', which are DIEs that |
| 1075 | /// describe variables or functions that are present in the linked |
| 1076 | /// binary (and thus have entries in the debug map). All the debug |
| 1077 | /// information that will be linked (the DIEs, but also the line |
| 1078 | /// tables, ranges, ...) is derived from that set of root DIEs. |
| 1079 | /// |
| 1080 | /// The root DIEs are identified because they contain relocations that |
| 1081 | /// correspond to a debug map entry at specific places (the low_pc for |
| 1082 | /// a function, the location for a variable). These relocations are |
| 1083 | /// called ValidRelocs in the DwarfLinker and are gathered as a very |
| 1084 | /// first step when we start processing a DebugMapObject. |
Frederic Riss | d345518 | 2015-01-28 18:27:01 +0000 | [diff] [blame] | 1085 | class DwarfLinker { |
| 1086 | public: |
Frederic Riss | b981832 | 2015-02-28 00:29:07 +0000 | [diff] [blame] | 1087 | DwarfLinker(StringRef OutputFilename, const LinkOptions &Options) |
| 1088 | : OutputFilename(OutputFilename), Options(Options), |
Frederic Riss | 5a64207 | 2015-06-05 23:06:11 +0000 | [diff] [blame] | 1089 | BinHolder(Options.Verbose), LastCIEOffset(0) {} |
Frederic Riss | d345518 | 2015-01-28 18:27:01 +0000 | [diff] [blame] | 1090 | |
Frederic Riss | b8b43d5 | 2015-03-04 22:07:44 +0000 | [diff] [blame] | 1091 | ~DwarfLinker() { |
| 1092 | for (auto *Abbrev : Abbreviations) |
| 1093 | delete Abbrev; |
| 1094 | } |
| 1095 | |
Frederic Riss | d345518 | 2015-01-28 18:27:01 +0000 | [diff] [blame] | 1096 | /// \brief Link the contents of the DebugMap. |
| 1097 | bool link(const DebugMap &); |
| 1098 | |
| 1099 | private: |
Frederic Riss | 563cba6 | 2015-01-28 22:15:14 +0000 | [diff] [blame] | 1100 | /// \brief Called at the start of a debug object link. |
Frederic Riss | 63786b0 | 2015-03-15 20:45:43 +0000 | [diff] [blame] | 1101 | void startDebugObject(DWARFContext &, DebugMapObject &); |
Frederic Riss | 563cba6 | 2015-01-28 22:15:14 +0000 | [diff] [blame] | 1102 | |
| 1103 | /// \brief Called at the end of a debug object link. |
| 1104 | void endDebugObject(); |
| 1105 | |
Frederic Riss | 1036e64 | 2015-02-13 23:18:22 +0000 | [diff] [blame] | 1106 | /// \defgroup FindValidRelocations Translate debug map into a list |
| 1107 | /// of relevant relocations |
| 1108 | /// |
| 1109 | /// @{ |
| 1110 | struct ValidReloc { |
| 1111 | uint32_t Offset; |
| 1112 | uint32_t Size; |
| 1113 | uint64_t Addend; |
| 1114 | const DebugMapObject::DebugMapEntry *Mapping; |
| 1115 | |
| 1116 | ValidReloc(uint32_t Offset, uint32_t Size, uint64_t Addend, |
| 1117 | const DebugMapObject::DebugMapEntry *Mapping) |
| 1118 | : Offset(Offset), Size(Size), Addend(Addend), Mapping(Mapping) {} |
| 1119 | |
| 1120 | bool operator<(const ValidReloc &RHS) const { return Offset < RHS.Offset; } |
| 1121 | }; |
| 1122 | |
| 1123 | /// \brief The valid relocations for the current DebugMapObject. |
| 1124 | /// This vector is sorted by relocation offset. |
| 1125 | std::vector<ValidReloc> ValidRelocs; |
| 1126 | |
| 1127 | /// \brief Index into ValidRelocs of the next relocation to |
| 1128 | /// consider. As we walk the DIEs in acsending file offset and as |
| 1129 | /// ValidRelocs is sorted by file offset, keeping this index |
| 1130 | /// uptodate is all we have to do to have a cheap lookup during the |
Frederic Riss | 23e20e9 | 2015-03-07 01:25:09 +0000 | [diff] [blame] | 1131 | /// root DIE selection and during DIE cloning. |
Frederic Riss | 1036e64 | 2015-02-13 23:18:22 +0000 | [diff] [blame] | 1132 | unsigned NextValidReloc; |
| 1133 | |
| 1134 | bool findValidRelocsInDebugInfo(const object::ObjectFile &Obj, |
| 1135 | const DebugMapObject &DMO); |
| 1136 | |
| 1137 | bool findValidRelocs(const object::SectionRef &Section, |
| 1138 | const object::ObjectFile &Obj, |
| 1139 | const DebugMapObject &DMO); |
| 1140 | |
| 1141 | void findValidRelocsMachO(const object::SectionRef &Section, |
| 1142 | const object::MachOObjectFile &Obj, |
| 1143 | const DebugMapObject &DMO); |
| 1144 | /// @} |
Frederic Riss | 1b9da42 | 2015-02-13 23:18:29 +0000 | [diff] [blame] | 1145 | |
Frederic Riss | 84c09a5 | 2015-02-13 23:18:34 +0000 | [diff] [blame] | 1146 | /// \defgroup FindRootDIEs Find DIEs corresponding to debug map entries. |
| 1147 | /// |
| 1148 | /// @{ |
| 1149 | /// \brief Recursively walk the \p DIE tree and look for DIEs to |
| 1150 | /// keep. Store that information in \p CU's DIEInfo. |
| 1151 | void lookForDIEsToKeep(const DWARFDebugInfoEntryMinimal &DIE, |
| 1152 | const DebugMapObject &DMO, CompileUnit &CU, |
| 1153 | unsigned Flags); |
| 1154 | |
| 1155 | /// \brief Flags passed to DwarfLinker::lookForDIEsToKeep |
| 1156 | enum TravesalFlags { |
| 1157 | TF_Keep = 1 << 0, ///< Mark the traversed DIEs as kept. |
| 1158 | TF_InFunctionScope = 1 << 1, ///< Current scope is a fucntion scope. |
| 1159 | TF_DependencyWalk = 1 << 2, ///< Walking the dependencies of a kept DIE. |
| 1160 | TF_ParentWalk = 1 << 3, ///< Walking up the parents of a kept DIE. |
Frederic Riss | 1c65094 | 2015-07-21 22:41:43 +0000 | [diff] [blame] | 1161 | TF_ODR = 1 << 4, ///< Use the ODR whhile keeping dependants. |
Frederic Riss | 84c09a5 | 2015-02-13 23:18:34 +0000 | [diff] [blame] | 1162 | }; |
| 1163 | |
| 1164 | /// \brief Mark the passed DIE as well as all the ones it depends on |
| 1165 | /// as kept. |
| 1166 | void keepDIEAndDenpendencies(const DWARFDebugInfoEntryMinimal &DIE, |
| 1167 | CompileUnit::DIEInfo &MyInfo, |
| 1168 | const DebugMapObject &DMO, CompileUnit &CU, |
Frederic Riss | 1c65094 | 2015-07-21 22:41:43 +0000 | [diff] [blame] | 1169 | bool UseODR); |
Frederic Riss | 84c09a5 | 2015-02-13 23:18:34 +0000 | [diff] [blame] | 1170 | |
| 1171 | unsigned shouldKeepDIE(const DWARFDebugInfoEntryMinimal &DIE, |
| 1172 | CompileUnit &Unit, CompileUnit::DIEInfo &MyInfo, |
| 1173 | unsigned Flags); |
| 1174 | |
| 1175 | unsigned shouldKeepVariableDIE(const DWARFDebugInfoEntryMinimal &DIE, |
| 1176 | CompileUnit &Unit, |
| 1177 | CompileUnit::DIEInfo &MyInfo, unsigned Flags); |
| 1178 | |
| 1179 | unsigned shouldKeepSubprogramDIE(const DWARFDebugInfoEntryMinimal &DIE, |
| 1180 | CompileUnit &Unit, |
| 1181 | CompileUnit::DIEInfo &MyInfo, |
| 1182 | unsigned Flags); |
| 1183 | |
| 1184 | bool hasValidRelocation(uint32_t StartOffset, uint32_t EndOffset, |
| 1185 | CompileUnit::DIEInfo &Info); |
| 1186 | /// @} |
| 1187 | |
Frederic Riss | b8b43d5 | 2015-03-04 22:07:44 +0000 | [diff] [blame] | 1188 | /// \defgroup Linking Methods used to link the debug information |
| 1189 | /// |
| 1190 | /// @{ |
| 1191 | /// \brief Recursively clone \p InputDIE into an tree of DIE objects |
| 1192 | /// where useless (as decided by lookForDIEsToKeep()) bits have been |
| 1193 | /// stripped out and addresses have been rewritten according to the |
| 1194 | /// debug map. |
| 1195 | /// |
| 1196 | /// \param OutOffset is the offset the cloned DIE in the output |
| 1197 | /// compile unit. |
Frederic Riss | 31da324 | 2015-03-11 18:45:52 +0000 | [diff] [blame] | 1198 | /// \param PCOffset (while cloning a function scope) is the offset |
| 1199 | /// applied to the entry point of the function to get the linked address. |
Frederic Riss | b8b43d5 | 2015-03-04 22:07:44 +0000 | [diff] [blame] | 1200 | /// |
| 1201 | /// \returns the root of the cloned tree. |
| 1202 | DIE *cloneDIE(const DWARFDebugInfoEntryMinimal &InputDIE, CompileUnit &U, |
Frederic Riss | 31da324 | 2015-03-11 18:45:52 +0000 | [diff] [blame] | 1203 | int64_t PCOffset, uint32_t OutOffset); |
Frederic Riss | b8b43d5 | 2015-03-04 22:07:44 +0000 | [diff] [blame] | 1204 | |
| 1205 | typedef DWARFAbbreviationDeclaration::AttributeSpec AttributeSpec; |
| 1206 | |
Frederic Riss | 31da324 | 2015-03-11 18:45:52 +0000 | [diff] [blame] | 1207 | /// \brief Information gathered and exchanged between the various |
| 1208 | /// clone*Attributes helpers about the attributes of a particular DIE. |
| 1209 | struct AttributesInfo { |
Frederic Riss | bce93ff | 2015-03-16 02:05:10 +0000 | [diff] [blame] | 1210 | const char *Name, *MangledName; ///< Names. |
| 1211 | uint32_t NameOffset, MangledNameOffset; ///< Offsets in the string pool. |
| 1212 | |
Frederic Riss | 7b5563a | 2015-08-31 01:43:14 +0000 | [diff] [blame] | 1213 | uint64_t OrigLowPc; ///< Value of AT_low_pc in the input DIE |
Frederic Riss | 31da324 | 2015-03-11 18:45:52 +0000 | [diff] [blame] | 1214 | uint64_t OrigHighPc; ///< Value of AT_high_pc in the input DIE |
| 1215 | int64_t PCOffset; ///< Offset to apply to PC addresses inside a function. |
| 1216 | |
Frederic Riss | bce93ff | 2015-03-16 02:05:10 +0000 | [diff] [blame] | 1217 | bool HasLowPc; ///< Does the DIE have a low_pc attribute? |
| 1218 | bool IsDeclaration; ///< Is this DIE only a declaration? |
| 1219 | |
| 1220 | AttributesInfo() |
| 1221 | : Name(nullptr), MangledName(nullptr), NameOffset(0), |
Frederic Riss | 7b5563a | 2015-08-31 01:43:14 +0000 | [diff] [blame] | 1222 | MangledNameOffset(0), OrigLowPc(UINT64_MAX), OrigHighPc(0), |
| 1223 | PCOffset(0), HasLowPc(false), IsDeclaration(false) {} |
Frederic Riss | 31da324 | 2015-03-11 18:45:52 +0000 | [diff] [blame] | 1224 | }; |
| 1225 | |
Frederic Riss | b8b43d5 | 2015-03-04 22:07:44 +0000 | [diff] [blame] | 1226 | /// \brief Helper for cloneDIE. |
| 1227 | unsigned cloneAttribute(DIE &Die, const DWARFDebugInfoEntryMinimal &InputDIE, |
| 1228 | CompileUnit &U, const DWARFFormValue &Val, |
Frederic Riss | 31da324 | 2015-03-11 18:45:52 +0000 | [diff] [blame] | 1229 | const AttributeSpec AttrSpec, unsigned AttrSize, |
| 1230 | AttributesInfo &AttrInfo); |
Frederic Riss | b8b43d5 | 2015-03-04 22:07:44 +0000 | [diff] [blame] | 1231 | |
| 1232 | /// \brief Helper for cloneDIE. |
Frederic Riss | ef64846 | 2015-03-06 17:56:30 +0000 | [diff] [blame] | 1233 | unsigned cloneStringAttribute(DIE &Die, AttributeSpec AttrSpec, |
| 1234 | const DWARFFormValue &Val, const DWARFUnit &U); |
Frederic Riss | b8b43d5 | 2015-03-04 22:07:44 +0000 | [diff] [blame] | 1235 | |
| 1236 | /// \brief Helper for cloneDIE. |
Frederic Riss | 9833de6 | 2015-03-06 23:22:53 +0000 | [diff] [blame] | 1237 | unsigned |
| 1238 | cloneDieReferenceAttribute(DIE &Die, |
| 1239 | const DWARFDebugInfoEntryMinimal &InputDIE, |
| 1240 | AttributeSpec AttrSpec, unsigned AttrSize, |
Frederic Riss | 6afcfce | 2015-03-13 18:35:57 +0000 | [diff] [blame] | 1241 | const DWARFFormValue &Val, CompileUnit &Unit); |
Frederic Riss | b8b43d5 | 2015-03-04 22:07:44 +0000 | [diff] [blame] | 1242 | |
| 1243 | /// \brief Helper for cloneDIE. |
| 1244 | unsigned cloneBlockAttribute(DIE &Die, AttributeSpec AttrSpec, |
| 1245 | const DWARFFormValue &Val, unsigned AttrSize); |
| 1246 | |
| 1247 | /// \brief Helper for cloneDIE. |
Frederic Riss | 31da324 | 2015-03-11 18:45:52 +0000 | [diff] [blame] | 1248 | unsigned cloneAddressAttribute(DIE &Die, AttributeSpec AttrSpec, |
| 1249 | const DWARFFormValue &Val, |
| 1250 | const CompileUnit &Unit, AttributesInfo &Info); |
| 1251 | |
| 1252 | /// \brief Helper for cloneDIE. |
Frederic Riss | b8b43d5 | 2015-03-04 22:07:44 +0000 | [diff] [blame] | 1253 | unsigned cloneScalarAttribute(DIE &Die, |
| 1254 | const DWARFDebugInfoEntryMinimal &InputDIE, |
Frederic Riss | 2544087 | 2015-03-13 23:30:31 +0000 | [diff] [blame] | 1255 | CompileUnit &U, AttributeSpec AttrSpec, |
Frederic Riss | dfb9790 | 2015-03-14 15:49:07 +0000 | [diff] [blame] | 1256 | const DWARFFormValue &Val, unsigned AttrSize, |
Frederic Riss | bce93ff | 2015-03-16 02:05:10 +0000 | [diff] [blame] | 1257 | AttributesInfo &Info); |
Frederic Riss | b8b43d5 | 2015-03-04 22:07:44 +0000 | [diff] [blame] | 1258 | |
Frederic Riss | 23e20e9 | 2015-03-07 01:25:09 +0000 | [diff] [blame] | 1259 | /// \brief Helper for cloneDIE. |
| 1260 | bool applyValidRelocs(MutableArrayRef<char> Data, uint32_t BaseOffset, |
| 1261 | bool isLittleEndian); |
| 1262 | |
Frederic Riss | b8b43d5 | 2015-03-04 22:07:44 +0000 | [diff] [blame] | 1263 | /// \brief Assign an abbreviation number to \p Abbrev |
| 1264 | void AssignAbbrev(DIEAbbrev &Abbrev); |
| 1265 | |
| 1266 | /// \brief FoldingSet that uniques the abbreviations. |
| 1267 | FoldingSet<DIEAbbrev> AbbreviationsSet; |
| 1268 | /// \brief Storage for the unique Abbreviations. |
| 1269 | /// This is passed to AsmPrinter::emitDwarfAbbrevs(), thus it cannot |
| 1270 | /// be changed to a vecot of unique_ptrs. |
| 1271 | std::vector<DIEAbbrev *> Abbreviations; |
| 1272 | |
Frederic Riss | 2544087 | 2015-03-13 23:30:31 +0000 | [diff] [blame] | 1273 | /// \brief Compute and emit debug_ranges section for \p Unit, and |
| 1274 | /// patch the attributes referencing it. |
| 1275 | void patchRangesForUnit(const CompileUnit &Unit, DWARFContext &Dwarf) const; |
| 1276 | |
| 1277 | /// \brief Generate and emit the DW_AT_ranges attribute for a |
| 1278 | /// compile_unit if it had one. |
| 1279 | void generateUnitRanges(CompileUnit &Unit) const; |
| 1280 | |
Frederic Riss | 63786b0 | 2015-03-15 20:45:43 +0000 | [diff] [blame] | 1281 | /// \brief Extract the line tables fromt he original dwarf, extract |
| 1282 | /// the relevant parts according to the linked function ranges and |
| 1283 | /// emit the result in the debug_line section. |
| 1284 | void patchLineTableForUnit(CompileUnit &Unit, DWARFContext &OrigDwarf); |
| 1285 | |
Frederic Riss | bce93ff | 2015-03-16 02:05:10 +0000 | [diff] [blame] | 1286 | /// \brief Emit the accelerator entries for \p Unit. |
| 1287 | void emitAcceleratorEntriesForUnit(CompileUnit &Unit); |
| 1288 | |
Frederic Riss | 5a64207 | 2015-06-05 23:06:11 +0000 | [diff] [blame] | 1289 | /// \brief Patch the frame info for an object file and emit it. |
| 1290 | void patchFrameInfoForObject(const DebugMapObject &, DWARFContext &, |
| 1291 | unsigned AddressSize); |
| 1292 | |
Frederic Riss | b8b43d5 | 2015-03-04 22:07:44 +0000 | [diff] [blame] | 1293 | /// \brief DIELoc objects that need to be destructed (but not freed!). |
| 1294 | std::vector<DIELoc *> DIELocs; |
| 1295 | /// \brief DIEBlock objects that need to be destructed (but not freed!). |
| 1296 | std::vector<DIEBlock *> DIEBlocks; |
| 1297 | /// \brief Allocator used for all the DIEValue objects. |
| 1298 | BumpPtrAllocator DIEAlloc; |
| 1299 | /// @} |
| 1300 | |
Frederic Riss | 1c65094 | 2015-07-21 22:41:43 +0000 | [diff] [blame] | 1301 | /// ODR Contexts for that link. |
| 1302 | DeclContextTree ODRContexts; |
| 1303 | |
Frederic Riss | 1b9da42 | 2015-02-13 23:18:29 +0000 | [diff] [blame] | 1304 | /// \defgroup Helpers Various helper methods. |
| 1305 | /// |
| 1306 | /// @{ |
| 1307 | const DWARFDebugInfoEntryMinimal * |
Frederic Riss | 1c65094 | 2015-07-21 22:41:43 +0000 | [diff] [blame] | 1308 | resolveDIEReference(const DWARFFormValue &RefValue, const DWARFUnit &Unit, |
Frederic Riss | 1b9da42 | 2015-02-13 23:18:29 +0000 | [diff] [blame] | 1309 | const DWARFDebugInfoEntryMinimal &DIE, |
| 1310 | CompileUnit *&ReferencedCU); |
| 1311 | |
| 1312 | CompileUnit *getUnitForOffset(unsigned Offset); |
| 1313 | |
Frederic Riss | bce93ff | 2015-03-16 02:05:10 +0000 | [diff] [blame] | 1314 | bool getDIENames(const DWARFDebugInfoEntryMinimal &Die, DWARFUnit &U, |
| 1315 | AttributesInfo &Info); |
| 1316 | |
Frederic Riss | 1b9da42 | 2015-02-13 23:18:29 +0000 | [diff] [blame] | 1317 | void reportWarning(const Twine &Warning, const DWARFUnit *Unit = nullptr, |
Frederic Riss | 2544087 | 2015-03-13 23:30:31 +0000 | [diff] [blame] | 1318 | const DWARFDebugInfoEntryMinimal *DIE = nullptr) const; |
Frederic Riss | c99ea20 | 2015-02-28 00:29:11 +0000 | [diff] [blame] | 1319 | |
| 1320 | bool createStreamer(Triple TheTriple, StringRef OutputFilename); |
Frederic Riss | eb85c8f | 2015-07-24 06:41:11 +0000 | [diff] [blame] | 1321 | |
| 1322 | /// \brief Attempt to load a debug object from disk. |
| 1323 | ErrorOr<const object::ObjectFile &> loadObject(BinaryHolder &BinaryHolder, |
| 1324 | DebugMapObject &Obj, |
| 1325 | const DebugMap &Map); |
Frederic Riss | 1b9da42 | 2015-02-13 23:18:29 +0000 | [diff] [blame] | 1326 | /// @} |
| 1327 | |
Frederic Riss | 563cba6 | 2015-01-28 22:15:14 +0000 | [diff] [blame] | 1328 | private: |
Frederic Riss | d345518 | 2015-01-28 18:27:01 +0000 | [diff] [blame] | 1329 | std::string OutputFilename; |
Frederic Riss | b981832 | 2015-02-28 00:29:07 +0000 | [diff] [blame] | 1330 | LinkOptions Options; |
Frederic Riss | d345518 | 2015-01-28 18:27:01 +0000 | [diff] [blame] | 1331 | BinaryHolder BinHolder; |
Frederic Riss | c99ea20 | 2015-02-28 00:29:11 +0000 | [diff] [blame] | 1332 | std::unique_ptr<DwarfStreamer> Streamer; |
Frederic Riss | 563cba6 | 2015-01-28 22:15:14 +0000 | [diff] [blame] | 1333 | |
| 1334 | /// The units of the current debug map object. |
| 1335 | std::vector<CompileUnit> Units; |
Frederic Riss | 1b9da42 | 2015-02-13 23:18:29 +0000 | [diff] [blame] | 1336 | |
| 1337 | /// The debug map object curently under consideration. |
| 1338 | DebugMapObject *CurrentDebugObject; |
Frederic Riss | ef64846 | 2015-03-06 17:56:30 +0000 | [diff] [blame] | 1339 | |
| 1340 | /// \brief The Dwarf string pool |
| 1341 | NonRelocatableStringpool StringPool; |
Frederic Riss | 63786b0 | 2015-03-15 20:45:43 +0000 | [diff] [blame] | 1342 | |
| 1343 | /// \brief This map is keyed by the entry PC of functions in that |
| 1344 | /// debug object and the associated value is a pair storing the |
| 1345 | /// corresponding end PC and the offset to apply to get the linked |
| 1346 | /// address. |
| 1347 | /// |
| 1348 | /// See startDebugObject() for a more complete description of its use. |
| 1349 | std::map<uint64_t, std::pair<uint64_t, int64_t>> Ranges; |
Frederic Riss | 5a64207 | 2015-06-05 23:06:11 +0000 | [diff] [blame] | 1350 | |
| 1351 | /// \brief The CIEs that have been emitted in the output |
| 1352 | /// section. The actual CIE data serves a the key to this StringMap, |
| 1353 | /// this takes care of comparing the semantics of CIEs defined in |
| 1354 | /// different object files. |
| 1355 | StringMap<uint32_t> EmittedCIEs; |
| 1356 | |
| 1357 | /// Offset of the last CIE that has been emitted in the output |
| 1358 | /// debug_frame section. |
| 1359 | uint32_t LastCIEOffset; |
Frederic Riss | d345518 | 2015-01-28 18:27:01 +0000 | [diff] [blame] | 1360 | }; |
| 1361 | |
Frederic Riss | 1b9da42 | 2015-02-13 23:18:29 +0000 | [diff] [blame] | 1362 | /// \brief Similar to DWARFUnitSection::getUnitForOffset(), but |
| 1363 | /// returning our CompileUnit object instead. |
| 1364 | CompileUnit *DwarfLinker::getUnitForOffset(unsigned Offset) { |
| 1365 | auto CU = |
| 1366 | std::upper_bound(Units.begin(), Units.end(), Offset, |
| 1367 | [](uint32_t LHS, const CompileUnit &RHS) { |
| 1368 | return LHS < RHS.getOrigUnit().getNextUnitOffset(); |
| 1369 | }); |
| 1370 | return CU != Units.end() ? &*CU : nullptr; |
| 1371 | } |
| 1372 | |
| 1373 | /// \brief Resolve the DIE attribute reference that has been |
| 1374 | /// extracted in \p RefValue. The resulting DIE migh be in another |
| 1375 | /// CompileUnit which is stored into \p ReferencedCU. |
| 1376 | /// \returns null if resolving fails for any reason. |
| 1377 | const DWARFDebugInfoEntryMinimal *DwarfLinker::resolveDIEReference( |
Frederic Riss | 1c65094 | 2015-07-21 22:41:43 +0000 | [diff] [blame] | 1378 | const DWARFFormValue &RefValue, const DWARFUnit &Unit, |
Frederic Riss | 1b9da42 | 2015-02-13 23:18:29 +0000 | [diff] [blame] | 1379 | const DWARFDebugInfoEntryMinimal &DIE, CompileUnit *&RefCU) { |
| 1380 | assert(RefValue.isFormClass(DWARFFormValue::FC_Reference)); |
| 1381 | uint64_t RefOffset = *RefValue.getAsReference(&Unit); |
| 1382 | |
| 1383 | if ((RefCU = getUnitForOffset(RefOffset))) |
| 1384 | if (const auto *RefDie = RefCU->getOrigUnit().getDIEForOffset(RefOffset)) |
| 1385 | return RefDie; |
| 1386 | |
| 1387 | reportWarning("could not find referenced DIE", &Unit, &DIE); |
| 1388 | return nullptr; |
| 1389 | } |
| 1390 | |
Frederic Riss | 1c65094 | 2015-07-21 22:41:43 +0000 | [diff] [blame] | 1391 | /// \returns whether the passed \a Attr type might contain a DIE |
| 1392 | /// reference suitable for ODR uniquing. |
| 1393 | static bool isODRAttribute(uint16_t Attr) { |
| 1394 | switch (Attr) { |
| 1395 | default: |
| 1396 | return false; |
| 1397 | case dwarf::DW_AT_type: |
| 1398 | case dwarf::DW_AT_containing_type: |
| 1399 | case dwarf::DW_AT_specification: |
| 1400 | case dwarf::DW_AT_abstract_origin: |
| 1401 | case dwarf::DW_AT_import: |
| 1402 | return true; |
| 1403 | } |
| 1404 | llvm_unreachable("Improper attribute."); |
| 1405 | } |
| 1406 | |
| 1407 | /// Set the last DIE/CU a context was seen in and, possibly invalidate |
| 1408 | /// the context if it is ambiguous. |
| 1409 | /// |
| 1410 | /// In the current implementation, we don't handle overloaded |
| 1411 | /// functions well, because the argument types are not taken into |
| 1412 | /// account when computing the DeclContext tree. |
| 1413 | /// |
| 1414 | /// Some of this is mitigated byt using mangled names that do contain |
| 1415 | /// the arguments types, but sometimes (eg. with function templates) |
| 1416 | /// we don't have that. In that case, just do not unique anything that |
| 1417 | /// refers to the contexts we are not able to distinguish. |
| 1418 | /// |
| 1419 | /// If a context that is not a namespace appears twice in the same CU, |
| 1420 | /// we know it is ambiguous. Make it invalid. |
| 1421 | bool DeclContext::setLastSeenDIE(CompileUnit &U, |
| 1422 | const DWARFDebugInfoEntryMinimal *Die) { |
| 1423 | if (LastSeenCompileUnitID == U.getUniqueID()) { |
| 1424 | DWARFUnit &OrigUnit = U.getOrigUnit(); |
| 1425 | uint32_t FirstIdx = OrigUnit.getDIEIndex(LastSeenDIE); |
| 1426 | U.getInfo(FirstIdx).Ctxt = nullptr; |
| 1427 | return false; |
| 1428 | } |
| 1429 | |
| 1430 | LastSeenCompileUnitID = U.getUniqueID(); |
| 1431 | LastSeenDIE = Die; |
| 1432 | return true; |
| 1433 | } |
| 1434 | |
| 1435 | /// Get the child context of \a Context corresponding to \a DIE. |
| 1436 | /// |
| 1437 | /// \returns the child context or null if we shouldn't track children |
| 1438 | /// contexts. It also returns an additional bit meaning 'invalid'. An |
| 1439 | /// invalid context means it shouldn't be considered for uniquing, but |
| 1440 | /// its not returning null, because some children of that context |
| 1441 | /// might be uniquing candidates. |
| 1442 | /// FIXME: this is for dsymutil-classic compatibility, I don't think |
| 1443 | /// it buys us much. |
| 1444 | PointerIntPair<DeclContext *, 1> DeclContextTree::getChildDeclContext( |
| 1445 | DeclContext &Context, const DWARFDebugInfoEntryMinimal *DIE, CompileUnit &U, |
| 1446 | NonRelocatableStringpool &StringPool) { |
| 1447 | unsigned Tag = DIE->getTag(); |
| 1448 | |
| 1449 | // FIXME: dsymutil-classic compat: We should bail out here if we |
| 1450 | // have a specification or an abstract_origin. We will get the |
| 1451 | // parent context wrong here. |
| 1452 | |
| 1453 | switch (Tag) { |
| 1454 | default: |
| 1455 | // By default stop gathering child contexts. |
| 1456 | return PointerIntPair<DeclContext *, 1>(nullptr); |
| 1457 | case dwarf::DW_TAG_compile_unit: |
| 1458 | // FIXME: Add support for DW_TAG_module. |
| 1459 | return PointerIntPair<DeclContext *, 1>(&Context); |
| 1460 | case dwarf::DW_TAG_subprogram: |
| 1461 | // Do not unique anything inside CU local functions. |
| 1462 | if ((Context.getTag() == dwarf::DW_TAG_namespace || |
| 1463 | Context.getTag() == dwarf::DW_TAG_compile_unit) && |
| 1464 | !DIE->getAttributeValueAsUnsignedConstant(&U.getOrigUnit(), |
| 1465 | dwarf::DW_AT_external, 0)) |
| 1466 | return PointerIntPair<DeclContext *, 1>(nullptr); |
| 1467 | // Fallthrough |
| 1468 | case dwarf::DW_TAG_member: |
| 1469 | case dwarf::DW_TAG_namespace: |
| 1470 | case dwarf::DW_TAG_structure_type: |
| 1471 | case dwarf::DW_TAG_class_type: |
| 1472 | case dwarf::DW_TAG_union_type: |
| 1473 | case dwarf::DW_TAG_enumeration_type: |
| 1474 | case dwarf::DW_TAG_typedef: |
| 1475 | // Artificial things might be ambiguous, because they might be |
| 1476 | // created on demand. For example implicitely defined constructors |
| 1477 | // are ambiguous because of the way we identify contexts, and they |
| 1478 | // won't be generated everytime everywhere. |
| 1479 | if (DIE->getAttributeValueAsUnsignedConstant(&U.getOrigUnit(), |
| 1480 | dwarf::DW_AT_artificial, 0)) |
| 1481 | return PointerIntPair<DeclContext *, 1>(nullptr); |
| 1482 | break; |
| 1483 | } |
| 1484 | |
| 1485 | const char *Name = DIE->getName(&U.getOrigUnit(), DINameKind::LinkageName); |
| 1486 | const char *ShortName = DIE->getName(&U.getOrigUnit(), DINameKind::ShortName); |
| 1487 | StringRef NameRef; |
| 1488 | StringRef ShortNameRef; |
| 1489 | StringRef FileRef; |
| 1490 | |
| 1491 | if (Name) |
| 1492 | NameRef = StringPool.internString(Name); |
| 1493 | else if (Tag == dwarf::DW_TAG_namespace) |
| 1494 | // FIXME: For dsymutil-classic compatibility. I think uniquing |
| 1495 | // within anonymous namespaces is wrong. There is no ODR guarantee |
| 1496 | // there. |
| 1497 | NameRef = StringPool.internString("(anonymous namespace)"); |
| 1498 | |
| 1499 | if (ShortName && ShortName != Name) |
| 1500 | ShortNameRef = StringPool.internString(ShortName); |
| 1501 | else |
| 1502 | ShortNameRef = NameRef; |
| 1503 | |
| 1504 | if (Tag != dwarf::DW_TAG_class_type && Tag != dwarf::DW_TAG_structure_type && |
| 1505 | Tag != dwarf::DW_TAG_union_type && |
| 1506 | Tag != dwarf::DW_TAG_enumeration_type && NameRef.empty()) |
| 1507 | return PointerIntPair<DeclContext *, 1>(nullptr); |
| 1508 | |
| 1509 | std::string File; |
| 1510 | unsigned Line = 0; |
| 1511 | unsigned ByteSize = 0; |
| 1512 | |
| 1513 | // Gather some discriminating data about the DeclContext we will be |
| 1514 | // creating: File, line number and byte size. This shouldn't be |
| 1515 | // necessary, because the ODR is just about names, but given that we |
| 1516 | // do some approximations with overloaded functions and anonymous |
| 1517 | // namespaces, use these additional data points to make the process safer. |
| 1518 | ByteSize = DIE->getAttributeValueAsUnsignedConstant( |
| 1519 | &U.getOrigUnit(), dwarf::DW_AT_byte_size, UINT64_MAX); |
| 1520 | if (Tag != dwarf::DW_TAG_namespace || !Name) { |
| 1521 | if (unsigned FileNum = DIE->getAttributeValueAsUnsignedConstant( |
| 1522 | &U.getOrigUnit(), dwarf::DW_AT_decl_file, 0)) { |
| 1523 | if (const auto *LT = U.getOrigUnit().getContext().getLineTableForUnit( |
| 1524 | &U.getOrigUnit())) { |
| 1525 | // FIXME: dsymutil-classic compatibility. I'd rather not |
| 1526 | // unique anything in anonymous namespaces, but if we do, then |
| 1527 | // verify that the file and line correspond. |
| 1528 | if (!Name && Tag == dwarf::DW_TAG_namespace) |
| 1529 | FileNum = 1; |
| 1530 | |
| 1531 | // FIXME: Passing U.getOrigUnit().getCompilationDir() |
| 1532 | // instead of "" would allow more uniquing, but for now, do |
| 1533 | // it this way to match dsymutil-classic. |
| 1534 | if (LT->getFileNameByIndex( |
| 1535 | FileNum, "", |
| 1536 | DILineInfoSpecifier::FileLineInfoKind::AbsoluteFilePath, |
| 1537 | File)) { |
| 1538 | Line = DIE->getAttributeValueAsUnsignedConstant( |
| 1539 | &U.getOrigUnit(), dwarf::DW_AT_decl_line, 0); |
| 1540 | #ifdef HAVE_REALPATH |
| 1541 | // Cache the resolved paths, because calling realpath is expansive. |
| 1542 | if (const char *ResolvedPath = U.getResolvedPath(FileNum)) { |
| 1543 | File = ResolvedPath; |
| 1544 | } else { |
| 1545 | char RealPath[PATH_MAX + 1]; |
| 1546 | RealPath[PATH_MAX] = 0; |
| 1547 | if (::realpath(File.c_str(), RealPath)) |
| 1548 | File = RealPath; |
| 1549 | U.setResolvedPath(FileNum, File); |
| 1550 | } |
| 1551 | #endif |
| 1552 | FileRef = StringPool.internString(File); |
| 1553 | } |
| 1554 | } |
| 1555 | } |
| 1556 | } |
| 1557 | |
| 1558 | if (!Line && NameRef.empty()) |
| 1559 | return PointerIntPair<DeclContext *, 1>(nullptr); |
| 1560 | |
| 1561 | // FIXME: dsymutil-classic compat won't unique the same type |
| 1562 | // presented once as a struct and once as a class. Use the Tag in |
| 1563 | // the fully qualified name hash to get the same effect. |
| 1564 | // We hash NameRef, which is the mangled name, in order to get most |
| 1565 | // overloaded functions resolvec correctly. |
| 1566 | unsigned Hash = hash_combine(Context.getQualifiedNameHash(), Tag, NameRef); |
| 1567 | |
| 1568 | // FIXME: dsymutil-classic compatibility: when we don't have a name, |
| 1569 | // use the filename. |
| 1570 | if (Tag == dwarf::DW_TAG_namespace && NameRef == "(anonymous namespace)") |
| 1571 | Hash = hash_combine(Hash, FileRef); |
| 1572 | |
| 1573 | // Now look if this context already exists. |
| 1574 | DeclContext Key(Hash, Line, ByteSize, Tag, NameRef, FileRef, Context); |
| 1575 | auto ContextIter = Contexts.find(&Key); |
| 1576 | |
| 1577 | if (ContextIter == Contexts.end()) { |
| 1578 | // The context wasn't found. |
| 1579 | bool Inserted; |
| 1580 | DeclContext *NewContext = |
| 1581 | new (Allocator) DeclContext(Hash, Line, ByteSize, Tag, NameRef, FileRef, |
| 1582 | Context, DIE, U.getUniqueID()); |
| 1583 | std::tie(ContextIter, Inserted) = Contexts.insert(NewContext); |
| 1584 | assert(Inserted && "Failed to insert DeclContext"); |
| 1585 | (void)Inserted; |
| 1586 | } else if (Tag != dwarf::DW_TAG_namespace && |
| 1587 | !(*ContextIter)->setLastSeenDIE(U, DIE)) { |
| 1588 | // The context was found, but it is ambiguous with another context |
| 1589 | // in the same file. Mark it invalid. |
| 1590 | return PointerIntPair<DeclContext *, 1>(*ContextIter, /* Invalid= */ 1); |
| 1591 | } |
| 1592 | |
| 1593 | assert(ContextIter != Contexts.end()); |
| 1594 | // FIXME: dsymutil-classic compatibility. Union types aren't |
| 1595 | // uniques, but their children might be. |
| 1596 | if ((Tag == dwarf::DW_TAG_subprogram && |
| 1597 | Context.getTag() != dwarf::DW_TAG_structure_type && |
| 1598 | Context.getTag() != dwarf::DW_TAG_class_type) || |
| 1599 | (Tag == dwarf::DW_TAG_union_type)) |
| 1600 | return PointerIntPair<DeclContext *, 1>(*ContextIter, /* Invalid= */ 1); |
| 1601 | |
| 1602 | return PointerIntPair<DeclContext *, 1>(*ContextIter); |
| 1603 | } |
| 1604 | |
Frederic Riss | bce93ff | 2015-03-16 02:05:10 +0000 | [diff] [blame] | 1605 | /// \brief Get the potential name and mangled name for the entity |
| 1606 | /// described by \p Die and store them in \Info if they are not |
| 1607 | /// already there. |
| 1608 | /// \returns is a name was found. |
| 1609 | bool DwarfLinker::getDIENames(const DWARFDebugInfoEntryMinimal &Die, |
| 1610 | DWARFUnit &U, AttributesInfo &Info) { |
| 1611 | // FIXME: a bit wastefull as the first getName might return the |
| 1612 | // short name. |
| 1613 | if (!Info.MangledName && |
| 1614 | (Info.MangledName = Die.getName(&U, DINameKind::LinkageName))) |
| 1615 | Info.MangledNameOffset = StringPool.getStringOffset(Info.MangledName); |
| 1616 | |
| 1617 | if (!Info.Name && (Info.Name = Die.getName(&U, DINameKind::ShortName))) |
| 1618 | Info.NameOffset = StringPool.getStringOffset(Info.Name); |
| 1619 | |
| 1620 | return Info.Name || Info.MangledName; |
| 1621 | } |
| 1622 | |
Frederic Riss | 1b9da42 | 2015-02-13 23:18:29 +0000 | [diff] [blame] | 1623 | /// \brief Report a warning to the user, optionaly including |
| 1624 | /// information about a specific \p DIE related to the warning. |
| 1625 | void DwarfLinker::reportWarning(const Twine &Warning, const DWARFUnit *Unit, |
Frederic Riss | 2544087 | 2015-03-13 23:30:31 +0000 | [diff] [blame] | 1626 | const DWARFDebugInfoEntryMinimal *DIE) const { |
Frederic Riss | def4fb7 | 2015-02-28 00:29:01 +0000 | [diff] [blame] | 1627 | StringRef Context = "<debug map>"; |
Frederic Riss | 1b9da42 | 2015-02-13 23:18:29 +0000 | [diff] [blame] | 1628 | if (CurrentDebugObject) |
Frederic Riss | def4fb7 | 2015-02-28 00:29:01 +0000 | [diff] [blame] | 1629 | Context = CurrentDebugObject->getObjectFilename(); |
| 1630 | warn(Warning, Context); |
Frederic Riss | 1b9da42 | 2015-02-13 23:18:29 +0000 | [diff] [blame] | 1631 | |
Frederic Riss | b981832 | 2015-02-28 00:29:07 +0000 | [diff] [blame] | 1632 | if (!Options.Verbose || !DIE) |
Frederic Riss | 1b9da42 | 2015-02-13 23:18:29 +0000 | [diff] [blame] | 1633 | return; |
| 1634 | |
| 1635 | errs() << " in DIE:\n"; |
| 1636 | DIE->dump(errs(), const_cast<DWARFUnit *>(Unit), 0 /* RecurseDepth */, |
| 1637 | 6 /* Indent */); |
| 1638 | } |
| 1639 | |
Frederic Riss | c99ea20 | 2015-02-28 00:29:11 +0000 | [diff] [blame] | 1640 | bool DwarfLinker::createStreamer(Triple TheTriple, StringRef OutputFilename) { |
| 1641 | if (Options.NoOutput) |
| 1642 | return true; |
| 1643 | |
Frederic Riss | b52cf52 | 2015-02-28 00:42:37 +0000 | [diff] [blame] | 1644 | Streamer = llvm::make_unique<DwarfStreamer>(); |
Frederic Riss | c99ea20 | 2015-02-28 00:29:11 +0000 | [diff] [blame] | 1645 | return Streamer->init(TheTriple, OutputFilename); |
| 1646 | } |
| 1647 | |
Frederic Riss | 563cba6 | 2015-01-28 22:15:14 +0000 | [diff] [blame] | 1648 | /// \brief Recursive helper to gather the child->parent relationships in the |
| 1649 | /// original compile unit. |
Frederic Riss | 9aa725b | 2015-02-13 23:18:31 +0000 | [diff] [blame] | 1650 | static void gatherDIEParents(const DWARFDebugInfoEntryMinimal *DIE, |
Frederic Riss | 1c65094 | 2015-07-21 22:41:43 +0000 | [diff] [blame] | 1651 | unsigned ParentIdx, CompileUnit &CU, |
| 1652 | DeclContext *CurrentDeclContext, |
| 1653 | NonRelocatableStringpool &StringPool, |
| 1654 | DeclContextTree &Contexts) { |
Frederic Riss | 563cba6 | 2015-01-28 22:15:14 +0000 | [diff] [blame] | 1655 | unsigned MyIdx = CU.getOrigUnit().getDIEIndex(DIE); |
Frederic Riss | 1c65094 | 2015-07-21 22:41:43 +0000 | [diff] [blame] | 1656 | CompileUnit::DIEInfo &Info = CU.getInfo(MyIdx); |
| 1657 | |
| 1658 | Info.ParentIdx = ParentIdx; |
| 1659 | if (CU.hasODR()) { |
| 1660 | if (CurrentDeclContext) { |
| 1661 | auto PtrInvalidPair = Contexts.getChildDeclContext(*CurrentDeclContext, |
| 1662 | DIE, CU, StringPool); |
| 1663 | CurrentDeclContext = PtrInvalidPair.getPointer(); |
| 1664 | Info.Ctxt = |
| 1665 | PtrInvalidPair.getInt() ? nullptr : PtrInvalidPair.getPointer(); |
| 1666 | } else |
| 1667 | Info.Ctxt = CurrentDeclContext = nullptr; |
| 1668 | } |
Frederic Riss | 563cba6 | 2015-01-28 22:15:14 +0000 | [diff] [blame] | 1669 | |
| 1670 | if (DIE->hasChildren()) |
| 1671 | for (auto *Child = DIE->getFirstChild(); Child && !Child->isNULL(); |
| 1672 | Child = Child->getSibling()) |
Frederic Riss | 1c65094 | 2015-07-21 22:41:43 +0000 | [diff] [blame] | 1673 | gatherDIEParents(Child, MyIdx, CU, CurrentDeclContext, StringPool, |
| 1674 | Contexts); |
Frederic Riss | 563cba6 | 2015-01-28 22:15:14 +0000 | [diff] [blame] | 1675 | } |
| 1676 | |
Frederic Riss | 84c09a5 | 2015-02-13 23:18:34 +0000 | [diff] [blame] | 1677 | static bool dieNeedsChildrenToBeMeaningful(uint32_t Tag) { |
| 1678 | switch (Tag) { |
| 1679 | default: |
| 1680 | return false; |
| 1681 | case dwarf::DW_TAG_subprogram: |
| 1682 | case dwarf::DW_TAG_lexical_block: |
| 1683 | case dwarf::DW_TAG_subroutine_type: |
| 1684 | case dwarf::DW_TAG_structure_type: |
| 1685 | case dwarf::DW_TAG_class_type: |
| 1686 | case dwarf::DW_TAG_union_type: |
| 1687 | return true; |
| 1688 | } |
| 1689 | llvm_unreachable("Invalid Tag"); |
| 1690 | } |
| 1691 | |
Frederic Riss | 1c65094 | 2015-07-21 22:41:43 +0000 | [diff] [blame] | 1692 | static unsigned getRefAddrSize(const DWARFUnit &U) { |
| 1693 | if (U.getVersion() == 2) |
| 1694 | return U.getAddressByteSize(); |
| 1695 | return 4; |
| 1696 | } |
| 1697 | |
Frederic Riss | 63786b0 | 2015-03-15 20:45:43 +0000 | [diff] [blame] | 1698 | void DwarfLinker::startDebugObject(DWARFContext &Dwarf, DebugMapObject &Obj) { |
Frederic Riss | 563cba6 | 2015-01-28 22:15:14 +0000 | [diff] [blame] | 1699 | Units.reserve(Dwarf.getNumCompileUnits()); |
Frederic Riss | 1036e64 | 2015-02-13 23:18:22 +0000 | [diff] [blame] | 1700 | NextValidReloc = 0; |
Frederic Riss | 63786b0 | 2015-03-15 20:45:43 +0000 | [diff] [blame] | 1701 | // Iterate over the debug map entries and put all the ones that are |
| 1702 | // functions (because they have a size) into the Ranges map. This |
| 1703 | // map is very similar to the FunctionRanges that are stored in each |
| 1704 | // unit, with 2 notable differences: |
| 1705 | // - obviously this one is global, while the other ones are per-unit. |
| 1706 | // - this one contains not only the functions described in the DIE |
| 1707 | // tree, but also the ones that are only in the debug map. |
| 1708 | // The latter information is required to reproduce dsymutil's logic |
| 1709 | // while linking line tables. The cases where this information |
| 1710 | // matters look like bugs that need to be investigated, but for now |
| 1711 | // we need to reproduce dsymutil's behavior. |
| 1712 | // FIXME: Once we understood exactly if that information is needed, |
| 1713 | // maybe totally remove this (or try to use it to do a real |
| 1714 | // -gline-tables-only on Darwin. |
| 1715 | for (const auto &Entry : Obj.symbols()) { |
| 1716 | const auto &Mapping = Entry.getValue(); |
| 1717 | if (Mapping.Size) |
| 1718 | Ranges[Mapping.ObjectAddress] = std::make_pair( |
| 1719 | Mapping.ObjectAddress + Mapping.Size, |
| 1720 | int64_t(Mapping.BinaryAddress) - Mapping.ObjectAddress); |
| 1721 | } |
Frederic Riss | 563cba6 | 2015-01-28 22:15:14 +0000 | [diff] [blame] | 1722 | } |
| 1723 | |
Frederic Riss | 1036e64 | 2015-02-13 23:18:22 +0000 | [diff] [blame] | 1724 | void DwarfLinker::endDebugObject() { |
| 1725 | Units.clear(); |
| 1726 | ValidRelocs.clear(); |
Frederic Riss | 63786b0 | 2015-03-15 20:45:43 +0000 | [diff] [blame] | 1727 | Ranges.clear(); |
Frederic Riss | b8b43d5 | 2015-03-04 22:07:44 +0000 | [diff] [blame] | 1728 | |
Aaron Ballman | a17cbff | 2015-06-26 14:51:22 +0000 | [diff] [blame] | 1729 | for (auto I = DIEBlocks.begin(), E = DIEBlocks.end(); I != E; ++I) |
| 1730 | (*I)->~DIEBlock(); |
| 1731 | for (auto I = DIELocs.begin(), E = DIELocs.end(); I != E; ++I) |
| 1732 | (*I)->~DIELoc(); |
Frederic Riss | b8b43d5 | 2015-03-04 22:07:44 +0000 | [diff] [blame] | 1733 | |
| 1734 | DIEBlocks.clear(); |
| 1735 | DIELocs.clear(); |
| 1736 | DIEAlloc.Reset(); |
Frederic Riss | 1036e64 | 2015-02-13 23:18:22 +0000 | [diff] [blame] | 1737 | } |
| 1738 | |
| 1739 | /// \brief Iterate over the relocations of the given \p Section and |
| 1740 | /// store the ones that correspond to debug map entries into the |
| 1741 | /// ValidRelocs array. |
| 1742 | void DwarfLinker::findValidRelocsMachO(const object::SectionRef &Section, |
| 1743 | const object::MachOObjectFile &Obj, |
| 1744 | const DebugMapObject &DMO) { |
| 1745 | StringRef Contents; |
| 1746 | Section.getContents(Contents); |
| 1747 | DataExtractor Data(Contents, Obj.isLittleEndian(), 0); |
| 1748 | |
| 1749 | for (const object::RelocationRef &Reloc : Section.relocations()) { |
| 1750 | object::DataRefImpl RelocDataRef = Reloc.getRawDataRefImpl(); |
| 1751 | MachO::any_relocation_info MachOReloc = Obj.getRelocation(RelocDataRef); |
| 1752 | unsigned RelocSize = 1 << Obj.getAnyRelocationLength(MachOReloc); |
Rafael Espindola | 96d071c | 2015-06-29 23:29:12 +0000 | [diff] [blame] | 1753 | uint64_t Offset64 = Reloc.getOffset(); |
| 1754 | if ((RelocSize != 4 && RelocSize != 8)) { |
Frederic Riss | 1b9da42 | 2015-02-13 23:18:29 +0000 | [diff] [blame] | 1755 | reportWarning(" unsupported relocation in debug_info section."); |
Frederic Riss | 1036e64 | 2015-02-13 23:18:22 +0000 | [diff] [blame] | 1756 | continue; |
| 1757 | } |
| 1758 | uint32_t Offset = Offset64; |
| 1759 | // Mach-o uses REL relocations, the addend is at the relocation offset. |
| 1760 | uint64_t Addend = Data.getUnsigned(&Offset, RelocSize); |
| 1761 | |
| 1762 | auto Sym = Reloc.getSymbol(); |
| 1763 | if (Sym != Obj.symbol_end()) { |
Rafael Espindola | 5d0c2ff | 2015-07-02 20:55:21 +0000 | [diff] [blame] | 1764 | ErrorOr<StringRef> SymbolName = Sym->getName(); |
| 1765 | if (!SymbolName) { |
Frederic Riss | 1b9da42 | 2015-02-13 23:18:29 +0000 | [diff] [blame] | 1766 | reportWarning("error getting relocation symbol name."); |
Frederic Riss | 1036e64 | 2015-02-13 23:18:22 +0000 | [diff] [blame] | 1767 | continue; |
| 1768 | } |
Rafael Espindola | 5d0c2ff | 2015-07-02 20:55:21 +0000 | [diff] [blame] | 1769 | if (const auto *Mapping = DMO.lookupSymbol(*SymbolName)) |
Frederic Riss | 1036e64 | 2015-02-13 23:18:22 +0000 | [diff] [blame] | 1770 | ValidRelocs.emplace_back(Offset64, RelocSize, Addend, Mapping); |
| 1771 | } else if (const auto *Mapping = DMO.lookupObjectAddress(Addend)) { |
| 1772 | // Do not store the addend. The addend was the address of the |
| 1773 | // symbol in the object file, the address in the binary that is |
| 1774 | // stored in the debug map doesn't need to be offseted. |
| 1775 | ValidRelocs.emplace_back(Offset64, RelocSize, 0, Mapping); |
| 1776 | } |
| 1777 | } |
| 1778 | } |
| 1779 | |
| 1780 | /// \brief Dispatch the valid relocation finding logic to the |
| 1781 | /// appropriate handler depending on the object file format. |
| 1782 | bool DwarfLinker::findValidRelocs(const object::SectionRef &Section, |
| 1783 | const object::ObjectFile &Obj, |
| 1784 | const DebugMapObject &DMO) { |
| 1785 | // Dispatch to the right handler depending on the file type. |
| 1786 | if (auto *MachOObj = dyn_cast<object::MachOObjectFile>(&Obj)) |
| 1787 | findValidRelocsMachO(Section, *MachOObj, DMO); |
| 1788 | else |
Frederic Riss | 1b9da42 | 2015-02-13 23:18:29 +0000 | [diff] [blame] | 1789 | reportWarning(Twine("unsupported object file type: ") + Obj.getFileName()); |
Frederic Riss | 1036e64 | 2015-02-13 23:18:22 +0000 | [diff] [blame] | 1790 | |
| 1791 | if (ValidRelocs.empty()) |
| 1792 | return false; |
| 1793 | |
| 1794 | // Sort the relocations by offset. We will walk the DIEs linearly in |
| 1795 | // the file, this allows us to just keep an index in the relocation |
| 1796 | // array that we advance during our walk, rather than resorting to |
| 1797 | // some associative container. See DwarfLinker::NextValidReloc. |
| 1798 | std::sort(ValidRelocs.begin(), ValidRelocs.end()); |
| 1799 | return true; |
| 1800 | } |
| 1801 | |
| 1802 | /// \brief Look for relocations in the debug_info section that match |
| 1803 | /// entries in the debug map. These relocations will drive the Dwarf |
| 1804 | /// link by indicating which DIEs refer to symbols present in the |
| 1805 | /// linked binary. |
| 1806 | /// \returns wether there are any valid relocations in the debug info. |
| 1807 | bool DwarfLinker::findValidRelocsInDebugInfo(const object::ObjectFile &Obj, |
| 1808 | const DebugMapObject &DMO) { |
| 1809 | // Find the debug_info section. |
| 1810 | for (const object::SectionRef &Section : Obj.sections()) { |
| 1811 | StringRef SectionName; |
| 1812 | Section.getName(SectionName); |
| 1813 | SectionName = SectionName.substr(SectionName.find_first_not_of("._")); |
| 1814 | if (SectionName != "debug_info") |
| 1815 | continue; |
| 1816 | return findValidRelocs(Section, Obj, DMO); |
| 1817 | } |
| 1818 | return false; |
| 1819 | } |
Frederic Riss | 563cba6 | 2015-01-28 22:15:14 +0000 | [diff] [blame] | 1820 | |
Frederic Riss | 84c09a5 | 2015-02-13 23:18:34 +0000 | [diff] [blame] | 1821 | /// \brief Checks that there is a relocation against an actual debug |
| 1822 | /// map entry between \p StartOffset and \p NextOffset. |
| 1823 | /// |
| 1824 | /// This function must be called with offsets in strictly ascending |
| 1825 | /// order because it never looks back at relocations it already 'went past'. |
| 1826 | /// \returns true and sets Info.InDebugMap if it is the case. |
| 1827 | bool DwarfLinker::hasValidRelocation(uint32_t StartOffset, uint32_t EndOffset, |
| 1828 | CompileUnit::DIEInfo &Info) { |
| 1829 | assert(NextValidReloc == 0 || |
| 1830 | StartOffset > ValidRelocs[NextValidReloc - 1].Offset); |
| 1831 | if (NextValidReloc >= ValidRelocs.size()) |
| 1832 | return false; |
| 1833 | |
| 1834 | uint64_t RelocOffset = ValidRelocs[NextValidReloc].Offset; |
| 1835 | |
| 1836 | // We might need to skip some relocs that we didn't consider. For |
| 1837 | // example the high_pc of a discarded DIE might contain a reloc that |
| 1838 | // is in the list because it actually corresponds to the start of a |
| 1839 | // function that is in the debug map. |
| 1840 | while (RelocOffset < StartOffset && NextValidReloc < ValidRelocs.size() - 1) |
| 1841 | RelocOffset = ValidRelocs[++NextValidReloc].Offset; |
| 1842 | |
| 1843 | if (RelocOffset < StartOffset || RelocOffset >= EndOffset) |
| 1844 | return false; |
| 1845 | |
| 1846 | const auto &ValidReloc = ValidRelocs[NextValidReloc++]; |
Frederic Riss | 08462f7 | 2015-06-01 21:12:45 +0000 | [diff] [blame] | 1847 | const auto &Mapping = ValidReloc.Mapping->getValue(); |
Frederic Riss | b981832 | 2015-02-28 00:29:07 +0000 | [diff] [blame] | 1848 | if (Options.Verbose) |
Frederic Riss | 84c09a5 | 2015-02-13 23:18:34 +0000 | [diff] [blame] | 1849 | outs() << "Found valid debug map entry: " << ValidReloc.Mapping->getKey() |
| 1850 | << " " << format("\t%016" PRIx64 " => %016" PRIx64, |
Frederic Riss | 08462f7 | 2015-06-01 21:12:45 +0000 | [diff] [blame] | 1851 | uint64_t(Mapping.ObjectAddress), |
| 1852 | uint64_t(Mapping.BinaryAddress)); |
Frederic Riss | 84c09a5 | 2015-02-13 23:18:34 +0000 | [diff] [blame] | 1853 | |
Frederic Riss | f37964c | 2015-06-05 20:27:07 +0000 | [diff] [blame] | 1854 | Info.AddrAdjust = int64_t(Mapping.BinaryAddress) + ValidReloc.Addend - |
| 1855 | Mapping.ObjectAddress; |
Frederic Riss | 84c09a5 | 2015-02-13 23:18:34 +0000 | [diff] [blame] | 1856 | Info.InDebugMap = true; |
| 1857 | return true; |
| 1858 | } |
| 1859 | |
| 1860 | /// \brief Get the starting and ending (exclusive) offset for the |
| 1861 | /// attribute with index \p Idx descibed by \p Abbrev. \p Offset is |
| 1862 | /// supposed to point to the position of the first attribute described |
| 1863 | /// by \p Abbrev. |
| 1864 | /// \return [StartOffset, EndOffset) as a pair. |
| 1865 | static std::pair<uint32_t, uint32_t> |
| 1866 | getAttributeOffsets(const DWARFAbbreviationDeclaration *Abbrev, unsigned Idx, |
| 1867 | unsigned Offset, const DWARFUnit &Unit) { |
| 1868 | DataExtractor Data = Unit.getDebugInfoExtractor(); |
| 1869 | |
| 1870 | for (unsigned i = 0; i < Idx; ++i) |
| 1871 | DWARFFormValue::skipValue(Abbrev->getFormByIndex(i), Data, &Offset, &Unit); |
| 1872 | |
| 1873 | uint32_t End = Offset; |
| 1874 | DWARFFormValue::skipValue(Abbrev->getFormByIndex(Idx), Data, &End, &Unit); |
| 1875 | |
| 1876 | return std::make_pair(Offset, End); |
| 1877 | } |
| 1878 | |
| 1879 | /// \brief Check if a variable describing DIE should be kept. |
| 1880 | /// \returns updated TraversalFlags. |
| 1881 | unsigned DwarfLinker::shouldKeepVariableDIE( |
| 1882 | const DWARFDebugInfoEntryMinimal &DIE, CompileUnit &Unit, |
| 1883 | CompileUnit::DIEInfo &MyInfo, unsigned Flags) { |
| 1884 | const auto *Abbrev = DIE.getAbbreviationDeclarationPtr(); |
| 1885 | |
| 1886 | // Global variables with constant value can always be kept. |
| 1887 | if (!(Flags & TF_InFunctionScope) && |
| 1888 | Abbrev->findAttributeIndex(dwarf::DW_AT_const_value) != -1U) { |
| 1889 | MyInfo.InDebugMap = true; |
| 1890 | return Flags | TF_Keep; |
| 1891 | } |
| 1892 | |
| 1893 | uint32_t LocationIdx = Abbrev->findAttributeIndex(dwarf::DW_AT_location); |
| 1894 | if (LocationIdx == -1U) |
| 1895 | return Flags; |
| 1896 | |
| 1897 | uint32_t Offset = DIE.getOffset() + getULEB128Size(Abbrev->getCode()); |
| 1898 | const DWARFUnit &OrigUnit = Unit.getOrigUnit(); |
| 1899 | uint32_t LocationOffset, LocationEndOffset; |
| 1900 | std::tie(LocationOffset, LocationEndOffset) = |
| 1901 | getAttributeOffsets(Abbrev, LocationIdx, Offset, OrigUnit); |
| 1902 | |
| 1903 | // See if there is a relocation to a valid debug map entry inside |
| 1904 | // this variable's location. The order is important here. We want to |
| 1905 | // always check in the variable has a valid relocation, so that the |
| 1906 | // DIEInfo is filled. However, we don't want a static variable in a |
| 1907 | // function to force us to keep the enclosing function. |
| 1908 | if (!hasValidRelocation(LocationOffset, LocationEndOffset, MyInfo) || |
| 1909 | (Flags & TF_InFunctionScope)) |
| 1910 | return Flags; |
| 1911 | |
Frederic Riss | b981832 | 2015-02-28 00:29:07 +0000 | [diff] [blame] | 1912 | if (Options.Verbose) |
Frederic Riss | 84c09a5 | 2015-02-13 23:18:34 +0000 | [diff] [blame] | 1913 | DIE.dump(outs(), const_cast<DWARFUnit *>(&OrigUnit), 0, 8 /* Indent */); |
| 1914 | |
| 1915 | return Flags | TF_Keep; |
| 1916 | } |
| 1917 | |
| 1918 | /// \brief Check if a function describing DIE should be kept. |
| 1919 | /// \returns updated TraversalFlags. |
| 1920 | unsigned DwarfLinker::shouldKeepSubprogramDIE( |
| 1921 | const DWARFDebugInfoEntryMinimal &DIE, CompileUnit &Unit, |
| 1922 | CompileUnit::DIEInfo &MyInfo, unsigned Flags) { |
| 1923 | const auto *Abbrev = DIE.getAbbreviationDeclarationPtr(); |
| 1924 | |
| 1925 | Flags |= TF_InFunctionScope; |
| 1926 | |
| 1927 | uint32_t LowPcIdx = Abbrev->findAttributeIndex(dwarf::DW_AT_low_pc); |
| 1928 | if (LowPcIdx == -1U) |
| 1929 | return Flags; |
| 1930 | |
| 1931 | uint32_t Offset = DIE.getOffset() + getULEB128Size(Abbrev->getCode()); |
| 1932 | const DWARFUnit &OrigUnit = Unit.getOrigUnit(); |
| 1933 | uint32_t LowPcOffset, LowPcEndOffset; |
| 1934 | std::tie(LowPcOffset, LowPcEndOffset) = |
| 1935 | getAttributeOffsets(Abbrev, LowPcIdx, Offset, OrigUnit); |
| 1936 | |
| 1937 | uint64_t LowPc = |
| 1938 | DIE.getAttributeValueAsAddress(&OrigUnit, dwarf::DW_AT_low_pc, -1ULL); |
| 1939 | assert(LowPc != -1ULL && "low_pc attribute is not an address."); |
| 1940 | if (LowPc == -1ULL || |
| 1941 | !hasValidRelocation(LowPcOffset, LowPcEndOffset, MyInfo)) |
| 1942 | return Flags; |
| 1943 | |
Frederic Riss | b981832 | 2015-02-28 00:29:07 +0000 | [diff] [blame] | 1944 | if (Options.Verbose) |
Frederic Riss | 84c09a5 | 2015-02-13 23:18:34 +0000 | [diff] [blame] | 1945 | DIE.dump(outs(), const_cast<DWARFUnit *>(&OrigUnit), 0, 8 /* Indent */); |
| 1946 | |
Frederic Riss | 1af75f7 | 2015-03-12 18:45:10 +0000 | [diff] [blame] | 1947 | Flags |= TF_Keep; |
| 1948 | |
| 1949 | DWARFFormValue HighPcValue; |
| 1950 | if (!DIE.getAttributeValue(&OrigUnit, dwarf::DW_AT_high_pc, HighPcValue)) { |
| 1951 | reportWarning("Function without high_pc. Range will be discarded.\n", |
| 1952 | &OrigUnit, &DIE); |
| 1953 | return Flags; |
| 1954 | } |
| 1955 | |
| 1956 | uint64_t HighPc; |
| 1957 | if (HighPcValue.isFormClass(DWARFFormValue::FC_Address)) { |
| 1958 | HighPc = *HighPcValue.getAsAddress(&OrigUnit); |
| 1959 | } else { |
| 1960 | assert(HighPcValue.isFormClass(DWARFFormValue::FC_Constant)); |
| 1961 | HighPc = LowPc + *HighPcValue.getAsUnsignedConstant(); |
| 1962 | } |
| 1963 | |
Frederic Riss | 63786b0 | 2015-03-15 20:45:43 +0000 | [diff] [blame] | 1964 | // Replace the debug map range with a more accurate one. |
| 1965 | Ranges[LowPc] = std::make_pair(HighPc, MyInfo.AddrAdjust); |
Frederic Riss | 1af75f7 | 2015-03-12 18:45:10 +0000 | [diff] [blame] | 1966 | Unit.addFunctionRange(LowPc, HighPc, MyInfo.AddrAdjust); |
| 1967 | return Flags; |
Frederic Riss | 84c09a5 | 2015-02-13 23:18:34 +0000 | [diff] [blame] | 1968 | } |
| 1969 | |
| 1970 | /// \brief Check if a DIE should be kept. |
| 1971 | /// \returns updated TraversalFlags. |
| 1972 | unsigned DwarfLinker::shouldKeepDIE(const DWARFDebugInfoEntryMinimal &DIE, |
| 1973 | CompileUnit &Unit, |
| 1974 | CompileUnit::DIEInfo &MyInfo, |
| 1975 | unsigned Flags) { |
| 1976 | switch (DIE.getTag()) { |
| 1977 | case dwarf::DW_TAG_constant: |
| 1978 | case dwarf::DW_TAG_variable: |
| 1979 | return shouldKeepVariableDIE(DIE, Unit, MyInfo, Flags); |
| 1980 | case dwarf::DW_TAG_subprogram: |
| 1981 | return shouldKeepSubprogramDIE(DIE, Unit, MyInfo, Flags); |
| 1982 | case dwarf::DW_TAG_module: |
| 1983 | case dwarf::DW_TAG_imported_module: |
| 1984 | case dwarf::DW_TAG_imported_declaration: |
| 1985 | case dwarf::DW_TAG_imported_unit: |
| 1986 | // We always want to keep these. |
| 1987 | return Flags | TF_Keep; |
| 1988 | } |
| 1989 | |
| 1990 | return Flags; |
| 1991 | } |
| 1992 | |
Frederic Riss | 84c09a5 | 2015-02-13 23:18:34 +0000 | [diff] [blame] | 1993 | /// \brief Mark the passed DIE as well as all the ones it depends on |
| 1994 | /// as kept. |
| 1995 | /// |
| 1996 | /// This function is called by lookForDIEsToKeep on DIEs that are |
| 1997 | /// newly discovered to be needed in the link. It recursively calls |
| 1998 | /// back to lookForDIEsToKeep while adding TF_DependencyWalk to the |
| 1999 | /// TraversalFlags to inform it that it's not doing the primary DIE |
| 2000 | /// tree walk. |
| 2001 | void DwarfLinker::keepDIEAndDenpendencies(const DWARFDebugInfoEntryMinimal &DIE, |
| 2002 | CompileUnit::DIEInfo &MyInfo, |
| 2003 | const DebugMapObject &DMO, |
Frederic Riss | 1c65094 | 2015-07-21 22:41:43 +0000 | [diff] [blame] | 2004 | CompileUnit &CU, bool UseODR) { |
Frederic Riss | 84c09a5 | 2015-02-13 23:18:34 +0000 | [diff] [blame] | 2005 | const DWARFUnit &Unit = CU.getOrigUnit(); |
| 2006 | MyInfo.Keep = true; |
| 2007 | |
| 2008 | // First mark all the parent chain as kept. |
| 2009 | unsigned AncestorIdx = MyInfo.ParentIdx; |
| 2010 | while (!CU.getInfo(AncestorIdx).Keep) { |
Frederic Riss | 1c65094 | 2015-07-21 22:41:43 +0000 | [diff] [blame] | 2011 | unsigned ODRFlag = UseODR ? TF_ODR : 0; |
Frederic Riss | 84c09a5 | 2015-02-13 23:18:34 +0000 | [diff] [blame] | 2012 | lookForDIEsToKeep(*Unit.getDIEAtIndex(AncestorIdx), DMO, CU, |
Frederic Riss | 1c65094 | 2015-07-21 22:41:43 +0000 | [diff] [blame] | 2013 | TF_ParentWalk | TF_Keep | TF_DependencyWalk | ODRFlag); |
Frederic Riss | 84c09a5 | 2015-02-13 23:18:34 +0000 | [diff] [blame] | 2014 | AncestorIdx = CU.getInfo(AncestorIdx).ParentIdx; |
| 2015 | } |
| 2016 | |
| 2017 | // Then we need to mark all the DIEs referenced by this DIE's |
| 2018 | // attributes as kept. |
| 2019 | DataExtractor Data = Unit.getDebugInfoExtractor(); |
| 2020 | const auto *Abbrev = DIE.getAbbreviationDeclarationPtr(); |
| 2021 | uint32_t Offset = DIE.getOffset() + getULEB128Size(Abbrev->getCode()); |
| 2022 | |
| 2023 | // Mark all DIEs referenced through atttributes as kept. |
| 2024 | for (const auto &AttrSpec : Abbrev->attributes()) { |
| 2025 | DWARFFormValue Val(AttrSpec.Form); |
| 2026 | |
| 2027 | if (!Val.isFormClass(DWARFFormValue::FC_Reference)) { |
| 2028 | DWARFFormValue::skipValue(AttrSpec.Form, Data, &Offset, &Unit); |
| 2029 | continue; |
| 2030 | } |
| 2031 | |
| 2032 | Val.extractValue(Data, &Offset, &Unit); |
| 2033 | CompileUnit *ReferencedCU; |
Frederic Riss | 1c65094 | 2015-07-21 22:41:43 +0000 | [diff] [blame] | 2034 | if (const auto *RefDIE = |
| 2035 | resolveDIEReference(Val, Unit, DIE, ReferencedCU)) { |
| 2036 | uint32_t RefIdx = ReferencedCU->getOrigUnit().getDIEIndex(RefDIE); |
| 2037 | CompileUnit::DIEInfo &Info = ReferencedCU->getInfo(RefIdx); |
| 2038 | // If the referenced DIE has a DeclContext that has already been |
| 2039 | // emitted, then do not keep the one in this CU. We'll link to |
| 2040 | // the canonical DIE in cloneDieReferenceAttribute. |
| 2041 | // FIXME: compatibility with dsymutil-classic. UseODR shouldn't |
| 2042 | // be necessary and could be advantageously replaced by |
| 2043 | // ReferencedCU->hasODR() && CU.hasODR(). |
| 2044 | // FIXME: compatibility with dsymutil-classic. There is no |
| 2045 | // reason not to unique ref_addr references. |
| 2046 | if (AttrSpec.Form != dwarf::DW_FORM_ref_addr && UseODR && Info.Ctxt && |
| 2047 | Info.Ctxt != ReferencedCU->getInfo(Info.ParentIdx).Ctxt && |
| 2048 | Info.Ctxt->getCanonicalDIEOffset() && isODRAttribute(AttrSpec.Attr)) |
| 2049 | continue; |
| 2050 | |
| 2051 | unsigned ODRFlag = UseODR ? TF_ODR : 0; |
Frederic Riss | 84c09a5 | 2015-02-13 23:18:34 +0000 | [diff] [blame] | 2052 | lookForDIEsToKeep(*RefDIE, DMO, *ReferencedCU, |
Frederic Riss | 1c65094 | 2015-07-21 22:41:43 +0000 | [diff] [blame] | 2053 | TF_Keep | TF_DependencyWalk | ODRFlag); |
| 2054 | } |
Frederic Riss | 84c09a5 | 2015-02-13 23:18:34 +0000 | [diff] [blame] | 2055 | } |
| 2056 | } |
| 2057 | |
| 2058 | /// \brief Recursively walk the \p DIE tree and look for DIEs to |
| 2059 | /// keep. Store that information in \p CU's DIEInfo. |
| 2060 | /// |
| 2061 | /// This function is the entry point of the DIE selection |
| 2062 | /// algorithm. It is expected to walk the DIE tree in file order and |
| 2063 | /// (though the mediation of its helper) call hasValidRelocation() on |
| 2064 | /// each DIE that might be a 'root DIE' (See DwarfLinker class |
| 2065 | /// comment). |
| 2066 | /// While walking the dependencies of root DIEs, this function is |
| 2067 | /// also called, but during these dependency walks the file order is |
| 2068 | /// not respected. The TF_DependencyWalk flag tells us which kind of |
| 2069 | /// traversal we are currently doing. |
| 2070 | void DwarfLinker::lookForDIEsToKeep(const DWARFDebugInfoEntryMinimal &DIE, |
| 2071 | const DebugMapObject &DMO, CompileUnit &CU, |
| 2072 | unsigned Flags) { |
| 2073 | unsigned Idx = CU.getOrigUnit().getDIEIndex(&DIE); |
| 2074 | CompileUnit::DIEInfo &MyInfo = CU.getInfo(Idx); |
| 2075 | bool AlreadyKept = MyInfo.Keep; |
| 2076 | |
| 2077 | // If the Keep flag is set, we are marking a required DIE's |
| 2078 | // dependencies. If our target is already marked as kept, we're all |
| 2079 | // set. |
| 2080 | if ((Flags & TF_DependencyWalk) && AlreadyKept) |
| 2081 | return; |
| 2082 | |
| 2083 | // We must not call shouldKeepDIE while called from keepDIEAndDenpendencies, |
| 2084 | // because it would screw up the relocation finding logic. |
| 2085 | if (!(Flags & TF_DependencyWalk)) |
| 2086 | Flags = shouldKeepDIE(DIE, CU, MyInfo, Flags); |
| 2087 | |
| 2088 | // If it is a newly kept DIE mark it as well as all its dependencies as kept. |
Frederic Riss | 1c65094 | 2015-07-21 22:41:43 +0000 | [diff] [blame] | 2089 | if (!AlreadyKept && (Flags & TF_Keep)) { |
| 2090 | bool UseOdr = (Flags & TF_DependencyWalk) ? (Flags & TF_ODR) : CU.hasODR(); |
| 2091 | keepDIEAndDenpendencies(DIE, MyInfo, DMO, CU, UseOdr); |
| 2092 | } |
Frederic Riss | 84c09a5 | 2015-02-13 23:18:34 +0000 | [diff] [blame] | 2093 | // The TF_ParentWalk flag tells us that we are currently walking up |
| 2094 | // the parent chain of a required DIE, and we don't want to mark all |
| 2095 | // the children of the parents as kept (consider for example a |
| 2096 | // DW_TAG_namespace node in the parent chain). There are however a |
| 2097 | // set of DIE types for which we want to ignore that directive and still |
| 2098 | // walk their children. |
| 2099 | if (dieNeedsChildrenToBeMeaningful(DIE.getTag())) |
| 2100 | Flags &= ~TF_ParentWalk; |
| 2101 | |
| 2102 | if (!DIE.hasChildren() || (Flags & TF_ParentWalk)) |
| 2103 | return; |
| 2104 | |
| 2105 | for (auto *Child = DIE.getFirstChild(); Child && !Child->isNULL(); |
| 2106 | Child = Child->getSibling()) |
| 2107 | lookForDIEsToKeep(*Child, DMO, CU, Flags); |
| 2108 | } |
| 2109 | |
Frederic Riss | b8b43d5 | 2015-03-04 22:07:44 +0000 | [diff] [blame] | 2110 | /// \brief Assign an abbreviation numer to \p Abbrev. |
| 2111 | /// |
| 2112 | /// Our DIEs get freed after every DebugMapObject has been processed, |
| 2113 | /// thus the FoldingSet we use to unique DIEAbbrevs cannot refer to |
| 2114 | /// the instances hold by the DIEs. When we encounter an abbreviation |
| 2115 | /// that we don't know, we create a permanent copy of it. |
| 2116 | void DwarfLinker::AssignAbbrev(DIEAbbrev &Abbrev) { |
| 2117 | // Check the set for priors. |
| 2118 | FoldingSetNodeID ID; |
| 2119 | Abbrev.Profile(ID); |
| 2120 | void *InsertToken; |
| 2121 | DIEAbbrev *InSet = AbbreviationsSet.FindNodeOrInsertPos(ID, InsertToken); |
| 2122 | |
| 2123 | // If it's newly added. |
| 2124 | if (InSet) { |
| 2125 | // Assign existing abbreviation number. |
| 2126 | Abbrev.setNumber(InSet->getNumber()); |
| 2127 | } else { |
| 2128 | // Add to abbreviation list. |
| 2129 | Abbreviations.push_back( |
| 2130 | new DIEAbbrev(Abbrev.getTag(), Abbrev.hasChildren())); |
| 2131 | for (const auto &Attr : Abbrev.getData()) |
| 2132 | Abbreviations.back()->AddAttribute(Attr.getAttribute(), Attr.getForm()); |
| 2133 | AbbreviationsSet.InsertNode(Abbreviations.back(), InsertToken); |
| 2134 | // Assign the unique abbreviation number. |
| 2135 | Abbrev.setNumber(Abbreviations.size()); |
| 2136 | Abbreviations.back()->setNumber(Abbreviations.size()); |
| 2137 | } |
| 2138 | } |
| 2139 | |
| 2140 | /// \brief Clone a string attribute described by \p AttrSpec and add |
| 2141 | /// it to \p Die. |
| 2142 | /// \returns the size of the new attribute. |
Frederic Riss | ef64846 | 2015-03-06 17:56:30 +0000 | [diff] [blame] | 2143 | unsigned DwarfLinker::cloneStringAttribute(DIE &Die, AttributeSpec AttrSpec, |
| 2144 | const DWARFFormValue &Val, |
| 2145 | const DWARFUnit &U) { |
Frederic Riss | b8b43d5 | 2015-03-04 22:07:44 +0000 | [diff] [blame] | 2146 | // Switch everything to out of line strings. |
Frederic Riss | ef64846 | 2015-03-06 17:56:30 +0000 | [diff] [blame] | 2147 | const char *String = *Val.getAsCString(&U); |
| 2148 | unsigned Offset = StringPool.getStringOffset(String); |
Duncan P. N. Exon Smith | 4fb1f9c | 2015-06-25 23:46:41 +0000 | [diff] [blame] | 2149 | Die.addValue(DIEAlloc, dwarf::Attribute(AttrSpec.Attr), dwarf::DW_FORM_strp, |
Duncan P. N. Exon Smith | e7e1d0c | 2015-05-27 22:14:58 +0000 | [diff] [blame] | 2150 | DIEInteger(Offset)); |
Frederic Riss | b8b43d5 | 2015-03-04 22:07:44 +0000 | [diff] [blame] | 2151 | return 4; |
| 2152 | } |
| 2153 | |
| 2154 | /// \brief Clone an attribute referencing another DIE and add |
| 2155 | /// it to \p Die. |
| 2156 | /// \returns the size of the new attribute. |
Frederic Riss | 9833de6 | 2015-03-06 23:22:53 +0000 | [diff] [blame] | 2157 | unsigned DwarfLinker::cloneDieReferenceAttribute( |
| 2158 | DIE &Die, const DWARFDebugInfoEntryMinimal &InputDIE, |
| 2159 | AttributeSpec AttrSpec, unsigned AttrSize, const DWARFFormValue &Val, |
Frederic Riss | 6afcfce | 2015-03-13 18:35:57 +0000 | [diff] [blame] | 2160 | CompileUnit &Unit) { |
Frederic Riss | 1c65094 | 2015-07-21 22:41:43 +0000 | [diff] [blame] | 2161 | const DWARFUnit &U = Unit.getOrigUnit(); |
| 2162 | uint32_t Ref = *Val.getAsReference(&U); |
Frederic Riss | 9833de6 | 2015-03-06 23:22:53 +0000 | [diff] [blame] | 2163 | DIE *NewRefDie = nullptr; |
| 2164 | CompileUnit *RefUnit = nullptr; |
Frederic Riss | 1c65094 | 2015-07-21 22:41:43 +0000 | [diff] [blame] | 2165 | DeclContext *Ctxt = nullptr; |
Frederic Riss | 9833de6 | 2015-03-06 23:22:53 +0000 | [diff] [blame] | 2166 | |
Frederic Riss | 1c65094 | 2015-07-21 22:41:43 +0000 | [diff] [blame] | 2167 | const DWARFDebugInfoEntryMinimal *RefDie = |
| 2168 | resolveDIEReference(Val, U, InputDIE, RefUnit); |
| 2169 | |
| 2170 | // If the referenced DIE is not found, drop the attribute. |
| 2171 | if (!RefDie) |
Frederic Riss | 9833de6 | 2015-03-06 23:22:53 +0000 | [diff] [blame] | 2172 | return 0; |
Frederic Riss | 9833de6 | 2015-03-06 23:22:53 +0000 | [diff] [blame] | 2173 | |
| 2174 | unsigned Idx = RefUnit->getOrigUnit().getDIEIndex(RefDie); |
| 2175 | CompileUnit::DIEInfo &RefInfo = RefUnit->getInfo(Idx); |
Frederic Riss | 1c65094 | 2015-07-21 22:41:43 +0000 | [diff] [blame] | 2176 | |
| 2177 | // If we already have emitted an equivalent DeclContext, just point |
| 2178 | // at it. |
| 2179 | if (isODRAttribute(AttrSpec.Attr)) { |
| 2180 | Ctxt = RefInfo.Ctxt; |
| 2181 | if (Ctxt && Ctxt->getCanonicalDIEOffset()) { |
| 2182 | DIEInteger Attr(Ctxt->getCanonicalDIEOffset()); |
| 2183 | Die.addValue(DIEAlloc, dwarf::Attribute(AttrSpec.Attr), |
| 2184 | dwarf::DW_FORM_ref_addr, Attr); |
| 2185 | return getRefAddrSize(U); |
| 2186 | } |
| 2187 | } |
| 2188 | |
Frederic Riss | 9833de6 | 2015-03-06 23:22:53 +0000 | [diff] [blame] | 2189 | if (!RefInfo.Clone) { |
| 2190 | assert(Ref > InputDIE.getOffset()); |
| 2191 | // We haven't cloned this DIE yet. Just create an empty one and |
| 2192 | // store it. It'll get really cloned when we process it. |
Duncan P. N. Exon Smith | 827200c | 2015-06-25 23:52:10 +0000 | [diff] [blame] | 2193 | RefInfo.Clone = DIE::get(DIEAlloc, dwarf::Tag(RefDie->getTag())); |
Frederic Riss | 9833de6 | 2015-03-06 23:22:53 +0000 | [diff] [blame] | 2194 | } |
| 2195 | NewRefDie = RefInfo.Clone; |
| 2196 | |
Frederic Riss | 1c65094 | 2015-07-21 22:41:43 +0000 | [diff] [blame] | 2197 | if (AttrSpec.Form == dwarf::DW_FORM_ref_addr || |
| 2198 | (Unit.hasODR() && isODRAttribute(AttrSpec.Attr))) { |
Frederic Riss | 9833de6 | 2015-03-06 23:22:53 +0000 | [diff] [blame] | 2199 | // We cannot currently rely on a DIEEntry to emit ref_addr |
| 2200 | // references, because the implementation calls back to DwarfDebug |
| 2201 | // to find the unit offset. (We don't have a DwarfDebug) |
| 2202 | // FIXME: we should be able to design DIEEntry reliance on |
| 2203 | // DwarfDebug away. |
Duncan P. N. Exon Smith | e7e1d0c | 2015-05-27 22:14:58 +0000 | [diff] [blame] | 2204 | uint64_t Attr; |
Frederic Riss | 9833de6 | 2015-03-06 23:22:53 +0000 | [diff] [blame] | 2205 | if (Ref < InputDIE.getOffset()) { |
| 2206 | // We must have already cloned that DIE. |
| 2207 | uint32_t NewRefOffset = |
| 2208 | RefUnit->getStartOffset() + NewRefDie->getOffset(); |
Duncan P. N. Exon Smith | e7e1d0c | 2015-05-27 22:14:58 +0000 | [diff] [blame] | 2209 | Attr = NewRefOffset; |
Duncan P. N. Exon Smith | 4fb1f9c | 2015-06-25 23:46:41 +0000 | [diff] [blame] | 2210 | Die.addValue(DIEAlloc, dwarf::Attribute(AttrSpec.Attr), |
| 2211 | dwarf::DW_FORM_ref_addr, DIEInteger(Attr)); |
Frederic Riss | 9833de6 | 2015-03-06 23:22:53 +0000 | [diff] [blame] | 2212 | } else { |
| 2213 | // A forward reference. Note and fixup later. |
Duncan P. N. Exon Smith | e7e1d0c | 2015-05-27 22:14:58 +0000 | [diff] [blame] | 2214 | Attr = 0xBADDEF; |
Duncan P. N. Exon Smith | 4fb1f9c | 2015-06-25 23:46:41 +0000 | [diff] [blame] | 2215 | Unit.noteForwardReference( |
Frederic Riss | 1c65094 | 2015-07-21 22:41:43 +0000 | [diff] [blame] | 2216 | NewRefDie, RefUnit, Ctxt, |
Duncan P. N. Exon Smith | 4fb1f9c | 2015-06-25 23:46:41 +0000 | [diff] [blame] | 2217 | Die.addValue(DIEAlloc, dwarf::Attribute(AttrSpec.Attr), |
| 2218 | dwarf::DW_FORM_ref_addr, DIEInteger(Attr))); |
Frederic Riss | 9833de6 | 2015-03-06 23:22:53 +0000 | [diff] [blame] | 2219 | } |
Frederic Riss | 1c65094 | 2015-07-21 22:41:43 +0000 | [diff] [blame] | 2220 | return getRefAddrSize(U); |
Frederic Riss | 9833de6 | 2015-03-06 23:22:53 +0000 | [diff] [blame] | 2221 | } |
| 2222 | |
Duncan P. N. Exon Smith | 4fb1f9c | 2015-06-25 23:46:41 +0000 | [diff] [blame] | 2223 | Die.addValue(DIEAlloc, dwarf::Attribute(AttrSpec.Attr), |
| 2224 | dwarf::Form(AttrSpec.Form), DIEEntry(*NewRefDie)); |
Frederic Riss | b8b43d5 | 2015-03-04 22:07:44 +0000 | [diff] [blame] | 2225 | return AttrSize; |
| 2226 | } |
| 2227 | |
| 2228 | /// \brief Clone an attribute of block form (locations, constants) and add |
| 2229 | /// it to \p Die. |
| 2230 | /// \returns the size of the new attribute. |
| 2231 | unsigned DwarfLinker::cloneBlockAttribute(DIE &Die, AttributeSpec AttrSpec, |
| 2232 | const DWARFFormValue &Val, |
| 2233 | unsigned AttrSize) { |
Duncan P. N. Exon Smith | af9bb0f | 2015-08-02 20:48:47 +0000 | [diff] [blame] | 2234 | DIEValueList *Attr; |
Duncan P. N. Exon Smith | e7e1d0c | 2015-05-27 22:14:58 +0000 | [diff] [blame] | 2235 | DIEValue Value; |
Frederic Riss | b8b43d5 | 2015-03-04 22:07:44 +0000 | [diff] [blame] | 2236 | DIELoc *Loc = nullptr; |
| 2237 | DIEBlock *Block = nullptr; |
| 2238 | // Just copy the block data over. |
Frederic Riss | 111a0a8 | 2015-03-13 18:35:39 +0000 | [diff] [blame] | 2239 | if (AttrSpec.Form == dwarf::DW_FORM_exprloc) { |
Duncan P. N. Exon Smith | e7e1d0c | 2015-05-27 22:14:58 +0000 | [diff] [blame] | 2240 | Loc = new (DIEAlloc) DIELoc; |
Frederic Riss | b8b43d5 | 2015-03-04 22:07:44 +0000 | [diff] [blame] | 2241 | DIELocs.push_back(Loc); |
| 2242 | } else { |
Duncan P. N. Exon Smith | e7e1d0c | 2015-05-27 22:14:58 +0000 | [diff] [blame] | 2243 | Block = new (DIEAlloc) DIEBlock; |
Frederic Riss | b8b43d5 | 2015-03-04 22:07:44 +0000 | [diff] [blame] | 2244 | DIEBlocks.push_back(Block); |
| 2245 | } |
Duncan P. N. Exon Smith | af9bb0f | 2015-08-02 20:48:47 +0000 | [diff] [blame] | 2246 | Attr = Loc ? static_cast<DIEValueList *>(Loc) |
| 2247 | : static_cast<DIEValueList *>(Block); |
Duncan P. N. Exon Smith | 815a6eb5 | 2015-05-27 22:31:41 +0000 | [diff] [blame] | 2248 | |
| 2249 | if (Loc) |
| 2250 | Value = DIEValue(dwarf::Attribute(AttrSpec.Attr), |
| 2251 | dwarf::Form(AttrSpec.Form), Loc); |
| 2252 | else |
| 2253 | Value = DIEValue(dwarf::Attribute(AttrSpec.Attr), |
| 2254 | dwarf::Form(AttrSpec.Form), Block); |
Frederic Riss | b8b43d5 | 2015-03-04 22:07:44 +0000 | [diff] [blame] | 2255 | ArrayRef<uint8_t> Bytes = *Val.getAsBlock(); |
| 2256 | for (auto Byte : Bytes) |
Duncan P. N. Exon Smith | 4fb1f9c | 2015-06-25 23:46:41 +0000 | [diff] [blame] | 2257 | Attr->addValue(DIEAlloc, static_cast<dwarf::Attribute>(0), |
| 2258 | dwarf::DW_FORM_data1, DIEInteger(Byte)); |
Frederic Riss | b8b43d5 | 2015-03-04 22:07:44 +0000 | [diff] [blame] | 2259 | // FIXME: If DIEBlock and DIELoc just reuses the Size field of |
| 2260 | // the DIE class, this if could be replaced by |
| 2261 | // Attr->setSize(Bytes.size()). |
| 2262 | if (Streamer) { |
| 2263 | if (Loc) |
| 2264 | Loc->ComputeSize(&Streamer->getAsmPrinter()); |
| 2265 | else |
| 2266 | Block->ComputeSize(&Streamer->getAsmPrinter()); |
| 2267 | } |
Duncan P. N. Exon Smith | 4fb1f9c | 2015-06-25 23:46:41 +0000 | [diff] [blame] | 2268 | Die.addValue(DIEAlloc, Value); |
Frederic Riss | b8b43d5 | 2015-03-04 22:07:44 +0000 | [diff] [blame] | 2269 | return AttrSize; |
| 2270 | } |
| 2271 | |
Frederic Riss | 31da324 | 2015-03-11 18:45:52 +0000 | [diff] [blame] | 2272 | /// \brief Clone an address attribute and add it to \p Die. |
| 2273 | /// \returns the size of the new attribute. |
| 2274 | unsigned DwarfLinker::cloneAddressAttribute(DIE &Die, AttributeSpec AttrSpec, |
| 2275 | const DWARFFormValue &Val, |
| 2276 | const CompileUnit &Unit, |
| 2277 | AttributesInfo &Info) { |
Frederic Riss | 5a62dc3 | 2015-03-13 18:35:54 +0000 | [diff] [blame] | 2278 | uint64_t Addr = *Val.getAsAddress(&Unit.getOrigUnit()); |
Frederic Riss | 31da324 | 2015-03-11 18:45:52 +0000 | [diff] [blame] | 2279 | if (AttrSpec.Attr == dwarf::DW_AT_low_pc) { |
| 2280 | if (Die.getTag() == dwarf::DW_TAG_inlined_subroutine || |
| 2281 | Die.getTag() == dwarf::DW_TAG_lexical_block) |
Frederic Riss | 7b5563a | 2015-08-31 01:43:14 +0000 | [diff] [blame] | 2282 | // The low_pc of a block or inline subroutine might get |
| 2283 | // relocated because it happens to match the low_pc of the |
| 2284 | // enclosing subprogram. To prevent issues with that, always use |
| 2285 | // the low_pc from the input DIE if relocations have been applied. |
| 2286 | Addr = (Info.OrigLowPc != UINT64_MAX ? Info.OrigLowPc : Addr) + |
| 2287 | Info.PCOffset; |
Frederic Riss | 5a62dc3 | 2015-03-13 18:35:54 +0000 | [diff] [blame] | 2288 | else if (Die.getTag() == dwarf::DW_TAG_compile_unit) { |
| 2289 | Addr = Unit.getLowPc(); |
| 2290 | if (Addr == UINT64_MAX) |
| 2291 | return 0; |
| 2292 | } |
Frederic Riss | bce93ff | 2015-03-16 02:05:10 +0000 | [diff] [blame] | 2293 | Info.HasLowPc = true; |
Frederic Riss | 31da324 | 2015-03-11 18:45:52 +0000 | [diff] [blame] | 2294 | } else if (AttrSpec.Attr == dwarf::DW_AT_high_pc) { |
Frederic Riss | 5a62dc3 | 2015-03-13 18:35:54 +0000 | [diff] [blame] | 2295 | if (Die.getTag() == dwarf::DW_TAG_compile_unit) { |
| 2296 | if (uint64_t HighPc = Unit.getHighPc()) |
| 2297 | Addr = HighPc; |
| 2298 | else |
| 2299 | return 0; |
| 2300 | } else |
| 2301 | // If we have a high_pc recorded for the input DIE, use |
| 2302 | // it. Otherwise (when no relocations where applied) just use the |
| 2303 | // one we just decoded. |
| 2304 | Addr = (Info.OrigHighPc ? Info.OrigHighPc : Addr) + Info.PCOffset; |
Frederic Riss | 31da324 | 2015-03-11 18:45:52 +0000 | [diff] [blame] | 2305 | } |
| 2306 | |
Duncan P. N. Exon Smith | 4fb1f9c | 2015-06-25 23:46:41 +0000 | [diff] [blame] | 2307 | Die.addValue(DIEAlloc, static_cast<dwarf::Attribute>(AttrSpec.Attr), |
Duncan P. N. Exon Smith | e7e1d0c | 2015-05-27 22:14:58 +0000 | [diff] [blame] | 2308 | static_cast<dwarf::Form>(AttrSpec.Form), DIEInteger(Addr)); |
Frederic Riss | 31da324 | 2015-03-11 18:45:52 +0000 | [diff] [blame] | 2309 | return Unit.getOrigUnit().getAddressByteSize(); |
| 2310 | } |
| 2311 | |
Frederic Riss | b8b43d5 | 2015-03-04 22:07:44 +0000 | [diff] [blame] | 2312 | /// \brief Clone a scalar attribute and add it to \p Die. |
| 2313 | /// \returns the size of the new attribute. |
| 2314 | unsigned DwarfLinker::cloneScalarAttribute( |
Frederic Riss | 2544087 | 2015-03-13 23:30:31 +0000 | [diff] [blame] | 2315 | DIE &Die, const DWARFDebugInfoEntryMinimal &InputDIE, CompileUnit &Unit, |
Frederic Riss | dfb9790 | 2015-03-14 15:49:07 +0000 | [diff] [blame] | 2316 | AttributeSpec AttrSpec, const DWARFFormValue &Val, unsigned AttrSize, |
Frederic Riss | bce93ff | 2015-03-16 02:05:10 +0000 | [diff] [blame] | 2317 | AttributesInfo &Info) { |
Frederic Riss | b8b43d5 | 2015-03-04 22:07:44 +0000 | [diff] [blame] | 2318 | uint64_t Value; |
Frederic Riss | 5a62dc3 | 2015-03-13 18:35:54 +0000 | [diff] [blame] | 2319 | if (AttrSpec.Attr == dwarf::DW_AT_high_pc && |
| 2320 | Die.getTag() == dwarf::DW_TAG_compile_unit) { |
| 2321 | if (Unit.getLowPc() == -1ULL) |
| 2322 | return 0; |
| 2323 | // Dwarf >= 4 high_pc is an size, not an address. |
| 2324 | Value = Unit.getHighPc() - Unit.getLowPc(); |
| 2325 | } else if (AttrSpec.Form == dwarf::DW_FORM_sec_offset) |
Frederic Riss | b8b43d5 | 2015-03-04 22:07:44 +0000 | [diff] [blame] | 2326 | Value = *Val.getAsSectionOffset(); |
| 2327 | else if (AttrSpec.Form == dwarf::DW_FORM_sdata) |
| 2328 | Value = *Val.getAsSignedConstant(); |
Frederic Riss | b8b43d5 | 2015-03-04 22:07:44 +0000 | [diff] [blame] | 2329 | else if (auto OptionalValue = Val.getAsUnsignedConstant()) |
| 2330 | Value = *OptionalValue; |
| 2331 | else { |
Frederic Riss | 5a62dc3 | 2015-03-13 18:35:54 +0000 | [diff] [blame] | 2332 | reportWarning("Unsupported scalar attribute form. Dropping attribute.", |
| 2333 | &Unit.getOrigUnit(), &InputDIE); |
Frederic Riss | b8b43d5 | 2015-03-04 22:07:44 +0000 | [diff] [blame] | 2334 | return 0; |
| 2335 | } |
Duncan P. N. Exon Smith | 4fb1f9c | 2015-06-25 23:46:41 +0000 | [diff] [blame] | 2336 | PatchLocation Patch = |
| 2337 | Die.addValue(DIEAlloc, dwarf::Attribute(AttrSpec.Attr), |
| 2338 | dwarf::Form(AttrSpec.Form), DIEInteger(Value)); |
Frederic Riss | 2544087 | 2015-03-13 23:30:31 +0000 | [diff] [blame] | 2339 | if (AttrSpec.Attr == dwarf::DW_AT_ranges) |
Duncan P. N. Exon Smith | 4fb1f9c | 2015-06-25 23:46:41 +0000 | [diff] [blame] | 2340 | Unit.noteRangeAttribute(Die, Patch); |
Frederic Riss | dfb9790 | 2015-03-14 15:49:07 +0000 | [diff] [blame] | 2341 | // A more generic way to check for location attributes would be |
| 2342 | // nice, but it's very unlikely that any other attribute needs a |
| 2343 | // location list. |
| 2344 | else if (AttrSpec.Attr == dwarf::DW_AT_location || |
| 2345 | AttrSpec.Attr == dwarf::DW_AT_frame_base) |
Duncan P. N. Exon Smith | 4fb1f9c | 2015-06-25 23:46:41 +0000 | [diff] [blame] | 2346 | Unit.noteLocationAttribute(Patch, Info.PCOffset); |
Frederic Riss | bce93ff | 2015-03-16 02:05:10 +0000 | [diff] [blame] | 2347 | else if (AttrSpec.Attr == dwarf::DW_AT_declaration && Value) |
| 2348 | Info.IsDeclaration = true; |
Frederic Riss | dfb9790 | 2015-03-14 15:49:07 +0000 | [diff] [blame] | 2349 | |
Frederic Riss | b8b43d5 | 2015-03-04 22:07:44 +0000 | [diff] [blame] | 2350 | return AttrSize; |
| 2351 | } |
| 2352 | |
| 2353 | /// \brief Clone \p InputDIE's attribute described by \p AttrSpec with |
| 2354 | /// value \p Val, and add it to \p Die. |
| 2355 | /// \returns the size of the cloned attribute. |
| 2356 | unsigned DwarfLinker::cloneAttribute(DIE &Die, |
| 2357 | const DWARFDebugInfoEntryMinimal &InputDIE, |
| 2358 | CompileUnit &Unit, |
| 2359 | const DWARFFormValue &Val, |
| 2360 | const AttributeSpec AttrSpec, |
Frederic Riss | 31da324 | 2015-03-11 18:45:52 +0000 | [diff] [blame] | 2361 | unsigned AttrSize, AttributesInfo &Info) { |
Frederic Riss | b8b43d5 | 2015-03-04 22:07:44 +0000 | [diff] [blame] | 2362 | const DWARFUnit &U = Unit.getOrigUnit(); |
| 2363 | |
| 2364 | switch (AttrSpec.Form) { |
| 2365 | case dwarf::DW_FORM_strp: |
| 2366 | case dwarf::DW_FORM_string: |
Frederic Riss | ef64846 | 2015-03-06 17:56:30 +0000 | [diff] [blame] | 2367 | return cloneStringAttribute(Die, AttrSpec, Val, U); |
Frederic Riss | b8b43d5 | 2015-03-04 22:07:44 +0000 | [diff] [blame] | 2368 | case dwarf::DW_FORM_ref_addr: |
| 2369 | case dwarf::DW_FORM_ref1: |
| 2370 | case dwarf::DW_FORM_ref2: |
| 2371 | case dwarf::DW_FORM_ref4: |
| 2372 | case dwarf::DW_FORM_ref8: |
Frederic Riss | 9833de6 | 2015-03-06 23:22:53 +0000 | [diff] [blame] | 2373 | return cloneDieReferenceAttribute(Die, InputDIE, AttrSpec, AttrSize, Val, |
Frederic Riss | 6afcfce | 2015-03-13 18:35:57 +0000 | [diff] [blame] | 2374 | Unit); |
Frederic Riss | b8b43d5 | 2015-03-04 22:07:44 +0000 | [diff] [blame] | 2375 | case dwarf::DW_FORM_block: |
| 2376 | case dwarf::DW_FORM_block1: |
| 2377 | case dwarf::DW_FORM_block2: |
| 2378 | case dwarf::DW_FORM_block4: |
| 2379 | case dwarf::DW_FORM_exprloc: |
| 2380 | return cloneBlockAttribute(Die, AttrSpec, Val, AttrSize); |
| 2381 | case dwarf::DW_FORM_addr: |
Frederic Riss | 31da324 | 2015-03-11 18:45:52 +0000 | [diff] [blame] | 2382 | return cloneAddressAttribute(Die, AttrSpec, Val, Unit, Info); |
Frederic Riss | b8b43d5 | 2015-03-04 22:07:44 +0000 | [diff] [blame] | 2383 | case dwarf::DW_FORM_data1: |
| 2384 | case dwarf::DW_FORM_data2: |
| 2385 | case dwarf::DW_FORM_data4: |
| 2386 | case dwarf::DW_FORM_data8: |
| 2387 | case dwarf::DW_FORM_udata: |
| 2388 | case dwarf::DW_FORM_sdata: |
| 2389 | case dwarf::DW_FORM_sec_offset: |
| 2390 | case dwarf::DW_FORM_flag: |
| 2391 | case dwarf::DW_FORM_flag_present: |
Frederic Riss | dfb9790 | 2015-03-14 15:49:07 +0000 | [diff] [blame] | 2392 | return cloneScalarAttribute(Die, InputDIE, Unit, AttrSpec, Val, AttrSize, |
| 2393 | Info); |
Frederic Riss | b8b43d5 | 2015-03-04 22:07:44 +0000 | [diff] [blame] | 2394 | default: |
| 2395 | reportWarning("Unsupported attribute form in cloneAttribute. Dropping.", &U, |
| 2396 | &InputDIE); |
| 2397 | } |
| 2398 | |
| 2399 | return 0; |
| 2400 | } |
| 2401 | |
Frederic Riss | 23e20e9 | 2015-03-07 01:25:09 +0000 | [diff] [blame] | 2402 | /// \brief Apply the valid relocations found by findValidRelocs() to |
| 2403 | /// the buffer \p Data, taking into account that Data is at \p BaseOffset |
| 2404 | /// in the debug_info section. |
| 2405 | /// |
| 2406 | /// Like for findValidRelocs(), this function must be called with |
| 2407 | /// monotonic \p BaseOffset values. |
| 2408 | /// |
| 2409 | /// \returns wether any reloc has been applied. |
| 2410 | bool DwarfLinker::applyValidRelocs(MutableArrayRef<char> Data, |
| 2411 | uint32_t BaseOffset, bool isLittleEndian) { |
Aaron Ballman | 6b329f5 | 2015-03-07 15:16:27 +0000 | [diff] [blame] | 2412 | assert((NextValidReloc == 0 || |
Frederic Riss | aa983ce | 2015-03-11 18:45:57 +0000 | [diff] [blame] | 2413 | BaseOffset > ValidRelocs[NextValidReloc - 1].Offset) && |
| 2414 | "BaseOffset should only be increasing."); |
Frederic Riss | 23e20e9 | 2015-03-07 01:25:09 +0000 | [diff] [blame] | 2415 | if (NextValidReloc >= ValidRelocs.size()) |
| 2416 | return false; |
| 2417 | |
| 2418 | // Skip relocs that haven't been applied. |
| 2419 | while (NextValidReloc < ValidRelocs.size() && |
| 2420 | ValidRelocs[NextValidReloc].Offset < BaseOffset) |
| 2421 | ++NextValidReloc; |
| 2422 | |
| 2423 | bool Applied = false; |
| 2424 | uint64_t EndOffset = BaseOffset + Data.size(); |
| 2425 | while (NextValidReloc < ValidRelocs.size() && |
| 2426 | ValidRelocs[NextValidReloc].Offset >= BaseOffset && |
| 2427 | ValidRelocs[NextValidReloc].Offset < EndOffset) { |
| 2428 | const auto &ValidReloc = ValidRelocs[NextValidReloc++]; |
| 2429 | assert(ValidReloc.Offset - BaseOffset < Data.size()); |
| 2430 | assert(ValidReloc.Offset - BaseOffset + ValidReloc.Size <= Data.size()); |
| 2431 | char Buf[8]; |
| 2432 | uint64_t Value = ValidReloc.Mapping->getValue().BinaryAddress; |
| 2433 | Value += ValidReloc.Addend; |
| 2434 | for (unsigned i = 0; i != ValidReloc.Size; ++i) { |
| 2435 | unsigned Index = isLittleEndian ? i : (ValidReloc.Size - i - 1); |
| 2436 | Buf[i] = uint8_t(Value >> (Index * 8)); |
| 2437 | } |
| 2438 | assert(ValidReloc.Size <= sizeof(Buf)); |
| 2439 | memcpy(&Data[ValidReloc.Offset - BaseOffset], Buf, ValidReloc.Size); |
| 2440 | Applied = true; |
| 2441 | } |
| 2442 | |
| 2443 | return Applied; |
| 2444 | } |
| 2445 | |
Frederic Riss | bce93ff | 2015-03-16 02:05:10 +0000 | [diff] [blame] | 2446 | static bool isTypeTag(uint16_t Tag) { |
| 2447 | switch (Tag) { |
| 2448 | case dwarf::DW_TAG_array_type: |
| 2449 | case dwarf::DW_TAG_class_type: |
| 2450 | case dwarf::DW_TAG_enumeration_type: |
| 2451 | case dwarf::DW_TAG_pointer_type: |
| 2452 | case dwarf::DW_TAG_reference_type: |
| 2453 | case dwarf::DW_TAG_string_type: |
| 2454 | case dwarf::DW_TAG_structure_type: |
| 2455 | case dwarf::DW_TAG_subroutine_type: |
| 2456 | case dwarf::DW_TAG_typedef: |
| 2457 | case dwarf::DW_TAG_union_type: |
| 2458 | case dwarf::DW_TAG_ptr_to_member_type: |
| 2459 | case dwarf::DW_TAG_set_type: |
| 2460 | case dwarf::DW_TAG_subrange_type: |
| 2461 | case dwarf::DW_TAG_base_type: |
| 2462 | case dwarf::DW_TAG_const_type: |
| 2463 | case dwarf::DW_TAG_constant: |
| 2464 | case dwarf::DW_TAG_file_type: |
| 2465 | case dwarf::DW_TAG_namelist: |
| 2466 | case dwarf::DW_TAG_packed_type: |
| 2467 | case dwarf::DW_TAG_volatile_type: |
| 2468 | case dwarf::DW_TAG_restrict_type: |
| 2469 | case dwarf::DW_TAG_interface_type: |
| 2470 | case dwarf::DW_TAG_unspecified_type: |
| 2471 | case dwarf::DW_TAG_shared_type: |
| 2472 | return true; |
| 2473 | default: |
| 2474 | break; |
| 2475 | } |
| 2476 | return false; |
| 2477 | } |
| 2478 | |
Frederic Riss | b8b43d5 | 2015-03-04 22:07:44 +0000 | [diff] [blame] | 2479 | /// \brief Recursively clone \p InputDIE's subtrees that have been |
| 2480 | /// selected to appear in the linked output. |
| 2481 | /// |
| 2482 | /// \param OutOffset is the Offset where the newly created DIE will |
| 2483 | /// lie in the linked compile unit. |
| 2484 | /// |
| 2485 | /// \returns the cloned DIE object or null if nothing was selected. |
| 2486 | DIE *DwarfLinker::cloneDIE(const DWARFDebugInfoEntryMinimal &InputDIE, |
Frederic Riss | 31da324 | 2015-03-11 18:45:52 +0000 | [diff] [blame] | 2487 | CompileUnit &Unit, int64_t PCOffset, |
| 2488 | uint32_t OutOffset) { |
Frederic Riss | b8b43d5 | 2015-03-04 22:07:44 +0000 | [diff] [blame] | 2489 | DWARFUnit &U = Unit.getOrigUnit(); |
| 2490 | unsigned Idx = U.getDIEIndex(&InputDIE); |
Frederic Riss | 9833de6 | 2015-03-06 23:22:53 +0000 | [diff] [blame] | 2491 | CompileUnit::DIEInfo &Info = Unit.getInfo(Idx); |
Frederic Riss | b8b43d5 | 2015-03-04 22:07:44 +0000 | [diff] [blame] | 2492 | |
| 2493 | // Should the DIE appear in the output? |
| 2494 | if (!Unit.getInfo(Idx).Keep) |
| 2495 | return nullptr; |
| 2496 | |
| 2497 | uint32_t Offset = InputDIE.getOffset(); |
Frederic Riss | 9833de6 | 2015-03-06 23:22:53 +0000 | [diff] [blame] | 2498 | // The DIE might have been already created by a forward reference |
| 2499 | // (see cloneDieReferenceAttribute()). |
| 2500 | DIE *Die = Info.Clone; |
| 2501 | if (!Die) |
Duncan P. N. Exon Smith | 827200c | 2015-06-25 23:52:10 +0000 | [diff] [blame] | 2502 | Die = Info.Clone = DIE::get(DIEAlloc, dwarf::Tag(InputDIE.getTag())); |
Frederic Riss | 9833de6 | 2015-03-06 23:22:53 +0000 | [diff] [blame] | 2503 | assert(Die->getTag() == InputDIE.getTag()); |
Frederic Riss | b8b43d5 | 2015-03-04 22:07:44 +0000 | [diff] [blame] | 2504 | Die->setOffset(OutOffset); |
Frederic Riss | 1c65094 | 2015-07-21 22:41:43 +0000 | [diff] [blame] | 2505 | if (Unit.hasODR() && Die->getTag() != dwarf::DW_TAG_namespace && Info.Ctxt && |
| 2506 | Info.Ctxt != Unit.getInfo(Info.ParentIdx).Ctxt && |
| 2507 | !Info.Ctxt->getCanonicalDIEOffset()) { |
| 2508 | // We are about to emit a DIE that is the root of its own valid |
| 2509 | // DeclContext tree. Make the current offset the canonical offset |
| 2510 | // for this context. |
| 2511 | Info.Ctxt->setCanonicalDIEOffset(OutOffset + Unit.getStartOffset()); |
| 2512 | } |
Frederic Riss | b8b43d5 | 2015-03-04 22:07:44 +0000 | [diff] [blame] | 2513 | |
| 2514 | // Extract and clone every attribute. |
| 2515 | DataExtractor Data = U.getDebugInfoExtractor(); |
Frederic Riss | 23e20e9 | 2015-03-07 01:25:09 +0000 | [diff] [blame] | 2516 | uint32_t NextOffset = U.getDIEAtIndex(Idx + 1)->getOffset(); |
Frederic Riss | 31da324 | 2015-03-11 18:45:52 +0000 | [diff] [blame] | 2517 | AttributesInfo AttrInfo; |
Frederic Riss | 23e20e9 | 2015-03-07 01:25:09 +0000 | [diff] [blame] | 2518 | |
| 2519 | // We could copy the data only if we need to aply a relocation to |
| 2520 | // it. After testing, it seems there is no performance downside to |
| 2521 | // doing the copy unconditionally, and it makes the code simpler. |
| 2522 | SmallString<40> DIECopy(Data.getData().substr(Offset, NextOffset - Offset)); |
| 2523 | Data = DataExtractor(DIECopy, Data.isLittleEndian(), Data.getAddressSize()); |
| 2524 | // Modify the copy with relocated addresses. |
Frederic Riss | 31da324 | 2015-03-11 18:45:52 +0000 | [diff] [blame] | 2525 | if (applyValidRelocs(DIECopy, Offset, Data.isLittleEndian())) { |
| 2526 | // If we applied relocations, we store the value of high_pc that was |
| 2527 | // potentially stored in the input DIE. If high_pc is an address |
| 2528 | // (Dwarf version == 2), then it might have been relocated to a |
| 2529 | // totally unrelated value (because the end address in the object |
| 2530 | // file might be start address of another function which got moved |
| 2531 | // independantly by the linker). The computation of the actual |
| 2532 | // high_pc value is done in cloneAddressAttribute(). |
| 2533 | AttrInfo.OrigHighPc = |
| 2534 | InputDIE.getAttributeValueAsAddress(&U, dwarf::DW_AT_high_pc, 0); |
Frederic Riss | 7b5563a | 2015-08-31 01:43:14 +0000 | [diff] [blame] | 2535 | // Also store the low_pc. It might get relocated in an |
| 2536 | // inline_subprogram that happens at the beginning of its |
| 2537 | // inlining function. |
| 2538 | AttrInfo.OrigLowPc = |
| 2539 | InputDIE.getAttributeValueAsAddress(&U, dwarf::DW_AT_low_pc, UINT64_MAX); |
Frederic Riss | 31da324 | 2015-03-11 18:45:52 +0000 | [diff] [blame] | 2540 | } |
Frederic Riss | 23e20e9 | 2015-03-07 01:25:09 +0000 | [diff] [blame] | 2541 | |
| 2542 | // Reset the Offset to 0 as we will be working on the local copy of |
| 2543 | // the data. |
| 2544 | Offset = 0; |
| 2545 | |
Frederic Riss | b8b43d5 | 2015-03-04 22:07:44 +0000 | [diff] [blame] | 2546 | const auto *Abbrev = InputDIE.getAbbreviationDeclarationPtr(); |
| 2547 | Offset += getULEB128Size(Abbrev->getCode()); |
| 2548 | |
Frederic Riss | 31da324 | 2015-03-11 18:45:52 +0000 | [diff] [blame] | 2549 | // We are entering a subprogram. Get and propagate the PCOffset. |
| 2550 | if (Die->getTag() == dwarf::DW_TAG_subprogram) |
| 2551 | PCOffset = Info.AddrAdjust; |
| 2552 | AttrInfo.PCOffset = PCOffset; |
| 2553 | |
Frederic Riss | b8b43d5 | 2015-03-04 22:07:44 +0000 | [diff] [blame] | 2554 | for (const auto &AttrSpec : Abbrev->attributes()) { |
| 2555 | DWARFFormValue Val(AttrSpec.Form); |
| 2556 | uint32_t AttrSize = Offset; |
| 2557 | Val.extractValue(Data, &Offset, &U); |
| 2558 | AttrSize = Offset - AttrSize; |
| 2559 | |
Frederic Riss | 31da324 | 2015-03-11 18:45:52 +0000 | [diff] [blame] | 2560 | OutOffset += |
| 2561 | cloneAttribute(*Die, InputDIE, Unit, Val, AttrSpec, AttrSize, AttrInfo); |
Frederic Riss | b8b43d5 | 2015-03-04 22:07:44 +0000 | [diff] [blame] | 2562 | } |
| 2563 | |
Frederic Riss | bce93ff | 2015-03-16 02:05:10 +0000 | [diff] [blame] | 2564 | // Look for accelerator entries. |
| 2565 | uint16_t Tag = InputDIE.getTag(); |
| 2566 | // FIXME: This is slightly wrong. An inline_subroutine without a |
| 2567 | // low_pc, but with AT_ranges might be interesting to get into the |
| 2568 | // accelerator tables too. For now stick with dsymutil's behavior. |
| 2569 | if ((Info.InDebugMap || AttrInfo.HasLowPc) && |
| 2570 | Tag != dwarf::DW_TAG_compile_unit && |
| 2571 | getDIENames(InputDIE, Unit.getOrigUnit(), AttrInfo)) { |
| 2572 | if (AttrInfo.MangledName && AttrInfo.MangledName != AttrInfo.Name) |
| 2573 | Unit.addNameAccelerator(Die, AttrInfo.MangledName, |
| 2574 | AttrInfo.MangledNameOffset, |
| 2575 | Tag == dwarf::DW_TAG_inlined_subroutine); |
| 2576 | if (AttrInfo.Name) |
| 2577 | Unit.addNameAccelerator(Die, AttrInfo.Name, AttrInfo.NameOffset, |
| 2578 | Tag == dwarf::DW_TAG_inlined_subroutine); |
| 2579 | } else if (isTypeTag(Tag) && !AttrInfo.IsDeclaration && |
| 2580 | getDIENames(InputDIE, Unit.getOrigUnit(), AttrInfo)) { |
| 2581 | Unit.addTypeAccelerator(Die, AttrInfo.Name, AttrInfo.NameOffset); |
| 2582 | } |
| 2583 | |
Duncan P. N. Exon Smith | 815a6eb5 | 2015-05-27 22:31:41 +0000 | [diff] [blame] | 2584 | DIEAbbrev NewAbbrev = Die->generateAbbrev(); |
Frederic Riss | b8b43d5 | 2015-03-04 22:07:44 +0000 | [diff] [blame] | 2585 | // If a scope DIE is kept, we must have kept at least one child. If |
| 2586 | // it's not the case, we'll just be emitting one wasteful end of |
| 2587 | // children marker, but things won't break. |
| 2588 | if (InputDIE.hasChildren()) |
| 2589 | NewAbbrev.setChildrenFlag(dwarf::DW_CHILDREN_yes); |
| 2590 | // Assign a permanent abbrev number |
Duncan P. N. Exon Smith | 815a6eb5 | 2015-05-27 22:31:41 +0000 | [diff] [blame] | 2591 | AssignAbbrev(NewAbbrev); |
| 2592 | Die->setAbbrevNumber(NewAbbrev.getNumber()); |
Frederic Riss | b8b43d5 | 2015-03-04 22:07:44 +0000 | [diff] [blame] | 2593 | |
| 2594 | // Add the size of the abbreviation number to the output offset. |
| 2595 | OutOffset += getULEB128Size(Die->getAbbrevNumber()); |
| 2596 | |
| 2597 | if (!Abbrev->hasChildren()) { |
| 2598 | // Update our size. |
| 2599 | Die->setSize(OutOffset - Die->getOffset()); |
| 2600 | return Die; |
| 2601 | } |
| 2602 | |
| 2603 | // Recursively clone children. |
| 2604 | for (auto *Child = InputDIE.getFirstChild(); Child && !Child->isNULL(); |
| 2605 | Child = Child->getSibling()) { |
Frederic Riss | 31da324 | 2015-03-11 18:45:52 +0000 | [diff] [blame] | 2606 | if (DIE *Clone = cloneDIE(*Child, Unit, PCOffset, OutOffset)) { |
Duncan P. N. Exon Smith | 827200c | 2015-06-25 23:52:10 +0000 | [diff] [blame] | 2607 | Die->addChild(Clone); |
Frederic Riss | b8b43d5 | 2015-03-04 22:07:44 +0000 | [diff] [blame] | 2608 | OutOffset = Clone->getOffset() + Clone->getSize(); |
| 2609 | } |
| 2610 | } |
| 2611 | |
| 2612 | // Account for the end of children marker. |
| 2613 | OutOffset += sizeof(int8_t); |
| 2614 | // Update our size. |
| 2615 | Die->setSize(OutOffset - Die->getOffset()); |
| 2616 | return Die; |
| 2617 | } |
| 2618 | |
Frederic Riss | 2544087 | 2015-03-13 23:30:31 +0000 | [diff] [blame] | 2619 | /// \brief Patch the input object file relevant debug_ranges entries |
| 2620 | /// and emit them in the output file. Update the relevant attributes |
| 2621 | /// to point at the new entries. |
| 2622 | void DwarfLinker::patchRangesForUnit(const CompileUnit &Unit, |
| 2623 | DWARFContext &OrigDwarf) const { |
| 2624 | DWARFDebugRangeList RangeList; |
| 2625 | const auto &FunctionRanges = Unit.getFunctionRanges(); |
| 2626 | unsigned AddressSize = Unit.getOrigUnit().getAddressByteSize(); |
| 2627 | DataExtractor RangeExtractor(OrigDwarf.getRangeSection(), |
| 2628 | OrigDwarf.isLittleEndian(), AddressSize); |
| 2629 | auto InvalidRange = FunctionRanges.end(), CurrRange = InvalidRange; |
| 2630 | DWARFUnit &OrigUnit = Unit.getOrigUnit(); |
Alexey Samsonov | 7a18c06 | 2015-05-19 21:54:32 +0000 | [diff] [blame] | 2631 | const auto *OrigUnitDie = OrigUnit.getUnitDIE(false); |
Frederic Riss | 2544087 | 2015-03-13 23:30:31 +0000 | [diff] [blame] | 2632 | uint64_t OrigLowPc = OrigUnitDie->getAttributeValueAsAddress( |
| 2633 | &OrigUnit, dwarf::DW_AT_low_pc, -1ULL); |
| 2634 | // Ranges addresses are based on the unit's low_pc. Compute the |
| 2635 | // offset we need to apply to adapt to the the new unit's low_pc. |
| 2636 | int64_t UnitPcOffset = 0; |
| 2637 | if (OrigLowPc != -1ULL) |
| 2638 | UnitPcOffset = int64_t(OrigLowPc) - Unit.getLowPc(); |
| 2639 | |
| 2640 | for (const auto &RangeAttribute : Unit.getRangesAttributes()) { |
Duncan P. N. Exon Smith | e7e1d0c | 2015-05-27 22:14:58 +0000 | [diff] [blame] | 2641 | uint32_t Offset = RangeAttribute.get(); |
| 2642 | RangeAttribute.set(Streamer->getRangesSectionSize()); |
Frederic Riss | 2544087 | 2015-03-13 23:30:31 +0000 | [diff] [blame] | 2643 | RangeList.extract(RangeExtractor, &Offset); |
| 2644 | const auto &Entries = RangeList.getEntries(); |
Frederic Riss | 9454620 | 2015-08-31 05:09:32 +0000 | [diff] [blame] | 2645 | if (!Entries.empty()) { |
| 2646 | const DWARFDebugRangeList::RangeListEntry &First = Entries.front(); |
Frederic Riss | 2544087 | 2015-03-13 23:30:31 +0000 | [diff] [blame] | 2647 | |
Frederic Riss | 2544087 | 2015-03-13 23:30:31 +0000 | [diff] [blame] | 2648 | if (CurrRange == InvalidRange || |
Frederic Riss | 9454620 | 2015-08-31 05:09:32 +0000 | [diff] [blame] | 2649 | First.StartAddress + OrigLowPc < CurrRange.start() || |
| 2650 | First.StartAddress + OrigLowPc >= CurrRange.stop()) { |
| 2651 | CurrRange = FunctionRanges.find(First.StartAddress + OrigLowPc); |
| 2652 | if (CurrRange == InvalidRange || |
| 2653 | CurrRange.start() > First.StartAddress + OrigLowPc) { |
| 2654 | reportWarning("no mapping for range."); |
| 2655 | continue; |
| 2656 | } |
Frederic Riss | 2544087 | 2015-03-13 23:30:31 +0000 | [diff] [blame] | 2657 | } |
| 2658 | } |
| 2659 | |
| 2660 | Streamer->emitRangesEntries(UnitPcOffset, OrigLowPc, CurrRange, Entries, |
| 2661 | AddressSize); |
| 2662 | } |
| 2663 | } |
| 2664 | |
Frederic Riss | 563b1b0 | 2015-03-14 03:46:51 +0000 | [diff] [blame] | 2665 | /// \brief Generate the debug_aranges entries for \p Unit and if the |
| 2666 | /// unit has a DW_AT_ranges attribute, also emit the debug_ranges |
| 2667 | /// contribution for this attribute. |
Frederic Riss | 2544087 | 2015-03-13 23:30:31 +0000 | [diff] [blame] | 2668 | /// FIXME: this could actually be done right in patchRangesForUnit, |
| 2669 | /// but for the sake of initial bit-for-bit compatibility with legacy |
| 2670 | /// dsymutil, we have to do it in a delayed pass. |
| 2671 | void DwarfLinker::generateUnitRanges(CompileUnit &Unit) const { |
Duncan P. N. Exon Smith | e7e1d0c | 2015-05-27 22:14:58 +0000 | [diff] [blame] | 2672 | auto Attr = Unit.getUnitRangesAttribute(); |
Frederic Riss | 563b1b0 | 2015-03-14 03:46:51 +0000 | [diff] [blame] | 2673 | if (Attr) |
Duncan P. N. Exon Smith | e7e1d0c | 2015-05-27 22:14:58 +0000 | [diff] [blame] | 2674 | Attr->set(Streamer->getRangesSectionSize()); |
| 2675 | Streamer->emitUnitRangesEntries(Unit, static_cast<bool>(Attr)); |
Frederic Riss | 2544087 | 2015-03-13 23:30:31 +0000 | [diff] [blame] | 2676 | } |
| 2677 | |
Frederic Riss | 63786b0 | 2015-03-15 20:45:43 +0000 | [diff] [blame] | 2678 | /// \brief Insert the new line info sequence \p Seq into the current |
| 2679 | /// set of already linked line info \p Rows. |
| 2680 | static void insertLineSequence(std::vector<DWARFDebugLine::Row> &Seq, |
| 2681 | std::vector<DWARFDebugLine::Row> &Rows) { |
| 2682 | if (Seq.empty()) |
| 2683 | return; |
| 2684 | |
| 2685 | if (!Rows.empty() && Rows.back().Address < Seq.front().Address) { |
| 2686 | Rows.insert(Rows.end(), Seq.begin(), Seq.end()); |
| 2687 | Seq.clear(); |
| 2688 | return; |
| 2689 | } |
| 2690 | |
| 2691 | auto InsertPoint = std::lower_bound( |
| 2692 | Rows.begin(), Rows.end(), Seq.front(), |
| 2693 | [](const DWARFDebugLine::Row &LHS, const DWARFDebugLine::Row &RHS) { |
| 2694 | return LHS.Address < RHS.Address; |
| 2695 | }); |
| 2696 | |
| 2697 | // FIXME: this only removes the unneeded end_sequence if the |
| 2698 | // sequences have been inserted in order. using a global sort like |
| 2699 | // described in patchLineTableForUnit() and delaying the end_sequene |
| 2700 | // elimination to emitLineTableForUnit() we can get rid of all of them. |
| 2701 | if (InsertPoint != Rows.end() && |
| 2702 | InsertPoint->Address == Seq.front().Address && InsertPoint->EndSequence) { |
| 2703 | *InsertPoint = Seq.front(); |
| 2704 | Rows.insert(InsertPoint + 1, Seq.begin() + 1, Seq.end()); |
| 2705 | } else { |
| 2706 | Rows.insert(InsertPoint, Seq.begin(), Seq.end()); |
| 2707 | } |
| 2708 | |
| 2709 | Seq.clear(); |
| 2710 | } |
| 2711 | |
Duncan P. N. Exon Smith | aed187c | 2015-06-25 21:42:46 +0000 | [diff] [blame] | 2712 | static void patchStmtList(DIE &Die, DIEInteger Offset) { |
| 2713 | for (auto &V : Die.values()) |
| 2714 | if (V.getAttribute() == dwarf::DW_AT_stmt_list) { |
Duncan P. N. Exon Smith | 4fb1f9c | 2015-06-25 23:46:41 +0000 | [diff] [blame] | 2715 | V = DIEValue(V.getAttribute(), V.getForm(), Offset); |
Duncan P. N. Exon Smith | aed187c | 2015-06-25 21:42:46 +0000 | [diff] [blame] | 2716 | return; |
| 2717 | } |
| 2718 | |
| 2719 | llvm_unreachable("Didn't find DW_AT_stmt_list in cloned DIE!"); |
| 2720 | } |
| 2721 | |
Frederic Riss | 63786b0 | 2015-03-15 20:45:43 +0000 | [diff] [blame] | 2722 | /// \brief Extract the line table for \p Unit from \p OrigDwarf, and |
| 2723 | /// recreate a relocated version of these for the address ranges that |
| 2724 | /// are present in the binary. |
| 2725 | void DwarfLinker::patchLineTableForUnit(CompileUnit &Unit, |
| 2726 | DWARFContext &OrigDwarf) { |
Frederic Riss | f37964c | 2015-06-05 20:27:07 +0000 | [diff] [blame] | 2727 | const DWARFDebugInfoEntryMinimal *CUDie = Unit.getOrigUnit().getUnitDIE(); |
Frederic Riss | 63786b0 | 2015-03-15 20:45:43 +0000 | [diff] [blame] | 2728 | uint64_t StmtList = CUDie->getAttributeValueAsSectionOffset( |
| 2729 | &Unit.getOrigUnit(), dwarf::DW_AT_stmt_list, -1ULL); |
| 2730 | if (StmtList == -1ULL) |
| 2731 | return; |
| 2732 | |
| 2733 | // Update the cloned DW_AT_stmt_list with the correct debug_line offset. |
Duncan P. N. Exon Smith | aed187c | 2015-06-25 21:42:46 +0000 | [diff] [blame] | 2734 | if (auto *OutputDIE = Unit.getOutputUnitDIE()) |
| 2735 | patchStmtList(*OutputDIE, DIEInteger(Streamer->getLineSectionSize())); |
Frederic Riss | 63786b0 | 2015-03-15 20:45:43 +0000 | [diff] [blame] | 2736 | |
| 2737 | // Parse the original line info for the unit. |
| 2738 | DWARFDebugLine::LineTable LineTable; |
| 2739 | uint32_t StmtOffset = StmtList; |
| 2740 | StringRef LineData = OrigDwarf.getLineSection().Data; |
| 2741 | DataExtractor LineExtractor(LineData, OrigDwarf.isLittleEndian(), |
| 2742 | Unit.getOrigUnit().getAddressByteSize()); |
| 2743 | LineTable.parse(LineExtractor, &OrigDwarf.getLineSection().Relocs, |
| 2744 | &StmtOffset); |
| 2745 | |
| 2746 | // This vector is the output line table. |
| 2747 | std::vector<DWARFDebugLine::Row> NewRows; |
| 2748 | NewRows.reserve(LineTable.Rows.size()); |
| 2749 | |
| 2750 | // Current sequence of rows being extracted, before being inserted |
| 2751 | // in NewRows. |
| 2752 | std::vector<DWARFDebugLine::Row> Seq; |
| 2753 | const auto &FunctionRanges = Unit.getFunctionRanges(); |
| 2754 | auto InvalidRange = FunctionRanges.end(), CurrRange = InvalidRange; |
| 2755 | |
| 2756 | // FIXME: This logic is meant to generate exactly the same output as |
| 2757 | // Darwin's classic dsynutil. There is a nicer way to implement this |
| 2758 | // by simply putting all the relocated line info in NewRows and simply |
| 2759 | // sorting NewRows before passing it to emitLineTableForUnit. This |
| 2760 | // should be correct as sequences for a function should stay |
| 2761 | // together in the sorted output. There are a few corner cases that |
| 2762 | // look suspicious though, and that required to implement the logic |
| 2763 | // this way. Revisit that once initial validation is finished. |
| 2764 | |
| 2765 | // Iterate over the object file line info and extract the sequences |
| 2766 | // that correspond to linked functions. |
| 2767 | for (auto &Row : LineTable.Rows) { |
| 2768 | // Check wether we stepped out of the range. The range is |
| 2769 | // half-open, but consider accept the end address of the range if |
| 2770 | // it is marked as end_sequence in the input (because in that |
| 2771 | // case, the relocation offset is accurate and that entry won't |
| 2772 | // serve as the start of another function). |
| 2773 | if (CurrRange == InvalidRange || Row.Address < CurrRange.start() || |
| 2774 | Row.Address > CurrRange.stop() || |
| 2775 | (Row.Address == CurrRange.stop() && !Row.EndSequence)) { |
| 2776 | // We just stepped out of a known range. Insert a end_sequence |
| 2777 | // corresponding to the end of the range. |
| 2778 | uint64_t StopAddress = CurrRange != InvalidRange |
| 2779 | ? CurrRange.stop() + CurrRange.value() |
| 2780 | : -1ULL; |
| 2781 | CurrRange = FunctionRanges.find(Row.Address); |
| 2782 | bool CurrRangeValid = |
| 2783 | CurrRange != InvalidRange && CurrRange.start() <= Row.Address; |
| 2784 | if (!CurrRangeValid) { |
| 2785 | CurrRange = InvalidRange; |
| 2786 | if (StopAddress != -1ULL) { |
| 2787 | // Try harder by looking in the DebugMapObject function |
| 2788 | // ranges map. There are corner cases where this finds a |
| 2789 | // valid entry. It's unclear if this is right or wrong, but |
| 2790 | // for now do as dsymutil. |
| 2791 | // FIXME: Understand exactly what cases this addresses and |
| 2792 | // potentially remove it along with the Ranges map. |
| 2793 | auto Range = Ranges.lower_bound(Row.Address); |
| 2794 | if (Range != Ranges.begin() && Range != Ranges.end()) |
| 2795 | --Range; |
| 2796 | |
| 2797 | if (Range != Ranges.end() && Range->first <= Row.Address && |
| 2798 | Range->second.first >= Row.Address) { |
| 2799 | StopAddress = Row.Address + Range->second.second; |
| 2800 | } |
| 2801 | } |
| 2802 | } |
| 2803 | if (StopAddress != -1ULL && !Seq.empty()) { |
| 2804 | // Insert end sequence row with the computed end address, but |
| 2805 | // the same line as the previous one. |
Yaron Keren | e3c0706 | 2015-08-10 16:15:51 +0000 | [diff] [blame] | 2806 | auto NextLine = Seq.back(); |
Yaron Keren | 2ad3b33 | 2015-08-10 18:27:51 +0000 | [diff] [blame] | 2807 | NextLine.Address = StopAddress; |
| 2808 | NextLine.EndSequence = 1; |
| 2809 | NextLine.PrologueEnd = 0; |
| 2810 | NextLine.BasicBlock = 0; |
| 2811 | NextLine.EpilogueBegin = 0; |
Yaron Keren | f850d98 | 2015-08-10 18:03:35 +0000 | [diff] [blame] | 2812 | Seq.push_back(NextLine); |
Frederic Riss | 63786b0 | 2015-03-15 20:45:43 +0000 | [diff] [blame] | 2813 | insertLineSequence(Seq, NewRows); |
| 2814 | } |
| 2815 | |
| 2816 | if (!CurrRangeValid) |
| 2817 | continue; |
| 2818 | } |
| 2819 | |
| 2820 | // Ignore empty sequences. |
| 2821 | if (Row.EndSequence && Seq.empty()) |
| 2822 | continue; |
| 2823 | |
| 2824 | // Relocate row address and add it to the current sequence. |
| 2825 | Row.Address += CurrRange.value(); |
| 2826 | Seq.emplace_back(Row); |
| 2827 | |
| 2828 | if (Row.EndSequence) |
| 2829 | insertLineSequence(Seq, NewRows); |
| 2830 | } |
| 2831 | |
| 2832 | // Finished extracting, now emit the line tables. |
| 2833 | uint32_t PrologueEnd = StmtList + 10 + LineTable.Prologue.PrologueLength; |
| 2834 | // FIXME: LLVM hardcodes it's prologue values. We just copy the |
| 2835 | // prologue over and that works because we act as both producer and |
| 2836 | // consumer. It would be nicer to have a real configurable line |
| 2837 | // table emitter. |
| 2838 | if (LineTable.Prologue.Version != 2 || |
| 2839 | LineTable.Prologue.DefaultIsStmt != DWARF2_LINE_DEFAULT_IS_STMT || |
Frederic Riss | a5e1453 | 2015-08-07 15:14:13 +0000 | [diff] [blame] | 2840 | LineTable.Prologue.OpcodeBase > 13) |
Frederic Riss | 63786b0 | 2015-03-15 20:45:43 +0000 | [diff] [blame] | 2841 | reportWarning("line table paramters mismatch. Cannot emit."); |
Frederic Riss | a5e1453 | 2015-08-07 15:14:13 +0000 | [diff] [blame] | 2842 | else { |
| 2843 | MCDwarfLineTableParams Params; |
| 2844 | Params.DWARF2LineOpcodeBase = LineTable.Prologue.OpcodeBase; |
| 2845 | Params.DWARF2LineBase = LineTable.Prologue.LineBase; |
| 2846 | Params.DWARF2LineRange = LineTable.Prologue.LineRange; |
| 2847 | Streamer->emitLineTableForUnit(Params, |
| 2848 | LineData.slice(StmtList + 4, PrologueEnd), |
Frederic Riss | 63786b0 | 2015-03-15 20:45:43 +0000 | [diff] [blame] | 2849 | LineTable.Prologue.MinInstLength, NewRows, |
| 2850 | Unit.getOrigUnit().getAddressByteSize()); |
Frederic Riss | a5e1453 | 2015-08-07 15:14:13 +0000 | [diff] [blame] | 2851 | } |
Frederic Riss | 63786b0 | 2015-03-15 20:45:43 +0000 | [diff] [blame] | 2852 | } |
| 2853 | |
Frederic Riss | bce93ff | 2015-03-16 02:05:10 +0000 | [diff] [blame] | 2854 | void DwarfLinker::emitAcceleratorEntriesForUnit(CompileUnit &Unit) { |
| 2855 | Streamer->emitPubNamesForUnit(Unit); |
| 2856 | Streamer->emitPubTypesForUnit(Unit); |
| 2857 | } |
| 2858 | |
Frederic Riss | 5a64207 | 2015-06-05 23:06:11 +0000 | [diff] [blame] | 2859 | /// \brief Read the frame info stored in the object, and emit the |
| 2860 | /// patched frame descriptions for the linked binary. |
| 2861 | /// |
| 2862 | /// This is actually pretty easy as the data of the CIEs and FDEs can |
| 2863 | /// be considered as black boxes and moved as is. The only thing to do |
| 2864 | /// is to patch the addresses in the headers. |
| 2865 | void DwarfLinker::patchFrameInfoForObject(const DebugMapObject &DMO, |
| 2866 | DWARFContext &OrigDwarf, |
| 2867 | unsigned AddrSize) { |
| 2868 | StringRef FrameData = OrigDwarf.getDebugFrameSection(); |
| 2869 | if (FrameData.empty()) |
| 2870 | return; |
| 2871 | |
| 2872 | DataExtractor Data(FrameData, OrigDwarf.isLittleEndian(), 0); |
| 2873 | uint32_t InputOffset = 0; |
| 2874 | |
| 2875 | // Store the data of the CIEs defined in this object, keyed by their |
| 2876 | // offsets. |
| 2877 | DenseMap<uint32_t, StringRef> LocalCIES; |
| 2878 | |
| 2879 | while (Data.isValidOffset(InputOffset)) { |
| 2880 | uint32_t EntryOffset = InputOffset; |
| 2881 | uint32_t InitialLength = Data.getU32(&InputOffset); |
| 2882 | if (InitialLength == 0xFFFFFFFF) |
| 2883 | return reportWarning("Dwarf64 bits no supported"); |
| 2884 | |
| 2885 | uint32_t CIEId = Data.getU32(&InputOffset); |
| 2886 | if (CIEId == 0xFFFFFFFF) { |
| 2887 | // This is a CIE, store it. |
| 2888 | StringRef CIEData = FrameData.substr(EntryOffset, InitialLength + 4); |
| 2889 | LocalCIES[EntryOffset] = CIEData; |
| 2890 | // The -4 is to account for the CIEId we just read. |
| 2891 | InputOffset += InitialLength - 4; |
| 2892 | continue; |
| 2893 | } |
| 2894 | |
| 2895 | uint32_t Loc = Data.getUnsigned(&InputOffset, AddrSize); |
| 2896 | |
| 2897 | // Some compilers seem to emit frame info that doesn't start at |
| 2898 | // the function entry point, thus we can't just lookup the address |
| 2899 | // in the debug map. Use the linker's range map to see if the FDE |
| 2900 | // describes something that we can relocate. |
| 2901 | auto Range = Ranges.upper_bound(Loc); |
| 2902 | if (Range != Ranges.begin()) |
| 2903 | --Range; |
| 2904 | if (Range == Ranges.end() || Range->first > Loc || |
| 2905 | Range->second.first <= Loc) { |
| 2906 | // The +4 is to account for the size of the InitialLength field itself. |
| 2907 | InputOffset = EntryOffset + InitialLength + 4; |
| 2908 | continue; |
| 2909 | } |
| 2910 | |
| 2911 | // This is an FDE, and we have a mapping. |
| 2912 | // Have we already emitted a corresponding CIE? |
| 2913 | StringRef CIEData = LocalCIES[CIEId]; |
| 2914 | if (CIEData.empty()) |
| 2915 | return reportWarning("Inconsistent debug_frame content. Dropping."); |
| 2916 | |
| 2917 | // Look if we already emitted a CIE that corresponds to the |
| 2918 | // referenced one (the CIE data is the key of that lookup). |
| 2919 | auto IteratorInserted = EmittedCIEs.insert( |
| 2920 | std::make_pair(CIEData, Streamer->getFrameSectionSize())); |
| 2921 | // If there is no CIE yet for this ID, emit it. |
| 2922 | if (IteratorInserted.second || |
| 2923 | // FIXME: dsymutil-classic only caches the last used CIE for |
| 2924 | // reuse. Mimic that behavior for now. Just removing that |
| 2925 | // second half of the condition and the LastCIEOffset variable |
| 2926 | // makes the code DTRT. |
| 2927 | LastCIEOffset != IteratorInserted.first->getValue()) { |
| 2928 | LastCIEOffset = Streamer->getFrameSectionSize(); |
| 2929 | IteratorInserted.first->getValue() = LastCIEOffset; |
| 2930 | Streamer->emitCIE(CIEData); |
| 2931 | } |
| 2932 | |
| 2933 | // Emit the FDE with updated address and CIE pointer. |
| 2934 | // (4 + AddrSize) is the size of the CIEId + initial_location |
| 2935 | // fields that will get reconstructed by emitFDE(). |
| 2936 | unsigned FDERemainingBytes = InitialLength - (4 + AddrSize); |
| 2937 | Streamer->emitFDE(IteratorInserted.first->getValue(), AddrSize, |
| 2938 | Loc + Range->second.second, |
| 2939 | FrameData.substr(InputOffset, FDERemainingBytes)); |
| 2940 | InputOffset += FDERemainingBytes; |
| 2941 | } |
| 2942 | } |
| 2943 | |
Frederic Riss | eb85c8f | 2015-07-24 06:41:11 +0000 | [diff] [blame] | 2944 | ErrorOr<const object::ObjectFile &> |
| 2945 | DwarfLinker::loadObject(BinaryHolder &BinaryHolder, DebugMapObject &Obj, |
| 2946 | const DebugMap &Map) { |
| 2947 | auto ErrOrObjs = |
| 2948 | BinaryHolder.GetObjectFiles(Obj.getObjectFilename(), Obj.getTimestamp()); |
Frederic Riss | afeac30 | 2015-08-31 05:16:35 +0000 | [diff] [blame] | 2949 | if (std::error_code EC = ErrOrObjs.getError()) { |
Frederic Riss | eb85c8f | 2015-07-24 06:41:11 +0000 | [diff] [blame] | 2950 | reportWarning(Twine(Obj.getObjectFilename()) + ": " + EC.message()); |
Frederic Riss | afeac30 | 2015-08-31 05:16:35 +0000 | [diff] [blame] | 2951 | return EC; |
| 2952 | } |
Frederic Riss | eb85c8f | 2015-07-24 06:41:11 +0000 | [diff] [blame] | 2953 | auto ErrOrObj = BinaryHolder.Get(Map.getTriple()); |
| 2954 | if (std::error_code EC = ErrOrObj.getError()) |
| 2955 | reportWarning(Twine(Obj.getObjectFilename()) + ": " + EC.message()); |
| 2956 | return ErrOrObj; |
| 2957 | } |
| 2958 | |
Frederic Riss | d345518 | 2015-01-28 18:27:01 +0000 | [diff] [blame] | 2959 | bool DwarfLinker::link(const DebugMap &Map) { |
| 2960 | |
Frederic Riss | c99ea20 | 2015-02-28 00:29:11 +0000 | [diff] [blame] | 2961 | if (!createStreamer(Map.getTriple(), OutputFilename)) |
| 2962 | return false; |
| 2963 | |
Frederic Riss | b8b43d5 | 2015-03-04 22:07:44 +0000 | [diff] [blame] | 2964 | // Size of the DIEs (and headers) generated for the linked output. |
| 2965 | uint64_t OutputDebugInfoSize = 0; |
Frederic Riss | 3cced05 | 2015-03-14 03:46:40 +0000 | [diff] [blame] | 2966 | // A unique ID that identifies each compile unit. |
| 2967 | unsigned UnitID = 0; |
Frederic Riss | d345518 | 2015-01-28 18:27:01 +0000 | [diff] [blame] | 2968 | for (const auto &Obj : Map.objects()) { |
Frederic Riss | 1b9da42 | 2015-02-13 23:18:29 +0000 | [diff] [blame] | 2969 | CurrentDebugObject = Obj.get(); |
| 2970 | |
Frederic Riss | b981832 | 2015-02-28 00:29:07 +0000 | [diff] [blame] | 2971 | if (Options.Verbose) |
Frederic Riss | d345518 | 2015-01-28 18:27:01 +0000 | [diff] [blame] | 2972 | outs() << "DEBUG MAP OBJECT: " << Obj->getObjectFilename() << "\n"; |
Frederic Riss | eb85c8f | 2015-07-24 06:41:11 +0000 | [diff] [blame] | 2973 | auto ErrOrObj = loadObject(BinHolder, *Obj, Map); |
| 2974 | if (!ErrOrObj) |
Frederic Riss | d345518 | 2015-01-28 18:27:01 +0000 | [diff] [blame] | 2975 | continue; |
Frederic Riss | d345518 | 2015-01-28 18:27:01 +0000 | [diff] [blame] | 2976 | |
Frederic Riss | 1036e64 | 2015-02-13 23:18:22 +0000 | [diff] [blame] | 2977 | // Look for relocations that correspond to debug map entries. |
| 2978 | if (!findValidRelocsInDebugInfo(*ErrOrObj, *Obj)) { |
Frederic Riss | b981832 | 2015-02-28 00:29:07 +0000 | [diff] [blame] | 2979 | if (Options.Verbose) |
Frederic Riss | 1036e64 | 2015-02-13 23:18:22 +0000 | [diff] [blame] | 2980 | outs() << "No valid relocations found. Skipping.\n"; |
| 2981 | continue; |
| 2982 | } |
| 2983 | |
Frederic Riss | 563cba6 | 2015-01-28 22:15:14 +0000 | [diff] [blame] | 2984 | // Setup access to the debug info. |
Frederic Riss | d345518 | 2015-01-28 18:27:01 +0000 | [diff] [blame] | 2985 | DWARFContextInMemory DwarfContext(*ErrOrObj); |
Frederic Riss | 63786b0 | 2015-03-15 20:45:43 +0000 | [diff] [blame] | 2986 | startDebugObject(DwarfContext, *Obj); |
Frederic Riss | d345518 | 2015-01-28 18:27:01 +0000 | [diff] [blame] | 2987 | |
Frederic Riss | 563cba6 | 2015-01-28 22:15:14 +0000 | [diff] [blame] | 2988 | // In a first phase, just read in the debug info and store the DIE |
| 2989 | // parent links that we will use during the next phase. |
Frederic Riss | d345518 | 2015-01-28 18:27:01 +0000 | [diff] [blame] | 2990 | for (const auto &CU : DwarfContext.compile_units()) { |
Alexey Samsonov | 7a18c06 | 2015-05-19 21:54:32 +0000 | [diff] [blame] | 2991 | auto *CUDie = CU->getUnitDIE(false); |
Frederic Riss | b981832 | 2015-02-28 00:29:07 +0000 | [diff] [blame] | 2992 | if (Options.Verbose) { |
Frederic Riss | d345518 | 2015-01-28 18:27:01 +0000 | [diff] [blame] | 2993 | outs() << "Input compilation unit:"; |
| 2994 | CUDie->dump(outs(), CU.get(), 0); |
| 2995 | } |
Frederic Riss | 1c65094 | 2015-07-21 22:41:43 +0000 | [diff] [blame] | 2996 | Units.emplace_back(*CU, UnitID++, !Options.NoODR); |
| 2997 | gatherDIEParents(CUDie, 0, Units.back(), &ODRContexts.getRoot(), |
| 2998 | StringPool, ODRContexts); |
Frederic Riss | d345518 | 2015-01-28 18:27:01 +0000 | [diff] [blame] | 2999 | } |
Frederic Riss | 563cba6 | 2015-01-28 22:15:14 +0000 | [diff] [blame] | 3000 | |
Frederic Riss | 84c09a5 | 2015-02-13 23:18:34 +0000 | [diff] [blame] | 3001 | // Then mark all the DIEs that need to be present in the linked |
| 3002 | // output and collect some information about them. Note that this |
| 3003 | // loop can not be merged with the previous one becaue cross-cu |
| 3004 | // references require the ParentIdx to be setup for every CU in |
| 3005 | // the object file before calling this. |
| 3006 | for (auto &CurrentUnit : Units) |
Alexey Samsonov | 7a18c06 | 2015-05-19 21:54:32 +0000 | [diff] [blame] | 3007 | lookForDIEsToKeep(*CurrentUnit.getOrigUnit().getUnitDIE(), *Obj, |
Frederic Riss | 84c09a5 | 2015-02-13 23:18:34 +0000 | [diff] [blame] | 3008 | CurrentUnit, 0); |
| 3009 | |
Frederic Riss | 23e20e9 | 2015-03-07 01:25:09 +0000 | [diff] [blame] | 3010 | // The calls to applyValidRelocs inside cloneDIE will walk the |
| 3011 | // reloc array again (in the same way findValidRelocsInDebugInfo() |
| 3012 | // did). We need to reset the NextValidReloc index to the beginning. |
| 3013 | NextValidReloc = 0; |
| 3014 | |
Frederic Riss | b8b43d5 | 2015-03-04 22:07:44 +0000 | [diff] [blame] | 3015 | // Construct the output DIE tree by cloning the DIEs we chose to |
| 3016 | // keep above. If there are no valid relocs, then there's nothing |
| 3017 | // to clone/emit. |
| 3018 | if (!ValidRelocs.empty()) |
| 3019 | for (auto &CurrentUnit : Units) { |
Alexey Samsonov | 7a18c06 | 2015-05-19 21:54:32 +0000 | [diff] [blame] | 3020 | const auto *InputDIE = CurrentUnit.getOrigUnit().getUnitDIE(); |
Frederic Riss | 9d441b6 | 2015-03-06 23:22:50 +0000 | [diff] [blame] | 3021 | CurrentUnit.setStartOffset(OutputDebugInfoSize); |
Frederic Riss | 31da324 | 2015-03-11 18:45:52 +0000 | [diff] [blame] | 3022 | DIE *OutputDIE = cloneDIE(*InputDIE, CurrentUnit, 0 /* PCOffset */, |
| 3023 | 11 /* Unit Header size */); |
Frederic Riss | b8b43d5 | 2015-03-04 22:07:44 +0000 | [diff] [blame] | 3024 | CurrentUnit.setOutputUnitDIE(OutputDIE); |
Frederic Riss | 9d441b6 | 2015-03-06 23:22:50 +0000 | [diff] [blame] | 3025 | OutputDebugInfoSize = CurrentUnit.computeNextUnitOffset(); |
Frederic Riss | 63786b0 | 2015-03-15 20:45:43 +0000 | [diff] [blame] | 3026 | if (Options.NoOutput) |
| 3027 | continue; |
| 3028 | // FIXME: for compatibility with the classic dsymutil, we emit |
| 3029 | // an empty line table for the unit, even if the unit doesn't |
| 3030 | // actually exist in the DIE tree. |
| 3031 | patchLineTableForUnit(CurrentUnit, DwarfContext); |
| 3032 | if (!OutputDIE) |
Frederic Riss | 2544087 | 2015-03-13 23:30:31 +0000 | [diff] [blame] | 3033 | continue; |
| 3034 | patchRangesForUnit(CurrentUnit, DwarfContext); |
Frederic Riss | dfb9790 | 2015-03-14 15:49:07 +0000 | [diff] [blame] | 3035 | Streamer->emitLocationsForUnit(CurrentUnit, DwarfContext); |
Frederic Riss | bce93ff | 2015-03-16 02:05:10 +0000 | [diff] [blame] | 3036 | emitAcceleratorEntriesForUnit(CurrentUnit); |
Frederic Riss | b8b43d5 | 2015-03-04 22:07:44 +0000 | [diff] [blame] | 3037 | } |
| 3038 | |
| 3039 | // Emit all the compile unit's debug information. |
| 3040 | if (!ValidRelocs.empty() && !Options.NoOutput) |
| 3041 | for (auto &CurrentUnit : Units) { |
Frederic Riss | 2544087 | 2015-03-13 23:30:31 +0000 | [diff] [blame] | 3042 | generateUnitRanges(CurrentUnit); |
Frederic Riss | 9833de6 | 2015-03-06 23:22:53 +0000 | [diff] [blame] | 3043 | CurrentUnit.fixupForwardReferences(); |
Frederic Riss | b8b43d5 | 2015-03-04 22:07:44 +0000 | [diff] [blame] | 3044 | Streamer->emitCompileUnitHeader(CurrentUnit); |
| 3045 | if (!CurrentUnit.getOutputUnitDIE()) |
| 3046 | continue; |
| 3047 | Streamer->emitDIE(*CurrentUnit.getOutputUnitDIE()); |
| 3048 | } |
| 3049 | |
Frederic Riss | 5a64207 | 2015-06-05 23:06:11 +0000 | [diff] [blame] | 3050 | if (!ValidRelocs.empty() && !Options.NoOutput && !Units.empty()) |
| 3051 | patchFrameInfoForObject(*Obj, DwarfContext, |
| 3052 | Units[0].getOrigUnit().getAddressByteSize()); |
| 3053 | |
Frederic Riss | 563cba6 | 2015-01-28 22:15:14 +0000 | [diff] [blame] | 3054 | // Clean-up before starting working on the next object. |
| 3055 | endDebugObject(); |
Frederic Riss | d345518 | 2015-01-28 18:27:01 +0000 | [diff] [blame] | 3056 | } |
| 3057 | |
Frederic Riss | b8b43d5 | 2015-03-04 22:07:44 +0000 | [diff] [blame] | 3058 | // Emit everything that's global. |
Frederic Riss | ef64846 | 2015-03-06 17:56:30 +0000 | [diff] [blame] | 3059 | if (!Options.NoOutput) { |
Frederic Riss | b8b43d5 | 2015-03-04 22:07:44 +0000 | [diff] [blame] | 3060 | Streamer->emitAbbrevs(Abbreviations); |
Frederic Riss | ef64846 | 2015-03-06 17:56:30 +0000 | [diff] [blame] | 3061 | Streamer->emitStrings(StringPool); |
| 3062 | } |
Frederic Riss | b8b43d5 | 2015-03-04 22:07:44 +0000 | [diff] [blame] | 3063 | |
Frederic Riss | 24faade | 2015-09-02 16:49:13 +0000 | [diff] [blame^] | 3064 | return Options.NoOutput ? true : Streamer->finish(Map); |
Frederic Riss | 231f714 | 2014-12-12 17:31:24 +0000 | [diff] [blame] | 3065 | } |
| 3066 | } |
Frederic Riss | d345518 | 2015-01-28 18:27:01 +0000 | [diff] [blame] | 3067 | |
Frederic Riss | 30711fb | 2015-08-26 05:09:52 +0000 | [diff] [blame] | 3068 | /// \brief Get the offset of string \p S in the string table. This |
| 3069 | /// can insert a new element or return the offset of a preexisitng |
| 3070 | /// one. |
| 3071 | uint32_t NonRelocatableStringpool::getStringOffset(StringRef S) { |
| 3072 | if (S.empty() && !Strings.empty()) |
| 3073 | return 0; |
| 3074 | |
| 3075 | std::pair<uint32_t, StringMapEntryBase *> Entry(0, nullptr); |
| 3076 | MapTy::iterator It; |
| 3077 | bool Inserted; |
| 3078 | |
| 3079 | // A non-empty string can't be at offset 0, so if we have an entry |
| 3080 | // with a 0 offset, it must be a previously interned string. |
| 3081 | std::tie(It, Inserted) = Strings.insert(std::make_pair(S, Entry)); |
| 3082 | if (Inserted || It->getValue().first == 0) { |
| 3083 | // Set offset and chain at the end of the entries list. |
| 3084 | It->getValue().first = CurrentEndOffset; |
| 3085 | CurrentEndOffset += S.size() + 1; // +1 for the '\0'. |
| 3086 | Last->getValue().second = &*It; |
| 3087 | Last = &*It; |
| 3088 | } |
| 3089 | return It->getValue().first; |
| 3090 | } |
| 3091 | |
| 3092 | /// \brief Put \p S into the StringMap so that it gets permanent |
| 3093 | /// storage, but do not actually link it in the chain of elements |
| 3094 | /// that go into the output section. A latter call to |
| 3095 | /// getStringOffset() with the same string will chain it though. |
| 3096 | StringRef NonRelocatableStringpool::internString(StringRef S) { |
| 3097 | std::pair<uint32_t, StringMapEntryBase *> Entry(0, nullptr); |
| 3098 | auto InsertResult = Strings.insert(std::make_pair(S, Entry)); |
| 3099 | return InsertResult.first->getKey(); |
| 3100 | } |
| 3101 | |
Frederic Riss | 65e145c | 2015-08-26 05:09:55 +0000 | [diff] [blame] | 3102 | void warn(const Twine &Warning, const Twine &Context) { |
| 3103 | errs() << Twine("while processing ") + Context + ":\n"; |
| 3104 | errs() << Twine("warning: ") + Warning + "\n"; |
| 3105 | } |
| 3106 | |
| 3107 | bool error(const Twine &Error, const Twine &Context) { |
| 3108 | errs() << Twine("while processing ") + Context + ":\n"; |
| 3109 | errs() << Twine("error: ") + Error + "\n"; |
| 3110 | return false; |
| 3111 | } |
| 3112 | |
Frederic Riss | b981832 | 2015-02-28 00:29:07 +0000 | [diff] [blame] | 3113 | bool linkDwarf(StringRef OutputFilename, const DebugMap &DM, |
| 3114 | const LinkOptions &Options) { |
| 3115 | DwarfLinker Linker(OutputFilename, Options); |
Frederic Riss | d345518 | 2015-01-28 18:27:01 +0000 | [diff] [blame] | 3116 | return Linker.link(DM); |
| 3117 | } |
| 3118 | } |
Frederic Riss | 231f714 | 2014-12-12 17:31:24 +0000 | [diff] [blame] | 3119 | } |