Eugene Zelenko | fb69e66 | 2017-06-06 22:22:41 +0000 | [diff] [blame] | 1 | //===- llvm/lib/CodeGen/AsmPrinter/CodeViewDebug.cpp ----------------------===// |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
Reid Kleckner | 70f5bc9 | 2016-01-14 19:25:04 +0000 | [diff] [blame] | 10 | // This file contains support for writing Microsoft CodeView debug info. |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Reid Kleckner | 70f5bc9 | 2016-01-14 19:25:04 +0000 | [diff] [blame] | 14 | #include "CodeViewDebug.h" |
Adrian Prantl | 032d238 | 2017-08-01 21:45:24 +0000 | [diff] [blame] | 15 | #include "DwarfExpression.h" |
Eugene Zelenko | fb69e66 | 2017-06-06 22:22:41 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/APSInt.h" |
| 17 | #include "llvm/ADT/ArrayRef.h" |
| 18 | #include "llvm/ADT/DenseMap.h" |
| 19 | #include "llvm/ADT/DenseSet.h" |
| 20 | #include "llvm/ADT/MapVector.h" |
| 21 | #include "llvm/ADT/None.h" |
| 22 | #include "llvm/ADT/Optional.h" |
David Blaikie | b3bde2e | 2017-11-17 01:07:10 +0000 | [diff] [blame] | 23 | #include "llvm/ADT/STLExtras.h" |
Eugene Zelenko | fb69e66 | 2017-06-06 22:22:41 +0000 | [diff] [blame] | 24 | #include "llvm/ADT/SmallString.h" |
| 25 | #include "llvm/ADT/SmallVector.h" |
Eugene Zelenko | fb69e66 | 2017-06-06 22:22:41 +0000 | [diff] [blame] | 26 | #include "llvm/ADT/StringRef.h" |
Reid Kleckner | 156a723 | 2016-06-22 18:31:14 +0000 | [diff] [blame] | 27 | #include "llvm/ADT/TinyPtrVector.h" |
Eugene Zelenko | fb69e66 | 2017-06-06 22:22:41 +0000 | [diff] [blame] | 28 | #include "llvm/ADT/Triple.h" |
| 29 | #include "llvm/ADT/Twine.h" |
Zachary Turner | 264b5d9 | 2017-06-07 03:48:56 +0000 | [diff] [blame] | 30 | #include "llvm/BinaryFormat/COFF.h" |
| 31 | #include "llvm/BinaryFormat/Dwarf.h" |
Eugene Zelenko | fb69e66 | 2017-06-06 22:22:41 +0000 | [diff] [blame] | 32 | #include "llvm/CodeGen/AsmPrinter.h" |
| 33 | #include "llvm/CodeGen/LexicalScopes.h" |
| 34 | #include "llvm/CodeGen/MachineFunction.h" |
| 35 | #include "llvm/CodeGen/MachineInstr.h" |
| 36 | #include "llvm/CodeGen/MachineModuleInfo.h" |
| 37 | #include "llvm/CodeGen/MachineOperand.h" |
David Blaikie | b3bde2e | 2017-11-17 01:07:10 +0000 | [diff] [blame] | 38 | #include "llvm/CodeGen/TargetFrameLowering.h" |
David Blaikie | b3bde2e | 2017-11-17 01:07:10 +0000 | [diff] [blame] | 39 | #include "llvm/CodeGen/TargetRegisterInfo.h" |
| 40 | #include "llvm/CodeGen/TargetSubtargetInfo.h" |
Eugene Zelenko | fb69e66 | 2017-06-06 22:22:41 +0000 | [diff] [blame] | 41 | #include "llvm/Config/llvm-config.h" |
Reid Kleckner | c92e946 | 2016-07-01 18:05:56 +0000 | [diff] [blame] | 42 | #include "llvm/DebugInfo/CodeView/CVTypeVisitor.h" |
Reid Kleckner | 6b3faef | 2016-01-13 23:44:57 +0000 | [diff] [blame] | 43 | #include "llvm/DebugInfo/CodeView/CodeView.h" |
Zachary Turner | 6900de1 | 2017-11-28 18:33:17 +0000 | [diff] [blame] | 44 | #include "llvm/DebugInfo/CodeView/ContinuationRecordBuilder.h" |
Zachary Turner | 8c099fe | 2017-05-30 16:36:15 +0000 | [diff] [blame] | 45 | #include "llvm/DebugInfo/CodeView/DebugInlineeLinesSubsection.h" |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 46 | #include "llvm/DebugInfo/CodeView/Line.h" |
Reid Kleckner | 6b3faef | 2016-01-13 23:44:57 +0000 | [diff] [blame] | 47 | #include "llvm/DebugInfo/CodeView/SymbolRecord.h" |
Zachary Turner | 629cb7d | 2017-01-11 23:24:22 +0000 | [diff] [blame] | 48 | #include "llvm/DebugInfo/CodeView/TypeDumpVisitor.h" |
Reid Kleckner | f3b9ba4 | 2016-01-29 18:16:43 +0000 | [diff] [blame] | 49 | #include "llvm/DebugInfo/CodeView/TypeIndex.h" |
| 50 | #include "llvm/DebugInfo/CodeView/TypeRecord.h" |
Zachary Turner | 526f4f2 | 2017-05-19 19:26:58 +0000 | [diff] [blame] | 51 | #include "llvm/DebugInfo/CodeView/TypeTableCollection.h" |
David Majnemer | 9319cbc | 2016-06-30 03:00:20 +0000 | [diff] [blame] | 52 | #include "llvm/IR/Constants.h" |
Eugene Zelenko | fb69e66 | 2017-06-06 22:22:41 +0000 | [diff] [blame] | 53 | #include "llvm/IR/DataLayout.h" |
| 54 | #include "llvm/IR/DebugInfoMetadata.h" |
| 55 | #include "llvm/IR/DebugLoc.h" |
| 56 | #include "llvm/IR/Function.h" |
| 57 | #include "llvm/IR/GlobalValue.h" |
| 58 | #include "llvm/IR/GlobalVariable.h" |
| 59 | #include "llvm/IR/Metadata.h" |
| 60 | #include "llvm/IR/Module.h" |
Reid Kleckner | 46cb48c | 2016-07-27 16:03:57 +0000 | [diff] [blame] | 61 | #include "llvm/MC/MCAsmInfo.h" |
Eugene Zelenko | fb69e66 | 2017-06-06 22:22:41 +0000 | [diff] [blame] | 62 | #include "llvm/MC/MCContext.h" |
Reid Kleckner | 5d122f8 | 2016-05-25 23:16:12 +0000 | [diff] [blame] | 63 | #include "llvm/MC/MCSectionCOFF.h" |
Eugene Zelenko | fb69e66 | 2017-06-06 22:22:41 +0000 | [diff] [blame] | 64 | #include "llvm/MC/MCStreamer.h" |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 65 | #include "llvm/MC/MCSymbol.h" |
Zachary Turner | 264b5d9 | 2017-06-07 03:48:56 +0000 | [diff] [blame] | 66 | #include "llvm/Support/BinaryByteStream.h" |
| 67 | #include "llvm/Support/BinaryStreamReader.h" |
Eugene Zelenko | fb69e66 | 2017-06-06 22:22:41 +0000 | [diff] [blame] | 68 | #include "llvm/Support/Casting.h" |
Zachary Turner | 048f8f9 | 2017-12-13 22:33:58 +0000 | [diff] [blame] | 69 | #include "llvm/Support/CommandLine.h" |
Eugene Zelenko | fb69e66 | 2017-06-06 22:22:41 +0000 | [diff] [blame] | 70 | #include "llvm/Support/Compiler.h" |
Eugene Zelenko | fb69e66 | 2017-06-06 22:22:41 +0000 | [diff] [blame] | 71 | #include "llvm/Support/Endian.h" |
| 72 | #include "llvm/Support/Error.h" |
| 73 | #include "llvm/Support/ErrorHandling.h" |
Zachary Turner | 048f8f9 | 2017-12-13 22:33:58 +0000 | [diff] [blame] | 74 | #include "llvm/Support/FormatVariadic.h" |
Eugene Zelenko | fb69e66 | 2017-06-06 22:22:41 +0000 | [diff] [blame] | 75 | #include "llvm/Support/SMLoc.h" |
David Blaikie | b3bde2e | 2017-11-17 01:07:10 +0000 | [diff] [blame] | 76 | #include "llvm/Support/ScopedPrinter.h" |
David Blaikie | 6054e65 | 2018-03-23 23:58:19 +0000 | [diff] [blame] | 77 | #include "llvm/Target/TargetLoweringObjectFile.h" |
Eugene Zelenko | fb69e66 | 2017-06-06 22:22:41 +0000 | [diff] [blame] | 78 | #include "llvm/Target/TargetMachine.h" |
Eugene Zelenko | fb69e66 | 2017-06-06 22:22:41 +0000 | [diff] [blame] | 79 | #include <algorithm> |
| 80 | #include <cassert> |
| 81 | #include <cctype> |
| 82 | #include <cstddef> |
| 83 | #include <cstdint> |
| 84 | #include <iterator> |
| 85 | #include <limits> |
| 86 | #include <string> |
| 87 | #include <utility> |
| 88 | #include <vector> |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 89 | |
Reid Kleckner | f9c275f | 2016-02-10 20:55:49 +0000 | [diff] [blame] | 90 | using namespace llvm; |
Reid Kleckner | 6b3faef | 2016-01-13 23:44:57 +0000 | [diff] [blame] | 91 | using namespace llvm::codeview; |
| 92 | |
Zachary Turner | 048f8f9 | 2017-12-13 22:33:58 +0000 | [diff] [blame] | 93 | static cl::opt<bool> EmitDebugGlobalHashes("emit-codeview-ghash-section", |
| 94 | cl::ReallyHidden, cl::init(false)); |
| 95 | |
Reid Kleckner | f9c275f | 2016-02-10 20:55:49 +0000 | [diff] [blame] | 96 | CodeViewDebug::CodeViewDebug(AsmPrinter *AP) |
Eugene Zelenko | fb69e66 | 2017-06-06 22:22:41 +0000 | [diff] [blame] | 97 | : DebugHandlerBase(AP), OS(*Asm->OutStreamer), TypeTable(Allocator) { |
Reid Kleckner | f9c275f | 2016-02-10 20:55:49 +0000 | [diff] [blame] | 98 | // If module doesn't have named metadata anchors or COFF debug section |
| 99 | // is not available, skip any debug info related stuff. |
| 100 | if (!MMI->getModule()->getNamedMetadata("llvm.dbg.cu") || |
| 101 | !AP->getObjFileLowering().getCOFFDebugSymbolsSection()) { |
| 102 | Asm = nullptr; |
| 103 | return; |
| 104 | } |
| 105 | |
| 106 | // Tell MMI that we have debug info. |
| 107 | MMI->setDebugInfoAvailability(true); |
| 108 | } |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 109 | |
Reid Kleckner | 9533af4 | 2016-01-16 00:09:09 +0000 | [diff] [blame] | 110 | StringRef CodeViewDebug::getFullFilepath(const DIFile *File) { |
| 111 | std::string &Filepath = FileToFilepathMap[File]; |
Reid Kleckner | 1f11b4e | 2015-12-02 22:34:30 +0000 | [diff] [blame] | 112 | if (!Filepath.empty()) |
| 113 | return Filepath; |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 114 | |
Reid Kleckner | 9533af4 | 2016-01-16 00:09:09 +0000 | [diff] [blame] | 115 | StringRef Dir = File->getDirectory(), Filename = File->getFilename(); |
| 116 | |
Peter Collingbourne | cb8a666 | 2018-04-11 18:24:03 +0000 | [diff] [blame] | 117 | // If this is a Unix-style path, just use it as is. Don't try to canonicalize |
| 118 | // it textually because one of the path components could be a symlink. |
| 119 | if (!Dir.empty() && Dir[0] == '/') { |
| 120 | Filepath = Dir; |
| 121 | if (Dir.back() != '/') |
| 122 | Filepath += '/'; |
| 123 | Filepath += Filename; |
| 124 | return Filepath; |
| 125 | } |
| 126 | |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 127 | // Clang emits directory and relative filename info into the IR, but CodeView |
| 128 | // operates on full paths. We could change Clang to emit full paths too, but |
| 129 | // that would increase the IR size and probably not needed for other users. |
| 130 | // For now, just concatenate and canonicalize the path here. |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 131 | if (Filename.find(':') == 1) |
| 132 | Filepath = Filename; |
| 133 | else |
Yaron Keren | 75e0c4b | 2015-03-27 17:51:30 +0000 | [diff] [blame] | 134 | Filepath = (Dir + "\\" + Filename).str(); |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 135 | |
| 136 | // Canonicalize the path. We have to do it textually because we may no longer |
| 137 | // have access the file in the filesystem. |
| 138 | // First, replace all slashes with backslashes. |
| 139 | std::replace(Filepath.begin(), Filepath.end(), '/', '\\'); |
| 140 | |
| 141 | // Remove all "\.\" with "\". |
| 142 | size_t Cursor = 0; |
| 143 | while ((Cursor = Filepath.find("\\.\\", Cursor)) != std::string::npos) |
| 144 | Filepath.erase(Cursor, 2); |
| 145 | |
| 146 | // Replace all "\XXX\..\" with "\". Don't try too hard though as the original |
| 147 | // path should be well-formatted, e.g. start with a drive letter, etc. |
| 148 | Cursor = 0; |
| 149 | while ((Cursor = Filepath.find("\\..\\", Cursor)) != std::string::npos) { |
| 150 | // Something's wrong if the path starts with "\..\", abort. |
| 151 | if (Cursor == 0) |
| 152 | break; |
| 153 | |
| 154 | size_t PrevSlash = Filepath.rfind('\\', Cursor - 1); |
| 155 | if (PrevSlash == std::string::npos) |
| 156 | // Something's wrong, abort. |
| 157 | break; |
| 158 | |
| 159 | Filepath.erase(PrevSlash, Cursor + 3 - PrevSlash); |
| 160 | // The next ".." might be following the one we've just erased. |
| 161 | Cursor = PrevSlash; |
| 162 | } |
| 163 | |
| 164 | // Remove all duplicate backslashes. |
| 165 | Cursor = 0; |
| 166 | while ((Cursor = Filepath.find("\\\\", Cursor)) != std::string::npos) |
| 167 | Filepath.erase(Cursor, 1); |
| 168 | |
Reid Kleckner | 1f11b4e | 2015-12-02 22:34:30 +0000 | [diff] [blame] | 169 | return Filepath; |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 170 | } |
| 171 | |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 172 | unsigned CodeViewDebug::maybeRecordFile(const DIFile *F) { |
Reid Kleckner | bc6f52d | 2017-10-31 21:52:15 +0000 | [diff] [blame] | 173 | StringRef FullPath = getFullFilepath(F); |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 174 | unsigned NextId = FileIdMap.size() + 1; |
Reid Kleckner | bc6f52d | 2017-10-31 21:52:15 +0000 | [diff] [blame] | 175 | auto Insertion = FileIdMap.insert(std::make_pair(FullPath, NextId)); |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 176 | if (Insertion.second) { |
| 177 | // We have to compute the full filepath and emit a .cv_file directive. |
Scott Linder | 7160384 | 2018-02-12 19:45:54 +0000 | [diff] [blame] | 178 | ArrayRef<uint8_t> ChecksumAsBytes; |
| 179 | FileChecksumKind CSKind = FileChecksumKind::None; |
| 180 | if (F->getChecksum()) { |
| 181 | std::string Checksum = fromHex(F->getChecksum()->Value); |
| 182 | void *CKMem = OS.getContext().allocate(Checksum.size(), 1); |
| 183 | memcpy(CKMem, Checksum.data(), Checksum.size()); |
| 184 | ChecksumAsBytes = ArrayRef<uint8_t>( |
| 185 | reinterpret_cast<const uint8_t *>(CKMem), Checksum.size()); |
| 186 | switch (F->getChecksum()->Kind) { |
| 187 | case DIFile::CSK_MD5: CSKind = FileChecksumKind::MD5; break; |
| 188 | case DIFile::CSK_SHA1: CSKind = FileChecksumKind::SHA1; break; |
| 189 | } |
| 190 | } |
Reid Kleckner | 26fa1bf | 2017-09-19 18:14:45 +0000 | [diff] [blame] | 191 | bool Success = OS.EmitCVFileDirective(NextId, FullPath, ChecksumAsBytes, |
Scott Linder | 7160384 | 2018-02-12 19:45:54 +0000 | [diff] [blame] | 192 | static_cast<unsigned>(CSKind)); |
Reid Kleckner | a5b1eef | 2016-08-26 17:58:37 +0000 | [diff] [blame] | 193 | (void)Success; |
| 194 | assert(Success && ".cv_file directive failed"); |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 195 | } |
| 196 | return Insertion.first->second; |
| 197 | } |
| 198 | |
Reid Kleckner | 876330d | 2016-02-12 21:48:30 +0000 | [diff] [blame] | 199 | CodeViewDebug::InlineSite & |
| 200 | CodeViewDebug::getInlineSite(const DILocation *InlinedAt, |
| 201 | const DISubprogram *Inlinee) { |
Reid Kleckner | fbd7787 | 2016-03-18 18:54:32 +0000 | [diff] [blame] | 202 | auto SiteInsertion = CurFn->InlineSites.insert({InlinedAt, InlineSite()}); |
| 203 | InlineSite *Site = &SiteInsertion.first->second; |
| 204 | if (SiteInsertion.second) { |
Reid Kleckner | a9f4cc9 | 2016-09-07 16:15:31 +0000 | [diff] [blame] | 205 | unsigned ParentFuncId = CurFn->FuncId; |
| 206 | if (const DILocation *OuterIA = InlinedAt->getInlinedAt()) |
| 207 | ParentFuncId = |
| 208 | getInlineSite(OuterIA, InlinedAt->getScope()->getSubprogram()) |
| 209 | .SiteFuncId; |
| 210 | |
Reid Kleckner | f9c275f | 2016-02-10 20:55:49 +0000 | [diff] [blame] | 211 | Site->SiteFuncId = NextFuncId++; |
Reid Kleckner | a9f4cc9 | 2016-09-07 16:15:31 +0000 | [diff] [blame] | 212 | OS.EmitCVInlineSiteIdDirective( |
| 213 | Site->SiteFuncId, ParentFuncId, maybeRecordFile(InlinedAt->getFile()), |
| 214 | InlinedAt->getLine(), InlinedAt->getColumn(), SMLoc()); |
Reid Kleckner | 876330d | 2016-02-12 21:48:30 +0000 | [diff] [blame] | 215 | Site->Inlinee = Inlinee; |
Reid Kleckner | 2280f93 | 2016-05-23 20:23:46 +0000 | [diff] [blame] | 216 | InlinedSubprograms.insert(Inlinee); |
David Majnemer | 75c3ebf | 2016-06-02 17:13:53 +0000 | [diff] [blame] | 217 | getFuncIdForSubprogram(Inlinee); |
Reid Kleckner | f3b9ba4 | 2016-01-29 18:16:43 +0000 | [diff] [blame] | 218 | } |
Reid Kleckner | f9c275f | 2016-02-10 20:55:49 +0000 | [diff] [blame] | 219 | return *Site; |
Reid Kleckner | f3b9ba4 | 2016-01-29 18:16:43 +0000 | [diff] [blame] | 220 | } |
| 221 | |
David Majnemer | 6bdc24e | 2016-07-01 23:12:45 +0000 | [diff] [blame] | 222 | static StringRef getPrettyScopeName(const DIScope *Scope) { |
| 223 | StringRef ScopeName = Scope->getName(); |
| 224 | if (!ScopeName.empty()) |
| 225 | return ScopeName; |
| 226 | |
| 227 | switch (Scope->getTag()) { |
| 228 | case dwarf::DW_TAG_enumeration_type: |
| 229 | case dwarf::DW_TAG_class_type: |
| 230 | case dwarf::DW_TAG_structure_type: |
| 231 | case dwarf::DW_TAG_union_type: |
| 232 | return "<unnamed-tag>"; |
| 233 | case dwarf::DW_TAG_namespace: |
| 234 | return "`anonymous namespace'"; |
| 235 | } |
| 236 | |
| 237 | return StringRef(); |
| 238 | } |
| 239 | |
Reid Kleckner | 0c5d874 | 2016-06-22 01:32:56 +0000 | [diff] [blame] | 240 | static const DISubprogram *getQualifiedNameComponents( |
| 241 | const DIScope *Scope, SmallVectorImpl<StringRef> &QualifiedNameComponents) { |
| 242 | const DISubprogram *ClosestSubprogram = nullptr; |
| 243 | while (Scope != nullptr) { |
| 244 | if (ClosestSubprogram == nullptr) |
| 245 | ClosestSubprogram = dyn_cast<DISubprogram>(Scope); |
David Majnemer | 6bdc24e | 2016-07-01 23:12:45 +0000 | [diff] [blame] | 246 | StringRef ScopeName = getPrettyScopeName(Scope); |
Reid Kleckner | 0c5d874 | 2016-06-22 01:32:56 +0000 | [diff] [blame] | 247 | if (!ScopeName.empty()) |
| 248 | QualifiedNameComponents.push_back(ScopeName); |
| 249 | Scope = Scope->getScope().resolve(); |
| 250 | } |
| 251 | return ClosestSubprogram; |
| 252 | } |
| 253 | |
| 254 | static std::string getQualifiedName(ArrayRef<StringRef> QualifiedNameComponents, |
| 255 | StringRef TypeName) { |
| 256 | std::string FullyQualifiedName; |
Eugene Zelenko | fb69e66 | 2017-06-06 22:22:41 +0000 | [diff] [blame] | 257 | for (StringRef QualifiedNameComponent : |
| 258 | llvm::reverse(QualifiedNameComponents)) { |
Reid Kleckner | 0c5d874 | 2016-06-22 01:32:56 +0000 | [diff] [blame] | 259 | FullyQualifiedName.append(QualifiedNameComponent); |
| 260 | FullyQualifiedName.append("::"); |
| 261 | } |
| 262 | FullyQualifiedName.append(TypeName); |
| 263 | return FullyQualifiedName; |
| 264 | } |
| 265 | |
| 266 | static std::string getFullyQualifiedName(const DIScope *Scope, StringRef Name) { |
| 267 | SmallVector<StringRef, 5> QualifiedNameComponents; |
| 268 | getQualifiedNameComponents(Scope, QualifiedNameComponents); |
| 269 | return getQualifiedName(QualifiedNameComponents, Name); |
| 270 | } |
| 271 | |
Reid Kleckner | b5af11d | 2016-07-01 02:41:21 +0000 | [diff] [blame] | 272 | struct CodeViewDebug::TypeLoweringScope { |
| 273 | TypeLoweringScope(CodeViewDebug &CVD) : CVD(CVD) { ++CVD.TypeEmissionLevel; } |
| 274 | ~TypeLoweringScope() { |
| 275 | // Don't decrement TypeEmissionLevel until after emitting deferred types, so |
| 276 | // inner TypeLoweringScopes don't attempt to emit deferred types. |
| 277 | if (CVD.TypeEmissionLevel == 1) |
| 278 | CVD.emitDeferredCompleteTypes(); |
| 279 | --CVD.TypeEmissionLevel; |
| 280 | } |
| 281 | CodeViewDebug &CVD; |
| 282 | }; |
| 283 | |
David Majnemer | 6bdc24e | 2016-07-01 23:12:45 +0000 | [diff] [blame] | 284 | static std::string getFullyQualifiedName(const DIScope *Ty) { |
| 285 | const DIScope *Scope = Ty->getScope().resolve(); |
| 286 | return getFullyQualifiedName(Scope, getPrettyScopeName(Ty)); |
| 287 | } |
| 288 | |
Reid Kleckner | 0c5d874 | 2016-06-22 01:32:56 +0000 | [diff] [blame] | 289 | TypeIndex CodeViewDebug::getScopeIndex(const DIScope *Scope) { |
| 290 | // No scope means global scope and that uses the zero index. |
| 291 | if (!Scope || isa<DIFile>(Scope)) |
| 292 | return TypeIndex(); |
| 293 | |
| 294 | assert(!isa<DIType>(Scope) && "shouldn't make a namespace scope for a type"); |
| 295 | |
| 296 | // Check if we've already translated this scope. |
| 297 | auto I = TypeIndices.find({Scope, nullptr}); |
| 298 | if (I != TypeIndices.end()) |
| 299 | return I->second; |
| 300 | |
| 301 | // Build the fully qualified name of the scope. |
David Majnemer | 6bdc24e | 2016-07-01 23:12:45 +0000 | [diff] [blame] | 302 | std::string ScopeName = getFullyQualifiedName(Scope); |
Zachary Turner | 4efa0a4 | 2016-11-08 22:24:53 +0000 | [diff] [blame] | 303 | StringIdRecord SID(TypeIndex(), ScopeName); |
Zachary Turner | 6900de1 | 2017-11-28 18:33:17 +0000 | [diff] [blame] | 304 | auto TI = TypeTable.writeLeafType(SID); |
Reid Kleckner | 0c5d874 | 2016-06-22 01:32:56 +0000 | [diff] [blame] | 305 | return recordTypeIndexForDINode(Scope, TI); |
| 306 | } |
| 307 | |
David Majnemer | 75c3ebf | 2016-06-02 17:13:53 +0000 | [diff] [blame] | 308 | TypeIndex CodeViewDebug::getFuncIdForSubprogram(const DISubprogram *SP) { |
David Majnemer | 67f684e | 2016-07-28 05:03:22 +0000 | [diff] [blame] | 309 | assert(SP); |
Reid Kleckner | 2280f93 | 2016-05-23 20:23:46 +0000 | [diff] [blame] | 310 | |
David Majnemer | 75c3ebf | 2016-06-02 17:13:53 +0000 | [diff] [blame] | 311 | // Check if we've already translated this subprogram. |
Amjad Aboud | 76c9eb9 | 2016-06-18 10:25:07 +0000 | [diff] [blame] | 312 | auto I = TypeIndices.find({SP, nullptr}); |
David Majnemer | 75c3ebf | 2016-06-02 17:13:53 +0000 | [diff] [blame] | 313 | if (I != TypeIndices.end()) |
| 314 | return I->second; |
Reid Kleckner | 2280f93 | 2016-05-23 20:23:46 +0000 | [diff] [blame] | 315 | |
Reid Kleckner | ac945e2 | 2016-06-17 16:11:20 +0000 | [diff] [blame] | 316 | // The display name includes function template arguments. Drop them to match |
| 317 | // MSVC. |
Adrian Prantl | 9d2f019 | 2017-04-26 23:59:52 +0000 | [diff] [blame] | 318 | StringRef DisplayName = SP->getName().split('<').first; |
David Majnemer | 75c3ebf | 2016-06-02 17:13:53 +0000 | [diff] [blame] | 319 | |
Reid Kleckner | 0c5d874 | 2016-06-22 01:32:56 +0000 | [diff] [blame] | 320 | const DIScope *Scope = SP->getScope().resolve(); |
| 321 | TypeIndex TI; |
| 322 | if (const auto *Class = dyn_cast_or_null<DICompositeType>(Scope)) { |
| 323 | // If the scope is a DICompositeType, then this must be a method. Member |
| 324 | // function types take some special handling, and require access to the |
| 325 | // subprogram. |
| 326 | TypeIndex ClassType = getTypeIndex(Class); |
| 327 | MemberFuncIdRecord MFuncId(ClassType, getMemberFunctionType(SP, Class), |
| 328 | DisplayName); |
Zachary Turner | 6900de1 | 2017-11-28 18:33:17 +0000 | [diff] [blame] | 329 | TI = TypeTable.writeLeafType(MFuncId); |
Reid Kleckner | 0c5d874 | 2016-06-22 01:32:56 +0000 | [diff] [blame] | 330 | } else { |
| 331 | // Otherwise, this must be a free function. |
| 332 | TypeIndex ParentScope = getScopeIndex(Scope); |
| 333 | FuncIdRecord FuncId(ParentScope, getTypeIndex(SP->getType()), DisplayName); |
Zachary Turner | 6900de1 | 2017-11-28 18:33:17 +0000 | [diff] [blame] | 334 | TI = TypeTable.writeLeafType(FuncId); |
Reid Kleckner | 0c5d874 | 2016-06-22 01:32:56 +0000 | [diff] [blame] | 335 | } |
| 336 | |
| 337 | return recordTypeIndexForDINode(SP, TI); |
Reid Kleckner | 2280f93 | 2016-05-23 20:23:46 +0000 | [diff] [blame] | 338 | } |
| 339 | |
Reid Kleckner | 0c5d874 | 2016-06-22 01:32:56 +0000 | [diff] [blame] | 340 | TypeIndex CodeViewDebug::getMemberFunctionType(const DISubprogram *SP, |
| 341 | const DICompositeType *Class) { |
Reid Kleckner | b5af11d | 2016-07-01 02:41:21 +0000 | [diff] [blame] | 342 | // Always use the method declaration as the key for the function type. The |
| 343 | // method declaration contains the this adjustment. |
| 344 | if (SP->getDeclaration()) |
| 345 | SP = SP->getDeclaration(); |
| 346 | assert(!SP->getDeclaration() && "should use declaration as key"); |
| 347 | |
Reid Kleckner | 0c5d874 | 2016-06-22 01:32:56 +0000 | [diff] [blame] | 348 | // Key the MemberFunctionRecord into the map as {SP, Class}. It won't collide |
| 349 | // with the MemberFuncIdRecord, which is keyed in as {SP, nullptr}. |
Reid Kleckner | b5af11d | 2016-07-01 02:41:21 +0000 | [diff] [blame] | 350 | auto I = TypeIndices.find({SP, Class}); |
Reid Kleckner | 0c5d874 | 2016-06-22 01:32:56 +0000 | [diff] [blame] | 351 | if (I != TypeIndices.end()) |
| 352 | return I->second; |
| 353 | |
Reid Kleckner | b5af11d | 2016-07-01 02:41:21 +0000 | [diff] [blame] | 354 | // Make sure complete type info for the class is emitted *after* the member |
| 355 | // function type, as the complete class type is likely to reference this |
| 356 | // member function type. |
| 357 | TypeLoweringScope S(*this); |
Adrian McCarthy | d91bf39 | 2017-09-13 20:53:55 +0000 | [diff] [blame] | 358 | const bool IsStaticMethod = (SP->getFlags() & DINode::FlagStaticMember) != 0; |
| 359 | TypeIndex TI = lowerTypeMemberFunction( |
| 360 | SP->getType(), Class, SP->getThisAdjustment(), IsStaticMethod); |
Reid Kleckner | 0c5d874 | 2016-06-22 01:32:56 +0000 | [diff] [blame] | 361 | return recordTypeIndexForDINode(SP, TI, Class); |
| 362 | } |
| 363 | |
Amjad Aboud | acee568 | 2016-07-12 12:06:34 +0000 | [diff] [blame] | 364 | TypeIndex CodeViewDebug::recordTypeIndexForDINode(const DINode *Node, |
| 365 | TypeIndex TI, |
| 366 | const DIType *ClassTy) { |
Amjad Aboud | 76c9eb9 | 2016-06-18 10:25:07 +0000 | [diff] [blame] | 367 | auto InsertResult = TypeIndices.insert({{Node, ClassTy}, TI}); |
Reid Kleckner | a8d5740 | 2016-06-03 15:58:20 +0000 | [diff] [blame] | 368 | (void)InsertResult; |
| 369 | assert(InsertResult.second && "DINode was already assigned a type index"); |
Reid Kleckner | 0c5d874 | 2016-06-22 01:32:56 +0000 | [diff] [blame] | 370 | return TI; |
Reid Kleckner | a8d5740 | 2016-06-03 15:58:20 +0000 | [diff] [blame] | 371 | } |
| 372 | |
Amjad Aboud | 76c9eb9 | 2016-06-18 10:25:07 +0000 | [diff] [blame] | 373 | unsigned CodeViewDebug::getPointerSizeInBytes() { |
| 374 | return MMI->getModule()->getDataLayout().getPointerSizeInBits() / 8; |
| 375 | } |
| 376 | |
Reid Kleckner | 876330d | 2016-02-12 21:48:30 +0000 | [diff] [blame] | 377 | void CodeViewDebug::recordLocalVariable(LocalVariable &&Var, |
Reid Kleckner | 5a791ee | 2018-03-15 21:24:04 +0000 | [diff] [blame] | 378 | const LexicalScope *LS) { |
| 379 | if (const DILocation *InlinedAt = LS->getInlinedAt()) { |
Reid Kleckner | 876330d | 2016-02-12 21:48:30 +0000 | [diff] [blame] | 380 | // This variable was inlined. Associate it with the InlineSite. |
| 381 | const DISubprogram *Inlinee = Var.DIVar->getScope()->getSubprogram(); |
| 382 | InlineSite &Site = getInlineSite(InlinedAt, Inlinee); |
| 383 | Site.InlinedLocals.emplace_back(Var); |
| 384 | } else { |
Reid Kleckner | 5a791ee | 2018-03-15 21:24:04 +0000 | [diff] [blame] | 385 | // This variable goes into the corresponding lexical scope. |
| 386 | ScopeVariables[LS].emplace_back(Var); |
Reid Kleckner | 876330d | 2016-02-12 21:48:30 +0000 | [diff] [blame] | 387 | } |
| 388 | } |
| 389 | |
Reid Kleckner | 829365a | 2016-02-11 19:41:47 +0000 | [diff] [blame] | 390 | static void addLocIfNotPresent(SmallVectorImpl<const DILocation *> &Locs, |
| 391 | const DILocation *Loc) { |
| 392 | auto B = Locs.begin(), E = Locs.end(); |
| 393 | if (std::find(B, E, Loc) == E) |
| 394 | Locs.push_back(Loc); |
| 395 | } |
| 396 | |
Benjamin Kramer | bdc4956 | 2016-06-12 15:39:02 +0000 | [diff] [blame] | 397 | void CodeViewDebug::maybeRecordLocation(const DebugLoc &DL, |
Reid Kleckner | 9533af4 | 2016-01-16 00:09:09 +0000 | [diff] [blame] | 398 | const MachineFunction *MF) { |
| 399 | // Skip this instruction if it has the same location as the previous one. |
Reid Kleckner | 45a7462 | 2017-06-30 21:33:44 +0000 | [diff] [blame] | 400 | if (!DL || DL == PrevInstLoc) |
Reid Kleckner | 9533af4 | 2016-01-16 00:09:09 +0000 | [diff] [blame] | 401 | return; |
| 402 | |
| 403 | const DIScope *Scope = DL.get()->getScope(); |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 404 | if (!Scope) |
| 405 | return; |
Reid Kleckner | 9533af4 | 2016-01-16 00:09:09 +0000 | [diff] [blame] | 406 | |
David Majnemer | c3340db | 2016-01-13 01:05:23 +0000 | [diff] [blame] | 407 | // Skip this line if it is longer than the maximum we can record. |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 408 | LineInfo LI(DL.getLine(), DL.getLine(), /*IsStatement=*/true); |
| 409 | if (LI.getStartLine() != DL.getLine() || LI.isAlwaysStepInto() || |
| 410 | LI.isNeverStepInto()) |
David Majnemer | c3340db | 2016-01-13 01:05:23 +0000 | [diff] [blame] | 411 | return; |
| 412 | |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 413 | ColumnInfo CI(DL.getCol(), /*EndColumn=*/0); |
| 414 | if (CI.getStartColumn() != DL.getCol()) |
| 415 | return; |
Reid Kleckner | 00d9639 | 2016-01-29 00:13:28 +0000 | [diff] [blame] | 416 | |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 417 | if (!CurFn->HaveLineInfo) |
| 418 | CurFn->HaveLineInfo = true; |
| 419 | unsigned FileId = 0; |
Reid Kleckner | 45a7462 | 2017-06-30 21:33:44 +0000 | [diff] [blame] | 420 | if (PrevInstLoc.get() && PrevInstLoc->getFile() == DL->getFile()) |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 421 | FileId = CurFn->LastFileId; |
| 422 | else |
| 423 | FileId = CurFn->LastFileId = maybeRecordFile(DL->getFile()); |
Reid Kleckner | 45a7462 | 2017-06-30 21:33:44 +0000 | [diff] [blame] | 424 | PrevInstLoc = DL; |
Reid Kleckner | f3b9ba4 | 2016-01-29 18:16:43 +0000 | [diff] [blame] | 425 | |
| 426 | unsigned FuncId = CurFn->FuncId; |
Reid Kleckner | 876330d | 2016-02-12 21:48:30 +0000 | [diff] [blame] | 427 | if (const DILocation *SiteLoc = DL->getInlinedAt()) { |
Reid Kleckner | 829365a | 2016-02-11 19:41:47 +0000 | [diff] [blame] | 428 | const DILocation *Loc = DL.get(); |
| 429 | |
Reid Kleckner | f3b9ba4 | 2016-01-29 18:16:43 +0000 | [diff] [blame] | 430 | // If this location was actually inlined from somewhere else, give it the ID |
| 431 | // of the inline call site. |
Reid Kleckner | 876330d | 2016-02-12 21:48:30 +0000 | [diff] [blame] | 432 | FuncId = |
| 433 | getInlineSite(SiteLoc, Loc->getScope()->getSubprogram()).SiteFuncId; |
Reid Kleckner | 829365a | 2016-02-11 19:41:47 +0000 | [diff] [blame] | 434 | |
Reid Kleckner | f3b9ba4 | 2016-01-29 18:16:43 +0000 | [diff] [blame] | 435 | // Ensure we have links in the tree of inline call sites. |
Reid Kleckner | 829365a | 2016-02-11 19:41:47 +0000 | [diff] [blame] | 436 | bool FirstLoc = true; |
| 437 | while ((SiteLoc = Loc->getInlinedAt())) { |
Reid Kleckner | 876330d | 2016-02-12 21:48:30 +0000 | [diff] [blame] | 438 | InlineSite &Site = |
| 439 | getInlineSite(SiteLoc, Loc->getScope()->getSubprogram()); |
Reid Kleckner | 829365a | 2016-02-11 19:41:47 +0000 | [diff] [blame] | 440 | if (!FirstLoc) |
| 441 | addLocIfNotPresent(Site.ChildSites, Loc); |
| 442 | FirstLoc = false; |
| 443 | Loc = SiteLoc; |
Reid Kleckner | f3b9ba4 | 2016-01-29 18:16:43 +0000 | [diff] [blame] | 444 | } |
Reid Kleckner | 829365a | 2016-02-11 19:41:47 +0000 | [diff] [blame] | 445 | addLocIfNotPresent(CurFn->ChildSites, Loc); |
Reid Kleckner | f3b9ba4 | 2016-01-29 18:16:43 +0000 | [diff] [blame] | 446 | } |
| 447 | |
Reid Kleckner | dac21b4 | 2016-02-03 21:15:48 +0000 | [diff] [blame] | 448 | OS.EmitCVLocDirective(FuncId, FileId, DL.getLine(), DL.getCol(), |
Reid Kleckner | a9f4cc9 | 2016-09-07 16:15:31 +0000 | [diff] [blame] | 449 | /*PrologueEnd=*/false, /*IsStmt=*/false, |
| 450 | DL->getFilename(), SMLoc()); |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 451 | } |
| 452 | |
Reid Kleckner | 5d122f8 | 2016-05-25 23:16:12 +0000 | [diff] [blame] | 453 | void CodeViewDebug::emitCodeViewMagicVersion() { |
| 454 | OS.EmitValueToAlignment(4); |
| 455 | OS.AddComment("Debug section magic"); |
| 456 | OS.EmitIntValue(COFF::DEBUG_SECTION_MAGIC, 4); |
| 457 | } |
| 458 | |
Reid Kleckner | 70f5bc9 | 2016-01-14 19:25:04 +0000 | [diff] [blame] | 459 | void CodeViewDebug::endModule() { |
Reid Kleckner | 6f3406d | 2016-06-07 00:02:03 +0000 | [diff] [blame] | 460 | if (!Asm || !MMI->hasDebugInfo()) |
Timur Iskhodzhanov | 2cf8a1d | 2014-10-10 16:05:32 +0000 | [diff] [blame] | 461 | return; |
| 462 | |
| 463 | assert(Asm != nullptr); |
Timur Iskhodzhanov | 2cf8a1d | 2014-10-10 16:05:32 +0000 | [diff] [blame] | 464 | |
| 465 | // The COFF .debug$S section consists of several subsections, each starting |
| 466 | // with a 4-byte control code (e.g. 0xF1, 0xF2, etc) and then a 4-byte length |
| 467 | // of the payload followed by the payload itself. The subsections are 4-byte |
| 468 | // aligned. |
| 469 | |
Reid Kleckner | 6f3406d | 2016-06-07 00:02:03 +0000 | [diff] [blame] | 470 | // Use the generic .debug$S section, and make a subsection for all the inlined |
| 471 | // subprograms. |
| 472 | switchToDebugSectionForSymbol(nullptr); |
Adrian McCarthy | 4333daa | 2016-11-02 21:30:35 +0000 | [diff] [blame] | 473 | |
Zachary Turner | 8c099fe | 2017-05-30 16:36:15 +0000 | [diff] [blame] | 474 | MCSymbol *CompilerInfo = beginCVSubsection(DebugSubsectionKind::Symbols); |
Adrian McCarthy | 4333daa | 2016-11-02 21:30:35 +0000 | [diff] [blame] | 475 | emitCompilerInformation(); |
| 476 | endCVSubsection(CompilerInfo); |
| 477 | |
Reid Kleckner | 5d122f8 | 2016-05-25 23:16:12 +0000 | [diff] [blame] | 478 | emitInlineeLinesSubsection(); |
Reid Kleckner | 1fcd610 | 2016-02-02 17:41:18 +0000 | [diff] [blame] | 479 | |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 480 | // Emit per-function debug information. |
| 481 | for (auto &P : FnDebugInfo) |
David Majnemer | 577be0f | 2016-06-15 00:19:52 +0000 | [diff] [blame] | 482 | if (!P.first->isDeclarationForLinker()) |
Reid Kleckner | 55baeef | 2018-03-15 21:12:21 +0000 | [diff] [blame] | 483 | emitDebugInfoForFunction(P.first, *P.second); |
Timur Iskhodzhanov | 2cf8a1d | 2014-10-10 16:05:32 +0000 | [diff] [blame] | 484 | |
Reid Kleckner | 6f3406d | 2016-06-07 00:02:03 +0000 | [diff] [blame] | 485 | // Emit global variable debug information. |
David Majnemer | 3128b10 | 2016-06-15 18:00:01 +0000 | [diff] [blame] | 486 | setCurrentSubprogram(nullptr); |
Reid Kleckner | 6f3406d | 2016-06-07 00:02:03 +0000 | [diff] [blame] | 487 | emitDebugInfoForGlobals(); |
| 488 | |
Hans Wennborg | b510b45 | 2016-06-23 16:33:53 +0000 | [diff] [blame] | 489 | // Emit retained types. |
| 490 | emitDebugInfoForRetainedTypes(); |
| 491 | |
Reid Kleckner | 5d122f8 | 2016-05-25 23:16:12 +0000 | [diff] [blame] | 492 | // Switch back to the generic .debug$S section after potentially processing |
| 493 | // comdat symbol sections. |
| 494 | switchToDebugSectionForSymbol(nullptr); |
| 495 | |
David Majnemer | 3128b10 | 2016-06-15 18:00:01 +0000 | [diff] [blame] | 496 | // Emit UDT records for any types used by global variables. |
| 497 | if (!GlobalUDTs.empty()) { |
Zachary Turner | 8c099fe | 2017-05-30 16:36:15 +0000 | [diff] [blame] | 498 | MCSymbol *SymbolsEnd = beginCVSubsection(DebugSubsectionKind::Symbols); |
David Majnemer | 3128b10 | 2016-06-15 18:00:01 +0000 | [diff] [blame] | 499 | emitDebugInfoForUDTs(GlobalUDTs); |
| 500 | endCVSubsection(SymbolsEnd); |
| 501 | } |
| 502 | |
Timur Iskhodzhanov | 2cf8a1d | 2014-10-10 16:05:32 +0000 | [diff] [blame] | 503 | // This subsection holds a file index to offset in string table table. |
Reid Kleckner | dac21b4 | 2016-02-03 21:15:48 +0000 | [diff] [blame] | 504 | OS.AddComment("File index to string table offset subsection"); |
| 505 | OS.EmitCVFileChecksumsDirective(); |
Timur Iskhodzhanov | 2cf8a1d | 2014-10-10 16:05:32 +0000 | [diff] [blame] | 506 | |
| 507 | // This subsection holds the string table. |
Reid Kleckner | dac21b4 | 2016-02-03 21:15:48 +0000 | [diff] [blame] | 508 | OS.AddComment("String table"); |
| 509 | OS.EmitCVStringTableDirective(); |
Timur Iskhodzhanov | 2cf8a1d | 2014-10-10 16:05:32 +0000 | [diff] [blame] | 510 | |
Zachary Turner | 048f8f9 | 2017-12-13 22:33:58 +0000 | [diff] [blame] | 511 | // Emit type information and hashes last, so that any types we translate while |
| 512 | // emitting function info are included. |
Reid Kleckner | 5acacbb | 2016-06-01 17:05:51 +0000 | [diff] [blame] | 513 | emitTypeInformation(); |
| 514 | |
Zachary Turner | 048f8f9 | 2017-12-13 22:33:58 +0000 | [diff] [blame] | 515 | if (EmitDebugGlobalHashes) |
| 516 | emitTypeGlobalHashes(); |
| 517 | |
Timur Iskhodzhanov | 2cf8a1d | 2014-10-10 16:05:32 +0000 | [diff] [blame] | 518 | clear(); |
| 519 | } |
| 520 | |
Brock Wyma | 19e17b3 | 2018-02-11 21:26:46 +0000 | [diff] [blame] | 521 | static void emitNullTerminatedSymbolName(MCStreamer &OS, StringRef S, |
| 522 | unsigned MaxFixedRecordLength = 0xF00) { |
Reid Kleckner | bb96df6 | 2016-10-05 22:36:07 +0000 | [diff] [blame] | 523 | // The maximum CV record length is 0xFF00. Most of the strings we emit appear |
| 524 | // after a fixed length portion of the record. The fixed length portion should |
| 525 | // always be less than 0xF00 (3840) bytes, so truncate the string so that the |
| 526 | // overall record size is less than the maximum allowed. |
Reid Kleckner | bb96df6 | 2016-10-05 22:36:07 +0000 | [diff] [blame] | 527 | SmallString<32> NullTerminatedString( |
| 528 | S.take_front(MaxRecordLength - MaxFixedRecordLength - 1)); |
David Majnemer | b9456a5 | 2016-03-14 05:15:09 +0000 | [diff] [blame] | 529 | NullTerminatedString.push_back('\0'); |
| 530 | OS.EmitBytes(NullTerminatedString); |
| 531 | } |
| 532 | |
Reid Kleckner | f3b9ba4 | 2016-01-29 18:16:43 +0000 | [diff] [blame] | 533 | void CodeViewDebug::emitTypeInformation() { |
Reid Kleckner | 2280f93 | 2016-05-23 20:23:46 +0000 | [diff] [blame] | 534 | if (TypeTable.empty()) |
Reid Kleckner | fbd7787 | 2016-03-18 18:54:32 +0000 | [diff] [blame] | 535 | return; |
| 536 | |
Alexandre Ganea | d9e9674 | 2018-04-09 20:17:56 +0000 | [diff] [blame] | 537 | // Start the .debug$T or .debug$P section with 0x4. |
Reid Kleckner | dac21b4 | 2016-02-03 21:15:48 +0000 | [diff] [blame] | 538 | OS.SwitchSection(Asm->getObjFileLowering().getCOFFDebugTypesSection()); |
Reid Kleckner | 5d122f8 | 2016-05-25 23:16:12 +0000 | [diff] [blame] | 539 | emitCodeViewMagicVersion(); |
Reid Kleckner | f3b9ba4 | 2016-01-29 18:16:43 +0000 | [diff] [blame] | 540 | |
Reid Kleckner | fbdbe9e | 2016-05-31 18:45:36 +0000 | [diff] [blame] | 541 | SmallString<8> CommentPrefix; |
| 542 | if (OS.isVerboseAsm()) { |
| 543 | CommentPrefix += '\t'; |
| 544 | CommentPrefix += Asm->MAI->getCommentString(); |
| 545 | CommentPrefix += ' '; |
| 546 | } |
| 547 | |
Zachary Turner | 526f4f2 | 2017-05-19 19:26:58 +0000 | [diff] [blame] | 548 | TypeTableCollection Table(TypeTable.records()); |
| 549 | Optional<TypeIndex> B = Table.getFirst(); |
| 550 | while (B) { |
| 551 | // This will fail if the record data is invalid. |
| 552 | CVType Record = Table.getType(*B); |
| 553 | |
Zachary Turner | 4efa0a4 | 2016-11-08 22:24:53 +0000 | [diff] [blame] | 554 | if (OS.isVerboseAsm()) { |
| 555 | // Emit a block comment describing the type record for readability. |
| 556 | SmallString<512> CommentBlock; |
| 557 | raw_svector_ostream CommentOS(CommentBlock); |
| 558 | ScopedPrinter SP(CommentOS); |
| 559 | SP.setPrefix(CommentPrefix); |
Zachary Turner | 526f4f2 | 2017-05-19 19:26:58 +0000 | [diff] [blame] | 560 | TypeDumpVisitor TDV(Table, &SP, false); |
| 561 | |
| 562 | Error E = codeview::visitTypeRecord(Record, *B, TDV); |
Zachary Turner | 4efa0a4 | 2016-11-08 22:24:53 +0000 | [diff] [blame] | 563 | if (E) { |
| 564 | logAllUnhandledErrors(std::move(E), errs(), "error: "); |
| 565 | llvm_unreachable("produced malformed type record"); |
| 566 | } |
| 567 | // emitRawComment will insert its own tab and comment string before |
| 568 | // the first line, so strip off our first one. It also prints its own |
| 569 | // newline. |
| 570 | OS.emitRawComment( |
| 571 | CommentOS.str().drop_front(CommentPrefix.size() - 1).rtrim()); |
Zachary Turner | 4efa0a4 | 2016-11-08 22:24:53 +0000 | [diff] [blame] | 572 | } |
Zachary Turner | 526f4f2 | 2017-05-19 19:26:58 +0000 | [diff] [blame] | 573 | OS.EmitBinaryData(Record.str_data()); |
| 574 | B = Table.getNext(*B); |
| 575 | } |
Reid Kleckner | f3b9ba4 | 2016-01-29 18:16:43 +0000 | [diff] [blame] | 576 | } |
| 577 | |
Zachary Turner | 048f8f9 | 2017-12-13 22:33:58 +0000 | [diff] [blame] | 578 | void CodeViewDebug::emitTypeGlobalHashes() { |
| 579 | if (TypeTable.empty()) |
| 580 | return; |
| 581 | |
| 582 | // Start the .debug$H section with the version and hash algorithm, currently |
| 583 | // hardcoded to version 0, SHA1. |
| 584 | OS.SwitchSection(Asm->getObjFileLowering().getCOFFGlobalTypeHashesSection()); |
| 585 | |
| 586 | OS.EmitValueToAlignment(4); |
| 587 | OS.AddComment("Magic"); |
| 588 | OS.EmitIntValue(COFF::DEBUG_HASHES_SECTION_MAGIC, 4); |
| 589 | OS.AddComment("Section Version"); |
| 590 | OS.EmitIntValue(0, 2); |
| 591 | OS.AddComment("Hash Algorithm"); |
Zachary Turner | c762666 | 2018-05-17 22:55:15 +0000 | [diff] [blame] | 592 | OS.EmitIntValue(uint16_t(GlobalTypeHashAlg::SHA1_8), 2); |
Zachary Turner | 048f8f9 | 2017-12-13 22:33:58 +0000 | [diff] [blame] | 593 | |
| 594 | TypeIndex TI(TypeIndex::FirstNonSimpleIndex); |
| 595 | for (const auto &GHR : TypeTable.hashes()) { |
| 596 | if (OS.isVerboseAsm()) { |
| 597 | // Emit an EOL-comment describing which TypeIndex this hash corresponds |
| 598 | // to, as well as the stringified SHA1 hash. |
| 599 | SmallString<32> Comment; |
| 600 | raw_svector_ostream CommentOS(Comment); |
| 601 | CommentOS << formatv("{0:X+} [{1}]", TI.getIndex(), GHR); |
| 602 | OS.AddComment(Comment); |
| 603 | ++TI; |
| 604 | } |
Zachary Turner | c762666 | 2018-05-17 22:55:15 +0000 | [diff] [blame] | 605 | assert(GHR.Hash.size() == 8); |
Zachary Turner | 048f8f9 | 2017-12-13 22:33:58 +0000 | [diff] [blame] | 606 | StringRef S(reinterpret_cast<const char *>(GHR.Hash.data()), |
| 607 | GHR.Hash.size()); |
| 608 | OS.EmitBinaryData(S); |
| 609 | } |
| 610 | } |
| 611 | |
Adrian McCarthy | c64acfd | 2016-09-20 17:20:51 +0000 | [diff] [blame] | 612 | static SourceLanguage MapDWLangToCVLang(unsigned DWLang) { |
| 613 | switch (DWLang) { |
| 614 | case dwarf::DW_LANG_C: |
| 615 | case dwarf::DW_LANG_C89: |
| 616 | case dwarf::DW_LANG_C99: |
| 617 | case dwarf::DW_LANG_C11: |
| 618 | case dwarf::DW_LANG_ObjC: |
| 619 | return SourceLanguage::C; |
| 620 | case dwarf::DW_LANG_C_plus_plus: |
| 621 | case dwarf::DW_LANG_C_plus_plus_03: |
| 622 | case dwarf::DW_LANG_C_plus_plus_11: |
| 623 | case dwarf::DW_LANG_C_plus_plus_14: |
| 624 | return SourceLanguage::Cpp; |
| 625 | case dwarf::DW_LANG_Fortran77: |
| 626 | case dwarf::DW_LANG_Fortran90: |
| 627 | case dwarf::DW_LANG_Fortran03: |
| 628 | case dwarf::DW_LANG_Fortran08: |
| 629 | return SourceLanguage::Fortran; |
| 630 | case dwarf::DW_LANG_Pascal83: |
| 631 | return SourceLanguage::Pascal; |
| 632 | case dwarf::DW_LANG_Cobol74: |
| 633 | case dwarf::DW_LANG_Cobol85: |
| 634 | return SourceLanguage::Cobol; |
| 635 | case dwarf::DW_LANG_Java: |
| 636 | return SourceLanguage::Java; |
Reid Kleckner | 898ddf6 | 2017-07-24 16:16:42 +0000 | [diff] [blame] | 637 | case dwarf::DW_LANG_D: |
| 638 | return SourceLanguage::D; |
Adrian McCarthy | c64acfd | 2016-09-20 17:20:51 +0000 | [diff] [blame] | 639 | default: |
| 640 | // There's no CodeView representation for this language, and CV doesn't |
| 641 | // have an "unknown" option for the language field, so we'll use MASM, |
| 642 | // as it's very low level. |
| 643 | return SourceLanguage::Masm; |
| 644 | } |
| 645 | } |
| 646 | |
Reid Kleckner | 7f6b253 | 2017-07-24 16:16:17 +0000 | [diff] [blame] | 647 | namespace { |
Adrian McCarthy | c64acfd | 2016-09-20 17:20:51 +0000 | [diff] [blame] | 648 | struct Version { |
| 649 | int Part[4]; |
| 650 | }; |
Reid Kleckner | 7f6b253 | 2017-07-24 16:16:17 +0000 | [diff] [blame] | 651 | } // end anonymous namespace |
Adrian McCarthy | c64acfd | 2016-09-20 17:20:51 +0000 | [diff] [blame] | 652 | |
| 653 | // Takes a StringRef like "clang 4.0.0.0 (other nonsense 123)" and parses out |
| 654 | // the version number. |
| 655 | static Version parseVersion(StringRef Name) { |
Adrian McCarthy | ad8ac54 | 2016-09-20 17:42:13 +0000 | [diff] [blame] | 656 | Version V = {{0}}; |
Adrian McCarthy | c64acfd | 2016-09-20 17:20:51 +0000 | [diff] [blame] | 657 | int N = 0; |
| 658 | for (const char C : Name) { |
| 659 | if (isdigit(C)) { |
| 660 | V.Part[N] *= 10; |
| 661 | V.Part[N] += C - '0'; |
| 662 | } else if (C == '.') { |
| 663 | ++N; |
| 664 | if (N >= 4) |
| 665 | return V; |
| 666 | } else if (N > 0) |
| 667 | return V; |
| 668 | } |
| 669 | return V; |
| 670 | } |
| 671 | |
| 672 | static CPUType mapArchToCVCPUType(Triple::ArchType Type) { |
| 673 | switch (Type) { |
Reid Kleckner | 7f6b253 | 2017-07-24 16:16:17 +0000 | [diff] [blame] | 674 | case Triple::ArchType::x86: |
| 675 | return CPUType::Pentium3; |
| 676 | case Triple::ArchType::x86_64: |
| 677 | return CPUType::X64; |
| 678 | case Triple::ArchType::thumb: |
| 679 | return CPUType::Thumb; |
| 680 | case Triple::ArchType::aarch64: |
| 681 | return CPUType::ARM64; |
| 682 | default: |
| 683 | report_fatal_error("target architecture doesn't map to a CodeView CPUType"); |
Adrian McCarthy | c64acfd | 2016-09-20 17:20:51 +0000 | [diff] [blame] | 684 | } |
| 685 | } |
| 686 | |
Adrian McCarthy | c64acfd | 2016-09-20 17:20:51 +0000 | [diff] [blame] | 687 | void CodeViewDebug::emitCompilerInformation() { |
| 688 | MCContext &Context = MMI->getContext(); |
| 689 | MCSymbol *CompilerBegin = Context.createTempSymbol(), |
| 690 | *CompilerEnd = Context.createTempSymbol(); |
| 691 | OS.AddComment("Record length"); |
| 692 | OS.emitAbsoluteSymbolDiff(CompilerEnd, CompilerBegin, 2); |
| 693 | OS.EmitLabel(CompilerBegin); |
| 694 | OS.AddComment("Record kind: S_COMPILE3"); |
| 695 | OS.EmitIntValue(SymbolKind::S_COMPILE3, 2); |
| 696 | uint32_t Flags = 0; |
| 697 | |
| 698 | NamedMDNode *CUs = MMI->getModule()->getNamedMetadata("llvm.dbg.cu"); |
| 699 | const MDNode *Node = *CUs->operands().begin(); |
| 700 | const auto *CU = cast<DICompileUnit>(Node); |
| 701 | |
| 702 | // The low byte of the flags indicates the source language. |
| 703 | Flags = MapDWLangToCVLang(CU->getSourceLanguage()); |
| 704 | // TODO: Figure out which other flags need to be set. |
| 705 | |
| 706 | OS.AddComment("Flags and language"); |
| 707 | OS.EmitIntValue(Flags, 4); |
| 708 | |
| 709 | OS.AddComment("CPUType"); |
| 710 | CPUType CPU = |
| 711 | mapArchToCVCPUType(Triple(MMI->getModule()->getTargetTriple()).getArch()); |
| 712 | OS.EmitIntValue(static_cast<uint64_t>(CPU), 2); |
| 713 | |
| 714 | StringRef CompilerVersion = CU->getProducer(); |
| 715 | Version FrontVer = parseVersion(CompilerVersion); |
| 716 | OS.AddComment("Frontend version"); |
| 717 | for (int N = 0; N < 4; ++N) |
| 718 | OS.EmitIntValue(FrontVer.Part[N], 2); |
| 719 | |
| 720 | // Some Microsoft tools, like Binscope, expect a backend version number of at |
| 721 | // least 8.something, so we'll coerce the LLVM version into a form that |
| 722 | // guarantees it'll be big enough without really lying about the version. |
Adrian McCarthy | d1185fc | 2016-09-29 20:28:25 +0000 | [diff] [blame] | 723 | int Major = 1000 * LLVM_VERSION_MAJOR + |
| 724 | 10 * LLVM_VERSION_MINOR + |
| 725 | LLVM_VERSION_PATCH; |
| 726 | // Clamp it for builds that use unusually large version numbers. |
| 727 | Major = std::min<int>(Major, std::numeric_limits<uint16_t>::max()); |
| 728 | Version BackVer = {{ Major, 0, 0, 0 }}; |
Adrian McCarthy | c64acfd | 2016-09-20 17:20:51 +0000 | [diff] [blame] | 729 | OS.AddComment("Backend version"); |
| 730 | for (int N = 0; N < 4; ++N) |
| 731 | OS.EmitIntValue(BackVer.Part[N], 2); |
| 732 | |
| 733 | OS.AddComment("Null-terminated compiler version string"); |
| 734 | emitNullTerminatedSymbolName(OS, CompilerVersion); |
| 735 | |
| 736 | OS.EmitLabel(CompilerEnd); |
| 737 | } |
| 738 | |
Reid Kleckner | 5d122f8 | 2016-05-25 23:16:12 +0000 | [diff] [blame] | 739 | void CodeViewDebug::emitInlineeLinesSubsection() { |
Reid Kleckner | 1fcd610 | 2016-02-02 17:41:18 +0000 | [diff] [blame] | 740 | if (InlinedSubprograms.empty()) |
| 741 | return; |
| 742 | |
Reid Kleckner | 1fcd610 | 2016-02-02 17:41:18 +0000 | [diff] [blame] | 743 | OS.AddComment("Inlinee lines subsection"); |
Zachary Turner | 8c099fe | 2017-05-30 16:36:15 +0000 | [diff] [blame] | 744 | MCSymbol *InlineEnd = beginCVSubsection(DebugSubsectionKind::InlineeLines); |
Reid Kleckner | 1fcd610 | 2016-02-02 17:41:18 +0000 | [diff] [blame] | 745 | |
Reid Kleckner | 26fa1bf | 2017-09-19 18:14:45 +0000 | [diff] [blame] | 746 | // We emit the checksum info for files. This is used by debuggers to |
| 747 | // determine if a pdb matches the source before loading it. Visual Studio, |
| 748 | // for instance, will display a warning that the breakpoints are not valid if |
| 749 | // the pdb does not match the source. |
David Majnemer | 30579ec | 2016-02-02 23:18:23 +0000 | [diff] [blame] | 750 | OS.AddComment("Inlinee lines signature"); |
Reid Kleckner | 1fcd610 | 2016-02-02 17:41:18 +0000 | [diff] [blame] | 751 | OS.EmitIntValue(unsigned(InlineeLinesSignature::Normal), 4); |
| 752 | |
| 753 | for (const DISubprogram *SP : InlinedSubprograms) { |
Amjad Aboud | 76c9eb9 | 2016-06-18 10:25:07 +0000 | [diff] [blame] | 754 | assert(TypeIndices.count({SP, nullptr})); |
| 755 | TypeIndex InlineeIdx = TypeIndices[{SP, nullptr}]; |
Reid Kleckner | 2280f93 | 2016-05-23 20:23:46 +0000 | [diff] [blame] | 756 | |
David Majnemer | 30579ec | 2016-02-02 23:18:23 +0000 | [diff] [blame] | 757 | OS.AddBlankLine(); |
Reid Kleckner | 1fcd610 | 2016-02-02 17:41:18 +0000 | [diff] [blame] | 758 | unsigned FileId = maybeRecordFile(SP->getFile()); |
Adrian Prantl | 9d2f019 | 2017-04-26 23:59:52 +0000 | [diff] [blame] | 759 | OS.AddComment("Inlined function " + SP->getName() + " starts at " + |
Reid Kleckner | 1fcd610 | 2016-02-02 17:41:18 +0000 | [diff] [blame] | 760 | SP->getFilename() + Twine(':') + Twine(SP->getLine())); |
David Majnemer | 30579ec | 2016-02-02 23:18:23 +0000 | [diff] [blame] | 761 | OS.AddBlankLine(); |
David Majnemer | 30579ec | 2016-02-02 23:18:23 +0000 | [diff] [blame] | 762 | OS.AddComment("Type index of inlined function"); |
Reid Kleckner | 2280f93 | 2016-05-23 20:23:46 +0000 | [diff] [blame] | 763 | OS.EmitIntValue(InlineeIdx.getIndex(), 4); |
David Majnemer | 30579ec | 2016-02-02 23:18:23 +0000 | [diff] [blame] | 764 | OS.AddComment("Offset into filechecksum table"); |
Reid Kleckner | 26fa1bf | 2017-09-19 18:14:45 +0000 | [diff] [blame] | 765 | OS.EmitCVFileChecksumOffsetDirective(FileId); |
David Majnemer | 30579ec | 2016-02-02 23:18:23 +0000 | [diff] [blame] | 766 | OS.AddComment("Starting line number"); |
Reid Kleckner | 1fcd610 | 2016-02-02 17:41:18 +0000 | [diff] [blame] | 767 | OS.EmitIntValue(SP->getLine(), 4); |
| 768 | } |
| 769 | |
Reid Kleckner | 6f3406d | 2016-06-07 00:02:03 +0000 | [diff] [blame] | 770 | endCVSubsection(InlineEnd); |
Reid Kleckner | 1fcd610 | 2016-02-02 17:41:18 +0000 | [diff] [blame] | 771 | } |
| 772 | |
Reid Kleckner | f3b9ba4 | 2016-01-29 18:16:43 +0000 | [diff] [blame] | 773 | void CodeViewDebug::emitInlinedCallSite(const FunctionInfo &FI, |
| 774 | const DILocation *InlinedAt, |
| 775 | const InlineSite &Site) { |
Reid Kleckner | f9c275f | 2016-02-10 20:55:49 +0000 | [diff] [blame] | 776 | MCSymbol *InlineBegin = MMI->getContext().createTempSymbol(), |
| 777 | *InlineEnd = MMI->getContext().createTempSymbol(); |
Reid Kleckner | f3b9ba4 | 2016-01-29 18:16:43 +0000 | [diff] [blame] | 778 | |
Amjad Aboud | 76c9eb9 | 2016-06-18 10:25:07 +0000 | [diff] [blame] | 779 | assert(TypeIndices.count({Site.Inlinee, nullptr})); |
| 780 | TypeIndex InlineeIdx = TypeIndices[{Site.Inlinee, nullptr}]; |
Reid Kleckner | f3b9ba4 | 2016-01-29 18:16:43 +0000 | [diff] [blame] | 781 | |
| 782 | // SymbolRecord |
Reid Kleckner | dac21b4 | 2016-02-03 21:15:48 +0000 | [diff] [blame] | 783 | OS.AddComment("Record length"); |
Reid Kleckner | eb3bcdd | 2016-02-03 21:24:42 +0000 | [diff] [blame] | 784 | OS.emitAbsoluteSymbolDiff(InlineEnd, InlineBegin, 2); // RecordLength |
Reid Kleckner | f3b9ba4 | 2016-01-29 18:16:43 +0000 | [diff] [blame] | 785 | OS.EmitLabel(InlineBegin); |
Reid Kleckner | dac21b4 | 2016-02-03 21:15:48 +0000 | [diff] [blame] | 786 | OS.AddComment("Record kind: S_INLINESITE"); |
Zachary Turner | 63a2846 | 2016-05-17 23:50:21 +0000 | [diff] [blame] | 787 | OS.EmitIntValue(SymbolKind::S_INLINESITE, 2); // RecordKind |
Reid Kleckner | f3b9ba4 | 2016-01-29 18:16:43 +0000 | [diff] [blame] | 788 | |
Reid Kleckner | dac21b4 | 2016-02-03 21:15:48 +0000 | [diff] [blame] | 789 | OS.AddComment("PtrParent"); |
| 790 | OS.EmitIntValue(0, 4); |
| 791 | OS.AddComment("PtrEnd"); |
| 792 | OS.EmitIntValue(0, 4); |
| 793 | OS.AddComment("Inlinee type index"); |
Reid Kleckner | 2280f93 | 2016-05-23 20:23:46 +0000 | [diff] [blame] | 794 | OS.EmitIntValue(InlineeIdx.getIndex(), 4); |
Reid Kleckner | f3b9ba4 | 2016-01-29 18:16:43 +0000 | [diff] [blame] | 795 | |
Reid Kleckner | 1fcd610 | 2016-02-02 17:41:18 +0000 | [diff] [blame] | 796 | unsigned FileId = maybeRecordFile(Site.Inlinee->getFile()); |
| 797 | unsigned StartLineNum = Site.Inlinee->getLine(); |
Reid Kleckner | 1fcd610 | 2016-02-02 17:41:18 +0000 | [diff] [blame] | 798 | |
| 799 | OS.EmitCVInlineLinetableDirective(Site.SiteFuncId, FileId, StartLineNum, |
Reid Kleckner | a9f4cc9 | 2016-09-07 16:15:31 +0000 | [diff] [blame] | 800 | FI.Begin, FI.End); |
Reid Kleckner | f3b9ba4 | 2016-01-29 18:16:43 +0000 | [diff] [blame] | 801 | |
| 802 | OS.EmitLabel(InlineEnd); |
| 803 | |
Reid Kleckner | 10dd55c | 2016-06-24 17:55:40 +0000 | [diff] [blame] | 804 | emitLocalVariableList(Site.InlinedLocals); |
Reid Kleckner | f9c275f | 2016-02-10 20:55:49 +0000 | [diff] [blame] | 805 | |
Reid Kleckner | f3b9ba4 | 2016-01-29 18:16:43 +0000 | [diff] [blame] | 806 | // Recurse on child inlined call sites before closing the scope. |
| 807 | for (const DILocation *ChildSite : Site.ChildSites) { |
| 808 | auto I = FI.InlineSites.find(ChildSite); |
| 809 | assert(I != FI.InlineSites.end() && |
| 810 | "child site not in function inline site map"); |
| 811 | emitInlinedCallSite(FI, ChildSite, I->second); |
| 812 | } |
| 813 | |
| 814 | // Close the scope. |
Reid Kleckner | dac21b4 | 2016-02-03 21:15:48 +0000 | [diff] [blame] | 815 | OS.AddComment("Record length"); |
| 816 | OS.EmitIntValue(2, 2); // RecordLength |
| 817 | OS.AddComment("Record kind: S_INLINESITE_END"); |
Zachary Turner | 63a2846 | 2016-05-17 23:50:21 +0000 | [diff] [blame] | 818 | OS.EmitIntValue(SymbolKind::S_INLINESITE_END, 2); // RecordKind |
Reid Kleckner | f3b9ba4 | 2016-01-29 18:16:43 +0000 | [diff] [blame] | 819 | } |
| 820 | |
Reid Kleckner | 5d122f8 | 2016-05-25 23:16:12 +0000 | [diff] [blame] | 821 | void CodeViewDebug::switchToDebugSectionForSymbol(const MCSymbol *GVSym) { |
| 822 | // If we have a symbol, it may be in a section that is COMDAT. If so, find the |
| 823 | // comdat key. A section may be comdat because of -ffunction-sections or |
| 824 | // because it is comdat in the IR. |
| 825 | MCSectionCOFF *GVSec = |
| 826 | GVSym ? dyn_cast<MCSectionCOFF>(&GVSym->getSection()) : nullptr; |
| 827 | const MCSymbol *KeySym = GVSec ? GVSec->getCOMDATSymbol() : nullptr; |
| 828 | |
| 829 | MCSectionCOFF *DebugSec = cast<MCSectionCOFF>( |
| 830 | Asm->getObjFileLowering().getCOFFDebugSymbolsSection()); |
| 831 | DebugSec = OS.getContext().getAssociativeCOFFSection(DebugSec, KeySym); |
| 832 | |
| 833 | OS.SwitchSection(DebugSec); |
| 834 | |
| 835 | // Emit the magic version number if this is the first time we've switched to |
| 836 | // this section. |
| 837 | if (ComdatDebugSections.insert(DebugSec).second) |
| 838 | emitCodeViewMagicVersion(); |
| 839 | } |
| 840 | |
Brock Wyma | 94ece8f | 2018-04-16 16:53:57 +0000 | [diff] [blame] | 841 | // Emit an S_THUNK32/S_END symbol pair for a thunk routine. |
| 842 | // The only supported thunk ordinal is currently the standard type. |
| 843 | void CodeViewDebug::emitDebugInfoForThunk(const Function *GV, |
| 844 | FunctionInfo &FI, |
| 845 | const MCSymbol *Fn) { |
| 846 | std::string FuncName = GlobalValue::dropLLVMManglingEscape(GV->getName()); |
| 847 | const ThunkOrdinal ordinal = ThunkOrdinal::Standard; // Only supported kind. |
| 848 | |
| 849 | OS.AddComment("Symbol subsection for " + Twine(FuncName)); |
| 850 | MCSymbol *SymbolsEnd = beginCVSubsection(DebugSubsectionKind::Symbols); |
| 851 | |
| 852 | // Emit S_THUNK32 |
| 853 | MCSymbol *ThunkRecordBegin = MMI->getContext().createTempSymbol(), |
| 854 | *ThunkRecordEnd = MMI->getContext().createTempSymbol(); |
| 855 | OS.AddComment("Record length"); |
| 856 | OS.emitAbsoluteSymbolDiff(ThunkRecordEnd, ThunkRecordBegin, 2); |
| 857 | OS.EmitLabel(ThunkRecordBegin); |
| 858 | OS.AddComment("Record kind: S_THUNK32"); |
| 859 | OS.EmitIntValue(unsigned(SymbolKind::S_THUNK32), 2); |
| 860 | OS.AddComment("PtrParent"); |
| 861 | OS.EmitIntValue(0, 4); |
| 862 | OS.AddComment("PtrEnd"); |
| 863 | OS.EmitIntValue(0, 4); |
| 864 | OS.AddComment("PtrNext"); |
| 865 | OS.EmitIntValue(0, 4); |
| 866 | OS.AddComment("Thunk section relative address"); |
| 867 | OS.EmitCOFFSecRel32(Fn, /*Offset=*/0); |
| 868 | OS.AddComment("Thunk section index"); |
| 869 | OS.EmitCOFFSectionIndex(Fn); |
| 870 | OS.AddComment("Code size"); |
| 871 | OS.emitAbsoluteSymbolDiff(FI.End, Fn, 2); |
| 872 | OS.AddComment("Ordinal"); |
| 873 | OS.EmitIntValue(unsigned(ordinal), 1); |
| 874 | OS.AddComment("Function name"); |
| 875 | emitNullTerminatedSymbolName(OS, FuncName); |
| 876 | // Additional fields specific to the thunk ordinal would go here. |
| 877 | OS.EmitLabel(ThunkRecordEnd); |
| 878 | |
| 879 | // Local variables/inlined routines are purposely omitted here. The point of |
| 880 | // marking this as a thunk is so Visual Studio will NOT stop in this routine. |
| 881 | |
| 882 | // Emit S_PROC_ID_END |
| 883 | const unsigned RecordLengthForSymbolEnd = 2; |
| 884 | OS.AddComment("Record length"); |
| 885 | OS.EmitIntValue(RecordLengthForSymbolEnd, 2); |
| 886 | OS.AddComment("Record kind: S_PROC_ID_END"); |
| 887 | OS.EmitIntValue(unsigned(SymbolKind::S_PROC_ID_END), 2); |
| 888 | |
| 889 | endCVSubsection(SymbolsEnd); |
| 890 | } |
| 891 | |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 892 | void CodeViewDebug::emitDebugInfoForFunction(const Function *GV, |
| 893 | FunctionInfo &FI) { |
Brock Wyma | 31cc1eb | 2018-01-30 13:16:50 +0000 | [diff] [blame] | 894 | // For each function there is a separate subsection which holds the PC to |
| 895 | // file:line table. |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 896 | const MCSymbol *Fn = Asm->getSymbol(GV); |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 897 | assert(Fn); |
Timur Iskhodzhanov | 8499a12 | 2014-03-26 09:50:36 +0000 | [diff] [blame] | 898 | |
Reid Kleckner | 5d122f8 | 2016-05-25 23:16:12 +0000 | [diff] [blame] | 899 | // Switch to the to a comdat section, if appropriate. |
| 900 | switchToDebugSectionForSymbol(Fn); |
| 901 | |
Reid Kleckner | ac945e2 | 2016-06-17 16:11:20 +0000 | [diff] [blame] | 902 | std::string FuncName; |
David Majnemer | 3128b10 | 2016-06-15 18:00:01 +0000 | [diff] [blame] | 903 | auto *SP = GV->getSubprogram(); |
David Majnemer | 67f684e | 2016-07-28 05:03:22 +0000 | [diff] [blame] | 904 | assert(SP); |
David Majnemer | 3128b10 | 2016-06-15 18:00:01 +0000 | [diff] [blame] | 905 | setCurrentSubprogram(SP); |
Reid Kleckner | ac945e2 | 2016-06-17 16:11:20 +0000 | [diff] [blame] | 906 | |
Brock Wyma | 94ece8f | 2018-04-16 16:53:57 +0000 | [diff] [blame] | 907 | if (SP->isThunk()) { |
| 908 | emitDebugInfoForThunk(GV, FI, Fn); |
| 909 | return; |
| 910 | } |
| 911 | |
Reid Kleckner | ac945e2 | 2016-06-17 16:11:20 +0000 | [diff] [blame] | 912 | // If we have a display name, build the fully qualified name by walking the |
| 913 | // chain of scopes. |
Adrian Prantl | 9d2f019 | 2017-04-26 23:59:52 +0000 | [diff] [blame] | 914 | if (!SP->getName().empty()) |
Reid Kleckner | 0c5d874 | 2016-06-22 01:32:56 +0000 | [diff] [blame] | 915 | FuncName = |
Adrian Prantl | 9d2f019 | 2017-04-26 23:59:52 +0000 | [diff] [blame] | 916 | getFullyQualifiedName(SP->getScope().resolve(), SP->getName()); |
Duncan P. N. Exon Smith | 23e56ec | 2015-03-20 19:50:00 +0000 | [diff] [blame] | 917 | |
Reid Kleckner | 3c0ff98 | 2016-01-14 00:12:54 +0000 | [diff] [blame] | 918 | // If our DISubprogram name is empty, use the mangled name. |
Reid Kleckner | 72e2ba7 | 2016-01-13 19:32:35 +0000 | [diff] [blame] | 919 | if (FuncName.empty()) |
Peter Collingbourne | 6f0ecca | 2017-05-16 00:39:01 +0000 | [diff] [blame] | 920 | FuncName = GlobalValue::dropLLVMManglingEscape(GV->getName()); |
Reid Kleckner | 3c0ff98 | 2016-01-14 00:12:54 +0000 | [diff] [blame] | 921 | |
Reid Kleckner | 9cdd4df | 2017-10-11 21:24:33 +0000 | [diff] [blame] | 922 | // Emit FPO data, but only on 32-bit x86. No other platforms use it. |
| 923 | if (Triple(MMI->getModule()->getTargetTriple()).getArch() == Triple::x86) |
| 924 | OS.EmitCVFPOData(Fn); |
| 925 | |
Timur Iskhodzhanov | 2bc90fd | 2014-10-24 01:27:45 +0000 | [diff] [blame] | 926 | // Emit a symbol subsection, required by VS2012+ to find function boundaries. |
Reid Kleckner | dac21b4 | 2016-02-03 21:15:48 +0000 | [diff] [blame] | 927 | OS.AddComment("Symbol subsection for " + Twine(FuncName)); |
Zachary Turner | 8c099fe | 2017-05-30 16:36:15 +0000 | [diff] [blame] | 928 | MCSymbol *SymbolsEnd = beginCVSubsection(DebugSubsectionKind::Symbols); |
Timur Iskhodzhanov | 2bc90fd | 2014-10-24 01:27:45 +0000 | [diff] [blame] | 929 | { |
Reid Kleckner | f9c275f | 2016-02-10 20:55:49 +0000 | [diff] [blame] | 930 | MCSymbol *ProcRecordBegin = MMI->getContext().createTempSymbol(), |
| 931 | *ProcRecordEnd = MMI->getContext().createTempSymbol(); |
Reid Kleckner | dac21b4 | 2016-02-03 21:15:48 +0000 | [diff] [blame] | 932 | OS.AddComment("Record length"); |
Reid Kleckner | eb3bcdd | 2016-02-03 21:24:42 +0000 | [diff] [blame] | 933 | OS.emitAbsoluteSymbolDiff(ProcRecordEnd, ProcRecordBegin, 2); |
Reid Kleckner | dac21b4 | 2016-02-03 21:15:48 +0000 | [diff] [blame] | 934 | OS.EmitLabel(ProcRecordBegin); |
Timur Iskhodzhanov | 2bc90fd | 2014-10-24 01:27:45 +0000 | [diff] [blame] | 935 | |
Reid Kleckner | 4ad127f | 2016-09-14 21:49:21 +0000 | [diff] [blame] | 936 | if (GV->hasLocalLinkage()) { |
| 937 | OS.AddComment("Record kind: S_LPROC32_ID"); |
| 938 | OS.EmitIntValue(unsigned(SymbolKind::S_LPROC32_ID), 2); |
| 939 | } else { |
| 940 | OS.AddComment("Record kind: S_GPROC32_ID"); |
| 941 | OS.EmitIntValue(unsigned(SymbolKind::S_GPROC32_ID), 2); |
| 942 | } |
Reid Kleckner | 6b3faef | 2016-01-13 23:44:57 +0000 | [diff] [blame] | 943 | |
David Majnemer | 30579ec | 2016-02-02 23:18:23 +0000 | [diff] [blame] | 944 | // These fields are filled in by tools like CVPACK which run after the fact. |
Reid Kleckner | dac21b4 | 2016-02-03 21:15:48 +0000 | [diff] [blame] | 945 | OS.AddComment("PtrParent"); |
| 946 | OS.EmitIntValue(0, 4); |
| 947 | OS.AddComment("PtrEnd"); |
| 948 | OS.EmitIntValue(0, 4); |
| 949 | OS.AddComment("PtrNext"); |
| 950 | OS.EmitIntValue(0, 4); |
Timur Iskhodzhanov | 2bc90fd | 2014-10-24 01:27:45 +0000 | [diff] [blame] | 951 | // This is the important bit that tells the debugger where the function |
| 952 | // code is located and what's its size: |
Reid Kleckner | dac21b4 | 2016-02-03 21:15:48 +0000 | [diff] [blame] | 953 | OS.AddComment("Code size"); |
Reid Kleckner | eb3bcdd | 2016-02-03 21:24:42 +0000 | [diff] [blame] | 954 | OS.emitAbsoluteSymbolDiff(FI.End, Fn, 4); |
Reid Kleckner | dac21b4 | 2016-02-03 21:15:48 +0000 | [diff] [blame] | 955 | OS.AddComment("Offset after prologue"); |
| 956 | OS.EmitIntValue(0, 4); |
| 957 | OS.AddComment("Offset before epilogue"); |
| 958 | OS.EmitIntValue(0, 4); |
| 959 | OS.AddComment("Function type index"); |
David Majnemer | 75c3ebf | 2016-06-02 17:13:53 +0000 | [diff] [blame] | 960 | OS.EmitIntValue(getFuncIdForSubprogram(GV->getSubprogram()).getIndex(), 4); |
Reid Kleckner | dac21b4 | 2016-02-03 21:15:48 +0000 | [diff] [blame] | 961 | OS.AddComment("Function section relative address"); |
Keno Fischer | f7d84ee | 2017-01-02 03:00:19 +0000 | [diff] [blame] | 962 | OS.EmitCOFFSecRel32(Fn, /*Offset=*/0); |
Reid Kleckner | dac21b4 | 2016-02-03 21:15:48 +0000 | [diff] [blame] | 963 | OS.AddComment("Function section index"); |
| 964 | OS.EmitCOFFSectionIndex(Fn); |
| 965 | OS.AddComment("Flags"); |
| 966 | OS.EmitIntValue(0, 1); |
Timur Iskhodzhanov | a11b32b | 2014-11-12 20:10:09 +0000 | [diff] [blame] | 967 | // Emit the function display name as a null-terminated string. |
Reid Kleckner | dac21b4 | 2016-02-03 21:15:48 +0000 | [diff] [blame] | 968 | OS.AddComment("Function name"); |
David Majnemer | 1256125 | 2016-03-13 10:53:30 +0000 | [diff] [blame] | 969 | // Truncate the name so we won't overflow the record length field. |
David Majnemer | b9456a5 | 2016-03-14 05:15:09 +0000 | [diff] [blame] | 970 | emitNullTerminatedSymbolName(OS, FuncName); |
Reid Kleckner | dac21b4 | 2016-02-03 21:15:48 +0000 | [diff] [blame] | 971 | OS.EmitLabel(ProcRecordEnd); |
Timur Iskhodzhanov | 2bc90fd | 2014-10-24 01:27:45 +0000 | [diff] [blame] | 972 | |
Reid Kleckner | 10dd55c | 2016-06-24 17:55:40 +0000 | [diff] [blame] | 973 | emitLocalVariableList(FI.Locals); |
Reid Kleckner | 5a791ee | 2018-03-15 21:24:04 +0000 | [diff] [blame] | 974 | emitLexicalBlockList(FI.ChildBlocks, FI); |
Reid Kleckner | f9c275f | 2016-02-10 20:55:49 +0000 | [diff] [blame] | 975 | |
Reid Kleckner | f3b9ba4 | 2016-01-29 18:16:43 +0000 | [diff] [blame] | 976 | // Emit inlined call site information. Only emit functions inlined directly |
| 977 | // into the parent function. We'll emit the other sites recursively as part |
| 978 | // of their parent inline site. |
Reid Kleckner | f9c275f | 2016-02-10 20:55:49 +0000 | [diff] [blame] | 979 | for (const DILocation *InlinedAt : FI.ChildSites) { |
| 980 | auto I = FI.InlineSites.find(InlinedAt); |
| 981 | assert(I != FI.InlineSites.end() && |
| 982 | "child site not in function inline site map"); |
| 983 | emitInlinedCallSite(FI, InlinedAt, I->second); |
Reid Kleckner | f3b9ba4 | 2016-01-29 18:16:43 +0000 | [diff] [blame] | 984 | } |
| 985 | |
Reid Kleckner | e33c94f | 2017-09-05 20:14:58 +0000 | [diff] [blame] | 986 | for (auto Annot : FI.Annotations) { |
| 987 | MCSymbol *Label = Annot.first; |
| 988 | MDTuple *Strs = cast<MDTuple>(Annot.second); |
| 989 | MCSymbol *AnnotBegin = MMI->getContext().createTempSymbol(), |
| 990 | *AnnotEnd = MMI->getContext().createTempSymbol(); |
| 991 | OS.AddComment("Record length"); |
| 992 | OS.emitAbsoluteSymbolDiff(AnnotEnd, AnnotBegin, 2); |
| 993 | OS.EmitLabel(AnnotBegin); |
| 994 | OS.AddComment("Record kind: S_ANNOTATION"); |
| 995 | OS.EmitIntValue(SymbolKind::S_ANNOTATION, 2); |
| 996 | OS.EmitCOFFSecRel32(Label, /*Offset=*/0); |
| 997 | // FIXME: Make sure we don't overflow the max record size. |
| 998 | OS.EmitCOFFSectionIndex(Label); |
| 999 | OS.EmitIntValue(Strs->getNumOperands(), 2); |
| 1000 | for (Metadata *MD : Strs->operands()) { |
| 1001 | // MDStrings are null terminated, so we can do EmitBytes and get the |
| 1002 | // nice .asciz directive. |
| 1003 | StringRef Str = cast<MDString>(MD)->getString(); |
| 1004 | assert(Str.data()[Str.size()] == '\0' && "non-nullterminated MDString"); |
| 1005 | OS.EmitBytes(StringRef(Str.data(), Str.size() + 1)); |
| 1006 | } |
| 1007 | OS.EmitLabel(AnnotEnd); |
| 1008 | } |
| 1009 | |
David Majnemer | 3128b10 | 2016-06-15 18:00:01 +0000 | [diff] [blame] | 1010 | if (SP != nullptr) |
| 1011 | emitDebugInfoForUDTs(LocalUDTs); |
| 1012 | |
Timur Iskhodzhanov | 2bc90fd | 2014-10-24 01:27:45 +0000 | [diff] [blame] | 1013 | // We're done with this function. |
Reid Kleckner | dac21b4 | 2016-02-03 21:15:48 +0000 | [diff] [blame] | 1014 | OS.AddComment("Record length"); |
| 1015 | OS.EmitIntValue(0x0002, 2); |
| 1016 | OS.AddComment("Record kind: S_PROC_ID_END"); |
Zachary Turner | 63a2846 | 2016-05-17 23:50:21 +0000 | [diff] [blame] | 1017 | OS.EmitIntValue(unsigned(SymbolKind::S_PROC_ID_END), 2); |
Timur Iskhodzhanov | 2bc90fd | 2014-10-24 01:27:45 +0000 | [diff] [blame] | 1018 | } |
Reid Kleckner | 6f3406d | 2016-06-07 00:02:03 +0000 | [diff] [blame] | 1019 | endCVSubsection(SymbolsEnd); |
Timur Iskhodzhanov | 2bc90fd | 2014-10-24 01:27:45 +0000 | [diff] [blame] | 1020 | |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 1021 | // We have an assembler directive that takes care of the whole line table. |
Reid Kleckner | dac21b4 | 2016-02-03 21:15:48 +0000 | [diff] [blame] | 1022 | OS.EmitCVLinetableDirective(FI.FuncId, Fn, FI.End); |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 1023 | } |
| 1024 | |
Reid Kleckner | 876330d | 2016-02-12 21:48:30 +0000 | [diff] [blame] | 1025 | CodeViewDebug::LocalVarDefRange |
| 1026 | CodeViewDebug::createDefRangeMem(uint16_t CVRegister, int Offset) { |
| 1027 | LocalVarDefRange DR; |
Aaron Ballman | c6a2f21 | 2016-02-16 15:35:51 +0000 | [diff] [blame] | 1028 | DR.InMemory = -1; |
Reid Kleckner | 876330d | 2016-02-12 21:48:30 +0000 | [diff] [blame] | 1029 | DR.DataOffset = Offset; |
| 1030 | assert(DR.DataOffset == Offset && "truncation"); |
Reid Kleckner | 2b3e642 | 2016-10-05 21:21:33 +0000 | [diff] [blame] | 1031 | DR.IsSubfield = 0; |
Reid Kleckner | 876330d | 2016-02-12 21:48:30 +0000 | [diff] [blame] | 1032 | DR.StructOffset = 0; |
| 1033 | DR.CVRegister = CVRegister; |
| 1034 | return DR; |
| 1035 | } |
| 1036 | |
Matthias Braun | ef331ef | 2016-11-30 23:48:50 +0000 | [diff] [blame] | 1037 | void CodeViewDebug::collectVariableInfoFromMFTable( |
Hsiangkai Wang | 760c1ab | 2018-09-06 02:22:06 +0000 | [diff] [blame] | 1038 | DenseSet<InlinedEntity> &Processed) { |
Matthias Braun | ef331ef | 2016-11-30 23:48:50 +0000 | [diff] [blame] | 1039 | const MachineFunction &MF = *Asm->MF; |
| 1040 | const TargetSubtargetInfo &TSI = MF.getSubtarget(); |
Reid Kleckner | 876330d | 2016-02-12 21:48:30 +0000 | [diff] [blame] | 1041 | const TargetFrameLowering *TFI = TSI.getFrameLowering(); |
| 1042 | const TargetRegisterInfo *TRI = TSI.getRegisterInfo(); |
| 1043 | |
Matthias Braun | ef331ef | 2016-11-30 23:48:50 +0000 | [diff] [blame] | 1044 | for (const MachineFunction::VariableDbgInfo &VI : MF.getVariableDbgInfo()) { |
Reid Kleckner | f9c275f | 2016-02-10 20:55:49 +0000 | [diff] [blame] | 1045 | if (!VI.Var) |
| 1046 | continue; |
| 1047 | assert(VI.Var->isValidLocationForIntrinsic(VI.Loc) && |
| 1048 | "Expected inlined-at fields to agree"); |
| 1049 | |
Hsiangkai Wang | 760c1ab | 2018-09-06 02:22:06 +0000 | [diff] [blame] | 1050 | Processed.insert(InlinedEntity(VI.Var, VI.Loc->getInlinedAt())); |
Reid Kleckner | f9c275f | 2016-02-10 20:55:49 +0000 | [diff] [blame] | 1051 | LexicalScope *Scope = LScopes.findLexicalScope(VI.Loc); |
| 1052 | |
| 1053 | // If variable scope is not found then skip this variable. |
| 1054 | if (!Scope) |
| 1055 | continue; |
| 1056 | |
Reid Kleckner | b5fced7 | 2017-05-09 19:59:29 +0000 | [diff] [blame] | 1057 | // If the variable has an attached offset expression, extract it. |
| 1058 | // FIXME: Try to handle DW_OP_deref as well. |
| 1059 | int64_t ExprOffset = 0; |
| 1060 | if (VI.Expr) |
| 1061 | if (!VI.Expr->extractIfOffset(ExprOffset)) |
| 1062 | continue; |
| 1063 | |
Reid Kleckner | f9c275f | 2016-02-10 20:55:49 +0000 | [diff] [blame] | 1064 | // Get the frame register used and the offset. |
| 1065 | unsigned FrameReg = 0; |
Reid Kleckner | 876330d | 2016-02-12 21:48:30 +0000 | [diff] [blame] | 1066 | int FrameOffset = TFI->getFrameIndexReference(*Asm->MF, VI.Slot, FrameReg); |
| 1067 | uint16_t CVReg = TRI->getCodeViewRegNum(FrameReg); |
Reid Kleckner | f9c275f | 2016-02-10 20:55:49 +0000 | [diff] [blame] | 1068 | |
| 1069 | // Calculate the label ranges. |
Reid Kleckner | b5fced7 | 2017-05-09 19:59:29 +0000 | [diff] [blame] | 1070 | LocalVarDefRange DefRange = |
| 1071 | createDefRangeMem(CVReg, FrameOffset + ExprOffset); |
Reid Kleckner | f9c275f | 2016-02-10 20:55:49 +0000 | [diff] [blame] | 1072 | for (const InsnRange &Range : Scope->getRanges()) { |
| 1073 | const MCSymbol *Begin = getLabelBeforeInsn(Range.first); |
| 1074 | const MCSymbol *End = getLabelAfterInsn(Range.second); |
Reid Kleckner | 876330d | 2016-02-12 21:48:30 +0000 | [diff] [blame] | 1075 | End = End ? End : Asm->getFunctionEnd(); |
| 1076 | DefRange.Ranges.emplace_back(Begin, End); |
Reid Kleckner | f9c275f | 2016-02-10 20:55:49 +0000 | [diff] [blame] | 1077 | } |
| 1078 | |
Reid Kleckner | 876330d | 2016-02-12 21:48:30 +0000 | [diff] [blame] | 1079 | LocalVariable Var; |
| 1080 | Var.DIVar = VI.Var; |
| 1081 | Var.DefRanges.emplace_back(std::move(DefRange)); |
Reid Kleckner | 5a791ee | 2018-03-15 21:24:04 +0000 | [diff] [blame] | 1082 | recordLocalVariable(std::move(Var), Scope); |
Reid Kleckner | 876330d | 2016-02-12 21:48:30 +0000 | [diff] [blame] | 1083 | } |
| 1084 | } |
| 1085 | |
Reid Kleckner | 08f5fd5 | 2017-08-31 15:56:49 +0000 | [diff] [blame] | 1086 | static bool canUseReferenceType(const DbgVariableLocation &Loc) { |
| 1087 | return !Loc.LoadChain.empty() && Loc.LoadChain.back() == 0; |
| 1088 | } |
| 1089 | |
| 1090 | static bool needsReferenceType(const DbgVariableLocation &Loc) { |
| 1091 | return Loc.LoadChain.size() == 2 && Loc.LoadChain.back() == 0; |
| 1092 | } |
| 1093 | |
Bob Haarman | 223303c | 2017-08-29 20:59:25 +0000 | [diff] [blame] | 1094 | void CodeViewDebug::calculateRanges( |
| 1095 | LocalVariable &Var, const DbgValueHistoryMap::InstrRanges &Ranges) { |
| 1096 | const TargetRegisterInfo *TRI = Asm->MF->getSubtarget().getRegisterInfo(); |
| 1097 | |
Reid Kleckner | 08f5fd5 | 2017-08-31 15:56:49 +0000 | [diff] [blame] | 1098 | // Calculate the definition ranges. |
Bob Haarman | 223303c | 2017-08-29 20:59:25 +0000 | [diff] [blame] | 1099 | for (auto I = Ranges.begin(), E = Ranges.end(); I != E; ++I) { |
| 1100 | const InsnRange &Range = *I; |
| 1101 | const MachineInstr *DVInst = Range.first; |
| 1102 | assert(DVInst->isDebugValue() && "Invalid History entry"); |
| 1103 | // FIXME: Find a way to represent constant variables, since they are |
| 1104 | // relatively common. |
Bob Haarman | 1a4cbbe | 2017-08-30 17:50:21 +0000 | [diff] [blame] | 1105 | Optional<DbgVariableLocation> Location = |
| 1106 | DbgVariableLocation::extractFromMachineInstruction(*DVInst); |
| 1107 | if (!Location) |
Bob Haarman | a88bce1 | 2017-08-29 21:01:55 +0000 | [diff] [blame] | 1108 | continue; |
Bob Haarman | 223303c | 2017-08-29 20:59:25 +0000 | [diff] [blame] | 1109 | |
Reid Kleckner | 08f5fd5 | 2017-08-31 15:56:49 +0000 | [diff] [blame] | 1110 | // CodeView can only express variables in register and variables in memory |
| 1111 | // at a constant offset from a register. However, for variables passed |
| 1112 | // indirectly by pointer, it is common for that pointer to be spilled to a |
| 1113 | // stack location. For the special case of one offseted load followed by a |
| 1114 | // zero offset load (a pointer spilled to the stack), we change the type of |
| 1115 | // the local variable from a value type to a reference type. This tricks the |
| 1116 | // debugger into doing the load for us. |
| 1117 | if (Var.UseReferenceType) { |
| 1118 | // We're using a reference type. Drop the last zero offset load. |
| 1119 | if (canUseReferenceType(*Location)) |
| 1120 | Location->LoadChain.pop_back(); |
| 1121 | else |
| 1122 | continue; |
| 1123 | } else if (needsReferenceType(*Location)) { |
| 1124 | // This location can't be expressed without switching to a reference type. |
| 1125 | // Start over using that. |
| 1126 | Var.UseReferenceType = true; |
Bob Haarman | 223303c | 2017-08-29 20:59:25 +0000 | [diff] [blame] | 1127 | Var.DefRanges.clear(); |
| 1128 | calculateRanges(Var, Ranges); |
| 1129 | return; |
| 1130 | } |
| 1131 | |
Reid Kleckner | 08f5fd5 | 2017-08-31 15:56:49 +0000 | [diff] [blame] | 1132 | // We can only handle a register or an offseted load of a register. |
| 1133 | if (Location->Register == 0 || Location->LoadChain.size() > 1) |
Bob Haarman | 223303c | 2017-08-29 20:59:25 +0000 | [diff] [blame] | 1134 | continue; |
Bob Haarman | 223303c | 2017-08-29 20:59:25 +0000 | [diff] [blame] | 1135 | { |
| 1136 | LocalVarDefRange DR; |
Bob Haarman | 1a4cbbe | 2017-08-30 17:50:21 +0000 | [diff] [blame] | 1137 | DR.CVRegister = TRI->getCodeViewRegNum(Location->Register); |
Reid Kleckner | 08f5fd5 | 2017-08-31 15:56:49 +0000 | [diff] [blame] | 1138 | DR.InMemory = !Location->LoadChain.empty(); |
| 1139 | DR.DataOffset = |
| 1140 | !Location->LoadChain.empty() ? Location->LoadChain.back() : 0; |
Bob Haarman | 1a4cbbe | 2017-08-30 17:50:21 +0000 | [diff] [blame] | 1141 | if (Location->FragmentInfo) { |
Bob Haarman | 223303c | 2017-08-29 20:59:25 +0000 | [diff] [blame] | 1142 | DR.IsSubfield = true; |
Bob Haarman | 1a4cbbe | 2017-08-30 17:50:21 +0000 | [diff] [blame] | 1143 | DR.StructOffset = Location->FragmentInfo->OffsetInBits / 8; |
Bob Haarman | 223303c | 2017-08-29 20:59:25 +0000 | [diff] [blame] | 1144 | } else { |
| 1145 | DR.IsSubfield = false; |
| 1146 | DR.StructOffset = 0; |
| 1147 | } |
| 1148 | |
| 1149 | if (Var.DefRanges.empty() || |
| 1150 | Var.DefRanges.back().isDifferentLocation(DR)) { |
| 1151 | Var.DefRanges.emplace_back(std::move(DR)); |
| 1152 | } |
| 1153 | } |
| 1154 | |
| 1155 | // Compute the label range. |
| 1156 | const MCSymbol *Begin = getLabelBeforeInsn(Range.first); |
| 1157 | const MCSymbol *End = getLabelAfterInsn(Range.second); |
| 1158 | if (!End) { |
| 1159 | // This range is valid until the next overlapping bitpiece. In the |
| 1160 | // common case, ranges will not be bitpieces, so they will overlap. |
| 1161 | auto J = std::next(I); |
| 1162 | const DIExpression *DIExpr = DVInst->getDebugExpression(); |
| 1163 | while (J != E && |
Bjorn Pettersson | a223f815 | 2018-03-12 18:02:39 +0000 | [diff] [blame] | 1164 | !DIExpr->fragmentsOverlap(J->first->getDebugExpression())) |
Bob Haarman | 223303c | 2017-08-29 20:59:25 +0000 | [diff] [blame] | 1165 | ++J; |
| 1166 | if (J != E) |
| 1167 | End = getLabelBeforeInsn(J->first); |
| 1168 | else |
| 1169 | End = Asm->getFunctionEnd(); |
| 1170 | } |
| 1171 | |
| 1172 | // If the last range end is our begin, just extend the last range. |
| 1173 | // Otherwise make a new range. |
| 1174 | SmallVectorImpl<std::pair<const MCSymbol *, const MCSymbol *>> &R = |
| 1175 | Var.DefRanges.back().Ranges; |
| 1176 | if (!R.empty() && R.back().second == Begin) |
| 1177 | R.back().second = End; |
| 1178 | else |
| 1179 | R.emplace_back(Begin, End); |
| 1180 | |
| 1181 | // FIXME: Do more range combining. |
| 1182 | } |
| 1183 | } |
| 1184 | |
Reid Kleckner | 876330d | 2016-02-12 21:48:30 +0000 | [diff] [blame] | 1185 | void CodeViewDebug::collectVariableInfo(const DISubprogram *SP) { |
Hsiangkai Wang | 760c1ab | 2018-09-06 02:22:06 +0000 | [diff] [blame] | 1186 | DenseSet<InlinedEntity> Processed; |
Reid Kleckner | 876330d | 2016-02-12 21:48:30 +0000 | [diff] [blame] | 1187 | // Grab the variable info that was squirreled away in the MMI side-table. |
Matthias Braun | ef331ef | 2016-11-30 23:48:50 +0000 | [diff] [blame] | 1188 | collectVariableInfoFromMFTable(Processed); |
Reid Kleckner | 876330d | 2016-02-12 21:48:30 +0000 | [diff] [blame] | 1189 | |
Reid Kleckner | 876330d | 2016-02-12 21:48:30 +0000 | [diff] [blame] | 1190 | for (const auto &I : DbgValues) { |
Hsiangkai Wang | 760c1ab | 2018-09-06 02:22:06 +0000 | [diff] [blame] | 1191 | InlinedEntity IV = I.first; |
Reid Kleckner | 876330d | 2016-02-12 21:48:30 +0000 | [diff] [blame] | 1192 | if (Processed.count(IV)) |
| 1193 | continue; |
Hsiangkai Wang | 760c1ab | 2018-09-06 02:22:06 +0000 | [diff] [blame] | 1194 | const DILocalVariable *DIVar = cast<DILocalVariable>(IV.first); |
Reid Kleckner | 876330d | 2016-02-12 21:48:30 +0000 | [diff] [blame] | 1195 | const DILocation *InlinedAt = IV.second; |
| 1196 | |
| 1197 | // Instruction ranges, specifying where IV is accessible. |
| 1198 | const auto &Ranges = I.second; |
| 1199 | |
| 1200 | LexicalScope *Scope = nullptr; |
| 1201 | if (InlinedAt) |
| 1202 | Scope = LScopes.findInlinedScope(DIVar->getScope(), InlinedAt); |
| 1203 | else |
| 1204 | Scope = LScopes.findLexicalScope(DIVar->getScope()); |
| 1205 | // If variable scope is not found then skip this variable. |
| 1206 | if (!Scope) |
| 1207 | continue; |
| 1208 | |
| 1209 | LocalVariable Var; |
| 1210 | Var.DIVar = DIVar; |
| 1211 | |
Bob Haarman | 223303c | 2017-08-29 20:59:25 +0000 | [diff] [blame] | 1212 | calculateRanges(Var, Ranges); |
Reid Kleckner | 5a791ee | 2018-03-15 21:24:04 +0000 | [diff] [blame] | 1213 | recordLocalVariable(std::move(Var), Scope); |
Reid Kleckner | f9c275f | 2016-02-10 20:55:49 +0000 | [diff] [blame] | 1214 | } |
| 1215 | } |
| 1216 | |
David Blaikie | b2fbb4b | 2017-02-16 18:48:33 +0000 | [diff] [blame] | 1217 | void CodeViewDebug::beginFunctionImpl(const MachineFunction *MF) { |
Matthias Braun | f1caa28 | 2017-12-15 22:22:58 +0000 | [diff] [blame] | 1218 | const Function &GV = MF->getFunction(); |
Reid Kleckner | 55baeef | 2018-03-15 21:12:21 +0000 | [diff] [blame] | 1219 | auto Insertion = FnDebugInfo.insert({&GV, llvm::make_unique<FunctionInfo>()}); |
Reid Kleckner | e9dc30d | 2018-03-15 21:18:42 +0000 | [diff] [blame] | 1220 | assert(Insertion.second && "function already has info"); |
Reid Kleckner | 55baeef | 2018-03-15 21:12:21 +0000 | [diff] [blame] | 1221 | CurFn = Insertion.first->second.get(); |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 1222 | CurFn->FuncId = NextFuncId++; |
Reid Kleckner | 1fcd610 | 2016-02-02 17:41:18 +0000 | [diff] [blame] | 1223 | CurFn->Begin = Asm->getFunctionBegin(); |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 1224 | |
Reid Kleckner | a9f4cc9 | 2016-09-07 16:15:31 +0000 | [diff] [blame] | 1225 | OS.EmitCVFuncIdDirective(CurFn->FuncId); |
| 1226 | |
Reid Kleckner | f9c275f | 2016-02-10 20:55:49 +0000 | [diff] [blame] | 1227 | // Find the end of the function prolog. First known non-DBG_VALUE and |
| 1228 | // non-frame setup location marks the beginning of the function body. |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 1229 | // FIXME: is there a simpler a way to do this? Can we just search |
| 1230 | // for the first instruction of the function, not the last of the prolog? |
| 1231 | DebugLoc PrologEndLoc; |
| 1232 | bool EmptyPrologue = true; |
Alexey Samsonov | f74bde6 | 2014-04-30 22:17:38 +0000 | [diff] [blame] | 1233 | for (const auto &MBB : *MF) { |
Alexey Samsonov | f74bde6 | 2014-04-30 22:17:38 +0000 | [diff] [blame] | 1234 | for (const auto &MI : MBB) { |
Adrian Prantl | fb31da1 | 2017-05-22 20:47:09 +0000 | [diff] [blame] | 1235 | if (!MI.isMetaInstruction() && !MI.getFlag(MachineInstr::FrameSetup) && |
Reid Kleckner | f9c275f | 2016-02-10 20:55:49 +0000 | [diff] [blame] | 1236 | MI.getDebugLoc()) { |
Alexey Samsonov | f74bde6 | 2014-04-30 22:17:38 +0000 | [diff] [blame] | 1237 | PrologEndLoc = MI.getDebugLoc(); |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 1238 | break; |
Adrian Prantl | fb31da1 | 2017-05-22 20:47:09 +0000 | [diff] [blame] | 1239 | } else if (!MI.isMetaInstruction()) { |
Reid Kleckner | f9c275f | 2016-02-10 20:55:49 +0000 | [diff] [blame] | 1240 | EmptyPrologue = false; |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 1241 | } |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 1242 | } |
| 1243 | } |
Reid Kleckner | f9c275f | 2016-02-10 20:55:49 +0000 | [diff] [blame] | 1244 | |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 1245 | // Record beginning of function if we have a non-empty prologue. |
Duncan P. N. Exon Smith | 9dffcd0 | 2015-03-30 19:14:47 +0000 | [diff] [blame] | 1246 | if (PrologEndLoc && !EmptyPrologue) { |
| 1247 | DebugLoc FnStartDL = PrologEndLoc.getFnDebugLoc(); |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 1248 | maybeRecordLocation(FnStartDL, MF); |
| 1249 | } |
| 1250 | } |
| 1251 | |
Zachary Turner | a7b0417 | 2017-08-28 18:49:04 +0000 | [diff] [blame] | 1252 | static bool shouldEmitUdt(const DIType *T) { |
Zachary Turner | 37c7474 | 2017-09-05 22:06:39 +0000 | [diff] [blame] | 1253 | if (!T) |
| 1254 | return false; |
| 1255 | |
| 1256 | // MSVC does not emit UDTs for typedefs that are scoped to classes. |
| 1257 | if (T->getTag() == dwarf::DW_TAG_typedef) { |
| 1258 | if (DIScope *Scope = T->getScope().resolve()) { |
| 1259 | switch (Scope->getTag()) { |
| 1260 | case dwarf::DW_TAG_structure_type: |
| 1261 | case dwarf::DW_TAG_class_type: |
| 1262 | case dwarf::DW_TAG_union_type: |
| 1263 | return false; |
| 1264 | } |
| 1265 | } |
| 1266 | } |
| 1267 | |
Zachary Turner | a7b0417 | 2017-08-28 18:49:04 +0000 | [diff] [blame] | 1268 | while (true) { |
| 1269 | if (!T || T->isForwardDecl()) |
| 1270 | return false; |
| 1271 | |
| 1272 | const DIDerivedType *DT = dyn_cast<DIDerivedType>(T); |
| 1273 | if (!DT) |
| 1274 | return true; |
| 1275 | T = DT->getBaseType().resolve(); |
| 1276 | } |
| 1277 | return true; |
| 1278 | } |
| 1279 | |
| 1280 | void CodeViewDebug::addToUDTs(const DIType *Ty) { |
Reid Kleckner | ad56ea3 | 2016-07-01 22:24:51 +0000 | [diff] [blame] | 1281 | // Don't record empty UDTs. |
| 1282 | if (Ty->getName().empty()) |
| 1283 | return; |
Zachary Turner | a7b0417 | 2017-08-28 18:49:04 +0000 | [diff] [blame] | 1284 | if (!shouldEmitUdt(Ty)) |
| 1285 | return; |
Reid Kleckner | ad56ea3 | 2016-07-01 22:24:51 +0000 | [diff] [blame] | 1286 | |
Hans Wennborg | 4b63a98 | 2016-06-23 22:57:25 +0000 | [diff] [blame] | 1287 | SmallVector<StringRef, 5> QualifiedNameComponents; |
| 1288 | const DISubprogram *ClosestSubprogram = getQualifiedNameComponents( |
| 1289 | Ty->getScope().resolve(), QualifiedNameComponents); |
| 1290 | |
| 1291 | std::string FullyQualifiedName = |
David Majnemer | 6bdc24e | 2016-07-01 23:12:45 +0000 | [diff] [blame] | 1292 | getQualifiedName(QualifiedNameComponents, getPrettyScopeName(Ty)); |
Hans Wennborg | 4b63a98 | 2016-06-23 22:57:25 +0000 | [diff] [blame] | 1293 | |
Zachary Turner | a7b0417 | 2017-08-28 18:49:04 +0000 | [diff] [blame] | 1294 | if (ClosestSubprogram == nullptr) { |
| 1295 | GlobalUDTs.emplace_back(std::move(FullyQualifiedName), Ty); |
| 1296 | } else if (ClosestSubprogram == CurrentSubprogram) { |
| 1297 | LocalUDTs.emplace_back(std::move(FullyQualifiedName), Ty); |
| 1298 | } |
Hans Wennborg | 4b63a98 | 2016-06-23 22:57:25 +0000 | [diff] [blame] | 1299 | |
| 1300 | // TODO: What if the ClosestSubprogram is neither null or the current |
| 1301 | // subprogram? Currently, the UDT just gets dropped on the floor. |
| 1302 | // |
| 1303 | // The current behavior is not desirable. To get maximal fidelity, we would |
| 1304 | // need to perform all type translation before beginning emission of .debug$S |
| 1305 | // and then make LocalUDTs a member of FunctionInfo |
| 1306 | } |
| 1307 | |
Amjad Aboud | 76c9eb9 | 2016-06-18 10:25:07 +0000 | [diff] [blame] | 1308 | TypeIndex CodeViewDebug::lowerType(const DIType *Ty, const DIType *ClassTy) { |
Reid Kleckner | 5acacbb | 2016-06-01 17:05:51 +0000 | [diff] [blame] | 1309 | // Generic dispatch for lowering an unknown type. |
| 1310 | switch (Ty->getTag()) { |
Adrian McCarthy | f3c3c13 | 2016-06-08 18:22:59 +0000 | [diff] [blame] | 1311 | case dwarf::DW_TAG_array_type: |
| 1312 | return lowerTypeArray(cast<DICompositeType>(Ty)); |
David Majnemer | d065e23 | 2016-06-02 06:21:37 +0000 | [diff] [blame] | 1313 | case dwarf::DW_TAG_typedef: |
| 1314 | return lowerTypeAlias(cast<DIDerivedType>(Ty)); |
Reid Kleckner | 5acacbb | 2016-06-01 17:05:51 +0000 | [diff] [blame] | 1315 | case dwarf::DW_TAG_base_type: |
| 1316 | return lowerTypeBasic(cast<DIBasicType>(Ty)); |
| 1317 | case dwarf::DW_TAG_pointer_type: |
Reid Kleckner | 9dac473 | 2016-08-31 15:59:30 +0000 | [diff] [blame] | 1318 | if (cast<DIDerivedType>(Ty)->getName() == "__vtbl_ptr_type") |
| 1319 | return lowerTypeVFTableShape(cast<DIDerivedType>(Ty)); |
| 1320 | LLVM_FALLTHROUGH; |
Reid Kleckner | 5acacbb | 2016-06-01 17:05:51 +0000 | [diff] [blame] | 1321 | case dwarf::DW_TAG_reference_type: |
| 1322 | case dwarf::DW_TAG_rvalue_reference_type: |
| 1323 | return lowerTypePointer(cast<DIDerivedType>(Ty)); |
| 1324 | case dwarf::DW_TAG_ptr_to_member_type: |
| 1325 | return lowerTypeMemberPointer(cast<DIDerivedType>(Ty)); |
Reid Kleckner | 3acdc67 | 2018-02-27 22:08:15 +0000 | [diff] [blame] | 1326 | case dwarf::DW_TAG_restrict_type: |
Reid Kleckner | 5acacbb | 2016-06-01 17:05:51 +0000 | [diff] [blame] | 1327 | case dwarf::DW_TAG_const_type: |
| 1328 | case dwarf::DW_TAG_volatile_type: |
Victor Leschuk | e1156c2 | 2016-10-31 19:09:38 +0000 | [diff] [blame] | 1329 | // TODO: add support for DW_TAG_atomic_type here |
Reid Kleckner | 5acacbb | 2016-06-01 17:05:51 +0000 | [diff] [blame] | 1330 | return lowerTypeModifier(cast<DIDerivedType>(Ty)); |
David Majnemer | 75c3ebf | 2016-06-02 17:13:53 +0000 | [diff] [blame] | 1331 | case dwarf::DW_TAG_subroutine_type: |
Reid Kleckner | 0c5d874 | 2016-06-22 01:32:56 +0000 | [diff] [blame] | 1332 | if (ClassTy) { |
| 1333 | // The member function type of a member function pointer has no |
| 1334 | // ThisAdjustment. |
| 1335 | return lowerTypeMemberFunction(cast<DISubroutineType>(Ty), ClassTy, |
Adrian McCarthy | d91bf39 | 2017-09-13 20:53:55 +0000 | [diff] [blame] | 1336 | /*ThisAdjustment=*/0, |
| 1337 | /*IsStaticMethod=*/false); |
Reid Kleckner | 0c5d874 | 2016-06-22 01:32:56 +0000 | [diff] [blame] | 1338 | } |
David Majnemer | 75c3ebf | 2016-06-02 17:13:53 +0000 | [diff] [blame] | 1339 | return lowerTypeFunction(cast<DISubroutineType>(Ty)); |
David Majnemer | 979cb88 | 2016-06-16 21:32:16 +0000 | [diff] [blame] | 1340 | case dwarf::DW_TAG_enumeration_type: |
| 1341 | return lowerTypeEnum(cast<DICompositeType>(Ty)); |
Reid Kleckner | a8d5740 | 2016-06-03 15:58:20 +0000 | [diff] [blame] | 1342 | case dwarf::DW_TAG_class_type: |
| 1343 | case dwarf::DW_TAG_structure_type: |
| 1344 | return lowerTypeClass(cast<DICompositeType>(Ty)); |
| 1345 | case dwarf::DW_TAG_union_type: |
| 1346 | return lowerTypeUnion(cast<DICompositeType>(Ty)); |
Aaron Smith | a73fa2a | 2018-01-11 06:42:11 +0000 | [diff] [blame] | 1347 | case dwarf::DW_TAG_unspecified_type: |
| 1348 | return TypeIndex::None(); |
Reid Kleckner | 5acacbb | 2016-06-01 17:05:51 +0000 | [diff] [blame] | 1349 | default: |
| 1350 | // Use the null type index. |
| 1351 | return TypeIndex(); |
| 1352 | } |
| 1353 | } |
| 1354 | |
David Majnemer | d065e23 | 2016-06-02 06:21:37 +0000 | [diff] [blame] | 1355 | TypeIndex CodeViewDebug::lowerTypeAlias(const DIDerivedType *Ty) { |
David Majnemer | d065e23 | 2016-06-02 06:21:37 +0000 | [diff] [blame] | 1356 | DITypeRef UnderlyingTypeRef = Ty->getBaseType(); |
| 1357 | TypeIndex UnderlyingTypeIndex = getTypeIndex(UnderlyingTypeRef); |
David Majnemer | 3128b10 | 2016-06-15 18:00:01 +0000 | [diff] [blame] | 1358 | StringRef TypeName = Ty->getName(); |
| 1359 | |
Zachary Turner | a7b0417 | 2017-08-28 18:49:04 +0000 | [diff] [blame] | 1360 | addToUDTs(Ty); |
David Majnemer | 3128b10 | 2016-06-15 18:00:01 +0000 | [diff] [blame] | 1361 | |
David Majnemer | d065e23 | 2016-06-02 06:21:37 +0000 | [diff] [blame] | 1362 | if (UnderlyingTypeIndex == TypeIndex(SimpleTypeKind::Int32Long) && |
David Majnemer | 3128b10 | 2016-06-15 18:00:01 +0000 | [diff] [blame] | 1363 | TypeName == "HRESULT") |
David Majnemer | d065e23 | 2016-06-02 06:21:37 +0000 | [diff] [blame] | 1364 | return TypeIndex(SimpleTypeKind::HResult); |
David Majnemer | 8c46a4c | 2016-06-04 15:40:33 +0000 | [diff] [blame] | 1365 | if (UnderlyingTypeIndex == TypeIndex(SimpleTypeKind::UInt16Short) && |
David Majnemer | 3128b10 | 2016-06-15 18:00:01 +0000 | [diff] [blame] | 1366 | TypeName == "wchar_t") |
David Majnemer | 8c46a4c | 2016-06-04 15:40:33 +0000 | [diff] [blame] | 1367 | return TypeIndex(SimpleTypeKind::WideCharacter); |
Hans Wennborg | 4b63a98 | 2016-06-23 22:57:25 +0000 | [diff] [blame] | 1368 | |
David Majnemer | d065e23 | 2016-06-02 06:21:37 +0000 | [diff] [blame] | 1369 | return UnderlyingTypeIndex; |
| 1370 | } |
| 1371 | |
Adrian McCarthy | f3c3c13 | 2016-06-08 18:22:59 +0000 | [diff] [blame] | 1372 | TypeIndex CodeViewDebug::lowerTypeArray(const DICompositeType *Ty) { |
| 1373 | DITypeRef ElementTypeRef = Ty->getBaseType(); |
| 1374 | TypeIndex ElementTypeIndex = getTypeIndex(ElementTypeRef); |
| 1375 | // IndexType is size_t, which depends on the bitness of the target. |
Matt Arsenault | 41e5ac4 | 2018-03-14 00:36:23 +0000 | [diff] [blame] | 1376 | TypeIndex IndexType = getPointerSizeInBytes() == 8 |
Adrian McCarthy | f3c3c13 | 2016-06-08 18:22:59 +0000 | [diff] [blame] | 1377 | ? TypeIndex(SimpleTypeKind::UInt64Quad) |
| 1378 | : TypeIndex(SimpleTypeKind::UInt32Long); |
Amjad Aboud | acee568 | 2016-07-12 12:06:34 +0000 | [diff] [blame] | 1379 | |
| 1380 | uint64_t ElementSize = getBaseTypeSize(ElementTypeRef) / 8; |
| 1381 | |
Amjad Aboud | acee568 | 2016-07-12 12:06:34 +0000 | [diff] [blame] | 1382 | // Add subranges to array type. |
| 1383 | DINodeArray Elements = Ty->getElements(); |
| 1384 | for (int i = Elements.size() - 1; i >= 0; --i) { |
| 1385 | const DINode *Element = Elements[i]; |
| 1386 | assert(Element->getTag() == dwarf::DW_TAG_subrange_type); |
| 1387 | |
| 1388 | const DISubrange *Subrange = cast<DISubrange>(Element); |
| 1389 | assert(Subrange->getLowerBound() == 0 && |
| 1390 | "codeview doesn't support subranges with lower bounds"); |
Sander de Smalen | fdf4091 | 2018-01-24 09:56:07 +0000 | [diff] [blame] | 1391 | int64_t Count = -1; |
| 1392 | if (auto *CI = Subrange->getCount().dyn_cast<ConstantInt*>()) |
| 1393 | Count = CI->getSExtValue(); |
Amjad Aboud | acee568 | 2016-07-12 12:06:34 +0000 | [diff] [blame] | 1394 | |
Reid Kleckner | cd7bba0 | 2017-09-13 23:30:01 +0000 | [diff] [blame] | 1395 | // Forward declarations of arrays without a size and VLAs use a count of -1. |
| 1396 | // Emit a count of zero in these cases to match what MSVC does for arrays |
| 1397 | // without a size. MSVC doesn't support VLAs, so it's not clear what we |
| 1398 | // should do for them even if we could distinguish them. |
Reid Kleckner | 6b78e16 | 2017-03-24 23:28:42 +0000 | [diff] [blame] | 1399 | if (Count == -1) |
Reid Kleckner | 89af112 | 2017-09-13 21:54:20 +0000 | [diff] [blame] | 1400 | Count = 0; |
Amjad Aboud | acee568 | 2016-07-12 12:06:34 +0000 | [diff] [blame] | 1401 | |
Amjad Aboud | acee568 | 2016-07-12 12:06:34 +0000 | [diff] [blame] | 1402 | // Update the element size and element type index for subsequent subranges. |
| 1403 | ElementSize *= Count; |
Reid Kleckner | 1076288 | 2016-09-09 17:29:36 +0000 | [diff] [blame] | 1404 | |
| 1405 | // If this is the outermost array, use the size from the array. It will be |
Reid Kleckner | 6b78e16 | 2017-03-24 23:28:42 +0000 | [diff] [blame] | 1406 | // more accurate if we had a VLA or an incomplete element type size. |
Reid Kleckner | 1076288 | 2016-09-09 17:29:36 +0000 | [diff] [blame] | 1407 | uint64_t ArraySize = |
| 1408 | (i == 0 && ElementSize == 0) ? Ty->getSizeInBits() / 8 : ElementSize; |
| 1409 | |
| 1410 | StringRef Name = (i == 0) ? Ty->getName() : ""; |
Zachary Turner | 4efa0a4 | 2016-11-08 22:24:53 +0000 | [diff] [blame] | 1411 | ArrayRecord AR(ElementTypeIndex, IndexType, ArraySize, Name); |
Zachary Turner | 6900de1 | 2017-11-28 18:33:17 +0000 | [diff] [blame] | 1412 | ElementTypeIndex = TypeTable.writeLeafType(AR); |
Amjad Aboud | acee568 | 2016-07-12 12:06:34 +0000 | [diff] [blame] | 1413 | } |
| 1414 | |
Amjad Aboud | acee568 | 2016-07-12 12:06:34 +0000 | [diff] [blame] | 1415 | return ElementTypeIndex; |
Adrian McCarthy | f3c3c13 | 2016-06-08 18:22:59 +0000 | [diff] [blame] | 1416 | } |
| 1417 | |
Reid Kleckner | 5acacbb | 2016-06-01 17:05:51 +0000 | [diff] [blame] | 1418 | TypeIndex CodeViewDebug::lowerTypeBasic(const DIBasicType *Ty) { |
| 1419 | TypeIndex Index; |
| 1420 | dwarf::TypeKind Kind; |
| 1421 | uint32_t ByteSize; |
| 1422 | |
| 1423 | Kind = static_cast<dwarf::TypeKind>(Ty->getEncoding()); |
David Majnemer | afefa67 | 2016-06-02 06:21:42 +0000 | [diff] [blame] | 1424 | ByteSize = Ty->getSizeInBits() / 8; |
Reid Kleckner | 5acacbb | 2016-06-01 17:05:51 +0000 | [diff] [blame] | 1425 | |
| 1426 | SimpleTypeKind STK = SimpleTypeKind::None; |
| 1427 | switch (Kind) { |
| 1428 | case dwarf::DW_ATE_address: |
| 1429 | // FIXME: Translate |
| 1430 | break; |
| 1431 | case dwarf::DW_ATE_boolean: |
| 1432 | switch (ByteSize) { |
David Majnemer | 1c2cb1d | 2016-06-02 07:02:32 +0000 | [diff] [blame] | 1433 | case 1: STK = SimpleTypeKind::Boolean8; break; |
| 1434 | case 2: STK = SimpleTypeKind::Boolean16; break; |
| 1435 | case 4: STK = SimpleTypeKind::Boolean32; break; |
| 1436 | case 8: STK = SimpleTypeKind::Boolean64; break; |
| 1437 | case 16: STK = SimpleTypeKind::Boolean128; break; |
Reid Kleckner | 5acacbb | 2016-06-01 17:05:51 +0000 | [diff] [blame] | 1438 | } |
| 1439 | break; |
| 1440 | case dwarf::DW_ATE_complex_float: |
| 1441 | switch (ByteSize) { |
David Majnemer | 1c2cb1d | 2016-06-02 07:02:32 +0000 | [diff] [blame] | 1442 | case 2: STK = SimpleTypeKind::Complex16; break; |
Reid Kleckner | 5acacbb | 2016-06-01 17:05:51 +0000 | [diff] [blame] | 1443 | case 4: STK = SimpleTypeKind::Complex32; break; |
| 1444 | case 8: STK = SimpleTypeKind::Complex64; break; |
| 1445 | case 10: STK = SimpleTypeKind::Complex80; break; |
| 1446 | case 16: STK = SimpleTypeKind::Complex128; break; |
| 1447 | } |
| 1448 | break; |
| 1449 | case dwarf::DW_ATE_float: |
| 1450 | switch (ByteSize) { |
David Majnemer | 1c2cb1d | 2016-06-02 07:02:32 +0000 | [diff] [blame] | 1451 | case 2: STK = SimpleTypeKind::Float16; break; |
Reid Kleckner | 5acacbb | 2016-06-01 17:05:51 +0000 | [diff] [blame] | 1452 | case 4: STK = SimpleTypeKind::Float32; break; |
| 1453 | case 6: STK = SimpleTypeKind::Float48; break; |
| 1454 | case 8: STK = SimpleTypeKind::Float64; break; |
| 1455 | case 10: STK = SimpleTypeKind::Float80; break; |
| 1456 | case 16: STK = SimpleTypeKind::Float128; break; |
| 1457 | } |
| 1458 | break; |
| 1459 | case dwarf::DW_ATE_signed: |
| 1460 | switch (ByteSize) { |
Reid Kleckner | e45b2c7 | 2016-09-29 17:55:01 +0000 | [diff] [blame] | 1461 | case 1: STK = SimpleTypeKind::SignedCharacter; break; |
| 1462 | case 2: STK = SimpleTypeKind::Int16Short; break; |
| 1463 | case 4: STK = SimpleTypeKind::Int32; break; |
| 1464 | case 8: STK = SimpleTypeKind::Int64Quad; break; |
| 1465 | case 16: STK = SimpleTypeKind::Int128Oct; break; |
Reid Kleckner | 5acacbb | 2016-06-01 17:05:51 +0000 | [diff] [blame] | 1466 | } |
| 1467 | break; |
| 1468 | case dwarf::DW_ATE_unsigned: |
| 1469 | switch (ByteSize) { |
Reid Kleckner | e45b2c7 | 2016-09-29 17:55:01 +0000 | [diff] [blame] | 1470 | case 1: STK = SimpleTypeKind::UnsignedCharacter; break; |
| 1471 | case 2: STK = SimpleTypeKind::UInt16Short; break; |
| 1472 | case 4: STK = SimpleTypeKind::UInt32; break; |
| 1473 | case 8: STK = SimpleTypeKind::UInt64Quad; break; |
| 1474 | case 16: STK = SimpleTypeKind::UInt128Oct; break; |
Reid Kleckner | 5acacbb | 2016-06-01 17:05:51 +0000 | [diff] [blame] | 1475 | } |
| 1476 | break; |
| 1477 | case dwarf::DW_ATE_UTF: |
| 1478 | switch (ByteSize) { |
| 1479 | case 2: STK = SimpleTypeKind::Character16; break; |
| 1480 | case 4: STK = SimpleTypeKind::Character32; break; |
| 1481 | } |
| 1482 | break; |
| 1483 | case dwarf::DW_ATE_signed_char: |
| 1484 | if (ByteSize == 1) |
| 1485 | STK = SimpleTypeKind::SignedCharacter; |
| 1486 | break; |
| 1487 | case dwarf::DW_ATE_unsigned_char: |
| 1488 | if (ByteSize == 1) |
| 1489 | STK = SimpleTypeKind::UnsignedCharacter; |
| 1490 | break; |
| 1491 | default: |
| 1492 | break; |
| 1493 | } |
| 1494 | |
| 1495 | // Apply some fixups based on the source-level type name. |
| 1496 | if (STK == SimpleTypeKind::Int32 && Ty->getName() == "long int") |
| 1497 | STK = SimpleTypeKind::Int32Long; |
| 1498 | if (STK == SimpleTypeKind::UInt32 && Ty->getName() == "long unsigned int") |
| 1499 | STK = SimpleTypeKind::UInt32Long; |
David Majnemer | 8c46a4c | 2016-06-04 15:40:33 +0000 | [diff] [blame] | 1500 | if (STK == SimpleTypeKind::UInt16Short && |
| 1501 | (Ty->getName() == "wchar_t" || Ty->getName() == "__wchar_t")) |
Reid Kleckner | 5acacbb | 2016-06-01 17:05:51 +0000 | [diff] [blame] | 1502 | STK = SimpleTypeKind::WideCharacter; |
| 1503 | if ((STK == SimpleTypeKind::SignedCharacter || |
| 1504 | STK == SimpleTypeKind::UnsignedCharacter) && |
| 1505 | Ty->getName() == "char") |
| 1506 | STK = SimpleTypeKind::NarrowCharacter; |
| 1507 | |
| 1508 | return TypeIndex(STK); |
| 1509 | } |
| 1510 | |
Reid Kleckner | 3acdc67 | 2018-02-27 22:08:15 +0000 | [diff] [blame] | 1511 | TypeIndex CodeViewDebug::lowerTypePointer(const DIDerivedType *Ty, |
| 1512 | PointerOptions PO) { |
Reid Kleckner | 5acacbb | 2016-06-01 17:05:51 +0000 | [diff] [blame] | 1513 | TypeIndex PointeeTI = getTypeIndex(Ty->getBaseType()); |
| 1514 | |
Reid Kleckner | 3acdc67 | 2018-02-27 22:08:15 +0000 | [diff] [blame] | 1515 | // Pointers to simple types without any options can use SimpleTypeMode, rather |
| 1516 | // than having a dedicated pointer type record. |
| 1517 | if (PointeeTI.isSimple() && PO == PointerOptions::None && |
Reid Kleckner | 5acacbb | 2016-06-01 17:05:51 +0000 | [diff] [blame] | 1518 | PointeeTI.getSimpleMode() == SimpleTypeMode::Direct && |
| 1519 | Ty->getTag() == dwarf::DW_TAG_pointer_type) { |
| 1520 | SimpleTypeMode Mode = Ty->getSizeInBits() == 64 |
| 1521 | ? SimpleTypeMode::NearPointer64 |
| 1522 | : SimpleTypeMode::NearPointer32; |
| 1523 | return TypeIndex(PointeeTI.getSimpleKind(), Mode); |
| 1524 | } |
| 1525 | |
| 1526 | PointerKind PK = |
| 1527 | Ty->getSizeInBits() == 64 ? PointerKind::Near64 : PointerKind::Near32; |
| 1528 | PointerMode PM = PointerMode::Pointer; |
| 1529 | switch (Ty->getTag()) { |
| 1530 | default: llvm_unreachable("not a pointer tag type"); |
| 1531 | case dwarf::DW_TAG_pointer_type: |
| 1532 | PM = PointerMode::Pointer; |
| 1533 | break; |
| 1534 | case dwarf::DW_TAG_reference_type: |
| 1535 | PM = PointerMode::LValueReference; |
| 1536 | break; |
| 1537 | case dwarf::DW_TAG_rvalue_reference_type: |
| 1538 | PM = PointerMode::RValueReference; |
| 1539 | break; |
| 1540 | } |
Reid Kleckner | 3acdc67 | 2018-02-27 22:08:15 +0000 | [diff] [blame] | 1541 | |
Reid Kleckner | 5acacbb | 2016-06-01 17:05:51 +0000 | [diff] [blame] | 1542 | PointerRecord PR(PointeeTI, PK, PM, PO, Ty->getSizeInBits() / 8); |
Zachary Turner | 6900de1 | 2017-11-28 18:33:17 +0000 | [diff] [blame] | 1543 | return TypeTable.writeLeafType(PR); |
Reid Kleckner | 5acacbb | 2016-06-01 17:05:51 +0000 | [diff] [blame] | 1544 | } |
| 1545 | |
Reid Kleckner | 6fa1546 | 2016-06-17 22:14:39 +0000 | [diff] [blame] | 1546 | static PointerToMemberRepresentation |
| 1547 | translatePtrToMemberRep(unsigned SizeInBytes, bool IsPMF, unsigned Flags) { |
| 1548 | // SizeInBytes being zero generally implies that the member pointer type was |
| 1549 | // incomplete, which can happen if it is part of a function prototype. In this |
| 1550 | // case, use the unknown model instead of the general model. |
Reid Kleckner | 604105b | 2016-06-17 21:31:33 +0000 | [diff] [blame] | 1551 | if (IsPMF) { |
| 1552 | switch (Flags & DINode::FlagPtrToMemberRep) { |
| 1553 | case 0: |
Reid Kleckner | 6fa1546 | 2016-06-17 22:14:39 +0000 | [diff] [blame] | 1554 | return SizeInBytes == 0 ? PointerToMemberRepresentation::Unknown |
| 1555 | : PointerToMemberRepresentation::GeneralFunction; |
Reid Kleckner | 604105b | 2016-06-17 21:31:33 +0000 | [diff] [blame] | 1556 | case DINode::FlagSingleInheritance: |
| 1557 | return PointerToMemberRepresentation::SingleInheritanceFunction; |
| 1558 | case DINode::FlagMultipleInheritance: |
| 1559 | return PointerToMemberRepresentation::MultipleInheritanceFunction; |
| 1560 | case DINode::FlagVirtualInheritance: |
| 1561 | return PointerToMemberRepresentation::VirtualInheritanceFunction; |
| 1562 | } |
| 1563 | } else { |
| 1564 | switch (Flags & DINode::FlagPtrToMemberRep) { |
| 1565 | case 0: |
Reid Kleckner | 6fa1546 | 2016-06-17 22:14:39 +0000 | [diff] [blame] | 1566 | return SizeInBytes == 0 ? PointerToMemberRepresentation::Unknown |
| 1567 | : PointerToMemberRepresentation::GeneralData; |
Reid Kleckner | 604105b | 2016-06-17 21:31:33 +0000 | [diff] [blame] | 1568 | case DINode::FlagSingleInheritance: |
| 1569 | return PointerToMemberRepresentation::SingleInheritanceData; |
| 1570 | case DINode::FlagMultipleInheritance: |
| 1571 | return PointerToMemberRepresentation::MultipleInheritanceData; |
| 1572 | case DINode::FlagVirtualInheritance: |
| 1573 | return PointerToMemberRepresentation::VirtualInheritanceData; |
| 1574 | } |
| 1575 | } |
| 1576 | llvm_unreachable("invalid ptr to member representation"); |
| 1577 | } |
| 1578 | |
Reid Kleckner | 3acdc67 | 2018-02-27 22:08:15 +0000 | [diff] [blame] | 1579 | TypeIndex CodeViewDebug::lowerTypeMemberPointer(const DIDerivedType *Ty, |
| 1580 | PointerOptions PO) { |
Reid Kleckner | 5acacbb | 2016-06-01 17:05:51 +0000 | [diff] [blame] | 1581 | assert(Ty->getTag() == dwarf::DW_TAG_ptr_to_member_type); |
| 1582 | TypeIndex ClassTI = getTypeIndex(Ty->getClassType()); |
Amjad Aboud | 76c9eb9 | 2016-06-18 10:25:07 +0000 | [diff] [blame] | 1583 | TypeIndex PointeeTI = getTypeIndex(Ty->getBaseType(), Ty->getClassType()); |
Matt Arsenault | 41e5ac4 | 2018-03-14 00:36:23 +0000 | [diff] [blame] | 1584 | PointerKind PK = getPointerSizeInBytes() == 8 ? PointerKind::Near64 |
| 1585 | : PointerKind::Near32; |
Reid Kleckner | 604105b | 2016-06-17 21:31:33 +0000 | [diff] [blame] | 1586 | bool IsPMF = isa<DISubroutineType>(Ty->getBaseType()); |
| 1587 | PointerMode PM = IsPMF ? PointerMode::PointerToMemberFunction |
| 1588 | : PointerMode::PointerToDataMember; |
Reid Kleckner | 3acdc67 | 2018-02-27 22:08:15 +0000 | [diff] [blame] | 1589 | |
Reid Kleckner | 6fa1546 | 2016-06-17 22:14:39 +0000 | [diff] [blame] | 1590 | assert(Ty->getSizeInBits() / 8 <= 0xff && "pointer size too big"); |
| 1591 | uint8_t SizeInBytes = Ty->getSizeInBits() / 8; |
| 1592 | MemberPointerInfo MPI( |
| 1593 | ClassTI, translatePtrToMemberRep(SizeInBytes, IsPMF, Ty->getFlags())); |
Reid Kleckner | 604105b | 2016-06-17 21:31:33 +0000 | [diff] [blame] | 1594 | PointerRecord PR(PointeeTI, PK, PM, PO, SizeInBytes, MPI); |
Zachary Turner | 6900de1 | 2017-11-28 18:33:17 +0000 | [diff] [blame] | 1595 | return TypeTable.writeLeafType(PR); |
Reid Kleckner | 5acacbb | 2016-06-01 17:05:51 +0000 | [diff] [blame] | 1596 | } |
| 1597 | |
Reid Kleckner | de3d8b5 | 2016-06-08 20:34:29 +0000 | [diff] [blame] | 1598 | /// Given a DWARF calling convention, get the CodeView equivalent. If we don't |
| 1599 | /// have a translation, use the NearC convention. |
| 1600 | static CallingConvention dwarfCCToCodeView(unsigned DwarfCC) { |
| 1601 | switch (DwarfCC) { |
| 1602 | case dwarf::DW_CC_normal: return CallingConvention::NearC; |
| 1603 | case dwarf::DW_CC_BORLAND_msfastcall: return CallingConvention::NearFast; |
| 1604 | case dwarf::DW_CC_BORLAND_thiscall: return CallingConvention::ThisCall; |
| 1605 | case dwarf::DW_CC_BORLAND_stdcall: return CallingConvention::NearStdCall; |
| 1606 | case dwarf::DW_CC_BORLAND_pascal: return CallingConvention::NearPascal; |
| 1607 | case dwarf::DW_CC_LLVM_vectorcall: return CallingConvention::NearVector; |
| 1608 | } |
| 1609 | return CallingConvention::NearC; |
| 1610 | } |
| 1611 | |
Reid Kleckner | 5acacbb | 2016-06-01 17:05:51 +0000 | [diff] [blame] | 1612 | TypeIndex CodeViewDebug::lowerTypeModifier(const DIDerivedType *Ty) { |
| 1613 | ModifierOptions Mods = ModifierOptions::None; |
Reid Kleckner | 3acdc67 | 2018-02-27 22:08:15 +0000 | [diff] [blame] | 1614 | PointerOptions PO = PointerOptions::None; |
Reid Kleckner | 5acacbb | 2016-06-01 17:05:51 +0000 | [diff] [blame] | 1615 | bool IsModifier = true; |
| 1616 | const DIType *BaseTy = Ty; |
Reid Kleckner | b9c80fd | 2016-06-02 17:40:51 +0000 | [diff] [blame] | 1617 | while (IsModifier && BaseTy) { |
Victor Leschuk | e1156c2 | 2016-10-31 19:09:38 +0000 | [diff] [blame] | 1618 | // FIXME: Need to add DWARF tags for __unaligned and _Atomic |
Reid Kleckner | 5acacbb | 2016-06-01 17:05:51 +0000 | [diff] [blame] | 1619 | switch (BaseTy->getTag()) { |
| 1620 | case dwarf::DW_TAG_const_type: |
| 1621 | Mods |= ModifierOptions::Const; |
Reid Kleckner | 3acdc67 | 2018-02-27 22:08:15 +0000 | [diff] [blame] | 1622 | PO |= PointerOptions::Const; |
Reid Kleckner | 5acacbb | 2016-06-01 17:05:51 +0000 | [diff] [blame] | 1623 | break; |
| 1624 | case dwarf::DW_TAG_volatile_type: |
| 1625 | Mods |= ModifierOptions::Volatile; |
Reid Kleckner | 3acdc67 | 2018-02-27 22:08:15 +0000 | [diff] [blame] | 1626 | PO |= PointerOptions::Volatile; |
| 1627 | break; |
| 1628 | case dwarf::DW_TAG_restrict_type: |
| 1629 | // Only pointer types be marked with __restrict. There is no known flag |
| 1630 | // for __restrict in LF_MODIFIER records. |
| 1631 | PO |= PointerOptions::Restrict; |
Reid Kleckner | 5acacbb | 2016-06-01 17:05:51 +0000 | [diff] [blame] | 1632 | break; |
| 1633 | default: |
| 1634 | IsModifier = false; |
| 1635 | break; |
| 1636 | } |
| 1637 | if (IsModifier) |
| 1638 | BaseTy = cast<DIDerivedType>(BaseTy)->getBaseType().resolve(); |
| 1639 | } |
Reid Kleckner | 3acdc67 | 2018-02-27 22:08:15 +0000 | [diff] [blame] | 1640 | |
| 1641 | // Check if the inner type will use an LF_POINTER record. If so, the |
| 1642 | // qualifiers will go in the LF_POINTER record. This comes up for types like |
| 1643 | // 'int *const' and 'int *__restrict', not the more common cases like 'const |
| 1644 | // char *'. |
| 1645 | if (BaseTy) { |
| 1646 | switch (BaseTy->getTag()) { |
| 1647 | case dwarf::DW_TAG_pointer_type: |
| 1648 | case dwarf::DW_TAG_reference_type: |
| 1649 | case dwarf::DW_TAG_rvalue_reference_type: |
| 1650 | return lowerTypePointer(cast<DIDerivedType>(BaseTy), PO); |
| 1651 | case dwarf::DW_TAG_ptr_to_member_type: |
| 1652 | return lowerTypeMemberPointer(cast<DIDerivedType>(BaseTy), PO); |
| 1653 | default: |
| 1654 | break; |
| 1655 | } |
| 1656 | } |
| 1657 | |
Reid Kleckner | 5acacbb | 2016-06-01 17:05:51 +0000 | [diff] [blame] | 1658 | TypeIndex ModifiedTI = getTypeIndex(BaseTy); |
Reid Kleckner | 3acdc67 | 2018-02-27 22:08:15 +0000 | [diff] [blame] | 1659 | |
| 1660 | // Return the base type index if there aren't any modifiers. For example, the |
| 1661 | // metadata could contain restrict wrappers around non-pointer types. |
| 1662 | if (Mods == ModifierOptions::None) |
| 1663 | return ModifiedTI; |
| 1664 | |
Zachary Turner | 4efa0a4 | 2016-11-08 22:24:53 +0000 | [diff] [blame] | 1665 | ModifierRecord MR(ModifiedTI, Mods); |
Zachary Turner | 6900de1 | 2017-11-28 18:33:17 +0000 | [diff] [blame] | 1666 | return TypeTable.writeLeafType(MR); |
Reid Kleckner | 5acacbb | 2016-06-01 17:05:51 +0000 | [diff] [blame] | 1667 | } |
| 1668 | |
David Majnemer | 75c3ebf | 2016-06-02 17:13:53 +0000 | [diff] [blame] | 1669 | TypeIndex CodeViewDebug::lowerTypeFunction(const DISubroutineType *Ty) { |
| 1670 | SmallVector<TypeIndex, 8> ReturnAndArgTypeIndices; |
| 1671 | for (DITypeRef ArgTypeRef : Ty->getTypeArray()) |
| 1672 | ReturnAndArgTypeIndices.push_back(getTypeIndex(ArgTypeRef)); |
| 1673 | |
Aaron Smith | a73fa2a | 2018-01-11 06:42:11 +0000 | [diff] [blame] | 1674 | // MSVC uses type none for variadic argument. |
| 1675 | if (ReturnAndArgTypeIndices.size() > 1 && |
| 1676 | ReturnAndArgTypeIndices.back() == TypeIndex::Void()) { |
| 1677 | ReturnAndArgTypeIndices.back() = TypeIndex::None(); |
| 1678 | } |
David Majnemer | 75c3ebf | 2016-06-02 17:13:53 +0000 | [diff] [blame] | 1679 | TypeIndex ReturnTypeIndex = TypeIndex::Void(); |
| 1680 | ArrayRef<TypeIndex> ArgTypeIndices = None; |
| 1681 | if (!ReturnAndArgTypeIndices.empty()) { |
| 1682 | auto ReturnAndArgTypesRef = makeArrayRef(ReturnAndArgTypeIndices); |
| 1683 | ReturnTypeIndex = ReturnAndArgTypesRef.front(); |
| 1684 | ArgTypeIndices = ReturnAndArgTypesRef.drop_front(); |
| 1685 | } |
| 1686 | |
| 1687 | ArgListRecord ArgListRec(TypeRecordKind::ArgList, ArgTypeIndices); |
Zachary Turner | 6900de1 | 2017-11-28 18:33:17 +0000 | [diff] [blame] | 1688 | TypeIndex ArgListIndex = TypeTable.writeLeafType(ArgListRec); |
David Majnemer | 75c3ebf | 2016-06-02 17:13:53 +0000 | [diff] [blame] | 1689 | |
Reid Kleckner | de3d8b5 | 2016-06-08 20:34:29 +0000 | [diff] [blame] | 1690 | CallingConvention CC = dwarfCCToCodeView(Ty->getCC()); |
| 1691 | |
Reid Kleckner | de3d8b5 | 2016-06-08 20:34:29 +0000 | [diff] [blame] | 1692 | ProcedureRecord Procedure(ReturnTypeIndex, CC, FunctionOptions::None, |
| 1693 | ArgTypeIndices.size(), ArgListIndex); |
Zachary Turner | 6900de1 | 2017-11-28 18:33:17 +0000 | [diff] [blame] | 1694 | return TypeTable.writeLeafType(Procedure); |
David Majnemer | 75c3ebf | 2016-06-02 17:13:53 +0000 | [diff] [blame] | 1695 | } |
| 1696 | |
Amjad Aboud | 76c9eb9 | 2016-06-18 10:25:07 +0000 | [diff] [blame] | 1697 | TypeIndex CodeViewDebug::lowerTypeMemberFunction(const DISubroutineType *Ty, |
Reid Kleckner | 0c5d874 | 2016-06-22 01:32:56 +0000 | [diff] [blame] | 1698 | const DIType *ClassTy, |
Adrian McCarthy | d91bf39 | 2017-09-13 20:53:55 +0000 | [diff] [blame] | 1699 | int ThisAdjustment, |
| 1700 | bool IsStaticMethod) { |
Amjad Aboud | 76c9eb9 | 2016-06-18 10:25:07 +0000 | [diff] [blame] | 1701 | // Lower the containing class type. |
| 1702 | TypeIndex ClassType = getTypeIndex(ClassTy); |
| 1703 | |
Amjad Aboud | 76c9eb9 | 2016-06-18 10:25:07 +0000 | [diff] [blame] | 1704 | SmallVector<TypeIndex, 8> ReturnAndArgTypeIndices; |
| 1705 | for (DITypeRef ArgTypeRef : Ty->getTypeArray()) |
| 1706 | ReturnAndArgTypeIndices.push_back(getTypeIndex(ArgTypeRef)); |
| 1707 | |
Aaron Smith | a73fa2a | 2018-01-11 06:42:11 +0000 | [diff] [blame] | 1708 | // MSVC uses type none for variadic argument. |
| 1709 | if (ReturnAndArgTypeIndices.size() > 1 && |
| 1710 | ReturnAndArgTypeIndices.back() == TypeIndex::Void()) { |
| 1711 | ReturnAndArgTypeIndices.back() = TypeIndex::None(); |
| 1712 | } |
Amjad Aboud | 76c9eb9 | 2016-06-18 10:25:07 +0000 | [diff] [blame] | 1713 | TypeIndex ReturnTypeIndex = TypeIndex::Void(); |
| 1714 | ArrayRef<TypeIndex> ArgTypeIndices = None; |
| 1715 | if (!ReturnAndArgTypeIndices.empty()) { |
| 1716 | auto ReturnAndArgTypesRef = makeArrayRef(ReturnAndArgTypeIndices); |
| 1717 | ReturnTypeIndex = ReturnAndArgTypesRef.front(); |
| 1718 | ArgTypeIndices = ReturnAndArgTypesRef.drop_front(); |
| 1719 | } |
Reid Kleckner | 87288b9 | 2017-09-15 00:59:07 +0000 | [diff] [blame] | 1720 | TypeIndex ThisTypeIndex; |
Adrian McCarthy | d91bf39 | 2017-09-13 20:53:55 +0000 | [diff] [blame] | 1721 | if (!IsStaticMethod && !ArgTypeIndices.empty()) { |
Amjad Aboud | 76c9eb9 | 2016-06-18 10:25:07 +0000 | [diff] [blame] | 1722 | ThisTypeIndex = ArgTypeIndices.front(); |
| 1723 | ArgTypeIndices = ArgTypeIndices.drop_front(); |
| 1724 | } |
| 1725 | |
| 1726 | ArgListRecord ArgListRec(TypeRecordKind::ArgList, ArgTypeIndices); |
Zachary Turner | 6900de1 | 2017-11-28 18:33:17 +0000 | [diff] [blame] | 1727 | TypeIndex ArgListIndex = TypeTable.writeLeafType(ArgListRec); |
Amjad Aboud | 76c9eb9 | 2016-06-18 10:25:07 +0000 | [diff] [blame] | 1728 | |
| 1729 | CallingConvention CC = dwarfCCToCodeView(Ty->getCC()); |
| 1730 | |
Adrian McCarthy | d91bf39 | 2017-09-13 20:53:55 +0000 | [diff] [blame] | 1731 | // TODO: Need to use the correct values for FunctionOptions. |
Zachary Turner | 4efa0a4 | 2016-11-08 22:24:53 +0000 | [diff] [blame] | 1732 | MemberFunctionRecord MFR(ReturnTypeIndex, ClassType, ThisTypeIndex, CC, |
| 1733 | FunctionOptions::None, ArgTypeIndices.size(), |
| 1734 | ArgListIndex, ThisAdjustment); |
Zachary Turner | 6900de1 | 2017-11-28 18:33:17 +0000 | [diff] [blame] | 1735 | return TypeTable.writeLeafType(MFR); |
Amjad Aboud | 76c9eb9 | 2016-06-18 10:25:07 +0000 | [diff] [blame] | 1736 | } |
| 1737 | |
Reid Kleckner | 9dac473 | 2016-08-31 15:59:30 +0000 | [diff] [blame] | 1738 | TypeIndex CodeViewDebug::lowerTypeVFTableShape(const DIDerivedType *Ty) { |
Konstantin Zhuravlyov | dc77b2e | 2017-04-17 17:41:25 +0000 | [diff] [blame] | 1739 | unsigned VSlotCount = |
| 1740 | Ty->getSizeInBits() / (8 * Asm->MAI->getCodePointerSize()); |
Reid Kleckner | 9dac473 | 2016-08-31 15:59:30 +0000 | [diff] [blame] | 1741 | SmallVector<VFTableSlotKind, 4> Slots(VSlotCount, VFTableSlotKind::Near); |
Zachary Turner | 4efa0a4 | 2016-11-08 22:24:53 +0000 | [diff] [blame] | 1742 | |
| 1743 | VFTableShapeRecord VFTSR(Slots); |
Zachary Turner | 6900de1 | 2017-11-28 18:33:17 +0000 | [diff] [blame] | 1744 | return TypeTable.writeLeafType(VFTSR); |
Reid Kleckner | 9dac473 | 2016-08-31 15:59:30 +0000 | [diff] [blame] | 1745 | } |
| 1746 | |
Amjad Aboud | 76c9eb9 | 2016-06-18 10:25:07 +0000 | [diff] [blame] | 1747 | static MemberAccess translateAccessFlags(unsigned RecordTag, unsigned Flags) { |
| 1748 | switch (Flags & DINode::FlagAccessibility) { |
Reid Kleckner | a8d5740 | 2016-06-03 15:58:20 +0000 | [diff] [blame] | 1749 | case DINode::FlagPrivate: return MemberAccess::Private; |
| 1750 | case DINode::FlagPublic: return MemberAccess::Public; |
| 1751 | case DINode::FlagProtected: return MemberAccess::Protected; |
| 1752 | case 0: |
| 1753 | // If there was no explicit access control, provide the default for the tag. |
| 1754 | return RecordTag == dwarf::DW_TAG_class_type ? MemberAccess::Private |
| 1755 | : MemberAccess::Public; |
| 1756 | } |
| 1757 | llvm_unreachable("access flags are exclusive"); |
| 1758 | } |
| 1759 | |
Amjad Aboud | 76c9eb9 | 2016-06-18 10:25:07 +0000 | [diff] [blame] | 1760 | static MethodOptions translateMethodOptionFlags(const DISubprogram *SP) { |
| 1761 | if (SP->isArtificial()) |
| 1762 | return MethodOptions::CompilerGenerated; |
| 1763 | |
| 1764 | // FIXME: Handle other MethodOptions. |
| 1765 | |
| 1766 | return MethodOptions::None; |
| 1767 | } |
| 1768 | |
| 1769 | static MethodKind translateMethodKindFlags(const DISubprogram *SP, |
| 1770 | bool Introduced) { |
Adrian McCarthy | d91bf39 | 2017-09-13 20:53:55 +0000 | [diff] [blame] | 1771 | if (SP->getFlags() & DINode::FlagStaticMember) |
| 1772 | return MethodKind::Static; |
| 1773 | |
Amjad Aboud | 76c9eb9 | 2016-06-18 10:25:07 +0000 | [diff] [blame] | 1774 | switch (SP->getVirtuality()) { |
| 1775 | case dwarf::DW_VIRTUALITY_none: |
| 1776 | break; |
| 1777 | case dwarf::DW_VIRTUALITY_virtual: |
| 1778 | return Introduced ? MethodKind::IntroducingVirtual : MethodKind::Virtual; |
| 1779 | case dwarf::DW_VIRTUALITY_pure_virtual: |
| 1780 | return Introduced ? MethodKind::PureIntroducingVirtual |
| 1781 | : MethodKind::PureVirtual; |
| 1782 | default: |
| 1783 | llvm_unreachable("unhandled virtuality case"); |
| 1784 | } |
| 1785 | |
Amjad Aboud | 76c9eb9 | 2016-06-18 10:25:07 +0000 | [diff] [blame] | 1786 | return MethodKind::Vanilla; |
| 1787 | } |
| 1788 | |
Reid Kleckner | a8d5740 | 2016-06-03 15:58:20 +0000 | [diff] [blame] | 1789 | static TypeRecordKind getRecordKind(const DICompositeType *Ty) { |
| 1790 | switch (Ty->getTag()) { |
| 1791 | case dwarf::DW_TAG_class_type: return TypeRecordKind::Class; |
| 1792 | case dwarf::DW_TAG_structure_type: return TypeRecordKind::Struct; |
| 1793 | } |
| 1794 | llvm_unreachable("unexpected tag"); |
| 1795 | } |
| 1796 | |
Reid Kleckner | e092dad | 2016-07-02 00:11:07 +0000 | [diff] [blame] | 1797 | /// Return ClassOptions that should be present on both the forward declaration |
| 1798 | /// and the defintion of a tag type. |
| 1799 | static ClassOptions getCommonClassOptions(const DICompositeType *Ty) { |
| 1800 | ClassOptions CO = ClassOptions::None; |
| 1801 | |
| 1802 | // MSVC always sets this flag, even for local types. Clang doesn't always |
Reid Kleckner | a8d5740 | 2016-06-03 15:58:20 +0000 | [diff] [blame] | 1803 | // appear to give every type a linkage name, which may be problematic for us. |
| 1804 | // FIXME: Investigate the consequences of not following them here. |
Reid Kleckner | e092dad | 2016-07-02 00:11:07 +0000 | [diff] [blame] | 1805 | if (!Ty->getIdentifier().empty()) |
| 1806 | CO |= ClassOptions::HasUniqueName; |
| 1807 | |
| 1808 | // Put the Nested flag on a type if it appears immediately inside a tag type. |
| 1809 | // Do not walk the scope chain. Do not attempt to compute ContainsNestedClass |
| 1810 | // here. That flag is only set on definitions, and not forward declarations. |
| 1811 | const DIScope *ImmediateScope = Ty->getScope().resolve(); |
| 1812 | if (ImmediateScope && isa<DICompositeType>(ImmediateScope)) |
| 1813 | CO |= ClassOptions::Nested; |
| 1814 | |
| 1815 | // Put the Scoped flag on function-local types. |
| 1816 | for (const DIScope *Scope = ImmediateScope; Scope != nullptr; |
| 1817 | Scope = Scope->getScope().resolve()) { |
| 1818 | if (isa<DISubprogram>(Scope)) { |
| 1819 | CO |= ClassOptions::Scoped; |
| 1820 | break; |
| 1821 | } |
| 1822 | } |
| 1823 | |
| 1824 | return CO; |
Reid Kleckner | a8d5740 | 2016-06-03 15:58:20 +0000 | [diff] [blame] | 1825 | } |
| 1826 | |
Aaron Smith | 122d9e7 | 2018-03-06 18:20:22 +0000 | [diff] [blame] | 1827 | void CodeViewDebug::addUDTSrcLine(const DIType *Ty, TypeIndex TI) { |
| 1828 | switch (Ty->getTag()) { |
| 1829 | case dwarf::DW_TAG_class_type: |
| 1830 | case dwarf::DW_TAG_structure_type: |
| 1831 | case dwarf::DW_TAG_union_type: |
| 1832 | case dwarf::DW_TAG_enumeration_type: |
| 1833 | break; |
| 1834 | default: |
| 1835 | return; |
| 1836 | } |
| 1837 | |
| 1838 | if (const auto *File = Ty->getFile()) { |
| 1839 | StringIdRecord SIDR(TypeIndex(0x0), getFullFilepath(File)); |
| 1840 | TypeIndex SIDI = TypeTable.writeLeafType(SIDR); |
| 1841 | |
| 1842 | UdtSourceLineRecord USLR(TI, SIDI, Ty->getLine()); |
| 1843 | TypeTable.writeLeafType(USLR); |
| 1844 | } |
| 1845 | } |
| 1846 | |
David Majnemer | 979cb88 | 2016-06-16 21:32:16 +0000 | [diff] [blame] | 1847 | TypeIndex CodeViewDebug::lowerTypeEnum(const DICompositeType *Ty) { |
Reid Kleckner | e092dad | 2016-07-02 00:11:07 +0000 | [diff] [blame] | 1848 | ClassOptions CO = getCommonClassOptions(Ty); |
David Majnemer | 979cb88 | 2016-06-16 21:32:16 +0000 | [diff] [blame] | 1849 | TypeIndex FTI; |
David Majnemer | da9548f | 2016-06-17 16:13:21 +0000 | [diff] [blame] | 1850 | unsigned EnumeratorCount = 0; |
David Majnemer | 979cb88 | 2016-06-16 21:32:16 +0000 | [diff] [blame] | 1851 | |
David Majnemer | da9548f | 2016-06-17 16:13:21 +0000 | [diff] [blame] | 1852 | if (Ty->isForwardDecl()) { |
David Majnemer | 979cb88 | 2016-06-16 21:32:16 +0000 | [diff] [blame] | 1853 | CO |= ClassOptions::ForwardReference; |
David Majnemer | da9548f | 2016-06-17 16:13:21 +0000 | [diff] [blame] | 1854 | } else { |
Zachary Turner | 6900de1 | 2017-11-28 18:33:17 +0000 | [diff] [blame] | 1855 | ContinuationRecordBuilder ContinuationBuilder; |
| 1856 | ContinuationBuilder.begin(ContinuationRecordKind::FieldList); |
David Majnemer | da9548f | 2016-06-17 16:13:21 +0000 | [diff] [blame] | 1857 | for (const DINode *Element : Ty->getElements()) { |
| 1858 | // We assume that the frontend provides all members in source declaration |
| 1859 | // order, which is what MSVC does. |
| 1860 | if (auto *Enumerator = dyn_cast_or_null<DIEnumerator>(Element)) { |
Zachary Turner | 4efa0a4 | 2016-11-08 22:24:53 +0000 | [diff] [blame] | 1861 | EnumeratorRecord ER(MemberAccess::Public, |
| 1862 | APSInt::getUnsigned(Enumerator->getValue()), |
| 1863 | Enumerator->getName()); |
Zachary Turner | 6900de1 | 2017-11-28 18:33:17 +0000 | [diff] [blame] | 1864 | ContinuationBuilder.writeMemberType(ER); |
David Majnemer | da9548f | 2016-06-17 16:13:21 +0000 | [diff] [blame] | 1865 | EnumeratorCount++; |
| 1866 | } |
| 1867 | } |
Zachary Turner | 6900de1 | 2017-11-28 18:33:17 +0000 | [diff] [blame] | 1868 | FTI = TypeTable.insertRecord(ContinuationBuilder); |
David Majnemer | da9548f | 2016-06-17 16:13:21 +0000 | [diff] [blame] | 1869 | } |
David Majnemer | 979cb88 | 2016-06-16 21:32:16 +0000 | [diff] [blame] | 1870 | |
David Majnemer | 6bdc24e | 2016-07-01 23:12:45 +0000 | [diff] [blame] | 1871 | std::string FullName = getFullyQualifiedName(Ty); |
Reid Kleckner | 0c5d874 | 2016-06-22 01:32:56 +0000 | [diff] [blame] | 1872 | |
Zachary Turner | 4efa0a4 | 2016-11-08 22:24:53 +0000 | [diff] [blame] | 1873 | EnumRecord ER(EnumeratorCount, CO, FTI, FullName, Ty->getIdentifier(), |
| 1874 | getTypeIndex(Ty->getBaseType())); |
Aaron Smith | 122d9e7 | 2018-03-06 18:20:22 +0000 | [diff] [blame] | 1875 | TypeIndex EnumTI = TypeTable.writeLeafType(ER); |
| 1876 | |
| 1877 | addUDTSrcLine(Ty, EnumTI); |
| 1878 | |
| 1879 | return EnumTI; |
David Majnemer | 979cb88 | 2016-06-16 21:32:16 +0000 | [diff] [blame] | 1880 | } |
| 1881 | |
Amjad Aboud | 76c9eb9 | 2016-06-18 10:25:07 +0000 | [diff] [blame] | 1882 | //===----------------------------------------------------------------------===// |
| 1883 | // ClassInfo |
| 1884 | //===----------------------------------------------------------------------===// |
| 1885 | |
| 1886 | struct llvm::ClassInfo { |
| 1887 | struct MemberInfo { |
| 1888 | const DIDerivedType *MemberTypeNode; |
David Majnemer | 08bd744 | 2016-07-01 23:12:48 +0000 | [diff] [blame] | 1889 | uint64_t BaseOffset; |
Amjad Aboud | 76c9eb9 | 2016-06-18 10:25:07 +0000 | [diff] [blame] | 1890 | }; |
| 1891 | // [MemberInfo] |
Eugene Zelenko | fb69e66 | 2017-06-06 22:22:41 +0000 | [diff] [blame] | 1892 | using MemberList = std::vector<MemberInfo>; |
Amjad Aboud | 76c9eb9 | 2016-06-18 10:25:07 +0000 | [diff] [blame] | 1893 | |
Eugene Zelenko | fb69e66 | 2017-06-06 22:22:41 +0000 | [diff] [blame] | 1894 | using MethodsList = TinyPtrVector<const DISubprogram *>; |
Amjad Aboud | 76c9eb9 | 2016-06-18 10:25:07 +0000 | [diff] [blame] | 1895 | // MethodName -> MethodsList |
Eugene Zelenko | fb69e66 | 2017-06-06 22:22:41 +0000 | [diff] [blame] | 1896 | using MethodsMap = MapVector<MDString *, MethodsList>; |
Amjad Aboud | 76c9eb9 | 2016-06-18 10:25:07 +0000 | [diff] [blame] | 1897 | |
Reid Kleckner | 9f7f3e1 | 2016-06-24 16:24:24 +0000 | [diff] [blame] | 1898 | /// Base classes. |
| 1899 | std::vector<const DIDerivedType *> Inheritance; |
| 1900 | |
Amjad Aboud | 76c9eb9 | 2016-06-18 10:25:07 +0000 | [diff] [blame] | 1901 | /// Direct members. |
| 1902 | MemberList Members; |
| 1903 | // Direct overloaded methods gathered by name. |
| 1904 | MethodsMap Methods; |
Adrian McCarthy | 820ca54 | 2016-07-06 19:49:51 +0000 | [diff] [blame] | 1905 | |
Reid Kleckner | 9dac473 | 2016-08-31 15:59:30 +0000 | [diff] [blame] | 1906 | TypeIndex VShapeTI; |
| 1907 | |
Reid Kleckner | e2e8206 | 2017-08-08 20:30:14 +0000 | [diff] [blame] | 1908 | std::vector<const DIType *> NestedTypes; |
Amjad Aboud | 76c9eb9 | 2016-06-18 10:25:07 +0000 | [diff] [blame] | 1909 | }; |
| 1910 | |
| 1911 | void CodeViewDebug::clear() { |
| 1912 | assert(CurFn == nullptr); |
| 1913 | FileIdMap.clear(); |
| 1914 | FnDebugInfo.clear(); |
| 1915 | FileToFilepathMap.clear(); |
| 1916 | LocalUDTs.clear(); |
| 1917 | GlobalUDTs.clear(); |
| 1918 | TypeIndices.clear(); |
| 1919 | CompleteTypeIndices.clear(); |
Amjad Aboud | 76c9eb9 | 2016-06-18 10:25:07 +0000 | [diff] [blame] | 1920 | } |
| 1921 | |
| 1922 | void CodeViewDebug::collectMemberInfo(ClassInfo &Info, |
| 1923 | const DIDerivedType *DDTy) { |
| 1924 | if (!DDTy->getName().empty()) { |
| 1925 | Info.Members.push_back({DDTy, 0}); |
| 1926 | return; |
| 1927 | } |
Shoaib Meenai | 03303a3 | 2018-02-27 21:48:41 +0000 | [diff] [blame] | 1928 | |
| 1929 | // An unnamed member may represent a nested struct or union. Attempt to |
| 1930 | // interpret the unnamed member as a DICompositeType possibly wrapped in |
| 1931 | // qualifier types. Add all the indirect fields to the current record if that |
| 1932 | // succeeds, and drop the member if that fails. |
Amjad Aboud | 76c9eb9 | 2016-06-18 10:25:07 +0000 | [diff] [blame] | 1933 | assert((DDTy->getOffsetInBits() % 8) == 0 && "Unnamed bitfield member!"); |
David Majnemer | 08bd744 | 2016-07-01 23:12:48 +0000 | [diff] [blame] | 1934 | uint64_t Offset = DDTy->getOffsetInBits(); |
Amjad Aboud | 76c9eb9 | 2016-06-18 10:25:07 +0000 | [diff] [blame] | 1935 | const DIType *Ty = DDTy->getBaseType().resolve(); |
Shoaib Meenai | 03303a3 | 2018-02-27 21:48:41 +0000 | [diff] [blame] | 1936 | bool FullyResolved = false; |
| 1937 | while (!FullyResolved) { |
| 1938 | switch (Ty->getTag()) { |
| 1939 | case dwarf::DW_TAG_const_type: |
| 1940 | case dwarf::DW_TAG_volatile_type: |
| 1941 | // FIXME: we should apply the qualifier types to the indirect fields |
| 1942 | // rather than dropping them. |
| 1943 | Ty = cast<DIDerivedType>(Ty)->getBaseType().resolve(); |
| 1944 | break; |
| 1945 | default: |
| 1946 | FullyResolved = true; |
| 1947 | break; |
| 1948 | } |
| 1949 | } |
| 1950 | |
| 1951 | const DICompositeType *DCTy = dyn_cast<DICompositeType>(Ty); |
| 1952 | if (!DCTy) |
| 1953 | return; |
| 1954 | |
Reid Kleckner | 1ab7eac | 2016-06-22 16:06:42 +0000 | [diff] [blame] | 1955 | ClassInfo NestedInfo = collectClassInfo(DCTy); |
| 1956 | for (const ClassInfo::MemberInfo &IndirectField : NestedInfo.Members) |
Amjad Aboud | 76c9eb9 | 2016-06-18 10:25:07 +0000 | [diff] [blame] | 1957 | Info.Members.push_back( |
Reid Kleckner | 1ab7eac | 2016-06-22 16:06:42 +0000 | [diff] [blame] | 1958 | {IndirectField.MemberTypeNode, IndirectField.BaseOffset + Offset}); |
Amjad Aboud | 76c9eb9 | 2016-06-18 10:25:07 +0000 | [diff] [blame] | 1959 | } |
| 1960 | |
Reid Kleckner | 1ab7eac | 2016-06-22 16:06:42 +0000 | [diff] [blame] | 1961 | ClassInfo CodeViewDebug::collectClassInfo(const DICompositeType *Ty) { |
| 1962 | ClassInfo Info; |
Amjad Aboud | 76c9eb9 | 2016-06-18 10:25:07 +0000 | [diff] [blame] | 1963 | // Add elements to structure type. |
| 1964 | DINodeArray Elements = Ty->getElements(); |
| 1965 | for (auto *Element : Elements) { |
| 1966 | // We assume that the frontend provides all members in source declaration |
| 1967 | // order, which is what MSVC does. |
| 1968 | if (!Element) |
| 1969 | continue; |
| 1970 | if (auto *SP = dyn_cast<DISubprogram>(Element)) { |
Reid Kleckner | 156a723 | 2016-06-22 18:31:14 +0000 | [diff] [blame] | 1971 | Info.Methods[SP->getRawName()].push_back(SP); |
Amjad Aboud | 76c9eb9 | 2016-06-18 10:25:07 +0000 | [diff] [blame] | 1972 | } else if (auto *DDTy = dyn_cast<DIDerivedType>(Element)) { |
Reid Kleckner | 9f7f3e1 | 2016-06-24 16:24:24 +0000 | [diff] [blame] | 1973 | if (DDTy->getTag() == dwarf::DW_TAG_member) { |
Reid Kleckner | 1ab7eac | 2016-06-22 16:06:42 +0000 | [diff] [blame] | 1974 | collectMemberInfo(Info, DDTy); |
Reid Kleckner | 9f7f3e1 | 2016-06-24 16:24:24 +0000 | [diff] [blame] | 1975 | } else if (DDTy->getTag() == dwarf::DW_TAG_inheritance) { |
| 1976 | Info.Inheritance.push_back(DDTy); |
Reid Kleckner | 9dac473 | 2016-08-31 15:59:30 +0000 | [diff] [blame] | 1977 | } else if (DDTy->getTag() == dwarf::DW_TAG_pointer_type && |
| 1978 | DDTy->getName() == "__vtbl_ptr_type") { |
| 1979 | Info.VShapeTI = getTypeIndex(DDTy); |
Reid Kleckner | e2e8206 | 2017-08-08 20:30:14 +0000 | [diff] [blame] | 1980 | } else if (DDTy->getTag() == dwarf::DW_TAG_typedef) { |
| 1981 | Info.NestedTypes.push_back(DDTy); |
Amjad Aboud | 76c9eb9 | 2016-06-18 10:25:07 +0000 | [diff] [blame] | 1982 | } else if (DDTy->getTag() == dwarf::DW_TAG_friend) { |
| 1983 | // Ignore friend members. It appears that MSVC emitted info about |
| 1984 | // friends in the past, but modern versions do not. |
| 1985 | } |
Adrian McCarthy | 820ca54 | 2016-07-06 19:49:51 +0000 | [diff] [blame] | 1986 | } else if (auto *Composite = dyn_cast<DICompositeType>(Element)) { |
Reid Kleckner | e2e8206 | 2017-08-08 20:30:14 +0000 | [diff] [blame] | 1987 | Info.NestedTypes.push_back(Composite); |
Amjad Aboud | 76c9eb9 | 2016-06-18 10:25:07 +0000 | [diff] [blame] | 1988 | } |
| 1989 | // Skip other unrecognized kinds of elements. |
| 1990 | } |
Reid Kleckner | 1ab7eac | 2016-06-22 16:06:42 +0000 | [diff] [blame] | 1991 | return Info; |
Amjad Aboud | 76c9eb9 | 2016-06-18 10:25:07 +0000 | [diff] [blame] | 1992 | } |
| 1993 | |
Brock Wyma | b60532f | 2018-06-11 01:39:34 +0000 | [diff] [blame] | 1994 | static bool shouldAlwaysEmitCompleteClassType(const DICompositeType *Ty) { |
| 1995 | // This routine is used by lowerTypeClass and lowerTypeUnion to determine |
| 1996 | // if a complete type should be emitted instead of a forward reference. |
| 1997 | return Ty->getName().empty() && Ty->getIdentifier().empty() && |
| 1998 | !Ty->isForwardDecl(); |
| 1999 | } |
| 2000 | |
Reid Kleckner | a8d5740 | 2016-06-03 15:58:20 +0000 | [diff] [blame] | 2001 | TypeIndex CodeViewDebug::lowerTypeClass(const DICompositeType *Ty) { |
Brock Wyma | b60532f | 2018-06-11 01:39:34 +0000 | [diff] [blame] | 2002 | // Emit the complete type for unnamed structs. C++ classes with methods |
| 2003 | // which have a circular reference back to the class type are expected to |
| 2004 | // be named by the front-end and should not be "unnamed". C unnamed |
| 2005 | // structs should not have circular references. |
| 2006 | if (shouldAlwaysEmitCompleteClassType(Ty)) { |
| 2007 | // If this unnamed complete type is already in the process of being defined |
| 2008 | // then the description of the type is malformed and cannot be emitted |
| 2009 | // into CodeView correctly so report a fatal error. |
| 2010 | auto I = CompleteTypeIndices.find(Ty); |
| 2011 | if (I != CompleteTypeIndices.end() && I->second == TypeIndex()) |
| 2012 | report_fatal_error("cannot debug circular reference to unnamed type"); |
| 2013 | return getCompleteTypeIndex(Ty); |
| 2014 | } |
| 2015 | |
Reid Kleckner | a8d5740 | 2016-06-03 15:58:20 +0000 | [diff] [blame] | 2016 | // First, construct the forward decl. Don't look into Ty to compute the |
| 2017 | // forward decl options, since it might not be available in all TUs. |
| 2018 | TypeRecordKind Kind = getRecordKind(Ty); |
| 2019 | ClassOptions CO = |
Reid Kleckner | e092dad | 2016-07-02 00:11:07 +0000 | [diff] [blame] | 2020 | ClassOptions::ForwardReference | getCommonClassOptions(Ty); |
David Majnemer | 6bdc24e | 2016-07-01 23:12:45 +0000 | [diff] [blame] | 2021 | std::string FullName = getFullyQualifiedName(Ty); |
Zachary Turner | 4efa0a4 | 2016-11-08 22:24:53 +0000 | [diff] [blame] | 2022 | ClassRecord CR(Kind, 0, CO, TypeIndex(), TypeIndex(), TypeIndex(), 0, |
| 2023 | FullName, Ty->getIdentifier()); |
Zachary Turner | 6900de1 | 2017-11-28 18:33:17 +0000 | [diff] [blame] | 2024 | TypeIndex FwdDeclTI = TypeTable.writeLeafType(CR); |
Reid Kleckner | 643dd83 | 2016-06-22 17:15:28 +0000 | [diff] [blame] | 2025 | if (!Ty->isForwardDecl()) |
| 2026 | DeferredCompleteTypes.push_back(Ty); |
Reid Kleckner | a8d5740 | 2016-06-03 15:58:20 +0000 | [diff] [blame] | 2027 | return FwdDeclTI; |
| 2028 | } |
| 2029 | |
| 2030 | TypeIndex CodeViewDebug::lowerCompleteTypeClass(const DICompositeType *Ty) { |
| 2031 | // Construct the field list and complete type record. |
| 2032 | TypeRecordKind Kind = getRecordKind(Ty); |
Reid Kleckner | e092dad | 2016-07-02 00:11:07 +0000 | [diff] [blame] | 2033 | ClassOptions CO = getCommonClassOptions(Ty); |
Amjad Aboud | 76c9eb9 | 2016-06-18 10:25:07 +0000 | [diff] [blame] | 2034 | TypeIndex FieldTI; |
| 2035 | TypeIndex VShapeTI; |
Reid Kleckner | a8d5740 | 2016-06-03 15:58:20 +0000 | [diff] [blame] | 2036 | unsigned FieldCount; |
Adrian McCarthy | 820ca54 | 2016-07-06 19:49:51 +0000 | [diff] [blame] | 2037 | bool ContainsNestedClass; |
| 2038 | std::tie(FieldTI, VShapeTI, FieldCount, ContainsNestedClass) = |
| 2039 | lowerRecordFieldList(Ty); |
| 2040 | |
| 2041 | if (ContainsNestedClass) |
| 2042 | CO |= ClassOptions::ContainsNestedClass; |
Reid Kleckner | a8d5740 | 2016-06-03 15:58:20 +0000 | [diff] [blame] | 2043 | |
David Majnemer | 6bdc24e | 2016-07-01 23:12:45 +0000 | [diff] [blame] | 2044 | std::string FullName = getFullyQualifiedName(Ty); |
Reid Kleckner | 0c5d874 | 2016-06-22 01:32:56 +0000 | [diff] [blame] | 2045 | |
Reid Kleckner | a8d5740 | 2016-06-03 15:58:20 +0000 | [diff] [blame] | 2046 | uint64_t SizeInBytes = Ty->getSizeInBits() / 8; |
Hans Wennborg | 9a519a0 | 2016-06-22 21:22:13 +0000 | [diff] [blame] | 2047 | |
Zachary Turner | 4efa0a4 | 2016-11-08 22:24:53 +0000 | [diff] [blame] | 2048 | ClassRecord CR(Kind, FieldCount, CO, FieldTI, TypeIndex(), VShapeTI, |
| 2049 | SizeInBytes, FullName, Ty->getIdentifier()); |
Zachary Turner | 6900de1 | 2017-11-28 18:33:17 +0000 | [diff] [blame] | 2050 | TypeIndex ClassTI = TypeTable.writeLeafType(CR); |
Hans Wennborg | 9a519a0 | 2016-06-22 21:22:13 +0000 | [diff] [blame] | 2051 | |
Aaron Smith | 122d9e7 | 2018-03-06 18:20:22 +0000 | [diff] [blame] | 2052 | addUDTSrcLine(Ty, ClassTI); |
Hans Wennborg | 9a519a0 | 2016-06-22 21:22:13 +0000 | [diff] [blame] | 2053 | |
Zachary Turner | a7b0417 | 2017-08-28 18:49:04 +0000 | [diff] [blame] | 2054 | addToUDTs(Ty); |
Hans Wennborg | 4b63a98 | 2016-06-23 22:57:25 +0000 | [diff] [blame] | 2055 | |
Hans Wennborg | 9a519a0 | 2016-06-22 21:22:13 +0000 | [diff] [blame] | 2056 | return ClassTI; |
Reid Kleckner | a8d5740 | 2016-06-03 15:58:20 +0000 | [diff] [blame] | 2057 | } |
| 2058 | |
| 2059 | TypeIndex CodeViewDebug::lowerTypeUnion(const DICompositeType *Ty) { |
Brock Wyma | b60532f | 2018-06-11 01:39:34 +0000 | [diff] [blame] | 2060 | // Emit the complete type for unnamed unions. |
| 2061 | if (shouldAlwaysEmitCompleteClassType(Ty)) |
| 2062 | return getCompleteTypeIndex(Ty); |
| 2063 | |
Reid Kleckner | a8d5740 | 2016-06-03 15:58:20 +0000 | [diff] [blame] | 2064 | ClassOptions CO = |
Reid Kleckner | e092dad | 2016-07-02 00:11:07 +0000 | [diff] [blame] | 2065 | ClassOptions::ForwardReference | getCommonClassOptions(Ty); |
David Majnemer | 6bdc24e | 2016-07-01 23:12:45 +0000 | [diff] [blame] | 2066 | std::string FullName = getFullyQualifiedName(Ty); |
Zachary Turner | 4efa0a4 | 2016-11-08 22:24:53 +0000 | [diff] [blame] | 2067 | UnionRecord UR(0, CO, TypeIndex(), 0, FullName, Ty->getIdentifier()); |
Zachary Turner | 6900de1 | 2017-11-28 18:33:17 +0000 | [diff] [blame] | 2068 | TypeIndex FwdDeclTI = TypeTable.writeLeafType(UR); |
Reid Kleckner | 643dd83 | 2016-06-22 17:15:28 +0000 | [diff] [blame] | 2069 | if (!Ty->isForwardDecl()) |
| 2070 | DeferredCompleteTypes.push_back(Ty); |
Reid Kleckner | a8d5740 | 2016-06-03 15:58:20 +0000 | [diff] [blame] | 2071 | return FwdDeclTI; |
| 2072 | } |
| 2073 | |
| 2074 | TypeIndex CodeViewDebug::lowerCompleteTypeUnion(const DICompositeType *Ty) { |
David Majnemer | e1e7372 | 2016-07-06 21:07:42 +0000 | [diff] [blame] | 2075 | ClassOptions CO = ClassOptions::Sealed | getCommonClassOptions(Ty); |
Amjad Aboud | 76c9eb9 | 2016-06-18 10:25:07 +0000 | [diff] [blame] | 2076 | TypeIndex FieldTI; |
Reid Kleckner | a8d5740 | 2016-06-03 15:58:20 +0000 | [diff] [blame] | 2077 | unsigned FieldCount; |
Adrian McCarthy | 820ca54 | 2016-07-06 19:49:51 +0000 | [diff] [blame] | 2078 | bool ContainsNestedClass; |
| 2079 | std::tie(FieldTI, std::ignore, FieldCount, ContainsNestedClass) = |
| 2080 | lowerRecordFieldList(Ty); |
| 2081 | |
| 2082 | if (ContainsNestedClass) |
| 2083 | CO |= ClassOptions::ContainsNestedClass; |
| 2084 | |
Reid Kleckner | a8d5740 | 2016-06-03 15:58:20 +0000 | [diff] [blame] | 2085 | uint64_t SizeInBytes = Ty->getSizeInBits() / 8; |
David Majnemer | 6bdc24e | 2016-07-01 23:12:45 +0000 | [diff] [blame] | 2086 | std::string FullName = getFullyQualifiedName(Ty); |
Hans Wennborg | 9a519a0 | 2016-06-22 21:22:13 +0000 | [diff] [blame] | 2087 | |
Zachary Turner | 4efa0a4 | 2016-11-08 22:24:53 +0000 | [diff] [blame] | 2088 | UnionRecord UR(FieldCount, CO, FieldTI, SizeInBytes, FullName, |
| 2089 | Ty->getIdentifier()); |
Zachary Turner | 6900de1 | 2017-11-28 18:33:17 +0000 | [diff] [blame] | 2090 | TypeIndex UnionTI = TypeTable.writeLeafType(UR); |
Hans Wennborg | 9a519a0 | 2016-06-22 21:22:13 +0000 | [diff] [blame] | 2091 | |
Aaron Smith | 122d9e7 | 2018-03-06 18:20:22 +0000 | [diff] [blame] | 2092 | addUDTSrcLine(Ty, UnionTI); |
Hans Wennborg | 9a519a0 | 2016-06-22 21:22:13 +0000 | [diff] [blame] | 2093 | |
Zachary Turner | a7b0417 | 2017-08-28 18:49:04 +0000 | [diff] [blame] | 2094 | addToUDTs(Ty); |
Hans Wennborg | 4b63a98 | 2016-06-23 22:57:25 +0000 | [diff] [blame] | 2095 | |
Hans Wennborg | 9a519a0 | 2016-06-22 21:22:13 +0000 | [diff] [blame] | 2096 | return UnionTI; |
Reid Kleckner | a8d5740 | 2016-06-03 15:58:20 +0000 | [diff] [blame] | 2097 | } |
| 2098 | |
Adrian McCarthy | 820ca54 | 2016-07-06 19:49:51 +0000 | [diff] [blame] | 2099 | std::tuple<TypeIndex, TypeIndex, unsigned, bool> |
Reid Kleckner | a8d5740 | 2016-06-03 15:58:20 +0000 | [diff] [blame] | 2100 | CodeViewDebug::lowerRecordFieldList(const DICompositeType *Ty) { |
| 2101 | // Manually count members. MSVC appears to count everything that generates a |
| 2102 | // field list record. Each individual overload in a method overload group |
| 2103 | // contributes to this count, even though the overload group is a single field |
| 2104 | // list record. |
| 2105 | unsigned MemberCount = 0; |
Reid Kleckner | 1ab7eac | 2016-06-22 16:06:42 +0000 | [diff] [blame] | 2106 | ClassInfo Info = collectClassInfo(Ty); |
Zachary Turner | 6900de1 | 2017-11-28 18:33:17 +0000 | [diff] [blame] | 2107 | ContinuationRecordBuilder ContinuationBuilder; |
| 2108 | ContinuationBuilder.begin(ContinuationRecordKind::FieldList); |
Amjad Aboud | 76c9eb9 | 2016-06-18 10:25:07 +0000 | [diff] [blame] | 2109 | |
Reid Kleckner | 9f7f3e1 | 2016-06-24 16:24:24 +0000 | [diff] [blame] | 2110 | // Create base classes. |
| 2111 | for (const DIDerivedType *I : Info.Inheritance) { |
| 2112 | if (I->getFlags() & DINode::FlagVirtual) { |
| 2113 | // Virtual base. |
Brock Wyma | 3db2b10 | 2018-05-14 21:21:22 +0000 | [diff] [blame] | 2114 | unsigned VBPtrOffset = I->getVBPtrOffset(); |
Reid Kleckner | 9f7f3e1 | 2016-06-24 16:24:24 +0000 | [diff] [blame] | 2115 | // FIXME: Despite the accessor name, the offset is really in bytes. |
| 2116 | unsigned VBTableIndex = I->getOffsetInBits() / 4; |
Bob Haarman | 26a87bd | 2016-10-25 22:11:52 +0000 | [diff] [blame] | 2117 | auto RecordKind = (I->getFlags() & DINode::FlagIndirectVirtualBase) == DINode::FlagIndirectVirtualBase |
| 2118 | ? TypeRecordKind::IndirectVirtualBaseClass |
| 2119 | : TypeRecordKind::VirtualBaseClass; |
Zachary Turner | 4efa0a4 | 2016-11-08 22:24:53 +0000 | [diff] [blame] | 2120 | VirtualBaseClassRecord VBCR( |
Bob Haarman | 26a87bd | 2016-10-25 22:11:52 +0000 | [diff] [blame] | 2121 | RecordKind, translateAccessFlags(Ty->getTag(), I->getFlags()), |
Reid Kleckner | 9f7f3e1 | 2016-06-24 16:24:24 +0000 | [diff] [blame] | 2122 | getTypeIndex(I->getBaseType()), getVBPTypeIndex(), VBPtrOffset, |
Zachary Turner | 4efa0a4 | 2016-11-08 22:24:53 +0000 | [diff] [blame] | 2123 | VBTableIndex); |
| 2124 | |
Zachary Turner | 6900de1 | 2017-11-28 18:33:17 +0000 | [diff] [blame] | 2125 | ContinuationBuilder.writeMemberType(VBCR); |
Brock Wyma | 4536c1f | 2018-02-01 20:37:38 +0000 | [diff] [blame] | 2126 | MemberCount++; |
Reid Kleckner | 9f7f3e1 | 2016-06-24 16:24:24 +0000 | [diff] [blame] | 2127 | } else { |
| 2128 | assert(I->getOffsetInBits() % 8 == 0 && |
| 2129 | "bases must be on byte boundaries"); |
Zachary Turner | 4efa0a4 | 2016-11-08 22:24:53 +0000 | [diff] [blame] | 2130 | BaseClassRecord BCR(translateAccessFlags(Ty->getTag(), I->getFlags()), |
| 2131 | getTypeIndex(I->getBaseType()), |
| 2132 | I->getOffsetInBits() / 8); |
Zachary Turner | 6900de1 | 2017-11-28 18:33:17 +0000 | [diff] [blame] | 2133 | ContinuationBuilder.writeMemberType(BCR); |
Brock Wyma | 4536c1f | 2018-02-01 20:37:38 +0000 | [diff] [blame] | 2134 | MemberCount++; |
Reid Kleckner | 9f7f3e1 | 2016-06-24 16:24:24 +0000 | [diff] [blame] | 2135 | } |
| 2136 | } |
| 2137 | |
Amjad Aboud | 76c9eb9 | 2016-06-18 10:25:07 +0000 | [diff] [blame] | 2138 | // Create members. |
| 2139 | for (ClassInfo::MemberInfo &MemberInfo : Info.Members) { |
| 2140 | const DIDerivedType *Member = MemberInfo.MemberTypeNode; |
| 2141 | TypeIndex MemberBaseType = getTypeIndex(Member->getBaseType()); |
David Majnemer | 9319cbc | 2016-06-30 03:00:20 +0000 | [diff] [blame] | 2142 | StringRef MemberName = Member->getName(); |
| 2143 | MemberAccess Access = |
| 2144 | translateAccessFlags(Ty->getTag(), Member->getFlags()); |
Amjad Aboud | 76c9eb9 | 2016-06-18 10:25:07 +0000 | [diff] [blame] | 2145 | |
| 2146 | if (Member->isStaticMember()) { |
Zachary Turner | 4efa0a4 | 2016-11-08 22:24:53 +0000 | [diff] [blame] | 2147 | StaticDataMemberRecord SDMR(Access, MemberBaseType, MemberName); |
Zachary Turner | 6900de1 | 2017-11-28 18:33:17 +0000 | [diff] [blame] | 2148 | ContinuationBuilder.writeMemberType(SDMR); |
Amjad Aboud | 76c9eb9 | 2016-06-18 10:25:07 +0000 | [diff] [blame] | 2149 | MemberCount++; |
Reid Kleckner | a8d5740 | 2016-06-03 15:58:20 +0000 | [diff] [blame] | 2150 | continue; |
Reid Kleckner | a8d5740 | 2016-06-03 15:58:20 +0000 | [diff] [blame] | 2151 | } |
Amjad Aboud | 76c9eb9 | 2016-06-18 10:25:07 +0000 | [diff] [blame] | 2152 | |
Reid Kleckner | 9dac473 | 2016-08-31 15:59:30 +0000 | [diff] [blame] | 2153 | // Virtual function pointer member. |
| 2154 | if ((Member->getFlags() & DINode::FlagArtificial) && |
| 2155 | Member->getName().startswith("_vptr$")) { |
Zachary Turner | 4efa0a4 | 2016-11-08 22:24:53 +0000 | [diff] [blame] | 2156 | VFPtrRecord VFPR(getTypeIndex(Member->getBaseType())); |
Zachary Turner | 6900de1 | 2017-11-28 18:33:17 +0000 | [diff] [blame] | 2157 | ContinuationBuilder.writeMemberType(VFPR); |
Reid Kleckner | 9dac473 | 2016-08-31 15:59:30 +0000 | [diff] [blame] | 2158 | MemberCount++; |
| 2159 | continue; |
| 2160 | } |
| 2161 | |
David Majnemer | 9319cbc | 2016-06-30 03:00:20 +0000 | [diff] [blame] | 2162 | // Data member. |
David Majnemer | 08bd744 | 2016-07-01 23:12:48 +0000 | [diff] [blame] | 2163 | uint64_t MemberOffsetInBits = |
| 2164 | Member->getOffsetInBits() + MemberInfo.BaseOffset; |
David Majnemer | 9319cbc | 2016-06-30 03:00:20 +0000 | [diff] [blame] | 2165 | if (Member->isBitField()) { |
| 2166 | uint64_t StartBitOffset = MemberOffsetInBits; |
| 2167 | if (const auto *CI = |
| 2168 | dyn_cast_or_null<ConstantInt>(Member->getStorageOffsetInBits())) { |
David Majnemer | 08bd744 | 2016-07-01 23:12:48 +0000 | [diff] [blame] | 2169 | MemberOffsetInBits = CI->getZExtValue() + MemberInfo.BaseOffset; |
David Majnemer | 9319cbc | 2016-06-30 03:00:20 +0000 | [diff] [blame] | 2170 | } |
| 2171 | StartBitOffset -= MemberOffsetInBits; |
Zachary Turner | 4efa0a4 | 2016-11-08 22:24:53 +0000 | [diff] [blame] | 2172 | BitFieldRecord BFR(MemberBaseType, Member->getSizeInBits(), |
| 2173 | StartBitOffset); |
Zachary Turner | 6900de1 | 2017-11-28 18:33:17 +0000 | [diff] [blame] | 2174 | MemberBaseType = TypeTable.writeLeafType(BFR); |
David Majnemer | 9319cbc | 2016-06-30 03:00:20 +0000 | [diff] [blame] | 2175 | } |
| 2176 | uint64_t MemberOffsetInBytes = MemberOffsetInBits / 8; |
Zachary Turner | 4efa0a4 | 2016-11-08 22:24:53 +0000 | [diff] [blame] | 2177 | DataMemberRecord DMR(Access, MemberBaseType, MemberOffsetInBytes, |
| 2178 | MemberName); |
Zachary Turner | 6900de1 | 2017-11-28 18:33:17 +0000 | [diff] [blame] | 2179 | ContinuationBuilder.writeMemberType(DMR); |
Amjad Aboud | 76c9eb9 | 2016-06-18 10:25:07 +0000 | [diff] [blame] | 2180 | MemberCount++; |
Reid Kleckner | a8d5740 | 2016-06-03 15:58:20 +0000 | [diff] [blame] | 2181 | } |
Amjad Aboud | 76c9eb9 | 2016-06-18 10:25:07 +0000 | [diff] [blame] | 2182 | |
| 2183 | // Create methods |
| 2184 | for (auto &MethodItr : Info.Methods) { |
| 2185 | StringRef Name = MethodItr.first->getString(); |
| 2186 | |
| 2187 | std::vector<OneMethodRecord> Methods; |
Reid Kleckner | 156a723 | 2016-06-22 18:31:14 +0000 | [diff] [blame] | 2188 | for (const DISubprogram *SP : MethodItr.second) { |
| 2189 | TypeIndex MethodType = getMemberFunctionType(SP, Ty); |
| 2190 | bool Introduced = SP->getFlags() & DINode::FlagIntroducedVirtual; |
Amjad Aboud | 76c9eb9 | 2016-06-18 10:25:07 +0000 | [diff] [blame] | 2191 | |
| 2192 | unsigned VFTableOffset = -1; |
| 2193 | if (Introduced) |
| 2194 | VFTableOffset = SP->getVirtualIndex() * getPointerSizeInBytes(); |
| 2195 | |
Zachary Turner | 7251ede | 2016-11-02 17:05:19 +0000 | [diff] [blame] | 2196 | Methods.push_back(OneMethodRecord( |
| 2197 | MethodType, translateAccessFlags(Ty->getTag(), SP->getFlags()), |
| 2198 | translateMethodKindFlags(SP, Introduced), |
| 2199 | translateMethodOptionFlags(SP), VFTableOffset, Name)); |
Amjad Aboud | 76c9eb9 | 2016-06-18 10:25:07 +0000 | [diff] [blame] | 2200 | MemberCount++; |
| 2201 | } |
Eugene Zelenko | fb69e66 | 2017-06-06 22:22:41 +0000 | [diff] [blame] | 2202 | assert(!Methods.empty() && "Empty methods map entry"); |
Amjad Aboud | 76c9eb9 | 2016-06-18 10:25:07 +0000 | [diff] [blame] | 2203 | if (Methods.size() == 1) |
Zachary Turner | 6900de1 | 2017-11-28 18:33:17 +0000 | [diff] [blame] | 2204 | ContinuationBuilder.writeMemberType(Methods[0]); |
Amjad Aboud | 76c9eb9 | 2016-06-18 10:25:07 +0000 | [diff] [blame] | 2205 | else { |
Zachary Turner | 6900de1 | 2017-11-28 18:33:17 +0000 | [diff] [blame] | 2206 | // FIXME: Make this use its own ContinuationBuilder so that |
| 2207 | // MethodOverloadList can be split correctly. |
Zachary Turner | 4efa0a4 | 2016-11-08 22:24:53 +0000 | [diff] [blame] | 2208 | MethodOverloadListRecord MOLR(Methods); |
Zachary Turner | 6900de1 | 2017-11-28 18:33:17 +0000 | [diff] [blame] | 2209 | TypeIndex MethodList = TypeTable.writeLeafType(MOLR); |
| 2210 | |
Zachary Turner | 4efa0a4 | 2016-11-08 22:24:53 +0000 | [diff] [blame] | 2211 | OverloadedMethodRecord OMR(Methods.size(), MethodList, Name); |
Zachary Turner | 6900de1 | 2017-11-28 18:33:17 +0000 | [diff] [blame] | 2212 | ContinuationBuilder.writeMemberType(OMR); |
Amjad Aboud | 76c9eb9 | 2016-06-18 10:25:07 +0000 | [diff] [blame] | 2213 | } |
| 2214 | } |
Adrian McCarthy | 820ca54 | 2016-07-06 19:49:51 +0000 | [diff] [blame] | 2215 | |
| 2216 | // Create nested classes. |
Reid Kleckner | e2e8206 | 2017-08-08 20:30:14 +0000 | [diff] [blame] | 2217 | for (const DIType *Nested : Info.NestedTypes) { |
Adrian McCarthy | 820ca54 | 2016-07-06 19:49:51 +0000 | [diff] [blame] | 2218 | NestedTypeRecord R(getTypeIndex(DITypeRef(Nested)), Nested->getName()); |
Zachary Turner | 6900de1 | 2017-11-28 18:33:17 +0000 | [diff] [blame] | 2219 | ContinuationBuilder.writeMemberType(R); |
Adrian McCarthy | 820ca54 | 2016-07-06 19:49:51 +0000 | [diff] [blame] | 2220 | MemberCount++; |
| 2221 | } |
| 2222 | |
Zachary Turner | 6900de1 | 2017-11-28 18:33:17 +0000 | [diff] [blame] | 2223 | TypeIndex FieldTI = TypeTable.insertRecord(ContinuationBuilder); |
Reid Kleckner | 9dac473 | 2016-08-31 15:59:30 +0000 | [diff] [blame] | 2224 | return std::make_tuple(FieldTI, Info.VShapeTI, MemberCount, |
Reid Kleckner | e2e8206 | 2017-08-08 20:30:14 +0000 | [diff] [blame] | 2225 | !Info.NestedTypes.empty()); |
Reid Kleckner | a8d5740 | 2016-06-03 15:58:20 +0000 | [diff] [blame] | 2226 | } |
| 2227 | |
Reid Kleckner | 9f7f3e1 | 2016-06-24 16:24:24 +0000 | [diff] [blame] | 2228 | TypeIndex CodeViewDebug::getVBPTypeIndex() { |
| 2229 | if (!VBPType.getIndex()) { |
| 2230 | // Make a 'const int *' type. |
| 2231 | ModifierRecord MR(TypeIndex::Int32(), ModifierOptions::Const); |
Zachary Turner | 6900de1 | 2017-11-28 18:33:17 +0000 | [diff] [blame] | 2232 | TypeIndex ModifiedTI = TypeTable.writeLeafType(MR); |
Reid Kleckner | 9f7f3e1 | 2016-06-24 16:24:24 +0000 | [diff] [blame] | 2233 | |
| 2234 | PointerKind PK = getPointerSizeInBytes() == 8 ? PointerKind::Near64 |
| 2235 | : PointerKind::Near32; |
| 2236 | PointerMode PM = PointerMode::Pointer; |
| 2237 | PointerOptions PO = PointerOptions::None; |
| 2238 | PointerRecord PR(ModifiedTI, PK, PM, PO, getPointerSizeInBytes()); |
Zachary Turner | 6900de1 | 2017-11-28 18:33:17 +0000 | [diff] [blame] | 2239 | VBPType = TypeTable.writeLeafType(PR); |
Reid Kleckner | 9f7f3e1 | 2016-06-24 16:24:24 +0000 | [diff] [blame] | 2240 | } |
| 2241 | |
| 2242 | return VBPType; |
| 2243 | } |
| 2244 | |
Amjad Aboud | 76c9eb9 | 2016-06-18 10:25:07 +0000 | [diff] [blame] | 2245 | TypeIndex CodeViewDebug::getTypeIndex(DITypeRef TypeRef, DITypeRef ClassTyRef) { |
Reid Kleckner | 5acacbb | 2016-06-01 17:05:51 +0000 | [diff] [blame] | 2246 | const DIType *Ty = TypeRef.resolve(); |
Amjad Aboud | 76c9eb9 | 2016-06-18 10:25:07 +0000 | [diff] [blame] | 2247 | const DIType *ClassTy = ClassTyRef.resolve(); |
Reid Kleckner | 5acacbb | 2016-06-01 17:05:51 +0000 | [diff] [blame] | 2248 | |
| 2249 | // The null DIType is the void type. Don't try to hash it. |
| 2250 | if (!Ty) |
| 2251 | return TypeIndex::Void(); |
| 2252 | |
Reid Kleckner | a8d5740 | 2016-06-03 15:58:20 +0000 | [diff] [blame] | 2253 | // Check if we've already translated this type. Don't try to do a |
| 2254 | // get-or-create style insertion that caches the hash lookup across the |
| 2255 | // lowerType call. It will update the TypeIndices map. |
Amjad Aboud | 76c9eb9 | 2016-06-18 10:25:07 +0000 | [diff] [blame] | 2256 | auto I = TypeIndices.find({Ty, ClassTy}); |
Reid Kleckner | 5acacbb | 2016-06-01 17:05:51 +0000 | [diff] [blame] | 2257 | if (I != TypeIndices.end()) |
| 2258 | return I->second; |
| 2259 | |
Reid Kleckner | b5af11d | 2016-07-01 02:41:21 +0000 | [diff] [blame] | 2260 | TypeLoweringScope S(*this); |
| 2261 | TypeIndex TI = lowerType(Ty, ClassTy); |
| 2262 | return recordTypeIndexForDINode(Ty, TI, ClassTy); |
Reid Kleckner | a8d5740 | 2016-06-03 15:58:20 +0000 | [diff] [blame] | 2263 | } |
| 2264 | |
Bob Haarman | 223303c | 2017-08-29 20:59:25 +0000 | [diff] [blame] | 2265 | TypeIndex CodeViewDebug::getTypeIndexForReferenceTo(DITypeRef TypeRef) { |
| 2266 | DIType *Ty = TypeRef.resolve(); |
| 2267 | PointerRecord PR(getTypeIndex(Ty), |
| 2268 | getPointerSizeInBytes() == 8 ? PointerKind::Near64 |
| 2269 | : PointerKind::Near32, |
| 2270 | PointerMode::LValueReference, PointerOptions::None, |
| 2271 | Ty->getSizeInBits() / 8); |
Zachary Turner | 6900de1 | 2017-11-28 18:33:17 +0000 | [diff] [blame] | 2272 | return TypeTable.writeLeafType(PR); |
Bob Haarman | 223303c | 2017-08-29 20:59:25 +0000 | [diff] [blame] | 2273 | } |
| 2274 | |
Reid Kleckner | a8d5740 | 2016-06-03 15:58:20 +0000 | [diff] [blame] | 2275 | TypeIndex CodeViewDebug::getCompleteTypeIndex(DITypeRef TypeRef) { |
| 2276 | const DIType *Ty = TypeRef.resolve(); |
| 2277 | |
| 2278 | // The null DIType is the void type. Don't try to hash it. |
| 2279 | if (!Ty) |
| 2280 | return TypeIndex::Void(); |
| 2281 | |
| 2282 | // If this is a non-record type, the complete type index is the same as the |
| 2283 | // normal type index. Just call getTypeIndex. |
| 2284 | switch (Ty->getTag()) { |
| 2285 | case dwarf::DW_TAG_class_type: |
| 2286 | case dwarf::DW_TAG_structure_type: |
| 2287 | case dwarf::DW_TAG_union_type: |
| 2288 | break; |
| 2289 | default: |
| 2290 | return getTypeIndex(Ty); |
| 2291 | } |
| 2292 | |
Brock Wyma | b60532f | 2018-06-11 01:39:34 +0000 | [diff] [blame] | 2293 | // Check if we've already translated the complete record type. |
Reid Kleckner | a8d5740 | 2016-06-03 15:58:20 +0000 | [diff] [blame] | 2294 | const auto *CTy = cast<DICompositeType>(Ty); |
| 2295 | auto InsertResult = CompleteTypeIndices.insert({CTy, TypeIndex()}); |
| 2296 | if (!InsertResult.second) |
| 2297 | return InsertResult.first->second; |
| 2298 | |
Reid Kleckner | 643dd83 | 2016-06-22 17:15:28 +0000 | [diff] [blame] | 2299 | TypeLoweringScope S(*this); |
| 2300 | |
Reid Kleckner | a8d5740 | 2016-06-03 15:58:20 +0000 | [diff] [blame] | 2301 | // Make sure the forward declaration is emitted first. It's unclear if this |
| 2302 | // is necessary, but MSVC does it, and we should follow suit until we can show |
| 2303 | // otherwise. |
Brock Wyma | b60532f | 2018-06-11 01:39:34 +0000 | [diff] [blame] | 2304 | // We only emit a forward declaration for named types. |
| 2305 | if (!CTy->getName().empty() || !CTy->getIdentifier().empty()) { |
| 2306 | TypeIndex FwdDeclTI = getTypeIndex(CTy); |
Reid Kleckner | a8d5740 | 2016-06-03 15:58:20 +0000 | [diff] [blame] | 2307 | |
Brock Wyma | b60532f | 2018-06-11 01:39:34 +0000 | [diff] [blame] | 2308 | // Just use the forward decl if we don't have complete type info. This |
| 2309 | // might happen if the frontend is using modules and expects the complete |
| 2310 | // definition to be emitted elsewhere. |
| 2311 | if (CTy->isForwardDecl()) |
| 2312 | return FwdDeclTI; |
| 2313 | } |
Reid Kleckner | a8d5740 | 2016-06-03 15:58:20 +0000 | [diff] [blame] | 2314 | |
| 2315 | TypeIndex TI; |
| 2316 | switch (CTy->getTag()) { |
| 2317 | case dwarf::DW_TAG_class_type: |
| 2318 | case dwarf::DW_TAG_structure_type: |
| 2319 | TI = lowerCompleteTypeClass(CTy); |
| 2320 | break; |
| 2321 | case dwarf::DW_TAG_union_type: |
| 2322 | TI = lowerCompleteTypeUnion(CTy); |
| 2323 | break; |
| 2324 | default: |
| 2325 | llvm_unreachable("not a record"); |
| 2326 | } |
| 2327 | |
Brock Wyma | b60532f | 2018-06-11 01:39:34 +0000 | [diff] [blame] | 2328 | // Update the type index associated with this CompositeType. This cannot |
| 2329 | // use the 'InsertResult' iterator above because it is potentially |
| 2330 | // invalidated by map insertions which can occur while lowering the class |
| 2331 | // type above. |
| 2332 | CompleteTypeIndices[CTy] = TI; |
Reid Kleckner | 5acacbb | 2016-06-01 17:05:51 +0000 | [diff] [blame] | 2333 | return TI; |
| 2334 | } |
| 2335 | |
Reid Kleckner | 643dd83 | 2016-06-22 17:15:28 +0000 | [diff] [blame] | 2336 | /// Emit all the deferred complete record types. Try to do this in FIFO order, |
Amjad Aboud | acee568 | 2016-07-12 12:06:34 +0000 | [diff] [blame] | 2337 | /// and do this until fixpoint, as each complete record type typically |
| 2338 | /// references |
Reid Kleckner | 643dd83 | 2016-06-22 17:15:28 +0000 | [diff] [blame] | 2339 | /// many other record types. |
| 2340 | void CodeViewDebug::emitDeferredCompleteTypes() { |
| 2341 | SmallVector<const DICompositeType *, 4> TypesToEmit; |
| 2342 | while (!DeferredCompleteTypes.empty()) { |
| 2343 | std::swap(DeferredCompleteTypes, TypesToEmit); |
| 2344 | for (const DICompositeType *RecordTy : TypesToEmit) |
| 2345 | getCompleteTypeIndex(RecordTy); |
| 2346 | TypesToEmit.clear(); |
| 2347 | } |
| 2348 | } |
| 2349 | |
Reid Kleckner | 10dd55c | 2016-06-24 17:55:40 +0000 | [diff] [blame] | 2350 | void CodeViewDebug::emitLocalVariableList(ArrayRef<LocalVariable> Locals) { |
| 2351 | // Get the sorted list of parameters and emit them first. |
| 2352 | SmallVector<const LocalVariable *, 6> Params; |
| 2353 | for (const LocalVariable &L : Locals) |
| 2354 | if (L.DIVar->isParameter()) |
| 2355 | Params.push_back(&L); |
Fangrui Song | 0cac726 | 2018-09-27 02:13:45 +0000 | [diff] [blame^] | 2356 | llvm::sort(Params, [](const LocalVariable *L, const LocalVariable *R) { |
| 2357 | return L->DIVar->getArg() < R->DIVar->getArg(); |
| 2358 | }); |
Reid Kleckner | 10dd55c | 2016-06-24 17:55:40 +0000 | [diff] [blame] | 2359 | for (const LocalVariable *L : Params) |
| 2360 | emitLocalVariable(*L); |
| 2361 | |
| 2362 | // Next emit all non-parameters in the order that we found them. |
| 2363 | for (const LocalVariable &L : Locals) |
| 2364 | if (!L.DIVar->isParameter()) |
| 2365 | emitLocalVariable(L); |
| 2366 | } |
| 2367 | |
Reid Kleckner | f9c275f | 2016-02-10 20:55:49 +0000 | [diff] [blame] | 2368 | void CodeViewDebug::emitLocalVariable(const LocalVariable &Var) { |
| 2369 | // LocalSym record, see SymbolRecord.h for more info. |
| 2370 | MCSymbol *LocalBegin = MMI->getContext().createTempSymbol(), |
| 2371 | *LocalEnd = MMI->getContext().createTempSymbol(); |
| 2372 | OS.AddComment("Record length"); |
| 2373 | OS.emitAbsoluteSymbolDiff(LocalEnd, LocalBegin, 2); |
| 2374 | OS.EmitLabel(LocalBegin); |
| 2375 | |
| 2376 | OS.AddComment("Record kind: S_LOCAL"); |
Zachary Turner | 63a2846 | 2016-05-17 23:50:21 +0000 | [diff] [blame] | 2377 | OS.EmitIntValue(unsigned(SymbolKind::S_LOCAL), 2); |
Reid Kleckner | f9c275f | 2016-02-10 20:55:49 +0000 | [diff] [blame] | 2378 | |
Zachary Turner | 63a2846 | 2016-05-17 23:50:21 +0000 | [diff] [blame] | 2379 | LocalSymFlags Flags = LocalSymFlags::None; |
Reid Kleckner | f9c275f | 2016-02-10 20:55:49 +0000 | [diff] [blame] | 2380 | if (Var.DIVar->isParameter()) |
Zachary Turner | 63a2846 | 2016-05-17 23:50:21 +0000 | [diff] [blame] | 2381 | Flags |= LocalSymFlags::IsParameter; |
Reid Kleckner | 876330d | 2016-02-12 21:48:30 +0000 | [diff] [blame] | 2382 | if (Var.DefRanges.empty()) |
Zachary Turner | 63a2846 | 2016-05-17 23:50:21 +0000 | [diff] [blame] | 2383 | Flags |= LocalSymFlags::IsOptimizedOut; |
Reid Kleckner | f9c275f | 2016-02-10 20:55:49 +0000 | [diff] [blame] | 2384 | |
| 2385 | OS.AddComment("TypeIndex"); |
Reid Kleckner | 08f5fd5 | 2017-08-31 15:56:49 +0000 | [diff] [blame] | 2386 | TypeIndex TI = Var.UseReferenceType |
| 2387 | ? getTypeIndexForReferenceTo(Var.DIVar->getType()) |
| 2388 | : getCompleteTypeIndex(Var.DIVar->getType()); |
Reid Kleckner | 5acacbb | 2016-06-01 17:05:51 +0000 | [diff] [blame] | 2389 | OS.EmitIntValue(TI.getIndex(), 4); |
Reid Kleckner | f9c275f | 2016-02-10 20:55:49 +0000 | [diff] [blame] | 2390 | OS.AddComment("Flags"); |
Zachary Turner | 63a2846 | 2016-05-17 23:50:21 +0000 | [diff] [blame] | 2391 | OS.EmitIntValue(static_cast<uint16_t>(Flags), 2); |
David Majnemer | 1256125 | 2016-03-13 10:53:30 +0000 | [diff] [blame] | 2392 | // Truncate the name so we won't overflow the record length field. |
David Majnemer | b9456a5 | 2016-03-14 05:15:09 +0000 | [diff] [blame] | 2393 | emitNullTerminatedSymbolName(OS, Var.DIVar->getName()); |
Reid Kleckner | f9c275f | 2016-02-10 20:55:49 +0000 | [diff] [blame] | 2394 | OS.EmitLabel(LocalEnd); |
| 2395 | |
Reid Kleckner | 876330d | 2016-02-12 21:48:30 +0000 | [diff] [blame] | 2396 | // Calculate the on disk prefix of the appropriate def range record. The |
| 2397 | // records and on disk formats are described in SymbolRecords.h. BytePrefix |
| 2398 | // should be big enough to hold all forms without memory allocation. |
| 2399 | SmallString<20> BytePrefix; |
| 2400 | for (const LocalVarDefRange &DefRange : Var.DefRanges) { |
| 2401 | BytePrefix.clear(); |
Reid Kleckner | 876330d | 2016-02-12 21:48:30 +0000 | [diff] [blame] | 2402 | if (DefRange.InMemory) { |
Reid Kleckner | 2b3e642 | 2016-10-05 21:21:33 +0000 | [diff] [blame] | 2403 | uint16_t RegRelFlags = 0; |
| 2404 | if (DefRange.IsSubfield) { |
| 2405 | RegRelFlags = DefRangeRegisterRelSym::IsSubfieldFlag | |
| 2406 | (DefRange.StructOffset |
| 2407 | << DefRangeRegisterRelSym::OffsetInParentShift); |
| 2408 | } |
Zachary Turner | 46225b1 | 2016-12-16 22:48:14 +0000 | [diff] [blame] | 2409 | DefRangeRegisterRelSym Sym(S_DEFRANGE_REGISTER_REL); |
| 2410 | Sym.Hdr.Register = DefRange.CVRegister; |
| 2411 | Sym.Hdr.Flags = RegRelFlags; |
| 2412 | Sym.Hdr.BasePointerOffset = DefRange.DataOffset; |
Reid Kleckner | 876330d | 2016-02-12 21:48:30 +0000 | [diff] [blame] | 2413 | ulittle16_t SymKind = ulittle16_t(S_DEFRANGE_REGISTER_REL); |
Reid Kleckner | 876330d | 2016-02-12 21:48:30 +0000 | [diff] [blame] | 2414 | BytePrefix += |
| 2415 | StringRef(reinterpret_cast<const char *>(&SymKind), sizeof(SymKind)); |
Zachary Turner | a78ecd1 | 2016-05-23 18:49:06 +0000 | [diff] [blame] | 2416 | BytePrefix += |
Zachary Turner | 46225b1 | 2016-12-16 22:48:14 +0000 | [diff] [blame] | 2417 | StringRef(reinterpret_cast<const char *>(&Sym.Hdr), sizeof(Sym.Hdr)); |
Reid Kleckner | 876330d | 2016-02-12 21:48:30 +0000 | [diff] [blame] | 2418 | } else { |
| 2419 | assert(DefRange.DataOffset == 0 && "unexpected offset into register"); |
Reid Kleckner | 2b3e642 | 2016-10-05 21:21:33 +0000 | [diff] [blame] | 2420 | if (DefRange.IsSubfield) { |
| 2421 | // Unclear what matters here. |
Zachary Turner | 46225b1 | 2016-12-16 22:48:14 +0000 | [diff] [blame] | 2422 | DefRangeSubfieldRegisterSym Sym(S_DEFRANGE_SUBFIELD_REGISTER); |
| 2423 | Sym.Hdr.Register = DefRange.CVRegister; |
| 2424 | Sym.Hdr.MayHaveNoName = 0; |
| 2425 | Sym.Hdr.OffsetInParent = DefRange.StructOffset; |
| 2426 | |
Reid Kleckner | 2b3e642 | 2016-10-05 21:21:33 +0000 | [diff] [blame] | 2427 | ulittle16_t SymKind = ulittle16_t(S_DEFRANGE_SUBFIELD_REGISTER); |
| 2428 | BytePrefix += StringRef(reinterpret_cast<const char *>(&SymKind), |
| 2429 | sizeof(SymKind)); |
Zachary Turner | 46225b1 | 2016-12-16 22:48:14 +0000 | [diff] [blame] | 2430 | BytePrefix += StringRef(reinterpret_cast<const char *>(&Sym.Hdr), |
| 2431 | sizeof(Sym.Hdr)); |
Reid Kleckner | 2b3e642 | 2016-10-05 21:21:33 +0000 | [diff] [blame] | 2432 | } else { |
| 2433 | // Unclear what matters here. |
Zachary Turner | 46225b1 | 2016-12-16 22:48:14 +0000 | [diff] [blame] | 2434 | DefRangeRegisterSym Sym(S_DEFRANGE_REGISTER); |
| 2435 | Sym.Hdr.Register = DefRange.CVRegister; |
| 2436 | Sym.Hdr.MayHaveNoName = 0; |
Reid Kleckner | 2b3e642 | 2016-10-05 21:21:33 +0000 | [diff] [blame] | 2437 | ulittle16_t SymKind = ulittle16_t(S_DEFRANGE_REGISTER); |
| 2438 | BytePrefix += StringRef(reinterpret_cast<const char *>(&SymKind), |
| 2439 | sizeof(SymKind)); |
Zachary Turner | 46225b1 | 2016-12-16 22:48:14 +0000 | [diff] [blame] | 2440 | BytePrefix += StringRef(reinterpret_cast<const char *>(&Sym.Hdr), |
| 2441 | sizeof(Sym.Hdr)); |
Reid Kleckner | 2b3e642 | 2016-10-05 21:21:33 +0000 | [diff] [blame] | 2442 | } |
Reid Kleckner | 876330d | 2016-02-12 21:48:30 +0000 | [diff] [blame] | 2443 | } |
| 2444 | OS.EmitCVDefRangeDirective(DefRange.Ranges, BytePrefix); |
| 2445 | } |
Reid Kleckner | f9c275f | 2016-02-10 20:55:49 +0000 | [diff] [blame] | 2446 | } |
| 2447 | |
Reid Kleckner | 5a791ee | 2018-03-15 21:24:04 +0000 | [diff] [blame] | 2448 | void CodeViewDebug::emitLexicalBlockList(ArrayRef<LexicalBlock *> Blocks, |
| 2449 | const FunctionInfo& FI) { |
| 2450 | for (LexicalBlock *Block : Blocks) |
| 2451 | emitLexicalBlock(*Block, FI); |
| 2452 | } |
| 2453 | |
| 2454 | /// Emit an S_BLOCK32 and S_END record pair delimiting the contents of a |
| 2455 | /// lexical block scope. |
| 2456 | void CodeViewDebug::emitLexicalBlock(const LexicalBlock &Block, |
| 2457 | const FunctionInfo& FI) { |
| 2458 | MCSymbol *RecordBegin = MMI->getContext().createTempSymbol(), |
| 2459 | *RecordEnd = MMI->getContext().createTempSymbol(); |
| 2460 | |
| 2461 | // Lexical block symbol record. |
| 2462 | OS.AddComment("Record length"); |
| 2463 | OS.emitAbsoluteSymbolDiff(RecordEnd, RecordBegin, 2); // Record Length |
| 2464 | OS.EmitLabel(RecordBegin); |
| 2465 | OS.AddComment("Record kind: S_BLOCK32"); |
| 2466 | OS.EmitIntValue(SymbolKind::S_BLOCK32, 2); // Record Kind |
| 2467 | OS.AddComment("PtrParent"); |
| 2468 | OS.EmitIntValue(0, 4); // PtrParent |
| 2469 | OS.AddComment("PtrEnd"); |
| 2470 | OS.EmitIntValue(0, 4); // PtrEnd |
| 2471 | OS.AddComment("Code size"); |
| 2472 | OS.emitAbsoluteSymbolDiff(Block.End, Block.Begin, 4); // Code Size |
| 2473 | OS.AddComment("Function section relative address"); |
| 2474 | OS.EmitCOFFSecRel32(Block.Begin, /*Offset=*/0); // Func Offset |
| 2475 | OS.AddComment("Function section index"); |
| 2476 | OS.EmitCOFFSectionIndex(FI.Begin); // Func Symbol |
| 2477 | OS.AddComment("Lexical block name"); |
| 2478 | emitNullTerminatedSymbolName(OS, Block.Name); // Name |
| 2479 | OS.EmitLabel(RecordEnd); |
| 2480 | |
| 2481 | // Emit variables local to this lexical block. |
| 2482 | emitLocalVariableList(Block.Locals); |
| 2483 | |
| 2484 | // Emit lexical blocks contained within this block. |
| 2485 | emitLexicalBlockList(Block.Children, FI); |
| 2486 | |
| 2487 | // Close the lexical block scope. |
| 2488 | OS.AddComment("Record length"); |
| 2489 | OS.EmitIntValue(2, 2); // Record Length |
| 2490 | OS.AddComment("Record kind: S_END"); |
| 2491 | OS.EmitIntValue(SymbolKind::S_END, 2); // Record Kind |
| 2492 | } |
| 2493 | |
| 2494 | /// Convenience routine for collecting lexical block information for a list |
| 2495 | /// of lexical scopes. |
| 2496 | void CodeViewDebug::collectLexicalBlockInfo( |
| 2497 | SmallVectorImpl<LexicalScope *> &Scopes, |
| 2498 | SmallVectorImpl<LexicalBlock *> &Blocks, |
| 2499 | SmallVectorImpl<LocalVariable> &Locals) { |
| 2500 | for (LexicalScope *Scope : Scopes) |
| 2501 | collectLexicalBlockInfo(*Scope, Blocks, Locals); |
| 2502 | } |
| 2503 | |
| 2504 | /// Populate the lexical blocks and local variable lists of the parent with |
| 2505 | /// information about the specified lexical scope. |
| 2506 | void CodeViewDebug::collectLexicalBlockInfo( |
| 2507 | LexicalScope &Scope, |
| 2508 | SmallVectorImpl<LexicalBlock *> &ParentBlocks, |
| 2509 | SmallVectorImpl<LocalVariable> &ParentLocals) { |
| 2510 | if (Scope.isAbstractScope()) |
| 2511 | return; |
| 2512 | |
| 2513 | auto LocalsIter = ScopeVariables.find(&Scope); |
| 2514 | if (LocalsIter == ScopeVariables.end()) { |
| 2515 | // This scope does not contain variables and can be eliminated. |
| 2516 | collectLexicalBlockInfo(Scope.getChildren(), ParentBlocks, ParentLocals); |
| 2517 | return; |
| 2518 | } |
| 2519 | SmallVectorImpl<LocalVariable> &Locals = LocalsIter->second; |
| 2520 | |
| 2521 | const DILexicalBlock *DILB = dyn_cast<DILexicalBlock>(Scope.getScopeNode()); |
| 2522 | if (!DILB) { |
| 2523 | // This scope is not a lexical block and can be eliminated, but keep any |
| 2524 | // local variables it contains. |
| 2525 | ParentLocals.append(Locals.begin(), Locals.end()); |
| 2526 | collectLexicalBlockInfo(Scope.getChildren(), ParentBlocks, ParentLocals); |
| 2527 | return; |
| 2528 | } |
| 2529 | |
| 2530 | const SmallVectorImpl<InsnRange> &Ranges = Scope.getRanges(); |
| 2531 | if (Ranges.size() != 1 || !getLabelAfterInsn(Ranges.front().second)) { |
| 2532 | // This lexical block scope has too many address ranges to represent in the |
| 2533 | // current CodeView format or does not have a valid address range. |
| 2534 | // Eliminate this lexical scope and promote any locals it contains to the |
| 2535 | // parent scope. |
| 2536 | // |
| 2537 | // For lexical scopes with multiple address ranges you may be tempted to |
| 2538 | // construct a single range covering every instruction where the block is |
| 2539 | // live and everything in between. Unfortunately, Visual Studio only |
| 2540 | // displays variables from the first matching lexical block scope. If the |
| 2541 | // first lexical block contains exception handling code or cold code which |
| 2542 | // is moved to the bottom of the routine creating a single range covering |
| 2543 | // nearly the entire routine, then it will hide all other lexical blocks |
| 2544 | // and the variables they contain. |
| 2545 | // |
| 2546 | ParentLocals.append(Locals.begin(), Locals.end()); |
| 2547 | collectLexicalBlockInfo(Scope.getChildren(), ParentBlocks, ParentLocals); |
| 2548 | return; |
| 2549 | } |
| 2550 | |
| 2551 | // Create a new CodeView lexical block for this lexical scope. If we've |
| 2552 | // seen this DILexicalBlock before then the scope tree is malformed and |
| 2553 | // we can handle this gracefully by not processing it a second time. |
| 2554 | auto BlockInsertion = CurFn->LexicalBlocks.insert({DILB, LexicalBlock()}); |
| 2555 | if (!BlockInsertion.second) |
| 2556 | return; |
| 2557 | |
| 2558 | // Create a lexical block containing the local variables and collect the |
| 2559 | // the lexical block information for the children. |
| 2560 | const InsnRange &Range = Ranges.front(); |
| 2561 | assert(Range.first && Range.second); |
| 2562 | LexicalBlock &Block = BlockInsertion.first->second; |
| 2563 | Block.Begin = getLabelBeforeInsn(Range.first); |
| 2564 | Block.End = getLabelAfterInsn(Range.second); |
| 2565 | assert(Block.Begin && "missing label for scope begin"); |
| 2566 | assert(Block.End && "missing label for scope end"); |
| 2567 | Block.Name = DILB->getName(); |
| 2568 | Block.Locals = std::move(Locals); |
| 2569 | ParentBlocks.push_back(&Block); |
| 2570 | collectLexicalBlockInfo(Scope.getChildren(), Block.Children, Block.Locals); |
| 2571 | } |
| 2572 | |
David Blaikie | b2fbb4b | 2017-02-16 18:48:33 +0000 | [diff] [blame] | 2573 | void CodeViewDebug::endFunctionImpl(const MachineFunction *MF) { |
Matthias Braun | f1caa28 | 2017-12-15 22:22:58 +0000 | [diff] [blame] | 2574 | const Function &GV = MF->getFunction(); |
| 2575 | assert(FnDebugInfo.count(&GV)); |
Reid Kleckner | 55baeef | 2018-03-15 21:12:21 +0000 | [diff] [blame] | 2576 | assert(CurFn == FnDebugInfo[&GV].get()); |
Timur Iskhodzhanov | b5b7a61 | 2014-03-26 11:24:36 +0000 | [diff] [blame] | 2577 | |
Matthias Braun | f1caa28 | 2017-12-15 22:22:58 +0000 | [diff] [blame] | 2578 | collectVariableInfo(GV.getSubprogram()); |
Reid Kleckner | 876330d | 2016-02-12 21:48:30 +0000 | [diff] [blame] | 2579 | |
Reid Kleckner | 5a791ee | 2018-03-15 21:24:04 +0000 | [diff] [blame] | 2580 | // Build the lexical block structure to emit for this routine. |
| 2581 | if (LexicalScope *CFS = LScopes.getCurrentFunctionScope()) |
| 2582 | collectLexicalBlockInfo(*CFS, CurFn->ChildBlocks, CurFn->Locals); |
| 2583 | |
| 2584 | // Clear the scope and variable information from the map which will not be |
| 2585 | // valid after we have finished processing this routine. This also prepares |
| 2586 | // the map for the subsequent routine. |
| 2587 | ScopeVariables.clear(); |
| 2588 | |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 2589 | // Don't emit anything if we don't have any line tables. |
Brock Wyma | 94ece8f | 2018-04-16 16:53:57 +0000 | [diff] [blame] | 2590 | // Thunks are compiler-generated and probably won't have source correlation. |
| 2591 | if (!CurFn->HaveLineInfo && !GV.getSubprogram()->isThunk()) { |
Matthias Braun | f1caa28 | 2017-12-15 22:22:58 +0000 | [diff] [blame] | 2592 | FnDebugInfo.erase(&GV); |
Reid Kleckner | f9c275f | 2016-02-10 20:55:49 +0000 | [diff] [blame] | 2593 | CurFn = nullptr; |
| 2594 | return; |
Timur Iskhodzhanov | 8499a12 | 2014-03-26 09:50:36 +0000 | [diff] [blame] | 2595 | } |
Reid Kleckner | f9c275f | 2016-02-10 20:55:49 +0000 | [diff] [blame] | 2596 | |
Reid Kleckner | e33c94f | 2017-09-05 20:14:58 +0000 | [diff] [blame] | 2597 | CurFn->Annotations = MF->getCodeViewAnnotations(); |
| 2598 | |
Reid Kleckner | f9c275f | 2016-02-10 20:55:49 +0000 | [diff] [blame] | 2599 | CurFn->End = Asm->getFunctionEnd(); |
| 2600 | |
Craig Topper | 353eda4 | 2014-04-24 06:44:33 +0000 | [diff] [blame] | 2601 | CurFn = nullptr; |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 2602 | } |
| 2603 | |
Reid Kleckner | 70f5bc9 | 2016-01-14 19:25:04 +0000 | [diff] [blame] | 2604 | void CodeViewDebug::beginInstruction(const MachineInstr *MI) { |
Reid Kleckner | f9c275f | 2016-02-10 20:55:49 +0000 | [diff] [blame] | 2605 | DebugHandlerBase::beginInstruction(MI); |
| 2606 | |
Shiva Chen | 801bf7e | 2018-05-09 02:42:00 +0000 | [diff] [blame] | 2607 | // Ignore DBG_VALUE and DBG_LABEL locations and function prologue. |
| 2608 | if (!Asm || !CurFn || MI->isDebugInstr() || |
David Majnemer | 67f684e | 2016-07-28 05:03:22 +0000 | [diff] [blame] | 2609 | MI->getFlag(MachineInstr::FrameSetup)) |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 2610 | return; |
Reid Kleckner | 45a7462 | 2017-06-30 21:33:44 +0000 | [diff] [blame] | 2611 | |
| 2612 | // If the first instruction of a new MBB has no location, find the first |
| 2613 | // instruction with a location and use that. |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 2614 | DebugLoc DL = MI->getDebugLoc(); |
Reid Kleckner | 45a7462 | 2017-06-30 21:33:44 +0000 | [diff] [blame] | 2615 | if (!DL && MI->getParent() != PrevInstBB) { |
| 2616 | for (const auto &NextMI : *MI->getParent()) { |
Shiva Chen | 801bf7e | 2018-05-09 02:42:00 +0000 | [diff] [blame] | 2617 | if (NextMI.isDebugInstr()) |
Reid Kleckner | 2de471d | 2017-07-31 21:03:08 +0000 | [diff] [blame] | 2618 | continue; |
Reid Kleckner | 45a7462 | 2017-06-30 21:33:44 +0000 | [diff] [blame] | 2619 | DL = NextMI.getDebugLoc(); |
| 2620 | if (DL) |
| 2621 | break; |
| 2622 | } |
| 2623 | } |
| 2624 | PrevInstBB = MI->getParent(); |
| 2625 | |
| 2626 | // If we still don't have a debug location, don't record a location. |
| 2627 | if (!DL) |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 2628 | return; |
Reid Kleckner | 45a7462 | 2017-06-30 21:33:44 +0000 | [diff] [blame] | 2629 | |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 2630 | maybeRecordLocation(DL, Asm->MF); |
| 2631 | } |
Reid Kleckner | 6f3406d | 2016-06-07 00:02:03 +0000 | [diff] [blame] | 2632 | |
Zachary Turner | 8c099fe | 2017-05-30 16:36:15 +0000 | [diff] [blame] | 2633 | MCSymbol *CodeViewDebug::beginCVSubsection(DebugSubsectionKind Kind) { |
Reid Kleckner | 6f3406d | 2016-06-07 00:02:03 +0000 | [diff] [blame] | 2634 | MCSymbol *BeginLabel = MMI->getContext().createTempSymbol(), |
| 2635 | *EndLabel = MMI->getContext().createTempSymbol(); |
| 2636 | OS.EmitIntValue(unsigned(Kind), 4); |
| 2637 | OS.AddComment("Subsection size"); |
| 2638 | OS.emitAbsoluteSymbolDiff(EndLabel, BeginLabel, 4); |
| 2639 | OS.EmitLabel(BeginLabel); |
| 2640 | return EndLabel; |
| 2641 | } |
| 2642 | |
| 2643 | void CodeViewDebug::endCVSubsection(MCSymbol *EndLabel) { |
| 2644 | OS.EmitLabel(EndLabel); |
| 2645 | // Every subsection must be aligned to a 4-byte boundary. |
| 2646 | OS.EmitValueToAlignment(4); |
| 2647 | } |
| 2648 | |
David Majnemer | 3128b10 | 2016-06-15 18:00:01 +0000 | [diff] [blame] | 2649 | void CodeViewDebug::emitDebugInfoForUDTs( |
Zachary Turner | a7b0417 | 2017-08-28 18:49:04 +0000 | [diff] [blame] | 2650 | ArrayRef<std::pair<std::string, const DIType *>> UDTs) { |
| 2651 | for (const auto &UDT : UDTs) { |
| 2652 | const DIType *T = UDT.second; |
| 2653 | assert(shouldEmitUdt(T)); |
| 2654 | |
David Majnemer | 3128b10 | 2016-06-15 18:00:01 +0000 | [diff] [blame] | 2655 | MCSymbol *UDTRecordBegin = MMI->getContext().createTempSymbol(), |
| 2656 | *UDTRecordEnd = MMI->getContext().createTempSymbol(); |
| 2657 | OS.AddComment("Record length"); |
| 2658 | OS.emitAbsoluteSymbolDiff(UDTRecordEnd, UDTRecordBegin, 2); |
| 2659 | OS.EmitLabel(UDTRecordBegin); |
| 2660 | |
| 2661 | OS.AddComment("Record kind: S_UDT"); |
| 2662 | OS.EmitIntValue(unsigned(SymbolKind::S_UDT), 2); |
| 2663 | |
| 2664 | OS.AddComment("Type"); |
Zachary Turner | a7b0417 | 2017-08-28 18:49:04 +0000 | [diff] [blame] | 2665 | OS.EmitIntValue(getCompleteTypeIndex(T).getIndex(), 4); |
David Majnemer | 3128b10 | 2016-06-15 18:00:01 +0000 | [diff] [blame] | 2666 | |
| 2667 | emitNullTerminatedSymbolName(OS, UDT.first); |
| 2668 | OS.EmitLabel(UDTRecordEnd); |
| 2669 | } |
| 2670 | } |
| 2671 | |
Reid Kleckner | 6f3406d | 2016-06-07 00:02:03 +0000 | [diff] [blame] | 2672 | void CodeViewDebug::emitDebugInfoForGlobals() { |
Adrian Prantl | bceaaa9 | 2016-12-20 02:09:43 +0000 | [diff] [blame] | 2673 | DenseMap<const DIGlobalVariableExpression *, const GlobalVariable *> |
| 2674 | GlobalMap; |
Peter Collingbourne | d4135bb | 2016-09-13 01:12:59 +0000 | [diff] [blame] | 2675 | for (const GlobalVariable &GV : MMI->getModule()->globals()) { |
Adrian Prantl | bceaaa9 | 2016-12-20 02:09:43 +0000 | [diff] [blame] | 2676 | SmallVector<DIGlobalVariableExpression *, 1> GVEs; |
| 2677 | GV.getDebugInfo(GVEs); |
| 2678 | for (const auto *GVE : GVEs) |
| 2679 | GlobalMap[GVE] = &GV; |
Peter Collingbourne | d4135bb | 2016-09-13 01:12:59 +0000 | [diff] [blame] | 2680 | } |
| 2681 | |
Reid Kleckner | 6f3406d | 2016-06-07 00:02:03 +0000 | [diff] [blame] | 2682 | NamedMDNode *CUs = MMI->getModule()->getNamedMetadata("llvm.dbg.cu"); |
| 2683 | for (const MDNode *Node : CUs->operands()) { |
| 2684 | const auto *CU = cast<DICompileUnit>(Node); |
| 2685 | |
| 2686 | // First, emit all globals that are not in a comdat in a single symbol |
| 2687 | // substream. MSVC doesn't like it if the substream is empty, so only open |
| 2688 | // it if we have at least one global to emit. |
| 2689 | switchToDebugSectionForSymbol(nullptr); |
| 2690 | MCSymbol *EndLabel = nullptr; |
Adrian Prantl | bceaaa9 | 2016-12-20 02:09:43 +0000 | [diff] [blame] | 2691 | for (const auto *GVE : CU->getGlobalVariables()) { |
| 2692 | if (const auto *GV = GlobalMap.lookup(GVE)) |
David Majnemer | 577be0f | 2016-06-15 00:19:52 +0000 | [diff] [blame] | 2693 | if (!GV->hasComdat() && !GV->isDeclarationForLinker()) { |
Reid Kleckner | 6f3406d | 2016-06-07 00:02:03 +0000 | [diff] [blame] | 2694 | if (!EndLabel) { |
| 2695 | OS.AddComment("Symbol subsection for globals"); |
Zachary Turner | 8c099fe | 2017-05-30 16:36:15 +0000 | [diff] [blame] | 2696 | EndLabel = beginCVSubsection(DebugSubsectionKind::Symbols); |
Reid Kleckner | 6f3406d | 2016-06-07 00:02:03 +0000 | [diff] [blame] | 2697 | } |
Adrian Prantl | bceaaa9 | 2016-12-20 02:09:43 +0000 | [diff] [blame] | 2698 | // FIXME: emitDebugInfoForGlobal() doesn't handle DIExpressions. |
| 2699 | emitDebugInfoForGlobal(GVE->getVariable(), GV, Asm->getSymbol(GV)); |
Reid Kleckner | 6f3406d | 2016-06-07 00:02:03 +0000 | [diff] [blame] | 2700 | } |
| 2701 | } |
| 2702 | if (EndLabel) |
| 2703 | endCVSubsection(EndLabel); |
| 2704 | |
| 2705 | // Second, emit each global that is in a comdat into its own .debug$S |
| 2706 | // section along with its own symbol substream. |
Adrian Prantl | bceaaa9 | 2016-12-20 02:09:43 +0000 | [diff] [blame] | 2707 | for (const auto *GVE : CU->getGlobalVariables()) { |
| 2708 | if (const auto *GV = GlobalMap.lookup(GVE)) { |
Reid Kleckner | 6f3406d | 2016-06-07 00:02:03 +0000 | [diff] [blame] | 2709 | if (GV->hasComdat()) { |
| 2710 | MCSymbol *GVSym = Asm->getSymbol(GV); |
| 2711 | OS.AddComment("Symbol subsection for " + |
Peter Collingbourne | 6f0ecca | 2017-05-16 00:39:01 +0000 | [diff] [blame] | 2712 | Twine(GlobalValue::dropLLVMManglingEscape(GV->getName()))); |
Reid Kleckner | 6f3406d | 2016-06-07 00:02:03 +0000 | [diff] [blame] | 2713 | switchToDebugSectionForSymbol(GVSym); |
Zachary Turner | 8c099fe | 2017-05-30 16:36:15 +0000 | [diff] [blame] | 2714 | EndLabel = beginCVSubsection(DebugSubsectionKind::Symbols); |
Adrian Prantl | bceaaa9 | 2016-12-20 02:09:43 +0000 | [diff] [blame] | 2715 | // FIXME: emitDebugInfoForGlobal() doesn't handle DIExpressions. |
| 2716 | emitDebugInfoForGlobal(GVE->getVariable(), GV, GVSym); |
Reid Kleckner | 6f3406d | 2016-06-07 00:02:03 +0000 | [diff] [blame] | 2717 | endCVSubsection(EndLabel); |
| 2718 | } |
| 2719 | } |
| 2720 | } |
| 2721 | } |
| 2722 | } |
| 2723 | |
Hans Wennborg | b510b45 | 2016-06-23 16:33:53 +0000 | [diff] [blame] | 2724 | void CodeViewDebug::emitDebugInfoForRetainedTypes() { |
| 2725 | NamedMDNode *CUs = MMI->getModule()->getNamedMetadata("llvm.dbg.cu"); |
| 2726 | for (const MDNode *Node : CUs->operands()) { |
| 2727 | for (auto *Ty : cast<DICompileUnit>(Node)->getRetainedTypes()) { |
| 2728 | if (DIType *RT = dyn_cast<DIType>(Ty)) { |
| 2729 | getTypeIndex(RT); |
| 2730 | // FIXME: Add to global/local DTU list. |
| 2731 | } |
| 2732 | } |
| 2733 | } |
| 2734 | } |
| 2735 | |
Reid Kleckner | 6f3406d | 2016-06-07 00:02:03 +0000 | [diff] [blame] | 2736 | void CodeViewDebug::emitDebugInfoForGlobal(const DIGlobalVariable *DIGV, |
Peter Collingbourne | d4135bb | 2016-09-13 01:12:59 +0000 | [diff] [blame] | 2737 | const GlobalVariable *GV, |
Reid Kleckner | 6f3406d | 2016-06-07 00:02:03 +0000 | [diff] [blame] | 2738 | MCSymbol *GVSym) { |
| 2739 | // DataSym record, see SymbolRecord.h for more info. |
| 2740 | // FIXME: Thread local data, etc |
| 2741 | MCSymbol *DataBegin = MMI->getContext().createTempSymbol(), |
| 2742 | *DataEnd = MMI->getContext().createTempSymbol(); |
Brock Wyma | 19e17b3 | 2018-02-11 21:26:46 +0000 | [diff] [blame] | 2743 | const unsigned FixedLengthOfThisRecord = 12; |
Reid Kleckner | 6f3406d | 2016-06-07 00:02:03 +0000 | [diff] [blame] | 2744 | OS.AddComment("Record length"); |
| 2745 | OS.emitAbsoluteSymbolDiff(DataEnd, DataBegin, 2); |
| 2746 | OS.EmitLabel(DataBegin); |
David Majnemer | 7abd269 | 2016-07-06 21:07:47 +0000 | [diff] [blame] | 2747 | if (DIGV->isLocalToUnit()) { |
David Majnemer | a54fe1a | 2016-07-07 05:14:21 +0000 | [diff] [blame] | 2748 | if (GV->isThreadLocal()) { |
| 2749 | OS.AddComment("Record kind: S_LTHREAD32"); |
| 2750 | OS.EmitIntValue(unsigned(SymbolKind::S_LTHREAD32), 2); |
| 2751 | } else { |
| 2752 | OS.AddComment("Record kind: S_LDATA32"); |
| 2753 | OS.EmitIntValue(unsigned(SymbolKind::S_LDATA32), 2); |
| 2754 | } |
David Majnemer | 7abd269 | 2016-07-06 21:07:47 +0000 | [diff] [blame] | 2755 | } else { |
David Majnemer | a54fe1a | 2016-07-07 05:14:21 +0000 | [diff] [blame] | 2756 | if (GV->isThreadLocal()) { |
| 2757 | OS.AddComment("Record kind: S_GTHREAD32"); |
| 2758 | OS.EmitIntValue(unsigned(SymbolKind::S_GTHREAD32), 2); |
| 2759 | } else { |
| 2760 | OS.AddComment("Record kind: S_GDATA32"); |
| 2761 | OS.EmitIntValue(unsigned(SymbolKind::S_GDATA32), 2); |
| 2762 | } |
David Majnemer | 7abd269 | 2016-07-06 21:07:47 +0000 | [diff] [blame] | 2763 | } |
Reid Kleckner | 6f3406d | 2016-06-07 00:02:03 +0000 | [diff] [blame] | 2764 | OS.AddComment("Type"); |
| 2765 | OS.EmitIntValue(getCompleteTypeIndex(DIGV->getType()).getIndex(), 4); |
| 2766 | OS.AddComment("DataOffset"); |
Keno Fischer | f7d84ee | 2017-01-02 03:00:19 +0000 | [diff] [blame] | 2767 | OS.EmitCOFFSecRel32(GVSym, /*Offset=*/0); |
Reid Kleckner | 6f3406d | 2016-06-07 00:02:03 +0000 | [diff] [blame] | 2768 | OS.AddComment("Segment"); |
| 2769 | OS.EmitCOFFSectionIndex(GVSym); |
| 2770 | OS.AddComment("Name"); |
Brock Wyma | 19e17b3 | 2018-02-11 21:26:46 +0000 | [diff] [blame] | 2771 | emitNullTerminatedSymbolName(OS, DIGV->getName(), FixedLengthOfThisRecord); |
Reid Kleckner | 6f3406d | 2016-06-07 00:02:03 +0000 | [diff] [blame] | 2772 | OS.EmitLabel(DataEnd); |
| 2773 | } |