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/Target/TargetRegistry.h"
28 #include "llvm/Target/TargetAsmBackend.h"
29 #include "llvm/ADT/StringMap.h"
31 #include "llvm/Support/COFF.h"
32 #include "llvm/Support/Debug.h"
33 #include "llvm/Support/ErrorHandling.h"
34 #include "llvm/Support/raw_ostream.h"
38 class WinCOFFStreamer
: public MCObjectStreamer
{
40 MCSymbol
const *CurSymbol
;
42 WinCOFFStreamer(MCContext
&Context
,
43 TargetAsmBackend
&TAB
,
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 EmitELFSize(MCSymbol
*Symbol
, const MCExpr
*Value
);
64 virtual void EmitCommonSymbol(MCSymbol
*Symbol
, uint64_t Size
,
65 unsigned ByteAlignment
);
66 virtual void EmitLocalCommonSymbol(MCSymbol
*Symbol
, uint64_t Size
);
67 virtual void EmitZerofill(const MCSection
*Section
, MCSymbol
*Symbol
,
68 unsigned Size
,unsigned ByteAlignment
);
69 virtual void EmitTBSSSymbol(const MCSection
*Section
, MCSymbol
*Symbol
,
70 uint64_t Size
, unsigned ByteAlignment
);
71 virtual void EmitBytes(StringRef Data
, unsigned AddrSpace
);
72 virtual void EmitValueToAlignment(unsigned ByteAlignment
, int64_t Value
,
73 unsigned ValueSize
, unsigned MaxBytesToEmit
);
74 virtual void EmitCodeAlignment(unsigned ByteAlignment
,
75 unsigned MaxBytesToEmit
);
76 virtual void EmitFileDirective(StringRef Filename
);
77 virtual void EmitInstruction(const MCInst
&Instruction
);
78 virtual void EmitWin64EHHandlerData();
79 virtual void Finish();
82 virtual void EmitInstToFragment(const MCInst
&Inst
) {
83 llvm_unreachable("Not used by WinCOFF.");
85 virtual void EmitInstToData(const MCInst
&Inst
) {
86 llvm_unreachable("Not used by WinCOFF.");
89 void SetSection(StringRef Section
,
90 unsigned Characteristics
,
92 SwitchSection(getContext().getCOFFSection(Section
, Characteristics
, Kind
));
95 void SetSectionText() {
97 COFF::IMAGE_SCN_CNT_CODE
98 | COFF::IMAGE_SCN_MEM_EXECUTE
99 | COFF::IMAGE_SCN_MEM_READ
,
100 SectionKind::getText());
101 EmitCodeAlignment(4, 0);
104 void SetSectionData() {
106 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA
107 | COFF::IMAGE_SCN_MEM_READ
108 | COFF::IMAGE_SCN_MEM_WRITE
,
109 SectionKind::getDataRel());
110 EmitCodeAlignment(4, 0);
113 void SetSectionBSS() {
115 COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA
116 | COFF::IMAGE_SCN_MEM_READ
117 | COFF::IMAGE_SCN_MEM_WRITE
,
118 SectionKind::getBSS());
119 EmitCodeAlignment(4, 0);
123 } // end anonymous namespace.
125 WinCOFFStreamer::WinCOFFStreamer(MCContext
&Context
,
126 TargetAsmBackend
&TAB
,
129 : MCObjectStreamer(Context
, TAB
, OS
, &CE
)
133 void WinCOFFStreamer::AddCommonSymbol(MCSymbol
*Symbol
, uint64_t Size
,
134 unsigned ByteAlignment
, bool External
) {
135 assert(!Symbol
->isInSection() && "Symbol must not already have a section!");
137 std::string
SectionName(".bss$linkonce");
138 SectionName
.append(Symbol
->getName().begin(), Symbol
->getName().end());
140 MCSymbolData
&SymbolData
= getAssembler().getOrCreateSymbolData(*Symbol
);
142 unsigned Characteristics
=
143 COFF::IMAGE_SCN_LNK_COMDAT
|
144 COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA
|
145 COFF::IMAGE_SCN_MEM_READ
|
146 COFF::IMAGE_SCN_MEM_WRITE
;
148 int Selection
= COFF::IMAGE_COMDAT_SELECT_LARGEST
;
150 const MCSection
*Section
= MCStreamer::getContext().getCOFFSection(
151 SectionName
, Characteristics
, Selection
, SectionKind::getBSS());
153 MCSectionData
&SectionData
= getAssembler().getOrCreateSectionData(*Section
);
155 if (SectionData
.getAlignment() < ByteAlignment
)
156 SectionData
.setAlignment(ByteAlignment
);
158 SymbolData
.setExternal(External
);
160 Symbol
->setSection(*Section
);
162 if (ByteAlignment
!= 1)
163 new MCAlignFragment(ByteAlignment
, 0, 0, ByteAlignment
, &SectionData
);
165 SymbolData
.setFragment(new MCFillFragment(0, 0, Size
, &SectionData
));
168 // MCStreamer interface
170 void WinCOFFStreamer::InitSections() {
177 void WinCOFFStreamer::EmitLabel(MCSymbol
*Symbol
) {
178 assert(Symbol
->isUndefined() && "Cannot define a symbol twice!");
179 MCObjectStreamer::EmitLabel(Symbol
);
182 void WinCOFFStreamer::EmitAssemblerFlag(MCAssemblerFlag Flag
) {
183 llvm_unreachable("not implemented");
186 void WinCOFFStreamer::EmitThumbFunc(MCSymbol
*Func
) {
187 llvm_unreachable("not implemented");
190 void WinCOFFStreamer::EmitAssignment(MCSymbol
*Symbol
, const MCExpr
*Value
) {
191 assert((Symbol
->isInSection()
192 ? Symbol
->getSection().getVariant() == MCSection::SV_COFF
193 : true) && "Got non COFF section in the COFF backend!");
194 // FIXME: This is all very ugly and depressing. What needs to happen here
195 // depends on quite a few things that are all part of relaxation, which we
196 // don't really even do.
198 if (Value
->getKind() != MCExpr::SymbolRef
) {
199 // TODO: This is exactly the same as MachOStreamer. Consider merging into
201 getAssembler().getOrCreateSymbolData(*Symbol
);
202 AddValueSymbols(Value
);
203 Symbol
->setVariableValue(Value
);
205 // FIXME: This is a horrible way to do this :(. This should really be
206 // handled after we are done with the MC* objects and immediately before
207 // writing out the object file when we know exactly what the symbol should
208 // look like in the coff symbol table. I'm not doing that now because the
209 // COFF object writer doesn't have a clearly defined separation between MC
210 // data structures, the object writers data structures, and the raw, POD,
211 // data structures that get written to disk.
213 // Copy over the aliased data.
214 MCSymbolData
&SD
= getAssembler().getOrCreateSymbolData(*Symbol
);
215 const MCSymbolData
&RealSD
= getAssembler().getOrCreateSymbolData(
216 dyn_cast
<const MCSymbolRefExpr
>(Value
)->getSymbol());
218 // FIXME: This is particularly nasty because it breaks as soon as any data
219 // members of MCSymbolData change.
220 SD
.CommonAlign
= RealSD
.CommonAlign
;
221 SD
.CommonSize
= RealSD
.CommonSize
;
222 SD
.Flags
= RealSD
.Flags
;
223 SD
.Fragment
= RealSD
.Fragment
;
224 SD
.Index
= RealSD
.Index
;
225 SD
.IsExternal
= RealSD
.IsExternal
;
226 SD
.IsPrivateExtern
= RealSD
.IsPrivateExtern
;
227 SD
.Offset
= RealSD
.Offset
;
228 SD
.SymbolSize
= RealSD
.SymbolSize
;
232 void WinCOFFStreamer::EmitSymbolAttribute(MCSymbol
*Symbol
,
233 MCSymbolAttr Attribute
) {
234 assert(Symbol
&& "Symbol must be non-null!");
235 assert((Symbol
->isInSection()
236 ? Symbol
->getSection().getVariant() == MCSection::SV_COFF
237 : true) && "Got non COFF section in the COFF backend!");
239 case MCSA_WeakReference
:
241 MCSymbolData
&SD
= getAssembler().getOrCreateSymbolData(*Symbol
);
242 SD
.modifyFlags(COFF::SF_WeakExternal
, COFF::SF_WeakExternal
);
243 SD
.setExternal(true);
248 getAssembler().getOrCreateSymbolData(*Symbol
).setExternal(true);
252 llvm_unreachable("unsupported attribute");
257 void WinCOFFStreamer::EmitSymbolDesc(MCSymbol
*Symbol
, unsigned DescValue
) {
258 llvm_unreachable("not implemented");
261 void WinCOFFStreamer::BeginCOFFSymbolDef(MCSymbol
const *Symbol
) {
262 assert((Symbol
->isInSection()
263 ? Symbol
->getSection().getVariant() == MCSection::SV_COFF
264 : true) && "Got non COFF section in the COFF backend!");
265 assert(CurSymbol
== NULL
&& "EndCOFFSymbolDef must be called between calls "
266 "to BeginCOFFSymbolDef!");
270 void WinCOFFStreamer::EmitCOFFSymbolStorageClass(int StorageClass
) {
271 assert(CurSymbol
!= NULL
&& "BeginCOFFSymbolDef must be called first!");
272 assert((StorageClass
& ~0xFF) == 0 && "StorageClass must only have data in "
275 getAssembler().getOrCreateSymbolData(*CurSymbol
).modifyFlags(
276 StorageClass
<< COFF::SF_ClassShift
,
280 void WinCOFFStreamer::EmitCOFFSymbolType(int Type
) {
281 assert(CurSymbol
!= NULL
&& "BeginCOFFSymbolDef must be called first!");
282 assert((Type
& ~0xFFFF) == 0 && "Type must only have data in the first 2 "
285 getAssembler().getOrCreateSymbolData(*CurSymbol
).modifyFlags(
286 Type
<< COFF::SF_TypeShift
,
290 void WinCOFFStreamer::EndCOFFSymbolDef() {
291 assert(CurSymbol
!= NULL
&& "BeginCOFFSymbolDef must be called first!");
295 void WinCOFFStreamer::EmitELFSize(MCSymbol
*Symbol
, const MCExpr
*Value
) {
296 llvm_unreachable("not implemented");
299 void WinCOFFStreamer::EmitCommonSymbol(MCSymbol
*Symbol
, uint64_t Size
,
300 unsigned ByteAlignment
) {
301 assert((Symbol
->isInSection()
302 ? Symbol
->getSection().getVariant() == MCSection::SV_COFF
303 : true) && "Got non COFF section in the COFF backend!");
304 AddCommonSymbol(Symbol
, Size
, ByteAlignment
, true);
307 void WinCOFFStreamer::EmitLocalCommonSymbol(MCSymbol
*Symbol
, uint64_t Size
) {
308 assert((Symbol
->isInSection()
309 ? Symbol
->getSection().getVariant() == MCSection::SV_COFF
310 : true) && "Got non COFF section in the COFF backend!");
311 AddCommonSymbol(Symbol
, Size
, 1, false);
314 void WinCOFFStreamer::EmitZerofill(const MCSection
*Section
, MCSymbol
*Symbol
,
315 unsigned Size
,unsigned ByteAlignment
) {
316 llvm_unreachable("not implemented");
319 void WinCOFFStreamer::EmitTBSSSymbol(const MCSection
*Section
, MCSymbol
*Symbol
,
320 uint64_t Size
, unsigned ByteAlignment
) {
321 llvm_unreachable("not implemented");
324 void WinCOFFStreamer::EmitBytes(StringRef Data
, unsigned AddrSpace
) {
325 // TODO: This is copied exactly from the MachOStreamer. Consider merging into
327 getOrCreateDataFragment()->getContents().append(Data
.begin(), Data
.end());
330 void WinCOFFStreamer::EmitValueToAlignment(unsigned ByteAlignment
,
333 unsigned MaxBytesToEmit
) {
334 // TODO: This is copied exactly from the MachOStreamer. Consider merging into
336 if (MaxBytesToEmit
== 0)
337 MaxBytesToEmit
= ByteAlignment
;
338 new MCAlignFragment(ByteAlignment
, Value
, ValueSize
, MaxBytesToEmit
,
339 getCurrentSectionData());
341 // Update the maximum alignment on the current section if necessary.
342 if (ByteAlignment
> getCurrentSectionData()->getAlignment())
343 getCurrentSectionData()->setAlignment(ByteAlignment
);
346 void WinCOFFStreamer::EmitCodeAlignment(unsigned ByteAlignment
,
347 unsigned MaxBytesToEmit
) {
348 // TODO: This is copied exactly from the MachOStreamer. Consider merging into
350 if (MaxBytesToEmit
== 0)
351 MaxBytesToEmit
= ByteAlignment
;
352 MCAlignFragment
*F
= new MCAlignFragment(ByteAlignment
, 0, 1, MaxBytesToEmit
,
353 getCurrentSectionData());
354 F
->setEmitNops(true);
356 // Update the maximum alignment on the current section if necessary.
357 if (ByteAlignment
> getCurrentSectionData()->getAlignment())
358 getCurrentSectionData()->setAlignment(ByteAlignment
);
361 void WinCOFFStreamer::EmitFileDirective(StringRef Filename
) {
362 // Ignore for now, linkers don't care, and proper debug
363 // info will be a much large effort.
366 void WinCOFFStreamer::EmitInstruction(const MCInst
&Instruction
) {
367 for (unsigned i
= 0, e
= Instruction
.getNumOperands(); i
!= e
; ++i
)
368 if (Instruction
.getOperand(i
).isExpr())
369 AddValueSymbols(Instruction
.getOperand(i
).getExpr());
371 getCurrentSectionData()->setHasInstructions(true);
373 MCInstFragment
*Fragment
=
374 new MCInstFragment(Instruction
, getCurrentSectionData());
376 raw_svector_ostream
VecOS(Fragment
->getCode());
378 getAssembler().getEmitter().EncodeInstruction(Instruction
, VecOS
,
379 Fragment
->getFixups());
382 void WinCOFFStreamer::EmitWin64EHHandlerData() {
383 MCStreamer::EmitWin64EHHandlerData();
385 // We have to emit the unwind info now, because this directive
386 // actually switches to the .xdata section!
387 MCWin64EHUnwindEmitter::EmitUnwindInfo(*this, getCurrentW64UnwindInfo());
390 void WinCOFFStreamer::Finish() {
392 MCObjectStreamer::Finish();
397 MCStreamer
*createWinCOFFStreamer(MCContext
&Context
,
398 TargetAsmBackend
&TAB
,
402 WinCOFFStreamer
*S
= new WinCOFFStreamer(Context
, TAB
, CE
, OS
);
403 S
->getAssembler().setRelaxAll(RelaxAll
);