blob: 78c009423c81895466c5b958352af84835e9b85d [file] [log] [blame]
Bill Wendling19f066e2013-09-04 22:35:41 +00001//===-- MCMachOStreamer.cpp - MachO Streamer ------------------------------===//
Daniel Dunbarfb4a6b32009-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 Carruthd04a8d42012-12-03 16:50:05 +000011#include "llvm/MC/MCAsmBackend.h"
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +000012#include "llvm/MC/MCAssembler.h"
Daniel Dunbar4fac7492009-08-27 08:17:51 +000013#include "llvm/MC/MCCodeEmitter.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000014#include "llvm/MC/MCContext.h"
15#include "llvm/MC/MCDwarf.h"
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +000016#include "llvm/MC/MCExpr.h"
Daniel Dunbar4fac7492009-08-27 08:17:51 +000017#include "llvm/MC/MCInst.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000018#include "llvm/MC/MCMachOSymbolFlags.h"
Daniel Dunbar8dc68ab2010-06-16 20:04:22 +000019#include "llvm/MC/MCObjectStreamer.h"
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +000020#include "llvm/MC/MCSection.h"
Kevin Enderbyb07ce602010-08-09 22:52:14 +000021#include "llvm/MC/MCSectionMachO.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000022#include "llvm/MC/MCSymbol.h"
Kevin Enderbyc0957932010-09-30 16:52:03 +000023#include "llvm/Support/Dwarf.h"
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +000024#include "llvm/Support/ErrorHandling.h"
Daniel Dunbar4fac7492009-08-27 08:17:51 +000025#include "llvm/Support/raw_ostream.h"
Daniel Dunbard8036fb2010-03-23 05:09:03 +000026
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +000027using namespace llvm;
28
29namespace {
30
Daniel Dunbar8dc68ab2010-06-16 20:04:22 +000031class MCMachOStreamer : public MCObjectStreamer {
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +000032private:
Rafael Espindolaf89671d2010-11-01 16:27:31 +000033 virtual void EmitInstToData(const MCInst &Inst);
Daniel Dunbar2ac0c452010-05-26 20:37:00 +000034
Jim Grosbach3e965312012-05-18 19:12:01 +000035 void EmitDataRegion(DataRegionData::KindTy Kind);
36 void EmitDataRegionEnd();
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +000037public:
Chandler Carruth5da36652013-01-31 23:29:57 +000038 MCMachOStreamer(MCContext &Context, MCAsmBackend &MAB, raw_ostream &OS,
39 MCCodeEmitter *Emitter)
Rafael Espindola320296a2013-10-08 13:08:17 +000040 : MCObjectStreamer(Context, 0, MAB, OS, Emitter) {}
Daniel Dunbarac2884a2010-03-25 22:49:09 +000041
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +000042 /// @name MCStreamer Interface
43 /// @{
44
Rafael Espindolad80781b2010-09-15 21:48:40 +000045 virtual void InitSections();
Eli Bendersky030f63a2013-01-14 19:04:57 +000046 virtual void InitToTextSection();
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +000047 virtual void EmitLabel(MCSymbol *Symbol);
Reed Kotler2c3a4642012-12-16 04:00:45 +000048 virtual void EmitDebugLabel(MCSymbol *Symbol);
Rafael Espindola8bca4102011-04-28 12:50:37 +000049 virtual void EmitEHSymAttributes(const MCSymbol *Symbol,
50 MCSymbol *EHSymbol);
Chris Lattnera5ad93a2010-01-23 06:39:22 +000051 virtual void EmitAssemblerFlag(MCAssemblerFlag Flag);
Daniel Dunbara94c3392013-01-18 01:26:07 +000052 virtual void EmitLinkerOptions(ArrayRef<std::string> Options);
Jim Grosbach3e965312012-05-18 19:12:01 +000053 virtual void EmitDataRegion(MCDataRegionType Kind);
Jim Grosbachce792992010-11-05 22:08:08 +000054 virtual void EmitThumbFunc(MCSymbol *Func);
Saleem Abdulrasool1c9cd022013-08-09 01:52:03 +000055 virtual bool EmitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +000056 virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue);
Chris Lattner9eb158d2010-01-23 07:47:02 +000057 virtual void EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
Daniel Dunbar7092c7e2009-08-30 06:17:16 +000058 unsigned ByteAlignment);
Chris Lattnerb54b9dd2010-05-08 19:54:22 +000059 virtual void BeginCOFFSymbolDef(const MCSymbol *Symbol) {
Craig Topper85814382012-02-07 05:05:23 +000060 llvm_unreachable("macho doesn't support this directive");
Chris Lattnerb54b9dd2010-05-08 19:54:22 +000061 }
62 virtual void EmitCOFFSymbolStorageClass(int StorageClass) {
Craig Topper85814382012-02-07 05:05:23 +000063 llvm_unreachable("macho doesn't support this directive");
Chris Lattnerb54b9dd2010-05-08 19:54:22 +000064 }
65 virtual void EmitCOFFSymbolType(int Type) {
Craig Topper85814382012-02-07 05:05:23 +000066 llvm_unreachable("macho doesn't support this directive");
Chris Lattnerb54b9dd2010-05-08 19:54:22 +000067 }
68 virtual void EndCOFFSymbolDef() {
Craig Topper85814382012-02-07 05:05:23 +000069 llvm_unreachable("macho doesn't support this directive");
Chris Lattnerb54b9dd2010-05-08 19:54:22 +000070 }
Chris Lattner99328ad2010-01-25 07:52:13 +000071 virtual void EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) {
Craig Topper85814382012-02-07 05:05:23 +000072 llvm_unreachable("macho doesn't support this directive");
Chris Lattner99328ad2010-01-25 07:52:13 +000073 }
Benjamin Kramer36a16012011-09-01 23:04:27 +000074 virtual void EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size,
Benjamin Kramer39646d92012-09-07 17:25:13 +000075 unsigned ByteAlignment);
Daniel Dunbar8751b942009-08-28 05:48:22 +000076 virtual void EmitZerofill(const MCSection *Section, MCSymbol *Symbol = 0,
Evan Chengc90a1fc2012-06-22 20:14:46 +000077 uint64_t Size = 0, unsigned ByteAlignment = 0);
Eric Christopher011c3f12010-05-18 21:26:41 +000078 virtual void EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol,
79 uint64_t Size, unsigned ByteAlignment = 0);
Daniel Dunbar2ac0c452010-05-26 20:37:00 +000080
Chris Lattnera6594fc2010-01-25 18:58:59 +000081 virtual void EmitFileDirective(StringRef Filename) {
Daniel Dunbar96749672010-07-19 20:44:20 +000082 // FIXME: Just ignore the .file; it isn't important enough to fail the
83 // entire assembly.
84
85 //report_fatal_error("unsupported directive: '.file'");
Chris Lattnera6594fc2010-01-25 18:58:59 +000086 }
Daniel Dunbar2ac0c452010-05-26 20:37:00 +000087
Rafael Espindola99b42372012-01-07 03:13:18 +000088 virtual void FinishImpl();
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +000089};
90
91} // end anonymous namespace.
92
Rafael Espindolad80781b2010-09-15 21:48:40 +000093void MCMachOStreamer::InitSections() {
Eli Bendersky030f63a2013-01-14 19:04:57 +000094 InitToTextSection();
95}
Rafael Espindolad80781b2010-09-15 21:48:40 +000096
Eli Bendersky030f63a2013-01-14 19:04:57 +000097void MCMachOStreamer::InitToTextSection() {
98 SwitchSection(getContext().getMachOSection(
99 "__TEXT", "__text",
100 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS, 0,
101 SectionKind::getText()));
Rafael Espindolad80781b2010-09-15 21:48:40 +0000102}
103
Rafael Espindola8bca4102011-04-28 12:50:37 +0000104void MCMachOStreamer::EmitEHSymAttributes(const MCSymbol *Symbol,
105 MCSymbol *EHSymbol) {
106 MCSymbolData &SD =
107 getAssembler().getOrCreateSymbolData(*Symbol);
108 if (SD.isExternal())
109 EmitSymbolAttribute(EHSymbol, MCSA_Global);
110 if (SD.getFlags() & SF_WeakDefinition)
111 EmitSymbolAttribute(EHSymbol, MCSA_WeakDefinition);
Rafael Espindola97f7f4e2011-04-30 16:22:46 +0000112 if (SD.isPrivateExtern())
113 EmitSymbolAttribute(EHSymbol, MCSA_PrivateExtern);
Rafael Espindola8bca4102011-04-28 12:50:37 +0000114}
115
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000116void MCMachOStreamer::EmitLabel(MCSymbol *Symbol) {
Rafael Espindolaba210242010-11-28 16:22:59 +0000117 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
Rafael Espindolaba210242010-11-28 16:22:59 +0000118
Rafael Espindolaea4afa92010-11-28 17:18:55 +0000119 // isSymbolLinkerVisible uses the section.
Richard Mitton5cc319a2013-09-19 23:21:01 +0000120 AssignSection(Symbol, getCurrentSection().first);
Daniel Dunbara86de102010-06-16 20:04:32 +0000121 // We have to create a new fragment if this is an atom defining symbol,
122 // fragments cannot span atoms.
Rafael Espindolaea4afa92010-11-28 17:18:55 +0000123 if (getAssembler().isSymbolLinkerVisible(*Symbol))
Peter Collingbournedf39be62013-04-17 21:18:16 +0000124 insert(new MCDataFragment());
Daniel Dunbar071f73d2010-05-10 22:45:09 +0000125
Rafael Espindolaea4afa92010-11-28 17:18:55 +0000126 MCObjectStreamer::EmitLabel(Symbol);
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000127
Rafael Espindolaea4afa92010-11-28 17:18:55 +0000128 MCSymbolData &SD = getAssembler().getSymbolData(*Symbol);
Daniel Dunbarb18d2dd2010-05-17 20:12:31 +0000129 // This causes the reference type flag to be cleared. Darwin 'as' was "trying"
130 // to clear the weak reference and weak definition bits too, but the
131 // implementation was buggy. For now we just try to match 'as', for
132 // diffability.
133 //
134 // FIXME: Cleanup this code, these bits should be emitted based on semantic
135 // properties, not on the order of definition, etc.
136 SD.setFlags(SD.getFlags() & ~SF_ReferenceTypeMask);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000137}
138
Reed Kotler2c3a4642012-12-16 04:00:45 +0000139void MCMachOStreamer::EmitDebugLabel(MCSymbol *Symbol) {
140 EmitLabel(Symbol);
141}
Jim Grosbach3e965312012-05-18 19:12:01 +0000142void MCMachOStreamer::EmitDataRegion(DataRegionData::KindTy Kind) {
Jim Grosbachb4316022012-10-01 22:20:54 +0000143 if (!getAssembler().getBackend().hasDataInCodeSupport())
144 return;
Jim Grosbach3e965312012-05-18 19:12:01 +0000145 // Create a temporary label to mark the start of the data region.
146 MCSymbol *Start = getContext().CreateTempSymbol();
147 EmitLabel(Start);
148 // Record the region for the object writer to use.
149 DataRegionData Data = { Kind, Start, NULL };
150 std::vector<DataRegionData> &Regions = getAssembler().getDataRegions();
151 Regions.push_back(Data);
152}
153
154void MCMachOStreamer::EmitDataRegionEnd() {
Jim Grosbachb4316022012-10-01 22:20:54 +0000155 if (!getAssembler().getBackend().hasDataInCodeSupport())
156 return;
Jim Grosbach3e965312012-05-18 19:12:01 +0000157 std::vector<DataRegionData> &Regions = getAssembler().getDataRegions();
158 assert(Regions.size() && "Mismatched .end_data_region!");
159 DataRegionData &Data = Regions.back();
160 assert(Data.End == NULL && "Mismatched .end_data_region!");
161 // Create a temporary label to mark the end of the data region.
162 Data.End = getContext().CreateTempSymbol();
163 EmitLabel(Data.End);
164}
165
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000166void MCMachOStreamer::EmitAssemblerFlag(MCAssemblerFlag Flag) {
Jim Grosbach5be6d2a2010-12-08 01:16:55 +0000167 // Let the target do whatever target specific stuff it needs to do.
Jim Grosbachec343382012-01-18 18:52:16 +0000168 getAssembler().getBackend().handleAssemblerFlag(Flag);
Jim Grosbach5be6d2a2010-12-08 01:16:55 +0000169 // Do any generic stuff we need to do.
Daniel Dunbar6009db42009-08-26 21:22:22 +0000170 switch (Flag) {
Jim Grosbachce792992010-11-05 22:08:08 +0000171 case MCAF_SyntaxUnified: return; // no-op here.
Evan Chengbd27f5a2011-07-27 00:38:12 +0000172 case MCAF_Code16: return; // Change parsing mode; no-op here.
173 case MCAF_Code32: return; // Change parsing mode; no-op here.
174 case MCAF_Code64: return; // Change parsing mode; no-op here.
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000175 case MCAF_SubsectionsViaSymbols:
Daniel Dunbar8dc68ab2010-06-16 20:04:22 +0000176 getAssembler().setSubsectionsViaSymbols(true);
Daniel Dunbar8c3eaf42009-08-28 07:08:47 +0000177 return;
Daniel Dunbar6009db42009-08-26 21:22:22 +0000178 }
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000179}
180
Daniel Dunbara94c3392013-01-18 01:26:07 +0000181void MCMachOStreamer::EmitLinkerOptions(ArrayRef<std::string> Options) {
182 getAssembler().getLinkerOptions().push_back(Options);
183}
184
Jim Grosbach3e965312012-05-18 19:12:01 +0000185void MCMachOStreamer::EmitDataRegion(MCDataRegionType Kind) {
186 switch (Kind) {
187 case MCDR_DataRegion:
188 EmitDataRegion(DataRegionData::Data);
189 return;
190 case MCDR_DataRegionJT8:
191 EmitDataRegion(DataRegionData::JumpTable8);
192 return;
193 case MCDR_DataRegionJT16:
194 EmitDataRegion(DataRegionData::JumpTable16);
195 return;
196 case MCDR_DataRegionJT32:
197 EmitDataRegion(DataRegionData::JumpTable32);
198 return;
199 case MCDR_DataRegionEnd:
200 EmitDataRegionEnd();
201 return;
202 }
203}
204
Daniel Dunbarb2624ed2010-12-29 14:14:06 +0000205void MCMachOStreamer::EmitThumbFunc(MCSymbol *Symbol) {
Jim Grosbach47500202010-12-14 18:46:57 +0000206 // Remember that the function is a thumb function. Fixup and relocation
207 // values will need adjusted.
Daniel Dunbarb2624ed2010-12-29 14:14:06 +0000208 getAssembler().setIsThumbFunc(Symbol);
209
210 // Mark the thumb bit on the symbol.
211 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
212 SD.setFlags(SD.getFlags() | SF_ThumbFunc);
Jim Grosbachce792992010-11-05 22:08:08 +0000213}
214
Saleem Abdulrasool1c9cd022013-08-09 01:52:03 +0000215bool MCMachOStreamer::EmitSymbolAttribute(MCSymbol *Symbol,
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000216 MCSymbolAttr Attribute) {
Daniel Dunbar0c7761b2009-08-24 11:56:58 +0000217 // Indirect symbols are handled differently, to match how 'as' handles
218 // them. This makes writing matching .o files easier.
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000219 if (Attribute == MCSA_IndirectSymbol) {
Daniel Dunbarad7c3d52009-08-26 00:18:21 +0000220 // Note that we intentionally cannot use the symbol data here; this is
221 // important for matching the string table that 'as' generates.
Daniel Dunbar0c7761b2009-08-24 11:56:58 +0000222 IndirectSymbolData ISD;
223 ISD.Symbol = Symbol;
Daniel Dunbar83b46712010-06-16 20:04:25 +0000224 ISD.SectionData = getCurrentSectionData();
Daniel Dunbar8dc68ab2010-06-16 20:04:22 +0000225 getAssembler().getIndirectSymbols().push_back(ISD);
Saleem Abdulrasool1c9cd022013-08-09 01:52:03 +0000226 return true;
Daniel Dunbar0c7761b2009-08-24 11:56:58 +0000227 }
228
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000229 // Adding a symbol attribute always introduces the symbol, note that an
Daniel Dunbar46836a72010-03-10 20:58:29 +0000230 // important side effect of calling getOrCreateSymbolData here is to register
231 // the symbol with the assembler.
Daniel Dunbar8dc68ab2010-06-16 20:04:22 +0000232 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000233
234 // The implementation of symbol attributes is designed to match 'as', but it
235 // leaves much to desired. It doesn't really make sense to arbitrarily add and
236 // remove flags, but 'as' allows this (in particular, see .desc).
237 //
238 // In the future it might be worth trying to make these operations more well
239 // defined.
Daniel Dunbar3edd9bb2009-08-22 11:41:10 +0000240 switch (Attribute) {
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000241 case MCSA_Invalid:
Chris Lattnered0ab152010-01-25 18:30:45 +0000242 case MCSA_ELF_TypeFunction:
243 case MCSA_ELF_TypeIndFunction:
244 case MCSA_ELF_TypeObject:
245 case MCSA_ELF_TypeTLS:
246 case MCSA_ELF_TypeCommon:
247 case MCSA_ELF_TypeNoType:
Rafael Espindolae13a0ff2010-11-13 04:51:02 +0000248 case MCSA_ELF_TypeGnuUniqueObject:
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000249 case MCSA_Hidden:
Chandler Carruth8bcf9492011-07-25 21:21:08 +0000250 case MCSA_IndirectSymbol:
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000251 case MCSA_Internal:
252 case MCSA_Protected:
253 case MCSA_Weak:
254 case MCSA_Local:
Saleem Abdulrasool1c9cd022013-08-09 01:52:03 +0000255 return false;
Daniel Dunbar3edd9bb2009-08-22 11:41:10 +0000256
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000257 case MCSA_Global:
Daniel Dunbar8f4d1462009-08-28 07:08:35 +0000258 SD.setExternal(true);
Daniel Dunbarb18d2dd2010-05-17 20:12:31 +0000259 // This effectively clears the undefined lazy bit, in Darwin 'as', although
260 // it isn't very consistent because it implements this as part of symbol
261 // lookup.
262 //
263 // FIXME: Cleanup this code, these bits should be emitted based on semantic
264 // properties, not on the order of definition, etc.
265 SD.setFlags(SD.getFlags() & ~SF_ReferenceTypeUndefinedLazy);
Daniel Dunbar3edd9bb2009-08-22 11:41:10 +0000266 break;
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000267
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000268 case MCSA_LazyReference:
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000269 // FIXME: This requires -dynamic.
270 SD.setFlags(SD.getFlags() | SF_NoDeadStrip);
271 if (Symbol->isUndefined())
272 SD.setFlags(SD.getFlags() | SF_ReferenceTypeUndefinedLazy);
273 break;
274
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000275 // Since .reference sets the no dead strip bit, it is equivalent to
276 // .no_dead_strip in practice.
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000277 case MCSA_Reference:
278 case MCSA_NoDeadStrip:
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000279 SD.setFlags(SD.getFlags() | SF_NoDeadStrip);
280 break;
281
Kevin Enderbye8e98d72010-11-19 18:39:33 +0000282 case MCSA_SymbolResolver:
283 SD.setFlags(SD.getFlags() | SF_SymbolResolver);
284 break;
285
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000286 case MCSA_PrivateExtern:
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000287 SD.setExternal(true);
288 SD.setPrivateExtern(true);
289 break;
290
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000291 case MCSA_WeakReference:
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000292 // FIXME: This requires -dynamic.
293 if (Symbol->isUndefined())
294 SD.setFlags(SD.getFlags() | SF_WeakReference);
295 break;
296
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000297 case MCSA_WeakDefinition:
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000298 // FIXME: 'as' enforces that this is defined and global. The manual claims
299 // it has to be in a coalesced section, but this isn't enforced.
300 SD.setFlags(SD.getFlags() | SF_WeakDefinition);
301 break;
Kevin Enderbyf59cac52010-07-08 17:22:42 +0000302
303 case MCSA_WeakDefAutoPrivate:
304 SD.setFlags(SD.getFlags() | SF_WeakDefinition | SF_WeakReference);
305 break;
Daniel Dunbar3edd9bb2009-08-22 11:41:10 +0000306 }
Saleem Abdulrasool1c9cd022013-08-09 01:52:03 +0000307
308 return true;
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000309}
310
311void MCMachOStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000312 // Encode the 'desc' value into the lowest implementation defined bits.
Daniel Dunbar2ac0c452010-05-26 20:37:00 +0000313 assert(DescValue == (DescValue & SF_DescFlagsMask) &&
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000314 "Invalid .desc value!");
Daniel Dunbar8dc68ab2010-06-16 20:04:22 +0000315 getAssembler().getOrCreateSymbolData(*Symbol).setFlags(
316 DescValue & SF_DescFlagsMask);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000317}
318
Chris Lattner9eb158d2010-01-23 07:47:02 +0000319void MCMachOStreamer::EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000320 unsigned ByteAlignment) {
Daniel Dunbar8f4d1462009-08-28 07:08:35 +0000321 // FIXME: Darwin 'as' does appear to allow redef of a .comm by itself.
322 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
323
Richard Mitton5cc319a2013-09-19 23:21:01 +0000324 AssignSection(Symbol, NULL);
325
Daniel Dunbar8dc68ab2010-06-16 20:04:22 +0000326 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
Daniel Dunbar8f4d1462009-08-28 07:08:35 +0000327 SD.setExternal(true);
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000328 SD.setCommon(Size, ByteAlignment);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000329}
330
Benjamin Kramer39646d92012-09-07 17:25:13 +0000331void MCMachOStreamer::EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size,
332 unsigned ByteAlignment) {
333 // '.lcomm' is equivalent to '.zerofill'.
334 return EmitZerofill(getContext().getMachOSection("__DATA", "__bss",
335 MCSectionMachO::S_ZEROFILL,
336 0, SectionKind::getBSS()),
337 Symbol, Size, ByteAlignment);
338}
339
Daniel Dunbar8751b942009-08-28 05:48:22 +0000340void MCMachOStreamer::EmitZerofill(const MCSection *Section, MCSymbol *Symbol,
Evan Chengc90a1fc2012-06-22 20:14:46 +0000341 uint64_t Size, unsigned ByteAlignment) {
Daniel Dunbar8dc68ab2010-06-16 20:04:22 +0000342 MCSectionData &SectData = getAssembler().getOrCreateSectionData(*Section);
Daniel Dunbard5a8e982009-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 Christopher189f7982013-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 Dunbard5a8e982009-08-28 05:49:21 +0000350
351 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
352
Daniel Dunbar8dc68ab2010-06-16 20:04:22 +0000353 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
Daniel Dunbard5a8e982009-08-28 05:49:21 +0000354
Daniel Dunbare73d49e2010-05-12 22:51:27 +0000355 // Emit an align fragment if necessary.
356 if (ByteAlignment != 1)
Daniel Dunbar1c154132010-05-12 22:56:23 +0000357 new MCAlignFragment(ByteAlignment, 0, 0, ByteAlignment, &SectData);
Daniel Dunbare73d49e2010-05-12 22:51:27 +0000358
Daniel Dunbar4e544872010-05-12 22:51:38 +0000359 MCFragment *F = new MCFillFragment(0, 0, Size, &SectData);
Daniel Dunbard5a8e982009-08-28 05:49:21 +0000360 SD.setFragment(F);
361
Richard Mitton5cc319a2013-09-19 23:21:01 +0000362 AssignSection(Symbol, Section);
Daniel Dunbard5a8e982009-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 Dunbarfb4a6b32009-08-21 09:11:24 +0000367}
368
Eric Christopher070c1ab2010-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 Christopher011c3f12010-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 Christopher482eba02010-05-14 01:50:28 +0000375}
376
Daniel Dunbar2ac0c452010-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 Dunbar8dc68ab2010-06-16 20:04:22 +0000383 getAssembler().getEmitter().EncodeInstruction(Inst, VecOS, Fixups);
Daniel Dunbar2ac0c452010-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 Bendersky64d9a322012-12-07 19:13:57 +0000389 DF->getFixups().push_back(Fixups[i]);
Daniel Dunbar2ac0c452010-05-26 20:37:00 +0000390 }
391 DF->getContents().append(Code.begin(), Code.end());
392}
393
Rafael Espindola99b42372012-01-07 03:13:18 +0000394void MCMachOStreamer::FinishImpl() {
Bill Wendlingda11df02013-09-09 19:48:37 +0000395 EmitFrames(&getAssembler().getBackend(), true);
Rafael Espindola450a5a12011-05-01 15:44:13 +0000396
Daniel Dunbara86de102010-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 Espindola99b42372012-01-07 03:13:18 +0000426 this->MCObjectStreamer::FinishImpl();
Daniel Dunbara86de102010-06-16 20:04:32 +0000427}
428
Evan Cheng78c10ee2011-07-25 23:24:55 +0000429MCStreamer *llvm::createMachOStreamer(MCContext &Context, MCAsmBackend &MAB,
Daniel Dunbarac2884a2010-03-25 22:49:09 +0000430 raw_ostream &OS, MCCodeEmitter *CE,
431 bool RelaxAll) {
Evan Cheng78c10ee2011-07-25 23:24:55 +0000432 MCMachOStreamer *S = new MCMachOStreamer(Context, MAB, OS, CE);
Daniel Dunbarac2884a2010-03-25 22:49:09 +0000433 if (RelaxAll)
434 S->getAssembler().setRelaxAll(true);
435 return S;
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000436}