Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 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 | |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 12 | #include "llvm/MC/MCAssembler.h" |
| 13 | #include "llvm/MC/MCContext.h" |
Daniel Dunbar | 4fac749 | 2009-08-27 08:17:51 +0000 | [diff] [blame] | 14 | #include "llvm/MC/MCCodeEmitter.h" |
Daniel Dunbar | 8c2eebe | 2009-08-31 08:08:38 +0000 | [diff] [blame] | 15 | #include "llvm/MC/MCExpr.h" |
Daniel Dunbar | 4fac749 | 2009-08-27 08:17:51 +0000 | [diff] [blame] | 16 | #include "llvm/MC/MCInst.h" |
Daniel Dunbar | 8dc68ab | 2010-06-16 20:04:22 +0000 | [diff] [blame^] | 17 | #include "llvm/MC/MCObjectStreamer.h" |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 18 | #include "llvm/MC/MCSection.h" |
| 19 | #include "llvm/MC/MCSymbol.h" |
Kevin Enderby | a6eeb6e | 2010-05-07 21:44:23 +0000 | [diff] [blame] | 20 | #include "llvm/MC/MCMachOSymbolFlags.h" |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 21 | #include "llvm/Support/ErrorHandling.h" |
Daniel Dunbar | 4fac749 | 2009-08-27 08:17:51 +0000 | [diff] [blame] | 22 | #include "llvm/Support/raw_ostream.h" |
Daniel Dunbar | d8036fb | 2010-03-23 05:09:03 +0000 | [diff] [blame] | 23 | #include "llvm/Target/TargetAsmBackend.h" |
| 24 | |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 25 | using namespace llvm; |
| 26 | |
| 27 | namespace { |
| 28 | |
Daniel Dunbar | 8dc68ab | 2010-06-16 20:04:22 +0000 | [diff] [blame^] | 29 | class MCMachOStreamer : public MCObjectStreamer { |
Daniel Dunbar | 6aff2fb | 2009-08-24 08:40:12 +0000 | [diff] [blame] | 30 | |
| 31 | private: |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 32 | MCSectionData *CurSectionData; |
Daniel Dunbar | f3d2ef0 | 2009-08-22 10:13:24 +0000 | [diff] [blame] | 33 | |
Daniel Dunbar | 071f73d | 2010-05-10 22:45:09 +0000 | [diff] [blame] | 34 | /// Track the current atom for each section. |
| 35 | DenseMap<const MCSectionData*, MCSymbolData*> CurrentAtomMap; |
| 36 | |
Daniel Dunbar | f3d2ef0 | 2009-08-22 10:13:24 +0000 | [diff] [blame] | 37 | private: |
| 38 | MCFragment *getCurrentFragment() const { |
| 39 | assert(CurSectionData && "No current section!"); |
| 40 | |
| 41 | if (!CurSectionData->empty()) |
| 42 | return &CurSectionData->getFragmentList().back(); |
| 43 | |
| 44 | return 0; |
| 45 | } |
| 46 | |
Daniel Dunbar | f70f477 | 2010-03-22 20:35:46 +0000 | [diff] [blame] | 47 | /// Get a data fragment to write into, creating a new one if the current |
| 48 | /// fragment is not a data fragment. |
| 49 | MCDataFragment *getOrCreateDataFragment() const { |
| 50 | MCDataFragment *F = dyn_cast_or_null<MCDataFragment>(getCurrentFragment()); |
| 51 | if (!F) |
Daniel Dunbar | 071f73d | 2010-05-10 22:45:09 +0000 | [diff] [blame] | 52 | F = createDataFragment(); |
Daniel Dunbar | f70f477 | 2010-03-22 20:35:46 +0000 | [diff] [blame] | 53 | return F; |
| 54 | } |
| 55 | |
Daniel Dunbar | 071f73d | 2010-05-10 22:45:09 +0000 | [diff] [blame] | 56 | /// Create a new data fragment in the current section. |
| 57 | MCDataFragment *createDataFragment() const { |
| 58 | MCDataFragment *DF = new MCDataFragment(CurSectionData); |
| 59 | DF->setAtom(CurrentAtomMap.lookup(CurSectionData)); |
| 60 | return DF; |
| 61 | } |
| 62 | |
Daniel Dunbar | 2ac0c45 | 2010-05-26 20:37:00 +0000 | [diff] [blame] | 63 | void EmitInstToFragment(const MCInst &Inst); |
| 64 | void EmitInstToData(const MCInst &Inst); |
| 65 | |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 66 | public: |
Daniel Dunbar | 1f3e445 | 2010-03-11 01:34:27 +0000 | [diff] [blame] | 67 | MCMachOStreamer(MCContext &Context, TargetAsmBackend &TAB, |
Daniel Dunbar | 8dc68ab | 2010-06-16 20:04:22 +0000 | [diff] [blame^] | 68 | raw_ostream &OS, MCCodeEmitter *Emitter) |
| 69 | : MCObjectStreamer(Context, TAB, OS, Emitter), CurSectionData(0) {} |
Daniel Dunbar | ac2884a | 2010-03-25 22:49:09 +0000 | [diff] [blame] | 70 | |
Daniel Dunbar | 8c2eebe | 2009-08-31 08:08:38 +0000 | [diff] [blame] | 71 | const MCExpr *AddValueSymbols(const MCExpr *Value) { |
| 72 | switch (Value->getKind()) { |
Chris Lattner | 5d917a8 | 2010-02-08 19:41:07 +0000 | [diff] [blame] | 73 | case MCExpr::Target: assert(0 && "Can't handle target exprs yet!"); |
Daniel Dunbar | 8c2eebe | 2009-08-31 08:08:38 +0000 | [diff] [blame] | 74 | case MCExpr::Constant: |
| 75 | break; |
| 76 | |
| 77 | case MCExpr::Binary: { |
| 78 | const MCBinaryExpr *BE = cast<MCBinaryExpr>(Value); |
| 79 | AddValueSymbols(BE->getLHS()); |
| 80 | AddValueSymbols(BE->getRHS()); |
| 81 | break; |
| 82 | } |
| 83 | |
| 84 | case MCExpr::SymbolRef: |
Daniel Dunbar | 8dc68ab | 2010-06-16 20:04:22 +0000 | [diff] [blame^] | 85 | getAssembler().getOrCreateSymbolData( |
Daniel Dunbar | 46836a7 | 2010-03-10 20:58:29 +0000 | [diff] [blame] | 86 | cast<MCSymbolRefExpr>(Value)->getSymbol()); |
Daniel Dunbar | 8c2eebe | 2009-08-31 08:08:38 +0000 | [diff] [blame] | 87 | break; |
| 88 | |
| 89 | case MCExpr::Unary: |
| 90 | AddValueSymbols(cast<MCUnaryExpr>(Value)->getSubExpr()); |
| 91 | break; |
| 92 | } |
| 93 | |
| 94 | return Value; |
| 95 | } |
| 96 | |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 97 | /// @name MCStreamer Interface |
| 98 | /// @{ |
| 99 | |
| 100 | virtual void SwitchSection(const MCSection *Section); |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 101 | virtual void EmitLabel(MCSymbol *Symbol); |
Chris Lattner | a5ad93a | 2010-01-23 06:39:22 +0000 | [diff] [blame] | 102 | virtual void EmitAssemblerFlag(MCAssemblerFlag Flag); |
Daniel Dunbar | 821e333 | 2009-08-31 08:09:28 +0000 | [diff] [blame] | 103 | virtual void EmitAssignment(MCSymbol *Symbol, const MCExpr *Value); |
Chris Lattner | a5ad93a | 2010-01-23 06:39:22 +0000 | [diff] [blame] | 104 | virtual void EmitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute); |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 105 | virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue); |
Chris Lattner | 9eb158d | 2010-01-23 07:47:02 +0000 | [diff] [blame] | 106 | virtual void EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size, |
Daniel Dunbar | 7092c7e | 2009-08-30 06:17:16 +0000 | [diff] [blame] | 107 | unsigned ByteAlignment); |
Chris Lattner | b54b9dd | 2010-05-08 19:54:22 +0000 | [diff] [blame] | 108 | virtual void BeginCOFFSymbolDef(const MCSymbol *Symbol) { |
| 109 | assert(0 && "macho doesn't support this directive"); |
| 110 | } |
| 111 | virtual void EmitCOFFSymbolStorageClass(int StorageClass) { |
| 112 | assert(0 && "macho doesn't support this directive"); |
| 113 | } |
| 114 | virtual void EmitCOFFSymbolType(int Type) { |
| 115 | assert(0 && "macho doesn't support this directive"); |
| 116 | } |
| 117 | virtual void EndCOFFSymbolDef() { |
| 118 | assert(0 && "macho doesn't support this directive"); |
| 119 | } |
Chris Lattner | 99328ad | 2010-01-25 07:52:13 +0000 | [diff] [blame] | 120 | virtual void EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) { |
| 121 | assert(0 && "macho doesn't support this directive"); |
| 122 | } |
Chris Lattner | 9eb158d | 2010-01-23 07:47:02 +0000 | [diff] [blame] | 123 | virtual void EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size) { |
| 124 | assert(0 && "macho doesn't support this directive"); |
| 125 | } |
Daniel Dunbar | 8751b94 | 2009-08-28 05:48:22 +0000 | [diff] [blame] | 126 | virtual void EmitZerofill(const MCSection *Section, MCSymbol *Symbol = 0, |
Daniel Dunbar | 7092c7e | 2009-08-30 06:17:16 +0000 | [diff] [blame] | 127 | unsigned Size = 0, unsigned ByteAlignment = 0); |
Eric Christopher | 011c3f1 | 2010-05-18 21:26:41 +0000 | [diff] [blame] | 128 | virtual void EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol, |
| 129 | uint64_t Size, unsigned ByteAlignment = 0); |
Chris Lattner | aaec205 | 2010-01-19 19:46:13 +0000 | [diff] [blame] | 130 | virtual void EmitBytes(StringRef Data, unsigned AddrSpace); |
Chris Lattner | aaec205 | 2010-01-19 19:46:13 +0000 | [diff] [blame] | 131 | virtual void EmitValue(const MCExpr *Value, unsigned Size,unsigned AddrSpace); |
Chris Lattner | 718fb59 | 2010-01-25 21:28:50 +0000 | [diff] [blame] | 132 | virtual void EmitGPRel32Value(const MCExpr *Value) { |
| 133 | assert(0 && "macho doesn't support this directive"); |
| 134 | } |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 135 | virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0, |
| 136 | unsigned ValueSize = 1, |
| 137 | unsigned MaxBytesToEmit = 0); |
Kevin Enderby | 6e72048 | 2010-02-23 18:26:34 +0000 | [diff] [blame] | 138 | virtual void EmitCodeAlignment(unsigned ByteAlignment, |
| 139 | unsigned MaxBytesToEmit = 0); |
Daniel Dunbar | 821e333 | 2009-08-31 08:09:28 +0000 | [diff] [blame] | 140 | virtual void EmitValueToOffset(const MCExpr *Offset, |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 141 | unsigned char Value = 0); |
Daniel Dunbar | 2ac0c45 | 2010-05-26 20:37:00 +0000 | [diff] [blame] | 142 | |
Chris Lattner | a6594fc | 2010-01-25 18:58:59 +0000 | [diff] [blame] | 143 | virtual void EmitFileDirective(StringRef Filename) { |
Daniel Dunbar | ca1212d | 2010-05-18 17:28:17 +0000 | [diff] [blame] | 144 | report_fatal_error("unsupported directive: '.file'"); |
Chris Lattner | a6594fc | 2010-01-25 18:58:59 +0000 | [diff] [blame] | 145 | } |
| 146 | virtual void EmitDwarfFileDirective(unsigned FileNo, StringRef Filename) { |
Daniel Dunbar | ca1212d | 2010-05-18 17:28:17 +0000 | [diff] [blame] | 147 | report_fatal_error("unsupported directive: '.file'"); |
Chris Lattner | a6594fc | 2010-01-25 18:58:59 +0000 | [diff] [blame] | 148 | } |
Daniel Dunbar | 2ac0c45 | 2010-05-26 20:37:00 +0000 | [diff] [blame] | 149 | |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 150 | virtual void EmitInstruction(const MCInst &Inst); |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 151 | virtual void Finish(); |
| 152 | |
| 153 | /// @} |
| 154 | }; |
| 155 | |
| 156 | } // end anonymous namespace. |
| 157 | |
| 158 | void MCMachOStreamer::SwitchSection(const MCSection *Section) { |
| 159 | assert(Section && "Cannot switch to a null section!"); |
Daniel Dunbar | 2ac0c45 | 2010-05-26 20:37:00 +0000 | [diff] [blame] | 160 | |
Chris Lattner | cda7f78 | 2009-08-22 19:35:08 +0000 | [diff] [blame] | 161 | // If already in this section, then this is a noop. |
| 162 | if (Section == CurSection) return; |
Daniel Dunbar | 0f5fa69 | 2009-08-28 05:48:54 +0000 | [diff] [blame] | 163 | |
Chris Lattner | cda7f78 | 2009-08-22 19:35:08 +0000 | [diff] [blame] | 164 | CurSection = Section; |
Daniel Dunbar | 8dc68ab | 2010-06-16 20:04:22 +0000 | [diff] [blame^] | 165 | CurSectionData = &getAssembler().getOrCreateSectionData(*Section); |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | void MCMachOStreamer::EmitLabel(MCSymbol *Symbol) { |
Daniel Dunbar | 8906ff1 | 2009-08-22 07:22:36 +0000 | [diff] [blame] | 169 | assert(Symbol->isUndefined() && "Cannot define a symbol twice!"); |
Daniel Dunbar | c304718 | 2010-05-05 19:01:00 +0000 | [diff] [blame] | 170 | assert(!Symbol->isVariable() && "Cannot emit a variable symbol!"); |
| 171 | assert(CurSection && "Cannot emit before setting section!"); |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 172 | |
Daniel Dunbar | 8dc68ab | 2010-06-16 20:04:22 +0000 | [diff] [blame^] | 173 | MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol); |
Daniel Dunbar | 071f73d | 2010-05-10 22:45:09 +0000 | [diff] [blame] | 174 | |
| 175 | // Update the current atom map, if necessary. |
| 176 | bool MustCreateFragment = false; |
Daniel Dunbar | 8dc68ab | 2010-06-16 20:04:22 +0000 | [diff] [blame^] | 177 | if (getAssembler().isSymbolLinkerVisible(&SD)) { |
Daniel Dunbar | 071f73d | 2010-05-10 22:45:09 +0000 | [diff] [blame] | 178 | CurrentAtomMap[CurSectionData] = &SD; |
| 179 | |
| 180 | // We have to create a new fragment, fragments cannot span atoms. |
| 181 | MustCreateFragment = true; |
| 182 | } |
| 183 | |
Daniel Dunbar | f70f477 | 2010-03-22 20:35:46 +0000 | [diff] [blame] | 184 | // FIXME: This is wasteful, we don't necessarily need to create a data |
| 185 | // fragment. Instead, we should mark the symbol as pointing into the data |
| 186 | // fragment if it exists, otherwise we should just queue the label and set its |
| 187 | // fragment pointer when we emit the next fragment. |
Daniel Dunbar | 071f73d | 2010-05-10 22:45:09 +0000 | [diff] [blame] | 188 | MCDataFragment *F = |
| 189 | MustCreateFragment ? createDataFragment() : getOrCreateDataFragment(); |
Daniel Dunbar | f3d2ef0 | 2009-08-22 10:13:24 +0000 | [diff] [blame] | 190 | assert(!SD.getFragment() && "Unexpected fragment on symbol data!"); |
| 191 | SD.setFragment(F); |
| 192 | SD.setOffset(F->getContents().size()); |
Daniel Dunbar | 6aff2fb | 2009-08-24 08:40:12 +0000 | [diff] [blame] | 193 | |
Daniel Dunbar | b18d2dd | 2010-05-17 20:12:31 +0000 | [diff] [blame] | 194 | // This causes the reference type flag to be cleared. Darwin 'as' was "trying" |
| 195 | // to clear the weak reference and weak definition bits too, but the |
| 196 | // implementation was buggy. For now we just try to match 'as', for |
| 197 | // diffability. |
| 198 | // |
| 199 | // FIXME: Cleanup this code, these bits should be emitted based on semantic |
| 200 | // properties, not on the order of definition, etc. |
| 201 | SD.setFlags(SD.getFlags() & ~SF_ReferenceTypeMask); |
Daniel Dunbar | 2ac0c45 | 2010-05-26 20:37:00 +0000 | [diff] [blame] | 202 | |
Daniel Dunbar | 8906ff1 | 2009-08-22 07:22:36 +0000 | [diff] [blame] | 203 | Symbol->setSection(*CurSection); |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 204 | } |
| 205 | |
Chris Lattner | a5ad93a | 2010-01-23 06:39:22 +0000 | [diff] [blame] | 206 | void MCMachOStreamer::EmitAssemblerFlag(MCAssemblerFlag Flag) { |
Daniel Dunbar | 6009db4 | 2009-08-26 21:22:22 +0000 | [diff] [blame] | 207 | switch (Flag) { |
Chris Lattner | a5ad93a | 2010-01-23 06:39:22 +0000 | [diff] [blame] | 208 | case MCAF_SubsectionsViaSymbols: |
Daniel Dunbar | 8dc68ab | 2010-06-16 20:04:22 +0000 | [diff] [blame^] | 209 | getAssembler().setSubsectionsViaSymbols(true); |
Daniel Dunbar | 8c3eaf4 | 2009-08-28 07:08:47 +0000 | [diff] [blame] | 210 | return; |
Daniel Dunbar | 6009db4 | 2009-08-26 21:22:22 +0000 | [diff] [blame] | 211 | } |
Daniel Dunbar | 8c3eaf4 | 2009-08-28 07:08:47 +0000 | [diff] [blame] | 212 | |
| 213 | assert(0 && "invalid assembler flag!"); |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 214 | } |
| 215 | |
Daniel Dunbar | 821e333 | 2009-08-31 08:09:28 +0000 | [diff] [blame] | 216 | void MCMachOStreamer::EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) { |
Daniel Dunbar | a4d8667 | 2009-10-16 01:58:23 +0000 | [diff] [blame] | 217 | // FIXME: Lift context changes into super class. |
Daniel Dunbar | 8dc68ab | 2010-06-16 20:04:22 +0000 | [diff] [blame^] | 218 | getAssembler().getOrCreateSymbolData(*Symbol); |
Daniel Dunbar | 08a408a | 2010-05-05 17:41:00 +0000 | [diff] [blame] | 219 | Symbol->setVariableValue(AddValueSymbols(Value)); |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | void MCMachOStreamer::EmitSymbolAttribute(MCSymbol *Symbol, |
Chris Lattner | a5ad93a | 2010-01-23 06:39:22 +0000 | [diff] [blame] | 223 | MCSymbolAttr Attribute) { |
Daniel Dunbar | 0c7761b | 2009-08-24 11:56:58 +0000 | [diff] [blame] | 224 | // Indirect symbols are handled differently, to match how 'as' handles |
| 225 | // them. This makes writing matching .o files easier. |
Chris Lattner | a5ad93a | 2010-01-23 06:39:22 +0000 | [diff] [blame] | 226 | if (Attribute == MCSA_IndirectSymbol) { |
Daniel Dunbar | ad7c3d5 | 2009-08-26 00:18:21 +0000 | [diff] [blame] | 227 | // Note that we intentionally cannot use the symbol data here; this is |
| 228 | // important for matching the string table that 'as' generates. |
Daniel Dunbar | 0c7761b | 2009-08-24 11:56:58 +0000 | [diff] [blame] | 229 | IndirectSymbolData ISD; |
| 230 | ISD.Symbol = Symbol; |
| 231 | ISD.SectionData = CurSectionData; |
Daniel Dunbar | 8dc68ab | 2010-06-16 20:04:22 +0000 | [diff] [blame^] | 232 | getAssembler().getIndirectSymbols().push_back(ISD); |
Daniel Dunbar | 0c7761b | 2009-08-24 11:56:58 +0000 | [diff] [blame] | 233 | return; |
| 234 | } |
| 235 | |
Daniel Dunbar | 6aff2fb | 2009-08-24 08:40:12 +0000 | [diff] [blame] | 236 | // Adding a symbol attribute always introduces the symbol, note that an |
Daniel Dunbar | 46836a7 | 2010-03-10 20:58:29 +0000 | [diff] [blame] | 237 | // important side effect of calling getOrCreateSymbolData here is to register |
| 238 | // the symbol with the assembler. |
Daniel Dunbar | 8dc68ab | 2010-06-16 20:04:22 +0000 | [diff] [blame^] | 239 | MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol); |
Daniel Dunbar | 6aff2fb | 2009-08-24 08:40:12 +0000 | [diff] [blame] | 240 | |
| 241 | // The implementation of symbol attributes is designed to match 'as', but it |
| 242 | // leaves much to desired. It doesn't really make sense to arbitrarily add and |
| 243 | // remove flags, but 'as' allows this (in particular, see .desc). |
| 244 | // |
| 245 | // In the future it might be worth trying to make these operations more well |
| 246 | // defined. |
Daniel Dunbar | 3edd9bb | 2009-08-22 11:41:10 +0000 | [diff] [blame] | 247 | switch (Attribute) { |
Chris Lattner | a5ad93a | 2010-01-23 06:39:22 +0000 | [diff] [blame] | 248 | case MCSA_Invalid: |
Chris Lattner | ed0ab15 | 2010-01-25 18:30:45 +0000 | [diff] [blame] | 249 | case MCSA_ELF_TypeFunction: |
| 250 | case MCSA_ELF_TypeIndFunction: |
| 251 | case MCSA_ELF_TypeObject: |
| 252 | case MCSA_ELF_TypeTLS: |
| 253 | case MCSA_ELF_TypeCommon: |
| 254 | case MCSA_ELF_TypeNoType: |
Chris Lattner | a5ad93a | 2010-01-23 06:39:22 +0000 | [diff] [blame] | 255 | case MCSA_IndirectSymbol: |
| 256 | case MCSA_Hidden: |
| 257 | case MCSA_Internal: |
| 258 | case MCSA_Protected: |
| 259 | case MCSA_Weak: |
| 260 | case MCSA_Local: |
Daniel Dunbar | 6aff2fb | 2009-08-24 08:40:12 +0000 | [diff] [blame] | 261 | assert(0 && "Invalid symbol attribute for Mach-O!"); |
| 262 | break; |
Daniel Dunbar | 3edd9bb | 2009-08-22 11:41:10 +0000 | [diff] [blame] | 263 | |
Chris Lattner | a5ad93a | 2010-01-23 06:39:22 +0000 | [diff] [blame] | 264 | case MCSA_Global: |
Daniel Dunbar | 8f4d146 | 2009-08-28 07:08:35 +0000 | [diff] [blame] | 265 | SD.setExternal(true); |
Daniel Dunbar | b18d2dd | 2010-05-17 20:12:31 +0000 | [diff] [blame] | 266 | // This effectively clears the undefined lazy bit, in Darwin 'as', although |
| 267 | // it isn't very consistent because it implements this as part of symbol |
| 268 | // lookup. |
| 269 | // |
| 270 | // FIXME: Cleanup this code, these bits should be emitted based on semantic |
| 271 | // properties, not on the order of definition, etc. |
| 272 | SD.setFlags(SD.getFlags() & ~SF_ReferenceTypeUndefinedLazy); |
Daniel Dunbar | 3edd9bb | 2009-08-22 11:41:10 +0000 | [diff] [blame] | 273 | break; |
Daniel Dunbar | 6aff2fb | 2009-08-24 08:40:12 +0000 | [diff] [blame] | 274 | |
Chris Lattner | a5ad93a | 2010-01-23 06:39:22 +0000 | [diff] [blame] | 275 | case MCSA_LazyReference: |
Daniel Dunbar | 6aff2fb | 2009-08-24 08:40:12 +0000 | [diff] [blame] | 276 | // FIXME: This requires -dynamic. |
| 277 | SD.setFlags(SD.getFlags() | SF_NoDeadStrip); |
| 278 | if (Symbol->isUndefined()) |
| 279 | SD.setFlags(SD.getFlags() | SF_ReferenceTypeUndefinedLazy); |
| 280 | break; |
| 281 | |
Daniel Dunbar | 6aff2fb | 2009-08-24 08:40:12 +0000 | [diff] [blame] | 282 | // Since .reference sets the no dead strip bit, it is equivalent to |
| 283 | // .no_dead_strip in practice. |
Chris Lattner | a5ad93a | 2010-01-23 06:39:22 +0000 | [diff] [blame] | 284 | case MCSA_Reference: |
| 285 | case MCSA_NoDeadStrip: |
Daniel Dunbar | 6aff2fb | 2009-08-24 08:40:12 +0000 | [diff] [blame] | 286 | SD.setFlags(SD.getFlags() | SF_NoDeadStrip); |
| 287 | break; |
| 288 | |
Chris Lattner | a5ad93a | 2010-01-23 06:39:22 +0000 | [diff] [blame] | 289 | case MCSA_PrivateExtern: |
Daniel Dunbar | 6aff2fb | 2009-08-24 08:40:12 +0000 | [diff] [blame] | 290 | SD.setExternal(true); |
| 291 | SD.setPrivateExtern(true); |
| 292 | break; |
| 293 | |
Chris Lattner | a5ad93a | 2010-01-23 06:39:22 +0000 | [diff] [blame] | 294 | case MCSA_WeakReference: |
Daniel Dunbar | 6aff2fb | 2009-08-24 08:40:12 +0000 | [diff] [blame] | 295 | // FIXME: This requires -dynamic. |
| 296 | if (Symbol->isUndefined()) |
| 297 | SD.setFlags(SD.getFlags() | SF_WeakReference); |
| 298 | break; |
| 299 | |
Chris Lattner | a5ad93a | 2010-01-23 06:39:22 +0000 | [diff] [blame] | 300 | case MCSA_WeakDefinition: |
Daniel Dunbar | 6aff2fb | 2009-08-24 08:40:12 +0000 | [diff] [blame] | 301 | // FIXME: 'as' enforces that this is defined and global. The manual claims |
| 302 | // it has to be in a coalesced section, but this isn't enforced. |
| 303 | SD.setFlags(SD.getFlags() | SF_WeakDefinition); |
| 304 | break; |
Daniel Dunbar | 3edd9bb | 2009-08-22 11:41:10 +0000 | [diff] [blame] | 305 | } |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 306 | } |
| 307 | |
| 308 | void MCMachOStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) { |
Daniel Dunbar | 6aff2fb | 2009-08-24 08:40:12 +0000 | [diff] [blame] | 309 | // Encode the 'desc' value into the lowest implementation defined bits. |
Daniel Dunbar | 2ac0c45 | 2010-05-26 20:37:00 +0000 | [diff] [blame] | 310 | assert(DescValue == (DescValue & SF_DescFlagsMask) && |
Daniel Dunbar | 6aff2fb | 2009-08-24 08:40:12 +0000 | [diff] [blame] | 311 | "Invalid .desc value!"); |
Daniel Dunbar | 8dc68ab | 2010-06-16 20:04:22 +0000 | [diff] [blame^] | 312 | getAssembler().getOrCreateSymbolData(*Symbol).setFlags( |
| 313 | DescValue & SF_DescFlagsMask); |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 314 | } |
| 315 | |
Chris Lattner | 9eb158d | 2010-01-23 07:47:02 +0000 | [diff] [blame] | 316 | void MCMachOStreamer::EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size, |
Daniel Dunbar | 7092c7e | 2009-08-30 06:17:16 +0000 | [diff] [blame] | 317 | unsigned ByteAlignment) { |
Daniel Dunbar | 8f4d146 | 2009-08-28 07:08:35 +0000 | [diff] [blame] | 318 | // FIXME: Darwin 'as' does appear to allow redef of a .comm by itself. |
| 319 | assert(Symbol->isUndefined() && "Cannot define a symbol twice!"); |
| 320 | |
Daniel Dunbar | 8dc68ab | 2010-06-16 20:04:22 +0000 | [diff] [blame^] | 321 | MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol); |
Daniel Dunbar | 8f4d146 | 2009-08-28 07:08:35 +0000 | [diff] [blame] | 322 | SD.setExternal(true); |
Daniel Dunbar | 7092c7e | 2009-08-30 06:17:16 +0000 | [diff] [blame] | 323 | SD.setCommon(Size, ByteAlignment); |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 324 | } |
| 325 | |
Daniel Dunbar | 8751b94 | 2009-08-28 05:48:22 +0000 | [diff] [blame] | 326 | void MCMachOStreamer::EmitZerofill(const MCSection *Section, MCSymbol *Symbol, |
Daniel Dunbar | 7092c7e | 2009-08-30 06:17:16 +0000 | [diff] [blame] | 327 | unsigned Size, unsigned ByteAlignment) { |
Daniel Dunbar | 8dc68ab | 2010-06-16 20:04:22 +0000 | [diff] [blame^] | 328 | MCSectionData &SectData = getAssembler().getOrCreateSectionData(*Section); |
Daniel Dunbar | d5a8e98 | 2009-08-28 05:49:21 +0000 | [diff] [blame] | 329 | |
| 330 | // The symbol may not be present, which only creates the section. |
| 331 | if (!Symbol) |
| 332 | return; |
| 333 | |
| 334 | // FIXME: Assert that this section has the zerofill type. |
| 335 | |
| 336 | assert(Symbol->isUndefined() && "Cannot define a symbol twice!"); |
| 337 | |
Daniel Dunbar | 8dc68ab | 2010-06-16 20:04:22 +0000 | [diff] [blame^] | 338 | MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol); |
Daniel Dunbar | d5a8e98 | 2009-08-28 05:49:21 +0000 | [diff] [blame] | 339 | |
Daniel Dunbar | e73d49e | 2010-05-12 22:51:27 +0000 | [diff] [blame] | 340 | // Emit an align fragment if necessary. |
| 341 | if (ByteAlignment != 1) |
Daniel Dunbar | 1c15413 | 2010-05-12 22:56:23 +0000 | [diff] [blame] | 342 | new MCAlignFragment(ByteAlignment, 0, 0, ByteAlignment, &SectData); |
Daniel Dunbar | e73d49e | 2010-05-12 22:51:27 +0000 | [diff] [blame] | 343 | |
Daniel Dunbar | 4e54487 | 2010-05-12 22:51:38 +0000 | [diff] [blame] | 344 | MCFragment *F = new MCFillFragment(0, 0, Size, &SectData); |
Daniel Dunbar | d5a8e98 | 2009-08-28 05:49:21 +0000 | [diff] [blame] | 345 | SD.setFragment(F); |
Daniel Dunbar | 8dc68ab | 2010-06-16 20:04:22 +0000 | [diff] [blame^] | 346 | if (getAssembler().isSymbolLinkerVisible(&SD)) |
Daniel Dunbar | 071f73d | 2010-05-10 22:45:09 +0000 | [diff] [blame] | 347 | F->setAtom(&SD); |
Daniel Dunbar | d5a8e98 | 2009-08-28 05:49:21 +0000 | [diff] [blame] | 348 | |
| 349 | Symbol->setSection(*Section); |
| 350 | |
| 351 | // Update the maximum alignment on the zero fill section if necessary. |
| 352 | if (ByteAlignment > SectData.getAlignment()) |
| 353 | SectData.setAlignment(ByteAlignment); |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 354 | } |
| 355 | |
Eric Christopher | 070c1ab | 2010-05-21 23:03:53 +0000 | [diff] [blame] | 356 | // This should always be called with the thread local bss section. Like the |
| 357 | // .zerofill directive this doesn't actually switch sections on us. |
Eric Christopher | 011c3f1 | 2010-05-18 21:26:41 +0000 | [diff] [blame] | 358 | void MCMachOStreamer::EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol, |
| 359 | uint64_t Size, unsigned ByteAlignment) { |
| 360 | EmitZerofill(Section, Symbol, Size, ByteAlignment); |
| 361 | return; |
Eric Christopher | 482eba0 | 2010-05-14 01:50:28 +0000 | [diff] [blame] | 362 | } |
| 363 | |
Chris Lattner | aaec205 | 2010-01-19 19:46:13 +0000 | [diff] [blame] | 364 | void MCMachOStreamer::EmitBytes(StringRef Data, unsigned AddrSpace) { |
Daniel Dunbar | f70f477 | 2010-03-22 20:35:46 +0000 | [diff] [blame] | 365 | getOrCreateDataFragment()->getContents().append(Data.begin(), Data.end()); |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 366 | } |
| 367 | |
Chris Lattner | aaec205 | 2010-01-19 19:46:13 +0000 | [diff] [blame] | 368 | void MCMachOStreamer::EmitValue(const MCExpr *Value, unsigned Size, |
| 369 | unsigned AddrSpace) { |
Daniel Dunbar | f70f477 | 2010-03-22 20:35:46 +0000 | [diff] [blame] | 370 | MCDataFragment *DF = getOrCreateDataFragment(); |
Daniel Dunbar | 45f4874 | 2010-02-13 09:28:22 +0000 | [diff] [blame] | 371 | |
| 372 | // Avoid fixups when possible. |
| 373 | int64_t AbsValue; |
Daniel Dunbar | 40ebe247 | 2010-02-22 22:08:57 +0000 | [diff] [blame] | 374 | if (AddValueSymbols(Value)->EvaluateAsAbsolute(AbsValue)) { |
Daniel Dunbar | 45f4874 | 2010-02-13 09:28:22 +0000 | [diff] [blame] | 375 | // FIXME: Endianness assumption. |
| 376 | for (unsigned i = 0; i != Size; ++i) |
| 377 | DF->getContents().push_back(uint8_t(AbsValue >> (i * 8))); |
| 378 | } else { |
Daniel Dunbar | c90e30a | 2010-05-26 15:18:56 +0000 | [diff] [blame] | 379 | DF->addFixup(MCFixup::Create(DF->getContents().size(), |
| 380 | AddValueSymbols(Value), |
| 381 | MCFixup::getKindForSize(Size))); |
Daniel Dunbar | 45f4874 | 2010-02-13 09:28:22 +0000 | [diff] [blame] | 382 | DF->getContents().resize(DF->getContents().size() + Size, 0); |
| 383 | } |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 384 | } |
| 385 | |
| 386 | void MCMachOStreamer::EmitValueToAlignment(unsigned ByteAlignment, |
| 387 | int64_t Value, unsigned ValueSize, |
| 388 | unsigned MaxBytesToEmit) { |
Daniel Dunbar | d6f761e | 2009-08-21 23:07:38 +0000 | [diff] [blame] | 389 | if (MaxBytesToEmit == 0) |
| 390 | MaxBytesToEmit = ByteAlignment; |
Daniel Dunbar | 071f73d | 2010-05-10 22:45:09 +0000 | [diff] [blame] | 391 | MCFragment *F = new MCAlignFragment(ByteAlignment, Value, ValueSize, |
Daniel Dunbar | 1c15413 | 2010-05-12 22:56:23 +0000 | [diff] [blame] | 392 | MaxBytesToEmit, CurSectionData); |
Daniel Dunbar | 071f73d | 2010-05-10 22:45:09 +0000 | [diff] [blame] | 393 | F->setAtom(CurrentAtomMap.lookup(CurSectionData)); |
Kevin Enderby | 6e72048 | 2010-02-23 18:26:34 +0000 | [diff] [blame] | 394 | |
| 395 | // Update the maximum alignment on the current section if necessary. |
| 396 | if (ByteAlignment > CurSectionData->getAlignment()) |
| 397 | CurSectionData->setAlignment(ByteAlignment); |
| 398 | } |
| 399 | |
| 400 | void MCMachOStreamer::EmitCodeAlignment(unsigned ByteAlignment, |
| 401 | unsigned MaxBytesToEmit) { |
| 402 | if (MaxBytesToEmit == 0) |
| 403 | MaxBytesToEmit = ByteAlignment; |
Daniel Dunbar | 1c15413 | 2010-05-12 22:56:23 +0000 | [diff] [blame] | 404 | MCAlignFragment *F = new MCAlignFragment(ByteAlignment, 0, 1, MaxBytesToEmit, |
| 405 | CurSectionData); |
| 406 | F->setEmitNops(true); |
Daniel Dunbar | 071f73d | 2010-05-10 22:45:09 +0000 | [diff] [blame] | 407 | F->setAtom(CurrentAtomMap.lookup(CurSectionData)); |
Daniel Dunbar | 0705fbf | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 408 | |
Daniel Dunbar | f3d2ef0 | 2009-08-22 10:13:24 +0000 | [diff] [blame] | 409 | // Update the maximum alignment on the current section if necessary. |
Daniel Dunbar | 0705fbf | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 410 | if (ByteAlignment > CurSectionData->getAlignment()) |
| 411 | CurSectionData->setAlignment(ByteAlignment); |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 412 | } |
| 413 | |
Daniel Dunbar | 821e333 | 2009-08-31 08:09:28 +0000 | [diff] [blame] | 414 | void MCMachOStreamer::EmitValueToOffset(const MCExpr *Offset, |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 415 | unsigned char Value) { |
Daniel Dunbar | 071f73d | 2010-05-10 22:45:09 +0000 | [diff] [blame] | 416 | MCFragment *F = new MCOrgFragment(*Offset, Value, CurSectionData); |
| 417 | F->setAtom(CurrentAtomMap.lookup(CurSectionData)); |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 418 | } |
| 419 | |
Daniel Dunbar | 2ac0c45 | 2010-05-26 20:37:00 +0000 | [diff] [blame] | 420 | void MCMachOStreamer::EmitInstToFragment(const MCInst &Inst) { |
| 421 | MCInstFragment *IF = new MCInstFragment(Inst, CurSectionData); |
| 422 | IF->setAtom(CurrentAtomMap.lookup(CurSectionData)); |
| 423 | |
| 424 | // Add the fixups and data. |
| 425 | // |
| 426 | // FIXME: Revisit this design decision when relaxation is done, we may be |
| 427 | // able to get away with not storing any extra data in the MCInst. |
| 428 | SmallVector<MCFixup, 4> Fixups; |
| 429 | SmallString<256> Code; |
| 430 | raw_svector_ostream VecOS(Code); |
Daniel Dunbar | 8dc68ab | 2010-06-16 20:04:22 +0000 | [diff] [blame^] | 431 | getAssembler().getEmitter().EncodeInstruction(Inst, VecOS, Fixups); |
Daniel Dunbar | 2ac0c45 | 2010-05-26 20:37:00 +0000 | [diff] [blame] | 432 | VecOS.flush(); |
| 433 | |
| 434 | IF->getCode() = Code; |
| 435 | IF->getFixups() = Fixups; |
| 436 | } |
| 437 | |
| 438 | void MCMachOStreamer::EmitInstToData(const MCInst &Inst) { |
| 439 | MCDataFragment *DF = getOrCreateDataFragment(); |
| 440 | |
| 441 | SmallVector<MCFixup, 4> Fixups; |
| 442 | SmallString<256> Code; |
| 443 | raw_svector_ostream VecOS(Code); |
Daniel Dunbar | 8dc68ab | 2010-06-16 20:04:22 +0000 | [diff] [blame^] | 444 | getAssembler().getEmitter().EncodeInstruction(Inst, VecOS, Fixups); |
Daniel Dunbar | 2ac0c45 | 2010-05-26 20:37:00 +0000 | [diff] [blame] | 445 | VecOS.flush(); |
| 446 | |
| 447 | // Add the fixups and data. |
| 448 | for (unsigned i = 0, e = Fixups.size(); i != e; ++i) { |
| 449 | Fixups[i].setOffset(Fixups[i].getOffset() + DF->getContents().size()); |
| 450 | DF->addFixup(Fixups[i]); |
| 451 | } |
| 452 | DF->getContents().append(Code.begin(), Code.end()); |
| 453 | } |
| 454 | |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 455 | void MCMachOStreamer::EmitInstruction(const MCInst &Inst) { |
Daniel Dunbar | 4fac749 | 2009-08-27 08:17:51 +0000 | [diff] [blame] | 456 | // Scan for values. |
Daniel Dunbar | db9014d | 2010-05-17 21:19:59 +0000 | [diff] [blame] | 457 | for (unsigned i = Inst.getNumOperands(); i--; ) |
Daniel Dunbar | 8c2eebe | 2009-08-31 08:08:38 +0000 | [diff] [blame] | 458 | if (Inst.getOperand(i).isExpr()) |
| 459 | AddValueSymbols(Inst.getOperand(i).getExpr()); |
Daniel Dunbar | 4fac749 | 2009-08-27 08:17:51 +0000 | [diff] [blame] | 460 | |
Daniel Dunbar | e1ec617 | 2010-02-02 21:44:01 +0000 | [diff] [blame] | 461 | CurSectionData->setHasInstructions(true); |
| 462 | |
Daniel Dunbar | 83194de | 2010-05-26 20:37:03 +0000 | [diff] [blame] | 463 | // If this instruction doesn't need relaxation, just emit it as data. |
Daniel Dunbar | 8dc68ab | 2010-06-16 20:04:22 +0000 | [diff] [blame^] | 464 | if (!getAssembler().getBackend().MayNeedRelaxation(Inst)) { |
Daniel Dunbar | 2ac0c45 | 2010-05-26 20:37:00 +0000 | [diff] [blame] | 465 | EmitInstToData(Inst); |
Daniel Dunbar | 83194de | 2010-05-26 20:37:03 +0000 | [diff] [blame] | 466 | return; |
| 467 | } |
| 468 | |
| 469 | // Otherwise, if we are relaxing everything, relax the instruction as much as |
| 470 | // possible and emit it as data. |
Daniel Dunbar | 8dc68ab | 2010-06-16 20:04:22 +0000 | [diff] [blame^] | 471 | if (getAssembler().getRelaxAll()) { |
Daniel Dunbar | 83194de | 2010-05-26 20:37:03 +0000 | [diff] [blame] | 472 | MCInst Relaxed; |
Daniel Dunbar | 8dc68ab | 2010-06-16 20:04:22 +0000 | [diff] [blame^] | 473 | getAssembler().getBackend().RelaxInstruction(Inst, Relaxed); |
| 474 | while (getAssembler().getBackend().MayNeedRelaxation(Relaxed)) |
| 475 | getAssembler().getBackend().RelaxInstruction(Relaxed, Relaxed); |
Daniel Dunbar | 83194de | 2010-05-26 20:37:03 +0000 | [diff] [blame] | 476 | EmitInstToData(Relaxed); |
| 477 | return; |
| 478 | } |
| 479 | |
| 480 | // Otherwise emit to a separate fragment. |
| 481 | EmitInstToFragment(Inst); |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 482 | } |
| 483 | |
| 484 | void MCMachOStreamer::Finish() { |
Daniel Dunbar | 8dc68ab | 2010-06-16 20:04:22 +0000 | [diff] [blame^] | 485 | getAssembler().Finish(); |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 486 | } |
| 487 | |
Daniel Dunbar | 1f3e445 | 2010-03-11 01:34:27 +0000 | [diff] [blame] | 488 | MCStreamer *llvm::createMachOStreamer(MCContext &Context, TargetAsmBackend &TAB, |
Daniel Dunbar | ac2884a | 2010-03-25 22:49:09 +0000 | [diff] [blame] | 489 | raw_ostream &OS, MCCodeEmitter *CE, |
| 490 | bool RelaxAll) { |
| 491 | MCMachOStreamer *S = new MCMachOStreamer(Context, TAB, OS, CE); |
| 492 | if (RelaxAll) |
| 493 | S->getAssembler().setRelaxAll(true); |
| 494 | return S; |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 495 | } |