1 //===- lib/MC/ARMELFStreamer.cpp - ELF Object Output for ARM --------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file assembles .s files and emits ARM ELF .o object files. Different
10 // from generic ELF streamer in emitting mapping symbols ($a, $t and $d) to
11 // delimit regions of data and code.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #include "ARMRegisterInfo.h"
16 #include "ARMUnwindOpAsm.h"
17 #include "llvm/ADT/DenseMap.h"
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>
58
59 using namespace llvm;
60
GetAEABIUnwindPersonalityName(unsigned Index)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();
65 }
66
67 namespace {
68
69 class ARMELFStreamer;
70
71 class ARMTargetAsmStreamer : public ARMTargetStreamer {
72 formatted_raw_ostream &OS;
73 MCInstPrinter &InstPrinter;
74 bool IsVerboseAsm;
75
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;
89
90 void switchVendor(StringRef Vendor) override;
91 void emitAttribute(unsigned Attribute, unsigned Value) override;
92 void emitTextAttribute(unsigned Attribute, StringRef String) override;
93 void emitIntTextAttribute(unsigned Attribute, unsigned IntValue,
94 StringRef StringValue) override;
95 void emitArch(ARM::ArchKind Arch) override;
96 void emitArchExtension(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;
101
102 void AnnotateTLSDescriptorSequence(const MCSymbolRefExpr *SRE) override;
103 void emitThumbSet(MCSymbol *Symbol, const MCExpr *Value) override;
104
105 public:
106 ARMTargetAsmStreamer(MCStreamer &S, formatted_raw_ostream &OS,
107 MCInstPrinter &InstPrinter, bool VerboseAsm);
108 };
109
ARMTargetAsmStreamer(MCStreamer & S,formatted_raw_ostream & OS,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) {}
116
emitFnStart()117 void ARMTargetAsmStreamer::emitFnStart() { OS << "\t.fnstart\n"; }
emitFnEnd()118 void ARMTargetAsmStreamer::emitFnEnd() { OS << "\t.fnend\n"; }
emitCantUnwind()119 void ARMTargetAsmStreamer::emitCantUnwind() { OS << "\t.cantunwind\n"; }
120
emitPersonality(const MCSymbol * Personality)121 void ARMTargetAsmStreamer::emitPersonality(const MCSymbol *Personality) {
122 OS << "\t.personality " << Personality->getName() << '\n';
123 }
124
emitPersonalityIndex(unsigned Index)125 void ARMTargetAsmStreamer::emitPersonalityIndex(unsigned Index) {
126 OS << "\t.personalityindex " << Index << '\n';
127 }
128
emitHandlerData()129 void ARMTargetAsmStreamer::emitHandlerData() { OS << "\t.handlerdata\n"; }
130
emitSetFP(unsigned FpReg,unsigned SpReg,int64_t Offset)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';
140 }
141
emitMovSP(unsigned Reg,int64_t Offset)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");
145
146 OS << "\t.movsp\t";
147 InstPrinter.printRegName(OS, Reg);
148 if (Offset)
149 OS << ", #" << Offset;
150 OS << '\n';
151 }
152
emitPad(int64_t Offset)153 void ARMTargetAsmStreamer::emitPad(int64_t Offset) {
154 OS << "\t.pad\t#" << Offset << '\n';
155 }
156
emitRegSave(const SmallVectorImpl<unsigned> & RegList,bool isVector)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{";
164
165 InstPrinter.printRegName(OS, RegList[0]);
166
167 for (unsigned i = 1, e = RegList.size(); i != e; ++i) {
168 OS << ", ";
169 InstPrinter.printRegName(OS, RegList[i]);
170 }
171
172 OS << "}\n";
173 }
174
switchVendor(StringRef Vendor)175 void ARMTargetAsmStreamer::switchVendor(StringRef Vendor) {}
176
emitAttribute(unsigned Attribute,unsigned Value)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;
183 }
184 OS << "\n";
185 }
186
emitTextAttribute(unsigned Attribute,StringRef String)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;
199 }
200 break;
201 }
202 OS << "\n";
203 }
204
emitIntTextAttribute(unsigned Attribute,unsigned IntValue,StringRef StringValue)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;
217 }
218 OS << "\n";
219 }
220
emitArch(ARM::ArchKind Arch)221 void ARMTargetAsmStreamer::emitArch(ARM::ArchKind Arch) {
222 OS << "\t.arch\t" << ARM::getArchName(Arch) << "\n";
223 }
224
emitArchExtension(unsigned ArchExt)225 void ARMTargetAsmStreamer::emitArchExtension(unsigned ArchExt) {
226 OS << "\t.arch_extension\t" << ARM::getArchExtName(ArchExt) << "\n";
227 }
228
emitObjectArch(ARM::ArchKind Arch)229 void ARMTargetAsmStreamer::emitObjectArch(ARM::ArchKind Arch) {
230 OS << "\t.object_arch\t" << ARM::getArchName(Arch) << '\n';
231 }
232
emitFPU(unsigned FPU)233 void ARMTargetAsmStreamer::emitFPU(unsigned FPU) {
234 OS << "\t.fpu\t" << ARM::getFPUName(FPU) << "\n";
235 }
236
finishAttributeSection()237 void ARMTargetAsmStreamer::finishAttributeSection() {}
238
239 void
AnnotateTLSDescriptorSequence(const MCSymbolRefExpr * S)240 ARMTargetAsmStreamer::AnnotateTLSDescriptorSequence(const MCSymbolRefExpr *S) {
241 OS << "\t.tlsdescseq\t" << S->getSymbol().getName();
242 }
243
emitThumbSet(MCSymbol * Symbol,const MCExpr * Value)244 void ARMTargetAsmStreamer::emitThumbSet(MCSymbol *Symbol, const MCExpr *Value) {
245 const MCAsmInfo *MAI = Streamer.getContext().getAsmInfo();
246
247 OS << "\t.thumb_set\t";
248 Symbol->print(OS, MAI);
249 OS << ", ";
250 Value->print(OS, MAI);
251 OS << '\n';
252 }
253
emitInst(uint32_t Inst,char Suffix)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";
259 }
260
emitUnwindRaw(int64_t Offset,const SmallVectorImpl<uint8_t> & Opcodes)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';
269 }
270
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;
286
LessTag__anon7f2171200111::ARMTargetELFStreamer::AttributeItem287 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):
291 //
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."
296 //
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));
301 }
302 };
303
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;
309
310 MCSection *AttributeSection = nullptr;
311
getAttributeItem(unsigned Attribute)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;
317 }
318
setAttributeItem(unsigned Attribute,unsigned Value,bool OverwriteExisting)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;
328 }
329
330 // Create new attribute item
331 AttributeItem Item = {
332 AttributeItem::NumericAttribute,
333 Attribute,
334 Value,
335 StringRef("")
336 };
337 Contents.push_back(Item);
338 }
339
setAttributeItem(unsigned Attribute,StringRef Value,bool OverwriteExisting)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;
349 }
350
351 // Create new attribute item
352 AttributeItem Item = {
353 AttributeItem::TextAttribute,
354 Attribute,
355 0,
356 Value
357 };
358 Contents.push_back(Item);
359 }
360
setAttributeItems(unsigned Attribute,unsigned IntValue,StringRef StringValue,bool OverwriteExisting)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;
371 }
372
373 // Create new attribute item
374 AttributeItem Item = {
375 AttributeItem::NumericAndTextAttributes,
376 Attribute,
377 IntValue,
378 StringValue
379 };
380 Contents.push_back(Item);
381 }
382
383 void emitArchDefaultAttributes();
384 void emitFPUDefaultAttributes();
385
386 ARMELFStreamer &getStreamer();
387
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;
401
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;
413
414 void AnnotateTLSDescriptorSequence(const MCSymbolRefExpr *SRE) override;
415 void emitThumbSet(MCSymbol *Symbol, const MCExpr *Value) override;
416
417 size_t calculateContentSize() const;
418
419 // Reset state between object emissions
420 void reset() override;
421
422 public:
ARMTargetELFStreamer(MCStreamer & S)423 ARMTargetELFStreamer(MCStreamer &S)
424 : ARMTargetStreamer(S), CurrentVendor("aeabi") {}
425 };
426
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.
430 ///
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).
436 ///
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;
442
ARMELFStreamer(MCContext & Context,std::unique_ptr<MCAsmBackend> TAB,std::unique_ptr<MCObjectWriter> OW,std::unique_ptr<MCCodeEmitter> Emitter,bool IsThumb,bool IsAndroid)443 ARMELFStreamer(MCContext &Context, std::unique_ptr<MCAsmBackend> TAB,
444 std::unique_ptr<MCObjectWriter> OW,
445 std::unique_ptr<MCCodeEmitter> Emitter, bool IsThumb,
446 bool IsAndroid)
447 : MCELFStreamer(Context, std::move(TAB), std::move(OW),
448 std::move(Emitter)),
449 IsThumb(IsThumb), IsAndroid(IsAndroid) {
450 EHReset();
451 }
452
453 ~ARMELFStreamer() override = default;
454
455 void FinishImpl() override;
456
457 // ARM exception handling directives
458 void emitFnStart();
459 void emitFnEnd();
460 void emitCantUnwind();
461 void emitPersonality(const MCSymbol *Per);
462 void emitPersonalityIndex(unsigned index);
463 void emitHandlerData();
464 void emitSetFP(unsigned NewFpReg, unsigned NewSpReg, int64_t Offset = 0);
465 void emitMovSP(unsigned Reg, int64_t Offset = 0);
466 void emitPad(int64_t Offset);
467 void emitRegSave(const SmallVectorImpl<unsigned> &RegList, bool isVector);
468 void emitUnwindRaw(int64_t Offset, const SmallVectorImpl<uint8_t> &Opcodes);
emitFill(const MCExpr & NumBytes,uint64_t FillValue,SMLoc Loc)469 void emitFill(const MCExpr &NumBytes, uint64_t FillValue,
470 SMLoc Loc) override {
471 EmitDataMappingSymbol();
472 MCObjectStreamer::emitFill(NumBytes, FillValue, Loc);
473 }
474
ChangeSection(MCSection * Section,const MCExpr * Subsection)475 void ChangeSection(MCSection *Section, const MCExpr *Subsection) override {
476 LastMappingSymbols[getCurrentSection().first] = std::move(LastEMSInfo);
477 MCELFStreamer::ChangeSection(Section, Subsection);
478 auto LastMappingSymbol = LastMappingSymbols.find(Section);
479 if (LastMappingSymbol != LastMappingSymbols.end()) {
480 LastEMSInfo = std::move(LastMappingSymbol->second);
481 return;
482 }
483 LastEMSInfo.reset(new ElfMappingSymbolInfo(SMLoc(), nullptr, 0));
484 }
485
486 /// This function is the one used to emit instruction data into the ELF
487 /// streamer. We override it to add the appropriate mapping symbol if
488 /// necessary.
EmitInstruction(const MCInst & Inst,const MCSubtargetInfo & STI)489 void EmitInstruction(const MCInst &Inst,
490 const MCSubtargetInfo &STI) override {
491 if (IsThumb)
492 EmitThumbMappingSymbol();
493 else
494 EmitARMMappingSymbol();
495
496 MCELFStreamer::EmitInstruction(Inst, STI);
497 }
498
emitInst(uint32_t Inst,char Suffix)499 void emitInst(uint32_t Inst, char Suffix) {
500 unsigned Size;
501 char Buffer[4];
502 const bool LittleEndian = getContext().getAsmInfo()->isLittleEndian();
503
504 switch (Suffix) {
505 case '\0':
506 Size = 4;
507
508 assert(!IsThumb);
509 EmitARMMappingSymbol();
510 for (unsigned II = 0, IE = Size; II != IE; II++) {
511 const unsigned I = LittleEndian ? (Size - II - 1) : II;
512 Buffer[Size - II - 1] = uint8_t(Inst >> I * CHAR_BIT);
513 }
514
515 break;
516 case 'n':
517 case 'w':
518 Size = (Suffix == 'n' ? 2 : 4);
519
520 assert(IsThumb);
521 EmitThumbMappingSymbol();
522 // Thumb wide instructions are emitted as a pair of 16-bit words of the
523 // appropriate endianness.
524 for (unsigned II = 0, IE = Size; II != IE; II = II + 2) {
525 const unsigned I0 = LittleEndian ? II + 0 : II + 1;
526 const unsigned I1 = LittleEndian ? II + 1 : II + 0;
527 Buffer[Size - II - 2] = uint8_t(Inst >> I0 * CHAR_BIT);
528 Buffer[Size - II - 1] = uint8_t(Inst >> I1 * CHAR_BIT);
529 }
530
531 break;
532 default:
533 llvm_unreachable("Invalid Suffix");
534 }
535
536 MCELFStreamer::EmitBytes(StringRef(Buffer, Size));
537 }
538
539 /// This is one of the functions used to emit data into an ELF section, so the
540 /// ARM streamer overrides it to add the appropriate mapping symbol ($d) if
541 /// necessary.
EmitBytes(StringRef Data)542 void EmitBytes(StringRef Data) override {
543 EmitDataMappingSymbol();
544 MCELFStreamer::EmitBytes(Data);
545 }
546
FlushPendingMappingSymbol()547 void FlushPendingMappingSymbol() {
548 if (!LastEMSInfo->hasInfo())
549 return;
550 ElfMappingSymbolInfo *EMS = LastEMSInfo.get();
551 EmitMappingSymbol("$d", EMS->Loc, EMS->F, EMS->Offset);
552 EMS->resetInfo();
553 }
554
555 /// This is one of the functions used to emit data into an ELF section, so the
556 /// ARM streamer overrides it to add the appropriate mapping symbol ($d) if
557 /// necessary.
EmitValueImpl(const MCExpr * Value,unsigned Size,SMLoc Loc)558 void EmitValueImpl(const MCExpr *Value, unsigned Size, SMLoc Loc) override {
559 if (const MCSymbolRefExpr *SRE = dyn_cast_or_null<MCSymbolRefExpr>(Value)) {
560 if (SRE->getKind() == MCSymbolRefExpr::VK_ARM_SBREL && !(Size == 4)) {
561 getContext().reportError(Loc, "relocated expression must be 32-bit");
562 return;
563 }
564 getOrCreateDataFragment();
565 }
566
567 EmitDataMappingSymbol();
568 MCELFStreamer::EmitValueImpl(Value, Size, Loc);
569 }
570
EmitAssemblerFlag(MCAssemblerFlag Flag)571 void EmitAssemblerFlag(MCAssemblerFlag Flag) override {
572 MCELFStreamer::EmitAssemblerFlag(Flag);
573
574 switch (Flag) {
575 case MCAF_SyntaxUnified:
576 return; // no-op here.
577 case MCAF_Code16:
578 IsThumb = true;
579 return; // Change to Thumb mode
580 case MCAF_Code32:
581 IsThumb = false;
582 return; // Change to ARM mode
583 case MCAF_Code64:
584 return;
585 case MCAF_SubsectionsViaSymbols:
586 return;
587 }
588 }
589
590 private:
591 enum ElfMappingSymbol {
592 EMS_None,
593 EMS_ARM,
594 EMS_Thumb,
595 EMS_Data
596 };
597
598 struct ElfMappingSymbolInfo {
ElfMappingSymbolInfo__anon7f2171200111::ARMELFStreamer::ElfMappingSymbolInfo599 explicit ElfMappingSymbolInfo(SMLoc Loc, MCFragment *F, uint64_t O)
600 : Loc(Loc), F(F), Offset(O), State(EMS_None) {}
resetInfo__anon7f2171200111::ARMELFStreamer::ElfMappingSymbolInfo601 void resetInfo() {
602 F = nullptr;
603 Offset = 0;
604 }
hasInfo__anon7f2171200111::ARMELFStreamer::ElfMappingSymbolInfo605 bool hasInfo() { return F != nullptr; }
606 SMLoc Loc;
607 MCFragment *F;
608 uint64_t Offset;
609 ElfMappingSymbol State;
610 };
611
EmitDataMappingSymbol()612 void EmitDataMappingSymbol() {
613 if (LastEMSInfo->State == EMS_Data)
614 return;
615 else if (LastEMSInfo->State == EMS_None) {
616 // This is a tentative symbol, it won't really be emitted until it's
617 // actually needed.
618 ElfMappingSymbolInfo *EMS = LastEMSInfo.get();
619 auto *DF = dyn_cast_or_null<MCDataFragment>(getCurrentFragment());
620 if (!DF)
621 return;
622 EMS->Loc = SMLoc();
623 EMS->F = getCurrentFragment();
624 EMS->Offset = DF->getContents().size();
625 LastEMSInfo->State = EMS_Data;
626 return;
627 }
628 EmitMappingSymbol("$d");
629 LastEMSInfo->State = EMS_Data;
630 }
631
EmitThumbMappingSymbol()632 void EmitThumbMappingSymbol() {
633 if (LastEMSInfo->State == EMS_Thumb)
634 return;
635 FlushPendingMappingSymbol();
636 EmitMappingSymbol("$t");
637 LastEMSInfo->State = EMS_Thumb;
638 }
639
EmitARMMappingSymbol()640 void EmitARMMappingSymbol() {
641 if (LastEMSInfo->State == EMS_ARM)
642 return;
643 FlushPendingMappingSymbol();
644 EmitMappingSymbol("$a");
645 LastEMSInfo->State = EMS_ARM;
646 }
647
EmitMappingSymbol(StringRef Name)648 void EmitMappingSymbol(StringRef Name) {
649 auto *Symbol = cast<MCSymbolELF>(getContext().getOrCreateSymbol(
650 Name + "." + Twine(MappingSymbolCounter++)));
651 EmitLabel(Symbol);
652
653 Symbol->setType(ELF::STT_NOTYPE);
654 Symbol->setBinding(ELF::STB_LOCAL);
655 Symbol->setExternal(false);
656 }
657
EmitMappingSymbol(StringRef Name,SMLoc Loc,MCFragment * F,uint64_t Offset)658 void EmitMappingSymbol(StringRef Name, SMLoc Loc, MCFragment *F,
659 uint64_t Offset) {
660 auto *Symbol = cast<MCSymbolELF>(getContext().getOrCreateSymbol(
661 Name + "." + Twine(MappingSymbolCounter++)));
662 EmitLabelAtPos(Symbol, Loc, F, Offset);
663 Symbol->setType(ELF::STT_NOTYPE);
664 Symbol->setBinding(ELF::STB_LOCAL);
665 Symbol->setExternal(false);
666 }
667
EmitThumbFunc(MCSymbol * Func)668 void EmitThumbFunc(MCSymbol *Func) override {
669 getAssembler().setIsThumbFunc(Func);
670 EmitSymbolAttribute(Func, MCSA_ELF_TypeFunction);
671 }
672
673 // Helper functions for ARM exception handling directives
674 void EHReset();
675
676 // Reset state between object emissions
677 void reset() override;
678
679 void EmitPersonalityFixup(StringRef Name);
680 void FlushPendingOffset();
681 void FlushUnwindOpcodes(bool NoHandlerData);
682
683 void SwitchToEHSection(StringRef Prefix, unsigned Type, unsigned Flags,
684 SectionKind Kind, const MCSymbol &Fn);
685 void SwitchToExTabSection(const MCSymbol &FnStart);
686 void SwitchToExIdxSection(const MCSymbol &FnStart);
687
688 void EmitFixup(const MCExpr *Expr, MCFixupKind Kind);
689
690 bool IsThumb;
691 bool IsAndroid;
692 int64_t MappingSymbolCounter = 0;
693
694 DenseMap<const MCSection *, std::unique_ptr<ElfMappingSymbolInfo>>
695 LastMappingSymbols;
696
697 std::unique_ptr<ElfMappingSymbolInfo> LastEMSInfo;
698
699 // ARM Exception Handling Frame Information
700 MCSymbol *ExTab;
701 MCSymbol *FnStart;
702 const MCSymbol *Personality;
703 unsigned PersonalityIndex;
704 unsigned FPReg; // Frame pointer register
705 int64_t FPOffset; // Offset: (final frame pointer) - (initial $sp)
706 int64_t SPOffset; // Offset: (final $sp) - (initial $sp)
707 int64_t PendingOffset; // Offset: (final $sp) - (emitted $sp)
708 bool UsedFP;
709 bool CantUnwind;
710 SmallVector<uint8_t, 64> Opcodes;
711 UnwindOpcodeAssembler UnwindOpAsm;
712 };
713
714 } // end anonymous namespace
715
getStreamer()716 ARMELFStreamer &ARMTargetELFStreamer::getStreamer() {
717 return static_cast<ARMELFStreamer &>(Streamer);
718 }
719
emitFnStart()720 void ARMTargetELFStreamer::emitFnStart() { getStreamer().emitFnStart(); }
emitFnEnd()721 void ARMTargetELFStreamer::emitFnEnd() { getStreamer().emitFnEnd(); }
emitCantUnwind()722 void ARMTargetELFStreamer::emitCantUnwind() { getStreamer().emitCantUnwind(); }
723
emitPersonality(const MCSymbol * Personality)724 void ARMTargetELFStreamer::emitPersonality(const MCSymbol *Personality) {
725 getStreamer().emitPersonality(Personality);
726 }
727
emitPersonalityIndex(unsigned Index)728 void ARMTargetELFStreamer::emitPersonalityIndex(unsigned Index) {
729 getStreamer().emitPersonalityIndex(Index);
730 }
731
emitHandlerData()732 void ARMTargetELFStreamer::emitHandlerData() {
733 getStreamer().emitHandlerData();
734 }
735
emitSetFP(unsigned FpReg,unsigned SpReg,int64_t Offset)736 void ARMTargetELFStreamer::emitSetFP(unsigned FpReg, unsigned SpReg,
737 int64_t Offset) {
738 getStreamer().emitSetFP(FpReg, SpReg, Offset);
739 }
740
emitMovSP(unsigned Reg,int64_t Offset)741 void ARMTargetELFStreamer::emitMovSP(unsigned Reg, int64_t Offset) {
742 getStreamer().emitMovSP(Reg, Offset);
743 }
744
emitPad(int64_t Offset)745 void ARMTargetELFStreamer::emitPad(int64_t Offset) {
746 getStreamer().emitPad(Offset);
747 }
748
emitRegSave(const SmallVectorImpl<unsigned> & RegList,bool isVector)749 void ARMTargetELFStreamer::emitRegSave(const SmallVectorImpl<unsigned> &RegList,
750 bool isVector) {
751 getStreamer().emitRegSave(RegList, isVector);
752 }
753
emitUnwindRaw(int64_t Offset,const SmallVectorImpl<uint8_t> & Opcodes)754 void ARMTargetELFStreamer::emitUnwindRaw(int64_t Offset,
755 const SmallVectorImpl<uint8_t> &Opcodes) {
756 getStreamer().emitUnwindRaw(Offset, Opcodes);
757 }
758
switchVendor(StringRef Vendor)759 void ARMTargetELFStreamer::switchVendor(StringRef Vendor) {
760 assert(!Vendor.empty() && "Vendor cannot be empty.");
761
762 if (CurrentVendor == Vendor)
763 return;
764
765 if (!CurrentVendor.empty())
766 finishAttributeSection();
767
768 assert(Contents.empty() &&
769 ".ARM.attributes should be flushed before changing vendor");
770 CurrentVendor = Vendor;
771
772 }
773
emitAttribute(unsigned Attribute,unsigned Value)774 void ARMTargetELFStreamer::emitAttribute(unsigned Attribute, unsigned Value) {
775 setAttributeItem(Attribute, Value, /* OverwriteExisting= */ true);
776 }
777
emitTextAttribute(unsigned Attribute,StringRef Value)778 void ARMTargetELFStreamer::emitTextAttribute(unsigned Attribute,
779 StringRef Value) {
780 setAttributeItem(Attribute, Value, /* OverwriteExisting= */ true);
781 }
782
emitIntTextAttribute(unsigned Attribute,unsigned IntValue,StringRef StringValue)783 void ARMTargetELFStreamer::emitIntTextAttribute(unsigned Attribute,
784 unsigned IntValue,
785 StringRef StringValue) {
786 setAttributeItems(Attribute, IntValue, StringValue,
787 /* OverwriteExisting= */ true);
788 }
789
emitArch(ARM::ArchKind Value)790 void ARMTargetELFStreamer::emitArch(ARM::ArchKind Value) {
791 Arch = Value;
792 }
793
emitObjectArch(ARM::ArchKind Value)794 void ARMTargetELFStreamer::emitObjectArch(ARM::ArchKind Value) {
795 EmittedArch = Value;
796 }
797
emitArchDefaultAttributes()798 void ARMTargetELFStreamer::emitArchDefaultAttributes() {
799 using namespace ARMBuildAttrs;
800
801 setAttributeItem(CPU_name,
802 ARM::getCPUAttr(Arch),
803 false);
804
805 if (EmittedArch == ARM::ArchKind::INVALID)
806 setAttributeItem(CPU_arch,
807 ARM::getArchAttr(Arch),
808 false);
809 else
810 setAttributeItem(CPU_arch,
811 ARM::getArchAttr(EmittedArch),
812 false);
813
814 switch (Arch) {
815 case ARM::ArchKind::ARMV2:
816 case ARM::ArchKind::ARMV2A:
817 case ARM::ArchKind::ARMV3:
818 case ARM::ArchKind::ARMV3M:
819 case ARM::ArchKind::ARMV4:
820 setAttributeItem(ARM_ISA_use, Allowed, false);
821 break;
822
823 case ARM::ArchKind::ARMV4T:
824 case ARM::ArchKind::ARMV5T:
825 case ARM::ArchKind::ARMV5TE:
826 case ARM::ArchKind::ARMV6:
827 setAttributeItem(ARM_ISA_use, Allowed, false);
828 setAttributeItem(THUMB_ISA_use, Allowed, false);
829 break;
830
831 case ARM::ArchKind::ARMV6T2:
832 setAttributeItem(ARM_ISA_use, Allowed, false);
833 setAttributeItem(THUMB_ISA_use, AllowThumb32, false);
834 break;
835
836 case ARM::ArchKind::ARMV6K:
837 case ARM::ArchKind::ARMV6KZ:
838 setAttributeItem(ARM_ISA_use, Allowed, false);
839 setAttributeItem(THUMB_ISA_use, Allowed, false);
840 setAttributeItem(Virtualization_use, AllowTZ, false);
841 break;
842
843 case ARM::ArchKind::ARMV6M:
844 setAttributeItem(THUMB_ISA_use, Allowed, false);
845 break;
846
847 case ARM::ArchKind::ARMV7A:
848 setAttributeItem(CPU_arch_profile, ApplicationProfile, false);
849 setAttributeItem(ARM_ISA_use, Allowed, false);
850 setAttributeItem(THUMB_ISA_use, AllowThumb32, false);
851 break;
852
853 case ARM::ArchKind::ARMV7R:
854 setAttributeItem(CPU_arch_profile, RealTimeProfile, false);
855 setAttributeItem(ARM_ISA_use, Allowed, false);
856 setAttributeItem(THUMB_ISA_use, AllowThumb32, false);
857 break;
858
859 case ARM::ArchKind::ARMV7EM:
860 case ARM::ArchKind::ARMV7M:
861 setAttributeItem(CPU_arch_profile, MicroControllerProfile, false);
862 setAttributeItem(THUMB_ISA_use, AllowThumb32, false);
863 break;
864
865 case ARM::ArchKind::ARMV8A:
866 case ARM::ArchKind::ARMV8_1A:
867 case ARM::ArchKind::ARMV8_2A:
868 case ARM::ArchKind::ARMV8_3A:
869 case ARM::ArchKind::ARMV8_4A:
870 case ARM::ArchKind::ARMV8_5A:
871 setAttributeItem(CPU_arch_profile, ApplicationProfile, false);
872 setAttributeItem(ARM_ISA_use, Allowed, false);
873 setAttributeItem(THUMB_ISA_use, AllowThumb32, false);
874 setAttributeItem(MPextension_use, Allowed, false);
875 setAttributeItem(Virtualization_use, AllowTZVirtualization, false);
876 break;
877
878 case ARM::ArchKind::ARMV8MBaseline:
879 case ARM::ArchKind::ARMV8MMainline:
880 setAttributeItem(THUMB_ISA_use, AllowThumbDerived, false);
881 setAttributeItem(CPU_arch_profile, MicroControllerProfile, false);
882 break;
883
884 case ARM::ArchKind::IWMMXT:
885 setAttributeItem(ARM_ISA_use, Allowed, false);
886 setAttributeItem(THUMB_ISA_use, Allowed, false);
887 setAttributeItem(WMMX_arch, AllowWMMXv1, false);
888 break;
889
890 case ARM::ArchKind::IWMMXT2:
891 setAttributeItem(ARM_ISA_use, Allowed, false);
892 setAttributeItem(THUMB_ISA_use, Allowed, false);
893 setAttributeItem(WMMX_arch, AllowWMMXv2, false);
894 break;
895
896 default:
897 report_fatal_error("Unknown Arch: " + Twine(ARM::getArchName(Arch)));
898 break;
899 }
900 }
901
emitFPU(unsigned Value)902 void ARMTargetELFStreamer::emitFPU(unsigned Value) {
903 FPU = Value;
904 }
905
emitFPUDefaultAttributes()906 void ARMTargetELFStreamer::emitFPUDefaultAttributes() {
907 switch (FPU) {
908 case ARM::FK_VFP:
909 case ARM::FK_VFPV2:
910 setAttributeItem(ARMBuildAttrs::FP_arch,
911 ARMBuildAttrs::AllowFPv2,
912 /* OverwriteExisting= */ false);
913 break;
914
915 case ARM::FK_VFPV3:
916 setAttributeItem(ARMBuildAttrs::FP_arch,
917 ARMBuildAttrs::AllowFPv3A,
918 /* OverwriteExisting= */ false);
919 break;
920
921 case ARM::FK_VFPV3_FP16:
922 setAttributeItem(ARMBuildAttrs::FP_arch,
923 ARMBuildAttrs::AllowFPv3A,
924 /* OverwriteExisting= */ false);
925 setAttributeItem(ARMBuildAttrs::FP_HP_extension,
926 ARMBuildAttrs::AllowHPFP,
927 /* OverwriteExisting= */ false);
928 break;
929
930 case ARM::FK_VFPV3_D16:
931 setAttributeItem(ARMBuildAttrs::FP_arch,
932 ARMBuildAttrs::AllowFPv3B,
933 /* OverwriteExisting= */ false);
934 break;
935
936 case ARM::FK_VFPV3_D16_FP16:
937 setAttributeItem(ARMBuildAttrs::FP_arch,
938 ARMBuildAttrs::AllowFPv3B,
939 /* OverwriteExisting= */ false);
940 setAttributeItem(ARMBuildAttrs::FP_HP_extension,
941 ARMBuildAttrs::AllowHPFP,
942 /* OverwriteExisting= */ false);
943 break;
944
945 case ARM::FK_VFPV3XD:
946 setAttributeItem(ARMBuildAttrs::FP_arch,
947 ARMBuildAttrs::AllowFPv3B,
948 /* OverwriteExisting= */ false);
949 break;
950 case ARM::FK_VFPV3XD_FP16:
951 setAttributeItem(ARMBuildAttrs::FP_arch,
952 ARMBuildAttrs::AllowFPv3B,
953 /* OverwriteExisting= */ false);
954 setAttributeItem(ARMBuildAttrs::FP_HP_extension,
955 ARMBuildAttrs::AllowHPFP,
956 /* OverwriteExisting= */ false);
957 break;
958
959 case ARM::FK_VFPV4:
960 setAttributeItem(ARMBuildAttrs::FP_arch,
961 ARMBuildAttrs::AllowFPv4A,
962 /* OverwriteExisting= */ false);
963 break;
964
965 // ABI_HardFP_use is handled in ARMAsmPrinter, so _SP_D16 is treated the same
966 // as _D16 here.
967 case ARM::FK_FPV4_SP_D16:
968 case ARM::FK_VFPV4_D16:
969 setAttributeItem(ARMBuildAttrs::FP_arch,
970 ARMBuildAttrs::AllowFPv4B,
971 /* OverwriteExisting= */ false);
972 break;
973
974 case ARM::FK_FP_ARMV8:
975 setAttributeItem(ARMBuildAttrs::FP_arch,
976 ARMBuildAttrs::AllowFPARMv8A,
977 /* OverwriteExisting= */ false);
978 break;
979
980 // FPV5_D16 is identical to FP_ARMV8 except for the number of D registers, so
981 // uses the FP_ARMV8_D16 build attribute.
982 case ARM::FK_FPV5_SP_D16:
983 case ARM::FK_FPV5_D16:
984 setAttributeItem(ARMBuildAttrs::FP_arch,
985 ARMBuildAttrs::AllowFPARMv8B,
986 /* OverwriteExisting= */ false);
987 break;
988
989 case ARM::FK_NEON:
990 setAttributeItem(ARMBuildAttrs::FP_arch,
991 ARMBuildAttrs::AllowFPv3A,
992 /* OverwriteExisting= */ false);
993 setAttributeItem(ARMBuildAttrs::Advanced_SIMD_arch,
994 ARMBuildAttrs::AllowNeon,
995 /* OverwriteExisting= */ false);
996 break;
997
998 case ARM::FK_NEON_FP16:
999 setAttributeItem(ARMBuildAttrs::FP_arch,
1000 ARMBuildAttrs::AllowFPv3A,
1001 /* OverwriteExisting= */ false);
1002 setAttributeItem(ARMBuildAttrs::Advanced_SIMD_arch,
1003 ARMBuildAttrs::AllowNeon,
1004 /* OverwriteExisting= */ false);
1005 setAttributeItem(ARMBuildAttrs::FP_HP_extension,
1006 ARMBuildAttrs::AllowHPFP,
1007 /* OverwriteExisting= */ false);
1008 break;
1009
1010 case ARM::FK_NEON_VFPV4:
1011 setAttributeItem(ARMBuildAttrs::FP_arch,
1012 ARMBuildAttrs::AllowFPv4A,
1013 /* OverwriteExisting= */ false);
1014 setAttributeItem(ARMBuildAttrs::Advanced_SIMD_arch,
1015 ARMBuildAttrs::AllowNeon2,
1016 /* OverwriteExisting= */ false);
1017 break;
1018
1019 case ARM::FK_NEON_FP_ARMV8:
1020 case ARM::FK_CRYPTO_NEON_FP_ARMV8:
1021 setAttributeItem(ARMBuildAttrs::FP_arch,
1022 ARMBuildAttrs::AllowFPARMv8A,
1023 /* OverwriteExisting= */ false);
1024 // 'Advanced_SIMD_arch' must be emitted not here, but within
1025 // ARMAsmPrinter::emitAttributes(), depending on hasV8Ops() and hasV8_1a()
1026 break;
1027
1028 case ARM::FK_SOFTVFP:
1029 case ARM::FK_NONE:
1030 break;
1031
1032 default:
1033 report_fatal_error("Unknown FPU: " + Twine(FPU));
1034 break;
1035 }
1036 }
1037
calculateContentSize() const1038 size_t ARMTargetELFStreamer::calculateContentSize() const {
1039 size_t Result = 0;
1040 for (size_t i = 0; i < Contents.size(); ++i) {
1041 AttributeItem item = Contents[i];
1042 switch (item.Type) {
1043 case AttributeItem::HiddenAttribute:
1044 break;
1045 case AttributeItem::NumericAttribute:
1046 Result += getULEB128Size(item.Tag);
1047 Result += getULEB128Size(item.IntValue);
1048 break;
1049 case AttributeItem::TextAttribute:
1050 Result += getULEB128Size(item.Tag);
1051 Result += item.StringValue.size() + 1; // string + '\0'
1052 break;
1053 case AttributeItem::NumericAndTextAttributes:
1054 Result += getULEB128Size(item.Tag);
1055 Result += getULEB128Size(item.IntValue);
1056 Result += item.StringValue.size() + 1; // string + '\0';
1057 break;
1058 }
1059 }
1060 return Result;
1061 }
1062
finishAttributeSection()1063 void ARMTargetELFStreamer::finishAttributeSection() {
1064 // <format-version>
1065 // [ <section-length> "vendor-name"
1066 // [ <file-tag> <size> <attribute>*
1067 // | <section-tag> <size> <section-number>* 0 <attribute>*
1068 // | <symbol-tag> <size> <symbol-number>* 0 <attribute>*
1069 // ]+
1070 // ]*
1071
1072 if (FPU != ARM::FK_INVALID)
1073 emitFPUDefaultAttributes();
1074
1075 if (Arch != ARM::ArchKind::INVALID)
1076 emitArchDefaultAttributes();
1077
1078 if (Contents.empty())
1079 return;
1080
1081 llvm::sort(Contents, AttributeItem::LessTag);
1082
1083 ARMELFStreamer &Streamer = getStreamer();
1084
1085 // Switch to .ARM.attributes section
1086 if (AttributeSection) {
1087 Streamer.SwitchSection(AttributeSection);
1088 } else {
1089 AttributeSection = Streamer.getContext().getELFSection(
1090 ".ARM.attributes", ELF::SHT_ARM_ATTRIBUTES, 0);
1091 Streamer.SwitchSection(AttributeSection);
1092
1093 // Format version
1094 Streamer.EmitIntValue(0x41, 1);
1095 }
1096
1097 // Vendor size + Vendor name + '\0'
1098 const size_t VendorHeaderSize = 4 + CurrentVendor.size() + 1;
1099
1100 // Tag + Tag Size
1101 const size_t TagHeaderSize = 1 + 4;
1102
1103 const size_t ContentsSize = calculateContentSize();
1104
1105 Streamer.EmitIntValue(VendorHeaderSize + TagHeaderSize + ContentsSize, 4);
1106 Streamer.EmitBytes(CurrentVendor);
1107 Streamer.EmitIntValue(0, 1); // '\0'
1108
1109 Streamer.EmitIntValue(ARMBuildAttrs::File, 1);
1110 Streamer.EmitIntValue(TagHeaderSize + ContentsSize, 4);
1111
1112 // Size should have been accounted for already, now
1113 // emit each field as its type (ULEB or String)
1114 for (size_t i = 0; i < Contents.size(); ++i) {
1115 AttributeItem item = Contents[i];
1116 Streamer.EmitULEB128IntValue(item.Tag);
1117 switch (item.Type) {
1118 default: llvm_unreachable("Invalid attribute type");
1119 case AttributeItem::NumericAttribute:
1120 Streamer.EmitULEB128IntValue(item.IntValue);
1121 break;
1122 case AttributeItem::TextAttribute:
1123 Streamer.EmitBytes(item.StringValue);
1124 Streamer.EmitIntValue(0, 1); // '\0'
1125 break;
1126 case AttributeItem::NumericAndTextAttributes:
1127 Streamer.EmitULEB128IntValue(item.IntValue);
1128 Streamer.EmitBytes(item.StringValue);
1129 Streamer.EmitIntValue(0, 1); // '\0'
1130 break;
1131 }
1132 }
1133
1134 Contents.clear();
1135 FPU = ARM::FK_INVALID;
1136 }
1137
emitLabel(MCSymbol * Symbol)1138 void ARMTargetELFStreamer::emitLabel(MCSymbol *Symbol) {
1139 ARMELFStreamer &Streamer = getStreamer();
1140 if (!Streamer.IsThumb)
1141 return;
1142
1143 Streamer.getAssembler().registerSymbol(*Symbol);
1144 unsigned Type = cast<MCSymbolELF>(Symbol)->getType();
1145 if (Type == ELF::STT_FUNC || Type == ELF::STT_GNU_IFUNC)
1146 Streamer.EmitThumbFunc(Symbol);
1147 }
1148
1149 void
AnnotateTLSDescriptorSequence(const MCSymbolRefExpr * S)1150 ARMTargetELFStreamer::AnnotateTLSDescriptorSequence(const MCSymbolRefExpr *S) {
1151 getStreamer().EmitFixup(S, FK_Data_4);
1152 }
1153
emitThumbSet(MCSymbol * Symbol,const MCExpr * Value)1154 void ARMTargetELFStreamer::emitThumbSet(MCSymbol *Symbol, const MCExpr *Value) {
1155 if (const MCSymbolRefExpr *SRE = dyn_cast<MCSymbolRefExpr>(Value)) {
1156 const MCSymbol &Sym = SRE->getSymbol();
1157 if (!Sym.isDefined()) {
1158 getStreamer().EmitAssignment(Symbol, Value);
1159 return;
1160 }
1161 }
1162
1163 getStreamer().EmitThumbFunc(Symbol);
1164 getStreamer().EmitAssignment(Symbol, Value);
1165 }
1166
emitInst(uint32_t Inst,char Suffix)1167 void ARMTargetELFStreamer::emitInst(uint32_t Inst, char Suffix) {
1168 getStreamer().emitInst(Inst, Suffix);
1169 }
1170
reset()1171 void ARMTargetELFStreamer::reset() { AttributeSection = nullptr; }
1172
FinishImpl()1173 void ARMELFStreamer::FinishImpl() {
1174 MCTargetStreamer &TS = *getTargetStreamer();
1175 ARMTargetStreamer &ATS = static_cast<ARMTargetStreamer &>(TS);
1176 ATS.finishAttributeSection();
1177
1178 MCELFStreamer::FinishImpl();
1179 }
1180
reset()1181 void ARMELFStreamer::reset() {
1182 MCTargetStreamer &TS = *getTargetStreamer();
1183 ARMTargetStreamer &ATS = static_cast<ARMTargetStreamer &>(TS);
1184 ATS.reset();
1185 MappingSymbolCounter = 0;
1186 MCELFStreamer::reset();
1187 LastMappingSymbols.clear();
1188 LastEMSInfo.reset();
1189 // MCELFStreamer clear's the assembler's e_flags. However, for
1190 // arm we manually set the ABI version on streamer creation, so
1191 // do the same here
1192 getAssembler().setELFHeaderEFlags(ELF::EF_ARM_EABI_VER5);
1193 }
1194
SwitchToEHSection(StringRef Prefix,unsigned Type,unsigned Flags,SectionKind Kind,const MCSymbol & Fn)1195 inline void ARMELFStreamer::SwitchToEHSection(StringRef Prefix,
1196 unsigned Type,
1197 unsigned Flags,
1198 SectionKind Kind,
1199 const MCSymbol &Fn) {
1200 const MCSectionELF &FnSection =
1201 static_cast<const MCSectionELF &>(Fn.getSection());
1202
1203 // Create the name for new section
1204 StringRef FnSecName(FnSection.getSectionName());
1205 SmallString<128> EHSecName(Prefix);
1206 if (FnSecName != ".text") {
1207 EHSecName += FnSecName;
1208 }
1209
1210 // Get .ARM.extab or .ARM.exidx section
1211 const MCSymbolELF *Group = FnSection.getGroup();
1212 if (Group)
1213 Flags |= ELF::SHF_GROUP;
1214 MCSectionELF *EHSection = getContext().getELFSection(
1215 EHSecName, Type, Flags, 0, Group, FnSection.getUniqueID(),
1216 static_cast<const MCSymbolELF *>(&Fn));
1217
1218 assert(EHSection && "Failed to get the required EH section");
1219
1220 // Switch to .ARM.extab or .ARM.exidx section
1221 SwitchSection(EHSection);
1222 EmitCodeAlignment(4);
1223 }
1224
SwitchToExTabSection(const MCSymbol & FnStart)1225 inline void ARMELFStreamer::SwitchToExTabSection(const MCSymbol &FnStart) {
1226 SwitchToEHSection(".ARM.extab", ELF::SHT_PROGBITS, ELF::SHF_ALLOC,
1227 SectionKind::getData(), FnStart);
1228 }
1229
SwitchToExIdxSection(const MCSymbol & FnStart)1230 inline void ARMELFStreamer::SwitchToExIdxSection(const MCSymbol &FnStart) {
1231 SwitchToEHSection(".ARM.exidx", ELF::SHT_ARM_EXIDX,
1232 ELF::SHF_ALLOC | ELF::SHF_LINK_ORDER,
1233 SectionKind::getData(), FnStart);
1234 }
1235
EmitFixup(const MCExpr * Expr,MCFixupKind Kind)1236 void ARMELFStreamer::EmitFixup(const MCExpr *Expr, MCFixupKind Kind) {
1237 MCDataFragment *Frag = getOrCreateDataFragment();
1238 Frag->getFixups().push_back(MCFixup::create(Frag->getContents().size(), Expr,
1239 Kind));
1240 }
1241
EHReset()1242 void ARMELFStreamer::EHReset() {
1243 ExTab = nullptr;
1244 FnStart = nullptr;
1245 Personality = nullptr;
1246 PersonalityIndex = ARM::EHABI::NUM_PERSONALITY_INDEX;
1247 FPReg = ARM::SP;
1248 FPOffset = 0;
1249 SPOffset = 0;
1250 PendingOffset = 0;
1251 UsedFP = false;
1252 CantUnwind = false;
1253
1254 Opcodes.clear();
1255 UnwindOpAsm.Reset();
1256 }
1257
emitFnStart()1258 void ARMELFStreamer::emitFnStart() {
1259 assert(FnStart == nullptr);
1260 FnStart = getContext().createTempSymbol();
1261 EmitLabel(FnStart);
1262 }
1263
emitFnEnd()1264 void ARMELFStreamer::emitFnEnd() {
1265 assert(FnStart && ".fnstart must precedes .fnend");
1266
1267 // Emit unwind opcodes if there is no .handlerdata directive
1268 if (!ExTab && !CantUnwind)
1269 FlushUnwindOpcodes(true);
1270
1271 // Emit the exception index table entry
1272 SwitchToExIdxSection(*FnStart);
1273
1274 // The EHABI requires a dependency preserving R_ARM_NONE relocation to the
1275 // personality routine to protect it from an arbitrary platform's static
1276 // linker garbage collection. We disable this for Android where the unwinder
1277 // is either dynamically linked or directly references the personality
1278 // routine.
1279 if (PersonalityIndex < ARM::EHABI::NUM_PERSONALITY_INDEX && !IsAndroid)
1280 EmitPersonalityFixup(GetAEABIUnwindPersonalityName(PersonalityIndex));
1281
1282 const MCSymbolRefExpr *FnStartRef =
1283 MCSymbolRefExpr::create(FnStart,
1284 MCSymbolRefExpr::VK_ARM_PREL31,
1285 getContext());
1286
1287 EmitValue(FnStartRef, 4);
1288
1289 if (CantUnwind) {
1290 EmitIntValue(ARM::EHABI::EXIDX_CANTUNWIND, 4);
1291 } else if (ExTab) {
1292 // Emit a reference to the unwind opcodes in the ".ARM.extab" section.
1293 const MCSymbolRefExpr *ExTabEntryRef =
1294 MCSymbolRefExpr::create(ExTab,
1295 MCSymbolRefExpr::VK_ARM_PREL31,
1296 getContext());
1297 EmitValue(ExTabEntryRef, 4);
1298 } else {
1299 // For the __aeabi_unwind_cpp_pr0, we have to emit the unwind opcodes in
1300 // the second word of exception index table entry. The size of the unwind
1301 // opcodes should always be 4 bytes.
1302 assert(PersonalityIndex == ARM::EHABI::AEABI_UNWIND_CPP_PR0 &&
1303 "Compact model must use __aeabi_unwind_cpp_pr0 as personality");
1304 assert(Opcodes.size() == 4u &&
1305 "Unwind opcode size for __aeabi_unwind_cpp_pr0 must be equal to 4");
1306 uint64_t Intval = Opcodes[0] |
1307 Opcodes[1] << 8 |
1308 Opcodes[2] << 16 |
1309 Opcodes[3] << 24;
1310 EmitIntValue(Intval, Opcodes.size());
1311 }
1312
1313 // Switch to the section containing FnStart
1314 SwitchSection(&FnStart->getSection());
1315
1316 // Clean exception handling frame information
1317 EHReset();
1318 }
1319
emitCantUnwind()1320 void ARMELFStreamer::emitCantUnwind() { CantUnwind = true; }
1321
1322 // Add the R_ARM_NONE fixup at the same position
EmitPersonalityFixup(StringRef Name)1323 void ARMELFStreamer::EmitPersonalityFixup(StringRef Name) {
1324 const MCSymbol *PersonalitySym = getContext().getOrCreateSymbol(Name);
1325
1326 const MCSymbolRefExpr *PersonalityRef = MCSymbolRefExpr::create(
1327 PersonalitySym, MCSymbolRefExpr::VK_ARM_NONE, getContext());
1328
1329 visitUsedExpr(*PersonalityRef);
1330 MCDataFragment *DF = getOrCreateDataFragment();
1331 DF->getFixups().push_back(MCFixup::create(DF->getContents().size(),
1332 PersonalityRef,
1333 MCFixup::getKindForSize(4, false)));
1334 }
1335
FlushPendingOffset()1336 void ARMELFStreamer::FlushPendingOffset() {
1337 if (PendingOffset != 0) {
1338 UnwindOpAsm.EmitSPOffset(-PendingOffset);
1339 PendingOffset = 0;
1340 }
1341 }
1342
FlushUnwindOpcodes(bool NoHandlerData)1343 void ARMELFStreamer::FlushUnwindOpcodes(bool NoHandlerData) {
1344 // Emit the unwind opcode to restore $sp.
1345 if (UsedFP) {
1346 const MCRegisterInfo *MRI = getContext().getRegisterInfo();
1347 int64_t LastRegSaveSPOffset = SPOffset - PendingOffset;
1348 UnwindOpAsm.EmitSPOffset(LastRegSaveSPOffset - FPOffset);
1349 UnwindOpAsm.EmitSetSP(MRI->getEncodingValue(FPReg));
1350 } else {
1351 FlushPendingOffset();
1352 }
1353
1354 // Finalize the unwind opcode sequence
1355 UnwindOpAsm.Finalize(PersonalityIndex, Opcodes);
1356
1357 // For compact model 0, we have to emit the unwind opcodes in the .ARM.exidx
1358 // section. Thus, we don't have to create an entry in the .ARM.extab
1359 // section.
1360 if (NoHandlerData && PersonalityIndex == ARM::EHABI::AEABI_UNWIND_CPP_PR0)
1361 return;
1362
1363 // Switch to .ARM.extab section.
1364 SwitchToExTabSection(*FnStart);
1365
1366 // Create .ARM.extab label for offset in .ARM.exidx
1367 assert(!ExTab);
1368 ExTab = getContext().createTempSymbol();
1369 EmitLabel(ExTab);
1370
1371 // Emit personality
1372 if (Personality) {
1373 const MCSymbolRefExpr *PersonalityRef =
1374 MCSymbolRefExpr::create(Personality,
1375 MCSymbolRefExpr::VK_ARM_PREL31,
1376 getContext());
1377
1378 EmitValue(PersonalityRef, 4);
1379 }
1380
1381 // Emit unwind opcodes
1382 assert((Opcodes.size() % 4) == 0 &&
1383 "Unwind opcode size for __aeabi_cpp_unwind_pr0 must be multiple of 4");
1384 for (unsigned I = 0; I != Opcodes.size(); I += 4) {
1385 uint64_t Intval = Opcodes[I] |
1386 Opcodes[I + 1] << 8 |
1387 Opcodes[I + 2] << 16 |
1388 Opcodes[I + 3] << 24;
1389 EmitIntValue(Intval, 4);
1390 }
1391
1392 // According to ARM EHABI section 9.2, if the __aeabi_unwind_cpp_pr1() or
1393 // __aeabi_unwind_cpp_pr2() is used, then the handler data must be emitted
1394 // after the unwind opcodes. The handler data consists of several 32-bit
1395 // words, and should be terminated by zero.
1396 //
1397 // In case that the .handlerdata directive is not specified by the
1398 // programmer, we should emit zero to terminate the handler data.
1399 if (NoHandlerData && !Personality)
1400 EmitIntValue(0, 4);
1401 }
1402
emitHandlerData()1403 void ARMELFStreamer::emitHandlerData() { FlushUnwindOpcodes(false); }
1404
emitPersonality(const MCSymbol * Per)1405 void ARMELFStreamer::emitPersonality(const MCSymbol *Per) {
1406 Personality = Per;
1407 UnwindOpAsm.setPersonality(Per);
1408 }
1409
emitPersonalityIndex(unsigned Index)1410 void ARMELFStreamer::emitPersonalityIndex(unsigned Index) {
1411 assert(Index < ARM::EHABI::NUM_PERSONALITY_INDEX && "invalid index");
1412 PersonalityIndex = Index;
1413 }
1414
emitSetFP(unsigned NewFPReg,unsigned NewSPReg,int64_t Offset)1415 void ARMELFStreamer::emitSetFP(unsigned NewFPReg, unsigned NewSPReg,
1416 int64_t Offset) {
1417 assert((NewSPReg == ARM::SP || NewSPReg == FPReg) &&
1418 "the operand of .setfp directive should be either $sp or $fp");
1419
1420 UsedFP = true;
1421 FPReg = NewFPReg;
1422
1423 if (NewSPReg == ARM::SP)
1424 FPOffset = SPOffset + Offset;
1425 else
1426 FPOffset += Offset;
1427 }
1428
emitMovSP(unsigned Reg,int64_t Offset)1429 void ARMELFStreamer::emitMovSP(unsigned Reg, int64_t Offset) {
1430 assert((Reg != ARM::SP && Reg != ARM::PC) &&
1431 "the operand of .movsp cannot be either sp or pc");
1432 assert(FPReg == ARM::SP && "current FP must be SP");
1433
1434 FlushPendingOffset();
1435
1436 FPReg = Reg;
1437 FPOffset = SPOffset + Offset;
1438
1439 const MCRegisterInfo *MRI = getContext().getRegisterInfo();
1440 UnwindOpAsm.EmitSetSP(MRI->getEncodingValue(FPReg));
1441 }
1442
emitPad(int64_t Offset)1443 void ARMELFStreamer::emitPad(int64_t Offset) {
1444 // Track the change of the $sp offset
1445 SPOffset -= Offset;
1446
1447 // To squash multiple .pad directives, we should delay the unwind opcode
1448 // until the .save, .vsave, .handlerdata, or .fnend directives.
1449 PendingOffset -= Offset;
1450 }
1451
emitRegSave(const SmallVectorImpl<unsigned> & RegList,bool IsVector)1452 void ARMELFStreamer::emitRegSave(const SmallVectorImpl<unsigned> &RegList,
1453 bool IsVector) {
1454 // Collect the registers in the register list
1455 unsigned Count = 0;
1456 uint32_t Mask = 0;
1457 const MCRegisterInfo *MRI = getContext().getRegisterInfo();
1458 for (size_t i = 0; i < RegList.size(); ++i) {
1459 unsigned Reg = MRI->getEncodingValue(RegList[i]);
1460 assert(Reg < (IsVector ? 32U : 16U) && "Register out of range");
1461 unsigned Bit = (1u << Reg);
1462 if ((Mask & Bit) == 0) {
1463 Mask |= Bit;
1464 ++Count;
1465 }
1466 }
1467
1468 // Track the change the $sp offset: For the .save directive, the
1469 // corresponding push instruction will decrease the $sp by (4 * Count).
1470 // For the .vsave directive, the corresponding vpush instruction will
1471 // decrease $sp by (8 * Count).
1472 SPOffset -= Count * (IsVector ? 8 : 4);
1473
1474 // Emit the opcode
1475 FlushPendingOffset();
1476 if (IsVector)
1477 UnwindOpAsm.EmitVFPRegSave(Mask);
1478 else
1479 UnwindOpAsm.EmitRegSave(Mask);
1480 }
1481
emitUnwindRaw(int64_t Offset,const SmallVectorImpl<uint8_t> & Opcodes)1482 void ARMELFStreamer::emitUnwindRaw(int64_t Offset,
1483 const SmallVectorImpl<uint8_t> &Opcodes) {
1484 FlushPendingOffset();
1485 SPOffset = SPOffset - Offset;
1486 UnwindOpAsm.EmitRaw(Opcodes);
1487 }
1488
1489 namespace llvm {
1490
createARMTargetAsmStreamer(MCStreamer & S,formatted_raw_ostream & OS,MCInstPrinter * InstPrint,bool isVerboseAsm)1491 MCTargetStreamer *createARMTargetAsmStreamer(MCStreamer &S,
1492 formatted_raw_ostream &OS,
1493 MCInstPrinter *InstPrint,
1494 bool isVerboseAsm) {
1495 return new ARMTargetAsmStreamer(S, OS, *InstPrint, isVerboseAsm);
1496 }
1497
createARMNullTargetStreamer(MCStreamer & S)1498 MCTargetStreamer *createARMNullTargetStreamer(MCStreamer &S) {
1499 return new ARMTargetStreamer(S);
1500 }
1501
createARMObjectTargetStreamer(MCStreamer & S,const MCSubtargetInfo & STI)1502 MCTargetStreamer *createARMObjectTargetStreamer(MCStreamer &S,
1503 const MCSubtargetInfo &STI) {
1504 const Triple &TT = STI.getTargetTriple();
1505 if (TT.isOSBinFormatELF())
1506 return new ARMTargetELFStreamer(S);
1507 return new ARMTargetStreamer(S);
1508 }
1509
createARMELFStreamer(MCContext & Context,std::unique_ptr<MCAsmBackend> TAB,std::unique_ptr<MCObjectWriter> OW,std::unique_ptr<MCCodeEmitter> Emitter,bool RelaxAll,bool IsThumb,bool IsAndroid)1510 MCELFStreamer *createARMELFStreamer(MCContext &Context,
1511 std::unique_ptr<MCAsmBackend> TAB,
1512 std::unique_ptr<MCObjectWriter> OW,
1513 std::unique_ptr<MCCodeEmitter> Emitter,
1514 bool RelaxAll, bool IsThumb,
1515 bool IsAndroid) {
1516 ARMELFStreamer *S =
1517 new ARMELFStreamer(Context, std::move(TAB), std::move(OW),
1518 std::move(Emitter), IsThumb, IsAndroid);
1519 // FIXME: This should eventually end up somewhere else where more
1520 // intelligent flag decisions can be made. For now we are just maintaining
1521 // the status quo for ARM and setting EF_ARM_EABI_VER5 as the default.
1522 S->getAssembler().setELFHeaderEFlags(ELF::EF_ARM_EABI_VER5);
1523
1524 if (RelaxAll)
1525 S->getAssembler().setRelaxAll(true);
1526 return S;
1527 }
1528
1529 } // end namespace llvm
1530