blob: d12201a6ca697c6ca35a0d72c41e5066960824ec [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);
Reed Kotler2c3a4642012-12-16 04:00:45 +000054 virtual void EmitDebugLabel(MCSymbol *Symbol);
Chris Lattnereb72dca2010-07-11 22:05:00 +000055 virtual void EmitAssemblerFlag(MCAssemblerFlag Flag);
Jim Grosbachce792992010-11-05 22:08:08 +000056 virtual void EmitThumbFunc(MCSymbol *Func);
Chris Lattnereb72dca2010-07-11 22:05:00 +000057 virtual void EmitAssignment(MCSymbol *Symbol, const MCExpr *Value);
58 virtual void EmitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute);
59 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();
Rafael Espindola8f7d12c2011-12-17 01:14:52 +000064 virtual void EmitCOFFSecRel32(MCSymbol const *Symbol);
Chris Lattnereb72dca2010-07-11 22:05:00 +000065 virtual void EmitELFSize(MCSymbol *Symbol, const MCExpr *Value);
66 virtual void EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
Michael J. Spencer8067adc2010-07-19 06:13:10 +000067 unsigned ByteAlignment);
Benjamin Kramer36a16012011-09-01 23:04:27 +000068 virtual void EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size,
69 unsigned ByteAlignment);
Chris Lattnereb72dca2010-07-11 22:05:00 +000070 virtual void EmitZerofill(const MCSection *Section, MCSymbol *Symbol,
Evan Chengc90a1fc2012-06-22 20:14:46 +000071 uint64_t Size,unsigned ByteAlignment);
Chris Lattnereb72dca2010-07-11 22:05:00 +000072 virtual void EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol,
Michael J. Spencer8067adc2010-07-19 06:13:10 +000073 uint64_t Size, unsigned ByteAlignment);
Chris Lattnereb72dca2010-07-11 22:05:00 +000074 virtual void EmitFileDirective(StringRef Filename);
Chris Lattnereb72dca2010-07-11 22:05:00 +000075 virtual void EmitInstruction(const MCInst &Instruction);
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 EmitInstToFragment(const MCInst &Inst) {
81 llvm_unreachable("Not used by WinCOFF.");
82 }
83 virtual void EmitInstToData(const MCInst &Inst) {
84 llvm_unreachable("Not used by WinCOFF.");
85 }
86
Michael J. Spencer192d1362010-10-09 15:44:27 +000087 void SetSection(StringRef Section,
88 unsigned Characteristics,
89 SectionKind Kind) {
90 SwitchSection(getContext().getCOFFSection(Section, Characteristics, Kind));
91 }
92
93 void SetSectionText() {
94 SetSection(".text",
95 COFF::IMAGE_SCN_CNT_CODE
96 | COFF::IMAGE_SCN_MEM_EXECUTE
97 | COFF::IMAGE_SCN_MEM_READ,
98 SectionKind::getText());
99 EmitCodeAlignment(4, 0);
100 }
101
102 void SetSectionData() {
103 SetSection(".data",
104 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA
105 | COFF::IMAGE_SCN_MEM_READ
106 | COFF::IMAGE_SCN_MEM_WRITE,
107 SectionKind::getDataRel());
108 EmitCodeAlignment(4, 0);
109 }
110
111 void SetSectionBSS() {
112 SetSection(".bss",
113 COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA
114 | COFF::IMAGE_SCN_MEM_READ
115 | COFF::IMAGE_SCN_MEM_WRITE,
116 SectionKind::getBSS());
117 EmitCodeAlignment(4, 0);
118 }
119
Chris Lattnereb72dca2010-07-11 22:05:00 +0000120};
121} // end anonymous namespace.
122
123WinCOFFStreamer::WinCOFFStreamer(MCContext &Context,
Evan Cheng78c10ee2011-07-25 23:24:55 +0000124 MCAsmBackend &MAB,
Chris Lattnereb72dca2010-07-11 22:05:00 +0000125 MCCodeEmitter &CE,
126 raw_ostream &OS)
Evan Cheng78c10ee2011-07-25 23:24:55 +0000127 : MCObjectStreamer(Context, MAB, OS, &CE)
Michael J. Spencer8067adc2010-07-19 06:13:10 +0000128 , CurSymbol(NULL) {
129}
130
131void WinCOFFStreamer::AddCommonSymbol(MCSymbol *Symbol, uint64_t Size,
132 unsigned ByteAlignment, bool External) {
133 assert(!Symbol->isInSection() && "Symbol must not already have a section!");
134
135 std::string SectionName(".bss$linkonce");
136 SectionName.append(Symbol->getName().begin(), Symbol->getName().end());
137
138 MCSymbolData &SymbolData = getAssembler().getOrCreateSymbolData(*Symbol);
139
140 unsigned Characteristics =
141 COFF::IMAGE_SCN_LNK_COMDAT |
142 COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA |
143 COFF::IMAGE_SCN_MEM_READ |
144 COFF::IMAGE_SCN_MEM_WRITE;
145
146 int Selection = COFF::IMAGE_COMDAT_SELECT_LARGEST;
147
148 const MCSection *Section = MCStreamer::getContext().getCOFFSection(
149 SectionName, Characteristics, Selection, SectionKind::getBSS());
150
151 MCSectionData &SectionData = getAssembler().getOrCreateSectionData(*Section);
152
153 if (SectionData.getAlignment() < ByteAlignment)
154 SectionData.setAlignment(ByteAlignment);
155
156 SymbolData.setExternal(External);
157
158 Symbol->setSection(*Section);
159
160 if (ByteAlignment != 1)
161 new MCAlignFragment(ByteAlignment, 0, 0, ByteAlignment, &SectionData);
162
163 SymbolData.setFragment(new MCFillFragment(0, 0, Size, &SectionData));
Chris Lattnereb72dca2010-07-11 22:05:00 +0000164}
165
166// MCStreamer interface
167
Rafael Espindolad80781b2010-09-15 21:48:40 +0000168void WinCOFFStreamer::InitSections() {
Michael J. Spencer192d1362010-10-09 15:44:27 +0000169 SetSectionText();
170 SetSectionData();
171 SetSectionBSS();
172 SetSectionText();
Rafael Espindolad80781b2010-09-15 21:48:40 +0000173}
174
Rafael Espindolaba210242010-11-28 16:22:59 +0000175void WinCOFFStreamer::EmitLabel(MCSymbol *Symbol) {
Rafael Espindolaba210242010-11-28 16:22:59 +0000176 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
Rafael Espindolaea4afa92010-11-28 17:18:55 +0000177 MCObjectStreamer::EmitLabel(Symbol);
Rafael Espindolaba210242010-11-28 16:22:59 +0000178}
179
Reed Kotler2c3a4642012-12-16 04:00:45 +0000180void WinCOFFStreamer::EmitDebugLabel(MCSymbol *Symbol) {
181 EmitLabel(Symbol);
182}
Chris Lattnereb72dca2010-07-11 22:05:00 +0000183void WinCOFFStreamer::EmitAssemblerFlag(MCAssemblerFlag Flag) {
Michael J. Spencer8067adc2010-07-19 06:13:10 +0000184 llvm_unreachable("not implemented");
Chris Lattnereb72dca2010-07-11 22:05:00 +0000185}
186
Jim Grosbachce792992010-11-05 22:08:08 +0000187void WinCOFFStreamer::EmitThumbFunc(MCSymbol *Func) {
188 llvm_unreachable("not implemented");
189}
190
Chris Lattnereb72dca2010-07-11 22:05:00 +0000191void WinCOFFStreamer::EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) {
Michael J. Spencerd47f4a92010-10-09 11:00:37 +0000192 assert((Symbol->isInSection()
193 ? Symbol->getSection().getVariant() == MCSection::SV_COFF
194 : true) && "Got non COFF section in the COFF backend!");
Michael J. Spencerb5814a32010-10-07 06:29:33 +0000195 // FIXME: This is all very ugly and depressing. What needs to happen here
196 // depends on quite a few things that are all part of relaxation, which we
197 // don't really even do.
198
199 if (Value->getKind() != MCExpr::SymbolRef) {
Eli Benderskycd81dce2012-12-07 17:55:28 +0000200 MCObjectStreamer::EmitAssignment(Symbol, Value);
Michael J. Spencerb5814a32010-10-07 06:29:33 +0000201 } else {
202 // FIXME: This is a horrible way to do this :(. This should really be
203 // handled after we are done with the MC* objects and immediately before
204 // writing out the object file when we know exactly what the symbol should
205 // look like in the coff symbol table. I'm not doing that now because the
206 // COFF object writer doesn't have a clearly defined separation between MC
207 // data structures, the object writers data structures, and the raw, POD,
208 // data structures that get written to disk.
209
210 // Copy over the aliased data.
211 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
212 const MCSymbolData &RealSD = getAssembler().getOrCreateSymbolData(
213 dyn_cast<const MCSymbolRefExpr>(Value)->getSymbol());
214
215 // FIXME: This is particularly nasty because it breaks as soon as any data
216 // members of MCSymbolData change.
217 SD.CommonAlign = RealSD.CommonAlign;
218 SD.CommonSize = RealSD.CommonSize;
219 SD.Flags = RealSD.Flags;
220 SD.Fragment = RealSD.Fragment;
221 SD.Index = RealSD.Index;
222 SD.IsExternal = RealSD.IsExternal;
223 SD.IsPrivateExtern = RealSD.IsPrivateExtern;
224 SD.Offset = RealSD.Offset;
225 SD.SymbolSize = RealSD.SymbolSize;
226 }
Chris Lattnereb72dca2010-07-11 22:05:00 +0000227}
228
229void WinCOFFStreamer::EmitSymbolAttribute(MCSymbol *Symbol,
230 MCSymbolAttr Attribute) {
Michael J. Spencerd47f4a92010-10-09 11:00:37 +0000231 assert(Symbol && "Symbol must be non-null!");
232 assert((Symbol->isInSection()
233 ? Symbol->getSection().getVariant() == MCSection::SV_COFF
234 : true) && "Got non COFF section in the COFF backend!");
Michael J. Spencer8067adc2010-07-19 06:13:10 +0000235 switch (Attribute) {
236 case MCSA_WeakReference:
Michael J. Spencer4cee2892010-10-16 08:25:57 +0000237 case MCSA_Weak: {
238 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
239 SD.modifyFlags(COFF::SF_WeakExternal, COFF::SF_WeakExternal);
240 SD.setExternal(true);
241 }
Michael J. Spencer8067adc2010-07-19 06:13:10 +0000242 break;
243
244 case MCSA_Global:
245 getAssembler().getOrCreateSymbolData(*Symbol).setExternal(true);
246 break;
247
248 default:
249 llvm_unreachable("unsupported attribute");
Michael J. Spencer8067adc2010-07-19 06:13:10 +0000250 }
Chris Lattnereb72dca2010-07-11 22:05:00 +0000251}
252
253void WinCOFFStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
Michael J. Spencer8067adc2010-07-19 06:13:10 +0000254 llvm_unreachable("not implemented");
Chris Lattnereb72dca2010-07-11 22:05:00 +0000255}
256
257void WinCOFFStreamer::BeginCOFFSymbolDef(MCSymbol const *Symbol) {
Michael J. Spencerd47f4a92010-10-09 11:00:37 +0000258 assert((Symbol->isInSection()
259 ? Symbol->getSection().getVariant() == MCSection::SV_COFF
260 : true) && "Got non COFF section in the COFF backend!");
Michael J. Spencer8067adc2010-07-19 06:13:10 +0000261 assert(CurSymbol == NULL && "EndCOFFSymbolDef must be called between calls "
262 "to BeginCOFFSymbolDef!");
263 CurSymbol = Symbol;
Chris Lattnereb72dca2010-07-11 22:05:00 +0000264}
265
266void WinCOFFStreamer::EmitCOFFSymbolStorageClass(int StorageClass) {
Michael J. Spencer8067adc2010-07-19 06:13:10 +0000267 assert(CurSymbol != NULL && "BeginCOFFSymbolDef must be called first!");
268 assert((StorageClass & ~0xFF) == 0 && "StorageClass must only have data in "
269 "the first byte!");
270
271 getAssembler().getOrCreateSymbolData(*CurSymbol).modifyFlags(
272 StorageClass << COFF::SF_ClassShift,
273 COFF::SF_ClassMask);
Chris Lattnereb72dca2010-07-11 22:05:00 +0000274}
275
276void WinCOFFStreamer::EmitCOFFSymbolType(int Type) {
Michael J. Spencer8067adc2010-07-19 06:13:10 +0000277 assert(CurSymbol != NULL && "BeginCOFFSymbolDef must be called first!");
278 assert((Type & ~0xFFFF) == 0 && "Type must only have data in the first 2 "
279 "bytes");
280
281 getAssembler().getOrCreateSymbolData(*CurSymbol).modifyFlags(
282 Type << COFF::SF_TypeShift,
283 COFF::SF_TypeMask);
Chris Lattnereb72dca2010-07-11 22:05:00 +0000284}
285
286void WinCOFFStreamer::EndCOFFSymbolDef() {
Michael J. Spencer8067adc2010-07-19 06:13:10 +0000287 assert(CurSymbol != NULL && "BeginCOFFSymbolDef must be called first!");
288 CurSymbol = NULL;
Chris Lattnereb72dca2010-07-11 22:05:00 +0000289}
290
Rafael Espindola8f7d12c2011-12-17 01:14:52 +0000291void WinCOFFStreamer::EmitCOFFSecRel32(MCSymbol const *Symbol)
292{
293 MCDataFragment *DF = getOrCreateDataFragment();
294
Eli Bendersky64d9a322012-12-07 19:13:57 +0000295 DF->getFixups().push_back(
296 MCFixup::Create(DF->getContents().size(),
297 MCSymbolRefExpr::Create (Symbol, getContext ()),
298 FK_SecRel_4));
Rafael Espindola8f7d12c2011-12-17 01:14:52 +0000299 DF->getContents().resize(DF->getContents().size() + 4, 0);
300}
301
Chris Lattnereb72dca2010-07-11 22:05:00 +0000302void WinCOFFStreamer::EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) {
Michael J. Spencer8067adc2010-07-19 06:13:10 +0000303 llvm_unreachable("not implemented");
Chris Lattnereb72dca2010-07-11 22:05:00 +0000304}
305
306void WinCOFFStreamer::EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
Michael J. Spencer8067adc2010-07-19 06:13:10 +0000307 unsigned ByteAlignment) {
Michael J. Spencerd47f4a92010-10-09 11:00:37 +0000308 assert((Symbol->isInSection()
309 ? Symbol->getSection().getVariant() == MCSection::SV_COFF
310 : true) && "Got non COFF section in the COFF backend!");
Michael J. Spencer8067adc2010-07-19 06:13:10 +0000311 AddCommonSymbol(Symbol, Size, ByteAlignment, true);
Chris Lattnereb72dca2010-07-11 22:05:00 +0000312}
313
Benjamin Kramer36a16012011-09-01 23:04:27 +0000314void WinCOFFStreamer::EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size,
315 unsigned ByteAlignment) {
Michael J. Spencerd47f4a92010-10-09 11:00:37 +0000316 assert((Symbol->isInSection()
317 ? Symbol->getSection().getVariant() == MCSection::SV_COFF
318 : true) && "Got non COFF section in the COFF backend!");
Benjamin Kramer36a16012011-09-01 23:04:27 +0000319 AddCommonSymbol(Symbol, Size, ByteAlignment, false);
Chris Lattnereb72dca2010-07-11 22:05:00 +0000320}
321
322void WinCOFFStreamer::EmitZerofill(const MCSection *Section, MCSymbol *Symbol,
Evan Chengc90a1fc2012-06-22 20:14:46 +0000323 uint64_t Size,unsigned ByteAlignment) {
Michael J. Spencer8067adc2010-07-19 06:13:10 +0000324 llvm_unreachable("not implemented");
Chris Lattnereb72dca2010-07-11 22:05:00 +0000325}
326
327void WinCOFFStreamer::EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol,
328 uint64_t Size, unsigned ByteAlignment) {
Michael J. Spencer8067adc2010-07-19 06:13:10 +0000329 llvm_unreachable("not implemented");
Chris Lattnereb72dca2010-07-11 22:05:00 +0000330}
331
Chris Lattnereb72dca2010-07-11 22:05:00 +0000332void WinCOFFStreamer::EmitFileDirective(StringRef Filename) {
333 // Ignore for now, linkers don't care, and proper debug
334 // info will be a much large effort.
335}
336
Chris Lattnereb72dca2010-07-11 22:05:00 +0000337void WinCOFFStreamer::EmitInstruction(const MCInst &Instruction) {
Michael J. Spencer8067adc2010-07-19 06:13:10 +0000338 for (unsigned i = 0, e = Instruction.getNumOperands(); i != e; ++i)
339 if (Instruction.getOperand(i).isExpr())
340 AddValueSymbols(Instruction.getOperand(i).getExpr());
341
342 getCurrentSectionData()->setHasInstructions(true);
343
344 MCInstFragment *Fragment =
345 new MCInstFragment(Instruction, getCurrentSectionData());
346
Eli Bendersky64d9a322012-12-07 19:13:57 +0000347 raw_svector_ostream VecOS(Fragment->getContents());
Michael J. Spencer8067adc2010-07-19 06:13:10 +0000348
349 getAssembler().getEmitter().EncodeInstruction(Instruction, VecOS,
350 Fragment->getFixups());
Chris Lattnereb72dca2010-07-11 22:05:00 +0000351}
352
Charles Davis3185f5c2011-05-22 03:01:05 +0000353void WinCOFFStreamer::EmitWin64EHHandlerData() {
354 MCStreamer::EmitWin64EHHandlerData();
355
356 // We have to emit the unwind info now, because this directive
357 // actually switches to the .xdata section!
358 MCWin64EHUnwindEmitter::EmitUnwindInfo(*this, getCurrentW64UnwindInfo());
359}
360
Rafael Espindola99b42372012-01-07 03:13:18 +0000361void WinCOFFStreamer::FinishImpl() {
Charles Davis38ea9ee2011-05-22 04:15:07 +0000362 EmitW64Tables();
Rafael Espindola99b42372012-01-07 03:13:18 +0000363 MCObjectStreamer::FinishImpl();
Chris Lattnereb72dca2010-07-11 22:05:00 +0000364}
365
366namespace llvm
367{
368 MCStreamer *createWinCOFFStreamer(MCContext &Context,
Evan Cheng78c10ee2011-07-25 23:24:55 +0000369 MCAsmBackend &MAB,
Chris Lattnereb72dca2010-07-11 22:05:00 +0000370 MCCodeEmitter &CE,
Michael J. Spencere2195d82010-07-31 06:22:29 +0000371 raw_ostream &OS,
372 bool RelaxAll) {
Evan Cheng78c10ee2011-07-25 23:24:55 +0000373 WinCOFFStreamer *S = new WinCOFFStreamer(Context, MAB, CE, OS);
Michael J. Spencere2195d82010-07-31 06:22:29 +0000374 S->getAssembler().setRelaxAll(RelaxAll);
375 return S;
Chris Lattnereb72dca2010-07-11 22:05:00 +0000376 }
377}