blob: a15b1ad808ed882fbaf76b2364d01a9022760825 [file] [log] [blame]
Tim Northover5cc3dc82012-12-07 16:50:23 +00001//===- 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
Logan Chiend8bb4b72013-04-16 12:02:21 +000016#include "ARMRegisterInfo.h"
Logan Chiend8bb4b72013-04-16 12:02:21 +000017#include "ARMUnwindOpAsm.h"
Saleem Abdulrasoolc0da2cb2013-12-19 05:17:58 +000018#include "llvm/ADT/StringExtras.h"
Benjamin Kramerf242d8c2012-12-08 10:45:24 +000019#include "llvm/ADT/Twine.h"
20#include "llvm/MC/MCAsmBackend.h"
Saleem Abdulrasoolc0da2cb2013-12-19 05:17:58 +000021#include "llvm/MC/MCAsmInfo.h"
Tim Northover5cc3dc82012-12-07 16:50:23 +000022#include "llvm/MC/MCAssembler.h"
23#include "llvm/MC/MCCodeEmitter.h"
24#include "llvm/MC/MCContext.h"
Tim Northover5cc3dc82012-12-07 16:50:23 +000025#include "llvm/MC/MCELFStreamer.h"
26#include "llvm/MC/MCELFSymbolFlags.h"
27#include "llvm/MC/MCExpr.h"
28#include "llvm/MC/MCInst.h"
Rafael Espindolaa17151a2013-10-08 13:08:17 +000029#include "llvm/MC/MCInstPrinter.h"
Rafael Espindola4c6f6132014-04-27 17:10:46 +000030#include "llvm/MC/MCObjectFileInfo.h"
Tim Northover5cc3dc82012-12-07 16:50:23 +000031#include "llvm/MC/MCObjectStreamer.h"
Logan Chiend8bb4b72013-04-16 12:02:21 +000032#include "llvm/MC/MCRegisterInfo.h"
Tim Northover5cc3dc82012-12-07 16:50:23 +000033#include "llvm/MC/MCSection.h"
Benjamin Kramerf242d8c2012-12-08 10:45:24 +000034#include "llvm/MC/MCSectionELF.h"
35#include "llvm/MC/MCStreamer.h"
Rafael Espindola95fb9b92015-06-02 20:38:46 +000036#include "llvm/MC/MCSymbolELF.h"
Tim Northover5cc3dc82012-12-07 16:50:23 +000037#include "llvm/MC/MCValue.h"
Saleem Abdulrasool278a9f42014-01-19 08:25:27 +000038#include "llvm/Support/ARMBuildAttributes.h"
Saleem Abdulrasoolb961c992014-01-06 00:15:00 +000039#include "llvm/Support/ARMEHABI.h"
Renato Golinf5f373f2015-05-08 21:04:27 +000040#include "llvm/Support/TargetParser.h"
Tim Northover5cc3dc82012-12-07 16:50:23 +000041#include "llvm/Support/Debug.h"
42#include "llvm/Support/ELF.h"
Rafael Espindolaa17151a2013-10-08 13:08:17 +000043#include "llvm/Support/FormattedStream.h"
Logan Chien5b776b72014-02-22 14:00:39 +000044#include "llvm/Support/LEB128.h"
Tim Northover5cc3dc82012-12-07 16:50:23 +000045#include "llvm/Support/raw_ostream.h"
Logan Chien8cbb80d2013-10-28 17:51:12 +000046#include <algorithm>
Tim Northover5cc3dc82012-12-07 16:50:23 +000047
48using namespace llvm;
49
Logan Chiend8bb4b72013-04-16 12:02:21 +000050static std::string GetAEABIUnwindPersonalityName(unsigned Index) {
Saleem Abdulrasoolb961c992014-01-06 00:15:00 +000051 assert(Index < ARM::EHABI::NUM_PERSONALITY_INDEX &&
52 "Invalid personality index");
Logan Chiend8bb4b72013-04-16 12:02:21 +000053 return (Twine("__aeabi_unwind_cpp_pr") + Twine(Index)).str();
54}
55
Tim Northover5cc3dc82012-12-07 16:50:23 +000056namespace {
57
Rafael Espindolaa17151a2013-10-08 13:08:17 +000058class ARMELFStreamer;
59
60class ARMTargetAsmStreamer : public ARMTargetStreamer {
61 formatted_raw_ostream &OS;
62 MCInstPrinter &InstPrinter;
Saleem Abdulrasoolf16e68a2014-01-07 02:28:50 +000063 bool IsVerboseAsm;
Rafael Espindolaa17151a2013-10-08 13:08:17 +000064
Craig Topperca7e3e52014-03-10 03:19:03 +000065 void emitFnStart() override;
66 void emitFnEnd() override;
67 void emitCantUnwind() override;
68 void emitPersonality(const MCSymbol *Personality) override;
69 void emitPersonalityIndex(unsigned Index) override;
70 void emitHandlerData() override;
71 void emitSetFP(unsigned FpReg, unsigned SpReg, int64_t Offset = 0) override;
72 void emitMovSP(unsigned Reg, int64_t Offset = 0) override;
73 void emitPad(int64_t Offset) override;
74 void emitRegSave(const SmallVectorImpl<unsigned> &RegList,
75 bool isVector) override;
76 void emitUnwindRaw(int64_t Offset,
77 const SmallVectorImpl<uint8_t> &Opcodes) override;
Rafael Espindolaa17151a2013-10-08 13:08:17 +000078
Craig Topperca7e3e52014-03-10 03:19:03 +000079 void switchVendor(StringRef Vendor) override;
80 void emitAttribute(unsigned Attribute, unsigned Value) override;
81 void emitTextAttribute(unsigned Attribute, StringRef String) override;
82 void emitIntTextAttribute(unsigned Attribute, unsigned IntValue,
83 StringRef StrinValue) override;
84 void emitArch(unsigned Arch) override;
Sumanth Gundapanenia9049ea2015-02-26 18:07:35 +000085 void emitArchExtension(unsigned ArchExt) override;
Craig Topperca7e3e52014-03-10 03:19:03 +000086 void emitObjectArch(unsigned Arch) override;
87 void emitFPU(unsigned FPU) override;
88 void emitInst(uint32_t Inst, char Suffix = '\0') override;
89 void finishAttributeSection() override;
Logan Chien8cbb80d2013-10-28 17:51:12 +000090
Craig Topperca7e3e52014-03-10 03:19:03 +000091 void AnnotateTLSDescriptorSequence(const MCSymbolRefExpr *SRE) override;
Rafael Espindola466d6632014-04-27 20:23:58 +000092 void emitThumbSet(MCSymbol *Symbol, const MCExpr *Value) override;
Saleem Abdulrasool56e06e82014-01-30 04:02:47 +000093
Rafael Espindolaa17151a2013-10-08 13:08:17 +000094public:
Rafael Espindola24ea09e2014-01-26 06:06:37 +000095 ARMTargetAsmStreamer(MCStreamer &S, formatted_raw_ostream &OS,
96 MCInstPrinter &InstPrinter, bool VerboseAsm);
Rafael Espindolaa17151a2013-10-08 13:08:17 +000097};
98
Rafael Espindola24ea09e2014-01-26 06:06:37 +000099ARMTargetAsmStreamer::ARMTargetAsmStreamer(MCStreamer &S,
100 formatted_raw_ostream &OS,
Saleem Abdulrasoolf16e68a2014-01-07 02:28:50 +0000101 MCInstPrinter &InstPrinter,
102 bool VerboseAsm)
Rafael Espindola24ea09e2014-01-26 06:06:37 +0000103 : ARMTargetStreamer(S), OS(OS), InstPrinter(InstPrinter),
104 IsVerboseAsm(VerboseAsm) {}
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000105void ARMTargetAsmStreamer::emitFnStart() { OS << "\t.fnstart\n"; }
106void ARMTargetAsmStreamer::emitFnEnd() { OS << "\t.fnend\n"; }
107void ARMTargetAsmStreamer::emitCantUnwind() { OS << "\t.cantunwind\n"; }
108void ARMTargetAsmStreamer::emitPersonality(const MCSymbol *Personality) {
109 OS << "\t.personality " << Personality->getName() << '\n';
110}
Saleem Abdulrasool662f5c12014-01-21 02:33:02 +0000111void ARMTargetAsmStreamer::emitPersonalityIndex(unsigned Index) {
112 OS << "\t.personalityindex " << Index << '\n';
113}
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000114void ARMTargetAsmStreamer::emitHandlerData() { OS << "\t.handlerdata\n"; }
115void ARMTargetAsmStreamer::emitSetFP(unsigned FpReg, unsigned SpReg,
116 int64_t Offset) {
117 OS << "\t.setfp\t";
118 InstPrinter.printRegName(OS, FpReg);
119 OS << ", ";
120 InstPrinter.printRegName(OS, SpReg);
121 if (Offset)
122 OS << ", #" << Offset;
123 OS << '\n';
124}
Saleem Abdulrasool5d962d32014-01-30 04:46:24 +0000125void ARMTargetAsmStreamer::emitMovSP(unsigned Reg, int64_t Offset) {
126 assert((Reg != ARM::SP && Reg != ARM::PC) &&
127 "the operand of .movsp cannot be either sp or pc");
128
129 OS << "\t.movsp\t";
130 InstPrinter.printRegName(OS, Reg);
131 if (Offset)
132 OS << ", #" << Offset;
133 OS << '\n';
134}
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000135void ARMTargetAsmStreamer::emitPad(int64_t Offset) {
136 OS << "\t.pad\t#" << Offset << '\n';
137}
138void ARMTargetAsmStreamer::emitRegSave(const SmallVectorImpl<unsigned> &RegList,
139 bool isVector) {
140 assert(RegList.size() && "RegList should not be empty");
141 if (isVector)
142 OS << "\t.vsave\t{";
143 else
144 OS << "\t.save\t{";
145
146 InstPrinter.printRegName(OS, RegList[0]);
147
148 for (unsigned i = 1, e = RegList.size(); i != e; ++i) {
149 OS << ", ";
150 InstPrinter.printRegName(OS, RegList[i]);
151 }
152
153 OS << "}\n";
154}
Logan Chien8cbb80d2013-10-28 17:51:12 +0000155void ARMTargetAsmStreamer::switchVendor(StringRef Vendor) {
156}
157void ARMTargetAsmStreamer::emitAttribute(unsigned Attribute, unsigned Value) {
Saleem Abdulrasoolf16e68a2014-01-07 02:28:50 +0000158 OS << "\t.eabi_attribute\t" << Attribute << ", " << Twine(Value);
159 if (IsVerboseAsm) {
160 StringRef Name = ARMBuildAttrs::AttrTypeAsString(Attribute);
161 if (!Name.empty())
162 OS << "\t@ " << Name;
163 }
164 OS << "\n";
Logan Chien8cbb80d2013-10-28 17:51:12 +0000165}
166void ARMTargetAsmStreamer::emitTextAttribute(unsigned Attribute,
167 StringRef String) {
168 switch (Attribute) {
Logan Chien8cbb80d2013-10-28 17:51:12 +0000169 case ARMBuildAttrs::CPU_name:
Saleem Abdulrasool87ccd362014-01-07 02:28:42 +0000170 OS << "\t.cpu\t" << String.lower();
171 break;
172 default:
173 OS << "\t.eabi_attribute\t" << Attribute << ", \"" << String << "\"";
Saleem Abdulrasoolf16e68a2014-01-07 02:28:50 +0000174 if (IsVerboseAsm) {
175 StringRef Name = ARMBuildAttrs::AttrTypeAsString(Attribute);
176 if (!Name.empty())
177 OS << "\t@ " << Name;
178 }
Logan Chien8cbb80d2013-10-28 17:51:12 +0000179 break;
180 }
Saleem Abdulrasool87ccd362014-01-07 02:28:42 +0000181 OS << "\n";
182}
183void ARMTargetAsmStreamer::emitIntTextAttribute(unsigned Attribute,
184 unsigned IntValue,
185 StringRef StringValue) {
186 switch (Attribute) {
187 default: llvm_unreachable("unsupported multi-value attribute in asm mode");
188 case ARMBuildAttrs::compatibility:
189 OS << "\t.eabi_attribute\t" << Attribute << ", " << IntValue;
190 if (!StringValue.empty())
191 OS << ", \"" << StringValue << "\"";
Saleem Abdulrasoolf16e68a2014-01-07 02:28:50 +0000192 if (IsVerboseAsm)
193 OS << "\t@ " << ARMBuildAttrs::AttrTypeAsString(Attribute);
Saleem Abdulrasool87ccd362014-01-07 02:28:42 +0000194 break;
195 }
196 OS << "\n";
Logan Chien8cbb80d2013-10-28 17:51:12 +0000197}
Logan Chien439e8f92013-12-11 17:16:25 +0000198void ARMTargetAsmStreamer::emitArch(unsigned Arch) {
Renato Golinf5f373f2015-05-08 21:04:27 +0000199 OS << "\t.arch\t" << ARMTargetParser::getArchName(Arch) << "\n";
Logan Chien439e8f92013-12-11 17:16:25 +0000200}
Sumanth Gundapanenia9049ea2015-02-26 18:07:35 +0000201void ARMTargetAsmStreamer::emitArchExtension(unsigned ArchExt) {
Renato Golinf5f373f2015-05-08 21:04:27 +0000202 OS << "\t.arch_extension\t" << ARMTargetParser::getArchExtName(ArchExt) << "\n";
Sumanth Gundapanenia9049ea2015-02-26 18:07:35 +0000203}
Saleem Abdulrasool4c4789b2014-01-30 04:46:41 +0000204void ARMTargetAsmStreamer::emitObjectArch(unsigned Arch) {
Renato Golinf5f373f2015-05-08 21:04:27 +0000205 OS << "\t.object_arch\t" << ARMTargetParser::getArchName(Arch) << '\n';
Saleem Abdulrasool4c4789b2014-01-30 04:46:41 +0000206}
Logan Chien8cbb80d2013-10-28 17:51:12 +0000207void ARMTargetAsmStreamer::emitFPU(unsigned FPU) {
Renato Golinf5f373f2015-05-08 21:04:27 +0000208 OS << "\t.fpu\t" << ARMTargetParser::getFPUName(FPU) << "\n";
Logan Chien8cbb80d2013-10-28 17:51:12 +0000209}
210void ARMTargetAsmStreamer::finishAttributeSection() {
211}
Saleem Abdulrasool56e06e82014-01-30 04:02:47 +0000212void
213ARMTargetAsmStreamer::AnnotateTLSDescriptorSequence(const MCSymbolRefExpr *S) {
214 OS << "\t.tlsdescseq\t" << S->getSymbol().getName();
215}
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000216
Rafael Espindola466d6632014-04-27 20:23:58 +0000217void ARMTargetAsmStreamer::emitThumbSet(MCSymbol *Symbol, const MCExpr *Value) {
218 OS << "\t.thumb_set\t" << *Symbol << ", " << *Value << '\n';
219}
220
Saleem Abdulrasoolc0da2cb2013-12-19 05:17:58 +0000221void ARMTargetAsmStreamer::emitInst(uint32_t Inst, char Suffix) {
222 OS << "\t.inst";
223 if (Suffix)
224 OS << "." << Suffix;
Benjamin Kramerbe48c402015-05-23 16:39:10 +0000225 OS << "\t0x" << Twine::utohexstr(Inst) << "\n";
Saleem Abdulrasoolc0da2cb2013-12-19 05:17:58 +0000226}
227
Saleem Abdulrasoold9f08602014-01-21 02:33:10 +0000228void ARMTargetAsmStreamer::emitUnwindRaw(int64_t Offset,
229 const SmallVectorImpl<uint8_t> &Opcodes) {
230 OS << "\t.unwind_raw " << Offset;
231 for (SmallVectorImpl<uint8_t>::const_iterator OCI = Opcodes.begin(),
232 OCE = Opcodes.end();
233 OCI != OCE; ++OCI)
Benjamin Kramerbe48c402015-05-23 16:39:10 +0000234 OS << ", 0x" << Twine::utohexstr(*OCI);
Saleem Abdulrasoold9f08602014-01-21 02:33:10 +0000235 OS << '\n';
236}
237
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000238class ARMTargetELFStreamer : public ARMTargetStreamer {
Logan Chien8cbb80d2013-10-28 17:51:12 +0000239private:
240 // This structure holds all attributes, accounting for
241 // their string/numeric value, so we can later emmit them
242 // in declaration order, keeping all in the same vector
243 struct AttributeItem {
244 enum {
245 HiddenAttribute = 0,
246 NumericAttribute,
Saleem Abdulrasool87ccd362014-01-07 02:28:42 +0000247 TextAttribute,
248 NumericAndTextAttributes
Logan Chien8cbb80d2013-10-28 17:51:12 +0000249 } Type;
250 unsigned Tag;
251 unsigned IntValue;
252 StringRef StringValue;
253
254 static bool LessTag(const AttributeItem &LHS, const AttributeItem &RHS) {
Charlie Turner8b2caa42015-01-05 13:12:17 +0000255 // The conformance tag must be emitted first when serialised
256 // into an object file. Specifically, the addenda to the ARM ABI
257 // states that (2.3.7.4):
258 //
259 // "To simplify recognition by consumers in the common case of
260 // claiming conformity for the whole file, this tag should be
261 // emitted first in a file-scope sub-subsection of the first
262 // public subsection of the attributes section."
263 //
264 // So it is special-cased in this comparison predicate when the
265 // attributes are sorted in finishAttributeSection().
266 return (RHS.Tag != ARMBuildAttrs::conformance) &&
267 ((LHS.Tag == ARMBuildAttrs::conformance) || (LHS.Tag < RHS.Tag));
Logan Chien8cbb80d2013-10-28 17:51:12 +0000268 }
269 };
270
271 StringRef CurrentVendor;
272 unsigned FPU;
Logan Chien439e8f92013-12-11 17:16:25 +0000273 unsigned Arch;
Saleem Abdulrasool4c4789b2014-01-30 04:46:41 +0000274 unsigned EmittedArch;
Logan Chien8cbb80d2013-10-28 17:51:12 +0000275 SmallVector<AttributeItem, 64> Contents;
276
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000277 MCSection *AttributeSection;
Logan Chien8cbb80d2013-10-28 17:51:12 +0000278
Logan Chien8cbb80d2013-10-28 17:51:12 +0000279 AttributeItem *getAttributeItem(unsigned Attribute) {
280 for (size_t i = 0; i < Contents.size(); ++i)
281 if (Contents[i].Tag == Attribute)
282 return &Contents[i];
Craig Topper062a2ba2014-04-25 05:30:21 +0000283 return nullptr;
Logan Chien8cbb80d2013-10-28 17:51:12 +0000284 }
285
286 void setAttributeItem(unsigned Attribute, unsigned Value,
287 bool OverwriteExisting) {
288 // Look for existing attribute item
289 if (AttributeItem *Item = getAttributeItem(Attribute)) {
290 if (!OverwriteExisting)
291 return;
Saleem Abdulrasool93900052014-01-19 08:25:41 +0000292 Item->Type = AttributeItem::NumericAttribute;
Logan Chien8cbb80d2013-10-28 17:51:12 +0000293 Item->IntValue = Value;
294 return;
295 }
296
297 // Create new attribute item
298 AttributeItem Item = {
299 AttributeItem::NumericAttribute,
300 Attribute,
301 Value,
302 StringRef("")
303 };
304 Contents.push_back(Item);
305 }
306
307 void setAttributeItem(unsigned Attribute, StringRef Value,
308 bool OverwriteExisting) {
309 // Look for existing attribute item
310 if (AttributeItem *Item = getAttributeItem(Attribute)) {
311 if (!OverwriteExisting)
312 return;
Saleem Abdulrasool93900052014-01-19 08:25:41 +0000313 Item->Type = AttributeItem::TextAttribute;
Logan Chien8cbb80d2013-10-28 17:51:12 +0000314 Item->StringValue = Value;
315 return;
316 }
317
318 // Create new attribute item
319 AttributeItem Item = {
320 AttributeItem::TextAttribute,
321 Attribute,
322 0,
323 Value
324 };
325 Contents.push_back(Item);
326 }
327
Saleem Abdulrasool87ccd362014-01-07 02:28:42 +0000328 void setAttributeItems(unsigned Attribute, unsigned IntValue,
329 StringRef StringValue, bool OverwriteExisting) {
330 // Look for existing attribute item
331 if (AttributeItem *Item = getAttributeItem(Attribute)) {
332 if (!OverwriteExisting)
333 return;
Saleem Abdulrasool93900052014-01-19 08:25:41 +0000334 Item->Type = AttributeItem::NumericAndTextAttributes;
Saleem Abdulrasool87ccd362014-01-07 02:28:42 +0000335 Item->IntValue = IntValue;
336 Item->StringValue = StringValue;
337 return;
338 }
339
340 // Create new attribute item
341 AttributeItem Item = {
342 AttributeItem::NumericAndTextAttributes,
343 Attribute,
344 IntValue,
345 StringValue
346 };
347 Contents.push_back(Item);
348 }
349
Logan Chien439e8f92013-12-11 17:16:25 +0000350 void emitArchDefaultAttributes();
Logan Chien8cbb80d2013-10-28 17:51:12 +0000351 void emitFPUDefaultAttributes();
352
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000353 ARMELFStreamer &getStreamer();
Logan Chien8cbb80d2013-10-28 17:51:12 +0000354
Craig Topperca7e3e52014-03-10 03:19:03 +0000355 void emitFnStart() override;
356 void emitFnEnd() override;
357 void emitCantUnwind() override;
358 void emitPersonality(const MCSymbol *Personality) override;
359 void emitPersonalityIndex(unsigned Index) override;
360 void emitHandlerData() override;
361 void emitSetFP(unsigned FpReg, unsigned SpReg, int64_t Offset = 0) override;
362 void emitMovSP(unsigned Reg, int64_t Offset = 0) override;
363 void emitPad(int64_t Offset) override;
364 void emitRegSave(const SmallVectorImpl<unsigned> &RegList,
365 bool isVector) override;
366 void emitUnwindRaw(int64_t Offset,
367 const SmallVectorImpl<uint8_t> &Opcodes) override;
Logan Chien8cbb80d2013-10-28 17:51:12 +0000368
Craig Topperca7e3e52014-03-10 03:19:03 +0000369 void switchVendor(StringRef Vendor) override;
370 void emitAttribute(unsigned Attribute, unsigned Value) override;
371 void emitTextAttribute(unsigned Attribute, StringRef String) override;
372 void emitIntTextAttribute(unsigned Attribute, unsigned IntValue,
373 StringRef StringValue) override;
374 void emitArch(unsigned Arch) override;
375 void emitObjectArch(unsigned Arch) override;
376 void emitFPU(unsigned FPU) override;
377 void emitInst(uint32_t Inst, char Suffix = '\0') override;
378 void finishAttributeSection() override;
Rafael Espindola4c6f6132014-04-27 17:10:46 +0000379 void emitLabel(MCSymbol *Symbol) override;
Logan Chien8cbb80d2013-10-28 17:51:12 +0000380
Craig Topperca7e3e52014-03-10 03:19:03 +0000381 void AnnotateTLSDescriptorSequence(const MCSymbolRefExpr *SRE) override;
Rafael Espindola466d6632014-04-27 20:23:58 +0000382 void emitThumbSet(MCSymbol *Symbol, const MCExpr *Value) override;
Saleem Abdulrasool56e06e82014-01-30 04:02:47 +0000383
Logan Chien8cbb80d2013-10-28 17:51:12 +0000384 size_t calculateContentSize() const;
385
386public:
Rafael Espindola24ea09e2014-01-26 06:06:37 +0000387 ARMTargetELFStreamer(MCStreamer &S)
Renato Golin35de35d2015-05-12 10:33:58 +0000388 : ARMTargetStreamer(S), CurrentVendor("aeabi"), FPU(ARM::FK_INVALID),
389 Arch(ARM::AK_INVALID), EmittedArch(ARM::AK_INVALID),
Craig Topper062a2ba2014-04-25 05:30:21 +0000390 AttributeSection(nullptr) {}
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000391};
392
Tim Northover5cc3dc82012-12-07 16:50:23 +0000393/// Extend the generic ELFStreamer class so that it can emit mapping symbols at
394/// the appropriate points in the object files. These symbols are defined in the
395/// ARM ELF ABI: infocenter.arm.com/help/topic/com.arm.../IHI0044D_aaelf.pdf.
396///
397/// In brief: $a, $t or $d should be emitted at the start of each contiguous
398/// region of ARM code, Thumb code or data in a section. In practice, this
399/// emission does not rely on explicit assembler directives but on inherent
400/// properties of the directives doing the emission (e.g. ".byte" is data, "add
401/// r0, r0, r0" an instruction).
402///
403/// As a result this system is orthogonal to the DataRegion infrastructure used
404/// by MachO. Beware!
405class ARMELFStreamer : public MCELFStreamer {
406public:
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000407 friend class ARMTargetELFStreamer;
408
Rafael Espindola5560a4c2015-04-14 22:14:34 +0000409 ARMELFStreamer(MCContext &Context, MCAsmBackend &TAB, raw_pwrite_stream &OS,
Rafael Espindola24ea09e2014-01-26 06:06:37 +0000410 MCCodeEmitter *Emitter, bool IsThumb)
411 : MCELFStreamer(Context, TAB, OS, Emitter), IsThumb(IsThumb),
412 MappingSymbolCounter(0), LastEMS(EMS_None) {
Logan Chiend8bb4b72013-04-16 12:02:21 +0000413 Reset();
414 }
Tim Northover5cc3dc82012-12-07 16:50:23 +0000415
416 ~ARMELFStreamer() {}
417
Craig Topperca7e3e52014-03-10 03:19:03 +0000418 void FinishImpl() override;
Logan Chien8cbb80d2013-10-28 17:51:12 +0000419
Logan Chien2bcc42c2013-01-30 15:39:04 +0000420 // ARM exception handling directives
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000421 void emitFnStart();
422 void emitFnEnd();
423 void emitCantUnwind();
424 void emitPersonality(const MCSymbol *Per);
Saleem Abdulrasool662f5c12014-01-21 02:33:02 +0000425 void emitPersonalityIndex(unsigned index);
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000426 void emitHandlerData();
427 void emitSetFP(unsigned NewFpReg, unsigned NewSpReg, int64_t Offset = 0);
Saleem Abdulrasool5d962d32014-01-30 04:46:24 +0000428 void emitMovSP(unsigned Reg, int64_t Offset = 0);
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000429 void emitPad(int64_t Offset);
430 void emitRegSave(const SmallVectorImpl<unsigned> &RegList, bool isVector);
Saleem Abdulrasoold9f08602014-01-21 02:33:10 +0000431 void emitUnwindRaw(int64_t Offset, const SmallVectorImpl<uint8_t> &Opcodes);
Logan Chien2bcc42c2013-01-30 15:39:04 +0000432
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000433 void ChangeSection(MCSection *Section, const MCExpr *Subsection) override {
Tim Northover5cc3dc82012-12-07 16:50:23 +0000434 // We have to keep track of the mapping symbol state of any sections we
435 // use. Each one should start off as EMS_None, which is provided as the
436 // default constructor by DenseMap::lookup.
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000437 LastMappingSymbols[getPreviousSection().first] = LastEMS;
Tim Northover5cc3dc82012-12-07 16:50:23 +0000438 LastEMS = LastMappingSymbols.lookup(Section);
439
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000440 MCELFStreamer::ChangeSection(Section, Subsection);
Tim Northover5cc3dc82012-12-07 16:50:23 +0000441 }
442
443 /// This function is the one used to emit instruction data into the ELF
444 /// streamer. We override it to add the appropriate mapping symbol if
445 /// necessary.
Craig Topperca7e3e52014-03-10 03:19:03 +0000446 void EmitInstruction(const MCInst& Inst,
447 const MCSubtargetInfo &STI) override {
Tim Northover5cc3dc82012-12-07 16:50:23 +0000448 if (IsThumb)
449 EmitThumbMappingSymbol();
450 else
451 EmitARMMappingSymbol();
452
David Woodhousee6c13e42014-01-28 23:12:42 +0000453 MCELFStreamer::EmitInstruction(Inst, STI);
Tim Northover5cc3dc82012-12-07 16:50:23 +0000454 }
455
Craig Topperd25ff6f2014-03-10 03:22:59 +0000456 void emitInst(uint32_t Inst, char Suffix) {
Saleem Abdulrasoolc0da2cb2013-12-19 05:17:58 +0000457 unsigned Size;
458 char Buffer[4];
459 const bool LittleEndian = getContext().getAsmInfo()->isLittleEndian();
460
461 switch (Suffix) {
462 case '\0':
463 Size = 4;
464
465 assert(!IsThumb);
466 EmitARMMappingSymbol();
467 for (unsigned II = 0, IE = Size; II != IE; II++) {
468 const unsigned I = LittleEndian ? (Size - II - 1) : II;
469 Buffer[Size - II - 1] = uint8_t(Inst >> I * CHAR_BIT);
470 }
471
472 break;
473 case 'n':
474 case 'w':
475 Size = (Suffix == 'n' ? 2 : 4);
476
477 assert(IsThumb);
478 EmitThumbMappingSymbol();
479 for (unsigned II = 0, IE = Size; II != IE; II = II + 2) {
480 const unsigned I0 = LittleEndian ? II + 0 : (Size - II - 1);
481 const unsigned I1 = LittleEndian ? II + 1 : (Size - II - 2);
482 Buffer[Size - II - 2] = uint8_t(Inst >> I0 * CHAR_BIT);
483 Buffer[Size - II - 1] = uint8_t(Inst >> I1 * CHAR_BIT);
484 }
485
486 break;
487 default:
488 llvm_unreachable("Invalid Suffix");
489 }
490
491 MCELFStreamer::EmitBytes(StringRef(Buffer, Size));
492 }
493
Tim Northover5cc3dc82012-12-07 16:50:23 +0000494 /// This is one of the functions used to emit data into an ELF section, so the
495 /// ARM streamer overrides it to add the appropriate mapping symbol ($d) if
496 /// necessary.
Craig Topperca7e3e52014-03-10 03:19:03 +0000497 void EmitBytes(StringRef Data) override {
Tim Northover5cc3dc82012-12-07 16:50:23 +0000498 EmitDataMappingSymbol();
Rafael Espindola64e1af82013-07-02 15:49:13 +0000499 MCELFStreamer::EmitBytes(Data);
Tim Northover5cc3dc82012-12-07 16:50:23 +0000500 }
501
502 /// This is one of the functions used to emit data into an ELF section, so the
503 /// ARM streamer overrides it to add the appropriate mapping symbol ($d) if
504 /// necessary.
Kevin Enderby96918bc2014-04-22 17:27:29 +0000505 void EmitValueImpl(const MCExpr *Value, unsigned Size,
506 const SMLoc &Loc) override {
Saleem Abdulrasoolfe781972015-01-11 04:39:18 +0000507 if (const MCSymbolRefExpr *SRE = dyn_cast_or_null<MCSymbolRefExpr>(Value))
508 if (SRE->getKind() == MCSymbolRefExpr::VK_ARM_SBREL && !(Size == 4))
Jim Grosbach6f482002015-05-18 18:43:14 +0000509 getContext().reportFatalError(Loc, "relocated expression must be 32-bit");
Saleem Abdulrasoolfe781972015-01-11 04:39:18 +0000510
Tim Northover5cc3dc82012-12-07 16:50:23 +0000511 EmitDataMappingSymbol();
Rafael Espindola64e1af82013-07-02 15:49:13 +0000512 MCELFStreamer::EmitValueImpl(Value, Size);
Tim Northover5cc3dc82012-12-07 16:50:23 +0000513 }
514
Craig Topperca7e3e52014-03-10 03:19:03 +0000515 void EmitAssemblerFlag(MCAssemblerFlag Flag) override {
Tim Northover5cc3dc82012-12-07 16:50:23 +0000516 MCELFStreamer::EmitAssemblerFlag(Flag);
517
518 switch (Flag) {
519 case MCAF_SyntaxUnified:
520 return; // no-op here.
521 case MCAF_Code16:
522 IsThumb = true;
523 return; // Change to Thumb mode
524 case MCAF_Code32:
525 IsThumb = false;
526 return; // Change to ARM mode
527 case MCAF_Code64:
528 return;
529 case MCAF_SubsectionsViaSymbols:
530 return;
531 }
532 }
533
534private:
535 enum ElfMappingSymbol {
536 EMS_None,
537 EMS_ARM,
538 EMS_Thumb,
539 EMS_Data
540 };
541
542 void EmitDataMappingSymbol() {
543 if (LastEMS == EMS_Data) return;
544 EmitMappingSymbol("$d");
545 LastEMS = EMS_Data;
546 }
547
548 void EmitThumbMappingSymbol() {
549 if (LastEMS == EMS_Thumb) return;
550 EmitMappingSymbol("$t");
551 LastEMS = EMS_Thumb;
552 }
553
554 void EmitARMMappingSymbol() {
555 if (LastEMS == EMS_ARM) return;
556 EmitMappingSymbol("$a");
557 LastEMS = EMS_ARM;
558 }
559
560 void EmitMappingSymbol(StringRef Name) {
Jim Grosbach6f482002015-05-18 18:43:14 +0000561 MCSymbol *Start = getContext().createTempSymbol();
Tim Northover5cc3dc82012-12-07 16:50:23 +0000562 EmitLabel(Start);
563
Rafael Espindola95fb9b92015-06-02 20:38:46 +0000564 auto *Symbol = cast<MCSymbolELF>(getContext().getOrCreateSymbol(
565 Name + "." + Twine(MappingSymbolCounter++)));
Tim Northover5cc3dc82012-12-07 16:50:23 +0000566
Rafael Espindolab5d316b2015-05-29 20:21:02 +0000567 getAssembler().registerSymbol(*Symbol);
Rafael Espindola95fb9b92015-06-02 20:38:46 +0000568 Symbol->setType(ELF::STT_NOTYPE);
569 Symbol->setBinding(ELF::STB_LOCAL);
Rafael Espindola4d37b2a2015-05-29 21:45:01 +0000570 Symbol->setExternal(false);
Richard Mitton21101b32013-09-19 23:21:01 +0000571 AssignSection(Symbol, getCurrentSection().first);
Tim Northover5cc3dc82012-12-07 16:50:23 +0000572
Jim Grosbach13760bd2015-05-30 01:25:56 +0000573 const MCExpr *Value = MCSymbolRefExpr::create(Start, getContext());
Tim Northover5cc3dc82012-12-07 16:50:23 +0000574 Symbol->setVariableValue(Value);
575 }
576
Craig Topperca7e3e52014-03-10 03:19:03 +0000577 void EmitThumbFunc(MCSymbol *Func) override {
Tim Northover5cc3dc82012-12-07 16:50:23 +0000578 getAssembler().setIsThumbFunc(Func);
Rafael Espindolab60c8292014-04-29 12:46:50 +0000579 EmitSymbolAttribute(Func, MCSA_ELF_TypeFunction);
Tim Northover5cc3dc82012-12-07 16:50:23 +0000580 }
581
Logan Chien2bcc42c2013-01-30 15:39:04 +0000582 // Helper functions for ARM exception handling directives
583 void Reset();
584
585 void EmitPersonalityFixup(StringRef Name);
Logan Chien325823a2013-06-09 12:22:30 +0000586 void FlushPendingOffset();
Logan Chienc931fce2013-07-02 12:43:27 +0000587 void FlushUnwindOpcodes(bool NoHandlerData);
Logan Chien2bcc42c2013-01-30 15:39:04 +0000588
589 void SwitchToEHSection(const char *Prefix, unsigned Type, unsigned Flags,
590 SectionKind Kind, const MCSymbol &Fn);
591 void SwitchToExTabSection(const MCSymbol &FnStart);
592 void SwitchToExIdxSection(const MCSymbol &FnStart);
Tim Northover5cc3dc82012-12-07 16:50:23 +0000593
Saleem Abdulrasool56e06e82014-01-30 04:02:47 +0000594 void EmitFixup(const MCExpr *Expr, MCFixupKind Kind);
595
Tim Northover5cc3dc82012-12-07 16:50:23 +0000596 bool IsThumb;
597 int64_t MappingSymbolCounter;
598
599 DenseMap<const MCSection *, ElfMappingSymbol> LastMappingSymbols;
600 ElfMappingSymbol LastEMS;
601
Logan Chien2bcc42c2013-01-30 15:39:04 +0000602 // ARM Exception Handling Frame Information
603 MCSymbol *ExTab;
604 MCSymbol *FnStart;
605 const MCSymbol *Personality;
Logan Chien325823a2013-06-09 12:22:30 +0000606 unsigned PersonalityIndex;
607 unsigned FPReg; // Frame pointer register
608 int64_t FPOffset; // Offset: (final frame pointer) - (initial $sp)
609 int64_t SPOffset; // Offset: (final $sp) - (initial $sp)
610 int64_t PendingOffset; // Offset: (final $sp) - (emitted $sp)
Logan Chiend8bb4b72013-04-16 12:02:21 +0000611 bool UsedFP;
Logan Chien2bcc42c2013-01-30 15:39:04 +0000612 bool CantUnwind;
Logan Chien325823a2013-06-09 12:22:30 +0000613 SmallVector<uint8_t, 64> Opcodes;
Logan Chiend8bb4b72013-04-16 12:02:21 +0000614 UnwindOpcodeAssembler UnwindOpAsm;
Tim Northover5cc3dc82012-12-07 16:50:23 +0000615};
Logan Chiend8bb4b72013-04-16 12:02:21 +0000616} // end anonymous namespace
Tim Northover5cc3dc82012-12-07 16:50:23 +0000617
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000618ARMELFStreamer &ARMTargetELFStreamer::getStreamer() {
Rafael Espindola24ea09e2014-01-26 06:06:37 +0000619 return static_cast<ARMELFStreamer &>(Streamer);
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000620}
621
622void ARMTargetELFStreamer::emitFnStart() { getStreamer().emitFnStart(); }
623void ARMTargetELFStreamer::emitFnEnd() { getStreamer().emitFnEnd(); }
624void ARMTargetELFStreamer::emitCantUnwind() { getStreamer().emitCantUnwind(); }
625void ARMTargetELFStreamer::emitPersonality(const MCSymbol *Personality) {
626 getStreamer().emitPersonality(Personality);
627}
Saleem Abdulrasool662f5c12014-01-21 02:33:02 +0000628void ARMTargetELFStreamer::emitPersonalityIndex(unsigned Index) {
629 getStreamer().emitPersonalityIndex(Index);
630}
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000631void ARMTargetELFStreamer::emitHandlerData() {
632 getStreamer().emitHandlerData();
633}
634void ARMTargetELFStreamer::emitSetFP(unsigned FpReg, unsigned SpReg,
635 int64_t Offset) {
636 getStreamer().emitSetFP(FpReg, SpReg, Offset);
637}
Saleem Abdulrasool5d962d32014-01-30 04:46:24 +0000638void ARMTargetELFStreamer::emitMovSP(unsigned Reg, int64_t Offset) {
639 getStreamer().emitMovSP(Reg, Offset);
640}
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000641void ARMTargetELFStreamer::emitPad(int64_t Offset) {
642 getStreamer().emitPad(Offset);
643}
644void ARMTargetELFStreamer::emitRegSave(const SmallVectorImpl<unsigned> &RegList,
645 bool isVector) {
646 getStreamer().emitRegSave(RegList, isVector);
647}
Saleem Abdulrasoold9f08602014-01-21 02:33:10 +0000648void ARMTargetELFStreamer::emitUnwindRaw(int64_t Offset,
649 const SmallVectorImpl<uint8_t> &Opcodes) {
650 getStreamer().emitUnwindRaw(Offset, Opcodes);
651}
Logan Chien8cbb80d2013-10-28 17:51:12 +0000652void ARMTargetELFStreamer::switchVendor(StringRef Vendor) {
653 assert(!Vendor.empty() && "Vendor cannot be empty.");
654
655 if (CurrentVendor == Vendor)
656 return;
657
658 if (!CurrentVendor.empty())
659 finishAttributeSection();
660
661 assert(Contents.empty() &&
662 ".ARM.attributes should be flushed before changing vendor");
663 CurrentVendor = Vendor;
664
665}
666void ARMTargetELFStreamer::emitAttribute(unsigned Attribute, unsigned Value) {
667 setAttributeItem(Attribute, Value, /* OverwriteExisting= */ true);
668}
669void ARMTargetELFStreamer::emitTextAttribute(unsigned Attribute,
670 StringRef Value) {
671 setAttributeItem(Attribute, Value, /* OverwriteExisting= */ true);
672}
Saleem Abdulrasool87ccd362014-01-07 02:28:42 +0000673void ARMTargetELFStreamer::emitIntTextAttribute(unsigned Attribute,
674 unsigned IntValue,
675 StringRef StringValue) {
676 setAttributeItems(Attribute, IntValue, StringValue,
677 /* OverwriteExisting= */ true);
678}
Logan Chien439e8f92013-12-11 17:16:25 +0000679void ARMTargetELFStreamer::emitArch(unsigned Value) {
680 Arch = Value;
681}
Saleem Abdulrasool4c4789b2014-01-30 04:46:41 +0000682void ARMTargetELFStreamer::emitObjectArch(unsigned Value) {
683 EmittedArch = Value;
684}
Logan Chien439e8f92013-12-11 17:16:25 +0000685void ARMTargetELFStreamer::emitArchDefaultAttributes() {
686 using namespace ARMBuildAttrs;
Saleem Abdulrasool4c4789b2014-01-30 04:46:41 +0000687
Renato Golinf5f373f2015-05-08 21:04:27 +0000688 setAttributeItem(CPU_name,
Renato Golinf7c0d5f2015-05-27 18:15:37 +0000689 ARMTargetParser::getCPUAttr(Arch),
Renato Golinf5f373f2015-05-08 21:04:27 +0000690 false);
691
Renato Golin35de35d2015-05-12 10:33:58 +0000692 if (EmittedArch == ARM::AK_INVALID)
Renato Golinf5f373f2015-05-08 21:04:27 +0000693 setAttributeItem(CPU_arch,
Renato Golinf7c0d5f2015-05-27 18:15:37 +0000694 ARMTargetParser::getArchAttr(Arch),
Renato Golinf5f373f2015-05-08 21:04:27 +0000695 false);
Saleem Abdulrasool4c4789b2014-01-30 04:46:41 +0000696 else
Renato Golinf5f373f2015-05-08 21:04:27 +0000697 setAttributeItem(CPU_arch,
Renato Golinf7c0d5f2015-05-27 18:15:37 +0000698 ARMTargetParser::getArchAttr(EmittedArch),
Renato Golinf5f373f2015-05-08 21:04:27 +0000699 false);
Logan Chien439e8f92013-12-11 17:16:25 +0000700
701 switch (Arch) {
Renato Golin35de35d2015-05-12 10:33:58 +0000702 case ARM::AK_ARMV2:
703 case ARM::AK_ARMV2A:
704 case ARM::AK_ARMV3:
705 case ARM::AK_ARMV3M:
706 case ARM::AK_ARMV4:
707 case ARM::AK_ARMV5:
Logan Chien439e8f92013-12-11 17:16:25 +0000708 setAttributeItem(ARM_ISA_use, Allowed, false);
709 break;
710
Renato Golin35de35d2015-05-12 10:33:58 +0000711 case ARM::AK_ARMV4T:
712 case ARM::AK_ARMV5T:
713 case ARM::AK_ARMV5TE:
714 case ARM::AK_ARMV6:
715 case ARM::AK_ARMV6J:
Logan Chien439e8f92013-12-11 17:16:25 +0000716 setAttributeItem(ARM_ISA_use, Allowed, false);
717 setAttributeItem(THUMB_ISA_use, Allowed, false);
718 break;
719
Renato Golin35de35d2015-05-12 10:33:58 +0000720 case ARM::AK_ARMV6T2:
Logan Chien439e8f92013-12-11 17:16:25 +0000721 setAttributeItem(ARM_ISA_use, Allowed, false);
722 setAttributeItem(THUMB_ISA_use, AllowThumb32, false);
723 break;
724
Renato Golin35de35d2015-05-12 10:33:58 +0000725 case ARM::AK_ARMV6K:
726 case ARM::AK_ARMV6Z:
727 case ARM::AK_ARMV6ZK:
Logan Chien439e8f92013-12-11 17:16:25 +0000728 setAttributeItem(ARM_ISA_use, Allowed, false);
729 setAttributeItem(THUMB_ISA_use, Allowed, false);
730 setAttributeItem(Virtualization_use, AllowTZ, false);
731 break;
732
Renato Golin35de35d2015-05-12 10:33:58 +0000733 case ARM::AK_ARMV6M:
Logan Chien439e8f92013-12-11 17:16:25 +0000734 setAttributeItem(THUMB_ISA_use, Allowed, false);
735 break;
736
Renato Golin35de35d2015-05-12 10:33:58 +0000737 case ARM::AK_ARMV7:
Logan Chien439e8f92013-12-11 17:16:25 +0000738 setAttributeItem(THUMB_ISA_use, AllowThumb32, false);
739 break;
740
Renato Golin35de35d2015-05-12 10:33:58 +0000741 case ARM::AK_ARMV7A:
Logan Chien439e8f92013-12-11 17:16:25 +0000742 setAttributeItem(CPU_arch_profile, ApplicationProfile, false);
743 setAttributeItem(ARM_ISA_use, Allowed, false);
744 setAttributeItem(THUMB_ISA_use, AllowThumb32, false);
745 break;
746
Renato Golin35de35d2015-05-12 10:33:58 +0000747 case ARM::AK_ARMV7R:
Logan Chien439e8f92013-12-11 17:16:25 +0000748 setAttributeItem(CPU_arch_profile, RealTimeProfile, false);
749 setAttributeItem(ARM_ISA_use, Allowed, false);
750 setAttributeItem(THUMB_ISA_use, AllowThumb32, false);
751 break;
752
Renato Golin35de35d2015-05-12 10:33:58 +0000753 case ARM::AK_ARMV7M:
Logan Chien439e8f92013-12-11 17:16:25 +0000754 setAttributeItem(CPU_arch_profile, MicroControllerProfile, false);
755 setAttributeItem(THUMB_ISA_use, AllowThumb32, false);
756 break;
757
Renato Golin35de35d2015-05-12 10:33:58 +0000758 case ARM::AK_ARMV8A:
759 case ARM::AK_ARMV8_1A:
Logan Chien439e8f92013-12-11 17:16:25 +0000760 setAttributeItem(CPU_arch_profile, ApplicationProfile, false);
761 setAttributeItem(ARM_ISA_use, Allowed, false);
762 setAttributeItem(THUMB_ISA_use, AllowThumb32, false);
763 setAttributeItem(MPextension_use, Allowed, false);
764 setAttributeItem(Virtualization_use, AllowTZVirtualization, false);
765 break;
766
Renato Golin35de35d2015-05-12 10:33:58 +0000767 case ARM::AK_IWMMXT:
Logan Chien439e8f92013-12-11 17:16:25 +0000768 setAttributeItem(ARM_ISA_use, Allowed, false);
769 setAttributeItem(THUMB_ISA_use, Allowed, false);
770 setAttributeItem(WMMX_arch, AllowWMMXv1, false);
771 break;
772
Renato Golin35de35d2015-05-12 10:33:58 +0000773 case ARM::AK_IWMMXT2:
Logan Chien439e8f92013-12-11 17:16:25 +0000774 setAttributeItem(ARM_ISA_use, Allowed, false);
775 setAttributeItem(THUMB_ISA_use, Allowed, false);
776 setAttributeItem(WMMX_arch, AllowWMMXv2, false);
777 break;
778
779 default:
780 report_fatal_error("Unknown Arch: " + Twine(Arch));
781 break;
782 }
783}
Logan Chien8cbb80d2013-10-28 17:51:12 +0000784void ARMTargetELFStreamer::emitFPU(unsigned Value) {
785 FPU = Value;
786}
787void ARMTargetELFStreamer::emitFPUDefaultAttributes() {
788 switch (FPU) {
Renato Golin35de35d2015-05-12 10:33:58 +0000789 case ARM::FK_VFP:
790 case ARM::FK_VFPV2:
Logan Chiena39510a2013-12-18 17:23:15 +0000791 setAttributeItem(ARMBuildAttrs::FP_arch,
Logan Chien8cbb80d2013-10-28 17:51:12 +0000792 ARMBuildAttrs::AllowFPv2,
793 /* OverwriteExisting= */ false);
794 break;
795
Renato Golin35de35d2015-05-12 10:33:58 +0000796 case ARM::FK_VFPV3:
Logan Chiena39510a2013-12-18 17:23:15 +0000797 setAttributeItem(ARMBuildAttrs::FP_arch,
Logan Chien8cbb80d2013-10-28 17:51:12 +0000798 ARMBuildAttrs::AllowFPv3A,
799 /* OverwriteExisting= */ false);
800 break;
801
Renato Golin35de35d2015-05-12 10:33:58 +0000802 case ARM::FK_VFPV3_D16:
Logan Chiena39510a2013-12-18 17:23:15 +0000803 setAttributeItem(ARMBuildAttrs::FP_arch,
Logan Chien8cbb80d2013-10-28 17:51:12 +0000804 ARMBuildAttrs::AllowFPv3B,
805 /* OverwriteExisting= */ false);
806 break;
807
Renato Golin35de35d2015-05-12 10:33:58 +0000808 case ARM::FK_VFPV4:
Logan Chiena39510a2013-12-18 17:23:15 +0000809 setAttributeItem(ARMBuildAttrs::FP_arch,
Logan Chien8cbb80d2013-10-28 17:51:12 +0000810 ARMBuildAttrs::AllowFPv4A,
811 /* OverwriteExisting= */ false);
812 break;
813
Renato Golin35de35d2015-05-12 10:33:58 +0000814 case ARM::FK_VFPV4_D16:
Logan Chiena39510a2013-12-18 17:23:15 +0000815 setAttributeItem(ARMBuildAttrs::FP_arch,
Logan Chien8cbb80d2013-10-28 17:51:12 +0000816 ARMBuildAttrs::AllowFPv4B,
817 /* OverwriteExisting= */ false);
818 break;
819
Renato Golin35de35d2015-05-12 10:33:58 +0000820 case ARM::FK_FP_ARMV8:
Logan Chiena39510a2013-12-18 17:23:15 +0000821 setAttributeItem(ARMBuildAttrs::FP_arch,
Logan Chien8cbb80d2013-10-28 17:51:12 +0000822 ARMBuildAttrs::AllowFPARMv8A,
823 /* OverwriteExisting= */ false);
824 break;
825
Oliver Stannard37e4daa2014-10-01 09:02:17 +0000826 // FPV5_D16 is identical to FP_ARMV8 except for the number of D registers, so
827 // uses the FP_ARMV8_D16 build attribute.
Renato Golin35de35d2015-05-12 10:33:58 +0000828 case ARM::FK_FPV5_D16:
Oliver Stannard37e4daa2014-10-01 09:02:17 +0000829 setAttributeItem(ARMBuildAttrs::FP_arch,
830 ARMBuildAttrs::AllowFPARMv8B,
831 /* OverwriteExisting= */ false);
832 break;
833
Renato Golin35de35d2015-05-12 10:33:58 +0000834 case ARM::FK_NEON:
Logan Chiena39510a2013-12-18 17:23:15 +0000835 setAttributeItem(ARMBuildAttrs::FP_arch,
Logan Chien8cbb80d2013-10-28 17:51:12 +0000836 ARMBuildAttrs::AllowFPv3A,
837 /* OverwriteExisting= */ false);
838 setAttributeItem(ARMBuildAttrs::Advanced_SIMD_arch,
839 ARMBuildAttrs::AllowNeon,
840 /* OverwriteExisting= */ false);
841 break;
842
Renato Golin35de35d2015-05-12 10:33:58 +0000843 case ARM::FK_NEON_VFPV4:
Logan Chiena39510a2013-12-18 17:23:15 +0000844 setAttributeItem(ARMBuildAttrs::FP_arch,
Logan Chien8cbb80d2013-10-28 17:51:12 +0000845 ARMBuildAttrs::AllowFPv4A,
846 /* OverwriteExisting= */ false);
847 setAttributeItem(ARMBuildAttrs::Advanced_SIMD_arch,
848 ARMBuildAttrs::AllowNeon2,
849 /* OverwriteExisting= */ false);
850 break;
851
Renato Golin35de35d2015-05-12 10:33:58 +0000852 case ARM::FK_NEON_FP_ARMV8:
853 case ARM::FK_CRYPTO_NEON_FP_ARMV8:
Logan Chiena39510a2013-12-18 17:23:15 +0000854 setAttributeItem(ARMBuildAttrs::FP_arch,
Logan Chien8cbb80d2013-10-28 17:51:12 +0000855 ARMBuildAttrs::AllowFPARMv8A,
856 /* OverwriteExisting= */ false);
Vladimir Sukharevc632cda2015-03-26 17:05:54 +0000857 // 'Advanced_SIMD_arch' must be emitted not here, but within
858 // ARMAsmPrinter::emitAttributes(), depending on hasV8Ops() and hasV8_1a()
Logan Chien8cbb80d2013-10-28 17:51:12 +0000859 break;
860
Renato Golin35de35d2015-05-12 10:33:58 +0000861 case ARM::FK_SOFTVFP:
Logan Chien05ae7442014-01-02 15:50:02 +0000862 break;
863
Logan Chien8cbb80d2013-10-28 17:51:12 +0000864 default:
865 report_fatal_error("Unknown FPU: " + Twine(FPU));
866 break;
867 }
868}
869size_t ARMTargetELFStreamer::calculateContentSize() const {
870 size_t Result = 0;
871 for (size_t i = 0; i < Contents.size(); ++i) {
872 AttributeItem item = Contents[i];
873 switch (item.Type) {
874 case AttributeItem::HiddenAttribute:
875 break;
876 case AttributeItem::NumericAttribute:
Logan Chien5b776b72014-02-22 14:00:39 +0000877 Result += getULEB128Size(item.Tag);
878 Result += getULEB128Size(item.IntValue);
Logan Chien8cbb80d2013-10-28 17:51:12 +0000879 break;
880 case AttributeItem::TextAttribute:
Logan Chien5b776b72014-02-22 14:00:39 +0000881 Result += getULEB128Size(item.Tag);
Logan Chien8cbb80d2013-10-28 17:51:12 +0000882 Result += item.StringValue.size() + 1; // string + '\0'
883 break;
Saleem Abdulrasool87ccd362014-01-07 02:28:42 +0000884 case AttributeItem::NumericAndTextAttributes:
Logan Chien5b776b72014-02-22 14:00:39 +0000885 Result += getULEB128Size(item.Tag);
886 Result += getULEB128Size(item.IntValue);
Saleem Abdulrasool87ccd362014-01-07 02:28:42 +0000887 Result += item.StringValue.size() + 1; // string + '\0';
888 break;
Logan Chien8cbb80d2013-10-28 17:51:12 +0000889 }
890 }
891 return Result;
892}
893void ARMTargetELFStreamer::finishAttributeSection() {
894 // <format-version>
895 // [ <section-length> "vendor-name"
896 // [ <file-tag> <size> <attribute>*
897 // | <section-tag> <size> <section-number>* 0 <attribute>*
898 // | <symbol-tag> <size> <symbol-number>* 0 <attribute>*
899 // ]+
900 // ]*
901
Renato Golin35de35d2015-05-12 10:33:58 +0000902 if (FPU != ARM::FK_INVALID)
Logan Chien8cbb80d2013-10-28 17:51:12 +0000903 emitFPUDefaultAttributes();
904
Renato Golin35de35d2015-05-12 10:33:58 +0000905 if (Arch != ARM::AK_INVALID)
Logan Chien439e8f92013-12-11 17:16:25 +0000906 emitArchDefaultAttributes();
907
Logan Chien8cbb80d2013-10-28 17:51:12 +0000908 if (Contents.empty())
909 return;
910
911 std::sort(Contents.begin(), Contents.end(), AttributeItem::LessTag);
912
913 ARMELFStreamer &Streamer = getStreamer();
914
915 // Switch to .ARM.attributes section
916 if (AttributeSection) {
917 Streamer.SwitchSection(AttributeSection);
918 } else {
Rafael Espindolaba31e272015-01-29 17:33:21 +0000919 AttributeSection = Streamer.getContext().getELFSection(
920 ".ARM.attributes", ELF::SHT_ARM_ATTRIBUTES, 0);
Logan Chien8cbb80d2013-10-28 17:51:12 +0000921 Streamer.SwitchSection(AttributeSection);
922
923 // Format version
924 Streamer.EmitIntValue(0x41, 1);
925 }
926
927 // Vendor size + Vendor name + '\0'
928 const size_t VendorHeaderSize = 4 + CurrentVendor.size() + 1;
929
930 // Tag + Tag Size
931 const size_t TagHeaderSize = 1 + 4;
932
933 const size_t ContentsSize = calculateContentSize();
934
935 Streamer.EmitIntValue(VendorHeaderSize + TagHeaderSize + ContentsSize, 4);
936 Streamer.EmitBytes(CurrentVendor);
937 Streamer.EmitIntValue(0, 1); // '\0'
938
939 Streamer.EmitIntValue(ARMBuildAttrs::File, 1);
940 Streamer.EmitIntValue(TagHeaderSize + ContentsSize, 4);
941
942 // Size should have been accounted for already, now
943 // emit each field as its type (ULEB or String)
944 for (size_t i = 0; i < Contents.size(); ++i) {
945 AttributeItem item = Contents[i];
946 Streamer.EmitULEB128IntValue(item.Tag);
947 switch (item.Type) {
948 default: llvm_unreachable("Invalid attribute type");
949 case AttributeItem::NumericAttribute:
950 Streamer.EmitULEB128IntValue(item.IntValue);
951 break;
952 case AttributeItem::TextAttribute:
Charlie Turner8d433692014-11-27 12:13:56 +0000953 Streamer.EmitBytes(item.StringValue);
Logan Chien8cbb80d2013-10-28 17:51:12 +0000954 Streamer.EmitIntValue(0, 1); // '\0'
955 break;
Saleem Abdulrasool87ccd362014-01-07 02:28:42 +0000956 case AttributeItem::NumericAndTextAttributes:
957 Streamer.EmitULEB128IntValue(item.IntValue);
Charlie Turner8d433692014-11-27 12:13:56 +0000958 Streamer.EmitBytes(item.StringValue);
Saleem Abdulrasool87ccd362014-01-07 02:28:42 +0000959 Streamer.EmitIntValue(0, 1); // '\0'
960 break;
Logan Chien8cbb80d2013-10-28 17:51:12 +0000961 }
962 }
963
964 Contents.clear();
Renato Golin35de35d2015-05-12 10:33:58 +0000965 FPU = ARM::FK_INVALID;
Logan Chien8cbb80d2013-10-28 17:51:12 +0000966}
Rafael Espindola4c6f6132014-04-27 17:10:46 +0000967
968void ARMTargetELFStreamer::emitLabel(MCSymbol *Symbol) {
969 ARMELFStreamer &Streamer = getStreamer();
970 if (!Streamer.IsThumb)
971 return;
972
Rafael Espindolae3b2acf2015-05-29 18:47:23 +0000973 Streamer.getOrCreateSymbolData(Symbol);
Rafael Espindola95fb9b92015-06-02 20:38:46 +0000974 unsigned Type = cast<MCSymbolELF>(Symbol)->getType();
Scott Douglass7650a9b2014-06-30 09:37:24 +0000975 if (Type == ELF_STT_Func || Type == ELF_STT_GnuIFunc)
Rafael Espindola4c6f6132014-04-27 17:10:46 +0000976 Streamer.EmitThumbFunc(Symbol);
977}
978
Saleem Abdulrasool56e06e82014-01-30 04:02:47 +0000979void
980ARMTargetELFStreamer::AnnotateTLSDescriptorSequence(const MCSymbolRefExpr *S) {
981 getStreamer().EmitFixup(S, FK_Data_4);
982}
Rafael Espindola466d6632014-04-27 20:23:58 +0000983
984void ARMTargetELFStreamer::emitThumbSet(MCSymbol *Symbol, const MCExpr *Value) {
985 if (const MCSymbolRefExpr *SRE = dyn_cast<MCSymbolRefExpr>(Value)) {
Rafael Espindola4a042942014-05-01 12:45:43 +0000986 const MCSymbol &Sym = SRE->getSymbol();
987 if (!Sym.isDefined()) {
Rafael Espindola466d6632014-04-27 20:23:58 +0000988 getStreamer().EmitAssignment(Symbol, Value);
989 return;
990 }
991 }
992
993 getStreamer().EmitThumbFunc(Symbol);
994 getStreamer().EmitAssignment(Symbol, Value);
995}
996
Saleem Abdulrasoolc0da2cb2013-12-19 05:17:58 +0000997void ARMTargetELFStreamer::emitInst(uint32_t Inst, char Suffix) {
998 getStreamer().emitInst(Inst, Suffix);
999}
Logan Chien8cbb80d2013-10-28 17:51:12 +00001000
1001void ARMELFStreamer::FinishImpl() {
Rafael Espindola4a1a3602014-01-14 01:21:46 +00001002 MCTargetStreamer &TS = *getTargetStreamer();
Logan Chien8cbb80d2013-10-28 17:51:12 +00001003 ARMTargetStreamer &ATS = static_cast<ARMTargetStreamer &>(TS);
1004 ATS.finishAttributeSection();
1005
1006 MCELFStreamer::FinishImpl();
1007}
Rafael Espindolaa17151a2013-10-08 13:08:17 +00001008
Logan Chien2bcc42c2013-01-30 15:39:04 +00001009inline void ARMELFStreamer::SwitchToEHSection(const char *Prefix,
1010 unsigned Type,
1011 unsigned Flags,
1012 SectionKind Kind,
1013 const MCSymbol &Fn) {
1014 const MCSectionELF &FnSection =
1015 static_cast<const MCSectionELF &>(Fn.getSection());
1016
1017 // Create the name for new section
1018 StringRef FnSecName(FnSection.getSectionName());
1019 SmallString<128> EHSecName(Prefix);
1020 if (FnSecName != ".text") {
1021 EHSecName += FnSecName;
1022 }
1023
1024 // Get .ARM.extab or .ARM.exidx section
Rafael Espindola0ccf9b72015-06-02 21:30:13 +00001025 const MCSymbolELF *Group = FnSection.getGroup();
Rafael Espindola61e8ce32015-04-06 04:25:18 +00001026 if (Group)
1027 Flags |= ELF::SHF_GROUP;
Rafael Espindola0709a7b2015-05-21 19:20:38 +00001028 MCSectionELF *EHSection =
Rafael Espindola61e8ce32015-04-06 04:25:18 +00001029 getContext().getELFSection(EHSecName, Type, Flags, 0, Group,
1030 FnSection.getUniqueID(), nullptr, &FnSection);
1031
Logan Chiend8bb4b72013-04-16 12:02:21 +00001032 assert(EHSection && "Failed to get the required EH section");
Logan Chien2bcc42c2013-01-30 15:39:04 +00001033
1034 // Switch to .ARM.extab or .ARM.exidx section
1035 SwitchSection(EHSection);
Rafael Espindola7b514962014-02-04 18:34:04 +00001036 EmitCodeAlignment(4);
Logan Chien2bcc42c2013-01-30 15:39:04 +00001037}
1038
1039inline void ARMELFStreamer::SwitchToExTabSection(const MCSymbol &FnStart) {
1040 SwitchToEHSection(".ARM.extab",
1041 ELF::SHT_PROGBITS,
1042 ELF::SHF_ALLOC,
1043 SectionKind::getDataRel(),
1044 FnStart);
1045}
1046
1047inline void ARMELFStreamer::SwitchToExIdxSection(const MCSymbol &FnStart) {
1048 SwitchToEHSection(".ARM.exidx",
1049 ELF::SHT_ARM_EXIDX,
1050 ELF::SHF_ALLOC | ELF::SHF_LINK_ORDER,
1051 SectionKind::getDataRel(),
1052 FnStart);
1053}
Saleem Abdulrasool56e06e82014-01-30 04:02:47 +00001054void ARMELFStreamer::EmitFixup(const MCExpr *Expr, MCFixupKind Kind) {
1055 MCDataFragment *Frag = getOrCreateDataFragment();
Jim Grosbach63661f82015-05-15 19:13:05 +00001056 Frag->getFixups().push_back(MCFixup::create(Frag->getContents().size(), Expr,
Saleem Abdulrasool56e06e82014-01-30 04:02:47 +00001057 Kind));
1058}
Logan Chien2bcc42c2013-01-30 15:39:04 +00001059
1060void ARMELFStreamer::Reset() {
Craig Topper062a2ba2014-04-25 05:30:21 +00001061 ExTab = nullptr;
1062 FnStart = nullptr;
1063 Personality = nullptr;
Saleem Abdulrasoolb961c992014-01-06 00:15:00 +00001064 PersonalityIndex = ARM::EHABI::NUM_PERSONALITY_INDEX;
Logan Chien325823a2013-06-09 12:22:30 +00001065 FPReg = ARM::SP;
Logan Chiend8bb4b72013-04-16 12:02:21 +00001066 FPOffset = 0;
1067 SPOffset = 0;
Logan Chien325823a2013-06-09 12:22:30 +00001068 PendingOffset = 0;
Logan Chiend8bb4b72013-04-16 12:02:21 +00001069 UsedFP = false;
Logan Chien2bcc42c2013-01-30 15:39:04 +00001070 CantUnwind = false;
Logan Chiend8bb4b72013-04-16 12:02:21 +00001071
Logan Chien325823a2013-06-09 12:22:30 +00001072 Opcodes.clear();
Logan Chiend8bb4b72013-04-16 12:02:21 +00001073 UnwindOpAsm.Reset();
Logan Chien2bcc42c2013-01-30 15:39:04 +00001074}
1075
Rafael Espindolaa17151a2013-10-08 13:08:17 +00001076void ARMELFStreamer::emitFnStart() {
Craig Toppere73658d2014-04-28 04:05:08 +00001077 assert(FnStart == nullptr);
Jim Grosbach6f482002015-05-18 18:43:14 +00001078 FnStart = getContext().createTempSymbol();
Logan Chien2bcc42c2013-01-30 15:39:04 +00001079 EmitLabel(FnStart);
1080}
1081
Rafael Espindolaa17151a2013-10-08 13:08:17 +00001082void ARMELFStreamer::emitFnEnd() {
Alp Tokercb402912014-01-24 17:20:08 +00001083 assert(FnStart && ".fnstart must precedes .fnend");
Logan Chien2bcc42c2013-01-30 15:39:04 +00001084
1085 // Emit unwind opcodes if there is no .handlerdata directive
Logan Chien4ea23b52013-05-10 16:17:24 +00001086 if (!ExTab && !CantUnwind)
1087 FlushUnwindOpcodes(true);
Logan Chien2bcc42c2013-01-30 15:39:04 +00001088
1089 // Emit the exception index table entry
1090 SwitchToExIdxSection(*FnStart);
1091
Saleem Abdulrasoolb961c992014-01-06 00:15:00 +00001092 if (PersonalityIndex < ARM::EHABI::NUM_PERSONALITY_INDEX)
Logan Chiend8bb4b72013-04-16 12:02:21 +00001093 EmitPersonalityFixup(GetAEABIUnwindPersonalityName(PersonalityIndex));
Logan Chien2bcc42c2013-01-30 15:39:04 +00001094
1095 const MCSymbolRefExpr *FnStartRef =
Jim Grosbach13760bd2015-05-30 01:25:56 +00001096 MCSymbolRefExpr::create(FnStart,
Logan Chien2bcc42c2013-01-30 15:39:04 +00001097 MCSymbolRefExpr::VK_ARM_PREL31,
1098 getContext());
1099
Rafael Espindola64e1af82013-07-02 15:49:13 +00001100 EmitValue(FnStartRef, 4);
Logan Chien2bcc42c2013-01-30 15:39:04 +00001101
1102 if (CantUnwind) {
Saleem Abdulrasoolb961c992014-01-06 00:15:00 +00001103 EmitIntValue(ARM::EHABI::EXIDX_CANTUNWIND, 4);
Logan Chiend8bb4b72013-04-16 12:02:21 +00001104 } else if (ExTab) {
1105 // Emit a reference to the unwind opcodes in the ".ARM.extab" section.
Logan Chien2bcc42c2013-01-30 15:39:04 +00001106 const MCSymbolRefExpr *ExTabEntryRef =
Jim Grosbach13760bd2015-05-30 01:25:56 +00001107 MCSymbolRefExpr::create(ExTab,
Logan Chien2bcc42c2013-01-30 15:39:04 +00001108 MCSymbolRefExpr::VK_ARM_PREL31,
1109 getContext());
Rafael Espindola64e1af82013-07-02 15:49:13 +00001110 EmitValue(ExTabEntryRef, 4);
Logan Chiend8bb4b72013-04-16 12:02:21 +00001111 } else {
1112 // For the __aeabi_unwind_cpp_pr0, we have to emit the unwind opcodes in
1113 // the second word of exception index table entry. The size of the unwind
1114 // opcodes should always be 4 bytes.
Saleem Abdulrasoolb961c992014-01-06 00:15:00 +00001115 assert(PersonalityIndex == ARM::EHABI::AEABI_UNWIND_CPP_PR0 &&
Jonathan Roelofs4971b402014-05-15 02:24:50 +00001116 "Compact model must use __aeabi_unwind_cpp_pr0 as personality");
Logan Chien325823a2013-06-09 12:22:30 +00001117 assert(Opcodes.size() == 4u &&
Jonathan Roelofs4971b402014-05-15 02:24:50 +00001118 "Unwind opcode size for __aeabi_unwind_cpp_pr0 must be equal to 4");
Christian Pirker39db7ec2014-05-13 16:44:30 +00001119 uint64_t Intval = Opcodes[0] |
1120 Opcodes[1] << 8 |
1121 Opcodes[2] << 16 |
1122 Opcodes[3] << 24;
1123 EmitIntValue(Intval, Opcodes.size());
Logan Chien2bcc42c2013-01-30 15:39:04 +00001124 }
1125
Logan Chien4ea23b52013-05-10 16:17:24 +00001126 // Switch to the section containing FnStart
1127 SwitchSection(&FnStart->getSection());
1128
Logan Chien2bcc42c2013-01-30 15:39:04 +00001129 // Clean exception handling frame information
1130 Reset();
1131}
1132
Rafael Espindolaa17151a2013-10-08 13:08:17 +00001133void ARMELFStreamer::emitCantUnwind() { CantUnwind = true; }
1134
1135// Add the R_ARM_NONE fixup at the same position
1136void ARMELFStreamer::EmitPersonalityFixup(StringRef Name) {
Jim Grosbach6f482002015-05-18 18:43:14 +00001137 const MCSymbol *PersonalitySym = getContext().getOrCreateSymbol(Name);
Rafael Espindolaa17151a2013-10-08 13:08:17 +00001138
Jim Grosbach13760bd2015-05-30 01:25:56 +00001139 const MCSymbolRefExpr *PersonalityRef = MCSymbolRefExpr::create(
Rafael Espindolaa17151a2013-10-08 13:08:17 +00001140 PersonalitySym, MCSymbolRefExpr::VK_ARM_NONE, getContext());
1141
Rafael Espindola2be12812014-06-25 15:29:54 +00001142 visitUsedExpr(*PersonalityRef);
Rafael Espindolaa17151a2013-10-08 13:08:17 +00001143 MCDataFragment *DF = getOrCreateDataFragment();
Jim Grosbach63661f82015-05-15 19:13:05 +00001144 DF->getFixups().push_back(MCFixup::create(DF->getContents().size(),
Rafael Espindolaa17151a2013-10-08 13:08:17 +00001145 PersonalityRef,
1146 MCFixup::getKindForSize(4, false)));
Logan Chien2bcc42c2013-01-30 15:39:04 +00001147}
1148
Logan Chien325823a2013-06-09 12:22:30 +00001149void ARMELFStreamer::FlushPendingOffset() {
1150 if (PendingOffset != 0) {
1151 UnwindOpAsm.EmitSPOffset(-PendingOffset);
1152 PendingOffset = 0;
1153 }
1154}
1155
Logan Chienc931fce2013-07-02 12:43:27 +00001156void ARMELFStreamer::FlushUnwindOpcodes(bool NoHandlerData) {
Logan Chien325823a2013-06-09 12:22:30 +00001157 // Emit the unwind opcode to restore $sp.
1158 if (UsedFP) {
Bill Wendlingbc07a892013-06-18 07:20:20 +00001159 const MCRegisterInfo *MRI = getContext().getRegisterInfo();
Logan Chien325823a2013-06-09 12:22:30 +00001160 int64_t LastRegSaveSPOffset = SPOffset - PendingOffset;
1161 UnwindOpAsm.EmitSPOffset(LastRegSaveSPOffset - FPOffset);
Bill Wendlingbc07a892013-06-18 07:20:20 +00001162 UnwindOpAsm.EmitSetSP(MRI->getEncodingValue(FPReg));
Logan Chien325823a2013-06-09 12:22:30 +00001163 } else {
1164 FlushPendingOffset();
1165 }
1166
1167 // Finalize the unwind opcode sequence
1168 UnwindOpAsm.Finalize(PersonalityIndex, Opcodes);
Logan Chien4ea23b52013-05-10 16:17:24 +00001169
1170 // For compact model 0, we have to emit the unwind opcodes in the .ARM.exidx
1171 // section. Thus, we don't have to create an entry in the .ARM.extab
1172 // section.
Saleem Abdulrasoolb961c992014-01-06 00:15:00 +00001173 if (NoHandlerData && PersonalityIndex == ARM::EHABI::AEABI_UNWIND_CPP_PR0)
Logan Chien4ea23b52013-05-10 16:17:24 +00001174 return;
1175
1176 // Switch to .ARM.extab section.
Logan Chien2bcc42c2013-01-30 15:39:04 +00001177 SwitchToExTabSection(*FnStart);
1178
1179 // Create .ARM.extab label for offset in .ARM.exidx
1180 assert(!ExTab);
Jim Grosbach6f482002015-05-18 18:43:14 +00001181 ExTab = getContext().createTempSymbol();
Logan Chien2bcc42c2013-01-30 15:39:04 +00001182 EmitLabel(ExTab);
1183
Logan Chien4ea23b52013-05-10 16:17:24 +00001184 // Emit personality
1185 if (Personality) {
1186 const MCSymbolRefExpr *PersonalityRef =
Jim Grosbach13760bd2015-05-30 01:25:56 +00001187 MCSymbolRefExpr::create(Personality,
Logan Chien4ea23b52013-05-10 16:17:24 +00001188 MCSymbolRefExpr::VK_ARM_PREL31,
1189 getContext());
Logan Chien2bcc42c2013-01-30 15:39:04 +00001190
Rafael Espindola64e1af82013-07-02 15:49:13 +00001191 EmitValue(PersonalityRef, 4);
Logan Chien4ea23b52013-05-10 16:17:24 +00001192 }
Logan Chien2bcc42c2013-01-30 15:39:04 +00001193
1194 // Emit unwind opcodes
Christian Pirker39db7ec2014-05-13 16:44:30 +00001195 assert((Opcodes.size() % 4) == 0 &&
1196 "Unwind opcode size for __aeabi_cpp_unwind_pr0 must be multiple of 4");
1197 for (unsigned I = 0; I != Opcodes.size(); I += 4) {
1198 uint64_t Intval = Opcodes[I] |
1199 Opcodes[I + 1] << 8 |
1200 Opcodes[I + 2] << 16 |
1201 Opcodes[I + 3] << 24;
1202 EmitIntValue(Intval, 4);
1203 }
Logan Chienc931fce2013-07-02 12:43:27 +00001204
1205 // According to ARM EHABI section 9.2, if the __aeabi_unwind_cpp_pr1() or
1206 // __aeabi_unwind_cpp_pr2() is used, then the handler data must be emitted
1207 // after the unwind opcodes. The handler data consists of several 32-bit
1208 // words, and should be terminated by zero.
1209 //
1210 // In case that the .handlerdata directive is not specified by the
1211 // programmer, we should emit zero to terminate the handler data.
1212 if (NoHandlerData && !Personality)
1213 EmitIntValue(0, 4);
Logan Chien2bcc42c2013-01-30 15:39:04 +00001214}
1215
Rafael Espindolaa17151a2013-10-08 13:08:17 +00001216void ARMELFStreamer::emitHandlerData() { FlushUnwindOpcodes(false); }
Logan Chien4ea23b52013-05-10 16:17:24 +00001217
Rafael Espindolaa17151a2013-10-08 13:08:17 +00001218void ARMELFStreamer::emitPersonality(const MCSymbol *Per) {
Logan Chien2bcc42c2013-01-30 15:39:04 +00001219 Personality = Per;
Logan Chiend8bb4b72013-04-16 12:02:21 +00001220 UnwindOpAsm.setPersonality(Per);
Logan Chien2bcc42c2013-01-30 15:39:04 +00001221}
1222
Saleem Abdulrasool662f5c12014-01-21 02:33:02 +00001223void ARMELFStreamer::emitPersonalityIndex(unsigned Index) {
1224 assert(Index < ARM::EHABI::NUM_PERSONALITY_INDEX && "invalid index");
1225 PersonalityIndex = Index;
1226}
1227
Rafael Espindolaa17151a2013-10-08 13:08:17 +00001228void ARMELFStreamer::emitSetFP(unsigned NewFPReg, unsigned NewSPReg,
Logan Chien2bcc42c2013-01-30 15:39:04 +00001229 int64_t Offset) {
Logan Chien325823a2013-06-09 12:22:30 +00001230 assert((NewSPReg == ARM::SP || NewSPReg == FPReg) &&
Logan Chiend8bb4b72013-04-16 12:02:21 +00001231 "the operand of .setfp directive should be either $sp or $fp");
1232
1233 UsedFP = true;
Logan Chien325823a2013-06-09 12:22:30 +00001234 FPReg = NewFPReg;
1235
1236 if (NewSPReg == ARM::SP)
1237 FPOffset = SPOffset + Offset;
1238 else
1239 FPOffset += Offset;
Logan Chien2bcc42c2013-01-30 15:39:04 +00001240}
1241
Saleem Abdulrasool5d962d32014-01-30 04:46:24 +00001242void ARMELFStreamer::emitMovSP(unsigned Reg, int64_t Offset) {
1243 assert((Reg != ARM::SP && Reg != ARM::PC) &&
1244 "the operand of .movsp cannot be either sp or pc");
1245 assert(FPReg == ARM::SP && "current FP must be SP");
1246
1247 FlushPendingOffset();
1248
1249 FPReg = Reg;
1250 FPOffset = SPOffset + Offset;
1251
1252 const MCRegisterInfo *MRI = getContext().getRegisterInfo();
1253 UnwindOpAsm.EmitSetSP(MRI->getEncodingValue(FPReg));
1254}
1255
Rafael Espindolaa17151a2013-10-08 13:08:17 +00001256void ARMELFStreamer::emitPad(int64_t Offset) {
Logan Chien325823a2013-06-09 12:22:30 +00001257 // Track the change of the $sp offset
1258 SPOffset -= Offset;
1259
1260 // To squash multiple .pad directives, we should delay the unwind opcode
1261 // until the .save, .vsave, .handlerdata, or .fnend directives.
1262 PendingOffset -= Offset;
Logan Chien2bcc42c2013-01-30 15:39:04 +00001263}
1264
Rafael Espindolaa17151a2013-10-08 13:08:17 +00001265void ARMELFStreamer::emitRegSave(const SmallVectorImpl<unsigned> &RegList,
Logan Chien2bcc42c2013-01-30 15:39:04 +00001266 bool IsVector) {
Logan Chien325823a2013-06-09 12:22:30 +00001267 // Collect the registers in the register list
1268 unsigned Count = 0;
1269 uint32_t Mask = 0;
Bill Wendlingbc07a892013-06-18 07:20:20 +00001270 const MCRegisterInfo *MRI = getContext().getRegisterInfo();
Logan Chiend8bb4b72013-04-16 12:02:21 +00001271 for (size_t i = 0; i < RegList.size(); ++i) {
Bill Wendlingbc07a892013-06-18 07:20:20 +00001272 unsigned Reg = MRI->getEncodingValue(RegList[i]);
Aaron Ballmanab1d27e2013-06-10 16:45:40 +00001273 assert(Reg < (IsVector ? 32U : 16U) && "Register out of range");
Logan Chien325823a2013-06-09 12:22:30 +00001274 unsigned Bit = (1u << Reg);
1275 if ((Mask & Bit) == 0) {
1276 Mask |= Bit;
1277 ++Count;
1278 }
Logan Chiend8bb4b72013-04-16 12:02:21 +00001279 }
Logan Chien325823a2013-06-09 12:22:30 +00001280
1281 // Track the change the $sp offset: For the .save directive, the
1282 // corresponding push instruction will decrease the $sp by (4 * Count).
1283 // For the .vsave directive, the corresponding vpush instruction will
1284 // decrease $sp by (8 * Count).
1285 SPOffset -= Count * (IsVector ? 8 : 4);
1286
1287 // Emit the opcode
1288 FlushPendingOffset();
1289 if (IsVector)
1290 UnwindOpAsm.EmitVFPRegSave(Mask);
1291 else
1292 UnwindOpAsm.EmitRegSave(Mask);
Logan Chien2bcc42c2013-01-30 15:39:04 +00001293}
1294
Saleem Abdulrasoold9f08602014-01-21 02:33:10 +00001295void ARMELFStreamer::emitUnwindRaw(int64_t Offset,
1296 const SmallVectorImpl<uint8_t> &Opcodes) {
1297 FlushPendingOffset();
1298 SPOffset = SPOffset - Offset;
1299 UnwindOpAsm.EmitRaw(Opcodes);
1300}
1301
Tim Northover5cc3dc82012-12-07 16:50:23 +00001302namespace llvm {
Rafael Espindolaa17151a2013-10-08 13:08:17 +00001303
Rafael Espindola73870dd2015-03-16 21:43:42 +00001304MCTargetStreamer *createARMTargetAsmStreamer(MCStreamer &S,
1305 formatted_raw_ostream &OS,
1306 MCInstPrinter *InstPrint,
1307 bool isVerboseAsm) {
1308 return new ARMTargetAsmStreamer(S, OS, *InstPrint, isVerboseAsm);
Rafael Espindolaa17151a2013-10-08 13:08:17 +00001309}
1310
Peter Collingbourne20c72592015-02-19 00:45:02 +00001311MCTargetStreamer *createARMNullTargetStreamer(MCStreamer &S) {
1312 return new ARMTargetStreamer(S);
1313}
1314
Rafael Espindolacd584a82015-03-19 01:50:16 +00001315MCTargetStreamer *createARMObjectTargetStreamer(MCStreamer &S,
1316 const MCSubtargetInfo &STI) {
1317 Triple TT(STI.getTargetTriple());
1318 if (TT.getObjectFormat() == Triple::ELF)
1319 return new ARMTargetELFStreamer(S);
1320 return new ARMTargetStreamer(S);
1321}
1322
Rafael Espindola7b61ddf2014-10-15 16:12:52 +00001323MCELFStreamer *createARMELFStreamer(MCContext &Context, MCAsmBackend &TAB,
Rafael Espindola5560a4c2015-04-14 22:14:34 +00001324 raw_pwrite_stream &OS,
1325 MCCodeEmitter *Emitter, bool RelaxAll,
1326 bool IsThumb) {
Rafael Espindola24ea09e2014-01-26 06:06:37 +00001327 ARMELFStreamer *S = new ARMELFStreamer(Context, TAB, OS, Emitter, IsThumb);
Rafael Espindolaac4ad252013-10-05 16:42:21 +00001328 // FIXME: This should eventually end up somewhere else where more
1329 // intelligent flag decisions can be made. For now we are just maintaining
1330 // the status quo for ARM and setting EF_ARM_EABI_VER5 as the default.
1331 S->getAssembler().setELFHeaderEFlags(ELF::EF_ARM_EABI_VER5);
1332
Tim Northover5cc3dc82012-12-07 16:50:23 +00001333 if (RelaxAll)
1334 S->getAssembler().setRelaxAll(true);
Tim Northover5cc3dc82012-12-07 16:50:23 +00001335 return S;
1336 }
1337
1338}
1339
1340