blob: 742f7846496b9e428587fa8397f0a19b5bbc795e [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"
Chandler Carruthed0881b2012-12-03 16:50:05 +000020#include "llvm/MC/MCMachOSymbolFlags.h"
Rafael Espindolae308c0c2014-01-23 22:49:25 +000021#include "llvm/MC/MCObjectFileInfo.h"
Daniel Dunbar8a3c9d92010-06-16 20:04:22 +000022#include "llvm/MC/MCObjectStreamer.h"
Daniel Dunbar3016db32009-08-21 09:11:24 +000023#include "llvm/MC/MCSection.h"
Kevin Enderby7221b762010-08-09 22:52:14 +000024#include "llvm/MC/MCSectionMachO.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000025#include "llvm/MC/MCSymbol.h"
Kevin Enderbye46564a2010-09-30 16:52:03 +000026#include "llvm/Support/Dwarf.h"
Daniel Dunbar3016db32009-08-21 09:11:24 +000027#include "llvm/Support/ErrorHandling.h"
Daniel Dunbarc7c5f9f2009-08-27 08:17:51 +000028#include "llvm/Support/raw_ostream.h"
Daniel Dunbarde04b3f2010-03-23 05:09:03 +000029
Daniel Dunbar3016db32009-08-21 09:11:24 +000030using namespace llvm;
31
32namespace {
33
Daniel Dunbar8a3c9d92010-06-16 20:04:22 +000034class MCMachOStreamer : public MCObjectStreamer {
Daniel Dunbar04a11582009-08-24 08:40:12 +000035private:
Tim Northoverc3988b42014-03-29 07:05:06 +000036 /// LabelSections - true if each section change should emit a linker local
37 /// label for use in relocations for assembler local references. Obviates the
38 /// need for local relocations. False by default.
39 bool LabelSections;
40
41 /// HasSectionLabel - map of which sections have already had a non-local
42 /// label emitted to them. Used so we don't emit extraneous linker local
43 /// labels in the middle of the section.
44 DenseMap<const MCSection*, bool> HasSectionLabel;
45
Craig Topper34a61bc2014-03-08 07:02:02 +000046 void EmitInstToData(const MCInst &Inst, const MCSubtargetInfo &STI) override;
Daniel Dunbar9d40ef12010-05-26 20:37:00 +000047
Jim Grosbach4b63d2a2012-05-18 19:12:01 +000048 void EmitDataRegion(DataRegionData::KindTy Kind);
49 void EmitDataRegionEnd();
Tim Northoverc3988b42014-03-29 07:05:06 +000050
Daniel Dunbar3016db32009-08-21 09:11:24 +000051public:
Chandler Carruthde093ef2013-01-31 23:29:57 +000052 MCMachOStreamer(MCContext &Context, MCAsmBackend &MAB, raw_ostream &OS,
Tim Northoverc3988b42014-03-29 07:05:06 +000053 MCCodeEmitter *Emitter, bool label)
54 : MCObjectStreamer(Context, MAB, OS, Emitter),
55 LabelSections(label) {}
Daniel Dunbard821f4a2010-03-25 22:49:09 +000056
Daniel Dunbar3016db32009-08-21 09:11:24 +000057 /// @name MCStreamer Interface
58 /// @{
59
Tim Northoverc3988b42014-03-29 07:05:06 +000060 void ChangeSection(const MCSection *Sect, const MCExpr *Subsect) override;
Craig Topper34a61bc2014-03-08 07:02:02 +000061 void EmitLabel(MCSymbol *Symbol) override;
62 void EmitDebugLabel(MCSymbol *Symbol) override;
63 void EmitEHSymAttributes(const MCSymbol *Symbol, MCSymbol *EHSymbol) override;
64 void EmitAssemblerFlag(MCAssemblerFlag Flag) override;
65 void EmitLinkerOptions(ArrayRef<std::string> Options) override;
66 void EmitDataRegion(MCDataRegionType Kind) override;
Jim Grosbach448334a2014-03-18 22:09:05 +000067 void EmitVersionMin(MCVersionMinType Kind, unsigned Major,
68 unsigned Minor, unsigned Update) override;
Craig Topper34a61bc2014-03-08 07:02:02 +000069 void EmitThumbFunc(MCSymbol *Func) override;
70 bool EmitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute) override;
71 void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) override;
72 void EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
73 unsigned ByteAlignment) override;
74 void BeginCOFFSymbolDef(const MCSymbol *Symbol) override {
Craig Toppera2886c22012-02-07 05:05:23 +000075 llvm_unreachable("macho doesn't support this directive");
Chris Lattner72afa952010-05-08 19:54:22 +000076 }
Craig Topper34a61bc2014-03-08 07:02:02 +000077 void EmitCOFFSymbolStorageClass(int StorageClass) override {
Craig Toppera2886c22012-02-07 05:05:23 +000078 llvm_unreachable("macho doesn't support this directive");
Chris Lattner72afa952010-05-08 19:54:22 +000079 }
Craig Topper34a61bc2014-03-08 07:02:02 +000080 void EmitCOFFSymbolType(int Type) 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 EndCOFFSymbolDef() 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 EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) override {
Craig Toppera2886c22012-02-07 05:05:23 +000087 llvm_unreachable("macho doesn't support this directive");
Chris Lattner91dac6d2010-01-25 07:52:13 +000088 }
Craig Topper34a61bc2014-03-08 07:02:02 +000089 void EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size,
90 unsigned ByteAlignment) override;
91 void EmitZerofill(const MCSection *Section, MCSymbol *Symbol = 0,
92 uint64_t Size = 0, unsigned ByteAlignment = 0) override;
Eric Christopherfeedc902010-05-18 21:26:41 +000093 virtual void EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol,
Craig Topper34a61bc2014-03-08 07:02:02 +000094 uint64_t Size, unsigned ByteAlignment = 0) override;
Daniel Dunbar9d40ef12010-05-26 20:37:00 +000095
Craig Topper34a61bc2014-03-08 07:02:02 +000096 void EmitFileDirective(StringRef Filename) override {
Daniel Dunbar6b4391a2010-07-19 20:44:20 +000097 // FIXME: Just ignore the .file; it isn't important enough to fail the
98 // entire assembly.
99
Rafael Espindola5645bad2013-10-16 01:05:45 +0000100 // report_fatal_error("unsupported directive: '.file'");
101 }
102
Craig Topper34a61bc2014-03-08 07:02:02 +0000103 void EmitIdent(StringRef IdentString) override {
Rafael Espindola5645bad2013-10-16 01:05:45 +0000104 llvm_unreachable("macho doesn't support this directive");
Chris Lattner601ef332010-01-25 18:58:59 +0000105 }
Daniel Dunbar9d40ef12010-05-26 20:37:00 +0000106
Craig Topper34a61bc2014-03-08 07:02:02 +0000107 void FinishImpl() override;
Daniel Dunbar3016db32009-08-21 09:11:24 +0000108};
109
110} // end anonymous namespace.
111
Tim Northoverc3988b42014-03-29 07:05:06 +0000112void MCMachOStreamer::ChangeSection(const MCSection *Section,
113 const MCExpr *Subsection) {
114 // Change the section normally.
115 MCObjectStreamer::ChangeSection(Section, Subsection);
116 // Output a linker-local symbol so we don't need section-relative local
117 // relocations. The linker hates us when we do that.
118 if (LabelSections && !HasSectionLabel[Section]) {
119 MCSymbol *Label = getContext().CreateLinkerPrivateTempSymbol();
120 EmitLabel(Label);
121 HasSectionLabel[Section] = true;
122 }
123}
124
Rafael Espindola349c3292011-04-28 12:50:37 +0000125void MCMachOStreamer::EmitEHSymAttributes(const MCSymbol *Symbol,
126 MCSymbol *EHSymbol) {
127 MCSymbolData &SD =
128 getAssembler().getOrCreateSymbolData(*Symbol);
129 if (SD.isExternal())
130 EmitSymbolAttribute(EHSymbol, MCSA_Global);
131 if (SD.getFlags() & SF_WeakDefinition)
132 EmitSymbolAttribute(EHSymbol, MCSA_WeakDefinition);
Rafael Espindola0b474c22011-04-30 16:22:46 +0000133 if (SD.isPrivateExtern())
134 EmitSymbolAttribute(EHSymbol, MCSA_PrivateExtern);
Rafael Espindola349c3292011-04-28 12:50:37 +0000135}
136
Daniel Dunbar3016db32009-08-21 09:11:24 +0000137void MCMachOStreamer::EmitLabel(MCSymbol *Symbol) {
Rafael Espindola16795802010-11-28 16:22:59 +0000138 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
Rafael Espindola16795802010-11-28 16:22:59 +0000139
Rafael Espindolae5b74152010-11-28 17:18:55 +0000140 // isSymbolLinkerVisible uses the section.
Richard Mitton21101b32013-09-19 23:21:01 +0000141 AssignSection(Symbol, getCurrentSection().first);
Daniel Dunbarede8e6d2010-06-16 20:04:32 +0000142 // We have to create a new fragment if this is an atom defining symbol,
143 // fragments cannot span atoms.
Rafael Espindolae5b74152010-11-28 17:18:55 +0000144 if (getAssembler().isSymbolLinkerVisible(*Symbol))
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000145 insert(new MCDataFragment());
Daniel Dunbaraadb2ca2010-05-10 22:45:09 +0000146
Rafael Espindolae5b74152010-11-28 17:18:55 +0000147 MCObjectStreamer::EmitLabel(Symbol);
Daniel Dunbar04a11582009-08-24 08:40:12 +0000148
Rafael Espindolae5b74152010-11-28 17:18:55 +0000149 MCSymbolData &SD = getAssembler().getSymbolData(*Symbol);
Daniel Dunbar0211a962010-05-17 20:12:31 +0000150 // This causes the reference type flag to be cleared. Darwin 'as' was "trying"
151 // to clear the weak reference and weak definition bits too, but the
152 // implementation was buggy. For now we just try to match 'as', for
153 // diffability.
154 //
155 // FIXME: Cleanup this code, these bits should be emitted based on semantic
156 // properties, not on the order of definition, etc.
157 SD.setFlags(SD.getFlags() & ~SF_ReferenceTypeMask);
Daniel Dunbar3016db32009-08-21 09:11:24 +0000158}
159
Reed Kotleraee4d5d12012-12-16 04:00:45 +0000160void MCMachOStreamer::EmitDebugLabel(MCSymbol *Symbol) {
161 EmitLabel(Symbol);
162}
Jim Grosbach4b63d2a2012-05-18 19:12:01 +0000163void MCMachOStreamer::EmitDataRegion(DataRegionData::KindTy Kind) {
Jim Grosbach745c52d2012-10-01 22:20:54 +0000164 if (!getAssembler().getBackend().hasDataInCodeSupport())
165 return;
Jim Grosbach4b63d2a2012-05-18 19:12:01 +0000166 // Create a temporary label to mark the start of the data region.
167 MCSymbol *Start = getContext().CreateTempSymbol();
168 EmitLabel(Start);
169 // Record the region for the object writer to use.
170 DataRegionData Data = { Kind, Start, NULL };
171 std::vector<DataRegionData> &Regions = getAssembler().getDataRegions();
172 Regions.push_back(Data);
173}
174
175void MCMachOStreamer::EmitDataRegionEnd() {
Jim Grosbach745c52d2012-10-01 22:20:54 +0000176 if (!getAssembler().getBackend().hasDataInCodeSupport())
177 return;
Jim Grosbach4b63d2a2012-05-18 19:12:01 +0000178 std::vector<DataRegionData> &Regions = getAssembler().getDataRegions();
179 assert(Regions.size() && "Mismatched .end_data_region!");
180 DataRegionData &Data = Regions.back();
181 assert(Data.End == NULL && "Mismatched .end_data_region!");
182 // Create a temporary label to mark the end of the data region.
183 Data.End = getContext().CreateTempSymbol();
184 EmitLabel(Data.End);
185}
186
Chris Lattner685508c2010-01-23 06:39:22 +0000187void MCMachOStreamer::EmitAssemblerFlag(MCAssemblerFlag Flag) {
Jim Grosbach87055ed2010-12-08 01:16:55 +0000188 // Let the target do whatever target specific stuff it needs to do.
Jim Grosbachaba3de92012-01-18 18:52:16 +0000189 getAssembler().getBackend().handleAssemblerFlag(Flag);
Jim Grosbach87055ed2010-12-08 01:16:55 +0000190 // Do any generic stuff we need to do.
Daniel Dunbare2697732009-08-26 21:22:22 +0000191 switch (Flag) {
Jim Grosbach5a2c68d2010-11-05 22:08:08 +0000192 case MCAF_SyntaxUnified: return; // no-op here.
Evan Cheng481ebb02011-07-27 00:38:12 +0000193 case MCAF_Code16: return; // Change parsing mode; no-op here.
194 case MCAF_Code32: return; // Change parsing mode; no-op here.
195 case MCAF_Code64: return; // Change parsing mode; no-op here.
Chris Lattner685508c2010-01-23 06:39:22 +0000196 case MCAF_SubsectionsViaSymbols:
Daniel Dunbar8a3c9d92010-06-16 20:04:22 +0000197 getAssembler().setSubsectionsViaSymbols(true);
Daniel Dunbar2a2459a2009-08-28 07:08:47 +0000198 return;
Daniel Dunbare2697732009-08-26 21:22:22 +0000199 }
Daniel Dunbar3016db32009-08-21 09:11:24 +0000200}
201
Daniel Dunbareec0f322013-01-18 01:26:07 +0000202void MCMachOStreamer::EmitLinkerOptions(ArrayRef<std::string> Options) {
203 getAssembler().getLinkerOptions().push_back(Options);
204}
205
Jim Grosbach4b63d2a2012-05-18 19:12:01 +0000206void MCMachOStreamer::EmitDataRegion(MCDataRegionType Kind) {
207 switch (Kind) {
208 case MCDR_DataRegion:
209 EmitDataRegion(DataRegionData::Data);
210 return;
211 case MCDR_DataRegionJT8:
212 EmitDataRegion(DataRegionData::JumpTable8);
213 return;
214 case MCDR_DataRegionJT16:
215 EmitDataRegion(DataRegionData::JumpTable16);
216 return;
217 case MCDR_DataRegionJT32:
218 EmitDataRegion(DataRegionData::JumpTable32);
219 return;
220 case MCDR_DataRegionEnd:
221 EmitDataRegionEnd();
222 return;
223 }
224}
225
Jim Grosbach448334a2014-03-18 22:09:05 +0000226void MCMachOStreamer::EmitVersionMin(MCVersionMinType Kind, unsigned Major,
227 unsigned Minor, unsigned Update) {
228 getAssembler().setVersionMinInfo(Kind, Major, Minor, Update);
229}
230
Daniel Dunbarab14a6f2010-12-29 14:14:06 +0000231void MCMachOStreamer::EmitThumbFunc(MCSymbol *Symbol) {
Jim Grosbach41955ff2010-12-14 18:46:57 +0000232 // Remember that the function is a thumb function. Fixup and relocation
233 // values will need adjusted.
Daniel Dunbarab14a6f2010-12-29 14:14:06 +0000234 getAssembler().setIsThumbFunc(Symbol);
235
236 // Mark the thumb bit on the symbol.
237 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
238 SD.setFlags(SD.getFlags() | SF_ThumbFunc);
Jim Grosbach5a2c68d2010-11-05 22:08:08 +0000239}
240
Saleem Abdulrasool4208b612013-08-09 01:52:03 +0000241bool MCMachOStreamer::EmitSymbolAttribute(MCSymbol *Symbol,
Chris Lattner685508c2010-01-23 06:39:22 +0000242 MCSymbolAttr Attribute) {
Daniel Dunbar582d6102009-08-24 11:56:58 +0000243 // Indirect symbols are handled differently, to match how 'as' handles
244 // them. This makes writing matching .o files easier.
Chris Lattner685508c2010-01-23 06:39:22 +0000245 if (Attribute == MCSA_IndirectSymbol) {
Daniel Dunbarc2c0bf92009-08-26 00:18:21 +0000246 // Note that we intentionally cannot use the symbol data here; this is
247 // important for matching the string table that 'as' generates.
Daniel Dunbar582d6102009-08-24 11:56:58 +0000248 IndirectSymbolData ISD;
249 ISD.Symbol = Symbol;
Daniel Dunbarb2347fe2010-06-16 20:04:25 +0000250 ISD.SectionData = getCurrentSectionData();
Daniel Dunbar8a3c9d92010-06-16 20:04:22 +0000251 getAssembler().getIndirectSymbols().push_back(ISD);
Saleem Abdulrasool4208b612013-08-09 01:52:03 +0000252 return true;
Daniel Dunbar582d6102009-08-24 11:56:58 +0000253 }
254
Daniel Dunbar04a11582009-08-24 08:40:12 +0000255 // Adding a symbol attribute always introduces the symbol, note that an
Daniel Dunbar6eab7212010-03-10 20:58:29 +0000256 // important side effect of calling getOrCreateSymbolData here is to register
257 // the symbol with the assembler.
Daniel Dunbar8a3c9d92010-06-16 20:04:22 +0000258 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
Daniel Dunbar04a11582009-08-24 08:40:12 +0000259
260 // The implementation of symbol attributes is designed to match 'as', but it
261 // leaves much to desired. It doesn't really make sense to arbitrarily add and
262 // remove flags, but 'as' allows this (in particular, see .desc).
263 //
264 // In the future it might be worth trying to make these operations more well
265 // defined.
Daniel Dunbard1859472009-08-22 11:41:10 +0000266 switch (Attribute) {
Chris Lattner685508c2010-01-23 06:39:22 +0000267 case MCSA_Invalid:
Chris Lattnerbc8f6382010-01-25 18:30:45 +0000268 case MCSA_ELF_TypeFunction:
269 case MCSA_ELF_TypeIndFunction:
270 case MCSA_ELF_TypeObject:
271 case MCSA_ELF_TypeTLS:
272 case MCSA_ELF_TypeCommon:
273 case MCSA_ELF_TypeNoType:
Rafael Espindola4bcf94c2010-11-13 04:51:02 +0000274 case MCSA_ELF_TypeGnuUniqueObject:
Chris Lattner685508c2010-01-23 06:39:22 +0000275 case MCSA_Hidden:
Chandler Carruthb2561022011-07-25 21:21:08 +0000276 case MCSA_IndirectSymbol:
Chris Lattner685508c2010-01-23 06:39:22 +0000277 case MCSA_Internal:
278 case MCSA_Protected:
279 case MCSA_Weak:
280 case MCSA_Local:
Saleem Abdulrasool4208b612013-08-09 01:52:03 +0000281 return false;
Daniel Dunbard1859472009-08-22 11:41:10 +0000282
Chris Lattner685508c2010-01-23 06:39:22 +0000283 case MCSA_Global:
Daniel Dunbar2701eee2009-08-28 07:08:35 +0000284 SD.setExternal(true);
Daniel Dunbar0211a962010-05-17 20:12:31 +0000285 // This effectively clears the undefined lazy bit, in Darwin 'as', although
286 // it isn't very consistent because it implements this as part of symbol
287 // lookup.
288 //
289 // FIXME: Cleanup this code, these bits should be emitted based on semantic
290 // properties, not on the order of definition, etc.
291 SD.setFlags(SD.getFlags() & ~SF_ReferenceTypeUndefinedLazy);
Daniel Dunbard1859472009-08-22 11:41:10 +0000292 break;
Daniel Dunbar04a11582009-08-24 08:40:12 +0000293
Chris Lattner685508c2010-01-23 06:39:22 +0000294 case MCSA_LazyReference:
Daniel Dunbar04a11582009-08-24 08:40:12 +0000295 // FIXME: This requires -dynamic.
296 SD.setFlags(SD.getFlags() | SF_NoDeadStrip);
297 if (Symbol->isUndefined())
298 SD.setFlags(SD.getFlags() | SF_ReferenceTypeUndefinedLazy);
299 break;
300
Daniel Dunbar04a11582009-08-24 08:40:12 +0000301 // Since .reference sets the no dead strip bit, it is equivalent to
302 // .no_dead_strip in practice.
Chris Lattner685508c2010-01-23 06:39:22 +0000303 case MCSA_Reference:
304 case MCSA_NoDeadStrip:
Daniel Dunbar04a11582009-08-24 08:40:12 +0000305 SD.setFlags(SD.getFlags() | SF_NoDeadStrip);
306 break;
307
Kevin Enderby8be14412010-11-19 18:39:33 +0000308 case MCSA_SymbolResolver:
309 SD.setFlags(SD.getFlags() | SF_SymbolResolver);
310 break;
311
Chris Lattner685508c2010-01-23 06:39:22 +0000312 case MCSA_PrivateExtern:
Daniel Dunbar04a11582009-08-24 08:40:12 +0000313 SD.setExternal(true);
314 SD.setPrivateExtern(true);
315 break;
316
Chris Lattner685508c2010-01-23 06:39:22 +0000317 case MCSA_WeakReference:
Daniel Dunbar04a11582009-08-24 08:40:12 +0000318 // FIXME: This requires -dynamic.
319 if (Symbol->isUndefined())
320 SD.setFlags(SD.getFlags() | SF_WeakReference);
321 break;
322
Chris Lattner685508c2010-01-23 06:39:22 +0000323 case MCSA_WeakDefinition:
Daniel Dunbar04a11582009-08-24 08:40:12 +0000324 // FIXME: 'as' enforces that this is defined and global. The manual claims
325 // it has to be in a coalesced section, but this isn't enforced.
326 SD.setFlags(SD.getFlags() | SF_WeakDefinition);
327 break;
Kevin Enderby082d0fd2010-07-08 17:22:42 +0000328
329 case MCSA_WeakDefAutoPrivate:
330 SD.setFlags(SD.getFlags() | SF_WeakDefinition | SF_WeakReference);
331 break;
Daniel Dunbard1859472009-08-22 11:41:10 +0000332 }
Saleem Abdulrasool4208b612013-08-09 01:52:03 +0000333
334 return true;
Daniel Dunbar3016db32009-08-21 09:11:24 +0000335}
336
337void MCMachOStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
Daniel Dunbar04a11582009-08-24 08:40:12 +0000338 // Encode the 'desc' value into the lowest implementation defined bits.
Daniel Dunbar9d40ef12010-05-26 20:37:00 +0000339 assert(DescValue == (DescValue & SF_DescFlagsMask) &&
Daniel Dunbar04a11582009-08-24 08:40:12 +0000340 "Invalid .desc value!");
Daniel Dunbar8a3c9d92010-06-16 20:04:22 +0000341 getAssembler().getOrCreateSymbolData(*Symbol).setFlags(
342 DescValue & SF_DescFlagsMask);
Daniel Dunbar3016db32009-08-21 09:11:24 +0000343}
344
Chris Lattnerb1301f72010-01-23 07:47:02 +0000345void MCMachOStreamer::EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
Daniel Dunbar6a715dc2009-08-30 06:17:16 +0000346 unsigned ByteAlignment) {
Daniel Dunbar2701eee2009-08-28 07:08:35 +0000347 // FIXME: Darwin 'as' does appear to allow redef of a .comm by itself.
348 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
349
Richard Mitton21101b32013-09-19 23:21:01 +0000350 AssignSection(Symbol, NULL);
351
Daniel Dunbar8a3c9d92010-06-16 20:04:22 +0000352 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
Daniel Dunbar2701eee2009-08-28 07:08:35 +0000353 SD.setExternal(true);
Daniel Dunbar6a715dc2009-08-30 06:17:16 +0000354 SD.setCommon(Size, ByteAlignment);
Daniel Dunbar3016db32009-08-21 09:11:24 +0000355}
356
Benjamin Kramer47f9ec92012-09-07 17:25:13 +0000357void MCMachOStreamer::EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size,
358 unsigned ByteAlignment) {
359 // '.lcomm' is equivalent to '.zerofill'.
Rafael Espindolae308c0c2014-01-23 22:49:25 +0000360 return EmitZerofill(getContext().getObjectFileInfo()->getDataBSSSection(),
Benjamin Kramer47f9ec92012-09-07 17:25:13 +0000361 Symbol, Size, ByteAlignment);
362}
363
Daniel Dunbarcf72e1c2009-08-28 05:48:22 +0000364void MCMachOStreamer::EmitZerofill(const MCSection *Section, MCSymbol *Symbol,
Evan Chengf5bd6c62012-06-22 20:14:46 +0000365 uint64_t Size, unsigned ByteAlignment) {
Daniel Dunbar8a3c9d92010-06-16 20:04:22 +0000366 MCSectionData &SectData = getAssembler().getOrCreateSectionData(*Section);
Daniel Dunbar42a39d02009-08-28 05:49:21 +0000367
368 // The symbol may not be present, which only creates the section.
369 if (!Symbol)
370 return;
371
Eric Christopher42ae4592013-08-07 21:13:01 +0000372 // On darwin all virtual sections have zerofill type.
373 assert(Section->isVirtualSection() && "Section does not have zerofill type!");
Daniel Dunbar42a39d02009-08-28 05:49:21 +0000374
375 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
376
Daniel Dunbar8a3c9d92010-06-16 20:04:22 +0000377 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
Daniel Dunbar42a39d02009-08-28 05:49:21 +0000378
Daniel Dunbar51402b72010-05-12 22:51:27 +0000379 // Emit an align fragment if necessary.
380 if (ByteAlignment != 1)
Daniel Dunbarb76df222010-05-12 22:56:23 +0000381 new MCAlignFragment(ByteAlignment, 0, 0, ByteAlignment, &SectData);
Daniel Dunbar51402b72010-05-12 22:51:27 +0000382
Daniel Dunbar4405ffc2010-05-12 22:51:38 +0000383 MCFragment *F = new MCFillFragment(0, 0, Size, &SectData);
Daniel Dunbar42a39d02009-08-28 05:49:21 +0000384 SD.setFragment(F);
385
Richard Mitton21101b32013-09-19 23:21:01 +0000386 AssignSection(Symbol, Section);
Daniel Dunbar42a39d02009-08-28 05:49:21 +0000387
388 // Update the maximum alignment on the zero fill section if necessary.
389 if (ByteAlignment > SectData.getAlignment())
390 SectData.setAlignment(ByteAlignment);
Daniel Dunbar3016db32009-08-21 09:11:24 +0000391}
392
Eric Christopher09d47032010-05-21 23:03:53 +0000393// This should always be called with the thread local bss section. Like the
394// .zerofill directive this doesn't actually switch sections on us.
Eric Christopherfeedc902010-05-18 21:26:41 +0000395void MCMachOStreamer::EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol,
396 uint64_t Size, unsigned ByteAlignment) {
397 EmitZerofill(Section, Symbol, Size, ByteAlignment);
398 return;
Eric Christopher9fb6bb02010-05-14 01:50:28 +0000399}
400
David Woodhouse6f3c73f2014-01-28 23:12:49 +0000401void MCMachOStreamer::EmitInstToData(const MCInst &Inst,
402 const MCSubtargetInfo &STI) {
Daniel Dunbar9d40ef12010-05-26 20:37:00 +0000403 MCDataFragment *DF = getOrCreateDataFragment();
404
405 SmallVector<MCFixup, 4> Fixups;
406 SmallString<256> Code;
407 raw_svector_ostream VecOS(Code);
David Woodhouse9784cef2014-01-28 23:13:07 +0000408 getAssembler().getEmitter().EncodeInstruction(Inst, VecOS, Fixups, STI);
Daniel Dunbar9d40ef12010-05-26 20:37:00 +0000409 VecOS.flush();
410
411 // Add the fixups and data.
412 for (unsigned i = 0, e = Fixups.size(); i != e; ++i) {
413 Fixups[i].setOffset(Fixups[i].getOffset() + DF->getContents().size());
Eli Benderskya31a8942012-12-07 19:13:57 +0000414 DF->getFixups().push_back(Fixups[i]);
Daniel Dunbar9d40ef12010-05-26 20:37:00 +0000415 }
416 DF->getContents().append(Code.begin(), Code.end());
417}
418
Rafael Espindola07082092012-01-07 03:13:18 +0000419void MCMachOStreamer::FinishImpl() {
Bill Wendling550c76d2013-09-09 19:48:37 +0000420 EmitFrames(&getAssembler().getBackend(), true);
Rafael Espindolafc822362011-05-01 15:44:13 +0000421
Daniel Dunbarede8e6d2010-06-16 20:04:32 +0000422 // We have to set the fragment atom associations so we can relax properly for
423 // Mach-O.
424
425 // First, scan the symbol table to build a lookup table from fragments to
426 // defining symbols.
427 DenseMap<const MCFragment*, MCSymbolData*> DefiningSymbolMap;
428 for (MCAssembler::symbol_iterator it = getAssembler().symbol_begin(),
429 ie = getAssembler().symbol_end(); it != ie; ++it) {
430 if (getAssembler().isSymbolLinkerVisible(it->getSymbol()) &&
431 it->getFragment()) {
432 // An atom defining symbol should never be internal to a fragment.
433 assert(it->getOffset() == 0 && "Invalid offset in atom defining symbol!");
434 DefiningSymbolMap[it->getFragment()] = it;
435 }
436 }
437
438 // Set the fragment atom associations by tracking the last seen atom defining
439 // symbol.
440 for (MCAssembler::iterator it = getAssembler().begin(),
441 ie = getAssembler().end(); it != ie; ++it) {
442 MCSymbolData *CurrentAtom = 0;
443 for (MCSectionData::iterator it2 = it->begin(),
444 ie2 = it->end(); it2 != ie2; ++it2) {
445 if (MCSymbolData *SD = DefiningSymbolMap.lookup(it2))
446 CurrentAtom = SD;
447 it2->setAtom(CurrentAtom);
448 }
449 }
450
Rafael Espindola07082092012-01-07 03:13:18 +0000451 this->MCObjectStreamer::FinishImpl();
Daniel Dunbarede8e6d2010-06-16 20:04:32 +0000452}
453
Evan Cheng5928e692011-07-25 23:24:55 +0000454MCStreamer *llvm::createMachOStreamer(MCContext &Context, MCAsmBackend &MAB,
Daniel Dunbard821f4a2010-03-25 22:49:09 +0000455 raw_ostream &OS, MCCodeEmitter *CE,
Tim Northoverc3988b42014-03-29 07:05:06 +0000456 bool RelaxAll,
457 bool LabelSections) {
458 MCMachOStreamer *S = new MCMachOStreamer(Context, MAB, OS, CE, LabelSections);
Daniel Dunbard821f4a2010-03-25 22:49:09 +0000459 if (RelaxAll)
460 S->getAssembler().setRelaxAll(true);
461 return S;
Daniel Dunbar3016db32009-08-21 09:11:24 +0000462}