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 InitToTextSection();
54 virtual void EmitLabel(MCSymbol *Symbol);
55 virtual void EmitDebugLabel(MCSymbol *Symbol);
56 virtual void EmitAssemblerFlag(MCAssemblerFlag Flag);
57 virtual void EmitThumbFunc(MCSymbol *Func);
58 virtual void EmitAssignment(MCSymbol *Symbol, const MCExpr *Value);
59 virtual void EmitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute);
60 virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue);
61 virtual void BeginCOFFSymbolDef(MCSymbol const *Symbol);
62 virtual void EmitCOFFSymbolStorageClass(int StorageClass);
63 virtual void EmitCOFFSymbolType(int Type);
64 virtual void EndCOFFSymbolDef();
65 virtual void EmitCOFFSecRel32(MCSymbol const *Symbol);
66 virtual void EmitELFSize(MCSymbol *Symbol, const MCExpr *Value);
67 virtual void EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
68 unsigned ByteAlignment);
69 virtual void EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size,
70 unsigned ByteAlignment);
71 virtual void EmitZerofill(const MCSection *Section, MCSymbol *Symbol,
72 uint64_t Size,unsigned ByteAlignment);
73 virtual void EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol,
74 uint64_t Size, unsigned ByteAlignment);
75 virtual void EmitFileDirective(StringRef Filename);
76 virtual void EmitWin64EHHandlerData();
77 virtual void FinishImpl();
80 virtual void EmitInstToData(const MCInst &Inst) {
81 MCDataFragment *DF = getOrCreateDataFragment();
83 SmallVector<MCFixup, 4> Fixups;
84 SmallString<256> Code;
85 raw_svector_ostream VecOS(Code);
86 getAssembler().getEmitter().EncodeInstruction(Inst, VecOS, Fixups);
89 // Add the fixups and data.
90 for (unsigned i = 0, e = Fixups.size(); i != e; ++i) {
91 Fixups[i].setOffset(Fixups[i].getOffset() + DF->getContents().size());
92 DF->getFixups().push_back(Fixups[i]);
94 DF->getContents().append(Code.begin(), Code.end());
97 void SetSection(StringRef Section,
98 unsigned Characteristics,
100 SwitchSection(getContext().getCOFFSection(Section, Characteristics, Kind));
103 void SetSectionText() {
105 COFF::IMAGE_SCN_CNT_CODE
106 | COFF::IMAGE_SCN_MEM_EXECUTE
107 | COFF::IMAGE_SCN_MEM_READ,
108 SectionKind::getText());
109 EmitCodeAlignment(4, 0);
112 void SetSectionData() {
114 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA
115 | COFF::IMAGE_SCN_MEM_READ
116 | COFF::IMAGE_SCN_MEM_WRITE,
117 SectionKind::getDataRel());
118 EmitCodeAlignment(4, 0);
121 void SetSectionBSS() {
123 COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA
124 | COFF::IMAGE_SCN_MEM_READ
125 | COFF::IMAGE_SCN_MEM_WRITE,
126 SectionKind::getBSS());
127 EmitCodeAlignment(4, 0);
130 } // end anonymous namespace.
132 WinCOFFStreamer::WinCOFFStreamer(MCContext &Context,
136 : MCObjectStreamer(Context, MAB, OS, &CE)
140 void WinCOFFStreamer::AddCommonSymbol(MCSymbol *Symbol, uint64_t Size,
141 unsigned ByteAlignment, bool External) {
142 assert(!Symbol->isInSection() && "Symbol must not already have a section!");
144 std::string SectionName(".bss$linkonce");
145 SectionName.append(Symbol->getName().begin(), Symbol->getName().end());
147 MCSymbolData &SymbolData = getAssembler().getOrCreateSymbolData(*Symbol);
149 unsigned Characteristics =
150 COFF::IMAGE_SCN_LNK_COMDAT |
151 COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA |
152 COFF::IMAGE_SCN_MEM_READ |
153 COFF::IMAGE_SCN_MEM_WRITE;
155 int Selection = COFF::IMAGE_COMDAT_SELECT_LARGEST;
157 const MCSection *Section = MCStreamer::getContext().getCOFFSection(
158 SectionName, Characteristics, Selection, SectionKind::getBSS());
160 MCSectionData &SectionData = getAssembler().getOrCreateSectionData(*Section);
162 if (SectionData.getAlignment() < ByteAlignment)
163 SectionData.setAlignment(ByteAlignment);
165 SymbolData.setExternal(External);
167 Symbol->setSection(*Section);
169 if (ByteAlignment != 1)
170 new MCAlignFragment(ByteAlignment, 0, 0, ByteAlignment, &SectionData);
172 SymbolData.setFragment(new MCFillFragment(0, 0, Size, &SectionData));
175 // MCStreamer interface
177 void WinCOFFStreamer::InitToTextSection() {
181 void WinCOFFStreamer::InitSections() {
188 void WinCOFFStreamer::EmitLabel(MCSymbol *Symbol) {
189 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
190 MCObjectStreamer::EmitLabel(Symbol);
193 void WinCOFFStreamer::EmitDebugLabel(MCSymbol *Symbol) {
196 void WinCOFFStreamer::EmitAssemblerFlag(MCAssemblerFlag Flag) {
197 llvm_unreachable("not implemented");
200 void WinCOFFStreamer::EmitThumbFunc(MCSymbol *Func) {
201 llvm_unreachable("not implemented");
204 void WinCOFFStreamer::EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) {
205 assert((Symbol->isInSection()
206 ? Symbol->getSection().getVariant() == MCSection::SV_COFF
207 : true) && "Got non COFF section in the COFF backend!");
208 // FIXME: This is all very ugly and depressing. What needs to happen here
209 // depends on quite a few things that are all part of relaxation, which we
210 // don't really even do.
212 if (Value->getKind() != MCExpr::SymbolRef) {
213 MCObjectStreamer::EmitAssignment(Symbol, Value);
215 // FIXME: This is a horrible way to do this :(. This should really be
216 // handled after we are done with the MC* objects and immediately before
217 // writing out the object file when we know exactly what the symbol should
218 // look like in the coff symbol table. I'm not doing that now because the
219 // COFF object writer doesn't have a clearly defined separation between MC
220 // data structures, the object writers data structures, and the raw, POD,
221 // data structures that get written to disk.
223 // Copy over the aliased data.
224 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
225 const MCSymbolData &RealSD = getAssembler().getOrCreateSymbolData(
226 dyn_cast<const MCSymbolRefExpr>(Value)->getSymbol());
228 // FIXME: This is particularly nasty because it breaks as soon as any data
229 // members of MCSymbolData change.
230 SD.CommonAlign = RealSD.CommonAlign;
231 SD.CommonSize = RealSD.CommonSize;
232 SD.Flags = RealSD.Flags;
233 SD.Fragment = RealSD.Fragment;
234 SD.Index = RealSD.Index;
235 SD.IsExternal = RealSD.IsExternal;
236 SD.IsPrivateExtern = RealSD.IsPrivateExtern;
237 SD.Offset = RealSD.Offset;
238 SD.SymbolSize = RealSD.SymbolSize;
242 void WinCOFFStreamer::EmitSymbolAttribute(MCSymbol *Symbol,
243 MCSymbolAttr Attribute) {
244 assert(Symbol && "Symbol must be non-null!");
245 assert((Symbol->isInSection()
246 ? Symbol->getSection().getVariant() == MCSection::SV_COFF
247 : true) && "Got non COFF section in the COFF backend!");
249 case MCSA_WeakReference:
251 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
252 SD.modifyFlags(COFF::SF_WeakExternal, COFF::SF_WeakExternal);
253 SD.setExternal(true);
258 getAssembler().getOrCreateSymbolData(*Symbol).setExternal(true);
262 llvm_unreachable("unsupported attribute");
266 void WinCOFFStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
267 llvm_unreachable("not implemented");
270 void WinCOFFStreamer::BeginCOFFSymbolDef(MCSymbol const *Symbol) {
271 assert((Symbol->isInSection()
272 ? Symbol->getSection().getVariant() == MCSection::SV_COFF
273 : true) && "Got non COFF section in the COFF backend!");
274 assert(CurSymbol == NULL && "EndCOFFSymbolDef must be called between calls "
275 "to BeginCOFFSymbolDef!");
279 void WinCOFFStreamer::EmitCOFFSymbolStorageClass(int StorageClass) {
280 assert(CurSymbol != NULL && "BeginCOFFSymbolDef must be called first!");
281 assert((StorageClass & ~0xFF) == 0 && "StorageClass must only have data in "
284 getAssembler().getOrCreateSymbolData(*CurSymbol).modifyFlags(
285 StorageClass << COFF::SF_ClassShift,
289 void WinCOFFStreamer::EmitCOFFSymbolType(int Type) {
290 assert(CurSymbol != NULL && "BeginCOFFSymbolDef must be called first!");
291 assert((Type & ~0xFFFF) == 0 && "Type must only have data in the first 2 "
294 getAssembler().getOrCreateSymbolData(*CurSymbol).modifyFlags(
295 Type << COFF::SF_TypeShift,
299 void WinCOFFStreamer::EndCOFFSymbolDef() {
300 assert(CurSymbol != NULL && "BeginCOFFSymbolDef must be called first!");
304 void WinCOFFStreamer::EmitCOFFSecRel32(MCSymbol const *Symbol)
306 MCDataFragment *DF = getOrCreateDataFragment();
308 DF->getFixups().push_back(
309 MCFixup::Create(DF->getContents().size(),
310 MCSymbolRefExpr::Create (Symbol, getContext ()),
312 DF->getContents().resize(DF->getContents().size() + 4, 0);
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 unsigned ByteAlignment) {
329 assert((Symbol->isInSection()
330 ? Symbol->getSection().getVariant() == MCSection::SV_COFF
331 : true) && "Got non COFF section in the COFF backend!");
332 AddCommonSymbol(Symbol, Size, ByteAlignment, false);
335 void WinCOFFStreamer::EmitZerofill(const MCSection *Section, MCSymbol *Symbol,
336 uint64_t Size,unsigned ByteAlignment) {
337 llvm_unreachable("not implemented");
340 void WinCOFFStreamer::EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol,
341 uint64_t Size, unsigned ByteAlignment) {
342 llvm_unreachable("not implemented");
345 void WinCOFFStreamer::EmitFileDirective(StringRef Filename) {
346 // Ignore for now, linkers don't care, and proper debug
347 // info will be a much large effort.
350 void WinCOFFStreamer::EmitWin64EHHandlerData() {
351 MCStreamer::EmitWin64EHHandlerData();
353 // We have to emit the unwind info now, because this directive
354 // actually switches to the .xdata section!
355 MCWin64EHUnwindEmitter::EmitUnwindInfo(*this, getCurrentW64UnwindInfo());
358 void WinCOFFStreamer::FinishImpl() {
360 MCObjectStreamer::FinishImpl();
365 MCStreamer *createWinCOFFStreamer(MCContext &Context,
370 WinCOFFStreamer *S = new WinCOFFStreamer(Context, MAB, CE, OS);
371 S->getAssembler().setRelaxAll(RelaxAll);