blob: cc2c272f38b8218e653ef2a2bf00716835af2c7f [file] [log] [blame]
Chris Lattnereb72dca2010-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 Carruthd04a8d42012-12-03 16:50:05 +000016#include "llvm/MC/MCStreamer.h"
Evan Cheng78c10ee2011-07-25 23:24:55 +000017#include "llvm/MC/MCAsmBackend.h"
Chandler Carruthd04a8d42012-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 Lattnereb72dca2010-07-11 22:05:00 +000029#include "llvm/Support/COFF.h"
30#include "llvm/Support/Debug.h"
31#include "llvm/Support/ErrorHandling.h"
Evan Cheng3e74d6f2011-08-24 18:08:43 +000032#include "llvm/Support/TargetRegistry.h"
Chris Lattnereb72dca2010-07-11 22:05:00 +000033#include "llvm/Support/raw_ostream.h"
Rafael Espindola8f7d12c2011-12-17 01:14:52 +000034
Chris Lattnereb72dca2010-07-11 22:05:00 +000035using namespace llvm;
36
Chris Lattnereb72dca2010-07-11 22:05:00 +000037namespace {
38class WinCOFFStreamer : public MCObjectStreamer {
39public:
Michael J. Spencer8067adc2010-07-19 06:13:10 +000040 MCSymbol const *CurSymbol;
41
Chris Lattnereb72dca2010-07-11 22:05:00 +000042 WinCOFFStreamer(MCContext &Context,
Evan Cheng78c10ee2011-07-25 23:24:55 +000043 MCAsmBackend &MAB,
Chris Lattnereb72dca2010-07-11 22:05:00 +000044 MCCodeEmitter &CE,
45 raw_ostream &OS);
46
Michael J. Spencer8067adc2010-07-19 06:13:10 +000047 void AddCommonSymbol(MCSymbol *Symbol, uint64_t Size,
48 unsigned ByteAlignment, bool External);
49
Chris Lattnereb72dca2010-07-11 22:05:00 +000050 // MCStreamer interface
51
Rafael Espindolad80781b2010-09-15 21:48:40 +000052 virtual void InitSections();
Eli Bendersky030f63a2013-01-14 19:04:57 +000053 virtual void InitToTextSection();
Rafael Espindolaba210242010-11-28 16:22:59 +000054 virtual void EmitLabel(MCSymbol *Symbol);
Reed Kotler2c3a4642012-12-16 04:00:45 +000055 virtual void EmitDebugLabel(MCSymbol *Symbol);
Chris Lattnereb72dca2010-07-11 22:05:00 +000056 virtual void EmitAssemblerFlag(MCAssemblerFlag Flag);
Jim Grosbachce792992010-11-05 22:08:08 +000057 virtual void EmitThumbFunc(MCSymbol *Func);
Chris Lattnereb72dca2010-07-11 22:05:00 +000058 virtual void EmitAssignment(MCSymbol *Symbol, const MCExpr *Value);
59 virtual void EmitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute);
60 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();
Rafael Espindola8f7d12c2011-12-17 01:14:52 +000065 virtual void EmitCOFFSecRel32(MCSymbol const *Symbol);
Chris Lattnereb72dca2010-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. Spencer8067adc2010-07-19 06:13:10 +000068 unsigned ByteAlignment);
Benjamin Kramer36a16012011-09-01 23:04:27 +000069 virtual void EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size,
70 unsigned ByteAlignment);
Chris Lattnereb72dca2010-07-11 22:05:00 +000071 virtual void EmitZerofill(const MCSection *Section, MCSymbol *Symbol,
Evan Chengc90a1fc2012-06-22 20:14:46 +000072 uint64_t Size,unsigned ByteAlignment);
Chris Lattnereb72dca2010-07-11 22:05:00 +000073 virtual void EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol,
Michael J. Spencer8067adc2010-07-19 06:13:10 +000074 uint64_t Size, unsigned ByteAlignment);
Chris Lattnereb72dca2010-07-11 22:05:00 +000075 virtual void EmitFileDirective(StringRef Filename);
Charles Davis3185f5c2011-05-22 03:01:05 +000076 virtual void EmitWin64EHHandlerData();
Rafael Espindola99b42372012-01-07 03:13:18 +000077 virtual void FinishImpl();
Michael J. Spencer192d1362010-10-09 15:44:27 +000078
79private:
Rafael Espindolaf89671d2010-11-01 16:27:31 +000080 virtual void EmitInstToData(const MCInst &Inst) {
Michael J. Spencerf94cfb62013-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 Espindolaf89671d2010-11-01 16:27:31 +000095 }
96
Michael J. Spencer192d1362010-10-09 15:44:27 +000097 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,
108 SectionKind::getText());
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,
117 SectionKind::getDataRel());
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,
126 SectionKind::getBSS());
127 EmitCodeAlignment(4, 0);
128 }
Chris Lattnereb72dca2010-07-11 22:05:00 +0000129};
130} // end anonymous namespace.
131
132WinCOFFStreamer::WinCOFFStreamer(MCContext &Context,
Evan Cheng78c10ee2011-07-25 23:24:55 +0000133 MCAsmBackend &MAB,
Chris Lattnereb72dca2010-07-11 22:05:00 +0000134 MCCodeEmitter &CE,
135 raw_ostream &OS)
Evan Cheng78c10ee2011-07-25 23:24:55 +0000136 : MCObjectStreamer(Context, MAB, OS, &CE)
Michael J. Spencer8067adc2010-07-19 06:13:10 +0000137 , CurSymbol(NULL) {
138}
139
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
144 std::string SectionName(".bss$linkonce");
145 SectionName.append(Symbol->getName().begin(), Symbol->getName().end());
146
147 MCSymbolData &SymbolData = getAssembler().getOrCreateSymbolData(*Symbol);
148
149 unsigned Characteristics =
150 COFF::IMAGE_SCN_LNK_COMDAT |
151 COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA |
152 COFF::IMAGE_SCN_MEM_READ |
153 COFF::IMAGE_SCN_MEM_WRITE;
154
155 int Selection = COFF::IMAGE_COMDAT_SELECT_LARGEST;
156
157 const MCSection *Section = MCStreamer::getContext().getCOFFSection(
158 SectionName, Characteristics, Selection, SectionKind::getBSS());
159
160 MCSectionData &SectionData = getAssembler().getOrCreateSectionData(*Section);
161
162 if (SectionData.getAlignment() < ByteAlignment)
163 SectionData.setAlignment(ByteAlignment);
164
165 SymbolData.setExternal(External);
166
167 Symbol->setSection(*Section);
168
169 if (ByteAlignment != 1)
170 new MCAlignFragment(ByteAlignment, 0, 0, ByteAlignment, &SectionData);
171
172 SymbolData.setFragment(new MCFillFragment(0, 0, Size, &SectionData));
Chris Lattnereb72dca2010-07-11 22:05:00 +0000173}
174
175// MCStreamer interface
176
Eli Bendersky030f63a2013-01-14 19:04:57 +0000177void WinCOFFStreamer::InitToTextSection() {
178 SetSectionText();
179}
180
Rafael Espindolad80781b2010-09-15 21:48:40 +0000181void WinCOFFStreamer::InitSections() {
Michael J. Spencer192d1362010-10-09 15:44:27 +0000182 SetSectionText();
183 SetSectionData();
184 SetSectionBSS();
185 SetSectionText();
Rafael Espindolad80781b2010-09-15 21:48:40 +0000186}
187
Rafael Espindolaba210242010-11-28 16:22:59 +0000188void WinCOFFStreamer::EmitLabel(MCSymbol *Symbol) {
Rafael Espindolaba210242010-11-28 16:22:59 +0000189 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
Rafael Espindolaea4afa92010-11-28 17:18:55 +0000190 MCObjectStreamer::EmitLabel(Symbol);
Rafael Espindolaba210242010-11-28 16:22:59 +0000191}
192
Reed Kotler2c3a4642012-12-16 04:00:45 +0000193void WinCOFFStreamer::EmitDebugLabel(MCSymbol *Symbol) {
194 EmitLabel(Symbol);
195}
Chris Lattnereb72dca2010-07-11 22:05:00 +0000196void WinCOFFStreamer::EmitAssemblerFlag(MCAssemblerFlag Flag) {
Michael J. Spencer8067adc2010-07-19 06:13:10 +0000197 llvm_unreachable("not implemented");
Chris Lattnereb72dca2010-07-11 22:05:00 +0000198}
199
Jim Grosbachce792992010-11-05 22:08:08 +0000200void WinCOFFStreamer::EmitThumbFunc(MCSymbol *Func) {
201 llvm_unreachable("not implemented");
202}
203
Chris Lattnereb72dca2010-07-11 22:05:00 +0000204void WinCOFFStreamer::EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) {
Michael J. Spencerd47f4a92010-10-09 11:00:37 +0000205 assert((Symbol->isInSection()
206 ? Symbol->getSection().getVariant() == MCSection::SV_COFF
207 : true) && "Got non COFF section in the COFF backend!");
Michael J. Spencerb5814a32010-10-07 06:29:33 +0000208 // FIXME: This is all very ugly and depressing. What needs to happen here
209 // depends on quite a few things that are all part of relaxation, which we
210 // don't really even do.
211
212 if (Value->getKind() != MCExpr::SymbolRef) {
Eli Benderskycd81dce2012-12-07 17:55:28 +0000213 MCObjectStreamer::EmitAssignment(Symbol, Value);
Michael J. Spencerb5814a32010-10-07 06:29:33 +0000214 } else {
215 // FIXME: This is a horrible way to do this :(. This should really be
216 // handled after we are done with the MC* objects and immediately before
217 // writing out the object file when we know exactly what the symbol should
218 // look like in the coff symbol table. I'm not doing that now because the
219 // COFF object writer doesn't have a clearly defined separation between MC
220 // data structures, the object writers data structures, and the raw, POD,
221 // data structures that get written to disk.
222
223 // Copy over the aliased data.
224 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
225 const MCSymbolData &RealSD = getAssembler().getOrCreateSymbolData(
226 dyn_cast<const MCSymbolRefExpr>(Value)->getSymbol());
227
228 // FIXME: This is particularly nasty because it breaks as soon as any data
229 // members of MCSymbolData change.
230 SD.CommonAlign = RealSD.CommonAlign;
231 SD.CommonSize = RealSD.CommonSize;
232 SD.Flags = RealSD.Flags;
233 SD.Fragment = RealSD.Fragment;
234 SD.Index = RealSD.Index;
235 SD.IsExternal = RealSD.IsExternal;
236 SD.IsPrivateExtern = RealSD.IsPrivateExtern;
237 SD.Offset = RealSD.Offset;
238 SD.SymbolSize = RealSD.SymbolSize;
239 }
Chris Lattnereb72dca2010-07-11 22:05:00 +0000240}
241
242void WinCOFFStreamer::EmitSymbolAttribute(MCSymbol *Symbol,
243 MCSymbolAttr Attribute) {
Michael J. Spencerd47f4a92010-10-09 11:00:37 +0000244 assert(Symbol && "Symbol must be non-null!");
245 assert((Symbol->isInSection()
246 ? Symbol->getSection().getVariant() == MCSection::SV_COFF
247 : true) && "Got non COFF section in the COFF backend!");
Michael J. Spencer8067adc2010-07-19 06:13:10 +0000248 switch (Attribute) {
249 case MCSA_WeakReference:
Michael J. Spencer4cee2892010-10-16 08:25:57 +0000250 case MCSA_Weak: {
251 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
252 SD.modifyFlags(COFF::SF_WeakExternal, COFF::SF_WeakExternal);
253 SD.setExternal(true);
254 }
Michael J. Spencer8067adc2010-07-19 06:13:10 +0000255 break;
256
257 case MCSA_Global:
258 getAssembler().getOrCreateSymbolData(*Symbol).setExternal(true);
259 break;
260
261 default:
262 llvm_unreachable("unsupported attribute");
Michael J. Spencer8067adc2010-07-19 06:13:10 +0000263 }
Chris Lattnereb72dca2010-07-11 22:05:00 +0000264}
265
266void WinCOFFStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
Michael J. Spencer8067adc2010-07-19 06:13:10 +0000267 llvm_unreachable("not implemented");
Chris Lattnereb72dca2010-07-11 22:05:00 +0000268}
269
270void WinCOFFStreamer::BeginCOFFSymbolDef(MCSymbol const *Symbol) {
Michael J. Spencerd47f4a92010-10-09 11:00:37 +0000271 assert((Symbol->isInSection()
272 ? Symbol->getSection().getVariant() == MCSection::SV_COFF
273 : true) && "Got non COFF section in the COFF backend!");
Michael J. Spencer8067adc2010-07-19 06:13:10 +0000274 assert(CurSymbol == NULL && "EndCOFFSymbolDef must be called between calls "
275 "to BeginCOFFSymbolDef!");
276 CurSymbol = Symbol;
Chris Lattnereb72dca2010-07-11 22:05:00 +0000277}
278
279void WinCOFFStreamer::EmitCOFFSymbolStorageClass(int StorageClass) {
Michael J. Spencer8067adc2010-07-19 06:13:10 +0000280 assert(CurSymbol != NULL && "BeginCOFFSymbolDef must be called first!");
281 assert((StorageClass & ~0xFF) == 0 && "StorageClass must only have data in "
282 "the first byte!");
283
284 getAssembler().getOrCreateSymbolData(*CurSymbol).modifyFlags(
285 StorageClass << COFF::SF_ClassShift,
286 COFF::SF_ClassMask);
Chris Lattnereb72dca2010-07-11 22:05:00 +0000287}
288
289void WinCOFFStreamer::EmitCOFFSymbolType(int Type) {
Michael J. Spencer8067adc2010-07-19 06:13:10 +0000290 assert(CurSymbol != NULL && "BeginCOFFSymbolDef must be called first!");
291 assert((Type & ~0xFFFF) == 0 && "Type must only have data in the first 2 "
292 "bytes");
293
294 getAssembler().getOrCreateSymbolData(*CurSymbol).modifyFlags(
295 Type << COFF::SF_TypeShift,
296 COFF::SF_TypeMask);
Chris Lattnereb72dca2010-07-11 22:05:00 +0000297}
298
299void WinCOFFStreamer::EndCOFFSymbolDef() {
Michael J. Spencer8067adc2010-07-19 06:13:10 +0000300 assert(CurSymbol != NULL && "BeginCOFFSymbolDef must be called first!");
301 CurSymbol = NULL;
Chris Lattnereb72dca2010-07-11 22:05:00 +0000302}
303
Rafael Espindola8f7d12c2011-12-17 01:14:52 +0000304void WinCOFFStreamer::EmitCOFFSecRel32(MCSymbol const *Symbol)
305{
306 MCDataFragment *DF = getOrCreateDataFragment();
307
Eli Bendersky64d9a322012-12-07 19:13:57 +0000308 DF->getFixups().push_back(
309 MCFixup::Create(DF->getContents().size(),
310 MCSymbolRefExpr::Create (Symbol, getContext ()),
311 FK_SecRel_4));
Rafael Espindola8f7d12c2011-12-17 01:14:52 +0000312 DF->getContents().resize(DF->getContents().size() + 4, 0);
313}
314
Chris Lattnereb72dca2010-07-11 22:05:00 +0000315void WinCOFFStreamer::EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) {
Michael J. Spencer8067adc2010-07-19 06:13:10 +0000316 llvm_unreachable("not implemented");
Chris Lattnereb72dca2010-07-11 22:05:00 +0000317}
318
319void WinCOFFStreamer::EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
Michael J. Spencer8067adc2010-07-19 06:13:10 +0000320 unsigned ByteAlignment) {
Michael J. Spencerd47f4a92010-10-09 11:00:37 +0000321 assert((Symbol->isInSection()
322 ? Symbol->getSection().getVariant() == MCSection::SV_COFF
323 : true) && "Got non COFF section in the COFF backend!");
Michael J. Spencer8067adc2010-07-19 06:13:10 +0000324 AddCommonSymbol(Symbol, Size, ByteAlignment, true);
Chris Lattnereb72dca2010-07-11 22:05:00 +0000325}
326
Benjamin Kramer36a16012011-09-01 23:04:27 +0000327void WinCOFFStreamer::EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size,
328 unsigned ByteAlignment) {
Michael J. Spencerd47f4a92010-10-09 11:00:37 +0000329 assert((Symbol->isInSection()
330 ? Symbol->getSection().getVariant() == MCSection::SV_COFF
331 : true) && "Got non COFF section in the COFF backend!");
Benjamin Kramer36a16012011-09-01 23:04:27 +0000332 AddCommonSymbol(Symbol, Size, ByteAlignment, false);
Chris Lattnereb72dca2010-07-11 22:05:00 +0000333}
334
335void WinCOFFStreamer::EmitZerofill(const MCSection *Section, MCSymbol *Symbol,
Evan Chengc90a1fc2012-06-22 20:14:46 +0000336 uint64_t Size,unsigned ByteAlignment) {
Michael J. Spencer8067adc2010-07-19 06:13:10 +0000337 llvm_unreachable("not implemented");
Chris Lattnereb72dca2010-07-11 22:05:00 +0000338}
339
340void WinCOFFStreamer::EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol,
341 uint64_t Size, unsigned ByteAlignment) {
Michael J. Spencer8067adc2010-07-19 06:13:10 +0000342 llvm_unreachable("not implemented");
Chris Lattnereb72dca2010-07-11 22:05:00 +0000343}
344
Chris Lattnereb72dca2010-07-11 22:05:00 +0000345void WinCOFFStreamer::EmitFileDirective(StringRef Filename) {
346 // Ignore for now, linkers don't care, and proper debug
347 // info will be a much large effort.
348}
349
Charles Davis3185f5c2011-05-22 03:01:05 +0000350void WinCOFFStreamer::EmitWin64EHHandlerData() {
351 MCStreamer::EmitWin64EHHandlerData();
352
353 // We have to emit the unwind info now, because this directive
354 // actually switches to the .xdata section!
355 MCWin64EHUnwindEmitter::EmitUnwindInfo(*this, getCurrentW64UnwindInfo());
356}
357
Rafael Espindola99b42372012-01-07 03:13:18 +0000358void WinCOFFStreamer::FinishImpl() {
Charles Davis38ea9ee2011-05-22 04:15:07 +0000359 EmitW64Tables();
Rafael Espindola99b42372012-01-07 03:13:18 +0000360 MCObjectStreamer::FinishImpl();
Chris Lattnereb72dca2010-07-11 22:05:00 +0000361}
362
363namespace llvm
364{
365 MCStreamer *createWinCOFFStreamer(MCContext &Context,
Evan Cheng78c10ee2011-07-25 23:24:55 +0000366 MCAsmBackend &MAB,
Chris Lattnereb72dca2010-07-11 22:05:00 +0000367 MCCodeEmitter &CE,
Michael J. Spencere2195d82010-07-31 06:22:29 +0000368 raw_ostream &OS,
369 bool RelaxAll) {
Evan Cheng78c10ee2011-07-25 23:24:55 +0000370 WinCOFFStreamer *S = new WinCOFFStreamer(Context, MAB, CE, OS);
Michael J. Spencere2195d82010-07-31 06:22:29 +0000371 S->getAssembler().setRelaxAll(RelaxAll);
372 return S;
Chris Lattnereb72dca2010-07-11 22:05:00 +0000373 }
374}