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 EmitDebugLabel(MCSymbol *Symbol);
55 virtual void EmitAssemblerFlag(MCAssemblerFlag Flag);
56 virtual void EmitThumbFunc(MCSymbol *Func);
57 virtual void EmitAssignment(MCSymbol *Symbol, const MCExpr *Value);
58 virtual void EmitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute);
59 virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue);
60 virtual void BeginCOFFSymbolDef(MCSymbol const *Symbol);
61 virtual void EmitCOFFSymbolStorageClass(int StorageClass);
62 virtual void EmitCOFFSymbolType(int Type);
63 virtual void EndCOFFSymbolDef();
64 virtual void EmitCOFFSecRel32(MCSymbol const *Symbol);
65 virtual void EmitELFSize(MCSymbol *Symbol, const MCExpr *Value);
66 virtual void EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
67 unsigned ByteAlignment);
68 virtual void EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size,
69 unsigned ByteAlignment);
70 virtual void EmitZerofill(const MCSection *Section, MCSymbol *Symbol,
71 uint64_t Size,unsigned ByteAlignment);
72 virtual void EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol,
73 uint64_t Size, unsigned ByteAlignment);
74 virtual void EmitFileDirective(StringRef Filename);
75 virtual void EmitWin64EHHandlerData();
76 virtual void FinishImpl();
79 virtual void EmitInstToData(const MCInst &Inst) {
80 MCDataFragment *DF = getOrCreateDataFragment();
82 SmallVector<MCFixup, 4> Fixups;
83 SmallString<256> Code;
84 raw_svector_ostream VecOS(Code);
85 getAssembler().getEmitter().EncodeInstruction(Inst, VecOS, Fixups);
88 // Add the fixups and data.
89 for (unsigned i = 0, e = Fixups.size(); i != e; ++i) {
90 Fixups[i].setOffset(Fixups[i].getOffset() + DF->getContents().size());
91 DF->getFixups().push_back(Fixups[i]);
93 DF->getContents().append(Code.begin(), Code.end());
96 void SetSection(StringRef Section,
97 unsigned Characteristics,
99 SwitchSection(getContext().getCOFFSection(Section, Characteristics, Kind));
102 void SetSectionText() {
104 COFF::IMAGE_SCN_CNT_CODE
105 | COFF::IMAGE_SCN_MEM_EXECUTE
106 | COFF::IMAGE_SCN_MEM_READ,
107 SectionKind::getText());
108 EmitCodeAlignment(4, 0);
111 void SetSectionData() {
113 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA
114 | COFF::IMAGE_SCN_MEM_READ
115 | COFF::IMAGE_SCN_MEM_WRITE,
116 SectionKind::getDataRel());
117 EmitCodeAlignment(4, 0);
120 void SetSectionBSS() {
122 COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA
123 | COFF::IMAGE_SCN_MEM_READ
124 | COFF::IMAGE_SCN_MEM_WRITE,
125 SectionKind::getBSS());
126 EmitCodeAlignment(4, 0);
129 } // end anonymous namespace.
131 WinCOFFStreamer::WinCOFFStreamer(MCContext &Context,
135 : MCObjectStreamer(Context, MAB, OS, &CE)
139 void WinCOFFStreamer::AddCommonSymbol(MCSymbol *Symbol, uint64_t Size,
140 unsigned ByteAlignment, bool External) {
141 assert(!Symbol->isInSection() && "Symbol must not already have a section!");
143 std::string SectionName(".bss$linkonce");
144 SectionName.append(Symbol->getName().begin(), Symbol->getName().end());
146 MCSymbolData &SymbolData = getAssembler().getOrCreateSymbolData(*Symbol);
148 unsigned Characteristics =
149 COFF::IMAGE_SCN_LNK_COMDAT |
150 COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA |
151 COFF::IMAGE_SCN_MEM_READ |
152 COFF::IMAGE_SCN_MEM_WRITE;
154 int Selection = COFF::IMAGE_COMDAT_SELECT_LARGEST;
156 const MCSection *Section = MCStreamer::getContext().getCOFFSection(
157 SectionName, Characteristics, Selection, SectionKind::getBSS());
159 MCSectionData &SectionData = getAssembler().getOrCreateSectionData(*Section);
161 if (SectionData.getAlignment() < ByteAlignment)
162 SectionData.setAlignment(ByteAlignment);
164 SymbolData.setExternal(External);
166 Symbol->setSection(*Section);
168 if (ByteAlignment != 1)
169 new MCAlignFragment(ByteAlignment, 0, 0, ByteAlignment, &SectionData);
171 SymbolData.setFragment(new MCFillFragment(0, 0, Size, &SectionData));
174 // MCStreamer interface
176 void WinCOFFStreamer::InitSections() {
183 void WinCOFFStreamer::EmitLabel(MCSymbol *Symbol) {
184 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
185 MCObjectStreamer::EmitLabel(Symbol);
188 void WinCOFFStreamer::EmitDebugLabel(MCSymbol *Symbol) {
191 void WinCOFFStreamer::EmitAssemblerFlag(MCAssemblerFlag Flag) {
192 llvm_unreachable("not implemented");
195 void WinCOFFStreamer::EmitThumbFunc(MCSymbol *Func) {
196 llvm_unreachable("not implemented");
199 void WinCOFFStreamer::EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) {
200 assert((Symbol->isInSection()
201 ? Symbol->getSection().getVariant() == MCSection::SV_COFF
202 : true) && "Got non COFF section in the COFF backend!");
203 // FIXME: This is all very ugly and depressing. What needs to happen here
204 // depends on quite a few things that are all part of relaxation, which we
205 // don't really even do.
207 if (Value->getKind() != MCExpr::SymbolRef) {
208 MCObjectStreamer::EmitAssignment(Symbol, Value);
210 // FIXME: This is a horrible way to do this :(. This should really be
211 // handled after we are done with the MC* objects and immediately before
212 // writing out the object file when we know exactly what the symbol should
213 // look like in the coff symbol table. I'm not doing that now because the
214 // COFF object writer doesn't have a clearly defined separation between MC
215 // data structures, the object writers data structures, and the raw, POD,
216 // data structures that get written to disk.
218 // Copy over the aliased data.
219 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
220 const MCSymbolData &RealSD = getAssembler().getOrCreateSymbolData(
221 dyn_cast<const MCSymbolRefExpr>(Value)->getSymbol());
223 // FIXME: This is particularly nasty because it breaks as soon as any data
224 // members of MCSymbolData change.
225 SD.CommonAlign = RealSD.CommonAlign;
226 SD.CommonSize = RealSD.CommonSize;
227 SD.Flags = RealSD.Flags;
228 SD.Fragment = RealSD.Fragment;
229 SD.Index = RealSD.Index;
230 SD.IsExternal = RealSD.IsExternal;
231 SD.IsPrivateExtern = RealSD.IsPrivateExtern;
232 SD.Offset = RealSD.Offset;
233 SD.SymbolSize = RealSD.SymbolSize;
237 void WinCOFFStreamer::EmitSymbolAttribute(MCSymbol *Symbol,
238 MCSymbolAttr Attribute) {
239 assert(Symbol && "Symbol must be non-null!");
240 assert((Symbol->isInSection()
241 ? Symbol->getSection().getVariant() == MCSection::SV_COFF
242 : true) && "Got non COFF section in the COFF backend!");
244 case MCSA_WeakReference:
246 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
247 SD.modifyFlags(COFF::SF_WeakExternal, COFF::SF_WeakExternal);
248 SD.setExternal(true);
253 getAssembler().getOrCreateSymbolData(*Symbol).setExternal(true);
257 llvm_unreachable("unsupported attribute");
261 void WinCOFFStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
262 llvm_unreachable("not implemented");
265 void WinCOFFStreamer::BeginCOFFSymbolDef(MCSymbol const *Symbol) {
266 assert((Symbol->isInSection()
267 ? Symbol->getSection().getVariant() == MCSection::SV_COFF
268 : true) && "Got non COFF section in the COFF backend!");
269 assert(CurSymbol == NULL && "EndCOFFSymbolDef must be called between calls "
270 "to BeginCOFFSymbolDef!");
274 void WinCOFFStreamer::EmitCOFFSymbolStorageClass(int StorageClass) {
275 assert(CurSymbol != NULL && "BeginCOFFSymbolDef must be called first!");
276 assert((StorageClass & ~0xFF) == 0 && "StorageClass must only have data in "
279 getAssembler().getOrCreateSymbolData(*CurSymbol).modifyFlags(
280 StorageClass << COFF::SF_ClassShift,
284 void WinCOFFStreamer::EmitCOFFSymbolType(int Type) {
285 assert(CurSymbol != NULL && "BeginCOFFSymbolDef must be called first!");
286 assert((Type & ~0xFFFF) == 0 && "Type must only have data in the first 2 "
289 getAssembler().getOrCreateSymbolData(*CurSymbol).modifyFlags(
290 Type << COFF::SF_TypeShift,
294 void WinCOFFStreamer::EndCOFFSymbolDef() {
295 assert(CurSymbol != NULL && "BeginCOFFSymbolDef must be called first!");
299 void WinCOFFStreamer::EmitCOFFSecRel32(MCSymbol const *Symbol)
301 MCDataFragment *DF = getOrCreateDataFragment();
303 DF->getFixups().push_back(
304 MCFixup::Create(DF->getContents().size(),
305 MCSymbolRefExpr::Create (Symbol, getContext ()),
307 DF->getContents().resize(DF->getContents().size() + 4, 0);
310 void WinCOFFStreamer::EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) {
311 llvm_unreachable("not implemented");
314 void WinCOFFStreamer::EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
315 unsigned ByteAlignment) {
316 assert((Symbol->isInSection()
317 ? Symbol->getSection().getVariant() == MCSection::SV_COFF
318 : true) && "Got non COFF section in the COFF backend!");
319 AddCommonSymbol(Symbol, Size, ByteAlignment, true);
322 void WinCOFFStreamer::EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size,
323 unsigned ByteAlignment) {
324 assert((Symbol->isInSection()
325 ? Symbol->getSection().getVariant() == MCSection::SV_COFF
326 : true) && "Got non COFF section in the COFF backend!");
327 AddCommonSymbol(Symbol, Size, ByteAlignment, false);
330 void WinCOFFStreamer::EmitZerofill(const MCSection *Section, MCSymbol *Symbol,
331 uint64_t Size,unsigned ByteAlignment) {
332 llvm_unreachable("not implemented");
335 void WinCOFFStreamer::EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol,
336 uint64_t Size, unsigned ByteAlignment) {
337 llvm_unreachable("not implemented");
340 void WinCOFFStreamer::EmitFileDirective(StringRef Filename) {
341 // Ignore for now, linkers don't care, and proper debug
342 // info will be a much large effort.
345 void WinCOFFStreamer::EmitWin64EHHandlerData() {
346 MCStreamer::EmitWin64EHHandlerData();
348 // We have to emit the unwind info now, because this directive
349 // actually switches to the .xdata section!
350 MCWin64EHUnwindEmitter::EmitUnwindInfo(*this, getCurrentW64UnwindInfo());
353 void WinCOFFStreamer::FinishImpl() {
355 MCObjectStreamer::FinishImpl();
360 MCStreamer *createWinCOFFStreamer(MCContext &Context,
365 WinCOFFStreamer *S = new WinCOFFStreamer(Context, MAB, CE, OS);
366 S->getAssembler().setRelaxAll(RelaxAll);