blob: cb4e9b1c37b99c14618fe9d2fb158e797e8e4919 [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();
Timur Iskhodzhanovc1fb2d62013-12-20 18:15:00 +000064 virtual void EmitCOFFSectionIndex(MCSymbol const *Symbol);
Rafael Espindolad3df3d32011-12-17 01:14:52 +000065 virtual void EmitCOFFSecRel32(MCSymbol const *Symbol);
Chris Lattner56725be2010-07-11 22:05:00 +000066 virtual void EmitELFSize(MCSymbol *Symbol, const MCExpr *Value);
67 virtual void EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
Michael J. Spencere2da0a42010-07-19 06:13:10 +000068 unsigned ByteAlignment);
Benjamin Kramer63970512011-09-01 23:04:27 +000069 virtual void EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size,
70 unsigned ByteAlignment);
Chris Lattner56725be2010-07-11 22:05:00 +000071 virtual void EmitZerofill(const MCSection *Section, MCSymbol *Symbol,
Evan Chengf5bd6c62012-06-22 20:14:46 +000072 uint64_t Size,unsigned ByteAlignment);
Chris Lattner56725be2010-07-11 22:05:00 +000073 virtual void EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol,
Michael J. Spencere2da0a42010-07-19 06:13:10 +000074 uint64_t Size, unsigned ByteAlignment);
Chris Lattner56725be2010-07-11 22:05:00 +000075 virtual void EmitFileDirective(StringRef Filename);
Rafael Espindola5645bad2013-10-16 01:05:45 +000076 virtual void EmitIdent(StringRef IdentString);
Charles Davis1c8bd5a2011-05-22 03:01:05 +000077 virtual void EmitWin64EHHandlerData();
Rafael Espindola07082092012-01-07 03:13:18 +000078 virtual void FinishImpl();
Michael J. Spencer86bbd712010-10-09 15:44:27 +000079
80private:
Rafael Espindola72b54882010-11-01 16:27:31 +000081 virtual void EmitInstToData(const MCInst &Inst) {
Michael J. Spencer088925e2013-01-03 01:09:22 +000082 MCDataFragment *DF = getOrCreateDataFragment();
83
84 SmallVector<MCFixup, 4> Fixups;
85 SmallString<256> Code;
86 raw_svector_ostream VecOS(Code);
87 getAssembler().getEmitter().EncodeInstruction(Inst, VecOS, Fixups);
88 VecOS.flush();
89
90 // Add the fixups and data.
91 for (unsigned i = 0, e = Fixups.size(); i != e; ++i) {
92 Fixups[i].setOffset(Fixups[i].getOffset() + DF->getContents().size());
93 DF->getFixups().push_back(Fixups[i]);
94 }
95 DF->getContents().append(Code.begin(), Code.end());
Rafael Espindola72b54882010-11-01 16:27:31 +000096 }
97
Rafael Espindola3294e052013-11-25 16:00:32 +000098 const MCSectionCOFF *getSectionText() {
99 return getContext().getCOFFSection(
100 ".text", COFF::IMAGE_SCN_CNT_CODE | COFF::IMAGE_SCN_MEM_EXECUTE |
101 COFF::IMAGE_SCN_MEM_READ,
102 SectionKind::getText());
103 }
104
105 const MCSectionCOFF *getSectionData() {
106 return getContext().getCOFFSection(
107 ".data", COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
108 COFF::IMAGE_SCN_MEM_READ | COFF::IMAGE_SCN_MEM_WRITE,
109 SectionKind::getDataRel());
110 }
111
112 const MCSectionCOFF *getSectionBSS() {
113 return getContext().getCOFFSection(
114 ".bss", COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA |
115 COFF::IMAGE_SCN_MEM_READ | COFF::IMAGE_SCN_MEM_WRITE,
116 SectionKind::getBSS());
Michael J. Spencer86bbd712010-10-09 15:44:27 +0000117 }
118
119 void SetSectionText() {
Rafael Espindola3294e052013-11-25 16:00:32 +0000120 SwitchSection(getSectionText());
Michael J. Spencer86bbd712010-10-09 15:44:27 +0000121 EmitCodeAlignment(4, 0);
122 }
123
124 void SetSectionData() {
Rafael Espindola3294e052013-11-25 16:00:32 +0000125 SwitchSection(getSectionData());
Michael J. Spencer86bbd712010-10-09 15:44:27 +0000126 EmitCodeAlignment(4, 0);
127 }
128
129 void SetSectionBSS() {
Rafael Espindola3294e052013-11-25 16:00:32 +0000130 SwitchSection(getSectionBSS());
Michael J. Spencer86bbd712010-10-09 15:44:27 +0000131 EmitCodeAlignment(4, 0);
132 }
Chris Lattner56725be2010-07-11 22:05:00 +0000133};
134} // end anonymous namespace.
135
Chandler Carruthde093ef2013-01-31 23:29:57 +0000136WinCOFFStreamer::WinCOFFStreamer(MCContext &Context, MCAsmBackend &MAB,
137 MCCodeEmitter &CE, raw_ostream &OS)
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000138 : MCObjectStreamer(Context, 0, MAB, OS, &CE), CurSymbol(NULL) {}
Michael J. Spencere2da0a42010-07-19 06:13:10 +0000139
140void WinCOFFStreamer::AddCommonSymbol(MCSymbol *Symbol, uint64_t Size,
141 unsigned ByteAlignment, bool External) {
142 assert(!Symbol->isInSection() && "Symbol must not already have a section!");
143
Rafael Espindolaedcf1ff2013-11-25 16:06:04 +0000144 const MCSectionCOFF *Section = getSectionBSS();
Michael J. Spencere2da0a42010-07-19 06:13:10 +0000145 MCSectionData &SectionData = getAssembler().getOrCreateSectionData(*Section);
Michael J. Spencere2da0a42010-07-19 06:13:10 +0000146 if (SectionData.getAlignment() < ByteAlignment)
147 SectionData.setAlignment(ByteAlignment);
148
Rafael Espindolaedcf1ff2013-11-25 16:06:04 +0000149 MCSymbolData &SymbolData = getAssembler().getOrCreateSymbolData(*Symbol);
Michael J. Spencere2da0a42010-07-19 06:13:10 +0000150 SymbolData.setExternal(External);
151
Richard Mitton21101b32013-09-19 23:21:01 +0000152 AssignSection(Symbol, Section);
Michael J. Spencere2da0a42010-07-19 06:13:10 +0000153
154 if (ByteAlignment != 1)
155 new MCAlignFragment(ByteAlignment, 0, 0, ByteAlignment, &SectionData);
156
157 SymbolData.setFragment(new MCFillFragment(0, 0, Size, &SectionData));
Chris Lattner56725be2010-07-11 22:05:00 +0000158}
159
160// MCStreamer interface
161
Eli Benderskycbb25142013-01-14 19:04:57 +0000162void WinCOFFStreamer::InitToTextSection() {
163 SetSectionText();
164}
165
Rafael Espindolaf667d922010-09-15 21:48:40 +0000166void WinCOFFStreamer::InitSections() {
Michael J. Spencer86bbd712010-10-09 15:44:27 +0000167 SetSectionText();
168 SetSectionData();
169 SetSectionBSS();
170 SetSectionText();
Rafael Espindolaf667d922010-09-15 21:48:40 +0000171}
172
Rafael Espindola16795802010-11-28 16:22:59 +0000173void WinCOFFStreamer::EmitLabel(MCSymbol *Symbol) {
Rafael Espindola16795802010-11-28 16:22:59 +0000174 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
Rafael Espindolae5b74152010-11-28 17:18:55 +0000175 MCObjectStreamer::EmitLabel(Symbol);
Rafael Espindola16795802010-11-28 16:22:59 +0000176}
177
Reed Kotleraee4d5d12012-12-16 04:00:45 +0000178void WinCOFFStreamer::EmitDebugLabel(MCSymbol *Symbol) {
179 EmitLabel(Symbol);
180}
Chris Lattner56725be2010-07-11 22:05:00 +0000181void WinCOFFStreamer::EmitAssemblerFlag(MCAssemblerFlag Flag) {
Michael J. Spencere2da0a42010-07-19 06:13:10 +0000182 llvm_unreachable("not implemented");
Chris Lattner56725be2010-07-11 22:05:00 +0000183}
184
Jim Grosbach5a2c68d2010-11-05 22:08:08 +0000185void WinCOFFStreamer::EmitThumbFunc(MCSymbol *Func) {
186 llvm_unreachable("not implemented");
187}
188
Saleem Abdulrasool4208b612013-08-09 01:52:03 +0000189bool WinCOFFStreamer::EmitSymbolAttribute(MCSymbol *Symbol,
Chris Lattner56725be2010-07-11 22:05:00 +0000190 MCSymbolAttr Attribute) {
Michael J. Spencerbe52c622010-10-09 11:00:37 +0000191 assert(Symbol && "Symbol must be non-null!");
192 assert((Symbol->isInSection()
193 ? Symbol->getSection().getVariant() == MCSection::SV_COFF
Alp Tokerf907b892013-12-05 05:44:44 +0000194 : true) && "Got non-COFF section in the COFF backend!");
Michael J. Spencere2da0a42010-07-19 06:13:10 +0000195 switch (Attribute) {
196 case MCSA_WeakReference:
Michael J. Spencer17990d52010-10-16 08:25:57 +0000197 case MCSA_Weak: {
198 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
199 SD.modifyFlags(COFF::SF_WeakExternal, COFF::SF_WeakExternal);
200 SD.setExternal(true);
201 }
Michael J. Spencere2da0a42010-07-19 06:13:10 +0000202 break;
203
204 case MCSA_Global:
205 getAssembler().getOrCreateSymbolData(*Symbol).setExternal(true);
206 break;
207
208 default:
Saleem Abdulrasool4208b612013-08-09 01:52:03 +0000209 return false;
Michael J. Spencere2da0a42010-07-19 06:13:10 +0000210 }
Saleem Abdulrasool4208b612013-08-09 01:52:03 +0000211
212 return true;
Chris Lattner56725be2010-07-11 22:05:00 +0000213}
214
215void WinCOFFStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
Michael J. Spencere2da0a42010-07-19 06:13:10 +0000216 llvm_unreachable("not implemented");
Chris Lattner56725be2010-07-11 22:05:00 +0000217}
218
219void WinCOFFStreamer::BeginCOFFSymbolDef(MCSymbol const *Symbol) {
Michael J. Spencerbe52c622010-10-09 11:00:37 +0000220 assert((Symbol->isInSection()
221 ? Symbol->getSection().getVariant() == MCSection::SV_COFF
Alp Tokerf907b892013-12-05 05:44:44 +0000222 : true) && "Got non-COFF section in the COFF backend!");
Michael J. Spencere2da0a42010-07-19 06:13:10 +0000223 assert(CurSymbol == NULL && "EndCOFFSymbolDef must be called between calls "
224 "to BeginCOFFSymbolDef!");
225 CurSymbol = Symbol;
Chris Lattner56725be2010-07-11 22:05:00 +0000226}
227
228void WinCOFFStreamer::EmitCOFFSymbolStorageClass(int StorageClass) {
Michael J. Spencere2da0a42010-07-19 06:13:10 +0000229 assert(CurSymbol != NULL && "BeginCOFFSymbolDef must be called first!");
230 assert((StorageClass & ~0xFF) == 0 && "StorageClass must only have data in "
231 "the first byte!");
232
233 getAssembler().getOrCreateSymbolData(*CurSymbol).modifyFlags(
234 StorageClass << COFF::SF_ClassShift,
235 COFF::SF_ClassMask);
Chris Lattner56725be2010-07-11 22:05:00 +0000236}
237
238void WinCOFFStreamer::EmitCOFFSymbolType(int Type) {
Michael J. Spencere2da0a42010-07-19 06:13:10 +0000239 assert(CurSymbol != NULL && "BeginCOFFSymbolDef must be called first!");
240 assert((Type & ~0xFFFF) == 0 && "Type must only have data in the first 2 "
241 "bytes");
242
243 getAssembler().getOrCreateSymbolData(*CurSymbol).modifyFlags(
244 Type << COFF::SF_TypeShift,
245 COFF::SF_TypeMask);
Chris Lattner56725be2010-07-11 22:05:00 +0000246}
247
248void WinCOFFStreamer::EndCOFFSymbolDef() {
Michael J. Spencere2da0a42010-07-19 06:13:10 +0000249 assert(CurSymbol != NULL && "BeginCOFFSymbolDef must be called first!");
250 CurSymbol = NULL;
Chris Lattner56725be2010-07-11 22:05:00 +0000251}
252
Timur Iskhodzhanov09069e02013-12-20 20:16:51 +0000253void WinCOFFStreamer::EmitCOFFSectionIndex(MCSymbol const *Symbol) {
Timur Iskhodzhanovc1fb2d62013-12-20 18:15:00 +0000254 MCDataFragment *DF = getOrCreateDataFragment();
Timur Iskhodzhanov09069e02013-12-20 20:16:51 +0000255 DF->getFixups().push_back(MCFixup::Create(
256 DF->getContents().size(), MCSymbolRefExpr::Create(Symbol, getContext()),
257 FK_SecRel_2));
Timur Iskhodzhanovc1fb2d62013-12-20 18:15:00 +0000258 DF->getContents().resize(DF->getContents().size() + 4, 0);
259}
260
Timur Iskhodzhanov09069e02013-12-20 20:16:51 +0000261void WinCOFFStreamer::EmitCOFFSecRel32(MCSymbol const *Symbol) {
Rafael Espindolad3df3d32011-12-17 01:14:52 +0000262 MCDataFragment *DF = getOrCreateDataFragment();
Timur Iskhodzhanov09069e02013-12-20 20:16:51 +0000263 DF->getFixups().push_back(MCFixup::Create(
264 DF->getContents().size(), MCSymbolRefExpr::Create(Symbol, getContext()),
265 FK_SecRel_4));
Rafael Espindolad3df3d32011-12-17 01:14:52 +0000266 DF->getContents().resize(DF->getContents().size() + 4, 0);
267}
268
Chris Lattner56725be2010-07-11 22:05:00 +0000269void WinCOFFStreamer::EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) {
Michael J. Spencere2da0a42010-07-19 06:13:10 +0000270 llvm_unreachable("not implemented");
Chris Lattner56725be2010-07-11 22:05:00 +0000271}
272
273void WinCOFFStreamer::EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
Michael J. Spencere2da0a42010-07-19 06:13:10 +0000274 unsigned ByteAlignment) {
Michael J. Spencerbe52c622010-10-09 11:00:37 +0000275 assert((Symbol->isInSection()
276 ? Symbol->getSection().getVariant() == MCSection::SV_COFF
Alp Tokerf907b892013-12-05 05:44:44 +0000277 : true) && "Got non-COFF section in the COFF backend!");
Michael J. Spencere2da0a42010-07-19 06:13:10 +0000278 AddCommonSymbol(Symbol, Size, ByteAlignment, true);
Chris Lattner56725be2010-07-11 22:05:00 +0000279}
280
Benjamin Kramer63970512011-09-01 23:04:27 +0000281void WinCOFFStreamer::EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size,
282 unsigned ByteAlignment) {
Michael J. Spencerbe52c622010-10-09 11:00:37 +0000283 assert((Symbol->isInSection()
284 ? Symbol->getSection().getVariant() == MCSection::SV_COFF
Alp Tokerf907b892013-12-05 05:44:44 +0000285 : true) && "Got non-COFF section in the COFF backend!");
Benjamin Kramer63970512011-09-01 23:04:27 +0000286 AddCommonSymbol(Symbol, Size, ByteAlignment, false);
Chris Lattner56725be2010-07-11 22:05:00 +0000287}
288
289void WinCOFFStreamer::EmitZerofill(const MCSection *Section, MCSymbol *Symbol,
Evan Chengf5bd6c62012-06-22 20:14:46 +0000290 uint64_t Size,unsigned ByteAlignment) {
Michael J. Spencere2da0a42010-07-19 06:13:10 +0000291 llvm_unreachable("not implemented");
Chris Lattner56725be2010-07-11 22:05:00 +0000292}
293
294void WinCOFFStreamer::EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol,
295 uint64_t Size, unsigned ByteAlignment) {
Michael J. Spencere2da0a42010-07-19 06:13:10 +0000296 llvm_unreachable("not implemented");
Chris Lattner56725be2010-07-11 22:05:00 +0000297}
298
Chris Lattner56725be2010-07-11 22:05:00 +0000299void WinCOFFStreamer::EmitFileDirective(StringRef Filename) {
300 // Ignore for now, linkers don't care, and proper debug
301 // info will be a much large effort.
302}
303
Rafael Espindola5645bad2013-10-16 01:05:45 +0000304// TODO: Implement this if you want to emit .comment section in COFF obj files.
305void WinCOFFStreamer::EmitIdent(StringRef IdentString) {
306 llvm_unreachable("unsupported directive");
307}
308
Charles Davis1c8bd5a2011-05-22 03:01:05 +0000309void WinCOFFStreamer::EmitWin64EHHandlerData() {
310 MCStreamer::EmitWin64EHHandlerData();
311
312 // We have to emit the unwind info now, because this directive
313 // actually switches to the .xdata section!
314 MCWin64EHUnwindEmitter::EmitUnwindInfo(*this, getCurrentW64UnwindInfo());
315}
316
Rafael Espindola07082092012-01-07 03:13:18 +0000317void WinCOFFStreamer::FinishImpl() {
Rafael Espindolaaf7131d2013-12-02 14:59:34 +0000318 EmitFrames(NULL, true);
Charles Davisbc2daa02011-05-22 04:15:07 +0000319 EmitW64Tables();
Rafael Espindola07082092012-01-07 03:13:18 +0000320 MCObjectStreamer::FinishImpl();
Chris Lattner56725be2010-07-11 22:05:00 +0000321}
322
323namespace llvm
324{
325 MCStreamer *createWinCOFFStreamer(MCContext &Context,
Evan Cheng5928e692011-07-25 23:24:55 +0000326 MCAsmBackend &MAB,
Chris Lattner56725be2010-07-11 22:05:00 +0000327 MCCodeEmitter &CE,
Michael J. Spencer6b4925e2010-07-31 06:22:29 +0000328 raw_ostream &OS,
329 bool RelaxAll) {
Evan Cheng5928e692011-07-25 23:24:55 +0000330 WinCOFFStreamer *S = new WinCOFFStreamer(Context, MAB, CE, OS);
Michael J. Spencer6b4925e2010-07-31 06:22:29 +0000331 S->getAssembler().setRelaxAll(RelaxAll);
332 return S;
Chris Lattner56725be2010-07-11 22:05:00 +0000333 }
334}