LLVM API Documentation

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
WinCOFFStreamer.cpp
Go to the documentation of this file.
1 //===-- llvm/MC/WinCOFFStreamer.cpp -----------------------------*- 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 // This file contains an implementation of a Win32 COFF object file streamer.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #define DEBUG_TYPE "WinCOFFStreamer"
15 
16 #include "llvm/MC/MCStreamer.h"
17 #include "llvm/MC/MCAsmBackend.h"
18 #include "llvm/MC/MCAsmLayout.h"
19 #include "llvm/MC/MCAssembler.h"
20 #include "llvm/MC/MCCodeEmitter.h"
21 #include "llvm/MC/MCContext.h"
22 #include "llvm/MC/MCExpr.h"
24 #include "llvm/MC/MCSection.h"
25 #include "llvm/MC/MCSectionCOFF.h"
26 #include "llvm/MC/MCSymbol.h"
27 #include "llvm/MC/MCValue.h"
28 #include "llvm/MC/MCWin64EH.h"
29 #include "llvm/Support/COFF.h"
30 #include "llvm/Support/Debug.h"
34 
35 using namespace llvm;
36 
37 namespace {
38 class WinCOFFStreamer : public MCObjectStreamer {
39 public:
40  MCSymbol const *CurSymbol;
41 
42  WinCOFFStreamer(MCContext &Context,
43  MCAsmBackend &MAB,
44  MCCodeEmitter &CE,
45  raw_ostream &OS);
46 
47  void AddCommonSymbol(MCSymbol *Symbol, uint64_t Size,
48  unsigned ByteAlignment, bool External);
49 
50  // MCStreamer interface
51 
52  virtual void InitSections();
53  virtual void InitToTextSection();
54  virtual void EmitLabel(MCSymbol *Symbol);
55  virtual void EmitDebugLabel(MCSymbol *Symbol);
56  virtual void EmitAssemblerFlag(MCAssemblerFlag Flag);
57  virtual void EmitThumbFunc(MCSymbol *Func);
58  virtual bool EmitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute);
59  virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue);
60  virtual void BeginCOFFSymbolDef(MCSymbol const *Symbol);
61  virtual void EmitCOFFSymbolStorageClass(int StorageClass);
62  virtual void EmitCOFFSymbolType(int Type);
63  virtual void EndCOFFSymbolDef();
64  virtual void EmitCOFFSecRel32(MCSymbol const *Symbol);
65  virtual void EmitELFSize(MCSymbol *Symbol, const MCExpr *Value);
66  virtual void EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
67  unsigned ByteAlignment);
68  virtual void EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size,
69  unsigned ByteAlignment);
70  virtual void EmitZerofill(const MCSection *Section, MCSymbol *Symbol,
71  uint64_t Size,unsigned ByteAlignment);
72  virtual void EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol,
73  uint64_t Size, unsigned ByteAlignment);
74  virtual void EmitFileDirective(StringRef Filename);
75  virtual void EmitIdent(StringRef IdentString);
76  virtual void EmitWin64EHHandlerData();
77  virtual void FinishImpl();
78 
79 private:
80  virtual void EmitInstToData(const MCInst &Inst) {
81  MCDataFragment *DF = getOrCreateDataFragment();
82 
85  raw_svector_ostream VecOS(Code);
86  getAssembler().getEmitter().EncodeInstruction(Inst, VecOS, Fixups);
87  VecOS.flush();
88 
89  // Add the fixups and data.
90  for (unsigned i = 0, e = Fixups.size(); i != e; ++i) {
91  Fixups[i].setOffset(Fixups[i].getOffset() + DF->getContents().size());
92  DF->getFixups().push_back(Fixups[i]);
93  }
94  DF->getContents().append(Code.begin(), Code.end());
95  }
96 
97  void SetSection(StringRef Section,
98  unsigned Characteristics,
99  SectionKind Kind) {
100  SwitchSection(getContext().getCOFFSection(Section, Characteristics, Kind));
101  }
102 
103  void SetSectionText() {
104  SetSection(".text",
105  COFF::IMAGE_SCN_CNT_CODE
106  | COFF::IMAGE_SCN_MEM_EXECUTE
107  | COFF::IMAGE_SCN_MEM_READ,
109  EmitCodeAlignment(4, 0);
110  }
111 
112  void SetSectionData() {
113  SetSection(".data",
114  COFF::IMAGE_SCN_CNT_INITIALIZED_DATA
115  | COFF::IMAGE_SCN_MEM_READ
116  | COFF::IMAGE_SCN_MEM_WRITE,
118  EmitCodeAlignment(4, 0);
119  }
120 
121  void SetSectionBSS() {
122  SetSection(".bss",
123  COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA
124  | COFF::IMAGE_SCN_MEM_READ
125  | COFF::IMAGE_SCN_MEM_WRITE,
127  EmitCodeAlignment(4, 0);
128  }
129 };
130 } // end anonymous namespace.
131 
132 WinCOFFStreamer::WinCOFFStreamer(MCContext &Context, MCAsmBackend &MAB,
133  MCCodeEmitter &CE, raw_ostream &OS)
134  : MCObjectStreamer(Context, 0, MAB, OS, &CE), CurSymbol(NULL) {}
135 
136 void WinCOFFStreamer::AddCommonSymbol(MCSymbol *Symbol, uint64_t Size,
137  unsigned ByteAlignment, bool External) {
138  assert(!Symbol->isInSection() && "Symbol must not already have a section!");
139 
140  std::string SectionName(".bss$linkonce");
141  SectionName.append(Symbol->getName().begin(), Symbol->getName().end());
142 
143  MCSymbolData &SymbolData = getAssembler().getOrCreateSymbolData(*Symbol);
144 
145  unsigned Characteristics =
146  COFF::IMAGE_SCN_LNK_COMDAT |
147  COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA |
148  COFF::IMAGE_SCN_MEM_READ |
149  COFF::IMAGE_SCN_MEM_WRITE;
150 
151  int Selection = COFF::IMAGE_COMDAT_SELECT_LARGEST;
152 
153  const MCSection *Section = MCStreamer::getContext().getCOFFSection(
154  SectionName, Characteristics, SectionKind::getBSS(), Selection);
155 
156  MCSectionData &SectionData = getAssembler().getOrCreateSectionData(*Section);
157 
158  if (SectionData.getAlignment() < ByteAlignment)
159  SectionData.setAlignment(ByteAlignment);
160 
161  SymbolData.setExternal(External);
162 
163  AssignSection(Symbol, Section);
164 
165  if (ByteAlignment != 1)
166  new MCAlignFragment(ByteAlignment, 0, 0, ByteAlignment, &SectionData);
167 
168  SymbolData.setFragment(new MCFillFragment(0, 0, Size, &SectionData));
169 }
170 
171 // MCStreamer interface
172 
173 void WinCOFFStreamer::InitToTextSection() {
174  SetSectionText();
175 }
176 
177 void WinCOFFStreamer::InitSections() {
178  SetSectionText();
179  SetSectionData();
180  SetSectionBSS();
181  SetSectionText();
182 }
183 
184 void WinCOFFStreamer::EmitLabel(MCSymbol *Symbol) {
185  assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
186  MCObjectStreamer::EmitLabel(Symbol);
187 }
188 
189 void WinCOFFStreamer::EmitDebugLabel(MCSymbol *Symbol) {
190  EmitLabel(Symbol);
191 }
192 void WinCOFFStreamer::EmitAssemblerFlag(MCAssemblerFlag Flag) {
193  llvm_unreachable("not implemented");
194 }
195 
196 void WinCOFFStreamer::EmitThumbFunc(MCSymbol *Func) {
197  llvm_unreachable("not implemented");
198 }
199 
200 bool WinCOFFStreamer::EmitSymbolAttribute(MCSymbol *Symbol,
202  assert(Symbol && "Symbol must be non-null!");
203  assert((Symbol->isInSection()
204  ? Symbol->getSection().getVariant() == MCSection::SV_COFF
205  : true) && "Got non COFF section in the COFF backend!");
206  switch (Attribute) {
207  case MCSA_WeakReference:
208  case MCSA_Weak: {
209  MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
211  SD.setExternal(true);
212  }
213  break;
214 
215  case MCSA_Global:
216  getAssembler().getOrCreateSymbolData(*Symbol).setExternal(true);
217  break;
218 
219  default:
220  return false;
221  }
222 
223  return true;
224 }
225 
226 void WinCOFFStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
227  llvm_unreachable("not implemented");
228 }
229 
230 void WinCOFFStreamer::BeginCOFFSymbolDef(MCSymbol const *Symbol) {
231  assert((Symbol->isInSection()
232  ? Symbol->getSection().getVariant() == MCSection::SV_COFF
233  : true) && "Got non COFF section in the COFF backend!");
234  assert(CurSymbol == NULL && "EndCOFFSymbolDef must be called between calls "
235  "to BeginCOFFSymbolDef!");
236  CurSymbol = Symbol;
237 }
238 
239 void WinCOFFStreamer::EmitCOFFSymbolStorageClass(int StorageClass) {
240  assert(CurSymbol != NULL && "BeginCOFFSymbolDef must be called first!");
241  assert((StorageClass & ~0xFF) == 0 && "StorageClass must only have data in "
242  "the first byte!");
243 
244  getAssembler().getOrCreateSymbolData(*CurSymbol).modifyFlags(
245  StorageClass << COFF::SF_ClassShift,
247 }
248 
249 void WinCOFFStreamer::EmitCOFFSymbolType(int Type) {
250  assert(CurSymbol != NULL && "BeginCOFFSymbolDef must be called first!");
251  assert((Type & ~0xFFFF) == 0 && "Type must only have data in the first 2 "
252  "bytes");
253 
254  getAssembler().getOrCreateSymbolData(*CurSymbol).modifyFlags(
255  Type << COFF::SF_TypeShift,
257 }
258 
259 void WinCOFFStreamer::EndCOFFSymbolDef() {
260  assert(CurSymbol != NULL && "BeginCOFFSymbolDef must be called first!");
261  CurSymbol = NULL;
262 }
263 
264 void WinCOFFStreamer::EmitCOFFSecRel32(MCSymbol const *Symbol)
265 {
266  MCDataFragment *DF = getOrCreateDataFragment();
267 
268  DF->getFixups().push_back(
269  MCFixup::Create(DF->getContents().size(),
270  MCSymbolRefExpr::Create (Symbol, getContext ()),
271  FK_SecRel_4));
272  DF->getContents().resize(DF->getContents().size() + 4, 0);
273 }
274 
275 void WinCOFFStreamer::EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) {
276  llvm_unreachable("not implemented");
277 }
278 
279 void WinCOFFStreamer::EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
280  unsigned ByteAlignment) {
281  assert((Symbol->isInSection()
282  ? Symbol->getSection().getVariant() == MCSection::SV_COFF
283  : true) && "Got non COFF section in the COFF backend!");
284  AddCommonSymbol(Symbol, Size, ByteAlignment, true);
285 }
286 
287 void WinCOFFStreamer::EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size,
288  unsigned ByteAlignment) {
289  assert((Symbol->isInSection()
290  ? Symbol->getSection().getVariant() == MCSection::SV_COFF
291  : true) && "Got non COFF section in the COFF backend!");
292  AddCommonSymbol(Symbol, Size, ByteAlignment, false);
293 }
294 
295 void WinCOFFStreamer::EmitZerofill(const MCSection *Section, MCSymbol *Symbol,
296  uint64_t Size,unsigned ByteAlignment) {
297  llvm_unreachable("not implemented");
298 }
299 
300 void WinCOFFStreamer::EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol,
301  uint64_t Size, unsigned ByteAlignment) {
302  llvm_unreachable("not implemented");
303 }
304 
305 void WinCOFFStreamer::EmitFileDirective(StringRef Filename) {
306  // Ignore for now, linkers don't care, and proper debug
307  // info will be a much large effort.
308 }
309 
310 // TODO: Implement this if you want to emit .comment section in COFF obj files.
311 void WinCOFFStreamer::EmitIdent(StringRef IdentString) {
312  llvm_unreachable("unsupported directive");
313 }
314 
315 void WinCOFFStreamer::EmitWin64EHHandlerData() {
316  MCStreamer::EmitWin64EHHandlerData();
317 
318  // We have to emit the unwind info now, because this directive
319  // actually switches to the .xdata section!
320  MCWin64EHUnwindEmitter::EmitUnwindInfo(*this, getCurrentW64UnwindInfo());
321 }
322 
323 void WinCOFFStreamer::FinishImpl() {
324  EmitW64Tables();
325  MCObjectStreamer::FinishImpl();
326 }
327 
328 namespace llvm
329 {
331  MCAsmBackend &MAB,
332  MCCodeEmitter &CE,
333  raw_ostream &OS,
334  bool RelaxAll) {
335  WinCOFFStreamer *S = new WinCOFFStreamer(Context, MAB, CE, OS);
336  S->getAssembler().setRelaxAll(RelaxAll);
337  return S;
338  }
339 }
void setExternal(bool Value)
Definition: MCAssembler.h:731
SectionVariant getVariant() const
Definition: MCSection.h:49
static SectionKind getDataRel()
Definition: SectionKind.h:229
virtual SmallVectorImpl< char > & getContents()
Definition: MCAssembler.h:221
COFF::SymbolStorageClass StorageClass
Definition: COFFYAML.cpp:204
static void EmitUnwindInfo(MCStreamer &streamer, MCWin64EHUnwindInfo *info)
Definition: MCWin64EH.cpp:156
SmallVectorImpl< MCFixup > & getFixups()
Definition: MCAssembler.h:224
static SectionKind getBSS()
Definition: SectionKind.h:225
const MCSection & getSection() const
Definition: MCSymbol.h:111
#define llvm_unreachable(msg)
A four-byte section relative fixup.
Definition: MCFixup.h:37
bool isInSection() const
Definition: MCSymbol.h:95
Streaming object file generation interface.
MCStreamer * createWinCOFFStreamer(MCContext &Ctx, MCAsmBackend &TAB, MCCodeEmitter &CE, raw_ostream &OS, bool RelaxAll=false)
iterator begin() const
Definition: StringRef.h:97
MCCodeEmitter - Generic instruction encoding interface.
Definition: MCCodeEmitter.h:22
.weak_reference (MachO)
Definition: MCDirectives.h:43
void append(in_iter in_start, in_iter in_end)
Definition: SmallVector.h:445
unsigned getAlignment() const
Definition: MCAssembler.h:607
MCSymbolAttr
Definition: MCDirectives.h:19
StringRef getName() const
getName - Get the symbol name.
Definition: MCSymbol.h:70
.type _foo,
Definition: MCDirectives.h:30
MCAssemblerFlag
Definition: MCDirectives.h:47
void setFragment(MCFragment *Value)
Definition: MCAssembler.h:721
void resize(unsigned N)
Definition: SmallVector.h:401
void setAlignment(unsigned Value)
Definition: MCAssembler.h:608
LLVM Value Representation.
Definition: Value.h:66
MCAsmBackend - Generic interface to target specific assembler backends.
Definition: MCAsmBackend.h:34
cl::opt< bool > RelaxAll("mc-relax-all", cl::desc("When used with filetype=obj, ""relax all fixups in the emitted object file"))
iterator end() const
Definition: StringRef.h:99
bool isUndefined() const
isUndefined - Check if this symbol undefined (i.e., implicitly defined).
Definition: MCSymbol.h:100
COFF::SectionCharacteristics Characteristics
Definition: COFFYAML.cpp:196
void modifyFlags(uint32_t Value, uint32_t Mask)
modifyFlags - Modify the flags via a mask
Definition: MCAssembler.h:776
static SectionKind getText()
Definition: SectionKind.h:208