blob: 8a194bff215137b695b1136153b00fb7eff38071 [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.
Michael J. Spencerbf252be2010-08-25 19:27:27 +0000144 MCDataFragment *DF = getOrCreateDataFragment();
145
Michael J. Spencer8067adc2010-07-19 06:13:10 +0000146 assert(!SD.getFragment() && "Unexpected fragment on symbol data!");
Michael J. Spencerbf252be2010-08-25 19:27:27 +0000147 SD.setFragment(DF);
148 SD.setOffset(DF->getContents().size());
Chris Lattnereb72dca2010-07-11 22:05:00 +0000149}
150
151void WinCOFFStreamer::EmitAssemblerFlag(MCAssemblerFlag Flag) {
Michael J. Spencer8067adc2010-07-19 06:13:10 +0000152 llvm_unreachable("not implemented");
Chris Lattnereb72dca2010-07-11 22:05:00 +0000153}
154
155void WinCOFFStreamer::EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) {
Michael J. Spencer8067adc2010-07-19 06:13:10 +0000156 // TODO: This is exactly the same as MachOStreamer. Consider merging into
157 // MCObjectStreamer.
158 getAssembler().getOrCreateSymbolData(*Symbol);
159 AddValueSymbols(Value);
160 Symbol->setVariableValue(Value);
Chris Lattnereb72dca2010-07-11 22:05:00 +0000161}
162
163void WinCOFFStreamer::EmitSymbolAttribute(MCSymbol *Symbol,
164 MCSymbolAttr Attribute) {
Michael J. Spencer8067adc2010-07-19 06:13:10 +0000165 switch (Attribute) {
166 case MCSA_WeakReference:
167 getAssembler().getOrCreateSymbolData(*Symbol).modifyFlags(
168 COFF::SF_WeakReference,
169 COFF::SF_WeakReference);
170 break;
171
172 case MCSA_Global:
173 getAssembler().getOrCreateSymbolData(*Symbol).setExternal(true);
174 break;
175
176 default:
177 llvm_unreachable("unsupported attribute");
178 break;
179 }
Chris Lattnereb72dca2010-07-11 22:05:00 +0000180}
181
182void WinCOFFStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
Michael J. Spencer8067adc2010-07-19 06:13:10 +0000183 llvm_unreachable("not implemented");
Chris Lattnereb72dca2010-07-11 22:05:00 +0000184}
185
186void WinCOFFStreamer::BeginCOFFSymbolDef(MCSymbol const *Symbol) {
Michael J. Spencer8067adc2010-07-19 06:13:10 +0000187 assert(CurSymbol == NULL && "EndCOFFSymbolDef must be called between calls "
188 "to BeginCOFFSymbolDef!");
189 CurSymbol = Symbol;
Chris Lattnereb72dca2010-07-11 22:05:00 +0000190}
191
192void WinCOFFStreamer::EmitCOFFSymbolStorageClass(int StorageClass) {
Michael J. Spencer8067adc2010-07-19 06:13:10 +0000193 assert(CurSymbol != NULL && "BeginCOFFSymbolDef must be called first!");
194 assert((StorageClass & ~0xFF) == 0 && "StorageClass must only have data in "
195 "the first byte!");
196
197 getAssembler().getOrCreateSymbolData(*CurSymbol).modifyFlags(
198 StorageClass << COFF::SF_ClassShift,
199 COFF::SF_ClassMask);
Chris Lattnereb72dca2010-07-11 22:05:00 +0000200}
201
202void WinCOFFStreamer::EmitCOFFSymbolType(int Type) {
Michael J. Spencer8067adc2010-07-19 06:13:10 +0000203 assert(CurSymbol != NULL && "BeginCOFFSymbolDef must be called first!");
204 assert((Type & ~0xFFFF) == 0 && "Type must only have data in the first 2 "
205 "bytes");
206
207 getAssembler().getOrCreateSymbolData(*CurSymbol).modifyFlags(
208 Type << COFF::SF_TypeShift,
209 COFF::SF_TypeMask);
Chris Lattnereb72dca2010-07-11 22:05:00 +0000210}
211
212void WinCOFFStreamer::EndCOFFSymbolDef() {
Michael J. Spencer8067adc2010-07-19 06:13:10 +0000213 assert(CurSymbol != NULL && "BeginCOFFSymbolDef must be called first!");
214 CurSymbol = NULL;
Chris Lattnereb72dca2010-07-11 22:05:00 +0000215}
216
217void WinCOFFStreamer::EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) {
Michael J. Spencer8067adc2010-07-19 06:13:10 +0000218 llvm_unreachable("not implemented");
Chris Lattnereb72dca2010-07-11 22:05:00 +0000219}
220
221void WinCOFFStreamer::EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
Michael J. Spencer8067adc2010-07-19 06:13:10 +0000222 unsigned ByteAlignment) {
223 AddCommonSymbol(Symbol, Size, ByteAlignment, true);
Chris Lattnereb72dca2010-07-11 22:05:00 +0000224}
225
226void WinCOFFStreamer::EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size) {
Michael J. Spencer8067adc2010-07-19 06:13:10 +0000227 AddCommonSymbol(Symbol, Size, 1, false);
Chris Lattnereb72dca2010-07-11 22:05:00 +0000228}
229
230void WinCOFFStreamer::EmitZerofill(const MCSection *Section, MCSymbol *Symbol,
Michael J. Spencer8067adc2010-07-19 06:13:10 +0000231 unsigned Size,unsigned ByteAlignment) {
232 llvm_unreachable("not implemented");
Chris Lattnereb72dca2010-07-11 22:05:00 +0000233}
234
235void WinCOFFStreamer::EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol,
236 uint64_t Size, unsigned ByteAlignment) {
Michael J. Spencer8067adc2010-07-19 06:13:10 +0000237 llvm_unreachable("not implemented");
Chris Lattnereb72dca2010-07-11 22:05:00 +0000238}
239
240void WinCOFFStreamer::EmitBytes(StringRef Data, unsigned AddrSpace) {
Michael J. Spencer8067adc2010-07-19 06:13:10 +0000241 // TODO: This is copied exactly from the MachOStreamer. Consider merging into
242 // MCObjectStreamer?
243 getOrCreateDataFragment()->getContents().append(Data.begin(), Data.end());
Chris Lattnereb72dca2010-07-11 22:05:00 +0000244}
245
246void WinCOFFStreamer::EmitValue(const MCExpr *Value, unsigned Size,
Michael J. Spencer8067adc2010-07-19 06:13:10 +0000247 unsigned AddrSpace) {
248 assert(AddrSpace == 0 && "Address space must be 0!");
249
250 // TODO: This is copied exactly from the MachOStreamer. Consider merging into
251 // MCObjectStreamer?
252 MCDataFragment *DF = getOrCreateDataFragment();
253
254 // Avoid fixups when possible.
255 int64_t AbsValue;
256 if (AddValueSymbols(Value)->EvaluateAsAbsolute(AbsValue)) {
257 // FIXME: Endianness assumption.
258 for (unsigned i = 0; i != Size; ++i)
259 DF->getContents().push_back(uint8_t(AbsValue >> (i * 8)));
260 } else {
261 DF->addFixup(MCFixup::Create(DF->getContents().size(),
262 AddValueSymbols(Value),
263 MCFixup::getKindForSize(Size)));
264 DF->getContents().resize(DF->getContents().size() + Size, 0);
265 }
Chris Lattnereb72dca2010-07-11 22:05:00 +0000266}
267
268void WinCOFFStreamer::EmitGPRel32Value(const MCExpr *Value) {
Michael J. Spencer8067adc2010-07-19 06:13:10 +0000269 llvm_unreachable("not implemented");
Chris Lattnereb72dca2010-07-11 22:05:00 +0000270}
271
272void WinCOFFStreamer::EmitValueToAlignment(unsigned ByteAlignment,
Michael J. Spencer8067adc2010-07-19 06:13:10 +0000273 int64_t Value,
274 unsigned ValueSize,
275 unsigned MaxBytesToEmit) {
276 // TODO: This is copied exactly from the MachOStreamer. Consider merging into
277 // MCObjectStreamer?
278 if (MaxBytesToEmit == 0)
279 MaxBytesToEmit = ByteAlignment;
280 new MCAlignFragment(ByteAlignment, Value, ValueSize, MaxBytesToEmit,
281 getCurrentSectionData());
282
283 // Update the maximum alignment on the current section if necessary.
284 if (ByteAlignment > getCurrentSectionData()->getAlignment())
285 getCurrentSectionData()->setAlignment(ByteAlignment);
Chris Lattnereb72dca2010-07-11 22:05:00 +0000286}
287
288void WinCOFFStreamer::EmitCodeAlignment(unsigned ByteAlignment,
Michael J. Spencer8067adc2010-07-19 06:13:10 +0000289 unsigned MaxBytesToEmit) {
290 // TODO: This is copied exactly from the MachOStreamer. Consider merging into
291 // MCObjectStreamer?
292 if (MaxBytesToEmit == 0)
293 MaxBytesToEmit = ByteAlignment;
294 MCAlignFragment *F = new MCAlignFragment(ByteAlignment, 0, 1, MaxBytesToEmit,
295 getCurrentSectionData());
296 F->setEmitNops(true);
297
298 // Update the maximum alignment on the current section if necessary.
299 if (ByteAlignment > getCurrentSectionData()->getAlignment())
300 getCurrentSectionData()->setAlignment(ByteAlignment);
Chris Lattnereb72dca2010-07-11 22:05:00 +0000301}
302
303void WinCOFFStreamer::EmitValueToOffset(const MCExpr *Offset,
Michael J. Spencer8067adc2010-07-19 06:13:10 +0000304 unsigned char Value) {
305 llvm_unreachable("not implemented");
Chris Lattnereb72dca2010-07-11 22:05:00 +0000306}
307
308void WinCOFFStreamer::EmitFileDirective(StringRef Filename) {
309 // Ignore for now, linkers don't care, and proper debug
310 // info will be a much large effort.
311}
312
313void WinCOFFStreamer::EmitDwarfFileDirective(unsigned FileNo,
Michael J. Spencer8067adc2010-07-19 06:13:10 +0000314 StringRef Filename) {
315 llvm_unreachable("not implemented");
Chris Lattnereb72dca2010-07-11 22:05:00 +0000316}
317
318void WinCOFFStreamer::EmitInstruction(const MCInst &Instruction) {
Michael J. Spencer8067adc2010-07-19 06:13:10 +0000319 for (unsigned i = 0, e = Instruction.getNumOperands(); i != e; ++i)
320 if (Instruction.getOperand(i).isExpr())
321 AddValueSymbols(Instruction.getOperand(i).getExpr());
322
323 getCurrentSectionData()->setHasInstructions(true);
324
325 MCInstFragment *Fragment =
326 new MCInstFragment(Instruction, getCurrentSectionData());
327
328 raw_svector_ostream VecOS(Fragment->getCode());
329
330 getAssembler().getEmitter().EncodeInstruction(Instruction, VecOS,
331 Fragment->getFixups());
Chris Lattnereb72dca2010-07-11 22:05:00 +0000332}
333
334void WinCOFFStreamer::Finish() {
335 MCObjectStreamer::Finish();
336}
337
338namespace llvm
339{
340 MCStreamer *createWinCOFFStreamer(MCContext &Context,
341 TargetAsmBackend &TAB,
342 MCCodeEmitter &CE,
Michael J. Spencere2195d82010-07-31 06:22:29 +0000343 raw_ostream &OS,
344 bool RelaxAll) {
345 WinCOFFStreamer *S = new WinCOFFStreamer(Context, TAB, CE, OS);
346 S->getAssembler().setRelaxAll(RelaxAll);
347 return S;
Chris Lattnereb72dca2010-07-11 22:05:00 +0000348 }
349}