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