blob: d3a58df81d12b8a11534fa620718895ecdf287c2 [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
51 virtual void EmitLabel(MCSymbol *Symbol);
52 virtual void EmitAssemblerFlag(MCAssemblerFlag Flag);
53 virtual void EmitAssignment(MCSymbol *Symbol, const MCExpr *Value);
54 virtual void EmitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute);
55 virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue);
56 virtual void BeginCOFFSymbolDef(MCSymbol const *Symbol);
57 virtual void EmitCOFFSymbolStorageClass(int StorageClass);
58 virtual void EmitCOFFSymbolType(int Type);
59 virtual void EndCOFFSymbolDef();
60 virtual void EmitELFSize(MCSymbol *Symbol, const MCExpr *Value);
61 virtual void EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
Michael J. Spencer8067adc2010-07-19 06:13:10 +000062 unsigned ByteAlignment);
Chris Lattnereb72dca2010-07-11 22:05:00 +000063 virtual void EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size);
64 virtual void EmitZerofill(const MCSection *Section, MCSymbol *Symbol,
Michael J. Spencer8067adc2010-07-19 06:13:10 +000065 unsigned Size,unsigned ByteAlignment);
Chris Lattnereb72dca2010-07-11 22:05:00 +000066 virtual void EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol,
Michael J. Spencer8067adc2010-07-19 06:13:10 +000067 uint64_t Size, unsigned ByteAlignment);
Chris Lattnereb72dca2010-07-11 22:05:00 +000068 virtual void EmitBytes(StringRef Data, unsigned AddrSpace);
Michael J. Spencer8067adc2010-07-19 06:13:10 +000069 virtual void EmitValue(const MCExpr *Value, unsigned Size,
Chris Lattnereb72dca2010-07-11 22:05:00 +000070 unsigned AddrSpace);
71 virtual void EmitGPRel32Value(const MCExpr *Value);
72 virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value,
Michael J. Spencer8067adc2010-07-19 06:13:10 +000073 unsigned ValueSize, unsigned MaxBytesToEmit);
Chris Lattnereb72dca2010-07-11 22:05:00 +000074 virtual void EmitCodeAlignment(unsigned ByteAlignment,
75 unsigned MaxBytesToEmit);
76 virtual void EmitValueToOffset(const MCExpr *Offset, unsigned char Value);
77 virtual void EmitFileDirective(StringRef Filename);
78 virtual void EmitDwarfFileDirective(unsigned FileNo,StringRef Filename);
79 virtual void EmitInstruction(const MCInst &Instruction);
80 virtual void Finish();
81};
82} // end anonymous namespace.
83
84WinCOFFStreamer::WinCOFFStreamer(MCContext &Context,
85 TargetAsmBackend &TAB,
86 MCCodeEmitter &CE,
87 raw_ostream &OS)
Michael J. Spencer8067adc2010-07-19 06:13:10 +000088 : MCObjectStreamer(Context, TAB, OS, &CE)
89 , CurSymbol(NULL) {
90}
91
92void WinCOFFStreamer::AddCommonSymbol(MCSymbol *Symbol, uint64_t Size,
93 unsigned ByteAlignment, bool External) {
94 assert(!Symbol->isInSection() && "Symbol must not already have a section!");
95
96 std::string SectionName(".bss$linkonce");
97 SectionName.append(Symbol->getName().begin(), Symbol->getName().end());
98
99 MCSymbolData &SymbolData = getAssembler().getOrCreateSymbolData(*Symbol);
100
101 unsigned Characteristics =
102 COFF::IMAGE_SCN_LNK_COMDAT |
103 COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA |
104 COFF::IMAGE_SCN_MEM_READ |
105 COFF::IMAGE_SCN_MEM_WRITE;
106
107 int Selection = COFF::IMAGE_COMDAT_SELECT_LARGEST;
108
109 const MCSection *Section = MCStreamer::getContext().getCOFFSection(
110 SectionName, Characteristics, Selection, SectionKind::getBSS());
111
112 MCSectionData &SectionData = getAssembler().getOrCreateSectionData(*Section);
113
114 if (SectionData.getAlignment() < ByteAlignment)
115 SectionData.setAlignment(ByteAlignment);
116
117 SymbolData.setExternal(External);
118
119 Symbol->setSection(*Section);
120
121 if (ByteAlignment != 1)
122 new MCAlignFragment(ByteAlignment, 0, 0, ByteAlignment, &SectionData);
123
124 SymbolData.setFragment(new MCFillFragment(0, 0, Size, &SectionData));
Chris Lattnereb72dca2010-07-11 22:05:00 +0000125}
126
127// MCStreamer interface
128
129void WinCOFFStreamer::EmitLabel(MCSymbol *Symbol) {
Michael J. Spencer8067adc2010-07-19 06:13:10 +0000130 // TODO: This is copied almost exactly from the MachOStreamer. Consider
131 // merging into MCObjectStreamer?
132 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
133 assert(!Symbol->isVariable() && "Cannot emit a variable symbol!");
134 assert(CurSection && "Cannot emit before setting section!");
135
136 Symbol->setSection(*CurSection);
137
138 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
139
140 // FIXME: This is wasteful, we don't necessarily need to create a data
141 // fragment. Instead, we should mark the symbol as pointing into the data
142 // fragment if it exists, otherwise we should just queue the label and set its
143 // fragment pointer when we emit the next fragment.
144 MCDataFragment *F = getOrCreateDataFragment();
145 assert(!SD.getFragment() && "Unexpected fragment on symbol data!");
146 SD.setFragment(F);
147 SD.setOffset(F->getContents().size());
Chris Lattnereb72dca2010-07-11 22:05:00 +0000148}
149
150void WinCOFFStreamer::EmitAssemblerFlag(MCAssemblerFlag Flag) {
Michael J. Spencer8067adc2010-07-19 06:13:10 +0000151 llvm_unreachable("not implemented");
Chris Lattnereb72dca2010-07-11 22:05:00 +0000152}
153
154void WinCOFFStreamer::EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) {
Michael J. Spencer8067adc2010-07-19 06:13:10 +0000155 // TODO: This is exactly the same as MachOStreamer. Consider merging into
156 // MCObjectStreamer.
157 getAssembler().getOrCreateSymbolData(*Symbol);
158 AddValueSymbols(Value);
159 Symbol->setVariableValue(Value);
Chris Lattnereb72dca2010-07-11 22:05:00 +0000160}
161
162void WinCOFFStreamer::EmitSymbolAttribute(MCSymbol *Symbol,
163 MCSymbolAttr Attribute) {
Michael J. Spencer8067adc2010-07-19 06:13:10 +0000164 switch (Attribute) {
165 case MCSA_WeakReference:
166 getAssembler().getOrCreateSymbolData(*Symbol).modifyFlags(
167 COFF::SF_WeakReference,
168 COFF::SF_WeakReference);
169 break;
170
171 case MCSA_Global:
172 getAssembler().getOrCreateSymbolData(*Symbol).setExternal(true);
173 break;
174
175 default:
176 llvm_unreachable("unsupported attribute");
177 break;
178 }
Chris Lattnereb72dca2010-07-11 22:05:00 +0000179}
180
181void WinCOFFStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
Michael J. Spencer8067adc2010-07-19 06:13:10 +0000182 llvm_unreachable("not implemented");
Chris Lattnereb72dca2010-07-11 22:05:00 +0000183}
184
185void WinCOFFStreamer::BeginCOFFSymbolDef(MCSymbol const *Symbol) {
Michael J. Spencer8067adc2010-07-19 06:13:10 +0000186 assert(CurSymbol == NULL && "EndCOFFSymbolDef must be called between calls "
187 "to BeginCOFFSymbolDef!");
188 CurSymbol = Symbol;
Chris Lattnereb72dca2010-07-11 22:05:00 +0000189}
190
191void WinCOFFStreamer::EmitCOFFSymbolStorageClass(int StorageClass) {
Michael J. Spencer8067adc2010-07-19 06:13:10 +0000192 assert(CurSymbol != NULL && "BeginCOFFSymbolDef must be called first!");
193 assert((StorageClass & ~0xFF) == 0 && "StorageClass must only have data in "
194 "the first byte!");
195
196 getAssembler().getOrCreateSymbolData(*CurSymbol).modifyFlags(
197 StorageClass << COFF::SF_ClassShift,
198 COFF::SF_ClassMask);
Chris Lattnereb72dca2010-07-11 22:05:00 +0000199}
200
201void WinCOFFStreamer::EmitCOFFSymbolType(int Type) {
Michael J. Spencer8067adc2010-07-19 06:13:10 +0000202 assert(CurSymbol != NULL && "BeginCOFFSymbolDef must be called first!");
203 assert((Type & ~0xFFFF) == 0 && "Type must only have data in the first 2 "
204 "bytes");
205
206 getAssembler().getOrCreateSymbolData(*CurSymbol).modifyFlags(
207 Type << COFF::SF_TypeShift,
208 COFF::SF_TypeMask);
Chris Lattnereb72dca2010-07-11 22:05:00 +0000209}
210
211void WinCOFFStreamer::EndCOFFSymbolDef() {
Michael J. Spencer8067adc2010-07-19 06:13:10 +0000212 assert(CurSymbol != NULL && "BeginCOFFSymbolDef must be called first!");
213 CurSymbol = NULL;
Chris Lattnereb72dca2010-07-11 22:05:00 +0000214}
215
216void WinCOFFStreamer::EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) {
Michael J. Spencer8067adc2010-07-19 06:13:10 +0000217 llvm_unreachable("not implemented");
Chris Lattnereb72dca2010-07-11 22:05:00 +0000218}
219
220void WinCOFFStreamer::EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
Michael J. Spencer8067adc2010-07-19 06:13:10 +0000221 unsigned ByteAlignment) {
222 AddCommonSymbol(Symbol, Size, ByteAlignment, true);
Chris Lattnereb72dca2010-07-11 22:05:00 +0000223}
224
225void WinCOFFStreamer::EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size) {
Michael J. Spencer8067adc2010-07-19 06:13:10 +0000226 AddCommonSymbol(Symbol, Size, 1, false);
Chris Lattnereb72dca2010-07-11 22:05:00 +0000227}
228
229void WinCOFFStreamer::EmitZerofill(const MCSection *Section, MCSymbol *Symbol,
Michael J. Spencer8067adc2010-07-19 06:13:10 +0000230 unsigned Size,unsigned ByteAlignment) {
231 llvm_unreachable("not implemented");
Chris Lattnereb72dca2010-07-11 22:05:00 +0000232}
233
234void WinCOFFStreamer::EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol,
235 uint64_t Size, unsigned ByteAlignment) {
Michael J. Spencer8067adc2010-07-19 06:13:10 +0000236 llvm_unreachable("not implemented");
Chris Lattnereb72dca2010-07-11 22:05:00 +0000237}
238
239void WinCOFFStreamer::EmitBytes(StringRef Data, unsigned AddrSpace) {
Michael J. Spencer8067adc2010-07-19 06:13:10 +0000240 // TODO: This is copied exactly from the MachOStreamer. Consider merging into
241 // MCObjectStreamer?
242 getOrCreateDataFragment()->getContents().append(Data.begin(), Data.end());
Chris Lattnereb72dca2010-07-11 22:05:00 +0000243}
244
245void WinCOFFStreamer::EmitValue(const MCExpr *Value, unsigned Size,
Michael J. Spencer8067adc2010-07-19 06:13:10 +0000246 unsigned AddrSpace) {
247 assert(AddrSpace == 0 && "Address space must be 0!");
248
249 // TODO: This is copied exactly from the MachOStreamer. Consider merging into
250 // MCObjectStreamer?
251 MCDataFragment *DF = getOrCreateDataFragment();
252
253 // Avoid fixups when possible.
254 int64_t AbsValue;
255 if (AddValueSymbols(Value)->EvaluateAsAbsolute(AbsValue)) {
256 // FIXME: Endianness assumption.
257 for (unsigned i = 0; i != Size; ++i)
258 DF->getContents().push_back(uint8_t(AbsValue >> (i * 8)));
259 } else {
260 DF->addFixup(MCFixup::Create(DF->getContents().size(),
261 AddValueSymbols(Value),
262 MCFixup::getKindForSize(Size)));
263 DF->getContents().resize(DF->getContents().size() + Size, 0);
264 }
Chris Lattnereb72dca2010-07-11 22:05:00 +0000265}
266
267void WinCOFFStreamer::EmitGPRel32Value(const MCExpr *Value) {
Michael J. Spencer8067adc2010-07-19 06:13:10 +0000268 llvm_unreachable("not implemented");
Chris Lattnereb72dca2010-07-11 22:05:00 +0000269}
270
271void WinCOFFStreamer::EmitValueToAlignment(unsigned ByteAlignment,
Michael J. Spencer8067adc2010-07-19 06:13:10 +0000272 int64_t Value,
273 unsigned ValueSize,
274 unsigned MaxBytesToEmit) {
275 // TODO: This is copied exactly from the MachOStreamer. Consider merging into
276 // MCObjectStreamer?
277 if (MaxBytesToEmit == 0)
278 MaxBytesToEmit = ByteAlignment;
279 new MCAlignFragment(ByteAlignment, Value, ValueSize, MaxBytesToEmit,
280 getCurrentSectionData());
281
282 // Update the maximum alignment on the current section if necessary.
283 if (ByteAlignment > getCurrentSectionData()->getAlignment())
284 getCurrentSectionData()->setAlignment(ByteAlignment);
Chris Lattnereb72dca2010-07-11 22:05:00 +0000285}
286
287void WinCOFFStreamer::EmitCodeAlignment(unsigned ByteAlignment,
Michael J. Spencer8067adc2010-07-19 06:13:10 +0000288 unsigned MaxBytesToEmit) {
289 // TODO: This is copied exactly from the MachOStreamer. Consider merging into
290 // MCObjectStreamer?
291 if (MaxBytesToEmit == 0)
292 MaxBytesToEmit = ByteAlignment;
293 MCAlignFragment *F = new MCAlignFragment(ByteAlignment, 0, 1, MaxBytesToEmit,
294 getCurrentSectionData());
295 F->setEmitNops(true);
296
297 // Update the maximum alignment on the current section if necessary.
298 if (ByteAlignment > getCurrentSectionData()->getAlignment())
299 getCurrentSectionData()->setAlignment(ByteAlignment);
Chris Lattnereb72dca2010-07-11 22:05:00 +0000300}
301
302void WinCOFFStreamer::EmitValueToOffset(const MCExpr *Offset,
Michael J. Spencer8067adc2010-07-19 06:13:10 +0000303 unsigned char Value) {
304 llvm_unreachable("not implemented");
Chris Lattnereb72dca2010-07-11 22:05:00 +0000305}
306
307void WinCOFFStreamer::EmitFileDirective(StringRef Filename) {
308 // Ignore for now, linkers don't care, and proper debug
309 // info will be a much large effort.
310}
311
312void WinCOFFStreamer::EmitDwarfFileDirective(unsigned FileNo,
Michael J. Spencer8067adc2010-07-19 06:13:10 +0000313 StringRef Filename) {
314 llvm_unreachable("not implemented");
Chris Lattnereb72dca2010-07-11 22:05:00 +0000315}
316
317void WinCOFFStreamer::EmitInstruction(const MCInst &Instruction) {
Michael J. Spencer8067adc2010-07-19 06:13:10 +0000318 for (unsigned i = 0, e = Instruction.getNumOperands(); i != e; ++i)
319 if (Instruction.getOperand(i).isExpr())
320 AddValueSymbols(Instruction.getOperand(i).getExpr());
321
322 getCurrentSectionData()->setHasInstructions(true);
323
324 MCInstFragment *Fragment =
325 new MCInstFragment(Instruction, getCurrentSectionData());
326
327 raw_svector_ostream VecOS(Fragment->getCode());
328
329 getAssembler().getEmitter().EncodeInstruction(Instruction, VecOS,
330 Fragment->getFixups());
Chris Lattnereb72dca2010-07-11 22:05:00 +0000331}
332
333void WinCOFFStreamer::Finish() {
334 MCObjectStreamer::Finish();
335}
336
337namespace llvm
338{
339 MCStreamer *createWinCOFFStreamer(MCContext &Context,
340 TargetAsmBackend &TAB,
341 MCCodeEmitter &CE,
Michael J. Spencere2195d82010-07-31 06:22:29 +0000342 raw_ostream &OS,
343 bool RelaxAll) {
344 WinCOFFStreamer *S = new WinCOFFStreamer(Context, TAB, CE, OS);
345 S->getAssembler().setRelaxAll(RelaxAll);
346 return S;
Chris Lattnereb72dca2010-07-11 22:05:00 +0000347 }
348}