blob: 82ccdd4ac07c4ff3296fdc3fed86ac0a1fab21c4 [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:
Evan Cheng78c10ee2011-07-25 23:24:55 +000037 MCMachOStreamer(MCContext &Context, MCAsmBackend &MAB,
Daniel Dunbar8dc68ab2010-06-16 20:04:22 +000038 raw_ostream &OS, MCCodeEmitter *Emitter)
Evan Cheng78c10ee2011-07-25 23:24:55 +000039 : MCObjectStreamer(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();
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +000045 virtual void EmitLabel(MCSymbol *Symbol);
Reed Kotler2c3a4642012-12-16 04:00:45 +000046 virtual void EmitDebugLabel(MCSymbol *Symbol);
Rafael Espindola8bca4102011-04-28 12:50:37 +000047 virtual void EmitEHSymAttributes(const MCSymbol *Symbol,
48 MCSymbol *EHSymbol);
Chris Lattnera5ad93a2010-01-23 06:39:22 +000049 virtual void EmitAssemblerFlag(MCAssemblerFlag Flag);
Jim Grosbach3e965312012-05-18 19:12:01 +000050 virtual void EmitDataRegion(MCDataRegionType Kind);
Jim Grosbachce792992010-11-05 22:08:08 +000051 virtual void EmitThumbFunc(MCSymbol *Func);
Chris Lattnera5ad93a2010-01-23 06:39:22 +000052 virtual void EmitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +000053 virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue);
Chris Lattner9eb158d2010-01-23 07:47:02 +000054 virtual void EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
Daniel Dunbar7092c7e2009-08-30 06:17:16 +000055 unsigned ByteAlignment);
Chris Lattnerb54b9dd2010-05-08 19:54:22 +000056 virtual void BeginCOFFSymbolDef(const MCSymbol *Symbol) {
Craig Topper85814382012-02-07 05:05:23 +000057 llvm_unreachable("macho doesn't support this directive");
Chris Lattnerb54b9dd2010-05-08 19:54:22 +000058 }
59 virtual void EmitCOFFSymbolStorageClass(int StorageClass) {
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 EmitCOFFSymbolType(int Type) {
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 EndCOFFSymbolDef() {
Craig Topper85814382012-02-07 05:05:23 +000066 llvm_unreachable("macho doesn't support this directive");
Chris Lattnerb54b9dd2010-05-08 19:54:22 +000067 }
Chris Lattner99328ad2010-01-25 07:52:13 +000068 virtual void EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) {
Craig Topper85814382012-02-07 05:05:23 +000069 llvm_unreachable("macho doesn't support this directive");
Chris Lattner99328ad2010-01-25 07:52:13 +000070 }
Benjamin Kramer36a16012011-09-01 23:04:27 +000071 virtual void EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size,
Benjamin Kramer39646d92012-09-07 17:25:13 +000072 unsigned ByteAlignment);
Daniel Dunbar8751b942009-08-28 05:48:22 +000073 virtual void EmitZerofill(const MCSection *Section, MCSymbol *Symbol = 0,
Evan Chengc90a1fc2012-06-22 20:14:46 +000074 uint64_t Size = 0, unsigned ByteAlignment = 0);
Eric Christopher011c3f12010-05-18 21:26:41 +000075 virtual void EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol,
76 uint64_t Size, unsigned ByteAlignment = 0);
Daniel Dunbar2ac0c452010-05-26 20:37:00 +000077
Chris Lattnera6594fc2010-01-25 18:58:59 +000078 virtual void EmitFileDirective(StringRef Filename) {
Daniel Dunbar96749672010-07-19 20:44:20 +000079 // FIXME: Just ignore the .file; it isn't important enough to fail the
80 // entire assembly.
81
82 //report_fatal_error("unsupported directive: '.file'");
Chris Lattnera6594fc2010-01-25 18:58:59 +000083 }
Daniel Dunbar2ac0c452010-05-26 20:37:00 +000084
Rafael Espindola99b42372012-01-07 03:13:18 +000085 virtual void FinishImpl();
Daniel Dunbara86de102010-06-16 20:04:32 +000086
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +000087 /// @}
88};
89
90} // end anonymous namespace.
91
Rafael Espindolad80781b2010-09-15 21:48:40 +000092void MCMachOStreamer::InitSections() {
93 SwitchSection(getContext().getMachOSection("__TEXT", "__text",
94 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
95 0, SectionKind::getText()));
96
97}
98
Rafael Espindola8bca4102011-04-28 12:50:37 +000099void MCMachOStreamer::EmitEHSymAttributes(const MCSymbol *Symbol,
100 MCSymbol *EHSymbol) {
101 MCSymbolData &SD =
102 getAssembler().getOrCreateSymbolData(*Symbol);
103 if (SD.isExternal())
104 EmitSymbolAttribute(EHSymbol, MCSA_Global);
105 if (SD.getFlags() & SF_WeakDefinition)
106 EmitSymbolAttribute(EHSymbol, MCSA_WeakDefinition);
Rafael Espindola97f7f4e2011-04-30 16:22:46 +0000107 if (SD.isPrivateExtern())
108 EmitSymbolAttribute(EHSymbol, MCSA_PrivateExtern);
Rafael Espindola8bca4102011-04-28 12:50:37 +0000109}
110
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000111void MCMachOStreamer::EmitLabel(MCSymbol *Symbol) {
Rafael Espindolaba210242010-11-28 16:22:59 +0000112 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
Rafael Espindolaba210242010-11-28 16:22:59 +0000113
Rafael Espindolaea4afa92010-11-28 17:18:55 +0000114 // isSymbolLinkerVisible uses the section.
Rafael Espindola7768a9d2011-02-16 01:08:29 +0000115 Symbol->setSection(*getCurrentSection());
Daniel Dunbara86de102010-06-16 20:04:32 +0000116 // We have to create a new fragment if this is an atom defining symbol,
117 // fragments cannot span atoms.
Rafael Espindolaea4afa92010-11-28 17:18:55 +0000118 if (getAssembler().isSymbolLinkerVisible(*Symbol))
Daniel Dunbara86de102010-06-16 20:04:32 +0000119 new MCDataFragment(getCurrentSectionData());
Daniel Dunbar071f73d2010-05-10 22:45:09 +0000120
Rafael Espindolaea4afa92010-11-28 17:18:55 +0000121 MCObjectStreamer::EmitLabel(Symbol);
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000122
Rafael Espindolaea4afa92010-11-28 17:18:55 +0000123 MCSymbolData &SD = getAssembler().getSymbolData(*Symbol);
Daniel Dunbarb18d2dd2010-05-17 20:12:31 +0000124 // This causes the reference type flag to be cleared. Darwin 'as' was "trying"
125 // to clear the weak reference and weak definition bits too, but the
126 // implementation was buggy. For now we just try to match 'as', for
127 // diffability.
128 //
129 // FIXME: Cleanup this code, these bits should be emitted based on semantic
130 // properties, not on the order of definition, etc.
131 SD.setFlags(SD.getFlags() & ~SF_ReferenceTypeMask);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000132}
133
Reed Kotler2c3a4642012-12-16 04:00:45 +0000134void MCMachOStreamer::EmitDebugLabel(MCSymbol *Symbol) {
135 EmitLabel(Symbol);
136}
Jim Grosbach3e965312012-05-18 19:12:01 +0000137void MCMachOStreamer::EmitDataRegion(DataRegionData::KindTy Kind) {
Jim Grosbachb4316022012-10-01 22:20:54 +0000138 if (!getAssembler().getBackend().hasDataInCodeSupport())
139 return;
Jim Grosbach3e965312012-05-18 19:12:01 +0000140 // Create a temporary label to mark the start of the data region.
141 MCSymbol *Start = getContext().CreateTempSymbol();
142 EmitLabel(Start);
143 // Record the region for the object writer to use.
144 DataRegionData Data = { Kind, Start, NULL };
145 std::vector<DataRegionData> &Regions = getAssembler().getDataRegions();
146 Regions.push_back(Data);
147}
148
149void MCMachOStreamer::EmitDataRegionEnd() {
Jim Grosbachb4316022012-10-01 22:20:54 +0000150 if (!getAssembler().getBackend().hasDataInCodeSupport())
151 return;
Jim Grosbach3e965312012-05-18 19:12:01 +0000152 std::vector<DataRegionData> &Regions = getAssembler().getDataRegions();
153 assert(Regions.size() && "Mismatched .end_data_region!");
154 DataRegionData &Data = Regions.back();
155 assert(Data.End == NULL && "Mismatched .end_data_region!");
156 // Create a temporary label to mark the end of the data region.
157 Data.End = getContext().CreateTempSymbol();
158 EmitLabel(Data.End);
159}
160
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000161void MCMachOStreamer::EmitAssemblerFlag(MCAssemblerFlag Flag) {
Jim Grosbach5be6d2a2010-12-08 01:16:55 +0000162 // Let the target do whatever target specific stuff it needs to do.
Jim Grosbachec343382012-01-18 18:52:16 +0000163 getAssembler().getBackend().handleAssemblerFlag(Flag);
Jim Grosbach5be6d2a2010-12-08 01:16:55 +0000164 // Do any generic stuff we need to do.
Daniel Dunbar6009db42009-08-26 21:22:22 +0000165 switch (Flag) {
Jim Grosbachce792992010-11-05 22:08:08 +0000166 case MCAF_SyntaxUnified: return; // no-op here.
Evan Chengbd27f5a2011-07-27 00:38:12 +0000167 case MCAF_Code16: return; // Change parsing mode; no-op here.
168 case MCAF_Code32: return; // Change parsing mode; no-op here.
169 case MCAF_Code64: return; // Change parsing mode; no-op here.
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000170 case MCAF_SubsectionsViaSymbols:
Daniel Dunbar8dc68ab2010-06-16 20:04:22 +0000171 getAssembler().setSubsectionsViaSymbols(true);
Daniel Dunbar8c3eaf42009-08-28 07:08:47 +0000172 return;
Daniel Dunbar6009db42009-08-26 21:22:22 +0000173 }
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000174}
175
Jim Grosbach3e965312012-05-18 19:12:01 +0000176void MCMachOStreamer::EmitDataRegion(MCDataRegionType Kind) {
177 switch (Kind) {
178 case MCDR_DataRegion:
179 EmitDataRegion(DataRegionData::Data);
180 return;
181 case MCDR_DataRegionJT8:
182 EmitDataRegion(DataRegionData::JumpTable8);
183 return;
184 case MCDR_DataRegionJT16:
185 EmitDataRegion(DataRegionData::JumpTable16);
186 return;
187 case MCDR_DataRegionJT32:
188 EmitDataRegion(DataRegionData::JumpTable32);
189 return;
190 case MCDR_DataRegionEnd:
191 EmitDataRegionEnd();
192 return;
193 }
194}
195
Daniel Dunbarb2624ed2010-12-29 14:14:06 +0000196void MCMachOStreamer::EmitThumbFunc(MCSymbol *Symbol) {
Jim Grosbach47500202010-12-14 18:46:57 +0000197 // Remember that the function is a thumb function. Fixup and relocation
198 // values will need adjusted.
Daniel Dunbarb2624ed2010-12-29 14:14:06 +0000199 getAssembler().setIsThumbFunc(Symbol);
200
201 // Mark the thumb bit on the symbol.
202 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
203 SD.setFlags(SD.getFlags() | SF_ThumbFunc);
Jim Grosbachce792992010-11-05 22:08:08 +0000204}
205
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000206void MCMachOStreamer::EmitSymbolAttribute(MCSymbol *Symbol,
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000207 MCSymbolAttr Attribute) {
Daniel Dunbar0c7761b2009-08-24 11:56:58 +0000208 // Indirect symbols are handled differently, to match how 'as' handles
209 // them. This makes writing matching .o files easier.
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000210 if (Attribute == MCSA_IndirectSymbol) {
Daniel Dunbarad7c3d52009-08-26 00:18:21 +0000211 // Note that we intentionally cannot use the symbol data here; this is
212 // important for matching the string table that 'as' generates.
Daniel Dunbar0c7761b2009-08-24 11:56:58 +0000213 IndirectSymbolData ISD;
214 ISD.Symbol = Symbol;
Daniel Dunbar83b46712010-06-16 20:04:25 +0000215 ISD.SectionData = getCurrentSectionData();
Daniel Dunbar8dc68ab2010-06-16 20:04:22 +0000216 getAssembler().getIndirectSymbols().push_back(ISD);
Daniel Dunbar0c7761b2009-08-24 11:56:58 +0000217 return;
218 }
219
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000220 // Adding a symbol attribute always introduces the symbol, note that an
Daniel Dunbar46836a72010-03-10 20:58:29 +0000221 // important side effect of calling getOrCreateSymbolData here is to register
222 // the symbol with the assembler.
Daniel Dunbar8dc68ab2010-06-16 20:04:22 +0000223 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000224
225 // The implementation of symbol attributes is designed to match 'as', but it
226 // leaves much to desired. It doesn't really make sense to arbitrarily add and
227 // remove flags, but 'as' allows this (in particular, see .desc).
228 //
229 // In the future it might be worth trying to make these operations more well
230 // defined.
Daniel Dunbar3edd9bb2009-08-22 11:41:10 +0000231 switch (Attribute) {
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000232 case MCSA_Invalid:
Chris Lattnered0ab152010-01-25 18:30:45 +0000233 case MCSA_ELF_TypeFunction:
234 case MCSA_ELF_TypeIndFunction:
235 case MCSA_ELF_TypeObject:
236 case MCSA_ELF_TypeTLS:
237 case MCSA_ELF_TypeCommon:
238 case MCSA_ELF_TypeNoType:
Rafael Espindolae13a0ff2010-11-13 04:51:02 +0000239 case MCSA_ELF_TypeGnuUniqueObject:
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000240 case MCSA_Hidden:
Chandler Carruth8bcf9492011-07-25 21:21:08 +0000241 case MCSA_IndirectSymbol:
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000242 case MCSA_Internal:
243 case MCSA_Protected:
244 case MCSA_Weak:
245 case MCSA_Local:
Craig Topper85814382012-02-07 05:05:23 +0000246 llvm_unreachable("Invalid symbol attribute for Mach-O!");
Daniel Dunbar3edd9bb2009-08-22 11:41:10 +0000247
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000248 case MCSA_Global:
Daniel Dunbar8f4d1462009-08-28 07:08:35 +0000249 SD.setExternal(true);
Daniel Dunbarb18d2dd2010-05-17 20:12:31 +0000250 // This effectively clears the undefined lazy bit, in Darwin 'as', although
251 // it isn't very consistent because it implements this as part of symbol
252 // lookup.
253 //
254 // FIXME: Cleanup this code, these bits should be emitted based on semantic
255 // properties, not on the order of definition, etc.
256 SD.setFlags(SD.getFlags() & ~SF_ReferenceTypeUndefinedLazy);
Daniel Dunbar3edd9bb2009-08-22 11:41:10 +0000257 break;
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000258
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000259 case MCSA_LazyReference:
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000260 // FIXME: This requires -dynamic.
261 SD.setFlags(SD.getFlags() | SF_NoDeadStrip);
262 if (Symbol->isUndefined())
263 SD.setFlags(SD.getFlags() | SF_ReferenceTypeUndefinedLazy);
264 break;
265
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000266 // Since .reference sets the no dead strip bit, it is equivalent to
267 // .no_dead_strip in practice.
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000268 case MCSA_Reference:
269 case MCSA_NoDeadStrip:
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000270 SD.setFlags(SD.getFlags() | SF_NoDeadStrip);
271 break;
272
Kevin Enderbye8e98d72010-11-19 18:39:33 +0000273 case MCSA_SymbolResolver:
274 SD.setFlags(SD.getFlags() | SF_SymbolResolver);
275 break;
276
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000277 case MCSA_PrivateExtern:
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000278 SD.setExternal(true);
279 SD.setPrivateExtern(true);
280 break;
281
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000282 case MCSA_WeakReference:
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000283 // FIXME: This requires -dynamic.
284 if (Symbol->isUndefined())
285 SD.setFlags(SD.getFlags() | SF_WeakReference);
286 break;
287
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000288 case MCSA_WeakDefinition:
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000289 // FIXME: 'as' enforces that this is defined and global. The manual claims
290 // it has to be in a coalesced section, but this isn't enforced.
291 SD.setFlags(SD.getFlags() | SF_WeakDefinition);
292 break;
Kevin Enderbyf59cac52010-07-08 17:22:42 +0000293
294 case MCSA_WeakDefAutoPrivate:
295 SD.setFlags(SD.getFlags() | SF_WeakDefinition | SF_WeakReference);
296 break;
Daniel Dunbar3edd9bb2009-08-22 11:41:10 +0000297 }
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000298}
299
300void MCMachOStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000301 // Encode the 'desc' value into the lowest implementation defined bits.
Daniel Dunbar2ac0c452010-05-26 20:37:00 +0000302 assert(DescValue == (DescValue & SF_DescFlagsMask) &&
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000303 "Invalid .desc value!");
Daniel Dunbar8dc68ab2010-06-16 20:04:22 +0000304 getAssembler().getOrCreateSymbolData(*Symbol).setFlags(
305 DescValue & SF_DescFlagsMask);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000306}
307
Chris Lattner9eb158d2010-01-23 07:47:02 +0000308void MCMachOStreamer::EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000309 unsigned ByteAlignment) {
Daniel Dunbar8f4d1462009-08-28 07:08:35 +0000310 // FIXME: Darwin 'as' does appear to allow redef of a .comm by itself.
311 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
312
Daniel Dunbar8dc68ab2010-06-16 20:04:22 +0000313 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
Daniel Dunbar8f4d1462009-08-28 07:08:35 +0000314 SD.setExternal(true);
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000315 SD.setCommon(Size, ByteAlignment);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000316}
317
Benjamin Kramer39646d92012-09-07 17:25:13 +0000318void MCMachOStreamer::EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size,
319 unsigned ByteAlignment) {
320 // '.lcomm' is equivalent to '.zerofill'.
321 return EmitZerofill(getContext().getMachOSection("__DATA", "__bss",
322 MCSectionMachO::S_ZEROFILL,
323 0, SectionKind::getBSS()),
324 Symbol, Size, ByteAlignment);
325}
326
Daniel Dunbar8751b942009-08-28 05:48:22 +0000327void MCMachOStreamer::EmitZerofill(const MCSection *Section, MCSymbol *Symbol,
Evan Chengc90a1fc2012-06-22 20:14:46 +0000328 uint64_t Size, unsigned ByteAlignment) {
Daniel Dunbar8dc68ab2010-06-16 20:04:22 +0000329 MCSectionData &SectData = getAssembler().getOrCreateSectionData(*Section);
Daniel Dunbard5a8e982009-08-28 05:49:21 +0000330
331 // The symbol may not be present, which only creates the section.
332 if (!Symbol)
333 return;
334
335 // FIXME: Assert that this section has the zerofill type.
336
337 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
338
Daniel Dunbar8dc68ab2010-06-16 20:04:22 +0000339 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
Daniel Dunbard5a8e982009-08-28 05:49:21 +0000340
Daniel Dunbare73d49e2010-05-12 22:51:27 +0000341 // Emit an align fragment if necessary.
342 if (ByteAlignment != 1)
Daniel Dunbar1c154132010-05-12 22:56:23 +0000343 new MCAlignFragment(ByteAlignment, 0, 0, ByteAlignment, &SectData);
Daniel Dunbare73d49e2010-05-12 22:51:27 +0000344
Daniel Dunbar4e544872010-05-12 22:51:38 +0000345 MCFragment *F = new MCFillFragment(0, 0, Size, &SectData);
Daniel Dunbard5a8e982009-08-28 05:49:21 +0000346 SD.setFragment(F);
347
348 Symbol->setSection(*Section);
349
350 // Update the maximum alignment on the zero fill section if necessary.
351 if (ByteAlignment > SectData.getAlignment())
352 SectData.setAlignment(ByteAlignment);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000353}
354
Eric Christopher070c1ab2010-05-21 23:03:53 +0000355// This should always be called with the thread local bss section. Like the
356// .zerofill directive this doesn't actually switch sections on us.
Eric Christopher011c3f12010-05-18 21:26:41 +0000357void MCMachOStreamer::EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol,
358 uint64_t Size, unsigned ByteAlignment) {
359 EmitZerofill(Section, Symbol, Size, ByteAlignment);
360 return;
Eric Christopher482eba02010-05-14 01:50:28 +0000361}
362
Daniel Dunbar2ac0c452010-05-26 20:37:00 +0000363void MCMachOStreamer::EmitInstToData(const MCInst &Inst) {
364 MCDataFragment *DF = getOrCreateDataFragment();
365
366 SmallVector<MCFixup, 4> Fixups;
367 SmallString<256> Code;
368 raw_svector_ostream VecOS(Code);
Daniel Dunbar8dc68ab2010-06-16 20:04:22 +0000369 getAssembler().getEmitter().EncodeInstruction(Inst, VecOS, Fixups);
Daniel Dunbar2ac0c452010-05-26 20:37:00 +0000370 VecOS.flush();
371
372 // Add the fixups and data.
373 for (unsigned i = 0, e = Fixups.size(); i != e; ++i) {
374 Fixups[i].setOffset(Fixups[i].getOffset() + DF->getContents().size());
Eli Bendersky64d9a322012-12-07 19:13:57 +0000375 DF->getFixups().push_back(Fixups[i]);
Daniel Dunbar2ac0c452010-05-26 20:37:00 +0000376 }
377 DF->getContents().append(Code.begin(), Code.end());
378}
379
Rafael Espindola99b42372012-01-07 03:13:18 +0000380void MCMachOStreamer::FinishImpl() {
Rafael Espindolac25dad82011-05-10 03:14:15 +0000381 EmitFrames(true);
Rafael Espindola450a5a12011-05-01 15:44:13 +0000382
Daniel Dunbara86de102010-06-16 20:04:32 +0000383 // We have to set the fragment atom associations so we can relax properly for
384 // Mach-O.
385
386 // First, scan the symbol table to build a lookup table from fragments to
387 // defining symbols.
388 DenseMap<const MCFragment*, MCSymbolData*> DefiningSymbolMap;
389 for (MCAssembler::symbol_iterator it = getAssembler().symbol_begin(),
390 ie = getAssembler().symbol_end(); it != ie; ++it) {
391 if (getAssembler().isSymbolLinkerVisible(it->getSymbol()) &&
392 it->getFragment()) {
393 // An atom defining symbol should never be internal to a fragment.
394 assert(it->getOffset() == 0 && "Invalid offset in atom defining symbol!");
395 DefiningSymbolMap[it->getFragment()] = it;
396 }
397 }
398
399 // Set the fragment atom associations by tracking the last seen atom defining
400 // symbol.
401 for (MCAssembler::iterator it = getAssembler().begin(),
402 ie = getAssembler().end(); it != ie; ++it) {
403 MCSymbolData *CurrentAtom = 0;
404 for (MCSectionData::iterator it2 = it->begin(),
405 ie2 = it->end(); it2 != ie2; ++it2) {
406 if (MCSymbolData *SD = DefiningSymbolMap.lookup(it2))
407 CurrentAtom = SD;
408 it2->setAtom(CurrentAtom);
409 }
410 }
411
Rafael Espindola99b42372012-01-07 03:13:18 +0000412 this->MCObjectStreamer::FinishImpl();
Daniel Dunbara86de102010-06-16 20:04:32 +0000413}
414
Evan Cheng78c10ee2011-07-25 23:24:55 +0000415MCStreamer *llvm::createMachOStreamer(MCContext &Context, MCAsmBackend &MAB,
Daniel Dunbarac2884a2010-03-25 22:49:09 +0000416 raw_ostream &OS, MCCodeEmitter *CE,
417 bool RelaxAll) {
Evan Cheng78c10ee2011-07-25 23:24:55 +0000418 MCMachOStreamer *S = new MCMachOStreamer(Context, MAB, OS, CE);
Daniel Dunbarac2884a2010-03-25 22:49:09 +0000419 if (RelaxAll)
420 S->getAssembler().setRelaxAll(true);
421 return S;
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000422}