blob: 50ee08fa1f9eec742fb6315a88d1bb4ebfbf56ce [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"
Kevin Enderbye46564a2010-09-30 16:52:03 +000026#include "llvm/Support/Dwarf.h"
Daniel Dunbar3016db32009-08-21 09:11:24 +000027#include "llvm/Support/ErrorHandling.h"
Rafael Espindolacd584a82015-03-19 01:50:16 +000028#include "llvm/Support/TargetRegistry.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
Rafael Espindola36a15cb2015-03-20 20:00:01 +000042 bool DWARFMustBeAtTheEnd;
43 bool CreatedADWARFSection;
44
Tim Northoverc3988b42014-03-29 07:05:06 +000045 /// HasSectionLabel - map of which sections have already had a non-local
46 /// label emitted to them. Used so we don't emit extraneous linker local
47 /// labels in the middle of the section.
48 DenseMap<const MCSection*, bool> HasSectionLabel;
49
Craig Topper34a61bc2014-03-08 07:02:02 +000050 void EmitInstToData(const MCInst &Inst, const MCSubtargetInfo &STI) override;
Daniel Dunbar9d40ef12010-05-26 20:37:00 +000051
Jim Grosbach4b63d2a2012-05-18 19:12:01 +000052 void EmitDataRegion(DataRegionData::KindTy Kind);
53 void EmitDataRegionEnd();
Tim Northoverc3988b42014-03-29 07:05:06 +000054
Daniel Dunbar3016db32009-08-21 09:11:24 +000055public:
Rafael Espindola5560a4c2015-04-14 22:14:34 +000056 MCMachOStreamer(MCContext &Context, MCAsmBackend &MAB, raw_pwrite_stream &OS,
Rafael Espindola36a15cb2015-03-20 20:00:01 +000057 MCCodeEmitter *Emitter, bool DWARFMustBeAtTheEnd, bool label)
58 : MCObjectStreamer(Context, MAB, OS, Emitter), LabelSections(label),
59 DWARFMustBeAtTheEnd(DWARFMustBeAtTheEnd), CreatedADWARFSection(false) {}
Daniel Dunbard821f4a2010-03-25 22:49:09 +000060
Yaron Keren559b47d2014-09-17 09:25:36 +000061 /// state management
62 void reset() override {
63 HasSectionLabel.clear();
64 MCObjectStreamer::reset();
65 }
66
Daniel Dunbar3016db32009-08-21 09:11:24 +000067 /// @name MCStreamer Interface
68 /// @{
69
Rafael Espindola0709a7b2015-05-21 19:20:38 +000070 void ChangeSection(MCSection *Sect, const MCExpr *Subsect) override;
Craig Topper34a61bc2014-03-08 07:02:02 +000071 void EmitLabel(MCSymbol *Symbol) override;
Craig Topper34a61bc2014-03-08 07:02:02 +000072 void EmitEHSymAttributes(const MCSymbol *Symbol, MCSymbol *EHSymbol) override;
73 void EmitAssemblerFlag(MCAssemblerFlag Flag) override;
74 void EmitLinkerOptions(ArrayRef<std::string> Options) override;
75 void EmitDataRegion(MCDataRegionType Kind) override;
Jim Grosbach448334a2014-03-18 22:09:05 +000076 void EmitVersionMin(MCVersionMinType Kind, unsigned Major,
77 unsigned Minor, unsigned Update) override;
Craig Topper34a61bc2014-03-08 07:02:02 +000078 void EmitThumbFunc(MCSymbol *Func) override;
79 bool EmitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute) override;
80 void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) override;
81 void EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
82 unsigned ByteAlignment) override;
83 void BeginCOFFSymbolDef(const MCSymbol *Symbol) 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 EmitCOFFSymbolStorageClass(int StorageClass) 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 EmitCOFFSymbolType(int Type) 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 EndCOFFSymbolDef() 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 EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size,
96 unsigned ByteAlignment) override;
Rafael Espindola0709a7b2015-05-21 19:20:38 +000097 void EmitZerofill(MCSection *Section, MCSymbol *Symbol = nullptr,
Craig Topper34a61bc2014-03-08 07:02:02 +000098 uint64_t Size = 0, unsigned ByteAlignment = 0) override;
Rafael Espindola0709a7b2015-05-21 19:20:38 +000099 void EmitTBSSSymbol(MCSection *Section, MCSymbol *Symbol, uint64_t Size,
Benjamin Kramer8c90fd72014-09-03 11:41:21 +0000100 unsigned ByteAlignment = 0) override;
Daniel Dunbar9d40ef12010-05-26 20:37:00 +0000101
Craig Topper34a61bc2014-03-08 07:02:02 +0000102 void EmitFileDirective(StringRef Filename) override {
Daniel Dunbar6b4391a2010-07-19 20:44:20 +0000103 // FIXME: Just ignore the .file; it isn't important enough to fail the
104 // entire assembly.
105
Rafael Espindola5645bad2013-10-16 01:05:45 +0000106 // report_fatal_error("unsupported directive: '.file'");
107 }
108
Craig Topper34a61bc2014-03-08 07:02:02 +0000109 void EmitIdent(StringRef IdentString) override {
Rafael Espindola5645bad2013-10-16 01:05:45 +0000110 llvm_unreachable("macho doesn't support this directive");
Chris Lattner601ef332010-01-25 18:58:59 +0000111 }
Daniel Dunbar9d40ef12010-05-26 20:37:00 +0000112
Tim Northover53d32512014-03-29 07:34:53 +0000113 void EmitLOHDirective(MCLOHType Kind, const MCLOHArgs &Args) override {
114 getAssembler().getLOHContainer().addDirective(Kind, Args);
115 }
116
Craig Topper34a61bc2014-03-08 07:02:02 +0000117 void FinishImpl() override;
Daniel Dunbar3016db32009-08-21 09:11:24 +0000118};
119
120} // end anonymous namespace.
121
Rafael Espindola36a15cb2015-03-20 20:00:01 +0000122static bool canGoAfterDWARF(const MCSectionMachO &MSec) {
123 // These sections are created by the assembler itself after the end of
124 // the .s file.
125 StringRef SegName = MSec.getSegmentName();
126 StringRef SecName = MSec.getSectionName();
127
128 if (SegName == "__LD" && SecName == "__compact_unwind")
129 return true;
130
131 if (SegName == "__IMPORT") {
132 if (SecName == "__jump_table")
133 return true;
134
135 if (SecName == "__pointers")
136 return true;
137 }
138
139 if (SegName == "__TEXT" && SecName == "__eh_frame")
140 return true;
141
142 if (SegName == "__DATA" && SecName == "__nl_symbol_ptr")
143 return true;
144
145 return false;
146}
147
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000148void MCMachOStreamer::ChangeSection(MCSection *Section,
Tim Northoverc3988b42014-03-29 07:05:06 +0000149 const MCExpr *Subsection) {
150 // Change the section normally.
Rafael Espindola36a15cb2015-03-20 20:00:01 +0000151 bool Created = MCObjectStreamer::changeSectionImpl(Section, Subsection);
152 const MCSectionMachO &MSec = *cast<MCSectionMachO>(Section);
153 StringRef SegName = MSec.getSegmentName();
154 if (SegName == "__DWARF")
155 CreatedADWARFSection = true;
156 else if (Created && DWARFMustBeAtTheEnd && !canGoAfterDWARF(MSec))
157 assert(!CreatedADWARFSection && "Creating regular section after DWARF");
158
Tim Northoverc3988b42014-03-29 07:05:06 +0000159 // Output a linker-local symbol so we don't need section-relative local
160 // relocations. The linker hates us when we do that.
Rafael Espindola2f9bdd82015-05-27 20:52:32 +0000161 if (LabelSections && !HasSectionLabel[Section] &&
162 !Section->getBeginSymbol()) {
Jim Grosbach6f482002015-05-18 18:43:14 +0000163 MCSymbol *Label = getContext().createLinkerPrivateTempSymbol();
Rafael Espindola2f9bdd82015-05-27 20:52:32 +0000164 Section->setBeginSymbol(Label);
Tim Northoverc3988b42014-03-29 07:05:06 +0000165 HasSectionLabel[Section] = true;
166 }
167}
168
Rafael Espindola349c3292011-04-28 12:50:37 +0000169void MCMachOStreamer::EmitEHSymAttributes(const MCSymbol *Symbol,
170 MCSymbol *EHSymbol) {
Rafael Espindolab5d316b2015-05-29 20:21:02 +0000171 getAssembler().registerSymbol(*Symbol);
Rafael Espindola4d37b2a2015-05-29 21:45:01 +0000172 if (Symbol->isExternal())
Rafael Espindola349c3292011-04-28 12:50:37 +0000173 EmitSymbolAttribute(EHSymbol, MCSA_Global);
Pete Cooper916f79e2015-06-08 17:17:28 +0000174 if (cast<MCSymbolMachO>(Symbol)->isWeakDefinition())
Rafael Espindola349c3292011-04-28 12:50:37 +0000175 EmitSymbolAttribute(EHSymbol, MCSA_WeakDefinition);
Rafael Espindola4d37b2a2015-05-29 21:45:01 +0000176 if (Symbol->isPrivateExtern())
Rafael Espindola0b474c22011-04-30 16:22:46 +0000177 EmitSymbolAttribute(EHSymbol, MCSA_PrivateExtern);
Rafael Espindola349c3292011-04-28 12:50:37 +0000178}
179
Daniel Dunbar3016db32009-08-21 09:11:24 +0000180void MCMachOStreamer::EmitLabel(MCSymbol *Symbol) {
Rafael Espindola16795802010-11-28 16:22:59 +0000181 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
Rafael Espindola16795802010-11-28 16:22:59 +0000182
Daniel Dunbarede8e6d2010-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 Espindolae5b74152010-11-28 17:18:55 +0000185 if (getAssembler().isSymbolLinkerVisible(*Symbol))
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000186 insert(new MCDataFragment());
Daniel Dunbaraadb2ca2010-05-10 22:45:09 +0000187
Rafael Espindolae5b74152010-11-28 17:18:55 +0000188 MCObjectStreamer::EmitLabel(Symbol);
Daniel Dunbar04a11582009-08-24 08:40:12 +0000189
Daniel Dunbar0211a962010-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 Cooper916f79e2015-06-08 17:17:28 +0000197 cast<MCSymbolMachO>(Symbol)->clearReferenceType();
Daniel Dunbar3016db32009-08-21 09:11:24 +0000198}
199
Jim Grosbach4b63d2a2012-05-18 19:12:01 +0000200void MCMachOStreamer::EmitDataRegion(DataRegionData::KindTy Kind) {
Jim Grosbach745c52d2012-10-01 22:20:54 +0000201 if (!getAssembler().getBackend().hasDataInCodeSupport())
202 return;
Jim Grosbach4b63d2a2012-05-18 19:12:01 +0000203 // Create a temporary label to mark the start of the data region.
Jim Grosbach6f482002015-05-18 18:43:14 +0000204 MCSymbol *Start = getContext().createTempSymbol();
Jim Grosbach4b63d2a2012-05-18 19:12:01 +0000205 EmitLabel(Start);
206 // Record the region for the object writer to use.
Craig Topperbb694de2014-04-13 04:57:38 +0000207 DataRegionData Data = { Kind, Start, nullptr };
Jim Grosbach4b63d2a2012-05-18 19:12:01 +0000208 std::vector<DataRegionData> &Regions = getAssembler().getDataRegions();
209 Regions.push_back(Data);
210}
211
212void MCMachOStreamer::EmitDataRegionEnd() {
Jim Grosbach745c52d2012-10-01 22:20:54 +0000213 if (!getAssembler().getBackend().hasDataInCodeSupport())
214 return;
Jim Grosbach4b63d2a2012-05-18 19:12:01 +0000215 std::vector<DataRegionData> &Regions = getAssembler().getDataRegions();
Alexander Kornienko8c0809c2015-01-15 11:41:30 +0000216 assert(!Regions.empty() && "Mismatched .end_data_region!");
Jim Grosbach4b63d2a2012-05-18 19:12:01 +0000217 DataRegionData &Data = Regions.back();
Craig Topperbb694de2014-04-13 04:57:38 +0000218 assert(!Data.End && "Mismatched .end_data_region!");
Jim Grosbach4b63d2a2012-05-18 19:12:01 +0000219 // Create a temporary label to mark the end of the data region.
Jim Grosbach6f482002015-05-18 18:43:14 +0000220 Data.End = getContext().createTempSymbol();
Jim Grosbach4b63d2a2012-05-18 19:12:01 +0000221 EmitLabel(Data.End);
222}
223
Chris Lattner685508c2010-01-23 06:39:22 +0000224void MCMachOStreamer::EmitAssemblerFlag(MCAssemblerFlag Flag) {
Jim Grosbach87055ed2010-12-08 01:16:55 +0000225 // Let the target do whatever target specific stuff it needs to do.
Jim Grosbachaba3de92012-01-18 18:52:16 +0000226 getAssembler().getBackend().handleAssemblerFlag(Flag);
Jim Grosbach87055ed2010-12-08 01:16:55 +0000227 // Do any generic stuff we need to do.
Daniel Dunbare2697732009-08-26 21:22:22 +0000228 switch (Flag) {
Jim Grosbach5a2c68d2010-11-05 22:08:08 +0000229 case MCAF_SyntaxUnified: return; // no-op here.
Evan Cheng481ebb02011-07-27 00:38:12 +0000230 case MCAF_Code16: return; // Change parsing mode; no-op here.
231 case MCAF_Code32: return; // Change parsing mode; no-op here.
232 case MCAF_Code64: return; // Change parsing mode; no-op here.
Chris Lattner685508c2010-01-23 06:39:22 +0000233 case MCAF_SubsectionsViaSymbols:
Daniel Dunbar8a3c9d92010-06-16 20:04:22 +0000234 getAssembler().setSubsectionsViaSymbols(true);
Daniel Dunbar2a2459a2009-08-28 07:08:47 +0000235 return;
Daniel Dunbare2697732009-08-26 21:22:22 +0000236 }
Daniel Dunbar3016db32009-08-21 09:11:24 +0000237}
238
Daniel Dunbareec0f322013-01-18 01:26:07 +0000239void MCMachOStreamer::EmitLinkerOptions(ArrayRef<std::string> Options) {
240 getAssembler().getLinkerOptions().push_back(Options);
241}
242
Jim Grosbach4b63d2a2012-05-18 19:12:01 +0000243void MCMachOStreamer::EmitDataRegion(MCDataRegionType Kind) {
244 switch (Kind) {
245 case MCDR_DataRegion:
246 EmitDataRegion(DataRegionData::Data);
247 return;
248 case MCDR_DataRegionJT8:
249 EmitDataRegion(DataRegionData::JumpTable8);
250 return;
251 case MCDR_DataRegionJT16:
252 EmitDataRegion(DataRegionData::JumpTable16);
253 return;
254 case MCDR_DataRegionJT32:
255 EmitDataRegion(DataRegionData::JumpTable32);
256 return;
257 case MCDR_DataRegionEnd:
258 EmitDataRegionEnd();
259 return;
260 }
261}
262
Jim Grosbach448334a2014-03-18 22:09:05 +0000263void MCMachOStreamer::EmitVersionMin(MCVersionMinType Kind, unsigned Major,
264 unsigned Minor, unsigned Update) {
265 getAssembler().setVersionMinInfo(Kind, Major, Minor, Update);
266}
267
Daniel Dunbarab14a6f2010-12-29 14:14:06 +0000268void MCMachOStreamer::EmitThumbFunc(MCSymbol *Symbol) {
Jim Grosbach41955ff2010-12-14 18:46:57 +0000269 // Remember that the function is a thumb function. Fixup and relocation
270 // values will need adjusted.
Daniel Dunbarab14a6f2010-12-29 14:14:06 +0000271 getAssembler().setIsThumbFunc(Symbol);
Pete Cooper916f79e2015-06-08 17:17:28 +0000272 cast<MCSymbolMachO>(Symbol)->setThumbFunc();
Jim Grosbach5a2c68d2010-11-05 22:08:08 +0000273}
274
Pete Cooper916f79e2015-06-08 17:17:28 +0000275bool MCMachOStreamer::EmitSymbolAttribute(MCSymbol *Sym,
Chris Lattner685508c2010-01-23 06:39:22 +0000276 MCSymbolAttr Attribute) {
Pete Cooper916f79e2015-06-08 17:17:28 +0000277 MCSymbolMachO *Symbol = cast<MCSymbolMachO>(Sym);
278
Daniel Dunbar582d6102009-08-24 11:56:58 +0000279 // Indirect symbols are handled differently, to match how 'as' handles
280 // them. This makes writing matching .o files easier.
Chris Lattner685508c2010-01-23 06:39:22 +0000281 if (Attribute == MCSA_IndirectSymbol) {
Daniel Dunbarc2c0bf92009-08-26 00:18:21 +0000282 // Note that we intentionally cannot use the symbol data here; this is
283 // important for matching the string table that 'as' generates.
Daniel Dunbar582d6102009-08-24 11:56:58 +0000284 IndirectSymbolData ISD;
285 ISD.Symbol = Symbol;
Rafael Espindola983bec62015-05-27 21:04:14 +0000286 ISD.Section = getCurrentSectionOnly();
Daniel Dunbar8a3c9d92010-06-16 20:04:22 +0000287 getAssembler().getIndirectSymbols().push_back(ISD);
Saleem Abdulrasool4208b612013-08-09 01:52:03 +0000288 return true;
Daniel Dunbar582d6102009-08-24 11:56:58 +0000289 }
290
Daniel Dunbar04a11582009-08-24 08:40:12 +0000291 // Adding a symbol attribute always introduces the symbol, note that an
Rafael Espindolab5d316b2015-05-29 20:21:02 +0000292 // important side effect of calling registerSymbol here is to register
Daniel Dunbar6eab7212010-03-10 20:58:29 +0000293 // the symbol with the assembler.
Rafael Espindolab5d316b2015-05-29 20:21:02 +0000294 getAssembler().registerSymbol(*Symbol);
Daniel Dunbar04a11582009-08-24 08:40:12 +0000295
296 // The implementation of symbol attributes is designed to match 'as', but it
297 // leaves much to desired. It doesn't really make sense to arbitrarily add and
298 // remove flags, but 'as' allows this (in particular, see .desc).
299 //
300 // In the future it might be worth trying to make these operations more well
301 // defined.
Daniel Dunbard1859472009-08-22 11:41:10 +0000302 switch (Attribute) {
Chris Lattner685508c2010-01-23 06:39:22 +0000303 case MCSA_Invalid:
Chris Lattnerbc8f6382010-01-25 18:30:45 +0000304 case MCSA_ELF_TypeFunction:
305 case MCSA_ELF_TypeIndFunction:
306 case MCSA_ELF_TypeObject:
307 case MCSA_ELF_TypeTLS:
308 case MCSA_ELF_TypeCommon:
309 case MCSA_ELF_TypeNoType:
Rafael Espindola4bcf94c2010-11-13 04:51:02 +0000310 case MCSA_ELF_TypeGnuUniqueObject:
Chris Lattner685508c2010-01-23 06:39:22 +0000311 case MCSA_Hidden:
Chandler Carruthb2561022011-07-25 21:21:08 +0000312 case MCSA_IndirectSymbol:
Chris Lattner685508c2010-01-23 06:39:22 +0000313 case MCSA_Internal:
314 case MCSA_Protected:
315 case MCSA_Weak:
316 case MCSA_Local:
Saleem Abdulrasool4208b612013-08-09 01:52:03 +0000317 return false;
Daniel Dunbard1859472009-08-22 11:41:10 +0000318
Chris Lattner685508c2010-01-23 06:39:22 +0000319 case MCSA_Global:
Rafael Espindola4d37b2a2015-05-29 21:45:01 +0000320 Symbol->setExternal(true);
Daniel Dunbar0211a962010-05-17 20:12:31 +0000321 // This effectively clears the undefined lazy bit, in Darwin 'as', although
322 // it isn't very consistent because it implements this as part of symbol
323 // lookup.
324 //
325 // FIXME: Cleanup this code, these bits should be emitted based on semantic
326 // properties, not on the order of definition, etc.
Pete Cooper916f79e2015-06-08 17:17:28 +0000327 Symbol->setReferenceTypeUndefinedLazy(false);
Daniel Dunbard1859472009-08-22 11:41:10 +0000328 break;
Daniel Dunbar04a11582009-08-24 08:40:12 +0000329
Chris Lattner685508c2010-01-23 06:39:22 +0000330 case MCSA_LazyReference:
Daniel Dunbar04a11582009-08-24 08:40:12 +0000331 // FIXME: This requires -dynamic.
Pete Cooper916f79e2015-06-08 17:17:28 +0000332 Symbol->setNoDeadStrip();
Daniel Dunbar04a11582009-08-24 08:40:12 +0000333 if (Symbol->isUndefined())
Pete Cooper916f79e2015-06-08 17:17:28 +0000334 Symbol->setReferenceTypeUndefinedLazy(true);
Daniel Dunbar04a11582009-08-24 08:40:12 +0000335 break;
336
Daniel Dunbar04a11582009-08-24 08:40:12 +0000337 // Since .reference sets the no dead strip bit, it is equivalent to
338 // .no_dead_strip in practice.
Chris Lattner685508c2010-01-23 06:39:22 +0000339 case MCSA_Reference:
340 case MCSA_NoDeadStrip:
Pete Cooper916f79e2015-06-08 17:17:28 +0000341 Symbol->setNoDeadStrip();
Daniel Dunbar04a11582009-08-24 08:40:12 +0000342 break;
343
Kevin Enderby8be14412010-11-19 18:39:33 +0000344 case MCSA_SymbolResolver:
Pete Cooper916f79e2015-06-08 17:17:28 +0000345 Symbol->setSymbolResolver();
Kevin Enderby8be14412010-11-19 18:39:33 +0000346 break;
347
Chris Lattner685508c2010-01-23 06:39:22 +0000348 case MCSA_PrivateExtern:
Rafael Espindola4d37b2a2015-05-29 21:45:01 +0000349 Symbol->setExternal(true);
350 Symbol->setPrivateExtern(true);
Daniel Dunbar04a11582009-08-24 08:40:12 +0000351 break;
352
Chris Lattner685508c2010-01-23 06:39:22 +0000353 case MCSA_WeakReference:
Daniel Dunbar04a11582009-08-24 08:40:12 +0000354 // FIXME: This requires -dynamic.
355 if (Symbol->isUndefined())
Pete Cooper916f79e2015-06-08 17:17:28 +0000356 Symbol->setWeakReference();
Daniel Dunbar04a11582009-08-24 08:40:12 +0000357 break;
358
Chris Lattner685508c2010-01-23 06:39:22 +0000359 case MCSA_WeakDefinition:
Daniel Dunbar04a11582009-08-24 08:40:12 +0000360 // FIXME: 'as' enforces that this is defined and global. The manual claims
361 // it has to be in a coalesced section, but this isn't enforced.
Pete Cooper916f79e2015-06-08 17:17:28 +0000362 Symbol->setWeakDefinition();
Daniel Dunbar04a11582009-08-24 08:40:12 +0000363 break;
Kevin Enderby082d0fd2010-07-08 17:22:42 +0000364
365 case MCSA_WeakDefAutoPrivate:
Pete Cooper916f79e2015-06-08 17:17:28 +0000366 Symbol->setWeakDefinition();
367 Symbol->setWeakReference();
Kevin Enderby082d0fd2010-07-08 17:22:42 +0000368 break;
Daniel Dunbard1859472009-08-22 11:41:10 +0000369 }
Saleem Abdulrasool4208b612013-08-09 01:52:03 +0000370
371 return true;
Daniel Dunbar3016db32009-08-21 09:11:24 +0000372}
373
374void MCMachOStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
Daniel Dunbar04a11582009-08-24 08:40:12 +0000375 // Encode the 'desc' value into the lowest implementation defined bits.
Rafael Espindolab5d316b2015-05-29 20:21:02 +0000376 getAssembler().registerSymbol(*Symbol);
Pete Cooper916f79e2015-06-08 17:17:28 +0000377 cast<MCSymbolMachO>(Symbol)->setDesc(DescValue);
Daniel Dunbar3016db32009-08-21 09:11:24 +0000378}
379
Chris Lattnerb1301f72010-01-23 07:47:02 +0000380void MCMachOStreamer::EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
Daniel Dunbar6a715dc2009-08-30 06:17:16 +0000381 unsigned ByteAlignment) {
Daniel Dunbar2701eee2009-08-28 07:08:35 +0000382 // FIXME: Darwin 'as' does appear to allow redef of a .comm by itself.
383 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
384
Rafael Espindolab5d316b2015-05-29 20:21:02 +0000385 getAssembler().registerSymbol(*Symbol);
Rafael Espindola4d37b2a2015-05-29 21:45:01 +0000386 Symbol->setExternal(true);
Rafael Espindola14672502015-05-29 17:48:04 +0000387 Symbol->setCommon(Size, ByteAlignment);
Daniel Dunbar3016db32009-08-21 09:11:24 +0000388}
389
Benjamin Kramer47f9ec92012-09-07 17:25:13 +0000390void MCMachOStreamer::EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size,
391 unsigned ByteAlignment) {
392 // '.lcomm' is equivalent to '.zerofill'.
Rafael Espindolae308c0c2014-01-23 22:49:25 +0000393 return EmitZerofill(getContext().getObjectFileInfo()->getDataBSSSection(),
Benjamin Kramer47f9ec92012-09-07 17:25:13 +0000394 Symbol, Size, ByteAlignment);
395}
396
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000397void MCMachOStreamer::EmitZerofill(MCSection *Section, MCSymbol *Symbol,
Evan Chengf5bd6c62012-06-22 20:14:46 +0000398 uint64_t Size, unsigned ByteAlignment) {
Rafael Espindolabb9a71c2015-05-26 15:07:25 +0000399 getAssembler().registerSection(*Section);
Daniel Dunbar42a39d02009-08-28 05:49:21 +0000400
401 // The symbol may not be present, which only creates the section.
402 if (!Symbol)
403 return;
404
Eric Christopher42ae4592013-08-07 21:13:01 +0000405 // On darwin all virtual sections have zerofill type.
406 assert(Section->isVirtualSection() && "Section does not have zerofill type!");
Daniel Dunbar42a39d02009-08-28 05:49:21 +0000407
408 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
409
Rafael Espindolab5d316b2015-05-29 20:21:02 +0000410 getAssembler().registerSymbol(*Symbol);
Daniel Dunbar42a39d02009-08-28 05:49:21 +0000411
Daniel Dunbar51402b72010-05-12 22:51:27 +0000412 // Emit an align fragment if necessary.
413 if (ByteAlignment != 1)
Rafael Espindolabb9a71c2015-05-26 15:07:25 +0000414 new MCAlignFragment(ByteAlignment, 0, 0, ByteAlignment, Section);
Daniel Dunbar51402b72010-05-12 22:51:27 +0000415
Rafael Espindolabb9a71c2015-05-26 15:07:25 +0000416 MCFragment *F = new MCFillFragment(0, 0, Size, Section);
Rafael Espindola4d37b2a2015-05-29 21:45:01 +0000417 Symbol->setFragment(F);
Daniel Dunbar42a39d02009-08-28 05:49:21 +0000418
Daniel Dunbar42a39d02009-08-28 05:49:21 +0000419 // Update the maximum alignment on the zero fill section if necessary.
Rafael Espindola967d6a62015-05-21 21:02:35 +0000420 if (ByteAlignment > Section->getAlignment())
421 Section->setAlignment(ByteAlignment);
Daniel Dunbar3016db32009-08-21 09:11:24 +0000422}
423
Eric Christopher09d47032010-05-21 23:03:53 +0000424// This should always be called with the thread local bss section. Like the
425// .zerofill directive this doesn't actually switch sections on us.
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000426void MCMachOStreamer::EmitTBSSSymbol(MCSection *Section, MCSymbol *Symbol,
Eric Christopherfeedc902010-05-18 21:26:41 +0000427 uint64_t Size, unsigned ByteAlignment) {
428 EmitZerofill(Section, Symbol, Size, ByteAlignment);
429 return;
Eric Christopher9fb6bb02010-05-14 01:50:28 +0000430}
431
David Woodhouse6f3c73f2014-01-28 23:12:49 +0000432void MCMachOStreamer::EmitInstToData(const MCInst &Inst,
433 const MCSubtargetInfo &STI) {
Daniel Dunbar9d40ef12010-05-26 20:37:00 +0000434 MCDataFragment *DF = getOrCreateDataFragment();
435
436 SmallVector<MCFixup, 4> Fixups;
437 SmallString<256> Code;
438 raw_svector_ostream VecOS(Code);
Jim Grosbach91df21f2015-05-15 19:13:16 +0000439 getAssembler().getEmitter().encodeInstruction(Inst, VecOS, Fixups, STI);
Daniel Dunbar9d40ef12010-05-26 20:37:00 +0000440
441 // Add the fixups and data.
442 for (unsigned i = 0, e = Fixups.size(); i != e; ++i) {
443 Fixups[i].setOffset(Fixups[i].getOffset() + DF->getContents().size());
Eli Benderskya31a8942012-12-07 19:13:57 +0000444 DF->getFixups().push_back(Fixups[i]);
Daniel Dunbar9d40ef12010-05-26 20:37:00 +0000445 }
446 DF->getContents().append(Code.begin(), Code.end());
447}
448
Rafael Espindola07082092012-01-07 03:13:18 +0000449void MCMachOStreamer::FinishImpl() {
Rafael Espindola7f4ccce2014-05-12 13:30:10 +0000450 EmitFrames(&getAssembler().getBackend());
Rafael Espindolafc822362011-05-01 15:44:13 +0000451
Daniel Dunbarede8e6d2010-06-16 20:04:32 +0000452 // We have to set the fragment atom associations so we can relax properly for
453 // Mach-O.
454
455 // First, scan the symbol table to build a lookup table from fragments to
456 // defining symbols.
Duncan P. N. Exon Smith92a699c2015-05-20 20:18:16 +0000457 DenseMap<const MCFragment *, const MCSymbol *> DefiningSymbolMap;
Duncan P. N. Exon Smithf48de1c2015-05-16 00:35:24 +0000458 for (const MCSymbol &Symbol : getAssembler().symbols()) {
Rafael Espindolae3a20f52015-10-05 12:07:05 +0000459 if (getAssembler().isSymbolLinkerVisible(Symbol) && Symbol.isInSection() &&
460 !Symbol.isVariable()) {
Daniel Dunbarede8e6d2010-06-16 20:04:32 +0000461 // An atom defining symbol should never be internal to a fragment.
Rafael Espindola14672502015-05-29 17:48:04 +0000462 assert(Symbol.getOffset() == 0 &&
463 "Invalid offset in atom defining symbol!");
Rafael Espindola4d37b2a2015-05-29 21:45:01 +0000464 DefiningSymbolMap[Symbol.getFragment()] = &Symbol;
Daniel Dunbarede8e6d2010-06-16 20:04:32 +0000465 }
466 }
467
468 // Set the fragment atom associations by tracking the last seen atom defining
469 // symbol.
470 for (MCAssembler::iterator it = getAssembler().begin(),
471 ie = getAssembler().end(); it != ie; ++it) {
Duncan P. N. Exon Smith09bfa582015-05-16 00:48:58 +0000472 const MCSymbol *CurrentAtom = nullptr;
Rafael Espindolaa32d0e92015-05-27 15:14:11 +0000473 for (MCSection::iterator it2 = it->begin(), ie2 = it->end(); it2 != ie2;
474 ++it2) {
Duncan P. N. Exon Smith92a699c2015-05-20 20:18:16 +0000475 if (const MCSymbol *Symbol = DefiningSymbolMap.lookup(it2))
476 CurrentAtom = Symbol;
Daniel Dunbarede8e6d2010-06-16 20:04:32 +0000477 it2->setAtom(CurrentAtom);
478 }
479 }
480
Rafael Espindola07082092012-01-07 03:13:18 +0000481 this->MCObjectStreamer::FinishImpl();
Daniel Dunbarede8e6d2010-06-16 20:04:32 +0000482}
483
Evan Cheng5928e692011-07-25 23:24:55 +0000484MCStreamer *llvm::createMachOStreamer(MCContext &Context, MCAsmBackend &MAB,
Rafael Espindola5560a4c2015-04-14 22:14:34 +0000485 raw_pwrite_stream &OS, MCCodeEmitter *CE,
Rafael Espindola36a15cb2015-03-20 20:00:01 +0000486 bool RelaxAll, bool DWARFMustBeAtTheEnd,
Tim Northoverc3988b42014-03-29 07:05:06 +0000487 bool LabelSections) {
Rafael Espindola36a15cb2015-03-20 20:00:01 +0000488 MCMachOStreamer *S = new MCMachOStreamer(Context, MAB, OS, CE,
489 DWARFMustBeAtTheEnd, LabelSections);
Steven Wu99272062015-08-05 15:36:38 +0000490 const Triple &TT = Context.getObjectFileInfo()->getTargetTriple();
491 if (TT.isOSDarwin()) {
492 unsigned Major, Minor, Update;
493 TT.getOSVersion(Major, Minor, Update);
494 // If there is a version specified, Major will be non-zero.
495 if (Major)
496 S->EmitVersionMin((TT.isMacOSX() ?
497 MCVM_OSXVersionMin : MCVM_IOSVersionMin),
498 Major, Minor, Update);
499 }
Daniel Dunbard821f4a2010-03-25 22:49:09 +0000500 if (RelaxAll)
501 S->getAssembler().setRelaxAll(true);
502 return S;
Daniel Dunbar3016db32009-08-21 09:11:24 +0000503}