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