blob: 0729b7a2f2a54df96d37068af5900f31593d72bd [file] [log] [blame]
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +00001//
2// The LLVM Compiler Infrastructure
3//
4// This file is distributed under the University of Illinois Open Source
5// License. See LICENSE.TXT for details.
6//
7//===----------------------------------------------------------------------===//
8
9#include "llvm/MC/MCStreamer.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000010#include "llvm/MC/MCAsmBackend.h"
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +000011#include "llvm/MC/MCAssembler.h"
Daniel Dunbar4fac7492009-08-27 08:17:51 +000012#include "llvm/MC/MCCodeEmitter.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000013#include "llvm/MC/MCContext.h"
14#include "llvm/MC/MCDwarf.h"
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +000015#include "llvm/MC/MCExpr.h"
Daniel Dunbar4fac7492009-08-27 08:17:51 +000016#include "llvm/MC/MCInst.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000017#include "llvm/MC/MCMachOSymbolFlags.h"
Daniel Dunbar8dc68ab2010-06-16 20:04:22 +000018#include "llvm/MC/MCObjectStreamer.h"
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +000019#include "llvm/MC/MCSection.h"
Kevin Enderbyb07ce602010-08-09 22:52:14 +000020#include "llvm/MC/MCSectionMachO.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000021#include "llvm/MC/MCSymbol.h"
Kevin Enderbyc0957932010-09-30 16:52:03 +000022#include "llvm/Support/Dwarf.h"
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +000023#include "llvm/Support/ErrorHandling.h"
Daniel Dunbar4fac7492009-08-27 08:17:51 +000024#include "llvm/Support/raw_ostream.h"
Daniel Dunbard8036fb2010-03-23 05:09:03 +000025
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +000026using namespace llvm;
27
28namespace {
29
Daniel Dunbar8dc68ab2010-06-16 20:04:22 +000030class MCMachOStreamer : public MCObjectStreamer {
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +000031private:
Rafael Espindolaf89671d2010-11-01 16:27:31 +000032 virtual void EmitInstToData(const MCInst &Inst);
Daniel Dunbar2ac0c452010-05-26 20:37:00 +000033
Jim Grosbach3e965312012-05-18 19:12:01 +000034 void EmitDataRegion(DataRegionData::KindTy Kind);
35 void EmitDataRegionEnd();
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +000036public:
Chandler Carruth5da36652013-01-31 23:29:57 +000037 MCMachOStreamer(MCContext &Context, MCAsmBackend &MAB, raw_ostream &OS,
38 MCCodeEmitter *Emitter)
39 : MCObjectStreamer(SK_MachOStreamer, Context, MAB, OS, Emitter) {}
Daniel Dunbarac2884a2010-03-25 22:49:09 +000040
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +000041 /// @name MCStreamer Interface
42 /// @{
43
Rafael Espindolad80781b2010-09-15 21:48:40 +000044 virtual void InitSections();
Eli Bendersky030f63a2013-01-14 19:04:57 +000045 virtual void InitToTextSection();
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +000046 virtual void EmitLabel(MCSymbol *Symbol);
Reed Kotler2c3a4642012-12-16 04:00:45 +000047 virtual void EmitDebugLabel(MCSymbol *Symbol);
Rafael Espindola8bca4102011-04-28 12:50:37 +000048 virtual void EmitEHSymAttributes(const MCSymbol *Symbol,
49 MCSymbol *EHSymbol);
Chris Lattnera5ad93a2010-01-23 06:39:22 +000050 virtual void EmitAssemblerFlag(MCAssemblerFlag Flag);
Daniel Dunbara94c3392013-01-18 01:26:07 +000051 virtual void EmitLinkerOptions(ArrayRef<std::string> Options);
Jim Grosbach3e965312012-05-18 19:12:01 +000052 virtual void EmitDataRegion(MCDataRegionType Kind);
Jim Grosbachce792992010-11-05 22:08:08 +000053 virtual void EmitThumbFunc(MCSymbol *Func);
Chris Lattnera5ad93a2010-01-23 06:39:22 +000054 virtual void EmitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +000055 virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue);
Chris Lattner9eb158d2010-01-23 07:47:02 +000056 virtual void EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
Daniel Dunbar7092c7e2009-08-30 06:17:16 +000057 unsigned ByteAlignment);
Chris Lattnerb54b9dd2010-05-08 19:54:22 +000058 virtual void BeginCOFFSymbolDef(const MCSymbol *Symbol) {
Craig Topper85814382012-02-07 05:05:23 +000059 llvm_unreachable("macho doesn't support this directive");
Chris Lattnerb54b9dd2010-05-08 19:54:22 +000060 }
61 virtual void EmitCOFFSymbolStorageClass(int StorageClass) {
Craig Topper85814382012-02-07 05:05:23 +000062 llvm_unreachable("macho doesn't support this directive");
Chris Lattnerb54b9dd2010-05-08 19:54:22 +000063 }
64 virtual void EmitCOFFSymbolType(int Type) {
Craig Topper85814382012-02-07 05:05:23 +000065 llvm_unreachable("macho doesn't support this directive");
Chris Lattnerb54b9dd2010-05-08 19:54:22 +000066 }
67 virtual void EndCOFFSymbolDef() {
Craig Topper85814382012-02-07 05:05:23 +000068 llvm_unreachable("macho doesn't support this directive");
Chris Lattnerb54b9dd2010-05-08 19:54:22 +000069 }
Chris Lattner99328ad2010-01-25 07:52:13 +000070 virtual void EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) {
Craig Topper85814382012-02-07 05:05:23 +000071 llvm_unreachable("macho doesn't support this directive");
Chris Lattner99328ad2010-01-25 07:52:13 +000072 }
Benjamin Kramer36a16012011-09-01 23:04:27 +000073 virtual void EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size,
Benjamin Kramer39646d92012-09-07 17:25:13 +000074 unsigned ByteAlignment);
Daniel Dunbar8751b942009-08-28 05:48:22 +000075 virtual void EmitZerofill(const MCSection *Section, MCSymbol *Symbol = 0,
Evan Chengc90a1fc2012-06-22 20:14:46 +000076 uint64_t Size = 0, unsigned ByteAlignment = 0);
Eric Christopher011c3f12010-05-18 21:26:41 +000077 virtual void EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol,
78 uint64_t Size, unsigned ByteAlignment = 0);
Daniel Dunbar2ac0c452010-05-26 20:37:00 +000079
Chris Lattnera6594fc2010-01-25 18:58:59 +000080 virtual void EmitFileDirective(StringRef Filename) {
Daniel Dunbar96749672010-07-19 20:44:20 +000081 // FIXME: Just ignore the .file; it isn't important enough to fail the
82 // entire assembly.
83
84 //report_fatal_error("unsupported directive: '.file'");
Chris Lattnera6594fc2010-01-25 18:58:59 +000085 }
Daniel Dunbar2ac0c452010-05-26 20:37:00 +000086
Rafael Espindola99b42372012-01-07 03:13:18 +000087 virtual void FinishImpl();
Daniel Dunbara86de102010-06-16 20:04:32 +000088
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +000089 /// @}
Chandler Carruth5da36652013-01-31 23:29:57 +000090
91 static bool classof(const MCStreamer *S) {
92 return S->getKind() == SK_MachOStreamer;
93 }
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +000094};
95
96} // end anonymous namespace.
97
Rafael Espindolad80781b2010-09-15 21:48:40 +000098void MCMachOStreamer::InitSections() {
Eli Bendersky030f63a2013-01-14 19:04:57 +000099 InitToTextSection();
100}
Rafael Espindolad80781b2010-09-15 21:48:40 +0000101
Eli Bendersky030f63a2013-01-14 19:04:57 +0000102void MCMachOStreamer::InitToTextSection() {
103 SwitchSection(getContext().getMachOSection(
104 "__TEXT", "__text",
105 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS, 0,
106 SectionKind::getText()));
Rafael Espindolad80781b2010-09-15 21:48:40 +0000107}
108
Rafael Espindola8bca4102011-04-28 12:50:37 +0000109void MCMachOStreamer::EmitEHSymAttributes(const MCSymbol *Symbol,
110 MCSymbol *EHSymbol) {
111 MCSymbolData &SD =
112 getAssembler().getOrCreateSymbolData(*Symbol);
113 if (SD.isExternal())
114 EmitSymbolAttribute(EHSymbol, MCSA_Global);
115 if (SD.getFlags() & SF_WeakDefinition)
116 EmitSymbolAttribute(EHSymbol, MCSA_WeakDefinition);
Rafael Espindola97f7f4e2011-04-30 16:22:46 +0000117 if (SD.isPrivateExtern())
118 EmitSymbolAttribute(EHSymbol, MCSA_PrivateExtern);
Rafael Espindola8bca4102011-04-28 12:50:37 +0000119}
120
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000121void MCMachOStreamer::EmitLabel(MCSymbol *Symbol) {
Rafael Espindolaba210242010-11-28 16:22:59 +0000122 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
Rafael Espindolaba210242010-11-28 16:22:59 +0000123
Rafael Espindolaea4afa92010-11-28 17:18:55 +0000124 // isSymbolLinkerVisible uses the section.
Peter Collingbournedf39be62013-04-17 21:18:16 +0000125 Symbol->setSection(*getCurrentSection().first);
Daniel Dunbara86de102010-06-16 20:04:32 +0000126 // We have to create a new fragment if this is an atom defining symbol,
127 // fragments cannot span atoms.
Rafael Espindolaea4afa92010-11-28 17:18:55 +0000128 if (getAssembler().isSymbolLinkerVisible(*Symbol))
Peter Collingbournedf39be62013-04-17 21:18:16 +0000129 insert(new MCDataFragment());
Daniel Dunbar071f73d2010-05-10 22:45:09 +0000130
Rafael Espindolaea4afa92010-11-28 17:18:55 +0000131 MCObjectStreamer::EmitLabel(Symbol);
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000132
Rafael Espindolaea4afa92010-11-28 17:18:55 +0000133 MCSymbolData &SD = getAssembler().getSymbolData(*Symbol);
Daniel Dunbarb18d2dd2010-05-17 20:12:31 +0000134 // This causes the reference type flag to be cleared. Darwin 'as' was "trying"
135 // to clear the weak reference and weak definition bits too, but the
136 // implementation was buggy. For now we just try to match 'as', for
137 // diffability.
138 //
139 // FIXME: Cleanup this code, these bits should be emitted based on semantic
140 // properties, not on the order of definition, etc.
141 SD.setFlags(SD.getFlags() & ~SF_ReferenceTypeMask);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000142}
143
Reed Kotler2c3a4642012-12-16 04:00:45 +0000144void MCMachOStreamer::EmitDebugLabel(MCSymbol *Symbol) {
145 EmitLabel(Symbol);
146}
Jim Grosbach3e965312012-05-18 19:12:01 +0000147void MCMachOStreamer::EmitDataRegion(DataRegionData::KindTy Kind) {
Jim Grosbachb4316022012-10-01 22:20:54 +0000148 if (!getAssembler().getBackend().hasDataInCodeSupport())
149 return;
Jim Grosbach3e965312012-05-18 19:12:01 +0000150 // Create a temporary label to mark the start of the data region.
151 MCSymbol *Start = getContext().CreateTempSymbol();
152 EmitLabel(Start);
153 // Record the region for the object writer to use.
154 DataRegionData Data = { Kind, Start, NULL };
155 std::vector<DataRegionData> &Regions = getAssembler().getDataRegions();
156 Regions.push_back(Data);
157}
158
159void MCMachOStreamer::EmitDataRegionEnd() {
Jim Grosbachb4316022012-10-01 22:20:54 +0000160 if (!getAssembler().getBackend().hasDataInCodeSupport())
161 return;
Jim Grosbach3e965312012-05-18 19:12:01 +0000162 std::vector<DataRegionData> &Regions = getAssembler().getDataRegions();
163 assert(Regions.size() && "Mismatched .end_data_region!");
164 DataRegionData &Data = Regions.back();
165 assert(Data.End == NULL && "Mismatched .end_data_region!");
166 // Create a temporary label to mark the end of the data region.
167 Data.End = getContext().CreateTempSymbol();
168 EmitLabel(Data.End);
169}
170
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000171void MCMachOStreamer::EmitAssemblerFlag(MCAssemblerFlag Flag) {
Jim Grosbach5be6d2a2010-12-08 01:16:55 +0000172 // Let the target do whatever target specific stuff it needs to do.
Jim Grosbachec343382012-01-18 18:52:16 +0000173 getAssembler().getBackend().handleAssemblerFlag(Flag);
Jim Grosbach5be6d2a2010-12-08 01:16:55 +0000174 // Do any generic stuff we need to do.
Daniel Dunbar6009db42009-08-26 21:22:22 +0000175 switch (Flag) {
Jim Grosbachce792992010-11-05 22:08:08 +0000176 case MCAF_SyntaxUnified: return; // no-op here.
Evan Chengbd27f5a2011-07-27 00:38:12 +0000177 case MCAF_Code16: return; // Change parsing mode; no-op here.
178 case MCAF_Code32: return; // Change parsing mode; no-op here.
179 case MCAF_Code64: return; // Change parsing mode; no-op here.
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000180 case MCAF_SubsectionsViaSymbols:
Daniel Dunbar8dc68ab2010-06-16 20:04:22 +0000181 getAssembler().setSubsectionsViaSymbols(true);
Daniel Dunbar8c3eaf42009-08-28 07:08:47 +0000182 return;
Daniel Dunbar6009db42009-08-26 21:22:22 +0000183 }
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000184}
185
Daniel Dunbara94c3392013-01-18 01:26:07 +0000186void MCMachOStreamer::EmitLinkerOptions(ArrayRef<std::string> Options) {
187 getAssembler().getLinkerOptions().push_back(Options);
188}
189
Jim Grosbach3e965312012-05-18 19:12:01 +0000190void MCMachOStreamer::EmitDataRegion(MCDataRegionType Kind) {
191 switch (Kind) {
192 case MCDR_DataRegion:
193 EmitDataRegion(DataRegionData::Data);
194 return;
195 case MCDR_DataRegionJT8:
196 EmitDataRegion(DataRegionData::JumpTable8);
197 return;
198 case MCDR_DataRegionJT16:
199 EmitDataRegion(DataRegionData::JumpTable16);
200 return;
201 case MCDR_DataRegionJT32:
202 EmitDataRegion(DataRegionData::JumpTable32);
203 return;
204 case MCDR_DataRegionEnd:
205 EmitDataRegionEnd();
206 return;
207 }
208}
209
Daniel Dunbarb2624ed2010-12-29 14:14:06 +0000210void MCMachOStreamer::EmitThumbFunc(MCSymbol *Symbol) {
Jim Grosbach47500202010-12-14 18:46:57 +0000211 // Remember that the function is a thumb function. Fixup and relocation
212 // values will need adjusted.
Daniel Dunbarb2624ed2010-12-29 14:14:06 +0000213 getAssembler().setIsThumbFunc(Symbol);
214
215 // Mark the thumb bit on the symbol.
216 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
217 SD.setFlags(SD.getFlags() | SF_ThumbFunc);
Jim Grosbachce792992010-11-05 22:08:08 +0000218}
219
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000220void MCMachOStreamer::EmitSymbolAttribute(MCSymbol *Symbol,
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000221 MCSymbolAttr Attribute) {
Daniel Dunbar0c7761b2009-08-24 11:56:58 +0000222 // Indirect symbols are handled differently, to match how 'as' handles
223 // them. This makes writing matching .o files easier.
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000224 if (Attribute == MCSA_IndirectSymbol) {
Daniel Dunbarad7c3d52009-08-26 00:18:21 +0000225 // Note that we intentionally cannot use the symbol data here; this is
226 // important for matching the string table that 'as' generates.
Daniel Dunbar0c7761b2009-08-24 11:56:58 +0000227 IndirectSymbolData ISD;
228 ISD.Symbol = Symbol;
Daniel Dunbar83b46712010-06-16 20:04:25 +0000229 ISD.SectionData = getCurrentSectionData();
Daniel Dunbar8dc68ab2010-06-16 20:04:22 +0000230 getAssembler().getIndirectSymbols().push_back(ISD);
Daniel Dunbar0c7761b2009-08-24 11:56:58 +0000231 return;
232 }
233
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000234 // Adding a symbol attribute always introduces the symbol, note that an
Daniel Dunbar46836a72010-03-10 20:58:29 +0000235 // important side effect of calling getOrCreateSymbolData here is to register
236 // the symbol with the assembler.
Daniel Dunbar8dc68ab2010-06-16 20:04:22 +0000237 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000238
239 // The implementation of symbol attributes is designed to match 'as', but it
240 // leaves much to desired. It doesn't really make sense to arbitrarily add and
241 // remove flags, but 'as' allows this (in particular, see .desc).
242 //
243 // In the future it might be worth trying to make these operations more well
244 // defined.
Daniel Dunbar3edd9bb2009-08-22 11:41:10 +0000245 switch (Attribute) {
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000246 case MCSA_Invalid:
Chris Lattnered0ab152010-01-25 18:30:45 +0000247 case MCSA_ELF_TypeFunction:
248 case MCSA_ELF_TypeIndFunction:
249 case MCSA_ELF_TypeObject:
250 case MCSA_ELF_TypeTLS:
251 case MCSA_ELF_TypeCommon:
252 case MCSA_ELF_TypeNoType:
Rafael Espindolae13a0ff2010-11-13 04:51:02 +0000253 case MCSA_ELF_TypeGnuUniqueObject:
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000254 case MCSA_Hidden:
Chandler Carruth8bcf9492011-07-25 21:21:08 +0000255 case MCSA_IndirectSymbol:
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000256 case MCSA_Internal:
257 case MCSA_Protected:
258 case MCSA_Weak:
259 case MCSA_Local:
Craig Topper85814382012-02-07 05:05:23 +0000260 llvm_unreachable("Invalid symbol attribute for Mach-O!");
Daniel Dunbar3edd9bb2009-08-22 11:41:10 +0000261
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000262 case MCSA_Global:
Daniel Dunbar8f4d1462009-08-28 07:08:35 +0000263 SD.setExternal(true);
Daniel Dunbarb18d2dd2010-05-17 20:12:31 +0000264 // This effectively clears the undefined lazy bit, in Darwin 'as', although
265 // it isn't very consistent because it implements this as part of symbol
266 // lookup.
267 //
268 // FIXME: Cleanup this code, these bits should be emitted based on semantic
269 // properties, not on the order of definition, etc.
270 SD.setFlags(SD.getFlags() & ~SF_ReferenceTypeUndefinedLazy);
Daniel Dunbar3edd9bb2009-08-22 11:41:10 +0000271 break;
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000272
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000273 case MCSA_LazyReference:
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000274 // FIXME: This requires -dynamic.
275 SD.setFlags(SD.getFlags() | SF_NoDeadStrip);
276 if (Symbol->isUndefined())
277 SD.setFlags(SD.getFlags() | SF_ReferenceTypeUndefinedLazy);
278 break;
279
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000280 // Since .reference sets the no dead strip bit, it is equivalent to
281 // .no_dead_strip in practice.
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000282 case MCSA_Reference:
283 case MCSA_NoDeadStrip:
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000284 SD.setFlags(SD.getFlags() | SF_NoDeadStrip);
285 break;
286
Kevin Enderbye8e98d72010-11-19 18:39:33 +0000287 case MCSA_SymbolResolver:
288 SD.setFlags(SD.getFlags() | SF_SymbolResolver);
289 break;
290
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000291 case MCSA_PrivateExtern:
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000292 SD.setExternal(true);
293 SD.setPrivateExtern(true);
294 break;
295
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000296 case MCSA_WeakReference:
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000297 // FIXME: This requires -dynamic.
298 if (Symbol->isUndefined())
299 SD.setFlags(SD.getFlags() | SF_WeakReference);
300 break;
301
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000302 case MCSA_WeakDefinition:
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000303 // FIXME: 'as' enforces that this is defined and global. The manual claims
304 // it has to be in a coalesced section, but this isn't enforced.
305 SD.setFlags(SD.getFlags() | SF_WeakDefinition);
306 break;
Kevin Enderbyf59cac52010-07-08 17:22:42 +0000307
308 case MCSA_WeakDefAutoPrivate:
309 SD.setFlags(SD.getFlags() | SF_WeakDefinition | SF_WeakReference);
310 break;
Daniel Dunbar3edd9bb2009-08-22 11:41:10 +0000311 }
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000312}
313
314void MCMachOStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000315 // Encode the 'desc' value into the lowest implementation defined bits.
Daniel Dunbar2ac0c452010-05-26 20:37:00 +0000316 assert(DescValue == (DescValue & SF_DescFlagsMask) &&
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000317 "Invalid .desc value!");
Daniel Dunbar8dc68ab2010-06-16 20:04:22 +0000318 getAssembler().getOrCreateSymbolData(*Symbol).setFlags(
319 DescValue & SF_DescFlagsMask);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000320}
321
Chris Lattner9eb158d2010-01-23 07:47:02 +0000322void MCMachOStreamer::EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000323 unsigned ByteAlignment) {
Daniel Dunbar8f4d1462009-08-28 07:08:35 +0000324 // FIXME: Darwin 'as' does appear to allow redef of a .comm by itself.
325 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
326
Daniel Dunbar8dc68ab2010-06-16 20:04:22 +0000327 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
Daniel Dunbar8f4d1462009-08-28 07:08:35 +0000328 SD.setExternal(true);
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000329 SD.setCommon(Size, ByteAlignment);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000330}
331
Benjamin Kramer39646d92012-09-07 17:25:13 +0000332void MCMachOStreamer::EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size,
333 unsigned ByteAlignment) {
334 // '.lcomm' is equivalent to '.zerofill'.
335 return EmitZerofill(getContext().getMachOSection("__DATA", "__bss",
336 MCSectionMachO::S_ZEROFILL,
337 0, SectionKind::getBSS()),
338 Symbol, Size, ByteAlignment);
339}
340
Daniel Dunbar8751b942009-08-28 05:48:22 +0000341void MCMachOStreamer::EmitZerofill(const MCSection *Section, MCSymbol *Symbol,
Evan Chengc90a1fc2012-06-22 20:14:46 +0000342 uint64_t Size, unsigned ByteAlignment) {
Daniel Dunbar8dc68ab2010-06-16 20:04:22 +0000343 MCSectionData &SectData = getAssembler().getOrCreateSectionData(*Section);
Daniel Dunbard5a8e982009-08-28 05:49:21 +0000344
345 // The symbol may not be present, which only creates the section.
346 if (!Symbol)
347 return;
348
Eric Christopher189f7982013-08-07 21:13:01 +0000349 // On darwin all virtual sections have zerofill type.
350 assert(Section->isVirtualSection() && "Section does not have zerofill type!");
Daniel Dunbard5a8e982009-08-28 05:49:21 +0000351
352 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
353
Daniel Dunbar8dc68ab2010-06-16 20:04:22 +0000354 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
Daniel Dunbard5a8e982009-08-28 05:49:21 +0000355
Daniel Dunbare73d49e2010-05-12 22:51:27 +0000356 // Emit an align fragment if necessary.
357 if (ByteAlignment != 1)
Daniel Dunbar1c154132010-05-12 22:56:23 +0000358 new MCAlignFragment(ByteAlignment, 0, 0, ByteAlignment, &SectData);
Daniel Dunbare73d49e2010-05-12 22:51:27 +0000359
Daniel Dunbar4e544872010-05-12 22:51:38 +0000360 MCFragment *F = new MCFillFragment(0, 0, Size, &SectData);
Daniel Dunbard5a8e982009-08-28 05:49:21 +0000361 SD.setFragment(F);
362
363 Symbol->setSection(*Section);
364
365 // Update the maximum alignment on the zero fill section if necessary.
366 if (ByteAlignment > SectData.getAlignment())
367 SectData.setAlignment(ByteAlignment);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000368}
369
Eric Christopher070c1ab2010-05-21 23:03:53 +0000370// This should always be called with the thread local bss section. Like the
371// .zerofill directive this doesn't actually switch sections on us.
Eric Christopher011c3f12010-05-18 21:26:41 +0000372void MCMachOStreamer::EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol,
373 uint64_t Size, unsigned ByteAlignment) {
374 EmitZerofill(Section, Symbol, Size, ByteAlignment);
375 return;
Eric Christopher482eba02010-05-14 01:50:28 +0000376}
377
Daniel Dunbar2ac0c452010-05-26 20:37:00 +0000378void MCMachOStreamer::EmitInstToData(const MCInst &Inst) {
379 MCDataFragment *DF = getOrCreateDataFragment();
380
381 SmallVector<MCFixup, 4> Fixups;
382 SmallString<256> Code;
383 raw_svector_ostream VecOS(Code);
Daniel Dunbar8dc68ab2010-06-16 20:04:22 +0000384 getAssembler().getEmitter().EncodeInstruction(Inst, VecOS, Fixups);
Daniel Dunbar2ac0c452010-05-26 20:37:00 +0000385 VecOS.flush();
386
387 // Add the fixups and data.
388 for (unsigned i = 0, e = Fixups.size(); i != e; ++i) {
389 Fixups[i].setOffset(Fixups[i].getOffset() + DF->getContents().size());
Eli Bendersky64d9a322012-12-07 19:13:57 +0000390 DF->getFixups().push_back(Fixups[i]);
Daniel Dunbar2ac0c452010-05-26 20:37:00 +0000391 }
392 DF->getContents().append(Code.begin(), Code.end());
393}
394
Rafael Espindola99b42372012-01-07 03:13:18 +0000395void MCMachOStreamer::FinishImpl() {
Rafael Espindolac25dad82011-05-10 03:14:15 +0000396 EmitFrames(true);
Rafael Espindola450a5a12011-05-01 15:44:13 +0000397
Daniel Dunbara86de102010-06-16 20:04:32 +0000398 // We have to set the fragment atom associations so we can relax properly for
399 // Mach-O.
400
401 // First, scan the symbol table to build a lookup table from fragments to
402 // defining symbols.
403 DenseMap<const MCFragment*, MCSymbolData*> DefiningSymbolMap;
404 for (MCAssembler::symbol_iterator it = getAssembler().symbol_begin(),
405 ie = getAssembler().symbol_end(); it != ie; ++it) {
406 if (getAssembler().isSymbolLinkerVisible(it->getSymbol()) &&
407 it->getFragment()) {
408 // An atom defining symbol should never be internal to a fragment.
409 assert(it->getOffset() == 0 && "Invalid offset in atom defining symbol!");
410 DefiningSymbolMap[it->getFragment()] = it;
411 }
412 }
413
414 // Set the fragment atom associations by tracking the last seen atom defining
415 // symbol.
416 for (MCAssembler::iterator it = getAssembler().begin(),
417 ie = getAssembler().end(); it != ie; ++it) {
418 MCSymbolData *CurrentAtom = 0;
419 for (MCSectionData::iterator it2 = it->begin(),
420 ie2 = it->end(); it2 != ie2; ++it2) {
421 if (MCSymbolData *SD = DefiningSymbolMap.lookup(it2))
422 CurrentAtom = SD;
423 it2->setAtom(CurrentAtom);
424 }
425 }
426
Rafael Espindola99b42372012-01-07 03:13:18 +0000427 this->MCObjectStreamer::FinishImpl();
Daniel Dunbara86de102010-06-16 20:04:32 +0000428}
429
Evan Cheng78c10ee2011-07-25 23:24:55 +0000430MCStreamer *llvm::createMachOStreamer(MCContext &Context, MCAsmBackend &MAB,
Daniel Dunbarac2884a2010-03-25 22:49:09 +0000431 raw_ostream &OS, MCCodeEmitter *CE,
432 bool RelaxAll) {
Evan Cheng78c10ee2011-07-25 23:24:55 +0000433 MCMachOStreamer *S = new MCMachOStreamer(Context, MAB, OS, CE);
Daniel Dunbarac2884a2010-03-25 22:49:09 +0000434 if (RelaxAll)
435 S->getAssembler().setRelaxAll(true);
436 return S;
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000437}