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.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#define DEBUG_TYPE "WinCOFFStreamer"
|
|
|
|
|
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"
|
|
|
|
#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;
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
2010-07-19 06:13:10 +00:00
|
|
|
void AddCommonSymbol(MCSymbol *Symbol, uint64_t Size,
|
|
|
|
unsigned ByteAlignment, bool External);
|
|
|
|
|
2010-07-11 22:05:00 +00:00
|
|
|
// MCStreamer interface
|
|
|
|
|
2010-09-15 21:48:40 +00:00
|
|
|
virtual void InitSections();
|
2013-01-14 19:04:57 +00:00
|
|
|
virtual void InitToTextSection();
|
2010-11-28 16:22:59 +00:00
|
|
|
virtual void EmitLabel(MCSymbol *Symbol);
|
2012-12-16 04:00:45 +00:00
|
|
|
virtual void EmitDebugLabel(MCSymbol *Symbol);
|
2010-07-11 22:05:00 +00:00
|
|
|
virtual void EmitAssemblerFlag(MCAssemblerFlag Flag);
|
2010-11-05 22:08:08 +00:00
|
|
|
virtual void EmitThumbFunc(MCSymbol *Func);
|
2013-08-09 01:52:03 +00:00
|
|
|
virtual bool EmitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute);
|
2010-07-11 22:05:00 +00:00
|
|
|
virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue);
|
|
|
|
virtual void BeginCOFFSymbolDef(MCSymbol const *Symbol);
|
|
|
|
virtual void EmitCOFFSymbolStorageClass(int StorageClass);
|
|
|
|
virtual void EmitCOFFSymbolType(int Type);
|
|
|
|
virtual void EndCOFFSymbolDef();
|
2011-12-17 01:14:52 +00:00
|
|
|
virtual void EmitCOFFSecRel32(MCSymbol const *Symbol);
|
2010-07-11 22:05:00 +00:00
|
|
|
virtual void EmitELFSize(MCSymbol *Symbol, const MCExpr *Value);
|
|
|
|
virtual void EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
|
2010-07-19 06:13:10 +00:00
|
|
|
unsigned ByteAlignment);
|
2011-09-01 23:04:27 +00:00
|
|
|
virtual void EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size,
|
|
|
|
unsigned ByteAlignment);
|
2010-07-11 22:05:00 +00:00
|
|
|
virtual void EmitZerofill(const MCSection *Section, MCSymbol *Symbol,
|
2012-06-22 20:14:46 +00:00
|
|
|
uint64_t Size,unsigned ByteAlignment);
|
2010-07-11 22:05:00 +00:00
|
|
|
virtual void EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol,
|
2010-07-19 06:13:10 +00:00
|
|
|
uint64_t Size, unsigned ByteAlignment);
|
2010-07-11 22:05:00 +00:00
|
|
|
virtual void EmitFileDirective(StringRef Filename);
|
2013-10-16 01:05:45 +00:00
|
|
|
virtual void EmitIdent(StringRef IdentString);
|
2011-05-22 03:01:05 +00:00
|
|
|
virtual void EmitWin64EHHandlerData();
|
2012-01-07 03:13:18 +00:00
|
|
|
virtual void FinishImpl();
|
2010-10-09 15:44:27 +00:00
|
|
|
|
|
|
|
private:
|
2010-11-01 16:27:31 +00:00
|
|
|
virtual void EmitInstToData(const MCInst &Inst) {
|
2013-01-03 01:09:22 +00:00
|
|
|
MCDataFragment *DF = getOrCreateDataFragment();
|
|
|
|
|
|
|
|
SmallVector<MCFixup, 4> Fixups;
|
|
|
|
SmallString<256> Code;
|
|
|
|
raw_svector_ostream VecOS(Code);
|
|
|
|
getAssembler().getEmitter().EncodeInstruction(Inst, VecOS, Fixups);
|
|
|
|
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-10-09 15:44:27 +00:00
|
|
|
void SetSection(StringRef Section,
|
|
|
|
unsigned Characteristics,
|
|
|
|
SectionKind Kind) {
|
|
|
|
SwitchSection(getContext().getCOFFSection(Section, Characteristics, Kind));
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetSectionText() {
|
|
|
|
SetSection(".text",
|
|
|
|
COFF::IMAGE_SCN_CNT_CODE
|
|
|
|
| COFF::IMAGE_SCN_MEM_EXECUTE
|
|
|
|
| COFF::IMAGE_SCN_MEM_READ,
|
|
|
|
SectionKind::getText());
|
|
|
|
EmitCodeAlignment(4, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetSectionData() {
|
|
|
|
SetSection(".data",
|
|
|
|
COFF::IMAGE_SCN_CNT_INITIALIZED_DATA
|
|
|
|
| COFF::IMAGE_SCN_MEM_READ
|
|
|
|
| COFF::IMAGE_SCN_MEM_WRITE,
|
|
|
|
SectionKind::getDataRel());
|
|
|
|
EmitCodeAlignment(4, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetSectionBSS() {
|
|
|
|
SetSection(".bss",
|
|
|
|
COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA
|
|
|
|
| COFF::IMAGE_SCN_MEM_READ
|
|
|
|
| COFF::IMAGE_SCN_MEM_WRITE,
|
|
|
|
SectionKind::getBSS());
|
|
|
|
EmitCodeAlignment(4, 0);
|
|
|
|
}
|
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)
|
2013-10-08 13:08:17 +00:00
|
|
|
: MCObjectStreamer(Context, 0, MAB, OS, &CE), CurSymbol(NULL) {}
|
2010-07-19 06:13:10 +00:00
|
|
|
|
|
|
|
void WinCOFFStreamer::AddCommonSymbol(MCSymbol *Symbol, uint64_t Size,
|
|
|
|
unsigned ByteAlignment, bool External) {
|
|
|
|
assert(!Symbol->isInSection() && "Symbol must not already have a section!");
|
|
|
|
|
|
|
|
std::string SectionName(".bss$linkonce");
|
|
|
|
SectionName.append(Symbol->getName().begin(), Symbol->getName().end());
|
|
|
|
|
|
|
|
MCSymbolData &SymbolData = getAssembler().getOrCreateSymbolData(*Symbol);
|
|
|
|
|
|
|
|
unsigned Characteristics =
|
|
|
|
COFF::IMAGE_SCN_LNK_COMDAT |
|
|
|
|
COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA |
|
|
|
|
COFF::IMAGE_SCN_MEM_READ |
|
|
|
|
COFF::IMAGE_SCN_MEM_WRITE;
|
|
|
|
|
|
|
|
int Selection = COFF::IMAGE_COMDAT_SELECT_LARGEST;
|
|
|
|
|
|
|
|
const MCSection *Section = MCStreamer::getContext().getCOFFSection(
|
2013-11-19 19:52:52 +00:00
|
|
|
SectionName, Characteristics, SectionKind::getBSS(), Symbol->getName(),
|
|
|
|
Selection);
|
2010-07-19 06:13:10 +00:00
|
|
|
|
|
|
|
MCSectionData &SectionData = getAssembler().getOrCreateSectionData(*Section);
|
|
|
|
|
|
|
|
if (SectionData.getAlignment() < ByteAlignment)
|
|
|
|
SectionData.setAlignment(ByteAlignment);
|
|
|
|
|
|
|
|
SymbolData.setExternal(External);
|
|
|
|
|
2013-09-19 23:21:01 +00:00
|
|
|
AssignSection(Symbol, Section);
|
2010-07-19 06:13:10 +00:00
|
|
|
|
|
|
|
if (ByteAlignment != 1)
|
|
|
|
new MCAlignFragment(ByteAlignment, 0, 0, ByteAlignment, &SectionData);
|
|
|
|
|
|
|
|
SymbolData.setFragment(new MCFillFragment(0, 0, Size, &SectionData));
|
2010-07-11 22:05:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// MCStreamer interface
|
|
|
|
|
2013-01-14 19:04:57 +00:00
|
|
|
void WinCOFFStreamer::InitToTextSection() {
|
|
|
|
SetSectionText();
|
|
|
|
}
|
|
|
|
|
2010-09-15 21:48:40 +00:00
|
|
|
void WinCOFFStreamer::InitSections() {
|
2010-10-09 15:44:27 +00:00
|
|
|
SetSectionText();
|
|
|
|
SetSectionData();
|
|
|
|
SetSectionBSS();
|
|
|
|
SetSectionText();
|
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
|
|
|
|
: 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
|
|
|
|
: true) && "Got non COFF section in the COFF backend!");
|
2010-07-19 06:13:10 +00:00
|
|
|
assert(CurSymbol == NULL && "EndCOFFSymbolDef must be called between calls "
|
|
|
|
"to BeginCOFFSymbolDef!");
|
|
|
|
CurSymbol = Symbol;
|
2010-07-11 22:05:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void WinCOFFStreamer::EmitCOFFSymbolStorageClass(int StorageClass) {
|
2010-07-19 06:13:10 +00:00
|
|
|
assert(CurSymbol != NULL && "BeginCOFFSymbolDef must be called first!");
|
|
|
|
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) {
|
2010-07-19 06:13:10 +00:00
|
|
|
assert(CurSymbol != NULL && "BeginCOFFSymbolDef must be called first!");
|
|
|
|
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() {
|
2010-07-19 06:13:10 +00:00
|
|
|
assert(CurSymbol != NULL && "BeginCOFFSymbolDef must be called first!");
|
|
|
|
CurSymbol = NULL;
|
2010-07-11 22:05:00 +00:00
|
|
|
}
|
|
|
|
|
2011-12-17 01:14:52 +00:00
|
|
|
void WinCOFFStreamer::EmitCOFFSecRel32(MCSymbol const *Symbol)
|
|
|
|
{
|
|
|
|
MCDataFragment *DF = getOrCreateDataFragment();
|
|
|
|
|
2012-12-07 19:13:57 +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
|
|
|
|
: true) && "Got non COFF section in the COFF backend!");
|
2010-07-19 06:13:10 +00:00
|
|
|
AddCommonSymbol(Symbol, Size, ByteAlignment, true);
|
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) {
|
2010-10-09 11:00:37 +00:00
|
|
|
assert((Symbol->isInSection()
|
|
|
|
? Symbol->getSection().getVariant() == MCSection::SV_COFF
|
|
|
|
: true) && "Got non COFF section in the COFF backend!");
|
2011-09-01 23:04:27 +00:00
|
|
|
AddCommonSymbol(Symbol, Size, ByteAlignment, false);
|
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) {
|
|
|
|
// Ignore for now, linkers don't care, and proper debug
|
|
|
|
// info will be a much large effort.
|
|
|
|
}
|
|
|
|
|
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() {
|
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
|
|
|
}
|
|
|
|
}
|