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