blob: b7d55741456a20e73394b47bb58e5c374fe33c71 [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 Espindolaf667d922010-09-15 21:48:40 +000053 virtual void InitSections();
Eli Benderskycbb25142013-01-14 19:04:57 +000054 virtual void InitToTextSection();
Rafael Espindola16795802010-11-28 16:22:59 +000055 virtual void EmitLabel(MCSymbol *Symbol);
Reed Kotleraee4d5d12012-12-16 04:00:45 +000056 virtual void EmitDebugLabel(MCSymbol *Symbol);
Chris Lattner56725be2010-07-11 22:05:00 +000057 virtual void EmitAssemblerFlag(MCAssemblerFlag Flag);
Jim Grosbach5a2c68d2010-11-05 22:08:08 +000058 virtual void EmitThumbFunc(MCSymbol *Func);
Saleem Abdulrasool4208b612013-08-09 01:52:03 +000059 virtual bool EmitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute);
Chris Lattner56725be2010-07-11 22:05:00 +000060 virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue);
61 virtual void BeginCOFFSymbolDef(MCSymbol const *Symbol);
62 virtual void EmitCOFFSymbolStorageClass(int StorageClass);
63 virtual void EmitCOFFSymbolType(int Type);
64 virtual void EndCOFFSymbolDef();
Timur Iskhodzhanovc1fb2d62013-12-20 18:15:00 +000065 virtual void EmitCOFFSectionIndex(MCSymbol const *Symbol);
Rafael Espindolad3df3d32011-12-17 01:14:52 +000066 virtual void EmitCOFFSecRel32(MCSymbol const *Symbol);
Chris Lattner56725be2010-07-11 22:05:00 +000067 virtual void EmitELFSize(MCSymbol *Symbol, const MCExpr *Value);
68 virtual void EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
Michael J. Spencere2da0a42010-07-19 06:13:10 +000069 unsigned ByteAlignment);
Benjamin Kramer63970512011-09-01 23:04:27 +000070 virtual void EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size,
71 unsigned ByteAlignment);
Chris Lattner56725be2010-07-11 22:05:00 +000072 virtual void EmitZerofill(const MCSection *Section, MCSymbol *Symbol,
Evan Chengf5bd6c62012-06-22 20:14:46 +000073 uint64_t Size,unsigned ByteAlignment);
Chris Lattner56725be2010-07-11 22:05:00 +000074 virtual void EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol,
Michael J. Spencere2da0a42010-07-19 06:13:10 +000075 uint64_t Size, unsigned ByteAlignment);
Chris Lattner56725be2010-07-11 22:05:00 +000076 virtual void EmitFileDirective(StringRef Filename);
Rafael Espindola5645bad2013-10-16 01:05:45 +000077 virtual void EmitIdent(StringRef IdentString);
Charles Davis1c8bd5a2011-05-22 03:01:05 +000078 virtual void EmitWin64EHHandlerData();
Rafael Espindola07082092012-01-07 03:13:18 +000079 virtual void FinishImpl();
Michael J. Spencer86bbd712010-10-09 15:44:27 +000080
81private:
Rafael Espindola72b54882010-11-01 16:27:31 +000082 virtual void EmitInstToData(const MCInst &Inst) {
Michael J. Spencer088925e2013-01-03 01:09:22 +000083 MCDataFragment *DF = getOrCreateDataFragment();
84
85 SmallVector<MCFixup, 4> Fixups;
86 SmallString<256> Code;
87 raw_svector_ostream VecOS(Code);
88 getAssembler().getEmitter().EncodeInstruction(Inst, VecOS, Fixups);
89 VecOS.flush();
90
91 // Add the fixups and data.
92 for (unsigned i = 0, e = Fixups.size(); i != e; ++i) {
93 Fixups[i].setOffset(Fixups[i].getOffset() + DF->getContents().size());
94 DF->getFixups().push_back(Fixups[i]);
95 }
96 DF->getContents().append(Code.begin(), Code.end());
Rafael Espindola72b54882010-11-01 16:27:31 +000097 }
98
Rafael Espindolae308c0c2014-01-23 22:49:25 +000099 const MCSection *getSectionText() {
100 return getContext().getObjectFileInfo()->getTextSection();
Rafael Espindola3294e052013-11-25 16:00:32 +0000101 }
102
Rafael Espindolae308c0c2014-01-23 22:49:25 +0000103 const MCSection *getSectionData() {
104 return getContext().getObjectFileInfo()->getDataSection();
Rafael Espindola3294e052013-11-25 16:00:32 +0000105 }
106
Rafael Espindolae308c0c2014-01-23 22:49:25 +0000107 const MCSection *getSectionBSS() {
108 return getContext().getObjectFileInfo()->getBSSSection();
Michael J. Spencer86bbd712010-10-09 15:44:27 +0000109 }
110
111 void SetSectionText() {
Rafael Espindola3294e052013-11-25 16:00:32 +0000112 SwitchSection(getSectionText());
Michael J. Spencer86bbd712010-10-09 15:44:27 +0000113 EmitCodeAlignment(4, 0);
114 }
115
116 void SetSectionData() {
Rafael Espindola3294e052013-11-25 16:00:32 +0000117 SwitchSection(getSectionData());
Michael J. Spencer86bbd712010-10-09 15:44:27 +0000118 EmitCodeAlignment(4, 0);
119 }
120
121 void SetSectionBSS() {
Rafael Espindola3294e052013-11-25 16:00:32 +0000122 SwitchSection(getSectionBSS());
Michael J. Spencer86bbd712010-10-09 15:44:27 +0000123 EmitCodeAlignment(4, 0);
124 }
Chris Lattner56725be2010-07-11 22:05:00 +0000125};
126} // end anonymous namespace.
127
Chandler Carruthde093ef2013-01-31 23:29:57 +0000128WinCOFFStreamer::WinCOFFStreamer(MCContext &Context, MCAsmBackend &MAB,
129 MCCodeEmitter &CE, raw_ostream &OS)
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000130 : MCObjectStreamer(Context, 0, MAB, OS, &CE), CurSymbol(NULL) {}
Michael J. Spencere2da0a42010-07-19 06:13:10 +0000131
132void WinCOFFStreamer::AddCommonSymbol(MCSymbol *Symbol, uint64_t Size,
133 unsigned ByteAlignment, bool External) {
134 assert(!Symbol->isInSection() && "Symbol must not already have a section!");
135
Rafael Espindolae308c0c2014-01-23 22:49:25 +0000136 const MCSection *Section = getSectionBSS();
Michael J. Spencere2da0a42010-07-19 06:13:10 +0000137 MCSectionData &SectionData = getAssembler().getOrCreateSectionData(*Section);
Michael J. Spencere2da0a42010-07-19 06:13:10 +0000138 if (SectionData.getAlignment() < ByteAlignment)
139 SectionData.setAlignment(ByteAlignment);
140
Rafael Espindolaedcf1ff2013-11-25 16:06:04 +0000141 MCSymbolData &SymbolData = getAssembler().getOrCreateSymbolData(*Symbol);
Michael J. Spencere2da0a42010-07-19 06:13:10 +0000142 SymbolData.setExternal(External);
143
Richard Mitton21101b32013-09-19 23:21:01 +0000144 AssignSection(Symbol, Section);
Michael J. Spencere2da0a42010-07-19 06:13:10 +0000145
146 if (ByteAlignment != 1)
147 new MCAlignFragment(ByteAlignment, 0, 0, ByteAlignment, &SectionData);
148
149 SymbolData.setFragment(new MCFillFragment(0, 0, Size, &SectionData));
Chris Lattner56725be2010-07-11 22:05:00 +0000150}
151
152// MCStreamer interface
153
Eli Benderskycbb25142013-01-14 19:04:57 +0000154void WinCOFFStreamer::InitToTextSection() {
155 SetSectionText();
156}
157
Rafael Espindolaf667d922010-09-15 21:48:40 +0000158void WinCOFFStreamer::InitSections() {
Michael J. Spencer86bbd712010-10-09 15:44:27 +0000159 SetSectionText();
160 SetSectionData();
161 SetSectionBSS();
162 SetSectionText();
Rafael Espindolaf667d922010-09-15 21:48:40 +0000163}
164
Rafael Espindola16795802010-11-28 16:22:59 +0000165void WinCOFFStreamer::EmitLabel(MCSymbol *Symbol) {
Rafael Espindola16795802010-11-28 16:22:59 +0000166 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
Rafael Espindolae5b74152010-11-28 17:18:55 +0000167 MCObjectStreamer::EmitLabel(Symbol);
Rafael Espindola16795802010-11-28 16:22:59 +0000168}
169
Reed Kotleraee4d5d12012-12-16 04:00:45 +0000170void WinCOFFStreamer::EmitDebugLabel(MCSymbol *Symbol) {
171 EmitLabel(Symbol);
172}
Chris Lattner56725be2010-07-11 22:05:00 +0000173void WinCOFFStreamer::EmitAssemblerFlag(MCAssemblerFlag Flag) {
Michael J. Spencere2da0a42010-07-19 06:13:10 +0000174 llvm_unreachable("not implemented");
Chris Lattner56725be2010-07-11 22:05:00 +0000175}
176
Jim Grosbach5a2c68d2010-11-05 22:08:08 +0000177void WinCOFFStreamer::EmitThumbFunc(MCSymbol *Func) {
178 llvm_unreachable("not implemented");
179}
180
Saleem Abdulrasool4208b612013-08-09 01:52:03 +0000181bool WinCOFFStreamer::EmitSymbolAttribute(MCSymbol *Symbol,
Chris Lattner56725be2010-07-11 22:05:00 +0000182 MCSymbolAttr Attribute) {
Michael J. Spencerbe52c622010-10-09 11:00:37 +0000183 assert(Symbol && "Symbol must be non-null!");
184 assert((Symbol->isInSection()
185 ? Symbol->getSection().getVariant() == MCSection::SV_COFF
Alp Tokerf907b892013-12-05 05:44:44 +0000186 : true) && "Got non-COFF section in the COFF backend!");
Michael J. Spencere2da0a42010-07-19 06:13:10 +0000187 switch (Attribute) {
188 case MCSA_WeakReference:
Michael J. Spencer17990d52010-10-16 08:25:57 +0000189 case MCSA_Weak: {
190 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
191 SD.modifyFlags(COFF::SF_WeakExternal, COFF::SF_WeakExternal);
192 SD.setExternal(true);
193 }
Michael J. Spencere2da0a42010-07-19 06:13:10 +0000194 break;
195
196 case MCSA_Global:
197 getAssembler().getOrCreateSymbolData(*Symbol).setExternal(true);
198 break;
199
200 default:
Saleem Abdulrasool4208b612013-08-09 01:52:03 +0000201 return false;
Michael J. Spencere2da0a42010-07-19 06:13:10 +0000202 }
Saleem Abdulrasool4208b612013-08-09 01:52:03 +0000203
204 return true;
Chris Lattner56725be2010-07-11 22:05:00 +0000205}
206
207void WinCOFFStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
Michael J. Spencere2da0a42010-07-19 06:13:10 +0000208 llvm_unreachable("not implemented");
Chris Lattner56725be2010-07-11 22:05:00 +0000209}
210
211void WinCOFFStreamer::BeginCOFFSymbolDef(MCSymbol const *Symbol) {
Michael J. Spencerbe52c622010-10-09 11:00:37 +0000212 assert((Symbol->isInSection()
213 ? Symbol->getSection().getVariant() == MCSection::SV_COFF
Alp Tokerf907b892013-12-05 05:44:44 +0000214 : true) && "Got non-COFF section in the COFF backend!");
Michael J. Spencere2da0a42010-07-19 06:13:10 +0000215 assert(CurSymbol == NULL && "EndCOFFSymbolDef must be called between calls "
216 "to BeginCOFFSymbolDef!");
217 CurSymbol = Symbol;
Chris Lattner56725be2010-07-11 22:05:00 +0000218}
219
220void WinCOFFStreamer::EmitCOFFSymbolStorageClass(int StorageClass) {
Michael J. Spencere2da0a42010-07-19 06:13:10 +0000221 assert(CurSymbol != NULL && "BeginCOFFSymbolDef must be called first!");
222 assert((StorageClass & ~0xFF) == 0 && "StorageClass must only have data in "
223 "the first byte!");
224
225 getAssembler().getOrCreateSymbolData(*CurSymbol).modifyFlags(
226 StorageClass << COFF::SF_ClassShift,
227 COFF::SF_ClassMask);
Chris Lattner56725be2010-07-11 22:05:00 +0000228}
229
230void WinCOFFStreamer::EmitCOFFSymbolType(int Type) {
Michael J. Spencere2da0a42010-07-19 06:13:10 +0000231 assert(CurSymbol != NULL && "BeginCOFFSymbolDef must be called first!");
232 assert((Type & ~0xFFFF) == 0 && "Type must only have data in the first 2 "
233 "bytes");
234
235 getAssembler().getOrCreateSymbolData(*CurSymbol).modifyFlags(
236 Type << COFF::SF_TypeShift,
237 COFF::SF_TypeMask);
Chris Lattner56725be2010-07-11 22:05:00 +0000238}
239
240void WinCOFFStreamer::EndCOFFSymbolDef() {
Michael J. Spencere2da0a42010-07-19 06:13:10 +0000241 assert(CurSymbol != NULL && "BeginCOFFSymbolDef must be called first!");
242 CurSymbol = NULL;
Chris Lattner56725be2010-07-11 22:05:00 +0000243}
244
Timur Iskhodzhanov09069e02013-12-20 20:16:51 +0000245void WinCOFFStreamer::EmitCOFFSectionIndex(MCSymbol const *Symbol) {
Timur Iskhodzhanovc1fb2d62013-12-20 18:15:00 +0000246 MCDataFragment *DF = getOrCreateDataFragment();
Timur Iskhodzhanov09069e02013-12-20 20:16:51 +0000247 DF->getFixups().push_back(MCFixup::Create(
248 DF->getContents().size(), MCSymbolRefExpr::Create(Symbol, getContext()),
249 FK_SecRel_2));
Timur Iskhodzhanovc1fb2d62013-12-20 18:15:00 +0000250 DF->getContents().resize(DF->getContents().size() + 4, 0);
251}
252
Timur Iskhodzhanov09069e02013-12-20 20:16:51 +0000253void WinCOFFStreamer::EmitCOFFSecRel32(MCSymbol const *Symbol) {
Rafael Espindolad3df3d32011-12-17 01:14:52 +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_4));
Rafael Espindolad3df3d32011-12-17 01:14:52 +0000258 DF->getContents().resize(DF->getContents().size() + 4, 0);
259}
260
Chris Lattner56725be2010-07-11 22:05:00 +0000261void WinCOFFStreamer::EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) {
Michael J. Spencere2da0a42010-07-19 06:13:10 +0000262 llvm_unreachable("not implemented");
Chris Lattner56725be2010-07-11 22:05:00 +0000263}
264
265void WinCOFFStreamer::EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
Michael J. Spencere2da0a42010-07-19 06:13:10 +0000266 unsigned ByteAlignment) {
Michael J. Spencerbe52c622010-10-09 11:00:37 +0000267 assert((Symbol->isInSection()
268 ? Symbol->getSection().getVariant() == MCSection::SV_COFF
Alp Tokerf907b892013-12-05 05:44:44 +0000269 : true) && "Got non-COFF section in the COFF backend!");
Michael J. Spencere2da0a42010-07-19 06:13:10 +0000270 AddCommonSymbol(Symbol, Size, ByteAlignment, true);
Chris Lattner56725be2010-07-11 22:05:00 +0000271}
272
Benjamin Kramer63970512011-09-01 23:04:27 +0000273void WinCOFFStreamer::EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size,
274 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!");
Benjamin Kramer63970512011-09-01 23:04:27 +0000278 AddCommonSymbol(Symbol, Size, ByteAlignment, false);
Chris Lattner56725be2010-07-11 22:05:00 +0000279}
280
281void WinCOFFStreamer::EmitZerofill(const MCSection *Section, MCSymbol *Symbol,
Evan Chengf5bd6c62012-06-22 20:14:46 +0000282 uint64_t Size,unsigned ByteAlignment) {
Michael J. Spencere2da0a42010-07-19 06:13:10 +0000283 llvm_unreachable("not implemented");
Chris Lattner56725be2010-07-11 22:05:00 +0000284}
285
286void WinCOFFStreamer::EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol,
287 uint64_t Size, unsigned ByteAlignment) {
Michael J. Spencere2da0a42010-07-19 06:13:10 +0000288 llvm_unreachable("not implemented");
Chris Lattner56725be2010-07-11 22:05:00 +0000289}
290
Chris Lattner56725be2010-07-11 22:05:00 +0000291void WinCOFFStreamer::EmitFileDirective(StringRef Filename) {
292 // Ignore for now, linkers don't care, and proper debug
293 // info will be a much large effort.
294}
295
Rafael Espindola5645bad2013-10-16 01:05:45 +0000296// TODO: Implement this if you want to emit .comment section in COFF obj files.
297void WinCOFFStreamer::EmitIdent(StringRef IdentString) {
298 llvm_unreachable("unsupported directive");
299}
300
Charles Davis1c8bd5a2011-05-22 03:01:05 +0000301void WinCOFFStreamer::EmitWin64EHHandlerData() {
302 MCStreamer::EmitWin64EHHandlerData();
303
304 // We have to emit the unwind info now, because this directive
305 // actually switches to the .xdata section!
306 MCWin64EHUnwindEmitter::EmitUnwindInfo(*this, getCurrentW64UnwindInfo());
307}
308
Rafael Espindola07082092012-01-07 03:13:18 +0000309void WinCOFFStreamer::FinishImpl() {
Rafael Espindolaaf7131d2013-12-02 14:59:34 +0000310 EmitFrames(NULL, true);
Charles Davisbc2daa02011-05-22 04:15:07 +0000311 EmitW64Tables();
Rafael Espindola07082092012-01-07 03:13:18 +0000312 MCObjectStreamer::FinishImpl();
Chris Lattner56725be2010-07-11 22:05:00 +0000313}
314
315namespace llvm
316{
317 MCStreamer *createWinCOFFStreamer(MCContext &Context,
Evan Cheng5928e692011-07-25 23:24:55 +0000318 MCAsmBackend &MAB,
Chris Lattner56725be2010-07-11 22:05:00 +0000319 MCCodeEmitter &CE,
Michael J. Spencer6b4925e2010-07-31 06:22:29 +0000320 raw_ostream &OS,
321 bool RelaxAll) {
Evan Cheng5928e692011-07-25 23:24:55 +0000322 WinCOFFStreamer *S = new WinCOFFStreamer(Context, MAB, CE, OS);
Michael J. Spencer6b4925e2010-07-31 06:22:29 +0000323 S->getAssembler().setRelaxAll(RelaxAll);
324 return S;
Chris Lattner56725be2010-07-11 22:05:00 +0000325 }
326}