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