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