blob: c9777809a7f489ff50bd71cc44639b9254c5f57c [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"
Rafael Espindolacd584a82015-03-19 01:50:16 +000029#include "llvm/Support/TargetRegistry.h"
Daniel Dunbarc7c5f9f2009-08-27 08:17:51 +000030#include "llvm/Support/raw_ostream.h"
Daniel Dunbarde04b3f2010-03-23 05:09:03 +000031
Daniel Dunbar3016db32009-08-21 09:11:24 +000032using namespace llvm;
33
34namespace {
35
Daniel Dunbar8a3c9d92010-06-16 20:04:22 +000036class MCMachOStreamer : public MCObjectStreamer {
Daniel Dunbar04a11582009-08-24 08:40:12 +000037private:
Tim Northoverc3988b42014-03-29 07:05:06 +000038 /// LabelSections - true if each section change should emit a linker local
39 /// label for use in relocations for assembler local references. Obviates the
40 /// need for local relocations. False by default.
41 bool LabelSections;
42
Rafael Espindola36a15cb2015-03-20 20:00:01 +000043 bool DWARFMustBeAtTheEnd;
44 bool CreatedADWARFSection;
45
Tim Northoverc3988b42014-03-29 07:05:06 +000046 /// HasSectionLabel - map of which sections have already had a non-local
47 /// label emitted to them. Used so we don't emit extraneous linker local
48 /// labels in the middle of the section.
49 DenseMap<const MCSection*, bool> HasSectionLabel;
50
Craig Topper34a61bc2014-03-08 07:02:02 +000051 void EmitInstToData(const MCInst &Inst, const MCSubtargetInfo &STI) override;
Daniel Dunbar9d40ef12010-05-26 20:37:00 +000052
Jim Grosbach4b63d2a2012-05-18 19:12:01 +000053 void EmitDataRegion(DataRegionData::KindTy Kind);
54 void EmitDataRegionEnd();
Tim Northoverc3988b42014-03-29 07:05:06 +000055
Daniel Dunbar3016db32009-08-21 09:11:24 +000056public:
Rafael Espindola5560a4c2015-04-14 22:14:34 +000057 MCMachOStreamer(MCContext &Context, MCAsmBackend &MAB, raw_pwrite_stream &OS,
Rafael Espindola36a15cb2015-03-20 20:00:01 +000058 MCCodeEmitter *Emitter, bool DWARFMustBeAtTheEnd, bool label)
59 : MCObjectStreamer(Context, MAB, OS, Emitter), LabelSections(label),
60 DWARFMustBeAtTheEnd(DWARFMustBeAtTheEnd), CreatedADWARFSection(false) {}
Daniel Dunbard821f4a2010-03-25 22:49:09 +000061
Yaron Keren559b47d2014-09-17 09:25:36 +000062 /// state management
63 void reset() override {
64 HasSectionLabel.clear();
65 MCObjectStreamer::reset();
66 }
67
Daniel Dunbar3016db32009-08-21 09:11:24 +000068 /// @name MCStreamer Interface
69 /// @{
70
Rafael Espindola0709a7b2015-05-21 19:20:38 +000071 void ChangeSection(MCSection *Sect, const MCExpr *Subsect) override;
Craig Topper34a61bc2014-03-08 07:02:02 +000072 void EmitLabel(MCSymbol *Symbol) override;
Craig Topper34a61bc2014-03-08 07:02:02 +000073 void EmitEHSymAttributes(const MCSymbol *Symbol, MCSymbol *EHSymbol) override;
74 void EmitAssemblerFlag(MCAssemblerFlag Flag) override;
75 void EmitLinkerOptions(ArrayRef<std::string> Options) override;
76 void EmitDataRegion(MCDataRegionType Kind) override;
Jim Grosbach448334a2014-03-18 22:09:05 +000077 void EmitVersionMin(MCVersionMinType Kind, unsigned Major,
78 unsigned Minor, unsigned Update) override;
Craig Topper34a61bc2014-03-08 07:02:02 +000079 void EmitThumbFunc(MCSymbol *Func) override;
80 bool EmitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute) override;
81 void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) override;
82 void EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
83 unsigned ByteAlignment) override;
84 void BeginCOFFSymbolDef(const MCSymbol *Symbol) override {
Craig Toppera2886c22012-02-07 05:05:23 +000085 llvm_unreachable("macho doesn't support this directive");
Chris Lattner72afa952010-05-08 19:54:22 +000086 }
Craig Topper34a61bc2014-03-08 07:02:02 +000087 void EmitCOFFSymbolStorageClass(int StorageClass) override {
Craig Toppera2886c22012-02-07 05:05:23 +000088 llvm_unreachable("macho doesn't support this directive");
Chris Lattner72afa952010-05-08 19:54:22 +000089 }
Craig Topper34a61bc2014-03-08 07:02:02 +000090 void EmitCOFFSymbolType(int Type) override {
Craig Toppera2886c22012-02-07 05:05:23 +000091 llvm_unreachable("macho doesn't support this directive");
Chris Lattner72afa952010-05-08 19:54:22 +000092 }
Craig Topper34a61bc2014-03-08 07:02:02 +000093 void EndCOFFSymbolDef() override {
Craig Toppera2886c22012-02-07 05:05:23 +000094 llvm_unreachable("macho doesn't support this directive");
Chris Lattner72afa952010-05-08 19:54:22 +000095 }
Craig Topper34a61bc2014-03-08 07:02:02 +000096 void EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) override {
Craig Toppera2886c22012-02-07 05:05:23 +000097 llvm_unreachable("macho doesn't support this directive");
Chris Lattner91dac6d2010-01-25 07:52:13 +000098 }
Craig Topper34a61bc2014-03-08 07:02:02 +000099 void EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size,
100 unsigned ByteAlignment) override;
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000101 void EmitZerofill(MCSection *Section, MCSymbol *Symbol = nullptr,
Craig Topper34a61bc2014-03-08 07:02:02 +0000102 uint64_t Size = 0, unsigned ByteAlignment = 0) override;
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000103 void EmitTBSSSymbol(MCSection *Section, MCSymbol *Symbol, uint64_t Size,
Benjamin Kramer8c90fd72014-09-03 11:41:21 +0000104 unsigned ByteAlignment = 0) override;
Daniel Dunbar9d40ef12010-05-26 20:37:00 +0000105
Craig Topper34a61bc2014-03-08 07:02:02 +0000106 void EmitFileDirective(StringRef Filename) override {
Daniel Dunbar6b4391a2010-07-19 20:44:20 +0000107 // FIXME: Just ignore the .file; it isn't important enough to fail the
108 // entire assembly.
109
Rafael Espindola5645bad2013-10-16 01:05:45 +0000110 // report_fatal_error("unsupported directive: '.file'");
111 }
112
Craig Topper34a61bc2014-03-08 07:02:02 +0000113 void EmitIdent(StringRef IdentString) override {
Rafael Espindola5645bad2013-10-16 01:05:45 +0000114 llvm_unreachable("macho doesn't support this directive");
Chris Lattner601ef332010-01-25 18:58:59 +0000115 }
Daniel Dunbar9d40ef12010-05-26 20:37:00 +0000116
Tim Northover53d32512014-03-29 07:34:53 +0000117 void EmitLOHDirective(MCLOHType Kind, const MCLOHArgs &Args) override {
118 getAssembler().getLOHContainer().addDirective(Kind, Args);
119 }
120
Craig Topper34a61bc2014-03-08 07:02:02 +0000121 void FinishImpl() override;
Daniel Dunbar3016db32009-08-21 09:11:24 +0000122};
123
124} // end anonymous namespace.
125
Rafael Espindola36a15cb2015-03-20 20:00:01 +0000126static bool canGoAfterDWARF(const MCSectionMachO &MSec) {
127 // These sections are created by the assembler itself after the end of
128 // the .s file.
129 StringRef SegName = MSec.getSegmentName();
130 StringRef SecName = MSec.getSectionName();
131
132 if (SegName == "__LD" && SecName == "__compact_unwind")
133 return true;
134
135 if (SegName == "__IMPORT") {
136 if (SecName == "__jump_table")
137 return true;
138
139 if (SecName == "__pointers")
140 return true;
141 }
142
143 if (SegName == "__TEXT" && SecName == "__eh_frame")
144 return true;
145
146 if (SegName == "__DATA" && SecName == "__nl_symbol_ptr")
147 return true;
148
149 return false;
150}
151
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000152void MCMachOStreamer::ChangeSection(MCSection *Section,
Tim Northoverc3988b42014-03-29 07:05:06 +0000153 const MCExpr *Subsection) {
154 // Change the section normally.
Rafael Espindola36a15cb2015-03-20 20:00:01 +0000155 bool Created = MCObjectStreamer::changeSectionImpl(Section, Subsection);
156 const MCSectionMachO &MSec = *cast<MCSectionMachO>(Section);
157 StringRef SegName = MSec.getSegmentName();
158 if (SegName == "__DWARF")
159 CreatedADWARFSection = true;
160 else if (Created && DWARFMustBeAtTheEnd && !canGoAfterDWARF(MSec))
161 assert(!CreatedADWARFSection && "Creating regular section after DWARF");
162
Tim Northoverc3988b42014-03-29 07:05:06 +0000163 // Output a linker-local symbol so we don't need section-relative local
164 // relocations. The linker hates us when we do that.
165 if (LabelSections && !HasSectionLabel[Section]) {
Jim Grosbach6f482002015-05-18 18:43:14 +0000166 MCSymbol *Label = getContext().createLinkerPrivateTempSymbol();
Tim Northoverc3988b42014-03-29 07:05:06 +0000167 EmitLabel(Label);
168 HasSectionLabel[Section] = true;
169 }
170}
171
Rafael Espindola349c3292011-04-28 12:50:37 +0000172void MCMachOStreamer::EmitEHSymAttributes(const MCSymbol *Symbol,
173 MCSymbol *EHSymbol) {
174 MCSymbolData &SD =
175 getAssembler().getOrCreateSymbolData(*Symbol);
176 if (SD.isExternal())
177 EmitSymbolAttribute(EHSymbol, MCSA_Global);
178 if (SD.getFlags() & SF_WeakDefinition)
179 EmitSymbolAttribute(EHSymbol, MCSA_WeakDefinition);
Rafael Espindola0b474c22011-04-30 16:22:46 +0000180 if (SD.isPrivateExtern())
181 EmitSymbolAttribute(EHSymbol, MCSA_PrivateExtern);
Rafael Espindola349c3292011-04-28 12:50:37 +0000182}
183
Daniel Dunbar3016db32009-08-21 09:11:24 +0000184void MCMachOStreamer::EmitLabel(MCSymbol *Symbol) {
Rafael Espindola16795802010-11-28 16:22:59 +0000185 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
Rafael Espindola16795802010-11-28 16:22:59 +0000186
Rafael Espindolae5b74152010-11-28 17:18:55 +0000187 // isSymbolLinkerVisible uses the section.
Richard Mitton21101b32013-09-19 23:21:01 +0000188 AssignSection(Symbol, getCurrentSection().first);
Daniel Dunbarede8e6d2010-06-16 20:04:32 +0000189 // We have to create a new fragment if this is an atom defining symbol,
190 // fragments cannot span atoms.
Rafael Espindolae5b74152010-11-28 17:18:55 +0000191 if (getAssembler().isSymbolLinkerVisible(*Symbol))
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000192 insert(new MCDataFragment());
Daniel Dunbaraadb2ca2010-05-10 22:45:09 +0000193
Rafael Espindolae5b74152010-11-28 17:18:55 +0000194 MCObjectStreamer::EmitLabel(Symbol);
Daniel Dunbar04a11582009-08-24 08:40:12 +0000195
Rafael Espindolae5b74152010-11-28 17:18:55 +0000196 MCSymbolData &SD = getAssembler().getSymbolData(*Symbol);
Daniel Dunbar0211a962010-05-17 20:12:31 +0000197 // This causes the reference type flag to be cleared. Darwin 'as' was "trying"
198 // to clear the weak reference and weak definition bits too, but the
199 // implementation was buggy. For now we just try to match 'as', for
200 // diffability.
201 //
202 // FIXME: Cleanup this code, these bits should be emitted based on semantic
203 // properties, not on the order of definition, etc.
204 SD.setFlags(SD.getFlags() & ~SF_ReferenceTypeMask);
Daniel Dunbar3016db32009-08-21 09:11:24 +0000205}
206
Jim Grosbach4b63d2a2012-05-18 19:12:01 +0000207void MCMachOStreamer::EmitDataRegion(DataRegionData::KindTy Kind) {
Jim Grosbach745c52d2012-10-01 22:20:54 +0000208 if (!getAssembler().getBackend().hasDataInCodeSupport())
209 return;
Jim Grosbach4b63d2a2012-05-18 19:12:01 +0000210 // Create a temporary label to mark the start of the data region.
Jim Grosbach6f482002015-05-18 18:43:14 +0000211 MCSymbol *Start = getContext().createTempSymbol();
Jim Grosbach4b63d2a2012-05-18 19:12:01 +0000212 EmitLabel(Start);
213 // Record the region for the object writer to use.
Craig Topperbb694de2014-04-13 04:57:38 +0000214 DataRegionData Data = { Kind, Start, nullptr };
Jim Grosbach4b63d2a2012-05-18 19:12:01 +0000215 std::vector<DataRegionData> &Regions = getAssembler().getDataRegions();
216 Regions.push_back(Data);
217}
218
219void MCMachOStreamer::EmitDataRegionEnd() {
Jim Grosbach745c52d2012-10-01 22:20:54 +0000220 if (!getAssembler().getBackend().hasDataInCodeSupport())
221 return;
Jim Grosbach4b63d2a2012-05-18 19:12:01 +0000222 std::vector<DataRegionData> &Regions = getAssembler().getDataRegions();
Alexander Kornienko8c0809c2015-01-15 11:41:30 +0000223 assert(!Regions.empty() && "Mismatched .end_data_region!");
Jim Grosbach4b63d2a2012-05-18 19:12:01 +0000224 DataRegionData &Data = Regions.back();
Craig Topperbb694de2014-04-13 04:57:38 +0000225 assert(!Data.End && "Mismatched .end_data_region!");
Jim Grosbach4b63d2a2012-05-18 19:12:01 +0000226 // Create a temporary label to mark the end of the data region.
Jim Grosbach6f482002015-05-18 18:43:14 +0000227 Data.End = getContext().createTempSymbol();
Jim Grosbach4b63d2a2012-05-18 19:12:01 +0000228 EmitLabel(Data.End);
229}
230
Chris Lattner685508c2010-01-23 06:39:22 +0000231void MCMachOStreamer::EmitAssemblerFlag(MCAssemblerFlag Flag) {
Jim Grosbach87055ed2010-12-08 01:16:55 +0000232 // Let the target do whatever target specific stuff it needs to do.
Jim Grosbachaba3de92012-01-18 18:52:16 +0000233 getAssembler().getBackend().handleAssemblerFlag(Flag);
Jim Grosbach87055ed2010-12-08 01:16:55 +0000234 // Do any generic stuff we need to do.
Daniel Dunbare2697732009-08-26 21:22:22 +0000235 switch (Flag) {
Jim Grosbach5a2c68d2010-11-05 22:08:08 +0000236 case MCAF_SyntaxUnified: return; // no-op here.
Evan Cheng481ebb02011-07-27 00:38:12 +0000237 case MCAF_Code16: return; // Change parsing mode; no-op here.
238 case MCAF_Code32: return; // Change parsing mode; no-op here.
239 case MCAF_Code64: return; // Change parsing mode; no-op here.
Chris Lattner685508c2010-01-23 06:39:22 +0000240 case MCAF_SubsectionsViaSymbols:
Daniel Dunbar8a3c9d92010-06-16 20:04:22 +0000241 getAssembler().setSubsectionsViaSymbols(true);
Daniel Dunbar2a2459a2009-08-28 07:08:47 +0000242 return;
Daniel Dunbare2697732009-08-26 21:22:22 +0000243 }
Daniel Dunbar3016db32009-08-21 09:11:24 +0000244}
245
Daniel Dunbareec0f322013-01-18 01:26:07 +0000246void MCMachOStreamer::EmitLinkerOptions(ArrayRef<std::string> Options) {
247 getAssembler().getLinkerOptions().push_back(Options);
248}
249
Jim Grosbach4b63d2a2012-05-18 19:12:01 +0000250void MCMachOStreamer::EmitDataRegion(MCDataRegionType Kind) {
251 switch (Kind) {
252 case MCDR_DataRegion:
253 EmitDataRegion(DataRegionData::Data);
254 return;
255 case MCDR_DataRegionJT8:
256 EmitDataRegion(DataRegionData::JumpTable8);
257 return;
258 case MCDR_DataRegionJT16:
259 EmitDataRegion(DataRegionData::JumpTable16);
260 return;
261 case MCDR_DataRegionJT32:
262 EmitDataRegion(DataRegionData::JumpTable32);
263 return;
264 case MCDR_DataRegionEnd:
265 EmitDataRegionEnd();
266 return;
267 }
268}
269
Jim Grosbach448334a2014-03-18 22:09:05 +0000270void MCMachOStreamer::EmitVersionMin(MCVersionMinType Kind, unsigned Major,
271 unsigned Minor, unsigned Update) {
272 getAssembler().setVersionMinInfo(Kind, Major, Minor, Update);
273}
274
Daniel Dunbarab14a6f2010-12-29 14:14:06 +0000275void MCMachOStreamer::EmitThumbFunc(MCSymbol *Symbol) {
Jim Grosbach41955ff2010-12-14 18:46:57 +0000276 // Remember that the function is a thumb function. Fixup and relocation
277 // values will need adjusted.
Daniel Dunbarab14a6f2010-12-29 14:14:06 +0000278 getAssembler().setIsThumbFunc(Symbol);
Jim Grosbach5a2c68d2010-11-05 22:08:08 +0000279}
280
Saleem Abdulrasool4208b612013-08-09 01:52:03 +0000281bool MCMachOStreamer::EmitSymbolAttribute(MCSymbol *Symbol,
Chris Lattner685508c2010-01-23 06:39:22 +0000282 MCSymbolAttr Attribute) {
Daniel Dunbar582d6102009-08-24 11:56:58 +0000283 // Indirect symbols are handled differently, to match how 'as' handles
284 // them. This makes writing matching .o files easier.
Chris Lattner685508c2010-01-23 06:39:22 +0000285 if (Attribute == MCSA_IndirectSymbol) {
Daniel Dunbarc2c0bf92009-08-26 00:18:21 +0000286 // Note that we intentionally cannot use the symbol data here; this is
287 // important for matching the string table that 'as' generates.
Daniel Dunbar582d6102009-08-24 11:56:58 +0000288 IndirectSymbolData ISD;
289 ISD.Symbol = Symbol;
Daniel Dunbarb2347fe2010-06-16 20:04:25 +0000290 ISD.SectionData = getCurrentSectionData();
Daniel Dunbar8a3c9d92010-06-16 20:04:22 +0000291 getAssembler().getIndirectSymbols().push_back(ISD);
Saleem Abdulrasool4208b612013-08-09 01:52:03 +0000292 return true;
Daniel Dunbar582d6102009-08-24 11:56:58 +0000293 }
294
Daniel Dunbar04a11582009-08-24 08:40:12 +0000295 // Adding a symbol attribute always introduces the symbol, note that an
Daniel Dunbar6eab7212010-03-10 20:58:29 +0000296 // important side effect of calling getOrCreateSymbolData here is to register
297 // the symbol with the assembler.
Daniel Dunbar8a3c9d92010-06-16 20:04:22 +0000298 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
Daniel Dunbar04a11582009-08-24 08:40:12 +0000299
300 // The implementation of symbol attributes is designed to match 'as', but it
301 // leaves much to desired. It doesn't really make sense to arbitrarily add and
302 // remove flags, but 'as' allows this (in particular, see .desc).
303 //
304 // In the future it might be worth trying to make these operations more well
305 // defined.
Daniel Dunbard1859472009-08-22 11:41:10 +0000306 switch (Attribute) {
Chris Lattner685508c2010-01-23 06:39:22 +0000307 case MCSA_Invalid:
Chris Lattnerbc8f6382010-01-25 18:30:45 +0000308 case MCSA_ELF_TypeFunction:
309 case MCSA_ELF_TypeIndFunction:
310 case MCSA_ELF_TypeObject:
311 case MCSA_ELF_TypeTLS:
312 case MCSA_ELF_TypeCommon:
313 case MCSA_ELF_TypeNoType:
Rafael Espindola4bcf94c2010-11-13 04:51:02 +0000314 case MCSA_ELF_TypeGnuUniqueObject:
Chris Lattner685508c2010-01-23 06:39:22 +0000315 case MCSA_Hidden:
Chandler Carruthb2561022011-07-25 21:21:08 +0000316 case MCSA_IndirectSymbol:
Chris Lattner685508c2010-01-23 06:39:22 +0000317 case MCSA_Internal:
318 case MCSA_Protected:
319 case MCSA_Weak:
320 case MCSA_Local:
Saleem Abdulrasool4208b612013-08-09 01:52:03 +0000321 return false;
Daniel Dunbard1859472009-08-22 11:41:10 +0000322
Chris Lattner685508c2010-01-23 06:39:22 +0000323 case MCSA_Global:
Daniel Dunbar2701eee2009-08-28 07:08:35 +0000324 SD.setExternal(true);
Daniel Dunbar0211a962010-05-17 20:12:31 +0000325 // This effectively clears the undefined lazy bit, in Darwin 'as', although
326 // it isn't very consistent because it implements this as part of symbol
327 // lookup.
328 //
329 // FIXME: Cleanup this code, these bits should be emitted based on semantic
330 // properties, not on the order of definition, etc.
331 SD.setFlags(SD.getFlags() & ~SF_ReferenceTypeUndefinedLazy);
Daniel Dunbard1859472009-08-22 11:41:10 +0000332 break;
Daniel Dunbar04a11582009-08-24 08:40:12 +0000333
Chris Lattner685508c2010-01-23 06:39:22 +0000334 case MCSA_LazyReference:
Daniel Dunbar04a11582009-08-24 08:40:12 +0000335 // FIXME: This requires -dynamic.
336 SD.setFlags(SD.getFlags() | SF_NoDeadStrip);
337 if (Symbol->isUndefined())
338 SD.setFlags(SD.getFlags() | SF_ReferenceTypeUndefinedLazy);
339 break;
340
Daniel Dunbar04a11582009-08-24 08:40:12 +0000341 // Since .reference sets the no dead strip bit, it is equivalent to
342 // .no_dead_strip in practice.
Chris Lattner685508c2010-01-23 06:39:22 +0000343 case MCSA_Reference:
344 case MCSA_NoDeadStrip:
Daniel Dunbar04a11582009-08-24 08:40:12 +0000345 SD.setFlags(SD.getFlags() | SF_NoDeadStrip);
346 break;
347
Kevin Enderby8be14412010-11-19 18:39:33 +0000348 case MCSA_SymbolResolver:
349 SD.setFlags(SD.getFlags() | SF_SymbolResolver);
350 break;
351
Chris Lattner685508c2010-01-23 06:39:22 +0000352 case MCSA_PrivateExtern:
Daniel Dunbar04a11582009-08-24 08:40:12 +0000353 SD.setExternal(true);
354 SD.setPrivateExtern(true);
355 break;
356
Chris Lattner685508c2010-01-23 06:39:22 +0000357 case MCSA_WeakReference:
Daniel Dunbar04a11582009-08-24 08:40:12 +0000358 // FIXME: This requires -dynamic.
359 if (Symbol->isUndefined())
360 SD.setFlags(SD.getFlags() | SF_WeakReference);
361 break;
362
Chris Lattner685508c2010-01-23 06:39:22 +0000363 case MCSA_WeakDefinition:
Daniel Dunbar04a11582009-08-24 08:40:12 +0000364 // FIXME: 'as' enforces that this is defined and global. The manual claims
365 // it has to be in a coalesced section, but this isn't enforced.
366 SD.setFlags(SD.getFlags() | SF_WeakDefinition);
367 break;
Kevin Enderby082d0fd2010-07-08 17:22:42 +0000368
369 case MCSA_WeakDefAutoPrivate:
370 SD.setFlags(SD.getFlags() | SF_WeakDefinition | SF_WeakReference);
371 break;
Daniel Dunbard1859472009-08-22 11:41:10 +0000372 }
Saleem Abdulrasool4208b612013-08-09 01:52:03 +0000373
374 return true;
Daniel Dunbar3016db32009-08-21 09:11:24 +0000375}
376
377void MCMachOStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
Daniel Dunbar04a11582009-08-24 08:40:12 +0000378 // Encode the 'desc' value into the lowest implementation defined bits.
Daniel Dunbar9d40ef12010-05-26 20:37:00 +0000379 assert(DescValue == (DescValue & SF_DescFlagsMask) &&
Daniel Dunbar04a11582009-08-24 08:40:12 +0000380 "Invalid .desc value!");
Daniel Dunbar8a3c9d92010-06-16 20:04:22 +0000381 getAssembler().getOrCreateSymbolData(*Symbol).setFlags(
382 DescValue & SF_DescFlagsMask);
Daniel Dunbar3016db32009-08-21 09:11:24 +0000383}
384
Chris Lattnerb1301f72010-01-23 07:47:02 +0000385void MCMachOStreamer::EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
Daniel Dunbar6a715dc2009-08-30 06:17:16 +0000386 unsigned ByteAlignment) {
Daniel Dunbar2701eee2009-08-28 07:08:35 +0000387 // FIXME: Darwin 'as' does appear to allow redef of a .comm by itself.
388 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
389
Craig Topperbb694de2014-04-13 04:57:38 +0000390 AssignSection(Symbol, nullptr);
Richard Mitton21101b32013-09-19 23:21:01 +0000391
Daniel Dunbar8a3c9d92010-06-16 20:04:22 +0000392 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
Daniel Dunbar2701eee2009-08-28 07:08:35 +0000393 SD.setExternal(true);
Daniel Dunbar6a715dc2009-08-30 06:17:16 +0000394 SD.setCommon(Size, ByteAlignment);
Daniel Dunbar3016db32009-08-21 09:11:24 +0000395}
396
Benjamin Kramer47f9ec92012-09-07 17:25:13 +0000397void MCMachOStreamer::EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size,
398 unsigned ByteAlignment) {
399 // '.lcomm' is equivalent to '.zerofill'.
Rafael Espindolae308c0c2014-01-23 22:49:25 +0000400 return EmitZerofill(getContext().getObjectFileInfo()->getDataBSSSection(),
Benjamin Kramer47f9ec92012-09-07 17:25:13 +0000401 Symbol, Size, ByteAlignment);
402}
403
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000404void MCMachOStreamer::EmitZerofill(MCSection *Section, MCSymbol *Symbol,
Evan Chengf5bd6c62012-06-22 20:14:46 +0000405 uint64_t Size, unsigned ByteAlignment) {
Daniel Dunbar8a3c9d92010-06-16 20:04:22 +0000406 MCSectionData &SectData = getAssembler().getOrCreateSectionData(*Section);
Daniel Dunbar42a39d02009-08-28 05:49:21 +0000407
408 // The symbol may not be present, which only creates the section.
409 if (!Symbol)
410 return;
411
Eric Christopher42ae4592013-08-07 21:13:01 +0000412 // On darwin all virtual sections have zerofill type.
413 assert(Section->isVirtualSection() && "Section does not have zerofill type!");
Daniel Dunbar42a39d02009-08-28 05:49:21 +0000414
415 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
416
Daniel Dunbar8a3c9d92010-06-16 20:04:22 +0000417 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
Daniel Dunbar42a39d02009-08-28 05:49:21 +0000418
Daniel Dunbar51402b72010-05-12 22:51:27 +0000419 // Emit an align fragment if necessary.
420 if (ByteAlignment != 1)
Daniel Dunbarb76df222010-05-12 22:56:23 +0000421 new MCAlignFragment(ByteAlignment, 0, 0, ByteAlignment, &SectData);
Daniel Dunbar51402b72010-05-12 22:51:27 +0000422
Daniel Dunbar4405ffc2010-05-12 22:51:38 +0000423 MCFragment *F = new MCFillFragment(0, 0, Size, &SectData);
Daniel Dunbar42a39d02009-08-28 05:49:21 +0000424 SD.setFragment(F);
425
Richard Mitton21101b32013-09-19 23:21:01 +0000426 AssignSection(Symbol, Section);
Daniel Dunbar42a39d02009-08-28 05:49:21 +0000427
428 // Update the maximum alignment on the zero fill section if necessary.
429 if (ByteAlignment > SectData.getAlignment())
430 SectData.setAlignment(ByteAlignment);
Daniel Dunbar3016db32009-08-21 09:11:24 +0000431}
432
Eric Christopher09d47032010-05-21 23:03:53 +0000433// This should always be called with the thread local bss section. Like the
434// .zerofill directive this doesn't actually switch sections on us.
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000435void MCMachOStreamer::EmitTBSSSymbol(MCSection *Section, MCSymbol *Symbol,
Eric Christopherfeedc902010-05-18 21:26:41 +0000436 uint64_t Size, unsigned ByteAlignment) {
437 EmitZerofill(Section, Symbol, Size, ByteAlignment);
438 return;
Eric Christopher9fb6bb02010-05-14 01:50:28 +0000439}
440
David Woodhouse6f3c73f2014-01-28 23:12:49 +0000441void MCMachOStreamer::EmitInstToData(const MCInst &Inst,
442 const MCSubtargetInfo &STI) {
Daniel Dunbar9d40ef12010-05-26 20:37:00 +0000443 MCDataFragment *DF = getOrCreateDataFragment();
444
445 SmallVector<MCFixup, 4> Fixups;
446 SmallString<256> Code;
447 raw_svector_ostream VecOS(Code);
Jim Grosbach91df21f2015-05-15 19:13:16 +0000448 getAssembler().getEmitter().encodeInstruction(Inst, VecOS, Fixups, STI);
Daniel Dunbar9d40ef12010-05-26 20:37:00 +0000449 VecOS.flush();
450
451 // Add the fixups and data.
452 for (unsigned i = 0, e = Fixups.size(); i != e; ++i) {
453 Fixups[i].setOffset(Fixups[i].getOffset() + DF->getContents().size());
Eli Benderskya31a8942012-12-07 19:13:57 +0000454 DF->getFixups().push_back(Fixups[i]);
Daniel Dunbar9d40ef12010-05-26 20:37:00 +0000455 }
456 DF->getContents().append(Code.begin(), Code.end());
457}
458
Rafael Espindola07082092012-01-07 03:13:18 +0000459void MCMachOStreamer::FinishImpl() {
Rafael Espindola7f4ccce2014-05-12 13:30:10 +0000460 EmitFrames(&getAssembler().getBackend());
Rafael Espindolafc822362011-05-01 15:44:13 +0000461
Daniel Dunbarede8e6d2010-06-16 20:04:32 +0000462 // We have to set the fragment atom associations so we can relax properly for
463 // Mach-O.
464
465 // First, scan the symbol table to build a lookup table from fragments to
466 // defining symbols.
Duncan P. N. Exon Smith92a699c2015-05-20 20:18:16 +0000467 DenseMap<const MCFragment *, const MCSymbol *> DefiningSymbolMap;
Duncan P. N. Exon Smithf48de1c2015-05-16 00:35:24 +0000468 for (const MCSymbol &Symbol : getAssembler().symbols()) {
469 MCSymbolData &SD = Symbol.getData();
470 if (getAssembler().isSymbolLinkerVisible(Symbol) && SD.getFragment()) {
Daniel Dunbarede8e6d2010-06-16 20:04:32 +0000471 // An atom defining symbol should never be internal to a fragment.
David Blaikie583a31c2014-04-18 18:24:25 +0000472 assert(SD.getOffset() == 0 && "Invalid offset in atom defining symbol!");
Duncan P. N. Exon Smith92a699c2015-05-20 20:18:16 +0000473 DefiningSymbolMap[SD.getFragment()] = &Symbol;
Daniel Dunbarede8e6d2010-06-16 20:04:32 +0000474 }
475 }
476
477 // Set the fragment atom associations by tracking the last seen atom defining
478 // symbol.
479 for (MCAssembler::iterator it = getAssembler().begin(),
480 ie = getAssembler().end(); it != ie; ++it) {
Duncan P. N. Exon Smith09bfa582015-05-16 00:48:58 +0000481 const MCSymbol *CurrentAtom = nullptr;
Daniel Dunbarede8e6d2010-06-16 20:04:32 +0000482 for (MCSectionData::iterator it2 = it->begin(),
483 ie2 = it->end(); it2 != ie2; ++it2) {
Duncan P. N. Exon Smith92a699c2015-05-20 20:18:16 +0000484 if (const MCSymbol *Symbol = DefiningSymbolMap.lookup(it2))
485 CurrentAtom = Symbol;
Daniel Dunbarede8e6d2010-06-16 20:04:32 +0000486 it2->setAtom(CurrentAtom);
487 }
488 }
489
Rafael Espindola07082092012-01-07 03:13:18 +0000490 this->MCObjectStreamer::FinishImpl();
Daniel Dunbarede8e6d2010-06-16 20:04:32 +0000491}
492
Evan Cheng5928e692011-07-25 23:24:55 +0000493MCStreamer *llvm::createMachOStreamer(MCContext &Context, MCAsmBackend &MAB,
Rafael Espindola5560a4c2015-04-14 22:14:34 +0000494 raw_pwrite_stream &OS, MCCodeEmitter *CE,
Rafael Espindola36a15cb2015-03-20 20:00:01 +0000495 bool RelaxAll, bool DWARFMustBeAtTheEnd,
Tim Northoverc3988b42014-03-29 07:05:06 +0000496 bool LabelSections) {
Rafael Espindola36a15cb2015-03-20 20:00:01 +0000497 MCMachOStreamer *S = new MCMachOStreamer(Context, MAB, OS, CE,
498 DWARFMustBeAtTheEnd, LabelSections);
Daniel Dunbard821f4a2010-03-25 22:49:09 +0000499 if (RelaxAll)
500 S->getAssembler().setRelaxAll(true);
501 return S;
Daniel Dunbar3016db32009-08-21 09:11:24 +0000502}