ARMELFStreamer.cpp 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399
  1. //===- lib/MC/ARMELFStreamer.cpp - ELF Object Output for ARM --------------===//
  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. //
  9. // This file assembles .s files and emits ARM ELF .o object files. Different
  10. // from generic ELF streamer in emitting mapping symbols ($a, $t and $d) to
  11. // delimit regions of data and code.
  12. //
  13. //===----------------------------------------------------------------------===//
  14. #include "ARMRegisterInfo.h"
  15. #include "ARMUnwindOpAsm.h"
  16. #include "llvm/ADT/DenseMap.h"
  17. #include "llvm/ADT/SmallString.h"
  18. #include "llvm/ADT/SmallVector.h"
  19. #include "llvm/ADT/StringRef.h"
  20. #include "llvm/ADT/Triple.h"
  21. #include "llvm/ADT/Twine.h"
  22. #include "llvm/BinaryFormat/ELF.h"
  23. #include "llvm/MC/MCAsmBackend.h"
  24. #include "llvm/MC/MCAsmInfo.h"
  25. #include "llvm/MC/MCAssembler.h"
  26. #include "llvm/MC/MCCodeEmitter.h"
  27. #include "llvm/MC/MCContext.h"
  28. #include "llvm/MC/MCELFStreamer.h"
  29. #include "llvm/MC/MCExpr.h"
  30. #include "llvm/MC/MCFixup.h"
  31. #include "llvm/MC/MCFragment.h"
  32. #include "llvm/MC/MCInst.h"
  33. #include "llvm/MC/MCInstPrinter.h"
  34. #include "llvm/MC/MCObjectWriter.h"
  35. #include "llvm/MC/MCRegisterInfo.h"
  36. #include "llvm/MC/MCSection.h"
  37. #include "llvm/MC/MCSectionELF.h"
  38. #include "llvm/MC/MCStreamer.h"
  39. #include "llvm/MC/MCSubtargetInfo.h"
  40. #include "llvm/MC/MCSymbol.h"
  41. #include "llvm/MC/MCSymbolELF.h"
  42. #include "llvm/MC/SectionKind.h"
  43. #include "llvm/Support/ARMBuildAttributes.h"
  44. #include "llvm/Support/ARMEHABI.h"
  45. #include "llvm/Support/Casting.h"
  46. #include "llvm/Support/ErrorHandling.h"
  47. #include "llvm/Support/FormattedStream.h"
  48. #include "llvm/Support/TargetParser.h"
  49. #include "llvm/Support/raw_ostream.h"
  50. #include <algorithm>
  51. #include <cassert>
  52. #include <climits>
  53. #include <cstddef>
  54. #include <cstdint>
  55. #include <string>
  56. using namespace llvm;
  57. static std::string GetAEABIUnwindPersonalityName(unsigned Index) {
  58. assert(Index < ARM::EHABI::NUM_PERSONALITY_INDEX &&
  59. "Invalid personality index");
  60. return (Twine("__aeabi_unwind_cpp_pr") + Twine(Index)).str();
  61. }
  62. namespace {
  63. class ARMELFStreamer;
  64. class ARMTargetAsmStreamer : public ARMTargetStreamer {
  65. formatted_raw_ostream &OS;
  66. MCInstPrinter &InstPrinter;
  67. bool IsVerboseAsm;
  68. void emitFnStart() override;
  69. void emitFnEnd() override;
  70. void emitCantUnwind() override;
  71. void emitPersonality(const MCSymbol *Personality) override;
  72. void emitPersonalityIndex(unsigned Index) override;
  73. void emitHandlerData() override;
  74. void emitSetFP(unsigned FpReg, unsigned SpReg, int64_t Offset = 0) override;
  75. void emitMovSP(unsigned Reg, int64_t Offset = 0) override;
  76. void emitPad(int64_t Offset) override;
  77. void emitRegSave(const SmallVectorImpl<unsigned> &RegList,
  78. bool isVector) override;
  79. void emitUnwindRaw(int64_t Offset,
  80. const SmallVectorImpl<uint8_t> &Opcodes) override;
  81. void switchVendor(StringRef Vendor) override;
  82. void emitAttribute(unsigned Attribute, unsigned Value) override;
  83. void emitTextAttribute(unsigned Attribute, StringRef String) override;
  84. void emitIntTextAttribute(unsigned Attribute, unsigned IntValue,
  85. StringRef StringValue) override;
  86. void emitArch(ARM::ArchKind Arch) override;
  87. void emitArchExtension(uint64_t ArchExt) override;
  88. void emitObjectArch(ARM::ArchKind Arch) override;
  89. void emitFPU(unsigned FPU) override;
  90. void emitInst(uint32_t Inst, char Suffix = '\0') override;
  91. void finishAttributeSection() override;
  92. void AnnotateTLSDescriptorSequence(const MCSymbolRefExpr *SRE) override;
  93. void emitThumbSet(MCSymbol *Symbol, const MCExpr *Value) override;
  94. public:
  95. ARMTargetAsmStreamer(MCStreamer &S, formatted_raw_ostream &OS,
  96. MCInstPrinter &InstPrinter, bool VerboseAsm);
  97. };
  98. ARMTargetAsmStreamer::ARMTargetAsmStreamer(MCStreamer &S,
  99. formatted_raw_ostream &OS,
  100. MCInstPrinter &InstPrinter,
  101. bool VerboseAsm)
  102. : ARMTargetStreamer(S), OS(OS), InstPrinter(InstPrinter),
  103. IsVerboseAsm(VerboseAsm) {}
  104. void ARMTargetAsmStreamer::emitFnStart() { OS << "\t.fnstart\n"; }
  105. void ARMTargetAsmStreamer::emitFnEnd() { OS << "\t.fnend\n"; }
  106. void ARMTargetAsmStreamer::emitCantUnwind() { OS << "\t.cantunwind\n"; }
  107. void ARMTargetAsmStreamer::emitPersonality(const MCSymbol *Personality) {
  108. OS << "\t.personality " << Personality->getName() << '\n';
  109. }
  110. void ARMTargetAsmStreamer::emitPersonalityIndex(unsigned Index) {
  111. OS << "\t.personalityindex " << Index << '\n';
  112. }
  113. void ARMTargetAsmStreamer::emitHandlerData() { OS << "\t.handlerdata\n"; }
  114. void ARMTargetAsmStreamer::emitSetFP(unsigned FpReg, unsigned SpReg,
  115. int64_t Offset) {
  116. OS << "\t.setfp\t";
  117. InstPrinter.printRegName(OS, FpReg);
  118. OS << ", ";
  119. InstPrinter.printRegName(OS, SpReg);
  120. if (Offset)
  121. OS << ", #" << Offset;
  122. OS << '\n';
  123. }
  124. void ARMTargetAsmStreamer::emitMovSP(unsigned Reg, int64_t Offset) {
  125. assert((Reg != ARM::SP && Reg != ARM::PC) &&
  126. "the operand of .movsp cannot be either sp or pc");
  127. OS << "\t.movsp\t";
  128. InstPrinter.printRegName(OS, Reg);
  129. if (Offset)
  130. OS << ", #" << Offset;
  131. OS << '\n';
  132. }
  133. void ARMTargetAsmStreamer::emitPad(int64_t Offset) {
  134. OS << "\t.pad\t#" << Offset << '\n';
  135. }
  136. void ARMTargetAsmStreamer::emitRegSave(const SmallVectorImpl<unsigned> &RegList,
  137. bool isVector) {
  138. assert(RegList.size() && "RegList should not be empty");
  139. if (isVector)
  140. OS << "\t.vsave\t{";
  141. else
  142. OS << "\t.save\t{";
  143. InstPrinter.printRegName(OS, RegList[0]);
  144. for (unsigned i = 1, e = RegList.size(); i != e; ++i) {
  145. OS << ", ";
  146. InstPrinter.printRegName(OS, RegList[i]);
  147. }
  148. OS << "}\n";
  149. }
  150. void ARMTargetAsmStreamer::switchVendor(StringRef Vendor) {}
  151. void ARMTargetAsmStreamer::emitAttribute(unsigned Attribute, unsigned Value) {
  152. OS << "\t.eabi_attribute\t" << Attribute << ", " << Twine(Value);
  153. if (IsVerboseAsm) {
  154. StringRef Name = ELFAttrs::attrTypeAsString(
  155. Attribute, ARMBuildAttrs::getARMAttributeTags());
  156. if (!Name.empty())
  157. OS << "\t@ " << Name;
  158. }
  159. OS << "\n";
  160. }
  161. void ARMTargetAsmStreamer::emitTextAttribute(unsigned Attribute,
  162. StringRef String) {
  163. switch (Attribute) {
  164. case ARMBuildAttrs::CPU_name:
  165. OS << "\t.cpu\t" << String.lower();
  166. break;
  167. default:
  168. OS << "\t.eabi_attribute\t" << Attribute << ", \"" << String << "\"";
  169. if (IsVerboseAsm) {
  170. StringRef Name = ELFAttrs::attrTypeAsString(
  171. Attribute, ARMBuildAttrs::getARMAttributeTags());
  172. if (!Name.empty())
  173. OS << "\t@ " << Name;
  174. }
  175. break;
  176. }
  177. OS << "\n";
  178. }
  179. void ARMTargetAsmStreamer::emitIntTextAttribute(unsigned Attribute,
  180. unsigned IntValue,
  181. StringRef StringValue) {
  182. switch (Attribute) {
  183. default: llvm_unreachable("unsupported multi-value attribute in asm mode");
  184. case ARMBuildAttrs::compatibility:
  185. OS << "\t.eabi_attribute\t" << Attribute << ", " << IntValue;
  186. if (!StringValue.empty())
  187. OS << ", \"" << StringValue << "\"";
  188. if (IsVerboseAsm)
  189. OS << "\t@ "
  190. << ELFAttrs::attrTypeAsString(Attribute,
  191. ARMBuildAttrs::getARMAttributeTags());
  192. break;
  193. }
  194. OS << "\n";
  195. }
  196. void ARMTargetAsmStreamer::emitArch(ARM::ArchKind Arch) {
  197. OS << "\t.arch\t" << ARM::getArchName(Arch) << "\n";
  198. }
  199. void ARMTargetAsmStreamer::emitArchExtension(uint64_t ArchExt) {
  200. OS << "\t.arch_extension\t" << ARM::getArchExtName(ArchExt) << "\n";
  201. }
  202. void ARMTargetAsmStreamer::emitObjectArch(ARM::ArchKind Arch) {
  203. OS << "\t.object_arch\t" << ARM::getArchName(Arch) << '\n';
  204. }
  205. void ARMTargetAsmStreamer::emitFPU(unsigned FPU) {
  206. OS << "\t.fpu\t" << ARM::getFPUName(FPU) << "\n";
  207. }
  208. void ARMTargetAsmStreamer::finishAttributeSection() {}
  209. void
  210. ARMTargetAsmStreamer::AnnotateTLSDescriptorSequence(const MCSymbolRefExpr *S) {
  211. OS << "\t.tlsdescseq\t" << S->getSymbol().getName() << "\n";
  212. }
  213. void ARMTargetAsmStreamer::emitThumbSet(MCSymbol *Symbol, const MCExpr *Value) {
  214. const MCAsmInfo *MAI = Streamer.getContext().getAsmInfo();
  215. OS << "\t.thumb_set\t";
  216. Symbol->print(OS, MAI);
  217. OS << ", ";
  218. Value->print(OS, MAI);
  219. OS << '\n';
  220. }
  221. void ARMTargetAsmStreamer::emitInst(uint32_t Inst, char Suffix) {
  222. OS << "\t.inst";
  223. if (Suffix)
  224. OS << "." << Suffix;
  225. OS << "\t0x" << Twine::utohexstr(Inst) << "\n";
  226. }
  227. void ARMTargetAsmStreamer::emitUnwindRaw(int64_t Offset,
  228. const SmallVectorImpl<uint8_t> &Opcodes) {
  229. OS << "\t.unwind_raw " << Offset;
  230. for (uint8_t Opcode : Opcodes)
  231. OS << ", 0x" << Twine::utohexstr(Opcode);
  232. OS << '\n';
  233. }
  234. class ARMTargetELFStreamer : public ARMTargetStreamer {
  235. private:
  236. StringRef CurrentVendor;
  237. unsigned FPU = ARM::FK_INVALID;
  238. ARM::ArchKind Arch = ARM::ArchKind::INVALID;
  239. ARM::ArchKind EmittedArch = ARM::ArchKind::INVALID;
  240. MCSection *AttributeSection = nullptr;
  241. void emitArchDefaultAttributes();
  242. void emitFPUDefaultAttributes();
  243. ARMELFStreamer &getStreamer();
  244. void emitFnStart() override;
  245. void emitFnEnd() override;
  246. void emitCantUnwind() override;
  247. void emitPersonality(const MCSymbol *Personality) override;
  248. void emitPersonalityIndex(unsigned Index) override;
  249. void emitHandlerData() override;
  250. void emitSetFP(unsigned FpReg, unsigned SpReg, int64_t Offset = 0) override;
  251. void emitMovSP(unsigned Reg, int64_t Offset = 0) override;
  252. void emitPad(int64_t Offset) override;
  253. void emitRegSave(const SmallVectorImpl<unsigned> &RegList,
  254. bool isVector) override;
  255. void emitUnwindRaw(int64_t Offset,
  256. const SmallVectorImpl<uint8_t> &Opcodes) override;
  257. void switchVendor(StringRef Vendor) override;
  258. void emitAttribute(unsigned Attribute, unsigned Value) override;
  259. void emitTextAttribute(unsigned Attribute, StringRef String) override;
  260. void emitIntTextAttribute(unsigned Attribute, unsigned IntValue,
  261. StringRef StringValue) override;
  262. void emitArch(ARM::ArchKind Arch) override;
  263. void emitObjectArch(ARM::ArchKind Arch) override;
  264. void emitFPU(unsigned FPU) override;
  265. void emitInst(uint32_t Inst, char Suffix = '\0') override;
  266. void finishAttributeSection() override;
  267. void emitLabel(MCSymbol *Symbol) override;
  268. void AnnotateTLSDescriptorSequence(const MCSymbolRefExpr *SRE) override;
  269. void emitThumbSet(MCSymbol *Symbol, const MCExpr *Value) override;
  270. // Reset state between object emissions
  271. void reset() override;
  272. public:
  273. ARMTargetELFStreamer(MCStreamer &S)
  274. : ARMTargetStreamer(S), CurrentVendor("aeabi") {}
  275. };
  276. /// Extend the generic ELFStreamer class so that it can emit mapping symbols at
  277. /// the appropriate points in the object files. These symbols are defined in the
  278. /// ARM ELF ABI: infocenter.arm.com/help/topic/com.arm.../IHI0044D_aaelf.pdf.
  279. ///
  280. /// In brief: $a, $t or $d should be emitted at the start of each contiguous
  281. /// region of ARM code, Thumb code or data in a section. In practice, this
  282. /// emission does not rely on explicit assembler directives but on inherent
  283. /// properties of the directives doing the emission (e.g. ".byte" is data, "add
  284. /// r0, r0, r0" an instruction).
  285. ///
  286. /// As a result this system is orthogonal to the DataRegion infrastructure used
  287. /// by MachO. Beware!
  288. class ARMELFStreamer : public MCELFStreamer {
  289. public:
  290. friend class ARMTargetELFStreamer;
  291. ARMELFStreamer(MCContext &Context, std::unique_ptr<MCAsmBackend> TAB,
  292. std::unique_ptr<MCObjectWriter> OW,
  293. std::unique_ptr<MCCodeEmitter> Emitter, bool IsThumb,
  294. bool IsAndroid)
  295. : MCELFStreamer(Context, std::move(TAB), std::move(OW),
  296. std::move(Emitter)),
  297. IsThumb(IsThumb), IsAndroid(IsAndroid) {
  298. EHReset();
  299. }
  300. ~ARMELFStreamer() override = default;
  301. void finishImpl() override;
  302. // ARM exception handling directives
  303. void emitFnStart();
  304. void emitFnEnd();
  305. void emitCantUnwind();
  306. void emitPersonality(const MCSymbol *Per);
  307. void emitPersonalityIndex(unsigned index);
  308. void emitHandlerData();
  309. void emitSetFP(unsigned NewFpReg, unsigned NewSpReg, int64_t Offset = 0);
  310. void emitMovSP(unsigned Reg, int64_t Offset = 0);
  311. void emitPad(int64_t Offset);
  312. void emitRegSave(const SmallVectorImpl<unsigned> &RegList, bool isVector);
  313. void emitUnwindRaw(int64_t Offset, const SmallVectorImpl<uint8_t> &Opcodes);
  314. void emitFill(const MCExpr &NumBytes, uint64_t FillValue,
  315. SMLoc Loc) override {
  316. emitDataMappingSymbol();
  317. MCObjectStreamer::emitFill(NumBytes, FillValue, Loc);
  318. }
  319. void changeSection(MCSection *Section, const MCExpr *Subsection) override {
  320. LastMappingSymbols[getCurrentSection().first] = std::move(LastEMSInfo);
  321. MCELFStreamer::changeSection(Section, Subsection);
  322. auto LastMappingSymbol = LastMappingSymbols.find(Section);
  323. if (LastMappingSymbol != LastMappingSymbols.end()) {
  324. LastEMSInfo = std::move(LastMappingSymbol->second);
  325. return;
  326. }
  327. LastEMSInfo.reset(new ElfMappingSymbolInfo(SMLoc(), nullptr, 0));
  328. }
  329. /// This function is the one used to emit instruction data into the ELF
  330. /// streamer. We override it to add the appropriate mapping symbol if
  331. /// necessary.
  332. void emitInstruction(const MCInst &Inst,
  333. const MCSubtargetInfo &STI) override {
  334. if (IsThumb)
  335. EmitThumbMappingSymbol();
  336. else
  337. EmitARMMappingSymbol();
  338. MCELFStreamer::emitInstruction(Inst, STI);
  339. }
  340. void emitInst(uint32_t Inst, char Suffix) {
  341. unsigned Size;
  342. char Buffer[4];
  343. const bool LittleEndian = getContext().getAsmInfo()->isLittleEndian();
  344. switch (Suffix) {
  345. case '\0':
  346. Size = 4;
  347. assert(!IsThumb);
  348. EmitARMMappingSymbol();
  349. for (unsigned II = 0, IE = Size; II != IE; II++) {
  350. const unsigned I = LittleEndian ? (Size - II - 1) : II;
  351. Buffer[Size - II - 1] = uint8_t(Inst >> I * CHAR_BIT);
  352. }
  353. break;
  354. case 'n':
  355. case 'w':
  356. Size = (Suffix == 'n' ? 2 : 4);
  357. assert(IsThumb);
  358. EmitThumbMappingSymbol();
  359. // Thumb wide instructions are emitted as a pair of 16-bit words of the
  360. // appropriate endianness.
  361. for (unsigned II = 0, IE = Size; II != IE; II = II + 2) {
  362. const unsigned I0 = LittleEndian ? II + 0 : II + 1;
  363. const unsigned I1 = LittleEndian ? II + 1 : II + 0;
  364. Buffer[Size - II - 2] = uint8_t(Inst >> I0 * CHAR_BIT);
  365. Buffer[Size - II - 1] = uint8_t(Inst >> I1 * CHAR_BIT);
  366. }
  367. break;
  368. default:
  369. llvm_unreachable("Invalid Suffix");
  370. }
  371. MCELFStreamer::emitBytes(StringRef(Buffer, Size));
  372. }
  373. /// This is one of the functions used to emit data into an ELF section, so the
  374. /// ARM streamer overrides it to add the appropriate mapping symbol ($d) if
  375. /// necessary.
  376. void emitBytes(StringRef Data) override {
  377. emitDataMappingSymbol();
  378. MCELFStreamer::emitBytes(Data);
  379. }
  380. void FlushPendingMappingSymbol() {
  381. if (!LastEMSInfo->hasInfo())
  382. return;
  383. ElfMappingSymbolInfo *EMS = LastEMSInfo.get();
  384. EmitMappingSymbol("$d", EMS->Loc, EMS->F, EMS->Offset);
  385. EMS->resetInfo();
  386. }
  387. /// This is one of the functions used to emit data into an ELF section, so the
  388. /// ARM streamer overrides it to add the appropriate mapping symbol ($d) if
  389. /// necessary.
  390. void emitValueImpl(const MCExpr *Value, unsigned Size, SMLoc Loc) override {
  391. if (const MCSymbolRefExpr *SRE = dyn_cast_or_null<MCSymbolRefExpr>(Value)) {
  392. if (SRE->getKind() == MCSymbolRefExpr::VK_ARM_SBREL && !(Size == 4)) {
  393. getContext().reportError(Loc, "relocated expression must be 32-bit");
  394. return;
  395. }
  396. getOrCreateDataFragment();
  397. }
  398. emitDataMappingSymbol();
  399. MCELFStreamer::emitValueImpl(Value, Size, Loc);
  400. }
  401. void emitAssemblerFlag(MCAssemblerFlag Flag) override {
  402. MCELFStreamer::emitAssemblerFlag(Flag);
  403. switch (Flag) {
  404. case MCAF_SyntaxUnified:
  405. return; // no-op here.
  406. case MCAF_Code16:
  407. IsThumb = true;
  408. return; // Change to Thumb mode
  409. case MCAF_Code32:
  410. IsThumb = false;
  411. return; // Change to ARM mode
  412. case MCAF_Code64:
  413. return;
  414. case MCAF_SubsectionsViaSymbols:
  415. return;
  416. }
  417. }
  418. /// If a label is defined before the .type directive sets the label's type
  419. /// then the label can't be recorded as thumb function when the label is
  420. /// defined. We override emitSymbolAttribute() which is called as part of the
  421. /// parsing of .type so that if the symbol has already been defined we can
  422. /// record the label as Thumb. FIXME: there is a corner case where the state
  423. /// is changed in between the label definition and the .type directive, this
  424. /// is not expected to occur in practice and handling it would require the
  425. /// backend to track IsThumb for every label.
  426. bool emitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute) override {
  427. bool Val = MCELFStreamer::emitSymbolAttribute(Symbol, Attribute);
  428. if (!IsThumb)
  429. return Val;
  430. unsigned Type = cast<MCSymbolELF>(Symbol)->getType();
  431. if ((Type == ELF::STT_FUNC || Type == ELF::STT_GNU_IFUNC) &&
  432. Symbol->isDefined())
  433. getAssembler().setIsThumbFunc(Symbol);
  434. return Val;
  435. };
  436. private:
  437. enum ElfMappingSymbol {
  438. EMS_None,
  439. EMS_ARM,
  440. EMS_Thumb,
  441. EMS_Data
  442. };
  443. struct ElfMappingSymbolInfo {
  444. explicit ElfMappingSymbolInfo(SMLoc Loc, MCFragment *F, uint64_t O)
  445. : Loc(Loc), F(F), Offset(O), State(EMS_None) {}
  446. void resetInfo() {
  447. F = nullptr;
  448. Offset = 0;
  449. }
  450. bool hasInfo() { return F != nullptr; }
  451. SMLoc Loc;
  452. MCFragment *F;
  453. uint64_t Offset;
  454. ElfMappingSymbol State;
  455. };
  456. void emitDataMappingSymbol() {
  457. if (LastEMSInfo->State == EMS_Data)
  458. return;
  459. else if (LastEMSInfo->State == EMS_None) {
  460. // This is a tentative symbol, it won't really be emitted until it's
  461. // actually needed.
  462. ElfMappingSymbolInfo *EMS = LastEMSInfo.get();
  463. auto *DF = dyn_cast_or_null<MCDataFragment>(getCurrentFragment());
  464. if (!DF)
  465. return;
  466. EMS->Loc = SMLoc();
  467. EMS->F = getCurrentFragment();
  468. EMS->Offset = DF->getContents().size();
  469. LastEMSInfo->State = EMS_Data;
  470. return;
  471. }
  472. EmitMappingSymbol("$d");
  473. LastEMSInfo->State = EMS_Data;
  474. }
  475. void EmitThumbMappingSymbol() {
  476. if (LastEMSInfo->State == EMS_Thumb)
  477. return;
  478. FlushPendingMappingSymbol();
  479. EmitMappingSymbol("$t");
  480. LastEMSInfo->State = EMS_Thumb;
  481. }
  482. void EmitARMMappingSymbol() {
  483. if (LastEMSInfo->State == EMS_ARM)
  484. return;
  485. FlushPendingMappingSymbol();
  486. EmitMappingSymbol("$a");
  487. LastEMSInfo->State = EMS_ARM;
  488. }
  489. void EmitMappingSymbol(StringRef Name) {
  490. auto *Symbol = cast<MCSymbolELF>(getContext().getOrCreateSymbol(
  491. Name + "." + Twine(MappingSymbolCounter++)));
  492. emitLabel(Symbol);
  493. Symbol->setType(ELF::STT_NOTYPE);
  494. Symbol->setBinding(ELF::STB_LOCAL);
  495. }
  496. void EmitMappingSymbol(StringRef Name, SMLoc Loc, MCFragment *F,
  497. uint64_t Offset) {
  498. auto *Symbol = cast<MCSymbolELF>(getContext().getOrCreateSymbol(
  499. Name + "." + Twine(MappingSymbolCounter++)));
  500. emitLabelAtPos(Symbol, Loc, F, Offset);
  501. Symbol->setType(ELF::STT_NOTYPE);
  502. Symbol->setBinding(ELF::STB_LOCAL);
  503. }
  504. void emitThumbFunc(MCSymbol *Func) override {
  505. getAssembler().setIsThumbFunc(Func);
  506. emitSymbolAttribute(Func, MCSA_ELF_TypeFunction);
  507. }
  508. // Helper functions for ARM exception handling directives
  509. void EHReset();
  510. // Reset state between object emissions
  511. void reset() override;
  512. void EmitPersonalityFixup(StringRef Name);
  513. void FlushPendingOffset();
  514. void FlushUnwindOpcodes(bool NoHandlerData);
  515. void SwitchToEHSection(StringRef Prefix, unsigned Type, unsigned Flags,
  516. SectionKind Kind, const MCSymbol &Fn);
  517. void SwitchToExTabSection(const MCSymbol &FnStart);
  518. void SwitchToExIdxSection(const MCSymbol &FnStart);
  519. void EmitFixup(const MCExpr *Expr, MCFixupKind Kind);
  520. bool IsThumb;
  521. bool IsAndroid;
  522. int64_t MappingSymbolCounter = 0;
  523. DenseMap<const MCSection *, std::unique_ptr<ElfMappingSymbolInfo>>
  524. LastMappingSymbols;
  525. std::unique_ptr<ElfMappingSymbolInfo> LastEMSInfo;
  526. // ARM Exception Handling Frame Information
  527. MCSymbol *ExTab;
  528. MCSymbol *FnStart;
  529. const MCSymbol *Personality;
  530. unsigned PersonalityIndex;
  531. unsigned FPReg; // Frame pointer register
  532. int64_t FPOffset; // Offset: (final frame pointer) - (initial $sp)
  533. int64_t SPOffset; // Offset: (final $sp) - (initial $sp)
  534. int64_t PendingOffset; // Offset: (final $sp) - (emitted $sp)
  535. bool UsedFP;
  536. bool CantUnwind;
  537. SmallVector<uint8_t, 64> Opcodes;
  538. UnwindOpcodeAssembler UnwindOpAsm;
  539. };
  540. } // end anonymous namespace
  541. ARMELFStreamer &ARMTargetELFStreamer::getStreamer() {
  542. return static_cast<ARMELFStreamer &>(Streamer);
  543. }
  544. void ARMTargetELFStreamer::emitFnStart() { getStreamer().emitFnStart(); }
  545. void ARMTargetELFStreamer::emitFnEnd() { getStreamer().emitFnEnd(); }
  546. void ARMTargetELFStreamer::emitCantUnwind() { getStreamer().emitCantUnwind(); }
  547. void ARMTargetELFStreamer::emitPersonality(const MCSymbol *Personality) {
  548. getStreamer().emitPersonality(Personality);
  549. }
  550. void ARMTargetELFStreamer::emitPersonalityIndex(unsigned Index) {
  551. getStreamer().emitPersonalityIndex(Index);
  552. }
  553. void ARMTargetELFStreamer::emitHandlerData() {
  554. getStreamer().emitHandlerData();
  555. }
  556. void ARMTargetELFStreamer::emitSetFP(unsigned FpReg, unsigned SpReg,
  557. int64_t Offset) {
  558. getStreamer().emitSetFP(FpReg, SpReg, Offset);
  559. }
  560. void ARMTargetELFStreamer::emitMovSP(unsigned Reg, int64_t Offset) {
  561. getStreamer().emitMovSP(Reg, Offset);
  562. }
  563. void ARMTargetELFStreamer::emitPad(int64_t Offset) {
  564. getStreamer().emitPad(Offset);
  565. }
  566. void ARMTargetELFStreamer::emitRegSave(const SmallVectorImpl<unsigned> &RegList,
  567. bool isVector) {
  568. getStreamer().emitRegSave(RegList, isVector);
  569. }
  570. void ARMTargetELFStreamer::emitUnwindRaw(int64_t Offset,
  571. const SmallVectorImpl<uint8_t> &Opcodes) {
  572. getStreamer().emitUnwindRaw(Offset, Opcodes);
  573. }
  574. void ARMTargetELFStreamer::switchVendor(StringRef Vendor) {
  575. assert(!Vendor.empty() && "Vendor cannot be empty.");
  576. if (CurrentVendor == Vendor)
  577. return;
  578. if (!CurrentVendor.empty())
  579. finishAttributeSection();
  580. assert(getStreamer().Contents.empty() &&
  581. ".ARM.attributes should be flushed before changing vendor");
  582. CurrentVendor = Vendor;
  583. }
  584. void ARMTargetELFStreamer::emitAttribute(unsigned Attribute, unsigned Value) {
  585. getStreamer().setAttributeItem(Attribute, Value,
  586. /* OverwriteExisting= */ true);
  587. }
  588. void ARMTargetELFStreamer::emitTextAttribute(unsigned Attribute,
  589. StringRef Value) {
  590. getStreamer().setAttributeItem(Attribute, Value,
  591. /* OverwriteExisting= */ true);
  592. }
  593. void ARMTargetELFStreamer::emitIntTextAttribute(unsigned Attribute,
  594. unsigned IntValue,
  595. StringRef StringValue) {
  596. getStreamer().setAttributeItems(Attribute, IntValue, StringValue,
  597. /* OverwriteExisting= */ true);
  598. }
  599. void ARMTargetELFStreamer::emitArch(ARM::ArchKind Value) {
  600. Arch = Value;
  601. }
  602. void ARMTargetELFStreamer::emitObjectArch(ARM::ArchKind Value) {
  603. EmittedArch = Value;
  604. }
  605. void ARMTargetELFStreamer::emitArchDefaultAttributes() {
  606. using namespace ARMBuildAttrs;
  607. ARMELFStreamer &S = getStreamer();
  608. S.setAttributeItem(CPU_name, ARM::getCPUAttr(Arch), false);
  609. if (EmittedArch == ARM::ArchKind::INVALID)
  610. S.setAttributeItem(CPU_arch, ARM::getArchAttr(Arch), false);
  611. else
  612. S.setAttributeItem(CPU_arch, ARM::getArchAttr(EmittedArch), false);
  613. switch (Arch) {
  614. case ARM::ArchKind::ARMV2:
  615. case ARM::ArchKind::ARMV2A:
  616. case ARM::ArchKind::ARMV3:
  617. case ARM::ArchKind::ARMV3M:
  618. case ARM::ArchKind::ARMV4:
  619. S.setAttributeItem(ARM_ISA_use, Allowed, false);
  620. break;
  621. case ARM::ArchKind::ARMV4T:
  622. case ARM::ArchKind::ARMV5T:
  623. case ARM::ArchKind::XSCALE:
  624. case ARM::ArchKind::ARMV5TE:
  625. case ARM::ArchKind::ARMV6:
  626. S.setAttributeItem(ARM_ISA_use, Allowed, false);
  627. S.setAttributeItem(THUMB_ISA_use, Allowed, false);
  628. break;
  629. case ARM::ArchKind::ARMV6T2:
  630. S.setAttributeItem(ARM_ISA_use, Allowed, false);
  631. S.setAttributeItem(THUMB_ISA_use, AllowThumb32, false);
  632. break;
  633. case ARM::ArchKind::ARMV6K:
  634. case ARM::ArchKind::ARMV6KZ:
  635. S.setAttributeItem(ARM_ISA_use, Allowed, false);
  636. S.setAttributeItem(THUMB_ISA_use, Allowed, false);
  637. S.setAttributeItem(Virtualization_use, AllowTZ, false);
  638. break;
  639. case ARM::ArchKind::ARMV6M:
  640. S.setAttributeItem(THUMB_ISA_use, Allowed, false);
  641. break;
  642. case ARM::ArchKind::ARMV7A:
  643. S.setAttributeItem(CPU_arch_profile, ApplicationProfile, false);
  644. S.setAttributeItem(ARM_ISA_use, Allowed, false);
  645. S.setAttributeItem(THUMB_ISA_use, AllowThumb32, false);
  646. break;
  647. case ARM::ArchKind::ARMV7R:
  648. S.setAttributeItem(CPU_arch_profile, RealTimeProfile, false);
  649. S.setAttributeItem(ARM_ISA_use, Allowed, false);
  650. S.setAttributeItem(THUMB_ISA_use, AllowThumb32, false);
  651. break;
  652. case ARM::ArchKind::ARMV7EM:
  653. case ARM::ArchKind::ARMV7M:
  654. S.setAttributeItem(CPU_arch_profile, MicroControllerProfile, false);
  655. S.setAttributeItem(THUMB_ISA_use, AllowThumb32, false);
  656. break;
  657. case ARM::ArchKind::ARMV8A:
  658. case ARM::ArchKind::ARMV8_1A:
  659. case ARM::ArchKind::ARMV8_2A:
  660. case ARM::ArchKind::ARMV8_3A:
  661. case ARM::ArchKind::ARMV8_4A:
  662. case ARM::ArchKind::ARMV8_5A:
  663. case ARM::ArchKind::ARMV8_6A:
  664. case ARM::ArchKind::ARMV9A:
  665. case ARM::ArchKind::ARMV9_1A:
  666. case ARM::ArchKind::ARMV9_2A:
  667. case ARM::ArchKind::ARMV9_3A:
  668. S.setAttributeItem(CPU_arch_profile, ApplicationProfile, false);
  669. S.setAttributeItem(ARM_ISA_use, Allowed, false);
  670. S.setAttributeItem(THUMB_ISA_use, AllowThumb32, false);
  671. S.setAttributeItem(MPextension_use, Allowed, false);
  672. S.setAttributeItem(Virtualization_use, AllowTZVirtualization, false);
  673. break;
  674. case ARM::ArchKind::ARMV8MBaseline:
  675. case ARM::ArchKind::ARMV8MMainline:
  676. S.setAttributeItem(THUMB_ISA_use, AllowThumbDerived, false);
  677. S.setAttributeItem(CPU_arch_profile, MicroControllerProfile, false);
  678. break;
  679. case ARM::ArchKind::IWMMXT:
  680. S.setAttributeItem(ARM_ISA_use, Allowed, false);
  681. S.setAttributeItem(THUMB_ISA_use, Allowed, false);
  682. S.setAttributeItem(WMMX_arch, AllowWMMXv1, false);
  683. break;
  684. case ARM::ArchKind::IWMMXT2:
  685. S.setAttributeItem(ARM_ISA_use, Allowed, false);
  686. S.setAttributeItem(THUMB_ISA_use, Allowed, false);
  687. S.setAttributeItem(WMMX_arch, AllowWMMXv2, false);
  688. break;
  689. default:
  690. report_fatal_error("Unknown Arch: " + Twine(ARM::getArchName(Arch)));
  691. break;
  692. }
  693. }
  694. void ARMTargetELFStreamer::emitFPU(unsigned Value) {
  695. FPU = Value;
  696. }
  697. void ARMTargetELFStreamer::emitFPUDefaultAttributes() {
  698. ARMELFStreamer &S = getStreamer();
  699. switch (FPU) {
  700. case ARM::FK_VFP:
  701. case ARM::FK_VFPV2:
  702. S.setAttributeItem(ARMBuildAttrs::FP_arch, ARMBuildAttrs::AllowFPv2,
  703. /* OverwriteExisting= */ false);
  704. break;
  705. case ARM::FK_VFPV3:
  706. S.setAttributeItem(ARMBuildAttrs::FP_arch, ARMBuildAttrs::AllowFPv3A,
  707. /* OverwriteExisting= */ false);
  708. break;
  709. case ARM::FK_VFPV3_FP16:
  710. S.setAttributeItem(ARMBuildAttrs::FP_arch, ARMBuildAttrs::AllowFPv3A,
  711. /* OverwriteExisting= */ false);
  712. S.setAttributeItem(ARMBuildAttrs::FP_HP_extension, ARMBuildAttrs::AllowHPFP,
  713. /* OverwriteExisting= */ false);
  714. break;
  715. case ARM::FK_VFPV3_D16:
  716. S.setAttributeItem(ARMBuildAttrs::FP_arch, ARMBuildAttrs::AllowFPv3B,
  717. /* OverwriteExisting= */ false);
  718. break;
  719. case ARM::FK_VFPV3_D16_FP16:
  720. S.setAttributeItem(ARMBuildAttrs::FP_arch, ARMBuildAttrs::AllowFPv3B,
  721. /* OverwriteExisting= */ false);
  722. S.setAttributeItem(ARMBuildAttrs::FP_HP_extension, ARMBuildAttrs::AllowHPFP,
  723. /* OverwriteExisting= */ false);
  724. break;
  725. case ARM::FK_VFPV3XD:
  726. S.setAttributeItem(ARMBuildAttrs::FP_arch, ARMBuildAttrs::AllowFPv3B,
  727. /* OverwriteExisting= */ false);
  728. break;
  729. case ARM::FK_VFPV3XD_FP16:
  730. S.setAttributeItem(ARMBuildAttrs::FP_arch, ARMBuildAttrs::AllowFPv3B,
  731. /* OverwriteExisting= */ false);
  732. S.setAttributeItem(ARMBuildAttrs::FP_HP_extension, ARMBuildAttrs::AllowHPFP,
  733. /* OverwriteExisting= */ false);
  734. break;
  735. case ARM::FK_VFPV4:
  736. S.setAttributeItem(ARMBuildAttrs::FP_arch, ARMBuildAttrs::AllowFPv4A,
  737. /* OverwriteExisting= */ false);
  738. break;
  739. // ABI_HardFP_use is handled in ARMAsmPrinter, so _SP_D16 is treated the same
  740. // as _D16 here.
  741. case ARM::FK_FPV4_SP_D16:
  742. case ARM::FK_VFPV4_D16:
  743. S.setAttributeItem(ARMBuildAttrs::FP_arch, ARMBuildAttrs::AllowFPv4B,
  744. /* OverwriteExisting= */ false);
  745. break;
  746. case ARM::FK_FP_ARMV8:
  747. S.setAttributeItem(ARMBuildAttrs::FP_arch, ARMBuildAttrs::AllowFPARMv8A,
  748. /* OverwriteExisting= */ false);
  749. break;
  750. // FPV5_D16 is identical to FP_ARMV8 except for the number of D registers, so
  751. // uses the FP_ARMV8_D16 build attribute.
  752. case ARM::FK_FPV5_SP_D16:
  753. case ARM::FK_FPV5_D16:
  754. S.setAttributeItem(ARMBuildAttrs::FP_arch, ARMBuildAttrs::AllowFPARMv8B,
  755. /* OverwriteExisting= */ false);
  756. break;
  757. case ARM::FK_NEON:
  758. S.setAttributeItem(ARMBuildAttrs::FP_arch, ARMBuildAttrs::AllowFPv3A,
  759. /* OverwriteExisting= */ false);
  760. S.setAttributeItem(ARMBuildAttrs::Advanced_SIMD_arch,
  761. ARMBuildAttrs::AllowNeon,
  762. /* OverwriteExisting= */ false);
  763. break;
  764. case ARM::FK_NEON_FP16:
  765. S.setAttributeItem(ARMBuildAttrs::FP_arch, ARMBuildAttrs::AllowFPv3A,
  766. /* OverwriteExisting= */ false);
  767. S.setAttributeItem(ARMBuildAttrs::Advanced_SIMD_arch,
  768. ARMBuildAttrs::AllowNeon,
  769. /* OverwriteExisting= */ false);
  770. S.setAttributeItem(ARMBuildAttrs::FP_HP_extension, ARMBuildAttrs::AllowHPFP,
  771. /* OverwriteExisting= */ false);
  772. break;
  773. case ARM::FK_NEON_VFPV4:
  774. S.setAttributeItem(ARMBuildAttrs::FP_arch, ARMBuildAttrs::AllowFPv4A,
  775. /* OverwriteExisting= */ false);
  776. S.setAttributeItem(ARMBuildAttrs::Advanced_SIMD_arch,
  777. ARMBuildAttrs::AllowNeon2,
  778. /* OverwriteExisting= */ false);
  779. break;
  780. case ARM::FK_NEON_FP_ARMV8:
  781. case ARM::FK_CRYPTO_NEON_FP_ARMV8:
  782. S.setAttributeItem(ARMBuildAttrs::FP_arch, ARMBuildAttrs::AllowFPARMv8A,
  783. /* OverwriteExisting= */ false);
  784. // 'Advanced_SIMD_arch' must be emitted not here, but within
  785. // ARMAsmPrinter::emitAttributes(), depending on hasV8Ops() and hasV8_1a()
  786. break;
  787. case ARM::FK_SOFTVFP:
  788. case ARM::FK_NONE:
  789. break;
  790. default:
  791. report_fatal_error("Unknown FPU: " + Twine(FPU));
  792. break;
  793. }
  794. }
  795. void ARMTargetELFStreamer::finishAttributeSection() {
  796. ARMELFStreamer &S = getStreamer();
  797. if (FPU != ARM::FK_INVALID)
  798. emitFPUDefaultAttributes();
  799. if (Arch != ARM::ArchKind::INVALID)
  800. emitArchDefaultAttributes();
  801. if (S.Contents.empty())
  802. return;
  803. auto LessTag = [](const MCELFStreamer::AttributeItem &LHS,
  804. const MCELFStreamer::AttributeItem &RHS) -> bool {
  805. // The conformance tag must be emitted first when serialised into an
  806. // object file. Specifically, the addenda to the ARM ABI states that
  807. // (2.3.7.4):
  808. //
  809. // "To simplify recognition by consumers in the common case of claiming
  810. // conformity for the whole file, this tag should be emitted first in a
  811. // file-scope sub-subsection of the first public subsection of the
  812. // attributes section."
  813. //
  814. // So it is special-cased in this comparison predicate when the
  815. // attributes are sorted in finishAttributeSection().
  816. return (RHS.Tag != ARMBuildAttrs::conformance) &&
  817. ((LHS.Tag == ARMBuildAttrs::conformance) || (LHS.Tag < RHS.Tag));
  818. };
  819. llvm::sort(S.Contents, LessTag);
  820. S.emitAttributesSection(CurrentVendor, ".ARM.attributes",
  821. ELF::SHT_ARM_ATTRIBUTES, AttributeSection);
  822. FPU = ARM::FK_INVALID;
  823. }
  824. void ARMTargetELFStreamer::emitLabel(MCSymbol *Symbol) {
  825. ARMELFStreamer &Streamer = getStreamer();
  826. if (!Streamer.IsThumb)
  827. return;
  828. Streamer.getAssembler().registerSymbol(*Symbol);
  829. unsigned Type = cast<MCSymbolELF>(Symbol)->getType();
  830. if (Type == ELF::STT_FUNC || Type == ELF::STT_GNU_IFUNC)
  831. Streamer.emitThumbFunc(Symbol);
  832. }
  833. void
  834. ARMTargetELFStreamer::AnnotateTLSDescriptorSequence(const MCSymbolRefExpr *S) {
  835. getStreamer().EmitFixup(S, FK_Data_4);
  836. }
  837. void ARMTargetELFStreamer::emitThumbSet(MCSymbol *Symbol, const MCExpr *Value) {
  838. if (const MCSymbolRefExpr *SRE = dyn_cast<MCSymbolRefExpr>(Value)) {
  839. const MCSymbol &Sym = SRE->getSymbol();
  840. if (!Sym.isDefined()) {
  841. getStreamer().emitAssignment(Symbol, Value);
  842. return;
  843. }
  844. }
  845. getStreamer().emitThumbFunc(Symbol);
  846. getStreamer().emitAssignment(Symbol, Value);
  847. }
  848. void ARMTargetELFStreamer::emitInst(uint32_t Inst, char Suffix) {
  849. getStreamer().emitInst(Inst, Suffix);
  850. }
  851. void ARMTargetELFStreamer::reset() { AttributeSection = nullptr; }
  852. void ARMELFStreamer::finishImpl() {
  853. MCTargetStreamer &TS = *getTargetStreamer();
  854. ARMTargetStreamer &ATS = static_cast<ARMTargetStreamer &>(TS);
  855. ATS.finishAttributeSection();
  856. MCELFStreamer::finishImpl();
  857. }
  858. void ARMELFStreamer::reset() {
  859. MCTargetStreamer &TS = *getTargetStreamer();
  860. ARMTargetStreamer &ATS = static_cast<ARMTargetStreamer &>(TS);
  861. ATS.reset();
  862. MappingSymbolCounter = 0;
  863. MCELFStreamer::reset();
  864. LastMappingSymbols.clear();
  865. LastEMSInfo.reset();
  866. // MCELFStreamer clear's the assembler's e_flags. However, for
  867. // arm we manually set the ABI version on streamer creation, so
  868. // do the same here
  869. getAssembler().setELFHeaderEFlags(ELF::EF_ARM_EABI_VER5);
  870. }
  871. inline void ARMELFStreamer::SwitchToEHSection(StringRef Prefix,
  872. unsigned Type,
  873. unsigned Flags,
  874. SectionKind Kind,
  875. const MCSymbol &Fn) {
  876. const MCSectionELF &FnSection =
  877. static_cast<const MCSectionELF &>(Fn.getSection());
  878. // Create the name for new section
  879. StringRef FnSecName(FnSection.getName());
  880. SmallString<128> EHSecName(Prefix);
  881. if (FnSecName != ".text") {
  882. EHSecName += FnSecName;
  883. }
  884. // Get .ARM.extab or .ARM.exidx section
  885. const MCSymbolELF *Group = FnSection.getGroup();
  886. if (Group)
  887. Flags |= ELF::SHF_GROUP;
  888. MCSectionELF *EHSection = getContext().getELFSection(
  889. EHSecName, Type, Flags, 0, Group, /*IsComdat=*/true,
  890. FnSection.getUniqueID(),
  891. static_cast<const MCSymbolELF *>(FnSection.getBeginSymbol()));
  892. assert(EHSection && "Failed to get the required EH section");
  893. // Switch to .ARM.extab or .ARM.exidx section
  894. SwitchSection(EHSection);
  895. emitValueToAlignment(4, 0, 1, 0);
  896. }
  897. inline void ARMELFStreamer::SwitchToExTabSection(const MCSymbol &FnStart) {
  898. SwitchToEHSection(".ARM.extab", ELF::SHT_PROGBITS, ELF::SHF_ALLOC,
  899. SectionKind::getData(), FnStart);
  900. }
  901. inline void ARMELFStreamer::SwitchToExIdxSection(const MCSymbol &FnStart) {
  902. SwitchToEHSection(".ARM.exidx", ELF::SHT_ARM_EXIDX,
  903. ELF::SHF_ALLOC | ELF::SHF_LINK_ORDER,
  904. SectionKind::getData(), FnStart);
  905. }
  906. void ARMELFStreamer::EmitFixup(const MCExpr *Expr, MCFixupKind Kind) {
  907. MCDataFragment *Frag = getOrCreateDataFragment();
  908. Frag->getFixups().push_back(MCFixup::create(Frag->getContents().size(), Expr,
  909. Kind));
  910. }
  911. void ARMELFStreamer::EHReset() {
  912. ExTab = nullptr;
  913. FnStart = nullptr;
  914. Personality = nullptr;
  915. PersonalityIndex = ARM::EHABI::NUM_PERSONALITY_INDEX;
  916. FPReg = ARM::SP;
  917. FPOffset = 0;
  918. SPOffset = 0;
  919. PendingOffset = 0;
  920. UsedFP = false;
  921. CantUnwind = false;
  922. Opcodes.clear();
  923. UnwindOpAsm.Reset();
  924. }
  925. void ARMELFStreamer::emitFnStart() {
  926. assert(FnStart == nullptr);
  927. FnStart = getContext().createTempSymbol();
  928. emitLabel(FnStart);
  929. }
  930. void ARMELFStreamer::emitFnEnd() {
  931. assert(FnStart && ".fnstart must precedes .fnend");
  932. // Emit unwind opcodes if there is no .handlerdata directive
  933. if (!ExTab && !CantUnwind)
  934. FlushUnwindOpcodes(true);
  935. // Emit the exception index table entry
  936. SwitchToExIdxSection(*FnStart);
  937. // The EHABI requires a dependency preserving R_ARM_NONE relocation to the
  938. // personality routine to protect it from an arbitrary platform's static
  939. // linker garbage collection. We disable this for Android where the unwinder
  940. // is either dynamically linked or directly references the personality
  941. // routine.
  942. if (PersonalityIndex < ARM::EHABI::NUM_PERSONALITY_INDEX && !IsAndroid)
  943. EmitPersonalityFixup(GetAEABIUnwindPersonalityName(PersonalityIndex));
  944. const MCSymbolRefExpr *FnStartRef =
  945. MCSymbolRefExpr::create(FnStart,
  946. MCSymbolRefExpr::VK_ARM_PREL31,
  947. getContext());
  948. emitValue(FnStartRef, 4);
  949. if (CantUnwind) {
  950. emitInt32(ARM::EHABI::EXIDX_CANTUNWIND);
  951. } else if (ExTab) {
  952. // Emit a reference to the unwind opcodes in the ".ARM.extab" section.
  953. const MCSymbolRefExpr *ExTabEntryRef =
  954. MCSymbolRefExpr::create(ExTab,
  955. MCSymbolRefExpr::VK_ARM_PREL31,
  956. getContext());
  957. emitValue(ExTabEntryRef, 4);
  958. } else {
  959. // For the __aeabi_unwind_cpp_pr0, we have to emit the unwind opcodes in
  960. // the second word of exception index table entry. The size of the unwind
  961. // opcodes should always be 4 bytes.
  962. assert(PersonalityIndex == ARM::EHABI::AEABI_UNWIND_CPP_PR0 &&
  963. "Compact model must use __aeabi_unwind_cpp_pr0 as personality");
  964. assert(Opcodes.size() == 4u &&
  965. "Unwind opcode size for __aeabi_unwind_cpp_pr0 must be equal to 4");
  966. uint64_t Intval = Opcodes[0] |
  967. Opcodes[1] << 8 |
  968. Opcodes[2] << 16 |
  969. Opcodes[3] << 24;
  970. emitIntValue(Intval, Opcodes.size());
  971. }
  972. // Switch to the section containing FnStart
  973. SwitchSection(&FnStart->getSection());
  974. // Clean exception handling frame information
  975. EHReset();
  976. }
  977. void ARMELFStreamer::emitCantUnwind() { CantUnwind = true; }
  978. // Add the R_ARM_NONE fixup at the same position
  979. void ARMELFStreamer::EmitPersonalityFixup(StringRef Name) {
  980. const MCSymbol *PersonalitySym = getContext().getOrCreateSymbol(Name);
  981. const MCSymbolRefExpr *PersonalityRef = MCSymbolRefExpr::create(
  982. PersonalitySym, MCSymbolRefExpr::VK_ARM_NONE, getContext());
  983. visitUsedExpr(*PersonalityRef);
  984. MCDataFragment *DF = getOrCreateDataFragment();
  985. DF->getFixups().push_back(MCFixup::create(DF->getContents().size(),
  986. PersonalityRef,
  987. MCFixup::getKindForSize(4, false)));
  988. }
  989. void ARMELFStreamer::FlushPendingOffset() {
  990. if (PendingOffset != 0) {
  991. UnwindOpAsm.EmitSPOffset(-PendingOffset);
  992. PendingOffset = 0;
  993. }
  994. }
  995. void ARMELFStreamer::FlushUnwindOpcodes(bool NoHandlerData) {
  996. // Emit the unwind opcode to restore $sp.
  997. if (UsedFP) {
  998. const MCRegisterInfo *MRI = getContext().getRegisterInfo();
  999. int64_t LastRegSaveSPOffset = SPOffset - PendingOffset;
  1000. UnwindOpAsm.EmitSPOffset(LastRegSaveSPOffset - FPOffset);
  1001. UnwindOpAsm.EmitSetSP(MRI->getEncodingValue(FPReg));
  1002. } else {
  1003. FlushPendingOffset();
  1004. }
  1005. // Finalize the unwind opcode sequence
  1006. UnwindOpAsm.Finalize(PersonalityIndex, Opcodes);
  1007. // For compact model 0, we have to emit the unwind opcodes in the .ARM.exidx
  1008. // section. Thus, we don't have to create an entry in the .ARM.extab
  1009. // section.
  1010. if (NoHandlerData && PersonalityIndex == ARM::EHABI::AEABI_UNWIND_CPP_PR0)
  1011. return;
  1012. // Switch to .ARM.extab section.
  1013. SwitchToExTabSection(*FnStart);
  1014. // Create .ARM.extab label for offset in .ARM.exidx
  1015. assert(!ExTab);
  1016. ExTab = getContext().createTempSymbol();
  1017. emitLabel(ExTab);
  1018. // Emit personality
  1019. if (Personality) {
  1020. const MCSymbolRefExpr *PersonalityRef =
  1021. MCSymbolRefExpr::create(Personality,
  1022. MCSymbolRefExpr::VK_ARM_PREL31,
  1023. getContext());
  1024. emitValue(PersonalityRef, 4);
  1025. }
  1026. // Emit unwind opcodes
  1027. assert((Opcodes.size() % 4) == 0 &&
  1028. "Unwind opcode size for __aeabi_cpp_unwind_pr0 must be multiple of 4");
  1029. for (unsigned I = 0; I != Opcodes.size(); I += 4) {
  1030. uint64_t Intval = Opcodes[I] |
  1031. Opcodes[I + 1] << 8 |
  1032. Opcodes[I + 2] << 16 |
  1033. Opcodes[I + 3] << 24;
  1034. emitInt32(Intval);
  1035. }
  1036. // According to ARM EHABI section 9.2, if the __aeabi_unwind_cpp_pr1() or
  1037. // __aeabi_unwind_cpp_pr2() is used, then the handler data must be emitted
  1038. // after the unwind opcodes. The handler data consists of several 32-bit
  1039. // words, and should be terminated by zero.
  1040. //
  1041. // In case that the .handlerdata directive is not specified by the
  1042. // programmer, we should emit zero to terminate the handler data.
  1043. if (NoHandlerData && !Personality)
  1044. emitInt32(0);
  1045. }
  1046. void ARMELFStreamer::emitHandlerData() { FlushUnwindOpcodes(false); }
  1047. void ARMELFStreamer::emitPersonality(const MCSymbol *Per) {
  1048. Personality = Per;
  1049. UnwindOpAsm.setPersonality(Per);
  1050. }
  1051. void ARMELFStreamer::emitPersonalityIndex(unsigned Index) {
  1052. assert(Index < ARM::EHABI::NUM_PERSONALITY_INDEX && "invalid index");
  1053. PersonalityIndex = Index;
  1054. }
  1055. void ARMELFStreamer::emitSetFP(unsigned NewFPReg, unsigned NewSPReg,
  1056. int64_t Offset) {
  1057. assert((NewSPReg == ARM::SP || NewSPReg == FPReg) &&
  1058. "the operand of .setfp directive should be either $sp or $fp");
  1059. UsedFP = true;
  1060. FPReg = NewFPReg;
  1061. if (NewSPReg == ARM::SP)
  1062. FPOffset = SPOffset + Offset;
  1063. else
  1064. FPOffset += Offset;
  1065. }
  1066. void ARMELFStreamer::emitMovSP(unsigned Reg, int64_t Offset) {
  1067. assert((Reg != ARM::SP && Reg != ARM::PC) &&
  1068. "the operand of .movsp cannot be either sp or pc");
  1069. assert(FPReg == ARM::SP && "current FP must be SP");
  1070. FlushPendingOffset();
  1071. FPReg = Reg;
  1072. FPOffset = SPOffset + Offset;
  1073. const MCRegisterInfo *MRI = getContext().getRegisterInfo();
  1074. UnwindOpAsm.EmitSetSP(MRI->getEncodingValue(FPReg));
  1075. }
  1076. void ARMELFStreamer::emitPad(int64_t Offset) {
  1077. // Track the change of the $sp offset
  1078. SPOffset -= Offset;
  1079. // To squash multiple .pad directives, we should delay the unwind opcode
  1080. // until the .save, .vsave, .handlerdata, or .fnend directives.
  1081. PendingOffset -= Offset;
  1082. }
  1083. static std::pair<unsigned, unsigned>
  1084. collectHWRegs(const MCRegisterInfo &MRI, unsigned Idx,
  1085. const SmallVectorImpl<unsigned> &RegList, bool IsVector,
  1086. uint32_t &Mask_) {
  1087. uint32_t Mask = 0;
  1088. unsigned Count = 0;
  1089. while (Idx > 0) {
  1090. unsigned Reg = RegList[Idx - 1];
  1091. if (Reg == ARM::RA_AUTH_CODE)
  1092. break;
  1093. Reg = MRI.getEncodingValue(Reg);
  1094. assert(Reg < (IsVector ? 32U : 16U) && "Register out of range");
  1095. unsigned Bit = (1u << Reg);
  1096. if ((Mask & Bit) == 0) {
  1097. Mask |= Bit;
  1098. ++Count;
  1099. }
  1100. --Idx;
  1101. }
  1102. Mask_ = Mask;
  1103. return {Idx, Count};
  1104. }
  1105. void ARMELFStreamer::emitRegSave(const SmallVectorImpl<unsigned> &RegList,
  1106. bool IsVector) {
  1107. uint32_t Mask;
  1108. unsigned Idx, Count;
  1109. const MCRegisterInfo &MRI = *getContext().getRegisterInfo();
  1110. // Collect the registers in the register list. Issue unwinding instructions in
  1111. // three parts: ordinary hardware registers, return address authentication
  1112. // code pseudo register, the rest of the registers. The RA PAC is kept in an
  1113. // architectural register (usually r12), but we treat it as a special case in
  1114. // order to distinguish between that register containing RA PAC or a general
  1115. // value.
  1116. Idx = RegList.size();
  1117. while (Idx > 0) {
  1118. std::tie(Idx, Count) = collectHWRegs(MRI, Idx, RegList, IsVector, Mask);
  1119. if (Count) {
  1120. // Track the change the $sp offset: For the .save directive, the
  1121. // corresponding push instruction will decrease the $sp by (4 * Count).
  1122. // For the .vsave directive, the corresponding vpush instruction will
  1123. // decrease $sp by (8 * Count).
  1124. SPOffset -= Count * (IsVector ? 8 : 4);
  1125. // Emit the opcode
  1126. FlushPendingOffset();
  1127. if (IsVector)
  1128. UnwindOpAsm.EmitVFPRegSave(Mask);
  1129. else
  1130. UnwindOpAsm.EmitRegSave(Mask);
  1131. } else if (Idx > 0 && RegList[Idx - 1] == ARM::RA_AUTH_CODE) {
  1132. --Idx;
  1133. SPOffset -= 4;
  1134. FlushPendingOffset();
  1135. UnwindOpAsm.EmitRegSave(0);
  1136. }
  1137. }
  1138. }
  1139. void ARMELFStreamer::emitUnwindRaw(int64_t Offset,
  1140. const SmallVectorImpl<uint8_t> &Opcodes) {
  1141. FlushPendingOffset();
  1142. SPOffset = SPOffset - Offset;
  1143. UnwindOpAsm.EmitRaw(Opcodes);
  1144. }
  1145. namespace llvm {
  1146. MCTargetStreamer *createARMTargetAsmStreamer(MCStreamer &S,
  1147. formatted_raw_ostream &OS,
  1148. MCInstPrinter *InstPrint,
  1149. bool isVerboseAsm) {
  1150. return new ARMTargetAsmStreamer(S, OS, *InstPrint, isVerboseAsm);
  1151. }
  1152. MCTargetStreamer *createARMNullTargetStreamer(MCStreamer &S) {
  1153. return new ARMTargetStreamer(S);
  1154. }
  1155. MCTargetStreamer *createARMObjectTargetStreamer(MCStreamer &S,
  1156. const MCSubtargetInfo &STI) {
  1157. const Triple &TT = STI.getTargetTriple();
  1158. if (TT.isOSBinFormatELF())
  1159. return new ARMTargetELFStreamer(S);
  1160. return new ARMTargetStreamer(S);
  1161. }
  1162. MCELFStreamer *createARMELFStreamer(MCContext &Context,
  1163. std::unique_ptr<MCAsmBackend> TAB,
  1164. std::unique_ptr<MCObjectWriter> OW,
  1165. std::unique_ptr<MCCodeEmitter> Emitter,
  1166. bool RelaxAll, bool IsThumb,
  1167. bool IsAndroid) {
  1168. ARMELFStreamer *S =
  1169. new ARMELFStreamer(Context, std::move(TAB), std::move(OW),
  1170. std::move(Emitter), IsThumb, IsAndroid);
  1171. // FIXME: This should eventually end up somewhere else where more
  1172. // intelligent flag decisions can be made. For now we are just maintaining
  1173. // the status quo for ARM and setting EF_ARM_EABI_VER5 as the default.
  1174. S->getAssembler().setELFHeaderEFlags(ELF::EF_ARM_EABI_VER5);
  1175. if (RelaxAll)
  1176. S->getAssembler().setRelaxAll(true);
  1177. return S;
  1178. }
  1179. } // end namespace llvm