blob: e8fe68016f33c843ce34424b62a5070bd3536260 [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();
Rafael Espindolaba210242010-11-28 16:22:59 +000053 virtual void EmitLabel(MCSymbol *Symbol);
Chris Lattnereb72dca2010-07-11 22:05:00 +000054 virtual void EmitAssemblerFlag(MCAssemblerFlag Flag);
Jim Grosbachce792992010-11-05 22:08:08 +000055 virtual void EmitThumbFunc(MCSymbol *Func);
Chris Lattnereb72dca2010-07-11 22:05:00 +000056 virtual void EmitAssignment(MCSymbol *Symbol, const MCExpr *Value);
57 virtual void EmitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute);
58 virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue);
59 virtual void BeginCOFFSymbolDef(MCSymbol const *Symbol);
60 virtual void EmitCOFFSymbolStorageClass(int StorageClass);
61 virtual void EmitCOFFSymbolType(int Type);
62 virtual void EndCOFFSymbolDef();
Rafael Espindola8f7d12c2011-12-17 01:14:52 +000063 virtual void EmitCOFFSecRel32(MCSymbol const *Symbol);
Chris Lattnereb72dca2010-07-11 22:05:00 +000064 virtual void EmitELFSize(MCSymbol *Symbol, const MCExpr *Value);
65 virtual void EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
Michael J. Spencer8067adc2010-07-19 06:13:10 +000066 unsigned ByteAlignment);
Benjamin Kramer36a16012011-09-01 23:04:27 +000067 virtual void EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size,
68 unsigned ByteAlignment);
Chris Lattnereb72dca2010-07-11 22:05:00 +000069 virtual void EmitZerofill(const MCSection *Section, MCSymbol *Symbol,
Evan Chengc90a1fc2012-06-22 20:14:46 +000070 uint64_t Size,unsigned ByteAlignment);
Chris Lattnereb72dca2010-07-11 22:05:00 +000071 virtual void EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol,
Michael J. Spencer8067adc2010-07-19 06:13:10 +000072 uint64_t Size, unsigned ByteAlignment);
Chris Lattnereb72dca2010-07-11 22:05:00 +000073 virtual void EmitFileDirective(StringRef Filename);
Chris Lattnereb72dca2010-07-11 22:05:00 +000074 virtual void EmitInstruction(const MCInst &Instruction);
Charles Davis3185f5c2011-05-22 03:01:05 +000075 virtual void EmitWin64EHHandlerData();
Rafael Espindola99b42372012-01-07 03:13:18 +000076 virtual void FinishImpl();
Michael J. Spencer192d1362010-10-09 15:44:27 +000077
78private:
Rafael Espindolaf89671d2010-11-01 16:27:31 +000079 virtual void EmitInstToFragment(const MCInst &Inst) {
80 llvm_unreachable("Not used by WinCOFF.");
81 }
82 virtual void EmitInstToData(const MCInst &Inst) {
83 llvm_unreachable("Not used by WinCOFF.");
84 }
85
Michael J. Spencer192d1362010-10-09 15:44:27 +000086 void SetSection(StringRef Section,
87 unsigned Characteristics,
88 SectionKind Kind) {
89 SwitchSection(getContext().getCOFFSection(Section, Characteristics, Kind));
90 }
91
92 void SetSectionText() {
93 SetSection(".text",
94 COFF::IMAGE_SCN_CNT_CODE
95 | COFF::IMAGE_SCN_MEM_EXECUTE
96 | COFF::IMAGE_SCN_MEM_READ,
97 SectionKind::getText());
98 EmitCodeAlignment(4, 0);
99 }
100
101 void SetSectionData() {
102 SetSection(".data",
103 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA
104 | COFF::IMAGE_SCN_MEM_READ
105 | COFF::IMAGE_SCN_MEM_WRITE,
106 SectionKind::getDataRel());
107 EmitCodeAlignment(4, 0);
108 }
109
110 void SetSectionBSS() {
111 SetSection(".bss",
112 COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA
113 | COFF::IMAGE_SCN_MEM_READ
114 | COFF::IMAGE_SCN_MEM_WRITE,
115 SectionKind::getBSS());
116 EmitCodeAlignment(4, 0);
117 }
118
Chris Lattnereb72dca2010-07-11 22:05:00 +0000119};
120} // end anonymous namespace.
121
122WinCOFFStreamer::WinCOFFStreamer(MCContext &Context,
Evan Cheng78c10ee2011-07-25 23:24:55 +0000123 MCAsmBackend &MAB,
Chris Lattnereb72dca2010-07-11 22:05:00 +0000124 MCCodeEmitter &CE,
125 raw_ostream &OS)
Evan Cheng78c10ee2011-07-25 23:24:55 +0000126 : MCObjectStreamer(Context, MAB, OS, &CE)
Michael J. Spencer8067adc2010-07-19 06:13:10 +0000127 , CurSymbol(NULL) {
128}
129
130void WinCOFFStreamer::AddCommonSymbol(MCSymbol *Symbol, uint64_t Size,
131 unsigned ByteAlignment, bool External) {
132 assert(!Symbol->isInSection() && "Symbol must not already have a section!");
133
134 std::string SectionName(".bss$linkonce");
135 SectionName.append(Symbol->getName().begin(), Symbol->getName().end());
136
137 MCSymbolData &SymbolData = getAssembler().getOrCreateSymbolData(*Symbol);
138
139 unsigned Characteristics =
140 COFF::IMAGE_SCN_LNK_COMDAT |
141 COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA |
142 COFF::IMAGE_SCN_MEM_READ |
143 COFF::IMAGE_SCN_MEM_WRITE;
144
145 int Selection = COFF::IMAGE_COMDAT_SELECT_LARGEST;
146
147 const MCSection *Section = MCStreamer::getContext().getCOFFSection(
148 SectionName, Characteristics, Selection, SectionKind::getBSS());
149
150 MCSectionData &SectionData = getAssembler().getOrCreateSectionData(*Section);
151
152 if (SectionData.getAlignment() < ByteAlignment)
153 SectionData.setAlignment(ByteAlignment);
154
155 SymbolData.setExternal(External);
156
157 Symbol->setSection(*Section);
158
159 if (ByteAlignment != 1)
160 new MCAlignFragment(ByteAlignment, 0, 0, ByteAlignment, &SectionData);
161
162 SymbolData.setFragment(new MCFillFragment(0, 0, Size, &SectionData));
Chris Lattnereb72dca2010-07-11 22:05:00 +0000163}
164
165// MCStreamer interface
166
Rafael Espindolad80781b2010-09-15 21:48:40 +0000167void WinCOFFStreamer::InitSections() {
Michael J. Spencer192d1362010-10-09 15:44:27 +0000168 SetSectionText();
169 SetSectionData();
170 SetSectionBSS();
171 SetSectionText();
Rafael Espindolad80781b2010-09-15 21:48:40 +0000172}
173
Rafael Espindolaba210242010-11-28 16:22:59 +0000174void WinCOFFStreamer::EmitLabel(MCSymbol *Symbol) {
Rafael Espindolaba210242010-11-28 16:22:59 +0000175 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
Rafael Espindolaea4afa92010-11-28 17:18:55 +0000176 MCObjectStreamer::EmitLabel(Symbol);
Rafael Espindolaba210242010-11-28 16:22:59 +0000177}
178
Chris Lattnereb72dca2010-07-11 22:05:00 +0000179void WinCOFFStreamer::EmitAssemblerFlag(MCAssemblerFlag Flag) {
Michael J. Spencer8067adc2010-07-19 06:13:10 +0000180 llvm_unreachable("not implemented");
Chris Lattnereb72dca2010-07-11 22:05:00 +0000181}
182
Jim Grosbachce792992010-11-05 22:08:08 +0000183void WinCOFFStreamer::EmitThumbFunc(MCSymbol *Func) {
184 llvm_unreachable("not implemented");
185}
186
Chris Lattnereb72dca2010-07-11 22:05:00 +0000187void WinCOFFStreamer::EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) {
Michael J. Spencerd47f4a92010-10-09 11:00:37 +0000188 assert((Symbol->isInSection()
189 ? Symbol->getSection().getVariant() == MCSection::SV_COFF
190 : true) && "Got non COFF section in the COFF backend!");
Michael J. Spencerb5814a32010-10-07 06:29:33 +0000191 // FIXME: This is all very ugly and depressing. What needs to happen here
192 // depends on quite a few things that are all part of relaxation, which we
193 // don't really even do.
194
195 if (Value->getKind() != MCExpr::SymbolRef) {
NAKAMURA Takumi86c36472010-10-07 07:21:04 +0000196 getAssembler().getOrCreateSymbolData(*Symbol);
197 AddValueSymbols(Value);
Michael J. Spencerb5814a32010-10-07 06:29:33 +0000198 Symbol->setVariableValue(Value);
199 } else {
200 // FIXME: This is a horrible way to do this :(. This should really be
201 // handled after we are done with the MC* objects and immediately before
202 // writing out the object file when we know exactly what the symbol should
203 // look like in the coff symbol table. I'm not doing that now because the
204 // COFF object writer doesn't have a clearly defined separation between MC
205 // data structures, the object writers data structures, and the raw, POD,
206 // data structures that get written to disk.
207
208 // Copy over the aliased data.
209 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
210 const MCSymbolData &RealSD = getAssembler().getOrCreateSymbolData(
211 dyn_cast<const MCSymbolRefExpr>(Value)->getSymbol());
212
213 // FIXME: This is particularly nasty because it breaks as soon as any data
214 // members of MCSymbolData change.
215 SD.CommonAlign = RealSD.CommonAlign;
216 SD.CommonSize = RealSD.CommonSize;
217 SD.Flags = RealSD.Flags;
218 SD.Fragment = RealSD.Fragment;
219 SD.Index = RealSD.Index;
220 SD.IsExternal = RealSD.IsExternal;
221 SD.IsPrivateExtern = RealSD.IsPrivateExtern;
222 SD.Offset = RealSD.Offset;
223 SD.SymbolSize = RealSD.SymbolSize;
224 }
Chris Lattnereb72dca2010-07-11 22:05:00 +0000225}
226
227void WinCOFFStreamer::EmitSymbolAttribute(MCSymbol *Symbol,
228 MCSymbolAttr Attribute) {
Michael J. Spencerd47f4a92010-10-09 11:00:37 +0000229 assert(Symbol && "Symbol must be non-null!");
230 assert((Symbol->isInSection()
231 ? Symbol->getSection().getVariant() == MCSection::SV_COFF
232 : true) && "Got non COFF section in the COFF backend!");
Michael J. Spencer8067adc2010-07-19 06:13:10 +0000233 switch (Attribute) {
234 case MCSA_WeakReference:
Michael J. Spencer4cee2892010-10-16 08:25:57 +0000235 case MCSA_Weak: {
236 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
237 SD.modifyFlags(COFF::SF_WeakExternal, COFF::SF_WeakExternal);
238 SD.setExternal(true);
239 }
Michael J. Spencer8067adc2010-07-19 06:13:10 +0000240 break;
241
242 case MCSA_Global:
243 getAssembler().getOrCreateSymbolData(*Symbol).setExternal(true);
244 break;
245
246 default:
247 llvm_unreachable("unsupported attribute");
Michael J. Spencer8067adc2010-07-19 06:13:10 +0000248 }
Chris Lattnereb72dca2010-07-11 22:05:00 +0000249}
250
251void WinCOFFStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
Michael J. Spencer8067adc2010-07-19 06:13:10 +0000252 llvm_unreachable("not implemented");
Chris Lattnereb72dca2010-07-11 22:05:00 +0000253}
254
255void WinCOFFStreamer::BeginCOFFSymbolDef(MCSymbol const *Symbol) {
Michael J. Spencerd47f4a92010-10-09 11:00:37 +0000256 assert((Symbol->isInSection()
257 ? Symbol->getSection().getVariant() == MCSection::SV_COFF
258 : true) && "Got non COFF section in the COFF backend!");
Michael J. Spencer8067adc2010-07-19 06:13:10 +0000259 assert(CurSymbol == NULL && "EndCOFFSymbolDef must be called between calls "
260 "to BeginCOFFSymbolDef!");
261 CurSymbol = Symbol;
Chris Lattnereb72dca2010-07-11 22:05:00 +0000262}
263
264void WinCOFFStreamer::EmitCOFFSymbolStorageClass(int StorageClass) {
Michael J. Spencer8067adc2010-07-19 06:13:10 +0000265 assert(CurSymbol != NULL && "BeginCOFFSymbolDef must be called first!");
266 assert((StorageClass & ~0xFF) == 0 && "StorageClass must only have data in "
267 "the first byte!");
268
269 getAssembler().getOrCreateSymbolData(*CurSymbol).modifyFlags(
270 StorageClass << COFF::SF_ClassShift,
271 COFF::SF_ClassMask);
Chris Lattnereb72dca2010-07-11 22:05:00 +0000272}
273
274void WinCOFFStreamer::EmitCOFFSymbolType(int Type) {
Michael J. Spencer8067adc2010-07-19 06:13:10 +0000275 assert(CurSymbol != NULL && "BeginCOFFSymbolDef must be called first!");
276 assert((Type & ~0xFFFF) == 0 && "Type must only have data in the first 2 "
277 "bytes");
278
279 getAssembler().getOrCreateSymbolData(*CurSymbol).modifyFlags(
280 Type << COFF::SF_TypeShift,
281 COFF::SF_TypeMask);
Chris Lattnereb72dca2010-07-11 22:05:00 +0000282}
283
284void WinCOFFStreamer::EndCOFFSymbolDef() {
Michael J. Spencer8067adc2010-07-19 06:13:10 +0000285 assert(CurSymbol != NULL && "BeginCOFFSymbolDef must be called first!");
286 CurSymbol = NULL;
Chris Lattnereb72dca2010-07-11 22:05:00 +0000287}
288
Rafael Espindola8f7d12c2011-12-17 01:14:52 +0000289void WinCOFFStreamer::EmitCOFFSecRel32(MCSymbol const *Symbol)
290{
291 MCDataFragment *DF = getOrCreateDataFragment();
292
293 DF->addFixup(MCFixup::Create(DF->getContents().size(),
294 MCSymbolRefExpr::Create (Symbol, getContext ()),
Rafael Espindolace618af2011-12-24 14:47:52 +0000295 FK_SecRel_4));
Rafael Espindola8f7d12c2011-12-17 01:14:52 +0000296 DF->getContents().resize(DF->getContents().size() + 4, 0);
297}
298
Chris Lattnereb72dca2010-07-11 22:05:00 +0000299void WinCOFFStreamer::EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) {
Michael J. Spencer8067adc2010-07-19 06:13:10 +0000300 llvm_unreachable("not implemented");
Chris Lattnereb72dca2010-07-11 22:05:00 +0000301}
302
303void WinCOFFStreamer::EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
Michael J. Spencer8067adc2010-07-19 06:13:10 +0000304 unsigned ByteAlignment) {
Michael J. Spencerd47f4a92010-10-09 11:00:37 +0000305 assert((Symbol->isInSection()
306 ? Symbol->getSection().getVariant() == MCSection::SV_COFF
307 : true) && "Got non COFF section in the COFF backend!");
Michael J. Spencer8067adc2010-07-19 06:13:10 +0000308 AddCommonSymbol(Symbol, Size, ByteAlignment, true);
Chris Lattnereb72dca2010-07-11 22:05:00 +0000309}
310
Benjamin Kramer36a16012011-09-01 23:04:27 +0000311void WinCOFFStreamer::EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size,
312 unsigned ByteAlignment) {
Michael J. Spencerd47f4a92010-10-09 11:00:37 +0000313 assert((Symbol->isInSection()
314 ? Symbol->getSection().getVariant() == MCSection::SV_COFF
315 : true) && "Got non COFF section in the COFF backend!");
Benjamin Kramer36a16012011-09-01 23:04:27 +0000316 AddCommonSymbol(Symbol, Size, ByteAlignment, false);
Chris Lattnereb72dca2010-07-11 22:05:00 +0000317}
318
319void WinCOFFStreamer::EmitZerofill(const MCSection *Section, MCSymbol *Symbol,
Evan Chengc90a1fc2012-06-22 20:14:46 +0000320 uint64_t Size,unsigned ByteAlignment) {
Michael J. Spencer8067adc2010-07-19 06:13:10 +0000321 llvm_unreachable("not implemented");
Chris Lattnereb72dca2010-07-11 22:05:00 +0000322}
323
324void WinCOFFStreamer::EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol,
325 uint64_t Size, unsigned ByteAlignment) {
Michael J. Spencer8067adc2010-07-19 06:13:10 +0000326 llvm_unreachable("not implemented");
Chris Lattnereb72dca2010-07-11 22:05:00 +0000327}
328
Chris Lattnereb72dca2010-07-11 22:05:00 +0000329void WinCOFFStreamer::EmitFileDirective(StringRef Filename) {
330 // Ignore for now, linkers don't care, and proper debug
331 // info will be a much large effort.
332}
333
Chris Lattnereb72dca2010-07-11 22:05:00 +0000334void WinCOFFStreamer::EmitInstruction(const MCInst &Instruction) {
Michael J. Spencer8067adc2010-07-19 06:13:10 +0000335 for (unsigned i = 0, e = Instruction.getNumOperands(); i != e; ++i)
336 if (Instruction.getOperand(i).isExpr())
337 AddValueSymbols(Instruction.getOperand(i).getExpr());
338
339 getCurrentSectionData()->setHasInstructions(true);
340
341 MCInstFragment *Fragment =
342 new MCInstFragment(Instruction, getCurrentSectionData());
343
344 raw_svector_ostream VecOS(Fragment->getCode());
345
346 getAssembler().getEmitter().EncodeInstruction(Instruction, VecOS,
347 Fragment->getFixups());
Chris Lattnereb72dca2010-07-11 22:05:00 +0000348}
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}