LLVM  3.7.0
ARMELFStreamer.cpp
Go to the documentation of this file.
1 //===- lib/MC/ARMELFStreamer.cpp - ELF Object Output for ARM --------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file assembles .s files and emits ARM ELF .o object files. Different
11 // from generic ELF streamer in emitting mapping symbols ($a, $t and $d) to
12 // delimit regions of data and code.
13 //
14 //===----------------------------------------------------------------------===//
15 
16 #include "ARMRegisterInfo.h"
17 #include "ARMUnwindOpAsm.h"
18 #include "llvm/ADT/StringExtras.h"
19 #include "llvm/ADT/Twine.h"
20 #include "llvm/MC/MCAsmBackend.h"
21 #include "llvm/MC/MCAsmInfo.h"
22 #include "llvm/MC/MCAssembler.h"
23 #include "llvm/MC/MCCodeEmitter.h"
24 #include "llvm/MC/MCContext.h"
25 #include "llvm/MC/MCELFStreamer.h"
26 #include "llvm/MC/MCExpr.h"
27 #include "llvm/MC/MCInst.h"
28 #include "llvm/MC/MCInstPrinter.h"
31 #include "llvm/MC/MCRegisterInfo.h"
32 #include "llvm/MC/MCSection.h"
33 #include "llvm/MC/MCSectionELF.h"
34 #include "llvm/MC/MCStreamer.h"
35 #include "llvm/MC/MCSymbolELF.h"
36 #include "llvm/MC/MCValue.h"
38 #include "llvm/Support/ARMEHABI.h"
40 #include "llvm/Support/Debug.h"
41 #include "llvm/Support/ELF.h"
43 #include "llvm/Support/LEB128.h"
45 #include <algorithm>
46 
47 using namespace llvm;
48 
49 static std::string GetAEABIUnwindPersonalityName(unsigned Index) {
50  assert(Index < ARM::EHABI::NUM_PERSONALITY_INDEX &&
51  "Invalid personality index");
52  return (Twine("__aeabi_unwind_cpp_pr") + Twine(Index)).str();
53 }
54 
55 namespace {
56 
57 class ARMELFStreamer;
58 
59 class ARMTargetAsmStreamer : public ARMTargetStreamer {
61  MCInstPrinter &InstPrinter;
62  bool IsVerboseAsm;
63 
64  void emitFnStart() override;
65  void emitFnEnd() override;
66  void emitCantUnwind() override;
67  void emitPersonality(const MCSymbol *Personality) override;
68  void emitPersonalityIndex(unsigned Index) override;
69  void emitHandlerData() override;
70  void emitSetFP(unsigned FpReg, unsigned SpReg, int64_t Offset = 0) override;
71  void emitMovSP(unsigned Reg, int64_t Offset = 0) override;
72  void emitPad(int64_t Offset) override;
73  void emitRegSave(const SmallVectorImpl<unsigned> &RegList,
74  bool isVector) override;
75  void emitUnwindRaw(int64_t Offset,
76  const SmallVectorImpl<uint8_t> &Opcodes) override;
77 
78  void switchVendor(StringRef Vendor) override;
79  void emitAttribute(unsigned Attribute, unsigned Value) override;
80  void emitTextAttribute(unsigned Attribute, StringRef String) override;
81  void emitIntTextAttribute(unsigned Attribute, unsigned IntValue,
82  StringRef StrinValue) override;
83  void emitArch(unsigned Arch) override;
84  void emitArchExtension(unsigned ArchExt) override;
85  void emitObjectArch(unsigned Arch) override;
86  void emitFPU(unsigned FPU) override;
87  void emitInst(uint32_t Inst, char Suffix = '\0') override;
88  void finishAttributeSection() override;
89 
90  void AnnotateTLSDescriptorSequence(const MCSymbolRefExpr *SRE) override;
91  void emitThumbSet(MCSymbol *Symbol, const MCExpr *Value) override;
92 
93 public:
94  ARMTargetAsmStreamer(MCStreamer &S, formatted_raw_ostream &OS,
95  MCInstPrinter &InstPrinter, bool VerboseAsm);
96 };
97 
98 ARMTargetAsmStreamer::ARMTargetAsmStreamer(MCStreamer &S,
100  MCInstPrinter &InstPrinter,
101  bool VerboseAsm)
102  : ARMTargetStreamer(S), OS(OS), InstPrinter(InstPrinter),
103  IsVerboseAsm(VerboseAsm) {}
104 void ARMTargetAsmStreamer::emitFnStart() { OS << "\t.fnstart\n"; }
105 void ARMTargetAsmStreamer::emitFnEnd() { OS << "\t.fnend\n"; }
106 void ARMTargetAsmStreamer::emitCantUnwind() { OS << "\t.cantunwind\n"; }
107 void ARMTargetAsmStreamer::emitPersonality(const MCSymbol *Personality) {
108  OS << "\t.personality " << Personality->getName() << '\n';
109 }
110 void ARMTargetAsmStreamer::emitPersonalityIndex(unsigned Index) {
111  OS << "\t.personalityindex " << Index << '\n';
112 }
113 void ARMTargetAsmStreamer::emitHandlerData() { OS << "\t.handlerdata\n"; }
114 void ARMTargetAsmStreamer::emitSetFP(unsigned FpReg, unsigned SpReg,
115  int64_t Offset) {
116  OS << "\t.setfp\t";
117  InstPrinter.printRegName(OS, FpReg);
118  OS << ", ";
119  InstPrinter.printRegName(OS, SpReg);
120  if (Offset)
121  OS << ", #" << Offset;
122  OS << '\n';
123 }
124 void ARMTargetAsmStreamer::emitMovSP(unsigned Reg, int64_t Offset) {
125  assert((Reg != ARM::SP && Reg != ARM::PC) &&
126  "the operand of .movsp cannot be either sp or pc");
127 
128  OS << "\t.movsp\t";
129  InstPrinter.printRegName(OS, Reg);
130  if (Offset)
131  OS << ", #" << Offset;
132  OS << '\n';
133 }
134 void ARMTargetAsmStreamer::emitPad(int64_t Offset) {
135  OS << "\t.pad\t#" << Offset << '\n';
136 }
137 void ARMTargetAsmStreamer::emitRegSave(const SmallVectorImpl<unsigned> &RegList,
138  bool isVector) {
139  assert(RegList.size() && "RegList should not be empty");
140  if (isVector)
141  OS << "\t.vsave\t{";
142  else
143  OS << "\t.save\t{";
144 
145  InstPrinter.printRegName(OS, RegList[0]);
146 
147  for (unsigned i = 1, e = RegList.size(); i != e; ++i) {
148  OS << ", ";
149  InstPrinter.printRegName(OS, RegList[i]);
150  }
151 
152  OS << "}\n";
153 }
154 void ARMTargetAsmStreamer::switchVendor(StringRef Vendor) {
155 }
156 void ARMTargetAsmStreamer::emitAttribute(unsigned Attribute, unsigned Value) {
157  OS << "\t.eabi_attribute\t" << Attribute << ", " << Twine(Value);
158  if (IsVerboseAsm) {
160  if (!Name.empty())
161  OS << "\t@ " << Name;
162  }
163  OS << "\n";
164 }
165 void ARMTargetAsmStreamer::emitTextAttribute(unsigned Attribute,
166  StringRef String) {
167  switch (Attribute) {
169  OS << "\t.cpu\t" << String.lower();
170  break;
171  default:
172  OS << "\t.eabi_attribute\t" << Attribute << ", \"" << String << "\"";
173  if (IsVerboseAsm) {
175  if (!Name.empty())
176  OS << "\t@ " << Name;
177  }
178  break;
179  }
180  OS << "\n";
181 }
182 void ARMTargetAsmStreamer::emitIntTextAttribute(unsigned Attribute,
183  unsigned IntValue,
184  StringRef StringValue) {
185  switch (Attribute) {
186  default: llvm_unreachable("unsupported multi-value attribute in asm mode");
188  OS << "\t.eabi_attribute\t" << Attribute << ", " << IntValue;
189  if (!StringValue.empty())
190  OS << ", \"" << StringValue << "\"";
191  if (IsVerboseAsm)
192  OS << "\t@ " << ARMBuildAttrs::AttrTypeAsString(Attribute);
193  break;
194  }
195  OS << "\n";
196 }
197 void ARMTargetAsmStreamer::emitArch(unsigned Arch) {
198  OS << "\t.arch\t" << ARMTargetParser::getArchName(Arch) << "\n";
199 }
200 void ARMTargetAsmStreamer::emitArchExtension(unsigned ArchExt) {
201  OS << "\t.arch_extension\t" << ARMTargetParser::getArchExtName(ArchExt) << "\n";
202 }
203 void ARMTargetAsmStreamer::emitObjectArch(unsigned Arch) {
204  OS << "\t.object_arch\t" << ARMTargetParser::getArchName(Arch) << '\n';
205 }
206 void ARMTargetAsmStreamer::emitFPU(unsigned FPU) {
207  OS << "\t.fpu\t" << ARMTargetParser::getFPUName(FPU) << "\n";
208 }
209 void ARMTargetAsmStreamer::finishAttributeSection() {
210 }
211 void
212 ARMTargetAsmStreamer::AnnotateTLSDescriptorSequence(const MCSymbolRefExpr *S) {
213  OS << "\t.tlsdescseq\t" << S->getSymbol().getName();
214 }
215 
216 void ARMTargetAsmStreamer::emitThumbSet(MCSymbol *Symbol, const MCExpr *Value) {
217  const MCAsmInfo *MAI = Streamer.getContext().getAsmInfo();
218 
219  OS << "\t.thumb_set\t";
220  Symbol->print(OS, MAI);
221  OS << ", ";
222  Value->print(OS, MAI);
223  OS << '\n';
224 }
225 
226 void ARMTargetAsmStreamer::emitInst(uint32_t Inst, char Suffix) {
227  OS << "\t.inst";
228  if (Suffix)
229  OS << "." << Suffix;
230  OS << "\t0x" << Twine::utohexstr(Inst) << "\n";
231 }
232 
233 void ARMTargetAsmStreamer::emitUnwindRaw(int64_t Offset,
234  const SmallVectorImpl<uint8_t> &Opcodes) {
235  OS << "\t.unwind_raw " << Offset;
236  for (SmallVectorImpl<uint8_t>::const_iterator OCI = Opcodes.begin(),
237  OCE = Opcodes.end();
238  OCI != OCE; ++OCI)
239  OS << ", 0x" << Twine::utohexstr(*OCI);
240  OS << '\n';
241 }
242 
243 class ARMTargetELFStreamer : public ARMTargetStreamer {
244 private:
245  // This structure holds all attributes, accounting for
246  // their string/numeric value, so we can later emmit them
247  // in declaration order, keeping all in the same vector
248  struct AttributeItem {
249  enum {
250  HiddenAttribute = 0,
251  NumericAttribute,
252  TextAttribute,
253  NumericAndTextAttributes
254  } Type;
255  unsigned Tag;
256  unsigned IntValue;
257  StringRef StringValue;
258 
259  static bool LessTag(const AttributeItem &LHS, const AttributeItem &RHS) {
260  // The conformance tag must be emitted first when serialised
261  // into an object file. Specifically, the addenda to the ARM ABI
262  // states that (2.3.7.4):
263  //
264  // "To simplify recognition by consumers in the common case of
265  // claiming conformity for the whole file, this tag should be
266  // emitted first in a file-scope sub-subsection of the first
267  // public subsection of the attributes section."
268  //
269  // So it is special-cased in this comparison predicate when the
270  // attributes are sorted in finishAttributeSection().
271  return (RHS.Tag != ARMBuildAttrs::conformance) &&
272  ((LHS.Tag == ARMBuildAttrs::conformance) || (LHS.Tag < RHS.Tag));
273  }
274  };
275 
276  StringRef CurrentVendor;
277  unsigned FPU;
278  unsigned Arch;
279  unsigned EmittedArch;
281 
282  MCSection *AttributeSection;
283 
284  AttributeItem *getAttributeItem(unsigned Attribute) {
285  for (size_t i = 0; i < Contents.size(); ++i)
286  if (Contents[i].Tag == Attribute)
287  return &Contents[i];
288  return nullptr;
289  }
290 
291  void setAttributeItem(unsigned Attribute, unsigned Value,
292  bool OverwriteExisting) {
293  // Look for existing attribute item
294  if (AttributeItem *Item = getAttributeItem(Attribute)) {
295  if (!OverwriteExisting)
296  return;
297  Item->Type = AttributeItem::NumericAttribute;
298  Item->IntValue = Value;
299  return;
300  }
301 
302  // Create new attribute item
303  AttributeItem Item = {
304  AttributeItem::NumericAttribute,
305  Attribute,
306  Value,
307  StringRef("")
308  };
309  Contents.push_back(Item);
310  }
311 
312  void setAttributeItem(unsigned Attribute, StringRef Value,
313  bool OverwriteExisting) {
314  // Look for existing attribute item
315  if (AttributeItem *Item = getAttributeItem(Attribute)) {
316  if (!OverwriteExisting)
317  return;
318  Item->Type = AttributeItem::TextAttribute;
319  Item->StringValue = Value;
320  return;
321  }
322 
323  // Create new attribute item
324  AttributeItem Item = {
325  AttributeItem::TextAttribute,
326  Attribute,
327  0,
328  Value
329  };
330  Contents.push_back(Item);
331  }
332 
333  void setAttributeItems(unsigned Attribute, unsigned IntValue,
334  StringRef StringValue, bool OverwriteExisting) {
335  // Look for existing attribute item
336  if (AttributeItem *Item = getAttributeItem(Attribute)) {
337  if (!OverwriteExisting)
338  return;
339  Item->Type = AttributeItem::NumericAndTextAttributes;
340  Item->IntValue = IntValue;
341  Item->StringValue = StringValue;
342  return;
343  }
344 
345  // Create new attribute item
346  AttributeItem Item = {
347  AttributeItem::NumericAndTextAttributes,
348  Attribute,
349  IntValue,
350  StringValue
351  };
352  Contents.push_back(Item);
353  }
354 
355  void emitArchDefaultAttributes();
356  void emitFPUDefaultAttributes();
357 
358  ARMELFStreamer &getStreamer();
359 
360  void emitFnStart() override;
361  void emitFnEnd() override;
362  void emitCantUnwind() override;
363  void emitPersonality(const MCSymbol *Personality) override;
364  void emitPersonalityIndex(unsigned Index) override;
365  void emitHandlerData() override;
366  void emitSetFP(unsigned FpReg, unsigned SpReg, int64_t Offset = 0) override;
367  void emitMovSP(unsigned Reg, int64_t Offset = 0) override;
368  void emitPad(int64_t Offset) override;
369  void emitRegSave(const SmallVectorImpl<unsigned> &RegList,
370  bool isVector) override;
371  void emitUnwindRaw(int64_t Offset,
372  const SmallVectorImpl<uint8_t> &Opcodes) override;
373 
374  void switchVendor(StringRef Vendor) override;
375  void emitAttribute(unsigned Attribute, unsigned Value) override;
376  void emitTextAttribute(unsigned Attribute, StringRef String) override;
377  void emitIntTextAttribute(unsigned Attribute, unsigned IntValue,
378  StringRef StringValue) override;
379  void emitArch(unsigned Arch) override;
380  void emitObjectArch(unsigned Arch) override;
381  void emitFPU(unsigned FPU) override;
382  void emitInst(uint32_t Inst, char Suffix = '\0') override;
383  void finishAttributeSection() override;
384  void emitLabel(MCSymbol *Symbol) override;
385 
386  void AnnotateTLSDescriptorSequence(const MCSymbolRefExpr *SRE) override;
387  void emitThumbSet(MCSymbol *Symbol, const MCExpr *Value) override;
388 
389  size_t calculateContentSize() const;
390 
391 public:
392  ARMTargetELFStreamer(MCStreamer &S)
393  : ARMTargetStreamer(S), CurrentVendor("aeabi"), FPU(ARM::FK_INVALID),
394  Arch(ARM::AK_INVALID), EmittedArch(ARM::AK_INVALID),
395  AttributeSection(nullptr) {}
396 };
397 
398 /// Extend the generic ELFStreamer class so that it can emit mapping symbols at
399 /// the appropriate points in the object files. These symbols are defined in the
400 /// ARM ELF ABI: infocenter.arm.com/help/topic/com.arm.../IHI0044D_aaelf.pdf.
401 ///
402 /// In brief: $a, $t or $d should be emitted at the start of each contiguous
403 /// region of ARM code, Thumb code or data in a section. In practice, this
404 /// emission does not rely on explicit assembler directives but on inherent
405 /// properties of the directives doing the emission (e.g. ".byte" is data, "add
406 /// r0, r0, r0" an instruction).
407 ///
408 /// As a result this system is orthogonal to the DataRegion infrastructure used
409 /// by MachO. Beware!
410 class ARMELFStreamer : public MCELFStreamer {
411 public:
412  friend class ARMTargetELFStreamer;
413 
414  ARMELFStreamer(MCContext &Context, MCAsmBackend &TAB, raw_pwrite_stream &OS,
415  MCCodeEmitter *Emitter, bool IsThumb)
416  : MCELFStreamer(Context, TAB, OS, Emitter), IsThumb(IsThumb),
417  MappingSymbolCounter(0), LastEMS(EMS_None) {
418  Reset();
419  }
420 
421  ~ARMELFStreamer() {}
422 
423  void FinishImpl() override;
424 
425  // ARM exception handling directives
426  void emitFnStart();
427  void emitFnEnd();
428  void emitCantUnwind();
429  void emitPersonality(const MCSymbol *Per);
430  void emitPersonalityIndex(unsigned index);
431  void emitHandlerData();
432  void emitSetFP(unsigned NewFpReg, unsigned NewSpReg, int64_t Offset = 0);
433  void emitMovSP(unsigned Reg, int64_t Offset = 0);
434  void emitPad(int64_t Offset);
435  void emitRegSave(const SmallVectorImpl<unsigned> &RegList, bool isVector);
436  void emitUnwindRaw(int64_t Offset, const SmallVectorImpl<uint8_t> &Opcodes);
437 
438  void ChangeSection(MCSection *Section, const MCExpr *Subsection) override {
439  // We have to keep track of the mapping symbol state of any sections we
440  // use. Each one should start off as EMS_None, which is provided as the
441  // default constructor by DenseMap::lookup.
442  LastMappingSymbols[getPreviousSection().first] = LastEMS;
443  LastEMS = LastMappingSymbols.lookup(Section);
444 
445  MCELFStreamer::ChangeSection(Section, Subsection);
446  }
447 
448  /// This function is the one used to emit instruction data into the ELF
449  /// streamer. We override it to add the appropriate mapping symbol if
450  /// necessary.
451  void EmitInstruction(const MCInst& Inst,
452  const MCSubtargetInfo &STI) override {
453  if (IsThumb)
454  EmitThumbMappingSymbol();
455  else
456  EmitARMMappingSymbol();
457 
459  }
460 
461  void emitInst(uint32_t Inst, char Suffix) {
462  unsigned Size;
463  char Buffer[4];
464  const bool LittleEndian = getContext().getAsmInfo()->isLittleEndian();
465 
466  switch (Suffix) {
467  case '\0':
468  Size = 4;
469 
470  assert(!IsThumb);
471  EmitARMMappingSymbol();
472  for (unsigned II = 0, IE = Size; II != IE; II++) {
473  const unsigned I = LittleEndian ? (Size - II - 1) : II;
474  Buffer[Size - II - 1] = uint8_t(Inst >> I * CHAR_BIT);
475  }
476 
477  break;
478  case 'n':
479  case 'w':
480  Size = (Suffix == 'n' ? 2 : 4);
481 
482  assert(IsThumb);
483  EmitThumbMappingSymbol();
484  for (unsigned II = 0, IE = Size; II != IE; II = II + 2) {
485  const unsigned I0 = LittleEndian ? II + 0 : (Size - II - 1);
486  const unsigned I1 = LittleEndian ? II + 1 : (Size - II - 2);
487  Buffer[Size - II - 2] = uint8_t(Inst >> I0 * CHAR_BIT);
488  Buffer[Size - II - 1] = uint8_t(Inst >> I1 * CHAR_BIT);
489  }
490 
491  break;
492  default:
493  llvm_unreachable("Invalid Suffix");
494  }
495 
496  MCELFStreamer::EmitBytes(StringRef(Buffer, Size));
497  }
498 
499  /// This is one of the functions used to emit data into an ELF section, so the
500  /// ARM streamer overrides it to add the appropriate mapping symbol ($d) if
501  /// necessary.
502  void EmitBytes(StringRef Data) override {
503  EmitDataMappingSymbol();
505  }
506 
507  /// This is one of the functions used to emit data into an ELF section, so the
508  /// ARM streamer overrides it to add the appropriate mapping symbol ($d) if
509  /// necessary.
510  void EmitValueImpl(const MCExpr *Value, unsigned Size,
511  const SMLoc &Loc) override {
512  if (const MCSymbolRefExpr *SRE = dyn_cast_or_null<MCSymbolRefExpr>(Value))
513  if (SRE->getKind() == MCSymbolRefExpr::VK_ARM_SBREL && !(Size == 4))
514  getContext().reportFatalError(Loc, "relocated expression must be 32-bit");
515 
516  EmitDataMappingSymbol();
517  MCELFStreamer::EmitValueImpl(Value, Size);
518  }
519 
520  void EmitAssemblerFlag(MCAssemblerFlag Flag) override {
522 
523  switch (Flag) {
524  case MCAF_SyntaxUnified:
525  return; // no-op here.
526  case MCAF_Code16:
527  IsThumb = true;
528  return; // Change to Thumb mode
529  case MCAF_Code32:
530  IsThumb = false;
531  return; // Change to ARM mode
532  case MCAF_Code64:
533  return;
535  return;
536  }
537  }
538 
539 private:
540  enum ElfMappingSymbol {
541  EMS_None,
542  EMS_ARM,
543  EMS_Thumb,
544  EMS_Data
545  };
546 
547  void EmitDataMappingSymbol() {
548  if (LastEMS == EMS_Data) return;
549  EmitMappingSymbol("$d");
550  LastEMS = EMS_Data;
551  }
552 
553  void EmitThumbMappingSymbol() {
554  if (LastEMS == EMS_Thumb) return;
555  EmitMappingSymbol("$t");
556  LastEMS = EMS_Thumb;
557  }
558 
559  void EmitARMMappingSymbol() {
560  if (LastEMS == EMS_ARM) return;
561  EmitMappingSymbol("$a");
562  LastEMS = EMS_ARM;
563  }
564 
565  void EmitMappingSymbol(StringRef Name) {
566  auto *Symbol = cast<MCSymbolELF>(getContext().getOrCreateSymbol(
567  Name + "." + Twine(MappingSymbolCounter++)));
568  EmitLabel(Symbol);
569 
570  Symbol->setType(ELF::STT_NOTYPE);
571  Symbol->setBinding(ELF::STB_LOCAL);
572  Symbol->setExternal(false);
573  }
574 
575  void EmitThumbFunc(MCSymbol *Func) override {
578  }
579 
580  // Helper functions for ARM exception handling directives
581  void Reset();
582 
583  void EmitPersonalityFixup(StringRef Name);
584  void FlushPendingOffset();
585  void FlushUnwindOpcodes(bool NoHandlerData);
586 
587  void SwitchToEHSection(const char *Prefix, unsigned Type, unsigned Flags,
588  SectionKind Kind, const MCSymbol &Fn);
589  void SwitchToExTabSection(const MCSymbol &FnStart);
590  void SwitchToExIdxSection(const MCSymbol &FnStart);
591 
592  void EmitFixup(const MCExpr *Expr, MCFixupKind Kind);
593 
594  bool IsThumb;
595  int64_t MappingSymbolCounter;
596 
598  ElfMappingSymbol LastEMS;
599 
600  // ARM Exception Handling Frame Information
601  MCSymbol *ExTab;
602  MCSymbol *FnStart;
603  const MCSymbol *Personality;
604  unsigned PersonalityIndex;
605  unsigned FPReg; // Frame pointer register
606  int64_t FPOffset; // Offset: (final frame pointer) - (initial $sp)
607  int64_t SPOffset; // Offset: (final $sp) - (initial $sp)
608  int64_t PendingOffset; // Offset: (final $sp) - (emitted $sp)
609  bool UsedFP;
610  bool CantUnwind;
611  SmallVector<uint8_t, 64> Opcodes;
612  UnwindOpcodeAssembler UnwindOpAsm;
613 };
614 } // end anonymous namespace
615 
616 ARMELFStreamer &ARMTargetELFStreamer::getStreamer() {
617  return static_cast<ARMELFStreamer &>(Streamer);
618 }
619 
620 void ARMTargetELFStreamer::emitFnStart() { getStreamer().emitFnStart(); }
621 void ARMTargetELFStreamer::emitFnEnd() { getStreamer().emitFnEnd(); }
622 void ARMTargetELFStreamer::emitCantUnwind() { getStreamer().emitCantUnwind(); }
623 void ARMTargetELFStreamer::emitPersonality(const MCSymbol *Personality) {
624  getStreamer().emitPersonality(Personality);
625 }
626 void ARMTargetELFStreamer::emitPersonalityIndex(unsigned Index) {
627  getStreamer().emitPersonalityIndex(Index);
628 }
629 void ARMTargetELFStreamer::emitHandlerData() {
630  getStreamer().emitHandlerData();
631 }
632 void ARMTargetELFStreamer::emitSetFP(unsigned FpReg, unsigned SpReg,
633  int64_t Offset) {
634  getStreamer().emitSetFP(FpReg, SpReg, Offset);
635 }
636 void ARMTargetELFStreamer::emitMovSP(unsigned Reg, int64_t Offset) {
637  getStreamer().emitMovSP(Reg, Offset);
638 }
639 void ARMTargetELFStreamer::emitPad(int64_t Offset) {
640  getStreamer().emitPad(Offset);
641 }
642 void ARMTargetELFStreamer::emitRegSave(const SmallVectorImpl<unsigned> &RegList,
643  bool isVector) {
644  getStreamer().emitRegSave(RegList, isVector);
645 }
646 void ARMTargetELFStreamer::emitUnwindRaw(int64_t Offset,
647  const SmallVectorImpl<uint8_t> &Opcodes) {
648  getStreamer().emitUnwindRaw(Offset, Opcodes);
649 }
650 void ARMTargetELFStreamer::switchVendor(StringRef Vendor) {
651  assert(!Vendor.empty() && "Vendor cannot be empty.");
652 
653  if (CurrentVendor == Vendor)
654  return;
655 
656  if (!CurrentVendor.empty())
657  finishAttributeSection();
658 
659  assert(Contents.empty() &&
660  ".ARM.attributes should be flushed before changing vendor");
661  CurrentVendor = Vendor;
662 
663 }
664 void ARMTargetELFStreamer::emitAttribute(unsigned Attribute, unsigned Value) {
665  setAttributeItem(Attribute, Value, /* OverwriteExisting= */ true);
666 }
667 void ARMTargetELFStreamer::emitTextAttribute(unsigned Attribute,
668  StringRef Value) {
669  setAttributeItem(Attribute, Value, /* OverwriteExisting= */ true);
670 }
671 void ARMTargetELFStreamer::emitIntTextAttribute(unsigned Attribute,
672  unsigned IntValue,
673  StringRef StringValue) {
674  setAttributeItems(Attribute, IntValue, StringValue,
675  /* OverwriteExisting= */ true);
676 }
677 void ARMTargetELFStreamer::emitArch(unsigned Value) {
678  Arch = Value;
679 }
680 void ARMTargetELFStreamer::emitObjectArch(unsigned Value) {
681  EmittedArch = Value;
682 }
683 void ARMTargetELFStreamer::emitArchDefaultAttributes() {
684  using namespace ARMBuildAttrs;
685 
686  setAttributeItem(CPU_name,
687  ARMTargetParser::getCPUAttr(Arch),
688  false);
689 
690  if (EmittedArch == ARM::AK_INVALID)
691  setAttributeItem(CPU_arch,
692  ARMTargetParser::getArchAttr(Arch),
693  false);
694  else
695  setAttributeItem(CPU_arch,
696  ARMTargetParser::getArchAttr(EmittedArch),
697  false);
698 
699  switch (Arch) {
700  case ARM::AK_ARMV2:
701  case ARM::AK_ARMV2A:
702  case ARM::AK_ARMV3:
703  case ARM::AK_ARMV3M:
704  case ARM::AK_ARMV4:
705  case ARM::AK_ARMV5:
706  setAttributeItem(ARM_ISA_use, Allowed, false);
707  break;
708 
709  case ARM::AK_ARMV4T:
710  case ARM::AK_ARMV5T:
711  case ARM::AK_ARMV5TE:
712  case ARM::AK_ARMV6:
713  case ARM::AK_ARMV6J:
714  setAttributeItem(ARM_ISA_use, Allowed, false);
715  setAttributeItem(THUMB_ISA_use, Allowed, false);
716  break;
717 
718  case ARM::AK_ARMV6T2:
719  setAttributeItem(ARM_ISA_use, Allowed, false);
720  setAttributeItem(THUMB_ISA_use, AllowThumb32, false);
721  break;
722 
723  case ARM::AK_ARMV6K:
724  case ARM::AK_ARMV6Z:
725  case ARM::AK_ARMV6ZK:
726  setAttributeItem(ARM_ISA_use, Allowed, false);
727  setAttributeItem(THUMB_ISA_use, Allowed, false);
728  setAttributeItem(Virtualization_use, AllowTZ, false);
729  break;
730 
731  case ARM::AK_ARMV6M:
732  setAttributeItem(THUMB_ISA_use, Allowed, false);
733  break;
734 
735  case ARM::AK_ARMV7:
736  setAttributeItem(THUMB_ISA_use, AllowThumb32, false);
737  break;
738 
739  case ARM::AK_ARMV7A:
740  setAttributeItem(CPU_arch_profile, ApplicationProfile, false);
741  setAttributeItem(ARM_ISA_use, Allowed, false);
742  setAttributeItem(THUMB_ISA_use, AllowThumb32, false);
743  break;
744 
745  case ARM::AK_ARMV7R:
746  setAttributeItem(CPU_arch_profile, RealTimeProfile, false);
747  setAttributeItem(ARM_ISA_use, Allowed, false);
748  setAttributeItem(THUMB_ISA_use, AllowThumb32, false);
749  break;
750 
751  case ARM::AK_ARMV7M:
752  setAttributeItem(CPU_arch_profile, MicroControllerProfile, false);
753  setAttributeItem(THUMB_ISA_use, AllowThumb32, false);
754  break;
755 
756  case ARM::AK_ARMV8A:
757  case ARM::AK_ARMV8_1A:
758  setAttributeItem(CPU_arch_profile, ApplicationProfile, false);
759  setAttributeItem(ARM_ISA_use, Allowed, false);
760  setAttributeItem(THUMB_ISA_use, AllowThumb32, false);
761  setAttributeItem(MPextension_use, Allowed, false);
762  setAttributeItem(Virtualization_use, AllowTZVirtualization, false);
763  break;
764 
765  case ARM::AK_IWMMXT:
766  setAttributeItem(ARM_ISA_use, Allowed, false);
767  setAttributeItem(THUMB_ISA_use, Allowed, false);
768  setAttributeItem(WMMX_arch, AllowWMMXv1, false);
769  break;
770 
771  case ARM::AK_IWMMXT2:
772  setAttributeItem(ARM_ISA_use, Allowed, false);
773  setAttributeItem(THUMB_ISA_use, Allowed, false);
774  setAttributeItem(WMMX_arch, AllowWMMXv2, false);
775  break;
776 
777  default:
778  report_fatal_error("Unknown Arch: " + Twine(Arch));
779  break;
780  }
781 }
782 void ARMTargetELFStreamer::emitFPU(unsigned Value) {
783  FPU = Value;
784 }
785 void ARMTargetELFStreamer::emitFPUDefaultAttributes() {
786  switch (FPU) {
787  case ARM::FK_VFP:
788  case ARM::FK_VFPV2:
789  setAttributeItem(ARMBuildAttrs::FP_arch,
791  /* OverwriteExisting= */ false);
792  break;
793 
794  case ARM::FK_VFPV3:
795  setAttributeItem(ARMBuildAttrs::FP_arch,
797  /* OverwriteExisting= */ false);
798  break;
799 
800  case ARM::FK_VFPV3_FP16:
801  setAttributeItem(ARMBuildAttrs::FP_arch,
803  /* OverwriteExisting= */ false);
804  setAttributeItem(ARMBuildAttrs::FP_HP_extension,
806  /* OverwriteExisting= */ false);
807  break;
808 
809  case ARM::FK_VFPV3_D16:
810  setAttributeItem(ARMBuildAttrs::FP_arch,
812  /* OverwriteExisting= */ false);
813  break;
814 
816  setAttributeItem(ARMBuildAttrs::FP_arch,
818  /* OverwriteExisting= */ false);
819  setAttributeItem(ARMBuildAttrs::FP_HP_extension,
821  /* OverwriteExisting= */ false);
822  break;
823 
824  case ARM::FK_VFPV3XD:
825  setAttributeItem(ARMBuildAttrs::FP_arch,
827  /* OverwriteExisting= */ false);
828  break;
830  setAttributeItem(ARMBuildAttrs::FP_arch,
832  /* OverwriteExisting= */ false);
833  setAttributeItem(ARMBuildAttrs::FP_HP_extension,
835  /* OverwriteExisting= */ false);
836  break;
837 
838  case ARM::FK_VFPV4:
839  setAttributeItem(ARMBuildAttrs::FP_arch,
841  /* OverwriteExisting= */ false);
842  break;
843 
844  // ABI_HardFP_use is handled in ARMAsmPrinter, so _SP_D16 is treated the same
845  // as _D16 here.
846  case ARM::FK_FPV4_SP_D16:
847  case ARM::FK_VFPV4_D16:
848  setAttributeItem(ARMBuildAttrs::FP_arch,
850  /* OverwriteExisting= */ false);
851  break;
852 
853  case ARM::FK_FP_ARMV8:
854  setAttributeItem(ARMBuildAttrs::FP_arch,
856  /* OverwriteExisting= */ false);
857  break;
858 
859  // FPV5_D16 is identical to FP_ARMV8 except for the number of D registers, so
860  // uses the FP_ARMV8_D16 build attribute.
861  case ARM::FK_FPV5_SP_D16:
862  case ARM::FK_FPV5_D16:
863  setAttributeItem(ARMBuildAttrs::FP_arch,
865  /* OverwriteExisting= */ false);
866  break;
867 
868  case ARM::FK_NEON:
869  setAttributeItem(ARMBuildAttrs::FP_arch,
871  /* OverwriteExisting= */ false);
872  setAttributeItem(ARMBuildAttrs::Advanced_SIMD_arch,
874  /* OverwriteExisting= */ false);
875  break;
876 
877  case ARM::FK_NEON_FP16:
878  setAttributeItem(ARMBuildAttrs::FP_arch,
880  /* OverwriteExisting= */ false);
881  setAttributeItem(ARMBuildAttrs::Advanced_SIMD_arch,
883  /* OverwriteExisting= */ false);
884  setAttributeItem(ARMBuildAttrs::FP_HP_extension,
886  /* OverwriteExisting= */ false);
887  break;
888 
889  case ARM::FK_NEON_VFPV4:
890  setAttributeItem(ARMBuildAttrs::FP_arch,
892  /* OverwriteExisting= */ false);
893  setAttributeItem(ARMBuildAttrs::Advanced_SIMD_arch,
895  /* OverwriteExisting= */ false);
896  break;
897 
900  setAttributeItem(ARMBuildAttrs::FP_arch,
902  /* OverwriteExisting= */ false);
903  // 'Advanced_SIMD_arch' must be emitted not here, but within
904  // ARMAsmPrinter::emitAttributes(), depending on hasV8Ops() and hasV8_1a()
905  break;
906 
907  case ARM::FK_SOFTVFP:
908  case ARM::FK_NONE:
909  break;
910 
911  default:
912  report_fatal_error("Unknown FPU: " + Twine(FPU));
913  break;
914  }
915 }
916 size_t ARMTargetELFStreamer::calculateContentSize() const {
917  size_t Result = 0;
918  for (size_t i = 0; i < Contents.size(); ++i) {
919  AttributeItem item = Contents[i];
920  switch (item.Type) {
921  case AttributeItem::HiddenAttribute:
922  break;
923  case AttributeItem::NumericAttribute:
924  Result += getULEB128Size(item.Tag);
925  Result += getULEB128Size(item.IntValue);
926  break;
927  case AttributeItem::TextAttribute:
928  Result += getULEB128Size(item.Tag);
929  Result += item.StringValue.size() + 1; // string + '\0'
930  break;
931  case AttributeItem::NumericAndTextAttributes:
932  Result += getULEB128Size(item.Tag);
933  Result += getULEB128Size(item.IntValue);
934  Result += item.StringValue.size() + 1; // string + '\0';
935  break;
936  }
937  }
938  return Result;
939 }
940 void ARMTargetELFStreamer::finishAttributeSection() {
941  // <format-version>
942  // [ <section-length> "vendor-name"
943  // [ <file-tag> <size> <attribute>*
944  // | <section-tag> <size> <section-number>* 0 <attribute>*
945  // | <symbol-tag> <size> <symbol-number>* 0 <attribute>*
946  // ]+
947  // ]*
948 
949  if (FPU != ARM::FK_INVALID)
950  emitFPUDefaultAttributes();
951 
952  if (Arch != ARM::AK_INVALID)
953  emitArchDefaultAttributes();
954 
955  if (Contents.empty())
956  return;
957 
958  std::sort(Contents.begin(), Contents.end(), AttributeItem::LessTag);
959 
960  ARMELFStreamer &Streamer = getStreamer();
961 
962  // Switch to .ARM.attributes section
963  if (AttributeSection) {
964  Streamer.SwitchSection(AttributeSection);
965  } else {
966  AttributeSection = Streamer.getContext().getELFSection(
967  ".ARM.attributes", ELF::SHT_ARM_ATTRIBUTES, 0);
968  Streamer.SwitchSection(AttributeSection);
969 
970  // Format version
971  Streamer.EmitIntValue(0x41, 1);
972  }
973 
974  // Vendor size + Vendor name + '\0'
975  const size_t VendorHeaderSize = 4 + CurrentVendor.size() + 1;
976 
977  // Tag + Tag Size
978  const size_t TagHeaderSize = 1 + 4;
979 
980  const size_t ContentsSize = calculateContentSize();
981 
982  Streamer.EmitIntValue(VendorHeaderSize + TagHeaderSize + ContentsSize, 4);
983  Streamer.EmitBytes(CurrentVendor);
984  Streamer.EmitIntValue(0, 1); // '\0'
985 
986  Streamer.EmitIntValue(ARMBuildAttrs::File, 1);
987  Streamer.EmitIntValue(TagHeaderSize + ContentsSize, 4);
988 
989  // Size should have been accounted for already, now
990  // emit each field as its type (ULEB or String)
991  for (size_t i = 0; i < Contents.size(); ++i) {
992  AttributeItem item = Contents[i];
993  Streamer.EmitULEB128IntValue(item.Tag);
994  switch (item.Type) {
995  default: llvm_unreachable("Invalid attribute type");
996  case AttributeItem::NumericAttribute:
997  Streamer.EmitULEB128IntValue(item.IntValue);
998  break;
999  case AttributeItem::TextAttribute:
1000  Streamer.EmitBytes(item.StringValue);
1001  Streamer.EmitIntValue(0, 1); // '\0'
1002  break;
1003  case AttributeItem::NumericAndTextAttributes:
1004  Streamer.EmitULEB128IntValue(item.IntValue);
1005  Streamer.EmitBytes(item.StringValue);
1006  Streamer.EmitIntValue(0, 1); // '\0'
1007  break;
1008  }
1009  }
1010 
1011  Contents.clear();
1012  FPU = ARM::FK_INVALID;
1013 }
1014 
1015 void ARMTargetELFStreamer::emitLabel(MCSymbol *Symbol) {
1016  ARMELFStreamer &Streamer = getStreamer();
1017  if (!Streamer.IsThumb)
1018  return;
1019 
1020  Streamer.getAssembler().registerSymbol(*Symbol);
1021  unsigned Type = cast<MCSymbolELF>(Symbol)->getType();
1022  if (Type == ELF::STT_FUNC || Type == ELF::STT_GNU_IFUNC)
1023  Streamer.EmitThumbFunc(Symbol);
1024 }
1025 
1026 void
1027 ARMTargetELFStreamer::AnnotateTLSDescriptorSequence(const MCSymbolRefExpr *S) {
1028  getStreamer().EmitFixup(S, FK_Data_4);
1029 }
1030 
1031 void ARMTargetELFStreamer::emitThumbSet(MCSymbol *Symbol, const MCExpr *Value) {
1032  if (const MCSymbolRefExpr *SRE = dyn_cast<MCSymbolRefExpr>(Value)) {
1033  const MCSymbol &Sym = SRE->getSymbol();
1034  if (!Sym.isDefined()) {
1035  getStreamer().EmitAssignment(Symbol, Value);
1036  return;
1037  }
1038  }
1039 
1040  getStreamer().EmitThumbFunc(Symbol);
1041  getStreamer().EmitAssignment(Symbol, Value);
1042 }
1043 
1044 void ARMTargetELFStreamer::emitInst(uint32_t Inst, char Suffix) {
1045  getStreamer().emitInst(Inst, Suffix);
1046 }
1047 
1048 void ARMELFStreamer::FinishImpl() {
1049  MCTargetStreamer &TS = *getTargetStreamer();
1050  ARMTargetStreamer &ATS = static_cast<ARMTargetStreamer &>(TS);
1051  ATS.finishAttributeSection();
1052 
1053  MCELFStreamer::FinishImpl();
1054 }
1055 
1056 inline void ARMELFStreamer::SwitchToEHSection(const char *Prefix,
1057  unsigned Type,
1058  unsigned Flags,
1059  SectionKind Kind,
1060  const MCSymbol &Fn) {
1061  const MCSectionELF &FnSection =
1062  static_cast<const MCSectionELF &>(Fn.getSection());
1063 
1064  // Create the name for new section
1065  StringRef FnSecName(FnSection.getSectionName());
1066  SmallString<128> EHSecName(Prefix);
1067  if (FnSecName != ".text") {
1068  EHSecName += FnSecName;
1069  }
1070 
1071  // Get .ARM.extab or .ARM.exidx section
1072  const MCSymbolELF *Group = FnSection.getGroup();
1073  if (Group)
1074  Flags |= ELF::SHF_GROUP;
1075  MCSectionELF *EHSection =
1076  getContext().getELFSection(EHSecName, Type, Flags, 0, Group,
1077  FnSection.getUniqueID(), nullptr, &FnSection);
1078 
1079  assert(EHSection && "Failed to get the required EH section");
1080 
1081  // Switch to .ARM.extab or .ARM.exidx section
1082  SwitchSection(EHSection);
1083  EmitCodeAlignment(4);
1084 }
1085 
1086 inline void ARMELFStreamer::SwitchToExTabSection(const MCSymbol &FnStart) {
1087  SwitchToEHSection(".ARM.extab",
1090  SectionKind::getDataRel(),
1091  FnStart);
1092 }
1093 
1094 inline void ARMELFStreamer::SwitchToExIdxSection(const MCSymbol &FnStart) {
1095  SwitchToEHSection(".ARM.exidx",
1098  SectionKind::getDataRel(),
1099  FnStart);
1100 }
1101 void ARMELFStreamer::EmitFixup(const MCExpr *Expr, MCFixupKind Kind) {
1102  MCDataFragment *Frag = getOrCreateDataFragment();
1103  Frag->getFixups().push_back(MCFixup::create(Frag->getContents().size(), Expr,
1104  Kind));
1105 }
1106 
1107 void ARMELFStreamer::Reset() {
1108  ExTab = nullptr;
1109  FnStart = nullptr;
1110  Personality = nullptr;
1111  PersonalityIndex = ARM::EHABI::NUM_PERSONALITY_INDEX;
1112  FPReg = ARM::SP;
1113  FPOffset = 0;
1114  SPOffset = 0;
1115  PendingOffset = 0;
1116  UsedFP = false;
1117  CantUnwind = false;
1118 
1119  Opcodes.clear();
1120  UnwindOpAsm.Reset();
1121 }
1122 
1123 void ARMELFStreamer::emitFnStart() {
1124  assert(FnStart == nullptr);
1125  FnStart = getContext().createTempSymbol();
1126  EmitLabel(FnStart);
1127 }
1128 
1129 void ARMELFStreamer::emitFnEnd() {
1130  assert(FnStart && ".fnstart must precedes .fnend");
1131 
1132  // Emit unwind opcodes if there is no .handlerdata directive
1133  if (!ExTab && !CantUnwind)
1134  FlushUnwindOpcodes(true);
1135 
1136  // Emit the exception index table entry
1137  SwitchToExIdxSection(*FnStart);
1138 
1139  if (PersonalityIndex < ARM::EHABI::NUM_PERSONALITY_INDEX)
1140  EmitPersonalityFixup(GetAEABIUnwindPersonalityName(PersonalityIndex));
1141 
1142  const MCSymbolRefExpr *FnStartRef =
1143  MCSymbolRefExpr::create(FnStart,
1144  MCSymbolRefExpr::VK_ARM_PREL31,
1145  getContext());
1146 
1147  EmitValue(FnStartRef, 4);
1148 
1149  if (CantUnwind) {
1150  EmitIntValue(ARM::EHABI::EXIDX_CANTUNWIND, 4);
1151  } else if (ExTab) {
1152  // Emit a reference to the unwind opcodes in the ".ARM.extab" section.
1153  const MCSymbolRefExpr *ExTabEntryRef =
1154  MCSymbolRefExpr::create(ExTab,
1155  MCSymbolRefExpr::VK_ARM_PREL31,
1156  getContext());
1157  EmitValue(ExTabEntryRef, 4);
1158  } else {
1159  // For the __aeabi_unwind_cpp_pr0, we have to emit the unwind opcodes in
1160  // the second word of exception index table entry. The size of the unwind
1161  // opcodes should always be 4 bytes.
1162  assert(PersonalityIndex == ARM::EHABI::AEABI_UNWIND_CPP_PR0 &&
1163  "Compact model must use __aeabi_unwind_cpp_pr0 as personality");
1164  assert(Opcodes.size() == 4u &&
1165  "Unwind opcode size for __aeabi_unwind_cpp_pr0 must be equal to 4");
1166  uint64_t Intval = Opcodes[0] |
1167  Opcodes[1] << 8 |
1168  Opcodes[2] << 16 |
1169  Opcodes[3] << 24;
1170  EmitIntValue(Intval, Opcodes.size());
1171  }
1172 
1173  // Switch to the section containing FnStart
1174  SwitchSection(&FnStart->getSection());
1175 
1176  // Clean exception handling frame information
1177  Reset();
1178 }
1179 
1180 void ARMELFStreamer::emitCantUnwind() { CantUnwind = true; }
1181 
1182 // Add the R_ARM_NONE fixup at the same position
1183 void ARMELFStreamer::EmitPersonalityFixup(StringRef Name) {
1184  const MCSymbol *PersonalitySym = getContext().getOrCreateSymbol(Name);
1185 
1186  const MCSymbolRefExpr *PersonalityRef = MCSymbolRefExpr::create(
1187  PersonalitySym, MCSymbolRefExpr::VK_ARM_NONE, getContext());
1188 
1189  visitUsedExpr(*PersonalityRef);
1190  MCDataFragment *DF = getOrCreateDataFragment();
1191  DF->getFixups().push_back(MCFixup::create(DF->getContents().size(),
1192  PersonalityRef,
1193  MCFixup::getKindForSize(4, false)));
1194 }
1195 
1196 void ARMELFStreamer::FlushPendingOffset() {
1197  if (PendingOffset != 0) {
1198  UnwindOpAsm.EmitSPOffset(-PendingOffset);
1199  PendingOffset = 0;
1200  }
1201 }
1202 
1203 void ARMELFStreamer::FlushUnwindOpcodes(bool NoHandlerData) {
1204  // Emit the unwind opcode to restore $sp.
1205  if (UsedFP) {
1206  const MCRegisterInfo *MRI = getContext().getRegisterInfo();
1207  int64_t LastRegSaveSPOffset = SPOffset - PendingOffset;
1208  UnwindOpAsm.EmitSPOffset(LastRegSaveSPOffset - FPOffset);
1209  UnwindOpAsm.EmitSetSP(MRI->getEncodingValue(FPReg));
1210  } else {
1211  FlushPendingOffset();
1212  }
1213 
1214  // Finalize the unwind opcode sequence
1215  UnwindOpAsm.Finalize(PersonalityIndex, Opcodes);
1216 
1217  // For compact model 0, we have to emit the unwind opcodes in the .ARM.exidx
1218  // section. Thus, we don't have to create an entry in the .ARM.extab
1219  // section.
1220  if (NoHandlerData && PersonalityIndex == ARM::EHABI::AEABI_UNWIND_CPP_PR0)
1221  return;
1222 
1223  // Switch to .ARM.extab section.
1224  SwitchToExTabSection(*FnStart);
1225 
1226  // Create .ARM.extab label for offset in .ARM.exidx
1227  assert(!ExTab);
1228  ExTab = getContext().createTempSymbol();
1229  EmitLabel(ExTab);
1230 
1231  // Emit personality
1232  if (Personality) {
1233  const MCSymbolRefExpr *PersonalityRef =
1234  MCSymbolRefExpr::create(Personality,
1235  MCSymbolRefExpr::VK_ARM_PREL31,
1236  getContext());
1237 
1238  EmitValue(PersonalityRef, 4);
1239  }
1240 
1241  // Emit unwind opcodes
1242  assert((Opcodes.size() % 4) == 0 &&
1243  "Unwind opcode size for __aeabi_cpp_unwind_pr0 must be multiple of 4");
1244  for (unsigned I = 0; I != Opcodes.size(); I += 4) {
1245  uint64_t Intval = Opcodes[I] |
1246  Opcodes[I + 1] << 8 |
1247  Opcodes[I + 2] << 16 |
1248  Opcodes[I + 3] << 24;
1249  EmitIntValue(Intval, 4);
1250  }
1251 
1252  // According to ARM EHABI section 9.2, if the __aeabi_unwind_cpp_pr1() or
1253  // __aeabi_unwind_cpp_pr2() is used, then the handler data must be emitted
1254  // after the unwind opcodes. The handler data consists of several 32-bit
1255  // words, and should be terminated by zero.
1256  //
1257  // In case that the .handlerdata directive is not specified by the
1258  // programmer, we should emit zero to terminate the handler data.
1259  if (NoHandlerData && !Personality)
1260  EmitIntValue(0, 4);
1261 }
1262 
1263 void ARMELFStreamer::emitHandlerData() { FlushUnwindOpcodes(false); }
1264 
1265 void ARMELFStreamer::emitPersonality(const MCSymbol *Per) {
1266  Personality = Per;
1267  UnwindOpAsm.setPersonality(Per);
1268 }
1269 
1270 void ARMELFStreamer::emitPersonalityIndex(unsigned Index) {
1271  assert(Index < ARM::EHABI::NUM_PERSONALITY_INDEX && "invalid index");
1272  PersonalityIndex = Index;
1273 }
1274 
1275 void ARMELFStreamer::emitSetFP(unsigned NewFPReg, unsigned NewSPReg,
1276  int64_t Offset) {
1277  assert((NewSPReg == ARM::SP || NewSPReg == FPReg) &&
1278  "the operand of .setfp directive should be either $sp or $fp");
1279 
1280  UsedFP = true;
1281  FPReg = NewFPReg;
1282 
1283  if (NewSPReg == ARM::SP)
1284  FPOffset = SPOffset + Offset;
1285  else
1286  FPOffset += Offset;
1287 }
1288 
1289 void ARMELFStreamer::emitMovSP(unsigned Reg, int64_t Offset) {
1290  assert((Reg != ARM::SP && Reg != ARM::PC) &&
1291  "the operand of .movsp cannot be either sp or pc");
1292  assert(FPReg == ARM::SP && "current FP must be SP");
1293 
1294  FlushPendingOffset();
1295 
1296  FPReg = Reg;
1297  FPOffset = SPOffset + Offset;
1298 
1299  const MCRegisterInfo *MRI = getContext().getRegisterInfo();
1300  UnwindOpAsm.EmitSetSP(MRI->getEncodingValue(FPReg));
1301 }
1302 
1303 void ARMELFStreamer::emitPad(int64_t Offset) {
1304  // Track the change of the $sp offset
1305  SPOffset -= Offset;
1306 
1307  // To squash multiple .pad directives, we should delay the unwind opcode
1308  // until the .save, .vsave, .handlerdata, or .fnend directives.
1309  PendingOffset -= Offset;
1310 }
1311 
1312 void ARMELFStreamer::emitRegSave(const SmallVectorImpl<unsigned> &RegList,
1313  bool IsVector) {
1314  // Collect the registers in the register list
1315  unsigned Count = 0;
1316  uint32_t Mask = 0;
1317  const MCRegisterInfo *MRI = getContext().getRegisterInfo();
1318  for (size_t i = 0; i < RegList.size(); ++i) {
1319  unsigned Reg = MRI->getEncodingValue(RegList[i]);
1320  assert(Reg < (IsVector ? 32U : 16U) && "Register out of range");
1321  unsigned Bit = (1u << Reg);
1322  if ((Mask & Bit) == 0) {
1323  Mask |= Bit;
1324  ++Count;
1325  }
1326  }
1327 
1328  // Track the change the $sp offset: For the .save directive, the
1329  // corresponding push instruction will decrease the $sp by (4 * Count).
1330  // For the .vsave directive, the corresponding vpush instruction will
1331  // decrease $sp by (8 * Count).
1332  SPOffset -= Count * (IsVector ? 8 : 4);
1333 
1334  // Emit the opcode
1335  FlushPendingOffset();
1336  if (IsVector)
1337  UnwindOpAsm.EmitVFPRegSave(Mask);
1338  else
1339  UnwindOpAsm.EmitRegSave(Mask);
1340 }
1341 
1342 void ARMELFStreamer::emitUnwindRaw(int64_t Offset,
1343  const SmallVectorImpl<uint8_t> &Opcodes) {
1344  FlushPendingOffset();
1345  SPOffset = SPOffset - Offset;
1346  UnwindOpAsm.EmitRaw(Opcodes);
1347 }
1348 
1349 namespace llvm {
1350 
1353  MCInstPrinter *InstPrint,
1354  bool isVerboseAsm) {
1355  return new ARMTargetAsmStreamer(S, OS, *InstPrint, isVerboseAsm);
1356 }
1357 
1359  return new ARMTargetStreamer(S);
1360 }
1361 
1363  const MCSubtargetInfo &STI) {
1364  const Triple &TT = STI.getTargetTriple();
1365  if (TT.isOSBinFormatELF())
1366  return new ARMTargetELFStreamer(S);
1367  return new ARMTargetStreamer(S);
1368 }
1369 
1371  raw_pwrite_stream &OS,
1372  MCCodeEmitter *Emitter, bool RelaxAll,
1373  bool IsThumb) {
1374  ARMELFStreamer *S = new ARMELFStreamer(Context, TAB, OS, Emitter, IsThumb);
1375  // FIXME: This should eventually end up somewhere else where more
1376  // intelligent flag decisions can be made. For now we are just maintaining
1377  // the status quo for ARM and setting EF_ARM_EABI_VER5 as the default.
1378  S->getAssembler().setELFHeaderEFlags(ELF::EF_ARM_EABI_VER5);
1379 
1380  if (RelaxAll)
1381  S->getAssembler().setRelaxAll(true);
1382  return S;
1383  }
1384 
1385 }
1386 
1387 
void setIsThumbFunc(const MCSymbol *Func)
Flag a function symbol as the target of a .thumb_func directive.
Definition: MCAssembler.h:697
Instances of this class represent a uniqued identifier for a section in the current translation unit...
Definition: MCSection.h:48
virtual void emitInst(uint32_t Inst, char Suffix= '\0')
MCELFStreamer * createARMELFStreamer(MCContext &Context, MCAsmBackend &TAB, raw_pwrite_stream &OS, MCCodeEmitter *Emitter, bool RelaxAll, bool IsThumb)
virtual void AnnotateTLSDescriptorSequence(const MCSymbolRefExpr *SRE)
virtual void emitFPU(unsigned FPU)
MCSectionSubPair getPreviousSection() const
Return the previous section that the streamer is emitting code to.
Definition: MCStreamer.h:282
const MCAsmInfo * getAsmInfo() const
Definition: MCContext.h:225
StringRef getSectionName() const
Definition: MCSectionELF.h:75
void EmitBytes(StringRef Data) override
Emit the bytes in Data into the output.
const MCSymbol & getSymbol() const
Definition: MCExpr.h:328
void print(raw_ostream &OS, const MCAsmInfo *MAI) const
print - Print the value to the stream OS.
Definition: MCSymbol.cpp:51
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:39
formatted_raw_ostream - A raw_ostream that wraps another one and keeps track of line and column posit...
Target specific streamer interface.
Definition: MCStreamer.h:73
virtual void emitPad(int64_t Offset)
LLVM_ATTRIBUTE_NORETURN void reportFatalError(SMLoc L, const Twine &Msg) const
Definition: MCContext.cpp:474
unsigned getUniqueID() const
Definition: MCSectionELF.h:87
virtual void finishAttributeSection()
FunctionType * getType(LLVMContext &Context, ID id, ArrayRef< Type * > Tys=None)
Return the function type for an intrinsic.
Definition: Function.cpp:822
virtual void emitPersonality(const MCSymbol *Personality)
LLVM_ATTRIBUTE_NORETURN void report_fatal_error(const char *reason, bool gen_crash_diag=true)
Reports a serious error, calling any installed error handler.
virtual void emitPersonalityIndex(unsigned Index)
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:79
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Definition: ErrorHandling.h:98
Base class for the full range of assembler expressions which are needed for parsing.
Definition: MCExpr.h:33
bool EmitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute) override
Add the given Attribute to Symbol.
Reg
All possible values of the reg field in the ModR/M byte.
void EmitLabel(MCSymbol *Symbol) override
Emit a label for Symbol into the current section.
Represent a reference to a symbol from inside an expression.
Definition: MCExpr.h:159
virtual void emitLabel(MCSymbol *Symbol)
Definition: MCStreamer.cpp:37
MCContext & getContext() const
Definition: MCStreamer.h:210
bool isLittleEndian() const
True if the target is little endian.
Definition: MCAsmInfo.h:376
A four-byte fixup.
Definition: MCFixup.h:26
Context object for machine code objects.
Definition: MCContext.h:48
void EmitThumbFunc(MCSymbol *Func) override
Note in the output that the specified Func is a Thumb mode function (ARM target only).
.code16 (X86) / .code 16 (ARM)
Definition: MCDirectives.h:50
const MCSymbolELF * getGroup() const
Definition: MCSectionELF.h:79
virtual void emitMovSP(unsigned Reg, int64_t Offset=0)
void setExternal(bool Value) const
Definition: MCSymbol.h:390
SmallVectorImpl< char > & getContents()
Definition: MCAssembler.h:186
MCTargetStreamer * createARMTargetAsmStreamer(MCStreamer &S, formatted_raw_ostream &OS, MCInstPrinter *InstPrint, bool isVerboseAsm)
Instances of this class represent a single low-level machine instruction.
Definition: MCInst.h:150
virtual void emitAttribute(unsigned Attribute, unsigned Value)
Flag
These should be considered private to the implementation of the MCInstrDesc class.
Definition: MCInstrDesc.h:97
MCRegisterInfo base class - We assume that the target defines a static array of MCRegisterDesc object...
This class is intended to be used as a base class for asm properties and features specific to the tar...
Definition: MCAsmInfo.h:58
StringRef AttrTypeAsString(unsigned Attr, bool HasTagPrefix=true)
void ChangeSection(MCSection *Section, const MCExpr *Subsection) override
Update streamer for a new active section.
MCTargetStreamer * createARMObjectTargetStreamer(MCStreamer &S, const MCSubtargetInfo &STI)
void print(raw_ostream &OS, const MCAsmInfo *MAI) const
Definition: MCExpr.cpp:33
Streaming machine code generation interface.
Definition: MCStreamer.h:157
virtual void emitRegSave(const SmallVectorImpl< unsigned > &RegList, bool isVector)
The instances of the Type class are immutable: once they are created, they are never changed...
Definition: Type.h:45
virtual void emitThumbSet(MCSymbol *Symbol, const MCExpr *Value)
MCAssembler & getAssembler()
virtual void emitSetFP(unsigned FpReg, unsigned SpReg, int64_t Offset=0)
MCCodeEmitter - Generic instruction encoding interface.
Definition: MCCodeEmitter.h:23
virtual void emitObjectArch(unsigned Arch)
MCFixupKind
Extensible enumeration to represent the type of a fixup.
Definition: MCFixup.h:23
SmallVectorImpl< MCFixup > & getFixups()
Definition: MCAssembler.h:211
Special entry for the function never unwind.
Definition: ARMEHABI.h:36
virtual void emitUnwindRaw(int64_t StackOffset, const SmallVectorImpl< uint8_t > &Opcodes)
.subsections_via_symbols (MachO)
Definition: MCDirectives.h:49
SectionKind - This is a simple POD value that classifies the properties of a section.
Definition: SectionKind.h:28
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:44
unsigned getULEB128Size(uint64_t Value)
Utility function to get the size of the ULEB128-encoded value.
Definition: LEB128.cpp:20
bool isDefined() const
isDefined - Check if this symbol is defined (i.e., it has an address).
Definition: MCSymbol.h:251
MCStreamer & getStreamer()
Definition: MCStreamer.h:81
void EmitInstruction(const MCInst &Inst, const MCSubtargetInfo &STI) override
Emit the given Instruction into the current section.
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:861
MCSection & getSection() const
Get the section associated with a defined, non-absolute symbol.
Definition: MCSymbol.h:264
.syntax (ARM/ELF)
Definition: MCDirectives.h:48
.code32 (X86) / .code 32 (ARM)
Definition: MCDirectives.h:51
.code64 (X86)
Definition: MCDirectives.h:52
This is an instance of a target assembly language printer that converts an MCInst to valid target ass...
Definition: MCInstPrinter.h:38
bool isOSBinFormatELF() const
Tests whether the OS uses the ELF binary format.
Definition: Triple.h:479
void EmitValueImpl(const MCExpr *Value, unsigned Size, const SMLoc &Loc=SMLoc()) override
Emit the expression Value into the output as a native integer of the given Size bytes.
StringRef getName() const
getName - Get the symbol name.
Definition: MCSymbol.h:205
MCSymbol * getOrCreateSymbol(const Twine &Name)
Lookup the symbol inside with the specified Name.
Definition: MCContext.cpp:111
MCAssemblerFlag
Definition: MCDirectives.h:47
.type _foo, STT_FUNC # aka
Definition: MCDirectives.h:23
virtual void emitIntTextAttribute(unsigned Attribute, unsigned IntValue, StringRef StringValue="")
#define I(x, y, z)
Definition: MD5.cpp:54
MCSubtargetInfo - Generic base class for all target subtargets.
MCSectionELF - This represents a section on linux, lots of unix variants and some bare metal systems...
Definition: MCSectionELF.h:30
const Triple & getTargetTriple() const
getTargetTriple - Return the target triple string.
static std::string utohexstr(uint64_t X, bool LowerCase=false)
Definition: StringExtras.h:72
An abstract base class for streams implementations that also support a pwrite operation.
Definition: raw_ostream.h:321
Fragment for data and encoded instructions.
Definition: MCAssembler.h:228
const ARM::ArchExtKind Kind
MCTargetStreamer * createARMNullTargetStreamer(MCStreamer &S)
cl::opt< bool > RelaxAll("mc-relax-all", cl::desc("When used with filetype=obj, ""relax all fixups in the emitted object file"))
uint16_t getEncodingValue(unsigned RegNo) const
Returns the encoding for RegNo.
LLVM Value Representation.
Definition: Value.h:69
Generic interface to target specific assembler backends.
Definition: MCAsmBackend.h:34
void EmitAssemblerFlag(MCAssemblerFlag Flag) override
Note in the output the specified Flag.
static TraceState * TS
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:40
virtual void emitArch(unsigned Arch)
static std::string GetAEABIUnwindPersonalityName(unsigned Index)
Represents a location in source code.
Definition: SMLoc.h:23
void FinishImpl() override
Streamer specific finalization.
std::string lower() const
Definition: StringRef.cpp:117
virtual void switchVendor(StringRef Vendor)
bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:110
virtual void emitTextAttribute(unsigned Attribute, StringRef String)