blob: af313b2ff88715eb3c1985c20d528c83f3cb736f [file] [log] [blame]
Chris Lattner56725be2010-07-11 22:05:00 +00001//===-- 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
Chandler Carruthed0881b2012-12-03 16:50:05 +000016#include "llvm/MC/MCStreamer.h"
Evan Cheng5928e692011-07-25 23:24:55 +000017#include "llvm/MC/MCAsmBackend.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000018#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"
23#include "llvm/MC/MCObjectStreamer.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"
Chris Lattner56725be2010-07-11 22:05:00 +000029#include "llvm/Support/COFF.h"
30#include "llvm/Support/Debug.h"
31#include "llvm/Support/ErrorHandling.h"
Evan Cheng2bb40352011-08-24 18:08:43 +000032#include "llvm/Support/TargetRegistry.h"
Chris Lattner56725be2010-07-11 22:05:00 +000033#include "llvm/Support/raw_ostream.h"
Rafael Espindolad3df3d32011-12-17 01:14:52 +000034
Chris Lattner56725be2010-07-11 22:05:00 +000035using namespace llvm;
36
Chris Lattner56725be2010-07-11 22:05:00 +000037namespace {
38class WinCOFFStreamer : public MCObjectStreamer {
39public:
Michael J. Spencere2da0a42010-07-19 06:13:10 +000040 MCSymbol const *CurSymbol;
41
Chris Lattner56725be2010-07-11 22:05:00 +000042 WinCOFFStreamer(MCContext &Context,
Evan Cheng5928e692011-07-25 23:24:55 +000043 MCAsmBackend &MAB,
Chris Lattner56725be2010-07-11 22:05:00 +000044 MCCodeEmitter &CE,
45 raw_ostream &OS);
46
Michael J. Spencere2da0a42010-07-19 06:13:10 +000047 void AddCommonSymbol(MCSymbol *Symbol, uint64_t Size,
48 unsigned ByteAlignment, bool External);
49
Chris Lattner56725be2010-07-11 22:05:00 +000050 // MCStreamer interface
51
Rafael Espindolaf667d922010-09-15 21:48:40 +000052 virtual void InitSections();
Eli Benderskycbb25142013-01-14 19:04:57 +000053 virtual void InitToTextSection();
Rafael Espindola16795802010-11-28 16:22:59 +000054 virtual void EmitLabel(MCSymbol *Symbol);
Reed Kotleraee4d5d12012-12-16 04:00:45 +000055 virtual void EmitDebugLabel(MCSymbol *Symbol);
Chris Lattner56725be2010-07-11 22:05:00 +000056 virtual void EmitAssemblerFlag(MCAssemblerFlag Flag);
Jim Grosbach5a2c68d2010-11-05 22:08:08 +000057 virtual void EmitThumbFunc(MCSymbol *Func);
Saleem Abdulrasool4208b612013-08-09 01:52:03 +000058 virtual bool EmitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute);
Chris Lattner56725be2010-07-11 22:05:00 +000059 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();
Rafael Espindolad3df3d32011-12-17 01:14:52 +000064 virtual void EmitCOFFSecRel32(MCSymbol const *Symbol);
Chris Lattner56725be2010-07-11 22:05:00 +000065 virtual void EmitELFSize(MCSymbol *Symbol, const MCExpr *Value);
66 virtual void EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
Michael J. Spencere2da0a42010-07-19 06:13:10 +000067 unsigned ByteAlignment);
Benjamin Kramer63970512011-09-01 23:04:27 +000068 virtual void EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size,
69 unsigned ByteAlignment);
Chris Lattner56725be2010-07-11 22:05:00 +000070 virtual void EmitZerofill(const MCSection *Section, MCSymbol *Symbol,
Evan Chengf5bd6c62012-06-22 20:14:46 +000071 uint64_t Size,unsigned ByteAlignment);
Chris Lattner56725be2010-07-11 22:05:00 +000072 virtual void EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol,
Michael J. Spencere2da0a42010-07-19 06:13:10 +000073 uint64_t Size, unsigned ByteAlignment);
Chris Lattner56725be2010-07-11 22:05:00 +000074 virtual void EmitFileDirective(StringRef Filename);
Rafael Espindola5645bad2013-10-16 01:05:45 +000075 virtual void EmitIdent(StringRef IdentString);
Charles Davis1c8bd5a2011-05-22 03:01:05 +000076 virtual void EmitWin64EHHandlerData();
Rafael Espindola07082092012-01-07 03:13:18 +000077 virtual void FinishImpl();
Michael J. Spencer86bbd712010-10-09 15:44:27 +000078
79private:
Rafael Espindola72b54882010-11-01 16:27:31 +000080 virtual void EmitInstToData(const MCInst &Inst) {
Michael J. Spencer088925e2013-01-03 01:09:22 +000081 MCDataFragment *DF = getOrCreateDataFragment();
82
83 SmallVector<MCFixup, 4> Fixups;
84 SmallString<256> Code;
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());
Rafael Espindola72b54882010-11-01 16:27:31 +000095 }
96
Rafael Espindola3294e052013-11-25 16:00:32 +000097 const MCSectionCOFF *getSectionText() {
98 return getContext().getCOFFSection(
99 ".text", COFF::IMAGE_SCN_CNT_CODE | COFF::IMAGE_SCN_MEM_EXECUTE |
100 COFF::IMAGE_SCN_MEM_READ,
101 SectionKind::getText());
102 }
103
104 const MCSectionCOFF *getSectionData() {
105 return getContext().getCOFFSection(
106 ".data", COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
107 COFF::IMAGE_SCN_MEM_READ | COFF::IMAGE_SCN_MEM_WRITE,
108 SectionKind::getDataRel());
109 }
110
111 const MCSectionCOFF *getSectionBSS() {
112 return getContext().getCOFFSection(
113 ".bss", COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA |
114 COFF::IMAGE_SCN_MEM_READ | COFF::IMAGE_SCN_MEM_WRITE,
115 SectionKind::getBSS());
Michael J. Spencer86bbd712010-10-09 15:44:27 +0000116 }
117
118 void SetSectionText() {
Rafael Espindola3294e052013-11-25 16:00:32 +0000119 SwitchSection(getSectionText());
Michael J. Spencer86bbd712010-10-09 15:44:27 +0000120 EmitCodeAlignment(4, 0);
121 }
122
123 void SetSectionData() {
Rafael Espindola3294e052013-11-25 16:00:32 +0000124 SwitchSection(getSectionData());
Michael J. Spencer86bbd712010-10-09 15:44:27 +0000125 EmitCodeAlignment(4, 0);
126 }
127
128 void SetSectionBSS() {
Rafael Espindola3294e052013-11-25 16:00:32 +0000129 SwitchSection(getSectionBSS());
Michael J. Spencer86bbd712010-10-09 15:44:27 +0000130 EmitCodeAlignment(4, 0);
131 }
Chris Lattner56725be2010-07-11 22:05:00 +0000132};
133} // end anonymous namespace.
134
Chandler Carruthde093ef2013-01-31 23:29:57 +0000135WinCOFFStreamer::WinCOFFStreamer(MCContext &Context, MCAsmBackend &MAB,
136 MCCodeEmitter &CE, raw_ostream &OS)
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000137 : MCObjectStreamer(Context, 0, MAB, OS, &CE), CurSymbol(NULL) {}
Michael J. Spencere2da0a42010-07-19 06:13:10 +0000138
139void WinCOFFStreamer::AddCommonSymbol(MCSymbol *Symbol, uint64_t Size,
140 unsigned ByteAlignment, bool External) {
141 assert(!Symbol->isInSection() && "Symbol must not already have a section!");
142
Rafael Espindolaedcf1ff2013-11-25 16:06:04 +0000143 const MCSectionCOFF *Section = getSectionBSS();
Michael J. Spencere2da0a42010-07-19 06:13:10 +0000144 MCSectionData &SectionData = getAssembler().getOrCreateSectionData(*Section);
Michael J. Spencere2da0a42010-07-19 06:13:10 +0000145 if (SectionData.getAlignment() < ByteAlignment)
146 SectionData.setAlignment(ByteAlignment);
147
Rafael Espindolaedcf1ff2013-11-25 16:06:04 +0000148 MCSymbolData &SymbolData = getAssembler().getOrCreateSymbolData(*Symbol);
Michael J. Spencere2da0a42010-07-19 06:13:10 +0000149 SymbolData.setExternal(External);
150
Richard Mitton21101b32013-09-19 23:21:01 +0000151 AssignSection(Symbol, Section);
Michael J. Spencere2da0a42010-07-19 06:13:10 +0000152
153 if (ByteAlignment != 1)
154 new MCAlignFragment(ByteAlignment, 0, 0, ByteAlignment, &SectionData);
155
156 SymbolData.setFragment(new MCFillFragment(0, 0, Size, &SectionData));
Chris Lattner56725be2010-07-11 22:05:00 +0000157}
158
159// MCStreamer interface
160
Eli Benderskycbb25142013-01-14 19:04:57 +0000161void WinCOFFStreamer::InitToTextSection() {
162 SetSectionText();
163}
164
Rafael Espindolaf667d922010-09-15 21:48:40 +0000165void WinCOFFStreamer::InitSections() {
Michael J. Spencer86bbd712010-10-09 15:44:27 +0000166 SetSectionText();
167 SetSectionData();
168 SetSectionBSS();
169 SetSectionText();
Rafael Espindolaf667d922010-09-15 21:48:40 +0000170}
171
Rafael Espindola16795802010-11-28 16:22:59 +0000172void WinCOFFStreamer::EmitLabel(MCSymbol *Symbol) {
Rafael Espindola16795802010-11-28 16:22:59 +0000173 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
Rafael Espindolae5b74152010-11-28 17:18:55 +0000174 MCObjectStreamer::EmitLabel(Symbol);
Rafael Espindola16795802010-11-28 16:22:59 +0000175}
176
Reed Kotleraee4d5d12012-12-16 04:00:45 +0000177void WinCOFFStreamer::EmitDebugLabel(MCSymbol *Symbol) {
178 EmitLabel(Symbol);
179}
Chris Lattner56725be2010-07-11 22:05:00 +0000180void WinCOFFStreamer::EmitAssemblerFlag(MCAssemblerFlag Flag) {
Michael J. Spencere2da0a42010-07-19 06:13:10 +0000181 llvm_unreachable("not implemented");
Chris Lattner56725be2010-07-11 22:05:00 +0000182}
183
Jim Grosbach5a2c68d2010-11-05 22:08:08 +0000184void WinCOFFStreamer::EmitThumbFunc(MCSymbol *Func) {
185 llvm_unreachable("not implemented");
186}
187
Saleem Abdulrasool4208b612013-08-09 01:52:03 +0000188bool WinCOFFStreamer::EmitSymbolAttribute(MCSymbol *Symbol,
Chris Lattner56725be2010-07-11 22:05:00 +0000189 MCSymbolAttr Attribute) {
Michael J. Spencerbe52c622010-10-09 11:00:37 +0000190 assert(Symbol && "Symbol must be non-null!");
191 assert((Symbol->isInSection()
192 ? Symbol->getSection().getVariant() == MCSection::SV_COFF
193 : true) && "Got non COFF section in the COFF backend!");
Michael J. Spencere2da0a42010-07-19 06:13:10 +0000194 switch (Attribute) {
195 case MCSA_WeakReference:
Michael J. Spencer17990d52010-10-16 08:25:57 +0000196 case MCSA_Weak: {
197 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
198 SD.modifyFlags(COFF::SF_WeakExternal, COFF::SF_WeakExternal);
199 SD.setExternal(true);
200 }
Michael J. Spencere2da0a42010-07-19 06:13:10 +0000201 break;
202
203 case MCSA_Global:
204 getAssembler().getOrCreateSymbolData(*Symbol).setExternal(true);
205 break;
206
207 default:
Saleem Abdulrasool4208b612013-08-09 01:52:03 +0000208 return false;
Michael J. Spencere2da0a42010-07-19 06:13:10 +0000209 }
Saleem Abdulrasool4208b612013-08-09 01:52:03 +0000210
211 return true;
Chris Lattner56725be2010-07-11 22:05:00 +0000212}
213
214void WinCOFFStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
Michael J. Spencere2da0a42010-07-19 06:13:10 +0000215 llvm_unreachable("not implemented");
Chris Lattner56725be2010-07-11 22:05:00 +0000216}
217
218void WinCOFFStreamer::BeginCOFFSymbolDef(MCSymbol const *Symbol) {
Michael J. Spencerbe52c622010-10-09 11:00:37 +0000219 assert((Symbol->isInSection()
220 ? Symbol->getSection().getVariant() == MCSection::SV_COFF
221 : true) && "Got non COFF section in the COFF backend!");
Michael J. Spencere2da0a42010-07-19 06:13:10 +0000222 assert(CurSymbol == NULL && "EndCOFFSymbolDef must be called between calls "
223 "to BeginCOFFSymbolDef!");
224 CurSymbol = Symbol;
Chris Lattner56725be2010-07-11 22:05:00 +0000225}
226
227void WinCOFFStreamer::EmitCOFFSymbolStorageClass(int StorageClass) {
Michael J. Spencere2da0a42010-07-19 06:13:10 +0000228 assert(CurSymbol != NULL && "BeginCOFFSymbolDef must be called first!");
229 assert((StorageClass & ~0xFF) == 0 && "StorageClass must only have data in "
230 "the first byte!");
231
232 getAssembler().getOrCreateSymbolData(*CurSymbol).modifyFlags(
233 StorageClass << COFF::SF_ClassShift,
234 COFF::SF_ClassMask);
Chris Lattner56725be2010-07-11 22:05:00 +0000235}
236
237void WinCOFFStreamer::EmitCOFFSymbolType(int Type) {
Michael J. Spencere2da0a42010-07-19 06:13:10 +0000238 assert(CurSymbol != NULL && "BeginCOFFSymbolDef must be called first!");
239 assert((Type & ~0xFFFF) == 0 && "Type must only have data in the first 2 "
240 "bytes");
241
242 getAssembler().getOrCreateSymbolData(*CurSymbol).modifyFlags(
243 Type << COFF::SF_TypeShift,
244 COFF::SF_TypeMask);
Chris Lattner56725be2010-07-11 22:05:00 +0000245}
246
247void WinCOFFStreamer::EndCOFFSymbolDef() {
Michael J. Spencere2da0a42010-07-19 06:13:10 +0000248 assert(CurSymbol != NULL && "BeginCOFFSymbolDef must be called first!");
249 CurSymbol = NULL;
Chris Lattner56725be2010-07-11 22:05:00 +0000250}
251
Rafael Espindolad3df3d32011-12-17 01:14:52 +0000252void WinCOFFStreamer::EmitCOFFSecRel32(MCSymbol const *Symbol)
253{
254 MCDataFragment *DF = getOrCreateDataFragment();
255
Eli Benderskya31a8942012-12-07 19:13:57 +0000256 DF->getFixups().push_back(
257 MCFixup::Create(DF->getContents().size(),
258 MCSymbolRefExpr::Create (Symbol, getContext ()),
259 FK_SecRel_4));
Rafael Espindolad3df3d32011-12-17 01:14:52 +0000260 DF->getContents().resize(DF->getContents().size() + 4, 0);
261}
262
Chris Lattner56725be2010-07-11 22:05:00 +0000263void WinCOFFStreamer::EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) {
Michael J. Spencere2da0a42010-07-19 06:13:10 +0000264 llvm_unreachable("not implemented");
Chris Lattner56725be2010-07-11 22:05:00 +0000265}
266
267void WinCOFFStreamer::EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
Michael J. Spencere2da0a42010-07-19 06:13:10 +0000268 unsigned ByteAlignment) {
Michael J. Spencerbe52c622010-10-09 11:00:37 +0000269 assert((Symbol->isInSection()
270 ? Symbol->getSection().getVariant() == MCSection::SV_COFF
271 : true) && "Got non COFF section in the COFF backend!");
Michael J. Spencere2da0a42010-07-19 06:13:10 +0000272 AddCommonSymbol(Symbol, Size, ByteAlignment, true);
Chris Lattner56725be2010-07-11 22:05:00 +0000273}
274
Benjamin Kramer63970512011-09-01 23:04:27 +0000275void WinCOFFStreamer::EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size,
276 unsigned ByteAlignment) {
Michael J. Spencerbe52c622010-10-09 11:00:37 +0000277 assert((Symbol->isInSection()
278 ? Symbol->getSection().getVariant() == MCSection::SV_COFF
279 : true) && "Got non COFF section in the COFF backend!");
Benjamin Kramer63970512011-09-01 23:04:27 +0000280 AddCommonSymbol(Symbol, Size, ByteAlignment, false);
Chris Lattner56725be2010-07-11 22:05:00 +0000281}
282
283void WinCOFFStreamer::EmitZerofill(const MCSection *Section, MCSymbol *Symbol,
Evan Chengf5bd6c62012-06-22 20:14:46 +0000284 uint64_t Size,unsigned ByteAlignment) {
Michael J. Spencere2da0a42010-07-19 06:13:10 +0000285 llvm_unreachable("not implemented");
Chris Lattner56725be2010-07-11 22:05:00 +0000286}
287
288void WinCOFFStreamer::EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol,
289 uint64_t Size, unsigned ByteAlignment) {
Michael J. Spencere2da0a42010-07-19 06:13:10 +0000290 llvm_unreachable("not implemented");
Chris Lattner56725be2010-07-11 22:05:00 +0000291}
292
Chris Lattner56725be2010-07-11 22:05:00 +0000293void WinCOFFStreamer::EmitFileDirective(StringRef Filename) {
294 // Ignore for now, linkers don't care, and proper debug
295 // info will be a much large effort.
296}
297
Rafael Espindola5645bad2013-10-16 01:05:45 +0000298// TODO: Implement this if you want to emit .comment section in COFF obj files.
299void WinCOFFStreamer::EmitIdent(StringRef IdentString) {
300 llvm_unreachable("unsupported directive");
301}
302
Charles Davis1c8bd5a2011-05-22 03:01:05 +0000303void WinCOFFStreamer::EmitWin64EHHandlerData() {
304 MCStreamer::EmitWin64EHHandlerData();
305
306 // We have to emit the unwind info now, because this directive
307 // actually switches to the .xdata section!
308 MCWin64EHUnwindEmitter::EmitUnwindInfo(*this, getCurrentW64UnwindInfo());
309}
310
Rafael Espindola07082092012-01-07 03:13:18 +0000311void WinCOFFStreamer::FinishImpl() {
Charles Davisbc2daa02011-05-22 04:15:07 +0000312 EmitW64Tables();
Rafael Espindola07082092012-01-07 03:13:18 +0000313 MCObjectStreamer::FinishImpl();
Chris Lattner56725be2010-07-11 22:05:00 +0000314}
315
316namespace llvm
317{
318 MCStreamer *createWinCOFFStreamer(MCContext &Context,
Evan Cheng5928e692011-07-25 23:24:55 +0000319 MCAsmBackend &MAB,
Chris Lattner56725be2010-07-11 22:05:00 +0000320 MCCodeEmitter &CE,
Michael J. Spencer6b4925e2010-07-31 06:22:29 +0000321 raw_ostream &OS,
322 bool RelaxAll) {
Evan Cheng5928e692011-07-25 23:24:55 +0000323 WinCOFFStreamer *S = new WinCOFFStreamer(Context, MAB, CE, OS);
Michael J. Spencer6b4925e2010-07-31 06:22:29 +0000324 S->getAssembler().setRelaxAll(RelaxAll);
325 return S;
Chris Lattner56725be2010-07-11 22:05:00 +0000326 }
327}