Bill Wendling | b1e2183 | 2013-09-04 22:35:41 +0000 | [diff] [blame] | 1 | //===-- MCMachOStreamer.cpp - MachO Streamer ------------------------------===// |
Daniel Dunbar | 3016db3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 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" |
Tim Northover | c3988b4 | 2014-03-29 07:05:06 +0000 | [diff] [blame] | 11 | #include "llvm/ADT/DenseMap.h" |
| 12 | #include "llvm/ADT/SmallVector.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 13 | #include "llvm/MC/MCAsmBackend.h" |
Daniel Dunbar | 3016db3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 14 | #include "llvm/MC/MCAssembler.h" |
Daniel Dunbar | c7c5f9f | 2009-08-27 08:17:51 +0000 | [diff] [blame] | 15 | #include "llvm/MC/MCCodeEmitter.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 16 | #include "llvm/MC/MCContext.h" |
| 17 | #include "llvm/MC/MCDwarf.h" |
Daniel Dunbar | 73da11e | 2009-08-31 08:08:38 +0000 | [diff] [blame] | 18 | #include "llvm/MC/MCExpr.h" |
Daniel Dunbar | c7c5f9f | 2009-08-27 08:17:51 +0000 | [diff] [blame] | 19 | #include "llvm/MC/MCInst.h" |
Tim Northover | 53d3251 | 2014-03-29 07:34:53 +0000 | [diff] [blame] | 20 | #include "llvm/MC/MCLinkerOptimizationHint.h" |
Rafael Espindola | e308c0c | 2014-01-23 22:49:25 +0000 | [diff] [blame] | 21 | #include "llvm/MC/MCObjectFileInfo.h" |
Daniel Dunbar | 8a3c9d9 | 2010-06-16 20:04:22 +0000 | [diff] [blame] | 22 | #include "llvm/MC/MCObjectStreamer.h" |
Daniel Dunbar | 3016db3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 23 | #include "llvm/MC/MCSection.h" |
Kevin Enderby | 7221b76 | 2010-08-09 22:52:14 +0000 | [diff] [blame] | 24 | #include "llvm/MC/MCSectionMachO.h" |
Pete Cooper | 916f79e | 2015-06-08 17:17:28 +0000 | [diff] [blame] | 25 | #include "llvm/MC/MCSymbolMachO.h" |
Kevin Enderby | e46564a | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 26 | #include "llvm/Support/Dwarf.h" |
Daniel Dunbar | 3016db3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 27 | #include "llvm/Support/ErrorHandling.h" |
Rafael Espindola | cd584a8 | 2015-03-19 01:50:16 +0000 | [diff] [blame] | 28 | #include "llvm/Support/TargetRegistry.h" |
Daniel Dunbar | c7c5f9f | 2009-08-27 08:17:51 +0000 | [diff] [blame] | 29 | #include "llvm/Support/raw_ostream.h" |
Daniel Dunbar | de04b3f | 2010-03-23 05:09:03 +0000 | [diff] [blame] | 30 | |
Daniel Dunbar | 3016db3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 31 | using namespace llvm; |
| 32 | |
| 33 | namespace { |
| 34 | |
Daniel Dunbar | 8a3c9d9 | 2010-06-16 20:04:22 +0000 | [diff] [blame] | 35 | class MCMachOStreamer : public MCObjectStreamer { |
Daniel Dunbar | 04a1158 | 2009-08-24 08:40:12 +0000 | [diff] [blame] | 36 | private: |
Tim Northover | c3988b4 | 2014-03-29 07:05:06 +0000 | [diff] [blame] | 37 | /// LabelSections - true if each section change should emit a linker local |
| 38 | /// label for use in relocations for assembler local references. Obviates the |
| 39 | /// need for local relocations. False by default. |
| 40 | bool LabelSections; |
| 41 | |
Rafael Espindola | 36a15cb | 2015-03-20 20:00:01 +0000 | [diff] [blame] | 42 | bool DWARFMustBeAtTheEnd; |
| 43 | bool CreatedADWARFSection; |
| 44 | |
Tim Northover | c3988b4 | 2014-03-29 07:05:06 +0000 | [diff] [blame] | 45 | /// HasSectionLabel - map of which sections have already had a non-local |
| 46 | /// label emitted to them. Used so we don't emit extraneous linker local |
| 47 | /// labels in the middle of the section. |
| 48 | DenseMap<const MCSection*, bool> HasSectionLabel; |
| 49 | |
Craig Topper | 34a61bc | 2014-03-08 07:02:02 +0000 | [diff] [blame] | 50 | void EmitInstToData(const MCInst &Inst, const MCSubtargetInfo &STI) override; |
Daniel Dunbar | 9d40ef1 | 2010-05-26 20:37:00 +0000 | [diff] [blame] | 51 | |
Jim Grosbach | 4b63d2a | 2012-05-18 19:12:01 +0000 | [diff] [blame] | 52 | void EmitDataRegion(DataRegionData::KindTy Kind); |
| 53 | void EmitDataRegionEnd(); |
Tim Northover | c3988b4 | 2014-03-29 07:05:06 +0000 | [diff] [blame] | 54 | |
Daniel Dunbar | 3016db3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 55 | public: |
Rafael Espindola | 5560a4c | 2015-04-14 22:14:34 +0000 | [diff] [blame] | 56 | MCMachOStreamer(MCContext &Context, MCAsmBackend &MAB, raw_pwrite_stream &OS, |
Rafael Espindola | 36a15cb | 2015-03-20 20:00:01 +0000 | [diff] [blame] | 57 | MCCodeEmitter *Emitter, bool DWARFMustBeAtTheEnd, bool label) |
| 58 | : MCObjectStreamer(Context, MAB, OS, Emitter), LabelSections(label), |
| 59 | DWARFMustBeAtTheEnd(DWARFMustBeAtTheEnd), CreatedADWARFSection(false) {} |
Daniel Dunbar | d821f4a | 2010-03-25 22:49:09 +0000 | [diff] [blame] | 60 | |
Yaron Keren | 559b47d | 2014-09-17 09:25:36 +0000 | [diff] [blame] | 61 | /// state management |
| 62 | void reset() override { |
| 63 | HasSectionLabel.clear(); |
| 64 | MCObjectStreamer::reset(); |
| 65 | } |
| 66 | |
Daniel Dunbar | 3016db3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 67 | /// @name MCStreamer Interface |
| 68 | /// @{ |
| 69 | |
Rafael Espindola | 0709a7b | 2015-05-21 19:20:38 +0000 | [diff] [blame] | 70 | void ChangeSection(MCSection *Sect, const MCExpr *Subsect) override; |
Craig Topper | 34a61bc | 2014-03-08 07:02:02 +0000 | [diff] [blame] | 71 | void EmitLabel(MCSymbol *Symbol) override; |
Craig Topper | 34a61bc | 2014-03-08 07:02:02 +0000 | [diff] [blame] | 72 | void EmitEHSymAttributes(const MCSymbol *Symbol, MCSymbol *EHSymbol) override; |
| 73 | void EmitAssemblerFlag(MCAssemblerFlag Flag) override; |
| 74 | void EmitLinkerOptions(ArrayRef<std::string> Options) override; |
| 75 | void EmitDataRegion(MCDataRegionType Kind) override; |
Jim Grosbach | 448334a | 2014-03-18 22:09:05 +0000 | [diff] [blame] | 76 | void EmitVersionMin(MCVersionMinType Kind, unsigned Major, |
| 77 | unsigned Minor, unsigned Update) override; |
Craig Topper | 34a61bc | 2014-03-08 07:02:02 +0000 | [diff] [blame] | 78 | void EmitThumbFunc(MCSymbol *Func) override; |
| 79 | bool EmitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute) override; |
| 80 | void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) override; |
| 81 | void EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size, |
| 82 | unsigned ByteAlignment) override; |
| 83 | void BeginCOFFSymbolDef(const MCSymbol *Symbol) override { |
Craig Topper | a2886c2 | 2012-02-07 05:05:23 +0000 | [diff] [blame] | 84 | llvm_unreachable("macho doesn't support this directive"); |
Chris Lattner | 72afa95 | 2010-05-08 19:54:22 +0000 | [diff] [blame] | 85 | } |
Craig Topper | 34a61bc | 2014-03-08 07:02:02 +0000 | [diff] [blame] | 86 | void EmitCOFFSymbolStorageClass(int StorageClass) override { |
Craig Topper | a2886c2 | 2012-02-07 05:05:23 +0000 | [diff] [blame] | 87 | llvm_unreachable("macho doesn't support this directive"); |
Chris Lattner | 72afa95 | 2010-05-08 19:54:22 +0000 | [diff] [blame] | 88 | } |
Craig Topper | 34a61bc | 2014-03-08 07:02:02 +0000 | [diff] [blame] | 89 | void EmitCOFFSymbolType(int Type) override { |
Craig Topper | a2886c2 | 2012-02-07 05:05:23 +0000 | [diff] [blame] | 90 | llvm_unreachable("macho doesn't support this directive"); |
Chris Lattner | 72afa95 | 2010-05-08 19:54:22 +0000 | [diff] [blame] | 91 | } |
Craig Topper | 34a61bc | 2014-03-08 07:02:02 +0000 | [diff] [blame] | 92 | void EndCOFFSymbolDef() override { |
Craig Topper | a2886c2 | 2012-02-07 05:05:23 +0000 | [diff] [blame] | 93 | llvm_unreachable("macho doesn't support this directive"); |
Chris Lattner | 72afa95 | 2010-05-08 19:54:22 +0000 | [diff] [blame] | 94 | } |
Craig Topper | 34a61bc | 2014-03-08 07:02:02 +0000 | [diff] [blame] | 95 | void EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size, |
| 96 | unsigned ByteAlignment) override; |
Rafael Espindola | 0709a7b | 2015-05-21 19:20:38 +0000 | [diff] [blame] | 97 | void EmitZerofill(MCSection *Section, MCSymbol *Symbol = nullptr, |
Craig Topper | 34a61bc | 2014-03-08 07:02:02 +0000 | [diff] [blame] | 98 | uint64_t Size = 0, unsigned ByteAlignment = 0) override; |
Rafael Espindola | 0709a7b | 2015-05-21 19:20:38 +0000 | [diff] [blame] | 99 | void EmitTBSSSymbol(MCSection *Section, MCSymbol *Symbol, uint64_t Size, |
Benjamin Kramer | 8c90fd7 | 2014-09-03 11:41:21 +0000 | [diff] [blame] | 100 | unsigned ByteAlignment = 0) override; |
Daniel Dunbar | 9d40ef1 | 2010-05-26 20:37:00 +0000 | [diff] [blame] | 101 | |
Craig Topper | 34a61bc | 2014-03-08 07:02:02 +0000 | [diff] [blame] | 102 | void EmitFileDirective(StringRef Filename) override { |
Daniel Dunbar | 6b4391a | 2010-07-19 20:44:20 +0000 | [diff] [blame] | 103 | // FIXME: Just ignore the .file; it isn't important enough to fail the |
| 104 | // entire assembly. |
| 105 | |
Rafael Espindola | 5645bad | 2013-10-16 01:05:45 +0000 | [diff] [blame] | 106 | // report_fatal_error("unsupported directive: '.file'"); |
| 107 | } |
| 108 | |
Craig Topper | 34a61bc | 2014-03-08 07:02:02 +0000 | [diff] [blame] | 109 | void EmitIdent(StringRef IdentString) override { |
Rafael Espindola | 5645bad | 2013-10-16 01:05:45 +0000 | [diff] [blame] | 110 | llvm_unreachable("macho doesn't support this directive"); |
Chris Lattner | 601ef33 | 2010-01-25 18:58:59 +0000 | [diff] [blame] | 111 | } |
Daniel Dunbar | 9d40ef1 | 2010-05-26 20:37:00 +0000 | [diff] [blame] | 112 | |
Tim Northover | 53d3251 | 2014-03-29 07:34:53 +0000 | [diff] [blame] | 113 | void EmitLOHDirective(MCLOHType Kind, const MCLOHArgs &Args) override { |
| 114 | getAssembler().getLOHContainer().addDirective(Kind, Args); |
| 115 | } |
| 116 | |
Craig Topper | 34a61bc | 2014-03-08 07:02:02 +0000 | [diff] [blame] | 117 | void FinishImpl() override; |
Daniel Dunbar | 3016db3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 118 | }; |
| 119 | |
| 120 | } // end anonymous namespace. |
| 121 | |
Rafael Espindola | 36a15cb | 2015-03-20 20:00:01 +0000 | [diff] [blame] | 122 | static bool canGoAfterDWARF(const MCSectionMachO &MSec) { |
| 123 | // These sections are created by the assembler itself after the end of |
| 124 | // the .s file. |
| 125 | StringRef SegName = MSec.getSegmentName(); |
| 126 | StringRef SecName = MSec.getSectionName(); |
| 127 | |
| 128 | if (SegName == "__LD" && SecName == "__compact_unwind") |
| 129 | return true; |
| 130 | |
| 131 | if (SegName == "__IMPORT") { |
| 132 | if (SecName == "__jump_table") |
| 133 | return true; |
| 134 | |
| 135 | if (SecName == "__pointers") |
| 136 | return true; |
| 137 | } |
| 138 | |
| 139 | if (SegName == "__TEXT" && SecName == "__eh_frame") |
| 140 | return true; |
| 141 | |
| 142 | if (SegName == "__DATA" && SecName == "__nl_symbol_ptr") |
| 143 | return true; |
| 144 | |
| 145 | return false; |
| 146 | } |
| 147 | |
Rafael Espindola | 0709a7b | 2015-05-21 19:20:38 +0000 | [diff] [blame] | 148 | void MCMachOStreamer::ChangeSection(MCSection *Section, |
Tim Northover | c3988b4 | 2014-03-29 07:05:06 +0000 | [diff] [blame] | 149 | const MCExpr *Subsection) { |
| 150 | // Change the section normally. |
Rafael Espindola | 36a15cb | 2015-03-20 20:00:01 +0000 | [diff] [blame] | 151 | bool Created = MCObjectStreamer::changeSectionImpl(Section, Subsection); |
| 152 | const MCSectionMachO &MSec = *cast<MCSectionMachO>(Section); |
| 153 | StringRef SegName = MSec.getSegmentName(); |
| 154 | if (SegName == "__DWARF") |
| 155 | CreatedADWARFSection = true; |
| 156 | else if (Created && DWARFMustBeAtTheEnd && !canGoAfterDWARF(MSec)) |
| 157 | assert(!CreatedADWARFSection && "Creating regular section after DWARF"); |
| 158 | |
Tim Northover | c3988b4 | 2014-03-29 07:05:06 +0000 | [diff] [blame] | 159 | // Output a linker-local symbol so we don't need section-relative local |
| 160 | // relocations. The linker hates us when we do that. |
Rafael Espindola | 2f9bdd8 | 2015-05-27 20:52:32 +0000 | [diff] [blame] | 161 | if (LabelSections && !HasSectionLabel[Section] && |
| 162 | !Section->getBeginSymbol()) { |
Jim Grosbach | 6f48200 | 2015-05-18 18:43:14 +0000 | [diff] [blame] | 163 | MCSymbol *Label = getContext().createLinkerPrivateTempSymbol(); |
Rafael Espindola | 2f9bdd8 | 2015-05-27 20:52:32 +0000 | [diff] [blame] | 164 | Section->setBeginSymbol(Label); |
Tim Northover | c3988b4 | 2014-03-29 07:05:06 +0000 | [diff] [blame] | 165 | HasSectionLabel[Section] = true; |
| 166 | } |
| 167 | } |
| 168 | |
Rafael Espindola | 349c329 | 2011-04-28 12:50:37 +0000 | [diff] [blame] | 169 | void MCMachOStreamer::EmitEHSymAttributes(const MCSymbol *Symbol, |
| 170 | MCSymbol *EHSymbol) { |
Rafael Espindola | b5d316b | 2015-05-29 20:21:02 +0000 | [diff] [blame] | 171 | getAssembler().registerSymbol(*Symbol); |
Rafael Espindola | 4d37b2a | 2015-05-29 21:45:01 +0000 | [diff] [blame] | 172 | if (Symbol->isExternal()) |
Rafael Espindola | 349c329 | 2011-04-28 12:50:37 +0000 | [diff] [blame] | 173 | EmitSymbolAttribute(EHSymbol, MCSA_Global); |
Pete Cooper | 916f79e | 2015-06-08 17:17:28 +0000 | [diff] [blame] | 174 | if (cast<MCSymbolMachO>(Symbol)->isWeakDefinition()) |
Rafael Espindola | 349c329 | 2011-04-28 12:50:37 +0000 | [diff] [blame] | 175 | EmitSymbolAttribute(EHSymbol, MCSA_WeakDefinition); |
Rafael Espindola | 4d37b2a | 2015-05-29 21:45:01 +0000 | [diff] [blame] | 176 | if (Symbol->isPrivateExtern()) |
Rafael Espindola | 0b474c2 | 2011-04-30 16:22:46 +0000 | [diff] [blame] | 177 | EmitSymbolAttribute(EHSymbol, MCSA_PrivateExtern); |
Rafael Espindola | 349c329 | 2011-04-28 12:50:37 +0000 | [diff] [blame] | 178 | } |
| 179 | |
Daniel Dunbar | 3016db3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 180 | void MCMachOStreamer::EmitLabel(MCSymbol *Symbol) { |
Rafael Espindola | 1679580 | 2010-11-28 16:22:59 +0000 | [diff] [blame] | 181 | assert(Symbol->isUndefined() && "Cannot define a symbol twice!"); |
Rafael Espindola | 1679580 | 2010-11-28 16:22:59 +0000 | [diff] [blame] | 182 | |
Daniel Dunbar | ede8e6d | 2010-06-16 20:04:32 +0000 | [diff] [blame] | 183 | // We have to create a new fragment if this is an atom defining symbol, |
| 184 | // fragments cannot span atoms. |
Rafael Espindola | e5b7415 | 2010-11-28 17:18:55 +0000 | [diff] [blame] | 185 | if (getAssembler().isSymbolLinkerVisible(*Symbol)) |
Peter Collingbourne | 2f495b9 | 2013-04-17 21:18:16 +0000 | [diff] [blame] | 186 | insert(new MCDataFragment()); |
Daniel Dunbar | aadb2ca | 2010-05-10 22:45:09 +0000 | [diff] [blame] | 187 | |
Rafael Espindola | e5b7415 | 2010-11-28 17:18:55 +0000 | [diff] [blame] | 188 | MCObjectStreamer::EmitLabel(Symbol); |
Daniel Dunbar | 04a1158 | 2009-08-24 08:40:12 +0000 | [diff] [blame] | 189 | |
Daniel Dunbar | 0211a96 | 2010-05-17 20:12:31 +0000 | [diff] [blame] | 190 | // This causes the reference type flag to be cleared. Darwin 'as' was "trying" |
| 191 | // to clear the weak reference and weak definition bits too, but the |
| 192 | // implementation was buggy. For now we just try to match 'as', for |
| 193 | // diffability. |
| 194 | // |
| 195 | // FIXME: Cleanup this code, these bits should be emitted based on semantic |
| 196 | // properties, not on the order of definition, etc. |
Pete Cooper | 916f79e | 2015-06-08 17:17:28 +0000 | [diff] [blame] | 197 | cast<MCSymbolMachO>(Symbol)->clearReferenceType(); |
Daniel Dunbar | 3016db3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 198 | } |
| 199 | |
Jim Grosbach | 4b63d2a | 2012-05-18 19:12:01 +0000 | [diff] [blame] | 200 | void MCMachOStreamer::EmitDataRegion(DataRegionData::KindTy Kind) { |
Jim Grosbach | 745c52d | 2012-10-01 22:20:54 +0000 | [diff] [blame] | 201 | if (!getAssembler().getBackend().hasDataInCodeSupport()) |
| 202 | return; |
Jim Grosbach | 4b63d2a | 2012-05-18 19:12:01 +0000 | [diff] [blame] | 203 | // Create a temporary label to mark the start of the data region. |
Jim Grosbach | 6f48200 | 2015-05-18 18:43:14 +0000 | [diff] [blame] | 204 | MCSymbol *Start = getContext().createTempSymbol(); |
Jim Grosbach | 4b63d2a | 2012-05-18 19:12:01 +0000 | [diff] [blame] | 205 | EmitLabel(Start); |
| 206 | // Record the region for the object writer to use. |
Craig Topper | bb694de | 2014-04-13 04:57:38 +0000 | [diff] [blame] | 207 | DataRegionData Data = { Kind, Start, nullptr }; |
Jim Grosbach | 4b63d2a | 2012-05-18 19:12:01 +0000 | [diff] [blame] | 208 | std::vector<DataRegionData> &Regions = getAssembler().getDataRegions(); |
| 209 | Regions.push_back(Data); |
| 210 | } |
| 211 | |
| 212 | void MCMachOStreamer::EmitDataRegionEnd() { |
Jim Grosbach | 745c52d | 2012-10-01 22:20:54 +0000 | [diff] [blame] | 213 | if (!getAssembler().getBackend().hasDataInCodeSupport()) |
| 214 | return; |
Jim Grosbach | 4b63d2a | 2012-05-18 19:12:01 +0000 | [diff] [blame] | 215 | std::vector<DataRegionData> &Regions = getAssembler().getDataRegions(); |
Alexander Kornienko | 8c0809c | 2015-01-15 11:41:30 +0000 | [diff] [blame] | 216 | assert(!Regions.empty() && "Mismatched .end_data_region!"); |
Jim Grosbach | 4b63d2a | 2012-05-18 19:12:01 +0000 | [diff] [blame] | 217 | DataRegionData &Data = Regions.back(); |
Craig Topper | bb694de | 2014-04-13 04:57:38 +0000 | [diff] [blame] | 218 | assert(!Data.End && "Mismatched .end_data_region!"); |
Jim Grosbach | 4b63d2a | 2012-05-18 19:12:01 +0000 | [diff] [blame] | 219 | // Create a temporary label to mark the end of the data region. |
Jim Grosbach | 6f48200 | 2015-05-18 18:43:14 +0000 | [diff] [blame] | 220 | Data.End = getContext().createTempSymbol(); |
Jim Grosbach | 4b63d2a | 2012-05-18 19:12:01 +0000 | [diff] [blame] | 221 | EmitLabel(Data.End); |
| 222 | } |
| 223 | |
Chris Lattner | 685508c | 2010-01-23 06:39:22 +0000 | [diff] [blame] | 224 | void MCMachOStreamer::EmitAssemblerFlag(MCAssemblerFlag Flag) { |
Jim Grosbach | 87055ed | 2010-12-08 01:16:55 +0000 | [diff] [blame] | 225 | // Let the target do whatever target specific stuff it needs to do. |
Jim Grosbach | aba3de9 | 2012-01-18 18:52:16 +0000 | [diff] [blame] | 226 | getAssembler().getBackend().handleAssemblerFlag(Flag); |
Jim Grosbach | 87055ed | 2010-12-08 01:16:55 +0000 | [diff] [blame] | 227 | // Do any generic stuff we need to do. |
Daniel Dunbar | e269773 | 2009-08-26 21:22:22 +0000 | [diff] [blame] | 228 | switch (Flag) { |
Jim Grosbach | 5a2c68d | 2010-11-05 22:08:08 +0000 | [diff] [blame] | 229 | case MCAF_SyntaxUnified: return; // no-op here. |
Evan Cheng | 481ebb0 | 2011-07-27 00:38:12 +0000 | [diff] [blame] | 230 | case MCAF_Code16: return; // Change parsing mode; no-op here. |
| 231 | case MCAF_Code32: return; // Change parsing mode; no-op here. |
| 232 | case MCAF_Code64: return; // Change parsing mode; no-op here. |
Chris Lattner | 685508c | 2010-01-23 06:39:22 +0000 | [diff] [blame] | 233 | case MCAF_SubsectionsViaSymbols: |
Daniel Dunbar | 8a3c9d9 | 2010-06-16 20:04:22 +0000 | [diff] [blame] | 234 | getAssembler().setSubsectionsViaSymbols(true); |
Daniel Dunbar | 2a2459a | 2009-08-28 07:08:47 +0000 | [diff] [blame] | 235 | return; |
Daniel Dunbar | e269773 | 2009-08-26 21:22:22 +0000 | [diff] [blame] | 236 | } |
Daniel Dunbar | 3016db3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 237 | } |
| 238 | |
Daniel Dunbar | eec0f32 | 2013-01-18 01:26:07 +0000 | [diff] [blame] | 239 | void MCMachOStreamer::EmitLinkerOptions(ArrayRef<std::string> Options) { |
| 240 | getAssembler().getLinkerOptions().push_back(Options); |
| 241 | } |
| 242 | |
Jim Grosbach | 4b63d2a | 2012-05-18 19:12:01 +0000 | [diff] [blame] | 243 | void MCMachOStreamer::EmitDataRegion(MCDataRegionType Kind) { |
| 244 | switch (Kind) { |
| 245 | case MCDR_DataRegion: |
| 246 | EmitDataRegion(DataRegionData::Data); |
| 247 | return; |
| 248 | case MCDR_DataRegionJT8: |
| 249 | EmitDataRegion(DataRegionData::JumpTable8); |
| 250 | return; |
| 251 | case MCDR_DataRegionJT16: |
| 252 | EmitDataRegion(DataRegionData::JumpTable16); |
| 253 | return; |
| 254 | case MCDR_DataRegionJT32: |
| 255 | EmitDataRegion(DataRegionData::JumpTable32); |
| 256 | return; |
| 257 | case MCDR_DataRegionEnd: |
| 258 | EmitDataRegionEnd(); |
| 259 | return; |
| 260 | } |
| 261 | } |
| 262 | |
Jim Grosbach | 448334a | 2014-03-18 22:09:05 +0000 | [diff] [blame] | 263 | void MCMachOStreamer::EmitVersionMin(MCVersionMinType Kind, unsigned Major, |
| 264 | unsigned Minor, unsigned Update) { |
| 265 | getAssembler().setVersionMinInfo(Kind, Major, Minor, Update); |
| 266 | } |
| 267 | |
Daniel Dunbar | ab14a6f | 2010-12-29 14:14:06 +0000 | [diff] [blame] | 268 | void MCMachOStreamer::EmitThumbFunc(MCSymbol *Symbol) { |
Jim Grosbach | 41955ff | 2010-12-14 18:46:57 +0000 | [diff] [blame] | 269 | // Remember that the function is a thumb function. Fixup and relocation |
| 270 | // values will need adjusted. |
Daniel Dunbar | ab14a6f | 2010-12-29 14:14:06 +0000 | [diff] [blame] | 271 | getAssembler().setIsThumbFunc(Symbol); |
Pete Cooper | 916f79e | 2015-06-08 17:17:28 +0000 | [diff] [blame] | 272 | cast<MCSymbolMachO>(Symbol)->setThumbFunc(); |
Jim Grosbach | 5a2c68d | 2010-11-05 22:08:08 +0000 | [diff] [blame] | 273 | } |
| 274 | |
Pete Cooper | 916f79e | 2015-06-08 17:17:28 +0000 | [diff] [blame] | 275 | bool MCMachOStreamer::EmitSymbolAttribute(MCSymbol *Sym, |
Chris Lattner | 685508c | 2010-01-23 06:39:22 +0000 | [diff] [blame] | 276 | MCSymbolAttr Attribute) { |
Pete Cooper | 916f79e | 2015-06-08 17:17:28 +0000 | [diff] [blame] | 277 | MCSymbolMachO *Symbol = cast<MCSymbolMachO>(Sym); |
| 278 | |
Daniel Dunbar | 582d610 | 2009-08-24 11:56:58 +0000 | [diff] [blame] | 279 | // Indirect symbols are handled differently, to match how 'as' handles |
| 280 | // them. This makes writing matching .o files easier. |
Chris Lattner | 685508c | 2010-01-23 06:39:22 +0000 | [diff] [blame] | 281 | if (Attribute == MCSA_IndirectSymbol) { |
Daniel Dunbar | c2c0bf9 | 2009-08-26 00:18:21 +0000 | [diff] [blame] | 282 | // Note that we intentionally cannot use the symbol data here; this is |
| 283 | // important for matching the string table that 'as' generates. |
Daniel Dunbar | 582d610 | 2009-08-24 11:56:58 +0000 | [diff] [blame] | 284 | IndirectSymbolData ISD; |
| 285 | ISD.Symbol = Symbol; |
Rafael Espindola | 983bec6 | 2015-05-27 21:04:14 +0000 | [diff] [blame] | 286 | ISD.Section = getCurrentSectionOnly(); |
Daniel Dunbar | 8a3c9d9 | 2010-06-16 20:04:22 +0000 | [diff] [blame] | 287 | getAssembler().getIndirectSymbols().push_back(ISD); |
Saleem Abdulrasool | 4208b61 | 2013-08-09 01:52:03 +0000 | [diff] [blame] | 288 | return true; |
Daniel Dunbar | 582d610 | 2009-08-24 11:56:58 +0000 | [diff] [blame] | 289 | } |
| 290 | |
Daniel Dunbar | 04a1158 | 2009-08-24 08:40:12 +0000 | [diff] [blame] | 291 | // Adding a symbol attribute always introduces the symbol, note that an |
Rafael Espindola | b5d316b | 2015-05-29 20:21:02 +0000 | [diff] [blame] | 292 | // important side effect of calling registerSymbol here is to register |
Daniel Dunbar | 6eab721 | 2010-03-10 20:58:29 +0000 | [diff] [blame] | 293 | // the symbol with the assembler. |
Rafael Espindola | b5d316b | 2015-05-29 20:21:02 +0000 | [diff] [blame] | 294 | getAssembler().registerSymbol(*Symbol); |
Daniel Dunbar | 04a1158 | 2009-08-24 08:40:12 +0000 | [diff] [blame] | 295 | |
| 296 | // The implementation of symbol attributes is designed to match 'as', but it |
| 297 | // leaves much to desired. It doesn't really make sense to arbitrarily add and |
| 298 | // remove flags, but 'as' allows this (in particular, see .desc). |
| 299 | // |
| 300 | // In the future it might be worth trying to make these operations more well |
| 301 | // defined. |
Daniel Dunbar | d185947 | 2009-08-22 11:41:10 +0000 | [diff] [blame] | 302 | switch (Attribute) { |
Chris Lattner | 685508c | 2010-01-23 06:39:22 +0000 | [diff] [blame] | 303 | case MCSA_Invalid: |
Chris Lattner | bc8f638 | 2010-01-25 18:30:45 +0000 | [diff] [blame] | 304 | case MCSA_ELF_TypeFunction: |
| 305 | case MCSA_ELF_TypeIndFunction: |
| 306 | case MCSA_ELF_TypeObject: |
| 307 | case MCSA_ELF_TypeTLS: |
| 308 | case MCSA_ELF_TypeCommon: |
| 309 | case MCSA_ELF_TypeNoType: |
Rafael Espindola | 4bcf94c | 2010-11-13 04:51:02 +0000 | [diff] [blame] | 310 | case MCSA_ELF_TypeGnuUniqueObject: |
Chris Lattner | 685508c | 2010-01-23 06:39:22 +0000 | [diff] [blame] | 311 | case MCSA_Hidden: |
Chandler Carruth | b256102 | 2011-07-25 21:21:08 +0000 | [diff] [blame] | 312 | case MCSA_IndirectSymbol: |
Chris Lattner | 685508c | 2010-01-23 06:39:22 +0000 | [diff] [blame] | 313 | case MCSA_Internal: |
| 314 | case MCSA_Protected: |
| 315 | case MCSA_Weak: |
| 316 | case MCSA_Local: |
Saleem Abdulrasool | 4208b61 | 2013-08-09 01:52:03 +0000 | [diff] [blame] | 317 | return false; |
Daniel Dunbar | d185947 | 2009-08-22 11:41:10 +0000 | [diff] [blame] | 318 | |
Chris Lattner | 685508c | 2010-01-23 06:39:22 +0000 | [diff] [blame] | 319 | case MCSA_Global: |
Rafael Espindola | 4d37b2a | 2015-05-29 21:45:01 +0000 | [diff] [blame] | 320 | Symbol->setExternal(true); |
Daniel Dunbar | 0211a96 | 2010-05-17 20:12:31 +0000 | [diff] [blame] | 321 | // This effectively clears the undefined lazy bit, in Darwin 'as', although |
| 322 | // it isn't very consistent because it implements this as part of symbol |
| 323 | // lookup. |
| 324 | // |
| 325 | // FIXME: Cleanup this code, these bits should be emitted based on semantic |
| 326 | // properties, not on the order of definition, etc. |
Pete Cooper | 916f79e | 2015-06-08 17:17:28 +0000 | [diff] [blame] | 327 | Symbol->setReferenceTypeUndefinedLazy(false); |
Daniel Dunbar | d185947 | 2009-08-22 11:41:10 +0000 | [diff] [blame] | 328 | break; |
Daniel Dunbar | 04a1158 | 2009-08-24 08:40:12 +0000 | [diff] [blame] | 329 | |
Chris Lattner | 685508c | 2010-01-23 06:39:22 +0000 | [diff] [blame] | 330 | case MCSA_LazyReference: |
Daniel Dunbar | 04a1158 | 2009-08-24 08:40:12 +0000 | [diff] [blame] | 331 | // FIXME: This requires -dynamic. |
Pete Cooper | 916f79e | 2015-06-08 17:17:28 +0000 | [diff] [blame] | 332 | Symbol->setNoDeadStrip(); |
Daniel Dunbar | 04a1158 | 2009-08-24 08:40:12 +0000 | [diff] [blame] | 333 | if (Symbol->isUndefined()) |
Pete Cooper | 916f79e | 2015-06-08 17:17:28 +0000 | [diff] [blame] | 334 | Symbol->setReferenceTypeUndefinedLazy(true); |
Daniel Dunbar | 04a1158 | 2009-08-24 08:40:12 +0000 | [diff] [blame] | 335 | break; |
| 336 | |
Daniel Dunbar | 04a1158 | 2009-08-24 08:40:12 +0000 | [diff] [blame] | 337 | // Since .reference sets the no dead strip bit, it is equivalent to |
| 338 | // .no_dead_strip in practice. |
Chris Lattner | 685508c | 2010-01-23 06:39:22 +0000 | [diff] [blame] | 339 | case MCSA_Reference: |
| 340 | case MCSA_NoDeadStrip: |
Pete Cooper | 916f79e | 2015-06-08 17:17:28 +0000 | [diff] [blame] | 341 | Symbol->setNoDeadStrip(); |
Daniel Dunbar | 04a1158 | 2009-08-24 08:40:12 +0000 | [diff] [blame] | 342 | break; |
| 343 | |
Kevin Enderby | 8be1441 | 2010-11-19 18:39:33 +0000 | [diff] [blame] | 344 | case MCSA_SymbolResolver: |
Pete Cooper | 916f79e | 2015-06-08 17:17:28 +0000 | [diff] [blame] | 345 | Symbol->setSymbolResolver(); |
Kevin Enderby | 8be1441 | 2010-11-19 18:39:33 +0000 | [diff] [blame] | 346 | break; |
| 347 | |
Chris Lattner | 685508c | 2010-01-23 06:39:22 +0000 | [diff] [blame] | 348 | case MCSA_PrivateExtern: |
Rafael Espindola | 4d37b2a | 2015-05-29 21:45:01 +0000 | [diff] [blame] | 349 | Symbol->setExternal(true); |
| 350 | Symbol->setPrivateExtern(true); |
Daniel Dunbar | 04a1158 | 2009-08-24 08:40:12 +0000 | [diff] [blame] | 351 | break; |
| 352 | |
Chris Lattner | 685508c | 2010-01-23 06:39:22 +0000 | [diff] [blame] | 353 | case MCSA_WeakReference: |
Daniel Dunbar | 04a1158 | 2009-08-24 08:40:12 +0000 | [diff] [blame] | 354 | // FIXME: This requires -dynamic. |
| 355 | if (Symbol->isUndefined()) |
Pete Cooper | 916f79e | 2015-06-08 17:17:28 +0000 | [diff] [blame] | 356 | Symbol->setWeakReference(); |
Daniel Dunbar | 04a1158 | 2009-08-24 08:40:12 +0000 | [diff] [blame] | 357 | break; |
| 358 | |
Chris Lattner | 685508c | 2010-01-23 06:39:22 +0000 | [diff] [blame] | 359 | case MCSA_WeakDefinition: |
Daniel Dunbar | 04a1158 | 2009-08-24 08:40:12 +0000 | [diff] [blame] | 360 | // FIXME: 'as' enforces that this is defined and global. The manual claims |
| 361 | // it has to be in a coalesced section, but this isn't enforced. |
Pete Cooper | 916f79e | 2015-06-08 17:17:28 +0000 | [diff] [blame] | 362 | Symbol->setWeakDefinition(); |
Daniel Dunbar | 04a1158 | 2009-08-24 08:40:12 +0000 | [diff] [blame] | 363 | break; |
Kevin Enderby | 082d0fd | 2010-07-08 17:22:42 +0000 | [diff] [blame] | 364 | |
| 365 | case MCSA_WeakDefAutoPrivate: |
Pete Cooper | 916f79e | 2015-06-08 17:17:28 +0000 | [diff] [blame] | 366 | Symbol->setWeakDefinition(); |
| 367 | Symbol->setWeakReference(); |
Kevin Enderby | 082d0fd | 2010-07-08 17:22:42 +0000 | [diff] [blame] | 368 | break; |
Daniel Dunbar | d185947 | 2009-08-22 11:41:10 +0000 | [diff] [blame] | 369 | } |
Saleem Abdulrasool | 4208b61 | 2013-08-09 01:52:03 +0000 | [diff] [blame] | 370 | |
| 371 | return true; |
Daniel Dunbar | 3016db3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 372 | } |
| 373 | |
| 374 | void MCMachOStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) { |
Daniel Dunbar | 04a1158 | 2009-08-24 08:40:12 +0000 | [diff] [blame] | 375 | // Encode the 'desc' value into the lowest implementation defined bits. |
Rafael Espindola | b5d316b | 2015-05-29 20:21:02 +0000 | [diff] [blame] | 376 | getAssembler().registerSymbol(*Symbol); |
Pete Cooper | 916f79e | 2015-06-08 17:17:28 +0000 | [diff] [blame] | 377 | cast<MCSymbolMachO>(Symbol)->setDesc(DescValue); |
Daniel Dunbar | 3016db3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 378 | } |
| 379 | |
Chris Lattner | b1301f7 | 2010-01-23 07:47:02 +0000 | [diff] [blame] | 380 | void MCMachOStreamer::EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size, |
Daniel Dunbar | 6a715dc | 2009-08-30 06:17:16 +0000 | [diff] [blame] | 381 | unsigned ByteAlignment) { |
Daniel Dunbar | 2701eee | 2009-08-28 07:08:35 +0000 | [diff] [blame] | 382 | // FIXME: Darwin 'as' does appear to allow redef of a .comm by itself. |
| 383 | assert(Symbol->isUndefined() && "Cannot define a symbol twice!"); |
| 384 | |
Rafael Espindola | b5d316b | 2015-05-29 20:21:02 +0000 | [diff] [blame] | 385 | getAssembler().registerSymbol(*Symbol); |
Rafael Espindola | 4d37b2a | 2015-05-29 21:45:01 +0000 | [diff] [blame] | 386 | Symbol->setExternal(true); |
Rafael Espindola | 1467250 | 2015-05-29 17:48:04 +0000 | [diff] [blame] | 387 | Symbol->setCommon(Size, ByteAlignment); |
Daniel Dunbar | 3016db3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 388 | } |
| 389 | |
Benjamin Kramer | 47f9ec9 | 2012-09-07 17:25:13 +0000 | [diff] [blame] | 390 | void MCMachOStreamer::EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size, |
| 391 | unsigned ByteAlignment) { |
| 392 | // '.lcomm' is equivalent to '.zerofill'. |
Rafael Espindola | e308c0c | 2014-01-23 22:49:25 +0000 | [diff] [blame] | 393 | return EmitZerofill(getContext().getObjectFileInfo()->getDataBSSSection(), |
Benjamin Kramer | 47f9ec9 | 2012-09-07 17:25:13 +0000 | [diff] [blame] | 394 | Symbol, Size, ByteAlignment); |
| 395 | } |
| 396 | |
Rafael Espindola | 0709a7b | 2015-05-21 19:20:38 +0000 | [diff] [blame] | 397 | void MCMachOStreamer::EmitZerofill(MCSection *Section, MCSymbol *Symbol, |
Evan Cheng | f5bd6c6 | 2012-06-22 20:14:46 +0000 | [diff] [blame] | 398 | uint64_t Size, unsigned ByteAlignment) { |
Rafael Espindola | bb9a71c | 2015-05-26 15:07:25 +0000 | [diff] [blame] | 399 | getAssembler().registerSection(*Section); |
Daniel Dunbar | 42a39d0 | 2009-08-28 05:49:21 +0000 | [diff] [blame] | 400 | |
| 401 | // The symbol may not be present, which only creates the section. |
| 402 | if (!Symbol) |
| 403 | return; |
| 404 | |
Eric Christopher | 42ae459 | 2013-08-07 21:13:01 +0000 | [diff] [blame] | 405 | // On darwin all virtual sections have zerofill type. |
| 406 | assert(Section->isVirtualSection() && "Section does not have zerofill type!"); |
Daniel Dunbar | 42a39d0 | 2009-08-28 05:49:21 +0000 | [diff] [blame] | 407 | |
| 408 | assert(Symbol->isUndefined() && "Cannot define a symbol twice!"); |
| 409 | |
Rafael Espindola | b5d316b | 2015-05-29 20:21:02 +0000 | [diff] [blame] | 410 | getAssembler().registerSymbol(*Symbol); |
Daniel Dunbar | 42a39d0 | 2009-08-28 05:49:21 +0000 | [diff] [blame] | 411 | |
Daniel Dunbar | 51402b7 | 2010-05-12 22:51:27 +0000 | [diff] [blame] | 412 | // Emit an align fragment if necessary. |
| 413 | if (ByteAlignment != 1) |
Rafael Espindola | bb9a71c | 2015-05-26 15:07:25 +0000 | [diff] [blame] | 414 | new MCAlignFragment(ByteAlignment, 0, 0, ByteAlignment, Section); |
Daniel Dunbar | 51402b7 | 2010-05-12 22:51:27 +0000 | [diff] [blame] | 415 | |
Rafael Espindola | bb9a71c | 2015-05-26 15:07:25 +0000 | [diff] [blame] | 416 | MCFragment *F = new MCFillFragment(0, 0, Size, Section); |
Rafael Espindola | 4d37b2a | 2015-05-29 21:45:01 +0000 | [diff] [blame] | 417 | Symbol->setFragment(F); |
Daniel Dunbar | 42a39d0 | 2009-08-28 05:49:21 +0000 | [diff] [blame] | 418 | |
Daniel Dunbar | 42a39d0 | 2009-08-28 05:49:21 +0000 | [diff] [blame] | 419 | // Update the maximum alignment on the zero fill section if necessary. |
Rafael Espindola | 967d6a6 | 2015-05-21 21:02:35 +0000 | [diff] [blame] | 420 | if (ByteAlignment > Section->getAlignment()) |
| 421 | Section->setAlignment(ByteAlignment); |
Daniel Dunbar | 3016db3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 422 | } |
| 423 | |
Eric Christopher | 09d4703 | 2010-05-21 23:03:53 +0000 | [diff] [blame] | 424 | // This should always be called with the thread local bss section. Like the |
| 425 | // .zerofill directive this doesn't actually switch sections on us. |
Rafael Espindola | 0709a7b | 2015-05-21 19:20:38 +0000 | [diff] [blame] | 426 | void MCMachOStreamer::EmitTBSSSymbol(MCSection *Section, MCSymbol *Symbol, |
Eric Christopher | feedc90 | 2010-05-18 21:26:41 +0000 | [diff] [blame] | 427 | uint64_t Size, unsigned ByteAlignment) { |
| 428 | EmitZerofill(Section, Symbol, Size, ByteAlignment); |
| 429 | return; |
Eric Christopher | 9fb6bb0 | 2010-05-14 01:50:28 +0000 | [diff] [blame] | 430 | } |
| 431 | |
David Woodhouse | 6f3c73f | 2014-01-28 23:12:49 +0000 | [diff] [blame] | 432 | void MCMachOStreamer::EmitInstToData(const MCInst &Inst, |
| 433 | const MCSubtargetInfo &STI) { |
Daniel Dunbar | 9d40ef1 | 2010-05-26 20:37:00 +0000 | [diff] [blame] | 434 | MCDataFragment *DF = getOrCreateDataFragment(); |
| 435 | |
| 436 | SmallVector<MCFixup, 4> Fixups; |
| 437 | SmallString<256> Code; |
| 438 | raw_svector_ostream VecOS(Code); |
Jim Grosbach | 91df21f | 2015-05-15 19:13:16 +0000 | [diff] [blame] | 439 | getAssembler().getEmitter().encodeInstruction(Inst, VecOS, Fixups, STI); |
Daniel Dunbar | 9d40ef1 | 2010-05-26 20:37:00 +0000 | [diff] [blame] | 440 | |
| 441 | // Add the fixups and data. |
| 442 | for (unsigned i = 0, e = Fixups.size(); i != e; ++i) { |
| 443 | Fixups[i].setOffset(Fixups[i].getOffset() + DF->getContents().size()); |
Eli Bendersky | a31a894 | 2012-12-07 19:13:57 +0000 | [diff] [blame] | 444 | DF->getFixups().push_back(Fixups[i]); |
Daniel Dunbar | 9d40ef1 | 2010-05-26 20:37:00 +0000 | [diff] [blame] | 445 | } |
| 446 | DF->getContents().append(Code.begin(), Code.end()); |
| 447 | } |
| 448 | |
Rafael Espindola | 0708209 | 2012-01-07 03:13:18 +0000 | [diff] [blame] | 449 | void MCMachOStreamer::FinishImpl() { |
Rafael Espindola | 7f4ccce | 2014-05-12 13:30:10 +0000 | [diff] [blame] | 450 | EmitFrames(&getAssembler().getBackend()); |
Rafael Espindola | fc82236 | 2011-05-01 15:44:13 +0000 | [diff] [blame] | 451 | |
Daniel Dunbar | ede8e6d | 2010-06-16 20:04:32 +0000 | [diff] [blame] | 452 | // We have to set the fragment atom associations so we can relax properly for |
| 453 | // Mach-O. |
| 454 | |
| 455 | // First, scan the symbol table to build a lookup table from fragments to |
| 456 | // defining symbols. |
Duncan P. N. Exon Smith | 92a699c | 2015-05-20 20:18:16 +0000 | [diff] [blame] | 457 | DenseMap<const MCFragment *, const MCSymbol *> DefiningSymbolMap; |
Duncan P. N. Exon Smith | f48de1c | 2015-05-16 00:35:24 +0000 | [diff] [blame] | 458 | for (const MCSymbol &Symbol : getAssembler().symbols()) { |
Rafael Espindola | e3a20f5 | 2015-10-05 12:07:05 +0000 | [diff] [blame^] | 459 | if (getAssembler().isSymbolLinkerVisible(Symbol) && Symbol.isInSection() && |
| 460 | !Symbol.isVariable()) { |
Daniel Dunbar | ede8e6d | 2010-06-16 20:04:32 +0000 | [diff] [blame] | 461 | // An atom defining symbol should never be internal to a fragment. |
Rafael Espindola | 1467250 | 2015-05-29 17:48:04 +0000 | [diff] [blame] | 462 | assert(Symbol.getOffset() == 0 && |
| 463 | "Invalid offset in atom defining symbol!"); |
Rafael Espindola | 4d37b2a | 2015-05-29 21:45:01 +0000 | [diff] [blame] | 464 | DefiningSymbolMap[Symbol.getFragment()] = &Symbol; |
Daniel Dunbar | ede8e6d | 2010-06-16 20:04:32 +0000 | [diff] [blame] | 465 | } |
| 466 | } |
| 467 | |
| 468 | // Set the fragment atom associations by tracking the last seen atom defining |
| 469 | // symbol. |
| 470 | for (MCAssembler::iterator it = getAssembler().begin(), |
| 471 | ie = getAssembler().end(); it != ie; ++it) { |
Duncan P. N. Exon Smith | 09bfa58 | 2015-05-16 00:48:58 +0000 | [diff] [blame] | 472 | const MCSymbol *CurrentAtom = nullptr; |
Rafael Espindola | a32d0e9 | 2015-05-27 15:14:11 +0000 | [diff] [blame] | 473 | for (MCSection::iterator it2 = it->begin(), ie2 = it->end(); it2 != ie2; |
| 474 | ++it2) { |
Duncan P. N. Exon Smith | 92a699c | 2015-05-20 20:18:16 +0000 | [diff] [blame] | 475 | if (const MCSymbol *Symbol = DefiningSymbolMap.lookup(it2)) |
| 476 | CurrentAtom = Symbol; |
Daniel Dunbar | ede8e6d | 2010-06-16 20:04:32 +0000 | [diff] [blame] | 477 | it2->setAtom(CurrentAtom); |
| 478 | } |
| 479 | } |
| 480 | |
Rafael Espindola | 0708209 | 2012-01-07 03:13:18 +0000 | [diff] [blame] | 481 | this->MCObjectStreamer::FinishImpl(); |
Daniel Dunbar | ede8e6d | 2010-06-16 20:04:32 +0000 | [diff] [blame] | 482 | } |
| 483 | |
Evan Cheng | 5928e69 | 2011-07-25 23:24:55 +0000 | [diff] [blame] | 484 | MCStreamer *llvm::createMachOStreamer(MCContext &Context, MCAsmBackend &MAB, |
Rafael Espindola | 5560a4c | 2015-04-14 22:14:34 +0000 | [diff] [blame] | 485 | raw_pwrite_stream &OS, MCCodeEmitter *CE, |
Rafael Espindola | 36a15cb | 2015-03-20 20:00:01 +0000 | [diff] [blame] | 486 | bool RelaxAll, bool DWARFMustBeAtTheEnd, |
Tim Northover | c3988b4 | 2014-03-29 07:05:06 +0000 | [diff] [blame] | 487 | bool LabelSections) { |
Rafael Espindola | 36a15cb | 2015-03-20 20:00:01 +0000 | [diff] [blame] | 488 | MCMachOStreamer *S = new MCMachOStreamer(Context, MAB, OS, CE, |
| 489 | DWARFMustBeAtTheEnd, LabelSections); |
Steven Wu | 9927206 | 2015-08-05 15:36:38 +0000 | [diff] [blame] | 490 | const Triple &TT = Context.getObjectFileInfo()->getTargetTriple(); |
| 491 | if (TT.isOSDarwin()) { |
| 492 | unsigned Major, Minor, Update; |
| 493 | TT.getOSVersion(Major, Minor, Update); |
| 494 | // If there is a version specified, Major will be non-zero. |
| 495 | if (Major) |
| 496 | S->EmitVersionMin((TT.isMacOSX() ? |
| 497 | MCVM_OSXVersionMin : MCVM_IOSVersionMin), |
| 498 | Major, Minor, Update); |
| 499 | } |
Daniel Dunbar | d821f4a | 2010-03-25 22:49:09 +0000 | [diff] [blame] | 500 | if (RelaxAll) |
| 501 | S->getAssembler().setRelaxAll(true); |
| 502 | return S; |
Daniel Dunbar | 3016db3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 503 | } |