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