1 //===-- llvm/MC/WinCOFFStreamer.cpp -----------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file contains an implementation of a Win32 COFF object file streamer.
12 //===----------------------------------------------------------------------===//
14 #define DEBUG_TYPE "WinCOFFStreamer"
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"
21 #include "llvm/MC/MCValue.h"
22 #include "llvm/MC/MCAssembler.h"
23 #include "llvm/MC/MCAsmLayout.h"
24 #include "llvm/MC/MCCodeEmitter.h"
25 #include "llvm/MC/MCSectionCOFF.h"
26 #include "llvm/Target/TargetRegistry.h"
27 #include "llvm/Target/TargetAsmBackend.h"
28 #include "llvm/ADT/StringMap.h"
30 #include "llvm/Support/COFF.h"
31 #include "llvm/Support/Debug.h"
32 #include "llvm/Support/ErrorHandling.h"
33 #include "llvm/Support/raw_ostream.h"
37 class WinCOFFStreamer : public MCObjectStreamer {
39 MCSymbol const *CurSymbol;
41 WinCOFFStreamer(MCContext &Context,
42 TargetAsmBackend &TAB,
46 void AddCommonSymbol(MCSymbol *Symbol, uint64_t Size,
47 unsigned ByteAlignment, bool External);
49 // MCStreamer interface
51 virtual void InitSections();
52 virtual void EmitLabel(MCSymbol *Symbol);
53 virtual void EmitAssemblerFlag(MCAssemblerFlag Flag);
54 virtual void EmitThumbFunc(MCSymbol *Func);
55 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,
64 unsigned ByteAlignment);
65 virtual void EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size);
66 virtual void EmitZerofill(const MCSection *Section, MCSymbol *Symbol,
67 unsigned Size,unsigned ByteAlignment);
68 virtual void EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol,
69 uint64_t Size, unsigned ByteAlignment);
70 virtual void EmitBytes(StringRef Data, unsigned AddrSpace);
71 virtual void EmitValue(const MCExpr *Value, unsigned Size,
73 virtual void EmitGPRel32Value(const MCExpr *Value);
74 virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value,
75 unsigned ValueSize, unsigned MaxBytesToEmit);
76 virtual void EmitCodeAlignment(unsigned ByteAlignment,
77 unsigned MaxBytesToEmit);
78 virtual void EmitValueToOffset(const MCExpr *Offset, unsigned char Value);
79 virtual void EmitFileDirective(StringRef Filename);
80 virtual void EmitDwarfFileDirective(unsigned FileNo,StringRef Filename);
81 virtual void EmitInstruction(const MCInst &Instruction);
82 virtual void Finish();
85 virtual void EmitInstToFragment(const MCInst &Inst) {
86 llvm_unreachable("Not used by WinCOFF.");
88 virtual void EmitInstToData(const MCInst &Inst) {
89 llvm_unreachable("Not used by WinCOFF.");
92 void SetSection(StringRef Section,
93 unsigned Characteristics,
95 SwitchSection(getContext().getCOFFSection(Section, Characteristics, Kind));
98 void SetSectionText() {
100 COFF::IMAGE_SCN_CNT_CODE
101 | COFF::IMAGE_SCN_MEM_EXECUTE
102 | COFF::IMAGE_SCN_MEM_READ,
103 SectionKind::getText());
104 EmitCodeAlignment(4, 0);
107 void SetSectionData() {
109 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA
110 | COFF::IMAGE_SCN_MEM_READ
111 | COFF::IMAGE_SCN_MEM_WRITE,
112 SectionKind::getDataRel());
113 EmitCodeAlignment(4, 0);
116 void SetSectionBSS() {
118 COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA
119 | COFF::IMAGE_SCN_MEM_READ
120 | COFF::IMAGE_SCN_MEM_WRITE,
121 SectionKind::getBSS());
122 EmitCodeAlignment(4, 0);
126 } // end anonymous namespace.
128 WinCOFFStreamer::WinCOFFStreamer(MCContext &Context,
129 TargetAsmBackend &TAB,
132 : MCObjectStreamer(Context, TAB, OS, &CE, true)
136 void WinCOFFStreamer::AddCommonSymbol(MCSymbol *Symbol, uint64_t Size,
137 unsigned ByteAlignment, bool External) {
138 assert(!Symbol->isInSection() && "Symbol must not already have a section!");
140 std::string SectionName(".bss$linkonce");
141 SectionName.append(Symbol->getName().begin(), Symbol->getName().end());
143 MCSymbolData &SymbolData = getAssembler().getOrCreateSymbolData(*Symbol);
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;
151 int Selection = COFF::IMAGE_COMDAT_SELECT_LARGEST;
153 const MCSection *Section = MCStreamer::getContext().getCOFFSection(
154 SectionName, Characteristics, Selection, SectionKind::getBSS());
156 MCSectionData &SectionData = getAssembler().getOrCreateSectionData(*Section);
158 if (SectionData.getAlignment() < ByteAlignment)
159 SectionData.setAlignment(ByteAlignment);
161 SymbolData.setExternal(External);
163 Symbol->setSection(*Section);
165 if (ByteAlignment != 1)
166 new MCAlignFragment(ByteAlignment, 0, 0, ByteAlignment, &SectionData);
168 SymbolData.setFragment(new MCFillFragment(0, 0, Size, &SectionData));
171 // MCStreamer interface
173 void WinCOFFStreamer::InitSections() {
180 void WinCOFFStreamer::EmitLabel(MCSymbol *Symbol) {
181 // TODO: This is copied almost exactly from the MachOStreamer. Consider
182 // merging into MCObjectStreamer?
183 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
184 assert(!Symbol->isVariable() && "Cannot emit a variable symbol!");
185 assert(CurSection && "Cannot emit before setting section!");
187 Symbol->setSection(*CurSection);
189 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
191 // FIXME: This is wasteful, we don't necessarily need to create a data
192 // fragment. Instead, we should mark the symbol as pointing into the data
193 // fragment if it exists, otherwise we should just queue the label and set its
194 // fragment pointer when we emit the next fragment.
195 MCDataFragment *DF = getOrCreateDataFragment();
197 assert(!SD.getFragment() && "Unexpected fragment on symbol data!");
199 SD.setOffset(DF->getContents().size());
202 void WinCOFFStreamer::EmitAssemblerFlag(MCAssemblerFlag Flag) {
203 llvm_unreachable("not implemented");
206 void WinCOFFStreamer::EmitThumbFunc(MCSymbol *Func) {
207 llvm_unreachable("not implemented");
210 void WinCOFFStreamer::EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) {
211 assert((Symbol->isInSection()
212 ? Symbol->getSection().getVariant() == MCSection::SV_COFF
213 : true) && "Got non COFF section in the COFF backend!");
214 // FIXME: This is all very ugly and depressing. What needs to happen here
215 // depends on quite a few things that are all part of relaxation, which we
216 // don't really even do.
218 if (Value->getKind() != MCExpr::SymbolRef) {
219 // TODO: This is exactly the same as MachOStreamer. Consider merging into
221 getAssembler().getOrCreateSymbolData(*Symbol);
222 AddValueSymbols(Value);
223 Symbol->setVariableValue(Value);
225 // FIXME: This is a horrible way to do this :(. This should really be
226 // handled after we are done with the MC* objects and immediately before
227 // writing out the object file when we know exactly what the symbol should
228 // look like in the coff symbol table. I'm not doing that now because the
229 // COFF object writer doesn't have a clearly defined separation between MC
230 // data structures, the object writers data structures, and the raw, POD,
231 // data structures that get written to disk.
233 // Copy over the aliased data.
234 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
235 const MCSymbolData &RealSD = getAssembler().getOrCreateSymbolData(
236 dyn_cast<const MCSymbolRefExpr>(Value)->getSymbol());
238 // FIXME: This is particularly nasty because it breaks as soon as any data
239 // members of MCSymbolData change.
240 SD.CommonAlign = RealSD.CommonAlign;
241 SD.CommonSize = RealSD.CommonSize;
242 SD.Flags = RealSD.Flags;
243 SD.Fragment = RealSD.Fragment;
244 SD.Index = RealSD.Index;
245 SD.IsExternal = RealSD.IsExternal;
246 SD.IsPrivateExtern = RealSD.IsPrivateExtern;
247 SD.Offset = RealSD.Offset;
248 SD.SymbolSize = RealSD.SymbolSize;
252 void WinCOFFStreamer::EmitSymbolAttribute(MCSymbol *Symbol,
253 MCSymbolAttr Attribute) {
254 assert(Symbol && "Symbol must be non-null!");
255 assert((Symbol->isInSection()
256 ? Symbol->getSection().getVariant() == MCSection::SV_COFF
257 : true) && "Got non COFF section in the COFF backend!");
259 case MCSA_WeakReference:
261 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
262 SD.modifyFlags(COFF::SF_WeakExternal, COFF::SF_WeakExternal);
263 SD.setExternal(true);
268 getAssembler().getOrCreateSymbolData(*Symbol).setExternal(true);
272 llvm_unreachable("unsupported attribute");
277 void WinCOFFStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
278 llvm_unreachable("not implemented");
281 void WinCOFFStreamer::BeginCOFFSymbolDef(MCSymbol const *Symbol) {
282 assert((Symbol->isInSection()
283 ? Symbol->getSection().getVariant() == MCSection::SV_COFF
284 : true) && "Got non COFF section in the COFF backend!");
285 assert(CurSymbol == NULL && "EndCOFFSymbolDef must be called between calls "
286 "to BeginCOFFSymbolDef!");
290 void WinCOFFStreamer::EmitCOFFSymbolStorageClass(int StorageClass) {
291 assert(CurSymbol != NULL && "BeginCOFFSymbolDef must be called first!");
292 assert((StorageClass & ~0xFF) == 0 && "StorageClass must only have data in "
295 getAssembler().getOrCreateSymbolData(*CurSymbol).modifyFlags(
296 StorageClass << COFF::SF_ClassShift,
300 void WinCOFFStreamer::EmitCOFFSymbolType(int Type) {
301 assert(CurSymbol != NULL && "BeginCOFFSymbolDef must be called first!");
302 assert((Type & ~0xFFFF) == 0 && "Type must only have data in the first 2 "
305 getAssembler().getOrCreateSymbolData(*CurSymbol).modifyFlags(
306 Type << COFF::SF_TypeShift,
310 void WinCOFFStreamer::EndCOFFSymbolDef() {
311 assert(CurSymbol != NULL && "BeginCOFFSymbolDef must be called first!");
315 void WinCOFFStreamer::EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) {
316 llvm_unreachable("not implemented");
319 void WinCOFFStreamer::EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
320 unsigned ByteAlignment) {
321 assert((Symbol->isInSection()
322 ? Symbol->getSection().getVariant() == MCSection::SV_COFF
323 : true) && "Got non COFF section in the COFF backend!");
324 AddCommonSymbol(Symbol, Size, ByteAlignment, true);
327 void WinCOFFStreamer::EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size) {
328 assert((Symbol->isInSection()
329 ? Symbol->getSection().getVariant() == MCSection::SV_COFF
330 : true) && "Got non COFF section in the COFF backend!");
331 AddCommonSymbol(Symbol, Size, 1, false);
334 void WinCOFFStreamer::EmitZerofill(const MCSection *Section, MCSymbol *Symbol,
335 unsigned Size,unsigned ByteAlignment) {
336 llvm_unreachable("not implemented");
339 void WinCOFFStreamer::EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol,
340 uint64_t Size, unsigned ByteAlignment) {
341 llvm_unreachable("not implemented");
344 void WinCOFFStreamer::EmitBytes(StringRef Data, unsigned AddrSpace) {
345 // TODO: This is copied exactly from the MachOStreamer. Consider merging into
347 getOrCreateDataFragment()->getContents().append(Data.begin(), Data.end());
350 void WinCOFFStreamer::EmitValue(const MCExpr *Value, unsigned Size,
351 unsigned AddrSpace) {
352 assert(AddrSpace == 0 && "Address space must be 0!");
354 // TODO: This is copied exactly from the MachOStreamer. Consider merging into
356 MCDataFragment *DF = getOrCreateDataFragment();
358 // Avoid fixups when possible.
360 if (AddValueSymbols(Value)->EvaluateAsAbsolute(AbsValue)) {
361 // FIXME: Endianness assumption.
362 for (unsigned i = 0; i != Size; ++i)
363 DF->getContents().push_back(uint8_t(AbsValue >> (i * 8)));
365 DF->addFixup(MCFixup::Create(DF->getContents().size(),
366 AddValueSymbols(Value),
367 MCFixup::getKindForSize(Size)));
368 DF->getContents().resize(DF->getContents().size() + Size, 0);
372 void WinCOFFStreamer::EmitGPRel32Value(const MCExpr *Value) {
373 llvm_unreachable("not implemented");
376 void WinCOFFStreamer::EmitValueToAlignment(unsigned ByteAlignment,
379 unsigned MaxBytesToEmit) {
380 // TODO: This is copied exactly from the MachOStreamer. Consider merging into
382 if (MaxBytesToEmit == 0)
383 MaxBytesToEmit = ByteAlignment;
384 new MCAlignFragment(ByteAlignment, Value, ValueSize, MaxBytesToEmit,
385 getCurrentSectionData());
387 // Update the maximum alignment on the current section if necessary.
388 if (ByteAlignment > getCurrentSectionData()->getAlignment())
389 getCurrentSectionData()->setAlignment(ByteAlignment);
392 void WinCOFFStreamer::EmitCodeAlignment(unsigned ByteAlignment,
393 unsigned MaxBytesToEmit) {
394 // TODO: This is copied exactly from the MachOStreamer. Consider merging into
396 if (MaxBytesToEmit == 0)
397 MaxBytesToEmit = ByteAlignment;
398 MCAlignFragment *F = new MCAlignFragment(ByteAlignment, 0, 1, MaxBytesToEmit,
399 getCurrentSectionData());
400 F->setEmitNops(true);
402 // Update the maximum alignment on the current section if necessary.
403 if (ByteAlignment > getCurrentSectionData()->getAlignment())
404 getCurrentSectionData()->setAlignment(ByteAlignment);
407 void WinCOFFStreamer::EmitValueToOffset(const MCExpr *Offset,
408 unsigned char Value) {
409 llvm_unreachable("not implemented");
412 void WinCOFFStreamer::EmitFileDirective(StringRef Filename) {
413 // Ignore for now, linkers don't care, and proper debug
414 // info will be a much large effort.
417 void WinCOFFStreamer::EmitDwarfFileDirective(unsigned FileNo,
418 StringRef Filename) {
419 llvm_unreachable("not implemented");
422 void WinCOFFStreamer::EmitInstruction(const MCInst &Instruction) {
423 for (unsigned i = 0, e = Instruction.getNumOperands(); i != e; ++i)
424 if (Instruction.getOperand(i).isExpr())
425 AddValueSymbols(Instruction.getOperand(i).getExpr());
427 getCurrentSectionData()->setHasInstructions(true);
429 MCInstFragment *Fragment =
430 new MCInstFragment(Instruction, getCurrentSectionData());
432 raw_svector_ostream VecOS(Fragment->getCode());
434 getAssembler().getEmitter().EncodeInstruction(Instruction, VecOS,
435 Fragment->getFixups());
438 void WinCOFFStreamer::Finish() {
439 MCObjectStreamer::Finish();
444 MCStreamer *createWinCOFFStreamer(MCContext &Context,
445 TargetAsmBackend &TAB,
449 WinCOFFStreamer *S = new WinCOFFStreamer(Context, TAB, CE, OS);
450 S->getAssembler().setRelaxAll(RelaxAll);