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