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" |
Kevin Enderby | b07ce60 | 2010-08-09 22:52:14 +0000 | [diff] [blame] | 21 | #include "llvm/MC/MCSectionMachO.h" |
| 22 | #include "llvm/MC/MCDwarf.h" |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 23 | #include "llvm/Support/Dwarf.h" |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 24 | #include "llvm/Support/ErrorHandling.h" |
Daniel Dunbar | 4fac749 | 2009-08-27 08:17:51 +0000 | [diff] [blame] | 25 | #include "llvm/Support/raw_ostream.h" |
Daniel Dunbar | d8036fb | 2010-03-23 05:09:03 +0000 | [diff] [blame] | 26 | #include "llvm/Target/TargetAsmBackend.h" |
| 27 | |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 28 | using namespace llvm; |
| 29 | |
| 30 | namespace { |
| 31 | |
Daniel Dunbar | 8dc68ab | 2010-06-16 20:04:22 +0000 | [diff] [blame] | 32 | class MCMachOStreamer : public MCObjectStreamer { |
Daniel Dunbar | 6aff2fb | 2009-08-24 08:40:12 +0000 | [diff] [blame] | 33 | private: |
Rafael Espindola | f89671d | 2010-11-01 16:27:31 +0000 | [diff] [blame] | 34 | virtual void EmitInstToData(const MCInst &Inst); |
Daniel Dunbar | 2ac0c45 | 2010-05-26 20:37:00 +0000 | [diff] [blame] | 35 | |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 36 | public: |
Daniel Dunbar | 1f3e445 | 2010-03-11 01:34:27 +0000 | [diff] [blame] | 37 | MCMachOStreamer(MCContext &Context, TargetAsmBackend &TAB, |
Daniel Dunbar | 8dc68ab | 2010-06-16 20:04:22 +0000 | [diff] [blame] | 38 | raw_ostream &OS, MCCodeEmitter *Emitter) |
Rafael Espindola | 59ff3c9 | 2010-09-22 22:27:05 +0000 | [diff] [blame] | 39 | : MCObjectStreamer(Context, TAB, OS, Emitter, true) {} |
Daniel Dunbar | ac2884a | 2010-03-25 22:49:09 +0000 | [diff] [blame] | 40 | |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 41 | /// @name MCStreamer Interface |
| 42 | /// @{ |
| 43 | |
Rafael Espindola | d80781b | 2010-09-15 21:48:40 +0000 | [diff] [blame] | 44 | virtual void InitSections(); |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 45 | virtual void EmitLabel(MCSymbol *Symbol); |
Chris Lattner | a5ad93a | 2010-01-23 06:39:22 +0000 | [diff] [blame] | 46 | virtual void EmitAssemblerFlag(MCAssemblerFlag Flag); |
Jim Grosbach | ce79299 | 2010-11-05 22:08:08 +0000 | [diff] [blame] | 47 | virtual void EmitThumbFunc(MCSymbol *Func); |
Daniel Dunbar | 821e333 | 2009-08-31 08:09:28 +0000 | [diff] [blame] | 48 | virtual void EmitAssignment(MCSymbol *Symbol, const MCExpr *Value); |
Chris Lattner | a5ad93a | 2010-01-23 06:39:22 +0000 | [diff] [blame] | 49 | virtual void EmitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute); |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 50 | virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue); |
Chris Lattner | 9eb158d | 2010-01-23 07:47:02 +0000 | [diff] [blame] | 51 | virtual void EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size, |
Daniel Dunbar | 7092c7e | 2009-08-30 06:17:16 +0000 | [diff] [blame] | 52 | unsigned ByteAlignment); |
Chris Lattner | b54b9dd | 2010-05-08 19:54:22 +0000 | [diff] [blame] | 53 | virtual void BeginCOFFSymbolDef(const MCSymbol *Symbol) { |
| 54 | assert(0 && "macho doesn't support this directive"); |
| 55 | } |
| 56 | virtual void EmitCOFFSymbolStorageClass(int StorageClass) { |
| 57 | assert(0 && "macho doesn't support this directive"); |
| 58 | } |
| 59 | virtual void EmitCOFFSymbolType(int Type) { |
| 60 | assert(0 && "macho doesn't support this directive"); |
| 61 | } |
| 62 | virtual void EndCOFFSymbolDef() { |
| 63 | assert(0 && "macho doesn't support this directive"); |
| 64 | } |
Chris Lattner | 99328ad | 2010-01-25 07:52:13 +0000 | [diff] [blame] | 65 | virtual void EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) { |
| 66 | assert(0 && "macho doesn't support this directive"); |
| 67 | } |
Chris Lattner | 9eb158d | 2010-01-23 07:47:02 +0000 | [diff] [blame] | 68 | virtual void EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size) { |
| 69 | assert(0 && "macho doesn't support this directive"); |
| 70 | } |
Daniel Dunbar | 8751b94 | 2009-08-28 05:48:22 +0000 | [diff] [blame] | 71 | virtual void EmitZerofill(const MCSection *Section, MCSymbol *Symbol = 0, |
Daniel Dunbar | 7092c7e | 2009-08-30 06:17:16 +0000 | [diff] [blame] | 72 | unsigned Size = 0, unsigned ByteAlignment = 0); |
Eric Christopher | 011c3f1 | 2010-05-18 21:26:41 +0000 | [diff] [blame] | 73 | virtual void EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol, |
| 74 | uint64_t Size, unsigned ByteAlignment = 0); |
Chris Lattner | aaec205 | 2010-01-19 19:46:13 +0000 | [diff] [blame] | 75 | virtual void EmitBytes(StringRef Data, unsigned AddrSpace); |
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 | 2ac0c45 | 2010-05-26 20:37:00 +0000 | [diff] [blame] | 81 | |
Chris Lattner | a6594fc | 2010-01-25 18:58:59 +0000 | [diff] [blame] | 82 | virtual void EmitFileDirective(StringRef Filename) { |
Daniel Dunbar | 9674967 | 2010-07-19 20:44:20 +0000 | [diff] [blame] | 83 | // FIXME: Just ignore the .file; it isn't important enough to fail the |
| 84 | // entire assembly. |
| 85 | |
| 86 | //report_fatal_error("unsupported directive: '.file'"); |
Chris Lattner | a6594fc | 2010-01-25 18:58:59 +0000 | [diff] [blame] | 87 | } |
Daniel Dunbar | 2ac0c45 | 2010-05-26 20:37:00 +0000 | [diff] [blame] | 88 | |
Daniel Dunbar | a86de10 | 2010-06-16 20:04:32 +0000 | [diff] [blame] | 89 | virtual void Finish(); |
| 90 | |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 91 | /// @} |
| 92 | }; |
| 93 | |
| 94 | } // end anonymous namespace. |
| 95 | |
Rafael Espindola | d80781b | 2010-09-15 21:48:40 +0000 | [diff] [blame] | 96 | void MCMachOStreamer::InitSections() { |
| 97 | SwitchSection(getContext().getMachOSection("__TEXT", "__text", |
| 98 | MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS, |
| 99 | 0, SectionKind::getText())); |
| 100 | |
| 101 | } |
| 102 | |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 103 | void MCMachOStreamer::EmitLabel(MCSymbol *Symbol) { |
Rafael Espindola | ba21024 | 2010-11-28 16:22:59 +0000 | [diff] [blame] | 104 | assert(Symbol->isUndefined() && "Cannot define a symbol twice!"); |
Rafael Espindola | ba21024 | 2010-11-28 16:22:59 +0000 | [diff] [blame] | 105 | |
Rafael Espindola | ea4afa9 | 2010-11-28 17:18:55 +0000 | [diff] [blame] | 106 | // isSymbolLinkerVisible uses the section. |
Rafael Espindola | ba21024 | 2010-11-28 16:22:59 +0000 | [diff] [blame] | 107 | Symbol->setSection(*CurSection); |
Daniel Dunbar | a86de10 | 2010-06-16 20:04:32 +0000 | [diff] [blame] | 108 | // We have to create a new fragment if this is an atom defining symbol, |
| 109 | // fragments cannot span atoms. |
Rafael Espindola | ea4afa9 | 2010-11-28 17:18:55 +0000 | [diff] [blame] | 110 | if (getAssembler().isSymbolLinkerVisible(*Symbol)) |
Daniel Dunbar | a86de10 | 2010-06-16 20:04:32 +0000 | [diff] [blame] | 111 | new MCDataFragment(getCurrentSectionData()); |
Daniel Dunbar | 071f73d | 2010-05-10 22:45:09 +0000 | [diff] [blame] | 112 | |
Rafael Espindola | ea4afa9 | 2010-11-28 17:18:55 +0000 | [diff] [blame] | 113 | MCObjectStreamer::EmitLabel(Symbol); |
Daniel Dunbar | 6aff2fb | 2009-08-24 08:40:12 +0000 | [diff] [blame] | 114 | |
Rafael Espindola | ea4afa9 | 2010-11-28 17:18:55 +0000 | [diff] [blame] | 115 | MCSymbolData &SD = getAssembler().getSymbolData(*Symbol); |
Daniel Dunbar | b18d2dd | 2010-05-17 20:12:31 +0000 | [diff] [blame] | 116 | // This causes the reference type flag to be cleared. Darwin 'as' was "trying" |
| 117 | // to clear the weak reference and weak definition bits too, but the |
| 118 | // implementation was buggy. For now we just try to match 'as', for |
| 119 | // diffability. |
| 120 | // |
| 121 | // FIXME: Cleanup this code, these bits should be emitted based on semantic |
| 122 | // properties, not on the order of definition, etc. |
| 123 | SD.setFlags(SD.getFlags() & ~SF_ReferenceTypeMask); |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 124 | } |
| 125 | |
Chris Lattner | a5ad93a | 2010-01-23 06:39:22 +0000 | [diff] [blame] | 126 | void MCMachOStreamer::EmitAssemblerFlag(MCAssemblerFlag Flag) { |
Daniel Dunbar | 6009db4 | 2009-08-26 21:22:22 +0000 | [diff] [blame] | 127 | switch (Flag) { |
Jim Grosbach | ce79299 | 2010-11-05 22:08:08 +0000 | [diff] [blame] | 128 | case MCAF_SyntaxUnified: return; // no-op here. |
| 129 | case MCAF_Code16: return; // no-op here. |
Jim Grosbach | ba21957 | 2010-11-05 22:40:09 +0000 | [diff] [blame] | 130 | case MCAF_Code32: return; // no-op here. |
Chris Lattner | a5ad93a | 2010-01-23 06:39:22 +0000 | [diff] [blame] | 131 | case MCAF_SubsectionsViaSymbols: |
Daniel Dunbar | 8dc68ab | 2010-06-16 20:04:22 +0000 | [diff] [blame] | 132 | getAssembler().setSubsectionsViaSymbols(true); |
Daniel Dunbar | 8c3eaf4 | 2009-08-28 07:08:47 +0000 | [diff] [blame] | 133 | return; |
Chandler Carruth | 5c0d472 | 2010-09-30 10:59:51 +0000 | [diff] [blame] | 134 | default: |
| 135 | llvm_unreachable("invalid assembler flag!"); |
Daniel Dunbar | 6009db4 | 2009-08-26 21:22:22 +0000 | [diff] [blame] | 136 | } |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 137 | } |
| 138 | |
Jim Grosbach | ce79299 | 2010-11-05 22:08:08 +0000 | [diff] [blame] | 139 | void MCMachOStreamer::EmitThumbFunc(MCSymbol *Func) { |
| 140 | // FIXME: Flag the function ISA as thumb with DW_AT_APPLE_isa. |
| 141 | } |
| 142 | |
Daniel Dunbar | 821e333 | 2009-08-31 08:09:28 +0000 | [diff] [blame] | 143 | void MCMachOStreamer::EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) { |
Michael J. Spencer | 8067adc | 2010-07-19 06:13:10 +0000 | [diff] [blame] | 144 | // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into |
| 145 | // MCObjectStreamer. |
Daniel Dunbar | a4d8667 | 2009-10-16 01:58:23 +0000 | [diff] [blame] | 146 | // FIXME: Lift context changes into super class. |
Daniel Dunbar | 8dc68ab | 2010-06-16 20:04:22 +0000 | [diff] [blame] | 147 | getAssembler().getOrCreateSymbolData(*Symbol); |
Daniel Dunbar | 08a408a | 2010-05-05 17:41:00 +0000 | [diff] [blame] | 148 | Symbol->setVariableValue(AddValueSymbols(Value)); |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | void MCMachOStreamer::EmitSymbolAttribute(MCSymbol *Symbol, |
Chris Lattner | a5ad93a | 2010-01-23 06:39:22 +0000 | [diff] [blame] | 152 | MCSymbolAttr Attribute) { |
Daniel Dunbar | 0c7761b | 2009-08-24 11:56:58 +0000 | [diff] [blame] | 153 | // Indirect symbols are handled differently, to match how 'as' handles |
| 154 | // them. This makes writing matching .o files easier. |
Chris Lattner | a5ad93a | 2010-01-23 06:39:22 +0000 | [diff] [blame] | 155 | if (Attribute == MCSA_IndirectSymbol) { |
Daniel Dunbar | ad7c3d5 | 2009-08-26 00:18:21 +0000 | [diff] [blame] | 156 | // Note that we intentionally cannot use the symbol data here; this is |
| 157 | // important for matching the string table that 'as' generates. |
Daniel Dunbar | 0c7761b | 2009-08-24 11:56:58 +0000 | [diff] [blame] | 158 | IndirectSymbolData ISD; |
| 159 | ISD.Symbol = Symbol; |
Daniel Dunbar | 83b4671 | 2010-06-16 20:04:25 +0000 | [diff] [blame] | 160 | ISD.SectionData = getCurrentSectionData(); |
Daniel Dunbar | 8dc68ab | 2010-06-16 20:04:22 +0000 | [diff] [blame] | 161 | getAssembler().getIndirectSymbols().push_back(ISD); |
Daniel Dunbar | 0c7761b | 2009-08-24 11:56:58 +0000 | [diff] [blame] | 162 | return; |
| 163 | } |
| 164 | |
Daniel Dunbar | 6aff2fb | 2009-08-24 08:40:12 +0000 | [diff] [blame] | 165 | // Adding a symbol attribute always introduces the symbol, note that an |
Daniel Dunbar | 46836a7 | 2010-03-10 20:58:29 +0000 | [diff] [blame] | 166 | // important side effect of calling getOrCreateSymbolData here is to register |
| 167 | // the symbol with the assembler. |
Daniel Dunbar | 8dc68ab | 2010-06-16 20:04:22 +0000 | [diff] [blame] | 168 | MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol); |
Daniel Dunbar | 6aff2fb | 2009-08-24 08:40:12 +0000 | [diff] [blame] | 169 | |
| 170 | // The implementation of symbol attributes is designed to match 'as', but it |
| 171 | // leaves much to desired. It doesn't really make sense to arbitrarily add and |
| 172 | // remove flags, but 'as' allows this (in particular, see .desc). |
| 173 | // |
| 174 | // In the future it might be worth trying to make these operations more well |
| 175 | // defined. |
Daniel Dunbar | 3edd9bb | 2009-08-22 11:41:10 +0000 | [diff] [blame] | 176 | switch (Attribute) { |
Chris Lattner | a5ad93a | 2010-01-23 06:39:22 +0000 | [diff] [blame] | 177 | case MCSA_Invalid: |
Chris Lattner | ed0ab15 | 2010-01-25 18:30:45 +0000 | [diff] [blame] | 178 | case MCSA_ELF_TypeFunction: |
| 179 | case MCSA_ELF_TypeIndFunction: |
| 180 | case MCSA_ELF_TypeObject: |
| 181 | case MCSA_ELF_TypeTLS: |
| 182 | case MCSA_ELF_TypeCommon: |
| 183 | case MCSA_ELF_TypeNoType: |
Rafael Espindola | e13a0ff | 2010-11-13 04:51:02 +0000 | [diff] [blame] | 184 | case MCSA_ELF_TypeGnuUniqueObject: |
Chris Lattner | a5ad93a | 2010-01-23 06:39:22 +0000 | [diff] [blame] | 185 | case MCSA_IndirectSymbol: |
| 186 | case MCSA_Hidden: |
| 187 | case MCSA_Internal: |
| 188 | case MCSA_Protected: |
| 189 | case MCSA_Weak: |
| 190 | case MCSA_Local: |
Daniel Dunbar | 6aff2fb | 2009-08-24 08:40:12 +0000 | [diff] [blame] | 191 | assert(0 && "Invalid symbol attribute for Mach-O!"); |
| 192 | break; |
Daniel Dunbar | 3edd9bb | 2009-08-22 11:41:10 +0000 | [diff] [blame] | 193 | |
Chris Lattner | a5ad93a | 2010-01-23 06:39:22 +0000 | [diff] [blame] | 194 | case MCSA_Global: |
Daniel Dunbar | 8f4d146 | 2009-08-28 07:08:35 +0000 | [diff] [blame] | 195 | SD.setExternal(true); |
Daniel Dunbar | b18d2dd | 2010-05-17 20:12:31 +0000 | [diff] [blame] | 196 | // This effectively clears the undefined lazy bit, in Darwin 'as', although |
| 197 | // it isn't very consistent because it implements this as part of symbol |
| 198 | // lookup. |
| 199 | // |
| 200 | // FIXME: Cleanup this code, these bits should be emitted based on semantic |
| 201 | // properties, not on the order of definition, etc. |
| 202 | SD.setFlags(SD.getFlags() & ~SF_ReferenceTypeUndefinedLazy); |
Daniel Dunbar | 3edd9bb | 2009-08-22 11:41:10 +0000 | [diff] [blame] | 203 | break; |
Daniel Dunbar | 6aff2fb | 2009-08-24 08:40:12 +0000 | [diff] [blame] | 204 | |
Chris Lattner | a5ad93a | 2010-01-23 06:39:22 +0000 | [diff] [blame] | 205 | case MCSA_LazyReference: |
Daniel Dunbar | 6aff2fb | 2009-08-24 08:40:12 +0000 | [diff] [blame] | 206 | // FIXME: This requires -dynamic. |
| 207 | SD.setFlags(SD.getFlags() | SF_NoDeadStrip); |
| 208 | if (Symbol->isUndefined()) |
| 209 | SD.setFlags(SD.getFlags() | SF_ReferenceTypeUndefinedLazy); |
| 210 | break; |
| 211 | |
Daniel Dunbar | 6aff2fb | 2009-08-24 08:40:12 +0000 | [diff] [blame] | 212 | // Since .reference sets the no dead strip bit, it is equivalent to |
| 213 | // .no_dead_strip in practice. |
Chris Lattner | a5ad93a | 2010-01-23 06:39:22 +0000 | [diff] [blame] | 214 | case MCSA_Reference: |
| 215 | case MCSA_NoDeadStrip: |
Daniel Dunbar | 6aff2fb | 2009-08-24 08:40:12 +0000 | [diff] [blame] | 216 | SD.setFlags(SD.getFlags() | SF_NoDeadStrip); |
| 217 | break; |
| 218 | |
Kevin Enderby | e8e98d7 | 2010-11-19 18:39:33 +0000 | [diff] [blame] | 219 | case MCSA_SymbolResolver: |
| 220 | SD.setFlags(SD.getFlags() | SF_SymbolResolver); |
| 221 | break; |
| 222 | |
Chris Lattner | a5ad93a | 2010-01-23 06:39:22 +0000 | [diff] [blame] | 223 | case MCSA_PrivateExtern: |
Daniel Dunbar | 6aff2fb | 2009-08-24 08:40:12 +0000 | [diff] [blame] | 224 | SD.setExternal(true); |
| 225 | SD.setPrivateExtern(true); |
| 226 | break; |
| 227 | |
Chris Lattner | a5ad93a | 2010-01-23 06:39:22 +0000 | [diff] [blame] | 228 | case MCSA_WeakReference: |
Daniel Dunbar | 6aff2fb | 2009-08-24 08:40:12 +0000 | [diff] [blame] | 229 | // FIXME: This requires -dynamic. |
| 230 | if (Symbol->isUndefined()) |
| 231 | SD.setFlags(SD.getFlags() | SF_WeakReference); |
| 232 | break; |
| 233 | |
Chris Lattner | a5ad93a | 2010-01-23 06:39:22 +0000 | [diff] [blame] | 234 | case MCSA_WeakDefinition: |
Daniel Dunbar | 6aff2fb | 2009-08-24 08:40:12 +0000 | [diff] [blame] | 235 | // FIXME: 'as' enforces that this is defined and global. The manual claims |
| 236 | // it has to be in a coalesced section, but this isn't enforced. |
| 237 | SD.setFlags(SD.getFlags() | SF_WeakDefinition); |
| 238 | break; |
Kevin Enderby | f59cac5 | 2010-07-08 17:22:42 +0000 | [diff] [blame] | 239 | |
| 240 | case MCSA_WeakDefAutoPrivate: |
| 241 | SD.setFlags(SD.getFlags() | SF_WeakDefinition | SF_WeakReference); |
| 242 | break; |
Daniel Dunbar | 3edd9bb | 2009-08-22 11:41:10 +0000 | [diff] [blame] | 243 | } |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 244 | } |
| 245 | |
| 246 | void MCMachOStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) { |
Daniel Dunbar | 6aff2fb | 2009-08-24 08:40:12 +0000 | [diff] [blame] | 247 | // Encode the 'desc' value into the lowest implementation defined bits. |
Daniel Dunbar | 2ac0c45 | 2010-05-26 20:37:00 +0000 | [diff] [blame] | 248 | assert(DescValue == (DescValue & SF_DescFlagsMask) && |
Daniel Dunbar | 6aff2fb | 2009-08-24 08:40:12 +0000 | [diff] [blame] | 249 | "Invalid .desc value!"); |
Daniel Dunbar | 8dc68ab | 2010-06-16 20:04:22 +0000 | [diff] [blame] | 250 | getAssembler().getOrCreateSymbolData(*Symbol).setFlags( |
| 251 | DescValue & SF_DescFlagsMask); |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 252 | } |
| 253 | |
Chris Lattner | 9eb158d | 2010-01-23 07:47:02 +0000 | [diff] [blame] | 254 | void MCMachOStreamer::EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size, |
Daniel Dunbar | 7092c7e | 2009-08-30 06:17:16 +0000 | [diff] [blame] | 255 | unsigned ByteAlignment) { |
Daniel Dunbar | 8f4d146 | 2009-08-28 07:08:35 +0000 | [diff] [blame] | 256 | // FIXME: Darwin 'as' does appear to allow redef of a .comm by itself. |
| 257 | assert(Symbol->isUndefined() && "Cannot define a symbol twice!"); |
| 258 | |
Daniel Dunbar | 8dc68ab | 2010-06-16 20:04:22 +0000 | [diff] [blame] | 259 | MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol); |
Daniel Dunbar | 8f4d146 | 2009-08-28 07:08:35 +0000 | [diff] [blame] | 260 | SD.setExternal(true); |
Daniel Dunbar | 7092c7e | 2009-08-30 06:17:16 +0000 | [diff] [blame] | 261 | SD.setCommon(Size, ByteAlignment); |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 262 | } |
| 263 | |
Daniel Dunbar | 8751b94 | 2009-08-28 05:48:22 +0000 | [diff] [blame] | 264 | void MCMachOStreamer::EmitZerofill(const MCSection *Section, MCSymbol *Symbol, |
Daniel Dunbar | 7092c7e | 2009-08-30 06:17:16 +0000 | [diff] [blame] | 265 | unsigned Size, unsigned ByteAlignment) { |
Daniel Dunbar | 8dc68ab | 2010-06-16 20:04:22 +0000 | [diff] [blame] | 266 | MCSectionData &SectData = getAssembler().getOrCreateSectionData(*Section); |
Daniel Dunbar | d5a8e98 | 2009-08-28 05:49:21 +0000 | [diff] [blame] | 267 | |
| 268 | // The symbol may not be present, which only creates the section. |
| 269 | if (!Symbol) |
| 270 | return; |
| 271 | |
| 272 | // FIXME: Assert that this section has the zerofill type. |
| 273 | |
| 274 | assert(Symbol->isUndefined() && "Cannot define a symbol twice!"); |
| 275 | |
Daniel Dunbar | 8dc68ab | 2010-06-16 20:04:22 +0000 | [diff] [blame] | 276 | MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol); |
Daniel Dunbar | d5a8e98 | 2009-08-28 05:49:21 +0000 | [diff] [blame] | 277 | |
Daniel Dunbar | e73d49e | 2010-05-12 22:51:27 +0000 | [diff] [blame] | 278 | // Emit an align fragment if necessary. |
| 279 | if (ByteAlignment != 1) |
Daniel Dunbar | 1c15413 | 2010-05-12 22:56:23 +0000 | [diff] [blame] | 280 | new MCAlignFragment(ByteAlignment, 0, 0, ByteAlignment, &SectData); |
Daniel Dunbar | e73d49e | 2010-05-12 22:51:27 +0000 | [diff] [blame] | 281 | |
Daniel Dunbar | 4e54487 | 2010-05-12 22:51:38 +0000 | [diff] [blame] | 282 | MCFragment *F = new MCFillFragment(0, 0, Size, &SectData); |
Daniel Dunbar | d5a8e98 | 2009-08-28 05:49:21 +0000 | [diff] [blame] | 283 | SD.setFragment(F); |
| 284 | |
| 285 | Symbol->setSection(*Section); |
| 286 | |
| 287 | // Update the maximum alignment on the zero fill section if necessary. |
| 288 | if (ByteAlignment > SectData.getAlignment()) |
| 289 | SectData.setAlignment(ByteAlignment); |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 290 | } |
| 291 | |
Eric Christopher | 070c1ab | 2010-05-21 23:03:53 +0000 | [diff] [blame] | 292 | // This should always be called with the thread local bss section. Like the |
| 293 | // .zerofill directive this doesn't actually switch sections on us. |
Eric Christopher | 011c3f1 | 2010-05-18 21:26:41 +0000 | [diff] [blame] | 294 | void MCMachOStreamer::EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol, |
| 295 | uint64_t Size, unsigned ByteAlignment) { |
| 296 | EmitZerofill(Section, Symbol, Size, ByteAlignment); |
| 297 | return; |
Eric Christopher | 482eba0 | 2010-05-14 01:50:28 +0000 | [diff] [blame] | 298 | } |
| 299 | |
Chris Lattner | aaec205 | 2010-01-19 19:46:13 +0000 | [diff] [blame] | 300 | void MCMachOStreamer::EmitBytes(StringRef Data, unsigned AddrSpace) { |
Michael J. Spencer | 8067adc | 2010-07-19 06:13:10 +0000 | [diff] [blame] | 301 | // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into |
| 302 | // MCObjectStreamer. |
Daniel Dunbar | f70f477 | 2010-03-22 20:35:46 +0000 | [diff] [blame] | 303 | getOrCreateDataFragment()->getContents().append(Data.begin(), Data.end()); |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 304 | } |
| 305 | |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 306 | void MCMachOStreamer::EmitValueToAlignment(unsigned ByteAlignment, |
| 307 | int64_t Value, unsigned ValueSize, |
| 308 | unsigned MaxBytesToEmit) { |
Michael J. Spencer | 8067adc | 2010-07-19 06:13:10 +0000 | [diff] [blame] | 309 | // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into |
| 310 | // MCObjectStreamer. |
Daniel Dunbar | d6f761e | 2009-08-21 23:07:38 +0000 | [diff] [blame] | 311 | if (MaxBytesToEmit == 0) |
| 312 | MaxBytesToEmit = ByteAlignment; |
Daniel Dunbar | a86de10 | 2010-06-16 20:04:32 +0000 | [diff] [blame] | 313 | new MCAlignFragment(ByteAlignment, Value, ValueSize, MaxBytesToEmit, |
| 314 | getCurrentSectionData()); |
Kevin Enderby | 6e72048 | 2010-02-23 18:26:34 +0000 | [diff] [blame] | 315 | |
| 316 | // Update the maximum alignment on the current section if necessary. |
Daniel Dunbar | 83b4671 | 2010-06-16 20:04:25 +0000 | [diff] [blame] | 317 | if (ByteAlignment > getCurrentSectionData()->getAlignment()) |
| 318 | getCurrentSectionData()->setAlignment(ByteAlignment); |
Kevin Enderby | 6e72048 | 2010-02-23 18:26:34 +0000 | [diff] [blame] | 319 | } |
| 320 | |
| 321 | void MCMachOStreamer::EmitCodeAlignment(unsigned ByteAlignment, |
| 322 | unsigned MaxBytesToEmit) { |
Michael J. Spencer | 8067adc | 2010-07-19 06:13:10 +0000 | [diff] [blame] | 323 | // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into |
| 324 | // MCObjectStreamer. |
Kevin Enderby | 6e72048 | 2010-02-23 18:26:34 +0000 | [diff] [blame] | 325 | if (MaxBytesToEmit == 0) |
| 326 | MaxBytesToEmit = ByteAlignment; |
Daniel Dunbar | 1c15413 | 2010-05-12 22:56:23 +0000 | [diff] [blame] | 327 | MCAlignFragment *F = new MCAlignFragment(ByteAlignment, 0, 1, MaxBytesToEmit, |
Daniel Dunbar | 83b4671 | 2010-06-16 20:04:25 +0000 | [diff] [blame] | 328 | getCurrentSectionData()); |
Daniel Dunbar | 1c15413 | 2010-05-12 22:56:23 +0000 | [diff] [blame] | 329 | F->setEmitNops(true); |
Daniel Dunbar | 0705fbf | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 330 | |
Daniel Dunbar | f3d2ef0 | 2009-08-22 10:13:24 +0000 | [diff] [blame] | 331 | // Update the maximum alignment on the current section if necessary. |
Daniel Dunbar | 83b4671 | 2010-06-16 20:04:25 +0000 | [diff] [blame] | 332 | if (ByteAlignment > getCurrentSectionData()->getAlignment()) |
| 333 | getCurrentSectionData()->setAlignment(ByteAlignment); |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 334 | } |
| 335 | |
Daniel Dunbar | 2ac0c45 | 2010-05-26 20:37:00 +0000 | [diff] [blame] | 336 | void MCMachOStreamer::EmitInstToData(const MCInst &Inst) { |
| 337 | MCDataFragment *DF = getOrCreateDataFragment(); |
| 338 | |
| 339 | SmallVector<MCFixup, 4> Fixups; |
| 340 | SmallString<256> Code; |
| 341 | raw_svector_ostream VecOS(Code); |
Daniel Dunbar | 8dc68ab | 2010-06-16 20:04:22 +0000 | [diff] [blame] | 342 | getAssembler().getEmitter().EncodeInstruction(Inst, VecOS, Fixups); |
Daniel Dunbar | 2ac0c45 | 2010-05-26 20:37:00 +0000 | [diff] [blame] | 343 | VecOS.flush(); |
| 344 | |
| 345 | // Add the fixups and data. |
| 346 | for (unsigned i = 0, e = Fixups.size(); i != e; ++i) { |
| 347 | Fixups[i].setOffset(Fixups[i].getOffset() + DF->getContents().size()); |
| 348 | DF->addFixup(Fixups[i]); |
| 349 | } |
| 350 | DF->getContents().append(Code.begin(), Code.end()); |
| 351 | } |
| 352 | |
Daniel Dunbar | a86de10 | 2010-06-16 20:04:32 +0000 | [diff] [blame] | 353 | void MCMachOStreamer::Finish() { |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 354 | // Dump out the dwarf file & directory tables and line tables. |
Kevin Enderby | 8c9aa92 | 2010-10-02 00:13:41 +0000 | [diff] [blame] | 355 | if (getContext().hasDwarfFiles()) { |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 356 | const MCSection *DwarfLineSection = getContext().getMachOSection("__DWARF", |
| 357 | "__debug_line", |
| 358 | MCSectionMachO::S_ATTR_DEBUG, |
| 359 | 0, SectionKind::getDataRelLocal()); |
Rafael Espindola | 32a006e | 2010-12-03 00:55:40 +0000 | [diff] [blame] | 360 | MCDwarfFileTable::Emit(this, DwarfLineSection); |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 361 | } |
Kevin Enderby | b07ce60 | 2010-08-09 22:52:14 +0000 | [diff] [blame] | 362 | |
Daniel Dunbar | a86de10 | 2010-06-16 20:04:32 +0000 | [diff] [blame] | 363 | // We have to set the fragment atom associations so we can relax properly for |
| 364 | // Mach-O. |
| 365 | |
| 366 | // First, scan the symbol table to build a lookup table from fragments to |
| 367 | // defining symbols. |
| 368 | DenseMap<const MCFragment*, MCSymbolData*> DefiningSymbolMap; |
| 369 | for (MCAssembler::symbol_iterator it = getAssembler().symbol_begin(), |
| 370 | ie = getAssembler().symbol_end(); it != ie; ++it) { |
| 371 | if (getAssembler().isSymbolLinkerVisible(it->getSymbol()) && |
| 372 | it->getFragment()) { |
| 373 | // An atom defining symbol should never be internal to a fragment. |
| 374 | assert(it->getOffset() == 0 && "Invalid offset in atom defining symbol!"); |
| 375 | DefiningSymbolMap[it->getFragment()] = it; |
| 376 | } |
| 377 | } |
| 378 | |
| 379 | // Set the fragment atom associations by tracking the last seen atom defining |
| 380 | // symbol. |
| 381 | for (MCAssembler::iterator it = getAssembler().begin(), |
| 382 | ie = getAssembler().end(); it != ie; ++it) { |
| 383 | MCSymbolData *CurrentAtom = 0; |
| 384 | for (MCSectionData::iterator it2 = it->begin(), |
| 385 | ie2 = it->end(); it2 != ie2; ++it2) { |
| 386 | if (MCSymbolData *SD = DefiningSymbolMap.lookup(it2)) |
| 387 | CurrentAtom = SD; |
| 388 | it2->setAtom(CurrentAtom); |
| 389 | } |
| 390 | } |
| 391 | |
| 392 | this->MCObjectStreamer::Finish(); |
| 393 | } |
| 394 | |
Daniel Dunbar | 1f3e445 | 2010-03-11 01:34:27 +0000 | [diff] [blame] | 395 | MCStreamer *llvm::createMachOStreamer(MCContext &Context, TargetAsmBackend &TAB, |
Daniel Dunbar | ac2884a | 2010-03-25 22:49:09 +0000 | [diff] [blame] | 396 | raw_ostream &OS, MCCodeEmitter *CE, |
| 397 | bool RelaxAll) { |
| 398 | MCMachOStreamer *S = new MCMachOStreamer(Context, TAB, OS, CE); |
| 399 | if (RelaxAll) |
| 400 | S->getAssembler().setRelaxAll(true); |
| 401 | return S; |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 402 | } |