LLVM 20.0.0git
ARMELFStreamer.cpp
Go to the documentation of this file.
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
15#include "ARMMCTargetDesc.h"
16#include "ARMUnwindOpAsm.h"
17#include "Utils/ARMBaseInfo.h"
18#include "llvm/ADT/DenseMap.h"
22#include "llvm/ADT/StringRef.h"
23#include "llvm/ADT/Twine.h"
26#include "llvm/MC/MCAsmInfo.h"
27#include "llvm/MC/MCAssembler.h"
29#include "llvm/MC/MCContext.h"
32#include "llvm/MC/MCExpr.h"
33#include "llvm/MC/MCFixup.h"
34#include "llvm/MC/MCFragment.h"
35#include "llvm/MC/MCInst.h"
40#include "llvm/MC/MCSection.h"
42#include "llvm/MC/MCStreamer.h"
44#include "llvm/MC/MCSymbol.h"
45#include "llvm/MC/MCSymbolELF.h"
46#include "llvm/MC/SectionKind.h"
53#include <algorithm>
54#include <cassert>
55#include <climits>
56#include <cstddef>
57#include <cstdint>
58#include <string>
59
60using namespace llvm;
61
62static std::string GetAEABIUnwindPersonalityName(unsigned Index) {
64 "Invalid personality index");
65 return (Twine("__aeabi_unwind_cpp_pr") + Twine(Index)).str();
66}
67
68namespace {
69
70class ARMELFStreamer;
71
72class ARMTargetAsmStreamer : public ARMTargetStreamer {
74 MCInstPrinter &InstPrinter;
75 bool IsVerboseAsm;
76
77 void emitFnStart() override;
78 void emitFnEnd() override;
79 void emitCantUnwind() override;
80 void emitPersonality(const MCSymbol *Personality) override;
81 void emitPersonalityIndex(unsigned Index) override;
82 void emitHandlerData() override;
83 void emitSetFP(unsigned FpReg, unsigned SpReg, int64_t Offset = 0) override;
84 void emitMovSP(unsigned Reg, int64_t Offset = 0) override;
85 void emitPad(int64_t Offset) override;
86 void emitRegSave(const SmallVectorImpl<unsigned> &RegList,
87 bool isVector) override;
88 void emitUnwindRaw(int64_t Offset,
89 const SmallVectorImpl<uint8_t> &Opcodes) override;
90
91 void switchVendor(StringRef Vendor) override;
92 void emitAttribute(unsigned Attribute, unsigned Value) override;
93 void emitTextAttribute(unsigned Attribute, StringRef String) override;
94 void emitIntTextAttribute(unsigned Attribute, unsigned IntValue,
95 StringRef StringValue) override;
96 void emitArch(ARM::ArchKind Arch) override;
97 void emitArchExtension(uint64_t ArchExt) override;
98 void emitObjectArch(ARM::ArchKind Arch) override;
99 void emitFPU(ARM::FPUKind FPU) override;
100 void emitInst(uint32_t Inst, char Suffix = '\0') override;
101 void finishAttributeSection() override;
102
103 void annotateTLSDescriptorSequence(const MCSymbolRefExpr *SRE) override;
104 void emitThumbSet(MCSymbol *Symbol, const MCExpr *Value) override;
105
106 void emitARMWinCFIAllocStack(unsigned Size, bool Wide) override;
107 void emitARMWinCFISaveRegMask(unsigned Mask, bool Wide) override;
108 void emitARMWinCFISaveSP(unsigned Reg) override;
109 void emitARMWinCFISaveFRegs(unsigned First, unsigned Last) override;
110 void emitARMWinCFISaveLR(unsigned Offset) override;
111 void emitARMWinCFIPrologEnd(bool Fragment) override;
112 void emitARMWinCFINop(bool Wide) override;
113 void emitARMWinCFIEpilogStart(unsigned Condition) override;
114 void emitARMWinCFIEpilogEnd() override;
115 void emitARMWinCFICustom(unsigned Opcode) override;
116
117public:
118 ARMTargetAsmStreamer(MCStreamer &S, formatted_raw_ostream &OS,
119 MCInstPrinter &InstPrinter);
120};
121
122ARMTargetAsmStreamer::ARMTargetAsmStreamer(MCStreamer &S,
124 MCInstPrinter &InstPrinter)
125 : ARMTargetStreamer(S), OS(OS), InstPrinter(InstPrinter),
126 IsVerboseAsm(S.isVerboseAsm()) {}
127
128void ARMTargetAsmStreamer::emitFnStart() { OS << "\t.fnstart\n"; }
129void ARMTargetAsmStreamer::emitFnEnd() { OS << "\t.fnend\n"; }
130void ARMTargetAsmStreamer::emitCantUnwind() { OS << "\t.cantunwind\n"; }
131
132void ARMTargetAsmStreamer::emitPersonality(const MCSymbol *Personality) {
133 OS << "\t.personality " << Personality->getName() << '\n';
134}
135
136void ARMTargetAsmStreamer::emitPersonalityIndex(unsigned Index) {
137 OS << "\t.personalityindex " << Index << '\n';
138}
139
140void ARMTargetAsmStreamer::emitHandlerData() { OS << "\t.handlerdata\n"; }
141
142void ARMTargetAsmStreamer::emitSetFP(unsigned FpReg, unsigned SpReg,
143 int64_t Offset) {
144 OS << "\t.setfp\t";
145 InstPrinter.printRegName(OS, FpReg);
146 OS << ", ";
147 InstPrinter.printRegName(OS, SpReg);
148 if (Offset)
149 OS << ", #" << Offset;
150 OS << '\n';
151}
152
153void ARMTargetAsmStreamer::emitMovSP(unsigned Reg, int64_t Offset) {
154 assert((Reg != ARM::SP && Reg != ARM::PC) &&
155 "the operand of .movsp cannot be either sp or pc");
156
157 OS << "\t.movsp\t";
158 InstPrinter.printRegName(OS, Reg);
159 if (Offset)
160 OS << ", #" << Offset;
161 OS << '\n';
162}
163
164void ARMTargetAsmStreamer::emitPad(int64_t Offset) {
165 OS << "\t.pad\t#" << Offset << '\n';
166}
167
168void ARMTargetAsmStreamer::emitRegSave(const SmallVectorImpl<unsigned> &RegList,
169 bool isVector) {
170 assert(RegList.size() && "RegList should not be empty");
171 if (isVector)
172 OS << "\t.vsave\t{";
173 else
174 OS << "\t.save\t{";
175
176 InstPrinter.printRegName(OS, RegList[0]);
177
178 for (unsigned i = 1, e = RegList.size(); i != e; ++i) {
179 OS << ", ";
180 InstPrinter.printRegName(OS, RegList[i]);
181 }
182
183 OS << "}\n";
184}
185
186void ARMTargetAsmStreamer::switchVendor(StringRef Vendor) {}
187
188void ARMTargetAsmStreamer::emitAttribute(unsigned Attribute, unsigned Value) {
189 OS << "\t.eabi_attribute\t" << Attribute << ", " << Twine(Value);
190 if (IsVerboseAsm) {
193 if (!Name.empty())
194 OS << "\t@ " << Name;
195 }
196 OS << "\n";
197}
198
199void ARMTargetAsmStreamer::emitTextAttribute(unsigned Attribute,
201 switch (Attribute) {
203 OS << "\t.cpu\t" << String.lower();
204 break;
205 default:
206 OS << "\t.eabi_attribute\t" << Attribute << ", \"";
209 else
210 OS << String;
211 OS << "\"";
212 if (IsVerboseAsm) {
215 if (!Name.empty())
216 OS << "\t@ " << Name;
217 }
218 break;
219 }
220 OS << "\n";
221}
222
223void ARMTargetAsmStreamer::emitIntTextAttribute(unsigned Attribute,
224 unsigned IntValue,
225 StringRef StringValue) {
226 switch (Attribute) {
227 default: llvm_unreachable("unsupported multi-value attribute in asm mode");
229 OS << "\t.eabi_attribute\t" << Attribute << ", " << IntValue;
230 if (!StringValue.empty())
231 OS << ", \"" << StringValue << "\"";
232 if (IsVerboseAsm)
233 OS << "\t@ "
236 break;
237 }
238 OS << "\n";
239}
240
241void ARMTargetAsmStreamer::emitArch(ARM::ArchKind Arch) {
242 OS << "\t.arch\t" << ARM::getArchName(Arch) << "\n";
243}
244
245void ARMTargetAsmStreamer::emitArchExtension(uint64_t ArchExt) {
246 OS << "\t.arch_extension\t" << ARM::getArchExtName(ArchExt) << "\n";
247}
248
249void ARMTargetAsmStreamer::emitObjectArch(ARM::ArchKind Arch) {
250 OS << "\t.object_arch\t" << ARM::getArchName(Arch) << '\n';
251}
252
253void ARMTargetAsmStreamer::emitFPU(ARM::FPUKind FPU) {
254 OS << "\t.fpu\t" << ARM::getFPUName(FPU) << "\n";
255}
256
257void ARMTargetAsmStreamer::finishAttributeSection() {}
258
259void ARMTargetAsmStreamer::annotateTLSDescriptorSequence(
260 const MCSymbolRefExpr *S) {
261 OS << "\t.tlsdescseq\t" << S->getSymbol().getName() << "\n";
262}
263
264void ARMTargetAsmStreamer::emitThumbSet(MCSymbol *Symbol, const MCExpr *Value) {
265 const MCAsmInfo *MAI = Streamer.getContext().getAsmInfo();
266
267 OS << "\t.thumb_set\t";
268 Symbol->print(OS, MAI);
269 OS << ", ";
270 Value->print(OS, MAI);
271 OS << '\n';
272}
273
274void ARMTargetAsmStreamer::emitInst(uint32_t Inst, char Suffix) {
275 OS << "\t.inst";
276 if (Suffix)
277 OS << "." << Suffix;
278 OS << "\t0x" << Twine::utohexstr(Inst) << "\n";
279}
280
281void ARMTargetAsmStreamer::emitUnwindRaw(int64_t Offset,
282 const SmallVectorImpl<uint8_t> &Opcodes) {
283 OS << "\t.unwind_raw " << Offset;
284 for (uint8_t Opcode : Opcodes)
285 OS << ", 0x" << Twine::utohexstr(Opcode);
286 OS << '\n';
287}
288
289void ARMTargetAsmStreamer::emitARMWinCFIAllocStack(unsigned Size, bool Wide) {
290 if (Wide)
291 OS << "\t.seh_stackalloc_w\t" << Size << "\n";
292 else
293 OS << "\t.seh_stackalloc\t" << Size << "\n";
294}
295
296static void printRegs(formatted_raw_ostream &OS, ListSeparator &LS, int First,
297 int Last) {
298 if (First != Last)
299 OS << LS << "r" << First << "-r" << Last;
300 else
301 OS << LS << "r" << First;
302}
303
304void ARMTargetAsmStreamer::emitARMWinCFISaveRegMask(unsigned Mask, bool Wide) {
305 if (Wide)
306 OS << "\t.seh_save_regs_w\t";
307 else
308 OS << "\t.seh_save_regs\t";
309 ListSeparator LS;
310 int First = -1;
311 OS << "{";
312 for (int I = 0; I <= 12; I++) {
313 if (Mask & (1 << I)) {
314 if (First < 0)
315 First = I;
316 } else {
317 if (First >= 0) {
318 printRegs(OS, LS, First, I - 1);
319 First = -1;
320 }
321 }
322 }
323 if (First >= 0)
324 printRegs(OS, LS, First, 12);
325 if (Mask & (1 << 14))
326 OS << LS << "lr";
327 OS << "}\n";
328}
329
330void ARMTargetAsmStreamer::emitARMWinCFISaveSP(unsigned Reg) {
331 OS << "\t.seh_save_sp\tr" << Reg << "\n";
332}
333
334void ARMTargetAsmStreamer::emitARMWinCFISaveFRegs(unsigned First,
335 unsigned Last) {
336 if (First != Last)
337 OS << "\t.seh_save_fregs\t{d" << First << "-d" << Last << "}\n";
338 else
339 OS << "\t.seh_save_fregs\t{d" << First << "}\n";
340}
341
342void ARMTargetAsmStreamer::emitARMWinCFISaveLR(unsigned Offset) {
343 OS << "\t.seh_save_lr\t" << Offset << "\n";
344}
345
346void ARMTargetAsmStreamer::emitARMWinCFIPrologEnd(bool Fragment) {
347 if (Fragment)
348 OS << "\t.seh_endprologue_fragment\n";
349 else
350 OS << "\t.seh_endprologue\n";
351}
352
353void ARMTargetAsmStreamer::emitARMWinCFINop(bool Wide) {
354 if (Wide)
355 OS << "\t.seh_nop_w\n";
356 else
357 OS << "\t.seh_nop\n";
358}
359
360void ARMTargetAsmStreamer::emitARMWinCFIEpilogStart(unsigned Condition) {
361 if (Condition == ARMCC::AL)
362 OS << "\t.seh_startepilogue\n";
363 else
364 OS << "\t.seh_startepilogue_cond\t"
365 << ARMCondCodeToString(static_cast<ARMCC::CondCodes>(Condition)) << "\n";
366}
367
368void ARMTargetAsmStreamer::emitARMWinCFIEpilogEnd() {
369 OS << "\t.seh_endepilogue\n";
370}
371
372void ARMTargetAsmStreamer::emitARMWinCFICustom(unsigned Opcode) {
373 int I;
374 for (I = 3; I > 0; I--)
375 if (Opcode & (0xffu << (8 * I)))
376 break;
377 ListSeparator LS;
378 OS << "\t.seh_custom\t";
379 for (; I >= 0; I--)
380 OS << LS << ((Opcode >> (8 * I)) & 0xff);
381 OS << "\n";
382}
383
384class ARMTargetELFStreamer : public ARMTargetStreamer {
385private:
386 StringRef CurrentVendor;
387 ARM::FPUKind FPU = ARM::FK_INVALID;
388 ARM::ArchKind Arch = ARM::ArchKind::INVALID;
389 ARM::ArchKind EmittedArch = ARM::ArchKind::INVALID;
390
391 MCSection *AttributeSection = nullptr;
392
393 void emitArchDefaultAttributes();
394 void emitFPUDefaultAttributes();
395
396 ARMELFStreamer &getStreamer();
397
398 void emitFnStart() override;
399 void emitFnEnd() override;
400 void emitCantUnwind() override;
401 void emitPersonality(const MCSymbol *Personality) override;
402 void emitPersonalityIndex(unsigned Index) override;
403 void emitHandlerData() override;
404 void emitSetFP(unsigned FpReg, unsigned SpReg, int64_t Offset = 0) override;
405 void emitMovSP(unsigned Reg, int64_t Offset = 0) override;
406 void emitPad(int64_t Offset) override;
407 void emitRegSave(const SmallVectorImpl<unsigned> &RegList,
408 bool isVector) override;
409 void emitUnwindRaw(int64_t Offset,
410 const SmallVectorImpl<uint8_t> &Opcodes) override;
411
412 void switchVendor(StringRef Vendor) override;
413 void emitAttribute(unsigned Attribute, unsigned Value) override;
414 void emitTextAttribute(unsigned Attribute, StringRef String) override;
415 void emitIntTextAttribute(unsigned Attribute, unsigned IntValue,
416 StringRef StringValue) override;
417 void emitArch(ARM::ArchKind Arch) override;
418 void emitObjectArch(ARM::ArchKind Arch) override;
419 void emitFPU(ARM::FPUKind FPU) override;
420 void emitInst(uint32_t Inst, char Suffix = '\0') override;
421 void finishAttributeSection() override;
422 void emitLabel(MCSymbol *Symbol) override;
423
424 void annotateTLSDescriptorSequence(const MCSymbolRefExpr *SRE) override;
425 void emitThumbSet(MCSymbol *Symbol, const MCExpr *Value) override;
426
427 // Reset state between object emissions
428 void reset() override;
429
430 void finish() override;
431
432public:
433 ARMTargetELFStreamer(MCStreamer &S)
434 : ARMTargetStreamer(S), CurrentVendor("aeabi") {}
435};
436
437/// Extend the generic ELFStreamer class so that it can emit mapping symbols at
438/// the appropriate points in the object files. These symbols are defined in the
439/// ARM ELF ABI: infocenter.arm.com/help/topic/com.arm.../IHI0044D_aaelf.pdf.
440///
441/// In brief: $a, $t or $d should be emitted at the start of each contiguous
442/// region of ARM code, Thumb code or data in a section. In practice, this
443/// emission does not rely on explicit assembler directives but on inherent
444/// properties of the directives doing the emission (e.g. ".byte" is data, "add
445/// r0, r0, r0" an instruction).
446///
447/// As a result this system is orthogonal to the DataRegion infrastructure used
448/// by MachO. Beware!
449class ARMELFStreamer : public MCELFStreamer {
450public:
451 friend class ARMTargetELFStreamer;
452
453 ARMELFStreamer(MCContext &Context, std::unique_ptr<MCAsmBackend> TAB,
454 std::unique_ptr<MCObjectWriter> OW,
455 std::unique_ptr<MCCodeEmitter> Emitter, bool IsThumb,
456 bool IsAndroid)
457 : MCELFStreamer(Context, std::move(TAB), std::move(OW),
458 std::move(Emitter)),
459 IsThumb(IsThumb), IsAndroid(IsAndroid) {
460 EHReset();
461 }
462
463 ~ARMELFStreamer() override = default;
464
465 // ARM exception handling directives
466 void emitFnStart();
467 void emitFnEnd();
468 void emitCantUnwind();
469 void emitPersonality(const MCSymbol *Per);
470 void emitPersonalityIndex(unsigned index);
471 void emitHandlerData();
472 void emitSetFP(unsigned NewFpReg, unsigned NewSpReg, int64_t Offset = 0);
473 void emitMovSP(unsigned Reg, int64_t Offset = 0);
474 void emitPad(int64_t Offset);
475 void emitRegSave(const SmallVectorImpl<unsigned> &RegList, bool isVector);
476 void emitUnwindRaw(int64_t Offset, const SmallVectorImpl<uint8_t> &Opcodes);
477 void emitFill(const MCExpr &NumBytes, uint64_t FillValue,
478 SMLoc Loc) override {
479 emitDataMappingSymbol();
480 MCObjectStreamer::emitFill(NumBytes, FillValue, Loc);
481 }
482
483 void changeSection(MCSection *Section, uint32_t Subsection) override {
484 LastMappingSymbols[getCurrentSection().first] = std::move(LastEMSInfo);
485 MCELFStreamer::changeSection(Section, Subsection);
486 auto LastMappingSymbol = LastMappingSymbols.find(Section);
487 if (LastMappingSymbol != LastMappingSymbols.end()) {
488 LastEMSInfo = std::move(LastMappingSymbol->second);
489 return;
490 }
491 LastEMSInfo.reset(new ElfMappingSymbolInfo);
492 }
493
494 /// This function is the one used to emit instruction data into the ELF
495 /// streamer. We override it to add the appropriate mapping symbol if
496 /// necessary.
497 void emitInstruction(const MCInst &Inst,
498 const MCSubtargetInfo &STI) override {
499 if (IsThumb)
500 EmitThumbMappingSymbol();
501 else
502 EmitARMMappingSymbol();
503
505 }
506
507 void emitInst(uint32_t Inst, char Suffix) {
508 unsigned Size;
509 char Buffer[4];
510 const bool LittleEndian = getContext().getAsmInfo()->isLittleEndian();
511
512 switch (Suffix) {
513 case '\0':
514 Size = 4;
515
516 assert(!IsThumb);
517 EmitARMMappingSymbol();
518 for (unsigned II = 0, IE = Size; II != IE; II++) {
519 const unsigned I = LittleEndian ? (Size - II - 1) : II;
520 Buffer[Size - II - 1] = uint8_t(Inst >> I * CHAR_BIT);
521 }
522
523 break;
524 case 'n':
525 case 'w':
526 Size = (Suffix == 'n' ? 2 : 4);
527
528 assert(IsThumb);
529 EmitThumbMappingSymbol();
530 // Thumb wide instructions are emitted as a pair of 16-bit words of the
531 // appropriate endianness.
532 for (unsigned II = 0, IE = Size; II != IE; II = II + 2) {
533 const unsigned I0 = LittleEndian ? II + 0 : II + 1;
534 const unsigned I1 = LittleEndian ? II + 1 : II + 0;
535 Buffer[Size - II - 2] = uint8_t(Inst >> I0 * CHAR_BIT);
536 Buffer[Size - II - 1] = uint8_t(Inst >> I1 * CHAR_BIT);
537 }
538
539 break;
540 default:
541 llvm_unreachable("Invalid Suffix");
542 }
543
545 }
546
547 /// This is one of the functions used to emit data into an ELF section, so the
548 /// ARM streamer overrides it to add the appropriate mapping symbol ($d) if
549 /// necessary.
550 void emitBytes(StringRef Data) override {
551 emitDataMappingSymbol();
553 }
554
555 void FlushPendingMappingSymbol() {
556 if (!LastEMSInfo->hasInfo())
557 return;
558 ElfMappingSymbolInfo *EMS = LastEMSInfo.get();
559 emitMappingSymbol("$d", *EMS->F, EMS->Offset);
560 EMS->resetInfo();
561 }
562
563 /// This is one of the functions used to emit data into an ELF section, so the
564 /// ARM streamer overrides it to add the appropriate mapping symbol ($d) if
565 /// necessary.
566 void emitValueImpl(const MCExpr *Value, unsigned Size, SMLoc Loc) override {
567 if (const MCSymbolRefExpr *SRE = dyn_cast_or_null<MCSymbolRefExpr>(Value)) {
568 if (SRE->getKind() == MCSymbolRefExpr::VK_ARM_SBREL && !(Size == 4)) {
569 getContext().reportError(Loc, "relocated expression must be 32-bit");
570 return;
571 }
573 }
574
575 emitDataMappingSymbol();
577 }
578
579 void emitAssemblerFlag(MCAssemblerFlag Flag) override {
581
582 switch (Flag) {
584 return; // no-op here.
585 case MCAF_Code16:
586 IsThumb = true;
587 return; // Change to Thumb mode
588 case MCAF_Code32:
589 IsThumb = false;
590 return; // Change to ARM mode
591 case MCAF_Code64:
592 return;
594 return;
595 }
596 }
597
598 /// If a label is defined before the .type directive sets the label's type
599 /// then the label can't be recorded as thumb function when the label is
600 /// defined. We override emitSymbolAttribute() which is called as part of the
601 /// parsing of .type so that if the symbol has already been defined we can
602 /// record the label as Thumb. FIXME: there is a corner case where the state
603 /// is changed in between the label definition and the .type directive, this
604 /// is not expected to occur in practice and handling it would require the
605 /// backend to track IsThumb for every label.
606 bool emitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute) override {
608
609 if (!IsThumb)
610 return Val;
611
612 unsigned Type = cast<MCSymbolELF>(Symbol)->getType();
613 if ((Type == ELF::STT_FUNC || Type == ELF::STT_GNU_IFUNC) &&
614 Symbol->isDefined())
616
617 return Val;
618 };
619
620private:
621 enum ElfMappingSymbol {
622 EMS_None,
623 EMS_ARM,
624 EMS_Thumb,
625 EMS_Data
626 };
627
628 struct ElfMappingSymbolInfo {
629 void resetInfo() {
630 F = nullptr;
631 Offset = 0;
632 }
633 bool hasInfo() { return F != nullptr; }
634 MCDataFragment *F = nullptr;
635 uint64_t Offset = 0;
636 ElfMappingSymbol State = EMS_None;
637 };
638
639 void emitDataMappingSymbol() {
640 if (LastEMSInfo->State == EMS_Data)
641 return;
642 else if (LastEMSInfo->State == EMS_None) {
643 // This is a tentative symbol, it won't really be emitted until it's
644 // actually needed.
645 ElfMappingSymbolInfo *EMS = LastEMSInfo.get();
646 auto *DF = dyn_cast_or_null<MCDataFragment>(getCurrentFragment());
647 if (!DF)
648 return;
649 EMS->F = DF;
650 EMS->Offset = DF->getContents().size();
651 LastEMSInfo->State = EMS_Data;
652 return;
653 }
654 EmitMappingSymbol("$d");
655 LastEMSInfo->State = EMS_Data;
656 }
657
658 void EmitThumbMappingSymbol() {
659 if (LastEMSInfo->State == EMS_Thumb)
660 return;
661 FlushPendingMappingSymbol();
662 EmitMappingSymbol("$t");
663 LastEMSInfo->State = EMS_Thumb;
664 }
665
666 void EmitARMMappingSymbol() {
667 if (LastEMSInfo->State == EMS_ARM)
668 return;
669 FlushPendingMappingSymbol();
670 EmitMappingSymbol("$a");
671 LastEMSInfo->State = EMS_ARM;
672 }
673
674 void EmitMappingSymbol(StringRef Name) {
675 auto *Symbol = cast<MCSymbolELF>(getContext().createLocalSymbol(Name));
676 emitLabel(Symbol);
677
678 Symbol->setType(ELF::STT_NOTYPE);
679 Symbol->setBinding(ELF::STB_LOCAL);
680 }
681
682 void emitMappingSymbol(StringRef Name, MCDataFragment &F, uint64_t Offset) {
683 auto *Symbol = cast<MCSymbolELF>(getContext().createLocalSymbol(Name));
684 emitLabelAtPos(Symbol, SMLoc(), F, Offset);
685 Symbol->setType(ELF::STT_NOTYPE);
686 Symbol->setBinding(ELF::STB_LOCAL);
687 }
688
689 void emitThumbFunc(MCSymbol *Func) override {
692 }
693
694 // Helper functions for ARM exception handling directives
695 void EHReset();
696
697 // Reset state between object emissions
698 void reset() override;
699
700 void EmitPersonalityFixup(StringRef Name);
701 void FlushPendingOffset();
702 void FlushUnwindOpcodes(bool NoHandlerData);
703
704 void SwitchToEHSection(StringRef Prefix, unsigned Type, unsigned Flags,
705 SectionKind Kind, const MCSymbol &Fn);
706 void SwitchToExTabSection(const MCSymbol &FnStart);
707 void SwitchToExIdxSection(const MCSymbol &FnStart);
708
709 void EmitFixup(const MCExpr *Expr, MCFixupKind Kind);
710
711 bool IsThumb;
712 bool IsAndroid;
713
715 LastMappingSymbols;
716
717 std::unique_ptr<ElfMappingSymbolInfo> LastEMSInfo;
718
719 // ARM Exception Handling Frame Information
720 MCSymbol *ExTab;
721 MCSymbol *FnStart;
722 const MCSymbol *Personality;
723 unsigned PersonalityIndex;
724 unsigned FPReg; // Frame pointer register
725 int64_t FPOffset; // Offset: (final frame pointer) - (initial $sp)
726 int64_t SPOffset; // Offset: (final $sp) - (initial $sp)
727 int64_t PendingOffset; // Offset: (final $sp) - (emitted $sp)
728 bool UsedFP;
729 bool CantUnwind;
731 UnwindOpcodeAssembler UnwindOpAsm;
732};
733
734} // end anonymous namespace
735
736ARMELFStreamer &ARMTargetELFStreamer::getStreamer() {
737 return static_cast<ARMELFStreamer &>(Streamer);
738}
739
740void ARMTargetELFStreamer::emitFnStart() { getStreamer().emitFnStart(); }
741void ARMTargetELFStreamer::emitFnEnd() { getStreamer().emitFnEnd(); }
742void ARMTargetELFStreamer::emitCantUnwind() { getStreamer().emitCantUnwind(); }
743
744void ARMTargetELFStreamer::emitPersonality(const MCSymbol *Personality) {
745 getStreamer().emitPersonality(Personality);
746}
747
748void ARMTargetELFStreamer::emitPersonalityIndex(unsigned Index) {
749 getStreamer().emitPersonalityIndex(Index);
750}
751
752void ARMTargetELFStreamer::emitHandlerData() {
753 getStreamer().emitHandlerData();
754}
755
756void ARMTargetELFStreamer::emitSetFP(unsigned FpReg, unsigned SpReg,
757 int64_t Offset) {
758 getStreamer().emitSetFP(FpReg, SpReg, Offset);
759}
760
761void ARMTargetELFStreamer::emitMovSP(unsigned Reg, int64_t Offset) {
762 getStreamer().emitMovSP(Reg, Offset);
763}
764
765void ARMTargetELFStreamer::emitPad(int64_t Offset) {
766 getStreamer().emitPad(Offset);
767}
768
769void ARMTargetELFStreamer::emitRegSave(const SmallVectorImpl<unsigned> &RegList,
770 bool isVector) {
771 getStreamer().emitRegSave(RegList, isVector);
772}
773
774void ARMTargetELFStreamer::emitUnwindRaw(int64_t Offset,
775 const SmallVectorImpl<uint8_t> &Opcodes) {
776 getStreamer().emitUnwindRaw(Offset, Opcodes);
777}
778
779void ARMTargetELFStreamer::switchVendor(StringRef Vendor) {
780 assert(!Vendor.empty() && "Vendor cannot be empty.");
781
782 if (CurrentVendor == Vendor)
783 return;
784
785 if (!CurrentVendor.empty())
786 finishAttributeSection();
787
788 assert(getStreamer().Contents.empty() &&
789 ".ARM.attributes should be flushed before changing vendor");
790 CurrentVendor = Vendor;
791
792}
793
794void ARMTargetELFStreamer::emitAttribute(unsigned Attribute, unsigned Value) {
795 getStreamer().setAttributeItem(Attribute, Value,
796 /* OverwriteExisting= */ true);
797}
798
799void ARMTargetELFStreamer::emitTextAttribute(unsigned Attribute,
801 getStreamer().setAttributeItem(Attribute, Value,
802 /* OverwriteExisting= */ true);
803}
804
805void ARMTargetELFStreamer::emitIntTextAttribute(unsigned Attribute,
806 unsigned IntValue,
807 StringRef StringValue) {
808 getStreamer().setAttributeItems(Attribute, IntValue, StringValue,
809 /* OverwriteExisting= */ true);
810}
811
812void ARMTargetELFStreamer::emitArch(ARM::ArchKind Value) {
813 Arch = Value;
814}
815
816void ARMTargetELFStreamer::emitObjectArch(ARM::ArchKind Value) {
817 EmittedArch = Value;
818}
819
820void ARMTargetELFStreamer::emitArchDefaultAttributes() {
821 using namespace ARMBuildAttrs;
822 ARMELFStreamer &S = getStreamer();
823
824 S.setAttributeItem(CPU_name, ARM::getCPUAttr(Arch), false);
825
826 if (EmittedArch == ARM::ArchKind::INVALID)
827 S.setAttributeItem(CPU_arch, ARM::getArchAttr(Arch), false);
828 else
829 S.setAttributeItem(CPU_arch, ARM::getArchAttr(EmittedArch), false);
830
831 switch (Arch) {
832 case ARM::ArchKind::ARMV4:
833 S.setAttributeItem(ARM_ISA_use, Allowed, false);
834 break;
835
836 case ARM::ArchKind::ARMV4T:
837 case ARM::ArchKind::ARMV5T:
838 case ARM::ArchKind::XSCALE:
839 case ARM::ArchKind::ARMV5TE:
840 case ARM::ArchKind::ARMV6:
841 S.setAttributeItem(ARM_ISA_use, Allowed, false);
842 S.setAttributeItem(THUMB_ISA_use, Allowed, false);
843 break;
844
845 case ARM::ArchKind::ARMV6T2:
846 S.setAttributeItem(ARM_ISA_use, Allowed, false);
847 S.setAttributeItem(THUMB_ISA_use, AllowThumb32, false);
848 break;
849
850 case ARM::ArchKind::ARMV6K:
851 case ARM::ArchKind::ARMV6KZ:
852 S.setAttributeItem(ARM_ISA_use, Allowed, false);
853 S.setAttributeItem(THUMB_ISA_use, Allowed, false);
854 S.setAttributeItem(Virtualization_use, AllowTZ, false);
855 break;
856
857 case ARM::ArchKind::ARMV6M:
858 S.setAttributeItem(THUMB_ISA_use, Allowed, false);
859 break;
860
861 case ARM::ArchKind::ARMV7A:
862 S.setAttributeItem(CPU_arch_profile, ApplicationProfile, false);
863 S.setAttributeItem(ARM_ISA_use, Allowed, false);
864 S.setAttributeItem(THUMB_ISA_use, AllowThumb32, false);
865 break;
866
867 case ARM::ArchKind::ARMV7R:
868 S.setAttributeItem(CPU_arch_profile, RealTimeProfile, false);
869 S.setAttributeItem(ARM_ISA_use, Allowed, false);
870 S.setAttributeItem(THUMB_ISA_use, AllowThumb32, false);
871 break;
872
873 case ARM::ArchKind::ARMV7EM:
874 case ARM::ArchKind::ARMV7M:
875 S.setAttributeItem(CPU_arch_profile, MicroControllerProfile, false);
876 S.setAttributeItem(THUMB_ISA_use, AllowThumb32, false);
877 break;
878
879 case ARM::ArchKind::ARMV8A:
880 case ARM::ArchKind::ARMV8_1A:
881 case ARM::ArchKind::ARMV8_2A:
882 case ARM::ArchKind::ARMV8_3A:
883 case ARM::ArchKind::ARMV8_4A:
884 case ARM::ArchKind::ARMV8_5A:
885 case ARM::ArchKind::ARMV8_6A:
886 case ARM::ArchKind::ARMV8_7A:
887 case ARM::ArchKind::ARMV8_8A:
888 case ARM::ArchKind::ARMV8_9A:
889 case ARM::ArchKind::ARMV9A:
890 case ARM::ArchKind::ARMV9_1A:
891 case ARM::ArchKind::ARMV9_2A:
892 case ARM::ArchKind::ARMV9_3A:
893 case ARM::ArchKind::ARMV9_4A:
894 case ARM::ArchKind::ARMV9_5A:
895 S.setAttributeItem(CPU_arch_profile, ApplicationProfile, false);
896 S.setAttributeItem(ARM_ISA_use, Allowed, false);
897 S.setAttributeItem(THUMB_ISA_use, AllowThumb32, false);
898 S.setAttributeItem(MPextension_use, Allowed, false);
899 S.setAttributeItem(Virtualization_use, AllowTZVirtualization, false);
900 break;
901
902 case ARM::ArchKind::ARMV8MBaseline:
903 case ARM::ArchKind::ARMV8MMainline:
904 S.setAttributeItem(THUMB_ISA_use, AllowThumbDerived, false);
905 S.setAttributeItem(CPU_arch_profile, MicroControllerProfile, false);
906 break;
907
908 case ARM::ArchKind::IWMMXT:
909 S.setAttributeItem(ARM_ISA_use, Allowed, false);
910 S.setAttributeItem(THUMB_ISA_use, Allowed, false);
911 S.setAttributeItem(WMMX_arch, AllowWMMXv1, false);
912 break;
913
914 case ARM::ArchKind::IWMMXT2:
915 S.setAttributeItem(ARM_ISA_use, Allowed, false);
916 S.setAttributeItem(THUMB_ISA_use, Allowed, false);
917 S.setAttributeItem(WMMX_arch, AllowWMMXv2, false);
918 break;
919
920 default:
921 report_fatal_error("Unknown Arch: " + Twine(ARM::getArchName(Arch)));
922 break;
923 }
924}
925
926void ARMTargetELFStreamer::emitFPU(ARM::FPUKind Value) { FPU = Value; }
927
928void ARMTargetELFStreamer::emitFPUDefaultAttributes() {
929 ARMELFStreamer &S = getStreamer();
930
931 switch (FPU) {
932 case ARM::FK_VFP:
933 case ARM::FK_VFPV2:
935 /* OverwriteExisting= */ false);
936 break;
937
938 case ARM::FK_VFPV3:
940 /* OverwriteExisting= */ false);
941 break;
942
943 case ARM::FK_VFPV3_FP16:
945 /* OverwriteExisting= */ false);
947 /* OverwriteExisting= */ false);
948 break;
949
950 case ARM::FK_VFPV3_D16:
952 /* OverwriteExisting= */ false);
953 break;
954
955 case ARM::FK_VFPV3_D16_FP16:
957 /* OverwriteExisting= */ false);
959 /* OverwriteExisting= */ false);
960 break;
961
962 case ARM::FK_VFPV3XD:
964 /* OverwriteExisting= */ false);
965 break;
966 case ARM::FK_VFPV3XD_FP16:
968 /* OverwriteExisting= */ false);
970 /* OverwriteExisting= */ false);
971 break;
972
973 case ARM::FK_VFPV4:
975 /* OverwriteExisting= */ false);
976 break;
977
978 // ABI_HardFP_use is handled in ARMAsmPrinter, so _SP_D16 is treated the same
979 // as _D16 here.
980 case ARM::FK_FPV4_SP_D16:
981 case ARM::FK_VFPV4_D16:
983 /* OverwriteExisting= */ false);
984 break;
985
986 case ARM::FK_FP_ARMV8:
988 /* OverwriteExisting= */ false);
989 break;
990
991 // FPV5_D16 is identical to FP_ARMV8 except for the number of D registers, so
992 // uses the FP_ARMV8_D16 build attribute.
993 case ARM::FK_FPV5_SP_D16:
994 case ARM::FK_FPV5_D16:
996 /* OverwriteExisting= */ false);
997 break;
998
999 case ARM::FK_NEON:
1001 /* OverwriteExisting= */ false);
1002 S.setAttributeItem(ARMBuildAttrs::Advanced_SIMD_arch,
1004 /* OverwriteExisting= */ false);
1005 break;
1006
1007 case ARM::FK_NEON_FP16:
1009 /* OverwriteExisting= */ false);
1010 S.setAttributeItem(ARMBuildAttrs::Advanced_SIMD_arch,
1012 /* OverwriteExisting= */ false);
1014 /* OverwriteExisting= */ false);
1015 break;
1016
1017 case ARM::FK_NEON_VFPV4:
1019 /* OverwriteExisting= */ false);
1020 S.setAttributeItem(ARMBuildAttrs::Advanced_SIMD_arch,
1022 /* OverwriteExisting= */ false);
1023 break;
1024
1025 case ARM::FK_NEON_FP_ARMV8:
1026 case ARM::FK_CRYPTO_NEON_FP_ARMV8:
1028 /* OverwriteExisting= */ false);
1029 // 'Advanced_SIMD_arch' must be emitted not here, but within
1030 // ARMAsmPrinter::emitAttributes(), depending on hasV8Ops() and hasV8_1a()
1031 break;
1032
1033 case ARM::FK_SOFTVFP:
1034 case ARM::FK_NONE:
1035 break;
1036
1037 default:
1038 report_fatal_error("Unknown FPU: " + Twine(FPU));
1039 break;
1040 }
1041}
1042
1043void ARMTargetELFStreamer::finishAttributeSection() {
1044 ARMELFStreamer &S = getStreamer();
1045
1046 if (FPU != ARM::FK_INVALID)
1047 emitFPUDefaultAttributes();
1048
1049 if (Arch != ARM::ArchKind::INVALID)
1050 emitArchDefaultAttributes();
1051
1052 if (S.Contents.empty())
1053 return;
1054
1055 auto LessTag = [](const MCELFStreamer::AttributeItem &LHS,
1056 const MCELFStreamer::AttributeItem &RHS) -> bool {
1057 // The conformance tag must be emitted first when serialised into an
1058 // object file. Specifically, the addenda to the ARM ABI states that
1059 // (2.3.7.4):
1060 //
1061 // "To simplify recognition by consumers in the common case of claiming
1062 // conformity for the whole file, this tag should be emitted first in a
1063 // file-scope sub-subsection of the first public subsection of the
1064 // attributes section."
1065 //
1066 // So it is special-cased in this comparison predicate when the
1067 // attributes are sorted in finishAttributeSection().
1068 return (RHS.Tag != ARMBuildAttrs::conformance) &&
1069 ((LHS.Tag == ARMBuildAttrs::conformance) || (LHS.Tag < RHS.Tag));
1070 };
1071 llvm::sort(S.Contents, LessTag);
1072
1073 S.emitAttributesSection(CurrentVendor, ".ARM.attributes",
1074 ELF::SHT_ARM_ATTRIBUTES, AttributeSection);
1075
1076 FPU = ARM::FK_INVALID;
1077}
1078
1079void ARMTargetELFStreamer::emitLabel(MCSymbol *Symbol) {
1080 ARMELFStreamer &Streamer = getStreamer();
1081 if (!Streamer.IsThumb)
1082 return;
1083
1084 Streamer.getAssembler().registerSymbol(*Symbol);
1085 unsigned Type = cast<MCSymbolELF>(Symbol)->getType();
1087 Streamer.emitThumbFunc(Symbol);
1088}
1089
1090void ARMTargetELFStreamer::annotateTLSDescriptorSequence(
1091 const MCSymbolRefExpr *S) {
1092 getStreamer().EmitFixup(S, FK_Data_4);
1093}
1094
1095void ARMTargetELFStreamer::emitThumbSet(MCSymbol *Symbol, const MCExpr *Value) {
1096 if (const MCSymbolRefExpr *SRE = dyn_cast<MCSymbolRefExpr>(Value)) {
1097 const MCSymbol &Sym = SRE->getSymbol();
1098 if (!Sym.isDefined()) {
1099 getStreamer().emitAssignment(Symbol, Value);
1100 return;
1101 }
1102 }
1103
1104 getStreamer().emitThumbFunc(Symbol);
1105 getStreamer().emitAssignment(Symbol, Value);
1106}
1107
1108void ARMTargetELFStreamer::emitInst(uint32_t Inst, char Suffix) {
1109 getStreamer().emitInst(Inst, Suffix);
1110}
1111
1112void ARMTargetELFStreamer::reset() { AttributeSection = nullptr; }
1113
1114void ARMTargetELFStreamer::finish() {
1115 ARMTargetStreamer::finish();
1116 finishAttributeSection();
1117
1118 // The mix of execute-only and non-execute-only at link time is
1119 // non-execute-only. To avoid the empty implicitly created .text
1120 // section from making the whole .text section non-execute-only, we
1121 // mark it execute-only if it is empty and there is at least one
1122 // execute-only section in the object.
1123 MCContext &Ctx = getStreamer().getContext();
1124 auto &Asm = getStreamer().getAssembler();
1125 if (any_of(Asm, [](const MCSection &Sec) {
1126 return cast<MCSectionELF>(Sec).getFlags() & ELF::SHF_ARM_PURECODE;
1127 })) {
1128 auto *Text =
1129 static_cast<MCSectionELF *>(Ctx.getObjectFileInfo()->getTextSection());
1130 for (auto &F : *Text)
1131 if (auto *DF = dyn_cast<MCDataFragment>(&F))
1132 if (!DF->getContents().empty())
1133 return;
1134 Text->setFlags(Text->getFlags() | ELF::SHF_ARM_PURECODE);
1135 }
1136}
1137
1138void ARMELFStreamer::reset() {
1139 MCTargetStreamer &TS = *getTargetStreamer();
1140 ARMTargetStreamer &ATS = static_cast<ARMTargetStreamer &>(TS);
1141 ATS.reset();
1143 LastMappingSymbols.clear();
1144 LastEMSInfo.reset();
1145 // MCELFStreamer clear's the assembler's e_flags. However, for
1146 // arm we manually set the ABI version on streamer creation, so
1147 // do the same here
1148 getWriter().setELFHeaderEFlags(ELF::EF_ARM_EABI_VER5);
1149}
1150
1151inline void ARMELFStreamer::SwitchToEHSection(StringRef Prefix,
1152 unsigned Type,
1153 unsigned Flags,
1154 SectionKind Kind,
1155 const MCSymbol &Fn) {
1156 const MCSectionELF &FnSection =
1157 static_cast<const MCSectionELF &>(Fn.getSection());
1158
1159 // Create the name for new section
1160 StringRef FnSecName(FnSection.getName());
1161 SmallString<128> EHSecName(Prefix);
1162 if (FnSecName != ".text") {
1163 EHSecName += FnSecName;
1164 }
1165
1166 // Get .ARM.extab or .ARM.exidx section
1167 const MCSymbolELF *Group = FnSection.getGroup();
1168 if (Group)
1170 MCSectionELF *EHSection = getContext().getELFSection(
1171 EHSecName, Type, Flags, 0, Group, /*IsComdat=*/true,
1172 FnSection.getUniqueID(),
1173 static_cast<const MCSymbolELF *>(FnSection.getBeginSymbol()));
1174
1175 assert(EHSection && "Failed to get the required EH section");
1176
1177 // Switch to .ARM.extab or .ARM.exidx section
1178 switchSection(EHSection);
1179 emitValueToAlignment(Align(4), 0, 1, 0);
1180}
1181
1182inline void ARMELFStreamer::SwitchToExTabSection(const MCSymbol &FnStart) {
1183 SwitchToEHSection(".ARM.extab", ELF::SHT_PROGBITS, ELF::SHF_ALLOC,
1184 SectionKind::getData(), FnStart);
1185}
1186
1187inline void ARMELFStreamer::SwitchToExIdxSection(const MCSymbol &FnStart) {
1188 SwitchToEHSection(".ARM.exidx", ELF::SHT_ARM_EXIDX,
1190 SectionKind::getData(), FnStart);
1191}
1192
1193void ARMELFStreamer::EmitFixup(const MCExpr *Expr, MCFixupKind Kind) {
1194 MCDataFragment *Frag = getOrCreateDataFragment();
1195 Frag->getFixups().push_back(MCFixup::create(Frag->getContents().size(), Expr,
1196 Kind));
1197}
1198
1199void ARMELFStreamer::EHReset() {
1200 ExTab = nullptr;
1201 FnStart = nullptr;
1202 Personality = nullptr;
1203 PersonalityIndex = ARM::EHABI::NUM_PERSONALITY_INDEX;
1204 FPReg = ARM::SP;
1205 FPOffset = 0;
1206 SPOffset = 0;
1207 PendingOffset = 0;
1208 UsedFP = false;
1209 CantUnwind = false;
1210
1211 Opcodes.clear();
1212 UnwindOpAsm.Reset();
1213}
1214
1215void ARMELFStreamer::emitFnStart() {
1216 assert(FnStart == nullptr);
1217 FnStart = getContext().createTempSymbol();
1218 emitLabel(FnStart);
1219}
1220
1221void ARMELFStreamer::emitFnEnd() {
1222 assert(FnStart && ".fnstart must precedes .fnend");
1223
1224 // Emit unwind opcodes if there is no .handlerdata directive
1225 if (!ExTab && !CantUnwind)
1226 FlushUnwindOpcodes(true);
1227
1228 // Emit the exception index table entry
1229 SwitchToExIdxSection(*FnStart);
1230
1231 // The EHABI requires a dependency preserving R_ARM_NONE relocation to the
1232 // personality routine to protect it from an arbitrary platform's static
1233 // linker garbage collection. We disable this for Android where the unwinder
1234 // is either dynamically linked or directly references the personality
1235 // routine.
1236 if (PersonalityIndex < ARM::EHABI::NUM_PERSONALITY_INDEX && !IsAndroid)
1237 EmitPersonalityFixup(GetAEABIUnwindPersonalityName(PersonalityIndex));
1238
1239 const MCSymbolRefExpr *FnStartRef =
1242 getContext());
1243
1244 emitValue(FnStartRef, 4);
1245
1246 if (CantUnwind) {
1248 } else if (ExTab) {
1249 // Emit a reference to the unwind opcodes in the ".ARM.extab" section.
1250 const MCSymbolRefExpr *ExTabEntryRef =
1253 getContext());
1254 emitValue(ExTabEntryRef, 4);
1255 } else {
1256 // For the __aeabi_unwind_cpp_pr0, we have to emit the unwind opcodes in
1257 // the second word of exception index table entry. The size of the unwind
1258 // opcodes should always be 4 bytes.
1259 assert(PersonalityIndex == ARM::EHABI::AEABI_UNWIND_CPP_PR0 &&
1260 "Compact model must use __aeabi_unwind_cpp_pr0 as personality");
1261 assert(Opcodes.size() == 4u &&
1262 "Unwind opcode size for __aeabi_unwind_cpp_pr0 must be equal to 4");
1263 uint64_t Intval = Opcodes[0] |
1264 Opcodes[1] << 8 |
1265 Opcodes[2] << 16 |
1266 Opcodes[3] << 24;
1267 emitIntValue(Intval, Opcodes.size());
1268 }
1269
1270 // Switch to the section containing FnStart
1271 switchSection(&FnStart->getSection());
1272
1273 // Clean exception handling frame information
1274 EHReset();
1275}
1276
1277void ARMELFStreamer::emitCantUnwind() { CantUnwind = true; }
1278
1279// Add the R_ARM_NONE fixup at the same position
1280void ARMELFStreamer::EmitPersonalityFixup(StringRef Name) {
1281 const MCSymbol *PersonalitySym = getContext().getOrCreateSymbol(Name);
1282
1283 const MCSymbolRefExpr *PersonalityRef = MCSymbolRefExpr::create(
1284 PersonalitySym, MCSymbolRefExpr::VK_ARM_NONE, getContext());
1285
1286 visitUsedExpr(*PersonalityRef);
1287 MCDataFragment *DF = getOrCreateDataFragment();
1288 DF->getFixups().push_back(MCFixup::create(DF->getContents().size(),
1289 PersonalityRef,
1290 MCFixup::getKindForSize(4, false)));
1291}
1292
1293void ARMELFStreamer::FlushPendingOffset() {
1294 if (PendingOffset != 0) {
1295 UnwindOpAsm.EmitSPOffset(-PendingOffset);
1296 PendingOffset = 0;
1297 }
1298}
1299
1300void ARMELFStreamer::FlushUnwindOpcodes(bool NoHandlerData) {
1301 // Emit the unwind opcode to restore $sp.
1302 if (UsedFP) {
1303 const MCRegisterInfo *MRI = getContext().getRegisterInfo();
1304 int64_t LastRegSaveSPOffset = SPOffset - PendingOffset;
1305 UnwindOpAsm.EmitSPOffset(LastRegSaveSPOffset - FPOffset);
1306 UnwindOpAsm.EmitSetSP(MRI->getEncodingValue(FPReg));
1307 } else {
1308 FlushPendingOffset();
1309 }
1310
1311 // Finalize the unwind opcode sequence
1312 UnwindOpAsm.Finalize(PersonalityIndex, Opcodes);
1313
1314 // For compact model 0, we have to emit the unwind opcodes in the .ARM.exidx
1315 // section. Thus, we don't have to create an entry in the .ARM.extab
1316 // section.
1317 if (NoHandlerData && PersonalityIndex == ARM::EHABI::AEABI_UNWIND_CPP_PR0)
1318 return;
1319
1320 // Switch to .ARM.extab section.
1321 SwitchToExTabSection(*FnStart);
1322
1323 // Create .ARM.extab label for offset in .ARM.exidx
1324 assert(!ExTab);
1325 ExTab = getContext().createTempSymbol();
1326 emitLabel(ExTab);
1327
1328 // Emit personality
1329 if (Personality) {
1330 const MCSymbolRefExpr *PersonalityRef =
1331 MCSymbolRefExpr::create(Personality,
1333 getContext());
1334
1335 emitValue(PersonalityRef, 4);
1336 }
1337
1338 // Emit unwind opcodes
1339 assert((Opcodes.size() % 4) == 0 &&
1340 "Unwind opcode size for __aeabi_cpp_unwind_pr0 must be multiple of 4");
1341 for (unsigned I = 0; I != Opcodes.size(); I += 4) {
1342 uint64_t Intval = Opcodes[I] |
1343 Opcodes[I + 1] << 8 |
1344 Opcodes[I + 2] << 16 |
1345 Opcodes[I + 3] << 24;
1346 emitInt32(Intval);
1347 }
1348
1349 // According to ARM EHABI section 9.2, if the __aeabi_unwind_cpp_pr1() or
1350 // __aeabi_unwind_cpp_pr2() is used, then the handler data must be emitted
1351 // after the unwind opcodes. The handler data consists of several 32-bit
1352 // words, and should be terminated by zero.
1353 //
1354 // In case that the .handlerdata directive is not specified by the
1355 // programmer, we should emit zero to terminate the handler data.
1356 if (NoHandlerData && !Personality)
1357 emitInt32(0);
1358}
1359
1360void ARMELFStreamer::emitHandlerData() { FlushUnwindOpcodes(false); }
1361
1362void ARMELFStreamer::emitPersonality(const MCSymbol *Per) {
1363 Personality = Per;
1364 UnwindOpAsm.setPersonality(Per);
1365}
1366
1367void ARMELFStreamer::emitPersonalityIndex(unsigned Index) {
1368 assert(Index < ARM::EHABI::NUM_PERSONALITY_INDEX && "invalid index");
1369 PersonalityIndex = Index;
1370}
1371
1372void ARMELFStreamer::emitSetFP(unsigned NewFPReg, unsigned NewSPReg,
1373 int64_t Offset) {
1374 assert((NewSPReg == ARM::SP || NewSPReg == FPReg) &&
1375 "the operand of .setfp directive should be either $sp or $fp");
1376
1377 UsedFP = true;
1378 FPReg = NewFPReg;
1379
1380 if (NewSPReg == ARM::SP)
1381 FPOffset = SPOffset + Offset;
1382 else
1383 FPOffset += Offset;
1384}
1385
1386void ARMELFStreamer::emitMovSP(unsigned Reg, int64_t Offset) {
1387 assert((Reg != ARM::SP && Reg != ARM::PC) &&
1388 "the operand of .movsp cannot be either sp or pc");
1389 assert(FPReg == ARM::SP && "current FP must be SP");
1390
1391 FlushPendingOffset();
1392
1393 FPReg = Reg;
1394 FPOffset = SPOffset + Offset;
1395
1396 const MCRegisterInfo *MRI = getContext().getRegisterInfo();
1397 UnwindOpAsm.EmitSetSP(MRI->getEncodingValue(FPReg));
1398}
1399
1400void ARMELFStreamer::emitPad(int64_t Offset) {
1401 // Track the change of the $sp offset
1402 SPOffset -= Offset;
1403
1404 // To squash multiple .pad directives, we should delay the unwind opcode
1405 // until the .save, .vsave, .handlerdata, or .fnend directives.
1406 PendingOffset -= Offset;
1407}
1408
1409static std::pair<unsigned, unsigned>
1411 const SmallVectorImpl<unsigned> &RegList, bool IsVector,
1412 uint32_t &Mask_) {
1413 uint32_t Mask = 0;
1414 unsigned Count = 0;
1415 while (Idx > 0) {
1416 unsigned Reg = RegList[Idx - 1];
1417 if (Reg == ARM::RA_AUTH_CODE)
1418 break;
1419 Reg = MRI.getEncodingValue(Reg);
1420 assert(Reg < (IsVector ? 32U : 16U) && "Register out of range");
1421 unsigned Bit = (1u << Reg);
1422 if ((Mask & Bit) == 0) {
1423 Mask |= Bit;
1424 ++Count;
1425 }
1426 --Idx;
1427 }
1428
1429 Mask_ = Mask;
1430 return {Idx, Count};
1431}
1432
1433void ARMELFStreamer::emitRegSave(const SmallVectorImpl<unsigned> &RegList,
1434 bool IsVector) {
1435 uint32_t Mask;
1436 unsigned Idx, Count;
1437 const MCRegisterInfo &MRI = *getContext().getRegisterInfo();
1438
1439 // Collect the registers in the register list. Issue unwinding instructions in
1440 // three parts: ordinary hardware registers, return address authentication
1441 // code pseudo register, the rest of the registers. The RA PAC is kept in an
1442 // architectural register (usually r12), but we treat it as a special case in
1443 // order to distinguish between that register containing RA PAC or a general
1444 // value.
1445 Idx = RegList.size();
1446 while (Idx > 0) {
1447 std::tie(Idx, Count) = collectHWRegs(MRI, Idx, RegList, IsVector, Mask);
1448 if (Count) {
1449 // Track the change the $sp offset: For the .save directive, the
1450 // corresponding push instruction will decrease the $sp by (4 * Count).
1451 // For the .vsave directive, the corresponding vpush instruction will
1452 // decrease $sp by (8 * Count).
1453 SPOffset -= Count * (IsVector ? 8 : 4);
1454
1455 // Emit the opcode
1456 FlushPendingOffset();
1457 if (IsVector)
1458 UnwindOpAsm.EmitVFPRegSave(Mask);
1459 else
1460 UnwindOpAsm.EmitRegSave(Mask);
1461 } else if (Idx > 0 && RegList[Idx - 1] == ARM::RA_AUTH_CODE) {
1462 --Idx;
1463 SPOffset -= 4;
1464 FlushPendingOffset();
1465 UnwindOpAsm.EmitRegSave(0);
1466 }
1467 }
1468}
1469
1470void ARMELFStreamer::emitUnwindRaw(int64_t Offset,
1471 const SmallVectorImpl<uint8_t> &Opcodes) {
1472 FlushPendingOffset();
1473 SPOffset = SPOffset - Offset;
1474 UnwindOpAsm.EmitRaw(Opcodes);
1475}
1476
1477namespace llvm {
1478
1481 MCInstPrinter *InstPrint) {
1482 return new ARMTargetAsmStreamer(S, OS, *InstPrint);
1483}
1484
1486 return new ARMTargetStreamer(S);
1487}
1488
1490 return new ARMTargetELFStreamer(S);
1491}
1492
1494 std::unique_ptr<MCAsmBackend> TAB,
1495 std::unique_ptr<MCObjectWriter> OW,
1496 std::unique_ptr<MCCodeEmitter> Emitter,
1497 bool IsThumb, bool IsAndroid) {
1498 ARMELFStreamer *S =
1499 new ARMELFStreamer(Context, std::move(TAB), std::move(OW),
1500 std::move(Emitter), IsThumb, IsAndroid);
1501 // FIXME: This should eventually end up somewhere else where more
1502 // intelligent flag decisions can be made. For now we are just maintaining
1503 // the status quo for ARM and setting EF_ARM_EABI_VER5 as the default.
1504 S->getWriter().setELFHeaderEFlags(ELF::EF_ARM_EABI_VER5);
1505
1506 return S;
1507}
1508
1509} // end namespace llvm
unsigned const MachineRegisterInfo * MRI
static std::string GetAEABIUnwindPersonalityName(unsigned Index)
static std::pair< unsigned, unsigned > collectHWRegs(const MCRegisterInfo &MRI, unsigned Idx, const SmallVectorImpl< unsigned > &RegList, bool IsVector, uint32_t &Mask_)
dxil DXContainer Global Emitter
Returns the sub type a function will return at a given Idx Should correspond to the result type of an ExtractValue instruction executed with just that one unsigned Idx
static RegisterPass< DebugifyFunctionPass > DF("debugify-function", "Attach debug info to a function")
This file defines the DenseMap class.
std::string Name
uint64_t Size
Symbol * Sym
Definition: ELF_riscv.cpp:479
#define F(x, y, z)
Definition: MD5.cpp:55
#define I(x, y, z)
Definition: MD5.cpp:58
unsigned Reg
uint64_t IntrinsicInst * II
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
raw_pwrite_stream & OS
This file defines the SmallString class.
This file defines the SmallVector class.
This file contains some functions that are useful when dealing with strings.
Value * RHS
Value * LHS
virtual void emitUnwindRaw(int64_t StackOffset, const SmallVectorImpl< uint8_t > &Opcodes)
virtual void reset()
Reset any state between object emissions, i.e.
virtual void annotateTLSDescriptorSequence(const MCSymbolRefExpr *SRE)
virtual void finishAttributeSection()
virtual void emitARMWinCFISaveSP(unsigned Reg)
virtual void emitPersonalityIndex(unsigned Index)
virtual void emitInst(uint32_t Inst, char Suffix='\0')
virtual void emitARMWinCFISaveLR(unsigned Offset)
virtual void emitArchExtension(uint64_t ArchExt)
virtual void emitRegSave(const SmallVectorImpl< unsigned > &RegList, bool isVector)
virtual void emitTextAttribute(unsigned Attribute, StringRef String)
virtual void emitARMWinCFIAllocStack(unsigned Size, bool Wide)
virtual void emitMovSP(unsigned Reg, int64_t Offset=0)
virtual void emitARMWinCFICustom(unsigned Opcode)
virtual void emitARMWinCFISaveRegMask(unsigned Mask, bool Wide)
virtual void emitARMWinCFIEpilogEnd()
virtual void emitARMWinCFIPrologEnd(bool Fragment)
virtual void switchVendor(StringRef Vendor)
virtual void emitPersonality(const MCSymbol *Personality)
virtual void emitObjectArch(ARM::ArchKind Arch)
virtual void emitIntTextAttribute(unsigned Attribute, unsigned IntValue, StringRef StringValue="")
virtual void emitThumbSet(MCSymbol *Symbol, const MCExpr *Value)
virtual void emitFPU(ARM::FPUKind FPU)
virtual void emitARMWinCFISaveFRegs(unsigned First, unsigned Last)
virtual void emitARMWinCFIEpilogStart(unsigned Condition)
virtual void emitPad(int64_t Offset)
virtual void emitAttribute(unsigned Attribute, unsigned Value)
virtual void emitARMWinCFINop(bool Wide)
virtual void emitSetFP(unsigned FpReg, unsigned SpReg, int64_t Offset=0)
virtual void emitArch(ARM::ArchKind Arch)
This class is intended to be used as a base class for asm properties and features specific to the tar...
Definition: MCAsmInfo.h:56
bool isLittleEndian() const
True if the target is little endian.
Definition: MCAsmInfo.h:527
void setIsThumbFunc(const MCSymbol *Func)
Flag a function symbol as the target of a .thumb_func directive.
Definition: MCAssembler.h:176
Context object for machine code objects.
Definition: MCContext.h:83
const MCObjectFileInfo * getObjectFileInfo() const
Definition: MCContext.h:416
const MCAsmInfo * getAsmInfo() const
Definition: MCContext.h:412
void reportError(SMLoc L, const Twine &Msg)
Definition: MCContext.cpp:1068
Fragment for data and encoded instructions.
Definition: MCFragment.h:219
void changeSection(MCSection *Section, uint32_t Subsection=0) override
This is called by popSection and switchSection, if the current section changes.
void emitValueImpl(const MCExpr *Value, unsigned Size, SMLoc Loc=SMLoc()) override
Emit the expression Value into the output as a native integer of the given Size bytes.
void emitAssemblerFlag(MCAssemblerFlag Flag) override
Note in the output the specified Flag.
void emitThumbFunc(MCSymbol *Func) override
Note in the output that the specified Func is a Thumb mode function (ARM target only).
void reset() override
state management
Definition: MCELFStreamer.h:41
void emitLabel(MCSymbol *Symbol, SMLoc Loc=SMLoc()) override
Emit a label for Symbol into the current section.
bool emitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute) override
Add the given Attribute to Symbol.
void emitLabelAtPos(MCSymbol *Symbol, SMLoc Loc, MCDataFragment &F, uint64_t Offset) override
SmallVectorImpl< MCFixup > & getFixups()
Definition: MCFragment.h:200
SmallVectorImpl< char > & getContents()
Definition: MCFragment.h:197
Base class for the full range of assembler expressions which are needed for parsing.
Definition: MCExpr.h:34
static MCFixupKind getKindForSize(unsigned Size, bool IsPCRel)
Return the generic fixup kind for a value with the given size.
Definition: MCFixup.h:109
static MCFixup create(uint32_t Offset, const MCExpr *Value, MCFixupKind Kind, SMLoc Loc=SMLoc())
Definition: MCFixup.h:87
This is an instance of a target assembly language printer that converts an MCInst to valid target ass...
Definition: MCInstPrinter.h:45
Instances of this class represent a single low-level machine instruction.
Definition: MCInst.h:184
MCSection * getTextSection() const
void emitFill(const MCExpr &NumBytes, uint64_t FillValue, SMLoc Loc=SMLoc()) override
Emit Size bytes worth of the value specified by FillValue.
MCDataFragment * getOrCreateDataFragment(const MCSubtargetInfo *STI=nullptr)
Get a data fragment to write into, creating a new one if the current fragment is not a data fragment.
MCAssembler & getAssembler()
void emitBytes(StringRef Data) override
Emit the bytes in Data into the output.
void emitInstruction(const MCInst &Inst, const MCSubtargetInfo &STI) override
Emit the given Instruction into the current section.
MCRegisterInfo base class - We assume that the target defines a static array of MCRegisterDesc object...
This represents a section on linux, lots of unix variants and some bare metal systems.
Definition: MCSectionELF.h:27
unsigned getUniqueID() const
Definition: MCSectionELF.h:92
const MCSymbolELF * getGroup() const
Definition: MCSectionELF.h:82
Instances of this class represent a uniqued identifier for a section in the current translation unit.
Definition: MCSection.h:36
StringRef getName() const
Definition: MCSection.h:130
MCSymbol * getBeginSymbol()
Definition: MCSection.h:135
Streaming machine code generation interface.
Definition: MCStreamer.h:213
MCFragment * getCurrentFragment() const
Definition: MCStreamer.h:409
MCContext & getContext() const
Definition: MCStreamer.h:300
MCSectionSubPair getCurrentSection() const
Return the current section that the streamer is emitting code to.
Definition: MCStreamer.h:393
Generic base class for all target subtargets.
Represent a reference to a symbol from inside an expression.
Definition: MCExpr.h:188
const MCSymbol & getSymbol() const
Definition: MCExpr.h:406
static const MCSymbolRefExpr * create(const MCSymbol *Symbol, MCContext &Ctx)
Definition: MCExpr.h:393
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:41
StringRef getName() const
getName - Get the symbol name.
Definition: MCSymbol.h:205
MCSection & getSection() const
Get the section associated with a defined, non-absolute symbol.
Definition: MCSymbol.h:269
Target specific streamer interface.
Definition: MCStreamer.h:94
virtual void finish()
Definition: MCStreamer.cpp:55
MCStreamer & getStreamer()
Definition: MCStreamer.h:102
virtual void emitLabel(MCSymbol *Symbol)
Definition: MCStreamer.cpp:53
Represents a location in source code.
Definition: SMLoc.h:23
SectionKind - This is a simple POD value that classifies the properties of a section.
Definition: SectionKind.h:22
static SectionKind getData()
Definition: SectionKind.h:213
SmallString - A SmallString is just a SmallVector with methods and accessors that make it work better...
Definition: SmallString.h:26
size_t size() const
Definition: SmallVector.h:91
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: SmallVector.h:586
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1209
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
constexpr bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:134
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:81
static Twine utohexstr(const uint64_t &Val)
Definition: Twine.h:416
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
LLVM Value Representation.
Definition: Value.h:74
void print(raw_ostream &O, bool IsForDebug=false) const
Implement operator<< on Value.
Definition: AsmWriter.cpp:5023
formatted_raw_ostream - A raw_ostream that wraps another one and keeps track of line and column posit...
raw_ostream & write_escaped(StringRef Str, bool UseHexEscapes=false)
Output Str, turning '\', '\t', ' ', '"', and anything that doesn't satisfy llvm::isPrint into an esca...
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
const TagNameMap & getARMAttributeTags()
@ EXIDX_CANTUNWIND
Special entry for the function never unwind.
Definition: ARMEHABI.h:35
StringRef getArchExtName(uint64_t ArchExtKind)
StringRef getCPUAttr(ArchKind AK)
StringRef getArchName(ArchKind AK)
unsigned getArchAttr(ArchKind AK)
StringRef getFPUName(FPUKind FPUKind)
constexpr std::underlying_type_t< E > Mask()
Get a bitmask with 1s in all places up to the high-order bit of E's largest value.
Definition: BitmaskEnum.h:121
StringRef attrTypeAsString(unsigned attr, TagNameMap tagNameMap, bool hasTagPrefix=true)
@ SHT_PROGBITS
Definition: ELF.h:1089
@ SHT_ARM_ATTRIBUTES
Definition: ELF.h:1148
@ SHT_ARM_EXIDX
Definition: ELF.h:1144
@ EF_ARM_EABI_VER5
Definition: ELF.h:448
@ STB_LOCAL
Definition: ELF.h:1339
@ SHF_ALLOC
Definition: ELF.h:1186
@ SHF_LINK_ORDER
Definition: ELF.h:1201
@ SHF_GROUP
Definition: ELF.h:1208
@ SHF_ARM_PURECODE
Definition: ELF.h:1281
@ STT_FUNC
Definition: ELF.h:1353
@ STT_NOTYPE
Definition: ELF.h:1351
@ STT_GNU_IFUNC
Definition: ELF.h:1358
Reg
All possible values of the reg field in the ModR/M byte.
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
@ Offset
Definition: DWP.cpp:480
MCELFStreamer * createARMELFStreamer(MCContext &Context, std::unique_ptr< MCAsmBackend > TAB, std::unique_ptr< MCObjectWriter > OW, std::unique_ptr< MCCodeEmitter > Emitter, bool IsThumb, bool IsAndroid)
MCTargetStreamer * createARMObjectTargetELFStreamer(MCStreamer &S)
bool any_of(R &&range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly.
Definition: STLExtras.h:1729
void sort(IteratorTy Start, IteratorTy End)
Definition: STLExtras.h:1647
void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
Definition: Error.cpp:167
MCFixupKind
Extensible enumeration to represent the type of a fixup.
Definition: MCFixup.h:21
@ FK_Data_4
A four-byte fixup.
Definition: MCFixup.h:25
@ First
Helpers to iterate all locations in the MemoryEffectsBase class.
MCAssemblerFlag
Definition: MCDirectives.h:53
@ MCAF_SyntaxUnified
.syntax (ARM/ELF)
Definition: MCDirectives.h:54
@ MCAF_Code64
.code64 (X86)
Definition: MCDirectives.h:58
@ MCAF_Code16
.code16 (X86) / .code 16 (ARM)
Definition: MCDirectives.h:56
@ MCAF_Code32
.code32 (X86) / .code 32 (ARM)
Definition: MCDirectives.h:57
@ MCAF_SubsectionsViaSymbols
.subsections_via_symbols (MachO)
Definition: MCDirectives.h:55
OutputIt move(R &&Range, OutputIt Out)
Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.
Definition: STLExtras.h:1856
static const char * ARMCondCodeToString(ARMCC::CondCodes CC)
Definition: ARMBaseInfo.h:146
MCTargetStreamer * createARMNullTargetStreamer(MCStreamer &S)
MCTargetStreamer * createARMTargetAsmStreamer(MCStreamer &S, formatted_raw_ostream &OS, MCInstPrinter *InstPrint)
MCSymbolAttr
Definition: MCDirectives.h:18
@ MCSA_ELF_TypeFunction
.type _foo, STT_FUNC # aka @function
Definition: MCDirectives.h:23
Implement std::hash so that hash_code can be used in STL containers.
Definition: BitVector.h:858
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition: Alignment.h:39
ELF object attributes section emission support.
Definition: MCELFStreamer.h:95