X86WinCOFFTargetStreamer.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  1. //===-- X86WinCOFFTargetStreamer.cpp ----------------------------*- C++ -*-===//
  2. //
  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
  6. //
  7. //===----------------------------------------------------------------------===//
  8. #include "X86MCTargetDesc.h"
  9. #include "X86TargetStreamer.h"
  10. #include "llvm/DebugInfo/CodeView/CodeView.h"
  11. #include "llvm/MC/MCCodeView.h"
  12. #include "llvm/MC/MCContext.h"
  13. #include "llvm/MC/MCInstPrinter.h"
  14. #include "llvm/MC/MCRegisterInfo.h"
  15. #include "llvm/MC/MCSubtargetInfo.h"
  16. #include "llvm/Support/FormattedStream.h"
  17. using namespace llvm;
  18. using namespace llvm::codeview;
  19. namespace {
  20. /// Implements Windows x86-only directives for assembly emission.
  21. class X86WinCOFFAsmTargetStreamer : public X86TargetStreamer {
  22. formatted_raw_ostream &OS;
  23. MCInstPrinter &InstPrinter;
  24. public:
  25. X86WinCOFFAsmTargetStreamer(MCStreamer &S, formatted_raw_ostream &OS,
  26. MCInstPrinter &InstPrinter)
  27. : X86TargetStreamer(S), OS(OS), InstPrinter(InstPrinter) {}
  28. bool emitFPOProc(const MCSymbol *ProcSym, unsigned ParamsSize,
  29. SMLoc L) override;
  30. bool emitFPOEndPrologue(SMLoc L) override;
  31. bool emitFPOEndProc(SMLoc L) override;
  32. bool emitFPOData(const MCSymbol *ProcSym, SMLoc L) override;
  33. bool emitFPOPushReg(unsigned Reg, SMLoc L) override;
  34. bool emitFPOStackAlloc(unsigned StackAlloc, SMLoc L) override;
  35. bool emitFPOStackAlign(unsigned Align, SMLoc L) override;
  36. bool emitFPOSetFrame(unsigned Reg, SMLoc L) override;
  37. };
  38. /// Represents a single FPO directive.
  39. struct FPOInstruction {
  40. MCSymbol *Label;
  41. enum Operation {
  42. PushReg,
  43. StackAlloc,
  44. StackAlign,
  45. SetFrame,
  46. } Op;
  47. unsigned RegOrOffset;
  48. };
  49. struct FPOData {
  50. const MCSymbol *Function = nullptr;
  51. MCSymbol *Begin = nullptr;
  52. MCSymbol *PrologueEnd = nullptr;
  53. MCSymbol *End = nullptr;
  54. unsigned ParamsSize = 0;
  55. SmallVector<FPOInstruction, 5> Instructions;
  56. };
  57. /// Implements Windows x86-only directives for object emission.
  58. class X86WinCOFFTargetStreamer : public X86TargetStreamer {
  59. /// Map from function symbol to its FPO data.
  60. DenseMap<const MCSymbol *, std::unique_ptr<FPOData>> AllFPOData;
  61. /// Current FPO data created by .cv_fpo_proc.
  62. std::unique_ptr<FPOData> CurFPOData;
  63. bool haveOpenFPOData() { return !!CurFPOData; }
  64. /// Diagnoses an error at L if we are not in an FPO prologue. Return true on
  65. /// error.
  66. bool checkInFPOPrologue(SMLoc L);
  67. MCSymbol *emitFPOLabel();
  68. MCContext &getContext() { return getStreamer().getContext(); }
  69. public:
  70. X86WinCOFFTargetStreamer(MCStreamer &S) : X86TargetStreamer(S) {}
  71. bool emitFPOProc(const MCSymbol *ProcSym, unsigned ParamsSize,
  72. SMLoc L) override;
  73. bool emitFPOEndPrologue(SMLoc L) override;
  74. bool emitFPOEndProc(SMLoc L) override;
  75. bool emitFPOData(const MCSymbol *ProcSym, SMLoc L) override;
  76. bool emitFPOPushReg(unsigned Reg, SMLoc L) override;
  77. bool emitFPOStackAlloc(unsigned StackAlloc, SMLoc L) override;
  78. bool emitFPOStackAlign(unsigned Align, SMLoc L) override;
  79. bool emitFPOSetFrame(unsigned Reg, SMLoc L) override;
  80. };
  81. } // end namespace
  82. bool X86WinCOFFAsmTargetStreamer::emitFPOProc(const MCSymbol *ProcSym,
  83. unsigned ParamsSize, SMLoc L) {
  84. OS << "\t.cv_fpo_proc\t";
  85. ProcSym->print(OS, getStreamer().getContext().getAsmInfo());
  86. OS << ' ' << ParamsSize << '\n';
  87. return false;
  88. }
  89. bool X86WinCOFFAsmTargetStreamer::emitFPOEndPrologue(SMLoc L) {
  90. OS << "\t.cv_fpo_endprologue\n";
  91. return false;
  92. }
  93. bool X86WinCOFFAsmTargetStreamer::emitFPOEndProc(SMLoc L) {
  94. OS << "\t.cv_fpo_endproc\n";
  95. return false;
  96. }
  97. bool X86WinCOFFAsmTargetStreamer::emitFPOData(const MCSymbol *ProcSym,
  98. SMLoc L) {
  99. OS << "\t.cv_fpo_data\t";
  100. ProcSym->print(OS, getStreamer().getContext().getAsmInfo());
  101. OS << '\n';
  102. return false;
  103. }
  104. bool X86WinCOFFAsmTargetStreamer::emitFPOPushReg(unsigned Reg, SMLoc L) {
  105. OS << "\t.cv_fpo_pushreg\t";
  106. InstPrinter.printRegName(OS, Reg);
  107. OS << '\n';
  108. return false;
  109. }
  110. bool X86WinCOFFAsmTargetStreamer::emitFPOStackAlloc(unsigned StackAlloc,
  111. SMLoc L) {
  112. OS << "\t.cv_fpo_stackalloc\t" << StackAlloc << '\n';
  113. return false;
  114. }
  115. bool X86WinCOFFAsmTargetStreamer::emitFPOStackAlign(unsigned Align, SMLoc L) {
  116. OS << "\t.cv_fpo_stackalign\t" << Align << '\n';
  117. return false;
  118. }
  119. bool X86WinCOFFAsmTargetStreamer::emitFPOSetFrame(unsigned Reg, SMLoc L) {
  120. OS << "\t.cv_fpo_setframe\t";
  121. InstPrinter.printRegName(OS, Reg);
  122. OS << '\n';
  123. return false;
  124. }
  125. bool X86WinCOFFTargetStreamer::checkInFPOPrologue(SMLoc L) {
  126. if (!haveOpenFPOData() || CurFPOData->PrologueEnd) {
  127. getContext().reportError(
  128. L,
  129. "directive must appear between .cv_fpo_proc and .cv_fpo_endprologue");
  130. return true;
  131. }
  132. return false;
  133. }
  134. MCSymbol *X86WinCOFFTargetStreamer::emitFPOLabel() {
  135. MCSymbol *Label = getContext().createTempSymbol("cfi", true);
  136. getStreamer().emitLabel(Label);
  137. return Label;
  138. }
  139. bool X86WinCOFFTargetStreamer::emitFPOProc(const MCSymbol *ProcSym,
  140. unsigned ParamsSize, SMLoc L) {
  141. if (haveOpenFPOData()) {
  142. getContext().reportError(
  143. L, "opening new .cv_fpo_proc before closing previous frame");
  144. return true;
  145. }
  146. CurFPOData = std::make_unique<FPOData>();
  147. CurFPOData->Function = ProcSym;
  148. CurFPOData->Begin = emitFPOLabel();
  149. CurFPOData->ParamsSize = ParamsSize;
  150. return false;
  151. }
  152. bool X86WinCOFFTargetStreamer::emitFPOEndProc(SMLoc L) {
  153. if (!haveOpenFPOData()) {
  154. getContext().reportError(L, ".cv_fpo_endproc must appear after .cv_proc");
  155. return true;
  156. }
  157. if (!CurFPOData->PrologueEnd) {
  158. // Complain if there were prologue setup instructions but no end prologue.
  159. if (!CurFPOData->Instructions.empty()) {
  160. getContext().reportError(L, "missing .cv_fpo_endprologue");
  161. CurFPOData->Instructions.clear();
  162. }
  163. // Claim there is a zero-length prologue to make the label math work out
  164. // later.
  165. CurFPOData->PrologueEnd = CurFPOData->Begin;
  166. }
  167. CurFPOData->End = emitFPOLabel();
  168. const MCSymbol *Fn = CurFPOData->Function;
  169. AllFPOData.insert({Fn, std::move(CurFPOData)});
  170. return false;
  171. }
  172. bool X86WinCOFFTargetStreamer::emitFPOSetFrame(unsigned Reg, SMLoc L) {
  173. if (checkInFPOPrologue(L))
  174. return true;
  175. FPOInstruction Inst;
  176. Inst.Label = emitFPOLabel();
  177. Inst.Op = FPOInstruction::SetFrame;
  178. Inst.RegOrOffset = Reg;
  179. CurFPOData->Instructions.push_back(Inst);
  180. return false;
  181. }
  182. bool X86WinCOFFTargetStreamer::emitFPOPushReg(unsigned Reg, SMLoc L) {
  183. if (checkInFPOPrologue(L))
  184. return true;
  185. FPOInstruction Inst;
  186. Inst.Label = emitFPOLabel();
  187. Inst.Op = FPOInstruction::PushReg;
  188. Inst.RegOrOffset = Reg;
  189. CurFPOData->Instructions.push_back(Inst);
  190. return false;
  191. }
  192. bool X86WinCOFFTargetStreamer::emitFPOStackAlloc(unsigned StackAlloc, SMLoc L) {
  193. if (checkInFPOPrologue(L))
  194. return true;
  195. FPOInstruction Inst;
  196. Inst.Label = emitFPOLabel();
  197. Inst.Op = FPOInstruction::StackAlloc;
  198. Inst.RegOrOffset = StackAlloc;
  199. CurFPOData->Instructions.push_back(Inst);
  200. return false;
  201. }
  202. bool X86WinCOFFTargetStreamer::emitFPOStackAlign(unsigned Align, SMLoc L) {
  203. if (checkInFPOPrologue(L))
  204. return true;
  205. if (llvm::none_of(CurFPOData->Instructions, [](const FPOInstruction &Inst) {
  206. return Inst.Op == FPOInstruction::SetFrame;
  207. })) {
  208. getContext().reportError(
  209. L, "a frame register must be established before aligning the stack");
  210. return true;
  211. }
  212. FPOInstruction Inst;
  213. Inst.Label = emitFPOLabel();
  214. Inst.Op = FPOInstruction::StackAlign;
  215. Inst.RegOrOffset = Align;
  216. CurFPOData->Instructions.push_back(Inst);
  217. return false;
  218. }
  219. bool X86WinCOFFTargetStreamer::emitFPOEndPrologue(SMLoc L) {
  220. if (checkInFPOPrologue(L))
  221. return true;
  222. CurFPOData->PrologueEnd = emitFPOLabel();
  223. return false;
  224. }
  225. namespace {
  226. struct RegSaveOffset {
  227. RegSaveOffset(unsigned Reg, unsigned Offset) : Reg(Reg), Offset(Offset) {}
  228. unsigned Reg = 0;
  229. unsigned Offset = 0;
  230. };
  231. struct FPOStateMachine {
  232. explicit FPOStateMachine(const FPOData *FPO) : FPO(FPO) {}
  233. const FPOData *FPO = nullptr;
  234. unsigned FrameReg = 0;
  235. unsigned FrameRegOff = 0;
  236. unsigned CurOffset = 0;
  237. unsigned LocalSize = 0;
  238. unsigned SavedRegSize = 0;
  239. unsigned StackOffsetBeforeAlign = 0;
  240. unsigned StackAlign = 0;
  241. unsigned Flags = 0; // FIXME: Set HasSEH / HasEH.
  242. SmallString<128> FrameFunc;
  243. SmallVector<RegSaveOffset, 4> RegSaveOffsets;
  244. void emitFrameDataRecord(MCStreamer &OS, MCSymbol *Label);
  245. };
  246. } // end namespace
  247. static Printable printFPOReg(const MCRegisterInfo *MRI, unsigned LLVMReg) {
  248. return Printable([MRI, LLVMReg](raw_ostream &OS) {
  249. switch (LLVMReg) {
  250. // MSVC only seems to emit symbolic register names for EIP, EBP, and ESP,
  251. // but the format seems to support more than that, so we emit them.
  252. case X86::EAX: OS << "$eax"; break;
  253. case X86::EBX: OS << "$ebx"; break;
  254. case X86::ECX: OS << "$ecx"; break;
  255. case X86::EDX: OS << "$edx"; break;
  256. case X86::EDI: OS << "$edi"; break;
  257. case X86::ESI: OS << "$esi"; break;
  258. case X86::ESP: OS << "$esp"; break;
  259. case X86::EBP: OS << "$ebp"; break;
  260. case X86::EIP: OS << "$eip"; break;
  261. // Otherwise, get the codeview register number and print $N.
  262. default:
  263. OS << '$' << MRI->getCodeViewRegNum(LLVMReg);
  264. break;
  265. }
  266. });
  267. }
  268. void FPOStateMachine::emitFrameDataRecord(MCStreamer &OS, MCSymbol *Label) {
  269. unsigned CurFlags = Flags;
  270. if (Label == FPO->Begin)
  271. CurFlags |= FrameData::IsFunctionStart;
  272. // Compute the new FrameFunc string.
  273. FrameFunc.clear();
  274. raw_svector_ostream FuncOS(FrameFunc);
  275. const MCRegisterInfo *MRI = OS.getContext().getRegisterInfo();
  276. assert((StackAlign == 0 || FrameReg != 0) &&
  277. "cannot align stack without frame reg");
  278. StringRef CFAVar = StackAlign == 0 ? "$T0" : "$T1";
  279. if (FrameReg) {
  280. // CFA is FrameReg + FrameRegOff.
  281. FuncOS << CFAVar << ' ' << printFPOReg(MRI, FrameReg) << ' ' << FrameRegOff
  282. << " + = ";
  283. // Assign $T0, the VFRAME register, the value of ESP after it is aligned.
  284. // Starting from the CFA, we subtract the size of all pushed registers, and
  285. // align the result. While we don't store any CSRs in this area, $T0 is used
  286. // by S_DEFRANGE_FRAMEPOINTER_REL records to find local variables.
  287. if (StackAlign) {
  288. FuncOS << "$T0 " << CFAVar << ' ' << StackOffsetBeforeAlign << " - "
  289. << StackAlign << " @ = ";
  290. }
  291. } else {
  292. // The address of return address is ESP + CurOffset, but we use .raSearch to
  293. // match MSVC. This seems to ask the debugger to subtract some combination
  294. // of LocalSize and SavedRegSize from ESP and grovel around in that memory
  295. // to find the address of a plausible return address.
  296. FuncOS << CFAVar << " .raSearch = ";
  297. }
  298. // Caller's $eip should be dereferenced CFA, and $esp should be CFA plus 4.
  299. FuncOS << "$eip " << CFAVar << " ^ = ";
  300. FuncOS << "$esp " << CFAVar << " 4 + = ";
  301. // Each saved register is stored at an unchanging negative CFA offset.
  302. for (RegSaveOffset RO : RegSaveOffsets)
  303. FuncOS << printFPOReg(MRI, RO.Reg) << ' ' << CFAVar << ' ' << RO.Offset
  304. << " - ^ = ";
  305. // Add it to the CV string table.
  306. CodeViewContext &CVCtx = OS.getContext().getCVContext();
  307. unsigned FrameFuncStrTabOff = CVCtx.addToStringTable(FuncOS.str()).second;
  308. // MSVC has only ever been observed to emit a MaxStackSize of zero.
  309. unsigned MaxStackSize = 0;
  310. // The FrameData record format is:
  311. // ulittle32_t RvaStart;
  312. // ulittle32_t CodeSize;
  313. // ulittle32_t LocalSize;
  314. // ulittle32_t ParamsSize;
  315. // ulittle32_t MaxStackSize;
  316. // ulittle32_t FrameFunc; // String table offset
  317. // ulittle16_t PrologSize;
  318. // ulittle16_t SavedRegsSize;
  319. // ulittle32_t Flags;
  320. OS.emitAbsoluteSymbolDiff(Label, FPO->Begin, 4); // RvaStart
  321. OS.emitAbsoluteSymbolDiff(FPO->End, Label, 4); // CodeSize
  322. OS.emitInt32(LocalSize);
  323. OS.emitInt32(FPO->ParamsSize);
  324. OS.emitInt32(MaxStackSize);
  325. OS.emitInt32(FrameFuncStrTabOff); // FrameFunc
  326. OS.emitAbsoluteSymbolDiff(FPO->PrologueEnd, Label, 2);
  327. OS.emitInt16(SavedRegSize);
  328. OS.emitInt32(CurFlags);
  329. }
  330. /// Compute and emit the real CodeView FrameData subsection.
  331. bool X86WinCOFFTargetStreamer::emitFPOData(const MCSymbol *ProcSym, SMLoc L) {
  332. MCStreamer &OS = getStreamer();
  333. MCContext &Ctx = OS.getContext();
  334. auto I = AllFPOData.find(ProcSym);
  335. if (I == AllFPOData.end()) {
  336. Ctx.reportError(L, Twine("no FPO data found for symbol ") +
  337. ProcSym->getName());
  338. return true;
  339. }
  340. const FPOData *FPO = I->second.get();
  341. assert(FPO->Begin && FPO->End && FPO->PrologueEnd && "missing FPO label");
  342. MCSymbol *FrameBegin = Ctx.createTempSymbol(),
  343. *FrameEnd = Ctx.createTempSymbol();
  344. OS.emitInt32(unsigned(DebugSubsectionKind::FrameData));
  345. OS.emitAbsoluteSymbolDiff(FrameEnd, FrameBegin, 4);
  346. OS.emitLabel(FrameBegin);
  347. // Start with the RVA of the function in question.
  348. OS.emitValue(MCSymbolRefExpr::create(FPO->Function,
  349. MCSymbolRefExpr::VK_COFF_IMGREL32, Ctx),
  350. 4);
  351. // Emit a sequence of FrameData records.
  352. FPOStateMachine FSM(FPO);
  353. FSM.emitFrameDataRecord(OS, FPO->Begin);
  354. for (const FPOInstruction &Inst : FPO->Instructions) {
  355. switch (Inst.Op) {
  356. case FPOInstruction::PushReg:
  357. FSM.CurOffset += 4;
  358. FSM.SavedRegSize += 4;
  359. FSM.RegSaveOffsets.push_back({Inst.RegOrOffset, FSM.CurOffset});
  360. break;
  361. case FPOInstruction::SetFrame:
  362. FSM.FrameReg = Inst.RegOrOffset;
  363. FSM.FrameRegOff = FSM.CurOffset;
  364. break;
  365. case FPOInstruction::StackAlign:
  366. FSM.StackOffsetBeforeAlign = FSM.CurOffset;
  367. FSM.StackAlign = Inst.RegOrOffset;
  368. break;
  369. case FPOInstruction::StackAlloc:
  370. FSM.CurOffset += Inst.RegOrOffset;
  371. FSM.LocalSize += Inst.RegOrOffset;
  372. // No need to emit FrameData for stack allocations with a frame pointer.
  373. if (FSM.FrameReg)
  374. continue;
  375. break;
  376. }
  377. FSM.emitFrameDataRecord(OS, Inst.Label);
  378. }
  379. OS.emitValueToAlignment(4, 0);
  380. OS.emitLabel(FrameEnd);
  381. return false;
  382. }
  383. MCTargetStreamer *llvm::createX86AsmTargetStreamer(MCStreamer &S,
  384. formatted_raw_ostream &OS,
  385. MCInstPrinter *InstPrinter,
  386. bool IsVerboseAsm) {
  387. // FIXME: This makes it so we textually assemble COFF directives on ELF.
  388. // That's kind of nonsensical.
  389. return new X86WinCOFFAsmTargetStreamer(S, OS, *InstPrinter);
  390. }
  391. MCTargetStreamer *
  392. llvm::createX86ObjectTargetStreamer(MCStreamer &S, const MCSubtargetInfo &STI) {
  393. // No need to register a target streamer.
  394. if (!STI.getTargetTriple().isOSBinFormatCOFF())
  395. return nullptr;
  396. // Registers itself to the MCStreamer.
  397. return new X86WinCOFFTargetStreamer(S);
  398. }