MCMachOStreamer.cpp revision ba210243ef7d325ef6954d459091edf580a241f9
1//===- lib/MC/MCMachOStreamer.cpp - Mach-O Object Output ------------===// 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#include "llvm/MC/MCStreamer.h" 11 12#include "llvm/MC/MCAssembler.h" 13#include "llvm/MC/MCContext.h" 14#include "llvm/MC/MCCodeEmitter.h" 15#include "llvm/MC/MCExpr.h" 16#include "llvm/MC/MCInst.h" 17#include "llvm/MC/MCObjectStreamer.h" 18#include "llvm/MC/MCSection.h" 19#include "llvm/MC/MCSymbol.h" 20#include "llvm/MC/MCMachOSymbolFlags.h" 21#include "llvm/MC/MCSectionMachO.h" 22#include "llvm/MC/MCDwarf.h" 23#include "llvm/Support/Dwarf.h" 24#include "llvm/Support/ErrorHandling.h" 25#include "llvm/Support/raw_ostream.h" 26#include "llvm/Target/TargetAsmBackend.h" 27 28using namespace llvm; 29 30namespace { 31 32class MCMachOStreamer : public MCObjectStreamer { 33private: 34 virtual void EmitInstToFragment(const MCInst &Inst); 35 virtual void EmitInstToData(const MCInst &Inst); 36 37public: 38 MCMachOStreamer(MCContext &Context, TargetAsmBackend &TAB, 39 raw_ostream &OS, MCCodeEmitter *Emitter) 40 : MCObjectStreamer(Context, TAB, OS, Emitter, true) {} 41 42 /// @name MCStreamer Interface 43 /// @{ 44 45 virtual void InitSections(); 46 virtual void EmitLabel(MCSymbol *Symbol); 47 virtual void EmitAssemblerFlag(MCAssemblerFlag Flag); 48 virtual void EmitThumbFunc(MCSymbol *Func); 49 virtual void EmitAssignment(MCSymbol *Symbol, const MCExpr *Value); 50 virtual void EmitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute); 51 virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue); 52 virtual void EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size, 53 unsigned ByteAlignment); 54 virtual void BeginCOFFSymbolDef(const MCSymbol *Symbol) { 55 assert(0 && "macho doesn't support this directive"); 56 } 57 virtual void EmitCOFFSymbolStorageClass(int StorageClass) { 58 assert(0 && "macho doesn't support this directive"); 59 } 60 virtual void EmitCOFFSymbolType(int Type) { 61 assert(0 && "macho doesn't support this directive"); 62 } 63 virtual void EndCOFFSymbolDef() { 64 assert(0 && "macho doesn't support this directive"); 65 } 66 virtual void EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) { 67 assert(0 && "macho doesn't support this directive"); 68 } 69 virtual void EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size) { 70 assert(0 && "macho doesn't support this directive"); 71 } 72 virtual void EmitZerofill(const MCSection *Section, MCSymbol *Symbol = 0, 73 unsigned Size = 0, unsigned ByteAlignment = 0); 74 virtual void EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol, 75 uint64_t Size, unsigned ByteAlignment = 0); 76 virtual void EmitBytes(StringRef Data, unsigned AddrSpace); 77 virtual void EmitValue(const MCExpr *Value, unsigned Size,unsigned AddrSpace); 78 virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0, 79 unsigned ValueSize = 1, 80 unsigned MaxBytesToEmit = 0); 81 virtual void EmitCodeAlignment(unsigned ByteAlignment, 82 unsigned MaxBytesToEmit = 0); 83 virtual void EmitValueToOffset(const MCExpr *Offset, 84 unsigned char Value = 0); 85 86 virtual void EmitFileDirective(StringRef Filename) { 87 // FIXME: Just ignore the .file; it isn't important enough to fail the 88 // entire assembly. 89 90 //report_fatal_error("unsupported directive: '.file'"); 91 } 92 93 virtual void Finish(); 94 95 /// @} 96}; 97 98} // end anonymous namespace. 99 100void MCMachOStreamer::InitSections() { 101 SwitchSection(getContext().getMachOSection("__TEXT", "__text", 102 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS, 103 0, SectionKind::getText())); 104 105} 106 107void MCMachOStreamer::EmitLabel(MCSymbol *Symbol) { 108 // TODO: This is almost exactly the same as WinCOFFStreamer. Consider merging 109 // into MCObjectStreamer. 110 assert(Symbol->isUndefined() && "Cannot define a symbol twice!"); 111 assert(!Symbol->isVariable() && "Cannot emit a variable symbol!"); 112 assert(CurSection && "Cannot emit before setting section!"); 113 114 Symbol->setSection(*CurSection); 115 116 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol); 117 118 // We have to create a new fragment if this is an atom defining symbol, 119 // fragments cannot span atoms. 120 if (getAssembler().isSymbolLinkerVisible(SD.getSymbol())) 121 new MCDataFragment(getCurrentSectionData()); 122 123 // FIXME: This is wasteful, we don't necessarily need to create a data 124 // fragment. Instead, we should mark the symbol as pointing into the data 125 // fragment if it exists, otherwise we should just queue the label and set its 126 // fragment pointer when we emit the next fragment. 127 MCDataFragment *F = getOrCreateDataFragment(); 128 assert(!SD.getFragment() && "Unexpected fragment on symbol data!"); 129 SD.setFragment(F); 130 SD.setOffset(F->getContents().size()); 131 132 // This causes the reference type flag to be cleared. Darwin 'as' was "trying" 133 // to clear the weak reference and weak definition bits too, but the 134 // implementation was buggy. For now we just try to match 'as', for 135 // diffability. 136 // 137 // FIXME: Cleanup this code, these bits should be emitted based on semantic 138 // properties, not on the order of definition, etc. 139 SD.setFlags(SD.getFlags() & ~SF_ReferenceTypeMask); 140} 141 142void MCMachOStreamer::EmitAssemblerFlag(MCAssemblerFlag Flag) { 143 switch (Flag) { 144 case MCAF_SyntaxUnified: return; // no-op here. 145 case MCAF_Code16: return; // no-op here. 146 case MCAF_Code32: return; // no-op here. 147 case MCAF_SubsectionsViaSymbols: 148 getAssembler().setSubsectionsViaSymbols(true); 149 return; 150 default: 151 llvm_unreachable("invalid assembler flag!"); 152 } 153} 154 155void MCMachOStreamer::EmitThumbFunc(MCSymbol *Func) { 156 // FIXME: Flag the function ISA as thumb with DW_AT_APPLE_isa. 157} 158 159void MCMachOStreamer::EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) { 160 // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into 161 // MCObjectStreamer. 162 // FIXME: Lift context changes into super class. 163 getAssembler().getOrCreateSymbolData(*Symbol); 164 Symbol->setVariableValue(AddValueSymbols(Value)); 165} 166 167void MCMachOStreamer::EmitSymbolAttribute(MCSymbol *Symbol, 168 MCSymbolAttr Attribute) { 169 // Indirect symbols are handled differently, to match how 'as' handles 170 // them. This makes writing matching .o files easier. 171 if (Attribute == MCSA_IndirectSymbol) { 172 // Note that we intentionally cannot use the symbol data here; this is 173 // important for matching the string table that 'as' generates. 174 IndirectSymbolData ISD; 175 ISD.Symbol = Symbol; 176 ISD.SectionData = getCurrentSectionData(); 177 getAssembler().getIndirectSymbols().push_back(ISD); 178 return; 179 } 180 181 // Adding a symbol attribute always introduces the symbol, note that an 182 // important side effect of calling getOrCreateSymbolData here is to register 183 // the symbol with the assembler. 184 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol); 185 186 // The implementation of symbol attributes is designed to match 'as', but it 187 // leaves much to desired. It doesn't really make sense to arbitrarily add and 188 // remove flags, but 'as' allows this (in particular, see .desc). 189 // 190 // In the future it might be worth trying to make these operations more well 191 // defined. 192 switch (Attribute) { 193 case MCSA_Invalid: 194 case MCSA_ELF_TypeFunction: 195 case MCSA_ELF_TypeIndFunction: 196 case MCSA_ELF_TypeObject: 197 case MCSA_ELF_TypeTLS: 198 case MCSA_ELF_TypeCommon: 199 case MCSA_ELF_TypeNoType: 200 case MCSA_ELF_TypeGnuUniqueObject: 201 case MCSA_IndirectSymbol: 202 case MCSA_Hidden: 203 case MCSA_Internal: 204 case MCSA_Protected: 205 case MCSA_Weak: 206 case MCSA_Local: 207 assert(0 && "Invalid symbol attribute for Mach-O!"); 208 break; 209 210 case MCSA_Global: 211 SD.setExternal(true); 212 // This effectively clears the undefined lazy bit, in Darwin 'as', although 213 // it isn't very consistent because it implements this as part of symbol 214 // lookup. 215 // 216 // FIXME: Cleanup this code, these bits should be emitted based on semantic 217 // properties, not on the order of definition, etc. 218 SD.setFlags(SD.getFlags() & ~SF_ReferenceTypeUndefinedLazy); 219 break; 220 221 case MCSA_LazyReference: 222 // FIXME: This requires -dynamic. 223 SD.setFlags(SD.getFlags() | SF_NoDeadStrip); 224 if (Symbol->isUndefined()) 225 SD.setFlags(SD.getFlags() | SF_ReferenceTypeUndefinedLazy); 226 break; 227 228 // Since .reference sets the no dead strip bit, it is equivalent to 229 // .no_dead_strip in practice. 230 case MCSA_Reference: 231 case MCSA_NoDeadStrip: 232 SD.setFlags(SD.getFlags() | SF_NoDeadStrip); 233 break; 234 235 case MCSA_SymbolResolver: 236 SD.setFlags(SD.getFlags() | SF_SymbolResolver); 237 break; 238 239 case MCSA_PrivateExtern: 240 SD.setExternal(true); 241 SD.setPrivateExtern(true); 242 break; 243 244 case MCSA_WeakReference: 245 // FIXME: This requires -dynamic. 246 if (Symbol->isUndefined()) 247 SD.setFlags(SD.getFlags() | SF_WeakReference); 248 break; 249 250 case MCSA_WeakDefinition: 251 // FIXME: 'as' enforces that this is defined and global. The manual claims 252 // it has to be in a coalesced section, but this isn't enforced. 253 SD.setFlags(SD.getFlags() | SF_WeakDefinition); 254 break; 255 256 case MCSA_WeakDefAutoPrivate: 257 SD.setFlags(SD.getFlags() | SF_WeakDefinition | SF_WeakReference); 258 break; 259 } 260} 261 262void MCMachOStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) { 263 // Encode the 'desc' value into the lowest implementation defined bits. 264 assert(DescValue == (DescValue & SF_DescFlagsMask) && 265 "Invalid .desc value!"); 266 getAssembler().getOrCreateSymbolData(*Symbol).setFlags( 267 DescValue & SF_DescFlagsMask); 268} 269 270void MCMachOStreamer::EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size, 271 unsigned ByteAlignment) { 272 // FIXME: Darwin 'as' does appear to allow redef of a .comm by itself. 273 assert(Symbol->isUndefined() && "Cannot define a symbol twice!"); 274 275 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol); 276 SD.setExternal(true); 277 SD.setCommon(Size, ByteAlignment); 278} 279 280void MCMachOStreamer::EmitZerofill(const MCSection *Section, MCSymbol *Symbol, 281 unsigned Size, unsigned ByteAlignment) { 282 MCSectionData &SectData = getAssembler().getOrCreateSectionData(*Section); 283 284 // The symbol may not be present, which only creates the section. 285 if (!Symbol) 286 return; 287 288 // FIXME: Assert that this section has the zerofill type. 289 290 assert(Symbol->isUndefined() && "Cannot define a symbol twice!"); 291 292 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol); 293 294 // Emit an align fragment if necessary. 295 if (ByteAlignment != 1) 296 new MCAlignFragment(ByteAlignment, 0, 0, ByteAlignment, &SectData); 297 298 MCFragment *F = new MCFillFragment(0, 0, Size, &SectData); 299 SD.setFragment(F); 300 301 Symbol->setSection(*Section); 302 303 // Update the maximum alignment on the zero fill section if necessary. 304 if (ByteAlignment > SectData.getAlignment()) 305 SectData.setAlignment(ByteAlignment); 306} 307 308// This should always be called with the thread local bss section. Like the 309// .zerofill directive this doesn't actually switch sections on us. 310void MCMachOStreamer::EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol, 311 uint64_t Size, unsigned ByteAlignment) { 312 EmitZerofill(Section, Symbol, Size, ByteAlignment); 313 return; 314} 315 316void MCMachOStreamer::EmitBytes(StringRef Data, unsigned AddrSpace) { 317 // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into 318 // MCObjectStreamer. 319 getOrCreateDataFragment()->getContents().append(Data.begin(), Data.end()); 320} 321 322void MCMachOStreamer::EmitValue(const MCExpr *Value, unsigned Size, 323 unsigned AddrSpace) { 324 // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into 325 // MCObjectStreamer. 326 MCDataFragment *DF = getOrCreateDataFragment(); 327 328 // Avoid fixups when possible. 329 int64_t AbsValue; 330 if (AddValueSymbols(Value)->EvaluateAsAbsolute(AbsValue)) { 331 // FIXME: Endianness assumption. 332 for (unsigned i = 0; i != Size; ++i) 333 DF->getContents().push_back(uint8_t(AbsValue >> (i * 8))); 334 } else { 335 DF->addFixup(MCFixup::Create(DF->getContents().size(), 336 AddValueSymbols(Value), 337 MCFixup::getKindForSize(Size, false))); 338 DF->getContents().resize(DF->getContents().size() + Size, 0); 339 } 340} 341 342void MCMachOStreamer::EmitValueToAlignment(unsigned ByteAlignment, 343 int64_t Value, unsigned ValueSize, 344 unsigned MaxBytesToEmit) { 345 // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into 346 // MCObjectStreamer. 347 if (MaxBytesToEmit == 0) 348 MaxBytesToEmit = ByteAlignment; 349 new MCAlignFragment(ByteAlignment, Value, ValueSize, MaxBytesToEmit, 350 getCurrentSectionData()); 351 352 // Update the maximum alignment on the current section if necessary. 353 if (ByteAlignment > getCurrentSectionData()->getAlignment()) 354 getCurrentSectionData()->setAlignment(ByteAlignment); 355} 356 357void MCMachOStreamer::EmitCodeAlignment(unsigned ByteAlignment, 358 unsigned MaxBytesToEmit) { 359 // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into 360 // MCObjectStreamer. 361 if (MaxBytesToEmit == 0) 362 MaxBytesToEmit = ByteAlignment; 363 MCAlignFragment *F = new MCAlignFragment(ByteAlignment, 0, 1, MaxBytesToEmit, 364 getCurrentSectionData()); 365 F->setEmitNops(true); 366 367 // Update the maximum alignment on the current section if necessary. 368 if (ByteAlignment > getCurrentSectionData()->getAlignment()) 369 getCurrentSectionData()->setAlignment(ByteAlignment); 370} 371 372void MCMachOStreamer::EmitValueToOffset(const MCExpr *Offset, 373 unsigned char Value) { 374 new MCOrgFragment(*Offset, Value, getCurrentSectionData()); 375} 376 377void MCMachOStreamer::EmitInstToFragment(const MCInst &Inst) { 378 MCInstFragment *IF = new MCInstFragment(Inst, getCurrentSectionData()); 379 380 // Add the fixups and data. 381 // 382 // FIXME: Revisit this design decision when relaxation is done, we may be 383 // able to get away with not storing any extra data in the MCInst. 384 SmallVector<MCFixup, 4> Fixups; 385 SmallString<256> Code; 386 raw_svector_ostream VecOS(Code); 387 getAssembler().getEmitter().EncodeInstruction(Inst, VecOS, Fixups); 388 VecOS.flush(); 389 390 IF->getCode() = Code; 391 IF->getFixups() = Fixups; 392} 393 394void MCMachOStreamer::EmitInstToData(const MCInst &Inst) { 395 MCDataFragment *DF = getOrCreateDataFragment(); 396 397 SmallVector<MCFixup, 4> Fixups; 398 SmallString<256> Code; 399 raw_svector_ostream VecOS(Code); 400 getAssembler().getEmitter().EncodeInstruction(Inst, VecOS, Fixups); 401 VecOS.flush(); 402 403 // Add the fixups and data. 404 for (unsigned i = 0, e = Fixups.size(); i != e; ++i) { 405 Fixups[i].setOffset(Fixups[i].getOffset() + DF->getContents().size()); 406 DF->addFixup(Fixups[i]); 407 } 408 DF->getContents().append(Code.begin(), Code.end()); 409} 410 411void MCMachOStreamer::Finish() { 412 // Dump out the dwarf file & directory tables and line tables. 413 if (getContext().hasDwarfFiles()) { 414 const MCSection *DwarfLineSection = getContext().getMachOSection("__DWARF", 415 "__debug_line", 416 MCSectionMachO::S_ATTR_DEBUG, 417 0, SectionKind::getDataRelLocal()); 418 MCSectionData &DLS = 419 getAssembler().getOrCreateSectionData(*DwarfLineSection); 420 int PointerSize = getAssembler().getBackend().getPointerSize(); 421 MCDwarfFileTable::Emit(this, DwarfLineSection, &DLS, PointerSize); 422 } 423 424 // We have to set the fragment atom associations so we can relax properly for 425 // Mach-O. 426 427 // First, scan the symbol table to build a lookup table from fragments to 428 // defining symbols. 429 DenseMap<const MCFragment*, MCSymbolData*> DefiningSymbolMap; 430 for (MCAssembler::symbol_iterator it = getAssembler().symbol_begin(), 431 ie = getAssembler().symbol_end(); it != ie; ++it) { 432 if (getAssembler().isSymbolLinkerVisible(it->getSymbol()) && 433 it->getFragment()) { 434 // An atom defining symbol should never be internal to a fragment. 435 assert(it->getOffset() == 0 && "Invalid offset in atom defining symbol!"); 436 DefiningSymbolMap[it->getFragment()] = it; 437 } 438 } 439 440 // Set the fragment atom associations by tracking the last seen atom defining 441 // symbol. 442 for (MCAssembler::iterator it = getAssembler().begin(), 443 ie = getAssembler().end(); it != ie; ++it) { 444 MCSymbolData *CurrentAtom = 0; 445 for (MCSectionData::iterator it2 = it->begin(), 446 ie2 = it->end(); it2 != ie2; ++it2) { 447 if (MCSymbolData *SD = DefiningSymbolMap.lookup(it2)) 448 CurrentAtom = SD; 449 it2->setAtom(CurrentAtom); 450 } 451 } 452 453 this->MCObjectStreamer::Finish(); 454} 455 456MCStreamer *llvm::createMachOStreamer(MCContext &Context, TargetAsmBackend &TAB, 457 raw_ostream &OS, MCCodeEmitter *CE, 458 bool RelaxAll) { 459 MCMachOStreamer *S = new MCMachOStreamer(Context, TAB, OS, CE); 460 if (RelaxAll) 461 S->getAssembler().setRelaxAll(true); 462 return S; 463} 464