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