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" |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 23 | #include "llvm/Support/ErrorHandling.h" |
Daniel Dunbar | 4fac749 | 2009-08-27 08:17:51 +0000 | [diff] [blame] | 24 | #include "llvm/Support/raw_ostream.h" |
Daniel Dunbar | d8036fb | 2010-03-23 05:09:03 +0000 | [diff] [blame] | 25 | #include "llvm/Target/TargetAsmBackend.h" |
| 26 | |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 27 | using namespace llvm; |
| 28 | |
| 29 | namespace { |
| 30 | |
Daniel Dunbar | 8dc68ab | 2010-06-16 20:04:22 +0000 | [diff] [blame] | 31 | class MCMachOStreamer : public MCObjectStreamer { |
Daniel Dunbar | 6aff2fb | 2009-08-24 08:40:12 +0000 | [diff] [blame] | 32 | private: |
Daniel Dunbar | 2ac0c45 | 2010-05-26 20:37:00 +0000 | [diff] [blame] | 33 | void EmitInstToFragment(const MCInst &Inst); |
| 34 | void EmitInstToData(const MCInst &Inst); |
Kevin Enderby | b07ce60 | 2010-08-09 22:52:14 +0000 | [diff] [blame] | 35 | // FIXME: These will likely moved to a better place. |
Kevin Enderby | 232ab94 | 2010-08-31 22:55:11 +0000 | [diff] [blame] | 36 | void MakeLineEntryForSection(const MCSection *Section); |
Kevin Enderby | b07ce60 | 2010-08-09 22:52:14 +0000 | [diff] [blame] | 37 | const MCExpr * MakeStartMinusEndExpr(MCSymbol *Start, MCSymbol *End, |
| 38 | int IntVal); |
| 39 | void EmitDwarfFileTable(void); |
Daniel Dunbar | 2ac0c45 | 2010-05-26 20:37:00 +0000 | [diff] [blame] | 40 | |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 41 | public: |
Daniel Dunbar | 1f3e445 | 2010-03-11 01:34:27 +0000 | [diff] [blame] | 42 | MCMachOStreamer(MCContext &Context, TargetAsmBackend &TAB, |
Daniel Dunbar | 8dc68ab | 2010-06-16 20:04:22 +0000 | [diff] [blame] | 43 | raw_ostream &OS, MCCodeEmitter *Emitter) |
Daniel Dunbar | 83b4671 | 2010-06-16 20:04:25 +0000 | [diff] [blame] | 44 | : MCObjectStreamer(Context, TAB, OS, Emitter) {} |
Daniel Dunbar | ac2884a | 2010-03-25 22:49:09 +0000 | [diff] [blame] | 45 | |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 46 | /// @name MCStreamer Interface |
| 47 | /// @{ |
| 48 | |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 49 | virtual void EmitLabel(MCSymbol *Symbol); |
Chris Lattner | a5ad93a | 2010-01-23 06:39:22 +0000 | [diff] [blame] | 50 | virtual void EmitAssemblerFlag(MCAssemblerFlag Flag); |
Daniel Dunbar | 821e333 | 2009-08-31 08:09:28 +0000 | [diff] [blame] | 51 | virtual void EmitAssignment(MCSymbol *Symbol, const MCExpr *Value); |
Chris Lattner | a5ad93a | 2010-01-23 06:39:22 +0000 | [diff] [blame] | 52 | virtual void EmitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute); |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 53 | virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue); |
Chris Lattner | 9eb158d | 2010-01-23 07:47:02 +0000 | [diff] [blame] | 54 | virtual void EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size, |
Daniel Dunbar | 7092c7e | 2009-08-30 06:17:16 +0000 | [diff] [blame] | 55 | unsigned ByteAlignment); |
Chris Lattner | b54b9dd | 2010-05-08 19:54:22 +0000 | [diff] [blame] | 56 | virtual void BeginCOFFSymbolDef(const MCSymbol *Symbol) { |
| 57 | assert(0 && "macho doesn't support this directive"); |
| 58 | } |
| 59 | virtual void EmitCOFFSymbolStorageClass(int StorageClass) { |
| 60 | assert(0 && "macho doesn't support this directive"); |
| 61 | } |
| 62 | virtual void EmitCOFFSymbolType(int Type) { |
| 63 | assert(0 && "macho doesn't support this directive"); |
| 64 | } |
| 65 | virtual void EndCOFFSymbolDef() { |
| 66 | assert(0 && "macho doesn't support this directive"); |
| 67 | } |
Chris Lattner | 99328ad | 2010-01-25 07:52:13 +0000 | [diff] [blame] | 68 | virtual void EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) { |
| 69 | assert(0 && "macho doesn't support this directive"); |
| 70 | } |
Chris Lattner | 9eb158d | 2010-01-23 07:47:02 +0000 | [diff] [blame] | 71 | virtual void EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size) { |
| 72 | assert(0 && "macho doesn't support this directive"); |
| 73 | } |
Daniel Dunbar | 8751b94 | 2009-08-28 05:48:22 +0000 | [diff] [blame] | 74 | virtual void EmitZerofill(const MCSection *Section, MCSymbol *Symbol = 0, |
Daniel Dunbar | 7092c7e | 2009-08-30 06:17:16 +0000 | [diff] [blame] | 75 | unsigned Size = 0, unsigned ByteAlignment = 0); |
Eric Christopher | 011c3f1 | 2010-05-18 21:26:41 +0000 | [diff] [blame] | 76 | virtual void EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol, |
| 77 | uint64_t Size, unsigned ByteAlignment = 0); |
Chris Lattner | aaec205 | 2010-01-19 19:46:13 +0000 | [diff] [blame] | 78 | virtual void EmitBytes(StringRef Data, unsigned AddrSpace); |
Chris Lattner | aaec205 | 2010-01-19 19:46:13 +0000 | [diff] [blame] | 79 | virtual void EmitValue(const MCExpr *Value, unsigned Size,unsigned AddrSpace); |
Chris Lattner | 718fb59 | 2010-01-25 21:28:50 +0000 | [diff] [blame] | 80 | virtual void EmitGPRel32Value(const MCExpr *Value) { |
| 81 | assert(0 && "macho doesn't support this directive"); |
| 82 | } |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 83 | virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0, |
| 84 | unsigned ValueSize = 1, |
| 85 | unsigned MaxBytesToEmit = 0); |
Kevin Enderby | 6e72048 | 2010-02-23 18:26:34 +0000 | [diff] [blame] | 86 | virtual void EmitCodeAlignment(unsigned ByteAlignment, |
| 87 | unsigned MaxBytesToEmit = 0); |
Daniel Dunbar | 821e333 | 2009-08-31 08:09:28 +0000 | [diff] [blame] | 88 | virtual void EmitValueToOffset(const MCExpr *Offset, |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 89 | unsigned char Value = 0); |
Daniel Dunbar | 2ac0c45 | 2010-05-26 20:37:00 +0000 | [diff] [blame] | 90 | |
Chris Lattner | a6594fc | 2010-01-25 18:58:59 +0000 | [diff] [blame] | 91 | virtual void EmitFileDirective(StringRef Filename) { |
Daniel Dunbar | 9674967 | 2010-07-19 20:44:20 +0000 | [diff] [blame] | 92 | // FIXME: Just ignore the .file; it isn't important enough to fail the |
| 93 | // entire assembly. |
| 94 | |
| 95 | //report_fatal_error("unsupported directive: '.file'"); |
Chris Lattner | a6594fc | 2010-01-25 18:58:59 +0000 | [diff] [blame] | 96 | } |
| 97 | virtual void EmitDwarfFileDirective(unsigned FileNo, StringRef Filename) { |
Daniel Dunbar | 9674967 | 2010-07-19 20:44:20 +0000 | [diff] [blame] | 98 | // FIXME: Just ignore the .file; it isn't important enough to fail the |
| 99 | // entire assembly. |
| 100 | |
| 101 | //report_fatal_error("unsupported directive: '.file'"); |
Chris Lattner | a6594fc | 2010-01-25 18:58:59 +0000 | [diff] [blame] | 102 | } |
Daniel Dunbar | 2ac0c45 | 2010-05-26 20:37:00 +0000 | [diff] [blame] | 103 | |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 104 | virtual void EmitInstruction(const MCInst &Inst); |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 105 | |
Daniel Dunbar | a86de10 | 2010-06-16 20:04:32 +0000 | [diff] [blame] | 106 | virtual void Finish(); |
| 107 | |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 108 | /// @} |
| 109 | }; |
| 110 | |
| 111 | } // end anonymous namespace. |
| 112 | |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 113 | void MCMachOStreamer::EmitLabel(MCSymbol *Symbol) { |
Michael J. Spencer | 8067adc | 2010-07-19 06:13:10 +0000 | [diff] [blame] | 114 | // TODO: This is almost exactly the same as WinCOFFStreamer. Consider merging |
| 115 | // into MCObjectStreamer. |
Daniel Dunbar | 8906ff1 | 2009-08-22 07:22:36 +0000 | [diff] [blame] | 116 | assert(Symbol->isUndefined() && "Cannot define a symbol twice!"); |
Daniel Dunbar | c304718 | 2010-05-05 19:01:00 +0000 | [diff] [blame] | 117 | assert(!Symbol->isVariable() && "Cannot emit a variable symbol!"); |
| 118 | assert(CurSection && "Cannot emit before setting section!"); |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 119 | |
Daniel Dunbar | a86de10 | 2010-06-16 20:04:32 +0000 | [diff] [blame] | 120 | Symbol->setSection(*CurSection); |
| 121 | |
Daniel Dunbar | 8dc68ab | 2010-06-16 20:04:22 +0000 | [diff] [blame] | 122 | MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol); |
Daniel Dunbar | 071f73d | 2010-05-10 22:45:09 +0000 | [diff] [blame] | 123 | |
Daniel Dunbar | a86de10 | 2010-06-16 20:04:32 +0000 | [diff] [blame] | 124 | // We have to create a new fragment if this is an atom defining symbol, |
| 125 | // fragments cannot span atoms. |
| 126 | if (getAssembler().isSymbolLinkerVisible(SD.getSymbol())) |
| 127 | new MCDataFragment(getCurrentSectionData()); |
Daniel Dunbar | 071f73d | 2010-05-10 22:45:09 +0000 | [diff] [blame] | 128 | |
Daniel Dunbar | f70f477 | 2010-03-22 20:35:46 +0000 | [diff] [blame] | 129 | // FIXME: This is wasteful, we don't necessarily need to create a data |
| 130 | // fragment. Instead, we should mark the symbol as pointing into the data |
| 131 | // fragment if it exists, otherwise we should just queue the label and set its |
| 132 | // fragment pointer when we emit the next fragment. |
Daniel Dunbar | a86de10 | 2010-06-16 20:04:32 +0000 | [diff] [blame] | 133 | MCDataFragment *F = getOrCreateDataFragment(); |
Daniel Dunbar | f3d2ef0 | 2009-08-22 10:13:24 +0000 | [diff] [blame] | 134 | assert(!SD.getFragment() && "Unexpected fragment on symbol data!"); |
| 135 | SD.setFragment(F); |
| 136 | SD.setOffset(F->getContents().size()); |
Daniel Dunbar | 6aff2fb | 2009-08-24 08:40:12 +0000 | [diff] [blame] | 137 | |
Daniel Dunbar | b18d2dd | 2010-05-17 20:12:31 +0000 | [diff] [blame] | 138 | // This causes the reference type flag to be cleared. Darwin 'as' was "trying" |
| 139 | // to clear the weak reference and weak definition bits too, but the |
| 140 | // implementation was buggy. For now we just try to match 'as', for |
| 141 | // diffability. |
| 142 | // |
| 143 | // FIXME: Cleanup this code, these bits should be emitted based on semantic |
| 144 | // properties, not on the order of definition, etc. |
| 145 | SD.setFlags(SD.getFlags() & ~SF_ReferenceTypeMask); |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 146 | } |
| 147 | |
Chris Lattner | a5ad93a | 2010-01-23 06:39:22 +0000 | [diff] [blame] | 148 | void MCMachOStreamer::EmitAssemblerFlag(MCAssemblerFlag Flag) { |
Daniel Dunbar | 6009db4 | 2009-08-26 21:22:22 +0000 | [diff] [blame] | 149 | switch (Flag) { |
Chris Lattner | a5ad93a | 2010-01-23 06:39:22 +0000 | [diff] [blame] | 150 | case MCAF_SubsectionsViaSymbols: |
Daniel Dunbar | 8dc68ab | 2010-06-16 20:04:22 +0000 | [diff] [blame] | 151 | getAssembler().setSubsectionsViaSymbols(true); |
Daniel Dunbar | 8c3eaf4 | 2009-08-28 07:08:47 +0000 | [diff] [blame] | 152 | return; |
Daniel Dunbar | 6009db4 | 2009-08-26 21:22:22 +0000 | [diff] [blame] | 153 | } |
Daniel Dunbar | 8c3eaf4 | 2009-08-28 07:08:47 +0000 | [diff] [blame] | 154 | |
| 155 | assert(0 && "invalid assembler flag!"); |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 156 | } |
| 157 | |
Daniel Dunbar | 821e333 | 2009-08-31 08:09:28 +0000 | [diff] [blame] | 158 | void MCMachOStreamer::EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) { |
Michael J. Spencer | 8067adc | 2010-07-19 06:13:10 +0000 | [diff] [blame] | 159 | // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into |
| 160 | // MCObjectStreamer. |
Daniel Dunbar | a4d8667 | 2009-10-16 01:58:23 +0000 | [diff] [blame] | 161 | // FIXME: Lift context changes into super class. |
Daniel Dunbar | 8dc68ab | 2010-06-16 20:04:22 +0000 | [diff] [blame] | 162 | getAssembler().getOrCreateSymbolData(*Symbol); |
Daniel Dunbar | 08a408a | 2010-05-05 17:41:00 +0000 | [diff] [blame] | 163 | Symbol->setVariableValue(AddValueSymbols(Value)); |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | void MCMachOStreamer::EmitSymbolAttribute(MCSymbol *Symbol, |
Chris Lattner | a5ad93a | 2010-01-23 06:39:22 +0000 | [diff] [blame] | 167 | MCSymbolAttr Attribute) { |
Daniel Dunbar | 0c7761b | 2009-08-24 11:56:58 +0000 | [diff] [blame] | 168 | // Indirect symbols are handled differently, to match how 'as' handles |
| 169 | // them. This makes writing matching .o files easier. |
Chris Lattner | a5ad93a | 2010-01-23 06:39:22 +0000 | [diff] [blame] | 170 | if (Attribute == MCSA_IndirectSymbol) { |
Daniel Dunbar | ad7c3d5 | 2009-08-26 00:18:21 +0000 | [diff] [blame] | 171 | // Note that we intentionally cannot use the symbol data here; this is |
| 172 | // important for matching the string table that 'as' generates. |
Daniel Dunbar | 0c7761b | 2009-08-24 11:56:58 +0000 | [diff] [blame] | 173 | IndirectSymbolData ISD; |
| 174 | ISD.Symbol = Symbol; |
Daniel Dunbar | 83b4671 | 2010-06-16 20:04:25 +0000 | [diff] [blame] | 175 | ISD.SectionData = getCurrentSectionData(); |
Daniel Dunbar | 8dc68ab | 2010-06-16 20:04:22 +0000 | [diff] [blame] | 176 | getAssembler().getIndirectSymbols().push_back(ISD); |
Daniel Dunbar | 0c7761b | 2009-08-24 11:56:58 +0000 | [diff] [blame] | 177 | return; |
| 178 | } |
| 179 | |
Daniel Dunbar | 6aff2fb | 2009-08-24 08:40:12 +0000 | [diff] [blame] | 180 | // Adding a symbol attribute always introduces the symbol, note that an |
Daniel Dunbar | 46836a7 | 2010-03-10 20:58:29 +0000 | [diff] [blame] | 181 | // important side effect of calling getOrCreateSymbolData here is to register |
| 182 | // the symbol with the assembler. |
Daniel Dunbar | 8dc68ab | 2010-06-16 20:04:22 +0000 | [diff] [blame] | 183 | MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol); |
Daniel Dunbar | 6aff2fb | 2009-08-24 08:40:12 +0000 | [diff] [blame] | 184 | |
| 185 | // The implementation of symbol attributes is designed to match 'as', but it |
| 186 | // leaves much to desired. It doesn't really make sense to arbitrarily add and |
| 187 | // remove flags, but 'as' allows this (in particular, see .desc). |
| 188 | // |
| 189 | // In the future it might be worth trying to make these operations more well |
| 190 | // defined. |
Daniel Dunbar | 3edd9bb | 2009-08-22 11:41:10 +0000 | [diff] [blame] | 191 | switch (Attribute) { |
Chris Lattner | a5ad93a | 2010-01-23 06:39:22 +0000 | [diff] [blame] | 192 | case MCSA_Invalid: |
Chris Lattner | ed0ab15 | 2010-01-25 18:30:45 +0000 | [diff] [blame] | 193 | case MCSA_ELF_TypeFunction: |
| 194 | case MCSA_ELF_TypeIndFunction: |
| 195 | case MCSA_ELF_TypeObject: |
| 196 | case MCSA_ELF_TypeTLS: |
| 197 | case MCSA_ELF_TypeCommon: |
| 198 | case MCSA_ELF_TypeNoType: |
Chris Lattner | a5ad93a | 2010-01-23 06:39:22 +0000 | [diff] [blame] | 199 | case MCSA_IndirectSymbol: |
| 200 | case MCSA_Hidden: |
| 201 | case MCSA_Internal: |
| 202 | case MCSA_Protected: |
| 203 | case MCSA_Weak: |
| 204 | case MCSA_Local: |
Daniel Dunbar | 6aff2fb | 2009-08-24 08:40:12 +0000 | [diff] [blame] | 205 | assert(0 && "Invalid symbol attribute for Mach-O!"); |
| 206 | break; |
Daniel Dunbar | 3edd9bb | 2009-08-22 11:41:10 +0000 | [diff] [blame] | 207 | |
Chris Lattner | a5ad93a | 2010-01-23 06:39:22 +0000 | [diff] [blame] | 208 | case MCSA_Global: |
Daniel Dunbar | 8f4d146 | 2009-08-28 07:08:35 +0000 | [diff] [blame] | 209 | SD.setExternal(true); |
Daniel Dunbar | b18d2dd | 2010-05-17 20:12:31 +0000 | [diff] [blame] | 210 | // This effectively clears the undefined lazy bit, in Darwin 'as', although |
| 211 | // it isn't very consistent because it implements this as part of symbol |
| 212 | // lookup. |
| 213 | // |
| 214 | // FIXME: Cleanup this code, these bits should be emitted based on semantic |
| 215 | // properties, not on the order of definition, etc. |
| 216 | SD.setFlags(SD.getFlags() & ~SF_ReferenceTypeUndefinedLazy); |
Daniel Dunbar | 3edd9bb | 2009-08-22 11:41:10 +0000 | [diff] [blame] | 217 | break; |
Daniel Dunbar | 6aff2fb | 2009-08-24 08:40:12 +0000 | [diff] [blame] | 218 | |
Chris Lattner | a5ad93a | 2010-01-23 06:39:22 +0000 | [diff] [blame] | 219 | case MCSA_LazyReference: |
Daniel Dunbar | 6aff2fb | 2009-08-24 08:40:12 +0000 | [diff] [blame] | 220 | // FIXME: This requires -dynamic. |
| 221 | SD.setFlags(SD.getFlags() | SF_NoDeadStrip); |
| 222 | if (Symbol->isUndefined()) |
| 223 | SD.setFlags(SD.getFlags() | SF_ReferenceTypeUndefinedLazy); |
| 224 | break; |
| 225 | |
Daniel Dunbar | 6aff2fb | 2009-08-24 08:40:12 +0000 | [diff] [blame] | 226 | // Since .reference sets the no dead strip bit, it is equivalent to |
| 227 | // .no_dead_strip in practice. |
Chris Lattner | a5ad93a | 2010-01-23 06:39:22 +0000 | [diff] [blame] | 228 | case MCSA_Reference: |
| 229 | case MCSA_NoDeadStrip: |
Daniel Dunbar | 6aff2fb | 2009-08-24 08:40:12 +0000 | [diff] [blame] | 230 | SD.setFlags(SD.getFlags() | SF_NoDeadStrip); |
| 231 | break; |
| 232 | |
Chris Lattner | a5ad93a | 2010-01-23 06:39:22 +0000 | [diff] [blame] | 233 | case MCSA_PrivateExtern: |
Daniel Dunbar | 6aff2fb | 2009-08-24 08:40:12 +0000 | [diff] [blame] | 234 | SD.setExternal(true); |
| 235 | SD.setPrivateExtern(true); |
| 236 | break; |
| 237 | |
Chris Lattner | a5ad93a | 2010-01-23 06:39:22 +0000 | [diff] [blame] | 238 | case MCSA_WeakReference: |
Daniel Dunbar | 6aff2fb | 2009-08-24 08:40:12 +0000 | [diff] [blame] | 239 | // FIXME: This requires -dynamic. |
| 240 | if (Symbol->isUndefined()) |
| 241 | SD.setFlags(SD.getFlags() | SF_WeakReference); |
| 242 | break; |
| 243 | |
Chris Lattner | a5ad93a | 2010-01-23 06:39:22 +0000 | [diff] [blame] | 244 | case MCSA_WeakDefinition: |
Daniel Dunbar | 6aff2fb | 2009-08-24 08:40:12 +0000 | [diff] [blame] | 245 | // FIXME: 'as' enforces that this is defined and global. The manual claims |
| 246 | // it has to be in a coalesced section, but this isn't enforced. |
| 247 | SD.setFlags(SD.getFlags() | SF_WeakDefinition); |
| 248 | break; |
Kevin Enderby | f59cac5 | 2010-07-08 17:22:42 +0000 | [diff] [blame] | 249 | |
| 250 | case MCSA_WeakDefAutoPrivate: |
| 251 | SD.setFlags(SD.getFlags() | SF_WeakDefinition | SF_WeakReference); |
| 252 | break; |
Daniel Dunbar | 3edd9bb | 2009-08-22 11:41:10 +0000 | [diff] [blame] | 253 | } |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | void MCMachOStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) { |
Daniel Dunbar | 6aff2fb | 2009-08-24 08:40:12 +0000 | [diff] [blame] | 257 | // Encode the 'desc' value into the lowest implementation defined bits. |
Daniel Dunbar | 2ac0c45 | 2010-05-26 20:37:00 +0000 | [diff] [blame] | 258 | assert(DescValue == (DescValue & SF_DescFlagsMask) && |
Daniel Dunbar | 6aff2fb | 2009-08-24 08:40:12 +0000 | [diff] [blame] | 259 | "Invalid .desc value!"); |
Daniel Dunbar | 8dc68ab | 2010-06-16 20:04:22 +0000 | [diff] [blame] | 260 | getAssembler().getOrCreateSymbolData(*Symbol).setFlags( |
| 261 | DescValue & SF_DescFlagsMask); |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 262 | } |
| 263 | |
Chris Lattner | 9eb158d | 2010-01-23 07:47:02 +0000 | [diff] [blame] | 264 | void MCMachOStreamer::EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size, |
Daniel Dunbar | 7092c7e | 2009-08-30 06:17:16 +0000 | [diff] [blame] | 265 | unsigned ByteAlignment) { |
Daniel Dunbar | 8f4d146 | 2009-08-28 07:08:35 +0000 | [diff] [blame] | 266 | // FIXME: Darwin 'as' does appear to allow redef of a .comm by itself. |
| 267 | assert(Symbol->isUndefined() && "Cannot define a symbol twice!"); |
| 268 | |
Daniel Dunbar | 8dc68ab | 2010-06-16 20:04:22 +0000 | [diff] [blame] | 269 | MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol); |
Daniel Dunbar | 8f4d146 | 2009-08-28 07:08:35 +0000 | [diff] [blame] | 270 | SD.setExternal(true); |
Daniel Dunbar | 7092c7e | 2009-08-30 06:17:16 +0000 | [diff] [blame] | 271 | SD.setCommon(Size, ByteAlignment); |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 272 | } |
| 273 | |
Daniel Dunbar | 8751b94 | 2009-08-28 05:48:22 +0000 | [diff] [blame] | 274 | void MCMachOStreamer::EmitZerofill(const MCSection *Section, MCSymbol *Symbol, |
Daniel Dunbar | 7092c7e | 2009-08-30 06:17:16 +0000 | [diff] [blame] | 275 | unsigned Size, unsigned ByteAlignment) { |
Daniel Dunbar | 8dc68ab | 2010-06-16 20:04:22 +0000 | [diff] [blame] | 276 | MCSectionData &SectData = getAssembler().getOrCreateSectionData(*Section); |
Daniel Dunbar | d5a8e98 | 2009-08-28 05:49:21 +0000 | [diff] [blame] | 277 | |
| 278 | // The symbol may not be present, which only creates the section. |
| 279 | if (!Symbol) |
| 280 | return; |
| 281 | |
| 282 | // FIXME: Assert that this section has the zerofill type. |
| 283 | |
| 284 | assert(Symbol->isUndefined() && "Cannot define a symbol twice!"); |
| 285 | |
Daniel Dunbar | 8dc68ab | 2010-06-16 20:04:22 +0000 | [diff] [blame] | 286 | MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol); |
Daniel Dunbar | d5a8e98 | 2009-08-28 05:49:21 +0000 | [diff] [blame] | 287 | |
Daniel Dunbar | e73d49e | 2010-05-12 22:51:27 +0000 | [diff] [blame] | 288 | // Emit an align fragment if necessary. |
| 289 | if (ByteAlignment != 1) |
Daniel Dunbar | 1c15413 | 2010-05-12 22:56:23 +0000 | [diff] [blame] | 290 | new MCAlignFragment(ByteAlignment, 0, 0, ByteAlignment, &SectData); |
Daniel Dunbar | e73d49e | 2010-05-12 22:51:27 +0000 | [diff] [blame] | 291 | |
Daniel Dunbar | 4e54487 | 2010-05-12 22:51:38 +0000 | [diff] [blame] | 292 | MCFragment *F = new MCFillFragment(0, 0, Size, &SectData); |
Daniel Dunbar | d5a8e98 | 2009-08-28 05:49:21 +0000 | [diff] [blame] | 293 | SD.setFragment(F); |
| 294 | |
| 295 | Symbol->setSection(*Section); |
| 296 | |
| 297 | // Update the maximum alignment on the zero fill section if necessary. |
| 298 | if (ByteAlignment > SectData.getAlignment()) |
| 299 | SectData.setAlignment(ByteAlignment); |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 300 | } |
| 301 | |
Eric Christopher | 070c1ab | 2010-05-21 23:03:53 +0000 | [diff] [blame] | 302 | // This should always be called with the thread local bss section. Like the |
| 303 | // .zerofill directive this doesn't actually switch sections on us. |
Eric Christopher | 011c3f1 | 2010-05-18 21:26:41 +0000 | [diff] [blame] | 304 | void MCMachOStreamer::EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol, |
| 305 | uint64_t Size, unsigned ByteAlignment) { |
| 306 | EmitZerofill(Section, Symbol, Size, ByteAlignment); |
| 307 | return; |
Eric Christopher | 482eba0 | 2010-05-14 01:50:28 +0000 | [diff] [blame] | 308 | } |
| 309 | |
Chris Lattner | aaec205 | 2010-01-19 19:46:13 +0000 | [diff] [blame] | 310 | void MCMachOStreamer::EmitBytes(StringRef Data, 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 | getOrCreateDataFragment()->getContents().append(Data.begin(), Data.end()); |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 314 | } |
| 315 | |
Chris Lattner | aaec205 | 2010-01-19 19:46:13 +0000 | [diff] [blame] | 316 | void MCMachOStreamer::EmitValue(const MCExpr *Value, unsigned Size, |
| 317 | unsigned AddrSpace) { |
Michael J. Spencer | 8067adc | 2010-07-19 06:13:10 +0000 | [diff] [blame] | 318 | // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into |
| 319 | // MCObjectStreamer. |
Daniel Dunbar | f70f477 | 2010-03-22 20:35:46 +0000 | [diff] [blame] | 320 | MCDataFragment *DF = getOrCreateDataFragment(); |
Daniel Dunbar | 45f4874 | 2010-02-13 09:28:22 +0000 | [diff] [blame] | 321 | |
| 322 | // Avoid fixups when possible. |
| 323 | int64_t AbsValue; |
Daniel Dunbar | 40ebe247 | 2010-02-22 22:08:57 +0000 | [diff] [blame] | 324 | if (AddValueSymbols(Value)->EvaluateAsAbsolute(AbsValue)) { |
Daniel Dunbar | 45f4874 | 2010-02-13 09:28:22 +0000 | [diff] [blame] | 325 | // FIXME: Endianness assumption. |
| 326 | for (unsigned i = 0; i != Size; ++i) |
| 327 | DF->getContents().push_back(uint8_t(AbsValue >> (i * 8))); |
| 328 | } else { |
Daniel Dunbar | c90e30a | 2010-05-26 15:18:56 +0000 | [diff] [blame] | 329 | DF->addFixup(MCFixup::Create(DF->getContents().size(), |
| 330 | AddValueSymbols(Value), |
| 331 | MCFixup::getKindForSize(Size))); |
Daniel Dunbar | 45f4874 | 2010-02-13 09:28:22 +0000 | [diff] [blame] | 332 | DF->getContents().resize(DF->getContents().size() + Size, 0); |
| 333 | } |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 334 | } |
| 335 | |
| 336 | void MCMachOStreamer::EmitValueToAlignment(unsigned ByteAlignment, |
| 337 | int64_t Value, unsigned ValueSize, |
| 338 | unsigned MaxBytesToEmit) { |
Michael J. Spencer | 8067adc | 2010-07-19 06:13:10 +0000 | [diff] [blame] | 339 | // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into |
| 340 | // MCObjectStreamer. |
Daniel Dunbar | d6f761e | 2009-08-21 23:07:38 +0000 | [diff] [blame] | 341 | if (MaxBytesToEmit == 0) |
| 342 | MaxBytesToEmit = ByteAlignment; |
Daniel Dunbar | a86de10 | 2010-06-16 20:04:32 +0000 | [diff] [blame] | 343 | new MCAlignFragment(ByteAlignment, Value, ValueSize, MaxBytesToEmit, |
| 344 | getCurrentSectionData()); |
Kevin Enderby | 6e72048 | 2010-02-23 18:26:34 +0000 | [diff] [blame] | 345 | |
| 346 | // Update the maximum alignment on the current section if necessary. |
Daniel Dunbar | 83b4671 | 2010-06-16 20:04:25 +0000 | [diff] [blame] | 347 | if (ByteAlignment > getCurrentSectionData()->getAlignment()) |
| 348 | getCurrentSectionData()->setAlignment(ByteAlignment); |
Kevin Enderby | 6e72048 | 2010-02-23 18:26:34 +0000 | [diff] [blame] | 349 | } |
| 350 | |
| 351 | void MCMachOStreamer::EmitCodeAlignment(unsigned ByteAlignment, |
| 352 | unsigned MaxBytesToEmit) { |
Michael J. Spencer | 8067adc | 2010-07-19 06:13:10 +0000 | [diff] [blame] | 353 | // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into |
| 354 | // MCObjectStreamer. |
Kevin Enderby | 6e72048 | 2010-02-23 18:26:34 +0000 | [diff] [blame] | 355 | if (MaxBytesToEmit == 0) |
| 356 | MaxBytesToEmit = ByteAlignment; |
Daniel Dunbar | 1c15413 | 2010-05-12 22:56:23 +0000 | [diff] [blame] | 357 | MCAlignFragment *F = new MCAlignFragment(ByteAlignment, 0, 1, MaxBytesToEmit, |
Daniel Dunbar | 83b4671 | 2010-06-16 20:04:25 +0000 | [diff] [blame] | 358 | getCurrentSectionData()); |
Daniel Dunbar | 1c15413 | 2010-05-12 22:56:23 +0000 | [diff] [blame] | 359 | F->setEmitNops(true); |
Daniel Dunbar | 0705fbf | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 360 | |
Daniel Dunbar | f3d2ef0 | 2009-08-22 10:13:24 +0000 | [diff] [blame] | 361 | // Update the maximum alignment on the current section if necessary. |
Daniel Dunbar | 83b4671 | 2010-06-16 20:04:25 +0000 | [diff] [blame] | 362 | if (ByteAlignment > getCurrentSectionData()->getAlignment()) |
| 363 | getCurrentSectionData()->setAlignment(ByteAlignment); |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 364 | } |
| 365 | |
Daniel Dunbar | 821e333 | 2009-08-31 08:09:28 +0000 | [diff] [blame] | 366 | void MCMachOStreamer::EmitValueToOffset(const MCExpr *Offset, |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 367 | unsigned char Value) { |
Daniel Dunbar | a86de10 | 2010-06-16 20:04:32 +0000 | [diff] [blame] | 368 | new MCOrgFragment(*Offset, Value, getCurrentSectionData()); |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 369 | } |
| 370 | |
Daniel Dunbar | 2ac0c45 | 2010-05-26 20:37:00 +0000 | [diff] [blame] | 371 | void MCMachOStreamer::EmitInstToFragment(const MCInst &Inst) { |
Daniel Dunbar | 83b4671 | 2010-06-16 20:04:25 +0000 | [diff] [blame] | 372 | MCInstFragment *IF = new MCInstFragment(Inst, getCurrentSectionData()); |
Daniel Dunbar | 2ac0c45 | 2010-05-26 20:37:00 +0000 | [diff] [blame] | 373 | |
| 374 | // Add the fixups and data. |
| 375 | // |
| 376 | // FIXME: Revisit this design decision when relaxation is done, we may be |
| 377 | // able to get away with not storing any extra data in the MCInst. |
| 378 | SmallVector<MCFixup, 4> Fixups; |
| 379 | SmallString<256> Code; |
| 380 | raw_svector_ostream VecOS(Code); |
Daniel Dunbar | 8dc68ab | 2010-06-16 20:04:22 +0000 | [diff] [blame] | 381 | getAssembler().getEmitter().EncodeInstruction(Inst, VecOS, Fixups); |
Daniel Dunbar | 2ac0c45 | 2010-05-26 20:37:00 +0000 | [diff] [blame] | 382 | VecOS.flush(); |
| 383 | |
| 384 | IF->getCode() = Code; |
| 385 | IF->getFixups() = Fixups; |
| 386 | } |
| 387 | |
| 388 | void MCMachOStreamer::EmitInstToData(const MCInst &Inst) { |
| 389 | MCDataFragment *DF = getOrCreateDataFragment(); |
| 390 | |
| 391 | SmallVector<MCFixup, 4> Fixups; |
| 392 | SmallString<256> Code; |
| 393 | raw_svector_ostream VecOS(Code); |
Daniel Dunbar | 8dc68ab | 2010-06-16 20:04:22 +0000 | [diff] [blame] | 394 | getAssembler().getEmitter().EncodeInstruction(Inst, VecOS, Fixups); |
Daniel Dunbar | 2ac0c45 | 2010-05-26 20:37:00 +0000 | [diff] [blame] | 395 | VecOS.flush(); |
| 396 | |
| 397 | // Add the fixups and data. |
| 398 | for (unsigned i = 0, e = Fixups.size(); i != e; ++i) { |
| 399 | Fixups[i].setOffset(Fixups[i].getOffset() + DF->getContents().size()); |
| 400 | DF->addFixup(Fixups[i]); |
| 401 | } |
| 402 | DF->getContents().append(Code.begin(), Code.end()); |
| 403 | } |
| 404 | |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 405 | void MCMachOStreamer::EmitInstruction(const MCInst &Inst) { |
Daniel Dunbar | 4fac749 | 2009-08-27 08:17:51 +0000 | [diff] [blame] | 406 | // Scan for values. |
Daniel Dunbar | db9014d | 2010-05-17 21:19:59 +0000 | [diff] [blame] | 407 | for (unsigned i = Inst.getNumOperands(); i--; ) |
Daniel Dunbar | 8c2eebe | 2009-08-31 08:08:38 +0000 | [diff] [blame] | 408 | if (Inst.getOperand(i).isExpr()) |
| 409 | AddValueSymbols(Inst.getOperand(i).getExpr()); |
Daniel Dunbar | 4fac749 | 2009-08-27 08:17:51 +0000 | [diff] [blame] | 410 | |
Daniel Dunbar | 83b4671 | 2010-06-16 20:04:25 +0000 | [diff] [blame] | 411 | getCurrentSectionData()->setHasInstructions(true); |
Daniel Dunbar | e1ec617 | 2010-02-02 21:44:01 +0000 | [diff] [blame] | 412 | |
Kevin Enderby | 232ab94 | 2010-08-31 22:55:11 +0000 | [diff] [blame] | 413 | // Now that a machine instruction has been assembled into this section, make |
| 414 | // a line entry for any .loc directive that has been seen. |
| 415 | MakeLineEntryForSection(getCurrentSection()); |
| 416 | |
Daniel Dunbar | 83194de | 2010-05-26 20:37:03 +0000 | [diff] [blame] | 417 | // If this instruction doesn't need relaxation, just emit it as data. |
Daniel Dunbar | 8dc68ab | 2010-06-16 20:04:22 +0000 | [diff] [blame] | 418 | if (!getAssembler().getBackend().MayNeedRelaxation(Inst)) { |
Daniel Dunbar | 2ac0c45 | 2010-05-26 20:37:00 +0000 | [diff] [blame] | 419 | EmitInstToData(Inst); |
Daniel Dunbar | 83194de | 2010-05-26 20:37:03 +0000 | [diff] [blame] | 420 | return; |
| 421 | } |
| 422 | |
| 423 | // Otherwise, if we are relaxing everything, relax the instruction as much as |
| 424 | // possible and emit it as data. |
Daniel Dunbar | 8dc68ab | 2010-06-16 20:04:22 +0000 | [diff] [blame] | 425 | if (getAssembler().getRelaxAll()) { |
Daniel Dunbar | 83194de | 2010-05-26 20:37:03 +0000 | [diff] [blame] | 426 | MCInst Relaxed; |
Daniel Dunbar | 8dc68ab | 2010-06-16 20:04:22 +0000 | [diff] [blame] | 427 | getAssembler().getBackend().RelaxInstruction(Inst, Relaxed); |
| 428 | while (getAssembler().getBackend().MayNeedRelaxation(Relaxed)) |
| 429 | getAssembler().getBackend().RelaxInstruction(Relaxed, Relaxed); |
Daniel Dunbar | 83194de | 2010-05-26 20:37:03 +0000 | [diff] [blame] | 430 | EmitInstToData(Relaxed); |
| 431 | return; |
| 432 | } |
| 433 | |
| 434 | // Otherwise emit to a separate fragment. |
| 435 | EmitInstToFragment(Inst); |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 436 | } |
| 437 | |
Kevin Enderby | b07ce60 | 2010-08-09 22:52:14 +0000 | [diff] [blame] | 438 | // |
Kevin Enderby | 232ab94 | 2010-08-31 22:55:11 +0000 | [diff] [blame] | 439 | // This is called when an instruction is assembled into the specified section |
| 440 | // and if there is information from the last .loc directive that has yet to have |
| 441 | // a line entry made for it is made. |
| 442 | // |
| 443 | void MCMachOStreamer::MakeLineEntryForSection(const MCSection *Section) { |
| 444 | if (!getContext().getDwarfLocSeen()) |
| 445 | return; |
| 446 | |
| 447 | // Create a symbol at in the current section for use in the line entry. |
| 448 | MCSymbol *LineSym = getContext().CreateTempSymbol(); |
| 449 | // Set the value of the symbol to use for the MCLineEntry. |
| 450 | EmitLabel(LineSym); |
| 451 | |
| 452 | // Get the current .loc info saved in the context. |
| 453 | const MCDwarfLoc &DwarfLoc = getContext().getCurrentDwarfLoc(); |
| 454 | |
| 455 | // Create a (local) line entry with the symbol and the current .loc info. |
| 456 | MCLineEntry LineEntry(LineSym, DwarfLoc); |
| 457 | |
| 458 | // clear DwarfLocSeen saying the current .loc info is now used. |
| 459 | getContext().clearDwarfLocSeen(); |
| 460 | |
| 461 | // Get the MCLineSection for this section, if one does not exist for this |
| 462 | // section create it. |
| 463 | DenseMap<const MCSection *, MCLineSection *> &MCLineSections = |
| 464 | getContext().getMCLineSections(); |
| 465 | MCLineSection *LineSection = MCLineSections[Section]; |
| 466 | if (!LineSection) { |
| 467 | // Create a new MCLineSection. This will be deleted after the dwarf line |
| 468 | // table is created using it by iterating through the MCLineSections |
| 469 | // DenseMap. |
| 470 | LineSection = new MCLineSection; |
| 471 | // Save a pointer to the new LineSection into the MCLineSections DenseMap. |
| 472 | MCLineSections[Section] = LineSection; |
| 473 | } |
| 474 | |
| 475 | // Add the line entry to this section's entries. |
| 476 | LineSection->addLineEntry(LineEntry); |
| 477 | } |
| 478 | |
| 479 | // |
Kevin Enderby | b07ce60 | 2010-08-09 22:52:14 +0000 | [diff] [blame] | 480 | // This helper routine returns an expression of End - Start + IntVal for use |
| 481 | // by EmitDwarfFileTable() below. |
| 482 | // |
| 483 | const MCExpr * MCMachOStreamer::MakeStartMinusEndExpr(MCSymbol *Start, |
| 484 | MCSymbol *End, |
| 485 | int IntVal) { |
| 486 | MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None; |
| 487 | const MCExpr *Res = |
| 488 | MCSymbolRefExpr::Create(End, Variant, getContext()); |
| 489 | const MCExpr *RHS = |
| 490 | MCSymbolRefExpr::Create(Start, Variant, getContext()); |
| 491 | const MCExpr *Res1 = |
| 492 | MCBinaryExpr::Create(MCBinaryExpr::Sub, Res, RHS,getContext()); |
| 493 | const MCExpr *Res2 = |
| 494 | MCConstantExpr::Create(IntVal, getContext()); |
| 495 | const MCExpr *Res3 = |
| 496 | MCBinaryExpr::Create(MCBinaryExpr::Sub, Res1, Res2, getContext()); |
| 497 | return Res3; |
| 498 | } |
| 499 | |
| 500 | // |
| 501 | // This emits the Dwarf file (and eventually the line) table. |
| 502 | // |
| 503 | void MCMachOStreamer::EmitDwarfFileTable(void) { |
| 504 | // For now make sure we don't put out the Dwarf file table if no .file |
| 505 | // directives were seen. |
| 506 | const std::vector<MCDwarfFile *> &MCDwarfFiles = |
| 507 | getContext().getMCDwarfFiles(); |
| 508 | if (MCDwarfFiles.size() == 0) |
| 509 | return; |
| 510 | |
| 511 | // This is the Mach-O section, for ELF it is the .debug_line section. |
| 512 | SwitchSection(getContext().getMachOSection("__DWARF", "__debug_line", |
| 513 | MCSectionMachO::S_ATTR_DEBUG, |
| 514 | 0, SectionKind::getDataRelLocal())); |
| 515 | |
| 516 | // Create a symbol at the beginning of this section. |
| 517 | MCSymbol *LineStartSym = getContext().CreateTempSymbol(); |
| 518 | // Set the value of the symbol, as we are at the start of the section. |
| 519 | EmitLabel(LineStartSym); |
| 520 | |
| 521 | // Create a symbol for the end of the section (to be set when we get there). |
| 522 | MCSymbol *LineEndSym = getContext().CreateTempSymbol(); |
| 523 | |
| 524 | // The first 4 bytes is the total length of the information for this |
| 525 | // compilation unit (not including these 4 bytes for the length). |
| 526 | EmitValue(MakeStartMinusEndExpr(LineStartSym, LineEndSym, 4), 4, 0); |
| 527 | |
| 528 | // Next 2 bytes is the Version, which is Dwarf 2. |
| 529 | EmitIntValue(2, 2); |
| 530 | |
| 531 | // Create a symbol for the end of the prologue (to be set when we get there). |
| 532 | MCSymbol *ProEndSym = getContext().CreateTempSymbol(); // Lprologue_end |
| 533 | |
| 534 | // Length of the prologue, is the next 4 bytes. Which is the start of the |
| 535 | // section to the end of the prologue. Not including the 4 bytes for the |
| 536 | // total length, the 2 bytes for the version, and these 4 bytes for the |
| 537 | // length of the prologue. |
| 538 | EmitValue(MakeStartMinusEndExpr(LineStartSym, ProEndSym, (4 + 2 + 4)), 4, 0); |
| 539 | |
| 540 | // Parameters of the state machine, are next. |
| 541 | // Define the architecture-dependent minimum instruction length (in |
| 542 | // bytes). This value should be rather too small than too big. */ |
| 543 | // DWARF2_LINE_MIN_INSN_LENGTH |
| 544 | EmitIntValue(1, 1); |
| 545 | // Flag that indicates the initial value of the is_stmt_start flag. |
| 546 | // DWARF2_LINE_DEFAULT_IS_STMT |
| 547 | EmitIntValue(1, 1); |
| 548 | // Minimum line offset in a special line info. opcode. This value |
| 549 | // was chosen to give a reasonable range of values. */ |
| 550 | // DWARF2_LINE_BASE |
Bill Wendling | 7d49f2b | 2010-09-03 19:09:46 +0000 | [diff] [blame^] | 551 | EmitIntValue(uint64_t(-5), 1); |
Kevin Enderby | b07ce60 | 2010-08-09 22:52:14 +0000 | [diff] [blame] | 552 | // Range of line offsets in a special line info. opcode. |
| 553 | // DWARF2_LINE_RANGE |
| 554 | EmitIntValue(14, 1); |
| 555 | // First special line opcode - leave room for the standard opcodes. |
| 556 | // DWARF2_LINE_OPCODE_BASE |
| 557 | EmitIntValue(13, 1); |
| 558 | |
| 559 | // Standard opcode lengths |
| 560 | EmitIntValue(0, 1); // length of DW_LNS_copy |
| 561 | EmitIntValue(1, 1); // length of DW_LNS_advance_pc |
| 562 | EmitIntValue(1, 1); // length of DW_LNS_advance_line |
| 563 | EmitIntValue(1, 1); // length of DW_LNS_set_file |
| 564 | EmitIntValue(1, 1); // length of DW_LNS_set_column |
| 565 | EmitIntValue(0, 1); // length of DW_LNS_negate_stmt |
| 566 | EmitIntValue(0, 1); // length of DW_LNS_set_basic_block |
| 567 | EmitIntValue(0, 1); // length of DW_LNS_const_add_pc |
| 568 | EmitIntValue(1, 1); // length of DW_LNS_fixed_advance_pc |
| 569 | EmitIntValue(0, 1); // length of DW_LNS_set_prologue_end |
| 570 | EmitIntValue(0, 1); // length of DW_LNS_set_epilogue_begin |
| 571 | EmitIntValue(1, 1); // DW_LNS_set_isa |
| 572 | |
| 573 | // Put out the directory and file tables. |
| 574 | |
| 575 | // First the directory table. |
| 576 | const std::vector<StringRef> &MCDwarfDirs = |
| 577 | getContext().getMCDwarfDirs(); |
| 578 | for (unsigned i = 0; i < MCDwarfDirs.size(); i++) { |
| 579 | EmitBytes(MCDwarfDirs[i], 0); // the DirectoryName |
| 580 | EmitBytes(StringRef("\0", 1), 0); // the null termination of the string |
| 581 | } |
| 582 | EmitIntValue(0, 1); // Terminate the directory list |
| 583 | |
| 584 | // Second the file table. |
| 585 | for (unsigned i = 1; i < MCDwarfFiles.size(); i++) { |
| 586 | EmitBytes(MCDwarfFiles[i]->getName(), 0); // FileName |
| 587 | EmitBytes(StringRef("\0", 1), 0); // the null termination of the string |
| 588 | // FIXME the Directory number should be a .uleb128 not a .byte |
| 589 | EmitIntValue(MCDwarfFiles[i]->getDirIndex(), 1); |
| 590 | EmitIntValue(0, 1); // last modification timestamp (always 0) |
| 591 | EmitIntValue(0, 1); // filesize (always 0) |
| 592 | } |
| 593 | EmitIntValue(0, 1); // Terminate the file list |
| 594 | |
| 595 | // This is the end of the prologue, so set the value of the symbol at the |
| 596 | // end of the prologue (that was used in a previous expression). |
| 597 | EmitLabel(ProEndSym); |
| 598 | |
| 599 | // TODO: This is the point where the line tables would be emitted. |
| 600 | |
Kevin Enderby | 232ab94 | 2010-08-31 22:55:11 +0000 | [diff] [blame] | 601 | // Delete the MCLineSections that were created in |
| 602 | // MCMachOStreamer::MakeLineEntryForSection() and used to emit the line |
| 603 | // tables. |
| 604 | DenseMap<const MCSection *, MCLineSection *> &MCLineSections = |
| 605 | getContext().getMCLineSections(); |
| 606 | for (DenseMap<const MCSection *, MCLineSection *>::iterator it = |
| 607 | MCLineSections.begin(), ie = MCLineSections.end(); it != ie; ++it) { |
| 608 | delete it->second; |
| 609 | } |
| 610 | |
Kevin Enderby | b07ce60 | 2010-08-09 22:52:14 +0000 | [diff] [blame] | 611 | // If there are no line tables emited then we emit: |
| 612 | // The following DW_LNE_set_address sequence to set the address to zero |
| 613 | // TODO test for 32-bit or 64-bit output |
| 614 | // This is the sequence for 32-bit code |
| 615 | EmitIntValue(0, 1); |
| 616 | EmitIntValue(5, 1); |
| 617 | EmitIntValue(2, 1); |
| 618 | EmitIntValue(0, 1); |
| 619 | EmitIntValue(0, 1); |
| 620 | EmitIntValue(0, 1); |
| 621 | EmitIntValue(0, 1); |
| 622 | |
| 623 | // Lastly emit the DW_LNE_end_sequence which consists of 3 bytes '00 01 01' |
| 624 | // (00 is the code for extended opcodes, followed by a ULEB128 length of the |
| 625 | // extended opcode (01), and the DW_LNE_end_sequence (01). |
| 626 | EmitIntValue(0, 1); // DW_LNS_extended_op |
| 627 | EmitIntValue(1, 1); // ULEB128 length of the extended opcode |
| 628 | EmitIntValue(1, 1); // DW_LNE_end_sequence |
| 629 | |
| 630 | // This is the end of the section, so set the value of the symbol at the end |
| 631 | // of this section (that was used in a previous expression). |
| 632 | EmitLabel(LineEndSym); |
| 633 | } |
| 634 | |
Daniel Dunbar | a86de10 | 2010-06-16 20:04:32 +0000 | [diff] [blame] | 635 | void MCMachOStreamer::Finish() { |
Kevin Enderby | b07ce60 | 2010-08-09 22:52:14 +0000 | [diff] [blame] | 636 | // Dump out the dwarf file and directory tables (soon to include line table) |
| 637 | EmitDwarfFileTable(); |
| 638 | |
Daniel Dunbar | a86de10 | 2010-06-16 20:04:32 +0000 | [diff] [blame] | 639 | // We have to set the fragment atom associations so we can relax properly for |
| 640 | // Mach-O. |
| 641 | |
| 642 | // First, scan the symbol table to build a lookup table from fragments to |
| 643 | // defining symbols. |
| 644 | DenseMap<const MCFragment*, MCSymbolData*> DefiningSymbolMap; |
| 645 | for (MCAssembler::symbol_iterator it = getAssembler().symbol_begin(), |
| 646 | ie = getAssembler().symbol_end(); it != ie; ++it) { |
| 647 | if (getAssembler().isSymbolLinkerVisible(it->getSymbol()) && |
| 648 | it->getFragment()) { |
| 649 | // An atom defining symbol should never be internal to a fragment. |
| 650 | assert(it->getOffset() == 0 && "Invalid offset in atom defining symbol!"); |
| 651 | DefiningSymbolMap[it->getFragment()] = it; |
| 652 | } |
| 653 | } |
| 654 | |
| 655 | // Set the fragment atom associations by tracking the last seen atom defining |
| 656 | // symbol. |
| 657 | for (MCAssembler::iterator it = getAssembler().begin(), |
| 658 | ie = getAssembler().end(); it != ie; ++it) { |
| 659 | MCSymbolData *CurrentAtom = 0; |
| 660 | for (MCSectionData::iterator it2 = it->begin(), |
| 661 | ie2 = it->end(); it2 != ie2; ++it2) { |
| 662 | if (MCSymbolData *SD = DefiningSymbolMap.lookup(it2)) |
| 663 | CurrentAtom = SD; |
| 664 | it2->setAtom(CurrentAtom); |
| 665 | } |
| 666 | } |
| 667 | |
| 668 | this->MCObjectStreamer::Finish(); |
| 669 | } |
| 670 | |
Daniel Dunbar | 1f3e445 | 2010-03-11 01:34:27 +0000 | [diff] [blame] | 671 | MCStreamer *llvm::createMachOStreamer(MCContext &Context, TargetAsmBackend &TAB, |
Daniel Dunbar | ac2884a | 2010-03-25 22:49:09 +0000 | [diff] [blame] | 672 | raw_ostream &OS, MCCodeEmitter *CE, |
| 673 | bool RelaxAll) { |
| 674 | MCMachOStreamer *S = new MCMachOStreamer(Context, TAB, OS, CE); |
| 675 | if (RelaxAll) |
| 676 | S->getAssembler().setRelaxAll(true); |
| 677 | return S; |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 678 | } |