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" |
Frederic Riss | b8b43d5 | 2015-03-04 22:07:44 +0000 | [diff] [blame] | 14 | #include "llvm/CodeGen/DIE.h" |
Zachary Turner | 82af943 | 2015-01-30 18:07:45 +0000 | [diff] [blame] | 15 | #include "llvm/DebugInfo/DWARF/DWARFContext.h" |
| 16 | #include "llvm/DebugInfo/DWARF/DWARFDebugInfoEntry.h" |
Frederic Riss | 1b9da42 | 2015-02-13 23:18:29 +0000 | [diff] [blame] | 17 | #include "llvm/DebugInfo/DWARF/DWARFFormValue.h" |
Frederic Riss | c99ea20 | 2015-02-28 00:29:11 +0000 | [diff] [blame] | 18 | #include "llvm/MC/MCAsmBackend.h" |
| 19 | #include "llvm/MC/MCAsmInfo.h" |
| 20 | #include "llvm/MC/MCContext.h" |
| 21 | #include "llvm/MC/MCCodeEmitter.h" |
| 22 | #include "llvm/MC/MCInstrInfo.h" |
| 23 | #include "llvm/MC/MCObjectFileInfo.h" |
| 24 | #include "llvm/MC/MCRegisterInfo.h" |
| 25 | #include "llvm/MC/MCStreamer.h" |
Frederic Riss | 1036e64 | 2015-02-13 23:18:22 +0000 | [diff] [blame] | 26 | #include "llvm/Object/MachO.h" |
Frederic Riss | 84c09a5 | 2015-02-13 23:18:34 +0000 | [diff] [blame] | 27 | #include "llvm/Support/Dwarf.h" |
| 28 | #include "llvm/Support/LEB128.h" |
Frederic Riss | c99ea20 | 2015-02-28 00:29:11 +0000 | [diff] [blame] | 29 | #include "llvm/Support/TargetRegistry.h" |
| 30 | #include "llvm/Target/TargetMachine.h" |
| 31 | #include "llvm/Target/TargetOptions.h" |
Frederic Riss | d345518 | 2015-01-28 18:27:01 +0000 | [diff] [blame] | 32 | #include <string> |
Frederic Riss | 231f714 | 2014-12-12 17:31:24 +0000 | [diff] [blame] | 33 | |
| 34 | namespace llvm { |
| 35 | namespace dsymutil { |
| 36 | |
Frederic Riss | d345518 | 2015-01-28 18:27:01 +0000 | [diff] [blame] | 37 | namespace { |
| 38 | |
Frederic Riss | def4fb7 | 2015-02-28 00:29:01 +0000 | [diff] [blame] | 39 | void warn(const Twine &Warning, const Twine &Context) { |
| 40 | errs() << Twine("while processing ") + Context + ":\n"; |
| 41 | errs() << Twine("warning: ") + Warning + "\n"; |
| 42 | } |
| 43 | |
Frederic Riss | c99ea20 | 2015-02-28 00:29:11 +0000 | [diff] [blame] | 44 | bool error(const Twine &Error, const Twine &Context) { |
| 45 | errs() << Twine("while processing ") + Context + ":\n"; |
| 46 | errs() << Twine("error: ") + Error + "\n"; |
| 47 | return false; |
| 48 | } |
| 49 | |
Frederic Riss | 563cba6 | 2015-01-28 22:15:14 +0000 | [diff] [blame] | 50 | /// \brief Stores all information relating to a compile unit, be it in |
| 51 | /// its original instance in the object file to its brand new cloned |
| 52 | /// and linked DIE tree. |
| 53 | class CompileUnit { |
| 54 | public: |
| 55 | /// \brief Information gathered about a DIE in the object file. |
| 56 | struct DIEInfo { |
Frederic Riss | 84c09a5 | 2015-02-13 23:18:34 +0000 | [diff] [blame] | 57 | uint64_t Address; ///< Linked address of the described entity. |
| 58 | uint32_t ParentIdx; ///< The index of this DIE's parent. |
| 59 | bool Keep; ///< Is the DIE part of the linked output? |
| 60 | bool InDebugMap; ///< Was this DIE's entity found in the map? |
Frederic Riss | 563cba6 | 2015-01-28 22:15:14 +0000 | [diff] [blame] | 61 | }; |
| 62 | |
| 63 | CompileUnit(DWARFUnit &OrigUnit) : OrigUnit(OrigUnit) { |
| 64 | Info.resize(OrigUnit.getNumDIEs()); |
| 65 | } |
| 66 | |
David Blaikie | a8adc13 | 2015-03-04 22:20:52 +0000 | [diff] [blame^] | 67 | // Workaround MSVC not supporting implicit move ops |
| 68 | CompileUnit(CompileUnit &&RHS) |
| 69 | : OrigUnit(RHS.OrigUnit), Info(std::move(RHS.Info)), |
| 70 | CUDie(std::move(RHS.CUDie)), StartOffset(RHS.StartOffset), |
| 71 | NextUnitOffset(RHS.NextUnitOffset) {} |
| 72 | |
Frederic Riss | c3349d4 | 2015-02-13 23:18:27 +0000 | [diff] [blame] | 73 | DWARFUnit &getOrigUnit() const { return OrigUnit; } |
Frederic Riss | 563cba6 | 2015-01-28 22:15:14 +0000 | [diff] [blame] | 74 | |
Frederic Riss | b8b43d5 | 2015-03-04 22:07:44 +0000 | [diff] [blame] | 75 | DIE *getOutputUnitDIE() const { return CUDie.get(); } |
| 76 | void setOutputUnitDIE(DIE *Die) { CUDie.reset(Die); } |
| 77 | |
Frederic Riss | 563cba6 | 2015-01-28 22:15:14 +0000 | [diff] [blame] | 78 | DIEInfo &getInfo(unsigned Idx) { return Info[Idx]; } |
| 79 | const DIEInfo &getInfo(unsigned Idx) const { return Info[Idx]; } |
| 80 | |
Frederic Riss | b8b43d5 | 2015-03-04 22:07:44 +0000 | [diff] [blame] | 81 | uint64_t getStartOffset() const { return StartOffset; } |
| 82 | uint64_t getNextUnitOffset() const { return NextUnitOffset; } |
| 83 | |
| 84 | /// \brief Set the start and end offsets for this unit. Must be |
| 85 | /// called after the CU's DIEs have been cloned. The unit start |
| 86 | /// offset will be set to \p DebugInfoSize. |
| 87 | /// \returns the next unit offset (which is also the current |
| 88 | /// debug_info section size). |
| 89 | uint64_t computeOffsets(uint64_t DebugInfoSize); |
| 90 | |
Frederic Riss | 563cba6 | 2015-01-28 22:15:14 +0000 | [diff] [blame] | 91 | private: |
| 92 | DWARFUnit &OrigUnit; |
Frederic Riss | b8b43d5 | 2015-03-04 22:07:44 +0000 | [diff] [blame] | 93 | std::vector<DIEInfo> Info; ///< DIE info indexed by DIE index. |
| 94 | std::unique_ptr<DIE> CUDie; ///< Root of the linked DIE tree. |
| 95 | |
| 96 | uint64_t StartOffset; |
| 97 | uint64_t NextUnitOffset; |
Frederic Riss | 563cba6 | 2015-01-28 22:15:14 +0000 | [diff] [blame] | 98 | }; |
| 99 | |
Frederic Riss | b8b43d5 | 2015-03-04 22:07:44 +0000 | [diff] [blame] | 100 | uint64_t CompileUnit::computeOffsets(uint64_t DebugInfoSize) { |
| 101 | StartOffset = DebugInfoSize; |
| 102 | NextUnitOffset = StartOffset + 11 /* Header size */; |
| 103 | // The root DIE might be null, meaning that the Unit had nothing to |
| 104 | // contribute to the linked output. In that case, we will emit the |
| 105 | // unit header without any actual DIE. |
| 106 | if (CUDie) |
| 107 | NextUnitOffset += CUDie->getSize(); |
| 108 | return NextUnitOffset; |
| 109 | } |
| 110 | |
Frederic Riss | c99ea20 | 2015-02-28 00:29:11 +0000 | [diff] [blame] | 111 | /// \brief The Dwarf streaming logic |
| 112 | /// |
| 113 | /// All interactions with the MC layer that is used to build the debug |
| 114 | /// information binary representation are handled in this class. |
| 115 | class DwarfStreamer { |
| 116 | /// \defgroup MCObjects MC layer objects constructed by the streamer |
| 117 | /// @{ |
| 118 | std::unique_ptr<MCRegisterInfo> MRI; |
| 119 | std::unique_ptr<MCAsmInfo> MAI; |
| 120 | std::unique_ptr<MCObjectFileInfo> MOFI; |
| 121 | std::unique_ptr<MCContext> MC; |
| 122 | MCAsmBackend *MAB; // Owned by MCStreamer |
| 123 | std::unique_ptr<MCInstrInfo> MII; |
| 124 | std::unique_ptr<MCSubtargetInfo> MSTI; |
| 125 | MCCodeEmitter *MCE; // Owned by MCStreamer |
| 126 | MCStreamer *MS; // Owned by AsmPrinter |
| 127 | std::unique_ptr<TargetMachine> TM; |
| 128 | std::unique_ptr<AsmPrinter> Asm; |
| 129 | /// @} |
| 130 | |
| 131 | /// \brief the file we stream the linked Dwarf to. |
| 132 | std::unique_ptr<raw_fd_ostream> OutFile; |
| 133 | |
| 134 | public: |
| 135 | /// \brief Actually create the streamer and the ouptut file. |
| 136 | /// |
| 137 | /// This could be done directly in the constructor, but it feels |
| 138 | /// more natural to handle errors through return value. |
| 139 | bool init(Triple TheTriple, StringRef OutputFilename); |
| 140 | |
Frederic Riss | b8b43d5 | 2015-03-04 22:07:44 +0000 | [diff] [blame] | 141 | /// \brief Dump the file to the disk. |
Frederic Riss | c99ea20 | 2015-02-28 00:29:11 +0000 | [diff] [blame] | 142 | bool finish(); |
Frederic Riss | b8b43d5 | 2015-03-04 22:07:44 +0000 | [diff] [blame] | 143 | |
| 144 | AsmPrinter &getAsmPrinter() const { return *Asm; } |
| 145 | |
| 146 | /// \brief Set the current output section to debug_info and change |
| 147 | /// the MC Dwarf version to \p DwarfVersion. |
| 148 | void switchToDebugInfoSection(unsigned DwarfVersion); |
| 149 | |
| 150 | /// \brief Emit the compilation unit header for \p Unit in the |
| 151 | /// debug_info section. |
| 152 | /// |
| 153 | /// As a side effect, this also switches the current Dwarf version |
| 154 | /// of the MC layer to the one of U.getOrigUnit(). |
| 155 | void emitCompileUnitHeader(CompileUnit &Unit); |
| 156 | |
| 157 | /// \brief Recursively emit the DIE tree rooted at \p Die. |
| 158 | void emitDIE(DIE &Die); |
| 159 | |
| 160 | /// \brief Emit the abbreviation table \p Abbrevs to the |
| 161 | /// debug_abbrev section. |
| 162 | void emitAbbrevs(const std::vector<DIEAbbrev *> &Abbrevs); |
Frederic Riss | c99ea20 | 2015-02-28 00:29:11 +0000 | [diff] [blame] | 163 | }; |
| 164 | |
| 165 | bool DwarfStreamer::init(Triple TheTriple, StringRef OutputFilename) { |
| 166 | std::string ErrorStr; |
| 167 | std::string TripleName; |
| 168 | StringRef Context = "dwarf streamer init"; |
| 169 | |
| 170 | // Get the target. |
| 171 | const Target *TheTarget = |
| 172 | TargetRegistry::lookupTarget(TripleName, TheTriple, ErrorStr); |
| 173 | if (!TheTarget) |
| 174 | return error(ErrorStr, Context); |
| 175 | TripleName = TheTriple.getTriple(); |
| 176 | |
| 177 | // Create all the MC Objects. |
| 178 | MRI.reset(TheTarget->createMCRegInfo(TripleName)); |
| 179 | if (!MRI) |
| 180 | return error(Twine("no register info for target ") + TripleName, Context); |
| 181 | |
| 182 | MAI.reset(TheTarget->createMCAsmInfo(*MRI, TripleName)); |
| 183 | if (!MAI) |
| 184 | return error("no asm info for target " + TripleName, Context); |
| 185 | |
| 186 | MOFI.reset(new MCObjectFileInfo); |
| 187 | MC.reset(new MCContext(MAI.get(), MRI.get(), MOFI.get())); |
| 188 | MOFI->InitMCObjectFileInfo(TripleName, Reloc::Default, CodeModel::Default, |
| 189 | *MC); |
| 190 | |
| 191 | MAB = TheTarget->createMCAsmBackend(*MRI, TripleName, ""); |
| 192 | if (!MAB) |
| 193 | return error("no asm backend for target " + TripleName, Context); |
| 194 | |
| 195 | MII.reset(TheTarget->createMCInstrInfo()); |
| 196 | if (!MII) |
| 197 | return error("no instr info info for target " + TripleName, Context); |
| 198 | |
| 199 | MSTI.reset(TheTarget->createMCSubtargetInfo(TripleName, "", "")); |
| 200 | if (!MSTI) |
| 201 | return error("no subtarget info for target " + TripleName, Context); |
| 202 | |
| 203 | MCE = TheTarget->createMCCodeEmitter(*MII, *MRI, *MSTI, *MC); |
| 204 | if (!MCE) |
| 205 | return error("no code emitter for target " + TripleName, Context); |
| 206 | |
| 207 | // Create the output file. |
| 208 | std::error_code EC; |
Frederic Riss | b8b43d5 | 2015-03-04 22:07:44 +0000 | [diff] [blame] | 209 | OutFile = |
| 210 | llvm::make_unique<raw_fd_ostream>(OutputFilename, EC, sys::fs::F_None); |
Frederic Riss | c99ea20 | 2015-02-28 00:29:11 +0000 | [diff] [blame] | 211 | if (EC) |
| 212 | return error(Twine(OutputFilename) + ": " + EC.message(), Context); |
| 213 | |
| 214 | MS = TheTarget->createMCObjectStreamer(TripleName, *MC, *MAB, *OutFile, MCE, |
| 215 | *MSTI, false); |
| 216 | if (!MS) |
| 217 | return error("no object streamer for target " + TripleName, Context); |
| 218 | |
| 219 | // Finally create the AsmPrinter we'll use to emit the DIEs. |
| 220 | TM.reset(TheTarget->createTargetMachine(TripleName, "", "", TargetOptions())); |
| 221 | if (!TM) |
| 222 | return error("no target machine for target " + TripleName, Context); |
| 223 | |
| 224 | Asm.reset(TheTarget->createAsmPrinter(*TM, std::unique_ptr<MCStreamer>(MS))); |
| 225 | if (!Asm) |
| 226 | return error("no asm printer for target " + TripleName, Context); |
| 227 | |
| 228 | return true; |
| 229 | } |
| 230 | |
| 231 | bool DwarfStreamer::finish() { |
| 232 | MS->Finish(); |
| 233 | return true; |
| 234 | } |
| 235 | |
Frederic Riss | b8b43d5 | 2015-03-04 22:07:44 +0000 | [diff] [blame] | 236 | /// \brief Set the current output section to debug_info and change |
| 237 | /// the MC Dwarf version to \p DwarfVersion. |
| 238 | void DwarfStreamer::switchToDebugInfoSection(unsigned DwarfVersion) { |
| 239 | MS->SwitchSection(MOFI->getDwarfInfoSection()); |
| 240 | MC->setDwarfVersion(DwarfVersion); |
| 241 | } |
| 242 | |
| 243 | /// \brief Emit the compilation unit header for \p Unit in the |
| 244 | /// debug_info section. |
| 245 | /// |
| 246 | /// A Dwarf scetion header is encoded as: |
| 247 | /// uint32_t Unit length (omiting this field) |
| 248 | /// uint16_t Version |
| 249 | /// uint32_t Abbreviation table offset |
| 250 | /// uint8_t Address size |
| 251 | /// |
| 252 | /// Leading to a total of 11 bytes. |
| 253 | void DwarfStreamer::emitCompileUnitHeader(CompileUnit &Unit) { |
| 254 | unsigned Version = Unit.getOrigUnit().getVersion(); |
| 255 | switchToDebugInfoSection(Version); |
| 256 | |
| 257 | // Emit size of content not including length itself. The size has |
| 258 | // already been computed in CompileUnit::computeOffsets(). Substract |
| 259 | // 4 to that size to account for the length field. |
| 260 | Asm->EmitInt32(Unit.getNextUnitOffset() - Unit.getStartOffset() - 4); |
| 261 | Asm->EmitInt16(Version); |
| 262 | // We share one abbreviations table across all units so it's always at the |
| 263 | // start of the section. |
| 264 | Asm->EmitInt32(0); |
| 265 | Asm->EmitInt8(Unit.getOrigUnit().getAddressByteSize()); |
| 266 | } |
| 267 | |
| 268 | /// \brief Emit the \p Abbrevs array as the shared abbreviation table |
| 269 | /// for the linked Dwarf file. |
| 270 | void DwarfStreamer::emitAbbrevs(const std::vector<DIEAbbrev *> &Abbrevs) { |
| 271 | MS->SwitchSection(MOFI->getDwarfAbbrevSection()); |
| 272 | Asm->emitDwarfAbbrevs(Abbrevs); |
| 273 | } |
| 274 | |
| 275 | /// \brief Recursively emit the DIE tree rooted at \p Die. |
| 276 | void DwarfStreamer::emitDIE(DIE &Die) { |
| 277 | MS->SwitchSection(MOFI->getDwarfInfoSection()); |
| 278 | Asm->emitDwarfDIE(Die); |
| 279 | } |
| 280 | |
Frederic Riss | d345518 | 2015-01-28 18:27:01 +0000 | [diff] [blame] | 281 | /// \brief The core of the Dwarf linking logic. |
Frederic Riss | 1036e64 | 2015-02-13 23:18:22 +0000 | [diff] [blame] | 282 | /// |
| 283 | /// The link of the dwarf information from the object files will be |
| 284 | /// driven by the selection of 'root DIEs', which are DIEs that |
| 285 | /// describe variables or functions that are present in the linked |
| 286 | /// binary (and thus have entries in the debug map). All the debug |
| 287 | /// information that will be linked (the DIEs, but also the line |
| 288 | /// tables, ranges, ...) is derived from that set of root DIEs. |
| 289 | /// |
| 290 | /// The root DIEs are identified because they contain relocations that |
| 291 | /// correspond to a debug map entry at specific places (the low_pc for |
| 292 | /// a function, the location for a variable). These relocations are |
| 293 | /// called ValidRelocs in the DwarfLinker and are gathered as a very |
| 294 | /// first step when we start processing a DebugMapObject. |
Frederic Riss | d345518 | 2015-01-28 18:27:01 +0000 | [diff] [blame] | 295 | class DwarfLinker { |
| 296 | public: |
Frederic Riss | b981832 | 2015-02-28 00:29:07 +0000 | [diff] [blame] | 297 | DwarfLinker(StringRef OutputFilename, const LinkOptions &Options) |
| 298 | : OutputFilename(OutputFilename), Options(Options), |
| 299 | BinHolder(Options.Verbose) {} |
Frederic Riss | d345518 | 2015-01-28 18:27:01 +0000 | [diff] [blame] | 300 | |
Frederic Riss | b8b43d5 | 2015-03-04 22:07:44 +0000 | [diff] [blame] | 301 | ~DwarfLinker() { |
| 302 | for (auto *Abbrev : Abbreviations) |
| 303 | delete Abbrev; |
| 304 | } |
| 305 | |
Frederic Riss | d345518 | 2015-01-28 18:27:01 +0000 | [diff] [blame] | 306 | /// \brief Link the contents of the DebugMap. |
| 307 | bool link(const DebugMap &); |
| 308 | |
| 309 | private: |
Frederic Riss | 563cba6 | 2015-01-28 22:15:14 +0000 | [diff] [blame] | 310 | /// \brief Called at the start of a debug object link. |
| 311 | void startDebugObject(DWARFContext &); |
| 312 | |
| 313 | /// \brief Called at the end of a debug object link. |
| 314 | void endDebugObject(); |
| 315 | |
Frederic Riss | 1036e64 | 2015-02-13 23:18:22 +0000 | [diff] [blame] | 316 | /// \defgroup FindValidRelocations Translate debug map into a list |
| 317 | /// of relevant relocations |
| 318 | /// |
| 319 | /// @{ |
| 320 | struct ValidReloc { |
| 321 | uint32_t Offset; |
| 322 | uint32_t Size; |
| 323 | uint64_t Addend; |
| 324 | const DebugMapObject::DebugMapEntry *Mapping; |
| 325 | |
| 326 | ValidReloc(uint32_t Offset, uint32_t Size, uint64_t Addend, |
| 327 | const DebugMapObject::DebugMapEntry *Mapping) |
| 328 | : Offset(Offset), Size(Size), Addend(Addend), Mapping(Mapping) {} |
| 329 | |
| 330 | bool operator<(const ValidReloc &RHS) const { return Offset < RHS.Offset; } |
| 331 | }; |
| 332 | |
| 333 | /// \brief The valid relocations for the current DebugMapObject. |
| 334 | /// This vector is sorted by relocation offset. |
| 335 | std::vector<ValidReloc> ValidRelocs; |
| 336 | |
| 337 | /// \brief Index into ValidRelocs of the next relocation to |
| 338 | /// consider. As we walk the DIEs in acsending file offset and as |
| 339 | /// ValidRelocs is sorted by file offset, keeping this index |
| 340 | /// uptodate is all we have to do to have a cheap lookup during the |
| 341 | /// root DIE selection. |
| 342 | unsigned NextValidReloc; |
| 343 | |
| 344 | bool findValidRelocsInDebugInfo(const object::ObjectFile &Obj, |
| 345 | const DebugMapObject &DMO); |
| 346 | |
| 347 | bool findValidRelocs(const object::SectionRef &Section, |
| 348 | const object::ObjectFile &Obj, |
| 349 | const DebugMapObject &DMO); |
| 350 | |
| 351 | void findValidRelocsMachO(const object::SectionRef &Section, |
| 352 | const object::MachOObjectFile &Obj, |
| 353 | const DebugMapObject &DMO); |
| 354 | /// @} |
Frederic Riss | 1b9da42 | 2015-02-13 23:18:29 +0000 | [diff] [blame] | 355 | |
Frederic Riss | 84c09a5 | 2015-02-13 23:18:34 +0000 | [diff] [blame] | 356 | /// \defgroup FindRootDIEs Find DIEs corresponding to debug map entries. |
| 357 | /// |
| 358 | /// @{ |
| 359 | /// \brief Recursively walk the \p DIE tree and look for DIEs to |
| 360 | /// keep. Store that information in \p CU's DIEInfo. |
| 361 | void lookForDIEsToKeep(const DWARFDebugInfoEntryMinimal &DIE, |
| 362 | const DebugMapObject &DMO, CompileUnit &CU, |
| 363 | unsigned Flags); |
| 364 | |
| 365 | /// \brief Flags passed to DwarfLinker::lookForDIEsToKeep |
| 366 | enum TravesalFlags { |
| 367 | TF_Keep = 1 << 0, ///< Mark the traversed DIEs as kept. |
| 368 | TF_InFunctionScope = 1 << 1, ///< Current scope is a fucntion scope. |
| 369 | TF_DependencyWalk = 1 << 2, ///< Walking the dependencies of a kept DIE. |
| 370 | TF_ParentWalk = 1 << 3, ///< Walking up the parents of a kept DIE. |
| 371 | }; |
| 372 | |
| 373 | /// \brief Mark the passed DIE as well as all the ones it depends on |
| 374 | /// as kept. |
| 375 | void keepDIEAndDenpendencies(const DWARFDebugInfoEntryMinimal &DIE, |
| 376 | CompileUnit::DIEInfo &MyInfo, |
| 377 | const DebugMapObject &DMO, CompileUnit &CU, |
| 378 | unsigned Flags); |
| 379 | |
| 380 | unsigned shouldKeepDIE(const DWARFDebugInfoEntryMinimal &DIE, |
| 381 | CompileUnit &Unit, CompileUnit::DIEInfo &MyInfo, |
| 382 | unsigned Flags); |
| 383 | |
| 384 | unsigned shouldKeepVariableDIE(const DWARFDebugInfoEntryMinimal &DIE, |
| 385 | CompileUnit &Unit, |
| 386 | CompileUnit::DIEInfo &MyInfo, unsigned Flags); |
| 387 | |
| 388 | unsigned shouldKeepSubprogramDIE(const DWARFDebugInfoEntryMinimal &DIE, |
| 389 | CompileUnit &Unit, |
| 390 | CompileUnit::DIEInfo &MyInfo, |
| 391 | unsigned Flags); |
| 392 | |
| 393 | bool hasValidRelocation(uint32_t StartOffset, uint32_t EndOffset, |
| 394 | CompileUnit::DIEInfo &Info); |
| 395 | /// @} |
| 396 | |
Frederic Riss | b8b43d5 | 2015-03-04 22:07:44 +0000 | [diff] [blame] | 397 | /// \defgroup Linking Methods used to link the debug information |
| 398 | /// |
| 399 | /// @{ |
| 400 | /// \brief Recursively clone \p InputDIE into an tree of DIE objects |
| 401 | /// where useless (as decided by lookForDIEsToKeep()) bits have been |
| 402 | /// stripped out and addresses have been rewritten according to the |
| 403 | /// debug map. |
| 404 | /// |
| 405 | /// \param OutOffset is the offset the cloned DIE in the output |
| 406 | /// compile unit. |
| 407 | /// |
| 408 | /// \returns the root of the cloned tree. |
| 409 | DIE *cloneDIE(const DWARFDebugInfoEntryMinimal &InputDIE, CompileUnit &U, |
| 410 | uint32_t OutOffset); |
| 411 | |
| 412 | typedef DWARFAbbreviationDeclaration::AttributeSpec AttributeSpec; |
| 413 | |
| 414 | /// \brief Helper for cloneDIE. |
| 415 | unsigned cloneAttribute(DIE &Die, const DWARFDebugInfoEntryMinimal &InputDIE, |
| 416 | CompileUnit &U, const DWARFFormValue &Val, |
| 417 | const AttributeSpec AttrSpec, unsigned AttrSize); |
| 418 | |
| 419 | /// \brief Helper for cloneDIE. |
| 420 | unsigned cloneStringAttribute(DIE &Die, AttributeSpec AttrSpec); |
| 421 | |
| 422 | /// \brief Helper for cloneDIE. |
| 423 | unsigned cloneDieReferenceAttribute(DIE &Die, AttributeSpec AttrSpec, |
| 424 | unsigned AttrSize); |
| 425 | |
| 426 | /// \brief Helper for cloneDIE. |
| 427 | unsigned cloneBlockAttribute(DIE &Die, AttributeSpec AttrSpec, |
| 428 | const DWARFFormValue &Val, unsigned AttrSize); |
| 429 | |
| 430 | /// \brief Helper for cloneDIE. |
| 431 | unsigned cloneScalarAttribute(DIE &Die, |
| 432 | const DWARFDebugInfoEntryMinimal &InputDIE, |
| 433 | const DWARFUnit &U, AttributeSpec AttrSpec, |
| 434 | const DWARFFormValue &Val, unsigned AttrSize); |
| 435 | |
| 436 | /// \brief Assign an abbreviation number to \p Abbrev |
| 437 | void AssignAbbrev(DIEAbbrev &Abbrev); |
| 438 | |
| 439 | /// \brief FoldingSet that uniques the abbreviations. |
| 440 | FoldingSet<DIEAbbrev> AbbreviationsSet; |
| 441 | /// \brief Storage for the unique Abbreviations. |
| 442 | /// This is passed to AsmPrinter::emitDwarfAbbrevs(), thus it cannot |
| 443 | /// be changed to a vecot of unique_ptrs. |
| 444 | std::vector<DIEAbbrev *> Abbreviations; |
| 445 | |
| 446 | /// \brief DIELoc objects that need to be destructed (but not freed!). |
| 447 | std::vector<DIELoc *> DIELocs; |
| 448 | /// \brief DIEBlock objects that need to be destructed (but not freed!). |
| 449 | std::vector<DIEBlock *> DIEBlocks; |
| 450 | /// \brief Allocator used for all the DIEValue objects. |
| 451 | BumpPtrAllocator DIEAlloc; |
| 452 | /// @} |
| 453 | |
Frederic Riss | 1b9da42 | 2015-02-13 23:18:29 +0000 | [diff] [blame] | 454 | /// \defgroup Helpers Various helper methods. |
| 455 | /// |
| 456 | /// @{ |
| 457 | const DWARFDebugInfoEntryMinimal * |
| 458 | resolveDIEReference(DWARFFormValue &RefValue, const DWARFUnit &Unit, |
| 459 | const DWARFDebugInfoEntryMinimal &DIE, |
| 460 | CompileUnit *&ReferencedCU); |
| 461 | |
| 462 | CompileUnit *getUnitForOffset(unsigned Offset); |
| 463 | |
| 464 | void reportWarning(const Twine &Warning, const DWARFUnit *Unit = nullptr, |
| 465 | const DWARFDebugInfoEntryMinimal *DIE = nullptr); |
Frederic Riss | c99ea20 | 2015-02-28 00:29:11 +0000 | [diff] [blame] | 466 | |
| 467 | bool createStreamer(Triple TheTriple, StringRef OutputFilename); |
Frederic Riss | 1b9da42 | 2015-02-13 23:18:29 +0000 | [diff] [blame] | 468 | /// @} |
| 469 | |
Frederic Riss | 563cba6 | 2015-01-28 22:15:14 +0000 | [diff] [blame] | 470 | private: |
Frederic Riss | d345518 | 2015-01-28 18:27:01 +0000 | [diff] [blame] | 471 | std::string OutputFilename; |
Frederic Riss | b981832 | 2015-02-28 00:29:07 +0000 | [diff] [blame] | 472 | LinkOptions Options; |
Frederic Riss | d345518 | 2015-01-28 18:27:01 +0000 | [diff] [blame] | 473 | BinaryHolder BinHolder; |
Frederic Riss | c99ea20 | 2015-02-28 00:29:11 +0000 | [diff] [blame] | 474 | std::unique_ptr<DwarfStreamer> Streamer; |
Frederic Riss | 563cba6 | 2015-01-28 22:15:14 +0000 | [diff] [blame] | 475 | |
| 476 | /// The units of the current debug map object. |
| 477 | std::vector<CompileUnit> Units; |
Frederic Riss | 1b9da42 | 2015-02-13 23:18:29 +0000 | [diff] [blame] | 478 | |
| 479 | /// The debug map object curently under consideration. |
| 480 | DebugMapObject *CurrentDebugObject; |
Frederic Riss | d345518 | 2015-01-28 18:27:01 +0000 | [diff] [blame] | 481 | }; |
| 482 | |
Frederic Riss | 1b9da42 | 2015-02-13 23:18:29 +0000 | [diff] [blame] | 483 | /// \brief Similar to DWARFUnitSection::getUnitForOffset(), but |
| 484 | /// returning our CompileUnit object instead. |
| 485 | CompileUnit *DwarfLinker::getUnitForOffset(unsigned Offset) { |
| 486 | auto CU = |
| 487 | std::upper_bound(Units.begin(), Units.end(), Offset, |
| 488 | [](uint32_t LHS, const CompileUnit &RHS) { |
| 489 | return LHS < RHS.getOrigUnit().getNextUnitOffset(); |
| 490 | }); |
| 491 | return CU != Units.end() ? &*CU : nullptr; |
| 492 | } |
| 493 | |
| 494 | /// \brief Resolve the DIE attribute reference that has been |
| 495 | /// extracted in \p RefValue. The resulting DIE migh be in another |
| 496 | /// CompileUnit which is stored into \p ReferencedCU. |
| 497 | /// \returns null if resolving fails for any reason. |
| 498 | const DWARFDebugInfoEntryMinimal *DwarfLinker::resolveDIEReference( |
| 499 | DWARFFormValue &RefValue, const DWARFUnit &Unit, |
| 500 | const DWARFDebugInfoEntryMinimal &DIE, CompileUnit *&RefCU) { |
| 501 | assert(RefValue.isFormClass(DWARFFormValue::FC_Reference)); |
| 502 | uint64_t RefOffset = *RefValue.getAsReference(&Unit); |
| 503 | |
| 504 | if ((RefCU = getUnitForOffset(RefOffset))) |
| 505 | if (const auto *RefDie = RefCU->getOrigUnit().getDIEForOffset(RefOffset)) |
| 506 | return RefDie; |
| 507 | |
| 508 | reportWarning("could not find referenced DIE", &Unit, &DIE); |
| 509 | return nullptr; |
| 510 | } |
| 511 | |
| 512 | /// \brief Report a warning to the user, optionaly including |
| 513 | /// information about a specific \p DIE related to the warning. |
| 514 | void DwarfLinker::reportWarning(const Twine &Warning, const DWARFUnit *Unit, |
| 515 | const DWARFDebugInfoEntryMinimal *DIE) { |
Frederic Riss | def4fb7 | 2015-02-28 00:29:01 +0000 | [diff] [blame] | 516 | StringRef Context = "<debug map>"; |
Frederic Riss | 1b9da42 | 2015-02-13 23:18:29 +0000 | [diff] [blame] | 517 | if (CurrentDebugObject) |
Frederic Riss | def4fb7 | 2015-02-28 00:29:01 +0000 | [diff] [blame] | 518 | Context = CurrentDebugObject->getObjectFilename(); |
| 519 | warn(Warning, Context); |
Frederic Riss | 1b9da42 | 2015-02-13 23:18:29 +0000 | [diff] [blame] | 520 | |
Frederic Riss | b981832 | 2015-02-28 00:29:07 +0000 | [diff] [blame] | 521 | if (!Options.Verbose || !DIE) |
Frederic Riss | 1b9da42 | 2015-02-13 23:18:29 +0000 | [diff] [blame] | 522 | return; |
| 523 | |
| 524 | errs() << " in DIE:\n"; |
| 525 | DIE->dump(errs(), const_cast<DWARFUnit *>(Unit), 0 /* RecurseDepth */, |
| 526 | 6 /* Indent */); |
| 527 | } |
| 528 | |
Frederic Riss | c99ea20 | 2015-02-28 00:29:11 +0000 | [diff] [blame] | 529 | bool DwarfLinker::createStreamer(Triple TheTriple, StringRef OutputFilename) { |
| 530 | if (Options.NoOutput) |
| 531 | return true; |
| 532 | |
Frederic Riss | b52cf52 | 2015-02-28 00:42:37 +0000 | [diff] [blame] | 533 | Streamer = llvm::make_unique<DwarfStreamer>(); |
Frederic Riss | c99ea20 | 2015-02-28 00:29:11 +0000 | [diff] [blame] | 534 | return Streamer->init(TheTriple, OutputFilename); |
| 535 | } |
| 536 | |
Frederic Riss | 563cba6 | 2015-01-28 22:15:14 +0000 | [diff] [blame] | 537 | /// \brief Recursive helper to gather the child->parent relationships in the |
| 538 | /// original compile unit. |
Frederic Riss | 9aa725b | 2015-02-13 23:18:31 +0000 | [diff] [blame] | 539 | static void gatherDIEParents(const DWARFDebugInfoEntryMinimal *DIE, |
| 540 | unsigned ParentIdx, CompileUnit &CU) { |
Frederic Riss | 563cba6 | 2015-01-28 22:15:14 +0000 | [diff] [blame] | 541 | unsigned MyIdx = CU.getOrigUnit().getDIEIndex(DIE); |
| 542 | CU.getInfo(MyIdx).ParentIdx = ParentIdx; |
| 543 | |
| 544 | if (DIE->hasChildren()) |
| 545 | for (auto *Child = DIE->getFirstChild(); Child && !Child->isNULL(); |
| 546 | Child = Child->getSibling()) |
Frederic Riss | 9aa725b | 2015-02-13 23:18:31 +0000 | [diff] [blame] | 547 | gatherDIEParents(Child, MyIdx, CU); |
Frederic Riss | 563cba6 | 2015-01-28 22:15:14 +0000 | [diff] [blame] | 548 | } |
| 549 | |
Frederic Riss | 84c09a5 | 2015-02-13 23:18:34 +0000 | [diff] [blame] | 550 | static bool dieNeedsChildrenToBeMeaningful(uint32_t Tag) { |
| 551 | switch (Tag) { |
| 552 | default: |
| 553 | return false; |
| 554 | case dwarf::DW_TAG_subprogram: |
| 555 | case dwarf::DW_TAG_lexical_block: |
| 556 | case dwarf::DW_TAG_subroutine_type: |
| 557 | case dwarf::DW_TAG_structure_type: |
| 558 | case dwarf::DW_TAG_class_type: |
| 559 | case dwarf::DW_TAG_union_type: |
| 560 | return true; |
| 561 | } |
| 562 | llvm_unreachable("Invalid Tag"); |
| 563 | } |
| 564 | |
Frederic Riss | 563cba6 | 2015-01-28 22:15:14 +0000 | [diff] [blame] | 565 | void DwarfLinker::startDebugObject(DWARFContext &Dwarf) { |
| 566 | Units.reserve(Dwarf.getNumCompileUnits()); |
Frederic Riss | 1036e64 | 2015-02-13 23:18:22 +0000 | [diff] [blame] | 567 | NextValidReloc = 0; |
Frederic Riss | 563cba6 | 2015-01-28 22:15:14 +0000 | [diff] [blame] | 568 | } |
| 569 | |
Frederic Riss | 1036e64 | 2015-02-13 23:18:22 +0000 | [diff] [blame] | 570 | void DwarfLinker::endDebugObject() { |
| 571 | Units.clear(); |
| 572 | ValidRelocs.clear(); |
Frederic Riss | b8b43d5 | 2015-03-04 22:07:44 +0000 | [diff] [blame] | 573 | |
| 574 | for (auto *Block : DIEBlocks) |
| 575 | Block->~DIEBlock(); |
| 576 | for (auto *Loc : DIELocs) |
| 577 | Loc->~DIELoc(); |
| 578 | |
| 579 | DIEBlocks.clear(); |
| 580 | DIELocs.clear(); |
| 581 | DIEAlloc.Reset(); |
Frederic Riss | 1036e64 | 2015-02-13 23:18:22 +0000 | [diff] [blame] | 582 | } |
| 583 | |
| 584 | /// \brief Iterate over the relocations of the given \p Section and |
| 585 | /// store the ones that correspond to debug map entries into the |
| 586 | /// ValidRelocs array. |
| 587 | void DwarfLinker::findValidRelocsMachO(const object::SectionRef &Section, |
| 588 | const object::MachOObjectFile &Obj, |
| 589 | const DebugMapObject &DMO) { |
| 590 | StringRef Contents; |
| 591 | Section.getContents(Contents); |
| 592 | DataExtractor Data(Contents, Obj.isLittleEndian(), 0); |
| 593 | |
| 594 | for (const object::RelocationRef &Reloc : Section.relocations()) { |
| 595 | object::DataRefImpl RelocDataRef = Reloc.getRawDataRefImpl(); |
| 596 | MachO::any_relocation_info MachOReloc = Obj.getRelocation(RelocDataRef); |
| 597 | unsigned RelocSize = 1 << Obj.getAnyRelocationLength(MachOReloc); |
| 598 | uint64_t Offset64; |
| 599 | if ((RelocSize != 4 && RelocSize != 8) || Reloc.getOffset(Offset64)) { |
Frederic Riss | 1b9da42 | 2015-02-13 23:18:29 +0000 | [diff] [blame] | 600 | reportWarning(" unsupported relocation in debug_info section."); |
Frederic Riss | 1036e64 | 2015-02-13 23:18:22 +0000 | [diff] [blame] | 601 | continue; |
| 602 | } |
| 603 | uint32_t Offset = Offset64; |
| 604 | // Mach-o uses REL relocations, the addend is at the relocation offset. |
| 605 | uint64_t Addend = Data.getUnsigned(&Offset, RelocSize); |
| 606 | |
| 607 | auto Sym = Reloc.getSymbol(); |
| 608 | if (Sym != Obj.symbol_end()) { |
| 609 | StringRef SymbolName; |
| 610 | if (Sym->getName(SymbolName)) { |
Frederic Riss | 1b9da42 | 2015-02-13 23:18:29 +0000 | [diff] [blame] | 611 | reportWarning("error getting relocation symbol name."); |
Frederic Riss | 1036e64 | 2015-02-13 23:18:22 +0000 | [diff] [blame] | 612 | continue; |
| 613 | } |
| 614 | if (const auto *Mapping = DMO.lookupSymbol(SymbolName)) |
| 615 | ValidRelocs.emplace_back(Offset64, RelocSize, Addend, Mapping); |
| 616 | } else if (const auto *Mapping = DMO.lookupObjectAddress(Addend)) { |
| 617 | // Do not store the addend. The addend was the address of the |
| 618 | // symbol in the object file, the address in the binary that is |
| 619 | // stored in the debug map doesn't need to be offseted. |
| 620 | ValidRelocs.emplace_back(Offset64, RelocSize, 0, Mapping); |
| 621 | } |
| 622 | } |
| 623 | } |
| 624 | |
| 625 | /// \brief Dispatch the valid relocation finding logic to the |
| 626 | /// appropriate handler depending on the object file format. |
| 627 | bool DwarfLinker::findValidRelocs(const object::SectionRef &Section, |
| 628 | const object::ObjectFile &Obj, |
| 629 | const DebugMapObject &DMO) { |
| 630 | // Dispatch to the right handler depending on the file type. |
| 631 | if (auto *MachOObj = dyn_cast<object::MachOObjectFile>(&Obj)) |
| 632 | findValidRelocsMachO(Section, *MachOObj, DMO); |
| 633 | else |
Frederic Riss | 1b9da42 | 2015-02-13 23:18:29 +0000 | [diff] [blame] | 634 | reportWarning(Twine("unsupported object file type: ") + Obj.getFileName()); |
Frederic Riss | 1036e64 | 2015-02-13 23:18:22 +0000 | [diff] [blame] | 635 | |
| 636 | if (ValidRelocs.empty()) |
| 637 | return false; |
| 638 | |
| 639 | // Sort the relocations by offset. We will walk the DIEs linearly in |
| 640 | // the file, this allows us to just keep an index in the relocation |
| 641 | // array that we advance during our walk, rather than resorting to |
| 642 | // some associative container. See DwarfLinker::NextValidReloc. |
| 643 | std::sort(ValidRelocs.begin(), ValidRelocs.end()); |
| 644 | return true; |
| 645 | } |
| 646 | |
| 647 | /// \brief Look for relocations in the debug_info section that match |
| 648 | /// entries in the debug map. These relocations will drive the Dwarf |
| 649 | /// link by indicating which DIEs refer to symbols present in the |
| 650 | /// linked binary. |
| 651 | /// \returns wether there are any valid relocations in the debug info. |
| 652 | bool DwarfLinker::findValidRelocsInDebugInfo(const object::ObjectFile &Obj, |
| 653 | const DebugMapObject &DMO) { |
| 654 | // Find the debug_info section. |
| 655 | for (const object::SectionRef &Section : Obj.sections()) { |
| 656 | StringRef SectionName; |
| 657 | Section.getName(SectionName); |
| 658 | SectionName = SectionName.substr(SectionName.find_first_not_of("._")); |
| 659 | if (SectionName != "debug_info") |
| 660 | continue; |
| 661 | return findValidRelocs(Section, Obj, DMO); |
| 662 | } |
| 663 | return false; |
| 664 | } |
Frederic Riss | 563cba6 | 2015-01-28 22:15:14 +0000 | [diff] [blame] | 665 | |
Frederic Riss | 84c09a5 | 2015-02-13 23:18:34 +0000 | [diff] [blame] | 666 | /// \brief Checks that there is a relocation against an actual debug |
| 667 | /// map entry between \p StartOffset and \p NextOffset. |
| 668 | /// |
| 669 | /// This function must be called with offsets in strictly ascending |
| 670 | /// order because it never looks back at relocations it already 'went past'. |
| 671 | /// \returns true and sets Info.InDebugMap if it is the case. |
| 672 | bool DwarfLinker::hasValidRelocation(uint32_t StartOffset, uint32_t EndOffset, |
| 673 | CompileUnit::DIEInfo &Info) { |
| 674 | assert(NextValidReloc == 0 || |
| 675 | StartOffset > ValidRelocs[NextValidReloc - 1].Offset); |
| 676 | if (NextValidReloc >= ValidRelocs.size()) |
| 677 | return false; |
| 678 | |
| 679 | uint64_t RelocOffset = ValidRelocs[NextValidReloc].Offset; |
| 680 | |
| 681 | // We might need to skip some relocs that we didn't consider. For |
| 682 | // example the high_pc of a discarded DIE might contain a reloc that |
| 683 | // is in the list because it actually corresponds to the start of a |
| 684 | // function that is in the debug map. |
| 685 | while (RelocOffset < StartOffset && NextValidReloc < ValidRelocs.size() - 1) |
| 686 | RelocOffset = ValidRelocs[++NextValidReloc].Offset; |
| 687 | |
| 688 | if (RelocOffset < StartOffset || RelocOffset >= EndOffset) |
| 689 | return false; |
| 690 | |
| 691 | const auto &ValidReloc = ValidRelocs[NextValidReloc++]; |
Frederic Riss | b981832 | 2015-02-28 00:29:07 +0000 | [diff] [blame] | 692 | if (Options.Verbose) |
Frederic Riss | 84c09a5 | 2015-02-13 23:18:34 +0000 | [diff] [blame] | 693 | outs() << "Found valid debug map entry: " << ValidReloc.Mapping->getKey() |
| 694 | << " " << format("\t%016" PRIx64 " => %016" PRIx64, |
| 695 | ValidReloc.Mapping->getValue().ObjectAddress, |
| 696 | ValidReloc.Mapping->getValue().BinaryAddress); |
| 697 | |
| 698 | Info.Address = |
| 699 | ValidReloc.Mapping->getValue().BinaryAddress + ValidReloc.Addend; |
| 700 | Info.InDebugMap = true; |
| 701 | return true; |
| 702 | } |
| 703 | |
| 704 | /// \brief Get the starting and ending (exclusive) offset for the |
| 705 | /// attribute with index \p Idx descibed by \p Abbrev. \p Offset is |
| 706 | /// supposed to point to the position of the first attribute described |
| 707 | /// by \p Abbrev. |
| 708 | /// \return [StartOffset, EndOffset) as a pair. |
| 709 | static std::pair<uint32_t, uint32_t> |
| 710 | getAttributeOffsets(const DWARFAbbreviationDeclaration *Abbrev, unsigned Idx, |
| 711 | unsigned Offset, const DWARFUnit &Unit) { |
| 712 | DataExtractor Data = Unit.getDebugInfoExtractor(); |
| 713 | |
| 714 | for (unsigned i = 0; i < Idx; ++i) |
| 715 | DWARFFormValue::skipValue(Abbrev->getFormByIndex(i), Data, &Offset, &Unit); |
| 716 | |
| 717 | uint32_t End = Offset; |
| 718 | DWARFFormValue::skipValue(Abbrev->getFormByIndex(Idx), Data, &End, &Unit); |
| 719 | |
| 720 | return std::make_pair(Offset, End); |
| 721 | } |
| 722 | |
| 723 | /// \brief Check if a variable describing DIE should be kept. |
| 724 | /// \returns updated TraversalFlags. |
| 725 | unsigned DwarfLinker::shouldKeepVariableDIE( |
| 726 | const DWARFDebugInfoEntryMinimal &DIE, CompileUnit &Unit, |
| 727 | CompileUnit::DIEInfo &MyInfo, unsigned Flags) { |
| 728 | const auto *Abbrev = DIE.getAbbreviationDeclarationPtr(); |
| 729 | |
| 730 | // Global variables with constant value can always be kept. |
| 731 | if (!(Flags & TF_InFunctionScope) && |
| 732 | Abbrev->findAttributeIndex(dwarf::DW_AT_const_value) != -1U) { |
| 733 | MyInfo.InDebugMap = true; |
| 734 | return Flags | TF_Keep; |
| 735 | } |
| 736 | |
| 737 | uint32_t LocationIdx = Abbrev->findAttributeIndex(dwarf::DW_AT_location); |
| 738 | if (LocationIdx == -1U) |
| 739 | return Flags; |
| 740 | |
| 741 | uint32_t Offset = DIE.getOffset() + getULEB128Size(Abbrev->getCode()); |
| 742 | const DWARFUnit &OrigUnit = Unit.getOrigUnit(); |
| 743 | uint32_t LocationOffset, LocationEndOffset; |
| 744 | std::tie(LocationOffset, LocationEndOffset) = |
| 745 | getAttributeOffsets(Abbrev, LocationIdx, Offset, OrigUnit); |
| 746 | |
| 747 | // See if there is a relocation to a valid debug map entry inside |
| 748 | // this variable's location. The order is important here. We want to |
| 749 | // always check in the variable has a valid relocation, so that the |
| 750 | // DIEInfo is filled. However, we don't want a static variable in a |
| 751 | // function to force us to keep the enclosing function. |
| 752 | if (!hasValidRelocation(LocationOffset, LocationEndOffset, MyInfo) || |
| 753 | (Flags & TF_InFunctionScope)) |
| 754 | return Flags; |
| 755 | |
Frederic Riss | b981832 | 2015-02-28 00:29:07 +0000 | [diff] [blame] | 756 | if (Options.Verbose) |
Frederic Riss | 84c09a5 | 2015-02-13 23:18:34 +0000 | [diff] [blame] | 757 | DIE.dump(outs(), const_cast<DWARFUnit *>(&OrigUnit), 0, 8 /* Indent */); |
| 758 | |
| 759 | return Flags | TF_Keep; |
| 760 | } |
| 761 | |
| 762 | /// \brief Check if a function describing DIE should be kept. |
| 763 | /// \returns updated TraversalFlags. |
| 764 | unsigned DwarfLinker::shouldKeepSubprogramDIE( |
| 765 | const DWARFDebugInfoEntryMinimal &DIE, CompileUnit &Unit, |
| 766 | CompileUnit::DIEInfo &MyInfo, unsigned Flags) { |
| 767 | const auto *Abbrev = DIE.getAbbreviationDeclarationPtr(); |
| 768 | |
| 769 | Flags |= TF_InFunctionScope; |
| 770 | |
| 771 | uint32_t LowPcIdx = Abbrev->findAttributeIndex(dwarf::DW_AT_low_pc); |
| 772 | if (LowPcIdx == -1U) |
| 773 | return Flags; |
| 774 | |
| 775 | uint32_t Offset = DIE.getOffset() + getULEB128Size(Abbrev->getCode()); |
| 776 | const DWARFUnit &OrigUnit = Unit.getOrigUnit(); |
| 777 | uint32_t LowPcOffset, LowPcEndOffset; |
| 778 | std::tie(LowPcOffset, LowPcEndOffset) = |
| 779 | getAttributeOffsets(Abbrev, LowPcIdx, Offset, OrigUnit); |
| 780 | |
| 781 | uint64_t LowPc = |
| 782 | DIE.getAttributeValueAsAddress(&OrigUnit, dwarf::DW_AT_low_pc, -1ULL); |
| 783 | assert(LowPc != -1ULL && "low_pc attribute is not an address."); |
| 784 | if (LowPc == -1ULL || |
| 785 | !hasValidRelocation(LowPcOffset, LowPcEndOffset, MyInfo)) |
| 786 | return Flags; |
| 787 | |
Frederic Riss | b981832 | 2015-02-28 00:29:07 +0000 | [diff] [blame] | 788 | if (Options.Verbose) |
Frederic Riss | 84c09a5 | 2015-02-13 23:18:34 +0000 | [diff] [blame] | 789 | DIE.dump(outs(), const_cast<DWARFUnit *>(&OrigUnit), 0, 8 /* Indent */); |
| 790 | |
| 791 | return Flags | TF_Keep; |
| 792 | } |
| 793 | |
| 794 | /// \brief Check if a DIE should be kept. |
| 795 | /// \returns updated TraversalFlags. |
| 796 | unsigned DwarfLinker::shouldKeepDIE(const DWARFDebugInfoEntryMinimal &DIE, |
| 797 | CompileUnit &Unit, |
| 798 | CompileUnit::DIEInfo &MyInfo, |
| 799 | unsigned Flags) { |
| 800 | switch (DIE.getTag()) { |
| 801 | case dwarf::DW_TAG_constant: |
| 802 | case dwarf::DW_TAG_variable: |
| 803 | return shouldKeepVariableDIE(DIE, Unit, MyInfo, Flags); |
| 804 | case dwarf::DW_TAG_subprogram: |
| 805 | return shouldKeepSubprogramDIE(DIE, Unit, MyInfo, Flags); |
| 806 | case dwarf::DW_TAG_module: |
| 807 | case dwarf::DW_TAG_imported_module: |
| 808 | case dwarf::DW_TAG_imported_declaration: |
| 809 | case dwarf::DW_TAG_imported_unit: |
| 810 | // We always want to keep these. |
| 811 | return Flags | TF_Keep; |
| 812 | } |
| 813 | |
| 814 | return Flags; |
| 815 | } |
| 816 | |
Frederic Riss | 84c09a5 | 2015-02-13 23:18:34 +0000 | [diff] [blame] | 817 | /// \brief Mark the passed DIE as well as all the ones it depends on |
| 818 | /// as kept. |
| 819 | /// |
| 820 | /// This function is called by lookForDIEsToKeep on DIEs that are |
| 821 | /// newly discovered to be needed in the link. It recursively calls |
| 822 | /// back to lookForDIEsToKeep while adding TF_DependencyWalk to the |
| 823 | /// TraversalFlags to inform it that it's not doing the primary DIE |
| 824 | /// tree walk. |
| 825 | void DwarfLinker::keepDIEAndDenpendencies(const DWARFDebugInfoEntryMinimal &DIE, |
| 826 | CompileUnit::DIEInfo &MyInfo, |
| 827 | const DebugMapObject &DMO, |
| 828 | CompileUnit &CU, unsigned Flags) { |
| 829 | const DWARFUnit &Unit = CU.getOrigUnit(); |
| 830 | MyInfo.Keep = true; |
| 831 | |
| 832 | // First mark all the parent chain as kept. |
| 833 | unsigned AncestorIdx = MyInfo.ParentIdx; |
| 834 | while (!CU.getInfo(AncestorIdx).Keep) { |
| 835 | lookForDIEsToKeep(*Unit.getDIEAtIndex(AncestorIdx), DMO, CU, |
| 836 | TF_ParentWalk | TF_Keep | TF_DependencyWalk); |
| 837 | AncestorIdx = CU.getInfo(AncestorIdx).ParentIdx; |
| 838 | } |
| 839 | |
| 840 | // Then we need to mark all the DIEs referenced by this DIE's |
| 841 | // attributes as kept. |
| 842 | DataExtractor Data = Unit.getDebugInfoExtractor(); |
| 843 | const auto *Abbrev = DIE.getAbbreviationDeclarationPtr(); |
| 844 | uint32_t Offset = DIE.getOffset() + getULEB128Size(Abbrev->getCode()); |
| 845 | |
| 846 | // Mark all DIEs referenced through atttributes as kept. |
| 847 | for (const auto &AttrSpec : Abbrev->attributes()) { |
| 848 | DWARFFormValue Val(AttrSpec.Form); |
| 849 | |
| 850 | if (!Val.isFormClass(DWARFFormValue::FC_Reference)) { |
| 851 | DWARFFormValue::skipValue(AttrSpec.Form, Data, &Offset, &Unit); |
| 852 | continue; |
| 853 | } |
| 854 | |
| 855 | Val.extractValue(Data, &Offset, &Unit); |
| 856 | CompileUnit *ReferencedCU; |
| 857 | if (const auto *RefDIE = resolveDIEReference(Val, Unit, DIE, ReferencedCU)) |
| 858 | lookForDIEsToKeep(*RefDIE, DMO, *ReferencedCU, |
| 859 | TF_Keep | TF_DependencyWalk); |
| 860 | } |
| 861 | } |
| 862 | |
| 863 | /// \brief Recursively walk the \p DIE tree and look for DIEs to |
| 864 | /// keep. Store that information in \p CU's DIEInfo. |
| 865 | /// |
| 866 | /// This function is the entry point of the DIE selection |
| 867 | /// algorithm. It is expected to walk the DIE tree in file order and |
| 868 | /// (though the mediation of its helper) call hasValidRelocation() on |
| 869 | /// each DIE that might be a 'root DIE' (See DwarfLinker class |
| 870 | /// comment). |
| 871 | /// While walking the dependencies of root DIEs, this function is |
| 872 | /// also called, but during these dependency walks the file order is |
| 873 | /// not respected. The TF_DependencyWalk flag tells us which kind of |
| 874 | /// traversal we are currently doing. |
| 875 | void DwarfLinker::lookForDIEsToKeep(const DWARFDebugInfoEntryMinimal &DIE, |
| 876 | const DebugMapObject &DMO, CompileUnit &CU, |
| 877 | unsigned Flags) { |
| 878 | unsigned Idx = CU.getOrigUnit().getDIEIndex(&DIE); |
| 879 | CompileUnit::DIEInfo &MyInfo = CU.getInfo(Idx); |
| 880 | bool AlreadyKept = MyInfo.Keep; |
| 881 | |
| 882 | // If the Keep flag is set, we are marking a required DIE's |
| 883 | // dependencies. If our target is already marked as kept, we're all |
| 884 | // set. |
| 885 | if ((Flags & TF_DependencyWalk) && AlreadyKept) |
| 886 | return; |
| 887 | |
| 888 | // We must not call shouldKeepDIE while called from keepDIEAndDenpendencies, |
| 889 | // because it would screw up the relocation finding logic. |
| 890 | if (!(Flags & TF_DependencyWalk)) |
| 891 | Flags = shouldKeepDIE(DIE, CU, MyInfo, Flags); |
| 892 | |
| 893 | // If it is a newly kept DIE mark it as well as all its dependencies as kept. |
| 894 | if (!AlreadyKept && (Flags & TF_Keep)) |
| 895 | keepDIEAndDenpendencies(DIE, MyInfo, DMO, CU, Flags); |
| 896 | |
| 897 | // The TF_ParentWalk flag tells us that we are currently walking up |
| 898 | // the parent chain of a required DIE, and we don't want to mark all |
| 899 | // the children of the parents as kept (consider for example a |
| 900 | // DW_TAG_namespace node in the parent chain). There are however a |
| 901 | // set of DIE types for which we want to ignore that directive and still |
| 902 | // walk their children. |
| 903 | if (dieNeedsChildrenToBeMeaningful(DIE.getTag())) |
| 904 | Flags &= ~TF_ParentWalk; |
| 905 | |
| 906 | if (!DIE.hasChildren() || (Flags & TF_ParentWalk)) |
| 907 | return; |
| 908 | |
| 909 | for (auto *Child = DIE.getFirstChild(); Child && !Child->isNULL(); |
| 910 | Child = Child->getSibling()) |
| 911 | lookForDIEsToKeep(*Child, DMO, CU, Flags); |
| 912 | } |
| 913 | |
Frederic Riss | b8b43d5 | 2015-03-04 22:07:44 +0000 | [diff] [blame] | 914 | /// \brief Assign an abbreviation numer to \p Abbrev. |
| 915 | /// |
| 916 | /// Our DIEs get freed after every DebugMapObject has been processed, |
| 917 | /// thus the FoldingSet we use to unique DIEAbbrevs cannot refer to |
| 918 | /// the instances hold by the DIEs. When we encounter an abbreviation |
| 919 | /// that we don't know, we create a permanent copy of it. |
| 920 | void DwarfLinker::AssignAbbrev(DIEAbbrev &Abbrev) { |
| 921 | // Check the set for priors. |
| 922 | FoldingSetNodeID ID; |
| 923 | Abbrev.Profile(ID); |
| 924 | void *InsertToken; |
| 925 | DIEAbbrev *InSet = AbbreviationsSet.FindNodeOrInsertPos(ID, InsertToken); |
| 926 | |
| 927 | // If it's newly added. |
| 928 | if (InSet) { |
| 929 | // Assign existing abbreviation number. |
| 930 | Abbrev.setNumber(InSet->getNumber()); |
| 931 | } else { |
| 932 | // Add to abbreviation list. |
| 933 | Abbreviations.push_back( |
| 934 | new DIEAbbrev(Abbrev.getTag(), Abbrev.hasChildren())); |
| 935 | for (const auto &Attr : Abbrev.getData()) |
| 936 | Abbreviations.back()->AddAttribute(Attr.getAttribute(), Attr.getForm()); |
| 937 | AbbreviationsSet.InsertNode(Abbreviations.back(), InsertToken); |
| 938 | // Assign the unique abbreviation number. |
| 939 | Abbrev.setNumber(Abbreviations.size()); |
| 940 | Abbreviations.back()->setNumber(Abbreviations.size()); |
| 941 | } |
| 942 | } |
| 943 | |
| 944 | /// \brief Clone a string attribute described by \p AttrSpec and add |
| 945 | /// it to \p Die. |
| 946 | /// \returns the size of the new attribute. |
| 947 | unsigned DwarfLinker::cloneStringAttribute(DIE &Die, AttributeSpec AttrSpec) { |
| 948 | // Switch everything to out of line strings. |
| 949 | // FIXME: Construct the actual string table. |
| 950 | Die.addValue(dwarf::Attribute(AttrSpec.Attr), dwarf::DW_FORM_strp, |
| 951 | new (DIEAlloc) DIEInteger(0)); |
| 952 | return 4; |
| 953 | } |
| 954 | |
| 955 | /// \brief Clone an attribute referencing another DIE and add |
| 956 | /// it to \p Die. |
| 957 | /// \returns the size of the new attribute. |
| 958 | unsigned DwarfLinker::cloneDieReferenceAttribute(DIE &Die, |
| 959 | AttributeSpec AttrSpec, |
| 960 | unsigned AttrSize) { |
| 961 | // FIXME: Handle DIE references. |
| 962 | Die.addValue(dwarf::Attribute(AttrSpec.Attr), dwarf::Form(AttrSpec.Form), |
| 963 | new (DIEAlloc) DIEInteger(0)); |
| 964 | return AttrSize; |
| 965 | } |
| 966 | |
| 967 | /// \brief Clone an attribute of block form (locations, constants) and add |
| 968 | /// it to \p Die. |
| 969 | /// \returns the size of the new attribute. |
| 970 | unsigned DwarfLinker::cloneBlockAttribute(DIE &Die, AttributeSpec AttrSpec, |
| 971 | const DWARFFormValue &Val, |
| 972 | unsigned AttrSize) { |
| 973 | DIE *Attr; |
| 974 | DIEValue *Value; |
| 975 | DIELoc *Loc = nullptr; |
| 976 | DIEBlock *Block = nullptr; |
| 977 | // Just copy the block data over. |
| 978 | if (AttrSpec.Attr == dwarf::DW_FORM_exprloc) { |
| 979 | Loc = new (DIEAlloc) DIELoc(); |
| 980 | DIELocs.push_back(Loc); |
| 981 | } else { |
| 982 | Block = new (DIEAlloc) DIEBlock(); |
| 983 | DIEBlocks.push_back(Block); |
| 984 | } |
| 985 | Attr = Loc ? static_cast<DIE *>(Loc) : static_cast<DIE *>(Block); |
| 986 | Value = Loc ? static_cast<DIEValue *>(Loc) : static_cast<DIEValue *>(Block); |
| 987 | ArrayRef<uint8_t> Bytes = *Val.getAsBlock(); |
| 988 | for (auto Byte : Bytes) |
| 989 | Attr->addValue(static_cast<dwarf::Attribute>(0), dwarf::DW_FORM_data1, |
| 990 | new (DIEAlloc) DIEInteger(Byte)); |
| 991 | // FIXME: If DIEBlock and DIELoc just reuses the Size field of |
| 992 | // the DIE class, this if could be replaced by |
| 993 | // Attr->setSize(Bytes.size()). |
| 994 | if (Streamer) { |
| 995 | if (Loc) |
| 996 | Loc->ComputeSize(&Streamer->getAsmPrinter()); |
| 997 | else |
| 998 | Block->ComputeSize(&Streamer->getAsmPrinter()); |
| 999 | } |
| 1000 | Die.addValue(dwarf::Attribute(AttrSpec.Attr), dwarf::Form(AttrSpec.Form), |
| 1001 | Value); |
| 1002 | return AttrSize; |
| 1003 | } |
| 1004 | |
| 1005 | /// \brief Clone a scalar attribute and add it to \p Die. |
| 1006 | /// \returns the size of the new attribute. |
| 1007 | unsigned DwarfLinker::cloneScalarAttribute( |
| 1008 | DIE &Die, const DWARFDebugInfoEntryMinimal &InputDIE, const DWARFUnit &U, |
| 1009 | AttributeSpec AttrSpec, const DWARFFormValue &Val, unsigned AttrSize) { |
| 1010 | uint64_t Value; |
| 1011 | if (AttrSpec.Form == dwarf::DW_FORM_sec_offset) |
| 1012 | Value = *Val.getAsSectionOffset(); |
| 1013 | else if (AttrSpec.Form == dwarf::DW_FORM_sdata) |
| 1014 | Value = *Val.getAsSignedConstant(); |
| 1015 | else if (AttrSpec.Form == dwarf::DW_FORM_addr) |
| 1016 | Value = *Val.getAsAddress(&U); |
| 1017 | else if (auto OptionalValue = Val.getAsUnsignedConstant()) |
| 1018 | Value = *OptionalValue; |
| 1019 | else { |
| 1020 | reportWarning("Unsupported scalar attribute form. Dropping attribute.", &U, |
| 1021 | &InputDIE); |
| 1022 | return 0; |
| 1023 | } |
| 1024 | Die.addValue(dwarf::Attribute(AttrSpec.Attr), dwarf::Form(AttrSpec.Form), |
| 1025 | new (DIEAlloc) DIEInteger(Value)); |
| 1026 | return AttrSize; |
| 1027 | } |
| 1028 | |
| 1029 | /// \brief Clone \p InputDIE's attribute described by \p AttrSpec with |
| 1030 | /// value \p Val, and add it to \p Die. |
| 1031 | /// \returns the size of the cloned attribute. |
| 1032 | unsigned DwarfLinker::cloneAttribute(DIE &Die, |
| 1033 | const DWARFDebugInfoEntryMinimal &InputDIE, |
| 1034 | CompileUnit &Unit, |
| 1035 | const DWARFFormValue &Val, |
| 1036 | const AttributeSpec AttrSpec, |
| 1037 | unsigned AttrSize) { |
| 1038 | const DWARFUnit &U = Unit.getOrigUnit(); |
| 1039 | |
| 1040 | switch (AttrSpec.Form) { |
| 1041 | case dwarf::DW_FORM_strp: |
| 1042 | case dwarf::DW_FORM_string: |
| 1043 | return cloneStringAttribute(Die, AttrSpec); |
| 1044 | case dwarf::DW_FORM_ref_addr: |
| 1045 | case dwarf::DW_FORM_ref1: |
| 1046 | case dwarf::DW_FORM_ref2: |
| 1047 | case dwarf::DW_FORM_ref4: |
| 1048 | case dwarf::DW_FORM_ref8: |
| 1049 | return cloneDieReferenceAttribute(Die, AttrSpec, AttrSize); |
| 1050 | case dwarf::DW_FORM_block: |
| 1051 | case dwarf::DW_FORM_block1: |
| 1052 | case dwarf::DW_FORM_block2: |
| 1053 | case dwarf::DW_FORM_block4: |
| 1054 | case dwarf::DW_FORM_exprloc: |
| 1055 | return cloneBlockAttribute(Die, AttrSpec, Val, AttrSize); |
| 1056 | case dwarf::DW_FORM_addr: |
| 1057 | case dwarf::DW_FORM_data1: |
| 1058 | case dwarf::DW_FORM_data2: |
| 1059 | case dwarf::DW_FORM_data4: |
| 1060 | case dwarf::DW_FORM_data8: |
| 1061 | case dwarf::DW_FORM_udata: |
| 1062 | case dwarf::DW_FORM_sdata: |
| 1063 | case dwarf::DW_FORM_sec_offset: |
| 1064 | case dwarf::DW_FORM_flag: |
| 1065 | case dwarf::DW_FORM_flag_present: |
| 1066 | return cloneScalarAttribute(Die, InputDIE, U, AttrSpec, Val, AttrSize); |
| 1067 | default: |
| 1068 | reportWarning("Unsupported attribute form in cloneAttribute. Dropping.", &U, |
| 1069 | &InputDIE); |
| 1070 | } |
| 1071 | |
| 1072 | return 0; |
| 1073 | } |
| 1074 | |
| 1075 | /// \brief Recursively clone \p InputDIE's subtrees that have been |
| 1076 | /// selected to appear in the linked output. |
| 1077 | /// |
| 1078 | /// \param OutOffset is the Offset where the newly created DIE will |
| 1079 | /// lie in the linked compile unit. |
| 1080 | /// |
| 1081 | /// \returns the cloned DIE object or null if nothing was selected. |
| 1082 | DIE *DwarfLinker::cloneDIE(const DWARFDebugInfoEntryMinimal &InputDIE, |
| 1083 | CompileUnit &Unit, uint32_t OutOffset) { |
| 1084 | DWARFUnit &U = Unit.getOrigUnit(); |
| 1085 | unsigned Idx = U.getDIEIndex(&InputDIE); |
| 1086 | |
| 1087 | // Should the DIE appear in the output? |
| 1088 | if (!Unit.getInfo(Idx).Keep) |
| 1089 | return nullptr; |
| 1090 | |
| 1091 | uint32_t Offset = InputDIE.getOffset(); |
| 1092 | |
| 1093 | DIE *Die = new DIE(static_cast<dwarf::Tag>(InputDIE.getTag())); |
| 1094 | Die->setOffset(OutOffset); |
| 1095 | |
| 1096 | // Extract and clone every attribute. |
| 1097 | DataExtractor Data = U.getDebugInfoExtractor(); |
| 1098 | const auto *Abbrev = InputDIE.getAbbreviationDeclarationPtr(); |
| 1099 | Offset += getULEB128Size(Abbrev->getCode()); |
| 1100 | |
| 1101 | for (const auto &AttrSpec : Abbrev->attributes()) { |
| 1102 | DWARFFormValue Val(AttrSpec.Form); |
| 1103 | uint32_t AttrSize = Offset; |
| 1104 | Val.extractValue(Data, &Offset, &U); |
| 1105 | AttrSize = Offset - AttrSize; |
| 1106 | |
| 1107 | OutOffset += cloneAttribute(*Die, InputDIE, Unit, Val, AttrSpec, AttrSize); |
| 1108 | } |
| 1109 | |
| 1110 | DIEAbbrev &NewAbbrev = Die->getAbbrev(); |
| 1111 | // If a scope DIE is kept, we must have kept at least one child. If |
| 1112 | // it's not the case, we'll just be emitting one wasteful end of |
| 1113 | // children marker, but things won't break. |
| 1114 | if (InputDIE.hasChildren()) |
| 1115 | NewAbbrev.setChildrenFlag(dwarf::DW_CHILDREN_yes); |
| 1116 | // Assign a permanent abbrev number |
| 1117 | AssignAbbrev(Die->getAbbrev()); |
| 1118 | |
| 1119 | // Add the size of the abbreviation number to the output offset. |
| 1120 | OutOffset += getULEB128Size(Die->getAbbrevNumber()); |
| 1121 | |
| 1122 | if (!Abbrev->hasChildren()) { |
| 1123 | // Update our size. |
| 1124 | Die->setSize(OutOffset - Die->getOffset()); |
| 1125 | return Die; |
| 1126 | } |
| 1127 | |
| 1128 | // Recursively clone children. |
| 1129 | for (auto *Child = InputDIE.getFirstChild(); Child && !Child->isNULL(); |
| 1130 | Child = Child->getSibling()) { |
| 1131 | if (DIE *Clone = cloneDIE(*Child, Unit, OutOffset)) { |
| 1132 | Die->addChild(std::unique_ptr<DIE>(Clone)); |
| 1133 | OutOffset = Clone->getOffset() + Clone->getSize(); |
| 1134 | } |
| 1135 | } |
| 1136 | |
| 1137 | // Account for the end of children marker. |
| 1138 | OutOffset += sizeof(int8_t); |
| 1139 | // Update our size. |
| 1140 | Die->setSize(OutOffset - Die->getOffset()); |
| 1141 | return Die; |
| 1142 | } |
| 1143 | |
Frederic Riss | d345518 | 2015-01-28 18:27:01 +0000 | [diff] [blame] | 1144 | bool DwarfLinker::link(const DebugMap &Map) { |
| 1145 | |
| 1146 | if (Map.begin() == Map.end()) { |
| 1147 | errs() << "Empty debug map.\n"; |
| 1148 | return false; |
| 1149 | } |
| 1150 | |
Frederic Riss | c99ea20 | 2015-02-28 00:29:11 +0000 | [diff] [blame] | 1151 | if (!createStreamer(Map.getTriple(), OutputFilename)) |
| 1152 | return false; |
| 1153 | |
Frederic Riss | b8b43d5 | 2015-03-04 22:07:44 +0000 | [diff] [blame] | 1154 | // Size of the DIEs (and headers) generated for the linked output. |
| 1155 | uint64_t OutputDebugInfoSize = 0; |
| 1156 | |
Frederic Riss | d345518 | 2015-01-28 18:27:01 +0000 | [diff] [blame] | 1157 | for (const auto &Obj : Map.objects()) { |
Frederic Riss | 1b9da42 | 2015-02-13 23:18:29 +0000 | [diff] [blame] | 1158 | CurrentDebugObject = Obj.get(); |
| 1159 | |
Frederic Riss | b981832 | 2015-02-28 00:29:07 +0000 | [diff] [blame] | 1160 | if (Options.Verbose) |
Frederic Riss | d345518 | 2015-01-28 18:27:01 +0000 | [diff] [blame] | 1161 | outs() << "DEBUG MAP OBJECT: " << Obj->getObjectFilename() << "\n"; |
| 1162 | auto ErrOrObj = BinHolder.GetObjectFile(Obj->getObjectFilename()); |
| 1163 | if (std::error_code EC = ErrOrObj.getError()) { |
Frederic Riss | 1b9da42 | 2015-02-13 23:18:29 +0000 | [diff] [blame] | 1164 | reportWarning(Twine(Obj->getObjectFilename()) + ": " + EC.message()); |
Frederic Riss | d345518 | 2015-01-28 18:27:01 +0000 | [diff] [blame] | 1165 | continue; |
| 1166 | } |
| 1167 | |
Frederic Riss | 1036e64 | 2015-02-13 23:18:22 +0000 | [diff] [blame] | 1168 | // Look for relocations that correspond to debug map entries. |
| 1169 | if (!findValidRelocsInDebugInfo(*ErrOrObj, *Obj)) { |
Frederic Riss | b981832 | 2015-02-28 00:29:07 +0000 | [diff] [blame] | 1170 | if (Options.Verbose) |
Frederic Riss | 1036e64 | 2015-02-13 23:18:22 +0000 | [diff] [blame] | 1171 | outs() << "No valid relocations found. Skipping.\n"; |
| 1172 | continue; |
| 1173 | } |
| 1174 | |
Frederic Riss | 563cba6 | 2015-01-28 22:15:14 +0000 | [diff] [blame] | 1175 | // Setup access to the debug info. |
Frederic Riss | d345518 | 2015-01-28 18:27:01 +0000 | [diff] [blame] | 1176 | DWARFContextInMemory DwarfContext(*ErrOrObj); |
Frederic Riss | 563cba6 | 2015-01-28 22:15:14 +0000 | [diff] [blame] | 1177 | startDebugObject(DwarfContext); |
Frederic Riss | d345518 | 2015-01-28 18:27:01 +0000 | [diff] [blame] | 1178 | |
Frederic Riss | 563cba6 | 2015-01-28 22:15:14 +0000 | [diff] [blame] | 1179 | // In a first phase, just read in the debug info and store the DIE |
| 1180 | // parent links that we will use during the next phase. |
Frederic Riss | d345518 | 2015-01-28 18:27:01 +0000 | [diff] [blame] | 1181 | for (const auto &CU : DwarfContext.compile_units()) { |
| 1182 | auto *CUDie = CU->getCompileUnitDIE(false); |
Frederic Riss | b981832 | 2015-02-28 00:29:07 +0000 | [diff] [blame] | 1183 | if (Options.Verbose) { |
Frederic Riss | d345518 | 2015-01-28 18:27:01 +0000 | [diff] [blame] | 1184 | outs() << "Input compilation unit:"; |
| 1185 | CUDie->dump(outs(), CU.get(), 0); |
| 1186 | } |
Frederic Riss | 563cba6 | 2015-01-28 22:15:14 +0000 | [diff] [blame] | 1187 | Units.emplace_back(*CU); |
Frederic Riss | 9aa725b | 2015-02-13 23:18:31 +0000 | [diff] [blame] | 1188 | gatherDIEParents(CUDie, 0, Units.back()); |
Frederic Riss | d345518 | 2015-01-28 18:27:01 +0000 | [diff] [blame] | 1189 | } |
Frederic Riss | 563cba6 | 2015-01-28 22:15:14 +0000 | [diff] [blame] | 1190 | |
Frederic Riss | 84c09a5 | 2015-02-13 23:18:34 +0000 | [diff] [blame] | 1191 | // Then mark all the DIEs that need to be present in the linked |
| 1192 | // output and collect some information about them. Note that this |
| 1193 | // loop can not be merged with the previous one becaue cross-cu |
| 1194 | // references require the ParentIdx to be setup for every CU in |
| 1195 | // the object file before calling this. |
| 1196 | for (auto &CurrentUnit : Units) |
| 1197 | lookForDIEsToKeep(*CurrentUnit.getOrigUnit().getCompileUnitDIE(), *Obj, |
| 1198 | CurrentUnit, 0); |
| 1199 | |
Frederic Riss | b8b43d5 | 2015-03-04 22:07:44 +0000 | [diff] [blame] | 1200 | // Construct the output DIE tree by cloning the DIEs we chose to |
| 1201 | // keep above. If there are no valid relocs, then there's nothing |
| 1202 | // to clone/emit. |
| 1203 | if (!ValidRelocs.empty()) |
| 1204 | for (auto &CurrentUnit : Units) { |
| 1205 | const auto *InputDIE = CurrentUnit.getOrigUnit().getCompileUnitDIE(); |
| 1206 | DIE *OutputDIE = |
| 1207 | cloneDIE(*InputDIE, CurrentUnit, 11 /* Unit Header size */); |
| 1208 | CurrentUnit.setOutputUnitDIE(OutputDIE); |
| 1209 | OutputDebugInfoSize = CurrentUnit.computeOffsets(OutputDebugInfoSize); |
| 1210 | } |
| 1211 | |
| 1212 | // Emit all the compile unit's debug information. |
| 1213 | if (!ValidRelocs.empty() && !Options.NoOutput) |
| 1214 | for (auto &CurrentUnit : Units) { |
| 1215 | Streamer->emitCompileUnitHeader(CurrentUnit); |
| 1216 | if (!CurrentUnit.getOutputUnitDIE()) |
| 1217 | continue; |
| 1218 | Streamer->emitDIE(*CurrentUnit.getOutputUnitDIE()); |
| 1219 | } |
| 1220 | |
Frederic Riss | 563cba6 | 2015-01-28 22:15:14 +0000 | [diff] [blame] | 1221 | // Clean-up before starting working on the next object. |
| 1222 | endDebugObject(); |
Frederic Riss | d345518 | 2015-01-28 18:27:01 +0000 | [diff] [blame] | 1223 | } |
| 1224 | |
Frederic Riss | b8b43d5 | 2015-03-04 22:07:44 +0000 | [diff] [blame] | 1225 | // Emit everything that's global. |
| 1226 | if (!Options.NoOutput) |
| 1227 | Streamer->emitAbbrevs(Abbreviations); |
| 1228 | |
Frederic Riss | c99ea20 | 2015-02-28 00:29:11 +0000 | [diff] [blame] | 1229 | return Options.NoOutput ? true : Streamer->finish(); |
Frederic Riss | 231f714 | 2014-12-12 17:31:24 +0000 | [diff] [blame] | 1230 | } |
| 1231 | } |
Frederic Riss | d345518 | 2015-01-28 18:27:01 +0000 | [diff] [blame] | 1232 | |
Frederic Riss | b981832 | 2015-02-28 00:29:07 +0000 | [diff] [blame] | 1233 | bool linkDwarf(StringRef OutputFilename, const DebugMap &DM, |
| 1234 | const LinkOptions &Options) { |
| 1235 | DwarfLinker Linker(OutputFilename, Options); |
Frederic Riss | d345518 | 2015-01-28 18:27:01 +0000 | [diff] [blame] | 1236 | return Linker.link(DM); |
| 1237 | } |
| 1238 | } |
Frederic Riss | 231f714 | 2014-12-12 17:31:24 +0000 | [diff] [blame] | 1239 | } |