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