blob: 7fc114cbff80c3f31840e6c29c0b7f92ee4af751 [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);
Saleem Abdulrasool1c9cd022013-08-09 01:52:03 +000058 virtual bool EmitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute);
Chris Lattnereb72dca2010-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();
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);
Rafael Espindolac7ce3e42013-10-16 01:05:45 +000075 virtual void EmitIdent(StringRef IdentString);
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
Chandler Carruth5da36652013-01-31 23:29:57 +0000132WinCOFFStreamer::WinCOFFStreamer(MCContext &Context, MCAsmBackend &MAB,
133 MCCodeEmitter &CE, raw_ostream &OS)
Rafael Espindola320296a2013-10-08 13:08:17 +0000134 : MCObjectStreamer(Context, 0, MAB, OS, &CE), CurSymbol(NULL) {}
Michael J. Spencer8067adc2010-07-19 06:13:10 +0000135
136void WinCOFFStreamer::AddCommonSymbol(MCSymbol *Symbol, uint64_t Size,
137 unsigned ByteAlignment, bool External) {
138 assert(!Symbol->isInSection() && "Symbol must not already have a section!");
139
140 std::string SectionName(".bss$linkonce");
141 SectionName.append(Symbol->getName().begin(), Symbol->getName().end());
142
143 MCSymbolData &SymbolData = getAssembler().getOrCreateSymbolData(*Symbol);
144
145 unsigned Characteristics =
146 COFF::IMAGE_SCN_LNK_COMDAT |
147 COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA |
148 COFF::IMAGE_SCN_MEM_READ |
149 COFF::IMAGE_SCN_MEM_WRITE;
150
151 int Selection = COFF::IMAGE_COMDAT_SELECT_LARGEST;
152
153 const MCSection *Section = MCStreamer::getContext().getCOFFSection(
Bill Wendling0ae07092013-11-27 06:44:18 +0000154 SectionName, Characteristics, SectionKind::getBSS(), Symbol->getName(),
155 Selection);
Michael J. Spencer8067adc2010-07-19 06:13:10 +0000156
157 MCSectionData &SectionData = getAssembler().getOrCreateSectionData(*Section);
158
159 if (SectionData.getAlignment() < ByteAlignment)
160 SectionData.setAlignment(ByteAlignment);
161
162 SymbolData.setExternal(External);
163
Richard Mitton5cc319a2013-09-19 23:21:01 +0000164 AssignSection(Symbol, Section);
Michael J. Spencer8067adc2010-07-19 06:13:10 +0000165
166 if (ByteAlignment != 1)
167 new MCAlignFragment(ByteAlignment, 0, 0, ByteAlignment, &SectionData);
168
169 SymbolData.setFragment(new MCFillFragment(0, 0, Size, &SectionData));
Chris Lattnereb72dca2010-07-11 22:05:00 +0000170}
171
172// MCStreamer interface
173
Eli Bendersky030f63a2013-01-14 19:04:57 +0000174void WinCOFFStreamer::InitToTextSection() {
175 SetSectionText();
176}
177
Rafael Espindolad80781b2010-09-15 21:48:40 +0000178void WinCOFFStreamer::InitSections() {
Michael J. Spencer192d1362010-10-09 15:44:27 +0000179 SetSectionText();
180 SetSectionData();
181 SetSectionBSS();
182 SetSectionText();
Rafael Espindolad80781b2010-09-15 21:48:40 +0000183}
184
Rafael Espindolaba210242010-11-28 16:22:59 +0000185void WinCOFFStreamer::EmitLabel(MCSymbol *Symbol) {
Rafael Espindolaba210242010-11-28 16:22:59 +0000186 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
Rafael Espindolaea4afa92010-11-28 17:18:55 +0000187 MCObjectStreamer::EmitLabel(Symbol);
Rafael Espindolaba210242010-11-28 16:22:59 +0000188}
189
Reed Kotler2c3a4642012-12-16 04:00:45 +0000190void WinCOFFStreamer::EmitDebugLabel(MCSymbol *Symbol) {
191 EmitLabel(Symbol);
192}
Chris Lattnereb72dca2010-07-11 22:05:00 +0000193void WinCOFFStreamer::EmitAssemblerFlag(MCAssemblerFlag Flag) {
Michael J. Spencer8067adc2010-07-19 06:13:10 +0000194 llvm_unreachable("not implemented");
Chris Lattnereb72dca2010-07-11 22:05:00 +0000195}
196
Jim Grosbachce792992010-11-05 22:08:08 +0000197void WinCOFFStreamer::EmitThumbFunc(MCSymbol *Func) {
198 llvm_unreachable("not implemented");
199}
200
Saleem Abdulrasool1c9cd022013-08-09 01:52:03 +0000201bool WinCOFFStreamer::EmitSymbolAttribute(MCSymbol *Symbol,
Chris Lattnereb72dca2010-07-11 22:05:00 +0000202 MCSymbolAttr Attribute) {
Michael J. Spencerd47f4a92010-10-09 11:00:37 +0000203 assert(Symbol && "Symbol must be non-null!");
204 assert((Symbol->isInSection()
205 ? Symbol->getSection().getVariant() == MCSection::SV_COFF
206 : true) && "Got non COFF section in the COFF backend!");
Michael J. Spencer8067adc2010-07-19 06:13:10 +0000207 switch (Attribute) {
208 case MCSA_WeakReference:
Michael J. Spencer4cee2892010-10-16 08:25:57 +0000209 case MCSA_Weak: {
210 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
211 SD.modifyFlags(COFF::SF_WeakExternal, COFF::SF_WeakExternal);
212 SD.setExternal(true);
213 }
Michael J. Spencer8067adc2010-07-19 06:13:10 +0000214 break;
215
216 case MCSA_Global:
217 getAssembler().getOrCreateSymbolData(*Symbol).setExternal(true);
218 break;
219
220 default:
Saleem Abdulrasool1c9cd022013-08-09 01:52:03 +0000221 return false;
Michael J. Spencer8067adc2010-07-19 06:13:10 +0000222 }
Saleem Abdulrasool1c9cd022013-08-09 01:52:03 +0000223
224 return true;
Chris Lattnereb72dca2010-07-11 22:05:00 +0000225}
226
227void WinCOFFStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
Michael J. Spencer8067adc2010-07-19 06:13:10 +0000228 llvm_unreachable("not implemented");
Chris Lattnereb72dca2010-07-11 22:05:00 +0000229}
230
231void WinCOFFStreamer::BeginCOFFSymbolDef(MCSymbol const *Symbol) {
Michael J. Spencerd47f4a92010-10-09 11:00:37 +0000232 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 assert(CurSymbol == NULL && "EndCOFFSymbolDef must be called between calls "
236 "to BeginCOFFSymbolDef!");
237 CurSymbol = Symbol;
Chris Lattnereb72dca2010-07-11 22:05:00 +0000238}
239
240void WinCOFFStreamer::EmitCOFFSymbolStorageClass(int StorageClass) {
Michael J. Spencer8067adc2010-07-19 06:13:10 +0000241 assert(CurSymbol != NULL && "BeginCOFFSymbolDef must be called first!");
242 assert((StorageClass & ~0xFF) == 0 && "StorageClass must only have data in "
243 "the first byte!");
244
245 getAssembler().getOrCreateSymbolData(*CurSymbol).modifyFlags(
246 StorageClass << COFF::SF_ClassShift,
247 COFF::SF_ClassMask);
Chris Lattnereb72dca2010-07-11 22:05:00 +0000248}
249
250void WinCOFFStreamer::EmitCOFFSymbolType(int Type) {
Michael J. Spencer8067adc2010-07-19 06:13:10 +0000251 assert(CurSymbol != NULL && "BeginCOFFSymbolDef must be called first!");
252 assert((Type & ~0xFFFF) == 0 && "Type must only have data in the first 2 "
253 "bytes");
254
255 getAssembler().getOrCreateSymbolData(*CurSymbol).modifyFlags(
256 Type << COFF::SF_TypeShift,
257 COFF::SF_TypeMask);
Chris Lattnereb72dca2010-07-11 22:05:00 +0000258}
259
260void WinCOFFStreamer::EndCOFFSymbolDef() {
Michael J. Spencer8067adc2010-07-19 06:13:10 +0000261 assert(CurSymbol != NULL && "BeginCOFFSymbolDef must be called first!");
262 CurSymbol = NULL;
Chris Lattnereb72dca2010-07-11 22:05:00 +0000263}
264
Rafael Espindola8f7d12c2011-12-17 01:14:52 +0000265void WinCOFFStreamer::EmitCOFFSecRel32(MCSymbol const *Symbol)
266{
267 MCDataFragment *DF = getOrCreateDataFragment();
268
Eli Bendersky64d9a322012-12-07 19:13:57 +0000269 DF->getFixups().push_back(
270 MCFixup::Create(DF->getContents().size(),
271 MCSymbolRefExpr::Create (Symbol, getContext ()),
272 FK_SecRel_4));
Rafael Espindola8f7d12c2011-12-17 01:14:52 +0000273 DF->getContents().resize(DF->getContents().size() + 4, 0);
274}
275
Chris Lattnereb72dca2010-07-11 22:05:00 +0000276void WinCOFFStreamer::EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) {
Michael J. Spencer8067adc2010-07-19 06:13:10 +0000277 llvm_unreachable("not implemented");
Chris Lattnereb72dca2010-07-11 22:05:00 +0000278}
279
280void WinCOFFStreamer::EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
Michael J. Spencer8067adc2010-07-19 06:13:10 +0000281 unsigned ByteAlignment) {
Michael J. Spencerd47f4a92010-10-09 11:00:37 +0000282 assert((Symbol->isInSection()
283 ? Symbol->getSection().getVariant() == MCSection::SV_COFF
284 : true) && "Got non COFF section in the COFF backend!");
Michael J. Spencer8067adc2010-07-19 06:13:10 +0000285 AddCommonSymbol(Symbol, Size, ByteAlignment, true);
Chris Lattnereb72dca2010-07-11 22:05:00 +0000286}
287
Benjamin Kramer36a16012011-09-01 23:04:27 +0000288void WinCOFFStreamer::EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size,
289 unsigned ByteAlignment) {
Michael J. Spencerd47f4a92010-10-09 11:00:37 +0000290 assert((Symbol->isInSection()
291 ? Symbol->getSection().getVariant() == MCSection::SV_COFF
292 : true) && "Got non COFF section in the COFF backend!");
Benjamin Kramer36a16012011-09-01 23:04:27 +0000293 AddCommonSymbol(Symbol, Size, ByteAlignment, false);
Chris Lattnereb72dca2010-07-11 22:05:00 +0000294}
295
296void WinCOFFStreamer::EmitZerofill(const MCSection *Section, MCSymbol *Symbol,
Evan Chengc90a1fc2012-06-22 20:14:46 +0000297 uint64_t Size,unsigned ByteAlignment) {
Michael J. Spencer8067adc2010-07-19 06:13:10 +0000298 llvm_unreachable("not implemented");
Chris Lattnereb72dca2010-07-11 22:05:00 +0000299}
300
301void WinCOFFStreamer::EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol,
302 uint64_t Size, unsigned ByteAlignment) {
Michael J. Spencer8067adc2010-07-19 06:13:10 +0000303 llvm_unreachable("not implemented");
Chris Lattnereb72dca2010-07-11 22:05:00 +0000304}
305
Chris Lattnereb72dca2010-07-11 22:05:00 +0000306void WinCOFFStreamer::EmitFileDirective(StringRef Filename) {
307 // Ignore for now, linkers don't care, and proper debug
308 // info will be a much large effort.
309}
310
Rafael Espindolac7ce3e42013-10-16 01:05:45 +0000311// TODO: Implement this if you want to emit .comment section in COFF obj files.
312void WinCOFFStreamer::EmitIdent(StringRef IdentString) {
313 llvm_unreachable("unsupported directive");
314}
315
Charles Davis3185f5c2011-05-22 03:01:05 +0000316void WinCOFFStreamer::EmitWin64EHHandlerData() {
317 MCStreamer::EmitWin64EHHandlerData();
318
319 // We have to emit the unwind info now, because this directive
320 // actually switches to the .xdata section!
321 MCWin64EHUnwindEmitter::EmitUnwindInfo(*this, getCurrentW64UnwindInfo());
322}
323
Rafael Espindola99b42372012-01-07 03:13:18 +0000324void WinCOFFStreamer::FinishImpl() {
Charles Davis38ea9ee2011-05-22 04:15:07 +0000325 EmitW64Tables();
Rafael Espindola99b42372012-01-07 03:13:18 +0000326 MCObjectStreamer::FinishImpl();
Chris Lattnereb72dca2010-07-11 22:05:00 +0000327}
328
329namespace llvm
330{
331 MCStreamer *createWinCOFFStreamer(MCContext &Context,
Evan Cheng78c10ee2011-07-25 23:24:55 +0000332 MCAsmBackend &MAB,
Chris Lattnereb72dca2010-07-11 22:05:00 +0000333 MCCodeEmitter &CE,
Michael J. Spencere2195d82010-07-31 06:22:29 +0000334 raw_ostream &OS,
335 bool RelaxAll) {
Evan Cheng78c10ee2011-07-25 23:24:55 +0000336 WinCOFFStreamer *S = new WinCOFFStreamer(Context, MAB, CE, OS);
Michael J. Spencere2195d82010-07-31 06:22:29 +0000337 S->getAssembler().setRelaxAll(RelaxAll);
338 return S;
Chris Lattnereb72dca2010-07-11 22:05:00 +0000339 }
340}