LLVM  7.0.0svn
MCWin64EH.cpp
Go to the documentation of this file.
1 //===- lib/MC/MCWin64EH.cpp - MCWin64EH implementation --------------------===//
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 #include "llvm/MC/MCWin64EH.h"
11 #include "llvm/ADT/Twine.h"
12 #include "llvm/MC/MCContext.h"
13 #include "llvm/MC/MCExpr.h"
14 #include "llvm/MC/MCStreamer.h"
15 #include "llvm/MC/MCSymbol.h"
16 #include "llvm/Support/Win64EH.h"
17 
18 using namespace llvm;
19 
20 // NOTE: All relocations generated here are 4-byte image-relative.
21 
22 static uint8_t CountOfUnwindCodes(std::vector<WinEH::Instruction> &Insns) {
23  uint8_t Count = 0;
24  for (const auto &I : Insns) {
25  switch (static_cast<Win64EH::UnwindOpcodes>(I.Operation)) {
30  Count += 1;
31  break;
34  Count += 2;
35  break;
38  Count += 3;
39  break;
41  Count += (I.Offset > 512 * 1024 - 8) ? 3 : 2;
42  break;
43  }
44  }
45  return Count;
46 }
47 
48 static void EmitAbsDifference(MCStreamer &Streamer, const MCSymbol *LHS,
49  const MCSymbol *RHS) {
50  MCContext &Context = Streamer.getContext();
51  const MCExpr *Diff =
53  MCSymbolRefExpr::create(RHS, Context), Context);
54  Streamer.EmitValue(Diff, 1);
55 }
56 
57 static void EmitUnwindCode(MCStreamer &streamer, const MCSymbol *begin,
58  WinEH::Instruction &inst) {
59  uint8_t b2;
60  uint16_t w;
61  b2 = (inst.Operation & 0x0F);
62  switch (static_cast<Win64EH::UnwindOpcodes>(inst.Operation)) {
64  EmitAbsDifference(streamer, inst.Label, begin);
65  b2 |= (inst.Register & 0x0F) << 4;
66  streamer.EmitIntValue(b2, 1);
67  break;
69  EmitAbsDifference(streamer, inst.Label, begin);
70  if (inst.Offset > 512 * 1024 - 8) {
71  b2 |= 0x10;
72  streamer.EmitIntValue(b2, 1);
73  w = inst.Offset & 0xFFF8;
74  streamer.EmitIntValue(w, 2);
75  w = inst.Offset >> 16;
76  } else {
77  streamer.EmitIntValue(b2, 1);
78  w = inst.Offset >> 3;
79  }
80  streamer.EmitIntValue(w, 2);
81  break;
83  b2 |= (((inst.Offset - 8) >> 3) & 0x0F) << 4;
84  EmitAbsDifference(streamer, inst.Label, begin);
85  streamer.EmitIntValue(b2, 1);
86  break;
88  EmitAbsDifference(streamer, inst.Label, begin);
89  streamer.EmitIntValue(b2, 1);
90  break;
93  b2 |= (inst.Register & 0x0F) << 4;
94  EmitAbsDifference(streamer, inst.Label, begin);
95  streamer.EmitIntValue(b2, 1);
96  w = inst.Offset >> 3;
98  w >>= 1;
99  streamer.EmitIntValue(w, 2);
100  break;
103  b2 |= (inst.Register & 0x0F) << 4;
104  EmitAbsDifference(streamer, inst.Label, begin);
105  streamer.EmitIntValue(b2, 1);
107  w = inst.Offset & 0xFFF0;
108  else
109  w = inst.Offset & 0xFFF8;
110  streamer.EmitIntValue(w, 2);
111  w = inst.Offset >> 16;
112  streamer.EmitIntValue(w, 2);
113  break;
115  if (inst.Offset == 1)
116  b2 |= 0x10;
117  EmitAbsDifference(streamer, inst.Label, begin);
118  streamer.EmitIntValue(b2, 1);
119  break;
120  }
121 }
122 
123 static void EmitSymbolRefWithOfs(MCStreamer &streamer,
124  const MCSymbol *Base,
125  const MCSymbol *Other) {
126  MCContext &Context = streamer.getContext();
127  const MCSymbolRefExpr *BaseRef = MCSymbolRefExpr::create(Base, Context);
128  const MCSymbolRefExpr *OtherRef = MCSymbolRefExpr::create(Other, Context);
129  const MCExpr *Ofs = MCBinaryExpr::createSub(OtherRef, BaseRef, Context);
130  const MCSymbolRefExpr *BaseRefRel = MCSymbolRefExpr::create(Base,
132  Context);
133  streamer.EmitValue(MCBinaryExpr::createAdd(BaseRefRel, Ofs, Context), 4);
134 }
135 
136 static void EmitRuntimeFunction(MCStreamer &streamer,
137  const WinEH::FrameInfo *info) {
138  MCContext &context = streamer.getContext();
139 
140  streamer.EmitValueToAlignment(4);
141  EmitSymbolRefWithOfs(streamer, info->Function, info->Begin);
142  EmitSymbolRefWithOfs(streamer, info->Function, info->End);
145  context), 4);
146 }
147 
148 static void EmitUnwindInfo(MCStreamer &streamer, WinEH::FrameInfo *info) {
149  // If this UNWIND_INFO already has a symbol, it's already been emitted.
150  if (info->Symbol)
151  return;
152 
153  MCContext &context = streamer.getContext();
154  MCSymbol *Label = context.createTempSymbol();
155 
156  streamer.EmitValueToAlignment(4);
157  streamer.EmitLabel(Label);
158  info->Symbol = Label;
159 
160  // Upper 3 bits are the version number (currently 1).
161  uint8_t flags = 0x01;
162  if (info->ChainedParent)
163  flags |= Win64EH::UNW_ChainInfo << 3;
164  else {
165  if (info->HandlesUnwind)
166  flags |= Win64EH::UNW_TerminateHandler << 3;
167  if (info->HandlesExceptions)
168  flags |= Win64EH::UNW_ExceptionHandler << 3;
169  }
170  streamer.EmitIntValue(flags, 1);
171 
172  if (info->PrologEnd)
173  EmitAbsDifference(streamer, info->PrologEnd, info->Begin);
174  else
175  streamer.EmitIntValue(0, 1);
176 
177  uint8_t numCodes = CountOfUnwindCodes(info->Instructions);
178  streamer.EmitIntValue(numCodes, 1);
179 
180  uint8_t frame = 0;
181  if (info->LastFrameInst >= 0) {
182  WinEH::Instruction &frameInst = info->Instructions[info->LastFrameInst];
183  assert(frameInst.Operation == Win64EH::UOP_SetFPReg);
184  frame = (frameInst.Register & 0x0F) | (frameInst.Offset & 0xF0);
185  }
186  streamer.EmitIntValue(frame, 1);
187 
188  // Emit unwind instructions (in reverse order).
189  uint8_t numInst = info->Instructions.size();
190  for (uint8_t c = 0; c < numInst; ++c) {
191  WinEH::Instruction inst = info->Instructions.back();
192  info->Instructions.pop_back();
193  EmitUnwindCode(streamer, info->Begin, inst);
194  }
195 
196  // For alignment purposes, the instruction array will always have an even
197  // number of entries, with the final entry potentially unused (in which case
198  // the array will be one longer than indicated by the count of unwind codes
199  // field).
200  if (numCodes & 1) {
201  streamer.EmitIntValue(0, 2);
202  }
203 
204  if (flags & (Win64EH::UNW_ChainInfo << 3))
205  EmitRuntimeFunction(streamer, info->ChainedParent);
206  else if (flags &
210  context), 4);
211  else if (numCodes == 0) {
212  // The minimum size of an UNWIND_INFO struct is 8 bytes. If we're not
213  // a chained unwind info, if there is no handler, and if there are fewer
214  // than 2 slots used in the unwind code array, we have to pad to 8 bytes.
215  streamer.EmitIntValue(0, 4);
216  }
217 }
218 
220  // Emit the unwind info structs first.
221  for (const auto &CFI : Streamer.getWinFrameInfos()) {
222  MCSection *XData = Streamer.getAssociatedXDataSection(CFI->TextSection);
223  Streamer.SwitchSection(XData);
224  ::EmitUnwindInfo(Streamer, CFI.get());
225  }
226 
227  // Now emit RUNTIME_FUNCTION entries.
228  for (const auto &CFI : Streamer.getWinFrameInfos()) {
229  MCSection *PData = Streamer.getAssociatedPDataSection(CFI->TextSection);
230  Streamer.SwitchSection(PData);
231  EmitRuntimeFunction(Streamer, CFI.get());
232  }
233 }
234 
236  MCStreamer &Streamer, WinEH::FrameInfo *info) const {
237  // Switch sections (the static function above is meant to be called from
238  // here and from Emit().
239  MCSection *XData = Streamer.getAssociatedXDataSection(info->TextSection);
240  Streamer.SwitchSection(XData);
241 
242  ::EmitUnwindInfo(Streamer, info);
243 }
244 
Instances of this class represent a uniqued identifier for a section in the current translation unit...
Definition: MCSection.h:39
UNW_ChainInfo - Specifies that this UnwindInfo structure is chained to another one.
Definition: Win64EH.h:65
LLVMContext & Context
const_iterator begin(StringRef path, Style style=Style::native)
Get begin iterator over path.
Definition: Path.cpp:236
static const MCSymbolRefExpr * create(const MCSymbol *Symbol, MCContext &Ctx)
Definition: MCExpr.h:313
Compute iterated dominance frontiers using a linear time algorithm.
Definition: AllocatorList.h:24
UNW_ExceptionHandler - Specifies that this function has an exception handler.
Definition: Win64EH.h:59
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:42
const MCSymbol * End
Definition: MCWinEH.h:33
const unsigned Register
Definition: MCWinEH.h:24
static void EmitUnwindCode(MCStreamer &streamer, const MCSymbol *begin, WinEH::Instruction &inst)
Definition: MCWin64EH.cpp:57
std::vector< Instruction > Instructions
Definition: MCWinEH.h:45
UNW_TerminateHandler - Specifies that this function has a termination handler.
Definition: Win64EH.h:62
MCContext & getContext() const
Definition: MCStreamer.h:246
Base class for the full range of assembler expressions which are needed for parsing.
Definition: MCExpr.h:36
Represent a reference to a symbol from inside an expression.
Definition: MCExpr.h:165
static void EmitAbsDifference(MCStreamer &Streamer, const MCSymbol *LHS, const MCSymbol *RHS)
Definition: MCWin64EH.cpp:48
ArrayRef< std::unique_ptr< WinEH::FrameInfo > > getWinFrameInfos() const
Definition: MCStreamer.h:260
Context object for machine code objects.
Definition: MCContext.h:61
static const MCBinaryExpr * createSub(const MCExpr *LHS, const MCExpr *RHS, MCContext &Ctx)
Definition: MCExpr.h:536
static const MCBinaryExpr * createAdd(const MCExpr *LHS, const MCExpr *RHS, MCContext &Ctx)
Definition: MCExpr.h:451
virtual void EmitIntValue(uint64_t Value, unsigned Size)
Special case of EmitValue that avoids the client having to pass in a MCExpr for constant integers...
Definition: MCStreamer.cpp:110
void EmitValue(const MCExpr *Value, unsigned Size, SMLoc Loc=SMLoc())
Definition: MCStreamer.cpp:141
static void EmitSymbolRefWithOfs(MCStreamer &streamer, const MCSymbol *Base, const MCSymbol *Other)
Definition: MCWin64EH.cpp:123
Streaming machine code generation interface.
Definition: MCStreamer.h:181
MCSymbol * createTempSymbol(bool CanBeUnnamed=true)
Create and return a new assembler temporary symbol with a unique but unspecified name.
Definition: MCContext.cpp:215
const FrameInfo * ChainedParent
Definition: MCWinEH.h:44
static void EmitRuntimeFunction(MCStreamer &streamer, const WinEH::FrameInfo *info)
Definition: MCWin64EH.cpp:136
virtual void SwitchSection(MCSection *Section, const MCExpr *Subsection=nullptr)
Set the current section where code is being emitted to Section.
Definition: MCStreamer.cpp:958
MCSection * getAssociatedPDataSection(const MCSection *TextSec)
Get the .pdata section used for the given section.
Definition: MCStreamer.cpp:671
virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value=0, unsigned ValueSize=1, unsigned MaxBytesToEmit=0)
Emit some number of copies of Value until the byte alignment ByteAlignment is reached.
Definition: MCStreamer.cpp:946
void EmitUnwindInfo(MCStreamer &Streamer, WinEH::FrameInfo *FI) const override
Definition: MCWin64EH.cpp:235
const MCSymbol * Symbol
Definition: MCWinEH.h:37
lazy value info
const MCSymbol * Begin
Definition: MCWinEH.h:32
const MCSymbol * PrologEnd
Definition: MCWinEH.h:36
const MCSection * TextSection
Definition: MCWinEH.h:38
const MCSymbol * Label
Definition: MCWinEH.h:22
const MCSymbol * Function
Definition: MCWinEH.h:35
#define I(x, y, z)
Definition: MD5.cpp:58
static uint8_t CountOfUnwindCodes(std::vector< WinEH::Instruction > &Insns)
Definition: MCWin64EH.cpp:22
const unsigned Offset
Definition: MCWinEH.h:23
const unsigned Operation
Definition: MCWinEH.h:25
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
virtual void EmitLabel(MCSymbol *Symbol, SMLoc Loc=SMLoc())
Emit a label for Symbol into the current section.
Definition: MCStreamer.cpp:309
static void EmitUnwindInfo(MCStreamer &streamer, WinEH::FrameInfo *info)
Definition: MCWin64EH.cpp:148
const MCSymbol * ExceptionHandler
Definition: MCWinEH.h:34
void Emit(MCStreamer &Streamer) const override
This emits the unwind info sections (.pdata and .xdata in PE/COFF).
Definition: MCWin64EH.cpp:219
MCSection * getAssociatedXDataSection(const MCSection *TextSec)
Get the .xdata section used for the given section.
Definition: MCStreamer.cpp:677