LLVM API Documentation

MCMachOStreamer.cpp
Go to the documentation of this file.
00001 //===-- MCMachOStreamer.cpp - MachO Streamer ------------------------------===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 
00010 #include "llvm/MC/MCStreamer.h"
00011 #include "llvm/ADT/DenseMap.h"
00012 #include "llvm/ADT/SmallVector.h"
00013 #include "llvm/MC/MCAsmBackend.h"
00014 #include "llvm/MC/MCAssembler.h"
00015 #include "llvm/MC/MCCodeEmitter.h"
00016 #include "llvm/MC/MCContext.h"
00017 #include "llvm/MC/MCDwarf.h"
00018 #include "llvm/MC/MCExpr.h"
00019 #include "llvm/MC/MCInst.h"
00020 #include "llvm/MC/MCLinkerOptimizationHint.h"
00021 #include "llvm/MC/MCMachOSymbolFlags.h"
00022 #include "llvm/MC/MCObjectFileInfo.h"
00023 #include "llvm/MC/MCObjectStreamer.h"
00024 #include "llvm/MC/MCSection.h"
00025 #include "llvm/MC/MCSectionMachO.h"
00026 #include "llvm/MC/MCSymbol.h"
00027 #include "llvm/Support/Dwarf.h"
00028 #include "llvm/Support/ErrorHandling.h"
00029 #include "llvm/Support/raw_ostream.h"
00030 
00031 using namespace llvm;
00032 
00033 namespace {
00034 
00035 class MCMachOStreamer : public MCObjectStreamer {
00036 private:
00037   /// LabelSections - true if each section change should emit a linker local
00038   /// label for use in relocations for assembler local references. Obviates the
00039   /// need for local relocations. False by default.
00040   bool LabelSections;
00041 
00042   /// HasSectionLabel - map of which sections have already had a non-local
00043   /// label emitted to them. Used so we don't emit extraneous linker local
00044   /// labels in the middle of the section.
00045   DenseMap<const MCSection*, bool> HasSectionLabel;
00046 
00047   void EmitInstToData(const MCInst &Inst, const MCSubtargetInfo &STI) override;
00048 
00049   void EmitDataRegion(DataRegionData::KindTy Kind);
00050   void EmitDataRegionEnd();
00051 
00052 public:
00053   MCMachOStreamer(MCContext &Context, MCAsmBackend &MAB, raw_ostream &OS,
00054                   MCCodeEmitter *Emitter, bool label)
00055       : MCObjectStreamer(Context, MAB, OS, Emitter),
00056         LabelSections(label) {}
00057 
00058   /// state management
00059   void reset() override {
00060     HasSectionLabel.clear();
00061     MCObjectStreamer::reset();
00062   }
00063 
00064   /// @name MCStreamer Interface
00065   /// @{
00066 
00067   void ChangeSection(const MCSection *Sect, const MCExpr *Subsect) override;
00068   void EmitLabel(MCSymbol *Symbol) override;
00069   void EmitEHSymAttributes(const MCSymbol *Symbol, MCSymbol *EHSymbol) override;
00070   void EmitAssemblerFlag(MCAssemblerFlag Flag) override;
00071   void EmitLinkerOptions(ArrayRef<std::string> Options) override;
00072   void EmitDataRegion(MCDataRegionType Kind) override;
00073   void EmitVersionMin(MCVersionMinType Kind, unsigned Major,
00074                       unsigned Minor, unsigned Update) override;
00075   void EmitThumbFunc(MCSymbol *Func) override;
00076   bool EmitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute) override;
00077   void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) override;
00078   void EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
00079                         unsigned ByteAlignment) override;
00080   void BeginCOFFSymbolDef(const MCSymbol *Symbol) override {
00081     llvm_unreachable("macho doesn't support this directive");
00082   }
00083   void EmitCOFFSymbolStorageClass(int StorageClass) override {
00084     llvm_unreachable("macho doesn't support this directive");
00085   }
00086   void EmitCOFFSymbolType(int Type) override {
00087     llvm_unreachable("macho doesn't support this directive");
00088   }
00089   void EndCOFFSymbolDef() override {
00090     llvm_unreachable("macho doesn't support this directive");
00091   }
00092   void EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) override {
00093     llvm_unreachable("macho doesn't support this directive");
00094   }
00095   void EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size,
00096                              unsigned ByteAlignment) override;
00097   void EmitZerofill(const MCSection *Section, MCSymbol *Symbol = nullptr,
00098                     uint64_t Size = 0, unsigned ByteAlignment = 0) override;
00099   void EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol, uint64_t Size,
00100                       unsigned ByteAlignment = 0) override;
00101 
00102   void EmitFileDirective(StringRef Filename) override {
00103     // FIXME: Just ignore the .file; it isn't important enough to fail the
00104     // entire assembly.
00105 
00106     // report_fatal_error("unsupported directive: '.file'");
00107   }
00108 
00109   void EmitIdent(StringRef IdentString) override {
00110     llvm_unreachable("macho doesn't support this directive");
00111   }
00112 
00113   void EmitLOHDirective(MCLOHType Kind, const MCLOHArgs &Args) override {
00114     getAssembler().getLOHContainer().addDirective(Kind, Args);
00115   }
00116 
00117   void FinishImpl() override;
00118 };
00119 
00120 } // end anonymous namespace.
00121 
00122 void MCMachOStreamer::ChangeSection(const MCSection *Section,
00123                                     const MCExpr *Subsection) {
00124   // Change the section normally.
00125   MCObjectStreamer::ChangeSection(Section, Subsection);
00126   // Output a linker-local symbol so we don't need section-relative local
00127   // relocations. The linker hates us when we do that.
00128   if (LabelSections && !HasSectionLabel[Section]) {
00129     MCSymbol *Label = getContext().CreateLinkerPrivateTempSymbol();
00130     EmitLabel(Label);
00131     HasSectionLabel[Section] = true;
00132   }
00133 }
00134 
00135 void MCMachOStreamer::EmitEHSymAttributes(const MCSymbol *Symbol,
00136                                           MCSymbol *EHSymbol) {
00137   MCSymbolData &SD =
00138     getAssembler().getOrCreateSymbolData(*Symbol);
00139   if (SD.isExternal())
00140     EmitSymbolAttribute(EHSymbol, MCSA_Global);
00141   if (SD.getFlags() & SF_WeakDefinition)
00142     EmitSymbolAttribute(EHSymbol, MCSA_WeakDefinition);
00143   if (SD.isPrivateExtern())
00144     EmitSymbolAttribute(EHSymbol, MCSA_PrivateExtern);
00145 }
00146 
00147 void MCMachOStreamer::EmitLabel(MCSymbol *Symbol) {
00148   assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
00149 
00150   // isSymbolLinkerVisible uses the section.
00151   AssignSection(Symbol, getCurrentSection().first);
00152   // We have to create a new fragment if this is an atom defining symbol,
00153   // fragments cannot span atoms.
00154   if (getAssembler().isSymbolLinkerVisible(*Symbol))
00155     insert(new MCDataFragment());
00156 
00157   MCObjectStreamer::EmitLabel(Symbol);
00158 
00159   MCSymbolData &SD = getAssembler().getSymbolData(*Symbol);
00160   // This causes the reference type flag to be cleared. Darwin 'as' was "trying"
00161   // to clear the weak reference and weak definition bits too, but the
00162   // implementation was buggy. For now we just try to match 'as', for
00163   // diffability.
00164   //
00165   // FIXME: Cleanup this code, these bits should be emitted based on semantic
00166   // properties, not on the order of definition, etc.
00167   SD.setFlags(SD.getFlags() & ~SF_ReferenceTypeMask);
00168 }
00169 
00170 void MCMachOStreamer::EmitDataRegion(DataRegionData::KindTy Kind) {
00171   if (!getAssembler().getBackend().hasDataInCodeSupport())
00172     return;
00173   // Create a temporary label to mark the start of the data region.
00174   MCSymbol *Start = getContext().CreateTempSymbol();
00175   EmitLabel(Start);
00176   // Record the region for the object writer to use.
00177   DataRegionData Data = { Kind, Start, nullptr };
00178   std::vector<DataRegionData> &Regions = getAssembler().getDataRegions();
00179   Regions.push_back(Data);
00180 }
00181 
00182 void MCMachOStreamer::EmitDataRegionEnd() {
00183   if (!getAssembler().getBackend().hasDataInCodeSupport())
00184     return;
00185   std::vector<DataRegionData> &Regions = getAssembler().getDataRegions();
00186   assert(Regions.size() && "Mismatched .end_data_region!");
00187   DataRegionData &Data = Regions.back();
00188   assert(!Data.End && "Mismatched .end_data_region!");
00189   // Create a temporary label to mark the end of the data region.
00190   Data.End = getContext().CreateTempSymbol();
00191   EmitLabel(Data.End);
00192 }
00193 
00194 void MCMachOStreamer::EmitAssemblerFlag(MCAssemblerFlag Flag) {
00195   // Let the target do whatever target specific stuff it needs to do.
00196   getAssembler().getBackend().handleAssemblerFlag(Flag);
00197   // Do any generic stuff we need to do.
00198   switch (Flag) {
00199   case MCAF_SyntaxUnified: return; // no-op here.
00200   case MCAF_Code16: return; // Change parsing mode; no-op here.
00201   case MCAF_Code32: return; // Change parsing mode; no-op here.
00202   case MCAF_Code64: return; // Change parsing mode; no-op here.
00203   case MCAF_SubsectionsViaSymbols:
00204     getAssembler().setSubsectionsViaSymbols(true);
00205     return;
00206   }
00207 }
00208 
00209 void MCMachOStreamer::EmitLinkerOptions(ArrayRef<std::string> Options) {
00210   getAssembler().getLinkerOptions().push_back(Options);
00211 }
00212 
00213 void MCMachOStreamer::EmitDataRegion(MCDataRegionType Kind) {
00214   switch (Kind) {
00215   case MCDR_DataRegion:
00216     EmitDataRegion(DataRegionData::Data);
00217     return;
00218   case MCDR_DataRegionJT8:
00219     EmitDataRegion(DataRegionData::JumpTable8);
00220     return;
00221   case MCDR_DataRegionJT16:
00222     EmitDataRegion(DataRegionData::JumpTable16);
00223     return;
00224   case MCDR_DataRegionJT32:
00225     EmitDataRegion(DataRegionData::JumpTable32);
00226     return;
00227   case MCDR_DataRegionEnd:
00228     EmitDataRegionEnd();
00229     return;
00230   }
00231 }
00232 
00233 void MCMachOStreamer::EmitVersionMin(MCVersionMinType Kind, unsigned Major,
00234                                      unsigned Minor, unsigned Update) {
00235   getAssembler().setVersionMinInfo(Kind, Major, Minor, Update);
00236 }
00237 
00238 void MCMachOStreamer::EmitThumbFunc(MCSymbol *Symbol) {
00239   // Remember that the function is a thumb function. Fixup and relocation
00240   // values will need adjusted.
00241   getAssembler().setIsThumbFunc(Symbol);
00242 }
00243 
00244 bool MCMachOStreamer::EmitSymbolAttribute(MCSymbol *Symbol,
00245                                           MCSymbolAttr Attribute) {
00246   // Indirect symbols are handled differently, to match how 'as' handles
00247   // them. This makes writing matching .o files easier.
00248   if (Attribute == MCSA_IndirectSymbol) {
00249     // Note that we intentionally cannot use the symbol data here; this is
00250     // important for matching the string table that 'as' generates.
00251     IndirectSymbolData ISD;
00252     ISD.Symbol = Symbol;
00253     ISD.SectionData = getCurrentSectionData();
00254     getAssembler().getIndirectSymbols().push_back(ISD);
00255     return true;
00256   }
00257 
00258   // Adding a symbol attribute always introduces the symbol, note that an
00259   // important side effect of calling getOrCreateSymbolData here is to register
00260   // the symbol with the assembler.
00261   MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
00262 
00263   // The implementation of symbol attributes is designed to match 'as', but it
00264   // leaves much to desired. It doesn't really make sense to arbitrarily add and
00265   // remove flags, but 'as' allows this (in particular, see .desc).
00266   //
00267   // In the future it might be worth trying to make these operations more well
00268   // defined.
00269   switch (Attribute) {
00270   case MCSA_Invalid:
00271   case MCSA_ELF_TypeFunction:
00272   case MCSA_ELF_TypeIndFunction:
00273   case MCSA_ELF_TypeObject:
00274   case MCSA_ELF_TypeTLS:
00275   case MCSA_ELF_TypeCommon:
00276   case MCSA_ELF_TypeNoType:
00277   case MCSA_ELF_TypeGnuUniqueObject:
00278   case MCSA_Hidden:
00279   case MCSA_IndirectSymbol:
00280   case MCSA_Internal:
00281   case MCSA_Protected:
00282   case MCSA_Weak:
00283   case MCSA_Local:
00284     return false;
00285 
00286   case MCSA_Global:
00287     SD.setExternal(true);
00288     // This effectively clears the undefined lazy bit, in Darwin 'as', although
00289     // it isn't very consistent because it implements this as part of symbol
00290     // lookup.
00291     //
00292     // FIXME: Cleanup this code, these bits should be emitted based on semantic
00293     // properties, not on the order of definition, etc.
00294     SD.setFlags(SD.getFlags() & ~SF_ReferenceTypeUndefinedLazy);
00295     break;
00296 
00297   case MCSA_LazyReference:
00298     // FIXME: This requires -dynamic.
00299     SD.setFlags(SD.getFlags() | SF_NoDeadStrip);
00300     if (Symbol->isUndefined())
00301       SD.setFlags(SD.getFlags() | SF_ReferenceTypeUndefinedLazy);
00302     break;
00303 
00304     // Since .reference sets the no dead strip bit, it is equivalent to
00305     // .no_dead_strip in practice.
00306   case MCSA_Reference:
00307   case MCSA_NoDeadStrip:
00308     SD.setFlags(SD.getFlags() | SF_NoDeadStrip);
00309     break;
00310 
00311   case MCSA_SymbolResolver:
00312     SD.setFlags(SD.getFlags() | SF_SymbolResolver);
00313     break;
00314 
00315   case MCSA_PrivateExtern:
00316     SD.setExternal(true);
00317     SD.setPrivateExtern(true);
00318     break;
00319 
00320   case MCSA_WeakReference:
00321     // FIXME: This requires -dynamic.
00322     if (Symbol->isUndefined())
00323       SD.setFlags(SD.getFlags() | SF_WeakReference);
00324     break;
00325 
00326   case MCSA_WeakDefinition:
00327     // FIXME: 'as' enforces that this is defined and global. The manual claims
00328     // it has to be in a coalesced section, but this isn't enforced.
00329     SD.setFlags(SD.getFlags() | SF_WeakDefinition);
00330     break;
00331 
00332   case MCSA_WeakDefAutoPrivate:
00333     SD.setFlags(SD.getFlags() | SF_WeakDefinition | SF_WeakReference);
00334     break;
00335   }
00336 
00337   return true;
00338 }
00339 
00340 void MCMachOStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
00341   // Encode the 'desc' value into the lowest implementation defined bits.
00342   assert(DescValue == (DescValue & SF_DescFlagsMask) &&
00343          "Invalid .desc value!");
00344   getAssembler().getOrCreateSymbolData(*Symbol).setFlags(
00345     DescValue & SF_DescFlagsMask);
00346 }
00347 
00348 void MCMachOStreamer::EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
00349                                        unsigned ByteAlignment) {
00350   // FIXME: Darwin 'as' does appear to allow redef of a .comm by itself.
00351   assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
00352 
00353   AssignSection(Symbol, nullptr);
00354 
00355   MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
00356   SD.setExternal(true);
00357   SD.setCommon(Size, ByteAlignment);
00358 }
00359 
00360 void MCMachOStreamer::EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size,
00361                                             unsigned ByteAlignment) {
00362   // '.lcomm' is equivalent to '.zerofill'.
00363   return EmitZerofill(getContext().getObjectFileInfo()->getDataBSSSection(),
00364                       Symbol, Size, ByteAlignment);
00365 }
00366 
00367 void MCMachOStreamer::EmitZerofill(const MCSection *Section, MCSymbol *Symbol,
00368                                    uint64_t Size, unsigned ByteAlignment) {
00369   MCSectionData &SectData = getAssembler().getOrCreateSectionData(*Section);
00370 
00371   // The symbol may not be present, which only creates the section.
00372   if (!Symbol)
00373     return;
00374 
00375   // On darwin all virtual sections have zerofill type.
00376   assert(Section->isVirtualSection() && "Section does not have zerofill type!");
00377 
00378   assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
00379 
00380   MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
00381 
00382   // Emit an align fragment if necessary.
00383   if (ByteAlignment != 1)
00384     new MCAlignFragment(ByteAlignment, 0, 0, ByteAlignment, &SectData);
00385 
00386   MCFragment *F = new MCFillFragment(0, 0, Size, &SectData);
00387   SD.setFragment(F);
00388 
00389   AssignSection(Symbol, Section);
00390 
00391   // Update the maximum alignment on the zero fill section if necessary.
00392   if (ByteAlignment > SectData.getAlignment())
00393     SectData.setAlignment(ByteAlignment);
00394 }
00395 
00396 // This should always be called with the thread local bss section.  Like the
00397 // .zerofill directive this doesn't actually switch sections on us.
00398 void MCMachOStreamer::EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol,
00399                                      uint64_t Size, unsigned ByteAlignment) {
00400   EmitZerofill(Section, Symbol, Size, ByteAlignment);
00401   return;
00402 }
00403 
00404 void MCMachOStreamer::EmitInstToData(const MCInst &Inst,
00405                                      const MCSubtargetInfo &STI) {
00406   MCDataFragment *DF = getOrCreateDataFragment();
00407 
00408   SmallVector<MCFixup, 4> Fixups;
00409   SmallString<256> Code;
00410   raw_svector_ostream VecOS(Code);
00411   getAssembler().getEmitter().EncodeInstruction(Inst, VecOS, Fixups, STI);
00412   VecOS.flush();
00413 
00414   // Add the fixups and data.
00415   for (unsigned i = 0, e = Fixups.size(); i != e; ++i) {
00416     Fixups[i].setOffset(Fixups[i].getOffset() + DF->getContents().size());
00417     DF->getFixups().push_back(Fixups[i]);
00418   }
00419   DF->getContents().append(Code.begin(), Code.end());
00420 }
00421 
00422 void MCMachOStreamer::FinishImpl() {
00423   EmitFrames(&getAssembler().getBackend());
00424 
00425   // We have to set the fragment atom associations so we can relax properly for
00426   // Mach-O.
00427 
00428   // First, scan the symbol table to build a lookup table from fragments to
00429   // defining symbols.
00430   DenseMap<const MCFragment*, MCSymbolData*> DefiningSymbolMap;
00431   for (MCSymbolData &SD : getAssembler().symbols()) {
00432     if (getAssembler().isSymbolLinkerVisible(SD.getSymbol()) &&
00433         SD.getFragment()) {
00434       // An atom defining symbol should never be internal to a fragment.
00435       assert(SD.getOffset() == 0 && "Invalid offset in atom defining symbol!");
00436       DefiningSymbolMap[SD.getFragment()] = &SD;
00437     }
00438   }
00439 
00440   // Set the fragment atom associations by tracking the last seen atom defining
00441   // symbol.
00442   for (MCAssembler::iterator it = getAssembler().begin(),
00443          ie = getAssembler().end(); it != ie; ++it) {
00444     MCSymbolData *CurrentAtom = nullptr;
00445     for (MCSectionData::iterator it2 = it->begin(),
00446            ie2 = it->end(); it2 != ie2; ++it2) {
00447       if (MCSymbolData *SD = DefiningSymbolMap.lookup(it2))
00448         CurrentAtom = SD;
00449       it2->setAtom(CurrentAtom);
00450     }
00451   }
00452 
00453   this->MCObjectStreamer::FinishImpl();
00454 }
00455 
00456 MCStreamer *llvm::createMachOStreamer(MCContext &Context, MCAsmBackend &MAB,
00457                                       raw_ostream &OS, MCCodeEmitter *CE,
00458                                       bool RelaxAll,
00459                                       bool LabelSections) {
00460   MCMachOStreamer *S = new MCMachOStreamer(Context, MAB, OS, CE, LabelSections);
00461   if (RelaxAll)
00462     S->getAssembler().setRelaxAll(true);
00463   return S;
00464 }