blob: 1e9ef4163256ae505421ba44381e655a70df6946 [file] [log] [blame]
Eugene Zelenko1d435522017-02-07 23:02:00 +00001//===- MCMachOStreamer.cpp - MachO Streamer -------------------------------===//
Daniel Dunbar3016db32009-08-21 09:11:24 +00002//
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 Northoverc3988b42014-03-29 07:05:06 +000010#include "llvm/ADT/DenseMap.h"
Eugene Zelenko1d435522017-02-07 23:02:00 +000011#include "llvm/ADT/SmallString.h"
Tim Northoverc3988b42014-03-29 07:05:06 +000012#include "llvm/ADT/SmallVector.h"
Eugene Zelenko1d435522017-02-07 23:02:00 +000013#include "llvm/ADT/StringRef.h"
14#include "llvm/ADT/Triple.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000015#include "llvm/MC/MCAsmBackend.h"
Daniel Dunbar3016db32009-08-21 09:11:24 +000016#include "llvm/MC/MCAssembler.h"
Daniel Dunbarc7c5f9f2009-08-27 08:17:51 +000017#include "llvm/MC/MCCodeEmitter.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000018#include "llvm/MC/MCContext.h"
Eugene Zelenko1d435522017-02-07 23:02:00 +000019#include "llvm/MC/MCDirectives.h"
Daniel Dunbar73da11e2009-08-31 08:08:38 +000020#include "llvm/MC/MCExpr.h"
Eugene Zelenko1d435522017-02-07 23:02:00 +000021#include "llvm/MC/MCFixup.h"
22#include "llvm/MC/MCFragment.h"
Daniel Dunbarc7c5f9f2009-08-27 08:17:51 +000023#include "llvm/MC/MCInst.h"
Tim Northover53d32512014-03-29 07:34:53 +000024#include "llvm/MC/MCLinkerOptimizationHint.h"
Rafael Espindolae308c0c2014-01-23 22:49:25 +000025#include "llvm/MC/MCObjectFileInfo.h"
Daniel Dunbar8a3c9d92010-06-16 20:04:22 +000026#include "llvm/MC/MCObjectStreamer.h"
Daniel Dunbar3016db32009-08-21 09:11:24 +000027#include "llvm/MC/MCSection.h"
Kevin Enderby7221b762010-08-09 22:52:14 +000028#include "llvm/MC/MCSectionMachO.h"
Eugene Zelenko1d435522017-02-07 23:02:00 +000029#include "llvm/MC/MCStreamer.h"
30#include "llvm/MC/MCSymbol.h"
Pete Cooper916f79e2015-06-08 17:17:28 +000031#include "llvm/MC/MCSymbolMachO.h"
Lang Hames1b640e02016-03-15 01:43:05 +000032#include "llvm/MC/MCValue.h"
Eugene Zelenko1d435522017-02-07 23:02:00 +000033#include "llvm/Support/Casting.h"
Daniel Dunbar3016db32009-08-21 09:11:24 +000034#include "llvm/Support/ErrorHandling.h"
Daniel Dunbarc7c5f9f2009-08-27 08:17:51 +000035#include "llvm/Support/raw_ostream.h"
Eugene Zelenko1d435522017-02-07 23:02:00 +000036#include "llvm/Support/TargetRegistry.h"
37#include <cassert>
38#include <vector>
Daniel Dunbarde04b3f2010-03-23 05:09:03 +000039
Daniel Dunbar3016db32009-08-21 09:11:24 +000040using namespace llvm;
41
42namespace {
43
Daniel Dunbar8a3c9d92010-06-16 20:04:22 +000044class MCMachOStreamer : public MCObjectStreamer {
Daniel Dunbar04a11582009-08-24 08:40:12 +000045private:
Tim Northoverc3988b42014-03-29 07:05:06 +000046 /// 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 Espindola36a15cb2015-03-20 20:00:01 +000051 bool DWARFMustBeAtTheEnd;
52 bool CreatedADWARFSection;
53
Tim Northoverc3988b42014-03-29 07:05:06 +000054 /// 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 Topper34a61bc2014-03-08 07:02:02 +000059 void EmitInstToData(const MCInst &Inst, const MCSubtargetInfo &STI) override;
Daniel Dunbar9d40ef12010-05-26 20:37:00 +000060
Jim Grosbach4b63d2a2012-05-18 19:12:01 +000061 void EmitDataRegion(DataRegionData::KindTy Kind);
62 void EmitDataRegionEnd();
Tim Northoverc3988b42014-03-29 07:05:06 +000063
Daniel Dunbar3016db32009-08-21 09:11:24 +000064public:
Rafael Espindola5560a4c2015-04-14 22:14:34 +000065 MCMachOStreamer(MCContext &Context, MCAsmBackend &MAB, raw_pwrite_stream &OS,
Rafael Espindola36a15cb2015-03-20 20:00:01 +000066 MCCodeEmitter *Emitter, bool DWARFMustBeAtTheEnd, bool label)
67 : MCObjectStreamer(Context, MAB, OS, Emitter), LabelSections(label),
68 DWARFMustBeAtTheEnd(DWARFMustBeAtTheEnd), CreatedADWARFSection(false) {}
Daniel Dunbard821f4a2010-03-25 22:49:09 +000069
Yaron Keren559b47d2014-09-17 09:25:36 +000070 /// state management
71 void reset() override {
Justin Bogner72e81892015-12-03 00:52:20 +000072 CreatedADWARFSection = false;
Yaron Keren559b47d2014-09-17 09:25:36 +000073 HasSectionLabel.clear();
74 MCObjectStreamer::reset();
75 }
76
Daniel Dunbar3016db32009-08-21 09:11:24 +000077 /// @name MCStreamer Interface
78 /// @{
79
Rafael Espindola0709a7b2015-05-21 19:20:38 +000080 void ChangeSection(MCSection *Sect, const MCExpr *Subsect) override;
Rafael Espindolabe991572017-02-10 15:13:12 +000081 void EmitLabel(MCSymbol *Symbol, SMLoc Loc = SMLoc()) override;
Lang Hames1b640e02016-03-15 01:43:05 +000082 void EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) override;
Craig Topper34a61bc2014-03-08 07:02:02 +000083 void EmitEHSymAttributes(const MCSymbol *Symbol, MCSymbol *EHSymbol) override;
84 void EmitAssemblerFlag(MCAssemblerFlag Flag) override;
85 void EmitLinkerOptions(ArrayRef<std::string> Options) override;
86 void EmitDataRegion(MCDataRegionType Kind) override;
Jim Grosbach448334a2014-03-18 22:09:05 +000087 void EmitVersionMin(MCVersionMinType Kind, unsigned Major,
88 unsigned Minor, unsigned Update) override;
Craig Topper34a61bc2014-03-08 07:02:02 +000089 void EmitThumbFunc(MCSymbol *Func) override;
90 bool EmitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute) override;
91 void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) override;
92 void EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
93 unsigned ByteAlignment) override;
Eugene Zelenko1d435522017-02-07 23:02:00 +000094
Craig Topper34a61bc2014-03-08 07:02:02 +000095 void EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size,
96 unsigned ByteAlignment) override;
Rafael Espindola0709a7b2015-05-21 19:20:38 +000097 void EmitZerofill(MCSection *Section, MCSymbol *Symbol = nullptr,
Craig Topper34a61bc2014-03-08 07:02:02 +000098 uint64_t Size = 0, unsigned ByteAlignment = 0) override;
Rafael Espindola0709a7b2015-05-21 19:20:38 +000099 void EmitTBSSSymbol(MCSection *Section, MCSymbol *Symbol, uint64_t Size,
Benjamin Kramer8c90fd72014-09-03 11:41:21 +0000100 unsigned ByteAlignment = 0) override;
Daniel Dunbar9d40ef12010-05-26 20:37:00 +0000101
Craig Topper34a61bc2014-03-08 07:02:02 +0000102 void EmitIdent(StringRef IdentString) override {
Rafael Espindola5645bad2013-10-16 01:05:45 +0000103 llvm_unreachable("macho doesn't support this directive");
Chris Lattner601ef332010-01-25 18:58:59 +0000104 }
Daniel Dunbar9d40ef12010-05-26 20:37:00 +0000105
Tim Northover53d32512014-03-29 07:34:53 +0000106 void EmitLOHDirective(MCLOHType Kind, const MCLOHArgs &Args) override {
107 getAssembler().getLOHContainer().addDirective(Kind, Args);
108 }
109
Craig Topper34a61bc2014-03-08 07:02:02 +0000110 void FinishImpl() override;
Daniel Dunbar3016db32009-08-21 09:11:24 +0000111};
112
113} // end anonymous namespace.
114
Rafael Espindola36a15cb2015-03-20 20:00:01 +0000115static bool canGoAfterDWARF(const MCSectionMachO &MSec) {
116 // These sections are created by the assembler itself after the end of
117 // the .s file.
118 StringRef SegName = MSec.getSegmentName();
119 StringRef SecName = MSec.getSectionName();
120
121 if (SegName == "__LD" && SecName == "__compact_unwind")
122 return true;
123
124 if (SegName == "__IMPORT") {
125 if (SecName == "__jump_table")
126 return true;
127
128 if (SecName == "__pointers")
129 return true;
130 }
131
132 if (SegName == "__TEXT" && SecName == "__eh_frame")
133 return true;
134
Matthias Braun8d115a32017-02-01 01:31:36 +0000135 if (SegName == "__DATA" && (SecName == "__nl_symbol_ptr" ||
136 SecName == "__thread_ptr"))
Rafael Espindola36a15cb2015-03-20 20:00:01 +0000137 return true;
138
139 return false;
140}
141
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000142void MCMachOStreamer::ChangeSection(MCSection *Section,
Tim Northoverc3988b42014-03-29 07:05:06 +0000143 const MCExpr *Subsection) {
144 // Change the section normally.
David Blaikie618d3422017-03-16 00:43:19 +0000145 bool Created = changeSectionImpl(Section, Subsection);
Rafael Espindola36a15cb2015-03-20 20:00:01 +0000146 const MCSectionMachO &MSec = *cast<MCSectionMachO>(Section);
147 StringRef SegName = MSec.getSegmentName();
148 if (SegName == "__DWARF")
149 CreatedADWARFSection = true;
150 else if (Created && DWARFMustBeAtTheEnd && !canGoAfterDWARF(MSec))
151 assert(!CreatedADWARFSection && "Creating regular section after DWARF");
152
Tim Northoverc3988b42014-03-29 07:05:06 +0000153 // Output a linker-local symbol so we don't need section-relative local
154 // relocations. The linker hates us when we do that.
Rafael Espindola2f9bdd82015-05-27 20:52:32 +0000155 if (LabelSections && !HasSectionLabel[Section] &&
156 !Section->getBeginSymbol()) {
Jim Grosbach6f482002015-05-18 18:43:14 +0000157 MCSymbol *Label = getContext().createLinkerPrivateTempSymbol();
Rafael Espindola2f9bdd82015-05-27 20:52:32 +0000158 Section->setBeginSymbol(Label);
Tim Northoverc3988b42014-03-29 07:05:06 +0000159 HasSectionLabel[Section] = true;
160 }
161}
162
Rafael Espindola349c3292011-04-28 12:50:37 +0000163void MCMachOStreamer::EmitEHSymAttributes(const MCSymbol *Symbol,
164 MCSymbol *EHSymbol) {
Rafael Espindolab5d316b2015-05-29 20:21:02 +0000165 getAssembler().registerSymbol(*Symbol);
Rafael Espindola4d37b2a2015-05-29 21:45:01 +0000166 if (Symbol->isExternal())
Rafael Espindola349c3292011-04-28 12:50:37 +0000167 EmitSymbolAttribute(EHSymbol, MCSA_Global);
Pete Cooper916f79e2015-06-08 17:17:28 +0000168 if (cast<MCSymbolMachO>(Symbol)->isWeakDefinition())
Rafael Espindola349c3292011-04-28 12:50:37 +0000169 EmitSymbolAttribute(EHSymbol, MCSA_WeakDefinition);
Rafael Espindola4d37b2a2015-05-29 21:45:01 +0000170 if (Symbol->isPrivateExtern())
Rafael Espindola0b474c22011-04-30 16:22:46 +0000171 EmitSymbolAttribute(EHSymbol, MCSA_PrivateExtern);
Rafael Espindola349c3292011-04-28 12:50:37 +0000172}
173
Rafael Espindolabe991572017-02-10 15:13:12 +0000174void MCMachOStreamer::EmitLabel(MCSymbol *Symbol, SMLoc Loc) {
Daniel Dunbarede8e6d2010-06-16 20:04:32 +0000175 // We have to create a new fragment if this is an atom defining symbol,
176 // fragments cannot span atoms.
Rafael Espindolae5b74152010-11-28 17:18:55 +0000177 if (getAssembler().isSymbolLinkerVisible(*Symbol))
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000178 insert(new MCDataFragment());
Daniel Dunbaraadb2ca2010-05-10 22:45:09 +0000179
Rafael Espindolabe991572017-02-10 15:13:12 +0000180 MCObjectStreamer::EmitLabel(Symbol, Loc);
Daniel Dunbar04a11582009-08-24 08:40:12 +0000181
Daniel Dunbar0211a962010-05-17 20:12:31 +0000182 // This causes the reference type flag to be cleared. Darwin 'as' was "trying"
183 // to clear the weak reference and weak definition bits too, but the
184 // implementation was buggy. For now we just try to match 'as', for
185 // diffability.
186 //
187 // FIXME: Cleanup this code, these bits should be emitted based on semantic
188 // properties, not on the order of definition, etc.
Pete Cooper916f79e2015-06-08 17:17:28 +0000189 cast<MCSymbolMachO>(Symbol)->clearReferenceType();
Daniel Dunbar3016db32009-08-21 09:11:24 +0000190}
191
Lang Hames1b640e02016-03-15 01:43:05 +0000192void MCMachOStreamer::EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) {
193 MCValue Res;
194
Lang Hamesabda4d22016-03-15 04:20:49 +0000195 if (Value->evaluateAsRelocatable(Res, nullptr, nullptr)) {
196 if (const MCSymbolRefExpr *SymAExpr = Res.getSymA()) {
197 const MCSymbol &SymA = SymAExpr->getSymbol();
198 if (!Res.getSymB() && (SymA.getName() == "" || Res.getConstant() != 0))
199 cast<MCSymbolMachO>(Symbol)->setAltEntry();
200 }
201 }
Lang Hames1b640e02016-03-15 01:43:05 +0000202 MCObjectStreamer::EmitAssignment(Symbol, Value);
203}
204
Jim Grosbach4b63d2a2012-05-18 19:12:01 +0000205void MCMachOStreamer::EmitDataRegion(DataRegionData::KindTy Kind) {
206 // Create a temporary label to mark the start of the data region.
Jim Grosbach6f482002015-05-18 18:43:14 +0000207 MCSymbol *Start = getContext().createTempSymbol();
Jim Grosbach4b63d2a2012-05-18 19:12:01 +0000208 EmitLabel(Start);
209 // Record the region for the object writer to use.
Craig Topperbb694de2014-04-13 04:57:38 +0000210 DataRegionData Data = { Kind, Start, nullptr };
Jim Grosbach4b63d2a2012-05-18 19:12:01 +0000211 std::vector<DataRegionData> &Regions = getAssembler().getDataRegions();
212 Regions.push_back(Data);
213}
214
215void MCMachOStreamer::EmitDataRegionEnd() {
216 std::vector<DataRegionData> &Regions = getAssembler().getDataRegions();
Alexander Kornienko8c0809c2015-01-15 11:41:30 +0000217 assert(!Regions.empty() && "Mismatched .end_data_region!");
Jim Grosbach4b63d2a2012-05-18 19:12:01 +0000218 DataRegionData &Data = Regions.back();
Craig Topperbb694de2014-04-13 04:57:38 +0000219 assert(!Data.End && "Mismatched .end_data_region!");
Jim Grosbach4b63d2a2012-05-18 19:12:01 +0000220 // Create a temporary label to mark the end of the data region.
Jim Grosbach6f482002015-05-18 18:43:14 +0000221 Data.End = getContext().createTempSymbol();
Jim Grosbach4b63d2a2012-05-18 19:12:01 +0000222 EmitLabel(Data.End);
223}
224
Chris Lattner685508c2010-01-23 06:39:22 +0000225void MCMachOStreamer::EmitAssemblerFlag(MCAssemblerFlag Flag) {
Jim Grosbach87055ed2010-12-08 01:16:55 +0000226 // Let the target do whatever target specific stuff it needs to do.
Jim Grosbachaba3de92012-01-18 18:52:16 +0000227 getAssembler().getBackend().handleAssemblerFlag(Flag);
Jim Grosbach87055ed2010-12-08 01:16:55 +0000228 // Do any generic stuff we need to do.
Daniel Dunbare2697732009-08-26 21:22:22 +0000229 switch (Flag) {
Jim Grosbach5a2c68d2010-11-05 22:08:08 +0000230 case MCAF_SyntaxUnified: return; // no-op here.
Evan Cheng481ebb02011-07-27 00:38:12 +0000231 case MCAF_Code16: return; // Change parsing mode; no-op here.
232 case MCAF_Code32: return; // Change parsing mode; no-op here.
233 case MCAF_Code64: return; // Change parsing mode; no-op here.
Chris Lattner685508c2010-01-23 06:39:22 +0000234 case MCAF_SubsectionsViaSymbols:
Daniel Dunbar8a3c9d92010-06-16 20:04:22 +0000235 getAssembler().setSubsectionsViaSymbols(true);
Daniel Dunbar2a2459a2009-08-28 07:08:47 +0000236 return;
Daniel Dunbare2697732009-08-26 21:22:22 +0000237 }
Daniel Dunbar3016db32009-08-21 09:11:24 +0000238}
239
Daniel Dunbareec0f322013-01-18 01:26:07 +0000240void MCMachOStreamer::EmitLinkerOptions(ArrayRef<std::string> Options) {
241 getAssembler().getLinkerOptions().push_back(Options);
242}
243
Jim Grosbach4b63d2a2012-05-18 19:12:01 +0000244void MCMachOStreamer::EmitDataRegion(MCDataRegionType Kind) {
245 switch (Kind) {
246 case MCDR_DataRegion:
247 EmitDataRegion(DataRegionData::Data);
248 return;
249 case MCDR_DataRegionJT8:
250 EmitDataRegion(DataRegionData::JumpTable8);
251 return;
252 case MCDR_DataRegionJT16:
253 EmitDataRegion(DataRegionData::JumpTable16);
254 return;
255 case MCDR_DataRegionJT32:
256 EmitDataRegion(DataRegionData::JumpTable32);
257 return;
258 case MCDR_DataRegionEnd:
259 EmitDataRegionEnd();
260 return;
261 }
262}
263
Jim Grosbach448334a2014-03-18 22:09:05 +0000264void MCMachOStreamer::EmitVersionMin(MCVersionMinType Kind, unsigned Major,
265 unsigned Minor, unsigned Update) {
266 getAssembler().setVersionMinInfo(Kind, Major, Minor, Update);
267}
268
Daniel Dunbarab14a6f2010-12-29 14:14:06 +0000269void MCMachOStreamer::EmitThumbFunc(MCSymbol *Symbol) {
Jim Grosbach41955ff2010-12-14 18:46:57 +0000270 // Remember that the function is a thumb function. Fixup and relocation
271 // values will need adjusted.
Daniel Dunbarab14a6f2010-12-29 14:14:06 +0000272 getAssembler().setIsThumbFunc(Symbol);
Pete Cooper916f79e2015-06-08 17:17:28 +0000273 cast<MCSymbolMachO>(Symbol)->setThumbFunc();
Jim Grosbach5a2c68d2010-11-05 22:08:08 +0000274}
275
Pete Cooper916f79e2015-06-08 17:17:28 +0000276bool MCMachOStreamer::EmitSymbolAttribute(MCSymbol *Sym,
Chris Lattner685508c2010-01-23 06:39:22 +0000277 MCSymbolAttr Attribute) {
Pete Cooper916f79e2015-06-08 17:17:28 +0000278 MCSymbolMachO *Symbol = cast<MCSymbolMachO>(Sym);
279
Daniel Dunbar582d6102009-08-24 11:56:58 +0000280 // Indirect symbols are handled differently, to match how 'as' handles
281 // them. This makes writing matching .o files easier.
Chris Lattner685508c2010-01-23 06:39:22 +0000282 if (Attribute == MCSA_IndirectSymbol) {
Daniel Dunbarc2c0bf92009-08-26 00:18:21 +0000283 // Note that we intentionally cannot use the symbol data here; this is
284 // important for matching the string table that 'as' generates.
Daniel Dunbar582d6102009-08-24 11:56:58 +0000285 IndirectSymbolData ISD;
286 ISD.Symbol = Symbol;
Rafael Espindola983bec62015-05-27 21:04:14 +0000287 ISD.Section = getCurrentSectionOnly();
Daniel Dunbar8a3c9d92010-06-16 20:04:22 +0000288 getAssembler().getIndirectSymbols().push_back(ISD);
Saleem Abdulrasool4208b612013-08-09 01:52:03 +0000289 return true;
Daniel Dunbar582d6102009-08-24 11:56:58 +0000290 }
291
Daniel Dunbar04a11582009-08-24 08:40:12 +0000292 // Adding a symbol attribute always introduces the symbol, note that an
Rafael Espindolab5d316b2015-05-29 20:21:02 +0000293 // important side effect of calling registerSymbol here is to register
Daniel Dunbar6eab7212010-03-10 20:58:29 +0000294 // the symbol with the assembler.
Rafael Espindolab5d316b2015-05-29 20:21:02 +0000295 getAssembler().registerSymbol(*Symbol);
Daniel Dunbar04a11582009-08-24 08:40:12 +0000296
297 // The implementation of symbol attributes is designed to match 'as', but it
298 // leaves much to desired. It doesn't really make sense to arbitrarily add and
299 // remove flags, but 'as' allows this (in particular, see .desc).
300 //
301 // In the future it might be worth trying to make these operations more well
302 // defined.
Daniel Dunbard1859472009-08-22 11:41:10 +0000303 switch (Attribute) {
Chris Lattner685508c2010-01-23 06:39:22 +0000304 case MCSA_Invalid:
Chris Lattnerbc8f6382010-01-25 18:30:45 +0000305 case MCSA_ELF_TypeFunction:
306 case MCSA_ELF_TypeIndFunction:
307 case MCSA_ELF_TypeObject:
308 case MCSA_ELF_TypeTLS:
309 case MCSA_ELF_TypeCommon:
310 case MCSA_ELF_TypeNoType:
Rafael Espindola4bcf94c2010-11-13 04:51:02 +0000311 case MCSA_ELF_TypeGnuUniqueObject:
Chris Lattner685508c2010-01-23 06:39:22 +0000312 case MCSA_Hidden:
Chandler Carruthb2561022011-07-25 21:21:08 +0000313 case MCSA_IndirectSymbol:
Chris Lattner685508c2010-01-23 06:39:22 +0000314 case MCSA_Internal:
315 case MCSA_Protected:
316 case MCSA_Weak:
317 case MCSA_Local:
Saleem Abdulrasool4208b612013-08-09 01:52:03 +0000318 return false;
Daniel Dunbard1859472009-08-22 11:41:10 +0000319
Chris Lattner685508c2010-01-23 06:39:22 +0000320 case MCSA_Global:
Rafael Espindola4d37b2a2015-05-29 21:45:01 +0000321 Symbol->setExternal(true);
Daniel Dunbar0211a962010-05-17 20:12:31 +0000322 // This effectively clears the undefined lazy bit, in Darwin 'as', although
323 // it isn't very consistent because it implements this as part of symbol
324 // lookup.
325 //
326 // FIXME: Cleanup this code, these bits should be emitted based on semantic
327 // properties, not on the order of definition, etc.
Pete Cooper916f79e2015-06-08 17:17:28 +0000328 Symbol->setReferenceTypeUndefinedLazy(false);
Daniel Dunbard1859472009-08-22 11:41:10 +0000329 break;
Daniel Dunbar04a11582009-08-24 08:40:12 +0000330
Chris Lattner685508c2010-01-23 06:39:22 +0000331 case MCSA_LazyReference:
Daniel Dunbar04a11582009-08-24 08:40:12 +0000332 // FIXME: This requires -dynamic.
Pete Cooper916f79e2015-06-08 17:17:28 +0000333 Symbol->setNoDeadStrip();
Daniel Dunbar04a11582009-08-24 08:40:12 +0000334 if (Symbol->isUndefined())
Pete Cooper916f79e2015-06-08 17:17:28 +0000335 Symbol->setReferenceTypeUndefinedLazy(true);
Daniel Dunbar04a11582009-08-24 08:40:12 +0000336 break;
337
Daniel Dunbar04a11582009-08-24 08:40:12 +0000338 // Since .reference sets the no dead strip bit, it is equivalent to
339 // .no_dead_strip in practice.
Chris Lattner685508c2010-01-23 06:39:22 +0000340 case MCSA_Reference:
341 case MCSA_NoDeadStrip:
Pete Cooper916f79e2015-06-08 17:17:28 +0000342 Symbol->setNoDeadStrip();
Daniel Dunbar04a11582009-08-24 08:40:12 +0000343 break;
344
Kevin Enderby8be14412010-11-19 18:39:33 +0000345 case MCSA_SymbolResolver:
Pete Cooper916f79e2015-06-08 17:17:28 +0000346 Symbol->setSymbolResolver();
Kevin Enderby8be14412010-11-19 18:39:33 +0000347 break;
348
Lang Hames1b640e02016-03-15 01:43:05 +0000349 case MCSA_AltEntry:
350 Symbol->setAltEntry();
351 break;
352
Chris Lattner685508c2010-01-23 06:39:22 +0000353 case MCSA_PrivateExtern:
Rafael Espindola4d37b2a2015-05-29 21:45:01 +0000354 Symbol->setExternal(true);
355 Symbol->setPrivateExtern(true);
Daniel Dunbar04a11582009-08-24 08:40:12 +0000356 break;
357
Chris Lattner685508c2010-01-23 06:39:22 +0000358 case MCSA_WeakReference:
Daniel Dunbar04a11582009-08-24 08:40:12 +0000359 // FIXME: This requires -dynamic.
360 if (Symbol->isUndefined())
Pete Cooper916f79e2015-06-08 17:17:28 +0000361 Symbol->setWeakReference();
Daniel Dunbar04a11582009-08-24 08:40:12 +0000362 break;
363
Chris Lattner685508c2010-01-23 06:39:22 +0000364 case MCSA_WeakDefinition:
Daniel Dunbar04a11582009-08-24 08:40:12 +0000365 // FIXME: 'as' enforces that this is defined and global. The manual claims
366 // it has to be in a coalesced section, but this isn't enforced.
Pete Cooper916f79e2015-06-08 17:17:28 +0000367 Symbol->setWeakDefinition();
Daniel Dunbar04a11582009-08-24 08:40:12 +0000368 break;
Kevin Enderby082d0fd2010-07-08 17:22:42 +0000369
370 case MCSA_WeakDefAutoPrivate:
Pete Cooper916f79e2015-06-08 17:17:28 +0000371 Symbol->setWeakDefinition();
372 Symbol->setWeakReference();
Kevin Enderby082d0fd2010-07-08 17:22:42 +0000373 break;
Daniel Dunbard1859472009-08-22 11:41:10 +0000374 }
Saleem Abdulrasool4208b612013-08-09 01:52:03 +0000375
376 return true;
Daniel Dunbar3016db32009-08-21 09:11:24 +0000377}
378
379void MCMachOStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
Daniel Dunbar04a11582009-08-24 08:40:12 +0000380 // Encode the 'desc' value into the lowest implementation defined bits.
Rafael Espindolab5d316b2015-05-29 20:21:02 +0000381 getAssembler().registerSymbol(*Symbol);
Pete Cooper916f79e2015-06-08 17:17:28 +0000382 cast<MCSymbolMachO>(Symbol)->setDesc(DescValue);
Daniel Dunbar3016db32009-08-21 09:11:24 +0000383}
384
Chris Lattnerb1301f72010-01-23 07:47:02 +0000385void MCMachOStreamer::EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
Daniel Dunbar6a715dc2009-08-30 06:17:16 +0000386 unsigned ByteAlignment) {
Daniel Dunbar2701eee2009-08-28 07:08:35 +0000387 // FIXME: Darwin 'as' does appear to allow redef of a .comm by itself.
388 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
389
Rafael Espindolab5d316b2015-05-29 20:21:02 +0000390 getAssembler().registerSymbol(*Symbol);
Rafael Espindola4d37b2a2015-05-29 21:45:01 +0000391 Symbol->setExternal(true);
Rafael Espindola14672502015-05-29 17:48:04 +0000392 Symbol->setCommon(Size, ByteAlignment);
Daniel Dunbar3016db32009-08-21 09:11:24 +0000393}
394
Benjamin Kramer47f9ec92012-09-07 17:25:13 +0000395void MCMachOStreamer::EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size,
396 unsigned ByteAlignment) {
397 // '.lcomm' is equivalent to '.zerofill'.
Rafael Espindolae308c0c2014-01-23 22:49:25 +0000398 return EmitZerofill(getContext().getObjectFileInfo()->getDataBSSSection(),
Benjamin Kramer47f9ec92012-09-07 17:25:13 +0000399 Symbol, Size, ByteAlignment);
400}
401
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000402void MCMachOStreamer::EmitZerofill(MCSection *Section, MCSymbol *Symbol,
Evan Chengf5bd6c62012-06-22 20:14:46 +0000403 uint64_t Size, unsigned ByteAlignment) {
Rafael Espindolabb9a71c2015-05-26 15:07:25 +0000404 getAssembler().registerSection(*Section);
Daniel Dunbar42a39d02009-08-28 05:49:21 +0000405
406 // The symbol may not be present, which only creates the section.
407 if (!Symbol)
408 return;
409
Eric Christopher42ae4592013-08-07 21:13:01 +0000410 // On darwin all virtual sections have zerofill type.
411 assert(Section->isVirtualSection() && "Section does not have zerofill type!");
Daniel Dunbar42a39d02009-08-28 05:49:21 +0000412
413 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
414
Rafael Espindolab5d316b2015-05-29 20:21:02 +0000415 getAssembler().registerSymbol(*Symbol);
Daniel Dunbar42a39d02009-08-28 05:49:21 +0000416
Daniel Dunbar51402b72010-05-12 22:51:27 +0000417 // Emit an align fragment if necessary.
418 if (ByteAlignment != 1)
Rafael Espindolabb9a71c2015-05-26 15:07:25 +0000419 new MCAlignFragment(ByteAlignment, 0, 0, ByteAlignment, Section);
Daniel Dunbar51402b72010-05-12 22:51:27 +0000420
Rafael Espindola1a7e8b42016-01-19 16:57:08 +0000421 MCFragment *F = new MCFillFragment(0, Size, Section);
Rafael Espindola4d37b2a2015-05-29 21:45:01 +0000422 Symbol->setFragment(F);
Daniel Dunbar42a39d02009-08-28 05:49:21 +0000423
Daniel Dunbar42a39d02009-08-28 05:49:21 +0000424 // Update the maximum alignment on the zero fill section if necessary.
Rafael Espindola967d6a62015-05-21 21:02:35 +0000425 if (ByteAlignment > Section->getAlignment())
426 Section->setAlignment(ByteAlignment);
Daniel Dunbar3016db32009-08-21 09:11:24 +0000427}
428
Eric Christopher09d47032010-05-21 23:03:53 +0000429// 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 Espindola0709a7b2015-05-21 19:20:38 +0000431void MCMachOStreamer::EmitTBSSSymbol(MCSection *Section, MCSymbol *Symbol,
Eric Christopherfeedc902010-05-18 21:26:41 +0000432 uint64_t Size, unsigned ByteAlignment) {
433 EmitZerofill(Section, Symbol, Size, ByteAlignment);
Eric Christopher9fb6bb02010-05-14 01:50:28 +0000434}
435
David Woodhouse6f3c73f2014-01-28 23:12:49 +0000436void MCMachOStreamer::EmitInstToData(const MCInst &Inst,
437 const MCSubtargetInfo &STI) {
Daniel Dunbar9d40ef12010-05-26 20:37:00 +0000438 MCDataFragment *DF = getOrCreateDataFragment();
439
440 SmallVector<MCFixup, 4> Fixups;
441 SmallString<256> Code;
442 raw_svector_ostream VecOS(Code);
Jim Grosbach91df21f2015-05-15 19:13:16 +0000443 getAssembler().getEmitter().encodeInstruction(Inst, VecOS, Fixups, STI);
Daniel Dunbar9d40ef12010-05-26 20:37:00 +0000444
445 // Add the fixups and data.
Craig Topper1129a002015-10-14 04:36:00 +0000446 for (MCFixup &Fixup : Fixups) {
447 Fixup.setOffset(Fixup.getOffset() + DF->getContents().size());
448 DF->getFixups().push_back(Fixup);
Daniel Dunbar9d40ef12010-05-26 20:37:00 +0000449 }
450 DF->getContents().append(Code.begin(), Code.end());
451}
452
Rafael Espindola07082092012-01-07 03:13:18 +0000453void MCMachOStreamer::FinishImpl() {
Rafael Espindola7f4ccce2014-05-12 13:30:10 +0000454 EmitFrames(&getAssembler().getBackend());
Rafael Espindolafc822362011-05-01 15:44:13 +0000455
Daniel Dunbarede8e6d2010-06-16 20:04:32 +0000456 // 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 Smith92a699c2015-05-20 20:18:16 +0000461 DenseMap<const MCFragment *, const MCSymbol *> DefiningSymbolMap;
Duncan P. N. Exon Smithf48de1c2015-05-16 00:35:24 +0000462 for (const MCSymbol &Symbol : getAssembler().symbols()) {
Rafael Espindolae3a20f52015-10-05 12:07:05 +0000463 if (getAssembler().isSymbolLinkerVisible(Symbol) && Symbol.isInSection() &&
464 !Symbol.isVariable()) {
Daniel Dunbarede8e6d2010-06-16 20:04:32 +0000465 // An atom defining symbol should never be internal to a fragment.
Rafael Espindola14672502015-05-29 17:48:04 +0000466 assert(Symbol.getOffset() == 0 &&
467 "Invalid offset in atom defining symbol!");
Rafael Espindola4d37b2a2015-05-29 21:45:01 +0000468 DefiningSymbolMap[Symbol.getFragment()] = &Symbol;
Daniel Dunbarede8e6d2010-06-16 20:04:32 +0000469 }
470 }
471
472 // Set the fragment atom associations by tracking the last seen atom defining
473 // symbol.
Craig Topper1129a002015-10-14 04:36:00 +0000474 for (MCSection &Sec : getAssembler()) {
Duncan P. N. Exon Smith09bfa582015-05-16 00:48:58 +0000475 const MCSymbol *CurrentAtom = nullptr;
Craig Topper1129a002015-10-14 04:36:00 +0000476 for (MCFragment &Frag : Sec) {
477 if (const MCSymbol *Symbol = DefiningSymbolMap.lookup(&Frag))
Duncan P. N. Exon Smith92a699c2015-05-20 20:18:16 +0000478 CurrentAtom = Symbol;
Craig Topper1129a002015-10-14 04:36:00 +0000479 Frag.setAtom(CurrentAtom);
Daniel Dunbarede8e6d2010-06-16 20:04:32 +0000480 }
481 }
482
Rafael Espindola07082092012-01-07 03:13:18 +0000483 this->MCObjectStreamer::FinishImpl();
Daniel Dunbarede8e6d2010-06-16 20:04:32 +0000484}
485
Evan Cheng5928e692011-07-25 23:24:55 +0000486MCStreamer *llvm::createMachOStreamer(MCContext &Context, MCAsmBackend &MAB,
Rafael Espindola5560a4c2015-04-14 22:14:34 +0000487 raw_pwrite_stream &OS, MCCodeEmitter *CE,
Rafael Espindola36a15cb2015-03-20 20:00:01 +0000488 bool RelaxAll, bool DWARFMustBeAtTheEnd,
Tim Northoverc3988b42014-03-29 07:05:06 +0000489 bool LabelSections) {
Rafael Espindola36a15cb2015-03-20 20:00:01 +0000490 MCMachOStreamer *S = new MCMachOStreamer(Context, MAB, OS, CE,
491 DWARFMustBeAtTheEnd, LabelSections);
Steven Wu99272062015-08-05 15:36:38 +0000492 const Triple &TT = Context.getObjectFileInfo()->getTargetTriple();
493 if (TT.isOSDarwin()) {
494 unsigned Major, Minor, Update;
495 TT.getOSVersion(Major, Minor, Update);
496 // If there is a version specified, Major will be non-zero.
Tim Northover2d4d1612015-10-28 22:36:05 +0000497 if (Major) {
498 MCVersionMinType VersionType;
499 if (TT.isWatchOS())
500 VersionType = MCVM_WatchOSVersionMin;
501 else if (TT.isTvOS())
502 VersionType = MCVM_TvOSVersionMin;
503 else if (TT.isMacOSX())
504 VersionType = MCVM_OSXVersionMin;
505 else {
506 assert(TT.isiOS() && "Must only be iOS platform left");
507 VersionType = MCVM_IOSVersionMin;
508 }
509 S->EmitVersionMin(VersionType, Major, Minor, Update);
510 }
Steven Wu99272062015-08-05 15:36:38 +0000511 }
Daniel Dunbard821f4a2010-03-25 22:49:09 +0000512 if (RelaxAll)
513 S->getAssembler().setRelaxAll(true);
514 return S;
Daniel Dunbar3016db32009-08-21 09:11:24 +0000515}