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 | c99ea20 | 2015-02-28 00:29:11 +0000 | [diff] [blame] | 13 | #include "llvm/CodeGen/AsmPrinter.h" |
Zachary Turner | 82af943 | 2015-01-30 18:07:45 +0000 | [diff] [blame] | 14 | #include "llvm/DebugInfo/DWARF/DWARFContext.h" |
| 15 | #include "llvm/DebugInfo/DWARF/DWARFDebugInfoEntry.h" |
Frederic Riss | 1b9da42 | 2015-02-13 23:18:29 +0000 | [diff] [blame] | 16 | #include "llvm/DebugInfo/DWARF/DWARFFormValue.h" |
Frederic Riss | c99ea20 | 2015-02-28 00:29:11 +0000 | [diff] [blame] | 17 | #include "llvm/MC/MCAsmBackend.h" |
| 18 | #include "llvm/MC/MCAsmInfo.h" |
| 19 | #include "llvm/MC/MCContext.h" |
| 20 | #include "llvm/MC/MCCodeEmitter.h" |
| 21 | #include "llvm/MC/MCInstrInfo.h" |
| 22 | #include "llvm/MC/MCObjectFileInfo.h" |
| 23 | #include "llvm/MC/MCRegisterInfo.h" |
| 24 | #include "llvm/MC/MCStreamer.h" |
Frederic Riss | 1036e64 | 2015-02-13 23:18:22 +0000 | [diff] [blame] | 25 | #include "llvm/Object/MachO.h" |
Frederic Riss | 84c09a5 | 2015-02-13 23:18:34 +0000 | [diff] [blame] | 26 | #include "llvm/Support/Dwarf.h" |
| 27 | #include "llvm/Support/LEB128.h" |
Frederic Riss | c99ea20 | 2015-02-28 00:29:11 +0000 | [diff] [blame] | 28 | #include "llvm/Support/TargetRegistry.h" |
| 29 | #include "llvm/Target/TargetMachine.h" |
| 30 | #include "llvm/Target/TargetOptions.h" |
Frederic Riss | d345518 | 2015-01-28 18:27:01 +0000 | [diff] [blame] | 31 | #include <string> |
Frederic Riss | 231f714 | 2014-12-12 17:31:24 +0000 | [diff] [blame] | 32 | |
| 33 | namespace llvm { |
| 34 | namespace dsymutil { |
| 35 | |
Frederic Riss | d345518 | 2015-01-28 18:27:01 +0000 | [diff] [blame] | 36 | namespace { |
| 37 | |
Frederic Riss | def4fb7 | 2015-02-28 00:29:01 +0000 | [diff] [blame] | 38 | void warn(const Twine &Warning, const Twine &Context) { |
| 39 | errs() << Twine("while processing ") + Context + ":\n"; |
| 40 | errs() << Twine("warning: ") + Warning + "\n"; |
| 41 | } |
| 42 | |
Frederic Riss | c99ea20 | 2015-02-28 00:29:11 +0000 | [diff] [blame] | 43 | bool error(const Twine &Error, const Twine &Context) { |
| 44 | errs() << Twine("while processing ") + Context + ":\n"; |
| 45 | errs() << Twine("error: ") + Error + "\n"; |
| 46 | return false; |
| 47 | } |
| 48 | |
Frederic Riss | 563cba6 | 2015-01-28 22:15:14 +0000 | [diff] [blame] | 49 | /// \brief Stores all information relating to a compile unit, be it in |
| 50 | /// its original instance in the object file to its brand new cloned |
| 51 | /// and linked DIE tree. |
| 52 | class CompileUnit { |
| 53 | public: |
| 54 | /// \brief Information gathered about a DIE in the object file. |
| 55 | struct DIEInfo { |
Frederic Riss | 84c09a5 | 2015-02-13 23:18:34 +0000 | [diff] [blame] | 56 | uint64_t Address; ///< Linked address of the described entity. |
| 57 | uint32_t ParentIdx; ///< The index of this DIE's parent. |
| 58 | bool Keep; ///< Is the DIE part of the linked output? |
| 59 | bool InDebugMap; ///< Was this DIE's entity found in the map? |
Frederic Riss | 563cba6 | 2015-01-28 22:15:14 +0000 | [diff] [blame] | 60 | }; |
| 61 | |
| 62 | CompileUnit(DWARFUnit &OrigUnit) : OrigUnit(OrigUnit) { |
| 63 | Info.resize(OrigUnit.getNumDIEs()); |
| 64 | } |
| 65 | |
Frederic Riss | c3349d4 | 2015-02-13 23:18:27 +0000 | [diff] [blame] | 66 | DWARFUnit &getOrigUnit() const { return OrigUnit; } |
Frederic Riss | 563cba6 | 2015-01-28 22:15:14 +0000 | [diff] [blame] | 67 | |
| 68 | DIEInfo &getInfo(unsigned Idx) { return Info[Idx]; } |
| 69 | const DIEInfo &getInfo(unsigned Idx) const { return Info[Idx]; } |
| 70 | |
| 71 | private: |
| 72 | DWARFUnit &OrigUnit; |
| 73 | std::vector<DIEInfo> Info; ///< DIE info indexed by DIE index. |
| 74 | }; |
| 75 | |
Frederic Riss | c99ea20 | 2015-02-28 00:29:11 +0000 | [diff] [blame] | 76 | /// \brief The Dwarf streaming logic |
| 77 | /// |
| 78 | /// All interactions with the MC layer that is used to build the debug |
| 79 | /// information binary representation are handled in this class. |
| 80 | class DwarfStreamer { |
| 81 | /// \defgroup MCObjects MC layer objects constructed by the streamer |
| 82 | /// @{ |
| 83 | std::unique_ptr<MCRegisterInfo> MRI; |
| 84 | std::unique_ptr<MCAsmInfo> MAI; |
| 85 | std::unique_ptr<MCObjectFileInfo> MOFI; |
| 86 | std::unique_ptr<MCContext> MC; |
| 87 | MCAsmBackend *MAB; // Owned by MCStreamer |
| 88 | std::unique_ptr<MCInstrInfo> MII; |
| 89 | std::unique_ptr<MCSubtargetInfo> MSTI; |
| 90 | MCCodeEmitter *MCE; // Owned by MCStreamer |
| 91 | MCStreamer *MS; // Owned by AsmPrinter |
| 92 | std::unique_ptr<TargetMachine> TM; |
| 93 | std::unique_ptr<AsmPrinter> Asm; |
| 94 | /// @} |
| 95 | |
| 96 | /// \brief the file we stream the linked Dwarf to. |
| 97 | std::unique_ptr<raw_fd_ostream> OutFile; |
| 98 | |
| 99 | public: |
| 100 | /// \brief Actually create the streamer and the ouptut file. |
| 101 | /// |
| 102 | /// This could be done directly in the constructor, but it feels |
| 103 | /// more natural to handle errors through return value. |
| 104 | bool init(Triple TheTriple, StringRef OutputFilename); |
| 105 | |
| 106 | ///\brief Dump the file to the disk. |
| 107 | bool finish(); |
| 108 | }; |
| 109 | |
| 110 | bool DwarfStreamer::init(Triple TheTriple, StringRef OutputFilename) { |
| 111 | std::string ErrorStr; |
| 112 | std::string TripleName; |
| 113 | StringRef Context = "dwarf streamer init"; |
| 114 | |
| 115 | // Get the target. |
| 116 | const Target *TheTarget = |
| 117 | TargetRegistry::lookupTarget(TripleName, TheTriple, ErrorStr); |
| 118 | if (!TheTarget) |
| 119 | return error(ErrorStr, Context); |
| 120 | TripleName = TheTriple.getTriple(); |
| 121 | |
| 122 | // Create all the MC Objects. |
| 123 | MRI.reset(TheTarget->createMCRegInfo(TripleName)); |
| 124 | if (!MRI) |
| 125 | return error(Twine("no register info for target ") + TripleName, Context); |
| 126 | |
| 127 | MAI.reset(TheTarget->createMCAsmInfo(*MRI, TripleName)); |
| 128 | if (!MAI) |
| 129 | return error("no asm info for target " + TripleName, Context); |
| 130 | |
| 131 | MOFI.reset(new MCObjectFileInfo); |
| 132 | MC.reset(new MCContext(MAI.get(), MRI.get(), MOFI.get())); |
| 133 | MOFI->InitMCObjectFileInfo(TripleName, Reloc::Default, CodeModel::Default, |
| 134 | *MC); |
| 135 | |
| 136 | MAB = TheTarget->createMCAsmBackend(*MRI, TripleName, ""); |
| 137 | if (!MAB) |
| 138 | return error("no asm backend for target " + TripleName, Context); |
| 139 | |
| 140 | MII.reset(TheTarget->createMCInstrInfo()); |
| 141 | if (!MII) |
| 142 | return error("no instr info info for target " + TripleName, Context); |
| 143 | |
| 144 | MSTI.reset(TheTarget->createMCSubtargetInfo(TripleName, "", "")); |
| 145 | if (!MSTI) |
| 146 | return error("no subtarget info for target " + TripleName, Context); |
| 147 | |
| 148 | MCE = TheTarget->createMCCodeEmitter(*MII, *MRI, *MSTI, *MC); |
| 149 | if (!MCE) |
| 150 | return error("no code emitter for target " + TripleName, Context); |
| 151 | |
| 152 | // Create the output file. |
| 153 | std::error_code EC; |
Frederic Riss | b52cf52 | 2015-02-28 00:42:37 +0000 | [diff] [blame^] | 154 | OutFile = llvm::make_unique<raw_fd_ostream>(OutputFilename, EC, |
| 155 | sys::fs::F_None); |
Frederic Riss | c99ea20 | 2015-02-28 00:29:11 +0000 | [diff] [blame] | 156 | if (EC) |
| 157 | return error(Twine(OutputFilename) + ": " + EC.message(), Context); |
| 158 | |
| 159 | MS = TheTarget->createMCObjectStreamer(TripleName, *MC, *MAB, *OutFile, MCE, |
| 160 | *MSTI, false); |
| 161 | if (!MS) |
| 162 | return error("no object streamer for target " + TripleName, Context); |
| 163 | |
| 164 | // Finally create the AsmPrinter we'll use to emit the DIEs. |
| 165 | TM.reset(TheTarget->createTargetMachine(TripleName, "", "", TargetOptions())); |
| 166 | if (!TM) |
| 167 | return error("no target machine for target " + TripleName, Context); |
| 168 | |
| 169 | Asm.reset(TheTarget->createAsmPrinter(*TM, std::unique_ptr<MCStreamer>(MS))); |
| 170 | if (!Asm) |
| 171 | return error("no asm printer for target " + TripleName, Context); |
| 172 | |
| 173 | return true; |
| 174 | } |
| 175 | |
| 176 | bool DwarfStreamer::finish() { |
| 177 | MS->Finish(); |
| 178 | return true; |
| 179 | } |
| 180 | |
Frederic Riss | d345518 | 2015-01-28 18:27:01 +0000 | [diff] [blame] | 181 | /// \brief The core of the Dwarf linking logic. |
Frederic Riss | 1036e64 | 2015-02-13 23:18:22 +0000 | [diff] [blame] | 182 | /// |
| 183 | /// The link of the dwarf information from the object files will be |
| 184 | /// driven by the selection of 'root DIEs', which are DIEs that |
| 185 | /// describe variables or functions that are present in the linked |
| 186 | /// binary (and thus have entries in the debug map). All the debug |
| 187 | /// information that will be linked (the DIEs, but also the line |
| 188 | /// tables, ranges, ...) is derived from that set of root DIEs. |
| 189 | /// |
| 190 | /// The root DIEs are identified because they contain relocations that |
| 191 | /// correspond to a debug map entry at specific places (the low_pc for |
| 192 | /// a function, the location for a variable). These relocations are |
| 193 | /// called ValidRelocs in the DwarfLinker and are gathered as a very |
| 194 | /// first step when we start processing a DebugMapObject. |
Frederic Riss | d345518 | 2015-01-28 18:27:01 +0000 | [diff] [blame] | 195 | class DwarfLinker { |
| 196 | public: |
Frederic Riss | b981832 | 2015-02-28 00:29:07 +0000 | [diff] [blame] | 197 | DwarfLinker(StringRef OutputFilename, const LinkOptions &Options) |
| 198 | : OutputFilename(OutputFilename), Options(Options), |
| 199 | BinHolder(Options.Verbose) {} |
Frederic Riss | d345518 | 2015-01-28 18:27:01 +0000 | [diff] [blame] | 200 | |
| 201 | /// \brief Link the contents of the DebugMap. |
| 202 | bool link(const DebugMap &); |
| 203 | |
| 204 | private: |
Frederic Riss | 563cba6 | 2015-01-28 22:15:14 +0000 | [diff] [blame] | 205 | /// \brief Called at the start of a debug object link. |
| 206 | void startDebugObject(DWARFContext &); |
| 207 | |
| 208 | /// \brief Called at the end of a debug object link. |
| 209 | void endDebugObject(); |
| 210 | |
Frederic Riss | 1036e64 | 2015-02-13 23:18:22 +0000 | [diff] [blame] | 211 | /// \defgroup FindValidRelocations Translate debug map into a list |
| 212 | /// of relevant relocations |
| 213 | /// |
| 214 | /// @{ |
| 215 | struct ValidReloc { |
| 216 | uint32_t Offset; |
| 217 | uint32_t Size; |
| 218 | uint64_t Addend; |
| 219 | const DebugMapObject::DebugMapEntry *Mapping; |
| 220 | |
| 221 | ValidReloc(uint32_t Offset, uint32_t Size, uint64_t Addend, |
| 222 | const DebugMapObject::DebugMapEntry *Mapping) |
| 223 | : Offset(Offset), Size(Size), Addend(Addend), Mapping(Mapping) {} |
| 224 | |
| 225 | bool operator<(const ValidReloc &RHS) const { return Offset < RHS.Offset; } |
| 226 | }; |
| 227 | |
| 228 | /// \brief The valid relocations for the current DebugMapObject. |
| 229 | /// This vector is sorted by relocation offset. |
| 230 | std::vector<ValidReloc> ValidRelocs; |
| 231 | |
| 232 | /// \brief Index into ValidRelocs of the next relocation to |
| 233 | /// consider. As we walk the DIEs in acsending file offset and as |
| 234 | /// ValidRelocs is sorted by file offset, keeping this index |
| 235 | /// uptodate is all we have to do to have a cheap lookup during the |
| 236 | /// root DIE selection. |
| 237 | unsigned NextValidReloc; |
| 238 | |
| 239 | bool findValidRelocsInDebugInfo(const object::ObjectFile &Obj, |
| 240 | const DebugMapObject &DMO); |
| 241 | |
| 242 | bool findValidRelocs(const object::SectionRef &Section, |
| 243 | const object::ObjectFile &Obj, |
| 244 | const DebugMapObject &DMO); |
| 245 | |
| 246 | void findValidRelocsMachO(const object::SectionRef &Section, |
| 247 | const object::MachOObjectFile &Obj, |
| 248 | const DebugMapObject &DMO); |
| 249 | /// @} |
Frederic Riss | 1b9da42 | 2015-02-13 23:18:29 +0000 | [diff] [blame] | 250 | |
Frederic Riss | 84c09a5 | 2015-02-13 23:18:34 +0000 | [diff] [blame] | 251 | /// \defgroup FindRootDIEs Find DIEs corresponding to debug map entries. |
| 252 | /// |
| 253 | /// @{ |
| 254 | /// \brief Recursively walk the \p DIE tree and look for DIEs to |
| 255 | /// keep. Store that information in \p CU's DIEInfo. |
| 256 | void lookForDIEsToKeep(const DWARFDebugInfoEntryMinimal &DIE, |
| 257 | const DebugMapObject &DMO, CompileUnit &CU, |
| 258 | unsigned Flags); |
| 259 | |
| 260 | /// \brief Flags passed to DwarfLinker::lookForDIEsToKeep |
| 261 | enum TravesalFlags { |
| 262 | TF_Keep = 1 << 0, ///< Mark the traversed DIEs as kept. |
| 263 | TF_InFunctionScope = 1 << 1, ///< Current scope is a fucntion scope. |
| 264 | TF_DependencyWalk = 1 << 2, ///< Walking the dependencies of a kept DIE. |
| 265 | TF_ParentWalk = 1 << 3, ///< Walking up the parents of a kept DIE. |
| 266 | }; |
| 267 | |
| 268 | /// \brief Mark the passed DIE as well as all the ones it depends on |
| 269 | /// as kept. |
| 270 | void keepDIEAndDenpendencies(const DWARFDebugInfoEntryMinimal &DIE, |
| 271 | CompileUnit::DIEInfo &MyInfo, |
| 272 | const DebugMapObject &DMO, CompileUnit &CU, |
| 273 | unsigned Flags); |
| 274 | |
| 275 | unsigned shouldKeepDIE(const DWARFDebugInfoEntryMinimal &DIE, |
| 276 | CompileUnit &Unit, CompileUnit::DIEInfo &MyInfo, |
| 277 | unsigned Flags); |
| 278 | |
| 279 | unsigned shouldKeepVariableDIE(const DWARFDebugInfoEntryMinimal &DIE, |
| 280 | CompileUnit &Unit, |
| 281 | CompileUnit::DIEInfo &MyInfo, unsigned Flags); |
| 282 | |
| 283 | unsigned shouldKeepSubprogramDIE(const DWARFDebugInfoEntryMinimal &DIE, |
| 284 | CompileUnit &Unit, |
| 285 | CompileUnit::DIEInfo &MyInfo, |
| 286 | unsigned Flags); |
| 287 | |
| 288 | bool hasValidRelocation(uint32_t StartOffset, uint32_t EndOffset, |
| 289 | CompileUnit::DIEInfo &Info); |
| 290 | /// @} |
| 291 | |
Frederic Riss | 1b9da42 | 2015-02-13 23:18:29 +0000 | [diff] [blame] | 292 | /// \defgroup Helpers Various helper methods. |
| 293 | /// |
| 294 | /// @{ |
| 295 | const DWARFDebugInfoEntryMinimal * |
| 296 | resolveDIEReference(DWARFFormValue &RefValue, const DWARFUnit &Unit, |
| 297 | const DWARFDebugInfoEntryMinimal &DIE, |
| 298 | CompileUnit *&ReferencedCU); |
| 299 | |
| 300 | CompileUnit *getUnitForOffset(unsigned Offset); |
| 301 | |
| 302 | void reportWarning(const Twine &Warning, const DWARFUnit *Unit = nullptr, |
| 303 | const DWARFDebugInfoEntryMinimal *DIE = nullptr); |
Frederic Riss | c99ea20 | 2015-02-28 00:29:11 +0000 | [diff] [blame] | 304 | |
| 305 | bool createStreamer(Triple TheTriple, StringRef OutputFilename); |
Frederic Riss | 1b9da42 | 2015-02-13 23:18:29 +0000 | [diff] [blame] | 306 | /// @} |
| 307 | |
Frederic Riss | 563cba6 | 2015-01-28 22:15:14 +0000 | [diff] [blame] | 308 | private: |
Frederic Riss | d345518 | 2015-01-28 18:27:01 +0000 | [diff] [blame] | 309 | std::string OutputFilename; |
Frederic Riss | b981832 | 2015-02-28 00:29:07 +0000 | [diff] [blame] | 310 | LinkOptions Options; |
Frederic Riss | d345518 | 2015-01-28 18:27:01 +0000 | [diff] [blame] | 311 | BinaryHolder BinHolder; |
Frederic Riss | c99ea20 | 2015-02-28 00:29:11 +0000 | [diff] [blame] | 312 | std::unique_ptr<DwarfStreamer> Streamer; |
Frederic Riss | 563cba6 | 2015-01-28 22:15:14 +0000 | [diff] [blame] | 313 | |
| 314 | /// The units of the current debug map object. |
| 315 | std::vector<CompileUnit> Units; |
Frederic Riss | 1b9da42 | 2015-02-13 23:18:29 +0000 | [diff] [blame] | 316 | |
| 317 | /// The debug map object curently under consideration. |
| 318 | DebugMapObject *CurrentDebugObject; |
Frederic Riss | d345518 | 2015-01-28 18:27:01 +0000 | [diff] [blame] | 319 | }; |
| 320 | |
Frederic Riss | 1b9da42 | 2015-02-13 23:18:29 +0000 | [diff] [blame] | 321 | /// \brief Similar to DWARFUnitSection::getUnitForOffset(), but |
| 322 | /// returning our CompileUnit object instead. |
| 323 | CompileUnit *DwarfLinker::getUnitForOffset(unsigned Offset) { |
| 324 | auto CU = |
| 325 | std::upper_bound(Units.begin(), Units.end(), Offset, |
| 326 | [](uint32_t LHS, const CompileUnit &RHS) { |
| 327 | return LHS < RHS.getOrigUnit().getNextUnitOffset(); |
| 328 | }); |
| 329 | return CU != Units.end() ? &*CU : nullptr; |
| 330 | } |
| 331 | |
| 332 | /// \brief Resolve the DIE attribute reference that has been |
| 333 | /// extracted in \p RefValue. The resulting DIE migh be in another |
| 334 | /// CompileUnit which is stored into \p ReferencedCU. |
| 335 | /// \returns null if resolving fails for any reason. |
| 336 | const DWARFDebugInfoEntryMinimal *DwarfLinker::resolveDIEReference( |
| 337 | DWARFFormValue &RefValue, const DWARFUnit &Unit, |
| 338 | const DWARFDebugInfoEntryMinimal &DIE, CompileUnit *&RefCU) { |
| 339 | assert(RefValue.isFormClass(DWARFFormValue::FC_Reference)); |
| 340 | uint64_t RefOffset = *RefValue.getAsReference(&Unit); |
| 341 | |
| 342 | if ((RefCU = getUnitForOffset(RefOffset))) |
| 343 | if (const auto *RefDie = RefCU->getOrigUnit().getDIEForOffset(RefOffset)) |
| 344 | return RefDie; |
| 345 | |
| 346 | reportWarning("could not find referenced DIE", &Unit, &DIE); |
| 347 | return nullptr; |
| 348 | } |
| 349 | |
| 350 | /// \brief Report a warning to the user, optionaly including |
| 351 | /// information about a specific \p DIE related to the warning. |
| 352 | void DwarfLinker::reportWarning(const Twine &Warning, const DWARFUnit *Unit, |
| 353 | const DWARFDebugInfoEntryMinimal *DIE) { |
Frederic Riss | def4fb7 | 2015-02-28 00:29:01 +0000 | [diff] [blame] | 354 | StringRef Context = "<debug map>"; |
Frederic Riss | 1b9da42 | 2015-02-13 23:18:29 +0000 | [diff] [blame] | 355 | if (CurrentDebugObject) |
Frederic Riss | def4fb7 | 2015-02-28 00:29:01 +0000 | [diff] [blame] | 356 | Context = CurrentDebugObject->getObjectFilename(); |
| 357 | warn(Warning, Context); |
Frederic Riss | 1b9da42 | 2015-02-13 23:18:29 +0000 | [diff] [blame] | 358 | |
Frederic Riss | b981832 | 2015-02-28 00:29:07 +0000 | [diff] [blame] | 359 | if (!Options.Verbose || !DIE) |
Frederic Riss | 1b9da42 | 2015-02-13 23:18:29 +0000 | [diff] [blame] | 360 | return; |
| 361 | |
| 362 | errs() << " in DIE:\n"; |
| 363 | DIE->dump(errs(), const_cast<DWARFUnit *>(Unit), 0 /* RecurseDepth */, |
| 364 | 6 /* Indent */); |
| 365 | } |
| 366 | |
Frederic Riss | c99ea20 | 2015-02-28 00:29:11 +0000 | [diff] [blame] | 367 | bool DwarfLinker::createStreamer(Triple TheTriple, StringRef OutputFilename) { |
| 368 | if (Options.NoOutput) |
| 369 | return true; |
| 370 | |
Frederic Riss | b52cf52 | 2015-02-28 00:42:37 +0000 | [diff] [blame^] | 371 | Streamer = llvm::make_unique<DwarfStreamer>(); |
Frederic Riss | c99ea20 | 2015-02-28 00:29:11 +0000 | [diff] [blame] | 372 | return Streamer->init(TheTriple, OutputFilename); |
| 373 | } |
| 374 | |
Frederic Riss | 563cba6 | 2015-01-28 22:15:14 +0000 | [diff] [blame] | 375 | /// \brief Recursive helper to gather the child->parent relationships in the |
| 376 | /// original compile unit. |
Frederic Riss | 9aa725b | 2015-02-13 23:18:31 +0000 | [diff] [blame] | 377 | static void gatherDIEParents(const DWARFDebugInfoEntryMinimal *DIE, |
| 378 | unsigned ParentIdx, CompileUnit &CU) { |
Frederic Riss | 563cba6 | 2015-01-28 22:15:14 +0000 | [diff] [blame] | 379 | unsigned MyIdx = CU.getOrigUnit().getDIEIndex(DIE); |
| 380 | CU.getInfo(MyIdx).ParentIdx = ParentIdx; |
| 381 | |
| 382 | if (DIE->hasChildren()) |
| 383 | for (auto *Child = DIE->getFirstChild(); Child && !Child->isNULL(); |
| 384 | Child = Child->getSibling()) |
Frederic Riss | 9aa725b | 2015-02-13 23:18:31 +0000 | [diff] [blame] | 385 | gatherDIEParents(Child, MyIdx, CU); |
Frederic Riss | 563cba6 | 2015-01-28 22:15:14 +0000 | [diff] [blame] | 386 | } |
| 387 | |
Frederic Riss | 84c09a5 | 2015-02-13 23:18:34 +0000 | [diff] [blame] | 388 | static bool dieNeedsChildrenToBeMeaningful(uint32_t Tag) { |
| 389 | switch (Tag) { |
| 390 | default: |
| 391 | return false; |
| 392 | case dwarf::DW_TAG_subprogram: |
| 393 | case dwarf::DW_TAG_lexical_block: |
| 394 | case dwarf::DW_TAG_subroutine_type: |
| 395 | case dwarf::DW_TAG_structure_type: |
| 396 | case dwarf::DW_TAG_class_type: |
| 397 | case dwarf::DW_TAG_union_type: |
| 398 | return true; |
| 399 | } |
| 400 | llvm_unreachable("Invalid Tag"); |
| 401 | } |
| 402 | |
Frederic Riss | 563cba6 | 2015-01-28 22:15:14 +0000 | [diff] [blame] | 403 | void DwarfLinker::startDebugObject(DWARFContext &Dwarf) { |
| 404 | Units.reserve(Dwarf.getNumCompileUnits()); |
Frederic Riss | 1036e64 | 2015-02-13 23:18:22 +0000 | [diff] [blame] | 405 | NextValidReloc = 0; |
Frederic Riss | 563cba6 | 2015-01-28 22:15:14 +0000 | [diff] [blame] | 406 | } |
| 407 | |
Frederic Riss | 1036e64 | 2015-02-13 23:18:22 +0000 | [diff] [blame] | 408 | void DwarfLinker::endDebugObject() { |
| 409 | Units.clear(); |
| 410 | ValidRelocs.clear(); |
| 411 | } |
| 412 | |
| 413 | /// \brief Iterate over the relocations of the given \p Section and |
| 414 | /// store the ones that correspond to debug map entries into the |
| 415 | /// ValidRelocs array. |
| 416 | void DwarfLinker::findValidRelocsMachO(const object::SectionRef &Section, |
| 417 | const object::MachOObjectFile &Obj, |
| 418 | const DebugMapObject &DMO) { |
| 419 | StringRef Contents; |
| 420 | Section.getContents(Contents); |
| 421 | DataExtractor Data(Contents, Obj.isLittleEndian(), 0); |
| 422 | |
| 423 | for (const object::RelocationRef &Reloc : Section.relocations()) { |
| 424 | object::DataRefImpl RelocDataRef = Reloc.getRawDataRefImpl(); |
| 425 | MachO::any_relocation_info MachOReloc = Obj.getRelocation(RelocDataRef); |
| 426 | unsigned RelocSize = 1 << Obj.getAnyRelocationLength(MachOReloc); |
| 427 | uint64_t Offset64; |
| 428 | if ((RelocSize != 4 && RelocSize != 8) || Reloc.getOffset(Offset64)) { |
Frederic Riss | 1b9da42 | 2015-02-13 23:18:29 +0000 | [diff] [blame] | 429 | reportWarning(" unsupported relocation in debug_info section."); |
Frederic Riss | 1036e64 | 2015-02-13 23:18:22 +0000 | [diff] [blame] | 430 | continue; |
| 431 | } |
| 432 | uint32_t Offset = Offset64; |
| 433 | // Mach-o uses REL relocations, the addend is at the relocation offset. |
| 434 | uint64_t Addend = Data.getUnsigned(&Offset, RelocSize); |
| 435 | |
| 436 | auto Sym = Reloc.getSymbol(); |
| 437 | if (Sym != Obj.symbol_end()) { |
| 438 | StringRef SymbolName; |
| 439 | if (Sym->getName(SymbolName)) { |
Frederic Riss | 1b9da42 | 2015-02-13 23:18:29 +0000 | [diff] [blame] | 440 | reportWarning("error getting relocation symbol name."); |
Frederic Riss | 1036e64 | 2015-02-13 23:18:22 +0000 | [diff] [blame] | 441 | continue; |
| 442 | } |
| 443 | if (const auto *Mapping = DMO.lookupSymbol(SymbolName)) |
| 444 | ValidRelocs.emplace_back(Offset64, RelocSize, Addend, Mapping); |
| 445 | } else if (const auto *Mapping = DMO.lookupObjectAddress(Addend)) { |
| 446 | // Do not store the addend. The addend was the address of the |
| 447 | // symbol in the object file, the address in the binary that is |
| 448 | // stored in the debug map doesn't need to be offseted. |
| 449 | ValidRelocs.emplace_back(Offset64, RelocSize, 0, Mapping); |
| 450 | } |
| 451 | } |
| 452 | } |
| 453 | |
| 454 | /// \brief Dispatch the valid relocation finding logic to the |
| 455 | /// appropriate handler depending on the object file format. |
| 456 | bool DwarfLinker::findValidRelocs(const object::SectionRef &Section, |
| 457 | const object::ObjectFile &Obj, |
| 458 | const DebugMapObject &DMO) { |
| 459 | // Dispatch to the right handler depending on the file type. |
| 460 | if (auto *MachOObj = dyn_cast<object::MachOObjectFile>(&Obj)) |
| 461 | findValidRelocsMachO(Section, *MachOObj, DMO); |
| 462 | else |
Frederic Riss | 1b9da42 | 2015-02-13 23:18:29 +0000 | [diff] [blame] | 463 | reportWarning(Twine("unsupported object file type: ") + Obj.getFileName()); |
Frederic Riss | 1036e64 | 2015-02-13 23:18:22 +0000 | [diff] [blame] | 464 | |
| 465 | if (ValidRelocs.empty()) |
| 466 | return false; |
| 467 | |
| 468 | // Sort the relocations by offset. We will walk the DIEs linearly in |
| 469 | // the file, this allows us to just keep an index in the relocation |
| 470 | // array that we advance during our walk, rather than resorting to |
| 471 | // some associative container. See DwarfLinker::NextValidReloc. |
| 472 | std::sort(ValidRelocs.begin(), ValidRelocs.end()); |
| 473 | return true; |
| 474 | } |
| 475 | |
| 476 | /// \brief Look for relocations in the debug_info section that match |
| 477 | /// entries in the debug map. These relocations will drive the Dwarf |
| 478 | /// link by indicating which DIEs refer to symbols present in the |
| 479 | /// linked binary. |
| 480 | /// \returns wether there are any valid relocations in the debug info. |
| 481 | bool DwarfLinker::findValidRelocsInDebugInfo(const object::ObjectFile &Obj, |
| 482 | const DebugMapObject &DMO) { |
| 483 | // Find the debug_info section. |
| 484 | for (const object::SectionRef &Section : Obj.sections()) { |
| 485 | StringRef SectionName; |
| 486 | Section.getName(SectionName); |
| 487 | SectionName = SectionName.substr(SectionName.find_first_not_of("._")); |
| 488 | if (SectionName != "debug_info") |
| 489 | continue; |
| 490 | return findValidRelocs(Section, Obj, DMO); |
| 491 | } |
| 492 | return false; |
| 493 | } |
Frederic Riss | 563cba6 | 2015-01-28 22:15:14 +0000 | [diff] [blame] | 494 | |
Frederic Riss | 84c09a5 | 2015-02-13 23:18:34 +0000 | [diff] [blame] | 495 | /// \brief Checks that there is a relocation against an actual debug |
| 496 | /// map entry between \p StartOffset and \p NextOffset. |
| 497 | /// |
| 498 | /// This function must be called with offsets in strictly ascending |
| 499 | /// order because it never looks back at relocations it already 'went past'. |
| 500 | /// \returns true and sets Info.InDebugMap if it is the case. |
| 501 | bool DwarfLinker::hasValidRelocation(uint32_t StartOffset, uint32_t EndOffset, |
| 502 | CompileUnit::DIEInfo &Info) { |
| 503 | assert(NextValidReloc == 0 || |
| 504 | StartOffset > ValidRelocs[NextValidReloc - 1].Offset); |
| 505 | if (NextValidReloc >= ValidRelocs.size()) |
| 506 | return false; |
| 507 | |
| 508 | uint64_t RelocOffset = ValidRelocs[NextValidReloc].Offset; |
| 509 | |
| 510 | // We might need to skip some relocs that we didn't consider. For |
| 511 | // example the high_pc of a discarded DIE might contain a reloc that |
| 512 | // is in the list because it actually corresponds to the start of a |
| 513 | // function that is in the debug map. |
| 514 | while (RelocOffset < StartOffset && NextValidReloc < ValidRelocs.size() - 1) |
| 515 | RelocOffset = ValidRelocs[++NextValidReloc].Offset; |
| 516 | |
| 517 | if (RelocOffset < StartOffset || RelocOffset >= EndOffset) |
| 518 | return false; |
| 519 | |
| 520 | const auto &ValidReloc = ValidRelocs[NextValidReloc++]; |
Frederic Riss | b981832 | 2015-02-28 00:29:07 +0000 | [diff] [blame] | 521 | if (Options.Verbose) |
Frederic Riss | 84c09a5 | 2015-02-13 23:18:34 +0000 | [diff] [blame] | 522 | outs() << "Found valid debug map entry: " << ValidReloc.Mapping->getKey() |
| 523 | << " " << format("\t%016" PRIx64 " => %016" PRIx64, |
| 524 | ValidReloc.Mapping->getValue().ObjectAddress, |
| 525 | ValidReloc.Mapping->getValue().BinaryAddress); |
| 526 | |
| 527 | Info.Address = |
| 528 | ValidReloc.Mapping->getValue().BinaryAddress + ValidReloc.Addend; |
| 529 | Info.InDebugMap = true; |
| 530 | return true; |
| 531 | } |
| 532 | |
| 533 | /// \brief Get the starting and ending (exclusive) offset for the |
| 534 | /// attribute with index \p Idx descibed by \p Abbrev. \p Offset is |
| 535 | /// supposed to point to the position of the first attribute described |
| 536 | /// by \p Abbrev. |
| 537 | /// \return [StartOffset, EndOffset) as a pair. |
| 538 | static std::pair<uint32_t, uint32_t> |
| 539 | getAttributeOffsets(const DWARFAbbreviationDeclaration *Abbrev, unsigned Idx, |
| 540 | unsigned Offset, const DWARFUnit &Unit) { |
| 541 | DataExtractor Data = Unit.getDebugInfoExtractor(); |
| 542 | |
| 543 | for (unsigned i = 0; i < Idx; ++i) |
| 544 | DWARFFormValue::skipValue(Abbrev->getFormByIndex(i), Data, &Offset, &Unit); |
| 545 | |
| 546 | uint32_t End = Offset; |
| 547 | DWARFFormValue::skipValue(Abbrev->getFormByIndex(Idx), Data, &End, &Unit); |
| 548 | |
| 549 | return std::make_pair(Offset, End); |
| 550 | } |
| 551 | |
| 552 | /// \brief Check if a variable describing DIE should be kept. |
| 553 | /// \returns updated TraversalFlags. |
| 554 | unsigned DwarfLinker::shouldKeepVariableDIE( |
| 555 | const DWARFDebugInfoEntryMinimal &DIE, CompileUnit &Unit, |
| 556 | CompileUnit::DIEInfo &MyInfo, unsigned Flags) { |
| 557 | const auto *Abbrev = DIE.getAbbreviationDeclarationPtr(); |
| 558 | |
| 559 | // Global variables with constant value can always be kept. |
| 560 | if (!(Flags & TF_InFunctionScope) && |
| 561 | Abbrev->findAttributeIndex(dwarf::DW_AT_const_value) != -1U) { |
| 562 | MyInfo.InDebugMap = true; |
| 563 | return Flags | TF_Keep; |
| 564 | } |
| 565 | |
| 566 | uint32_t LocationIdx = Abbrev->findAttributeIndex(dwarf::DW_AT_location); |
| 567 | if (LocationIdx == -1U) |
| 568 | return Flags; |
| 569 | |
| 570 | uint32_t Offset = DIE.getOffset() + getULEB128Size(Abbrev->getCode()); |
| 571 | const DWARFUnit &OrigUnit = Unit.getOrigUnit(); |
| 572 | uint32_t LocationOffset, LocationEndOffset; |
| 573 | std::tie(LocationOffset, LocationEndOffset) = |
| 574 | getAttributeOffsets(Abbrev, LocationIdx, Offset, OrigUnit); |
| 575 | |
| 576 | // See if there is a relocation to a valid debug map entry inside |
| 577 | // this variable's location. The order is important here. We want to |
| 578 | // always check in the variable has a valid relocation, so that the |
| 579 | // DIEInfo is filled. However, we don't want a static variable in a |
| 580 | // function to force us to keep the enclosing function. |
| 581 | if (!hasValidRelocation(LocationOffset, LocationEndOffset, MyInfo) || |
| 582 | (Flags & TF_InFunctionScope)) |
| 583 | return Flags; |
| 584 | |
Frederic Riss | b981832 | 2015-02-28 00:29:07 +0000 | [diff] [blame] | 585 | if (Options.Verbose) |
Frederic Riss | 84c09a5 | 2015-02-13 23:18:34 +0000 | [diff] [blame] | 586 | DIE.dump(outs(), const_cast<DWARFUnit *>(&OrigUnit), 0, 8 /* Indent */); |
| 587 | |
| 588 | return Flags | TF_Keep; |
| 589 | } |
| 590 | |
| 591 | /// \brief Check if a function describing DIE should be kept. |
| 592 | /// \returns updated TraversalFlags. |
| 593 | unsigned DwarfLinker::shouldKeepSubprogramDIE( |
| 594 | const DWARFDebugInfoEntryMinimal &DIE, CompileUnit &Unit, |
| 595 | CompileUnit::DIEInfo &MyInfo, unsigned Flags) { |
| 596 | const auto *Abbrev = DIE.getAbbreviationDeclarationPtr(); |
| 597 | |
| 598 | Flags |= TF_InFunctionScope; |
| 599 | |
| 600 | uint32_t LowPcIdx = Abbrev->findAttributeIndex(dwarf::DW_AT_low_pc); |
| 601 | if (LowPcIdx == -1U) |
| 602 | return Flags; |
| 603 | |
| 604 | uint32_t Offset = DIE.getOffset() + getULEB128Size(Abbrev->getCode()); |
| 605 | const DWARFUnit &OrigUnit = Unit.getOrigUnit(); |
| 606 | uint32_t LowPcOffset, LowPcEndOffset; |
| 607 | std::tie(LowPcOffset, LowPcEndOffset) = |
| 608 | getAttributeOffsets(Abbrev, LowPcIdx, Offset, OrigUnit); |
| 609 | |
| 610 | uint64_t LowPc = |
| 611 | DIE.getAttributeValueAsAddress(&OrigUnit, dwarf::DW_AT_low_pc, -1ULL); |
| 612 | assert(LowPc != -1ULL && "low_pc attribute is not an address."); |
| 613 | if (LowPc == -1ULL || |
| 614 | !hasValidRelocation(LowPcOffset, LowPcEndOffset, MyInfo)) |
| 615 | return Flags; |
| 616 | |
Frederic Riss | b981832 | 2015-02-28 00:29:07 +0000 | [diff] [blame] | 617 | if (Options.Verbose) |
Frederic Riss | 84c09a5 | 2015-02-13 23:18:34 +0000 | [diff] [blame] | 618 | DIE.dump(outs(), const_cast<DWARFUnit *>(&OrigUnit), 0, 8 /* Indent */); |
| 619 | |
| 620 | return Flags | TF_Keep; |
| 621 | } |
| 622 | |
| 623 | /// \brief Check if a DIE should be kept. |
| 624 | /// \returns updated TraversalFlags. |
| 625 | unsigned DwarfLinker::shouldKeepDIE(const DWARFDebugInfoEntryMinimal &DIE, |
| 626 | CompileUnit &Unit, |
| 627 | CompileUnit::DIEInfo &MyInfo, |
| 628 | unsigned Flags) { |
| 629 | switch (DIE.getTag()) { |
| 630 | case dwarf::DW_TAG_constant: |
| 631 | case dwarf::DW_TAG_variable: |
| 632 | return shouldKeepVariableDIE(DIE, Unit, MyInfo, Flags); |
| 633 | case dwarf::DW_TAG_subprogram: |
| 634 | return shouldKeepSubprogramDIE(DIE, Unit, MyInfo, Flags); |
| 635 | case dwarf::DW_TAG_module: |
| 636 | case dwarf::DW_TAG_imported_module: |
| 637 | case dwarf::DW_TAG_imported_declaration: |
| 638 | case dwarf::DW_TAG_imported_unit: |
| 639 | // We always want to keep these. |
| 640 | return Flags | TF_Keep; |
| 641 | } |
| 642 | |
| 643 | return Flags; |
| 644 | } |
| 645 | |
Frederic Riss | 84c09a5 | 2015-02-13 23:18:34 +0000 | [diff] [blame] | 646 | /// \brief Mark the passed DIE as well as all the ones it depends on |
| 647 | /// as kept. |
| 648 | /// |
| 649 | /// This function is called by lookForDIEsToKeep on DIEs that are |
| 650 | /// newly discovered to be needed in the link. It recursively calls |
| 651 | /// back to lookForDIEsToKeep while adding TF_DependencyWalk to the |
| 652 | /// TraversalFlags to inform it that it's not doing the primary DIE |
| 653 | /// tree walk. |
| 654 | void DwarfLinker::keepDIEAndDenpendencies(const DWARFDebugInfoEntryMinimal &DIE, |
| 655 | CompileUnit::DIEInfo &MyInfo, |
| 656 | const DebugMapObject &DMO, |
| 657 | CompileUnit &CU, unsigned Flags) { |
| 658 | const DWARFUnit &Unit = CU.getOrigUnit(); |
| 659 | MyInfo.Keep = true; |
| 660 | |
| 661 | // First mark all the parent chain as kept. |
| 662 | unsigned AncestorIdx = MyInfo.ParentIdx; |
| 663 | while (!CU.getInfo(AncestorIdx).Keep) { |
| 664 | lookForDIEsToKeep(*Unit.getDIEAtIndex(AncestorIdx), DMO, CU, |
| 665 | TF_ParentWalk | TF_Keep | TF_DependencyWalk); |
| 666 | AncestorIdx = CU.getInfo(AncestorIdx).ParentIdx; |
| 667 | } |
| 668 | |
| 669 | // Then we need to mark all the DIEs referenced by this DIE's |
| 670 | // attributes as kept. |
| 671 | DataExtractor Data = Unit.getDebugInfoExtractor(); |
| 672 | const auto *Abbrev = DIE.getAbbreviationDeclarationPtr(); |
| 673 | uint32_t Offset = DIE.getOffset() + getULEB128Size(Abbrev->getCode()); |
| 674 | |
| 675 | // Mark all DIEs referenced through atttributes as kept. |
| 676 | for (const auto &AttrSpec : Abbrev->attributes()) { |
| 677 | DWARFFormValue Val(AttrSpec.Form); |
| 678 | |
| 679 | if (!Val.isFormClass(DWARFFormValue::FC_Reference)) { |
| 680 | DWARFFormValue::skipValue(AttrSpec.Form, Data, &Offset, &Unit); |
| 681 | continue; |
| 682 | } |
| 683 | |
| 684 | Val.extractValue(Data, &Offset, &Unit); |
| 685 | CompileUnit *ReferencedCU; |
| 686 | if (const auto *RefDIE = resolveDIEReference(Val, Unit, DIE, ReferencedCU)) |
| 687 | lookForDIEsToKeep(*RefDIE, DMO, *ReferencedCU, |
| 688 | TF_Keep | TF_DependencyWalk); |
| 689 | } |
| 690 | } |
| 691 | |
| 692 | /// \brief Recursively walk the \p DIE tree and look for DIEs to |
| 693 | /// keep. Store that information in \p CU's DIEInfo. |
| 694 | /// |
| 695 | /// This function is the entry point of the DIE selection |
| 696 | /// algorithm. It is expected to walk the DIE tree in file order and |
| 697 | /// (though the mediation of its helper) call hasValidRelocation() on |
| 698 | /// each DIE that might be a 'root DIE' (See DwarfLinker class |
| 699 | /// comment). |
| 700 | /// While walking the dependencies of root DIEs, this function is |
| 701 | /// also called, but during these dependency walks the file order is |
| 702 | /// not respected. The TF_DependencyWalk flag tells us which kind of |
| 703 | /// traversal we are currently doing. |
| 704 | void DwarfLinker::lookForDIEsToKeep(const DWARFDebugInfoEntryMinimal &DIE, |
| 705 | const DebugMapObject &DMO, CompileUnit &CU, |
| 706 | unsigned Flags) { |
| 707 | unsigned Idx = CU.getOrigUnit().getDIEIndex(&DIE); |
| 708 | CompileUnit::DIEInfo &MyInfo = CU.getInfo(Idx); |
| 709 | bool AlreadyKept = MyInfo.Keep; |
| 710 | |
| 711 | // If the Keep flag is set, we are marking a required DIE's |
| 712 | // dependencies. If our target is already marked as kept, we're all |
| 713 | // set. |
| 714 | if ((Flags & TF_DependencyWalk) && AlreadyKept) |
| 715 | return; |
| 716 | |
| 717 | // We must not call shouldKeepDIE while called from keepDIEAndDenpendencies, |
| 718 | // because it would screw up the relocation finding logic. |
| 719 | if (!(Flags & TF_DependencyWalk)) |
| 720 | Flags = shouldKeepDIE(DIE, CU, MyInfo, Flags); |
| 721 | |
| 722 | // If it is a newly kept DIE mark it as well as all its dependencies as kept. |
| 723 | if (!AlreadyKept && (Flags & TF_Keep)) |
| 724 | keepDIEAndDenpendencies(DIE, MyInfo, DMO, CU, Flags); |
| 725 | |
| 726 | // The TF_ParentWalk flag tells us that we are currently walking up |
| 727 | // the parent chain of a required DIE, and we don't want to mark all |
| 728 | // the children of the parents as kept (consider for example a |
| 729 | // DW_TAG_namespace node in the parent chain). There are however a |
| 730 | // set of DIE types for which we want to ignore that directive and still |
| 731 | // walk their children. |
| 732 | if (dieNeedsChildrenToBeMeaningful(DIE.getTag())) |
| 733 | Flags &= ~TF_ParentWalk; |
| 734 | |
| 735 | if (!DIE.hasChildren() || (Flags & TF_ParentWalk)) |
| 736 | return; |
| 737 | |
| 738 | for (auto *Child = DIE.getFirstChild(); Child && !Child->isNULL(); |
| 739 | Child = Child->getSibling()) |
| 740 | lookForDIEsToKeep(*Child, DMO, CU, Flags); |
| 741 | } |
| 742 | |
Frederic Riss | d345518 | 2015-01-28 18:27:01 +0000 | [diff] [blame] | 743 | bool DwarfLinker::link(const DebugMap &Map) { |
| 744 | |
| 745 | if (Map.begin() == Map.end()) { |
| 746 | errs() << "Empty debug map.\n"; |
| 747 | return false; |
| 748 | } |
| 749 | |
Frederic Riss | c99ea20 | 2015-02-28 00:29:11 +0000 | [diff] [blame] | 750 | if (!createStreamer(Map.getTriple(), OutputFilename)) |
| 751 | return false; |
| 752 | |
Frederic Riss | d345518 | 2015-01-28 18:27:01 +0000 | [diff] [blame] | 753 | for (const auto &Obj : Map.objects()) { |
Frederic Riss | 1b9da42 | 2015-02-13 23:18:29 +0000 | [diff] [blame] | 754 | CurrentDebugObject = Obj.get(); |
| 755 | |
Frederic Riss | b981832 | 2015-02-28 00:29:07 +0000 | [diff] [blame] | 756 | if (Options.Verbose) |
Frederic Riss | d345518 | 2015-01-28 18:27:01 +0000 | [diff] [blame] | 757 | outs() << "DEBUG MAP OBJECT: " << Obj->getObjectFilename() << "\n"; |
| 758 | auto ErrOrObj = BinHolder.GetObjectFile(Obj->getObjectFilename()); |
| 759 | if (std::error_code EC = ErrOrObj.getError()) { |
Frederic Riss | 1b9da42 | 2015-02-13 23:18:29 +0000 | [diff] [blame] | 760 | reportWarning(Twine(Obj->getObjectFilename()) + ": " + EC.message()); |
Frederic Riss | d345518 | 2015-01-28 18:27:01 +0000 | [diff] [blame] | 761 | continue; |
| 762 | } |
| 763 | |
Frederic Riss | 1036e64 | 2015-02-13 23:18:22 +0000 | [diff] [blame] | 764 | // Look for relocations that correspond to debug map entries. |
| 765 | if (!findValidRelocsInDebugInfo(*ErrOrObj, *Obj)) { |
Frederic Riss | b981832 | 2015-02-28 00:29:07 +0000 | [diff] [blame] | 766 | if (Options.Verbose) |
Frederic Riss | 1036e64 | 2015-02-13 23:18:22 +0000 | [diff] [blame] | 767 | outs() << "No valid relocations found. Skipping.\n"; |
| 768 | continue; |
| 769 | } |
| 770 | |
Frederic Riss | 563cba6 | 2015-01-28 22:15:14 +0000 | [diff] [blame] | 771 | // Setup access to the debug info. |
Frederic Riss | d345518 | 2015-01-28 18:27:01 +0000 | [diff] [blame] | 772 | DWARFContextInMemory DwarfContext(*ErrOrObj); |
Frederic Riss | 563cba6 | 2015-01-28 22:15:14 +0000 | [diff] [blame] | 773 | startDebugObject(DwarfContext); |
Frederic Riss | d345518 | 2015-01-28 18:27:01 +0000 | [diff] [blame] | 774 | |
Frederic Riss | 563cba6 | 2015-01-28 22:15:14 +0000 | [diff] [blame] | 775 | // In a first phase, just read in the debug info and store the DIE |
| 776 | // parent links that we will use during the next phase. |
Frederic Riss | d345518 | 2015-01-28 18:27:01 +0000 | [diff] [blame] | 777 | for (const auto &CU : DwarfContext.compile_units()) { |
| 778 | auto *CUDie = CU->getCompileUnitDIE(false); |
Frederic Riss | b981832 | 2015-02-28 00:29:07 +0000 | [diff] [blame] | 779 | if (Options.Verbose) { |
Frederic Riss | d345518 | 2015-01-28 18:27:01 +0000 | [diff] [blame] | 780 | outs() << "Input compilation unit:"; |
| 781 | CUDie->dump(outs(), CU.get(), 0); |
| 782 | } |
Frederic Riss | 563cba6 | 2015-01-28 22:15:14 +0000 | [diff] [blame] | 783 | Units.emplace_back(*CU); |
Frederic Riss | 9aa725b | 2015-02-13 23:18:31 +0000 | [diff] [blame] | 784 | gatherDIEParents(CUDie, 0, Units.back()); |
Frederic Riss | d345518 | 2015-01-28 18:27:01 +0000 | [diff] [blame] | 785 | } |
Frederic Riss | 563cba6 | 2015-01-28 22:15:14 +0000 | [diff] [blame] | 786 | |
Frederic Riss | 84c09a5 | 2015-02-13 23:18:34 +0000 | [diff] [blame] | 787 | // Then mark all the DIEs that need to be present in the linked |
| 788 | // output and collect some information about them. Note that this |
| 789 | // loop can not be merged with the previous one becaue cross-cu |
| 790 | // references require the ParentIdx to be setup for every CU in |
| 791 | // the object file before calling this. |
| 792 | for (auto &CurrentUnit : Units) |
| 793 | lookForDIEsToKeep(*CurrentUnit.getOrigUnit().getCompileUnitDIE(), *Obj, |
| 794 | CurrentUnit, 0); |
| 795 | |
Frederic Riss | 563cba6 | 2015-01-28 22:15:14 +0000 | [diff] [blame] | 796 | // Clean-up before starting working on the next object. |
| 797 | endDebugObject(); |
Frederic Riss | d345518 | 2015-01-28 18:27:01 +0000 | [diff] [blame] | 798 | } |
| 799 | |
Frederic Riss | c99ea20 | 2015-02-28 00:29:11 +0000 | [diff] [blame] | 800 | return Options.NoOutput ? true : Streamer->finish(); |
Frederic Riss | 231f714 | 2014-12-12 17:31:24 +0000 | [diff] [blame] | 801 | } |
| 802 | } |
Frederic Riss | d345518 | 2015-01-28 18:27:01 +0000 | [diff] [blame] | 803 | |
Frederic Riss | b981832 | 2015-02-28 00:29:07 +0000 | [diff] [blame] | 804 | bool linkDwarf(StringRef OutputFilename, const DebugMap &DM, |
| 805 | const LinkOptions &Options) { |
| 806 | DwarfLinker Linker(OutputFilename, Options); |
Frederic Riss | d345518 | 2015-01-28 18:27:01 +0000 | [diff] [blame] | 807 | return Linker.link(DM); |
| 808 | } |
| 809 | } |
Frederic Riss | 231f714 | 2014-12-12 17:31:24 +0000 | [diff] [blame] | 810 | } |