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