blob: 970aa8baf43c094c390be5b6add172ed509d669e [file] [log] [blame]
Daniel Dunbar3016db32009-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 Dunbar3016db32009-08-21 09:11:24 +000011#include "llvm/MC/MCAssembler.h"
12#include "llvm/MC/MCContext.h"
Daniel Dunbarc7c5f9f2009-08-27 08:17:51 +000013#include "llvm/MC/MCCodeEmitter.h"
Daniel Dunbar73da11e2009-08-31 08:08:38 +000014#include "llvm/MC/MCExpr.h"
Daniel Dunbarc7c5f9f2009-08-27 08:17:51 +000015#include "llvm/MC/MCInst.h"
Daniel Dunbar8a3c9d92010-06-16 20:04:22 +000016#include "llvm/MC/MCObjectStreamer.h"
Daniel Dunbar3016db32009-08-21 09:11:24 +000017#include "llvm/MC/MCSection.h"
18#include "llvm/MC/MCSymbol.h"
Kevin Enderby51bed9c2010-05-07 21:44:23 +000019#include "llvm/MC/MCMachOSymbolFlags.h"
Kevin Enderby7221b762010-08-09 22:52:14 +000020#include "llvm/MC/MCSectionMachO.h"
21#include "llvm/MC/MCDwarf.h"
Evan Cheng5928e692011-07-25 23:24:55 +000022#include "llvm/MC/MCAsmBackend.h"
Kevin Enderbye46564a2010-09-30 16:52:03 +000023#include "llvm/Support/Dwarf.h"
Daniel Dunbar3016db32009-08-21 09:11:24 +000024#include "llvm/Support/ErrorHandling.h"
Daniel Dunbarc7c5f9f2009-08-27 08:17:51 +000025#include "llvm/Support/raw_ostream.h"
Daniel Dunbarde04b3f2010-03-23 05:09:03 +000026
Daniel Dunbar3016db32009-08-21 09:11:24 +000027using namespace llvm;
28
29namespace {
30
Daniel Dunbar8a3c9d92010-06-16 20:04:22 +000031class MCMachOStreamer : public MCObjectStreamer {
Daniel Dunbar04a11582009-08-24 08:40:12 +000032private:
Rafael Espindola72b54882010-11-01 16:27:31 +000033 virtual void EmitInstToData(const MCInst &Inst);
Daniel Dunbar9d40ef12010-05-26 20:37:00 +000034
Jim Grosbach4b63d2a2012-05-18 19:12:01 +000035 void EmitDataRegion(DataRegionData::KindTy Kind);
36 void EmitDataRegionEnd();
Daniel Dunbar3016db32009-08-21 09:11:24 +000037public:
Evan Cheng5928e692011-07-25 23:24:55 +000038 MCMachOStreamer(MCContext &Context, MCAsmBackend &MAB,
Daniel Dunbar8a3c9d92010-06-16 20:04:22 +000039 raw_ostream &OS, MCCodeEmitter *Emitter)
Evan Cheng5928e692011-07-25 23:24:55 +000040 : MCObjectStreamer(Context, MAB, OS, Emitter) {}
Daniel Dunbard821f4a2010-03-25 22:49:09 +000041
Daniel Dunbar3016db32009-08-21 09:11:24 +000042 /// @name MCStreamer Interface
43 /// @{
44
Rafael Espindolaf667d922010-09-15 21:48:40 +000045 virtual void InitSections();
Daniel Dunbar3016db32009-08-21 09:11:24 +000046 virtual void EmitLabel(MCSymbol *Symbol);
Rafael Espindola349c3292011-04-28 12:50:37 +000047 virtual void EmitEHSymAttributes(const MCSymbol *Symbol,
48 MCSymbol *EHSymbol);
Chris Lattner685508c2010-01-23 06:39:22 +000049 virtual void EmitAssemblerFlag(MCAssemblerFlag Flag);
Jim Grosbach4b63d2a2012-05-18 19:12:01 +000050 virtual void EmitDataRegion(MCDataRegionType Kind);
Jim Grosbach5a2c68d2010-11-05 22:08:08 +000051 virtual void EmitThumbFunc(MCSymbol *Func);
Daniel Dunbar897ffad2009-08-31 08:09:28 +000052 virtual void EmitAssignment(MCSymbol *Symbol, const MCExpr *Value);
Chris Lattner685508c2010-01-23 06:39:22 +000053 virtual void EmitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute);
Daniel Dunbar3016db32009-08-21 09:11:24 +000054 virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue);
Chris Lattnerb1301f72010-01-23 07:47:02 +000055 virtual void EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
Daniel Dunbar6a715dc2009-08-30 06:17:16 +000056 unsigned ByteAlignment);
Chris Lattner72afa952010-05-08 19:54:22 +000057 virtual void BeginCOFFSymbolDef(const MCSymbol *Symbol) {
Craig Toppera2886c22012-02-07 05:05:23 +000058 llvm_unreachable("macho doesn't support this directive");
Chris Lattner72afa952010-05-08 19:54:22 +000059 }
60 virtual void EmitCOFFSymbolStorageClass(int StorageClass) {
Craig Toppera2886c22012-02-07 05:05:23 +000061 llvm_unreachable("macho doesn't support this directive");
Chris Lattner72afa952010-05-08 19:54:22 +000062 }
63 virtual void EmitCOFFSymbolType(int Type) {
Craig Toppera2886c22012-02-07 05:05:23 +000064 llvm_unreachable("macho doesn't support this directive");
Chris Lattner72afa952010-05-08 19:54:22 +000065 }
66 virtual void EndCOFFSymbolDef() {
Craig Toppera2886c22012-02-07 05:05:23 +000067 llvm_unreachable("macho doesn't support this directive");
Chris Lattner72afa952010-05-08 19:54:22 +000068 }
Chris Lattner91dac6d2010-01-25 07:52:13 +000069 virtual void EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) {
Craig Toppera2886c22012-02-07 05:05:23 +000070 llvm_unreachable("macho doesn't support this directive");
Chris Lattner91dac6d2010-01-25 07:52:13 +000071 }
Benjamin Kramer63970512011-09-01 23:04:27 +000072 virtual void EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size,
73 unsigned ByteAlignment) {
Craig Toppera2886c22012-02-07 05:05:23 +000074 llvm_unreachable("macho doesn't support this directive");
Chris Lattnerb1301f72010-01-23 07:47:02 +000075 }
Daniel Dunbarcf72e1c2009-08-28 05:48:22 +000076 virtual void EmitZerofill(const MCSection *Section, MCSymbol *Symbol = 0,
Daniel Dunbar6a715dc2009-08-30 06:17:16 +000077 unsigned Size = 0, unsigned ByteAlignment = 0);
Eric Christopherfeedc902010-05-18 21:26:41 +000078 virtual void EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol,
79 uint64_t Size, unsigned ByteAlignment = 0);
Chris Lattnerc35681b2010-01-19 19:46:13 +000080 virtual void EmitBytes(StringRef Data, unsigned AddrSpace);
Daniel Dunbar3016db32009-08-21 09:11:24 +000081 virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0,
82 unsigned ValueSize = 1,
83 unsigned MaxBytesToEmit = 0);
Kevin Enderbye83d74f2010-02-23 18:26:34 +000084 virtual void EmitCodeAlignment(unsigned ByteAlignment,
85 unsigned MaxBytesToEmit = 0);
Daniel Dunbar9d40ef12010-05-26 20:37:00 +000086
Chris Lattner601ef332010-01-25 18:58:59 +000087 virtual void EmitFileDirective(StringRef Filename) {
Daniel Dunbar6b4391a2010-07-19 20:44:20 +000088 // FIXME: Just ignore the .file; it isn't important enough to fail the
89 // entire assembly.
90
91 //report_fatal_error("unsupported directive: '.file'");
Chris Lattner601ef332010-01-25 18:58:59 +000092 }
Daniel Dunbar9d40ef12010-05-26 20:37:00 +000093
Rafael Espindola07082092012-01-07 03:13:18 +000094 virtual void FinishImpl();
Daniel Dunbarede8e6d2010-06-16 20:04:32 +000095
Daniel Dunbar3016db32009-08-21 09:11:24 +000096 /// @}
97};
98
99} // end anonymous namespace.
100
Rafael Espindolaf667d922010-09-15 21:48:40 +0000101void MCMachOStreamer::InitSections() {
102 SwitchSection(getContext().getMachOSection("__TEXT", "__text",
103 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
104 0, SectionKind::getText()));
105
106}
107
Rafael Espindola349c3292011-04-28 12:50:37 +0000108void MCMachOStreamer::EmitEHSymAttributes(const MCSymbol *Symbol,
109 MCSymbol *EHSymbol) {
110 MCSymbolData &SD =
111 getAssembler().getOrCreateSymbolData(*Symbol);
112 if (SD.isExternal())
113 EmitSymbolAttribute(EHSymbol, MCSA_Global);
114 if (SD.getFlags() & SF_WeakDefinition)
115 EmitSymbolAttribute(EHSymbol, MCSA_WeakDefinition);
Rafael Espindola0b474c22011-04-30 16:22:46 +0000116 if (SD.isPrivateExtern())
117 EmitSymbolAttribute(EHSymbol, MCSA_PrivateExtern);
Rafael Espindola349c3292011-04-28 12:50:37 +0000118}
119
Daniel Dunbar3016db32009-08-21 09:11:24 +0000120void MCMachOStreamer::EmitLabel(MCSymbol *Symbol) {
Rafael Espindola16795802010-11-28 16:22:59 +0000121 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
Rafael Espindola16795802010-11-28 16:22:59 +0000122
Rafael Espindolae5b74152010-11-28 17:18:55 +0000123 // isSymbolLinkerVisible uses the section.
Rafael Espindola58ac6e12011-02-16 01:08:29 +0000124 Symbol->setSection(*getCurrentSection());
Daniel Dunbarede8e6d2010-06-16 20:04:32 +0000125 // We have to create a new fragment if this is an atom defining symbol,
126 // fragments cannot span atoms.
Rafael Espindolae5b74152010-11-28 17:18:55 +0000127 if (getAssembler().isSymbolLinkerVisible(*Symbol))
Daniel Dunbarede8e6d2010-06-16 20:04:32 +0000128 new MCDataFragment(getCurrentSectionData());
Daniel Dunbaraadb2ca2010-05-10 22:45:09 +0000129
Rafael Espindolae5b74152010-11-28 17:18:55 +0000130 MCObjectStreamer::EmitLabel(Symbol);
Daniel Dunbar04a11582009-08-24 08:40:12 +0000131
Rafael Espindolae5b74152010-11-28 17:18:55 +0000132 MCSymbolData &SD = getAssembler().getSymbolData(*Symbol);
Daniel Dunbar0211a962010-05-17 20:12:31 +0000133 // This causes the reference type flag to be cleared. Darwin 'as' was "trying"
134 // to clear the weak reference and weak definition bits too, but the
135 // implementation was buggy. For now we just try to match 'as', for
136 // diffability.
137 //
138 // FIXME: Cleanup this code, these bits should be emitted based on semantic
139 // properties, not on the order of definition, etc.
140 SD.setFlags(SD.getFlags() & ~SF_ReferenceTypeMask);
Daniel Dunbar3016db32009-08-21 09:11:24 +0000141}
142
Jim Grosbach4b63d2a2012-05-18 19:12:01 +0000143void MCMachOStreamer::EmitDataRegion(DataRegionData::KindTy Kind) {
144 // 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() {
154 std::vector<DataRegionData> &Regions = getAssembler().getDataRegions();
155 assert(Regions.size() && "Mismatched .end_data_region!");
156 DataRegionData &Data = Regions.back();
157 assert(Data.End == NULL && "Mismatched .end_data_region!");
158 // Create a temporary label to mark the end of the data region.
159 Data.End = getContext().CreateTempSymbol();
160 EmitLabel(Data.End);
161}
162
Chris Lattner685508c2010-01-23 06:39:22 +0000163void MCMachOStreamer::EmitAssemblerFlag(MCAssemblerFlag Flag) {
Jim Grosbach87055ed2010-12-08 01:16:55 +0000164 // Let the target do whatever target specific stuff it needs to do.
Jim Grosbachaba3de92012-01-18 18:52:16 +0000165 getAssembler().getBackend().handleAssemblerFlag(Flag);
Jim Grosbach87055ed2010-12-08 01:16:55 +0000166 // Do any generic stuff we need to do.
Daniel Dunbare2697732009-08-26 21:22:22 +0000167 switch (Flag) {
Jim Grosbach5a2c68d2010-11-05 22:08:08 +0000168 case MCAF_SyntaxUnified: return; // no-op here.
Evan Cheng481ebb02011-07-27 00:38:12 +0000169 case MCAF_Code16: return; // Change parsing mode; no-op here.
170 case MCAF_Code32: return; // Change parsing mode; no-op here.
171 case MCAF_Code64: return; // Change parsing mode; no-op here.
Chris Lattner685508c2010-01-23 06:39:22 +0000172 case MCAF_SubsectionsViaSymbols:
Daniel Dunbar8a3c9d92010-06-16 20:04:22 +0000173 getAssembler().setSubsectionsViaSymbols(true);
Daniel Dunbar2a2459a2009-08-28 07:08:47 +0000174 return;
Daniel Dunbare2697732009-08-26 21:22:22 +0000175 }
Daniel Dunbar3016db32009-08-21 09:11:24 +0000176}
177
Jim Grosbach4b63d2a2012-05-18 19:12:01 +0000178void MCMachOStreamer::EmitDataRegion(MCDataRegionType Kind) {
179 switch (Kind) {
180 case MCDR_DataRegion:
181 EmitDataRegion(DataRegionData::Data);
182 return;
183 case MCDR_DataRegionJT8:
184 EmitDataRegion(DataRegionData::JumpTable8);
185 return;
186 case MCDR_DataRegionJT16:
187 EmitDataRegion(DataRegionData::JumpTable16);
188 return;
189 case MCDR_DataRegionJT32:
190 EmitDataRegion(DataRegionData::JumpTable32);
191 return;
192 case MCDR_DataRegionEnd:
193 EmitDataRegionEnd();
194 return;
195 }
196}
197
Daniel Dunbarab14a6f2010-12-29 14:14:06 +0000198void MCMachOStreamer::EmitThumbFunc(MCSymbol *Symbol) {
Jim Grosbach41955ff2010-12-14 18:46:57 +0000199 // Remember that the function is a thumb function. Fixup and relocation
200 // values will need adjusted.
Daniel Dunbarab14a6f2010-12-29 14:14:06 +0000201 getAssembler().setIsThumbFunc(Symbol);
202
203 // Mark the thumb bit on the symbol.
204 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
205 SD.setFlags(SD.getFlags() | SF_ThumbFunc);
Jim Grosbach5a2c68d2010-11-05 22:08:08 +0000206}
207
Daniel Dunbar897ffad2009-08-31 08:09:28 +0000208void MCMachOStreamer::EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) {
Michael J. Spencere2da0a42010-07-19 06:13:10 +0000209 // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into
210 // MCObjectStreamer.
Daniel Dunbar5d913472009-10-16 01:58:23 +0000211 // FIXME: Lift context changes into super class.
Daniel Dunbar8a3c9d92010-06-16 20:04:22 +0000212 getAssembler().getOrCreateSymbolData(*Symbol);
Daniel Dunbar7a989da2010-05-05 17:41:00 +0000213 Symbol->setVariableValue(AddValueSymbols(Value));
Daniel Dunbar3016db32009-08-21 09:11:24 +0000214}
215
216void MCMachOStreamer::EmitSymbolAttribute(MCSymbol *Symbol,
Chris Lattner685508c2010-01-23 06:39:22 +0000217 MCSymbolAttr Attribute) {
Daniel Dunbar582d6102009-08-24 11:56:58 +0000218 // Indirect symbols are handled differently, to match how 'as' handles
219 // them. This makes writing matching .o files easier.
Chris Lattner685508c2010-01-23 06:39:22 +0000220 if (Attribute == MCSA_IndirectSymbol) {
Daniel Dunbarc2c0bf92009-08-26 00:18:21 +0000221 // Note that we intentionally cannot use the symbol data here; this is
222 // important for matching the string table that 'as' generates.
Daniel Dunbar582d6102009-08-24 11:56:58 +0000223 IndirectSymbolData ISD;
224 ISD.Symbol = Symbol;
Daniel Dunbarb2347fe2010-06-16 20:04:25 +0000225 ISD.SectionData = getCurrentSectionData();
Daniel Dunbar8a3c9d92010-06-16 20:04:22 +0000226 getAssembler().getIndirectSymbols().push_back(ISD);
Daniel Dunbar582d6102009-08-24 11:56:58 +0000227 return;
228 }
229
Daniel Dunbar04a11582009-08-24 08:40:12 +0000230 // Adding a symbol attribute always introduces the symbol, note that an
Daniel Dunbar6eab7212010-03-10 20:58:29 +0000231 // important side effect of calling getOrCreateSymbolData here is to register
232 // the symbol with the assembler.
Daniel Dunbar8a3c9d92010-06-16 20:04:22 +0000233 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
Daniel Dunbar04a11582009-08-24 08:40:12 +0000234
235 // The implementation of symbol attributes is designed to match 'as', but it
236 // leaves much to desired. It doesn't really make sense to arbitrarily add and
237 // remove flags, but 'as' allows this (in particular, see .desc).
238 //
239 // In the future it might be worth trying to make these operations more well
240 // defined.
Daniel Dunbard1859472009-08-22 11:41:10 +0000241 switch (Attribute) {
Chris Lattner685508c2010-01-23 06:39:22 +0000242 case MCSA_Invalid:
Chris Lattnerbc8f6382010-01-25 18:30:45 +0000243 case MCSA_ELF_TypeFunction:
244 case MCSA_ELF_TypeIndFunction:
245 case MCSA_ELF_TypeObject:
246 case MCSA_ELF_TypeTLS:
247 case MCSA_ELF_TypeCommon:
248 case MCSA_ELF_TypeNoType:
Rafael Espindola4bcf94c2010-11-13 04:51:02 +0000249 case MCSA_ELF_TypeGnuUniqueObject:
Chris Lattner685508c2010-01-23 06:39:22 +0000250 case MCSA_Hidden:
Chandler Carruthb2561022011-07-25 21:21:08 +0000251 case MCSA_IndirectSymbol:
Chris Lattner685508c2010-01-23 06:39:22 +0000252 case MCSA_Internal:
253 case MCSA_Protected:
254 case MCSA_Weak:
255 case MCSA_Local:
Craig Toppera2886c22012-02-07 05:05:23 +0000256 llvm_unreachable("Invalid symbol attribute for Mach-O!");
Daniel Dunbard1859472009-08-22 11:41:10 +0000257
Chris Lattner685508c2010-01-23 06:39:22 +0000258 case MCSA_Global:
Daniel Dunbar2701eee2009-08-28 07:08:35 +0000259 SD.setExternal(true);
Daniel Dunbar0211a962010-05-17 20:12:31 +0000260 // This effectively clears the undefined lazy bit, in Darwin 'as', although
261 // it isn't very consistent because it implements this as part of symbol
262 // lookup.
263 //
264 // FIXME: Cleanup this code, these bits should be emitted based on semantic
265 // properties, not on the order of definition, etc.
266 SD.setFlags(SD.getFlags() & ~SF_ReferenceTypeUndefinedLazy);
Daniel Dunbard1859472009-08-22 11:41:10 +0000267 break;
Daniel Dunbar04a11582009-08-24 08:40:12 +0000268
Chris Lattner685508c2010-01-23 06:39:22 +0000269 case MCSA_LazyReference:
Daniel Dunbar04a11582009-08-24 08:40:12 +0000270 // FIXME: This requires -dynamic.
271 SD.setFlags(SD.getFlags() | SF_NoDeadStrip);
272 if (Symbol->isUndefined())
273 SD.setFlags(SD.getFlags() | SF_ReferenceTypeUndefinedLazy);
274 break;
275
Daniel Dunbar04a11582009-08-24 08:40:12 +0000276 // Since .reference sets the no dead strip bit, it is equivalent to
277 // .no_dead_strip in practice.
Chris Lattner685508c2010-01-23 06:39:22 +0000278 case MCSA_Reference:
279 case MCSA_NoDeadStrip:
Daniel Dunbar04a11582009-08-24 08:40:12 +0000280 SD.setFlags(SD.getFlags() | SF_NoDeadStrip);
281 break;
282
Kevin Enderby8be14412010-11-19 18:39:33 +0000283 case MCSA_SymbolResolver:
284 SD.setFlags(SD.getFlags() | SF_SymbolResolver);
285 break;
286
Chris Lattner685508c2010-01-23 06:39:22 +0000287 case MCSA_PrivateExtern:
Daniel Dunbar04a11582009-08-24 08:40:12 +0000288 SD.setExternal(true);
289 SD.setPrivateExtern(true);
290 break;
291
Chris Lattner685508c2010-01-23 06:39:22 +0000292 case MCSA_WeakReference:
Daniel Dunbar04a11582009-08-24 08:40:12 +0000293 // FIXME: This requires -dynamic.
294 if (Symbol->isUndefined())
295 SD.setFlags(SD.getFlags() | SF_WeakReference);
296 break;
297
Chris Lattner685508c2010-01-23 06:39:22 +0000298 case MCSA_WeakDefinition:
Daniel Dunbar04a11582009-08-24 08:40:12 +0000299 // FIXME: 'as' enforces that this is defined and global. The manual claims
300 // it has to be in a coalesced section, but this isn't enforced.
301 SD.setFlags(SD.getFlags() | SF_WeakDefinition);
302 break;
Kevin Enderby082d0fd2010-07-08 17:22:42 +0000303
304 case MCSA_WeakDefAutoPrivate:
305 SD.setFlags(SD.getFlags() | SF_WeakDefinition | SF_WeakReference);
306 break;
Daniel Dunbard1859472009-08-22 11:41:10 +0000307 }
Daniel Dunbar3016db32009-08-21 09:11:24 +0000308}
309
310void MCMachOStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
Daniel Dunbar04a11582009-08-24 08:40:12 +0000311 // Encode the 'desc' value into the lowest implementation defined bits.
Daniel Dunbar9d40ef12010-05-26 20:37:00 +0000312 assert(DescValue == (DescValue & SF_DescFlagsMask) &&
Daniel Dunbar04a11582009-08-24 08:40:12 +0000313 "Invalid .desc value!");
Daniel Dunbar8a3c9d92010-06-16 20:04:22 +0000314 getAssembler().getOrCreateSymbolData(*Symbol).setFlags(
315 DescValue & SF_DescFlagsMask);
Daniel Dunbar3016db32009-08-21 09:11:24 +0000316}
317
Chris Lattnerb1301f72010-01-23 07:47:02 +0000318void MCMachOStreamer::EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
Daniel Dunbar6a715dc2009-08-30 06:17:16 +0000319 unsigned ByteAlignment) {
Daniel Dunbar2701eee2009-08-28 07:08:35 +0000320 // FIXME: Darwin 'as' does appear to allow redef of a .comm by itself.
321 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
322
Daniel Dunbar8a3c9d92010-06-16 20:04:22 +0000323 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
Daniel Dunbar2701eee2009-08-28 07:08:35 +0000324 SD.setExternal(true);
Daniel Dunbar6a715dc2009-08-30 06:17:16 +0000325 SD.setCommon(Size, ByteAlignment);
Daniel Dunbar3016db32009-08-21 09:11:24 +0000326}
327
Daniel Dunbarcf72e1c2009-08-28 05:48:22 +0000328void MCMachOStreamer::EmitZerofill(const MCSection *Section, MCSymbol *Symbol,
Daniel Dunbar6a715dc2009-08-30 06:17:16 +0000329 unsigned Size, unsigned ByteAlignment) {
Daniel Dunbar8a3c9d92010-06-16 20:04:22 +0000330 MCSectionData &SectData = getAssembler().getOrCreateSectionData(*Section);
Daniel Dunbar42a39d02009-08-28 05:49:21 +0000331
332 // The symbol may not be present, which only creates the section.
333 if (!Symbol)
334 return;
335
336 // FIXME: Assert that this section has the zerofill type.
337
338 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
339
Daniel Dunbar8a3c9d92010-06-16 20:04:22 +0000340 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
Daniel Dunbar42a39d02009-08-28 05:49:21 +0000341
Daniel Dunbar51402b72010-05-12 22:51:27 +0000342 // Emit an align fragment if necessary.
343 if (ByteAlignment != 1)
Daniel Dunbarb76df222010-05-12 22:56:23 +0000344 new MCAlignFragment(ByteAlignment, 0, 0, ByteAlignment, &SectData);
Daniel Dunbar51402b72010-05-12 22:51:27 +0000345
Daniel Dunbar4405ffc2010-05-12 22:51:38 +0000346 MCFragment *F = new MCFillFragment(0, 0, Size, &SectData);
Daniel Dunbar42a39d02009-08-28 05:49:21 +0000347 SD.setFragment(F);
348
349 Symbol->setSection(*Section);
350
351 // Update the maximum alignment on the zero fill section if necessary.
352 if (ByteAlignment > SectData.getAlignment())
353 SectData.setAlignment(ByteAlignment);
Daniel Dunbar3016db32009-08-21 09:11:24 +0000354}
355
Eric Christopher09d47032010-05-21 23:03:53 +0000356// This should always be called with the thread local bss section. Like the
357// .zerofill directive this doesn't actually switch sections on us.
Eric Christopherfeedc902010-05-18 21:26:41 +0000358void MCMachOStreamer::EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol,
359 uint64_t Size, unsigned ByteAlignment) {
360 EmitZerofill(Section, Symbol, Size, ByteAlignment);
361 return;
Eric Christopher9fb6bb02010-05-14 01:50:28 +0000362}
363
Chris Lattnerc35681b2010-01-19 19:46:13 +0000364void MCMachOStreamer::EmitBytes(StringRef Data, unsigned AddrSpace) {
Michael J. Spencere2da0a42010-07-19 06:13:10 +0000365 // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into
366 // MCObjectStreamer.
Daniel Dunbarf1f18272010-03-22 20:35:46 +0000367 getOrCreateDataFragment()->getContents().append(Data.begin(), Data.end());
Daniel Dunbar3016db32009-08-21 09:11:24 +0000368}
369
Daniel Dunbar3016db32009-08-21 09:11:24 +0000370void MCMachOStreamer::EmitValueToAlignment(unsigned ByteAlignment,
371 int64_t Value, unsigned ValueSize,
372 unsigned MaxBytesToEmit) {
Michael J. Spencere2da0a42010-07-19 06:13:10 +0000373 // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into
374 // MCObjectStreamer.
Daniel Dunbarb1068e42009-08-21 23:07:38 +0000375 if (MaxBytesToEmit == 0)
376 MaxBytesToEmit = ByteAlignment;
Daniel Dunbarede8e6d2010-06-16 20:04:32 +0000377 new MCAlignFragment(ByteAlignment, Value, ValueSize, MaxBytesToEmit,
378 getCurrentSectionData());
Kevin Enderbye83d74f2010-02-23 18:26:34 +0000379
380 // Update the maximum alignment on the current section if necessary.
Daniel Dunbarb2347fe2010-06-16 20:04:25 +0000381 if (ByteAlignment > getCurrentSectionData()->getAlignment())
382 getCurrentSectionData()->setAlignment(ByteAlignment);
Kevin Enderbye83d74f2010-02-23 18:26:34 +0000383}
384
385void MCMachOStreamer::EmitCodeAlignment(unsigned ByteAlignment,
386 unsigned MaxBytesToEmit) {
Michael J. Spencere2da0a42010-07-19 06:13:10 +0000387 // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into
388 // MCObjectStreamer.
Kevin Enderbye83d74f2010-02-23 18:26:34 +0000389 if (MaxBytesToEmit == 0)
390 MaxBytesToEmit = ByteAlignment;
Daniel Dunbarb76df222010-05-12 22:56:23 +0000391 MCAlignFragment *F = new MCAlignFragment(ByteAlignment, 0, 1, MaxBytesToEmit,
Daniel Dunbarb2347fe2010-06-16 20:04:25 +0000392 getCurrentSectionData());
Daniel Dunbarb76df222010-05-12 22:56:23 +0000393 F->setEmitNops(true);
Daniel Dunbar4ef7fb92009-08-21 18:29:01 +0000394
Daniel Dunbarff3eafd2009-08-22 10:13:24 +0000395 // Update the maximum alignment on the current section if necessary.
Daniel Dunbarb2347fe2010-06-16 20:04:25 +0000396 if (ByteAlignment > getCurrentSectionData()->getAlignment())
397 getCurrentSectionData()->setAlignment(ByteAlignment);
Daniel Dunbar3016db32009-08-21 09:11:24 +0000398}
399
Daniel Dunbar9d40ef12010-05-26 20:37:00 +0000400void MCMachOStreamer::EmitInstToData(const MCInst &Inst) {
401 MCDataFragment *DF = getOrCreateDataFragment();
402
403 SmallVector<MCFixup, 4> Fixups;
404 SmallString<256> Code;
405 raw_svector_ostream VecOS(Code);
Daniel Dunbar8a3c9d92010-06-16 20:04:22 +0000406 getAssembler().getEmitter().EncodeInstruction(Inst, VecOS, Fixups);
Daniel Dunbar9d40ef12010-05-26 20:37:00 +0000407 VecOS.flush();
408
409 // Add the fixups and data.
410 for (unsigned i = 0, e = Fixups.size(); i != e; ++i) {
411 Fixups[i].setOffset(Fixups[i].getOffset() + DF->getContents().size());
412 DF->addFixup(Fixups[i]);
413 }
414 DF->getContents().append(Code.begin(), Code.end());
415}
416
Rafael Espindola07082092012-01-07 03:13:18 +0000417void MCMachOStreamer::FinishImpl() {
Rafael Espindolab6089d62011-05-10 03:14:15 +0000418 EmitFrames(true);
Rafael Espindolafc822362011-05-01 15:44:13 +0000419
Daniel Dunbarede8e6d2010-06-16 20:04:32 +0000420 // We have to set the fragment atom associations so we can relax properly for
421 // Mach-O.
422
423 // First, scan the symbol table to build a lookup table from fragments to
424 // defining symbols.
425 DenseMap<const MCFragment*, MCSymbolData*> DefiningSymbolMap;
426 for (MCAssembler::symbol_iterator it = getAssembler().symbol_begin(),
427 ie = getAssembler().symbol_end(); it != ie; ++it) {
428 if (getAssembler().isSymbolLinkerVisible(it->getSymbol()) &&
429 it->getFragment()) {
430 // An atom defining symbol should never be internal to a fragment.
431 assert(it->getOffset() == 0 && "Invalid offset in atom defining symbol!");
432 DefiningSymbolMap[it->getFragment()] = it;
433 }
434 }
435
436 // Set the fragment atom associations by tracking the last seen atom defining
437 // symbol.
438 for (MCAssembler::iterator it = getAssembler().begin(),
439 ie = getAssembler().end(); it != ie; ++it) {
440 MCSymbolData *CurrentAtom = 0;
441 for (MCSectionData::iterator it2 = it->begin(),
442 ie2 = it->end(); it2 != ie2; ++it2) {
443 if (MCSymbolData *SD = DefiningSymbolMap.lookup(it2))
444 CurrentAtom = SD;
445 it2->setAtom(CurrentAtom);
446 }
447 }
448
Rafael Espindola07082092012-01-07 03:13:18 +0000449 this->MCObjectStreamer::FinishImpl();
Daniel Dunbarede8e6d2010-06-16 20:04:32 +0000450}
451
Evan Cheng5928e692011-07-25 23:24:55 +0000452MCStreamer *llvm::createMachOStreamer(MCContext &Context, MCAsmBackend &MAB,
Daniel Dunbard821f4a2010-03-25 22:49:09 +0000453 raw_ostream &OS, MCCodeEmitter *CE,
454 bool RelaxAll) {
Evan Cheng5928e692011-07-25 23:24:55 +0000455 MCMachOStreamer *S = new MCMachOStreamer(Context, MAB, OS, CE);
Daniel Dunbard821f4a2010-03-25 22:49:09 +0000456 if (RelaxAll)
457 S->getAssembler().setRelaxAll(true);
458 return S;
Daniel Dunbar3016db32009-08-21 09:11:24 +0000459}