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 | ce98470 | 2015-03-11 18:45:59 +0000 | [diff] [blame^] | 13 | #include "llvm/ADT/IntervalMap.h" |
Frederic Riss | c99ea20 | 2015-02-28 00:29:11 +0000 | [diff] [blame] | 14 | #include "llvm/CodeGen/AsmPrinter.h" |
Frederic Riss | b8b43d5 | 2015-03-04 22:07:44 +0000 | [diff] [blame] | 15 | #include "llvm/CodeGen/DIE.h" |
Zachary Turner | 82af943 | 2015-01-30 18:07:45 +0000 | [diff] [blame] | 16 | #include "llvm/DebugInfo/DWARF/DWARFContext.h" |
| 17 | #include "llvm/DebugInfo/DWARF/DWARFDebugInfoEntry.h" |
Frederic Riss | 1b9da42 | 2015-02-13 23:18:29 +0000 | [diff] [blame] | 18 | #include "llvm/DebugInfo/DWARF/DWARFFormValue.h" |
Frederic Riss | c99ea20 | 2015-02-28 00:29:11 +0000 | [diff] [blame] | 19 | #include "llvm/MC/MCAsmBackend.h" |
| 20 | #include "llvm/MC/MCAsmInfo.h" |
| 21 | #include "llvm/MC/MCContext.h" |
| 22 | #include "llvm/MC/MCCodeEmitter.h" |
| 23 | #include "llvm/MC/MCInstrInfo.h" |
| 24 | #include "llvm/MC/MCObjectFileInfo.h" |
| 25 | #include "llvm/MC/MCRegisterInfo.h" |
| 26 | #include "llvm/MC/MCStreamer.h" |
Frederic Riss | 1036e64 | 2015-02-13 23:18:22 +0000 | [diff] [blame] | 27 | #include "llvm/Object/MachO.h" |
Frederic Riss | 84c09a5 | 2015-02-13 23:18:34 +0000 | [diff] [blame] | 28 | #include "llvm/Support/Dwarf.h" |
| 29 | #include "llvm/Support/LEB128.h" |
Frederic Riss | c99ea20 | 2015-02-28 00:29:11 +0000 | [diff] [blame] | 30 | #include "llvm/Support/TargetRegistry.h" |
| 31 | #include "llvm/Target/TargetMachine.h" |
| 32 | #include "llvm/Target/TargetOptions.h" |
Frederic Riss | d345518 | 2015-01-28 18:27:01 +0000 | [diff] [blame] | 33 | #include <string> |
Frederic Riss | 231f714 | 2014-12-12 17:31:24 +0000 | [diff] [blame] | 34 | |
| 35 | namespace llvm { |
| 36 | namespace dsymutil { |
| 37 | |
Frederic Riss | d345518 | 2015-01-28 18:27:01 +0000 | [diff] [blame] | 38 | namespace { |
| 39 | |
Frederic Riss | def4fb7 | 2015-02-28 00:29:01 +0000 | [diff] [blame] | 40 | void warn(const Twine &Warning, const Twine &Context) { |
| 41 | errs() << Twine("while processing ") + Context + ":\n"; |
| 42 | errs() << Twine("warning: ") + Warning + "\n"; |
| 43 | } |
| 44 | |
Frederic Riss | c99ea20 | 2015-02-28 00:29:11 +0000 | [diff] [blame] | 45 | bool error(const Twine &Error, const Twine &Context) { |
| 46 | errs() << Twine("while processing ") + Context + ":\n"; |
| 47 | errs() << Twine("error: ") + Error + "\n"; |
| 48 | return false; |
| 49 | } |
| 50 | |
Frederic Riss | ce98470 | 2015-03-11 18:45:59 +0000 | [diff] [blame^] | 51 | template <typename KeyT, typename ValT> |
| 52 | using HalfOpenIntervalMap = |
| 53 | IntervalMap<KeyT, ValT, IntervalMapImpl::NodeSizer<KeyT, ValT>::LeafSize, |
| 54 | IntervalMapHalfOpenInfo<KeyT>>; |
| 55 | |
Frederic Riss | 563cba6 | 2015-01-28 22:15:14 +0000 | [diff] [blame] | 56 | /// \brief Stores all information relating to a compile unit, be it in |
| 57 | /// its original instance in the object file to its brand new cloned |
| 58 | /// and linked DIE tree. |
| 59 | class CompileUnit { |
| 60 | public: |
| 61 | /// \brief Information gathered about a DIE in the object file. |
| 62 | struct DIEInfo { |
Frederic Riss | 31da324 | 2015-03-11 18:45:52 +0000 | [diff] [blame] | 63 | int64_t AddrAdjust; ///< Address offset to apply to the described entity. |
Frederic Riss | 9833de6 | 2015-03-06 23:22:53 +0000 | [diff] [blame] | 64 | DIE *Clone; ///< Cloned version of that DIE. |
Frederic Riss | 84c09a5 | 2015-02-13 23:18:34 +0000 | [diff] [blame] | 65 | uint32_t ParentIdx; ///< The index of this DIE's parent. |
| 66 | bool Keep; ///< Is the DIE part of the linked output? |
| 67 | bool InDebugMap; ///< Was this DIE's entity found in the map? |
Frederic Riss | 563cba6 | 2015-01-28 22:15:14 +0000 | [diff] [blame] | 68 | }; |
| 69 | |
Frederic Riss | ce98470 | 2015-03-11 18:45:59 +0000 | [diff] [blame^] | 70 | CompileUnit(DWARFUnit &OrigUnit) |
| 71 | : OrigUnit(OrigUnit), RangeAlloc(), Ranges(RangeAlloc) { |
Frederic Riss | 563cba6 | 2015-01-28 22:15:14 +0000 | [diff] [blame] | 72 | Info.resize(OrigUnit.getNumDIEs()); |
| 73 | } |
| 74 | |
Frederic Riss | 2838f9e | 2015-03-05 05:29:05 +0000 | [diff] [blame] | 75 | CompileUnit(CompileUnit &&RHS) |
| 76 | : OrigUnit(RHS.OrigUnit), Info(std::move(RHS.Info)), |
| 77 | CUDie(std::move(RHS.CUDie)), StartOffset(RHS.StartOffset), |
Frederic Riss | ce98470 | 2015-03-11 18:45:59 +0000 | [diff] [blame^] | 78 | NextUnitOffset(RHS.NextUnitOffset), RangeAlloc(), Ranges(RangeAlloc) { |
| 79 | // The CompileUnit container has been 'reserve()'d with the right |
| 80 | // size. We cannot move the IntervalMap anyway. |
| 81 | llvm_unreachable("CompileUnits should not be moved."); |
| 82 | } |
David Blaikie | a8adc13 | 2015-03-04 22:20:52 +0000 | [diff] [blame] | 83 | |
Frederic Riss | c3349d4 | 2015-02-13 23:18:27 +0000 | [diff] [blame] | 84 | DWARFUnit &getOrigUnit() const { return OrigUnit; } |
Frederic Riss | 563cba6 | 2015-01-28 22:15:14 +0000 | [diff] [blame] | 85 | |
Frederic Riss | b8b43d5 | 2015-03-04 22:07:44 +0000 | [diff] [blame] | 86 | DIE *getOutputUnitDIE() const { return CUDie.get(); } |
| 87 | void setOutputUnitDIE(DIE *Die) { CUDie.reset(Die); } |
| 88 | |
Frederic Riss | 563cba6 | 2015-01-28 22:15:14 +0000 | [diff] [blame] | 89 | DIEInfo &getInfo(unsigned Idx) { return Info[Idx]; } |
| 90 | const DIEInfo &getInfo(unsigned Idx) const { return Info[Idx]; } |
| 91 | |
Frederic Riss | b8b43d5 | 2015-03-04 22:07:44 +0000 | [diff] [blame] | 92 | uint64_t getStartOffset() const { return StartOffset; } |
| 93 | uint64_t getNextUnitOffset() const { return NextUnitOffset; } |
| 94 | |
Frederic Riss | 9d441b6 | 2015-03-06 23:22:50 +0000 | [diff] [blame] | 95 | void setStartOffset(uint64_t DebugInfoSize) { StartOffset = DebugInfoSize; } |
| 96 | |
| 97 | /// \brief Compute the end offset for this unit. Must be |
| 98 | /// called after the CU's DIEs have been cloned. |
Frederic Riss | b8b43d5 | 2015-03-04 22:07:44 +0000 | [diff] [blame] | 99 | /// \returns the next unit offset (which is also the current |
| 100 | /// debug_info section size). |
Frederic Riss | 9d441b6 | 2015-03-06 23:22:50 +0000 | [diff] [blame] | 101 | uint64_t computeNextUnitOffset(); |
Frederic Riss | b8b43d5 | 2015-03-04 22:07:44 +0000 | [diff] [blame] | 102 | |
Frederic Riss | 9833de6 | 2015-03-06 23:22:53 +0000 | [diff] [blame] | 103 | /// \brief Keep track of a forward reference to DIE \p Die by |
| 104 | /// \p Attr. The attribute should be fixed up later to point to the |
| 105 | /// absolute offset of \p Die in the debug_info section. |
| 106 | void noteForwardReference(DIE *Die, DIEInteger *Attr); |
| 107 | |
| 108 | /// \brief Apply all fixups recored by noteForwardReference(). |
| 109 | void fixupForwardReferences(); |
| 110 | |
Frederic Riss | ce98470 | 2015-03-11 18:45:59 +0000 | [diff] [blame^] | 111 | /// \brief Add a function range [\p LowPC, \p HighPC) that is |
| 112 | /// relocatad by applying offset \p PCOffset. |
| 113 | void addFunctionRange(uint64_t LowPC, uint64_t HighPC, int64_t PCOffset); |
| 114 | |
Frederic Riss | 563cba6 | 2015-01-28 22:15:14 +0000 | [diff] [blame] | 115 | private: |
| 116 | DWARFUnit &OrigUnit; |
Frederic Riss | b8b43d5 | 2015-03-04 22:07:44 +0000 | [diff] [blame] | 117 | std::vector<DIEInfo> Info; ///< DIE info indexed by DIE index. |
| 118 | std::unique_ptr<DIE> CUDie; ///< Root of the linked DIE tree. |
| 119 | |
| 120 | uint64_t StartOffset; |
| 121 | uint64_t NextUnitOffset; |
Frederic Riss | 9833de6 | 2015-03-06 23:22:53 +0000 | [diff] [blame] | 122 | |
| 123 | /// \brief A list of attributes to fixup with the absolute offset of |
| 124 | /// a DIE in the debug_info section. |
| 125 | /// |
| 126 | /// The offsets for the attributes in this array couldn't be set while |
| 127 | /// cloning because for forward refences the target DIE's offset isn't |
| 128 | /// known you emit the reference attribute. |
| 129 | std::vector<std::pair<DIE *, DIEInteger *>> ForwardDIEReferences; |
Frederic Riss | ce98470 | 2015-03-11 18:45:59 +0000 | [diff] [blame^] | 130 | |
| 131 | HalfOpenIntervalMap<uint64_t, int64_t>::Allocator RangeAlloc; |
| 132 | /// \brief The ranges in that interval map are the PC ranges for |
| 133 | /// functions in this unit, associated with the PC offset to apply |
| 134 | /// to the addresses to get the linked address. |
| 135 | HalfOpenIntervalMap<uint64_t, int64_t> Ranges; |
Frederic Riss | 563cba6 | 2015-01-28 22:15:14 +0000 | [diff] [blame] | 136 | }; |
| 137 | |
Frederic Riss | 9d441b6 | 2015-03-06 23:22:50 +0000 | [diff] [blame] | 138 | uint64_t CompileUnit::computeNextUnitOffset() { |
Frederic Riss | b8b43d5 | 2015-03-04 22:07:44 +0000 | [diff] [blame] | 139 | NextUnitOffset = StartOffset + 11 /* Header size */; |
| 140 | // The root DIE might be null, meaning that the Unit had nothing to |
| 141 | // contribute to the linked output. In that case, we will emit the |
| 142 | // unit header without any actual DIE. |
| 143 | if (CUDie) |
| 144 | NextUnitOffset += CUDie->getSize(); |
| 145 | return NextUnitOffset; |
| 146 | } |
| 147 | |
Frederic Riss | 9833de6 | 2015-03-06 23:22:53 +0000 | [diff] [blame] | 148 | /// \brief Keep track of a forward reference to \p Die. |
| 149 | void CompileUnit::noteForwardReference(DIE *Die, DIEInteger *Attr) { |
| 150 | ForwardDIEReferences.emplace_back(Die, Attr); |
| 151 | } |
| 152 | |
| 153 | /// \brief Apply all fixups recorded by noteForwardReference(). |
| 154 | void CompileUnit::fixupForwardReferences() { |
| 155 | for (const auto &Ref : ForwardDIEReferences) |
| 156 | Ref.second->setValue(Ref.first->getOffset() + getStartOffset()); |
| 157 | } |
| 158 | |
Frederic Riss | ce98470 | 2015-03-11 18:45:59 +0000 | [diff] [blame^] | 159 | void CompileUnit::addFunctionRange(uint64_t LowPC, uint64_t HighPC, |
| 160 | int64_t PCOffset) { |
| 161 | Ranges.insert(LowPC, HighPC, PCOffset); |
| 162 | } |
| 163 | |
Frederic Riss | ef64846 | 2015-03-06 17:56:30 +0000 | [diff] [blame] | 164 | /// \brief A string table that doesn't need relocations. |
| 165 | /// |
| 166 | /// We are doing a final link, no need for a string table that |
| 167 | /// has relocation entries for every reference to it. This class |
| 168 | /// provides this ablitity by just associating offsets with |
| 169 | /// strings. |
| 170 | class NonRelocatableStringpool { |
| 171 | public: |
| 172 | /// \brief Entries are stored into the StringMap and simply linked |
| 173 | /// together through the second element of this pair in order to |
| 174 | /// keep track of insertion order. |
| 175 | typedef StringMap<std::pair<uint32_t, StringMapEntryBase *>, BumpPtrAllocator> |
| 176 | MapTy; |
| 177 | |
| 178 | NonRelocatableStringpool() |
| 179 | : CurrentEndOffset(0), Sentinel(0), Last(&Sentinel) { |
| 180 | // Legacy dsymutil puts an empty string at the start of the line |
| 181 | // table. |
| 182 | getStringOffset(""); |
| 183 | } |
| 184 | |
| 185 | /// \brief Get the offset of string \p S in the string table. This |
| 186 | /// can insert a new element or return the offset of a preexisitng |
| 187 | /// one. |
| 188 | uint32_t getStringOffset(StringRef S); |
| 189 | |
| 190 | /// \brief Get permanent storage for \p S (but do not necessarily |
| 191 | /// emit \p S in the output section). |
| 192 | /// \returns The StringRef that points to permanent storage to use |
| 193 | /// in place of \p S. |
| 194 | StringRef internString(StringRef S); |
| 195 | |
| 196 | // \brief Return the first entry of the string table. |
| 197 | const MapTy::MapEntryTy *getFirstEntry() const { |
| 198 | return getNextEntry(&Sentinel); |
| 199 | } |
| 200 | |
| 201 | // \brief Get the entry following \p E in the string table or null |
| 202 | // if \p E was the last entry. |
| 203 | const MapTy::MapEntryTy *getNextEntry(const MapTy::MapEntryTy *E) const { |
| 204 | return static_cast<const MapTy::MapEntryTy *>(E->getValue().second); |
| 205 | } |
| 206 | |
| 207 | uint64_t getSize() { return CurrentEndOffset; } |
| 208 | |
| 209 | private: |
| 210 | MapTy Strings; |
| 211 | uint32_t CurrentEndOffset; |
| 212 | MapTy::MapEntryTy Sentinel, *Last; |
| 213 | }; |
| 214 | |
| 215 | /// \brief Get the offset of string \p S in the string table. This |
| 216 | /// can insert a new element or return the offset of a preexisitng |
| 217 | /// one. |
| 218 | uint32_t NonRelocatableStringpool::getStringOffset(StringRef S) { |
| 219 | if (S.empty() && !Strings.empty()) |
| 220 | return 0; |
| 221 | |
| 222 | std::pair<uint32_t, StringMapEntryBase *> Entry(0, nullptr); |
| 223 | MapTy::iterator It; |
| 224 | bool Inserted; |
| 225 | |
| 226 | // A non-empty string can't be at offset 0, so if we have an entry |
| 227 | // with a 0 offset, it must be a previously interned string. |
| 228 | std::tie(It, Inserted) = Strings.insert(std::make_pair(S, Entry)); |
| 229 | if (Inserted || It->getValue().first == 0) { |
| 230 | // Set offset and chain at the end of the entries list. |
| 231 | It->getValue().first = CurrentEndOffset; |
| 232 | CurrentEndOffset += S.size() + 1; // +1 for the '\0'. |
| 233 | Last->getValue().second = &*It; |
| 234 | Last = &*It; |
| 235 | } |
| 236 | return It->getValue().first; |
Aaron Ballman | 5287f0c | 2015-03-07 15:10:32 +0000 | [diff] [blame] | 237 | } |
Frederic Riss | ef64846 | 2015-03-06 17:56:30 +0000 | [diff] [blame] | 238 | |
| 239 | /// \brief Put \p S into the StringMap so that it gets permanent |
| 240 | /// storage, but do not actually link it in the chain of elements |
| 241 | /// that go into the output section. A latter call to |
| 242 | /// getStringOffset() with the same string will chain it though. |
| 243 | StringRef NonRelocatableStringpool::internString(StringRef S) { |
| 244 | std::pair<uint32_t, StringMapEntryBase *> Entry(0, nullptr); |
| 245 | auto InsertResult = Strings.insert(std::make_pair(S, Entry)); |
| 246 | return InsertResult.first->getKey(); |
Aaron Ballman | 5287f0c | 2015-03-07 15:10:32 +0000 | [diff] [blame] | 247 | } |
Frederic Riss | ef64846 | 2015-03-06 17:56:30 +0000 | [diff] [blame] | 248 | |
Frederic Riss | c99ea20 | 2015-02-28 00:29:11 +0000 | [diff] [blame] | 249 | /// \brief The Dwarf streaming logic |
| 250 | /// |
| 251 | /// All interactions with the MC layer that is used to build the debug |
| 252 | /// information binary representation are handled in this class. |
| 253 | class DwarfStreamer { |
| 254 | /// \defgroup MCObjects MC layer objects constructed by the streamer |
| 255 | /// @{ |
| 256 | std::unique_ptr<MCRegisterInfo> MRI; |
| 257 | std::unique_ptr<MCAsmInfo> MAI; |
| 258 | std::unique_ptr<MCObjectFileInfo> MOFI; |
| 259 | std::unique_ptr<MCContext> MC; |
| 260 | MCAsmBackend *MAB; // Owned by MCStreamer |
| 261 | std::unique_ptr<MCInstrInfo> MII; |
| 262 | std::unique_ptr<MCSubtargetInfo> MSTI; |
| 263 | MCCodeEmitter *MCE; // Owned by MCStreamer |
| 264 | MCStreamer *MS; // Owned by AsmPrinter |
| 265 | std::unique_ptr<TargetMachine> TM; |
| 266 | std::unique_ptr<AsmPrinter> Asm; |
| 267 | /// @} |
| 268 | |
| 269 | /// \brief the file we stream the linked Dwarf to. |
| 270 | std::unique_ptr<raw_fd_ostream> OutFile; |
| 271 | |
| 272 | public: |
| 273 | /// \brief Actually create the streamer and the ouptut file. |
| 274 | /// |
| 275 | /// This could be done directly in the constructor, but it feels |
| 276 | /// more natural to handle errors through return value. |
| 277 | bool init(Triple TheTriple, StringRef OutputFilename); |
| 278 | |
Frederic Riss | b8b43d5 | 2015-03-04 22:07:44 +0000 | [diff] [blame] | 279 | /// \brief Dump the file to the disk. |
Frederic Riss | c99ea20 | 2015-02-28 00:29:11 +0000 | [diff] [blame] | 280 | bool finish(); |
Frederic Riss | b8b43d5 | 2015-03-04 22:07:44 +0000 | [diff] [blame] | 281 | |
| 282 | AsmPrinter &getAsmPrinter() const { return *Asm; } |
| 283 | |
| 284 | /// \brief Set the current output section to debug_info and change |
| 285 | /// the MC Dwarf version to \p DwarfVersion. |
| 286 | void switchToDebugInfoSection(unsigned DwarfVersion); |
| 287 | |
| 288 | /// \brief Emit the compilation unit header for \p Unit in the |
| 289 | /// debug_info section. |
| 290 | /// |
| 291 | /// As a side effect, this also switches the current Dwarf version |
| 292 | /// of the MC layer to the one of U.getOrigUnit(). |
| 293 | void emitCompileUnitHeader(CompileUnit &Unit); |
| 294 | |
| 295 | /// \brief Recursively emit the DIE tree rooted at \p Die. |
| 296 | void emitDIE(DIE &Die); |
| 297 | |
| 298 | /// \brief Emit the abbreviation table \p Abbrevs to the |
| 299 | /// debug_abbrev section. |
| 300 | void emitAbbrevs(const std::vector<DIEAbbrev *> &Abbrevs); |
Frederic Riss | ef64846 | 2015-03-06 17:56:30 +0000 | [diff] [blame] | 301 | |
| 302 | /// \brief Emit the string table described by \p Pool. |
| 303 | void emitStrings(const NonRelocatableStringpool &Pool); |
Frederic Riss | c99ea20 | 2015-02-28 00:29:11 +0000 | [diff] [blame] | 304 | }; |
| 305 | |
| 306 | bool DwarfStreamer::init(Triple TheTriple, StringRef OutputFilename) { |
| 307 | std::string ErrorStr; |
| 308 | std::string TripleName; |
| 309 | StringRef Context = "dwarf streamer init"; |
| 310 | |
| 311 | // Get the target. |
| 312 | const Target *TheTarget = |
| 313 | TargetRegistry::lookupTarget(TripleName, TheTriple, ErrorStr); |
| 314 | if (!TheTarget) |
| 315 | return error(ErrorStr, Context); |
| 316 | TripleName = TheTriple.getTriple(); |
| 317 | |
| 318 | // Create all the MC Objects. |
| 319 | MRI.reset(TheTarget->createMCRegInfo(TripleName)); |
| 320 | if (!MRI) |
| 321 | return error(Twine("no register info for target ") + TripleName, Context); |
| 322 | |
| 323 | MAI.reset(TheTarget->createMCAsmInfo(*MRI, TripleName)); |
| 324 | if (!MAI) |
| 325 | return error("no asm info for target " + TripleName, Context); |
| 326 | |
| 327 | MOFI.reset(new MCObjectFileInfo); |
| 328 | MC.reset(new MCContext(MAI.get(), MRI.get(), MOFI.get())); |
| 329 | MOFI->InitMCObjectFileInfo(TripleName, Reloc::Default, CodeModel::Default, |
| 330 | *MC); |
| 331 | |
| 332 | MAB = TheTarget->createMCAsmBackend(*MRI, TripleName, ""); |
| 333 | if (!MAB) |
| 334 | return error("no asm backend for target " + TripleName, Context); |
| 335 | |
| 336 | MII.reset(TheTarget->createMCInstrInfo()); |
| 337 | if (!MII) |
| 338 | return error("no instr info info for target " + TripleName, Context); |
| 339 | |
| 340 | MSTI.reset(TheTarget->createMCSubtargetInfo(TripleName, "", "")); |
| 341 | if (!MSTI) |
| 342 | return error("no subtarget info for target " + TripleName, Context); |
| 343 | |
Eric Christopher | 0169e42 | 2015-03-10 22:03:14 +0000 | [diff] [blame] | 344 | MCE = TheTarget->createMCCodeEmitter(*MII, *MRI, *MC); |
Frederic Riss | c99ea20 | 2015-02-28 00:29:11 +0000 | [diff] [blame] | 345 | if (!MCE) |
| 346 | return error("no code emitter for target " + TripleName, Context); |
| 347 | |
| 348 | // Create the output file. |
| 349 | std::error_code EC; |
Frederic Riss | b8b43d5 | 2015-03-04 22:07:44 +0000 | [diff] [blame] | 350 | OutFile = |
| 351 | llvm::make_unique<raw_fd_ostream>(OutputFilename, EC, sys::fs::F_None); |
Frederic Riss | c99ea20 | 2015-02-28 00:29:11 +0000 | [diff] [blame] | 352 | if (EC) |
| 353 | return error(Twine(OutputFilename) + ": " + EC.message(), Context); |
| 354 | |
| 355 | MS = TheTarget->createMCObjectStreamer(TripleName, *MC, *MAB, *OutFile, MCE, |
| 356 | *MSTI, false); |
| 357 | if (!MS) |
| 358 | return error("no object streamer for target " + TripleName, Context); |
| 359 | |
| 360 | // Finally create the AsmPrinter we'll use to emit the DIEs. |
| 361 | TM.reset(TheTarget->createTargetMachine(TripleName, "", "", TargetOptions())); |
| 362 | if (!TM) |
| 363 | return error("no target machine for target " + TripleName, Context); |
| 364 | |
| 365 | Asm.reset(TheTarget->createAsmPrinter(*TM, std::unique_ptr<MCStreamer>(MS))); |
| 366 | if (!Asm) |
| 367 | return error("no asm printer for target " + TripleName, Context); |
| 368 | |
| 369 | return true; |
| 370 | } |
| 371 | |
| 372 | bool DwarfStreamer::finish() { |
| 373 | MS->Finish(); |
| 374 | return true; |
| 375 | } |
| 376 | |
Frederic Riss | b8b43d5 | 2015-03-04 22:07:44 +0000 | [diff] [blame] | 377 | /// \brief Set the current output section to debug_info and change |
| 378 | /// the MC Dwarf version to \p DwarfVersion. |
| 379 | void DwarfStreamer::switchToDebugInfoSection(unsigned DwarfVersion) { |
| 380 | MS->SwitchSection(MOFI->getDwarfInfoSection()); |
| 381 | MC->setDwarfVersion(DwarfVersion); |
| 382 | } |
| 383 | |
| 384 | /// \brief Emit the compilation unit header for \p Unit in the |
| 385 | /// debug_info section. |
| 386 | /// |
| 387 | /// A Dwarf scetion header is encoded as: |
| 388 | /// uint32_t Unit length (omiting this field) |
| 389 | /// uint16_t Version |
| 390 | /// uint32_t Abbreviation table offset |
| 391 | /// uint8_t Address size |
| 392 | /// |
| 393 | /// Leading to a total of 11 bytes. |
| 394 | void DwarfStreamer::emitCompileUnitHeader(CompileUnit &Unit) { |
| 395 | unsigned Version = Unit.getOrigUnit().getVersion(); |
| 396 | switchToDebugInfoSection(Version); |
| 397 | |
| 398 | // Emit size of content not including length itself. The size has |
| 399 | // already been computed in CompileUnit::computeOffsets(). Substract |
| 400 | // 4 to that size to account for the length field. |
| 401 | Asm->EmitInt32(Unit.getNextUnitOffset() - Unit.getStartOffset() - 4); |
| 402 | Asm->EmitInt16(Version); |
| 403 | // We share one abbreviations table across all units so it's always at the |
| 404 | // start of the section. |
| 405 | Asm->EmitInt32(0); |
| 406 | Asm->EmitInt8(Unit.getOrigUnit().getAddressByteSize()); |
| 407 | } |
| 408 | |
| 409 | /// \brief Emit the \p Abbrevs array as the shared abbreviation table |
| 410 | /// for the linked Dwarf file. |
| 411 | void DwarfStreamer::emitAbbrevs(const std::vector<DIEAbbrev *> &Abbrevs) { |
| 412 | MS->SwitchSection(MOFI->getDwarfAbbrevSection()); |
| 413 | Asm->emitDwarfAbbrevs(Abbrevs); |
| 414 | } |
| 415 | |
| 416 | /// \brief Recursively emit the DIE tree rooted at \p Die. |
| 417 | void DwarfStreamer::emitDIE(DIE &Die) { |
| 418 | MS->SwitchSection(MOFI->getDwarfInfoSection()); |
| 419 | Asm->emitDwarfDIE(Die); |
| 420 | } |
| 421 | |
Frederic Riss | ef64846 | 2015-03-06 17:56:30 +0000 | [diff] [blame] | 422 | /// \brief Emit the debug_str section stored in \p Pool. |
| 423 | void DwarfStreamer::emitStrings(const NonRelocatableStringpool &Pool) { |
| 424 | Asm->OutStreamer.SwitchSection(MOFI->getDwarfStrSection()); |
| 425 | for (auto *Entry = Pool.getFirstEntry(); Entry; |
| 426 | Entry = Pool.getNextEntry(Entry)) |
| 427 | Asm->OutStreamer.EmitBytes( |
| 428 | StringRef(Entry->getKey().data(), Entry->getKey().size() + 1)); |
| 429 | } |
| 430 | |
Frederic Riss | d345518 | 2015-01-28 18:27:01 +0000 | [diff] [blame] | 431 | /// \brief The core of the Dwarf linking logic. |
Frederic Riss | 1036e64 | 2015-02-13 23:18:22 +0000 | [diff] [blame] | 432 | /// |
| 433 | /// The link of the dwarf information from the object files will be |
| 434 | /// driven by the selection of 'root DIEs', which are DIEs that |
| 435 | /// describe variables or functions that are present in the linked |
| 436 | /// binary (and thus have entries in the debug map). All the debug |
| 437 | /// information that will be linked (the DIEs, but also the line |
| 438 | /// tables, ranges, ...) is derived from that set of root DIEs. |
| 439 | /// |
| 440 | /// The root DIEs are identified because they contain relocations that |
| 441 | /// correspond to a debug map entry at specific places (the low_pc for |
| 442 | /// a function, the location for a variable). These relocations are |
| 443 | /// called ValidRelocs in the DwarfLinker and are gathered as a very |
| 444 | /// first step when we start processing a DebugMapObject. |
Frederic Riss | d345518 | 2015-01-28 18:27:01 +0000 | [diff] [blame] | 445 | class DwarfLinker { |
| 446 | public: |
Frederic Riss | b981832 | 2015-02-28 00:29:07 +0000 | [diff] [blame] | 447 | DwarfLinker(StringRef OutputFilename, const LinkOptions &Options) |
| 448 | : OutputFilename(OutputFilename), Options(Options), |
| 449 | BinHolder(Options.Verbose) {} |
Frederic Riss | d345518 | 2015-01-28 18:27:01 +0000 | [diff] [blame] | 450 | |
Frederic Riss | b8b43d5 | 2015-03-04 22:07:44 +0000 | [diff] [blame] | 451 | ~DwarfLinker() { |
| 452 | for (auto *Abbrev : Abbreviations) |
| 453 | delete Abbrev; |
| 454 | } |
| 455 | |
Frederic Riss | d345518 | 2015-01-28 18:27:01 +0000 | [diff] [blame] | 456 | /// \brief Link the contents of the DebugMap. |
| 457 | bool link(const DebugMap &); |
| 458 | |
| 459 | private: |
Frederic Riss | 563cba6 | 2015-01-28 22:15:14 +0000 | [diff] [blame] | 460 | /// \brief Called at the start of a debug object link. |
| 461 | void startDebugObject(DWARFContext &); |
| 462 | |
| 463 | /// \brief Called at the end of a debug object link. |
| 464 | void endDebugObject(); |
| 465 | |
Frederic Riss | 1036e64 | 2015-02-13 23:18:22 +0000 | [diff] [blame] | 466 | /// \defgroup FindValidRelocations Translate debug map into a list |
| 467 | /// of relevant relocations |
| 468 | /// |
| 469 | /// @{ |
| 470 | struct ValidReloc { |
| 471 | uint32_t Offset; |
| 472 | uint32_t Size; |
| 473 | uint64_t Addend; |
| 474 | const DebugMapObject::DebugMapEntry *Mapping; |
| 475 | |
| 476 | ValidReloc(uint32_t Offset, uint32_t Size, uint64_t Addend, |
| 477 | const DebugMapObject::DebugMapEntry *Mapping) |
| 478 | : Offset(Offset), Size(Size), Addend(Addend), Mapping(Mapping) {} |
| 479 | |
| 480 | bool operator<(const ValidReloc &RHS) const { return Offset < RHS.Offset; } |
| 481 | }; |
| 482 | |
| 483 | /// \brief The valid relocations for the current DebugMapObject. |
| 484 | /// This vector is sorted by relocation offset. |
| 485 | std::vector<ValidReloc> ValidRelocs; |
| 486 | |
| 487 | /// \brief Index into ValidRelocs of the next relocation to |
| 488 | /// consider. As we walk the DIEs in acsending file offset and as |
| 489 | /// ValidRelocs is sorted by file offset, keeping this index |
| 490 | /// uptodate is all we have to do to have a cheap lookup during the |
Frederic Riss | 23e20e9 | 2015-03-07 01:25:09 +0000 | [diff] [blame] | 491 | /// root DIE selection and during DIE cloning. |
Frederic Riss | 1036e64 | 2015-02-13 23:18:22 +0000 | [diff] [blame] | 492 | unsigned NextValidReloc; |
| 493 | |
| 494 | bool findValidRelocsInDebugInfo(const object::ObjectFile &Obj, |
| 495 | const DebugMapObject &DMO); |
| 496 | |
| 497 | bool findValidRelocs(const object::SectionRef &Section, |
| 498 | const object::ObjectFile &Obj, |
| 499 | const DebugMapObject &DMO); |
| 500 | |
| 501 | void findValidRelocsMachO(const object::SectionRef &Section, |
| 502 | const object::MachOObjectFile &Obj, |
| 503 | const DebugMapObject &DMO); |
| 504 | /// @} |
Frederic Riss | 1b9da42 | 2015-02-13 23:18:29 +0000 | [diff] [blame] | 505 | |
Frederic Riss | 84c09a5 | 2015-02-13 23:18:34 +0000 | [diff] [blame] | 506 | /// \defgroup FindRootDIEs Find DIEs corresponding to debug map entries. |
| 507 | /// |
| 508 | /// @{ |
| 509 | /// \brief Recursively walk the \p DIE tree and look for DIEs to |
| 510 | /// keep. Store that information in \p CU's DIEInfo. |
| 511 | void lookForDIEsToKeep(const DWARFDebugInfoEntryMinimal &DIE, |
| 512 | const DebugMapObject &DMO, CompileUnit &CU, |
| 513 | unsigned Flags); |
| 514 | |
| 515 | /// \brief Flags passed to DwarfLinker::lookForDIEsToKeep |
| 516 | enum TravesalFlags { |
| 517 | TF_Keep = 1 << 0, ///< Mark the traversed DIEs as kept. |
| 518 | TF_InFunctionScope = 1 << 1, ///< Current scope is a fucntion scope. |
| 519 | TF_DependencyWalk = 1 << 2, ///< Walking the dependencies of a kept DIE. |
| 520 | TF_ParentWalk = 1 << 3, ///< Walking up the parents of a kept DIE. |
| 521 | }; |
| 522 | |
| 523 | /// \brief Mark the passed DIE as well as all the ones it depends on |
| 524 | /// as kept. |
| 525 | void keepDIEAndDenpendencies(const DWARFDebugInfoEntryMinimal &DIE, |
| 526 | CompileUnit::DIEInfo &MyInfo, |
| 527 | const DebugMapObject &DMO, CompileUnit &CU, |
| 528 | unsigned Flags); |
| 529 | |
| 530 | unsigned shouldKeepDIE(const DWARFDebugInfoEntryMinimal &DIE, |
| 531 | CompileUnit &Unit, CompileUnit::DIEInfo &MyInfo, |
| 532 | unsigned Flags); |
| 533 | |
| 534 | unsigned shouldKeepVariableDIE(const DWARFDebugInfoEntryMinimal &DIE, |
| 535 | CompileUnit &Unit, |
| 536 | CompileUnit::DIEInfo &MyInfo, unsigned Flags); |
| 537 | |
| 538 | unsigned shouldKeepSubprogramDIE(const DWARFDebugInfoEntryMinimal &DIE, |
| 539 | CompileUnit &Unit, |
| 540 | CompileUnit::DIEInfo &MyInfo, |
| 541 | unsigned Flags); |
| 542 | |
| 543 | bool hasValidRelocation(uint32_t StartOffset, uint32_t EndOffset, |
| 544 | CompileUnit::DIEInfo &Info); |
| 545 | /// @} |
| 546 | |
Frederic Riss | b8b43d5 | 2015-03-04 22:07:44 +0000 | [diff] [blame] | 547 | /// \defgroup Linking Methods used to link the debug information |
| 548 | /// |
| 549 | /// @{ |
| 550 | /// \brief Recursively clone \p InputDIE into an tree of DIE objects |
| 551 | /// where useless (as decided by lookForDIEsToKeep()) bits have been |
| 552 | /// stripped out and addresses have been rewritten according to the |
| 553 | /// debug map. |
| 554 | /// |
| 555 | /// \param OutOffset is the offset the cloned DIE in the output |
| 556 | /// compile unit. |
Frederic Riss | 31da324 | 2015-03-11 18:45:52 +0000 | [diff] [blame] | 557 | /// \param PCOffset (while cloning a function scope) is the offset |
| 558 | /// applied to the entry point of the function to get the linked address. |
Frederic Riss | b8b43d5 | 2015-03-04 22:07:44 +0000 | [diff] [blame] | 559 | /// |
| 560 | /// \returns the root of the cloned tree. |
| 561 | DIE *cloneDIE(const DWARFDebugInfoEntryMinimal &InputDIE, CompileUnit &U, |
Frederic Riss | 31da324 | 2015-03-11 18:45:52 +0000 | [diff] [blame] | 562 | int64_t PCOffset, uint32_t OutOffset); |
Frederic Riss | b8b43d5 | 2015-03-04 22:07:44 +0000 | [diff] [blame] | 563 | |
| 564 | typedef DWARFAbbreviationDeclaration::AttributeSpec AttributeSpec; |
| 565 | |
Frederic Riss | 31da324 | 2015-03-11 18:45:52 +0000 | [diff] [blame] | 566 | /// \brief Information gathered and exchanged between the various |
| 567 | /// clone*Attributes helpers about the attributes of a particular DIE. |
| 568 | struct AttributesInfo { |
| 569 | uint64_t OrigHighPc; ///< Value of AT_high_pc in the input DIE |
| 570 | int64_t PCOffset; ///< Offset to apply to PC addresses inside a function. |
| 571 | |
| 572 | AttributesInfo() : OrigHighPc(0), PCOffset(0) {} |
| 573 | }; |
| 574 | |
Frederic Riss | b8b43d5 | 2015-03-04 22:07:44 +0000 | [diff] [blame] | 575 | /// \brief Helper for cloneDIE. |
| 576 | unsigned cloneAttribute(DIE &Die, const DWARFDebugInfoEntryMinimal &InputDIE, |
| 577 | CompileUnit &U, const DWARFFormValue &Val, |
Frederic Riss | 31da324 | 2015-03-11 18:45:52 +0000 | [diff] [blame] | 578 | const AttributeSpec AttrSpec, unsigned AttrSize, |
| 579 | AttributesInfo &AttrInfo); |
Frederic Riss | b8b43d5 | 2015-03-04 22:07:44 +0000 | [diff] [blame] | 580 | |
| 581 | /// \brief Helper for cloneDIE. |
Frederic Riss | ef64846 | 2015-03-06 17:56:30 +0000 | [diff] [blame] | 582 | unsigned cloneStringAttribute(DIE &Die, AttributeSpec AttrSpec, |
| 583 | const DWARFFormValue &Val, const DWARFUnit &U); |
Frederic Riss | b8b43d5 | 2015-03-04 22:07:44 +0000 | [diff] [blame] | 584 | |
| 585 | /// \brief Helper for cloneDIE. |
Frederic Riss | 9833de6 | 2015-03-06 23:22:53 +0000 | [diff] [blame] | 586 | unsigned |
| 587 | cloneDieReferenceAttribute(DIE &Die, |
| 588 | const DWARFDebugInfoEntryMinimal &InputDIE, |
| 589 | AttributeSpec AttrSpec, unsigned AttrSize, |
| 590 | const DWARFFormValue &Val, const DWARFUnit &U); |
Frederic Riss | b8b43d5 | 2015-03-04 22:07:44 +0000 | [diff] [blame] | 591 | |
| 592 | /// \brief Helper for cloneDIE. |
| 593 | unsigned cloneBlockAttribute(DIE &Die, AttributeSpec AttrSpec, |
| 594 | const DWARFFormValue &Val, unsigned AttrSize); |
| 595 | |
| 596 | /// \brief Helper for cloneDIE. |
Frederic Riss | 31da324 | 2015-03-11 18:45:52 +0000 | [diff] [blame] | 597 | unsigned cloneAddressAttribute(DIE &Die, AttributeSpec AttrSpec, |
| 598 | const DWARFFormValue &Val, |
| 599 | const CompileUnit &Unit, AttributesInfo &Info); |
| 600 | |
| 601 | /// \brief Helper for cloneDIE. |
Frederic Riss | b8b43d5 | 2015-03-04 22:07:44 +0000 | [diff] [blame] | 602 | unsigned cloneScalarAttribute(DIE &Die, |
| 603 | const DWARFDebugInfoEntryMinimal &InputDIE, |
| 604 | const DWARFUnit &U, AttributeSpec AttrSpec, |
| 605 | const DWARFFormValue &Val, unsigned AttrSize); |
| 606 | |
Frederic Riss | 23e20e9 | 2015-03-07 01:25:09 +0000 | [diff] [blame] | 607 | /// \brief Helper for cloneDIE. |
| 608 | bool applyValidRelocs(MutableArrayRef<char> Data, uint32_t BaseOffset, |
| 609 | bool isLittleEndian); |
| 610 | |
Frederic Riss | b8b43d5 | 2015-03-04 22:07:44 +0000 | [diff] [blame] | 611 | /// \brief Assign an abbreviation number to \p Abbrev |
| 612 | void AssignAbbrev(DIEAbbrev &Abbrev); |
| 613 | |
| 614 | /// \brief FoldingSet that uniques the abbreviations. |
| 615 | FoldingSet<DIEAbbrev> AbbreviationsSet; |
| 616 | /// \brief Storage for the unique Abbreviations. |
| 617 | /// This is passed to AsmPrinter::emitDwarfAbbrevs(), thus it cannot |
| 618 | /// be changed to a vecot of unique_ptrs. |
| 619 | std::vector<DIEAbbrev *> Abbreviations; |
| 620 | |
| 621 | /// \brief DIELoc objects that need to be destructed (but not freed!). |
| 622 | std::vector<DIELoc *> DIELocs; |
| 623 | /// \brief DIEBlock objects that need to be destructed (but not freed!). |
| 624 | std::vector<DIEBlock *> DIEBlocks; |
| 625 | /// \brief Allocator used for all the DIEValue objects. |
| 626 | BumpPtrAllocator DIEAlloc; |
| 627 | /// @} |
| 628 | |
Frederic Riss | 1b9da42 | 2015-02-13 23:18:29 +0000 | [diff] [blame] | 629 | /// \defgroup Helpers Various helper methods. |
| 630 | /// |
| 631 | /// @{ |
| 632 | const DWARFDebugInfoEntryMinimal * |
| 633 | resolveDIEReference(DWARFFormValue &RefValue, const DWARFUnit &Unit, |
| 634 | const DWARFDebugInfoEntryMinimal &DIE, |
| 635 | CompileUnit *&ReferencedCU); |
| 636 | |
| 637 | CompileUnit *getUnitForOffset(unsigned Offset); |
| 638 | |
| 639 | void reportWarning(const Twine &Warning, const DWARFUnit *Unit = nullptr, |
| 640 | const DWARFDebugInfoEntryMinimal *DIE = nullptr); |
Frederic Riss | c99ea20 | 2015-02-28 00:29:11 +0000 | [diff] [blame] | 641 | |
| 642 | bool createStreamer(Triple TheTriple, StringRef OutputFilename); |
Frederic Riss | 1b9da42 | 2015-02-13 23:18:29 +0000 | [diff] [blame] | 643 | /// @} |
| 644 | |
Frederic Riss | 563cba6 | 2015-01-28 22:15:14 +0000 | [diff] [blame] | 645 | private: |
Frederic Riss | d345518 | 2015-01-28 18:27:01 +0000 | [diff] [blame] | 646 | std::string OutputFilename; |
Frederic Riss | b981832 | 2015-02-28 00:29:07 +0000 | [diff] [blame] | 647 | LinkOptions Options; |
Frederic Riss | d345518 | 2015-01-28 18:27:01 +0000 | [diff] [blame] | 648 | BinaryHolder BinHolder; |
Frederic Riss | c99ea20 | 2015-02-28 00:29:11 +0000 | [diff] [blame] | 649 | std::unique_ptr<DwarfStreamer> Streamer; |
Frederic Riss | 563cba6 | 2015-01-28 22:15:14 +0000 | [diff] [blame] | 650 | |
| 651 | /// The units of the current debug map object. |
| 652 | std::vector<CompileUnit> Units; |
Frederic Riss | 1b9da42 | 2015-02-13 23:18:29 +0000 | [diff] [blame] | 653 | |
| 654 | /// The debug map object curently under consideration. |
| 655 | DebugMapObject *CurrentDebugObject; |
Frederic Riss | ef64846 | 2015-03-06 17:56:30 +0000 | [diff] [blame] | 656 | |
| 657 | /// \brief The Dwarf string pool |
| 658 | NonRelocatableStringpool StringPool; |
Frederic Riss | d345518 | 2015-01-28 18:27:01 +0000 | [diff] [blame] | 659 | }; |
| 660 | |
Frederic Riss | 1b9da42 | 2015-02-13 23:18:29 +0000 | [diff] [blame] | 661 | /// \brief Similar to DWARFUnitSection::getUnitForOffset(), but |
| 662 | /// returning our CompileUnit object instead. |
| 663 | CompileUnit *DwarfLinker::getUnitForOffset(unsigned Offset) { |
| 664 | auto CU = |
| 665 | std::upper_bound(Units.begin(), Units.end(), Offset, |
| 666 | [](uint32_t LHS, const CompileUnit &RHS) { |
| 667 | return LHS < RHS.getOrigUnit().getNextUnitOffset(); |
| 668 | }); |
| 669 | return CU != Units.end() ? &*CU : nullptr; |
| 670 | } |
| 671 | |
| 672 | /// \brief Resolve the DIE attribute reference that has been |
| 673 | /// extracted in \p RefValue. The resulting DIE migh be in another |
| 674 | /// CompileUnit which is stored into \p ReferencedCU. |
| 675 | /// \returns null if resolving fails for any reason. |
| 676 | const DWARFDebugInfoEntryMinimal *DwarfLinker::resolveDIEReference( |
| 677 | DWARFFormValue &RefValue, const DWARFUnit &Unit, |
| 678 | const DWARFDebugInfoEntryMinimal &DIE, CompileUnit *&RefCU) { |
| 679 | assert(RefValue.isFormClass(DWARFFormValue::FC_Reference)); |
| 680 | uint64_t RefOffset = *RefValue.getAsReference(&Unit); |
| 681 | |
| 682 | if ((RefCU = getUnitForOffset(RefOffset))) |
| 683 | if (const auto *RefDie = RefCU->getOrigUnit().getDIEForOffset(RefOffset)) |
| 684 | return RefDie; |
| 685 | |
| 686 | reportWarning("could not find referenced DIE", &Unit, &DIE); |
| 687 | return nullptr; |
| 688 | } |
| 689 | |
| 690 | /// \brief Report a warning to the user, optionaly including |
| 691 | /// information about a specific \p DIE related to the warning. |
| 692 | void DwarfLinker::reportWarning(const Twine &Warning, const DWARFUnit *Unit, |
| 693 | const DWARFDebugInfoEntryMinimal *DIE) { |
Frederic Riss | def4fb7 | 2015-02-28 00:29:01 +0000 | [diff] [blame] | 694 | StringRef Context = "<debug map>"; |
Frederic Riss | 1b9da42 | 2015-02-13 23:18:29 +0000 | [diff] [blame] | 695 | if (CurrentDebugObject) |
Frederic Riss | def4fb7 | 2015-02-28 00:29:01 +0000 | [diff] [blame] | 696 | Context = CurrentDebugObject->getObjectFilename(); |
| 697 | warn(Warning, Context); |
Frederic Riss | 1b9da42 | 2015-02-13 23:18:29 +0000 | [diff] [blame] | 698 | |
Frederic Riss | b981832 | 2015-02-28 00:29:07 +0000 | [diff] [blame] | 699 | if (!Options.Verbose || !DIE) |
Frederic Riss | 1b9da42 | 2015-02-13 23:18:29 +0000 | [diff] [blame] | 700 | return; |
| 701 | |
| 702 | errs() << " in DIE:\n"; |
| 703 | DIE->dump(errs(), const_cast<DWARFUnit *>(Unit), 0 /* RecurseDepth */, |
| 704 | 6 /* Indent */); |
| 705 | } |
| 706 | |
Frederic Riss | c99ea20 | 2015-02-28 00:29:11 +0000 | [diff] [blame] | 707 | bool DwarfLinker::createStreamer(Triple TheTriple, StringRef OutputFilename) { |
| 708 | if (Options.NoOutput) |
| 709 | return true; |
| 710 | |
Frederic Riss | b52cf52 | 2015-02-28 00:42:37 +0000 | [diff] [blame] | 711 | Streamer = llvm::make_unique<DwarfStreamer>(); |
Frederic Riss | c99ea20 | 2015-02-28 00:29:11 +0000 | [diff] [blame] | 712 | return Streamer->init(TheTriple, OutputFilename); |
| 713 | } |
| 714 | |
Frederic Riss | 563cba6 | 2015-01-28 22:15:14 +0000 | [diff] [blame] | 715 | /// \brief Recursive helper to gather the child->parent relationships in the |
| 716 | /// original compile unit. |
Frederic Riss | 9aa725b | 2015-02-13 23:18:31 +0000 | [diff] [blame] | 717 | static void gatherDIEParents(const DWARFDebugInfoEntryMinimal *DIE, |
| 718 | unsigned ParentIdx, CompileUnit &CU) { |
Frederic Riss | 563cba6 | 2015-01-28 22:15:14 +0000 | [diff] [blame] | 719 | unsigned MyIdx = CU.getOrigUnit().getDIEIndex(DIE); |
| 720 | CU.getInfo(MyIdx).ParentIdx = ParentIdx; |
| 721 | |
| 722 | if (DIE->hasChildren()) |
| 723 | for (auto *Child = DIE->getFirstChild(); Child && !Child->isNULL(); |
| 724 | Child = Child->getSibling()) |
Frederic Riss | 9aa725b | 2015-02-13 23:18:31 +0000 | [diff] [blame] | 725 | gatherDIEParents(Child, MyIdx, CU); |
Frederic Riss | 563cba6 | 2015-01-28 22:15:14 +0000 | [diff] [blame] | 726 | } |
| 727 | |
Frederic Riss | 84c09a5 | 2015-02-13 23:18:34 +0000 | [diff] [blame] | 728 | static bool dieNeedsChildrenToBeMeaningful(uint32_t Tag) { |
| 729 | switch (Tag) { |
| 730 | default: |
| 731 | return false; |
| 732 | case dwarf::DW_TAG_subprogram: |
| 733 | case dwarf::DW_TAG_lexical_block: |
| 734 | case dwarf::DW_TAG_subroutine_type: |
| 735 | case dwarf::DW_TAG_structure_type: |
| 736 | case dwarf::DW_TAG_class_type: |
| 737 | case dwarf::DW_TAG_union_type: |
| 738 | return true; |
| 739 | } |
| 740 | llvm_unreachable("Invalid Tag"); |
| 741 | } |
| 742 | |
Frederic Riss | 563cba6 | 2015-01-28 22:15:14 +0000 | [diff] [blame] | 743 | void DwarfLinker::startDebugObject(DWARFContext &Dwarf) { |
| 744 | Units.reserve(Dwarf.getNumCompileUnits()); |
Frederic Riss | 1036e64 | 2015-02-13 23:18:22 +0000 | [diff] [blame] | 745 | NextValidReloc = 0; |
Frederic Riss | 563cba6 | 2015-01-28 22:15:14 +0000 | [diff] [blame] | 746 | } |
| 747 | |
Frederic Riss | 1036e64 | 2015-02-13 23:18:22 +0000 | [diff] [blame] | 748 | void DwarfLinker::endDebugObject() { |
| 749 | Units.clear(); |
| 750 | ValidRelocs.clear(); |
Frederic Riss | b8b43d5 | 2015-03-04 22:07:44 +0000 | [diff] [blame] | 751 | |
| 752 | for (auto *Block : DIEBlocks) |
| 753 | Block->~DIEBlock(); |
| 754 | for (auto *Loc : DIELocs) |
| 755 | Loc->~DIELoc(); |
| 756 | |
| 757 | DIEBlocks.clear(); |
| 758 | DIELocs.clear(); |
| 759 | DIEAlloc.Reset(); |
Frederic Riss | 1036e64 | 2015-02-13 23:18:22 +0000 | [diff] [blame] | 760 | } |
| 761 | |
| 762 | /// \brief Iterate over the relocations of the given \p Section and |
| 763 | /// store the ones that correspond to debug map entries into the |
| 764 | /// ValidRelocs array. |
| 765 | void DwarfLinker::findValidRelocsMachO(const object::SectionRef &Section, |
| 766 | const object::MachOObjectFile &Obj, |
| 767 | const DebugMapObject &DMO) { |
| 768 | StringRef Contents; |
| 769 | Section.getContents(Contents); |
| 770 | DataExtractor Data(Contents, Obj.isLittleEndian(), 0); |
| 771 | |
| 772 | for (const object::RelocationRef &Reloc : Section.relocations()) { |
| 773 | object::DataRefImpl RelocDataRef = Reloc.getRawDataRefImpl(); |
| 774 | MachO::any_relocation_info MachOReloc = Obj.getRelocation(RelocDataRef); |
| 775 | unsigned RelocSize = 1 << Obj.getAnyRelocationLength(MachOReloc); |
| 776 | uint64_t Offset64; |
| 777 | if ((RelocSize != 4 && RelocSize != 8) || Reloc.getOffset(Offset64)) { |
Frederic Riss | 1b9da42 | 2015-02-13 23:18:29 +0000 | [diff] [blame] | 778 | reportWarning(" unsupported relocation in debug_info section."); |
Frederic Riss | 1036e64 | 2015-02-13 23:18:22 +0000 | [diff] [blame] | 779 | continue; |
| 780 | } |
| 781 | uint32_t Offset = Offset64; |
| 782 | // Mach-o uses REL relocations, the addend is at the relocation offset. |
| 783 | uint64_t Addend = Data.getUnsigned(&Offset, RelocSize); |
| 784 | |
| 785 | auto Sym = Reloc.getSymbol(); |
| 786 | if (Sym != Obj.symbol_end()) { |
| 787 | StringRef SymbolName; |
| 788 | if (Sym->getName(SymbolName)) { |
Frederic Riss | 1b9da42 | 2015-02-13 23:18:29 +0000 | [diff] [blame] | 789 | reportWarning("error getting relocation symbol name."); |
Frederic Riss | 1036e64 | 2015-02-13 23:18:22 +0000 | [diff] [blame] | 790 | continue; |
| 791 | } |
| 792 | if (const auto *Mapping = DMO.lookupSymbol(SymbolName)) |
| 793 | ValidRelocs.emplace_back(Offset64, RelocSize, Addend, Mapping); |
| 794 | } else if (const auto *Mapping = DMO.lookupObjectAddress(Addend)) { |
| 795 | // Do not store the addend. The addend was the address of the |
| 796 | // symbol in the object file, the address in the binary that is |
| 797 | // stored in the debug map doesn't need to be offseted. |
| 798 | ValidRelocs.emplace_back(Offset64, RelocSize, 0, Mapping); |
| 799 | } |
| 800 | } |
| 801 | } |
| 802 | |
| 803 | /// \brief Dispatch the valid relocation finding logic to the |
| 804 | /// appropriate handler depending on the object file format. |
| 805 | bool DwarfLinker::findValidRelocs(const object::SectionRef &Section, |
| 806 | const object::ObjectFile &Obj, |
| 807 | const DebugMapObject &DMO) { |
| 808 | // Dispatch to the right handler depending on the file type. |
| 809 | if (auto *MachOObj = dyn_cast<object::MachOObjectFile>(&Obj)) |
| 810 | findValidRelocsMachO(Section, *MachOObj, DMO); |
| 811 | else |
Frederic Riss | 1b9da42 | 2015-02-13 23:18:29 +0000 | [diff] [blame] | 812 | reportWarning(Twine("unsupported object file type: ") + Obj.getFileName()); |
Frederic Riss | 1036e64 | 2015-02-13 23:18:22 +0000 | [diff] [blame] | 813 | |
| 814 | if (ValidRelocs.empty()) |
| 815 | return false; |
| 816 | |
| 817 | // Sort the relocations by offset. We will walk the DIEs linearly in |
| 818 | // the file, this allows us to just keep an index in the relocation |
| 819 | // array that we advance during our walk, rather than resorting to |
| 820 | // some associative container. See DwarfLinker::NextValidReloc. |
| 821 | std::sort(ValidRelocs.begin(), ValidRelocs.end()); |
| 822 | return true; |
| 823 | } |
| 824 | |
| 825 | /// \brief Look for relocations in the debug_info section that match |
| 826 | /// entries in the debug map. These relocations will drive the Dwarf |
| 827 | /// link by indicating which DIEs refer to symbols present in the |
| 828 | /// linked binary. |
| 829 | /// \returns wether there are any valid relocations in the debug info. |
| 830 | bool DwarfLinker::findValidRelocsInDebugInfo(const object::ObjectFile &Obj, |
| 831 | const DebugMapObject &DMO) { |
| 832 | // Find the debug_info section. |
| 833 | for (const object::SectionRef &Section : Obj.sections()) { |
| 834 | StringRef SectionName; |
| 835 | Section.getName(SectionName); |
| 836 | SectionName = SectionName.substr(SectionName.find_first_not_of("._")); |
| 837 | if (SectionName != "debug_info") |
| 838 | continue; |
| 839 | return findValidRelocs(Section, Obj, DMO); |
| 840 | } |
| 841 | return false; |
| 842 | } |
Frederic Riss | 563cba6 | 2015-01-28 22:15:14 +0000 | [diff] [blame] | 843 | |
Frederic Riss | 84c09a5 | 2015-02-13 23:18:34 +0000 | [diff] [blame] | 844 | /// \brief Checks that there is a relocation against an actual debug |
| 845 | /// map entry between \p StartOffset and \p NextOffset. |
| 846 | /// |
| 847 | /// This function must be called with offsets in strictly ascending |
| 848 | /// order because it never looks back at relocations it already 'went past'. |
| 849 | /// \returns true and sets Info.InDebugMap if it is the case. |
| 850 | bool DwarfLinker::hasValidRelocation(uint32_t StartOffset, uint32_t EndOffset, |
| 851 | CompileUnit::DIEInfo &Info) { |
| 852 | assert(NextValidReloc == 0 || |
| 853 | StartOffset > ValidRelocs[NextValidReloc - 1].Offset); |
| 854 | if (NextValidReloc >= ValidRelocs.size()) |
| 855 | return false; |
| 856 | |
| 857 | uint64_t RelocOffset = ValidRelocs[NextValidReloc].Offset; |
| 858 | |
| 859 | // We might need to skip some relocs that we didn't consider. For |
| 860 | // example the high_pc of a discarded DIE might contain a reloc that |
| 861 | // is in the list because it actually corresponds to the start of a |
| 862 | // function that is in the debug map. |
| 863 | while (RelocOffset < StartOffset && NextValidReloc < ValidRelocs.size() - 1) |
| 864 | RelocOffset = ValidRelocs[++NextValidReloc].Offset; |
| 865 | |
| 866 | if (RelocOffset < StartOffset || RelocOffset >= EndOffset) |
| 867 | return false; |
| 868 | |
| 869 | const auto &ValidReloc = ValidRelocs[NextValidReloc++]; |
Frederic Riss | b981832 | 2015-02-28 00:29:07 +0000 | [diff] [blame] | 870 | if (Options.Verbose) |
Frederic Riss | 84c09a5 | 2015-02-13 23:18:34 +0000 | [diff] [blame] | 871 | outs() << "Found valid debug map entry: " << ValidReloc.Mapping->getKey() |
| 872 | << " " << format("\t%016" PRIx64 " => %016" PRIx64, |
| 873 | ValidReloc.Mapping->getValue().ObjectAddress, |
| 874 | ValidReloc.Mapping->getValue().BinaryAddress); |
| 875 | |
Frederic Riss | 31da324 | 2015-03-11 18:45:52 +0000 | [diff] [blame] | 876 | Info.AddrAdjust = int64_t(ValidReloc.Mapping->getValue().BinaryAddress) + |
| 877 | ValidReloc.Addend - |
| 878 | ValidReloc.Mapping->getValue().ObjectAddress; |
Frederic Riss | 84c09a5 | 2015-02-13 23:18:34 +0000 | [diff] [blame] | 879 | Info.InDebugMap = true; |
| 880 | return true; |
| 881 | } |
| 882 | |
| 883 | /// \brief Get the starting and ending (exclusive) offset for the |
| 884 | /// attribute with index \p Idx descibed by \p Abbrev. \p Offset is |
| 885 | /// supposed to point to the position of the first attribute described |
| 886 | /// by \p Abbrev. |
| 887 | /// \return [StartOffset, EndOffset) as a pair. |
| 888 | static std::pair<uint32_t, uint32_t> |
| 889 | getAttributeOffsets(const DWARFAbbreviationDeclaration *Abbrev, unsigned Idx, |
| 890 | unsigned Offset, const DWARFUnit &Unit) { |
| 891 | DataExtractor Data = Unit.getDebugInfoExtractor(); |
| 892 | |
| 893 | for (unsigned i = 0; i < Idx; ++i) |
| 894 | DWARFFormValue::skipValue(Abbrev->getFormByIndex(i), Data, &Offset, &Unit); |
| 895 | |
| 896 | uint32_t End = Offset; |
| 897 | DWARFFormValue::skipValue(Abbrev->getFormByIndex(Idx), Data, &End, &Unit); |
| 898 | |
| 899 | return std::make_pair(Offset, End); |
| 900 | } |
| 901 | |
| 902 | /// \brief Check if a variable describing DIE should be kept. |
| 903 | /// \returns updated TraversalFlags. |
| 904 | unsigned DwarfLinker::shouldKeepVariableDIE( |
| 905 | const DWARFDebugInfoEntryMinimal &DIE, CompileUnit &Unit, |
| 906 | CompileUnit::DIEInfo &MyInfo, unsigned Flags) { |
| 907 | const auto *Abbrev = DIE.getAbbreviationDeclarationPtr(); |
| 908 | |
| 909 | // Global variables with constant value can always be kept. |
| 910 | if (!(Flags & TF_InFunctionScope) && |
| 911 | Abbrev->findAttributeIndex(dwarf::DW_AT_const_value) != -1U) { |
| 912 | MyInfo.InDebugMap = true; |
| 913 | return Flags | TF_Keep; |
| 914 | } |
| 915 | |
| 916 | uint32_t LocationIdx = Abbrev->findAttributeIndex(dwarf::DW_AT_location); |
| 917 | if (LocationIdx == -1U) |
| 918 | return Flags; |
| 919 | |
| 920 | uint32_t Offset = DIE.getOffset() + getULEB128Size(Abbrev->getCode()); |
| 921 | const DWARFUnit &OrigUnit = Unit.getOrigUnit(); |
| 922 | uint32_t LocationOffset, LocationEndOffset; |
| 923 | std::tie(LocationOffset, LocationEndOffset) = |
| 924 | getAttributeOffsets(Abbrev, LocationIdx, Offset, OrigUnit); |
| 925 | |
| 926 | // See if there is a relocation to a valid debug map entry inside |
| 927 | // this variable's location. The order is important here. We want to |
| 928 | // always check in the variable has a valid relocation, so that the |
| 929 | // DIEInfo is filled. However, we don't want a static variable in a |
| 930 | // function to force us to keep the enclosing function. |
| 931 | if (!hasValidRelocation(LocationOffset, LocationEndOffset, MyInfo) || |
| 932 | (Flags & TF_InFunctionScope)) |
| 933 | return Flags; |
| 934 | |
Frederic Riss | b981832 | 2015-02-28 00:29:07 +0000 | [diff] [blame] | 935 | if (Options.Verbose) |
Frederic Riss | 84c09a5 | 2015-02-13 23:18:34 +0000 | [diff] [blame] | 936 | DIE.dump(outs(), const_cast<DWARFUnit *>(&OrigUnit), 0, 8 /* Indent */); |
| 937 | |
| 938 | return Flags | TF_Keep; |
| 939 | } |
| 940 | |
| 941 | /// \brief Check if a function describing DIE should be kept. |
| 942 | /// \returns updated TraversalFlags. |
| 943 | unsigned DwarfLinker::shouldKeepSubprogramDIE( |
| 944 | const DWARFDebugInfoEntryMinimal &DIE, CompileUnit &Unit, |
| 945 | CompileUnit::DIEInfo &MyInfo, unsigned Flags) { |
| 946 | const auto *Abbrev = DIE.getAbbreviationDeclarationPtr(); |
| 947 | |
| 948 | Flags |= TF_InFunctionScope; |
| 949 | |
| 950 | uint32_t LowPcIdx = Abbrev->findAttributeIndex(dwarf::DW_AT_low_pc); |
| 951 | if (LowPcIdx == -1U) |
| 952 | return Flags; |
| 953 | |
| 954 | uint32_t Offset = DIE.getOffset() + getULEB128Size(Abbrev->getCode()); |
| 955 | const DWARFUnit &OrigUnit = Unit.getOrigUnit(); |
| 956 | uint32_t LowPcOffset, LowPcEndOffset; |
| 957 | std::tie(LowPcOffset, LowPcEndOffset) = |
| 958 | getAttributeOffsets(Abbrev, LowPcIdx, Offset, OrigUnit); |
| 959 | |
| 960 | uint64_t LowPc = |
| 961 | DIE.getAttributeValueAsAddress(&OrigUnit, dwarf::DW_AT_low_pc, -1ULL); |
| 962 | assert(LowPc != -1ULL && "low_pc attribute is not an address."); |
| 963 | if (LowPc == -1ULL || |
| 964 | !hasValidRelocation(LowPcOffset, LowPcEndOffset, MyInfo)) |
| 965 | return Flags; |
| 966 | |
Frederic Riss | b981832 | 2015-02-28 00:29:07 +0000 | [diff] [blame] | 967 | if (Options.Verbose) |
Frederic Riss | 84c09a5 | 2015-02-13 23:18:34 +0000 | [diff] [blame] | 968 | DIE.dump(outs(), const_cast<DWARFUnit *>(&OrigUnit), 0, 8 /* Indent */); |
| 969 | |
Frederic Riss | ce98470 | 2015-03-11 18:45:59 +0000 | [diff] [blame^] | 970 | Flags |= TF_Keep; |
| 971 | |
| 972 | DWARFFormValue HighPcValue; |
| 973 | if (!DIE.getAttributeValue(&OrigUnit, dwarf::DW_AT_high_pc, HighPcValue)) { |
| 974 | reportWarning("Function without high_pc. Range will be discarded.\n", |
| 975 | &OrigUnit, &DIE); |
| 976 | return Flags; |
| 977 | } |
| 978 | |
| 979 | uint64_t HighPc; |
| 980 | if (HighPcValue.isFormClass(DWARFFormValue::FC_Address)) { |
| 981 | HighPc = *HighPcValue.getAsAddress(&OrigUnit); |
| 982 | } else { |
| 983 | assert(HighPcValue.isFormClass(DWARFFormValue::FC_Constant)); |
| 984 | HighPc = LowPc + *HighPcValue.getAsUnsignedConstant(); |
| 985 | } |
| 986 | |
| 987 | Unit.addFunctionRange(LowPc, HighPc, MyInfo.AddrAdjust); |
| 988 | return Flags; |
Frederic Riss | 84c09a5 | 2015-02-13 23:18:34 +0000 | [diff] [blame] | 989 | } |
| 990 | |
| 991 | /// \brief Check if a DIE should be kept. |
| 992 | /// \returns updated TraversalFlags. |
| 993 | unsigned DwarfLinker::shouldKeepDIE(const DWARFDebugInfoEntryMinimal &DIE, |
| 994 | CompileUnit &Unit, |
| 995 | CompileUnit::DIEInfo &MyInfo, |
| 996 | unsigned Flags) { |
| 997 | switch (DIE.getTag()) { |
| 998 | case dwarf::DW_TAG_constant: |
| 999 | case dwarf::DW_TAG_variable: |
| 1000 | return shouldKeepVariableDIE(DIE, Unit, MyInfo, Flags); |
| 1001 | case dwarf::DW_TAG_subprogram: |
| 1002 | return shouldKeepSubprogramDIE(DIE, Unit, MyInfo, Flags); |
| 1003 | case dwarf::DW_TAG_module: |
| 1004 | case dwarf::DW_TAG_imported_module: |
| 1005 | case dwarf::DW_TAG_imported_declaration: |
| 1006 | case dwarf::DW_TAG_imported_unit: |
| 1007 | // We always want to keep these. |
| 1008 | return Flags | TF_Keep; |
| 1009 | } |
| 1010 | |
| 1011 | return Flags; |
| 1012 | } |
| 1013 | |
Frederic Riss | 84c09a5 | 2015-02-13 23:18:34 +0000 | [diff] [blame] | 1014 | /// \brief Mark the passed DIE as well as all the ones it depends on |
| 1015 | /// as kept. |
| 1016 | /// |
| 1017 | /// This function is called by lookForDIEsToKeep on DIEs that are |
| 1018 | /// newly discovered to be needed in the link. It recursively calls |
| 1019 | /// back to lookForDIEsToKeep while adding TF_DependencyWalk to the |
| 1020 | /// TraversalFlags to inform it that it's not doing the primary DIE |
| 1021 | /// tree walk. |
| 1022 | void DwarfLinker::keepDIEAndDenpendencies(const DWARFDebugInfoEntryMinimal &DIE, |
| 1023 | CompileUnit::DIEInfo &MyInfo, |
| 1024 | const DebugMapObject &DMO, |
| 1025 | CompileUnit &CU, unsigned Flags) { |
| 1026 | const DWARFUnit &Unit = CU.getOrigUnit(); |
| 1027 | MyInfo.Keep = true; |
| 1028 | |
| 1029 | // First mark all the parent chain as kept. |
| 1030 | unsigned AncestorIdx = MyInfo.ParentIdx; |
| 1031 | while (!CU.getInfo(AncestorIdx).Keep) { |
| 1032 | lookForDIEsToKeep(*Unit.getDIEAtIndex(AncestorIdx), DMO, CU, |
| 1033 | TF_ParentWalk | TF_Keep | TF_DependencyWalk); |
| 1034 | AncestorIdx = CU.getInfo(AncestorIdx).ParentIdx; |
| 1035 | } |
| 1036 | |
| 1037 | // Then we need to mark all the DIEs referenced by this DIE's |
| 1038 | // attributes as kept. |
| 1039 | DataExtractor Data = Unit.getDebugInfoExtractor(); |
| 1040 | const auto *Abbrev = DIE.getAbbreviationDeclarationPtr(); |
| 1041 | uint32_t Offset = DIE.getOffset() + getULEB128Size(Abbrev->getCode()); |
| 1042 | |
| 1043 | // Mark all DIEs referenced through atttributes as kept. |
| 1044 | for (const auto &AttrSpec : Abbrev->attributes()) { |
| 1045 | DWARFFormValue Val(AttrSpec.Form); |
| 1046 | |
| 1047 | if (!Val.isFormClass(DWARFFormValue::FC_Reference)) { |
| 1048 | DWARFFormValue::skipValue(AttrSpec.Form, Data, &Offset, &Unit); |
| 1049 | continue; |
| 1050 | } |
| 1051 | |
| 1052 | Val.extractValue(Data, &Offset, &Unit); |
| 1053 | CompileUnit *ReferencedCU; |
| 1054 | if (const auto *RefDIE = resolveDIEReference(Val, Unit, DIE, ReferencedCU)) |
| 1055 | lookForDIEsToKeep(*RefDIE, DMO, *ReferencedCU, |
| 1056 | TF_Keep | TF_DependencyWalk); |
| 1057 | } |
| 1058 | } |
| 1059 | |
| 1060 | /// \brief Recursively walk the \p DIE tree and look for DIEs to |
| 1061 | /// keep. Store that information in \p CU's DIEInfo. |
| 1062 | /// |
| 1063 | /// This function is the entry point of the DIE selection |
| 1064 | /// algorithm. It is expected to walk the DIE tree in file order and |
| 1065 | /// (though the mediation of its helper) call hasValidRelocation() on |
| 1066 | /// each DIE that might be a 'root DIE' (See DwarfLinker class |
| 1067 | /// comment). |
| 1068 | /// While walking the dependencies of root DIEs, this function is |
| 1069 | /// also called, but during these dependency walks the file order is |
| 1070 | /// not respected. The TF_DependencyWalk flag tells us which kind of |
| 1071 | /// traversal we are currently doing. |
| 1072 | void DwarfLinker::lookForDIEsToKeep(const DWARFDebugInfoEntryMinimal &DIE, |
| 1073 | const DebugMapObject &DMO, CompileUnit &CU, |
| 1074 | unsigned Flags) { |
| 1075 | unsigned Idx = CU.getOrigUnit().getDIEIndex(&DIE); |
| 1076 | CompileUnit::DIEInfo &MyInfo = CU.getInfo(Idx); |
| 1077 | bool AlreadyKept = MyInfo.Keep; |
| 1078 | |
| 1079 | // If the Keep flag is set, we are marking a required DIE's |
| 1080 | // dependencies. If our target is already marked as kept, we're all |
| 1081 | // set. |
| 1082 | if ((Flags & TF_DependencyWalk) && AlreadyKept) |
| 1083 | return; |
| 1084 | |
| 1085 | // We must not call shouldKeepDIE while called from keepDIEAndDenpendencies, |
| 1086 | // because it would screw up the relocation finding logic. |
| 1087 | if (!(Flags & TF_DependencyWalk)) |
| 1088 | Flags = shouldKeepDIE(DIE, CU, MyInfo, Flags); |
| 1089 | |
| 1090 | // If it is a newly kept DIE mark it as well as all its dependencies as kept. |
| 1091 | if (!AlreadyKept && (Flags & TF_Keep)) |
| 1092 | keepDIEAndDenpendencies(DIE, MyInfo, DMO, CU, Flags); |
| 1093 | |
| 1094 | // The TF_ParentWalk flag tells us that we are currently walking up |
| 1095 | // the parent chain of a required DIE, and we don't want to mark all |
| 1096 | // the children of the parents as kept (consider for example a |
| 1097 | // DW_TAG_namespace node in the parent chain). There are however a |
| 1098 | // set of DIE types for which we want to ignore that directive and still |
| 1099 | // walk their children. |
| 1100 | if (dieNeedsChildrenToBeMeaningful(DIE.getTag())) |
| 1101 | Flags &= ~TF_ParentWalk; |
| 1102 | |
| 1103 | if (!DIE.hasChildren() || (Flags & TF_ParentWalk)) |
| 1104 | return; |
| 1105 | |
| 1106 | for (auto *Child = DIE.getFirstChild(); Child && !Child->isNULL(); |
| 1107 | Child = Child->getSibling()) |
| 1108 | lookForDIEsToKeep(*Child, DMO, CU, Flags); |
| 1109 | } |
| 1110 | |
Frederic Riss | b8b43d5 | 2015-03-04 22:07:44 +0000 | [diff] [blame] | 1111 | /// \brief Assign an abbreviation numer to \p Abbrev. |
| 1112 | /// |
| 1113 | /// Our DIEs get freed after every DebugMapObject has been processed, |
| 1114 | /// thus the FoldingSet we use to unique DIEAbbrevs cannot refer to |
| 1115 | /// the instances hold by the DIEs. When we encounter an abbreviation |
| 1116 | /// that we don't know, we create a permanent copy of it. |
| 1117 | void DwarfLinker::AssignAbbrev(DIEAbbrev &Abbrev) { |
| 1118 | // Check the set for priors. |
| 1119 | FoldingSetNodeID ID; |
| 1120 | Abbrev.Profile(ID); |
| 1121 | void *InsertToken; |
| 1122 | DIEAbbrev *InSet = AbbreviationsSet.FindNodeOrInsertPos(ID, InsertToken); |
| 1123 | |
| 1124 | // If it's newly added. |
| 1125 | if (InSet) { |
| 1126 | // Assign existing abbreviation number. |
| 1127 | Abbrev.setNumber(InSet->getNumber()); |
| 1128 | } else { |
| 1129 | // Add to abbreviation list. |
| 1130 | Abbreviations.push_back( |
| 1131 | new DIEAbbrev(Abbrev.getTag(), Abbrev.hasChildren())); |
| 1132 | for (const auto &Attr : Abbrev.getData()) |
| 1133 | Abbreviations.back()->AddAttribute(Attr.getAttribute(), Attr.getForm()); |
| 1134 | AbbreviationsSet.InsertNode(Abbreviations.back(), InsertToken); |
| 1135 | // Assign the unique abbreviation number. |
| 1136 | Abbrev.setNumber(Abbreviations.size()); |
| 1137 | Abbreviations.back()->setNumber(Abbreviations.size()); |
| 1138 | } |
| 1139 | } |
| 1140 | |
| 1141 | /// \brief Clone a string attribute described by \p AttrSpec and add |
| 1142 | /// it to \p Die. |
| 1143 | /// \returns the size of the new attribute. |
Frederic Riss | ef64846 | 2015-03-06 17:56:30 +0000 | [diff] [blame] | 1144 | unsigned DwarfLinker::cloneStringAttribute(DIE &Die, AttributeSpec AttrSpec, |
| 1145 | const DWARFFormValue &Val, |
| 1146 | const DWARFUnit &U) { |
Frederic Riss | b8b43d5 | 2015-03-04 22:07:44 +0000 | [diff] [blame] | 1147 | // Switch everything to out of line strings. |
Frederic Riss | ef64846 | 2015-03-06 17:56:30 +0000 | [diff] [blame] | 1148 | const char *String = *Val.getAsCString(&U); |
| 1149 | unsigned Offset = StringPool.getStringOffset(String); |
Frederic Riss | b8b43d5 | 2015-03-04 22:07:44 +0000 | [diff] [blame] | 1150 | Die.addValue(dwarf::Attribute(AttrSpec.Attr), dwarf::DW_FORM_strp, |
Frederic Riss | ef64846 | 2015-03-06 17:56:30 +0000 | [diff] [blame] | 1151 | new (DIEAlloc) DIEInteger(Offset)); |
Frederic Riss | b8b43d5 | 2015-03-04 22:07:44 +0000 | [diff] [blame] | 1152 | return 4; |
| 1153 | } |
| 1154 | |
| 1155 | /// \brief Clone an attribute referencing another DIE and add |
| 1156 | /// it to \p Die. |
| 1157 | /// \returns the size of the new attribute. |
Frederic Riss | 9833de6 | 2015-03-06 23:22:53 +0000 | [diff] [blame] | 1158 | unsigned DwarfLinker::cloneDieReferenceAttribute( |
| 1159 | DIE &Die, const DWARFDebugInfoEntryMinimal &InputDIE, |
| 1160 | AttributeSpec AttrSpec, unsigned AttrSize, const DWARFFormValue &Val, |
| 1161 | const DWARFUnit &U) { |
| 1162 | uint32_t Ref = *Val.getAsReference(&U); |
| 1163 | DIE *NewRefDie = nullptr; |
| 1164 | CompileUnit *RefUnit = nullptr; |
| 1165 | const DWARFDebugInfoEntryMinimal *RefDie = nullptr; |
| 1166 | |
| 1167 | if (!(RefUnit = getUnitForOffset(Ref)) || |
| 1168 | !(RefDie = RefUnit->getOrigUnit().getDIEForOffset(Ref))) { |
| 1169 | const char *AttributeString = dwarf::AttributeString(AttrSpec.Attr); |
| 1170 | if (!AttributeString) |
| 1171 | AttributeString = "DW_AT_???"; |
| 1172 | reportWarning(Twine("Missing DIE for ref in attribute ") + AttributeString + |
| 1173 | ". Dropping.", |
| 1174 | &U, &InputDIE); |
| 1175 | return 0; |
| 1176 | } |
| 1177 | |
| 1178 | unsigned Idx = RefUnit->getOrigUnit().getDIEIndex(RefDie); |
| 1179 | CompileUnit::DIEInfo &RefInfo = RefUnit->getInfo(Idx); |
| 1180 | if (!RefInfo.Clone) { |
| 1181 | assert(Ref > InputDIE.getOffset()); |
| 1182 | // We haven't cloned this DIE yet. Just create an empty one and |
| 1183 | // store it. It'll get really cloned when we process it. |
| 1184 | RefInfo.Clone = new DIE(dwarf::Tag(RefDie->getTag())); |
| 1185 | } |
| 1186 | NewRefDie = RefInfo.Clone; |
| 1187 | |
| 1188 | if (AttrSpec.Form == dwarf::DW_FORM_ref_addr) { |
| 1189 | // We cannot currently rely on a DIEEntry to emit ref_addr |
| 1190 | // references, because the implementation calls back to DwarfDebug |
| 1191 | // to find the unit offset. (We don't have a DwarfDebug) |
| 1192 | // FIXME: we should be able to design DIEEntry reliance on |
| 1193 | // DwarfDebug away. |
| 1194 | DIEInteger *Attr; |
| 1195 | if (Ref < InputDIE.getOffset()) { |
| 1196 | // We must have already cloned that DIE. |
| 1197 | uint32_t NewRefOffset = |
| 1198 | RefUnit->getStartOffset() + NewRefDie->getOffset(); |
| 1199 | Attr = new (DIEAlloc) DIEInteger(NewRefOffset); |
| 1200 | } else { |
| 1201 | // A forward reference. Note and fixup later. |
| 1202 | Attr = new (DIEAlloc) DIEInteger(0xBADDEF); |
| 1203 | RefUnit->noteForwardReference(NewRefDie, Attr); |
| 1204 | } |
| 1205 | Die.addValue(dwarf::Attribute(AttrSpec.Attr), dwarf::DW_FORM_ref_addr, |
| 1206 | Attr); |
| 1207 | return AttrSize; |
| 1208 | } |
| 1209 | |
Frederic Riss | b8b43d5 | 2015-03-04 22:07:44 +0000 | [diff] [blame] | 1210 | Die.addValue(dwarf::Attribute(AttrSpec.Attr), dwarf::Form(AttrSpec.Form), |
Frederic Riss | 9833de6 | 2015-03-06 23:22:53 +0000 | [diff] [blame] | 1211 | new (DIEAlloc) DIEEntry(*NewRefDie)); |
Frederic Riss | b8b43d5 | 2015-03-04 22:07:44 +0000 | [diff] [blame] | 1212 | return AttrSize; |
| 1213 | } |
| 1214 | |
| 1215 | /// \brief Clone an attribute of block form (locations, constants) and add |
| 1216 | /// it to \p Die. |
| 1217 | /// \returns the size of the new attribute. |
| 1218 | unsigned DwarfLinker::cloneBlockAttribute(DIE &Die, AttributeSpec AttrSpec, |
| 1219 | const DWARFFormValue &Val, |
| 1220 | unsigned AttrSize) { |
| 1221 | DIE *Attr; |
| 1222 | DIEValue *Value; |
| 1223 | DIELoc *Loc = nullptr; |
| 1224 | DIEBlock *Block = nullptr; |
| 1225 | // Just copy the block data over. |
| 1226 | if (AttrSpec.Attr == dwarf::DW_FORM_exprloc) { |
| 1227 | Loc = new (DIEAlloc) DIELoc(); |
| 1228 | DIELocs.push_back(Loc); |
| 1229 | } else { |
| 1230 | Block = new (DIEAlloc) DIEBlock(); |
| 1231 | DIEBlocks.push_back(Block); |
| 1232 | } |
| 1233 | Attr = Loc ? static_cast<DIE *>(Loc) : static_cast<DIE *>(Block); |
| 1234 | Value = Loc ? static_cast<DIEValue *>(Loc) : static_cast<DIEValue *>(Block); |
| 1235 | ArrayRef<uint8_t> Bytes = *Val.getAsBlock(); |
| 1236 | for (auto Byte : Bytes) |
| 1237 | Attr->addValue(static_cast<dwarf::Attribute>(0), dwarf::DW_FORM_data1, |
| 1238 | new (DIEAlloc) DIEInteger(Byte)); |
| 1239 | // FIXME: If DIEBlock and DIELoc just reuses the Size field of |
| 1240 | // the DIE class, this if could be replaced by |
| 1241 | // Attr->setSize(Bytes.size()). |
| 1242 | if (Streamer) { |
| 1243 | if (Loc) |
| 1244 | Loc->ComputeSize(&Streamer->getAsmPrinter()); |
| 1245 | else |
| 1246 | Block->ComputeSize(&Streamer->getAsmPrinter()); |
| 1247 | } |
| 1248 | Die.addValue(dwarf::Attribute(AttrSpec.Attr), dwarf::Form(AttrSpec.Form), |
| 1249 | Value); |
| 1250 | return AttrSize; |
| 1251 | } |
| 1252 | |
Frederic Riss | 31da324 | 2015-03-11 18:45:52 +0000 | [diff] [blame] | 1253 | /// \brief Clone an address attribute and add it to \p Die. |
| 1254 | /// \returns the size of the new attribute. |
| 1255 | unsigned DwarfLinker::cloneAddressAttribute(DIE &Die, AttributeSpec AttrSpec, |
| 1256 | const DWARFFormValue &Val, |
| 1257 | const CompileUnit &Unit, |
| 1258 | AttributesInfo &Info) { |
| 1259 | int64_t Addr = *Val.getAsAddress(&Unit.getOrigUnit()); |
| 1260 | if (AttrSpec.Attr == dwarf::DW_AT_low_pc) { |
| 1261 | if (Die.getTag() == dwarf::DW_TAG_inlined_subroutine || |
| 1262 | Die.getTag() == dwarf::DW_TAG_lexical_block) |
| 1263 | Addr += Info.PCOffset; |
| 1264 | } else if (AttrSpec.Attr == dwarf::DW_AT_high_pc) { |
| 1265 | // If we have a high_pc recorded for the input DIE, use |
| 1266 | // it. Otherwise (when no relocations where applied) just use the |
| 1267 | // one we just decoded. |
| 1268 | Addr = (Info.OrigHighPc ? Info.OrigHighPc : Addr) + Info.PCOffset; |
| 1269 | } |
| 1270 | |
| 1271 | Die.addValue(static_cast<dwarf::Attribute>(AttrSpec.Attr), |
| 1272 | static_cast<dwarf::Form>(AttrSpec.Form), |
| 1273 | new (DIEAlloc) DIEInteger(Addr)); |
| 1274 | return Unit.getOrigUnit().getAddressByteSize(); |
| 1275 | } |
| 1276 | |
Frederic Riss | b8b43d5 | 2015-03-04 22:07:44 +0000 | [diff] [blame] | 1277 | /// \brief Clone a scalar attribute and add it to \p Die. |
| 1278 | /// \returns the size of the new attribute. |
| 1279 | unsigned DwarfLinker::cloneScalarAttribute( |
| 1280 | DIE &Die, const DWARFDebugInfoEntryMinimal &InputDIE, const DWARFUnit &U, |
| 1281 | AttributeSpec AttrSpec, const DWARFFormValue &Val, unsigned AttrSize) { |
| 1282 | uint64_t Value; |
| 1283 | if (AttrSpec.Form == dwarf::DW_FORM_sec_offset) |
| 1284 | Value = *Val.getAsSectionOffset(); |
| 1285 | else if (AttrSpec.Form == dwarf::DW_FORM_sdata) |
| 1286 | Value = *Val.getAsSignedConstant(); |
Frederic Riss | b8b43d5 | 2015-03-04 22:07:44 +0000 | [diff] [blame] | 1287 | else if (auto OptionalValue = Val.getAsUnsignedConstant()) |
| 1288 | Value = *OptionalValue; |
| 1289 | else { |
| 1290 | reportWarning("Unsupported scalar attribute form. Dropping attribute.", &U, |
| 1291 | &InputDIE); |
| 1292 | return 0; |
| 1293 | } |
| 1294 | Die.addValue(dwarf::Attribute(AttrSpec.Attr), dwarf::Form(AttrSpec.Form), |
| 1295 | new (DIEAlloc) DIEInteger(Value)); |
| 1296 | return AttrSize; |
| 1297 | } |
| 1298 | |
| 1299 | /// \brief Clone \p InputDIE's attribute described by \p AttrSpec with |
| 1300 | /// value \p Val, and add it to \p Die. |
| 1301 | /// \returns the size of the cloned attribute. |
| 1302 | unsigned DwarfLinker::cloneAttribute(DIE &Die, |
| 1303 | const DWARFDebugInfoEntryMinimal &InputDIE, |
| 1304 | CompileUnit &Unit, |
| 1305 | const DWARFFormValue &Val, |
| 1306 | const AttributeSpec AttrSpec, |
Frederic Riss | 31da324 | 2015-03-11 18:45:52 +0000 | [diff] [blame] | 1307 | unsigned AttrSize, AttributesInfo &Info) { |
Frederic Riss | b8b43d5 | 2015-03-04 22:07:44 +0000 | [diff] [blame] | 1308 | const DWARFUnit &U = Unit.getOrigUnit(); |
| 1309 | |
| 1310 | switch (AttrSpec.Form) { |
| 1311 | case dwarf::DW_FORM_strp: |
| 1312 | case dwarf::DW_FORM_string: |
Frederic Riss | ef64846 | 2015-03-06 17:56:30 +0000 | [diff] [blame] | 1313 | return cloneStringAttribute(Die, AttrSpec, Val, U); |
Frederic Riss | b8b43d5 | 2015-03-04 22:07:44 +0000 | [diff] [blame] | 1314 | case dwarf::DW_FORM_ref_addr: |
| 1315 | case dwarf::DW_FORM_ref1: |
| 1316 | case dwarf::DW_FORM_ref2: |
| 1317 | case dwarf::DW_FORM_ref4: |
| 1318 | case dwarf::DW_FORM_ref8: |
Frederic Riss | 9833de6 | 2015-03-06 23:22:53 +0000 | [diff] [blame] | 1319 | return cloneDieReferenceAttribute(Die, InputDIE, AttrSpec, AttrSize, Val, |
| 1320 | U); |
Frederic Riss | b8b43d5 | 2015-03-04 22:07:44 +0000 | [diff] [blame] | 1321 | case dwarf::DW_FORM_block: |
| 1322 | case dwarf::DW_FORM_block1: |
| 1323 | case dwarf::DW_FORM_block2: |
| 1324 | case dwarf::DW_FORM_block4: |
| 1325 | case dwarf::DW_FORM_exprloc: |
| 1326 | return cloneBlockAttribute(Die, AttrSpec, Val, AttrSize); |
| 1327 | case dwarf::DW_FORM_addr: |
Frederic Riss | 31da324 | 2015-03-11 18:45:52 +0000 | [diff] [blame] | 1328 | return cloneAddressAttribute(Die, AttrSpec, Val, Unit, Info); |
Frederic Riss | b8b43d5 | 2015-03-04 22:07:44 +0000 | [diff] [blame] | 1329 | case dwarf::DW_FORM_data1: |
| 1330 | case dwarf::DW_FORM_data2: |
| 1331 | case dwarf::DW_FORM_data4: |
| 1332 | case dwarf::DW_FORM_data8: |
| 1333 | case dwarf::DW_FORM_udata: |
| 1334 | case dwarf::DW_FORM_sdata: |
| 1335 | case dwarf::DW_FORM_sec_offset: |
| 1336 | case dwarf::DW_FORM_flag: |
| 1337 | case dwarf::DW_FORM_flag_present: |
| 1338 | return cloneScalarAttribute(Die, InputDIE, U, AttrSpec, Val, AttrSize); |
| 1339 | default: |
| 1340 | reportWarning("Unsupported attribute form in cloneAttribute. Dropping.", &U, |
| 1341 | &InputDIE); |
| 1342 | } |
| 1343 | |
| 1344 | return 0; |
| 1345 | } |
| 1346 | |
Frederic Riss | 23e20e9 | 2015-03-07 01:25:09 +0000 | [diff] [blame] | 1347 | /// \brief Apply the valid relocations found by findValidRelocs() to |
| 1348 | /// the buffer \p Data, taking into account that Data is at \p BaseOffset |
| 1349 | /// in the debug_info section. |
| 1350 | /// |
| 1351 | /// Like for findValidRelocs(), this function must be called with |
| 1352 | /// monotonic \p BaseOffset values. |
| 1353 | /// |
| 1354 | /// \returns wether any reloc has been applied. |
| 1355 | bool DwarfLinker::applyValidRelocs(MutableArrayRef<char> Data, |
| 1356 | uint32_t BaseOffset, bool isLittleEndian) { |
Aaron Ballman | 6b329f5 | 2015-03-07 15:16:27 +0000 | [diff] [blame] | 1357 | assert((NextValidReloc == 0 || |
Frederic Riss | aa983ce | 2015-03-11 18:45:57 +0000 | [diff] [blame] | 1358 | BaseOffset > ValidRelocs[NextValidReloc - 1].Offset) && |
| 1359 | "BaseOffset should only be increasing."); |
Frederic Riss | 23e20e9 | 2015-03-07 01:25:09 +0000 | [diff] [blame] | 1360 | if (NextValidReloc >= ValidRelocs.size()) |
| 1361 | return false; |
| 1362 | |
| 1363 | // Skip relocs that haven't been applied. |
| 1364 | while (NextValidReloc < ValidRelocs.size() && |
| 1365 | ValidRelocs[NextValidReloc].Offset < BaseOffset) |
| 1366 | ++NextValidReloc; |
| 1367 | |
| 1368 | bool Applied = false; |
| 1369 | uint64_t EndOffset = BaseOffset + Data.size(); |
| 1370 | while (NextValidReloc < ValidRelocs.size() && |
| 1371 | ValidRelocs[NextValidReloc].Offset >= BaseOffset && |
| 1372 | ValidRelocs[NextValidReloc].Offset < EndOffset) { |
| 1373 | const auto &ValidReloc = ValidRelocs[NextValidReloc++]; |
| 1374 | assert(ValidReloc.Offset - BaseOffset < Data.size()); |
| 1375 | assert(ValidReloc.Offset - BaseOffset + ValidReloc.Size <= Data.size()); |
| 1376 | char Buf[8]; |
| 1377 | uint64_t Value = ValidReloc.Mapping->getValue().BinaryAddress; |
| 1378 | Value += ValidReloc.Addend; |
| 1379 | for (unsigned i = 0; i != ValidReloc.Size; ++i) { |
| 1380 | unsigned Index = isLittleEndian ? i : (ValidReloc.Size - i - 1); |
| 1381 | Buf[i] = uint8_t(Value >> (Index * 8)); |
| 1382 | } |
| 1383 | assert(ValidReloc.Size <= sizeof(Buf)); |
| 1384 | memcpy(&Data[ValidReloc.Offset - BaseOffset], Buf, ValidReloc.Size); |
| 1385 | Applied = true; |
| 1386 | } |
| 1387 | |
| 1388 | return Applied; |
| 1389 | } |
| 1390 | |
Frederic Riss | b8b43d5 | 2015-03-04 22:07:44 +0000 | [diff] [blame] | 1391 | /// \brief Recursively clone \p InputDIE's subtrees that have been |
| 1392 | /// selected to appear in the linked output. |
| 1393 | /// |
| 1394 | /// \param OutOffset is the Offset where the newly created DIE will |
| 1395 | /// lie in the linked compile unit. |
| 1396 | /// |
| 1397 | /// \returns the cloned DIE object or null if nothing was selected. |
| 1398 | DIE *DwarfLinker::cloneDIE(const DWARFDebugInfoEntryMinimal &InputDIE, |
Frederic Riss | 31da324 | 2015-03-11 18:45:52 +0000 | [diff] [blame] | 1399 | CompileUnit &Unit, int64_t PCOffset, |
| 1400 | uint32_t OutOffset) { |
Frederic Riss | b8b43d5 | 2015-03-04 22:07:44 +0000 | [diff] [blame] | 1401 | DWARFUnit &U = Unit.getOrigUnit(); |
| 1402 | unsigned Idx = U.getDIEIndex(&InputDIE); |
Frederic Riss | 9833de6 | 2015-03-06 23:22:53 +0000 | [diff] [blame] | 1403 | CompileUnit::DIEInfo &Info = Unit.getInfo(Idx); |
Frederic Riss | b8b43d5 | 2015-03-04 22:07:44 +0000 | [diff] [blame] | 1404 | |
| 1405 | // Should the DIE appear in the output? |
| 1406 | if (!Unit.getInfo(Idx).Keep) |
| 1407 | return nullptr; |
| 1408 | |
| 1409 | uint32_t Offset = InputDIE.getOffset(); |
Frederic Riss | 9833de6 | 2015-03-06 23:22:53 +0000 | [diff] [blame] | 1410 | // The DIE might have been already created by a forward reference |
| 1411 | // (see cloneDieReferenceAttribute()). |
| 1412 | DIE *Die = Info.Clone; |
| 1413 | if (!Die) |
| 1414 | Die = Info.Clone = new DIE(dwarf::Tag(InputDIE.getTag())); |
| 1415 | assert(Die->getTag() == InputDIE.getTag()); |
Frederic Riss | b8b43d5 | 2015-03-04 22:07:44 +0000 | [diff] [blame] | 1416 | Die->setOffset(OutOffset); |
| 1417 | |
| 1418 | // Extract and clone every attribute. |
| 1419 | DataExtractor Data = U.getDebugInfoExtractor(); |
Frederic Riss | 23e20e9 | 2015-03-07 01:25:09 +0000 | [diff] [blame] | 1420 | uint32_t NextOffset = U.getDIEAtIndex(Idx + 1)->getOffset(); |
Frederic Riss | 31da324 | 2015-03-11 18:45:52 +0000 | [diff] [blame] | 1421 | AttributesInfo AttrInfo; |
Frederic Riss | 23e20e9 | 2015-03-07 01:25:09 +0000 | [diff] [blame] | 1422 | |
| 1423 | // We could copy the data only if we need to aply a relocation to |
| 1424 | // it. After testing, it seems there is no performance downside to |
| 1425 | // doing the copy unconditionally, and it makes the code simpler. |
| 1426 | SmallString<40> DIECopy(Data.getData().substr(Offset, NextOffset - Offset)); |
| 1427 | Data = DataExtractor(DIECopy, Data.isLittleEndian(), Data.getAddressSize()); |
| 1428 | // Modify the copy with relocated addresses. |
Frederic Riss | 31da324 | 2015-03-11 18:45:52 +0000 | [diff] [blame] | 1429 | if (applyValidRelocs(DIECopy, Offset, Data.isLittleEndian())) { |
| 1430 | // If we applied relocations, we store the value of high_pc that was |
| 1431 | // potentially stored in the input DIE. If high_pc is an address |
| 1432 | // (Dwarf version == 2), then it might have been relocated to a |
| 1433 | // totally unrelated value (because the end address in the object |
| 1434 | // file might be start address of another function which got moved |
| 1435 | // independantly by the linker). The computation of the actual |
| 1436 | // high_pc value is done in cloneAddressAttribute(). |
| 1437 | AttrInfo.OrigHighPc = |
| 1438 | InputDIE.getAttributeValueAsAddress(&U, dwarf::DW_AT_high_pc, 0); |
| 1439 | } |
Frederic Riss | 23e20e9 | 2015-03-07 01:25:09 +0000 | [diff] [blame] | 1440 | |
| 1441 | // Reset the Offset to 0 as we will be working on the local copy of |
| 1442 | // the data. |
| 1443 | Offset = 0; |
| 1444 | |
Frederic Riss | b8b43d5 | 2015-03-04 22:07:44 +0000 | [diff] [blame] | 1445 | const auto *Abbrev = InputDIE.getAbbreviationDeclarationPtr(); |
| 1446 | Offset += getULEB128Size(Abbrev->getCode()); |
| 1447 | |
Frederic Riss | 31da324 | 2015-03-11 18:45:52 +0000 | [diff] [blame] | 1448 | // We are entering a subprogram. Get and propagate the PCOffset. |
| 1449 | if (Die->getTag() == dwarf::DW_TAG_subprogram) |
| 1450 | PCOffset = Info.AddrAdjust; |
| 1451 | AttrInfo.PCOffset = PCOffset; |
| 1452 | |
Frederic Riss | b8b43d5 | 2015-03-04 22:07:44 +0000 | [diff] [blame] | 1453 | for (const auto &AttrSpec : Abbrev->attributes()) { |
| 1454 | DWARFFormValue Val(AttrSpec.Form); |
| 1455 | uint32_t AttrSize = Offset; |
| 1456 | Val.extractValue(Data, &Offset, &U); |
| 1457 | AttrSize = Offset - AttrSize; |
| 1458 | |
Frederic Riss | 31da324 | 2015-03-11 18:45:52 +0000 | [diff] [blame] | 1459 | OutOffset += |
| 1460 | cloneAttribute(*Die, InputDIE, Unit, Val, AttrSpec, AttrSize, AttrInfo); |
Frederic Riss | b8b43d5 | 2015-03-04 22:07:44 +0000 | [diff] [blame] | 1461 | } |
| 1462 | |
| 1463 | DIEAbbrev &NewAbbrev = Die->getAbbrev(); |
| 1464 | // If a scope DIE is kept, we must have kept at least one child. If |
| 1465 | // it's not the case, we'll just be emitting one wasteful end of |
| 1466 | // children marker, but things won't break. |
| 1467 | if (InputDIE.hasChildren()) |
| 1468 | NewAbbrev.setChildrenFlag(dwarf::DW_CHILDREN_yes); |
| 1469 | // Assign a permanent abbrev number |
| 1470 | AssignAbbrev(Die->getAbbrev()); |
| 1471 | |
| 1472 | // Add the size of the abbreviation number to the output offset. |
| 1473 | OutOffset += getULEB128Size(Die->getAbbrevNumber()); |
| 1474 | |
| 1475 | if (!Abbrev->hasChildren()) { |
| 1476 | // Update our size. |
| 1477 | Die->setSize(OutOffset - Die->getOffset()); |
| 1478 | return Die; |
| 1479 | } |
| 1480 | |
| 1481 | // Recursively clone children. |
| 1482 | for (auto *Child = InputDIE.getFirstChild(); Child && !Child->isNULL(); |
| 1483 | Child = Child->getSibling()) { |
Frederic Riss | 31da324 | 2015-03-11 18:45:52 +0000 | [diff] [blame] | 1484 | if (DIE *Clone = cloneDIE(*Child, Unit, PCOffset, OutOffset)) { |
Frederic Riss | b8b43d5 | 2015-03-04 22:07:44 +0000 | [diff] [blame] | 1485 | Die->addChild(std::unique_ptr<DIE>(Clone)); |
| 1486 | OutOffset = Clone->getOffset() + Clone->getSize(); |
| 1487 | } |
| 1488 | } |
| 1489 | |
| 1490 | // Account for the end of children marker. |
| 1491 | OutOffset += sizeof(int8_t); |
| 1492 | // Update our size. |
| 1493 | Die->setSize(OutOffset - Die->getOffset()); |
| 1494 | return Die; |
| 1495 | } |
| 1496 | |
Frederic Riss | d345518 | 2015-01-28 18:27:01 +0000 | [diff] [blame] | 1497 | bool DwarfLinker::link(const DebugMap &Map) { |
| 1498 | |
| 1499 | if (Map.begin() == Map.end()) { |
| 1500 | errs() << "Empty debug map.\n"; |
| 1501 | return false; |
| 1502 | } |
| 1503 | |
Frederic Riss | c99ea20 | 2015-02-28 00:29:11 +0000 | [diff] [blame] | 1504 | if (!createStreamer(Map.getTriple(), OutputFilename)) |
| 1505 | return false; |
| 1506 | |
Frederic Riss | b8b43d5 | 2015-03-04 22:07:44 +0000 | [diff] [blame] | 1507 | // Size of the DIEs (and headers) generated for the linked output. |
| 1508 | uint64_t OutputDebugInfoSize = 0; |
| 1509 | |
Frederic Riss | d345518 | 2015-01-28 18:27:01 +0000 | [diff] [blame] | 1510 | for (const auto &Obj : Map.objects()) { |
Frederic Riss | 1b9da42 | 2015-02-13 23:18:29 +0000 | [diff] [blame] | 1511 | CurrentDebugObject = Obj.get(); |
| 1512 | |
Frederic Riss | b981832 | 2015-02-28 00:29:07 +0000 | [diff] [blame] | 1513 | if (Options.Verbose) |
Frederic Riss | d345518 | 2015-01-28 18:27:01 +0000 | [diff] [blame] | 1514 | outs() << "DEBUG MAP OBJECT: " << Obj->getObjectFilename() << "\n"; |
| 1515 | auto ErrOrObj = BinHolder.GetObjectFile(Obj->getObjectFilename()); |
| 1516 | if (std::error_code EC = ErrOrObj.getError()) { |
Frederic Riss | 1b9da42 | 2015-02-13 23:18:29 +0000 | [diff] [blame] | 1517 | reportWarning(Twine(Obj->getObjectFilename()) + ": " + EC.message()); |
Frederic Riss | d345518 | 2015-01-28 18:27:01 +0000 | [diff] [blame] | 1518 | continue; |
| 1519 | } |
| 1520 | |
Frederic Riss | 1036e64 | 2015-02-13 23:18:22 +0000 | [diff] [blame] | 1521 | // Look for relocations that correspond to debug map entries. |
| 1522 | if (!findValidRelocsInDebugInfo(*ErrOrObj, *Obj)) { |
Frederic Riss | b981832 | 2015-02-28 00:29:07 +0000 | [diff] [blame] | 1523 | if (Options.Verbose) |
Frederic Riss | 1036e64 | 2015-02-13 23:18:22 +0000 | [diff] [blame] | 1524 | outs() << "No valid relocations found. Skipping.\n"; |
| 1525 | continue; |
| 1526 | } |
| 1527 | |
Frederic Riss | 563cba6 | 2015-01-28 22:15:14 +0000 | [diff] [blame] | 1528 | // Setup access to the debug info. |
Frederic Riss | d345518 | 2015-01-28 18:27:01 +0000 | [diff] [blame] | 1529 | DWARFContextInMemory DwarfContext(*ErrOrObj); |
Frederic Riss | 563cba6 | 2015-01-28 22:15:14 +0000 | [diff] [blame] | 1530 | startDebugObject(DwarfContext); |
Frederic Riss | d345518 | 2015-01-28 18:27:01 +0000 | [diff] [blame] | 1531 | |
Frederic Riss | 563cba6 | 2015-01-28 22:15:14 +0000 | [diff] [blame] | 1532 | // In a first phase, just read in the debug info and store the DIE |
| 1533 | // parent links that we will use during the next phase. |
Frederic Riss | d345518 | 2015-01-28 18:27:01 +0000 | [diff] [blame] | 1534 | for (const auto &CU : DwarfContext.compile_units()) { |
| 1535 | auto *CUDie = CU->getCompileUnitDIE(false); |
Frederic Riss | b981832 | 2015-02-28 00:29:07 +0000 | [diff] [blame] | 1536 | if (Options.Verbose) { |
Frederic Riss | d345518 | 2015-01-28 18:27:01 +0000 | [diff] [blame] | 1537 | outs() << "Input compilation unit:"; |
| 1538 | CUDie->dump(outs(), CU.get(), 0); |
| 1539 | } |
Frederic Riss | 563cba6 | 2015-01-28 22:15:14 +0000 | [diff] [blame] | 1540 | Units.emplace_back(*CU); |
Frederic Riss | 9aa725b | 2015-02-13 23:18:31 +0000 | [diff] [blame] | 1541 | gatherDIEParents(CUDie, 0, Units.back()); |
Frederic Riss | d345518 | 2015-01-28 18:27:01 +0000 | [diff] [blame] | 1542 | } |
Frederic Riss | 563cba6 | 2015-01-28 22:15:14 +0000 | [diff] [blame] | 1543 | |
Frederic Riss | 84c09a5 | 2015-02-13 23:18:34 +0000 | [diff] [blame] | 1544 | // Then mark all the DIEs that need to be present in the linked |
| 1545 | // output and collect some information about them. Note that this |
| 1546 | // loop can not be merged with the previous one becaue cross-cu |
| 1547 | // references require the ParentIdx to be setup for every CU in |
| 1548 | // the object file before calling this. |
| 1549 | for (auto &CurrentUnit : Units) |
| 1550 | lookForDIEsToKeep(*CurrentUnit.getOrigUnit().getCompileUnitDIE(), *Obj, |
| 1551 | CurrentUnit, 0); |
| 1552 | |
Frederic Riss | 23e20e9 | 2015-03-07 01:25:09 +0000 | [diff] [blame] | 1553 | // The calls to applyValidRelocs inside cloneDIE will walk the |
| 1554 | // reloc array again (in the same way findValidRelocsInDebugInfo() |
| 1555 | // did). We need to reset the NextValidReloc index to the beginning. |
| 1556 | NextValidReloc = 0; |
| 1557 | |
Frederic Riss | b8b43d5 | 2015-03-04 22:07:44 +0000 | [diff] [blame] | 1558 | // Construct the output DIE tree by cloning the DIEs we chose to |
| 1559 | // keep above. If there are no valid relocs, then there's nothing |
| 1560 | // to clone/emit. |
| 1561 | if (!ValidRelocs.empty()) |
| 1562 | for (auto &CurrentUnit : Units) { |
| 1563 | const auto *InputDIE = CurrentUnit.getOrigUnit().getCompileUnitDIE(); |
Frederic Riss | 9d441b6 | 2015-03-06 23:22:50 +0000 | [diff] [blame] | 1564 | CurrentUnit.setStartOffset(OutputDebugInfoSize); |
Frederic Riss | 31da324 | 2015-03-11 18:45:52 +0000 | [diff] [blame] | 1565 | DIE *OutputDIE = cloneDIE(*InputDIE, CurrentUnit, 0 /* PCOffset */, |
| 1566 | 11 /* Unit Header size */); |
Frederic Riss | b8b43d5 | 2015-03-04 22:07:44 +0000 | [diff] [blame] | 1567 | CurrentUnit.setOutputUnitDIE(OutputDIE); |
Frederic Riss | 9d441b6 | 2015-03-06 23:22:50 +0000 | [diff] [blame] | 1568 | OutputDebugInfoSize = CurrentUnit.computeNextUnitOffset(); |
Frederic Riss | b8b43d5 | 2015-03-04 22:07:44 +0000 | [diff] [blame] | 1569 | } |
| 1570 | |
| 1571 | // Emit all the compile unit's debug information. |
| 1572 | if (!ValidRelocs.empty() && !Options.NoOutput) |
| 1573 | for (auto &CurrentUnit : Units) { |
Frederic Riss | 9833de6 | 2015-03-06 23:22:53 +0000 | [diff] [blame] | 1574 | CurrentUnit.fixupForwardReferences(); |
Frederic Riss | b8b43d5 | 2015-03-04 22:07:44 +0000 | [diff] [blame] | 1575 | Streamer->emitCompileUnitHeader(CurrentUnit); |
| 1576 | if (!CurrentUnit.getOutputUnitDIE()) |
| 1577 | continue; |
| 1578 | Streamer->emitDIE(*CurrentUnit.getOutputUnitDIE()); |
| 1579 | } |
| 1580 | |
Frederic Riss | 563cba6 | 2015-01-28 22:15:14 +0000 | [diff] [blame] | 1581 | // Clean-up before starting working on the next object. |
| 1582 | endDebugObject(); |
Frederic Riss | d345518 | 2015-01-28 18:27:01 +0000 | [diff] [blame] | 1583 | } |
| 1584 | |
Frederic Riss | b8b43d5 | 2015-03-04 22:07:44 +0000 | [diff] [blame] | 1585 | // Emit everything that's global. |
Frederic Riss | ef64846 | 2015-03-06 17:56:30 +0000 | [diff] [blame] | 1586 | if (!Options.NoOutput) { |
Frederic Riss | b8b43d5 | 2015-03-04 22:07:44 +0000 | [diff] [blame] | 1587 | Streamer->emitAbbrevs(Abbreviations); |
Frederic Riss | ef64846 | 2015-03-06 17:56:30 +0000 | [diff] [blame] | 1588 | Streamer->emitStrings(StringPool); |
| 1589 | } |
Frederic Riss | b8b43d5 | 2015-03-04 22:07:44 +0000 | [diff] [blame] | 1590 | |
Frederic Riss | c99ea20 | 2015-02-28 00:29:11 +0000 | [diff] [blame] | 1591 | return Options.NoOutput ? true : Streamer->finish(); |
Frederic Riss | 231f714 | 2014-12-12 17:31:24 +0000 | [diff] [blame] | 1592 | } |
| 1593 | } |
Frederic Riss | d345518 | 2015-01-28 18:27:01 +0000 | [diff] [blame] | 1594 | |
Frederic Riss | b981832 | 2015-02-28 00:29:07 +0000 | [diff] [blame] | 1595 | bool linkDwarf(StringRef OutputFilename, const DebugMap &DM, |
| 1596 | const LinkOptions &Options) { |
| 1597 | DwarfLinker Linker(OutputFilename, Options); |
Frederic Riss | d345518 | 2015-01-28 18:27:01 +0000 | [diff] [blame] | 1598 | return Linker.link(DM); |
| 1599 | } |
| 1600 | } |
Frederic Riss | 231f714 | 2014-12-12 17:31:24 +0000 | [diff] [blame] | 1601 | } |