blob: 23b89759a2c5ec8507452d4f5206d86427c9b055 [file] [log] [blame]
Bill Wendlingb1e21832013-09-04 22:35:41 +00001//===-- MCMachOStreamer.cpp - MachO Streamer ------------------------------===//
Daniel Dunbar3016db32009-08-21 09:11:24 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "llvm/MC/MCStreamer.h"
Tim Northoverc3988b42014-03-29 07:05:06 +000011#include "llvm/ADT/DenseMap.h"
12#include "llvm/ADT/SmallVector.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000013#include "llvm/MC/MCAsmBackend.h"
Daniel Dunbar3016db32009-08-21 09:11:24 +000014#include "llvm/MC/MCAssembler.h"
Daniel Dunbarc7c5f9f2009-08-27 08:17:51 +000015#include "llvm/MC/MCCodeEmitter.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000016#include "llvm/MC/MCContext.h"
17#include "llvm/MC/MCDwarf.h"
Daniel Dunbar73da11e2009-08-31 08:08:38 +000018#include "llvm/MC/MCExpr.h"
Daniel Dunbarc7c5f9f2009-08-27 08:17:51 +000019#include "llvm/MC/MCInst.h"
Tim Northover53d32512014-03-29 07:34:53 +000020#include "llvm/MC/MCLinkerOptimizationHint.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000021#include "llvm/MC/MCMachOSymbolFlags.h"
Rafael Espindolae308c0c2014-01-23 22:49:25 +000022#include "llvm/MC/MCObjectFileInfo.h"
Daniel Dunbar8a3c9d92010-06-16 20:04:22 +000023#include "llvm/MC/MCObjectStreamer.h"
Daniel Dunbar3016db32009-08-21 09:11:24 +000024#include "llvm/MC/MCSection.h"
Kevin Enderby7221b762010-08-09 22:52:14 +000025#include "llvm/MC/MCSectionMachO.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000026#include "llvm/MC/MCSymbol.h"
Kevin Enderbye46564a2010-09-30 16:52:03 +000027#include "llvm/Support/Dwarf.h"
Daniel Dunbar3016db32009-08-21 09:11:24 +000028#include "llvm/Support/ErrorHandling.h"
Daniel Dunbarc7c5f9f2009-08-27 08:17:51 +000029#include "llvm/Support/raw_ostream.h"
Daniel Dunbarde04b3f2010-03-23 05:09:03 +000030
Daniel Dunbar3016db32009-08-21 09:11:24 +000031using namespace llvm;
32
33namespace {
34
Daniel Dunbar8a3c9d92010-06-16 20:04:22 +000035class MCMachOStreamer : public MCObjectStreamer {
Daniel Dunbar04a11582009-08-24 08:40:12 +000036private:
Tim Northoverc3988b42014-03-29 07:05:06 +000037 /// LabelSections - true if each section change should emit a linker local
38 /// label for use in relocations for assembler local references. Obviates the
39 /// need for local relocations. False by default.
40 bool LabelSections;
41
42 /// HasSectionLabel - map of which sections have already had a non-local
43 /// label emitted to them. Used so we don't emit extraneous linker local
44 /// labels in the middle of the section.
45 DenseMap<const MCSection*, bool> HasSectionLabel;
46
Craig Topper34a61bc2014-03-08 07:02:02 +000047 void EmitInstToData(const MCInst &Inst, const MCSubtargetInfo &STI) override;
Daniel Dunbar9d40ef12010-05-26 20:37:00 +000048
Jim Grosbach4b63d2a2012-05-18 19:12:01 +000049 void EmitDataRegion(DataRegionData::KindTy Kind);
50 void EmitDataRegionEnd();
Tim Northoverc3988b42014-03-29 07:05:06 +000051
Daniel Dunbar3016db32009-08-21 09:11:24 +000052public:
Chandler Carruthde093ef2013-01-31 23:29:57 +000053 MCMachOStreamer(MCContext &Context, MCAsmBackend &MAB, raw_ostream &OS,
Tim Northoverc3988b42014-03-29 07:05:06 +000054 MCCodeEmitter *Emitter, bool label)
55 : MCObjectStreamer(Context, MAB, OS, Emitter),
56 LabelSections(label) {}
Daniel Dunbard821f4a2010-03-25 22:49:09 +000057
Daniel Dunbar3016db32009-08-21 09:11:24 +000058 /// @name MCStreamer Interface
59 /// @{
60
Tim Northoverc3988b42014-03-29 07:05:06 +000061 void ChangeSection(const MCSection *Sect, const MCExpr *Subsect) override;
Craig Topper34a61bc2014-03-08 07:02:02 +000062 void EmitLabel(MCSymbol *Symbol) override;
Craig Topper34a61bc2014-03-08 07:02:02 +000063 void EmitEHSymAttributes(const MCSymbol *Symbol, MCSymbol *EHSymbol) override;
64 void EmitAssemblerFlag(MCAssemblerFlag Flag) override;
65 void EmitLinkerOptions(ArrayRef<std::string> Options) override;
66 void EmitDataRegion(MCDataRegionType Kind) override;
Jim Grosbach448334a2014-03-18 22:09:05 +000067 void EmitVersionMin(MCVersionMinType Kind, unsigned Major,
68 unsigned Minor, unsigned Update) override;
Craig Topper34a61bc2014-03-08 07:02:02 +000069 void EmitThumbFunc(MCSymbol *Func) override;
70 bool EmitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute) override;
71 void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) override;
72 void EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
73 unsigned ByteAlignment) override;
74 void BeginCOFFSymbolDef(const MCSymbol *Symbol) override {
Craig Toppera2886c22012-02-07 05:05:23 +000075 llvm_unreachable("macho doesn't support this directive");
Chris Lattner72afa952010-05-08 19:54:22 +000076 }
Craig Topper34a61bc2014-03-08 07:02:02 +000077 void EmitCOFFSymbolStorageClass(int StorageClass) override {
Craig Toppera2886c22012-02-07 05:05:23 +000078 llvm_unreachable("macho doesn't support this directive");
Chris Lattner72afa952010-05-08 19:54:22 +000079 }
Craig Topper34a61bc2014-03-08 07:02:02 +000080 void EmitCOFFSymbolType(int Type) override {
Craig Toppera2886c22012-02-07 05:05:23 +000081 llvm_unreachable("macho doesn't support this directive");
Chris Lattner72afa952010-05-08 19:54:22 +000082 }
Craig Topper34a61bc2014-03-08 07:02:02 +000083 void EndCOFFSymbolDef() override {
Craig Toppera2886c22012-02-07 05:05:23 +000084 llvm_unreachable("macho doesn't support this directive");
Chris Lattner72afa952010-05-08 19:54:22 +000085 }
Craig Topper34a61bc2014-03-08 07:02:02 +000086 void EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) override {
Craig Toppera2886c22012-02-07 05:05:23 +000087 llvm_unreachable("macho doesn't support this directive");
Chris Lattner91dac6d2010-01-25 07:52:13 +000088 }
Craig Topper34a61bc2014-03-08 07:02:02 +000089 void EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size,
90 unsigned ByteAlignment) override;
Craig Topperbb694de2014-04-13 04:57:38 +000091 void EmitZerofill(const MCSection *Section, MCSymbol *Symbol = nullptr,
Craig Topper34a61bc2014-03-08 07:02:02 +000092 uint64_t Size = 0, unsigned ByteAlignment = 0) override;
Benjamin Kramer8c90fd72014-09-03 11:41:21 +000093 void EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol, uint64_t Size,
94 unsigned ByteAlignment = 0) override;
Daniel Dunbar9d40ef12010-05-26 20:37:00 +000095
Craig Topper34a61bc2014-03-08 07:02:02 +000096 void EmitFileDirective(StringRef Filename) override {
Daniel Dunbar6b4391a2010-07-19 20:44:20 +000097 // FIXME: Just ignore the .file; it isn't important enough to fail the
98 // entire assembly.
99
Rafael Espindola5645bad2013-10-16 01:05:45 +0000100 // report_fatal_error("unsupported directive: '.file'");
101 }
102
Craig Topper34a61bc2014-03-08 07:02:02 +0000103 void EmitIdent(StringRef IdentString) override {
Rafael Espindola5645bad2013-10-16 01:05:45 +0000104 llvm_unreachable("macho doesn't support this directive");
Chris Lattner601ef332010-01-25 18:58:59 +0000105 }
Daniel Dunbar9d40ef12010-05-26 20:37:00 +0000106
Tim Northover53d32512014-03-29 07:34:53 +0000107 void EmitLOHDirective(MCLOHType Kind, const MCLOHArgs &Args) override {
108 getAssembler().getLOHContainer().addDirective(Kind, Args);
109 }
110
Craig Topper34a61bc2014-03-08 07:02:02 +0000111 void FinishImpl() override;
Daniel Dunbar3016db32009-08-21 09:11:24 +0000112};
113
114} // end anonymous namespace.
115
Tim Northoverc3988b42014-03-29 07:05:06 +0000116void MCMachOStreamer::ChangeSection(const MCSection *Section,
117 const MCExpr *Subsection) {
118 // Change the section normally.
119 MCObjectStreamer::ChangeSection(Section, Subsection);
120 // Output a linker-local symbol so we don't need section-relative local
121 // relocations. The linker hates us when we do that.
122 if (LabelSections && !HasSectionLabel[Section]) {
123 MCSymbol *Label = getContext().CreateLinkerPrivateTempSymbol();
124 EmitLabel(Label);
125 HasSectionLabel[Section] = true;
126 }
127}
128
Rafael Espindola349c3292011-04-28 12:50:37 +0000129void MCMachOStreamer::EmitEHSymAttributes(const MCSymbol *Symbol,
130 MCSymbol *EHSymbol) {
131 MCSymbolData &SD =
132 getAssembler().getOrCreateSymbolData(*Symbol);
133 if (SD.isExternal())
134 EmitSymbolAttribute(EHSymbol, MCSA_Global);
135 if (SD.getFlags() & SF_WeakDefinition)
136 EmitSymbolAttribute(EHSymbol, MCSA_WeakDefinition);
Rafael Espindola0b474c22011-04-30 16:22:46 +0000137 if (SD.isPrivateExtern())
138 EmitSymbolAttribute(EHSymbol, MCSA_PrivateExtern);
Rafael Espindola349c3292011-04-28 12:50:37 +0000139}
140
Daniel Dunbar3016db32009-08-21 09:11:24 +0000141void MCMachOStreamer::EmitLabel(MCSymbol *Symbol) {
Rafael Espindola16795802010-11-28 16:22:59 +0000142 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
Rafael Espindola16795802010-11-28 16:22:59 +0000143
Rafael Espindolae5b74152010-11-28 17:18:55 +0000144 // isSymbolLinkerVisible uses the section.
Richard Mitton21101b32013-09-19 23:21:01 +0000145 AssignSection(Symbol, getCurrentSection().first);
Daniel Dunbarede8e6d2010-06-16 20:04:32 +0000146 // We have to create a new fragment if this is an atom defining symbol,
147 // fragments cannot span atoms.
Rafael Espindolae5b74152010-11-28 17:18:55 +0000148 if (getAssembler().isSymbolLinkerVisible(*Symbol))
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000149 insert(new MCDataFragment());
Daniel Dunbaraadb2ca2010-05-10 22:45:09 +0000150
Rafael Espindolae5b74152010-11-28 17:18:55 +0000151 MCObjectStreamer::EmitLabel(Symbol);
Daniel Dunbar04a11582009-08-24 08:40:12 +0000152
Rafael Espindolae5b74152010-11-28 17:18:55 +0000153 MCSymbolData &SD = getAssembler().getSymbolData(*Symbol);
Daniel Dunbar0211a962010-05-17 20:12:31 +0000154 // This causes the reference type flag to be cleared. Darwin 'as' was "trying"
155 // to clear the weak reference and weak definition bits too, but the
156 // implementation was buggy. For now we just try to match 'as', for
157 // diffability.
158 //
159 // FIXME: Cleanup this code, these bits should be emitted based on semantic
160 // properties, not on the order of definition, etc.
161 SD.setFlags(SD.getFlags() & ~SF_ReferenceTypeMask);
Daniel Dunbar3016db32009-08-21 09:11:24 +0000162}
163
Jim Grosbach4b63d2a2012-05-18 19:12:01 +0000164void MCMachOStreamer::EmitDataRegion(DataRegionData::KindTy Kind) {
Jim Grosbach745c52d2012-10-01 22:20:54 +0000165 if (!getAssembler().getBackend().hasDataInCodeSupport())
166 return;
Jim Grosbach4b63d2a2012-05-18 19:12:01 +0000167 // Create a temporary label to mark the start of the data region.
168 MCSymbol *Start = getContext().CreateTempSymbol();
169 EmitLabel(Start);
170 // Record the region for the object writer to use.
Craig Topperbb694de2014-04-13 04:57:38 +0000171 DataRegionData Data = { Kind, Start, nullptr };
Jim Grosbach4b63d2a2012-05-18 19:12:01 +0000172 std::vector<DataRegionData> &Regions = getAssembler().getDataRegions();
173 Regions.push_back(Data);
174}
175
176void MCMachOStreamer::EmitDataRegionEnd() {
Jim Grosbach745c52d2012-10-01 22:20:54 +0000177 if (!getAssembler().getBackend().hasDataInCodeSupport())
178 return;
Jim Grosbach4b63d2a2012-05-18 19:12:01 +0000179 std::vector<DataRegionData> &Regions = getAssembler().getDataRegions();
180 assert(Regions.size() && "Mismatched .end_data_region!");
181 DataRegionData &Data = Regions.back();
Craig Topperbb694de2014-04-13 04:57:38 +0000182 assert(!Data.End && "Mismatched .end_data_region!");
Jim Grosbach4b63d2a2012-05-18 19:12:01 +0000183 // Create a temporary label to mark the end of the data region.
184 Data.End = getContext().CreateTempSymbol();
185 EmitLabel(Data.End);
186}
187
Chris Lattner685508c2010-01-23 06:39:22 +0000188void MCMachOStreamer::EmitAssemblerFlag(MCAssemblerFlag Flag) {
Jim Grosbach87055ed2010-12-08 01:16:55 +0000189 // Let the target do whatever target specific stuff it needs to do.
Jim Grosbachaba3de92012-01-18 18:52:16 +0000190 getAssembler().getBackend().handleAssemblerFlag(Flag);
Jim Grosbach87055ed2010-12-08 01:16:55 +0000191 // Do any generic stuff we need to do.
Daniel Dunbare2697732009-08-26 21:22:22 +0000192 switch (Flag) {
Jim Grosbach5a2c68d2010-11-05 22:08:08 +0000193 case MCAF_SyntaxUnified: return; // no-op here.
Evan Cheng481ebb02011-07-27 00:38:12 +0000194 case MCAF_Code16: return; // Change parsing mode; no-op here.
195 case MCAF_Code32: return; // Change parsing mode; no-op here.
196 case MCAF_Code64: return; // Change parsing mode; no-op here.
Chris Lattner685508c2010-01-23 06:39:22 +0000197 case MCAF_SubsectionsViaSymbols:
Daniel Dunbar8a3c9d92010-06-16 20:04:22 +0000198 getAssembler().setSubsectionsViaSymbols(true);
Daniel Dunbar2a2459a2009-08-28 07:08:47 +0000199 return;
Daniel Dunbare2697732009-08-26 21:22:22 +0000200 }
Daniel Dunbar3016db32009-08-21 09:11:24 +0000201}
202
Daniel Dunbareec0f322013-01-18 01:26:07 +0000203void MCMachOStreamer::EmitLinkerOptions(ArrayRef<std::string> Options) {
204 getAssembler().getLinkerOptions().push_back(Options);
205}
206
Jim Grosbach4b63d2a2012-05-18 19:12:01 +0000207void MCMachOStreamer::EmitDataRegion(MCDataRegionType Kind) {
208 switch (Kind) {
209 case MCDR_DataRegion:
210 EmitDataRegion(DataRegionData::Data);
211 return;
212 case MCDR_DataRegionJT8:
213 EmitDataRegion(DataRegionData::JumpTable8);
214 return;
215 case MCDR_DataRegionJT16:
216 EmitDataRegion(DataRegionData::JumpTable16);
217 return;
218 case MCDR_DataRegionJT32:
219 EmitDataRegion(DataRegionData::JumpTable32);
220 return;
221 case MCDR_DataRegionEnd:
222 EmitDataRegionEnd();
223 return;
224 }
225}
226
Jim Grosbach448334a2014-03-18 22:09:05 +0000227void MCMachOStreamer::EmitVersionMin(MCVersionMinType Kind, unsigned Major,
228 unsigned Minor, unsigned Update) {
229 getAssembler().setVersionMinInfo(Kind, Major, Minor, Update);
230}
231
Daniel Dunbarab14a6f2010-12-29 14:14:06 +0000232void MCMachOStreamer::EmitThumbFunc(MCSymbol *Symbol) {
Jim Grosbach41955ff2010-12-14 18:46:57 +0000233 // Remember that the function is a thumb function. Fixup and relocation
234 // values will need adjusted.
Daniel Dunbarab14a6f2010-12-29 14:14:06 +0000235 getAssembler().setIsThumbFunc(Symbol);
Jim Grosbach5a2c68d2010-11-05 22:08:08 +0000236}
237
Saleem Abdulrasool4208b612013-08-09 01:52:03 +0000238bool MCMachOStreamer::EmitSymbolAttribute(MCSymbol *Symbol,
Chris Lattner685508c2010-01-23 06:39:22 +0000239 MCSymbolAttr Attribute) {
Daniel Dunbar582d6102009-08-24 11:56:58 +0000240 // Indirect symbols are handled differently, to match how 'as' handles
241 // them. This makes writing matching .o files easier.
Chris Lattner685508c2010-01-23 06:39:22 +0000242 if (Attribute == MCSA_IndirectSymbol) {
Daniel Dunbarc2c0bf92009-08-26 00:18:21 +0000243 // Note that we intentionally cannot use the symbol data here; this is
244 // important for matching the string table that 'as' generates.
Daniel Dunbar582d6102009-08-24 11:56:58 +0000245 IndirectSymbolData ISD;
246 ISD.Symbol = Symbol;
Daniel Dunbarb2347fe2010-06-16 20:04:25 +0000247 ISD.SectionData = getCurrentSectionData();
Daniel Dunbar8a3c9d92010-06-16 20:04:22 +0000248 getAssembler().getIndirectSymbols().push_back(ISD);
Saleem Abdulrasool4208b612013-08-09 01:52:03 +0000249 return true;
Daniel Dunbar582d6102009-08-24 11:56:58 +0000250 }
251
Daniel Dunbar04a11582009-08-24 08:40:12 +0000252 // Adding a symbol attribute always introduces the symbol, note that an
Daniel Dunbar6eab7212010-03-10 20:58:29 +0000253 // important side effect of calling getOrCreateSymbolData here is to register
254 // the symbol with the assembler.
Daniel Dunbar8a3c9d92010-06-16 20:04:22 +0000255 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
Daniel Dunbar04a11582009-08-24 08:40:12 +0000256
257 // The implementation of symbol attributes is designed to match 'as', but it
258 // leaves much to desired. It doesn't really make sense to arbitrarily add and
259 // remove flags, but 'as' allows this (in particular, see .desc).
260 //
261 // In the future it might be worth trying to make these operations more well
262 // defined.
Daniel Dunbard1859472009-08-22 11:41:10 +0000263 switch (Attribute) {
Chris Lattner685508c2010-01-23 06:39:22 +0000264 case MCSA_Invalid:
Chris Lattnerbc8f6382010-01-25 18:30:45 +0000265 case MCSA_ELF_TypeFunction:
266 case MCSA_ELF_TypeIndFunction:
267 case MCSA_ELF_TypeObject:
268 case MCSA_ELF_TypeTLS:
269 case MCSA_ELF_TypeCommon:
270 case MCSA_ELF_TypeNoType:
Rafael Espindola4bcf94c2010-11-13 04:51:02 +0000271 case MCSA_ELF_TypeGnuUniqueObject:
Chris Lattner685508c2010-01-23 06:39:22 +0000272 case MCSA_Hidden:
Chandler Carruthb2561022011-07-25 21:21:08 +0000273 case MCSA_IndirectSymbol:
Chris Lattner685508c2010-01-23 06:39:22 +0000274 case MCSA_Internal:
275 case MCSA_Protected:
276 case MCSA_Weak:
277 case MCSA_Local:
Saleem Abdulrasool4208b612013-08-09 01:52:03 +0000278 return false;
Daniel Dunbard1859472009-08-22 11:41:10 +0000279
Chris Lattner685508c2010-01-23 06:39:22 +0000280 case MCSA_Global:
Daniel Dunbar2701eee2009-08-28 07:08:35 +0000281 SD.setExternal(true);
Daniel Dunbar0211a962010-05-17 20:12:31 +0000282 // This effectively clears the undefined lazy bit, in Darwin 'as', although
283 // it isn't very consistent because it implements this as part of symbol
284 // lookup.
285 //
286 // FIXME: Cleanup this code, these bits should be emitted based on semantic
287 // properties, not on the order of definition, etc.
288 SD.setFlags(SD.getFlags() & ~SF_ReferenceTypeUndefinedLazy);
Daniel Dunbard1859472009-08-22 11:41:10 +0000289 break;
Daniel Dunbar04a11582009-08-24 08:40:12 +0000290
Chris Lattner685508c2010-01-23 06:39:22 +0000291 case MCSA_LazyReference:
Daniel Dunbar04a11582009-08-24 08:40:12 +0000292 // FIXME: This requires -dynamic.
293 SD.setFlags(SD.getFlags() | SF_NoDeadStrip);
294 if (Symbol->isUndefined())
295 SD.setFlags(SD.getFlags() | SF_ReferenceTypeUndefinedLazy);
296 break;
297
Daniel Dunbar04a11582009-08-24 08:40:12 +0000298 // Since .reference sets the no dead strip bit, it is equivalent to
299 // .no_dead_strip in practice.
Chris Lattner685508c2010-01-23 06:39:22 +0000300 case MCSA_Reference:
301 case MCSA_NoDeadStrip:
Daniel Dunbar04a11582009-08-24 08:40:12 +0000302 SD.setFlags(SD.getFlags() | SF_NoDeadStrip);
303 break;
304
Kevin Enderby8be14412010-11-19 18:39:33 +0000305 case MCSA_SymbolResolver:
306 SD.setFlags(SD.getFlags() | SF_SymbolResolver);
307 break;
308
Chris Lattner685508c2010-01-23 06:39:22 +0000309 case MCSA_PrivateExtern:
Daniel Dunbar04a11582009-08-24 08:40:12 +0000310 SD.setExternal(true);
311 SD.setPrivateExtern(true);
312 break;
313
Chris Lattner685508c2010-01-23 06:39:22 +0000314 case MCSA_WeakReference:
Daniel Dunbar04a11582009-08-24 08:40:12 +0000315 // FIXME: This requires -dynamic.
316 if (Symbol->isUndefined())
317 SD.setFlags(SD.getFlags() | SF_WeakReference);
318 break;
319
Chris Lattner685508c2010-01-23 06:39:22 +0000320 case MCSA_WeakDefinition:
Daniel Dunbar04a11582009-08-24 08:40:12 +0000321 // FIXME: 'as' enforces that this is defined and global. The manual claims
322 // it has to be in a coalesced section, but this isn't enforced.
323 SD.setFlags(SD.getFlags() | SF_WeakDefinition);
324 break;
Kevin Enderby082d0fd2010-07-08 17:22:42 +0000325
326 case MCSA_WeakDefAutoPrivate:
327 SD.setFlags(SD.getFlags() | SF_WeakDefinition | SF_WeakReference);
328 break;
Daniel Dunbard1859472009-08-22 11:41:10 +0000329 }
Saleem Abdulrasool4208b612013-08-09 01:52:03 +0000330
331 return true;
Daniel Dunbar3016db32009-08-21 09:11:24 +0000332}
333
334void MCMachOStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
Daniel Dunbar04a11582009-08-24 08:40:12 +0000335 // Encode the 'desc' value into the lowest implementation defined bits.
Daniel Dunbar9d40ef12010-05-26 20:37:00 +0000336 assert(DescValue == (DescValue & SF_DescFlagsMask) &&
Daniel Dunbar04a11582009-08-24 08:40:12 +0000337 "Invalid .desc value!");
Daniel Dunbar8a3c9d92010-06-16 20:04:22 +0000338 getAssembler().getOrCreateSymbolData(*Symbol).setFlags(
339 DescValue & SF_DescFlagsMask);
Daniel Dunbar3016db32009-08-21 09:11:24 +0000340}
341
Chris Lattnerb1301f72010-01-23 07:47:02 +0000342void MCMachOStreamer::EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
Daniel Dunbar6a715dc2009-08-30 06:17:16 +0000343 unsigned ByteAlignment) {
Daniel Dunbar2701eee2009-08-28 07:08:35 +0000344 // FIXME: Darwin 'as' does appear to allow redef of a .comm by itself.
345 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
346
Craig Topperbb694de2014-04-13 04:57:38 +0000347 AssignSection(Symbol, nullptr);
Richard Mitton21101b32013-09-19 23:21:01 +0000348
Daniel Dunbar8a3c9d92010-06-16 20:04:22 +0000349 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
Daniel Dunbar2701eee2009-08-28 07:08:35 +0000350 SD.setExternal(true);
Daniel Dunbar6a715dc2009-08-30 06:17:16 +0000351 SD.setCommon(Size, ByteAlignment);
Daniel Dunbar3016db32009-08-21 09:11:24 +0000352}
353
Benjamin Kramer47f9ec92012-09-07 17:25:13 +0000354void MCMachOStreamer::EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size,
355 unsigned ByteAlignment) {
356 // '.lcomm' is equivalent to '.zerofill'.
Rafael Espindolae308c0c2014-01-23 22:49:25 +0000357 return EmitZerofill(getContext().getObjectFileInfo()->getDataBSSSection(),
Benjamin Kramer47f9ec92012-09-07 17:25:13 +0000358 Symbol, Size, ByteAlignment);
359}
360
Daniel Dunbarcf72e1c2009-08-28 05:48:22 +0000361void MCMachOStreamer::EmitZerofill(const MCSection *Section, MCSymbol *Symbol,
Evan Chengf5bd6c62012-06-22 20:14:46 +0000362 uint64_t Size, unsigned ByteAlignment) {
Daniel Dunbar8a3c9d92010-06-16 20:04:22 +0000363 MCSectionData &SectData = getAssembler().getOrCreateSectionData(*Section);
Daniel Dunbar42a39d02009-08-28 05:49:21 +0000364
365 // The symbol may not be present, which only creates the section.
366 if (!Symbol)
367 return;
368
Eric Christopher42ae4592013-08-07 21:13:01 +0000369 // On darwin all virtual sections have zerofill type.
370 assert(Section->isVirtualSection() && "Section does not have zerofill type!");
Daniel Dunbar42a39d02009-08-28 05:49:21 +0000371
372 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
373
Daniel Dunbar8a3c9d92010-06-16 20:04:22 +0000374 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
Daniel Dunbar42a39d02009-08-28 05:49:21 +0000375
Daniel Dunbar51402b72010-05-12 22:51:27 +0000376 // Emit an align fragment if necessary.
377 if (ByteAlignment != 1)
Daniel Dunbarb76df222010-05-12 22:56:23 +0000378 new MCAlignFragment(ByteAlignment, 0, 0, ByteAlignment, &SectData);
Daniel Dunbar51402b72010-05-12 22:51:27 +0000379
Daniel Dunbar4405ffc2010-05-12 22:51:38 +0000380 MCFragment *F = new MCFillFragment(0, 0, Size, &SectData);
Daniel Dunbar42a39d02009-08-28 05:49:21 +0000381 SD.setFragment(F);
382
Richard Mitton21101b32013-09-19 23:21:01 +0000383 AssignSection(Symbol, Section);
Daniel Dunbar42a39d02009-08-28 05:49:21 +0000384
385 // Update the maximum alignment on the zero fill section if necessary.
386 if (ByteAlignment > SectData.getAlignment())
387 SectData.setAlignment(ByteAlignment);
Daniel Dunbar3016db32009-08-21 09:11:24 +0000388}
389
Eric Christopher09d47032010-05-21 23:03:53 +0000390// This should always be called with the thread local bss section. Like the
391// .zerofill directive this doesn't actually switch sections on us.
Eric Christopherfeedc902010-05-18 21:26:41 +0000392void MCMachOStreamer::EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol,
393 uint64_t Size, unsigned ByteAlignment) {
394 EmitZerofill(Section, Symbol, Size, ByteAlignment);
395 return;
Eric Christopher9fb6bb02010-05-14 01:50:28 +0000396}
397
David Woodhouse6f3c73f2014-01-28 23:12:49 +0000398void MCMachOStreamer::EmitInstToData(const MCInst &Inst,
399 const MCSubtargetInfo &STI) {
Daniel Dunbar9d40ef12010-05-26 20:37:00 +0000400 MCDataFragment *DF = getOrCreateDataFragment();
401
402 SmallVector<MCFixup, 4> Fixups;
403 SmallString<256> Code;
404 raw_svector_ostream VecOS(Code);
David Woodhouse9784cef2014-01-28 23:13:07 +0000405 getAssembler().getEmitter().EncodeInstruction(Inst, VecOS, Fixups, STI);
Daniel Dunbar9d40ef12010-05-26 20:37:00 +0000406 VecOS.flush();
407
408 // Add the fixups and data.
409 for (unsigned i = 0, e = Fixups.size(); i != e; ++i) {
410 Fixups[i].setOffset(Fixups[i].getOffset() + DF->getContents().size());
Eli Benderskya31a8942012-12-07 19:13:57 +0000411 DF->getFixups().push_back(Fixups[i]);
Daniel Dunbar9d40ef12010-05-26 20:37:00 +0000412 }
413 DF->getContents().append(Code.begin(), Code.end());
414}
415
Rafael Espindola07082092012-01-07 03:13:18 +0000416void MCMachOStreamer::FinishImpl() {
Rafael Espindola7f4ccce2014-05-12 13:30:10 +0000417 EmitFrames(&getAssembler().getBackend());
Rafael Espindolafc822362011-05-01 15:44:13 +0000418
Daniel Dunbarede8e6d2010-06-16 20:04:32 +0000419 // We have to set the fragment atom associations so we can relax properly for
420 // Mach-O.
421
422 // First, scan the symbol table to build a lookup table from fragments to
423 // defining symbols.
424 DenseMap<const MCFragment*, MCSymbolData*> DefiningSymbolMap;
David Blaikie583a31c2014-04-18 18:24:25 +0000425 for (MCSymbolData &SD : getAssembler().symbols()) {
426 if (getAssembler().isSymbolLinkerVisible(SD.getSymbol()) &&
427 SD.getFragment()) {
Daniel Dunbarede8e6d2010-06-16 20:04:32 +0000428 // An atom defining symbol should never be internal to a fragment.
David Blaikie583a31c2014-04-18 18:24:25 +0000429 assert(SD.getOffset() == 0 && "Invalid offset in atom defining symbol!");
430 DefiningSymbolMap[SD.getFragment()] = &SD;
Daniel Dunbarede8e6d2010-06-16 20:04:32 +0000431 }
432 }
433
434 // Set the fragment atom associations by tracking the last seen atom defining
435 // symbol.
436 for (MCAssembler::iterator it = getAssembler().begin(),
437 ie = getAssembler().end(); it != ie; ++it) {
Craig Topperbb694de2014-04-13 04:57:38 +0000438 MCSymbolData *CurrentAtom = nullptr;
Daniel Dunbarede8e6d2010-06-16 20:04:32 +0000439 for (MCSectionData::iterator it2 = it->begin(),
440 ie2 = it->end(); it2 != ie2; ++it2) {
441 if (MCSymbolData *SD = DefiningSymbolMap.lookup(it2))
442 CurrentAtom = SD;
443 it2->setAtom(CurrentAtom);
444 }
445 }
446
Rafael Espindola07082092012-01-07 03:13:18 +0000447 this->MCObjectStreamer::FinishImpl();
Daniel Dunbarede8e6d2010-06-16 20:04:32 +0000448}
449
Evan Cheng5928e692011-07-25 23:24:55 +0000450MCStreamer *llvm::createMachOStreamer(MCContext &Context, MCAsmBackend &MAB,
Daniel Dunbard821f4a2010-03-25 22:49:09 +0000451 raw_ostream &OS, MCCodeEmitter *CE,
Tim Northoverc3988b42014-03-29 07:05:06 +0000452 bool RelaxAll,
453 bool LabelSections) {
454 MCMachOStreamer *S = new MCMachOStreamer(Context, MAB, OS, CE, LabelSections);
Daniel Dunbard821f4a2010-03-25 22:49:09 +0000455 if (RelaxAll)
456 S->getAssembler().setRelaxAll(true);
457 return S;
Daniel Dunbar3016db32009-08-21 09:11:24 +0000458}