MCMachOStreamer.cpp 19 KB

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