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