blob: faecfcbe333b42affd18b3e32dde75db3a486aae [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();
Chris Lattnereb72dca2010-07-11 22:05:00 +000052 virtual void EmitLabel(MCSymbol *Symbol);
53 virtual void EmitAssemblerFlag(MCAssemblerFlag Flag);
54 virtual void EmitAssignment(MCSymbol *Symbol, const MCExpr *Value);
55 virtual void EmitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute);
56 virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue);
57 virtual void BeginCOFFSymbolDef(MCSymbol const *Symbol);
58 virtual void EmitCOFFSymbolStorageClass(int StorageClass);
59 virtual void EmitCOFFSymbolType(int Type);
60 virtual void EndCOFFSymbolDef();
61 virtual void EmitELFSize(MCSymbol *Symbol, const MCExpr *Value);
62 virtual void EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
Michael J. Spencer8067adc2010-07-19 06:13:10 +000063 unsigned ByteAlignment);
Chris Lattnereb72dca2010-07-11 22:05:00 +000064 virtual void EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size);
65 virtual void EmitZerofill(const MCSection *Section, MCSymbol *Symbol,
Michael J. Spencer8067adc2010-07-19 06:13:10 +000066 unsigned Size,unsigned ByteAlignment);
Chris Lattnereb72dca2010-07-11 22:05:00 +000067 virtual void EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol,
Michael J. Spencer8067adc2010-07-19 06:13:10 +000068 uint64_t Size, unsigned ByteAlignment);
Chris Lattnereb72dca2010-07-11 22:05:00 +000069 virtual void EmitBytes(StringRef Data, unsigned AddrSpace);
Michael J. Spencer8067adc2010-07-19 06:13:10 +000070 virtual void EmitValue(const MCExpr *Value, unsigned Size,
Chris Lattnereb72dca2010-07-11 22:05:00 +000071 unsigned AddrSpace);
72 virtual void EmitGPRel32Value(const MCExpr *Value);
73 virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value,
Michael J. Spencer8067adc2010-07-19 06:13:10 +000074 unsigned ValueSize, unsigned MaxBytesToEmit);
Chris Lattnereb72dca2010-07-11 22:05:00 +000075 virtual void EmitCodeAlignment(unsigned ByteAlignment,
76 unsigned MaxBytesToEmit);
77 virtual void EmitValueToOffset(const MCExpr *Offset, unsigned char Value);
78 virtual void EmitFileDirective(StringRef Filename);
79 virtual void EmitDwarfFileDirective(unsigned FileNo,StringRef Filename);
80 virtual void EmitInstruction(const MCInst &Instruction);
81 virtual void Finish();
82};
83} // end anonymous namespace.
84
85WinCOFFStreamer::WinCOFFStreamer(MCContext &Context,
86 TargetAsmBackend &TAB,
87 MCCodeEmitter &CE,
88 raw_ostream &OS)
Michael J. Spencer8067adc2010-07-19 06:13:10 +000089 : MCObjectStreamer(Context, TAB, OS, &CE)
90 , CurSymbol(NULL) {
91}
92
93void WinCOFFStreamer::AddCommonSymbol(MCSymbol *Symbol, uint64_t Size,
94 unsigned ByteAlignment, bool External) {
95 assert(!Symbol->isInSection() && "Symbol must not already have a section!");
96
97 std::string SectionName(".bss$linkonce");
98 SectionName.append(Symbol->getName().begin(), Symbol->getName().end());
99
100 MCSymbolData &SymbolData = getAssembler().getOrCreateSymbolData(*Symbol);
101
102 unsigned Characteristics =
103 COFF::IMAGE_SCN_LNK_COMDAT |
104 COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA |
105 COFF::IMAGE_SCN_MEM_READ |
106 COFF::IMAGE_SCN_MEM_WRITE;
107
108 int Selection = COFF::IMAGE_COMDAT_SELECT_LARGEST;
109
110 const MCSection *Section = MCStreamer::getContext().getCOFFSection(
111 SectionName, Characteristics, Selection, SectionKind::getBSS());
112
113 MCSectionData &SectionData = getAssembler().getOrCreateSectionData(*Section);
114
115 if (SectionData.getAlignment() < ByteAlignment)
116 SectionData.setAlignment(ByteAlignment);
117
118 SymbolData.setExternal(External);
119
120 Symbol->setSection(*Section);
121
122 if (ByteAlignment != 1)
123 new MCAlignFragment(ByteAlignment, 0, 0, ByteAlignment, &SectionData);
124
125 SymbolData.setFragment(new MCFillFragment(0, 0, Size, &SectionData));
Chris Lattnereb72dca2010-07-11 22:05:00 +0000126}
127
128// MCStreamer interface
129
Rafael Espindolad80781b2010-09-15 21:48:40 +0000130void WinCOFFStreamer::InitSections() {
131}
132
Chris Lattnereb72dca2010-07-11 22:05:00 +0000133void WinCOFFStreamer::EmitLabel(MCSymbol *Symbol) {
Michael J. Spencer8067adc2010-07-19 06:13:10 +0000134 // TODO: This is copied almost exactly from the MachOStreamer. Consider
135 // merging into MCObjectStreamer?
136 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
137 assert(!Symbol->isVariable() && "Cannot emit a variable symbol!");
138 assert(CurSection && "Cannot emit before setting section!");
139
140 Symbol->setSection(*CurSection);
141
142 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
143
144 // FIXME: This is wasteful, we don't necessarily need to create a data
145 // fragment. Instead, we should mark the symbol as pointing into the data
146 // fragment if it exists, otherwise we should just queue the label and set its
147 // fragment pointer when we emit the next fragment.
Michael J. Spencerbf252be2010-08-25 19:27:27 +0000148 MCDataFragment *DF = getOrCreateDataFragment();
149
Michael J. Spencer8067adc2010-07-19 06:13:10 +0000150 assert(!SD.getFragment() && "Unexpected fragment on symbol data!");
Michael J. Spencerbf252be2010-08-25 19:27:27 +0000151 SD.setFragment(DF);
152 SD.setOffset(DF->getContents().size());
Chris Lattnereb72dca2010-07-11 22:05:00 +0000153}
154
155void WinCOFFStreamer::EmitAssemblerFlag(MCAssemblerFlag Flag) {
Michael J. Spencer8067adc2010-07-19 06:13:10 +0000156 llvm_unreachable("not implemented");
Chris Lattnereb72dca2010-07-11 22:05:00 +0000157}
158
159void WinCOFFStreamer::EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) {
Michael J. Spencer8067adc2010-07-19 06:13:10 +0000160 // TODO: This is exactly the same as MachOStreamer. Consider merging into
161 // MCObjectStreamer.
162 getAssembler().getOrCreateSymbolData(*Symbol);
163 AddValueSymbols(Value);
164 Symbol->setVariableValue(Value);
Chris Lattnereb72dca2010-07-11 22:05:00 +0000165}
166
167void WinCOFFStreamer::EmitSymbolAttribute(MCSymbol *Symbol,
168 MCSymbolAttr Attribute) {
Michael J. Spencer8067adc2010-07-19 06:13:10 +0000169 switch (Attribute) {
170 case MCSA_WeakReference:
171 getAssembler().getOrCreateSymbolData(*Symbol).modifyFlags(
172 COFF::SF_WeakReference,
173 COFF::SF_WeakReference);
174 break;
175
176 case MCSA_Global:
177 getAssembler().getOrCreateSymbolData(*Symbol).setExternal(true);
178 break;
179
180 default:
181 llvm_unreachable("unsupported attribute");
182 break;
183 }
Chris Lattnereb72dca2010-07-11 22:05:00 +0000184}
185
186void WinCOFFStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
Michael J. Spencer8067adc2010-07-19 06:13:10 +0000187 llvm_unreachable("not implemented");
Chris Lattnereb72dca2010-07-11 22:05:00 +0000188}
189
190void WinCOFFStreamer::BeginCOFFSymbolDef(MCSymbol const *Symbol) {
Michael J. Spencer8067adc2010-07-19 06:13:10 +0000191 assert(CurSymbol == NULL && "EndCOFFSymbolDef must be called between calls "
192 "to BeginCOFFSymbolDef!");
193 CurSymbol = Symbol;
Chris Lattnereb72dca2010-07-11 22:05:00 +0000194}
195
196void WinCOFFStreamer::EmitCOFFSymbolStorageClass(int StorageClass) {
Michael J. Spencer8067adc2010-07-19 06:13:10 +0000197 assert(CurSymbol != NULL && "BeginCOFFSymbolDef must be called first!");
198 assert((StorageClass & ~0xFF) == 0 && "StorageClass must only have data in "
199 "the first byte!");
200
201 getAssembler().getOrCreateSymbolData(*CurSymbol).modifyFlags(
202 StorageClass << COFF::SF_ClassShift,
203 COFF::SF_ClassMask);
Chris Lattnereb72dca2010-07-11 22:05:00 +0000204}
205
206void WinCOFFStreamer::EmitCOFFSymbolType(int Type) {
Michael J. Spencer8067adc2010-07-19 06:13:10 +0000207 assert(CurSymbol != NULL && "BeginCOFFSymbolDef must be called first!");
208 assert((Type & ~0xFFFF) == 0 && "Type must only have data in the first 2 "
209 "bytes");
210
211 getAssembler().getOrCreateSymbolData(*CurSymbol).modifyFlags(
212 Type << COFF::SF_TypeShift,
213 COFF::SF_TypeMask);
Chris Lattnereb72dca2010-07-11 22:05:00 +0000214}
215
216void WinCOFFStreamer::EndCOFFSymbolDef() {
Michael J. Spencer8067adc2010-07-19 06:13:10 +0000217 assert(CurSymbol != NULL && "BeginCOFFSymbolDef must be called first!");
218 CurSymbol = NULL;
Chris Lattnereb72dca2010-07-11 22:05:00 +0000219}
220
221void WinCOFFStreamer::EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) {
Michael J. Spencer8067adc2010-07-19 06:13:10 +0000222 llvm_unreachable("not implemented");
Chris Lattnereb72dca2010-07-11 22:05:00 +0000223}
224
225void WinCOFFStreamer::EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
Michael J. Spencer8067adc2010-07-19 06:13:10 +0000226 unsigned ByteAlignment) {
227 AddCommonSymbol(Symbol, Size, ByteAlignment, true);
Chris Lattnereb72dca2010-07-11 22:05:00 +0000228}
229
230void WinCOFFStreamer::EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size) {
Michael J. Spencer8067adc2010-07-19 06:13:10 +0000231 AddCommonSymbol(Symbol, Size, 1, false);
Chris Lattnereb72dca2010-07-11 22:05:00 +0000232}
233
234void WinCOFFStreamer::EmitZerofill(const MCSection *Section, MCSymbol *Symbol,
Michael J. Spencer8067adc2010-07-19 06:13:10 +0000235 unsigned Size,unsigned ByteAlignment) {
236 llvm_unreachable("not implemented");
Chris Lattnereb72dca2010-07-11 22:05:00 +0000237}
238
239void WinCOFFStreamer::EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol,
240 uint64_t Size, unsigned ByteAlignment) {
Michael J. Spencer8067adc2010-07-19 06:13:10 +0000241 llvm_unreachable("not implemented");
Chris Lattnereb72dca2010-07-11 22:05:00 +0000242}
243
244void WinCOFFStreamer::EmitBytes(StringRef Data, unsigned AddrSpace) {
Michael J. Spencer8067adc2010-07-19 06:13:10 +0000245 // TODO: This is copied exactly from the MachOStreamer. Consider merging into
246 // MCObjectStreamer?
247 getOrCreateDataFragment()->getContents().append(Data.begin(), Data.end());
Chris Lattnereb72dca2010-07-11 22:05:00 +0000248}
249
250void WinCOFFStreamer::EmitValue(const MCExpr *Value, unsigned Size,
Michael J. Spencer8067adc2010-07-19 06:13:10 +0000251 unsigned AddrSpace) {
252 assert(AddrSpace == 0 && "Address space must be 0!");
253
254 // TODO: This is copied exactly from the MachOStreamer. Consider merging into
255 // MCObjectStreamer?
256 MCDataFragment *DF = getOrCreateDataFragment();
257
258 // Avoid fixups when possible.
259 int64_t AbsValue;
260 if (AddValueSymbols(Value)->EvaluateAsAbsolute(AbsValue)) {
261 // FIXME: Endianness assumption.
262 for (unsigned i = 0; i != Size; ++i)
263 DF->getContents().push_back(uint8_t(AbsValue >> (i * 8)));
264 } else {
265 DF->addFixup(MCFixup::Create(DF->getContents().size(),
266 AddValueSymbols(Value),
267 MCFixup::getKindForSize(Size)));
268 DF->getContents().resize(DF->getContents().size() + Size, 0);
269 }
Chris Lattnereb72dca2010-07-11 22:05:00 +0000270}
271
272void WinCOFFStreamer::EmitGPRel32Value(const MCExpr *Value) {
Michael J. Spencer8067adc2010-07-19 06:13:10 +0000273 llvm_unreachable("not implemented");
Chris Lattnereb72dca2010-07-11 22:05:00 +0000274}
275
276void WinCOFFStreamer::EmitValueToAlignment(unsigned ByteAlignment,
Michael J. Spencer8067adc2010-07-19 06:13:10 +0000277 int64_t Value,
278 unsigned ValueSize,
279 unsigned MaxBytesToEmit) {
280 // TODO: This is copied exactly from the MachOStreamer. Consider merging into
281 // MCObjectStreamer?
282 if (MaxBytesToEmit == 0)
283 MaxBytesToEmit = ByteAlignment;
284 new MCAlignFragment(ByteAlignment, Value, ValueSize, MaxBytesToEmit,
285 getCurrentSectionData());
286
287 // Update the maximum alignment on the current section if necessary.
288 if (ByteAlignment > getCurrentSectionData()->getAlignment())
289 getCurrentSectionData()->setAlignment(ByteAlignment);
Chris Lattnereb72dca2010-07-11 22:05:00 +0000290}
291
292void WinCOFFStreamer::EmitCodeAlignment(unsigned ByteAlignment,
Michael J. Spencer8067adc2010-07-19 06:13:10 +0000293 unsigned MaxBytesToEmit) {
294 // TODO: This is copied exactly from the MachOStreamer. Consider merging into
295 // MCObjectStreamer?
296 if (MaxBytesToEmit == 0)
297 MaxBytesToEmit = ByteAlignment;
298 MCAlignFragment *F = new MCAlignFragment(ByteAlignment, 0, 1, MaxBytesToEmit,
299 getCurrentSectionData());
300 F->setEmitNops(true);
301
302 // Update the maximum alignment on the current section if necessary.
303 if (ByteAlignment > getCurrentSectionData()->getAlignment())
304 getCurrentSectionData()->setAlignment(ByteAlignment);
Chris Lattnereb72dca2010-07-11 22:05:00 +0000305}
306
307void WinCOFFStreamer::EmitValueToOffset(const MCExpr *Offset,
Michael J. Spencer8067adc2010-07-19 06:13:10 +0000308 unsigned char Value) {
309 llvm_unreachable("not implemented");
Chris Lattnereb72dca2010-07-11 22:05:00 +0000310}
311
312void WinCOFFStreamer::EmitFileDirective(StringRef Filename) {
313 // Ignore for now, linkers don't care, and proper debug
314 // info will be a much large effort.
315}
316
317void WinCOFFStreamer::EmitDwarfFileDirective(unsigned FileNo,
Michael J. Spencer8067adc2010-07-19 06:13:10 +0000318 StringRef Filename) {
319 llvm_unreachable("not implemented");
Chris Lattnereb72dca2010-07-11 22:05:00 +0000320}
321
322void WinCOFFStreamer::EmitInstruction(const MCInst &Instruction) {
Michael J. Spencer8067adc2010-07-19 06:13:10 +0000323 for (unsigned i = 0, e = Instruction.getNumOperands(); i != e; ++i)
324 if (Instruction.getOperand(i).isExpr())
325 AddValueSymbols(Instruction.getOperand(i).getExpr());
326
327 getCurrentSectionData()->setHasInstructions(true);
328
329 MCInstFragment *Fragment =
330 new MCInstFragment(Instruction, getCurrentSectionData());
331
332 raw_svector_ostream VecOS(Fragment->getCode());
333
334 getAssembler().getEmitter().EncodeInstruction(Instruction, VecOS,
335 Fragment->getFixups());
Chris Lattnereb72dca2010-07-11 22:05:00 +0000336}
337
338void WinCOFFStreamer::Finish() {
339 MCObjectStreamer::Finish();
340}
341
342namespace llvm
343{
344 MCStreamer *createWinCOFFStreamer(MCContext &Context,
345 TargetAsmBackend &TAB,
346 MCCodeEmitter &CE,
Michael J. Spencere2195d82010-07-31 06:22:29 +0000347 raw_ostream &OS,
348 bool RelaxAll) {
349 WinCOFFStreamer *S = new WinCOFFStreamer(Context, TAB, CE, OS);
350 S->getAssembler().setRelaxAll(RelaxAll);
351 return S;
Chris Lattnereb72dca2010-07-11 22:05:00 +0000352 }
353}