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.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2014-04-27 03:48:05 +00:00
|
|
|
// This file contains an implementation of a Windows COFF object file streamer.
|
2010-07-11 22:05:00 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2014-05-22 02:18:10 +00:00
|
|
|
#include "llvm/ADT/StringExtras.h"
|
2012-12-03 16:50:05 +00:00
|
|
|
#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"
|
2015-01-14 11:23:27 +00:00
|
|
|
#include "llvm/MC/MCStreamer.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"
|
2014-04-27 03:48:05 +00:00
|
|
|
#include "llvm/MC/MCWinCOFFStreamer.h"
|
2010-07-11 22:05:00 +00:00
|
|
|
#include "llvm/Support/COFF.h"
|
|
|
|
#include "llvm/Support/Debug.h"
|
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
2014-10-07 19:37:57 +00:00
|
|
|
#include "llvm/Support/MathExtras.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"
|
|
|
|
|
2014-04-27 03:48:05 +00:00
|
|
|
namespace llvm {
|
|
|
|
MCWinCOFFStreamer::MCWinCOFFStreamer(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
|
|
|
|
2014-04-27 03:48:05 +00:00
|
|
|
void MCWinCOFFStreamer::EmitInstToData(const MCInst &Inst,
|
|
|
|
const MCSubtargetInfo &STI) {
|
|
|
|
MCDataFragment *DF = getOrCreateDataFragment();
|
|
|
|
|
|
|
|
SmallVector<MCFixup, 4> Fixups;
|
|
|
|
SmallString<256> Code;
|
|
|
|
raw_svector_ostream VecOS(Code);
|
|
|
|
getAssembler().getEmitter().EncodeInstruction(Inst, VecOS, Fixups, STI);
|
|
|
|
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-07-11 22:05:00 +00:00
|
|
|
|
2014-10-15 16:12:52 +00:00
|
|
|
void MCWinCOFFStreamer::InitSections(bool NoExecStack) {
|
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
|
|
|
}
|
|
|
|
|
2014-04-27 03:48:05 +00:00
|
|
|
void MCWinCOFFStreamer::EmitLabel(MCSymbol *Symbol) {
|
2010-11-28 16:22:59 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2014-04-27 03:48:05 +00:00
|
|
|
void MCWinCOFFStreamer::EmitAssemblerFlag(MCAssemblerFlag Flag) {
|
2010-07-19 06:13:10 +00:00
|
|
|
llvm_unreachable("not implemented");
|
2010-07-11 22:05:00 +00:00
|
|
|
}
|
|
|
|
|
2014-04-27 03:48:05 +00:00
|
|
|
void MCWinCOFFStreamer::EmitThumbFunc(MCSymbol *Func) {
|
2010-11-05 22:08:08 +00:00
|
|
|
llvm_unreachable("not implemented");
|
|
|
|
}
|
|
|
|
|
2014-04-27 03:48:05 +00:00
|
|
|
bool MCWinCOFFStreamer::EmitSymbolAttribute(MCSymbol *Symbol,
|
|
|
|
MCSymbolAttr Attribute) {
|
2010-10-09 11:00:37 +00:00
|
|
|
assert(Symbol && "Symbol must be non-null!");
|
2014-04-27 03:48:01 +00:00
|
|
|
assert((!Symbol->isInSection() ||
|
|
|
|
Symbol->getSection().getVariant() == MCSection::SV_COFF) &&
|
|
|
|
"Got non-COFF section in the COFF backend!");
|
|
|
|
|
|
|
|
MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
|
|
|
|
|
2010-07-19 06:13:10 +00:00
|
|
|
switch (Attribute) {
|
2014-04-27 03:48:01 +00:00
|
|
|
default: return false;
|
2010-07-19 06:13:10 +00:00
|
|
|
case MCSA_WeakReference:
|
2014-04-27 03:48:01 +00:00
|
|
|
case MCSA_Weak:
|
|
|
|
SD.modifyFlags(COFF::SF_WeakExternal, COFF::SF_WeakExternal);
|
|
|
|
SD.setExternal(true);
|
2010-07-19 06:13:10 +00:00
|
|
|
break;
|
|
|
|
case MCSA_Global:
|
2014-04-27 03:48:01 +00:00
|
|
|
SD.setExternal(true);
|
2010-07-19 06:13:10 +00:00
|
|
|
break;
|
|
|
|
}
|
2013-08-09 01:52:03 +00:00
|
|
|
|
|
|
|
return true;
|
2010-07-11 22:05:00 +00:00
|
|
|
}
|
|
|
|
|
2014-04-27 03:48:05 +00:00
|
|
|
void MCWinCOFFStreamer::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
|
|
|
}
|
|
|
|
|
2014-04-27 03:48:05 +00:00
|
|
|
void MCWinCOFFStreamer::BeginCOFFSymbolDef(MCSymbol const *Symbol) {
|
2014-04-27 03:48:01 +00:00
|
|
|
assert((!Symbol->isInSection() ||
|
|
|
|
Symbol->getSection().getVariant() == MCSection::SV_COFF) &&
|
|
|
|
"Got non-COFF section in the COFF backend!");
|
2014-05-22 02:18:10 +00:00
|
|
|
|
|
|
|
if (CurSymbol)
|
|
|
|
FatalError("starting a new symbol definition without completing the "
|
|
|
|
"previous one");
|
2010-07-19 06:13:10 +00:00
|
|
|
CurSymbol = Symbol;
|
2010-07-11 22:05:00 +00:00
|
|
|
}
|
|
|
|
|
2014-04-27 03:48:05 +00:00
|
|
|
void MCWinCOFFStreamer::EmitCOFFSymbolStorageClass(int StorageClass) {
|
2014-05-22 02:18:10 +00:00
|
|
|
if (!CurSymbol)
|
|
|
|
FatalError("storage class specified outside of symbol definition");
|
|
|
|
|
2014-11-11 08:43:57 +00:00
|
|
|
if (StorageClass & ~COFF::SSC_Invalid)
|
2014-05-22 02:18:10 +00:00
|
|
|
FatalError(Twine("storage class value '") + itostr(StorageClass) +
|
|
|
|
"' out of range");
|
2010-07-19 06:13:10 +00:00
|
|
|
|
2014-04-27 03:48:01 +00:00
|
|
|
MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*CurSymbol);
|
|
|
|
SD.modifyFlags(StorageClass << COFF::SF_ClassShift, COFF::SF_ClassMask);
|
2010-07-11 22:05:00 +00:00
|
|
|
}
|
|
|
|
|
2014-04-27 03:48:05 +00:00
|
|
|
void MCWinCOFFStreamer::EmitCOFFSymbolType(int Type) {
|
2014-05-22 02:18:10 +00:00
|
|
|
if (!CurSymbol)
|
|
|
|
FatalError("symbol type specified outside of a symbol definition");
|
|
|
|
|
|
|
|
if (Type & ~0xffff)
|
|
|
|
FatalError(Twine("type value '") + itostr(Type) + "' out of range");
|
2010-07-19 06:13:10 +00:00
|
|
|
|
2014-04-27 03:48:01 +00:00
|
|
|
MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*CurSymbol);
|
|
|
|
SD.modifyFlags(Type << COFF::SF_TypeShift, COFF::SF_TypeMask);
|
2010-07-11 22:05:00 +00:00
|
|
|
}
|
|
|
|
|
2014-04-27 03:48:05 +00:00
|
|
|
void MCWinCOFFStreamer::EndCOFFSymbolDef() {
|
2014-05-22 02:18:10 +00:00
|
|
|
if (!CurSymbol)
|
|
|
|
FatalError("ending symbol definition without starting one");
|
2014-04-13 04:57:38 +00:00
|
|
|
CurSymbol = nullptr;
|
2010-07-11 22:05:00 +00:00
|
|
|
}
|
|
|
|
|
2014-04-27 03:48:05 +00:00
|
|
|
void MCWinCOFFStreamer::EmitCOFFSectionIndex(MCSymbol const *Symbol) {
|
2013-12-20 18:15:00 +00:00
|
|
|
MCDataFragment *DF = getOrCreateDataFragment();
|
2014-04-27 03:48:01 +00:00
|
|
|
const MCSymbolRefExpr *SRE = MCSymbolRefExpr::Create(Symbol, getContext());
|
|
|
|
MCFixup Fixup = MCFixup::Create(DF->getContents().size(), SRE, FK_SecRel_2);
|
|
|
|
DF->getFixups().push_back(Fixup);
|
2014-10-08 18:01:49 +00:00
|
|
|
DF->getContents().resize(DF->getContents().size() + 2, 0);
|
2013-12-20 18:15:00 +00:00
|
|
|
}
|
|
|
|
|
2014-04-27 03:48:05 +00:00
|
|
|
void MCWinCOFFStreamer::EmitCOFFSecRel32(MCSymbol const *Symbol) {
|
2011-12-17 01:14:52 +00:00
|
|
|
MCDataFragment *DF = getOrCreateDataFragment();
|
2014-04-27 03:48:01 +00:00
|
|
|
const MCSymbolRefExpr *SRE = MCSymbolRefExpr::Create(Symbol, getContext());
|
|
|
|
MCFixup Fixup = MCFixup::Create(DF->getContents().size(), SRE, FK_SecRel_4);
|
|
|
|
DF->getFixups().push_back(Fixup);
|
2011-12-17 01:14:52 +00:00
|
|
|
DF->getContents().resize(DF->getContents().size() + 4, 0);
|
|
|
|
}
|
|
|
|
|
2014-04-27 03:48:05 +00:00
|
|
|
void MCWinCOFFStreamer::EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) {
|
2014-04-27 03:48:01 +00:00
|
|
|
llvm_unreachable("not supported");
|
2010-07-11 22:05:00 +00:00
|
|
|
}
|
|
|
|
|
2014-04-27 03:48:05 +00:00
|
|
|
void MCWinCOFFStreamer::EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
|
|
|
|
unsigned ByteAlignment) {
|
2014-04-27 03:48:01 +00:00
|
|
|
assert((!Symbol->isInSection() ||
|
|
|
|
Symbol->getSection().getVariant() == MCSection::SV_COFF) &&
|
|
|
|
"Got non-COFF section in the COFF backend!");
|
2014-04-08 22:33:40 +00:00
|
|
|
|
2014-09-21 09:18:07 +00:00
|
|
|
const Triple &T = getContext().getObjectFileInfo()->getTargetTriple();
|
2014-10-08 06:38:53 +00:00
|
|
|
if (T.isKnownWindowsMSVCEnvironment()) {
|
2014-09-21 09:18:07 +00:00
|
|
|
if (ByteAlignment > 32)
|
|
|
|
report_fatal_error("alignment is limited to 32-bytes");
|
2014-10-07 19:37:57 +00:00
|
|
|
|
2014-10-08 06:38:53 +00:00
|
|
|
// Round size up to alignment so that we will honor the alignment request.
|
|
|
|
Size = std::max(Size, static_cast<uint64_t>(ByteAlignment));
|
|
|
|
}
|
2014-04-08 22:33:40 +00:00
|
|
|
|
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);
|
2014-10-07 19:37:57 +00:00
|
|
|
|
|
|
|
if (!T.isKnownWindowsMSVCEnvironment() && ByteAlignment > 1) {
|
|
|
|
SmallString<128> Directive;
|
|
|
|
raw_svector_ostream OS(Directive);
|
|
|
|
const MCObjectFileInfo *MFI = getContext().getObjectFileInfo();
|
|
|
|
|
|
|
|
OS << " -aligncomm:\"" << Symbol->getName() << "\","
|
|
|
|
<< Log2_32_Ceil(ByteAlignment);
|
|
|
|
OS.flush();
|
|
|
|
|
|
|
|
PushSection();
|
|
|
|
SwitchSection(MFI->getDrectveSection());
|
|
|
|
EmitBytes(Directive);
|
|
|
|
PopSection();
|
|
|
|
}
|
2010-07-11 22:05:00 +00:00
|
|
|
}
|
|
|
|
|
2014-04-27 03:48:05 +00:00
|
|
|
void MCWinCOFFStreamer::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);
|
|
|
|
|
2014-04-27 03:48:01 +00:00
|
|
|
MCFillFragment *Fragment =
|
|
|
|
new MCFillFragment(/*_Value=*/0, /*_ValueSize=*/0, Size, &SectionData);
|
|
|
|
SD.setFragment(Fragment);
|
2010-07-11 22:05:00 +00:00
|
|
|
}
|
|
|
|
|
2014-04-27 03:48:05 +00:00
|
|
|
void MCWinCOFFStreamer::EmitZerofill(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
|
|
|
}
|
|
|
|
|
2014-04-27 03:48:05 +00:00
|
|
|
void MCWinCOFFStreamer::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
|
|
|
}
|
|
|
|
|
2014-04-27 03:48:05 +00:00
|
|
|
void MCWinCOFFStreamer::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.
|
2014-04-27 03:48:05 +00:00
|
|
|
void MCWinCOFFStreamer::EmitIdent(StringRef IdentString) {
|
2014-04-27 03:48:01 +00:00
|
|
|
llvm_unreachable("not implemented");
|
2013-10-16 01:05:45 +00:00
|
|
|
}
|
|
|
|
|
2014-06-29 01:52:01 +00:00
|
|
|
void MCWinCOFFStreamer::EmitWinEHHandlerData() {
|
2014-04-27 03:48:12 +00:00
|
|
|
llvm_unreachable("not implemented");
|
2011-05-22 03:01:05 +00:00
|
|
|
}
|
|
|
|
|
2014-04-27 03:48:05 +00:00
|
|
|
void MCWinCOFFStreamer::FinishImpl() {
|
2012-01-07 03:13:18 +00:00
|
|
|
MCObjectStreamer::FinishImpl();
|
2010-07-11 22:05:00 +00:00
|
|
|
}
|
2014-05-22 02:18:10 +00:00
|
|
|
|
|
|
|
LLVM_ATTRIBUTE_NORETURN
|
|
|
|
void MCWinCOFFStreamer::FatalError(const Twine &Msg) const {
|
|
|
|
getContext().FatalError(SMLoc(), Msg);
|
|
|
|
}
|
2010-07-11 22:05:00 +00:00
|
|
|
}
|
2014-04-27 03:48:01 +00:00
|
|
|
|