blob: 35e3dc4c2dfa025e4b05c214cc521bcf854efe2d [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"
Tim Northover5cc3dc82012-12-07 16:50:23 +000026#include "llvm/MC/MCExpr.h"
27#include "llvm/MC/MCInst.h"
Rafael Espindolaa17151a2013-10-08 13:08:17 +000028#include "llvm/MC/MCInstPrinter.h"
Rafael Espindola4c6f6132014-04-27 17:10:46 +000029#include "llvm/MC/MCObjectFileInfo.h"
Tim Northover5cc3dc82012-12-07 16:50:23 +000030#include "llvm/MC/MCObjectStreamer.h"
Logan Chiend8bb4b72013-04-16 12:02:21 +000031#include "llvm/MC/MCRegisterInfo.h"
Tim Northover5cc3dc82012-12-07 16:50:23 +000032#include "llvm/MC/MCSection.h"
Benjamin Kramerf242d8c2012-12-08 10:45:24 +000033#include "llvm/MC/MCSectionELF.h"
34#include "llvm/MC/MCStreamer.h"
Rafael Espindola95fb9b92015-06-02 20:38:46 +000035#include "llvm/MC/MCSymbolELF.h"
Tim Northover5cc3dc82012-12-07 16:50:23 +000036#include "llvm/MC/MCValue.h"
Saleem Abdulrasool278a9f42014-01-19 08:25:27 +000037#include "llvm/Support/ARMBuildAttributes.h"
Saleem Abdulrasoolb961c992014-01-06 00:15:00 +000038#include "llvm/Support/ARMEHABI.h"
Renato Golinf5f373f2015-05-08 21:04:27 +000039#include "llvm/Support/TargetParser.h"
Tim Northover5cc3dc82012-12-07 16:50:23 +000040#include "llvm/Support/Debug.h"
41#include "llvm/Support/ELF.h"
Rafael Espindolaa17151a2013-10-08 13:08:17 +000042#include "llvm/Support/FormattedStream.h"
Logan Chien5b776b72014-02-22 14:00:39 +000043#include "llvm/Support/LEB128.h"
Tim Northover5cc3dc82012-12-07 16:50:23 +000044#include "llvm/Support/raw_ostream.h"
Logan Chien8cbb80d2013-10-28 17:51:12 +000045#include <algorithm>
Tim Northover5cc3dc82012-12-07 16:50:23 +000046
47using namespace llvm;
48
Logan Chiend8bb4b72013-04-16 12:02:21 +000049static std::string GetAEABIUnwindPersonalityName(unsigned Index) {
Saleem Abdulrasoolb961c992014-01-06 00:15:00 +000050 assert(Index < ARM::EHABI::NUM_PERSONALITY_INDEX &&
51 "Invalid personality index");
Logan Chiend8bb4b72013-04-16 12:02:21 +000052 return (Twine("__aeabi_unwind_cpp_pr") + Twine(Index)).str();
53}
54
Tim Northover5cc3dc82012-12-07 16:50:23 +000055namespace {
56
Rafael Espindolaa17151a2013-10-08 13:08:17 +000057class ARMELFStreamer;
58
59class ARMTargetAsmStreamer : public ARMTargetStreamer {
60 formatted_raw_ostream &OS;
61 MCInstPrinter &InstPrinter;
Saleem Abdulrasoolf16e68a2014-01-07 02:28:50 +000062 bool IsVerboseAsm;
Rafael Espindolaa17151a2013-10-08 13:08:17 +000063
Craig Topperca7e3e52014-03-10 03:19:03 +000064 void emitFnStart() override;
65 void emitFnEnd() override;
66 void emitCantUnwind() override;
67 void emitPersonality(const MCSymbol *Personality) override;
68 void emitPersonalityIndex(unsigned Index) override;
69 void emitHandlerData() override;
70 void emitSetFP(unsigned FpReg, unsigned SpReg, int64_t Offset = 0) override;
71 void emitMovSP(unsigned Reg, int64_t Offset = 0) override;
72 void emitPad(int64_t Offset) override;
73 void emitRegSave(const SmallVectorImpl<unsigned> &RegList,
74 bool isVector) override;
75 void emitUnwindRaw(int64_t Offset,
76 const SmallVectorImpl<uint8_t> &Opcodes) override;
Rafael Espindolaa17151a2013-10-08 13:08:17 +000077
Craig Topperca7e3e52014-03-10 03:19:03 +000078 void switchVendor(StringRef Vendor) override;
79 void emitAttribute(unsigned Attribute, unsigned Value) override;
80 void emitTextAttribute(unsigned Attribute, StringRef String) override;
81 void emitIntTextAttribute(unsigned Attribute, unsigned IntValue,
82 StringRef StrinValue) override;
83 void emitArch(unsigned Arch) override;
Sumanth Gundapanenia9049ea2015-02-26 18:07:35 +000084 void emitArchExtension(unsigned ArchExt) override;
Craig Topperca7e3e52014-03-10 03:19:03 +000085 void emitObjectArch(unsigned Arch) override;
86 void emitFPU(unsigned FPU) override;
87 void emitInst(uint32_t Inst, char Suffix = '\0') override;
88 void finishAttributeSection() override;
Logan Chien8cbb80d2013-10-28 17:51:12 +000089
Craig Topperca7e3e52014-03-10 03:19:03 +000090 void AnnotateTLSDescriptorSequence(const MCSymbolRefExpr *SRE) override;
Rafael Espindola466d6632014-04-27 20:23:58 +000091 void emitThumbSet(MCSymbol *Symbol, const MCExpr *Value) override;
Saleem Abdulrasool56e06e82014-01-30 04:02:47 +000092
Rafael Espindolaa17151a2013-10-08 13:08:17 +000093public:
Rafael Espindola24ea09e2014-01-26 06:06:37 +000094 ARMTargetAsmStreamer(MCStreamer &S, formatted_raw_ostream &OS,
95 MCInstPrinter &InstPrinter, bool VerboseAsm);
Rafael Espindolaa17151a2013-10-08 13:08:17 +000096};
97
Rafael Espindola24ea09e2014-01-26 06:06:37 +000098ARMTargetAsmStreamer::ARMTargetAsmStreamer(MCStreamer &S,
99 formatted_raw_ostream &OS,
Saleem Abdulrasoolf16e68a2014-01-07 02:28:50 +0000100 MCInstPrinter &InstPrinter,
101 bool VerboseAsm)
Rafael Espindola24ea09e2014-01-26 06:06:37 +0000102 : ARMTargetStreamer(S), OS(OS), InstPrinter(InstPrinter),
103 IsVerboseAsm(VerboseAsm) {}
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000104void ARMTargetAsmStreamer::emitFnStart() { OS << "\t.fnstart\n"; }
105void ARMTargetAsmStreamer::emitFnEnd() { OS << "\t.fnend\n"; }
106void ARMTargetAsmStreamer::emitCantUnwind() { OS << "\t.cantunwind\n"; }
107void ARMTargetAsmStreamer::emitPersonality(const MCSymbol *Personality) {
108 OS << "\t.personality " << Personality->getName() << '\n';
109}
Saleem Abdulrasool662f5c12014-01-21 02:33:02 +0000110void ARMTargetAsmStreamer::emitPersonalityIndex(unsigned Index) {
111 OS << "\t.personalityindex " << Index << '\n';
112}
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000113void ARMTargetAsmStreamer::emitHandlerData() { OS << "\t.handlerdata\n"; }
114void ARMTargetAsmStreamer::emitSetFP(unsigned FpReg, unsigned SpReg,
115 int64_t Offset) {
116 OS << "\t.setfp\t";
117 InstPrinter.printRegName(OS, FpReg);
118 OS << ", ";
119 InstPrinter.printRegName(OS, SpReg);
120 if (Offset)
121 OS << ", #" << Offset;
122 OS << '\n';
123}
Saleem Abdulrasool5d962d32014-01-30 04:46:24 +0000124void ARMTargetAsmStreamer::emitMovSP(unsigned Reg, int64_t Offset) {
125 assert((Reg != ARM::SP && Reg != ARM::PC) &&
126 "the operand of .movsp cannot be either sp or pc");
127
128 OS << "\t.movsp\t";
129 InstPrinter.printRegName(OS, Reg);
130 if (Offset)
131 OS << ", #" << Offset;
132 OS << '\n';
133}
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000134void ARMTargetAsmStreamer::emitPad(int64_t Offset) {
135 OS << "\t.pad\t#" << Offset << '\n';
136}
137void ARMTargetAsmStreamer::emitRegSave(const SmallVectorImpl<unsigned> &RegList,
138 bool isVector) {
139 assert(RegList.size() && "RegList should not be empty");
140 if (isVector)
141 OS << "\t.vsave\t{";
142 else
143 OS << "\t.save\t{";
144
145 InstPrinter.printRegName(OS, RegList[0]);
146
147 for (unsigned i = 1, e = RegList.size(); i != e; ++i) {
148 OS << ", ";
149 InstPrinter.printRegName(OS, RegList[i]);
150 }
151
152 OS << "}\n";
153}
Logan Chien8cbb80d2013-10-28 17:51:12 +0000154void ARMTargetAsmStreamer::switchVendor(StringRef Vendor) {
155}
156void ARMTargetAsmStreamer::emitAttribute(unsigned Attribute, unsigned Value) {
Saleem Abdulrasoolf16e68a2014-01-07 02:28:50 +0000157 OS << "\t.eabi_attribute\t" << Attribute << ", " << Twine(Value);
158 if (IsVerboseAsm) {
159 StringRef Name = ARMBuildAttrs::AttrTypeAsString(Attribute);
160 if (!Name.empty())
161 OS << "\t@ " << Name;
162 }
163 OS << "\n";
Logan Chien8cbb80d2013-10-28 17:51:12 +0000164}
165void ARMTargetAsmStreamer::emitTextAttribute(unsigned Attribute,
166 StringRef String) {
167 switch (Attribute) {
Logan Chien8cbb80d2013-10-28 17:51:12 +0000168 case ARMBuildAttrs::CPU_name:
Saleem Abdulrasool87ccd362014-01-07 02:28:42 +0000169 OS << "\t.cpu\t" << String.lower();
170 break;
171 default:
172 OS << "\t.eabi_attribute\t" << Attribute << ", \"" << String << "\"";
Saleem Abdulrasoolf16e68a2014-01-07 02:28:50 +0000173 if (IsVerboseAsm) {
174 StringRef Name = ARMBuildAttrs::AttrTypeAsString(Attribute);
175 if (!Name.empty())
176 OS << "\t@ " << Name;
177 }
Logan Chien8cbb80d2013-10-28 17:51:12 +0000178 break;
179 }
Saleem Abdulrasool87ccd362014-01-07 02:28:42 +0000180 OS << "\n";
181}
182void ARMTargetAsmStreamer::emitIntTextAttribute(unsigned Attribute,
183 unsigned IntValue,
184 StringRef StringValue) {
185 switch (Attribute) {
186 default: llvm_unreachable("unsupported multi-value attribute in asm mode");
187 case ARMBuildAttrs::compatibility:
188 OS << "\t.eabi_attribute\t" << Attribute << ", " << IntValue;
189 if (!StringValue.empty())
190 OS << ", \"" << StringValue << "\"";
Saleem Abdulrasoolf16e68a2014-01-07 02:28:50 +0000191 if (IsVerboseAsm)
192 OS << "\t@ " << ARMBuildAttrs::AttrTypeAsString(Attribute);
Saleem Abdulrasool87ccd362014-01-07 02:28:42 +0000193 break;
194 }
195 OS << "\n";
Logan Chien8cbb80d2013-10-28 17:51:12 +0000196}
Logan Chien439e8f92013-12-11 17:16:25 +0000197void ARMTargetAsmStreamer::emitArch(unsigned Arch) {
Renato Golinf5f373f2015-05-08 21:04:27 +0000198 OS << "\t.arch\t" << ARMTargetParser::getArchName(Arch) << "\n";
Logan Chien439e8f92013-12-11 17:16:25 +0000199}
Sumanth Gundapanenia9049ea2015-02-26 18:07:35 +0000200void ARMTargetAsmStreamer::emitArchExtension(unsigned ArchExt) {
Renato Golinf5f373f2015-05-08 21:04:27 +0000201 OS << "\t.arch_extension\t" << ARMTargetParser::getArchExtName(ArchExt) << "\n";
Sumanth Gundapanenia9049ea2015-02-26 18:07:35 +0000202}
Saleem Abdulrasool4c4789b2014-01-30 04:46:41 +0000203void ARMTargetAsmStreamer::emitObjectArch(unsigned Arch) {
Renato Golinf5f373f2015-05-08 21:04:27 +0000204 OS << "\t.object_arch\t" << ARMTargetParser::getArchName(Arch) << '\n';
Saleem Abdulrasool4c4789b2014-01-30 04:46:41 +0000205}
Logan Chien8cbb80d2013-10-28 17:51:12 +0000206void ARMTargetAsmStreamer::emitFPU(unsigned FPU) {
Renato Golinf5f373f2015-05-08 21:04:27 +0000207 OS << "\t.fpu\t" << ARMTargetParser::getFPUName(FPU) << "\n";
Logan Chien8cbb80d2013-10-28 17:51:12 +0000208}
209void ARMTargetAsmStreamer::finishAttributeSection() {
210}
Saleem Abdulrasool56e06e82014-01-30 04:02:47 +0000211void
212ARMTargetAsmStreamer::AnnotateTLSDescriptorSequence(const MCSymbolRefExpr *S) {
213 OS << "\t.tlsdescseq\t" << S->getSymbol().getName();
214}
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000215
Rafael Espindola466d6632014-04-27 20:23:58 +0000216void ARMTargetAsmStreamer::emitThumbSet(MCSymbol *Symbol, const MCExpr *Value) {
217 OS << "\t.thumb_set\t" << *Symbol << ", " << *Value << '\n';
218}
219
Saleem Abdulrasoolc0da2cb2013-12-19 05:17:58 +0000220void ARMTargetAsmStreamer::emitInst(uint32_t Inst, char Suffix) {
221 OS << "\t.inst";
222 if (Suffix)
223 OS << "." << Suffix;
Benjamin Kramerbe48c402015-05-23 16:39:10 +0000224 OS << "\t0x" << Twine::utohexstr(Inst) << "\n";
Saleem Abdulrasoolc0da2cb2013-12-19 05:17:58 +0000225}
226
Saleem Abdulrasoold9f08602014-01-21 02:33:10 +0000227void ARMTargetAsmStreamer::emitUnwindRaw(int64_t Offset,
228 const SmallVectorImpl<uint8_t> &Opcodes) {
229 OS << "\t.unwind_raw " << Offset;
230 for (SmallVectorImpl<uint8_t>::const_iterator OCI = Opcodes.begin(),
231 OCE = Opcodes.end();
232 OCI != OCE; ++OCI)
Benjamin Kramerbe48c402015-05-23 16:39:10 +0000233 OS << ", 0x" << Twine::utohexstr(*OCI);
Saleem Abdulrasoold9f08602014-01-21 02:33:10 +0000234 OS << '\n';
235}
236
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000237class ARMTargetELFStreamer : public ARMTargetStreamer {
Logan Chien8cbb80d2013-10-28 17:51:12 +0000238private:
239 // This structure holds all attributes, accounting for
240 // their string/numeric value, so we can later emmit them
241 // in declaration order, keeping all in the same vector
242 struct AttributeItem {
243 enum {
244 HiddenAttribute = 0,
245 NumericAttribute,
Saleem Abdulrasool87ccd362014-01-07 02:28:42 +0000246 TextAttribute,
247 NumericAndTextAttributes
Logan Chien8cbb80d2013-10-28 17:51:12 +0000248 } Type;
249 unsigned Tag;
250 unsigned IntValue;
251 StringRef StringValue;
252
253 static bool LessTag(const AttributeItem &LHS, const AttributeItem &RHS) {
Charlie Turner8b2caa42015-01-05 13:12:17 +0000254 // The conformance tag must be emitted first when serialised
255 // into an object file. Specifically, the addenda to the ARM ABI
256 // states that (2.3.7.4):
257 //
258 // "To simplify recognition by consumers in the common case of
259 // claiming conformity for the whole file, this tag should be
260 // emitted first in a file-scope sub-subsection of the first
261 // public subsection of the attributes section."
262 //
263 // So it is special-cased in this comparison predicate when the
264 // attributes are sorted in finishAttributeSection().
265 return (RHS.Tag != ARMBuildAttrs::conformance) &&
266 ((LHS.Tag == ARMBuildAttrs::conformance) || (LHS.Tag < RHS.Tag));
Logan Chien8cbb80d2013-10-28 17:51:12 +0000267 }
268 };
269
270 StringRef CurrentVendor;
271 unsigned FPU;
Logan Chien439e8f92013-12-11 17:16:25 +0000272 unsigned Arch;
Saleem Abdulrasool4c4789b2014-01-30 04:46:41 +0000273 unsigned EmittedArch;
Logan Chien8cbb80d2013-10-28 17:51:12 +0000274 SmallVector<AttributeItem, 64> Contents;
275
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000276 MCSection *AttributeSection;
Logan Chien8cbb80d2013-10-28 17:51:12 +0000277
Logan Chien8cbb80d2013-10-28 17:51:12 +0000278 AttributeItem *getAttributeItem(unsigned Attribute) {
279 for (size_t i = 0; i < Contents.size(); ++i)
280 if (Contents[i].Tag == Attribute)
281 return &Contents[i];
Craig Topper062a2ba2014-04-25 05:30:21 +0000282 return nullptr;
Logan Chien8cbb80d2013-10-28 17:51:12 +0000283 }
284
285 void setAttributeItem(unsigned Attribute, unsigned Value,
286 bool OverwriteExisting) {
287 // Look for existing attribute item
288 if (AttributeItem *Item = getAttributeItem(Attribute)) {
289 if (!OverwriteExisting)
290 return;
Saleem Abdulrasool93900052014-01-19 08:25:41 +0000291 Item->Type = AttributeItem::NumericAttribute;
Logan Chien8cbb80d2013-10-28 17:51:12 +0000292 Item->IntValue = Value;
293 return;
294 }
295
296 // Create new attribute item
297 AttributeItem Item = {
298 AttributeItem::NumericAttribute,
299 Attribute,
300 Value,
301 StringRef("")
302 };
303 Contents.push_back(Item);
304 }
305
306 void setAttributeItem(unsigned Attribute, StringRef Value,
307 bool OverwriteExisting) {
308 // Look for existing attribute item
309 if (AttributeItem *Item = getAttributeItem(Attribute)) {
310 if (!OverwriteExisting)
311 return;
Saleem Abdulrasool93900052014-01-19 08:25:41 +0000312 Item->Type = AttributeItem::TextAttribute;
Logan Chien8cbb80d2013-10-28 17:51:12 +0000313 Item->StringValue = Value;
314 return;
315 }
316
317 // Create new attribute item
318 AttributeItem Item = {
319 AttributeItem::TextAttribute,
320 Attribute,
321 0,
322 Value
323 };
324 Contents.push_back(Item);
325 }
326
Saleem Abdulrasool87ccd362014-01-07 02:28:42 +0000327 void setAttributeItems(unsigned Attribute, unsigned IntValue,
328 StringRef StringValue, bool OverwriteExisting) {
329 // Look for existing attribute item
330 if (AttributeItem *Item = getAttributeItem(Attribute)) {
331 if (!OverwriteExisting)
332 return;
Saleem Abdulrasool93900052014-01-19 08:25:41 +0000333 Item->Type = AttributeItem::NumericAndTextAttributes;
Saleem Abdulrasool87ccd362014-01-07 02:28:42 +0000334 Item->IntValue = IntValue;
335 Item->StringValue = StringValue;
336 return;
337 }
338
339 // Create new attribute item
340 AttributeItem Item = {
341 AttributeItem::NumericAndTextAttributes,
342 Attribute,
343 IntValue,
344 StringValue
345 };
346 Contents.push_back(Item);
347 }
348
Logan Chien439e8f92013-12-11 17:16:25 +0000349 void emitArchDefaultAttributes();
Logan Chien8cbb80d2013-10-28 17:51:12 +0000350 void emitFPUDefaultAttributes();
351
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000352 ARMELFStreamer &getStreamer();
Logan Chien8cbb80d2013-10-28 17:51:12 +0000353
Craig Topperca7e3e52014-03-10 03:19:03 +0000354 void emitFnStart() override;
355 void emitFnEnd() override;
356 void emitCantUnwind() override;
357 void emitPersonality(const MCSymbol *Personality) override;
358 void emitPersonalityIndex(unsigned Index) override;
359 void emitHandlerData() override;
360 void emitSetFP(unsigned FpReg, unsigned SpReg, int64_t Offset = 0) override;
361 void emitMovSP(unsigned Reg, int64_t Offset = 0) override;
362 void emitPad(int64_t Offset) override;
363 void emitRegSave(const SmallVectorImpl<unsigned> &RegList,
364 bool isVector) override;
365 void emitUnwindRaw(int64_t Offset,
366 const SmallVectorImpl<uint8_t> &Opcodes) override;
Logan Chien8cbb80d2013-10-28 17:51:12 +0000367
Craig Topperca7e3e52014-03-10 03:19:03 +0000368 void switchVendor(StringRef Vendor) override;
369 void emitAttribute(unsigned Attribute, unsigned Value) override;
370 void emitTextAttribute(unsigned Attribute, StringRef String) override;
371 void emitIntTextAttribute(unsigned Attribute, unsigned IntValue,
372 StringRef StringValue) override;
373 void emitArch(unsigned Arch) override;
374 void emitObjectArch(unsigned Arch) override;
375 void emitFPU(unsigned FPU) override;
376 void emitInst(uint32_t Inst, char Suffix = '\0') override;
377 void finishAttributeSection() override;
Rafael Espindola4c6f6132014-04-27 17:10:46 +0000378 void emitLabel(MCSymbol *Symbol) override;
Logan Chien8cbb80d2013-10-28 17:51:12 +0000379
Craig Topperca7e3e52014-03-10 03:19:03 +0000380 void AnnotateTLSDescriptorSequence(const MCSymbolRefExpr *SRE) override;
Rafael Espindola466d6632014-04-27 20:23:58 +0000381 void emitThumbSet(MCSymbol *Symbol, const MCExpr *Value) override;
Saleem Abdulrasool56e06e82014-01-30 04:02:47 +0000382
Logan Chien8cbb80d2013-10-28 17:51:12 +0000383 size_t calculateContentSize() const;
384
385public:
Rafael Espindola24ea09e2014-01-26 06:06:37 +0000386 ARMTargetELFStreamer(MCStreamer &S)
Renato Golin35de35d2015-05-12 10:33:58 +0000387 : ARMTargetStreamer(S), CurrentVendor("aeabi"), FPU(ARM::FK_INVALID),
388 Arch(ARM::AK_INVALID), EmittedArch(ARM::AK_INVALID),
Craig Topper062a2ba2014-04-25 05:30:21 +0000389 AttributeSection(nullptr) {}
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000390};
391
Tim Northover5cc3dc82012-12-07 16:50:23 +0000392/// Extend the generic ELFStreamer class so that it can emit mapping symbols at
393/// the appropriate points in the object files. These symbols are defined in the
394/// ARM ELF ABI: infocenter.arm.com/help/topic/com.arm.../IHI0044D_aaelf.pdf.
395///
396/// In brief: $a, $t or $d should be emitted at the start of each contiguous
397/// region of ARM code, Thumb code or data in a section. In practice, this
398/// emission does not rely on explicit assembler directives but on inherent
399/// properties of the directives doing the emission (e.g. ".byte" is data, "add
400/// r0, r0, r0" an instruction).
401///
402/// As a result this system is orthogonal to the DataRegion infrastructure used
403/// by MachO. Beware!
404class ARMELFStreamer : public MCELFStreamer {
405public:
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000406 friend class ARMTargetELFStreamer;
407
Rafael Espindola5560a4c2015-04-14 22:14:34 +0000408 ARMELFStreamer(MCContext &Context, MCAsmBackend &TAB, raw_pwrite_stream &OS,
Rafael Espindola24ea09e2014-01-26 06:06:37 +0000409 MCCodeEmitter *Emitter, bool IsThumb)
410 : MCELFStreamer(Context, TAB, OS, Emitter), IsThumb(IsThumb),
411 MappingSymbolCounter(0), LastEMS(EMS_None) {
Logan Chiend8bb4b72013-04-16 12:02:21 +0000412 Reset();
413 }
Tim Northover5cc3dc82012-12-07 16:50:23 +0000414
415 ~ARMELFStreamer() {}
416
Craig Topperca7e3e52014-03-10 03:19:03 +0000417 void FinishImpl() override;
Logan Chien8cbb80d2013-10-28 17:51:12 +0000418
Logan Chien2bcc42c2013-01-30 15:39:04 +0000419 // ARM exception handling directives
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000420 void emitFnStart();
421 void emitFnEnd();
422 void emitCantUnwind();
423 void emitPersonality(const MCSymbol *Per);
Saleem Abdulrasool662f5c12014-01-21 02:33:02 +0000424 void emitPersonalityIndex(unsigned index);
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000425 void emitHandlerData();
426 void emitSetFP(unsigned NewFpReg, unsigned NewSpReg, int64_t Offset = 0);
Saleem Abdulrasool5d962d32014-01-30 04:46:24 +0000427 void emitMovSP(unsigned Reg, int64_t Offset = 0);
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000428 void emitPad(int64_t Offset);
429 void emitRegSave(const SmallVectorImpl<unsigned> &RegList, bool isVector);
Saleem Abdulrasoold9f08602014-01-21 02:33:10 +0000430 void emitUnwindRaw(int64_t Offset, const SmallVectorImpl<uint8_t> &Opcodes);
Logan Chien2bcc42c2013-01-30 15:39:04 +0000431
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000432 void ChangeSection(MCSection *Section, const MCExpr *Subsection) override {
Tim Northover5cc3dc82012-12-07 16:50:23 +0000433 // We have to keep track of the mapping symbol state of any sections we
434 // use. Each one should start off as EMS_None, which is provided as the
435 // default constructor by DenseMap::lookup.
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000436 LastMappingSymbols[getPreviousSection().first] = LastEMS;
Tim Northover5cc3dc82012-12-07 16:50:23 +0000437 LastEMS = LastMappingSymbols.lookup(Section);
438
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000439 MCELFStreamer::ChangeSection(Section, Subsection);
Tim Northover5cc3dc82012-12-07 16:50:23 +0000440 }
441
442 /// This function is the one used to emit instruction data into the ELF
443 /// streamer. We override it to add the appropriate mapping symbol if
444 /// necessary.
Craig Topperca7e3e52014-03-10 03:19:03 +0000445 void EmitInstruction(const MCInst& Inst,
446 const MCSubtargetInfo &STI) override {
Tim Northover5cc3dc82012-12-07 16:50:23 +0000447 if (IsThumb)
448 EmitThumbMappingSymbol();
449 else
450 EmitARMMappingSymbol();
451
David Woodhousee6c13e42014-01-28 23:12:42 +0000452 MCELFStreamer::EmitInstruction(Inst, STI);
Tim Northover5cc3dc82012-12-07 16:50:23 +0000453 }
454
Craig Topperd25ff6f2014-03-10 03:22:59 +0000455 void emitInst(uint32_t Inst, char Suffix) {
Saleem Abdulrasoolc0da2cb2013-12-19 05:17:58 +0000456 unsigned Size;
457 char Buffer[4];
458 const bool LittleEndian = getContext().getAsmInfo()->isLittleEndian();
459
460 switch (Suffix) {
461 case '\0':
462 Size = 4;
463
464 assert(!IsThumb);
465 EmitARMMappingSymbol();
466 for (unsigned II = 0, IE = Size; II != IE; II++) {
467 const unsigned I = LittleEndian ? (Size - II - 1) : II;
468 Buffer[Size - II - 1] = uint8_t(Inst >> I * CHAR_BIT);
469 }
470
471 break;
472 case 'n':
473 case 'w':
474 Size = (Suffix == 'n' ? 2 : 4);
475
476 assert(IsThumb);
477 EmitThumbMappingSymbol();
478 for (unsigned II = 0, IE = Size; II != IE; II = II + 2) {
479 const unsigned I0 = LittleEndian ? II + 0 : (Size - II - 1);
480 const unsigned I1 = LittleEndian ? II + 1 : (Size - II - 2);
481 Buffer[Size - II - 2] = uint8_t(Inst >> I0 * CHAR_BIT);
482 Buffer[Size - II - 1] = uint8_t(Inst >> I1 * CHAR_BIT);
483 }
484
485 break;
486 default:
487 llvm_unreachable("Invalid Suffix");
488 }
489
490 MCELFStreamer::EmitBytes(StringRef(Buffer, Size));
491 }
492
Tim Northover5cc3dc82012-12-07 16:50:23 +0000493 /// This is one of the functions used to emit data into an ELF section, so the
494 /// ARM streamer overrides it to add the appropriate mapping symbol ($d) if
495 /// necessary.
Craig Topperca7e3e52014-03-10 03:19:03 +0000496 void EmitBytes(StringRef Data) override {
Tim Northover5cc3dc82012-12-07 16:50:23 +0000497 EmitDataMappingSymbol();
Rafael Espindola64e1af82013-07-02 15:49:13 +0000498 MCELFStreamer::EmitBytes(Data);
Tim Northover5cc3dc82012-12-07 16:50:23 +0000499 }
500
501 /// This is one of the functions used to emit data into an ELF section, so the
502 /// ARM streamer overrides it to add the appropriate mapping symbol ($d) if
503 /// necessary.
Kevin Enderby96918bc2014-04-22 17:27:29 +0000504 void EmitValueImpl(const MCExpr *Value, unsigned Size,
505 const SMLoc &Loc) override {
Saleem Abdulrasoolfe781972015-01-11 04:39:18 +0000506 if (const MCSymbolRefExpr *SRE = dyn_cast_or_null<MCSymbolRefExpr>(Value))
507 if (SRE->getKind() == MCSymbolRefExpr::VK_ARM_SBREL && !(Size == 4))
Jim Grosbach6f482002015-05-18 18:43:14 +0000508 getContext().reportFatalError(Loc, "relocated expression must be 32-bit");
Saleem Abdulrasoolfe781972015-01-11 04:39:18 +0000509
Tim Northover5cc3dc82012-12-07 16:50:23 +0000510 EmitDataMappingSymbol();
Rafael Espindola64e1af82013-07-02 15:49:13 +0000511 MCELFStreamer::EmitValueImpl(Value, Size);
Tim Northover5cc3dc82012-12-07 16:50:23 +0000512 }
513
Craig Topperca7e3e52014-03-10 03:19:03 +0000514 void EmitAssemblerFlag(MCAssemblerFlag Flag) override {
Tim Northover5cc3dc82012-12-07 16:50:23 +0000515 MCELFStreamer::EmitAssemblerFlag(Flag);
516
517 switch (Flag) {
518 case MCAF_SyntaxUnified:
519 return; // no-op here.
520 case MCAF_Code16:
521 IsThumb = true;
522 return; // Change to Thumb mode
523 case MCAF_Code32:
524 IsThumb = false;
525 return; // Change to ARM mode
526 case MCAF_Code64:
527 return;
528 case MCAF_SubsectionsViaSymbols:
529 return;
530 }
531 }
532
533private:
534 enum ElfMappingSymbol {
535 EMS_None,
536 EMS_ARM,
537 EMS_Thumb,
538 EMS_Data
539 };
540
541 void EmitDataMappingSymbol() {
542 if (LastEMS == EMS_Data) return;
543 EmitMappingSymbol("$d");
544 LastEMS = EMS_Data;
545 }
546
547 void EmitThumbMappingSymbol() {
548 if (LastEMS == EMS_Thumb) return;
549 EmitMappingSymbol("$t");
550 LastEMS = EMS_Thumb;
551 }
552
553 void EmitARMMappingSymbol() {
554 if (LastEMS == EMS_ARM) return;
555 EmitMappingSymbol("$a");
556 LastEMS = EMS_ARM;
557 }
558
559 void EmitMappingSymbol(StringRef Name) {
Jim Grosbach6f482002015-05-18 18:43:14 +0000560 MCSymbol *Start = getContext().createTempSymbol();
Tim Northover5cc3dc82012-12-07 16:50:23 +0000561 EmitLabel(Start);
562
Rafael Espindola95fb9b92015-06-02 20:38:46 +0000563 auto *Symbol = cast<MCSymbolELF>(getContext().getOrCreateSymbol(
564 Name + "." + Twine(MappingSymbolCounter++)));
Tim Northover5cc3dc82012-12-07 16:50:23 +0000565
Rafael Espindolab5d316b2015-05-29 20:21:02 +0000566 getAssembler().registerSymbol(*Symbol);
Rafael Espindola95fb9b92015-06-02 20:38:46 +0000567 Symbol->setType(ELF::STT_NOTYPE);
568 Symbol->setBinding(ELF::STB_LOCAL);
Rafael Espindola4d37b2a2015-05-29 21:45:01 +0000569 Symbol->setExternal(false);
Richard Mitton21101b32013-09-19 23:21:01 +0000570 AssignSection(Symbol, getCurrentSection().first);
Tim Northover5cc3dc82012-12-07 16:50:23 +0000571
Jim Grosbach13760bd2015-05-30 01:25:56 +0000572 const MCExpr *Value = MCSymbolRefExpr::create(Start, getContext());
Tim Northover5cc3dc82012-12-07 16:50:23 +0000573 Symbol->setVariableValue(Value);
574 }
575
Craig Topperca7e3e52014-03-10 03:19:03 +0000576 void EmitThumbFunc(MCSymbol *Func) override {
Tim Northover5cc3dc82012-12-07 16:50:23 +0000577 getAssembler().setIsThumbFunc(Func);
Rafael Espindolab60c8292014-04-29 12:46:50 +0000578 EmitSymbolAttribute(Func, MCSA_ELF_TypeFunction);
Tim Northover5cc3dc82012-12-07 16:50:23 +0000579 }
580
Logan Chien2bcc42c2013-01-30 15:39:04 +0000581 // Helper functions for ARM exception handling directives
582 void Reset();
583
584 void EmitPersonalityFixup(StringRef Name);
Logan Chien325823a2013-06-09 12:22:30 +0000585 void FlushPendingOffset();
Logan Chienc931fce2013-07-02 12:43:27 +0000586 void FlushUnwindOpcodes(bool NoHandlerData);
Logan Chien2bcc42c2013-01-30 15:39:04 +0000587
588 void SwitchToEHSection(const char *Prefix, unsigned Type, unsigned Flags,
589 SectionKind Kind, const MCSymbol &Fn);
590 void SwitchToExTabSection(const MCSymbol &FnStart);
591 void SwitchToExIdxSection(const MCSymbol &FnStart);
Tim Northover5cc3dc82012-12-07 16:50:23 +0000592
Saleem Abdulrasool56e06e82014-01-30 04:02:47 +0000593 void EmitFixup(const MCExpr *Expr, MCFixupKind Kind);
594
Tim Northover5cc3dc82012-12-07 16:50:23 +0000595 bool IsThumb;
596 int64_t MappingSymbolCounter;
597
598 DenseMap<const MCSection *, ElfMappingSymbol> LastMappingSymbols;
599 ElfMappingSymbol LastEMS;
600
Logan Chien2bcc42c2013-01-30 15:39:04 +0000601 // ARM Exception Handling Frame Information
602 MCSymbol *ExTab;
603 MCSymbol *FnStart;
604 const MCSymbol *Personality;
Logan Chien325823a2013-06-09 12:22:30 +0000605 unsigned PersonalityIndex;
606 unsigned FPReg; // Frame pointer register
607 int64_t FPOffset; // Offset: (final frame pointer) - (initial $sp)
608 int64_t SPOffset; // Offset: (final $sp) - (initial $sp)
609 int64_t PendingOffset; // Offset: (final $sp) - (emitted $sp)
Logan Chiend8bb4b72013-04-16 12:02:21 +0000610 bool UsedFP;
Logan Chien2bcc42c2013-01-30 15:39:04 +0000611 bool CantUnwind;
Logan Chien325823a2013-06-09 12:22:30 +0000612 SmallVector<uint8_t, 64> Opcodes;
Logan Chiend8bb4b72013-04-16 12:02:21 +0000613 UnwindOpcodeAssembler UnwindOpAsm;
Tim Northover5cc3dc82012-12-07 16:50:23 +0000614};
Logan Chiend8bb4b72013-04-16 12:02:21 +0000615} // end anonymous namespace
Tim Northover5cc3dc82012-12-07 16:50:23 +0000616
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000617ARMELFStreamer &ARMTargetELFStreamer::getStreamer() {
Rafael Espindola24ea09e2014-01-26 06:06:37 +0000618 return static_cast<ARMELFStreamer &>(Streamer);
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000619}
620
621void ARMTargetELFStreamer::emitFnStart() { getStreamer().emitFnStart(); }
622void ARMTargetELFStreamer::emitFnEnd() { getStreamer().emitFnEnd(); }
623void ARMTargetELFStreamer::emitCantUnwind() { getStreamer().emitCantUnwind(); }
624void ARMTargetELFStreamer::emitPersonality(const MCSymbol *Personality) {
625 getStreamer().emitPersonality(Personality);
626}
Saleem Abdulrasool662f5c12014-01-21 02:33:02 +0000627void ARMTargetELFStreamer::emitPersonalityIndex(unsigned Index) {
628 getStreamer().emitPersonalityIndex(Index);
629}
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000630void ARMTargetELFStreamer::emitHandlerData() {
631 getStreamer().emitHandlerData();
632}
633void ARMTargetELFStreamer::emitSetFP(unsigned FpReg, unsigned SpReg,
634 int64_t Offset) {
635 getStreamer().emitSetFP(FpReg, SpReg, Offset);
636}
Saleem Abdulrasool5d962d32014-01-30 04:46:24 +0000637void ARMTargetELFStreamer::emitMovSP(unsigned Reg, int64_t Offset) {
638 getStreamer().emitMovSP(Reg, Offset);
639}
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000640void ARMTargetELFStreamer::emitPad(int64_t Offset) {
641 getStreamer().emitPad(Offset);
642}
643void ARMTargetELFStreamer::emitRegSave(const SmallVectorImpl<unsigned> &RegList,
644 bool isVector) {
645 getStreamer().emitRegSave(RegList, isVector);
646}
Saleem Abdulrasoold9f08602014-01-21 02:33:10 +0000647void ARMTargetELFStreamer::emitUnwindRaw(int64_t Offset,
648 const SmallVectorImpl<uint8_t> &Opcodes) {
649 getStreamer().emitUnwindRaw(Offset, Opcodes);
650}
Logan Chien8cbb80d2013-10-28 17:51:12 +0000651void ARMTargetELFStreamer::switchVendor(StringRef Vendor) {
652 assert(!Vendor.empty() && "Vendor cannot be empty.");
653
654 if (CurrentVendor == Vendor)
655 return;
656
657 if (!CurrentVendor.empty())
658 finishAttributeSection();
659
660 assert(Contents.empty() &&
661 ".ARM.attributes should be flushed before changing vendor");
662 CurrentVendor = Vendor;
663
664}
665void ARMTargetELFStreamer::emitAttribute(unsigned Attribute, unsigned Value) {
666 setAttributeItem(Attribute, Value, /* OverwriteExisting= */ true);
667}
668void ARMTargetELFStreamer::emitTextAttribute(unsigned Attribute,
669 StringRef Value) {
670 setAttributeItem(Attribute, Value, /* OverwriteExisting= */ true);
671}
Saleem Abdulrasool87ccd362014-01-07 02:28:42 +0000672void ARMTargetELFStreamer::emitIntTextAttribute(unsigned Attribute,
673 unsigned IntValue,
674 StringRef StringValue) {
675 setAttributeItems(Attribute, IntValue, StringValue,
676 /* OverwriteExisting= */ true);
677}
Logan Chien439e8f92013-12-11 17:16:25 +0000678void ARMTargetELFStreamer::emitArch(unsigned Value) {
679 Arch = Value;
680}
Saleem Abdulrasool4c4789b2014-01-30 04:46:41 +0000681void ARMTargetELFStreamer::emitObjectArch(unsigned Value) {
682 EmittedArch = Value;
683}
Logan Chien439e8f92013-12-11 17:16:25 +0000684void ARMTargetELFStreamer::emitArchDefaultAttributes() {
685 using namespace ARMBuildAttrs;
Saleem Abdulrasool4c4789b2014-01-30 04:46:41 +0000686
Renato Golinf5f373f2015-05-08 21:04:27 +0000687 setAttributeItem(CPU_name,
Renato Golinf7c0d5f2015-05-27 18:15:37 +0000688 ARMTargetParser::getCPUAttr(Arch),
Renato Golinf5f373f2015-05-08 21:04:27 +0000689 false);
690
Renato Golin35de35d2015-05-12 10:33:58 +0000691 if (EmittedArch == ARM::AK_INVALID)
Renato Golinf5f373f2015-05-08 21:04:27 +0000692 setAttributeItem(CPU_arch,
Renato Golinf7c0d5f2015-05-27 18:15:37 +0000693 ARMTargetParser::getArchAttr(Arch),
Renato Golinf5f373f2015-05-08 21:04:27 +0000694 false);
Saleem Abdulrasool4c4789b2014-01-30 04:46:41 +0000695 else
Renato Golinf5f373f2015-05-08 21:04:27 +0000696 setAttributeItem(CPU_arch,
Renato Golinf7c0d5f2015-05-27 18:15:37 +0000697 ARMTargetParser::getArchAttr(EmittedArch),
Renato Golinf5f373f2015-05-08 21:04:27 +0000698 false);
Logan Chien439e8f92013-12-11 17:16:25 +0000699
700 switch (Arch) {
Renato Golin35de35d2015-05-12 10:33:58 +0000701 case ARM::AK_ARMV2:
702 case ARM::AK_ARMV2A:
703 case ARM::AK_ARMV3:
704 case ARM::AK_ARMV3M:
705 case ARM::AK_ARMV4:
706 case ARM::AK_ARMV5:
Logan Chien439e8f92013-12-11 17:16:25 +0000707 setAttributeItem(ARM_ISA_use, Allowed, false);
708 break;
709
Renato Golin35de35d2015-05-12 10:33:58 +0000710 case ARM::AK_ARMV4T:
711 case ARM::AK_ARMV5T:
712 case ARM::AK_ARMV5TE:
713 case ARM::AK_ARMV6:
714 case ARM::AK_ARMV6J:
Logan Chien439e8f92013-12-11 17:16:25 +0000715 setAttributeItem(ARM_ISA_use, Allowed, false);
716 setAttributeItem(THUMB_ISA_use, Allowed, false);
717 break;
718
Renato Golin35de35d2015-05-12 10:33:58 +0000719 case ARM::AK_ARMV6T2:
Logan Chien439e8f92013-12-11 17:16:25 +0000720 setAttributeItem(ARM_ISA_use, Allowed, false);
721 setAttributeItem(THUMB_ISA_use, AllowThumb32, false);
722 break;
723
Renato Golin35de35d2015-05-12 10:33:58 +0000724 case ARM::AK_ARMV6K:
725 case ARM::AK_ARMV6Z:
726 case ARM::AK_ARMV6ZK:
Logan Chien439e8f92013-12-11 17:16:25 +0000727 setAttributeItem(ARM_ISA_use, Allowed, false);
728 setAttributeItem(THUMB_ISA_use, Allowed, false);
729 setAttributeItem(Virtualization_use, AllowTZ, false);
730 break;
731
Renato Golin35de35d2015-05-12 10:33:58 +0000732 case ARM::AK_ARMV6M:
Logan Chien439e8f92013-12-11 17:16:25 +0000733 setAttributeItem(THUMB_ISA_use, Allowed, false);
734 break;
735
Renato Golin35de35d2015-05-12 10:33:58 +0000736 case ARM::AK_ARMV7:
Logan Chien439e8f92013-12-11 17:16:25 +0000737 setAttributeItem(THUMB_ISA_use, AllowThumb32, false);
738 break;
739
Renato Golin35de35d2015-05-12 10:33:58 +0000740 case ARM::AK_ARMV7A:
Logan Chien439e8f92013-12-11 17:16:25 +0000741 setAttributeItem(CPU_arch_profile, ApplicationProfile, false);
742 setAttributeItem(ARM_ISA_use, Allowed, false);
743 setAttributeItem(THUMB_ISA_use, AllowThumb32, false);
744 break;
745
Renato Golin35de35d2015-05-12 10:33:58 +0000746 case ARM::AK_ARMV7R:
Logan Chien439e8f92013-12-11 17:16:25 +0000747 setAttributeItem(CPU_arch_profile, RealTimeProfile, false);
748 setAttributeItem(ARM_ISA_use, Allowed, false);
749 setAttributeItem(THUMB_ISA_use, AllowThumb32, false);
750 break;
751
Renato Golin35de35d2015-05-12 10:33:58 +0000752 case ARM::AK_ARMV7M:
Logan Chien439e8f92013-12-11 17:16:25 +0000753 setAttributeItem(CPU_arch_profile, MicroControllerProfile, false);
754 setAttributeItem(THUMB_ISA_use, AllowThumb32, false);
755 break;
756
Renato Golin35de35d2015-05-12 10:33:58 +0000757 case ARM::AK_ARMV8A:
758 case ARM::AK_ARMV8_1A:
Logan Chien439e8f92013-12-11 17:16:25 +0000759 setAttributeItem(CPU_arch_profile, ApplicationProfile, false);
760 setAttributeItem(ARM_ISA_use, Allowed, false);
761 setAttributeItem(THUMB_ISA_use, AllowThumb32, false);
762 setAttributeItem(MPextension_use, Allowed, false);
763 setAttributeItem(Virtualization_use, AllowTZVirtualization, false);
764 break;
765
Renato Golin35de35d2015-05-12 10:33:58 +0000766 case ARM::AK_IWMMXT:
Logan Chien439e8f92013-12-11 17:16:25 +0000767 setAttributeItem(ARM_ISA_use, Allowed, false);
768 setAttributeItem(THUMB_ISA_use, Allowed, false);
769 setAttributeItem(WMMX_arch, AllowWMMXv1, false);
770 break;
771
Renato Golin35de35d2015-05-12 10:33:58 +0000772 case ARM::AK_IWMMXT2:
Logan Chien439e8f92013-12-11 17:16:25 +0000773 setAttributeItem(ARM_ISA_use, Allowed, false);
774 setAttributeItem(THUMB_ISA_use, Allowed, false);
775 setAttributeItem(WMMX_arch, AllowWMMXv2, false);
776 break;
777
778 default:
779 report_fatal_error("Unknown Arch: " + Twine(Arch));
780 break;
781 }
782}
Logan Chien8cbb80d2013-10-28 17:51:12 +0000783void ARMTargetELFStreamer::emitFPU(unsigned Value) {
784 FPU = Value;
785}
786void ARMTargetELFStreamer::emitFPUDefaultAttributes() {
787 switch (FPU) {
Renato Golin35de35d2015-05-12 10:33:58 +0000788 case ARM::FK_VFP:
789 case ARM::FK_VFPV2:
Logan Chiena39510a2013-12-18 17:23:15 +0000790 setAttributeItem(ARMBuildAttrs::FP_arch,
Logan Chien8cbb80d2013-10-28 17:51:12 +0000791 ARMBuildAttrs::AllowFPv2,
792 /* OverwriteExisting= */ false);
793 break;
794
Renato Golin35de35d2015-05-12 10:33:58 +0000795 case ARM::FK_VFPV3:
Logan Chiena39510a2013-12-18 17:23:15 +0000796 setAttributeItem(ARMBuildAttrs::FP_arch,
Logan Chien8cbb80d2013-10-28 17:51:12 +0000797 ARMBuildAttrs::AllowFPv3A,
798 /* OverwriteExisting= */ false);
799 break;
800
Renato Golin35de35d2015-05-12 10:33:58 +0000801 case ARM::FK_VFPV3_D16:
Logan Chiena39510a2013-12-18 17:23:15 +0000802 setAttributeItem(ARMBuildAttrs::FP_arch,
Logan Chien8cbb80d2013-10-28 17:51:12 +0000803 ARMBuildAttrs::AllowFPv3B,
804 /* OverwriteExisting= */ false);
805 break;
806
Renato Golin35de35d2015-05-12 10:33:58 +0000807 case ARM::FK_VFPV4:
Logan Chiena39510a2013-12-18 17:23:15 +0000808 setAttributeItem(ARMBuildAttrs::FP_arch,
Logan Chien8cbb80d2013-10-28 17:51:12 +0000809 ARMBuildAttrs::AllowFPv4A,
810 /* OverwriteExisting= */ false);
811 break;
812
John Brawn985c04e2015-06-05 13:31:19 +0000813 // ABI_HardFP_use is handled in ARMAsmPrinter, so _SP_D16 is treated the same
814 // as _D16 here.
815 case ARM::FK_FPV4_SP_D16:
Renato Golin35de35d2015-05-12 10:33:58 +0000816 case ARM::FK_VFPV4_D16:
Logan Chiena39510a2013-12-18 17:23:15 +0000817 setAttributeItem(ARMBuildAttrs::FP_arch,
Logan Chien8cbb80d2013-10-28 17:51:12 +0000818 ARMBuildAttrs::AllowFPv4B,
819 /* OverwriteExisting= */ false);
820 break;
821
Renato Golin35de35d2015-05-12 10:33:58 +0000822 case ARM::FK_FP_ARMV8:
Logan Chiena39510a2013-12-18 17:23:15 +0000823 setAttributeItem(ARMBuildAttrs::FP_arch,
Logan Chien8cbb80d2013-10-28 17:51:12 +0000824 ARMBuildAttrs::AllowFPARMv8A,
825 /* OverwriteExisting= */ false);
826 break;
827
Oliver Stannard37e4daa2014-10-01 09:02:17 +0000828 // FPV5_D16 is identical to FP_ARMV8 except for the number of D registers, so
829 // uses the FP_ARMV8_D16 build attribute.
John Brawn985c04e2015-06-05 13:31:19 +0000830 case ARM::FK_FPV5_SP_D16:
Renato Golin35de35d2015-05-12 10:33:58 +0000831 case ARM::FK_FPV5_D16:
Oliver Stannard37e4daa2014-10-01 09:02:17 +0000832 setAttributeItem(ARMBuildAttrs::FP_arch,
833 ARMBuildAttrs::AllowFPARMv8B,
834 /* OverwriteExisting= */ false);
835 break;
836
Renato Golin35de35d2015-05-12 10:33:58 +0000837 case ARM::FK_NEON:
Logan Chiena39510a2013-12-18 17:23:15 +0000838 setAttributeItem(ARMBuildAttrs::FP_arch,
Logan Chien8cbb80d2013-10-28 17:51:12 +0000839 ARMBuildAttrs::AllowFPv3A,
840 /* OverwriteExisting= */ false);
841 setAttributeItem(ARMBuildAttrs::Advanced_SIMD_arch,
842 ARMBuildAttrs::AllowNeon,
843 /* OverwriteExisting= */ false);
844 break;
845
Renato Golin35de35d2015-05-12 10:33:58 +0000846 case ARM::FK_NEON_VFPV4:
Logan Chiena39510a2013-12-18 17:23:15 +0000847 setAttributeItem(ARMBuildAttrs::FP_arch,
Logan Chien8cbb80d2013-10-28 17:51:12 +0000848 ARMBuildAttrs::AllowFPv4A,
849 /* OverwriteExisting= */ false);
850 setAttributeItem(ARMBuildAttrs::Advanced_SIMD_arch,
851 ARMBuildAttrs::AllowNeon2,
852 /* OverwriteExisting= */ false);
853 break;
854
Renato Golin35de35d2015-05-12 10:33:58 +0000855 case ARM::FK_NEON_FP_ARMV8:
856 case ARM::FK_CRYPTO_NEON_FP_ARMV8:
Logan Chiena39510a2013-12-18 17:23:15 +0000857 setAttributeItem(ARMBuildAttrs::FP_arch,
Logan Chien8cbb80d2013-10-28 17:51:12 +0000858 ARMBuildAttrs::AllowFPARMv8A,
859 /* OverwriteExisting= */ false);
Vladimir Sukharevc632cda2015-03-26 17:05:54 +0000860 // 'Advanced_SIMD_arch' must be emitted not here, but within
861 // ARMAsmPrinter::emitAttributes(), depending on hasV8Ops() and hasV8_1a()
Logan Chien8cbb80d2013-10-28 17:51:12 +0000862 break;
863
Renato Golin35de35d2015-05-12 10:33:58 +0000864 case ARM::FK_SOFTVFP:
John Brawn985c04e2015-06-05 13:31:19 +0000865 case ARM::FK_NONE:
Logan Chien05ae7442014-01-02 15:50:02 +0000866 break;
867
Logan Chien8cbb80d2013-10-28 17:51:12 +0000868 default:
869 report_fatal_error("Unknown FPU: " + Twine(FPU));
870 break;
871 }
872}
873size_t ARMTargetELFStreamer::calculateContentSize() const {
874 size_t Result = 0;
875 for (size_t i = 0; i < Contents.size(); ++i) {
876 AttributeItem item = Contents[i];
877 switch (item.Type) {
878 case AttributeItem::HiddenAttribute:
879 break;
880 case AttributeItem::NumericAttribute:
Logan Chien5b776b72014-02-22 14:00:39 +0000881 Result += getULEB128Size(item.Tag);
882 Result += getULEB128Size(item.IntValue);
Logan Chien8cbb80d2013-10-28 17:51:12 +0000883 break;
884 case AttributeItem::TextAttribute:
Logan Chien5b776b72014-02-22 14:00:39 +0000885 Result += getULEB128Size(item.Tag);
Logan Chien8cbb80d2013-10-28 17:51:12 +0000886 Result += item.StringValue.size() + 1; // string + '\0'
887 break;
Saleem Abdulrasool87ccd362014-01-07 02:28:42 +0000888 case AttributeItem::NumericAndTextAttributes:
Logan Chien5b776b72014-02-22 14:00:39 +0000889 Result += getULEB128Size(item.Tag);
890 Result += getULEB128Size(item.IntValue);
Saleem Abdulrasool87ccd362014-01-07 02:28:42 +0000891 Result += item.StringValue.size() + 1; // string + '\0';
892 break;
Logan Chien8cbb80d2013-10-28 17:51:12 +0000893 }
894 }
895 return Result;
896}
897void ARMTargetELFStreamer::finishAttributeSection() {
898 // <format-version>
899 // [ <section-length> "vendor-name"
900 // [ <file-tag> <size> <attribute>*
901 // | <section-tag> <size> <section-number>* 0 <attribute>*
902 // | <symbol-tag> <size> <symbol-number>* 0 <attribute>*
903 // ]+
904 // ]*
905
Renato Golin35de35d2015-05-12 10:33:58 +0000906 if (FPU != ARM::FK_INVALID)
Logan Chien8cbb80d2013-10-28 17:51:12 +0000907 emitFPUDefaultAttributes();
908
Renato Golin35de35d2015-05-12 10:33:58 +0000909 if (Arch != ARM::AK_INVALID)
Logan Chien439e8f92013-12-11 17:16:25 +0000910 emitArchDefaultAttributes();
911
Logan Chien8cbb80d2013-10-28 17:51:12 +0000912 if (Contents.empty())
913 return;
914
915 std::sort(Contents.begin(), Contents.end(), AttributeItem::LessTag);
916
917 ARMELFStreamer &Streamer = getStreamer();
918
919 // Switch to .ARM.attributes section
920 if (AttributeSection) {
921 Streamer.SwitchSection(AttributeSection);
922 } else {
Rafael Espindolaba31e272015-01-29 17:33:21 +0000923 AttributeSection = Streamer.getContext().getELFSection(
924 ".ARM.attributes", ELF::SHT_ARM_ATTRIBUTES, 0);
Logan Chien8cbb80d2013-10-28 17:51:12 +0000925 Streamer.SwitchSection(AttributeSection);
926
927 // Format version
928 Streamer.EmitIntValue(0x41, 1);
929 }
930
931 // Vendor size + Vendor name + '\0'
932 const size_t VendorHeaderSize = 4 + CurrentVendor.size() + 1;
933
934 // Tag + Tag Size
935 const size_t TagHeaderSize = 1 + 4;
936
937 const size_t ContentsSize = calculateContentSize();
938
939 Streamer.EmitIntValue(VendorHeaderSize + TagHeaderSize + ContentsSize, 4);
940 Streamer.EmitBytes(CurrentVendor);
941 Streamer.EmitIntValue(0, 1); // '\0'
942
943 Streamer.EmitIntValue(ARMBuildAttrs::File, 1);
944 Streamer.EmitIntValue(TagHeaderSize + ContentsSize, 4);
945
946 // Size should have been accounted for already, now
947 // emit each field as its type (ULEB or String)
948 for (size_t i = 0; i < Contents.size(); ++i) {
949 AttributeItem item = Contents[i];
950 Streamer.EmitULEB128IntValue(item.Tag);
951 switch (item.Type) {
952 default: llvm_unreachable("Invalid attribute type");
953 case AttributeItem::NumericAttribute:
954 Streamer.EmitULEB128IntValue(item.IntValue);
955 break;
956 case AttributeItem::TextAttribute:
Charlie Turner8d433692014-11-27 12:13:56 +0000957 Streamer.EmitBytes(item.StringValue);
Logan Chien8cbb80d2013-10-28 17:51:12 +0000958 Streamer.EmitIntValue(0, 1); // '\0'
959 break;
Saleem Abdulrasool87ccd362014-01-07 02:28:42 +0000960 case AttributeItem::NumericAndTextAttributes:
961 Streamer.EmitULEB128IntValue(item.IntValue);
Charlie Turner8d433692014-11-27 12:13:56 +0000962 Streamer.EmitBytes(item.StringValue);
Saleem Abdulrasool87ccd362014-01-07 02:28:42 +0000963 Streamer.EmitIntValue(0, 1); // '\0'
964 break;
Logan Chien8cbb80d2013-10-28 17:51:12 +0000965 }
966 }
967
968 Contents.clear();
Renato Golin35de35d2015-05-12 10:33:58 +0000969 FPU = ARM::FK_INVALID;
Logan Chien8cbb80d2013-10-28 17:51:12 +0000970}
Rafael Espindola4c6f6132014-04-27 17:10:46 +0000971
972void ARMTargetELFStreamer::emitLabel(MCSymbol *Symbol) {
973 ARMELFStreamer &Streamer = getStreamer();
974 if (!Streamer.IsThumb)
975 return;
976
Rafael Espindolac73aed12015-06-03 19:03:11 +0000977 Streamer.getAssembler().registerSymbol(*Symbol);
Rafael Espindola95fb9b92015-06-02 20:38:46 +0000978 unsigned Type = cast<MCSymbolELF>(Symbol)->getType();
Rafael Espindolaf8794ff2015-06-04 00:47:43 +0000979 if (Type == ELF::STT_FUNC || Type == ELF::STT_GNU_IFUNC)
Rafael Espindola4c6f6132014-04-27 17:10:46 +0000980 Streamer.EmitThumbFunc(Symbol);
981}
982
Saleem Abdulrasool56e06e82014-01-30 04:02:47 +0000983void
984ARMTargetELFStreamer::AnnotateTLSDescriptorSequence(const MCSymbolRefExpr *S) {
985 getStreamer().EmitFixup(S, FK_Data_4);
986}
Rafael Espindola466d6632014-04-27 20:23:58 +0000987
988void ARMTargetELFStreamer::emitThumbSet(MCSymbol *Symbol, const MCExpr *Value) {
989 if (const MCSymbolRefExpr *SRE = dyn_cast<MCSymbolRefExpr>(Value)) {
Rafael Espindola4a042942014-05-01 12:45:43 +0000990 const MCSymbol &Sym = SRE->getSymbol();
991 if (!Sym.isDefined()) {
Rafael Espindola466d6632014-04-27 20:23:58 +0000992 getStreamer().EmitAssignment(Symbol, Value);
993 return;
994 }
995 }
996
997 getStreamer().EmitThumbFunc(Symbol);
998 getStreamer().EmitAssignment(Symbol, Value);
999}
1000
Saleem Abdulrasoolc0da2cb2013-12-19 05:17:58 +00001001void ARMTargetELFStreamer::emitInst(uint32_t Inst, char Suffix) {
1002 getStreamer().emitInst(Inst, Suffix);
1003}
Logan Chien8cbb80d2013-10-28 17:51:12 +00001004
1005void ARMELFStreamer::FinishImpl() {
Rafael Espindola4a1a3602014-01-14 01:21:46 +00001006 MCTargetStreamer &TS = *getTargetStreamer();
Logan Chien8cbb80d2013-10-28 17:51:12 +00001007 ARMTargetStreamer &ATS = static_cast<ARMTargetStreamer &>(TS);
1008 ATS.finishAttributeSection();
1009
1010 MCELFStreamer::FinishImpl();
1011}
Rafael Espindolaa17151a2013-10-08 13:08:17 +00001012
Logan Chien2bcc42c2013-01-30 15:39:04 +00001013inline void ARMELFStreamer::SwitchToEHSection(const char *Prefix,
1014 unsigned Type,
1015 unsigned Flags,
1016 SectionKind Kind,
1017 const MCSymbol &Fn) {
1018 const MCSectionELF &FnSection =
1019 static_cast<const MCSectionELF &>(Fn.getSection());
1020
1021 // Create the name for new section
1022 StringRef FnSecName(FnSection.getSectionName());
1023 SmallString<128> EHSecName(Prefix);
1024 if (FnSecName != ".text") {
1025 EHSecName += FnSecName;
1026 }
1027
1028 // Get .ARM.extab or .ARM.exidx section
Rafael Espindola0ccf9b72015-06-02 21:30:13 +00001029 const MCSymbolELF *Group = FnSection.getGroup();
Rafael Espindola61e8ce32015-04-06 04:25:18 +00001030 if (Group)
1031 Flags |= ELF::SHF_GROUP;
Rafael Espindola0709a7b2015-05-21 19:20:38 +00001032 MCSectionELF *EHSection =
Rafael Espindola61e8ce32015-04-06 04:25:18 +00001033 getContext().getELFSection(EHSecName, Type, Flags, 0, Group,
1034 FnSection.getUniqueID(), nullptr, &FnSection);
1035
Logan Chiend8bb4b72013-04-16 12:02:21 +00001036 assert(EHSection && "Failed to get the required EH section");
Logan Chien2bcc42c2013-01-30 15:39:04 +00001037
1038 // Switch to .ARM.extab or .ARM.exidx section
1039 SwitchSection(EHSection);
Rafael Espindola7b514962014-02-04 18:34:04 +00001040 EmitCodeAlignment(4);
Logan Chien2bcc42c2013-01-30 15:39:04 +00001041}
1042
1043inline void ARMELFStreamer::SwitchToExTabSection(const MCSymbol &FnStart) {
1044 SwitchToEHSection(".ARM.extab",
1045 ELF::SHT_PROGBITS,
1046 ELF::SHF_ALLOC,
1047 SectionKind::getDataRel(),
1048 FnStart);
1049}
1050
1051inline void ARMELFStreamer::SwitchToExIdxSection(const MCSymbol &FnStart) {
1052 SwitchToEHSection(".ARM.exidx",
1053 ELF::SHT_ARM_EXIDX,
1054 ELF::SHF_ALLOC | ELF::SHF_LINK_ORDER,
1055 SectionKind::getDataRel(),
1056 FnStart);
1057}
Saleem Abdulrasool56e06e82014-01-30 04:02:47 +00001058void ARMELFStreamer::EmitFixup(const MCExpr *Expr, MCFixupKind Kind) {
1059 MCDataFragment *Frag = getOrCreateDataFragment();
Jim Grosbach63661f82015-05-15 19:13:05 +00001060 Frag->getFixups().push_back(MCFixup::create(Frag->getContents().size(), Expr,
Saleem Abdulrasool56e06e82014-01-30 04:02:47 +00001061 Kind));
1062}
Logan Chien2bcc42c2013-01-30 15:39:04 +00001063
1064void ARMELFStreamer::Reset() {
Craig Topper062a2ba2014-04-25 05:30:21 +00001065 ExTab = nullptr;
1066 FnStart = nullptr;
1067 Personality = nullptr;
Saleem Abdulrasoolb961c992014-01-06 00:15:00 +00001068 PersonalityIndex = ARM::EHABI::NUM_PERSONALITY_INDEX;
Logan Chien325823a2013-06-09 12:22:30 +00001069 FPReg = ARM::SP;
Logan Chiend8bb4b72013-04-16 12:02:21 +00001070 FPOffset = 0;
1071 SPOffset = 0;
Logan Chien325823a2013-06-09 12:22:30 +00001072 PendingOffset = 0;
Logan Chiend8bb4b72013-04-16 12:02:21 +00001073 UsedFP = false;
Logan Chien2bcc42c2013-01-30 15:39:04 +00001074 CantUnwind = false;
Logan Chiend8bb4b72013-04-16 12:02:21 +00001075
Logan Chien325823a2013-06-09 12:22:30 +00001076 Opcodes.clear();
Logan Chiend8bb4b72013-04-16 12:02:21 +00001077 UnwindOpAsm.Reset();
Logan Chien2bcc42c2013-01-30 15:39:04 +00001078}
1079
Rafael Espindolaa17151a2013-10-08 13:08:17 +00001080void ARMELFStreamer::emitFnStart() {
Craig Toppere73658d2014-04-28 04:05:08 +00001081 assert(FnStart == nullptr);
Jim Grosbach6f482002015-05-18 18:43:14 +00001082 FnStart = getContext().createTempSymbol();
Logan Chien2bcc42c2013-01-30 15:39:04 +00001083 EmitLabel(FnStart);
1084}
1085
Rafael Espindolaa17151a2013-10-08 13:08:17 +00001086void ARMELFStreamer::emitFnEnd() {
Alp Tokercb402912014-01-24 17:20:08 +00001087 assert(FnStart && ".fnstart must precedes .fnend");
Logan Chien2bcc42c2013-01-30 15:39:04 +00001088
1089 // Emit unwind opcodes if there is no .handlerdata directive
Logan Chien4ea23b52013-05-10 16:17:24 +00001090 if (!ExTab && !CantUnwind)
1091 FlushUnwindOpcodes(true);
Logan Chien2bcc42c2013-01-30 15:39:04 +00001092
1093 // Emit the exception index table entry
1094 SwitchToExIdxSection(*FnStart);
1095
Saleem Abdulrasoolb961c992014-01-06 00:15:00 +00001096 if (PersonalityIndex < ARM::EHABI::NUM_PERSONALITY_INDEX)
Logan Chiend8bb4b72013-04-16 12:02:21 +00001097 EmitPersonalityFixup(GetAEABIUnwindPersonalityName(PersonalityIndex));
Logan Chien2bcc42c2013-01-30 15:39:04 +00001098
1099 const MCSymbolRefExpr *FnStartRef =
Jim Grosbach13760bd2015-05-30 01:25:56 +00001100 MCSymbolRefExpr::create(FnStart,
Logan Chien2bcc42c2013-01-30 15:39:04 +00001101 MCSymbolRefExpr::VK_ARM_PREL31,
1102 getContext());
1103
Rafael Espindola64e1af82013-07-02 15:49:13 +00001104 EmitValue(FnStartRef, 4);
Logan Chien2bcc42c2013-01-30 15:39:04 +00001105
1106 if (CantUnwind) {
Saleem Abdulrasoolb961c992014-01-06 00:15:00 +00001107 EmitIntValue(ARM::EHABI::EXIDX_CANTUNWIND, 4);
Logan Chiend8bb4b72013-04-16 12:02:21 +00001108 } else if (ExTab) {
1109 // Emit a reference to the unwind opcodes in the ".ARM.extab" section.
Logan Chien2bcc42c2013-01-30 15:39:04 +00001110 const MCSymbolRefExpr *ExTabEntryRef =
Jim Grosbach13760bd2015-05-30 01:25:56 +00001111 MCSymbolRefExpr::create(ExTab,
Logan Chien2bcc42c2013-01-30 15:39:04 +00001112 MCSymbolRefExpr::VK_ARM_PREL31,
1113 getContext());
Rafael Espindola64e1af82013-07-02 15:49:13 +00001114 EmitValue(ExTabEntryRef, 4);
Logan Chiend8bb4b72013-04-16 12:02:21 +00001115 } else {
1116 // For the __aeabi_unwind_cpp_pr0, we have to emit the unwind opcodes in
1117 // the second word of exception index table entry. The size of the unwind
1118 // opcodes should always be 4 bytes.
Saleem Abdulrasoolb961c992014-01-06 00:15:00 +00001119 assert(PersonalityIndex == ARM::EHABI::AEABI_UNWIND_CPP_PR0 &&
Jonathan Roelofs4971b402014-05-15 02:24:50 +00001120 "Compact model must use __aeabi_unwind_cpp_pr0 as personality");
Logan Chien325823a2013-06-09 12:22:30 +00001121 assert(Opcodes.size() == 4u &&
Jonathan Roelofs4971b402014-05-15 02:24:50 +00001122 "Unwind opcode size for __aeabi_unwind_cpp_pr0 must be equal to 4");
Christian Pirker39db7ec2014-05-13 16:44:30 +00001123 uint64_t Intval = Opcodes[0] |
1124 Opcodes[1] << 8 |
1125 Opcodes[2] << 16 |
1126 Opcodes[3] << 24;
1127 EmitIntValue(Intval, Opcodes.size());
Logan Chien2bcc42c2013-01-30 15:39:04 +00001128 }
1129
Logan Chien4ea23b52013-05-10 16:17:24 +00001130 // Switch to the section containing FnStart
1131 SwitchSection(&FnStart->getSection());
1132
Logan Chien2bcc42c2013-01-30 15:39:04 +00001133 // Clean exception handling frame information
1134 Reset();
1135}
1136
Rafael Espindolaa17151a2013-10-08 13:08:17 +00001137void ARMELFStreamer::emitCantUnwind() { CantUnwind = true; }
1138
1139// Add the R_ARM_NONE fixup at the same position
1140void ARMELFStreamer::EmitPersonalityFixup(StringRef Name) {
Jim Grosbach6f482002015-05-18 18:43:14 +00001141 const MCSymbol *PersonalitySym = getContext().getOrCreateSymbol(Name);
Rafael Espindolaa17151a2013-10-08 13:08:17 +00001142
Jim Grosbach13760bd2015-05-30 01:25:56 +00001143 const MCSymbolRefExpr *PersonalityRef = MCSymbolRefExpr::create(
Rafael Espindolaa17151a2013-10-08 13:08:17 +00001144 PersonalitySym, MCSymbolRefExpr::VK_ARM_NONE, getContext());
1145
Rafael Espindola2be12812014-06-25 15:29:54 +00001146 visitUsedExpr(*PersonalityRef);
Rafael Espindolaa17151a2013-10-08 13:08:17 +00001147 MCDataFragment *DF = getOrCreateDataFragment();
Jim Grosbach63661f82015-05-15 19:13:05 +00001148 DF->getFixups().push_back(MCFixup::create(DF->getContents().size(),
Rafael Espindolaa17151a2013-10-08 13:08:17 +00001149 PersonalityRef,
1150 MCFixup::getKindForSize(4, false)));
Logan Chien2bcc42c2013-01-30 15:39:04 +00001151}
1152
Logan Chien325823a2013-06-09 12:22:30 +00001153void ARMELFStreamer::FlushPendingOffset() {
1154 if (PendingOffset != 0) {
1155 UnwindOpAsm.EmitSPOffset(-PendingOffset);
1156 PendingOffset = 0;
1157 }
1158}
1159
Logan Chienc931fce2013-07-02 12:43:27 +00001160void ARMELFStreamer::FlushUnwindOpcodes(bool NoHandlerData) {
Logan Chien325823a2013-06-09 12:22:30 +00001161 // Emit the unwind opcode to restore $sp.
1162 if (UsedFP) {
Bill Wendlingbc07a892013-06-18 07:20:20 +00001163 const MCRegisterInfo *MRI = getContext().getRegisterInfo();
Logan Chien325823a2013-06-09 12:22:30 +00001164 int64_t LastRegSaveSPOffset = SPOffset - PendingOffset;
1165 UnwindOpAsm.EmitSPOffset(LastRegSaveSPOffset - FPOffset);
Bill Wendlingbc07a892013-06-18 07:20:20 +00001166 UnwindOpAsm.EmitSetSP(MRI->getEncodingValue(FPReg));
Logan Chien325823a2013-06-09 12:22:30 +00001167 } else {
1168 FlushPendingOffset();
1169 }
1170
1171 // Finalize the unwind opcode sequence
1172 UnwindOpAsm.Finalize(PersonalityIndex, Opcodes);
Logan Chien4ea23b52013-05-10 16:17:24 +00001173
1174 // For compact model 0, we have to emit the unwind opcodes in the .ARM.exidx
1175 // section. Thus, we don't have to create an entry in the .ARM.extab
1176 // section.
Saleem Abdulrasoolb961c992014-01-06 00:15:00 +00001177 if (NoHandlerData && PersonalityIndex == ARM::EHABI::AEABI_UNWIND_CPP_PR0)
Logan Chien4ea23b52013-05-10 16:17:24 +00001178 return;
1179
1180 // Switch to .ARM.extab section.
Logan Chien2bcc42c2013-01-30 15:39:04 +00001181 SwitchToExTabSection(*FnStart);
1182
1183 // Create .ARM.extab label for offset in .ARM.exidx
1184 assert(!ExTab);
Jim Grosbach6f482002015-05-18 18:43:14 +00001185 ExTab = getContext().createTempSymbol();
Logan Chien2bcc42c2013-01-30 15:39:04 +00001186 EmitLabel(ExTab);
1187
Logan Chien4ea23b52013-05-10 16:17:24 +00001188 // Emit personality
1189 if (Personality) {
1190 const MCSymbolRefExpr *PersonalityRef =
Jim Grosbach13760bd2015-05-30 01:25:56 +00001191 MCSymbolRefExpr::create(Personality,
Logan Chien4ea23b52013-05-10 16:17:24 +00001192 MCSymbolRefExpr::VK_ARM_PREL31,
1193 getContext());
Logan Chien2bcc42c2013-01-30 15:39:04 +00001194
Rafael Espindola64e1af82013-07-02 15:49:13 +00001195 EmitValue(PersonalityRef, 4);
Logan Chien4ea23b52013-05-10 16:17:24 +00001196 }
Logan Chien2bcc42c2013-01-30 15:39:04 +00001197
1198 // Emit unwind opcodes
Christian Pirker39db7ec2014-05-13 16:44:30 +00001199 assert((Opcodes.size() % 4) == 0 &&
1200 "Unwind opcode size for __aeabi_cpp_unwind_pr0 must be multiple of 4");
1201 for (unsigned I = 0; I != Opcodes.size(); I += 4) {
1202 uint64_t Intval = Opcodes[I] |
1203 Opcodes[I + 1] << 8 |
1204 Opcodes[I + 2] << 16 |
1205 Opcodes[I + 3] << 24;
1206 EmitIntValue(Intval, 4);
1207 }
Logan Chienc931fce2013-07-02 12:43:27 +00001208
1209 // According to ARM EHABI section 9.2, if the __aeabi_unwind_cpp_pr1() or
1210 // __aeabi_unwind_cpp_pr2() is used, then the handler data must be emitted
1211 // after the unwind opcodes. The handler data consists of several 32-bit
1212 // words, and should be terminated by zero.
1213 //
1214 // In case that the .handlerdata directive is not specified by the
1215 // programmer, we should emit zero to terminate the handler data.
1216 if (NoHandlerData && !Personality)
1217 EmitIntValue(0, 4);
Logan Chien2bcc42c2013-01-30 15:39:04 +00001218}
1219
Rafael Espindolaa17151a2013-10-08 13:08:17 +00001220void ARMELFStreamer::emitHandlerData() { FlushUnwindOpcodes(false); }
Logan Chien4ea23b52013-05-10 16:17:24 +00001221
Rafael Espindolaa17151a2013-10-08 13:08:17 +00001222void ARMELFStreamer::emitPersonality(const MCSymbol *Per) {
Logan Chien2bcc42c2013-01-30 15:39:04 +00001223 Personality = Per;
Logan Chiend8bb4b72013-04-16 12:02:21 +00001224 UnwindOpAsm.setPersonality(Per);
Logan Chien2bcc42c2013-01-30 15:39:04 +00001225}
1226
Saleem Abdulrasool662f5c12014-01-21 02:33:02 +00001227void ARMELFStreamer::emitPersonalityIndex(unsigned Index) {
1228 assert(Index < ARM::EHABI::NUM_PERSONALITY_INDEX && "invalid index");
1229 PersonalityIndex = Index;
1230}
1231
Rafael Espindolaa17151a2013-10-08 13:08:17 +00001232void ARMELFStreamer::emitSetFP(unsigned NewFPReg, unsigned NewSPReg,
Logan Chien2bcc42c2013-01-30 15:39:04 +00001233 int64_t Offset) {
Logan Chien325823a2013-06-09 12:22:30 +00001234 assert((NewSPReg == ARM::SP || NewSPReg == FPReg) &&
Logan Chiend8bb4b72013-04-16 12:02:21 +00001235 "the operand of .setfp directive should be either $sp or $fp");
1236
1237 UsedFP = true;
Logan Chien325823a2013-06-09 12:22:30 +00001238 FPReg = NewFPReg;
1239
1240 if (NewSPReg == ARM::SP)
1241 FPOffset = SPOffset + Offset;
1242 else
1243 FPOffset += Offset;
Logan Chien2bcc42c2013-01-30 15:39:04 +00001244}
1245
Saleem Abdulrasool5d962d32014-01-30 04:46:24 +00001246void ARMELFStreamer::emitMovSP(unsigned Reg, int64_t Offset) {
1247 assert((Reg != ARM::SP && Reg != ARM::PC) &&
1248 "the operand of .movsp cannot be either sp or pc");
1249 assert(FPReg == ARM::SP && "current FP must be SP");
1250
1251 FlushPendingOffset();
1252
1253 FPReg = Reg;
1254 FPOffset = SPOffset + Offset;
1255
1256 const MCRegisterInfo *MRI = getContext().getRegisterInfo();
1257 UnwindOpAsm.EmitSetSP(MRI->getEncodingValue(FPReg));
1258}
1259
Rafael Espindolaa17151a2013-10-08 13:08:17 +00001260void ARMELFStreamer::emitPad(int64_t Offset) {
Logan Chien325823a2013-06-09 12:22:30 +00001261 // Track the change of the $sp offset
1262 SPOffset -= Offset;
1263
1264 // To squash multiple .pad directives, we should delay the unwind opcode
1265 // until the .save, .vsave, .handlerdata, or .fnend directives.
1266 PendingOffset -= Offset;
Logan Chien2bcc42c2013-01-30 15:39:04 +00001267}
1268
Rafael Espindolaa17151a2013-10-08 13:08:17 +00001269void ARMELFStreamer::emitRegSave(const SmallVectorImpl<unsigned> &RegList,
Logan Chien2bcc42c2013-01-30 15:39:04 +00001270 bool IsVector) {
Logan Chien325823a2013-06-09 12:22:30 +00001271 // Collect the registers in the register list
1272 unsigned Count = 0;
1273 uint32_t Mask = 0;
Bill Wendlingbc07a892013-06-18 07:20:20 +00001274 const MCRegisterInfo *MRI = getContext().getRegisterInfo();
Logan Chiend8bb4b72013-04-16 12:02:21 +00001275 for (size_t i = 0; i < RegList.size(); ++i) {
Bill Wendlingbc07a892013-06-18 07:20:20 +00001276 unsigned Reg = MRI->getEncodingValue(RegList[i]);
Aaron Ballmanab1d27e2013-06-10 16:45:40 +00001277 assert(Reg < (IsVector ? 32U : 16U) && "Register out of range");
Logan Chien325823a2013-06-09 12:22:30 +00001278 unsigned Bit = (1u << Reg);
1279 if ((Mask & Bit) == 0) {
1280 Mask |= Bit;
1281 ++Count;
1282 }
Logan Chiend8bb4b72013-04-16 12:02:21 +00001283 }
Logan Chien325823a2013-06-09 12:22:30 +00001284
1285 // Track the change the $sp offset: For the .save directive, the
1286 // corresponding push instruction will decrease the $sp by (4 * Count).
1287 // For the .vsave directive, the corresponding vpush instruction will
1288 // decrease $sp by (8 * Count).
1289 SPOffset -= Count * (IsVector ? 8 : 4);
1290
1291 // Emit the opcode
1292 FlushPendingOffset();
1293 if (IsVector)
1294 UnwindOpAsm.EmitVFPRegSave(Mask);
1295 else
1296 UnwindOpAsm.EmitRegSave(Mask);
Logan Chien2bcc42c2013-01-30 15:39:04 +00001297}
1298
Saleem Abdulrasoold9f08602014-01-21 02:33:10 +00001299void ARMELFStreamer::emitUnwindRaw(int64_t Offset,
1300 const SmallVectorImpl<uint8_t> &Opcodes) {
1301 FlushPendingOffset();
1302 SPOffset = SPOffset - Offset;
1303 UnwindOpAsm.EmitRaw(Opcodes);
1304}
1305
Tim Northover5cc3dc82012-12-07 16:50:23 +00001306namespace llvm {
Rafael Espindolaa17151a2013-10-08 13:08:17 +00001307
Rafael Espindola73870dd2015-03-16 21:43:42 +00001308MCTargetStreamer *createARMTargetAsmStreamer(MCStreamer &S,
1309 formatted_raw_ostream &OS,
1310 MCInstPrinter *InstPrint,
1311 bool isVerboseAsm) {
1312 return new ARMTargetAsmStreamer(S, OS, *InstPrint, isVerboseAsm);
Rafael Espindolaa17151a2013-10-08 13:08:17 +00001313}
1314
Peter Collingbourne20c72592015-02-19 00:45:02 +00001315MCTargetStreamer *createARMNullTargetStreamer(MCStreamer &S) {
1316 return new ARMTargetStreamer(S);
1317}
1318
Rafael Espindolacd584a82015-03-19 01:50:16 +00001319MCTargetStreamer *createARMObjectTargetStreamer(MCStreamer &S,
1320 const MCSubtargetInfo &STI) {
1321 Triple TT(STI.getTargetTriple());
1322 if (TT.getObjectFormat() == Triple::ELF)
1323 return new ARMTargetELFStreamer(S);
1324 return new ARMTargetStreamer(S);
1325}
1326
Rafael Espindola7b61ddf2014-10-15 16:12:52 +00001327MCELFStreamer *createARMELFStreamer(MCContext &Context, MCAsmBackend &TAB,
Rafael Espindola5560a4c2015-04-14 22:14:34 +00001328 raw_pwrite_stream &OS,
1329 MCCodeEmitter *Emitter, bool RelaxAll,
1330 bool IsThumb) {
Rafael Espindola24ea09e2014-01-26 06:06:37 +00001331 ARMELFStreamer *S = new ARMELFStreamer(Context, TAB, OS, Emitter, IsThumb);
Rafael Espindolaac4ad252013-10-05 16:42:21 +00001332 // FIXME: This should eventually end up somewhere else where more
1333 // intelligent flag decisions can be made. For now we are just maintaining
1334 // the status quo for ARM and setting EF_ARM_EABI_VER5 as the default.
1335 S->getAssembler().setELFHeaderEFlags(ELF::EF_ARM_EABI_VER5);
1336
Tim Northover5cc3dc82012-12-07 16:50:23 +00001337 if (RelaxAll)
1338 S->getAssembler().setRelaxAll(true);
Tim Northover5cc3dc82012-12-07 16:50:23 +00001339 return S;
1340 }
1341
1342}
1343
1344