blob: a59d082acc7d8a0c44fb9474ff6931d532e618bd [file] [log] [blame]
Eugene Zelenko1d435522017-02-07 23:02:00 +00001//===- MCMachOStreamer.cpp - MachO Streamer -------------------------------===//
Daniel Dunbar3016db32009-08-21 09:11:24 +00002//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Daniel Dunbar3016db32009-08-21 09:11:24 +00006//
7//===----------------------------------------------------------------------===//
8
Tim Northoverc3988b42014-03-29 07:05:06 +00009#include "llvm/ADT/DenseMap.h"
Eugene Zelenko1d435522017-02-07 23:02:00 +000010#include "llvm/ADT/SmallString.h"
Tim Northoverc3988b42014-03-29 07:05:06 +000011#include "llvm/ADT/SmallVector.h"
Eugene Zelenko1d435522017-02-07 23:02:00 +000012#include "llvm/ADT/StringRef.h"
13#include "llvm/ADT/Triple.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000014#include "llvm/MC/MCAsmBackend.h"
Daniel Dunbar3016db32009-08-21 09:11:24 +000015#include "llvm/MC/MCAssembler.h"
Daniel Dunbarc7c5f9f2009-08-27 08:17:51 +000016#include "llvm/MC/MCCodeEmitter.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000017#include "llvm/MC/MCContext.h"
Eugene Zelenko1d435522017-02-07 23:02:00 +000018#include "llvm/MC/MCDirectives.h"
Daniel Dunbar73da11e2009-08-31 08:08:38 +000019#include "llvm/MC/MCExpr.h"
Eugene Zelenko1d435522017-02-07 23:02:00 +000020#include "llvm/MC/MCFixup.h"
21#include "llvm/MC/MCFragment.h"
Daniel Dunbarc7c5f9f2009-08-27 08:17:51 +000022#include "llvm/MC/MCInst.h"
Tim Northover53d32512014-03-29 07:34:53 +000023#include "llvm/MC/MCLinkerOptimizationHint.h"
Rafael Espindolae308c0c2014-01-23 22:49:25 +000024#include "llvm/MC/MCObjectFileInfo.h"
Daniel Dunbar8a3c9d92010-06-16 20:04:22 +000025#include "llvm/MC/MCObjectStreamer.h"
Peter Collingbournef7b81db2018-05-18 18:26:45 +000026#include "llvm/MC/MCObjectWriter.h"
Daniel Dunbar3016db32009-08-21 09:11:24 +000027#include "llvm/MC/MCSection.h"
Kevin Enderby7221b762010-08-09 22:52:14 +000028#include "llvm/MC/MCSectionMachO.h"
Eugene Zelenko1d435522017-02-07 23:02:00 +000029#include "llvm/MC/MCStreamer.h"
30#include "llvm/MC/MCSymbol.h"
Pete Cooper916f79e2015-06-08 17:17:28 +000031#include "llvm/MC/MCSymbolMachO.h"
Lang Hames1b640e02016-03-15 01:43:05 +000032#include "llvm/MC/MCValue.h"
Eugene Zelenko1d435522017-02-07 23:02:00 +000033#include "llvm/Support/Casting.h"
Daniel Dunbar3016db32009-08-21 09:11:24 +000034#include "llvm/Support/ErrorHandling.h"
Eugene Zelenko1d435522017-02-07 23:02:00 +000035#include "llvm/Support/TargetRegistry.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000036#include "llvm/Support/raw_ostream.h"
Eugene Zelenko1d435522017-02-07 23:02:00 +000037#include <cassert>
38#include <vector>
Daniel Dunbarde04b3f2010-03-23 05:09:03 +000039
Daniel Dunbar3016db32009-08-21 09:11:24 +000040using namespace llvm;
41
42namespace {
43
Daniel Dunbar8a3c9d92010-06-16 20:04:22 +000044class MCMachOStreamer : public MCObjectStreamer {
Daniel Dunbar04a11582009-08-24 08:40:12 +000045private:
Tim Northoverc3988b42014-03-29 07:05:06 +000046 /// LabelSections - true if each section change should emit a linker local
47 /// label for use in relocations for assembler local references. Obviates the
48 /// need for local relocations. False by default.
49 bool LabelSections;
50
Rafael Espindola36a15cb2015-03-20 20:00:01 +000051 bool DWARFMustBeAtTheEnd;
52 bool CreatedADWARFSection;
53
Tim Northoverc3988b42014-03-29 07:05:06 +000054 /// HasSectionLabel - map of which sections have already had a non-local
55 /// label emitted to them. Used so we don't emit extraneous linker local
56 /// labels in the middle of the section.
57 DenseMap<const MCSection*, bool> HasSectionLabel;
58
Shengchen Kanf0019d42020-04-20 02:53:00 -070059 void emitInstToData(const MCInst &Inst, const MCSubtargetInfo &STI) override;
Daniel Dunbar9d40ef12010-05-26 20:37:00 +000060
Fangrui Songa55daa12020-02-14 18:16:24 -080061 void emitDataRegion(DataRegionData::KindTy Kind);
62 void emitDataRegionEnd();
Tim Northoverc3988b42014-03-29 07:05:06 +000063
Daniel Dunbar3016db32009-08-21 09:11:24 +000064public:
Lang Hames02d33052017-10-11 01:57:21 +000065 MCMachOStreamer(MCContext &Context, std::unique_ptr<MCAsmBackend> MAB,
Peter Collingbournef7b81db2018-05-18 18:26:45 +000066 std::unique_ptr<MCObjectWriter> OW,
67 std::unique_ptr<MCCodeEmitter> Emitter,
Lang Hames02d33052017-10-11 01:57:21 +000068 bool DWARFMustBeAtTheEnd, bool label)
Peter Collingbournef7b81db2018-05-18 18:26:45 +000069 : MCObjectStreamer(Context, std::move(MAB), std::move(OW),
70 std::move(Emitter)),
Lang Hames02d33052017-10-11 01:57:21 +000071 LabelSections(label), DWARFMustBeAtTheEnd(DWARFMustBeAtTheEnd),
72 CreatedADWARFSection(false) {}
Daniel Dunbard821f4a2010-03-25 22:49:09 +000073
Yaron Keren559b47d2014-09-17 09:25:36 +000074 /// state management
75 void reset() override {
Justin Bogner72e81892015-12-03 00:52:20 +000076 CreatedADWARFSection = false;
Yaron Keren559b47d2014-09-17 09:25:36 +000077 HasSectionLabel.clear();
78 MCObjectStreamer::reset();
79 }
80
Daniel Dunbar3016db32009-08-21 09:11:24 +000081 /// @name MCStreamer Interface
82 /// @{
83
Shengchen Kanc0313782020-04-20 19:28:13 -070084 void changeSection(MCSection *Sect, const MCExpr *Subsect) override;
Fangrui Song6d2d5892020-02-14 19:21:58 -080085 void emitLabel(MCSymbol *Symbol, SMLoc Loc = SMLoc()) override;
Fangrui Songa55daa12020-02-14 18:16:24 -080086 void emitAssignment(MCSymbol *Symbol, const MCExpr *Value) override;
87 void emitEHSymAttributes(const MCSymbol *Symbol, MCSymbol *EHSymbol) override;
88 void emitAssemblerFlag(MCAssemblerFlag Flag) override;
89 void emitLinkerOptions(ArrayRef<std::string> Options) override;
90 void emitDataRegion(MCDataRegionType Kind) override;
91 void emitVersionMin(MCVersionMinType Kind, unsigned Major, unsigned Minor,
Alex Lorenzafa75d72018-12-14 01:14:10 +000092 unsigned Update, VersionTuple SDKVersion) override;
Fangrui Songa55daa12020-02-14 18:16:24 -080093 void emitBuildVersion(unsigned Platform, unsigned Major, unsigned Minor,
Alex Lorenzafa75d72018-12-14 01:14:10 +000094 unsigned Update, VersionTuple SDKVersion) override;
Fangrui Songa55daa12020-02-14 18:16:24 -080095 void emitThumbFunc(MCSymbol *Func) override;
96 bool emitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute) override;
97 void emitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) override;
98 void emitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
Craig Topper34a61bc2014-03-08 07:02:02 +000099 unsigned ByteAlignment) override;
Eugene Zelenko1d435522017-02-07 23:02:00 +0000100
Fangrui Songa55daa12020-02-14 18:16:24 -0800101 void emitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size,
Craig Topper34a61bc2014-03-08 07:02:02 +0000102 unsigned ByteAlignment) override;
Fangrui Songa55daa12020-02-14 18:16:24 -0800103 void emitZerofill(MCSection *Section, MCSymbol *Symbol = nullptr,
Francis Visoiu Mistrih4d5b1072018-07-02 17:29:43 +0000104 uint64_t Size = 0, unsigned ByteAlignment = 0,
105 SMLoc Loc = SMLoc()) override;
Fangrui Song549b4362020-02-15 08:52:56 -0800106 void emitTBSSSymbol(MCSection *Section, MCSymbol *Symbol, uint64_t Size,
Benjamin Kramer8c90fd72014-09-03 11:41:21 +0000107 unsigned ByteAlignment = 0) override;
Daniel Dunbar9d40ef12010-05-26 20:37:00 +0000108
Fangrui Song549b4362020-02-15 08:52:56 -0800109 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
Fangrui Songa55daa12020-02-14 18:16:24 -0800113 void emitLOHDirective(MCLOHType Kind, const MCLOHArgs &Args) override {
Tim Northover53d32512014-03-29 07:34:53 +0000114 getAssembler().getLOHContainer().addDirective(Kind, Args);
115 }
116
Shengchen Kanc0313782020-04-20 19:28:13 -0700117 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();
Fangrui Song7d1ff442020-04-15 15:49:05 -0700126 StringRef SecName = MSec.getName();
Rafael Espindola36a15cb2015-03-20 20:00:01 +0000127
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
Matthias Braun8d115a32017-02-01 01:31:36 +0000142 if (SegName == "__DATA" && (SecName == "__nl_symbol_ptr" ||
143 SecName == "__thread_ptr"))
Rafael Espindola36a15cb2015-03-20 20:00:01 +0000144 return true;
145
146 return false;
147}
148
Shengchen Kanc0313782020-04-20 19:28:13 -0700149void MCMachOStreamer::changeSection(MCSection *Section,
Tim Northoverc3988b42014-03-29 07:05:06 +0000150 const MCExpr *Subsection) {
151 // Change the section normally.
David Blaikie618d3422017-03-16 00:43:19 +0000152 bool Created = changeSectionImpl(Section, Subsection);
Rafael Espindola36a15cb2015-03-20 20:00:01 +0000153 const MCSectionMachO &MSec = *cast<MCSectionMachO>(Section);
154 StringRef SegName = MSec.getSegmentName();
155 if (SegName == "__DWARF")
156 CreatedADWARFSection = true;
157 else if (Created && DWARFMustBeAtTheEnd && !canGoAfterDWARF(MSec))
Lang Hames3f981cd2020-03-08 21:41:10 -0700158 assert((!CreatedADWARFSection ||
159 Section == getContext().getObjectFileInfo()->getStackMapSection())
160 && "Creating regular section after DWARF");
Rafael Espindola36a15cb2015-03-20 20:00:01 +0000161
Tim Northoverc3988b42014-03-29 07:05:06 +0000162 // Output a linker-local symbol so we don't need section-relative local
163 // relocations. The linker hates us when we do that.
Rafael Espindola2f9bdd82015-05-27 20:52:32 +0000164 if (LabelSections && !HasSectionLabel[Section] &&
165 !Section->getBeginSymbol()) {
Jim Grosbach6f482002015-05-18 18:43:14 +0000166 MCSymbol *Label = getContext().createLinkerPrivateTempSymbol();
Rafael Espindola2f9bdd82015-05-27 20:52:32 +0000167 Section->setBeginSymbol(Label);
Tim Northoverc3988b42014-03-29 07:05:06 +0000168 HasSectionLabel[Section] = true;
169 }
170}
171
Fangrui Songa55daa12020-02-14 18:16:24 -0800172void MCMachOStreamer::emitEHSymAttributes(const MCSymbol *Symbol,
Rafael Espindola349c3292011-04-28 12:50:37 +0000173 MCSymbol *EHSymbol) {
Rafael Espindolab5d316b2015-05-29 20:21:02 +0000174 getAssembler().registerSymbol(*Symbol);
Rafael Espindola4d37b2a2015-05-29 21:45:01 +0000175 if (Symbol->isExternal())
Fangrui Songa55daa12020-02-14 18:16:24 -0800176 emitSymbolAttribute(EHSymbol, MCSA_Global);
Pete Cooper916f79e2015-06-08 17:17:28 +0000177 if (cast<MCSymbolMachO>(Symbol)->isWeakDefinition())
Fangrui Songa55daa12020-02-14 18:16:24 -0800178 emitSymbolAttribute(EHSymbol, MCSA_WeakDefinition);
Rafael Espindola4d37b2a2015-05-29 21:45:01 +0000179 if (Symbol->isPrivateExtern())
Fangrui Songa55daa12020-02-14 18:16:24 -0800180 emitSymbolAttribute(EHSymbol, MCSA_PrivateExtern);
Rafael Espindola349c3292011-04-28 12:50:37 +0000181}
182
Fangrui Song6d2d5892020-02-14 19:21:58 -0800183void MCMachOStreamer::emitLabel(MCSymbol *Symbol, SMLoc Loc) {
Daniel Dunbarede8e6d2010-06-16 20:04:32 +0000184 // We have to create a new fragment if this is an atom defining symbol,
185 // fragments cannot span atoms.
Rafael Espindolae5b74152010-11-28 17:18:55 +0000186 if (getAssembler().isSymbolLinkerVisible(*Symbol))
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000187 insert(new MCDataFragment());
Daniel Dunbaraadb2ca2010-05-10 22:45:09 +0000188
Fangrui Song6d2d5892020-02-14 19:21:58 -0800189 MCObjectStreamer::emitLabel(Symbol, Loc);
Daniel Dunbar04a11582009-08-24 08:40:12 +0000190
Daniel Dunbar0211a962010-05-17 20:12:31 +0000191 // This causes the reference type flag to be cleared. Darwin 'as' was "trying"
192 // to clear the weak reference and weak definition bits too, but the
193 // implementation was buggy. For now we just try to match 'as', for
194 // diffability.
195 //
196 // FIXME: Cleanup this code, these bits should be emitted based on semantic
197 // properties, not on the order of definition, etc.
Pete Cooper916f79e2015-06-08 17:17:28 +0000198 cast<MCSymbolMachO>(Symbol)->clearReferenceType();
Daniel Dunbar3016db32009-08-21 09:11:24 +0000199}
200
Fangrui Songa55daa12020-02-14 18:16:24 -0800201void MCMachOStreamer::emitAssignment(MCSymbol *Symbol, const MCExpr *Value) {
Lang Hames1b640e02016-03-15 01:43:05 +0000202 MCValue Res;
203
Lang Hamesabda4d22016-03-15 04:20:49 +0000204 if (Value->evaluateAsRelocatable(Res, nullptr, nullptr)) {
205 if (const MCSymbolRefExpr *SymAExpr = Res.getSymA()) {
206 const MCSymbol &SymA = SymAExpr->getSymbol();
207 if (!Res.getSymB() && (SymA.getName() == "" || Res.getConstant() != 0))
208 cast<MCSymbolMachO>(Symbol)->setAltEntry();
209 }
210 }
Fangrui Songa55daa12020-02-14 18:16:24 -0800211 MCObjectStreamer::emitAssignment(Symbol, Value);
Lang Hames1b640e02016-03-15 01:43:05 +0000212}
213
Fangrui Songa55daa12020-02-14 18:16:24 -0800214void MCMachOStreamer::emitDataRegion(DataRegionData::KindTy Kind) {
Jim Grosbach4b63d2a2012-05-18 19:12:01 +0000215 // Create a temporary label to mark the start of the data region.
Jim Grosbach6f482002015-05-18 18:43:14 +0000216 MCSymbol *Start = getContext().createTempSymbol();
Fangrui Song6d2d5892020-02-14 19:21:58 -0800217 emitLabel(Start);
Jim Grosbach4b63d2a2012-05-18 19:12:01 +0000218 // Record the region for the object writer to use.
Craig Topperbb694de2014-04-13 04:57:38 +0000219 DataRegionData Data = { Kind, Start, nullptr };
Jim Grosbach4b63d2a2012-05-18 19:12:01 +0000220 std::vector<DataRegionData> &Regions = getAssembler().getDataRegions();
221 Regions.push_back(Data);
222}
223
Fangrui Songa55daa12020-02-14 18:16:24 -0800224void MCMachOStreamer::emitDataRegionEnd() {
Jim Grosbach4b63d2a2012-05-18 19:12:01 +0000225 std::vector<DataRegionData> &Regions = getAssembler().getDataRegions();
Alexander Kornienko8c0809c2015-01-15 11:41:30 +0000226 assert(!Regions.empty() && "Mismatched .end_data_region!");
Jim Grosbach4b63d2a2012-05-18 19:12:01 +0000227 DataRegionData &Data = Regions.back();
Craig Topperbb694de2014-04-13 04:57:38 +0000228 assert(!Data.End && "Mismatched .end_data_region!");
Jim Grosbach4b63d2a2012-05-18 19:12:01 +0000229 // Create a temporary label to mark the end of the data region.
Jim Grosbach6f482002015-05-18 18:43:14 +0000230 Data.End = getContext().createTempSymbol();
Fangrui Song6d2d5892020-02-14 19:21:58 -0800231 emitLabel(Data.End);
Jim Grosbach4b63d2a2012-05-18 19:12:01 +0000232}
233
Fangrui Songa55daa12020-02-14 18:16:24 -0800234void MCMachOStreamer::emitAssemblerFlag(MCAssemblerFlag Flag) {
Jim Grosbach87055ed2010-12-08 01:16:55 +0000235 // Let the target do whatever target specific stuff it needs to do.
Jim Grosbachaba3de92012-01-18 18:52:16 +0000236 getAssembler().getBackend().handleAssemblerFlag(Flag);
Jim Grosbach87055ed2010-12-08 01:16:55 +0000237 // Do any generic stuff we need to do.
Daniel Dunbare2697732009-08-26 21:22:22 +0000238 switch (Flag) {
Jim Grosbach5a2c68d2010-11-05 22:08:08 +0000239 case MCAF_SyntaxUnified: return; // no-op here.
Evan Cheng481ebb02011-07-27 00:38:12 +0000240 case MCAF_Code16: return; // Change parsing mode; no-op here.
241 case MCAF_Code32: return; // Change parsing mode; no-op here.
242 case MCAF_Code64: return; // Change parsing mode; no-op here.
Chris Lattner685508c2010-01-23 06:39:22 +0000243 case MCAF_SubsectionsViaSymbols:
Daniel Dunbar8a3c9d92010-06-16 20:04:22 +0000244 getAssembler().setSubsectionsViaSymbols(true);
Daniel Dunbar2a2459a2009-08-28 07:08:47 +0000245 return;
Daniel Dunbare2697732009-08-26 21:22:22 +0000246 }
Daniel Dunbar3016db32009-08-21 09:11:24 +0000247}
248
Fangrui Songa55daa12020-02-14 18:16:24 -0800249void MCMachOStreamer::emitLinkerOptions(ArrayRef<std::string> Options) {
Daniel Dunbareec0f322013-01-18 01:26:07 +0000250 getAssembler().getLinkerOptions().push_back(Options);
251}
252
Fangrui Songa55daa12020-02-14 18:16:24 -0800253void MCMachOStreamer::emitDataRegion(MCDataRegionType Kind) {
Jim Grosbach4b63d2a2012-05-18 19:12:01 +0000254 switch (Kind) {
255 case MCDR_DataRegion:
Fangrui Songa55daa12020-02-14 18:16:24 -0800256 emitDataRegion(DataRegionData::Data);
Jim Grosbach4b63d2a2012-05-18 19:12:01 +0000257 return;
258 case MCDR_DataRegionJT8:
Fangrui Songa55daa12020-02-14 18:16:24 -0800259 emitDataRegion(DataRegionData::JumpTable8);
Jim Grosbach4b63d2a2012-05-18 19:12:01 +0000260 return;
261 case MCDR_DataRegionJT16:
Fangrui Songa55daa12020-02-14 18:16:24 -0800262 emitDataRegion(DataRegionData::JumpTable16);
Jim Grosbach4b63d2a2012-05-18 19:12:01 +0000263 return;
264 case MCDR_DataRegionJT32:
Fangrui Songa55daa12020-02-14 18:16:24 -0800265 emitDataRegion(DataRegionData::JumpTable32);
Jim Grosbach4b63d2a2012-05-18 19:12:01 +0000266 return;
267 case MCDR_DataRegionEnd:
Fangrui Songa55daa12020-02-14 18:16:24 -0800268 emitDataRegionEnd();
Jim Grosbach4b63d2a2012-05-18 19:12:01 +0000269 return;
270 }
271}
272
Fangrui Songa55daa12020-02-14 18:16:24 -0800273void MCMachOStreamer::emitVersionMin(MCVersionMinType Kind, unsigned Major,
Alex Lorenzafa75d72018-12-14 01:14:10 +0000274 unsigned Minor, unsigned Update,
275 VersionTuple SDKVersion) {
276 getAssembler().setVersionMin(Kind, Major, Minor, Update, SDKVersion);
Matthias Braun0148c882017-12-14 00:12:46 +0000277}
278
Fangrui Songa55daa12020-02-14 18:16:24 -0800279void MCMachOStreamer::emitBuildVersion(unsigned Platform, unsigned Major,
Alex Lorenzafa75d72018-12-14 01:14:10 +0000280 unsigned Minor, unsigned Update,
281 VersionTuple SDKVersion) {
Matthias Braun0148c882017-12-14 00:12:46 +0000282 getAssembler().setBuildVersion((MachO::PlatformType)Platform, Major, Minor,
Alex Lorenzafa75d72018-12-14 01:14:10 +0000283 Update, SDKVersion);
Jim Grosbach448334a2014-03-18 22:09:05 +0000284}
285
Fangrui Songa55daa12020-02-14 18:16:24 -0800286void MCMachOStreamer::emitThumbFunc(MCSymbol *Symbol) {
Jim Grosbach41955ff2010-12-14 18:46:57 +0000287 // Remember that the function is a thumb function. Fixup and relocation
288 // values will need adjusted.
Daniel Dunbarab14a6f2010-12-29 14:14:06 +0000289 getAssembler().setIsThumbFunc(Symbol);
Pete Cooper916f79e2015-06-08 17:17:28 +0000290 cast<MCSymbolMachO>(Symbol)->setThumbFunc();
Jim Grosbach5a2c68d2010-11-05 22:08:08 +0000291}
292
Fangrui Songa55daa12020-02-14 18:16:24 -0800293bool MCMachOStreamer::emitSymbolAttribute(MCSymbol *Sym,
Chris Lattner685508c2010-01-23 06:39:22 +0000294 MCSymbolAttr Attribute) {
Pete Cooper916f79e2015-06-08 17:17:28 +0000295 MCSymbolMachO *Symbol = cast<MCSymbolMachO>(Sym);
296
Daniel Dunbar582d6102009-08-24 11:56:58 +0000297 // Indirect symbols are handled differently, to match how 'as' handles
298 // them. This makes writing matching .o files easier.
Chris Lattner685508c2010-01-23 06:39:22 +0000299 if (Attribute == MCSA_IndirectSymbol) {
Daniel Dunbarc2c0bf92009-08-26 00:18:21 +0000300 // Note that we intentionally cannot use the symbol data here; this is
301 // important for matching the string table that 'as' generates.
Daniel Dunbar582d6102009-08-24 11:56:58 +0000302 IndirectSymbolData ISD;
303 ISD.Symbol = Symbol;
Rafael Espindola983bec62015-05-27 21:04:14 +0000304 ISD.Section = getCurrentSectionOnly();
Daniel Dunbar8a3c9d92010-06-16 20:04:22 +0000305 getAssembler().getIndirectSymbols().push_back(ISD);
Saleem Abdulrasool4208b612013-08-09 01:52:03 +0000306 return true;
Daniel Dunbar582d6102009-08-24 11:56:58 +0000307 }
308
Daniel Dunbar04a11582009-08-24 08:40:12 +0000309 // Adding a symbol attribute always introduces the symbol, note that an
Rafael Espindolab5d316b2015-05-29 20:21:02 +0000310 // important side effect of calling registerSymbol here is to register
Daniel Dunbar6eab7212010-03-10 20:58:29 +0000311 // the symbol with the assembler.
Rafael Espindolab5d316b2015-05-29 20:21:02 +0000312 getAssembler().registerSymbol(*Symbol);
Daniel Dunbar04a11582009-08-24 08:40:12 +0000313
314 // The implementation of symbol attributes is designed to match 'as', but it
315 // leaves much to desired. It doesn't really make sense to arbitrarily add and
316 // remove flags, but 'as' allows this (in particular, see .desc).
317 //
318 // In the future it might be worth trying to make these operations more well
319 // defined.
Daniel Dunbard1859472009-08-22 11:41:10 +0000320 switch (Attribute) {
Chris Lattner685508c2010-01-23 06:39:22 +0000321 case MCSA_Invalid:
Chris Lattnerbc8f6382010-01-25 18:30:45 +0000322 case MCSA_ELF_TypeFunction:
323 case MCSA_ELF_TypeIndFunction:
324 case MCSA_ELF_TypeObject:
325 case MCSA_ELF_TypeTLS:
326 case MCSA_ELF_TypeCommon:
327 case MCSA_ELF_TypeNoType:
Rafael Espindola4bcf94c2010-11-13 04:51:02 +0000328 case MCSA_ELF_TypeGnuUniqueObject:
Chris Lattner685508c2010-01-23 06:39:22 +0000329 case MCSA_Hidden:
Chandler Carruthb2561022011-07-25 21:21:08 +0000330 case MCSA_IndirectSymbol:
Chris Lattner685508c2010-01-23 06:39:22 +0000331 case MCSA_Internal:
332 case MCSA_Protected:
333 case MCSA_Weak:
334 case MCSA_Local:
Xiangling Liao3b808fb2019-09-26 19:38:32 +0000335 case MCSA_LGlobal:
Saleem Abdulrasool4208b612013-08-09 01:52:03 +0000336 return false;
Daniel Dunbard1859472009-08-22 11:41:10 +0000337
Chris Lattner685508c2010-01-23 06:39:22 +0000338 case MCSA_Global:
Rafael Espindola4d37b2a2015-05-29 21:45:01 +0000339 Symbol->setExternal(true);
Daniel Dunbar0211a962010-05-17 20:12:31 +0000340 // This effectively clears the undefined lazy bit, in Darwin 'as', although
341 // it isn't very consistent because it implements this as part of symbol
342 // lookup.
343 //
344 // FIXME: Cleanup this code, these bits should be emitted based on semantic
345 // properties, not on the order of definition, etc.
Pete Cooper916f79e2015-06-08 17:17:28 +0000346 Symbol->setReferenceTypeUndefinedLazy(false);
Daniel Dunbard1859472009-08-22 11:41:10 +0000347 break;
Daniel Dunbar04a11582009-08-24 08:40:12 +0000348
Chris Lattner685508c2010-01-23 06:39:22 +0000349 case MCSA_LazyReference:
Daniel Dunbar04a11582009-08-24 08:40:12 +0000350 // FIXME: This requires -dynamic.
Pete Cooper916f79e2015-06-08 17:17:28 +0000351 Symbol->setNoDeadStrip();
Daniel Dunbar04a11582009-08-24 08:40:12 +0000352 if (Symbol->isUndefined())
Pete Cooper916f79e2015-06-08 17:17:28 +0000353 Symbol->setReferenceTypeUndefinedLazy(true);
Daniel Dunbar04a11582009-08-24 08:40:12 +0000354 break;
355
Daniel Dunbar04a11582009-08-24 08:40:12 +0000356 // Since .reference sets the no dead strip bit, it is equivalent to
357 // .no_dead_strip in practice.
Chris Lattner685508c2010-01-23 06:39:22 +0000358 case MCSA_Reference:
359 case MCSA_NoDeadStrip:
Pete Cooper916f79e2015-06-08 17:17:28 +0000360 Symbol->setNoDeadStrip();
Daniel Dunbar04a11582009-08-24 08:40:12 +0000361 break;
362
Kevin Enderby8be14412010-11-19 18:39:33 +0000363 case MCSA_SymbolResolver:
Pete Cooper916f79e2015-06-08 17:17:28 +0000364 Symbol->setSymbolResolver();
Kevin Enderby8be14412010-11-19 18:39:33 +0000365 break;
366
Lang Hames1b640e02016-03-15 01:43:05 +0000367 case MCSA_AltEntry:
368 Symbol->setAltEntry();
369 break;
370
Chris Lattner685508c2010-01-23 06:39:22 +0000371 case MCSA_PrivateExtern:
Rafael Espindola4d37b2a2015-05-29 21:45:01 +0000372 Symbol->setExternal(true);
373 Symbol->setPrivateExtern(true);
Daniel Dunbar04a11582009-08-24 08:40:12 +0000374 break;
375
Chris Lattner685508c2010-01-23 06:39:22 +0000376 case MCSA_WeakReference:
Daniel Dunbar04a11582009-08-24 08:40:12 +0000377 // FIXME: This requires -dynamic.
378 if (Symbol->isUndefined())
Pete Cooper916f79e2015-06-08 17:17:28 +0000379 Symbol->setWeakReference();
Daniel Dunbar04a11582009-08-24 08:40:12 +0000380 break;
381
Chris Lattner685508c2010-01-23 06:39:22 +0000382 case MCSA_WeakDefinition:
Daniel Dunbar04a11582009-08-24 08:40:12 +0000383 // FIXME: 'as' enforces that this is defined and global. The manual claims
384 // it has to be in a coalesced section, but this isn't enforced.
Pete Cooper916f79e2015-06-08 17:17:28 +0000385 Symbol->setWeakDefinition();
Daniel Dunbar04a11582009-08-24 08:40:12 +0000386 break;
Kevin Enderby082d0fd2010-07-08 17:22:42 +0000387
388 case MCSA_WeakDefAutoPrivate:
Pete Cooper916f79e2015-06-08 17:17:28 +0000389 Symbol->setWeakDefinition();
390 Symbol->setWeakReference();
Kevin Enderby082d0fd2010-07-08 17:22:42 +0000391 break;
Vedant Kumar13ef84f2019-01-25 18:30:22 +0000392
393 case MCSA_Cold:
394 Symbol->setCold();
395 break;
Daniel Dunbard1859472009-08-22 11:41:10 +0000396 }
Saleem Abdulrasool4208b612013-08-09 01:52:03 +0000397
398 return true;
Daniel Dunbar3016db32009-08-21 09:11:24 +0000399}
400
Fangrui Songa55daa12020-02-14 18:16:24 -0800401void MCMachOStreamer::emitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
Daniel Dunbar04a11582009-08-24 08:40:12 +0000402 // Encode the 'desc' value into the lowest implementation defined bits.
Rafael Espindolab5d316b2015-05-29 20:21:02 +0000403 getAssembler().registerSymbol(*Symbol);
Pete Cooper916f79e2015-06-08 17:17:28 +0000404 cast<MCSymbolMachO>(Symbol)->setDesc(DescValue);
Daniel Dunbar3016db32009-08-21 09:11:24 +0000405}
406
Fangrui Songa55daa12020-02-14 18:16:24 -0800407void MCMachOStreamer::emitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
Daniel Dunbar6a715dc2009-08-30 06:17:16 +0000408 unsigned ByteAlignment) {
Daniel Dunbar2701eee2009-08-28 07:08:35 +0000409 // FIXME: Darwin 'as' does appear to allow redef of a .comm by itself.
410 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
411
Rafael Espindolab5d316b2015-05-29 20:21:02 +0000412 getAssembler().registerSymbol(*Symbol);
Rafael Espindola4d37b2a2015-05-29 21:45:01 +0000413 Symbol->setExternal(true);
Rafael Espindola14672502015-05-29 17:48:04 +0000414 Symbol->setCommon(Size, ByteAlignment);
Daniel Dunbar3016db32009-08-21 09:11:24 +0000415}
416
Fangrui Songa55daa12020-02-14 18:16:24 -0800417void MCMachOStreamer::emitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size,
Benjamin Kramer47f9ec92012-09-07 17:25:13 +0000418 unsigned ByteAlignment) {
419 // '.lcomm' is equivalent to '.zerofill'.
Fangrui Songa55daa12020-02-14 18:16:24 -0800420 return emitZerofill(getContext().getObjectFileInfo()->getDataBSSSection(),
Benjamin Kramer47f9ec92012-09-07 17:25:13 +0000421 Symbol, Size, ByteAlignment);
422}
423
Fangrui Songa55daa12020-02-14 18:16:24 -0800424void MCMachOStreamer::emitZerofill(MCSection *Section, MCSymbol *Symbol,
Francis Visoiu Mistrih4d5b1072018-07-02 17:29:43 +0000425 uint64_t Size, unsigned ByteAlignment,
426 SMLoc Loc) {
427 // On darwin all virtual sections have zerofill type. Disallow the usage of
428 // .zerofill in non-virtual functions. If something similar is needed, use
429 // .space or .zero.
430 if (!Section->isVirtualSection()) {
431 getContext().reportError(
432 Loc, "The usage of .zerofill is restricted to sections of "
433 "ZEROFILL type. Use .zero or .space instead.");
434 return; // Early returning here shouldn't harm. EmitZeros should work on any
435 // section.
436 }
Daniel Dunbar42a39d02009-08-28 05:49:21 +0000437
Rafael Espindola07657a82018-01-09 21:55:10 +0000438 PushSection();
439 SwitchSection(Section);
Daniel Dunbar42a39d02009-08-28 05:49:21 +0000440
Rafael Espindola07657a82018-01-09 21:55:10 +0000441 // The symbol may not be present, which only creates the section.
442 if (Symbol) {
Fangrui Song6d2d5892020-02-14 19:21:58 -0800443 emitValueToAlignment(ByteAlignment, 0, 1, 0);
444 emitLabel(Symbol);
Fangrui Song549b4362020-02-15 08:52:56 -0800445 emitZeros(Size);
Rafael Espindola07657a82018-01-09 21:55:10 +0000446 }
447 PopSection();
Daniel Dunbar3016db32009-08-21 09:11:24 +0000448}
449
Eric Christopher09d47032010-05-21 23:03:53 +0000450// This should always be called with the thread local bss section. Like the
451// .zerofill directive this doesn't actually switch sections on us.
Fangrui Song549b4362020-02-15 08:52:56 -0800452void MCMachOStreamer::emitTBSSSymbol(MCSection *Section, MCSymbol *Symbol,
Eric Christopherfeedc902010-05-18 21:26:41 +0000453 uint64_t Size, unsigned ByteAlignment) {
Fangrui Songa55daa12020-02-14 18:16:24 -0800454 emitZerofill(Section, Symbol, Size, ByteAlignment);
Eric Christopher9fb6bb02010-05-14 01:50:28 +0000455}
456
Shengchen Kanf0019d42020-04-20 02:53:00 -0700457void MCMachOStreamer::emitInstToData(const MCInst &Inst,
David Woodhouse6f3c73f2014-01-28 23:12:49 +0000458 const MCSubtargetInfo &STI) {
Daniel Dunbar9d40ef12010-05-26 20:37:00 +0000459 MCDataFragment *DF = getOrCreateDataFragment();
460
461 SmallVector<MCFixup, 4> Fixups;
462 SmallString<256> Code;
463 raw_svector_ostream VecOS(Code);
Jim Grosbach91df21f2015-05-15 19:13:16 +0000464 getAssembler().getEmitter().encodeInstruction(Inst, VecOS, Fixups, STI);
Daniel Dunbar9d40ef12010-05-26 20:37:00 +0000465
466 // Add the fixups and data.
Craig Topper1129a002015-10-14 04:36:00 +0000467 for (MCFixup &Fixup : Fixups) {
468 Fixup.setOffset(Fixup.getOffset() + DF->getContents().size());
469 DF->getFixups().push_back(Fixup);
Daniel Dunbar9d40ef12010-05-26 20:37:00 +0000470 }
Peter Smith57f661b2018-06-06 09:40:06 +0000471 DF->setHasInstructions(STI);
Daniel Dunbar9d40ef12010-05-26 20:37:00 +0000472 DF->getContents().append(Code.begin(), Code.end());
473}
474
Shengchen Kanc0313782020-04-20 19:28:13 -0700475void MCMachOStreamer::finishImpl() {
476 emitFrames(&getAssembler().getBackend());
Rafael Espindolafc822362011-05-01 15:44:13 +0000477
Daniel Dunbarede8e6d2010-06-16 20:04:32 +0000478 // We have to set the fragment atom associations so we can relax properly for
479 // Mach-O.
Sam Cleggc0dba0a2018-06-14 17:11:19 +0000480
481 // First, scan the symbol table to build a lookup table from fragments to
482 // defining symbols.
483 DenseMap<const MCFragment *, const MCSymbol *> DefiningSymbolMap;
484 for (const MCSymbol &Symbol : getAssembler().symbols()) {
485 if (getAssembler().isSymbolLinkerVisible(Symbol) && Symbol.isInSection() &&
486 !Symbol.isVariable()) {
487 // An atom defining symbol should never be internal to a fragment.
488 assert(Symbol.getOffset() == 0 &&
489 "Invalid offset in atom defining symbol!");
490 DefiningSymbolMap[Symbol.getFragment()] = &Symbol;
491 }
492 }
493
494 // Set the fragment atom associations by tracking the last seen atom defining
495 // symbol.
496 for (MCSection &Sec : getAssembler()) {
497 const MCSymbol *CurrentAtom = nullptr;
498 for (MCFragment &Frag : Sec) {
499 if (const MCSymbol *Symbol = DefiningSymbolMap.lookup(&Frag))
500 CurrentAtom = Symbol;
501 Frag.setAtom(CurrentAtom);
502 }
503 }
Daniel Dunbarede8e6d2010-06-16 20:04:32 +0000504
Shengchen Kanc0313782020-04-20 19:28:13 -0700505 this->MCObjectStreamer::finishImpl();
Daniel Dunbarede8e6d2010-06-16 20:04:32 +0000506}
507
Lang Hames02d33052017-10-11 01:57:21 +0000508MCStreamer *llvm::createMachOStreamer(MCContext &Context,
509 std::unique_ptr<MCAsmBackend> &&MAB,
Peter Collingbournef7b81db2018-05-18 18:26:45 +0000510 std::unique_ptr<MCObjectWriter> &&OW,
Lang Hames2241ffa2017-10-11 23:34:47 +0000511 std::unique_ptr<MCCodeEmitter> &&CE,
Rafael Espindola36a15cb2015-03-20 20:00:01 +0000512 bool RelaxAll, bool DWARFMustBeAtTheEnd,
Tim Northoverc3988b42014-03-29 07:05:06 +0000513 bool LabelSections) {
Lang Hames2241ffa2017-10-11 23:34:47 +0000514 MCMachOStreamer *S =
Peter Collingbournef7b81db2018-05-18 18:26:45 +0000515 new MCMachOStreamer(Context, std::move(MAB), std::move(OW), std::move(CE),
Lang Hames2241ffa2017-10-11 23:34:47 +0000516 DWARFMustBeAtTheEnd, LabelSections);
Matthias Braun5dd72ad2017-12-14 03:59:24 +0000517 const Triple &Target = Context.getObjectFileInfo()->getTargetTriple();
Fangrui Songa55daa12020-02-14 18:16:24 -0800518 S->emitVersionForTarget(Target, Context.getObjectFileInfo()->getSDKVersion());
Daniel Dunbard821f4a2010-03-25 22:49:09 +0000519 if (RelaxAll)
520 S->getAssembler().setRelaxAll(true);
521 return S;
Daniel Dunbar3016db32009-08-21 09:11:24 +0000522}