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/Target/TargetRegistry.h"
27 #include "llvm/Target/TargetAsmBackend.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/raw_ostream.h"
37 class WinCOFFStreamer
: public MCObjectStreamer
{
39 MCSymbol
const *CurSymbol
;
41 WinCOFFStreamer(MCContext
&Context
,
42 TargetAsmBackend
&TAB
,
46 void AddCommonSymbol(MCSymbol
*Symbol
, uint64_t Size
,
47 unsigned ByteAlignment
, bool External
);
49 // MCStreamer interface
51 virtual void InitSections();
52 virtual void EmitLabel(MCSymbol
*Symbol
);
53 virtual void EmitAssemblerFlag(MCAssemblerFlag Flag
);
54 virtual void EmitAssignment(MCSymbol
*Symbol
, const MCExpr
*Value
);
55 virtual void EmitSymbolAttribute(MCSymbol
*Symbol
, MCSymbolAttr Attribute
);
56 virtual void EmitSymbolDesc(MCSymbol
*Symbol
, unsigned DescValue
);
57 virtual void BeginCOFFSymbolDef(MCSymbol
const *Symbol
);
58 virtual void EmitCOFFSymbolStorageClass(int StorageClass
);
59 virtual void EmitCOFFSymbolType(int Type
);
60 virtual void EndCOFFSymbolDef();
61 virtual void EmitELFSize(MCSymbol
*Symbol
, const MCExpr
*Value
);
62 virtual void EmitCommonSymbol(MCSymbol
*Symbol
, uint64_t Size
,
63 unsigned ByteAlignment
);
64 virtual void EmitLocalCommonSymbol(MCSymbol
*Symbol
, uint64_t Size
);
65 virtual void EmitZerofill(const MCSection
*Section
, MCSymbol
*Symbol
,
66 unsigned Size
,unsigned ByteAlignment
);
67 virtual void EmitTBSSSymbol(const MCSection
*Section
, MCSymbol
*Symbol
,
68 uint64_t Size
, unsigned ByteAlignment
);
69 virtual void EmitBytes(StringRef Data
, unsigned AddrSpace
);
70 virtual void EmitValue(const MCExpr
*Value
, unsigned Size
,
72 virtual void EmitGPRel32Value(const MCExpr
*Value
);
73 virtual void EmitValueToAlignment(unsigned ByteAlignment
, int64_t Value
,
74 unsigned ValueSize
, unsigned MaxBytesToEmit
);
75 virtual void EmitCodeAlignment(unsigned ByteAlignment
,
76 unsigned MaxBytesToEmit
);
77 virtual void EmitValueToOffset(const MCExpr
*Offset
, unsigned char Value
);
78 virtual void EmitFileDirective(StringRef Filename
);
79 virtual void EmitDwarfFileDirective(unsigned FileNo
,StringRef Filename
);
80 virtual void EmitInstruction(const MCInst
&Instruction
);
81 virtual void Finish();
84 virtual void EmitInstToFragment(const MCInst
&Inst
) {
85 llvm_unreachable("Not used by WinCOFF.");
87 virtual void EmitInstToData(const MCInst
&Inst
) {
88 llvm_unreachable("Not used by WinCOFF.");
91 void SetSection(StringRef Section
,
92 unsigned Characteristics
,
94 SwitchSection(getContext().getCOFFSection(Section
, Characteristics
, Kind
));
97 void SetSectionText() {
99 COFF::IMAGE_SCN_CNT_CODE
100 | COFF::IMAGE_SCN_MEM_EXECUTE
101 | COFF::IMAGE_SCN_MEM_READ
,
102 SectionKind::getText());
103 EmitCodeAlignment(4, 0);
106 void SetSectionData() {
108 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA
109 | COFF::IMAGE_SCN_MEM_READ
110 | COFF::IMAGE_SCN_MEM_WRITE
,
111 SectionKind::getDataRel());
112 EmitCodeAlignment(4, 0);
115 void SetSectionBSS() {
117 COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA
118 | COFF::IMAGE_SCN_MEM_READ
119 | COFF::IMAGE_SCN_MEM_WRITE
,
120 SectionKind::getBSS());
121 EmitCodeAlignment(4, 0);
125 } // end anonymous namespace.
127 WinCOFFStreamer::WinCOFFStreamer(MCContext
&Context
,
128 TargetAsmBackend
&TAB
,
131 : MCObjectStreamer(Context
, TAB
, OS
, &CE
, true)
135 void WinCOFFStreamer::AddCommonSymbol(MCSymbol
*Symbol
, uint64_t Size
,
136 unsigned ByteAlignment
, bool External
) {
137 assert(!Symbol
->isInSection() && "Symbol must not already have a section!");
139 std::string
SectionName(".bss$linkonce");
140 SectionName
.append(Symbol
->getName().begin(), Symbol
->getName().end());
142 MCSymbolData
&SymbolData
= getAssembler().getOrCreateSymbolData(*Symbol
);
144 unsigned Characteristics
=
145 COFF::IMAGE_SCN_LNK_COMDAT
|
146 COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA
|
147 COFF::IMAGE_SCN_MEM_READ
|
148 COFF::IMAGE_SCN_MEM_WRITE
;
150 int Selection
= COFF::IMAGE_COMDAT_SELECT_LARGEST
;
152 const MCSection
*Section
= MCStreamer::getContext().getCOFFSection(
153 SectionName
, Characteristics
, Selection
, SectionKind::getBSS());
155 MCSectionData
&SectionData
= getAssembler().getOrCreateSectionData(*Section
);
157 if (SectionData
.getAlignment() < ByteAlignment
)
158 SectionData
.setAlignment(ByteAlignment
);
160 SymbolData
.setExternal(External
);
162 Symbol
->setSection(*Section
);
164 if (ByteAlignment
!= 1)
165 new MCAlignFragment(ByteAlignment
, 0, 0, ByteAlignment
, &SectionData
);
167 SymbolData
.setFragment(new MCFillFragment(0, 0, Size
, &SectionData
));
170 // MCStreamer interface
172 void WinCOFFStreamer::InitSections() {
179 void WinCOFFStreamer::EmitLabel(MCSymbol
*Symbol
) {
180 // TODO: This is copied almost exactly from the MachOStreamer. Consider
181 // merging into MCObjectStreamer?
182 assert(Symbol
->isUndefined() && "Cannot define a symbol twice!");
183 assert(!Symbol
->isVariable() && "Cannot emit a variable symbol!");
184 assert(CurSection
&& "Cannot emit before setting section!");
186 Symbol
->setSection(*CurSection
);
188 MCSymbolData
&SD
= getAssembler().getOrCreateSymbolData(*Symbol
);
190 // FIXME: This is wasteful, we don't necessarily need to create a data
191 // fragment. Instead, we should mark the symbol as pointing into the data
192 // fragment if it exists, otherwise we should just queue the label and set its
193 // fragment pointer when we emit the next fragment.
194 MCDataFragment
*DF
= getOrCreateDataFragment();
196 assert(!SD
.getFragment() && "Unexpected fragment on symbol data!");
198 SD
.setOffset(DF
->getContents().size());
201 void WinCOFFStreamer::EmitAssemblerFlag(MCAssemblerFlag Flag
) {
202 llvm_unreachable("not implemented");
205 void WinCOFFStreamer::EmitAssignment(MCSymbol
*Symbol
, const MCExpr
*Value
) {
206 assert((Symbol
->isInSection()
207 ? Symbol
->getSection().getVariant() == MCSection::SV_COFF
208 : true) && "Got non COFF section in the COFF backend!");
209 // FIXME: This is all very ugly and depressing. What needs to happen here
210 // depends on quite a few things that are all part of relaxation, which we
211 // don't really even do.
213 if (Value
->getKind() != MCExpr::SymbolRef
) {
214 // TODO: This is exactly the same as MachOStreamer. Consider merging into
216 getAssembler().getOrCreateSymbolData(*Symbol
);
217 AddValueSymbols(Value
);
218 Symbol
->setVariableValue(Value
);
220 // FIXME: This is a horrible way to do this :(. This should really be
221 // handled after we are done with the MC* objects and immediately before
222 // writing out the object file when we know exactly what the symbol should
223 // look like in the coff symbol table. I'm not doing that now because the
224 // COFF object writer doesn't have a clearly defined separation between MC
225 // data structures, the object writers data structures, and the raw, POD,
226 // data structures that get written to disk.
228 // Copy over the aliased data.
229 MCSymbolData
&SD
= getAssembler().getOrCreateSymbolData(*Symbol
);
230 const MCSymbolData
&RealSD
= getAssembler().getOrCreateSymbolData(
231 dyn_cast
<const MCSymbolRefExpr
>(Value
)->getSymbol());
233 // FIXME: This is particularly nasty because it breaks as soon as any data
234 // members of MCSymbolData change.
235 SD
.CommonAlign
= RealSD
.CommonAlign
;
236 SD
.CommonSize
= RealSD
.CommonSize
;
237 SD
.Flags
= RealSD
.Flags
;
238 SD
.Fragment
= RealSD
.Fragment
;
239 SD
.Index
= RealSD
.Index
;
240 SD
.IsExternal
= RealSD
.IsExternal
;
241 SD
.IsPrivateExtern
= RealSD
.IsPrivateExtern
;
242 SD
.Offset
= RealSD
.Offset
;
243 SD
.SymbolSize
= RealSD
.SymbolSize
;
247 void WinCOFFStreamer::EmitSymbolAttribute(MCSymbol
*Symbol
,
248 MCSymbolAttr Attribute
) {
249 assert(Symbol
&& "Symbol must be non-null!");
250 assert((Symbol
->isInSection()
251 ? Symbol
->getSection().getVariant() == MCSection::SV_COFF
252 : true) && "Got non COFF section in the COFF backend!");
254 case MCSA_WeakReference
:
256 MCSymbolData
&SD
= getAssembler().getOrCreateSymbolData(*Symbol
);
257 SD
.modifyFlags(COFF::SF_WeakExternal
, COFF::SF_WeakExternal
);
258 SD
.setExternal(true);
263 getAssembler().getOrCreateSymbolData(*Symbol
).setExternal(true);
267 llvm_unreachable("unsupported attribute");
272 void WinCOFFStreamer::EmitSymbolDesc(MCSymbol
*Symbol
, unsigned DescValue
) {
273 llvm_unreachable("not implemented");
276 void WinCOFFStreamer::BeginCOFFSymbolDef(MCSymbol
const *Symbol
) {
277 assert((Symbol
->isInSection()
278 ? Symbol
->getSection().getVariant() == MCSection::SV_COFF
279 : true) && "Got non COFF section in the COFF backend!");
280 assert(CurSymbol
== NULL
&& "EndCOFFSymbolDef must be called between calls "
281 "to BeginCOFFSymbolDef!");
285 void WinCOFFStreamer::EmitCOFFSymbolStorageClass(int StorageClass
) {
286 assert(CurSymbol
!= NULL
&& "BeginCOFFSymbolDef must be called first!");
287 assert((StorageClass
& ~0xFF) == 0 && "StorageClass must only have data in "
290 getAssembler().getOrCreateSymbolData(*CurSymbol
).modifyFlags(
291 StorageClass
<< COFF::SF_ClassShift
,
295 void WinCOFFStreamer::EmitCOFFSymbolType(int Type
) {
296 assert(CurSymbol
!= NULL
&& "BeginCOFFSymbolDef must be called first!");
297 assert((Type
& ~0xFFFF) == 0 && "Type must only have data in the first 2 "
300 getAssembler().getOrCreateSymbolData(*CurSymbol
).modifyFlags(
301 Type
<< COFF::SF_TypeShift
,
305 void WinCOFFStreamer::EndCOFFSymbolDef() {
306 assert(CurSymbol
!= NULL
&& "BeginCOFFSymbolDef must be called first!");
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 assert((Symbol
->isInSection()
324 ? Symbol
->getSection().getVariant() == MCSection::SV_COFF
325 : true) && "Got non COFF section in the COFF backend!");
326 AddCommonSymbol(Symbol
, Size
, 1, false);
329 void WinCOFFStreamer::EmitZerofill(const MCSection
*Section
, MCSymbol
*Symbol
,
330 unsigned Size
,unsigned ByteAlignment
) {
331 llvm_unreachable("not implemented");
334 void WinCOFFStreamer::EmitTBSSSymbol(const MCSection
*Section
, MCSymbol
*Symbol
,
335 uint64_t Size
, unsigned ByteAlignment
) {
336 llvm_unreachable("not implemented");
339 void WinCOFFStreamer::EmitBytes(StringRef Data
, unsigned AddrSpace
) {
340 // TODO: This is copied exactly from the MachOStreamer. Consider merging into
342 getOrCreateDataFragment()->getContents().append(Data
.begin(), Data
.end());
345 void WinCOFFStreamer::EmitValue(const MCExpr
*Value
, unsigned Size
,
346 unsigned AddrSpace
) {
347 assert(AddrSpace
== 0 && "Address space must be 0!");
349 // TODO: This is copied exactly from the MachOStreamer. Consider merging into
351 MCDataFragment
*DF
= getOrCreateDataFragment();
353 // Avoid fixups when possible.
355 if (AddValueSymbols(Value
)->EvaluateAsAbsolute(AbsValue
)) {
356 // FIXME: Endianness assumption.
357 for (unsigned i
= 0; i
!= Size
; ++i
)
358 DF
->getContents().push_back(uint8_t(AbsValue
>> (i
* 8)));
360 DF
->addFixup(MCFixup::Create(DF
->getContents().size(),
361 AddValueSymbols(Value
),
362 MCFixup::getKindForSize(Size
)));
363 DF
->getContents().resize(DF
->getContents().size() + Size
, 0);
367 void WinCOFFStreamer::EmitGPRel32Value(const MCExpr
*Value
) {
368 llvm_unreachable("not implemented");
371 void WinCOFFStreamer::EmitValueToAlignment(unsigned ByteAlignment
,
374 unsigned MaxBytesToEmit
) {
375 // TODO: This is copied exactly from the MachOStreamer. Consider merging into
377 if (MaxBytesToEmit
== 0)
378 MaxBytesToEmit
= ByteAlignment
;
379 new MCAlignFragment(ByteAlignment
, Value
, ValueSize
, MaxBytesToEmit
,
380 getCurrentSectionData());
382 // Update the maximum alignment on the current section if necessary.
383 if (ByteAlignment
> getCurrentSectionData()->getAlignment())
384 getCurrentSectionData()->setAlignment(ByteAlignment
);
387 void WinCOFFStreamer::EmitCodeAlignment(unsigned ByteAlignment
,
388 unsigned MaxBytesToEmit
) {
389 // TODO: This is copied exactly from the MachOStreamer. Consider merging into
391 if (MaxBytesToEmit
== 0)
392 MaxBytesToEmit
= ByteAlignment
;
393 MCAlignFragment
*F
= new MCAlignFragment(ByteAlignment
, 0, 1, MaxBytesToEmit
,
394 getCurrentSectionData());
395 F
->setEmitNops(true);
397 // Update the maximum alignment on the current section if necessary.
398 if (ByteAlignment
> getCurrentSectionData()->getAlignment())
399 getCurrentSectionData()->setAlignment(ByteAlignment
);
402 void WinCOFFStreamer::EmitValueToOffset(const MCExpr
*Offset
,
403 unsigned char Value
) {
404 llvm_unreachable("not implemented");
407 void WinCOFFStreamer::EmitFileDirective(StringRef Filename
) {
408 // Ignore for now, linkers don't care, and proper debug
409 // info will be a much large effort.
412 void WinCOFFStreamer::EmitDwarfFileDirective(unsigned FileNo
,
413 StringRef Filename
) {
414 llvm_unreachable("not implemented");
417 void WinCOFFStreamer::EmitInstruction(const MCInst
&Instruction
) {
418 for (unsigned i
= 0, e
= Instruction
.getNumOperands(); i
!= e
; ++i
)
419 if (Instruction
.getOperand(i
).isExpr())
420 AddValueSymbols(Instruction
.getOperand(i
).getExpr());
422 getCurrentSectionData()->setHasInstructions(true);
424 MCInstFragment
*Fragment
=
425 new MCInstFragment(Instruction
, getCurrentSectionData());
427 raw_svector_ostream
VecOS(Fragment
->getCode());
429 getAssembler().getEmitter().EncodeInstruction(Instruction
, VecOS
,
430 Fragment
->getFixups());
433 void WinCOFFStreamer::Finish() {
434 MCObjectStreamer::Finish();
439 MCStreamer
*createWinCOFFStreamer(MCContext
&Context
,
440 TargetAsmBackend
&TAB
,
444 WinCOFFStreamer
*S
= new WinCOFFStreamer(Context
, TAB
, CE
, OS
);
445 S
->getAssembler().setRelaxAll(RelaxAll
);