blob: b30317e74672bb3198a1f6aa8efad4e61411fcf9 [file] [log] [blame]
Eugene Zelenkof31871c2017-02-07 23:02:00 +00001//===- MCMachOStreamer.cpp - MachO Streamer -------------------------------===//
Daniel Dunbarfb4a6b32009-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
Tim Northoverc5d592d2014-03-29 07:05:06 +000010#include "llvm/ADT/DenseMap.h"
Eugene Zelenkof31871c2017-02-07 23:02:00 +000011#include "llvm/ADT/SmallString.h"
Tim Northoverc5d592d2014-03-29 07:05:06 +000012#include "llvm/ADT/SmallVector.h"
Eugene Zelenkof31871c2017-02-07 23:02:00 +000013#include "llvm/ADT/StringRef.h"
14#include "llvm/ADT/Triple.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000015#include "llvm/MC/MCAsmBackend.h"
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +000016#include "llvm/MC/MCAssembler.h"
Daniel Dunbar4fac7492009-08-27 08:17:51 +000017#include "llvm/MC/MCCodeEmitter.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000018#include "llvm/MC/MCContext.h"
Eugene Zelenkof31871c2017-02-07 23:02:00 +000019#include "llvm/MC/MCDirectives.h"
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +000020#include "llvm/MC/MCExpr.h"
Eugene Zelenkof31871c2017-02-07 23:02:00 +000021#include "llvm/MC/MCFixup.h"
22#include "llvm/MC/MCFragment.h"
Daniel Dunbar4fac7492009-08-27 08:17:51 +000023#include "llvm/MC/MCInst.h"
Tim Northover1330ee32014-03-29 07:34:53 +000024#include "llvm/MC/MCLinkerOptimizationHint.h"
Rafael Espindolae0fd5672014-01-23 22:49:25 +000025#include "llvm/MC/MCObjectFileInfo.h"
Daniel Dunbar8dc68ab2010-06-16 20:04:22 +000026#include "llvm/MC/MCObjectStreamer.h"
Peter Collingbourne17a98142018-05-18 18:26:45 +000027#include "llvm/MC/MCObjectWriter.h"
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +000028#include "llvm/MC/MCSection.h"
Kevin Enderbyb07ce602010-08-09 22:52:14 +000029#include "llvm/MC/MCSectionMachO.h"
Eugene Zelenkof31871c2017-02-07 23:02:00 +000030#include "llvm/MC/MCStreamer.h"
31#include "llvm/MC/MCSymbol.h"
Pete Cooperd3869ee2015-06-08 17:17:28 +000032#include "llvm/MC/MCSymbolMachO.h"
Lang Hames875b7562016-03-15 01:43:05 +000033#include "llvm/MC/MCValue.h"
Eugene Zelenkof31871c2017-02-07 23:02:00 +000034#include "llvm/Support/Casting.h"
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +000035#include "llvm/Support/ErrorHandling.h"
Eugene Zelenkof31871c2017-02-07 23:02:00 +000036#include "llvm/Support/TargetRegistry.h"
Chandler Carruthe3e43d92017-06-06 11:49:48 +000037#include "llvm/Support/raw_ostream.h"
Eugene Zelenkof31871c2017-02-07 23:02:00 +000038#include <cassert>
39#include <vector>
Daniel Dunbard8036fb2010-03-23 05:09:03 +000040
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +000041using namespace llvm;
42
43namespace {
44
Daniel Dunbar8dc68ab2010-06-16 20:04:22 +000045class MCMachOStreamer : public MCObjectStreamer {
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +000046private:
Tim Northoverc5d592d2014-03-29 07:05:06 +000047 /// LabelSections - true if each section change should emit a linker local
48 /// label for use in relocations for assembler local references. Obviates the
49 /// need for local relocations. False by default.
50 bool LabelSections;
51
Rafael Espindolad80979b2015-03-20 20:00:01 +000052 bool DWARFMustBeAtTheEnd;
53 bool CreatedADWARFSection;
54
Tim Northoverc5d592d2014-03-29 07:05:06 +000055 /// HasSectionLabel - map of which sections have already had a non-local
56 /// label emitted to them. Used so we don't emit extraneous linker local
57 /// labels in the middle of the section.
58 DenseMap<const MCSection*, bool> HasSectionLabel;
59
Craig Topper5747a142014-03-08 07:02:02 +000060 void EmitInstToData(const MCInst &Inst, const MCSubtargetInfo &STI) override;
Daniel Dunbar2ac0c452010-05-26 20:37:00 +000061
Jim Grosbach3e965312012-05-18 19:12:01 +000062 void EmitDataRegion(DataRegionData::KindTy Kind);
63 void EmitDataRegionEnd();
Tim Northoverc5d592d2014-03-29 07:05:06 +000064
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +000065public:
Lang Hames445025a2017-10-11 01:57:21 +000066 MCMachOStreamer(MCContext &Context, std::unique_ptr<MCAsmBackend> MAB,
Peter Collingbourne17a98142018-05-18 18:26:45 +000067 std::unique_ptr<MCObjectWriter> OW,
68 std::unique_ptr<MCCodeEmitter> Emitter,
Lang Hames445025a2017-10-11 01:57:21 +000069 bool DWARFMustBeAtTheEnd, bool label)
Peter Collingbourne17a98142018-05-18 18:26:45 +000070 : MCObjectStreamer(Context, std::move(MAB), std::move(OW),
71 std::move(Emitter)),
Lang Hames445025a2017-10-11 01:57:21 +000072 LabelSections(label), DWARFMustBeAtTheEnd(DWARFMustBeAtTheEnd),
73 CreatedADWARFSection(false) {}
Daniel Dunbarac2884a2010-03-25 22:49:09 +000074
Yaron Kerenc63035a2014-09-17 09:25:36 +000075 /// state management
76 void reset() override {
Justin Bogner51b567d2015-12-03 00:52:20 +000077 CreatedADWARFSection = false;
Yaron Kerenc63035a2014-09-17 09:25:36 +000078 HasSectionLabel.clear();
79 MCObjectStreamer::reset();
80 }
81
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +000082 /// @name MCStreamer Interface
83 /// @{
84
Rafael Espindola75219642015-05-21 19:20:38 +000085 void ChangeSection(MCSection *Sect, const MCExpr *Subsect) override;
Rafael Espindola940b0c02017-02-10 15:13:12 +000086 void EmitLabel(MCSymbol *Symbol, SMLoc Loc = SMLoc()) override;
Lang Hames875b7562016-03-15 01:43:05 +000087 void EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) override;
Craig Topper5747a142014-03-08 07:02:02 +000088 void EmitEHSymAttributes(const MCSymbol *Symbol, MCSymbol *EHSymbol) override;
89 void EmitAssemblerFlag(MCAssemblerFlag Flag) override;
90 void EmitLinkerOptions(ArrayRef<std::string> Options) override;
91 void EmitDataRegion(MCDataRegionType Kind) override;
Alex Lorenz6592c092018-12-14 01:14:10 +000092 void EmitVersionMin(MCVersionMinType Kind, unsigned Major, unsigned Minor,
93 unsigned Update, VersionTuple SDKVersion) override;
94 void EmitBuildVersion(unsigned Platform, unsigned Major, unsigned Minor,
95 unsigned Update, VersionTuple SDKVersion) override;
Craig Topper5747a142014-03-08 07:02:02 +000096 void EmitThumbFunc(MCSymbol *Func) override;
97 bool EmitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute) override;
98 void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) override;
99 void EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
100 unsigned ByteAlignment) override;
Eugene Zelenkof31871c2017-02-07 23:02:00 +0000101
Craig Topper5747a142014-03-08 07:02:02 +0000102 void EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size,
103 unsigned ByteAlignment) override;
Rafael Espindola75219642015-05-21 19:20:38 +0000104 void EmitZerofill(MCSection *Section, MCSymbol *Symbol = nullptr,
Francis Visoiu Mistriha5d70d82018-07-02 17:29:43 +0000105 uint64_t Size = 0, unsigned ByteAlignment = 0,
106 SMLoc Loc = SMLoc()) override;
Rafael Espindola75219642015-05-21 19:20:38 +0000107 void EmitTBSSSymbol(MCSection *Section, MCSymbol *Symbol, uint64_t Size,
Benjamin Kramera80ff262014-09-03 11:41:21 +0000108 unsigned ByteAlignment = 0) override;
Daniel Dunbar2ac0c452010-05-26 20:37:00 +0000109
Craig Topper5747a142014-03-08 07:02:02 +0000110 void EmitIdent(StringRef IdentString) override {
Rafael Espindolac7ce3e42013-10-16 01:05:45 +0000111 llvm_unreachable("macho doesn't support this directive");
Chris Lattnera6594fc2010-01-25 18:58:59 +0000112 }
Daniel Dunbar2ac0c452010-05-26 20:37:00 +0000113
Tim Northover1330ee32014-03-29 07:34:53 +0000114 void EmitLOHDirective(MCLOHType Kind, const MCLOHArgs &Args) override {
115 getAssembler().getLOHContainer().addDirective(Kind, Args);
116 }
117
Craig Topper5747a142014-03-08 07:02:02 +0000118 void FinishImpl() override;
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000119};
120
121} // end anonymous namespace.
122
Rafael Espindolad80979b2015-03-20 20:00:01 +0000123static bool canGoAfterDWARF(const MCSectionMachO &MSec) {
124 // These sections are created by the assembler itself after the end of
125 // the .s file.
126 StringRef SegName = MSec.getSegmentName();
127 StringRef SecName = MSec.getSectionName();
128
129 if (SegName == "__LD" && SecName == "__compact_unwind")
130 return true;
131
132 if (SegName == "__IMPORT") {
133 if (SecName == "__jump_table")
134 return true;
135
136 if (SecName == "__pointers")
137 return true;
138 }
139
140 if (SegName == "__TEXT" && SecName == "__eh_frame")
141 return true;
142
Matthias Braunc7e76542017-02-01 01:31:36 +0000143 if (SegName == "__DATA" && (SecName == "__nl_symbol_ptr" ||
144 SecName == "__thread_ptr"))
Rafael Espindolad80979b2015-03-20 20:00:01 +0000145 return true;
146
147 return false;
148}
149
Rafael Espindola75219642015-05-21 19:20:38 +0000150void MCMachOStreamer::ChangeSection(MCSection *Section,
Tim Northoverc5d592d2014-03-29 07:05:06 +0000151 const MCExpr *Subsection) {
152 // Change the section normally.
David Blaikie1e54c562017-03-16 00:43:19 +0000153 bool Created = changeSectionImpl(Section, Subsection);
Rafael Espindolad80979b2015-03-20 20:00:01 +0000154 const MCSectionMachO &MSec = *cast<MCSectionMachO>(Section);
155 StringRef SegName = MSec.getSegmentName();
156 if (SegName == "__DWARF")
157 CreatedADWARFSection = true;
158 else if (Created && DWARFMustBeAtTheEnd && !canGoAfterDWARF(MSec))
159 assert(!CreatedADWARFSection && "Creating regular section after DWARF");
160
Tim Northoverc5d592d2014-03-29 07:05:06 +0000161 // Output a linker-local symbol so we don't need section-relative local
162 // relocations. The linker hates us when we do that.
Rafael Espindola41cea412015-05-27 20:52:32 +0000163 if (LabelSections && !HasSectionLabel[Section] &&
164 !Section->getBeginSymbol()) {
Jim Grosbach19696da2015-05-18 18:43:14 +0000165 MCSymbol *Label = getContext().createLinkerPrivateTempSymbol();
Rafael Espindola41cea412015-05-27 20:52:32 +0000166 Section->setBeginSymbol(Label);
Tim Northoverc5d592d2014-03-29 07:05:06 +0000167 HasSectionLabel[Section] = true;
168 }
169}
170
Rafael Espindola8bca4102011-04-28 12:50:37 +0000171void MCMachOStreamer::EmitEHSymAttributes(const MCSymbol *Symbol,
172 MCSymbol *EHSymbol) {
Rafael Espindolaf00654b2015-05-29 20:21:02 +0000173 getAssembler().registerSymbol(*Symbol);
Rafael Espindolacfac75a2015-05-29 21:45:01 +0000174 if (Symbol->isExternal())
Rafael Espindola8bca4102011-04-28 12:50:37 +0000175 EmitSymbolAttribute(EHSymbol, MCSA_Global);
Pete Cooperd3869ee2015-06-08 17:17:28 +0000176 if (cast<MCSymbolMachO>(Symbol)->isWeakDefinition())
Rafael Espindola8bca4102011-04-28 12:50:37 +0000177 EmitSymbolAttribute(EHSymbol, MCSA_WeakDefinition);
Rafael Espindolacfac75a2015-05-29 21:45:01 +0000178 if (Symbol->isPrivateExtern())
Rafael Espindola97f7f4e2011-04-30 16:22:46 +0000179 EmitSymbolAttribute(EHSymbol, MCSA_PrivateExtern);
Rafael Espindola8bca4102011-04-28 12:50:37 +0000180}
181
Rafael Espindola940b0c02017-02-10 15:13:12 +0000182void MCMachOStreamer::EmitLabel(MCSymbol *Symbol, SMLoc Loc) {
Daniel Dunbara86de102010-06-16 20:04:32 +0000183 // We have to create a new fragment if this is an atom defining symbol,
184 // fragments cannot span atoms.
Rafael Espindolaea4afa92010-11-28 17:18:55 +0000185 if (getAssembler().isSymbolLinkerVisible(*Symbol))
Peter Collingbournedf39be62013-04-17 21:18:16 +0000186 insert(new MCDataFragment());
Daniel Dunbar071f73d2010-05-10 22:45:09 +0000187
Rafael Espindola940b0c02017-02-10 15:13:12 +0000188 MCObjectStreamer::EmitLabel(Symbol, Loc);
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000189
Daniel Dunbarb18d2dd2010-05-17 20:12:31 +0000190 // This causes the reference type flag to be cleared. Darwin 'as' was "trying"
191 // to clear the weak reference and weak definition bits too, but the
192 // implementation was buggy. For now we just try to match 'as', for
193 // diffability.
194 //
195 // FIXME: Cleanup this code, these bits should be emitted based on semantic
196 // properties, not on the order of definition, etc.
Pete Cooperd3869ee2015-06-08 17:17:28 +0000197 cast<MCSymbolMachO>(Symbol)->clearReferenceType();
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000198}
199
Lang Hames875b7562016-03-15 01:43:05 +0000200void MCMachOStreamer::EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) {
201 MCValue Res;
202
Lang Hames767c5c32016-03-15 04:20:49 +0000203 if (Value->evaluateAsRelocatable(Res, nullptr, nullptr)) {
204 if (const MCSymbolRefExpr *SymAExpr = Res.getSymA()) {
205 const MCSymbol &SymA = SymAExpr->getSymbol();
206 if (!Res.getSymB() && (SymA.getName() == "" || Res.getConstant() != 0))
207 cast<MCSymbolMachO>(Symbol)->setAltEntry();
208 }
209 }
Lang Hames875b7562016-03-15 01:43:05 +0000210 MCObjectStreamer::EmitAssignment(Symbol, Value);
211}
212
Jim Grosbach3e965312012-05-18 19:12:01 +0000213void MCMachOStreamer::EmitDataRegion(DataRegionData::KindTy Kind) {
214 // Create a temporary label to mark the start of the data region.
Jim Grosbach19696da2015-05-18 18:43:14 +0000215 MCSymbol *Start = getContext().createTempSymbol();
Jim Grosbach3e965312012-05-18 19:12:01 +0000216 EmitLabel(Start);
217 // Record the region for the object writer to use.
Craig Topper4266ae82014-04-13 04:57:38 +0000218 DataRegionData Data = { Kind, Start, nullptr };
Jim Grosbach3e965312012-05-18 19:12:01 +0000219 std::vector<DataRegionData> &Regions = getAssembler().getDataRegions();
220 Regions.push_back(Data);
221}
222
223void MCMachOStreamer::EmitDataRegionEnd() {
224 std::vector<DataRegionData> &Regions = getAssembler().getDataRegions();
Alexander Kornienkob4c62672015-01-15 11:41:30 +0000225 assert(!Regions.empty() && "Mismatched .end_data_region!");
Jim Grosbach3e965312012-05-18 19:12:01 +0000226 DataRegionData &Data = Regions.back();
Craig Topper4266ae82014-04-13 04:57:38 +0000227 assert(!Data.End && "Mismatched .end_data_region!");
Jim Grosbach3e965312012-05-18 19:12:01 +0000228 // Create a temporary label to mark the end of the data region.
Jim Grosbach19696da2015-05-18 18:43:14 +0000229 Data.End = getContext().createTempSymbol();
Jim Grosbach3e965312012-05-18 19:12:01 +0000230 EmitLabel(Data.End);
231}
232
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000233void MCMachOStreamer::EmitAssemblerFlag(MCAssemblerFlag Flag) {
Jim Grosbach5be6d2a2010-12-08 01:16:55 +0000234 // Let the target do whatever target specific stuff it needs to do.
Jim Grosbachec343382012-01-18 18:52:16 +0000235 getAssembler().getBackend().handleAssemblerFlag(Flag);
Jim Grosbach5be6d2a2010-12-08 01:16:55 +0000236 // Do any generic stuff we need to do.
Daniel Dunbar6009db42009-08-26 21:22:22 +0000237 switch (Flag) {
Jim Grosbachce792992010-11-05 22:08:08 +0000238 case MCAF_SyntaxUnified: return; // no-op here.
Evan Chengbd27f5a2011-07-27 00:38:12 +0000239 case MCAF_Code16: return; // Change parsing mode; no-op here.
240 case MCAF_Code32: return; // Change parsing mode; no-op here.
241 case MCAF_Code64: return; // Change parsing mode; no-op here.
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000242 case MCAF_SubsectionsViaSymbols:
Daniel Dunbar8dc68ab2010-06-16 20:04:22 +0000243 getAssembler().setSubsectionsViaSymbols(true);
Daniel Dunbar8c3eaf42009-08-28 07:08:47 +0000244 return;
Daniel Dunbar6009db42009-08-26 21:22:22 +0000245 }
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000246}
247
Daniel Dunbara94c3392013-01-18 01:26:07 +0000248void MCMachOStreamer::EmitLinkerOptions(ArrayRef<std::string> Options) {
249 getAssembler().getLinkerOptions().push_back(Options);
250}
251
Jim Grosbach3e965312012-05-18 19:12:01 +0000252void MCMachOStreamer::EmitDataRegion(MCDataRegionType Kind) {
253 switch (Kind) {
254 case MCDR_DataRegion:
255 EmitDataRegion(DataRegionData::Data);
256 return;
257 case MCDR_DataRegionJT8:
258 EmitDataRegion(DataRegionData::JumpTable8);
259 return;
260 case MCDR_DataRegionJT16:
261 EmitDataRegion(DataRegionData::JumpTable16);
262 return;
263 case MCDR_DataRegionJT32:
264 EmitDataRegion(DataRegionData::JumpTable32);
265 return;
266 case MCDR_DataRegionEnd:
267 EmitDataRegionEnd();
268 return;
269 }
270}
271
Jim Grosbachd55fc3f2014-03-18 22:09:05 +0000272void MCMachOStreamer::EmitVersionMin(MCVersionMinType Kind, unsigned Major,
Alex Lorenz6592c092018-12-14 01:14:10 +0000273 unsigned Minor, unsigned Update,
274 VersionTuple SDKVersion) {
275 getAssembler().setVersionMin(Kind, Major, Minor, Update, SDKVersion);
Matthias Braun177a4fc2017-12-14 00:12:46 +0000276}
277
278void MCMachOStreamer::EmitBuildVersion(unsigned Platform, unsigned Major,
Alex Lorenz6592c092018-12-14 01:14:10 +0000279 unsigned Minor, unsigned Update,
280 VersionTuple SDKVersion) {
Matthias Braun177a4fc2017-12-14 00:12:46 +0000281 getAssembler().setBuildVersion((MachO::PlatformType)Platform, Major, Minor,
Alex Lorenz6592c092018-12-14 01:14:10 +0000282 Update, SDKVersion);
Jim Grosbachd55fc3f2014-03-18 22:09:05 +0000283}
284
Daniel Dunbarb2624ed2010-12-29 14:14:06 +0000285void MCMachOStreamer::EmitThumbFunc(MCSymbol *Symbol) {
Jim Grosbach47500202010-12-14 18:46:57 +0000286 // Remember that the function is a thumb function. Fixup and relocation
287 // values will need adjusted.
Daniel Dunbarb2624ed2010-12-29 14:14:06 +0000288 getAssembler().setIsThumbFunc(Symbol);
Pete Cooperd3869ee2015-06-08 17:17:28 +0000289 cast<MCSymbolMachO>(Symbol)->setThumbFunc();
Jim Grosbachce792992010-11-05 22:08:08 +0000290}
291
Pete Cooperd3869ee2015-06-08 17:17:28 +0000292bool MCMachOStreamer::EmitSymbolAttribute(MCSymbol *Sym,
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000293 MCSymbolAttr Attribute) {
Pete Cooperd3869ee2015-06-08 17:17:28 +0000294 MCSymbolMachO *Symbol = cast<MCSymbolMachO>(Sym);
295
Daniel Dunbar0c7761b2009-08-24 11:56:58 +0000296 // Indirect symbols are handled differently, to match how 'as' handles
297 // them. This makes writing matching .o files easier.
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000298 if (Attribute == MCSA_IndirectSymbol) {
Daniel Dunbarad7c3d52009-08-26 00:18:21 +0000299 // Note that we intentionally cannot use the symbol data here; this is
300 // important for matching the string table that 'as' generates.
Daniel Dunbar0c7761b2009-08-24 11:56:58 +0000301 IndirectSymbolData ISD;
302 ISD.Symbol = Symbol;
Rafael Espindola792e1582015-05-27 21:04:14 +0000303 ISD.Section = getCurrentSectionOnly();
Daniel Dunbar8dc68ab2010-06-16 20:04:22 +0000304 getAssembler().getIndirectSymbols().push_back(ISD);
Saleem Abdulrasool1c9cd022013-08-09 01:52:03 +0000305 return true;
Daniel Dunbar0c7761b2009-08-24 11:56:58 +0000306 }
307
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000308 // Adding a symbol attribute always introduces the symbol, note that an
Rafael Espindolaf00654b2015-05-29 20:21:02 +0000309 // important side effect of calling registerSymbol here is to register
Daniel Dunbar46836a72010-03-10 20:58:29 +0000310 // the symbol with the assembler.
Rafael Espindolaf00654b2015-05-29 20:21:02 +0000311 getAssembler().registerSymbol(*Symbol);
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000312
313 // The implementation of symbol attributes is designed to match 'as', but it
314 // leaves much to desired. It doesn't really make sense to arbitrarily add and
315 // remove flags, but 'as' allows this (in particular, see .desc).
316 //
317 // In the future it might be worth trying to make these operations more well
318 // defined.
Daniel Dunbar3edd9bb2009-08-22 11:41:10 +0000319 switch (Attribute) {
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000320 case MCSA_Invalid:
Chris Lattnered0ab152010-01-25 18:30:45 +0000321 case MCSA_ELF_TypeFunction:
322 case MCSA_ELF_TypeIndFunction:
323 case MCSA_ELF_TypeObject:
324 case MCSA_ELF_TypeTLS:
325 case MCSA_ELF_TypeCommon:
326 case MCSA_ELF_TypeNoType:
Rafael Espindolae13a0ff2010-11-13 04:51:02 +0000327 case MCSA_ELF_TypeGnuUniqueObject:
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000328 case MCSA_Hidden:
Chandler Carruth8bcf9492011-07-25 21:21:08 +0000329 case MCSA_IndirectSymbol:
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000330 case MCSA_Internal:
331 case MCSA_Protected:
332 case MCSA_Weak:
333 case MCSA_Local:
Saleem Abdulrasool1c9cd022013-08-09 01:52:03 +0000334 return false;
Daniel Dunbar3edd9bb2009-08-22 11:41:10 +0000335
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000336 case MCSA_Global:
Rafael Espindolacfac75a2015-05-29 21:45:01 +0000337 Symbol->setExternal(true);
Daniel Dunbarb18d2dd2010-05-17 20:12:31 +0000338 // This effectively clears the undefined lazy bit, in Darwin 'as', although
339 // it isn't very consistent because it implements this as part of symbol
340 // lookup.
341 //
342 // FIXME: Cleanup this code, these bits should be emitted based on semantic
343 // properties, not on the order of definition, etc.
Pete Cooperd3869ee2015-06-08 17:17:28 +0000344 Symbol->setReferenceTypeUndefinedLazy(false);
Daniel Dunbar3edd9bb2009-08-22 11:41:10 +0000345 break;
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000346
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000347 case MCSA_LazyReference:
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000348 // FIXME: This requires -dynamic.
Pete Cooperd3869ee2015-06-08 17:17:28 +0000349 Symbol->setNoDeadStrip();
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000350 if (Symbol->isUndefined())
Pete Cooperd3869ee2015-06-08 17:17:28 +0000351 Symbol->setReferenceTypeUndefinedLazy(true);
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000352 break;
353
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000354 // Since .reference sets the no dead strip bit, it is equivalent to
355 // .no_dead_strip in practice.
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000356 case MCSA_Reference:
357 case MCSA_NoDeadStrip:
Pete Cooperd3869ee2015-06-08 17:17:28 +0000358 Symbol->setNoDeadStrip();
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000359 break;
360
Kevin Enderbye8e98d72010-11-19 18:39:33 +0000361 case MCSA_SymbolResolver:
Pete Cooperd3869ee2015-06-08 17:17:28 +0000362 Symbol->setSymbolResolver();
Kevin Enderbye8e98d72010-11-19 18:39:33 +0000363 break;
364
Lang Hames875b7562016-03-15 01:43:05 +0000365 case MCSA_AltEntry:
366 Symbol->setAltEntry();
367 break;
368
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000369 case MCSA_PrivateExtern:
Rafael Espindolacfac75a2015-05-29 21:45:01 +0000370 Symbol->setExternal(true);
371 Symbol->setPrivateExtern(true);
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000372 break;
373
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000374 case MCSA_WeakReference:
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000375 // FIXME: This requires -dynamic.
376 if (Symbol->isUndefined())
Pete Cooperd3869ee2015-06-08 17:17:28 +0000377 Symbol->setWeakReference();
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000378 break;
379
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000380 case MCSA_WeakDefinition:
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000381 // FIXME: 'as' enforces that this is defined and global. The manual claims
382 // it has to be in a coalesced section, but this isn't enforced.
Pete Cooperd3869ee2015-06-08 17:17:28 +0000383 Symbol->setWeakDefinition();
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000384 break;
Kevin Enderbyf59cac52010-07-08 17:22:42 +0000385
386 case MCSA_WeakDefAutoPrivate:
Pete Cooperd3869ee2015-06-08 17:17:28 +0000387 Symbol->setWeakDefinition();
388 Symbol->setWeakReference();
Kevin Enderbyf59cac52010-07-08 17:22:42 +0000389 break;
Daniel Dunbar3edd9bb2009-08-22 11:41:10 +0000390 }
Saleem Abdulrasool1c9cd022013-08-09 01:52:03 +0000391
392 return true;
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000393}
394
395void MCMachOStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000396 // Encode the 'desc' value into the lowest implementation defined bits.
Rafael Espindolaf00654b2015-05-29 20:21:02 +0000397 getAssembler().registerSymbol(*Symbol);
Pete Cooperd3869ee2015-06-08 17:17:28 +0000398 cast<MCSymbolMachO>(Symbol)->setDesc(DescValue);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000399}
400
Chris Lattner9eb158d2010-01-23 07:47:02 +0000401void MCMachOStreamer::EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000402 unsigned ByteAlignment) {
Daniel Dunbar8f4d1462009-08-28 07:08:35 +0000403 // FIXME: Darwin 'as' does appear to allow redef of a .comm by itself.
404 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
405
Rafael Espindolaf00654b2015-05-29 20:21:02 +0000406 getAssembler().registerSymbol(*Symbol);
Rafael Espindolacfac75a2015-05-29 21:45:01 +0000407 Symbol->setExternal(true);
Rafael Espindola82fdcc02015-05-29 17:48:04 +0000408 Symbol->setCommon(Size, ByteAlignment);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000409}
410
Benjamin Kramer39646d92012-09-07 17:25:13 +0000411void MCMachOStreamer::EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size,
412 unsigned ByteAlignment) {
413 // '.lcomm' is equivalent to '.zerofill'.
Rafael Espindolae0fd5672014-01-23 22:49:25 +0000414 return EmitZerofill(getContext().getObjectFileInfo()->getDataBSSSection(),
Benjamin Kramer39646d92012-09-07 17:25:13 +0000415 Symbol, Size, ByteAlignment);
416}
417
Rafael Espindola75219642015-05-21 19:20:38 +0000418void MCMachOStreamer::EmitZerofill(MCSection *Section, MCSymbol *Symbol,
Francis Visoiu Mistriha5d70d82018-07-02 17:29:43 +0000419 uint64_t Size, unsigned ByteAlignment,
420 SMLoc Loc) {
421 // On darwin all virtual sections have zerofill type. Disallow the usage of
422 // .zerofill in non-virtual functions. If something similar is needed, use
423 // .space or .zero.
424 if (!Section->isVirtualSection()) {
425 getContext().reportError(
426 Loc, "The usage of .zerofill is restricted to sections of "
427 "ZEROFILL type. Use .zero or .space instead.");
428 return; // Early returning here shouldn't harm. EmitZeros should work on any
429 // section.
430 }
Daniel Dunbard5a8e982009-08-28 05:49:21 +0000431
Rafael Espindola8b2b7db2018-01-09 21:55:10 +0000432 PushSection();
433 SwitchSection(Section);
Daniel Dunbard5a8e982009-08-28 05:49:21 +0000434
Rafael Espindola8b2b7db2018-01-09 21:55:10 +0000435 // The symbol may not be present, which only creates the section.
436 if (Symbol) {
437 EmitValueToAlignment(ByteAlignment, 0, 1, 0);
438 EmitLabel(Symbol);
439 EmitZeros(Size);
440 }
441 PopSection();
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000442}
443
Eric Christopher070c1ab2010-05-21 23:03:53 +0000444// This should always be called with the thread local bss section. Like the
445// .zerofill directive this doesn't actually switch sections on us.
Rafael Espindola75219642015-05-21 19:20:38 +0000446void MCMachOStreamer::EmitTBSSSymbol(MCSection *Section, MCSymbol *Symbol,
Eric Christopher011c3f12010-05-18 21:26:41 +0000447 uint64_t Size, unsigned ByteAlignment) {
448 EmitZerofill(Section, Symbol, Size, ByteAlignment);
Eric Christopher482eba02010-05-14 01:50:28 +0000449}
450
David Woodhoused5d381b2014-01-28 23:12:49 +0000451void MCMachOStreamer::EmitInstToData(const MCInst &Inst,
452 const MCSubtargetInfo &STI) {
Daniel Dunbar2ac0c452010-05-26 20:37:00 +0000453 MCDataFragment *DF = getOrCreateDataFragment();
454
455 SmallVector<MCFixup, 4> Fixups;
456 SmallString<256> Code;
457 raw_svector_ostream VecOS(Code);
Jim Grosbach251a66e2015-05-15 19:13:16 +0000458 getAssembler().getEmitter().encodeInstruction(Inst, VecOS, Fixups, STI);
Daniel Dunbar2ac0c452010-05-26 20:37:00 +0000459
460 // Add the fixups and data.
Craig Topper3f45c272015-10-14 04:36:00 +0000461 for (MCFixup &Fixup : Fixups) {
462 Fixup.setOffset(Fixup.getOffset() + DF->getContents().size());
463 DF->getFixups().push_back(Fixup);
Daniel Dunbar2ac0c452010-05-26 20:37:00 +0000464 }
Peter Smithe2b2a912018-06-06 09:40:06 +0000465 DF->setHasInstructions(STI);
Daniel Dunbar2ac0c452010-05-26 20:37:00 +0000466 DF->getContents().append(Code.begin(), Code.end());
467}
468
Rafael Espindola99b42372012-01-07 03:13:18 +0000469void MCMachOStreamer::FinishImpl() {
Rafael Espindola044302d2014-05-12 13:30:10 +0000470 EmitFrames(&getAssembler().getBackend());
Rafael Espindola450a5a12011-05-01 15:44:13 +0000471
Daniel Dunbara86de102010-06-16 20:04:32 +0000472 // We have to set the fragment atom associations so we can relax properly for
473 // Mach-O.
Sam Clegg80e49162018-06-14 17:11:19 +0000474
475 // First, scan the symbol table to build a lookup table from fragments to
476 // defining symbols.
477 DenseMap<const MCFragment *, const MCSymbol *> DefiningSymbolMap;
478 for (const MCSymbol &Symbol : getAssembler().symbols()) {
479 if (getAssembler().isSymbolLinkerVisible(Symbol) && Symbol.isInSection() &&
480 !Symbol.isVariable()) {
481 // An atom defining symbol should never be internal to a fragment.
482 assert(Symbol.getOffset() == 0 &&
483 "Invalid offset in atom defining symbol!");
484 DefiningSymbolMap[Symbol.getFragment()] = &Symbol;
485 }
486 }
487
488 // Set the fragment atom associations by tracking the last seen atom defining
489 // symbol.
490 for (MCSection &Sec : getAssembler()) {
491 const MCSymbol *CurrentAtom = nullptr;
492 for (MCFragment &Frag : Sec) {
493 if (const MCSymbol *Symbol = DefiningSymbolMap.lookup(&Frag))
494 CurrentAtom = Symbol;
495 Frag.setAtom(CurrentAtom);
496 }
497 }
Daniel Dunbara86de102010-06-16 20:04:32 +0000498
Rafael Espindola99b42372012-01-07 03:13:18 +0000499 this->MCObjectStreamer::FinishImpl();
Daniel Dunbara86de102010-06-16 20:04:32 +0000500}
501
Lang Hames445025a2017-10-11 01:57:21 +0000502MCStreamer *llvm::createMachOStreamer(MCContext &Context,
503 std::unique_ptr<MCAsmBackend> &&MAB,
Peter Collingbourne17a98142018-05-18 18:26:45 +0000504 std::unique_ptr<MCObjectWriter> &&OW,
Lang Hames806f68b2017-10-11 23:34:47 +0000505 std::unique_ptr<MCCodeEmitter> &&CE,
Rafael Espindolad80979b2015-03-20 20:00:01 +0000506 bool RelaxAll, bool DWARFMustBeAtTheEnd,
Tim Northoverc5d592d2014-03-29 07:05:06 +0000507 bool LabelSections) {
Lang Hames806f68b2017-10-11 23:34:47 +0000508 MCMachOStreamer *S =
Peter Collingbourne17a98142018-05-18 18:26:45 +0000509 new MCMachOStreamer(Context, std::move(MAB), std::move(OW), std::move(CE),
Lang Hames806f68b2017-10-11 23:34:47 +0000510 DWARFMustBeAtTheEnd, LabelSections);
Matthias Braun34045e12017-12-14 03:59:24 +0000511 const Triple &Target = Context.getObjectFileInfo()->getTargetTriple();
Alex Lorenz6592c092018-12-14 01:14:10 +0000512 S->EmitVersionForTarget(Target, Context.getObjectFileInfo()->getSDKVersion());
Daniel Dunbarac2884a2010-03-25 22:49:09 +0000513 if (RelaxAll)
514 S->getAssembler().setRelaxAll(true);
515 return S;
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000516}