2010-07-11 22:05:00 +00:00
|
|
|
//===-- llvm/MC/WinCOFFStreamer.cpp -----------------------------*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file contains an implementation of a Win32 COFF object file streamer.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2012-12-03 16:50:05 +00:00
|
|
|
#include "llvm/MC/MCStreamer.h"
|
|
|
|
#include "llvm/MC/MCAsmBackend.h"
|
|
|
|
#include "llvm/MC/MCAsmLayout.h"
|
|
|
|
#include "llvm/MC/MCAssembler.h"
|
|
|
|
#include "llvm/MC/MCCodeEmitter.h"
|
2010-07-11 22:05:00 +00:00
|
|
|
#include "llvm/MC/MCContext.h"
|
2012-12-03 16:50:05 +00:00
|
|
|
#include "llvm/MC/MCExpr.h"
|
2014-01-23 22:49:25 +00:00
|
|
|
#include "llvm/MC/MCObjectFileInfo.h"
|
2012-12-03 16:50:05 +00:00
|
|
|
#include "llvm/MC/MCObjectStreamer.h"
|
2010-07-11 22:05:00 +00:00
|
|
|
#include "llvm/MC/MCSection.h"
|
2012-12-03 16:50:05 +00:00
|
|
|
#include "llvm/MC/MCSectionCOFF.h"
|
2010-07-11 22:05:00 +00:00
|
|
|
#include "llvm/MC/MCSymbol.h"
|
2010-07-19 06:13:10 +00:00
|
|
|
#include "llvm/MC/MCValue.h"
|
2011-05-22 03:01:05 +00:00
|
|
|
#include "llvm/MC/MCWin64EH.h"
|
2010-07-11 22:05:00 +00:00
|
|
|
#include "llvm/Support/COFF.h"
|
|
|
|
#include "llvm/Support/Debug.h"
|
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
2011-08-24 18:08:43 +00:00
|
|
|
#include "llvm/Support/TargetRegistry.h"
|
2010-07-11 22:05:00 +00:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2011-12-17 01:14:52 +00:00
|
|
|
|
2010-07-11 22:05:00 +00:00
|
|
|
using namespace llvm;
|
|
|
|
|
2014-04-22 03:04:17 +00:00
|
|
|
#define DEBUG_TYPE "WinCOFFStreamer"
|
|
|
|
|
2010-07-11 22:05:00 +00:00
|
|
|
namespace {
|
|
|
|
class WinCOFFStreamer : public MCObjectStreamer {
|
|
|
|
public:
|
2010-07-19 06:13:10 +00:00
|
|
|
MCSymbol const *CurSymbol;
|
|
|
|
|
2010-07-11 22:05:00 +00:00
|
|
|
WinCOFFStreamer(MCContext &Context,
|
2011-07-25 23:24:55 +00:00
|
|
|
MCAsmBackend &MAB,
|
2010-07-11 22:05:00 +00:00
|
|
|
MCCodeEmitter &CE,
|
|
|
|
raw_ostream &OS);
|
|
|
|
|
|
|
|
// MCStreamer interface
|
|
|
|
|
2014-03-08 07:02:02 +00:00
|
|
|
void InitSections() override;
|
|
|
|
void EmitLabel(MCSymbol *Symbol) override;
|
|
|
|
void EmitDebugLabel(MCSymbol *Symbol) override;
|
|
|
|
void EmitAssemblerFlag(MCAssemblerFlag Flag) override;
|
|
|
|
void EmitThumbFunc(MCSymbol *Func) override;
|
|
|
|
bool EmitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute) override;
|
|
|
|
void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) override;
|
|
|
|
void BeginCOFFSymbolDef(MCSymbol const *Symbol) override;
|
|
|
|
void EmitCOFFSymbolStorageClass(int StorageClass) override;
|
|
|
|
void EmitCOFFSymbolType(int Type) override;
|
|
|
|
void EndCOFFSymbolDef() override;
|
|
|
|
void EmitCOFFSectionIndex(MCSymbol const *Symbol) override;
|
|
|
|
void EmitCOFFSecRel32(MCSymbol const *Symbol) override;
|
|
|
|
void EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) override;
|
|
|
|
void EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
|
|
|
|
unsigned ByteAlignment) override;
|
|
|
|
void EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size,
|
|
|
|
unsigned ByteAlignment) override;
|
|
|
|
void EmitZerofill(const MCSection *Section, MCSymbol *Symbol,
|
|
|
|
uint64_t Size,unsigned ByteAlignment) override;
|
|
|
|
void EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol,
|
|
|
|
uint64_t Size, unsigned ByteAlignment) override;
|
|
|
|
void EmitFileDirective(StringRef Filename) override;
|
|
|
|
void EmitIdent(StringRef IdentString) override;
|
|
|
|
void EmitWin64EHHandlerData() override;
|
|
|
|
void FinishImpl() override;
|
2010-10-09 15:44:27 +00:00
|
|
|
|
|
|
|
private:
|
2014-03-08 07:02:02 +00:00
|
|
|
void EmitInstToData(const MCInst &Inst, const MCSubtargetInfo &STI) override {
|
2013-01-03 01:09:22 +00:00
|
|
|
MCDataFragment *DF = getOrCreateDataFragment();
|
|
|
|
|
|
|
|
SmallVector<MCFixup, 4> Fixups;
|
|
|
|
SmallString<256> Code;
|
|
|
|
raw_svector_ostream VecOS(Code);
|
2014-01-28 23:13:07 +00:00
|
|
|
getAssembler().getEmitter().EncodeInstruction(Inst, VecOS, Fixups, STI);
|
2013-01-03 01:09:22 +00:00
|
|
|
VecOS.flush();
|
|
|
|
|
|
|
|
// Add the fixups and data.
|
|
|
|
for (unsigned i = 0, e = Fixups.size(); i != e; ++i) {
|
|
|
|
Fixups[i].setOffset(Fixups[i].getOffset() + DF->getContents().size());
|
|
|
|
DF->getFixups().push_back(Fixups[i]);
|
|
|
|
}
|
|
|
|
DF->getContents().append(Code.begin(), Code.end());
|
2010-11-01 16:27:31 +00:00
|
|
|
}
|
2010-07-11 22:05:00 +00:00
|
|
|
};
|
|
|
|
} // end anonymous namespace.
|
|
|
|
|
2013-01-31 23:29:57 +00:00
|
|
|
WinCOFFStreamer::WinCOFFStreamer(MCContext &Context, MCAsmBackend &MAB,
|
|
|
|
MCCodeEmitter &CE, raw_ostream &OS)
|
2014-04-13 04:57:38 +00:00
|
|
|
: MCObjectStreamer(Context, MAB, OS, &CE), CurSymbol(nullptr) {}
|
2010-07-19 06:13:10 +00:00
|
|
|
|
2010-07-11 22:05:00 +00:00
|
|
|
// MCStreamer interface
|
|
|
|
|
2014-03-05 20:09:15 +00:00
|
|
|
void WinCOFFStreamer::InitSections() {
|
2014-01-24 02:28:11 +00:00
|
|
|
// FIXME: this is identical to the ELF one.
|
|
|
|
// This emulates the same behavior of GNU as. This makes it easier
|
|
|
|
// to compare the output as the major sections are in the same order.
|
|
|
|
SwitchSection(getContext().getObjectFileInfo()->getTextSection());
|
2014-02-04 18:34:04 +00:00
|
|
|
EmitCodeAlignment(4);
|
2014-01-24 02:28:11 +00:00
|
|
|
|
|
|
|
SwitchSection(getContext().getObjectFileInfo()->getDataSection());
|
2014-02-04 18:34:04 +00:00
|
|
|
EmitCodeAlignment(4);
|
2014-01-24 02:28:11 +00:00
|
|
|
|
|
|
|
SwitchSection(getContext().getObjectFileInfo()->getBSSSection());
|
2014-02-04 18:34:04 +00:00
|
|
|
EmitCodeAlignment(4);
|
2014-01-24 02:28:11 +00:00
|
|
|
|
|
|
|
SwitchSection(getContext().getObjectFileInfo()->getTextSection());
|
2010-09-15 21:48:40 +00:00
|
|
|
}
|
|
|
|
|
2010-11-28 16:22:59 +00:00
|
|
|
void WinCOFFStreamer::EmitLabel(MCSymbol *Symbol) {
|
|
|
|
assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
|
2010-11-28 17:18:55 +00:00
|
|
|
MCObjectStreamer::EmitLabel(Symbol);
|
2010-11-28 16:22:59 +00:00
|
|
|
}
|
|
|
|
|
2012-12-16 04:00:45 +00:00
|
|
|
void WinCOFFStreamer::EmitDebugLabel(MCSymbol *Symbol) {
|
|
|
|
EmitLabel(Symbol);
|
|
|
|
}
|
2010-07-11 22:05:00 +00:00
|
|
|
void WinCOFFStreamer::EmitAssemblerFlag(MCAssemblerFlag Flag) {
|
2010-07-19 06:13:10 +00:00
|
|
|
llvm_unreachable("not implemented");
|
2010-07-11 22:05:00 +00:00
|
|
|
}
|
|
|
|
|
2010-11-05 22:08:08 +00:00
|
|
|
void WinCOFFStreamer::EmitThumbFunc(MCSymbol *Func) {
|
|
|
|
llvm_unreachable("not implemented");
|
|
|
|
}
|
|
|
|
|
2013-08-09 01:52:03 +00:00
|
|
|
bool WinCOFFStreamer::EmitSymbolAttribute(MCSymbol *Symbol,
|
2010-07-11 22:05:00 +00:00
|
|
|
MCSymbolAttr Attribute) {
|
2010-10-09 11:00:37 +00:00
|
|
|
assert(Symbol && "Symbol must be non-null!");
|
|
|
|
assert((Symbol->isInSection()
|
|
|
|
? Symbol->getSection().getVariant() == MCSection::SV_COFF
|
2013-12-05 05:44:44 +00:00
|
|
|
: true) && "Got non-COFF section in the COFF backend!");
|
2010-07-19 06:13:10 +00:00
|
|
|
switch (Attribute) {
|
|
|
|
case MCSA_WeakReference:
|
2010-10-16 08:25:57 +00:00
|
|
|
case MCSA_Weak: {
|
|
|
|
MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
|
|
|
|
SD.modifyFlags(COFF::SF_WeakExternal, COFF::SF_WeakExternal);
|
|
|
|
SD.setExternal(true);
|
|
|
|
}
|
2010-07-19 06:13:10 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case MCSA_Global:
|
|
|
|
getAssembler().getOrCreateSymbolData(*Symbol).setExternal(true);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2013-08-09 01:52:03 +00:00
|
|
|
return false;
|
2010-07-19 06:13:10 +00:00
|
|
|
}
|
2013-08-09 01:52:03 +00:00
|
|
|
|
|
|
|
return true;
|
2010-07-11 22:05:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void WinCOFFStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
|
2010-07-19 06:13:10 +00:00
|
|
|
llvm_unreachable("not implemented");
|
2010-07-11 22:05:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void WinCOFFStreamer::BeginCOFFSymbolDef(MCSymbol const *Symbol) {
|
2010-10-09 11:00:37 +00:00
|
|
|
assert((Symbol->isInSection()
|
|
|
|
? Symbol->getSection().getVariant() == MCSection::SV_COFF
|
2013-12-05 05:44:44 +00:00
|
|
|
: true) && "Got non-COFF section in the COFF backend!");
|
2014-04-13 04:57:38 +00:00
|
|
|
assert(!CurSymbol && "EndCOFFSymbolDef must be called between calls "
|
|
|
|
"to BeginCOFFSymbolDef!");
|
2010-07-19 06:13:10 +00:00
|
|
|
CurSymbol = Symbol;
|
2010-07-11 22:05:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void WinCOFFStreamer::EmitCOFFSymbolStorageClass(int StorageClass) {
|
2014-04-13 04:57:38 +00:00
|
|
|
assert(CurSymbol && "BeginCOFFSymbolDef must be called first!");
|
2010-07-19 06:13:10 +00:00
|
|
|
assert((StorageClass & ~0xFF) == 0 && "StorageClass must only have data in "
|
|
|
|
"the first byte!");
|
|
|
|
|
|
|
|
getAssembler().getOrCreateSymbolData(*CurSymbol).modifyFlags(
|
|
|
|
StorageClass << COFF::SF_ClassShift,
|
|
|
|
COFF::SF_ClassMask);
|
2010-07-11 22:05:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void WinCOFFStreamer::EmitCOFFSymbolType(int Type) {
|
2014-04-13 04:57:38 +00:00
|
|
|
assert(CurSymbol && "BeginCOFFSymbolDef must be called first!");
|
2010-07-19 06:13:10 +00:00
|
|
|
assert((Type & ~0xFFFF) == 0 && "Type must only have data in the first 2 "
|
|
|
|
"bytes");
|
|
|
|
|
|
|
|
getAssembler().getOrCreateSymbolData(*CurSymbol).modifyFlags(
|
|
|
|
Type << COFF::SF_TypeShift,
|
|
|
|
COFF::SF_TypeMask);
|
2010-07-11 22:05:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void WinCOFFStreamer::EndCOFFSymbolDef() {
|
2014-04-13 04:57:38 +00:00
|
|
|
assert(CurSymbol && "BeginCOFFSymbolDef must be called first!");
|
|
|
|
CurSymbol = nullptr;
|
2010-07-11 22:05:00 +00:00
|
|
|
}
|
|
|
|
|
2013-12-20 20:16:51 +00:00
|
|
|
void WinCOFFStreamer::EmitCOFFSectionIndex(MCSymbol const *Symbol) {
|
2013-12-20 18:15:00 +00:00
|
|
|
MCDataFragment *DF = getOrCreateDataFragment();
|
2013-12-20 20:16:51 +00:00
|
|
|
DF->getFixups().push_back(MCFixup::Create(
|
|
|
|
DF->getContents().size(), MCSymbolRefExpr::Create(Symbol, getContext()),
|
|
|
|
FK_SecRel_2));
|
2013-12-20 18:15:00 +00:00
|
|
|
DF->getContents().resize(DF->getContents().size() + 4, 0);
|
|
|
|
}
|
|
|
|
|
2013-12-20 20:16:51 +00:00
|
|
|
void WinCOFFStreamer::EmitCOFFSecRel32(MCSymbol const *Symbol) {
|
2011-12-17 01:14:52 +00:00
|
|
|
MCDataFragment *DF = getOrCreateDataFragment();
|
2013-12-20 20:16:51 +00:00
|
|
|
DF->getFixups().push_back(MCFixup::Create(
|
|
|
|
DF->getContents().size(), MCSymbolRefExpr::Create(Symbol, getContext()),
|
|
|
|
FK_SecRel_4));
|
2011-12-17 01:14:52 +00:00
|
|
|
DF->getContents().resize(DF->getContents().size() + 4, 0);
|
|
|
|
}
|
|
|
|
|
2010-07-11 22:05:00 +00:00
|
|
|
void WinCOFFStreamer::EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) {
|
2010-07-19 06:13:10 +00:00
|
|
|
llvm_unreachable("not implemented");
|
2010-07-11 22:05:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void WinCOFFStreamer::EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
|
2010-07-19 06:13:10 +00:00
|
|
|
unsigned ByteAlignment) {
|
2010-10-09 11:00:37 +00:00
|
|
|
assert((Symbol->isInSection()
|
|
|
|
? Symbol->getSection().getVariant() == MCSection::SV_COFF
|
2013-12-05 05:44:44 +00:00
|
|
|
: true) && "Got non-COFF section in the COFF backend!");
|
2014-04-08 22:33:40 +00:00
|
|
|
|
|
|
|
if (ByteAlignment > 32)
|
|
|
|
report_fatal_error(
|
|
|
|
"The linker won't align common symbols beyond 32 bytes.");
|
|
|
|
|
2014-04-13 04:57:38 +00:00
|
|
|
AssignSection(Symbol, nullptr);
|
2014-04-08 22:33:40 +00:00
|
|
|
|
|
|
|
MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
|
|
|
|
SD.setExternal(true);
|
|
|
|
SD.setCommon(Size, ByteAlignment);
|
2010-07-11 22:05:00 +00:00
|
|
|
}
|
|
|
|
|
2011-09-01 23:04:27 +00:00
|
|
|
void WinCOFFStreamer::EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size,
|
|
|
|
unsigned ByteAlignment) {
|
2014-04-08 22:33:40 +00:00
|
|
|
assert(!Symbol->isInSection() && "Symbol must not already have a section!");
|
|
|
|
|
|
|
|
const MCSection *Section = getContext().getObjectFileInfo()->getBSSSection();
|
|
|
|
MCSectionData &SectionData = getAssembler().getOrCreateSectionData(*Section);
|
|
|
|
if (SectionData.getAlignment() < ByteAlignment)
|
|
|
|
SectionData.setAlignment(ByteAlignment);
|
|
|
|
|
|
|
|
MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
|
|
|
|
SD.setExternal(false);
|
|
|
|
|
|
|
|
AssignSection(Symbol, Section);
|
|
|
|
|
|
|
|
if (ByteAlignment != 1)
|
|
|
|
new MCAlignFragment(ByteAlignment, /*_Value=*/0, /*_ValueSize=*/0,
|
|
|
|
ByteAlignment, &SectionData);
|
|
|
|
|
|
|
|
SD.setFragment(
|
|
|
|
new MCFillFragment(/*_Value=*/0, /*_ValueSize=*/0, Size, &SectionData));
|
2010-07-11 22:05:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void WinCOFFStreamer::EmitZerofill(const MCSection *Section, MCSymbol *Symbol,
|
2012-06-22 20:14:46 +00:00
|
|
|
uint64_t Size,unsigned ByteAlignment) {
|
2010-07-19 06:13:10 +00:00
|
|
|
llvm_unreachable("not implemented");
|
2010-07-11 22:05:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void WinCOFFStreamer::EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol,
|
|
|
|
uint64_t Size, unsigned ByteAlignment) {
|
2010-07-19 06:13:10 +00:00
|
|
|
llvm_unreachable("not implemented");
|
2010-07-11 22:05:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void WinCOFFStreamer::EmitFileDirective(StringRef Filename) {
|
2014-04-16 04:15:32 +00:00
|
|
|
getAssembler().addFileName(Filename);
|
2010-07-11 22:05:00 +00:00
|
|
|
}
|
|
|
|
|
2013-10-16 01:05:45 +00:00
|
|
|
// TODO: Implement this if you want to emit .comment section in COFF obj files.
|
|
|
|
void WinCOFFStreamer::EmitIdent(StringRef IdentString) {
|
|
|
|
llvm_unreachable("unsupported directive");
|
|
|
|
}
|
|
|
|
|
2011-05-22 03:01:05 +00:00
|
|
|
void WinCOFFStreamer::EmitWin64EHHandlerData() {
|
|
|
|
MCStreamer::EmitWin64EHHandlerData();
|
|
|
|
|
|
|
|
// We have to emit the unwind info now, because this directive
|
|
|
|
// actually switches to the .xdata section!
|
|
|
|
MCWin64EHUnwindEmitter::EmitUnwindInfo(*this, getCurrentW64UnwindInfo());
|
|
|
|
}
|
|
|
|
|
2012-01-07 03:13:18 +00:00
|
|
|
void WinCOFFStreamer::FinishImpl() {
|
2014-04-13 04:57:38 +00:00
|
|
|
EmitFrames(nullptr, true);
|
2011-05-22 04:15:07 +00:00
|
|
|
EmitW64Tables();
|
2012-01-07 03:13:18 +00:00
|
|
|
MCObjectStreamer::FinishImpl();
|
2010-07-11 22:05:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
namespace llvm
|
|
|
|
{
|
|
|
|
MCStreamer *createWinCOFFStreamer(MCContext &Context,
|
2011-07-25 23:24:55 +00:00
|
|
|
MCAsmBackend &MAB,
|
2010-07-11 22:05:00 +00:00
|
|
|
MCCodeEmitter &CE,
|
2010-07-31 06:22:29 +00:00
|
|
|
raw_ostream &OS,
|
|
|
|
bool RelaxAll) {
|
2011-07-25 23:24:55 +00:00
|
|
|
WinCOFFStreamer *S = new WinCOFFStreamer(Context, MAB, CE, OS);
|
2010-07-31 06:22:29 +00:00
|
|
|
S->getAssembler().setRelaxAll(RelaxAll);
|
|
|
|
return S;
|
2010-07-11 22:05:00 +00:00
|
|
|
}
|
|
|
|
}
|