LLVM  6.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"
15 #include "llvm/MC/MCSectionCOFF.h"
16 #include "llvm/MC/MCStreamer.h"
17 #include "llvm/MC/MCSymbol.h"
18 #include "llvm/Support/Win64EH.h"
19 
20 using namespace llvm;
21 
22 // NOTE: All relocations generated here are 4-byte image-relative.
23 
24 static uint8_t CountOfUnwindCodes(std::vector<WinEH::Instruction> &Insns) {
25  uint8_t Count = 0;
26  for (const auto &I : Insns) {
27  switch (static_cast<Win64EH::UnwindOpcodes>(I.Operation)) {
32  Count += 1;
33  break;
36  Count += 2;
37  break;
40  Count += 3;
41  break;
43  Count += (I.Offset > 512 * 1024 - 8) ? 3 : 2;
44  break;
45  }
46  }
47  return Count;
48 }
49 
50 static void EmitAbsDifference(MCStreamer &Streamer, const MCSymbol *LHS,
51  const MCSymbol *RHS) {
52  MCContext &Context = Streamer.getContext();
53  const MCExpr *Diff =
55  MCSymbolRefExpr::create(RHS, Context), Context);
56  Streamer.EmitValue(Diff, 1);
57 }
58 
59 static void EmitUnwindCode(MCStreamer &streamer, const MCSymbol *begin,
60  WinEH::Instruction &inst) {
61  uint8_t b2;
62  uint16_t w;
63  b2 = (inst.Operation & 0x0F);
64  switch (static_cast<Win64EH::UnwindOpcodes>(inst.Operation)) {
66  EmitAbsDifference(streamer, inst.Label, begin);
67  b2 |= (inst.Register & 0x0F) << 4;
68  streamer.EmitIntValue(b2, 1);
69  break;
71  EmitAbsDifference(streamer, inst.Label, begin);
72  if (inst.Offset > 512 * 1024 - 8) {
73  b2 |= 0x10;
74  streamer.EmitIntValue(b2, 1);
75  w = inst.Offset & 0xFFF8;
76  streamer.EmitIntValue(w, 2);
77  w = inst.Offset >> 16;
78  } else {
79  streamer.EmitIntValue(b2, 1);
80  w = inst.Offset >> 3;
81  }
82  streamer.EmitIntValue(w, 2);
83  break;
85  b2 |= (((inst.Offset - 8) >> 3) & 0x0F) << 4;
86  EmitAbsDifference(streamer, inst.Label, begin);
87  streamer.EmitIntValue(b2, 1);
88  break;
90  EmitAbsDifference(streamer, inst.Label, begin);
91  streamer.EmitIntValue(b2, 1);
92  break;
95  b2 |= (inst.Register & 0x0F) << 4;
96  EmitAbsDifference(streamer, inst.Label, begin);
97  streamer.EmitIntValue(b2, 1);
98  w = inst.Offset >> 3;
100  w >>= 1;
101  streamer.EmitIntValue(w, 2);
102  break;
105  b2 |= (inst.Register & 0x0F) << 4;
106  EmitAbsDifference(streamer, inst.Label, begin);
107  streamer.EmitIntValue(b2, 1);
109  w = inst.Offset & 0xFFF0;
110  else
111  w = inst.Offset & 0xFFF8;
112  streamer.EmitIntValue(w, 2);
113  w = inst.Offset >> 16;
114  streamer.EmitIntValue(w, 2);
115  break;
117  if (inst.Offset == 1)
118  b2 |= 0x10;
119  EmitAbsDifference(streamer, inst.Label, begin);
120  streamer.EmitIntValue(b2, 1);
121  break;
122  }
123 }
124 
125 static void EmitSymbolRefWithOfs(MCStreamer &streamer,
126  const MCSymbol *Base,
127  const MCSymbol *Other) {
128  MCContext &Context = streamer.getContext();
129  const MCSymbolRefExpr *BaseRef = MCSymbolRefExpr::create(Base, Context);
130  const MCSymbolRefExpr *OtherRef = MCSymbolRefExpr::create(Other, Context);
131  const MCExpr *Ofs = MCBinaryExpr::createSub(OtherRef, BaseRef, Context);
132  const MCSymbolRefExpr *BaseRefRel = MCSymbolRefExpr::create(Base,
134  Context);
135  streamer.EmitValue(MCBinaryExpr::createAdd(BaseRefRel, Ofs, Context), 4);
136 }
137 
138 static void EmitRuntimeFunction(MCStreamer &streamer,
139  const WinEH::FrameInfo *info) {
140  MCContext &context = streamer.getContext();
141 
142  streamer.EmitValueToAlignment(4);
143  EmitSymbolRefWithOfs(streamer, info->Function, info->Begin);
144  EmitSymbolRefWithOfs(streamer, info->Function, info->End);
147  context), 4);
148 }
149 
150 static void EmitUnwindInfo(MCStreamer &streamer, WinEH::FrameInfo *info) {
151  // If this UNWIND_INFO already has a symbol, it's already been emitted.
152  if (info->Symbol)
153  return;
154 
155  MCContext &context = streamer.getContext();
156  MCSymbol *Label = context.createTempSymbol();
157 
158  streamer.EmitValueToAlignment(4);
159  streamer.EmitLabel(Label);
160  info->Symbol = Label;
161 
162  // Upper 3 bits are the version number (currently 1).
163  uint8_t flags = 0x01;
164  if (info->ChainedParent)
165  flags |= Win64EH::UNW_ChainInfo << 3;
166  else {
167  if (info->HandlesUnwind)
168  flags |= Win64EH::UNW_TerminateHandler << 3;
169  if (info->HandlesExceptions)
170  flags |= Win64EH::UNW_ExceptionHandler << 3;
171  }
172  streamer.EmitIntValue(flags, 1);
173 
174  if (info->PrologEnd)
175  EmitAbsDifference(streamer, info->PrologEnd, info->Begin);
176  else
177  streamer.EmitIntValue(0, 1);
178 
179  uint8_t numCodes = CountOfUnwindCodes(info->Instructions);
180  streamer.EmitIntValue(numCodes, 1);
181 
182  uint8_t frame = 0;
183  if (info->LastFrameInst >= 0) {
184  WinEH::Instruction &frameInst = info->Instructions[info->LastFrameInst];
185  assert(frameInst.Operation == Win64EH::UOP_SetFPReg);
186  frame = (frameInst.Register & 0x0F) | (frameInst.Offset & 0xF0);
187  }
188  streamer.EmitIntValue(frame, 1);
189 
190  // Emit unwind instructions (in reverse order).
191  uint8_t numInst = info->Instructions.size();
192  for (uint8_t c = 0; c < numInst; ++c) {
193  WinEH::Instruction inst = info->Instructions.back();
194  info->Instructions.pop_back();
195  EmitUnwindCode(streamer, info->Begin, inst);
196  }
197 
198  // For alignment purposes, the instruction array will always have an even
199  // number of entries, with the final entry potentially unused (in which case
200  // the array will be one longer than indicated by the count of unwind codes
201  // field).
202  if (numCodes & 1) {
203  streamer.EmitIntValue(0, 2);
204  }
205 
206  if (flags & (Win64EH::UNW_ChainInfo << 3))
207  EmitRuntimeFunction(streamer, info->ChainedParent);
208  else if (flags &
212  context), 4);
213  else if (numCodes == 0) {
214  // The minimum size of an UNWIND_INFO struct is 8 bytes. If we're not
215  // a chained unwind info, if there is no handler, and if there are fewer
216  // than 2 slots used in the unwind code array, we have to pad to 8 bytes.
217  streamer.EmitIntValue(0, 4);
218  }
219 }
220 
222  // Emit the unwind info structs first.
223  for (WinEH::FrameInfo *CFI : Streamer.getWinFrameInfos()) {
224  MCSection *XData = Streamer.getAssociatedXDataSection(CFI->TextSection);
225  Streamer.SwitchSection(XData);
226  ::EmitUnwindInfo(Streamer, CFI);
227  }
228 
229  // Now emit RUNTIME_FUNCTION entries.
230  for (WinEH::FrameInfo *CFI : Streamer.getWinFrameInfos()) {
231  MCSection *PData = Streamer.getAssociatedPDataSection(CFI->TextSection);
232  Streamer.SwitchSection(PData);
233  EmitRuntimeFunction(Streamer, CFI);
234  }
235 }
236 
238  MCStreamer &Streamer, WinEH::FrameInfo *info) const {
239  // Switch sections (the static function above is meant to be called from
240  // here and from Emit().
241  MCSection *XData = Streamer.getAssociatedXDataSection(info->TextSection);
242  Streamer.SwitchSection(XData);
243 
244  ::EmitUnwindInfo(Streamer, info);
245 }
246 
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:234
static const MCSymbolRefExpr * create(const MCSymbol *Symbol, MCContext &Ctx)
Definition: MCExpr.h:305
Compute iterated dominance frontiers using a linear time algorithm.
Definition: AllocatorList.h:24
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:59
ArrayRef< WinEH::FrameInfo * > getWinFrameInfos() const
Definition: MCStreamer.h:240
std::vector< Instruction > Instructions
Definition: MCWinEH.h:45
MCContext & getContext() const
Definition: MCStreamer.h:226
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:50
Context object for machine code objects.
Definition: MCContext.h:59
static const MCBinaryExpr * createSub(const MCExpr *LHS, const MCExpr *RHS, MCContext &Ctx)
Definition: MCExpr.h:528
static const MCBinaryExpr * createAdd(const MCExpr *LHS, const MCExpr *RHS, MCContext &Ctx)
Definition: MCExpr.h:443
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:93
void EmitValue(const MCExpr *Value, unsigned Size, SMLoc Loc=SMLoc())
Definition: MCStreamer.cpp:124
static void EmitSymbolRefWithOfs(MCStreamer &streamer, const MCSymbol *Base, const MCSymbol *Other)
Definition: MCWin64EH.cpp:125
UNW_TerminateHandler - Specifies that this function has a termination handler.
Definition: Win64EH.h:62
Streaming machine code generation interface.
Definition: MCStreamer.h:167
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:138
UNW_ExceptionHandler - Specifies that this function has an exception handler.
Definition: Win64EH.h:59
virtual void SwitchSection(MCSection *Section, const MCExpr *Subsection=nullptr)
Set the current section where code is being emitted to Section.
Definition: MCStreamer.cpp:854
MCSection * getAssociatedPDataSection(const MCSection *TextSec)
Get the .pdata section used for the given section.
Definition: MCStreamer.cpp:605
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:842
void EmitUnwindInfo(MCStreamer &Streamer, WinEH::FrameInfo *FI) const override
Definition: MCWin64EH.cpp:237
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:24
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:301
static void EmitUnwindInfo(MCStreamer &streamer, WinEH::FrameInfo *info)
Definition: MCWin64EH.cpp:150
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:221
MCSection * getAssociatedXDataSection(const MCSection *TextSec)
Get the .xdata section used for the given section.
Definition: MCStreamer.cpp:611