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/MC/MCWin64EH.h"
27 #include "llvm/MC/MCAsmBackend.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/TargetRegistry.h"
34 #include "llvm/Support/raw_ostream.h"
36 #include "../Target/X86/MCTargetDesc/X86FixupKinds.h"
41 class WinCOFFStreamer : public MCObjectStreamer {
43 MCSymbol const *CurSymbol;
45 WinCOFFStreamer(MCContext &Context,
50 void AddCommonSymbol(MCSymbol *Symbol, uint64_t Size,
51 unsigned ByteAlignment, bool External);
53 // MCStreamer interface
55 virtual void InitSections();
56 virtual void EmitLabel(MCSymbol *Symbol);
57 virtual void EmitAssemblerFlag(MCAssemblerFlag Flag);
58 virtual void EmitThumbFunc(MCSymbol *Func);
59 virtual void EmitAssignment(MCSymbol *Symbol, const MCExpr *Value);
60 virtual void EmitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute);
61 virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue);
62 virtual void BeginCOFFSymbolDef(MCSymbol const *Symbol);
63 virtual void EmitCOFFSymbolStorageClass(int StorageClass);
64 virtual void EmitCOFFSymbolType(int Type);
65 virtual void EndCOFFSymbolDef();
66 virtual void EmitCOFFSecRel32(MCSymbol const *Symbol);
67 virtual void EmitELFSize(MCSymbol *Symbol, const MCExpr *Value);
68 virtual void EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
69 unsigned ByteAlignment);
70 virtual void EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size,
71 unsigned ByteAlignment);
72 virtual void EmitZerofill(const MCSection *Section, MCSymbol *Symbol,
73 unsigned Size,unsigned ByteAlignment);
74 virtual void EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol,
75 uint64_t Size, unsigned ByteAlignment);
76 virtual void EmitBytes(StringRef Data, unsigned AddrSpace);
77 virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value,
78 unsigned ValueSize, unsigned MaxBytesToEmit);
79 virtual void EmitCodeAlignment(unsigned ByteAlignment,
80 unsigned MaxBytesToEmit);
81 virtual void EmitFileDirective(StringRef Filename);
82 virtual void EmitInstruction(const MCInst &Instruction);
83 virtual void EmitWin64EHHandlerData();
84 virtual void Finish();
87 virtual void EmitInstToFragment(const MCInst &Inst) {
88 llvm_unreachable("Not used by WinCOFF.");
90 virtual void EmitInstToData(const MCInst &Inst) {
91 llvm_unreachable("Not used by WinCOFF.");
94 void SetSection(StringRef Section,
95 unsigned Characteristics,
97 SwitchSection(getContext().getCOFFSection(Section, Characteristics, Kind));
100 void SetSectionText() {
102 COFF::IMAGE_SCN_CNT_CODE
103 | COFF::IMAGE_SCN_MEM_EXECUTE
104 | COFF::IMAGE_SCN_MEM_READ,
105 SectionKind::getText());
106 EmitCodeAlignment(4, 0);
109 void SetSectionData() {
111 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA
112 | COFF::IMAGE_SCN_MEM_READ
113 | COFF::IMAGE_SCN_MEM_WRITE,
114 SectionKind::getDataRel());
115 EmitCodeAlignment(4, 0);
118 void SetSectionBSS() {
120 COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA
121 | COFF::IMAGE_SCN_MEM_READ
122 | COFF::IMAGE_SCN_MEM_WRITE,
123 SectionKind::getBSS());
124 EmitCodeAlignment(4, 0);
128 } // end anonymous namespace.
130 WinCOFFStreamer::WinCOFFStreamer(MCContext &Context,
134 : MCObjectStreamer(Context, MAB, OS, &CE)
138 void WinCOFFStreamer::AddCommonSymbol(MCSymbol *Symbol, uint64_t Size,
139 unsigned ByteAlignment, bool External) {
140 assert(!Symbol->isInSection() && "Symbol must not already have a section!");
142 std::string SectionName(".bss$linkonce");
143 SectionName.append(Symbol->getName().begin(), Symbol->getName().end());
145 MCSymbolData &SymbolData = getAssembler().getOrCreateSymbolData(*Symbol);
147 unsigned Characteristics =
148 COFF::IMAGE_SCN_LNK_COMDAT |
149 COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA |
150 COFF::IMAGE_SCN_MEM_READ |
151 COFF::IMAGE_SCN_MEM_WRITE;
153 int Selection = COFF::IMAGE_COMDAT_SELECT_LARGEST;
155 const MCSection *Section = MCStreamer::getContext().getCOFFSection(
156 SectionName, Characteristics, Selection, SectionKind::getBSS());
158 MCSectionData &SectionData = getAssembler().getOrCreateSectionData(*Section);
160 if (SectionData.getAlignment() < ByteAlignment)
161 SectionData.setAlignment(ByteAlignment);
163 SymbolData.setExternal(External);
165 Symbol->setSection(*Section);
167 if (ByteAlignment != 1)
168 new MCAlignFragment(ByteAlignment, 0, 0, ByteAlignment, &SectionData);
170 SymbolData.setFragment(new MCFillFragment(0, 0, Size, &SectionData));
173 // MCStreamer interface
175 void WinCOFFStreamer::InitSections() {
182 void WinCOFFStreamer::EmitLabel(MCSymbol *Symbol) {
183 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
184 MCObjectStreamer::EmitLabel(Symbol);
187 void WinCOFFStreamer::EmitAssemblerFlag(MCAssemblerFlag Flag) {
188 llvm_unreachable("not implemented");
191 void WinCOFFStreamer::EmitThumbFunc(MCSymbol *Func) {
192 llvm_unreachable("not implemented");
195 void WinCOFFStreamer::EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) {
196 assert((Symbol->isInSection()
197 ? Symbol->getSection().getVariant() == MCSection::SV_COFF
198 : true) && "Got non COFF section in the COFF backend!");
199 // FIXME: This is all very ugly and depressing. What needs to happen here
200 // depends on quite a few things that are all part of relaxation, which we
201 // don't really even do.
203 if (Value->getKind() != MCExpr::SymbolRef) {
204 // TODO: This is exactly the same as MachOStreamer. Consider merging into
206 getAssembler().getOrCreateSymbolData(*Symbol);
207 AddValueSymbols(Value);
208 Symbol->setVariableValue(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");
262 void WinCOFFStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
263 llvm_unreachable("not implemented");
266 void WinCOFFStreamer::BeginCOFFSymbolDef(MCSymbol const *Symbol) {
267 assert((Symbol->isInSection()
268 ? Symbol->getSection().getVariant() == MCSection::SV_COFF
269 : true) && "Got non COFF section in the COFF backend!");
270 assert(CurSymbol == NULL && "EndCOFFSymbolDef must be called between calls "
271 "to BeginCOFFSymbolDef!");
275 void WinCOFFStreamer::EmitCOFFSymbolStorageClass(int StorageClass) {
276 assert(CurSymbol != NULL && "BeginCOFFSymbolDef must be called first!");
277 assert((StorageClass & ~0xFF) == 0 && "StorageClass must only have data in "
280 getAssembler().getOrCreateSymbolData(*CurSymbol).modifyFlags(
281 StorageClass << COFF::SF_ClassShift,
285 void WinCOFFStreamer::EmitCOFFSymbolType(int Type) {
286 assert(CurSymbol != NULL && "BeginCOFFSymbolDef must be called first!");
287 assert((Type & ~0xFFFF) == 0 && "Type must only have data in the first 2 "
290 getAssembler().getOrCreateSymbolData(*CurSymbol).modifyFlags(
291 Type << COFF::SF_TypeShift,
295 void WinCOFFStreamer::EndCOFFSymbolDef() {
296 assert(CurSymbol != NULL && "BeginCOFFSymbolDef must be called first!");
300 void WinCOFFStreamer::EmitCOFFSecRel32(MCSymbol const *Symbol)
302 MCDataFragment *DF = getOrCreateDataFragment();
304 DF->addFixup(MCFixup::Create(DF->getContents().size(),
305 MCSymbolRefExpr::Create (Symbol, getContext ()),
306 (MCFixupKind)X86::reloc_coff_secrel32));
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 unsigned 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::EmitBytes(StringRef Data, unsigned AddrSpace) {
341 // TODO: This is copied exactly from the MachOStreamer. Consider merging into
343 getOrCreateDataFragment()->getContents().append(Data.begin(), Data.end());
346 void WinCOFFStreamer::EmitValueToAlignment(unsigned ByteAlignment,
349 unsigned MaxBytesToEmit) {
350 // TODO: This is copied exactly from the MachOStreamer. Consider merging into
352 if (MaxBytesToEmit == 0)
353 MaxBytesToEmit = ByteAlignment;
354 new MCAlignFragment(ByteAlignment, Value, ValueSize, MaxBytesToEmit,
355 getCurrentSectionData());
357 // Update the maximum alignment on the current section if necessary.
358 if (ByteAlignment > getCurrentSectionData()->getAlignment())
359 getCurrentSectionData()->setAlignment(ByteAlignment);
362 void WinCOFFStreamer::EmitCodeAlignment(unsigned ByteAlignment,
363 unsigned MaxBytesToEmit) {
364 // TODO: This is copied exactly from the MachOStreamer. Consider merging into
366 if (MaxBytesToEmit == 0)
367 MaxBytesToEmit = ByteAlignment;
368 MCAlignFragment *F = new MCAlignFragment(ByteAlignment, 0, 1, MaxBytesToEmit,
369 getCurrentSectionData());
370 F->setEmitNops(true);
372 // Update the maximum alignment on the current section if necessary.
373 if (ByteAlignment > getCurrentSectionData()->getAlignment())
374 getCurrentSectionData()->setAlignment(ByteAlignment);
377 void WinCOFFStreamer::EmitFileDirective(StringRef Filename) {
378 // Ignore for now, linkers don't care, and proper debug
379 // info will be a much large effort.
382 void WinCOFFStreamer::EmitInstruction(const MCInst &Instruction) {
383 for (unsigned i = 0, e = Instruction.getNumOperands(); i != e; ++i)
384 if (Instruction.getOperand(i).isExpr())
385 AddValueSymbols(Instruction.getOperand(i).getExpr());
387 getCurrentSectionData()->setHasInstructions(true);
389 MCInstFragment *Fragment =
390 new MCInstFragment(Instruction, getCurrentSectionData());
392 raw_svector_ostream VecOS(Fragment->getCode());
394 getAssembler().getEmitter().EncodeInstruction(Instruction, VecOS,
395 Fragment->getFixups());
398 void WinCOFFStreamer::EmitWin64EHHandlerData() {
399 MCStreamer::EmitWin64EHHandlerData();
401 // We have to emit the unwind info now, because this directive
402 // actually switches to the .xdata section!
403 MCWin64EHUnwindEmitter::EmitUnwindInfo(*this, getCurrentW64UnwindInfo());
406 void WinCOFFStreamer::Finish() {
408 MCObjectStreamer::Finish();
413 MCStreamer *createWinCOFFStreamer(MCContext &Context,
418 WinCOFFStreamer *S = new WinCOFFStreamer(Context, MAB, CE, OS);
419 S->getAssembler().setRelaxAll(RelaxAll);