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/MCStreamer.h"
17 #include "llvm/MC/MCAsmBackend.h"
18 #include "llvm/MC/MCAsmLayout.h"
19 #include "llvm/MC/MCAssembler.h"
20 #include "llvm/MC/MCCodeEmitter.h"
21 #include "llvm/MC/MCContext.h"
22 #include "llvm/MC/MCExpr.h"
23 #include "llvm/MC/MCObjectStreamer.h"
24 #include "llvm/MC/MCSection.h"
25 #include "llvm/MC/MCSectionCOFF.h"
26 #include "llvm/MC/MCSymbol.h"
27 #include "llvm/MC/MCValue.h"
28 #include "llvm/MC/MCWin64EH.h"
29 #include "llvm/Support/COFF.h"
30 #include "llvm/Support/Debug.h"
31 #include "llvm/Support/ErrorHandling.h"
32 #include "llvm/Support/TargetRegistry.h"
33 #include "llvm/Support/raw_ostream.h"
38 class WinCOFFStreamer : public MCObjectStreamer {
40 MCSymbol const *CurSymbol;
42 WinCOFFStreamer(MCContext &Context,
47 void AddCommonSymbol(MCSymbol *Symbol, uint64_t Size,
48 unsigned ByteAlignment, bool External);
50 // MCStreamer interface
52 virtual void InitSections();
53 virtual void EmitLabel(MCSymbol *Symbol);
54 virtual void EmitAssemblerFlag(MCAssemblerFlag Flag);
55 virtual void EmitThumbFunc(MCSymbol *Func);
56 virtual void EmitAssignment(MCSymbol *Symbol, const MCExpr *Value);
57 virtual void EmitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute);
58 virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue);
59 virtual void BeginCOFFSymbolDef(MCSymbol const *Symbol);
60 virtual void EmitCOFFSymbolStorageClass(int StorageClass);
61 virtual void EmitCOFFSymbolType(int Type);
62 virtual void EndCOFFSymbolDef();
63 virtual void EmitCOFFSecRel32(MCSymbol const *Symbol);
64 virtual void EmitELFSize(MCSymbol *Symbol, const MCExpr *Value);
65 virtual void EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
66 unsigned ByteAlignment);
67 virtual void EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size,
68 unsigned ByteAlignment);
69 virtual void EmitZerofill(const MCSection *Section, MCSymbol *Symbol,
70 uint64_t Size,unsigned ByteAlignment);
71 virtual void EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol,
72 uint64_t Size, unsigned ByteAlignment);
73 virtual void EmitFileDirective(StringRef Filename);
74 virtual void EmitInstruction(const MCInst &Instruction);
75 virtual void EmitWin64EHHandlerData();
76 virtual void FinishImpl();
79 virtual void EmitInstToFragment(const MCInst &Inst) {
80 llvm_unreachable("Not used by WinCOFF.");
82 virtual void EmitInstToData(const MCInst &Inst) {
83 llvm_unreachable("Not used by WinCOFF.");
86 void SetSection(StringRef Section,
87 unsigned Characteristics,
89 SwitchSection(getContext().getCOFFSection(Section, Characteristics, Kind));
92 void SetSectionText() {
94 COFF::IMAGE_SCN_CNT_CODE
95 | COFF::IMAGE_SCN_MEM_EXECUTE
96 | COFF::IMAGE_SCN_MEM_READ,
97 SectionKind::getText());
98 EmitCodeAlignment(4, 0);
101 void SetSectionData() {
103 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA
104 | COFF::IMAGE_SCN_MEM_READ
105 | COFF::IMAGE_SCN_MEM_WRITE,
106 SectionKind::getDataRel());
107 EmitCodeAlignment(4, 0);
110 void SetSectionBSS() {
112 COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA
113 | COFF::IMAGE_SCN_MEM_READ
114 | COFF::IMAGE_SCN_MEM_WRITE,
115 SectionKind::getBSS());
116 EmitCodeAlignment(4, 0);
120 } // end anonymous namespace.
122 WinCOFFStreamer::WinCOFFStreamer(MCContext &Context,
126 : MCObjectStreamer(Context, MAB, OS, &CE)
130 void WinCOFFStreamer::AddCommonSymbol(MCSymbol *Symbol, uint64_t Size,
131 unsigned ByteAlignment, bool External) {
132 assert(!Symbol->isInSection() && "Symbol must not already have a section!");
134 std::string SectionName(".bss$linkonce");
135 SectionName.append(Symbol->getName().begin(), Symbol->getName().end());
137 MCSymbolData &SymbolData = getAssembler().getOrCreateSymbolData(*Symbol);
139 unsigned Characteristics =
140 COFF::IMAGE_SCN_LNK_COMDAT |
141 COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA |
142 COFF::IMAGE_SCN_MEM_READ |
143 COFF::IMAGE_SCN_MEM_WRITE;
145 int Selection = COFF::IMAGE_COMDAT_SELECT_LARGEST;
147 const MCSection *Section = MCStreamer::getContext().getCOFFSection(
148 SectionName, Characteristics, Selection, SectionKind::getBSS());
150 MCSectionData &SectionData = getAssembler().getOrCreateSectionData(*Section);
152 if (SectionData.getAlignment() < ByteAlignment)
153 SectionData.setAlignment(ByteAlignment);
155 SymbolData.setExternal(External);
157 Symbol->setSection(*Section);
159 if (ByteAlignment != 1)
160 new MCAlignFragment(ByteAlignment, 0, 0, ByteAlignment, &SectionData);
162 SymbolData.setFragment(new MCFillFragment(0, 0, Size, &SectionData));
165 // MCStreamer interface
167 void WinCOFFStreamer::InitSections() {
174 void WinCOFFStreamer::EmitLabel(MCSymbol *Symbol) {
175 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
176 MCObjectStreamer::EmitLabel(Symbol);
179 void WinCOFFStreamer::EmitAssemblerFlag(MCAssemblerFlag Flag) {
180 llvm_unreachable("not implemented");
183 void WinCOFFStreamer::EmitThumbFunc(MCSymbol *Func) {
184 llvm_unreachable("not implemented");
187 void WinCOFFStreamer::EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) {
188 assert((Symbol->isInSection()
189 ? Symbol->getSection().getVariant() == MCSection::SV_COFF
190 : true) && "Got non COFF section in the COFF backend!");
191 // FIXME: This is all very ugly and depressing. What needs to happen here
192 // depends on quite a few things that are all part of relaxation, which we
193 // don't really even do.
195 if (Value->getKind() != MCExpr::SymbolRef) {
196 MCObjectStreamer::EmitAssignment(Symbol, Value);
198 // FIXME: This is a horrible way to do this :(. This should really be
199 // handled after we are done with the MC* objects and immediately before
200 // writing out the object file when we know exactly what the symbol should
201 // look like in the coff symbol table. I'm not doing that now because the
202 // COFF object writer doesn't have a clearly defined separation between MC
203 // data structures, the object writers data structures, and the raw, POD,
204 // data structures that get written to disk.
206 // Copy over the aliased data.
207 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
208 const MCSymbolData &RealSD = getAssembler().getOrCreateSymbolData(
209 dyn_cast<const MCSymbolRefExpr>(Value)->getSymbol());
211 // FIXME: This is particularly nasty because it breaks as soon as any data
212 // members of MCSymbolData change.
213 SD.CommonAlign = RealSD.CommonAlign;
214 SD.CommonSize = RealSD.CommonSize;
215 SD.Flags = RealSD.Flags;
216 SD.Fragment = RealSD.Fragment;
217 SD.Index = RealSD.Index;
218 SD.IsExternal = RealSD.IsExternal;
219 SD.IsPrivateExtern = RealSD.IsPrivateExtern;
220 SD.Offset = RealSD.Offset;
221 SD.SymbolSize = RealSD.SymbolSize;
225 void WinCOFFStreamer::EmitSymbolAttribute(MCSymbol *Symbol,
226 MCSymbolAttr Attribute) {
227 assert(Symbol && "Symbol must be non-null!");
228 assert((Symbol->isInSection()
229 ? Symbol->getSection().getVariant() == MCSection::SV_COFF
230 : true) && "Got non COFF section in the COFF backend!");
232 case MCSA_WeakReference:
234 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
235 SD.modifyFlags(COFF::SF_WeakExternal, COFF::SF_WeakExternal);
236 SD.setExternal(true);
241 getAssembler().getOrCreateSymbolData(*Symbol).setExternal(true);
245 llvm_unreachable("unsupported attribute");
249 void WinCOFFStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
250 llvm_unreachable("not implemented");
253 void WinCOFFStreamer::BeginCOFFSymbolDef(MCSymbol const *Symbol) {
254 assert((Symbol->isInSection()
255 ? Symbol->getSection().getVariant() == MCSection::SV_COFF
256 : true) && "Got non COFF section in the COFF backend!");
257 assert(CurSymbol == NULL && "EndCOFFSymbolDef must be called between calls "
258 "to BeginCOFFSymbolDef!");
262 void WinCOFFStreamer::EmitCOFFSymbolStorageClass(int StorageClass) {
263 assert(CurSymbol != NULL && "BeginCOFFSymbolDef must be called first!");
264 assert((StorageClass & ~0xFF) == 0 && "StorageClass must only have data in "
267 getAssembler().getOrCreateSymbolData(*CurSymbol).modifyFlags(
268 StorageClass << COFF::SF_ClassShift,
272 void WinCOFFStreamer::EmitCOFFSymbolType(int Type) {
273 assert(CurSymbol != NULL && "BeginCOFFSymbolDef must be called first!");
274 assert((Type & ~0xFFFF) == 0 && "Type must only have data in the first 2 "
277 getAssembler().getOrCreateSymbolData(*CurSymbol).modifyFlags(
278 Type << COFF::SF_TypeShift,
282 void WinCOFFStreamer::EndCOFFSymbolDef() {
283 assert(CurSymbol != NULL && "BeginCOFFSymbolDef must be called first!");
287 void WinCOFFStreamer::EmitCOFFSecRel32(MCSymbol const *Symbol)
289 MCDataFragment *DF = getOrCreateDataFragment();
291 DF->addFixup(MCFixup::Create(DF->getContents().size(),
292 MCSymbolRefExpr::Create (Symbol, getContext ()),
294 DF->getContents().resize(DF->getContents().size() + 4, 0);
297 void WinCOFFStreamer::EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) {
298 llvm_unreachable("not implemented");
301 void WinCOFFStreamer::EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
302 unsigned ByteAlignment) {
303 assert((Symbol->isInSection()
304 ? Symbol->getSection().getVariant() == MCSection::SV_COFF
305 : true) && "Got non COFF section in the COFF backend!");
306 AddCommonSymbol(Symbol, Size, ByteAlignment, true);
309 void WinCOFFStreamer::EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size,
310 unsigned ByteAlignment) {
311 assert((Symbol->isInSection()
312 ? Symbol->getSection().getVariant() == MCSection::SV_COFF
313 : true) && "Got non COFF section in the COFF backend!");
314 AddCommonSymbol(Symbol, Size, ByteAlignment, false);
317 void WinCOFFStreamer::EmitZerofill(const MCSection *Section, MCSymbol *Symbol,
318 uint64_t Size,unsigned ByteAlignment) {
319 llvm_unreachable("not implemented");
322 void WinCOFFStreamer::EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol,
323 uint64_t Size, unsigned ByteAlignment) {
324 llvm_unreachable("not implemented");
327 void WinCOFFStreamer::EmitFileDirective(StringRef Filename) {
328 // Ignore for now, linkers don't care, and proper debug
329 // info will be a much large effort.
332 void WinCOFFStreamer::EmitInstruction(const MCInst &Instruction) {
333 for (unsigned i = 0, e = Instruction.getNumOperands(); i != e; ++i)
334 if (Instruction.getOperand(i).isExpr())
335 AddValueSymbols(Instruction.getOperand(i).getExpr());
337 getCurrentSectionData()->setHasInstructions(true);
339 MCInstFragment *Fragment =
340 new MCInstFragment(Instruction, getCurrentSectionData());
342 raw_svector_ostream VecOS(Fragment->getCode());
344 getAssembler().getEmitter().EncodeInstruction(Instruction, VecOS,
345 Fragment->getFixups());
348 void WinCOFFStreamer::EmitWin64EHHandlerData() {
349 MCStreamer::EmitWin64EHHandlerData();
351 // We have to emit the unwind info now, because this directive
352 // actually switches to the .xdata section!
353 MCWin64EHUnwindEmitter::EmitUnwindInfo(*this, getCurrentW64UnwindInfo());
356 void WinCOFFStreamer::FinishImpl() {
358 MCObjectStreamer::FinishImpl();
363 MCStreamer *createWinCOFFStreamer(MCContext &Context,
368 WinCOFFStreamer *S = new WinCOFFStreamer(Context, MAB, CE, OS);
369 S->getAssembler().setRelaxAll(RelaxAll);