blob: 6fe466376671d04ca97f1102bd18cf71c140f99a [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"
Chandler Carruthed0881b2012-12-03 16:50:05 +000011#include "llvm/MC/MCAsmBackend.h"
Daniel Dunbar3016db32009-08-21 09:11:24 +000012#include "llvm/MC/MCAssembler.h"
Daniel Dunbarc7c5f9f2009-08-27 08:17:51 +000013#include "llvm/MC/MCCodeEmitter.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000014#include "llvm/MC/MCContext.h"
15#include "llvm/MC/MCDwarf.h"
Daniel Dunbar73da11e2009-08-31 08:08:38 +000016#include "llvm/MC/MCExpr.h"
Daniel Dunbarc7c5f9f2009-08-27 08:17:51 +000017#include "llvm/MC/MCInst.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000018#include "llvm/MC/MCMachOSymbolFlags.h"
Rafael Espindolae308c0c2014-01-23 22:49:25 +000019#include "llvm/MC/MCObjectFileInfo.h"
Daniel Dunbar8a3c9d92010-06-16 20:04:22 +000020#include "llvm/MC/MCObjectStreamer.h"
Daniel Dunbar3016db32009-08-21 09:11:24 +000021#include "llvm/MC/MCSection.h"
Kevin Enderby7221b762010-08-09 22:52:14 +000022#include "llvm/MC/MCSectionMachO.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000023#include "llvm/MC/MCSymbol.h"
Kevin Enderbye46564a2010-09-30 16:52:03 +000024#include "llvm/Support/Dwarf.h"
Daniel Dunbar3016db32009-08-21 09:11:24 +000025#include "llvm/Support/ErrorHandling.h"
Daniel Dunbarc7c5f9f2009-08-27 08:17:51 +000026#include "llvm/Support/raw_ostream.h"
Daniel Dunbarde04b3f2010-03-23 05:09:03 +000027
Daniel Dunbar3016db32009-08-21 09:11:24 +000028using namespace llvm;
29
30namespace {
31
Daniel Dunbar8a3c9d92010-06-16 20:04:22 +000032class MCMachOStreamer : public MCObjectStreamer {
Daniel Dunbar04a11582009-08-24 08:40:12 +000033private:
Rafael Espindola72b54882010-11-01 16:27:31 +000034 virtual void EmitInstToData(const MCInst &Inst);
Daniel Dunbar9d40ef12010-05-26 20:37:00 +000035
Jim Grosbach4b63d2a2012-05-18 19:12:01 +000036 void EmitDataRegion(DataRegionData::KindTy Kind);
37 void EmitDataRegionEnd();
Daniel Dunbar3016db32009-08-21 09:11:24 +000038public:
Chandler Carruthde093ef2013-01-31 23:29:57 +000039 MCMachOStreamer(MCContext &Context, MCAsmBackend &MAB, raw_ostream &OS,
40 MCCodeEmitter *Emitter)
Rafael Espindolaa17151a2013-10-08 13:08:17 +000041 : MCObjectStreamer(Context, 0, MAB, OS, Emitter) {}
Daniel Dunbard821f4a2010-03-25 22:49:09 +000042
Daniel Dunbar3016db32009-08-21 09:11:24 +000043 /// @name MCStreamer Interface
44 /// @{
45
Rafael Espindolaf667d922010-09-15 21:48:40 +000046 virtual void InitSections();
Eli Benderskycbb25142013-01-14 19:04:57 +000047 virtual void InitToTextSection();
Daniel Dunbar3016db32009-08-21 09:11:24 +000048 virtual void EmitLabel(MCSymbol *Symbol);
Reed Kotleraee4d5d12012-12-16 04:00:45 +000049 virtual void EmitDebugLabel(MCSymbol *Symbol);
Rafael Espindola349c3292011-04-28 12:50:37 +000050 virtual void EmitEHSymAttributes(const MCSymbol *Symbol,
51 MCSymbol *EHSymbol);
Chris Lattner685508c2010-01-23 06:39:22 +000052 virtual void EmitAssemblerFlag(MCAssemblerFlag Flag);
Daniel Dunbareec0f322013-01-18 01:26:07 +000053 virtual void EmitLinkerOptions(ArrayRef<std::string> Options);
Jim Grosbach4b63d2a2012-05-18 19:12:01 +000054 virtual void EmitDataRegion(MCDataRegionType Kind);
Jim Grosbach5a2c68d2010-11-05 22:08:08 +000055 virtual void EmitThumbFunc(MCSymbol *Func);
Saleem Abdulrasool4208b612013-08-09 01:52:03 +000056 virtual bool EmitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute);
Daniel Dunbar3016db32009-08-21 09:11:24 +000057 virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue);
Chris Lattnerb1301f72010-01-23 07:47:02 +000058 virtual void EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
Daniel Dunbar6a715dc2009-08-30 06:17:16 +000059 unsigned ByteAlignment);
Chris Lattner72afa952010-05-08 19:54:22 +000060 virtual void BeginCOFFSymbolDef(const MCSymbol *Symbol) {
Craig Toppera2886c22012-02-07 05:05:23 +000061 llvm_unreachable("macho doesn't support this directive");
Chris Lattner72afa952010-05-08 19:54:22 +000062 }
63 virtual void EmitCOFFSymbolStorageClass(int StorageClass) {
Craig Toppera2886c22012-02-07 05:05:23 +000064 llvm_unreachable("macho doesn't support this directive");
Chris Lattner72afa952010-05-08 19:54:22 +000065 }
66 virtual void EmitCOFFSymbolType(int Type) {
Craig Toppera2886c22012-02-07 05:05:23 +000067 llvm_unreachable("macho doesn't support this directive");
Chris Lattner72afa952010-05-08 19:54:22 +000068 }
69 virtual void EndCOFFSymbolDef() {
Craig Toppera2886c22012-02-07 05:05:23 +000070 llvm_unreachable("macho doesn't support this directive");
Chris Lattner72afa952010-05-08 19:54:22 +000071 }
Chris Lattner91dac6d2010-01-25 07:52:13 +000072 virtual void EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) {
Craig Toppera2886c22012-02-07 05:05:23 +000073 llvm_unreachable("macho doesn't support this directive");
Chris Lattner91dac6d2010-01-25 07:52:13 +000074 }
Benjamin Kramer63970512011-09-01 23:04:27 +000075 virtual void EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size,
Benjamin Kramer47f9ec92012-09-07 17:25:13 +000076 unsigned ByteAlignment);
Daniel Dunbarcf72e1c2009-08-28 05:48:22 +000077 virtual void EmitZerofill(const MCSection *Section, MCSymbol *Symbol = 0,
Evan Chengf5bd6c62012-06-22 20:14:46 +000078 uint64_t Size = 0, unsigned ByteAlignment = 0);
Eric Christopherfeedc902010-05-18 21:26:41 +000079 virtual void EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol,
80 uint64_t Size, unsigned ByteAlignment = 0);
Daniel Dunbar9d40ef12010-05-26 20:37:00 +000081
Chris Lattner601ef332010-01-25 18:58:59 +000082 virtual void EmitFileDirective(StringRef Filename) {
Daniel Dunbar6b4391a2010-07-19 20:44:20 +000083 // FIXME: Just ignore the .file; it isn't important enough to fail the
84 // entire assembly.
85
Rafael Espindola5645bad2013-10-16 01:05:45 +000086 // report_fatal_error("unsupported directive: '.file'");
87 }
88
89 virtual void EmitIdent(StringRef IdentString) {
90 llvm_unreachable("macho doesn't support this directive");
Chris Lattner601ef332010-01-25 18:58:59 +000091 }
Daniel Dunbar9d40ef12010-05-26 20:37:00 +000092
Rafael Espindola07082092012-01-07 03:13:18 +000093 virtual void FinishImpl();
Daniel Dunbar3016db32009-08-21 09:11:24 +000094};
95
96} // end anonymous namespace.
97
Rafael Espindolaf667d922010-09-15 21:48:40 +000098void MCMachOStreamer::InitSections() {
Eli Benderskycbb25142013-01-14 19:04:57 +000099 InitToTextSection();
100}
Rafael Espindolaf667d922010-09-15 21:48:40 +0000101
Eli Benderskycbb25142013-01-14 19:04:57 +0000102void MCMachOStreamer::InitToTextSection() {
Rafael Espindolae308c0c2014-01-23 22:49:25 +0000103 SwitchSection(getContext().getObjectFileInfo()->getTextSection());
Rafael Espindolaf667d922010-09-15 21:48:40 +0000104}
105
Rafael Espindola349c3292011-04-28 12:50:37 +0000106void MCMachOStreamer::EmitEHSymAttributes(const MCSymbol *Symbol,
107 MCSymbol *EHSymbol) {
108 MCSymbolData &SD =
109 getAssembler().getOrCreateSymbolData(*Symbol);
110 if (SD.isExternal())
111 EmitSymbolAttribute(EHSymbol, MCSA_Global);
112 if (SD.getFlags() & SF_WeakDefinition)
113 EmitSymbolAttribute(EHSymbol, MCSA_WeakDefinition);
Rafael Espindola0b474c22011-04-30 16:22:46 +0000114 if (SD.isPrivateExtern())
115 EmitSymbolAttribute(EHSymbol, MCSA_PrivateExtern);
Rafael Espindola349c3292011-04-28 12:50:37 +0000116}
117
Daniel Dunbar3016db32009-08-21 09:11:24 +0000118void MCMachOStreamer::EmitLabel(MCSymbol *Symbol) {
Rafael Espindola16795802010-11-28 16:22:59 +0000119 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
Rafael Espindola16795802010-11-28 16:22:59 +0000120
Rafael Espindolae5b74152010-11-28 17:18:55 +0000121 // isSymbolLinkerVisible uses the section.
Richard Mitton21101b32013-09-19 23:21:01 +0000122 AssignSection(Symbol, getCurrentSection().first);
Daniel Dunbarede8e6d2010-06-16 20:04:32 +0000123 // We have to create a new fragment if this is an atom defining symbol,
124 // fragments cannot span atoms.
Rafael Espindolae5b74152010-11-28 17:18:55 +0000125 if (getAssembler().isSymbolLinkerVisible(*Symbol))
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000126 insert(new MCDataFragment());
Daniel Dunbaraadb2ca2010-05-10 22:45:09 +0000127
Rafael Espindolae5b74152010-11-28 17:18:55 +0000128 MCObjectStreamer::EmitLabel(Symbol);
Daniel Dunbar04a11582009-08-24 08:40:12 +0000129
Rafael Espindolae5b74152010-11-28 17:18:55 +0000130 MCSymbolData &SD = getAssembler().getSymbolData(*Symbol);
Daniel Dunbar0211a962010-05-17 20:12:31 +0000131 // This causes the reference type flag to be cleared. Darwin 'as' was "trying"
132 // to clear the weak reference and weak definition bits too, but the
133 // implementation was buggy. For now we just try to match 'as', for
134 // diffability.
135 //
136 // FIXME: Cleanup this code, these bits should be emitted based on semantic
137 // properties, not on the order of definition, etc.
138 SD.setFlags(SD.getFlags() & ~SF_ReferenceTypeMask);
Daniel Dunbar3016db32009-08-21 09:11:24 +0000139}
140
Reed Kotleraee4d5d12012-12-16 04:00:45 +0000141void MCMachOStreamer::EmitDebugLabel(MCSymbol *Symbol) {
142 EmitLabel(Symbol);
143}
Jim Grosbach4b63d2a2012-05-18 19:12:01 +0000144void MCMachOStreamer::EmitDataRegion(DataRegionData::KindTy Kind) {
Jim Grosbach745c52d2012-10-01 22:20:54 +0000145 if (!getAssembler().getBackend().hasDataInCodeSupport())
146 return;
Jim Grosbach4b63d2a2012-05-18 19:12:01 +0000147 // Create a temporary label to mark the start of the data region.
148 MCSymbol *Start = getContext().CreateTempSymbol();
149 EmitLabel(Start);
150 // Record the region for the object writer to use.
151 DataRegionData Data = { Kind, Start, NULL };
152 std::vector<DataRegionData> &Regions = getAssembler().getDataRegions();
153 Regions.push_back(Data);
154}
155
156void MCMachOStreamer::EmitDataRegionEnd() {
Jim Grosbach745c52d2012-10-01 22:20:54 +0000157 if (!getAssembler().getBackend().hasDataInCodeSupport())
158 return;
Jim Grosbach4b63d2a2012-05-18 19:12:01 +0000159 std::vector<DataRegionData> &Regions = getAssembler().getDataRegions();
160 assert(Regions.size() && "Mismatched .end_data_region!");
161 DataRegionData &Data = Regions.back();
162 assert(Data.End == NULL && "Mismatched .end_data_region!");
163 // Create a temporary label to mark the end of the data region.
164 Data.End = getContext().CreateTempSymbol();
165 EmitLabel(Data.End);
166}
167
Chris Lattner685508c2010-01-23 06:39:22 +0000168void MCMachOStreamer::EmitAssemblerFlag(MCAssemblerFlag Flag) {
Jim Grosbach87055ed2010-12-08 01:16:55 +0000169 // Let the target do whatever target specific stuff it needs to do.
Jim Grosbachaba3de92012-01-18 18:52:16 +0000170 getAssembler().getBackend().handleAssemblerFlag(Flag);
Jim Grosbach87055ed2010-12-08 01:16:55 +0000171 // Do any generic stuff we need to do.
Daniel Dunbare2697732009-08-26 21:22:22 +0000172 switch (Flag) {
Jim Grosbach5a2c68d2010-11-05 22:08:08 +0000173 case MCAF_SyntaxUnified: return; // no-op here.
Evan Cheng481ebb02011-07-27 00:38:12 +0000174 case MCAF_Code16: return; // Change parsing mode; no-op here.
175 case MCAF_Code32: return; // Change parsing mode; no-op here.
176 case MCAF_Code64: return; // Change parsing mode; no-op here.
Chris Lattner685508c2010-01-23 06:39:22 +0000177 case MCAF_SubsectionsViaSymbols:
Daniel Dunbar8a3c9d92010-06-16 20:04:22 +0000178 getAssembler().setSubsectionsViaSymbols(true);
Daniel Dunbar2a2459a2009-08-28 07:08:47 +0000179 return;
Daniel Dunbare2697732009-08-26 21:22:22 +0000180 }
Daniel Dunbar3016db32009-08-21 09:11:24 +0000181}
182
Daniel Dunbareec0f322013-01-18 01:26:07 +0000183void MCMachOStreamer::EmitLinkerOptions(ArrayRef<std::string> Options) {
184 getAssembler().getLinkerOptions().push_back(Options);
185}
186
Jim Grosbach4b63d2a2012-05-18 19:12:01 +0000187void MCMachOStreamer::EmitDataRegion(MCDataRegionType Kind) {
188 switch (Kind) {
189 case MCDR_DataRegion:
190 EmitDataRegion(DataRegionData::Data);
191 return;
192 case MCDR_DataRegionJT8:
193 EmitDataRegion(DataRegionData::JumpTable8);
194 return;
195 case MCDR_DataRegionJT16:
196 EmitDataRegion(DataRegionData::JumpTable16);
197 return;
198 case MCDR_DataRegionJT32:
199 EmitDataRegion(DataRegionData::JumpTable32);
200 return;
201 case MCDR_DataRegionEnd:
202 EmitDataRegionEnd();
203 return;
204 }
205}
206
Daniel Dunbarab14a6f2010-12-29 14:14:06 +0000207void MCMachOStreamer::EmitThumbFunc(MCSymbol *Symbol) {
Jim Grosbach41955ff2010-12-14 18:46:57 +0000208 // Remember that the function is a thumb function. Fixup and relocation
209 // values will need adjusted.
Daniel Dunbarab14a6f2010-12-29 14:14:06 +0000210 getAssembler().setIsThumbFunc(Symbol);
211
212 // Mark the thumb bit on the symbol.
213 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
214 SD.setFlags(SD.getFlags() | SF_ThumbFunc);
Jim Grosbach5a2c68d2010-11-05 22:08:08 +0000215}
216
Saleem Abdulrasool4208b612013-08-09 01:52:03 +0000217bool MCMachOStreamer::EmitSymbolAttribute(MCSymbol *Symbol,
Chris Lattner685508c2010-01-23 06:39:22 +0000218 MCSymbolAttr Attribute) {
Daniel Dunbar582d6102009-08-24 11:56:58 +0000219 // Indirect symbols are handled differently, to match how 'as' handles
220 // them. This makes writing matching .o files easier.
Chris Lattner685508c2010-01-23 06:39:22 +0000221 if (Attribute == MCSA_IndirectSymbol) {
Daniel Dunbarc2c0bf92009-08-26 00:18:21 +0000222 // Note that we intentionally cannot use the symbol data here; this is
223 // important for matching the string table that 'as' generates.
Daniel Dunbar582d6102009-08-24 11:56:58 +0000224 IndirectSymbolData ISD;
225 ISD.Symbol = Symbol;
Daniel Dunbarb2347fe2010-06-16 20:04:25 +0000226 ISD.SectionData = getCurrentSectionData();
Daniel Dunbar8a3c9d92010-06-16 20:04:22 +0000227 getAssembler().getIndirectSymbols().push_back(ISD);
Saleem Abdulrasool4208b612013-08-09 01:52:03 +0000228 return true;
Daniel Dunbar582d6102009-08-24 11:56:58 +0000229 }
230
Daniel Dunbar04a11582009-08-24 08:40:12 +0000231 // Adding a symbol attribute always introduces the symbol, note that an
Daniel Dunbar6eab7212010-03-10 20:58:29 +0000232 // important side effect of calling getOrCreateSymbolData here is to register
233 // the symbol with the assembler.
Daniel Dunbar8a3c9d92010-06-16 20:04:22 +0000234 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
Daniel Dunbar04a11582009-08-24 08:40:12 +0000235
236 // The implementation of symbol attributes is designed to match 'as', but it
237 // leaves much to desired. It doesn't really make sense to arbitrarily add and
238 // remove flags, but 'as' allows this (in particular, see .desc).
239 //
240 // In the future it might be worth trying to make these operations more well
241 // defined.
Daniel Dunbard1859472009-08-22 11:41:10 +0000242 switch (Attribute) {
Chris Lattner685508c2010-01-23 06:39:22 +0000243 case MCSA_Invalid:
Chris Lattnerbc8f6382010-01-25 18:30:45 +0000244 case MCSA_ELF_TypeFunction:
245 case MCSA_ELF_TypeIndFunction:
246 case MCSA_ELF_TypeObject:
247 case MCSA_ELF_TypeTLS:
248 case MCSA_ELF_TypeCommon:
249 case MCSA_ELF_TypeNoType:
Rafael Espindola4bcf94c2010-11-13 04:51:02 +0000250 case MCSA_ELF_TypeGnuUniqueObject:
Chris Lattner685508c2010-01-23 06:39:22 +0000251 case MCSA_Hidden:
Chandler Carruthb2561022011-07-25 21:21:08 +0000252 case MCSA_IndirectSymbol:
Chris Lattner685508c2010-01-23 06:39:22 +0000253 case MCSA_Internal:
254 case MCSA_Protected:
255 case MCSA_Weak:
256 case MCSA_Local:
Saleem Abdulrasool4208b612013-08-09 01:52:03 +0000257 return false;
Daniel Dunbard1859472009-08-22 11:41:10 +0000258
Chris Lattner685508c2010-01-23 06:39:22 +0000259 case MCSA_Global:
Daniel Dunbar2701eee2009-08-28 07:08:35 +0000260 SD.setExternal(true);
Daniel Dunbar0211a962010-05-17 20:12:31 +0000261 // This effectively clears the undefined lazy bit, in Darwin 'as', although
262 // it isn't very consistent because it implements this as part of symbol
263 // lookup.
264 //
265 // FIXME: Cleanup this code, these bits should be emitted based on semantic
266 // properties, not on the order of definition, etc.
267 SD.setFlags(SD.getFlags() & ~SF_ReferenceTypeUndefinedLazy);
Daniel Dunbard1859472009-08-22 11:41:10 +0000268 break;
Daniel Dunbar04a11582009-08-24 08:40:12 +0000269
Chris Lattner685508c2010-01-23 06:39:22 +0000270 case MCSA_LazyReference:
Daniel Dunbar04a11582009-08-24 08:40:12 +0000271 // FIXME: This requires -dynamic.
272 SD.setFlags(SD.getFlags() | SF_NoDeadStrip);
273 if (Symbol->isUndefined())
274 SD.setFlags(SD.getFlags() | SF_ReferenceTypeUndefinedLazy);
275 break;
276
Daniel Dunbar04a11582009-08-24 08:40:12 +0000277 // Since .reference sets the no dead strip bit, it is equivalent to
278 // .no_dead_strip in practice.
Chris Lattner685508c2010-01-23 06:39:22 +0000279 case MCSA_Reference:
280 case MCSA_NoDeadStrip:
Daniel Dunbar04a11582009-08-24 08:40:12 +0000281 SD.setFlags(SD.getFlags() | SF_NoDeadStrip);
282 break;
283
Kevin Enderby8be14412010-11-19 18:39:33 +0000284 case MCSA_SymbolResolver:
285 SD.setFlags(SD.getFlags() | SF_SymbolResolver);
286 break;
287
Chris Lattner685508c2010-01-23 06:39:22 +0000288 case MCSA_PrivateExtern:
Daniel Dunbar04a11582009-08-24 08:40:12 +0000289 SD.setExternal(true);
290 SD.setPrivateExtern(true);
291 break;
292
Chris Lattner685508c2010-01-23 06:39:22 +0000293 case MCSA_WeakReference:
Daniel Dunbar04a11582009-08-24 08:40:12 +0000294 // FIXME: This requires -dynamic.
295 if (Symbol->isUndefined())
296 SD.setFlags(SD.getFlags() | SF_WeakReference);
297 break;
298
Chris Lattner685508c2010-01-23 06:39:22 +0000299 case MCSA_WeakDefinition:
Daniel Dunbar04a11582009-08-24 08:40:12 +0000300 // FIXME: 'as' enforces that this is defined and global. The manual claims
301 // it has to be in a coalesced section, but this isn't enforced.
302 SD.setFlags(SD.getFlags() | SF_WeakDefinition);
303 break;
Kevin Enderby082d0fd2010-07-08 17:22:42 +0000304
305 case MCSA_WeakDefAutoPrivate:
306 SD.setFlags(SD.getFlags() | SF_WeakDefinition | SF_WeakReference);
307 break;
Daniel Dunbard1859472009-08-22 11:41:10 +0000308 }
Saleem Abdulrasool4208b612013-08-09 01:52:03 +0000309
310 return true;
Daniel Dunbar3016db32009-08-21 09:11:24 +0000311}
312
313void MCMachOStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
Daniel Dunbar04a11582009-08-24 08:40:12 +0000314 // Encode the 'desc' value into the lowest implementation defined bits.
Daniel Dunbar9d40ef12010-05-26 20:37:00 +0000315 assert(DescValue == (DescValue & SF_DescFlagsMask) &&
Daniel Dunbar04a11582009-08-24 08:40:12 +0000316 "Invalid .desc value!");
Daniel Dunbar8a3c9d92010-06-16 20:04:22 +0000317 getAssembler().getOrCreateSymbolData(*Symbol).setFlags(
318 DescValue & SF_DescFlagsMask);
Daniel Dunbar3016db32009-08-21 09:11:24 +0000319}
320
Chris Lattnerb1301f72010-01-23 07:47:02 +0000321void MCMachOStreamer::EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
Daniel Dunbar6a715dc2009-08-30 06:17:16 +0000322 unsigned ByteAlignment) {
Daniel Dunbar2701eee2009-08-28 07:08:35 +0000323 // FIXME: Darwin 'as' does appear to allow redef of a .comm by itself.
324 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
325
Richard Mitton21101b32013-09-19 23:21:01 +0000326 AssignSection(Symbol, NULL);
327
Daniel Dunbar8a3c9d92010-06-16 20:04:22 +0000328 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
Daniel Dunbar2701eee2009-08-28 07:08:35 +0000329 SD.setExternal(true);
Daniel Dunbar6a715dc2009-08-30 06:17:16 +0000330 SD.setCommon(Size, ByteAlignment);
Daniel Dunbar3016db32009-08-21 09:11:24 +0000331}
332
Benjamin Kramer47f9ec92012-09-07 17:25:13 +0000333void MCMachOStreamer::EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size,
334 unsigned ByteAlignment) {
335 // '.lcomm' is equivalent to '.zerofill'.
Rafael Espindolae308c0c2014-01-23 22:49:25 +0000336 return EmitZerofill(getContext().getObjectFileInfo()->getDataBSSSection(),
Benjamin Kramer47f9ec92012-09-07 17:25:13 +0000337 Symbol, Size, ByteAlignment);
338}
339
Daniel Dunbarcf72e1c2009-08-28 05:48:22 +0000340void MCMachOStreamer::EmitZerofill(const MCSection *Section, MCSymbol *Symbol,
Evan Chengf5bd6c62012-06-22 20:14:46 +0000341 uint64_t Size, unsigned ByteAlignment) {
Daniel Dunbar8a3c9d92010-06-16 20:04:22 +0000342 MCSectionData &SectData = getAssembler().getOrCreateSectionData(*Section);
Daniel Dunbar42a39d02009-08-28 05:49:21 +0000343
344 // The symbol may not be present, which only creates the section.
345 if (!Symbol)
346 return;
347
Eric Christopher42ae4592013-08-07 21:13:01 +0000348 // On darwin all virtual sections have zerofill type.
349 assert(Section->isVirtualSection() && "Section does not have zerofill type!");
Daniel Dunbar42a39d02009-08-28 05:49:21 +0000350
351 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
352
Daniel Dunbar8a3c9d92010-06-16 20:04:22 +0000353 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
Daniel Dunbar42a39d02009-08-28 05:49:21 +0000354
Daniel Dunbar51402b72010-05-12 22:51:27 +0000355 // Emit an align fragment if necessary.
356 if (ByteAlignment != 1)
Daniel Dunbarb76df222010-05-12 22:56:23 +0000357 new MCAlignFragment(ByteAlignment, 0, 0, ByteAlignment, &SectData);
Daniel Dunbar51402b72010-05-12 22:51:27 +0000358
Daniel Dunbar4405ffc2010-05-12 22:51:38 +0000359 MCFragment *F = new MCFillFragment(0, 0, Size, &SectData);
Daniel Dunbar42a39d02009-08-28 05:49:21 +0000360 SD.setFragment(F);
361
Richard Mitton21101b32013-09-19 23:21:01 +0000362 AssignSection(Symbol, Section);
Daniel Dunbar42a39d02009-08-28 05:49:21 +0000363
364 // Update the maximum alignment on the zero fill section if necessary.
365 if (ByteAlignment > SectData.getAlignment())
366 SectData.setAlignment(ByteAlignment);
Daniel Dunbar3016db32009-08-21 09:11:24 +0000367}
368
Eric Christopher09d47032010-05-21 23:03:53 +0000369// This should always be called with the thread local bss section. Like the
370// .zerofill directive this doesn't actually switch sections on us.
Eric Christopherfeedc902010-05-18 21:26:41 +0000371void MCMachOStreamer::EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol,
372 uint64_t Size, unsigned ByteAlignment) {
373 EmitZerofill(Section, Symbol, Size, ByteAlignment);
374 return;
Eric Christopher9fb6bb02010-05-14 01:50:28 +0000375}
376
Daniel Dunbar9d40ef12010-05-26 20:37:00 +0000377void MCMachOStreamer::EmitInstToData(const MCInst &Inst) {
378 MCDataFragment *DF = getOrCreateDataFragment();
379
380 SmallVector<MCFixup, 4> Fixups;
381 SmallString<256> Code;
382 raw_svector_ostream VecOS(Code);
Daniel Dunbar8a3c9d92010-06-16 20:04:22 +0000383 getAssembler().getEmitter().EncodeInstruction(Inst, VecOS, Fixups);
Daniel Dunbar9d40ef12010-05-26 20:37:00 +0000384 VecOS.flush();
385
386 // Add the fixups and data.
387 for (unsigned i = 0, e = Fixups.size(); i != e; ++i) {
388 Fixups[i].setOffset(Fixups[i].getOffset() + DF->getContents().size());
Eli Benderskya31a8942012-12-07 19:13:57 +0000389 DF->getFixups().push_back(Fixups[i]);
Daniel Dunbar9d40ef12010-05-26 20:37:00 +0000390 }
391 DF->getContents().append(Code.begin(), Code.end());
392}
393
Rafael Espindola07082092012-01-07 03:13:18 +0000394void MCMachOStreamer::FinishImpl() {
Bill Wendling550c76d2013-09-09 19:48:37 +0000395 EmitFrames(&getAssembler().getBackend(), true);
Rafael Espindolafc822362011-05-01 15:44:13 +0000396
Daniel Dunbarede8e6d2010-06-16 20:04:32 +0000397 // We have to set the fragment atom associations so we can relax properly for
398 // Mach-O.
399
400 // First, scan the symbol table to build a lookup table from fragments to
401 // defining symbols.
402 DenseMap<const MCFragment*, MCSymbolData*> DefiningSymbolMap;
403 for (MCAssembler::symbol_iterator it = getAssembler().symbol_begin(),
404 ie = getAssembler().symbol_end(); it != ie; ++it) {
405 if (getAssembler().isSymbolLinkerVisible(it->getSymbol()) &&
406 it->getFragment()) {
407 // An atom defining symbol should never be internal to a fragment.
408 assert(it->getOffset() == 0 && "Invalid offset in atom defining symbol!");
409 DefiningSymbolMap[it->getFragment()] = it;
410 }
411 }
412
413 // Set the fragment atom associations by tracking the last seen atom defining
414 // symbol.
415 for (MCAssembler::iterator it = getAssembler().begin(),
416 ie = getAssembler().end(); it != ie; ++it) {
417 MCSymbolData *CurrentAtom = 0;
418 for (MCSectionData::iterator it2 = it->begin(),
419 ie2 = it->end(); it2 != ie2; ++it2) {
420 if (MCSymbolData *SD = DefiningSymbolMap.lookup(it2))
421 CurrentAtom = SD;
422 it2->setAtom(CurrentAtom);
423 }
424 }
425
Rafael Espindola07082092012-01-07 03:13:18 +0000426 this->MCObjectStreamer::FinishImpl();
Daniel Dunbarede8e6d2010-06-16 20:04:32 +0000427}
428
Evan Cheng5928e692011-07-25 23:24:55 +0000429MCStreamer *llvm::createMachOStreamer(MCContext &Context, MCAsmBackend &MAB,
Daniel Dunbard821f4a2010-03-25 22:49:09 +0000430 raw_ostream &OS, MCCodeEmitter *CE,
431 bool RelaxAll) {
Evan Cheng5928e692011-07-25 23:24:55 +0000432 MCMachOStreamer *S = new MCMachOStreamer(Context, MAB, OS, CE);
Daniel Dunbard821f4a2010-03-25 22:49:09 +0000433 if (RelaxAll)
434 S->getAssembler().setRelaxAll(true);
435 return S;
Daniel Dunbar3016db32009-08-21 09:11:24 +0000436}