MCMachOStreamer.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576
  1. //===- MCMachOStreamer.cpp - MachO Streamer -------------------------------===//
  2. //
  3. // 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
  6. //
  7. //===----------------------------------------------------------------------===//
  8. #include "llvm/ADT/DenseMap.h"
  9. #include "llvm/ADT/SmallString.h"
  10. #include "llvm/ADT/SmallVector.h"
  11. #include "llvm/ADT/StringRef.h"
  12. #include "llvm/ADT/Triple.h"
  13. #include "llvm/MC/MCAsmBackend.h"
  14. #include "llvm/MC/MCAssembler.h"
  15. #include "llvm/MC/MCCodeEmitter.h"
  16. #include "llvm/MC/MCContext.h"
  17. #include "llvm/MC/MCDirectives.h"
  18. #include "llvm/MC/MCExpr.h"
  19. #include "llvm/MC/MCFixup.h"
  20. #include "llvm/MC/MCFragment.h"
  21. #include "llvm/MC/MCInst.h"
  22. #include "llvm/MC/MCLinkerOptimizationHint.h"
  23. #include "llvm/MC/MCObjectFileInfo.h"
  24. #include "llvm/MC/MCObjectStreamer.h"
  25. #include "llvm/MC/MCObjectWriter.h"
  26. #include "llvm/MC/MCSection.h"
  27. #include "llvm/MC/MCSectionMachO.h"
  28. #include "llvm/MC/MCStreamer.h"
  29. #include "llvm/MC/MCSymbol.h"
  30. #include "llvm/MC/MCSymbolMachO.h"
  31. #include "llvm/MC/MCValue.h"
  32. #include "llvm/MC/TargetRegistry.h"
  33. #include "llvm/Support/Casting.h"
  34. #include "llvm/Support/ErrorHandling.h"
  35. #include "llvm/Support/raw_ostream.h"
  36. #include <cassert>
  37. #include <vector>
  38. using namespace llvm;
  39. namespace {
  40. class MCMachOStreamer : public MCObjectStreamer {
  41. private:
  42. /// LabelSections - true if each section change should emit a linker local
  43. /// label for use in relocations for assembler local references. Obviates the
  44. /// need for local relocations. False by default.
  45. bool LabelSections;
  46. bool DWARFMustBeAtTheEnd;
  47. bool CreatedADWARFSection;
  48. /// HasSectionLabel - map of which sections have already had a non-local
  49. /// label emitted to them. Used so we don't emit extraneous linker local
  50. /// labels in the middle of the section.
  51. DenseMap<const MCSection*, bool> HasSectionLabel;
  52. void emitInstToData(const MCInst &Inst, const MCSubtargetInfo &STI) override;
  53. void emitDataRegion(DataRegionData::KindTy Kind);
  54. void emitDataRegionEnd();
  55. public:
  56. MCMachOStreamer(MCContext &Context, std::unique_ptr<MCAsmBackend> MAB,
  57. std::unique_ptr<MCObjectWriter> OW,
  58. std::unique_ptr<MCCodeEmitter> Emitter,
  59. bool DWARFMustBeAtTheEnd, bool label)
  60. : MCObjectStreamer(Context, std::move(MAB), std::move(OW),
  61. std::move(Emitter)),
  62. LabelSections(label), DWARFMustBeAtTheEnd(DWARFMustBeAtTheEnd),
  63. CreatedADWARFSection(false) {}
  64. /// state management
  65. void reset() override {
  66. CreatedADWARFSection = false;
  67. HasSectionLabel.clear();
  68. MCObjectStreamer::reset();
  69. }
  70. /// @name MCStreamer Interface
  71. /// @{
  72. void changeSection(MCSection *Sect, const MCExpr *Subsect) override;
  73. void emitLabel(MCSymbol *Symbol, SMLoc Loc = SMLoc()) override;
  74. void emitAssignment(MCSymbol *Symbol, const MCExpr *Value) override;
  75. void emitEHSymAttributes(const MCSymbol *Symbol, MCSymbol *EHSymbol) override;
  76. void emitAssemblerFlag(MCAssemblerFlag Flag) override;
  77. void emitLinkerOptions(ArrayRef<std::string> Options) override;
  78. void emitDataRegion(MCDataRegionType Kind) override;
  79. void emitVersionMin(MCVersionMinType Kind, unsigned Major, unsigned Minor,
  80. unsigned Update, VersionTuple SDKVersion) override;
  81. void emitBuildVersion(unsigned Platform, unsigned Major, unsigned Minor,
  82. unsigned Update, VersionTuple SDKVersion) override;
  83. void emitDarwinTargetVariantBuildVersion(unsigned Platform, unsigned Major,
  84. unsigned Minor, unsigned Update,
  85. VersionTuple SDKVersion) override;
  86. void emitThumbFunc(MCSymbol *Func) override;
  87. bool emitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute) override;
  88. void emitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) override;
  89. void emitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
  90. unsigned ByteAlignment) override;
  91. void emitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size,
  92. unsigned ByteAlignment) override;
  93. void emitZerofill(MCSection *Section, MCSymbol *Symbol = nullptr,
  94. uint64_t Size = 0, unsigned ByteAlignment = 0,
  95. SMLoc Loc = SMLoc()) override;
  96. void emitTBSSSymbol(MCSection *Section, MCSymbol *Symbol, uint64_t Size,
  97. unsigned ByteAlignment = 0) override;
  98. void emitIdent(StringRef IdentString) override {
  99. llvm_unreachable("macho doesn't support this directive");
  100. }
  101. void emitLOHDirective(MCLOHType Kind, const MCLOHArgs &Args) override {
  102. getAssembler().getLOHContainer().addDirective(Kind, Args);
  103. }
  104. void emitCGProfileEntry(const MCSymbolRefExpr *From,
  105. const MCSymbolRefExpr *To, uint64_t Count) override {
  106. if (!From->getSymbol().isTemporary() && !To->getSymbol().isTemporary())
  107. getAssembler().CGProfile.push_back({From, To, Count});
  108. }
  109. void finishImpl() override;
  110. void finalizeCGProfileEntry(const MCSymbolRefExpr *&SRE);
  111. void finalizeCGProfile();
  112. };
  113. } // end anonymous namespace.
  114. static bool canGoAfterDWARF(const MCSectionMachO &MSec) {
  115. // These sections are created by the assembler itself after the end of
  116. // the .s file.
  117. StringRef SegName = MSec.getSegmentName();
  118. StringRef SecName = MSec.getName();
  119. if (SegName == "__LD" && SecName == "__compact_unwind")
  120. return true;
  121. if (SegName == "__IMPORT") {
  122. if (SecName == "__jump_table")
  123. return true;
  124. if (SecName == "__pointers")
  125. return true;
  126. }
  127. if (SegName == "__TEXT" && SecName == "__eh_frame")
  128. return true;
  129. if (SegName == "__DATA" && (SecName == "__nl_symbol_ptr" ||
  130. SecName == "__thread_ptr"))
  131. return true;
  132. if (SegName == "__LLVM" && SecName == "__cg_profile")
  133. return true;
  134. return false;
  135. }
  136. void MCMachOStreamer::changeSection(MCSection *Section,
  137. const MCExpr *Subsection) {
  138. // Change the section normally.
  139. bool Created = changeSectionImpl(Section, Subsection);
  140. const MCSectionMachO &MSec = *cast<MCSectionMachO>(Section);
  141. StringRef SegName = MSec.getSegmentName();
  142. if (SegName == "__DWARF")
  143. CreatedADWARFSection = true;
  144. else if (Created && DWARFMustBeAtTheEnd && !canGoAfterDWARF(MSec))
  145. assert((!CreatedADWARFSection ||
  146. Section == getContext().getObjectFileInfo()->getStackMapSection())
  147. && "Creating regular section after DWARF");
  148. // Output a linker-local symbol so we don't need section-relative local
  149. // relocations. The linker hates us when we do that.
  150. if (LabelSections && !HasSectionLabel[Section] &&
  151. !Section->getBeginSymbol()) {
  152. MCSymbol *Label = getContext().createLinkerPrivateTempSymbol();
  153. Section->setBeginSymbol(Label);
  154. HasSectionLabel[Section] = true;
  155. }
  156. }
  157. void MCMachOStreamer::emitEHSymAttributes(const MCSymbol *Symbol,
  158. MCSymbol *EHSymbol) {
  159. getAssembler().registerSymbol(*Symbol);
  160. if (Symbol->isExternal())
  161. emitSymbolAttribute(EHSymbol, MCSA_Global);
  162. if (cast<MCSymbolMachO>(Symbol)->isWeakDefinition())
  163. emitSymbolAttribute(EHSymbol, MCSA_WeakDefinition);
  164. if (Symbol->isPrivateExtern())
  165. emitSymbolAttribute(EHSymbol, MCSA_PrivateExtern);
  166. }
  167. void MCMachOStreamer::emitLabel(MCSymbol *Symbol, SMLoc Loc) {
  168. // We have to create a new fragment if this is an atom defining symbol,
  169. // fragments cannot span atoms.
  170. if (getAssembler().isSymbolLinkerVisible(*Symbol))
  171. insert(new MCDataFragment());
  172. MCObjectStreamer::emitLabel(Symbol, Loc);
  173. // This causes the reference type flag to be cleared. Darwin 'as' was "trying"
  174. // to clear the weak reference and weak definition bits too, but the
  175. // implementation was buggy. For now we just try to match 'as', for
  176. // diffability.
  177. //
  178. // FIXME: Cleanup this code, these bits should be emitted based on semantic
  179. // properties, not on the order of definition, etc.
  180. cast<MCSymbolMachO>(Symbol)->clearReferenceType();
  181. }
  182. void MCMachOStreamer::emitAssignment(MCSymbol *Symbol, const MCExpr *Value) {
  183. MCValue Res;
  184. if (Value->evaluateAsRelocatable(Res, nullptr, nullptr)) {
  185. if (const MCSymbolRefExpr *SymAExpr = Res.getSymA()) {
  186. const MCSymbol &SymA = SymAExpr->getSymbol();
  187. if (!Res.getSymB() && (SymA.getName() == "" || Res.getConstant() != 0))
  188. cast<MCSymbolMachO>(Symbol)->setAltEntry();
  189. }
  190. }
  191. MCObjectStreamer::emitAssignment(Symbol, Value);
  192. }
  193. void MCMachOStreamer::emitDataRegion(DataRegionData::KindTy Kind) {
  194. // Create a temporary label to mark the start of the data region.
  195. MCSymbol *Start = getContext().createTempSymbol();
  196. emitLabel(Start);
  197. // Record the region for the object writer to use.
  198. DataRegionData Data = { Kind, Start, nullptr };
  199. std::vector<DataRegionData> &Regions = getAssembler().getDataRegions();
  200. Regions.push_back(Data);
  201. }
  202. void MCMachOStreamer::emitDataRegionEnd() {
  203. std::vector<DataRegionData> &Regions = getAssembler().getDataRegions();
  204. assert(!Regions.empty() && "Mismatched .end_data_region!");
  205. DataRegionData &Data = Regions.back();
  206. assert(!Data.End && "Mismatched .end_data_region!");
  207. // Create a temporary label to mark the end of the data region.
  208. Data.End = getContext().createTempSymbol();
  209. emitLabel(Data.End);
  210. }
  211. void MCMachOStreamer::emitAssemblerFlag(MCAssemblerFlag Flag) {
  212. // Let the target do whatever target specific stuff it needs to do.
  213. getAssembler().getBackend().handleAssemblerFlag(Flag);
  214. // Do any generic stuff we need to do.
  215. switch (Flag) {
  216. case MCAF_SyntaxUnified: return; // no-op here.
  217. case MCAF_Code16: return; // Change parsing mode; no-op here.
  218. case MCAF_Code32: return; // Change parsing mode; no-op here.
  219. case MCAF_Code64: return; // Change parsing mode; no-op here.
  220. case MCAF_SubsectionsViaSymbols:
  221. getAssembler().setSubsectionsViaSymbols(true);
  222. return;
  223. }
  224. }
  225. void MCMachOStreamer::emitLinkerOptions(ArrayRef<std::string> Options) {
  226. getAssembler().getLinkerOptions().push_back(Options);
  227. }
  228. void MCMachOStreamer::emitDataRegion(MCDataRegionType Kind) {
  229. switch (Kind) {
  230. case MCDR_DataRegion:
  231. emitDataRegion(DataRegionData::Data);
  232. return;
  233. case MCDR_DataRegionJT8:
  234. emitDataRegion(DataRegionData::JumpTable8);
  235. return;
  236. case MCDR_DataRegionJT16:
  237. emitDataRegion(DataRegionData::JumpTable16);
  238. return;
  239. case MCDR_DataRegionJT32:
  240. emitDataRegion(DataRegionData::JumpTable32);
  241. return;
  242. case MCDR_DataRegionEnd:
  243. emitDataRegionEnd();
  244. return;
  245. }
  246. }
  247. void MCMachOStreamer::emitVersionMin(MCVersionMinType Kind, unsigned Major,
  248. unsigned Minor, unsigned Update,
  249. VersionTuple SDKVersion) {
  250. getAssembler().setVersionMin(Kind, Major, Minor, Update, SDKVersion);
  251. }
  252. void MCMachOStreamer::emitBuildVersion(unsigned Platform, unsigned Major,
  253. unsigned Minor, unsigned Update,
  254. VersionTuple SDKVersion) {
  255. getAssembler().setBuildVersion((MachO::PlatformType)Platform, Major, Minor,
  256. Update, SDKVersion);
  257. }
  258. void MCMachOStreamer::emitDarwinTargetVariantBuildVersion(
  259. unsigned Platform, unsigned Major, unsigned Minor, unsigned Update,
  260. VersionTuple SDKVersion) {
  261. getAssembler().setDarwinTargetVariantBuildVersion(
  262. (MachO::PlatformType)Platform, Major, Minor, Update, SDKVersion);
  263. }
  264. void MCMachOStreamer::emitThumbFunc(MCSymbol *Symbol) {
  265. // Remember that the function is a thumb function. Fixup and relocation
  266. // values will need adjusted.
  267. getAssembler().setIsThumbFunc(Symbol);
  268. cast<MCSymbolMachO>(Symbol)->setThumbFunc();
  269. }
  270. bool MCMachOStreamer::emitSymbolAttribute(MCSymbol *Sym,
  271. MCSymbolAttr Attribute) {
  272. MCSymbolMachO *Symbol = cast<MCSymbolMachO>(Sym);
  273. // Indirect symbols are handled differently, to match how 'as' handles
  274. // them. This makes writing matching .o files easier.
  275. if (Attribute == MCSA_IndirectSymbol) {
  276. // Note that we intentionally cannot use the symbol data here; this is
  277. // important for matching the string table that 'as' generates.
  278. IndirectSymbolData ISD;
  279. ISD.Symbol = Symbol;
  280. ISD.Section = getCurrentSectionOnly();
  281. getAssembler().getIndirectSymbols().push_back(ISD);
  282. return true;
  283. }
  284. // Adding a symbol attribute always introduces the symbol, note that an
  285. // important side effect of calling registerSymbol here is to register
  286. // the symbol with the assembler.
  287. getAssembler().registerSymbol(*Symbol);
  288. // The implementation of symbol attributes is designed to match 'as', but it
  289. // leaves much to desired. It doesn't really make sense to arbitrarily add and
  290. // remove flags, but 'as' allows this (in particular, see .desc).
  291. //
  292. // In the future it might be worth trying to make these operations more well
  293. // defined.
  294. switch (Attribute) {
  295. case MCSA_Invalid:
  296. case MCSA_ELF_TypeFunction:
  297. case MCSA_ELF_TypeIndFunction:
  298. case MCSA_ELF_TypeObject:
  299. case MCSA_ELF_TypeTLS:
  300. case MCSA_ELF_TypeCommon:
  301. case MCSA_ELF_TypeNoType:
  302. case MCSA_ELF_TypeGnuUniqueObject:
  303. case MCSA_Extern:
  304. case MCSA_Hidden:
  305. case MCSA_IndirectSymbol:
  306. case MCSA_Internal:
  307. case MCSA_Protected:
  308. case MCSA_Weak:
  309. case MCSA_Local:
  310. case MCSA_LGlobal:
  311. return false;
  312. case MCSA_Global:
  313. Symbol->setExternal(true);
  314. // This effectively clears the undefined lazy bit, in Darwin 'as', although
  315. // it isn't very consistent because it implements this as part of symbol
  316. // lookup.
  317. //
  318. // FIXME: Cleanup this code, these bits should be emitted based on semantic
  319. // properties, not on the order of definition, etc.
  320. Symbol->setReferenceTypeUndefinedLazy(false);
  321. break;
  322. case MCSA_LazyReference:
  323. // FIXME: This requires -dynamic.
  324. Symbol->setNoDeadStrip();
  325. if (Symbol->isUndefined())
  326. Symbol->setReferenceTypeUndefinedLazy(true);
  327. break;
  328. // Since .reference sets the no dead strip bit, it is equivalent to
  329. // .no_dead_strip in practice.
  330. case MCSA_Reference:
  331. case MCSA_NoDeadStrip:
  332. Symbol->setNoDeadStrip();
  333. break;
  334. case MCSA_SymbolResolver:
  335. Symbol->setSymbolResolver();
  336. break;
  337. case MCSA_AltEntry:
  338. Symbol->setAltEntry();
  339. break;
  340. case MCSA_PrivateExtern:
  341. Symbol->setExternal(true);
  342. Symbol->setPrivateExtern(true);
  343. break;
  344. case MCSA_WeakReference:
  345. // FIXME: This requires -dynamic.
  346. if (Symbol->isUndefined())
  347. Symbol->setWeakReference();
  348. break;
  349. case MCSA_WeakDefinition:
  350. // FIXME: 'as' enforces that this is defined and global. The manual claims
  351. // it has to be in a coalesced section, but this isn't enforced.
  352. Symbol->setWeakDefinition();
  353. break;
  354. case MCSA_WeakDefAutoPrivate:
  355. Symbol->setWeakDefinition();
  356. Symbol->setWeakReference();
  357. break;
  358. case MCSA_Cold:
  359. Symbol->setCold();
  360. break;
  361. }
  362. return true;
  363. }
  364. void MCMachOStreamer::emitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
  365. // Encode the 'desc' value into the lowest implementation defined bits.
  366. getAssembler().registerSymbol(*Symbol);
  367. cast<MCSymbolMachO>(Symbol)->setDesc(DescValue);
  368. }
  369. void MCMachOStreamer::emitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
  370. unsigned ByteAlignment) {
  371. // FIXME: Darwin 'as' does appear to allow redef of a .comm by itself.
  372. assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
  373. getAssembler().registerSymbol(*Symbol);
  374. Symbol->setExternal(true);
  375. Symbol->setCommon(Size, ByteAlignment);
  376. }
  377. void MCMachOStreamer::emitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size,
  378. unsigned ByteAlignment) {
  379. // '.lcomm' is equivalent to '.zerofill'.
  380. return emitZerofill(getContext().getObjectFileInfo()->getDataBSSSection(),
  381. Symbol, Size, ByteAlignment);
  382. }
  383. void MCMachOStreamer::emitZerofill(MCSection *Section, MCSymbol *Symbol,
  384. uint64_t Size, unsigned ByteAlignment,
  385. SMLoc Loc) {
  386. // On darwin all virtual sections have zerofill type. Disallow the usage of
  387. // .zerofill in non-virtual functions. If something similar is needed, use
  388. // .space or .zero.
  389. if (!Section->isVirtualSection()) {
  390. getContext().reportError(
  391. Loc, "The usage of .zerofill is restricted to sections of "
  392. "ZEROFILL type. Use .zero or .space instead.");
  393. return; // Early returning here shouldn't harm. EmitZeros should work on any
  394. // section.
  395. }
  396. PushSection();
  397. SwitchSection(Section);
  398. // The symbol may not be present, which only creates the section.
  399. if (Symbol) {
  400. emitValueToAlignment(ByteAlignment, 0, 1, 0);
  401. emitLabel(Symbol);
  402. emitZeros(Size);
  403. }
  404. PopSection();
  405. }
  406. // This should always be called with the thread local bss section. Like the
  407. // .zerofill directive this doesn't actually switch sections on us.
  408. void MCMachOStreamer::emitTBSSSymbol(MCSection *Section, MCSymbol *Symbol,
  409. uint64_t Size, unsigned ByteAlignment) {
  410. emitZerofill(Section, Symbol, Size, ByteAlignment);
  411. }
  412. void MCMachOStreamer::emitInstToData(const MCInst &Inst,
  413. const MCSubtargetInfo &STI) {
  414. MCDataFragment *DF = getOrCreateDataFragment();
  415. SmallVector<MCFixup, 4> Fixups;
  416. SmallString<256> Code;
  417. raw_svector_ostream VecOS(Code);
  418. getAssembler().getEmitter().encodeInstruction(Inst, VecOS, Fixups, STI);
  419. // Add the fixups and data.
  420. for (MCFixup &Fixup : Fixups) {
  421. Fixup.setOffset(Fixup.getOffset() + DF->getContents().size());
  422. DF->getFixups().push_back(Fixup);
  423. }
  424. DF->setHasInstructions(STI);
  425. DF->getContents().append(Code.begin(), Code.end());
  426. }
  427. void MCMachOStreamer::finishImpl() {
  428. emitFrames(&getAssembler().getBackend());
  429. // We have to set the fragment atom associations so we can relax properly for
  430. // Mach-O.
  431. // First, scan the symbol table to build a lookup table from fragments to
  432. // defining symbols.
  433. DenseMap<const MCFragment *, const MCSymbol *> DefiningSymbolMap;
  434. for (const MCSymbol &Symbol : getAssembler().symbols()) {
  435. if (getAssembler().isSymbolLinkerVisible(Symbol) && Symbol.isInSection() &&
  436. !Symbol.isVariable()) {
  437. // An atom defining symbol should never be internal to a fragment.
  438. assert(Symbol.getOffset() == 0 &&
  439. "Invalid offset in atom defining symbol!");
  440. DefiningSymbolMap[Symbol.getFragment()] = &Symbol;
  441. }
  442. }
  443. // Set the fragment atom associations by tracking the last seen atom defining
  444. // symbol.
  445. for (MCSection &Sec : getAssembler()) {
  446. const MCSymbol *CurrentAtom = nullptr;
  447. for (MCFragment &Frag : Sec) {
  448. if (const MCSymbol *Symbol = DefiningSymbolMap.lookup(&Frag))
  449. CurrentAtom = Symbol;
  450. Frag.setAtom(CurrentAtom);
  451. }
  452. }
  453. finalizeCGProfile();
  454. this->MCObjectStreamer::finishImpl();
  455. }
  456. void MCMachOStreamer::finalizeCGProfileEntry(const MCSymbolRefExpr *&SRE) {
  457. const MCSymbol *S = &SRE->getSymbol();
  458. bool Created;
  459. getAssembler().registerSymbol(*S, &Created);
  460. if (Created)
  461. S->setExternal(true);
  462. }
  463. void MCMachOStreamer::finalizeCGProfile() {
  464. MCAssembler &Asm = getAssembler();
  465. if (Asm.CGProfile.empty())
  466. return;
  467. for (MCAssembler::CGProfileEntry &E : Asm.CGProfile) {
  468. finalizeCGProfileEntry(E.From);
  469. finalizeCGProfileEntry(E.To);
  470. }
  471. // We can't write the section out until symbol indices are finalized which
  472. // doesn't happen until after section layout. We need to create the section
  473. // and set its size now so that it's accounted for in layout.
  474. MCSection *CGProfileSection = Asm.getContext().getMachOSection(
  475. "__LLVM", "__cg_profile", 0, SectionKind::getMetadata());
  476. Asm.registerSection(*CGProfileSection);
  477. auto *Frag = new MCDataFragment(CGProfileSection);
  478. // For each entry, reserve space for 2 32-bit indices and a 64-bit count.
  479. size_t SectionBytes =
  480. Asm.CGProfile.size() * (2 * sizeof(uint32_t) + sizeof(uint64_t));
  481. Frag->getContents().resize(SectionBytes);
  482. }
  483. MCStreamer *llvm::createMachOStreamer(MCContext &Context,
  484. std::unique_ptr<MCAsmBackend> &&MAB,
  485. std::unique_ptr<MCObjectWriter> &&OW,
  486. std::unique_ptr<MCCodeEmitter> &&CE,
  487. bool RelaxAll, bool DWARFMustBeAtTheEnd,
  488. bool LabelSections) {
  489. MCMachOStreamer *S =
  490. new MCMachOStreamer(Context, std::move(MAB), std::move(OW), std::move(CE),
  491. DWARFMustBeAtTheEnd, LabelSections);
  492. const Triple &Target = Context.getTargetTriple();
  493. S->emitVersionForTarget(
  494. Target, Context.getObjectFileInfo()->getSDKVersion(),
  495. Context.getObjectFileInfo()->getDarwinTargetVariantTriple(),
  496. Context.getObjectFileInfo()->getDarwinTargetVariantSDKVersion());
  497. if (RelaxAll)
  498. S->getAssembler().setRelaxAll(true);
  499. return S;
  500. }