blob: bd425bb73093f84c4b8f4fab7084e720e9cbb3c9 [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"
Rafael Espindolae308c0c2014-01-23 22:49:25 +000021#include "llvm/MC/MCObjectFileInfo.h"
Daniel Dunbar8a3c9d92010-06-16 20:04:22 +000022#include "llvm/MC/MCObjectStreamer.h"
Daniel Dunbar3016db32009-08-21 09:11:24 +000023#include "llvm/MC/MCSection.h"
Kevin Enderby7221b762010-08-09 22:52:14 +000024#include "llvm/MC/MCSectionMachO.h"
Pete Cooper916f79e2015-06-08 17:17:28 +000025#include "llvm/MC/MCSymbolMachO.h"
Lang Hames1b640e02016-03-15 01:43:05 +000026#include "llvm/MC/MCValue.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 {
Justin Bogner72e81892015-12-03 00:52:20 +000064 CreatedADWARFSection = false;
Yaron Keren559b47d2014-09-17 09:25:36 +000065 HasSectionLabel.clear();
66 MCObjectStreamer::reset();
67 }
68
Daniel Dunbar3016db32009-08-21 09:11:24 +000069 /// @name MCStreamer Interface
70 /// @{
71
Rafael Espindola0709a7b2015-05-21 19:20:38 +000072 void ChangeSection(MCSection *Sect, const MCExpr *Subsect) override;
Craig Topper34a61bc2014-03-08 07:02:02 +000073 void EmitLabel(MCSymbol *Symbol) override;
Lang Hames1b640e02016-03-15 01:43:05 +000074 void EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) override;
Craig Topper34a61bc2014-03-08 07:02:02 +000075 void EmitEHSymAttributes(const MCSymbol *Symbol, MCSymbol *EHSymbol) override;
76 void EmitAssemblerFlag(MCAssemblerFlag Flag) override;
77 void EmitLinkerOptions(ArrayRef<std::string> Options) override;
78 void EmitDataRegion(MCDataRegionType Kind) override;
Jim Grosbach448334a2014-03-18 22:09:05 +000079 void EmitVersionMin(MCVersionMinType Kind, unsigned Major,
80 unsigned Minor, unsigned Update) override;
Craig Topper34a61bc2014-03-08 07:02:02 +000081 void EmitThumbFunc(MCSymbol *Func) override;
82 bool EmitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute) override;
83 void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) override;
84 void EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
85 unsigned ByteAlignment) override;
86 void BeginCOFFSymbolDef(const MCSymbol *Symbol) override {
Craig Toppera2886c22012-02-07 05:05:23 +000087 llvm_unreachable("macho doesn't support this directive");
Chris Lattner72afa952010-05-08 19:54:22 +000088 }
Craig Topper34a61bc2014-03-08 07:02:02 +000089 void EmitCOFFSymbolStorageClass(int StorageClass) override {
Craig Toppera2886c22012-02-07 05:05:23 +000090 llvm_unreachable("macho doesn't support this directive");
Chris Lattner72afa952010-05-08 19:54:22 +000091 }
Craig Topper34a61bc2014-03-08 07:02:02 +000092 void EmitCOFFSymbolType(int Type) override {
Craig Toppera2886c22012-02-07 05:05:23 +000093 llvm_unreachable("macho doesn't support this directive");
Chris Lattner72afa952010-05-08 19:54:22 +000094 }
Craig Topper34a61bc2014-03-08 07:02:02 +000095 void EndCOFFSymbolDef() override {
Craig Toppera2886c22012-02-07 05:05:23 +000096 llvm_unreachable("macho doesn't support this directive");
Chris Lattner72afa952010-05-08 19:54:22 +000097 }
Craig Topper34a61bc2014-03-08 07:02:02 +000098 void EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size,
99 unsigned ByteAlignment) override;
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000100 void EmitZerofill(MCSection *Section, MCSymbol *Symbol = nullptr,
Craig Topper34a61bc2014-03-08 07:02:02 +0000101 uint64_t Size = 0, unsigned ByteAlignment = 0) override;
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000102 void EmitTBSSSymbol(MCSection *Section, MCSymbol *Symbol, uint64_t Size,
Benjamin Kramer8c90fd72014-09-03 11:41:21 +0000103 unsigned ByteAlignment = 0) override;
Daniel Dunbar9d40ef12010-05-26 20:37:00 +0000104
Craig Topper34a61bc2014-03-08 07:02:02 +0000105 void EmitFileDirective(StringRef Filename) override {
Daniel Dunbar6b4391a2010-07-19 20:44:20 +0000106 // FIXME: Just ignore the .file; it isn't important enough to fail the
107 // entire assembly.
108
Rafael Espindola5645bad2013-10-16 01:05:45 +0000109 // report_fatal_error("unsupported directive: '.file'");
110 }
111
Craig Topper34a61bc2014-03-08 07:02:02 +0000112 void EmitIdent(StringRef IdentString) override {
Rafael Espindola5645bad2013-10-16 01:05:45 +0000113 llvm_unreachable("macho doesn't support this directive");
Chris Lattner601ef332010-01-25 18:58:59 +0000114 }
Daniel Dunbar9d40ef12010-05-26 20:37:00 +0000115
Tim Northover53d32512014-03-29 07:34:53 +0000116 void EmitLOHDirective(MCLOHType Kind, const MCLOHArgs &Args) override {
117 getAssembler().getLOHContainer().addDirective(Kind, Args);
118 }
119
Craig Topper34a61bc2014-03-08 07:02:02 +0000120 void FinishImpl() override;
Daniel Dunbar3016db32009-08-21 09:11:24 +0000121};
122
123} // end anonymous namespace.
124
Rafael Espindola36a15cb2015-03-20 20:00:01 +0000125static bool canGoAfterDWARF(const MCSectionMachO &MSec) {
126 // These sections are created by the assembler itself after the end of
127 // the .s file.
128 StringRef SegName = MSec.getSegmentName();
129 StringRef SecName = MSec.getSectionName();
130
131 if (SegName == "__LD" && SecName == "__compact_unwind")
132 return true;
133
134 if (SegName == "__IMPORT") {
135 if (SecName == "__jump_table")
136 return true;
137
138 if (SecName == "__pointers")
139 return true;
140 }
141
142 if (SegName == "__TEXT" && SecName == "__eh_frame")
143 return true;
144
Matthias Braun8d115a32017-02-01 01:31:36 +0000145 if (SegName == "__DATA" && (SecName == "__nl_symbol_ptr" ||
146 SecName == "__thread_ptr"))
Rafael Espindola36a15cb2015-03-20 20:00:01 +0000147 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.
Rafael Espindola2f9bdd82015-05-27 20:52:32 +0000165 if (LabelSections && !HasSectionLabel[Section] &&
166 !Section->getBeginSymbol()) {
Jim Grosbach6f482002015-05-18 18:43:14 +0000167 MCSymbol *Label = getContext().createLinkerPrivateTempSymbol();
Rafael Espindola2f9bdd82015-05-27 20:52:32 +0000168 Section->setBeginSymbol(Label);
Tim Northoverc3988b42014-03-29 07:05:06 +0000169 HasSectionLabel[Section] = true;
170 }
171}
172
Rafael Espindola349c3292011-04-28 12:50:37 +0000173void MCMachOStreamer::EmitEHSymAttributes(const MCSymbol *Symbol,
174 MCSymbol *EHSymbol) {
Rafael Espindolab5d316b2015-05-29 20:21:02 +0000175 getAssembler().registerSymbol(*Symbol);
Rafael Espindola4d37b2a2015-05-29 21:45:01 +0000176 if (Symbol->isExternal())
Rafael Espindola349c3292011-04-28 12:50:37 +0000177 EmitSymbolAttribute(EHSymbol, MCSA_Global);
Pete Cooper916f79e2015-06-08 17:17:28 +0000178 if (cast<MCSymbolMachO>(Symbol)->isWeakDefinition())
Rafael Espindola349c3292011-04-28 12:50:37 +0000179 EmitSymbolAttribute(EHSymbol, MCSA_WeakDefinition);
Rafael Espindola4d37b2a2015-05-29 21:45:01 +0000180 if (Symbol->isPrivateExtern())
Rafael Espindola0b474c22011-04-30 16:22:46 +0000181 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
Daniel Dunbarede8e6d2010-06-16 20:04:32 +0000187 // We have to create a new fragment if this is an atom defining symbol,
188 // fragments cannot span atoms.
Rafael Espindolae5b74152010-11-28 17:18:55 +0000189 if (getAssembler().isSymbolLinkerVisible(*Symbol))
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000190 insert(new MCDataFragment());
Daniel Dunbaraadb2ca2010-05-10 22:45:09 +0000191
Rafael Espindolae5b74152010-11-28 17:18:55 +0000192 MCObjectStreamer::EmitLabel(Symbol);
Daniel Dunbar04a11582009-08-24 08:40:12 +0000193
Daniel Dunbar0211a962010-05-17 20:12:31 +0000194 // This causes the reference type flag to be cleared. Darwin 'as' was "trying"
195 // to clear the weak reference and weak definition bits too, but the
196 // implementation was buggy. For now we just try to match 'as', for
197 // diffability.
198 //
199 // FIXME: Cleanup this code, these bits should be emitted based on semantic
200 // properties, not on the order of definition, etc.
Pete Cooper916f79e2015-06-08 17:17:28 +0000201 cast<MCSymbolMachO>(Symbol)->clearReferenceType();
Daniel Dunbar3016db32009-08-21 09:11:24 +0000202}
203
Lang Hames1b640e02016-03-15 01:43:05 +0000204void MCMachOStreamer::EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) {
205 MCValue Res;
206
Lang Hamesabda4d22016-03-15 04:20:49 +0000207 if (Value->evaluateAsRelocatable(Res, nullptr, nullptr)) {
208 if (const MCSymbolRefExpr *SymAExpr = Res.getSymA()) {
209 const MCSymbol &SymA = SymAExpr->getSymbol();
210 if (!Res.getSymB() && (SymA.getName() == "" || Res.getConstant() != 0))
211 cast<MCSymbolMachO>(Symbol)->setAltEntry();
212 }
213 }
Lang Hames1b640e02016-03-15 01:43:05 +0000214 MCObjectStreamer::EmitAssignment(Symbol, Value);
215}
216
Jim Grosbach4b63d2a2012-05-18 19:12:01 +0000217void MCMachOStreamer::EmitDataRegion(DataRegionData::KindTy Kind) {
218 // Create a temporary label to mark the start of the data region.
Jim Grosbach6f482002015-05-18 18:43:14 +0000219 MCSymbol *Start = getContext().createTempSymbol();
Jim Grosbach4b63d2a2012-05-18 19:12:01 +0000220 EmitLabel(Start);
221 // Record the region for the object writer to use.
Craig Topperbb694de2014-04-13 04:57:38 +0000222 DataRegionData Data = { Kind, Start, nullptr };
Jim Grosbach4b63d2a2012-05-18 19:12:01 +0000223 std::vector<DataRegionData> &Regions = getAssembler().getDataRegions();
224 Regions.push_back(Data);
225}
226
227void MCMachOStreamer::EmitDataRegionEnd() {
228 std::vector<DataRegionData> &Regions = getAssembler().getDataRegions();
Alexander Kornienko8c0809c2015-01-15 11:41:30 +0000229 assert(!Regions.empty() && "Mismatched .end_data_region!");
Jim Grosbach4b63d2a2012-05-18 19:12:01 +0000230 DataRegionData &Data = Regions.back();
Craig Topperbb694de2014-04-13 04:57:38 +0000231 assert(!Data.End && "Mismatched .end_data_region!");
Jim Grosbach4b63d2a2012-05-18 19:12:01 +0000232 // Create a temporary label to mark the end of the data region.
Jim Grosbach6f482002015-05-18 18:43:14 +0000233 Data.End = getContext().createTempSymbol();
Jim Grosbach4b63d2a2012-05-18 19:12:01 +0000234 EmitLabel(Data.End);
235}
236
Chris Lattner685508c2010-01-23 06:39:22 +0000237void MCMachOStreamer::EmitAssemblerFlag(MCAssemblerFlag Flag) {
Jim Grosbach87055ed2010-12-08 01:16:55 +0000238 // Let the target do whatever target specific stuff it needs to do.
Jim Grosbachaba3de92012-01-18 18:52:16 +0000239 getAssembler().getBackend().handleAssemblerFlag(Flag);
Jim Grosbach87055ed2010-12-08 01:16:55 +0000240 // Do any generic stuff we need to do.
Daniel Dunbare2697732009-08-26 21:22:22 +0000241 switch (Flag) {
Jim Grosbach5a2c68d2010-11-05 22:08:08 +0000242 case MCAF_SyntaxUnified: return; // no-op here.
Evan Cheng481ebb02011-07-27 00:38:12 +0000243 case MCAF_Code16: return; // Change parsing mode; no-op here.
244 case MCAF_Code32: return; // Change parsing mode; no-op here.
245 case MCAF_Code64: return; // Change parsing mode; no-op here.
Chris Lattner685508c2010-01-23 06:39:22 +0000246 case MCAF_SubsectionsViaSymbols:
Daniel Dunbar8a3c9d92010-06-16 20:04:22 +0000247 getAssembler().setSubsectionsViaSymbols(true);
Daniel Dunbar2a2459a2009-08-28 07:08:47 +0000248 return;
Daniel Dunbare2697732009-08-26 21:22:22 +0000249 }
Daniel Dunbar3016db32009-08-21 09:11:24 +0000250}
251
Daniel Dunbareec0f322013-01-18 01:26:07 +0000252void MCMachOStreamer::EmitLinkerOptions(ArrayRef<std::string> Options) {
253 getAssembler().getLinkerOptions().push_back(Options);
254}
255
Jim Grosbach4b63d2a2012-05-18 19:12:01 +0000256void MCMachOStreamer::EmitDataRegion(MCDataRegionType Kind) {
257 switch (Kind) {
258 case MCDR_DataRegion:
259 EmitDataRegion(DataRegionData::Data);
260 return;
261 case MCDR_DataRegionJT8:
262 EmitDataRegion(DataRegionData::JumpTable8);
263 return;
264 case MCDR_DataRegionJT16:
265 EmitDataRegion(DataRegionData::JumpTable16);
266 return;
267 case MCDR_DataRegionJT32:
268 EmitDataRegion(DataRegionData::JumpTable32);
269 return;
270 case MCDR_DataRegionEnd:
271 EmitDataRegionEnd();
272 return;
273 }
274}
275
Jim Grosbach448334a2014-03-18 22:09:05 +0000276void MCMachOStreamer::EmitVersionMin(MCVersionMinType Kind, unsigned Major,
277 unsigned Minor, unsigned Update) {
278 getAssembler().setVersionMinInfo(Kind, Major, Minor, Update);
279}
280
Daniel Dunbarab14a6f2010-12-29 14:14:06 +0000281void MCMachOStreamer::EmitThumbFunc(MCSymbol *Symbol) {
Jim Grosbach41955ff2010-12-14 18:46:57 +0000282 // Remember that the function is a thumb function. Fixup and relocation
283 // values will need adjusted.
Daniel Dunbarab14a6f2010-12-29 14:14:06 +0000284 getAssembler().setIsThumbFunc(Symbol);
Pete Cooper916f79e2015-06-08 17:17:28 +0000285 cast<MCSymbolMachO>(Symbol)->setThumbFunc();
Jim Grosbach5a2c68d2010-11-05 22:08:08 +0000286}
287
Pete Cooper916f79e2015-06-08 17:17:28 +0000288bool MCMachOStreamer::EmitSymbolAttribute(MCSymbol *Sym,
Chris Lattner685508c2010-01-23 06:39:22 +0000289 MCSymbolAttr Attribute) {
Pete Cooper916f79e2015-06-08 17:17:28 +0000290 MCSymbolMachO *Symbol = cast<MCSymbolMachO>(Sym);
291
Daniel Dunbar582d6102009-08-24 11:56:58 +0000292 // Indirect symbols are handled differently, to match how 'as' handles
293 // them. This makes writing matching .o files easier.
Chris Lattner685508c2010-01-23 06:39:22 +0000294 if (Attribute == MCSA_IndirectSymbol) {
Daniel Dunbarc2c0bf92009-08-26 00:18:21 +0000295 // Note that we intentionally cannot use the symbol data here; this is
296 // important for matching the string table that 'as' generates.
Daniel Dunbar582d6102009-08-24 11:56:58 +0000297 IndirectSymbolData ISD;
298 ISD.Symbol = Symbol;
Rafael Espindola983bec62015-05-27 21:04:14 +0000299 ISD.Section = getCurrentSectionOnly();
Daniel Dunbar8a3c9d92010-06-16 20:04:22 +0000300 getAssembler().getIndirectSymbols().push_back(ISD);
Saleem Abdulrasool4208b612013-08-09 01:52:03 +0000301 return true;
Daniel Dunbar582d6102009-08-24 11:56:58 +0000302 }
303
Daniel Dunbar04a11582009-08-24 08:40:12 +0000304 // Adding a symbol attribute always introduces the symbol, note that an
Rafael Espindolab5d316b2015-05-29 20:21:02 +0000305 // important side effect of calling registerSymbol here is to register
Daniel Dunbar6eab7212010-03-10 20:58:29 +0000306 // the symbol with the assembler.
Rafael Espindolab5d316b2015-05-29 20:21:02 +0000307 getAssembler().registerSymbol(*Symbol);
Daniel Dunbar04a11582009-08-24 08:40:12 +0000308
309 // The implementation of symbol attributes is designed to match 'as', but it
310 // leaves much to desired. It doesn't really make sense to arbitrarily add and
311 // remove flags, but 'as' allows this (in particular, see .desc).
312 //
313 // In the future it might be worth trying to make these operations more well
314 // defined.
Daniel Dunbard1859472009-08-22 11:41:10 +0000315 switch (Attribute) {
Chris Lattner685508c2010-01-23 06:39:22 +0000316 case MCSA_Invalid:
Chris Lattnerbc8f6382010-01-25 18:30:45 +0000317 case MCSA_ELF_TypeFunction:
318 case MCSA_ELF_TypeIndFunction:
319 case MCSA_ELF_TypeObject:
320 case MCSA_ELF_TypeTLS:
321 case MCSA_ELF_TypeCommon:
322 case MCSA_ELF_TypeNoType:
Rafael Espindola4bcf94c2010-11-13 04:51:02 +0000323 case MCSA_ELF_TypeGnuUniqueObject:
Chris Lattner685508c2010-01-23 06:39:22 +0000324 case MCSA_Hidden:
Chandler Carruthb2561022011-07-25 21:21:08 +0000325 case MCSA_IndirectSymbol:
Chris Lattner685508c2010-01-23 06:39:22 +0000326 case MCSA_Internal:
327 case MCSA_Protected:
328 case MCSA_Weak:
329 case MCSA_Local:
Saleem Abdulrasool4208b612013-08-09 01:52:03 +0000330 return false;
Daniel Dunbard1859472009-08-22 11:41:10 +0000331
Chris Lattner685508c2010-01-23 06:39:22 +0000332 case MCSA_Global:
Rafael Espindola4d37b2a2015-05-29 21:45:01 +0000333 Symbol->setExternal(true);
Daniel Dunbar0211a962010-05-17 20:12:31 +0000334 // This effectively clears the undefined lazy bit, in Darwin 'as', although
335 // it isn't very consistent because it implements this as part of symbol
336 // lookup.
337 //
338 // FIXME: Cleanup this code, these bits should be emitted based on semantic
339 // properties, not on the order of definition, etc.
Pete Cooper916f79e2015-06-08 17:17:28 +0000340 Symbol->setReferenceTypeUndefinedLazy(false);
Daniel Dunbard1859472009-08-22 11:41:10 +0000341 break;
Daniel Dunbar04a11582009-08-24 08:40:12 +0000342
Chris Lattner685508c2010-01-23 06:39:22 +0000343 case MCSA_LazyReference:
Daniel Dunbar04a11582009-08-24 08:40:12 +0000344 // FIXME: This requires -dynamic.
Pete Cooper916f79e2015-06-08 17:17:28 +0000345 Symbol->setNoDeadStrip();
Daniel Dunbar04a11582009-08-24 08:40:12 +0000346 if (Symbol->isUndefined())
Pete Cooper916f79e2015-06-08 17:17:28 +0000347 Symbol->setReferenceTypeUndefinedLazy(true);
Daniel Dunbar04a11582009-08-24 08:40:12 +0000348 break;
349
Daniel Dunbar04a11582009-08-24 08:40:12 +0000350 // Since .reference sets the no dead strip bit, it is equivalent to
351 // .no_dead_strip in practice.
Chris Lattner685508c2010-01-23 06:39:22 +0000352 case MCSA_Reference:
353 case MCSA_NoDeadStrip:
Pete Cooper916f79e2015-06-08 17:17:28 +0000354 Symbol->setNoDeadStrip();
Daniel Dunbar04a11582009-08-24 08:40:12 +0000355 break;
356
Kevin Enderby8be14412010-11-19 18:39:33 +0000357 case MCSA_SymbolResolver:
Pete Cooper916f79e2015-06-08 17:17:28 +0000358 Symbol->setSymbolResolver();
Kevin Enderby8be14412010-11-19 18:39:33 +0000359 break;
360
Lang Hames1b640e02016-03-15 01:43:05 +0000361 case MCSA_AltEntry:
362 Symbol->setAltEntry();
363 break;
364
Chris Lattner685508c2010-01-23 06:39:22 +0000365 case MCSA_PrivateExtern:
Rafael Espindola4d37b2a2015-05-29 21:45:01 +0000366 Symbol->setExternal(true);
367 Symbol->setPrivateExtern(true);
Daniel Dunbar04a11582009-08-24 08:40:12 +0000368 break;
369
Chris Lattner685508c2010-01-23 06:39:22 +0000370 case MCSA_WeakReference:
Daniel Dunbar04a11582009-08-24 08:40:12 +0000371 // FIXME: This requires -dynamic.
372 if (Symbol->isUndefined())
Pete Cooper916f79e2015-06-08 17:17:28 +0000373 Symbol->setWeakReference();
Daniel Dunbar04a11582009-08-24 08:40:12 +0000374 break;
375
Chris Lattner685508c2010-01-23 06:39:22 +0000376 case MCSA_WeakDefinition:
Daniel Dunbar04a11582009-08-24 08:40:12 +0000377 // FIXME: 'as' enforces that this is defined and global. The manual claims
378 // it has to be in a coalesced section, but this isn't enforced.
Pete Cooper916f79e2015-06-08 17:17:28 +0000379 Symbol->setWeakDefinition();
Daniel Dunbar04a11582009-08-24 08:40:12 +0000380 break;
Kevin Enderby082d0fd2010-07-08 17:22:42 +0000381
382 case MCSA_WeakDefAutoPrivate:
Pete Cooper916f79e2015-06-08 17:17:28 +0000383 Symbol->setWeakDefinition();
384 Symbol->setWeakReference();
Kevin Enderby082d0fd2010-07-08 17:22:42 +0000385 break;
Daniel Dunbard1859472009-08-22 11:41:10 +0000386 }
Saleem Abdulrasool4208b612013-08-09 01:52:03 +0000387
388 return true;
Daniel Dunbar3016db32009-08-21 09:11:24 +0000389}
390
391void MCMachOStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
Daniel Dunbar04a11582009-08-24 08:40:12 +0000392 // Encode the 'desc' value into the lowest implementation defined bits.
Rafael Espindolab5d316b2015-05-29 20:21:02 +0000393 getAssembler().registerSymbol(*Symbol);
Pete Cooper916f79e2015-06-08 17:17:28 +0000394 cast<MCSymbolMachO>(Symbol)->setDesc(DescValue);
Daniel Dunbar3016db32009-08-21 09:11:24 +0000395}
396
Chris Lattnerb1301f72010-01-23 07:47:02 +0000397void MCMachOStreamer::EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
Daniel Dunbar6a715dc2009-08-30 06:17:16 +0000398 unsigned ByteAlignment) {
Daniel Dunbar2701eee2009-08-28 07:08:35 +0000399 // FIXME: Darwin 'as' does appear to allow redef of a .comm by itself.
400 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
401
Rafael Espindolab5d316b2015-05-29 20:21:02 +0000402 getAssembler().registerSymbol(*Symbol);
Rafael Espindola4d37b2a2015-05-29 21:45:01 +0000403 Symbol->setExternal(true);
Rafael Espindola14672502015-05-29 17:48:04 +0000404 Symbol->setCommon(Size, ByteAlignment);
Daniel Dunbar3016db32009-08-21 09:11:24 +0000405}
406
Benjamin Kramer47f9ec92012-09-07 17:25:13 +0000407void MCMachOStreamer::EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size,
408 unsigned ByteAlignment) {
409 // '.lcomm' is equivalent to '.zerofill'.
Rafael Espindolae308c0c2014-01-23 22:49:25 +0000410 return EmitZerofill(getContext().getObjectFileInfo()->getDataBSSSection(),
Benjamin Kramer47f9ec92012-09-07 17:25:13 +0000411 Symbol, Size, ByteAlignment);
412}
413
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000414void MCMachOStreamer::EmitZerofill(MCSection *Section, MCSymbol *Symbol,
Evan Chengf5bd6c62012-06-22 20:14:46 +0000415 uint64_t Size, unsigned ByteAlignment) {
Rafael Espindolabb9a71c2015-05-26 15:07:25 +0000416 getAssembler().registerSection(*Section);
Daniel Dunbar42a39d02009-08-28 05:49:21 +0000417
418 // The symbol may not be present, which only creates the section.
419 if (!Symbol)
420 return;
421
Eric Christopher42ae4592013-08-07 21:13:01 +0000422 // On darwin all virtual sections have zerofill type.
423 assert(Section->isVirtualSection() && "Section does not have zerofill type!");
Daniel Dunbar42a39d02009-08-28 05:49:21 +0000424
425 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
426
Rafael Espindolab5d316b2015-05-29 20:21:02 +0000427 getAssembler().registerSymbol(*Symbol);
Daniel Dunbar42a39d02009-08-28 05:49:21 +0000428
Daniel Dunbar51402b72010-05-12 22:51:27 +0000429 // Emit an align fragment if necessary.
430 if (ByteAlignment != 1)
Rafael Espindolabb9a71c2015-05-26 15:07:25 +0000431 new MCAlignFragment(ByteAlignment, 0, 0, ByteAlignment, Section);
Daniel Dunbar51402b72010-05-12 22:51:27 +0000432
Rafael Espindola1a7e8b42016-01-19 16:57:08 +0000433 MCFragment *F = new MCFillFragment(0, Size, Section);
Rafael Espindola4d37b2a2015-05-29 21:45:01 +0000434 Symbol->setFragment(F);
Daniel Dunbar42a39d02009-08-28 05:49:21 +0000435
Daniel Dunbar42a39d02009-08-28 05:49:21 +0000436 // Update the maximum alignment on the zero fill section if necessary.
Rafael Espindola967d6a62015-05-21 21:02:35 +0000437 if (ByteAlignment > Section->getAlignment())
438 Section->setAlignment(ByteAlignment);
Daniel Dunbar3016db32009-08-21 09:11:24 +0000439}
440
Eric Christopher09d47032010-05-21 23:03:53 +0000441// This should always be called with the thread local bss section. Like the
442// .zerofill directive this doesn't actually switch sections on us.
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000443void MCMachOStreamer::EmitTBSSSymbol(MCSection *Section, MCSymbol *Symbol,
Eric Christopherfeedc902010-05-18 21:26:41 +0000444 uint64_t Size, unsigned ByteAlignment) {
445 EmitZerofill(Section, Symbol, Size, ByteAlignment);
Eric Christopher9fb6bb02010-05-14 01:50:28 +0000446}
447
David Woodhouse6f3c73f2014-01-28 23:12:49 +0000448void MCMachOStreamer::EmitInstToData(const MCInst &Inst,
449 const MCSubtargetInfo &STI) {
Daniel Dunbar9d40ef12010-05-26 20:37:00 +0000450 MCDataFragment *DF = getOrCreateDataFragment();
451
452 SmallVector<MCFixup, 4> Fixups;
453 SmallString<256> Code;
454 raw_svector_ostream VecOS(Code);
Jim Grosbach91df21f2015-05-15 19:13:16 +0000455 getAssembler().getEmitter().encodeInstruction(Inst, VecOS, Fixups, STI);
Daniel Dunbar9d40ef12010-05-26 20:37:00 +0000456
457 // Add the fixups and data.
Craig Topper1129a002015-10-14 04:36:00 +0000458 for (MCFixup &Fixup : Fixups) {
459 Fixup.setOffset(Fixup.getOffset() + DF->getContents().size());
460 DF->getFixups().push_back(Fixup);
Daniel Dunbar9d40ef12010-05-26 20:37:00 +0000461 }
462 DF->getContents().append(Code.begin(), Code.end());
463}
464
Rafael Espindola07082092012-01-07 03:13:18 +0000465void MCMachOStreamer::FinishImpl() {
Rafael Espindola7f4ccce2014-05-12 13:30:10 +0000466 EmitFrames(&getAssembler().getBackend());
Rafael Espindolafc822362011-05-01 15:44:13 +0000467
Daniel Dunbarede8e6d2010-06-16 20:04:32 +0000468 // We have to set the fragment atom associations so we can relax properly for
469 // Mach-O.
470
471 // First, scan the symbol table to build a lookup table from fragments to
472 // defining symbols.
Duncan P. N. Exon Smith92a699c2015-05-20 20:18:16 +0000473 DenseMap<const MCFragment *, const MCSymbol *> DefiningSymbolMap;
Duncan P. N. Exon Smithf48de1c2015-05-16 00:35:24 +0000474 for (const MCSymbol &Symbol : getAssembler().symbols()) {
Rafael Espindolae3a20f52015-10-05 12:07:05 +0000475 if (getAssembler().isSymbolLinkerVisible(Symbol) && Symbol.isInSection() &&
476 !Symbol.isVariable()) {
Daniel Dunbarede8e6d2010-06-16 20:04:32 +0000477 // An atom defining symbol should never be internal to a fragment.
Rafael Espindola14672502015-05-29 17:48:04 +0000478 assert(Symbol.getOffset() == 0 &&
479 "Invalid offset in atom defining symbol!");
Rafael Espindola4d37b2a2015-05-29 21:45:01 +0000480 DefiningSymbolMap[Symbol.getFragment()] = &Symbol;
Daniel Dunbarede8e6d2010-06-16 20:04:32 +0000481 }
482 }
483
484 // Set the fragment atom associations by tracking the last seen atom defining
485 // symbol.
Craig Topper1129a002015-10-14 04:36:00 +0000486 for (MCSection &Sec : getAssembler()) {
Duncan P. N. Exon Smith09bfa582015-05-16 00:48:58 +0000487 const MCSymbol *CurrentAtom = nullptr;
Craig Topper1129a002015-10-14 04:36:00 +0000488 for (MCFragment &Frag : Sec) {
489 if (const MCSymbol *Symbol = DefiningSymbolMap.lookup(&Frag))
Duncan P. N. Exon Smith92a699c2015-05-20 20:18:16 +0000490 CurrentAtom = Symbol;
Craig Topper1129a002015-10-14 04:36:00 +0000491 Frag.setAtom(CurrentAtom);
Daniel Dunbarede8e6d2010-06-16 20:04:32 +0000492 }
493 }
494
Rafael Espindola07082092012-01-07 03:13:18 +0000495 this->MCObjectStreamer::FinishImpl();
Daniel Dunbarede8e6d2010-06-16 20:04:32 +0000496}
497
Evan Cheng5928e692011-07-25 23:24:55 +0000498MCStreamer *llvm::createMachOStreamer(MCContext &Context, MCAsmBackend &MAB,
Rafael Espindola5560a4c2015-04-14 22:14:34 +0000499 raw_pwrite_stream &OS, MCCodeEmitter *CE,
Rafael Espindola36a15cb2015-03-20 20:00:01 +0000500 bool RelaxAll, bool DWARFMustBeAtTheEnd,
Tim Northoverc3988b42014-03-29 07:05:06 +0000501 bool LabelSections) {
Rafael Espindola36a15cb2015-03-20 20:00:01 +0000502 MCMachOStreamer *S = new MCMachOStreamer(Context, MAB, OS, CE,
503 DWARFMustBeAtTheEnd, LabelSections);
Steven Wu99272062015-08-05 15:36:38 +0000504 const Triple &TT = Context.getObjectFileInfo()->getTargetTriple();
505 if (TT.isOSDarwin()) {
506 unsigned Major, Minor, Update;
507 TT.getOSVersion(Major, Minor, Update);
508 // If there is a version specified, Major will be non-zero.
Tim Northover2d4d1612015-10-28 22:36:05 +0000509 if (Major) {
510 MCVersionMinType VersionType;
511 if (TT.isWatchOS())
512 VersionType = MCVM_WatchOSVersionMin;
513 else if (TT.isTvOS())
514 VersionType = MCVM_TvOSVersionMin;
515 else if (TT.isMacOSX())
516 VersionType = MCVM_OSXVersionMin;
517 else {
518 assert(TT.isiOS() && "Must only be iOS platform left");
519 VersionType = MCVM_IOSVersionMin;
520 }
521 S->EmitVersionMin(VersionType, Major, Minor, Update);
522 }
Steven Wu99272062015-08-05 15:36:38 +0000523 }
Daniel Dunbard821f4a2010-03-25 22:49:09 +0000524 if (RelaxAll)
525 S->getAssembler().setRelaxAll(true);
526 return S;
Daniel Dunbar3016db32009-08-21 09:11:24 +0000527}