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 | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 17 | #include "llvm/MC/MCSection.h" |
| 18 | #include "llvm/MC/MCSymbol.h" |
Daniel Dunbar | 821e333 | 2009-08-31 08:09:28 +0000 | [diff] [blame] | 19 | #include "llvm/MC/MCValue.h" |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 20 | #include "llvm/Support/ErrorHandling.h" |
Daniel Dunbar | 4fac749 | 2009-08-27 08:17:51 +0000 | [diff] [blame] | 21 | #include "llvm/Support/raw_ostream.h" |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 22 | using namespace llvm; |
| 23 | |
| 24 | namespace { |
| 25 | |
| 26 | class MCMachOStreamer : public MCStreamer { |
Daniel Dunbar | 6aff2fb | 2009-08-24 08:40:12 +0000 | [diff] [blame] | 27 | /// SymbolFlags - We store the value for the 'desc' symbol field in the lowest |
| 28 | /// 16 bits of the implementation defined flags. |
| 29 | enum SymbolFlags { // See <mach-o/nlist.h>. |
| 30 | SF_DescFlagsMask = 0xFFFF, |
| 31 | |
| 32 | // Reference type flags. |
| 33 | SF_ReferenceTypeMask = 0x0007, |
| 34 | SF_ReferenceTypeUndefinedNonLazy = 0x0000, |
| 35 | SF_ReferenceTypeUndefinedLazy = 0x0001, |
| 36 | SF_ReferenceTypeDefined = 0x0002, |
| 37 | SF_ReferenceTypePrivateDefined = 0x0003, |
| 38 | SF_ReferenceTypePrivateUndefinedNonLazy = 0x0004, |
| 39 | SF_ReferenceTypePrivateUndefinedLazy = 0x0005, |
| 40 | |
| 41 | // Other 'desc' flags. |
| 42 | SF_NoDeadStrip = 0x0020, |
| 43 | SF_WeakReference = 0x0040, |
| 44 | SF_WeakDefinition = 0x0080 |
| 45 | }; |
| 46 | |
| 47 | private: |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 48 | MCAssembler Assembler; |
Daniel Dunbar | 4fac749 | 2009-08-27 08:17:51 +0000 | [diff] [blame] | 49 | MCCodeEmitter *Emitter; |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 50 | MCSectionData *CurSectionData; |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 51 | DenseMap<const MCSection*, MCSectionData*> SectionMap; |
Daniel Dunbar | f3d2ef0 | 2009-08-22 10:13:24 +0000 | [diff] [blame] | 52 | DenseMap<const MCSymbol*, MCSymbolData*> SymbolMap; |
| 53 | |
| 54 | private: |
| 55 | MCFragment *getCurrentFragment() const { |
| 56 | assert(CurSectionData && "No current section!"); |
| 57 | |
| 58 | if (!CurSectionData->empty()) |
| 59 | return &CurSectionData->getFragmentList().back(); |
| 60 | |
| 61 | return 0; |
| 62 | } |
| 63 | |
Daniel Dunbar | 0f5fa69 | 2009-08-28 05:48:54 +0000 | [diff] [blame] | 64 | MCSectionData &getSectionData(const MCSection &Section) { |
| 65 | MCSectionData *&Entry = SectionMap[&Section]; |
| 66 | |
| 67 | if (!Entry) |
| 68 | Entry = new MCSectionData(Section, &Assembler); |
| 69 | |
| 70 | return *Entry; |
| 71 | } |
| 72 | |
Daniel Dunbar | cb579b3 | 2009-08-31 08:08:06 +0000 | [diff] [blame] | 73 | MCSymbolData &getSymbolData(const MCSymbol &Symbol) { |
Daniel Dunbar | f3d2ef0 | 2009-08-22 10:13:24 +0000 | [diff] [blame] | 74 | MCSymbolData *&Entry = SymbolMap[&Symbol]; |
| 75 | |
| 76 | if (!Entry) |
| 77 | Entry = new MCSymbolData(Symbol, 0, 0, &Assembler); |
| 78 | |
| 79 | return *Entry; |
| 80 | } |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 81 | |
| 82 | public: |
Daniel Dunbar | 4fac749 | 2009-08-27 08:17:51 +0000 | [diff] [blame] | 83 | MCMachOStreamer(MCContext &Context, raw_ostream &_OS, MCCodeEmitter *_Emitter) |
Daniel Dunbar | a03a368 | 2009-08-31 08:07:55 +0000 | [diff] [blame] | 84 | : MCStreamer(Context), Assembler(Context, _OS), Emitter(_Emitter), |
Daniel Dunbar | 4fac749 | 2009-08-27 08:17:51 +0000 | [diff] [blame] | 85 | CurSectionData(0) {} |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 86 | ~MCMachOStreamer() {} |
| 87 | |
Daniel Dunbar | 8c2eebe | 2009-08-31 08:08:38 +0000 | [diff] [blame] | 88 | const MCExpr *AddValueSymbols(const MCExpr *Value) { |
| 89 | switch (Value->getKind()) { |
Chris Lattner | 5d917a8 | 2010-02-08 19:41:07 +0000 | [diff] [blame] | 90 | case MCExpr::Target: assert(0 && "Can't handle target exprs yet!"); |
Daniel Dunbar | 8c2eebe | 2009-08-31 08:08:38 +0000 | [diff] [blame] | 91 | case MCExpr::Constant: |
| 92 | break; |
| 93 | |
| 94 | case MCExpr::Binary: { |
| 95 | const MCBinaryExpr *BE = cast<MCBinaryExpr>(Value); |
| 96 | AddValueSymbols(BE->getLHS()); |
| 97 | AddValueSymbols(BE->getRHS()); |
| 98 | break; |
| 99 | } |
| 100 | |
| 101 | case MCExpr::SymbolRef: |
| 102 | getSymbolData(cast<MCSymbolRefExpr>(Value)->getSymbol()); |
| 103 | break; |
| 104 | |
| 105 | case MCExpr::Unary: |
| 106 | AddValueSymbols(cast<MCUnaryExpr>(Value)->getSubExpr()); |
| 107 | break; |
| 108 | } |
| 109 | |
| 110 | return Value; |
| 111 | } |
| 112 | |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 113 | /// @name MCStreamer Interface |
| 114 | /// @{ |
| 115 | |
| 116 | virtual void SwitchSection(const MCSection *Section); |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 117 | virtual void EmitLabel(MCSymbol *Symbol); |
Chris Lattner | a5ad93a | 2010-01-23 06:39:22 +0000 | [diff] [blame] | 118 | virtual void EmitAssemblerFlag(MCAssemblerFlag Flag); |
Daniel Dunbar | 821e333 | 2009-08-31 08:09:28 +0000 | [diff] [blame] | 119 | virtual void EmitAssignment(MCSymbol *Symbol, const MCExpr *Value); |
Chris Lattner | a5ad93a | 2010-01-23 06:39:22 +0000 | [diff] [blame] | 120 | virtual void EmitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute); |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 121 | virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue); |
Chris Lattner | 9eb158d | 2010-01-23 07:47:02 +0000 | [diff] [blame] | 122 | virtual void EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size, |
Daniel Dunbar | 7092c7e | 2009-08-30 06:17:16 +0000 | [diff] [blame] | 123 | unsigned ByteAlignment); |
Chris Lattner | 99328ad | 2010-01-25 07:52:13 +0000 | [diff] [blame] | 124 | virtual void EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) { |
| 125 | assert(0 && "macho doesn't support this directive"); |
| 126 | } |
Chris Lattner | 9eb158d | 2010-01-23 07:47:02 +0000 | [diff] [blame] | 127 | virtual void EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size) { |
| 128 | assert(0 && "macho doesn't support this directive"); |
| 129 | } |
Daniel Dunbar | 8751b94 | 2009-08-28 05:48:22 +0000 | [diff] [blame] | 130 | virtual void EmitZerofill(const MCSection *Section, MCSymbol *Symbol = 0, |
Daniel Dunbar | 7092c7e | 2009-08-30 06:17:16 +0000 | [diff] [blame] | 131 | unsigned Size = 0, unsigned ByteAlignment = 0); |
Chris Lattner | aaec205 | 2010-01-19 19:46:13 +0000 | [diff] [blame] | 132 | virtual void EmitBytes(StringRef Data, unsigned AddrSpace); |
Chris Lattner | aaec205 | 2010-01-19 19:46:13 +0000 | [diff] [blame] | 133 | virtual void EmitValue(const MCExpr *Value, unsigned Size,unsigned AddrSpace); |
Chris Lattner | 718fb59 | 2010-01-25 21:28:50 +0000 | [diff] [blame] | 134 | virtual void EmitGPRel32Value(const MCExpr *Value) { |
| 135 | assert(0 && "macho doesn't support this directive"); |
| 136 | } |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 137 | virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0, |
| 138 | unsigned ValueSize = 1, |
| 139 | unsigned MaxBytesToEmit = 0); |
Kevin Enderby | 6e72048 | 2010-02-23 18:26:34 +0000 | [diff] [blame^] | 140 | virtual void EmitCodeAlignment(unsigned ByteAlignment, |
| 141 | unsigned MaxBytesToEmit = 0); |
Daniel Dunbar | 821e333 | 2009-08-31 08:09:28 +0000 | [diff] [blame] | 142 | virtual void EmitValueToOffset(const MCExpr *Offset, |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 143 | unsigned char Value = 0); |
Chris Lattner | a6594fc | 2010-01-25 18:58:59 +0000 | [diff] [blame] | 144 | |
| 145 | virtual void EmitFileDirective(StringRef Filename) { |
| 146 | errs() << "FIXME: MCMachoStreamer:EmitFileDirective not implemented\n"; |
| 147 | } |
| 148 | virtual void EmitDwarfFileDirective(unsigned FileNo, StringRef Filename) { |
| 149 | errs() << "FIXME: MCMachoStreamer:EmitDwarfFileDirective not implemented\n"; |
| 150 | } |
| 151 | |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 152 | virtual void EmitInstruction(const MCInst &Inst); |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 153 | virtual void Finish(); |
| 154 | |
| 155 | /// @} |
| 156 | }; |
| 157 | |
| 158 | } // end anonymous namespace. |
| 159 | |
| 160 | void MCMachOStreamer::SwitchSection(const MCSection *Section) { |
| 161 | assert(Section && "Cannot switch to a null section!"); |
Chris Lattner | cda7f78 | 2009-08-22 19:35:08 +0000 | [diff] [blame] | 162 | |
| 163 | // If already in this section, then this is a noop. |
| 164 | if (Section == CurSection) return; |
Daniel Dunbar | 0f5fa69 | 2009-08-28 05:48:54 +0000 | [diff] [blame] | 165 | |
Chris Lattner | cda7f78 | 2009-08-22 19:35:08 +0000 | [diff] [blame] | 166 | CurSection = Section; |
Daniel Dunbar | 0f5fa69 | 2009-08-28 05:48:54 +0000 | [diff] [blame] | 167 | CurSectionData = &getSectionData(*Section); |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | void MCMachOStreamer::EmitLabel(MCSymbol *Symbol) { |
Daniel Dunbar | 8906ff1 | 2009-08-22 07:22:36 +0000 | [diff] [blame] | 171 | assert(Symbol->isUndefined() && "Cannot define a symbol twice!"); |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 172 | |
Daniel Dunbar | 5e83596 | 2009-08-26 02:48:04 +0000 | [diff] [blame] | 173 | // FIXME: We should also use offsets into Fill fragments. |
Daniel Dunbar | f3d2ef0 | 2009-08-22 10:13:24 +0000 | [diff] [blame] | 174 | MCDataFragment *F = dyn_cast_or_null<MCDataFragment>(getCurrentFragment()); |
| 175 | if (!F) |
| 176 | F = new MCDataFragment(CurSectionData); |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 177 | |
Daniel Dunbar | f3d2ef0 | 2009-08-22 10:13:24 +0000 | [diff] [blame] | 178 | MCSymbolData &SD = getSymbolData(*Symbol); |
| 179 | assert(!SD.getFragment() && "Unexpected fragment on symbol data!"); |
| 180 | SD.setFragment(F); |
| 181 | SD.setOffset(F->getContents().size()); |
Daniel Dunbar | 6aff2fb | 2009-08-24 08:40:12 +0000 | [diff] [blame] | 182 | |
| 183 | // This causes the reference type and weak reference flags to be cleared. |
| 184 | SD.setFlags(SD.getFlags() & ~(SF_WeakReference | SF_ReferenceTypeMask)); |
Daniel Dunbar | f3d2ef0 | 2009-08-22 10:13:24 +0000 | [diff] [blame] | 185 | |
Daniel Dunbar | 8906ff1 | 2009-08-22 07:22:36 +0000 | [diff] [blame] | 186 | Symbol->setSection(*CurSection); |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 187 | } |
| 188 | |
Chris Lattner | a5ad93a | 2010-01-23 06:39:22 +0000 | [diff] [blame] | 189 | void MCMachOStreamer::EmitAssemblerFlag(MCAssemblerFlag Flag) { |
Daniel Dunbar | 6009db4 | 2009-08-26 21:22:22 +0000 | [diff] [blame] | 190 | switch (Flag) { |
Chris Lattner | a5ad93a | 2010-01-23 06:39:22 +0000 | [diff] [blame] | 191 | case MCAF_SubsectionsViaSymbols: |
Daniel Dunbar | 6009db4 | 2009-08-26 21:22:22 +0000 | [diff] [blame] | 192 | Assembler.setSubsectionsViaSymbols(true); |
Daniel Dunbar | 8c3eaf4 | 2009-08-28 07:08:47 +0000 | [diff] [blame] | 193 | return; |
Daniel Dunbar | 6009db4 | 2009-08-26 21:22:22 +0000 | [diff] [blame] | 194 | } |
Daniel Dunbar | 8c3eaf4 | 2009-08-28 07:08:47 +0000 | [diff] [blame] | 195 | |
| 196 | assert(0 && "invalid assembler flag!"); |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 197 | } |
| 198 | |
Daniel Dunbar | 821e333 | 2009-08-31 08:09:28 +0000 | [diff] [blame] | 199 | void MCMachOStreamer::EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) { |
Daniel Dunbar | 8906ff1 | 2009-08-22 07:22:36 +0000 | [diff] [blame] | 200 | // Only absolute symbols can be redefined. |
| 201 | assert((Symbol->isUndefined() || Symbol->isAbsolute()) && |
| 202 | "Cannot define a symbol twice!"); |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 203 | |
Daniel Dunbar | a4d8667 | 2009-10-16 01:58:23 +0000 | [diff] [blame] | 204 | // FIXME: Lift context changes into super class. |
| 205 | // FIXME: Set associated section. |
| 206 | Symbol->setValue(Value); |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | void MCMachOStreamer::EmitSymbolAttribute(MCSymbol *Symbol, |
Chris Lattner | a5ad93a | 2010-01-23 06:39:22 +0000 | [diff] [blame] | 210 | MCSymbolAttr Attribute) { |
Daniel Dunbar | 0c7761b | 2009-08-24 11:56:58 +0000 | [diff] [blame] | 211 | // Indirect symbols are handled differently, to match how 'as' handles |
| 212 | // them. This makes writing matching .o files easier. |
Chris Lattner | a5ad93a | 2010-01-23 06:39:22 +0000 | [diff] [blame] | 213 | if (Attribute == MCSA_IndirectSymbol) { |
Daniel Dunbar | ad7c3d5 | 2009-08-26 00:18:21 +0000 | [diff] [blame] | 214 | // Note that we intentionally cannot use the symbol data here; this is |
| 215 | // important for matching the string table that 'as' generates. |
Daniel Dunbar | 0c7761b | 2009-08-24 11:56:58 +0000 | [diff] [blame] | 216 | IndirectSymbolData ISD; |
| 217 | ISD.Symbol = Symbol; |
| 218 | ISD.SectionData = CurSectionData; |
| 219 | Assembler.getIndirectSymbols().push_back(ISD); |
| 220 | return; |
| 221 | } |
| 222 | |
Daniel Dunbar | 6aff2fb | 2009-08-24 08:40:12 +0000 | [diff] [blame] | 223 | // Adding a symbol attribute always introduces the symbol, note that an |
| 224 | // important side effect of calling getSymbolData here is to register the |
| 225 | // symbol with the assembler. |
| 226 | MCSymbolData &SD = getSymbolData(*Symbol); |
| 227 | |
| 228 | // The implementation of symbol attributes is designed to match 'as', but it |
| 229 | // leaves much to desired. It doesn't really make sense to arbitrarily add and |
| 230 | // remove flags, but 'as' allows this (in particular, see .desc). |
| 231 | // |
| 232 | // In the future it might be worth trying to make these operations more well |
| 233 | // defined. |
Daniel Dunbar | 3edd9bb | 2009-08-22 11:41:10 +0000 | [diff] [blame] | 234 | switch (Attribute) { |
Chris Lattner | a5ad93a | 2010-01-23 06:39:22 +0000 | [diff] [blame] | 235 | case MCSA_Invalid: |
Chris Lattner | ed0ab15 | 2010-01-25 18:30:45 +0000 | [diff] [blame] | 236 | case MCSA_ELF_TypeFunction: |
| 237 | case MCSA_ELF_TypeIndFunction: |
| 238 | case MCSA_ELF_TypeObject: |
| 239 | case MCSA_ELF_TypeTLS: |
| 240 | case MCSA_ELF_TypeCommon: |
| 241 | case MCSA_ELF_TypeNoType: |
Chris Lattner | a5ad93a | 2010-01-23 06:39:22 +0000 | [diff] [blame] | 242 | case MCSA_IndirectSymbol: |
| 243 | case MCSA_Hidden: |
| 244 | case MCSA_Internal: |
| 245 | case MCSA_Protected: |
| 246 | case MCSA_Weak: |
| 247 | case MCSA_Local: |
Daniel Dunbar | 6aff2fb | 2009-08-24 08:40:12 +0000 | [diff] [blame] | 248 | assert(0 && "Invalid symbol attribute for Mach-O!"); |
| 249 | break; |
Daniel Dunbar | 3edd9bb | 2009-08-22 11:41:10 +0000 | [diff] [blame] | 250 | |
Chris Lattner | a5ad93a | 2010-01-23 06:39:22 +0000 | [diff] [blame] | 251 | case MCSA_Global: |
Daniel Dunbar | 8f4d146 | 2009-08-28 07:08:35 +0000 | [diff] [blame] | 252 | SD.setExternal(true); |
Daniel Dunbar | 3edd9bb | 2009-08-22 11:41:10 +0000 | [diff] [blame] | 253 | break; |
Daniel Dunbar | 6aff2fb | 2009-08-24 08:40:12 +0000 | [diff] [blame] | 254 | |
Chris Lattner | a5ad93a | 2010-01-23 06:39:22 +0000 | [diff] [blame] | 255 | case MCSA_LazyReference: |
Daniel Dunbar | 6aff2fb | 2009-08-24 08:40:12 +0000 | [diff] [blame] | 256 | // FIXME: This requires -dynamic. |
| 257 | SD.setFlags(SD.getFlags() | SF_NoDeadStrip); |
| 258 | if (Symbol->isUndefined()) |
| 259 | SD.setFlags(SD.getFlags() | SF_ReferenceTypeUndefinedLazy); |
| 260 | break; |
| 261 | |
Daniel Dunbar | 6aff2fb | 2009-08-24 08:40:12 +0000 | [diff] [blame] | 262 | // Since .reference sets the no dead strip bit, it is equivalent to |
| 263 | // .no_dead_strip in practice. |
Chris Lattner | a5ad93a | 2010-01-23 06:39:22 +0000 | [diff] [blame] | 264 | case MCSA_Reference: |
| 265 | case MCSA_NoDeadStrip: |
Daniel Dunbar | 6aff2fb | 2009-08-24 08:40:12 +0000 | [diff] [blame] | 266 | SD.setFlags(SD.getFlags() | SF_NoDeadStrip); |
| 267 | break; |
| 268 | |
Chris Lattner | a5ad93a | 2010-01-23 06:39:22 +0000 | [diff] [blame] | 269 | case MCSA_PrivateExtern: |
Daniel Dunbar | 6aff2fb | 2009-08-24 08:40:12 +0000 | [diff] [blame] | 270 | SD.setExternal(true); |
| 271 | SD.setPrivateExtern(true); |
| 272 | break; |
| 273 | |
Chris Lattner | a5ad93a | 2010-01-23 06:39:22 +0000 | [diff] [blame] | 274 | case MCSA_WeakReference: |
Daniel Dunbar | 6aff2fb | 2009-08-24 08:40:12 +0000 | [diff] [blame] | 275 | // FIXME: This requires -dynamic. |
| 276 | if (Symbol->isUndefined()) |
| 277 | SD.setFlags(SD.getFlags() | SF_WeakReference); |
| 278 | break; |
| 279 | |
Chris Lattner | a5ad93a | 2010-01-23 06:39:22 +0000 | [diff] [blame] | 280 | case MCSA_WeakDefinition: |
Daniel Dunbar | 6aff2fb | 2009-08-24 08:40:12 +0000 | [diff] [blame] | 281 | // FIXME: 'as' enforces that this is defined and global. The manual claims |
| 282 | // it has to be in a coalesced section, but this isn't enforced. |
| 283 | SD.setFlags(SD.getFlags() | SF_WeakDefinition); |
| 284 | break; |
Daniel Dunbar | 3edd9bb | 2009-08-22 11:41:10 +0000 | [diff] [blame] | 285 | } |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 286 | } |
| 287 | |
| 288 | void MCMachOStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) { |
Daniel Dunbar | 6aff2fb | 2009-08-24 08:40:12 +0000 | [diff] [blame] | 289 | // Encode the 'desc' value into the lowest implementation defined bits. |
| 290 | assert(DescValue == (DescValue & SF_DescFlagsMask) && |
| 291 | "Invalid .desc value!"); |
| 292 | getSymbolData(*Symbol).setFlags(DescValue & SF_DescFlagsMask); |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 293 | } |
| 294 | |
Chris Lattner | 9eb158d | 2010-01-23 07:47:02 +0000 | [diff] [blame] | 295 | void MCMachOStreamer::EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size, |
Daniel Dunbar | 7092c7e | 2009-08-30 06:17:16 +0000 | [diff] [blame] | 296 | unsigned ByteAlignment) { |
Daniel Dunbar | 8f4d146 | 2009-08-28 07:08:35 +0000 | [diff] [blame] | 297 | // FIXME: Darwin 'as' does appear to allow redef of a .comm by itself. |
| 298 | assert(Symbol->isUndefined() && "Cannot define a symbol twice!"); |
| 299 | |
| 300 | MCSymbolData &SD = getSymbolData(*Symbol); |
| 301 | SD.setExternal(true); |
Daniel Dunbar | 7092c7e | 2009-08-30 06:17:16 +0000 | [diff] [blame] | 302 | SD.setCommon(Size, ByteAlignment); |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 303 | } |
| 304 | |
Daniel Dunbar | 8751b94 | 2009-08-28 05:48:22 +0000 | [diff] [blame] | 305 | void MCMachOStreamer::EmitZerofill(const MCSection *Section, MCSymbol *Symbol, |
Daniel Dunbar | 7092c7e | 2009-08-30 06:17:16 +0000 | [diff] [blame] | 306 | unsigned Size, unsigned ByteAlignment) { |
Daniel Dunbar | d5a8e98 | 2009-08-28 05:49:21 +0000 | [diff] [blame] | 307 | MCSectionData &SectData = getSectionData(*Section); |
| 308 | |
| 309 | // The symbol may not be present, which only creates the section. |
| 310 | if (!Symbol) |
| 311 | return; |
| 312 | |
| 313 | // FIXME: Assert that this section has the zerofill type. |
| 314 | |
| 315 | assert(Symbol->isUndefined() && "Cannot define a symbol twice!"); |
| 316 | |
| 317 | MCSymbolData &SD = getSymbolData(*Symbol); |
| 318 | |
Daniel Dunbar | 7092c7e | 2009-08-30 06:17:16 +0000 | [diff] [blame] | 319 | MCFragment *F = new MCZeroFillFragment(Size, ByteAlignment, &SectData); |
Daniel Dunbar | d5a8e98 | 2009-08-28 05:49:21 +0000 | [diff] [blame] | 320 | SD.setFragment(F); |
| 321 | |
| 322 | Symbol->setSection(*Section); |
| 323 | |
| 324 | // Update the maximum alignment on the zero fill section if necessary. |
| 325 | if (ByteAlignment > SectData.getAlignment()) |
| 326 | SectData.setAlignment(ByteAlignment); |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 327 | } |
| 328 | |
Chris Lattner | aaec205 | 2010-01-19 19:46:13 +0000 | [diff] [blame] | 329 | void MCMachOStreamer::EmitBytes(StringRef Data, unsigned AddrSpace) { |
Daniel Dunbar | f3d2ef0 | 2009-08-22 10:13:24 +0000 | [diff] [blame] | 330 | MCDataFragment *DF = dyn_cast_or_null<MCDataFragment>(getCurrentFragment()); |
| 331 | if (!DF) |
| 332 | DF = new MCDataFragment(CurSectionData); |
Daniel Dunbar | 0705fbf | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 333 | DF->getContents().append(Data.begin(), Data.end()); |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 334 | } |
| 335 | |
Chris Lattner | aaec205 | 2010-01-19 19:46:13 +0000 | [diff] [blame] | 336 | void MCMachOStreamer::EmitValue(const MCExpr *Value, unsigned Size, |
| 337 | unsigned AddrSpace) { |
Daniel Dunbar | 45f4874 | 2010-02-13 09:28:22 +0000 | [diff] [blame] | 338 | MCDataFragment *DF = dyn_cast_or_null<MCDataFragment>(getCurrentFragment()); |
| 339 | if (!DF) |
| 340 | DF = new MCDataFragment(CurSectionData); |
| 341 | |
| 342 | // Avoid fixups when possible. |
| 343 | int64_t AbsValue; |
Daniel Dunbar | 40ebe247 | 2010-02-22 22:08:57 +0000 | [diff] [blame] | 344 | if (AddValueSymbols(Value)->EvaluateAsAbsolute(AbsValue)) { |
Daniel Dunbar | 45f4874 | 2010-02-13 09:28:22 +0000 | [diff] [blame] | 345 | // FIXME: Endianness assumption. |
| 346 | for (unsigned i = 0; i != Size; ++i) |
| 347 | DF->getContents().push_back(uint8_t(AbsValue >> (i * 8))); |
| 348 | } else { |
| 349 | DF->getFixups().push_back(MCAsmFixup(DF->getContents().size(), |
Daniel Dunbar | 2be2fd0 | 2010-02-13 09:28:54 +0000 | [diff] [blame] | 350 | *AddValueSymbols(Value), |
| 351 | MCFixup::getKindForSize(Size))); |
Daniel Dunbar | 45f4874 | 2010-02-13 09:28:22 +0000 | [diff] [blame] | 352 | DF->getContents().resize(DF->getContents().size() + Size, 0); |
| 353 | } |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 354 | } |
| 355 | |
| 356 | void MCMachOStreamer::EmitValueToAlignment(unsigned ByteAlignment, |
| 357 | int64_t Value, unsigned ValueSize, |
| 358 | unsigned MaxBytesToEmit) { |
Daniel Dunbar | d6f761e | 2009-08-21 23:07:38 +0000 | [diff] [blame] | 359 | if (MaxBytesToEmit == 0) |
| 360 | MaxBytesToEmit = ByteAlignment; |
Daniel Dunbar | 0705fbf | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 361 | new MCAlignFragment(ByteAlignment, Value, ValueSize, MaxBytesToEmit, |
Kevin Enderby | 6e72048 | 2010-02-23 18:26:34 +0000 | [diff] [blame^] | 362 | false /* EmitNops */, CurSectionData); |
| 363 | |
| 364 | // Update the maximum alignment on the current section if necessary. |
| 365 | if (ByteAlignment > CurSectionData->getAlignment()) |
| 366 | CurSectionData->setAlignment(ByteAlignment); |
| 367 | } |
| 368 | |
| 369 | void MCMachOStreamer::EmitCodeAlignment(unsigned ByteAlignment, |
| 370 | unsigned MaxBytesToEmit) { |
| 371 | if (MaxBytesToEmit == 0) |
| 372 | MaxBytesToEmit = ByteAlignment; |
| 373 | // FIXME the 0x90 is the default x86 1 byte nop opcode. |
| 374 | new MCAlignFragment(ByteAlignment, 0x90, 1, MaxBytesToEmit, |
| 375 | true /* EmitNops */, CurSectionData); |
Daniel Dunbar | 0705fbf | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 376 | |
Daniel Dunbar | f3d2ef0 | 2009-08-22 10:13:24 +0000 | [diff] [blame] | 377 | // Update the maximum alignment on the current section if necessary. |
Daniel Dunbar | 0705fbf | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 378 | if (ByteAlignment > CurSectionData->getAlignment()) |
| 379 | CurSectionData->setAlignment(ByteAlignment); |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 380 | } |
| 381 | |
Daniel Dunbar | 821e333 | 2009-08-31 08:09:28 +0000 | [diff] [blame] | 382 | void MCMachOStreamer::EmitValueToOffset(const MCExpr *Offset, |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 383 | unsigned char Value) { |
Daniel Dunbar | 1253a6f | 2009-10-16 01:58:03 +0000 | [diff] [blame] | 384 | new MCOrgFragment(*Offset, Value, CurSectionData); |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 385 | } |
| 386 | |
| 387 | void MCMachOStreamer::EmitInstruction(const MCInst &Inst) { |
Daniel Dunbar | 4fac749 | 2009-08-27 08:17:51 +0000 | [diff] [blame] | 388 | // Scan for values. |
| 389 | for (unsigned i = 0; i != Inst.getNumOperands(); ++i) |
Daniel Dunbar | 8c2eebe | 2009-08-31 08:08:38 +0000 | [diff] [blame] | 390 | if (Inst.getOperand(i).isExpr()) |
| 391 | AddValueSymbols(Inst.getOperand(i).getExpr()); |
Daniel Dunbar | 4fac749 | 2009-08-27 08:17:51 +0000 | [diff] [blame] | 392 | |
| 393 | if (!Emitter) |
| 394 | llvm_unreachable("no code emitter available!"); |
| 395 | |
Daniel Dunbar | e1ec617 | 2010-02-02 21:44:01 +0000 | [diff] [blame] | 396 | CurSectionData->setHasInstructions(true); |
| 397 | |
Daniel Dunbar | 73c5574 | 2010-02-09 22:59:55 +0000 | [diff] [blame] | 398 | SmallVector<MCFixup, 4> Fixups; |
Daniel Dunbar | 4fac749 | 2009-08-27 08:17:51 +0000 | [diff] [blame] | 399 | SmallString<256> Code; |
| 400 | raw_svector_ostream VecOS(Code); |
Daniel Dunbar | 73c5574 | 2010-02-09 22:59:55 +0000 | [diff] [blame] | 401 | Emitter->EncodeInstruction(Inst, VecOS, Fixups); |
Daniel Dunbar | f634676 | 2010-02-13 09:29:02 +0000 | [diff] [blame] | 402 | VecOS.flush(); |
| 403 | |
| 404 | // Add the fixups and data. |
| 405 | MCDataFragment *DF = dyn_cast_or_null<MCDataFragment>(getCurrentFragment()); |
| 406 | if (!DF) |
| 407 | DF = new MCDataFragment(CurSectionData); |
| 408 | for (unsigned i = 0, e = Fixups.size(); i != e; ++i) { |
| 409 | MCFixup &F = Fixups[i]; |
| 410 | DF->getFixups().push_back(MCAsmFixup(DF->getContents().size()+F.getOffset(), |
| 411 | *F.getValue(), |
| 412 | F.getKind())); |
| 413 | } |
| 414 | DF->getContents().append(Code.begin(), Code.end()); |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 415 | } |
| 416 | |
| 417 | void MCMachOStreamer::Finish() { |
| 418 | Assembler.Finish(); |
| 419 | } |
| 420 | |
Daniel Dunbar | 4fac749 | 2009-08-27 08:17:51 +0000 | [diff] [blame] | 421 | MCStreamer *llvm::createMachOStreamer(MCContext &Context, raw_ostream &OS, |
| 422 | MCCodeEmitter *CE) { |
| 423 | return new MCMachOStreamer(Context, OS, CE); |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 424 | } |