blob: b152a8c85e24eb4ad58fbc070afff20e20136b42 [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"
10
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +000011#include "llvm/MC/MCAssembler.h"
12#include "llvm/MC/MCContext.h"
Daniel Dunbar4fac7492009-08-27 08:17:51 +000013#include "llvm/MC/MCCodeEmitter.h"
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +000014#include "llvm/MC/MCExpr.h"
Daniel Dunbar4fac7492009-08-27 08:17:51 +000015#include "llvm/MC/MCInst.h"
Daniel Dunbar8dc68ab2010-06-16 20:04:22 +000016#include "llvm/MC/MCObjectStreamer.h"
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +000017#include "llvm/MC/MCSection.h"
18#include "llvm/MC/MCSymbol.h"
Kevin Enderbya6eeb6e2010-05-07 21:44:23 +000019#include "llvm/MC/MCMachOSymbolFlags.h"
Kevin Enderbyb07ce602010-08-09 22:52:14 +000020#include "llvm/MC/MCSectionMachO.h"
21#include "llvm/MC/MCDwarf.h"
Evan Cheng78c10ee2011-07-25 23:24:55 +000022#include "llvm/MC/MCAsmBackend.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:
Evan Cheng78c10ee2011-07-25 23:24:55 +000038 MCMachOStreamer(MCContext &Context, MCAsmBackend &MAB,
Daniel Dunbar8dc68ab2010-06-16 20:04:22 +000039 raw_ostream &OS, MCCodeEmitter *Emitter)
Evan Cheng78c10ee2011-07-25 23:24:55 +000040 : MCObjectStreamer(Context, 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();
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +000046 virtual void EmitLabel(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);
Daniel Dunbar821e3332009-08-31 08:09:28 +000052 virtual void EmitAssignment(MCSymbol *Symbol, const MCExpr *Value);
Chris Lattnera5ad93a2010-01-23 06:39:22 +000053 virtual void EmitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +000054 virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue);
Chris Lattner9eb158d2010-01-23 07:47:02 +000055 virtual void EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
Daniel Dunbar7092c7e2009-08-30 06:17:16 +000056 unsigned ByteAlignment);
Chris Lattnerb54b9dd2010-05-08 19:54:22 +000057 virtual void BeginCOFFSymbolDef(const MCSymbol *Symbol) {
Craig Topper85814382012-02-07 05:05:23 +000058 llvm_unreachable("macho doesn't support this directive");
Chris Lattnerb54b9dd2010-05-08 19:54:22 +000059 }
60 virtual void EmitCOFFSymbolStorageClass(int StorageClass) {
Craig Topper85814382012-02-07 05:05:23 +000061 llvm_unreachable("macho doesn't support this directive");
Chris Lattnerb54b9dd2010-05-08 19:54:22 +000062 }
63 virtual void EmitCOFFSymbolType(int Type) {
Craig Topper85814382012-02-07 05:05:23 +000064 llvm_unreachable("macho doesn't support this directive");
Chris Lattnerb54b9dd2010-05-08 19:54:22 +000065 }
66 virtual void EndCOFFSymbolDef() {
Craig Topper85814382012-02-07 05:05:23 +000067 llvm_unreachable("macho doesn't support this directive");
Chris Lattnerb54b9dd2010-05-08 19:54:22 +000068 }
Chris Lattner99328ad2010-01-25 07:52:13 +000069 virtual void EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) {
Craig Topper85814382012-02-07 05:05:23 +000070 llvm_unreachable("macho doesn't support this directive");
Chris Lattner99328ad2010-01-25 07:52:13 +000071 }
Benjamin Kramer36a16012011-09-01 23:04:27 +000072 virtual void EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size,
Benjamin Kramer39646d92012-09-07 17:25:13 +000073 unsigned ByteAlignment);
Daniel Dunbar8751b942009-08-28 05:48:22 +000074 virtual void EmitZerofill(const MCSection *Section, MCSymbol *Symbol = 0,
Evan Chengc90a1fc2012-06-22 20:14:46 +000075 uint64_t Size = 0, unsigned ByteAlignment = 0);
Eric Christopher011c3f12010-05-18 21:26:41 +000076 virtual void EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol,
77 uint64_t Size, unsigned ByteAlignment = 0);
Chris Lattneraaec2052010-01-19 19:46:13 +000078 virtual void EmitBytes(StringRef Data, unsigned AddrSpace);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +000079 virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0,
80 unsigned ValueSize = 1,
81 unsigned MaxBytesToEmit = 0);
Kevin Enderby6e720482010-02-23 18:26:34 +000082 virtual void EmitCodeAlignment(unsigned ByteAlignment,
83 unsigned MaxBytesToEmit = 0);
Daniel Dunbar2ac0c452010-05-26 20:37:00 +000084
Chris Lattnera6594fc2010-01-25 18:58:59 +000085 virtual void EmitFileDirective(StringRef Filename) {
Daniel Dunbar96749672010-07-19 20:44:20 +000086 // FIXME: Just ignore the .file; it isn't important enough to fail the
87 // entire assembly.
88
89 //report_fatal_error("unsupported directive: '.file'");
Chris Lattnera6594fc2010-01-25 18:58:59 +000090 }
Daniel Dunbar2ac0c452010-05-26 20:37:00 +000091
Rafael Espindola99b42372012-01-07 03:13:18 +000092 virtual void FinishImpl();
Daniel Dunbara86de102010-06-16 20:04:32 +000093
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +000094 /// @}
95};
96
97} // end anonymous namespace.
98
Rafael Espindolad80781b2010-09-15 21:48:40 +000099void MCMachOStreamer::InitSections() {
100 SwitchSection(getContext().getMachOSection("__TEXT", "__text",
101 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
102 0, SectionKind::getText()));
103
104}
105
Rafael Espindola8bca4102011-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 Espindola97f7f4e2011-04-30 16:22:46 +0000114 if (SD.isPrivateExtern())
115 EmitSymbolAttribute(EHSymbol, MCSA_PrivateExtern);
Rafael Espindola8bca4102011-04-28 12:50:37 +0000116}
117
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000118void MCMachOStreamer::EmitLabel(MCSymbol *Symbol) {
Rafael Espindolaba210242010-11-28 16:22:59 +0000119 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
Rafael Espindolaba210242010-11-28 16:22:59 +0000120
Rafael Espindolaea4afa92010-11-28 17:18:55 +0000121 // isSymbolLinkerVisible uses the section.
Rafael Espindola7768a9d2011-02-16 01:08:29 +0000122 Symbol->setSection(*getCurrentSection());
Daniel Dunbara86de102010-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 Espindolaea4afa92010-11-28 17:18:55 +0000125 if (getAssembler().isSymbolLinkerVisible(*Symbol))
Daniel Dunbara86de102010-06-16 20:04:32 +0000126 new MCDataFragment(getCurrentSectionData());
Daniel Dunbar071f73d2010-05-10 22:45:09 +0000127
Rafael Espindolaea4afa92010-11-28 17:18:55 +0000128 MCObjectStreamer::EmitLabel(Symbol);
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000129
Rafael Espindolaea4afa92010-11-28 17:18:55 +0000130 MCSymbolData &SD = getAssembler().getSymbolData(*Symbol);
Daniel Dunbarb18d2dd2010-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 Dunbarfb4a6b32009-08-21 09:11:24 +0000139}
140
Jim Grosbach3e965312012-05-18 19:12:01 +0000141void MCMachOStreamer::EmitDataRegion(DataRegionData::KindTy Kind) {
Jim Grosbachb4316022012-10-01 22:20:54 +0000142 if (!getAssembler().getBackend().hasDataInCodeSupport())
143 return;
Jim Grosbach3e965312012-05-18 19:12:01 +0000144 // Create a temporary label to mark the start of the data region.
145 MCSymbol *Start = getContext().CreateTempSymbol();
146 EmitLabel(Start);
147 // Record the region for the object writer to use.
148 DataRegionData Data = { Kind, Start, NULL };
149 std::vector<DataRegionData> &Regions = getAssembler().getDataRegions();
150 Regions.push_back(Data);
151}
152
153void MCMachOStreamer::EmitDataRegionEnd() {
Jim Grosbachb4316022012-10-01 22:20:54 +0000154 if (!getAssembler().getBackend().hasDataInCodeSupport())
155 return;
Jim Grosbach3e965312012-05-18 19:12:01 +0000156 std::vector<DataRegionData> &Regions = getAssembler().getDataRegions();
157 assert(Regions.size() && "Mismatched .end_data_region!");
158 DataRegionData &Data = Regions.back();
159 assert(Data.End == NULL && "Mismatched .end_data_region!");
160 // Create a temporary label to mark the end of the data region.
161 Data.End = getContext().CreateTempSymbol();
162 EmitLabel(Data.End);
163}
164
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000165void MCMachOStreamer::EmitAssemblerFlag(MCAssemblerFlag Flag) {
Jim Grosbach5be6d2a2010-12-08 01:16:55 +0000166 // Let the target do whatever target specific stuff it needs to do.
Jim Grosbachec343382012-01-18 18:52:16 +0000167 getAssembler().getBackend().handleAssemblerFlag(Flag);
Jim Grosbach5be6d2a2010-12-08 01:16:55 +0000168 // Do any generic stuff we need to do.
Daniel Dunbar6009db42009-08-26 21:22:22 +0000169 switch (Flag) {
Jim Grosbachce792992010-11-05 22:08:08 +0000170 case MCAF_SyntaxUnified: return; // no-op here.
Evan Chengbd27f5a2011-07-27 00:38:12 +0000171 case MCAF_Code16: return; // Change parsing mode; no-op here.
172 case MCAF_Code32: return; // Change parsing mode; no-op here.
173 case MCAF_Code64: return; // Change parsing mode; no-op here.
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000174 case MCAF_SubsectionsViaSymbols:
Daniel Dunbar8dc68ab2010-06-16 20:04:22 +0000175 getAssembler().setSubsectionsViaSymbols(true);
Daniel Dunbar8c3eaf42009-08-28 07:08:47 +0000176 return;
Daniel Dunbar6009db42009-08-26 21:22:22 +0000177 }
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000178}
179
Jim Grosbach3e965312012-05-18 19:12:01 +0000180void MCMachOStreamer::EmitDataRegion(MCDataRegionType Kind) {
181 switch (Kind) {
182 case MCDR_DataRegion:
183 EmitDataRegion(DataRegionData::Data);
184 return;
185 case MCDR_DataRegionJT8:
186 EmitDataRegion(DataRegionData::JumpTable8);
187 return;
188 case MCDR_DataRegionJT16:
189 EmitDataRegion(DataRegionData::JumpTable16);
190 return;
191 case MCDR_DataRegionJT32:
192 EmitDataRegion(DataRegionData::JumpTable32);
193 return;
194 case MCDR_DataRegionEnd:
195 EmitDataRegionEnd();
196 return;
197 }
198}
199
Daniel Dunbarb2624ed2010-12-29 14:14:06 +0000200void MCMachOStreamer::EmitThumbFunc(MCSymbol *Symbol) {
Jim Grosbach47500202010-12-14 18:46:57 +0000201 // Remember that the function is a thumb function. Fixup and relocation
202 // values will need adjusted.
Daniel Dunbarb2624ed2010-12-29 14:14:06 +0000203 getAssembler().setIsThumbFunc(Symbol);
204
205 // Mark the thumb bit on the symbol.
206 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
207 SD.setFlags(SD.getFlags() | SF_ThumbFunc);
Jim Grosbachce792992010-11-05 22:08:08 +0000208}
209
Daniel Dunbar821e3332009-08-31 08:09:28 +0000210void MCMachOStreamer::EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) {
Michael J. Spencer8067adc2010-07-19 06:13:10 +0000211 // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into
212 // MCObjectStreamer.
Daniel Dunbara4d86672009-10-16 01:58:23 +0000213 // FIXME: Lift context changes into super class.
Daniel Dunbar8dc68ab2010-06-16 20:04:22 +0000214 getAssembler().getOrCreateSymbolData(*Symbol);
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000215 Symbol->setVariableValue(AddValueSymbols(Value));
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000216}
217
218void MCMachOStreamer::EmitSymbolAttribute(MCSymbol *Symbol,
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000219 MCSymbolAttr Attribute) {
Daniel Dunbar0c7761b2009-08-24 11:56:58 +0000220 // Indirect symbols are handled differently, to match how 'as' handles
221 // them. This makes writing matching .o files easier.
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000222 if (Attribute == MCSA_IndirectSymbol) {
Daniel Dunbarad7c3d52009-08-26 00:18:21 +0000223 // Note that we intentionally cannot use the symbol data here; this is
224 // important for matching the string table that 'as' generates.
Daniel Dunbar0c7761b2009-08-24 11:56:58 +0000225 IndirectSymbolData ISD;
226 ISD.Symbol = Symbol;
Daniel Dunbar83b46712010-06-16 20:04:25 +0000227 ISD.SectionData = getCurrentSectionData();
Daniel Dunbar8dc68ab2010-06-16 20:04:22 +0000228 getAssembler().getIndirectSymbols().push_back(ISD);
Daniel Dunbar0c7761b2009-08-24 11:56:58 +0000229 return;
230 }
231
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000232 // Adding a symbol attribute always introduces the symbol, note that an
Daniel Dunbar46836a72010-03-10 20:58:29 +0000233 // important side effect of calling getOrCreateSymbolData here is to register
234 // the symbol with the assembler.
Daniel Dunbar8dc68ab2010-06-16 20:04:22 +0000235 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000236
237 // The implementation of symbol attributes is designed to match 'as', but it
238 // leaves much to desired. It doesn't really make sense to arbitrarily add and
239 // remove flags, but 'as' allows this (in particular, see .desc).
240 //
241 // In the future it might be worth trying to make these operations more well
242 // defined.
Daniel Dunbar3edd9bb2009-08-22 11:41:10 +0000243 switch (Attribute) {
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000244 case MCSA_Invalid:
Chris Lattnered0ab152010-01-25 18:30:45 +0000245 case MCSA_ELF_TypeFunction:
246 case MCSA_ELF_TypeIndFunction:
247 case MCSA_ELF_TypeObject:
248 case MCSA_ELF_TypeTLS:
249 case MCSA_ELF_TypeCommon:
250 case MCSA_ELF_TypeNoType:
Rafael Espindolae13a0ff2010-11-13 04:51:02 +0000251 case MCSA_ELF_TypeGnuUniqueObject:
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000252 case MCSA_Hidden:
Chandler Carruth8bcf9492011-07-25 21:21:08 +0000253 case MCSA_IndirectSymbol:
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000254 case MCSA_Internal:
255 case MCSA_Protected:
256 case MCSA_Weak:
257 case MCSA_Local:
Craig Topper85814382012-02-07 05:05:23 +0000258 llvm_unreachable("Invalid symbol attribute for Mach-O!");
Daniel Dunbar3edd9bb2009-08-22 11:41:10 +0000259
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000260 case MCSA_Global:
Daniel Dunbar8f4d1462009-08-28 07:08:35 +0000261 SD.setExternal(true);
Daniel Dunbarb18d2dd2010-05-17 20:12:31 +0000262 // This effectively clears the undefined lazy bit, in Darwin 'as', although
263 // it isn't very consistent because it implements this as part of symbol
264 // lookup.
265 //
266 // FIXME: Cleanup this code, these bits should be emitted based on semantic
267 // properties, not on the order of definition, etc.
268 SD.setFlags(SD.getFlags() & ~SF_ReferenceTypeUndefinedLazy);
Daniel Dunbar3edd9bb2009-08-22 11:41:10 +0000269 break;
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000270
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000271 case MCSA_LazyReference:
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000272 // FIXME: This requires -dynamic.
273 SD.setFlags(SD.getFlags() | SF_NoDeadStrip);
274 if (Symbol->isUndefined())
275 SD.setFlags(SD.getFlags() | SF_ReferenceTypeUndefinedLazy);
276 break;
277
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000278 // Since .reference sets the no dead strip bit, it is equivalent to
279 // .no_dead_strip in practice.
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000280 case MCSA_Reference:
281 case MCSA_NoDeadStrip:
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000282 SD.setFlags(SD.getFlags() | SF_NoDeadStrip);
283 break;
284
Kevin Enderbye8e98d72010-11-19 18:39:33 +0000285 case MCSA_SymbolResolver:
286 SD.setFlags(SD.getFlags() | SF_SymbolResolver);
287 break;
288
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000289 case MCSA_PrivateExtern:
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000290 SD.setExternal(true);
291 SD.setPrivateExtern(true);
292 break;
293
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000294 case MCSA_WeakReference:
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000295 // FIXME: This requires -dynamic.
296 if (Symbol->isUndefined())
297 SD.setFlags(SD.getFlags() | SF_WeakReference);
298 break;
299
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000300 case MCSA_WeakDefinition:
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000301 // FIXME: 'as' enforces that this is defined and global. The manual claims
302 // it has to be in a coalesced section, but this isn't enforced.
303 SD.setFlags(SD.getFlags() | SF_WeakDefinition);
304 break;
Kevin Enderbyf59cac52010-07-08 17:22:42 +0000305
306 case MCSA_WeakDefAutoPrivate:
307 SD.setFlags(SD.getFlags() | SF_WeakDefinition | SF_WeakReference);
308 break;
Daniel Dunbar3edd9bb2009-08-22 11:41:10 +0000309 }
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000310}
311
312void MCMachOStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000313 // Encode the 'desc' value into the lowest implementation defined bits.
Daniel Dunbar2ac0c452010-05-26 20:37:00 +0000314 assert(DescValue == (DescValue & SF_DescFlagsMask) &&
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000315 "Invalid .desc value!");
Daniel Dunbar8dc68ab2010-06-16 20:04:22 +0000316 getAssembler().getOrCreateSymbolData(*Symbol).setFlags(
317 DescValue & SF_DescFlagsMask);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000318}
319
Chris Lattner9eb158d2010-01-23 07:47:02 +0000320void MCMachOStreamer::EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000321 unsigned ByteAlignment) {
Daniel Dunbar8f4d1462009-08-28 07:08:35 +0000322 // FIXME: Darwin 'as' does appear to allow redef of a .comm by itself.
323 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
324
Daniel Dunbar8dc68ab2010-06-16 20:04:22 +0000325 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
Daniel Dunbar8f4d1462009-08-28 07:08:35 +0000326 SD.setExternal(true);
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000327 SD.setCommon(Size, ByteAlignment);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000328}
329
Benjamin Kramer39646d92012-09-07 17:25:13 +0000330void MCMachOStreamer::EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size,
331 unsigned ByteAlignment) {
332 // '.lcomm' is equivalent to '.zerofill'.
333 return EmitZerofill(getContext().getMachOSection("__DATA", "__bss",
334 MCSectionMachO::S_ZEROFILL,
335 0, SectionKind::getBSS()),
336 Symbol, Size, ByteAlignment);
337}
338
Daniel Dunbar8751b942009-08-28 05:48:22 +0000339void MCMachOStreamer::EmitZerofill(const MCSection *Section, MCSymbol *Symbol,
Evan Chengc90a1fc2012-06-22 20:14:46 +0000340 uint64_t Size, unsigned ByteAlignment) {
Daniel Dunbar8dc68ab2010-06-16 20:04:22 +0000341 MCSectionData &SectData = getAssembler().getOrCreateSectionData(*Section);
Daniel Dunbard5a8e982009-08-28 05:49:21 +0000342
343 // The symbol may not be present, which only creates the section.
344 if (!Symbol)
345 return;
346
347 // FIXME: Assert that this section has the zerofill type.
348
349 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
350
Daniel Dunbar8dc68ab2010-06-16 20:04:22 +0000351 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
Daniel Dunbard5a8e982009-08-28 05:49:21 +0000352
Daniel Dunbare73d49e2010-05-12 22:51:27 +0000353 // Emit an align fragment if necessary.
354 if (ByteAlignment != 1)
Daniel Dunbar1c154132010-05-12 22:56:23 +0000355 new MCAlignFragment(ByteAlignment, 0, 0, ByteAlignment, &SectData);
Daniel Dunbare73d49e2010-05-12 22:51:27 +0000356
Daniel Dunbar4e544872010-05-12 22:51:38 +0000357 MCFragment *F = new MCFillFragment(0, 0, Size, &SectData);
Daniel Dunbard5a8e982009-08-28 05:49:21 +0000358 SD.setFragment(F);
359
360 Symbol->setSection(*Section);
361
362 // Update the maximum alignment on the zero fill section if necessary.
363 if (ByteAlignment > SectData.getAlignment())
364 SectData.setAlignment(ByteAlignment);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000365}
366
Eric Christopher070c1ab2010-05-21 23:03:53 +0000367// This should always be called with the thread local bss section. Like the
368// .zerofill directive this doesn't actually switch sections on us.
Eric Christopher011c3f12010-05-18 21:26:41 +0000369void MCMachOStreamer::EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol,
370 uint64_t Size, unsigned ByteAlignment) {
371 EmitZerofill(Section, Symbol, Size, ByteAlignment);
372 return;
Eric Christopher482eba02010-05-14 01:50:28 +0000373}
374
Chris Lattneraaec2052010-01-19 19:46:13 +0000375void MCMachOStreamer::EmitBytes(StringRef Data, unsigned AddrSpace) {
Michael J. Spencer8067adc2010-07-19 06:13:10 +0000376 // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into
377 // MCObjectStreamer.
Daniel Dunbarf70f4772010-03-22 20:35:46 +0000378 getOrCreateDataFragment()->getContents().append(Data.begin(), Data.end());
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000379}
380
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000381void MCMachOStreamer::EmitValueToAlignment(unsigned ByteAlignment,
382 int64_t Value, unsigned ValueSize,
383 unsigned MaxBytesToEmit) {
Michael J. Spencer8067adc2010-07-19 06:13:10 +0000384 // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into
385 // MCObjectStreamer.
Daniel Dunbard6f761e2009-08-21 23:07:38 +0000386 if (MaxBytesToEmit == 0)
387 MaxBytesToEmit = ByteAlignment;
Daniel Dunbara86de102010-06-16 20:04:32 +0000388 new MCAlignFragment(ByteAlignment, Value, ValueSize, MaxBytesToEmit,
389 getCurrentSectionData());
Kevin Enderby6e720482010-02-23 18:26:34 +0000390
391 // Update the maximum alignment on the current section if necessary.
Daniel Dunbar83b46712010-06-16 20:04:25 +0000392 if (ByteAlignment > getCurrentSectionData()->getAlignment())
393 getCurrentSectionData()->setAlignment(ByteAlignment);
Kevin Enderby6e720482010-02-23 18:26:34 +0000394}
395
396void MCMachOStreamer::EmitCodeAlignment(unsigned ByteAlignment,
397 unsigned MaxBytesToEmit) {
Michael J. Spencer8067adc2010-07-19 06:13:10 +0000398 // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into
399 // MCObjectStreamer.
Kevin Enderby6e720482010-02-23 18:26:34 +0000400 if (MaxBytesToEmit == 0)
401 MaxBytesToEmit = ByteAlignment;
Daniel Dunbar1c154132010-05-12 22:56:23 +0000402 MCAlignFragment *F = new MCAlignFragment(ByteAlignment, 0, 1, MaxBytesToEmit,
Daniel Dunbar83b46712010-06-16 20:04:25 +0000403 getCurrentSectionData());
Daniel Dunbar1c154132010-05-12 22:56:23 +0000404 F->setEmitNops(true);
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000405
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +0000406 // Update the maximum alignment on the current section if necessary.
Daniel Dunbar83b46712010-06-16 20:04:25 +0000407 if (ByteAlignment > getCurrentSectionData()->getAlignment())
408 getCurrentSectionData()->setAlignment(ByteAlignment);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000409}
410
Daniel Dunbar2ac0c452010-05-26 20:37:00 +0000411void MCMachOStreamer::EmitInstToData(const MCInst &Inst) {
412 MCDataFragment *DF = getOrCreateDataFragment();
413
414 SmallVector<MCFixup, 4> Fixups;
415 SmallString<256> Code;
416 raw_svector_ostream VecOS(Code);
Daniel Dunbar8dc68ab2010-06-16 20:04:22 +0000417 getAssembler().getEmitter().EncodeInstruction(Inst, VecOS, Fixups);
Daniel Dunbar2ac0c452010-05-26 20:37:00 +0000418 VecOS.flush();
419
420 // Add the fixups and data.
421 for (unsigned i = 0, e = Fixups.size(); i != e; ++i) {
422 Fixups[i].setOffset(Fixups[i].getOffset() + DF->getContents().size());
423 DF->addFixup(Fixups[i]);
424 }
425 DF->getContents().append(Code.begin(), Code.end());
426}
427
Rafael Espindola99b42372012-01-07 03:13:18 +0000428void MCMachOStreamer::FinishImpl() {
Rafael Espindolac25dad82011-05-10 03:14:15 +0000429 EmitFrames(true);
Rafael Espindola450a5a12011-05-01 15:44:13 +0000430
Daniel Dunbara86de102010-06-16 20:04:32 +0000431 // We have to set the fragment atom associations so we can relax properly for
432 // Mach-O.
433
434 // First, scan the symbol table to build a lookup table from fragments to
435 // defining symbols.
436 DenseMap<const MCFragment*, MCSymbolData*> DefiningSymbolMap;
437 for (MCAssembler::symbol_iterator it = getAssembler().symbol_begin(),
438 ie = getAssembler().symbol_end(); it != ie; ++it) {
439 if (getAssembler().isSymbolLinkerVisible(it->getSymbol()) &&
440 it->getFragment()) {
441 // An atom defining symbol should never be internal to a fragment.
442 assert(it->getOffset() == 0 && "Invalid offset in atom defining symbol!");
443 DefiningSymbolMap[it->getFragment()] = it;
444 }
445 }
446
447 // Set the fragment atom associations by tracking the last seen atom defining
448 // symbol.
449 for (MCAssembler::iterator it = getAssembler().begin(),
450 ie = getAssembler().end(); it != ie; ++it) {
451 MCSymbolData *CurrentAtom = 0;
452 for (MCSectionData::iterator it2 = it->begin(),
453 ie2 = it->end(); it2 != ie2; ++it2) {
454 if (MCSymbolData *SD = DefiningSymbolMap.lookup(it2))
455 CurrentAtom = SD;
456 it2->setAtom(CurrentAtom);
457 }
458 }
459
Rafael Espindola99b42372012-01-07 03:13:18 +0000460 this->MCObjectStreamer::FinishImpl();
Daniel Dunbara86de102010-06-16 20:04:32 +0000461}
462
Evan Cheng78c10ee2011-07-25 23:24:55 +0000463MCStreamer *llvm::createMachOStreamer(MCContext &Context, MCAsmBackend &MAB,
Daniel Dunbarac2884a2010-03-25 22:49:09 +0000464 raw_ostream &OS, MCCodeEmitter *CE,
465 bool RelaxAll) {
Evan Cheng78c10ee2011-07-25 23:24:55 +0000466 MCMachOStreamer *S = new MCMachOStreamer(Context, MAB, OS, CE);
Daniel Dunbarac2884a2010-03-25 22:49:09 +0000467 if (RelaxAll)
468 S->getAssembler().setRelaxAll(true);
469 return S;
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000470}