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