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