Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 1 | //===- MCCodeView.h - Machine Code CodeView support -------------*- C++ -*-===// |
| 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 |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // Holds state from .cv_file and .cv_loc directives for later emission. |
| 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
| 13 | #include "llvm/MC/MCCodeView.h" |
| 14 | #include "llvm/ADT/STLExtras.h" |
Reid Kleckner | 26fa1bf | 2017-09-19 18:14:45 +0000 | [diff] [blame] | 15 | #include "llvm/ADT/StringExtras.h" |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 16 | #include "llvm/DebugInfo/CodeView/CodeView.h" |
| 17 | #include "llvm/DebugInfo/CodeView/Line.h" |
David Majnemer | 6fcbd7e | 2016-01-29 19:24:12 +0000 | [diff] [blame] | 18 | #include "llvm/DebugInfo/CodeView/SymbolRecord.h" |
Zachary Turner | d427383 | 2017-05-30 21:53:05 +0000 | [diff] [blame] | 19 | #include "llvm/MC/MCAsmLayout.h" |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 20 | #include "llvm/MC/MCContext.h" |
| 21 | #include "llvm/MC/MCObjectStreamer.h" |
David Majnemer | 408b5e6 | 2016-02-05 01:55:49 +0000 | [diff] [blame] | 22 | #include "llvm/MC/MCValue.h" |
Reid Kleckner | 858239d | 2016-06-22 23:23:08 +0000 | [diff] [blame] | 23 | #include "llvm/Support/EndianStream.h" |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 24 | |
| 25 | using namespace llvm; |
| 26 | using namespace llvm::codeview; |
| 27 | |
| 28 | CodeViewContext::CodeViewContext() {} |
| 29 | |
| 30 | CodeViewContext::~CodeViewContext() { |
| 31 | // If someone inserted strings into the string table but never actually |
| 32 | // emitted them somewhere, clean up the fragment. |
| 33 | if (!InsertedStrTabFragment) |
| 34 | delete StrTabFragment; |
| 35 | } |
| 36 | |
| 37 | /// This is a valid number for use with .cv_loc if we've already seen a .cv_file |
| 38 | /// for it. |
| 39 | bool CodeViewContext::isValidFileNumber(unsigned FileNumber) const { |
| 40 | unsigned Idx = FileNumber - 1; |
Reid Kleckner | 26fa1bf | 2017-09-19 18:14:45 +0000 | [diff] [blame] | 41 | if (Idx < Files.size()) |
| 42 | return Files[Idx].Assigned; |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 43 | return false; |
| 44 | } |
| 45 | |
Reid Kleckner | 26fa1bf | 2017-09-19 18:14:45 +0000 | [diff] [blame] | 46 | bool CodeViewContext::addFile(MCStreamer &OS, unsigned FileNumber, |
| 47 | StringRef Filename, |
| 48 | ArrayRef<uint8_t> ChecksumBytes, |
| 49 | uint8_t ChecksumKind) { |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 50 | assert(FileNumber > 0); |
Reid Kleckner | 26fa1bf | 2017-09-19 18:14:45 +0000 | [diff] [blame] | 51 | auto FilenameOffset = addToStringTable(Filename); |
| 52 | Filename = FilenameOffset.first; |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 53 | unsigned Idx = FileNumber - 1; |
Reid Kleckner | 26fa1bf | 2017-09-19 18:14:45 +0000 | [diff] [blame] | 54 | if (Idx >= Files.size()) |
| 55 | Files.resize(Idx + 1); |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 56 | |
| 57 | if (Filename.empty()) |
| 58 | Filename = "<stdin>"; |
| 59 | |
Reid Kleckner | 26fa1bf | 2017-09-19 18:14:45 +0000 | [diff] [blame] | 60 | if (Files[Idx].Assigned) |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 61 | return false; |
| 62 | |
Reid Kleckner | 26fa1bf | 2017-09-19 18:14:45 +0000 | [diff] [blame] | 63 | FilenameOffset = addToStringTable(Filename); |
| 64 | Filename = FilenameOffset.first; |
| 65 | unsigned Offset = FilenameOffset.second; |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 66 | |
Reid Kleckner | 26fa1bf | 2017-09-19 18:14:45 +0000 | [diff] [blame] | 67 | auto ChecksumOffsetSymbol = |
| 68 | OS.getContext().createTempSymbol("checksum_offset", false); |
| 69 | Files[Idx].StringTableOffset = Offset; |
| 70 | Files[Idx].ChecksumTableOffset = ChecksumOffsetSymbol; |
| 71 | Files[Idx].Assigned = true; |
| 72 | Files[Idx].Checksum = ChecksumBytes; |
| 73 | Files[Idx].ChecksumKind = ChecksumKind; |
| 74 | |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 75 | return true; |
| 76 | } |
| 77 | |
Reid Kleckner | b525872 | 2018-01-18 22:55:14 +0000 | [diff] [blame] | 78 | MCCVFunctionInfo *CodeViewContext::getCVFunctionInfo(unsigned FuncId) { |
| 79 | if (FuncId >= Functions.size()) |
| 80 | return nullptr; |
| 81 | if (Functions[FuncId].isUnallocatedFunctionInfo()) |
| 82 | return nullptr; |
| 83 | return &Functions[FuncId]; |
| 84 | } |
| 85 | |
Reid Kleckner | a9f4cc9 | 2016-09-07 16:15:31 +0000 | [diff] [blame] | 86 | bool CodeViewContext::recordFunctionId(unsigned FuncId) { |
| 87 | if (FuncId >= Functions.size()) |
| 88 | Functions.resize(FuncId + 1); |
| 89 | |
| 90 | // Return false if this function info was already allocated. |
| 91 | if (!Functions[FuncId].isUnallocatedFunctionInfo()) |
| 92 | return false; |
| 93 | |
| 94 | // Mark this as an allocated normal function, and leave the rest alone. |
| 95 | Functions[FuncId].ParentFuncIdPlusOne = MCCVFunctionInfo::FunctionSentinel; |
| 96 | return true; |
| 97 | } |
| 98 | |
| 99 | bool CodeViewContext::recordInlinedCallSiteId(unsigned FuncId, unsigned IAFunc, |
| 100 | unsigned IAFile, unsigned IALine, |
| 101 | unsigned IACol) { |
| 102 | if (FuncId >= Functions.size()) |
| 103 | Functions.resize(FuncId + 1); |
| 104 | |
| 105 | // Return false if this function info was already allocated. |
| 106 | if (!Functions[FuncId].isUnallocatedFunctionInfo()) |
| 107 | return false; |
| 108 | |
| 109 | MCCVFunctionInfo::LineInfo InlinedAt; |
| 110 | InlinedAt.File = IAFile; |
| 111 | InlinedAt.Line = IALine; |
| 112 | InlinedAt.Col = IACol; |
| 113 | |
| 114 | // Mark this as an inlined call site and record call site line info. |
| 115 | MCCVFunctionInfo *Info = &Functions[FuncId]; |
| 116 | Info->ParentFuncIdPlusOne = IAFunc + 1; |
| 117 | Info->InlinedAt = InlinedAt; |
| 118 | |
| 119 | // Walk up the call chain adding this function id to the InlinedAtMap of all |
| 120 | // transitive callers until we hit a real function. |
| 121 | while (Info->isInlinedCallSite()) { |
| 122 | InlinedAt = Info->InlinedAt; |
| 123 | Info = getCVFunctionInfo(Info->getParentFuncId()); |
| 124 | Info->InlinedAtMap[FuncId] = InlinedAt; |
| 125 | } |
| 126 | |
| 127 | return true; |
| 128 | } |
| 129 | |
Reid Kleckner | 689f773 | 2018-08-28 23:25:59 +0000 | [diff] [blame] | 130 | void CodeViewContext::recordCVLoc(MCContext &Ctx, const MCSymbol *Label, |
| 131 | unsigned FunctionId, unsigned FileNo, |
| 132 | unsigned Line, unsigned Column, |
| 133 | bool PrologueEnd, bool IsStmt) { |
| 134 | addLineEntry(MCCVLoc{ |
| 135 | Label, FunctionId, FileNo, Line, Column, PrologueEnd, IsStmt}); |
| 136 | } |
| 137 | |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 138 | MCDataFragment *CodeViewContext::getStringTableFragment() { |
| 139 | if (!StrTabFragment) { |
| 140 | StrTabFragment = new MCDataFragment(); |
| 141 | // Start a new string table out with a null byte. |
| 142 | StrTabFragment->getContents().push_back('\0'); |
| 143 | } |
| 144 | return StrTabFragment; |
| 145 | } |
| 146 | |
Reid Kleckner | 26fa1bf | 2017-09-19 18:14:45 +0000 | [diff] [blame] | 147 | std::pair<StringRef, unsigned> CodeViewContext::addToStringTable(StringRef S) { |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 148 | SmallVectorImpl<char> &Contents = getStringTableFragment()->getContents(); |
| 149 | auto Insertion = |
| 150 | StringTable.insert(std::make_pair(S, unsigned(Contents.size()))); |
| 151 | // Return the string from the table, since it is stable. |
Reid Kleckner | 26fa1bf | 2017-09-19 18:14:45 +0000 | [diff] [blame] | 152 | std::pair<StringRef, unsigned> Ret = |
| 153 | std::make_pair(Insertion.first->first(), Insertion.first->second); |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 154 | if (Insertion.second) { |
| 155 | // The string map key is always null terminated. |
Reid Kleckner | 26fa1bf | 2017-09-19 18:14:45 +0000 | [diff] [blame] | 156 | Contents.append(Ret.first.begin(), Ret.first.end() + 1); |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 157 | } |
Reid Kleckner | 26fa1bf | 2017-09-19 18:14:45 +0000 | [diff] [blame] | 158 | return Ret; |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | unsigned CodeViewContext::getStringTableOffset(StringRef S) { |
| 162 | // A string table offset of zero is always the empty string. |
| 163 | if (S.empty()) |
| 164 | return 0; |
| 165 | auto I = StringTable.find(S); |
| 166 | assert(I != StringTable.end()); |
| 167 | return I->second; |
| 168 | } |
| 169 | |
| 170 | void CodeViewContext::emitStringTable(MCObjectStreamer &OS) { |
| 171 | MCContext &Ctx = OS.getContext(); |
David Blaikie | a0b44ef | 2016-01-29 02:23:13 +0000 | [diff] [blame] | 172 | MCSymbol *StringBegin = Ctx.createTempSymbol("strtab_begin", false), |
| 173 | *StringEnd = Ctx.createTempSymbol("strtab_end", false); |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 174 | |
Fangrui Song | 692e0c9 | 2020-02-29 08:25:22 -0800 | [diff] [blame] | 175 | OS.emitInt32(uint32_t(DebugSubsectionKind::StringTable)); |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 176 | OS.emitAbsoluteSymbolDiff(StringEnd, StringBegin, 4); |
Fangrui Song | 6d2d589 | 2020-02-14 19:21:58 -0800 | [diff] [blame] | 177 | OS.emitLabel(StringBegin); |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 178 | |
| 179 | // Put the string table data fragment here, if we haven't already put it |
| 180 | // somewhere else. If somebody wants two string tables in their .s file, one |
| 181 | // will just be empty. |
| 182 | if (!InsertedStrTabFragment) { |
| 183 | OS.insert(getStringTableFragment()); |
| 184 | InsertedStrTabFragment = true; |
| 185 | } |
| 186 | |
Fangrui Song | 6d2d589 | 2020-02-14 19:21:58 -0800 | [diff] [blame] | 187 | OS.emitValueToAlignment(4, 0); |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 188 | |
Fangrui Song | 6d2d589 | 2020-02-14 19:21:58 -0800 | [diff] [blame] | 189 | OS.emitLabel(StringEnd); |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | void CodeViewContext::emitFileChecksums(MCObjectStreamer &OS) { |
Reid Kleckner | ee641c2 | 2016-06-08 17:50:29 +0000 | [diff] [blame] | 193 | // Do nothing if there are no file checksums. Microsoft's linker rejects empty |
| 194 | // CodeView substreams. |
Reid Kleckner | 26fa1bf | 2017-09-19 18:14:45 +0000 | [diff] [blame] | 195 | if (Files.empty()) |
Reid Kleckner | ee641c2 | 2016-06-08 17:50:29 +0000 | [diff] [blame] | 196 | return; |
| 197 | |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 198 | MCContext &Ctx = OS.getContext(); |
David Blaikie | a0b44ef | 2016-01-29 02:23:13 +0000 | [diff] [blame] | 199 | MCSymbol *FileBegin = Ctx.createTempSymbol("filechecksums_begin", false), |
| 200 | *FileEnd = Ctx.createTempSymbol("filechecksums_end", false); |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 201 | |
Fangrui Song | 692e0c9 | 2020-02-29 08:25:22 -0800 | [diff] [blame] | 202 | OS.emitInt32(uint32_t(DebugSubsectionKind::FileChecksums)); |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 203 | OS.emitAbsoluteSymbolDiff(FileEnd, FileBegin, 4); |
Fangrui Song | 6d2d589 | 2020-02-14 19:21:58 -0800 | [diff] [blame] | 204 | OS.emitLabel(FileBegin); |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 205 | |
Reid Kleckner | 26fa1bf | 2017-09-19 18:14:45 +0000 | [diff] [blame] | 206 | unsigned CurrentOffset = 0; |
| 207 | |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 208 | // Emit an array of FileChecksum entries. We index into this table using the |
Reid Kleckner | 26fa1bf | 2017-09-19 18:14:45 +0000 | [diff] [blame] | 209 | // user-provided file number. Each entry may be a variable number of bytes |
| 210 | // determined by the checksum kind and size. |
| 211 | for (auto File : Files) { |
Fangrui Song | a55daa1 | 2020-02-14 18:16:24 -0800 | [diff] [blame] | 212 | OS.emitAssignment(File.ChecksumTableOffset, |
Reid Kleckner | 26fa1bf | 2017-09-19 18:14:45 +0000 | [diff] [blame] | 213 | MCConstantExpr::create(CurrentOffset, Ctx)); |
| 214 | CurrentOffset += 4; // String table offset. |
| 215 | if (!File.ChecksumKind) { |
| 216 | CurrentOffset += |
| 217 | 4; // One byte each for checksum size and kind, then align to 4 bytes. |
| 218 | } else { |
| 219 | CurrentOffset += 2; // One byte each for checksum size and kind. |
| 220 | CurrentOffset += File.Checksum.size(); |
| 221 | CurrentOffset = alignTo(CurrentOffset, 4); |
| 222 | } |
| 223 | |
Fangrui Song | 692e0c9 | 2020-02-29 08:25:22 -0800 | [diff] [blame] | 224 | OS.emitInt32(File.StringTableOffset); |
Reid Kleckner | 26fa1bf | 2017-09-19 18:14:45 +0000 | [diff] [blame] | 225 | |
| 226 | if (!File.ChecksumKind) { |
| 227 | // There is no checksum. Therefore zero the next two fields and align |
| 228 | // back to 4 bytes. |
Fangrui Song | 692e0c9 | 2020-02-29 08:25:22 -0800 | [diff] [blame] | 229 | OS.emitInt32(0); |
Reid Kleckner | 26fa1bf | 2017-09-19 18:14:45 +0000 | [diff] [blame] | 230 | continue; |
| 231 | } |
Fangrui Song | 692e0c9 | 2020-02-29 08:25:22 -0800 | [diff] [blame] | 232 | OS.emitInt8(static_cast<uint8_t>(File.Checksum.size())); |
| 233 | OS.emitInt8(File.ChecksumKind); |
Fangrui Song | a55daa1 | 2020-02-14 18:16:24 -0800 | [diff] [blame] | 234 | OS.emitBytes(toStringRef(File.Checksum)); |
Fangrui Song | 6d2d589 | 2020-02-14 19:21:58 -0800 | [diff] [blame] | 235 | OS.emitValueToAlignment(4); |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 236 | } |
| 237 | |
Fangrui Song | 6d2d589 | 2020-02-14 19:21:58 -0800 | [diff] [blame] | 238 | OS.emitLabel(FileEnd); |
Reid Kleckner | 26fa1bf | 2017-09-19 18:14:45 +0000 | [diff] [blame] | 239 | |
| 240 | ChecksumOffsetsAssigned = true; |
| 241 | } |
| 242 | |
| 243 | // Output checksum table offset of the given file number. It is possible that |
| 244 | // not all files have been registered yet, and so the offset cannot be |
| 245 | // calculated. In this case a symbol representing the offset is emitted, and |
| 246 | // the value of this symbol will be fixed up at a later time. |
| 247 | void CodeViewContext::emitFileChecksumOffset(MCObjectStreamer &OS, |
| 248 | unsigned FileNo) { |
| 249 | unsigned Idx = FileNo - 1; |
| 250 | |
| 251 | if (Idx >= Files.size()) |
| 252 | Files.resize(Idx + 1); |
| 253 | |
| 254 | if (ChecksumOffsetsAssigned) { |
Fangrui Song | 6d2d589 | 2020-02-14 19:21:58 -0800 | [diff] [blame] | 255 | OS.emitSymbolValue(Files[Idx].ChecksumTableOffset, 4); |
Reid Kleckner | 26fa1bf | 2017-09-19 18:14:45 +0000 | [diff] [blame] | 256 | return; |
| 257 | } |
| 258 | |
| 259 | const MCSymbolRefExpr *SRE = |
| 260 | MCSymbolRefExpr::create(Files[Idx].ChecksumTableOffset, OS.getContext()); |
| 261 | |
Fangrui Song | 6d2d589 | 2020-02-14 19:21:58 -0800 | [diff] [blame] | 262 | OS.emitValueImpl(SRE, 4); |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 263 | } |
| 264 | |
Reid Kleckner | 689f773 | 2018-08-28 23:25:59 +0000 | [diff] [blame] | 265 | void CodeViewContext::addLineEntry(const MCCVLoc &LineEntry) { |
Reid Kleckner | b525872 | 2018-01-18 22:55:14 +0000 | [diff] [blame] | 266 | size_t Offset = MCCVLines.size(); |
| 267 | auto I = MCCVLineStartStop.insert( |
| 268 | {LineEntry.getFunctionId(), {Offset, Offset + 1}}); |
| 269 | if (!I.second) |
| 270 | I.first->second.second = Offset + 1; |
| 271 | MCCVLines.push_back(LineEntry); |
| 272 | } |
| 273 | |
Reid Kleckner | 689f773 | 2018-08-28 23:25:59 +0000 | [diff] [blame] | 274 | std::vector<MCCVLoc> |
Reid Kleckner | b525872 | 2018-01-18 22:55:14 +0000 | [diff] [blame] | 275 | CodeViewContext::getFunctionLineEntries(unsigned FuncId) { |
Reid Kleckner | 689f773 | 2018-08-28 23:25:59 +0000 | [diff] [blame] | 276 | std::vector<MCCVLoc> FilteredLines; |
Reid Kleckner | b525872 | 2018-01-18 22:55:14 +0000 | [diff] [blame] | 277 | auto I = MCCVLineStartStop.find(FuncId); |
Reid Kleckner | 7897a78 | 2018-01-18 22:55:43 +0000 | [diff] [blame] | 278 | if (I != MCCVLineStartStop.end()) { |
| 279 | MCCVFunctionInfo *SiteInfo = getCVFunctionInfo(FuncId); |
Reid Kleckner | b525872 | 2018-01-18 22:55:14 +0000 | [diff] [blame] | 280 | for (size_t Idx = I->second.first, End = I->second.second; Idx != End; |
Reid Kleckner | 7897a78 | 2018-01-18 22:55:43 +0000 | [diff] [blame] | 281 | ++Idx) { |
| 282 | unsigned LocationFuncId = MCCVLines[Idx].getFunctionId(); |
| 283 | if (LocationFuncId == FuncId) { |
| 284 | // This was a .cv_loc directly for FuncId, so record it. |
Reid Kleckner | b525872 | 2018-01-18 22:55:14 +0000 | [diff] [blame] | 285 | FilteredLines.push_back(MCCVLines[Idx]); |
Reid Kleckner | 7897a78 | 2018-01-18 22:55:43 +0000 | [diff] [blame] | 286 | } else { |
| 287 | // Check if the current location is inlined in this function. If it is, |
| 288 | // synthesize a statement .cv_loc at the original inlined call site. |
| 289 | auto I = SiteInfo->InlinedAtMap.find(LocationFuncId); |
| 290 | if (I != SiteInfo->InlinedAtMap.end()) { |
| 291 | MCCVFunctionInfo::LineInfo &IA = I->second; |
| 292 | // Only add the location if it differs from the previous location. |
| 293 | // Large inlined calls will have many .cv_loc entries and we only need |
| 294 | // one line table entry in the parent function. |
| 295 | if (FilteredLines.empty() || |
| 296 | FilteredLines.back().getFileNum() != IA.File || |
| 297 | FilteredLines.back().getLine() != IA.Line || |
| 298 | FilteredLines.back().getColumn() != IA.Col) { |
Reid Kleckner | 689f773 | 2018-08-28 23:25:59 +0000 | [diff] [blame] | 299 | FilteredLines.push_back(MCCVLoc( |
Reid Kleckner | 7897a78 | 2018-01-18 22:55:43 +0000 | [diff] [blame] | 300 | MCCVLines[Idx].getLabel(), |
Reid Kleckner | 689f773 | 2018-08-28 23:25:59 +0000 | [diff] [blame] | 301 | FuncId, IA.File, IA.Line, IA.Col, false, false)); |
Reid Kleckner | 7897a78 | 2018-01-18 22:55:43 +0000 | [diff] [blame] | 302 | } |
| 303 | } |
| 304 | } |
| 305 | } |
| 306 | } |
Reid Kleckner | b525872 | 2018-01-18 22:55:14 +0000 | [diff] [blame] | 307 | return FilteredLines; |
| 308 | } |
| 309 | |
| 310 | std::pair<size_t, size_t> CodeViewContext::getLineExtent(unsigned FuncId) { |
| 311 | auto I = MCCVLineStartStop.find(FuncId); |
| 312 | // Return an empty extent if there are no cv_locs for this function id. |
| 313 | if (I == MCCVLineStartStop.end()) |
| 314 | return {~0ULL, 0}; |
| 315 | return I->second; |
| 316 | } |
| 317 | |
Reid Kleckner | 689f773 | 2018-08-28 23:25:59 +0000 | [diff] [blame] | 318 | ArrayRef<MCCVLoc> CodeViewContext::getLinesForExtent(size_t L, size_t R) { |
Reid Kleckner | b525872 | 2018-01-18 22:55:14 +0000 | [diff] [blame] | 319 | if (R <= L) |
| 320 | return None; |
| 321 | if (L >= MCCVLines.size()) |
| 322 | return None; |
| 323 | return makeArrayRef(&MCCVLines[L], R - L); |
| 324 | } |
| 325 | |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 326 | void CodeViewContext::emitLineTableForFunction(MCObjectStreamer &OS, |
| 327 | unsigned FuncId, |
| 328 | const MCSymbol *FuncBegin, |
| 329 | const MCSymbol *FuncEnd) { |
| 330 | MCContext &Ctx = OS.getContext(); |
David Blaikie | a0b44ef | 2016-01-29 02:23:13 +0000 | [diff] [blame] | 331 | MCSymbol *LineBegin = Ctx.createTempSymbol("linetable_begin", false), |
| 332 | *LineEnd = Ctx.createTempSymbol("linetable_end", false); |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 333 | |
Fangrui Song | 692e0c9 | 2020-02-29 08:25:22 -0800 | [diff] [blame] | 334 | OS.emitInt32(uint32_t(DebugSubsectionKind::Lines)); |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 335 | OS.emitAbsoluteSymbolDiff(LineEnd, LineBegin, 4); |
Fangrui Song | 6d2d589 | 2020-02-14 19:21:58 -0800 | [diff] [blame] | 336 | OS.emitLabel(LineBegin); |
Keno Fischer | f7d84ee | 2017-01-02 03:00:19 +0000 | [diff] [blame] | 337 | OS.EmitCOFFSecRel32(FuncBegin, /*Offset=*/0); |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 338 | OS.EmitCOFFSectionIndex(FuncBegin); |
| 339 | |
| 340 | // Actual line info. |
Reid Kleckner | 689f773 | 2018-08-28 23:25:59 +0000 | [diff] [blame] | 341 | std::vector<MCCVLoc> Locs = getFunctionLineEntries(FuncId); |
| 342 | bool HaveColumns = any_of(Locs, [](const MCCVLoc &LineEntry) { |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 343 | return LineEntry.getColumn() != 0; |
| 344 | }); |
Fangrui Song | 692e0c9 | 2020-02-29 08:25:22 -0800 | [diff] [blame] | 345 | OS.emitInt16(HaveColumns ? int(LF_HaveColumns) : 0); |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 346 | OS.emitAbsoluteSymbolDiff(FuncEnd, FuncBegin, 4); |
| 347 | |
| 348 | for (auto I = Locs.begin(), E = Locs.end(); I != E;) { |
| 349 | // Emit a file segment for the run of locations that share a file id. |
| 350 | unsigned CurFileNum = I->getFileNum(); |
| 351 | auto FileSegEnd = |
Reid Kleckner | 689f773 | 2018-08-28 23:25:59 +0000 | [diff] [blame] | 352 | std::find_if(I, E, [CurFileNum](const MCCVLoc &Loc) { |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 353 | return Loc.getFileNum() != CurFileNum; |
| 354 | }); |
| 355 | unsigned EntryCount = FileSegEnd - I; |
Reid Kleckner | 26fa1bf | 2017-09-19 18:14:45 +0000 | [diff] [blame] | 356 | OS.AddComment( |
| 357 | "Segment for file '" + |
| 358 | Twine(getStringTableFragment() |
| 359 | ->getContents()[Files[CurFileNum - 1].StringTableOffset]) + |
| 360 | "' begins"); |
Shengchen Kan | c031378 | 2020-04-20 19:28:13 -0700 | [diff] [blame^] | 361 | OS.emitCVFileChecksumOffsetDirective(CurFileNum); |
Fangrui Song | 692e0c9 | 2020-02-29 08:25:22 -0800 | [diff] [blame] | 362 | OS.emitInt32(EntryCount); |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 363 | uint32_t SegmentSize = 12; |
| 364 | SegmentSize += 8 * EntryCount; |
| 365 | if (HaveColumns) |
| 366 | SegmentSize += 4 * EntryCount; |
Fangrui Song | 692e0c9 | 2020-02-29 08:25:22 -0800 | [diff] [blame] | 367 | OS.emitInt32(SegmentSize); |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 368 | |
| 369 | for (auto J = I; J != FileSegEnd; ++J) { |
| 370 | OS.emitAbsoluteSymbolDiff(J->getLabel(), FuncBegin, 4); |
| 371 | unsigned LineData = J->getLine(); |
| 372 | if (J->isStmt()) |
David Majnemer | 6fcbd7e | 2016-01-29 19:24:12 +0000 | [diff] [blame] | 373 | LineData |= LineInfo::StatementFlag; |
Fangrui Song | 692e0c9 | 2020-02-29 08:25:22 -0800 | [diff] [blame] | 374 | OS.emitInt32(LineData); |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 375 | } |
| 376 | if (HaveColumns) { |
| 377 | for (auto J = I; J != FileSegEnd; ++J) { |
Fangrui Song | 692e0c9 | 2020-02-29 08:25:22 -0800 | [diff] [blame] | 378 | OS.emitInt16(J->getColumn()); |
| 379 | OS.emitInt16(0); |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 380 | } |
| 381 | } |
| 382 | I = FileSegEnd; |
| 383 | } |
Fangrui Song | 6d2d589 | 2020-02-14 19:21:58 -0800 | [diff] [blame] | 384 | OS.emitLabel(LineEnd); |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 385 | } |
| 386 | |
David Majnemer | 6fcbd7e | 2016-01-29 19:24:12 +0000 | [diff] [blame] | 387 | static bool compressAnnotation(uint32_t Data, SmallVectorImpl<char> &Buffer) { |
| 388 | if (isUInt<7>(Data)) { |
| 389 | Buffer.push_back(Data); |
| 390 | return true; |
| 391 | } |
| 392 | |
| 393 | if (isUInt<14>(Data)) { |
| 394 | Buffer.push_back((Data >> 8) | 0x80); |
| 395 | Buffer.push_back(Data & 0xff); |
| 396 | return true; |
| 397 | } |
| 398 | |
| 399 | if (isUInt<29>(Data)) { |
| 400 | Buffer.push_back((Data >> 24) | 0xC0); |
| 401 | Buffer.push_back((Data >> 16) & 0xff); |
| 402 | Buffer.push_back((Data >> 8) & 0xff); |
| 403 | Buffer.push_back(Data & 0xff); |
| 404 | return true; |
| 405 | } |
| 406 | |
| 407 | return false; |
| 408 | } |
| 409 | |
Zachary Turner | 63a2846 | 2016-05-17 23:50:21 +0000 | [diff] [blame] | 410 | static bool compressAnnotation(BinaryAnnotationsOpCode Annotation, |
| 411 | SmallVectorImpl<char> &Buffer) { |
| 412 | return compressAnnotation(static_cast<uint32_t>(Annotation), Buffer); |
| 413 | } |
| 414 | |
David Majnemer | 6fcbd7e | 2016-01-29 19:24:12 +0000 | [diff] [blame] | 415 | static uint32_t encodeSignedNumber(uint32_t Data) { |
| 416 | if (Data >> 31) |
| 417 | return ((-Data) << 1) | 1; |
| 418 | return Data << 1; |
| 419 | } |
| 420 | |
Reid Kleckner | a9f4cc9 | 2016-09-07 16:15:31 +0000 | [diff] [blame] | 421 | void CodeViewContext::emitInlineLineTableForFunction(MCObjectStreamer &OS, |
| 422 | unsigned PrimaryFunctionId, |
| 423 | unsigned SourceFileId, |
| 424 | unsigned SourceLineNum, |
| 425 | const MCSymbol *FnStartSym, |
| 426 | const MCSymbol *FnEndSym) { |
Reid Kleckner | 1fcd610 | 2016-02-02 17:41:18 +0000 | [diff] [blame] | 427 | // Create and insert a fragment into the current section that will be encoded |
| 428 | // later. |
Reid Kleckner | a9f4cc9 | 2016-09-07 16:15:31 +0000 | [diff] [blame] | 429 | new MCCVInlineLineTableFragment(PrimaryFunctionId, SourceFileId, |
| 430 | SourceLineNum, FnStartSym, FnEndSym, |
| 431 | OS.getCurrentSectionOnly()); |
Reid Kleckner | 1fcd610 | 2016-02-02 17:41:18 +0000 | [diff] [blame] | 432 | } |
David Majnemer | 6fcbd7e | 2016-01-29 19:24:12 +0000 | [diff] [blame] | 433 | |
Reid Kleckner | 94ee072 | 2018-12-17 21:49:35 +0000 | [diff] [blame] | 434 | MCFragment *CodeViewContext::emitDefRange( |
David Majnemer | 408b5e6 | 2016-02-05 01:55:49 +0000 | [diff] [blame] | 435 | MCObjectStreamer &OS, |
| 436 | ArrayRef<std::pair<const MCSymbol *, const MCSymbol *>> Ranges, |
| 437 | StringRef FixedSizePortion) { |
| 438 | // Create and insert a fragment into the current section that will be encoded |
| 439 | // later. |
Reid Kleckner | 94ee072 | 2018-12-17 21:49:35 +0000 | [diff] [blame] | 440 | return new MCCVDefRangeFragment(Ranges, FixedSizePortion, |
David Majnemer | 408b5e6 | 2016-02-05 01:55:49 +0000 | [diff] [blame] | 441 | OS.getCurrentSectionOnly()); |
| 442 | } |
| 443 | |
Reid Kleckner | cb91e7d | 2016-02-04 00:21:42 +0000 | [diff] [blame] | 444 | static unsigned computeLabelDiff(MCAsmLayout &Layout, const MCSymbol *Begin, |
| 445 | const MCSymbol *End) { |
Reid Kleckner | 1fcd610 | 2016-02-02 17:41:18 +0000 | [diff] [blame] | 446 | MCContext &Ctx = Layout.getAssembler().getContext(); |
| 447 | MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None; |
| 448 | const MCExpr *BeginRef = MCSymbolRefExpr::create(Begin, Variant, Ctx), |
| 449 | *EndRef = MCSymbolRefExpr::create(End, Variant, Ctx); |
| 450 | const MCExpr *AddrDelta = |
| 451 | MCBinaryExpr::create(MCBinaryExpr::Sub, EndRef, BeginRef, Ctx); |
| 452 | int64_t Result; |
| 453 | bool Success = AddrDelta->evaluateKnownAbsolute(Result, Layout); |
| 454 | assert(Success && "failed to evaluate label difference as absolute"); |
| 455 | (void)Success; |
| 456 | assert(Result >= 0 && "negative label difference requested"); |
| 457 | assert(Result < UINT_MAX && "label difference greater than 2GB"); |
| 458 | return unsigned(Result); |
| 459 | } |
David Majnemer | 6fcbd7e | 2016-01-29 19:24:12 +0000 | [diff] [blame] | 460 | |
Reid Kleckner | 1fcd610 | 2016-02-02 17:41:18 +0000 | [diff] [blame] | 461 | void CodeViewContext::encodeInlineLineTable(MCAsmLayout &Layout, |
| 462 | MCCVInlineLineTableFragment &Frag) { |
| 463 | size_t LocBegin; |
| 464 | size_t LocEnd; |
| 465 | std::tie(LocBegin, LocEnd) = getLineExtent(Frag.SiteFuncId); |
Reid Kleckner | a9f4cc9 | 2016-09-07 16:15:31 +0000 | [diff] [blame] | 466 | |
| 467 | // Include all child inline call sites in our .cv_loc extent. |
| 468 | MCCVFunctionInfo *SiteInfo = getCVFunctionInfo(Frag.SiteFuncId); |
| 469 | for (auto &KV : SiteInfo->InlinedAtMap) { |
| 470 | unsigned ChildId = KV.first; |
| 471 | auto Extent = getLineExtent(ChildId); |
Reid Kleckner | 1fcd610 | 2016-02-02 17:41:18 +0000 | [diff] [blame] | 472 | LocBegin = std::min(LocBegin, Extent.first); |
| 473 | LocEnd = std::max(LocEnd, Extent.second); |
| 474 | } |
Reid Kleckner | a9f4cc9 | 2016-09-07 16:15:31 +0000 | [diff] [blame] | 475 | |
Reid Kleckner | 1fcd610 | 2016-02-02 17:41:18 +0000 | [diff] [blame] | 476 | if (LocBegin >= LocEnd) |
| 477 | return; |
Reid Kleckner | 689f773 | 2018-08-28 23:25:59 +0000 | [diff] [blame] | 478 | ArrayRef<MCCVLoc> Locs = getLinesForExtent(LocBegin, LocEnd); |
Reid Kleckner | 1fcd610 | 2016-02-02 17:41:18 +0000 | [diff] [blame] | 479 | if (Locs.empty()) |
| 480 | return; |
| 481 | |
Reid Kleckner | 2c6430f | 2018-04-25 23:34:15 +0000 | [diff] [blame] | 482 | // Check that the locations are all in the same section. |
| 483 | #ifndef NDEBUG |
| 484 | const MCSection *FirstSec = &Locs.front().getLabel()->getSection(); |
Reid Kleckner | 689f773 | 2018-08-28 23:25:59 +0000 | [diff] [blame] | 485 | for (const MCCVLoc &Loc : Locs) { |
Reid Kleckner | 2c6430f | 2018-04-25 23:34:15 +0000 | [diff] [blame] | 486 | if (&Loc.getLabel()->getSection() != FirstSec) { |
| 487 | errs() << ".cv_loc " << Loc.getFunctionId() << ' ' << Loc.getFileNum() |
| 488 | << ' ' << Loc.getLine() << ' ' << Loc.getColumn() |
| 489 | << " is in the wrong section\n"; |
| 490 | llvm_unreachable(".cv_loc crosses sections"); |
| 491 | } |
| 492 | } |
| 493 | #endif |
| 494 | |
Reid Kleckner | 1fcd610 | 2016-02-02 17:41:18 +0000 | [diff] [blame] | 495 | // Make an artificial start location using the function start and the inlinee |
| 496 | // lines start location information. All deltas start relative to this |
| 497 | // location. |
Reid Kleckner | 689f773 | 2018-08-28 23:25:59 +0000 | [diff] [blame] | 498 | MCCVLoc StartLoc = Locs.front(); |
| 499 | StartLoc.setLabel(Frag.getFnStartSym()); |
Reid Kleckner | 1fcd610 | 2016-02-02 17:41:18 +0000 | [diff] [blame] | 500 | StartLoc.setFileNum(Frag.StartFileId); |
| 501 | StartLoc.setLine(Frag.StartLineNum); |
Reid Kleckner | c29b4f0 | 2016-07-14 23:47:15 +0000 | [diff] [blame] | 502 | bool HaveOpenRange = false; |
Reid Kleckner | 1fcd610 | 2016-02-02 17:41:18 +0000 | [diff] [blame] | 503 | |
Reid Kleckner | a9f4cc9 | 2016-09-07 16:15:31 +0000 | [diff] [blame] | 504 | const MCSymbol *LastLabel = Frag.getFnStartSym(); |
| 505 | MCCVFunctionInfo::LineInfo LastSourceLoc, CurSourceLoc; |
| 506 | LastSourceLoc.File = Frag.StartFileId; |
| 507 | LastSourceLoc.Line = Frag.StartLineNum; |
| 508 | |
Reid Kleckner | 1fcd610 | 2016-02-02 17:41:18 +0000 | [diff] [blame] | 509 | SmallVectorImpl<char> &Buffer = Frag.getContents(); |
| 510 | Buffer.clear(); // Clear old contents if we went through relaxation. |
Reid Kleckner | 689f773 | 2018-08-28 23:25:59 +0000 | [diff] [blame] | 511 | for (const MCCVLoc &Loc : Locs) { |
Reid Kleckner | bb96df6 | 2016-10-05 22:36:07 +0000 | [diff] [blame] | 512 | // Exit early if our line table would produce an oversized InlineSiteSym |
| 513 | // record. Account for the ChangeCodeLength annotation emitted after the |
| 514 | // loop ends. |
Zachary Turner | 46225b1 | 2016-12-16 22:48:14 +0000 | [diff] [blame] | 515 | constexpr uint32_t InlineSiteSize = 12; |
| 516 | constexpr uint32_t AnnotationSize = 8; |
| 517 | size_t MaxBufferSize = MaxRecordLength - InlineSiteSize - AnnotationSize; |
Reid Kleckner | bb96df6 | 2016-10-05 22:36:07 +0000 | [diff] [blame] | 518 | if (Buffer.size() >= MaxBufferSize) |
| 519 | break; |
| 520 | |
Reid Kleckner | a9f4cc9 | 2016-09-07 16:15:31 +0000 | [diff] [blame] | 521 | if (Loc.getFunctionId() == Frag.SiteFuncId) { |
| 522 | CurSourceLoc.File = Loc.getFileNum(); |
| 523 | CurSourceLoc.Line = Loc.getLine(); |
| 524 | } else { |
| 525 | auto I = SiteInfo->InlinedAtMap.find(Loc.getFunctionId()); |
| 526 | if (I != SiteInfo->InlinedAtMap.end()) { |
| 527 | // This .cv_loc is from a child inline call site. Use the source |
| 528 | // location of the inlined call site instead of the .cv_loc directive |
| 529 | // source location. |
| 530 | CurSourceLoc = I->second; |
| 531 | } else { |
| 532 | // We've hit a cv_loc not attributed to this inline call site. Use this |
| 533 | // label to end the PC range. |
| 534 | if (HaveOpenRange) { |
| 535 | unsigned Length = computeLabelDiff(Layout, LastLabel, Loc.getLabel()); |
| 536 | compressAnnotation(BinaryAnnotationsOpCode::ChangeCodeLength, Buffer); |
| 537 | compressAnnotation(Length, Buffer); |
Reid Kleckner | eddd6c2 | 2019-05-31 22:55:03 +0000 | [diff] [blame] | 538 | LastLabel = Loc.getLabel(); |
Reid Kleckner | a9f4cc9 | 2016-09-07 16:15:31 +0000 | [diff] [blame] | 539 | } |
| 540 | HaveOpenRange = false; |
| 541 | continue; |
Reid Kleckner | 1fcd610 | 2016-02-02 17:41:18 +0000 | [diff] [blame] | 542 | } |
Reid Kleckner | 1fcd610 | 2016-02-02 17:41:18 +0000 | [diff] [blame] | 543 | } |
Reid Kleckner | c29b4f0 | 2016-07-14 23:47:15 +0000 | [diff] [blame] | 544 | |
Reid Kleckner | a9f4cc9 | 2016-09-07 16:15:31 +0000 | [diff] [blame] | 545 | // Skip this .cv_loc if we have an open range and this isn't a meaningful |
| 546 | // source location update. The current table format does not support column |
| 547 | // info, so we can skip updates for those. |
| 548 | if (HaveOpenRange && CurSourceLoc.File == LastSourceLoc.File && |
| 549 | CurSourceLoc.Line == LastSourceLoc.Line) |
Reid Kleckner | c29b4f0 | 2016-07-14 23:47:15 +0000 | [diff] [blame] | 550 | continue; |
Reid Kleckner | a9f4cc9 | 2016-09-07 16:15:31 +0000 | [diff] [blame] | 551 | |
Reid Kleckner | c29b4f0 | 2016-07-14 23:47:15 +0000 | [diff] [blame] | 552 | HaveOpenRange = true; |
Reid Kleckner | 1fcd610 | 2016-02-02 17:41:18 +0000 | [diff] [blame] | 553 | |
Reid Kleckner | a9f4cc9 | 2016-09-07 16:15:31 +0000 | [diff] [blame] | 554 | if (CurSourceLoc.File != LastSourceLoc.File) { |
Reid Kleckner | 26fa1bf | 2017-09-19 18:14:45 +0000 | [diff] [blame] | 555 | unsigned FileOffset = static_cast<const MCConstantExpr *>( |
| 556 | Files[CurSourceLoc.File - 1] |
| 557 | .ChecksumTableOffset->getVariableValue()) |
| 558 | ->getValue(); |
Zachary Turner | 63a2846 | 2016-05-17 23:50:21 +0000 | [diff] [blame] | 559 | compressAnnotation(BinaryAnnotationsOpCode::ChangeFile, Buffer); |
Reid Kleckner | 344078f | 2016-02-19 23:55:38 +0000 | [diff] [blame] | 560 | compressAnnotation(FileOffset, Buffer); |
David Majnemer | 6fcbd7e | 2016-01-29 19:24:12 +0000 | [diff] [blame] | 561 | } |
| 562 | |
Reid Kleckner | a9f4cc9 | 2016-09-07 16:15:31 +0000 | [diff] [blame] | 563 | int LineDelta = CurSourceLoc.Line - LastSourceLoc.Line; |
Reid Kleckner | 1fcd610 | 2016-02-02 17:41:18 +0000 | [diff] [blame] | 564 | unsigned EncodedLineDelta = encodeSignedNumber(LineDelta); |
Reid Kleckner | a9f4cc9 | 2016-09-07 16:15:31 +0000 | [diff] [blame] | 565 | unsigned CodeDelta = computeLabelDiff(Layout, LastLabel, Loc.getLabel()); |
| 566 | if (CodeDelta == 0 && LineDelta != 0) { |
Zachary Turner | 63a2846 | 2016-05-17 23:50:21 +0000 | [diff] [blame] | 567 | compressAnnotation(BinaryAnnotationsOpCode::ChangeLineOffset, Buffer); |
Reid Kleckner | 1fcd610 | 2016-02-02 17:41:18 +0000 | [diff] [blame] | 568 | compressAnnotation(EncodedLineDelta, Buffer); |
| 569 | } else if (EncodedLineDelta < 0x8 && CodeDelta <= 0xf) { |
| 570 | // The ChangeCodeOffsetAndLineOffset combination opcode is used when the |
| 571 | // encoded line delta uses 3 or fewer set bits and the code offset fits |
| 572 | // in one nibble. |
| 573 | unsigned Operand = (EncodedLineDelta << 4) | CodeDelta; |
Zachary Turner | 63a2846 | 2016-05-17 23:50:21 +0000 | [diff] [blame] | 574 | compressAnnotation(BinaryAnnotationsOpCode::ChangeCodeOffsetAndLineOffset, |
| 575 | Buffer); |
Reid Kleckner | 1fcd610 | 2016-02-02 17:41:18 +0000 | [diff] [blame] | 576 | compressAnnotation(Operand, Buffer); |
| 577 | } else { |
| 578 | // Otherwise use the separate line and code deltas. |
Reid Kleckner | a9f4cc9 | 2016-09-07 16:15:31 +0000 | [diff] [blame] | 579 | if (LineDelta != 0) { |
| 580 | compressAnnotation(BinaryAnnotationsOpCode::ChangeLineOffset, Buffer); |
| 581 | compressAnnotation(EncodedLineDelta, Buffer); |
| 582 | } |
Zachary Turner | 63a2846 | 2016-05-17 23:50:21 +0000 | [diff] [blame] | 583 | compressAnnotation(BinaryAnnotationsOpCode::ChangeCodeOffset, Buffer); |
Reid Kleckner | 1fcd610 | 2016-02-02 17:41:18 +0000 | [diff] [blame] | 584 | compressAnnotation(CodeDelta, Buffer); |
| 585 | } |
David Majnemer | 6fcbd7e | 2016-01-29 19:24:12 +0000 | [diff] [blame] | 586 | |
Reid Kleckner | a9f4cc9 | 2016-09-07 16:15:31 +0000 | [diff] [blame] | 587 | LastLabel = Loc.getLabel(); |
| 588 | LastSourceLoc = CurSourceLoc; |
David Majnemer | 6fcbd7e | 2016-01-29 19:24:12 +0000 | [diff] [blame] | 589 | } |
David Majnemer | c9911f2 | 2016-02-02 19:22:34 +0000 | [diff] [blame] | 590 | |
Reid Kleckner | c29b4f0 | 2016-07-14 23:47:15 +0000 | [diff] [blame] | 591 | assert(HaveOpenRange); |
David Majnemer | c9911f2 | 2016-02-02 19:22:34 +0000 | [diff] [blame] | 592 | |
| 593 | unsigned EndSymLength = |
Reid Kleckner | a9f4cc9 | 2016-09-07 16:15:31 +0000 | [diff] [blame] | 594 | computeLabelDiff(Layout, LastLabel, Frag.getFnEndSym()); |
David Majnemer | c9911f2 | 2016-02-02 19:22:34 +0000 | [diff] [blame] | 595 | unsigned LocAfterLength = ~0U; |
Reid Kleckner | 689f773 | 2018-08-28 23:25:59 +0000 | [diff] [blame] | 596 | ArrayRef<MCCVLoc> LocAfter = getLinesForExtent(LocEnd, LocEnd + 1); |
Reid Kleckner | cb91e7d | 2016-02-04 00:21:42 +0000 | [diff] [blame] | 597 | if (!LocAfter.empty()) { |
| 598 | // Only try to compute this difference if we're in the same section. |
Reid Kleckner | 689f773 | 2018-08-28 23:25:59 +0000 | [diff] [blame] | 599 | const MCCVLoc &Loc = LocAfter[0]; |
Sam Clegg | 5e102ee | 2018-01-12 18:05:40 +0000 | [diff] [blame] | 600 | if (&Loc.getLabel()->getSection() == &LastLabel->getSection()) |
Reid Kleckner | a9f4cc9 | 2016-09-07 16:15:31 +0000 | [diff] [blame] | 601 | LocAfterLength = computeLabelDiff(Layout, LastLabel, Loc.getLabel()); |
Reid Kleckner | cb91e7d | 2016-02-04 00:21:42 +0000 | [diff] [blame] | 602 | } |
David Majnemer | c9911f2 | 2016-02-02 19:22:34 +0000 | [diff] [blame] | 603 | |
Zachary Turner | 63a2846 | 2016-05-17 23:50:21 +0000 | [diff] [blame] | 604 | compressAnnotation(BinaryAnnotationsOpCode::ChangeCodeLength, Buffer); |
David Majnemer | c9911f2 | 2016-02-02 19:22:34 +0000 | [diff] [blame] | 605 | compressAnnotation(std::min(EndSymLength, LocAfterLength), Buffer); |
David Majnemer | 6fcbd7e | 2016-01-29 19:24:12 +0000 | [diff] [blame] | 606 | } |
| 607 | |
David Majnemer | 408b5e6 | 2016-02-05 01:55:49 +0000 | [diff] [blame] | 608 | void CodeViewContext::encodeDefRange(MCAsmLayout &Layout, |
| 609 | MCCVDefRangeFragment &Frag) { |
| 610 | MCContext &Ctx = Layout.getAssembler().getContext(); |
| 611 | SmallVectorImpl<char> &Contents = Frag.getContents(); |
| 612 | Contents.clear(); |
| 613 | SmallVectorImpl<MCFixup> &Fixups = Frag.getFixups(); |
| 614 | Fixups.clear(); |
| 615 | raw_svector_ostream OS(Contents); |
| 616 | |
Reid Kleckner | be82d3e | 2016-09-15 22:05:08 +0000 | [diff] [blame] | 617 | // Compute all the sizes up front. |
| 618 | SmallVector<std::pair<unsigned, unsigned>, 4> GapAndRangeSizes; |
| 619 | const MCSymbol *LastLabel = nullptr; |
David Majnemer | 408b5e6 | 2016-02-05 01:55:49 +0000 | [diff] [blame] | 620 | for (std::pair<const MCSymbol *, const MCSymbol *> Range : Frag.getRanges()) { |
Reid Kleckner | be82d3e | 2016-09-15 22:05:08 +0000 | [diff] [blame] | 621 | unsigned GapSize = |
| 622 | LastLabel ? computeLabelDiff(Layout, LastLabel, Range.first) : 0; |
David Majnemer | 408b5e6 | 2016-02-05 01:55:49 +0000 | [diff] [blame] | 623 | unsigned RangeSize = computeLabelDiff(Layout, Range.first, Range.second); |
Reid Kleckner | be82d3e | 2016-09-15 22:05:08 +0000 | [diff] [blame] | 624 | GapAndRangeSizes.push_back({GapSize, RangeSize}); |
| 625 | LastLabel = Range.second; |
| 626 | } |
| 627 | |
| 628 | // Write down each range where the variable is defined. |
| 629 | for (size_t I = 0, E = Frag.getRanges().size(); I != E;) { |
| 630 | // If the range size of multiple consecutive ranges is under the max, |
| 631 | // combine the ranges and emit some gaps. |
| 632 | const MCSymbol *RangeBegin = Frag.getRanges()[I].first; |
| 633 | unsigned RangeSize = GapAndRangeSizes[I].second; |
| 634 | size_t J = I + 1; |
| 635 | for (; J != E; ++J) { |
| 636 | unsigned GapAndRangeSize = GapAndRangeSizes[J].first + GapAndRangeSizes[J].second; |
| 637 | if (RangeSize + GapAndRangeSize > MaxDefRange) |
| 638 | break; |
| 639 | RangeSize += GapAndRangeSize; |
| 640 | } |
| 641 | unsigned NumGaps = J - I - 1; |
| 642 | |
Peter Collingbourne | e3f6529 | 2018-05-18 19:46:24 +0000 | [diff] [blame] | 643 | support::endian::Writer LEWriter(OS, support::little); |
Reid Kleckner | be82d3e | 2016-09-15 22:05:08 +0000 | [diff] [blame] | 644 | |
David Majnemer | 408b5e6 | 2016-02-05 01:55:49 +0000 | [diff] [blame] | 645 | unsigned Bias = 0; |
| 646 | // We must split the range into chunks of MaxDefRange, this is a fundamental |
| 647 | // limitation of the file format. |
| 648 | do { |
| 649 | uint16_t Chunk = std::min((uint32_t)MaxDefRange, RangeSize); |
| 650 | |
Reid Kleckner | be82d3e | 2016-09-15 22:05:08 +0000 | [diff] [blame] | 651 | const MCSymbolRefExpr *SRE = MCSymbolRefExpr::create(RangeBegin, Ctx); |
David Majnemer | 408b5e6 | 2016-02-05 01:55:49 +0000 | [diff] [blame] | 652 | const MCBinaryExpr *BE = |
| 653 | MCBinaryExpr::createAdd(SRE, MCConstantExpr::create(Bias, Ctx), Ctx); |
| 654 | MCValue Res; |
| 655 | BE->evaluateAsRelocatable(Res, &Layout, /*Fixup=*/nullptr); |
| 656 | |
| 657 | // Each record begins with a 2-byte number indicating how large the record |
| 658 | // is. |
| 659 | StringRef FixedSizePortion = Frag.getFixedSizePortion(); |
| 660 | // Our record is a fixed sized prefix and a LocalVariableAddrRange that we |
| 661 | // are artificially constructing. |
Reid Kleckner | be82d3e | 2016-09-15 22:05:08 +0000 | [diff] [blame] | 662 | size_t RecordSize = FixedSizePortion.size() + |
| 663 | sizeof(LocalVariableAddrRange) + 4 * NumGaps; |
Reid Kleckner | 11cf053 | 2017-01-24 16:57:55 +0000 | [diff] [blame] | 664 | // Write out the record size. |
| 665 | LEWriter.write<uint16_t>(RecordSize); |
David Majnemer | 408b5e6 | 2016-02-05 01:55:49 +0000 | [diff] [blame] | 666 | // Write out the fixed size prefix. |
| 667 | OS << FixedSizePortion; |
| 668 | // Make space for a fixup that will eventually have a section relative |
| 669 | // relocation pointing at the offset where the variable becomes live. |
| 670 | Fixups.push_back(MCFixup::create(Contents.size(), BE, FK_SecRel_4)); |
Reid Kleckner | 11cf053 | 2017-01-24 16:57:55 +0000 | [diff] [blame] | 671 | LEWriter.write<uint32_t>(0); // Fixup for code start. |
David Majnemer | 408b5e6 | 2016-02-05 01:55:49 +0000 | [diff] [blame] | 672 | // Make space for a fixup that will record the section index for the code. |
| 673 | Fixups.push_back(MCFixup::create(Contents.size(), BE, FK_SecRel_2)); |
Reid Kleckner | 11cf053 | 2017-01-24 16:57:55 +0000 | [diff] [blame] | 674 | LEWriter.write<uint16_t>(0); // Fixup for section index. |
David Majnemer | 408b5e6 | 2016-02-05 01:55:49 +0000 | [diff] [blame] | 675 | // Write down the range's extent. |
Reid Kleckner | be82d3e | 2016-09-15 22:05:08 +0000 | [diff] [blame] | 676 | LEWriter.write<uint16_t>(Chunk); |
David Majnemer | 408b5e6 | 2016-02-05 01:55:49 +0000 | [diff] [blame] | 677 | |
| 678 | // Move on to the next range. |
| 679 | Bias += Chunk; |
| 680 | RangeSize -= Chunk; |
| 681 | } while (RangeSize > 0); |
Reid Kleckner | be82d3e | 2016-09-15 22:05:08 +0000 | [diff] [blame] | 682 | |
| 683 | // Emit the gaps afterwards. |
Reid Kleckner | 11cf053 | 2017-01-24 16:57:55 +0000 | [diff] [blame] | 684 | assert((NumGaps == 0 || Bias <= MaxDefRange) && |
Reid Kleckner | be82d3e | 2016-09-15 22:05:08 +0000 | [diff] [blame] | 685 | "large ranges should not have gaps"); |
| 686 | unsigned GapStartOffset = GapAndRangeSizes[I].second; |
| 687 | for (++I; I != J; ++I) { |
| 688 | unsigned GapSize, RangeSize; |
| 689 | assert(I < GapAndRangeSizes.size()); |
| 690 | std::tie(GapSize, RangeSize) = GapAndRangeSizes[I]; |
| 691 | LEWriter.write<uint16_t>(GapStartOffset); |
Reid Kleckner | 11cf053 | 2017-01-24 16:57:55 +0000 | [diff] [blame] | 692 | LEWriter.write<uint16_t>(GapSize); |
Reid Kleckner | be82d3e | 2016-09-15 22:05:08 +0000 | [diff] [blame] | 693 | GapStartOffset += GapSize + RangeSize; |
| 694 | } |
David Majnemer | 408b5e6 | 2016-02-05 01:55:49 +0000 | [diff] [blame] | 695 | } |
| 696 | } |