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