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