[Alignment][NFC] Use Align with TargetLowering::setMinFunctionAlignment
[llvm-core.git] / lib / Target / ARM / MCTargetDesc / ARMELFStreamer.cpp
blobf51fbdcd84da7c62bdca9ee72c6c6b307e12a9e4
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.
13 //===----------------------------------------------------------------------===//
15 #include "ARMRegisterInfo.h"
16 #include "ARMUnwindOpAsm.h"
17 #include "llvm/ADT/DenseMap.h"
18 #include "llvm/ADT/SmallString.h"
19 #include "llvm/ADT/SmallVector.h"
20 #include "llvm/ADT/StringRef.h"
21 #include "llvm/ADT/Triple.h"
22 #include "llvm/ADT/Twine.h"
23 #include "llvm/BinaryFormat/ELF.h"
24 #include "llvm/MC/MCAsmBackend.h"
25 #include "llvm/MC/MCAsmInfo.h"
26 #include "llvm/MC/MCAssembler.h"
27 #include "llvm/MC/MCCodeEmitter.h"
28 #include "llvm/MC/MCContext.h"
29 #include "llvm/MC/MCELFStreamer.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"
34 #include "llvm/MC/MCInstPrinter.h"
35 #include "llvm/MC/MCObjectWriter.h"
36 #include "llvm/MC/MCRegisterInfo.h"
37 #include "llvm/MC/MCSection.h"
38 #include "llvm/MC/MCSectionELF.h"
39 #include "llvm/MC/MCStreamer.h"
40 #include "llvm/MC/MCSubtargetInfo.h"
41 #include "llvm/MC/MCSymbol.h"
42 #include "llvm/MC/MCSymbolELF.h"
43 #include "llvm/MC/SectionKind.h"
44 #include "llvm/Support/ARMBuildAttributes.h"
45 #include "llvm/Support/ARMEHABI.h"
46 #include "llvm/Support/Casting.h"
47 #include "llvm/Support/ErrorHandling.h"
48 #include "llvm/Support/FormattedStream.h"
49 #include "llvm/Support/LEB128.h"
50 #include "llvm/Support/TargetParser.h"
51 #include "llvm/Support/raw_ostream.h"
52 #include <algorithm>
53 #include <cassert>
54 #include <climits>
55 #include <cstddef>
56 #include <cstdint>
57 #include <string>
59 using namespace llvm;
61 static std::string GetAEABIUnwindPersonalityName(unsigned Index) {
62 assert(Index < ARM::EHABI::NUM_PERSONALITY_INDEX &&
63 "Invalid personality index");
64 return (Twine("__aeabi_unwind_cpp_pr") + Twine(Index)).str();
67 namespace {
69 class ARMELFStreamer;
71 class ARMTargetAsmStreamer : public ARMTargetStreamer {
72 formatted_raw_ostream &OS;
73 MCInstPrinter &InstPrinter;
74 bool IsVerboseAsm;
76 void emitFnStart() override;
77 void emitFnEnd() override;
78 void emitCantUnwind() override;
79 void emitPersonality(const MCSymbol *Personality) override;
80 void emitPersonalityIndex(unsigned Index) override;
81 void emitHandlerData() override;
82 void emitSetFP(unsigned FpReg, unsigned SpReg, int64_t Offset = 0) override;
83 void emitMovSP(unsigned Reg, int64_t Offset = 0) override;
84 void emitPad(int64_t Offset) override;
85 void emitRegSave(const SmallVectorImpl<unsigned> &RegList,
86 bool isVector) override;
87 void emitUnwindRaw(int64_t Offset,
88 const SmallVectorImpl<uint8_t> &Opcodes) override;
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(unsigned ArchExt) override;
97 void emitObjectArch(ARM::ArchKind Arch) override;
98 void emitFPU(unsigned FPU) override;
99 void emitInst(uint32_t Inst, char Suffix = '\0') override;
100 void finishAttributeSection() override;
102 void AnnotateTLSDescriptorSequence(const MCSymbolRefExpr *SRE) override;
103 void emitThumbSet(MCSymbol *Symbol, const MCExpr *Value) override;
105 public:
106 ARMTargetAsmStreamer(MCStreamer &S, formatted_raw_ostream &OS,
107 MCInstPrinter &InstPrinter, bool VerboseAsm);
110 ARMTargetAsmStreamer::ARMTargetAsmStreamer(MCStreamer &S,
111 formatted_raw_ostream &OS,
112 MCInstPrinter &InstPrinter,
113 bool VerboseAsm)
114 : ARMTargetStreamer(S), OS(OS), InstPrinter(InstPrinter),
115 IsVerboseAsm(VerboseAsm) {}
117 void ARMTargetAsmStreamer::emitFnStart() { OS << "\t.fnstart\n"; }
118 void ARMTargetAsmStreamer::emitFnEnd() { OS << "\t.fnend\n"; }
119 void ARMTargetAsmStreamer::emitCantUnwind() { OS << "\t.cantunwind\n"; }
121 void ARMTargetAsmStreamer::emitPersonality(const MCSymbol *Personality) {
122 OS << "\t.personality " << Personality->getName() << '\n';
125 void ARMTargetAsmStreamer::emitPersonalityIndex(unsigned Index) {
126 OS << "\t.personalityindex " << Index << '\n';
129 void ARMTargetAsmStreamer::emitHandlerData() { OS << "\t.handlerdata\n"; }
131 void ARMTargetAsmStreamer::emitSetFP(unsigned FpReg, unsigned SpReg,
132 int64_t Offset) {
133 OS << "\t.setfp\t";
134 InstPrinter.printRegName(OS, FpReg);
135 OS << ", ";
136 InstPrinter.printRegName(OS, SpReg);
137 if (Offset)
138 OS << ", #" << Offset;
139 OS << '\n';
142 void ARMTargetAsmStreamer::emitMovSP(unsigned Reg, int64_t Offset) {
143 assert((Reg != ARM::SP && Reg != ARM::PC) &&
144 "the operand of .movsp cannot be either sp or pc");
146 OS << "\t.movsp\t";
147 InstPrinter.printRegName(OS, Reg);
148 if (Offset)
149 OS << ", #" << Offset;
150 OS << '\n';
153 void ARMTargetAsmStreamer::emitPad(int64_t Offset) {
154 OS << "\t.pad\t#" << Offset << '\n';
157 void ARMTargetAsmStreamer::emitRegSave(const SmallVectorImpl<unsigned> &RegList,
158 bool isVector) {
159 assert(RegList.size() && "RegList should not be empty");
160 if (isVector)
161 OS << "\t.vsave\t{";
162 else
163 OS << "\t.save\t{";
165 InstPrinter.printRegName(OS, RegList[0]);
167 for (unsigned i = 1, e = RegList.size(); i != e; ++i) {
168 OS << ", ";
169 InstPrinter.printRegName(OS, RegList[i]);
172 OS << "}\n";
175 void ARMTargetAsmStreamer::switchVendor(StringRef Vendor) {}
177 void ARMTargetAsmStreamer::emitAttribute(unsigned Attribute, unsigned Value) {
178 OS << "\t.eabi_attribute\t" << Attribute << ", " << Twine(Value);
179 if (IsVerboseAsm) {
180 StringRef Name = ARMBuildAttrs::AttrTypeAsString(Attribute);
181 if (!Name.empty())
182 OS << "\t@ " << Name;
184 OS << "\n";
187 void ARMTargetAsmStreamer::emitTextAttribute(unsigned Attribute,
188 StringRef String) {
189 switch (Attribute) {
190 case ARMBuildAttrs::CPU_name:
191 OS << "\t.cpu\t" << String.lower();
192 break;
193 default:
194 OS << "\t.eabi_attribute\t" << Attribute << ", \"" << String << "\"";
195 if (IsVerboseAsm) {
196 StringRef Name = ARMBuildAttrs::AttrTypeAsString(Attribute);
197 if (!Name.empty())
198 OS << "\t@ " << Name;
200 break;
202 OS << "\n";
205 void ARMTargetAsmStreamer::emitIntTextAttribute(unsigned Attribute,
206 unsigned IntValue,
207 StringRef StringValue) {
208 switch (Attribute) {
209 default: llvm_unreachable("unsupported multi-value attribute in asm mode");
210 case ARMBuildAttrs::compatibility:
211 OS << "\t.eabi_attribute\t" << Attribute << ", " << IntValue;
212 if (!StringValue.empty())
213 OS << ", \"" << StringValue << "\"";
214 if (IsVerboseAsm)
215 OS << "\t@ " << ARMBuildAttrs::AttrTypeAsString(Attribute);
216 break;
218 OS << "\n";
221 void ARMTargetAsmStreamer::emitArch(ARM::ArchKind Arch) {
222 OS << "\t.arch\t" << ARM::getArchName(Arch) << "\n";
225 void ARMTargetAsmStreamer::emitArchExtension(unsigned ArchExt) {
226 OS << "\t.arch_extension\t" << ARM::getArchExtName(ArchExt) << "\n";
229 void ARMTargetAsmStreamer::emitObjectArch(ARM::ArchKind Arch) {
230 OS << "\t.object_arch\t" << ARM::getArchName(Arch) << '\n';
233 void ARMTargetAsmStreamer::emitFPU(unsigned FPU) {
234 OS << "\t.fpu\t" << ARM::getFPUName(FPU) << "\n";
237 void ARMTargetAsmStreamer::finishAttributeSection() {}
239 void
240 ARMTargetAsmStreamer::AnnotateTLSDescriptorSequence(const MCSymbolRefExpr *S) {
241 OS << "\t.tlsdescseq\t" << S->getSymbol().getName();
244 void ARMTargetAsmStreamer::emitThumbSet(MCSymbol *Symbol, const MCExpr *Value) {
245 const MCAsmInfo *MAI = Streamer.getContext().getAsmInfo();
247 OS << "\t.thumb_set\t";
248 Symbol->print(OS, MAI);
249 OS << ", ";
250 Value->print(OS, MAI);
251 OS << '\n';
254 void ARMTargetAsmStreamer::emitInst(uint32_t Inst, char Suffix) {
255 OS << "\t.inst";
256 if (Suffix)
257 OS << "." << Suffix;
258 OS << "\t0x" << Twine::utohexstr(Inst) << "\n";
261 void ARMTargetAsmStreamer::emitUnwindRaw(int64_t Offset,
262 const SmallVectorImpl<uint8_t> &Opcodes) {
263 OS << "\t.unwind_raw " << Offset;
264 for (SmallVectorImpl<uint8_t>::const_iterator OCI = Opcodes.begin(),
265 OCE = Opcodes.end();
266 OCI != OCE; ++OCI)
267 OS << ", 0x" << Twine::utohexstr(*OCI);
268 OS << '\n';
271 class ARMTargetELFStreamer : public ARMTargetStreamer {
272 private:
273 // This structure holds all attributes, accounting for
274 // their string/numeric value, so we can later emit them
275 // in declaration order, keeping all in the same vector
276 struct AttributeItem {
277 enum {
278 HiddenAttribute = 0,
279 NumericAttribute,
280 TextAttribute,
281 NumericAndTextAttributes
282 } Type;
283 unsigned Tag;
284 unsigned IntValue;
285 std::string StringValue;
287 static bool LessTag(const AttributeItem &LHS, const AttributeItem &RHS) {
288 // The conformance tag must be emitted first when serialised
289 // into an object file. Specifically, the addenda to the ARM ABI
290 // states that (2.3.7.4):
292 // "To simplify recognition by consumers in the common case of
293 // claiming conformity for the whole file, this tag should be
294 // emitted first in a file-scope sub-subsection of the first
295 // public subsection of the attributes section."
297 // So it is special-cased in this comparison predicate when the
298 // attributes are sorted in finishAttributeSection().
299 return (RHS.Tag != ARMBuildAttrs::conformance) &&
300 ((LHS.Tag == ARMBuildAttrs::conformance) || (LHS.Tag < RHS.Tag));
304 StringRef CurrentVendor;
305 unsigned FPU = ARM::FK_INVALID;
306 ARM::ArchKind Arch = ARM::ArchKind::INVALID;
307 ARM::ArchKind EmittedArch = ARM::ArchKind::INVALID;
308 SmallVector<AttributeItem, 64> Contents;
310 MCSection *AttributeSection = nullptr;
312 AttributeItem *getAttributeItem(unsigned Attribute) {
313 for (size_t i = 0; i < Contents.size(); ++i)
314 if (Contents[i].Tag == Attribute)
315 return &Contents[i];
316 return nullptr;
319 void setAttributeItem(unsigned Attribute, unsigned Value,
320 bool OverwriteExisting) {
321 // Look for existing attribute item
322 if (AttributeItem *Item = getAttributeItem(Attribute)) {
323 if (!OverwriteExisting)
324 return;
325 Item->Type = AttributeItem::NumericAttribute;
326 Item->IntValue = Value;
327 return;
330 // Create new attribute item
331 AttributeItem Item = {
332 AttributeItem::NumericAttribute,
333 Attribute,
334 Value,
335 StringRef("")
337 Contents.push_back(Item);
340 void setAttributeItem(unsigned Attribute, StringRef Value,
341 bool OverwriteExisting) {
342 // Look for existing attribute item
343 if (AttributeItem *Item = getAttributeItem(Attribute)) {
344 if (!OverwriteExisting)
345 return;
346 Item->Type = AttributeItem::TextAttribute;
347 Item->StringValue = Value;
348 return;
351 // Create new attribute item
352 AttributeItem Item = {
353 AttributeItem::TextAttribute,
354 Attribute,
356 Value
358 Contents.push_back(Item);
361 void setAttributeItems(unsigned Attribute, unsigned IntValue,
362 StringRef StringValue, bool OverwriteExisting) {
363 // Look for existing attribute item
364 if (AttributeItem *Item = getAttributeItem(Attribute)) {
365 if (!OverwriteExisting)
366 return;
367 Item->Type = AttributeItem::NumericAndTextAttributes;
368 Item->IntValue = IntValue;
369 Item->StringValue = StringValue;
370 return;
373 // Create new attribute item
374 AttributeItem Item = {
375 AttributeItem::NumericAndTextAttributes,
376 Attribute,
377 IntValue,
378 StringValue
380 Contents.push_back(Item);
383 void emitArchDefaultAttributes();
384 void emitFPUDefaultAttributes();
386 ARMELFStreamer &getStreamer();
388 void emitFnStart() override;
389 void emitFnEnd() override;
390 void emitCantUnwind() override;
391 void emitPersonality(const MCSymbol *Personality) override;
392 void emitPersonalityIndex(unsigned Index) override;
393 void emitHandlerData() override;
394 void emitSetFP(unsigned FpReg, unsigned SpReg, int64_t Offset = 0) override;
395 void emitMovSP(unsigned Reg, int64_t Offset = 0) override;
396 void emitPad(int64_t Offset) override;
397 void emitRegSave(const SmallVectorImpl<unsigned> &RegList,
398 bool isVector) override;
399 void emitUnwindRaw(int64_t Offset,
400 const SmallVectorImpl<uint8_t> &Opcodes) override;
402 void switchVendor(StringRef Vendor) override;
403 void emitAttribute(unsigned Attribute, unsigned Value) override;
404 void emitTextAttribute(unsigned Attribute, StringRef String) override;
405 void emitIntTextAttribute(unsigned Attribute, unsigned IntValue,
406 StringRef StringValue) override;
407 void emitArch(ARM::ArchKind Arch) override;
408 void emitObjectArch(ARM::ArchKind Arch) override;
409 void emitFPU(unsigned FPU) override;
410 void emitInst(uint32_t Inst, char Suffix = '\0') override;
411 void finishAttributeSection() override;
412 void emitLabel(MCSymbol *Symbol) override;
414 void AnnotateTLSDescriptorSequence(const MCSymbolRefExpr *SRE) override;
415 void emitThumbSet(MCSymbol *Symbol, const MCExpr *Value) override;
417 size_t calculateContentSize() const;
419 // Reset state between object emissions
420 void reset() override;
422 public:
423 ARMTargetELFStreamer(MCStreamer &S)
424 : ARMTargetStreamer(S), CurrentVendor("aeabi") {}
427 /// Extend the generic ELFStreamer class so that it can emit mapping symbols at
428 /// the appropriate points in the object files. These symbols are defined in the
429 /// ARM ELF ABI: infocenter.arm.com/help/topic/com.arm.../IHI0044D_aaelf.pdf.
431 /// In brief: $a, $t or $d should be emitted at the start of each contiguous
432 /// region of ARM code, Thumb code or data in a section. In practice, this
433 /// emission does not rely on explicit assembler directives but on inherent
434 /// properties of the directives doing the emission (e.g. ".byte" is data, "add
435 /// r0, r0, r0" an instruction).
437 /// As a result this system is orthogonal to the DataRegion infrastructure used
438 /// by MachO. Beware!
439 class ARMELFStreamer : public MCELFStreamer {
440 public:
441 friend class ARMTargetELFStreamer;
443 ARMELFStreamer(MCContext &Context, std::unique_ptr<MCAsmBackend> TAB,
444 std::unique_ptr<MCObjectWriter> OW, std::unique_ptr<MCCodeEmitter> Emitter,
445 bool IsThumb)
446 : MCELFStreamer(Context, std::move(TAB), std::move(OW), std::move(Emitter)),
447 IsThumb(IsThumb) {
448 EHReset();
451 ~ARMELFStreamer() override = default;
453 void FinishImpl() override;
455 // ARM exception handling directives
456 void emitFnStart();
457 void emitFnEnd();
458 void emitCantUnwind();
459 void emitPersonality(const MCSymbol *Per);
460 void emitPersonalityIndex(unsigned index);
461 void emitHandlerData();
462 void emitSetFP(unsigned NewFpReg, unsigned NewSpReg, int64_t Offset = 0);
463 void emitMovSP(unsigned Reg, int64_t Offset = 0);
464 void emitPad(int64_t Offset);
465 void emitRegSave(const SmallVectorImpl<unsigned> &RegList, bool isVector);
466 void emitUnwindRaw(int64_t Offset, const SmallVectorImpl<uint8_t> &Opcodes);
467 void emitFill(const MCExpr &NumBytes, uint64_t FillValue,
468 SMLoc Loc) override {
469 EmitDataMappingSymbol();
470 MCObjectStreamer::emitFill(NumBytes, FillValue, Loc);
473 void ChangeSection(MCSection *Section, const MCExpr *Subsection) override {
474 LastMappingSymbols[getCurrentSection().first] = std::move(LastEMSInfo);
475 MCELFStreamer::ChangeSection(Section, Subsection);
476 auto LastMappingSymbol = LastMappingSymbols.find(Section);
477 if (LastMappingSymbol != LastMappingSymbols.end()) {
478 LastEMSInfo = std::move(LastMappingSymbol->second);
479 return;
481 LastEMSInfo.reset(new ElfMappingSymbolInfo(SMLoc(), nullptr, 0));
484 /// This function is the one used to emit instruction data into the ELF
485 /// streamer. We override it to add the appropriate mapping symbol if
486 /// necessary.
487 void EmitInstruction(const MCInst &Inst,
488 const MCSubtargetInfo &STI) override {
489 if (IsThumb)
490 EmitThumbMappingSymbol();
491 else
492 EmitARMMappingSymbol();
494 MCELFStreamer::EmitInstruction(Inst, STI);
497 void emitInst(uint32_t Inst, char Suffix) {
498 unsigned Size;
499 char Buffer[4];
500 const bool LittleEndian = getContext().getAsmInfo()->isLittleEndian();
502 switch (Suffix) {
503 case '\0':
504 Size = 4;
506 assert(!IsThumb);
507 EmitARMMappingSymbol();
508 for (unsigned II = 0, IE = Size; II != IE; II++) {
509 const unsigned I = LittleEndian ? (Size - II - 1) : II;
510 Buffer[Size - II - 1] = uint8_t(Inst >> I * CHAR_BIT);
513 break;
514 case 'n':
515 case 'w':
516 Size = (Suffix == 'n' ? 2 : 4);
518 assert(IsThumb);
519 EmitThumbMappingSymbol();
520 // Thumb wide instructions are emitted as a pair of 16-bit words of the
521 // appropriate endianness.
522 for (unsigned II = 0, IE = Size; II != IE; II = II + 2) {
523 const unsigned I0 = LittleEndian ? II + 0 : II + 1;
524 const unsigned I1 = LittleEndian ? II + 1 : II + 0;
525 Buffer[Size - II - 2] = uint8_t(Inst >> I0 * CHAR_BIT);
526 Buffer[Size - II - 1] = uint8_t(Inst >> I1 * CHAR_BIT);
529 break;
530 default:
531 llvm_unreachable("Invalid Suffix");
534 MCELFStreamer::EmitBytes(StringRef(Buffer, Size));
537 /// This is one of the functions used to emit data into an ELF section, so the
538 /// ARM streamer overrides it to add the appropriate mapping symbol ($d) if
539 /// necessary.
540 void EmitBytes(StringRef Data) override {
541 EmitDataMappingSymbol();
542 MCELFStreamer::EmitBytes(Data);
545 void FlushPendingMappingSymbol() {
546 if (!LastEMSInfo->hasInfo())
547 return;
548 ElfMappingSymbolInfo *EMS = LastEMSInfo.get();
549 EmitMappingSymbol("$d", EMS->Loc, EMS->F, EMS->Offset);
550 EMS->resetInfo();
553 /// This is one of the functions used to emit data into an ELF section, so the
554 /// ARM streamer overrides it to add the appropriate mapping symbol ($d) if
555 /// necessary.
556 void EmitValueImpl(const MCExpr *Value, unsigned Size, SMLoc Loc) override {
557 if (const MCSymbolRefExpr *SRE = dyn_cast_or_null<MCSymbolRefExpr>(Value)) {
558 if (SRE->getKind() == MCSymbolRefExpr::VK_ARM_SBREL && !(Size == 4)) {
559 getContext().reportError(Loc, "relocated expression must be 32-bit");
560 return;
562 getOrCreateDataFragment();
565 EmitDataMappingSymbol();
566 MCELFStreamer::EmitValueImpl(Value, Size, Loc);
569 void EmitAssemblerFlag(MCAssemblerFlag Flag) override {
570 MCELFStreamer::EmitAssemblerFlag(Flag);
572 switch (Flag) {
573 case MCAF_SyntaxUnified:
574 return; // no-op here.
575 case MCAF_Code16:
576 IsThumb = true;
577 return; // Change to Thumb mode
578 case MCAF_Code32:
579 IsThumb = false;
580 return; // Change to ARM mode
581 case MCAF_Code64:
582 return;
583 case MCAF_SubsectionsViaSymbols:
584 return;
588 private:
589 enum ElfMappingSymbol {
590 EMS_None,
591 EMS_ARM,
592 EMS_Thumb,
593 EMS_Data
596 struct ElfMappingSymbolInfo {
597 explicit ElfMappingSymbolInfo(SMLoc Loc, MCFragment *F, uint64_t O)
598 : Loc(Loc), F(F), Offset(O), State(EMS_None) {}
599 void resetInfo() {
600 F = nullptr;
601 Offset = 0;
603 bool hasInfo() { return F != nullptr; }
604 SMLoc Loc;
605 MCFragment *F;
606 uint64_t Offset;
607 ElfMappingSymbol State;
610 void EmitDataMappingSymbol() {
611 if (LastEMSInfo->State == EMS_Data)
612 return;
613 else if (LastEMSInfo->State == EMS_None) {
614 // This is a tentative symbol, it won't really be emitted until it's
615 // actually needed.
616 ElfMappingSymbolInfo *EMS = LastEMSInfo.get();
617 auto *DF = dyn_cast_or_null<MCDataFragment>(getCurrentFragment());
618 if (!DF)
619 return;
620 EMS->Loc = SMLoc();
621 EMS->F = getCurrentFragment();
622 EMS->Offset = DF->getContents().size();
623 LastEMSInfo->State = EMS_Data;
624 return;
626 EmitMappingSymbol("$d");
627 LastEMSInfo->State = EMS_Data;
630 void EmitThumbMappingSymbol() {
631 if (LastEMSInfo->State == EMS_Thumb)
632 return;
633 FlushPendingMappingSymbol();
634 EmitMappingSymbol("$t");
635 LastEMSInfo->State = EMS_Thumb;
638 void EmitARMMappingSymbol() {
639 if (LastEMSInfo->State == EMS_ARM)
640 return;
641 FlushPendingMappingSymbol();
642 EmitMappingSymbol("$a");
643 LastEMSInfo->State = EMS_ARM;
646 void EmitMappingSymbol(StringRef Name) {
647 auto *Symbol = cast<MCSymbolELF>(getContext().getOrCreateSymbol(
648 Name + "." + Twine(MappingSymbolCounter++)));
649 EmitLabel(Symbol);
651 Symbol->setType(ELF::STT_NOTYPE);
652 Symbol->setBinding(ELF::STB_LOCAL);
653 Symbol->setExternal(false);
656 void EmitMappingSymbol(StringRef Name, SMLoc Loc, MCFragment *F,
657 uint64_t Offset) {
658 auto *Symbol = cast<MCSymbolELF>(getContext().getOrCreateSymbol(
659 Name + "." + Twine(MappingSymbolCounter++)));
660 EmitLabel(Symbol, Loc, F);
661 Symbol->setType(ELF::STT_NOTYPE);
662 Symbol->setBinding(ELF::STB_LOCAL);
663 Symbol->setExternal(false);
664 Symbol->setOffset(Offset);
667 void EmitThumbFunc(MCSymbol *Func) override {
668 getAssembler().setIsThumbFunc(Func);
669 EmitSymbolAttribute(Func, MCSA_ELF_TypeFunction);
672 // Helper functions for ARM exception handling directives
673 void EHReset();
675 // Reset state between object emissions
676 void reset() override;
678 void EmitPersonalityFixup(StringRef Name);
679 void FlushPendingOffset();
680 void FlushUnwindOpcodes(bool NoHandlerData);
682 void SwitchToEHSection(StringRef Prefix, unsigned Type, unsigned Flags,
683 SectionKind Kind, const MCSymbol &Fn);
684 void SwitchToExTabSection(const MCSymbol &FnStart);
685 void SwitchToExIdxSection(const MCSymbol &FnStart);
687 void EmitFixup(const MCExpr *Expr, MCFixupKind Kind);
689 bool IsThumb;
690 int64_t MappingSymbolCounter = 0;
692 DenseMap<const MCSection *, std::unique_ptr<ElfMappingSymbolInfo>>
693 LastMappingSymbols;
695 std::unique_ptr<ElfMappingSymbolInfo> LastEMSInfo;
697 // ARM Exception Handling Frame Information
698 MCSymbol *ExTab;
699 MCSymbol *FnStart;
700 const MCSymbol *Personality;
701 unsigned PersonalityIndex;
702 unsigned FPReg; // Frame pointer register
703 int64_t FPOffset; // Offset: (final frame pointer) - (initial $sp)
704 int64_t SPOffset; // Offset: (final $sp) - (initial $sp)
705 int64_t PendingOffset; // Offset: (final $sp) - (emitted $sp)
706 bool UsedFP;
707 bool CantUnwind;
708 SmallVector<uint8_t, 64> Opcodes;
709 UnwindOpcodeAssembler UnwindOpAsm;
712 } // end anonymous namespace
714 ARMELFStreamer &ARMTargetELFStreamer::getStreamer() {
715 return static_cast<ARMELFStreamer &>(Streamer);
718 void ARMTargetELFStreamer::emitFnStart() { getStreamer().emitFnStart(); }
719 void ARMTargetELFStreamer::emitFnEnd() { getStreamer().emitFnEnd(); }
720 void ARMTargetELFStreamer::emitCantUnwind() { getStreamer().emitCantUnwind(); }
722 void ARMTargetELFStreamer::emitPersonality(const MCSymbol *Personality) {
723 getStreamer().emitPersonality(Personality);
726 void ARMTargetELFStreamer::emitPersonalityIndex(unsigned Index) {
727 getStreamer().emitPersonalityIndex(Index);
730 void ARMTargetELFStreamer::emitHandlerData() {
731 getStreamer().emitHandlerData();
734 void ARMTargetELFStreamer::emitSetFP(unsigned FpReg, unsigned SpReg,
735 int64_t Offset) {
736 getStreamer().emitSetFP(FpReg, SpReg, Offset);
739 void ARMTargetELFStreamer::emitMovSP(unsigned Reg, int64_t Offset) {
740 getStreamer().emitMovSP(Reg, Offset);
743 void ARMTargetELFStreamer::emitPad(int64_t Offset) {
744 getStreamer().emitPad(Offset);
747 void ARMTargetELFStreamer::emitRegSave(const SmallVectorImpl<unsigned> &RegList,
748 bool isVector) {
749 getStreamer().emitRegSave(RegList, isVector);
752 void ARMTargetELFStreamer::emitUnwindRaw(int64_t Offset,
753 const SmallVectorImpl<uint8_t> &Opcodes) {
754 getStreamer().emitUnwindRaw(Offset, Opcodes);
757 void ARMTargetELFStreamer::switchVendor(StringRef Vendor) {
758 assert(!Vendor.empty() && "Vendor cannot be empty.");
760 if (CurrentVendor == Vendor)
761 return;
763 if (!CurrentVendor.empty())
764 finishAttributeSection();
766 assert(Contents.empty() &&
767 ".ARM.attributes should be flushed before changing vendor");
768 CurrentVendor = Vendor;
772 void ARMTargetELFStreamer::emitAttribute(unsigned Attribute, unsigned Value) {
773 setAttributeItem(Attribute, Value, /* OverwriteExisting= */ true);
776 void ARMTargetELFStreamer::emitTextAttribute(unsigned Attribute,
777 StringRef Value) {
778 setAttributeItem(Attribute, Value, /* OverwriteExisting= */ true);
781 void ARMTargetELFStreamer::emitIntTextAttribute(unsigned Attribute,
782 unsigned IntValue,
783 StringRef StringValue) {
784 setAttributeItems(Attribute, IntValue, StringValue,
785 /* OverwriteExisting= */ true);
788 void ARMTargetELFStreamer::emitArch(ARM::ArchKind Value) {
789 Arch = Value;
792 void ARMTargetELFStreamer::emitObjectArch(ARM::ArchKind Value) {
793 EmittedArch = Value;
796 void ARMTargetELFStreamer::emitArchDefaultAttributes() {
797 using namespace ARMBuildAttrs;
799 setAttributeItem(CPU_name,
800 ARM::getCPUAttr(Arch),
801 false);
803 if (EmittedArch == ARM::ArchKind::INVALID)
804 setAttributeItem(CPU_arch,
805 ARM::getArchAttr(Arch),
806 false);
807 else
808 setAttributeItem(CPU_arch,
809 ARM::getArchAttr(EmittedArch),
810 false);
812 switch (Arch) {
813 case ARM::ArchKind::ARMV2:
814 case ARM::ArchKind::ARMV2A:
815 case ARM::ArchKind::ARMV3:
816 case ARM::ArchKind::ARMV3M:
817 case ARM::ArchKind::ARMV4:
818 setAttributeItem(ARM_ISA_use, Allowed, false);
819 break;
821 case ARM::ArchKind::ARMV4T:
822 case ARM::ArchKind::ARMV5T:
823 case ARM::ArchKind::ARMV5TE:
824 case ARM::ArchKind::ARMV6:
825 setAttributeItem(ARM_ISA_use, Allowed, false);
826 setAttributeItem(THUMB_ISA_use, Allowed, false);
827 break;
829 case ARM::ArchKind::ARMV6T2:
830 setAttributeItem(ARM_ISA_use, Allowed, false);
831 setAttributeItem(THUMB_ISA_use, AllowThumb32, false);
832 break;
834 case ARM::ArchKind::ARMV6K:
835 case ARM::ArchKind::ARMV6KZ:
836 setAttributeItem(ARM_ISA_use, Allowed, false);
837 setAttributeItem(THUMB_ISA_use, Allowed, false);
838 setAttributeItem(Virtualization_use, AllowTZ, false);
839 break;
841 case ARM::ArchKind::ARMV6M:
842 setAttributeItem(THUMB_ISA_use, Allowed, false);
843 break;
845 case ARM::ArchKind::ARMV7A:
846 setAttributeItem(CPU_arch_profile, ApplicationProfile, false);
847 setAttributeItem(ARM_ISA_use, Allowed, false);
848 setAttributeItem(THUMB_ISA_use, AllowThumb32, false);
849 break;
851 case ARM::ArchKind::ARMV7R:
852 setAttributeItem(CPU_arch_profile, RealTimeProfile, false);
853 setAttributeItem(ARM_ISA_use, Allowed, false);
854 setAttributeItem(THUMB_ISA_use, AllowThumb32, false);
855 break;
857 case ARM::ArchKind::ARMV7EM:
858 case ARM::ArchKind::ARMV7M:
859 setAttributeItem(CPU_arch_profile, MicroControllerProfile, false);
860 setAttributeItem(THUMB_ISA_use, AllowThumb32, false);
861 break;
863 case ARM::ArchKind::ARMV8A:
864 case ARM::ArchKind::ARMV8_1A:
865 case ARM::ArchKind::ARMV8_2A:
866 case ARM::ArchKind::ARMV8_3A:
867 case ARM::ArchKind::ARMV8_4A:
868 case ARM::ArchKind::ARMV8_5A:
869 setAttributeItem(CPU_arch_profile, ApplicationProfile, false);
870 setAttributeItem(ARM_ISA_use, Allowed, false);
871 setAttributeItem(THUMB_ISA_use, AllowThumb32, false);
872 setAttributeItem(MPextension_use, Allowed, false);
873 setAttributeItem(Virtualization_use, AllowTZVirtualization, false);
874 break;
876 case ARM::ArchKind::ARMV8MBaseline:
877 case ARM::ArchKind::ARMV8MMainline:
878 setAttributeItem(THUMB_ISA_use, AllowThumbDerived, false);
879 setAttributeItem(CPU_arch_profile, MicroControllerProfile, false);
880 break;
882 case ARM::ArchKind::IWMMXT:
883 setAttributeItem(ARM_ISA_use, Allowed, false);
884 setAttributeItem(THUMB_ISA_use, Allowed, false);
885 setAttributeItem(WMMX_arch, AllowWMMXv1, false);
886 break;
888 case ARM::ArchKind::IWMMXT2:
889 setAttributeItem(ARM_ISA_use, Allowed, false);
890 setAttributeItem(THUMB_ISA_use, Allowed, false);
891 setAttributeItem(WMMX_arch, AllowWMMXv2, false);
892 break;
894 default:
895 report_fatal_error("Unknown Arch: " + Twine(ARM::getArchName(Arch)));
896 break;
900 void ARMTargetELFStreamer::emitFPU(unsigned Value) {
901 FPU = Value;
904 void ARMTargetELFStreamer::emitFPUDefaultAttributes() {
905 switch (FPU) {
906 case ARM::FK_VFP:
907 case ARM::FK_VFPV2:
908 setAttributeItem(ARMBuildAttrs::FP_arch,
909 ARMBuildAttrs::AllowFPv2,
910 /* OverwriteExisting= */ false);
911 break;
913 case ARM::FK_VFPV3:
914 setAttributeItem(ARMBuildAttrs::FP_arch,
915 ARMBuildAttrs::AllowFPv3A,
916 /* OverwriteExisting= */ false);
917 break;
919 case ARM::FK_VFPV3_FP16:
920 setAttributeItem(ARMBuildAttrs::FP_arch,
921 ARMBuildAttrs::AllowFPv3A,
922 /* OverwriteExisting= */ false);
923 setAttributeItem(ARMBuildAttrs::FP_HP_extension,
924 ARMBuildAttrs::AllowHPFP,
925 /* OverwriteExisting= */ false);
926 break;
928 case ARM::FK_VFPV3_D16:
929 setAttributeItem(ARMBuildAttrs::FP_arch,
930 ARMBuildAttrs::AllowFPv3B,
931 /* OverwriteExisting= */ false);
932 break;
934 case ARM::FK_VFPV3_D16_FP16:
935 setAttributeItem(ARMBuildAttrs::FP_arch,
936 ARMBuildAttrs::AllowFPv3B,
937 /* OverwriteExisting= */ false);
938 setAttributeItem(ARMBuildAttrs::FP_HP_extension,
939 ARMBuildAttrs::AllowHPFP,
940 /* OverwriteExisting= */ false);
941 break;
943 case ARM::FK_VFPV3XD:
944 setAttributeItem(ARMBuildAttrs::FP_arch,
945 ARMBuildAttrs::AllowFPv3B,
946 /* OverwriteExisting= */ false);
947 break;
948 case ARM::FK_VFPV3XD_FP16:
949 setAttributeItem(ARMBuildAttrs::FP_arch,
950 ARMBuildAttrs::AllowFPv3B,
951 /* OverwriteExisting= */ false);
952 setAttributeItem(ARMBuildAttrs::FP_HP_extension,
953 ARMBuildAttrs::AllowHPFP,
954 /* OverwriteExisting= */ false);
955 break;
957 case ARM::FK_VFPV4:
958 setAttributeItem(ARMBuildAttrs::FP_arch,
959 ARMBuildAttrs::AllowFPv4A,
960 /* OverwriteExisting= */ false);
961 break;
963 // ABI_HardFP_use is handled in ARMAsmPrinter, so _SP_D16 is treated the same
964 // as _D16 here.
965 case ARM::FK_FPV4_SP_D16:
966 case ARM::FK_VFPV4_D16:
967 setAttributeItem(ARMBuildAttrs::FP_arch,
968 ARMBuildAttrs::AllowFPv4B,
969 /* OverwriteExisting= */ false);
970 break;
972 case ARM::FK_FP_ARMV8:
973 setAttributeItem(ARMBuildAttrs::FP_arch,
974 ARMBuildAttrs::AllowFPARMv8A,
975 /* OverwriteExisting= */ false);
976 break;
978 // FPV5_D16 is identical to FP_ARMV8 except for the number of D registers, so
979 // uses the FP_ARMV8_D16 build attribute.
980 case ARM::FK_FPV5_SP_D16:
981 case ARM::FK_FPV5_D16:
982 setAttributeItem(ARMBuildAttrs::FP_arch,
983 ARMBuildAttrs::AllowFPARMv8B,
984 /* OverwriteExisting= */ false);
985 break;
987 case ARM::FK_NEON:
988 setAttributeItem(ARMBuildAttrs::FP_arch,
989 ARMBuildAttrs::AllowFPv3A,
990 /* OverwriteExisting= */ false);
991 setAttributeItem(ARMBuildAttrs::Advanced_SIMD_arch,
992 ARMBuildAttrs::AllowNeon,
993 /* OverwriteExisting= */ false);
994 break;
996 case ARM::FK_NEON_FP16:
997 setAttributeItem(ARMBuildAttrs::FP_arch,
998 ARMBuildAttrs::AllowFPv3A,
999 /* OverwriteExisting= */ false);
1000 setAttributeItem(ARMBuildAttrs::Advanced_SIMD_arch,
1001 ARMBuildAttrs::AllowNeon,
1002 /* OverwriteExisting= */ false);
1003 setAttributeItem(ARMBuildAttrs::FP_HP_extension,
1004 ARMBuildAttrs::AllowHPFP,
1005 /* OverwriteExisting= */ false);
1006 break;
1008 case ARM::FK_NEON_VFPV4:
1009 setAttributeItem(ARMBuildAttrs::FP_arch,
1010 ARMBuildAttrs::AllowFPv4A,
1011 /* OverwriteExisting= */ false);
1012 setAttributeItem(ARMBuildAttrs::Advanced_SIMD_arch,
1013 ARMBuildAttrs::AllowNeon2,
1014 /* OverwriteExisting= */ false);
1015 break;
1017 case ARM::FK_NEON_FP_ARMV8:
1018 case ARM::FK_CRYPTO_NEON_FP_ARMV8:
1019 setAttributeItem(ARMBuildAttrs::FP_arch,
1020 ARMBuildAttrs::AllowFPARMv8A,
1021 /* OverwriteExisting= */ false);
1022 // 'Advanced_SIMD_arch' must be emitted not here, but within
1023 // ARMAsmPrinter::emitAttributes(), depending on hasV8Ops() and hasV8_1a()
1024 break;
1026 case ARM::FK_SOFTVFP:
1027 case ARM::FK_NONE:
1028 break;
1030 default:
1031 report_fatal_error("Unknown FPU: " + Twine(FPU));
1032 break;
1036 size_t ARMTargetELFStreamer::calculateContentSize() const {
1037 size_t Result = 0;
1038 for (size_t i = 0; i < Contents.size(); ++i) {
1039 AttributeItem item = Contents[i];
1040 switch (item.Type) {
1041 case AttributeItem::HiddenAttribute:
1042 break;
1043 case AttributeItem::NumericAttribute:
1044 Result += getULEB128Size(item.Tag);
1045 Result += getULEB128Size(item.IntValue);
1046 break;
1047 case AttributeItem::TextAttribute:
1048 Result += getULEB128Size(item.Tag);
1049 Result += item.StringValue.size() + 1; // string + '\0'
1050 break;
1051 case AttributeItem::NumericAndTextAttributes:
1052 Result += getULEB128Size(item.Tag);
1053 Result += getULEB128Size(item.IntValue);
1054 Result += item.StringValue.size() + 1; // string + '\0';
1055 break;
1058 return Result;
1061 void ARMTargetELFStreamer::finishAttributeSection() {
1062 // <format-version>
1063 // [ <section-length> "vendor-name"
1064 // [ <file-tag> <size> <attribute>*
1065 // | <section-tag> <size> <section-number>* 0 <attribute>*
1066 // | <symbol-tag> <size> <symbol-number>* 0 <attribute>*
1067 // ]+
1068 // ]*
1070 if (FPU != ARM::FK_INVALID)
1071 emitFPUDefaultAttributes();
1073 if (Arch != ARM::ArchKind::INVALID)
1074 emitArchDefaultAttributes();
1076 if (Contents.empty())
1077 return;
1079 llvm::sort(Contents, AttributeItem::LessTag);
1081 ARMELFStreamer &Streamer = getStreamer();
1083 // Switch to .ARM.attributes section
1084 if (AttributeSection) {
1085 Streamer.SwitchSection(AttributeSection);
1086 } else {
1087 AttributeSection = Streamer.getContext().getELFSection(
1088 ".ARM.attributes", ELF::SHT_ARM_ATTRIBUTES, 0);
1089 Streamer.SwitchSection(AttributeSection);
1091 // Format version
1092 Streamer.EmitIntValue(0x41, 1);
1095 // Vendor size + Vendor name + '\0'
1096 const size_t VendorHeaderSize = 4 + CurrentVendor.size() + 1;
1098 // Tag + Tag Size
1099 const size_t TagHeaderSize = 1 + 4;
1101 const size_t ContentsSize = calculateContentSize();
1103 Streamer.EmitIntValue(VendorHeaderSize + TagHeaderSize + ContentsSize, 4);
1104 Streamer.EmitBytes(CurrentVendor);
1105 Streamer.EmitIntValue(0, 1); // '\0'
1107 Streamer.EmitIntValue(ARMBuildAttrs::File, 1);
1108 Streamer.EmitIntValue(TagHeaderSize + ContentsSize, 4);
1110 // Size should have been accounted for already, now
1111 // emit each field as its type (ULEB or String)
1112 for (size_t i = 0; i < Contents.size(); ++i) {
1113 AttributeItem item = Contents[i];
1114 Streamer.EmitULEB128IntValue(item.Tag);
1115 switch (item.Type) {
1116 default: llvm_unreachable("Invalid attribute type");
1117 case AttributeItem::NumericAttribute:
1118 Streamer.EmitULEB128IntValue(item.IntValue);
1119 break;
1120 case AttributeItem::TextAttribute:
1121 Streamer.EmitBytes(item.StringValue);
1122 Streamer.EmitIntValue(0, 1); // '\0'
1123 break;
1124 case AttributeItem::NumericAndTextAttributes:
1125 Streamer.EmitULEB128IntValue(item.IntValue);
1126 Streamer.EmitBytes(item.StringValue);
1127 Streamer.EmitIntValue(0, 1); // '\0'
1128 break;
1132 Contents.clear();
1133 FPU = ARM::FK_INVALID;
1136 void ARMTargetELFStreamer::emitLabel(MCSymbol *Symbol) {
1137 ARMELFStreamer &Streamer = getStreamer();
1138 if (!Streamer.IsThumb)
1139 return;
1141 Streamer.getAssembler().registerSymbol(*Symbol);
1142 unsigned Type = cast<MCSymbolELF>(Symbol)->getType();
1143 if (Type == ELF::STT_FUNC || Type == ELF::STT_GNU_IFUNC)
1144 Streamer.EmitThumbFunc(Symbol);
1147 void
1148 ARMTargetELFStreamer::AnnotateTLSDescriptorSequence(const MCSymbolRefExpr *S) {
1149 getStreamer().EmitFixup(S, FK_Data_4);
1152 void ARMTargetELFStreamer::emitThumbSet(MCSymbol *Symbol, const MCExpr *Value) {
1153 if (const MCSymbolRefExpr *SRE = dyn_cast<MCSymbolRefExpr>(Value)) {
1154 const MCSymbol &Sym = SRE->getSymbol();
1155 if (!Sym.isDefined()) {
1156 getStreamer().EmitAssignment(Symbol, Value);
1157 return;
1161 getStreamer().EmitThumbFunc(Symbol);
1162 getStreamer().EmitAssignment(Symbol, Value);
1165 void ARMTargetELFStreamer::emitInst(uint32_t Inst, char Suffix) {
1166 getStreamer().emitInst(Inst, Suffix);
1169 void ARMTargetELFStreamer::reset() { AttributeSection = nullptr; }
1171 void ARMELFStreamer::FinishImpl() {
1172 MCTargetStreamer &TS = *getTargetStreamer();
1173 ARMTargetStreamer &ATS = static_cast<ARMTargetStreamer &>(TS);
1174 ATS.finishAttributeSection();
1176 MCELFStreamer::FinishImpl();
1179 void ARMELFStreamer::reset() {
1180 MCTargetStreamer &TS = *getTargetStreamer();
1181 ARMTargetStreamer &ATS = static_cast<ARMTargetStreamer &>(TS);
1182 ATS.reset();
1183 MappingSymbolCounter = 0;
1184 MCELFStreamer::reset();
1185 LastMappingSymbols.clear();
1186 LastEMSInfo.reset();
1187 // MCELFStreamer clear's the assembler's e_flags. However, for
1188 // arm we manually set the ABI version on streamer creation, so
1189 // do the same here
1190 getAssembler().setELFHeaderEFlags(ELF::EF_ARM_EABI_VER5);
1193 inline void ARMELFStreamer::SwitchToEHSection(StringRef Prefix,
1194 unsigned Type,
1195 unsigned Flags,
1196 SectionKind Kind,
1197 const MCSymbol &Fn) {
1198 const MCSectionELF &FnSection =
1199 static_cast<const MCSectionELF &>(Fn.getSection());
1201 // Create the name for new section
1202 StringRef FnSecName(FnSection.getSectionName());
1203 SmallString<128> EHSecName(Prefix);
1204 if (FnSecName != ".text") {
1205 EHSecName += FnSecName;
1208 // Get .ARM.extab or .ARM.exidx section
1209 const MCSymbolELF *Group = FnSection.getGroup();
1210 if (Group)
1211 Flags |= ELF::SHF_GROUP;
1212 MCSectionELF *EHSection = getContext().getELFSection(
1213 EHSecName, Type, Flags, 0, Group, FnSection.getUniqueID(),
1214 static_cast<const MCSymbolELF *>(&Fn));
1216 assert(EHSection && "Failed to get the required EH section");
1218 // Switch to .ARM.extab or .ARM.exidx section
1219 SwitchSection(EHSection);
1220 EmitCodeAlignment(4);
1223 inline void ARMELFStreamer::SwitchToExTabSection(const MCSymbol &FnStart) {
1224 SwitchToEHSection(".ARM.extab", ELF::SHT_PROGBITS, ELF::SHF_ALLOC,
1225 SectionKind::getData(), FnStart);
1228 inline void ARMELFStreamer::SwitchToExIdxSection(const MCSymbol &FnStart) {
1229 SwitchToEHSection(".ARM.exidx", ELF::SHT_ARM_EXIDX,
1230 ELF::SHF_ALLOC | ELF::SHF_LINK_ORDER,
1231 SectionKind::getData(), FnStart);
1234 void ARMELFStreamer::EmitFixup(const MCExpr *Expr, MCFixupKind Kind) {
1235 MCDataFragment *Frag = getOrCreateDataFragment();
1236 Frag->getFixups().push_back(MCFixup::create(Frag->getContents().size(), Expr,
1237 Kind));
1240 void ARMELFStreamer::EHReset() {
1241 ExTab = nullptr;
1242 FnStart = nullptr;
1243 Personality = nullptr;
1244 PersonalityIndex = ARM::EHABI::NUM_PERSONALITY_INDEX;
1245 FPReg = ARM::SP;
1246 FPOffset = 0;
1247 SPOffset = 0;
1248 PendingOffset = 0;
1249 UsedFP = false;
1250 CantUnwind = false;
1252 Opcodes.clear();
1253 UnwindOpAsm.Reset();
1256 void ARMELFStreamer::emitFnStart() {
1257 assert(FnStart == nullptr);
1258 FnStart = getContext().createTempSymbol();
1259 EmitLabel(FnStart);
1262 void ARMELFStreamer::emitFnEnd() {
1263 assert(FnStart && ".fnstart must precedes .fnend");
1265 // Emit unwind opcodes if there is no .handlerdata directive
1266 if (!ExTab && !CantUnwind)
1267 FlushUnwindOpcodes(true);
1269 // Emit the exception index table entry
1270 SwitchToExIdxSection(*FnStart);
1272 if (PersonalityIndex < ARM::EHABI::NUM_PERSONALITY_INDEX)
1273 EmitPersonalityFixup(GetAEABIUnwindPersonalityName(PersonalityIndex));
1275 const MCSymbolRefExpr *FnStartRef =
1276 MCSymbolRefExpr::create(FnStart,
1277 MCSymbolRefExpr::VK_ARM_PREL31,
1278 getContext());
1280 EmitValue(FnStartRef, 4);
1282 if (CantUnwind) {
1283 EmitIntValue(ARM::EHABI::EXIDX_CANTUNWIND, 4);
1284 } else if (ExTab) {
1285 // Emit a reference to the unwind opcodes in the ".ARM.extab" section.
1286 const MCSymbolRefExpr *ExTabEntryRef =
1287 MCSymbolRefExpr::create(ExTab,
1288 MCSymbolRefExpr::VK_ARM_PREL31,
1289 getContext());
1290 EmitValue(ExTabEntryRef, 4);
1291 } else {
1292 // For the __aeabi_unwind_cpp_pr0, we have to emit the unwind opcodes in
1293 // the second word of exception index table entry. The size of the unwind
1294 // opcodes should always be 4 bytes.
1295 assert(PersonalityIndex == ARM::EHABI::AEABI_UNWIND_CPP_PR0 &&
1296 "Compact model must use __aeabi_unwind_cpp_pr0 as personality");
1297 assert(Opcodes.size() == 4u &&
1298 "Unwind opcode size for __aeabi_unwind_cpp_pr0 must be equal to 4");
1299 uint64_t Intval = Opcodes[0] |
1300 Opcodes[1] << 8 |
1301 Opcodes[2] << 16 |
1302 Opcodes[3] << 24;
1303 EmitIntValue(Intval, Opcodes.size());
1306 // Switch to the section containing FnStart
1307 SwitchSection(&FnStart->getSection());
1309 // Clean exception handling frame information
1310 EHReset();
1313 void ARMELFStreamer::emitCantUnwind() { CantUnwind = true; }
1315 // Add the R_ARM_NONE fixup at the same position
1316 void ARMELFStreamer::EmitPersonalityFixup(StringRef Name) {
1317 const MCSymbol *PersonalitySym = getContext().getOrCreateSymbol(Name);
1319 const MCSymbolRefExpr *PersonalityRef = MCSymbolRefExpr::create(
1320 PersonalitySym, MCSymbolRefExpr::VK_ARM_NONE, getContext());
1322 visitUsedExpr(*PersonalityRef);
1323 MCDataFragment *DF = getOrCreateDataFragment();
1324 DF->getFixups().push_back(MCFixup::create(DF->getContents().size(),
1325 PersonalityRef,
1326 MCFixup::getKindForSize(4, false)));
1329 void ARMELFStreamer::FlushPendingOffset() {
1330 if (PendingOffset != 0) {
1331 UnwindOpAsm.EmitSPOffset(-PendingOffset);
1332 PendingOffset = 0;
1336 void ARMELFStreamer::FlushUnwindOpcodes(bool NoHandlerData) {
1337 // Emit the unwind opcode to restore $sp.
1338 if (UsedFP) {
1339 const MCRegisterInfo *MRI = getContext().getRegisterInfo();
1340 int64_t LastRegSaveSPOffset = SPOffset - PendingOffset;
1341 UnwindOpAsm.EmitSPOffset(LastRegSaveSPOffset - FPOffset);
1342 UnwindOpAsm.EmitSetSP(MRI->getEncodingValue(FPReg));
1343 } else {
1344 FlushPendingOffset();
1347 // Finalize the unwind opcode sequence
1348 UnwindOpAsm.Finalize(PersonalityIndex, Opcodes);
1350 // For compact model 0, we have to emit the unwind opcodes in the .ARM.exidx
1351 // section. Thus, we don't have to create an entry in the .ARM.extab
1352 // section.
1353 if (NoHandlerData && PersonalityIndex == ARM::EHABI::AEABI_UNWIND_CPP_PR0)
1354 return;
1356 // Switch to .ARM.extab section.
1357 SwitchToExTabSection(*FnStart);
1359 // Create .ARM.extab label for offset in .ARM.exidx
1360 assert(!ExTab);
1361 ExTab = getContext().createTempSymbol();
1362 EmitLabel(ExTab);
1364 // Emit personality
1365 if (Personality) {
1366 const MCSymbolRefExpr *PersonalityRef =
1367 MCSymbolRefExpr::create(Personality,
1368 MCSymbolRefExpr::VK_ARM_PREL31,
1369 getContext());
1371 EmitValue(PersonalityRef, 4);
1374 // Emit unwind opcodes
1375 assert((Opcodes.size() % 4) == 0 &&
1376 "Unwind opcode size for __aeabi_cpp_unwind_pr0 must be multiple of 4");
1377 for (unsigned I = 0; I != Opcodes.size(); I += 4) {
1378 uint64_t Intval = Opcodes[I] |
1379 Opcodes[I + 1] << 8 |
1380 Opcodes[I + 2] << 16 |
1381 Opcodes[I + 3] << 24;
1382 EmitIntValue(Intval, 4);
1385 // According to ARM EHABI section 9.2, if the __aeabi_unwind_cpp_pr1() or
1386 // __aeabi_unwind_cpp_pr2() is used, then the handler data must be emitted
1387 // after the unwind opcodes. The handler data consists of several 32-bit
1388 // words, and should be terminated by zero.
1390 // In case that the .handlerdata directive is not specified by the
1391 // programmer, we should emit zero to terminate the handler data.
1392 if (NoHandlerData && !Personality)
1393 EmitIntValue(0, 4);
1396 void ARMELFStreamer::emitHandlerData() { FlushUnwindOpcodes(false); }
1398 void ARMELFStreamer::emitPersonality(const MCSymbol *Per) {
1399 Personality = Per;
1400 UnwindOpAsm.setPersonality(Per);
1403 void ARMELFStreamer::emitPersonalityIndex(unsigned Index) {
1404 assert(Index < ARM::EHABI::NUM_PERSONALITY_INDEX && "invalid index");
1405 PersonalityIndex = Index;
1408 void ARMELFStreamer::emitSetFP(unsigned NewFPReg, unsigned NewSPReg,
1409 int64_t Offset) {
1410 assert((NewSPReg == ARM::SP || NewSPReg == FPReg) &&
1411 "the operand of .setfp directive should be either $sp or $fp");
1413 UsedFP = true;
1414 FPReg = NewFPReg;
1416 if (NewSPReg == ARM::SP)
1417 FPOffset = SPOffset + Offset;
1418 else
1419 FPOffset += Offset;
1422 void ARMELFStreamer::emitMovSP(unsigned Reg, int64_t Offset) {
1423 assert((Reg != ARM::SP && Reg != ARM::PC) &&
1424 "the operand of .movsp cannot be either sp or pc");
1425 assert(FPReg == ARM::SP && "current FP must be SP");
1427 FlushPendingOffset();
1429 FPReg = Reg;
1430 FPOffset = SPOffset + Offset;
1432 const MCRegisterInfo *MRI = getContext().getRegisterInfo();
1433 UnwindOpAsm.EmitSetSP(MRI->getEncodingValue(FPReg));
1436 void ARMELFStreamer::emitPad(int64_t Offset) {
1437 // Track the change of the $sp offset
1438 SPOffset -= Offset;
1440 // To squash multiple .pad directives, we should delay the unwind opcode
1441 // until the .save, .vsave, .handlerdata, or .fnend directives.
1442 PendingOffset -= Offset;
1445 void ARMELFStreamer::emitRegSave(const SmallVectorImpl<unsigned> &RegList,
1446 bool IsVector) {
1447 // Collect the registers in the register list
1448 unsigned Count = 0;
1449 uint32_t Mask = 0;
1450 const MCRegisterInfo *MRI = getContext().getRegisterInfo();
1451 for (size_t i = 0; i < RegList.size(); ++i) {
1452 unsigned Reg = MRI->getEncodingValue(RegList[i]);
1453 assert(Reg < (IsVector ? 32U : 16U) && "Register out of range");
1454 unsigned Bit = (1u << Reg);
1455 if ((Mask & Bit) == 0) {
1456 Mask |= Bit;
1457 ++Count;
1461 // Track the change the $sp offset: For the .save directive, the
1462 // corresponding push instruction will decrease the $sp by (4 * Count).
1463 // For the .vsave directive, the corresponding vpush instruction will
1464 // decrease $sp by (8 * Count).
1465 SPOffset -= Count * (IsVector ? 8 : 4);
1467 // Emit the opcode
1468 FlushPendingOffset();
1469 if (IsVector)
1470 UnwindOpAsm.EmitVFPRegSave(Mask);
1471 else
1472 UnwindOpAsm.EmitRegSave(Mask);
1475 void ARMELFStreamer::emitUnwindRaw(int64_t Offset,
1476 const SmallVectorImpl<uint8_t> &Opcodes) {
1477 FlushPendingOffset();
1478 SPOffset = SPOffset - Offset;
1479 UnwindOpAsm.EmitRaw(Opcodes);
1482 namespace llvm {
1484 MCTargetStreamer *createARMTargetAsmStreamer(MCStreamer &S,
1485 formatted_raw_ostream &OS,
1486 MCInstPrinter *InstPrint,
1487 bool isVerboseAsm) {
1488 return new ARMTargetAsmStreamer(S, OS, *InstPrint, isVerboseAsm);
1491 MCTargetStreamer *createARMNullTargetStreamer(MCStreamer &S) {
1492 return new ARMTargetStreamer(S);
1495 MCTargetStreamer *createARMObjectTargetStreamer(MCStreamer &S,
1496 const MCSubtargetInfo &STI) {
1497 const Triple &TT = STI.getTargetTriple();
1498 if (TT.isOSBinFormatELF())
1499 return new ARMTargetELFStreamer(S);
1500 return new ARMTargetStreamer(S);
1503 MCELFStreamer *createARMELFStreamer(MCContext &Context,
1504 std::unique_ptr<MCAsmBackend> TAB,
1505 std::unique_ptr<MCObjectWriter> OW,
1506 std::unique_ptr<MCCodeEmitter> Emitter,
1507 bool RelaxAll, bool IsThumb) {
1508 ARMELFStreamer *S = new ARMELFStreamer(Context, std::move(TAB), std::move(OW),
1509 std::move(Emitter), IsThumb);
1510 // FIXME: This should eventually end up somewhere else where more
1511 // intelligent flag decisions can be made. For now we are just maintaining
1512 // the status quo for ARM and setting EF_ARM_EABI_VER5 as the default.
1513 S->getAssembler().setELFHeaderEFlags(ELF::EF_ARM_EABI_VER5);
1515 if (RelaxAll)
1516 S->getAssembler().setRelaxAll(true);
1517 return S;
1520 } // end namespace llvm