WinCOFFStreamer.cpp revision 288943
1//===-- llvm/MC/WinCOFFStreamer.cpp -----------------------------*- C++ -*-===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file is distributed under the University of Illinois Open Source 6// License. See LICENSE.TXT for details. 7// 8//===----------------------------------------------------------------------===// 9// 10// This file contains an implementation of a Windows COFF object file streamer. 11// 12//===----------------------------------------------------------------------===// 13 14#include "llvm/MC/MCAsmBackend.h" 15#include "llvm/MC/MCAsmLayout.h" 16#include "llvm/MC/MCAssembler.h" 17#include "llvm/MC/MCCodeEmitter.h" 18#include "llvm/MC/MCContext.h" 19#include "llvm/MC/MCExpr.h" 20#include "llvm/MC/MCObjectFileInfo.h" 21#include "llvm/MC/MCObjectStreamer.h" 22#include "llvm/MC/MCSection.h" 23#include "llvm/MC/MCSectionCOFF.h" 24#include "llvm/MC/MCStreamer.h" 25#include "llvm/MC/MCSymbolCOFF.h" 26#include "llvm/MC/MCValue.h" 27#include "llvm/MC/MCWinCOFFStreamer.h" 28#include "llvm/Support/COFF.h" 29#include "llvm/Support/Debug.h" 30#include "llvm/Support/ErrorHandling.h" 31#include "llvm/Support/MathExtras.h" 32#include "llvm/Support/TargetRegistry.h" 33#include "llvm/Support/raw_ostream.h" 34 35using namespace llvm; 36 37#define DEBUG_TYPE "WinCOFFStreamer" 38 39namespace llvm { 40MCWinCOFFStreamer::MCWinCOFFStreamer(MCContext &Context, MCAsmBackend &MAB, 41 MCCodeEmitter &CE, raw_pwrite_stream &OS) 42 : MCObjectStreamer(Context, MAB, OS, &CE), CurSymbol(nullptr) {} 43 44void MCWinCOFFStreamer::EmitInstToData(const MCInst &Inst, 45 const MCSubtargetInfo &STI) { 46 MCDataFragment *DF = getOrCreateDataFragment(); 47 48 SmallVector<MCFixup, 4> Fixups; 49 SmallString<256> Code; 50 raw_svector_ostream VecOS(Code); 51 getAssembler().getEmitter().encodeInstruction(Inst, VecOS, Fixups, STI); 52 VecOS.flush(); 53 54 // Add the fixups and data. 55 for (unsigned i = 0, e = Fixups.size(); i != e; ++i) { 56 Fixups[i].setOffset(Fixups[i].getOffset() + DF->getContents().size()); 57 DF->getFixups().push_back(Fixups[i]); 58 } 59 60 DF->getContents().append(Code.begin(), Code.end()); 61} 62 63void MCWinCOFFStreamer::InitSections(bool NoExecStack) { 64 // FIXME: this is identical to the ELF one. 65 // This emulates the same behavior of GNU as. This makes it easier 66 // to compare the output as the major sections are in the same order. 67 SwitchSection(getContext().getObjectFileInfo()->getTextSection()); 68 EmitCodeAlignment(4); 69 70 SwitchSection(getContext().getObjectFileInfo()->getDataSection()); 71 EmitCodeAlignment(4); 72 73 SwitchSection(getContext().getObjectFileInfo()->getBSSSection()); 74 EmitCodeAlignment(4); 75 76 SwitchSection(getContext().getObjectFileInfo()->getTextSection()); 77} 78 79void MCWinCOFFStreamer::EmitLabel(MCSymbol *Symbol) { 80 assert(Symbol->isUndefined() && "Cannot define a symbol twice!"); 81 MCObjectStreamer::EmitLabel(Symbol); 82} 83 84void MCWinCOFFStreamer::EmitAssemblerFlag(MCAssemblerFlag Flag) { 85 llvm_unreachable("not implemented"); 86} 87 88void MCWinCOFFStreamer::EmitThumbFunc(MCSymbol *Func) { 89 llvm_unreachable("not implemented"); 90} 91 92bool MCWinCOFFStreamer::EmitSymbolAttribute(MCSymbol *Symbol, 93 MCSymbolAttr Attribute) { 94 assert(Symbol && "Symbol must be non-null!"); 95 assert((!Symbol->isInSection() || 96 Symbol->getSection().getVariant() == MCSection::SV_COFF) && 97 "Got non-COFF section in the COFF backend!"); 98 99 getAssembler().registerSymbol(*Symbol); 100 101 switch (Attribute) { 102 default: return false; 103 case MCSA_WeakReference: 104 case MCSA_Weak: 105 cast<MCSymbolCOFF>(Symbol)->setIsWeakExternal(); 106 Symbol->setExternal(true); 107 break; 108 case MCSA_Global: 109 Symbol->setExternal(true); 110 break; 111 } 112 113 return true; 114} 115 116void MCWinCOFFStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) { 117 llvm_unreachable("not implemented"); 118} 119 120void MCWinCOFFStreamer::BeginCOFFSymbolDef(MCSymbol const *Symbol) { 121 assert((!Symbol->isInSection() || 122 Symbol->getSection().getVariant() == MCSection::SV_COFF) && 123 "Got non-COFF section in the COFF backend!"); 124 125 if (CurSymbol) 126 FatalError("starting a new symbol definition without completing the " 127 "previous one"); 128 CurSymbol = Symbol; 129} 130 131void MCWinCOFFStreamer::EmitCOFFSymbolStorageClass(int StorageClass) { 132 if (!CurSymbol) 133 FatalError("storage class specified outside of symbol definition"); 134 135 if (StorageClass & ~COFF::SSC_Invalid) 136 FatalError("storage class value '" + Twine(StorageClass) + 137 "' out of range"); 138 139 getAssembler().registerSymbol(*CurSymbol); 140 cast<MCSymbolCOFF>(CurSymbol)->setClass((uint16_t)StorageClass); 141} 142 143void MCWinCOFFStreamer::EmitCOFFSymbolType(int Type) { 144 if (!CurSymbol) 145 FatalError("symbol type specified outside of a symbol definition"); 146 147 if (Type & ~0xffff) 148 FatalError("type value '" + Twine(Type) + "' out of range"); 149 150 getAssembler().registerSymbol(*CurSymbol); 151 cast<MCSymbolCOFF>(CurSymbol)->setType((uint16_t)Type); 152} 153 154void MCWinCOFFStreamer::EndCOFFSymbolDef() { 155 if (!CurSymbol) 156 FatalError("ending symbol definition without starting one"); 157 CurSymbol = nullptr; 158} 159 160void MCWinCOFFStreamer::EmitCOFFSafeSEH(MCSymbol const *Symbol) { 161 // SafeSEH is a feature specific to 32-bit x86. It does not exist (and is 162 // unnecessary) on all platforms which use table-based exception dispatch. 163 if (getContext().getObjectFileInfo()->getTargetTriple().getArch() != 164 Triple::x86) 165 return; 166 167 const MCSymbolCOFF *CSymbol = cast<MCSymbolCOFF>(Symbol); 168 if (CSymbol->isSafeSEH()) 169 return; 170 171 MCSection *SXData = getContext().getObjectFileInfo()->getSXDataSection(); 172 getAssembler().registerSection(*SXData); 173 if (SXData->getAlignment() < 4) 174 SXData->setAlignment(4); 175 176 new MCSafeSEHFragment(Symbol, SXData); 177 178 getAssembler().registerSymbol(*Symbol); 179 CSymbol->setIsSafeSEH(); 180 181 // The Microsoft linker requires that the symbol type of a handler be 182 // function. Go ahead and oblige it here. 183 CSymbol->setType(COFF::IMAGE_SYM_DTYPE_FUNCTION 184 << COFF::SCT_COMPLEX_TYPE_SHIFT); 185} 186 187void MCWinCOFFStreamer::EmitCOFFSectionIndex(MCSymbol const *Symbol) { 188 MCDataFragment *DF = getOrCreateDataFragment(); 189 const MCSymbolRefExpr *SRE = MCSymbolRefExpr::create(Symbol, getContext()); 190 MCFixup Fixup = MCFixup::create(DF->getContents().size(), SRE, FK_SecRel_2); 191 DF->getFixups().push_back(Fixup); 192 DF->getContents().resize(DF->getContents().size() + 2, 0); 193} 194 195void MCWinCOFFStreamer::EmitCOFFSecRel32(MCSymbol const *Symbol) { 196 MCDataFragment *DF = getOrCreateDataFragment(); 197 const MCSymbolRefExpr *SRE = MCSymbolRefExpr::create(Symbol, getContext()); 198 MCFixup Fixup = MCFixup::create(DF->getContents().size(), SRE, FK_SecRel_4); 199 DF->getFixups().push_back(Fixup); 200 DF->getContents().resize(DF->getContents().size() + 4, 0); 201} 202 203void MCWinCOFFStreamer::EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size, 204 unsigned ByteAlignment) { 205 assert((!Symbol->isInSection() || 206 Symbol->getSection().getVariant() == MCSection::SV_COFF) && 207 "Got non-COFF section in the COFF backend!"); 208 209 const Triple &T = getContext().getObjectFileInfo()->getTargetTriple(); 210 if (T.isKnownWindowsMSVCEnvironment()) { 211 if (ByteAlignment > 32) 212 report_fatal_error("alignment is limited to 32-bytes"); 213 214 // Round size up to alignment so that we will honor the alignment request. 215 Size = std::max(Size, static_cast<uint64_t>(ByteAlignment)); 216 } 217 218 AssignSection(Symbol, nullptr); 219 220 getAssembler().registerSymbol(*Symbol); 221 Symbol->setExternal(true); 222 Symbol->setCommon(Size, ByteAlignment); 223 224 if (!T.isKnownWindowsMSVCEnvironment() && ByteAlignment > 1) { 225 SmallString<128> Directive; 226 raw_svector_ostream OS(Directive); 227 const MCObjectFileInfo *MFI = getContext().getObjectFileInfo(); 228 229 OS << " -aligncomm:\"" << Symbol->getName() << "\"," 230 << Log2_32_Ceil(ByteAlignment); 231 OS.flush(); 232 233 PushSection(); 234 SwitchSection(MFI->getDrectveSection()); 235 EmitBytes(Directive); 236 PopSection(); 237 } 238} 239 240void MCWinCOFFStreamer::EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size, 241 unsigned ByteAlignment) { 242 assert(!Symbol->isInSection() && "Symbol must not already have a section!"); 243 244 MCSection *Section = getContext().getObjectFileInfo()->getBSSSection(); 245 getAssembler().registerSection(*Section); 246 if (Section->getAlignment() < ByteAlignment) 247 Section->setAlignment(ByteAlignment); 248 249 getAssembler().registerSymbol(*Symbol); 250 Symbol->setExternal(false); 251 252 AssignSection(Symbol, Section); 253 254 if (ByteAlignment != 1) 255 new MCAlignFragment(ByteAlignment, /*Value=*/0, /*ValueSize=*/0, 256 ByteAlignment, Section); 257 258 MCFillFragment *Fragment = new MCFillFragment( 259 /*Value=*/0, /*ValueSize=*/0, Size, Section); 260 Symbol->setFragment(Fragment); 261} 262 263void MCWinCOFFStreamer::EmitZerofill(MCSection *Section, MCSymbol *Symbol, 264 uint64_t Size, unsigned ByteAlignment) { 265 llvm_unreachable("not implemented"); 266} 267 268void MCWinCOFFStreamer::EmitTBSSSymbol(MCSection *Section, MCSymbol *Symbol, 269 uint64_t Size, unsigned ByteAlignment) { 270 llvm_unreachable("not implemented"); 271} 272 273void MCWinCOFFStreamer::EmitFileDirective(StringRef Filename) { 274 getAssembler().addFileName(Filename); 275} 276 277// TODO: Implement this if you want to emit .comment section in COFF obj files. 278void MCWinCOFFStreamer::EmitIdent(StringRef IdentString) { 279 llvm_unreachable("not implemented"); 280} 281 282void MCWinCOFFStreamer::EmitWinEHHandlerData() { 283 llvm_unreachable("not implemented"); 284} 285 286void MCWinCOFFStreamer::FinishImpl() { 287 MCObjectStreamer::FinishImpl(); 288} 289 290LLVM_ATTRIBUTE_NORETURN 291void MCWinCOFFStreamer::FatalError(const Twine &Msg) const { 292 getContext().reportFatalError(SMLoc(), Msg); 293} 294} 295 296