blob: a147c3dda38174815af665a9c9b9f4428ce80d4a [file] [log] [blame]
Bill Wendlingb1e21832013-09-04 22:35:41 +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
10#include "llvm/MC/MCStreamer.h"
Tim Northoverc3988b42014-03-29 07:05:06 +000011#include "llvm/ADT/DenseMap.h"
12#include "llvm/ADT/SmallVector.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000013#include "llvm/MC/MCAsmBackend.h"
Daniel Dunbar3016db32009-08-21 09:11:24 +000014#include "llvm/MC/MCAssembler.h"
Daniel Dunbarc7c5f9f2009-08-27 08:17:51 +000015#include "llvm/MC/MCCodeEmitter.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000016#include "llvm/MC/MCContext.h"
17#include "llvm/MC/MCDwarf.h"
Daniel Dunbar73da11e2009-08-31 08:08:38 +000018#include "llvm/MC/MCExpr.h"
Daniel Dunbarc7c5f9f2009-08-27 08:17:51 +000019#include "llvm/MC/MCInst.h"
Tim Northover53d32512014-03-29 07:34:53 +000020#include "llvm/MC/MCLinkerOptimizationHint.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000021#include "llvm/MC/MCMachOSymbolFlags.h"
Rafael Espindolae308c0c2014-01-23 22:49:25 +000022#include "llvm/MC/MCObjectFileInfo.h"
Daniel Dunbar8a3c9d92010-06-16 20:04:22 +000023#include "llvm/MC/MCObjectStreamer.h"
Daniel Dunbar3016db32009-08-21 09:11:24 +000024#include "llvm/MC/MCSection.h"
Kevin Enderby7221b762010-08-09 22:52:14 +000025#include "llvm/MC/MCSectionMachO.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000026#include "llvm/MC/MCSymbol.h"
Kevin Enderbye46564a2010-09-30 16:52:03 +000027#include "llvm/Support/Dwarf.h"
Daniel Dunbar3016db32009-08-21 09:11:24 +000028#include "llvm/Support/ErrorHandling.h"
Daniel Dunbarc7c5f9f2009-08-27 08:17:51 +000029#include "llvm/Support/raw_ostream.h"
Daniel Dunbarde04b3f2010-03-23 05:09:03 +000030
Daniel Dunbar3016db32009-08-21 09:11:24 +000031using namespace llvm;
32
33namespace {
34
Daniel Dunbar8a3c9d92010-06-16 20:04:22 +000035class MCMachOStreamer : public MCObjectStreamer {
Daniel Dunbar04a11582009-08-24 08:40:12 +000036private:
Tim Northoverc3988b42014-03-29 07:05:06 +000037 /// 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
42 /// HasSectionLabel - map of which sections have already had a non-local
43 /// label emitted to them. Used so we don't emit extraneous linker local
44 /// labels in the middle of the section.
45 DenseMap<const MCSection*, bool> HasSectionLabel;
46
Craig Topper34a61bc2014-03-08 07:02:02 +000047 void EmitInstToData(const MCInst &Inst, const MCSubtargetInfo &STI) override;
Daniel Dunbar9d40ef12010-05-26 20:37:00 +000048
Jim Grosbach4b63d2a2012-05-18 19:12:01 +000049 void EmitDataRegion(DataRegionData::KindTy Kind);
50 void EmitDataRegionEnd();
Tim Northoverc3988b42014-03-29 07:05:06 +000051
Daniel Dunbar3016db32009-08-21 09:11:24 +000052public:
Chandler Carruthde093ef2013-01-31 23:29:57 +000053 MCMachOStreamer(MCContext &Context, MCAsmBackend &MAB, raw_ostream &OS,
Tim Northoverc3988b42014-03-29 07:05:06 +000054 MCCodeEmitter *Emitter, bool label)
55 : MCObjectStreamer(Context, MAB, OS, Emitter),
56 LabelSections(label) {}
Daniel Dunbard821f4a2010-03-25 22:49:09 +000057
Yaron Keren559b47d2014-09-17 09:25:36 +000058 /// state management
59 void reset() override {
60 HasSectionLabel.clear();
61 MCObjectStreamer::reset();
62 }
63
Daniel Dunbar3016db32009-08-21 09:11:24 +000064 /// @name MCStreamer Interface
65 /// @{
66
Tim Northoverc3988b42014-03-29 07:05:06 +000067 void ChangeSection(const MCSection *Sect, const MCExpr *Subsect) override;
Craig Topper34a61bc2014-03-08 07:02:02 +000068 void EmitLabel(MCSymbol *Symbol) override;
Craig Topper34a61bc2014-03-08 07:02:02 +000069 void EmitEHSymAttributes(const MCSymbol *Symbol, MCSymbol *EHSymbol) override;
70 void EmitAssemblerFlag(MCAssemblerFlag Flag) override;
71 void EmitLinkerOptions(ArrayRef<std::string> Options) override;
72 void EmitDataRegion(MCDataRegionType Kind) override;
Jim Grosbach448334a2014-03-18 22:09:05 +000073 void EmitVersionMin(MCVersionMinType Kind, unsigned Major,
74 unsigned Minor, unsigned Update) override;
Craig Topper34a61bc2014-03-08 07:02:02 +000075 void EmitThumbFunc(MCSymbol *Func) override;
76 bool EmitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute) override;
77 void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) override;
78 void EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
79 unsigned ByteAlignment) override;
80 void BeginCOFFSymbolDef(const MCSymbol *Symbol) override {
Craig Toppera2886c22012-02-07 05:05:23 +000081 llvm_unreachable("macho doesn't support this directive");
Chris Lattner72afa952010-05-08 19:54:22 +000082 }
Craig Topper34a61bc2014-03-08 07:02:02 +000083 void EmitCOFFSymbolStorageClass(int StorageClass) override {
Craig Toppera2886c22012-02-07 05:05:23 +000084 llvm_unreachable("macho doesn't support this directive");
Chris Lattner72afa952010-05-08 19:54:22 +000085 }
Craig Topper34a61bc2014-03-08 07:02:02 +000086 void EmitCOFFSymbolType(int Type) override {
Craig Toppera2886c22012-02-07 05:05:23 +000087 llvm_unreachable("macho doesn't support this directive");
Chris Lattner72afa952010-05-08 19:54:22 +000088 }
Craig Topper34a61bc2014-03-08 07:02:02 +000089 void EndCOFFSymbolDef() override {
Craig Toppera2886c22012-02-07 05:05:23 +000090 llvm_unreachable("macho doesn't support this directive");
Chris Lattner72afa952010-05-08 19:54:22 +000091 }
Craig Topper34a61bc2014-03-08 07:02:02 +000092 void EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) override {
Craig Toppera2886c22012-02-07 05:05:23 +000093 llvm_unreachable("macho doesn't support this directive");
Chris Lattner91dac6d2010-01-25 07:52:13 +000094 }
Craig Topper34a61bc2014-03-08 07:02:02 +000095 void EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size,
96 unsigned ByteAlignment) override;
Craig Topperbb694de2014-04-13 04:57:38 +000097 void EmitZerofill(const MCSection *Section, MCSymbol *Symbol = nullptr,
Craig Topper34a61bc2014-03-08 07:02:02 +000098 uint64_t Size = 0, unsigned ByteAlignment = 0) override;
Benjamin Kramer8c90fd72014-09-03 11:41:21 +000099 void EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol, uint64_t Size,
100 unsigned ByteAlignment = 0) override;
Daniel Dunbar9d40ef12010-05-26 20:37:00 +0000101
Craig Topper34a61bc2014-03-08 07:02:02 +0000102 void EmitFileDirective(StringRef Filename) override {
Daniel Dunbar6b4391a2010-07-19 20:44:20 +0000103 // FIXME: Just ignore the .file; it isn't important enough to fail the
104 // entire assembly.
105
Rafael Espindola5645bad2013-10-16 01:05:45 +0000106 // report_fatal_error("unsupported directive: '.file'");
107 }
108
Craig Topper34a61bc2014-03-08 07:02:02 +0000109 void EmitIdent(StringRef IdentString) override {
Rafael Espindola5645bad2013-10-16 01:05:45 +0000110 llvm_unreachable("macho doesn't support this directive");
Chris Lattner601ef332010-01-25 18:58:59 +0000111 }
Daniel Dunbar9d40ef12010-05-26 20:37:00 +0000112
Tim Northover53d32512014-03-29 07:34:53 +0000113 void EmitLOHDirective(MCLOHType Kind, const MCLOHArgs &Args) override {
114 getAssembler().getLOHContainer().addDirective(Kind, Args);
115 }
116
Craig Topper34a61bc2014-03-08 07:02:02 +0000117 void FinishImpl() override;
Daniel Dunbar3016db32009-08-21 09:11:24 +0000118};
119
120} // end anonymous namespace.
121
Tim Northoverc3988b42014-03-29 07:05:06 +0000122void MCMachOStreamer::ChangeSection(const MCSection *Section,
123 const MCExpr *Subsection) {
124 // Change the section normally.
125 MCObjectStreamer::ChangeSection(Section, Subsection);
126 // Output a linker-local symbol so we don't need section-relative local
127 // relocations. The linker hates us when we do that.
128 if (LabelSections && !HasSectionLabel[Section]) {
129 MCSymbol *Label = getContext().CreateLinkerPrivateTempSymbol();
130 EmitLabel(Label);
131 HasSectionLabel[Section] = true;
132 }
133}
134
Rafael Espindola349c3292011-04-28 12:50:37 +0000135void MCMachOStreamer::EmitEHSymAttributes(const MCSymbol *Symbol,
136 MCSymbol *EHSymbol) {
137 MCSymbolData &SD =
138 getAssembler().getOrCreateSymbolData(*Symbol);
139 if (SD.isExternal())
140 EmitSymbolAttribute(EHSymbol, MCSA_Global);
141 if (SD.getFlags() & SF_WeakDefinition)
142 EmitSymbolAttribute(EHSymbol, MCSA_WeakDefinition);
Rafael Espindola0b474c22011-04-30 16:22:46 +0000143 if (SD.isPrivateExtern())
144 EmitSymbolAttribute(EHSymbol, MCSA_PrivateExtern);
Rafael Espindola349c3292011-04-28 12:50:37 +0000145}
146
Daniel Dunbar3016db32009-08-21 09:11:24 +0000147void MCMachOStreamer::EmitLabel(MCSymbol *Symbol) {
Rafael Espindola16795802010-11-28 16:22:59 +0000148 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
Rafael Espindola16795802010-11-28 16:22:59 +0000149
Rafael Espindolae5b74152010-11-28 17:18:55 +0000150 // isSymbolLinkerVisible uses the section.
Richard Mitton21101b32013-09-19 23:21:01 +0000151 AssignSection(Symbol, getCurrentSection().first);
Daniel Dunbarede8e6d2010-06-16 20:04:32 +0000152 // We have to create a new fragment if this is an atom defining symbol,
153 // fragments cannot span atoms.
Rafael Espindolae5b74152010-11-28 17:18:55 +0000154 if (getAssembler().isSymbolLinkerVisible(*Symbol))
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000155 insert(new MCDataFragment());
Daniel Dunbaraadb2ca2010-05-10 22:45:09 +0000156
Rafael Espindolae5b74152010-11-28 17:18:55 +0000157 MCObjectStreamer::EmitLabel(Symbol);
Daniel Dunbar04a11582009-08-24 08:40:12 +0000158
Rafael Espindolae5b74152010-11-28 17:18:55 +0000159 MCSymbolData &SD = getAssembler().getSymbolData(*Symbol);
Daniel Dunbar0211a962010-05-17 20:12:31 +0000160 // This causes the reference type flag to be cleared. Darwin 'as' was "trying"
161 // to clear the weak reference and weak definition bits too, but the
162 // implementation was buggy. For now we just try to match 'as', for
163 // diffability.
164 //
165 // FIXME: Cleanup this code, these bits should be emitted based on semantic
166 // properties, not on the order of definition, etc.
167 SD.setFlags(SD.getFlags() & ~SF_ReferenceTypeMask);
Daniel Dunbar3016db32009-08-21 09:11:24 +0000168}
169
Jim Grosbach4b63d2a2012-05-18 19:12:01 +0000170void MCMachOStreamer::EmitDataRegion(DataRegionData::KindTy Kind) {
Jim Grosbach745c52d2012-10-01 22:20:54 +0000171 if (!getAssembler().getBackend().hasDataInCodeSupport())
172 return;
Jim Grosbach4b63d2a2012-05-18 19:12:01 +0000173 // Create a temporary label to mark the start of the data region.
174 MCSymbol *Start = getContext().CreateTempSymbol();
175 EmitLabel(Start);
176 // Record the region for the object writer to use.
Craig Topperbb694de2014-04-13 04:57:38 +0000177 DataRegionData Data = { Kind, Start, nullptr };
Jim Grosbach4b63d2a2012-05-18 19:12:01 +0000178 std::vector<DataRegionData> &Regions = getAssembler().getDataRegions();
179 Regions.push_back(Data);
180}
181
182void MCMachOStreamer::EmitDataRegionEnd() {
Jim Grosbach745c52d2012-10-01 22:20:54 +0000183 if (!getAssembler().getBackend().hasDataInCodeSupport())
184 return;
Jim Grosbach4b63d2a2012-05-18 19:12:01 +0000185 std::vector<DataRegionData> &Regions = getAssembler().getDataRegions();
186 assert(Regions.size() && "Mismatched .end_data_region!");
187 DataRegionData &Data = Regions.back();
Craig Topperbb694de2014-04-13 04:57:38 +0000188 assert(!Data.End && "Mismatched .end_data_region!");
Jim Grosbach4b63d2a2012-05-18 19:12:01 +0000189 // Create a temporary label to mark the end of the data region.
190 Data.End = getContext().CreateTempSymbol();
191 EmitLabel(Data.End);
192}
193
Chris Lattner685508c2010-01-23 06:39:22 +0000194void MCMachOStreamer::EmitAssemblerFlag(MCAssemblerFlag Flag) {
Jim Grosbach87055ed2010-12-08 01:16:55 +0000195 // Let the target do whatever target specific stuff it needs to do.
Jim Grosbachaba3de92012-01-18 18:52:16 +0000196 getAssembler().getBackend().handleAssemblerFlag(Flag);
Jim Grosbach87055ed2010-12-08 01:16:55 +0000197 // Do any generic stuff we need to do.
Daniel Dunbare2697732009-08-26 21:22:22 +0000198 switch (Flag) {
Jim Grosbach5a2c68d2010-11-05 22:08:08 +0000199 case MCAF_SyntaxUnified: return; // no-op here.
Evan Cheng481ebb02011-07-27 00:38:12 +0000200 case MCAF_Code16: return; // Change parsing mode; no-op here.
201 case MCAF_Code32: return; // Change parsing mode; no-op here.
202 case MCAF_Code64: return; // Change parsing mode; no-op here.
Chris Lattner685508c2010-01-23 06:39:22 +0000203 case MCAF_SubsectionsViaSymbols:
Daniel Dunbar8a3c9d92010-06-16 20:04:22 +0000204 getAssembler().setSubsectionsViaSymbols(true);
Daniel Dunbar2a2459a2009-08-28 07:08:47 +0000205 return;
Daniel Dunbare2697732009-08-26 21:22:22 +0000206 }
Daniel Dunbar3016db32009-08-21 09:11:24 +0000207}
208
Daniel Dunbareec0f322013-01-18 01:26:07 +0000209void MCMachOStreamer::EmitLinkerOptions(ArrayRef<std::string> Options) {
210 getAssembler().getLinkerOptions().push_back(Options);
211}
212
Jim Grosbach4b63d2a2012-05-18 19:12:01 +0000213void MCMachOStreamer::EmitDataRegion(MCDataRegionType Kind) {
214 switch (Kind) {
215 case MCDR_DataRegion:
216 EmitDataRegion(DataRegionData::Data);
217 return;
218 case MCDR_DataRegionJT8:
219 EmitDataRegion(DataRegionData::JumpTable8);
220 return;
221 case MCDR_DataRegionJT16:
222 EmitDataRegion(DataRegionData::JumpTable16);
223 return;
224 case MCDR_DataRegionJT32:
225 EmitDataRegion(DataRegionData::JumpTable32);
226 return;
227 case MCDR_DataRegionEnd:
228 EmitDataRegionEnd();
229 return;
230 }
231}
232
Jim Grosbach448334a2014-03-18 22:09:05 +0000233void MCMachOStreamer::EmitVersionMin(MCVersionMinType Kind, unsigned Major,
234 unsigned Minor, unsigned Update) {
235 getAssembler().setVersionMinInfo(Kind, Major, Minor, Update);
236}
237
Daniel Dunbarab14a6f2010-12-29 14:14:06 +0000238void MCMachOStreamer::EmitThumbFunc(MCSymbol *Symbol) {
Jim Grosbach41955ff2010-12-14 18:46:57 +0000239 // Remember that the function is a thumb function. Fixup and relocation
240 // values will need adjusted.
Daniel Dunbarab14a6f2010-12-29 14:14:06 +0000241 getAssembler().setIsThumbFunc(Symbol);
Jim Grosbach5a2c68d2010-11-05 22:08:08 +0000242}
243
Saleem Abdulrasool4208b612013-08-09 01:52:03 +0000244bool MCMachOStreamer::EmitSymbolAttribute(MCSymbol *Symbol,
Chris Lattner685508c2010-01-23 06:39:22 +0000245 MCSymbolAttr Attribute) {
Daniel Dunbar582d6102009-08-24 11:56:58 +0000246 // Indirect symbols are handled differently, to match how 'as' handles
247 // them. This makes writing matching .o files easier.
Chris Lattner685508c2010-01-23 06:39:22 +0000248 if (Attribute == MCSA_IndirectSymbol) {
Daniel Dunbarc2c0bf92009-08-26 00:18:21 +0000249 // Note that we intentionally cannot use the symbol data here; this is
250 // important for matching the string table that 'as' generates.
Daniel Dunbar582d6102009-08-24 11:56:58 +0000251 IndirectSymbolData ISD;
252 ISD.Symbol = Symbol;
Daniel Dunbarb2347fe2010-06-16 20:04:25 +0000253 ISD.SectionData = getCurrentSectionData();
Daniel Dunbar8a3c9d92010-06-16 20:04:22 +0000254 getAssembler().getIndirectSymbols().push_back(ISD);
Saleem Abdulrasool4208b612013-08-09 01:52:03 +0000255 return true;
Daniel Dunbar582d6102009-08-24 11:56:58 +0000256 }
257
Daniel Dunbar04a11582009-08-24 08:40:12 +0000258 // Adding a symbol attribute always introduces the symbol, note that an
Daniel Dunbar6eab7212010-03-10 20:58:29 +0000259 // important side effect of calling getOrCreateSymbolData here is to register
260 // the symbol with the assembler.
Daniel Dunbar8a3c9d92010-06-16 20:04:22 +0000261 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
Daniel Dunbar04a11582009-08-24 08:40:12 +0000262
263 // The implementation of symbol attributes is designed to match 'as', but it
264 // leaves much to desired. It doesn't really make sense to arbitrarily add and
265 // remove flags, but 'as' allows this (in particular, see .desc).
266 //
267 // In the future it might be worth trying to make these operations more well
268 // defined.
Daniel Dunbard1859472009-08-22 11:41:10 +0000269 switch (Attribute) {
Chris Lattner685508c2010-01-23 06:39:22 +0000270 case MCSA_Invalid:
Chris Lattnerbc8f6382010-01-25 18:30:45 +0000271 case MCSA_ELF_TypeFunction:
272 case MCSA_ELF_TypeIndFunction:
273 case MCSA_ELF_TypeObject:
274 case MCSA_ELF_TypeTLS:
275 case MCSA_ELF_TypeCommon:
276 case MCSA_ELF_TypeNoType:
Rafael Espindola4bcf94c2010-11-13 04:51:02 +0000277 case MCSA_ELF_TypeGnuUniqueObject:
Chris Lattner685508c2010-01-23 06:39:22 +0000278 case MCSA_Hidden:
Chandler Carruthb2561022011-07-25 21:21:08 +0000279 case MCSA_IndirectSymbol:
Chris Lattner685508c2010-01-23 06:39:22 +0000280 case MCSA_Internal:
281 case MCSA_Protected:
282 case MCSA_Weak:
283 case MCSA_Local:
Saleem Abdulrasool4208b612013-08-09 01:52:03 +0000284 return false;
Daniel Dunbard1859472009-08-22 11:41:10 +0000285
Chris Lattner685508c2010-01-23 06:39:22 +0000286 case MCSA_Global:
Daniel Dunbar2701eee2009-08-28 07:08:35 +0000287 SD.setExternal(true);
Daniel Dunbar0211a962010-05-17 20:12:31 +0000288 // This effectively clears the undefined lazy bit, in Darwin 'as', although
289 // it isn't very consistent because it implements this as part of symbol
290 // lookup.
291 //
292 // FIXME: Cleanup this code, these bits should be emitted based on semantic
293 // properties, not on the order of definition, etc.
294 SD.setFlags(SD.getFlags() & ~SF_ReferenceTypeUndefinedLazy);
Daniel Dunbard1859472009-08-22 11:41:10 +0000295 break;
Daniel Dunbar04a11582009-08-24 08:40:12 +0000296
Chris Lattner685508c2010-01-23 06:39:22 +0000297 case MCSA_LazyReference:
Daniel Dunbar04a11582009-08-24 08:40:12 +0000298 // FIXME: This requires -dynamic.
299 SD.setFlags(SD.getFlags() | SF_NoDeadStrip);
300 if (Symbol->isUndefined())
301 SD.setFlags(SD.getFlags() | SF_ReferenceTypeUndefinedLazy);
302 break;
303
Daniel Dunbar04a11582009-08-24 08:40:12 +0000304 // Since .reference sets the no dead strip bit, it is equivalent to
305 // .no_dead_strip in practice.
Chris Lattner685508c2010-01-23 06:39:22 +0000306 case MCSA_Reference:
307 case MCSA_NoDeadStrip:
Daniel Dunbar04a11582009-08-24 08:40:12 +0000308 SD.setFlags(SD.getFlags() | SF_NoDeadStrip);
309 break;
310
Kevin Enderby8be14412010-11-19 18:39:33 +0000311 case MCSA_SymbolResolver:
312 SD.setFlags(SD.getFlags() | SF_SymbolResolver);
313 break;
314
Chris Lattner685508c2010-01-23 06:39:22 +0000315 case MCSA_PrivateExtern:
Daniel Dunbar04a11582009-08-24 08:40:12 +0000316 SD.setExternal(true);
317 SD.setPrivateExtern(true);
318 break;
319
Chris Lattner685508c2010-01-23 06:39:22 +0000320 case MCSA_WeakReference:
Daniel Dunbar04a11582009-08-24 08:40:12 +0000321 // FIXME: This requires -dynamic.
322 if (Symbol->isUndefined())
323 SD.setFlags(SD.getFlags() | SF_WeakReference);
324 break;
325
Chris Lattner685508c2010-01-23 06:39:22 +0000326 case MCSA_WeakDefinition:
Daniel Dunbar04a11582009-08-24 08:40:12 +0000327 // FIXME: 'as' enforces that this is defined and global. The manual claims
328 // it has to be in a coalesced section, but this isn't enforced.
329 SD.setFlags(SD.getFlags() | SF_WeakDefinition);
330 break;
Kevin Enderby082d0fd2010-07-08 17:22:42 +0000331
332 case MCSA_WeakDefAutoPrivate:
333 SD.setFlags(SD.getFlags() | SF_WeakDefinition | SF_WeakReference);
334 break;
Daniel Dunbard1859472009-08-22 11:41:10 +0000335 }
Saleem Abdulrasool4208b612013-08-09 01:52:03 +0000336
337 return true;
Daniel Dunbar3016db32009-08-21 09:11:24 +0000338}
339
340void MCMachOStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
Daniel Dunbar04a11582009-08-24 08:40:12 +0000341 // Encode the 'desc' value into the lowest implementation defined bits.
Daniel Dunbar9d40ef12010-05-26 20:37:00 +0000342 assert(DescValue == (DescValue & SF_DescFlagsMask) &&
Daniel Dunbar04a11582009-08-24 08:40:12 +0000343 "Invalid .desc value!");
Daniel Dunbar8a3c9d92010-06-16 20:04:22 +0000344 getAssembler().getOrCreateSymbolData(*Symbol).setFlags(
345 DescValue & SF_DescFlagsMask);
Daniel Dunbar3016db32009-08-21 09:11:24 +0000346}
347
Chris Lattnerb1301f72010-01-23 07:47:02 +0000348void MCMachOStreamer::EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
Daniel Dunbar6a715dc2009-08-30 06:17:16 +0000349 unsigned ByteAlignment) {
Daniel Dunbar2701eee2009-08-28 07:08:35 +0000350 // FIXME: Darwin 'as' does appear to allow redef of a .comm by itself.
351 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
352
Craig Topperbb694de2014-04-13 04:57:38 +0000353 AssignSection(Symbol, nullptr);
Richard Mitton21101b32013-09-19 23:21:01 +0000354
Daniel Dunbar8a3c9d92010-06-16 20:04:22 +0000355 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
Daniel Dunbar2701eee2009-08-28 07:08:35 +0000356 SD.setExternal(true);
Daniel Dunbar6a715dc2009-08-30 06:17:16 +0000357 SD.setCommon(Size, ByteAlignment);
Daniel Dunbar3016db32009-08-21 09:11:24 +0000358}
359
Benjamin Kramer47f9ec92012-09-07 17:25:13 +0000360void MCMachOStreamer::EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size,
361 unsigned ByteAlignment) {
362 // '.lcomm' is equivalent to '.zerofill'.
Rafael Espindolae308c0c2014-01-23 22:49:25 +0000363 return EmitZerofill(getContext().getObjectFileInfo()->getDataBSSSection(),
Benjamin Kramer47f9ec92012-09-07 17:25:13 +0000364 Symbol, Size, ByteAlignment);
365}
366
Daniel Dunbarcf72e1c2009-08-28 05:48:22 +0000367void MCMachOStreamer::EmitZerofill(const MCSection *Section, MCSymbol *Symbol,
Evan Chengf5bd6c62012-06-22 20:14:46 +0000368 uint64_t Size, unsigned ByteAlignment) {
Daniel Dunbar8a3c9d92010-06-16 20:04:22 +0000369 MCSectionData &SectData = getAssembler().getOrCreateSectionData(*Section);
Daniel Dunbar42a39d02009-08-28 05:49:21 +0000370
371 // The symbol may not be present, which only creates the section.
372 if (!Symbol)
373 return;
374
Eric Christopher42ae4592013-08-07 21:13:01 +0000375 // On darwin all virtual sections have zerofill type.
376 assert(Section->isVirtualSection() && "Section does not have zerofill type!");
Daniel Dunbar42a39d02009-08-28 05:49:21 +0000377
378 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
379
Daniel Dunbar8a3c9d92010-06-16 20:04:22 +0000380 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
Daniel Dunbar42a39d02009-08-28 05:49:21 +0000381
Daniel Dunbar51402b72010-05-12 22:51:27 +0000382 // Emit an align fragment if necessary.
383 if (ByteAlignment != 1)
Daniel Dunbarb76df222010-05-12 22:56:23 +0000384 new MCAlignFragment(ByteAlignment, 0, 0, ByteAlignment, &SectData);
Daniel Dunbar51402b72010-05-12 22:51:27 +0000385
Daniel Dunbar4405ffc2010-05-12 22:51:38 +0000386 MCFragment *F = new MCFillFragment(0, 0, Size, &SectData);
Daniel Dunbar42a39d02009-08-28 05:49:21 +0000387 SD.setFragment(F);
388
Richard Mitton21101b32013-09-19 23:21:01 +0000389 AssignSection(Symbol, Section);
Daniel Dunbar42a39d02009-08-28 05:49:21 +0000390
391 // Update the maximum alignment on the zero fill section if necessary.
392 if (ByteAlignment > SectData.getAlignment())
393 SectData.setAlignment(ByteAlignment);
Daniel Dunbar3016db32009-08-21 09:11:24 +0000394}
395
Eric Christopher09d47032010-05-21 23:03:53 +0000396// This should always be called with the thread local bss section. Like the
397// .zerofill directive this doesn't actually switch sections on us.
Eric Christopherfeedc902010-05-18 21:26:41 +0000398void MCMachOStreamer::EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol,
399 uint64_t Size, unsigned ByteAlignment) {
400 EmitZerofill(Section, Symbol, Size, ByteAlignment);
401 return;
Eric Christopher9fb6bb02010-05-14 01:50:28 +0000402}
403
David Woodhouse6f3c73f2014-01-28 23:12:49 +0000404void MCMachOStreamer::EmitInstToData(const MCInst &Inst,
405 const MCSubtargetInfo &STI) {
Daniel Dunbar9d40ef12010-05-26 20:37:00 +0000406 MCDataFragment *DF = getOrCreateDataFragment();
407
408 SmallVector<MCFixup, 4> Fixups;
409 SmallString<256> Code;
410 raw_svector_ostream VecOS(Code);
David Woodhouse9784cef2014-01-28 23:13:07 +0000411 getAssembler().getEmitter().EncodeInstruction(Inst, VecOS, Fixups, STI);
Daniel Dunbar9d40ef12010-05-26 20:37:00 +0000412 VecOS.flush();
413
414 // Add the fixups and data.
415 for (unsigned i = 0, e = Fixups.size(); i != e; ++i) {
416 Fixups[i].setOffset(Fixups[i].getOffset() + DF->getContents().size());
Eli Benderskya31a8942012-12-07 19:13:57 +0000417 DF->getFixups().push_back(Fixups[i]);
Daniel Dunbar9d40ef12010-05-26 20:37:00 +0000418 }
419 DF->getContents().append(Code.begin(), Code.end());
420}
421
Rafael Espindola07082092012-01-07 03:13:18 +0000422void MCMachOStreamer::FinishImpl() {
Rafael Espindola7f4ccce2014-05-12 13:30:10 +0000423 EmitFrames(&getAssembler().getBackend());
Rafael Espindolafc822362011-05-01 15:44:13 +0000424
Daniel Dunbarede8e6d2010-06-16 20:04:32 +0000425 // We have to set the fragment atom associations so we can relax properly for
426 // Mach-O.
427
428 // First, scan the symbol table to build a lookup table from fragments to
429 // defining symbols.
430 DenseMap<const MCFragment*, MCSymbolData*> DefiningSymbolMap;
David Blaikie583a31c2014-04-18 18:24:25 +0000431 for (MCSymbolData &SD : getAssembler().symbols()) {
432 if (getAssembler().isSymbolLinkerVisible(SD.getSymbol()) &&
433 SD.getFragment()) {
Daniel Dunbarede8e6d2010-06-16 20:04:32 +0000434 // An atom defining symbol should never be internal to a fragment.
David Blaikie583a31c2014-04-18 18:24:25 +0000435 assert(SD.getOffset() == 0 && "Invalid offset in atom defining symbol!");
436 DefiningSymbolMap[SD.getFragment()] = &SD;
Daniel Dunbarede8e6d2010-06-16 20:04:32 +0000437 }
438 }
439
440 // Set the fragment atom associations by tracking the last seen atom defining
441 // symbol.
442 for (MCAssembler::iterator it = getAssembler().begin(),
443 ie = getAssembler().end(); it != ie; ++it) {
Craig Topperbb694de2014-04-13 04:57:38 +0000444 MCSymbolData *CurrentAtom = nullptr;
Daniel Dunbarede8e6d2010-06-16 20:04:32 +0000445 for (MCSectionData::iterator it2 = it->begin(),
446 ie2 = it->end(); it2 != ie2; ++it2) {
447 if (MCSymbolData *SD = DefiningSymbolMap.lookup(it2))
448 CurrentAtom = SD;
449 it2->setAtom(CurrentAtom);
450 }
451 }
452
Rafael Espindola07082092012-01-07 03:13:18 +0000453 this->MCObjectStreamer::FinishImpl();
Daniel Dunbarede8e6d2010-06-16 20:04:32 +0000454}
455
Evan Cheng5928e692011-07-25 23:24:55 +0000456MCStreamer *llvm::createMachOStreamer(MCContext &Context, MCAsmBackend &MAB,
Daniel Dunbard821f4a2010-03-25 22:49:09 +0000457 raw_ostream &OS, MCCodeEmitter *CE,
Tim Northoverc3988b42014-03-29 07:05:06 +0000458 bool RelaxAll,
459 bool LabelSections) {
460 MCMachOStreamer *S = new MCMachOStreamer(Context, MAB, OS, CE, LabelSections);
Daniel Dunbard821f4a2010-03-25 22:49:09 +0000461 if (RelaxAll)
462 S->getAssembler().setRelaxAll(true);
463 return S;
Daniel Dunbar3016db32009-08-21 09:11:24 +0000464}