blob: 3654add4f405b699aefca96c0d7af47e50e75a69 [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
16#include "llvm/MC/MCObjectStreamer.h"
17#include "llvm/MC/MCContext.h"
18#include "llvm/MC/MCSection.h"
19#include "llvm/MC/MCSymbol.h"
20#include "llvm/MC/MCExpr.h"
Michael J. Spencer8067adc2010-07-19 06:13:10 +000021#include "llvm/MC/MCValue.h"
22#include "llvm/MC/MCAssembler.h"
23#include "llvm/MC/MCAsmLayout.h"
Chris Lattnereb72dca2010-07-11 22:05:00 +000024#include "llvm/MC/MCCodeEmitter.h"
25#include "llvm/MC/MCSectionCOFF.h"
Michael J. Spencer8067adc2010-07-19 06:13:10 +000026#include "llvm/Target/TargetRegistry.h"
Chris Lattnereb72dca2010-07-11 22:05:00 +000027#include "llvm/Target/TargetAsmBackend.h"
Michael J. Spencer8067adc2010-07-19 06:13:10 +000028#include "llvm/ADT/StringMap.h"
29
Chris Lattnereb72dca2010-07-11 22:05:00 +000030#include "llvm/Support/COFF.h"
31#include "llvm/Support/Debug.h"
32#include "llvm/Support/ErrorHandling.h"
33#include "llvm/Support/raw_ostream.h"
34using namespace llvm;
35
Chris Lattnereb72dca2010-07-11 22:05:00 +000036namespace {
37class WinCOFFStreamer : public MCObjectStreamer {
38public:
Michael J. Spencer8067adc2010-07-19 06:13:10 +000039 MCSymbol const *CurSymbol;
40
Chris Lattnereb72dca2010-07-11 22:05:00 +000041 WinCOFFStreamer(MCContext &Context,
42 TargetAsmBackend &TAB,
43 MCCodeEmitter &CE,
44 raw_ostream &OS);
45
Michael J. Spencer8067adc2010-07-19 06:13:10 +000046 void AddCommonSymbol(MCSymbol *Symbol, uint64_t Size,
47 unsigned ByteAlignment, bool External);
48
Chris Lattnereb72dca2010-07-11 22:05:00 +000049 // MCStreamer interface
50
Rafael Espindolad80781b2010-09-15 21:48:40 +000051 virtual void InitSections();
Rafael Espindolaba210242010-11-28 16:22:59 +000052 virtual void EmitLabel(MCSymbol *Symbol);
Chris Lattnereb72dca2010-07-11 22:05:00 +000053 virtual void EmitAssemblerFlag(MCAssemblerFlag Flag);
Jim Grosbachce792992010-11-05 22:08:08 +000054 virtual void EmitThumbFunc(MCSymbol *Func);
Chris Lattnereb72dca2010-07-11 22:05:00 +000055 virtual void EmitAssignment(MCSymbol *Symbol, const MCExpr *Value);
56 virtual void EmitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute);
57 virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue);
58 virtual void BeginCOFFSymbolDef(MCSymbol const *Symbol);
59 virtual void EmitCOFFSymbolStorageClass(int StorageClass);
60 virtual void EmitCOFFSymbolType(int Type);
61 virtual void EndCOFFSymbolDef();
62 virtual void EmitELFSize(MCSymbol *Symbol, const MCExpr *Value);
63 virtual void EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
Michael J. Spencer8067adc2010-07-19 06:13:10 +000064 unsigned ByteAlignment);
Chris Lattnereb72dca2010-07-11 22:05:00 +000065 virtual void EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size);
66 virtual void EmitZerofill(const MCSection *Section, MCSymbol *Symbol,
Michael J. Spencer8067adc2010-07-19 06:13:10 +000067 unsigned Size,unsigned ByteAlignment);
Chris Lattnereb72dca2010-07-11 22:05:00 +000068 virtual void EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol,
Michael J. Spencer8067adc2010-07-19 06:13:10 +000069 uint64_t Size, unsigned ByteAlignment);
Chris Lattnereb72dca2010-07-11 22:05:00 +000070 virtual void EmitBytes(StringRef Data, unsigned AddrSpace);
Chris Lattnereb72dca2010-07-11 22:05:00 +000071 virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value,
Michael J. Spencer8067adc2010-07-19 06:13:10 +000072 unsigned ValueSize, unsigned MaxBytesToEmit);
Chris Lattnereb72dca2010-07-11 22:05:00 +000073 virtual void EmitCodeAlignment(unsigned ByteAlignment,
74 unsigned MaxBytesToEmit);
Chris Lattnereb72dca2010-07-11 22:05:00 +000075 virtual void EmitFileDirective(StringRef Filename);
Chris Lattnereb72dca2010-07-11 22:05:00 +000076 virtual void EmitInstruction(const MCInst &Instruction);
77 virtual void Finish();
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,
124 TargetAsmBackend &TAB,
125 MCCodeEmitter &CE,
126 raw_ostream &OS)
Rafael Espindola340a7a12010-12-06 03:03:44 +0000127 : MCObjectStreamer(Context, TAB, OS, &CE, false)
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
Chris Lattnereb72dca2010-07-11 22:05:00 +0000180void WinCOFFStreamer::EmitAssemblerFlag(MCAssemblerFlag Flag) {
Michael J. Spencer8067adc2010-07-19 06:13:10 +0000181 llvm_unreachable("not implemented");
Chris Lattnereb72dca2010-07-11 22:05:00 +0000182}
183
Jim Grosbachce792992010-11-05 22:08:08 +0000184void WinCOFFStreamer::EmitThumbFunc(MCSymbol *Func) {
185 llvm_unreachable("not implemented");
186}
187
Chris Lattnereb72dca2010-07-11 22:05:00 +0000188void WinCOFFStreamer::EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) {
Michael J. Spencerd47f4a92010-10-09 11:00:37 +0000189 assert((Symbol->isInSection()
190 ? Symbol->getSection().getVariant() == MCSection::SV_COFF
191 : true) && "Got non COFF section in the COFF backend!");
Michael J. Spencerb5814a32010-10-07 06:29:33 +0000192 // FIXME: This is all very ugly and depressing. What needs to happen here
193 // depends on quite a few things that are all part of relaxation, which we
194 // don't really even do.
195
196 if (Value->getKind() != MCExpr::SymbolRef) {
NAKAMURA Takumi86c36472010-10-07 07:21:04 +0000197 // TODO: This is exactly the same as MachOStreamer. Consider merging into
198 // MCObjectStreamer.
199 getAssembler().getOrCreateSymbolData(*Symbol);
200 AddValueSymbols(Value);
Michael J. Spencerb5814a32010-10-07 06:29:33 +0000201 Symbol->setVariableValue(Value);
202 } else {
203 // FIXME: This is a horrible way to do this :(. This should really be
204 // handled after we are done with the MC* objects and immediately before
205 // writing out the object file when we know exactly what the symbol should
206 // look like in the coff symbol table. I'm not doing that now because the
207 // COFF object writer doesn't have a clearly defined separation between MC
208 // data structures, the object writers data structures, and the raw, POD,
209 // data structures that get written to disk.
210
211 // Copy over the aliased data.
212 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
213 const MCSymbolData &RealSD = getAssembler().getOrCreateSymbolData(
214 dyn_cast<const MCSymbolRefExpr>(Value)->getSymbol());
215
216 // FIXME: This is particularly nasty because it breaks as soon as any data
217 // members of MCSymbolData change.
218 SD.CommonAlign = RealSD.CommonAlign;
219 SD.CommonSize = RealSD.CommonSize;
220 SD.Flags = RealSD.Flags;
221 SD.Fragment = RealSD.Fragment;
222 SD.Index = RealSD.Index;
223 SD.IsExternal = RealSD.IsExternal;
224 SD.IsPrivateExtern = RealSD.IsPrivateExtern;
225 SD.Offset = RealSD.Offset;
226 SD.SymbolSize = RealSD.SymbolSize;
227 }
Chris Lattnereb72dca2010-07-11 22:05:00 +0000228}
229
230void WinCOFFStreamer::EmitSymbolAttribute(MCSymbol *Symbol,
231 MCSymbolAttr Attribute) {
Michael J. Spencerd47f4a92010-10-09 11:00:37 +0000232 assert(Symbol && "Symbol must be non-null!");
233 assert((Symbol->isInSection()
234 ? Symbol->getSection().getVariant() == MCSection::SV_COFF
235 : true) && "Got non COFF section in the COFF backend!");
Michael J. Spencer8067adc2010-07-19 06:13:10 +0000236 switch (Attribute) {
237 case MCSA_WeakReference:
Michael J. Spencer4cee2892010-10-16 08:25:57 +0000238 case MCSA_Weak: {
239 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
240 SD.modifyFlags(COFF::SF_WeakExternal, COFF::SF_WeakExternal);
241 SD.setExternal(true);
242 }
Michael J. Spencer8067adc2010-07-19 06:13:10 +0000243 break;
244
245 case MCSA_Global:
246 getAssembler().getOrCreateSymbolData(*Symbol).setExternal(true);
247 break;
248
249 default:
250 llvm_unreachable("unsupported attribute");
251 break;
252 }
Chris Lattnereb72dca2010-07-11 22:05:00 +0000253}
254
255void WinCOFFStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
Michael J. Spencer8067adc2010-07-19 06:13:10 +0000256 llvm_unreachable("not implemented");
Chris Lattnereb72dca2010-07-11 22:05:00 +0000257}
258
259void WinCOFFStreamer::BeginCOFFSymbolDef(MCSymbol const *Symbol) {
Michael J. Spencerd47f4a92010-10-09 11:00:37 +0000260 assert((Symbol->isInSection()
261 ? Symbol->getSection().getVariant() == MCSection::SV_COFF
262 : true) && "Got non COFF section in the COFF backend!");
Michael J. Spencer8067adc2010-07-19 06:13:10 +0000263 assert(CurSymbol == NULL && "EndCOFFSymbolDef must be called between calls "
264 "to BeginCOFFSymbolDef!");
265 CurSymbol = Symbol;
Chris Lattnereb72dca2010-07-11 22:05:00 +0000266}
267
268void WinCOFFStreamer::EmitCOFFSymbolStorageClass(int StorageClass) {
Michael J. Spencer8067adc2010-07-19 06:13:10 +0000269 assert(CurSymbol != NULL && "BeginCOFFSymbolDef must be called first!");
270 assert((StorageClass & ~0xFF) == 0 && "StorageClass must only have data in "
271 "the first byte!");
272
273 getAssembler().getOrCreateSymbolData(*CurSymbol).modifyFlags(
274 StorageClass << COFF::SF_ClassShift,
275 COFF::SF_ClassMask);
Chris Lattnereb72dca2010-07-11 22:05:00 +0000276}
277
278void WinCOFFStreamer::EmitCOFFSymbolType(int Type) {
Michael J. Spencer8067adc2010-07-19 06:13:10 +0000279 assert(CurSymbol != NULL && "BeginCOFFSymbolDef must be called first!");
280 assert((Type & ~0xFFFF) == 0 && "Type must only have data in the first 2 "
281 "bytes");
282
283 getAssembler().getOrCreateSymbolData(*CurSymbol).modifyFlags(
284 Type << COFF::SF_TypeShift,
285 COFF::SF_TypeMask);
Chris Lattnereb72dca2010-07-11 22:05:00 +0000286}
287
288void WinCOFFStreamer::EndCOFFSymbolDef() {
Michael J. Spencer8067adc2010-07-19 06:13:10 +0000289 assert(CurSymbol != NULL && "BeginCOFFSymbolDef must be called first!");
290 CurSymbol = NULL;
Chris Lattnereb72dca2010-07-11 22:05:00 +0000291}
292
293void WinCOFFStreamer::EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) {
Michael J. Spencer8067adc2010-07-19 06:13:10 +0000294 llvm_unreachable("not implemented");
Chris Lattnereb72dca2010-07-11 22:05:00 +0000295}
296
297void WinCOFFStreamer::EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
Michael J. Spencer8067adc2010-07-19 06:13:10 +0000298 unsigned ByteAlignment) {
Michael J. Spencerd47f4a92010-10-09 11:00:37 +0000299 assert((Symbol->isInSection()
300 ? Symbol->getSection().getVariant() == MCSection::SV_COFF
301 : true) && "Got non COFF section in the COFF backend!");
Michael J. Spencer8067adc2010-07-19 06:13:10 +0000302 AddCommonSymbol(Symbol, Size, ByteAlignment, true);
Chris Lattnereb72dca2010-07-11 22:05:00 +0000303}
304
305void WinCOFFStreamer::EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size) {
Michael J. Spencerd47f4a92010-10-09 11:00:37 +0000306 assert((Symbol->isInSection()
307 ? Symbol->getSection().getVariant() == MCSection::SV_COFF
308 : true) && "Got non COFF section in the COFF backend!");
Michael J. Spencer8067adc2010-07-19 06:13:10 +0000309 AddCommonSymbol(Symbol, Size, 1, false);
Chris Lattnereb72dca2010-07-11 22:05:00 +0000310}
311
312void WinCOFFStreamer::EmitZerofill(const MCSection *Section, MCSymbol *Symbol,
Michael J. Spencer8067adc2010-07-19 06:13:10 +0000313 unsigned Size,unsigned ByteAlignment) {
314 llvm_unreachable("not implemented");
Chris Lattnereb72dca2010-07-11 22:05:00 +0000315}
316
317void WinCOFFStreamer::EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol,
318 uint64_t Size, unsigned ByteAlignment) {
Michael J. Spencer8067adc2010-07-19 06:13:10 +0000319 llvm_unreachable("not implemented");
Chris Lattnereb72dca2010-07-11 22:05:00 +0000320}
321
322void WinCOFFStreamer::EmitBytes(StringRef Data, unsigned AddrSpace) {
Michael J. Spencer8067adc2010-07-19 06:13:10 +0000323 // TODO: This is copied exactly from the MachOStreamer. Consider merging into
324 // MCObjectStreamer?
325 getOrCreateDataFragment()->getContents().append(Data.begin(), Data.end());
Chris Lattnereb72dca2010-07-11 22:05:00 +0000326}
327
Chris Lattnereb72dca2010-07-11 22:05:00 +0000328void WinCOFFStreamer::EmitValueToAlignment(unsigned ByteAlignment,
Michael J. Spencer8067adc2010-07-19 06:13:10 +0000329 int64_t Value,
330 unsigned ValueSize,
331 unsigned MaxBytesToEmit) {
332 // TODO: This is copied exactly from the MachOStreamer. Consider merging into
333 // MCObjectStreamer?
334 if (MaxBytesToEmit == 0)
335 MaxBytesToEmit = ByteAlignment;
336 new MCAlignFragment(ByteAlignment, Value, ValueSize, MaxBytesToEmit,
337 getCurrentSectionData());
338
339 // Update the maximum alignment on the current section if necessary.
340 if (ByteAlignment > getCurrentSectionData()->getAlignment())
341 getCurrentSectionData()->setAlignment(ByteAlignment);
Chris Lattnereb72dca2010-07-11 22:05:00 +0000342}
343
344void WinCOFFStreamer::EmitCodeAlignment(unsigned ByteAlignment,
Michael J. Spencer8067adc2010-07-19 06:13:10 +0000345 unsigned MaxBytesToEmit) {
346 // TODO: This is copied exactly from the MachOStreamer. Consider merging into
347 // MCObjectStreamer?
348 if (MaxBytesToEmit == 0)
349 MaxBytesToEmit = ByteAlignment;
350 MCAlignFragment *F = new MCAlignFragment(ByteAlignment, 0, 1, MaxBytesToEmit,
351 getCurrentSectionData());
352 F->setEmitNops(true);
353
354 // Update the maximum alignment on the current section if necessary.
355 if (ByteAlignment > getCurrentSectionData()->getAlignment())
356 getCurrentSectionData()->setAlignment(ByteAlignment);
Chris Lattnereb72dca2010-07-11 22:05:00 +0000357}
358
Chris Lattnereb72dca2010-07-11 22:05:00 +0000359void WinCOFFStreamer::EmitFileDirective(StringRef Filename) {
360 // Ignore for now, linkers don't care, and proper debug
361 // info will be a much large effort.
362}
363
Chris Lattnereb72dca2010-07-11 22:05:00 +0000364void WinCOFFStreamer::EmitInstruction(const MCInst &Instruction) {
Michael J. Spencer8067adc2010-07-19 06:13:10 +0000365 for (unsigned i = 0, e = Instruction.getNumOperands(); i != e; ++i)
366 if (Instruction.getOperand(i).isExpr())
367 AddValueSymbols(Instruction.getOperand(i).getExpr());
368
369 getCurrentSectionData()->setHasInstructions(true);
370
371 MCInstFragment *Fragment =
372 new MCInstFragment(Instruction, getCurrentSectionData());
373
374 raw_svector_ostream VecOS(Fragment->getCode());
375
376 getAssembler().getEmitter().EncodeInstruction(Instruction, VecOS,
377 Fragment->getFixups());
Chris Lattnereb72dca2010-07-11 22:05:00 +0000378}
379
380void WinCOFFStreamer::Finish() {
381 MCObjectStreamer::Finish();
382}
383
384namespace llvm
385{
386 MCStreamer *createWinCOFFStreamer(MCContext &Context,
387 TargetAsmBackend &TAB,
388 MCCodeEmitter &CE,
Michael J. Spencere2195d82010-07-31 06:22:29 +0000389 raw_ostream &OS,
390 bool RelaxAll) {
391 WinCOFFStreamer *S = new WinCOFFStreamer(Context, TAB, CE, OS);
392 S->getAssembler().setRelaxAll(RelaxAll);
393 return S;
Chris Lattnereb72dca2010-07-11 22:05:00 +0000394 }
395}