blob: 445d26106ad1a733e1abb7764183808f8409cb5a [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"
Rafael Espindolae308c0c2014-01-23 22:49:25 +000023#include "llvm/MC/MCObjectFileInfo.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000024#include "llvm/MC/MCObjectStreamer.h"
25#include "llvm/MC/MCSection.h"
26#include "llvm/MC/MCSectionCOFF.h"
27#include "llvm/MC/MCSymbol.h"
28#include "llvm/MC/MCValue.h"
29#include "llvm/MC/MCWin64EH.h"
Chris Lattner56725be2010-07-11 22:05:00 +000030#include "llvm/Support/COFF.h"
31#include "llvm/Support/Debug.h"
32#include "llvm/Support/ErrorHandling.h"
Evan Cheng2bb40352011-08-24 18:08:43 +000033#include "llvm/Support/TargetRegistry.h"
Chris Lattner56725be2010-07-11 22:05:00 +000034#include "llvm/Support/raw_ostream.h"
Rafael Espindolad3df3d32011-12-17 01:14:52 +000035
Chris Lattner56725be2010-07-11 22:05:00 +000036using namespace llvm;
37
Chris Lattner56725be2010-07-11 22:05:00 +000038namespace {
39class WinCOFFStreamer : public MCObjectStreamer {
40public:
Michael J. Spencere2da0a42010-07-19 06:13:10 +000041 MCSymbol const *CurSymbol;
42
Chris Lattner56725be2010-07-11 22:05:00 +000043 WinCOFFStreamer(MCContext &Context,
Evan Cheng5928e692011-07-25 23:24:55 +000044 MCAsmBackend &MAB,
Chris Lattner56725be2010-07-11 22:05:00 +000045 MCCodeEmitter &CE,
46 raw_ostream &OS);
47
Michael J. Spencere2da0a42010-07-19 06:13:10 +000048 void AddCommonSymbol(MCSymbol *Symbol, uint64_t Size,
49 unsigned ByteAlignment, bool External);
50
Chris Lattner56725be2010-07-11 22:05:00 +000051 // MCStreamer interface
52
Rafael Espindola0e2ccb22014-01-24 03:54:40 +000053 virtual void InitSections(bool Force);
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:
David Woodhouse6f3c73f2014-01-28 23:12:49 +000081 virtual void EmitInstToData(const MCInst &Inst, const MCSubtargetInfo &STI) {
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);
David Woodhouse9784cef2014-01-28 23:13:07 +000087 getAssembler().getEmitter().EncodeInstruction(Inst, VecOS, Fixups, STI);
Michael J. Spencer088925e2013-01-03 01:09:22 +000088 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 }
Chris Lattner56725be2010-07-11 22:05:00 +000097};
98} // end anonymous namespace.
99
Chandler Carruthde093ef2013-01-31 23:29:57 +0000100WinCOFFStreamer::WinCOFFStreamer(MCContext &Context, MCAsmBackend &MAB,
101 MCCodeEmitter &CE, raw_ostream &OS)
Rafael Espindola24ea09e2014-01-26 06:06:37 +0000102 : MCObjectStreamer(Context, MAB, OS, &CE), CurSymbol(NULL) {}
Michael J. Spencere2da0a42010-07-19 06:13:10 +0000103
104void WinCOFFStreamer::AddCommonSymbol(MCSymbol *Symbol, uint64_t Size,
105 unsigned ByteAlignment, bool External) {
106 assert(!Symbol->isInSection() && "Symbol must not already have a section!");
107
Rafael Espindolaf2812532014-01-24 02:28:11 +0000108 const MCSection *Section = getContext().getObjectFileInfo()->getBSSSection();
Michael J. Spencere2da0a42010-07-19 06:13:10 +0000109 MCSectionData &SectionData = getAssembler().getOrCreateSectionData(*Section);
Michael J. Spencere2da0a42010-07-19 06:13:10 +0000110 if (SectionData.getAlignment() < ByteAlignment)
111 SectionData.setAlignment(ByteAlignment);
112
Rafael Espindolaedcf1ff2013-11-25 16:06:04 +0000113 MCSymbolData &SymbolData = getAssembler().getOrCreateSymbolData(*Symbol);
Michael J. Spencere2da0a42010-07-19 06:13:10 +0000114 SymbolData.setExternal(External);
115
Richard Mitton21101b32013-09-19 23:21:01 +0000116 AssignSection(Symbol, Section);
Michael J. Spencere2da0a42010-07-19 06:13:10 +0000117
118 if (ByteAlignment != 1)
119 new MCAlignFragment(ByteAlignment, 0, 0, ByteAlignment, &SectionData);
120
121 SymbolData.setFragment(new MCFillFragment(0, 0, Size, &SectionData));
Chris Lattner56725be2010-07-11 22:05:00 +0000122}
123
124// MCStreamer interface
125
Rafael Espindola0e2ccb22014-01-24 03:54:40 +0000126void WinCOFFStreamer::InitSections(bool Force) {
Rafael Espindolaf2812532014-01-24 02:28:11 +0000127 // FIXME: this is identical to the ELF one.
128 // This emulates the same behavior of GNU as. This makes it easier
129 // to compare the output as the major sections are in the same order.
130 SwitchSection(getContext().getObjectFileInfo()->getTextSection());
Rafael Espindola7b514962014-02-04 18:34:04 +0000131 EmitCodeAlignment(4);
Rafael Espindolaf2812532014-01-24 02:28:11 +0000132
133 SwitchSection(getContext().getObjectFileInfo()->getDataSection());
Rafael Espindola7b514962014-02-04 18:34:04 +0000134 EmitCodeAlignment(4);
Rafael Espindolaf2812532014-01-24 02:28:11 +0000135
136 SwitchSection(getContext().getObjectFileInfo()->getBSSSection());
Rafael Espindola7b514962014-02-04 18:34:04 +0000137 EmitCodeAlignment(4);
Rafael Espindolaf2812532014-01-24 02:28:11 +0000138
139 SwitchSection(getContext().getObjectFileInfo()->getTextSection());
Rafael Espindolaf667d922010-09-15 21:48:40 +0000140}
141
Rafael Espindola16795802010-11-28 16:22:59 +0000142void WinCOFFStreamer::EmitLabel(MCSymbol *Symbol) {
Rafael Espindola16795802010-11-28 16:22:59 +0000143 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
Rafael Espindolae5b74152010-11-28 17:18:55 +0000144 MCObjectStreamer::EmitLabel(Symbol);
Rafael Espindola16795802010-11-28 16:22:59 +0000145}
146
Reed Kotleraee4d5d12012-12-16 04:00:45 +0000147void WinCOFFStreamer::EmitDebugLabel(MCSymbol *Symbol) {
148 EmitLabel(Symbol);
149}
Chris Lattner56725be2010-07-11 22:05:00 +0000150void WinCOFFStreamer::EmitAssemblerFlag(MCAssemblerFlag Flag) {
Michael J. Spencere2da0a42010-07-19 06:13:10 +0000151 llvm_unreachable("not implemented");
Chris Lattner56725be2010-07-11 22:05:00 +0000152}
153
Jim Grosbach5a2c68d2010-11-05 22:08:08 +0000154void WinCOFFStreamer::EmitThumbFunc(MCSymbol *Func) {
155 llvm_unreachable("not implemented");
156}
157
Saleem Abdulrasool4208b612013-08-09 01:52:03 +0000158bool WinCOFFStreamer::EmitSymbolAttribute(MCSymbol *Symbol,
Chris Lattner56725be2010-07-11 22:05:00 +0000159 MCSymbolAttr Attribute) {
Michael J. Spencerbe52c622010-10-09 11:00:37 +0000160 assert(Symbol && "Symbol must be non-null!");
161 assert((Symbol->isInSection()
162 ? Symbol->getSection().getVariant() == MCSection::SV_COFF
Alp Tokerf907b892013-12-05 05:44:44 +0000163 : true) && "Got non-COFF section in the COFF backend!");
Michael J. Spencere2da0a42010-07-19 06:13:10 +0000164 switch (Attribute) {
165 case MCSA_WeakReference:
Michael J. Spencer17990d52010-10-16 08:25:57 +0000166 case MCSA_Weak: {
167 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
168 SD.modifyFlags(COFF::SF_WeakExternal, COFF::SF_WeakExternal);
169 SD.setExternal(true);
170 }
Michael J. Spencere2da0a42010-07-19 06:13:10 +0000171 break;
172
173 case MCSA_Global:
174 getAssembler().getOrCreateSymbolData(*Symbol).setExternal(true);
175 break;
176
177 default:
Saleem Abdulrasool4208b612013-08-09 01:52:03 +0000178 return false;
Michael J. Spencere2da0a42010-07-19 06:13:10 +0000179 }
Saleem Abdulrasool4208b612013-08-09 01:52:03 +0000180
181 return true;
Chris Lattner56725be2010-07-11 22:05:00 +0000182}
183
184void WinCOFFStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
Michael J. Spencere2da0a42010-07-19 06:13:10 +0000185 llvm_unreachable("not implemented");
Chris Lattner56725be2010-07-11 22:05:00 +0000186}
187
188void WinCOFFStreamer::BeginCOFFSymbolDef(MCSymbol const *Symbol) {
Michael J. Spencerbe52c622010-10-09 11:00:37 +0000189 assert((Symbol->isInSection()
190 ? Symbol->getSection().getVariant() == MCSection::SV_COFF
Alp Tokerf907b892013-12-05 05:44:44 +0000191 : true) && "Got non-COFF section in the COFF backend!");
Michael J. Spencere2da0a42010-07-19 06:13:10 +0000192 assert(CurSymbol == NULL && "EndCOFFSymbolDef must be called between calls "
193 "to BeginCOFFSymbolDef!");
194 CurSymbol = Symbol;
Chris Lattner56725be2010-07-11 22:05:00 +0000195}
196
197void WinCOFFStreamer::EmitCOFFSymbolStorageClass(int StorageClass) {
Michael J. Spencere2da0a42010-07-19 06:13:10 +0000198 assert(CurSymbol != NULL && "BeginCOFFSymbolDef must be called first!");
199 assert((StorageClass & ~0xFF) == 0 && "StorageClass must only have data in "
200 "the first byte!");
201
202 getAssembler().getOrCreateSymbolData(*CurSymbol).modifyFlags(
203 StorageClass << COFF::SF_ClassShift,
204 COFF::SF_ClassMask);
Chris Lattner56725be2010-07-11 22:05:00 +0000205}
206
207void WinCOFFStreamer::EmitCOFFSymbolType(int Type) {
Michael J. Spencere2da0a42010-07-19 06:13:10 +0000208 assert(CurSymbol != NULL && "BeginCOFFSymbolDef must be called first!");
209 assert((Type & ~0xFFFF) == 0 && "Type must only have data in the first 2 "
210 "bytes");
211
212 getAssembler().getOrCreateSymbolData(*CurSymbol).modifyFlags(
213 Type << COFF::SF_TypeShift,
214 COFF::SF_TypeMask);
Chris Lattner56725be2010-07-11 22:05:00 +0000215}
216
217void WinCOFFStreamer::EndCOFFSymbolDef() {
Michael J. Spencere2da0a42010-07-19 06:13:10 +0000218 assert(CurSymbol != NULL && "BeginCOFFSymbolDef must be called first!");
219 CurSymbol = NULL;
Chris Lattner56725be2010-07-11 22:05:00 +0000220}
221
Timur Iskhodzhanov09069e02013-12-20 20:16:51 +0000222void WinCOFFStreamer::EmitCOFFSectionIndex(MCSymbol const *Symbol) {
Timur Iskhodzhanovc1fb2d62013-12-20 18:15:00 +0000223 MCDataFragment *DF = getOrCreateDataFragment();
Timur Iskhodzhanov09069e02013-12-20 20:16:51 +0000224 DF->getFixups().push_back(MCFixup::Create(
225 DF->getContents().size(), MCSymbolRefExpr::Create(Symbol, getContext()),
226 FK_SecRel_2));
Timur Iskhodzhanovc1fb2d62013-12-20 18:15:00 +0000227 DF->getContents().resize(DF->getContents().size() + 4, 0);
228}
229
Timur Iskhodzhanov09069e02013-12-20 20:16:51 +0000230void WinCOFFStreamer::EmitCOFFSecRel32(MCSymbol const *Symbol) {
Rafael Espindolad3df3d32011-12-17 01:14:52 +0000231 MCDataFragment *DF = getOrCreateDataFragment();
Timur Iskhodzhanov09069e02013-12-20 20:16:51 +0000232 DF->getFixups().push_back(MCFixup::Create(
233 DF->getContents().size(), MCSymbolRefExpr::Create(Symbol, getContext()),
234 FK_SecRel_4));
Rafael Espindolad3df3d32011-12-17 01:14:52 +0000235 DF->getContents().resize(DF->getContents().size() + 4, 0);
236}
237
Chris Lattner56725be2010-07-11 22:05:00 +0000238void WinCOFFStreamer::EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) {
Michael J. Spencere2da0a42010-07-19 06:13:10 +0000239 llvm_unreachable("not implemented");
Chris Lattner56725be2010-07-11 22:05:00 +0000240}
241
242void WinCOFFStreamer::EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
Michael J. Spencere2da0a42010-07-19 06:13:10 +0000243 unsigned ByteAlignment) {
Michael J. Spencerbe52c622010-10-09 11:00:37 +0000244 assert((Symbol->isInSection()
245 ? Symbol->getSection().getVariant() == MCSection::SV_COFF
Alp Tokerf907b892013-12-05 05:44:44 +0000246 : true) && "Got non-COFF section in the COFF backend!");
Michael J. Spencere2da0a42010-07-19 06:13:10 +0000247 AddCommonSymbol(Symbol, Size, ByteAlignment, true);
Chris Lattner56725be2010-07-11 22:05:00 +0000248}
249
Benjamin Kramer63970512011-09-01 23:04:27 +0000250void WinCOFFStreamer::EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size,
251 unsigned ByteAlignment) {
Michael J. Spencerbe52c622010-10-09 11:00:37 +0000252 assert((Symbol->isInSection()
253 ? Symbol->getSection().getVariant() == MCSection::SV_COFF
Alp Tokerf907b892013-12-05 05:44:44 +0000254 : true) && "Got non-COFF section in the COFF backend!");
Benjamin Kramer63970512011-09-01 23:04:27 +0000255 AddCommonSymbol(Symbol, Size, ByteAlignment, false);
Chris Lattner56725be2010-07-11 22:05:00 +0000256}
257
258void WinCOFFStreamer::EmitZerofill(const MCSection *Section, MCSymbol *Symbol,
Evan Chengf5bd6c62012-06-22 20:14:46 +0000259 uint64_t Size,unsigned ByteAlignment) {
Michael J. Spencere2da0a42010-07-19 06:13:10 +0000260 llvm_unreachable("not implemented");
Chris Lattner56725be2010-07-11 22:05:00 +0000261}
262
263void WinCOFFStreamer::EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol,
264 uint64_t Size, unsigned ByteAlignment) {
Michael J. Spencere2da0a42010-07-19 06:13:10 +0000265 llvm_unreachable("not implemented");
Chris Lattner56725be2010-07-11 22:05:00 +0000266}
267
Chris Lattner56725be2010-07-11 22:05:00 +0000268void WinCOFFStreamer::EmitFileDirective(StringRef Filename) {
269 // Ignore for now, linkers don't care, and proper debug
270 // info will be a much large effort.
271}
272
Rafael Espindola5645bad2013-10-16 01:05:45 +0000273// TODO: Implement this if you want to emit .comment section in COFF obj files.
274void WinCOFFStreamer::EmitIdent(StringRef IdentString) {
275 llvm_unreachable("unsupported directive");
276}
277
Charles Davis1c8bd5a2011-05-22 03:01:05 +0000278void WinCOFFStreamer::EmitWin64EHHandlerData() {
279 MCStreamer::EmitWin64EHHandlerData();
280
281 // We have to emit the unwind info now, because this directive
282 // actually switches to the .xdata section!
283 MCWin64EHUnwindEmitter::EmitUnwindInfo(*this, getCurrentW64UnwindInfo());
284}
285
Rafael Espindola07082092012-01-07 03:13:18 +0000286void WinCOFFStreamer::FinishImpl() {
Rafael Espindolaaf7131d2013-12-02 14:59:34 +0000287 EmitFrames(NULL, true);
Charles Davisbc2daa02011-05-22 04:15:07 +0000288 EmitW64Tables();
Rafael Espindola07082092012-01-07 03:13:18 +0000289 MCObjectStreamer::FinishImpl();
Chris Lattner56725be2010-07-11 22:05:00 +0000290}
291
292namespace llvm
293{
294 MCStreamer *createWinCOFFStreamer(MCContext &Context,
Evan Cheng5928e692011-07-25 23:24:55 +0000295 MCAsmBackend &MAB,
Chris Lattner56725be2010-07-11 22:05:00 +0000296 MCCodeEmitter &CE,
Michael J. Spencer6b4925e2010-07-31 06:22:29 +0000297 raw_ostream &OS,
298 bool RelaxAll) {
Evan Cheng5928e692011-07-25 23:24:55 +0000299 WinCOFFStreamer *S = new WinCOFFStreamer(Context, MAB, CE, OS);
Michael J. Spencer6b4925e2010-07-31 06:22:29 +0000300 S->getAssembler().setRelaxAll(RelaxAll);
301 return S;
Chris Lattner56725be2010-07-11 22:05:00 +0000302 }
303}